VirtualBox

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

最後變更 在這個檔案從75354是 69046,由 vboxsync 提交於 7 年 前

Global: replace fall-through comments with RT_FALL_THRU().
bugref:8192: gcc warnings

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 86.0 KB
 
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
503 fe->shader_read_semantic(&ptr, &semantic);
504
505 switch (semantic.reg.reg.type)
506 {
507 /* Mark input registers used. */
508 case WINED3DSPR_INPUT:
509 reg_maps->input_registers |= 1 << semantic.reg.reg.idx;
510 shader_signature_from_semantic(&input_signature[semantic.reg.reg.idx], &semantic);
511 break;
512
513 /* Vertex shader: mark 3.0 output registers used, save token. */
514 case WINED3DSPR_OUTPUT:
515 reg_maps->output_registers |= 1 << semantic.reg.reg.idx;
516 shader_signature_from_semantic(&output_signature[semantic.reg.reg.idx], &semantic);
517 if (semantic.usage == WINED3DDECLUSAGE_FOG) reg_maps->fog = 1;
518 break;
519
520 /* Save sampler usage token. */
521 case WINED3DSPR_SAMPLER:
522 reg_maps->sampler_type[semantic.reg.reg.idx] = semantic.sampler_type;
523 break;
524
525 default:
526 TRACE("Not recording DCL register type %#x.\n", semantic.reg.reg.type);
527 break;
528 }
529 }
530 else if (ins.handler_idx == WINED3DSIH_DEF)
531 {
532 struct wined3d_shader_src_param rel_addr;
533 struct wined3d_shader_dst_param dst;
534
535 local_constant *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
536 if (!lconst) return E_OUTOFMEMORY;
537
538 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
539 lconst->idx = dst.reg.idx;
540
541 memcpy(lconst->value, ptr, 4 * sizeof(DWORD));
542 ptr += 4;
543
544 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
545 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
546 {
547 float *value = (float *)lconst->value;
548 if (value[0] < -1.0f) value[0] = -1.0f;
549 else if (value[0] > 1.0f) value[0] = 1.0f;
550 if (value[1] < -1.0f) value[1] = -1.0f;
551 else if (value[1] > 1.0f) value[1] = 1.0f;
552 if (value[2] < -1.0f) value[2] = -1.0f;
553 else if (value[2] > 1.0f) value[2] = 1.0f;
554 if (value[3] < -1.0f) value[3] = -1.0f;
555 else if (value[3] > 1.0f) value[3] = 1.0f;
556 }
557
558 list_add_head(&shader->baseShader.constantsF, &lconst->entry);
559 }
560 else if (ins.handler_idx == WINED3DSIH_DEFI)
561 {
562 struct wined3d_shader_src_param rel_addr;
563 struct wined3d_shader_dst_param dst;
564
565 local_constant *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
566 if (!lconst) return E_OUTOFMEMORY;
567
568 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
569 lconst->idx = dst.reg.idx;
570
571 memcpy(lconst->value, ptr, 4 * sizeof(DWORD));
572 ptr += 4;
573
574 list_add_head(&shader->baseShader.constantsI, &lconst->entry);
575 reg_maps->local_int_consts |= (1 << dst.reg.idx);
576 }
577 else if (ins.handler_idx == WINED3DSIH_DEFB)
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, sizeof(DWORD));
589 ++ptr;
590
591 list_add_head(&shader->baseShader.constantsB, &lconst->entry);
592 reg_maps->local_bool_consts |= (1 << dst.reg.idx);
593 }
594 /* If there's a loop in the shader. */
595 else if (ins.handler_idx == WINED3DSIH_LOOP
596 || ins.handler_idx == WINED3DSIH_REP)
597 {
598 struct wined3d_shader_src_param src, rel_addr;
599
600 fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
601
602 /* Rep and Loop always use an integer constant for the control parameters. */
603 if (ins.handler_idx == WINED3DSIH_REP)
604 {
605 reg_maps->integer_constants |= 1 << src.reg.idx;
606 }
607 else
608 {
609 fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
610 reg_maps->integer_constants |= 1 << src.reg.idx;
611 }
612
613 cur_loop_depth++;
614 if (cur_loop_depth > max_loop_depth) max_loop_depth = cur_loop_depth;
615 }
616 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
617 || ins.handler_idx == WINED3DSIH_ENDREP)
618 {
619 cur_loop_depth--;
620 }
621 /* For subroutine prototypes. */
622 else if (ins.handler_idx == WINED3DSIH_LABEL)
623 {
624 struct wined3d_shader_src_param src, rel_addr;
625
626 fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
627 reg_maps->labels |= 1 << src.reg.idx;
628 }
629 /* Set texture, address, temporary registers. */
630 else
631 {
632 BOOL color0_mov = FALSE;
633 int i, limit;
634
635 /* This will loop over all the registers and try to
636 * make a bitmask of the ones we're interested in.
637 *
638 * Relative addressing tokens are ignored, but that's
639 * okay, since we'll catch any address registers when
640 * they are initialized (required by spec). */
641 if (ins.dst_count)
642 {
643 struct wined3d_shader_src_param dst_rel_addr;
644 struct wined3d_shader_dst_param dst_param;
645
646 fe->shader_read_dst_param(fe_data, &ptr, &dst_param, &dst_rel_addr);
647
648 shader_record_register_usage(shader, reg_maps, &dst_param.reg, shader_version.type);
649
650 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
651 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
652 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
653 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX && shader_version.major < 3
654 && dst_param.reg.type == WINED3DSPR_TEXCRDOUT)
655 {
656 reg_maps->texcoord_mask[dst_param.reg.idx] |= dst_param.write_mask;
657 }
658
659 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
660 {
661 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *)shader;
662
663 if (dst_param.reg.type == WINED3DSPR_COLOROUT && dst_param.reg.idx == 0)
664 {
665 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
666 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
667 * the mov and perform the sRGB write correction from the source register.
668 *
669 * However, if the mov is only partial, we can't do this, and if the write
670 * comes from an instruction other than MOV it is hard to do as well. If
671 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
672
673 ps->color0_mov = FALSE;
674 if (ins.handler_idx == WINED3DSIH_MOV)
675 {
676 /* Used later when the source register is read. */
677 color0_mov = TRUE;
678 }
679 }
680 /* Also drop the MOV marker if the source register is overwritten prior to the shader
681 * end
682 */
683 else if (dst_param.reg.type == WINED3DSPR_TEMP && dst_param.reg.idx == ps->color0_reg)
684 {
685 ps->color0_mov = FALSE;
686 }
687 }
688
689 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
690 if (shader_version.major == 1
691 && (ins.handler_idx == WINED3DSIH_TEX
692 || ins.handler_idx == WINED3DSIH_TEXBEM
693 || ins.handler_idx == WINED3DSIH_TEXBEML
694 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
695 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
696 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
697 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
698 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
699 || ins.handler_idx == WINED3DSIH_TEXREG2AR
700 || ins.handler_idx == WINED3DSIH_TEXREG2GB
701 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
702 {
703 /* Fake sampler usage, only set reserved bit and type. */
704 DWORD sampler_code = dst_param.reg.idx;
705
706 TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
707 reg_maps->sampler_type[sampler_code] = WINED3DSTT_2D;
708
709 /* texbem is only valid with < 1.4 pixel shaders */
710 if (ins.handler_idx == WINED3DSIH_TEXBEM
711 || ins.handler_idx == WINED3DSIH_TEXBEML)
712 {
713 reg_maps->bumpmat |= 1 << dst_param.reg.idx;
714 if (ins.handler_idx == WINED3DSIH_TEXBEML)
715 {
716 reg_maps->luminanceparams |= 1 << dst_param.reg.idx;
717 }
718 }
719 }
720 else if (ins.handler_idx == WINED3DSIH_BEM)
721 {
722 reg_maps->bumpmat |= 1 << dst_param.reg.idx;
723 }
724 }
725
726 if (ins.handler_idx == WINED3DSIH_NRM) reg_maps->usesnrm = 1;
727 else if (ins.handler_idx == WINED3DSIH_DSY) reg_maps->usesdsy = 1;
728 else if (ins.handler_idx == WINED3DSIH_DSX) reg_maps->usesdsx = 1;
729 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
730 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
731 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
732 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
733 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
734
735 limit = ins.src_count + (ins.predicate ? 1 : 0);
736 for (i = 0; i < limit; ++i)
737 {
738 struct wined3d_shader_src_param src_param, src_rel_addr;
739 unsigned int count;
740
741 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
742 count = get_instr_extra_regcount(ins.handler_idx, i);
743
744 shader_record_register_usage(shader, reg_maps, &src_param.reg, shader_version.type);
745 while (count)
746 {
747 ++src_param.reg.idx;
748 shader_record_register_usage(shader, reg_maps, &src_param.reg, shader_version.type);
749 --count;
750 }
751
752 if (color0_mov)
753 {
754 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *)shader;
755 if (src_param.reg.type == WINED3DSPR_TEMP
756 && src_param.swizzle == WINED3DSP_NOSWIZZLE)
757 {
758 ps->color0_mov = TRUE;
759 ps->color0_reg = src_param.reg.idx;
760 }
761 }
762 }
763 }
764 }
765 reg_maps->loop_depth = max_loop_depth;
766
767 shader->baseShader.functionLength = ((const char *)ptr - (const char *)byte_code);
768
769 return WINED3D_OK;
770}
771
772unsigned int shader_find_free_input_register(const struct shader_reg_maps *reg_maps, unsigned int max)
773{
774 DWORD map = 1 << max;
775 map |= map - 1;
776 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
777
778 return wined3d_log2i(map);
779}
780
781static void shader_dump_decl_usage(const struct wined3d_shader_semantic *semantic,
782 const struct wined3d_shader_version *shader_version)
783{
784 TRACE("dcl");
785
786 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
787 {
788 switch (semantic->sampler_type)
789 {
790 case WINED3DSTT_2D: TRACE("_2d"); break;
791 case WINED3DSTT_CUBE: TRACE("_cube"); break;
792 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
793 default: TRACE("_unknown_ttype(0x%08x)", semantic->sampler_type);
794 }
795 }
796 else
797 {
798 /* Pixel shaders 3.0 don't have usage semantics. */
799 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL) return;
800 else TRACE("_");
801
802 switch (semantic->usage)
803 {
804 case WINED3DDECLUSAGE_POSITION:
805 TRACE("position%u", semantic->usage_idx);
806 break;
807
808 case WINED3DDECLUSAGE_BLENDINDICES:
809 TRACE("blend");
810 break;
811
812 case WINED3DDECLUSAGE_BLENDWEIGHT:
813 TRACE("weight");
814 break;
815
816 case WINED3DDECLUSAGE_NORMAL:
817 TRACE("normal%u", semantic->usage_idx);
818 break;
819
820 case WINED3DDECLUSAGE_PSIZE:
821 TRACE("psize");
822 break;
823
824 case WINED3DDECLUSAGE_COLOR:
825 if (semantic->usage_idx == 0) TRACE("color");
826 else TRACE("specular%u", (semantic->usage_idx - 1));
827 break;
828
829 case WINED3DDECLUSAGE_TEXCOORD:
830 TRACE("texture%u", semantic->usage_idx);
831 break;
832
833 case WINED3DDECLUSAGE_TANGENT:
834 TRACE("tangent");
835 break;
836
837 case WINED3DDECLUSAGE_BINORMAL:
838 TRACE("binormal");
839 break;
840
841 case WINED3DDECLUSAGE_TESSFACTOR:
842 TRACE("tessfactor");
843 break;
844
845 case WINED3DDECLUSAGE_POSITIONT:
846 TRACE("positionT%u", semantic->usage_idx);
847 break;
848
849 case WINED3DDECLUSAGE_FOG:
850 TRACE("fog");
851 break;
852
853 case WINED3DDECLUSAGE_DEPTH:
854 TRACE("depth");
855 break;
856
857 case WINED3DDECLUSAGE_SAMPLE:
858 TRACE("sample");
859 break;
860
861 default:
862 FIXME("unknown_semantics(0x%08x)", semantic->usage);
863 }
864 }
865}
866
867static void shader_dump_register(const struct wined3d_shader_register *reg,
868 const struct wined3d_shader_version *shader_version)
869{
870 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
871 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
872 UINT offset = reg->idx;
873
874 switch (reg->type)
875 {
876 case WINED3DSPR_TEMP:
877 TRACE("r");
878 break;
879
880 case WINED3DSPR_INPUT:
881 TRACE("v");
882 break;
883
884 case WINED3DSPR_CONST:
885 case WINED3DSPR_CONST2:
886 case WINED3DSPR_CONST3:
887 case WINED3DSPR_CONST4:
888 TRACE("c");
889 offset = shader_get_float_offset(reg->type, reg->idx);
890 break;
891
892 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
893 TRACE("%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
894 break;
895
896 case WINED3DSPR_RASTOUT:
897 TRACE("%s", rastout_reg_names[reg->idx]);
898 break;
899
900 case WINED3DSPR_COLOROUT:
901 TRACE("oC");
902 break;
903
904 case WINED3DSPR_DEPTHOUT:
905 TRACE("oDepth");
906 break;
907
908 case WINED3DSPR_ATTROUT:
909 TRACE("oD");
910 break;
911
912 case WINED3DSPR_TEXCRDOUT:
913 /* Vertex shaders >= 3.0 use general purpose output registers
914 * (WINED3DSPR_OUTPUT), which can include an address token. */
915 if (shader_version->major >= 3) TRACE("o");
916 else TRACE("oT");
917 break;
918
919 case WINED3DSPR_CONSTINT:
920 TRACE("i");
921 break;
922
923 case WINED3DSPR_CONSTBOOL:
924 TRACE("b");
925 break;
926
927 case WINED3DSPR_LABEL:
928 TRACE("l");
929 break;
930
931 case WINED3DSPR_LOOP:
932 TRACE("aL");
933 break;
934
935 case WINED3DSPR_SAMPLER:
936 TRACE("s");
937 break;
938
939 case WINED3DSPR_MISCTYPE:
940 if (reg->idx > 1) FIXME("Unhandled misctype register %u.\n", reg->idx);
941 else TRACE("%s", misctype_reg_names[reg->idx]);
942 break;
943
944 case WINED3DSPR_PREDICATE:
945 TRACE("p");
946 break;
947
948 case WINED3DSPR_IMMCONST:
949 TRACE("l");
950 break;
951
952 case WINED3DSPR_CONSTBUFFER:
953 TRACE("cb");
954 break;
955
956 default:
957 TRACE("unhandled_rtype(%#x)", reg->type);
958 break;
959 }
960
961 if (reg->type == WINED3DSPR_IMMCONST)
962 {
963 TRACE("(");
964 switch (reg->immconst_type)
965 {
966 case WINED3D_IMMCONST_FLOAT:
967 TRACE("%.8e", *(const float *)reg->immconst_data);
968 break;
969
970 case WINED3D_IMMCONST_FLOAT4:
971 TRACE("%.8e, %.8e, %.8e, %.8e",
972 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
973 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
974 break;
975
976 default:
977 TRACE("<unhandled immconst_type %#x>", reg->immconst_type);
978 break;
979 }
980 TRACE(")");
981 }
982 else if (reg->type != WINED3DSPR_RASTOUT && reg->type != WINED3DSPR_MISCTYPE)
983 {
984 if (reg->array_idx != ~0U)
985 {
986 TRACE("%u[%u", offset, reg->array_idx);
987 if (reg->rel_addr)
988 {
989 TRACE(" + ");
990 shader_dump_src_param(reg->rel_addr, shader_version);
991 }
992 TRACE("]");
993 }
994 else
995 {
996 if (reg->rel_addr)
997 {
998 TRACE("[");
999 shader_dump_src_param(reg->rel_addr, shader_version);
1000 TRACE(" + ");
1001 }
1002 TRACE("%u", offset);
1003 if (reg->rel_addr) TRACE("]");
1004 }
1005 }
1006}
1007
1008void shader_dump_dst_param(const struct wined3d_shader_dst_param *param,
1009 const struct wined3d_shader_version *shader_version)
1010{
1011 DWORD write_mask = param->write_mask;
1012
1013 shader_dump_register(&param->reg, shader_version);
1014
1015 if (write_mask != WINED3DSP_WRITEMASK_ALL)
1016 {
1017 static const char *write_mask_chars = "xyzw";
1018
1019 TRACE(".");
1020 if (write_mask & WINED3DSP_WRITEMASK_0) TRACE("%c", write_mask_chars[0]);
1021 if (write_mask & WINED3DSP_WRITEMASK_1) TRACE("%c", write_mask_chars[1]);
1022 if (write_mask & WINED3DSP_WRITEMASK_2) TRACE("%c", write_mask_chars[2]);
1023 if (write_mask & WINED3DSP_WRITEMASK_3) TRACE("%c", write_mask_chars[3]);
1024 }
1025}
1026
1027void shader_dump_src_param(const struct wined3d_shader_src_param *param,
1028 const struct wined3d_shader_version *shader_version)
1029{
1030 DWORD src_modifier = param->modifiers;
1031 DWORD swizzle = param->swizzle;
1032
1033 if (src_modifier == WINED3DSPSM_NEG
1034 || src_modifier == WINED3DSPSM_BIASNEG
1035 || src_modifier == WINED3DSPSM_SIGNNEG
1036 || src_modifier == WINED3DSPSM_X2NEG
1037 || src_modifier == WINED3DSPSM_ABSNEG)
1038 TRACE("-");
1039 else if (src_modifier == WINED3DSPSM_COMP)
1040 TRACE("1-");
1041 else if (src_modifier == WINED3DSPSM_NOT)
1042 TRACE("!");
1043
1044 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
1045 TRACE("abs(");
1046
1047 shader_dump_register(&param->reg, shader_version);
1048
1049 if (src_modifier)
1050 {
1051 switch (src_modifier)
1052 {
1053 case WINED3DSPSM_NONE: break;
1054 case WINED3DSPSM_NEG: break;
1055 case WINED3DSPSM_NOT: break;
1056 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
1057 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
1058 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
1059 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
1060 case WINED3DSPSM_COMP: break;
1061 case WINED3DSPSM_X2: TRACE("_x2"); break;
1062 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
1063 case WINED3DSPSM_DZ: TRACE("_dz"); break;
1064 case WINED3DSPSM_DW: TRACE("_dw"); break;
1065 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
1066 case WINED3DSPSM_ABS: TRACE(")"); break;
1067 default: TRACE("_unknown_modifier(%#x)", src_modifier);
1068 }
1069 }
1070
1071 if (swizzle != WINED3DSP_NOSWIZZLE)
1072 {
1073 static const char *swizzle_chars = "xyzw";
1074 DWORD swizzle_x = swizzle & 0x03;
1075 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1076 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1077 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1078
1079 if (swizzle_x == swizzle_y
1080 && swizzle_x == swizzle_z
1081 && swizzle_x == swizzle_w)
1082 {
1083 TRACE(".%c", swizzle_chars[swizzle_x]);
1084 }
1085 else
1086 {
1087 TRACE(".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
1088 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
1089 }
1090 }
1091}
1092
1093/* Shared code in order to generate the bulk of the shader string.
1094 * NOTE: A description of how to parse tokens can be found on MSDN. */
1095void shader_generate_main(IWineD3DBaseShader *iface, struct wined3d_shader_buffer *buffer,
1096 const shader_reg_maps *reg_maps, const DWORD *byte_code, void *backend_ctx)
1097{
1098 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
1099 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)shader->baseShader.device;
1100 const struct wined3d_shader_frontend *fe = shader->baseShader.frontend;
1101 void *fe_data = shader->baseShader.frontend_data;
1102 struct wined3d_shader_src_param src_rel_addr[4];
1103 struct wined3d_shader_src_param src_param[4];
1104 struct wined3d_shader_version shader_version;
1105 struct wined3d_shader_src_param dst_rel_addr;
1106 struct wined3d_shader_dst_param dst_param;
1107 struct wined3d_shader_instruction ins;
1108 struct wined3d_shader_context ctx;
1109 const DWORD *ptr = byte_code;
1110 DWORD i;
1111
1112 /* Initialize current parsing state. */
1113 ctx.shader = iface;
1114 ctx.gl_info = &device->adapter->gl_info;
1115 ctx.reg_maps = reg_maps;
1116 ctx.buffer = buffer;
1117 ctx.backend_data = backend_ctx;
1118
1119 ins.ctx = &ctx;
1120 ins.dst = &dst_param;
1121 ins.src = src_param;
1122 shader->baseShader.parse_state.current_row = 0;
1123
1124 fe->shader_read_header(fe_data, &ptr, &shader_version);
1125
1126 while (!fe->shader_is_end(fe_data, &ptr))
1127 {
1128 const char *comment;
1129 UINT comment_size;
1130 UINT param_size;
1131
1132 /* Skip comment tokens. */
1133 fe->shader_read_comment(&ptr, &comment, &comment_size);
1134 if (comment) continue;
1135
1136 /* Read opcode. */
1137 fe->shader_read_opcode(fe_data, &ptr, &ins, &param_size);
1138
1139 /* Unknown opcode and its parameters. */
1140 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1141 {
1142 TRACE("Skipping unrecognized instruction.\n");
1143 ptr += param_size;
1144 continue;
1145 }
1146
1147 /* Nothing to do. */
1148 if (ins.handler_idx == WINED3DSIH_DCL
1149 || ins.handler_idx == WINED3DSIH_NOP
1150 || ins.handler_idx == WINED3DSIH_DEF
1151 || ins.handler_idx == WINED3DSIH_DEFI
1152 || ins.handler_idx == WINED3DSIH_DEFB
1153 || ins.handler_idx == WINED3DSIH_PHASE)
1154 {
1155 ptr += param_size;
1156 continue;
1157 }
1158
1159 /* Destination token */
1160 if (ins.dst_count) fe->shader_read_dst_param(fe_data, &ptr, &dst_param, &dst_rel_addr);
1161
1162 /* Predication token */
1163 if (ins.predicate)
1164 {
1165#ifndef VBOX_WITH_VMSVGA
1166 FIXME("Predicates not implemented.\n");
1167#endif
1168 ins.predicate = *ptr++;
1169 }
1170
1171 /* Other source tokens */
1172 for (i = 0; i < ins.src_count; ++i)
1173 {
1174 fe->shader_read_src_param(fe_data, &ptr, &src_param[i], &src_rel_addr[i]);
1175 }
1176
1177 /* Call appropriate function for output target */
1178 device->shader_backend->shader_handle_instruction(&ins);
1179 }
1180}
1181
1182static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param *dst)
1183{
1184 DWORD mmask = dst->modifiers;
1185
1186 switch (dst->shift)
1187 {
1188 case 0: break;
1189 case 13: TRACE("_d8"); break;
1190 case 14: TRACE("_d4"); break;
1191 case 15: TRACE("_d2"); break;
1192 case 1: TRACE("_x2"); break;
1193 case 2: TRACE("_x4"); break;
1194 case 3: TRACE("_x8"); break;
1195 default: TRACE("_unhandled_shift(%d)", dst->shift); break;
1196 }
1197
1198 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
1199 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
1200 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
1201
1202 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
1203 if (mmask) FIXME("_unrecognized_modifier(%#x)", mmask);
1204}
1205
1206static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *byte_code)
1207{
1208 struct wined3d_shader_version shader_version;
1209 const DWORD *ptr = byte_code;
1210 const char *type_prefix;
1211 DWORD i;
1212
1213 TRACE("Parsing %p.\n", byte_code);
1214
1215 fe->shader_read_header(fe_data, &ptr, &shader_version);
1216
1217 switch (shader_version.type)
1218 {
1219 case WINED3D_SHADER_TYPE_VERTEX:
1220 type_prefix = "vs";
1221 break;
1222
1223 case WINED3D_SHADER_TYPE_GEOMETRY:
1224 type_prefix = "gs";
1225 break;
1226
1227 case WINED3D_SHADER_TYPE_PIXEL:
1228 type_prefix = "ps";
1229 break;
1230
1231 default:
1232 FIXME("Unhandled shader type %#x.\n", shader_version.type);
1233 type_prefix = "unknown";
1234 break;
1235 }
1236
1237 TRACE("%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
1238
1239 while (!fe->shader_is_end(fe_data, &ptr))
1240 {
1241 struct wined3d_shader_instruction ins;
1242 const char *comment;
1243 UINT comment_size;
1244 UINT param_size;
1245
1246 /* comment */
1247 fe->shader_read_comment(&ptr, &comment, &comment_size);
1248 if (comment)
1249 {
1250 if (comment_size > 4 && *(const DWORD *)comment == WINEMAKEFOURCC('T', 'E', 'X', 'T'))
1251 {
1252 const char *end = comment + comment_size;
1253 const char *cur = comment + 4;
1254 const char *line = cur;
1255
1256 TRACE("// TEXT\n");
1257 while (cur != end)
1258 {
1259 if (*cur == '\n')
1260 {
1261 UINT len = cur - line;
1262 if (len && *(cur - 1) == '\r') --len;
1263 TRACE("// %s\n", debugstr_an(line, len));
1264 line = ++cur;
1265 }
1266 else ++cur;
1267 }
1268 if (line != cur) TRACE("// %s\n", debugstr_an(line, cur - line));
1269 }
1270 else TRACE("// %s\n", debugstr_an(comment, comment_size));
1271 continue;
1272 }
1273
1274 fe->shader_read_opcode(fe_data, &ptr, &ins, &param_size);
1275 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1276 {
1277 TRACE("Skipping unrecognized instruction.\n");
1278 ptr += param_size;
1279 continue;
1280 }
1281
1282 if (ins.handler_idx == WINED3DSIH_DCL)
1283 {
1284 struct wined3d_shader_semantic semantic;
1285
1286 fe->shader_read_semantic(&ptr, &semantic);
1287
1288 shader_dump_decl_usage(&semantic, &shader_version);
1289 shader_dump_ins_modifiers(&semantic.reg);
1290 TRACE(" ");
1291 shader_dump_dst_param(&semantic.reg, &shader_version);
1292 }
1293 else if (ins.handler_idx == WINED3DSIH_DEF)
1294 {
1295 struct wined3d_shader_src_param rel_addr;
1296 struct wined3d_shader_dst_param dst;
1297
1298 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1299
1300 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(dst.reg.type, dst.reg.idx),
1301 *(const float *)(ptr),
1302 *(const float *)(ptr + 1),
1303 *(const float *)(ptr + 2),
1304 *(const float *)(ptr + 3));
1305 ptr += 4;
1306 }
1307 else if (ins.handler_idx == WINED3DSIH_DEFI)
1308 {
1309 struct wined3d_shader_src_param rel_addr;
1310 struct wined3d_shader_dst_param dst;
1311
1312 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1313
1314 TRACE("defi i%u = %d, %d, %d, %d", dst.reg.idx,
1315 *(ptr),
1316 *(ptr + 1),
1317 *(ptr + 2),
1318 *(ptr + 3));
1319 ptr += 4;
1320 }
1321 else if (ins.handler_idx == WINED3DSIH_DEFB)
1322 {
1323 struct wined3d_shader_src_param rel_addr;
1324 struct wined3d_shader_dst_param dst;
1325
1326 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1327
1328 TRACE("defb b%u = %s", dst.reg.idx, *ptr ? "true" : "false");
1329 ++ptr;
1330 }
1331 else
1332 {
1333 struct wined3d_shader_src_param dst_rel_addr, src_rel_addr;
1334 struct wined3d_shader_dst_param dst_param;
1335 struct wined3d_shader_src_param src_param;
1336
1337 if (ins.dst_count)
1338 {
1339 fe->shader_read_dst_param(fe_data, &ptr, &dst_param, &dst_rel_addr);
1340 }
1341
1342 /* Print out predication source token first - it follows
1343 * the destination token. */
1344 if (ins.predicate)
1345 {
1346 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
1347 TRACE("(");
1348 shader_dump_src_param(&src_param, &shader_version);
1349 TRACE(") ");
1350 }
1351
1352 /* PixWin marks instructions with the coissue flag with a '+' */
1353 if (ins.coissue) TRACE("+");
1354
1355 TRACE("%s", shader_opcode_names[ins.handler_idx]);
1356
1357 if (ins.handler_idx == WINED3DSIH_IFC
1358 || ins.handler_idx == WINED3DSIH_BREAKC)
1359 {
1360 switch (ins.flags)
1361 {
1362 case COMPARISON_GT: TRACE("_gt"); break;
1363 case COMPARISON_EQ: TRACE("_eq"); break;
1364 case COMPARISON_GE: TRACE("_ge"); break;
1365 case COMPARISON_LT: TRACE("_lt"); break;
1366 case COMPARISON_NE: TRACE("_ne"); break;
1367 case COMPARISON_LE: TRACE("_le"); break;
1368 default: TRACE("_(%u)", ins.flags);
1369 }
1370 }
1371 else if (ins.handler_idx == WINED3DSIH_TEX
1372 && shader_version.major >= 2
1373 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
1374 {
1375 TRACE("p");
1376 }
1377
1378 /* We already read the destination token, print it. */
1379 if (ins.dst_count)
1380 {
1381 shader_dump_ins_modifiers(&dst_param);
1382 TRACE(" ");
1383 shader_dump_dst_param(&dst_param, &shader_version);
1384 }
1385
1386 /* Other source tokens */
1387 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
1388 {
1389 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
1390 TRACE(!i ? " " : ", ");
1391 shader_dump_src_param(&src_param, &shader_version);
1392 }
1393 }
1394 TRACE("\n");
1395 }
1396}
1397
1398static void shader_cleanup(IWineD3DBaseShader *iface)
1399{
1400 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
1401
1402 ((IWineD3DDeviceImpl *)shader->baseShader.device)->shader_backend->shader_destroy(iface);
1403 HeapFree(GetProcessHeap(), 0, shader->baseShader.reg_maps.constf);
1404 HeapFree(GetProcessHeap(), 0, shader->baseShader.function);
1405 shader_delete_constant_list(&shader->baseShader.constantsF);
1406 shader_delete_constant_list(&shader->baseShader.constantsB);
1407 shader_delete_constant_list(&shader->baseShader.constantsI);
1408 list_remove(&shader->baseShader.shader_list_entry);
1409
1410 if (shader->baseShader.frontend && shader->baseShader.frontend_data)
1411 {
1412 shader->baseShader.frontend->shader_free(shader->baseShader.frontend_data);
1413 }
1414}
1415
1416static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
1417static void shader_none_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS) {}
1418static void shader_none_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {}
1419static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1420static void shader_none_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1421static void shader_none_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1422static void shader_none_load_constants(const struct wined3d_context *context, char usePS, char useVS) {}
1423static void shader_none_load_np2fixup_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1424static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1425static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1426static void shader_none_free(IWineD3DDevice *iface) {}
1427static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1428
1429static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
1430{
1431 /* Set the shader caps to 0 for the none shader backend */
1432 caps->VertexShaderVersion = 0;
1433 caps->PixelShaderVersion = 0;
1434 caps->PixelShader1xMaxValue = 0.0f;
1435}
1436
1437static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1438{
1439 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1440 {
1441 TRACE("Checking support for fixup:\n");
1442 dump_color_fixup_desc(fixup);
1443 }
1444
1445 /* Faked to make some apps happy. */
1446 if (!is_complex_fixup(fixup))
1447 {
1448 TRACE("[OK]\n");
1449 return TRUE;
1450 }
1451
1452 TRACE("[FAILED]\n");
1453 return FALSE;
1454}
1455
1456const shader_backend_t none_shader_backend = {
1457 shader_none_handle_instruction,
1458 shader_none_select,
1459 shader_none_select_depth_blt,
1460 shader_none_deselect_depth_blt,
1461 shader_none_update_float_vertex_constants,
1462 shader_none_update_float_pixel_constants,
1463 shader_none_load_constants,
1464 shader_none_load_np2fixup_constants,
1465 shader_none_destroy,
1466 shader_none_alloc,
1467 shader_none_free,
1468 shader_none_dirty_const,
1469 shader_none_get_caps,
1470 shader_none_color_fixup_supported,
1471};
1472
1473static void shader_get_parent(IWineD3DBaseShaderImpl *shader, IUnknown **parent)
1474{
1475 *parent = shader->baseShader.parent;
1476 IUnknown_AddRef(*parent);
1477 TRACE("shader %p, returning %p.\n", shader, *parent);
1478}
1479
1480static HRESULT shader_get_function(IWineD3DBaseShaderImpl *shader, void *data, UINT *data_size)
1481{
1482 if (!data)
1483 {
1484 *data_size = shader->baseShader.functionLength;
1485 return WINED3D_OK;
1486 }
1487
1488 if (*data_size < shader->baseShader.functionLength)
1489 {
1490 /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
1491 * than the required size we should write the required size and
1492 * return D3DERR_MOREDATA. That's not actually true. */
1493 return WINED3DERR_INVALIDCALL;
1494 }
1495
1496 memcpy(data, shader->baseShader.function, shader->baseShader.functionLength);
1497
1498 return WINED3D_OK;
1499}
1500
1501static HRESULT shader_set_function(IWineD3DBaseShaderImpl *shader, const DWORD *byte_code,
1502 const struct wined3d_shader_signature *output_signature, DWORD float_const_count)
1503{
1504 struct shader_reg_maps *reg_maps = &shader->baseShader.reg_maps;
1505 const struct wined3d_shader_frontend *fe;
1506 HRESULT hr;
1507
1508 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1509 shader, byte_code, output_signature, float_const_count);
1510
1511 fe = shader_select_frontend(*byte_code);
1512 if (!fe)
1513 {
1514 FIXME("Unable to find frontend for shader.\n");
1515 return WINED3DERR_INVALIDCALL;
1516 }
1517 shader->baseShader.frontend = fe;
1518 shader->baseShader.frontend_data = fe->shader_init(byte_code, output_signature);
1519 if (!shader->baseShader.frontend_data)
1520 {
1521 FIXME("Failed to initialize frontend.\n");
1522 return WINED3DERR_INVALIDCALL;
1523 }
1524
1525 /* First pass: trace shader. */
1526 if (TRACE_ON(d3d_shader)) shader_trace_init(fe, shader->baseShader.frontend_data, byte_code);
1527
1528 /* Initialize immediate constant lists. */
1529 list_init(&shader->baseShader.constantsF);
1530 list_init(&shader->baseShader.constantsB);
1531 list_init(&shader->baseShader.constantsI);
1532
1533 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1534 hr = shader_get_registers_used((IWineD3DBaseShader *)shader, fe,
1535 reg_maps, shader->baseShader.input_signature, shader->baseShader.output_signature,
1536 byte_code, float_const_count);
1537 if (FAILED(hr)) return hr;
1538
1539 shader->baseShader.function = HeapAlloc(GetProcessHeap(), 0, shader->baseShader.functionLength);
1540 if (!shader->baseShader.function) return E_OUTOFMEMORY;
1541 memcpy(shader->baseShader.function, byte_code, shader->baseShader.functionLength);
1542
1543 return WINED3D_OK;
1544}
1545
1546static HRESULT STDMETHODCALLTYPE vertexshader_QueryInterface(IWineD3DVertexShader *iface, REFIID riid, void **object)
1547{
1548 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1549
1550 if (IsEqualGUID(riid, &IID_IWineD3DVertexShader)
1551 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1552 || IsEqualGUID(riid, &IID_IWineD3DBase)
1553 || IsEqualGUID(riid, &IID_IUnknown))
1554 {
1555 IUnknown_AddRef(iface);
1556 *object = iface;
1557 return S_OK;
1558 }
1559
1560 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1561
1562 *object = NULL;
1563 return E_NOINTERFACE;
1564}
1565
1566static ULONG STDMETHODCALLTYPE vertexshader_AddRef(IWineD3DVertexShader *iface)
1567{
1568 IWineD3DVertexShaderImpl *shader = (IWineD3DVertexShaderImpl *)iface;
1569 ULONG refcount = InterlockedIncrement(&shader->baseShader.ref);
1570
1571 TRACE("%p increasing refcount to %u.\n", shader, refcount);
1572
1573 return refcount;
1574}
1575
1576#ifdef VBOX_WINE_WITH_SHADER_CACHE
1577static void shader_chache_put(PVBOXEXT_HASHCACHE pCache, IWineD3DBaseShaderImpl *pShader);
1578#endif
1579
1580static ULONG STDMETHODCALLTYPE vertexshader_Release(IWineD3DVertexShader *iface)
1581{
1582 IWineD3DVertexShaderImpl *shader = (IWineD3DVertexShaderImpl *)iface;
1583 ULONG refcount = InterlockedDecrement(&shader->baseShader.ref);
1584
1585 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1586
1587 if (!refcount)
1588 {
1589#ifdef VBOX_WINE_WITH_SHADER_CACHE
1590 shader_chache_put(&((IWineD3DDeviceImpl*)shader->baseShader.device)->vshaderCache, (IWineD3DBaseShaderImpl*)shader);
1591#else
1592 shader_cleanup((IWineD3DBaseShader *)iface);
1593#ifndef VBOX_WITH_VMSVGA
1594 shader->baseShader.parent_ops->wined3d_object_destroyed(shader->baseShader.parent);
1595#endif
1596 HeapFree(GetProcessHeap(), 0, shader);
1597#endif
1598 }
1599
1600 return refcount;
1601}
1602
1603static HRESULT STDMETHODCALLTYPE vertexshader_GetParent(IWineD3DVertexShader *iface, IUnknown **parent)
1604{
1605 TRACE("iface %p, parent %p.\n", iface, parent);
1606
1607 shader_get_parent((IWineD3DBaseShaderImpl *)iface, parent);
1608
1609 return WINED3D_OK;
1610}
1611
1612static HRESULT STDMETHODCALLTYPE vertexshader_GetFunction(IWineD3DVertexShader *iface, void *data, UINT *data_size)
1613{
1614 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
1615
1616 return shader_get_function((IWineD3DBaseShaderImpl *)iface, data, data_size);
1617}
1618
1619/* Set local constants for d3d8 shaders. */
1620static HRESULT STDMETHODCALLTYPE vertexshader_SetLocalConstantsF(IWineD3DVertexShader *iface,
1621 UINT start_idx, const float *src_data, UINT count)
1622{
1623 IWineD3DVertexShaderImpl *shader =(IWineD3DVertexShaderImpl *)iface;
1624 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)shader->baseShader.device;
1625 UINT i, end_idx;
1626
1627 TRACE("iface %p, start_idx %u, src_data %p, count %u.\n", iface, start_idx, src_data, count);
1628
1629 end_idx = start_idx + count;
1630 if (end_idx > device->d3d_vshader_constantF)
1631 {
1632 WARN("end_idx %u > float constants limit %u.\n", end_idx, device->d3d_vshader_constantF);
1633 end_idx = device->d3d_vshader_constantF;
1634 }
1635
1636 for (i = start_idx; i < end_idx; ++i)
1637 {
1638 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
1639 if (!lconst) return E_OUTOFMEMORY;
1640
1641 lconst->idx = i;
1642 memcpy(lconst->value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
1643 list_add_head(&shader->baseShader.constantsF, &lconst->entry);
1644 }
1645
1646 return WINED3D_OK;
1647}
1648
1649static const IWineD3DVertexShaderVtbl IWineD3DVertexShader_Vtbl =
1650{
1651 /* IUnknown methods */
1652 vertexshader_QueryInterface,
1653 vertexshader_AddRef,
1654 vertexshader_Release,
1655 /* IWineD3DBase methods */
1656 vertexshader_GetParent,
1657 /* IWineD3DBaseShader methods */
1658 vertexshader_GetFunction,
1659 /* IWineD3DVertexShader methods */
1660 vertexshader_SetLocalConstantsF,
1661};
1662
1663void find_vs_compile_args(IWineD3DVertexShaderImpl *shader,
1664 IWineD3DStateBlockImpl *stateblock, struct vs_compile_args *args)
1665{
1666 args->fog_src = stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
1667 args->clip_enabled = stateblock->renderState[WINED3DRS_CLIPPING]
1668 && stateblock->renderState[WINED3DRS_CLIPPLANEENABLE];
1669 args->swizzle_map = ((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.swizzle_map;
1670}
1671
1672static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
1673{
1674 if (usage_idx1 != usage_idx2) return FALSE;
1675 if (usage1 == usage2) return TRUE;
1676 if (usage1 == WINED3DDECLUSAGE_POSITION && usage2 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
1677 if (usage2 == WINED3DDECLUSAGE_POSITION && usage1 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
1678
1679 return FALSE;
1680}
1681
1682BOOL vshader_get_input(IWineD3DVertexShader *iface, BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
1683{
1684 IWineD3DVertexShaderImpl *shader = (IWineD3DVertexShaderImpl *)iface;
1685 WORD map = shader->baseShader.reg_maps.input_registers;
1686 unsigned int i;
1687
1688 for (i = 0; map; map >>= 1, ++i)
1689 {
1690 if (!(map & 1)) continue;
1691
1692 if (match_usage(shader->attributes[i].usage,
1693 shader->attributes[i].usage_idx, usage_req, usage_idx_req))
1694 {
1695 *regnum = i;
1696 return TRUE;
1697 }
1698 }
1699 return FALSE;
1700}
1701
1702static void vertexshader_set_limits(IWineD3DVertexShaderImpl *shader)
1703{
1704 DWORD shader_version = WINED3D_SHADER_VERSION(shader->baseShader.reg_maps.shader_version.major,
1705 shader->baseShader.reg_maps.shader_version.minor);
1706 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)shader->baseShader.device;
1707
1708 shader->baseShader.limits.texcoord = 0;
1709 shader->baseShader.limits.attributes = 16;
1710 shader->baseShader.limits.packed_input = 0;
1711
1712 switch (shader_version)
1713 {
1714 case WINED3D_SHADER_VERSION(1, 0):
1715 case WINED3D_SHADER_VERSION(1, 1):
1716 shader->baseShader.limits.temporary = 12;
1717 shader->baseShader.limits.constant_bool = 0;
1718 shader->baseShader.limits.constant_int = 0;
1719 shader->baseShader.limits.address = 1;
1720 shader->baseShader.limits.packed_output = 0;
1721 shader->baseShader.limits.sampler = 0;
1722 shader->baseShader.limits.label = 0;
1723 /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1724 * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1725 * constants? */
1726 shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1727 break;
1728
1729 case WINED3D_SHADER_VERSION(2, 0):
1730 case WINED3D_SHADER_VERSION(2, 1):
1731 shader->baseShader.limits.temporary = 12;
1732 shader->baseShader.limits.constant_bool = 16;
1733 shader->baseShader.limits.constant_int = 16;
1734 shader->baseShader.limits.address = 1;
1735 shader->baseShader.limits.packed_output = 0;
1736 shader->baseShader.limits.sampler = 0;
1737 shader->baseShader.limits.label = 16;
1738 shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1739 break;
1740
1741 case WINED3D_SHADER_VERSION(4, 0):
1742 FIXME("Using 3.0 limits for 4.0 shader.\n");
1743 RT_FALL_THRU();
1744
1745 case WINED3D_SHADER_VERSION(3, 0):
1746 shader->baseShader.limits.temporary = 32;
1747 shader->baseShader.limits.constant_bool = 32;
1748 shader->baseShader.limits.constant_int = 32;
1749 shader->baseShader.limits.address = 1;
1750 shader->baseShader.limits.packed_output = 12;
1751 shader->baseShader.limits.sampler = 4;
1752 shader->baseShader.limits.label = 16; /* FIXME: 2048 */
1753 /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1754 * even though they are capable of supporting much more (GL
1755 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1756 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1757 * shaders to 256. */
1758 shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1759 break;
1760
1761 default:
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 FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1771 shader->baseShader.reg_maps.shader_version.major,
1772 shader->baseShader.reg_maps.shader_version.minor);
1773 }
1774}
1775
1776HRESULT vertexshader_init(IWineD3DVertexShaderImpl *shader, IWineD3DDeviceImpl *device,
1777 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1778 IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
1779{
1780 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1781 struct shader_reg_maps *reg_maps = &shader->baseShader.reg_maps;
1782 unsigned int i;
1783 HRESULT hr;
1784 WORD map;
1785
1786 if (!byte_code) return WINED3DERR_INVALIDCALL;
1787
1788 shader->lpVtbl = &IWineD3DVertexShader_Vtbl;
1789 shader_init(&shader->baseShader, device, parent, parent_ops);
1790
1791 hr = shader_set_function((IWineD3DBaseShaderImpl *)shader, byte_code,
1792 output_signature, device->d3d_vshader_constantF);
1793 if (FAILED(hr))
1794 {
1795 WARN("Failed to set function, hr %#x.\n", hr);
1796 shader_cleanup((IWineD3DBaseShader *)shader);
1797 return hr;
1798 }
1799
1800 map = shader->baseShader.reg_maps.input_registers;
1801 for (i = 0; map; map >>= 1, ++i)
1802 {
1803 if (!(map & 1) || !shader->baseShader.input_signature[i].semantic_name) continue;
1804
1805 shader->attributes[i].usage =
1806 shader_usage_from_semantic_name(shader->baseShader.input_signature[i].semantic_name);
1807 shader->attributes[i].usage_idx = shader->baseShader.input_signature[i].semantic_idx;
1808 }
1809
1810 if (output_signature)
1811 {
1812 for (i = 0; i < output_signature->element_count; ++i)
1813 {
1814 struct wined3d_shader_signature_element *e = &output_signature->elements[i];
1815 reg_maps->output_registers |= 1 << e->register_idx;
1816 shader->baseShader.output_signature[e->register_idx] = *e;
1817 }
1818 }
1819
1820 vertexshader_set_limits(shader);
1821
1822 if (device->vs_selected_mode == SHADER_ARB
1823 && (gl_info->quirks & WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT)
1824 && shader->min_rel_offset <= shader->max_rel_offset)
1825 {
1826 if (shader->max_rel_offset - shader->min_rel_offset > 127)
1827 {
1828 FIXME("The difference between the minimum and maximum relative offset is > 127.\n");
1829 FIXME("Which this OpenGL implementation does not support. Try using GLSL.\n");
1830 FIXME("Min: %d, Max: %d.\n", shader->min_rel_offset, shader->max_rel_offset);
1831 }
1832 else if (shader->max_rel_offset - shader->min_rel_offset > 63)
1833 {
1834 shader->rel_offset = shader->min_rel_offset + 63;
1835 }
1836 else if (shader->max_rel_offset > 63)
1837 {
1838 shader->rel_offset = shader->min_rel_offset;
1839 }
1840 else
1841 {
1842 shader->rel_offset = 0;
1843 }
1844 }
1845
1846 shader->baseShader.load_local_constsF = shader->baseShader.reg_maps.usesrelconstF
1847 && !list_empty(&shader->baseShader.constantsF);
1848
1849 return WINED3D_OK;
1850}
1851
1852static HRESULT STDMETHODCALLTYPE geometryshader_QueryInterface(IWineD3DGeometryShader *iface,
1853 REFIID riid, void **object)
1854{
1855 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1856
1857 if (IsEqualGUID(riid, &IID_IWineD3DGeometryShader)
1858 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1859 || IsEqualGUID(riid, &IID_IWineD3DBase)
1860 || IsEqualGUID(riid, &IID_IUnknown))
1861 {
1862 IUnknown_AddRef(iface);
1863 *object = iface;
1864 return S_OK;
1865 }
1866
1867 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1868
1869 *object = NULL;
1870 return E_NOINTERFACE;
1871}
1872
1873static ULONG STDMETHODCALLTYPE geometryshader_AddRef(IWineD3DGeometryShader *iface)
1874{
1875 struct wined3d_geometryshader *shader = (struct wined3d_geometryshader *)iface;
1876 ULONG refcount = InterlockedIncrement(&shader->base_shader.ref);
1877
1878 TRACE("%p increasing refcount to %u.\n", shader, refcount);
1879
1880 return refcount;
1881}
1882
1883static ULONG STDMETHODCALLTYPE geometryshader_Release(IWineD3DGeometryShader *iface)
1884{
1885 struct wined3d_geometryshader *shader = (struct wined3d_geometryshader *)iface;
1886 ULONG refcount = InterlockedDecrement(&shader->base_shader.ref);
1887
1888 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1889
1890 if (!refcount)
1891 {
1892 shader_cleanup((IWineD3DBaseShader *)iface);
1893 shader->base_shader.parent_ops->wined3d_object_destroyed(shader->base_shader.parent);
1894 HeapFree(GetProcessHeap(), 0, shader);
1895 }
1896
1897 return refcount;
1898}
1899
1900static HRESULT STDMETHODCALLTYPE geometryshader_GetParent(IWineD3DGeometryShader *iface, IUnknown **parent)
1901{
1902 TRACE("iface %p, parent %p.\n", iface, parent);
1903
1904 shader_get_parent((IWineD3DBaseShaderImpl *)iface, parent);
1905
1906 return WINED3D_OK;
1907}
1908
1909static HRESULT STDMETHODCALLTYPE geometryshader_GetFunction(IWineD3DGeometryShader *iface, void *data, UINT *data_size)
1910{
1911 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
1912
1913 return shader_get_function((IWineD3DBaseShaderImpl *)iface, data, data_size);
1914}
1915
1916static const IWineD3DGeometryShaderVtbl wined3d_geometryshader_vtbl =
1917{
1918 /* IUnknown methods */
1919 geometryshader_QueryInterface,
1920 geometryshader_AddRef,
1921 geometryshader_Release,
1922 /* IWineD3DBase methods */
1923 geometryshader_GetParent,
1924 /* IWineD3DBaseShader methods */
1925 geometryshader_GetFunction,
1926};
1927
1928HRESULT geometryshader_init(struct wined3d_geometryshader *shader, IWineD3DDeviceImpl *device,
1929 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1930 IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
1931{
1932 HRESULT hr;
1933
1934 shader->vtbl = &wined3d_geometryshader_vtbl;
1935 shader_init(&shader->base_shader, device, parent, parent_ops);
1936
1937 hr = shader_set_function((IWineD3DBaseShaderImpl *)shader, byte_code, output_signature, 0);
1938 if (FAILED(hr))
1939 {
1940 WARN("Failed to set function, hr %#x.\n", hr);
1941 shader_cleanup((IWineD3DBaseShader *)shader);
1942 return hr;
1943 }
1944
1945 shader->base_shader.load_local_constsF = FALSE;
1946
1947 return WINED3D_OK;
1948}
1949
1950static HRESULT STDMETHODCALLTYPE pixelshader_QueryInterface(IWineD3DPixelShader *iface, REFIID riid, void **object)
1951{
1952 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1953
1954 if (IsEqualGUID(riid, &IID_IWineD3DPixelShader)
1955 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1956 || IsEqualGUID(riid, &IID_IWineD3DBase)
1957 || IsEqualGUID(riid, &IID_IUnknown))
1958 {
1959 IUnknown_AddRef(iface);
1960 *object = iface;
1961 return S_OK;
1962 }
1963
1964 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1965
1966 *object = NULL;
1967 return E_NOINTERFACE;
1968}
1969
1970static ULONG STDMETHODCALLTYPE pixelshader_AddRef(IWineD3DPixelShader *iface)
1971{
1972 IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)iface;
1973 ULONG refcount = InterlockedIncrement(&shader->baseShader.ref);
1974
1975 TRACE("%p increasing refcount to %u.\n", shader, refcount);
1976
1977 return refcount;
1978}
1979
1980static ULONG STDMETHODCALLTYPE pixelshader_Release(IWineD3DPixelShader *iface)
1981{
1982 IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)iface;
1983 ULONG refcount = InterlockedDecrement(&shader->baseShader.ref);
1984
1985 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1986
1987 if (!refcount)
1988 {
1989#ifdef VBOX_WINE_WITH_SHADER_CACHE
1990 shader_chache_put(&((IWineD3DDeviceImpl*)shader->baseShader.device)->pshaderCache, (IWineD3DBaseShaderImpl*)shader);
1991#else
1992 shader_cleanup((IWineD3DBaseShader *)iface);
1993#ifndef VBOX_WITH_VMSVGA
1994 shader->baseShader.parent_ops->wined3d_object_destroyed(shader->baseShader.parent);
1995#endif
1996 HeapFree(GetProcessHeap(), 0, shader);
1997#endif
1998 }
1999
2000 return refcount;
2001}
2002
2003static HRESULT STDMETHODCALLTYPE pixelshader_GetParent(IWineD3DPixelShader *iface, IUnknown **parent)
2004{
2005 TRACE("iface %p, parent %p.\n", iface, parent);
2006
2007 shader_get_parent((IWineD3DBaseShaderImpl *)iface, parent);
2008
2009 return WINED3D_OK;
2010}
2011
2012static HRESULT STDMETHODCALLTYPE pixelshader_GetFunction(IWineD3DPixelShader *iface, void *data, UINT *data_size)
2013{
2014 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
2015
2016 return shader_get_function((IWineD3DBaseShaderImpl *)iface, data, data_size);
2017}
2018
2019static const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
2020{
2021 /* IUnknown methods */
2022 pixelshader_QueryInterface,
2023 pixelshader_AddRef,
2024 pixelshader_Release,
2025 /* IWineD3DBase methods */
2026 pixelshader_GetParent,
2027 /* IWineD3DBaseShader methods */
2028 pixelshader_GetFunction
2029};
2030
2031void find_ps_compile_args(IWineD3DPixelShaderImpl *shader,
2032 IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args)
2033{
2034 IWineD3DBaseTextureImpl *texture;
2035 UINT i;
2036
2037 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
2038 args->srgb_correction = stateblock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
2039 args->np2_fixup = 0;
2040 args->t_mirror = 0;
2041
2042 if (shader->baseShader.reg_maps.shader_version.major==1
2043 && shader->baseShader.reg_maps.shader_version.minor<=3)
2044 {
2045 for (i=0; i<4; ++i)
2046 {
2047 DWORD flags = stateblock->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2048 DWORD tex_transform = flags & ~WINED3DTTFF_PROJECTED;
2049 if (flags & WINED3DTTFF_PROJECTED)
2050 {
2051 tex_transform |= WINED3D_PSARGS_PROJECTED;
2052 }
2053 args->tex_transform |= tex_transform << (i*WINED3D_PSARGS_TEXTRANSFORM_SHIFT);
2054 }
2055 }
2056
2057 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2058 {
2059 if (!shader->baseShader.reg_maps.sampler_type[i]) continue;
2060 texture = (IWineD3DBaseTextureImpl *)stateblock->textures[i];
2061 if (!texture)
2062 {
2063 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
2064 continue;
2065 }
2066 args->color_fixup[i] = texture->resource.format_desc->color_fixup;
2067
2068 /* Flag samplers that need NP2 texcoord fixup. */
2069 if (!texture->baseTexture.pow2Matrix_identity)
2070 {
2071 args->np2_fixup |= (1 << i);
2072 }
2073
2074 if (texture->baseTexture.t_mirror)
2075 {
2076 args->t_mirror |= (1 << i);
2077 }
2078 }
2079 if (shader->baseShader.reg_maps.shader_version.major >= 3)
2080 {
2081 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed)
2082 {
2083 args->vp_mode = pretransformed;
2084 }
2085 else if (use_vs(stateblock))
2086 {
2087 args->vp_mode = vertexshader;
2088 }
2089 else
2090 {
2091 args->vp_mode = fixedfunction;
2092 }
2093 args->fog = FOG_OFF;
2094 }
2095 else
2096 {
2097 args->vp_mode = vertexshader;
2098 if (stateblock->renderState[WINED3DRS_FOGENABLE])
2099 {
2100 switch (stateblock->renderState[WINED3DRS_FOGTABLEMODE])
2101 {
2102 case WINED3DFOG_NONE:
2103 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed
2104 || use_vs(stateblock))
2105 {
2106 args->fog = FOG_LINEAR;
2107 break;
2108 }
2109
2110 switch (stateblock->renderState[WINED3DRS_FOGVERTEXMODE])
2111 {
2112 case WINED3DFOG_NONE: RT_FALL_THRU();
2113 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
2114 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
2115 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
2116 }
2117 break;
2118
2119 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
2120 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
2121 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
2122 }
2123 }
2124 else
2125 {
2126 args->fog = FOG_OFF;
2127 }
2128 }
2129}
2130
2131static void pixelshader_set_limits(IWineD3DPixelShaderImpl *shader)
2132{
2133 DWORD shader_version = WINED3D_SHADER_VERSION(shader->baseShader.reg_maps.shader_version.major,
2134 shader->baseShader.reg_maps.shader_version.minor);
2135
2136 shader->baseShader.limits.attributes = 0;
2137 shader->baseShader.limits.address = 0;
2138 shader->baseShader.limits.packed_output = 0;
2139
2140 switch (shader_version)
2141 {
2142 case WINED3D_SHADER_VERSION(1, 0):
2143 case WINED3D_SHADER_VERSION(1, 1):
2144 case WINED3D_SHADER_VERSION(1, 2):
2145 case WINED3D_SHADER_VERSION(1, 3):
2146 shader->baseShader.limits.temporary = 2;
2147 shader->baseShader.limits.constant_float = 8;
2148 shader->baseShader.limits.constant_int = 0;
2149 shader->baseShader.limits.constant_bool = 0;
2150 shader->baseShader.limits.texcoord = 4;
2151 shader->baseShader.limits.sampler = 4;
2152 shader->baseShader.limits.packed_input = 0;
2153 shader->baseShader.limits.label = 0;
2154 break;
2155
2156 case WINED3D_SHADER_VERSION(1, 4):
2157 shader->baseShader.limits.temporary = 6;
2158 shader->baseShader.limits.constant_float = 8;
2159 shader->baseShader.limits.constant_int = 0;
2160 shader->baseShader.limits.constant_bool = 0;
2161 shader->baseShader.limits.texcoord = 6;
2162 shader->baseShader.limits.sampler = 6;
2163 shader->baseShader.limits.packed_input = 0;
2164 shader->baseShader.limits.label = 0;
2165 break;
2166
2167 /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2168 case WINED3D_SHADER_VERSION(2, 0):
2169 shader->baseShader.limits.temporary = 32;
2170 shader->baseShader.limits.constant_float = 32;
2171 shader->baseShader.limits.constant_int = 16;
2172 shader->baseShader.limits.constant_bool = 16;
2173 shader->baseShader.limits.texcoord = 8;
2174 shader->baseShader.limits.sampler = 16;
2175 shader->baseShader.limits.packed_input = 0;
2176 break;
2177
2178 case WINED3D_SHADER_VERSION(2, 1):
2179 shader->baseShader.limits.temporary = 32;
2180 shader->baseShader.limits.constant_float = 32;
2181 shader->baseShader.limits.constant_int = 16;
2182 shader->baseShader.limits.constant_bool = 16;
2183 shader->baseShader.limits.texcoord = 8;
2184 shader->baseShader.limits.sampler = 16;
2185 shader->baseShader.limits.packed_input = 0;
2186 shader->baseShader.limits.label = 16;
2187 break;
2188
2189 case WINED3D_SHADER_VERSION(4, 0):
2190 FIXME("Using 3.0 limits for 4.0 shader.\n");
2191 RT_FALL_THRU();
2192
2193 case WINED3D_SHADER_VERSION(3, 0):
2194 shader->baseShader.limits.temporary = 32;
2195 shader->baseShader.limits.constant_float = 224;
2196 shader->baseShader.limits.constant_int = 16;
2197 shader->baseShader.limits.constant_bool = 16;
2198 shader->baseShader.limits.texcoord = 0;
2199 shader->baseShader.limits.sampler = 16;
2200 shader->baseShader.limits.packed_input = 12;
2201 shader->baseShader.limits.label = 16; /* FIXME: 2048 */
2202 break;
2203
2204 default:
2205 shader->baseShader.limits.temporary = 32;
2206 shader->baseShader.limits.constant_float = 32;
2207 shader->baseShader.limits.constant_int = 16;
2208 shader->baseShader.limits.constant_bool = 16;
2209 shader->baseShader.limits.texcoord = 8;
2210 shader->baseShader.limits.sampler = 16;
2211 shader->baseShader.limits.packed_input = 0;
2212 shader->baseShader.limits.label = 0;
2213 FIXME("Unrecognized pixel shader version %u.%u\n",
2214 shader->baseShader.reg_maps.shader_version.major,
2215 shader->baseShader.reg_maps.shader_version.minor);
2216 }
2217}
2218
2219HRESULT pixelshader_init(IWineD3DPixelShaderImpl *shader, IWineD3DDeviceImpl *device,
2220 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
2221 IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
2222{
2223 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2224 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
2225 HRESULT hr;
2226
2227 if (!byte_code) return WINED3DERR_INVALIDCALL;
2228
2229 shader->lpVtbl = &IWineD3DPixelShader_Vtbl;
2230 shader_init(&shader->baseShader, device, parent, parent_ops);
2231
2232 hr = shader_set_function((IWineD3DBaseShaderImpl *)shader, byte_code,
2233 output_signature, device->d3d_pshader_constantF);
2234 if (FAILED(hr))
2235 {
2236 WARN("Failed to set function, hr %#x.\n", hr);
2237 shader_cleanup((IWineD3DBaseShader *)shader);
2238 return hr;
2239 }
2240
2241 pixelshader_set_limits(shader);
2242
2243 for (i = 0; i < MAX_REG_INPUT; ++i)
2244 {
2245 if (shader->input_reg_used[i])
2246 {
2247 ++num_regs_used;
2248 highest_reg_used = i;
2249 }
2250 }
2251
2252 /* Don't do any register mapping magic if it is not needed, or if we can't
2253 * achieve anything anyway */
2254 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
2255 || num_regs_used > (gl_info->limits.glsl_varyings / 4))
2256 {
2257 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
2258 {
2259 /* This happens with relative addressing. The input mapper function
2260 * warns about this if the higher registers are declared too, so
2261 * don't write a FIXME here */
2262 WARN("More varying registers used than supported\n");
2263 }
2264
2265 for (i = 0; i < MAX_REG_INPUT; ++i)
2266 {
2267 shader->input_reg_map[i] = i;
2268 }
2269
2270 shader->declared_in_count = highest_reg_used + 1;
2271 }
2272 else
2273 {
2274 shader->declared_in_count = 0;
2275 for (i = 0; i < MAX_REG_INPUT; ++i)
2276 {
2277 if (shader->input_reg_used[i]) shader->input_reg_map[i] = shader->declared_in_count++;
2278 else shader->input_reg_map[i] = ~0U;
2279 }
2280 }
2281
2282 shader->baseShader.load_local_constsF = FALSE;
2283
2284 return WINED3D_OK;
2285}
2286
2287void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures)
2288{
2289 WINED3DSAMPLER_TEXTURE_TYPE *sampler_type = reg_maps->sampler_type;
2290 unsigned int i;
2291
2292 if (reg_maps->shader_version.major != 1) return;
2293
2294 for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
2295 {
2296 /* We don't sample from this sampler. */
2297 if (!sampler_type[i]) continue;
2298
2299 if (!textures[i])
2300 {
2301 WARN("No texture bound to sampler %u, using 2D.\n", i);
2302 sampler_type[i] = WINED3DSTT_2D;
2303 continue;
2304 }
2305
2306 switch (IWineD3DBaseTexture_GetTextureDimensions(textures[i]))
2307 {
2308 case GL_TEXTURE_RECTANGLE_ARB:
2309 case GL_TEXTURE_2D:
2310 /* We have to select between texture rectangles and 2D
2311 * textures later because 2.0 and 3.0 shaders only have
2312 * WINED3DSTT_2D as well. */
2313 sampler_type[i] = WINED3DSTT_2D;
2314 break;
2315
2316 case GL_TEXTURE_3D:
2317 sampler_type[i] = WINED3DSTT_VOLUME;
2318 break;
2319
2320 case GL_TEXTURE_CUBE_MAP_ARB:
2321 sampler_type[i] = WINED3DSTT_CUBE;
2322 break;
2323
2324 default:
2325 FIXME("Unrecognized texture type %#x, using 2D.\n",
2326 IWineD3DBaseTexture_GetTextureDimensions(textures[i]));
2327 sampler_type[i] = WINED3DSTT_2D;
2328 }
2329 }
2330}
2331
2332#ifdef VBOX_WINE_WITH_SHADER_CACHE
2333
2334static void shader_reinit(struct IWineD3DBaseShaderClass *shader, IWineD3DDeviceImpl *device,
2335 IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
2336{
2337 shader->ref = 1;
2338 shader->device = (IWineD3DDevice *)device;
2339 shader->parent = parent;
2340 shader->parent_ops = parent_ops;
2341#ifdef DEBUG
2342 {
2343 IWineD3DBaseShaderImpl *tstShader;
2344 LIST_FOR_EACH_ENTRY(tstShader, &device->shaders, IWineD3DBaseShaderImpl, baseShader.shader_list_entry) {
2345 if (&tstShader->baseShader == shader)
2346 return;
2347 }
2348 ERR("shader not in list!");
2349 }
2350#endif
2351}
2352
2353static DECLCALLBACK(uint32_t) shader_cache_hash(void *pvKey)
2354{
2355 uint32_t u32Hash, i;
2356 DWORD *function;
2357 IWineD3DBaseShaderImpl *pShader = (IWineD3DBaseShaderImpl *)pvKey;
2358 if (pShader->baseShader.u32CacheDataInited)
2359 return pShader->baseShader.u32Hash;
2360
2361 u32Hash = pShader->baseShader.functionLength;
2362 function = pShader->baseShader.function;
2363 for (i = 0; i < pShader->baseShader.functionLength / 4; ++i)
2364 {
2365 u32Hash += *function++;
2366 }
2367
2368 for (i = 0; i < pShader->baseShader.functionLength % 4; ++i)
2369 {
2370 u32Hash += *((uint8_t*)function)++;
2371 }
2372
2373 pShader->baseShader.u32Hash = u32Hash;
2374 pShader->baseShader.u32CacheDataInited = TRUE;
2375 return pShader->baseShader.u32Hash;
2376}
2377
2378static DECLCALLBACK(bool) shader_cache_equal(void *pvKey1, void *pvKey2)
2379{
2380 IWineD3DBaseShaderImpl *pShader1 = (IWineD3DBaseShaderImpl *)pvKey1;
2381 IWineD3DBaseShaderImpl *pShader2 = (IWineD3DBaseShaderImpl *)pvKey2;
2382
2383 if (pShader1 == pShader2)
2384 return TRUE;
2385
2386 if (pShader1->baseShader.functionLength != pShader2->baseShader.functionLength)
2387 return FALSE;
2388
2389 if (shader_cache_hash(pvKey1) != shader_cache_hash(pvKey2))
2390 {
2391#ifdef DEBUG_misha
2392 Assert(memcmp(pShader1->baseShader.function, pShader2->baseShader.function, pShader1->baseShader.functionLength));
2393#endif
2394 return FALSE;
2395 }
2396
2397 return !memcmp(pShader1->baseShader.function, pShader2->baseShader.function, pShader1->baseShader.functionLength);
2398}
2399
2400#define VBOX_SHADER_FROM_CACHE_ENTRY(_pEntry) RT_FROM_MEMBER(RT_FROM_MEMBER((_pEntry), IWineD3DBaseShaderClass, CacheEntry), IWineD3DBaseShaderImpl, baseShader)
2401
2402static DECLCALLBACK(void) shader_cache_cleanup_entry(void *pvKey, struct VBOXEXT_HASHCACHE_ENTRY *pEntry)
2403{
2404 IWineD3DBaseShaderImpl *pShader = VBOX_SHADER_FROM_CACHE_ENTRY(pEntry);
2405 shader_cleanup((IWineD3DBaseShader *)pShader);
2406 if (pShader->baseShader.parent)
2407 pShader->baseShader.parent_ops->wined3d_object_destroyed(pShader->baseShader.parent);
2408 HeapFree(GetProcessHeap(), 0, pShader);
2409}
2410
2411static void shader_chache_init(PVBOXEXT_HASHCACHE pCache)
2412{
2413 VBoxExtCacheInit(pCache, 1024, shader_cache_hash, shader_cache_equal, shader_cache_cleanup_entry);
2414}
2415
2416static void shader_chache_term(PVBOXEXT_HASHCACHE pCache)
2417{
2418 VBoxExtCacheTerm(pCache);
2419}
2420
2421static void shader_chache_put(PVBOXEXT_HASHCACHE pCache, IWineD3DBaseShaderImpl *pShader)
2422{
2423 if (pShader->baseShader.parent)
2424 {
2425 pShader->baseShader.parent_ops->wined3d_object_destroyed(pShader->baseShader.parent);
2426 pShader->baseShader.parent = NULL;
2427 }
2428
2429 VBoxExtCachePut(pCache, pShader, &pShader->baseShader.CacheEntry);
2430}
2431
2432static IWineD3DBaseShaderImpl* shader_chache_get(PVBOXEXT_HASHCACHE pCache, IWineD3DBaseShaderImpl *pShader)
2433{
2434 PVBOXEXT_HASHCACHE_ENTRY pEntry = VBoxExtCacheGet(pCache, pShader);
2435 IWineD3DBaseShaderImpl *pCachedShader;
2436
2437 if (!pEntry)
2438 return pShader;
2439
2440 pCachedShader = VBOX_SHADER_FROM_CACHE_ENTRY(pEntry);
2441 shader_reinit(&pCachedShader->baseShader, (IWineD3DDeviceImpl *)pShader->baseShader.device,
2442 pShader->baseShader.parent, pShader->baseShader.parent_ops);
2443 pShader->baseShader.parent = NULL;
2444 /* we can not do a IWineD3DBaseShader_Release here since this would result in putting a shader to the cache */
2445 shader_cleanup((IWineD3DBaseShader *)pShader);
2446 HeapFree(GetProcessHeap(), 0, pShader);
2447 return pCachedShader;
2448}
2449
2450IWineD3DVertexShaderImpl * vertexshader_check_cached(IWineD3DDeviceImpl *device, IWineD3DVertexShaderImpl *object)
2451{
2452 return (IWineD3DVertexShaderImpl*)shader_chache_get(&device->vshaderCache, (IWineD3DBaseShaderImpl *)object);
2453}
2454
2455IWineD3DPixelShaderImpl * pixelshader_check_cached(IWineD3DDeviceImpl *device, IWineD3DPixelShaderImpl *object)
2456{
2457 return (IWineD3DPixelShaderImpl*)shader_chache_get(&device->pshaderCache, (IWineD3DBaseShaderImpl *)object);
2458}
2459
2460void shader_chaches_init(IWineD3DDeviceImpl *device)
2461{
2462 shader_chache_init(&device->vshaderCache);
2463 shader_chache_init(&device->pshaderCache);
2464}
2465
2466void shader_chaches_term(IWineD3DDeviceImpl *device)
2467{
2468 shader_chache_term(&device->vshaderCache);
2469 shader_chache_term(&device->pshaderCache);
2470}
2471#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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