1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef sw_Renderer_hpp
16 #define sw_Renderer_hpp
18 #include "VertexProcessor.hpp"
19 #include "PixelProcessor.hpp"
20 #include "SetupProcessor.hpp"
22 #include "Blitter.hpp"
23 #include "System/MutexLock.hpp"
24 #include "System/Thread.hpp"
25 #include "Device/Config.hpp"
45 enum TranscendentalPrecision
54 extern TranscendentalPrecision logPrecision;
55 extern TranscendentalPrecision expPrecision;
56 extern TranscendentalPrecision rcpPrecision;
57 extern TranscendentalPrecision rsqPrecision;
58 extern bool perspectiveCorrection;
62 bool halfIntegerCoordinates;
63 bool symmetricNormalizedDepth;
64 bool booleanFaceRegister;
65 bool fullPixelPositionRegister;
66 bool colorsDefaultToZero;
69 static const Conventions OpenGL =
71 true, // halfIntegerCoordinates
72 true, // symmetricNormalizedDepth
73 true, // booleanFaceRegister
74 true, // fullPixelPositionRegister
75 true, // colorsDefaultToZero
78 static const Conventions Direct3D =
80 false, // halfIntegerCoordinates
81 false, // symmetricNormalizedDepth
82 false, // booleanFaceRegister
83 false, // fullPixelPositionRegister
84 false, // colorsDefaultToZero
89 enum Type { FRAGMENTS_PASSED, TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN };
91 Query(Type type) : building(false), reference(0), data(0), type(type)
115 const Constants *constants;
117 vk::DescriptorSet *descriptorSets[vk::MAX_BOUND_DESCRIPTOR_SETS];
119 const void *input[MAX_VERTEX_INPUTS];
120 unsigned int stride[MAX_VERTEX_INPUTS];
121 Texture mipmap[TOTAL_IMAGE_UNITS];
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
139 float4 c[FRAGMENT_UNIFORM_VECTORS];
140 byte* u[MAX_UNIFORM_BUFFER_BINDINGS];
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
157 int64_t cycles[PERF_TIMERS][16];
166 float viewportHeight;
167 float slopeDepthBias;
172 unsigned int *colorBuffer[RENDERTARGETS];
173 int colorPitchB[RENDERTARGETS];
174 int colorSliceB[RENDERTARGETS];
178 unsigned char *stencilBuffer;
192 PushConstantStorage pushConstants;
195 class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
209 AtomicInt primitiveUnit;
210 AtomicInt pixelCluster;
213 struct PrimitiveProgress
225 AtomicInt firstPrimitive;
226 AtomicInt primitiveCount;
228 AtomicInt references;
236 processedPrimitives = 0;
241 AtomicInt processedPrimitives;
246 Renderer(Context *context, Conventions conventions, bool exactColorRounding);
250 void *operator new(size_t size);
251 void operator delete(void * mem);
253 void draw(DrawType drawType, unsigned int count, bool update = true);
255 void setContext(const sw::Context& context);
257 void setMultiSampleMask(unsigned int mask);
258 void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
260 void setLineWidth(float width);
262 void setDepthBias(float bias);
263 void setSlopeDepthBias(float slopeBias);
265 void setRasterizerDiscard(bool rasterizerDiscard);
267 // Programmable pipelines
268 void setPixelShader(const SpirvShader *shader);
269 void setVertexShader(const SpirvShader *shader);
271 // Viewport & Clipper
272 void setViewport(const VkViewport &viewport);
273 void setScissor(const VkRect2D &scissor);
275 void addQuery(Query *query);
276 void removeQuery(Query *query);
281 // Performance timers
282 int getThreadCount();
283 int64_t getVertexTime(int thread);
284 int64_t getSetupTime(int thread);
285 int64_t getPixelTime(int thread);
289 static int getClusterCount() { return clusterCount; }
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);
300 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
302 int setupTriangles(int batch, int count);
303 int setupLines(int batch, int count);
304 int setupPoints(int batch, int count);
306 bool setupLine(Primitive &primitive, Triangle &triangle, const DrawCall &draw);
307 bool setupPoint(Primitive &primitive, Triangle &triangle, const DrawCall &draw);
309 void updateConfiguration(bool initialUpdate = false);
310 void initializeThreads();
311 void terminateThreads();
320 Triangle *triangleBatch[16];
321 Primitive *primitiveBatch[16];
323 AtomicInt exitThreads;
324 AtomicInt threadsAwake;
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
330 PrimitiveProgress primitiveProgress[16];
331 PixelProgress pixelProgress[16];
332 Task task[16]; // Current tasks for threads
335 DRAW_COUNT = 16, // Number of draw calls buffered (must be power of 2)
336 DRAW_COUNT_BITS = DRAW_COUNT - 1,
338 DrawCall *drawCall[DRAW_COUNT];
339 DrawCall *drawList[DRAW_COUNT];
341 AtomicInt currentDraw;
345 TASK_COUNT = 32, // Size of the task queue (must be power of 2)
346 TASK_COUNT_BITS = TASK_COUNT - 1,
348 Task taskQueue[TASK_COUNT];
352 static AtomicInt unitCount;
353 static AtomicInt clusterCount;
355 MutexLock schedulerMutex;
358 int64_t vertexTime[16];
359 int64_t setupTime[16];
360 int64_t pixelTime[16];
363 VertexTask *vertexTask[16];
365 SwiftConfig *swiftConfig;
367 std::list<Query*> queries;
370 VertexProcessor::State vertexState;
371 SetupProcessor::State setupState;
372 PixelProcessor::State pixelState;
374 Routine *vertexRoutine;
375 Routine *setupRoutine;
376 Routine *pixelRoutine;
388 Routine *vertexRoutine;
389 Routine *setupRoutine;
390 Routine *pixelRoutine;
392 VertexProcessor::RoutinePointer vertexPointer;
393 SetupProcessor::RoutinePointer setupPointer;
394 PixelProcessor::RoutinePointer pixelPointer;
396 int (Renderer::*setupPrimitives)(int batch, int count);
397 SetupProcessor::State setupState;
399 vk::ImageView *renderTarget[RENDERTARGETS];
400 vk::ImageView *depthBuffer;
401 vk::ImageView *stencilBuffer;
403 std::list<Query*> *queries;
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
413 #endif // sw_Renderer_hpp