OSDN Git Service

Blitter clear 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 clear(void* pixel, Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
307                 virtual void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
308                 virtual void blit3D(Surface *source, Surface *dest);
309                 virtual void draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true);
310
311                 virtual void setIndexBuffer(Resource *indexBuffer);
312
313                 virtual void setMultiSampleMask(unsigned int mask);
314                 virtual void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
315
316                 virtual void setTextureResource(unsigned int sampler, Resource *resource);
317                 virtual void setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type);
318
319                 virtual void setTextureFilter(SamplerType type, int sampler, FilterType textureFilter);
320                 virtual void setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter);
321                 virtual void setGatherEnable(SamplerType type, int sampler, bool enable);
322                 virtual void setAddressingModeU(SamplerType type, int sampler, AddressingMode addressingMode);
323                 virtual void setAddressingModeV(SamplerType type, int sampler, AddressingMode addressingMode);
324                 virtual void setAddressingModeW(SamplerType type, int sampler, AddressingMode addressingMode);
325                 virtual void setReadSRGB(SamplerType type, int sampler, bool sRGB);
326                 virtual void setMipmapLOD(SamplerType type, int sampler, float bias);
327                 virtual void setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor);
328                 virtual void setMaxAnisotropy(SamplerType type, int sampler, float maxAnisotropy);
329                 virtual void setSwizzleR(SamplerType type, int sampler, SwizzleType swizzleR);
330                 virtual void setSwizzleG(SamplerType type, int sampler, SwizzleType swizzleG);
331                 virtual void setSwizzleB(SamplerType type, int sampler, SwizzleType swizzleB);
332                 virtual void setSwizzleA(SamplerType type, int sampler, SwizzleType swizzleA);
333                 
334                 virtual void setPointSpriteEnable(bool pointSpriteEnable);
335                 virtual void setPointScaleEnable(bool pointScaleEnable);
336                 virtual void setLineWidth(float width);
337
338                 virtual void setDepthBias(float bias);
339                 virtual void setSlopeDepthBias(float slopeBias);
340
341                 // Programmable pipelines
342                 virtual void setPixelShader(const PixelShader *shader);
343                 virtual void setVertexShader(const VertexShader *shader);
344
345                 virtual void setPixelShaderConstantF(int index, const float value[4], int count = 1);
346                 virtual void setPixelShaderConstantI(int index, const int value[4], int count = 1);
347                 virtual void setPixelShaderConstantB(int index, const int *boolean, int count = 1);
348
349                 virtual void setVertexShaderConstantF(int index, const float value[4], int count = 1);
350                 virtual void setVertexShaderConstantI(int index, const int value[4], int count = 1);
351                 virtual void setVertexShaderConstantB(int index, const int *boolean, int count = 1);
352
353                 // Viewport & Clipper
354                 virtual void setViewport(const Viewport &viewport);
355                 virtual void setScissor(const Rect &scissor);
356                 virtual void setClipFlags(int flags);
357                 virtual void setClipPlane(unsigned int index, const float plane[4]);
358
359                 // Partial transform
360                 virtual void setModelMatrix(const Matrix &M, int i = 0);
361                 virtual void setViewMatrix(const Matrix &V);
362                 virtual void setBaseMatrix(const Matrix &B);
363                 virtual void setProjectionMatrix(const Matrix &P);
364
365                 virtual void addQuery(Query *query);
366                 virtual void removeQuery(Query *query);
367
368                 void synchronize();
369
370                 #if PERF_HUD
371                         // Performance timers
372                         int getThreadCount();
373                         int64_t getVertexTime(int thread);
374                         int64_t getSetupTime(int thread);
375                         int64_t getPixelTime(int thread);
376                         void resetTimers();
377                 #endif
378
379         private:
380                 static void threadFunction(void *parameters);
381                 void threadLoop(int threadIndex);
382                 void taskLoop(int threadIndex);
383                 void findAvailableTasks();
384                 void scheduleTask(int threadIndex);
385                 void executeTask(int threadIndex);
386                 void finishRendering(Task &pixelTask);
387
388                 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
389
390                 static int setupSolidTriangles(Renderer *renderer, int batch, int count);
391                 static int setupWireframeTriangle(Renderer *renderer, int batch, int count);
392                 static int setupVertexTriangle(Renderer *renderer, int batch, int count);
393                 static int setupLines(Renderer *renderer, int batch, int count);
394                 static int setupPoints(Renderer *renderer, int batch, int count);
395
396                 static bool setupLine(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
397                 static bool setupPoint(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
398
399                 bool isReadWriteTexture(int sampler);
400                 void updateClipper();
401                 void updateConfiguration(bool initialUpdate = false);
402                 static unsigned int computeClipFlags(const float4 &v, const DrawData &data);
403                 void initializeThreads();
404                 void terminateThreads();
405
406                 void loadConstants(const VertexShader *vertexShader);
407                 void loadConstants(const PixelShader *pixelShader);
408
409                 Context *context;
410                 Clipper *clipper;
411                 Viewport viewport;
412                 Rect scissor;
413                 int clipFlags;
414
415                 Triangle *triangleBatch[16];
416                 Primitive *primitiveBatch[16];
417
418                 // User-defined clipping planes
419                 Plane userPlane[MAX_CLIP_PLANES];
420                 Plane clipPlane[MAX_CLIP_PLANES];   // Tranformed to clip space
421                 bool updateClipPlanes;
422
423                 volatile bool exitThreads;
424                 volatile int threadsAwake;
425                 Thread *worker[16];
426                 Event *resume[16];         // Events for resuming threads
427                 Event *suspend[16];        // Events for suspending threads
428                 Event *resumeApp;          // Event for resuming the application thread
429
430                 PrimitiveProgress primitiveProgress[16];
431                 PixelProgress pixelProgress[16];
432                 Task task[16];   // Current tasks for threads
433
434                 enum {DRAW_COUNT = 16};   // Number of draw calls buffered
435                 DrawCall *drawCall[DRAW_COUNT];
436                 DrawCall *drawList[DRAW_COUNT];
437
438                 volatile int currentDraw;
439                 volatile int nextDraw;
440
441                 Task taskQueue[32];
442                 unsigned int qHead;
443                 unsigned int qSize;
444
445                 BackoffLock schedulerMutex;
446
447                 #if PERF_HUD
448                         int64_t vertexTime[16];
449                         int64_t setupTime[16];
450                         int64_t pixelTime[16];
451                 #endif
452
453                 VertexTask *vertexTask[16];
454
455                 SwiftConfig *swiftConfig;
456
457                 std::list<Query*> queries;
458                 Resource *sync;
459
460                 VertexProcessor::State vertexState;
461                 SetupProcessor::State setupState;
462                 PixelProcessor::State pixelState;
463         };
464 }
465
466 #endif   // sw_Renderer_hpp