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_VertexProgram_hpp
16 #define sw_VertexProgram_hpp
17 
18 #include "VertexRoutine.hpp"
19 #include "ShaderCore.hpp"
20 
21 #include "SamplerCore.hpp"
22 #include "Renderer/Stream.hpp"
23 #include "Common/Types.hpp"
24 
25 #include <unordered_map>
26 
27 namespace sw
28 {
29 	struct Stream;
30 	class VertexShader;
31 
32 	class VertexProgram : public VertexRoutine, public ShaderCore
33 	{
34 	public:
35 		VertexProgram(const VertexProcessor::State &state, const VertexShader *vertexShader);
36 
37 		virtual ~VertexProgram();
38 
39 	private:
40 		const VertexShader *const shader;
41 
42 		RegisterArray<NUM_TEMPORARY_REGISTERS> r;   // Temporary registers
43 		Vector4f a0;
44 		Array<Int> aL; // loop counter register
45 		Vector4f p0;
46 
47 		Array<Int> increment;
48 		Array<Int> iteration;
49 
50 		Int loopDepth;
51 		Int stackIndex;   // FIXME: Inc/decrement callStack
52 		Array<UInt> callStack;
53 
54 		Int enableIndex;
55 		Array<Int4, MAX_SHADER_ENABLE_STACK_SIZE> enableStack;
56 		Int4 enableBreak;
57 		Int4 enableContinue;
58 		Int4 enableLeave;
59 
60 		Int instanceID;
61 		Int4 vertexID;
62 
63 		typedef Shader::DestinationParameter Dst;
64 		typedef Shader::SourceParameter Src;
65 		typedef Shader::Control Control;
66 		typedef Shader::Usage Usage;
67 
68 		void pipeline(UInt &index) override;
69 		void program(UInt &index);
70 		void passThrough();
71 
72 		Vector4f fetchRegister(const Src &src, unsigned int offset = 0);
73 		Vector4f readConstant(const Src &src, unsigned int offset = 0);
74 		RValue<Pointer<Byte>> uniformAddress(int bufferIndex, unsigned int index);
75 		RValue<Pointer<Byte>> uniformAddress(int bufferIndex, unsigned int index, Int &offset);
76 		Int relativeAddress(const Shader::Relative &rel, int bufferIndex = -1);
77 		Int4 dynamicAddress(const Shader::Relative &rel);
78 		Int4 enableMask(const Shader::Instruction *instruction);
79 
80 		void M3X2(Vector4f &dst, Vector4f &src0, Src &src1);
81 		void M3X3(Vector4f &dst, Vector4f &src0, Src &src1);
82 		void M3X4(Vector4f &dst, Vector4f &src0, Src &src1);
83 		void M4X3(Vector4f &dst, Vector4f &src0, Src &src1);
84 		void M4X4(Vector4f &dst, Vector4f &src0, Src &src1);
85 		void BREAK();
86 		void BREAKC(Vector4f &src0, Vector4f &src1, Control);
87 		void BREAKP(const Src &predicateRegister);
88 		void BREAK(Int4 &condition);
89 		void CONTINUE();
90 		void TEST();
91 		void SCALAR();
92 		void CALL(int labelIndex, int callSiteIndex);
93 		void CALLNZ(int labelIndex, int callSiteIndex, const Src &src);
94 		void CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister);
95 		void CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister);
96 		void ELSE();
97 		void ENDIF();
98 		void ENDLOOP();
99 		void ENDREP();
100 		void ENDWHILE();
101 		void ENDSWITCH();
102 		void IF(const Src &src);
103 		void IFb(const Src &boolRegister);
104 		void IFp(const Src &predicateRegister);
105 		void IFC(Vector4f &src0, Vector4f &src1, Control);
106 		void IF(Int4 &condition);
107 		void LABEL(int labelIndex);
108 		void LOOP(const Src &integerRegister);
109 		void REP(const Src &integerRegister);
110 		void WHILE(const Src &temporaryRegister);
111 		void SWITCH();
112 		void RET();
113 		void LEAVE();
114 		void TEX(Vector4f &dst, Vector4f &src, const Src&);
115 		void TEXOFFSET(Vector4f &dst, Vector4f &src, const Src&, Vector4f &offset);
116 		void TEXLOD(Vector4f &dst, Vector4f &src, const Src&, Float4 &lod);
117 		void TEXLODOFFSET(Vector4f &dst, Vector4f &src, const Src&, 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 &src, const Src&, Vector4f &dsx, Vector4f &dsy);
121 		void TEXGRADOFFSET(Vector4f &dst, Vector4f &src, const Src&, Vector4f &dsx, Vector4f &dsy, Vector4f &offset);
122 		void TEXSIZE(Vector4f &dst, Float4 &lod, const Src&);
123 
124 		Vector4f sampleTexture(const Src &s, Vector4f &uvwq, Float4 &lod, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function);
125 		Vector4f sampleTexture(int sampler, Vector4f &uvwq, Float4 &lod, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function);
126 
127 		int ifDepth = 0;
128 		int loopRepDepth = 0;
129 		int currentLabel = -1;
130 		bool scalar = false;
131 
132 		std::vector<BasicBlock*> ifFalseBlock;
133 		std::vector<BasicBlock*> loopRepTestBlock;
134 		std::vector<BasicBlock*> loopRepEndBlock;
135 		std::vector<BasicBlock*> labelBlock;
136 		std::unordered_map<unsigned int, std::vector<BasicBlock*>> callRetBlock; // label -> list of call sites
137 		BasicBlock *returnBlock;
138 		std::vector<bool> isConditionalIf;
139 		std::vector<Int4> restoreContinue;
140 	};
141 }
142 
143 #endif   // sw_VertexProgram_hpp
144