wined3d: Make the shader backend responsible for controlling the fixed function fragm...
[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_INDEX_TYPE_SHIFT            11
36 #define WINED3D_SM4_INDEX_TYPE_MASK             (0x1 << WINED3D_SM4_INDEX_TYPE_SHIFT)
37
38 #define WINED3D_SM4_OPCODE_MASK                 0xff
39
40 #define WINED3D_SM4_REGISTER_MODIFIER           (1 << 31)
41
42 #define WINED3D_SM4_ADDRESSING_SHIFT1           25
43 #define WINED3D_SM4_ADDRESSING_MASK1            (0x3 << WINED3D_SM4_ADDRESSING_SHIFT1)
44
45 #define WINED3D_SM4_ADDRESSING_SHIFT0           22
46 #define WINED3D_SM4_ADDRESSING_MASK0            (0x3 << WINED3D_SM4_ADDRESSING_SHIFT0)
47
48 #define WINED3D_SM4_REGISTER_ORDER_SHIFT        20
49 #define WINED3D_SM4_REGISTER_ORDER_MASK         (0x3 << WINED3D_SM4_REGISTER_ORDER_SHIFT)
50
51 #define WINED3D_SM4_REGISTER_TYPE_SHIFT         12
52 #define WINED3D_SM4_REGISTER_TYPE_MASK          (0xf << WINED3D_SM4_REGISTER_TYPE_SHIFT)
53
54 #define WINED3D_SM4_SWIZZLE_TYPE_SHIFT          2
55 #define WINED3D_SM4_SWIZZLE_TYPE_MASK           (0x3 << WINED3D_SM4_SWIZZLE_TYPE_SHIFT)
56
57 #define WINED3D_SM4_IMMCONST_TYPE_SHIFT         0
58 #define WINED3D_SM4_IMMCONST_TYPE_MASK          (0x3 << WINED3D_SM4_IMMCONST_TYPE_SHIFT)
59
60 #define WINED3D_SM4_WRITEMASK_SHIFT             4
61 #define WINED3D_SM4_WRITEMASK_MASK              (0xf << WINED3D_SM4_WRITEMASK_SHIFT)
62
63 #define WINED3D_SM4_SWIZZLE_SHIFT               4
64 #define WINED3D_SM4_SWIZZLE_MASK                (0xff << WINED3D_SM4_SWIZZLE_SHIFT)
65
66 #define WINED3D_SM4_VERSION_MAJOR(version)      (((version) >> 4) & 0xf)
67 #define WINED3D_SM4_VERSION_MINOR(version)      (((version) >> 0) & 0xf)
68
69 #define WINED3D_SM4_ADDRESSING_RELATIVE         0x2
70 #define WINED3D_SM4_ADDRESSING_OFFSET           0x1
71
72 enum wined3d_sm4_opcode
73 {
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,
124 };
125
126 enum wined3d_sm4_register_type
127 {
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,
136 };
137
138 enum wined3d_sm4_output_primitive_type
139 {
140     WINED3D_SM4_OUTPUT_PT_POINTLIST     = 0x1,
141     WINED3D_SM4_OUTPUT_PT_LINELIST      = 0x3,
142     WINED3D_SM4_OUTPUT_PT_TRIANGLESTRIP = 0x5,
143 };
144
145 enum wined3d_sm4_input_primitive_type
146 {
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,
152 };
153
154 enum wined3d_sm4_swizzle_type
155 {
156     WINED3D_SM4_SWIZZLE_VEC4            = 0x1,
157     WINED3D_SM4_SWIZZLE_SCALAR          = 0x2,
158 };
159
160 enum wined3d_sm4_immconst_type
161 {
162     WINED3D_SM4_IMMCONST_SCALAR = 0x1,
163     WINED3D_SM4_IMMCONST_VEC4   = 0x2,
164 };
165
166 struct wined3d_shader_src_param_entry
167 {
168     struct list entry;
169     struct wined3d_shader_src_param param;
170 };
171
172 struct wined3d_sm4_data
173 {
174     struct wined3d_shader_version shader_version;
175     const DWORD *end;
176     const struct wined3d_shader_signature *output_signature;
177
178     struct wined3d_shader_src_param src_param[5];
179     struct wined3d_shader_dst_param dst_param[2];
180     struct list src_free;
181     struct list src;
182 };
183
184 struct wined3d_sm4_opcode_info
185 {
186     enum wined3d_sm4_opcode opcode;
187     enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx;
188     const char *dst_info;
189     const char *src_info;
190 };
191
192 struct sysval_map
193 {
194     enum wined3d_sysval_semantic sysval;
195     enum wined3d_shader_register_type register_type;
196     UINT register_idx;
197 };
198
199 /*
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
205  */
206 static const struct wined3d_sm4_opcode_info opcode_table[] =
207 {
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,    "",     ""},
258 };
259
260 static const enum wined3d_shader_register_type register_type_table[] =
261 {
262     /* WINED3D_SM4_RT_TEMP */           WINED3DSPR_TEMP,
263     /* WINED3D_SM4_RT_INPUT */          WINED3DSPR_INPUT,
264     /* WINED3D_SM4_RT_OUTPUT */         WINED3DSPR_OUTPUT,
265     /* UNKNOWN */                       0,
266     /* WINED3D_SM4_RT_IMMCONST */       WINED3DSPR_IMMCONST,
267     /* UNKNOWN */                       0,
268     /* WINED3D_SM4_RT_SAMPLER */        WINED3DSPR_SAMPLER,
269     /* WINED3D_SM4_RT_RESOURCE */       WINED3DSPR_RESOURCE,
270     /* WINED3D_SM4_RT_CONSTBUFFER */    WINED3DSPR_CONSTBUFFER,
271     /* UNKNOWN */                       0,
272     /* UNKNOWN */                       0,
273     /* WINED3D_SM4_RT_PRIMID */         WINED3DSPR_PRIMID,
274     /* UNKNOWN */                       0,
275     /* WINED3D_SM4_RT_NULL */           WINED3DSPR_NULL,
276 };
277
278 static const enum wined3d_primitive_type output_primitive_type_table[] =
279 {
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,
286 };
287
288 static const enum wined3d_primitive_type input_primitive_type_table[] =
289 {
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,
298 };
299
300 static const struct sysval_map sysval_map[] =
301 {
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},
311 };
312
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);
315
316 static const struct wined3d_sm4_opcode_info *get_opcode_info(enum wined3d_sm4_opcode opcode)
317 {
318     unsigned int i;
319
320     for (i = 0; i < sizeof(opcode_table) / sizeof(*opcode_table); ++i)
321     {
322         if (opcode == opcode_table[i].opcode) return &opcode_table[i];
323     }
324
325     return NULL;
326 }
327
328 static void map_sysval(enum wined3d_sysval_semantic sysval, struct wined3d_shader_register *reg)
329 {
330     unsigned int i;
331
332     for (i = 0; i < sizeof(sysval_map) / sizeof(*sysval_map); ++i)
333     {
334         if (sysval == sysval_map[i].sysval)
335         {
336             reg->type = sysval_map[i].register_type;
337             reg->idx[0].offset = sysval_map[i].register_idx;
338         }
339     }
340 }
341
342 static void map_register(const struct wined3d_sm4_data *priv, struct wined3d_shader_register *reg)
343 {
344     switch (priv->shader_version.type)
345     {
346         case WINED3D_SHADER_TYPE_PIXEL:
347             if (reg->type == WINED3DSPR_OUTPUT)
348             {
349                 unsigned int i;
350                 const struct wined3d_shader_signature *s = priv->output_signature;
351
352                 if (!s)
353                 {
354                     ERR("Shader has no output signature, unable to map register.\n");
355                     break;
356                 }
357
358                 for (i = 0; i < s->element_count; ++i)
359                 {
360                     if (s->elements[i].register_idx == reg->idx[0].offset)
361                     {
362                         map_sysval(s->elements[i].sysval_semantic, reg);
363                         break;
364                     }
365                 }
366             }
367             break;
368
369         default:
370             break;
371     }
372 }
373
374 static enum wined3d_data_type map_data_type(char t)
375 {
376     switch (t)
377     {
378         case 'F':
379             return WINED3D_DATA_FLOAT;
380         case 'I':
381             return WINED3D_DATA_INT;
382         case 'R':
383             return WINED3D_DATA_RESOURCE;
384         case 'S':
385             return WINED3D_DATA_SAMPLER;
386         case 'U':
387             return WINED3D_DATA_UINT;
388         default:
389             ERR("Invalid data type '%c'.\n", t);
390             return WINED3D_DATA_FLOAT;
391     }
392 }
393
394 static void *shader_sm4_init(const DWORD *byte_code, const struct wined3d_shader_signature *output_signature)
395 {
396     struct wined3d_sm4_data *priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv));
397     if (!priv)
398     {
399         ERR("Failed to allocate private data\n");
400         return NULL;
401     }
402
403     priv->output_signature = output_signature;
404     list_init(&priv->src_free);
405     list_init(&priv->src);
406
407     return priv;
408 }
409
410 static void shader_sm4_free(void *data)
411 {
412     struct wined3d_shader_src_param_entry *e1, *e2;
413     struct wined3d_sm4_data *priv = data;
414
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)
417     {
418         HeapFree(GetProcessHeap(), 0, e1);
419     }
420     HeapFree(GetProcessHeap(), 0, priv);
421 }
422
423 static struct wined3d_shader_src_param *get_src_param(struct wined3d_sm4_data *priv)
424 {
425     struct wined3d_shader_src_param_entry *e;
426     struct list *elem;
427
428     if (!list_empty(&priv->src_free))
429     {
430         elem = list_head(&priv->src_free);
431         list_remove(elem);
432     }
433     else
434     {
435         if (!(e = HeapAlloc(GetProcessHeap(), 0, sizeof(*e))))
436             return NULL;
437         elem = &e->entry;
438     }
439
440     list_add_tail(&priv->src, elem);
441     e = LIST_ENTRY(elem, struct wined3d_shader_src_param_entry, entry);
442     return &e->param;
443 }
444
445 static void shader_sm4_read_header(void *data, const DWORD **ptr, struct wined3d_shader_version *shader_version)
446 {
447     struct wined3d_sm4_data *priv = data;
448     DWORD version_token;
449
450     priv->end = *ptr;
451
452     version_token = *(*ptr)++;
453     TRACE("version: 0x%08x\n", version_token);
454
455     TRACE("token count: %u\n", **ptr);
456     priv->end += *(*ptr)++;
457
458     switch (version_token >> 16)
459     {
460         case WINED3D_SM4_PS:
461             priv->shader_version.type = WINED3D_SHADER_TYPE_PIXEL;
462             break;
463
464         case WINED3D_SM4_VS:
465             priv->shader_version.type = WINED3D_SHADER_TYPE_VERTEX;
466             break;
467
468         case WINED3D_SM4_GS:
469             priv->shader_version.type = WINED3D_SHADER_TYPE_GEOMETRY;
470             break;
471
472         default:
473             FIXME("Unrecognized shader type %#x\n", version_token >> 16);
474     }
475     priv->shader_version.major = WINED3D_SM4_VERSION_MAJOR(version_token);
476     priv->shader_version.minor = WINED3D_SM4_VERSION_MINOR(version_token);
477
478     *shader_version = priv->shader_version;
479 }
480
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)
483 {
484     if (addressing & WINED3D_SM4_ADDRESSING_RELATIVE)
485     {
486         struct wined3d_shader_src_param *rel_addr = get_src_param(priv);
487
488         if (!(reg_idx->rel_addr = rel_addr))
489         {
490             ERR("Failed to get src param for relative addressing.\n");
491             return FALSE;
492         }
493
494         if (addressing & WINED3D_SM4_ADDRESSING_OFFSET)
495             reg_idx->offset = *(*ptr)++;
496         else
497             reg_idx->offset = 0;
498         shader_sm4_read_src_param(priv, ptr, WINED3D_DATA_INT, rel_addr);
499     }
500     else
501     {
502         reg_idx->rel_addr = NULL;
503         reg_idx->offset = *(*ptr)++;
504     }
505
506     return TRUE;
507 }
508
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)
512 {
513     enum wined3d_sm4_register_type register_type;
514     DWORD token = *(*ptr)++;
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         param->type = WINED3DSPR_TEMP;
522     }
523     else
524     {
525         param->type = register_type_table[register_type];
526     }
527     param->data_type = data_type;
528
529     if (token & WINED3D_SM4_REGISTER_MODIFIER)
530     {
531         DWORD m = *(*ptr)++;
532
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. */
536         switch (m)
537         {
538             case 0x41:
539                 *modifier = WINED3DSPSM_NEG;
540                 break;
541
542             case 0x81:
543                 *modifier = WINED3DSPSM_ABS;
544                 break;
545
546             default:
547                 FIXME("Skipping modifier 0x%08x.\n", m);
548                 *modifier = WINED3DSPSM_NONE;
549                 break;
550         }
551     }
552     else
553     {
554         *modifier = WINED3DSPSM_NONE;
555     }
556
557     order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
558
559     if (order < 1)
560         param->idx[0].offset = ~0U;
561     else
562     {
563         DWORD addressing = (token & WINED3D_SM4_ADDRESSING_MASK0) >> WINED3D_SM4_ADDRESSING_SHIFT0;
564         if (!(shader_sm4_read_reg_idx(priv, ptr, addressing, &param->idx[0])))
565         {
566             ERR("Failed to read register index.\n");
567             return FALSE;
568         }
569     }
570
571     if (order < 2)
572         param->idx[1].offset = ~0U;
573     else
574     {
575         DWORD addressing = (token & WINED3D_SM4_ADDRESSING_MASK1) >> WINED3D_SM4_ADDRESSING_SHIFT1;
576         if (!(shader_sm4_read_reg_idx(priv, ptr, addressing, &param->idx[1])))
577         {
578             ERR("Failed to read register index.\n");
579             return FALSE;
580         }
581     }
582
583     if (order > 2)
584         FIXME("Unhandled order %u.\n", order);
585
586     if (register_type == WINED3D_SM4_RT_IMMCONST)
587     {
588         enum wined3d_sm4_immconst_type immconst_type =
589                 (token & WINED3D_SM4_IMMCONST_TYPE_MASK) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT;
590
591         switch (immconst_type)
592         {
593             case WINED3D_SM4_IMMCONST_SCALAR:
594                 param->immconst_type = WINED3D_IMMCONST_SCALAR;
595                 memcpy(param->immconst_data, *ptr, 1 * sizeof(DWORD));
596                 *ptr += 1;
597                 break;
598
599             case WINED3D_SM4_IMMCONST_VEC4:
600                 param->immconst_type = WINED3D_IMMCONST_VEC4;
601                 memcpy(param->immconst_data, *ptr, 4 * sizeof(DWORD));
602                 *ptr += 4;
603                 break;
604
605             default:
606                 FIXME("Unhandled immediate constant type %#x.\n", immconst_type);
607                 break;
608         }
609     }
610
611     map_register(priv, param);
612
613     return TRUE;
614 }
615
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)
618 {
619     DWORD token = **ptr;
620
621     if (!shader_sm4_read_param(priv, ptr, data_type, &src_param->reg, &src_param->modifiers))
622     {
623         ERR("Failed to read parameter.\n");
624         return FALSE;
625     }
626
627     if (src_param->reg.type == WINED3DSPR_IMMCONST)
628     {
629         src_param->swizzle = WINED3DSP_NOSWIZZLE;
630     }
631     else
632     {
633         enum wined3d_sm4_swizzle_type swizzle_type =
634                 (token & WINED3D_SM4_SWIZZLE_TYPE_MASK) >> WINED3D_SM4_SWIZZLE_TYPE_SHIFT;
635
636         switch (swizzle_type)
637         {
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;
641                 break;
642
643             case WINED3D_SM4_SWIZZLE_VEC4:
644                 src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
645                 break;
646
647             default:
648                 FIXME("Unhandled swizzle type %#x.\n", swizzle_type);
649                 break;
650         }
651     }
652
653     return TRUE;
654 }
655
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)
658 {
659     enum wined3d_shader_src_modifier modifier;
660     DWORD token = **ptr;
661
662     if (!shader_sm4_read_param(priv, ptr, data_type, &dst_param->reg, &modifier))
663     {
664         ERR("Failed to read parameter.\n");
665         return FALSE;
666     }
667
668     if (modifier != WINED3DSPSM_NONE)
669     {
670         ERR("Invalid source modifier %#x on destination register.\n", modifier);
671         return FALSE;
672     }
673
674     dst_param->write_mask = (token & WINED3D_SM4_WRITEMASK_MASK) >> WINED3D_SM4_WRITEMASK_SHIFT;
675     dst_param->modifiers = 0;
676     dst_param->shift = 0;
677
678     return TRUE;
679 }
680
681 static void shader_sm4_read_instruction(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins)
682 {
683     const struct wined3d_sm4_opcode_info *opcode_info;
684     struct wined3d_sm4_data *priv = data;
685     DWORD opcode_token, opcode;
686     const DWORD *p;
687     UINT i, len;
688
689     list_move_head(&priv->src_free, &priv->src);
690
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;
694
695     if (TRACE_ON(d3d_bytecode))
696     {
697         TRACE_(d3d_bytecode)("[ %08x ", opcode_token);
698         for (i = 0; i < len; ++i)
699         {
700             TRACE_(d3d_bytecode)("%08x ", (*ptr)[i]);
701         }
702         TRACE_(d3d_bytecode)("]\n");
703     }
704
705     if (!(opcode_info = get_opcode_info(opcode)))
706     {
707         FIXME("Unrecognized opcode %#x, opcode_token 0x%08x.\n", opcode, opcode_token);
708         ins->handler_idx = WINED3DSIH_TABLE_SIZE;
709         *ptr += len;
710         return;
711     }
712
713     ins->handler_idx = opcode_info->handler_idx;
714     ins->flags = 0;
715     ins->coissue = 0;
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;
721
722     p = *ptr;
723     *ptr += len;
724
725     if (opcode_token & WINED3D_SM4_INSTRUCTION_MODIFIER)
726     {
727         DWORD modifier = *p++;
728         FIXME("Skipping modifier 0x%08x.\n", modifier);
729     }
730
731     if (opcode == WINED3D_SM4_OP_DCL_CONSTANT_BUFFER)
732     {
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;
736     }
737     else if (opcode == WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY)
738     {
739         enum wined3d_sm4_output_primitive_type primitive_type;
740
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))
743         {
744             FIXME("Unhandled output primitive type %#x.\n", primitive_type);
745             ins->declaration.primitive_type = WINED3D_PT_UNDEFINED;
746         }
747         else
748         {
749             ins->declaration.primitive_type = output_primitive_type_table[primitive_type];
750         }
751     }
752     else if (opcode == WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE)
753     {
754         enum wined3d_sm4_input_primitive_type primitive_type;
755
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))
758         {
759             FIXME("Unhandled input primitive type %#x.\n", primitive_type);
760             ins->declaration.primitive_type = WINED3D_PT_UNDEFINED;
761         }
762         else
763         {
764             ins->declaration.primitive_type = input_primitive_type_table[primitive_type];
765         }
766     }
767     else if (opcode == WINED3D_SM4_OP_DCL_VERTICES_OUT)
768     {
769         ins->declaration.count = *p++;
770     }
771     else
772     {
773         for (i = 0; i < ins->dst_count; ++i)
774         {
775             if (!(shader_sm4_read_dst_param(priv, &p, map_data_type(opcode_info->dst_info[i]), &priv->dst_param[i])))
776             {
777                 ins->handler_idx = WINED3DSIH_TABLE_SIZE;
778                 return;
779             }
780         }
781
782         for (i = 0; i < ins->src_count; ++i)
783         {
784             if (!(shader_sm4_read_src_param(priv, &p, map_data_type(opcode_info->src_info[i]), &priv->src_param[i])))
785             {
786                 ins->handler_idx = WINED3DSIH_TABLE_SIZE;
787                 return;
788             }
789         }
790     }
791 }
792
793 static BOOL shader_sm4_is_end(void *data, const DWORD **ptr)
794 {
795     struct wined3d_sm4_data *priv = data;
796     return *ptr == priv->end;
797 }
798
799 const struct wined3d_shader_frontend sm4_shader_frontend =
800 {
801     shader_sm4_init,
802     shader_sm4_free,
803     shader_sm4_read_header,
804     shader_sm4_read_instruction,
805     shader_sm4_is_end,
806 };