1 // SwiftShader Software Renderer
3 // Copyright(c) 2005-2013 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 "PixelProcessor.hpp"
14 #include "PixelPipeline.hpp"
15 #include "PixelProgram.hpp"
16 #include "PixelShader.hpp"
17 #include "MetaMacro.hpp"
18 #include "Surface.hpp"
19 #include "Primitive.hpp"
20 #include "Constants.hpp"
27 extern bool complementaryDepthBuffer;
28 extern TransparencyAntialiasing transparencyAntialiasing;
29 extern bool perspectiveCorrection;
31 bool precachePixel = false;
33 unsigned int PixelProcessor::States::computeHash()
35 unsigned int *state = (unsigned int*)this;
36 unsigned int hash = 0;
38 for(unsigned int i = 0; i < sizeof(States) / 4; i++)
46 PixelProcessor::State::State()
48 memset(this, 0, sizeof(State));
51 bool PixelProcessor::State::operator==(const State &state) const
53 if(hash != state.hash)
58 return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0;
61 PixelProcessor::PixelProcessor(Context *context) : context(context)
63 setGlobalMipmapBias(0.0f); // Round to highest LOD [0.5, 1.0]: -0.5
64 // Round to nearest LOD [0.7, 1.4]: 0.0
65 // Round to lowest LOD [1.0, 2.0]: 0.5
68 setRoutineCacheSize(1024);
71 PixelProcessor::~PixelProcessor()
77 void PixelProcessor::setFloatConstant(unsigned int index, const float value[4])
79 if(index < FRAGMENT_UNIFORM_VECTORS)
81 c[index][0] = value[0];
82 c[index][1] = value[1];
83 c[index][2] = value[2];
84 c[index][3] = value[3];
88 if(index < 8) // ps_1_x constants
90 // FIXME: Compact into generic function
91 short x = iround(4095 * clamp(value[0], -1.0f, 1.0f));
92 short y = iround(4095 * clamp(value[1], -1.0f, 1.0f));
93 short z = iround(4095 * clamp(value[2], -1.0f, 1.0f));
94 short w = iround(4095 * clamp(value[3], -1.0f, 1.0f));
118 void PixelProcessor::setIntegerConstant(unsigned int index, const int value[4])
122 i[index][0] = value[0];
123 i[index][1] = value[1];
124 i[index][2] = value[2];
125 i[index][3] = value[3];
130 void PixelProcessor::setBooleanConstant(unsigned int index, int boolean)
134 b[index] = boolean != 0;
139 void PixelProcessor::setRenderTarget(int index, Surface *renderTarget)
141 context->renderTarget[index] = renderTarget;
144 void PixelProcessor::setDepthStencil(Surface *depthStencil)
146 context->depthStencil = depthStencil;
149 void PixelProcessor::setTexCoordIndex(unsigned int stage, int texCoordIndex)
153 context->textureStage[stage].setTexCoordIndex(texCoordIndex);
158 void PixelProcessor::setStageOperation(unsigned int stage, TextureStage::StageOperation stageOperation)
162 context->textureStage[stage].setStageOperation(stageOperation);
167 void PixelProcessor::setFirstArgument(unsigned int stage, TextureStage::SourceArgument firstArgument)
171 context->textureStage[stage].setFirstArgument(firstArgument);
176 void PixelProcessor::setSecondArgument(unsigned int stage, TextureStage::SourceArgument secondArgument)
180 context->textureStage[stage].setSecondArgument(secondArgument);
185 void PixelProcessor::setThirdArgument(unsigned int stage, TextureStage::SourceArgument thirdArgument)
189 context->textureStage[stage].setThirdArgument(thirdArgument);
194 void PixelProcessor::setStageOperationAlpha(unsigned int stage, TextureStage::StageOperation stageOperationAlpha)
198 context->textureStage[stage].setStageOperationAlpha(stageOperationAlpha);
203 void PixelProcessor::setFirstArgumentAlpha(unsigned int stage, TextureStage::SourceArgument firstArgumentAlpha)
207 context->textureStage[stage].setFirstArgumentAlpha(firstArgumentAlpha);
212 void PixelProcessor::setSecondArgumentAlpha(unsigned int stage, TextureStage::SourceArgument secondArgumentAlpha)
216 context->textureStage[stage].setSecondArgumentAlpha(secondArgumentAlpha);
221 void PixelProcessor::setThirdArgumentAlpha(unsigned int stage, TextureStage::SourceArgument thirdArgumentAlpha)
225 context->textureStage[stage].setThirdArgumentAlpha(thirdArgumentAlpha);
230 void PixelProcessor::setFirstModifier(unsigned int stage, TextureStage::ArgumentModifier firstModifier)
234 context->textureStage[stage].setFirstModifier(firstModifier);
239 void PixelProcessor::setSecondModifier(unsigned int stage, TextureStage::ArgumentModifier secondModifier)
243 context->textureStage[stage].setSecondModifier(secondModifier);
248 void PixelProcessor::setThirdModifier(unsigned int stage, TextureStage::ArgumentModifier thirdModifier)
252 context->textureStage[stage].setThirdModifier(thirdModifier);
257 void PixelProcessor::setFirstModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier firstModifierAlpha)
261 context->textureStage[stage].setFirstModifierAlpha(firstModifierAlpha);
266 void PixelProcessor::setSecondModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier secondModifierAlpha)
270 context->textureStage[stage].setSecondModifierAlpha(secondModifierAlpha);
275 void PixelProcessor::setThirdModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier thirdModifierAlpha)
279 context->textureStage[stage].setThirdModifierAlpha(thirdModifierAlpha);
284 void PixelProcessor::setDestinationArgument(unsigned int stage, TextureStage::DestinationArgument destinationArgument)
288 context->textureStage[stage].setDestinationArgument(destinationArgument);
293 void PixelProcessor::setConstantColor(unsigned int stage, const Color<float> &constantColor)
297 context->textureStage[stage].setConstantColor(constantColor);
302 void PixelProcessor::setBumpmapMatrix(unsigned int stage, int element, float value)
306 context->textureStage[stage].setBumpmapMatrix(element, value);
311 void PixelProcessor::setLuminanceScale(unsigned int stage, float value)
315 context->textureStage[stage].setLuminanceScale(value);
320 void PixelProcessor::setLuminanceOffset(unsigned int stage, float value)
324 context->textureStage[stage].setLuminanceOffset(value);
329 void PixelProcessor::setTextureFilter(unsigned int sampler, FilterType textureFilter)
331 if(sampler < TEXTURE_IMAGE_UNITS)
333 context->sampler[sampler].setTextureFilter(textureFilter);
338 void PixelProcessor::setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter)
340 if(sampler < TEXTURE_IMAGE_UNITS)
342 context->sampler[sampler].setMipmapFilter(mipmapFilter);
347 void PixelProcessor::setGatherEnable(unsigned int sampler, bool enable)
349 if(sampler < TEXTURE_IMAGE_UNITS)
351 context->sampler[sampler].setGatherEnable(enable);
356 void PixelProcessor::setAddressingModeU(unsigned int sampler, AddressingMode addressMode)
358 if(sampler < TEXTURE_IMAGE_UNITS)
360 context->sampler[sampler].setAddressingModeU(addressMode);
365 void PixelProcessor::setAddressingModeV(unsigned int sampler, AddressingMode addressMode)
367 if(sampler < TEXTURE_IMAGE_UNITS)
369 context->sampler[sampler].setAddressingModeV(addressMode);
374 void PixelProcessor::setAddressingModeW(unsigned int sampler, AddressingMode addressMode)
376 if(sampler < TEXTURE_IMAGE_UNITS)
378 context->sampler[sampler].setAddressingModeW(addressMode);
383 void PixelProcessor::setReadSRGB(unsigned int sampler, bool sRGB)
385 if(sampler < TEXTURE_IMAGE_UNITS)
387 context->sampler[sampler].setReadSRGB(sRGB);
392 void PixelProcessor::setMipmapLOD(unsigned int sampler, float bias)
394 if(sampler < TEXTURE_IMAGE_UNITS)
396 context->sampler[sampler].setMipmapLOD(bias);
401 void PixelProcessor::setBorderColor(unsigned int sampler, const Color<float> &borderColor)
403 if(sampler < TEXTURE_IMAGE_UNITS)
405 context->sampler[sampler].setBorderColor(borderColor);
410 void PixelProcessor::setMaxAnisotropy(unsigned int sampler, float maxAnisotropy)
412 if(sampler < TEXTURE_IMAGE_UNITS)
414 context->sampler[sampler].setMaxAnisotropy(maxAnisotropy);
419 void PixelProcessor::setSwizzleR(unsigned int sampler, SwizzleType swizzleR)
421 if(sampler < TEXTURE_IMAGE_UNITS)
423 context->sampler[sampler].setSwizzleR(swizzleR);
428 void PixelProcessor::setSwizzleG(unsigned int sampler, SwizzleType swizzleG)
430 if(sampler < TEXTURE_IMAGE_UNITS)
432 context->sampler[sampler].setSwizzleG(swizzleG);
437 void PixelProcessor::setSwizzleB(unsigned int sampler, SwizzleType swizzleB)
439 if(sampler < TEXTURE_IMAGE_UNITS)
441 context->sampler[sampler].setSwizzleB(swizzleB);
446 void PixelProcessor::setSwizzleA(unsigned int sampler, SwizzleType swizzleA)
448 if(sampler < TEXTURE_IMAGE_UNITS)
450 context->sampler[sampler].setSwizzleA(swizzleA);
455 void PixelProcessor::setWriteSRGB(bool sRGB)
457 context->setWriteSRGB(sRGB);
460 void PixelProcessor::setColorLogicOpEnabled(bool colorLogicOpEnabled)
462 context->setColorLogicOpEnabled(colorLogicOpEnabled);
465 void PixelProcessor::setLogicalOperation(LogicalOperation logicalOperation)
467 context->setLogicalOperation(logicalOperation);
470 void PixelProcessor::setDepthBufferEnable(bool depthBufferEnable)
472 context->setDepthBufferEnable(depthBufferEnable);
475 void PixelProcessor::setDepthCompare(DepthCompareMode depthCompareMode)
477 context->depthCompareMode = depthCompareMode;
480 void PixelProcessor::setAlphaCompare(AlphaCompareMode alphaCompareMode)
482 context->alphaCompareMode = alphaCompareMode;
485 void PixelProcessor::setDepthWriteEnable(bool depthWriteEnable)
487 context->depthWriteEnable = depthWriteEnable;
490 void PixelProcessor::setAlphaTestEnable(bool alphaTestEnable)
492 context->alphaTestEnable = alphaTestEnable;
495 void PixelProcessor::setCullMode(CullMode cullMode)
497 context->cullMode = cullMode;
500 void PixelProcessor::setColorWriteMask(int index, int rgbaMask)
502 context->setColorWriteMask(index, rgbaMask);
505 void PixelProcessor::setStencilEnable(bool stencilEnable)
507 context->stencilEnable = stencilEnable;
510 void PixelProcessor::setStencilCompare(StencilCompareMode stencilCompareMode)
512 context->stencilCompareMode = stencilCompareMode;
515 void PixelProcessor::setStencilReference(int stencilReference)
517 context->stencilReference = stencilReference;
518 stencil.set(stencilReference, context->stencilMask, context->stencilWriteMask);
521 void PixelProcessor::setStencilReferenceCCW(int stencilReferenceCCW)
523 context->stencilReferenceCCW = stencilReferenceCCW;
524 stencilCCW.set(stencilReferenceCCW, context->stencilMaskCCW, context->stencilWriteMaskCCW);
527 void PixelProcessor::setStencilMask(int stencilMask)
529 context->stencilMask = stencilMask;
530 stencil.set(context->stencilReference, stencilMask, context->stencilWriteMask);
533 void PixelProcessor::setStencilMaskCCW(int stencilMaskCCW)
535 context->stencilMaskCCW = stencilMaskCCW;
536 stencilCCW.set(context->stencilReferenceCCW, stencilMaskCCW, context->stencilWriteMaskCCW);
539 void PixelProcessor::setStencilFailOperation(StencilOperation stencilFailOperation)
541 context->stencilFailOperation = stencilFailOperation;
544 void PixelProcessor::setStencilPassOperation(StencilOperation stencilPassOperation)
546 context->stencilPassOperation = stencilPassOperation;
549 void PixelProcessor::setStencilZFailOperation(StencilOperation stencilZFailOperation)
551 context->stencilZFailOperation = stencilZFailOperation;
554 void PixelProcessor::setStencilWriteMask(int stencilWriteMask)
556 context->stencilWriteMask = stencilWriteMask;
557 stencil.set(context->stencilReference, context->stencilMask, stencilWriteMask);
560 void PixelProcessor::setStencilWriteMaskCCW(int stencilWriteMaskCCW)
562 context->stencilWriteMaskCCW = stencilWriteMaskCCW;
563 stencilCCW.set(context->stencilReferenceCCW, context->stencilMaskCCW, stencilWriteMaskCCW);
566 void PixelProcessor::setTwoSidedStencil(bool enable)
568 context->twoSidedStencil = enable;
571 void PixelProcessor::setStencilCompareCCW(StencilCompareMode stencilCompareMode)
573 context->stencilCompareModeCCW = stencilCompareMode;
576 void PixelProcessor::setStencilFailOperationCCW(StencilOperation stencilFailOperation)
578 context->stencilFailOperationCCW = stencilFailOperation;
581 void PixelProcessor::setStencilPassOperationCCW(StencilOperation stencilPassOperation)
583 context->stencilPassOperationCCW = stencilPassOperation;
586 void PixelProcessor::setStencilZFailOperationCCW(StencilOperation stencilZFailOperation)
588 context->stencilZFailOperationCCW = stencilZFailOperation;
591 void PixelProcessor::setTextureFactor(const Color<float> &textureFactor)
593 // FIXME: Compact into generic function // FIXME: Clamp
594 short textureFactorR = iround(4095 * textureFactor.r);
595 short textureFactorG = iround(4095 * textureFactor.g);
596 short textureFactorB = iround(4095 * textureFactor.b);
597 short textureFactorA = iround(4095 * textureFactor.a);
599 factor.textureFactor4[0][0] = textureFactorR;
600 factor.textureFactor4[0][1] = textureFactorR;
601 factor.textureFactor4[0][2] = textureFactorR;
602 factor.textureFactor4[0][3] = textureFactorR;
604 factor.textureFactor4[1][0] = textureFactorG;
605 factor.textureFactor4[1][1] = textureFactorG;
606 factor.textureFactor4[1][2] = textureFactorG;
607 factor.textureFactor4[1][3] = textureFactorG;
609 factor.textureFactor4[2][0] = textureFactorB;
610 factor.textureFactor4[2][1] = textureFactorB;
611 factor.textureFactor4[2][2] = textureFactorB;
612 factor.textureFactor4[2][3] = textureFactorB;
614 factor.textureFactor4[3][0] = textureFactorA;
615 factor.textureFactor4[3][1] = textureFactorA;
616 factor.textureFactor4[3][2] = textureFactorA;
617 factor.textureFactor4[3][3] = textureFactorA;
620 void PixelProcessor::setBlendConstant(const Color<float> &blendConstant)
622 // FIXME: Compact into generic function // FIXME: Clamp
623 short blendConstantR = iround(65535 * blendConstant.r);
624 short blendConstantG = iround(65535 * blendConstant.g);
625 short blendConstantB = iround(65535 * blendConstant.b);
626 short blendConstantA = iround(65535 * blendConstant.a);
628 factor.blendConstant4W[0][0] = blendConstantR;
629 factor.blendConstant4W[0][1] = blendConstantR;
630 factor.blendConstant4W[0][2] = blendConstantR;
631 factor.blendConstant4W[0][3] = blendConstantR;
633 factor.blendConstant4W[1][0] = blendConstantG;
634 factor.blendConstant4W[1][1] = blendConstantG;
635 factor.blendConstant4W[1][2] = blendConstantG;
636 factor.blendConstant4W[1][3] = blendConstantG;
638 factor.blendConstant4W[2][0] = blendConstantB;
639 factor.blendConstant4W[2][1] = blendConstantB;
640 factor.blendConstant4W[2][2] = blendConstantB;
641 factor.blendConstant4W[2][3] = blendConstantB;
643 factor.blendConstant4W[3][0] = blendConstantA;
644 factor.blendConstant4W[3][1] = blendConstantA;
645 factor.blendConstant4W[3][2] = blendConstantA;
646 factor.blendConstant4W[3][3] = blendConstantA;
648 // FIXME: Compact into generic function // FIXME: Clamp
649 short invBlendConstantR = iround(65535 * (1 - blendConstant.r));
650 short invBlendConstantG = iround(65535 * (1 - blendConstant.g));
651 short invBlendConstantB = iround(65535 * (1 - blendConstant.b));
652 short invBlendConstantA = iround(65535 * (1 - blendConstant.a));
654 factor.invBlendConstant4W[0][0] = invBlendConstantR;
655 factor.invBlendConstant4W[0][1] = invBlendConstantR;
656 factor.invBlendConstant4W[0][2] = invBlendConstantR;
657 factor.invBlendConstant4W[0][3] = invBlendConstantR;
659 factor.invBlendConstant4W[1][0] = invBlendConstantG;
660 factor.invBlendConstant4W[1][1] = invBlendConstantG;
661 factor.invBlendConstant4W[1][2] = invBlendConstantG;
662 factor.invBlendConstant4W[1][3] = invBlendConstantG;
664 factor.invBlendConstant4W[2][0] = invBlendConstantB;
665 factor.invBlendConstant4W[2][1] = invBlendConstantB;
666 factor.invBlendConstant4W[2][2] = invBlendConstantB;
667 factor.invBlendConstant4W[2][3] = invBlendConstantB;
669 factor.invBlendConstant4W[3][0] = invBlendConstantA;
670 factor.invBlendConstant4W[3][1] = invBlendConstantA;
671 factor.invBlendConstant4W[3][2] = invBlendConstantA;
672 factor.invBlendConstant4W[3][3] = invBlendConstantA;
674 factor.blendConstant4F[0][0] = blendConstant.r;
675 factor.blendConstant4F[0][1] = blendConstant.r;
676 factor.blendConstant4F[0][2] = blendConstant.r;
677 factor.blendConstant4F[0][3] = blendConstant.r;
679 factor.blendConstant4F[1][0] = blendConstant.g;
680 factor.blendConstant4F[1][1] = blendConstant.g;
681 factor.blendConstant4F[1][2] = blendConstant.g;
682 factor.blendConstant4F[1][3] = blendConstant.g;
684 factor.blendConstant4F[2][0] = blendConstant.b;
685 factor.blendConstant4F[2][1] = blendConstant.b;
686 factor.blendConstant4F[2][2] = blendConstant.b;
687 factor.blendConstant4F[2][3] = blendConstant.b;
689 factor.blendConstant4F[3][0] = blendConstant.a;
690 factor.blendConstant4F[3][1] = blendConstant.a;
691 factor.blendConstant4F[3][2] = blendConstant.a;
692 factor.blendConstant4F[3][3] = blendConstant.a;
694 factor.invBlendConstant4F[0][0] = 1 - blendConstant.r;
695 factor.invBlendConstant4F[0][1] = 1 - blendConstant.r;
696 factor.invBlendConstant4F[0][2] = 1 - blendConstant.r;
697 factor.invBlendConstant4F[0][3] = 1 - blendConstant.r;
699 factor.invBlendConstant4F[1][0] = 1 - blendConstant.g;
700 factor.invBlendConstant4F[1][1] = 1 - blendConstant.g;
701 factor.invBlendConstant4F[1][2] = 1 - blendConstant.g;
702 factor.invBlendConstant4F[1][3] = 1 - blendConstant.g;
704 factor.invBlendConstant4F[2][0] = 1 - blendConstant.b;
705 factor.invBlendConstant4F[2][1] = 1 - blendConstant.b;
706 factor.invBlendConstant4F[2][2] = 1 - blendConstant.b;
707 factor.invBlendConstant4F[2][3] = 1 - blendConstant.b;
709 factor.invBlendConstant4F[3][0] = 1 - blendConstant.a;
710 factor.invBlendConstant4F[3][1] = 1 - blendConstant.a;
711 factor.invBlendConstant4F[3][2] = 1 - blendConstant.a;
712 factor.invBlendConstant4F[3][3] = 1 - blendConstant.a;
715 void PixelProcessor::setFillMode(FillMode fillMode)
717 context->fillMode = fillMode;
720 void PixelProcessor::setShadingMode(ShadingMode shadingMode)
722 context->shadingMode = shadingMode;
725 void PixelProcessor::setAlphaBlendEnable(bool alphaBlendEnable)
727 context->setAlphaBlendEnable(alphaBlendEnable);
730 void PixelProcessor::setSourceBlendFactor(BlendFactor sourceBlendFactor)
732 context->setSourceBlendFactor(sourceBlendFactor);
735 void PixelProcessor::setDestBlendFactor(BlendFactor destBlendFactor)
737 context->setDestBlendFactor(destBlendFactor);
740 void PixelProcessor::setBlendOperation(BlendOperation blendOperation)
742 context->setBlendOperation(blendOperation);
745 void PixelProcessor::setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable)
747 context->setSeparateAlphaBlendEnable(separateAlphaBlendEnable);
750 void PixelProcessor::setSourceBlendFactorAlpha(BlendFactor sourceBlendFactorAlpha)
752 context->setSourceBlendFactorAlpha(sourceBlendFactorAlpha);
755 void PixelProcessor::setDestBlendFactorAlpha(BlendFactor destBlendFactorAlpha)
757 context->setDestBlendFactorAlpha(destBlendFactorAlpha);
760 void PixelProcessor::setBlendOperationAlpha(BlendOperation blendOperationAlpha)
762 context->setBlendOperationAlpha(blendOperationAlpha);
765 void PixelProcessor::setAlphaReference(float alphaReference)
767 context->alphaReference = alphaReference;
769 factor.alphaReference4[0] = (word)iround(alphaReference * 0x1000 / 0xFF);
770 factor.alphaReference4[1] = (word)iround(alphaReference * 0x1000 / 0xFF);
771 factor.alphaReference4[2] = (word)iround(alphaReference * 0x1000 / 0xFF);
772 factor.alphaReference4[3] = (word)iround(alphaReference * 0x1000 / 0xFF);
775 void PixelProcessor::setGlobalMipmapBias(float bias)
777 context->setGlobalMipmapBias(bias);
780 void PixelProcessor::setFogStart(float start)
782 setFogRanges(start, context->fogEnd);
785 void PixelProcessor::setFogEnd(float end)
787 setFogRanges(context->fogStart, end);
790 void PixelProcessor::setFogColor(Color<float> fogColor)
792 // TODO: Compact into generic function
793 word fogR = (unsigned short)(65535 * fogColor.r);
794 word fogG = (unsigned short)(65535 * fogColor.g);
795 word fogB = (unsigned short)(65535 * fogColor.b);
797 fog.color4[0][0] = fogR;
798 fog.color4[0][1] = fogR;
799 fog.color4[0][2] = fogR;
800 fog.color4[0][3] = fogR;
802 fog.color4[1][0] = fogG;
803 fog.color4[1][1] = fogG;
804 fog.color4[1][2] = fogG;
805 fog.color4[1][3] = fogG;
807 fog.color4[2][0] = fogB;
808 fog.color4[2][1] = fogB;
809 fog.color4[2][2] = fogB;
810 fog.color4[2][3] = fogB;
812 fog.colorF[0] = replicate(fogColor.r);
813 fog.colorF[1] = replicate(fogColor.g);
814 fog.colorF[2] = replicate(fogColor.b);
817 void PixelProcessor::setFogDensity(float fogDensity)
819 fog.densityE = replicate(-fogDensity * 1.442695f); // 1/e^x = 2^(-x*1.44)
820 fog.density2E = replicate(-fogDensity * fogDensity * 1.442695f);
823 void PixelProcessor::setPixelFogMode(FogMode fogMode)
825 context->pixelFogMode = fogMode;
828 void PixelProcessor::setPerspectiveCorrection(bool perspectiveEnable)
830 perspectiveCorrection = perspectiveEnable;
833 void PixelProcessor::setOcclusionEnabled(bool enable)
835 context->occlusionEnabled = enable;
838 void PixelProcessor::setRoutineCacheSize(int cacheSize)
841 routineCache = new RoutineCache<State>(clamp(cacheSize, 1, 65536), precachePixel ? "sw-pixel" : 0);
844 void PixelProcessor::setFogRanges(float start, float end)
846 context->fogStart = start;
847 context->fogEnd = end;
851 end += 0.001f; // Hack: ensure there is a small range
854 float fogScale = -1.0f / (end - start);
855 float fogOffset = end * -fogScale;
857 fog.scale = replicate(fogScale);
858 fog.offset = replicate(fogOffset);
861 const PixelProcessor::State PixelProcessor::update() const
865 if(context->pixelShader)
867 state.shaderID = context->pixelShader->getSerialID();
874 state.depthOverride = context->pixelShader && context->pixelShader->depthOverride();
875 state.shaderContainsKill = context->pixelShader ? context->pixelShader->containsKill() : false;
877 if(context->alphaTestActive())
879 state.alphaCompareMode = context->alphaCompareMode;
881 state.transparencyAntialiasing = context->getMultiSampleCount() > 1 ? transparencyAntialiasing : TRANSPARENCY_NONE;
884 state.depthWriteEnable = context->depthWriteActive();
886 if(context->stencilActive())
888 state.stencilActive = true;
889 state.stencilCompareMode = context->stencilCompareMode;
890 state.stencilFailOperation = context->stencilFailOperation;
891 state.stencilPassOperation = context->stencilPassOperation;
892 state.stencilZFailOperation = context->stencilZFailOperation;
893 state.noStencilMask = (context->stencilMask == 0xFF);
894 state.noStencilWriteMask = (context->stencilWriteMask == 0xFF);
895 state.stencilWriteMasked = (context->stencilWriteMask == 0x00);
897 state.twoSidedStencil = context->twoSidedStencil;
898 state.stencilCompareModeCCW = context->twoSidedStencil ? context->stencilCompareModeCCW : state.stencilCompareMode;
899 state.stencilFailOperationCCW = context->twoSidedStencil ? context->stencilFailOperationCCW : state.stencilFailOperation;
900 state.stencilPassOperationCCW = context->twoSidedStencil ? context->stencilPassOperationCCW : state.stencilPassOperation;
901 state.stencilZFailOperationCCW = context->twoSidedStencil ? context->stencilZFailOperationCCW : state.stencilZFailOperation;
902 state.noStencilMaskCCW = context->twoSidedStencil ? (context->stencilMaskCCW == 0xFF) : state.noStencilMask;
903 state.noStencilWriteMaskCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0xFF) : state.noStencilWriteMask;
904 state.stencilWriteMaskedCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0x00) : state.stencilWriteMasked;
907 if(context->depthBufferActive())
909 state.depthTestActive = true;
910 state.depthCompareMode = context->depthCompareMode;
911 state.quadLayoutDepthBuffer = context->depthStencil->getInternalFormat() != FORMAT_D32F_LOCKABLE &&
912 context->depthStencil->getInternalFormat() != FORMAT_D32FS8_TEXTURE &&
913 context->depthStencil->getInternalFormat() != FORMAT_D32FS8_SHADOW;
916 state.occlusionEnabled = context->occlusionEnabled;
918 state.fogActive = context->fogActive();
919 state.pixelFogMode = context->pixelFogActive();
920 state.wBasedFog = context->wBasedFog && context->pixelFogActive() != FOG_NONE;
921 state.perspective = context->perspectiveActive();
923 if(context->alphaBlendActive())
925 state.alphaBlendActive = true;
926 state.sourceBlendFactor = context->sourceBlendFactor();
927 state.destBlendFactor = context->destBlendFactor();
928 state.blendOperation = context->blendOperation();
929 state.sourceBlendFactorAlpha = context->sourceBlendFactorAlpha();
930 state.destBlendFactorAlpha = context->destBlendFactorAlpha();
931 state.blendOperationAlpha = context->blendOperationAlpha();
934 state.logicalOperation = context->colorLogicOp();
936 state.colorWriteMask = (context->colorWriteActive(0) << 0) |
937 (context->colorWriteActive(1) << 4) |
938 (context->colorWriteActive(2) << 8) |
939 (context->colorWriteActive(3) << 12);
941 for(int i = 0; i < RENDERTARGETS; i++)
943 state.targetFormat[i] = context->renderTargetInternalFormat(i);
946 state.writeSRGB = context->writeSRGB && context->renderTarget[0] && Surface::isSRGBwritable(context->renderTarget[0]->getExternalFormat());
947 state.multiSample = context->getMultiSampleCount();
948 state.multiSampleMask = context->multiSampleMask;
950 if(state.multiSample > 1 && context->pixelShader)
952 state.centroid = context->pixelShader->containsCentroid();
955 if(!context->pixelShader)
957 for(unsigned int i = 0; i < 8; i++)
959 state.textureStage[i] = context->textureStage[i].textureStageState();
962 state.specularAdd = context->specularActive() && context->specularEnable;
965 for(unsigned int i = 0; i < 16; i++)
967 if(context->pixelShader)
969 if(context->pixelShader->usesSampler(i))
971 state.sampler[i] = context->sampler[i].samplerState();
976 if(i < 8 && state.textureStage[i].stageOperation != TextureStage::STAGE_DISABLE)
978 state.sampler[i] = context->sampler[i].samplerState();
984 const bool point = context->isDrawPoint(true);
985 const bool sprite = context->pointSpriteActive();
986 const bool flatShading = (context->shadingMode == SHADING_FLAT) || point;
988 if(context->pixelShaderVersion() < 0x0300)
990 for(int coordinate = 0; coordinate < 8; coordinate++)
992 for(int component = 0; component < 4; component++)
994 if(context->textureActive(coordinate, component))
996 state.texture[coordinate].component |= 1 << component;
1000 state.texture[coordinate].flat |= 1 << component;
1005 if(context->textureTransformProject[coordinate] && context->pixelShaderVersion() <= 0x0103)
1007 if(context->textureTransformCount[coordinate] == 2)
1009 state.texture[coordinate].project = 1;
1011 else if(context->textureTransformCount[coordinate] == 3)
1013 state.texture[coordinate].project = 2;
1015 else if(context->textureTransformCount[coordinate] == 4 || context->textureTransformCount[coordinate] == 0)
1017 state.texture[coordinate].project = 3;
1022 for(int color = 0; color < 2; color++)
1024 for(int component = 0; component < 4; component++)
1026 if(context->colorActive(color, component))
1028 state.color[color].component |= 1 << component;
1030 if(point || flatShading)
1032 state.color[color].flat |= 1 << component;
1038 if(context->fogActive())
1040 state.fog.component = true;
1044 state.fog.flat = true;
1050 for(int interpolant = 0; interpolant < 10; interpolant++)
1052 for(int component = 0; component < 4; component++)
1054 if(context->pixelShader->semantic[interpolant][component].active())
1058 switch(context->pixelShader->semantic[interpolant][component].usage)
1060 case Shader::USAGE_TEXCOORD: flat = point && !sprite; break;
1061 case Shader::USAGE_COLOR: flat = flatShading; break;
1064 state.interpolant[interpolant].component |= 1 << component;
1068 state.interpolant[interpolant].flat |= 1 << component;
1077 for(int interpolant = 0; interpolant < 10; interpolant++)
1079 for(int component = 0; component < 4; component++)
1081 state.interpolant[interpolant].centroid = context->pixelShader->semantic[interpolant][0].centroid;
1086 state.hash = state.computeHash();
1091 Routine *PixelProcessor::routine(const State &state)
1093 Routine *routine = routineCache->query(state);
1097 const bool integerPipeline = (context->pixelShaderVersion() <= 0x0104);
1098 Rasterizer *generator = nullptr;
1102 generator = new PixelPipeline(state, context->pixelShader);
1106 generator = new PixelProgram(state, context->pixelShader);
1109 generator->generate();
1110 routine = (*generator)(L"PixelRoutine_%0.8X", state.shaderID);
1113 routineCache->add(state, routine);