1 // SwiftShader Software Renderer
3 // Copyright(c) 2005-2012 TransGaming Inc.
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
6 // transcribed, stored in a retrieval system, translated into any human or computer
7 // language by any means, or disclosed to third parties without the explicit written
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9 // or implied, including but not limited to any patent rights, are granted to you.
12 #ifndef sw_Renderer_hpp
13 #define sw_Renderer_hpp
15 #include "VertexProcessor.hpp"
16 #include "PixelProcessor.hpp"
17 #include "SetupProcessor.hpp"
19 #include "Blitter.hpp"
20 #include "Common/MutexLock.hpp"
21 #include "Common/Thread.hpp"
22 #include "Main/Config.hpp"
37 extern int threadCount;
39 extern int clusterCount;
41 enum TranscendentalPrecision
50 extern TranscendentalPrecision logPrecision;
51 extern TranscendentalPrecision expPrecision;
52 extern TranscendentalPrecision rcpPrecision;
53 extern TranscendentalPrecision rsqPrecision;
54 extern bool perspectiveCorrection;
58 bool halfIntegerCoordinates;
59 bool symmetricNormalizedDepth;
60 bool booleanFaceRegister;
61 bool fullPixelPositionRegister;
62 bool leadingVertexFirst;
65 static const Conventions OpenGL =
67 true, // halfIntegerCoordinates
68 true, // symmetricNormalizedDepth
69 true, // booleanFaceRegister
70 true, // fullPixelPositionRegister
71 false // leadingVertexFirst
95 volatile int reference;
96 volatile unsigned int data;
101 const void *constants;
103 const void *input[VERTEX_ATTRIBUTES];
104 unsigned int stride[VERTEX_ATTRIBUTES];
105 Texture mipmap[TOTAL_IMAGE_UNITS];
110 float4 c[256 + 1]; // One extra for indices out of range, c[256] = {0, 0, 0, 0}
118 float4 c[FRAGMENT_UNIFORM_VECTORS];
126 VertexProcessor::FixedFunction ff;
133 VertexProcessor::PointSprite point;
136 PixelProcessor::Stencil stencil[2]; // clockwise, counterclockwise
137 PixelProcessor::Stencil stencilCCW;
138 PixelProcessor::Fog fog;
139 PixelProcessor::Factor factor;
140 unsigned int occlusion[16]; // Number of pixels passing depth test
143 int64_t cycles[PERF_TIMERS][16];
146 TextureStage::Uniforms textureStage[8];
156 float viewportHeight;
157 float slopeDepthBias;
162 unsigned int *colorBuffer[4];
168 unsigned char *stencilBuffer;
192 Routine *vertexRoutine;
193 Routine *setupRoutine;
194 Routine *pixelRoutine;
196 VertexProcessor::RoutinePointer vertexPointer;
197 SetupProcessor::RoutinePointer setupPointer;
198 PixelProcessor::RoutinePointer pixelPointer;
200 int (*setupPrimitives)(Renderer *renderer, int batch, int count);
201 SetupProcessor::State setupState;
203 Resource *vertexStream[VERTEX_ATTRIBUTES];
204 Resource *indexBuffer;
205 Surface *renderTarget[4];
206 Surface *depthStencil;
207 Resource *texture[TOTAL_IMAGE_UNITS];
217 std::list<Query*> *queries;
221 volatile int primitive; // Current primitive to enter pipeline
222 volatile int count; // Number of primitives to render
223 volatile int references; // Remaining references to this draw call, 0 when done drawing, -1 when resources unlocked and slot is free
238 class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
252 volatile int primitiveUnit;
253 volatile int pixelCluster;
256 struct PrimitiveProgress
267 volatile int drawCall;
268 volatile int firstPrimitive;
269 volatile int primitiveCount;
270 volatile int visible;
271 volatile int references;
279 processedPrimitives = 0;
283 volatile int drawCall;
284 volatile int processedPrimitives;
285 volatile bool executing;
289 Renderer(Context *context, Conventions conventions, bool exactColorRounding);
293 virtual void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
294 virtual void blit3D(Surface *source, Surface *dest);
295 virtual void draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true);
297 virtual void setIndexBuffer(Resource *indexBuffer);
299 virtual void setMultiSampleMask(unsigned int mask);
300 virtual void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
302 virtual void setTextureResource(unsigned int sampler, Resource *resource);
303 virtual void setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type);
305 virtual void setTextureFilter(SamplerType type, int sampler, FilterType textureFilter);
306 virtual void setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter);
307 virtual void setGatherEnable(SamplerType type, int sampler, bool enable);
308 virtual void setAddressingModeU(SamplerType type, int sampler, AddressingMode addressingMode);
309 virtual void setAddressingModeV(SamplerType type, int sampler, AddressingMode addressingMode);
310 virtual void setAddressingModeW(SamplerType type, int sampler, AddressingMode addressingMode);
311 virtual void setReadSRGB(SamplerType type, int sampler, bool sRGB);
312 virtual void setMipmapLOD(SamplerType type, int sampler, float bias);
313 virtual void setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor);
314 virtual void setMaxAnisotropy(SamplerType type, int sampler, float maxAnisotropy);
316 virtual void setPointSpriteEnable(bool pointSpriteEnable);
317 virtual void setPointScaleEnable(bool pointScaleEnable);
318 virtual void setLineWidth(float width);
320 virtual void setDepthBias(float bias);
321 virtual void setSlopeDepthBias(float slopeBias);
323 // Programmable pipelines
324 virtual void setPixelShader(const PixelShader *shader);
325 virtual void setVertexShader(const VertexShader *shader);
327 virtual void setPixelShaderConstantF(int index, const float value[4], int count = 1);
328 virtual void setPixelShaderConstantI(int index, const int value[4], int count = 1);
329 virtual void setPixelShaderConstantB(int index, const int *boolean, int count = 1);
331 virtual void setVertexShaderConstantF(int index, const float value[4], int count = 1);
332 virtual void setVertexShaderConstantI(int index, const int value[4], int count = 1);
333 virtual void setVertexShaderConstantB(int index, const int *boolean, int count = 1);
335 // Viewport & Clipper
336 virtual void setViewport(const Viewport &viewport);
337 virtual void setScissor(const Rect &scissor);
338 virtual void setClipFlags(int flags);
339 virtual void setClipPlane(unsigned int index, const float plane[4]);
342 virtual void setModelMatrix(const Matrix &M, int i = 0);
343 virtual void setViewMatrix(const Matrix &V);
344 virtual void setBaseMatrix(const Matrix &B);
345 virtual void setProjectionMatrix(const Matrix &P);
347 virtual void addQuery(Query *query);
348 virtual void removeQuery(Query *query);
353 // Performance timers
354 int getThreadCount();
355 int64_t getVertexTime(int thread);
356 int64_t getSetupTime(int thread);
357 int64_t getPixelTime(int thread);
362 static void threadFunction(void *parameters);
363 void threadLoop(int threadIndex);
364 void taskLoop(int threadIndex);
365 void findAvailableTasks();
366 void scheduleTask(int threadIndex);
367 void executeTask(int threadIndex);
368 void finishRendering(Task &pixelTask);
370 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
372 static int setupSolidTriangles(Renderer *renderer, int batch, int count);
373 static int setupWireframeTriangle(Renderer *renderer, int batch, int count);
374 static int setupVertexTriangle(Renderer *renderer, int batch, int count);
375 static int setupLines(Renderer *renderer, int batch, int count);
376 static int setupPoints(Renderer *renderer, int batch, int count);
378 static bool setupLine(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
379 static bool setupPoint(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
381 bool isReadWriteTexture(int sampler);
382 void updateClipper();
383 void updateConfiguration(bool initialUpdate = false);
384 static unsigned int computeClipFlags(const float4 &v, const DrawData &data);
385 void initializeThreads();
386 void terminateThreads();
388 void loadConstants(const VertexShader *vertexShader);
389 void loadConstants(const PixelShader *pixelShader);
397 Triangle *triangleBatch[16];
398 Primitive *primitiveBatch[16];
400 // User-defined clipping planes
402 Plane clipPlane[6]; // Tranformed to clip space
403 bool updateClipPlanes;
405 volatile bool exitThreads;
406 volatile int threadsAwake;
408 Event *resume[16]; // Events for resuming threads
409 Event *suspend[16]; // Events for suspending threads
410 Event *resumeApp; // Event for resuming the application thread
412 PrimitiveProgress primitiveProgress[16];
413 PixelProgress pixelProgress[16];
414 Task task[16]; // Current tasks for threads
416 enum {DRAW_COUNT = 16}; // Number of draw calls buffered
417 DrawCall *drawCall[DRAW_COUNT];
418 DrawCall *drawList[DRAW_COUNT];
420 volatile int currentDraw;
421 volatile int nextDraw;
427 BackoffLock schedulerMutex;
430 int64_t vertexTime[16];
431 int64_t setupTime[16];
432 int64_t pixelTime[16];
435 VertexTask *vertexTask[16];
437 SwiftConfig *swiftConfig;
439 std::list<Query*> queries;
442 VertexProcessor::State vertexState;
443 SetupProcessor::State setupState;
444 PixelProcessor::State pixelState;
448 #endif // sw_Renderer_hpp