1 // SwiftShader Software Renderer
3 // Copyright(c) 2005-2011 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"
18 #include "Viewport.hpp"
20 #include "Blitter.hpp"
21 #include "Common/MutexLock.hpp"
22 #include "Common/Thread.hpp"
23 #include "Main/Config.hpp"
39 extern int threadCount;
41 extern int clusterCount;
64 volatile int reference;
65 volatile unsigned int data;
70 const void *constants;
72 const void *input[16];
73 unsigned int stride[16];
74 Texture mipmap[16 + 4];
79 float4 c[256 + 1]; // One extra for indices out of range, c[256] = {0, 0, 0, 0}
95 VertexProcessor::FixedFunction ff;
100 VertexProcessor::PointSprite point;
102 PixelProcessor::Stencil stencil[2]; // clockwise, counterclockwise
103 PixelProcessor::Stencil stencilCCW;
104 PixelProcessor::Fog fog;
105 PixelProcessor::Factor factor;
106 unsigned int occlusion[16]; // Number of pixels passing depth test
109 int64_t cycles[PERF_TIMERS][16];
112 TextureStage::Uniforms textureStage[8];
125 float viewportHeight;
126 float slopeDepthBias;
131 unsigned int *colorBuffer[4];
137 unsigned char *stencilBuffer;
153 Context::DrawType drawType;
156 Routine *vertexRoutine;
157 Routine *setupRoutine;
158 Routine *pixelRoutine;
160 VertexProcessor::RoutinePointer vertexPointer;
161 SetupProcessor::RoutinePointer setupPointer;
162 PixelProcessor::RoutinePointer pixelPointer;
164 int (*setupPrimitives)(Renderer *renderer, int batch, int count);
165 SetupProcessor::State setupState;
167 Resource *vertexStream[16];
168 Resource *indexBuffer;
169 Surface *renderTarget[4];
170 Surface *depthStencil;
171 Resource *texture[16 + 4];
181 std::list<Query*> *queries;
185 volatile int primitive; // Current primitive to enter pipeline
186 volatile int count; // Number of primitives to render
187 volatile int references; // Remaining references to this draw call, 0 when done drawing, -1 when resources unlocked and slot is free
192 class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
206 volatile int primitiveUnit;
207 volatile int pixelCluster;
210 struct PrimitiveProgress
221 volatile int drawCall;
222 volatile int firstPrimitive;
223 volatile int primitiveCount;
224 volatile int visible;
225 volatile int references;
233 processedPrimitives = 0;
237 volatile int drawCall;
238 volatile int processedPrimitives;
239 volatile bool executing;
243 Renderer(Context *context, Surface *renderTarget = 0);
247 virtual void blit(Surface *source, const Rect &sRect, Surface *dest, const Rect &dRect, bool filter);
248 virtual void draw(Context::DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true);
250 virtual void setIndexBuffer(Resource *indexBuffer);
252 virtual void setMultiSampleMask(unsigned int mask);
253 virtual void setTransparencyAntialiasing(Context::TransparencyAntialiasing transparencyAntialiasing);
255 virtual void setTextureResource(unsigned int sampler, Resource *resource);
256 virtual void setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type);
258 virtual void setTextureFilter(SamplerType type, int sampler, FilterType textureFilter);
259 virtual void setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter);
260 virtual void setGatherEnable(SamplerType type, int sampler, bool enable);
261 virtual void setAddressingModeU(SamplerType type, int sampler, AddressingMode addressingMode);
262 virtual void setAddressingModeV(SamplerType type, int sampler, AddressingMode addressingMode);
263 virtual void setAddressingModeW(SamplerType type, int sampler, AddressingMode addressingMode);
264 virtual void setReadSRGB(SamplerType type, int sampler, bool sRGB);
265 virtual void setMipmapLOD(SamplerType type, int sampler, float bias);
266 virtual void setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor);
267 virtual void setMaxAnisotropy(SamplerType type, int sampler, unsigned int maxAnisotropy);
269 virtual void setPointSpriteEnable(bool pointSpriteEnable);
270 virtual void setPointScaleEnable(bool pointScaleEnable);
272 virtual void setDepthBias(float bias);
273 virtual void setSlopeDepthBias(float slopeBias);
275 // Programmable pipelines
276 virtual void setPixelShader(const PixelShader *shader);
277 virtual void setVertexShader(const VertexShader *shader);
279 virtual void setPixelShaderConstantF(int index, const float value[4], int count = 1);
280 virtual void setPixelShaderConstantI(int index, const int value[4], int count = 1);
281 virtual void setPixelShaderConstantB(int index, const int *boolean, int count = 1);
283 virtual void setVertexShaderConstantF(int index, const float value[4], int count = 1);
284 virtual void setVertexShaderConstantI(int index, const int value[4], int count = 1);
285 virtual void setVertexShaderConstantB(int index, const int *boolean, int count = 1);
287 // Viewport & Clipper
288 virtual void setViewport(const Viewport &viewport);
289 virtual void setClipFlags(int flags);
290 virtual void setClipPlane(unsigned int index, const float plane[4]);
293 virtual void setModelMatrix(const Matrix &M, int i = 0);
294 virtual void setViewMatrix(const Matrix &V);
295 virtual void setBaseMatrix(const Matrix &B);
296 virtual void setProjectionMatrix(const Matrix &P);
298 virtual void setPostTransformEnable(bool enable);
300 virtual void setPosScale(float x, float y);
301 virtual void setPosOffset(float x, float y);
303 virtual void addQuery(Query *query);
304 virtual void removeQuery(Query *query);
307 // Performance timers
308 int getThreadCount();
309 int64_t getVertexTime(int thread);
310 int64_t getSetupTime(int thread);
311 int64_t getPixelTime(int thread);
316 static void threadFunction(void *parameters);
317 void threadLoop(int threadIndex);
318 void taskLoop(int threadIndex);
319 void findAvailableTasks();
320 void scheduleTask(int threadIndex);
321 void executeTask(int threadIndex);
322 void finishRendering(Task &pixelTask);
324 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
326 static int setupSolidTriangles(Renderer *renderer, int batch, int count);
327 static int setupWireframeTriangle(Renderer *renderer, int batch, int count);
328 static int setupVertexTriangle(Renderer *renderer, int batch, int count);
329 static int setupLines(Renderer *renderer, int batch, int count);
330 static int setupPoints(Renderer *renderer, int batch, int count);
332 static bool setupLine(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
333 static bool setupPoint(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
335 bool isReadWriteTexture(int sampler);
336 void updateClipper();
337 void updateConfiguration(bool initialUpdate = false);
338 void startupConfiguration();
339 static unsigned int computeClipFlags(const float4 &v, const DrawData &data);
340 void initializeThreads(int threadCount);
341 void terminateThreads();
342 void deleteBatches();
344 void loadConstants(const VertexShader *vertexShader);
345 void loadConstants(const PixelShader *pixelShader);
354 Triangle *triangleBatch[16];
355 Primitive *primitiveBatch[16];
357 // User-defined clipping planes
359 Plane clipPlane[6]; // Tranformed to clip space
360 bool updateClipPlanes;
362 volatile bool exitThreads;
363 volatile int threadsAwake;
365 Event *resume[16]; // Events for resuming threads
366 Event *suspend[16]; // Events for suspending threads
367 Event *resumeApp; // Event for resuming the application thread
369 PrimitiveProgress primitiveProgress[16];
370 PixelProgress pixelProgress[16];
371 Task task[16]; // Current tasks for threads
373 enum {DRAW_COUNT = 16}; // Number of draw calls buffered
374 DrawCall *drawCall[DRAW_COUNT];
375 DrawCall *drawList[DRAW_COUNT];
377 volatile int currentDraw;
378 volatile int nextDraw;
387 int64_t vertexTime[16];
388 int64_t setupTime[16];
389 int64_t pixelTime[16];
392 VertexTask *vertexTask[16];
394 SwiftConfig *swiftConfig;
396 std::list<Query*> queries;
398 VertexProcessor::State vertexState;
399 SetupProcessor::State setupState;
400 PixelProcessor::State pixelState;
401 int (*setupPrimitives)(Renderer *renderer, int batch, int count);
403 Routine *vertexRoutine;
404 Routine *setupRoutine;
405 Routine *pixelRoutine;
411 #endif // sw_Renderer_hpp