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