OSDN Git Service

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