wined3d: Recognize the SM4 ftoi 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_OPCODE_MASK                 0xff
32
33 #define WINED3D_SM4_REGISTER_MODIFIER           (1 << 31)
34
35 #define WINED3D_SM4_REGISTER_ORDER_SHIFT        20
36 #define WINED3D_SM4_REGISTER_ORDER_MASK         (0x3 << WINED3D_SM4_REGISTER_ORDER_SHIFT)
37
38 #define WINED3D_SM4_REGISTER_TYPE_SHIFT         12
39 #define WINED3D_SM4_REGISTER_TYPE_MASK          (0xf << WINED3D_SM4_REGISTER_TYPE_SHIFT)
40
41 #define WINED3D_SM4_IMMCONST_TYPE_SHIFT         0
42 #define WINED3D_SM4_IMMCONST_TYPE_MASK          (0x3 << WINED3D_SM4_IMMCONST_TYPE_SHIFT)
43
44 #define WINED3D_SM4_WRITEMASK_SHIFT             4
45 #define WINED3D_SM4_WRITEMASK_MASK              (0xf << WINED3D_SM4_WRITEMASK_SHIFT)
46
47 #define WINED3D_SM4_SWIZZLE_SHIFT               4
48 #define WINED3D_SM4_SWIZZLE_MASK                (0xff << WINED3D_SM4_SWIZZLE_SHIFT)
49
50 #define WINED3D_SM4_VERSION_MAJOR(version)      (((version) >> 4) & 0xf)
51 #define WINED3D_SM4_VERSION_MINOR(version)      (((version) >> 0) & 0xf)
52
53 enum wined3d_sm4_opcode
54 {
55     WINED3D_SM4_OP_ADD          = 0x00,
56     WINED3D_SM4_OP_AND          = 0x01,
57     WINED3D_SM4_OP_BREAK        = 0x02,
58     WINED3D_SM4_OP_BREAKC       = 0x03,
59     WINED3D_SM4_OP_CUT          = 0x09,
60     WINED3D_SM4_OP_DIV          = 0x0e,
61     WINED3D_SM4_OP_DP3          = 0x10,
62     WINED3D_SM4_OP_DP4          = 0x11,
63     WINED3D_SM4_OP_EMIT         = 0x13,
64     WINED3D_SM4_OP_ENDIF        = 0x15,
65     WINED3D_SM4_OP_ENDLOOP      = 0x16,
66     WINED3D_SM4_OP_EXP          = 0x19,
67     WINED3D_SM4_OP_FRC          = 0x1a,
68     WINED3D_SM4_OP_FTOI         = 0x1b,
69     WINED3D_SM4_OP_IADD         = 0x1e,
70     WINED3D_SM4_OP_IF           = 0x1f,
71     WINED3D_SM4_OP_IGE          = 0x21,
72     WINED3D_SM4_OP_IMUL         = 0x26,
73     WINED3D_SM4_OP_ITOF         = 0x2b,
74     WINED3D_SM4_OP_LOG          = 0x2f,
75     WINED3D_SM4_OP_LOOP         = 0x30,
76     WINED3D_SM4_OP_LT           = 0x31,
77     WINED3D_SM4_OP_MAD          = 0x32,
78     WINED3D_SM4_OP_MIN          = 0x33,
79     WINED3D_SM4_OP_MAX          = 0x34,
80     WINED3D_SM4_OP_MOV          = 0x36,
81     WINED3D_SM4_OP_MOVC         = 0x37,
82     WINED3D_SM4_OP_MUL          = 0x38,
83     WINED3D_SM4_OP_RET          = 0x3e,
84     WINED3D_SM4_OP_RSQ          = 0x44,
85     WINED3D_SM4_OP_SAMPLE       = 0x45,
86     WINED3D_SM4_OP_SAMPLE_LOD   = 0x48,
87     WINED3D_SM4_OP_SINCOS       = 0x4d,
88     WINED3D_SM4_OP_UTOF         = 0x56,
89 };
90
91 enum wined3d_sm4_register_type
92 {
93     WINED3D_SM4_RT_TEMP         = 0x0,
94     WINED3D_SM4_RT_INPUT        = 0x1,
95     WINED3D_SM4_RT_OUTPUT       = 0x2,
96     WINED3D_SM4_RT_IMMCONST     = 0x4,
97     WINED3D_SM4_RT_SAMPLER      = 0x6,
98     WINED3D_SM4_RT_CONSTBUFFER  = 0x8,
99     WINED3D_SM4_RT_NULL         = 0xd,
100 };
101
102 enum wined3d_sm4_immconst_type
103 {
104     WINED3D_SM4_IMMCONST_SCALAR = 0x1,
105     WINED3D_SM4_IMMCONST_VEC4   = 0x2,
106 };
107
108 struct wined3d_sm4_data
109 {
110     struct wined3d_shader_version shader_version;
111     const DWORD *end;
112     const struct wined3d_shader_signature *output_signature;
113 };
114
115 struct wined3d_sm4_opcode_info
116 {
117     enum wined3d_sm4_opcode opcode;
118     enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx;
119     UINT dst_count;
120     UINT src_count;
121 };
122
123 struct sysval_map
124 {
125     enum wined3d_sysval_semantic sysval;
126     WINED3DSHADER_PARAM_REGISTER_TYPE register_type;
127     UINT register_idx;
128 };
129
130 static const struct wined3d_sm4_opcode_info opcode_table[] =
131 {
132     {WINED3D_SM4_OP_ADD,        WINED3DSIH_ADD,         1,  2},
133     {WINED3D_SM4_OP_AND,        WINED3DSIH_AND,         1,  2},
134     {WINED3D_SM4_OP_BREAK,      WINED3DSIH_BREAK,       0,  0},
135     {WINED3D_SM4_OP_BREAKC,     WINED3DSIH_BREAKP,      0,  1},
136     {WINED3D_SM4_OP_CUT,        WINED3DSIH_CUT,         0,  0},
137     {WINED3D_SM4_OP_DIV,        WINED3DSIH_DIV,         1,  2},
138     {WINED3D_SM4_OP_DP3,        WINED3DSIH_DP3,         1,  2},
139     {WINED3D_SM4_OP_DP4,        WINED3DSIH_DP4,         1,  2},
140     {WINED3D_SM4_OP_EMIT,       WINED3DSIH_EMIT,        0,  0},
141     {WINED3D_SM4_OP_ENDIF,      WINED3DSIH_ENDIF,       0,  0},
142     {WINED3D_SM4_OP_ENDLOOP,    WINED3DSIH_ENDLOOP,     0,  0},
143     {WINED3D_SM4_OP_EXP,        WINED3DSIH_EXP,         1,  1},
144     {WINED3D_SM4_OP_FRC,        WINED3DSIH_FRC,         1,  1},
145     {WINED3D_SM4_OP_FTOI,       WINED3DSIH_FTOI,        1,  1},
146     {WINED3D_SM4_OP_IADD,       WINED3DSIH_IADD,        1,  2},
147     {WINED3D_SM4_OP_IF,         WINED3DSIH_IF,          0,  1},
148     {WINED3D_SM4_OP_IGE,        WINED3DSIH_IGE,         1,  2},
149     {WINED3D_SM4_OP_IMUL,       WINED3DSIH_IMUL,        2,  2},
150     {WINED3D_SM4_OP_ITOF,       WINED3DSIH_ITOF,        1,  1},
151     {WINED3D_SM4_OP_LOG,        WINED3DSIH_LOG,         1,  1},
152     {WINED3D_SM4_OP_LOOP,       WINED3DSIH_LOOP,        0,  0},
153     {WINED3D_SM4_OP_LT,         WINED3DSIH_LT,          1,  2},
154     {WINED3D_SM4_OP_MAD,        WINED3DSIH_MAD,         1,  3},
155     {WINED3D_SM4_OP_MIN,        WINED3DSIH_MIN,         1,  2},
156     {WINED3D_SM4_OP_MAX,        WINED3DSIH_MAX,         1,  2},
157     {WINED3D_SM4_OP_MOV,        WINED3DSIH_MOV,         1,  1},
158     {WINED3D_SM4_OP_MOVC,       WINED3DSIH_MOVC,        1,  3},
159     {WINED3D_SM4_OP_MUL,        WINED3DSIH_MUL,         1,  2},
160     {WINED3D_SM4_OP_RET,        WINED3DSIH_RET,         0,  0},
161     {WINED3D_SM4_OP_RSQ,        WINED3DSIH_RSQ,         1,  1},
162     {WINED3D_SM4_OP_SAMPLE,     WINED3DSIH_SAMPLE,      1,  3},
163     {WINED3D_SM4_OP_SAMPLE_LOD, WINED3DSIH_SAMPLE_LOD,  1,  4},
164     {WINED3D_SM4_OP_SINCOS,     WINED3DSIH_SINCOS,      2,  1},
165     {WINED3D_SM4_OP_UTOF,       WINED3DSIH_UTOF,        1,  1},
166 };
167
168 static const WINED3DSHADER_PARAM_REGISTER_TYPE register_type_table[] =
169 {
170     /* WINED3D_SM4_RT_TEMP */           WINED3DSPR_TEMP,
171     /* WINED3D_SM4_RT_INPUT */          WINED3DSPR_INPUT,
172     /* WINED3D_SM4_RT_OUTPUT */         WINED3DSPR_OUTPUT,
173     /* UNKNOWN */                       0,
174     /* WINED3D_SM4_RT_IMMCONST */       WINED3DSPR_IMMCONST,
175     /* UNKNOWN */                       0,
176     /* WINED3D_SM4_RT_SAMPLER */        WINED3DSPR_SAMPLER,
177     /* WINED3D_SM4_RT_RESOURCE */       WINED3DSPR_RESOURCE,
178     /* WINED3D_SM4_RT_CONSTBUFFER */    WINED3DSPR_CONSTBUFFER,
179     /* UNKNOWN */                       0,
180     /* UNKNOWN */                       0,
181     /* UNKNOWN */                       0,
182     /* UNKNOWN */                       0,
183     /* WINED3D_SM4_RT_NULL */           WINED3DSPR_NULL,
184 };
185
186 static const struct sysval_map sysval_map[] =
187 {
188     {WINED3D_SV_DEPTH,      WINED3DSPR_DEPTHOUT,    0},
189     {WINED3D_SV_TARGET0,    WINED3DSPR_COLOROUT,    0},
190     {WINED3D_SV_TARGET1,    WINED3DSPR_COLOROUT,    1},
191     {WINED3D_SV_TARGET2,    WINED3DSPR_COLOROUT,    2},
192     {WINED3D_SV_TARGET3,    WINED3DSPR_COLOROUT,    3},
193     {WINED3D_SV_TARGET4,    WINED3DSPR_COLOROUT,    4},
194     {WINED3D_SV_TARGET5,    WINED3DSPR_COLOROUT,    5},
195     {WINED3D_SV_TARGET6,    WINED3DSPR_COLOROUT,    6},
196     {WINED3D_SV_TARGET7,    WINED3DSPR_COLOROUT,    7},
197 };
198
199 static const struct wined3d_sm4_opcode_info *get_opcode_info(enum wined3d_sm4_opcode opcode)
200 {
201     unsigned int i;
202
203     for (i = 0; i < sizeof(opcode_table) / sizeof(*opcode_table); ++i)
204     {
205         if (opcode == opcode_table[i].opcode) return &opcode_table[i];
206     }
207
208     return NULL;
209 }
210
211 static void map_sysval(enum wined3d_sysval_semantic sysval, struct wined3d_shader_register *reg)
212 {
213     unsigned int i;
214
215     for (i = 0; i < sizeof(sysval_map) / sizeof(*sysval_map); ++i)
216     {
217         if (sysval == sysval_map[i].sysval)
218         {
219             reg->type = sysval_map[i].register_type;
220             reg->idx = sysval_map[i].register_idx;
221         }
222     }
223 }
224
225 static void map_register(struct wined3d_sm4_data *priv, struct wined3d_shader_register *reg)
226 {
227     switch (priv->shader_version.type)
228     {
229         case WINED3D_SHADER_TYPE_PIXEL:
230             if (reg->type == WINED3DSPR_OUTPUT)
231             {
232                 unsigned int i;
233                 const struct wined3d_shader_signature *s = priv->output_signature;
234
235                 if (!s)
236                 {
237                     ERR("Shader has no output signature, unable to map register.\n");
238                     break;
239                 }
240
241                 for (i = 0; i < s->element_count; ++i)
242                 {
243                     if (s->elements[i].register_idx == reg->idx)
244                     {
245                         map_sysval(s->elements[i].sysval_semantic, reg);
246                         break;
247                     }
248                 }
249             }
250             break;
251
252         default:
253             break;
254     }
255 }
256
257 static void *shader_sm4_init(const DWORD *byte_code, const struct wined3d_shader_signature *output_signature)
258 {
259     struct wined3d_sm4_data *priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv));
260     if (!priv)
261     {
262         ERR("Failed to allocate private data\n");
263         return NULL;
264     }
265
266     priv->output_signature = output_signature;
267
268     return priv;
269 }
270
271 static void shader_sm4_free(void *data)
272 {
273     HeapFree(GetProcessHeap(), 0, data);
274 }
275
276 static void shader_sm4_read_header(void *data, const DWORD **ptr, struct wined3d_shader_version *shader_version)
277 {
278     struct wined3d_sm4_data *priv = data;
279     DWORD version_token;
280
281     priv->end = *ptr;
282
283     version_token = *(*ptr)++;
284     TRACE("version: 0x%08x\n", version_token);
285
286     TRACE("token count: %u\n", **ptr);
287     priv->end += *(*ptr)++;
288
289     switch (version_token >> 16)
290     {
291         case WINED3D_SM4_PS:
292             priv->shader_version.type = WINED3D_SHADER_TYPE_PIXEL;
293             break;
294
295         case WINED3D_SM4_VS:
296             priv->shader_version.type = WINED3D_SHADER_TYPE_VERTEX;
297             break;
298
299         case WINED3D_SM4_GS:
300             priv->shader_version.type = WINED3D_SHADER_TYPE_GEOMETRY;
301             break;
302
303         default:
304             FIXME("Unrecognized shader type %#x\n", version_token >> 16);
305     }
306     priv->shader_version.major = WINED3D_SM4_VERSION_MAJOR(version_token);
307     priv->shader_version.minor = WINED3D_SM4_VERSION_MINOR(version_token);
308
309     *shader_version = priv->shader_version;
310 }
311
312 static void shader_sm4_read_opcode(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins,
313         UINT *param_size)
314 {
315     const struct wined3d_sm4_opcode_info *opcode_info;
316     DWORD token = *(*ptr)++;
317     DWORD opcode = token & WINED3D_SM4_OPCODE_MASK;
318
319     *param_size = ((token & WINED3D_SM4_INSTRUCTION_LENGTH_MASK) >> WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT) - 1;
320
321     opcode_info = get_opcode_info(opcode);
322     if (!opcode_info)
323     {
324         FIXME("Unrecognized opcode %#x, token 0x%08x\n", opcode, token);
325         ins->handler_idx = WINED3DSIH_TABLE_SIZE;
326         return;
327     }
328
329     ins->handler_idx = opcode_info->handler_idx;
330     ins->flags = 0;
331     ins->coissue = 0;
332     ins->predicate = 0;
333     ins->dst_count = opcode_info->dst_count;
334     ins->src_count = opcode_info->src_count;
335
336     if (token & WINED3D_SM4_INSTRUCTION_MODIFIER)
337     {
338         DWORD modifier = *(*ptr)++;
339         FIXME("Skipping modifier 0x%08x.\n", modifier);
340     }
341 }
342
343 static void shader_sm4_read_src_param(void *data, const DWORD **ptr, struct wined3d_shader_src_param *src_param,
344         struct wined3d_shader_src_param *src_rel_addr)
345 {
346     struct wined3d_sm4_data *priv = data;
347     DWORD token = *(*ptr)++;
348     enum wined3d_sm4_register_type register_type;
349     DWORD order;
350
351     register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
352     if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
353     {
354         FIXME("Unhandled register type %#x\n", register_type);
355         src_param->reg.type = WINED3DSPR_TEMP;
356     }
357     else
358     {
359         src_param->reg.type = register_type_table[register_type];
360     }
361
362     if (token & WINED3D_SM4_REGISTER_MODIFIER)
363     {
364         DWORD modifier = *(*ptr)++;
365         FIXME("Skipping modifier 0x%08x.\n", modifier);
366     }
367
368     order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
369
370     if (order < 1) src_param->reg.idx = ~0U;
371     else src_param->reg.idx = *(*ptr)++;
372
373     if (order < 2) src_param->reg.array_idx = ~0U;
374     else src_param->reg.array_idx = *(*ptr)++;
375
376     if (order > 2) FIXME("Unhandled order %u.\n", order);
377
378     if (register_type == WINED3D_SM4_RT_IMMCONST)
379     {
380         enum wined3d_sm4_immconst_type immconst_type =
381                 (token & WINED3D_SM4_IMMCONST_TYPE_MASK) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT;
382         src_param->swizzle = WINED3DSP_NOSWIZZLE;
383
384         switch(immconst_type)
385         {
386             case WINED3D_SM4_IMMCONST_SCALAR:
387                 src_param->reg.immconst_type = WINED3D_IMMCONST_SCALAR;
388                 memcpy(src_param->reg.immconst_data, *ptr, 1 * sizeof(DWORD));
389                 *ptr += 1;
390                 break;
391
392             case WINED3D_SM4_IMMCONST_VEC4:
393                 src_param->reg.immconst_type = WINED3D_IMMCONST_VEC4;
394                 memcpy(src_param->reg.immconst_data, *ptr, 4 * sizeof(DWORD));
395                 *ptr += 4;
396                 break;
397
398             default:
399                 FIXME("Unhandled immediate constant type %#x\n", immconst_type);
400                 break;
401         }
402     }
403     else
404     {
405         src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
406     }
407
408     src_param->modifiers = 0;
409     src_param->reg.rel_addr = NULL;
410
411     map_register(priv, &src_param->reg);
412 }
413
414 static void shader_sm4_read_dst_param(void *data, const DWORD **ptr, struct wined3d_shader_dst_param *dst_param,
415         struct wined3d_shader_src_param *dst_rel_addr)
416 {
417     struct wined3d_sm4_data *priv = data;
418     DWORD token = *(*ptr)++;
419     enum wined3d_sm4_register_type register_type;
420     DWORD order;
421
422     register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
423     if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
424     {
425         FIXME("Unhandled register type %#x\n", register_type);
426         dst_param->reg.type = WINED3DSPR_TEMP;
427     }
428     else
429     {
430         dst_param->reg.type = register_type_table[register_type];
431     }
432
433     order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
434
435     if (order < 1) dst_param->reg.idx = ~0U;
436     else dst_param->reg.idx = *(*ptr)++;
437
438     if (order < 2) dst_param->reg.array_idx = ~0U;
439     else dst_param->reg.array_idx = *(*ptr)++;
440
441     if (order > 2) FIXME("Unhandled order %u.\n", order);
442
443     dst_param->write_mask = (token & WINED3D_SM4_WRITEMASK_MASK) >> WINED3D_SM4_WRITEMASK_SHIFT;
444     dst_param->modifiers = 0;
445     dst_param->shift = 0;
446     dst_param->reg.rel_addr = NULL;
447
448     map_register(priv, &dst_param->reg);
449 }
450
451 static void shader_sm4_read_semantic(const DWORD **ptr, struct wined3d_shader_semantic *semantic)
452 {
453     FIXME("ptr %p, semantic %p stub!\n", ptr, semantic);
454 }
455
456 static void shader_sm4_read_comment(const DWORD **ptr, const char **comment, UINT *comment_size)
457 {
458     FIXME("ptr %p, comment %p, comment_size %p stub!\n", ptr, comment, comment_size);
459     *comment = NULL;
460 }
461
462 static BOOL shader_sm4_is_end(void *data, const DWORD **ptr)
463 {
464     struct wined3d_sm4_data *priv = data;
465     return *ptr == priv->end;
466 }
467
468 const struct wined3d_shader_frontend sm4_shader_frontend =
469 {
470     shader_sm4_init,
471     shader_sm4_free,
472     shader_sm4_read_header,
473     shader_sm4_read_opcode,
474     shader_sm4_read_src_param,
475     shader_sm4_read_dst_param,
476     shader_sm4_read_semantic,
477     shader_sm4_read_comment,
478     shader_sm4_is_end,
479 };