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 "Common/MutexLock.hpp"
24 #include "Common/Thread.hpp"
25 #include "Main/Config.hpp"
41 extern int threadCount;
43 extern int clusterCount;
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 leadingVertexFirst;
70 static const Conventions OpenGL =
72 true, // halfIntegerCoordinates
73 true, // symmetricNormalizedDepth
74 true, // booleanFaceRegister
75 true, // fullPixelPositionRegister
76 false, // leadingVertexFirst
77 false // secondaryColor
80 static const Conventions Direct3D =
82 false, // halfIntegerCoordinates
83 false, // symmetricNormalizedDepth
84 false, // booleanFaceRegister
85 false, // fullPixelPositionRegister
86 true, // leadingVertexFirst
87 true, // secondardyColor
92 enum Type { FRAGMENTS_PASSED, TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN };
94 Query(Type type) : building(false), reference(0), data(0), type(type)
110 volatile int reference;
111 volatile unsigned int data;
118 const Constants *constants;
120 const void *input[MAX_VERTEX_INPUTS];
121 unsigned int stride[MAX_VERTEX_INPUTS];
122 Texture mipmap[TOTAL_IMAGE_UNITS];
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
141 float4 c[FRAGMENT_UNIFORM_VECTORS];
142 byte* u[MAX_UNIFORM_BUFFER_BINDINGS];
150 VertexProcessor::FixedFunction ff;
157 VertexProcessor::PointSprite point;
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
167 int64_t cycles[PERF_TIMERS][16];
170 TextureStage::Uniforms textureStage[8];
180 float viewportHeight;
181 float slopeDepthBias;
186 unsigned int *colorBuffer[RENDERTARGETS];
187 int colorPitchB[RENDERTARGETS];
188 int colorSliceB[RENDERTARGETS];
192 unsigned char *stencilBuffer;
216 Routine *vertexRoutine;
217 Routine *setupRoutine;
218 Routine *pixelRoutine;
220 VertexProcessor::RoutinePointer vertexPointer;
221 SetupProcessor::RoutinePointer setupPointer;
222 PixelProcessor::RoutinePointer pixelPointer;
224 int (*setupPrimitives)(Renderer *renderer, int batch, int count);
225 SetupProcessor::State setupState;
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];
245 std::list<Query*> *queries;
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
266 class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor
280 volatile int primitiveUnit;
281 volatile int pixelCluster;
284 struct PrimitiveProgress
295 volatile int drawCall;
296 volatile int firstPrimitive;
297 volatile int primitiveCount;
298 volatile int visible;
299 volatile int references;
307 processedPrimitives = 0;
311 volatile int drawCall;
312 volatile int processedPrimitives;
313 volatile bool executing;
317 Renderer(Context *context, Conventions conventions, bool exactColorRounding);
321 virtual void clear(void* pixel, Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
322 virtual void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
323 virtual void blit3D(Surface *source, Surface *dest);
324 virtual void draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true);
326 virtual void setIndexBuffer(Resource *indexBuffer);
328 virtual void setMultiSampleMask(unsigned int mask);
329 virtual void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
331 virtual void setTextureResource(unsigned int sampler, Resource *resource);
332 virtual void setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type);
334 virtual void setTextureFilter(SamplerType type, int sampler, FilterType textureFilter);
335 virtual void setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter);
336 virtual void setGatherEnable(SamplerType type, int sampler, bool enable);
337 virtual void setAddressingModeU(SamplerType type, int sampler, AddressingMode addressingMode);
338 virtual void setAddressingModeV(SamplerType type, int sampler, AddressingMode addressingMode);
339 virtual void setAddressingModeW(SamplerType type, int sampler, AddressingMode addressingMode);
340 virtual void setReadSRGB(SamplerType type, int sampler, bool sRGB);
341 virtual void setMipmapLOD(SamplerType type, int sampler, float bias);
342 virtual void setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor);
343 virtual void setMaxAnisotropy(SamplerType type, int sampler, float maxAnisotropy);
344 virtual void setSwizzleR(SamplerType type, int sampler, SwizzleType swizzleR);
345 virtual void setSwizzleG(SamplerType type, int sampler, SwizzleType swizzleG);
346 virtual void setSwizzleB(SamplerType type, int sampler, SwizzleType swizzleB);
347 virtual void setSwizzleA(SamplerType type, int sampler, SwizzleType swizzleA);
349 virtual void setPointSpriteEnable(bool pointSpriteEnable);
350 virtual void setPointScaleEnable(bool pointScaleEnable);
351 virtual void setLineWidth(float width);
353 virtual void setDepthBias(float bias);
354 virtual void setSlopeDepthBias(float slopeBias);
356 virtual void setRasterizerDiscard(bool rasterizerDiscard);
358 // Programmable pipelines
359 virtual void setPixelShader(const PixelShader *shader);
360 virtual void setVertexShader(const VertexShader *shader);
362 virtual void setPixelShaderConstantF(int index, const float value[4], int count = 1);
363 virtual void setPixelShaderConstantI(int index, const int value[4], int count = 1);
364 virtual void setPixelShaderConstantB(int index, const int *boolean, int count = 1);
366 virtual void setVertexShaderConstantF(int index, const float value[4], int count = 1);
367 virtual void setVertexShaderConstantI(int index, const int value[4], int count = 1);
368 virtual void setVertexShaderConstantB(int index, const int *boolean, int count = 1);
370 // Viewport & Clipper
371 virtual void setViewport(const Viewport &viewport);
372 virtual void setScissor(const Rect &scissor);
373 virtual void setClipFlags(int flags);
374 virtual void setClipPlane(unsigned int index, const float plane[4]);
377 virtual void setModelMatrix(const Matrix &M, int i = 0);
378 virtual void setViewMatrix(const Matrix &V);
379 virtual void setBaseMatrix(const Matrix &B);
380 virtual void setProjectionMatrix(const Matrix &P);
382 virtual void addQuery(Query *query);
383 virtual void removeQuery(Query *query);
388 // Performance timers
389 int getThreadCount();
390 int64_t getVertexTime(int thread);
391 int64_t getSetupTime(int thread);
392 int64_t getPixelTime(int thread);
397 static void threadFunction(void *parameters);
398 void threadLoop(int threadIndex);
399 void taskLoop(int threadIndex);
400 void findAvailableTasks();
401 void scheduleTask(int threadIndex);
402 void executeTask(int threadIndex);
403 void finishRendering(Task &pixelTask);
405 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
407 static int setupSolidTriangles(Renderer *renderer, int batch, int count);
408 static int setupWireframeTriangle(Renderer *renderer, int batch, int count);
409 static int setupVertexTriangle(Renderer *renderer, int batch, int count);
410 static int setupLines(Renderer *renderer, int batch, int count);
411 static int setupPoints(Renderer *renderer, int batch, int count);
413 static bool setupLine(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
414 static bool setupPoint(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw);
416 bool isReadWriteTexture(int sampler);
417 void updateClipper();
418 void updateConfiguration(bool initialUpdate = false);
419 static unsigned int computeClipFlags(const float4 &v, const DrawData &data);
420 void initializeThreads();
421 void terminateThreads();
423 void loadConstants(const VertexShader *vertexShader);
424 void loadConstants(const PixelShader *pixelShader);
432 Triangle *triangleBatch[16];
433 Primitive *primitiveBatch[16];
435 // User-defined clipping planes
436 Plane userPlane[MAX_CLIP_PLANES];
437 Plane clipPlane[MAX_CLIP_PLANES]; // Tranformed to clip space
438 bool updateClipPlanes;
440 volatile bool exitThreads;
441 volatile int threadsAwake;
443 Event *resume[16]; // Events for resuming threads
444 Event *suspend[16]; // Events for suspending threads
445 Event *resumeApp; // Event for resuming the application thread
447 PrimitiveProgress primitiveProgress[16];
448 PixelProgress pixelProgress[16];
449 Task task[16]; // Current tasks for threads
451 enum {DRAW_COUNT = 16}; // Number of draw calls buffered
452 DrawCall *drawCall[DRAW_COUNT];
453 DrawCall *drawList[DRAW_COUNT];
455 volatile int currentDraw;
456 volatile int nextDraw;
462 BackoffLock schedulerMutex;
465 int64_t vertexTime[16];
466 int64_t setupTime[16];
467 int64_t pixelTime[16];
470 VertexTask *vertexTask[16];
472 SwiftConfig *swiftConfig;
474 std::list<Query*> queries;
477 VertexProcessor::State vertexState;
478 SetupProcessor::State setupState;
479 PixelProcessor::State pixelState;
483 #endif // sw_Renderer_hpp