OSDN Git Service

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