1 /*
2  * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
3  * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
4  */
5 
6 #ifndef BGFX_SHADER_DX9BC_H
7 #define BGFX_SHADER_DX9BC_H
8 
9 #include <bx/readerwriter.h>
10 
11 namespace bgfx
12 {
13 	struct Dx9bcOpcode
14 	{
15 		enum Enum
16 		{
17 			NOP,
18 			MOV,
19 			ADD,
20 			SUB,
21 			MAD,
22 			MUL,
23 			RCP,
24 			RSQ,
25 			DP3,
26 			DP4,
27 			MIN,
28 			MAX,
29 			SLT,
30 			SGE,
31 			EXP,
32 			LOG,
33 			LIT,
34 			DST,
35 			LRP,
36 			FRC,
37 			M4X4,
38 			M4X3,
39 			M3X4,
40 			M3X3,
41 			M3X2,
42 			CALL,
43 			CALLNZ,
44 			LOOP,
45 			RET,
46 			ENDLOOP,
47 			LABEL,
48 			DCL,
49 			POW,
50 			CRS,
51 			SGN,
52 			ABS,
53 			NRM,
54 			SINCOS,
55 			REP,
56 			ENDREP,
57 			IF,
58 			IFC,
59 			ELSE,
60 			ENDIF,
61 			BREAK,
62 			BREAKC,
63 			MOVA,
64 			DEFB,
65 			DEFI,
66 
67 			Unknown = 63,
68 			TEXCOORD,
69 			TEXKILL,
70 			TEX,
71 			TEXBEM,
72 			TEXBEM1,
73 			TEXREG2AR,
74 			TEXREG2GB,
75 			TEXM3X2PAD,
76 			TEXM3X2TEX,
77 			TEXM3X3PAD,
78 			TEXM3X3TEX,
79 			TEXM3X3DIFF,
80 			TEXM3X3SPEC,
81 			TEXM3X3VSPEC,
82 			EXPP,
83 			LOGP,
84 			CND,
85 			DEF,
86 			TEXREG2RGB,
87 			TEXDP3TEX,
88 			TEXM3X2DEPTH,
89 			TEXDP3,
90 			TEXM3X3,
91 			TEXDEPTH,
92 			CMP,
93 			BEM,
94 			DP2ADD,
95 			DSX,
96 			DSY,
97 			TEXLDD,
98 			SETP,
99 			TEXLDL,
100 			BREAKP,
101 
102 			Count,
103 
104 			Phase   = 0xfffd,
105 			Comment = 0xfffe,
106 			End     = 0xffff
107 		};
108 	};
109 
110 	const char* getName(Dx9bcOpcode::Enum _opcode);
111 
112 	struct Dx9bcResourceDim
113 	{
114 		enum Enum
115 		{
116 			Unknown,
117 			Texture1D,
118 			Texture2D,
119 			TextureCube,
120 			Texture3D,
121 		};
122 	};
123 
124 	struct Dx9bcOperandType
125 	{
126 		enum Enum
127 		{
128 			Temp,
129 			Input,
130 			Const,
131 			Texture,
132 			RastOut,
133 			AttrOut,
134 			TexCrdOut,
135 			Output,
136 			ConstInt,
137 			ColorOut,
138 			DepthOut,
139 			Sampler,
140 			Const2,
141 			Const3,
142 			Const4,
143 			ConstBool,
144 			Loop,
145 			TempFloat16,
146 			MiscType,
147 			Label,
148 			Predicate,
149 
150 			Count
151 		};
152 	};
153 
154 	struct Dx9bcDeclUsage
155 	{
156 		enum Enum
157 		{
158 			Position,
159 			BlendWeight,
160 			BlendIndices,
161 			Normal,
162 			Psize,
163 			Texcoord,
164 			Tangent,
165 			Binormal,
166 			TessFactor,
167 			PositionT,
168 			Color,
169 			Fog,
170 			Depth,
171 			Sample,
172 
173 			Count
174   		};
175 	};
176 
177 	struct Dx9bcOperandAddrMode
178 	{
179 		enum Enum
180 		{
181 			Absolute,
182 			Relative,
183 
184 			Count
185 		};
186 	};
187 
188 	struct Dx9bcSubOperand
189 	{
Dx9bcSubOperandDx9bcSubOperand190 		Dx9bcSubOperand() { /* not pod */ }
191 
192 		Dx9bcOperandType::Enum type;
193 		uint32_t regIndex;
194 		uint8_t swizzleBits;
195 	};
196 
197 	struct Dx9bcOperand
198 	{
Dx9bcOperandDx9bcOperand199 		Dx9bcOperand() { /* not pod */ }
200 
201 		Dx9bcOperandType::Enum type;
202 		uint32_t regIndex;
203 
204 		bool destination;
205 
206 		// Destination
207 		uint8_t writeMask;
208 		bool saturate;
209 		bool partialPrecision;
210 		bool centroid;
211 
212 		// Source
213 		uint8_t swizzleBits;
214 
215 		Dx9bcOperandAddrMode::Enum addrMode;
216 		Dx9bcSubOperand subOperand;
217 	};
218 
219 	struct Dx9bcInstruction
220 	{
Dx9bcInstructionDx9bcInstruction221 		Dx9bcInstruction() { /* not pod */ }
222 
223 		Dx9bcOpcode::Enum opcode;
224 		uint16_t length;
225 		uint8_t numOperands;
226 		uint8_t numValues;
227 		uint8_t specific;
228 		bool predicated;
229 		bool coissue;
230 
231 		Dx9bcOperand operand[6];
232 		int32_t value[4];
233 	};
234 
235 	int32_t read(bx::ReaderI* _reader, Dx9bcInstruction& _instruction, bx::Error* _err);
236 	int32_t write(bx::WriterI* _writer, const Dx9bcInstruction& _instruction, bx::Error* _err);
237 	int32_t toString(char* _out, int32_t _size, const Dx9bcInstruction& _instruction);
238 
239 	struct Dx9bcShader
240 	{
Dx9bcShaderDx9bcShader241 		Dx9bcShader() { /* not pod */ }
242 
243 		stl::vector<uint8_t> byteCode;
244 	};
245 
246 	int32_t read(bx::ReaderSeekerI* _reader, Dx9bcShader& _shader, bx::Error* _err);
247 	int32_t write(bx::WriterI* _writer, const Dx9bcShader& _shader, bx::Error* _err);
248 
249 	struct Dx9bc
250 	{
Dx9bcDx9bc251 		Dx9bc() { /* not pod */ }
252 
253 		uint32_t version;
254 		Dx9bcShader shader;
255 	};
256 
257 	int32_t read(bx::ReaderSeekerI* _reader, Dx9bc& _dx9bc, bx::Error* _err);
258 	int32_t write(bx::WriterSeekerI* _writer, const Dx9bc& _dx9bc, bx::Error* _err);
259 
260 	typedef bool (*Dx9bcParseFn)(uint32_t _offset, const Dx9bcInstruction& _instruction, void* _userData);
261 	void parse(const Dx9bcShader& _src, Dx9bcParseFn _fn, void* _userData, bx::Error* _err = NULL);
262 
263 	typedef void (*Dx9bcFilterFn)(Dx9bcInstruction& _instruction, void* _userData);
264 	void filter(Dx9bcShader& _dst, const Dx9bcShader& _src, Dx9bcFilterFn _fn, void* _userData, bx::Error* _err = NULL);
265 
266 } // namespace bgfx
267 
268 #endif // BGFX_SHADER_DX9BC_H
269