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 (0x1f << 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_DISCARD
= 0x0d,
82 WINED3D_SM4_OP_DIV
= 0x0e,
83 WINED3D_SM4_OP_DP2
= 0x0f,
84 WINED3D_SM4_OP_DP3
= 0x10,
85 WINED3D_SM4_OP_DP4
= 0x11,
86 WINED3D_SM4_OP_EMIT
= 0x13,
87 WINED3D_SM4_OP_ENDIF
= 0x15,
88 WINED3D_SM4_OP_ENDLOOP
= 0x16,
89 WINED3D_SM4_OP_EQ
= 0x18,
90 WINED3D_SM4_OP_EXP
= 0x19,
91 WINED3D_SM4_OP_FRC
= 0x1a,
92 WINED3D_SM4_OP_FTOI
= 0x1b,
93 WINED3D_SM4_OP_GE
= 0x1d,
94 WINED3D_SM4_OP_IADD
= 0x1e,
95 WINED3D_SM4_OP_IF
= 0x1f,
96 WINED3D_SM4_OP_IEQ
= 0x20,
97 WINED3D_SM4_OP_IGE
= 0x21,
98 WINED3D_SM4_OP_IMUL
= 0x26,
99 WINED3D_SM4_OP_ISHL
= 0x29,
100 WINED3D_SM4_OP_ITOF
= 0x2b,
101 WINED3D_SM4_OP_LD
= 0x2d,
102 WINED3D_SM4_OP_LOG
= 0x2f,
103 WINED3D_SM4_OP_LOOP
= 0x30,
104 WINED3D_SM4_OP_LT
= 0x31,
105 WINED3D_SM4_OP_MAD
= 0x32,
106 WINED3D_SM4_OP_MIN
= 0x33,
107 WINED3D_SM4_OP_MAX
= 0x34,
108 WINED3D_SM4_OP_MOV
= 0x36,
109 WINED3D_SM4_OP_MOVC
= 0x37,
110 WINED3D_SM4_OP_MUL
= 0x38,
111 WINED3D_SM4_OP_RET
= 0x3e,
112 WINED3D_SM4_OP_ROUND_NI
= 0x41,
113 WINED3D_SM4_OP_RSQ
= 0x44,
114 WINED3D_SM4_OP_SAMPLE
= 0x45,
115 WINED3D_SM4_OP_SAMPLE_LOD
= 0x48,
116 WINED3D_SM4_OP_SAMPLE_GRAD
= 0x49,
117 WINED3D_SM4_OP_SQRT
= 0x4b,
118 WINED3D_SM4_OP_SINCOS
= 0x4d,
119 WINED3D_SM4_OP_UDIV
= 0x4e,
120 WINED3D_SM4_OP_USHR
= 0x55,
121 WINED3D_SM4_OP_UTOF
= 0x56,
122 WINED3D_SM4_OP_XOR
= 0x57,
123 WINED3D_SM4_OP_DCL_CONSTANT_BUFFER
= 0x59,
124 WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY
= 0x5c,
125 WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE
= 0x5d,
126 WINED3D_SM4_OP_DCL_VERTICES_OUT
= 0x5e,
129 enum wined3d_sm4_register_type
131 WINED3D_SM4_RT_TEMP
= 0x0,
132 WINED3D_SM4_RT_INPUT
= 0x1,
133 WINED3D_SM4_RT_OUTPUT
= 0x2,
134 WINED3D_SM4_RT_IMMCONST
= 0x4,
135 WINED3D_SM4_RT_SAMPLER
= 0x6,
136 WINED3D_SM4_RT_CONSTBUFFER
= 0x8,
137 WINED3D_SM4_RT_PRIMID
= 0xb,
138 WINED3D_SM4_RT_NULL
= 0xd,
141 enum wined3d_sm4_output_primitive_type
143 WINED3D_SM4_OUTPUT_PT_POINTLIST
= 0x1,
144 WINED3D_SM4_OUTPUT_PT_LINELIST
= 0x3,
145 WINED3D_SM4_OUTPUT_PT_TRIANGLESTRIP
= 0x5,
148 enum wined3d_sm4_input_primitive_type
150 WINED3D_SM4_INPUT_PT_POINT
= 0x1,
151 WINED3D_SM4_INPUT_PT_LINE
= 0x2,
152 WINED3D_SM4_INPUT_PT_TRIANGLE
= 0x3,
153 WINED3D_SM4_INPUT_PT_LINEADJ
= 0x6,
154 WINED3D_SM4_INPUT_PT_TRIANGLEADJ
= 0x7,
157 enum wined3d_sm4_swizzle_type
159 WINED3D_SM4_SWIZZLE_VEC4
= 0x1,
160 WINED3D_SM4_SWIZZLE_SCALAR
= 0x2,
163 enum wined3d_sm4_immconst_type
165 WINED3D_SM4_IMMCONST_SCALAR
= 0x1,
166 WINED3D_SM4_IMMCONST_VEC4
= 0x2,
169 struct wined3d_shader_src_param_entry
172 struct wined3d_shader_src_param param
;
175 struct wined3d_sm4_data
177 struct wined3d_shader_version shader_version
;
182 enum wined3d_shader_register_type register_type
;
184 } output_map
[MAX_REG_OUTPUT
];
186 struct wined3d_shader_src_param src_param
[5];
187 struct wined3d_shader_dst_param dst_param
[2];
188 struct list src_free
;
192 struct wined3d_sm4_opcode_info
194 enum wined3d_sm4_opcode opcode
;
195 enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx
;
196 const char *dst_info
;
197 const char *src_info
;
202 enum wined3d_sysval_semantic sysval
;
203 enum wined3d_shader_register_type register_type
;
208 * F -> WINED3D_DATA_FLOAT
209 * I -> WINED3D_DATA_INT
210 * R -> WINED3D_DATA_RESOURCE
211 * S -> WINED3D_DATA_SAMPLER
212 * U -> WINED3D_DATA_UINT
214 static const struct wined3d_sm4_opcode_info opcode_table
[] =
216 {WINED3D_SM4_OP_ADD
, WINED3DSIH_ADD
, "F", "FF"},
217 {WINED3D_SM4_OP_AND
, WINED3DSIH_AND
, "U", "UU"},
218 {WINED3D_SM4_OP_BREAK
, WINED3DSIH_BREAK
, "", ""},
219 {WINED3D_SM4_OP_BREAKC
, WINED3DSIH_BREAKP
, "", "U"},
220 {WINED3D_SM4_OP_CUT
, WINED3DSIH_CUT
, "", ""},
221 {WINED3D_SM4_OP_DERIV_RTX
, WINED3DSIH_DSX
, "F", "F"},
222 {WINED3D_SM4_OP_DERIV_RTY
, WINED3DSIH_DSY
, "F", "F"},
223 {WINED3D_SM4_OP_DISCARD
, WINED3DSIH_TEXKILL
, "", "U"},
224 {WINED3D_SM4_OP_DIV
, WINED3DSIH_DIV
, "F", "FF"},
225 {WINED3D_SM4_OP_DP2
, WINED3DSIH_DP2
, "F", "FF"},
226 {WINED3D_SM4_OP_DP3
, WINED3DSIH_DP3
, "F", "FF"},
227 {WINED3D_SM4_OP_DP4
, WINED3DSIH_DP4
, "F", "FF"},
228 {WINED3D_SM4_OP_EMIT
, WINED3DSIH_EMIT
, "", ""},
229 {WINED3D_SM4_OP_ENDIF
, WINED3DSIH_ENDIF
, "", ""},
230 {WINED3D_SM4_OP_ENDLOOP
, WINED3DSIH_ENDLOOP
, "", ""},
231 {WINED3D_SM4_OP_EQ
, WINED3DSIH_EQ
, "U", "FF"},
232 {WINED3D_SM4_OP_EXP
, WINED3DSIH_EXP
, "F", "F"},
233 {WINED3D_SM4_OP_FRC
, WINED3DSIH_FRC
, "F", "F"},
234 {WINED3D_SM4_OP_FTOI
, WINED3DSIH_FTOI
, "I", "F"},
235 {WINED3D_SM4_OP_GE
, WINED3DSIH_GE
, "U", "FF"},
236 {WINED3D_SM4_OP_IADD
, WINED3DSIH_IADD
, "I", "II"},
237 {WINED3D_SM4_OP_IF
, WINED3DSIH_IF
, "", "U"},
238 {WINED3D_SM4_OP_IEQ
, WINED3DSIH_IEQ
, "U", "II"},
239 {WINED3D_SM4_OP_IGE
, WINED3DSIH_IGE
, "U", "II"},
240 {WINED3D_SM4_OP_IMUL
, WINED3DSIH_IMUL
, "II", "II"},
241 {WINED3D_SM4_OP_ISHL
, WINED3DSIH_ISHL
, "I", "II"},
242 {WINED3D_SM4_OP_ITOF
, WINED3DSIH_ITOF
, "F", "I"},
243 {WINED3D_SM4_OP_LD
, WINED3DSIH_LD
, "U", "FR"},
244 {WINED3D_SM4_OP_LOG
, WINED3DSIH_LOG
, "F", "F"},
245 {WINED3D_SM4_OP_LOOP
, WINED3DSIH_LOOP
, "", ""},
246 {WINED3D_SM4_OP_LT
, WINED3DSIH_LT
, "U", "FF"},
247 {WINED3D_SM4_OP_MAD
, WINED3DSIH_MAD
, "F", "FFF"},
248 {WINED3D_SM4_OP_MIN
, WINED3DSIH_MIN
, "F", "FF"},
249 {WINED3D_SM4_OP_MAX
, WINED3DSIH_MAX
, "F", "FF"},
250 {WINED3D_SM4_OP_MOV
, WINED3DSIH_MOV
, "F", "F"},
251 {WINED3D_SM4_OP_MOVC
, WINED3DSIH_MOVC
, "F", "UFF"},
252 {WINED3D_SM4_OP_MUL
, WINED3DSIH_MUL
, "F", "FF"},
253 {WINED3D_SM4_OP_RET
, WINED3DSIH_RET
, "", ""},
254 {WINED3D_SM4_OP_ROUND_NI
, WINED3DSIH_ROUND_NI
, "F", "F"},
255 {WINED3D_SM4_OP_RSQ
, WINED3DSIH_RSQ
, "F", "F"},
256 {WINED3D_SM4_OP_SAMPLE
, WINED3DSIH_SAMPLE
, "U", "FRS"},
257 {WINED3D_SM4_OP_SAMPLE_LOD
, WINED3DSIH_SAMPLE_LOD
, "U", "FRSF"},
258 {WINED3D_SM4_OP_SAMPLE_GRAD
, WINED3DSIH_SAMPLE_GRAD
, "U", "FRSFF"},
259 {WINED3D_SM4_OP_SQRT
, WINED3DSIH_SQRT
, "F", "F"},
260 {WINED3D_SM4_OP_SINCOS
, WINED3DSIH_SINCOS
, "FF", "F"},
261 {WINED3D_SM4_OP_UDIV
, WINED3DSIH_UDIV
, "UU", "UU"},
262 {WINED3D_SM4_OP_USHR
, WINED3DSIH_USHR
, "U", "UU"},
263 {WINED3D_SM4_OP_UTOF
, WINED3DSIH_UTOF
, "F", "U"},
264 {WINED3D_SM4_OP_XOR
, WINED3DSIH_XOR
, "U", "UU"},
265 {WINED3D_SM4_OP_DCL_CONSTANT_BUFFER
, WINED3DSIH_DCL_CONSTANT_BUFFER
, "", ""},
266 {WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY
, WINED3DSIH_DCL_OUTPUT_TOPOLOGY
, "", ""},
267 {WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE
, WINED3DSIH_DCL_INPUT_PRIMITIVE
, "", ""},
268 {WINED3D_SM4_OP_DCL_VERTICES_OUT
, WINED3DSIH_DCL_VERTICES_OUT
, "", ""},
271 static const enum wined3d_shader_register_type register_type_table
[] =
273 /* WINED3D_SM4_RT_TEMP */ WINED3DSPR_TEMP
,
274 /* WINED3D_SM4_RT_INPUT */ WINED3DSPR_INPUT
,
275 /* WINED3D_SM4_RT_OUTPUT */ WINED3DSPR_OUTPUT
,
277 /* WINED3D_SM4_RT_IMMCONST */ WINED3DSPR_IMMCONST
,
279 /* WINED3D_SM4_RT_SAMPLER */ WINED3DSPR_SAMPLER
,
280 /* WINED3D_SM4_RT_RESOURCE */ WINED3DSPR_RESOURCE
,
281 /* WINED3D_SM4_RT_CONSTBUFFER */ WINED3DSPR_CONSTBUFFER
,
284 /* WINED3D_SM4_RT_PRIMID */ WINED3DSPR_PRIMID
,
286 /* WINED3D_SM4_RT_NULL */ WINED3DSPR_NULL
,
289 static const enum wined3d_primitive_type output_primitive_type_table
[] =
291 /* UNKNOWN */ WINED3D_PT_UNDEFINED
,
292 /* WINED3D_SM4_OUTPUT_PT_POINTLIST */ WINED3D_PT_POINTLIST
,
293 /* UNKNOWN */ WINED3D_PT_UNDEFINED
,
294 /* WINED3D_SM4_OUTPUT_PT_LINELIST */ WINED3D_PT_LINELIST
,
295 /* UNKNOWN */ WINED3D_PT_UNDEFINED
,
296 /* WINED3D_SM4_OUTPUT_PT_TRIANGLESTRIP */ WINED3D_PT_TRIANGLESTRIP
,
299 static const enum wined3d_primitive_type input_primitive_type_table
[] =
301 /* UNKNOWN */ WINED3D_PT_UNDEFINED
,
302 /* WINED3D_SM4_INPUT_PT_POINT */ WINED3D_PT_POINTLIST
,
303 /* WINED3D_SM4_INPUT_PT_LINE */ WINED3D_PT_LINELIST
,
304 /* WINED3D_SM4_INPUT_PT_TRIANGLE */ WINED3D_PT_TRIANGLELIST
,
305 /* UNKNOWN */ WINED3D_PT_UNDEFINED
,
306 /* UNKNOWN */ WINED3D_PT_UNDEFINED
,
307 /* WINED3D_SM4_INPUT_PT_LINEADJ */ WINED3D_PT_LINELIST_ADJ
,
308 /* WINED3D_SM4_INPUT_PT_TRIANGLEADJ */ WINED3D_PT_TRIANGLELIST_ADJ
,
311 static const struct sysval_map sysval_map
[] =
313 {WINED3D_SV_DEPTH
, WINED3DSPR_DEPTHOUT
, 0},
314 {WINED3D_SV_TARGET0
, WINED3DSPR_COLOROUT
, 0},
315 {WINED3D_SV_TARGET1
, WINED3DSPR_COLOROUT
, 1},
316 {WINED3D_SV_TARGET2
, WINED3DSPR_COLOROUT
, 2},
317 {WINED3D_SV_TARGET3
, WINED3DSPR_COLOROUT
, 3},
318 {WINED3D_SV_TARGET4
, WINED3DSPR_COLOROUT
, 4},
319 {WINED3D_SV_TARGET5
, WINED3DSPR_COLOROUT
, 5},
320 {WINED3D_SV_TARGET6
, WINED3DSPR_COLOROUT
, 6},
321 {WINED3D_SV_TARGET7
, WINED3DSPR_COLOROUT
, 7},
324 static BOOL
shader_sm4_read_src_param(struct wined3d_sm4_data
*priv
, const DWORD
**ptr
,
325 enum wined3d_data_type data_type
, struct wined3d_shader_src_param
*src_param
);
327 static const struct wined3d_sm4_opcode_info
*get_opcode_info(enum wined3d_sm4_opcode opcode
)
331 for (i
= 0; i
< sizeof(opcode_table
) / sizeof(*opcode_table
); ++i
)
333 if (opcode
== opcode_table
[i
].opcode
) return &opcode_table
[i
];
339 static void map_register(const struct wined3d_sm4_data
*priv
, struct wined3d_shader_register
*reg
)
341 switch (priv
->shader_version
.type
)
343 case WINED3D_SHADER_TYPE_PIXEL
:
344 if (reg
->type
== WINED3DSPR_OUTPUT
)
346 unsigned int reg_idx
= reg
->idx
[0].offset
;
348 if (reg_idx
>= ARRAY_SIZE(priv
->output_map
))
350 ERR("Invalid output index %u.\n", reg_idx
);
354 reg
->type
= priv
->output_map
[reg_idx
].register_type
;
355 reg
->idx
[0].offset
= priv
->output_map
[reg_idx
].register_idx
;
364 static enum wined3d_data_type
map_data_type(char t
)
369 return WINED3D_DATA_FLOAT
;
371 return WINED3D_DATA_INT
;
373 return WINED3D_DATA_RESOURCE
;
375 return WINED3D_DATA_SAMPLER
;
377 return WINED3D_DATA_UINT
;
379 ERR("Invalid data type '%c'.\n", t
);
380 return WINED3D_DATA_FLOAT
;
384 static void *shader_sm4_init(const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
)
386 struct wined3d_sm4_data
*priv
;
389 if (!(priv
= HeapAlloc(GetProcessHeap(), 0, sizeof(*priv
))))
391 ERR("Failed to allocate private data\n");
395 memset(priv
->output_map
, 0xff, sizeof(priv
->output_map
));
396 for (i
= 0; i
< output_signature
->element_count
; ++i
)
398 struct wined3d_shader_signature_element
*e
= &output_signature
->elements
[i
];
400 if (e
->register_idx
>= ARRAY_SIZE(priv
->output_map
))
402 WARN("Invalid output index %u.\n", e
->register_idx
);
406 for (j
= 0; j
< ARRAY_SIZE(sysval_map
); ++j
)
408 if (e
->sysval_semantic
== sysval_map
[j
].sysval
)
410 priv
->output_map
[e
->register_idx
].register_type
= sysval_map
[j
].register_type
;
411 priv
->output_map
[e
->register_idx
].register_idx
= sysval_map
[j
].register_idx
;
417 list_init(&priv
->src_free
);
418 list_init(&priv
->src
);
423 static void shader_sm4_free(void *data
)
425 struct wined3d_shader_src_param_entry
*e1
, *e2
;
426 struct wined3d_sm4_data
*priv
= data
;
428 list_move_head(&priv
->src_free
, &priv
->src
);
429 LIST_FOR_EACH_ENTRY_SAFE(e1
, e2
, &priv
->src_free
, struct wined3d_shader_src_param_entry
, entry
)
431 HeapFree(GetProcessHeap(), 0, e1
);
433 HeapFree(GetProcessHeap(), 0, priv
);
436 static struct wined3d_shader_src_param
*get_src_param(struct wined3d_sm4_data
*priv
)
438 struct wined3d_shader_src_param_entry
*e
;
441 if (!list_empty(&priv
->src_free
))
443 elem
= list_head(&priv
->src_free
);
448 if (!(e
= HeapAlloc(GetProcessHeap(), 0, sizeof(*e
))))
453 list_add_tail(&priv
->src
, elem
);
454 e
= LIST_ENTRY(elem
, struct wined3d_shader_src_param_entry
, entry
);
458 static void shader_sm4_read_header(void *data
, const DWORD
**ptr
, struct wined3d_shader_version
*shader_version
)
460 struct wined3d_sm4_data
*priv
= data
;
465 version_token
= *(*ptr
)++;
466 TRACE("version: 0x%08x\n", version_token
);
468 TRACE("token count: %u\n", **ptr
);
469 priv
->end
+= *(*ptr
)++;
471 switch (version_token
>> 16)
474 priv
->shader_version
.type
= WINED3D_SHADER_TYPE_PIXEL
;
478 priv
->shader_version
.type
= WINED3D_SHADER_TYPE_VERTEX
;
482 priv
->shader_version
.type
= WINED3D_SHADER_TYPE_GEOMETRY
;
486 FIXME("Unrecognized shader type %#x\n", version_token
>> 16);
488 priv
->shader_version
.major
= WINED3D_SM4_VERSION_MAJOR(version_token
);
489 priv
->shader_version
.minor
= WINED3D_SM4_VERSION_MINOR(version_token
);
491 *shader_version
= priv
->shader_version
;
494 static BOOL
shader_sm4_read_reg_idx(struct wined3d_sm4_data
*priv
, const DWORD
**ptr
,
495 DWORD addressing
, struct wined3d_shader_register_index
*reg_idx
)
497 if (addressing
& WINED3D_SM4_ADDRESSING_RELATIVE
)
499 struct wined3d_shader_src_param
*rel_addr
= get_src_param(priv
);
501 if (!(reg_idx
->rel_addr
= rel_addr
))
503 ERR("Failed to get src param for relative addressing.\n");
507 if (addressing
& WINED3D_SM4_ADDRESSING_OFFSET
)
508 reg_idx
->offset
= *(*ptr
)++;
511 shader_sm4_read_src_param(priv
, ptr
, WINED3D_DATA_INT
, rel_addr
);
515 reg_idx
->rel_addr
= NULL
;
516 reg_idx
->offset
= *(*ptr
)++;
522 static BOOL
shader_sm4_read_param(struct wined3d_sm4_data
*priv
, const DWORD
**ptr
,
523 enum wined3d_data_type data_type
, struct wined3d_shader_register
*param
,
524 enum wined3d_shader_src_modifier
*modifier
)
526 enum wined3d_sm4_register_type register_type
;
527 DWORD token
= *(*ptr
)++;
530 register_type
= (token
& WINED3D_SM4_REGISTER_TYPE_MASK
) >> WINED3D_SM4_REGISTER_TYPE_SHIFT
;
531 if (register_type
>= sizeof(register_type_table
) / sizeof(*register_type_table
))
533 FIXME("Unhandled register type %#x.\n", register_type
);
534 param
->type
= WINED3DSPR_TEMP
;
538 param
->type
= register_type_table
[register_type
];
540 param
->data_type
= data_type
;
542 if (token
& WINED3D_SM4_REGISTER_MODIFIER
)
546 /* FIXME: This will probably break down at some point. The SM4
547 * modifiers look like flags, while wined3d currently has an enum
548 * with possible combinations, e.g. WINED3DSPSM_ABSNEG. */
552 *modifier
= WINED3DSPSM_NEG
;
556 *modifier
= WINED3DSPSM_ABS
;
560 FIXME("Skipping modifier 0x%08x.\n", m
);
561 *modifier
= WINED3DSPSM_NONE
;
567 *modifier
= WINED3DSPSM_NONE
;
570 order
= (token
& WINED3D_SM4_REGISTER_ORDER_MASK
) >> WINED3D_SM4_REGISTER_ORDER_SHIFT
;
573 param
->idx
[0].offset
= ~0U;
576 DWORD addressing
= (token
& WINED3D_SM4_ADDRESSING_MASK0
) >> WINED3D_SM4_ADDRESSING_SHIFT0
;
577 if (!(shader_sm4_read_reg_idx(priv
, ptr
, addressing
, ¶m
->idx
[0])))
579 ERR("Failed to read register index.\n");
585 param
->idx
[1].offset
= ~0U;
588 DWORD addressing
= (token
& WINED3D_SM4_ADDRESSING_MASK1
) >> WINED3D_SM4_ADDRESSING_SHIFT1
;
589 if (!(shader_sm4_read_reg_idx(priv
, ptr
, addressing
, ¶m
->idx
[1])))
591 ERR("Failed to read register index.\n");
597 FIXME("Unhandled order %u.\n", order
);
599 if (register_type
== WINED3D_SM4_RT_IMMCONST
)
601 enum wined3d_sm4_immconst_type immconst_type
=
602 (token
& WINED3D_SM4_IMMCONST_TYPE_MASK
) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT
;
604 switch (immconst_type
)
606 case WINED3D_SM4_IMMCONST_SCALAR
:
607 param
->immconst_type
= WINED3D_IMMCONST_SCALAR
;
608 memcpy(param
->immconst_data
, *ptr
, 1 * sizeof(DWORD
));
612 case WINED3D_SM4_IMMCONST_VEC4
:
613 param
->immconst_type
= WINED3D_IMMCONST_VEC4
;
614 memcpy(param
->immconst_data
, *ptr
, 4 * sizeof(DWORD
));
619 FIXME("Unhandled immediate constant type %#x.\n", immconst_type
);
624 map_register(priv
, param
);
629 static BOOL
shader_sm4_read_src_param(struct wined3d_sm4_data
*priv
, const DWORD
**ptr
,
630 enum wined3d_data_type data_type
, struct wined3d_shader_src_param
*src_param
)
634 if (!shader_sm4_read_param(priv
, ptr
, data_type
, &src_param
->reg
, &src_param
->modifiers
))
636 ERR("Failed to read parameter.\n");
640 if (src_param
->reg
.type
== WINED3DSPR_IMMCONST
)
642 src_param
->swizzle
= WINED3DSP_NOSWIZZLE
;
646 enum wined3d_sm4_swizzle_type swizzle_type
=
647 (token
& WINED3D_SM4_SWIZZLE_TYPE_MASK
) >> WINED3D_SM4_SWIZZLE_TYPE_SHIFT
;
649 switch (swizzle_type
)
651 case WINED3D_SM4_SWIZZLE_SCALAR
:
652 src_param
->swizzle
= (token
& WINED3D_SM4_SWIZZLE_MASK
) >> WINED3D_SM4_SWIZZLE_SHIFT
;
653 src_param
->swizzle
= (src_param
->swizzle
& 0x3) * 0x55;
656 case WINED3D_SM4_SWIZZLE_VEC4
:
657 src_param
->swizzle
= (token
& WINED3D_SM4_SWIZZLE_MASK
) >> WINED3D_SM4_SWIZZLE_SHIFT
;
661 FIXME("Unhandled swizzle type %#x.\n", swizzle_type
);
669 static BOOL
shader_sm4_read_dst_param(struct wined3d_sm4_data
*priv
, const DWORD
**ptr
,
670 enum wined3d_data_type data_type
, struct wined3d_shader_dst_param
*dst_param
)
672 enum wined3d_shader_src_modifier modifier
;
675 if (!shader_sm4_read_param(priv
, ptr
, data_type
, &dst_param
->reg
, &modifier
))
677 ERR("Failed to read parameter.\n");
681 if (modifier
!= WINED3DSPSM_NONE
)
683 ERR("Invalid source modifier %#x on destination register.\n", modifier
);
687 dst_param
->write_mask
= (token
& WINED3D_SM4_WRITEMASK_MASK
) >> WINED3D_SM4_WRITEMASK_SHIFT
;
688 dst_param
->modifiers
= 0;
689 dst_param
->shift
= 0;
694 static void shader_sm4_read_instruction(void *data
, const DWORD
**ptr
, struct wined3d_shader_instruction
*ins
)
696 const struct wined3d_sm4_opcode_info
*opcode_info
;
697 struct wined3d_sm4_data
*priv
= data
;
698 DWORD opcode_token
, opcode
;
702 list_move_head(&priv
->src_free
, &priv
->src
);
704 opcode_token
= *(*ptr
)++;
705 opcode
= opcode_token
& WINED3D_SM4_OPCODE_MASK
;
706 len
= ((opcode_token
& WINED3D_SM4_INSTRUCTION_LENGTH_MASK
) >> WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT
) - 1;
708 if (TRACE_ON(d3d_bytecode
))
710 TRACE_(d3d_bytecode
)("[ %08x ", opcode_token
);
711 for (i
= 0; i
< len
; ++i
)
713 TRACE_(d3d_bytecode
)("%08x ", (*ptr
)[i
]);
715 TRACE_(d3d_bytecode
)("]\n");
718 if (!(opcode_info
= get_opcode_info(opcode
)))
720 FIXME("Unrecognized opcode %#x, opcode_token 0x%08x.\n", opcode
, opcode_token
);
721 ins
->handler_idx
= WINED3DSIH_TABLE_SIZE
;
726 ins
->handler_idx
= opcode_info
->handler_idx
;
729 ins
->predicate
= NULL
;
730 ins
->dst_count
= strlen(opcode_info
->dst_info
);
731 ins
->dst
= priv
->dst_param
;
732 ins
->src_count
= strlen(opcode_info
->src_info
);
733 ins
->src
= priv
->src_param
;
738 if (opcode_token
& WINED3D_SM4_INSTRUCTION_MODIFIER
)
740 DWORD modifier
= *p
++;
741 FIXME("Skipping modifier 0x%08x.\n", modifier
);
744 if (opcode
== WINED3D_SM4_OP_DCL_CONSTANT_BUFFER
)
746 shader_sm4_read_src_param(priv
, &p
, WINED3D_DATA_FLOAT
, &ins
->declaration
.src
);
747 if (opcode_token
& WINED3D_SM4_INDEX_TYPE_MASK
)
748 ins
->flags
|= WINED3DSI_INDEXED_DYNAMIC
;
750 else if (opcode
== WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY
)
752 enum wined3d_sm4_output_primitive_type primitive_type
;
754 primitive_type
= (opcode_token
& WINED3D_SM4_PRIMITIVE_TYPE_MASK
) >> WINED3D_SM4_PRIMITIVE_TYPE_SHIFT
;
755 if (primitive_type
>= sizeof(output_primitive_type_table
) / sizeof(*output_primitive_type_table
))
757 FIXME("Unhandled output primitive type %#x.\n", primitive_type
);
758 ins
->declaration
.primitive_type
= WINED3D_PT_UNDEFINED
;
762 ins
->declaration
.primitive_type
= output_primitive_type_table
[primitive_type
];
765 else if (opcode
== WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE
)
767 enum wined3d_sm4_input_primitive_type primitive_type
;
769 primitive_type
= (opcode_token
& WINED3D_SM4_PRIMITIVE_TYPE_MASK
) >> WINED3D_SM4_PRIMITIVE_TYPE_SHIFT
;
770 if (primitive_type
>= sizeof(input_primitive_type_table
) / sizeof(*input_primitive_type_table
))
772 FIXME("Unhandled input primitive type %#x.\n", primitive_type
);
773 ins
->declaration
.primitive_type
= WINED3D_PT_UNDEFINED
;
777 ins
->declaration
.primitive_type
= input_primitive_type_table
[primitive_type
];
780 else if (opcode
== WINED3D_SM4_OP_DCL_VERTICES_OUT
)
782 ins
->declaration
.count
= *p
++;
786 for (i
= 0; i
< ins
->dst_count
; ++i
)
788 if (!(shader_sm4_read_dst_param(priv
, &p
, map_data_type(opcode_info
->dst_info
[i
]), &priv
->dst_param
[i
])))
790 ins
->handler_idx
= WINED3DSIH_TABLE_SIZE
;
795 for (i
= 0; i
< ins
->src_count
; ++i
)
797 if (!(shader_sm4_read_src_param(priv
, &p
, map_data_type(opcode_info
->src_info
[i
]), &priv
->src_param
[i
])))
799 ins
->handler_idx
= WINED3DSIH_TABLE_SIZE
;
806 static BOOL
shader_sm4_is_end(void *data
, const DWORD
**ptr
)
808 struct wined3d_sm4_data
*priv
= data
;
809 return *ptr
== priv
->end
;
812 const struct wined3d_shader_frontend sm4_shader_frontend
=
816 shader_sm4_read_header
,
817 shader_sm4_read_instruction
,