1 // SwiftShader Software Renderer
\r
3 // Copyright(c) 2005-2012 TransGaming Inc.
\r
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
\r
6 // transcribed, stored in a retrieval system, translated into any human or computer
\r
7 // language by any means, or disclosed to third parties without the explicit written
\r
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
\r
9 // or implied, including but not limited to any patent rights, are granted to you.
\r
12 #ifndef sw_Renderer_hpp
\r
13 #define sw_Renderer_hpp
\r
15 #include "VertexProcessor.hpp"
\r
16 #include "PixelProcessor.hpp"
\r
17 #include "SetupProcessor.hpp"
\r
18 #include "Plane.hpp"
\r
19 #include "Blitter.hpp"
\r
20 #include "Common/MutexLock.hpp"
\r
21 #include "Common/Thread.hpp"
\r
22 #include "Main/Config.hpp"
\r
36 extern int batchSize;
\r
37 extern int threadCount;
\r
38 extern int unitCount;
\r
39 extern int clusterCount;
\r
41 enum TranscendentalPrecision
\r
50 extern TranscendentalPrecision logPrecision;
\r
51 extern TranscendentalPrecision expPrecision;
\r
52 extern TranscendentalPrecision rcpPrecision;
\r
53 extern TranscendentalPrecision rsqPrecision;
\r
54 extern bool perspectiveCorrection;
\r
77 volatile int reference;
\r
78 volatile unsigned int data;
\r
83 const void *constants;
\r
85 const void *input[16];
\r
86 unsigned int stride[16];
\r
87 Texture mipmap[16 + 4];
\r
88 const void *indices;
\r
92 float4 c[256 + 1]; // One extra for indices out of range, c[256] = {0, 0, 0, 0}
\r
108 VertexProcessor::FixedFunction ff;
\r
113 VertexProcessor::PointSprite point;
\r
115 PixelProcessor::Stencil stencil[2]; // clockwise, counterclockwise
\r
116 PixelProcessor::Stencil stencilCCW;
\r
117 PixelProcessor::Fog fog;
\r
118 PixelProcessor::Factor factor;
\r
119 unsigned int occlusion[16]; // Number of pixels passing depth test
\r
122 int64_t cycles[PERF_TIMERS][16];
\r
125 TextureStage::Uniforms textureStage[8];
\r
135 float viewportHeight;
\r
136 float slopeDepthBias;
\r
139 Plane clipPlane[6];
\r
141 unsigned int *colorBuffer[4];
\r
142 int colorPitchB[4];
\r
143 int colorSliceB[4];
\r
144 float *depthBuffer;
\r
147 unsigned char *stencilBuffer;
\r
168 Context::DrawType drawType;
\r
171 Routine *vertexRoutine;
\r
172 Routine *setupRoutine;
\r
173 Routine *pixelRoutine;
\r
175 VertexProcessor::RoutinePointer vertexPointer;
\r
176 SetupProcessor::RoutinePointer setupPointer;
\r
177 PixelProcessor::RoutinePointer pixelPointer;
\r
179 int (*setupPrimitives)(Renderer *renderer, int batch, int count);
\r
180 SetupProcessor::State setupState;
\r
182 Resource *vertexStream[16];
\r
183 Resource *indexBuffer;
\r
184 Surface *renderTarget[4];
\r
185 Surface *depthStencil;
\r
186 Resource *texture[16 + 4];
\r
196 std::list<Query*> *queries;
\r
200 volatile int primitive; // Current primitive to enter pipeline
\r
201 volatile int count; // Number of primitives to render
\r
202 volatile int references; // Remaining references to this draw call, 0 when done drawing, -1 when resources unlocked and slot is free
\r
217 class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
\r
230 volatile Type type;
\r
231 volatile int primitiveUnit;
\r
232 volatile int pixelCluster;
\r
235 struct PrimitiveProgress
\r
240 firstPrimitive = 0;
\r
241 primitiveCount = 0;
\r
246 volatile int drawCall;
\r
247 volatile int firstPrimitive;
\r
248 volatile int primitiveCount;
\r
249 volatile int visible;
\r
250 volatile int references;
\r
253 struct PixelProgress
\r
258 processedPrimitives = 0;
\r
262 volatile int drawCall;
\r
263 volatile int processedPrimitives;
\r
264 volatile bool executing;
\r
268 Renderer(Context *context, bool halfIntegerCoordinates, bool symmetricNormalizedDepth, bool booleanFaceRegister, bool fullPixelPositionRegister, bool exactColorRounding);
\r
270 virtual ~Renderer();
\r
272 virtual void blit(Surface *source, const Rect &sRect, Surface *dest, const Rect &dRect, bool filter);
\r
273 virtual void draw(Context::DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true);
\r
275 virtual void setIndexBuffer(Resource *indexBuffer);
\r
277 virtual void setMultiSampleMask(unsigned int mask);
\r
278 virtual void setTransparencyAntialiasing(Context::TransparencyAntialiasing transparencyAntialiasing);
\r
280 virtual void setTextureResource(unsigned int sampler, Resource *resource);
\r
281 virtual void setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type);
\r
283 virtual void setTextureFilter(SamplerType type, int sampler, FilterType textureFilter);
\r
284 virtual void setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter);
\r
285 virtual void setGatherEnable(SamplerType type, int sampler, bool enable);
\r
286 virtual void setAddressingModeU(SamplerType type, int sampler, AddressingMode addressingMode);
\r
287 virtual void setAddressingModeV(SamplerType type, int sampler, AddressingMode addressingMode);
\r
288 virtual void setAddressingModeW(SamplerType type, int sampler, AddressingMode addressingMode);
\r
289 virtual void setReadSRGB(SamplerType type, int sampler, bool sRGB);
\r
290 virtual void setMipmapLOD(SamplerType type, int sampler, float bias);
\r
291 virtual void setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor);
\r
292 virtual void setMaxAnisotropy(SamplerType type, int sampler, unsigned int maxAnisotropy);
\r
294 virtual void setPointSpriteEnable(bool pointSpriteEnable);
\r
295 virtual void setPointScaleEnable(bool pointScaleEnable);
\r
297 virtual void setDepthBias(float bias);
\r
298 virtual void setSlopeDepthBias(float slopeBias);
\r
300 // Programmable pipelines
\r
301 virtual void setPixelShader(const PixelShader *shader);
\r
302 virtual void setVertexShader(const VertexShader *shader);
\r
304 virtual void setPixelShaderConstantF(int index, const float value[4], int count = 1);
\r
305 virtual void setPixelShaderConstantI(int index, const int value[4], int count = 1);
\r
306 virtual void setPixelShaderConstantB(int index, const int *boolean, int count = 1);
\r
308 virtual void setVertexShaderConstantF(int index, const float value[4], int count = 1);
\r
309 virtual void setVertexShaderConstantI(int index, const int value[4], int count = 1);
\r
310 virtual void setVertexShaderConstantB(int index, const int *boolean, int count = 1);
\r
312 // Viewport & Clipper
\r
313 virtual void setViewport(const Viewport &viewport);
\r
314 virtual void setScissor(const Rect &scissor);
\r
315 virtual void setClipFlags(int flags);
\r
316 virtual void setClipPlane(unsigned int index, const float plane[4]);
\r
318 // Partial transform
\r
319 virtual void setModelMatrix(const Matrix &M, int i = 0);
\r
320 virtual void setViewMatrix(const Matrix &V);
\r
321 virtual void setBaseMatrix(const Matrix &B);
\r
322 virtual void setProjectionMatrix(const Matrix &P);
\r
324 virtual void addQuery(Query *query);
\r
325 virtual void removeQuery(Query *query);
\r
327 void synchronize();
\r
330 // Performance timers
\r
331 int getThreadCount();
\r
332 int64_t getVertexTime(int thread);
\r
333 int64_t getSetupTime(int thread);
\r
334 int64_t getPixelTime(int thread);
\r
335 void resetTimers();
\r
339 static void threadFunction(void *parameters);
\r
340 void threadLoop(int threadIndex);
\r
341 void taskLoop(int threadIndex);
\r
342 void findAvailableTasks();
\r
343 void scheduleTask(int threadIndex);
\r
344 void executeTask(int threadIndex);
\r
345 void finishRendering(Task &pixelTask);
\r
347 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
\r
349 static int setupSolidTriangles(Renderer *renderer, int batch, int count);
\r
350 static int setupWireframeTriangle(Renderer *renderer, int batch, int count);
\r
351 static int setupVertexTriangle(Renderer *renderer, int batch, int count);
\r
352 static int setupLines(Renderer *renderer, int batch, int count);
\r
353 static int setupPoints(Renderer *renderer, int batch, int count);
\r
355 static bool setupLine(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
\r
356 static bool setupPoint(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
\r
358 bool isReadWriteTexture(int sampler);
\r
359 void updateClipper();
\r
360 void updateConfiguration(bool initialUpdate = false);
\r
361 static unsigned int computeClipFlags(const float4 &v, const DrawData &data);
\r
362 void initializeThreads(int threadCount);
\r
363 void terminateThreads();
\r
364 void deleteBatches();
\r
366 void loadConstants(const VertexShader *vertexShader);
\r
367 void loadConstants(const PixelShader *pixelShader);
\r
375 Triangle *triangleBatch[16];
\r
376 Primitive *primitiveBatch[16];
\r
378 // User-defined clipping planes
\r
379 Plane userPlane[6];
\r
380 Plane clipPlane[6]; // Tranformed to clip space
\r
381 bool updateClipPlanes;
\r
383 volatile bool exitThreads;
\r
384 volatile int threadsAwake;
\r
385 Thread *worker[16];
\r
386 Event *resume[16]; // Events for resuming threads
\r
387 Event *suspend[16]; // Events for suspending threads
\r
388 Event *resumeApp; // Event for resuming the application thread
\r
390 PrimitiveProgress primitiveProgress[16];
\r
391 PixelProgress pixelProgress[16];
\r
392 Task task[16]; // Current tasks for threads
\r
394 enum {DRAW_COUNT = 16}; // Number of draw calls buffered
\r
395 DrawCall *drawCall[DRAW_COUNT];
\r
396 DrawCall *drawList[DRAW_COUNT];
\r
398 volatile int currentDraw;
\r
399 volatile int nextDraw;
\r
401 Task taskQueue[32];
\r
402 unsigned int qHead;
\r
403 unsigned int qSize;
\r
408 int64_t vertexTime[16];
\r
409 int64_t setupTime[16];
\r
410 int64_t pixelTime[16];
\r
413 VertexTask *vertexTask[16];
\r
415 SwiftConfig *swiftConfig;
\r
417 std::list<Query*> queries;
\r
420 VertexProcessor::State vertexState;
\r
421 SetupProcessor::State setupState;
\r
422 PixelProcessor::State pixelState;
\r
423 int (*setupPrimitives)(Renderer *renderer, int batch, int count);
\r
425 Routine *vertexRoutine;
\r
426 Routine *setupRoutine;
\r
427 Routine *pixelRoutine;
\r
433 #endif // sw_Renderer_hpp
\r