VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine_new/wined3d/shader.c@ 48345

Last change on this file since 48345 was 48345, checked in by vboxsync, 11 years ago

header fixes

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