OSDN Git Service

e51b78897cf9e94b32955968ed975450962f6d7a
[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                 int instanceID;
125                 float lineWidth;
126
127                 PixelProcessor::Stencil stencil[2];   // clockwise, counterclockwise
128                 PixelProcessor::Factor factor;
129                 unsigned int occlusion[16];   // Number of pixels passing depth test
130
131                 #if PERF_PROFILE
132                         int64_t cycles[PERF_TIMERS][16];
133                 #endif
134
135                 float4 Wx16;
136                 float4 Hx16;
137                 float4 X0x16;
138                 float4 Y0x16;
139                 float4 halfPixelX;
140                 float4 halfPixelY;
141                 float viewportHeight;
142                 float slopeDepthBias;
143                 float depthRange;
144                 float depthNear;
145                 Plane clipPlane[6];
146
147                 unsigned int *colorBuffer[RENDERTARGETS];
148                 int colorPitchB[RENDERTARGETS];
149                 int colorSliceB[RENDERTARGETS];
150                 float *depthBuffer;
151                 int depthPitchB;
152                 int depthSliceB;
153                 unsigned char *stencilBuffer;
154                 int stencilPitchB;
155                 int stencilSliceB;
156
157                 int scissorX0;
158                 int scissorX1;
159                 int scissorY0;
160                 int scissorY1;
161
162                 float4 a2c0;
163                 float4 a2c1;
164                 float4 a2c2;
165                 float4 a2c3;
166
167                 PushConstantStorage pushConstants;
168         };
169
170         class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
171         {
172                 struct Task
173                 {
174                         enum Type
175                         {
176                                 PRIMITIVES,
177                                 PIXELS,
178
179                                 RESUME,
180                                 SUSPEND
181                         };
182
183                         AtomicInt type;
184                         AtomicInt primitiveUnit;
185                         AtomicInt pixelCluster;
186                 };
187
188                 struct PrimitiveProgress
189                 {
190                         void init()
191                         {
192                                 drawCall = 0;
193                                 firstPrimitive = 0;
194                                 primitiveCount = 0;
195                                 visible = 0;
196                                 references = 0;
197                         }
198
199                         AtomicInt drawCall;
200                         AtomicInt firstPrimitive;
201                         AtomicInt primitiveCount;
202                         AtomicInt visible;
203                         AtomicInt references;
204                 };
205
206                 struct PixelProgress
207                 {
208                         void init()
209                         {
210                                 drawCall = 0;
211                                 processedPrimitives = 0;
212                                 executing = false;
213                         }
214
215                         AtomicInt drawCall;
216                         AtomicInt processedPrimitives;
217                         AtomicInt executing;
218                 };
219
220         public:
221                 Renderer(Context *context, Conventions conventions, bool exactColorRounding);
222
223                 virtual ~Renderer();
224
225                 void *operator new(size_t size);
226                 void operator delete(void * mem);
227
228                 void draw(DrawType drawType, unsigned int count, bool update = true);
229
230                 void setContext(const sw::Context& context);
231
232                 void setMultiSampleMask(unsigned int mask);
233                 void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
234
235                 void setLineWidth(float width);
236
237                 void setDepthBias(float bias);
238                 void setSlopeDepthBias(float slopeBias);
239
240                 void setRasterizerDiscard(bool rasterizerDiscard);
241
242                 // Programmable pipelines
243                 void setPixelShader(const SpirvShader *shader);
244                 void setVertexShader(const SpirvShader *shader);
245
246                 // Viewport & Clipper
247                 void setViewport(const VkViewport &viewport);
248                 void setScissor(const VkRect2D &scissor);
249
250                 void addQuery(Query *query);
251                 void removeQuery(Query *query);
252
253                 void synchronize();
254
255                 #if PERF_HUD
256                         // Performance timers
257                         int getThreadCount();
258                         int64_t getVertexTime(int thread);
259                         int64_t getSetupTime(int thread);
260                         int64_t getPixelTime(int thread);
261                         void resetTimers();
262                 #endif
263
264                 static int getClusterCount() { return clusterCount; }
265
266         private:
267                 static void threadFunction(void *parameters);
268                 void threadLoop(int threadIndex);
269                 void taskLoop(int threadIndex);
270                 void findAvailableTasks();
271                 void scheduleTask(int threadIndex);
272                 void executeTask(int threadIndex);
273                 void finishRendering(Task &pixelTask);
274
275                 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
276
277                 int setupTriangles(int batch, int count);
278                 int setupLines(int batch, int count);
279                 int setupPoints(int batch, int count);
280
281                 bool setupLine(Primitive &primitive, Triangle &triangle, const DrawCall &draw);
282                 bool setupPoint(Primitive &primitive, Triangle &triangle, const DrawCall &draw);
283
284                 void updateConfiguration(bool initialUpdate = false);
285                 void initializeThreads();
286                 void terminateThreads();
287
288                 Context *context;
289                 Clipper *clipper;
290                 Blitter *blitter;
291                 VkViewport viewport;
292                 VkRect2D scissor;
293                 int clipFlags;
294
295                 Triangle *triangleBatch[16];
296                 Primitive *primitiveBatch[16];
297
298                 AtomicInt exitThreads;
299                 AtomicInt threadsAwake;
300                 Thread *worker[16];
301                 Event *resume[16];         // Events for resuming threads
302                 Event *suspend[16];        // Events for suspending threads
303                 Event *resumeApp;          // Event for resuming the application thread
304
305                 PrimitiveProgress primitiveProgress[16];
306                 PixelProgress pixelProgress[16];
307                 Task task[16];   // Current tasks for threads
308
309                 enum {
310                         DRAW_COUNT = 16,   // Number of draw calls buffered (must be power of 2)
311                         DRAW_COUNT_BITS = DRAW_COUNT - 1,
312                 };
313                 DrawCall *drawCall[DRAW_COUNT];
314                 DrawCall *drawList[DRAW_COUNT];
315
316                 AtomicInt currentDraw;
317                 AtomicInt nextDraw;
318
319                 enum {
320                         TASK_COUNT = 32,   // Size of the task queue (must be power of 2)
321                         TASK_COUNT_BITS = TASK_COUNT - 1,
322                 };
323                 Task taskQueue[TASK_COUNT];
324                 AtomicInt qHead;
325                 AtomicInt qSize;
326
327                 static AtomicInt unitCount;
328                 static AtomicInt clusterCount;
329
330                 MutexLock schedulerMutex;
331
332                 #if PERF_HUD
333                         int64_t vertexTime[16];
334                         int64_t setupTime[16];
335                         int64_t pixelTime[16];
336                 #endif
337
338                 VertexTask *vertexTask[16];
339
340                 SwiftConfig *swiftConfig;
341
342                 std::list<Query*> queries;
343                 Resource *sync;
344
345                 VertexProcessor::State vertexState;
346                 SetupProcessor::State setupState;
347                 PixelProcessor::State pixelState;
348
349                 Routine *vertexRoutine;
350                 Routine *setupRoutine;
351                 Routine *pixelRoutine;
352         };
353
354         struct DrawCall
355         {
356                 DrawCall();
357
358                 ~DrawCall();
359
360                 AtomicInt drawType;
361                 AtomicInt batchSize;
362
363                 Routine *vertexRoutine;
364                 Routine *setupRoutine;
365                 Routine *pixelRoutine;
366
367                 VertexProcessor::RoutinePointer vertexPointer;
368                 SetupProcessor::RoutinePointer setupPointer;
369                 PixelProcessor::RoutinePointer pixelPointer;
370
371                 int (Renderer::*setupPrimitives)(int batch, int count);
372                 SetupProcessor::State setupState;
373
374                 vk::ImageView *renderTarget[RENDERTARGETS];
375                 vk::ImageView *depthBuffer;
376                 vk::ImageView *stencilBuffer;
377
378                 std::list<Query*> *queries;
379
380                 AtomicInt primitive;    // Current primitive to enter pipeline
381                 AtomicInt count;        // Number of primitives to render
382                 AtomicInt references;   // Remaining references to this draw call, 0 when done drawing, -1 when resources unlocked and slot is free
383
384                 DrawData *data;
385         };
386 }
387
388 #endif   // sw_Renderer_hpp