wined3d: Recognize the SM4 break opcode.
[wine/multimedia.git] / dlls / wined3d / shader_sm4.c
blobb47384431b22eb624de603f3f7296ddba1759ff9
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);
26 #define WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT 24
27 #define WINED3D_SM4_INSTRUCTION_LENGTH_MASK (0xf << WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT)
29 #define WINED3D_SM4_OPCODE_MASK 0xff
31 #define WINED3D_SM4_REGISTER_ORDER_SHIFT 20
32 #define WINED3D_SM4_REGISTER_ORDER_MASK (0x3 << WINED3D_SM4_REGISTER_ORDER_SHIFT)
34 #define WINED3D_SM4_REGISTER_TYPE_SHIFT 12
35 #define WINED3D_SM4_REGISTER_TYPE_MASK (0xf << WINED3D_SM4_REGISTER_TYPE_SHIFT)
37 #define WINED3D_SM4_IMMCONST_TYPE_SHIFT 0
38 #define WINED3D_SM4_IMMCONST_TYPE_MASK (0x3 << WINED3D_SM4_IMMCONST_TYPE_SHIFT)
40 #define WINED3D_SM4_WRITEMASK_SHIFT 4
41 #define WINED3D_SM4_WRITEMASK_MASK (0xf << WINED3D_SM4_WRITEMASK_SHIFT)
43 #define WINED3D_SM4_SWIZZLE_SHIFT 4
44 #define WINED3D_SM4_SWIZZLE_MASK (0xff << WINED3D_SM4_SWIZZLE_SHIFT)
46 #define WINED3D_SM4_VERSION_MAJOR(version) (((version) >> 4) & 0xf)
47 #define WINED3D_SM4_VERSION_MINOR(version) (((version) >> 0) & 0xf)
49 enum wined3d_sm4_opcode
51 WINED3D_SM4_OP_ADD = 0x00,
52 WINED3D_SM4_OP_BREAK = 0x02,
53 WINED3D_SM4_OP_BREAKC = 0x03,
54 WINED3D_SM4_OP_DP3 = 0x10,
55 WINED3D_SM4_OP_DP4 = 0x11,
56 WINED3D_SM4_OP_EMIT = 0x13,
57 WINED3D_SM4_OP_EXP = 0x19,
58 WINED3D_SM4_OP_IADD = 0x1e,
59 WINED3D_SM4_OP_IF = 0x1f,
60 WINED3D_SM4_OP_IGE = 0x21,
61 WINED3D_SM4_OP_LOG = 0x2f,
62 WINED3D_SM4_OP_LT = 0x31,
63 WINED3D_SM4_OP_MIN = 0x33,
64 WINED3D_SM4_OP_MAX = 0x34,
65 WINED3D_SM4_OP_MOV = 0x36,
66 WINED3D_SM4_OP_MUL = 0x38,
67 WINED3D_SM4_OP_RET = 0x3e,
68 WINED3D_SM4_OP_RSQ = 0x44,
69 WINED3D_SM4_OP_SINCOS = 0x4d,
72 enum wined3d_sm4_register_type
74 WINED3D_SM4_RT_TEMP = 0x0,
75 WINED3D_SM4_RT_INPUT = 0x1,
76 WINED3D_SM4_RT_OUTPUT = 0x2,
77 WINED3D_SM4_RT_IMMCONST = 0x4,
78 WINED3D_SM4_RT_CONSTBUFFER = 0x8,
81 enum wined3d_sm4_immconst_type
83 WINED3D_SM4_IMMCONST_FLOAT = 0x1,
84 WINED3D_SM4_IMMCONST_FLOAT4 = 0x2,
87 struct wined3d_sm4_data
89 struct wined3d_shader_version shader_version;
90 const DWORD *end;
91 const struct wined3d_shader_signature *output_signature;
94 struct wined3d_sm4_opcode_info
96 enum wined3d_sm4_opcode opcode;
97 enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx;
98 UINT dst_count;
99 UINT src_count;
102 struct sysval_map
104 enum wined3d_sysval_semantic sysval;
105 WINED3DSHADER_PARAM_REGISTER_TYPE register_type;
106 UINT register_idx;
109 static const struct wined3d_sm4_opcode_info opcode_table[] =
111 {WINED3D_SM4_OP_ADD, WINED3DSIH_ADD, 1, 2},
112 {WINED3D_SM4_OP_BREAK, WINED3DSIH_BREAK, 0, 0},
113 {WINED3D_SM4_OP_BREAKC, WINED3DSIH_BREAKP, 0, 1},
114 {WINED3D_SM4_OP_DP3, WINED3DSIH_DP3, 1, 2},
115 {WINED3D_SM4_OP_DP4, WINED3DSIH_DP4, 1, 2},
116 {WINED3D_SM4_OP_EMIT, WINED3DSIH_EMIT, 0, 0},
117 {WINED3D_SM4_OP_EXP, WINED3DSIH_EXP, 1, 1},
118 {WINED3D_SM4_OP_IADD, WINED3DSIH_IADD, 1, 2},
119 {WINED3D_SM4_OP_IF, WINED3DSIH_IF, 0, 1},
120 {WINED3D_SM4_OP_IGE, WINED3DSIH_IGE, 1, 2},
121 {WINED3D_SM4_OP_LOG, WINED3DSIH_LOG, 1, 1},
122 {WINED3D_SM4_OP_LT, WINED3DSIH_LT, 1, 2},
123 {WINED3D_SM4_OP_MIN, WINED3DSIH_MIN, 1, 2},
124 {WINED3D_SM4_OP_MAX, WINED3DSIH_MAX, 1, 2},
125 {WINED3D_SM4_OP_MOV, WINED3DSIH_MOV, 1, 1},
126 {WINED3D_SM4_OP_MUL, WINED3DSIH_MUL, 1, 2},
127 {WINED3D_SM4_OP_RET, WINED3DSIH_RET, 0, 0},
128 {WINED3D_SM4_OP_RSQ, WINED3DSIH_RSQ, 1, 1},
129 {WINED3D_SM4_OP_SINCOS, WINED3DSIH_SINCOS, 1, 2},
132 static const WINED3DSHADER_PARAM_REGISTER_TYPE register_type_table[] =
134 /* WINED3D_SM4_RT_TEMP */ WINED3DSPR_TEMP,
135 /* WINED3D_SM4_RT_INPUT */ WINED3DSPR_INPUT,
136 /* WINED3D_SM4_RT_OUTPUT */ WINED3DSPR_OUTPUT,
137 /* UNKNOWN */ 0,
138 /* WINED3D_SM4_RT_IMMCONST */ WINED3DSPR_IMMCONST,
139 /* UNKNOWN */ 0,
140 /* UNKNOWN */ 0,
141 /* UNKNOWN */ 0,
142 /* WINED3D_SM4_RT_CONSTBUFFER */ WINED3DSPR_CONSTBUFFER,
145 static const struct sysval_map sysval_map[] =
147 {WINED3D_SV_DEPTH, WINED3DSPR_DEPTHOUT, 0},
148 {WINED3D_SV_TARGET0, WINED3DSPR_COLOROUT, 0},
149 {WINED3D_SV_TARGET1, WINED3DSPR_COLOROUT, 1},
150 {WINED3D_SV_TARGET2, WINED3DSPR_COLOROUT, 2},
151 {WINED3D_SV_TARGET3, WINED3DSPR_COLOROUT, 3},
152 {WINED3D_SV_TARGET4, WINED3DSPR_COLOROUT, 4},
153 {WINED3D_SV_TARGET5, WINED3DSPR_COLOROUT, 5},
154 {WINED3D_SV_TARGET6, WINED3DSPR_COLOROUT, 6},
155 {WINED3D_SV_TARGET7, WINED3DSPR_COLOROUT, 7},
158 static const struct wined3d_sm4_opcode_info *get_opcode_info(enum wined3d_sm4_opcode opcode)
160 unsigned int i;
162 for (i = 0; i < sizeof(opcode_table) / sizeof(*opcode_table); ++i)
164 if (opcode == opcode_table[i].opcode) return &opcode_table[i];
167 return NULL;
170 static void map_sysval(enum wined3d_sysval_semantic sysval, struct wined3d_shader_register *reg)
172 unsigned int i;
174 for (i = 0; i < sizeof(sysval_map) / sizeof(*sysval_map); ++i)
176 if (sysval == sysval_map[i].sysval)
178 reg->type = sysval_map[i].register_type;
179 reg->idx = sysval_map[i].register_idx;
184 static void map_register(struct wined3d_sm4_data *priv, struct wined3d_shader_register *reg)
186 switch (priv->shader_version.type)
188 case WINED3D_SHADER_TYPE_PIXEL:
189 if (reg->type == WINED3DSPR_OUTPUT)
191 unsigned int i;
192 const struct wined3d_shader_signature *s = priv->output_signature;
194 if (!s)
196 ERR("Shader has no output signature, unable to map register.\n");
197 break;
200 for (i = 0; i < s->element_count; ++i)
202 if (s->elements[i].register_idx == reg->idx)
204 map_sysval(s->elements[i].sysval_semantic, reg);
205 break;
209 break;
211 default:
212 break;
216 static void *shader_sm4_init(const DWORD *byte_code, const struct wined3d_shader_signature *output_signature)
218 struct wined3d_sm4_data *priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv));
219 if (!priv)
221 ERR("Failed to allocate private data\n");
222 return NULL;
225 priv->output_signature = output_signature;
227 return priv;
230 static void shader_sm4_free(void *data)
232 HeapFree(GetProcessHeap(), 0, data);
235 static void shader_sm4_read_header(void *data, const DWORD **ptr, struct wined3d_shader_version *shader_version)
237 struct wined3d_sm4_data *priv = data;
238 DWORD version_token;
240 priv->end = *ptr;
242 version_token = *(*ptr)++;
243 TRACE("version: 0x%08x\n", version_token);
245 TRACE("token count: %u\n", **ptr);
246 priv->end += *(*ptr)++;
248 switch (version_token >> 16)
250 case WINED3D_SM4_PS:
251 priv->shader_version.type = WINED3D_SHADER_TYPE_PIXEL;
252 break;
254 case WINED3D_SM4_VS:
255 priv->shader_version.type = WINED3D_SHADER_TYPE_VERTEX;
256 break;
258 case WINED3D_SM4_GS:
259 priv->shader_version.type = WINED3D_SHADER_TYPE_GEOMETRY;
260 break;
262 default:
263 FIXME("Unrecognized shader type %#x\n", version_token >> 16);
265 priv->shader_version.major = WINED3D_SM4_VERSION_MAJOR(version_token);
266 priv->shader_version.minor = WINED3D_SM4_VERSION_MINOR(version_token);
268 *shader_version = priv->shader_version;
271 static void shader_sm4_read_opcode(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins,
272 UINT *param_size)
274 const struct wined3d_sm4_opcode_info *opcode_info;
275 DWORD token = *(*ptr)++;
276 DWORD opcode = token & WINED3D_SM4_OPCODE_MASK;
278 *param_size = ((token & WINED3D_SM4_INSTRUCTION_LENGTH_MASK) >> WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT) - 1;
280 opcode_info = get_opcode_info(opcode);
281 if (!opcode_info)
283 FIXME("Unrecognized opcode %#x, token 0x%08x\n", opcode, token);
284 ins->handler_idx = WINED3DSIH_TABLE_SIZE;
285 return;
288 ins->handler_idx = opcode_info->handler_idx;
289 ins->flags = 0;
290 ins->coissue = 0;
291 ins->predicate = 0;
292 ins->dst_count = opcode_info->dst_count;
293 ins->src_count = opcode_info->src_count;
296 static void shader_sm4_read_src_param(void *data, const DWORD **ptr, struct wined3d_shader_src_param *src_param,
297 struct wined3d_shader_src_param *src_rel_addr)
299 struct wined3d_sm4_data *priv = data;
300 DWORD token = *(*ptr)++;
301 enum wined3d_sm4_register_type register_type;
302 DWORD order;
304 register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
305 if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
307 FIXME("Unhandled register type %#x\n", register_type);
308 src_param->reg.type = WINED3DSPR_TEMP;
310 else
312 src_param->reg.type = register_type_table[register_type];
315 order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
317 if (order < 1) src_param->reg.idx = ~0U;
318 else src_param->reg.idx = *(*ptr)++;
320 if (order < 2) src_param->reg.array_idx = ~0U;
321 else src_param->reg.array_idx = *(*ptr)++;
323 if (order > 2) FIXME("Unhandled order %u.\n", order);
325 if (register_type == WINED3D_SM4_RT_IMMCONST)
327 enum wined3d_sm4_immconst_type immconst_type =
328 (token & WINED3D_SM4_IMMCONST_TYPE_MASK) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT;
329 src_param->swizzle = WINED3DSP_NOSWIZZLE;
331 switch(immconst_type)
333 case WINED3D_SM4_IMMCONST_FLOAT:
334 src_param->reg.immconst_type = WINED3D_IMMCONST_FLOAT;
335 memcpy(src_param->reg.immconst_data, *ptr, 1 * sizeof(DWORD));
336 *ptr += 1;
337 break;
339 case WINED3D_SM4_IMMCONST_FLOAT4:
340 src_param->reg.immconst_type = WINED3D_IMMCONST_FLOAT4;
341 memcpy(src_param->reg.immconst_data, *ptr, 4 * sizeof(DWORD));
342 *ptr += 4;
343 break;
345 default:
346 FIXME("Unhandled immediate constant type %#x\n", immconst_type);
347 break;
350 else
352 src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
355 src_param->modifiers = 0;
356 src_param->reg.rel_addr = NULL;
358 map_register(priv, &src_param->reg);
361 static void shader_sm4_read_dst_param(void *data, const DWORD **ptr, struct wined3d_shader_dst_param *dst_param,
362 struct wined3d_shader_src_param *dst_rel_addr)
364 struct wined3d_sm4_data *priv = data;
365 DWORD token = *(*ptr)++;
366 enum wined3d_sm4_register_type register_type;
367 DWORD order;
369 register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
370 if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
372 FIXME("Unhandled register type %#x\n", register_type);
373 dst_param->reg.type = WINED3DSPR_TEMP;
375 else
377 dst_param->reg.type = register_type_table[register_type];
380 order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
382 if (order < 1) dst_param->reg.idx = ~0U;
383 else dst_param->reg.idx = *(*ptr)++;
385 if (order < 2) dst_param->reg.array_idx = ~0U;
386 else dst_param->reg.array_idx = *(*ptr)++;
388 if (order > 2) FIXME("Unhandled order %u.\n", order);
390 dst_param->write_mask = (token & WINED3D_SM4_WRITEMASK_MASK) >> WINED3D_SM4_WRITEMASK_SHIFT;
391 dst_param->modifiers = 0;
392 dst_param->shift = 0;
393 dst_param->reg.rel_addr = NULL;
395 map_register(priv, &dst_param->reg);
398 static void shader_sm4_read_semantic(const DWORD **ptr, struct wined3d_shader_semantic *semantic)
400 FIXME("ptr %p, semantic %p stub!\n", ptr, semantic);
403 static void shader_sm4_read_comment(const DWORD **ptr, const char **comment)
405 FIXME("ptr %p, comment %p stub!\n", ptr, comment);
406 *comment = NULL;
409 static BOOL shader_sm4_is_end(void *data, const DWORD **ptr)
411 struct wined3d_sm4_data *priv = data;
412 return *ptr == priv->end;
415 const struct wined3d_shader_frontend sm4_shader_frontend =
417 shader_sm4_init,
418 shader_sm4_free,
419 shader_sm4_read_header,
420 shader_sm4_read_opcode,
421 shader_sm4_read_src_param,
422 shader_sm4_read_dst_param,
423 shader_sm4_read_semantic,
424 shader_sm4_read_comment,
425 shader_sm4_is_end,