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 clear(void* pixel, Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
307 virtual void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
308 virtual void blit3D(Surface *source, Surface *dest);
309 virtual void draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true);
311 virtual void setIndexBuffer(Resource *indexBuffer);
313 virtual void setMultiSampleMask(unsigned int mask);
314 virtual void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
316 virtual void setTextureResource(unsigned int sampler, Resource *resource);
317 virtual void setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type);
319 virtual void setTextureFilter(SamplerType type, int sampler, FilterType textureFilter);
320 virtual void setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter);
321 virtual void setGatherEnable(SamplerType type, int sampler, bool enable);
322 virtual void setAddressingModeU(SamplerType type, int sampler, AddressingMode addressingMode);
323 virtual void setAddressingModeV(SamplerType type, int sampler, AddressingMode addressingMode);
324 virtual void setAddressingModeW(SamplerType type, int sampler, AddressingMode addressingMode);
325 virtual void setReadSRGB(SamplerType type, int sampler, bool sRGB);
326 virtual void setMipmapLOD(SamplerType type, int sampler, float bias);
327 virtual void setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor);
328 virtual void setMaxAnisotropy(SamplerType type, int sampler, float maxAnisotropy);
329 virtual void setSwizzleR(SamplerType type, int sampler, SwizzleType swizzleR);
330 virtual void setSwizzleG(SamplerType type, int sampler, SwizzleType swizzleG);
331 virtual void setSwizzleB(SamplerType type, int sampler, SwizzleType swizzleB);
332 virtual void setSwizzleA(SamplerType type, int sampler, SwizzleType swizzleA);
334 virtual void setPointSpriteEnable(bool pointSpriteEnable);
335 virtual void setPointScaleEnable(bool pointScaleEnable);
336 virtual void setLineWidth(float width);
338 virtual void setDepthBias(float bias);
339 virtual void setSlopeDepthBias(float slopeBias);
341 // Programmable pipelines
342 virtual void setPixelShader(const PixelShader *shader);
343 virtual void setVertexShader(const VertexShader *shader);
345 virtual void setPixelShaderConstantF(int index, const float value[4], int count = 1);
346 virtual void setPixelShaderConstantI(int index, const int value[4], int count = 1);
347 virtual void setPixelShaderConstantB(int index, const int *boolean, int count = 1);
349 virtual void setVertexShaderConstantF(int index, const float value[4], int count = 1);
350 virtual void setVertexShaderConstantI(int index, const int value[4], int count = 1);
351 virtual void setVertexShaderConstantB(int index, const int *boolean, int count = 1);
353 // Viewport & Clipper
354 virtual void setViewport(const Viewport &viewport);
355 virtual void setScissor(const Rect &scissor);
356 virtual void setClipFlags(int flags);
357 virtual void setClipPlane(unsigned int index, const float plane[4]);
360 virtual void setModelMatrix(const Matrix &M, int i = 0);
361 virtual void setViewMatrix(const Matrix &V);
362 virtual void setBaseMatrix(const Matrix &B);
363 virtual void setProjectionMatrix(const Matrix &P);
365 virtual void addQuery(Query *query);
366 virtual void removeQuery(Query *query);
371 // Performance timers
372 int getThreadCount();
373 int64_t getVertexTime(int thread);
374 int64_t getSetupTime(int thread);
375 int64_t getPixelTime(int thread);
380 static void threadFunction(void *parameters);
381 void threadLoop(int threadIndex);
382 void taskLoop(int threadIndex);
383 void findAvailableTasks();
384 void scheduleTask(int threadIndex);
385 void executeTask(int threadIndex);
386 void finishRendering(Task &pixelTask);
388 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
390 static int setupSolidTriangles(Renderer *renderer, int batch, int count);
391 static int setupWireframeTriangle(Renderer *renderer, int batch, int count);
392 static int setupVertexTriangle(Renderer *renderer, int batch, int count);
393 static int setupLines(Renderer *renderer, int batch, int count);
394 static int setupPoints(Renderer *renderer, int batch, int count);
396 static bool setupLine(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
397 static bool setupPoint(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
399 bool isReadWriteTexture(int sampler);
400 void updateClipper();
401 void updateConfiguration(bool initialUpdate = false);
402 static unsigned int computeClipFlags(const float4 &v, const DrawData &data);
403 void initializeThreads();
404 void terminateThreads();
406 void loadConstants(const VertexShader *vertexShader);
407 void loadConstants(const PixelShader *pixelShader);
415 Triangle *triangleBatch[16];
416 Primitive *primitiveBatch[16];
418 // User-defined clipping planes
419 Plane userPlane[MAX_CLIP_PLANES];
420 Plane clipPlane[MAX_CLIP_PLANES]; // Tranformed to clip space
421 bool updateClipPlanes;
423 volatile bool exitThreads;
424 volatile int threadsAwake;
426 Event *resume[16]; // Events for resuming threads
427 Event *suspend[16]; // Events for suspending threads
428 Event *resumeApp; // Event for resuming the application thread
430 PrimitiveProgress primitiveProgress[16];
431 PixelProgress pixelProgress[16];
432 Task task[16]; // Current tasks for threads
434 enum {DRAW_COUNT = 16}; // Number of draw calls buffered
435 DrawCall *drawCall[DRAW_COUNT];
436 DrawCall *drawList[DRAW_COUNT];
438 volatile int currentDraw;
439 volatile int nextDraw;
445 BackoffLock schedulerMutex;
448 int64_t vertexTime[16];
449 int64_t setupTime[16];
450 int64_t pixelTime[16];
453 VertexTask *vertexTask[16];
455 SwiftConfig *swiftConfig;
457 std::list<Query*> queries;
460 VertexProcessor::State vertexState;
461 SetupProcessor::State setupState;
462 PixelProcessor::State pixelState;
466 #endif // sw_Renderer_hpp