OSDN Git Service

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