wined3d: Improve shader_sm4_read_opcode() to handle some simple opcodes.
[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 enum wined3d_sm4_opcode
44 {
45     WINED3D_SM4_OP_ADD      = 0x00,
46     WINED3D_SM4_OP_EXP      = 0x19,
47     WINED3D_SM4_OP_MOV      = 0x36,
48     WINED3D_SM4_OP_MUL      = 0x38,
49     WINED3D_SM4_OP_RET      = 0x3e,
50     WINED3D_SM4_OP_SINCOS   = 0x4d,
51 };
52
53 enum wined3d_sm4_register_type
54 {
55     WINED3D_SM4_RT_TEMP     = 0x0,
56     WINED3D_SM4_RT_INPUT    = 0x1,
57     WINED3D_SM4_RT_OUTPUT   = 0x2,
58     WINED3D_SM4_RT_IMMCONST = 0x4,
59 };
60
61 enum wined3d_sm4_immconst_type
62 {
63     WINED3D_SM4_IMMCONST_FLOAT  = 0x1,
64     WINED3D_SM4_IMMCONST_FLOAT4 = 0x2,
65 };
66
67 struct wined3d_sm4_data
68 {
69     const DWORD *end;
70 };
71
72 struct wined3d_sm4_opcode_info
73 {
74     enum wined3d_sm4_opcode opcode;
75     enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx;
76     UINT dst_count;
77     UINT src_count;
78 };
79
80 static const struct wined3d_sm4_opcode_info opcode_table[] =
81 {
82     {WINED3D_SM4_OP_ADD,    WINED3DSIH_ADD,         1,  2},
83     {WINED3D_SM4_OP_EXP,    WINED3DSIH_EXP,         1,  1},
84     {WINED3D_SM4_OP_MOV,    WINED3DSIH_MOV,         1,  1},
85     {WINED3D_SM4_OP_MUL,    WINED3DSIH_MUL,         1,  2},
86     {WINED3D_SM4_OP_RET,    WINED3DSIH_RET,         0,  0},
87     {WINED3D_SM4_OP_SINCOS, WINED3DSIH_SINCOS,      1,  2},
88 };
89
90 static const WINED3DSHADER_PARAM_REGISTER_TYPE register_type_table[] =
91 {
92     /* WINED3D_SM4_RT_TEMP */       WINED3DSPR_TEMP,
93     /* WINED3D_SM4_RT_INPUT */      WINED3DSPR_INPUT,
94     /* WINED3D_SM4_RT_OUTPUT */     WINED3DSPR_OUTPUT,
95     /* UNKNOWN */                   0,
96     /* WINED3D_SM4_RT_IMMCONST */   WINED3DSPR_IMMCONST,
97 };
98
99 static const struct wined3d_sm4_opcode_info *get_opcode_info(enum wined3d_sm4_opcode opcode)
100 {
101     unsigned int i;
102
103     for (i = 0; i < sizeof(opcode_table) / sizeof(*opcode_table); ++i)
104     {
105         if (opcode == opcode_table[i].opcode) return &opcode_table[i];
106     }
107
108     return NULL;
109 }
110
111 static void *shader_sm4_init(const DWORD *byte_code)
112 {
113     struct wined3d_sm4_data *priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv));
114     if (!priv)
115     {
116         ERR("Failed to allocate private data\n");
117         return NULL;
118     }
119
120     return priv;
121 }
122
123 static void shader_sm4_free(void *data)
124 {
125     HeapFree(GetProcessHeap(), 0, data);
126 }
127
128 static void shader_sm4_read_header(void *data, const DWORD **ptr, DWORD *shader_version)
129 {
130     struct wined3d_sm4_data *priv = data;
131     priv->end = *ptr;
132
133     TRACE("version: 0x%08x\n", **ptr);
134     *shader_version = *(*ptr)++;
135     TRACE("token count: %u\n", **ptr);
136     priv->end += *(*ptr)++;
137 }
138
139 static void shader_sm4_read_opcode(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins,
140         UINT *param_size, DWORD shader_version)
141 {
142     const struct wined3d_sm4_opcode_info *opcode_info;
143     DWORD token = *(*ptr)++;
144     DWORD opcode = token & WINED3D_SM4_OPCODE_MASK;
145
146     *param_size = ((token & WINED3D_SM4_INSTRUCTION_LENGTH_MASK) >> WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT) - 1;
147
148     opcode_info = get_opcode_info(opcode);
149     if (!opcode_info)
150     {
151         FIXME("Unrecognized opcode %#x, token 0x%08x\n", opcode, token);
152         ins->handler_idx = WINED3DSIH_TABLE_SIZE;
153         return;
154     }
155
156     ins->handler_idx = opcode_info->handler_idx;
157     ins->flags = 0;
158     ins->coissue = 0;
159     ins->predicate = 0;
160     ins->dst_count = opcode_info->dst_count;
161     ins->src_count = opcode_info->src_count;
162 }
163
164 static void shader_sm4_read_src_param(const DWORD **ptr, struct wined3d_shader_src_param *src_param,
165         struct wined3d_shader_src_param *src_rel_addr, DWORD shader_version)
166 {
167     DWORD token = *(*ptr)++;
168     enum wined3d_sm4_register_type register_type;
169
170     register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
171     if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
172     {
173         FIXME("Unhandled register type %#x\n", register_type);
174         src_param->register_type = WINED3DSPR_TEMP;
175     }
176     else
177     {
178         src_param->register_type = register_type_table[register_type];
179     }
180
181     if (register_type == WINED3D_SM4_RT_IMMCONST)
182     {
183         enum wined3d_sm4_immconst_type immconst_type =
184                 (token & WINED3D_SM4_IMMCONST_TYPE_MASK) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT;
185         src_param->swizzle = WINED3DSP_NOSWIZZLE;
186
187         switch(immconst_type)
188         {
189             case WINED3D_SM4_IMMCONST_FLOAT:
190                 src_param->immconst_type = WINED3D_IMMCONST_FLOAT;
191                 memcpy(src_param->immconst_data, *ptr, 1 * sizeof(DWORD));
192                 *ptr += 1;
193                 break;
194
195             case WINED3D_SM4_IMMCONST_FLOAT4:
196                 src_param->immconst_type = WINED3D_IMMCONST_FLOAT4;
197                 memcpy(src_param->immconst_data, *ptr, 4 * sizeof(DWORD));
198                 *ptr += 4;
199                 break;
200
201             default:
202                 FIXME("Unhandled immediate constant type %#x\n", immconst_type);
203                 break;
204         }
205     }
206     else
207     {
208         src_param->register_idx = *(*ptr)++;
209         src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
210     }
211
212     src_param->modifiers = 0;
213     src_param->rel_addr = NULL;
214 }
215
216 static void shader_sm4_read_dst_param(const DWORD **ptr, struct wined3d_shader_dst_param *dst_param,
217         struct wined3d_shader_src_param *dst_rel_addr, DWORD shader_version)
218 {
219     DWORD token = *(*ptr)++;
220     UINT register_idx = *(*ptr)++;
221     enum wined3d_sm4_register_type register_type;
222
223     register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
224     if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
225     {
226         FIXME("Unhandled register type %#x\n", register_type);
227         dst_param->register_type = WINED3DSPR_TEMP;
228     }
229     else
230     {
231         dst_param->register_type = register_type_table[register_type];
232     }
233
234     dst_param->register_idx = register_idx;
235     dst_param->write_mask = (token & WINED3D_SM4_WRITEMASK_MASK) >> WINED3D_SM4_WRITEMASK_SHIFT;
236     dst_param->modifiers = 0;
237     dst_param->shift = 0;
238     dst_param->rel_addr = NULL;
239 }
240
241 static void shader_sm4_read_semantic(const DWORD **ptr, struct wined3d_shader_semantic *semantic)
242 {
243     FIXME("ptr %p, semantic %p stub!\n", ptr, semantic);
244 }
245
246 static void shader_sm4_read_comment(const DWORD **ptr, const char **comment)
247 {
248     FIXME("ptr %p, comment %p stub!\n", ptr, comment);
249     *comment = NULL;
250 }
251
252 static BOOL shader_sm4_is_end(void *data, const DWORD **ptr)
253 {
254     struct wined3d_sm4_data *priv = data;
255     return *ptr == priv->end;
256 }
257
258 const struct wined3d_shader_frontend sm4_shader_frontend =
259 {
260     shader_sm4_init,
261     shader_sm4_free,
262     shader_sm4_read_header,
263     shader_sm4_read_opcode,
264     shader_sm4_read_src_param,
265     shader_sm4_read_dst_param,
266     shader_sm4_read_semantic,
267     shader_sm4_read_comment,
268     shader_sm4_is_end,
269 };