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
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 (0xf << WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT)
32 #define WINED3D_SM4_PRIMITIVE_TYPE_SHIFT 11
33 #define WINED3D_SM4_PRIMITIVE_TYPE_MASK (0x7 << WINED3D_SM4_PRIMITIVE_TYPE_SHIFT)
35 #define WINED3D_SM4_INDEX_TYPE_SHIFT 11
36 #define WINED3D_SM4_INDEX_TYPE_MASK (0x1 << WINED3D_SM4_INDEX_TYPE_SHIFT)
38 #define WINED3D_SM4_OPCODE_MASK 0xff
40 #define WINED3D_SM4_REGISTER_MODIFIER (1 << 31)
42 #define WINED3D_SM4_ADDRESSING_SHIFT1 25
43 #define WINED3D_SM4_ADDRESSING_MASK1 (0x3 << WINED3D_SM4_ADDRESSING_SHIFT1)
45 #define WINED3D_SM4_ADDRESSING_SHIFT0 22
46 #define WINED3D_SM4_ADDRESSING_MASK0 (0x3 << WINED3D_SM4_ADDRESSING_SHIFT0)
48 #define WINED3D_SM4_REGISTER_ORDER_SHIFT 20
49 #define WINED3D_SM4_REGISTER_ORDER_MASK (0x3 << WINED3D_SM4_REGISTER_ORDER_SHIFT)
51 #define WINED3D_SM4_REGISTER_TYPE_SHIFT 12
52 #define WINED3D_SM4_REGISTER_TYPE_MASK (0xf << WINED3D_SM4_REGISTER_TYPE_SHIFT)
54 #define WINED3D_SM4_SWIZZLE_TYPE_SHIFT 2
55 #define WINED3D_SM4_SWIZZLE_TYPE_MASK (0x3 << WINED3D_SM4_SWIZZLE_TYPE_SHIFT)
57 #define WINED3D_SM4_IMMCONST_TYPE_SHIFT 0
58 #define WINED3D_SM4_IMMCONST_TYPE_MASK (0x3 << WINED3D_SM4_IMMCONST_TYPE_SHIFT)
60 #define WINED3D_SM4_WRITEMASK_SHIFT 4
61 #define WINED3D_SM4_WRITEMASK_MASK (0xf << WINED3D_SM4_WRITEMASK_SHIFT)
63 #define WINED3D_SM4_SWIZZLE_SHIFT 4
64 #define WINED3D_SM4_SWIZZLE_MASK (0xff << WINED3D_SM4_SWIZZLE_SHIFT)
66 #define WINED3D_SM4_VERSION_MAJOR(version) (((version) >> 4) & 0xf)
67 #define WINED3D_SM4_VERSION_MINOR(version) (((version) >> 0) & 0xf)
69 #define WINED3D_SM4_ADDRESSING_RELATIVE 0x2
70 #define WINED3D_SM4_ADDRESSING_OFFSET 0x1
72 enum wined3d_sm4_opcode
74 WINED3D_SM4_OP_ADD = 0x00,
75 WINED3D_SM4_OP_AND = 0x01,
76 WINED3D_SM4_OP_BREAK = 0x02,
77 WINED3D_SM4_OP_BREAKC = 0x03,
78 WINED3D_SM4_OP_CUT = 0x09,
79 WINED3D_SM4_OP_DERIV_RTX = 0x0b,
80 WINED3D_SM4_OP_DERIV_RTY = 0x0c,
81 WINED3D_SM4_OP_DIV = 0x0e,
82 WINED3D_SM4_OP_DP3 = 0x10,
83 WINED3D_SM4_OP_DP4 = 0x11,
84 WINED3D_SM4_OP_EMIT = 0x13,
85 WINED3D_SM4_OP_ENDIF = 0x15,
86 WINED3D_SM4_OP_ENDLOOP = 0x16,
87 WINED3D_SM4_OP_EQ = 0x18,
88 WINED3D_SM4_OP_EXP = 0x19,
89 WINED3D_SM4_OP_FRC = 0x1a,
90 WINED3D_SM4_OP_FTOI = 0x1b,
91 WINED3D_SM4_OP_GE = 0x1d,
92 WINED3D_SM4_OP_IADD = 0x1e,
93 WINED3D_SM4_OP_IF = 0x1f,
94 WINED3D_SM4_OP_IEQ = 0x20,
95 WINED3D_SM4_OP_IGE = 0x21,
96 WINED3D_SM4_OP_IMUL = 0x26,
97 WINED3D_SM4_OP_ITOF = 0x2b,
98 WINED3D_SM4_OP_LD = 0x2d,
99 WINED3D_SM4_OP_LOG = 0x2f,
100 WINED3D_SM4_OP_LOOP = 0x30,
101 WINED3D_SM4_OP_LT = 0x31,
102 WINED3D_SM4_OP_MAD = 0x32,
103 WINED3D_SM4_OP_MIN = 0x33,
104 WINED3D_SM4_OP_MAX = 0x34,
105 WINED3D_SM4_OP_MOV = 0x36,
106 WINED3D_SM4_OP_MOVC = 0x37,
107 WINED3D_SM4_OP_MUL = 0x38,
108 WINED3D_SM4_OP_RET = 0x3e,
109 WINED3D_SM4_OP_ROUND_NI = 0x41,
110 WINED3D_SM4_OP_RSQ = 0x44,
111 WINED3D_SM4_OP_SAMPLE = 0x45,
112 WINED3D_SM4_OP_SAMPLE_LOD = 0x48,
113 WINED3D_SM4_OP_SAMPLE_GRAD = 0x49,
114 WINED3D_SM4_OP_SQRT = 0x4b,
115 WINED3D_SM4_OP_SINCOS = 0x4d,
116 WINED3D_SM4_OP_UDIV = 0x4e,
117 WINED3D_SM4_OP_USHR = 0x55,
118 WINED3D_SM4_OP_UTOF = 0x56,
119 WINED3D_SM4_OP_XOR = 0x57,
120 WINED3D_SM4_OP_DCL_CONSTANT_BUFFER = 0x59,
121 WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY = 0x5c,
122 WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE = 0x5d,
123 WINED3D_SM4_OP_DCL_VERTICES_OUT = 0x5e,
126 enum wined3d_sm4_register_type
128 WINED3D_SM4_RT_TEMP = 0x0,
129 WINED3D_SM4_RT_INPUT = 0x1,
130 WINED3D_SM4_RT_OUTPUT = 0x2,
131 WINED3D_SM4_RT_IMMCONST = 0x4,
132 WINED3D_SM4_RT_SAMPLER = 0x6,
133 WINED3D_SM4_RT_CONSTBUFFER = 0x8,
134 WINED3D_SM4_RT_PRIMID = 0xb,
135 WINED3D_SM4_RT_NULL = 0xd,
138 enum wined3d_sm4_output_primitive_type
140 WINED3D_SM4_OUTPUT_PT_POINTLIST = 0x1,
141 WINED3D_SM4_OUTPUT_PT_LINELIST = 0x3,
142 WINED3D_SM4_OUTPUT_PT_TRIANGLESTRIP = 0x5,
145 enum wined3d_sm4_input_primitive_type
147 WINED3D_SM4_INPUT_PT_POINT = 0x1,
148 WINED3D_SM4_INPUT_PT_LINE = 0x2,
149 WINED3D_SM4_INPUT_PT_TRIANGLE = 0x3,
150 WINED3D_SM4_INPUT_PT_LINEADJ = 0x6,
151 WINED3D_SM4_INPUT_PT_TRIANGLEADJ = 0x7,
154 enum wined3d_sm4_swizzle_type
156 WINED3D_SM4_SWIZZLE_VEC4 = 0x1,
157 WINED3D_SM4_SWIZZLE_SCALAR = 0x2,
160 enum wined3d_sm4_immconst_type
162 WINED3D_SM4_IMMCONST_SCALAR = 0x1,
163 WINED3D_SM4_IMMCONST_VEC4 = 0x2,
166 struct wined3d_shader_src_param_entry
169 struct wined3d_shader_src_param param;
172 struct wined3d_sm4_data
174 struct wined3d_shader_version shader_version;
176 const struct wined3d_shader_signature *output_signature;
178 struct wined3d_shader_src_param src_param[5];
179 struct wined3d_shader_dst_param dst_param[2];
180 struct list src_free;
184 struct wined3d_sm4_opcode_info
186 enum wined3d_sm4_opcode opcode;
187 enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx;
188 const char *dst_info;
189 const char *src_info;
194 enum wined3d_sysval_semantic sysval;
195 enum wined3d_shader_register_type register_type;
200 * F -> WINED3D_DATA_FLOAT
201 * I -> WINED3D_DATA_INT
202 * R -> WINED3D_DATA_RESOURCE
203 * S -> WINED3D_DATA_SAMPLER
204 * U -> WINED3D_DATA_UINT
206 static const struct wined3d_sm4_opcode_info opcode_table[] =
208 {WINED3D_SM4_OP_ADD, WINED3DSIH_ADD, "F", "FF"},
209 {WINED3D_SM4_OP_AND, WINED3DSIH_AND, "U", "UU"},
210 {WINED3D_SM4_OP_BREAK, WINED3DSIH_BREAK, "", ""},
211 {WINED3D_SM4_OP_BREAKC, WINED3DSIH_BREAKP, "", "U"},
212 {WINED3D_SM4_OP_CUT, WINED3DSIH_CUT, "", ""},
213 {WINED3D_SM4_OP_DERIV_RTX, WINED3DSIH_DSX, "F", "F"},
214 {WINED3D_SM4_OP_DERIV_RTY, WINED3DSIH_DSY, "F", "F"},
215 {WINED3D_SM4_OP_DIV, WINED3DSIH_DIV, "F", "FF"},
216 {WINED3D_SM4_OP_DP3, WINED3DSIH_DP3, "F", "FF"},
217 {WINED3D_SM4_OP_DP4, WINED3DSIH_DP4, "F", "FF"},
218 {WINED3D_SM4_OP_EMIT, WINED3DSIH_EMIT, "", ""},
219 {WINED3D_SM4_OP_ENDIF, WINED3DSIH_ENDIF, "", ""},
220 {WINED3D_SM4_OP_ENDLOOP, WINED3DSIH_ENDLOOP, "", ""},
221 {WINED3D_SM4_OP_EQ, WINED3DSIH_EQ, "U", "FF"},
222 {WINED3D_SM4_OP_EXP, WINED3DSIH_EXP, "F", "F"},
223 {WINED3D_SM4_OP_FRC, WINED3DSIH_FRC, "F", "F"},
224 {WINED3D_SM4_OP_FTOI, WINED3DSIH_FTOI, "I", "F"},
225 {WINED3D_SM4_OP_GE, WINED3DSIH_GE, "U", "FF"},
226 {WINED3D_SM4_OP_IADD, WINED3DSIH_IADD, "I", "II"},
227 {WINED3D_SM4_OP_IF, WINED3DSIH_IF, "", "U"},
228 {WINED3D_SM4_OP_IEQ, WINED3DSIH_IEQ, "U", "II"},
229 {WINED3D_SM4_OP_IGE, WINED3DSIH_IGE, "U", "II"},
230 {WINED3D_SM4_OP_IMUL, WINED3DSIH_IMUL, "II", "II"},
231 {WINED3D_SM4_OP_ITOF, WINED3DSIH_ITOF, "F", "I"},
232 {WINED3D_SM4_OP_LD, WINED3DSIH_LD, "U", "FR"},
233 {WINED3D_SM4_OP_LOG, WINED3DSIH_LOG, "F", "F"},
234 {WINED3D_SM4_OP_LOOP, WINED3DSIH_LOOP, "", ""},
235 {WINED3D_SM4_OP_LT, WINED3DSIH_LT, "U", "FF"},
236 {WINED3D_SM4_OP_MAD, WINED3DSIH_MAD, "F", "FFF"},
237 {WINED3D_SM4_OP_MIN, WINED3DSIH_MIN, "F", "FF"},
238 {WINED3D_SM4_OP_MAX, WINED3DSIH_MAX, "F", "FF"},
239 {WINED3D_SM4_OP_MOV, WINED3DSIH_MOV, "F", "F"},
240 {WINED3D_SM4_OP_MOVC, WINED3DSIH_MOVC, "F", "UFF"},
241 {WINED3D_SM4_OP_MUL, WINED3DSIH_MUL, "F", "FF"},
242 {WINED3D_SM4_OP_RET, WINED3DSIH_RET, "", ""},
243 {WINED3D_SM4_OP_ROUND_NI, WINED3DSIH_ROUND_NI, "F", "F"},
244 {WINED3D_SM4_OP_RSQ, WINED3DSIH_RSQ, "F", "F"},
245 {WINED3D_SM4_OP_SAMPLE, WINED3DSIH_SAMPLE, "U", "FRS"},
246 {WINED3D_SM4_OP_SAMPLE_LOD, WINED3DSIH_SAMPLE_LOD, "U", "FRSF"},
247 {WINED3D_SM4_OP_SAMPLE_GRAD, WINED3DSIH_SAMPLE_GRAD, "U", "FRSFF"},
248 {WINED3D_SM4_OP_SQRT, WINED3DSIH_SQRT, "F", "F"},
249 {WINED3D_SM4_OP_SINCOS, WINED3DSIH_SINCOS, "FF", "F"},
250 {WINED3D_SM4_OP_UDIV, WINED3DSIH_UDIV, "UU", "UU"},
251 {WINED3D_SM4_OP_USHR, WINED3DSIH_USHR, "U", "UU"},
252 {WINED3D_SM4_OP_UTOF, WINED3DSIH_UTOF, "F", "U"},
253 {WINED3D_SM4_OP_XOR, WINED3DSIH_XOR, "U", "UU"},
254 {WINED3D_SM4_OP_DCL_CONSTANT_BUFFER, WINED3DSIH_DCL_CONSTANT_BUFFER, "", ""},
255 {WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY, WINED3DSIH_DCL_OUTPUT_TOPOLOGY, "", ""},
256 {WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE, WINED3DSIH_DCL_INPUT_PRIMITIVE, "", ""},
257 {WINED3D_SM4_OP_DCL_VERTICES_OUT, WINED3DSIH_DCL_VERTICES_OUT, "", ""},
260 static const enum wined3d_shader_register_type register_type_table[] =
262 /* WINED3D_SM4_RT_TEMP */ WINED3DSPR_TEMP,
263 /* WINED3D_SM4_RT_INPUT */ WINED3DSPR_INPUT,
264 /* WINED3D_SM4_RT_OUTPUT */ WINED3DSPR_OUTPUT,
266 /* WINED3D_SM4_RT_IMMCONST */ WINED3DSPR_IMMCONST,
268 /* WINED3D_SM4_RT_SAMPLER */ WINED3DSPR_SAMPLER,
269 /* WINED3D_SM4_RT_RESOURCE */ WINED3DSPR_RESOURCE,
270 /* WINED3D_SM4_RT_CONSTBUFFER */ WINED3DSPR_CONSTBUFFER,
273 /* WINED3D_SM4_RT_PRIMID */ WINED3DSPR_PRIMID,
275 /* WINED3D_SM4_RT_NULL */ WINED3DSPR_NULL,
278 static const enum wined3d_primitive_type output_primitive_type_table[] =
280 /* UNKNOWN */ WINED3D_PT_UNDEFINED,
281 /* WINED3D_SM4_OUTPUT_PT_POINTLIST */ WINED3D_PT_POINTLIST,
282 /* UNKNOWN */ WINED3D_PT_UNDEFINED,
283 /* WINED3D_SM4_OUTPUT_PT_LINELIST */ WINED3D_PT_LINELIST,
284 /* UNKNOWN */ WINED3D_PT_UNDEFINED,
285 /* WINED3D_SM4_OUTPUT_PT_TRIANGLESTRIP */ WINED3D_PT_TRIANGLESTRIP,
288 static const enum wined3d_primitive_type input_primitive_type_table[] =
290 /* UNKNOWN */ WINED3D_PT_UNDEFINED,
291 /* WINED3D_SM4_INPUT_PT_POINT */ WINED3D_PT_POINTLIST,
292 /* WINED3D_SM4_INPUT_PT_LINE */ WINED3D_PT_LINELIST,
293 /* WINED3D_SM4_INPUT_PT_TRIANGLE */ WINED3D_PT_TRIANGLELIST,
294 /* UNKNOWN */ WINED3D_PT_UNDEFINED,
295 /* UNKNOWN */ WINED3D_PT_UNDEFINED,
296 /* WINED3D_SM4_INPUT_PT_LINEADJ */ WINED3D_PT_LINELIST_ADJ,
297 /* WINED3D_SM4_INPUT_PT_TRIANGLEADJ */ WINED3D_PT_TRIANGLELIST_ADJ,
300 static const struct sysval_map sysval_map[] =
302 {WINED3D_SV_DEPTH, WINED3DSPR_DEPTHOUT, 0},
303 {WINED3D_SV_TARGET0, WINED3DSPR_COLOROUT, 0},
304 {WINED3D_SV_TARGET1, WINED3DSPR_COLOROUT, 1},
305 {WINED3D_SV_TARGET2, WINED3DSPR_COLOROUT, 2},
306 {WINED3D_SV_TARGET3, WINED3DSPR_COLOROUT, 3},
307 {WINED3D_SV_TARGET4, WINED3DSPR_COLOROUT, 4},
308 {WINED3D_SV_TARGET5, WINED3DSPR_COLOROUT, 5},
309 {WINED3D_SV_TARGET6, WINED3DSPR_COLOROUT, 6},
310 {WINED3D_SV_TARGET7, WINED3DSPR_COLOROUT, 7},
313 static BOOL shader_sm4_read_src_param(struct wined3d_sm4_data *priv, const DWORD **ptr,
314 enum wined3d_data_type data_type, struct wined3d_shader_src_param *src_param);
316 static const struct wined3d_sm4_opcode_info *get_opcode_info(enum wined3d_sm4_opcode opcode)
320 for (i = 0; i < sizeof(opcode_table) / sizeof(*opcode_table); ++i)
322 if (opcode == opcode_table[i].opcode) return &opcode_table[i];
328 static void map_sysval(enum wined3d_sysval_semantic sysval, struct wined3d_shader_register *reg)
332 for (i = 0; i < sizeof(sysval_map) / sizeof(*sysval_map); ++i)
334 if (sysval == sysval_map[i].sysval)
336 reg->type = sysval_map[i].register_type;
337 reg->idx[0].offset = sysval_map[i].register_idx;
342 static void map_register(const struct wined3d_sm4_data *priv, struct wined3d_shader_register *reg)
344 switch (priv->shader_version.type)
346 case WINED3D_SHADER_TYPE_PIXEL:
347 if (reg->type == WINED3DSPR_OUTPUT)
350 const struct wined3d_shader_signature *s = priv->output_signature;
354 ERR("Shader has no output signature, unable to map register.\n");
358 for (i = 0; i < s->element_count; ++i)
360 if (s->elements[i].register_idx == reg->idx[0].offset)
362 map_sysval(s->elements[i].sysval_semantic, reg);
374 static enum wined3d_data_type map_data_type(char t)
379 return WINED3D_DATA_FLOAT;
381 return WINED3D_DATA_INT;
383 return WINED3D_DATA_RESOURCE;
385 return WINED3D_DATA_SAMPLER;
387 return WINED3D_DATA_UINT;
389 ERR("Invalid data type '%c'.\n", t);
390 return WINED3D_DATA_FLOAT;
394 static void *shader_sm4_init(const DWORD *byte_code, const struct wined3d_shader_signature *output_signature)
396 struct wined3d_sm4_data *priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv));
399 ERR("Failed to allocate private data\n");
403 priv->output_signature = output_signature;
404 list_init(&priv->src_free);
405 list_init(&priv->src);
410 static void shader_sm4_free(void *data)
412 struct wined3d_shader_src_param_entry *e1, *e2;
413 struct wined3d_sm4_data *priv = data;
415 list_move_head(&priv->src_free, &priv->src);
416 LIST_FOR_EACH_ENTRY_SAFE(e1, e2, &priv->src_free, struct wined3d_shader_src_param_entry, entry)
418 HeapFree(GetProcessHeap(), 0, e1);
420 HeapFree(GetProcessHeap(), 0, priv);
423 static struct wined3d_shader_src_param *get_src_param(struct wined3d_sm4_data *priv)
425 struct wined3d_shader_src_param_entry *e;
428 if (!list_empty(&priv->src_free))
430 elem = list_head(&priv->src_free);
435 if (!(e = HeapAlloc(GetProcessHeap(), 0, sizeof(*e))))
440 list_add_tail(&priv->src, elem);
441 e = LIST_ENTRY(elem, struct wined3d_shader_src_param_entry, entry);
445 static void shader_sm4_read_header(void *data, const DWORD **ptr, struct wined3d_shader_version *shader_version)
447 struct wined3d_sm4_data *priv = data;
452 version_token = *(*ptr)++;
453 TRACE("version: 0x%08x\n", version_token);
455 TRACE("token count: %u\n", **ptr);
456 priv->end += *(*ptr)++;
458 switch (version_token >> 16)
461 priv->shader_version.type = WINED3D_SHADER_TYPE_PIXEL;
465 priv->shader_version.type = WINED3D_SHADER_TYPE_VERTEX;
469 priv->shader_version.type = WINED3D_SHADER_TYPE_GEOMETRY;
473 FIXME("Unrecognized shader type %#x\n", version_token >> 16);
475 priv->shader_version.major = WINED3D_SM4_VERSION_MAJOR(version_token);
476 priv->shader_version.minor = WINED3D_SM4_VERSION_MINOR(version_token);
478 *shader_version = priv->shader_version;
481 static BOOL shader_sm4_read_reg_idx(struct wined3d_sm4_data *priv, const DWORD **ptr,
482 DWORD addressing, struct wined3d_shader_register_index *reg_idx)
484 if (addressing & WINED3D_SM4_ADDRESSING_RELATIVE)
486 struct wined3d_shader_src_param *rel_addr = get_src_param(priv);
488 if (!(reg_idx->rel_addr = rel_addr))
490 ERR("Failed to get src param for relative addressing.\n");
494 if (addressing & WINED3D_SM4_ADDRESSING_OFFSET)
495 reg_idx->offset = *(*ptr)++;
498 shader_sm4_read_src_param(priv, ptr, WINED3D_DATA_INT, rel_addr);
502 reg_idx->rel_addr = NULL;
503 reg_idx->offset = *(*ptr)++;
509 static BOOL shader_sm4_read_param(struct wined3d_sm4_data *priv, const DWORD **ptr,
510 enum wined3d_data_type data_type, struct wined3d_shader_register *param,
511 enum wined3d_shader_src_modifier *modifier)
513 enum wined3d_sm4_register_type register_type;
514 DWORD token = *(*ptr)++;
517 register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
518 if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
520 FIXME("Unhandled register type %#x.\n", register_type);
521 param->type = WINED3DSPR_TEMP;
525 param->type = register_type_table[register_type];
527 param->data_type = data_type;
529 if (token & WINED3D_SM4_REGISTER_MODIFIER)
533 /* FIXME: This will probably break down at some point. The SM4
534 * modifiers look like flags, while wined3d currently has an enum
535 * with possible combinations, e.g. WINED3DSPSM_ABSNEG. */
539 *modifier = WINED3DSPSM_NEG;
543 *modifier = WINED3DSPSM_ABS;
547 FIXME("Skipping modifier 0x%08x.\n", m);
548 *modifier = WINED3DSPSM_NONE;
554 *modifier = WINED3DSPSM_NONE;
557 order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
560 param->idx[0].offset = ~0U;
563 DWORD addressing = (token & WINED3D_SM4_ADDRESSING_MASK0) >> WINED3D_SM4_ADDRESSING_SHIFT0;
564 if (!(shader_sm4_read_reg_idx(priv, ptr, addressing, ¶m->idx[0])))
566 ERR("Failed to read register index.\n");
572 param->idx[1].offset = ~0U;
575 DWORD addressing = (token & WINED3D_SM4_ADDRESSING_MASK1) >> WINED3D_SM4_ADDRESSING_SHIFT1;
576 if (!(shader_sm4_read_reg_idx(priv, ptr, addressing, ¶m->idx[1])))
578 ERR("Failed to read register index.\n");
584 FIXME("Unhandled order %u.\n", order);
586 if (register_type == WINED3D_SM4_RT_IMMCONST)
588 enum wined3d_sm4_immconst_type immconst_type =
589 (token & WINED3D_SM4_IMMCONST_TYPE_MASK) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT;
591 switch (immconst_type)
593 case WINED3D_SM4_IMMCONST_SCALAR:
594 param->immconst_type = WINED3D_IMMCONST_SCALAR;
595 memcpy(param->immconst_data, *ptr, 1 * sizeof(DWORD));
599 case WINED3D_SM4_IMMCONST_VEC4:
600 param->immconst_type = WINED3D_IMMCONST_VEC4;
601 memcpy(param->immconst_data, *ptr, 4 * sizeof(DWORD));
606 FIXME("Unhandled immediate constant type %#x.\n", immconst_type);
611 map_register(priv, param);
616 static BOOL shader_sm4_read_src_param(struct wined3d_sm4_data *priv, const DWORD **ptr,
617 enum wined3d_data_type data_type, struct wined3d_shader_src_param *src_param)
621 if (!shader_sm4_read_param(priv, ptr, data_type, &src_param->reg, &src_param->modifiers))
623 ERR("Failed to read parameter.\n");
627 if (src_param->reg.type == WINED3DSPR_IMMCONST)
629 src_param->swizzle = WINED3DSP_NOSWIZZLE;
633 enum wined3d_sm4_swizzle_type swizzle_type =
634 (token & WINED3D_SM4_SWIZZLE_TYPE_MASK) >> WINED3D_SM4_SWIZZLE_TYPE_SHIFT;
636 switch (swizzle_type)
638 case WINED3D_SM4_SWIZZLE_SCALAR:
639 src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
640 src_param->swizzle = (src_param->swizzle & 0x3) * 0x55;
643 case WINED3D_SM4_SWIZZLE_VEC4:
644 src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
648 FIXME("Unhandled swizzle type %#x.\n", swizzle_type);
656 static BOOL shader_sm4_read_dst_param(struct wined3d_sm4_data *priv, const DWORD **ptr,
657 enum wined3d_data_type data_type, struct wined3d_shader_dst_param *dst_param)
659 enum wined3d_shader_src_modifier modifier;
662 if (!shader_sm4_read_param(priv, ptr, data_type, &dst_param->reg, &modifier))
664 ERR("Failed to read parameter.\n");
668 if (modifier != WINED3DSPSM_NONE)
670 ERR("Invalid source modifier %#x on destination register.\n", modifier);
674 dst_param->write_mask = (token & WINED3D_SM4_WRITEMASK_MASK) >> WINED3D_SM4_WRITEMASK_SHIFT;
675 dst_param->modifiers = 0;
676 dst_param->shift = 0;
681 static void shader_sm4_read_instruction(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins)
683 const struct wined3d_sm4_opcode_info *opcode_info;
684 struct wined3d_sm4_data *priv = data;
685 DWORD opcode_token, opcode;
689 list_move_head(&priv->src_free, &priv->src);
691 opcode_token = *(*ptr)++;
692 opcode = opcode_token & WINED3D_SM4_OPCODE_MASK;
693 len = ((opcode_token & WINED3D_SM4_INSTRUCTION_LENGTH_MASK) >> WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT) - 1;
695 if (TRACE_ON(d3d_bytecode))
697 TRACE_(d3d_bytecode)("[ %08x ", opcode_token);
698 for (i = 0; i < len; ++i)
700 TRACE_(d3d_bytecode)("%08x ", (*ptr)[i]);
702 TRACE_(d3d_bytecode)("]\n");
705 if (!(opcode_info = get_opcode_info(opcode)))
707 FIXME("Unrecognized opcode %#x, opcode_token 0x%08x.\n", opcode, opcode_token);
708 ins->handler_idx = WINED3DSIH_TABLE_SIZE;
713 ins->handler_idx = opcode_info->handler_idx;
716 ins->predicate = NULL;
717 ins->dst_count = strlen(opcode_info->dst_info);
718 ins->dst = priv->dst_param;
719 ins->src_count = strlen(opcode_info->src_info);
720 ins->src = priv->src_param;
725 if (opcode_token & WINED3D_SM4_INSTRUCTION_MODIFIER)
727 DWORD modifier = *p++;
728 FIXME("Skipping modifier 0x%08x.\n", modifier);
731 if (opcode == WINED3D_SM4_OP_DCL_CONSTANT_BUFFER)
733 shader_sm4_read_src_param(priv, &p, WINED3D_DATA_FLOAT, &ins->declaration.src);
734 if (opcode_token & WINED3D_SM4_INDEX_TYPE_MASK)
735 ins->flags |= WINED3DSI_INDEXED_DYNAMIC;
737 else if (opcode == WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY)
739 enum wined3d_sm4_output_primitive_type primitive_type;
741 primitive_type = (opcode_token & WINED3D_SM4_PRIMITIVE_TYPE_MASK) >> WINED3D_SM4_PRIMITIVE_TYPE_SHIFT;
742 if (primitive_type >= sizeof(output_primitive_type_table) / sizeof(*output_primitive_type_table))
744 FIXME("Unhandled output primitive type %#x.\n", primitive_type);
745 ins->declaration.primitive_type = WINED3D_PT_UNDEFINED;
749 ins->declaration.primitive_type = output_primitive_type_table[primitive_type];
752 else if (opcode == WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE)
754 enum wined3d_sm4_input_primitive_type primitive_type;
756 primitive_type = (opcode_token & WINED3D_SM4_PRIMITIVE_TYPE_MASK) >> WINED3D_SM4_PRIMITIVE_TYPE_SHIFT;
757 if (primitive_type >= sizeof(input_primitive_type_table) / sizeof(*input_primitive_type_table))
759 FIXME("Unhandled input primitive type %#x.\n", primitive_type);
760 ins->declaration.primitive_type = WINED3D_PT_UNDEFINED;
764 ins->declaration.primitive_type = input_primitive_type_table[primitive_type];
767 else if (opcode == WINED3D_SM4_OP_DCL_VERTICES_OUT)
769 ins->declaration.count = *p++;
773 for (i = 0; i < ins->dst_count; ++i)
775 if (!(shader_sm4_read_dst_param(priv, &p, map_data_type(opcode_info->dst_info[i]), &priv->dst_param[i])))
777 ins->handler_idx = WINED3DSIH_TABLE_SIZE;
782 for (i = 0; i < ins->src_count; ++i)
784 if (!(shader_sm4_read_src_param(priv, &p, map_data_type(opcode_info->src_info[i]), &priv->src_param[i])))
786 ins->handler_idx = WINED3DSIH_TABLE_SIZE;
793 static BOOL shader_sm4_is_end(void *data, const DWORD **ptr)
795 struct wined3d_sm4_data *priv = data;
796 return *ptr == priv->end;
799 const struct wined3d_shader_frontend sm4_shader_frontend =
803 shader_sm4_read_header,
804 shader_sm4_read_instruction,