wined3d: Recognize the SM4 dcl_outputTopology opcode.
[wine] / dlls / wined3d / shader_sm4.c
1 /*
2  * Copyright 2009 Henri Verbeet for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include "wined3d_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
25
26 #define WINED3D_SM4_INSTRUCTION_MODIFIER        (1 << 31)
27
28 #define WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT    24
29 #define WINED3D_SM4_INSTRUCTION_LENGTH_MASK     (0xf << WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT)
30
31 #define WINED3D_SM4_PRIMITIVE_TYPE_SHIFT        11
32 #define WINED3D_SM4_PRIMITIVE_TYPE_MASK         (0x7 << WINED3D_SM4_PRIMITIVE_TYPE_SHIFT)
33
34 #define WINED3D_SM4_OPCODE_MASK                 0xff
35
36 #define WINED3D_SM4_REGISTER_MODIFIER           (1 << 31)
37
38 #define WINED3D_SM4_REGISTER_ORDER_SHIFT        20
39 #define WINED3D_SM4_REGISTER_ORDER_MASK         (0x3 << WINED3D_SM4_REGISTER_ORDER_SHIFT)
40
41 #define WINED3D_SM4_REGISTER_TYPE_SHIFT         12
42 #define WINED3D_SM4_REGISTER_TYPE_MASK          (0xf << WINED3D_SM4_REGISTER_TYPE_SHIFT)
43
44 #define WINED3D_SM4_IMMCONST_TYPE_SHIFT         0
45 #define WINED3D_SM4_IMMCONST_TYPE_MASK          (0x3 << WINED3D_SM4_IMMCONST_TYPE_SHIFT)
46
47 #define WINED3D_SM4_WRITEMASK_SHIFT             4
48 #define WINED3D_SM4_WRITEMASK_MASK              (0xf << WINED3D_SM4_WRITEMASK_SHIFT)
49
50 #define WINED3D_SM4_SWIZZLE_SHIFT               4
51 #define WINED3D_SM4_SWIZZLE_MASK                (0xff << WINED3D_SM4_SWIZZLE_SHIFT)
52
53 #define WINED3D_SM4_VERSION_MAJOR(version)      (((version) >> 4) & 0xf)
54 #define WINED3D_SM4_VERSION_MINOR(version)      (((version) >> 0) & 0xf)
55
56 enum wined3d_sm4_opcode
57 {
58     WINED3D_SM4_OP_ADD                  = 0x00,
59     WINED3D_SM4_OP_AND                  = 0x01,
60     WINED3D_SM4_OP_BREAK                = 0x02,
61     WINED3D_SM4_OP_BREAKC               = 0x03,
62     WINED3D_SM4_OP_CUT                  = 0x09,
63     WINED3D_SM4_OP_DERIV_RTX            = 0x0b,
64     WINED3D_SM4_OP_DERIV_RTY            = 0x0c,
65     WINED3D_SM4_OP_DIV                  = 0x0e,
66     WINED3D_SM4_OP_DP3                  = 0x10,
67     WINED3D_SM4_OP_DP4                  = 0x11,
68     WINED3D_SM4_OP_EMIT                 = 0x13,
69     WINED3D_SM4_OP_ENDIF                = 0x15,
70     WINED3D_SM4_OP_ENDLOOP              = 0x16,
71     WINED3D_SM4_OP_EQ                   = 0x18,
72     WINED3D_SM4_OP_EXP                  = 0x19,
73     WINED3D_SM4_OP_FRC                  = 0x1a,
74     WINED3D_SM4_OP_FTOI                 = 0x1b,
75     WINED3D_SM4_OP_GE                   = 0x1d,
76     WINED3D_SM4_OP_IADD                 = 0x1e,
77     WINED3D_SM4_OP_IF                   = 0x1f,
78     WINED3D_SM4_OP_IEQ                  = 0x20,
79     WINED3D_SM4_OP_IGE                  = 0x21,
80     WINED3D_SM4_OP_IMUL                 = 0x26,
81     WINED3D_SM4_OP_ITOF                 = 0x2b,
82     WINED3D_SM4_OP_LD                   = 0x2d,
83     WINED3D_SM4_OP_LOG                  = 0x2f,
84     WINED3D_SM4_OP_LOOP                 = 0x30,
85     WINED3D_SM4_OP_LT                   = 0x31,
86     WINED3D_SM4_OP_MAD                  = 0x32,
87     WINED3D_SM4_OP_MIN                  = 0x33,
88     WINED3D_SM4_OP_MAX                  = 0x34,
89     WINED3D_SM4_OP_MOV                  = 0x36,
90     WINED3D_SM4_OP_MOVC                 = 0x37,
91     WINED3D_SM4_OP_MUL                  = 0x38,
92     WINED3D_SM4_OP_RET                  = 0x3e,
93     WINED3D_SM4_OP_ROUND_NI             = 0x41,
94     WINED3D_SM4_OP_RSQ                  = 0x44,
95     WINED3D_SM4_OP_SAMPLE               = 0x45,
96     WINED3D_SM4_OP_SAMPLE_LOD           = 0x48,
97     WINED3D_SM4_OP_SAMPLE_GRAD          = 0x49,
98     WINED3D_SM4_OP_SQRT                 = 0x4b,
99     WINED3D_SM4_OP_SINCOS               = 0x4d,
100     WINED3D_SM4_OP_UDIV                 = 0x4e,
101     WINED3D_SM4_OP_USHR                 = 0x55,
102     WINED3D_SM4_OP_UTOF                 = 0x56,
103     WINED3D_SM4_OP_XOR                  = 0x57,
104     WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY  = 0x5c,
105     WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE  = 0x5d,
106     WINED3D_SM4_OP_DCL_VERTICES_OUT     = 0x5e,
107 };
108
109 enum wined3d_sm4_register_type
110 {
111     WINED3D_SM4_RT_TEMP         = 0x0,
112     WINED3D_SM4_RT_INPUT        = 0x1,
113     WINED3D_SM4_RT_OUTPUT       = 0x2,
114     WINED3D_SM4_RT_IMMCONST     = 0x4,
115     WINED3D_SM4_RT_SAMPLER      = 0x6,
116     WINED3D_SM4_RT_CONSTBUFFER  = 0x8,
117     WINED3D_SM4_RT_NULL         = 0xd,
118 };
119
120 enum wined3d_sm4_output_primitive_type
121 {
122     WINED3D_SM4_OUTPUT_PT_POINTLIST     = 0x1,
123     WINED3D_SM4_OUTPUT_PT_LINELIST      = 0x3,
124     WINED3D_SM4_OUTPUT_PT_TRIANGLESTRIP = 0x5,
125 };
126
127 enum wined3d_sm4_input_primitive_type
128 {
129     WINED3D_SM4_INPUT_PT_POINT          = 0x1,
130     WINED3D_SM4_INPUT_PT_LINE           = 0x2,
131     WINED3D_SM4_INPUT_PT_TRIANGLE       = 0x3,
132     WINED3D_SM4_INPUT_PT_LINEADJ        = 0x6,
133     WINED3D_SM4_INPUT_PT_TRIANGLEADJ    = 0x7,
134 };
135
136 enum wined3d_sm4_immconst_type
137 {
138     WINED3D_SM4_IMMCONST_SCALAR = 0x1,
139     WINED3D_SM4_IMMCONST_VEC4   = 0x2,
140 };
141
142 struct wined3d_sm4_data
143 {
144     struct wined3d_shader_version shader_version;
145     const DWORD *end;
146     const struct wined3d_shader_signature *output_signature;
147
148     struct wined3d_shader_src_param src_rel_addr[5];
149     struct wined3d_shader_src_param dst_rel_addr[2];
150     struct wined3d_shader_src_param src_param[5];
151     struct wined3d_shader_dst_param dst_param[2];
152 };
153
154 struct wined3d_sm4_opcode_info
155 {
156     enum wined3d_sm4_opcode opcode;
157     enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx;
158     const char *dst_info;
159     const char *src_info;
160 };
161
162 struct sysval_map
163 {
164     enum wined3d_sysval_semantic sysval;
165     enum wined3d_shader_register_type register_type;
166     UINT register_idx;
167 };
168
169 /*
170  * F -> WINED3D_DATA_FLOAT
171  * I -> WINED3D_DATA_INT
172  * R -> WINED3D_DATA_RESOURCE
173  * S -> WINED3D_DATA_SAMPLER
174  * U -> WINED3D_DATA_UINT
175  */
176 static const struct wined3d_sm4_opcode_info opcode_table[] =
177 {
178     {WINED3D_SM4_OP_ADD,                    WINED3DSIH_ADD,                 "F",    "FF"},
179     {WINED3D_SM4_OP_AND,                    WINED3DSIH_AND,                 "U",    "UU"},
180     {WINED3D_SM4_OP_BREAK,                  WINED3DSIH_BREAK,               "",     ""},
181     {WINED3D_SM4_OP_BREAKC,                 WINED3DSIH_BREAKP,              "",     "U"},
182     {WINED3D_SM4_OP_CUT,                    WINED3DSIH_CUT,                 "",     ""},
183     {WINED3D_SM4_OP_DERIV_RTX,              WINED3DSIH_DSX,                 "F",    "F"},
184     {WINED3D_SM4_OP_DERIV_RTY,              WINED3DSIH_DSY,                 "F",    "F"},
185     {WINED3D_SM4_OP_DIV,                    WINED3DSIH_DIV,                 "F",    "FF"},
186     {WINED3D_SM4_OP_DP3,                    WINED3DSIH_DP3,                 "F",    "FF"},
187     {WINED3D_SM4_OP_DP4,                    WINED3DSIH_DP4,                 "F",    "FF"},
188     {WINED3D_SM4_OP_EMIT,                   WINED3DSIH_EMIT,                "",     ""},
189     {WINED3D_SM4_OP_ENDIF,                  WINED3DSIH_ENDIF,               "",     ""},
190     {WINED3D_SM4_OP_ENDLOOP,                WINED3DSIH_ENDLOOP,             "",     ""},
191     {WINED3D_SM4_OP_EQ,                     WINED3DSIH_EQ,                  "U",    "FF"},
192     {WINED3D_SM4_OP_EXP,                    WINED3DSIH_EXP,                 "F",    "F"},
193     {WINED3D_SM4_OP_FRC,                    WINED3DSIH_FRC,                 "F",    "F"},
194     {WINED3D_SM4_OP_FTOI,                   WINED3DSIH_FTOI,                "I",    "F"},
195     {WINED3D_SM4_OP_GE,                     WINED3DSIH_GE,                  "U",    "FF"},
196     {WINED3D_SM4_OP_IADD,                   WINED3DSIH_IADD,                "I",    "II"},
197     {WINED3D_SM4_OP_IF,                     WINED3DSIH_IF,                  "",     "U"},
198     {WINED3D_SM4_OP_IEQ,                    WINED3DSIH_IEQ,                 "U",    "II"},
199     {WINED3D_SM4_OP_IGE,                    WINED3DSIH_IGE,                 "U",    "II"},
200     {WINED3D_SM4_OP_IMUL,                   WINED3DSIH_IMUL,                "II",   "II"},
201     {WINED3D_SM4_OP_ITOF,                   WINED3DSIH_ITOF,                "F",    "I"},
202     {WINED3D_SM4_OP_LD,                     WINED3DSIH_LD,                  "U",    "FR"},
203     {WINED3D_SM4_OP_LOG,                    WINED3DSIH_LOG,                 "F",    "F"},
204     {WINED3D_SM4_OP_LOOP,                   WINED3DSIH_LOOP,                "",     ""},
205     {WINED3D_SM4_OP_LT,                     WINED3DSIH_LT,                  "U",    "FF"},
206     {WINED3D_SM4_OP_MAD,                    WINED3DSIH_MAD,                 "F",    "FFF"},
207     {WINED3D_SM4_OP_MIN,                    WINED3DSIH_MIN,                 "F",    "FF"},
208     {WINED3D_SM4_OP_MAX,                    WINED3DSIH_MAX,                 "F",    "FF"},
209     {WINED3D_SM4_OP_MOV,                    WINED3DSIH_MOV,                 "F",    "F"},
210     {WINED3D_SM4_OP_MOVC,                   WINED3DSIH_MOVC,                "F",    "UFF"},
211     {WINED3D_SM4_OP_MUL,                    WINED3DSIH_MUL,                 "F",    "FF"},
212     {WINED3D_SM4_OP_RET,                    WINED3DSIH_RET,                 "",     ""},
213     {WINED3D_SM4_OP_ROUND_NI,               WINED3DSIH_ROUND_NI,            "F",    "F"},
214     {WINED3D_SM4_OP_RSQ,                    WINED3DSIH_RSQ,                 "F",    "F"},
215     {WINED3D_SM4_OP_SAMPLE,                 WINED3DSIH_SAMPLE,              "U",    "FRS"},
216     {WINED3D_SM4_OP_SAMPLE_LOD,             WINED3DSIH_SAMPLE_LOD,          "U",    "FRSF"},
217     {WINED3D_SM4_OP_SAMPLE_GRAD,            WINED3DSIH_SAMPLE_GRAD,         "U",    "FRSFF"},
218     {WINED3D_SM4_OP_SQRT,                   WINED3DSIH_SQRT,                "F",    "F"},
219     {WINED3D_SM4_OP_SINCOS,                 WINED3DSIH_SINCOS,              "FF",   "F"},
220     {WINED3D_SM4_OP_UDIV,                   WINED3DSIH_UDIV,                "UU",   "UU"},
221     {WINED3D_SM4_OP_USHR,                   WINED3DSIH_USHR,                "U",    "UU"},
222     {WINED3D_SM4_OP_UTOF,                   WINED3DSIH_UTOF,                "F",    "U"},
223     {WINED3D_SM4_OP_XOR,                    WINED3DSIH_XOR,                 "U",    "UU"},
224     {WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY,    WINED3DSIH_DCL_OUTPUT_TOPOLOGY, "",     ""},
225     {WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE,    WINED3DSIH_DCL_INPUT_PRIMITIVE, "",     ""},
226     {WINED3D_SM4_OP_DCL_VERTICES_OUT,       WINED3DSIH_DCL_VERTICES_OUT,    "",     ""},
227 };
228
229 static const enum wined3d_shader_register_type register_type_table[] =
230 {
231     /* WINED3D_SM4_RT_TEMP */           WINED3DSPR_TEMP,
232     /* WINED3D_SM4_RT_INPUT */          WINED3DSPR_INPUT,
233     /* WINED3D_SM4_RT_OUTPUT */         WINED3DSPR_OUTPUT,
234     /* UNKNOWN */                       0,
235     /* WINED3D_SM4_RT_IMMCONST */       WINED3DSPR_IMMCONST,
236     /* UNKNOWN */                       0,
237     /* WINED3D_SM4_RT_SAMPLER */        WINED3DSPR_SAMPLER,
238     /* WINED3D_SM4_RT_RESOURCE */       WINED3DSPR_RESOURCE,
239     /* WINED3D_SM4_RT_CONSTBUFFER */    WINED3DSPR_CONSTBUFFER,
240     /* UNKNOWN */                       0,
241     /* UNKNOWN */                       0,
242     /* UNKNOWN */                       0,
243     /* UNKNOWN */                       0,
244     /* WINED3D_SM4_RT_NULL */           WINED3DSPR_NULL,
245 };
246
247 static const enum wined3d_primitive_type output_primitive_type_table[] =
248 {
249     /* UNKNOWN */                               WINED3D_PT_UNDEFINED,
250     /* WINED3D_SM4_OUTPUT_PT_POINTLIST */       WINED3D_PT_POINTLIST,
251     /* UNKNOWN */                               WINED3D_PT_UNDEFINED,
252     /* WINED3D_SM4_OUTPUT_PT_LINELIST */        WINED3D_PT_LINELIST,
253     /* UNKNOWN */                               WINED3D_PT_UNDEFINED,
254     /* WINED3D_SM4_OUTPUT_PT_TRIANGLESTRIP */   WINED3D_PT_TRIANGLESTRIP,
255 };
256
257 static const enum wined3d_primitive_type input_primitive_type_table[] =
258 {
259     /* UNKNOWN */                               WINED3D_PT_UNDEFINED,
260     /* WINED3D_SM4_INPUT_PT_POINT */            WINED3D_PT_POINTLIST,
261     /* WINED3D_SM4_INPUT_PT_LINE */             WINED3D_PT_LINELIST,
262     /* WINED3D_SM4_INPUT_PT_TRIANGLE */         WINED3D_PT_TRIANGLELIST,
263     /* UNKNOWN */                               WINED3D_PT_UNDEFINED,
264     /* UNKNOWN */                               WINED3D_PT_UNDEFINED,
265     /* WINED3D_SM4_INPUT_PT_LINEADJ */          WINED3D_PT_LINELIST_ADJ,
266     /* WINED3D_SM4_INPUT_PT_TRIANGLEADJ */      WINED3D_PT_TRIANGLELIST_ADJ,
267 };
268
269 static const struct sysval_map sysval_map[] =
270 {
271     {WINED3D_SV_DEPTH,      WINED3DSPR_DEPTHOUT,    0},
272     {WINED3D_SV_TARGET0,    WINED3DSPR_COLOROUT,    0},
273     {WINED3D_SV_TARGET1,    WINED3DSPR_COLOROUT,    1},
274     {WINED3D_SV_TARGET2,    WINED3DSPR_COLOROUT,    2},
275     {WINED3D_SV_TARGET3,    WINED3DSPR_COLOROUT,    3},
276     {WINED3D_SV_TARGET4,    WINED3DSPR_COLOROUT,    4},
277     {WINED3D_SV_TARGET5,    WINED3DSPR_COLOROUT,    5},
278     {WINED3D_SV_TARGET6,    WINED3DSPR_COLOROUT,    6},
279     {WINED3D_SV_TARGET7,    WINED3DSPR_COLOROUT,    7},
280 };
281
282 static const struct wined3d_sm4_opcode_info *get_opcode_info(enum wined3d_sm4_opcode opcode)
283 {
284     unsigned int i;
285
286     for (i = 0; i < sizeof(opcode_table) / sizeof(*opcode_table); ++i)
287     {
288         if (opcode == opcode_table[i].opcode) return &opcode_table[i];
289     }
290
291     return NULL;
292 }
293
294 static void map_sysval(enum wined3d_sysval_semantic sysval, struct wined3d_shader_register *reg)
295 {
296     unsigned int i;
297
298     for (i = 0; i < sizeof(sysval_map) / sizeof(*sysval_map); ++i)
299     {
300         if (sysval == sysval_map[i].sysval)
301         {
302             reg->type = sysval_map[i].register_type;
303             reg->idx = sysval_map[i].register_idx;
304         }
305     }
306 }
307
308 static void map_register(const struct wined3d_sm4_data *priv, struct wined3d_shader_register *reg)
309 {
310     switch (priv->shader_version.type)
311     {
312         case WINED3D_SHADER_TYPE_PIXEL:
313             if (reg->type == WINED3DSPR_OUTPUT)
314             {
315                 unsigned int i;
316                 const struct wined3d_shader_signature *s = priv->output_signature;
317
318                 if (!s)
319                 {
320                     ERR("Shader has no output signature, unable to map register.\n");
321                     break;
322                 }
323
324                 for (i = 0; i < s->element_count; ++i)
325                 {
326                     if (s->elements[i].register_idx == reg->idx)
327                     {
328                         map_sysval(s->elements[i].sysval_semantic, reg);
329                         break;
330                     }
331                 }
332             }
333             break;
334
335         default:
336             break;
337     }
338 }
339
340 static enum wined3d_data_type map_data_type(char t)
341 {
342     switch (t)
343     {
344         case 'F':
345             return WINED3D_DATA_FLOAT;
346         case 'I':
347             return WINED3D_DATA_INT;
348         case 'R':
349             return WINED3D_DATA_RESOURCE;
350         case 'S':
351             return WINED3D_DATA_SAMPLER;
352         case 'U':
353             return WINED3D_DATA_UINT;
354         default:
355             ERR("Invalid data type '%c'.\n", t);
356             return WINED3D_DATA_FLOAT;
357     }
358 }
359
360 static void *shader_sm4_init(const DWORD *byte_code, const struct wined3d_shader_signature *output_signature)
361 {
362     struct wined3d_sm4_data *priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv));
363     if (!priv)
364     {
365         ERR("Failed to allocate private data\n");
366         return NULL;
367     }
368
369     priv->output_signature = output_signature;
370
371     return priv;
372 }
373
374 static void shader_sm4_free(void *data)
375 {
376     HeapFree(GetProcessHeap(), 0, data);
377 }
378
379 static void shader_sm4_read_header(void *data, const DWORD **ptr, struct wined3d_shader_version *shader_version)
380 {
381     struct wined3d_sm4_data *priv = data;
382     DWORD version_token;
383
384     priv->end = *ptr;
385
386     version_token = *(*ptr)++;
387     TRACE("version: 0x%08x\n", version_token);
388
389     TRACE("token count: %u\n", **ptr);
390     priv->end += *(*ptr)++;
391
392     switch (version_token >> 16)
393     {
394         case WINED3D_SM4_PS:
395             priv->shader_version.type = WINED3D_SHADER_TYPE_PIXEL;
396             break;
397
398         case WINED3D_SM4_VS:
399             priv->shader_version.type = WINED3D_SHADER_TYPE_VERTEX;
400             break;
401
402         case WINED3D_SM4_GS:
403             priv->shader_version.type = WINED3D_SHADER_TYPE_GEOMETRY;
404             break;
405
406         default:
407             FIXME("Unrecognized shader type %#x\n", version_token >> 16);
408     }
409     priv->shader_version.major = WINED3D_SM4_VERSION_MAJOR(version_token);
410     priv->shader_version.minor = WINED3D_SM4_VERSION_MINOR(version_token);
411
412     *shader_version = priv->shader_version;
413 }
414
415 static void shader_sm4_read_src_param(struct wined3d_sm4_data *priv, const DWORD **ptr,
416         enum wined3d_data_type data_type, struct wined3d_shader_src_param *src_param,
417         struct wined3d_shader_src_param *src_rel_addr)
418 {
419     DWORD token = *(*ptr)++;
420     enum wined3d_sm4_register_type register_type;
421     DWORD order;
422
423     register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
424     if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
425     {
426         FIXME("Unhandled register type %#x\n", register_type);
427         src_param->reg.type = WINED3DSPR_TEMP;
428     }
429     else
430     {
431         src_param->reg.type = register_type_table[register_type];
432     }
433     src_param->reg.data_type = data_type;
434
435     if (token & WINED3D_SM4_REGISTER_MODIFIER)
436     {
437         DWORD modifier = *(*ptr)++;
438
439         /* FIXME: This will probably break down at some point. The SM4
440          * modifiers look like flags, while wined3d currently has an enum
441          * with possible combinations, e.g. WINED3DSPSM_ABSNEG. */
442         switch (modifier)
443         {
444             case 0x41:
445                 src_param->modifiers = WINED3DSPSM_NEG;
446                 break;
447
448             case 0x81:
449                 src_param->modifiers = WINED3DSPSM_ABS;
450                 break;
451
452             default:
453                 FIXME("Skipping modifier 0x%08x.\n", modifier);
454                 src_param->modifiers = WINED3DSPSM_NONE;
455                 break;
456         }
457     }
458     else
459     {
460         src_param->modifiers = WINED3DSPSM_NONE;
461     }
462
463     order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
464
465     if (order < 1) src_param->reg.idx = ~0U;
466     else src_param->reg.idx = *(*ptr)++;
467
468     if (order < 2) src_param->reg.array_idx = ~0U;
469     else src_param->reg.array_idx = *(*ptr)++;
470
471     if (order > 2) FIXME("Unhandled order %u.\n", order);
472
473     if (register_type == WINED3D_SM4_RT_IMMCONST)
474     {
475         enum wined3d_sm4_immconst_type immconst_type =
476                 (token & WINED3D_SM4_IMMCONST_TYPE_MASK) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT;
477         src_param->swizzle = WINED3DSP_NOSWIZZLE;
478
479         switch(immconst_type)
480         {
481             case WINED3D_SM4_IMMCONST_SCALAR:
482                 src_param->reg.immconst_type = WINED3D_IMMCONST_SCALAR;
483                 memcpy(src_param->reg.immconst_data, *ptr, 1 * sizeof(DWORD));
484                 *ptr += 1;
485                 break;
486
487             case WINED3D_SM4_IMMCONST_VEC4:
488                 src_param->reg.immconst_type = WINED3D_IMMCONST_VEC4;
489                 memcpy(src_param->reg.immconst_data, *ptr, 4 * sizeof(DWORD));
490                 *ptr += 4;
491                 break;
492
493             default:
494                 FIXME("Unhandled immediate constant type %#x\n", immconst_type);
495                 break;
496         }
497     }
498     else
499     {
500         src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
501     }
502
503     src_param->reg.rel_addr = NULL;
504
505     map_register(priv, &src_param->reg);
506 }
507
508 static void shader_sm4_read_dst_param(struct wined3d_sm4_data *priv, const DWORD **ptr,
509         enum wined3d_data_type data_type, struct wined3d_shader_dst_param *dst_param,
510         struct wined3d_shader_src_param *dst_rel_addr)
511 {
512     DWORD token = *(*ptr)++;
513     enum wined3d_sm4_register_type register_type;
514     DWORD order;
515
516     register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
517     if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
518     {
519         FIXME("Unhandled register type %#x\n", register_type);
520         dst_param->reg.type = WINED3DSPR_TEMP;
521     }
522     else
523     {
524         dst_param->reg.type = register_type_table[register_type];
525     }
526     dst_param->reg.data_type = data_type;
527
528     order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
529
530     if (order < 1) dst_param->reg.idx = ~0U;
531     else dst_param->reg.idx = *(*ptr)++;
532
533     if (order < 2) dst_param->reg.array_idx = ~0U;
534     else dst_param->reg.array_idx = *(*ptr)++;
535
536     if (order > 2) FIXME("Unhandled order %u.\n", order);
537
538     dst_param->write_mask = (token & WINED3D_SM4_WRITEMASK_MASK) >> WINED3D_SM4_WRITEMASK_SHIFT;
539     dst_param->modifiers = 0;
540     dst_param->shift = 0;
541     dst_param->reg.rel_addr = NULL;
542
543     map_register(priv, &dst_param->reg);
544 }
545
546 static void shader_sm4_read_instruction(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins)
547 {
548     const struct wined3d_sm4_opcode_info *opcode_info;
549     struct wined3d_sm4_data *priv = data;
550     DWORD opcode_token, opcode;
551     const DWORD *p;
552     UINT i, len;
553
554     opcode_token = *(*ptr)++;
555     opcode = opcode_token & WINED3D_SM4_OPCODE_MASK;
556     len = ((opcode_token & WINED3D_SM4_INSTRUCTION_LENGTH_MASK) >> WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT) - 1;
557
558     if (!(opcode_info = get_opcode_info(opcode)))
559     {
560         FIXME("Unrecognized opcode %#x, opcode_token 0x%08x.\n", opcode, opcode_token);
561         ins->handler_idx = WINED3DSIH_TABLE_SIZE;
562         *ptr += len;
563         return;
564     }
565
566     ins->handler_idx = opcode_info->handler_idx;
567     ins->flags = 0;
568     ins->coissue = 0;
569     ins->predicate = NULL;
570     ins->dst_count = strlen(opcode_info->dst_info);
571     ins->dst = priv->dst_param;
572     ins->src_count = strlen(opcode_info->src_info);
573     ins->src = priv->src_param;
574
575     p = *ptr;
576     *ptr += len;
577
578     if (opcode_token & WINED3D_SM4_INSTRUCTION_MODIFIER)
579     {
580         DWORD modifier = *p++;
581         FIXME("Skipping modifier 0x%08x.\n", modifier);
582     }
583
584     if (opcode == WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY)
585     {
586         enum wined3d_sm4_output_primitive_type primitive_type;
587
588         primitive_type = (opcode_token & WINED3D_SM4_PRIMITIVE_TYPE_MASK) >> WINED3D_SM4_PRIMITIVE_TYPE_SHIFT;
589         if (primitive_type >= sizeof(output_primitive_type_table) / sizeof(*output_primitive_type_table))
590         {
591             FIXME("Unhandled output primitive type %#x.\n", primitive_type);
592             ins->declaration.primitive_type = WINED3D_PT_UNDEFINED;
593         }
594         else
595         {
596             ins->declaration.primitive_type = output_primitive_type_table[primitive_type];
597         }
598     }
599     else if (opcode == WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE)
600     {
601         enum wined3d_sm4_input_primitive_type primitive_type;
602
603         primitive_type = (opcode_token & WINED3D_SM4_PRIMITIVE_TYPE_MASK) >> WINED3D_SM4_PRIMITIVE_TYPE_SHIFT;
604         if (primitive_type >= sizeof(input_primitive_type_table) / sizeof(*input_primitive_type_table))
605         {
606             FIXME("Unhandled input primitive type %#x.\n", primitive_type);
607             ins->declaration.primitive_type = WINED3D_PT_UNDEFINED;
608         }
609         else
610         {
611             ins->declaration.primitive_type = input_primitive_type_table[primitive_type];
612         }
613     }
614     else if (opcode == WINED3D_SM4_OP_DCL_VERTICES_OUT)
615     {
616         ins->declaration.count = *p++;
617     }
618     else
619     {
620         for (i = 0; i < ins->dst_count; ++i)
621         {
622             shader_sm4_read_dst_param(priv, &p, map_data_type(opcode_info->dst_info[i]),
623                     &priv->dst_param[i], &priv->dst_rel_addr[i]);
624         }
625
626         for (i = 0; i < ins->src_count; ++i)
627         {
628             shader_sm4_read_src_param(priv, &p, map_data_type(opcode_info->src_info[i]),
629                     &priv->src_param[i], &priv->src_rel_addr[i]);
630         }
631     }
632 }
633
634 static void shader_sm4_read_comment(const DWORD **ptr, const char **comment, UINT *comment_size)
635 {
636     FIXME("ptr %p, comment %p, comment_size %p stub!\n", ptr, comment, comment_size);
637     *comment = NULL;
638 }
639
640 static BOOL shader_sm4_is_end(void *data, const DWORD **ptr)
641 {
642     struct wined3d_sm4_data *priv = data;
643     return *ptr == priv->end;
644 }
645
646 const struct wined3d_shader_frontend sm4_shader_frontend =
647 {
648     shader_sm4_init,
649     shader_sm4_free,
650     shader_sm4_read_header,
651     shader_sm4_read_instruction,
652     shader_sm4_read_comment,
653     shader_sm4_is_end,
654 };