dxgi: Implement IDXGIAdapter::GetDesc().
[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_LENGTH_SHIFT    24
27 #define WINED3D_SM4_INSTRUCTION_LENGTH_MASK     (0xf << WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT)
28
29 #define WINED3D_SM4_OPCODE_MASK                 0xff
30
31 #define WINED3D_SM4_REGISTER_ORDER_SHIFT        20
32 #define WINED3D_SM4_REGISTER_ORDER_MASK         (0x3 << WINED3D_SM4_REGISTER_ORDER_SHIFT)
33
34 #define WINED3D_SM4_REGISTER_TYPE_SHIFT         12
35 #define WINED3D_SM4_REGISTER_TYPE_MASK          (0xf << WINED3D_SM4_REGISTER_TYPE_SHIFT)
36
37 #define WINED3D_SM4_IMMCONST_TYPE_SHIFT         0
38 #define WINED3D_SM4_IMMCONST_TYPE_MASK          (0x3 << WINED3D_SM4_IMMCONST_TYPE_SHIFT)
39
40 #define WINED3D_SM4_WRITEMASK_SHIFT             4
41 #define WINED3D_SM4_WRITEMASK_MASK              (0xf << WINED3D_SM4_WRITEMASK_SHIFT)
42
43 #define WINED3D_SM4_SWIZZLE_SHIFT               4
44 #define WINED3D_SM4_SWIZZLE_MASK                (0xff << WINED3D_SM4_SWIZZLE_SHIFT)
45
46 #define WINED3D_SM4_VERSION_MAJOR(version)      (((version) >> 4) & 0xf)
47 #define WINED3D_SM4_VERSION_MINOR(version)      (((version) >> 0) & 0xf)
48
49 enum wined3d_sm4_opcode
50 {
51     WINED3D_SM4_OP_ADD      = 0x00,
52     WINED3D_SM4_OP_DP3      = 0x10,
53     WINED3D_SM4_OP_DP4      = 0x11,
54     WINED3D_SM4_OP_EXP      = 0x19,
55     WINED3D_SM4_OP_LOG      = 0x2f,
56     WINED3D_SM4_OP_MIN      = 0x33,
57     WINED3D_SM4_OP_MAX      = 0x34,
58     WINED3D_SM4_OP_MOV      = 0x36,
59     WINED3D_SM4_OP_MUL      = 0x38,
60     WINED3D_SM4_OP_RET      = 0x3e,
61     WINED3D_SM4_OP_RSQ      = 0x44,
62     WINED3D_SM4_OP_SINCOS   = 0x4d,
63 };
64
65 enum wined3d_sm4_register_type
66 {
67     WINED3D_SM4_RT_TEMP         = 0x0,
68     WINED3D_SM4_RT_INPUT        = 0x1,
69     WINED3D_SM4_RT_OUTPUT       = 0x2,
70     WINED3D_SM4_RT_IMMCONST     = 0x4,
71     WINED3D_SM4_RT_CONSTBUFFER  = 0x8,
72 };
73
74 enum wined3d_sm4_immconst_type
75 {
76     WINED3D_SM4_IMMCONST_FLOAT  = 0x1,
77     WINED3D_SM4_IMMCONST_FLOAT4 = 0x2,
78 };
79
80 struct wined3d_sm4_data
81 {
82     struct wined3d_shader_version shader_version;
83     const DWORD *end;
84     const struct wined3d_shader_signature *output_signature;
85 };
86
87 struct wined3d_sm4_opcode_info
88 {
89     enum wined3d_sm4_opcode opcode;
90     enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx;
91     UINT dst_count;
92     UINT src_count;
93 };
94
95 struct sysval_map
96 {
97     enum wined3d_sysval_semantic sysval;
98     WINED3DSHADER_PARAM_REGISTER_TYPE register_type;
99     UINT register_idx;
100 };
101
102 static const struct wined3d_sm4_opcode_info opcode_table[] =
103 {
104     {WINED3D_SM4_OP_ADD,    WINED3DSIH_ADD,         1,  2},
105     {WINED3D_SM4_OP_DP3,    WINED3DSIH_DP3,         1,  2},
106     {WINED3D_SM4_OP_DP4,    WINED3DSIH_DP4,         1,  2},
107     {WINED3D_SM4_OP_EXP,    WINED3DSIH_EXP,         1,  1},
108     {WINED3D_SM4_OP_LOG,    WINED3DSIH_LOG,         1,  1},
109     {WINED3D_SM4_OP_MIN,    WINED3DSIH_MIN,         1,  2},
110     {WINED3D_SM4_OP_MAX,    WINED3DSIH_MAX,         1,  2},
111     {WINED3D_SM4_OP_MOV,    WINED3DSIH_MOV,         1,  1},
112     {WINED3D_SM4_OP_MUL,    WINED3DSIH_MUL,         1,  2},
113     {WINED3D_SM4_OP_RET,    WINED3DSIH_RET,         0,  0},
114     {WINED3D_SM4_OP_RSQ,    WINED3DSIH_RSQ,         1,  1},
115     {WINED3D_SM4_OP_SINCOS, WINED3DSIH_SINCOS,      1,  2},
116 };
117
118 static const WINED3DSHADER_PARAM_REGISTER_TYPE register_type_table[] =
119 {
120     /* WINED3D_SM4_RT_TEMP */           WINED3DSPR_TEMP,
121     /* WINED3D_SM4_RT_INPUT */          WINED3DSPR_INPUT,
122     /* WINED3D_SM4_RT_OUTPUT */         WINED3DSPR_OUTPUT,
123     /* UNKNOWN */                       0,
124     /* WINED3D_SM4_RT_IMMCONST */       WINED3DSPR_IMMCONST,
125     /* UNKNOWN */                       0,
126     /* UNKNOWN */                       0,
127     /* UNKNOWN */                       0,
128     /* WINED3D_SM4_RT_CONSTBUFFER */    WINED3DSPR_CONSTBUFFER,
129 };
130
131 static const struct sysval_map sysval_map[] =
132 {
133     {WINED3D_SV_DEPTH,      WINED3DSPR_DEPTHOUT,    0},
134     {WINED3D_SV_TARGET0,    WINED3DSPR_COLOROUT,    0},
135     {WINED3D_SV_TARGET1,    WINED3DSPR_COLOROUT,    1},
136     {WINED3D_SV_TARGET2,    WINED3DSPR_COLOROUT,    2},
137     {WINED3D_SV_TARGET3,    WINED3DSPR_COLOROUT,    3},
138     {WINED3D_SV_TARGET4,    WINED3DSPR_COLOROUT,    4},
139     {WINED3D_SV_TARGET5,    WINED3DSPR_COLOROUT,    5},
140     {WINED3D_SV_TARGET6,    WINED3DSPR_COLOROUT,    6},
141     {WINED3D_SV_TARGET7,    WINED3DSPR_COLOROUT,    7},
142 };
143
144 static const struct wined3d_sm4_opcode_info *get_opcode_info(enum wined3d_sm4_opcode opcode)
145 {
146     unsigned int i;
147
148     for (i = 0; i < sizeof(opcode_table) / sizeof(*opcode_table); ++i)
149     {
150         if (opcode == opcode_table[i].opcode) return &opcode_table[i];
151     }
152
153     return NULL;
154 }
155
156 static void map_sysval(enum wined3d_sysval_semantic sysval, struct wined3d_shader_register *reg)
157 {
158     unsigned int i;
159
160     for (i = 0; i < sizeof(sysval_map) / sizeof(*sysval_map); ++i)
161     {
162         if (sysval == sysval_map[i].sysval)
163         {
164             reg->type = sysval_map[i].register_type;
165             reg->idx = sysval_map[i].register_idx;
166         }
167     }
168 }
169
170 static void map_register(struct wined3d_sm4_data *priv, struct wined3d_shader_register *reg)
171 {
172     switch (priv->shader_version.type)
173     {
174         case WINED3D_SHADER_TYPE_PIXEL:
175             if (reg->type == WINED3DSPR_OUTPUT)
176             {
177                 unsigned int i;
178                 const struct wined3d_shader_signature *s = priv->output_signature;
179
180                 if (!s)
181                 {
182                     ERR("Shader has no output signature, unable to map register.\n");
183                     break;
184                 }
185
186                 for (i = 0; i < s->element_count; ++i)
187                 {
188                     if (s->elements[i].register_idx == reg->idx)
189                     {
190                         map_sysval(s->elements[i].sysval_semantic, reg);
191                         break;
192                     }
193                 }
194             }
195             break;
196
197         default:
198             break;
199     }
200 }
201
202 static void *shader_sm4_init(const DWORD *byte_code, const struct wined3d_shader_signature *output_signature)
203 {
204     struct wined3d_sm4_data *priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv));
205     if (!priv)
206     {
207         ERR("Failed to allocate private data\n");
208         return NULL;
209     }
210
211     priv->output_signature = output_signature;
212
213     return priv;
214 }
215
216 static void shader_sm4_free(void *data)
217 {
218     HeapFree(GetProcessHeap(), 0, data);
219 }
220
221 static void shader_sm4_read_header(void *data, const DWORD **ptr, struct wined3d_shader_version *shader_version)
222 {
223     struct wined3d_sm4_data *priv = data;
224     DWORD version_token;
225
226     priv->end = *ptr;
227
228     version_token = *(*ptr)++;
229     TRACE("version: 0x%08x\n", version_token);
230
231     TRACE("token count: %u\n", **ptr);
232     priv->end += *(*ptr)++;
233
234     switch (version_token >> 16)
235     {
236         case WINED3D_SM4_PS:
237             priv->shader_version.type = WINED3D_SHADER_TYPE_PIXEL;
238             break;
239
240         case WINED3D_SM4_VS:
241             priv->shader_version.type = WINED3D_SHADER_TYPE_VERTEX;
242             break;
243
244         case WINED3D_SM4_GS:
245             priv->shader_version.type = WINED3D_SHADER_TYPE_GEOMETRY;
246             break;
247
248         default:
249             FIXME("Unrecognized shader type %#x\n", version_token >> 16);
250     }
251     priv->shader_version.major = WINED3D_SM4_VERSION_MAJOR(version_token);
252     priv->shader_version.minor = WINED3D_SM4_VERSION_MINOR(version_token);
253
254     *shader_version = priv->shader_version;
255 }
256
257 static void shader_sm4_read_opcode(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins,
258         UINT *param_size)
259 {
260     const struct wined3d_sm4_opcode_info *opcode_info;
261     DWORD token = *(*ptr)++;
262     DWORD opcode = token & WINED3D_SM4_OPCODE_MASK;
263
264     *param_size = ((token & WINED3D_SM4_INSTRUCTION_LENGTH_MASK) >> WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT) - 1;
265
266     opcode_info = get_opcode_info(opcode);
267     if (!opcode_info)
268     {
269         FIXME("Unrecognized opcode %#x, token 0x%08x\n", opcode, token);
270         ins->handler_idx = WINED3DSIH_TABLE_SIZE;
271         return;
272     }
273
274     ins->handler_idx = opcode_info->handler_idx;
275     ins->flags = 0;
276     ins->coissue = 0;
277     ins->predicate = 0;
278     ins->dst_count = opcode_info->dst_count;
279     ins->src_count = opcode_info->src_count;
280 }
281
282 static void shader_sm4_read_src_param(void *data, const DWORD **ptr, struct wined3d_shader_src_param *src_param,
283         struct wined3d_shader_src_param *src_rel_addr)
284 {
285     struct wined3d_sm4_data *priv = data;
286     DWORD token = *(*ptr)++;
287     enum wined3d_sm4_register_type register_type;
288     DWORD order;
289
290     register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
291     if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
292     {
293         FIXME("Unhandled register type %#x\n", register_type);
294         src_param->reg.type = WINED3DSPR_TEMP;
295     }
296     else
297     {
298         src_param->reg.type = register_type_table[register_type];
299     }
300
301     order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
302
303     if (order < 1) src_param->reg.idx = ~0U;
304     else src_param->reg.idx = *(*ptr)++;
305
306     if (order < 2) src_param->reg.array_idx = ~0U;
307     else src_param->reg.array_idx = *(*ptr)++;
308
309     if (order > 2) FIXME("Unhandled order %u.\n", order);
310
311     if (register_type == WINED3D_SM4_RT_IMMCONST)
312     {
313         enum wined3d_sm4_immconst_type immconst_type =
314                 (token & WINED3D_SM4_IMMCONST_TYPE_MASK) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT;
315         src_param->swizzle = WINED3DSP_NOSWIZZLE;
316
317         switch(immconst_type)
318         {
319             case WINED3D_SM4_IMMCONST_FLOAT:
320                 src_param->reg.immconst_type = WINED3D_IMMCONST_FLOAT;
321                 memcpy(src_param->reg.immconst_data, *ptr, 1 * sizeof(DWORD));
322                 *ptr += 1;
323                 break;
324
325             case WINED3D_SM4_IMMCONST_FLOAT4:
326                 src_param->reg.immconst_type = WINED3D_IMMCONST_FLOAT4;
327                 memcpy(src_param->reg.immconst_data, *ptr, 4 * sizeof(DWORD));
328                 *ptr += 4;
329                 break;
330
331             default:
332                 FIXME("Unhandled immediate constant type %#x\n", immconst_type);
333                 break;
334         }
335     }
336     else
337     {
338         src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
339     }
340
341     src_param->modifiers = 0;
342     src_param->reg.rel_addr = NULL;
343
344     map_register(priv, &src_param->reg);
345 }
346
347 static void shader_sm4_read_dst_param(void *data, const DWORD **ptr, struct wined3d_shader_dst_param *dst_param,
348         struct wined3d_shader_src_param *dst_rel_addr)
349 {
350     struct wined3d_sm4_data *priv = data;
351     DWORD token = *(*ptr)++;
352     enum wined3d_sm4_register_type register_type;
353     DWORD order;
354
355     register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
356     if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
357     {
358         FIXME("Unhandled register type %#x\n", register_type);
359         dst_param->reg.type = WINED3DSPR_TEMP;
360     }
361     else
362     {
363         dst_param->reg.type = register_type_table[register_type];
364     }
365
366     order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
367
368     if (order < 1) dst_param->reg.idx = ~0U;
369     else dst_param->reg.idx = *(*ptr)++;
370
371     if (order < 2) dst_param->reg.array_idx = ~0U;
372     else dst_param->reg.array_idx = *(*ptr)++;
373
374     if (order > 2) FIXME("Unhandled order %u.\n", order);
375
376     dst_param->write_mask = (token & WINED3D_SM4_WRITEMASK_MASK) >> WINED3D_SM4_WRITEMASK_SHIFT;
377     dst_param->modifiers = 0;
378     dst_param->shift = 0;
379     dst_param->reg.rel_addr = NULL;
380
381     map_register(priv, &dst_param->reg);
382 }
383
384 static void shader_sm4_read_semantic(const DWORD **ptr, struct wined3d_shader_semantic *semantic)
385 {
386     FIXME("ptr %p, semantic %p stub!\n", ptr, semantic);
387 }
388
389 static void shader_sm4_read_comment(const DWORD **ptr, const char **comment)
390 {
391     FIXME("ptr %p, comment %p stub!\n", ptr, comment);
392     *comment = NULL;
393 }
394
395 static BOOL shader_sm4_is_end(void *data, const DWORD **ptr)
396 {
397     struct wined3d_sm4_data *priv = data;
398     return *ptr == priv->end;
399 }
400
401 const struct wined3d_shader_frontend sm4_shader_frontend =
402 {
403     shader_sm4_init,
404     shader_sm4_free,
405     shader_sm4_read_header,
406     shader_sm4_read_opcode,
407     shader_sm4_read_src_param,
408     shader_sm4_read_dst_param,
409     shader_sm4_read_semantic,
410     shader_sm4_read_comment,
411     shader_sm4_is_end,
412 };