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 (Renderer::*setupPrimitives)(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 void *operator new(size_t size);
322 void operator delete(void * mem);
324 void clear(void* pixel, Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
325 void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
326 void blit3D(Surface *source, Surface *dest);
327 void draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true);
329 void setIndexBuffer(Resource *indexBuffer);
331 void setMultiSampleMask(unsigned int mask);
332 void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing);
334 void setTextureResource(unsigned int sampler, Resource *resource);
335 void setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type);
337 void setTextureFilter(SamplerType type, int sampler, FilterType textureFilter);
338 void setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter);
339 void setGatherEnable(SamplerType type, int sampler, bool enable);
340 void setAddressingModeU(SamplerType type, int sampler, AddressingMode addressingMode);
341 void setAddressingModeV(SamplerType type, int sampler, AddressingMode addressingMode);
342 void setAddressingModeW(SamplerType type, int sampler, AddressingMode addressingMode);
343 void setReadSRGB(SamplerType type, int sampler, bool sRGB);
344 void setMipmapLOD(SamplerType type, int sampler, float bias);
345 void setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor);
346 void setMaxAnisotropy(SamplerType type, int sampler, float maxAnisotropy);
347 void setSwizzleR(SamplerType type, int sampler, SwizzleType swizzleR);
348 void setSwizzleG(SamplerType type, int sampler, SwizzleType swizzleG);
349 void setSwizzleB(SamplerType type, int sampler, SwizzleType swizzleB);
350 void setSwizzleA(SamplerType type, int sampler, SwizzleType swizzleA);
351 void setBaseLevel(SamplerType type, int sampler, int baseLevel);
352 void setMaxLevel(SamplerType type, int sampler, int maxLevel);
353 void setMinLod(SamplerType type, int sampler, float minLod);
354 void setMaxLod(SamplerType type, int sampler, float maxLod);
356 void setPointSpriteEnable(bool pointSpriteEnable);
357 void setPointScaleEnable(bool pointScaleEnable);
358 void setLineWidth(float width);
360 void setDepthBias(float bias);
361 void setSlopeDepthBias(float slopeBias);
363 void setRasterizerDiscard(bool rasterizerDiscard);
365 // Programmable pipelines
366 void setPixelShader(const PixelShader *shader);
367 void setVertexShader(const VertexShader *shader);
369 void setPixelShaderConstantF(int index, const float value[4], int count = 1);
370 void setPixelShaderConstantI(int index, const int value[4], int count = 1);
371 void setPixelShaderConstantB(int index, const int *boolean, int count = 1);
373 void setVertexShaderConstantF(int index, const float value[4], int count = 1);
374 void setVertexShaderConstantI(int index, const int value[4], int count = 1);
375 void setVertexShaderConstantB(int index, const int *boolean, int count = 1);
377 // Viewport & Clipper
378 void setViewport(const Viewport &viewport);
379 void setScissor(const Rect &scissor);
380 void setClipFlags(int flags);
381 void setClipPlane(unsigned int index, const float plane[4]);
384 void setModelMatrix(const Matrix &M, int i = 0);
385 void setViewMatrix(const Matrix &V);
386 void setBaseMatrix(const Matrix &B);
387 void setProjectionMatrix(const Matrix &P);
389 void addQuery(Query *query);
390 void removeQuery(Query *query);
395 // Performance timers
396 int getThreadCount();
397 int64_t getVertexTime(int thread);
398 int64_t getSetupTime(int thread);
399 int64_t getPixelTime(int thread);
404 static void threadFunction(void *parameters);
405 void threadLoop(int threadIndex);
406 void taskLoop(int threadIndex);
407 void findAvailableTasks();
408 void scheduleTask(int threadIndex);
409 void executeTask(int threadIndex);
410 void finishRendering(Task &pixelTask);
412 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread);
414 int setupSolidTriangles(int batch, int count);
415 int setupWireframeTriangle(int batch, int count);
416 int setupVertexTriangle(int batch, int count);
417 int setupLines(int batch, int count);
418 int setupPoints(int batch, int count);
420 bool setupLine(Primitive &primitive, Triangle &triangle, const DrawCall &draw);
421 bool setupPoint(Primitive &primitive, Triangle &triangle, const DrawCall &draw);
423 bool isReadWriteTexture(int sampler);
424 void updateClipper();
425 void updateConfiguration(bool initialUpdate = false);
426 void initializeThreads();
427 void terminateThreads();
429 void loadConstants(const VertexShader *vertexShader);
430 void loadConstants(const PixelShader *pixelShader);
438 Triangle *triangleBatch[16];
439 Primitive *primitiveBatch[16];
441 // User-defined clipping planes
442 Plane userPlane[MAX_CLIP_PLANES];
443 Plane clipPlane[MAX_CLIP_PLANES]; // Tranformed to clip space
444 bool updateClipPlanes;
446 volatile bool exitThreads;
447 volatile int threadsAwake;
449 Event *resume[16]; // Events for resuming threads
450 Event *suspend[16]; // Events for suspending threads
451 Event *resumeApp; // Event for resuming the application thread
453 PrimitiveProgress primitiveProgress[16];
454 PixelProgress pixelProgress[16];
455 Task task[16]; // Current tasks for threads
457 enum {DRAW_COUNT = 16}; // Number of draw calls buffered
458 DrawCall *drawCall[DRAW_COUNT];
459 DrawCall *drawList[DRAW_COUNT];
461 volatile int currentDraw;
462 volatile int nextDraw;
468 BackoffLock schedulerMutex;
471 int64_t vertexTime[16];
472 int64_t setupTime[16];
473 int64_t pixelTime[16];
476 VertexTask *vertexTask[16];
478 SwiftConfig *swiftConfig;
480 std::list<Query*> queries;
483 VertexProcessor::State vertexState;
484 SetupProcessor::State setupState;
485 PixelProcessor::State pixelState;
487 Routine *vertexRoutine;
488 Routine *setupRoutine;
489 Routine *pixelRoutine;
493 #endif // sw_Renderer_hpp