OSDN Git Service

Renderer side code to lock/unlock transform feedback data
[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                 enum Type { FRAGMENTS_PASSED, TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN };
90
91                 Query(Type type) : building(false), reference(0), data(0), type(type)
92                 {
93                 }
94
95                 void begin()
96                 {
97                         building = true;
98                         data = 0;
99                 }
100
101                 void end()
102                 {
103                         building = false;
104                 }
105
106                 bool building;
107                 volatile int reference;
108                 volatile unsigned int data;
109
110                 const Type type;
111         };
112
113         struct DrawData
114         {
115                 const Constants *constants;
116
117                 const void *input[VERTEX_ATTRIBUTES];
118                 unsigned int stride[VERTEX_ATTRIBUTES];
119                 Texture mipmap[TOTAL_IMAGE_UNITS];
120                 const void *indices;
121
122                 struct VS
123                 {
124                         float4 c[VERTEX_UNIFORM_VECTORS + 1];   // One extra for indices out of range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
125                         byte* u[MAX_UNIFORM_BUFFER_BINDINGS];
126                         byte* t[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS];
127                         unsigned int reg[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Offset used when reading from registers, in components
128                         unsigned int row[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of rows to read
129                         unsigned int col[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of columns to read
130                         unsigned int str[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of components between each varying in output buffer
131                         int4 i[16];
132                         bool b[16];
133                 };
134
135                 struct PS
136                 {
137                         word4 cW[8][4];
138                         float4 c[FRAGMENT_UNIFORM_VECTORS];
139                         byte* u[MAX_UNIFORM_BUFFER_BINDINGS];
140                         int4 i[16];
141                         bool b[16];
142                 };
143
144                 union
145                 {
146                         VS vs;
147                         VertexProcessor::FixedFunction ff;
148                 };
149
150                 PS ps;
151
152                 int instanceID;
153
154                 VertexProcessor::PointSprite point;
155                 float lineWidth;
156
157                 PixelProcessor::Stencil stencil[2];   // clockwise, counterclockwise
158                 PixelProcessor::Stencil stencilCCW;
159                 PixelProcessor::Fog fog;
160                 PixelProcessor::Factor factor;
161                 unsigned int occlusion[16];   // Number of pixels passing depth test
162
163                 #if PERF_PROFILE
164                         int64_t cycles[PERF_TIMERS][16];
165                 #endif
166
167                 TextureStage::Uniforms textureStage[8];
168
169                 float4 Wx16;
170                 float4 Hx16;
171                 float4 X0x16;
172                 float4 Y0x16;
173                 float4 XXXX;
174                 float4 YYYY;
175                 float4 halfPixelX;
176                 float4 halfPixelY;
177                 float viewportHeight;
178                 float slopeDepthBias;
179                 float depthRange;
180                 float depthNear;
181                 Plane clipPlane[6];
182
183                 unsigned int *colorBuffer[RENDERTARGETS];
184                 int colorPitchB[RENDERTARGETS];
185                 int colorSliceB[RENDERTARGETS];
186                 float *depthBuffer;
187                 int depthPitchB;
188                 int depthSliceB;
189                 unsigned char *stencilBuffer;
190                 int stencilPitchB;
191                 int stencilSliceB;
192
193                 int scissorX0;
194                 int scissorX1;
195                 int scissorY0;
196                 int scissorY1;
197
198                 float4 a2c0;
199                 float4 a2c1;
200                 float4 a2c2;
201                 float4 a2c3;
202         };
203
204         struct DrawCall
205         {
206                 DrawCall();
207
208                 ~DrawCall();
209
210                 DrawType drawType;
211                 int batchSize;
212
213                 Routine *vertexRoutine;
214                 Routine *setupRoutine;
215                 Routine *pixelRoutine;
216
217                 VertexProcessor::RoutinePointer vertexPointer;
218                 SetupProcessor::RoutinePointer setupPointer;
219                 PixelProcessor::RoutinePointer pixelPointer;
220
221                 int (*setupPrimitives)(Renderer *renderer, int batch, int count);
222                 SetupProcessor::State setupState;
223
224                 Resource *vertexStream[VERTEX_ATTRIBUTES];
225                 Resource *indexBuffer;
226                 Surface *renderTarget[RENDERTARGETS];
227                 Surface *depthBuffer;
228                 Surface *stencilBuffer;
229                 Resource *texture[TOTAL_IMAGE_UNITS];
230                 Resource* pUniformBuffers[MAX_UNIFORM_BUFFER_BINDINGS];
231                 Resource* vUniformBuffers[MAX_UNIFORM_BUFFER_BINDINGS];
232                 Resource* transformFeedbackBuffers[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS];
233
234                 int vsDirtyConstF;
235                 int vsDirtyConstI;
236                 int vsDirtyConstB;
237
238                 int psDirtyConstF;
239                 int psDirtyConstI;
240                 int psDirtyConstB;
241
242                 std::list<Query*> *queries;
243
244                 int clipFlags;
245
246                 volatile int primitive;    // Current primitive to enter pipeline
247                 volatile int count;        // Number of primitives to render
248                 volatile int references;   // Remaining references to this draw call, 0 when done drawing, -1 when resources unlocked and slot is free
249
250                 DrawData *data;
251         };
252
253         struct Viewport
254         {
255                 float x0;
256                 float y0;
257                 float width;
258                 float height;
259                 float minZ;
260                 float maxZ;
261         };
262
263         class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
264         {
265                 struct Task
266                 {
267                         enum Type
268                         {
269                                 PRIMITIVES,
270                                 PIXELS,
271
272                                 RESUME,
273                                 SUSPEND
274                         };
275
276                         volatile Type type;
277                         volatile int primitiveUnit;
278                         volatile int pixelCluster;
279                 };
280
281                 struct PrimitiveProgress
282                 {
283                         void init()
284                         {
285                                 drawCall = 0;
286                                 firstPrimitive = 0;
287                                 primitiveCount = 0;
288                                 visible = 0;
289                                 references = 0;
290                         }
291
292                         volatile int drawCall;
293                         volatile int firstPrimitive;
294                         volatile int primitiveCount;
295                         volatile int visible;
296                         volatile int references;
297                 };
298
299                 struct PixelProgress
300                 {
301                         void init()
302                         {
303                                 drawCall = 0;
304                                 processedPrimitives = 0;
305                                 executing = false;
306                         }
307
308                         volatile int drawCall;
309                         volatile int processedPrimitives;
310                         volatile bool executing;
311                 };
312
313         public:
314                 Renderer(Context *context, Conventions conventions, bool exactColorRounding);
315
316                 virtual ~Renderer();
317
318                 virtual void clear(void* pixel, Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
319                 virtual void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
320                 virtual void blit3D(Surface *source, Surface *dest);
321                 virtual void draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true);
322
323                 virtual void setIndexBuffer(Resource *indexBuffer);
324
325                 virtual void setMultiSampleMask(unsigned int mask);
326                 virtual void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
327
328                 virtual void setTextureResource(unsigned int sampler, Resource *resource);
329                 virtual void setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type);
330
331                 virtual void setTextureFilter(SamplerType type, int sampler, FilterType textureFilter);
332                 virtual void setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter);
333                 virtual void setGatherEnable(SamplerType type, int sampler, bool enable);
334                 virtual void setAddressingModeU(SamplerType type, int sampler, AddressingMode addressingMode);
335                 virtual void setAddressingModeV(SamplerType type, int sampler, AddressingMode addressingMode);
336                 virtual void setAddressingModeW(SamplerType type, int sampler, AddressingMode addressingMode);
337                 virtual void setReadSRGB(SamplerType type, int sampler, bool sRGB);
338                 virtual void setMipmapLOD(SamplerType type, int sampler, float bias);
339                 virtual void setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor);
340                 virtual void setMaxAnisotropy(SamplerType type, int sampler, float maxAnisotropy);
341                 virtual void setSwizzleR(SamplerType type, int sampler, SwizzleType swizzleR);
342                 virtual void setSwizzleG(SamplerType type, int sampler, SwizzleType swizzleG);
343                 virtual void setSwizzleB(SamplerType type, int sampler, SwizzleType swizzleB);
344                 virtual void setSwizzleA(SamplerType type, int sampler, SwizzleType swizzleA);
345                 
346                 virtual void setPointSpriteEnable(bool pointSpriteEnable);
347                 virtual void setPointScaleEnable(bool pointScaleEnable);
348                 virtual void setLineWidth(float width);
349
350                 virtual void setDepthBias(float bias);
351                 virtual void setSlopeDepthBias(float slopeBias);
352
353                 virtual void setRasterizerDiscard(bool rasterizerDiscard);
354
355                 // Programmable pipelines
356                 virtual void setPixelShader(const PixelShader *shader);
357                 virtual void setVertexShader(const VertexShader *shader);
358
359                 virtual void setPixelShaderConstantF(int index, const float value[4], int count = 1);
360                 virtual void setPixelShaderConstantI(int index, const int value[4], int count = 1);
361                 virtual void setPixelShaderConstantB(int index, const int *boolean, int count = 1);
362
363                 virtual void setVertexShaderConstantF(int index, const float value[4], int count = 1);
364                 virtual void setVertexShaderConstantI(int index, const int value[4], int count = 1);
365                 virtual void setVertexShaderConstantB(int index, const int *boolean, int count = 1);
366
367                 // Viewport & Clipper
368                 virtual void setViewport(const Viewport &viewport);
369                 virtual void setScissor(const Rect &scissor);
370                 virtual void setClipFlags(int flags);
371                 virtual void setClipPlane(unsigned int index, const float plane[4]);
372
373                 // Partial transform
374                 virtual void setModelMatrix(const Matrix &M, int i = 0);
375                 virtual void setViewMatrix(const Matrix &V);
376                 virtual void setBaseMatrix(const Matrix &B);
377                 virtual void setProjectionMatrix(const Matrix &P);
378
379                 virtual void addQuery(Query *query);
380                 virtual void removeQuery(Query *query);
381
382                 void synchronize();
383
384                 #if PERF_HUD
385                         // Performance timers
386                         int getThreadCount();
387                         int64_t getVertexTime(int thread);
388                         int64_t getSetupTime(int thread);
389                         int64_t getPixelTime(int thread);
390                         void resetTimers();
391                 #endif
392
393         private:
394                 static void threadFunction(void *parameters);
395                 void threadLoop(int threadIndex);
396                 void taskLoop(int threadIndex);
397                 void findAvailableTasks();
398                 void scheduleTask(int threadIndex);
399                 void executeTask(int threadIndex);
400                 void finishRendering(Task &pixelTask);
401
402                 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
403
404                 static int setupSolidTriangles(Renderer *renderer, int batch, int count);
405                 static int setupWireframeTriangle(Renderer *renderer, int batch, int count);
406                 static int setupVertexTriangle(Renderer *renderer, int batch, int count);
407                 static int setupLines(Renderer *renderer, int batch, int count);
408                 static int setupPoints(Renderer *renderer, int batch, int count);
409
410                 static bool setupLine(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
411                 static bool setupPoint(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
412
413                 bool isReadWriteTexture(int sampler);
414                 void updateClipper();
415                 void updateConfiguration(bool initialUpdate = false);
416                 static unsigned int computeClipFlags(const float4 &v, const DrawData &data);
417                 void initializeThreads();
418                 void terminateThreads();
419
420                 void loadConstants(const VertexShader *vertexShader);
421                 void loadConstants(const PixelShader *pixelShader);
422
423                 Context *context;
424                 Clipper *clipper;
425                 Viewport viewport;
426                 Rect scissor;
427                 int clipFlags;
428
429                 Triangle *triangleBatch[16];
430                 Primitive *primitiveBatch[16];
431
432                 // User-defined clipping planes
433                 Plane userPlane[MAX_CLIP_PLANES];
434                 Plane clipPlane[MAX_CLIP_PLANES];   // Tranformed to clip space
435                 bool updateClipPlanes;
436
437                 volatile bool exitThreads;
438                 volatile int threadsAwake;
439                 Thread *worker[16];
440                 Event *resume[16];         // Events for resuming threads
441                 Event *suspend[16];        // Events for suspending threads
442                 Event *resumeApp;          // Event for resuming the application thread
443
444                 PrimitiveProgress primitiveProgress[16];
445                 PixelProgress pixelProgress[16];
446                 Task task[16];   // Current tasks for threads
447
448                 enum {DRAW_COUNT = 16};   // Number of draw calls buffered
449                 DrawCall *drawCall[DRAW_COUNT];
450                 DrawCall *drawList[DRAW_COUNT];
451
452                 volatile int currentDraw;
453                 volatile int nextDraw;
454
455                 Task taskQueue[32];
456                 unsigned int qHead;
457                 unsigned int qSize;
458
459                 BackoffLock schedulerMutex;
460
461                 #if PERF_HUD
462                         int64_t vertexTime[16];
463                         int64_t setupTime[16];
464                         int64_t pixelTime[16];
465                 #endif
466
467                 VertexTask *vertexTask[16];
468
469                 SwiftConfig *swiftConfig;
470
471                 std::list<Query*> queries;
472                 Resource *sync;
473
474                 VertexProcessor::State vertexState;
475                 SetupProcessor::State setupState;
476                 PixelProcessor::State pixelState;
477         };
478 }
479
480 #endif   // sw_Renderer_hpp