VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/shaderlib/shader.c@ 79670

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

VMSVGA: Adding some more validation of a shader bytecode, bugref:9460

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 87.2 KB
Line 
1/*
2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2005 Oliver Stieber
6 * Copyright 2006 Ivan Gyurdiev
7 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2009 Henri Verbeet for CodeWeavers
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25/*
26 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
27 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
28 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
29 * a choice of LGPL license versions is made available with the language indicating
30 * that LGPLv2 or any later version may be used, or where a choice of which version
31 * of the LGPL is applied is otherwise unspecified.
32 */
33
34#include "config.h"
35#include "wine/port.h"
36
37#include <math.h>
38#include <stdio.h>
39#include <string.h>
40
41#include "wined3d_private.h"
42
43WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
44WINE_DECLARE_DEBUG_CHANNEL(d3d);
45
46static const char *shader_opcode_names[] =
47{
48 /* WINED3DSIH_ABS */ "abs",
49 /* WINED3DSIH_ADD */ "add",
50 /* WINED3DSIH_BEM */ "bem",
51 /* WINED3DSIH_BREAK */ "break",
52 /* WINED3DSIH_BREAKC */ "breakc",
53 /* WINED3DSIH_BREAKP */ "breakp",
54 /* WINED3DSIH_CALL */ "call",
55 /* WINED3DSIH_CALLNZ */ "callnz",
56 /* WINED3DSIH_CMP */ "cmp",
57 /* WINED3DSIH_CND */ "cnd",
58 /* WINED3DSIH_CRS */ "crs",
59 /* WINED3DSIH_CUT */ "cut",
60 /* WINED3DSIH_DCL */ "dcl",
61 /* WINED3DSIH_DEF */ "def",
62 /* WINED3DSIH_DEFB */ "defb",
63 /* WINED3DSIH_DEFI */ "defi",
64 /* WINED3DSIH_DP2ADD */ "dp2add",
65 /* WINED3DSIH_DP3 */ "dp3",
66 /* WINED3DSIH_DP4 */ "dp4",
67 /* WINED3DSIH_DST */ "dst",
68 /* WINED3DSIH_DSX */ "dsx",
69 /* WINED3DSIH_DSY */ "dsy",
70 /* WINED3DSIH_ELSE */ "else",
71 /* WINED3DSIH_EMIT */ "emit",
72 /* WINED3DSIH_ENDIF */ "endif",
73 /* WINED3DSIH_ENDLOOP */ "endloop",
74 /* WINED3DSIH_ENDREP */ "endrep",
75 /* WINED3DSIH_EXP */ "exp",
76 /* WINED3DSIH_EXPP */ "expp",
77 /* WINED3DSIH_FRC */ "frc",
78 /* WINED3DSIH_IADD */ "iadd",
79 /* WINED3DSIH_IF */ "if",
80 /* WINED3DSIH_IFC */ "ifc",
81 /* WINED3DSIH_IGE */ "ige",
82 /* WINED3DSIH_LABEL */ "label",
83 /* WINED3DSIH_LIT */ "lit",
84 /* WINED3DSIH_LOG */ "log",
85 /* WINED3DSIH_LOGP */ "logp",
86 /* WINED3DSIH_LOOP */ "loop",
87 /* WINED3DSIH_LRP */ "lrp",
88 /* WINED3DSIH_LT */ "lt",
89 /* WINED3DSIH_M3x2 */ "m3x2",
90 /* WINED3DSIH_M3x3 */ "m3x3",
91 /* WINED3DSIH_M3x4 */ "m3x4",
92 /* WINED3DSIH_M4x3 */ "m4x3",
93 /* WINED3DSIH_M4x4 */ "m4x4",
94 /* WINED3DSIH_MAD */ "mad",
95 /* WINED3DSIH_MAX */ "max",
96 /* WINED3DSIH_MIN */ "min",
97 /* WINED3DSIH_MOV */ "mov",
98 /* WINED3DSIH_MOVA */ "mova",
99 /* WINED3DSIH_MUL */ "mul",
100 /* WINED3DSIH_NOP */ "nop",
101 /* WINED3DSIH_NRM */ "nrm",
102 /* WINED3DSIH_PHASE */ "phase",
103 /* WINED3DSIH_POW */ "pow",
104 /* WINED3DSIH_RCP */ "rcp",
105 /* WINED3DSIH_REP */ "rep",
106 /* WINED3DSIH_RET */ "ret",
107 /* WINED3DSIH_RSQ */ "rsq",
108 /* WINED3DSIH_SETP */ "setp",
109 /* WINED3DSIH_SGE */ "sge",
110 /* WINED3DSIH_SGN */ "sgn",
111 /* WINED3DSIH_SINCOS */ "sincos",
112 /* WINED3DSIH_SLT */ "slt",
113 /* WINED3DSIH_SUB */ "sub",
114 /* WINED3DSIH_TEX */ "texld",
115 /* WINED3DSIH_TEXBEM */ "texbem",
116 /* WINED3DSIH_TEXBEML */ "texbeml",
117 /* WINED3DSIH_TEXCOORD */ "texcrd",
118 /* WINED3DSIH_TEXDEPTH */ "texdepth",
119 /* WINED3DSIH_TEXDP3 */ "texdp3",
120 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
121 /* WINED3DSIH_TEXKILL */ "texkill",
122 /* WINED3DSIH_TEXLDD */ "texldd",
123 /* WINED3DSIH_TEXLDL */ "texldl",
124 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
125 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
126 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
127 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
128 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
129 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
130 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
131 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
132 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
133 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
134 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
135 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
136};
137
138static const char *semantic_names[] =
139{
140 /* WINED3DDECLUSAGE_POSITION */ "SV_POSITION",
141 /* WINED3DDECLUSAGE_BLENDWEIGHT */ "BLENDWEIGHT",
142 /* WINED3DDECLUSAGE_BLENDINDICES */ "BLENDINDICES",
143 /* WINED3DDECLUSAGE_NORMAL */ "NORMAL",
144 /* WINED3DDECLUSAGE_PSIZE */ "PSIZE",
145 /* WINED3DDECLUSAGE_TEXCOORD */ "TEXCOORD",
146 /* WINED3DDECLUSAGE_TANGENT */ "TANGENT",
147 /* WINED3DDECLUSAGE_BINORMAL */ "BINORMAL",
148 /* WINED3DDECLUSAGE_TESSFACTOR */ "TESSFACTOR",
149 /* WINED3DDECLUSAGE_POSITIONT */ "POSITIONT",
150 /* WINED3DDECLUSAGE_COLOR */ "COLOR",
151 /* WINED3DDECLUSAGE_FOG */ "FOG",
152 /* WINED3DDECLUSAGE_DEPTH */ "DEPTH",
153 /* WINED3DDECLUSAGE_SAMPLE */ "SAMPLE",
154};
155
156static const char *shader_semantic_name_from_usage(WINED3DDECLUSAGE usage)
157{
158 if (usage >= sizeof(semantic_names) / sizeof(*semantic_names))
159 {
160 FIXME("Unrecognized usage %#x.\n", usage);
161 return "UNRECOGNIZED";
162 }
163
164 return semantic_names[usage];
165}
166
167static WINED3DDECLUSAGE shader_usage_from_semantic_name(const char *name)
168{
169 unsigned int i;
170
171 for (i = 0; i < sizeof(semantic_names) / sizeof(*semantic_names); ++i)
172 {
173 if (!strcmp(name, semantic_names[i])) return i;
174 }
175
176 return ~0U;
177}
178
179BOOL shader_match_semantic(const char *semantic_name, WINED3DDECLUSAGE usage)
180{
181 return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
182}
183
184static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
185 const struct wined3d_shader_semantic *s)
186{
187 e->semantic_name = shader_semantic_name_from_usage(s->usage);
188 e->semantic_idx = s->usage_idx;
189 e->sysval_semantic = 0;
190 e->component_type = 0;
191 e->register_idx = s->reg.reg.idx;
192 e->mask = s->reg.write_mask;
193}
194
195static const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token)
196{
197 switch (version_token >> 16)
198 {
199 case WINED3D_SM1_VS:
200 case WINED3D_SM1_PS:
201 return &sm1_shader_frontend;
202
203 case WINED3D_SM4_PS:
204 case WINED3D_SM4_VS:
205 case WINED3D_SM4_GS:
206 return &sm4_shader_frontend;
207
208 default:
209 FIXME("Unrecognised version token %#x\n", version_token);
210 return NULL;
211 }
212}
213
214void shader_buffer_clear(struct wined3d_shader_buffer *buffer)
215{
216 buffer->buffer[0] = '\0';
217 buffer->bsize = 0;
218 buffer->lineNo = 0;
219 buffer->newline = TRUE;
220}
221
222BOOL shader_buffer_init(struct wined3d_shader_buffer *buffer)
223{
224 buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
225 if (!buffer->buffer)
226 {
227 ERR("Failed to allocate shader buffer memory.\n");
228 return FALSE;
229 }
230
231 shader_buffer_clear(buffer);
232 return TRUE;
233}
234
235void shader_buffer_free(struct wined3d_shader_buffer *buffer)
236{
237 HeapFree(GetProcessHeap(), 0, buffer->buffer);
238}
239
240int shader_vaddline(struct wined3d_shader_buffer *buffer, const char *format, va_list args)
241{
242 char *base = buffer->buffer + buffer->bsize;
243 int rc;
244
245 rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
246
247 if (rc < 0 /* C89 */ || (unsigned int)rc > SHADER_PGMSIZE - 1 - buffer->bsize /* C99 */)
248 {
249 ERR("The buffer allocated for the shader program string "
250 "is too small at %d bytes.\n", SHADER_PGMSIZE);
251 buffer->bsize = SHADER_PGMSIZE - 1;
252 return -1;
253 }
254
255 if (buffer->newline)
256 {
257 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
258 buffer->newline = FALSE;
259 }
260 else
261 {
262 TRACE("%s", base);
263 }
264
265 buffer->bsize += rc;
266 if (buffer->buffer[buffer->bsize-1] == '\n')
267 {
268 ++buffer->lineNo;
269 buffer->newline = TRUE;
270 }
271
272 return 0;
273}
274
275int shader_addline(struct wined3d_shader_buffer *buffer, const char *format, ...)
276{
277 va_list args;
278 int ret;
279
280 va_start(args, format);
281 ret = shader_vaddline(buffer, format, args);
282 va_end(args);
283
284 return ret;
285}
286
287static void shader_init(struct IWineD3DBaseShaderClass *shader, IWineD3DDeviceImpl *device,
288 IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
289{
290 shader->ref = 1;
291 shader->device = (IWineD3DDevice *)device;
292 shader->parent = parent;
293 shader->parent_ops = parent_ops;
294 list_init(&shader->linked_programs);
295 list_add_head(&device->shaders, &shader->shader_list_entry);
296}
297
298/* Convert floating point offset relative to a register file to an absolute
299 * offset for float constants. */
300static unsigned int shader_get_float_offset(WINED3DSHADER_PARAM_REGISTER_TYPE register_type, UINT register_idx)
301{
302 switch (register_type)
303 {
304 case WINED3DSPR_CONST: return register_idx;
305 case WINED3DSPR_CONST2: return 2048 + register_idx;
306 case WINED3DSPR_CONST3: return 4096 + register_idx;
307 case WINED3DSPR_CONST4: return 6144 + register_idx;
308 default:
309 FIXME("Unsupported register type: %u.\n", register_type);
310 return register_idx;
311 }
312}
313
314static void shader_delete_constant_list(struct list *clist)
315{
316 struct local_constant *constant;
317 struct list *ptr;
318
319 ptr = list_head(clist);
320 while (ptr)
321 {
322 constant = LIST_ENTRY(ptr, struct local_constant, entry);
323 ptr = list_next(clist, ptr);
324 HeapFree(GetProcessHeap(), 0, constant);
325 }
326 list_init(clist);
327}
328
329static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
330{
331 DWORD idx, shift;
332 idx = bit >> 5;
333 shift = bit & 0x1f;
334 bitmap[idx] |= (1 << shift);
335}
336
337static void shader_record_register_usage(IWineD3DBaseShaderImpl *shader, struct shader_reg_maps *reg_maps,
338 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type)
339{
340 switch (reg->type)
341 {
342 case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
343 if (shader_type == WINED3D_SHADER_TYPE_PIXEL) reg_maps->texcoord |= 1 << reg->idx;
344 else reg_maps->address |= 1 << reg->idx;
345 break;
346
347 case WINED3DSPR_TEMP:
348 reg_maps->temporary |= 1 << reg->idx;
349 break;
350
351 case WINED3DSPR_INPUT:
352 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
353 {
354 if (reg->rel_addr)
355 {
356 /* If relative addressing is used, we must assume that all registers
357 * are used. Even if it is a construct like v3[aL], we can't assume
358 * that v0, v1 and v2 aren't read because aL can be negative */
359 unsigned int i;
360 for (i = 0; i < MAX_REG_INPUT; ++i)
361 {
362 ((IWineD3DPixelShaderImpl *)shader)->input_reg_used[i] = TRUE;
363 }
364 }
365 else
366 {
367 ((IWineD3DPixelShaderImpl *)shader)->input_reg_used[reg->idx] = TRUE;
368 }
369 }
370 else reg_maps->input_registers |= 1 << reg->idx;
371 break;
372
373 case WINED3DSPR_RASTOUT:
374 if (reg->idx == 1) reg_maps->fog = 1;
375 break;
376
377 case WINED3DSPR_MISCTYPE:
378 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
379 {
380 if (reg->idx == 0) reg_maps->vpos = 1;
381 else if (reg->idx == 1) reg_maps->usesfacing = 1;
382 }
383 break;
384
385 case WINED3DSPR_CONST:
386 if (reg->rel_addr)
387 {
388 if (shader_type != WINED3D_SHADER_TYPE_PIXEL)
389 {
390 if (reg->idx < ((IWineD3DVertexShaderImpl *)shader)->min_rel_offset)
391 {
392 ((IWineD3DVertexShaderImpl *)shader)->min_rel_offset = reg->idx;
393 }
394 if (reg->idx > ((IWineD3DVertexShaderImpl *)shader)->max_rel_offset)
395 {
396 ((IWineD3DVertexShaderImpl *)shader)->max_rel_offset = reg->idx;
397 }
398 }
399 reg_maps->usesrelconstF = TRUE;
400 }
401 else
402 {
403 set_bitmap_bit(reg_maps->constf, reg->idx);
404 }
405 break;
406
407 case WINED3DSPR_CONSTINT:
408 reg_maps->integer_constants |= (1 << reg->idx);
409 break;
410
411 case WINED3DSPR_CONSTBOOL:
412 reg_maps->boolean_constants |= (1 << reg->idx);
413 break;
414
415 case WINED3DSPR_COLOROUT:
416 reg_maps->highest_render_target = max(reg_maps->highest_render_target, reg->idx);
417 break;
418
419 default:
420 TRACE("Not recording register of type %#x and idx %u\n", reg->type, reg->idx);
421 break;
422 }
423}
424
425static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
426{
427 switch (instr)
428 {
429 case WINED3DSIH_M4x4:
430 case WINED3DSIH_M3x4:
431 return param == 1 ? 3 : 0;
432
433 case WINED3DSIH_M4x3:
434 case WINED3DSIH_M3x3:
435 return param == 1 ? 2 : 0;
436
437 case WINED3DSIH_M3x2:
438 return param == 1 ? 1 : 0;
439
440 default:
441 return 0;
442 }
443}
444
445/* Note that this does not count the loop register as an address register. */
446static HRESULT shader_get_registers_used(IWineD3DBaseShader *iface, const struct wined3d_shader_frontend *fe,
447 struct shader_reg_maps *reg_maps, struct wined3d_shader_signature_element *input_signature,
448 struct wined3d_shader_signature_element *output_signature, const DWORD *byte_code, DWORD constf_size)
449{
450 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
451 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
452 void *fe_data = shader->baseShader.frontend_data;
453 struct wined3d_shader_version shader_version;
454 const DWORD *ptr = byte_code;
455
456 memset(reg_maps, 0, sizeof(*reg_maps));
457
458 /* get_registers_used() is called on every compile on some 1.x shaders,
459 * which can result in stacking up a collection of local constants.
460 * Delete the old constants if existing. */
461 shader_delete_constant_list(&shader->baseShader.constantsF);
462 shader_delete_constant_list(&shader->baseShader.constantsB);
463 shader_delete_constant_list(&shader->baseShader.constantsI);
464
465 fe->shader_read_header(fe_data, &ptr, &shader_version);
466 reg_maps->shader_version = shader_version;
467
468 reg_maps->constf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
469 sizeof(*reg_maps->constf) * ((constf_size + 31) / 32));
470 if (!reg_maps->constf)
471 {
472 ERR("Failed to allocate constant map memory.\n");
473 return E_OUTOFMEMORY;
474 }
475
476 while (!fe->shader_is_end(fe_data, &ptr))
477 {
478 struct wined3d_shader_instruction ins;
479 const char *comment;
480 UINT comment_size;
481 UINT param_size;
482
483 /* Skip comments. */
484 fe->shader_read_comment(&ptr, &comment, &comment_size);
485 if (comment) continue;
486
487 /* Fetch opcode. */
488 fe->shader_read_opcode(fe_data, &ptr, &ins, &param_size);
489
490 /* Unhandled opcode, and its parameters. */
491 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
492 {
493 TRACE("Skipping unrecognized instruction.\n");
494 ptr += param_size;
495 continue;
496 }
497
498 /* Handle declarations. */
499 if (ins.handler_idx == WINED3DSIH_DCL)
500 {
501 struct wined3d_shader_semantic semantic;
502 unsigned int reg_idx;
503
504 fe->shader_read_semantic(&ptr, &semantic);
505 reg_idx = semantic.reg.reg.idx;
506
507 switch (semantic.reg.reg.type)
508 {
509 /* Mark input registers used. */
510 case WINED3DSPR_INPUT:
511 if (reg_idx >= MAX_REG_INPUT)
512 {
513 ERR("Invalid input register index %d.\n", reg_idx);
514 return E_INVALIDARG;
515 }
516 reg_maps->input_registers |= 1 << reg_idx;
517 shader_signature_from_semantic(&input_signature[reg_idx], &semantic);
518 break;
519
520 /* Vertex shader: mark 3.0 output registers used, save token. */
521 case WINED3DSPR_OUTPUT:
522 if (reg_idx >= MAX_REG_OUTPUT)
523 {
524 ERR("Invalid output register index %d.\n", reg_idx);
525 return E_INVALIDARG;
526 }
527 reg_maps->output_registers |= 1 << reg_idx;
528 shader_signature_from_semantic(&output_signature[reg_idx], &semantic);
529 if (semantic.usage == WINED3DDECLUSAGE_FOG) reg_maps->fog = 1;
530 break;
531
532 /* Save sampler usage token. */
533 case WINED3DSPR_SAMPLER:
534 if (reg_idx >= RT_ELEMENTS(reg_maps->sampler_type))
535 {
536 ERR("Invalid sampler index %d.\n", reg_idx);
537 return E_INVALIDARG;
538 }
539 reg_maps->sampler_type[reg_idx] = semantic.sampler_type;
540 break;
541
542 default:
543 TRACE("Not recording DCL register type %#x.\n", semantic.reg.reg.type);
544 break;
545 }
546 }
547 else if (ins.handler_idx == WINED3DSIH_DEF)
548 {
549 struct wined3d_shader_src_param rel_addr;
550 struct wined3d_shader_dst_param dst;
551
552 local_constant *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
553 if (!lconst) return E_OUTOFMEMORY;
554
555 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
556 lconst->idx = dst.reg.idx;
557
558 memcpy(lconst->value, ptr, 4 * sizeof(DWORD));
559 ptr += 4;
560
561 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
562 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
563 {
564 float *value = (float *)lconst->value;
565 if (value[0] < -1.0f) value[0] = -1.0f;
566 else if (value[0] > 1.0f) value[0] = 1.0f;
567 if (value[1] < -1.0f) value[1] = -1.0f;
568 else if (value[1] > 1.0f) value[1] = 1.0f;
569 if (value[2] < -1.0f) value[2] = -1.0f;
570 else if (value[2] > 1.0f) value[2] = 1.0f;
571 if (value[3] < -1.0f) value[3] = -1.0f;
572 else if (value[3] > 1.0f) value[3] = 1.0f;
573 }
574
575 list_add_head(&shader->baseShader.constantsF, &lconst->entry);
576 }
577 else if (ins.handler_idx == WINED3DSIH_DEFI)
578 {
579 struct wined3d_shader_src_param rel_addr;
580 struct wined3d_shader_dst_param dst;
581
582 local_constant *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
583 if (!lconst) return E_OUTOFMEMORY;
584
585 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
586 lconst->idx = dst.reg.idx;
587
588 memcpy(lconst->value, ptr, 4 * sizeof(DWORD));
589 ptr += 4;
590
591 list_add_head(&shader->baseShader.constantsI, &lconst->entry);
592 reg_maps->local_int_consts |= (1 << dst.reg.idx);
593 }
594 else if (ins.handler_idx == WINED3DSIH_DEFB)
595 {
596 struct wined3d_shader_src_param rel_addr;
597 struct wined3d_shader_dst_param dst;
598
599 local_constant *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
600 if (!lconst) return E_OUTOFMEMORY;
601
602 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
603 lconst->idx = dst.reg.idx;
604
605 memcpy(lconst->value, ptr, sizeof(DWORD));
606 ++ptr;
607
608 list_add_head(&shader->baseShader.constantsB, &lconst->entry);
609 reg_maps->local_bool_consts |= (1 << dst.reg.idx);
610 }
611 /* If there's a loop in the shader. */
612 else if (ins.handler_idx == WINED3DSIH_LOOP
613 || ins.handler_idx == WINED3DSIH_REP)
614 {
615 struct wined3d_shader_src_param src, rel_addr;
616
617 fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
618
619 /* Rep and Loop always use an integer constant for the control parameters. */
620 if (ins.handler_idx == WINED3DSIH_REP)
621 {
622 reg_maps->integer_constants |= 1 << src.reg.idx;
623 }
624 else
625 {
626 fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
627 reg_maps->integer_constants |= 1 << src.reg.idx;
628 }
629
630 cur_loop_depth++;
631 if (cur_loop_depth > max_loop_depth) max_loop_depth = cur_loop_depth;
632 }
633 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
634 || ins.handler_idx == WINED3DSIH_ENDREP)
635 {
636 cur_loop_depth--;
637 }
638 /* For subroutine prototypes. */
639 else if (ins.handler_idx == WINED3DSIH_LABEL)
640 {
641 struct wined3d_shader_src_param src, rel_addr;
642
643 fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
644 reg_maps->labels |= 1 << src.reg.idx;
645 }
646 /* Set texture, address, temporary registers. */
647 else
648 {
649 BOOL color0_mov = FALSE;
650 int i, limit;
651
652 /* This will loop over all the registers and try to
653 * make a bitmask of the ones we're interested in.
654 *
655 * Relative addressing tokens are ignored, but that's
656 * okay, since we'll catch any address registers when
657 * they are initialized (required by spec). */
658 if (ins.dst_count)
659 {
660 struct wined3d_shader_src_param dst_rel_addr;
661 struct wined3d_shader_dst_param dst_param;
662
663 fe->shader_read_dst_param(fe_data, &ptr, &dst_param, &dst_rel_addr);
664
665 shader_record_register_usage(shader, reg_maps, &dst_param.reg, shader_version.type);
666
667 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
668 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
669 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
670 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX && shader_version.major < 3
671 && dst_param.reg.type == WINED3DSPR_TEXCRDOUT)
672 {
673 unsigned int idx = dst_param.reg.idx;
674
675 if (idx >= MAX_REG_TEXCRD)
676 {
677 ERR("Invalid texcoord index %d.\n", idx);
678 return E_INVALIDARG;
679 }
680
681 reg_maps->texcoord_mask[idx] |= dst_param.write_mask;
682 }
683
684 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
685 {
686 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *)shader;
687
688 if (dst_param.reg.type == WINED3DSPR_COLOROUT && dst_param.reg.idx == 0)
689 {
690 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
691 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
692 * the mov and perform the sRGB write correction from the source register.
693 *
694 * However, if the mov is only partial, we can't do this, and if the write
695 * comes from an instruction other than MOV it is hard to do as well. If
696 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
697
698 ps->color0_mov = FALSE;
699 if (ins.handler_idx == WINED3DSIH_MOV)
700 {
701 /* Used later when the source register is read. */
702 color0_mov = TRUE;
703 }
704 }
705 /* Also drop the MOV marker if the source register is overwritten prior to the shader
706 * end
707 */
708 else if (dst_param.reg.type == WINED3DSPR_TEMP && dst_param.reg.idx == ps->color0_reg)
709 {
710 ps->color0_mov = FALSE;
711 }
712 }
713
714 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
715 if (shader_version.major == 1
716 && (ins.handler_idx == WINED3DSIH_TEX
717 || ins.handler_idx == WINED3DSIH_TEXBEM
718 || ins.handler_idx == WINED3DSIH_TEXBEML
719 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
720 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
721 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
722 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
723 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
724 || ins.handler_idx == WINED3DSIH_TEXREG2AR
725 || ins.handler_idx == WINED3DSIH_TEXREG2GB
726 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
727 {
728 /* Fake sampler usage, only set reserved bit and type. */
729 DWORD sampler_code = dst_param.reg.idx;
730
731 if (sampler_code >= RT_ELEMENTS(reg_maps->sampler_type))
732 {
733 ERR("Invalid sampler index %d.\n", sampler_code);
734 return E_INVALIDARG;
735 }
736
737 TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
738 reg_maps->sampler_type[sampler_code] = WINED3DSTT_2D;
739
740 /* texbem is only valid with < 1.4 pixel shaders */
741 if (ins.handler_idx == WINED3DSIH_TEXBEM
742 || ins.handler_idx == WINED3DSIH_TEXBEML)
743 {
744 reg_maps->bumpmat |= 1 << dst_param.reg.idx;
745 if (ins.handler_idx == WINED3DSIH_TEXBEML)
746 {
747 reg_maps->luminanceparams |= 1 << dst_param.reg.idx;
748 }
749 }
750 }
751 else if (ins.handler_idx == WINED3DSIH_BEM)
752 {
753 reg_maps->bumpmat |= 1 << dst_param.reg.idx;
754 }
755 }
756
757 if (ins.handler_idx == WINED3DSIH_NRM) reg_maps->usesnrm = 1;
758 else if (ins.handler_idx == WINED3DSIH_DSY) reg_maps->usesdsy = 1;
759 else if (ins.handler_idx == WINED3DSIH_DSX) reg_maps->usesdsx = 1;
760 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
761 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
762 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
763 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
764 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
765
766 limit = ins.src_count + (ins.predicate ? 1 : 0);
767 for (i = 0; i < limit; ++i)
768 {
769 struct wined3d_shader_src_param src_param, src_rel_addr;
770 unsigned int count;
771
772 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
773 count = get_instr_extra_regcount(ins.handler_idx, i);
774
775 shader_record_register_usage(shader, reg_maps, &src_param.reg, shader_version.type);
776 while (count)
777 {
778 ++src_param.reg.idx;
779 shader_record_register_usage(shader, reg_maps, &src_param.reg, shader_version.type);
780 --count;
781 }
782
783 if (color0_mov)
784 {
785 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *)shader;
786 if (src_param.reg.type == WINED3DSPR_TEMP
787 && src_param.swizzle == WINED3DSP_NOSWIZZLE)
788 {
789 ps->color0_mov = TRUE;
790 ps->color0_reg = src_param.reg.idx;
791 }
792 }
793 }
794 }
795 }
796 reg_maps->loop_depth = max_loop_depth;
797
798 shader->baseShader.functionLength = ((const char *)ptr - (const char *)byte_code);
799
800 return WINED3D_OK;
801}
802
803unsigned int shader_find_free_input_register(const struct shader_reg_maps *reg_maps, unsigned int max)
804{
805 DWORD map = 1 << max;
806 map |= map - 1;
807 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
808
809 return wined3d_log2i(map);
810}
811
812static void shader_dump_decl_usage(const struct wined3d_shader_semantic *semantic,
813 const struct wined3d_shader_version *shader_version)
814{
815 TRACE("dcl");
816
817 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
818 {
819 switch (semantic->sampler_type)
820 {
821 case WINED3DSTT_2D: TRACE("_2d"); break;
822 case WINED3DSTT_CUBE: TRACE("_cube"); break;
823 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
824 default: TRACE("_unknown_ttype(0x%08x)", semantic->sampler_type);
825 }
826 }
827 else
828 {
829 /* Pixel shaders 3.0 don't have usage semantics. */
830 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL) return;
831 else TRACE("_");
832
833 switch (semantic->usage)
834 {
835 case WINED3DDECLUSAGE_POSITION:
836 TRACE("position%u", semantic->usage_idx);
837 break;
838
839 case WINED3DDECLUSAGE_BLENDINDICES:
840 TRACE("blend");
841 break;
842
843 case WINED3DDECLUSAGE_BLENDWEIGHT:
844 TRACE("weight");
845 break;
846
847 case WINED3DDECLUSAGE_NORMAL:
848 TRACE("normal%u", semantic->usage_idx);
849 break;
850
851 case WINED3DDECLUSAGE_PSIZE:
852 TRACE("psize");
853 break;
854
855 case WINED3DDECLUSAGE_COLOR:
856 if (semantic->usage_idx == 0) TRACE("color");
857 else TRACE("specular%u", (semantic->usage_idx - 1));
858 break;
859
860 case WINED3DDECLUSAGE_TEXCOORD:
861 TRACE("texture%u", semantic->usage_idx);
862 break;
863
864 case WINED3DDECLUSAGE_TANGENT:
865 TRACE("tangent");
866 break;
867
868 case WINED3DDECLUSAGE_BINORMAL:
869 TRACE("binormal");
870 break;
871
872 case WINED3DDECLUSAGE_TESSFACTOR:
873 TRACE("tessfactor");
874 break;
875
876 case WINED3DDECLUSAGE_POSITIONT:
877 TRACE("positionT%u", semantic->usage_idx);
878 break;
879
880 case WINED3DDECLUSAGE_FOG:
881 TRACE("fog");
882 break;
883
884 case WINED3DDECLUSAGE_DEPTH:
885 TRACE("depth");
886 break;
887
888 case WINED3DDECLUSAGE_SAMPLE:
889 TRACE("sample");
890 break;
891
892 default:
893 FIXME("unknown_semantics(0x%08x)", semantic->usage);
894 }
895 }
896}
897
898static void shader_dump_register(const struct wined3d_shader_register *reg,
899 const struct wined3d_shader_version *shader_version)
900{
901 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
902 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
903 UINT offset = reg->idx;
904
905 switch (reg->type)
906 {
907 case WINED3DSPR_TEMP:
908 TRACE("r");
909 break;
910
911 case WINED3DSPR_INPUT:
912 TRACE("v");
913 break;
914
915 case WINED3DSPR_CONST:
916 case WINED3DSPR_CONST2:
917 case WINED3DSPR_CONST3:
918 case WINED3DSPR_CONST4:
919 TRACE("c");
920 offset = shader_get_float_offset(reg->type, reg->idx);
921 break;
922
923 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
924 TRACE("%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
925 break;
926
927 case WINED3DSPR_RASTOUT:
928 TRACE("%s", rastout_reg_names[reg->idx]);
929 break;
930
931 case WINED3DSPR_COLOROUT:
932 TRACE("oC");
933 break;
934
935 case WINED3DSPR_DEPTHOUT:
936 TRACE("oDepth");
937 break;
938
939 case WINED3DSPR_ATTROUT:
940 TRACE("oD");
941 break;
942
943 case WINED3DSPR_TEXCRDOUT:
944 /* Vertex shaders >= 3.0 use general purpose output registers
945 * (WINED3DSPR_OUTPUT), which can include an address token. */
946 if (shader_version->major >= 3) TRACE("o");
947 else TRACE("oT");
948 break;
949
950 case WINED3DSPR_CONSTINT:
951 TRACE("i");
952 break;
953
954 case WINED3DSPR_CONSTBOOL:
955 TRACE("b");
956 break;
957
958 case WINED3DSPR_LABEL:
959 TRACE("l");
960 break;
961
962 case WINED3DSPR_LOOP:
963 TRACE("aL");
964 break;
965
966 case WINED3DSPR_SAMPLER:
967 TRACE("s");
968 break;
969
970 case WINED3DSPR_MISCTYPE:
971 if (reg->idx > 1) FIXME("Unhandled misctype register %u.\n", reg->idx);
972 else TRACE("%s", misctype_reg_names[reg->idx]);
973 break;
974
975 case WINED3DSPR_PREDICATE:
976 TRACE("p");
977 break;
978
979 case WINED3DSPR_IMMCONST:
980 TRACE("l");
981 break;
982
983 case WINED3DSPR_CONSTBUFFER:
984 TRACE("cb");
985 break;
986
987 default:
988 TRACE("unhandled_rtype(%#x)", reg->type);
989 break;
990 }
991
992 if (reg->type == WINED3DSPR_IMMCONST)
993 {
994 TRACE("(");
995 switch (reg->immconst_type)
996 {
997 case WINED3D_IMMCONST_FLOAT:
998 TRACE("%.8e", *(const float *)reg->immconst_data);
999 break;
1000
1001 case WINED3D_IMMCONST_FLOAT4:
1002 TRACE("%.8e, %.8e, %.8e, %.8e",
1003 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
1004 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
1005 break;
1006
1007 default:
1008 TRACE("<unhandled immconst_type %#x>", reg->immconst_type);
1009 break;
1010 }
1011 TRACE(")");
1012 }
1013 else if (reg->type != WINED3DSPR_RASTOUT && reg->type != WINED3DSPR_MISCTYPE)
1014 {
1015 if (reg->array_idx != ~0U)
1016 {
1017 TRACE("%u[%u", offset, reg->array_idx);
1018 if (reg->rel_addr)
1019 {
1020 TRACE(" + ");
1021 shader_dump_src_param(reg->rel_addr, shader_version);
1022 }
1023 TRACE("]");
1024 }
1025 else
1026 {
1027 if (reg->rel_addr)
1028 {
1029 TRACE("[");
1030 shader_dump_src_param(reg->rel_addr, shader_version);
1031 TRACE(" + ");
1032 }
1033 TRACE("%u", offset);
1034 if (reg->rel_addr) TRACE("]");
1035 }
1036 }
1037}
1038
1039void shader_dump_dst_param(const struct wined3d_shader_dst_param *param,
1040 const struct wined3d_shader_version *shader_version)
1041{
1042 DWORD write_mask = param->write_mask;
1043
1044 shader_dump_register(&param->reg, shader_version);
1045
1046 if (write_mask != WINED3DSP_WRITEMASK_ALL)
1047 {
1048 static const char *write_mask_chars = "xyzw";
1049
1050 TRACE(".");
1051 if (write_mask & WINED3DSP_WRITEMASK_0) TRACE("%c", write_mask_chars[0]);
1052 if (write_mask & WINED3DSP_WRITEMASK_1) TRACE("%c", write_mask_chars[1]);
1053 if (write_mask & WINED3DSP_WRITEMASK_2) TRACE("%c", write_mask_chars[2]);
1054 if (write_mask & WINED3DSP_WRITEMASK_3) TRACE("%c", write_mask_chars[3]);
1055 }
1056}
1057
1058void shader_dump_src_param(const struct wined3d_shader_src_param *param,
1059 const struct wined3d_shader_version *shader_version)
1060{
1061 DWORD src_modifier = param->modifiers;
1062 DWORD swizzle = param->swizzle;
1063
1064 if (src_modifier == WINED3DSPSM_NEG
1065 || src_modifier == WINED3DSPSM_BIASNEG
1066 || src_modifier == WINED3DSPSM_SIGNNEG
1067 || src_modifier == WINED3DSPSM_X2NEG
1068 || src_modifier == WINED3DSPSM_ABSNEG)
1069 TRACE("-");
1070 else if (src_modifier == WINED3DSPSM_COMP)
1071 TRACE("1-");
1072 else if (src_modifier == WINED3DSPSM_NOT)
1073 TRACE("!");
1074
1075 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
1076 TRACE("abs(");
1077
1078 shader_dump_register(&param->reg, shader_version);
1079
1080 if (src_modifier)
1081 {
1082 switch (src_modifier)
1083 {
1084 case WINED3DSPSM_NONE: break;
1085 case WINED3DSPSM_NEG: break;
1086 case WINED3DSPSM_NOT: break;
1087 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
1088 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
1089 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
1090 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
1091 case WINED3DSPSM_COMP: break;
1092 case WINED3DSPSM_X2: TRACE("_x2"); break;
1093 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
1094 case WINED3DSPSM_DZ: TRACE("_dz"); break;
1095 case WINED3DSPSM_DW: TRACE("_dw"); break;
1096 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
1097 case WINED3DSPSM_ABS: TRACE(")"); break;
1098 default: TRACE("_unknown_modifier(%#x)", src_modifier);
1099 }
1100 }
1101
1102 if (swizzle != WINED3DSP_NOSWIZZLE)
1103 {
1104 static const char *swizzle_chars = "xyzw";
1105 DWORD swizzle_x = swizzle & 0x03;
1106 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1107 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1108 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1109
1110 if (swizzle_x == swizzle_y
1111 && swizzle_x == swizzle_z
1112 && swizzle_x == swizzle_w)
1113 {
1114 TRACE(".%c", swizzle_chars[swizzle_x]);
1115 }
1116 else
1117 {
1118 TRACE(".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
1119 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
1120 }
1121 }
1122}
1123
1124/* Shared code in order to generate the bulk of the shader string.
1125 * NOTE: A description of how to parse tokens can be found on MSDN. */
1126void shader_generate_main(IWineD3DBaseShader *iface, struct wined3d_shader_buffer *buffer,
1127 const shader_reg_maps *reg_maps, const DWORD *byte_code, void *backend_ctx)
1128{
1129 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
1130 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)shader->baseShader.device;
1131 const struct wined3d_shader_frontend *fe = shader->baseShader.frontend;
1132 void *fe_data = shader->baseShader.frontend_data;
1133 struct wined3d_shader_src_param src_rel_addr[4];
1134 struct wined3d_shader_src_param src_param[4];
1135 struct wined3d_shader_version shader_version;
1136 struct wined3d_shader_src_param dst_rel_addr;
1137 struct wined3d_shader_dst_param dst_param;
1138 struct wined3d_shader_instruction ins;
1139 struct wined3d_shader_context ctx;
1140 const DWORD *ptr = byte_code;
1141 DWORD i;
1142
1143 /* Initialize current parsing state. */
1144 ctx.shader = iface;
1145 ctx.gl_info = &device->adapter->gl_info;
1146 ctx.reg_maps = reg_maps;
1147 ctx.buffer = buffer;
1148 ctx.backend_data = backend_ctx;
1149
1150 ins.ctx = &ctx;
1151 ins.dst = &dst_param;
1152 ins.src = src_param;
1153 shader->baseShader.parse_state.current_row = 0;
1154
1155 fe->shader_read_header(fe_data, &ptr, &shader_version);
1156
1157 while (!fe->shader_is_end(fe_data, &ptr))
1158 {
1159 const char *comment;
1160 UINT comment_size;
1161 UINT param_size;
1162
1163 /* Skip comment tokens. */
1164 fe->shader_read_comment(&ptr, &comment, &comment_size);
1165 if (comment) continue;
1166
1167 /* Read opcode. */
1168 fe->shader_read_opcode(fe_data, &ptr, &ins, &param_size);
1169
1170 /* Unknown opcode and its parameters. */
1171 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1172 {
1173 TRACE("Skipping unrecognized instruction.\n");
1174 ptr += param_size;
1175 continue;
1176 }
1177
1178 /* Nothing to do. */
1179 if (ins.handler_idx == WINED3DSIH_DCL
1180 || ins.handler_idx == WINED3DSIH_NOP
1181 || ins.handler_idx == WINED3DSIH_DEF
1182 || ins.handler_idx == WINED3DSIH_DEFI
1183 || ins.handler_idx == WINED3DSIH_DEFB
1184 || ins.handler_idx == WINED3DSIH_PHASE)
1185 {
1186 ptr += param_size;
1187 continue;
1188 }
1189
1190 /* Destination token */
1191 if (ins.dst_count) fe->shader_read_dst_param(fe_data, &ptr, &dst_param, &dst_rel_addr);
1192
1193 /* Predication token */
1194 if (ins.predicate)
1195 {
1196#ifndef VBOX_WITH_VMSVGA
1197 FIXME("Predicates not implemented.\n");
1198#endif
1199 ins.predicate = *ptr++;
1200 }
1201
1202 /* Other source tokens */
1203 for (i = 0; i < ins.src_count; ++i)
1204 {
1205 fe->shader_read_src_param(fe_data, &ptr, &src_param[i], &src_rel_addr[i]);
1206 }
1207
1208 /* Call appropriate function for output target */
1209 device->shader_backend->shader_handle_instruction(&ins);
1210 }
1211}
1212
1213static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param *dst)
1214{
1215 DWORD mmask = dst->modifiers;
1216
1217 switch (dst->shift)
1218 {
1219 case 0: break;
1220 case 13: TRACE("_d8"); break;
1221 case 14: TRACE("_d4"); break;
1222 case 15: TRACE("_d2"); break;
1223 case 1: TRACE("_x2"); break;
1224 case 2: TRACE("_x4"); break;
1225 case 3: TRACE("_x8"); break;
1226 default: TRACE("_unhandled_shift(%d)", dst->shift); break;
1227 }
1228
1229 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
1230 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
1231 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
1232
1233 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
1234 if (mmask) FIXME("_unrecognized_modifier(%#x)", mmask);
1235}
1236
1237static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *byte_code)
1238{
1239 struct wined3d_shader_version shader_version;
1240 const DWORD *ptr = byte_code;
1241 const char *type_prefix;
1242 DWORD i;
1243
1244 TRACE("Parsing %p.\n", byte_code);
1245
1246 fe->shader_read_header(fe_data, &ptr, &shader_version);
1247
1248 switch (shader_version.type)
1249 {
1250 case WINED3D_SHADER_TYPE_VERTEX:
1251 type_prefix = "vs";
1252 break;
1253
1254 case WINED3D_SHADER_TYPE_GEOMETRY:
1255 type_prefix = "gs";
1256 break;
1257
1258 case WINED3D_SHADER_TYPE_PIXEL:
1259 type_prefix = "ps";
1260 break;
1261
1262 default:
1263 FIXME("Unhandled shader type %#x.\n", shader_version.type);
1264 type_prefix = "unknown";
1265 break;
1266 }
1267
1268 TRACE("%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
1269
1270 while (!fe->shader_is_end(fe_data, &ptr))
1271 {
1272 struct wined3d_shader_instruction ins;
1273 const char *comment;
1274 UINT comment_size;
1275 UINT param_size;
1276
1277 /* comment */
1278 fe->shader_read_comment(&ptr, &comment, &comment_size);
1279 if (comment)
1280 {
1281 if (comment_size > 4 && *(const DWORD *)comment == WINEMAKEFOURCC('T', 'E', 'X', 'T'))
1282 {
1283 const char *end = comment + comment_size;
1284 const char *cur = comment + 4;
1285 const char *line = cur;
1286
1287 TRACE("// TEXT\n");
1288 while (cur != end)
1289 {
1290 if (*cur == '\n')
1291 {
1292 UINT len = cur - line;
1293 if (len && *(cur - 1) == '\r') --len;
1294 TRACE("// %s\n", debugstr_an(line, len));
1295 line = ++cur;
1296 }
1297 else ++cur;
1298 }
1299 if (line != cur) TRACE("// %s\n", debugstr_an(line, cur - line));
1300 }
1301 else TRACE("// %s\n", debugstr_an(comment, comment_size));
1302 continue;
1303 }
1304
1305 fe->shader_read_opcode(fe_data, &ptr, &ins, &param_size);
1306 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1307 {
1308 TRACE("Skipping unrecognized instruction.\n");
1309 ptr += param_size;
1310 continue;
1311 }
1312
1313 if (ins.handler_idx == WINED3DSIH_DCL)
1314 {
1315 struct wined3d_shader_semantic semantic;
1316
1317 fe->shader_read_semantic(&ptr, &semantic);
1318
1319 shader_dump_decl_usage(&semantic, &shader_version);
1320 shader_dump_ins_modifiers(&semantic.reg);
1321 TRACE(" ");
1322 shader_dump_dst_param(&semantic.reg, &shader_version);
1323 }
1324 else if (ins.handler_idx == WINED3DSIH_DEF)
1325 {
1326 struct wined3d_shader_src_param rel_addr;
1327 struct wined3d_shader_dst_param dst;
1328
1329 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1330
1331 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(dst.reg.type, dst.reg.idx),
1332 *(const float *)(ptr),
1333 *(const float *)(ptr + 1),
1334 *(const float *)(ptr + 2),
1335 *(const float *)(ptr + 3));
1336 ptr += 4;
1337 }
1338 else if (ins.handler_idx == WINED3DSIH_DEFI)
1339 {
1340 struct wined3d_shader_src_param rel_addr;
1341 struct wined3d_shader_dst_param dst;
1342
1343 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1344
1345 TRACE("defi i%u = %d, %d, %d, %d", dst.reg.idx,
1346 *(ptr),
1347 *(ptr + 1),
1348 *(ptr + 2),
1349 *(ptr + 3));
1350 ptr += 4;
1351 }
1352 else if (ins.handler_idx == WINED3DSIH_DEFB)
1353 {
1354 struct wined3d_shader_src_param rel_addr;
1355 struct wined3d_shader_dst_param dst;
1356
1357 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1358
1359 TRACE("defb b%u = %s", dst.reg.idx, *ptr ? "true" : "false");
1360 ++ptr;
1361 }
1362 else
1363 {
1364 struct wined3d_shader_src_param dst_rel_addr, src_rel_addr;
1365 struct wined3d_shader_dst_param dst_param;
1366 struct wined3d_shader_src_param src_param;
1367
1368 if (ins.dst_count)
1369 {
1370 fe->shader_read_dst_param(fe_data, &ptr, &dst_param, &dst_rel_addr);
1371 }
1372
1373 /* Print out predication source token first - it follows
1374 * the destination token. */
1375 if (ins.predicate)
1376 {
1377 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
1378 TRACE("(");
1379 shader_dump_src_param(&src_param, &shader_version);
1380 TRACE(") ");
1381 }
1382
1383 /* PixWin marks instructions with the coissue flag with a '+' */
1384 if (ins.coissue) TRACE("+");
1385
1386 TRACE("%s", shader_opcode_names[ins.handler_idx]);
1387
1388 if (ins.handler_idx == WINED3DSIH_IFC
1389 || ins.handler_idx == WINED3DSIH_BREAKC)
1390 {
1391 switch (ins.flags)
1392 {
1393 case COMPARISON_GT: TRACE("_gt"); break;
1394 case COMPARISON_EQ: TRACE("_eq"); break;
1395 case COMPARISON_GE: TRACE("_ge"); break;
1396 case COMPARISON_LT: TRACE("_lt"); break;
1397 case COMPARISON_NE: TRACE("_ne"); break;
1398 case COMPARISON_LE: TRACE("_le"); break;
1399 default: TRACE("_(%u)", ins.flags);
1400 }
1401 }
1402 else if (ins.handler_idx == WINED3DSIH_TEX
1403 && shader_version.major >= 2
1404 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
1405 {
1406 TRACE("p");
1407 }
1408
1409 /* We already read the destination token, print it. */
1410 if (ins.dst_count)
1411 {
1412 shader_dump_ins_modifiers(&dst_param);
1413 TRACE(" ");
1414 shader_dump_dst_param(&dst_param, &shader_version);
1415 }
1416
1417 /* Other source tokens */
1418 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
1419 {
1420 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
1421 TRACE(!i ? " " : ", ");
1422 shader_dump_src_param(&src_param, &shader_version);
1423 }
1424 }
1425 TRACE("\n");
1426 }
1427}
1428
1429static void shader_cleanup(IWineD3DBaseShader *iface)
1430{
1431 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
1432
1433 ((IWineD3DDeviceImpl *)shader->baseShader.device)->shader_backend->shader_destroy(iface);
1434 HeapFree(GetProcessHeap(), 0, shader->baseShader.reg_maps.constf);
1435 HeapFree(GetProcessHeap(), 0, shader->baseShader.function);
1436 shader_delete_constant_list(&shader->baseShader.constantsF);
1437 shader_delete_constant_list(&shader->baseShader.constantsB);
1438 shader_delete_constant_list(&shader->baseShader.constantsI);
1439 list_remove(&shader->baseShader.shader_list_entry);
1440
1441 if (shader->baseShader.frontend && shader->baseShader.frontend_data)
1442 {
1443 shader->baseShader.frontend->shader_free(shader->baseShader.frontend_data);
1444 }
1445}
1446
1447static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
1448static void shader_none_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS) {}
1449static void shader_none_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {}
1450static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1451static void shader_none_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1452static void shader_none_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1453static void shader_none_load_constants(const struct wined3d_context *context, char usePS, char useVS) {}
1454static void shader_none_load_np2fixup_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1455static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1456static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1457static void shader_none_free(IWineD3DDevice *iface) {}
1458static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1459
1460static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
1461{
1462 /* Set the shader caps to 0 for the none shader backend */
1463 caps->VertexShaderVersion = 0;
1464 caps->PixelShaderVersion = 0;
1465 caps->PixelShader1xMaxValue = 0.0f;
1466}
1467
1468static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1469{
1470 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1471 {
1472 TRACE("Checking support for fixup:\n");
1473 dump_color_fixup_desc(fixup);
1474 }
1475
1476 /* Faked to make some apps happy. */
1477 if (!is_complex_fixup(fixup))
1478 {
1479 TRACE("[OK]\n");
1480 return TRUE;
1481 }
1482
1483 TRACE("[FAILED]\n");
1484 return FALSE;
1485}
1486
1487const shader_backend_t none_shader_backend = {
1488 shader_none_handle_instruction,
1489 shader_none_select,
1490 shader_none_select_depth_blt,
1491 shader_none_deselect_depth_blt,
1492 shader_none_update_float_vertex_constants,
1493 shader_none_update_float_pixel_constants,
1494 shader_none_load_constants,
1495 shader_none_load_np2fixup_constants,
1496 shader_none_destroy,
1497 shader_none_alloc,
1498 shader_none_free,
1499 shader_none_dirty_const,
1500 shader_none_get_caps,
1501 shader_none_color_fixup_supported,
1502};
1503
1504static void shader_get_parent(IWineD3DBaseShaderImpl *shader, IUnknown **parent)
1505{
1506 *parent = shader->baseShader.parent;
1507 IUnknown_AddRef(*parent);
1508 TRACE("shader %p, returning %p.\n", shader, *parent);
1509}
1510
1511static HRESULT shader_get_function(IWineD3DBaseShaderImpl *shader, void *data, UINT *data_size)
1512{
1513 if (!data)
1514 {
1515 *data_size = shader->baseShader.functionLength;
1516 return WINED3D_OK;
1517 }
1518
1519 if (*data_size < shader->baseShader.functionLength)
1520 {
1521 /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
1522 * than the required size we should write the required size and
1523 * return D3DERR_MOREDATA. That's not actually true. */
1524 return WINED3DERR_INVALIDCALL;
1525 }
1526
1527 memcpy(data, shader->baseShader.function, shader->baseShader.functionLength);
1528
1529 return WINED3D_OK;
1530}
1531
1532static HRESULT shader_set_function(IWineD3DBaseShaderImpl *shader, const DWORD *byte_code,
1533 const struct wined3d_shader_signature *output_signature, DWORD float_const_count)
1534{
1535 struct shader_reg_maps *reg_maps = &shader->baseShader.reg_maps;
1536 const struct wined3d_shader_frontend *fe;
1537 HRESULT hr;
1538
1539 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1540 shader, byte_code, output_signature, float_const_count);
1541
1542 fe = shader_select_frontend(*byte_code);
1543 if (!fe)
1544 {
1545 FIXME("Unable to find frontend for shader.\n");
1546 return WINED3DERR_INVALIDCALL;
1547 }
1548 shader->baseShader.frontend = fe;
1549 shader->baseShader.frontend_data = fe->shader_init(byte_code, output_signature);
1550 if (!shader->baseShader.frontend_data)
1551 {
1552 FIXME("Failed to initialize frontend.\n");
1553 return WINED3DERR_INVALIDCALL;
1554 }
1555
1556 /* First pass: trace shader. */
1557 if (TRACE_ON(d3d_shader)) shader_trace_init(fe, shader->baseShader.frontend_data, byte_code);
1558
1559 /* Initialize immediate constant lists. */
1560 list_init(&shader->baseShader.constantsF);
1561 list_init(&shader->baseShader.constantsB);
1562 list_init(&shader->baseShader.constantsI);
1563
1564 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1565 hr = shader_get_registers_used((IWineD3DBaseShader *)shader, fe,
1566 reg_maps, shader->baseShader.input_signature, shader->baseShader.output_signature,
1567 byte_code, float_const_count);
1568 if (FAILED(hr)) return hr;
1569
1570 shader->baseShader.function = HeapAlloc(GetProcessHeap(), 0, shader->baseShader.functionLength);
1571 if (!shader->baseShader.function) return E_OUTOFMEMORY;
1572 memcpy(shader->baseShader.function, byte_code, shader->baseShader.functionLength);
1573
1574 return WINED3D_OK;
1575}
1576
1577static HRESULT STDMETHODCALLTYPE vertexshader_QueryInterface(IWineD3DVertexShader *iface, REFIID riid, void **object)
1578{
1579 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1580
1581 if (IsEqualGUID(riid, &IID_IWineD3DVertexShader)
1582 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1583 || IsEqualGUID(riid, &IID_IWineD3DBase)
1584 || IsEqualGUID(riid, &IID_IUnknown))
1585 {
1586 IUnknown_AddRef(iface);
1587 *object = iface;
1588 return S_OK;
1589 }
1590
1591 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1592
1593 *object = NULL;
1594 return E_NOINTERFACE;
1595}
1596
1597static ULONG STDMETHODCALLTYPE vertexshader_AddRef(IWineD3DVertexShader *iface)
1598{
1599 IWineD3DVertexShaderImpl *shader = (IWineD3DVertexShaderImpl *)iface;
1600 ULONG refcount = InterlockedIncrement(&shader->baseShader.ref);
1601
1602 TRACE("%p increasing refcount to %u.\n", shader, refcount);
1603
1604 return refcount;
1605}
1606
1607#ifdef VBOX_WINE_WITH_SHADER_CACHE
1608static void shader_chache_put(PVBOXEXT_HASHCACHE pCache, IWineD3DBaseShaderImpl *pShader);
1609#endif
1610
1611static ULONG STDMETHODCALLTYPE vertexshader_Release(IWineD3DVertexShader *iface)
1612{
1613 IWineD3DVertexShaderImpl *shader = (IWineD3DVertexShaderImpl *)iface;
1614 ULONG refcount = InterlockedDecrement(&shader->baseShader.ref);
1615
1616 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1617
1618 if (!refcount)
1619 {
1620#ifdef VBOX_WINE_WITH_SHADER_CACHE
1621 shader_chache_put(&((IWineD3DDeviceImpl*)shader->baseShader.device)->vshaderCache, (IWineD3DBaseShaderImpl*)shader);
1622#else
1623 shader_cleanup((IWineD3DBaseShader *)iface);
1624#ifndef VBOX_WITH_VMSVGA
1625 shader->baseShader.parent_ops->wined3d_object_destroyed(shader->baseShader.parent);
1626#endif
1627 HeapFree(GetProcessHeap(), 0, shader);
1628#endif
1629 }
1630
1631 return refcount;
1632}
1633
1634static HRESULT STDMETHODCALLTYPE vertexshader_GetParent(IWineD3DVertexShader *iface, IUnknown **parent)
1635{
1636 TRACE("iface %p, parent %p.\n", iface, parent);
1637
1638 shader_get_parent((IWineD3DBaseShaderImpl *)iface, parent);
1639
1640 return WINED3D_OK;
1641}
1642
1643static HRESULT STDMETHODCALLTYPE vertexshader_GetFunction(IWineD3DVertexShader *iface, void *data, UINT *data_size)
1644{
1645 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
1646
1647 return shader_get_function((IWineD3DBaseShaderImpl *)iface, data, data_size);
1648}
1649
1650/* Set local constants for d3d8 shaders. */
1651static HRESULT STDMETHODCALLTYPE vertexshader_SetLocalConstantsF(IWineD3DVertexShader *iface,
1652 UINT start_idx, const float *src_data, UINT count)
1653{
1654 IWineD3DVertexShaderImpl *shader =(IWineD3DVertexShaderImpl *)iface;
1655 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)shader->baseShader.device;
1656 UINT i, end_idx;
1657
1658 TRACE("iface %p, start_idx %u, src_data %p, count %u.\n", iface, start_idx, src_data, count);
1659
1660 end_idx = start_idx + count;
1661 if (end_idx > device->d3d_vshader_constantF)
1662 {
1663 WARN("end_idx %u > float constants limit %u.\n", end_idx, device->d3d_vshader_constantF);
1664 end_idx = device->d3d_vshader_constantF;
1665 }
1666
1667 for (i = start_idx; i < end_idx; ++i)
1668 {
1669 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
1670 if (!lconst) return E_OUTOFMEMORY;
1671
1672 lconst->idx = i;
1673 memcpy(lconst->value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
1674 list_add_head(&shader->baseShader.constantsF, &lconst->entry);
1675 }
1676
1677 return WINED3D_OK;
1678}
1679
1680static const IWineD3DVertexShaderVtbl IWineD3DVertexShader_Vtbl =
1681{
1682 /* IUnknown methods */
1683 vertexshader_QueryInterface,
1684 vertexshader_AddRef,
1685 vertexshader_Release,
1686 /* IWineD3DBase methods */
1687 vertexshader_GetParent,
1688 /* IWineD3DBaseShader methods */
1689 vertexshader_GetFunction,
1690 /* IWineD3DVertexShader methods */
1691 vertexshader_SetLocalConstantsF,
1692};
1693
1694void find_vs_compile_args(IWineD3DVertexShaderImpl *shader,
1695 IWineD3DStateBlockImpl *stateblock, struct vs_compile_args *args)
1696{
1697 args->fog_src = stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
1698 args->clip_enabled = stateblock->renderState[WINED3DRS_CLIPPING]
1699 && stateblock->renderState[WINED3DRS_CLIPPLANEENABLE];
1700 args->swizzle_map = ((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.swizzle_map;
1701}
1702
1703static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
1704{
1705 if (usage_idx1 != usage_idx2) return FALSE;
1706 if (usage1 == usage2) return TRUE;
1707 if (usage1 == WINED3DDECLUSAGE_POSITION && usage2 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
1708 if (usage2 == WINED3DDECLUSAGE_POSITION && usage1 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
1709
1710 return FALSE;
1711}
1712
1713BOOL vshader_get_input(IWineD3DVertexShader *iface, BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
1714{
1715 IWineD3DVertexShaderImpl *shader = (IWineD3DVertexShaderImpl *)iface;
1716 WORD map = shader->baseShader.reg_maps.input_registers;
1717 unsigned int i;
1718
1719 for (i = 0; map; map >>= 1, ++i)
1720 {
1721 if (!(map & 1)) continue;
1722
1723 if (match_usage(shader->attributes[i].usage,
1724 shader->attributes[i].usage_idx, usage_req, usage_idx_req))
1725 {
1726 *regnum = i;
1727 return TRUE;
1728 }
1729 }
1730 return FALSE;
1731}
1732
1733static void vertexshader_set_limits(IWineD3DVertexShaderImpl *shader)
1734{
1735 DWORD shader_version = WINED3D_SHADER_VERSION(shader->baseShader.reg_maps.shader_version.major,
1736 shader->baseShader.reg_maps.shader_version.minor);
1737 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)shader->baseShader.device;
1738
1739 shader->baseShader.limits.texcoord = 0;
1740 shader->baseShader.limits.attributes = 16;
1741 shader->baseShader.limits.packed_input = 0;
1742
1743 switch (shader_version)
1744 {
1745 case WINED3D_SHADER_VERSION(1, 0):
1746 case WINED3D_SHADER_VERSION(1, 1):
1747 shader->baseShader.limits.temporary = 12;
1748 shader->baseShader.limits.constant_bool = 0;
1749 shader->baseShader.limits.constant_int = 0;
1750 shader->baseShader.limits.address = 1;
1751 shader->baseShader.limits.packed_output = 0;
1752 shader->baseShader.limits.sampler = 0;
1753 shader->baseShader.limits.label = 0;
1754 /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1755 * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1756 * constants? */
1757 shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1758 break;
1759
1760 case WINED3D_SHADER_VERSION(2, 0):
1761 case WINED3D_SHADER_VERSION(2, 1):
1762 shader->baseShader.limits.temporary = 12;
1763 shader->baseShader.limits.constant_bool = 16;
1764 shader->baseShader.limits.constant_int = 16;
1765 shader->baseShader.limits.address = 1;
1766 shader->baseShader.limits.packed_output = 0;
1767 shader->baseShader.limits.sampler = 0;
1768 shader->baseShader.limits.label = 16;
1769 shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1770 break;
1771
1772 case WINED3D_SHADER_VERSION(4, 0):
1773 FIXME("Using 3.0 limits for 4.0 shader.\n");
1774 RT_FALL_THRU();
1775
1776 case WINED3D_SHADER_VERSION(3, 0):
1777 shader->baseShader.limits.temporary = 32;
1778 shader->baseShader.limits.constant_bool = 32;
1779 shader->baseShader.limits.constant_int = 32;
1780 shader->baseShader.limits.address = 1;
1781 shader->baseShader.limits.packed_output = 12;
1782 shader->baseShader.limits.sampler = 4;
1783 shader->baseShader.limits.label = 16; /* FIXME: 2048 */
1784 /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1785 * even though they are capable of supporting much more (GL
1786 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1787 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1788 * shaders to 256. */
1789 shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1790 break;
1791
1792 default:
1793 shader->baseShader.limits.temporary = 12;
1794 shader->baseShader.limits.constant_bool = 16;
1795 shader->baseShader.limits.constant_int = 16;
1796 shader->baseShader.limits.address = 1;
1797 shader->baseShader.limits.packed_output = 0;
1798 shader->baseShader.limits.sampler = 0;
1799 shader->baseShader.limits.label = 16;
1800 shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1801 FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1802 shader->baseShader.reg_maps.shader_version.major,
1803 shader->baseShader.reg_maps.shader_version.minor);
1804 }
1805}
1806
1807HRESULT vertexshader_init(IWineD3DVertexShaderImpl *shader, IWineD3DDeviceImpl *device,
1808 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1809 IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
1810{
1811 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1812 struct shader_reg_maps *reg_maps = &shader->baseShader.reg_maps;
1813 unsigned int i;
1814 HRESULT hr;
1815 WORD map;
1816
1817 if (!byte_code) return WINED3DERR_INVALIDCALL;
1818
1819 shader->lpVtbl = &IWineD3DVertexShader_Vtbl;
1820 shader_init(&shader->baseShader, device, parent, parent_ops);
1821
1822 hr = shader_set_function((IWineD3DBaseShaderImpl *)shader, byte_code,
1823 output_signature, device->d3d_vshader_constantF);
1824 if (FAILED(hr))
1825 {
1826 WARN("Failed to set function, hr %#x.\n", hr);
1827 shader_cleanup((IWineD3DBaseShader *)shader);
1828 return hr;
1829 }
1830
1831 map = shader->baseShader.reg_maps.input_registers;
1832 for (i = 0; map; map >>= 1, ++i)
1833 {
1834 if (!(map & 1) || !shader->baseShader.input_signature[i].semantic_name) continue;
1835
1836 shader->attributes[i].usage =
1837 shader_usage_from_semantic_name(shader->baseShader.input_signature[i].semantic_name);
1838 shader->attributes[i].usage_idx = shader->baseShader.input_signature[i].semantic_idx;
1839 }
1840
1841 if (output_signature)
1842 {
1843 for (i = 0; i < output_signature->element_count; ++i)
1844 {
1845 struct wined3d_shader_signature_element *e = &output_signature->elements[i];
1846 reg_maps->output_registers |= 1 << e->register_idx;
1847 shader->baseShader.output_signature[e->register_idx] = *e;
1848 }
1849 }
1850
1851 vertexshader_set_limits(shader);
1852
1853 if (device->vs_selected_mode == SHADER_ARB
1854 && (gl_info->quirks & WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT)
1855 && shader->min_rel_offset <= shader->max_rel_offset)
1856 {
1857 if (shader->max_rel_offset - shader->min_rel_offset > 127)
1858 {
1859 FIXME("The difference between the minimum and maximum relative offset is > 127.\n");
1860 FIXME("Which this OpenGL implementation does not support. Try using GLSL.\n");
1861 FIXME("Min: %d, Max: %d.\n", shader->min_rel_offset, shader->max_rel_offset);
1862 }
1863 else if (shader->max_rel_offset - shader->min_rel_offset > 63)
1864 {
1865 shader->rel_offset = shader->min_rel_offset + 63;
1866 }
1867 else if (shader->max_rel_offset > 63)
1868 {
1869 shader->rel_offset = shader->min_rel_offset;
1870 }
1871 else
1872 {
1873 shader->rel_offset = 0;
1874 }
1875 }
1876
1877 shader->baseShader.load_local_constsF = shader->baseShader.reg_maps.usesrelconstF
1878 && !list_empty(&shader->baseShader.constantsF);
1879
1880 return WINED3D_OK;
1881}
1882
1883static HRESULT STDMETHODCALLTYPE geometryshader_QueryInterface(IWineD3DGeometryShader *iface,
1884 REFIID riid, void **object)
1885{
1886 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1887
1888 if (IsEqualGUID(riid, &IID_IWineD3DGeometryShader)
1889 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1890 || IsEqualGUID(riid, &IID_IWineD3DBase)
1891 || IsEqualGUID(riid, &IID_IUnknown))
1892 {
1893 IUnknown_AddRef(iface);
1894 *object = iface;
1895 return S_OK;
1896 }
1897
1898 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1899
1900 *object = NULL;
1901 return E_NOINTERFACE;
1902}
1903
1904static ULONG STDMETHODCALLTYPE geometryshader_AddRef(IWineD3DGeometryShader *iface)
1905{
1906 struct wined3d_geometryshader *shader = (struct wined3d_geometryshader *)iface;
1907 ULONG refcount = InterlockedIncrement(&shader->base_shader.ref);
1908
1909 TRACE("%p increasing refcount to %u.\n", shader, refcount);
1910
1911 return refcount;
1912}
1913
1914static ULONG STDMETHODCALLTYPE geometryshader_Release(IWineD3DGeometryShader *iface)
1915{
1916 struct wined3d_geometryshader *shader = (struct wined3d_geometryshader *)iface;
1917 ULONG refcount = InterlockedDecrement(&shader->base_shader.ref);
1918
1919 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1920
1921 if (!refcount)
1922 {
1923 shader_cleanup((IWineD3DBaseShader *)iface);
1924 shader->base_shader.parent_ops->wined3d_object_destroyed(shader->base_shader.parent);
1925 HeapFree(GetProcessHeap(), 0, shader);
1926 }
1927
1928 return refcount;
1929}
1930
1931static HRESULT STDMETHODCALLTYPE geometryshader_GetParent(IWineD3DGeometryShader *iface, IUnknown **parent)
1932{
1933 TRACE("iface %p, parent %p.\n", iface, parent);
1934
1935 shader_get_parent((IWineD3DBaseShaderImpl *)iface, parent);
1936
1937 return WINED3D_OK;
1938}
1939
1940static HRESULT STDMETHODCALLTYPE geometryshader_GetFunction(IWineD3DGeometryShader *iface, void *data, UINT *data_size)
1941{
1942 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
1943
1944 return shader_get_function((IWineD3DBaseShaderImpl *)iface, data, data_size);
1945}
1946
1947static const IWineD3DGeometryShaderVtbl wined3d_geometryshader_vtbl =
1948{
1949 /* IUnknown methods */
1950 geometryshader_QueryInterface,
1951 geometryshader_AddRef,
1952 geometryshader_Release,
1953 /* IWineD3DBase methods */
1954 geometryshader_GetParent,
1955 /* IWineD3DBaseShader methods */
1956 geometryshader_GetFunction,
1957};
1958
1959HRESULT geometryshader_init(struct wined3d_geometryshader *shader, IWineD3DDeviceImpl *device,
1960 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1961 IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
1962{
1963 HRESULT hr;
1964
1965 shader->vtbl = &wined3d_geometryshader_vtbl;
1966 shader_init(&shader->base_shader, device, parent, parent_ops);
1967
1968 hr = shader_set_function((IWineD3DBaseShaderImpl *)shader, byte_code, output_signature, 0);
1969 if (FAILED(hr))
1970 {
1971 WARN("Failed to set function, hr %#x.\n", hr);
1972 shader_cleanup((IWineD3DBaseShader *)shader);
1973 return hr;
1974 }
1975
1976 shader->base_shader.load_local_constsF = FALSE;
1977
1978 return WINED3D_OK;
1979}
1980
1981static HRESULT STDMETHODCALLTYPE pixelshader_QueryInterface(IWineD3DPixelShader *iface, REFIID riid, void **object)
1982{
1983 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1984
1985 if (IsEqualGUID(riid, &IID_IWineD3DPixelShader)
1986 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1987 || IsEqualGUID(riid, &IID_IWineD3DBase)
1988 || IsEqualGUID(riid, &IID_IUnknown))
1989 {
1990 IUnknown_AddRef(iface);
1991 *object = iface;
1992 return S_OK;
1993 }
1994
1995 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1996
1997 *object = NULL;
1998 return E_NOINTERFACE;
1999}
2000
2001static ULONG STDMETHODCALLTYPE pixelshader_AddRef(IWineD3DPixelShader *iface)
2002{
2003 IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)iface;
2004 ULONG refcount = InterlockedIncrement(&shader->baseShader.ref);
2005
2006 TRACE("%p increasing refcount to %u.\n", shader, refcount);
2007
2008 return refcount;
2009}
2010
2011static ULONG STDMETHODCALLTYPE pixelshader_Release(IWineD3DPixelShader *iface)
2012{
2013 IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)iface;
2014 ULONG refcount = InterlockedDecrement(&shader->baseShader.ref);
2015
2016 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
2017
2018 if (!refcount)
2019 {
2020#ifdef VBOX_WINE_WITH_SHADER_CACHE
2021 shader_chache_put(&((IWineD3DDeviceImpl*)shader->baseShader.device)->pshaderCache, (IWineD3DBaseShaderImpl*)shader);
2022#else
2023 shader_cleanup((IWineD3DBaseShader *)iface);
2024#ifndef VBOX_WITH_VMSVGA
2025 shader->baseShader.parent_ops->wined3d_object_destroyed(shader->baseShader.parent);
2026#endif
2027 HeapFree(GetProcessHeap(), 0, shader);
2028#endif
2029 }
2030
2031 return refcount;
2032}
2033
2034static HRESULT STDMETHODCALLTYPE pixelshader_GetParent(IWineD3DPixelShader *iface, IUnknown **parent)
2035{
2036 TRACE("iface %p, parent %p.\n", iface, parent);
2037
2038 shader_get_parent((IWineD3DBaseShaderImpl *)iface, parent);
2039
2040 return WINED3D_OK;
2041}
2042
2043static HRESULT STDMETHODCALLTYPE pixelshader_GetFunction(IWineD3DPixelShader *iface, void *data, UINT *data_size)
2044{
2045 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
2046
2047 return shader_get_function((IWineD3DBaseShaderImpl *)iface, data, data_size);
2048}
2049
2050static const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
2051{
2052 /* IUnknown methods */
2053 pixelshader_QueryInterface,
2054 pixelshader_AddRef,
2055 pixelshader_Release,
2056 /* IWineD3DBase methods */
2057 pixelshader_GetParent,
2058 /* IWineD3DBaseShader methods */
2059 pixelshader_GetFunction
2060};
2061
2062void find_ps_compile_args(IWineD3DPixelShaderImpl *shader,
2063 IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args)
2064{
2065 IWineD3DBaseTextureImpl *texture;
2066 UINT i;
2067
2068 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
2069 args->srgb_correction = stateblock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
2070 args->np2_fixup = 0;
2071 args->t_mirror = 0;
2072
2073 if (shader->baseShader.reg_maps.shader_version.major==1
2074 && shader->baseShader.reg_maps.shader_version.minor<=3)
2075 {
2076 for (i=0; i<4; ++i)
2077 {
2078 DWORD flags = stateblock->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2079 DWORD tex_transform = flags & ~WINED3DTTFF_PROJECTED;
2080 if (flags & WINED3DTTFF_PROJECTED)
2081 {
2082 tex_transform |= WINED3D_PSARGS_PROJECTED;
2083 }
2084 args->tex_transform |= tex_transform << (i*WINED3D_PSARGS_TEXTRANSFORM_SHIFT);
2085 }
2086 }
2087
2088 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2089 {
2090 if (!shader->baseShader.reg_maps.sampler_type[i]) continue;
2091 texture = (IWineD3DBaseTextureImpl *)stateblock->textures[i];
2092 if (!texture)
2093 {
2094 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
2095 continue;
2096 }
2097 args->color_fixup[i] = texture->resource.format_desc->color_fixup;
2098
2099 /* Flag samplers that need NP2 texcoord fixup. */
2100 if (!texture->baseTexture.pow2Matrix_identity)
2101 {
2102 args->np2_fixup |= (1 << i);
2103 }
2104
2105 if (texture->baseTexture.t_mirror)
2106 {
2107 args->t_mirror |= (1 << i);
2108 }
2109 }
2110 if (shader->baseShader.reg_maps.shader_version.major >= 3)
2111 {
2112 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed)
2113 {
2114 args->vp_mode = pretransformed;
2115 }
2116 else if (use_vs(stateblock))
2117 {
2118 args->vp_mode = vertexshader;
2119 }
2120 else
2121 {
2122 args->vp_mode = fixedfunction;
2123 }
2124 args->fog = FOG_OFF;
2125 }
2126 else
2127 {
2128 args->vp_mode = vertexshader;
2129 if (stateblock->renderState[WINED3DRS_FOGENABLE])
2130 {
2131 switch (stateblock->renderState[WINED3DRS_FOGTABLEMODE])
2132 {
2133 case WINED3DFOG_NONE:
2134 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed
2135 || use_vs(stateblock))
2136 {
2137 args->fog = FOG_LINEAR;
2138 break;
2139 }
2140
2141 switch (stateblock->renderState[WINED3DRS_FOGVERTEXMODE])
2142 {
2143 case WINED3DFOG_NONE: RT_FALL_THRU();
2144 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
2145 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
2146 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
2147 }
2148 break;
2149
2150 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
2151 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
2152 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
2153 }
2154 }
2155 else
2156 {
2157 args->fog = FOG_OFF;
2158 }
2159 }
2160}
2161
2162static void pixelshader_set_limits(IWineD3DPixelShaderImpl *shader)
2163{
2164 DWORD shader_version = WINED3D_SHADER_VERSION(shader->baseShader.reg_maps.shader_version.major,
2165 shader->baseShader.reg_maps.shader_version.minor);
2166
2167 shader->baseShader.limits.attributes = 0;
2168 shader->baseShader.limits.address = 0;
2169 shader->baseShader.limits.packed_output = 0;
2170
2171 switch (shader_version)
2172 {
2173 case WINED3D_SHADER_VERSION(1, 0):
2174 case WINED3D_SHADER_VERSION(1, 1):
2175 case WINED3D_SHADER_VERSION(1, 2):
2176 case WINED3D_SHADER_VERSION(1, 3):
2177 shader->baseShader.limits.temporary = 2;
2178 shader->baseShader.limits.constant_float = 8;
2179 shader->baseShader.limits.constant_int = 0;
2180 shader->baseShader.limits.constant_bool = 0;
2181 shader->baseShader.limits.texcoord = 4;
2182 shader->baseShader.limits.sampler = 4;
2183 shader->baseShader.limits.packed_input = 0;
2184 shader->baseShader.limits.label = 0;
2185 break;
2186
2187 case WINED3D_SHADER_VERSION(1, 4):
2188 shader->baseShader.limits.temporary = 6;
2189 shader->baseShader.limits.constant_float = 8;
2190 shader->baseShader.limits.constant_int = 0;
2191 shader->baseShader.limits.constant_bool = 0;
2192 shader->baseShader.limits.texcoord = 6;
2193 shader->baseShader.limits.sampler = 6;
2194 shader->baseShader.limits.packed_input = 0;
2195 shader->baseShader.limits.label = 0;
2196 break;
2197
2198 /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2199 case WINED3D_SHADER_VERSION(2, 0):
2200 shader->baseShader.limits.temporary = 32;
2201 shader->baseShader.limits.constant_float = 32;
2202 shader->baseShader.limits.constant_int = 16;
2203 shader->baseShader.limits.constant_bool = 16;
2204 shader->baseShader.limits.texcoord = 8;
2205 shader->baseShader.limits.sampler = 16;
2206 shader->baseShader.limits.packed_input = 0;
2207 break;
2208
2209 case WINED3D_SHADER_VERSION(2, 1):
2210 shader->baseShader.limits.temporary = 32;
2211 shader->baseShader.limits.constant_float = 32;
2212 shader->baseShader.limits.constant_int = 16;
2213 shader->baseShader.limits.constant_bool = 16;
2214 shader->baseShader.limits.texcoord = 8;
2215 shader->baseShader.limits.sampler = 16;
2216 shader->baseShader.limits.packed_input = 0;
2217 shader->baseShader.limits.label = 16;
2218 break;
2219
2220 case WINED3D_SHADER_VERSION(4, 0):
2221 FIXME("Using 3.0 limits for 4.0 shader.\n");
2222 RT_FALL_THRU();
2223
2224 case WINED3D_SHADER_VERSION(3, 0):
2225 shader->baseShader.limits.temporary = 32;
2226 shader->baseShader.limits.constant_float = 224;
2227 shader->baseShader.limits.constant_int = 16;
2228 shader->baseShader.limits.constant_bool = 16;
2229 shader->baseShader.limits.texcoord = 0;
2230 shader->baseShader.limits.sampler = 16;
2231 shader->baseShader.limits.packed_input = 12;
2232 shader->baseShader.limits.label = 16; /* FIXME: 2048 */
2233 break;
2234
2235 default:
2236 shader->baseShader.limits.temporary = 32;
2237 shader->baseShader.limits.constant_float = 32;
2238 shader->baseShader.limits.constant_int = 16;
2239 shader->baseShader.limits.constant_bool = 16;
2240 shader->baseShader.limits.texcoord = 8;
2241 shader->baseShader.limits.sampler = 16;
2242 shader->baseShader.limits.packed_input = 0;
2243 shader->baseShader.limits.label = 0;
2244 FIXME("Unrecognized pixel shader version %u.%u\n",
2245 shader->baseShader.reg_maps.shader_version.major,
2246 shader->baseShader.reg_maps.shader_version.minor);
2247 }
2248}
2249
2250HRESULT pixelshader_init(IWineD3DPixelShaderImpl *shader, IWineD3DDeviceImpl *device,
2251 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
2252 IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
2253{
2254 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2255 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
2256 HRESULT hr;
2257
2258 if (!byte_code) return WINED3DERR_INVALIDCALL;
2259
2260 shader->lpVtbl = &IWineD3DPixelShader_Vtbl;
2261 shader_init(&shader->baseShader, device, parent, parent_ops);
2262
2263 hr = shader_set_function((IWineD3DBaseShaderImpl *)shader, byte_code,
2264 output_signature, device->d3d_pshader_constantF);
2265 if (FAILED(hr))
2266 {
2267 WARN("Failed to set function, hr %#x.\n", hr);
2268 shader_cleanup((IWineD3DBaseShader *)shader);
2269 return hr;
2270 }
2271
2272 pixelshader_set_limits(shader);
2273
2274 for (i = 0; i < MAX_REG_INPUT; ++i)
2275 {
2276 if (shader->input_reg_used[i])
2277 {
2278 ++num_regs_used;
2279 highest_reg_used = i;
2280 }
2281 }
2282
2283 /* Don't do any register mapping magic if it is not needed, or if we can't
2284 * achieve anything anyway */
2285 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
2286 || num_regs_used > (gl_info->limits.glsl_varyings / 4))
2287 {
2288 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
2289 {
2290 /* This happens with relative addressing. The input mapper function
2291 * warns about this if the higher registers are declared too, so
2292 * don't write a FIXME here */
2293 WARN("More varying registers used than supported\n");
2294 }
2295
2296 for (i = 0; i < MAX_REG_INPUT; ++i)
2297 {
2298 shader->input_reg_map[i] = i;
2299 }
2300
2301 shader->declared_in_count = highest_reg_used + 1;
2302 }
2303 else
2304 {
2305 shader->declared_in_count = 0;
2306 for (i = 0; i < MAX_REG_INPUT; ++i)
2307 {
2308 if (shader->input_reg_used[i]) shader->input_reg_map[i] = shader->declared_in_count++;
2309 else shader->input_reg_map[i] = ~0U;
2310 }
2311 }
2312
2313 shader->baseShader.load_local_constsF = FALSE;
2314
2315 return WINED3D_OK;
2316}
2317
2318void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures)
2319{
2320 WINED3DSAMPLER_TEXTURE_TYPE *sampler_type = reg_maps->sampler_type;
2321 unsigned int i;
2322
2323 if (reg_maps->shader_version.major != 1) return;
2324
2325 for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
2326 {
2327 /* We don't sample from this sampler. */
2328 if (!sampler_type[i]) continue;
2329
2330 if (!textures[i])
2331 {
2332 WARN("No texture bound to sampler %u, using 2D.\n", i);
2333 sampler_type[i] = WINED3DSTT_2D;
2334 continue;
2335 }
2336
2337 switch (IWineD3DBaseTexture_GetTextureDimensions(textures[i]))
2338 {
2339 case GL_TEXTURE_RECTANGLE_ARB:
2340 case GL_TEXTURE_2D:
2341 /* We have to select between texture rectangles and 2D
2342 * textures later because 2.0 and 3.0 shaders only have
2343 * WINED3DSTT_2D as well. */
2344 sampler_type[i] = WINED3DSTT_2D;
2345 break;
2346
2347 case GL_TEXTURE_3D:
2348 sampler_type[i] = WINED3DSTT_VOLUME;
2349 break;
2350
2351 case GL_TEXTURE_CUBE_MAP_ARB:
2352 sampler_type[i] = WINED3DSTT_CUBE;
2353 break;
2354
2355 default:
2356 FIXME("Unrecognized texture type %#x, using 2D.\n",
2357 IWineD3DBaseTexture_GetTextureDimensions(textures[i]));
2358 sampler_type[i] = WINED3DSTT_2D;
2359 }
2360 }
2361}
2362
2363#ifdef VBOX_WINE_WITH_SHADER_CACHE
2364
2365static void shader_reinit(struct IWineD3DBaseShaderClass *shader, IWineD3DDeviceImpl *device,
2366 IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
2367{
2368 shader->ref = 1;
2369 shader->device = (IWineD3DDevice *)device;
2370 shader->parent = parent;
2371 shader->parent_ops = parent_ops;
2372#ifdef DEBUG
2373 {
2374 IWineD3DBaseShaderImpl *tstShader;
2375 LIST_FOR_EACH_ENTRY(tstShader, &device->shaders, IWineD3DBaseShaderImpl, baseShader.shader_list_entry) {
2376 if (&tstShader->baseShader == shader)
2377 return;
2378 }
2379 ERR("shader not in list!");
2380 }
2381#endif
2382}
2383
2384static DECLCALLBACK(uint32_t) shader_cache_hash(void *pvKey)
2385{
2386 uint32_t u32Hash, i;
2387 DWORD *function;
2388 IWineD3DBaseShaderImpl *pShader = (IWineD3DBaseShaderImpl *)pvKey;
2389 if (pShader->baseShader.u32CacheDataInited)
2390 return pShader->baseShader.u32Hash;
2391
2392 u32Hash = pShader->baseShader.functionLength;
2393 function = pShader->baseShader.function;
2394 for (i = 0; i < pShader->baseShader.functionLength / 4; ++i)
2395 {
2396 u32Hash += *function++;
2397 }
2398
2399 for (i = 0; i < pShader->baseShader.functionLength % 4; ++i)
2400 {
2401 u32Hash += *((uint8_t*)function)++;
2402 }
2403
2404 pShader->baseShader.u32Hash = u32Hash;
2405 pShader->baseShader.u32CacheDataInited = TRUE;
2406 return pShader->baseShader.u32Hash;
2407}
2408
2409static DECLCALLBACK(bool) shader_cache_equal(void *pvKey1, void *pvKey2)
2410{
2411 IWineD3DBaseShaderImpl *pShader1 = (IWineD3DBaseShaderImpl *)pvKey1;
2412 IWineD3DBaseShaderImpl *pShader2 = (IWineD3DBaseShaderImpl *)pvKey2;
2413
2414 if (pShader1 == pShader2)
2415 return TRUE;
2416
2417 if (pShader1->baseShader.functionLength != pShader2->baseShader.functionLength)
2418 return FALSE;
2419
2420 if (shader_cache_hash(pvKey1) != shader_cache_hash(pvKey2))
2421 {
2422#ifdef DEBUG_misha
2423 Assert(memcmp(pShader1->baseShader.function, pShader2->baseShader.function, pShader1->baseShader.functionLength));
2424#endif
2425 return FALSE;
2426 }
2427
2428 return !memcmp(pShader1->baseShader.function, pShader2->baseShader.function, pShader1->baseShader.functionLength);
2429}
2430
2431#define VBOX_SHADER_FROM_CACHE_ENTRY(_pEntry) RT_FROM_MEMBER(RT_FROM_MEMBER((_pEntry), IWineD3DBaseShaderClass, CacheEntry), IWineD3DBaseShaderImpl, baseShader)
2432
2433static DECLCALLBACK(void) shader_cache_cleanup_entry(void *pvKey, struct VBOXEXT_HASHCACHE_ENTRY *pEntry)
2434{
2435 IWineD3DBaseShaderImpl *pShader = VBOX_SHADER_FROM_CACHE_ENTRY(pEntry);
2436 shader_cleanup((IWineD3DBaseShader *)pShader);
2437 if (pShader->baseShader.parent)
2438 pShader->baseShader.parent_ops->wined3d_object_destroyed(pShader->baseShader.parent);
2439 HeapFree(GetProcessHeap(), 0, pShader);
2440}
2441
2442static void shader_chache_init(PVBOXEXT_HASHCACHE pCache)
2443{
2444 VBoxExtCacheInit(pCache, 1024, shader_cache_hash, shader_cache_equal, shader_cache_cleanup_entry);
2445}
2446
2447static void shader_chache_term(PVBOXEXT_HASHCACHE pCache)
2448{
2449 VBoxExtCacheTerm(pCache);
2450}
2451
2452static void shader_chache_put(PVBOXEXT_HASHCACHE pCache, IWineD3DBaseShaderImpl *pShader)
2453{
2454 if (pShader->baseShader.parent)
2455 {
2456 pShader->baseShader.parent_ops->wined3d_object_destroyed(pShader->baseShader.parent);
2457 pShader->baseShader.parent = NULL;
2458 }
2459
2460 VBoxExtCachePut(pCache, pShader, &pShader->baseShader.CacheEntry);
2461}
2462
2463static IWineD3DBaseShaderImpl* shader_chache_get(PVBOXEXT_HASHCACHE pCache, IWineD3DBaseShaderImpl *pShader)
2464{
2465 PVBOXEXT_HASHCACHE_ENTRY pEntry = VBoxExtCacheGet(pCache, pShader);
2466 IWineD3DBaseShaderImpl *pCachedShader;
2467
2468 if (!pEntry)
2469 return pShader;
2470
2471 pCachedShader = VBOX_SHADER_FROM_CACHE_ENTRY(pEntry);
2472 shader_reinit(&pCachedShader->baseShader, (IWineD3DDeviceImpl *)pShader->baseShader.device,
2473 pShader->baseShader.parent, pShader->baseShader.parent_ops);
2474 pShader->baseShader.parent = NULL;
2475 /* we can not do a IWineD3DBaseShader_Release here since this would result in putting a shader to the cache */
2476 shader_cleanup((IWineD3DBaseShader *)pShader);
2477 HeapFree(GetProcessHeap(), 0, pShader);
2478 return pCachedShader;
2479}
2480
2481IWineD3DVertexShaderImpl * vertexshader_check_cached(IWineD3DDeviceImpl *device, IWineD3DVertexShaderImpl *object)
2482{
2483 return (IWineD3DVertexShaderImpl*)shader_chache_get(&device->vshaderCache, (IWineD3DBaseShaderImpl *)object);
2484}
2485
2486IWineD3DPixelShaderImpl * pixelshader_check_cached(IWineD3DDeviceImpl *device, IWineD3DPixelShaderImpl *object)
2487{
2488 return (IWineD3DPixelShaderImpl*)shader_chache_get(&device->pshaderCache, (IWineD3DBaseShaderImpl *)object);
2489}
2490
2491void shader_chaches_init(IWineD3DDeviceImpl *device)
2492{
2493 shader_chache_init(&device->vshaderCache);
2494 shader_chache_init(&device->pshaderCache);
2495}
2496
2497void shader_chaches_term(IWineD3DDeviceImpl *device)
2498{
2499 shader_chache_term(&device->vshaderCache);
2500 shader_chache_term(&device->pshaderCache);
2501}
2502#endif
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