OSDN Git Service

66768b4c01814a8d84841d06f1fd4b23e514ad30
[android-x86/external-swiftshader.git] / src / Device / 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 "System/MutexLock.hpp"
24 #include "System/Thread.hpp"
25 #include "Device/Config.hpp"
26
27 #include <list>
28
29 namespace vk
30 {
31         class DescriptorSet;
32 }
33
34 namespace sw
35 {
36         class Clipper;
37         struct DrawCall;
38         class PixelShader;
39         class VertexShader;
40         class SwiftConfig;
41         struct Task;
42         class Resource;
43         struct Constants;
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 colorsDefaultToZero;
67         };
68
69         static const Conventions OpenGL =
70         {
71                 true,    // halfIntegerCoordinates
72                 true,    // symmetricNormalizedDepth
73                 true,    // booleanFaceRegister
74                 true,    // fullPixelPositionRegister
75                 true,    // colorsDefaultToZero
76         };
77
78         static const Conventions Direct3D =
79         {
80                 false,   // halfIntegerCoordinates
81                 false,   // symmetricNormalizedDepth
82                 false,   // booleanFaceRegister
83                 false,   // fullPixelPositionRegister
84                 false,   // colorsDefaultToZero
85         };
86
87         struct Query
88         {
89                 enum Type { FRAGMENTS_PASSED, TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN };
90
91                 Query(Type type) : building(false), reference(0), data(0), type(type)
92                 {
93                 }
94
95                 void begin()
96                 {
97                         building = true;
98                         data = 0;
99                 }
100
101                 void end()
102                 {
103                         building = false;
104                 }
105
106                 bool building;
107                 AtomicInt reference;
108                 AtomicInt data;
109
110                 const Type type;
111         };
112
113         struct DrawData
114         {
115                 const Constants *constants;
116
117                 vk::DescriptorSet *descriptorSets[vk::MAX_BOUND_DESCRIPTOR_SETS];
118
119                 const void *input[MAX_VERTEX_INPUTS];
120                 unsigned int stride[MAX_VERTEX_INPUTS];
121                 Texture mipmap[TOTAL_IMAGE_UNITS];
122                 const void *indices;
123
124                 struct VS
125                 {
126                         float4 c[VERTEX_UNIFORM_VECTORS + 1];   // One extra for indices out of range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
127                         byte* u[MAX_UNIFORM_BUFFER_BINDINGS];
128                         byte* t[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS];
129                         unsigned int reg[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Offset used when reading from registers, in components
130                         unsigned int row[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of rows to read
131                         unsigned int col[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of columns to read
132                         unsigned int str[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of components between each varying in output buffer
133                         int4 i[16];
134                         bool b[16];
135                 };
136
137                 struct PS
138                 {
139                         float4 c[FRAGMENT_UNIFORM_VECTORS];
140                         byte* u[MAX_UNIFORM_BUFFER_BINDINGS];
141                         int4 i[16];
142                         bool b[16];
143                 };
144
145                 VS vs;
146                 PS ps;
147
148                 int instanceID;
149                 float lineWidth;
150
151                 PixelProcessor::Stencil stencil[2];   // clockwise, counterclockwise
152                 PixelProcessor::Stencil stencilCCW;
153                 PixelProcessor::Factor factor;
154                 unsigned int occlusion[16];   // Number of pixels passing depth test
155
156                 #if PERF_PROFILE
157                         int64_t cycles[PERF_TIMERS][16];
158                 #endif
159
160                 float4 Wx16;
161                 float4 Hx16;
162                 float4 X0x16;
163                 float4 Y0x16;
164                 float4 halfPixelX;
165                 float4 halfPixelY;
166                 float viewportHeight;
167                 float slopeDepthBias;
168                 float depthRange;
169                 float depthNear;
170                 Plane clipPlane[6];
171
172                 unsigned int *colorBuffer[RENDERTARGETS];
173                 int colorPitchB[RENDERTARGETS];
174                 int colorSliceB[RENDERTARGETS];
175                 float *depthBuffer;
176                 int depthPitchB;
177                 int depthSliceB;
178                 unsigned char *stencilBuffer;
179                 int stencilPitchB;
180                 int stencilSliceB;
181
182                 int scissorX0;
183                 int scissorX1;
184                 int scissorY0;
185                 int scissorY1;
186
187                 float4 a2c0;
188                 float4 a2c1;
189                 float4 a2c2;
190                 float4 a2c3;
191
192                 PushConstantStorage pushConstants;
193         };
194
195         class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
196         {
197                 struct Task
198                 {
199                         enum Type
200                         {
201                                 PRIMITIVES,
202                                 PIXELS,
203
204                                 RESUME,
205                                 SUSPEND
206                         };
207
208                         AtomicInt type;
209                         AtomicInt primitiveUnit;
210                         AtomicInt pixelCluster;
211                 };
212
213                 struct PrimitiveProgress
214                 {
215                         void init()
216                         {
217                                 drawCall = 0;
218                                 firstPrimitive = 0;
219                                 primitiveCount = 0;
220                                 visible = 0;
221                                 references = 0;
222                         }
223
224                         AtomicInt drawCall;
225                         AtomicInt firstPrimitive;
226                         AtomicInt primitiveCount;
227                         AtomicInt visible;
228                         AtomicInt references;
229                 };
230
231                 struct PixelProgress
232                 {
233                         void init()
234                         {
235                                 drawCall = 0;
236                                 processedPrimitives = 0;
237                                 executing = false;
238                         }
239
240                         AtomicInt drawCall;
241                         AtomicInt processedPrimitives;
242                         AtomicInt executing;
243                 };
244
245         public:
246                 Renderer(Context *context, Conventions conventions, bool exactColorRounding);
247
248                 virtual ~Renderer();
249
250                 void *operator new(size_t size);
251                 void operator delete(void * mem);
252
253                 void draw(DrawType drawType, unsigned int count, bool update = true);
254
255                 void setContext(const sw::Context& context);
256
257                 void setMultiSampleMask(unsigned int mask);
258                 void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
259
260                 void setLineWidth(float width);
261
262                 void setDepthBias(float bias);
263                 void setSlopeDepthBias(float slopeBias);
264
265                 void setRasterizerDiscard(bool rasterizerDiscard);
266
267                 // Programmable pipelines
268                 void setPixelShader(const SpirvShader *shader);
269                 void setVertexShader(const SpirvShader *shader);
270
271                 // Viewport & Clipper
272                 void setViewport(const VkViewport &viewport);
273                 void setScissor(const VkRect2D &scissor);
274
275                 void addQuery(Query *query);
276                 void removeQuery(Query *query);
277
278                 void synchronize();
279
280                 #if PERF_HUD
281                         // Performance timers
282                         int getThreadCount();
283                         int64_t getVertexTime(int thread);
284                         int64_t getSetupTime(int thread);
285                         int64_t getPixelTime(int thread);
286                         void resetTimers();
287                 #endif
288
289                 static int getClusterCount() { return clusterCount; }
290
291         private:
292                 static void threadFunction(void *parameters);
293                 void threadLoop(int threadIndex);
294                 void taskLoop(int threadIndex);
295                 void findAvailableTasks();
296                 void scheduleTask(int threadIndex);
297                 void executeTask(int threadIndex);
298                 void finishRendering(Task &pixelTask);
299
300                 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
301
302                 int setupTriangles(int batch, int count);
303                 int setupLines(int batch, int count);
304                 int setupPoints(int batch, int count);
305
306                 bool setupLine(Primitive &primitive, Triangle &triangle, const DrawCall &draw);
307                 bool setupPoint(Primitive &primitive, Triangle &triangle, const DrawCall &draw);
308
309                 void updateConfiguration(bool initialUpdate = false);
310                 void initializeThreads();
311                 void terminateThreads();
312
313                 Context *context;
314                 Clipper *clipper;
315                 Blitter *blitter;
316                 VkViewport viewport;
317                 VkRect2D scissor;
318                 int clipFlags;
319
320                 Triangle *triangleBatch[16];
321                 Primitive *primitiveBatch[16];
322
323                 AtomicInt exitThreads;
324                 AtomicInt threadsAwake;
325                 Thread *worker[16];
326                 Event *resume[16];         // Events for resuming threads
327                 Event *suspend[16];        // Events for suspending threads
328                 Event *resumeApp;          // Event for resuming the application thread
329
330                 PrimitiveProgress primitiveProgress[16];
331                 PixelProgress pixelProgress[16];
332                 Task task[16];   // Current tasks for threads
333
334                 enum {
335                         DRAW_COUNT = 16,   // Number of draw calls buffered (must be power of 2)
336                         DRAW_COUNT_BITS = DRAW_COUNT - 1,
337                 };
338                 DrawCall *drawCall[DRAW_COUNT];
339                 DrawCall *drawList[DRAW_COUNT];
340
341                 AtomicInt currentDraw;
342                 AtomicInt nextDraw;
343
344                 enum {
345                         TASK_COUNT = 32,   // Size of the task queue (must be power of 2)
346                         TASK_COUNT_BITS = TASK_COUNT - 1,
347                 };
348                 Task taskQueue[TASK_COUNT];
349                 AtomicInt qHead;
350                 AtomicInt qSize;
351
352                 static AtomicInt unitCount;
353                 static AtomicInt clusterCount;
354
355                 MutexLock schedulerMutex;
356
357                 #if PERF_HUD
358                         int64_t vertexTime[16];
359                         int64_t setupTime[16];
360                         int64_t pixelTime[16];
361                 #endif
362
363                 VertexTask *vertexTask[16];
364
365                 SwiftConfig *swiftConfig;
366
367                 std::list<Query*> queries;
368                 Resource *sync;
369
370                 VertexProcessor::State vertexState;
371                 SetupProcessor::State setupState;
372                 PixelProcessor::State pixelState;
373
374                 Routine *vertexRoutine;
375                 Routine *setupRoutine;
376                 Routine *pixelRoutine;
377         };
378
379         struct DrawCall
380         {
381                 DrawCall();
382
383                 ~DrawCall();
384
385                 AtomicInt drawType;
386                 AtomicInt batchSize;
387
388                 Routine *vertexRoutine;
389                 Routine *setupRoutine;
390                 Routine *pixelRoutine;
391
392                 VertexProcessor::RoutinePointer vertexPointer;
393                 SetupProcessor::RoutinePointer setupPointer;
394                 PixelProcessor::RoutinePointer pixelPointer;
395
396                 int (Renderer::*setupPrimitives)(int batch, int count);
397                 SetupProcessor::State setupState;
398
399                 vk::ImageView *renderTarget[RENDERTARGETS];
400                 vk::ImageView *depthBuffer;
401                 vk::ImageView *stencilBuffer;
402
403                 std::list<Query*> *queries;
404
405                 AtomicInt primitive;    // Current primitive to enter pipeline
406                 AtomicInt count;        // Number of primitives to render
407                 AtomicInt references;   // Remaining references to this draw call, 0 when done drawing, -1 when resources unlocked and slot is free
408
409                 DrawData *data;
410         };
411 }
412
413 #endif   // sw_Renderer_hpp