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