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];
127 PixelProcessor::Stencil stencil[2]; // clockwise, counterclockwise
128 PixelProcessor::Factor factor;
129 unsigned int occlusion[16]; // Number of pixels passing depth test
132 int64_t cycles[PERF_TIMERS][16];
141 float viewportHeight;
142 float slopeDepthBias;
147 unsigned int *colorBuffer[RENDERTARGETS];
148 int colorPitchB[RENDERTARGETS];
149 int colorSliceB[RENDERTARGETS];
153 unsigned char *stencilBuffer;
167 PushConstantStorage pushConstants;
170 class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
184 AtomicInt primitiveUnit;
185 AtomicInt pixelCluster;
188 struct PrimitiveProgress
200 AtomicInt firstPrimitive;
201 AtomicInt primitiveCount;
203 AtomicInt references;
211 processedPrimitives = 0;
216 AtomicInt processedPrimitives;
221 Renderer(Context *context, Conventions conventions, bool exactColorRounding);
225 void *operator new(size_t size);
226 void operator delete(void * mem);
228 void draw(DrawType drawType, unsigned int count, bool update = true);
230 void setContext(const sw::Context& context);
232 void setMultiSampleMask(unsigned int mask);
233 void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
235 void setLineWidth(float width);
237 void setDepthBias(float bias);
238 void setSlopeDepthBias(float slopeBias);
240 void setRasterizerDiscard(bool rasterizerDiscard);
242 // Programmable pipelines
243 void setPixelShader(const SpirvShader *shader);
244 void setVertexShader(const SpirvShader *shader);
246 // Viewport & Clipper
247 void setViewport(const VkViewport &viewport);
248 void setScissor(const VkRect2D &scissor);
250 void addQuery(Query *query);
251 void removeQuery(Query *query);
256 // Performance timers
257 int getThreadCount();
258 int64_t getVertexTime(int thread);
259 int64_t getSetupTime(int thread);
260 int64_t getPixelTime(int thread);
264 static int getClusterCount() { return clusterCount; }
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);
275 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
277 int setupTriangles(int batch, int count);
278 int setupLines(int batch, int count);
279 int setupPoints(int batch, int count);
281 bool setupLine(Primitive &primitive, Triangle &triangle, const DrawCall &draw);
282 bool setupPoint(Primitive &primitive, Triangle &triangle, const DrawCall &draw);
284 void updateConfiguration(bool initialUpdate = false);
285 void initializeThreads();
286 void terminateThreads();
295 Triangle *triangleBatch[16];
296 Primitive *primitiveBatch[16];
298 AtomicInt exitThreads;
299 AtomicInt threadsAwake;
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
305 PrimitiveProgress primitiveProgress[16];
306 PixelProgress pixelProgress[16];
307 Task task[16]; // Current tasks for threads
310 DRAW_COUNT = 16, // Number of draw calls buffered (must be power of 2)
311 DRAW_COUNT_BITS = DRAW_COUNT - 1,
313 DrawCall *drawCall[DRAW_COUNT];
314 DrawCall *drawList[DRAW_COUNT];
316 AtomicInt currentDraw;
320 TASK_COUNT = 32, // Size of the task queue (must be power of 2)
321 TASK_COUNT_BITS = TASK_COUNT - 1,
323 Task taskQueue[TASK_COUNT];
327 static AtomicInt unitCount;
328 static AtomicInt clusterCount;
330 MutexLock schedulerMutex;
333 int64_t vertexTime[16];
334 int64_t setupTime[16];
335 int64_t pixelTime[16];
338 VertexTask *vertexTask[16];
340 SwiftConfig *swiftConfig;
342 std::list<Query*> queries;
345 VertexProcessor::State vertexState;
346 SetupProcessor::State setupState;
347 PixelProcessor::State pixelState;
349 Routine *vertexRoutine;
350 Routine *setupRoutine;
351 Routine *pixelRoutine;
363 Routine *vertexRoutine;
364 Routine *setupRoutine;
365 Routine *pixelRoutine;
367 VertexProcessor::RoutinePointer vertexPointer;
368 SetupProcessor::RoutinePointer setupPointer;
369 PixelProcessor::RoutinePointer pixelPointer;
371 int (Renderer::*setupPrimitives)(int batch, int count);
372 SetupProcessor::State setupState;
374 vk::ImageView *renderTarget[RENDERTARGETS];
375 vk::ImageView *depthBuffer;
376 vk::ImageView *stencilBuffer;
378 std::list<Query*> *queries;
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
388 #endif // sw_Renderer_hpp