OSDN Git Service

Update SwiftShader to April code dump.
[android-x86/external-swiftshader.git] / src / Renderer / Renderer.hpp
1 // SwiftShader Software Renderer\r
2 //\r
3 // Copyright(c) 2005-2012 TransGaming Inc.\r
4 //\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
10 //\r
11 \r
12 #ifndef sw_Renderer_hpp\r
13 #define sw_Renderer_hpp\r
14 \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
23 \r
24 #include <list>\r
25 \r
26 namespace sw\r
27 {\r
28         class Clipper;\r
29         class PixelShader;\r
30         class VertexShader;\r
31         class SwiftConfig;\r
32         struct Task;\r
33         class Resource;\r
34         class Renderer;\r
35 \r
36         extern int batchSize;\r
37         extern int threadCount;\r
38         extern int unitCount;\r
39         extern int clusterCount;\r
40 \r
41         enum TranscendentalPrecision\r
42         {\r
43                 APPROXIMATE,\r
44                 PARTIAL,        // 2^-10\r
45                 ACCURATE,\r
46                 WHQL,           // 2^-21\r
47                 IEEE            // 2^-23\r
48         };\r
49 \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
55 \r
56         struct Query\r
57         {\r
58                 Query()\r
59                 {\r
60                         building = false;\r
61                         reference = 0;\r
62                         data = 0;\r
63                 }\r
64 \r
65                 void begin()\r
66                 {\r
67                         building = true;\r
68                         data = 0;\r
69                 }\r
70 \r
71                 void end()\r
72                 {\r
73                         building = false;\r
74                 }\r
75 \r
76                 bool building;\r
77                 volatile int reference;\r
78                 volatile unsigned int data;\r
79         };\r
80 \r
81         struct DrawData\r
82         {\r
83                 const void *constants;\r
84 \r
85                 const void *input[16];\r
86                 unsigned int stride[16];\r
87                 Texture mipmap[16 + 4];\r
88                 const void *indices;\r
89 \r
90                 struct VS\r
91                 {\r
92                         float4 c[256 + 1];   // One extra for indices out of range, c[256] = {0, 0, 0, 0}\r
93                         int4 i[16];\r
94                         bool b[16];\r
95                 };\r
96 \r
97                 struct PS\r
98                 {\r
99                         word4 cW[8][4];\r
100                         float4 c[224];\r
101                         int4 i[16];\r
102                         bool b[16];\r
103                 };\r
104 \r
105                 union\r
106                 {\r
107                         VS vs;\r
108                         VertexProcessor::FixedFunction ff;\r
109                 };\r
110 \r
111                 PS ps;\r
112 \r
113                 VertexProcessor::PointSprite point;\r
114 \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
120 \r
121                 #if PERF_PROFILE\r
122                         int64_t cycles[PERF_TIMERS][16];\r
123                 #endif\r
124 \r
125                 TextureStage::Uniforms textureStage[8];\r
126 \r
127                 float4 Wx16;\r
128                 float4 Hx16;\r
129                 float4 X0x16;\r
130                 float4 Y0x16;\r
131                 float4 XXXX;\r
132                 float4 YYYY;\r
133                 float4 halfPixelX;\r
134                 float4 halfPixelY;\r
135                 float viewportHeight;\r
136                 float slopeDepthBias;\r
137                 float depthRange;\r
138                 float depthNear;\r
139                 Plane clipPlane[6];\r
140 \r
141                 unsigned int *colorBuffer[4];\r
142                 int colorPitchB[4];\r
143                 int colorSliceB[4];\r
144                 float *depthBuffer;\r
145                 int depthPitchB;\r
146                 int depthSliceB;\r
147                 unsigned char *stencilBuffer;\r
148                 int stencilPitchB;\r
149                 int stencilSliceB;\r
150 \r
151                 int scissorX0;\r
152                 int scissorX1;\r
153                 int scissorY0;\r
154                 int scissorY1;\r
155 \r
156                 float4 a2c0;\r
157                 float4 a2c1;\r
158                 float4 a2c2;\r
159                 float4 a2c3;\r
160         };\r
161 \r
162         struct DrawCall\r
163         {\r
164                 DrawCall();\r
165 \r
166                 ~DrawCall();\r
167 \r
168                 Context::DrawType drawType;\r
169                 int batchSize;\r
170 \r
171                 Routine *vertexRoutine;\r
172                 Routine *setupRoutine;\r
173                 Routine *pixelRoutine;\r
174 \r
175                 VertexProcessor::RoutinePointer vertexPointer;\r
176                 SetupProcessor::RoutinePointer setupPointer;\r
177                 PixelProcessor::RoutinePointer pixelPointer;\r
178 \r
179                 int (*setupPrimitives)(Renderer *renderer, int batch, int count);\r
180                 SetupProcessor::State setupState;\r
181 \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
187 \r
188                 int vsDirtyConstF;\r
189                 int vsDirtyConstI;\r
190                 int vsDirtyConstB;\r
191 \r
192                 int psDirtyConstF;\r
193                 int psDirtyConstI;\r
194                 int psDirtyConstB;\r
195 \r
196                 std::list<Query*> *queries;\r
197 \r
198                 int clipFlags;\r
199 \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
203 \r
204                 DrawData *data;\r
205         };\r
206 \r
207         struct Viewport\r
208         {\r
209                 float x0;\r
210                 float y0;\r
211                 float width;\r
212                 float height;\r
213                 float minZ;\r
214                 float maxZ;\r
215         };\r
216 \r
217         class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor\r
218         {\r
219                 struct Task\r
220                 {\r
221                         enum Type\r
222                         {\r
223                                 PRIMITIVES,\r
224                                 PIXELS,\r
225 \r
226                                 RESUME,\r
227                                 SUSPEND\r
228                         };\r
229 \r
230                         volatile Type type;\r
231                         volatile int primitiveUnit;\r
232                         volatile int pixelCluster;\r
233                 };\r
234 \r
235                 struct PrimitiveProgress\r
236                 {\r
237                         void init()\r
238                         {\r
239                                 drawCall = 0;\r
240                                 firstPrimitive = 0;\r
241                                 primitiveCount = 0;\r
242                                 visible = 0;\r
243                                 references = 0;\r
244                         }\r
245 \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
251                 };\r
252 \r
253                 struct PixelProgress\r
254                 {\r
255                         void init()\r
256                         {\r
257                                 drawCall = 0;\r
258                                 processedPrimitives = 0;\r
259                                 executing = false;\r
260                         }\r
261 \r
262                         volatile int drawCall;\r
263                         volatile int processedPrimitives;\r
264                         volatile bool executing;\r
265                 };\r
266 \r
267         public:\r
268                 Renderer(Context *context, bool halfIntegerCoordinates, bool symmetricNormalizedDepth, bool booleanFaceRegister, bool fullPixelPositionRegister, bool exactColorRounding);\r
269 \r
270                 virtual ~Renderer();\r
271 \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
274 \r
275                 virtual void setIndexBuffer(Resource *indexBuffer);\r
276 \r
277                 virtual void setMultiSampleMask(unsigned int mask);\r
278                 virtual void setTransparencyAntialiasing(Context::TransparencyAntialiasing transparencyAntialiasing);\r
279 \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
282 \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
293                 \r
294                 virtual void setPointSpriteEnable(bool pointSpriteEnable);\r
295                 virtual void setPointScaleEnable(bool pointScaleEnable);\r
296 \r
297                 virtual void setDepthBias(float bias);\r
298                 virtual void setSlopeDepthBias(float slopeBias);\r
299 \r
300                 // Programmable pipelines\r
301                 virtual void setPixelShader(const PixelShader *shader);\r
302                 virtual void setVertexShader(const VertexShader *shader);\r
303 \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
307 \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
311 \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
317 \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
323 \r
324                 virtual void addQuery(Query *query);\r
325                 virtual void removeQuery(Query *query);\r
326 \r
327                 void synchronize();\r
328 \r
329                 #if PERF_HUD\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
336                 #endif\r
337 \r
338         private:\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
346 \r
347                 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);\r
348 \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
354 \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
357 \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
365 \r
366                 void loadConstants(const VertexShader *vertexShader);\r
367                 void loadConstants(const PixelShader *pixelShader);\r
368 \r
369                 Context *context;\r
370                 Clipper *clipper;\r
371                 Viewport viewport;\r
372                 Rect scissor;\r
373                 int clipFlags;\r
374 \r
375                 Triangle *triangleBatch[16];\r
376                 Primitive *primitiveBatch[16];\r
377 \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
382 \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
389 \r
390                 PrimitiveProgress primitiveProgress[16];\r
391                 PixelProgress pixelProgress[16];\r
392                 Task task[16];   // Current tasks for threads\r
393 \r
394                 enum {DRAW_COUNT = 16};   // Number of draw calls buffered\r
395                 DrawCall *drawCall[DRAW_COUNT];\r
396                 DrawCall *drawList[DRAW_COUNT];\r
397 \r
398                 volatile int currentDraw;\r
399                 volatile int nextDraw;\r
400 \r
401                 Task taskQueue[32];\r
402                 unsigned int qHead;\r
403                 unsigned int qSize;\r
404 \r
405                 BackoffLock mutex;\r
406 \r
407                 #if PERF_HUD\r
408                         int64_t vertexTime[16];\r
409                         int64_t setupTime[16];\r
410                         int64_t pixelTime[16];\r
411                 #endif\r
412 \r
413                 VertexTask *vertexTask[16];\r
414 \r
415                 SwiftConfig *swiftConfig;\r
416 \r
417                 std::list<Query*> queries;\r
418                 Resource *sync;\r
419 \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
424 \r
425                 Routine *vertexRoutine;\r
426                 Routine *setupRoutine;\r
427                 Routine *pixelRoutine;\r
428 \r
429                 Blitter blitter;\r
430         };\r
431 }\r
432 \r
433 #endif   // sw_Renderer_hpp\r