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
108 volatile int reference;
109 volatile unsigned int data;
114 const Constants *constants;
116 const void *input[VERTEX_ATTRIBUTES];
117 unsigned int stride[VERTEX_ATTRIBUTES];
118 Texture mipmap[TOTAL_IMAGE_UNITS];
123 float4 c[256 + 1]; // One extra for indices out of range, c[256] = {0, 0, 0, 0}
131 float4 c[FRAGMENT_UNIFORM_VECTORS];
139 VertexProcessor::FixedFunction ff;
146 VertexProcessor::PointSprite point;
149 PixelProcessor::Stencil stencil[2]; // clockwise, counterclockwise
150 PixelProcessor::Stencil stencilCCW;
151 PixelProcessor::Fog fog;
152 PixelProcessor::Factor factor;
153 unsigned int occlusion[16]; // Number of pixels passing depth test
156 int64_t cycles[PERF_TIMERS][16];
159 TextureStage::Uniforms textureStage[8];
169 float viewportHeight;
170 float slopeDepthBias;
175 unsigned int *colorBuffer[RENDERTARGETS];
176 int colorPitchB[RENDERTARGETS];
177 int colorSliceB[RENDERTARGETS];
181 unsigned char *stencilBuffer;
205 Routine *vertexRoutine;
206 Routine *setupRoutine;
207 Routine *pixelRoutine;
209 VertexProcessor::RoutinePointer vertexPointer;
210 SetupProcessor::RoutinePointer setupPointer;
211 PixelProcessor::RoutinePointer pixelPointer;
213 int (*setupPrimitives)(Renderer *renderer, int batch, int count);
214 SetupProcessor::State setupState;
216 Resource *vertexStream[VERTEX_ATTRIBUTES];
217 Resource *indexBuffer;
218 Surface *renderTarget[RENDERTARGETS];
219 Surface *depthStencil;
220 Resource *texture[TOTAL_IMAGE_UNITS];
230 std::list<Query*> *queries;
234 volatile int primitive; // Current primitive to enter pipeline
235 volatile int count; // Number of primitives to render
236 volatile int references; // Remaining references to this draw call, 0 when done drawing, -1 when resources unlocked and slot is free
251 class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
265 volatile int primitiveUnit;
266 volatile int pixelCluster;
269 struct PrimitiveProgress
280 volatile int drawCall;
281 volatile int firstPrimitive;
282 volatile int primitiveCount;
283 volatile int visible;
284 volatile int references;
292 processedPrimitives = 0;
296 volatile int drawCall;
297 volatile int processedPrimitives;
298 volatile bool executing;
302 Renderer(Context *context, Conventions conventions, bool exactColorRounding);
306 virtual void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
307 virtual void blit3D(Surface *source, Surface *dest);
308 virtual void draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true);
310 virtual void setIndexBuffer(Resource *indexBuffer);
312 virtual void setMultiSampleMask(unsigned int mask);
313 virtual void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
315 virtual void setTextureResource(unsigned int sampler, Resource *resource);
316 virtual void setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type);
318 virtual void setTextureFilter(SamplerType type, int sampler, FilterType textureFilter);
319 virtual void setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter);
320 virtual void setGatherEnable(SamplerType type, int sampler, bool enable);
321 virtual void setAddressingModeU(SamplerType type, int sampler, AddressingMode addressingMode);
322 virtual void setAddressingModeV(SamplerType type, int sampler, AddressingMode addressingMode);
323 virtual void setAddressingModeW(SamplerType type, int sampler, AddressingMode addressingMode);
324 virtual void setReadSRGB(SamplerType type, int sampler, bool sRGB);
325 virtual void setMipmapLOD(SamplerType type, int sampler, float bias);
326 virtual void setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor);
327 virtual void setMaxAnisotropy(SamplerType type, int sampler, float maxAnisotropy);
328 virtual void setSwizzleR(SamplerType type, int sampler, SwizzleType swizzleR);
329 virtual void setSwizzleG(SamplerType type, int sampler, SwizzleType swizzleG);
330 virtual void setSwizzleB(SamplerType type, int sampler, SwizzleType swizzleB);
331 virtual void setSwizzleA(SamplerType type, int sampler, SwizzleType swizzleA);
333 virtual void setPointSpriteEnable(bool pointSpriteEnable);
334 virtual void setPointScaleEnable(bool pointScaleEnable);
335 virtual void setLineWidth(float width);
337 virtual void setDepthBias(float bias);
338 virtual void setSlopeDepthBias(float slopeBias);
340 // Programmable pipelines
341 virtual void setPixelShader(const PixelShader *shader);
342 virtual void setVertexShader(const VertexShader *shader);
344 virtual void setPixelShaderConstantF(int index, const float value[4], int count = 1);
345 virtual void setPixelShaderConstantI(int index, const int value[4], int count = 1);
346 virtual void setPixelShaderConstantB(int index, const int *boolean, int count = 1);
348 virtual void setVertexShaderConstantF(int index, const float value[4], int count = 1);
349 virtual void setVertexShaderConstantI(int index, const int value[4], int count = 1);
350 virtual void setVertexShaderConstantB(int index, const int *boolean, int count = 1);
352 // Viewport & Clipper
353 virtual void setViewport(const Viewport &viewport);
354 virtual void setScissor(const Rect &scissor);
355 virtual void setClipFlags(int flags);
356 virtual void setClipPlane(unsigned int index, const float plane[4]);
359 virtual void setModelMatrix(const Matrix &M, int i = 0);
360 virtual void setViewMatrix(const Matrix &V);
361 virtual void setBaseMatrix(const Matrix &B);
362 virtual void setProjectionMatrix(const Matrix &P);
364 virtual void addQuery(Query *query);
365 virtual void removeQuery(Query *query);
370 // Performance timers
371 int getThreadCount();
372 int64_t getVertexTime(int thread);
373 int64_t getSetupTime(int thread);
374 int64_t getPixelTime(int thread);
379 static void threadFunction(void *parameters);
380 void threadLoop(int threadIndex);
381 void taskLoop(int threadIndex);
382 void findAvailableTasks();
383 void scheduleTask(int threadIndex);
384 void executeTask(int threadIndex);
385 void finishRendering(Task &pixelTask);
387 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
389 static int setupSolidTriangles(Renderer *renderer, int batch, int count);
390 static int setupWireframeTriangle(Renderer *renderer, int batch, int count);
391 static int setupVertexTriangle(Renderer *renderer, int batch, int count);
392 static int setupLines(Renderer *renderer, int batch, int count);
393 static int setupPoints(Renderer *renderer, int batch, int count);
395 static bool setupLine(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
396 static bool setupPoint(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
398 bool isReadWriteTexture(int sampler);
399 void updateClipper();
400 void updateConfiguration(bool initialUpdate = false);
401 static unsigned int computeClipFlags(const float4 &v, const DrawData &data);
402 void initializeThreads();
403 void terminateThreads();
405 void loadConstants(const VertexShader *vertexShader);
406 void loadConstants(const PixelShader *pixelShader);
414 Triangle *triangleBatch[16];
415 Primitive *primitiveBatch[16];
417 // User-defined clipping planes
418 Plane userPlane[MAX_CLIP_PLANES];
419 Plane clipPlane[MAX_CLIP_PLANES]; // Tranformed to clip space
420 bool updateClipPlanes;
422 volatile bool exitThreads;
423 volatile int threadsAwake;
425 Event *resume[16]; // Events for resuming threads
426 Event *suspend[16]; // Events for suspending threads
427 Event *resumeApp; // Event for resuming the application thread
429 PrimitiveProgress primitiveProgress[16];
430 PixelProgress pixelProgress[16];
431 Task task[16]; // Current tasks for threads
433 enum {DRAW_COUNT = 16}; // Number of draw calls buffered
434 DrawCall *drawCall[DRAW_COUNT];
435 DrawCall *drawList[DRAW_COUNT];
437 volatile int currentDraw;
438 volatile int nextDraw;
444 BackoffLock schedulerMutex;
447 int64_t vertexTime[16];
448 int64_t setupTime[16];
449 int64_t pixelTime[16];
452 VertexTask *vertexTask[16];
454 SwiftConfig *swiftConfig;
456 std::list<Query*> queries;
459 VertexProcessor::State vertexState;
460 SetupProcessor::State setupState;
461 PixelProcessor::State pixelState;
465 #endif // sw_Renderer_hpp