VirtualBox

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

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

3D: Corrected r135648 to fix blank guest screen, bugref:9613

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