wined3d: Don't replicate shader limits values for each shader.
[wine/multimedia.git] / dlls / wined3d / shader.c
blob92273462031a8d701357d8f9a0b0d83d4440b2d1
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
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.
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.
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <math.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 /* pow, mul_high, sub_high, mul_low */
37 const float wined3d_srgb_const0[] = {0.41666f, 1.055f, 0.055f, 12.92f};
38 /* cmp */
39 const float wined3d_srgb_const1[] = {0.0031308f, 0.0f, 0.0f, 0.0f};
41 static const char * const shader_opcode_names[] =
43 /* WINED3DSIH_ABS */ "abs",
44 /* WINED3DSIH_ADD */ "add",
45 /* WINED3DSIH_AND */ "and",
46 /* WINED3DSIH_BEM */ "bem",
47 /* WINED3DSIH_BREAK */ "break",
48 /* WINED3DSIH_BREAKC */ "breakc",
49 /* WINED3DSIH_BREAKP */ "breakp",
50 /* WINED3DSIH_CALL */ "call",
51 /* WINED3DSIH_CALLNZ */ "callnz",
52 /* WINED3DSIH_CMP */ "cmp",
53 /* WINED3DSIH_CND */ "cnd",
54 /* WINED3DSIH_CRS */ "crs",
55 /* WINED3DSIH_CUT */ "cut",
56 /* WINED3DSIH_DCL */ "dcl",
57 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
58 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
59 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
60 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
61 /* WINED3DSIH_DEF */ "def",
62 /* WINED3DSIH_DEFB */ "defb",
63 /* WINED3DSIH_DEFI */ "defi",
64 /* WINED3DSIH_DIV */ "div",
65 /* WINED3DSIH_DP2 */ "dp2",
66 /* WINED3DSIH_DP2ADD */ "dp2add",
67 /* WINED3DSIH_DP3 */ "dp3",
68 /* WINED3DSIH_DP4 */ "dp4",
69 /* WINED3DSIH_DST */ "dst",
70 /* WINED3DSIH_DSX */ "dsx",
71 /* WINED3DSIH_DSY */ "dsy",
72 /* WINED3DSIH_ELSE */ "else",
73 /* WINED3DSIH_EMIT */ "emit",
74 /* WINED3DSIH_ENDIF */ "endif",
75 /* WINED3DSIH_ENDLOOP */ "endloop",
76 /* WINED3DSIH_ENDREP */ "endrep",
77 /* WINED3DSIH_EQ */ "eq",
78 /* WINED3DSIH_EXP */ "exp",
79 /* WINED3DSIH_EXPP */ "expp",
80 /* WINED3DSIH_FRC */ "frc",
81 /* WINED3DSIH_FTOI */ "ftoi",
82 /* WINED3DSIH_GE */ "ge",
83 /* WINED3DSIH_IADD */ "iadd",
84 /* WINED3DSIH_IEQ */ "ieq",
85 /* WINED3DSIH_IF */ "if",
86 /* WINED3DSIH_IFC */ "ifc",
87 /* WINED3DSIH_IGE */ "ige",
88 /* WINED3DSIH_IMUL */ "imul",
89 /* WINED3DSIH_ISHL */ "ishl",
90 /* WINED3DSIH_ITOF */ "itof",
91 /* WINED3DSIH_LABEL */ "label",
92 /* WINED3DSIH_LD */ "ld",
93 /* WINED3DSIH_LIT */ "lit",
94 /* WINED3DSIH_LOG */ "log",
95 /* WINED3DSIH_LOGP */ "logp",
96 /* WINED3DSIH_LOOP */ "loop",
97 /* WINED3DSIH_LRP */ "lrp",
98 /* WINED3DSIH_LT */ "lt",
99 /* WINED3DSIH_M3x2 */ "m3x2",
100 /* WINED3DSIH_M3x3 */ "m3x3",
101 /* WINED3DSIH_M3x4 */ "m3x4",
102 /* WINED3DSIH_M4x3 */ "m4x3",
103 /* WINED3DSIH_M4x4 */ "m4x4",
104 /* WINED3DSIH_MAD */ "mad",
105 /* WINED3DSIH_MAX */ "max",
106 /* WINED3DSIH_MIN */ "min",
107 /* WINED3DSIH_MOV */ "mov",
108 /* WINED3DSIH_MOVA */ "mova",
109 /* WINED3DSIH_MOVC */ "movc",
110 /* WINED3DSIH_MUL */ "mul",
111 /* WINED3DSIH_NOP */ "nop",
112 /* WINED3DSIH_NRM */ "nrm",
113 /* WINED3DSIH_PHASE */ "phase",
114 /* WINED3DSIH_POW */ "pow",
115 /* WINED3DSIH_RCP */ "rcp",
116 /* WINED3DSIH_REP */ "rep",
117 /* WINED3DSIH_RET */ "ret",
118 /* WINED3DSIH_ROUND_NI */ "round_ni",
119 /* WINED3DSIH_RSQ */ "rsq",
120 /* WINED3DSIH_SAMPLE */ "sample",
121 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
122 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
123 /* WINED3DSIH_SETP */ "setp",
124 /* WINED3DSIH_SGE */ "sge",
125 /* WINED3DSIH_SGN */ "sgn",
126 /* WINED3DSIH_SINCOS */ "sincos",
127 /* WINED3DSIH_SLT */ "slt",
128 /* WINED3DSIH_SQRT */ "sqrt",
129 /* WINED3DSIH_SUB */ "sub",
130 /* WINED3DSIH_TEX */ "texld",
131 /* WINED3DSIH_TEXBEM */ "texbem",
132 /* WINED3DSIH_TEXBEML */ "texbeml",
133 /* WINED3DSIH_TEXCOORD */ "texcrd",
134 /* WINED3DSIH_TEXDEPTH */ "texdepth",
135 /* WINED3DSIH_TEXDP3 */ "texdp3",
136 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
137 /* WINED3DSIH_TEXKILL */ "texkill",
138 /* WINED3DSIH_TEXLDD */ "texldd",
139 /* WINED3DSIH_TEXLDL */ "texldl",
140 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
141 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
142 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
143 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
144 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
145 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
146 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
147 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
148 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
149 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
150 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
151 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
152 /* WINED3DSIH_UDIV */ "udiv",
153 /* WINED3DSIH_USHR */ "ushr",
154 /* WINED3DSIH_UTOF */ "utof",
155 /* WINED3DSIH_XOR */ "xor",
158 static const char * const semantic_names[] =
160 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
161 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
162 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
163 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
164 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
165 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
166 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
167 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
168 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
169 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
170 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
171 /* WINED3D_DECL_USAGE_FOG */ "FOG",
172 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
173 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
176 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage)
178 if (usage >= sizeof(semantic_names) / sizeof(*semantic_names))
180 FIXME("Unrecognized usage %#x.\n", usage);
181 return "UNRECOGNIZED";
184 return semantic_names[usage];
187 static enum wined3d_decl_usage shader_usage_from_semantic_name(const char *name)
189 unsigned int i;
191 for (i = 0; i < sizeof(semantic_names) / sizeof(*semantic_names); ++i)
193 if (!strcmp(name, semantic_names[i])) return i;
196 return ~0U;
199 BOOL shader_match_semantic(const char *semantic_name, enum wined3d_decl_usage usage)
201 return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
204 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
205 const struct wined3d_shader_semantic *s)
207 e->semantic_name = shader_semantic_name_from_usage(s->usage);
208 e->semantic_idx = s->usage_idx;
209 e->sysval_semantic = 0;
210 e->component_type = 0;
211 e->register_idx = s->reg.reg.idx[0].offset;
212 e->mask = s->reg.write_mask;
215 static void shader_signature_from_usage(struct wined3d_shader_signature_element *e,
216 enum wined3d_decl_usage usage, UINT usage_idx, UINT reg_idx, DWORD write_mask)
218 e->semantic_name = shader_semantic_name_from_usage(usage);
219 e->semantic_idx = usage_idx;
220 e->sysval_semantic = 0;
221 e->component_type = 0;
222 e->register_idx = reg_idx;
223 e->mask = write_mask;
226 static const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token)
228 switch (version_token >> 16)
230 case WINED3D_SM1_VS:
231 case WINED3D_SM1_PS:
232 return &sm1_shader_frontend;
234 case WINED3D_SM4_PS:
235 case WINED3D_SM4_VS:
236 case WINED3D_SM4_GS:
237 return &sm4_shader_frontend;
239 default:
240 FIXME("Unrecognised version token %#x\n", version_token);
241 return NULL;
245 void shader_buffer_clear(struct wined3d_shader_buffer *buffer)
247 buffer->buffer[0] = '\0';
248 buffer->content_size = 0;
249 buffer->lineNo = 0;
250 buffer->newline = TRUE;
253 BOOL shader_buffer_init(struct wined3d_shader_buffer *buffer)
255 buffer->buffer_size = 16384;
256 if (!(buffer->buffer = HeapAlloc(GetProcessHeap(), 0, buffer->buffer_size)))
258 ERR("Failed to allocate shader buffer memory.\n");
259 return FALSE;
262 shader_buffer_clear(buffer);
263 return TRUE;
266 void shader_buffer_free(struct wined3d_shader_buffer *buffer)
268 HeapFree(GetProcessHeap(), 0, buffer->buffer);
271 int shader_vaddline(struct wined3d_shader_buffer *buffer, const char *format, va_list args)
273 char *base = buffer->buffer + buffer->content_size;
274 int rc;
275 char *new_buffer;
277 while(1)
279 rc = vsnprintf(base, buffer->buffer_size - buffer->content_size, format, args);
280 if (rc < 0 /* C89 */ || (unsigned int)rc >= buffer->buffer_size - buffer->content_size /* C99 */)
282 new_buffer = HeapReAlloc(GetProcessHeap(), 0, buffer->buffer, buffer->buffer_size * 2);
283 if (!new_buffer)
285 ERR("The buffer allocated for the shader program string is too small at %d bytes.\n", buffer->buffer_size);
286 buffer->content_size = buffer->buffer_size - 1;
287 return -1;
289 buffer->buffer = new_buffer;
290 buffer->buffer_size = buffer->buffer_size * 2;
291 base = buffer->buffer + buffer->content_size;
293 else
295 break;
299 if (buffer->newline)
301 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->content_size, base);
302 buffer->newline = FALSE;
304 else
306 TRACE("%s", base);
309 buffer->content_size += rc;
310 if (buffer->buffer[buffer->content_size-1] == '\n')
312 ++buffer->lineNo;
313 buffer->newline = TRUE;
316 return 0;
319 int shader_addline(struct wined3d_shader_buffer *buffer, const char *format, ...)
321 va_list args;
322 int ret;
324 va_start(args, format);
325 ret = shader_vaddline(buffer, format, args);
326 va_end(args);
328 return ret;
331 static void shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
332 void *parent, const struct wined3d_parent_ops *parent_ops)
334 shader->ref = 1;
335 shader->device = device;
336 shader->parent = parent;
337 shader->parent_ops = parent_ops;
338 list_init(&shader->linked_programs);
339 list_add_head(&device->shaders, &shader->shader_list_entry);
342 /* Convert floating point offset relative to a register file to an absolute
343 * offset for float constants. */
344 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type, UINT register_idx)
346 switch (register_type)
348 case WINED3DSPR_CONST: return register_idx;
349 case WINED3DSPR_CONST2: return 2048 + register_idx;
350 case WINED3DSPR_CONST3: return 4096 + register_idx;
351 case WINED3DSPR_CONST4: return 6144 + register_idx;
352 default:
353 FIXME("Unsupported register type: %u.\n", register_type);
354 return register_idx;
358 static void shader_delete_constant_list(struct list *clist)
360 struct wined3d_shader_lconst *constant;
361 struct list *ptr;
363 ptr = list_head(clist);
364 while (ptr)
366 constant = LIST_ENTRY(ptr, struct wined3d_shader_lconst, entry);
367 ptr = list_next(clist, ptr);
368 HeapFree(GetProcessHeap(), 0, constant);
370 list_init(clist);
373 static void shader_set_limits(struct wined3d_shader *shader)
375 static const struct limits_entry
377 unsigned int min_version;
378 unsigned int max_version;
379 struct wined3d_shader_limits limits;
381 vs_limits[] =
383 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
384 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
385 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
386 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
387 * even though they are capable of supporting much more (GL
388 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
389 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
390 * shaders to 256. */
391 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
392 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
395 gs_limits[] =
397 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
398 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
401 ps_limits[] =
403 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
404 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
405 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
406 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
407 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 12}},
408 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 0, 32}},
411 const struct limits_entry *limits_array;
412 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
413 shader->reg_maps.shader_version.minor);
414 int i = 0;
416 switch (shader->reg_maps.shader_version.type)
418 default:
419 FIXME("Unexpected shader type %u found.\n", shader->reg_maps.shader_version.type);
420 /* Fall-through. */
421 case WINED3D_SHADER_TYPE_VERTEX:
422 limits_array = vs_limits;
423 break;
424 case WINED3D_SHADER_TYPE_GEOMETRY:
425 limits_array = gs_limits;
426 break;
427 case WINED3D_SHADER_TYPE_PIXEL:
428 limits_array = ps_limits;
429 break;
432 while (limits_array[i].min_version && limits_array[i].min_version <= shader_version)
434 if (shader_version <= limits_array[i].max_version)
436 shader->limits = &limits_array[i].limits;
437 break;
439 ++i;
441 if (!shader->limits)
443 FIXME("Unexpected shader version \"%u.%u\".\n",
444 shader->reg_maps.shader_version.major,
445 shader->reg_maps.shader_version.minor);
446 shader->limits = &limits_array[max(0, i - 1)].limits;
450 static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
452 DWORD idx, shift;
453 idx = bit >> 5;
454 shift = bit & 0x1f;
455 bitmap[idx] |= (1 << shift);
458 static BOOL shader_record_register_usage(struct wined3d_shader *shader, struct wined3d_shader_reg_maps *reg_maps,
459 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type, unsigned int constf_size)
461 switch (reg->type)
463 case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
464 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
465 reg_maps->texcoord |= 1 << reg->idx[0].offset;
466 else
467 reg_maps->address |= 1 << reg->idx[0].offset;
468 break;
470 case WINED3DSPR_TEMP:
471 reg_maps->temporary |= 1 << reg->idx[0].offset;
472 break;
474 case WINED3DSPR_INPUT:
475 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
477 if (reg->idx[0].rel_addr)
479 /* If relative addressing is used, we must assume that all registers
480 * are used. Even if it is a construct like v3[aL], we can't assume
481 * that v0, v1 and v2 aren't read because aL can be negative */
482 unsigned int i;
483 for (i = 0; i < MAX_REG_INPUT; ++i)
485 shader->u.ps.input_reg_used[i] = TRUE;
488 else
490 shader->u.ps.input_reg_used[reg->idx[0].offset] = TRUE;
493 else
494 reg_maps->input_registers |= 1 << reg->idx[0].offset;
495 break;
497 case WINED3DSPR_RASTOUT:
498 if (reg->idx[0].offset == 1)
499 reg_maps->fog = 1;
500 break;
502 case WINED3DSPR_MISCTYPE:
503 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
505 if (!reg->idx[0].offset)
506 reg_maps->vpos = 1;
507 else if (reg->idx[0].offset == 1)
508 reg_maps->usesfacing = 1;
510 break;
512 case WINED3DSPR_CONST:
513 if (reg->idx[0].rel_addr)
515 if (reg->idx[0].offset < reg_maps->min_rel_offset)
516 reg_maps->min_rel_offset = reg->idx[0].offset;
517 if (reg->idx[0].offset > reg_maps->max_rel_offset)
518 reg_maps->max_rel_offset = reg->idx[0].offset;
519 reg_maps->usesrelconstF = TRUE;
521 else
523 if (reg->idx[0].offset >= min(shader->limits->constant_float, constf_size))
525 WARN("Shader using float constant %u which is not supported.\n", reg->idx[0].offset);
526 return FALSE;
528 else
530 set_bitmap_bit(reg_maps->constf, reg->idx[0].offset);
533 break;
535 case WINED3DSPR_CONSTINT:
536 if (reg->idx[0].offset >= shader->limits->constant_int)
538 WARN("Shader using integer constant %u which is not supported.\n", reg->idx[0].offset);
539 return FALSE;
541 else
543 reg_maps->integer_constants |= (1 << reg->idx[0].offset);
545 break;
547 case WINED3DSPR_CONSTBOOL:
548 if (reg->idx[0].offset >= shader->limits->constant_bool)
550 WARN("Shader using bool constant %u which is not supported.\n", reg->idx[0].offset);
551 return FALSE;
553 else
555 reg_maps->boolean_constants |= (1 << reg->idx[0].offset);
557 break;
559 case WINED3DSPR_COLOROUT:
560 reg_maps->rt_mask |= (1 << reg->idx[0].offset);
561 break;
563 default:
564 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
565 reg->type, reg->idx[0].offset, reg->idx[1].offset);
566 break;
568 return TRUE;
571 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
573 switch (instr)
575 case WINED3DSIH_M4x4:
576 case WINED3DSIH_M3x4:
577 return param == 1 ? 3 : 0;
579 case WINED3DSIH_M4x3:
580 case WINED3DSIH_M3x3:
581 return param == 1 ? 2 : 0;
583 case WINED3DSIH_M3x2:
584 return param == 1 ? 1 : 0;
586 default:
587 return 0;
591 /* Note that this does not count the loop register as an address register. */
592 static HRESULT shader_get_registers_used(struct wined3d_shader *shader, const struct wined3d_shader_frontend *fe,
593 struct wined3d_shader_reg_maps *reg_maps, struct wined3d_shader_signature_element *input_signature,
594 struct wined3d_shader_signature_element *output_signature, const DWORD *byte_code, DWORD constf_size)
596 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
597 void *fe_data = shader->frontend_data;
598 struct wined3d_shader_version shader_version;
599 const DWORD *ptr = byte_code;
601 memset(reg_maps, 0, sizeof(*reg_maps));
602 reg_maps->min_rel_offset = ~0U;
604 fe->shader_read_header(fe_data, &ptr, &shader_version);
605 reg_maps->shader_version = shader_version;
607 shader_set_limits(shader);
609 reg_maps->constf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
610 sizeof(*reg_maps->constf) * ((min(shader->limits->constant_float, constf_size) + 31) / 32));
611 if (!reg_maps->constf)
613 ERR("Failed to allocate constant map memory.\n");
614 return E_OUTOFMEMORY;
617 while (!fe->shader_is_end(fe_data, &ptr))
619 struct wined3d_shader_instruction ins;
621 /* Fetch opcode. */
622 fe->shader_read_instruction(fe_data, &ptr, &ins);
624 /* Unhandled opcode, and its parameters. */
625 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
627 TRACE("Skipping unrecognized instruction.\n");
628 continue;
631 /* Handle declarations. */
632 if (ins.handler_idx == WINED3DSIH_DCL)
634 struct wined3d_shader_semantic *semantic = &ins.declaration.semantic;
636 switch (semantic->reg.reg.type)
638 /* Mark input registers used. */
639 case WINED3DSPR_INPUT:
640 reg_maps->input_registers |= 1 << semantic->reg.reg.idx[0].offset;
641 shader_signature_from_semantic(&input_signature[semantic->reg.reg.idx[0].offset], semantic);
642 break;
644 /* Vertex shader: mark 3.0 output registers used, save token. */
645 case WINED3DSPR_OUTPUT:
646 reg_maps->output_registers |= 1 << semantic->reg.reg.idx[0].offset;
647 shader_signature_from_semantic(&output_signature[semantic->reg.reg.idx[0].offset], semantic);
648 if (semantic->usage == WINED3D_DECL_USAGE_FOG)
649 reg_maps->fog = 1;
650 break;
652 /* Save sampler usage token. */
653 case WINED3DSPR_SAMPLER:
654 reg_maps->sampler_type[semantic->reg.reg.idx[0].offset] = semantic->sampler_type;
655 break;
657 default:
658 TRACE("Not recording DCL register type %#x.\n", semantic->reg.reg.type);
659 break;
662 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
664 struct wined3d_shader_register *reg = &ins.declaration.src.reg;
665 if (reg->idx[0].offset >= WINED3D_MAX_CBS)
666 ERR("Invalid CB index %u.\n", reg->idx[0].offset);
667 else
668 reg_maps->cb_sizes[reg->idx[0].offset] = reg->idx[1].offset;
670 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE)
672 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
673 shader->u.gs.input_type = ins.declaration.primitive_type;
674 else
675 FIXME("Invalid instruction %#x for shader type %#x.\n",
676 ins.handler_idx, shader_version.type);
678 else if (ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
680 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
681 shader->u.gs.output_type = ins.declaration.primitive_type;
682 else
683 FIXME("Invalid instruction %#x for shader type %#x.\n",
684 ins.handler_idx, shader_version.type);
686 else if (ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
688 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
689 shader->u.gs.vertices_out = ins.declaration.count;
690 else
691 FIXME("Invalid instruction %#x for shader type %#x.\n",
692 ins.handler_idx, shader_version.type);
694 else if (ins.handler_idx == WINED3DSIH_DEF)
696 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
697 float *value;
698 if (!lconst) return E_OUTOFMEMORY;
700 lconst->idx = ins.dst[0].reg.idx[0].offset;
701 memcpy(lconst->value, ins.src[0].reg.immconst_data, 4 * sizeof(DWORD));
702 value = (float *)lconst->value;
704 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
705 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
707 if (value[0] < -1.0f) value[0] = -1.0f;
708 else if (value[0] > 1.0f) value[0] = 1.0f;
709 if (value[1] < -1.0f) value[1] = -1.0f;
710 else if (value[1] > 1.0f) value[1] = 1.0f;
711 if (value[2] < -1.0f) value[2] = -1.0f;
712 else if (value[2] > 1.0f) value[2] = 1.0f;
713 if (value[3] < -1.0f) value[3] = -1.0f;
714 else if (value[3] > 1.0f) value[3] = 1.0f;
717 list_add_head(&shader->constantsF, &lconst->entry);
719 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
720 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
722 shader->lconst_inf_or_nan = TRUE;
725 else if (ins.handler_idx == WINED3DSIH_DEFI)
727 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
728 if (!lconst) return E_OUTOFMEMORY;
730 lconst->idx = ins.dst[0].reg.idx[0].offset;
731 memcpy(lconst->value, ins.src[0].reg.immconst_data, 4 * sizeof(DWORD));
733 list_add_head(&shader->constantsI, &lconst->entry);
734 reg_maps->local_int_consts |= (1 << lconst->idx);
736 else if (ins.handler_idx == WINED3DSIH_DEFB)
738 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
739 if (!lconst) return E_OUTOFMEMORY;
741 lconst->idx = ins.dst[0].reg.idx[0].offset;
742 memcpy(lconst->value, ins.src[0].reg.immconst_data, sizeof(DWORD));
744 list_add_head(&shader->constantsB, &lconst->entry);
745 reg_maps->local_bool_consts |= (1 << lconst->idx);
747 /* For subroutine prototypes. */
748 else if (ins.handler_idx == WINED3DSIH_LABEL)
750 reg_maps->labels |= 1 << ins.src[0].reg.idx[0].offset;
752 /* Set texture, address, temporary registers. */
753 else
755 BOOL color0_mov = FALSE;
756 unsigned int i;
758 /* This will loop over all the registers and try to
759 * make a bitmask of the ones we're interested in.
761 * Relative addressing tokens are ignored, but that's
762 * okay, since we'll catch any address registers when
763 * they are initialized (required by spec). */
764 for (i = 0; i < ins.dst_count; ++i)
766 if (!shader_record_register_usage(shader, reg_maps, &ins.dst[i].reg,
767 shader_version.type, constf_size))
768 return WINED3DERR_INVALIDCALL;
770 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
771 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
772 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
773 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX && shader_version.major < 3)
775 UINT idx = ins.dst[i].reg.idx[0].offset;
777 switch (ins.dst[i].reg.type)
779 case WINED3DSPR_RASTOUT:
780 switch (idx)
782 case 0: /* oPos */
783 reg_maps->output_registers |= 1 << 10;
784 shader_signature_from_usage(&output_signature[10],
785 WINED3D_DECL_USAGE_POSITION, 0, 10, WINED3DSP_WRITEMASK_ALL);
786 break;
788 case 1: /* oFog */
789 reg_maps->output_registers |= 1 << 11;
790 shader_signature_from_usage(&output_signature[11],
791 WINED3D_DECL_USAGE_FOG, 0, 11, WINED3DSP_WRITEMASK_0);
792 break;
794 case 2: /* oPts */
795 reg_maps->output_registers |= 1 << 11;
796 shader_signature_from_usage(&output_signature[11],
797 WINED3D_DECL_USAGE_PSIZE, 0, 11, WINED3DSP_WRITEMASK_1);
798 break;
800 break;
802 case WINED3DSPR_ATTROUT:
803 if (idx < 2)
805 idx += 8;
806 if (reg_maps->output_registers & (1 << idx))
808 output_signature[idx].mask |= ins.dst[i].write_mask;
810 else
812 reg_maps->output_registers |= 1 << idx;
813 shader_signature_from_usage(&output_signature[idx],
814 WINED3D_DECL_USAGE_COLOR, idx - 8, idx, ins.dst[i].write_mask);
817 break;
819 case WINED3DSPR_TEXCRDOUT:
821 reg_maps->texcoord_mask[idx] |= ins.dst[i].write_mask;
822 if (reg_maps->output_registers & (1 << idx))
824 output_signature[idx].mask |= ins.dst[i].write_mask;
826 else
828 reg_maps->output_registers |= 1 << idx;
829 shader_signature_from_usage(&output_signature[idx],
830 WINED3D_DECL_USAGE_TEXCOORD, idx, idx, ins.dst[i].write_mask);
832 break;
834 default:
835 break;
839 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
841 if (ins.dst[i].reg.type == WINED3DSPR_COLOROUT && !ins.dst[i].reg.idx[0].offset)
843 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
844 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
845 * the mov and perform the sRGB write correction from the source register.
847 * However, if the mov is only partial, we can't do this, and if the write
848 * comes from an instruction other than MOV it is hard to do as well. If
849 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
850 shader->u.ps.color0_mov = FALSE;
851 if (ins.handler_idx == WINED3DSIH_MOV
852 && ins.dst[i].write_mask == WINED3DSP_WRITEMASK_ALL)
854 /* Used later when the source register is read. */
855 color0_mov = TRUE;
858 /* Also drop the MOV marker if the source register is overwritten prior to the shader
859 * end
861 else if (ins.dst[i].reg.type == WINED3DSPR_TEMP
862 && ins.dst[i].reg.idx[0].offset == shader->u.ps.color0_reg)
864 shader->u.ps.color0_mov = FALSE;
868 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
869 if (shader_version.major == 1
870 && (ins.handler_idx == WINED3DSIH_TEX
871 || ins.handler_idx == WINED3DSIH_TEXBEM
872 || ins.handler_idx == WINED3DSIH_TEXBEML
873 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
874 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
875 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
876 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
877 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
878 || ins.handler_idx == WINED3DSIH_TEXREG2AR
879 || ins.handler_idx == WINED3DSIH_TEXREG2GB
880 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
882 /* Fake sampler usage, only set reserved bit and type. */
883 DWORD sampler_code = ins.dst[i].reg.idx[0].offset;
885 TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
886 reg_maps->sampler_type[sampler_code] = WINED3DSTT_2D;
888 /* texbem is only valid with < 1.4 pixel shaders */
889 if (ins.handler_idx == WINED3DSIH_TEXBEM
890 || ins.handler_idx == WINED3DSIH_TEXBEML)
892 reg_maps->bumpmat |= 1 << ins.dst[i].reg.idx[0].offset;
893 if (ins.handler_idx == WINED3DSIH_TEXBEML)
895 reg_maps->luminanceparams |= 1 << ins.dst[i].reg.idx[0].offset;
899 else if (ins.handler_idx == WINED3DSIH_BEM)
901 reg_maps->bumpmat |= 1 << ins.dst[i].reg.idx[0].offset;
905 if (ins.handler_idx == WINED3DSIH_NRM) reg_maps->usesnrm = 1;
906 else if (ins.handler_idx == WINED3DSIH_DSY) reg_maps->usesdsy = 1;
907 else if (ins.handler_idx == WINED3DSIH_DSX) reg_maps->usesdsx = 1;
908 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
909 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
910 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
911 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
912 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
913 else if (ins.handler_idx == WINED3DSIH_POW) reg_maps->usespow = 1;
914 else if (ins.handler_idx == WINED3DSIH_LOOP
915 || ins.handler_idx == WINED3DSIH_REP)
917 ++cur_loop_depth;
918 if (cur_loop_depth > max_loop_depth)
919 max_loop_depth = cur_loop_depth;
921 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
922 || ins.handler_idx == WINED3DSIH_ENDREP)
923 --cur_loop_depth;
925 if (ins.predicate)
926 if (!shader_record_register_usage(shader, reg_maps, &ins.predicate->reg,
927 shader_version.type, constf_size))
928 return WINED3DERR_INVALIDCALL;
930 for (i = 0; i < ins.src_count; ++i)
932 unsigned int count = get_instr_extra_regcount(ins.handler_idx, i);
933 struct wined3d_shader_register reg = ins.src[i].reg;
935 if (!shader_record_register_usage(shader, reg_maps, &ins.src[i].reg,
936 shader_version.type, constf_size))
937 return WINED3DERR_INVALIDCALL;
938 while (count)
940 ++reg.idx[0].offset;
941 if (!shader_record_register_usage(shader, reg_maps, &reg,
942 shader_version.type, constf_size))
943 return WINED3DERR_INVALIDCALL;
944 --count;
947 if (color0_mov)
949 if (ins.src[i].reg.type == WINED3DSPR_TEMP
950 && ins.src[i].swizzle == WINED3DSP_NOSWIZZLE)
952 shader->u.ps.color0_mov = TRUE;
953 shader->u.ps.color0_reg = ins.src[i].reg.idx[0].offset;
959 reg_maps->loop_depth = max_loop_depth;
961 /* PS before 2.0 don't have explicit color outputs. Instead the value of
962 * R0 is written to the render target. */
963 if (shader_version.major < 2 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
964 reg_maps->rt_mask |= (1 << 0);
966 shader->functionLength = ((const char *)ptr - (const char *)byte_code);
968 return WINED3D_OK;
971 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
973 DWORD map = 1 << max;
974 map |= map - 1;
975 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
977 return wined3d_log2i(map);
980 static void shader_dump_decl_usage(const struct wined3d_shader_semantic *semantic,
981 const struct wined3d_shader_version *shader_version)
983 TRACE("dcl");
985 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
987 switch (semantic->sampler_type)
989 case WINED3DSTT_2D: TRACE("_2d"); break;
990 case WINED3DSTT_CUBE: TRACE("_cube"); break;
991 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
992 default: TRACE("_unknown_ttype(0x%08x)", semantic->sampler_type);
995 else
997 /* Pixel shaders 3.0 don't have usage semantics. */
998 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL) return;
999 else TRACE("_");
1001 switch (semantic->usage)
1003 case WINED3D_DECL_USAGE_POSITION:
1004 TRACE("position%u", semantic->usage_idx);
1005 break;
1007 case WINED3D_DECL_USAGE_BLEND_INDICES:
1008 TRACE("blend");
1009 break;
1011 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
1012 TRACE("weight");
1013 break;
1015 case WINED3D_DECL_USAGE_NORMAL:
1016 TRACE("normal%u", semantic->usage_idx);
1017 break;
1019 case WINED3D_DECL_USAGE_PSIZE:
1020 TRACE("psize");
1021 break;
1023 case WINED3D_DECL_USAGE_COLOR:
1024 if (!semantic->usage_idx) TRACE("color");
1025 else TRACE("specular%u", (semantic->usage_idx - 1));
1026 break;
1028 case WINED3D_DECL_USAGE_TEXCOORD:
1029 TRACE("texture%u", semantic->usage_idx);
1030 break;
1032 case WINED3D_DECL_USAGE_TANGENT:
1033 TRACE("tangent");
1034 break;
1036 case WINED3D_DECL_USAGE_BINORMAL:
1037 TRACE("binormal");
1038 break;
1040 case WINED3D_DECL_USAGE_TESS_FACTOR:
1041 TRACE("tessfactor");
1042 break;
1044 case WINED3D_DECL_USAGE_POSITIONT:
1045 TRACE("positionT%u", semantic->usage_idx);
1046 break;
1048 case WINED3D_DECL_USAGE_FOG:
1049 TRACE("fog");
1050 break;
1052 case WINED3D_DECL_USAGE_DEPTH:
1053 TRACE("depth");
1054 break;
1056 case WINED3D_DECL_USAGE_SAMPLE:
1057 TRACE("sample");
1058 break;
1060 default:
1061 FIXME("unknown_semantics(0x%08x)", semantic->usage);
1066 static void shader_dump_register(const struct wined3d_shader_register *reg,
1067 const struct wined3d_shader_version *shader_version)
1069 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
1070 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
1071 UINT offset = reg->idx[0].offset;
1073 switch (reg->type)
1075 case WINED3DSPR_TEMP:
1076 TRACE("r");
1077 break;
1079 case WINED3DSPR_INPUT:
1080 TRACE("v");
1081 break;
1083 case WINED3DSPR_CONST:
1084 case WINED3DSPR_CONST2:
1085 case WINED3DSPR_CONST3:
1086 case WINED3DSPR_CONST4:
1087 TRACE("c");
1088 offset = shader_get_float_offset(reg->type, offset);
1089 break;
1091 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
1092 TRACE("%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
1093 break;
1095 case WINED3DSPR_RASTOUT:
1096 TRACE("%s", rastout_reg_names[offset]);
1097 break;
1099 case WINED3DSPR_COLOROUT:
1100 TRACE("oC");
1101 break;
1103 case WINED3DSPR_DEPTHOUT:
1104 TRACE("oDepth");
1105 break;
1107 case WINED3DSPR_ATTROUT:
1108 TRACE("oD");
1109 break;
1111 case WINED3DSPR_TEXCRDOUT:
1112 /* Vertex shaders >= 3.0 use general purpose output registers
1113 * (WINED3DSPR_OUTPUT), which can include an address token. */
1114 if (shader_version->major >= 3) TRACE("o");
1115 else TRACE("oT");
1116 break;
1118 case WINED3DSPR_CONSTINT:
1119 TRACE("i");
1120 break;
1122 case WINED3DSPR_CONSTBOOL:
1123 TRACE("b");
1124 break;
1126 case WINED3DSPR_LABEL:
1127 TRACE("l");
1128 break;
1130 case WINED3DSPR_LOOP:
1131 TRACE("aL");
1132 break;
1134 case WINED3DSPR_SAMPLER:
1135 TRACE("s");
1136 break;
1138 case WINED3DSPR_MISCTYPE:
1139 if (offset > 1)
1140 FIXME("Unhandled misctype register %u.\n", offset);
1141 else
1142 TRACE("%s", misctype_reg_names[offset]);
1143 break;
1145 case WINED3DSPR_PREDICATE:
1146 TRACE("p");
1147 break;
1149 case WINED3DSPR_IMMCONST:
1150 TRACE("l");
1151 break;
1153 case WINED3DSPR_CONSTBUFFER:
1154 TRACE("cb");
1155 break;
1157 case WINED3DSPR_PRIMID:
1158 TRACE("primID");
1159 break;
1161 case WINED3DSPR_NULL:
1162 TRACE("null");
1163 break;
1165 case WINED3DSPR_RESOURCE:
1166 TRACE("t");
1167 break;
1169 default:
1170 TRACE("unhandled_rtype(%#x)", reg->type);
1171 break;
1174 if (reg->type == WINED3DSPR_IMMCONST)
1176 TRACE("(");
1177 switch (reg->immconst_type)
1179 case WINED3D_IMMCONST_SCALAR:
1180 switch (reg->data_type)
1182 case WINED3D_DATA_FLOAT:
1183 TRACE("%.8e", *(const float *)reg->immconst_data);
1184 break;
1185 case WINED3D_DATA_INT:
1186 TRACE("%d", reg->immconst_data[0]);
1187 break;
1188 case WINED3D_DATA_RESOURCE:
1189 case WINED3D_DATA_SAMPLER:
1190 case WINED3D_DATA_UINT:
1191 TRACE("%u", reg->immconst_data[0]);
1192 break;
1193 default:
1194 TRACE("<unhandled data type %#x>", reg->data_type);
1195 break;
1197 break;
1199 case WINED3D_IMMCONST_VEC4:
1200 switch (reg->data_type)
1202 case WINED3D_DATA_FLOAT:
1203 TRACE("%.8e, %.8e, %.8e, %.8e",
1204 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
1205 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
1206 break;
1207 case WINED3D_DATA_INT:
1208 TRACE("%d, %d, %d, %d",
1209 reg->immconst_data[0], reg->immconst_data[1],
1210 reg->immconst_data[2], reg->immconst_data[3]);
1211 break;
1212 case WINED3D_DATA_RESOURCE:
1213 case WINED3D_DATA_SAMPLER:
1214 case WINED3D_DATA_UINT:
1215 TRACE("%u, %u, %u, %u",
1216 reg->immconst_data[0], reg->immconst_data[1],
1217 reg->immconst_data[2], reg->immconst_data[3]);
1218 break;
1219 default:
1220 TRACE("<unhandled data type %#x>", reg->data_type);
1221 break;
1223 break;
1225 default:
1226 TRACE("<unhandled immconst_type %#x>", reg->immconst_type);
1227 break;
1229 TRACE(")");
1231 else if (reg->type != WINED3DSPR_RASTOUT
1232 && reg->type != WINED3DSPR_MISCTYPE
1233 && reg->type != WINED3DSPR_NULL)
1235 if (offset != ~0U)
1237 TRACE("[");
1238 if (reg->idx[0].rel_addr)
1240 shader_dump_src_param(reg->idx[0].rel_addr, shader_version);
1241 TRACE(" + ");
1243 TRACE("%u]", offset);
1245 if (reg->idx[1].offset != ~0U)
1247 TRACE("[");
1248 if (reg->idx[1].rel_addr)
1250 shader_dump_src_param(reg->idx[1].rel_addr, shader_version);
1251 TRACE(" + ");
1253 TRACE("%u]", reg->idx[1].offset);
1259 void shader_dump_dst_param(const struct wined3d_shader_dst_param *param,
1260 const struct wined3d_shader_version *shader_version)
1262 DWORD write_mask = param->write_mask;
1264 shader_dump_register(&param->reg, shader_version);
1266 if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
1268 static const char write_mask_chars[] = "xyzw";
1270 TRACE(".");
1271 if (write_mask & WINED3DSP_WRITEMASK_0) TRACE("%c", write_mask_chars[0]);
1272 if (write_mask & WINED3DSP_WRITEMASK_1) TRACE("%c", write_mask_chars[1]);
1273 if (write_mask & WINED3DSP_WRITEMASK_2) TRACE("%c", write_mask_chars[2]);
1274 if (write_mask & WINED3DSP_WRITEMASK_3) TRACE("%c", write_mask_chars[3]);
1278 void shader_dump_src_param(const struct wined3d_shader_src_param *param,
1279 const struct wined3d_shader_version *shader_version)
1281 enum wined3d_shader_src_modifier src_modifier = param->modifiers;
1282 DWORD swizzle = param->swizzle;
1284 if (src_modifier == WINED3DSPSM_NEG
1285 || src_modifier == WINED3DSPSM_BIASNEG
1286 || src_modifier == WINED3DSPSM_SIGNNEG
1287 || src_modifier == WINED3DSPSM_X2NEG
1288 || src_modifier == WINED3DSPSM_ABSNEG)
1289 TRACE("-");
1290 else if (src_modifier == WINED3DSPSM_COMP)
1291 TRACE("1-");
1292 else if (src_modifier == WINED3DSPSM_NOT)
1293 TRACE("!");
1295 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
1296 TRACE("abs(");
1298 shader_dump_register(&param->reg, shader_version);
1300 if (src_modifier)
1302 switch (src_modifier)
1304 case WINED3DSPSM_NONE: break;
1305 case WINED3DSPSM_NEG: break;
1306 case WINED3DSPSM_NOT: break;
1307 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
1308 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
1309 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
1310 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
1311 case WINED3DSPSM_COMP: break;
1312 case WINED3DSPSM_X2: TRACE("_x2"); break;
1313 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
1314 case WINED3DSPSM_DZ: TRACE("_dz"); break;
1315 case WINED3DSPSM_DW: TRACE("_dw"); break;
1316 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
1317 case WINED3DSPSM_ABS: TRACE(")"); break;
1318 default: TRACE("_unknown_modifier(%#x)", src_modifier);
1322 if (swizzle != WINED3DSP_NOSWIZZLE)
1324 static const char swizzle_chars[] = "xyzw";
1325 DWORD swizzle_x = swizzle & 0x03;
1326 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1327 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1328 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1330 if (swizzle_x == swizzle_y
1331 && swizzle_x == swizzle_z
1332 && swizzle_x == swizzle_w)
1334 TRACE(".%c", swizzle_chars[swizzle_x]);
1336 else
1338 TRACE(".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
1339 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
1344 /* Shared code in order to generate the bulk of the shader string.
1345 * NOTE: A description of how to parse tokens can be found on MSDN. */
1346 void shader_generate_main(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
1347 const struct wined3d_shader_reg_maps *reg_maps, const DWORD *byte_code, void *backend_ctx)
1349 struct wined3d_device *device = shader->device;
1350 const struct wined3d_shader_frontend *fe = shader->frontend;
1351 void *fe_data = shader->frontend_data;
1352 struct wined3d_shader_version shader_version;
1353 struct wined3d_shader_loop_state loop_state;
1354 struct wined3d_shader_instruction ins;
1355 struct wined3d_shader_tex_mx tex_mx;
1356 struct wined3d_shader_context ctx;
1357 const DWORD *ptr = byte_code;
1359 /* Initialize current parsing state. */
1360 tex_mx.current_row = 0;
1361 loop_state.current_depth = 0;
1362 loop_state.current_reg = 0;
1364 ctx.shader = shader;
1365 ctx.gl_info = &device->adapter->gl_info;
1366 ctx.reg_maps = reg_maps;
1367 ctx.buffer = buffer;
1368 ctx.tex_mx = &tex_mx;
1369 ctx.loop_state = &loop_state;
1370 ctx.backend_data = backend_ctx;
1371 ins.ctx = &ctx;
1373 fe->shader_read_header(fe_data, &ptr, &shader_version);
1375 while (!fe->shader_is_end(fe_data, &ptr))
1377 /* Read opcode. */
1378 fe->shader_read_instruction(fe_data, &ptr, &ins);
1380 /* Unknown opcode and its parameters. */
1381 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1383 TRACE("Skipping unrecognized instruction.\n");
1384 continue;
1387 if (ins.predicate)
1388 FIXME("Predicates not implemented.\n");
1390 /* Call appropriate function for output target */
1391 device->shader_backend->shader_handle_instruction(&ins);
1395 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param *dst)
1397 DWORD mmask = dst->modifiers;
1399 switch (dst->shift)
1401 case 0: break;
1402 case 13: TRACE("_d8"); break;
1403 case 14: TRACE("_d4"); break;
1404 case 15: TRACE("_d2"); break;
1405 case 1: TRACE("_x2"); break;
1406 case 2: TRACE("_x4"); break;
1407 case 3: TRACE("_x8"); break;
1408 default: TRACE("_unhandled_shift(%d)", dst->shift); break;
1411 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
1412 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
1413 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
1415 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
1416 if (mmask) FIXME("_unrecognized_modifier(%#x)", mmask);
1419 static void shader_dump_primitive_type(enum wined3d_primitive_type primitive_type)
1421 switch (primitive_type)
1423 case WINED3D_PT_UNDEFINED:
1424 TRACE("undefined");
1425 break;
1426 case WINED3D_PT_POINTLIST:
1427 TRACE("pointlist");
1428 break;
1429 case WINED3D_PT_LINELIST:
1430 TRACE("linelist");
1431 break;
1432 case WINED3D_PT_LINESTRIP:
1433 TRACE("linestrip");
1434 break;
1435 case WINED3D_PT_TRIANGLELIST:
1436 TRACE("trianglelist");
1437 break;
1438 case WINED3D_PT_TRIANGLESTRIP:
1439 TRACE("trianglestrip");
1440 break;
1441 case WINED3D_PT_TRIANGLEFAN:
1442 TRACE("trianglefan");
1443 break;
1444 case WINED3D_PT_LINELIST_ADJ:
1445 TRACE("linelist_adj");
1446 break;
1447 case WINED3D_PT_LINESTRIP_ADJ:
1448 TRACE("linestrip_adj");
1449 break;
1450 case WINED3D_PT_TRIANGLELIST_ADJ:
1451 TRACE("trianglelist_adj");
1452 break;
1453 case WINED3D_PT_TRIANGLESTRIP_ADJ:
1454 TRACE("trianglestrip_adj");
1455 break;
1456 default:
1457 TRACE("<unrecognized_primitive_type %#x>", primitive_type);
1458 break;
1462 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *byte_code)
1464 struct wined3d_shader_version shader_version;
1465 const DWORD *ptr = byte_code;
1466 const char *type_prefix;
1467 DWORD i;
1469 TRACE("Parsing %p.\n", byte_code);
1471 fe->shader_read_header(fe_data, &ptr, &shader_version);
1473 switch (shader_version.type)
1475 case WINED3D_SHADER_TYPE_VERTEX:
1476 type_prefix = "vs";
1477 break;
1479 case WINED3D_SHADER_TYPE_GEOMETRY:
1480 type_prefix = "gs";
1481 break;
1483 case WINED3D_SHADER_TYPE_PIXEL:
1484 type_prefix = "ps";
1485 break;
1487 default:
1488 FIXME("Unhandled shader type %#x.\n", shader_version.type);
1489 type_prefix = "unknown";
1490 break;
1493 TRACE("%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
1495 while (!fe->shader_is_end(fe_data, &ptr))
1497 struct wined3d_shader_instruction ins;
1499 fe->shader_read_instruction(fe_data, &ptr, &ins);
1500 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1502 TRACE("Skipping unrecognized instruction.\n");
1503 continue;
1506 if (ins.handler_idx == WINED3DSIH_DCL)
1508 shader_dump_decl_usage(&ins.declaration.semantic, &shader_version);
1509 shader_dump_ins_modifiers(&ins.declaration.semantic.reg);
1510 TRACE(" ");
1511 shader_dump_dst_param(&ins.declaration.semantic.reg, &shader_version);
1513 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
1515 TRACE("%s ", shader_opcode_names[ins.handler_idx]);
1516 shader_dump_src_param(&ins.declaration.src, &shader_version);
1517 TRACE(", %s", ins.flags & WINED3DSI_INDEXED_DYNAMIC ? "dynamicIndexed" : "immediateIndexed");
1519 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE
1520 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
1522 TRACE("%s ", shader_opcode_names[ins.handler_idx]);
1523 shader_dump_primitive_type(ins.declaration.primitive_type);
1525 else if (ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
1527 TRACE("%s %u", shader_opcode_names[ins.handler_idx], ins.declaration.count);
1529 else if (ins.handler_idx == WINED3DSIH_DEF)
1531 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(ins.dst[0].reg.type,
1532 ins.dst[0].reg.idx[0].offset),
1533 *(const float *)&ins.src[0].reg.immconst_data[0],
1534 *(const float *)&ins.src[0].reg.immconst_data[1],
1535 *(const float *)&ins.src[0].reg.immconst_data[2],
1536 *(const float *)&ins.src[0].reg.immconst_data[3]);
1538 else if (ins.handler_idx == WINED3DSIH_DEFI)
1540 TRACE("defi i%u = %d, %d, %d, %d", ins.dst[0].reg.idx[0].offset,
1541 ins.src[0].reg.immconst_data[0],
1542 ins.src[0].reg.immconst_data[1],
1543 ins.src[0].reg.immconst_data[2],
1544 ins.src[0].reg.immconst_data[3]);
1546 else if (ins.handler_idx == WINED3DSIH_DEFB)
1548 TRACE("defb b%u = %s", ins.dst[0].reg.idx[0].offset, ins.src[0].reg.immconst_data[0] ? "true" : "false");
1550 else
1552 if (ins.predicate)
1554 TRACE("(");
1555 shader_dump_src_param(ins.predicate, &shader_version);
1556 TRACE(") ");
1559 /* PixWin marks instructions with the coissue flag with a '+' */
1560 if (ins.coissue) TRACE("+");
1562 TRACE("%s", shader_opcode_names[ins.handler_idx]);
1564 if (ins.handler_idx == WINED3DSIH_IFC
1565 || ins.handler_idx == WINED3DSIH_BREAKC)
1567 switch (ins.flags)
1569 case WINED3D_SHADER_REL_OP_GT: TRACE("_gt"); break;
1570 case WINED3D_SHADER_REL_OP_EQ: TRACE("_eq"); break;
1571 case WINED3D_SHADER_REL_OP_GE: TRACE("_ge"); break;
1572 case WINED3D_SHADER_REL_OP_LT: TRACE("_lt"); break;
1573 case WINED3D_SHADER_REL_OP_NE: TRACE("_ne"); break;
1574 case WINED3D_SHADER_REL_OP_LE: TRACE("_le"); break;
1575 default: TRACE("_(%u)", ins.flags);
1578 else if (ins.handler_idx == WINED3DSIH_TEX
1579 && shader_version.major >= 2
1580 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
1582 TRACE("p");
1585 for (i = 0; i < ins.dst_count; ++i)
1587 shader_dump_ins_modifiers(&ins.dst[i]);
1588 TRACE(!i ? " " : ", ");
1589 shader_dump_dst_param(&ins.dst[i], &shader_version);
1592 /* Other source tokens */
1593 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
1595 TRACE(!i ? " " : ", ");
1596 shader_dump_src_param(&ins.src[i - ins.dst_count], &shader_version);
1599 TRACE("\n");
1603 static void shader_cleanup(struct wined3d_shader *shader)
1605 shader->device->shader_backend->shader_destroy(shader);
1606 HeapFree(GetProcessHeap(), 0, shader->reg_maps.constf);
1607 HeapFree(GetProcessHeap(), 0, shader->function);
1608 shader_delete_constant_list(&shader->constantsF);
1609 shader_delete_constant_list(&shader->constantsB);
1610 shader_delete_constant_list(&shader->constantsI);
1611 list_remove(&shader->shader_list_entry);
1613 if (shader->frontend && shader->frontend_data)
1614 shader->frontend->shader_free(shader->frontend_data);
1617 struct shader_none_priv
1619 const struct wined3d_vertex_pipe_ops *vertex_pipe;
1620 const struct fragment_pipeline *fragment_pipe;
1621 BOOL ffp_proj_control;
1624 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
1625 static void shader_none_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
1626 enum tex_types tex_type, const SIZE *ds_mask_size) {}
1627 static void shader_none_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info) {}
1628 static void shader_none_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count) {}
1629 static void shader_none_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count) {}
1630 static void shader_none_load_constants(void *shader_priv, struct wined3d_context *context,
1631 const struct wined3d_state *state) {}
1632 static void shader_none_destroy(struct wined3d_shader *shader) {}
1633 static void shader_none_free_context_data(struct wined3d_context *context) {}
1635 /* Context activation is done by the caller. */
1636 static void shader_none_select(void *shader_priv, struct wined3d_context *context,
1637 const struct wined3d_state *state)
1639 const struct wined3d_gl_info *gl_info = context->gl_info;
1640 struct shader_none_priv *priv = shader_priv;
1642 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
1643 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
1646 /* Context activation is done by the caller. */
1647 static void shader_none_disable(void *shader_priv, struct wined3d_context *context)
1649 struct shader_none_priv *priv = shader_priv;
1650 const struct wined3d_gl_info *gl_info = context->gl_info;
1652 priv->vertex_pipe->vp_enable(gl_info, FALSE);
1653 priv->fragment_pipe->enable_extension(gl_info, FALSE);
1655 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
1656 | (1 << WINED3D_SHADER_TYPE_VERTEX)
1657 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
1660 static HRESULT shader_none_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
1661 const struct fragment_pipeline *fragment_pipe)
1663 struct fragment_caps fragment_caps;
1664 void *vertex_priv, *fragment_priv;
1665 struct shader_none_priv *priv;
1667 if (!(priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv))))
1668 return E_OUTOFMEMORY;
1670 if (!(vertex_priv = vertex_pipe->vp_alloc(&none_shader_backend, priv)))
1672 ERR("Failed to initialize vertex pipe.\n");
1673 HeapFree(GetProcessHeap(), 0, priv);
1674 return E_FAIL;
1677 if (!(fragment_priv = fragment_pipe->alloc_private(&none_shader_backend, priv)))
1679 ERR("Failed to initialize fragment pipe.\n");
1680 vertex_pipe->vp_free(device);
1681 HeapFree(GetProcessHeap(), 0, priv);
1682 return E_FAIL;
1685 priv->vertex_pipe = vertex_pipe;
1686 priv->fragment_pipe = fragment_pipe;
1687 fragment_pipe->get_caps(&device->adapter->gl_info, &fragment_caps);
1688 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
1690 device->vertex_priv = vertex_priv;
1691 device->fragment_priv = fragment_priv;
1692 device->shader_priv = priv;
1694 return WINED3D_OK;
1697 static void shader_none_free(struct wined3d_device *device)
1699 struct shader_none_priv *priv = device->shader_priv;
1701 priv->fragment_pipe->free_private(device);
1702 priv->vertex_pipe->vp_free(device);
1703 HeapFree(GetProcessHeap(), 0, priv);
1706 static BOOL shader_none_allocate_context_data(struct wined3d_context *context)
1708 return TRUE;
1711 static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
1713 /* Set the shader caps to 0 for the none shader backend */
1714 caps->vs_version = 0;
1715 caps->gs_version = 0;
1716 caps->ps_version = 0;
1717 caps->vs_uniform_count = 0;
1718 caps->ps_uniform_count = 0;
1719 caps->ps_1x_max_value = 0.0f;
1720 caps->wined3d_caps = 0;
1723 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1725 /* We "support" every possible fixup, since we don't support any shader
1726 * model, and will never have to actually sample a texture. */
1727 return TRUE;
1730 static BOOL shader_none_has_ffp_proj_control(void *shader_priv)
1732 struct shader_none_priv *priv = shader_priv;
1734 return priv->ffp_proj_control;
1737 const struct wined3d_shader_backend_ops none_shader_backend =
1739 shader_none_handle_instruction,
1740 shader_none_select,
1741 shader_none_disable,
1742 shader_none_select_depth_blt,
1743 shader_none_deselect_depth_blt,
1744 shader_none_update_float_vertex_constants,
1745 shader_none_update_float_pixel_constants,
1746 shader_none_load_constants,
1747 shader_none_destroy,
1748 shader_none_alloc,
1749 shader_none_free,
1750 shader_none_allocate_context_data,
1751 shader_none_free_context_data,
1752 shader_none_get_caps,
1753 shader_none_color_fixup_supported,
1754 shader_none_has_ffp_proj_control,
1757 static HRESULT shader_set_function(struct wined3d_shader *shader, const DWORD *byte_code,
1758 const struct wined3d_shader_signature *output_signature, DWORD float_const_count,
1759 enum wined3d_shader_type type, unsigned int max_version)
1761 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1762 const struct wined3d_shader_frontend *fe;
1763 HRESULT hr;
1764 unsigned int backend_version;
1765 const struct wined3d_d3d_info *d3d_info = &shader->device->adapter->d3d_info;
1767 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1768 shader, byte_code, output_signature, float_const_count);
1770 fe = shader_select_frontend(*byte_code);
1771 if (!fe)
1773 FIXME("Unable to find frontend for shader.\n");
1774 return WINED3DERR_INVALIDCALL;
1776 shader->frontend = fe;
1777 shader->frontend_data = fe->shader_init(byte_code, output_signature);
1778 if (!shader->frontend_data)
1780 FIXME("Failed to initialize frontend.\n");
1781 return WINED3DERR_INVALIDCALL;
1784 /* First pass: trace shader. */
1785 if (TRACE_ON(d3d_shader))
1786 shader_trace_init(fe, shader->frontend_data, byte_code);
1788 /* Initialize immediate constant lists. */
1789 list_init(&shader->constantsF);
1790 list_init(&shader->constantsB);
1791 list_init(&shader->constantsI);
1792 shader->lconst_inf_or_nan = FALSE;
1794 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1795 hr = shader_get_registers_used(shader, fe,
1796 reg_maps, shader->input_signature, shader->output_signature,
1797 byte_code, float_const_count);
1798 if (FAILED(hr)) return hr;
1800 if (reg_maps->shader_version.type != type)
1802 WARN("Wrong shader type %d.\n", reg_maps->shader_version.type);
1803 return WINED3DERR_INVALIDCALL;
1805 if (reg_maps->shader_version.major > max_version)
1807 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps->shader_version.major);
1808 return WINED3DERR_INVALIDCALL;
1810 switch (type)
1812 case WINED3D_SHADER_TYPE_VERTEX:
1813 backend_version = d3d_info->limits.vs_version;
1814 break;
1815 case WINED3D_SHADER_TYPE_GEOMETRY:
1816 backend_version = d3d_info->limits.gs_version;
1817 break;
1818 case WINED3D_SHADER_TYPE_PIXEL:
1819 backend_version = d3d_info->limits.ps_version;
1820 break;
1821 default:
1822 FIXME("No backend version-checking for this shader type\n");
1823 backend_version = 0;
1825 if (reg_maps->shader_version.major > backend_version)
1827 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
1828 reg_maps->shader_version.major, reg_maps->shader_version.minor);
1829 return WINED3DERR_INVALIDCALL;
1832 shader->function = HeapAlloc(GetProcessHeap(), 0, shader->functionLength);
1833 if (!shader->function)
1834 return E_OUTOFMEMORY;
1835 memcpy(shader->function, byte_code, shader->functionLength);
1837 return WINED3D_OK;
1840 ULONG CDECL wined3d_shader_incref(struct wined3d_shader *shader)
1842 ULONG refcount = InterlockedIncrement(&shader->ref);
1844 TRACE("%p increasing refcount to %u.\n", shader, refcount);
1846 return refcount;
1849 ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
1851 ULONG refcount = InterlockedDecrement(&shader->ref);
1853 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1855 if (!refcount)
1857 shader_cleanup(shader);
1858 shader->parent_ops->wined3d_object_destroyed(shader->parent);
1859 HeapFree(GetProcessHeap(), 0, shader);
1862 return refcount;
1865 void * CDECL wined3d_shader_get_parent(const struct wined3d_shader *shader)
1867 TRACE("shader %p.\n", shader);
1869 return shader->parent;
1872 HRESULT CDECL wined3d_shader_get_byte_code(const struct wined3d_shader *shader,
1873 void *byte_code, UINT *byte_code_size)
1875 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader, byte_code, byte_code_size);
1877 if (!byte_code)
1879 *byte_code_size = shader->functionLength;
1880 return WINED3D_OK;
1883 if (*byte_code_size < shader->functionLength)
1885 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
1886 * than the required size we should write the required size and
1887 * return D3DERR_MOREDATA. That's not actually true. */
1888 return WINED3DERR_INVALIDCALL;
1891 memcpy(byte_code, shader->function, shader->functionLength);
1893 return WINED3D_OK;
1896 /* Set local constants for d3d8 shaders. */
1897 HRESULT CDECL wined3d_shader_set_local_constants_float(struct wined3d_shader *shader,
1898 UINT start_idx, const float *src_data, UINT count)
1900 UINT end_idx = start_idx + count;
1901 UINT i;
1903 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader, start_idx, src_data, count);
1905 if (end_idx > shader->limits->constant_float)
1907 WARN("end_idx %u > float constants limit %u.\n",
1908 end_idx, shader->limits->constant_float);
1909 end_idx = shader->limits->constant_float;
1912 for (i = start_idx; i < end_idx; ++i)
1914 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1915 float *value;
1916 if (!lconst)
1917 return E_OUTOFMEMORY;
1919 lconst->idx = i;
1920 value = (float *)lconst->value;
1921 memcpy(value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
1922 list_add_head(&shader->constantsF, &lconst->entry);
1924 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
1925 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
1927 shader->lconst_inf_or_nan = TRUE;
1931 return WINED3D_OK;
1934 void find_vs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
1935 WORD swizzle_map, struct vs_compile_args *args)
1937 args->fog_src = state->render_states[WINED3D_RS_FOGTABLEMODE]
1938 == WINED3D_FOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
1939 args->clip_enabled = state->render_states[WINED3D_RS_CLIPPING]
1940 && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
1941 args->swizzle_map = swizzle_map;
1944 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
1946 if (usage_idx1 != usage_idx2)
1947 return FALSE;
1948 if (usage1 == usage2)
1949 return TRUE;
1950 if (usage1 == WINED3D_DECL_USAGE_POSITION && usage2 == WINED3D_DECL_USAGE_POSITIONT)
1951 return TRUE;
1952 if (usage2 == WINED3D_DECL_USAGE_POSITION && usage1 == WINED3D_DECL_USAGE_POSITIONT)
1953 return TRUE;
1955 return FALSE;
1958 BOOL vshader_get_input(const struct wined3d_shader *shader,
1959 BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
1961 WORD map = shader->reg_maps.input_registers;
1962 unsigned int i;
1964 for (i = 0; map; map >>= 1, ++i)
1966 if (!(map & 1)) continue;
1968 if (match_usage(shader->u.vs.attributes[i].usage,
1969 shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
1971 *regnum = i;
1972 return TRUE;
1975 return FALSE;
1978 static HRESULT vertexshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
1979 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1980 void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
1982 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1983 unsigned int i;
1984 HRESULT hr;
1985 WORD map;
1986 const DWORD vs_uniform_count = device->adapter->d3d_info.limits.vs_uniform_count;
1988 if (!byte_code) return WINED3DERR_INVALIDCALL;
1990 shader_init(shader, device, parent, parent_ops);
1991 hr = shader_set_function(shader, byte_code, output_signature, vs_uniform_count,
1992 WINED3D_SHADER_TYPE_VERTEX, max_version);
1993 if (FAILED(hr))
1995 WARN("Failed to set function, hr %#x.\n", hr);
1996 shader_cleanup(shader);
1997 return hr;
2000 map = reg_maps->input_registers;
2001 for (i = 0; map; map >>= 1, ++i)
2003 if (!(map & 1) || !shader->input_signature[i].semantic_name)
2004 continue;
2006 shader->u.vs.attributes[i].usage =
2007 shader_usage_from_semantic_name(shader->input_signature[i].semantic_name);
2008 shader->u.vs.attributes[i].usage_idx = shader->input_signature[i].semantic_idx;
2011 if (output_signature)
2013 for (i = 0; i < output_signature->element_count; ++i)
2015 struct wined3d_shader_signature_element *e = &output_signature->elements[i];
2016 reg_maps->output_registers |= 1 << e->register_idx;
2017 shader->output_signature[e->register_idx] = *e;
2021 shader->load_local_constsF = (reg_maps->usesrelconstF && !list_empty(&shader->constantsF)) ||
2022 shader->lconst_inf_or_nan;
2024 return WINED3D_OK;
2027 static HRESULT geometryshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2028 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
2029 void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
2031 HRESULT hr;
2033 shader_init(shader, device, parent, parent_ops);
2034 hr = shader_set_function(shader, byte_code, output_signature, 0,
2035 WINED3D_SHADER_TYPE_GEOMETRY, max_version);
2036 if (FAILED(hr))
2038 WARN("Failed to set function, hr %#x.\n", hr);
2039 shader_cleanup(shader);
2040 return hr;
2043 shader->load_local_constsF = shader->lconst_inf_or_nan;
2045 return WINED3D_OK;
2048 void find_ps_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
2049 BOOL position_transformed, struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
2051 const struct wined3d_texture *texture;
2052 UINT i;
2054 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
2055 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && state->render_states[WINED3D_RS_SRGBWRITEENABLE])
2057 const struct wined3d_format *rt_format = state->fb->render_targets[0]->format;
2058 if (rt_format->flags & WINED3DFMT_FLAG_SRGB_WRITE)
2060 static unsigned int warned = 0;
2062 args->srgb_correction = 1;
2063 if (state->render_states[WINED3D_RS_ALPHABLENDENABLE] && !warned++)
2064 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
2065 "support, expect rendering artifacts.\n");
2069 if (shader->reg_maps.shader_version.major == 1
2070 && shader->reg_maps.shader_version.minor <= 3)
2072 for (i = 0; i < shader->limits->sampler; ++i)
2074 DWORD flags = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
2076 if (flags & WINED3D_TTFF_PROJECTED)
2078 DWORD tex_transform = flags & ~WINED3D_TTFF_PROJECTED;
2080 if (!state->shader[WINED3D_SHADER_TYPE_VERTEX])
2082 unsigned int j;
2083 unsigned int index = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
2084 DWORD max_valid = WINED3D_TTFF_COUNT4;
2085 enum wined3d_sampler_texture_type sampler_type = shader->reg_maps.sampler_type[i];
2087 for (j = 0; j < state->vertex_declaration->element_count; ++j)
2089 struct wined3d_vertex_declaration_element *element =
2090 &state->vertex_declaration->elements[j];
2092 if (element->usage == WINED3D_DECL_USAGE_TEXCOORD
2093 && element->usage_idx == index)
2095 max_valid = element->format->component_count;
2096 break;
2099 if (!tex_transform || tex_transform > max_valid)
2101 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2102 tex_transform, max_valid);
2103 tex_transform = max_valid;
2105 if ((sampler_type == WINED3DSTT_1D && tex_transform > WINED3D_TTFF_COUNT1)
2106 || (sampler_type == WINED3DSTT_2D && tex_transform > WINED3D_TTFF_COUNT2)
2107 || (sampler_type == WINED3DSTT_VOLUME && tex_transform > WINED3D_TTFF_COUNT3))
2108 tex_transform |= WINED3D_PSARGS_PROJECTED;
2109 else
2111 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2112 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2113 i, tex_transform, sampler_type);
2116 else
2117 tex_transform = WINED3D_TTFF_COUNT4 | WINED3D_PSARGS_PROJECTED;
2119 args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
2123 if (shader->reg_maps.shader_version.major == 1
2124 && shader->reg_maps.shader_version.minor <= 4)
2126 for (i = 0; i < shader->limits->sampler; ++i)
2128 const struct wined3d_texture *texture = state->textures[i];
2130 if (!shader->reg_maps.sampler_type[i])
2131 continue;
2133 /* Treat unbound textures as 2D. The dummy texture will provide
2134 * the proper sample value. The tex_types bitmap defaults to
2135 * 2D because of the memset. */
2136 if (!texture)
2137 continue;
2139 switch (texture->target)
2141 /* RECT textures are distinguished from 2D textures via np2_fixup */
2142 case GL_TEXTURE_RECTANGLE_ARB:
2143 case GL_TEXTURE_2D:
2144 break;
2146 case GL_TEXTURE_3D:
2147 args->tex_types |= WINED3D_SHADER_TEX_3D << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
2148 break;
2150 case GL_TEXTURE_CUBE_MAP_ARB:
2151 args->tex_types |= WINED3D_SHADER_TEX_CUBE << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
2152 break;
2157 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2159 if (!shader->reg_maps.sampler_type[i])
2160 continue;
2162 texture = state->textures[i];
2163 if (!texture)
2165 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
2166 continue;
2168 args->color_fixup[i] = texture->resource.format->color_fixup;
2170 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
2171 args->shadow |= 1 << i;
2173 /* Flag samplers that need NP2 texcoord fixup. */
2174 if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
2175 args->np2_fixup |= (1 << i);
2177 if (shader->reg_maps.shader_version.major >= 3)
2179 if (position_transformed)
2180 args->vp_mode = pretransformed;
2181 else if (use_vs(state))
2182 args->vp_mode = vertexshader;
2183 else
2184 args->vp_mode = fixedfunction;
2185 args->fog = WINED3D_FFP_PS_FOG_OFF;
2187 else
2189 args->vp_mode = vertexshader;
2190 if (state->render_states[WINED3D_RS_FOGENABLE])
2192 switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
2194 case WINED3D_FOG_NONE:
2195 if (position_transformed || use_vs(state))
2197 args->fog = WINED3D_FFP_PS_FOG_LINEAR;
2198 break;
2201 switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
2203 case WINED3D_FOG_NONE: /* Fall through. */
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;
2208 break;
2210 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
2211 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
2212 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
2215 else
2217 args->fog = WINED3D_FFP_PS_FOG_OFF;
2222 static HRESULT pixelshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2223 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
2224 void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
2226 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2227 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
2228 HRESULT hr;
2229 const DWORD ps_uniform_count = device->adapter->d3d_info.limits.ps_uniform_count;
2231 if (!byte_code) return WINED3DERR_INVALIDCALL;
2233 shader_init(shader, device, parent, parent_ops);
2234 hr = shader_set_function(shader, byte_code, output_signature, ps_uniform_count,
2235 WINED3D_SHADER_TYPE_PIXEL, max_version);
2236 if (FAILED(hr))
2238 WARN("Failed to set function, hr %#x.\n", hr);
2239 shader_cleanup(shader);
2240 return hr;
2243 for (i = 0; i < MAX_REG_INPUT; ++i)
2245 if (shader->u.ps.input_reg_used[i])
2247 ++num_regs_used;
2248 highest_reg_used = i;
2252 /* Don't do any register mapping magic if it is not needed, or if we can't
2253 * achieve anything anyway */
2254 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
2255 || num_regs_used > (gl_info->limits.glsl_varyings / 4))
2257 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
2259 /* This happens with relative addressing. The input mapper function
2260 * warns about this if the higher registers are declared too, so
2261 * don't write a FIXME here */
2262 WARN("More varying registers used than supported\n");
2265 for (i = 0; i < MAX_REG_INPUT; ++i)
2267 shader->u.ps.input_reg_map[i] = i;
2270 shader->u.ps.declared_in_count = highest_reg_used + 1;
2272 else
2274 shader->u.ps.declared_in_count = 0;
2275 for (i = 0; i < MAX_REG_INPUT; ++i)
2277 if (shader->u.ps.input_reg_used[i])
2278 shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
2279 else shader->u.ps.input_reg_map[i] = ~0U;
2283 shader->load_local_constsF = shader->lconst_inf_or_nan;
2285 return WINED3D_OK;
2288 void pixelshader_update_samplers(struct wined3d_shader *shader, WORD tex_types)
2290 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2291 enum wined3d_sampler_texture_type *sampler_type = reg_maps->sampler_type;
2292 unsigned int i;
2294 if (reg_maps->shader_version.major != 1) return;
2296 for (i = 0; i < shader->limits->sampler; ++i)
2298 /* We don't sample from this sampler. */
2299 if (!sampler_type[i]) continue;
2301 switch ((tex_types >> i * WINED3D_PSARGS_TEXTYPE_SHIFT) & WINED3D_PSARGS_TEXTYPE_MASK)
2303 case WINED3D_SHADER_TEX_2D:
2304 sampler_type[i] = WINED3DSTT_2D;
2305 break;
2307 case WINED3D_SHADER_TEX_3D:
2308 sampler_type[i] = WINED3DSTT_VOLUME;
2309 break;
2311 case WINED3D_SHADER_TEX_CUBE:
2312 sampler_type[i] = WINED3DSTT_CUBE;
2313 break;
2318 HRESULT CDECL wined3d_shader_create_gs(struct wined3d_device *device, const DWORD *byte_code,
2319 const struct wined3d_shader_signature *output_signature, void *parent,
2320 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2322 struct wined3d_shader *object;
2323 HRESULT hr;
2325 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2326 device, byte_code, output_signature, parent, parent_ops, shader);
2328 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2329 if (!object)
2330 return E_OUTOFMEMORY;
2332 hr = geometryshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2333 if (FAILED(hr))
2335 WARN("Failed to initialize geometry shader, hr %#x.\n", hr);
2336 HeapFree(GetProcessHeap(), 0, object);
2337 return hr;
2340 TRACE("Created geometry shader %p.\n", object);
2341 *shader = object;
2343 return WINED3D_OK;
2346 HRESULT CDECL wined3d_shader_create_ps(struct wined3d_device *device, const DWORD *byte_code,
2347 const struct wined3d_shader_signature *output_signature, void *parent,
2348 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2350 struct wined3d_shader *object;
2351 HRESULT hr;
2353 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2354 device, byte_code, output_signature, parent, parent_ops, shader);
2356 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2357 if (!object)
2358 return E_OUTOFMEMORY;
2360 hr = pixelshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2361 if (FAILED(hr))
2363 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2364 HeapFree(GetProcessHeap(), 0, object);
2365 return hr;
2368 TRACE("Created pixel shader %p.\n", object);
2369 *shader = object;
2371 return WINED3D_OK;
2374 HRESULT CDECL wined3d_shader_create_vs(struct wined3d_device *device, const DWORD *byte_code,
2375 const struct wined3d_shader_signature *output_signature, void *parent,
2376 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2378 struct wined3d_shader *object;
2379 HRESULT hr;
2381 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2382 device, byte_code, output_signature, parent, parent_ops, shader);
2384 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2385 if (!object)
2386 return E_OUTOFMEMORY;
2388 hr = vertexshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2389 if (FAILED(hr))
2391 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2392 HeapFree(GetProcessHeap(), 0, object);
2393 return hr;
2396 TRACE("Created vertex shader %p.\n", object);
2397 *shader = object;
2399 return WINED3D_OK;