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