wined3d: Add an initial shader_sm4_read_src_param() implementation.
[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_register_type
44 {
45     WINED3D_SM4_RT_TEMP     = 0x0,
46     WINED3D_SM4_RT_INPUT    = 0x1,
47     WINED3D_SM4_RT_OUTPUT   = 0x2,
48     WINED3D_SM4_RT_IMMCONST = 0x4,
49 };
50
51 enum wined3d_sm4_immconst_type
52 {
53     WINED3D_SM4_IMMCONST_FLOAT  = 0x1,
54     WINED3D_SM4_IMMCONST_FLOAT4 = 0x2,
55 };
56
57 struct wined3d_sm4_data
58 {
59     const DWORD *end;
60 };
61
62 static const WINED3DSHADER_PARAM_REGISTER_TYPE register_type_table[] =
63 {
64     /* WINED3D_SM4_RT_TEMP */       WINED3DSPR_TEMP,
65     /* WINED3D_SM4_RT_INPUT */      WINED3DSPR_INPUT,
66     /* WINED3D_SM4_RT_OUTPUT */     WINED3DSPR_OUTPUT,
67     /* UNKNOWN */                   0,
68     /* WINED3D_SM4_RT_IMMCONST */   WINED3DSPR_IMMCONST,
69 };
70
71 static void *shader_sm4_init(const DWORD *byte_code)
72 {
73     struct wined3d_sm4_data *priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv));
74     if (!priv)
75     {
76         ERR("Failed to allocate private data\n");
77         return NULL;
78     }
79
80     return priv;
81 }
82
83 static void shader_sm4_free(void *data)
84 {
85     HeapFree(GetProcessHeap(), 0, data);
86 }
87
88 static void shader_sm4_read_header(void *data, const DWORD **ptr, DWORD *shader_version)
89 {
90     struct wined3d_sm4_data *priv = data;
91     priv->end = *ptr;
92
93     TRACE("version: 0x%08x\n", **ptr);
94     *shader_version = *(*ptr)++;
95     TRACE("token count: %u\n", **ptr);
96     priv->end += *(*ptr)++;
97 }
98
99 static void shader_sm4_read_opcode(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins,
100         UINT *param_size, DWORD shader_version)
101 {
102     DWORD token = *(*ptr)++;
103     DWORD opcode = token & WINED3D_SM4_OPCODE_MASK;
104
105     FIXME("Unrecognized opcode %#x, token 0x%08x\n", opcode, token);
106
107     ins->handler_idx = WINED3DSIH_TABLE_SIZE;
108     ins->flags = 0;
109     ins->coissue = 0;
110     ins->predicate = 0;
111     ins->dst_count = 0;
112     ins->src_count = 0;
113     *param_size = ((token & WINED3D_SM4_INSTRUCTION_LENGTH_MASK) >> WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT) - 1;
114 }
115
116 static void shader_sm4_read_src_param(const DWORD **ptr, struct wined3d_shader_src_param *src_param,
117         struct wined3d_shader_src_param *src_rel_addr, DWORD shader_version)
118 {
119     DWORD token = *(*ptr)++;
120     enum wined3d_sm4_register_type register_type;
121
122     register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
123     if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
124     {
125         FIXME("Unhandled register type %#x\n", register_type);
126         src_param->register_type = WINED3DSPR_TEMP;
127     }
128     else
129     {
130         src_param->register_type = register_type_table[register_type];
131     }
132
133     if (register_type == WINED3D_SM4_RT_IMMCONST)
134     {
135         enum wined3d_sm4_immconst_type immconst_type =
136                 (token & WINED3D_SM4_IMMCONST_TYPE_MASK) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT;
137         src_param->swizzle = WINED3DSP_NOSWIZZLE;
138
139         switch(immconst_type)
140         {
141             case WINED3D_SM4_IMMCONST_FLOAT:
142                 src_param->immconst_type = WINED3D_IMMCONST_FLOAT;
143                 memcpy(src_param->immconst_data, *ptr, 1 * sizeof(DWORD));
144                 *ptr += 1;
145                 break;
146
147             case WINED3D_SM4_IMMCONST_FLOAT4:
148                 src_param->immconst_type = WINED3D_IMMCONST_FLOAT4;
149                 memcpy(src_param->immconst_data, *ptr, 4 * sizeof(DWORD));
150                 *ptr += 4;
151                 break;
152
153             default:
154                 FIXME("Unhandled immediate constant type %#x\n", immconst_type);
155                 break;
156         }
157     }
158     else
159     {
160         src_param->register_idx = *(*ptr)++;
161         src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
162     }
163
164     src_param->modifiers = 0;
165     src_param->rel_addr = NULL;
166 }
167
168 static void shader_sm4_read_dst_param(const DWORD **ptr, struct wined3d_shader_dst_param *dst_param,
169         struct wined3d_shader_src_param *dst_rel_addr, DWORD shader_version)
170 {
171     DWORD token = *(*ptr)++;
172     UINT register_idx = *(*ptr)++;
173     enum wined3d_sm4_register_type register_type;
174
175     register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
176     if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
177     {
178         FIXME("Unhandled register type %#x\n", register_type);
179         dst_param->register_type = WINED3DSPR_TEMP;
180     }
181     else
182     {
183         dst_param->register_type = register_type_table[register_type];
184     }
185
186     dst_param->register_idx = register_idx;
187     dst_param->write_mask = (token & WINED3D_SM4_WRITEMASK_MASK) >> WINED3D_SM4_WRITEMASK_SHIFT;
188     dst_param->modifiers = 0;
189     dst_param->shift = 0;
190     dst_param->rel_addr = NULL;
191 }
192
193 static void shader_sm4_read_semantic(const DWORD **ptr, struct wined3d_shader_semantic *semantic)
194 {
195     FIXME("ptr %p, semantic %p stub!\n", ptr, semantic);
196 }
197
198 static void shader_sm4_read_comment(const DWORD **ptr, const char **comment)
199 {
200     FIXME("ptr %p, comment %p stub!\n", ptr, comment);
201     *comment = NULL;
202 }
203
204 static BOOL shader_sm4_is_end(void *data, const DWORD **ptr)
205 {
206     struct wined3d_sm4_data *priv = data;
207     return *ptr == priv->end;
208 }
209
210 const struct wined3d_shader_frontend sm4_shader_frontend =
211 {
212     shader_sm4_init,
213     shader_sm4_free,
214     shader_sm4_read_header,
215     shader_sm4_read_opcode,
216     shader_sm4_read_src_param,
217     shader_sm4_read_dst_param,
218     shader_sm4_read_semantic,
219     shader_sm4_read_comment,
220     shader_sm4_is_end,
221 };