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