1 // SwiftShader Software Renderer
3 // Copyright(c) 2005-2012 TransGaming Inc.
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
6 // transcribed, stored in a retrieval system, translated into any human or computer
7 // language by any means, or disclosed to third parties without the explicit written
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9 // or implied, including but not limited to any patent rights, are granted to you.
12 #include "Renderer.hpp"
14 #include "Clipper.hpp"
16 #include "FrameBuffer.hpp"
18 #include "Surface.hpp"
20 #include "Primitive.hpp"
21 #include "Polygon.hpp"
22 #include "SwiftConfig.hpp"
23 #include "MutexLock.hpp"
26 #include "Resource.hpp"
27 #include "Constants.hpp"
29 #include "Reactor/Reactor.hpp"
35 bool disableServer = true;
38 unsigned int minPrimitives = 1;
39 unsigned int maxPrimitives = 1 << 21;
44 extern bool halfIntegerCoordinates; // Pixel centers are not at integer coordinates
45 extern bool symmetricNormalizedDepth; // [-1, 1] instead of [0, 1]
46 extern bool booleanFaceRegister;
47 extern bool fullPixelPositionRegister;
49 extern bool forceWindowed;
50 extern bool complementaryDepthBuffer;
51 extern bool postBlendSRGB;
52 extern bool exactColorRounding;
53 extern bool leadingVertexFirst;
54 extern TransparencyAntialiasing transparencyAntialiasing;
55 extern bool forceClearRegisters;
57 extern bool precacheVertex;
58 extern bool precacheSetup;
59 extern bool precachePixel;
66 TranscendentalPrecision logPrecision = ACCURATE;
67 TranscendentalPrecision expPrecision = ACCURATE;
68 TranscendentalPrecision rcpPrecision = ACCURATE;
69 TranscendentalPrecision rsqPrecision = ACCURATE;
70 bool perspectiveCorrection = true;
82 vsDirtyConstF = 256 + 1;
92 data = (DrawData*)allocate(sizeof(DrawData));
93 data->constants = &constants;
103 Renderer::Renderer(Context *context, Conventions conventions, bool exactColorRounding) : context(context), VertexProcessor(context), PixelProcessor(context), SetupProcessor(context), viewport()
105 sw::halfIntegerCoordinates = conventions.halfIntegerCoordinates;
106 sw::symmetricNormalizedDepth = conventions.symmetricNormalizedDepth;
107 sw::booleanFaceRegister = conventions.booleanFaceRegister;
108 sw::fullPixelPositionRegister = conventions.fullPixelPositionRegister;
109 sw::leadingVertexFirst = conventions.leadingVertexFirst;
110 sw::exactColorRounding = exactColorRounding;
112 setRenderTarget(0, 0);
113 clipper = new Clipper();
115 updateViewMatrix = true;
116 updateBaseMatrix = true;
117 updateProjectionMatrix = true;
118 updateClipPlanes = true;
124 for(int i = 0; i < 16; i++)
134 resumeApp = new Event();
142 for(int i = 0; i < 16; i++)
144 triangleBatch[i] = 0;
145 primitiveBatch[i] = 0;
148 for(int draw = 0; draw < DRAW_COUNT; draw++)
150 drawCall[draw] = new DrawCall();
151 drawList[draw] = drawCall[draw];
154 for(int unit = 0; unit < 16; unit++)
156 primitiveProgress[unit].init();
159 for(int cluster = 0; cluster < 16; cluster++)
161 pixelProgress[cluster].init();
166 swiftConfig = new SwiftConfig(disableServer);
167 updateConfiguration(true);
169 sync = new Resource(0);
172 Renderer::~Renderer()
182 for(int draw = 0; draw < DRAW_COUNT; draw++)
184 delete drawCall[draw];
190 void Renderer::blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter)
192 blitter.blit(source, sRect, dest, dRect, filter);
195 void Renderer::blit3D(Surface *source, Surface *dest)
197 blitter.blit3D(source, dest);
200 void Renderer::draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update)
203 if(count < minPrimitives || count > maxPrimitives)
209 context->drawType = drawType;
211 updateConfiguration();
214 int ss = context->getSuperSampleCount();
215 int ms = context->getMultiSampleCount();
217 for(int q = 0; q < ss; q++)
219 int oldMultiSampleMask = context->multiSampleMask;
220 context->multiSampleMask = (context->sampleMask >> (ms * q)) & ((unsigned)0xFFFFFFFF >> (32 - ms));
222 if(!context->multiSampleMask)
227 sync->lock(sw::PRIVATE);
229 Routine *vertexRoutine;
230 Routine *setupRoutine;
231 Routine *pixelRoutine;
233 if(update || oldMultiSampleMask != context->multiSampleMask)
235 vertexState = VertexProcessor::update();
236 setupState = SetupProcessor::update();
237 pixelState = PixelProcessor::update();
239 vertexRoutine = VertexProcessor::routine(vertexState);
240 setupRoutine = SetupProcessor::routine(setupState);
241 pixelRoutine = PixelProcessor::routine(pixelState);
244 int batch = batchSize / ms;
246 int (*setupPrimitives)(Renderer *renderer, int batch, int count);
248 if(context->isDrawTriangle())
250 switch(context->fillMode)
253 setupPrimitives = setupSolidTriangles;
256 setupPrimitives = setupWireframeTriangle;
260 setupPrimitives = setupVertexTriangle;
263 default: ASSERT(false);
266 else if(context->isDrawLine())
268 setupPrimitives = setupLines;
272 setupPrimitives = setupPoints;
279 for(int i = 0; i < DRAW_COUNT; i++)
281 if(drawCall[i]->references == -1)
284 drawList[nextDraw % DRAW_COUNT] = draw;
297 DrawData *data = draw->data;
299 if(queries.size() != 0)
301 for(std::list<Query*>::iterator query = queries.begin(); query != queries.end(); query++)
303 atomicIncrement(&(*query)->reference);
306 draw->queries = new std::list<Query*>(queries);
309 draw->drawType = drawType;
310 draw->batchSize = batch;
312 vertexRoutine->bind();
313 setupRoutine->bind();
314 pixelRoutine->bind();
316 draw->vertexRoutine = vertexRoutine;
317 draw->setupRoutine = setupRoutine;
318 draw->pixelRoutine = pixelRoutine;
319 draw->vertexPointer = (VertexProcessor::RoutinePointer)vertexRoutine->getEntry();
320 draw->setupPointer = (SetupProcessor::RoutinePointer)setupRoutine->getEntry();
321 draw->pixelPointer = (PixelProcessor::RoutinePointer)pixelRoutine->getEntry();
322 draw->setupPrimitives = setupPrimitives;
323 draw->setupState = setupState;
325 for(int i = 0; i < 16; i++)
327 draw->vertexStream[i] = context->input[i].resource;
328 data->input[i] = context->input[i].buffer;
329 data->stride[i] = context->input[i].stride;
331 if(draw->vertexStream[i])
333 draw->vertexStream[i]->lock(PUBLIC, PRIVATE);
337 if(context->indexBuffer)
339 data->indices = (unsigned char*)context->indexBuffer->lock(PUBLIC, PRIVATE) + indexOffset;
342 draw->indexBuffer = context->indexBuffer;
344 for(int sampler = 0; sampler < TOTAL_IMAGE_UNITS; sampler++)
346 draw->texture[sampler] = 0;
349 for(int sampler = 0; sampler < TEXTURE_IMAGE_UNITS; sampler++)
351 if(pixelState.sampler[sampler].textureType != TEXTURE_NULL)
353 draw->texture[sampler] = context->texture[sampler];
354 draw->texture[sampler]->lock(PUBLIC, isReadWriteTexture(sampler) ? MANAGED : PRIVATE); // If the texure is both read and written, use the same read/write lock as render targets
356 data->mipmap[sampler] = context->sampler[sampler].getTextureData();
360 if(context->pixelShader)
362 if(draw->psDirtyConstF)
364 memcpy(&data->ps.cW, PixelProcessor::cW, sizeof(word4) * 4 * (draw->psDirtyConstF < 8 ? draw->psDirtyConstF : 8));
365 memcpy(&data->ps.c, PixelProcessor::c, sizeof(float4) * draw->psDirtyConstF);
366 draw->psDirtyConstF = 0;
369 if(draw->psDirtyConstI)
371 memcpy(&data->ps.i, PixelProcessor::i, sizeof(int4) * draw->psDirtyConstI);
372 draw->psDirtyConstI = 0;
375 if(draw->psDirtyConstB)
377 memcpy(&data->ps.b, PixelProcessor::b, sizeof(bool) * draw->psDirtyConstB);
378 draw->psDirtyConstB = 0;
382 if(context->pixelShaderVersion() <= 0x0104)
384 for(int stage = 0; stage < 8; stage++)
386 if(pixelState.textureStage[stage].stageOperation != TextureStage::STAGE_DISABLE || context->pixelShader)
388 data->textureStage[stage] = context->textureStage[stage].uniforms;
394 if(context->vertexShader)
396 if(context->vertexShader->getVersion() >= 0x0300)
398 for(int sampler = 0; sampler < VERTEX_TEXTURE_IMAGE_UNITS; sampler++)
400 if(vertexState.samplerState[sampler].textureType != TEXTURE_NULL)
402 draw->texture[TEXTURE_IMAGE_UNITS + sampler] = context->texture[TEXTURE_IMAGE_UNITS + sampler];
403 draw->texture[TEXTURE_IMAGE_UNITS + sampler]->lock(PUBLIC, PRIVATE);
405 data->mipmap[TEXTURE_IMAGE_UNITS + sampler] = context->sampler[TEXTURE_IMAGE_UNITS + sampler].getTextureData();
410 if(draw->vsDirtyConstF)
412 memcpy(&data->vs.c, VertexProcessor::c, sizeof(float4) * draw->vsDirtyConstF);
413 draw->vsDirtyConstF = 0;
416 if(draw->vsDirtyConstI)
418 memcpy(&data->vs.i, VertexProcessor::i, sizeof(int4) * draw->vsDirtyConstI);
419 draw->vsDirtyConstI = 0;
422 if(draw->vsDirtyConstB)
424 memcpy(&data->vs.b, VertexProcessor::b, sizeof(bool) * draw->vsDirtyConstB);
425 draw->vsDirtyConstB = 0;
428 if(context->vertexShader->instanceIdDeclared)
430 data->instanceID = context->instanceID;
437 draw->vsDirtyConstF = 256 + 1;
438 draw->vsDirtyConstI = 16;
439 draw->vsDirtyConstB = 16;
442 if(pixelState.stencilActive)
444 data->stencil[0] = stencil;
445 data->stencil[1] = stencilCCW;
448 if(pixelState.fogActive)
453 if(setupState.isDrawPoint)
458 data->lineWidth = context->lineWidth;
460 data->factor = factor;
462 if(pixelState.transparencyAntialiasing == TRANSPARENCY_ALPHA_TO_COVERAGE)
464 float ref = (float)context->alphaReference * (1.0f / 255.0f);
465 float margin = sw::min(ref, 1.0f - ref);
469 data->a2c0 = replicate(ref - margin * 0.6f);
470 data->a2c1 = replicate(ref - margin * 0.2f);
471 data->a2c2 = replicate(ref + margin * 0.2f);
472 data->a2c3 = replicate(ref + margin * 0.6f);
476 data->a2c0 = replicate(ref - margin * 0.3f);
477 data->a2c1 = replicate(ref + margin * 0.3f);
482 if(pixelState.occlusionEnabled)
484 for(int cluster = 0; cluster < clusterCount; cluster++)
486 data->occlusion[cluster] = 0;
491 for(int cluster = 0; cluster < clusterCount; cluster++)
493 for(int i = 0; i < PERF_TIMERS; i++)
495 data->cycles[i][cluster] = 0;
502 float W = 0.5f * viewport.width;
503 float H = 0.5f * viewport.height;
504 float X0 = viewport.x0 + W;
505 float Y0 = viewport.y0 + H;
506 float N = viewport.minZ;
507 float F = viewport.maxZ;
510 if(context->isDrawTriangle(false))
515 if(complementaryDepthBuffer)
521 static const float X[5][16] = // Fragment offsets
523 {+0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f}, // 1 sample
524 {-0.2500f, +0.2500f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f}, // 2 samples
525 {-0.3000f, +0.1000f, +0.3000f, -0.1000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f}, // 4 samples
526 {+0.1875f, -0.3125f, +0.3125f, -0.4375f, -0.0625f, +0.4375f, +0.0625f, -0.1875f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f}, // 8 samples
527 {+0.2553f, -0.1155f, +0.1661f, -0.1828f, +0.2293f, -0.4132f, -0.1773f, -0.0577f, +0.3891f, -0.4656f, +0.4103f, +0.4248f, -0.2109f, +0.3966f, -0.2664f, -0.3872f} // 16 samples
530 static const float Y[5][16] = // Fragment offsets
532 {+0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f}, // 1 sample
533 {-0.2500f, +0.2500f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f}, // 2 samples
534 {-0.1000f, -0.3000f, +0.1000f, +0.3000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f}, // 4 samples
535 {-0.4375f, -0.3125f, -0.1875f, -0.0625f, +0.0625f, +0.1875f, +0.3125f, +0.4375f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f, +0.0000f}, // 8 samples
536 {-0.4503f, +0.1883f, +0.3684f, -0.4668f, -0.0690f, -0.1315f, +0.4999f, +0.0728f, +0.1070f, -0.3086f, +0.3725f, -0.1547f, -0.1102f, -0.3588f, +0.1789f, +0.0269f} // 16 samples
539 int s = sw::log2(ss);
541 data->Wx16 = replicate(W * 16);
542 data->Hx16 = replicate(H * 16);
543 data->X0x16 = replicate(X0 * 16);
544 data->Y0x16 = replicate(Y0 * 16);
545 data->XXXX = replicate(X[s][q] / W);
546 data->YYYY = replicate(Y[s][q] / H);
547 data->halfPixelX = replicate(0.5f / W);
548 data->halfPixelY = replicate(0.5f / H);
549 data->viewportHeight = abs(viewport.height);
550 data->slopeDepthBias = slopeDepthBias;
551 data->depthRange = Z;
553 draw->clipFlags = clipFlags;
557 if(clipFlags & Clipper::CLIP_PLANE0) data->clipPlane[0] = clipPlane[0];
558 if(clipFlags & Clipper::CLIP_PLANE1) data->clipPlane[1] = clipPlane[1];
559 if(clipFlags & Clipper::CLIP_PLANE2) data->clipPlane[2] = clipPlane[2];
560 if(clipFlags & Clipper::CLIP_PLANE3) data->clipPlane[3] = clipPlane[3];
561 if(clipFlags & Clipper::CLIP_PLANE4) data->clipPlane[4] = clipPlane[4];
562 if(clipFlags & Clipper::CLIP_PLANE5) data->clipPlane[5] = clipPlane[5];
568 for(int index = 0; index < 4; index++)
570 draw->renderTarget[index] = context->renderTarget[index];
572 if(draw->renderTarget[index])
574 data->colorBuffer[index] = (unsigned int*)context->renderTarget[index]->lockInternal(0, 0, q * ms, LOCK_READWRITE, MANAGED);
575 data->colorPitchB[index] = context->renderTarget[index]->getInternalPitchB();
576 data->colorSliceB[index] = context->renderTarget[index]->getInternalSliceB();
580 draw->depthStencil = context->depthStencil;
582 if(draw->depthStencil)
584 data->depthBuffer = (float*)context->depthStencil->lockInternal(0, 0, q * ms, LOCK_READWRITE, MANAGED);
585 data->depthPitchB = context->depthStencil->getInternalPitchB();
586 data->depthSliceB = context->depthStencil->getInternalSliceB();
588 data->stencilBuffer = (unsigned char*)context->depthStencil->lockStencil(q * ms, MANAGED);
589 data->stencilPitchB = context->depthStencil->getStencilPitchB();
590 data->stencilSliceB = context->depthStencil->getStencilSliceB();
596 data->scissorX0 = scissor.x0;
597 data->scissorX1 = scissor.x1;
598 data->scissorY0 = scissor.y0;
599 data->scissorY1 = scissor.y1;
605 draw->references = (count + batch - 1) / batch;
607 schedulerMutex.lock();
609 schedulerMutex.unlock();
616 task[0].type = Task::RESUME;
623 void Renderer::threadFunction(void *parameters)
625 Renderer *renderer = static_cast<Parameters*>(parameters)->renderer;
626 int threadIndex = static_cast<Parameters*>(parameters)->threadIndex;
628 if(logPrecision < IEEE)
630 CPUID::setFlushToZero(true);
631 CPUID::setDenormalsAreZero(true);
634 renderer->threadLoop(threadIndex);
637 void Renderer::threadLoop(int threadIndex)
641 taskLoop(threadIndex);
643 suspend[threadIndex]->signal();
644 resume[threadIndex]->wait();
648 void Renderer::taskLoop(int threadIndex)
650 while(task[threadIndex].type != Task::SUSPEND)
652 scheduleTask(threadIndex);
653 executeTask(threadIndex);
657 void Renderer::findAvailableTasks()
660 for(int cluster = 0; cluster < clusterCount; cluster++)
662 if(!pixelProgress[cluster].executing)
664 for(int unit = 0; unit < unitCount; unit++)
666 if(primitiveProgress[unit].references > 0) // Contains processed primitives
668 if(pixelProgress[cluster].drawCall == primitiveProgress[unit].drawCall)
670 if(pixelProgress[cluster].processedPrimitives == primitiveProgress[unit].firstPrimitive) // Previous primitives have been rendered
672 Task &task = taskQueue[qHead];
673 task.type = Task::PIXELS;
674 task.primitiveUnit = unit;
675 task.pixelCluster = cluster;
677 pixelProgress[cluster].executing = true;
679 // Commit to the task queue
680 qHead = (qHead + 1) % 32;
691 // Find primitive tasks
692 if(currentDraw == nextDraw)
694 return; // No more primitives to process
697 for(int unit = 0; unit < unitCount; unit++)
699 DrawCall *draw = drawList[currentDraw % DRAW_COUNT];
701 if(draw->primitive >= draw->count)
705 if(currentDraw == nextDraw)
707 return; // No more primitives to process
710 draw = drawList[currentDraw % DRAW_COUNT];
713 if(!primitiveProgress[unit].references) // Task not already being executed and not still in use by a pixel unit
715 int primitive = draw->primitive;
716 int count = draw->count;
717 int batch = draw->batchSize;
719 primitiveProgress[unit].drawCall = currentDraw;
720 primitiveProgress[unit].firstPrimitive = primitive;
721 primitiveProgress[unit].primitiveCount = count - primitive >= batch ? batch : count - primitive;
723 draw->primitive += batch;
725 Task &task = taskQueue[qHead];
726 task.type = Task::PRIMITIVES;
727 task.primitiveUnit = unit;
729 primitiveProgress[unit].references = -1;
731 // Commit to the task queue
732 qHead = (qHead + 1) % 32;
738 void Renderer::scheduleTask(int threadIndex)
740 schedulerMutex.lock();
742 if((int)qSize < threadCount - threadsAwake + 1)
744 findAvailableTasks();
749 task[threadIndex] = taskQueue[(qHead - qSize) % 32];
752 if(threadsAwake != threadCount)
754 int wakeup = qSize - threadsAwake + 1;
756 for(int i = 0; i < threadCount && wakeup > 0; i++)
758 if(task[i].type == Task::SUSPEND)
761 task[i].type = Task::RESUME;
772 task[threadIndex].type = Task::SUSPEND;
777 schedulerMutex.unlock();
780 void Renderer::executeTask(int threadIndex)
783 int64_t startTick = Timer::ticks();
786 switch(task[threadIndex].type)
788 case Task::PRIMITIVES:
790 int unit = task[threadIndex].primitiveUnit;
792 int input = primitiveProgress[unit].firstPrimitive;
793 int count = primitiveProgress[unit].primitiveCount;
794 DrawCall *draw = drawList[primitiveProgress[unit].drawCall % DRAW_COUNT];
795 int (*setupPrimitives)(Renderer *renderer, int batch, int count) = draw->setupPrimitives;
797 processPrimitiveVertices(unit, input, count, draw->count, threadIndex);
800 int64_t time = Timer::ticks();
801 vertexTime[threadIndex] += time - startTick;
805 int visible = setupPrimitives(this, unit, count);
807 primitiveProgress[unit].visible = visible;
808 primitiveProgress[unit].references = clusterCount;
811 setupTime[threadIndex] += Timer::ticks() - startTick;
817 int unit = task[threadIndex].primitiveUnit;
818 int visible = primitiveProgress[unit].visible;
822 int cluster = task[threadIndex].pixelCluster;
823 Primitive *primitive = primitiveBatch[unit];
824 DrawCall *draw = drawList[pixelProgress[cluster].drawCall % DRAW_COUNT];
825 DrawData *data = draw->data;
826 PixelProcessor::RoutinePointer pixelRoutine = draw->pixelPointer;
828 pixelRoutine(primitive, visible, cluster, data);
831 finishRendering(task[threadIndex]);
834 pixelTime[threadIndex] += Timer::ticks() - startTick;
847 void Renderer::synchronize()
849 sync->lock(sw::PUBLIC);
853 void Renderer::finishRendering(Task &pixelTask)
855 int unit = pixelTask.primitiveUnit;
856 int cluster = pixelTask.pixelCluster;
858 DrawCall &draw = *drawList[primitiveProgress[unit].drawCall % DRAW_COUNT];
859 DrawData &data = *draw.data;
860 int primitive = primitiveProgress[unit].firstPrimitive;
861 int count = primitiveProgress[unit].primitiveCount;
863 pixelProgress[cluster].processedPrimitives = primitive + count;
865 if(pixelProgress[cluster].processedPrimitives >= draw.count)
867 pixelProgress[cluster].drawCall++;
868 pixelProgress[cluster].processedPrimitives = 0;
871 int ref = atomicDecrement(&primitiveProgress[unit].references);
875 ref = atomicDecrement(&draw.references);
880 for(int cluster = 0; cluster < clusterCount; cluster++)
882 for(int i = 0; i < PERF_TIMERS; i++)
884 profiler.cycles[i] += data.cycles[i][cluster];
891 for(std::list<Query*>::iterator q = draw.queries->begin(); q != draw.queries->end(); q++)
895 for(int cluster = 0; cluster < clusterCount; cluster++)
897 atomicAdd((volatile int*)&query->data, data.occlusion[cluster]);
900 atomicDecrement(&query->reference);
907 for(int i = 0; i < 4; i++)
909 if(draw.renderTarget[i])
911 draw.renderTarget[i]->unlockInternal();
915 if(draw.depthStencil)
917 draw.depthStencil->unlockInternal();
918 draw.depthStencil->unlockStencil();
921 for(int i = 0; i < TOTAL_IMAGE_UNITS; i++)
925 draw.texture[i]->unlock();
929 for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
931 if(draw.vertexStream[i])
933 draw.vertexStream[i]->unlock();
939 draw.indexBuffer->unlock();
942 draw.vertexRoutine->unbind();
943 draw.setupRoutine->unbind();
944 draw.pixelRoutine->unbind();
948 draw.references = -1;
953 pixelProgress[cluster].executing = false;
956 void Renderer::processPrimitiveVertices(int unit, unsigned int start, unsigned int triangleCount, unsigned int loop, int thread)
958 Triangle *triangle = triangleBatch[unit];
959 DrawCall *draw = drawList[primitiveProgress[unit].drawCall % DRAW_COUNT];
960 DrawData *data = draw->data;
961 VertexTask *task = vertexTask[thread];
963 const void *indices = data->indices;
964 VertexProcessor::RoutinePointer vertexRoutine = draw->vertexPointer;
966 if(task->vertexCache.drawCall != primitiveProgress[unit].drawCall)
968 task->vertexCache.clear();
969 task->vertexCache.drawCall = primitiveProgress[unit].drawCall;
972 unsigned int batch[128][3]; // FIXME: Adjust to dynamic batch size
974 switch(draw->drawType)
978 unsigned int index = start;
980 for(unsigned int i = 0; i < triangleCount; i++)
992 unsigned int index = 2 * start;
994 for(unsigned int i = 0; i < triangleCount; i++)
996 batch[i][0] = index + 0;
997 batch[i][1] = index + 1;
998 batch[i][2] = index + 1;
1004 case DRAW_LINESTRIP:
1006 unsigned int index = start;
1008 for(unsigned int i = 0; i < triangleCount; i++)
1010 batch[i][0] = index + 0;
1011 batch[i][1] = index + 1;
1012 batch[i][2] = index + 1;
1020 unsigned int index = start;
1022 for(unsigned int i = 0; i < triangleCount; i++)
1024 batch[i][0] = (index + 0) % loop;
1025 batch[i][1] = (index + 1) % loop;
1026 batch[i][2] = (index + 1) % loop;
1032 case DRAW_TRIANGLELIST:
1034 unsigned int index = 3 * start;
1036 for(unsigned int i = 0; i < triangleCount; i++)
1038 batch[i][0] = index + 0;
1039 batch[i][1] = index + 1;
1040 batch[i][2] = index + 2;
1046 case DRAW_TRIANGLESTRIP:
1048 unsigned int index = start;
1050 for(unsigned int i = 0; i < triangleCount; i++)
1052 batch[i][0] = index + 0;
1053 batch[i][1] = index + (index & 1) + 1;
1054 batch[i][2] = index + (~index & 1) + 1;
1060 case DRAW_TRIANGLEFAN:
1062 unsigned int index = start;
1064 for(unsigned int i = 0; i < triangleCount; i++)
1066 batch[i][0] = index + 1;
1067 batch[i][1] = index + 2;
1074 case DRAW_INDEXEDPOINTLIST8:
1076 const unsigned char *index = (const unsigned char*)indices + start;
1078 for(unsigned int i = 0; i < triangleCount; i++)
1080 batch[i][0] = *index;
1081 batch[i][1] = *index;
1082 batch[i][2] = *index;
1088 case DRAW_INDEXEDPOINTLIST16:
1090 const unsigned short *index = (const unsigned short*)indices + start;
1092 for(unsigned int i = 0; i < triangleCount; i++)
1094 batch[i][0] = *index;
1095 batch[i][1] = *index;
1096 batch[i][2] = *index;
1102 case DRAW_INDEXEDPOINTLIST32:
1104 const unsigned int *index = (const unsigned int*)indices + start;
1106 for(unsigned int i = 0; i < triangleCount; i++)
1108 batch[i][0] = *index;
1109 batch[i][1] = *index;
1110 batch[i][2] = *index;
1116 case DRAW_INDEXEDLINELIST8:
1118 const unsigned char *index = (const unsigned char*)indices + 2 * start;
1120 for(unsigned int i = 0; i < triangleCount; i++)
1122 batch[i][0] = index[0];
1123 batch[i][1] = index[1];
1124 batch[i][2] = index[1];
1130 case DRAW_INDEXEDLINELIST16:
1132 const unsigned short *index = (const unsigned short*)indices + 2 * start;
1134 for(unsigned int i = 0; i < triangleCount; i++)
1136 batch[i][0] = index[0];
1137 batch[i][1] = index[1];
1138 batch[i][2] = index[1];
1144 case DRAW_INDEXEDLINELIST32:
1146 const unsigned int *index = (const unsigned int*)indices + 2 * start;
1148 for(unsigned int i = 0; i < triangleCount; i++)
1150 batch[i][0] = index[0];
1151 batch[i][1] = index[1];
1152 batch[i][2] = index[1];
1158 case DRAW_INDEXEDLINESTRIP8:
1160 const unsigned char *index = (const unsigned char*)indices + start;
1162 for(unsigned int i = 0; i < triangleCount; i++)
1164 batch[i][0] = index[0];
1165 batch[i][1] = index[1];
1166 batch[i][2] = index[1];
1172 case DRAW_INDEXEDLINESTRIP16:
1174 const unsigned short *index = (const unsigned short*)indices + start;
1176 for(unsigned int i = 0; i < triangleCount; i++)
1178 batch[i][0] = index[0];
1179 batch[i][1] = index[1];
1180 batch[i][2] = index[1];
1186 case DRAW_INDEXEDLINESTRIP32:
1188 const unsigned int *index = (const unsigned int*)indices + start;
1190 for(unsigned int i = 0; i < triangleCount; i++)
1192 batch[i][0] = index[0];
1193 batch[i][1] = index[1];
1194 batch[i][2] = index[1];
1200 case DRAW_INDEXEDLINELOOP8:
1202 const unsigned char *index = (const unsigned char*)indices;
1204 for(unsigned int i = 0; i < triangleCount; i++)
1206 batch[i][0] = index[(start + i + 0) % loop];
1207 batch[i][1] = index[(start + i + 1) % loop];
1208 batch[i][2] = index[(start + i + 1) % loop];
1212 case DRAW_INDEXEDLINELOOP16:
1214 const unsigned short *index = (const unsigned short*)indices;
1216 for(unsigned int i = 0; i < triangleCount; i++)
1218 batch[i][0] = index[(start + i + 0) % loop];
1219 batch[i][1] = index[(start + i + 1) % loop];
1220 batch[i][2] = index[(start + i + 1) % loop];
1224 case DRAW_INDEXEDLINELOOP32:
1226 const unsigned int *index = (const unsigned int*)indices;
1228 for(unsigned int i = 0; i < triangleCount; i++)
1230 batch[i][0] = index[(start + i + 0) % loop];
1231 batch[i][1] = index[(start + i + 1) % loop];
1232 batch[i][2] = index[(start + i + 1) % loop];
1236 case DRAW_INDEXEDTRIANGLELIST8:
1238 const unsigned char *index = (const unsigned char*)indices + 3 * start;
1240 for(unsigned int i = 0; i < triangleCount; i++)
1242 batch[i][0] = index[0];
1243 batch[i][1] = index[1];
1244 batch[i][2] = index[2];
1250 case DRAW_INDEXEDTRIANGLELIST16:
1252 const unsigned short *index = (const unsigned short*)indices + 3 * start;
1254 for(unsigned int i = 0; i < triangleCount; i++)
1256 batch[i][0] = index[0];
1257 batch[i][1] = index[1];
1258 batch[i][2] = index[2];
1264 case DRAW_INDEXEDTRIANGLELIST32:
1266 const unsigned int *index = (const unsigned int*)indices + 3 * start;
1268 for(unsigned int i = 0; i < triangleCount; i++)
1270 batch[i][0] = index[0];
1271 batch[i][1] = index[1];
1272 batch[i][2] = index[2];
1278 case DRAW_INDEXEDTRIANGLESTRIP8:
1280 const unsigned char *index = (const unsigned char*)indices + start;
1282 for(unsigned int i = 0; i < triangleCount; i++)
1284 batch[i][0] = index[0];
1285 batch[i][1] = index[((start + i) & 1) + 1];
1286 batch[i][2] = index[(~(start + i) & 1) + 1];
1292 case DRAW_INDEXEDTRIANGLESTRIP16:
1294 const unsigned short *index = (const unsigned short*)indices + start;
1296 for(unsigned int i = 0; i < triangleCount; i++)
1298 batch[i][0] = index[0];
1299 batch[i][1] = index[((start + i) & 1) + 1];
1300 batch[i][2] = index[(~(start + i) & 1) + 1];
1306 case DRAW_INDEXEDTRIANGLESTRIP32:
1308 const unsigned int *index = (const unsigned int*)indices + start;
1310 for(unsigned int i = 0; i < triangleCount; i++)
1312 batch[i][0] = index[0];
1313 batch[i][1] = index[((start + i) & 1) + 1];
1314 batch[i][2] = index[(~(start + i) & 1) + 1];
1320 case DRAW_INDEXEDTRIANGLEFAN8:
1322 const unsigned char *index = (const unsigned char*)indices;
1324 for(unsigned int i = 0; i < triangleCount; i++)
1326 batch[i][0] = index[start + i + 1];
1327 batch[i][1] = index[start + i + 2];
1328 batch[i][2] = index[0];
1332 case DRAW_INDEXEDTRIANGLEFAN16:
1334 const unsigned short *index = (const unsigned short*)indices;
1336 for(unsigned int i = 0; i < triangleCount; i++)
1338 batch[i][0] = index[start + i + 1];
1339 batch[i][1] = index[start + i + 2];
1340 batch[i][2] = index[0];
1344 case DRAW_INDEXEDTRIANGLEFAN32:
1346 const unsigned int *index = (const unsigned int*)indices;
1348 for(unsigned int i = 0; i < triangleCount; i++)
1350 batch[i][0] = index[start + i + 1];
1351 batch[i][1] = index[start + i + 2];
1352 batch[i][2] = index[0];
1358 unsigned int index = 4 * start / 2;
1360 for(unsigned int i = 0; i < triangleCount; i += 2)
1362 batch[i+0][0] = index + 0;
1363 batch[i+0][1] = index + 1;
1364 batch[i+0][2] = index + 2;
1366 batch[i+1][0] = index + 0;
1367 batch[i+1][1] = index + 2;
1368 batch[i+1][2] = index + 3;
1378 task->vertexCount = triangleCount * 3;
1379 vertexRoutine(&triangle->v0, (unsigned int*)&batch, task, data);
1382 int Renderer::setupSolidTriangles(Renderer *renderer, int unit, int count)
1384 Triangle *triangle = renderer->triangleBatch[unit];
1385 Primitive *primitive = renderer->primitiveBatch[unit];
1387 DrawCall &draw = *renderer->drawList[renderer->primitiveProgress[unit].drawCall % DRAW_COUNT];
1388 SetupProcessor::State &state = draw.setupState;
1389 const SetupProcessor::RoutinePointer &setupRoutine = draw.setupPointer;
1391 int ms = state.multiSample;
1392 int pos = state.positionRegister;
1393 const DrawData *data = draw.data;
1396 for(int i = 0; i < count; i++, triangle++)
1398 Vertex &v0 = triangle->v0;
1399 Vertex &v1 = triangle->v1;
1400 Vertex &v2 = triangle->v2;
1402 if((v0.clipFlags & v1.clipFlags & v2.clipFlags) == Clipper::CLIP_FINITE)
1404 Polygon polygon(&v0.v[pos], &v1.v[pos], &v2.v[pos]);
1406 int clipFlagsOr = v0.clipFlags | v1.clipFlags | v2.clipFlags | draw.clipFlags;
1408 if(clipFlagsOr != Clipper::CLIP_FINITE)
1410 if(!renderer->clipper->clip(polygon, clipFlagsOr, draw))
1416 if(setupRoutine(primitive, triangle, &polygon, data))
1427 int Renderer::setupWireframeTriangle(Renderer *renderer, int unit, int count)
1429 Triangle *triangle = renderer->triangleBatch[unit];
1430 Primitive *primitive = renderer->primitiveBatch[unit];
1433 DrawCall &draw = *renderer->drawList[renderer->primitiveProgress[unit].drawCall % DRAW_COUNT];
1434 SetupProcessor::State &state = draw.setupState;
1435 SetupProcessor::RoutinePointer setupRoutine = draw.setupPointer;
1437 const Vertex &v0 = triangle[0].v0;
1438 const Vertex &v1 = triangle[0].v1;
1439 const Vertex &v2 = triangle[0].v2;
1441 float d = (v0.y * v1.x - v0.x * v1.y) * v2.w + (v0.x * v2.y - v0.y * v2.x) * v1.w + (v2.x * v1.y - v1.x * v2.y) * v0.w;
1443 if(state.cullMode == CULL_CLOCKWISE)
1445 if(d >= 0) return 0;
1447 else if(state.cullMode == CULL_COUNTERCLOCKWISE)
1449 if(d <= 0) return 0;
1453 triangle[1].v0 = v1;
1454 triangle[1].v1 = v2;
1455 triangle[2].v0 = v2;
1456 triangle[2].v1 = v0;
1458 if(state.color[0][0].flat) // FIXME
1460 for(int i = 0; i < 2; i++)
1462 triangle[1].v0.C[i] = triangle[0].v0.C[i];
1463 triangle[1].v1.C[i] = triangle[0].v0.C[i];
1464 triangle[2].v0.C[i] = triangle[0].v0.C[i];
1465 triangle[2].v1.C[i] = triangle[0].v0.C[i];
1469 for(int i = 0; i < 3; i++)
1471 if(setupLine(renderer, *primitive, *triangle, draw))
1473 primitive->area = 0.5f * d;
1485 int Renderer::setupVertexTriangle(Renderer *renderer, int unit, int count)
1487 Triangle *triangle = renderer->triangleBatch[unit];
1488 Primitive *primitive = renderer->primitiveBatch[unit];
1491 DrawCall &draw = *renderer->drawList[renderer->primitiveProgress[unit].drawCall % DRAW_COUNT];
1492 SetupProcessor::State &state = draw.setupState;
1494 const Vertex &v0 = triangle[0].v0;
1495 const Vertex &v1 = triangle[0].v1;
1496 const Vertex &v2 = triangle[0].v2;
1498 float d = (v0.y * v1.x - v0.x * v1.y) * v2.w + (v0.x * v2.y - v0.y * v2.x) * v1.w + (v2.x * v1.y - v1.x * v2.y) * v0.w;
1500 if(state.cullMode == CULL_CLOCKWISE)
1502 if(d >= 0) return 0;
1504 else if(state.cullMode == CULL_COUNTERCLOCKWISE)
1506 if(d <= 0) return 0;
1510 triangle[1].v0 = v1;
1511 triangle[2].v0 = v2;
1513 for(int i = 0; i < 3; i++)
1515 if(setupPoint(renderer, *primitive, *triangle, draw))
1517 primitive->area = 0.5f * d;
1529 int Renderer::setupLines(Renderer *renderer, int unit, int count)
1531 Triangle *triangle = renderer->triangleBatch[unit];
1532 Primitive *primitive = renderer->primitiveBatch[unit];
1535 DrawCall &draw = *renderer->drawList[renderer->primitiveProgress[unit].drawCall % DRAW_COUNT];
1536 SetupProcessor::State &state = draw.setupState;
1538 int ms = state.multiSample;
1540 for(int i = 0; i < count; i++)
1542 if(setupLine(renderer, *primitive, *triangle, draw))
1554 int Renderer::setupPoints(Renderer *renderer, int unit, int count)
1556 Triangle *triangle = renderer->triangleBatch[unit];
1557 Primitive *primitive = renderer->primitiveBatch[unit];
1560 DrawCall &draw = *renderer->drawList[renderer->primitiveProgress[unit].drawCall % DRAW_COUNT];
1561 SetupProcessor::State &state = draw.setupState;
1563 int ms = state.multiSample;
1565 for(int i = 0; i < count; i++)
1567 if(setupPoint(renderer, *primitive, *triangle, draw))
1579 bool Renderer::setupLine(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw)
1581 const SetupProcessor::RoutinePointer &setupRoutine = draw.setupPointer;
1582 const SetupProcessor::State &state = draw.setupState;
1583 const DrawData &data = *draw.data;
1585 float lineWidth = data.lineWidth;
1587 Vertex &v0 = triangle.v0;
1588 Vertex &v1 = triangle.v1;
1590 int pos = state.positionRegister;
1592 const float4 &P0 = v0.v[pos];
1593 const float4 &P1 = v1.v[pos];
1595 if(P0.w <= 0 && P1.w <= 0)
1600 const float W = data.Wx16[0] * (1.0f / 16.0f);
1601 const float H = data.Hx16[0] * (1.0f / 16.0f);
1603 float dx = W * (P1.x / P1.w - P0.x / P0.w);
1604 float dy = H * (P1.y / P1.w - P0.y / P0.w);
1606 if(dx == 0 && dy == 0)
1611 if(false) // Rectangle
1621 float scale = lineWidth * 0.5f / sqrt(dx*dx + dy*dy);
1626 float dx0w = dx * P0.w / W;
1627 float dy0h = dy * P0.w / H;
1628 float dx0h = dx * P0.w / H;
1629 float dy0w = dy * P0.w / W;
1631 float dx1w = dx * P1.w / W;
1632 float dy1h = dy * P1.w / H;
1633 float dx1h = dx * P1.w / H;
1634 float dy1w = dy * P1.w / W;
1636 P[0].x += -dy0w + -dx0w;
1637 P[0].y += -dx0h + +dy0h;
1638 C[0] = computeClipFlags(P[0], data);
1640 P[1].x += -dy1w + +dx1w;
1641 P[1].y += -dx1h + +dy1h;
1642 C[1] = computeClipFlags(P[1], data);
1644 P[2].x += +dy1w + +dx1w;
1645 P[2].y += +dx1h + -dy1h;
1646 C[2] = computeClipFlags(P[2], data);
1648 P[3].x += +dy0w + -dx0w;
1649 P[3].y += +dx0h + +dy0h;
1650 C[3] = computeClipFlags(P[3], data);
1652 if((C[0] & C[1] & C[2] & C[3]) == Clipper::CLIP_FINITE)
1654 Polygon polygon(P, 4);
1656 int clipFlagsOr = C[0] | C[1] | C[2] | C[3] | draw.clipFlags;
1658 if(clipFlagsOr != Clipper::CLIP_FINITE)
1660 if(!renderer->clipper->clip(polygon, clipFlagsOr, draw))
1666 return setupRoutine(&primitive, &triangle, &polygon, &data);
1669 else // Diamond test convention
1683 float dx0 = lineWidth * 0.5f * P0.w / W;
1684 float dy0 = lineWidth * 0.5f * P0.w / H;
1686 float dx1 = lineWidth * 0.5f * P1.w / W;
1687 float dy1 = lineWidth * 0.5f * P1.w / H;
1690 C[0] = computeClipFlags(P[0], data);
1693 C[1] = computeClipFlags(P[1], data);
1696 C[2] = computeClipFlags(P[2], data);
1699 C[3] = computeClipFlags(P[3], data);
1702 C[4] = computeClipFlags(P[4], data);
1705 C[5] = computeClipFlags(P[5], data);
1708 C[6] = computeClipFlags(P[6], data);
1711 C[7] = computeClipFlags(P[7], data);
1713 if((C[0] & C[1] & C[2] & C[3] & C[4] & C[5] & C[6] & C[7]) == Clipper::CLIP_FINITE)
1719 if(dx > dy) // Right
1760 Polygon polygon(L, 6);
1762 int clipFlagsOr = C[0] | C[1] | C[2] | C[3] | C[4] | C[5] | C[6] | C[7] | draw.clipFlags;
1764 if(clipFlagsOr != Clipper::CLIP_FINITE)
1766 if(!renderer->clipper->clip(polygon, clipFlagsOr, draw))
1772 return setupRoutine(&primitive, &triangle, &polygon, &data);
1779 bool Renderer::setupPoint(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw)
1781 const SetupProcessor::RoutinePointer &setupRoutine = draw.setupPointer;
1782 const SetupProcessor::State &state = draw.setupState;
1783 const DrawData &data = *draw.data;
1785 Vertex &v = triangle.v0;
1789 int pts = state.pointSizeRegister;
1791 if(state.pointSizeRegister != 0xF)
1797 pSize = data.point.pointSize[0];
1800 pSize = clamp(pSize, data.point.pointSizeMin, data.point.pointSizeMax);
1805 int pos = state.positionRegister;
1812 const float X = pSize * P[0].w * data.halfPixelX[0];
1813 const float Y = pSize * P[0].w * data.halfPixelY[0];
1817 C[0] = computeClipFlags(P[0], data);
1821 C[1] = computeClipFlags(P[1], data);
1825 C[2] = computeClipFlags(P[2], data);
1829 C[3] = computeClipFlags(P[3], data);
1831 triangle.v1 = triangle.v0;
1832 triangle.v2 = triangle.v0;
1834 triangle.v1.X += iround(16 * 0.5f * pSize);
1835 triangle.v2.Y -= iround(16 * 0.5f * pSize) * (data.Hx16[0] > 0.0f ? 1 : -1); // Both Direct3D and OpenGL expect (0, 0) in the top-left corner
1837 Polygon polygon(P, 4);
1839 if((C[0] & C[1] & C[2] & C[3]) == Clipper::CLIP_FINITE)
1841 int clipFlagsOr = C[0] | C[1] | C[2] | C[3] | draw.clipFlags;
1843 if(clipFlagsOr != Clipper::CLIP_FINITE)
1845 if(!renderer->clipper->clip(polygon, clipFlagsOr, draw))
1851 return setupRoutine(&primitive, &triangle, &polygon, &data);
1857 unsigned int Renderer::computeClipFlags(const float4 &v, const DrawData &data)
1859 float clX = v.x + data.halfPixelX[0] * v.w;
1860 float clY = v.y + data.halfPixelY[0] * v.w;
1862 return ((clX > v.w) << 0) |
1863 ((clY > v.w) << 1) |
1864 ((v.z > v.w) << 2) |
1865 ((clX < -v.w) << 3) |
1866 ((clY < -v.w) << 4) |
1868 Clipper::CLIP_FINITE; // FIXME: xyz finite
1871 void Renderer::initializeThreads()
1873 unitCount = ceilPow2(threadCount);
1874 clusterCount = ceilPow2(threadCount);
1876 for(int i = 0; i < unitCount; i++)
1878 triangleBatch[i] = (Triangle*)allocate(batchSize * sizeof(Triangle));
1879 primitiveBatch[i] = (Primitive*)allocate(batchSize * sizeof(Primitive));
1882 for(int i = 0; i < threadCount; i++)
1884 vertexTask[i] = (VertexTask*)allocate(sizeof(VertexTask));
1885 vertexTask[i]->vertexCache.drawCall = -1;
1887 task[i].type = Task::SUSPEND;
1889 resume[i] = new Event();
1890 suspend[i] = new Event();
1892 Parameters parameters;
1893 parameters.threadIndex = i;
1894 parameters.renderer = this;
1896 exitThreads = false;
1897 worker[i] = new Thread(threadFunction, ¶meters);
1900 suspend[i]->signal();
1904 void Renderer::terminateThreads()
1906 while(threadsAwake != 0)
1911 for(int thread = 0; thread < threadCount; thread++)
1916 resume[thread]->signal();
1917 worker[thread]->join();
1919 delete worker[thread];
1921 delete resume[thread];
1923 delete suspend[thread];
1924 suspend[thread] = 0;
1927 deallocate(vertexTask[thread]);
1928 vertexTask[thread] = 0;
1931 for(int i = 0; i < 16; i++)
1933 deallocate(triangleBatch[i]);
1934 triangleBatch[i] = 0;
1936 deallocate(primitiveBatch[i]);
1937 primitiveBatch[i] = 0;
1941 void Renderer::loadConstants(const VertexShader *vertexShader)
1943 if(!vertexShader) return;
1945 size_t count = vertexShader->getLength();
1947 for(size_t i = 0; i < count; i++)
1949 const Shader::Instruction *instruction = vertexShader->getInstruction(i);
1951 if(instruction->opcode == Shader::OPCODE_DEF)
1953 int index = instruction->dst.index;
1956 value[0] = instruction->src[0].value[0];
1957 value[1] = instruction->src[0].value[1];
1958 value[2] = instruction->src[0].value[2];
1959 value[3] = instruction->src[0].value[3];
1961 setVertexShaderConstantF(index, value);
1963 else if(instruction->opcode == Shader::OPCODE_DEFI)
1965 int index = instruction->dst.index;
1968 integer[0] = instruction->src[0].integer[0];
1969 integer[1] = instruction->src[0].integer[1];
1970 integer[2] = instruction->src[0].integer[2];
1971 integer[3] = instruction->src[0].integer[3];
1973 setVertexShaderConstantI(index, integer);
1975 else if(instruction->opcode == Shader::OPCODE_DEFB)
1977 int index = instruction->dst.index;
1978 int boolean = instruction->src[0].boolean[0];
1980 setVertexShaderConstantB(index, &boolean);
1985 void Renderer::loadConstants(const PixelShader *pixelShader)
1987 if(!pixelShader) return;
1989 size_t count = pixelShader->getLength();
1991 for(size_t i = 0; i < count; i++)
1993 const Shader::Instruction *instruction = pixelShader->getInstruction(i);
1995 if(instruction->opcode == Shader::OPCODE_DEF)
1997 int index = instruction->dst.index;
2000 value[0] = instruction->src[0].value[0];
2001 value[1] = instruction->src[0].value[1];
2002 value[2] = instruction->src[0].value[2];
2003 value[3] = instruction->src[0].value[3];
2005 setPixelShaderConstantF(index, value);
2007 else if(instruction->opcode == Shader::OPCODE_DEFI)
2009 int index = instruction->dst.index;
2012 integer[0] = instruction->src[0].integer[0];
2013 integer[1] = instruction->src[0].integer[1];
2014 integer[2] = instruction->src[0].integer[2];
2015 integer[3] = instruction->src[0].integer[3];
2017 setPixelShaderConstantI(index, integer);
2019 else if(instruction->opcode == Shader::OPCODE_DEFB)
2021 int index = instruction->dst.index;
2022 int boolean = instruction->src[0].boolean[0];
2024 setPixelShaderConstantB(index, &boolean);
2029 void Renderer::setIndexBuffer(Resource *indexBuffer)
2031 context->indexBuffer = indexBuffer;
2034 void Renderer::setMultiSampleMask(unsigned int mask)
2036 context->sampleMask = mask;
2039 void Renderer::setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing)
2041 sw::transparencyAntialiasing = transparencyAntialiasing;
2044 bool Renderer::isReadWriteTexture(int sampler)
2046 for(int index = 0; index < 4; index++)
2048 if(context->renderTarget[index] && context->texture[sampler] == context->renderTarget[index]->getResource())
2054 if(context->depthStencil && context->texture[sampler] == context->depthStencil->getResource())
2062 void Renderer::updateClipper()
2064 if(updateClipPlanes)
2066 if(VertexProcessor::isFixedFunction()) // User plane in world space
2068 const Matrix &scissorWorld = getViewTransform();
2070 if(clipFlags & Clipper::CLIP_PLANE0) clipPlane[0] = scissorWorld * userPlane[0];
2071 if(clipFlags & Clipper::CLIP_PLANE1) clipPlane[1] = scissorWorld * userPlane[1];
2072 if(clipFlags & Clipper::CLIP_PLANE2) clipPlane[2] = scissorWorld * userPlane[2];
2073 if(clipFlags & Clipper::CLIP_PLANE3) clipPlane[3] = scissorWorld * userPlane[3];
2074 if(clipFlags & Clipper::CLIP_PLANE4) clipPlane[4] = scissorWorld * userPlane[4];
2075 if(clipFlags & Clipper::CLIP_PLANE5) clipPlane[5] = scissorWorld * userPlane[5];
2077 else // User plane in clip space
2079 if(clipFlags & Clipper::CLIP_PLANE0) clipPlane[0] = userPlane[0];
2080 if(clipFlags & Clipper::CLIP_PLANE1) clipPlane[1] = userPlane[1];
2081 if(clipFlags & Clipper::CLIP_PLANE2) clipPlane[2] = userPlane[2];
2082 if(clipFlags & Clipper::CLIP_PLANE3) clipPlane[3] = userPlane[3];
2083 if(clipFlags & Clipper::CLIP_PLANE4) clipPlane[4] = userPlane[4];
2084 if(clipFlags & Clipper::CLIP_PLANE5) clipPlane[5] = userPlane[5];
2087 updateClipPlanes = false;
2091 void Renderer::setTextureResource(unsigned int sampler, Resource *resource)
2093 ASSERT(sampler < TOTAL_IMAGE_UNITS);
2095 context->texture[sampler] = resource;
2098 void Renderer::setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type)
2100 ASSERT(sampler < TOTAL_IMAGE_UNITS && face < 6 && level < MIPMAP_LEVELS);
2102 context->sampler[sampler].setTextureLevel(face, level, surface, type);
2105 void Renderer::setTextureFilter(SamplerType type, int sampler, FilterType textureFilter)
2107 if(type == SAMPLER_PIXEL)
2109 PixelProcessor::setTextureFilter(sampler, textureFilter);
2113 VertexProcessor::setTextureFilter(sampler, textureFilter);
2117 void Renderer::setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter)
2119 if(type == SAMPLER_PIXEL)
2121 PixelProcessor::setMipmapFilter(sampler, mipmapFilter);
2125 VertexProcessor::setMipmapFilter(sampler, mipmapFilter);
2129 void Renderer::setGatherEnable(SamplerType type, int sampler, bool enable)
2131 if(type == SAMPLER_PIXEL)
2133 PixelProcessor::setGatherEnable(sampler, enable);
2137 VertexProcessor::setGatherEnable(sampler, enable);
2141 void Renderer::setAddressingModeU(SamplerType type, int sampler, AddressingMode addressMode)
2143 if(type == SAMPLER_PIXEL)
2145 PixelProcessor::setAddressingModeU(sampler, addressMode);
2149 VertexProcessor::setAddressingModeU(sampler, addressMode);
2153 void Renderer::setAddressingModeV(SamplerType type, int sampler, AddressingMode addressMode)
2155 if(type == SAMPLER_PIXEL)
2157 PixelProcessor::setAddressingModeV(sampler, addressMode);
2161 VertexProcessor::setAddressingModeV(sampler, addressMode);
2165 void Renderer::setAddressingModeW(SamplerType type, int sampler, AddressingMode addressMode)
2167 if(type == SAMPLER_PIXEL)
2169 PixelProcessor::setAddressingModeW(sampler, addressMode);
2173 VertexProcessor::setAddressingModeW(sampler, addressMode);
2177 void Renderer::setReadSRGB(SamplerType type, int sampler, bool sRGB)
2179 if(type == SAMPLER_PIXEL)
2181 PixelProcessor::setReadSRGB(sampler, sRGB);
2185 VertexProcessor::setReadSRGB(sampler, sRGB);
2189 void Renderer::setMipmapLOD(SamplerType type, int sampler, float bias)
2191 if(type == SAMPLER_PIXEL)
2193 PixelProcessor::setMipmapLOD(sampler, bias);
2197 VertexProcessor::setMipmapLOD(sampler, bias);
2201 void Renderer::setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor)
2203 if(type == SAMPLER_PIXEL)
2205 PixelProcessor::setBorderColor(sampler, borderColor);
2209 VertexProcessor::setBorderColor(sampler, borderColor);
2213 void Renderer::setMaxAnisotropy(SamplerType type, int sampler, float maxAnisotropy)
2215 if(type == SAMPLER_PIXEL)
2217 PixelProcessor::setMaxAnisotropy(sampler, maxAnisotropy);
2221 VertexProcessor::setMaxAnisotropy(sampler, maxAnisotropy);
2225 void Renderer::setPointSpriteEnable(bool pointSpriteEnable)
2227 context->setPointSpriteEnable(pointSpriteEnable);
2230 void Renderer::setPointScaleEnable(bool pointScaleEnable)
2232 context->setPointScaleEnable(pointScaleEnable);
2235 void Renderer::setLineWidth(float width)
2237 context->lineWidth = width;
2240 void Renderer::setDepthBias(float bias)
2245 void Renderer::setSlopeDepthBias(float slopeBias)
2247 slopeDepthBias = slopeBias;
2250 void Renderer::setPixelShader(const PixelShader *shader)
2252 context->pixelShader = shader;
2254 loadConstants(shader);
2257 void Renderer::setVertexShader(const VertexShader *shader)
2259 context->vertexShader = shader;
2261 loadConstants(shader);
2264 void Renderer::setPixelShaderConstantF(int index, const float value[4], int count)
2266 for(int i = 0; i < DRAW_COUNT; i++)
2268 if(drawCall[i]->psDirtyConstF < index + count)
2270 drawCall[i]->psDirtyConstF = index + count;
2274 for(int i = 0; i < count; i++)
2276 PixelProcessor::setFloatConstant(index + i, value);
2281 void Renderer::setPixelShaderConstantI(int index, const int value[4], int count)
2283 for(int i = 0; i < DRAW_COUNT; i++)
2285 if(drawCall[i]->psDirtyConstI < index + count)
2287 drawCall[i]->psDirtyConstI = index + count;
2291 for(int i = 0; i < count; i++)
2293 PixelProcessor::setIntegerConstant(index + i, value);
2298 void Renderer::setPixelShaderConstantB(int index, const int *boolean, int count)
2300 for(int i = 0; i < DRAW_COUNT; i++)
2302 if(drawCall[i]->psDirtyConstB < index + count)
2304 drawCall[i]->psDirtyConstB = index + count;
2308 for(int i = 0; i < count; i++)
2310 PixelProcessor::setBooleanConstant(index + i, *boolean);
2315 void Renderer::setVertexShaderConstantF(int index, const float value[4], int count)
2317 for(int i = 0; i < DRAW_COUNT; i++)
2319 if(drawCall[i]->vsDirtyConstF < index + count)
2321 drawCall[i]->vsDirtyConstF = index + count;
2325 for(int i = 0; i < count; i++)
2327 VertexProcessor::setFloatConstant(index + i, value);
2332 void Renderer::setVertexShaderConstantI(int index, const int value[4], int count)
2334 for(int i = 0; i < DRAW_COUNT; i++)
2336 if(drawCall[i]->vsDirtyConstI < index + count)
2338 drawCall[i]->vsDirtyConstI = index + count;
2342 for(int i = 0; i < count; i++)
2344 VertexProcessor::setIntegerConstant(index + i, value);
2349 void Renderer::setVertexShaderConstantB(int index, const int *boolean, int count)
2351 for(int i = 0; i < DRAW_COUNT; i++)
2353 if(drawCall[i]->vsDirtyConstB < index + count)
2355 drawCall[i]->vsDirtyConstB = index + count;
2359 for(int i = 0; i < count; i++)
2361 VertexProcessor::setBooleanConstant(index + i, *boolean);
2366 void Renderer::setModelMatrix(const Matrix &M, int i)
2368 VertexProcessor::setModelMatrix(M, i);
2371 void Renderer::setViewMatrix(const Matrix &V)
2373 VertexProcessor::setViewMatrix(V);
2374 updateClipPlanes = true;
2377 void Renderer::setBaseMatrix(const Matrix &B)
2379 VertexProcessor::setBaseMatrix(B);
2380 updateClipPlanes = true;
2383 void Renderer::setProjectionMatrix(const Matrix &P)
2385 VertexProcessor::setProjectionMatrix(P);
2386 updateClipPlanes = true;
2389 void Renderer::addQuery(Query *query)
2391 queries.push_back(query);
2394 void Renderer::removeQuery(Query *query)
2396 queries.remove(query);
2400 int Renderer::getThreadCount()
2405 int64_t Renderer::getVertexTime(int thread)
2407 return vertexTime[thread];
2410 int64_t Renderer::getSetupTime(int thread)
2412 return setupTime[thread];
2415 int64_t Renderer::getPixelTime(int thread)
2417 return pixelTime[thread];
2420 void Renderer::resetTimers()
2422 for(int thread = 0; thread < threadCount; thread++)
2424 vertexTime[thread] = 0;
2425 setupTime[thread] = 0;
2426 pixelTime[thread] = 0;
2431 void Renderer::setViewport(const Viewport &viewport)
2433 this->viewport = viewport;
2436 void Renderer::setScissor(const Rect &scissor)
2438 this->scissor = scissor;
2441 void Renderer::setClipFlags(int flags)
2443 clipFlags = flags << 8; // Bottom 8 bits used by legacy frustum
2446 void Renderer::setClipPlane(unsigned int index, const float plane[4])
2450 userPlane[index] = plane;
2454 updateClipPlanes = true;
2457 void Renderer::updateConfiguration(bool initialUpdate)
2459 bool newConfiguration = swiftConfig->hasNewConfiguration();
2461 if(newConfiguration || initialUpdate)
2465 SwiftConfig::Configuration configuration = {0};
2466 swiftConfig->getConfiguration(configuration);
2468 precacheVertex = !newConfiguration && configuration.precache;
2469 precacheSetup = !newConfiguration && configuration.precache;
2470 precachePixel = !newConfiguration && configuration.precache;
2472 VertexProcessor::setRoutineCacheSize(configuration.vertexRoutineCacheSize);
2473 PixelProcessor::setRoutineCacheSize(configuration.pixelRoutineCacheSize);
2474 SetupProcessor::setRoutineCacheSize(configuration.setupRoutineCacheSize);
2476 switch(configuration.textureSampleQuality)
2478 case 0: Sampler::setFilterQuality(FILTER_POINT); break;
2479 case 1: Sampler::setFilterQuality(FILTER_LINEAR); break;
2480 case 2: Sampler::setFilterQuality(FILTER_ANISOTROPIC); break;
2481 default: Sampler::setFilterQuality(FILTER_ANISOTROPIC); break;
2484 switch(configuration.mipmapQuality)
2486 case 0: Sampler::setMipmapQuality(MIPMAP_POINT); break;
2487 case 1: Sampler::setMipmapQuality(MIPMAP_LINEAR); break;
2488 default: Sampler::setMipmapQuality(MIPMAP_LINEAR); break;
2491 setPerspectiveCorrection(configuration.perspectiveCorrection);
2493 switch(configuration.transcendentalPrecision)
2496 logPrecision = APPROXIMATE;
2497 expPrecision = APPROXIMATE;
2498 rcpPrecision = APPROXIMATE;
2499 rsqPrecision = APPROXIMATE;
2502 logPrecision = PARTIAL;
2503 expPrecision = PARTIAL;
2504 rcpPrecision = PARTIAL;
2505 rsqPrecision = PARTIAL;
2508 logPrecision = ACCURATE;
2509 expPrecision = ACCURATE;
2510 rcpPrecision = ACCURATE;
2511 rsqPrecision = ACCURATE;
2514 logPrecision = WHQL;
2515 expPrecision = WHQL;
2516 rcpPrecision = WHQL;
2517 rsqPrecision = WHQL;
2520 logPrecision = IEEE;
2521 expPrecision = IEEE;
2522 rcpPrecision = IEEE;
2523 rsqPrecision = IEEE;
2526 logPrecision = ACCURATE;
2527 expPrecision = ACCURATE;
2528 rcpPrecision = ACCURATE;
2529 rsqPrecision = ACCURATE;
2533 switch(configuration.transparencyAntialiasing)
2535 case 0: transparencyAntialiasing = TRANSPARENCY_NONE; break;
2536 case 1: transparencyAntialiasing = TRANSPARENCY_ALPHA_TO_COVERAGE; break;
2537 default: transparencyAntialiasing = TRANSPARENCY_NONE; break;
2540 switch(configuration.threadCount)
2542 case -1: threadCount = CPUID::coreCount(); break;
2543 case 0: threadCount = CPUID::processAffinity(); break;
2544 default: threadCount = configuration.threadCount; break;
2547 CPUID::setEnableSSE4_1(configuration.enableSSE4_1);
2548 CPUID::setEnableSSSE3(configuration.enableSSSE3);
2549 CPUID::setEnableSSE3(configuration.enableSSE3);
2550 CPUID::setEnableSSE2(configuration.enableSSE2);
2551 CPUID::setEnableSSE(configuration.enableSSE);
2553 for(int pass = 0; pass < 10; pass++)
2555 optimization[pass] = configuration.optimization[pass];
2558 forceWindowed = configuration.forceWindowed;
2559 complementaryDepthBuffer = configuration.complementaryDepthBuffer;
2560 postBlendSRGB = configuration.postBlendSRGB;
2561 exactColorRounding = configuration.exactColorRounding;
2562 forceClearRegisters = configuration.forceClearRegisters;
2565 minPrimitives = configuration.minPrimitives;
2566 maxPrimitives = configuration.maxPrimitives;
2570 if(!initialUpdate && !worker[0])
2572 initializeThreads();