1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef sw_PixelProgram_hpp 16 #define sw_PixelProgram_hpp 17 18 #include "PixelRoutine.hpp" 19 #include "SamplerCore.hpp" 20 21 namespace sw 22 { 23 class PixelProgram : public PixelRoutine 24 { 25 public: 26 PixelProgram(const PixelProcessor::State &state, const PixelShader *shader) : 27 PixelRoutine(state, shader), r(shader->dynamicallyIndexedTemporaries), 28 loopDepth(-1), ifDepth(0), loopRepDepth(0), currentLabel(-1), whileTest(false) 29 { 30 for(int i = 0; i < 2048; ++i) 31 { 32 labelBlock[i] = 0; 33 } 34 35 enableStack[0] = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); 36 37 if(shader->containsBreakInstruction()) 38 { 39 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); 40 } 41 42 if(shader->containsContinueInstruction()) 43 { 44 enableContinue = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); 45 } 46 } 47 48 virtual ~PixelProgram() {} 49 50 protected: 51 virtual void setBuiltins(Int &x, Int &y, Float4(&z)[4], Float4 &w); 52 virtual void applyShader(Int cMask[4]); 53 virtual Bool alphaTest(Int cMask[4]); 54 virtual void rasterOperation(Float4 &fog, Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4]); 55 56 private: 57 // Temporary registers 58 RegisterArray<4096> r; 59 60 // Color outputs 61 Vector4f c[RENDERTARGETS]; 62 RegisterArray<RENDERTARGETS, true> oC; 63 64 // Shader variables 65 Vector4f vPos; 66 Vector4f vFace; 67 68 // DX9 specific variables 69 Vector4f p0; 70 Array<Int, 4> aL; 71 Array<Int, 4> increment; 72 Array<Int, 4> iteration; 73 74 Int loopDepth; // FIXME: Add support for switch 75 Int stackIndex; // FIXME: Inc/decrement callStack 76 Array<UInt, 16> callStack; 77 78 // Per pixel based on conditions reached 79 Int enableIndex; 80 Array<Int4, 1 + 24> enableStack; 81 Int4 enableBreak; 82 Int4 enableContinue; 83 Int4 enableLeave; 84 85 Vector4f sampleTexture(const Src &sampler, Vector4f &uvwq, Float4 &bias, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function); 86 Vector4f sampleTexture(int samplerIndex, Vector4f &uvwq, Float4 &bias, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function); 87 88 // Raster operations 89 void clampColor(Vector4f oC[RENDERTARGETS]); 90 91 Int4 enableMask(const Shader::Instruction *instruction); 92 93 Vector4f fetchRegister(const Src &src, unsigned int offset = 0); 94 Vector4f readConstant(const Src &src, unsigned int offset = 0); 95 RValue<Pointer<Byte>> uniformAddress(int bufferIndex, unsigned int index); 96 RValue<Pointer<Byte>> uniformAddress(int bufferIndex, unsigned int index, Int& offset); 97 Int relativeAddress(const Shader::Parameter &var, int bufferIndex = -1); 98 99 Float4 linearToSRGB(const Float4 &x); 100 101 // Instructions 102 typedef Shader::Control Control; 103 104 void M3X2(Vector4f &dst, Vector4f &src0, const Src &src1); 105 void M3X3(Vector4f &dst, Vector4f &src0, const Src &src1); 106 void M3X4(Vector4f &dst, Vector4f &src0, const Src &src1); 107 void M4X3(Vector4f &dst, Vector4f &src0, const Src &src1); 108 void M4X4(Vector4f &dst, Vector4f &src0, const Src &src1); 109 void TEX(Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias); 110 void TEXLOD(Vector4f &dst, Vector4f &src0, const Src &src1, Float4 &lod); 111 void TEXBIAS(Vector4f &dst, Vector4f &src0, const Src &src1, Float4 &bias); 112 void TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1); 113 void TEXKILL(Int cMask[4], Vector4f &src, unsigned char mask); 114 void TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &offset); 115 void TEXOFFSETBIAS(Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &offset, Float4 &bias); 116 void TEXLODOFFSET(Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &offset, Float4 &lod); 117 void TEXELFETCH(Vector4f &dst, Vector4f &src, const Src &, Float4 &lod); 118 void TEXELFETCHOFFSET(Vector4f &dst, Vector4f &src, const Src &, Vector4f &offset, Float4 &lod); 119 void TEXGRAD(Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &dsx, Vector4f &dsy); 120 void TEXGRADOFFSET(Vector4f &dst, Vector4f &src, const Src &, Vector4f &dsx, Vector4f &dsy, Vector4f &offset); 121 void DISCARD(Int cMask[4], const Shader::Instruction *instruction); 122 void DFDX(Vector4f &dst, Vector4f &src); 123 void DFDY(Vector4f &dst, Vector4f &src); 124 void FWIDTH(Vector4f &dst, Vector4f &src); 125 void BREAK(); 126 void BREAKC(Vector4f &src0, Vector4f &src1, Control); 127 void BREAKP(const Src &predicateRegister); 128 void BREAK(Int4 &condition); 129 void CONTINUE(); 130 void TEST(); 131 void CALL(int labelIndex, int callSiteIndex); 132 void CALLNZ(int labelIndex, int callSiteIndex, const Src &src); 133 void CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister); 134 void CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister); 135 void ELSE(); 136 void ENDIF(); 137 void ENDLOOP(); 138 void ENDREP(); 139 void ENDWHILE(); 140 void ENDSWITCH(); 141 void IF(const Src &src); 142 void IFb(const Src &boolRegister); 143 void IFp(const Src &predicateRegister); 144 void IFC(Vector4f &src0, Vector4f &src1, Control); 145 void IF(Int4 &condition); 146 void LABEL(int labelIndex); 147 void LOOP(const Src &integerRegister); 148 void REP(const Src &integerRegister); 149 void WHILE(const Src &temporaryRegister); 150 void SWITCH(); 151 void RET(); 152 void LEAVE(); 153 154 int ifDepth; 155 int loopRepDepth; 156 int currentLabel; 157 bool whileTest; 158 159 BasicBlock *ifFalseBlock[24 + 24]; 160 BasicBlock *loopRepTestBlock[4]; 161 BasicBlock *loopRepEndBlock[4]; 162 BasicBlock *labelBlock[2048]; 163 std::vector<BasicBlock*> callRetBlock[2048]; 164 BasicBlock *returnBlock; 165 bool isConditionalIf[24 + 24]; 166 }; 167 } 168 169 #endif 170