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