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 }; 69 70 static const Conventions OpenGL = 71 { 72 true, // halfIntegerCoordinates 73 true, // symmetricNormalizedDepth 74 true, // booleanFaceRegister 75 true, // fullPixelPositionRegister 76 false, // leadingVertexFirst 77 false // secondaryColor 78 }; 79 80 static const Conventions Direct3D = 81 { 82 false, // halfIntegerCoordinates 83 false, // symmetricNormalizedDepth 84 false, // booleanFaceRegister 85 false, // fullPixelPositionRegister 86 true, // leadingVertexFirst 87 true, // secondardyColor 88 }; 89 90 struct Query 91 { 92 enum Type { FRAGMENTS_PASSED, TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN }; 93 Querysw::Query94 Query(Type type) : building(false), reference(0), data(0), type(type) 95 { 96 } 97 beginsw::Query98 void begin() 99 { 100 building = true; 101 data = 0; 102 } 103 endsw::Query104 void end() 105 { 106 building = false; 107 } 108 109 bool building; 110 volatile int reference; 111 volatile unsigned int data; 112 113 const Type type; 114 }; 115 116 struct DrawData 117 { 118 const Constants *constants; 119 120 const void *input[MAX_VERTEX_INPUTS]; 121 unsigned int stride[MAX_VERTEX_INPUTS]; 122 Texture mipmap[TOTAL_IMAGE_UNITS]; 123 const void *indices; 124 125 struct VS 126 { 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 134 int4 i[16]; 135 bool b[16]; 136 }; 137 138 struct PS 139 { 140 word4 cW[8][4]; 141 float4 c[FRAGMENT_UNIFORM_VECTORS]; 142 byte* u[MAX_UNIFORM_BUFFER_BINDINGS]; 143 int4 i[16]; 144 bool b[16]; 145 }; 146 147 union 148 { 149 VS vs; 150 VertexProcessor::FixedFunction ff; 151 }; 152 153 PS ps; 154 155 int instanceID; 156 157 VertexProcessor::PointSprite point; 158 float lineWidth; 159 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 165 166 #if PERF_PROFILE 167 int64_t cycles[PERF_TIMERS][16]; 168 #endif 169 170 TextureStage::Uniforms textureStage[8]; 171 172 float4 Wx16; 173 float4 Hx16; 174 float4 X0x16; 175 float4 Y0x16; 176 float4 XXXX; 177 float4 YYYY; 178 float4 halfPixelX; 179 float4 halfPixelY; 180 float viewportHeight; 181 float slopeDepthBias; 182 float depthRange; 183 float depthNear; 184 Plane clipPlane[6]; 185 186 unsigned int *colorBuffer[RENDERTARGETS]; 187 int colorPitchB[RENDERTARGETS]; 188 int colorSliceB[RENDERTARGETS]; 189 float *depthBuffer; 190 int depthPitchB; 191 int depthSliceB; 192 unsigned char *stencilBuffer; 193 int stencilPitchB; 194 int stencilSliceB; 195 196 int scissorX0; 197 int scissorX1; 198 int scissorY0; 199 int scissorY1; 200 201 float4 a2c0; 202 float4 a2c1; 203 float4 a2c2; 204 float4 a2c3; 205 }; 206 207 struct DrawCall 208 { 209 DrawCall(); 210 211 ~DrawCall(); 212 213 DrawType drawType; 214 int batchSize; 215 216 Routine *vertexRoutine; 217 Routine *setupRoutine; 218 Routine *pixelRoutine; 219 220 VertexProcessor::RoutinePointer vertexPointer; 221 SetupProcessor::RoutinePointer setupPointer; 222 PixelProcessor::RoutinePointer pixelPointer; 223 224 int (Renderer::*setupPrimitives)(int batch, int count); 225 SetupProcessor::State setupState; 226 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]; 236 237 int vsDirtyConstF; 238 int vsDirtyConstI; 239 int vsDirtyConstB; 240 241 int psDirtyConstF; 242 int psDirtyConstI; 243 int psDirtyConstB; 244 245 std::list<Query*> *queries; 246 247 int clipFlags; 248 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 252 253 DrawData *data; 254 }; 255 256 struct Viewport 257 { 258 float x0; 259 float y0; 260 float width; 261 float height; 262 float minZ; 263 float maxZ; 264 }; 265 266 class Renderer : public VertexProcessor, public PixelProcessor, public SetupProcessor 267 { 268 struct Task 269 { 270 enum Type 271 { 272 PRIMITIVES, 273 PIXELS, 274 275 RESUME, 276 SUSPEND 277 }; 278 279 volatile Type type; 280 volatile int primitiveUnit; 281 volatile int pixelCluster; 282 }; 283 284 struct PrimitiveProgress 285 { initsw::Renderer::PrimitiveProgress286 void init() 287 { 288 drawCall = 0; 289 firstPrimitive = 0; 290 primitiveCount = 0; 291 visible = 0; 292 references = 0; 293 } 294 295 volatile int drawCall; 296 volatile int firstPrimitive; 297 volatile int primitiveCount; 298 volatile int visible; 299 volatile int references; 300 }; 301 302 struct PixelProgress 303 { initsw::Renderer::PixelProgress304 void init() 305 { 306 drawCall = 0; 307 processedPrimitives = 0; 308 executing = false; 309 } 310 311 volatile int drawCall; 312 volatile int processedPrimitives; 313 volatile bool executing; 314 }; 315 316 public: 317 Renderer(Context *context, Conventions conventions, bool exactColorRounding); 318 319 virtual ~Renderer(); 320 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); 325 326 virtual void setIndexBuffer(Resource *indexBuffer); 327 328 virtual void setMultiSampleMask(unsigned int mask); 329 virtual void setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing); 330 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); 333 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); 348 349 virtual void setPointSpriteEnable(bool pointSpriteEnable); 350 virtual void setPointScaleEnable(bool pointScaleEnable); 351 virtual void setLineWidth(float width); 352 353 virtual void setDepthBias(float bias); 354 virtual void setSlopeDepthBias(float slopeBias); 355 356 virtual void setRasterizerDiscard(bool rasterizerDiscard); 357 358 // Programmable pipelines 359 virtual void setPixelShader(const PixelShader *shader); 360 virtual void setVertexShader(const VertexShader *shader); 361 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); 365 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); 369 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]); 375 376 // Partial transform 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); 381 382 virtual void addQuery(Query *query); 383 virtual void removeQuery(Query *query); 384 385 void synchronize(); 386 387 #if PERF_HUD 388 // Performance timers 389 int getThreadCount(); 390 int64_t getVertexTime(int thread); 391 int64_t getSetupTime(int thread); 392 int64_t getPixelTime(int thread); 393 void resetTimers(); 394 #endif 395 396 private: 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); 404 405 void processPrimitiveVertices(int unit, unsigned int start, unsigned int count, unsigned int loop, int thread); 406 407 int setupSolidTriangles(int batch, int count); 408 int setupWireframeTriangle(int batch, int count); 409 int setupVertexTriangle(int batch, int count); 410 int setupLines(int batch, int count); 411 int setupPoints(int batch, int count); 412 413 bool setupLine(Primitive &primitive, Triangle &triangle, const DrawCall &draw); 414 bool setupPoint(Primitive &primitive, Triangle &triangle, const DrawCall &draw); 415 416 bool isReadWriteTexture(int sampler); 417 void updateClipper(); 418 void updateConfiguration(bool initialUpdate = false); 419 void initializeThreads(); 420 void terminateThreads(); 421 422 void loadConstants(const VertexShader *vertexShader); 423 void loadConstants(const PixelShader *pixelShader); 424 425 Context *context; 426 Clipper *clipper; 427 Viewport viewport; 428 Rect scissor; 429 int clipFlags; 430 431 Triangle *triangleBatch[16]; 432 Primitive *primitiveBatch[16]; 433 434 // User-defined clipping planes 435 Plane userPlane[MAX_CLIP_PLANES]; 436 Plane clipPlane[MAX_CLIP_PLANES]; // Tranformed to clip space 437 bool updateClipPlanes; 438 439 volatile bool exitThreads; 440 volatile int threadsAwake; 441 Thread *worker[16]; 442 Event *resume[16]; // Events for resuming threads 443 Event *suspend[16]; // Events for suspending threads 444 Event *resumeApp; // Event for resuming the application thread 445 446 PrimitiveProgress primitiveProgress[16]; 447 PixelProgress pixelProgress[16]; 448 Task task[16]; // Current tasks for threads 449 450 enum {DRAW_COUNT = 16}; // Number of draw calls buffered 451 DrawCall *drawCall[DRAW_COUNT]; 452 DrawCall *drawList[DRAW_COUNT]; 453 454 volatile int currentDraw; 455 volatile int nextDraw; 456 457 Task taskQueue[32]; 458 unsigned int qHead; 459 unsigned int qSize; 460 461 BackoffLock schedulerMutex; 462 463 #if PERF_HUD 464 int64_t vertexTime[16]; 465 int64_t setupTime[16]; 466 int64_t pixelTime[16]; 467 #endif 468 469 VertexTask *vertexTask[16]; 470 471 SwiftConfig *swiftConfig; 472 473 std::list<Query*> queries; 474 Resource *sync; 475 476 VertexProcessor::State vertexState; 477 SetupProcessor::State setupState; 478 PixelProcessor::State pixelState; 479 }; 480 } 481 482 #endif // sw_Renderer_hpp 483