• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
PixelProgram(const PixelProcessor::State & state,const PixelShader * shader)26 		PixelProgram(const PixelProcessor::State &state, const PixelShader *shader) :
27 			PixelRoutine(state, shader), r(shader->indirectAddressableTemporaries)
28 		{
29 			for(int i = 0; i < MAX_SHADER_CALL_SITES; ++i)
30 			{
31 				labelBlock[i] = 0;
32 			}
33 
34 			loopDepth = -1;
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 
~PixelProgram()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<NUM_TEMPORARY_REGISTERS> 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, MAX_SHADER_NESTED_LOOPS> aL;
71 		Array<Int, MAX_SHADER_NESTED_LOOPS> increment;
72 		Array<Int, MAX_SHADER_NESTED_LOOPS> iteration;
73 
74 		Int loopDepth;    // FIXME: Add support for switch
75 		Int stackIndex;   // FIXME: Inc/decrement callStack
76 		Array<UInt, MAX_SHADER_CALL_STACK_SIZE> callStack;
77 
78 		// Per pixel based on conditions reached
79 		Int enableIndex;
80 		Array<Int4, MAX_SHADER_ENABLE_STACK_SIZE> 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::Relative &rel, int bufferIndex = -1);
98 		Int4 dynamicAddress(const Shader::Relative &rel);
99 
100 		Float4 linearToSRGB(const Float4 &x);
101 
102 		// Instructions
103 		typedef Shader::Control Control;
104 
105 		void M3X2(Vector4f &dst, Vector4f &src0, const Src &src1);
106 		void M3X3(Vector4f &dst, Vector4f &src0, const Src &src1);
107 		void M3X4(Vector4f &dst, Vector4f &src0, const Src &src1);
108 		void M4X3(Vector4f &dst, Vector4f &src0, const Src &src1);
109 		void M4X4(Vector4f &dst, Vector4f &src0, const Src &src1);
110 		void TEX(Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias);
111 		void TEXLOD(Vector4f &dst, Vector4f &src0, const Src &src1, Float4 &lod);
112 		void TEXBIAS(Vector4f &dst, Vector4f &src0, const Src &src1, Float4 &bias);
113 		void TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1);
114 		void TEXKILL(Int cMask[4], Vector4f &src, unsigned char mask);
115 		void TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &offset);
116 		void TEXOFFSETBIAS(Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &offset, Float4 &bias);
117 		void TEXLODOFFSET(Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &offset, Float4 &lod);
118 		void TEXELFETCH(Vector4f &dst, Vector4f &src, const Src &, Float4 &lod);
119 		void TEXELFETCHOFFSET(Vector4f &dst, Vector4f &src, const Src &, Vector4f &offset, Float4 &lod);
120 		void TEXGRAD(Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &dsx, Vector4f &dsy);
121 		void TEXGRADOFFSET(Vector4f &dst, Vector4f &src, const Src &, Vector4f &dsx, Vector4f &dsy, Vector4f &offset);
122 		void DISCARD(Int cMask[4], const Shader::Instruction *instruction);
123 		void DFDX(Vector4f &dst, Vector4f &src);
124 		void DFDY(Vector4f &dst, Vector4f &src);
125 		void FWIDTH(Vector4f &dst, Vector4f &src);
126 		void BREAK();
127 		void BREAKC(Vector4f &src0, Vector4f &src1, Control);
128 		void BREAKP(const Src &predicateRegister);
129 		void BREAK(Int4 &condition);
130 		void CONTINUE();
131 		void TEST();
132 		void SCALAR();
133 		void CALL(int labelIndex, int callSiteIndex);
134 		void CALLNZ(int labelIndex, int callSiteIndex, const Src &src);
135 		void CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister);
136 		void CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister);
137 		void ELSE();
138 		void ENDIF();
139 		void ENDLOOP();
140 		void ENDREP();
141 		void ENDWHILE();
142 		void ENDSWITCH();
143 		void IF(const Src &src);
144 		void IFb(const Src &boolRegister);
145 		void IFp(const Src &predicateRegister);
146 		void IFC(Vector4f &src0, Vector4f &src1, Control);
147 		void IF(Int4 &condition);
148 		void LABEL(int labelIndex);
149 		void LOOP(const Src &integerRegister);
150 		void REP(const Src &integerRegister);
151 		void WHILE(const Src &temporaryRegister);
152 		void SWITCH();
153 		void RET();
154 		void LEAVE();
155 
156 		BoundedIndex<MAX_SHADER_NESTED_IFS> ifDepth = 0;
157 		BoundedIndex<MAX_SHADER_NESTED_LOOPS> loopRepDepth = 0;
158 		BoundedIndex<MAX_SHADER_CALL_SITES> currentLabel = -1;
159 		bool scalar = false;
160 
161 		BasicBlock *ifFalseBlock[MAX_SHADER_NESTED_IFS];
162 		BasicBlock *loopRepTestBlock[MAX_SHADER_NESTED_LOOPS];
163 		BasicBlock *loopRepEndBlock[MAX_SHADER_NESTED_LOOPS];
164 		BasicBlock *labelBlock[MAX_SHADER_CALL_SITES];
165 		std::vector<BasicBlock*> callRetBlock[MAX_SHADER_CALL_SITES];
166 		BasicBlock *returnBlock;
167 		bool isConditionalIf[MAX_SHADER_NESTED_IFS];
168 		std::vector<Int4> restoreContinue;
169 	};
170 }
171 
172 #endif
173