VirtualBox

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

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

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

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