VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/shaderlib/shader_sm4.c@ 82718

Last change on this file since 82718 was 82718, checked in by vboxsync, 5 years ago

3D: Check last shader bytecode token, bugref:9613

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette