VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/shader_sm4.c@ 40588

Last change on this file since 40588 was 33656, checked in by vboxsync, 14 years ago

*: rebrand Sun (L)GPL disclaimers

  • Property svn:eol-style set to native
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, 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->output_signature = output_signature;
241
242 return priv;
243}
244
245static void shader_sm4_free(void *data)
246{
247 HeapFree(GetProcessHeap(), 0, data);
248}
249
250static void shader_sm4_read_header(void *data, const DWORD **ptr, struct wined3d_shader_version *shader_version)
251{
252 struct wined3d_sm4_data *priv = data;
253 DWORD version_token;
254
255 priv->end = *ptr;
256
257 version_token = *(*ptr)++;
258 TRACE("version: 0x%08x\n", version_token);
259
260 TRACE("token count: %u\n", **ptr);
261 priv->end += *(*ptr)++;
262
263 switch (version_token >> 16)
264 {
265 case WINED3D_SM4_PS:
266 priv->shader_version.type = WINED3D_SHADER_TYPE_PIXEL;
267 break;
268
269 case WINED3D_SM4_VS:
270 priv->shader_version.type = WINED3D_SHADER_TYPE_VERTEX;
271 break;
272
273 case WINED3D_SM4_GS:
274 priv->shader_version.type = WINED3D_SHADER_TYPE_GEOMETRY;
275 break;
276
277 default:
278 FIXME("Unrecognized shader type %#x\n", version_token >> 16);
279 }
280 priv->shader_version.major = WINED3D_SM4_VERSION_MAJOR(version_token);
281 priv->shader_version.minor = WINED3D_SM4_VERSION_MINOR(version_token);
282
283 *shader_version = priv->shader_version;
284}
285
286static void shader_sm4_read_opcode(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins,
287 UINT *param_size)
288{
289 const struct wined3d_sm4_opcode_info *opcode_info;
290 DWORD token = *(*ptr)++;
291 DWORD opcode = token & WINED3D_SM4_OPCODE_MASK;
292
293 *param_size = ((token & WINED3D_SM4_INSTRUCTION_LENGTH_MASK) >> WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT) - 1;
294
295 opcode_info = get_opcode_info(opcode);
296 if (!opcode_info)
297 {
298 FIXME("Unrecognized opcode %#x, token 0x%08x\n", opcode, token);
299 ins->handler_idx = WINED3DSIH_TABLE_SIZE;
300 return;
301 }
302
303 ins->handler_idx = opcode_info->handler_idx;
304 ins->flags = 0;
305 ins->coissue = 0;
306 ins->predicate = 0;
307 ins->dst_count = opcode_info->dst_count;
308 ins->src_count = opcode_info->src_count;
309}
310
311static void shader_sm4_read_src_param(void *data, const DWORD **ptr, struct wined3d_shader_src_param *src_param,
312 struct wined3d_shader_src_param *src_rel_addr)
313{
314 struct wined3d_sm4_data *priv = data;
315 DWORD token = *(*ptr)++;
316 enum wined3d_sm4_register_type register_type;
317 DWORD order;
318
319 register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
320 if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
321 {
322 FIXME("Unhandled register type %#x\n", register_type);
323 src_param->reg.type = WINED3DSPR_TEMP;
324 }
325 else
326 {
327 src_param->reg.type = register_type_table[register_type];
328 }
329
330 order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
331
332 if (order < 1) src_param->reg.idx = ~0U;
333 else src_param->reg.idx = *(*ptr)++;
334
335 if (order < 2) src_param->reg.array_idx = ~0U;
336 else src_param->reg.array_idx = *(*ptr)++;
337
338 if (order > 2) FIXME("Unhandled order %u.\n", order);
339
340 if (register_type == WINED3D_SM4_RT_IMMCONST)
341 {
342 enum wined3d_sm4_immconst_type immconst_type =
343 (token & WINED3D_SM4_IMMCONST_TYPE_MASK) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT;
344 src_param->swizzle = WINED3DSP_NOSWIZZLE;
345
346 switch(immconst_type)
347 {
348 case WINED3D_SM4_IMMCONST_FLOAT:
349 src_param->reg.immconst_type = WINED3D_IMMCONST_FLOAT;
350 memcpy(src_param->reg.immconst_data, *ptr, 1 * sizeof(DWORD));
351 *ptr += 1;
352 break;
353
354 case WINED3D_SM4_IMMCONST_FLOAT4:
355 src_param->reg.immconst_type = WINED3D_IMMCONST_FLOAT4;
356 memcpy(src_param->reg.immconst_data, *ptr, 4 * sizeof(DWORD));
357 *ptr += 4;
358 break;
359
360 default:
361 FIXME("Unhandled immediate constant type %#x\n", immconst_type);
362 break;
363 }
364 }
365 else
366 {
367 src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
368 }
369
370 src_param->modifiers = 0;
371 src_param->reg.rel_addr = NULL;
372
373 map_register(priv, &src_param->reg);
374}
375
376static void shader_sm4_read_dst_param(void *data, const DWORD **ptr, struct wined3d_shader_dst_param *dst_param,
377 struct wined3d_shader_src_param *dst_rel_addr)
378{
379 struct wined3d_sm4_data *priv = data;
380 DWORD token = *(*ptr)++;
381 enum wined3d_sm4_register_type register_type;
382 DWORD order;
383
384 register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
385 if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
386 {
387 FIXME("Unhandled register type %#x\n", register_type);
388 dst_param->reg.type = WINED3DSPR_TEMP;
389 }
390 else
391 {
392 dst_param->reg.type = register_type_table[register_type];
393 }
394
395 order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
396
397 if (order < 1) dst_param->reg.idx = ~0U;
398 else dst_param->reg.idx = *(*ptr)++;
399
400 if (order < 2) dst_param->reg.array_idx = ~0U;
401 else dst_param->reg.array_idx = *(*ptr)++;
402
403 if (order > 2) FIXME("Unhandled order %u.\n", order);
404
405 dst_param->write_mask = (token & WINED3D_SM4_WRITEMASK_MASK) >> WINED3D_SM4_WRITEMASK_SHIFT;
406 dst_param->modifiers = 0;
407 dst_param->shift = 0;
408 dst_param->reg.rel_addr = NULL;
409
410 map_register(priv, &dst_param->reg);
411}
412
413static void shader_sm4_read_semantic(const DWORD **ptr, struct wined3d_shader_semantic *semantic)
414{
415 FIXME("ptr %p, semantic %p stub!\n", ptr, semantic);
416}
417
418static void shader_sm4_read_comment(const DWORD **ptr, const char **comment, UINT *comment_size)
419{
420 FIXME("ptr %p, comment %p, comment_size %p stub!\n", ptr, comment, comment_size);
421 *comment = NULL;
422}
423
424static BOOL shader_sm4_is_end(void *data, const DWORD **ptr)
425{
426 struct wined3d_sm4_data *priv = data;
427 return *ptr == priv->end;
428}
429
430const struct wined3d_shader_frontend sm4_shader_frontend =
431{
432 shader_sm4_init,
433 shader_sm4_free,
434 shader_sm4_read_header,
435 shader_sm4_read_opcode,
436 shader_sm4_read_src_param,
437 shader_sm4_read_dst_param,
438 shader_sm4_read_semantic,
439 shader_sm4_read_comment,
440 shader_sm4_is_end,
441};
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