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"
36 bool disableServer = true;
39 unsigned int minPrimitives = 1;
40 unsigned int maxPrimitives = 1 << 21;
45 extern bool halfIntegerCoordinates; // Pixel centers are not at integer coordinates
46 extern bool symmetricNormalizedDepth; // [-1, 1] instead of [0, 1]
47 extern bool booleanFaceRegister;
48 extern bool fullPixelPositionRegister;
50 extern bool forceWindowed;
51 extern bool complementaryDepthBuffer;
52 extern bool postBlendSRGB;
53 extern bool exactColorRounding;
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;
72 BackoffLock Renderer::codegenMutex;
84 vsDirtyConstF = 256 + 1;
94 data = (DrawData*)allocate(sizeof(DrawData));
95 data->constants = &constants;
105 Renderer::Renderer(Context *context, bool halfIntegerCoordinates, bool symmetricNormalizedDepth, bool booleanFaceRegister, bool fullPixelPositionRegister, bool exactColorRounding) : context(context), VertexProcessor(context), PixelProcessor(context), SetupProcessor(context), viewport()
107 sw::halfIntegerCoordinates = halfIntegerCoordinates;
108 sw::symmetricNormalizedDepth = symmetricNormalizedDepth;
109 sw::booleanFaceRegister = booleanFaceRegister;
110 sw::fullPixelPositionRegister = fullPixelPositionRegister;
111 sw::exactColorRounding = exactColorRounding;
113 setRenderTarget(0, 0);
114 clipper = new Clipper();
116 updateViewMatrix = true;
117 updateBaseMatrix = true;
118 updateProjectionMatrix = true;
119 updateClipPlanes = true;
125 for(int i = 0; i < 16; i++)
135 resumeApp = new Event();
143 for(int i = 0; i < 16; i++)
145 triangleBatch[i] = 0;
146 primitiveBatch[i] = 0;
149 for(int draw = 0; draw < DRAW_COUNT; draw++)
151 drawCall[draw] = new DrawCall();
152 drawList[draw] = drawCall[draw];
155 for(int unit = 0; unit < 16; unit++)
157 primitiveProgress[unit].init();
160 for(int cluster = 0; cluster < 16; cluster++)
162 pixelProgress[cluster].init();
167 swiftConfig = new SwiftConfig(disableServer);
168 updateConfiguration(true);
170 sync = new Resource(0);
173 Renderer::~Renderer()
183 for(int draw = 0; draw < DRAW_COUNT; draw++)
185 delete drawCall[draw];
191 void Renderer::blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter)
193 blitter.blit(source, sRect, dest, dRect, filter);
196 void Renderer::draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update)
199 if(count < minPrimitives || count > maxPrimitives)
205 context->drawType = drawType;
207 updateConfiguration();
210 int ss = context->getSuperSampleCount();
211 int ms = context->getMultiSampleCount();
213 for(int q = 0; q < ss; q++)
215 int oldMultiSampleMask = context->multiSampleMask;
216 context->multiSampleMask = (context->sampleMask >> (ms * q)) & ((unsigned)0xFFFFFFFF >> (32 - ms));
218 if(!context->multiSampleMask)
223 sync->lock(sw::PRIVATE);
225 Routine *vertexRoutine;
226 Routine *setupRoutine;
227 Routine *pixelRoutine;
229 if(update || oldMultiSampleMask != context->multiSampleMask)
231 vertexState = VertexProcessor::update();
232 setupState = SetupProcessor::update();
233 pixelState = PixelProcessor::update();
237 vertexRoutine = VertexProcessor::routine(vertexState);
238 setupRoutine = SetupProcessor::routine(setupState);
239 pixelRoutine = PixelProcessor::routine(pixelState);
241 codegenMutex.unlock();
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;
432 draw->vsDirtyConstF = 256 + 1;
433 draw->vsDirtyConstI = 16;
434 draw->vsDirtyConstB = 16;
437 if(pixelState.stencilActive)
439 data->stencil[0] = stencil;
440 data->stencil[1] = stencilCCW;
443 if(pixelState.fogActive)
448 if(setupState.isDrawPoint)
453 data->lineWidth = context->lineWidth;
455 data->factor = factor;
457 if(pixelState.transparencyAntialiasing == TRANSPARENCY_ALPHA_TO_COVERAGE)
459 float ref = (float)context->alphaReference * (1.0f / 255.0f);
460 float margin = sw::min(ref, 1.0f - ref);
464 data->a2c0 = replicate(ref - margin * 0.6f);
465 data->a2c1 = replicate(ref - margin * 0.2f);
466 data->a2c2 = replicate(ref + margin * 0.2f);
467 data->a2c3 = replicate(ref + margin * 0.6f);
471 data->a2c0 = replicate(ref - margin * 0.3f);
472 data->a2c1 = replicate(ref + margin * 0.3f);
477 if(pixelState.occlusionEnabled)
479 for(int cluster = 0; cluster < clusterCount; cluster++)
481 data->occlusion[cluster] = 0;
486 for(int cluster = 0; cluster < clusterCount; cluster++)
488 for(int i = 0; i < PERF_TIMERS; i++)
490 data->cycles[i][cluster] = 0;
497 float W = 0.5f * viewport.width;
498 float H = 0.5f * viewport.height;
499 float X0 = viewport.x0 + W;
500 float Y0 = viewport.y0 + H;
501 float N = viewport.minZ;
502 float F = viewport.maxZ;
505 if(context->isDrawTriangle(false))
510 if(complementaryDepthBuffer)
516 static const float X[5][16] = // Fragment offsets
518 {+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
519 {-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
520 {-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
521 {+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
522 {+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
525 static const float Y[5][16] = // Fragment offsets
527 {+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
528 {-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
529 {-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
530 {-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
531 {-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
534 int s = sw::log2(ss);
536 data->Wx16 = replicate(W * 16);
537 data->Hx16 = replicate(H * 16);
538 data->X0x16 = replicate(X0 * 16);
539 data->Y0x16 = replicate(Y0 * 16);
540 data->XXXX = replicate(X[s][q] / W);
541 data->YYYY = replicate(Y[s][q] / H);
542 data->halfPixelX = replicate(0.5f / W);
543 data->halfPixelY = replicate(0.5f / H);
544 data->viewportHeight = abs(viewport.height);
545 data->slopeDepthBias = slopeDepthBias;
546 data->depthRange = Z;
548 draw->clipFlags = clipFlags;
552 if(clipFlags & Clipper::CLIP_PLANE0) data->clipPlane[0] = clipPlane[0];
553 if(clipFlags & Clipper::CLIP_PLANE1) data->clipPlane[1] = clipPlane[1];
554 if(clipFlags & Clipper::CLIP_PLANE2) data->clipPlane[2] = clipPlane[2];
555 if(clipFlags & Clipper::CLIP_PLANE3) data->clipPlane[3] = clipPlane[3];
556 if(clipFlags & Clipper::CLIP_PLANE4) data->clipPlane[4] = clipPlane[4];
557 if(clipFlags & Clipper::CLIP_PLANE5) data->clipPlane[5] = clipPlane[5];
563 for(int index = 0; index < 4; index++)
565 draw->renderTarget[index] = context->renderTarget[index];
567 if(draw->renderTarget[index])
569 data->colorBuffer[index] = (unsigned int*)context->renderTarget[index]->lockInternal(0, 0, q * ms, LOCK_READWRITE, MANAGED);
570 data->colorPitchB[index] = context->renderTarget[index]->getInternalPitchB();
571 data->colorSliceB[index] = context->renderTarget[index]->getInternalSliceB();
575 draw->depthStencil = context->depthStencil;
577 if(draw->depthStencil)
579 data->depthBuffer = (float*)context->depthStencil->lockInternal(0, 0, q * ms, LOCK_READWRITE, MANAGED);
580 data->depthPitchB = context->depthStencil->getInternalPitchB();
581 data->depthSliceB = context->depthStencil->getInternalSliceB();
583 data->stencilBuffer = (unsigned char*)context->depthStencil->lockStencil(q * ms, MANAGED);
584 data->stencilPitchB = context->depthStencil->getStencilPitchB();
585 data->stencilSliceB = context->depthStencil->getStencilSliceB();
591 data->scissorX0 = scissor.x0;
592 data->scissorX1 = scissor.x1;
593 data->scissorY0 = scissor.y0;
594 data->scissorY1 = scissor.y1;
600 draw->references = (count + batch - 1) / batch;
602 schedulerMutex.lock();
604 schedulerMutex.unlock();
611 task[0].type = Task::RESUME;
618 void Renderer::threadFunction(void *parameters)
620 Renderer *renderer = static_cast<Parameters*>(parameters)->renderer;
621 int threadIndex = static_cast<Parameters*>(parameters)->threadIndex;
623 if(logPrecision < IEEE)
625 CPUID::setFlushToZero(true);
626 CPUID::setDenormalsAreZero(true);
629 renderer->threadLoop(threadIndex);
632 void Renderer::threadLoop(int threadIndex)
636 taskLoop(threadIndex);
638 suspend[threadIndex]->signal();
639 resume[threadIndex]->wait();
643 void Renderer::taskLoop(int threadIndex)
645 while(task[threadIndex].type != Task::SUSPEND)
647 scheduleTask(threadIndex);
648 executeTask(threadIndex);
652 void Renderer::findAvailableTasks()
655 for(int cluster = 0; cluster < clusterCount; cluster++)
657 if(!pixelProgress[cluster].executing)
659 for(int unit = 0; unit < unitCount; unit++)
661 if(primitiveProgress[unit].references > 0) // Contains processed primitives
663 if(pixelProgress[cluster].drawCall == primitiveProgress[unit].drawCall)
665 if(pixelProgress[cluster].processedPrimitives == primitiveProgress[unit].firstPrimitive) // Previous primitives have been rendered
667 Task &task = taskQueue[qHead];
668 task.type = Task::PIXELS;
669 task.primitiveUnit = unit;
670 task.pixelCluster = cluster;
672 pixelProgress[cluster].executing = true;
674 // Commit to the task queue
675 qHead = (qHead + 1) % 32;
686 // Find primitive tasks
687 if(currentDraw == nextDraw)
689 return; // No more primitives to process
692 for(int unit = 0; unit < unitCount; unit++)
694 DrawCall *draw = drawList[currentDraw % DRAW_COUNT];
696 if(draw->primitive >= draw->count)
700 if(currentDraw == nextDraw)
702 return; // No more primitives to process
705 draw = drawList[currentDraw % DRAW_COUNT];
708 if(!primitiveProgress[unit].references) // Task not already being executed and not still in use by a pixel unit
710 int primitive = draw->primitive;
711 int count = draw->count;
712 int batch = draw->batchSize;
714 primitiveProgress[unit].drawCall = currentDraw;
715 primitiveProgress[unit].firstPrimitive = primitive;
716 primitiveProgress[unit].primitiveCount = count - primitive >= batch ? batch : count - primitive;
718 draw->primitive += batch;
720 Task &task = taskQueue[qHead];
721 task.type = Task::PRIMITIVES;
722 task.primitiveUnit = unit;
724 primitiveProgress[unit].references = -1;
726 // Commit to the task queue
727 qHead = (qHead + 1) % 32;
733 void Renderer::scheduleTask(int threadIndex)
735 schedulerMutex.lock();
737 if((int)qSize < threadCount - threadsAwake + 1)
739 findAvailableTasks();
744 task[threadIndex] = taskQueue[(qHead - qSize) % 32];
747 if(threadsAwake != threadCount)
749 int wakeup = qSize - threadsAwake + 1;
751 for(int i = 0; i < threadCount && wakeup > 0; i++)
753 if(task[i].type == Task::SUSPEND)
756 task[i].type = Task::RESUME;
767 task[threadIndex].type = Task::SUSPEND;
772 schedulerMutex.unlock();
775 void Renderer::executeTask(int threadIndex)
778 int64_t startTick = Timer::ticks();
781 switch(task[threadIndex].type)
783 case Task::PRIMITIVES:
785 int unit = task[threadIndex].primitiveUnit;
787 int input = primitiveProgress[unit].firstPrimitive;
788 int count = primitiveProgress[unit].primitiveCount;
789 DrawCall *draw = drawList[primitiveProgress[unit].drawCall % DRAW_COUNT];
790 int (*setupPrimitives)(Renderer *renderer, int batch, int count) = draw->setupPrimitives;
792 processPrimitiveVertices(unit, input, count, draw->count, threadIndex);
795 int64_t time = Timer::ticks();
796 vertexTime[threadIndex] += time - startTick;
800 int visible = setupPrimitives(this, unit, count);
802 primitiveProgress[unit].visible = visible;
803 primitiveProgress[unit].references = clusterCount;
806 setupTime[threadIndex] += Timer::ticks() - startTick;
812 int unit = task[threadIndex].primitiveUnit;
813 int visible = primitiveProgress[unit].visible;
817 int cluster = task[threadIndex].pixelCluster;
818 Primitive *primitive = primitiveBatch[unit];
819 DrawCall *draw = drawList[pixelProgress[cluster].drawCall % DRAW_COUNT];
820 DrawData *data = draw->data;
821 PixelProcessor::RoutinePointer pixelRoutine = draw->pixelPointer;
823 pixelRoutine(primitive, visible, cluster, data);
826 finishRendering(task[threadIndex]);
829 pixelTime[threadIndex] += Timer::ticks() - startTick;
842 void Renderer::synchronize()
844 sync->lock(sw::PUBLIC);
848 void Renderer::finishRendering(Task &pixelTask)
850 int unit = pixelTask.primitiveUnit;
851 int cluster = pixelTask.pixelCluster;
853 DrawCall &draw = *drawList[primitiveProgress[unit].drawCall % DRAW_COUNT];
854 DrawData &data = *draw.data;
855 int primitive = primitiveProgress[unit].firstPrimitive;
856 int count = primitiveProgress[unit].primitiveCount;
858 pixelProgress[cluster].processedPrimitives = primitive + count;
860 if(pixelProgress[cluster].processedPrimitives >= draw.count)
862 pixelProgress[cluster].drawCall++;
863 pixelProgress[cluster].processedPrimitives = 0;
866 int ref = atomicDecrement(&primitiveProgress[unit].references);
870 ref = atomicDecrement(&draw.references);
875 for(int cluster = 0; cluster < clusterCount; cluster++)
877 for(int i = 0; i < PERF_TIMERS; i++)
879 profiler.cycles[i] += data.cycles[i][cluster];
886 for(std::list<Query*>::iterator q = draw.queries->begin(); q != draw.queries->end(); q++)
890 for(int cluster = 0; cluster < clusterCount; cluster++)
892 atomicAdd((volatile int*)&query->data, data.occlusion[cluster]);
895 atomicDecrement(&query->reference);
902 for(int i = 0; i < 4; i++)
904 if(draw.renderTarget[i])
906 draw.renderTarget[i]->unlockInternal();
910 if(draw.depthStencil)
912 draw.depthStencil->unlockInternal();
913 draw.depthStencil->unlockStencil();
916 for(int i = 0; i < TOTAL_IMAGE_UNITS; i++)
920 draw.texture[i]->unlock();
924 for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
926 if(draw.vertexStream[i])
928 draw.vertexStream[i]->unlock();
934 draw.indexBuffer->unlock();
937 draw.vertexRoutine->unbind();
938 draw.setupRoutine->unbind();
939 draw.pixelRoutine->unbind();
943 draw.references = -1;
948 pixelProgress[cluster].executing = false;
951 void Renderer::processPrimitiveVertices(int unit, unsigned int start, unsigned int triangleCount, unsigned int loop, int thread)
953 Triangle *triangle = triangleBatch[unit];
954 DrawCall *draw = drawList[primitiveProgress[unit].drawCall % DRAW_COUNT];
955 DrawData *data = draw->data;
956 VertexTask *task = vertexTask[thread];
958 const void *indices = data->indices;
959 VertexProcessor::RoutinePointer vertexRoutine = draw->vertexPointer;
961 if(task->vertexCache.drawCall != primitiveProgress[unit].drawCall)
963 task->vertexCache.clear();
964 task->vertexCache.drawCall = primitiveProgress[unit].drawCall;
967 unsigned int batch[128][3]; // FIXME: Adjust to dynamic batch size
969 switch(draw->drawType)
973 unsigned int index = start;
975 for(unsigned int i = 0; i < triangleCount; i++)
987 unsigned int index = 2 * start;
989 for(unsigned int i = 0; i < triangleCount; i++)
991 batch[i][0] = index + 0;
992 batch[i][1] = index + 1;
993 batch[i][2] = index + 1;
1001 unsigned int index = start;
1003 for(unsigned int i = 0; i < triangleCount; i++)
1005 batch[i][0] = index + 0;
1006 batch[i][1] = index + 1;
1007 batch[i][2] = index + 1;
1015 unsigned int index = start;
1017 for(unsigned int i = 0; i < triangleCount; i++)
1019 batch[i][0] = (index + 0) % loop;
1020 batch[i][1] = (index + 1) % loop;
1021 batch[i][2] = (index + 1) % loop;
1027 case DRAW_TRIANGLELIST:
1029 unsigned int index = 3 * start;
1031 for(unsigned int i = 0; i < triangleCount; i++)
1033 batch[i][0] = index + 0;
1034 batch[i][1] = index + 1;
1035 batch[i][2] = index + 2;
1041 case DRAW_TRIANGLESTRIP:
1043 unsigned int index = start;
1045 for(unsigned int i = 0; i < triangleCount; i++)
1047 batch[i][0] = index + 0;
1048 batch[i][1] = index + (index & 1) + 1;
1049 batch[i][2] = index + (~index & 1) + 1;
1055 case DRAW_TRIANGLEFAN:
1057 unsigned int index = start;
1059 for(unsigned int i = 0; i < triangleCount; i++)
1061 batch[i][0] = index + 1;
1062 batch[i][1] = index + 2;
1069 case DRAW_INDEXEDPOINTLIST8:
1071 const unsigned char *index = (const unsigned char*)indices + start;
1073 for(unsigned int i = 0; i < triangleCount; i++)
1075 batch[i][0] = *index;
1076 batch[i][1] = *index;
1077 batch[i][2] = *index;
1083 case DRAW_INDEXEDPOINTLIST16:
1085 const unsigned short *index = (const unsigned short*)indices + start;
1087 for(unsigned int i = 0; i < triangleCount; i++)
1089 batch[i][0] = *index;
1090 batch[i][1] = *index;
1091 batch[i][2] = *index;
1097 case DRAW_INDEXEDPOINTLIST32:
1099 const unsigned int *index = (const unsigned int*)indices + start;
1101 for(unsigned int i = 0; i < triangleCount; i++)
1103 batch[i][0] = *index;
1104 batch[i][1] = *index;
1105 batch[i][2] = *index;
1111 case DRAW_INDEXEDLINELIST8:
1113 const unsigned char *index = (const unsigned char*)indices + 2 * start;
1115 for(unsigned int i = 0; i < triangleCount; i++)
1117 batch[i][0] = index[0];
1118 batch[i][1] = index[1];
1119 batch[i][2] = index[1];
1125 case DRAW_INDEXEDLINELIST16:
1127 const unsigned short *index = (const unsigned short*)indices + 2 * start;
1129 for(unsigned int i = 0; i < triangleCount; i++)
1131 batch[i][0] = index[0];
1132 batch[i][1] = index[1];
1133 batch[i][2] = index[1];
1139 case DRAW_INDEXEDLINELIST32:
1141 const unsigned int *index = (const unsigned int*)indices + 2 * start;
1143 for(unsigned int i = 0; i < triangleCount; i++)
1145 batch[i][0] = index[0];
1146 batch[i][1] = index[1];
1147 batch[i][2] = index[1];
1153 case DRAW_INDEXEDLINESTRIP8:
1155 const unsigned char *index = (const unsigned char*)indices + start;
1157 for(unsigned int i = 0; i < triangleCount; i++)
1159 batch[i][0] = index[0];
1160 batch[i][1] = index[1];
1161 batch[i][2] = index[1];
1167 case DRAW_INDEXEDLINESTRIP16:
1169 const unsigned short *index = (const unsigned short*)indices + start;
1171 for(unsigned int i = 0; i < triangleCount; i++)
1173 batch[i][0] = index[0];
1174 batch[i][1] = index[1];
1175 batch[i][2] = index[1];
1181 case DRAW_INDEXEDLINESTRIP32:
1183 const unsigned int *index = (const unsigned int*)indices + start;
1185 for(unsigned int i = 0; i < triangleCount; i++)
1187 batch[i][0] = index[0];
1188 batch[i][1] = index[1];
1189 batch[i][2] = index[1];
1195 case DRAW_INDEXEDLINELOOP8:
1197 const unsigned char *index = (const unsigned char*)indices;
1199 for(unsigned int i = 0; i < triangleCount; i++)
1201 batch[i][0] = index[(start + i + 0) % loop];
1202 batch[i][1] = index[(start + i + 1) % loop];
1203 batch[i][2] = index[(start + i + 1) % loop];
1207 case DRAW_INDEXEDLINELOOP16:
1209 const unsigned short *index = (const unsigned short*)indices;
1211 for(unsigned int i = 0; i < triangleCount; i++)
1213 batch[i][0] = index[(start + i + 0) % loop];
1214 batch[i][1] = index[(start + i + 1) % loop];
1215 batch[i][2] = index[(start + i + 1) % loop];
1219 case DRAW_INDEXEDLINELOOP32:
1221 const unsigned int *index = (const unsigned int*)indices;
1223 for(unsigned int i = 0; i < triangleCount; i++)
1225 batch[i][0] = index[(start + i + 0) % loop];
1226 batch[i][1] = index[(start + i + 1) % loop];
1227 batch[i][2] = index[(start + i + 1) % loop];
1231 case DRAW_INDEXEDTRIANGLELIST8:
1233 const unsigned char *index = (const unsigned char*)indices + 3 * start;
1235 for(unsigned int i = 0; i < triangleCount; i++)
1237 batch[i][0] = index[0];
1238 batch[i][1] = index[1];
1239 batch[i][2] = index[2];
1245 case DRAW_INDEXEDTRIANGLELIST16:
1247 const unsigned short *index = (const unsigned short*)indices + 3 * start;
1249 for(unsigned int i = 0; i < triangleCount; i++)
1251 batch[i][0] = index[0];
1252 batch[i][1] = index[1];
1253 batch[i][2] = index[2];
1259 case DRAW_INDEXEDTRIANGLELIST32:
1261 const unsigned int *index = (const unsigned int*)indices + 3 * start;
1263 for(unsigned int i = 0; i < triangleCount; i++)
1265 batch[i][0] = index[0];
1266 batch[i][1] = index[1];
1267 batch[i][2] = index[2];
1273 case DRAW_INDEXEDTRIANGLESTRIP8:
1275 const unsigned char *index = (const unsigned char*)indices + start;
1277 for(unsigned int i = 0; i < triangleCount; i++)
1279 batch[i][0] = index[0];
1280 batch[i][1] = index[((start + i) & 1) + 1];
1281 batch[i][2] = index[(~(start + i) & 1) + 1];
1287 case DRAW_INDEXEDTRIANGLESTRIP16:
1289 const unsigned short *index = (const unsigned short*)indices + start;
1291 for(unsigned int i = 0; i < triangleCount; i++)
1293 batch[i][0] = index[0];
1294 batch[i][1] = index[((start + i) & 1) + 1];
1295 batch[i][2] = index[(~(start + i) & 1) + 1];
1301 case DRAW_INDEXEDTRIANGLESTRIP32:
1303 const unsigned int *index = (const unsigned int*)indices + start;
1305 for(unsigned int i = 0; i < triangleCount; i++)
1307 batch[i][0] = index[0];
1308 batch[i][1] = index[((start + i) & 1) + 1];
1309 batch[i][2] = index[(~(start + i) & 1) + 1];
1315 case DRAW_INDEXEDTRIANGLEFAN8:
1317 const unsigned char *index = (const unsigned char*)indices;
1319 for(unsigned int i = 0; i < triangleCount; i++)
1321 batch[i][0] = index[start + i + 1];
1322 batch[i][1] = index[start + i + 2];
1323 batch[i][2] = index[0];
1327 case DRAW_INDEXEDTRIANGLEFAN16:
1329 const unsigned short *index = (const unsigned short*)indices;
1331 for(unsigned int i = 0; i < triangleCount; i++)
1333 batch[i][0] = index[start + i + 1];
1334 batch[i][1] = index[start + i + 2];
1335 batch[i][2] = index[0];
1339 case DRAW_INDEXEDTRIANGLEFAN32:
1341 const unsigned int *index = (const unsigned int*)indices;
1343 for(unsigned int i = 0; i < triangleCount; i++)
1345 batch[i][0] = index[start + i + 1];
1346 batch[i][1] = index[start + i + 2];
1347 batch[i][2] = index[0];
1353 unsigned int index = 4 * start / 2;
1355 for(unsigned int i = 0; i < triangleCount; i += 2)
1357 batch[i+0][0] = index + 0;
1358 batch[i+0][1] = index + 1;
1359 batch[i+0][2] = index + 2;
1361 batch[i+1][0] = index + 0;
1362 batch[i+1][1] = index + 2;
1363 batch[i+1][2] = index + 3;
1373 task->vertexCount = triangleCount * 3;
1374 vertexRoutine(&triangle->v0, (unsigned int*)&batch, task, data);
1377 int Renderer::setupSolidTriangles(Renderer *renderer, int unit, int count)
1379 Triangle *triangle = renderer->triangleBatch[unit];
1380 Primitive *primitive = renderer->primitiveBatch[unit];
1382 DrawCall &draw = *renderer->drawList[renderer->primitiveProgress[unit].drawCall % DRAW_COUNT];
1383 SetupProcessor::State &state = draw.setupState;
1384 const SetupProcessor::RoutinePointer &setupRoutine = draw.setupPointer;
1386 int ms = state.multiSample;
1387 int pos = state.positionRegister;
1388 const DrawData *data = draw.data;
1391 for(int i = 0; i < count; i++, triangle++)
1393 Vertex &v0 = triangle->v0;
1394 Vertex &v1 = triangle->v1;
1395 Vertex &v2 = triangle->v2;
1397 if((v0.clipFlags & v1.clipFlags & v2.clipFlags) == Clipper::CLIP_FINITE)
1399 Polygon polygon(&v0.v[pos], &v1.v[pos], &v2.v[pos]);
1401 int clipFlagsOr = v0.clipFlags | v1.clipFlags | v2.clipFlags | draw.clipFlags;
1403 if(clipFlagsOr != Clipper::CLIP_FINITE)
1405 if(!renderer->clipper->clip(polygon, clipFlagsOr, draw))
1411 if(setupRoutine(primitive, triangle, &polygon, data))
1422 int Renderer::setupWireframeTriangle(Renderer *renderer, int unit, int count)
1424 Triangle *triangle = renderer->triangleBatch[unit];
1425 Primitive *primitive = renderer->primitiveBatch[unit];
1428 DrawCall &draw = *renderer->drawList[renderer->primitiveProgress[unit].drawCall % DRAW_COUNT];
1429 SetupProcessor::State &state = draw.setupState;
1430 SetupProcessor::RoutinePointer setupRoutine = draw.setupPointer;
1432 const Vertex &v0 = triangle[0].v0;
1433 const Vertex &v1 = triangle[0].v1;
1434 const Vertex &v2 = triangle[0].v2;
1436 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;
1438 if(state.cullMode == CULL_CLOCKWISE)
1440 if(d >= 0) return 0;
1442 else if(state.cullMode == CULL_COUNTERCLOCKWISE)
1444 if(d <= 0) return 0;
1448 triangle[1].v0 = v1;
1449 triangle[1].v1 = v2;
1450 triangle[2].v0 = v2;
1451 triangle[2].v1 = v0;
1453 if(state.color[0][0].flat) // FIXME
1455 for(int i = 0; i < 2; i++)
1457 triangle[1].v0.C[i] = triangle[0].v0.C[i];
1458 triangle[1].v1.C[i] = triangle[0].v0.C[i];
1459 triangle[2].v0.C[i] = triangle[0].v0.C[i];
1460 triangle[2].v1.C[i] = triangle[0].v0.C[i];
1464 for(int i = 0; i < 3; i++)
1466 if(setupLine(renderer, *primitive, *triangle, draw))
1468 primitive->area = 0.5f * d;
1480 int Renderer::setupVertexTriangle(Renderer *renderer, int unit, int count)
1482 Triangle *triangle = renderer->triangleBatch[unit];
1483 Primitive *primitive = renderer->primitiveBatch[unit];
1486 DrawCall &draw = *renderer->drawList[renderer->primitiveProgress[unit].drawCall % DRAW_COUNT];
1487 SetupProcessor::State &state = draw.setupState;
1489 const Vertex &v0 = triangle[0].v0;
1490 const Vertex &v1 = triangle[0].v1;
1491 const Vertex &v2 = triangle[0].v2;
1493 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;
1495 if(state.cullMode == CULL_CLOCKWISE)
1497 if(d >= 0) return 0;
1499 else if(state.cullMode == CULL_COUNTERCLOCKWISE)
1501 if(d <= 0) return 0;
1505 triangle[1].v0 = v1;
1506 triangle[2].v0 = v2;
1508 for(int i = 0; i < 3; i++)
1510 if(setupPoint(renderer, *primitive, *triangle, draw))
1512 primitive->area = 0.5f * d;
1524 int Renderer::setupLines(Renderer *renderer, int unit, int count)
1526 Triangle *triangle = renderer->triangleBatch[unit];
1527 Primitive *primitive = renderer->primitiveBatch[unit];
1530 DrawCall &draw = *renderer->drawList[renderer->primitiveProgress[unit].drawCall % DRAW_COUNT];
1531 SetupProcessor::State &state = draw.setupState;
1533 int ms = state.multiSample;
1535 for(int i = 0; i < count; i++)
1537 if(setupLine(renderer, *primitive, *triangle, draw))
1549 int Renderer::setupPoints(Renderer *renderer, int unit, int count)
1551 Triangle *triangle = renderer->triangleBatch[unit];
1552 Primitive *primitive = renderer->primitiveBatch[unit];
1555 DrawCall &draw = *renderer->drawList[renderer->primitiveProgress[unit].drawCall % DRAW_COUNT];
1556 SetupProcessor::State &state = draw.setupState;
1558 int ms = state.multiSample;
1560 for(int i = 0; i < count; i++)
1562 if(setupPoint(renderer, *primitive, *triangle, draw))
1574 bool Renderer::setupLine(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw)
1576 const SetupProcessor::RoutinePointer &setupRoutine = draw.setupPointer;
1577 const SetupProcessor::State &state = draw.setupState;
1578 const DrawData &data = *draw.data;
1580 float lineWidth = data.lineWidth;
1582 Vertex &v0 = triangle.v0;
1583 Vertex &v1 = triangle.v1;
1585 int pos = state.positionRegister;
1587 const float4 &P0 = v0.v[pos];
1588 const float4 &P1 = v1.v[pos];
1590 if(P0.w <= 0 && P1.w <= 0)
1595 const float W = data.Wx16[0] * (1.0f / 16.0f);
1596 const float H = data.Hx16[0] * (1.0f / 16.0f);
1598 float dx = W * (P1.x / P1.w - P0.x / P0.w);
1599 float dy = H * (P1.y / P1.w - P0.y / P0.w);
1601 if(dx == 0 && dy == 0)
1606 if(false) // Rectangle
1616 float scale = lineWidth * 0.5f / sqrt(dx*dx + dy*dy);
1621 float dx0w = dx * P0.w / W;
1622 float dy0h = dy * P0.w / H;
1623 float dx0h = dx * P0.w / H;
1624 float dy0w = dy * P0.w / W;
1626 float dx1w = dx * P1.w / W;
1627 float dy1h = dy * P1.w / H;
1628 float dx1h = dx * P1.w / H;
1629 float dy1w = dy * P1.w / W;
1631 P[0].x += -dy0w + -dx0w;
1632 P[0].y += -dx0h + +dy0h;
1633 C[0] = computeClipFlags(P[0], data);
1635 P[1].x += -dy1w + +dx1w;
1636 P[1].y += -dx1h + +dy1h;
1637 C[1] = computeClipFlags(P[1], data);
1639 P[2].x += +dy1w + +dx1w;
1640 P[2].y += +dx1h + -dy1h;
1641 C[2] = computeClipFlags(P[2], data);
1643 P[3].x += +dy0w + -dx0w;
1644 P[3].y += +dx0h + +dy0h;
1645 C[3] = computeClipFlags(P[3], data);
1647 if((C[0] & C[1] & C[2] & C[3]) == Clipper::CLIP_FINITE)
1649 Polygon polygon(P, 4);
1651 int clipFlagsOr = C[0] | C[1] | C[2] | C[3] | draw.clipFlags;
1653 if(clipFlagsOr != Clipper::CLIP_FINITE)
1655 if(!renderer->clipper->clip(polygon, clipFlagsOr, draw))
1661 return setupRoutine(&primitive, &triangle, &polygon, &data);
1664 else // Diamond test convention
1678 float dx0 = lineWidth * 0.5f * P0.w / W;
1679 float dy0 = lineWidth * 0.5f * P0.w / H;
1681 float dx1 = lineWidth * 0.5f * P1.w / W;
1682 float dy1 = lineWidth * 0.5f * P1.w / H;
1685 C[0] = computeClipFlags(P[0], data);
1688 C[1] = computeClipFlags(P[1], data);
1691 C[2] = computeClipFlags(P[2], data);
1694 C[3] = computeClipFlags(P[3], data);
1697 C[4] = computeClipFlags(P[4], data);
1700 C[5] = computeClipFlags(P[5], data);
1703 C[6] = computeClipFlags(P[6], data);
1706 C[7] = computeClipFlags(P[7], data);
1708 if((C[0] & C[1] & C[2] & C[3] & C[4] & C[5] & C[6] & C[7]) == Clipper::CLIP_FINITE)
1714 if(dx > dy) // Right
1755 Polygon polygon(L, 6);
1757 int clipFlagsOr = C[0] | C[1] | C[2] | C[3] | C[4] | C[5] | C[6] | C[7] | draw.clipFlags;
1759 if(clipFlagsOr != Clipper::CLIP_FINITE)
1761 if(!renderer->clipper->clip(polygon, clipFlagsOr, draw))
1767 return setupRoutine(&primitive, &triangle, &polygon, &data);
1774 bool Renderer::setupPoint(Renderer *renderer, Primitive &primitive, Triangle &triangle, const DrawCall &draw)
1776 const SetupProcessor::RoutinePointer &setupRoutine = draw.setupPointer;
1777 const SetupProcessor::State &state = draw.setupState;
1778 const DrawData &data = *draw.data;
1780 Vertex &v = triangle.v0;
1784 int pts = state.pointSizeRegister;
1786 if(state.pointSizeRegister != 0xF)
1792 pSize = data.point.pointSize[0];
1795 pSize = clamp(pSize, data.point.pointSizeMin, data.point.pointSizeMax);
1800 int pos = state.positionRegister;
1807 const float X = pSize * P[0].w * data.halfPixelX[0];
1808 const float Y = pSize * P[0].w * data.halfPixelY[0];
1812 C[0] = computeClipFlags(P[0], data);
1816 C[1] = computeClipFlags(P[1], data);
1820 C[2] = computeClipFlags(P[2], data);
1824 C[3] = computeClipFlags(P[3], data);
1826 triangle.v1 = triangle.v0;
1827 triangle.v2 = triangle.v0;
1829 triangle.v1.X += iround(16 * 0.5f * pSize);
1830 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
1832 Polygon polygon(P, 4);
1834 if((C[0] & C[1] & C[2] & C[3]) == Clipper::CLIP_FINITE)
1836 int clipFlagsOr = C[0] | C[1] | C[2] | C[3] | draw.clipFlags;
1838 if(clipFlagsOr != Clipper::CLIP_FINITE)
1840 if(!renderer->clipper->clip(polygon, clipFlagsOr, draw))
1846 return setupRoutine(&primitive, &triangle, &polygon, &data);
1852 unsigned int Renderer::computeClipFlags(const float4 &v, const DrawData &data)
1854 float clX = v.x + data.halfPixelX[0] * v.w;
1855 float clY = v.y + data.halfPixelY[0] * v.w;
1857 return ((clX > v.w) << 0) |
1858 ((clY > v.w) << 1) |
1859 ((v.z > v.w) << 2) |
1860 ((clX < -v.w) << 3) |
1861 ((clY < -v.w) << 4) |
1863 Clipper::CLIP_FINITE; // FIXME: xyz finite
1866 void Renderer::initializeThreads()
1868 unitCount = ceilPow2(threadCount);
1869 clusterCount = ceilPow2(threadCount);
1871 for(int i = 0; i < unitCount; i++)
1873 triangleBatch[i] = (Triangle*)allocate(batchSize * sizeof(Triangle));
1874 primitiveBatch[i] = (Primitive*)allocate(batchSize * sizeof(Primitive));
1877 for(int i = 0; i < threadCount; i++)
1879 vertexTask[i] = (VertexTask*)allocate(sizeof(VertexTask));
1880 vertexTask[i]->vertexCache.drawCall = -1;
1882 task[i].type = Task::SUSPEND;
1884 resume[i] = new Event();
1885 suspend[i] = new Event();
1887 Parameters parameters;
1888 parameters.threadIndex = i;
1889 parameters.renderer = this;
1891 exitThreads = false;
1892 worker[i] = new Thread(threadFunction, ¶meters);
1895 suspend[i]->signal();
1899 void Renderer::terminateThreads()
1901 while(threadsAwake != 0)
1906 for(int thread = 0; thread < threadCount; thread++)
1911 resume[thread]->signal();
1912 worker[thread]->join();
1914 delete worker[thread];
1916 delete resume[thread];
1918 delete suspend[thread];
1919 suspend[thread] = 0;
1922 deallocate(vertexTask[thread]);
1923 vertexTask[thread] = 0;
1926 for(int i = 0; i < 16; i++)
1928 deallocate(triangleBatch[i]);
1929 triangleBatch[i] = 0;
1931 deallocate(primitiveBatch[i]);
1932 primitiveBatch[i] = 0;
1936 void Renderer::loadConstants(const VertexShader *vertexShader)
1938 if(!vertexShader) return;
1940 size_t count = vertexShader->getLength();
1942 for(size_t i = 0; i < count; i++)
1944 const Shader::Instruction *instruction = vertexShader->getInstruction(i);
1946 if(instruction->opcode == Shader::OPCODE_DEF)
1948 int index = instruction->dst.index;
1951 value[0] = instruction->src[0].value[0];
1952 value[1] = instruction->src[0].value[1];
1953 value[2] = instruction->src[0].value[2];
1954 value[3] = instruction->src[0].value[3];
1956 setVertexShaderConstantF(index, value);
1958 else if(instruction->opcode == Shader::OPCODE_DEFI)
1960 int index = instruction->dst.index;
1963 integer[0] = instruction->src[0].integer[0];
1964 integer[1] = instruction->src[0].integer[1];
1965 integer[2] = instruction->src[0].integer[2];
1966 integer[3] = instruction->src[0].integer[3];
1968 setVertexShaderConstantI(index, integer);
1970 else if(instruction->opcode == Shader::OPCODE_DEFB)
1972 int index = instruction->dst.index;
1973 int boolean = instruction->src[0].boolean[0];
1975 setVertexShaderConstantB(index, &boolean);
1980 void Renderer::loadConstants(const PixelShader *pixelShader)
1982 if(!pixelShader) return;
1984 size_t count = pixelShader->getLength();
1986 for(size_t i = 0; i < count; i++)
1988 const Shader::Instruction *instruction = pixelShader->getInstruction(i);
1990 if(instruction->opcode == Shader::OPCODE_DEF)
1992 int index = instruction->dst.index;
1995 value[0] = instruction->src[0].value[0];
1996 value[1] = instruction->src[0].value[1];
1997 value[2] = instruction->src[0].value[2];
1998 value[3] = instruction->src[0].value[3];
2000 setPixelShaderConstantF(index, value);
2002 else if(instruction->opcode == Shader::OPCODE_DEFI)
2004 int index = instruction->dst.index;
2007 integer[0] = instruction->src[0].integer[0];
2008 integer[1] = instruction->src[0].integer[1];
2009 integer[2] = instruction->src[0].integer[2];
2010 integer[3] = instruction->src[0].integer[3];
2012 setPixelShaderConstantI(index, integer);
2014 else if(instruction->opcode == Shader::OPCODE_DEFB)
2016 int index = instruction->dst.index;
2017 int boolean = instruction->src[0].boolean[0];
2019 setPixelShaderConstantB(index, &boolean);
2024 void Renderer::setIndexBuffer(Resource *indexBuffer)
2026 context->indexBuffer = indexBuffer;
2029 void Renderer::setMultiSampleMask(unsigned int mask)
2031 context->sampleMask = mask;
2034 void Renderer::setTransparencyAntialiasing(TransparencyAntialiasing transparencyAntialiasing)
2036 sw::transparencyAntialiasing = transparencyAntialiasing;
2039 bool Renderer::isReadWriteTexture(int sampler)
2041 for(int index = 0; index < 4; index++)
2043 if(context->renderTarget[index] && context->texture[sampler] == context->renderTarget[index]->getResource())
2049 if(context->depthStencil && context->texture[sampler] == context->depthStencil->getResource())
2057 void Renderer::updateClipper()
2059 if(updateClipPlanes)
2061 if(VertexProcessor::isFixedFunction()) // User plane in world space
2063 const Matrix &scissorWorld = getViewTransform();
2065 if(clipFlags & Clipper::CLIP_PLANE0) clipPlane[0] = scissorWorld * userPlane[0];
2066 if(clipFlags & Clipper::CLIP_PLANE1) clipPlane[1] = scissorWorld * userPlane[1];
2067 if(clipFlags & Clipper::CLIP_PLANE2) clipPlane[2] = scissorWorld * userPlane[2];
2068 if(clipFlags & Clipper::CLIP_PLANE3) clipPlane[3] = scissorWorld * userPlane[3];
2069 if(clipFlags & Clipper::CLIP_PLANE4) clipPlane[4] = scissorWorld * userPlane[4];
2070 if(clipFlags & Clipper::CLIP_PLANE5) clipPlane[5] = scissorWorld * userPlane[5];
2072 else // User plane in clip space
2074 if(clipFlags & Clipper::CLIP_PLANE0) clipPlane[0] = userPlane[0];
2075 if(clipFlags & Clipper::CLIP_PLANE1) clipPlane[1] = userPlane[1];
2076 if(clipFlags & Clipper::CLIP_PLANE2) clipPlane[2] = userPlane[2];
2077 if(clipFlags & Clipper::CLIP_PLANE3) clipPlane[3] = userPlane[3];
2078 if(clipFlags & Clipper::CLIP_PLANE4) clipPlane[4] = userPlane[4];
2079 if(clipFlags & Clipper::CLIP_PLANE5) clipPlane[5] = userPlane[5];
2082 updateClipPlanes = false;
2086 void Renderer::setTextureResource(unsigned int sampler, Resource *resource)
2088 ASSERT(sampler < TOTAL_IMAGE_UNITS);
2090 context->texture[sampler] = resource;
2093 void Renderer::setTextureLevel(unsigned int sampler, unsigned int face, unsigned int level, Surface *surface, TextureType type)
2095 ASSERT(sampler < TOTAL_IMAGE_UNITS && face < 6 && level < MIPMAP_LEVELS);
2097 context->sampler[sampler].setTextureLevel(face, level, surface, type);
2100 void Renderer::setTextureFilter(SamplerType type, int sampler, FilterType textureFilter)
2102 if(type == SAMPLER_PIXEL)
2104 PixelProcessor::setTextureFilter(sampler, textureFilter);
2108 VertexProcessor::setTextureFilter(sampler, textureFilter);
2112 void Renderer::setMipmapFilter(SamplerType type, int sampler, MipmapType mipmapFilter)
2114 if(type == SAMPLER_PIXEL)
2116 PixelProcessor::setMipmapFilter(sampler, mipmapFilter);
2120 VertexProcessor::setMipmapFilter(sampler, mipmapFilter);
2124 void Renderer::setGatherEnable(SamplerType type, int sampler, bool enable)
2126 if(type == SAMPLER_PIXEL)
2128 PixelProcessor::setGatherEnable(sampler, enable);
2132 VertexProcessor::setGatherEnable(sampler, enable);
2136 void Renderer::setAddressingModeU(SamplerType type, int sampler, AddressingMode addressMode)
2138 if(type == SAMPLER_PIXEL)
2140 PixelProcessor::setAddressingModeU(sampler, addressMode);
2144 VertexProcessor::setAddressingModeU(sampler, addressMode);
2148 void Renderer::setAddressingModeV(SamplerType type, int sampler, AddressingMode addressMode)
2150 if(type == SAMPLER_PIXEL)
2152 PixelProcessor::setAddressingModeV(sampler, addressMode);
2156 VertexProcessor::setAddressingModeV(sampler, addressMode);
2160 void Renderer::setAddressingModeW(SamplerType type, int sampler, AddressingMode addressMode)
2162 if(type == SAMPLER_PIXEL)
2164 PixelProcessor::setAddressingModeW(sampler, addressMode);
2168 VertexProcessor::setAddressingModeW(sampler, addressMode);
2172 void Renderer::setReadSRGB(SamplerType type, int sampler, bool sRGB)
2174 if(type == SAMPLER_PIXEL)
2176 PixelProcessor::setReadSRGB(sampler, sRGB);
2180 VertexProcessor::setReadSRGB(sampler, sRGB);
2184 void Renderer::setMipmapLOD(SamplerType type, int sampler, float bias)
2186 if(type == SAMPLER_PIXEL)
2188 PixelProcessor::setMipmapLOD(sampler, bias);
2192 VertexProcessor::setMipmapLOD(sampler, bias);
2196 void Renderer::setBorderColor(SamplerType type, int sampler, const Color<float> &borderColor)
2198 if(type == SAMPLER_PIXEL)
2200 PixelProcessor::setBorderColor(sampler, borderColor);
2204 VertexProcessor::setBorderColor(sampler, borderColor);
2208 void Renderer::setMaxAnisotropy(SamplerType type, int sampler, float maxAnisotropy)
2210 if(type == SAMPLER_PIXEL)
2212 PixelProcessor::setMaxAnisotropy(sampler, maxAnisotropy);
2216 VertexProcessor::setMaxAnisotropy(sampler, maxAnisotropy);
2220 void Renderer::setPointSpriteEnable(bool pointSpriteEnable)
2222 context->setPointSpriteEnable(pointSpriteEnable);
2225 void Renderer::setPointScaleEnable(bool pointScaleEnable)
2227 context->setPointScaleEnable(pointScaleEnable);
2230 void Renderer::setLineWidth(float width)
2232 context->lineWidth = width;
2235 void Renderer::setDepthBias(float bias)
2240 void Renderer::setSlopeDepthBias(float slopeBias)
2242 slopeDepthBias = slopeBias;
2245 void Renderer::setPixelShader(const PixelShader *shader)
2247 context->pixelShader = shader;
2249 loadConstants(shader);
2252 void Renderer::setVertexShader(const VertexShader *shader)
2254 context->vertexShader = shader;
2256 loadConstants(shader);
2259 void Renderer::setPixelShaderConstantF(int index, const float value[4], int count)
2261 for(int i = 0; i < DRAW_COUNT; i++)
2263 if(drawCall[i]->psDirtyConstF < index + count)
2265 drawCall[i]->psDirtyConstF = index + count;
2269 for(int i = 0; i < count; i++)
2271 PixelProcessor::setFloatConstant(index + i, value);
2276 void Renderer::setPixelShaderConstantI(int index, const int value[4], int count)
2278 for(int i = 0; i < DRAW_COUNT; i++)
2280 if(drawCall[i]->psDirtyConstI < index + count)
2282 drawCall[i]->psDirtyConstI = index + count;
2286 for(int i = 0; i < count; i++)
2288 PixelProcessor::setIntegerConstant(index + i, value);
2293 void Renderer::setPixelShaderConstantB(int index, const int *boolean, int count)
2295 for(int i = 0; i < DRAW_COUNT; i++)
2297 if(drawCall[i]->psDirtyConstB < index + count)
2299 drawCall[i]->psDirtyConstB = index + count;
2303 for(int i = 0; i < count; i++)
2305 PixelProcessor::setBooleanConstant(index + i, *boolean);
2310 void Renderer::setVertexShaderConstantF(int index, const float value[4], int count)
2312 for(int i = 0; i < DRAW_COUNT; i++)
2314 if(drawCall[i]->vsDirtyConstF < index + count)
2316 drawCall[i]->vsDirtyConstF = index + count;
2320 for(int i = 0; i < count; i++)
2322 VertexProcessor::setFloatConstant(index + i, value);
2327 void Renderer::setVertexShaderConstantI(int index, const int value[4], int count)
2329 for(int i = 0; i < DRAW_COUNT; i++)
2331 if(drawCall[i]->vsDirtyConstI < index + count)
2333 drawCall[i]->vsDirtyConstI = index + count;
2337 for(int i = 0; i < count; i++)
2339 VertexProcessor::setIntegerConstant(index + i, value);
2344 void Renderer::setVertexShaderConstantB(int index, const int *boolean, int count)
2346 for(int i = 0; i < DRAW_COUNT; i++)
2348 if(drawCall[i]->vsDirtyConstB < index + count)
2350 drawCall[i]->vsDirtyConstB = index + count;
2354 for(int i = 0; i < count; i++)
2356 VertexProcessor::setBooleanConstant(index + i, *boolean);
2361 void Renderer::setModelMatrix(const Matrix &M, int i)
2363 VertexProcessor::setModelMatrix(M, i);
2366 void Renderer::setViewMatrix(const Matrix &V)
2368 VertexProcessor::setViewMatrix(V);
2369 updateClipPlanes = true;
2372 void Renderer::setBaseMatrix(const Matrix &B)
2374 VertexProcessor::setBaseMatrix(B);
2375 updateClipPlanes = true;
2378 void Renderer::setProjectionMatrix(const Matrix &P)
2380 VertexProcessor::setProjectionMatrix(P);
2381 updateClipPlanes = true;
2384 void Renderer::addQuery(Query *query)
2386 queries.push_back(query);
2389 void Renderer::removeQuery(Query *query)
2391 queries.remove(query);
2395 int Renderer::getThreadCount()
2400 int64_t Renderer::getVertexTime(int thread)
2402 return vertexTime[thread];
2405 int64_t Renderer::getSetupTime(int thread)
2407 return setupTime[thread];
2410 int64_t Renderer::getPixelTime(int thread)
2412 return pixelTime[thread];
2415 void Renderer::resetTimers()
2417 for(int thread = 0; thread < threadCount; thread++)
2419 vertexTime[thread] = 0;
2420 setupTime[thread] = 0;
2421 pixelTime[thread] = 0;
2426 void Renderer::setViewport(const Viewport &viewport)
2428 this->viewport = viewport;
2431 void Renderer::setScissor(const Rect &scissor)
2433 this->scissor = scissor;
2436 void Renderer::setClipFlags(int flags)
2438 clipFlags = flags << 8; // Bottom 8 bits used by legacy frustum
2441 void Renderer::setClipPlane(unsigned int index, const float plane[4])
2445 userPlane[index] = plane;
2449 updateClipPlanes = true;
2452 void Renderer::updateConfiguration(bool initialUpdate)
2454 bool newConfiguration = swiftConfig->hasNewConfiguration();
2456 if(newConfiguration || initialUpdate)
2460 SwiftConfig::Configuration configuration = {0};
2461 swiftConfig->getConfiguration(configuration);
2463 precacheVertex = !newConfiguration && configuration.precache;
2464 precacheSetup = !newConfiguration && configuration.precache;
2465 precachePixel = !newConfiguration && configuration.precache;
2467 VertexProcessor::setRoutineCacheSize(configuration.vertexRoutineCacheSize);
2468 PixelProcessor::setRoutineCacheSize(configuration.pixelRoutineCacheSize);
2469 SetupProcessor::setRoutineCacheSize(configuration.setupRoutineCacheSize);
2471 switch(configuration.textureSampleQuality)
2473 case 0: Sampler::setFilterQuality(FILTER_POINT); break;
2474 case 1: Sampler::setFilterQuality(FILTER_LINEAR); break;
2475 case 2: Sampler::setFilterQuality(FILTER_ANISOTROPIC); break;
2476 default: Sampler::setFilterQuality(FILTER_ANISOTROPIC); break;
2479 switch(configuration.mipmapQuality)
2481 case 0: Sampler::setMipmapQuality(MIPMAP_POINT); break;
2482 case 1: Sampler::setMipmapQuality(MIPMAP_LINEAR); break;
2483 default: Sampler::setMipmapQuality(MIPMAP_LINEAR); break;
2486 setPerspectiveCorrection(configuration.perspectiveCorrection);
2488 switch(configuration.transcendentalPrecision)
2491 logPrecision = APPROXIMATE;
2492 expPrecision = APPROXIMATE;
2493 rcpPrecision = APPROXIMATE;
2494 rsqPrecision = APPROXIMATE;
2497 logPrecision = PARTIAL;
2498 expPrecision = PARTIAL;
2499 rcpPrecision = PARTIAL;
2500 rsqPrecision = PARTIAL;
2503 logPrecision = ACCURATE;
2504 expPrecision = ACCURATE;
2505 rcpPrecision = ACCURATE;
2506 rsqPrecision = ACCURATE;
2509 logPrecision = WHQL;
2510 expPrecision = WHQL;
2511 rcpPrecision = WHQL;
2512 rsqPrecision = WHQL;
2515 logPrecision = IEEE;
2516 expPrecision = IEEE;
2517 rcpPrecision = IEEE;
2518 rsqPrecision = IEEE;
2521 logPrecision = ACCURATE;
2522 expPrecision = ACCURATE;
2523 rcpPrecision = ACCURATE;
2524 rsqPrecision = ACCURATE;
2528 switch(configuration.transparencyAntialiasing)
2530 case 0: transparencyAntialiasing = TRANSPARENCY_NONE; break;
2531 case 1: transparencyAntialiasing = TRANSPARENCY_ALPHA_TO_COVERAGE; break;
2532 default: transparencyAntialiasing = TRANSPARENCY_NONE; break;
2535 switch(configuration.threadCount)
2537 case -1: threadCount = CPUID::coreCount(); break;
2538 case 0: threadCount = CPUID::processAffinity(); break;
2539 default: threadCount = configuration.threadCount; break;
2542 CPUID::setEnableSSE4_1(configuration.enableSSE4_1);
2543 CPUID::setEnableSSSE3(configuration.enableSSSE3);
2544 CPUID::setEnableSSE3(configuration.enableSSE3);
2545 CPUID::setEnableSSE2(configuration.enableSSE2);
2546 CPUID::setEnableSSE(configuration.enableSSE);
2548 for(int pass = 0; pass < 10; pass++)
2550 optimization[pass] = configuration.optimization[pass];
2553 forceWindowed = configuration.forceWindowed;
2554 complementaryDepthBuffer = configuration.complementaryDepthBuffer;
2555 postBlendSRGB = configuration.postBlendSRGB;
2556 exactColorRounding = configuration.exactColorRounding;
2557 forceClearRegisters = configuration.forceClearRegisters;
2560 minPrimitives = configuration.minPrimitives;
2561 maxPrimitives = configuration.maxPrimitives;
2565 if(!initialUpdate && !worker[0])
2567 initializeThreads();