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"
38 extern int threadCount;
40 extern int clusterCount;
42 enum TranscendentalPrecision
51 extern TranscendentalPrecision logPrecision;
52 extern TranscendentalPrecision expPrecision;
53 extern TranscendentalPrecision rcpPrecision;
54 extern TranscendentalPrecision rsqPrecision;
55 extern bool perspectiveCorrection;
59 bool halfIntegerCoordinates;
60 bool symmetricNormalizedDepth;
61 bool booleanFaceRegister;
62 bool fullPixelPositionRegister;
63 bool leadingVertexFirst;
67 static const Conventions OpenGL =
69 true, // halfIntegerCoordinates
70 true, // symmetricNormalizedDepth
71 true, // booleanFaceRegister
72 true, // fullPixelPositionRegister
73 false, // leadingVertexFirst
74 false // secondaryColor
77 static const Conventions Direct3D =
79 false, // halfIntegerCoordinates
80 false, // symmetricNormalizedDepth
81 false, // booleanFaceRegister
82 false, // fullPixelPositionRegister
83 true, // leadingVertexFirst
84 true, // secondardyColor
89 enum Type { FRAGMENTS_PASSED, TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN };
91 Query(Type type) : building(false), reference(0), data(0), type(type)
107 volatile int reference;
108 volatile unsigned int data;
115 const Constants *constants;
117 const void *input[VERTEX_ATTRIBUTES];
118 unsigned int stride[VERTEX_ATTRIBUTES];
119 Texture mipmap[TOTAL_IMAGE_UNITS];
124 float4 c[VERTEX_UNIFORM_VECTORS + 1]; // One extra for indices out of range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
125 byte* u[MAX_UNIFORM_BUFFER_BINDINGS];
126 byte* t[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS];
127 unsigned int reg[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Offset used when reading from registers, in components
128 unsigned int row[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of rows to read
129 unsigned int col[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of columns to read
130 unsigned int str[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of components between each varying in output buffer
138 float4 c[FRAGMENT_UNIFORM_VECTORS];
139 byte* u[MAX_UNIFORM_BUFFER_BINDINGS];
147 VertexProcessor::FixedFunction ff;
154 VertexProcessor::PointSprite point;
157 PixelProcessor::Stencil stencil[2]; // clockwise, counterclockwise
158 PixelProcessor::Stencil stencilCCW;
159 PixelProcessor::Fog fog;
160 PixelProcessor::Factor factor;
161 unsigned int occlusion[16]; // Number of pixels passing depth test
164 int64_t cycles[PERF_TIMERS][16];
167 TextureStage::Uniforms textureStage[8];
177 float viewportHeight;
178 float slopeDepthBias;
183 unsigned int *colorBuffer[RENDERTARGETS];
184 int colorPitchB[RENDERTARGETS];
185 int colorSliceB[RENDERTARGETS];
189 unsigned char *stencilBuffer;
213 Routine *vertexRoutine;
214 Routine *setupRoutine;
215 Routine *pixelRoutine;
217 VertexProcessor::RoutinePointer vertexPointer;
218 SetupProcessor::RoutinePointer setupPointer;
219 PixelProcessor::RoutinePointer pixelPointer;
221 int (*setupPrimitives)(Renderer *renderer, int batch, int count);
222 SetupProcessor::State setupState;
224 Resource *vertexStream[VERTEX_ATTRIBUTES];
225 Resource *indexBuffer;
226 Surface *renderTarget[RENDERTARGETS];
227 Surface *depthBuffer;
228 Surface *stencilBuffer;
229 Resource *texture[TOTAL_IMAGE_UNITS];
230 Resource* pUniformBuffers[MAX_UNIFORM_BUFFER_BINDINGS];
231 Resource* vUniformBuffers[MAX_UNIFORM_BUFFER_BINDINGS];
232 Resource* transformFeedbackBuffers[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS];
242 std::list<Query*> *queries;
246 volatile int primitive; // Current primitive to enter pipeline
247 volatile int count; // Number of primitives to render
248 volatile int references; // Remaining references to this draw call, 0 when done drawing, -1 when resources unlocked and slot is free
263 class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
277 volatile int primitiveUnit;
278 volatile int pixelCluster;
281 struct PrimitiveProgress
292 volatile int drawCall;
293 volatile int firstPrimitive;
294 volatile int primitiveCount;
295 volatile int visible;
296 volatile int references;
304 processedPrimitives = 0;
308 volatile int drawCall;
309 volatile int processedPrimitives;
310 volatile bool executing;
314 Renderer(Context *context, Conventions conventions, bool exactColorRounding);
318 virtual void clear(void* pixel, Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
319 virtual void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
320 virtual void blit3D(Surface *source, Surface *dest);
321 virtual void draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true);
323 virtual void setIndexBuffer(Resource *indexBuffer);
325 virtual void setMultiSampleMask(unsigned int mask);
326 virtual void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
328 virtual void setTextureResource(unsigned int sampler, Resource *resource);
329 virtual void setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type);
331 virtual void setTextureFilter(SamplerType type, int sampler, FilterType textureFilter);
332 virtual void setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter);
333 virtual void setGatherEnable(SamplerType type, int sampler, bool enable);
334 virtual void setAddressingModeU(SamplerType type, int sampler, AddressingMode addressingMode);
335 virtual void setAddressingModeV(SamplerType type, int sampler, AddressingMode addressingMode);
336 virtual void setAddressingModeW(SamplerType type, int sampler, AddressingMode addressingMode);
337 virtual void setReadSRGB(SamplerType type, int sampler, bool sRGB);
338 virtual void setMipmapLOD(SamplerType type, int sampler, float bias);
339 virtual void setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor);
340 virtual void setMaxAnisotropy(SamplerType type, int sampler, float maxAnisotropy);
341 virtual void setSwizzleR(SamplerType type, int sampler, SwizzleType swizzleR);
342 virtual void setSwizzleG(SamplerType type, int sampler, SwizzleType swizzleG);
343 virtual void setSwizzleB(SamplerType type, int sampler, SwizzleType swizzleB);
344 virtual void setSwizzleA(SamplerType type, int sampler, SwizzleType swizzleA);
346 virtual void setPointSpriteEnable(bool pointSpriteEnable);
347 virtual void setPointScaleEnable(bool pointScaleEnable);
348 virtual void setLineWidth(float width);
350 virtual void setDepthBias(float bias);
351 virtual void setSlopeDepthBias(float slopeBias);
353 virtual void setRasterizerDiscard(bool rasterizerDiscard);
355 // Programmable pipelines
356 virtual void setPixelShader(const PixelShader *shader);
357 virtual void setVertexShader(const VertexShader *shader);
359 virtual void setPixelShaderConstantF(int index, const float value[4], int count = 1);
360 virtual void setPixelShaderConstantI(int index, const int value[4], int count = 1);
361 virtual void setPixelShaderConstantB(int index, const int *boolean, int count = 1);
363 virtual void setVertexShaderConstantF(int index, const float value[4], int count = 1);
364 virtual void setVertexShaderConstantI(int index, const int value[4], int count = 1);
365 virtual void setVertexShaderConstantB(int index, const int *boolean, int count = 1);
367 // Viewport & Clipper
368 virtual void setViewport(const Viewport &viewport);
369 virtual void setScissor(const Rect &scissor);
370 virtual void setClipFlags(int flags);
371 virtual void setClipPlane(unsigned int index, const float plane[4]);
374 virtual void setModelMatrix(const Matrix &M, int i = 0);
375 virtual void setViewMatrix(const Matrix &V);
376 virtual void setBaseMatrix(const Matrix &B);
377 virtual void setProjectionMatrix(const Matrix &P);
379 virtual void addQuery(Query *query);
380 virtual void removeQuery(Query *query);
385 // Performance timers
386 int getThreadCount();
387 int64_t getVertexTime(int thread);
388 int64_t getSetupTime(int thread);
389 int64_t getPixelTime(int thread);
394 static void threadFunction(void *parameters);
395 void threadLoop(int threadIndex);
396 void taskLoop(int threadIndex);
397 void findAvailableTasks();
398 void scheduleTask(int threadIndex);
399 void executeTask(int threadIndex);
400 void finishRendering(Task &pixelTask);
402 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
404 static int setupSolidTriangles(Renderer *renderer, int batch, int count);
405 static int setupWireframeTriangle(Renderer *renderer, int batch, int count);
406 static int setupVertexTriangle(Renderer *renderer, int batch, int count);
407 static int setupLines(Renderer *renderer, int batch, int count);
408 static int setupPoints(Renderer *renderer, int batch, int count);
410 static bool setupLine(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
411 static bool setupPoint(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
413 bool isReadWriteTexture(int sampler);
414 void updateClipper();
415 void updateConfiguration(bool initialUpdate = false);
416 static unsigned int computeClipFlags(const float4 &v, const DrawData &data);
417 void initializeThreads();
418 void terminateThreads();
420 void loadConstants(const VertexShader *vertexShader);
421 void loadConstants(const PixelShader *pixelShader);
429 Triangle *triangleBatch[16];
430 Primitive *primitiveBatch[16];
432 // User-defined clipping planes
433 Plane userPlane[MAX_CLIP_PLANES];
434 Plane clipPlane[MAX_CLIP_PLANES]; // Tranformed to clip space
435 bool updateClipPlanes;
437 volatile bool exitThreads;
438 volatile int threadsAwake;
440 Event *resume[16]; // Events for resuming threads
441 Event *suspend[16]; // Events for suspending threads
442 Event *resumeApp; // Event for resuming the application thread
444 PrimitiveProgress primitiveProgress[16];
445 PixelProgress pixelProgress[16];
446 Task task[16]; // Current tasks for threads
448 enum {DRAW_COUNT = 16}; // Number of draw calls buffered
449 DrawCall *drawCall[DRAW_COUNT];
450 DrawCall *drawList[DRAW_COUNT];
452 volatile int currentDraw;
453 volatile int nextDraw;
459 BackoffLock schedulerMutex;
462 int64_t vertexTime[16];
463 int64_t setupTime[16];
464 int64_t pixelTime[16];
467 VertexTask *vertexTask[16];
469 SwiftConfig *swiftConfig;
471 std::list<Query*> queries;
474 VertexProcessor::State vertexState;
475 SetupProcessor::State setupState;
476 PixelProcessor::State pixelState;
480 #endif // sw_Renderer_hpp