VirtualBox

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

Last change on this file since 46966 was 46521, checked in by vboxsync, 12 years ago

wine/new: export

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