makefiles: Always expand the library name for cross-compiled import libraries.
[wine.git] / dlls / wined3d / shader.c
blobdb30868d97d2489c2851a5470f36dcb6758c9253
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_GLOBAL_FLAGS */ "dcl_globalFlags",
59 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ "dcl_immediateConstantBuffer",
60 /* WINED3DSIH_DCL_INPUT */ "dcl_input",
61 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
62 /* WINED3DSIH_DCL_INPUT_PS */ "dcl_input_ps",
63 /* WINED3DSIH_DCL_INPUT_PS_SGV */ "dcl_input_ps_sgv",
64 /* WINED3DSIH_DCL_INPUT_PS_SIV */ "dcl_input_ps_siv",
65 /* WINED3DSIH_DCL_INPUT_SGV */ "dcl_input_sgv",
66 /* WINED3DSIH_DCL_INPUT_SIV */ "dcl_input_siv",
67 /* WINED3DSIH_DCL_OUTPUT */ "dcl_output",
68 /* WINED3DSIH_DCL_OUTPUT_SIV */ "dcl_output_siv",
69 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
70 /* WINED3DSIH_DCL_SAMPLER */ "dcl_sampler",
71 /* WINED3DSIH_DCL_TEMPS */ "dcl_temps",
72 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
73 /* WINED3DSIH_DEF */ "def",
74 /* WINED3DSIH_DEFB */ "defb",
75 /* WINED3DSIH_DEFI */ "defi",
76 /* WINED3DSIH_DIV */ "div",
77 /* WINED3DSIH_DP2 */ "dp2",
78 /* WINED3DSIH_DP2ADD */ "dp2add",
79 /* WINED3DSIH_DP3 */ "dp3",
80 /* WINED3DSIH_DP4 */ "dp4",
81 /* WINED3DSIH_DST */ "dst",
82 /* WINED3DSIH_DSX */ "dsx",
83 /* WINED3DSIH_DSX_COARSE */ "deriv_rtx_coarse",
84 /* WINED3DSIH_DSX_FINE */ "deriv_rtx_fine",
85 /* WINED3DSIH_DSY */ "dsy",
86 /* WINED3DSIH_DSY_COARSE */ "deriv_rty_coarse",
87 /* WINED3DSIH_DSY_FINE */ "deriv_rty_fine",
88 /* WINED3DSIH_ELSE */ "else",
89 /* WINED3DSIH_EMIT */ "emit",
90 /* WINED3DSIH_ENDIF */ "endif",
91 /* WINED3DSIH_ENDLOOP */ "endloop",
92 /* WINED3DSIH_ENDREP */ "endrep",
93 /* WINED3DSIH_EQ */ "eq",
94 /* WINED3DSIH_EXP */ "exp",
95 /* WINED3DSIH_EXPP */ "expp",
96 /* WINED3DSIH_FRC */ "frc",
97 /* WINED3DSIH_FTOI */ "ftoi",
98 /* WINED3DSIH_FTOU */ "ftou",
99 /* WINED3DSIH_GE */ "ge",
100 /* WINED3DSIH_IADD */ "iadd",
101 /* WINED3DSIH_IEQ */ "ieq",
102 /* WINED3DSIH_IF */ "if",
103 /* WINED3DSIH_IFC */ "ifc",
104 /* WINED3DSIH_IGE */ "ige",
105 /* WINED3DSIH_ILT */ "ilt",
106 /* WINED3DSIH_IMAD */ "imad",
107 /* WINED3DSIH_IMAX */ "imax",
108 /* WINED3DSIH_IMIN */ "imin",
109 /* WINED3DSIH_IMUL */ "imul",
110 /* WINED3DSIH_INE */ "ine",
111 /* WINED3DSIH_INEG */ "ineg",
112 /* WINED3DSIH_ISHL */ "ishl",
113 /* WINED3DSIH_ITOF */ "itof",
114 /* WINED3DSIH_LABEL */ "label",
115 /* WINED3DSIH_LD */ "ld",
116 /* WINED3DSIH_LD_STRUCTURED */ "ld_structured",
117 /* WINED3DSIH_LIT */ "lit",
118 /* WINED3DSIH_LOG */ "log",
119 /* WINED3DSIH_LOGP */ "logp",
120 /* WINED3DSIH_LOOP */ "loop",
121 /* WINED3DSIH_LRP */ "lrp",
122 /* WINED3DSIH_LT */ "lt",
123 /* WINED3DSIH_M3x2 */ "m3x2",
124 /* WINED3DSIH_M3x3 */ "m3x3",
125 /* WINED3DSIH_M3x4 */ "m3x4",
126 /* WINED3DSIH_M4x3 */ "m4x3",
127 /* WINED3DSIH_M4x4 */ "m4x4",
128 /* WINED3DSIH_MAD */ "mad",
129 /* WINED3DSIH_MAX */ "max",
130 /* WINED3DSIH_MIN */ "min",
131 /* WINED3DSIH_MOV */ "mov",
132 /* WINED3DSIH_MOVA */ "mova",
133 /* WINED3DSIH_MOVC */ "movc",
134 /* WINED3DSIH_MUL */ "mul",
135 /* WINED3DSIH_NE */ "ne",
136 /* WINED3DSIH_NOP */ "nop",
137 /* WINED3DSIH_NOT */ "not",
138 /* WINED3DSIH_NRM */ "nrm",
139 /* WINED3DSIH_OR */ "or",
140 /* WINED3DSIH_PHASE */ "phase",
141 /* WINED3DSIH_POW */ "pow",
142 /* WINED3DSIH_RCP */ "rcp",
143 /* WINED3DSIH_REP */ "rep",
144 /* WINED3DSIH_RESINFO */ "resinfo",
145 /* WINED3DSIH_RET */ "ret",
146 /* WINED3DSIH_ROUND_NI */ "round_ni",
147 /* WINED3DSIH_ROUND_PI */ "round_pi",
148 /* WINED3DSIH_ROUND_Z */ "round_z",
149 /* WINED3DSIH_RSQ */ "rsq",
150 /* WINED3DSIH_SAMPLE */ "sample",
151 /* WINED3DSIH_SAMPLE_B */ "sample_b",
152 /* WINED3DSIH_SAMPLE_C */ "sample_c",
153 /* WINED3DSIH_SAMPLE_C_LZ */ "sample_c_lz",
154 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
155 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
156 /* WINED3DSIH_SETP */ "setp",
157 /* WINED3DSIH_SGE */ "sge",
158 /* WINED3DSIH_SGN */ "sgn",
159 /* WINED3DSIH_SINCOS */ "sincos",
160 /* WINED3DSIH_SLT */ "slt",
161 /* WINED3DSIH_SQRT */ "sqrt",
162 /* WINED3DSIH_SUB */ "sub",
163 /* WINED3DSIH_TEX */ "texld",
164 /* WINED3DSIH_TEXBEM */ "texbem",
165 /* WINED3DSIH_TEXBEML */ "texbeml",
166 /* WINED3DSIH_TEXCOORD */ "texcrd",
167 /* WINED3DSIH_TEXDEPTH */ "texdepth",
168 /* WINED3DSIH_TEXDP3 */ "texdp3",
169 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
170 /* WINED3DSIH_TEXKILL */ "texkill",
171 /* WINED3DSIH_TEXLDD */ "texldd",
172 /* WINED3DSIH_TEXLDL */ "texldl",
173 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
174 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
175 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
176 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
177 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
178 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
179 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
180 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
181 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
182 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
183 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
184 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
185 /* WINED3DSIH_UDIV */ "udiv",
186 /* WINED3DSIH_UGE */ "uge",
187 /* WINED3DSIH_USHR */ "ushr",
188 /* WINED3DSIH_UTOF */ "utof",
189 /* WINED3DSIH_XOR */ "xor",
192 static const char * const semantic_names[] =
194 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
195 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
196 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
197 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
198 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
199 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
200 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
201 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
202 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
203 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
204 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
205 /* WINED3D_DECL_USAGE_FOG */ "FOG",
206 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
207 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
210 static const struct
212 enum wined3d_sysval_semantic sysval_semantic;
213 const char *sysval_name;
215 sysval_semantic_names[] =
217 {WINED3D_SV_POSITION, "SV_Position"},
218 {WINED3D_SV_INSTANCEID, "SV_InstanceID"},
219 {WINED3D_SV_PRIMITIVEID, "SV_PrimitiveID"},
220 {WINED3D_SV_ISFRONTFACE, "SV_IsFrontFace"},
221 {WINED3D_SV_SAMPLEINDEX, "SV_SampleIndex"},
224 static void shader_dump_src_param(struct wined3d_string_buffer *buffer,
225 const struct wined3d_shader_src_param *param, const struct wined3d_shader_version *shader_version);
227 const char *debug_d3dshaderinstructionhandler(enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx)
229 if (handler_idx >= sizeof(shader_opcode_names) / sizeof(*shader_opcode_names))
230 return wine_dbg_sprintf("UNRECOGNIZED(%#x)", handler_idx);
232 return shader_opcode_names[handler_idx];
235 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage)
237 if (usage >= sizeof(semantic_names) / sizeof(*semantic_names))
239 FIXME("Unrecognized usage %#x.\n", usage);
240 return "UNRECOGNIZED";
243 return semantic_names[usage];
246 static enum wined3d_decl_usage shader_usage_from_semantic_name(const char *name)
248 unsigned int i;
250 for (i = 0; i < sizeof(semantic_names) / sizeof(*semantic_names); ++i)
252 if (!strcmp(name, semantic_names[i])) return i;
255 return ~0U;
258 BOOL shader_match_semantic(const char *semantic_name, enum wined3d_decl_usage usage)
260 return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
263 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
264 const struct wined3d_shader_semantic *s)
266 e->semantic_name = shader_semantic_name_from_usage(s->usage);
267 e->semantic_idx = s->usage_idx;
268 e->sysval_semantic = 0;
269 e->component_type = 0;
270 e->register_idx = s->reg.reg.idx[0].offset;
271 e->mask = s->reg.write_mask;
274 static void shader_signature_from_usage(struct wined3d_shader_signature_element *e,
275 enum wined3d_decl_usage usage, UINT usage_idx, UINT reg_idx, DWORD write_mask)
277 e->semantic_name = shader_semantic_name_from_usage(usage);
278 e->semantic_idx = usage_idx;
279 e->sysval_semantic = 0;
280 e->component_type = 0;
281 e->register_idx = reg_idx;
282 e->mask = write_mask;
285 static const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token)
287 switch (version_token >> 16)
289 case WINED3D_SM1_VS:
290 case WINED3D_SM1_PS:
291 return &sm1_shader_frontend;
293 case WINED3D_SM4_PS:
294 case WINED3D_SM4_VS:
295 case WINED3D_SM4_GS:
296 return &sm4_shader_frontend;
298 default:
299 FIXME("Unrecognised version token %#x\n", version_token);
300 return NULL;
304 void string_buffer_clear(struct wined3d_string_buffer *buffer)
306 buffer->buffer[0] = '\0';
307 buffer->content_size = 0;
310 BOOL string_buffer_init(struct wined3d_string_buffer *buffer)
312 buffer->buffer_size = 32;
313 if (!(buffer->buffer = HeapAlloc(GetProcessHeap(), 0, buffer->buffer_size)))
315 ERR("Failed to allocate shader buffer memory.\n");
316 return FALSE;
319 string_buffer_clear(buffer);
320 return TRUE;
323 void string_buffer_free(struct wined3d_string_buffer *buffer)
325 HeapFree(GetProcessHeap(), 0, buffer->buffer);
328 BOOL string_buffer_resize(struct wined3d_string_buffer *buffer, int rc)
330 char *new_buffer;
331 unsigned int new_buffer_size = buffer->buffer_size * 2;
333 while (rc > 0 && (unsigned int)rc >= new_buffer_size - buffer->content_size)
334 new_buffer_size *= 2;
335 if (!(new_buffer = HeapReAlloc(GetProcessHeap(), 0, buffer->buffer, new_buffer_size)))
337 ERR("Failed to grow buffer.\n");
338 buffer->buffer[buffer->content_size] = '\0';
339 return FALSE;
341 buffer->buffer = new_buffer;
342 buffer->buffer_size = new_buffer_size;
343 return TRUE;
346 int shader_vaddline(struct wined3d_string_buffer *buffer, const char *format, va_list args)
348 unsigned int rem;
349 int rc;
351 rem = buffer->buffer_size - buffer->content_size;
352 rc = vsnprintf(&buffer->buffer[buffer->content_size], rem, format, args);
353 if (rc < 0 /* C89 */ || (unsigned int)rc >= rem /* C99 */)
354 return rc;
356 buffer->content_size += rc;
357 return 0;
360 int shader_addline(struct wined3d_string_buffer *buffer, const char *format, ...)
362 va_list args;
363 int ret;
365 for (;;)
367 va_start(args, format);
368 ret = shader_vaddline(buffer, format, args);
369 va_end(args);
370 if (!ret)
371 return ret;
372 if (!string_buffer_resize(buffer, ret))
373 return -1;
377 struct wined3d_string_buffer *string_buffer_get(struct wined3d_string_buffer_list *list)
379 struct wined3d_string_buffer *buffer;
381 if (list_empty(&list->list))
383 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer));
384 if (!buffer || !string_buffer_init(buffer))
386 ERR("Couldn't allocate buffer for temporary string.\n");
387 if (buffer)
388 HeapFree(GetProcessHeap(), 0, buffer);
389 return NULL;
392 else
394 buffer = LIST_ENTRY(list_head(&list->list), struct wined3d_string_buffer, entry);
395 list_remove(&buffer->entry);
397 string_buffer_clear(buffer);
398 return buffer;
401 static int string_buffer_vsprintf(struct wined3d_string_buffer *buffer, const char *format, va_list args)
403 if (!buffer)
404 return 0;
405 string_buffer_clear(buffer);
406 return shader_vaddline(buffer, format, args);
409 void string_buffer_sprintf(struct wined3d_string_buffer *buffer, const char *format, ...)
411 va_list args;
412 int ret;
414 for (;;)
416 va_start(args, format);
417 ret = string_buffer_vsprintf(buffer, format, args);
418 va_end(args);
419 if (!ret)
420 return;
421 if (!string_buffer_resize(buffer, ret))
422 return;
426 void string_buffer_release(struct wined3d_string_buffer_list *list, struct wined3d_string_buffer *buffer)
428 if (!buffer)
429 return;
430 list_add_head(&list->list, &buffer->entry);
433 void string_buffer_list_init(struct wined3d_string_buffer_list *list)
435 list_init(&list->list);
438 void string_buffer_list_cleanup(struct wined3d_string_buffer_list *list)
440 struct wined3d_string_buffer *buffer, *buffer_next;
442 LIST_FOR_EACH_ENTRY_SAFE(buffer, buffer_next, &list->list, struct wined3d_string_buffer, entry)
444 string_buffer_free(buffer);
445 HeapFree(GetProcessHeap(), 0, buffer);
447 list_init(&list->list);
450 /* Convert floating point offset relative to a register file to an absolute
451 * offset for float constants. */
452 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type, UINT register_idx)
454 switch (register_type)
456 case WINED3DSPR_CONST: return register_idx;
457 case WINED3DSPR_CONST2: return 2048 + register_idx;
458 case WINED3DSPR_CONST3: return 4096 + register_idx;
459 case WINED3DSPR_CONST4: return 6144 + register_idx;
460 default:
461 FIXME("Unsupported register type: %u.\n", register_type);
462 return register_idx;
466 static void shader_delete_constant_list(struct list *clist)
468 struct wined3d_shader_lconst *constant, *constant_next;
470 LIST_FOR_EACH_ENTRY_SAFE(constant, constant_next, clist, struct wined3d_shader_lconst, entry)
471 HeapFree(GetProcessHeap(), 0, constant);
472 list_init(clist);
475 static void shader_set_limits(struct wined3d_shader *shader)
477 static const struct limits_entry
479 unsigned int min_version;
480 unsigned int max_version;
481 struct wined3d_shader_limits limits;
483 vs_limits[] =
485 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
486 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
487 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
488 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
489 * even though they are capable of supporting much more (GL
490 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
491 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
492 * shaders to 256. */
493 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
494 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
495 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 0}},
498 gs_limits[] =
500 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
501 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
502 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
505 ps_limits[] =
507 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
508 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
509 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
510 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
511 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
512 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 12}},
513 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 32}},
516 const struct limits_entry *limits_array;
517 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
518 shader->reg_maps.shader_version.minor);
519 int i = 0;
521 switch (shader->reg_maps.shader_version.type)
523 default:
524 FIXME("Unexpected shader type %u found.\n", shader->reg_maps.shader_version.type);
525 /* Fall-through. */
526 case WINED3D_SHADER_TYPE_VERTEX:
527 limits_array = vs_limits;
528 break;
529 case WINED3D_SHADER_TYPE_GEOMETRY:
530 limits_array = gs_limits;
531 break;
532 case WINED3D_SHADER_TYPE_PIXEL:
533 limits_array = ps_limits;
534 break;
537 while (limits_array[i].min_version && limits_array[i].min_version <= shader_version)
539 if (shader_version <= limits_array[i].max_version)
541 shader->limits = &limits_array[i].limits;
542 break;
544 ++i;
546 if (!shader->limits)
548 FIXME("Unexpected shader version \"%u.%u\".\n",
549 shader->reg_maps.shader_version.major,
550 shader->reg_maps.shader_version.minor);
551 shader->limits = &limits_array[max(0, i - 1)].limits;
555 static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
557 DWORD idx, shift;
558 idx = bit >> 5;
559 shift = bit & 0x1f;
560 bitmap[idx] |= (1u << shift);
563 static BOOL shader_record_register_usage(struct wined3d_shader *shader, struct wined3d_shader_reg_maps *reg_maps,
564 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type, unsigned int constf_size)
566 switch (reg->type)
568 case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
569 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
570 reg_maps->texcoord |= 1u << reg->idx[0].offset;
571 else
572 reg_maps->address |= 1u << reg->idx[0].offset;
573 break;
575 case WINED3DSPR_TEMP:
576 reg_maps->temporary |= 1u << reg->idx[0].offset;
577 break;
579 case WINED3DSPR_INPUT:
580 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
582 if (reg->idx[0].rel_addr)
584 /* If relative addressing is used, we must assume that all registers
585 * are used. Even if it is a construct like v3[aL], we can't assume
586 * that v0, v1 and v2 aren't read because aL can be negative */
587 unsigned int i;
588 for (i = 0; i < MAX_REG_INPUT; ++i)
590 shader->u.ps.input_reg_used[i] = TRUE;
593 else
595 shader->u.ps.input_reg_used[reg->idx[0].offset] = TRUE;
598 else
599 reg_maps->input_registers |= 1u << reg->idx[0].offset;
600 break;
602 case WINED3DSPR_RASTOUT:
603 if (reg->idx[0].offset == 1)
604 reg_maps->fog = 1;
605 if (reg->idx[0].offset == 2)
606 reg_maps->point_size = 1;
607 break;
609 case WINED3DSPR_MISCTYPE:
610 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
612 if (!reg->idx[0].offset)
613 reg_maps->vpos = 1;
614 else if (reg->idx[0].offset == 1)
615 reg_maps->usesfacing = 1;
617 break;
619 case WINED3DSPR_CONST:
620 if (reg->idx[0].rel_addr)
622 if (reg->idx[0].offset < reg_maps->min_rel_offset)
623 reg_maps->min_rel_offset = reg->idx[0].offset;
624 if (reg->idx[0].offset > reg_maps->max_rel_offset)
625 reg_maps->max_rel_offset = reg->idx[0].offset;
626 reg_maps->usesrelconstF = TRUE;
628 else
630 if (reg->idx[0].offset >= min(shader->limits->constant_float, constf_size))
632 WARN("Shader using float constant %u which is not supported.\n", reg->idx[0].offset);
633 return FALSE;
635 else
637 set_bitmap_bit(reg_maps->constf, reg->idx[0].offset);
640 break;
642 case WINED3DSPR_CONSTINT:
643 if (reg->idx[0].offset >= shader->limits->constant_int)
645 WARN("Shader using integer constant %u which is not supported.\n", reg->idx[0].offset);
646 return FALSE;
648 else
650 reg_maps->integer_constants |= (1u << reg->idx[0].offset);
652 break;
654 case WINED3DSPR_CONSTBOOL:
655 if (reg->idx[0].offset >= shader->limits->constant_bool)
657 WARN("Shader using bool constant %u which is not supported.\n", reg->idx[0].offset);
658 return FALSE;
660 else
662 reg_maps->boolean_constants |= (1u << reg->idx[0].offset);
664 break;
666 case WINED3DSPR_COLOROUT:
667 reg_maps->rt_mask |= (1u << reg->idx[0].offset);
668 break;
670 default:
671 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
672 reg->type, reg->idx[0].offset, reg->idx[1].offset);
673 break;
675 return TRUE;
678 static void shader_record_sample(struct wined3d_shader_reg_maps *reg_maps,
679 unsigned int resource_idx, unsigned int sampler_idx, unsigned int bind_idx)
681 struct wined3d_shader_sampler_map_entry *entries, *entry;
682 struct wined3d_shader_sampler_map *map;
683 unsigned int i;
685 map = &reg_maps->sampler_map;
686 entries = map->entries;
687 for (i = 0; i < map->count; ++i)
689 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
690 return;
693 if (!map->size)
695 if (!(entries = HeapAlloc(GetProcessHeap(), 0, sizeof(*entries) * 4)))
697 ERR("Failed to allocate sampler map entries.\n");
698 return;
700 map->size = 4;
701 map->entries = entries;
703 else if (map->count == map->size)
705 size_t new_size = map->size * 2;
707 if (sizeof(*entries) * new_size <= sizeof(*entries) * map->size
708 || !(entries = HeapReAlloc(GetProcessHeap(), 0, entries, sizeof(*entries) * new_size)))
710 ERR("Failed to resize sampler map entries.\n");
711 return;
713 map->size = new_size;
714 map->entries = entries;
717 entry = &entries[map->count++];
718 entry->resource_idx = resource_idx;
719 entry->sampler_idx = sampler_idx;
720 entry->bind_idx = bind_idx;
723 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
725 switch (instr)
727 case WINED3DSIH_M4x4:
728 case WINED3DSIH_M3x4:
729 return param == 1 ? 3 : 0;
731 case WINED3DSIH_M4x3:
732 case WINED3DSIH_M3x3:
733 return param == 1 ? 2 : 0;
735 case WINED3DSIH_M3x2:
736 return param == 1 ? 1 : 0;
738 default:
739 return 0;
743 /* Note that this does not count the loop register as an address register. */
744 static HRESULT shader_get_registers_used(struct wined3d_shader *shader, const struct wined3d_shader_frontend *fe,
745 struct wined3d_shader_reg_maps *reg_maps, struct wined3d_shader_signature *input_signature,
746 struct wined3d_shader_signature *output_signature, const DWORD *byte_code, DWORD constf_size)
748 struct wined3d_shader_signature_element input_signature_elements[max(MAX_ATTRIBS, MAX_REG_INPUT)];
749 struct wined3d_shader_signature_element output_signature_elements[MAX_REG_OUTPUT];
750 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
751 void *fe_data = shader->frontend_data;
752 struct wined3d_shader_version shader_version;
753 const DWORD *ptr = byte_code;
754 unsigned int i;
756 memset(reg_maps, 0, sizeof(*reg_maps));
757 memset(input_signature_elements, 0, sizeof(input_signature_elements));
758 memset(output_signature_elements, 0, sizeof(output_signature_elements));
759 reg_maps->min_rel_offset = ~0U;
761 fe->shader_read_header(fe_data, &ptr, &shader_version);
762 reg_maps->shader_version = shader_version;
764 shader_set_limits(shader);
766 reg_maps->constf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
767 sizeof(*reg_maps->constf) * ((min(shader->limits->constant_float, constf_size) + 31) / 32));
768 if (!reg_maps->constf)
770 ERR("Failed to allocate constant map memory.\n");
771 return E_OUTOFMEMORY;
774 while (!fe->shader_is_end(fe_data, &ptr))
776 struct wined3d_shader_instruction ins;
778 /* Fetch opcode. */
779 fe->shader_read_instruction(fe_data, &ptr, &ins);
781 /* Unhandled opcode, and its parameters. */
782 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
784 TRACE("Skipping unrecognized instruction.\n");
785 continue;
788 /* Handle declarations. */
789 if (ins.handler_idx == WINED3DSIH_DCL)
791 struct wined3d_shader_semantic *semantic = &ins.declaration.semantic;
792 unsigned int reg_idx = semantic->reg.reg.idx[0].offset;
794 switch (semantic->reg.reg.type)
796 /* Mark input registers used. */
797 case WINED3DSPR_INPUT:
798 if (reg_idx >= MAX_REG_INPUT)
800 ERR("Invalid input register index %u.\n", reg_idx);
801 break;
803 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL && shader_version.major == 3
804 && semantic->usage == WINED3D_DECL_USAGE_POSITION && !semantic->usage_idx)
805 return WINED3DERR_INVALIDCALL;
806 reg_maps->input_registers |= 1u << reg_idx;
807 shader_signature_from_semantic(&input_signature_elements[reg_idx], semantic);
808 break;
810 /* Vertex shader: mark 3.0 output registers used, save token. */
811 case WINED3DSPR_OUTPUT:
812 if (reg_idx >= MAX_REG_OUTPUT)
814 ERR("Invalid output register index %u.\n", reg_idx);
815 break;
817 reg_maps->output_registers |= 1u << reg_idx;
818 shader_signature_from_semantic(&output_signature_elements[reg_idx], semantic);
819 if (semantic->usage == WINED3D_DECL_USAGE_FOG)
820 reg_maps->fog = 1;
821 if (semantic->usage == WINED3D_DECL_USAGE_PSIZE)
822 reg_maps->point_size = 1;
823 break;
825 case WINED3DSPR_SAMPLER:
826 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
827 case WINED3DSPR_RESOURCE:
828 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
830 ERR("Invalid resource index %u.\n", reg_idx);
831 break;
833 reg_maps->resource_info[reg_idx].type = semantic->resource_type;
834 reg_maps->resource_info[reg_idx].data_type = semantic->resource_data_type;
835 break;
837 default:
838 TRACE("Not recording DCL register type %#x.\n", semantic->reg.reg.type);
839 break;
842 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
844 struct wined3d_shader_register *reg = &ins.declaration.src.reg;
845 if (reg->idx[0].offset >= WINED3D_MAX_CBS)
846 ERR("Invalid CB index %u.\n", reg->idx[0].offset);
847 else
848 reg_maps->cb_sizes[reg->idx[0].offset] = reg->idx[1].offset;
850 else if (ins.handler_idx == WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER)
852 if (reg_maps->icb)
853 FIXME("Multiple immediate constant buffers.\n");
854 reg_maps->icb = ins.declaration.icb;
856 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE)
858 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
859 shader->u.gs.input_type = ins.declaration.primitive_type;
860 else
861 FIXME("Invalid instruction %#x for shader type %#x.\n",
862 ins.handler_idx, shader_version.type);
864 else if (ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
866 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
867 shader->u.gs.output_type = ins.declaration.primitive_type;
868 else
869 FIXME("Invalid instruction %#x for shader type %#x.\n",
870 ins.handler_idx, shader_version.type);
872 else if (ins.handler_idx == WINED3DSIH_DCL_SAMPLER)
874 if (ins.flags & WINED3DSI_SAMPLER_COMPARISON_MODE)
875 reg_maps->sampler_comparison_mode |= (1u << ins.declaration.dst.reg.idx[0].offset);
877 else if (ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
879 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
880 shader->u.gs.vertices_out = ins.declaration.count;
881 else
882 FIXME("Invalid instruction %#x for shader type %#x.\n",
883 ins.handler_idx, shader_version.type);
885 else if (ins.handler_idx == WINED3DSIH_DEF)
887 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
888 float *value;
889 if (!lconst) return E_OUTOFMEMORY;
891 lconst->idx = ins.dst[0].reg.idx[0].offset;
892 memcpy(lconst->value, ins.src[0].reg.immconst_data, 4 * sizeof(DWORD));
893 value = (float *)lconst->value;
895 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
896 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
898 if (value[0] < -1.0f) value[0] = -1.0f;
899 else if (value[0] > 1.0f) value[0] = 1.0f;
900 if (value[1] < -1.0f) value[1] = -1.0f;
901 else if (value[1] > 1.0f) value[1] = 1.0f;
902 if (value[2] < -1.0f) value[2] = -1.0f;
903 else if (value[2] > 1.0f) value[2] = 1.0f;
904 if (value[3] < -1.0f) value[3] = -1.0f;
905 else if (value[3] > 1.0f) value[3] = 1.0f;
908 list_add_head(&shader->constantsF, &lconst->entry);
910 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
911 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
913 shader->lconst_inf_or_nan = TRUE;
916 else if (ins.handler_idx == WINED3DSIH_DEFI)
918 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
919 if (!lconst) return E_OUTOFMEMORY;
921 lconst->idx = ins.dst[0].reg.idx[0].offset;
922 memcpy(lconst->value, ins.src[0].reg.immconst_data, 4 * sizeof(DWORD));
924 list_add_head(&shader->constantsI, &lconst->entry);
925 reg_maps->local_int_consts |= (1u << lconst->idx);
927 else if (ins.handler_idx == WINED3DSIH_DEFB)
929 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
930 if (!lconst) return E_OUTOFMEMORY;
932 lconst->idx = ins.dst[0].reg.idx[0].offset;
933 memcpy(lconst->value, ins.src[0].reg.immconst_data, sizeof(DWORD));
935 list_add_head(&shader->constantsB, &lconst->entry);
936 reg_maps->local_bool_consts |= (1u << lconst->idx);
938 /* For subroutine prototypes. */
939 else if (ins.handler_idx == WINED3DSIH_LABEL)
941 reg_maps->labels |= 1u << ins.src[0].reg.idx[0].offset;
943 /* Set texture, address, temporary registers. */
944 else
946 BOOL color0_mov = FALSE;
947 unsigned int i;
949 /* This will loop over all the registers and try to
950 * make a bitmask of the ones we're interested in.
952 * Relative addressing tokens are ignored, but that's
953 * okay, since we'll catch any address registers when
954 * they are initialized (required by spec). */
955 for (i = 0; i < ins.dst_count; ++i)
957 if (!shader_record_register_usage(shader, reg_maps, &ins.dst[i].reg,
958 shader_version.type, constf_size))
959 return WINED3DERR_INVALIDCALL;
961 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
963 UINT idx = ins.dst[i].reg.idx[0].offset;
965 switch (ins.dst[i].reg.type)
967 case WINED3DSPR_RASTOUT:
968 if (shader_version.major >= 3)
969 break;
970 switch (idx)
972 case 0: /* oPos */
973 reg_maps->output_registers |= 1u << 10;
974 shader_signature_from_usage(&output_signature_elements[10],
975 WINED3D_DECL_USAGE_POSITION, 0, 10, WINED3DSP_WRITEMASK_ALL);
976 break;
978 case 1: /* oFog */
979 reg_maps->output_registers |= 1u << 11;
980 shader_signature_from_usage(&output_signature_elements[11],
981 WINED3D_DECL_USAGE_FOG, 0, 11, WINED3DSP_WRITEMASK_0);
982 break;
984 case 2: /* oPts */
985 reg_maps->output_registers |= 1u << 11;
986 shader_signature_from_usage(&output_signature_elements[11],
987 WINED3D_DECL_USAGE_PSIZE, 0, 11, WINED3DSP_WRITEMASK_1);
988 break;
990 break;
992 case WINED3DSPR_ATTROUT:
993 if (shader_version.major >= 3)
994 break;
995 if (idx < 2)
997 idx += 8;
998 if (reg_maps->output_registers & (1u << idx))
1000 output_signature_elements[idx].mask |= ins.dst[i].write_mask;
1002 else
1004 reg_maps->output_registers |= 1u << idx;
1005 shader_signature_from_usage(&output_signature_elements[idx],
1006 WINED3D_DECL_USAGE_COLOR, idx - 8, idx, ins.dst[i].write_mask);
1009 break;
1011 case WINED3DSPR_TEXCRDOUT:
1012 if (shader_version.major >= 3)
1014 reg_maps->u.output_registers_mask[idx] |= ins.dst[i].write_mask;
1015 break;
1017 reg_maps->u.texcoord_mask[idx] |= ins.dst[i].write_mask;
1018 if (reg_maps->output_registers & (1u << idx))
1020 output_signature_elements[idx].mask |= ins.dst[i].write_mask;
1022 else
1024 reg_maps->output_registers |= 1u << idx;
1025 shader_signature_from_usage(&output_signature_elements[idx],
1026 WINED3D_DECL_USAGE_TEXCOORD, idx, idx, ins.dst[i].write_mask);
1028 break;
1030 default:
1031 break;
1035 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1037 if (ins.dst[i].reg.type == WINED3DSPR_COLOROUT && !ins.dst[i].reg.idx[0].offset)
1039 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
1040 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
1041 * the mov and perform the sRGB write correction from the source register.
1043 * However, if the mov is only partial, we can't do this, and if the write
1044 * comes from an instruction other than MOV it is hard to do as well. If
1045 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
1046 shader->u.ps.color0_mov = FALSE;
1047 if (ins.handler_idx == WINED3DSIH_MOV
1048 && ins.dst[i].write_mask == WINED3DSP_WRITEMASK_ALL)
1050 /* Used later when the source register is read. */
1051 color0_mov = TRUE;
1054 /* Also drop the MOV marker if the source register is overwritten prior to the shader
1055 * end
1057 else if (ins.dst[i].reg.type == WINED3DSPR_TEMP
1058 && ins.dst[i].reg.idx[0].offset == shader->u.ps.color0_reg)
1060 shader->u.ps.color0_mov = FALSE;
1064 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
1065 if (shader_version.major == 1
1066 && (ins.handler_idx == WINED3DSIH_TEX
1067 || ins.handler_idx == WINED3DSIH_TEXBEM
1068 || ins.handler_idx == WINED3DSIH_TEXBEML
1069 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
1070 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
1071 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
1072 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
1073 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
1074 || ins.handler_idx == WINED3DSIH_TEXREG2AR
1075 || ins.handler_idx == WINED3DSIH_TEXREG2GB
1076 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
1078 unsigned int reg_idx = ins.dst[i].reg.idx[0].offset;
1080 TRACE("Setting fake 2D resource for 1.x pixelshader.\n");
1081 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
1082 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_FLOAT;
1083 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
1085 /* texbem is only valid with < 1.4 pixel shaders */
1086 if (ins.handler_idx == WINED3DSIH_TEXBEM
1087 || ins.handler_idx == WINED3DSIH_TEXBEML)
1089 reg_maps->bumpmat |= 1u << reg_idx;
1090 if (ins.handler_idx == WINED3DSIH_TEXBEML)
1092 reg_maps->luminanceparams |= 1u << reg_idx;
1096 else if (ins.handler_idx == WINED3DSIH_BEM)
1098 reg_maps->bumpmat |= 1u << ins.dst[i].reg.idx[0].offset;
1102 if (ins.handler_idx == WINED3DSIH_NRM) reg_maps->usesnrm = 1;
1103 else if (ins.handler_idx == WINED3DSIH_DSY) reg_maps->usesdsy = 1;
1104 else if (ins.handler_idx == WINED3DSIH_DSX) reg_maps->usesdsx = 1;
1105 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
1106 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
1107 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
1108 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
1109 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
1110 else if (ins.handler_idx == WINED3DSIH_POW) reg_maps->usespow = 1;
1111 else if (ins.handler_idx == WINED3DSIH_LOOP
1112 || ins.handler_idx == WINED3DSIH_REP)
1114 ++cur_loop_depth;
1115 if (cur_loop_depth > max_loop_depth)
1116 max_loop_depth = cur_loop_depth;
1118 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
1119 || ins.handler_idx == WINED3DSIH_ENDREP)
1121 --cur_loop_depth;
1123 else if (ins.handler_idx == WINED3DSIH_SAMPLE
1124 || ins.handler_idx == WINED3DSIH_SAMPLE_B
1125 || ins.handler_idx == WINED3DSIH_SAMPLE_C_LZ
1126 || ins.handler_idx == WINED3DSIH_SAMPLE_GRAD
1127 || ins.handler_idx == WINED3DSIH_SAMPLE_LOD)
1129 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1130 ins.src[2].reg.idx[0].offset, reg_maps->sampler_map.count);
1132 else if (ins.handler_idx == WINED3DSIH_LD
1133 || ins.handler_idx == WINED3DSIH_RESINFO)
1135 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1136 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1139 if (ins.predicate)
1140 if (!shader_record_register_usage(shader, reg_maps, &ins.predicate->reg,
1141 shader_version.type, constf_size))
1142 return WINED3DERR_INVALIDCALL;
1144 for (i = 0; i < ins.src_count; ++i)
1146 unsigned int count = get_instr_extra_regcount(ins.handler_idx, i);
1147 struct wined3d_shader_register reg = ins.src[i].reg;
1149 if (!shader_record_register_usage(shader, reg_maps, &ins.src[i].reg,
1150 shader_version.type, constf_size))
1151 return WINED3DERR_INVALIDCALL;
1152 while (count)
1154 ++reg.idx[0].offset;
1155 if (!shader_record_register_usage(shader, reg_maps, &reg,
1156 shader_version.type, constf_size))
1157 return WINED3DERR_INVALIDCALL;
1158 --count;
1161 if (color0_mov)
1163 if (ins.src[i].reg.type == WINED3DSPR_TEMP
1164 && ins.src[i].swizzle == WINED3DSP_NOSWIZZLE)
1166 shader->u.ps.color0_mov = TRUE;
1167 shader->u.ps.color0_reg = ins.src[i].reg.idx[0].offset;
1173 reg_maps->loop_depth = max_loop_depth;
1175 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1176 * R0 is written to the render target. */
1177 if (shader_version.major < 2 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1178 reg_maps->rt_mask |= (1u << 0);
1180 shader->functionLength = ((const char *)ptr - (const char *)byte_code);
1182 if (input_signature->elements)
1184 for (i = 0; i < input_signature->element_count; ++i)
1186 reg_maps->input_registers |= 1u << input_signature->elements[i].register_idx;
1187 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1188 && input_signature->elements[i].sysval_semantic == WINED3D_SV_POSITION)
1189 reg_maps->vpos = 1;
1192 else if (!input_signature->elements && reg_maps->input_registers)
1194 unsigned int count = wined3d_popcount(reg_maps->input_registers);
1195 struct wined3d_shader_signature_element *e;
1196 unsigned int i;
1198 if (!(input_signature->elements = HeapAlloc(GetProcessHeap(), 0, sizeof(*input_signature->elements) * count)))
1199 return E_OUTOFMEMORY;
1200 input_signature->element_count = count;
1202 e = input_signature->elements;
1203 for (i = 0; i < ARRAY_SIZE(input_signature_elements); ++i)
1205 if (!(reg_maps->input_registers & (1u << i)))
1206 continue;
1207 input_signature_elements[i].register_idx = i;
1208 *e++ = input_signature_elements[i];
1212 if (output_signature->elements)
1214 for (i = 0; i < output_signature->element_count; ++i)
1216 reg_maps->output_registers |= 1u << output_signature->elements[i].register_idx;
1219 else if (reg_maps->output_registers)
1221 unsigned int count = wined3d_popcount(reg_maps->output_registers);
1222 struct wined3d_shader_signature_element *e;
1224 if (!(output_signature->elements = HeapAlloc(GetProcessHeap(), 0, sizeof(*output_signature->elements) * count)))
1225 return E_OUTOFMEMORY;
1226 output_signature->element_count = count;
1228 e = output_signature->elements;
1229 for (i = 0; i < ARRAY_SIZE(output_signature_elements); ++i)
1231 if (!(reg_maps->output_registers & (1u << i)))
1232 continue;
1233 *e++ = output_signature_elements[i];
1237 return WINED3D_OK;
1240 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
1242 DWORD map = 1u << max;
1243 map |= map - 1;
1244 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
1246 return wined3d_log2i(map);
1249 static void shader_dump_global_flags(struct wined3d_string_buffer *buffer, DWORD global_flags)
1251 if (global_flags & WINED3DSGF_REFACTORING_ALLOWED)
1253 shader_addline(buffer, "refactoringAllowed");
1254 global_flags &= ~WINED3DSGF_REFACTORING_ALLOWED;
1255 if (global_flags)
1256 shader_addline(buffer, " | ");
1259 if (global_flags & WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS)
1261 shader_addline(buffer, "enableRawAndStructuredBuffers");
1262 global_flags &= ~WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS;
1265 if (global_flags)
1266 shader_addline(buffer, "unknown_flags(%#x)", global_flags);
1269 static void shader_dump_sysval_semantic(struct wined3d_string_buffer *buffer, enum wined3d_sysval_semantic semantic)
1271 unsigned int i;
1273 for (i = 0; i < ARRAY_SIZE(sysval_semantic_names); ++i)
1275 if (sysval_semantic_names[i].sysval_semantic == semantic)
1277 shader_addline(buffer, "%s", sysval_semantic_names[i].sysval_name);
1278 return;
1282 shader_addline(buffer, "unknown_sysval_semantic(%#x)", semantic);
1285 static void shader_dump_decl_usage(struct wined3d_string_buffer *buffer,
1286 const struct wined3d_shader_semantic *semantic, const struct wined3d_shader_version *shader_version)
1288 shader_addline(buffer, "dcl");
1290 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
1292 switch (semantic->resource_type)
1294 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1295 shader_addline(buffer, "_2d");
1296 break;
1298 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1299 shader_addline(buffer, "_3d");
1300 break;
1302 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1303 shader_addline(buffer, "_cube");
1304 break;
1306 default:
1307 shader_addline(buffer, "_unknown_resource_type(%#x)", semantic->resource_type);
1308 break;
1311 else if (semantic->reg.reg.type == WINED3DSPR_RESOURCE)
1313 shader_addline(buffer, "_resource_");
1314 switch (semantic->resource_type)
1316 case WINED3D_SHADER_RESOURCE_BUFFER:
1317 shader_addline(buffer, "buffer");
1318 break;
1320 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1321 shader_addline(buffer, "texture1d");
1322 break;
1324 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1325 shader_addline(buffer, "texture2d");
1326 break;
1328 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
1329 shader_addline(buffer, "texture2dms");
1330 break;
1332 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1333 shader_addline(buffer, "texture3d");
1334 break;
1336 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1337 shader_addline(buffer, "texturecube");
1338 break;
1340 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
1341 shader_addline(buffer, "texture1darray");
1342 break;
1344 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
1345 shader_addline(buffer, "texture2darray");
1346 break;
1348 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
1349 shader_addline(buffer, "texture2dmsarray");
1350 break;
1352 default:
1353 shader_addline(buffer, "unknown");
1354 break;
1356 switch (semantic->resource_data_type)
1358 case WINED3D_DATA_FLOAT:
1359 shader_addline(buffer, " (float)");
1360 break;
1362 case WINED3D_DATA_INT:
1363 shader_addline(buffer, " (int)");
1364 break;
1366 case WINED3D_DATA_UINT:
1367 shader_addline(buffer, " (uint)");
1368 break;
1370 case WINED3D_DATA_UNORM:
1371 shader_addline(buffer, " (unorm)");
1372 break;
1374 case WINED3D_DATA_SNORM:
1375 shader_addline(buffer, " (snorm)");
1376 break;
1378 default:
1379 shader_addline(buffer, " (unknown)");
1380 break;
1383 else
1385 /* Pixel shaders 3.0 don't have usage semantics. */
1386 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL)
1387 return;
1388 else
1389 shader_addline(buffer, "_");
1391 switch (semantic->usage)
1393 case WINED3D_DECL_USAGE_POSITION:
1394 shader_addline(buffer, "position%u", semantic->usage_idx);
1395 break;
1397 case WINED3D_DECL_USAGE_BLEND_INDICES:
1398 shader_addline(buffer, "blend");
1399 break;
1401 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
1402 shader_addline(buffer, "weight");
1403 break;
1405 case WINED3D_DECL_USAGE_NORMAL:
1406 shader_addline(buffer, "normal%u", semantic->usage_idx);
1407 break;
1409 case WINED3D_DECL_USAGE_PSIZE:
1410 shader_addline(buffer, "psize");
1411 break;
1413 case WINED3D_DECL_USAGE_COLOR:
1414 if (!semantic->usage_idx)
1415 shader_addline(buffer, "color");
1416 else
1417 shader_addline(buffer, "specular%u", (semantic->usage_idx - 1));
1418 break;
1420 case WINED3D_DECL_USAGE_TEXCOORD:
1421 shader_addline(buffer, "texture%u", semantic->usage_idx);
1422 break;
1424 case WINED3D_DECL_USAGE_TANGENT:
1425 shader_addline(buffer, "tangent");
1426 break;
1428 case WINED3D_DECL_USAGE_BINORMAL:
1429 shader_addline(buffer, "binormal");
1430 break;
1432 case WINED3D_DECL_USAGE_TESS_FACTOR:
1433 shader_addline(buffer, "tessfactor");
1434 break;
1436 case WINED3D_DECL_USAGE_POSITIONT:
1437 shader_addline(buffer, "positionT%u", semantic->usage_idx);
1438 break;
1440 case WINED3D_DECL_USAGE_FOG:
1441 shader_addline(buffer, "fog");
1442 break;
1444 case WINED3D_DECL_USAGE_DEPTH:
1445 shader_addline(buffer, "depth");
1446 break;
1448 case WINED3D_DECL_USAGE_SAMPLE:
1449 shader_addline(buffer, "sample");
1450 break;
1452 default:
1453 shader_addline(buffer, "<unknown_semantic(%#x)>", semantic->usage);
1454 FIXME("unknown_semantics(0x%08x)", semantic->usage);
1459 static void shader_dump_register(struct wined3d_string_buffer *buffer,
1460 const struct wined3d_shader_register *reg, const struct wined3d_shader_version *shader_version)
1462 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
1463 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
1464 UINT offset = reg->idx[0].offset;
1466 switch (reg->type)
1468 case WINED3DSPR_TEMP:
1469 shader_addline(buffer, "r");
1470 break;
1472 case WINED3DSPR_INPUT:
1473 shader_addline(buffer, "v");
1474 break;
1476 case WINED3DSPR_CONST:
1477 case WINED3DSPR_CONST2:
1478 case WINED3DSPR_CONST3:
1479 case WINED3DSPR_CONST4:
1480 shader_addline(buffer, "c");
1481 offset = shader_get_float_offset(reg->type, offset);
1482 break;
1484 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
1485 shader_addline(buffer, "%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
1486 break;
1488 case WINED3DSPR_RASTOUT:
1489 shader_addline(buffer, "%s", rastout_reg_names[offset]);
1490 break;
1492 case WINED3DSPR_COLOROUT:
1493 shader_addline(buffer, "oC");
1494 break;
1496 case WINED3DSPR_DEPTHOUT:
1497 shader_addline(buffer, "oDepth");
1498 break;
1500 case WINED3DSPR_ATTROUT:
1501 shader_addline(buffer, "oD");
1502 break;
1504 case WINED3DSPR_TEXCRDOUT:
1505 /* Vertex shaders >= 3.0 use general purpose output registers
1506 * (WINED3DSPR_OUTPUT), which can include an address token. */
1507 if (shader_version->major >= 3)
1508 shader_addline(buffer, "o");
1509 else
1510 shader_addline(buffer, "oT");
1511 break;
1513 case WINED3DSPR_CONSTINT:
1514 shader_addline(buffer, "i");
1515 break;
1517 case WINED3DSPR_CONSTBOOL:
1518 shader_addline(buffer, "b");
1519 break;
1521 case WINED3DSPR_LABEL:
1522 shader_addline(buffer, "l");
1523 break;
1525 case WINED3DSPR_LOOP:
1526 shader_addline(buffer, "aL");
1527 break;
1529 case WINED3DSPR_SAMPLER:
1530 shader_addline(buffer, "s");
1531 break;
1533 case WINED3DSPR_MISCTYPE:
1534 if (offset > 1)
1536 FIXME("Unhandled misctype register %u.\n", offset);
1537 shader_addline(buffer, "<unhandled misctype %#x>", offset);
1539 else
1541 shader_addline(buffer, "%s", misctype_reg_names[offset]);
1543 break;
1545 case WINED3DSPR_PREDICATE:
1546 shader_addline(buffer, "p");
1547 break;
1549 case WINED3DSPR_IMMCONST:
1550 shader_addline(buffer, "l");
1551 break;
1553 case WINED3DSPR_CONSTBUFFER:
1554 shader_addline(buffer, "cb");
1555 break;
1557 case WINED3DSPR_IMMCONSTBUFFER:
1558 shader_addline(buffer, "icb");
1559 break;
1561 case WINED3DSPR_PRIMID:
1562 shader_addline(buffer, "primID");
1563 break;
1565 case WINED3DSPR_NULL:
1566 shader_addline(buffer, "null");
1567 break;
1569 case WINED3DSPR_RESOURCE:
1570 shader_addline(buffer, "t");
1571 break;
1573 default:
1574 shader_addline(buffer, "<unhandled_rtype(%#x)>", reg->type);
1575 break;
1578 if (reg->type == WINED3DSPR_IMMCONST)
1580 shader_addline(buffer, "(");
1581 switch (reg->immconst_type)
1583 case WINED3D_IMMCONST_SCALAR:
1584 switch (reg->data_type)
1586 case WINED3D_DATA_FLOAT:
1587 shader_addline(buffer, "%.8e", *(const float *)reg->immconst_data);
1588 break;
1589 case WINED3D_DATA_INT:
1590 shader_addline(buffer, "%d", reg->immconst_data[0]);
1591 break;
1592 case WINED3D_DATA_RESOURCE:
1593 case WINED3D_DATA_SAMPLER:
1594 case WINED3D_DATA_UINT:
1595 shader_addline(buffer, "%u", reg->immconst_data[0]);
1596 break;
1597 default:
1598 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
1599 break;
1601 break;
1603 case WINED3D_IMMCONST_VEC4:
1604 switch (reg->data_type)
1606 case WINED3D_DATA_FLOAT:
1607 shader_addline(buffer, "%.8e, %.8e, %.8e, %.8e",
1608 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
1609 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
1610 break;
1611 case WINED3D_DATA_INT:
1612 shader_addline(buffer, "%d, %d, %d, %d",
1613 reg->immconst_data[0], reg->immconst_data[1],
1614 reg->immconst_data[2], reg->immconst_data[3]);
1615 break;
1616 case WINED3D_DATA_RESOURCE:
1617 case WINED3D_DATA_SAMPLER:
1618 case WINED3D_DATA_UINT:
1619 shader_addline(buffer, "%u, %u, %u, %u",
1620 reg->immconst_data[0], reg->immconst_data[1],
1621 reg->immconst_data[2], reg->immconst_data[3]);
1622 break;
1623 default:
1624 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
1625 break;
1627 break;
1629 default:
1630 shader_addline(buffer, "<unhandled immconst_type %#x>", reg->immconst_type);
1631 break;
1633 shader_addline(buffer, ")");
1635 else if (reg->type != WINED3DSPR_RASTOUT
1636 && reg->type != WINED3DSPR_MISCTYPE
1637 && reg->type != WINED3DSPR_NULL)
1639 if (offset != ~0u)
1641 shader_addline(buffer, "[");
1642 if (reg->idx[0].rel_addr)
1644 shader_dump_src_param(buffer, reg->idx[0].rel_addr, shader_version);
1645 shader_addline(buffer, " + ");
1647 shader_addline(buffer, "%u]", offset);
1649 if (reg->idx[1].offset != ~0u)
1651 shader_addline(buffer, "[");
1652 if (reg->idx[1].rel_addr)
1654 shader_dump_src_param(buffer, reg->idx[1].rel_addr, shader_version);
1655 shader_addline(buffer, " + ");
1657 shader_addline(buffer, "%u]", reg->idx[1].offset);
1663 static void shader_dump_dst_param(struct wined3d_string_buffer *buffer,
1664 const struct wined3d_shader_dst_param *param, const struct wined3d_shader_version *shader_version)
1666 DWORD write_mask = param->write_mask;
1668 shader_dump_register(buffer, &param->reg, shader_version);
1670 if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
1672 static const char write_mask_chars[] = "xyzw";
1674 shader_addline(buffer, ".");
1675 if (write_mask & WINED3DSP_WRITEMASK_0)
1676 shader_addline(buffer, "%c", write_mask_chars[0]);
1677 if (write_mask & WINED3DSP_WRITEMASK_1)
1678 shader_addline(buffer, "%c", write_mask_chars[1]);
1679 if (write_mask & WINED3DSP_WRITEMASK_2)
1680 shader_addline(buffer, "%c", write_mask_chars[2]);
1681 if (write_mask & WINED3DSP_WRITEMASK_3)
1682 shader_addline(buffer, "%c", write_mask_chars[3]);
1686 static void shader_dump_src_param(struct wined3d_string_buffer *buffer,
1687 const struct wined3d_shader_src_param *param, const struct wined3d_shader_version *shader_version)
1689 enum wined3d_shader_src_modifier src_modifier = param->modifiers;
1690 DWORD swizzle = param->swizzle;
1692 if (src_modifier == WINED3DSPSM_NEG
1693 || src_modifier == WINED3DSPSM_BIASNEG
1694 || src_modifier == WINED3DSPSM_SIGNNEG
1695 || src_modifier == WINED3DSPSM_X2NEG
1696 || src_modifier == WINED3DSPSM_ABSNEG)
1697 shader_addline(buffer, "-");
1698 else if (src_modifier == WINED3DSPSM_COMP)
1699 shader_addline(buffer, "1-");
1700 else if (src_modifier == WINED3DSPSM_NOT)
1701 shader_addline(buffer, "!");
1703 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
1704 shader_addline(buffer, "abs(");
1706 shader_dump_register(buffer, &param->reg, shader_version);
1708 switch (src_modifier)
1710 case WINED3DSPSM_NONE: break;
1711 case WINED3DSPSM_NEG: break;
1712 case WINED3DSPSM_NOT: break;
1713 case WINED3DSPSM_BIAS: shader_addline(buffer, "_bias"); break;
1714 case WINED3DSPSM_BIASNEG: shader_addline(buffer, "_bias"); break;
1715 case WINED3DSPSM_SIGN: shader_addline(buffer, "_bx2"); break;
1716 case WINED3DSPSM_SIGNNEG: shader_addline(buffer, "_bx2"); break;
1717 case WINED3DSPSM_COMP: break;
1718 case WINED3DSPSM_X2: shader_addline(buffer, "_x2"); break;
1719 case WINED3DSPSM_X2NEG: shader_addline(buffer, "_x2"); break;
1720 case WINED3DSPSM_DZ: shader_addline(buffer, "_dz"); break;
1721 case WINED3DSPSM_DW: shader_addline(buffer, "_dw"); break;
1722 case WINED3DSPSM_ABSNEG: shader_addline(buffer, ")"); break;
1723 case WINED3DSPSM_ABS: shader_addline(buffer, ")"); break;
1724 default: shader_addline(buffer, "_unknown_modifier(%#x)", src_modifier);
1727 if (swizzle != WINED3DSP_NOSWIZZLE)
1729 static const char swizzle_chars[] = "xyzw";
1730 DWORD swizzle_x = swizzle & 0x03;
1731 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1732 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1733 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1735 if (swizzle_x == swizzle_y
1736 && swizzle_x == swizzle_z
1737 && swizzle_x == swizzle_w)
1739 shader_addline(buffer, ".%c", swizzle_chars[swizzle_x]);
1741 else
1743 shader_addline(buffer, ".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
1744 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
1749 /* Shared code in order to generate the bulk of the shader string.
1750 * NOTE: A description of how to parse tokens can be found on MSDN. */
1751 void shader_generate_main(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
1752 const struct wined3d_shader_reg_maps *reg_maps, const DWORD *byte_code, void *backend_ctx)
1754 struct wined3d_device *device = shader->device;
1755 const struct wined3d_shader_frontend *fe = shader->frontend;
1756 void *fe_data = shader->frontend_data;
1757 struct wined3d_shader_version shader_version;
1758 struct wined3d_shader_loop_state loop_state;
1759 struct wined3d_shader_instruction ins;
1760 struct wined3d_shader_tex_mx tex_mx;
1761 struct wined3d_shader_context ctx;
1762 const DWORD *ptr = byte_code;
1764 /* Initialize current parsing state. */
1765 tex_mx.current_row = 0;
1766 loop_state.current_depth = 0;
1767 loop_state.current_reg = 0;
1769 ctx.shader = shader;
1770 ctx.gl_info = &device->adapter->gl_info;
1771 ctx.reg_maps = reg_maps;
1772 ctx.buffer = buffer;
1773 ctx.tex_mx = &tex_mx;
1774 ctx.loop_state = &loop_state;
1775 ctx.backend_data = backend_ctx;
1776 ins.ctx = &ctx;
1778 fe->shader_read_header(fe_data, &ptr, &shader_version);
1780 while (!fe->shader_is_end(fe_data, &ptr))
1782 /* Read opcode. */
1783 fe->shader_read_instruction(fe_data, &ptr, &ins);
1785 /* Unknown opcode and its parameters. */
1786 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1788 TRACE("Skipping unrecognized instruction.\n");
1789 continue;
1792 if (ins.predicate)
1793 FIXME("Predicates not implemented.\n");
1795 /* Call appropriate function for output target */
1796 device->shader_backend->shader_handle_instruction(&ins);
1800 static void shader_dump_ins_modifiers(struct wined3d_string_buffer *buffer,
1801 const struct wined3d_shader_dst_param *dst)
1803 DWORD mmask = dst->modifiers;
1805 switch (dst->shift)
1807 case 0: break;
1808 case 13: shader_addline(buffer, "_d8"); break;
1809 case 14: shader_addline(buffer, "_d4"); break;
1810 case 15: shader_addline(buffer, "_d2"); break;
1811 case 1: shader_addline(buffer, "_x2"); break;
1812 case 2: shader_addline(buffer, "_x4"); break;
1813 case 3: shader_addline(buffer, "_x8"); break;
1814 default: shader_addline(buffer, "_unhandled_shift(%d)", dst->shift); break;
1817 if (mmask & WINED3DSPDM_SATURATE) shader_addline(buffer, "_sat");
1818 if (mmask & WINED3DSPDM_PARTIALPRECISION) shader_addline(buffer, "_pp");
1819 if (mmask & WINED3DSPDM_MSAMPCENTROID) shader_addline(buffer, "_centroid");
1821 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
1822 if (mmask) FIXME("_unrecognized_modifier(%#x)", mmask);
1825 static void shader_dump_primitive_type(struct wined3d_string_buffer *buffer,
1826 enum wined3d_primitive_type primitive_type)
1828 switch (primitive_type)
1830 case WINED3D_PT_UNDEFINED:
1831 shader_addline(buffer, "undefined");
1832 break;
1833 case WINED3D_PT_POINTLIST:
1834 shader_addline(buffer, "pointlist");
1835 break;
1836 case WINED3D_PT_LINELIST:
1837 shader_addline(buffer, "linelist");
1838 break;
1839 case WINED3D_PT_LINESTRIP:
1840 shader_addline(buffer, "linestrip");
1841 break;
1842 case WINED3D_PT_TRIANGLELIST:
1843 shader_addline(buffer, "trianglelist");
1844 break;
1845 case WINED3D_PT_TRIANGLESTRIP:
1846 shader_addline(buffer, "trianglestrip");
1847 break;
1848 case WINED3D_PT_TRIANGLEFAN:
1849 shader_addline(buffer, "trianglefan");
1850 break;
1851 case WINED3D_PT_LINELIST_ADJ:
1852 shader_addline(buffer, "linelist_adj");
1853 break;
1854 case WINED3D_PT_LINESTRIP_ADJ:
1855 shader_addline(buffer, "linestrip_adj");
1856 break;
1857 case WINED3D_PT_TRIANGLELIST_ADJ:
1858 shader_addline(buffer, "trianglelist_adj");
1859 break;
1860 case WINED3D_PT_TRIANGLESTRIP_ADJ:
1861 shader_addline(buffer, "trianglestrip_adj");
1862 break;
1863 default:
1864 shader_addline(buffer, "<unrecognized_primitive_type %#x>", primitive_type);
1865 break;
1869 static void shader_dump_interpolation_mode(struct wined3d_string_buffer *buffer,
1870 enum wined3d_shader_interpolation_mode interpolation_mode)
1872 switch (interpolation_mode)
1874 case WINED3DSIM_CONSTANT:
1875 shader_addline(buffer, "constant");
1876 break;
1877 case WINED3DSIM_LINEAR:
1878 shader_addline(buffer, "linear");
1879 break;
1880 case WINED3DSIM_LINEAR_CENTROID:
1881 shader_addline(buffer, "linear centroid");
1882 break;
1883 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
1884 shader_addline(buffer, "linear noperspective");
1885 break;
1886 case WINED3DSIM_LINEAR_SAMPLE:
1887 shader_addline(buffer, "linear sample");
1888 break;
1889 case WINED3DSIM_LINEAR_NOPERSPECTIVE_CENTROID:
1890 shader_addline(buffer, "linear noperspective centroid");
1891 break;
1892 case WINED3DSIM_LINEAR_NOPERSPECTIVE_SAMPLE:
1893 shader_addline(buffer, "linear noperspective sample");
1894 break;
1895 default:
1896 shader_addline(buffer, "<unrecognized_interpolation_mode %#x>", interpolation_mode);
1897 break;
1901 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *byte_code)
1903 struct wined3d_shader_version shader_version;
1904 struct wined3d_string_buffer buffer;
1905 const DWORD *ptr = byte_code;
1906 const char *type_prefix;
1907 const char *p, *q;
1908 DWORD i;
1910 if (!string_buffer_init(&buffer))
1912 ERR("Failed to initialize string buffer.\n");
1913 return;
1916 TRACE("Parsing %p.\n", byte_code);
1918 fe->shader_read_header(fe_data, &ptr, &shader_version);
1920 switch (shader_version.type)
1922 case WINED3D_SHADER_TYPE_VERTEX:
1923 type_prefix = "vs";
1924 break;
1926 case WINED3D_SHADER_TYPE_GEOMETRY:
1927 type_prefix = "gs";
1928 break;
1930 case WINED3D_SHADER_TYPE_PIXEL:
1931 type_prefix = "ps";
1932 break;
1934 default:
1935 FIXME("Unhandled shader type %#x.\n", shader_version.type);
1936 type_prefix = "unknown";
1937 break;
1940 shader_addline(&buffer, "%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
1942 while (!fe->shader_is_end(fe_data, &ptr))
1944 struct wined3d_shader_instruction ins;
1946 fe->shader_read_instruction(fe_data, &ptr, &ins);
1947 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1949 WARN("Skipping unrecognized instruction.\n");
1950 shader_addline(&buffer, "<unrecognized instruction>\n");
1951 continue;
1954 if (ins.handler_idx == WINED3DSIH_DCL)
1956 shader_dump_decl_usage(&buffer, &ins.declaration.semantic, &shader_version);
1957 shader_dump_ins_modifiers(&buffer, &ins.declaration.semantic.reg);
1958 shader_addline(&buffer, " ");
1959 shader_dump_dst_param(&buffer, &ins.declaration.semantic.reg, &shader_version);
1961 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
1963 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
1964 shader_dump_src_param(&buffer, &ins.declaration.src, &shader_version);
1965 shader_addline(&buffer, ", %s",
1966 ins.flags & WINED3DSI_INDEXED_DYNAMIC ? "dynamicIndexed" : "immediateIndexed");
1968 else if (ins.handler_idx == WINED3DSIH_DCL_GLOBAL_FLAGS)
1970 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
1971 shader_dump_global_flags(&buffer, ins.flags);
1973 else if (ins.handler_idx == WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER)
1975 shader_addline(&buffer, "%s {\n", shader_opcode_names[ins.handler_idx]);
1976 for (i = 0; i < ins.declaration.icb->element_count / 4; ++i)
1978 shader_addline(&buffer, " {0x%08x, 0x%08x, 0x%08x, 0x%08x},\n",
1979 ins.declaration.icb->data[4 * i + 0],
1980 ins.declaration.icb->data[4 * i + 1],
1981 ins.declaration.icb->data[4 * i + 2],
1982 ins.declaration.icb->data[4 * i + 3]);
1984 shader_addline(&buffer, "}");
1986 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS)
1988 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
1989 shader_dump_interpolation_mode(&buffer, ins.flags);
1990 shader_addline(&buffer, " ");
1991 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
1993 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SGV
1994 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SGV
1995 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SIV
1996 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_SIV)
1998 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
1999 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2000 shader_addline(&buffer, ", ");
2001 shader_dump_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2003 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SIV)
2005 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2006 shader_dump_interpolation_mode(&buffer, ins.flags);
2007 shader_addline(&buffer, " ");
2008 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2009 shader_addline(&buffer, ", ");
2010 shader_dump_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2012 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT
2013 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT)
2015 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2016 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2018 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE
2019 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
2021 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2022 shader_dump_primitive_type(&buffer, ins.declaration.primitive_type);
2024 else if (ins.handler_idx == WINED3DSIH_DCL_SAMPLER)
2026 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2027 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2028 if (ins.flags == WINED3DSI_SAMPLER_COMPARISON_MODE)
2029 shader_addline(&buffer, ", comparisonMode");
2031 else if (ins.handler_idx == WINED3DSIH_DCL_TEMPS
2032 || ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
2034 shader_addline(&buffer, "%s %u", shader_opcode_names[ins.handler_idx], ins.declaration.count);
2036 else if (ins.handler_idx == WINED3DSIH_DEF)
2038 shader_addline(&buffer, "def c%u = %.8e, %.8e, %.8e, %.8e", shader_get_float_offset(ins.dst[0].reg.type,
2039 ins.dst[0].reg.idx[0].offset),
2040 *(const float *)&ins.src[0].reg.immconst_data[0],
2041 *(const float *)&ins.src[0].reg.immconst_data[1],
2042 *(const float *)&ins.src[0].reg.immconst_data[2],
2043 *(const float *)&ins.src[0].reg.immconst_data[3]);
2045 else if (ins.handler_idx == WINED3DSIH_DEFI)
2047 shader_addline(&buffer, "defi i%u = %d, %d, %d, %d", ins.dst[0].reg.idx[0].offset,
2048 ins.src[0].reg.immconst_data[0],
2049 ins.src[0].reg.immconst_data[1],
2050 ins.src[0].reg.immconst_data[2],
2051 ins.src[0].reg.immconst_data[3]);
2053 else if (ins.handler_idx == WINED3DSIH_DEFB)
2055 shader_addline(&buffer, "defb b%u = %s",
2056 ins.dst[0].reg.idx[0].offset, ins.src[0].reg.immconst_data[0] ? "true" : "false");
2058 else
2060 if (ins.predicate)
2062 shader_addline(&buffer, "(");
2063 shader_dump_src_param(&buffer, ins.predicate, &shader_version);
2064 shader_addline(&buffer, ") ");
2067 /* PixWin marks instructions with the coissue flag with a '+' */
2068 if (ins.coissue)
2069 shader_addline(&buffer, "+");
2071 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2073 if (ins.handler_idx == WINED3DSIH_IFC
2074 || ins.handler_idx == WINED3DSIH_BREAKC)
2076 switch (ins.flags)
2078 case WINED3D_SHADER_REL_OP_GT: shader_addline(&buffer, "_gt"); break;
2079 case WINED3D_SHADER_REL_OP_EQ: shader_addline(&buffer, "_eq"); break;
2080 case WINED3D_SHADER_REL_OP_GE: shader_addline(&buffer, "_ge"); break;
2081 case WINED3D_SHADER_REL_OP_LT: shader_addline(&buffer, "_lt"); break;
2082 case WINED3D_SHADER_REL_OP_NE: shader_addline(&buffer, "_ne"); break;
2083 case WINED3D_SHADER_REL_OP_LE: shader_addline(&buffer, "_le"); break;
2084 default: shader_addline(&buffer, "_(%u)", ins.flags);
2087 else if (ins.handler_idx == WINED3DSIH_TEX
2088 && shader_version.major >= 2
2089 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
2091 shader_addline(&buffer, "p");
2093 else if (ins.handler_idx == WINED3DSIH_RESINFO
2094 && ins.flags)
2096 switch (ins.flags)
2098 case WINED3DSI_RESINFO_RCP_FLOAT: shader_addline(&buffer, "_rcpFloat"); break;
2099 case WINED3DSI_RESINFO_UINT: shader_addline(&buffer, "_uint"); break;
2100 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags);
2104 if (wined3d_shader_instruction_has_texel_offset(&ins))
2105 shader_addline(&buffer, "(%d,%d,%d)", ins.texel_offset.u, ins.texel_offset.v, ins.texel_offset.w);
2107 for (i = 0; i < ins.dst_count; ++i)
2109 shader_dump_ins_modifiers(&buffer, &ins.dst[i]);
2110 shader_addline(&buffer, !i ? " " : ", ");
2111 shader_dump_dst_param(&buffer, &ins.dst[i], &shader_version);
2114 /* Other source tokens */
2115 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
2117 shader_addline(&buffer, !i ? " " : ", ");
2118 shader_dump_src_param(&buffer, &ins.src[i - ins.dst_count], &shader_version);
2121 shader_addline(&buffer, "\n");
2124 for (p = buffer.buffer; *p; p = q)
2126 if (!(q = strstr(p, "\n")))
2127 q = p + strlen(p);
2128 else
2129 ++q;
2130 TRACE(" %.*s", (int)(q - p), p);
2133 string_buffer_free(&buffer);
2136 static void shader_cleanup(struct wined3d_shader *shader)
2138 HeapFree(GetProcessHeap(), 0, shader->output_signature.elements);
2139 HeapFree(GetProcessHeap(), 0, shader->input_signature.elements);
2140 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
2141 shader->device->shader_backend->shader_destroy(shader);
2142 HeapFree(GetProcessHeap(), 0, shader->reg_maps.constf);
2143 HeapFree(GetProcessHeap(), 0, shader->reg_maps.sampler_map.entries);
2144 HeapFree(GetProcessHeap(), 0, shader->function);
2145 shader_delete_constant_list(&shader->constantsF);
2146 shader_delete_constant_list(&shader->constantsB);
2147 shader_delete_constant_list(&shader->constantsI);
2148 list_remove(&shader->shader_list_entry);
2150 if (shader->frontend && shader->frontend_data)
2151 shader->frontend->shader_free(shader->frontend_data);
2154 struct shader_none_priv
2156 const struct wined3d_vertex_pipe_ops *vertex_pipe;
2157 const struct fragment_pipeline *fragment_pipe;
2158 BOOL ffp_proj_control;
2161 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
2162 static void shader_none_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
2163 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size) {}
2164 static void shader_none_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info) {}
2165 static void shader_none_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count) {}
2166 static void shader_none_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count) {}
2167 static void shader_none_load_constants(void *shader_priv, struct wined3d_context *context,
2168 const struct wined3d_state *state) {}
2169 static void shader_none_destroy(struct wined3d_shader *shader) {}
2170 static void shader_none_free_context_data(struct wined3d_context *context) {}
2171 static void shader_none_init_context_state(struct wined3d_context *context) {}
2173 /* Context activation is done by the caller. */
2174 static void shader_none_select(void *shader_priv, struct wined3d_context *context,
2175 const struct wined3d_state *state)
2177 const struct wined3d_gl_info *gl_info = context->gl_info;
2178 struct shader_none_priv *priv = shader_priv;
2180 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
2181 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
2184 /* Context activation is done by the caller. */
2185 static void shader_none_disable(void *shader_priv, struct wined3d_context *context)
2187 struct shader_none_priv *priv = shader_priv;
2188 const struct wined3d_gl_info *gl_info = context->gl_info;
2190 priv->vertex_pipe->vp_enable(gl_info, FALSE);
2191 priv->fragment_pipe->enable_extension(gl_info, FALSE);
2193 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
2194 | (1u << WINED3D_SHADER_TYPE_VERTEX)
2195 | (1u << WINED3D_SHADER_TYPE_GEOMETRY);
2198 static HRESULT shader_none_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
2199 const struct fragment_pipeline *fragment_pipe)
2201 struct fragment_caps fragment_caps;
2202 void *vertex_priv, *fragment_priv;
2203 struct shader_none_priv *priv;
2205 if (!(priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv))))
2206 return E_OUTOFMEMORY;
2208 if (!(vertex_priv = vertex_pipe->vp_alloc(&none_shader_backend, priv)))
2210 ERR("Failed to initialize vertex pipe.\n");
2211 HeapFree(GetProcessHeap(), 0, priv);
2212 return E_FAIL;
2215 if (!(fragment_priv = fragment_pipe->alloc_private(&none_shader_backend, priv)))
2217 ERR("Failed to initialize fragment pipe.\n");
2218 vertex_pipe->vp_free(device);
2219 HeapFree(GetProcessHeap(), 0, priv);
2220 return E_FAIL;
2223 priv->vertex_pipe = vertex_pipe;
2224 priv->fragment_pipe = fragment_pipe;
2225 fragment_pipe->get_caps(&device->adapter->gl_info, &fragment_caps);
2226 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
2228 device->vertex_priv = vertex_priv;
2229 device->fragment_priv = fragment_priv;
2230 device->shader_priv = priv;
2232 return WINED3D_OK;
2235 static void shader_none_free(struct wined3d_device *device)
2237 struct shader_none_priv *priv = device->shader_priv;
2239 priv->fragment_pipe->free_private(device);
2240 priv->vertex_pipe->vp_free(device);
2241 HeapFree(GetProcessHeap(), 0, priv);
2244 static BOOL shader_none_allocate_context_data(struct wined3d_context *context)
2246 return TRUE;
2249 static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
2251 /* Set the shader caps to 0 for the none shader backend */
2252 caps->vs_version = 0;
2253 caps->gs_version = 0;
2254 caps->ps_version = 0;
2255 caps->vs_uniform_count = 0;
2256 caps->ps_uniform_count = 0;
2257 caps->ps_1x_max_value = 0.0f;
2258 caps->varying_count = 0;
2259 caps->wined3d_caps = 0;
2262 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
2264 /* We "support" every possible fixup, since we don't support any shader
2265 * model, and will never have to actually sample a texture. */
2266 return TRUE;
2269 static BOOL shader_none_has_ffp_proj_control(void *shader_priv)
2271 struct shader_none_priv *priv = shader_priv;
2273 return priv->ffp_proj_control;
2276 const struct wined3d_shader_backend_ops none_shader_backend =
2278 shader_none_handle_instruction,
2279 shader_none_select,
2280 shader_none_disable,
2281 shader_none_select_depth_blt,
2282 shader_none_deselect_depth_blt,
2283 shader_none_update_float_vertex_constants,
2284 shader_none_update_float_pixel_constants,
2285 shader_none_load_constants,
2286 shader_none_destroy,
2287 shader_none_alloc,
2288 shader_none_free,
2289 shader_none_allocate_context_data,
2290 shader_none_free_context_data,
2291 shader_none_init_context_state,
2292 shader_none_get_caps,
2293 shader_none_color_fixup_supported,
2294 shader_none_has_ffp_proj_control,
2297 static HRESULT shader_set_function(struct wined3d_shader *shader, const DWORD *byte_code,
2298 const struct wined3d_shader_signature *output_signature, DWORD float_const_count,
2299 enum wined3d_shader_type type, unsigned int max_version)
2301 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2302 const struct wined3d_shader_frontend *fe;
2303 HRESULT hr;
2304 unsigned int backend_version;
2305 const struct wined3d_d3d_info *d3d_info = &shader->device->adapter->d3d_info;
2307 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u, type %#x, max_version %u.\n",
2308 shader, byte_code, output_signature, float_const_count, type, max_version);
2310 list_init(&shader->constantsF);
2311 list_init(&shader->constantsB);
2312 list_init(&shader->constantsI);
2313 shader->lconst_inf_or_nan = FALSE;
2315 fe = shader_select_frontend(*byte_code);
2316 if (!fe)
2318 FIXME("Unable to find frontend for shader.\n");
2319 return WINED3DERR_INVALIDCALL;
2321 shader->frontend = fe;
2322 shader->frontend_data = fe->shader_init(byte_code, output_signature);
2323 if (!shader->frontend_data)
2325 FIXME("Failed to initialize frontend.\n");
2326 return WINED3DERR_INVALIDCALL;
2329 /* First pass: trace shader. */
2330 if (TRACE_ON(d3d_shader))
2331 shader_trace_init(fe, shader->frontend_data, byte_code);
2333 /* Second pass: figure out which registers are used, what the semantics are, etc. */
2334 if (FAILED(hr = shader_get_registers_used(shader, fe, reg_maps, &shader->input_signature,
2335 &shader->output_signature, byte_code, float_const_count)))
2336 return hr;
2338 if (reg_maps->shader_version.type != type)
2340 WARN("Wrong shader type %d.\n", reg_maps->shader_version.type);
2341 return WINED3DERR_INVALIDCALL;
2343 if (reg_maps->shader_version.major > max_version)
2345 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps->shader_version.major);
2346 return WINED3DERR_INVALIDCALL;
2348 switch (type)
2350 case WINED3D_SHADER_TYPE_VERTEX:
2351 backend_version = d3d_info->limits.vs_version;
2352 break;
2353 case WINED3D_SHADER_TYPE_GEOMETRY:
2354 backend_version = d3d_info->limits.gs_version;
2355 break;
2356 case WINED3D_SHADER_TYPE_PIXEL:
2357 backend_version = d3d_info->limits.ps_version;
2358 break;
2359 default:
2360 FIXME("No backend version-checking for this shader type\n");
2361 backend_version = 0;
2363 if (reg_maps->shader_version.major > backend_version)
2365 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
2366 reg_maps->shader_version.major, reg_maps->shader_version.minor);
2367 return WINED3DERR_INVALIDCALL;
2370 shader->function = HeapAlloc(GetProcessHeap(), 0, shader->functionLength);
2371 if (!shader->function)
2372 return E_OUTOFMEMORY;
2373 memcpy(shader->function, byte_code, shader->functionLength);
2375 return WINED3D_OK;
2378 ULONG CDECL wined3d_shader_incref(struct wined3d_shader *shader)
2380 ULONG refcount = InterlockedIncrement(&shader->ref);
2382 TRACE("%p increasing refcount to %u.\n", shader, refcount);
2384 return refcount;
2387 ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
2389 ULONG refcount = InterlockedDecrement(&shader->ref);
2391 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
2393 if (!refcount)
2395 shader_cleanup(shader);
2396 shader->parent_ops->wined3d_object_destroyed(shader->parent);
2397 HeapFree(GetProcessHeap(), 0, shader);
2400 return refcount;
2403 void * CDECL wined3d_shader_get_parent(const struct wined3d_shader *shader)
2405 TRACE("shader %p.\n", shader);
2407 return shader->parent;
2410 HRESULT CDECL wined3d_shader_get_byte_code(const struct wined3d_shader *shader,
2411 void *byte_code, UINT *byte_code_size)
2413 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader, byte_code, byte_code_size);
2415 if (!byte_code)
2417 *byte_code_size = shader->functionLength;
2418 return WINED3D_OK;
2421 if (*byte_code_size < shader->functionLength)
2423 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
2424 * than the required size we should write the required size and
2425 * return D3DERR_MOREDATA. That's not actually true. */
2426 return WINED3DERR_INVALIDCALL;
2429 memcpy(byte_code, shader->function, shader->functionLength);
2431 return WINED3D_OK;
2434 /* Set local constants for d3d8 shaders. */
2435 HRESULT CDECL wined3d_shader_set_local_constants_float(struct wined3d_shader *shader,
2436 UINT start_idx, const float *src_data, UINT count)
2438 UINT end_idx = start_idx + count;
2439 UINT i;
2441 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader, start_idx, src_data, count);
2443 if (end_idx > shader->limits->constant_float)
2445 WARN("end_idx %u > float constants limit %u.\n",
2446 end_idx, shader->limits->constant_float);
2447 end_idx = shader->limits->constant_float;
2450 for (i = start_idx; i < end_idx; ++i)
2452 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
2453 float *value;
2454 if (!lconst)
2455 return E_OUTOFMEMORY;
2457 lconst->idx = i;
2458 value = (float *)lconst->value;
2459 memcpy(value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
2460 list_add_head(&shader->constantsF, &lconst->entry);
2462 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
2463 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
2465 shader->lconst_inf_or_nan = TRUE;
2469 return WINED3D_OK;
2472 void find_vs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
2473 WORD swizzle_map, struct vs_compile_args *args, const struct wined3d_d3d_info *d3d_info)
2475 args->fog_src = state->render_states[WINED3D_RS_FOGTABLEMODE]
2476 == WINED3D_FOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
2477 args->clip_enabled = state->render_states[WINED3D_RS_CLIPPING]
2478 && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
2479 args->point_size = state->gl_primitive_type == GL_POINTS;
2480 args->per_vertex_point_size = shader->reg_maps.point_size;
2481 args->swizzle_map = swizzle_map;
2482 if (d3d_info->emulated_flatshading)
2483 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
2484 else
2485 args->flatshading = FALSE;
2488 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
2490 if (usage_idx1 != usage_idx2)
2491 return FALSE;
2492 if (usage1 == usage2)
2493 return TRUE;
2494 if (usage1 == WINED3D_DECL_USAGE_POSITION && usage2 == WINED3D_DECL_USAGE_POSITIONT)
2495 return TRUE;
2496 if (usage2 == WINED3D_DECL_USAGE_POSITION && usage1 == WINED3D_DECL_USAGE_POSITIONT)
2497 return TRUE;
2499 return FALSE;
2502 BOOL vshader_get_input(const struct wined3d_shader *shader,
2503 BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
2505 WORD map = shader->reg_maps.input_registers;
2506 unsigned int i;
2508 for (i = 0; map; map >>= 1, ++i)
2510 if (!(map & 1)) continue;
2512 if (match_usage(shader->u.vs.attributes[i].usage,
2513 shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
2515 *regnum = i;
2516 return TRUE;
2519 return FALSE;
2522 static HRESULT shader_signature_copy(struct wined3d_shader_signature *dst,
2523 const struct wined3d_shader_signature *src, char **signature_strings)
2525 struct wined3d_shader_signature_element *e;
2526 unsigned int i;
2527 SIZE_T len;
2528 char *ptr;
2530 ptr = *signature_strings;
2532 dst->element_count = src->element_count;
2533 if (!(dst->elements = HeapAlloc(GetProcessHeap(), 0, sizeof(*dst->elements) * dst->element_count)))
2534 return E_OUTOFMEMORY;
2536 for (i = 0; i < src->element_count; ++i)
2538 e = &src->elements[i];
2539 dst->elements[i] = *e;
2541 len = strlen(e->semantic_name);
2542 memcpy(ptr, e->semantic_name, len + 1);
2543 dst->elements[i].semantic_name = ptr;
2544 ptr += len + 1;
2547 *signature_strings = ptr;
2549 return WINED3D_OK;
2552 static HRESULT shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2553 const struct wined3d_shader_desc *desc, DWORD float_const_count, enum wined3d_shader_type type,
2554 void *parent, const struct wined3d_parent_ops *parent_ops)
2556 struct wined3d_shader_signature_element *e;
2557 SIZE_T total, len;
2558 unsigned int i;
2559 HRESULT hr;
2560 char *ptr;
2562 if (!desc->byte_code)
2563 return WINED3DERR_INVALIDCALL;
2565 shader->ref = 1;
2566 shader->device = device;
2567 shader->parent = parent;
2568 shader->parent_ops = parent_ops;
2570 total = 0;
2571 if (desc->input_signature)
2573 for (i = 0; i < desc->input_signature->element_count; ++i)
2575 e = &desc->input_signature->elements[i];
2576 len = strlen(e->semantic_name);
2577 if (len >= ~(SIZE_T)0 - total)
2578 return E_OUTOFMEMORY;
2580 total += len + 1;
2583 if (desc->output_signature)
2585 for (i = 0; i < desc->output_signature->element_count; ++i)
2587 e = &desc->output_signature->elements[i];
2588 len = strlen(e->semantic_name);
2589 if (len >= ~(SIZE_T)0 - total)
2590 return E_OUTOFMEMORY;
2592 total += len + 1;
2595 if (!(shader->signature_strings = HeapAlloc(GetProcessHeap(), 0, total)))
2596 return E_OUTOFMEMORY;
2597 ptr = shader->signature_strings;
2599 if (desc->input_signature && FAILED(hr = shader_signature_copy(&shader->input_signature,
2600 desc->input_signature, &ptr)))
2602 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
2603 return hr;
2605 if (desc->output_signature && FAILED(hr = shader_signature_copy(&shader->output_signature,
2606 desc->output_signature, &ptr)))
2608 HeapFree(GetProcessHeap(), 0, shader->input_signature.elements);
2609 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
2610 return hr;
2613 list_init(&shader->linked_programs);
2614 list_add_head(&device->shaders, &shader->shader_list_entry);
2616 if (FAILED(hr = shader_set_function(shader, desc->byte_code, desc->output_signature,
2617 float_const_count, type, desc->max_version)))
2619 WARN("Failed to set function, hr %#x.\n", hr);
2620 shader_cleanup(shader);
2623 return hr;
2626 static HRESULT vertexshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2627 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
2629 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2630 unsigned int i;
2631 HRESULT hr;
2633 if (FAILED(hr = shader_init(shader, device, desc, device->adapter->d3d_info.limits.vs_uniform_count,
2634 WINED3D_SHADER_TYPE_VERTEX, parent, parent_ops)))
2635 return hr;
2637 for (i = 0; i < shader->input_signature.element_count; ++i)
2639 const struct wined3d_shader_signature_element *input = &shader->input_signature.elements[i];
2641 if (!(reg_maps->input_registers & (1u << input->register_idx)) || !input->semantic_name)
2642 continue;
2644 shader->u.vs.attributes[input->register_idx].usage =
2645 shader_usage_from_semantic_name(input->semantic_name);
2646 shader->u.vs.attributes[input->register_idx].usage_idx = input->semantic_idx;
2649 shader->load_local_constsF = (reg_maps->usesrelconstF && !list_empty(&shader->constantsF)) ||
2650 shader->lconst_inf_or_nan;
2652 return WINED3D_OK;
2655 static HRESULT geometryshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2656 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
2658 HRESULT hr;
2660 if (FAILED(hr = shader_init(shader, device, desc, 0,
2661 WINED3D_SHADER_TYPE_GEOMETRY, parent, parent_ops)))
2662 return hr;
2664 shader->load_local_constsF = shader->lconst_inf_or_nan;
2666 return WINED3D_OK;
2669 void find_ps_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
2670 BOOL position_transformed, struct ps_compile_args *args, const struct wined3d_context *context)
2672 const struct wined3d_gl_info *gl_info = context->gl_info;
2673 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
2674 const struct wined3d_texture *texture;
2675 UINT i;
2677 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
2678 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, state->fb))
2680 static unsigned int warned = 0;
2682 args->srgb_correction = 1;
2683 if (state->render_states[WINED3D_RS_ALPHABLENDENABLE] && !warned++)
2684 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
2685 "support, expect rendering artifacts.\n");
2688 if (shader->reg_maps.shader_version.major == 1
2689 && shader->reg_maps.shader_version.minor <= 3)
2691 for (i = 0; i < shader->limits->sampler; ++i)
2693 DWORD flags = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
2695 if (flags & WINED3D_TTFF_PROJECTED)
2697 DWORD tex_transform = flags & ~WINED3D_TTFF_PROJECTED;
2699 if (!state->shader[WINED3D_SHADER_TYPE_VERTEX])
2701 enum wined3d_shader_resource_type resource_type = shader->reg_maps.resource_info[i].type;
2702 unsigned int j;
2703 unsigned int index = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
2704 DWORD max_valid = WINED3D_TTFF_COUNT4;
2706 for (j = 0; j < state->vertex_declaration->element_count; ++j)
2708 struct wined3d_vertex_declaration_element *element =
2709 &state->vertex_declaration->elements[j];
2711 if (element->usage == WINED3D_DECL_USAGE_TEXCOORD
2712 && element->usage_idx == index)
2714 max_valid = element->format->component_count;
2715 break;
2718 if (!tex_transform || tex_transform > max_valid)
2720 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2721 tex_transform, max_valid);
2722 tex_transform = max_valid;
2724 if ((resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1D && tex_transform > WINED3D_TTFF_COUNT1)
2725 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D
2726 && tex_transform > WINED3D_TTFF_COUNT2)
2727 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_3D
2728 && tex_transform > WINED3D_TTFF_COUNT3))
2729 tex_transform |= WINED3D_PSARGS_PROJECTED;
2730 else
2732 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2733 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2734 i, tex_transform, resource_type);
2737 else
2738 tex_transform = WINED3D_TTFF_COUNT4 | WINED3D_PSARGS_PROJECTED;
2740 args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
2744 if (shader->reg_maps.shader_version.major == 1
2745 && shader->reg_maps.shader_version.minor <= 4)
2747 for (i = 0; i < shader->limits->sampler; ++i)
2749 const struct wined3d_texture *texture = state->textures[i];
2751 if (!shader->reg_maps.resource_info[i].type)
2752 continue;
2754 /* Treat unbound textures as 2D. The dummy texture will provide
2755 * the proper sample value. The tex_types bitmap defaults to
2756 * 2D because of the memset. */
2757 if (!texture)
2758 continue;
2760 switch (texture->target)
2762 /* RECT textures are distinguished from 2D textures via np2_fixup */
2763 case GL_TEXTURE_RECTANGLE_ARB:
2764 case GL_TEXTURE_2D:
2765 break;
2767 case GL_TEXTURE_3D:
2768 args->tex_types |= WINED3D_SHADER_TEX_3D << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
2769 break;
2771 case GL_TEXTURE_CUBE_MAP_ARB:
2772 args->tex_types |= WINED3D_SHADER_TEX_CUBE << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
2773 break;
2778 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2780 if (!shader->reg_maps.resource_info[i].type)
2781 continue;
2783 texture = state->textures[i];
2784 if (!texture)
2786 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
2787 continue;
2789 if (can_use_texture_swizzle(gl_info, texture->resource.format))
2790 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
2791 else
2792 args->color_fixup[i] = texture->resource.format->color_fixup;
2794 if (texture->resource.format_flags & WINED3DFMT_FLAG_SHADOW)
2795 args->shadow |= 1u << i;
2797 /* Flag samplers that need NP2 texcoord fixup. */
2798 if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
2799 args->np2_fixup |= (1u << i);
2802 /* In SM4+ we use dcl_sampler in order to determine if we should use shadow sampler. */
2803 if (shader->reg_maps.shader_version.major >= 4)
2804 args->shadow = 0;
2806 if (shader->reg_maps.shader_version.major >= 3)
2808 if (position_transformed)
2809 args->vp_mode = pretransformed;
2810 else if (use_vs(state))
2811 args->vp_mode = vertexshader;
2812 else
2813 args->vp_mode = fixedfunction;
2814 args->fog = WINED3D_FFP_PS_FOG_OFF;
2816 else
2818 args->vp_mode = vertexshader;
2819 if (state->render_states[WINED3D_RS_FOGENABLE])
2821 switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
2823 case WINED3D_FOG_NONE:
2824 if (position_transformed || use_vs(state))
2826 args->fog = WINED3D_FFP_PS_FOG_LINEAR;
2827 break;
2830 switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
2832 case WINED3D_FOG_NONE: /* Fall through. */
2833 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
2834 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
2835 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
2837 break;
2839 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
2840 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
2841 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
2844 else
2846 args->fog = WINED3D_FFP_PS_FOG_OFF;
2850 if (context->d3d_info->limits.varying_count < wined3d_max_compat_varyings(context->gl_info))
2852 const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
2854 args->texcoords_initialized = 0;
2855 for (i = 0; i < MAX_TEXTURES; ++i)
2857 if (vs)
2859 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.output_registers & (1u << i))
2860 args->texcoords_initialized |= 1u << i;
2862 else
2864 const struct wined3d_stream_info *si = &context->stream_info;
2865 unsigned int coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
2867 if ((state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX] >> WINED3D_FFP_TCI_SHIFT)
2868 & WINED3D_FFP_TCI_MASK
2869 || (coord_idx < MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))))
2870 args->texcoords_initialized |= 1u << i;
2874 else
2876 args->texcoords_initialized = (1u << MAX_TEXTURES) - 1;
2879 args->pointsprite = state->render_states[WINED3D_RS_POINTSPRITEENABLE]
2880 && state->gl_primitive_type == GL_POINTS;
2882 if (d3d_info->emulated_flatshading)
2883 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
2886 static HRESULT pixelshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2887 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
2889 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2890 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
2891 HRESULT hr;
2893 if (FAILED(hr = shader_init(shader, device, desc, device->adapter->d3d_info.limits.ps_uniform_count,
2894 WINED3D_SHADER_TYPE_PIXEL, parent, parent_ops)))
2895 return hr;
2897 for (i = 0; i < MAX_REG_INPUT; ++i)
2899 if (shader->u.ps.input_reg_used[i])
2901 ++num_regs_used;
2902 highest_reg_used = i;
2906 /* Don't do any register mapping magic if it is not needed, or if we can't
2907 * achieve anything anyway */
2908 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
2909 || num_regs_used > (gl_info->limits.glsl_varyings / 4))
2911 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
2913 /* This happens with relative addressing. The input mapper function
2914 * warns about this if the higher registers are declared too, so
2915 * don't write a FIXME here */
2916 WARN("More varying registers used than supported\n");
2919 for (i = 0; i < MAX_REG_INPUT; ++i)
2921 shader->u.ps.input_reg_map[i] = i;
2924 shader->u.ps.declared_in_count = highest_reg_used + 1;
2926 else
2928 shader->u.ps.declared_in_count = 0;
2929 for (i = 0; i < MAX_REG_INPUT; ++i)
2931 if (shader->u.ps.input_reg_used[i])
2932 shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
2933 else shader->u.ps.input_reg_map[i] = ~0U;
2937 shader->load_local_constsF = shader->lconst_inf_or_nan;
2939 return WINED3D_OK;
2942 void pixelshader_update_resource_types(struct wined3d_shader *shader, WORD tex_types)
2944 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2945 struct wined3d_shader_resource_info *resource_info = reg_maps->resource_info;
2946 unsigned int i;
2948 if (reg_maps->shader_version.major != 1) return;
2950 for (i = 0; i < shader->limits->sampler; ++i)
2952 /* We don't sample from this sampler. */
2953 if (!resource_info[i].type)
2954 continue;
2956 switch ((tex_types >> i * WINED3D_PSARGS_TEXTYPE_SHIFT) & WINED3D_PSARGS_TEXTYPE_MASK)
2958 case WINED3D_SHADER_TEX_2D:
2959 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2960 break;
2962 case WINED3D_SHADER_TEX_3D:
2963 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_3D;
2964 break;
2966 case WINED3D_SHADER_TEX_CUBE:
2967 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_CUBE;
2968 break;
2973 HRESULT CDECL wined3d_shader_create_gs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
2974 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
2976 struct wined3d_shader *object;
2977 HRESULT hr;
2979 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
2980 device, desc, parent, parent_ops, shader);
2982 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2983 if (!object)
2984 return E_OUTOFMEMORY;
2986 if (FAILED(hr = geometryshader_init(object, device, desc, parent, parent_ops)))
2988 WARN("Failed to initialize geometry shader, hr %#x.\n", hr);
2989 HeapFree(GetProcessHeap(), 0, object);
2990 return hr;
2993 TRACE("Created geometry shader %p.\n", object);
2994 *shader = object;
2996 return WINED3D_OK;
2999 HRESULT CDECL wined3d_shader_create_ps(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3000 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3002 struct wined3d_shader *object;
3003 HRESULT hr;
3005 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3006 device, desc, parent, parent_ops, shader);
3008 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3009 if (!object)
3010 return E_OUTOFMEMORY;
3012 if (FAILED(hr = pixelshader_init(object, device, desc, parent, parent_ops)))
3014 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3015 HeapFree(GetProcessHeap(), 0, object);
3016 return hr;
3019 TRACE("Created pixel shader %p.\n", object);
3020 *shader = object;
3022 return WINED3D_OK;
3025 HRESULT CDECL wined3d_shader_create_vs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3026 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3028 struct wined3d_shader *object;
3029 HRESULT hr;
3031 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3032 device, desc, parent, parent_ops, shader);
3034 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3035 if (!object)
3036 return E_OUTOFMEMORY;
3038 if (FAILED(hr = vertexshader_init(object, device, desc, parent, parent_ops)))
3040 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
3041 HeapFree(GetProcessHeap(), 0, object);
3042 return hr;
3045 TRACE("Created vertex shader %p.\n", object);
3046 *shader = object;
3048 return WINED3D_OK;