dbghelp: Fix compiler warnings with flag -Wunused-macros.
[wine.git] / dlls / wined3d / shader_sm4.c
blob046b25292b303d2b359b4a1cd8a3958695fb7666
1 /*
2 * Copyright 2009 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include "wined3d_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
25 WINE_DECLARE_DEBUG_CHANNEL(d3d_bytecode);
27 #define WINED3D_SM4_INSTRUCTION_MODIFIER (1 << 31)
29 #define WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT 24
30 #define WINED3D_SM4_INSTRUCTION_LENGTH_MASK (0x1f << WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT)
32 #define WINED3D_SM4_RESOURCE_TYPE_SHIFT 11
33 #define WINED3D_SM4_RESOURCE_TYPE_MASK (0xf << WINED3D_SM4_RESOURCE_TYPE_SHIFT)
35 #define WINED3D_SM4_PRIMITIVE_TYPE_SHIFT 11
36 #define WINED3D_SM4_PRIMITIVE_TYPE_MASK (0x7 << WINED3D_SM4_PRIMITIVE_TYPE_SHIFT)
38 #define WINED3D_SM4_INDEX_TYPE_SHIFT 11
39 #define WINED3D_SM4_INDEX_TYPE_MASK (0x1 << WINED3D_SM4_INDEX_TYPE_SHIFT)
41 #define WINED3D_SM4_OPCODE_MASK 0xff
43 #define WINED3D_SM4_REGISTER_MODIFIER (1 << 31)
45 #define WINED3D_SM4_ADDRESSING_SHIFT1 25
46 #define WINED3D_SM4_ADDRESSING_MASK1 (0x3 << WINED3D_SM4_ADDRESSING_SHIFT1)
48 #define WINED3D_SM4_ADDRESSING_SHIFT0 22
49 #define WINED3D_SM4_ADDRESSING_MASK0 (0x3 << WINED3D_SM4_ADDRESSING_SHIFT0)
51 #define WINED3D_SM4_REGISTER_ORDER_SHIFT 20
52 #define WINED3D_SM4_REGISTER_ORDER_MASK (0x3 << WINED3D_SM4_REGISTER_ORDER_SHIFT)
54 #define WINED3D_SM4_REGISTER_TYPE_SHIFT 12
55 #define WINED3D_SM4_REGISTER_TYPE_MASK (0xf << WINED3D_SM4_REGISTER_TYPE_SHIFT)
57 #define WINED3D_SM4_SWIZZLE_TYPE_SHIFT 2
58 #define WINED3D_SM4_SWIZZLE_TYPE_MASK (0x3 << WINED3D_SM4_SWIZZLE_TYPE_SHIFT)
60 #define WINED3D_SM4_IMMCONST_TYPE_SHIFT 0
61 #define WINED3D_SM4_IMMCONST_TYPE_MASK (0x3 << WINED3D_SM4_IMMCONST_TYPE_SHIFT)
63 #define WINED3D_SM4_WRITEMASK_SHIFT 4
64 #define WINED3D_SM4_WRITEMASK_MASK (0xf << WINED3D_SM4_WRITEMASK_SHIFT)
66 #define WINED3D_SM4_SWIZZLE_SHIFT 4
67 #define WINED3D_SM4_SWIZZLE_MASK (0xff << WINED3D_SM4_SWIZZLE_SHIFT)
69 #define WINED3D_SM4_VERSION_MAJOR(version) (((version) >> 4) & 0xf)
70 #define WINED3D_SM4_VERSION_MINOR(version) (((version) >> 0) & 0xf)
72 #define WINED3D_SM4_ADDRESSING_RELATIVE 0x2
73 #define WINED3D_SM4_ADDRESSING_OFFSET 0x1
75 enum wined3d_sm4_opcode
77 WINED3D_SM4_OP_ADD = 0x00,
78 WINED3D_SM4_OP_AND = 0x01,
79 WINED3D_SM4_OP_BREAK = 0x02,
80 WINED3D_SM4_OP_BREAKC = 0x03,
81 WINED3D_SM4_OP_CUT = 0x09,
82 WINED3D_SM4_OP_DERIV_RTX = 0x0b,
83 WINED3D_SM4_OP_DERIV_RTY = 0x0c,
84 WINED3D_SM4_OP_DISCARD = 0x0d,
85 WINED3D_SM4_OP_DIV = 0x0e,
86 WINED3D_SM4_OP_DP2 = 0x0f,
87 WINED3D_SM4_OP_DP3 = 0x10,
88 WINED3D_SM4_OP_DP4 = 0x11,
89 WINED3D_SM4_OP_EMIT = 0x13,
90 WINED3D_SM4_OP_ENDIF = 0x15,
91 WINED3D_SM4_OP_ENDLOOP = 0x16,
92 WINED3D_SM4_OP_EQ = 0x18,
93 WINED3D_SM4_OP_EXP = 0x19,
94 WINED3D_SM4_OP_FRC = 0x1a,
95 WINED3D_SM4_OP_FTOI = 0x1b,
96 WINED3D_SM4_OP_GE = 0x1d,
97 WINED3D_SM4_OP_IADD = 0x1e,
98 WINED3D_SM4_OP_IF = 0x1f,
99 WINED3D_SM4_OP_IEQ = 0x20,
100 WINED3D_SM4_OP_IGE = 0x21,
101 WINED3D_SM4_OP_IMUL = 0x26,
102 WINED3D_SM4_OP_ISHL = 0x29,
103 WINED3D_SM4_OP_ITOF = 0x2b,
104 WINED3D_SM4_OP_LD = 0x2d,
105 WINED3D_SM4_OP_LOG = 0x2f,
106 WINED3D_SM4_OP_LOOP = 0x30,
107 WINED3D_SM4_OP_LT = 0x31,
108 WINED3D_SM4_OP_MAD = 0x32,
109 WINED3D_SM4_OP_MIN = 0x33,
110 WINED3D_SM4_OP_MAX = 0x34,
111 WINED3D_SM4_OP_MOV = 0x36,
112 WINED3D_SM4_OP_MOVC = 0x37,
113 WINED3D_SM4_OP_MUL = 0x38,
114 WINED3D_SM4_OP_RET = 0x3e,
115 WINED3D_SM4_OP_ROUND_NI = 0x41,
116 WINED3D_SM4_OP_RSQ = 0x44,
117 WINED3D_SM4_OP_SAMPLE = 0x45,
118 WINED3D_SM4_OP_SAMPLE_LOD = 0x48,
119 WINED3D_SM4_OP_SAMPLE_GRAD = 0x49,
120 WINED3D_SM4_OP_SQRT = 0x4b,
121 WINED3D_SM4_OP_SINCOS = 0x4d,
122 WINED3D_SM4_OP_UDIV = 0x4e,
123 WINED3D_SM4_OP_UGE = 0x50,
124 WINED3D_SM4_OP_USHR = 0x55,
125 WINED3D_SM4_OP_UTOF = 0x56,
126 WINED3D_SM4_OP_XOR = 0x57,
127 WINED3D_SM4_OP_DCL_RESOURCE = 0x58,
128 WINED3D_SM4_OP_DCL_CONSTANT_BUFFER = 0x59,
129 WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY = 0x5c,
130 WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE = 0x5d,
131 WINED3D_SM4_OP_DCL_VERTICES_OUT = 0x5e,
134 enum wined3d_sm4_register_type
136 WINED3D_SM4_RT_TEMP = 0x0,
137 WINED3D_SM4_RT_INPUT = 0x1,
138 WINED3D_SM4_RT_OUTPUT = 0x2,
139 WINED3D_SM4_RT_IMMCONST = 0x4,
140 WINED3D_SM4_RT_SAMPLER = 0x6,
141 WINED3D_SM4_RT_RESOURCE = 0x7,
142 WINED3D_SM4_RT_CONSTBUFFER = 0x8,
143 WINED3D_SM4_RT_PRIMID = 0xb,
144 WINED3D_SM4_RT_NULL = 0xd,
147 enum wined3d_sm4_output_primitive_type
149 WINED3D_SM4_OUTPUT_PT_POINTLIST = 0x1,
150 WINED3D_SM4_OUTPUT_PT_LINELIST = 0x3,
151 WINED3D_SM4_OUTPUT_PT_TRIANGLESTRIP = 0x5,
154 enum wined3d_sm4_input_primitive_type
156 WINED3D_SM4_INPUT_PT_POINT = 0x1,
157 WINED3D_SM4_INPUT_PT_LINE = 0x2,
158 WINED3D_SM4_INPUT_PT_TRIANGLE = 0x3,
159 WINED3D_SM4_INPUT_PT_LINEADJ = 0x6,
160 WINED3D_SM4_INPUT_PT_TRIANGLEADJ = 0x7,
163 enum wined3d_sm4_swizzle_type
165 WINED3D_SM4_SWIZZLE_VEC4 = 0x1,
166 WINED3D_SM4_SWIZZLE_SCALAR = 0x2,
169 enum wined3d_sm4_immconst_type
171 WINED3D_SM4_IMMCONST_SCALAR = 0x1,
172 WINED3D_SM4_IMMCONST_VEC4 = 0x2,
175 enum wined3d_sm4_resource_type
177 WINED3D_SM4_RESOURCE_BUFFER = 0x1,
178 WINED3D_SM4_RESOURCE_TEXTURE_1D = 0x2,
179 WINED3D_SM4_RESOURCE_TEXTURE_2D = 0x3,
180 WINED3D_SM4_RESOURCE_TEXTURE_2DMS = 0x4,
181 WINED3D_SM4_RESOURCE_TEXTURE_3D = 0x5,
182 WINED3D_SM4_RESOURCE_TEXTURE_CUBE = 0x6,
183 WINED3D_SM4_RESOURCE_TEXTURE_1DARRAY = 0x7,
184 WINED3D_SM4_RESOURCE_TEXTURE_2DARRAY = 0x8,
185 WINED3D_SM4_RESOURCE_TEXTURE_2DMSARRAY = 0x9,
188 enum wined3d_sm4_data_type
190 WINED3D_SM4_DATA_UNORM = 0x1,
191 WINED3D_SM4_DATA_SNORM = 0x2,
192 WINED3D_SM4_DATA_INT = 0x3,
193 WINED3D_SM4_DATA_UINT = 0x4,
194 WINED3D_SM4_DATA_FLOAT = 0x5,
197 struct wined3d_shader_src_param_entry
199 struct list entry;
200 struct wined3d_shader_src_param param;
203 struct wined3d_sm4_data
205 struct wined3d_shader_version shader_version;
206 const DWORD *end;
208 struct
210 enum wined3d_shader_register_type register_type;
211 UINT register_idx;
212 } output_map[MAX_REG_OUTPUT];
214 struct wined3d_shader_src_param src_param[5];
215 struct wined3d_shader_dst_param dst_param[2];
216 struct list src_free;
217 struct list src;
220 struct wined3d_sm4_opcode_info
222 enum wined3d_sm4_opcode opcode;
223 enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx;
224 const char *dst_info;
225 const char *src_info;
228 struct sysval_map
230 enum wined3d_sysval_semantic sysval;
231 enum wined3d_shader_register_type register_type;
232 UINT register_idx;
236 * F -> WINED3D_DATA_FLOAT
237 * I -> WINED3D_DATA_INT
238 * R -> WINED3D_DATA_RESOURCE
239 * S -> WINED3D_DATA_SAMPLER
240 * U -> WINED3D_DATA_UINT
242 static const struct wined3d_sm4_opcode_info opcode_table[] =
244 {WINED3D_SM4_OP_ADD, WINED3DSIH_ADD, "F", "FF"},
245 {WINED3D_SM4_OP_AND, WINED3DSIH_AND, "U", "UU"},
246 {WINED3D_SM4_OP_BREAK, WINED3DSIH_BREAK, "", ""},
247 {WINED3D_SM4_OP_BREAKC, WINED3DSIH_BREAKP, "", "U"},
248 {WINED3D_SM4_OP_CUT, WINED3DSIH_CUT, "", ""},
249 {WINED3D_SM4_OP_DERIV_RTX, WINED3DSIH_DSX, "F", "F"},
250 {WINED3D_SM4_OP_DERIV_RTY, WINED3DSIH_DSY, "F", "F"},
251 {WINED3D_SM4_OP_DISCARD, WINED3DSIH_TEXKILL, "", "U"},
252 {WINED3D_SM4_OP_DIV, WINED3DSIH_DIV, "F", "FF"},
253 {WINED3D_SM4_OP_DP2, WINED3DSIH_DP2, "F", "FF"},
254 {WINED3D_SM4_OP_DP3, WINED3DSIH_DP3, "F", "FF"},
255 {WINED3D_SM4_OP_DP4, WINED3DSIH_DP4, "F", "FF"},
256 {WINED3D_SM4_OP_EMIT, WINED3DSIH_EMIT, "", ""},
257 {WINED3D_SM4_OP_ENDIF, WINED3DSIH_ENDIF, "", ""},
258 {WINED3D_SM4_OP_ENDLOOP, WINED3DSIH_ENDLOOP, "", ""},
259 {WINED3D_SM4_OP_EQ, WINED3DSIH_EQ, "U", "FF"},
260 {WINED3D_SM4_OP_EXP, WINED3DSIH_EXP, "F", "F"},
261 {WINED3D_SM4_OP_FRC, WINED3DSIH_FRC, "F", "F"},
262 {WINED3D_SM4_OP_FTOI, WINED3DSIH_FTOI, "I", "F"},
263 {WINED3D_SM4_OP_GE, WINED3DSIH_GE, "U", "FF"},
264 {WINED3D_SM4_OP_IADD, WINED3DSIH_IADD, "I", "II"},
265 {WINED3D_SM4_OP_IF, WINED3DSIH_IF, "", "U"},
266 {WINED3D_SM4_OP_IEQ, WINED3DSIH_IEQ, "U", "II"},
267 {WINED3D_SM4_OP_IGE, WINED3DSIH_IGE, "U", "II"},
268 {WINED3D_SM4_OP_IMUL, WINED3DSIH_IMUL, "II", "II"},
269 {WINED3D_SM4_OP_ISHL, WINED3DSIH_ISHL, "I", "II"},
270 {WINED3D_SM4_OP_ITOF, WINED3DSIH_ITOF, "F", "I"},
271 {WINED3D_SM4_OP_LD, WINED3DSIH_LD, "U", "FR"},
272 {WINED3D_SM4_OP_LOG, WINED3DSIH_LOG, "F", "F"},
273 {WINED3D_SM4_OP_LOOP, WINED3DSIH_LOOP, "", ""},
274 {WINED3D_SM4_OP_LT, WINED3DSIH_LT, "U", "FF"},
275 {WINED3D_SM4_OP_MAD, WINED3DSIH_MAD, "F", "FFF"},
276 {WINED3D_SM4_OP_MIN, WINED3DSIH_MIN, "F", "FF"},
277 {WINED3D_SM4_OP_MAX, WINED3DSIH_MAX, "F", "FF"},
278 {WINED3D_SM4_OP_MOV, WINED3DSIH_MOV, "F", "F"},
279 {WINED3D_SM4_OP_MOVC, WINED3DSIH_MOVC, "F", "UFF"},
280 {WINED3D_SM4_OP_MUL, WINED3DSIH_MUL, "F", "FF"},
281 {WINED3D_SM4_OP_RET, WINED3DSIH_RET, "", ""},
282 {WINED3D_SM4_OP_ROUND_NI, WINED3DSIH_ROUND_NI, "F", "F"},
283 {WINED3D_SM4_OP_RSQ, WINED3DSIH_RSQ, "F", "F"},
284 {WINED3D_SM4_OP_SAMPLE, WINED3DSIH_SAMPLE, "U", "FRS"},
285 {WINED3D_SM4_OP_SAMPLE_LOD, WINED3DSIH_SAMPLE_LOD, "U", "FRSF"},
286 {WINED3D_SM4_OP_SAMPLE_GRAD, WINED3DSIH_SAMPLE_GRAD, "U", "FRSFF"},
287 {WINED3D_SM4_OP_SQRT, WINED3DSIH_SQRT, "F", "F"},
288 {WINED3D_SM4_OP_SINCOS, WINED3DSIH_SINCOS, "FF", "F"},
289 {WINED3D_SM4_OP_UDIV, WINED3DSIH_UDIV, "UU", "UU"},
290 {WINED3D_SM4_OP_UGE, WINED3DSIH_UGE, "U", "UU"},
291 {WINED3D_SM4_OP_USHR, WINED3DSIH_USHR, "U", "UU"},
292 {WINED3D_SM4_OP_UTOF, WINED3DSIH_UTOF, "F", "U"},
293 {WINED3D_SM4_OP_XOR, WINED3DSIH_XOR, "U", "UU"},
294 {WINED3D_SM4_OP_DCL_RESOURCE, WINED3DSIH_DCL, "R", ""},
295 {WINED3D_SM4_OP_DCL_CONSTANT_BUFFER, WINED3DSIH_DCL_CONSTANT_BUFFER, "", ""},
296 {WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY, WINED3DSIH_DCL_OUTPUT_TOPOLOGY, "", ""},
297 {WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE, WINED3DSIH_DCL_INPUT_PRIMITIVE, "", ""},
298 {WINED3D_SM4_OP_DCL_VERTICES_OUT, WINED3DSIH_DCL_VERTICES_OUT, "", ""},
301 static const enum wined3d_shader_register_type register_type_table[] =
303 /* WINED3D_SM4_RT_TEMP */ WINED3DSPR_TEMP,
304 /* WINED3D_SM4_RT_INPUT */ WINED3DSPR_INPUT,
305 /* WINED3D_SM4_RT_OUTPUT */ WINED3DSPR_OUTPUT,
306 /* UNKNOWN */ 0,
307 /* WINED3D_SM4_RT_IMMCONST */ WINED3DSPR_IMMCONST,
308 /* UNKNOWN */ 0,
309 /* WINED3D_SM4_RT_SAMPLER */ WINED3DSPR_SAMPLER,
310 /* WINED3D_SM4_RT_RESOURCE */ WINED3DSPR_RESOURCE,
311 /* WINED3D_SM4_RT_CONSTBUFFER */ WINED3DSPR_CONSTBUFFER,
312 /* UNKNOWN */ 0,
313 /* UNKNOWN */ 0,
314 /* WINED3D_SM4_RT_PRIMID */ WINED3DSPR_PRIMID,
315 /* UNKNOWN */ 0,
316 /* WINED3D_SM4_RT_NULL */ WINED3DSPR_NULL,
319 static const enum wined3d_primitive_type output_primitive_type_table[] =
321 /* UNKNOWN */ WINED3D_PT_UNDEFINED,
322 /* WINED3D_SM4_OUTPUT_PT_POINTLIST */ WINED3D_PT_POINTLIST,
323 /* UNKNOWN */ WINED3D_PT_UNDEFINED,
324 /* WINED3D_SM4_OUTPUT_PT_LINELIST */ WINED3D_PT_LINELIST,
325 /* UNKNOWN */ WINED3D_PT_UNDEFINED,
326 /* WINED3D_SM4_OUTPUT_PT_TRIANGLESTRIP */ WINED3D_PT_TRIANGLESTRIP,
329 static const enum wined3d_primitive_type input_primitive_type_table[] =
331 /* UNKNOWN */ WINED3D_PT_UNDEFINED,
332 /* WINED3D_SM4_INPUT_PT_POINT */ WINED3D_PT_POINTLIST,
333 /* WINED3D_SM4_INPUT_PT_LINE */ WINED3D_PT_LINELIST,
334 /* WINED3D_SM4_INPUT_PT_TRIANGLE */ WINED3D_PT_TRIANGLELIST,
335 /* UNKNOWN */ WINED3D_PT_UNDEFINED,
336 /* UNKNOWN */ WINED3D_PT_UNDEFINED,
337 /* WINED3D_SM4_INPUT_PT_LINEADJ */ WINED3D_PT_LINELIST_ADJ,
338 /* WINED3D_SM4_INPUT_PT_TRIANGLEADJ */ WINED3D_PT_TRIANGLELIST_ADJ,
341 static const struct sysval_map sysval_map[] =
343 {WINED3D_SV_DEPTH, WINED3DSPR_DEPTHOUT, 0},
344 {WINED3D_SV_TARGET0, WINED3DSPR_COLOROUT, 0},
345 {WINED3D_SV_TARGET1, WINED3DSPR_COLOROUT, 1},
346 {WINED3D_SV_TARGET2, WINED3DSPR_COLOROUT, 2},
347 {WINED3D_SV_TARGET3, WINED3DSPR_COLOROUT, 3},
348 {WINED3D_SV_TARGET4, WINED3DSPR_COLOROUT, 4},
349 {WINED3D_SV_TARGET5, WINED3DSPR_COLOROUT, 5},
350 {WINED3D_SV_TARGET6, WINED3DSPR_COLOROUT, 6},
351 {WINED3D_SV_TARGET7, WINED3DSPR_COLOROUT, 7},
354 static const enum wined3d_shader_resource_type resource_type_table[] =
356 /* 0 */ WINED3D_SHADER_RESOURCE_NONE,
357 /* WINED3D_SM4_RESOURCE_BUFFER */ WINED3D_SHADER_RESOURCE_BUFFER,
358 /* WINED3D_SM4_RESOURCE_TEXTURE_1D */ WINED3D_SHADER_RESOURCE_TEXTURE_1D,
359 /* WINED3D_SM4_RESOURCE_TEXTURE_2D */ WINED3D_SHADER_RESOURCE_TEXTURE_2D,
360 /* WINED3D_SM4_RESOURCE_TEXTURE_2DMS */ WINED3D_SHADER_RESOURCE_TEXTURE_2DMS,
361 /* WINED3D_SM4_RESOURCE_TEXTURE_3D */ WINED3D_SHADER_RESOURCE_TEXTURE_3D,
362 /* WINED3D_SM4_RESOURCE_TEXTURE_CUBE */ WINED3D_SHADER_RESOURCE_TEXTURE_CUBE,
363 /* WINED3D_SM4_RESOURCE_TEXTURE_1DARRAY */ WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY,
364 /* WINED3D_SM4_RESOURCE_TEXTURE_2DARRAY */ WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY,
365 /* WINED3D_SM4_RESOURCE_TEXTURE_2DMSARRAY */ WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY,
368 static const enum wined3d_data_type data_type_table[] =
370 /* 0 */ WINED3D_DATA_FLOAT,
371 /* WINED3D_SM4_DATA_UNORM */ WINED3D_DATA_UNORM,
372 /* WINED3D_SM4_DATA_SNORM */ WINED3D_DATA_SNORM,
373 /* WINED3D_SM4_DATA_INT */ WINED3D_DATA_INT,
374 /* WINED3D_SM4_DATA_UINT */ WINED3D_DATA_UINT,
375 /* WINED3D_SM4_DATA_FLOAT */ WINED3D_DATA_FLOAT,
378 static BOOL shader_sm4_read_src_param(struct wined3d_sm4_data *priv, const DWORD **ptr,
379 enum wined3d_data_type data_type, struct wined3d_shader_src_param *src_param);
381 static const struct wined3d_sm4_opcode_info *get_opcode_info(enum wined3d_sm4_opcode opcode)
383 unsigned int i;
385 for (i = 0; i < sizeof(opcode_table) / sizeof(*opcode_table); ++i)
387 if (opcode == opcode_table[i].opcode) return &opcode_table[i];
390 return NULL;
393 static void map_register(const struct wined3d_sm4_data *priv, struct wined3d_shader_register *reg)
395 switch (priv->shader_version.type)
397 case WINED3D_SHADER_TYPE_PIXEL:
398 if (reg->type == WINED3DSPR_OUTPUT)
400 unsigned int reg_idx = reg->idx[0].offset;
402 if (reg_idx >= ARRAY_SIZE(priv->output_map))
404 ERR("Invalid output index %u.\n", reg_idx);
405 break;
408 reg->type = priv->output_map[reg_idx].register_type;
409 reg->idx[0].offset = priv->output_map[reg_idx].register_idx;
411 break;
413 default:
414 break;
418 static enum wined3d_data_type map_data_type(char t)
420 switch (t)
422 case 'F':
423 return WINED3D_DATA_FLOAT;
424 case 'I':
425 return WINED3D_DATA_INT;
426 case 'R':
427 return WINED3D_DATA_RESOURCE;
428 case 'S':
429 return WINED3D_DATA_SAMPLER;
430 case 'U':
431 return WINED3D_DATA_UINT;
432 default:
433 ERR("Invalid data type '%c'.\n", t);
434 return WINED3D_DATA_FLOAT;
438 static void *shader_sm4_init(const DWORD *byte_code, const struct wined3d_shader_signature *output_signature)
440 struct wined3d_sm4_data *priv;
441 unsigned int i, j;
443 if (!(priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv))))
445 ERR("Failed to allocate private data\n");
446 return NULL;
449 memset(priv->output_map, 0xff, sizeof(priv->output_map));
450 for (i = 0; i < output_signature->element_count; ++i)
452 struct wined3d_shader_signature_element *e = &output_signature->elements[i];
454 if (e->register_idx >= ARRAY_SIZE(priv->output_map))
456 WARN("Invalid output index %u.\n", e->register_idx);
457 continue;
460 for (j = 0; j < ARRAY_SIZE(sysval_map); ++j)
462 if (e->sysval_semantic == sysval_map[j].sysval)
464 priv->output_map[e->register_idx].register_type = sysval_map[j].register_type;
465 priv->output_map[e->register_idx].register_idx = sysval_map[j].register_idx;
466 break;
471 list_init(&priv->src_free);
472 list_init(&priv->src);
474 return priv;
477 static void shader_sm4_free(void *data)
479 struct wined3d_shader_src_param_entry *e1, *e2;
480 struct wined3d_sm4_data *priv = data;
482 list_move_head(&priv->src_free, &priv->src);
483 LIST_FOR_EACH_ENTRY_SAFE(e1, e2, &priv->src_free, struct wined3d_shader_src_param_entry, entry)
485 HeapFree(GetProcessHeap(), 0, e1);
487 HeapFree(GetProcessHeap(), 0, priv);
490 static struct wined3d_shader_src_param *get_src_param(struct wined3d_sm4_data *priv)
492 struct wined3d_shader_src_param_entry *e;
493 struct list *elem;
495 if (!list_empty(&priv->src_free))
497 elem = list_head(&priv->src_free);
498 list_remove(elem);
500 else
502 if (!(e = HeapAlloc(GetProcessHeap(), 0, sizeof(*e))))
503 return NULL;
504 elem = &e->entry;
507 list_add_tail(&priv->src, elem);
508 e = LIST_ENTRY(elem, struct wined3d_shader_src_param_entry, entry);
509 return &e->param;
512 static void shader_sm4_read_header(void *data, const DWORD **ptr, struct wined3d_shader_version *shader_version)
514 struct wined3d_sm4_data *priv = data;
515 DWORD version_token;
517 priv->end = *ptr;
519 version_token = *(*ptr)++;
520 TRACE("version: 0x%08x\n", version_token);
522 TRACE("token count: %u\n", **ptr);
523 priv->end += *(*ptr)++;
525 switch (version_token >> 16)
527 case WINED3D_SM4_PS:
528 priv->shader_version.type = WINED3D_SHADER_TYPE_PIXEL;
529 break;
531 case WINED3D_SM4_VS:
532 priv->shader_version.type = WINED3D_SHADER_TYPE_VERTEX;
533 break;
535 case WINED3D_SM4_GS:
536 priv->shader_version.type = WINED3D_SHADER_TYPE_GEOMETRY;
537 break;
539 default:
540 FIXME("Unrecognized shader type %#x\n", version_token >> 16);
542 priv->shader_version.major = WINED3D_SM4_VERSION_MAJOR(version_token);
543 priv->shader_version.minor = WINED3D_SM4_VERSION_MINOR(version_token);
545 *shader_version = priv->shader_version;
548 static BOOL shader_sm4_read_reg_idx(struct wined3d_sm4_data *priv, const DWORD **ptr,
549 DWORD addressing, struct wined3d_shader_register_index *reg_idx)
551 if (addressing & WINED3D_SM4_ADDRESSING_RELATIVE)
553 struct wined3d_shader_src_param *rel_addr = get_src_param(priv);
555 if (!(reg_idx->rel_addr = rel_addr))
557 ERR("Failed to get src param for relative addressing.\n");
558 return FALSE;
561 if (addressing & WINED3D_SM4_ADDRESSING_OFFSET)
562 reg_idx->offset = *(*ptr)++;
563 else
564 reg_idx->offset = 0;
565 shader_sm4_read_src_param(priv, ptr, WINED3D_DATA_INT, rel_addr);
567 else
569 reg_idx->rel_addr = NULL;
570 reg_idx->offset = *(*ptr)++;
573 return TRUE;
576 static BOOL shader_sm4_read_param(struct wined3d_sm4_data *priv, const DWORD **ptr,
577 enum wined3d_data_type data_type, struct wined3d_shader_register *param,
578 enum wined3d_shader_src_modifier *modifier)
580 enum wined3d_sm4_register_type register_type;
581 DWORD token = *(*ptr)++;
582 DWORD order;
584 register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
585 if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
587 FIXME("Unhandled register type %#x.\n", register_type);
588 param->type = WINED3DSPR_TEMP;
590 else
592 param->type = register_type_table[register_type];
594 param->data_type = data_type;
596 if (token & WINED3D_SM4_REGISTER_MODIFIER)
598 DWORD m = *(*ptr)++;
600 /* FIXME: This will probably break down at some point. The SM4
601 * modifiers look like flags, while wined3d currently has an enum
602 * with possible combinations, e.g. WINED3DSPSM_ABSNEG. */
603 switch (m)
605 case 0x41:
606 *modifier = WINED3DSPSM_NEG;
607 break;
609 case 0x81:
610 *modifier = WINED3DSPSM_ABS;
611 break;
613 default:
614 FIXME("Skipping modifier 0x%08x.\n", m);
615 *modifier = WINED3DSPSM_NONE;
616 break;
619 else
621 *modifier = WINED3DSPSM_NONE;
624 order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
626 if (order < 1)
627 param->idx[0].offset = ~0U;
628 else
630 DWORD addressing = (token & WINED3D_SM4_ADDRESSING_MASK0) >> WINED3D_SM4_ADDRESSING_SHIFT0;
631 if (!(shader_sm4_read_reg_idx(priv, ptr, addressing, &param->idx[0])))
633 ERR("Failed to read register index.\n");
634 return FALSE;
638 if (order < 2)
639 param->idx[1].offset = ~0U;
640 else
642 DWORD addressing = (token & WINED3D_SM4_ADDRESSING_MASK1) >> WINED3D_SM4_ADDRESSING_SHIFT1;
643 if (!(shader_sm4_read_reg_idx(priv, ptr, addressing, &param->idx[1])))
645 ERR("Failed to read register index.\n");
646 return FALSE;
650 if (order > 2)
651 FIXME("Unhandled order %u.\n", order);
653 if (register_type == WINED3D_SM4_RT_IMMCONST)
655 enum wined3d_sm4_immconst_type immconst_type =
656 (token & WINED3D_SM4_IMMCONST_TYPE_MASK) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT;
658 switch (immconst_type)
660 case WINED3D_SM4_IMMCONST_SCALAR:
661 param->immconst_type = WINED3D_IMMCONST_SCALAR;
662 memcpy(param->immconst_data, *ptr, 1 * sizeof(DWORD));
663 *ptr += 1;
664 break;
666 case WINED3D_SM4_IMMCONST_VEC4:
667 param->immconst_type = WINED3D_IMMCONST_VEC4;
668 memcpy(param->immconst_data, *ptr, 4 * sizeof(DWORD));
669 *ptr += 4;
670 break;
672 default:
673 FIXME("Unhandled immediate constant type %#x.\n", immconst_type);
674 break;
678 map_register(priv, param);
680 return TRUE;
683 static BOOL shader_sm4_read_src_param(struct wined3d_sm4_data *priv, const DWORD **ptr,
684 enum wined3d_data_type data_type, struct wined3d_shader_src_param *src_param)
686 DWORD token = **ptr;
688 if (!shader_sm4_read_param(priv, ptr, data_type, &src_param->reg, &src_param->modifiers))
690 ERR("Failed to read parameter.\n");
691 return FALSE;
694 if (src_param->reg.type == WINED3DSPR_IMMCONST)
696 src_param->swizzle = WINED3DSP_NOSWIZZLE;
698 else
700 enum wined3d_sm4_swizzle_type swizzle_type =
701 (token & WINED3D_SM4_SWIZZLE_TYPE_MASK) >> WINED3D_SM4_SWIZZLE_TYPE_SHIFT;
703 switch (swizzle_type)
705 case WINED3D_SM4_SWIZZLE_SCALAR:
706 src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
707 src_param->swizzle = (src_param->swizzle & 0x3) * 0x55;
708 break;
710 case WINED3D_SM4_SWIZZLE_VEC4:
711 src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
712 break;
714 default:
715 FIXME("Unhandled swizzle type %#x.\n", swizzle_type);
716 break;
720 return TRUE;
723 static BOOL shader_sm4_read_dst_param(struct wined3d_sm4_data *priv, const DWORD **ptr,
724 enum wined3d_data_type data_type, struct wined3d_shader_dst_param *dst_param)
726 enum wined3d_shader_src_modifier modifier;
727 DWORD token = **ptr;
729 if (!shader_sm4_read_param(priv, ptr, data_type, &dst_param->reg, &modifier))
731 ERR("Failed to read parameter.\n");
732 return FALSE;
735 if (modifier != WINED3DSPSM_NONE)
737 ERR("Invalid source modifier %#x on destination register.\n", modifier);
738 return FALSE;
741 dst_param->write_mask = (token & WINED3D_SM4_WRITEMASK_MASK) >> WINED3D_SM4_WRITEMASK_SHIFT;
742 dst_param->modifiers = 0;
743 dst_param->shift = 0;
745 return TRUE;
748 static void shader_sm4_read_instruction(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins)
750 const struct wined3d_sm4_opcode_info *opcode_info;
751 struct wined3d_sm4_data *priv = data;
752 DWORD opcode_token, opcode;
753 const DWORD *p;
754 UINT i, len;
756 list_move_head(&priv->src_free, &priv->src);
758 opcode_token = *(*ptr)++;
759 opcode = opcode_token & WINED3D_SM4_OPCODE_MASK;
760 len = ((opcode_token & WINED3D_SM4_INSTRUCTION_LENGTH_MASK) >> WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT) - 1;
762 if (TRACE_ON(d3d_bytecode))
764 TRACE_(d3d_bytecode)("[ %08x ", opcode_token);
765 for (i = 0; i < len; ++i)
767 TRACE_(d3d_bytecode)("%08x ", (*ptr)[i]);
769 TRACE_(d3d_bytecode)("]\n");
772 if (!(opcode_info = get_opcode_info(opcode)))
774 FIXME("Unrecognized opcode %#x, opcode_token 0x%08x.\n", opcode, opcode_token);
775 ins->handler_idx = WINED3DSIH_TABLE_SIZE;
776 *ptr += len;
777 return;
780 ins->handler_idx = opcode_info->handler_idx;
781 ins->flags = 0;
782 ins->coissue = 0;
783 ins->predicate = NULL;
784 ins->dst_count = strlen(opcode_info->dst_info);
785 ins->dst = priv->dst_param;
786 ins->src_count = strlen(opcode_info->src_info);
787 ins->src = priv->src_param;
789 p = *ptr;
790 *ptr += len;
792 if (opcode_token & WINED3D_SM4_INSTRUCTION_MODIFIER)
794 DWORD modifier = *p++;
795 FIXME("Skipping modifier 0x%08x.\n", modifier);
798 if (opcode == WINED3D_SM4_OP_DCL_RESOURCE)
800 enum wined3d_sm4_resource_type resource_type;
801 enum wined3d_sm4_data_type data_type;
802 DWORD components;
804 resource_type = (opcode_token & WINED3D_SM4_RESOURCE_TYPE_MASK) >> WINED3D_SM4_RESOURCE_TYPE_SHIFT;
805 if (!resource_type || (resource_type >= ARRAY_SIZE(resource_type_table)))
807 FIXME("Unhandled resource type %#x.\n", resource_type);
808 ins->declaration.semantic.resource_type = WINED3D_SHADER_RESOURCE_NONE;
810 else
812 ins->declaration.semantic.resource_type = resource_type_table[resource_type];
814 shader_sm4_read_dst_param(priv, &p, WINED3D_DATA_RESOURCE, &ins->declaration.semantic.reg);
816 components = *p++;
817 if ((components & 0xfff0) != (components & 0xf) * 0x1110)
818 FIXME("Components (%#x) have different data types.\n", components);
819 data_type = components & 0xf;
821 if (!data_type || (data_type >= ARRAY_SIZE(data_type_table)))
823 FIXME("Unhandled data type %#x.\n", data_type);
824 ins->declaration.semantic.resource_data_type = WINED3D_DATA_FLOAT;
826 else
828 ins->declaration.semantic.resource_data_type = data_type_table[data_type];
831 else if (opcode == WINED3D_SM4_OP_DCL_CONSTANT_BUFFER)
833 shader_sm4_read_src_param(priv, &p, WINED3D_DATA_FLOAT, &ins->declaration.src);
834 if (opcode_token & WINED3D_SM4_INDEX_TYPE_MASK)
835 ins->flags |= WINED3DSI_INDEXED_DYNAMIC;
837 else if (opcode == WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY)
839 enum wined3d_sm4_output_primitive_type primitive_type;
841 primitive_type = (opcode_token & WINED3D_SM4_PRIMITIVE_TYPE_MASK) >> WINED3D_SM4_PRIMITIVE_TYPE_SHIFT;
842 if (primitive_type >= sizeof(output_primitive_type_table) / sizeof(*output_primitive_type_table))
844 FIXME("Unhandled output primitive type %#x.\n", primitive_type);
845 ins->declaration.primitive_type = WINED3D_PT_UNDEFINED;
847 else
849 ins->declaration.primitive_type = output_primitive_type_table[primitive_type];
852 else if (opcode == WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE)
854 enum wined3d_sm4_input_primitive_type primitive_type;
856 primitive_type = (opcode_token & WINED3D_SM4_PRIMITIVE_TYPE_MASK) >> WINED3D_SM4_PRIMITIVE_TYPE_SHIFT;
857 if (primitive_type >= sizeof(input_primitive_type_table) / sizeof(*input_primitive_type_table))
859 FIXME("Unhandled input primitive type %#x.\n", primitive_type);
860 ins->declaration.primitive_type = WINED3D_PT_UNDEFINED;
862 else
864 ins->declaration.primitive_type = input_primitive_type_table[primitive_type];
867 else if (opcode == WINED3D_SM4_OP_DCL_VERTICES_OUT)
869 ins->declaration.count = *p++;
871 else
873 for (i = 0; i < ins->dst_count; ++i)
875 if (!(shader_sm4_read_dst_param(priv, &p, map_data_type(opcode_info->dst_info[i]), &priv->dst_param[i])))
877 ins->handler_idx = WINED3DSIH_TABLE_SIZE;
878 return;
882 for (i = 0; i < ins->src_count; ++i)
884 if (!(shader_sm4_read_src_param(priv, &p, map_data_type(opcode_info->src_info[i]), &priv->src_param[i])))
886 ins->handler_idx = WINED3DSIH_TABLE_SIZE;
887 return;
893 static BOOL shader_sm4_is_end(void *data, const DWORD **ptr)
895 struct wined3d_sm4_data *priv = data;
896 return *ptr == priv->end;
899 const struct wined3d_shader_frontend sm4_shader_frontend =
901 shader_sm4_init,
902 shader_sm4_free,
903 shader_sm4_read_header,
904 shader_sm4_read_instruction,
905 shader_sm4_is_end,