OSDN Git Service

Make Blitter part of Renderer.
[android-x86/external-swiftshader.git] / src / Renderer / Renderer.hpp
1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef sw_Renderer_hpp
16 #define sw_Renderer_hpp
17
18 #include "VertexProcessor.hpp"
19 #include "PixelProcessor.hpp"
20 #include "SetupProcessor.hpp"
21 #include "Plane.hpp"
22 #include "Blitter.hpp"
23 #include "Common/MutexLock.hpp"
24 #include "Common/Thread.hpp"
25 #include "Main/Config.hpp"
26
27 #include <list>
28
29 namespace sw
30 {
31         class Clipper;
32         class PixelShader;
33         class VertexShader;
34         class SwiftConfig;
35         struct Task;
36         class Resource;
37         class Renderer;
38         struct Constants;
39
40         extern int batchSize;
41         extern int threadCount;
42         extern int unitCount;
43         extern int clusterCount;
44
45         enum TranscendentalPrecision
46         {
47                 APPROXIMATE,
48                 PARTIAL,        // 2^-10
49                 ACCURATE,
50                 WHQL,           // 2^-21
51                 IEEE            // 2^-23
52         };
53
54         extern TranscendentalPrecision logPrecision;
55         extern TranscendentalPrecision expPrecision;
56         extern TranscendentalPrecision rcpPrecision;
57         extern TranscendentalPrecision rsqPrecision;
58         extern bool perspectiveCorrection;
59
60         struct Conventions
61         {
62                 bool halfIntegerCoordinates;
63                 bool symmetricNormalizedDepth;
64                 bool booleanFaceRegister;
65                 bool fullPixelPositionRegister;
66                 bool leadingVertexFirst;
67                 bool secondaryColor;
68         };
69
70         static const Conventions OpenGL =
71         {
72                 true,    // halfIntegerCoordinates
73                 true,    // symmetricNormalizedDepth
74                 true,    // booleanFaceRegister
75                 true,    // fullPixelPositionRegister
76                 false,   // leadingVertexFirst
77                 false    // secondaryColor
78         };
79
80         static const Conventions Direct3D =
81         {
82                 false,   // halfIntegerCoordinates
83                 false,   // symmetricNormalizedDepth
84                 false,   // booleanFaceRegister
85                 false,   // fullPixelPositionRegister
86                 true,    // leadingVertexFirst
87                 true,    // secondardyColor
88         };
89
90         struct Query
91         {
92                 enum Type { FRAGMENTS_PASSED, TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN };
93
94                 Query(Type type) : building(false), reference(0), data(0), type(type)
95                 {
96                 }
97
98                 void begin()
99                 {
100                         building = true;
101                         data = 0;
102                 }
103
104                 void end()
105                 {
106                         building = false;
107                 }
108
109                 bool building;
110                 volatile int reference;
111                 volatile unsigned int data;
112
113                 const Type type;
114         };
115
116         struct DrawData
117         {
118                 const Constants *constants;
119
120                 const void *input[MAX_VERTEX_INPUTS];
121                 unsigned int stride[MAX_VERTEX_INPUTS];
122                 Texture mipmap[TOTAL_IMAGE_UNITS];
123                 const void *indices;
124
125                 struct VS
126                 {
127                         float4 c[VERTEX_UNIFORM_VECTORS + 1];   // One extra for indices out of range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
128                         byte* u[MAX_UNIFORM_BUFFER_BINDINGS];
129                         byte* t[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS];
130                         unsigned int reg[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Offset used when reading from registers, in components
131                         unsigned int row[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of rows to read
132                         unsigned int col[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of columns to read
133                         unsigned int str[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of components between each varying in output buffer
134                         int4 i[16];
135                         bool b[16];
136                 };
137
138                 struct PS
139                 {
140                         word4 cW[8][4];
141                         float4 c[FRAGMENT_UNIFORM_VECTORS];
142                         byte* u[MAX_UNIFORM_BUFFER_BINDINGS];
143                         int4 i[16];
144                         bool b[16];
145                 };
146
147                 union
148                 {
149                         VS vs;
150                         VertexProcessor::FixedFunction ff;
151                 };
152
153                 PS ps;
154
155                 int instanceID;
156
157                 VertexProcessor::PointSprite point;
158                 float lineWidth;
159
160                 PixelProcessor::Stencil stencil[2];   // clockwise, counterclockwise
161                 PixelProcessor::Stencil stencilCCW;
162                 PixelProcessor::Fog fog;
163                 PixelProcessor::Factor factor;
164                 unsigned int occlusion[16];   // Number of pixels passing depth test
165
166                 #if PERF_PROFILE
167                         int64_t cycles[PERF_TIMERS][16];
168                 #endif
169
170                 TextureStage::Uniforms textureStage[8];
171
172                 float4 Wx16;
173                 float4 Hx16;
174                 float4 X0x16;
175                 float4 Y0x16;
176                 float4 XXXX;
177                 float4 YYYY;
178                 float4 halfPixelX;
179                 float4 halfPixelY;
180                 float viewportHeight;
181                 float slopeDepthBias;
182                 float depthRange;
183                 float depthNear;
184                 Plane clipPlane[6];
185
186                 unsigned int *colorBuffer[RENDERTARGETS];
187                 int colorPitchB[RENDERTARGETS];
188                 int colorSliceB[RENDERTARGETS];
189                 float *depthBuffer;
190                 int depthPitchB;
191                 int depthSliceB;
192                 unsigned char *stencilBuffer;
193                 int stencilPitchB;
194                 int stencilSliceB;
195
196                 int scissorX0;
197                 int scissorX1;
198                 int scissorY0;
199                 int scissorY1;
200
201                 float4 a2c0;
202                 float4 a2c1;
203                 float4 a2c2;
204                 float4 a2c3;
205         };
206
207         struct DrawCall
208         {
209                 DrawCall();
210
211                 ~DrawCall();
212
213                 DrawType drawType;
214                 int batchSize;
215
216                 Routine *vertexRoutine;
217                 Routine *setupRoutine;
218                 Routine *pixelRoutine;
219
220                 VertexProcessor::RoutinePointer vertexPointer;
221                 SetupProcessor::RoutinePointer setupPointer;
222                 PixelProcessor::RoutinePointer pixelPointer;
223
224                 int (Renderer::*setupPrimitives)(int batch, int count);
225                 SetupProcessor::State setupState;
226
227                 Resource *vertexStream[MAX_VERTEX_INPUTS];
228                 Resource *indexBuffer;
229                 Surface *renderTarget[RENDERTARGETS];
230                 Surface *depthBuffer;
231                 Surface *stencilBuffer;
232                 Resource *texture[TOTAL_IMAGE_UNITS];
233                 Resource* pUniformBuffers[MAX_UNIFORM_BUFFER_BINDINGS];
234                 Resource* vUniformBuffers[MAX_UNIFORM_BUFFER_BINDINGS];
235                 Resource* transformFeedbackBuffers[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS];
236
237                 int vsDirtyConstF;
238                 int vsDirtyConstI;
239                 int vsDirtyConstB;
240
241                 int psDirtyConstF;
242                 int psDirtyConstI;
243                 int psDirtyConstB;
244
245                 std::list<Query*> *queries;
246
247                 int clipFlags;
248
249                 volatile int primitive;    // Current primitive to enter pipeline
250                 volatile int count;        // Number of primitives to render
251                 volatile int references;   // Remaining references to this draw call, 0 when done drawing, -1 when resources unlocked and slot is free
252
253                 DrawData *data;
254         };
255
256         struct Viewport
257         {
258                 float x0;
259                 float y0;
260                 float width;
261                 float height;
262                 float minZ;
263                 float maxZ;
264         };
265
266         class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
267         {
268                 struct Task
269                 {
270                         enum Type
271                         {
272                                 PRIMITIVES,
273                                 PIXELS,
274
275                                 RESUME,
276                                 SUSPEND
277                         };
278
279                         volatile Type type;
280                         volatile int primitiveUnit;
281                         volatile int pixelCluster;
282                 };
283
284                 struct PrimitiveProgress
285                 {
286                         void init()
287                         {
288                                 drawCall = 0;
289                                 firstPrimitive = 0;
290                                 primitiveCount = 0;
291                                 visible = 0;
292                                 references = 0;
293                         }
294
295                         volatile int drawCall;
296                         volatile int firstPrimitive;
297                         volatile int primitiveCount;
298                         volatile int visible;
299                         volatile int references;
300                 };
301
302                 struct PixelProgress
303                 {
304                         void init()
305                         {
306                                 drawCall = 0;
307                                 processedPrimitives = 0;
308                                 executing = false;
309                         }
310
311                         volatile int drawCall;
312                         volatile int processedPrimitives;
313                         volatile bool executing;
314                 };
315
316         public:
317                 Renderer(Context *context, Conventions conventions, bool exactColorRounding);
318
319                 virtual ~Renderer();
320
321                 void *operator new(size_t size);
322                 void operator delete(void * mem);
323
324                 void draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true);
325
326                 void clear(void* pixel, Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
327                 void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter, bool isStencil = false);
328                 void blit3D(Surface *source, Surface *dest);
329
330                 void setIndexBuffer(Resource *indexBuffer);
331
332                 void setMultiSampleMask(unsigned int mask);
333                 void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
334
335                 void setTextureResource(unsigned int sampler, Resource *resource);
336                 void setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type);
337
338                 void setTextureFilter(SamplerType type, int sampler, FilterType textureFilter);
339                 void setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter);
340                 void setGatherEnable(SamplerType type, int sampler, bool enable);
341                 void setAddressingModeU(SamplerType type, int sampler, AddressingMode addressingMode);
342                 void setAddressingModeV(SamplerType type, int sampler, AddressingMode addressingMode);
343                 void setAddressingModeW(SamplerType type, int sampler, AddressingMode addressingMode);
344                 void setReadSRGB(SamplerType type, int sampler, bool sRGB);
345                 void setMipmapLOD(SamplerType type, int sampler, float bias);
346                 void setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor);
347                 void setMaxAnisotropy(SamplerType type, int sampler, float maxAnisotropy);
348                 void setSwizzleR(SamplerType type, int sampler, SwizzleType swizzleR);
349                 void setSwizzleG(SamplerType type, int sampler, SwizzleType swizzleG);
350                 void setSwizzleB(SamplerType type, int sampler, SwizzleType swizzleB);
351                 void setSwizzleA(SamplerType type, int sampler, SwizzleType swizzleA);
352                 void setBaseLevel(SamplerType type, int sampler, int baseLevel);
353                 void setMaxLevel(SamplerType type, int sampler, int maxLevel);
354                 void setMinLod(SamplerType type, int sampler, float minLod);
355                 void setMaxLod(SamplerType type, int sampler, float maxLod);
356
357                 void setPointSpriteEnable(bool pointSpriteEnable);
358                 void setPointScaleEnable(bool pointScaleEnable);
359                 void setLineWidth(float width);
360
361                 void setDepthBias(float bias);
362                 void setSlopeDepthBias(float slopeBias);
363
364                 void setRasterizerDiscard(bool rasterizerDiscard);
365
366                 // Programmable pipelines
367                 void setPixelShader(const PixelShader *shader);
368                 void setVertexShader(const VertexShader *shader);
369
370                 void setPixelShaderConstantF(int index, const float value[4], int count = 1);
371                 void setPixelShaderConstantI(int index, const int value[4], int count = 1);
372                 void setPixelShaderConstantB(int index, const int *boolean, int count = 1);
373
374                 void setVertexShaderConstantF(int index, const float value[4], int count = 1);
375                 void setVertexShaderConstantI(int index, const int value[4], int count = 1);
376                 void setVertexShaderConstantB(int index, const int *boolean, int count = 1);
377
378                 // Viewport & Clipper
379                 void setViewport(const Viewport &viewport);
380                 void setScissor(const Rect &scissor);
381                 void setClipFlags(int flags);
382                 void setClipPlane(unsigned int index, const float plane[4]);
383
384                 // Partial transform
385                 void setModelMatrix(const Matrix &M, int i = 0);
386                 void setViewMatrix(const Matrix &V);
387                 void setBaseMatrix(const Matrix &B);
388                 void setProjectionMatrix(const Matrix &P);
389
390                 void addQuery(Query *query);
391                 void removeQuery(Query *query);
392
393                 void synchronize();
394
395                 #if PERF_HUD
396                         // Performance timers
397                         int getThreadCount();
398                         int64_t getVertexTime(int thread);
399                         int64_t getSetupTime(int thread);
400                         int64_t getPixelTime(int thread);
401                         void resetTimers();
402                 #endif
403
404         private:
405                 static void threadFunction(void *parameters);
406                 void threadLoop(int threadIndex);
407                 void taskLoop(int threadIndex);
408                 void findAvailableTasks();
409                 void scheduleTask(int threadIndex);
410                 void executeTask(int threadIndex);
411                 void finishRendering(Task &pixelTask);
412
413                 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
414
415                 int setupSolidTriangles(int batch, int count);
416                 int setupWireframeTriangle(int batch, int count);
417                 int setupVertexTriangle(int batch, int count);
418                 int setupLines(int batch, int count);
419                 int setupPoints(int batch, int count);
420
421                 bool setupLine(Primitive &primitive, Triangle &triangle, const DrawCall &draw);
422                 bool setupPoint(Primitive &primitive, Triangle &triangle, const DrawCall &draw);
423
424                 bool isReadWriteTexture(int sampler);
425                 void updateClipper();
426                 void updateConfiguration(bool initialUpdate = false);
427                 void initializeThreads();
428                 void terminateThreads();
429
430                 void loadConstants(const VertexShader *vertexShader);
431                 void loadConstants(const PixelShader *pixelShader);
432
433                 Context *context;
434                 Clipper *clipper;
435                 Blitter *blitter;
436                 Viewport viewport;
437                 Rect scissor;
438                 int clipFlags;
439
440                 Triangle *triangleBatch[16];
441                 Primitive *primitiveBatch[16];
442
443                 // User-defined clipping planes
444                 Plane userPlane[MAX_CLIP_PLANES];
445                 Plane clipPlane[MAX_CLIP_PLANES];   // Tranformed to clip space
446                 bool updateClipPlanes;
447
448                 volatile bool exitThreads;
449                 volatile int threadsAwake;
450                 Thread *worker[16];
451                 Event *resume[16];         // Events for resuming threads
452                 Event *suspend[16];        // Events for suspending threads
453                 Event *resumeApp;          // Event for resuming the application thread
454
455                 PrimitiveProgress primitiveProgress[16];
456                 PixelProgress pixelProgress[16];
457                 Task task[16];   // Current tasks for threads
458
459                 enum {DRAW_COUNT = 16};   // Number of draw calls buffered
460                 DrawCall *drawCall[DRAW_COUNT];
461                 DrawCall *drawList[DRAW_COUNT];
462
463                 volatile int currentDraw;
464                 volatile int nextDraw;
465
466                 Task taskQueue[32];
467                 unsigned int qHead;
468                 unsigned int qSize;
469
470                 MutexLock schedulerMutex;
471
472                 #if PERF_HUD
473                         int64_t vertexTime[16];
474                         int64_t setupTime[16];
475                         int64_t pixelTime[16];
476                 #endif
477
478                 VertexTask *vertexTask[16];
479
480                 SwiftConfig *swiftConfig;
481
482                 std::list<Query*> queries;
483                 Resource *sync;
484
485                 VertexProcessor::State vertexState;
486                 SetupProcessor::State setupState;
487                 PixelProcessor::State pixelState;
488
489                 Routine *vertexRoutine;
490                 Routine *setupRoutine;
491                 Routine *pixelRoutine;
492         };
493 }
494
495 #endif   // sw_Renderer_hpp