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;
77 volatile int reference;
78 volatile unsigned int data;
83 const void *constants;
85 const void *input[TEXTURE_IMAGE_UNITS];
86 unsigned int stride[TEXTURE_IMAGE_UNITS];
87 Texture mipmap[TOTAL_IMAGE_UNITS];
92 float4 c[256 + 1]; // One extra for indices out of range, c[256] = {0, 0, 0, 0}
100 float4 c[FRAGMENT_UNIFORM_VECTORS];
108 VertexProcessor::FixedFunction ff;
113 VertexProcessor::PointSprite point;
116 PixelProcessor::Stencil stencil[2]; // clockwise, counterclockwise
117 PixelProcessor::Stencil stencilCCW;
118 PixelProcessor::Fog fog;
119 PixelProcessor::Factor factor;
120 unsigned int occlusion[16]; // Number of pixels passing depth test
123 int64_t cycles[PERF_TIMERS][16];
126 TextureStage::Uniforms textureStage[8];
136 float viewportHeight;
137 float slopeDepthBias;
142 unsigned int *colorBuffer[4];
148 unsigned char *stencilBuffer;
172 Routine *vertexRoutine;
173 Routine *setupRoutine;
174 Routine *pixelRoutine;
176 VertexProcessor::RoutinePointer vertexPointer;
177 SetupProcessor::RoutinePointer setupPointer;
178 PixelProcessor::RoutinePointer pixelPointer;
180 int (*setupPrimitives)(Renderer *renderer, int batch, int count);
181 SetupProcessor::State setupState;
183 Resource *vertexStream[TEXTURE_IMAGE_UNITS];
184 Resource *indexBuffer;
185 Surface *renderTarget[4];
186 Surface *depthStencil;
187 Resource *texture[TOTAL_IMAGE_UNITS];
197 std::list<Query*> *queries;
201 volatile int primitive; // Current primitive to enter pipeline
202 volatile int count; // Number of primitives to render
203 volatile int references; // Remaining references to this draw call, 0 when done drawing, -1 when resources unlocked and slot is free
218 class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
232 volatile int primitiveUnit;
233 volatile int pixelCluster;
236 struct PrimitiveProgress
247 volatile int drawCall;
248 volatile int firstPrimitive;
249 volatile int primitiveCount;
250 volatile int visible;
251 volatile int references;
259 processedPrimitives = 0;
263 volatile int drawCall;
264 volatile int processedPrimitives;
265 volatile bool executing;
269 Renderer(Context *context, bool halfIntegerCoordinates, bool symmetricNormalizedDepth, bool booleanFaceRegister, bool fullPixelPositionRegister, bool exactColorRounding);
273 virtual void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
274 virtual void draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true);
276 virtual void setIndexBuffer(Resource *indexBuffer);
278 virtual void setMultiSampleMask(unsigned int mask);
279 virtual void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
281 virtual void setTextureResource(unsigned int sampler, Resource *resource);
282 virtual void setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type);
284 virtual void setTextureFilter(SamplerType type, int sampler, FilterType textureFilter);
285 virtual void setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter);
286 virtual void setGatherEnable(SamplerType type, int sampler, bool enable);
287 virtual void setAddressingModeU(SamplerType type, int sampler, AddressingMode addressingMode);
288 virtual void setAddressingModeV(SamplerType type, int sampler, AddressingMode addressingMode);
289 virtual void setAddressingModeW(SamplerType type, int sampler, AddressingMode addressingMode);
290 virtual void setReadSRGB(SamplerType type, int sampler, bool sRGB);
291 virtual void setMipmapLOD(SamplerType type, int sampler, float bias);
292 virtual void setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor);
293 virtual void setMaxAnisotropy(SamplerType type, int sampler, float maxAnisotropy);
295 virtual void setPointSpriteEnable(bool pointSpriteEnable);
296 virtual void setPointScaleEnable(bool pointScaleEnable);
297 virtual void setLineWidth(float width);
299 virtual void setDepthBias(float bias);
300 virtual void setSlopeDepthBias(float slopeBias);
302 // Programmable pipelines
303 virtual void setPixelShader(const PixelShader *shader);
304 virtual void setVertexShader(const VertexShader *shader);
306 virtual void setPixelShaderConstantF(int index, const float value[4], int count = 1);
307 virtual void setPixelShaderConstantI(int index, const int value[4], int count = 1);
308 virtual void setPixelShaderConstantB(int index, const int *boolean, int count = 1);
310 virtual void setVertexShaderConstantF(int index, const float value[4], int count = 1);
311 virtual void setVertexShaderConstantI(int index, const int value[4], int count = 1);
312 virtual void setVertexShaderConstantB(int index, const int *boolean, int count = 1);
314 // Viewport & Clipper
315 virtual void setViewport(const Viewport &viewport);
316 virtual void setScissor(const Rect &scissor);
317 virtual void setClipFlags(int flags);
318 virtual void setClipPlane(unsigned int index, const float plane[4]);
321 virtual void setModelMatrix(const Matrix &M, int i = 0);
322 virtual void setViewMatrix(const Matrix &V);
323 virtual void setBaseMatrix(const Matrix &B);
324 virtual void setProjectionMatrix(const Matrix &P);
326 virtual void addQuery(Query *query);
327 virtual void removeQuery(Query *query);
332 // Performance timers
333 int getThreadCount();
334 int64_t getVertexTime(int thread);
335 int64_t getSetupTime(int thread);
336 int64_t getPixelTime(int thread);
341 static void threadFunction(void *parameters);
342 void threadLoop(int threadIndex);
343 void taskLoop(int threadIndex);
344 void findAvailableTasks();
345 void scheduleTask(int threadIndex);
346 void executeTask(int threadIndex);
347 void finishRendering(Task &pixelTask);
349 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
351 static int setupSolidTriangles(Renderer *renderer, int batch, int count);
352 static int setupWireframeTriangle(Renderer *renderer, int batch, int count);
353 static int setupVertexTriangle(Renderer *renderer, int batch, int count);
354 static int setupLines(Renderer *renderer, int batch, int count);
355 static int setupPoints(Renderer *renderer, int batch, int count);
357 static bool setupLine(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
358 static bool setupPoint(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
360 bool isReadWriteTexture(int sampler);
361 void updateClipper();
362 void updateConfiguration(bool initialUpdate = false);
363 static unsigned int computeClipFlags(const float4 &v, const DrawData &data);
364 void initializeThreads();
365 void terminateThreads();
367 void loadConstants(const VertexShader *vertexShader);
368 void loadConstants(const PixelShader *pixelShader);
376 Triangle *triangleBatch[16];
377 Primitive *primitiveBatch[16];
379 // User-defined clipping planes
381 Plane clipPlane[6]; // Tranformed to clip space
382 bool updateClipPlanes;
384 volatile bool exitThreads;
385 volatile int threadsAwake;
387 Event *resume[16]; // Events for resuming threads
388 Event *suspend[16]; // Events for suspending threads
389 Event *resumeApp; // Event for resuming the application thread
391 PrimitiveProgress primitiveProgress[16];
392 PixelProgress pixelProgress[16];
393 Task task[16]; // Current tasks for threads
395 enum {DRAW_COUNT = 16}; // Number of draw calls buffered
396 DrawCall *drawCall[DRAW_COUNT];
397 DrawCall *drawList[DRAW_COUNT];
399 volatile int currentDraw;
400 volatile int nextDraw;
406 BackoffLock schedulerMutex;
409 int64_t vertexTime[16];
410 int64_t setupTime[16];
411 int64_t pixelTime[16];
414 VertexTask *vertexTask[16];
416 SwiftConfig *swiftConfig;
418 std::list<Query*> queries;
421 VertexProcessor::State vertexState;
422 SetupProcessor::State setupState;
423 PixelProcessor::State pixelState;
429 #endif // sw_Renderer_hpp