OSDN Git Service

Multiple draw buffers implementation
[android-x86/external-swiftshader.git] / src / Renderer / Renderer.hpp
1 // SwiftShader Software Renderer
2 //
3 // Copyright(c) 2005-2012 TransGaming Inc.
4 //
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.
10 //
11
12 #ifndef sw_Renderer_hpp
13 #define sw_Renderer_hpp
14
15 #include "VertexProcessor.hpp"
16 #include "PixelProcessor.hpp"
17 #include "SetupProcessor.hpp"
18 #include "Plane.hpp"
19 #include "Blitter.hpp"
20 #include "Common/MutexLock.hpp"
21 #include "Common/Thread.hpp"
22 #include "Main/Config.hpp"
23
24 #include <list>
25
26 namespace sw
27 {
28         class Clipper;
29         class PixelShader;
30         class VertexShader;
31         class SwiftConfig;
32         struct Task;
33         class Resource;
34         class Renderer;
35         struct Constants;
36
37         extern int batchSize;
38         extern int threadCount;
39         extern int unitCount;
40         extern int clusterCount;
41
42         enum TranscendentalPrecision
43         {
44                 APPROXIMATE,
45                 PARTIAL,        // 2^-10
46                 ACCURATE,
47                 WHQL,           // 2^-21
48                 IEEE            // 2^-23
49         };
50
51         extern TranscendentalPrecision logPrecision;
52         extern TranscendentalPrecision expPrecision;
53         extern TranscendentalPrecision rcpPrecision;
54         extern TranscendentalPrecision rsqPrecision;
55         extern bool perspectiveCorrection;
56
57         struct Conventions
58         {
59                 bool halfIntegerCoordinates;
60                 bool symmetricNormalizedDepth;
61                 bool booleanFaceRegister;
62                 bool fullPixelPositionRegister;
63                 bool leadingVertexFirst;
64                 bool secondaryColor;
65         };
66
67         static const Conventions OpenGL =
68         {
69                 true,    // halfIntegerCoordinates
70                 true,    // symmetricNormalizedDepth
71                 true,    // booleanFaceRegister
72                 true,    // fullPixelPositionRegister
73                 false,   // leadingVertexFirst
74                 false    // secondaryColor
75         };
76
77         static const Conventions Direct3D =
78         {
79                 false,   // halfIntegerCoordinates
80                 false,   // symmetricNormalizedDepth
81                 false,   // booleanFaceRegister
82                 false,   // fullPixelPositionRegister
83                 true,    // leadingVertexFirst
84                 true,    // secondardyColor
85         };
86
87         struct Query
88         {
89                 Query()
90                 {
91                         building = false;
92                         reference = 0;
93                         data = 0;
94                 }
95
96                 void begin()
97                 {
98                         building = true;
99                         data = 0;
100                 }
101
102                 void end()
103                 {
104                         building = false;
105                 }
106
107                 bool building;
108                 volatile int reference;
109                 volatile unsigned int data;
110         };
111
112         struct DrawData
113         {
114                 const Constants *constants;
115
116                 const void *input[VERTEX_ATTRIBUTES];
117                 unsigned int stride[VERTEX_ATTRIBUTES];
118                 Texture mipmap[TOTAL_IMAGE_UNITS];
119                 const void *indices;
120
121                 struct VS
122                 {
123                         float4 c[256 + 1];   // One extra for indices out of range, c[256] = {0, 0, 0, 0}
124                         int4 i[16];
125                         bool b[16];
126                 };
127
128                 struct PS
129                 {
130                         word4 cW[8][4];
131                         float4 c[FRAGMENT_UNIFORM_VECTORS];
132                         int4 i[16];
133                         bool b[16];
134                 };
135
136                 union
137                 {
138                         VS vs;
139                         VertexProcessor::FixedFunction ff;
140                 };
141
142                 PS ps;
143
144                 int instanceID;
145
146                 VertexProcessor::PointSprite point;
147                 float lineWidth;
148
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
154
155                 #if PERF_PROFILE
156                         int64_t cycles[PERF_TIMERS][16];
157                 #endif
158
159                 TextureStage::Uniforms textureStage[8];
160
161                 float4 Wx16;
162                 float4 Hx16;
163                 float4 X0x16;
164                 float4 Y0x16;
165                 float4 XXXX;
166                 float4 YYYY;
167                 float4 halfPixelX;
168                 float4 halfPixelY;
169                 float viewportHeight;
170                 float slopeDepthBias;
171                 float depthRange;
172                 float depthNear;
173                 Plane clipPlane[6];
174
175                 unsigned int *colorBuffer[RENDERTARGETS];
176                 int colorPitchB[RENDERTARGETS];
177                 int colorSliceB[RENDERTARGETS];
178                 float *depthBuffer;
179                 int depthPitchB;
180                 int depthSliceB;
181                 unsigned char *stencilBuffer;
182                 int stencilPitchB;
183                 int stencilSliceB;
184
185                 int scissorX0;
186                 int scissorX1;
187                 int scissorY0;
188                 int scissorY1;
189
190                 float4 a2c0;
191                 float4 a2c1;
192                 float4 a2c2;
193                 float4 a2c3;
194         };
195
196         struct DrawCall
197         {
198                 DrawCall();
199
200                 ~DrawCall();
201
202                 DrawType drawType;
203                 int batchSize;
204
205                 Routine *vertexRoutine;
206                 Routine *setupRoutine;
207                 Routine *pixelRoutine;
208
209                 VertexProcessor::RoutinePointer vertexPointer;
210                 SetupProcessor::RoutinePointer setupPointer;
211                 PixelProcessor::RoutinePointer pixelPointer;
212
213                 int (*setupPrimitives)(Renderer *renderer, int batch, int count);
214                 SetupProcessor::State setupState;
215
216                 Resource *vertexStream[VERTEX_ATTRIBUTES];
217                 Resource *indexBuffer;
218                 Surface *renderTarget[RENDERTARGETS];
219                 Surface *depthStencil;
220                 Resource *texture[TOTAL_IMAGE_UNITS];
221
222                 int vsDirtyConstF;
223                 int vsDirtyConstI;
224                 int vsDirtyConstB;
225
226                 int psDirtyConstF;
227                 int psDirtyConstI;
228                 int psDirtyConstB;
229
230                 std::list<Query*> *queries;
231
232                 int clipFlags;
233
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
237
238                 DrawData *data;
239         };
240
241         struct Viewport
242         {
243                 float x0;
244                 float y0;
245                 float width;
246                 float height;
247                 float minZ;
248                 float maxZ;
249         };
250
251         class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
252         {
253                 struct Task
254                 {
255                         enum Type
256                         {
257                                 PRIMITIVES,
258                                 PIXELS,
259
260                                 RESUME,
261                                 SUSPEND
262                         };
263
264                         volatile Type type;
265                         volatile int primitiveUnit;
266                         volatile int pixelCluster;
267                 };
268
269                 struct PrimitiveProgress
270                 {
271                         void init()
272                         {
273                                 drawCall = 0;
274                                 firstPrimitive = 0;
275                                 primitiveCount = 0;
276                                 visible = 0;
277                                 references = 0;
278                         }
279
280                         volatile int drawCall;
281                         volatile int firstPrimitive;
282                         volatile int primitiveCount;
283                         volatile int visible;
284                         volatile int references;
285                 };
286
287                 struct PixelProgress
288                 {
289                         void init()
290                         {
291                                 drawCall = 0;
292                                 processedPrimitives = 0;
293                                 executing = false;
294                         }
295
296                         volatile int drawCall;
297                         volatile int processedPrimitives;
298                         volatile bool executing;
299                 };
300
301         public:
302                 Renderer(Context *context, Conventions conventions, bool exactColorRounding);
303
304                 virtual ~Renderer();
305
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);
309
310                 virtual void setIndexBuffer(Resource *indexBuffer);
311
312                 virtual void setMultiSampleMask(unsigned int mask);
313                 virtual void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
314
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);
317
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);
332                 
333                 virtual void setPointSpriteEnable(bool pointSpriteEnable);
334                 virtual void setPointScaleEnable(bool pointScaleEnable);
335                 virtual void setLineWidth(float width);
336
337                 virtual void setDepthBias(float bias);
338                 virtual void setSlopeDepthBias(float slopeBias);
339
340                 // Programmable pipelines
341                 virtual void setPixelShader(const PixelShader *shader);
342                 virtual void setVertexShader(const VertexShader *shader);
343
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);
347
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);
351
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]);
357
358                 // Partial transform
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);
363
364                 virtual void addQuery(Query *query);
365                 virtual void removeQuery(Query *query);
366
367                 void synchronize();
368
369                 #if PERF_HUD
370                         // Performance timers
371                         int getThreadCount();
372                         int64_t getVertexTime(int thread);
373                         int64_t getSetupTime(int thread);
374                         int64_t getPixelTime(int thread);
375                         void resetTimers();
376                 #endif
377
378         private:
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);
386
387                 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
388
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);
394
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);
397
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();
404
405                 void loadConstants(const VertexShader *vertexShader);
406                 void loadConstants(const PixelShader *pixelShader);
407
408                 Context *context;
409                 Clipper *clipper;
410                 Viewport viewport;
411                 Rect scissor;
412                 int clipFlags;
413
414                 Triangle *triangleBatch[16];
415                 Primitive *primitiveBatch[16];
416
417                 // User-defined clipping planes
418                 Plane userPlane[MAX_CLIP_PLANES];
419                 Plane clipPlane[MAX_CLIP_PLANES];   // Tranformed to clip space
420                 bool updateClipPlanes;
421
422                 volatile bool exitThreads;
423                 volatile int threadsAwake;
424                 Thread *worker[16];
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
428
429                 PrimitiveProgress primitiveProgress[16];
430                 PixelProgress pixelProgress[16];
431                 Task task[16];   // Current tasks for threads
432
433                 enum {DRAW_COUNT = 16};   // Number of draw calls buffered
434                 DrawCall *drawCall[DRAW_COUNT];
435                 DrawCall *drawList[DRAW_COUNT];
436
437                 volatile int currentDraw;
438                 volatile int nextDraw;
439
440                 Task taskQueue[32];
441                 unsigned int qHead;
442                 unsigned int qSize;
443
444                 BackoffLock schedulerMutex;
445
446                 #if PERF_HUD
447                         int64_t vertexTime[16];
448                         int64_t setupTime[16];
449                         int64_t pixelTime[16];
450                 #endif
451
452                 VertexTask *vertexTask[16];
453
454                 SwiftConfig *swiftConfig;
455
456                 std::list<Query*> queries;
457                 Resource *sync;
458
459                 VertexProcessor::State vertexState;
460                 SetupProcessor::State setupState;
461                 PixelProcessor::State pixelState;
462         };
463 }
464
465 #endif   // sw_Renderer_hpp