1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved. 2 // 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 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 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. 14 15 #ifndef sw_Renderer_hpp 16 #define sw_Renderer_hpp 17 18 #include "VertexProcessor.hpp" 19 #include "PixelProcessor.hpp" 20 #include "SetupProcessor.hpp" 21 #include "Plane.hpp" 22 #include "Blitter.hpp" 23 #include "Common/MutexLock.hpp" 24 #include "Common/Thread.hpp" 25 #include "Main/Config.hpp" 26 27 #include <list> 28 29 namespace sw 30 { 31 class Clipper; 32 struct DrawCall; 33 class PixelShader; 34 class VertexShader; 35 class SwiftConfig; 36 struct Task; 37 class Resource; 38 struct Constants; 39 40 enum TranscendentalPrecision 41 { 42 APPROXIMATE, 43 PARTIAL, // 2^-10 44 ACCURATE, 45 WHQL, // 2^-21 46 IEEE // 2^-23 47 }; 48 49 extern TranscendentalPrecision logPrecision; 50 extern TranscendentalPrecision expPrecision; 51 extern TranscendentalPrecision rcpPrecision; 52 extern TranscendentalPrecision rsqPrecision; 53 extern bool perspectiveCorrection; 54 55 struct Conventions 56 { 57 bool halfIntegerCoordinates; 58 bool symmetricNormalizedDepth; 59 bool booleanFaceRegister; 60 bool fullPixelPositionRegister; 61 bool leadingVertexFirst; 62 bool secondaryColor; 63 bool colorsDefaultToZero; 64 }; 65 66 static const Conventions OpenGL = 67 { 68 true, // halfIntegerCoordinates 69 true, // symmetricNormalizedDepth 70 true, // booleanFaceRegister 71 true, // fullPixelPositionRegister 72 false, // leadingVertexFirst 73 false, // secondaryColor 74 true, // colorsDefaultToZero 75 }; 76 77 static const Conventions Direct3D = 78 { 79 false, // halfIntegerCoordinates 80 false, // symmetricNormalizedDepth 81 false, // booleanFaceRegister 82 false, // fullPixelPositionRegister 83 true, // leadingVertexFirst 84 true, // secondardyColor 85 false, // colorsDefaultToZero 86 }; 87 88 struct Query 89 { 90 enum Type { FRAGMENTS_PASSED, TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN }; 91 92 Query(Type type); 93 94 void addRef(); 95 void release(); 96 97 inline void begin() 98 { 99 building = true; 100 data = 0; 101 } 102 103 inline void end() 104 { 105 building = false; 106 } 107 108 inline bool isReady() const 109 { 110 return (reference == 1); 111 } 112 113 bool building; 114 AtomicInt data; 115 116 const Type type; 117 private: 118 ~Query() {} // Only delete a query within the release() function 119 120 AtomicInt reference; 121 }; 122 123 struct DrawData 124 { 125 const Constants *constants; 126 127 const void *input[MAX_VERTEX_INPUTS]; 128 unsigned int stride[MAX_VERTEX_INPUTS]; 129 Texture mipmap[TOTAL_IMAGE_UNITS]; 130 const void *indices; 131 132 struct VS 133 { 134 float4 c[VERTEX_UNIFORM_VECTORS + 1]; // One extra for indices out of range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0} 135 byte* u[MAX_UNIFORM_BUFFER_BINDINGS]; 136 byte* t[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; 137 unsigned int reg[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Offset used when reading from registers, in components 138 unsigned int row[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of rows to read 139 unsigned int col[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of columns to read 140 unsigned int str[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; // Number of components between each varying in output buffer 141 int4 i[16]; 142 bool b[16]; 143 }; 144 145 struct PS 146 { 147 word4 cW[8][4]; 148 float4 c[FRAGMENT_UNIFORM_VECTORS]; 149 byte* u[MAX_UNIFORM_BUFFER_BINDINGS]; 150 int4 i[16]; 151 bool b[16]; 152 }; 153 154 union 155 { 156 VS vs; 157 VertexProcessor::FixedFunction ff; 158 }; 159 160 PS ps; 161 162 int instanceID; 163 164 VertexProcessor::PointSprite point; 165 float lineWidth; 166 167 PixelProcessor::Stencil stencil[2]; // clockwise, counterclockwise 168 PixelProcessor::Stencil stencilCCW; 169 PixelProcessor::Fog fog; 170 PixelProcessor::Factor factor; 171 unsigned int occlusion[16]; // Number of pixels passing depth test 172 173 #if PERF_PROFILE 174 int64_t cycles[PERF_TIMERS][16]; 175 #endif 176 177 TextureStage::Uniforms textureStage[8]; 178 179 float4 Wx16; 180 float4 Hx16; 181 float4 X0x16; 182 float4 Y0x16; 183 float4 XXXX; 184 float4 YYYY; 185 float4 halfPixelX; 186 float4 halfPixelY; 187 float viewportHeight; 188 float slopeDepthBias; 189 float depthRange; 190 float depthNear; 191 Plane clipPlane[6]; 192 193 unsigned int *colorBuffer[RENDERTARGETS]; 194 int colorPitchB[RENDERTARGETS]; 195 int colorSliceB[RENDERTARGETS]; 196 float *depthBuffer; 197 int depthPitchB; 198 int depthSliceB; 199 unsigned char *stencilBuffer; 200 int stencilPitchB; 201 int stencilSliceB; 202 203 int scissorX0; 204 int scissorX1; 205 int scissorY0; 206 int scissorY1; 207 208 float4 a2c0; 209 float4 a2c1; 210 float4 a2c2; 211 float4 a2c3; 212 }; 213 214 struct Viewport 215 { 216 float x0; 217 float y0; 218 float width; 219 float height; 220 float minZ; 221 float maxZ; 222 }; 223 224 class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor 225 { 226 struct Task 227 { 228 enum Type 229 { 230 PRIMITIVES, 231 PIXELS, 232 233 RESUME, 234 SUSPEND 235 }; 236 237 AtomicInt type; 238 AtomicInt primitiveUnit; 239 AtomicInt pixelCluster; 240 }; 241 242 struct PrimitiveProgress 243 { 244 void init() 245 { 246 drawCall = 0; 247 firstPrimitive = 0; 248 primitiveCount = 0; 249 visible = 0; 250 references = 0; 251 } 252 253 AtomicInt drawCall; 254 AtomicInt firstPrimitive; 255 AtomicInt primitiveCount; 256 AtomicInt visible; 257 AtomicInt references; 258 }; 259 260 struct PixelProgress 261 { 262 void init() 263 { 264 drawCall = 0; 265 processedPrimitives = 0; 266 executing = false; 267 } 268 269 AtomicInt drawCall; 270 AtomicInt processedPrimitives; 271 AtomicInt executing; 272 }; 273 274 public: 275 Renderer(Context *context, Conventions conventions, bool exactColorRounding); 276 277 virtual ~Renderer(); 278 279 void *operator new(size_t size); 280 void operator delete(void * mem); 281 282 void draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true); 283 284 void clear(void *value, Format format, Surface *dest, const Rect &rect, unsigned int rgbaMask); 285 void blit(Surface *source, const SliceRectF &sRect, Surface *dest, const SliceRect &dRect, bool filter, bool isStencil = false, bool sRGBconversion = true); 286 void blit3D(Surface *source, Surface *dest); 287 288 void setIndexBuffer(Resource *indexBuffer); 289 290 void setMultiSampleMask(unsigned int mask); 291 void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing); 292 293 void setTextureResource(unsigned int sampler, Resource *resource); 294 void setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type); 295 296 void setTextureFilter(SamplerType type, int sampler, FilterType textureFilter); 297 void setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter); 298 void setGatherEnable(SamplerType type, int sampler, bool enable); 299 void setAddressingModeU(SamplerType type, int sampler, AddressingMode addressingMode); 300 void setAddressingModeV(SamplerType type, int sampler, AddressingMode addressingMode); 301 void setAddressingModeW(SamplerType type, int sampler, AddressingMode addressingMode); 302 void setReadSRGB(SamplerType type, int sampler, bool sRGB); 303 void setMipmapLOD(SamplerType type, int sampler, float bias); 304 void setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor); 305 void setMaxAnisotropy(SamplerType type, int sampler, float maxAnisotropy); 306 void setHighPrecisionFiltering(SamplerType type, int sampler, bool highPrecisionFiltering); 307 void setSwizzleR(SamplerType type, int sampler, SwizzleType swizzleR); 308 void setSwizzleG(SamplerType type, int sampler, SwizzleType swizzleG); 309 void setSwizzleB(SamplerType type, int sampler, SwizzleType swizzleB); 310 void setSwizzleA(SamplerType type, int sampler, SwizzleType swizzleA); 311 void setCompareFunc(SamplerType type, int sampler, CompareFunc compare); 312 void setBaseLevel(SamplerType type, int sampler, int baseLevel); 313 void setMaxLevel(SamplerType type, int sampler, int maxLevel); 314 void setMinLod(SamplerType type, int sampler, float minLod); 315 void setMaxLod(SamplerType type, int sampler, float maxLod); 316 void setSyncRequired(SamplerType type, int sampler, bool syncRequired); 317 318 void setPointSpriteEnable(bool pointSpriteEnable); 319 void setPointScaleEnable(bool pointScaleEnable); 320 void setLineWidth(float width); 321 322 void setDepthBias(float bias); 323 void setSlopeDepthBias(float slopeBias); 324 325 void setRasterizerDiscard(bool rasterizerDiscard); 326 327 // Programmable pipelines 328 void setPixelShader(const PixelShader *shader); 329 void setVertexShader(const VertexShader *shader); 330 331 void setPixelShaderConstantF(unsigned int index, const float value[4], unsigned int count = 1); 332 void setPixelShaderConstantI(unsigned int index, const int value[4], unsigned int count = 1); 333 void setPixelShaderConstantB(unsigned int index, const int *boolean, unsigned int count = 1); 334 335 void setVertexShaderConstantF(unsigned int index, const float value[4], unsigned int count = 1); 336 void setVertexShaderConstantI(unsigned int index, const int value[4], unsigned int count = 1); 337 void setVertexShaderConstantB(unsigned int index, const int *boolean, unsigned int count = 1); 338 339 // Viewport & Clipper 340 void setViewport(const Viewport &viewport); 341 void setScissor(const Rect &scissor); 342 void setClipFlags(int flags); 343 void setClipPlane(unsigned int index, const float plane[4]); 344 345 // Partial transform 346 void setModelMatrix(const Matrix &M, int i = 0); 347 void setViewMatrix(const Matrix &V); 348 void setBaseMatrix(const Matrix &B); 349 void setProjectionMatrix(const Matrix &P); 350 351 void addQuery(Query *query); 352 void removeQuery(Query *query); 353 354 void synchronize(); 355 356 #if PERF_HUD 357 // Performance timers 358 int getThreadCount(); 359 int64_t getVertexTime(int thread); 360 int64_t getSetupTime(int thread); 361 int64_t getPixelTime(int thread); 362 void resetTimers(); 363 #endif 364 365 static int getClusterCount() { return clusterCount; } 366 367 private: 368 static void threadFunction(void *parameters); 369 void threadLoop(int threadIndex); 370 void taskLoop(int threadIndex); 371 void findAvailableTasks(); 372 void scheduleTask(int threadIndex); 373 void executeTask(int threadIndex); 374 void finishRendering(Task &pixelTask); 375 376 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread); 377 378 int setupSolidTriangles(int batch, int count); 379 int setupWireframeTriangle(int batch, int count); 380 int setupVertexTriangle(int batch, int count); 381 int setupLines(int batch, int count); 382 int setupPoints(int batch, int count); 383 384 bool setupLine(Primitive &primitive, Triangle &triangle, const DrawCall &draw); 385 bool setupPoint(Primitive &primitive, Triangle &triangle, const DrawCall &draw); 386 387 bool isReadWriteTexture(int sampler); 388 void updateClipper(); 389 void updateConfiguration(bool initialUpdate = false); 390 void initializeThreads(); 391 void terminateThreads(); 392 393 void loadConstants(const VertexShader *vertexShader); 394 void loadConstants(const PixelShader *pixelShader); 395 396 Context *context; 397 Clipper *clipper; 398 Blitter *blitter; 399 Viewport viewport; 400 Rect scissor; 401 int clipFlags; 402 403 Triangle *triangleBatch[16]; 404 Primitive *primitiveBatch[16]; 405 406 // User-defined clipping planes 407 Plane userPlane[MAX_CLIP_PLANES]; 408 Plane clipPlane[MAX_CLIP_PLANES]; // Tranformed to clip space 409 bool updateClipPlanes; 410 411 AtomicInt exitThreads; 412 AtomicInt threadsAwake; 413 Thread *worker[16]; 414 Event *resume[16]; // Events for resuming threads 415 Event *suspend[16]; // Events for suspending threads 416 Event *resumeApp; // Event for resuming the application thread 417 418 PrimitiveProgress primitiveProgress[16]; 419 PixelProgress pixelProgress[16]; 420 Task task[16]; // Current tasks for threads 421 422 enum { 423 DRAW_COUNT = 16, // Number of draw calls buffered (must be power of 2) 424 DRAW_COUNT_BITS = DRAW_COUNT - 1, 425 }; 426 DrawCall *drawCall[DRAW_COUNT]; 427 DrawCall *drawList[DRAW_COUNT]; 428 429 AtomicInt currentDraw; 430 AtomicInt nextDraw; 431 432 enum { 433 TASK_COUNT = 32, // Size of the task queue (must be power of 2) 434 TASK_COUNT_BITS = TASK_COUNT - 1, 435 }; 436 Task taskQueue[TASK_COUNT]; 437 AtomicInt qHead; 438 AtomicInt qSize; 439 440 static AtomicInt unitCount; 441 static AtomicInt clusterCount; 442 443 MutexLock schedulerMutex; 444 445 #if PERF_HUD 446 int64_t vertexTime[16]; 447 int64_t setupTime[16]; 448 int64_t pixelTime[16]; 449 #endif 450 451 VertexTask *vertexTask[16]; 452 453 SwiftConfig *swiftConfig; 454 455 std::list<Query*> queries; 456 Resource *sync; 457 458 VertexProcessor::State vertexState; 459 SetupProcessor::State setupState; 460 PixelProcessor::State pixelState; 461 462 Routine *vertexRoutine; 463 Routine *setupRoutine; 464 Routine *pixelRoutine; 465 }; 466 467 struct DrawCall 468 { 469 DrawCall(); 470 471 ~DrawCall(); 472 473 AtomicInt drawType; 474 AtomicInt batchSize; 475 476 Routine *vertexRoutine; 477 Routine *setupRoutine; 478 Routine *pixelRoutine; 479 480 VertexProcessor::RoutinePointer vertexPointer; 481 SetupProcessor::RoutinePointer setupPointer; 482 PixelProcessor::RoutinePointer pixelPointer; 483 484 int (Renderer::*setupPrimitives)(int batch, int count); 485 SetupProcessor::State setupState; 486 487 Resource *vertexStream[MAX_VERTEX_INPUTS]; 488 Resource *indexBuffer; 489 Surface *renderTarget[RENDERTARGETS]; 490 Surface *depthBuffer; 491 Surface *stencilBuffer; 492 Resource *texture[TOTAL_IMAGE_UNITS]; 493 Resource* pUniformBuffers[MAX_UNIFORM_BUFFER_BINDINGS]; 494 Resource* vUniformBuffers[MAX_UNIFORM_BUFFER_BINDINGS]; 495 Resource* transformFeedbackBuffers[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; 496 497 unsigned int vsDirtyConstF; 498 unsigned int vsDirtyConstI; 499 unsigned int vsDirtyConstB; 500 501 unsigned int psDirtyConstF; 502 unsigned int psDirtyConstI; 503 unsigned int psDirtyConstB; 504 505 std::list<Query*> *queries; 506 507 AtomicInt clipFlags; 508 509 AtomicInt primitive; // Current primitive to enter pipeline 510 AtomicInt count; // Number of primitives to render 511 AtomicInt references; // Remaining references to this draw call, 0 when done drawing, -1 when resources unlocked and slot is free 512 513 DrawData *data; 514 }; 515 } 516 517 #endif // sw_Renderer_hpp 518