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