VirtualBox

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

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

wine: update to 1.6.2

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