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