wined3d: Set load_local_constsF in shader_init().
[wine.git] / dlls / wined3d / shader.c
blobd671ad009e88e461a541ef6db20e3b1b496464cb
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_BFI */ "bfi",
48 /* WINED3DSIH_BFREV */ "bfrev",
49 /* WINED3DSIH_BREAK */ "break",
50 /* WINED3DSIH_BREAKC */ "breakc",
51 /* WINED3DSIH_BREAKP */ "breakp",
52 /* WINED3DSIH_BUFINFO */ "bufinfo",
53 /* WINED3DSIH_CALL */ "call",
54 /* WINED3DSIH_CALLNZ */ "callnz",
55 /* WINED3DSIH_CASE */ "case",
56 /* WINED3DSIH_CMP */ "cmp",
57 /* WINED3DSIH_CND */ "cnd",
58 /* WINED3DSIH_CONTINUE */ "continue",
59 /* WINED3DSIH_CRS */ "crs",
60 /* WINED3DSIH_CUT */ "cut",
61 /* WINED3DSIH_CUT_STREAM */ "cut_stream",
62 /* WINED3DSIH_DCL */ "dcl",
63 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
64 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ "dcl_globalFlags",
65 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ "dcl_hs_fork_phase_instance_count",
66 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ "dcl_hs_max_tessfactor",
67 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ "dcl_immediateConstantBuffer",
68 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ "dcl_indexableTemp",
69 /* WINED3DSIH_DCL_INPUT */ "dcl_input",
70 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ "dcl_input_control_point_count",
71 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
72 /* WINED3DSIH_DCL_INPUT_PS */ "dcl_input_ps",
73 /* WINED3DSIH_DCL_INPUT_PS_SGV */ "dcl_input_ps_sgv",
74 /* WINED3DSIH_DCL_INPUT_PS_SIV */ "dcl_input_ps_siv",
75 /* WINED3DSIH_DCL_INPUT_SGV */ "dcl_input_sgv",
76 /* WINED3DSIH_DCL_INPUT_SIV */ "dcl_input_siv",
77 /* WINED3DSIH_DCL_OUTPUT */ "dcl_output",
78 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ "dcl_output_control_point_count",
79 /* WINED3DSIH_DCL_OUTPUT_SIV */ "dcl_output_siv",
80 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
81 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ "dcl_resource_structured",
82 /* WINED3DSIH_DCL_SAMPLER */ "dcl_sampler",
83 /* WINED3DSIH_DCL_STREAM */ "dcl_stream",
84 /* WINED3DSIH_DCL_TEMPS */ "dcl_temps",
85 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ "dcl_tessellator_domain",
86 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ "dcl_tessellator_output_primitive",
87 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ "dcl_tessellator_partitioning",
88 /* WINED3DSIH_DCL_TGSM_RAW */ "dcl_tgsm_raw",
89 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ "dcl_tgsm_structured",
90 /* WINED3DSIH_DCL_THREAD_GROUP */ "dcl_thread_group",
91 /* WINED3DSIH_DCL_UAV_TYPED */ "dcl_uav_typed",
92 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
93 /* WINED3DSIH_DEF */ "def",
94 /* WINED3DSIH_DEFAULT */ "default",
95 /* WINED3DSIH_DEFB */ "defb",
96 /* WINED3DSIH_DEFI */ "defi",
97 /* WINED3DSIH_DIV */ "div",
98 /* WINED3DSIH_DP2 */ "dp2",
99 /* WINED3DSIH_DP2ADD */ "dp2add",
100 /* WINED3DSIH_DP3 */ "dp3",
101 /* WINED3DSIH_DP4 */ "dp4",
102 /* WINED3DSIH_DST */ "dst",
103 /* WINED3DSIH_DSX */ "dsx",
104 /* WINED3DSIH_DSX_COARSE */ "deriv_rtx_coarse",
105 /* WINED3DSIH_DSX_FINE */ "deriv_rtx_fine",
106 /* WINED3DSIH_DSY */ "dsy",
107 /* WINED3DSIH_DSY_COARSE */ "deriv_rty_coarse",
108 /* WINED3DSIH_DSY_FINE */ "deriv_rty_fine",
109 /* WINED3DSIH_ELSE */ "else",
110 /* WINED3DSIH_EMIT */ "emit",
111 /* WINED3DSIH_EMIT_STREAM */ "emit_stream",
112 /* WINED3DSIH_ENDIF */ "endif",
113 /* WINED3DSIH_ENDLOOP */ "endloop",
114 /* WINED3DSIH_ENDREP */ "endrep",
115 /* WINED3DSIH_ENDSWITCH */ "endswitch",
116 /* WINED3DSIH_EQ */ "eq",
117 /* WINED3DSIH_EXP */ "exp",
118 /* WINED3DSIH_EXPP */ "expp",
119 /* WINED3DSIH_FRC */ "frc",
120 /* WINED3DSIH_FTOI */ "ftoi",
121 /* WINED3DSIH_FTOU */ "ftou",
122 /* WINED3DSIH_GATHER4 */ "gather4",
123 /* WINED3DSIH_GATHER4_C */ "gather4_c",
124 /* WINED3DSIH_GE */ "ge",
125 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ "hs_control_point_phase",
126 /* WINED3DSIH_HS_DECLS */ "hs_decls",
127 /* WINED3DSIH_HS_FORK_PHASE */ "hs_fork_phase",
128 /* WINED3DSIH_HS_JOIN_PHASE */ "hs_join_phase",
129 /* WINED3DSIH_IADD */ "iadd",
130 /* WINED3DSIH_IEQ */ "ieq",
131 /* WINED3DSIH_IF */ "if",
132 /* WINED3DSIH_IFC */ "ifc",
133 /* WINED3DSIH_IGE */ "ige",
134 /* WINED3DSIH_ILT */ "ilt",
135 /* WINED3DSIH_IMAD */ "imad",
136 /* WINED3DSIH_IMAX */ "imax",
137 /* WINED3DSIH_IMIN */ "imin",
138 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ "imm_atomic_alloc",
139 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ "imm_atomic_consume",
140 /* WINED3DSIH_IMUL */ "imul",
141 /* WINED3DSIH_INE */ "ine",
142 /* WINED3DSIH_INEG */ "ineg",
143 /* WINED3DSIH_ISHL */ "ishl",
144 /* WINED3DSIH_ISHR */ "ishr",
145 /* WINED3DSIH_ITOF */ "itof",
146 /* WINED3DSIH_LABEL */ "label",
147 /* WINED3DSIH_LD */ "ld",
148 /* WINED3DSIH_LD2DMS */ "ld2dms",
149 /* WINED3DSIH_LD_RAW */ "ld_raw",
150 /* WINED3DSIH_LD_STRUCTURED */ "ld_structured",
151 /* WINED3DSIH_LD_UAV_TYPED */ "ld_uav_typed",
152 /* WINED3DSIH_LIT */ "lit",
153 /* WINED3DSIH_LOG */ "log",
154 /* WINED3DSIH_LOGP */ "logp",
155 /* WINED3DSIH_LOOP */ "loop",
156 /* WINED3DSIH_LRP */ "lrp",
157 /* WINED3DSIH_LT */ "lt",
158 /* WINED3DSIH_M3x2 */ "m3x2",
159 /* WINED3DSIH_M3x3 */ "m3x3",
160 /* WINED3DSIH_M3x4 */ "m3x4",
161 /* WINED3DSIH_M4x3 */ "m4x3",
162 /* WINED3DSIH_M4x4 */ "m4x4",
163 /* WINED3DSIH_MAD */ "mad",
164 /* WINED3DSIH_MAX */ "max",
165 /* WINED3DSIH_MIN */ "min",
166 /* WINED3DSIH_MOV */ "mov",
167 /* WINED3DSIH_MOVA */ "mova",
168 /* WINED3DSIH_MOVC */ "movc",
169 /* WINED3DSIH_MUL */ "mul",
170 /* WINED3DSIH_NE */ "ne",
171 /* WINED3DSIH_NOP */ "nop",
172 /* WINED3DSIH_NOT */ "not",
173 /* WINED3DSIH_NRM */ "nrm",
174 /* WINED3DSIH_OR */ "or",
175 /* WINED3DSIH_PHASE */ "phase",
176 /* WINED3DSIH_POW */ "pow",
177 /* WINED3DSIH_RCP */ "rcp",
178 /* WINED3DSIH_REP */ "rep",
179 /* WINED3DSIH_RESINFO */ "resinfo",
180 /* WINED3DSIH_RET */ "ret",
181 /* WINED3DSIH_ROUND_NE */ "round_ne",
182 /* WINED3DSIH_ROUND_NI */ "round_ni",
183 /* WINED3DSIH_ROUND_PI */ "round_pi",
184 /* WINED3DSIH_ROUND_Z */ "round_z",
185 /* WINED3DSIH_RSQ */ "rsq",
186 /* WINED3DSIH_SAMPLE */ "sample",
187 /* WINED3DSIH_SAMPLE_B */ "sample_b",
188 /* WINED3DSIH_SAMPLE_C */ "sample_c",
189 /* WINED3DSIH_SAMPLE_C_LZ */ "sample_c_lz",
190 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
191 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
192 /* WINED3DSIH_SETP */ "setp",
193 /* WINED3DSIH_SGE */ "sge",
194 /* WINED3DSIH_SGN */ "sgn",
195 /* WINED3DSIH_SINCOS */ "sincos",
196 /* WINED3DSIH_SLT */ "slt",
197 /* WINED3DSIH_SQRT */ "sqrt",
198 /* WINED3DSIH_STORE_RAW */ "store_raw",
199 /* WINED3DSIH_STORE_STRUCTURED */ "store_structured",
200 /* WINED3DSIH_STORE_UAV_TYPED */ "store_uav_typed",
201 /* WINED3DSIH_SUB */ "sub",
202 /* WINED3DSIH_SWAPC */ "swapc",
203 /* WINED3DSIH_SWITCH */ "switch",
204 /* WINED3DSIH_TEX */ "texld",
205 /* WINED3DSIH_TEXBEM */ "texbem",
206 /* WINED3DSIH_TEXBEML */ "texbeml",
207 /* WINED3DSIH_TEXCOORD */ "texcrd",
208 /* WINED3DSIH_TEXDEPTH */ "texdepth",
209 /* WINED3DSIH_TEXDP3 */ "texdp3",
210 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
211 /* WINED3DSIH_TEXKILL */ "texkill",
212 /* WINED3DSIH_TEXLDD */ "texldd",
213 /* WINED3DSIH_TEXLDL */ "texldl",
214 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
215 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
216 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
217 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
218 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
219 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
220 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
221 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
222 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
223 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
224 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
225 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
226 /* WINED3DSIH_UDIV */ "udiv",
227 /* WINED3DSIH_UGE */ "uge",
228 /* WINED3DSIH_ULT */ "ult",
229 /* WINED3DSIH_UMAX */ "umax",
230 /* WINED3DSIH_USHR */ "ushr",
231 /* WINED3DSIH_UTOF */ "utof",
232 /* WINED3DSIH_XOR */ "xor",
235 static const char * const semantic_names[] =
237 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
238 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
239 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
240 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
241 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
242 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
243 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
244 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
245 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
246 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
247 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
248 /* WINED3D_DECL_USAGE_FOG */ "FOG",
249 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
250 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
253 static const struct
255 enum wined3d_sysval_semantic sysval_semantic;
256 const char *sysval_name;
258 sysval_semantic_names[] =
260 {WINED3D_SV_POSITION, "SV_Position"},
261 {WINED3D_SV_CLIP_DISTANCE, "SV_ClipDistance"},
262 {WINED3D_SV_CULL_DISTANCE, "SV_CullDistance"},
263 {WINED3D_SV_RENDER_TARGET_ARRAY_INDEX, "SV_RenderTargetArrayIndex"},
264 {WINED3D_SV_VIEWPORT_ARRAY_INDEX, "SV_ViewportArrayIndex"},
265 {WINED3D_SV_VERTEX_ID, "SV_VertexID"},
266 {WINED3D_SV_INSTANCE_ID, "SV_InstanceID"},
267 {WINED3D_SV_PRIMITIVE_ID, "SV_PrimitiveID"},
268 {WINED3D_SV_IS_FRONT_FACE, "SV_IsFrontFace"},
269 {WINED3D_SV_SAMPLE_INDEX, "SV_SampleIndex"},
270 {WINED3D_SV_QUAD_U0_TESS_FACTOR, "finalQuadUeq0EdgeTessFactor"},
271 {WINED3D_SV_QUAD_V0_TESS_FACTOR, "finalQuadVeq0EdgeTessFactor"},
272 {WINED3D_SV_QUAD_U1_TESS_FACTOR, "finalQuadUeq1EdgeTessFactor"},
273 {WINED3D_SV_QUAD_V1_TESS_FACTOR, "finalQuadVeq1EdgeTessFactor"},
274 {WINED3D_SV_QUAD_U_INNER_TESS_FACTOR, "finalQuadUInsideTessFactor"},
275 {WINED3D_SV_QUAD_V_INNER_TESS_FACTOR, "finalQuadVInsideTessFactor"},
276 {WINED3D_SV_TRIANGLE_U_TESS_FACTOR, "finalTriUeq0EdgeTessFactor"},
277 {WINED3D_SV_TRIANGLE_V_TESS_FACTOR, "finalTriVeq0EdgeTessFactor"},
278 {WINED3D_SV_TRIANGLE_W_TESS_FACTOR, "finalTriWeq0EdgeTessFactor"},
279 {WINED3D_SV_TRIANGLE_INNER_TESS_FACTOR, "finalTriInsideTessFactor"},
280 {WINED3D_SV_LINE_DETAIL_TESS_FACTOR, "finalLineDetailTessFactor"},
281 {WINED3D_SV_LINE_DENSITY_TESS_FACTOR, "finalLineDensityTessFactor"},
284 static void shader_dump_src_param(struct wined3d_string_buffer *buffer,
285 const struct wined3d_shader_src_param *param, const struct wined3d_shader_version *shader_version);
287 const char *debug_d3dshaderinstructionhandler(enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx)
289 if (handler_idx >= ARRAY_SIZE(shader_opcode_names))
290 return wine_dbg_sprintf("UNRECOGNIZED(%#x)", handler_idx);
292 return shader_opcode_names[handler_idx];
295 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage)
297 if (usage >= ARRAY_SIZE(semantic_names))
299 FIXME("Unrecognized usage %#x.\n", usage);
300 return "UNRECOGNIZED";
303 return semantic_names[usage];
306 static enum wined3d_decl_usage shader_usage_from_semantic_name(const char *name)
308 unsigned int i;
310 for (i = 0; i < ARRAY_SIZE(semantic_names); ++i)
312 if (!strcmp(name, semantic_names[i]))
313 return i;
316 return ~0U;
319 static enum wined3d_sysval_semantic shader_sysval_semantic_from_usage(enum wined3d_decl_usage usage)
321 switch (usage)
323 case WINED3D_DECL_USAGE_POSITION:
324 return WINED3D_SV_POSITION;
325 default:
326 return 0;
330 BOOL shader_match_semantic(const char *semantic_name, enum wined3d_decl_usage usage)
332 return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
335 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
336 const struct wined3d_shader_semantic *s)
338 e->semantic_name = shader_semantic_name_from_usage(s->usage);
339 e->semantic_idx = s->usage_idx;
340 e->sysval_semantic = shader_sysval_semantic_from_usage(s->usage);
341 e->component_type = WINED3D_TYPE_FLOAT;
342 e->register_idx = s->reg.reg.idx[0].offset;
343 e->mask = s->reg.write_mask;
346 static void shader_signature_from_usage(struct wined3d_shader_signature_element *e,
347 enum wined3d_decl_usage usage, UINT usage_idx, UINT reg_idx, DWORD write_mask)
349 e->semantic_name = shader_semantic_name_from_usage(usage);
350 e->semantic_idx = usage_idx;
351 e->sysval_semantic = shader_sysval_semantic_from_usage(usage);
352 e->component_type = WINED3D_TYPE_FLOAT;
353 e->register_idx = reg_idx;
354 e->mask = write_mask;
357 static const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token)
359 switch (version_token >> 16)
361 case WINED3D_SM1_VS:
362 case WINED3D_SM1_PS:
363 return &sm1_shader_frontend;
365 case WINED3D_SM4_PS:
366 case WINED3D_SM4_VS:
367 case WINED3D_SM4_GS:
368 case WINED3D_SM5_HS:
369 case WINED3D_SM5_DS:
370 case WINED3D_SM5_CS:
371 return &sm4_shader_frontend;
373 default:
374 FIXME("Unrecognised version token %#x.\n", version_token);
375 return NULL;
379 void string_buffer_clear(struct wined3d_string_buffer *buffer)
381 buffer->buffer[0] = '\0';
382 buffer->content_size = 0;
385 BOOL string_buffer_init(struct wined3d_string_buffer *buffer)
387 buffer->buffer_size = 32;
388 if (!(buffer->buffer = HeapAlloc(GetProcessHeap(), 0, buffer->buffer_size)))
390 ERR("Failed to allocate shader buffer memory.\n");
391 return FALSE;
394 string_buffer_clear(buffer);
395 return TRUE;
398 void string_buffer_free(struct wined3d_string_buffer *buffer)
400 HeapFree(GetProcessHeap(), 0, buffer->buffer);
403 BOOL string_buffer_resize(struct wined3d_string_buffer *buffer, int rc)
405 char *new_buffer;
406 unsigned int new_buffer_size = buffer->buffer_size * 2;
408 while (rc > 0 && (unsigned int)rc >= new_buffer_size - buffer->content_size)
409 new_buffer_size *= 2;
410 if (!(new_buffer = HeapReAlloc(GetProcessHeap(), 0, buffer->buffer, new_buffer_size)))
412 ERR("Failed to grow buffer.\n");
413 buffer->buffer[buffer->content_size] = '\0';
414 return FALSE;
416 buffer->buffer = new_buffer;
417 buffer->buffer_size = new_buffer_size;
418 return TRUE;
421 int shader_vaddline(struct wined3d_string_buffer *buffer, const char *format, va_list args)
423 unsigned int rem;
424 int rc;
426 rem = buffer->buffer_size - buffer->content_size;
427 rc = vsnprintf(&buffer->buffer[buffer->content_size], rem, format, args);
428 if (rc < 0 /* C89 */ || (unsigned int)rc >= rem /* C99 */)
429 return rc;
431 buffer->content_size += rc;
432 return 0;
435 int shader_addline(struct wined3d_string_buffer *buffer, const char *format, ...)
437 va_list args;
438 int ret;
440 for (;;)
442 va_start(args, format);
443 ret = shader_vaddline(buffer, format, args);
444 va_end(args);
445 if (!ret)
446 return ret;
447 if (!string_buffer_resize(buffer, ret))
448 return -1;
452 struct wined3d_string_buffer *string_buffer_get(struct wined3d_string_buffer_list *list)
454 struct wined3d_string_buffer *buffer;
456 if (list_empty(&list->list))
458 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer));
459 if (!buffer || !string_buffer_init(buffer))
461 ERR("Couldn't allocate buffer for temporary string.\n");
462 HeapFree(GetProcessHeap(), 0, buffer);
463 return NULL;
466 else
468 buffer = LIST_ENTRY(list_head(&list->list), struct wined3d_string_buffer, entry);
469 list_remove(&buffer->entry);
471 string_buffer_clear(buffer);
472 return buffer;
475 static int string_buffer_vsprintf(struct wined3d_string_buffer *buffer, const char *format, va_list args)
477 if (!buffer)
478 return 0;
479 string_buffer_clear(buffer);
480 return shader_vaddline(buffer, format, args);
483 void string_buffer_sprintf(struct wined3d_string_buffer *buffer, const char *format, ...)
485 va_list args;
486 int ret;
488 for (;;)
490 va_start(args, format);
491 ret = string_buffer_vsprintf(buffer, format, args);
492 va_end(args);
493 if (!ret)
494 return;
495 if (!string_buffer_resize(buffer, ret))
496 return;
500 void string_buffer_release(struct wined3d_string_buffer_list *list, struct wined3d_string_buffer *buffer)
502 if (!buffer)
503 return;
504 list_add_head(&list->list, &buffer->entry);
507 void string_buffer_list_init(struct wined3d_string_buffer_list *list)
509 list_init(&list->list);
512 void string_buffer_list_cleanup(struct wined3d_string_buffer_list *list)
514 struct wined3d_string_buffer *buffer, *buffer_next;
516 LIST_FOR_EACH_ENTRY_SAFE(buffer, buffer_next, &list->list, struct wined3d_string_buffer, entry)
518 string_buffer_free(buffer);
519 HeapFree(GetProcessHeap(), 0, buffer);
521 list_init(&list->list);
524 /* Convert floating point offset relative to a register file to an absolute
525 * offset for float constants. */
526 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type, UINT register_idx)
528 switch (register_type)
530 case WINED3DSPR_CONST: return register_idx;
531 case WINED3DSPR_CONST2: return 2048 + register_idx;
532 case WINED3DSPR_CONST3: return 4096 + register_idx;
533 case WINED3DSPR_CONST4: return 6144 + register_idx;
534 default:
535 FIXME("Unsupported register type: %u.\n", register_type);
536 return register_idx;
540 static void shader_delete_constant_list(struct list *clist)
542 struct wined3d_shader_lconst *constant, *constant_next;
544 LIST_FOR_EACH_ENTRY_SAFE(constant, constant_next, clist, struct wined3d_shader_lconst, entry)
545 HeapFree(GetProcessHeap(), 0, constant);
546 list_init(clist);
549 static void shader_set_limits(struct wined3d_shader *shader)
551 static const struct limits_entry
553 unsigned int min_version;
554 unsigned int max_version;
555 struct wined3d_shader_limits limits;
557 vs_limits[] =
559 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
560 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
561 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
562 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
563 * even though they are capable of supporting much more (GL
564 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
565 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
566 * shaders to 256. */
567 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
568 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
569 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 0}},
572 hs_limits[] =
574 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
575 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
577 ds_limits[] =
579 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
580 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
582 gs_limits[] =
584 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
585 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
586 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
589 ps_limits[] =
591 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
592 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
593 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
594 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
595 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
596 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 12}},
597 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 32}},
600 cs_limits[] =
602 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
603 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 0}},
605 const struct limits_entry *limits_array;
606 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
607 shader->reg_maps.shader_version.minor);
608 int i = 0;
610 switch (shader->reg_maps.shader_version.type)
612 default:
613 FIXME("Unexpected shader type %u found.\n", shader->reg_maps.shader_version.type);
614 /* Fall-through. */
615 case WINED3D_SHADER_TYPE_VERTEX:
616 limits_array = vs_limits;
617 break;
618 case WINED3D_SHADER_TYPE_HULL:
619 limits_array = hs_limits;
620 break;
621 case WINED3D_SHADER_TYPE_DOMAIN:
622 limits_array = ds_limits;
623 break;
624 case WINED3D_SHADER_TYPE_GEOMETRY:
625 limits_array = gs_limits;
626 break;
627 case WINED3D_SHADER_TYPE_PIXEL:
628 limits_array = ps_limits;
629 break;
630 case WINED3D_SHADER_TYPE_COMPUTE:
631 limits_array = cs_limits;
632 break;
635 while (limits_array[i].min_version && limits_array[i].min_version <= shader_version)
637 if (shader_version <= limits_array[i].max_version)
639 shader->limits = &limits_array[i].limits;
640 break;
642 ++i;
644 if (!shader->limits)
646 FIXME("Unexpected shader version \"%u.%u\".\n",
647 shader->reg_maps.shader_version.major,
648 shader->reg_maps.shader_version.minor);
649 shader->limits = &limits_array[max(0, i - 1)].limits;
653 static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
655 DWORD idx, shift;
656 idx = bit >> 5;
657 shift = bit & 0x1f;
658 bitmap[idx] |= (1u << shift);
661 static BOOL shader_record_register_usage(struct wined3d_shader *shader, struct wined3d_shader_reg_maps *reg_maps,
662 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type, unsigned int constf_size)
664 switch (reg->type)
666 case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
667 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
668 reg_maps->texcoord |= 1u << reg->idx[0].offset;
669 else
670 reg_maps->address |= 1u << reg->idx[0].offset;
671 break;
673 case WINED3DSPR_TEMP:
674 reg_maps->temporary |= 1u << reg->idx[0].offset;
675 break;
677 case WINED3DSPR_INPUT:
678 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
680 if (reg->idx[0].rel_addr)
682 /* If relative addressing is used, we must assume that all registers
683 * are used. Even if it is a construct like v3[aL], we can't assume
684 * that v0, v1 and v2 aren't read because aL can be negative */
685 unsigned int i;
686 for (i = 0; i < MAX_REG_INPUT; ++i)
688 shader->u.ps.input_reg_used[i] = TRUE;
691 else
693 shader->u.ps.input_reg_used[reg->idx[0].offset] = TRUE;
696 else
697 reg_maps->input_registers |= 1u << reg->idx[0].offset;
698 break;
700 case WINED3DSPR_RASTOUT:
701 if (reg->idx[0].offset == 1)
702 reg_maps->fog = 1;
703 if (reg->idx[0].offset == 2)
704 reg_maps->point_size = 1;
705 break;
707 case WINED3DSPR_MISCTYPE:
708 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
710 if (!reg->idx[0].offset)
711 reg_maps->vpos = 1;
712 else if (reg->idx[0].offset == 1)
713 reg_maps->usesfacing = 1;
715 break;
717 case WINED3DSPR_CONST:
718 if (reg->idx[0].rel_addr)
720 if (reg->idx[0].offset < reg_maps->min_rel_offset)
721 reg_maps->min_rel_offset = reg->idx[0].offset;
722 if (reg->idx[0].offset > reg_maps->max_rel_offset)
723 reg_maps->max_rel_offset = reg->idx[0].offset;
724 reg_maps->usesrelconstF = TRUE;
726 else
728 if (reg->idx[0].offset >= min(shader->limits->constant_float, constf_size))
730 WARN("Shader using float constant %u which is not supported.\n", reg->idx[0].offset);
731 return FALSE;
733 else
735 set_bitmap_bit(reg_maps->constf, reg->idx[0].offset);
738 break;
740 case WINED3DSPR_CONSTINT:
741 if (reg->idx[0].offset >= shader->limits->constant_int)
743 WARN("Shader using integer constant %u which is not supported.\n", reg->idx[0].offset);
744 return FALSE;
746 else
748 reg_maps->integer_constants |= (1u << reg->idx[0].offset);
750 break;
752 case WINED3DSPR_CONSTBOOL:
753 if (reg->idx[0].offset >= shader->limits->constant_bool)
755 WARN("Shader using bool constant %u which is not supported.\n", reg->idx[0].offset);
756 return FALSE;
758 else
760 reg_maps->boolean_constants |= (1u << reg->idx[0].offset);
762 break;
764 case WINED3DSPR_COLOROUT:
765 reg_maps->rt_mask |= (1u << reg->idx[0].offset);
766 break;
768 default:
769 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
770 reg->type, reg->idx[0].offset, reg->idx[1].offset);
771 break;
773 return TRUE;
776 static void shader_record_sample(struct wined3d_shader_reg_maps *reg_maps,
777 unsigned int resource_idx, unsigned int sampler_idx, unsigned int bind_idx)
779 struct wined3d_shader_sampler_map_entry *entries, *entry;
780 struct wined3d_shader_sampler_map *map;
781 unsigned int i;
783 map = &reg_maps->sampler_map;
784 entries = map->entries;
785 for (i = 0; i < map->count; ++i)
787 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
788 return;
791 if (!map->size)
793 if (!(entries = wined3d_calloc(4, sizeof(*entries))))
795 ERR("Failed to allocate sampler map entries.\n");
796 return;
798 map->size = 4;
799 map->entries = entries;
801 else if (map->count == map->size)
803 size_t new_size = map->size * 2;
805 if (sizeof(*entries) * new_size <= sizeof(*entries) * map->size
806 || !(entries = HeapReAlloc(GetProcessHeap(), 0, entries, sizeof(*entries) * new_size)))
808 ERR("Failed to resize sampler map entries.\n");
809 return;
811 map->size = new_size;
812 map->entries = entries;
815 entry = &entries[map->count++];
816 entry->resource_idx = resource_idx;
817 entry->sampler_idx = sampler_idx;
818 entry->bind_idx = bind_idx;
821 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
823 switch (instr)
825 case WINED3DSIH_M4x4:
826 case WINED3DSIH_M3x4:
827 return param == 1 ? 3 : 0;
829 case WINED3DSIH_M4x3:
830 case WINED3DSIH_M3x3:
831 return param == 1 ? 2 : 0;
833 case WINED3DSIH_M3x2:
834 return param == 1 ? 1 : 0;
836 default:
837 return 0;
841 /* Note that this does not count the loop register as an address register. */
842 static HRESULT shader_get_registers_used(struct wined3d_shader *shader, const struct wined3d_shader_frontend *fe,
843 struct wined3d_shader_reg_maps *reg_maps, struct wined3d_shader_signature *input_signature,
844 struct wined3d_shader_signature *output_signature, const DWORD *byte_code, DWORD constf_size)
846 struct wined3d_shader_signature_element input_signature_elements[max(MAX_ATTRIBS, MAX_REG_INPUT)];
847 struct wined3d_shader_signature_element output_signature_elements[MAX_REG_OUTPUT];
848 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
849 void *fe_data = shader->frontend_data;
850 struct wined3d_shader_version shader_version;
851 const DWORD *ptr = byte_code;
852 unsigned int i;
854 memset(reg_maps, 0, sizeof(*reg_maps));
855 memset(input_signature_elements, 0, sizeof(input_signature_elements));
856 memset(output_signature_elements, 0, sizeof(output_signature_elements));
857 reg_maps->min_rel_offset = ~0U;
858 list_init(&reg_maps->indexable_temps);
860 fe->shader_read_header(fe_data, &ptr, &shader_version);
861 reg_maps->shader_version = shader_version;
863 shader_set_limits(shader);
865 if (!(reg_maps->constf = wined3d_calloc(((min(shader->limits->constant_float, constf_size) + 31) / 32),
866 sizeof(*reg_maps->constf))))
868 ERR("Failed to allocate constant map memory.\n");
869 return E_OUTOFMEMORY;
872 while (!fe->shader_is_end(fe_data, &ptr))
874 struct wined3d_shader_instruction ins;
876 /* Fetch opcode. */
877 fe->shader_read_instruction(fe_data, &ptr, &ins);
879 /* Unhandled opcode, and its parameters. */
880 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
882 TRACE("Skipping unrecognized instruction.\n");
883 continue;
886 /* Handle declarations. */
887 if (ins.handler_idx == WINED3DSIH_DCL)
889 struct wined3d_shader_semantic *semantic = &ins.declaration.semantic;
890 unsigned int reg_idx = semantic->reg.reg.idx[0].offset;
892 switch (semantic->reg.reg.type)
894 /* Mark input registers used. */
895 case WINED3DSPR_INPUT:
896 if (reg_idx >= MAX_REG_INPUT)
898 ERR("Invalid input register index %u.\n", reg_idx);
899 break;
901 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL && shader_version.major == 3
902 && semantic->usage == WINED3D_DECL_USAGE_POSITION && !semantic->usage_idx)
903 return WINED3DERR_INVALIDCALL;
904 reg_maps->input_registers |= 1u << reg_idx;
905 shader_signature_from_semantic(&input_signature_elements[reg_idx], semantic);
906 break;
908 /* Vertex shader: mark 3.0 output registers used, save token. */
909 case WINED3DSPR_OUTPUT:
910 if (reg_idx >= MAX_REG_OUTPUT)
912 ERR("Invalid output register index %u.\n", reg_idx);
913 break;
915 reg_maps->output_registers |= 1u << reg_idx;
916 shader_signature_from_semantic(&output_signature_elements[reg_idx], semantic);
917 if (semantic->usage == WINED3D_DECL_USAGE_FOG)
918 reg_maps->fog = 1;
919 if (semantic->usage == WINED3D_DECL_USAGE_PSIZE)
920 reg_maps->point_size = 1;
921 break;
923 case WINED3DSPR_SAMPLER:
924 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
925 case WINED3DSPR_RESOURCE:
926 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
928 ERR("Invalid resource index %u.\n", reg_idx);
929 break;
931 reg_maps->resource_info[reg_idx].type = semantic->resource_type;
932 reg_maps->resource_info[reg_idx].data_type = semantic->resource_data_type;
933 break;
935 default:
936 TRACE("Not recording DCL register type %#x.\n", semantic->reg.reg.type);
937 break;
940 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
942 struct wined3d_shader_register *reg = &ins.declaration.src.reg;
943 if (reg->idx[0].offset >= WINED3D_MAX_CBS)
944 ERR("Invalid CB index %u.\n", reg->idx[0].offset);
945 else
946 reg_maps->cb_sizes[reg->idx[0].offset] = reg->idx[1].offset;
948 else if (ins.handler_idx == WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER)
950 if (reg_maps->icb)
951 FIXME("Multiple immediate constant buffers.\n");
952 reg_maps->icb = ins.declaration.icb;
954 else if (ins.handler_idx == WINED3DSIH_DCL_INDEXABLE_TEMP)
956 struct wined3d_shader_indexable_temp *reg;
958 if (!(reg = HeapAlloc(GetProcessHeap(), 0, sizeof(*reg))))
959 return E_OUTOFMEMORY;
961 *reg = ins.declaration.indexable_temp;
962 list_add_tail(&reg_maps->indexable_temps, &reg->entry);
964 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE)
966 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
967 shader->u.gs.input_type = ins.declaration.primitive_type;
968 else
969 FIXME("Invalid instruction %#x for shader type %#x.\n",
970 ins.handler_idx, shader_version.type);
972 else if (ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
974 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
975 shader->u.gs.output_type = ins.declaration.primitive_type;
976 else
977 FIXME("Invalid instruction %#x for shader type %#x.\n",
978 ins.handler_idx, shader_version.type);
980 else if (ins.handler_idx == WINED3DSIH_DCL_SAMPLER)
982 if (ins.flags & WINED3DSI_SAMPLER_COMPARISON_MODE)
983 reg_maps->sampler_comparison_mode |= (1u << ins.declaration.dst.reg.idx[0].offset);
985 else if (ins.handler_idx == WINED3DSIH_DCL_TEMPS)
987 reg_maps->temporary_count = ins.declaration.count;
989 else if (ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
991 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
992 shader->u.gs.vertices_out = ins.declaration.count;
993 else
994 FIXME("Invalid instruction %#x for shader type %#x.\n",
995 ins.handler_idx, shader_version.type);
997 else if (ins.handler_idx == WINED3DSIH_DEF)
999 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1000 float *value;
1001 if (!lconst) return E_OUTOFMEMORY;
1003 lconst->idx = ins.dst[0].reg.idx[0].offset;
1004 memcpy(lconst->value, ins.src[0].reg.immconst_data, 4 * sizeof(DWORD));
1005 value = (float *)lconst->value;
1007 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
1008 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1010 if (value[0] < -1.0f) value[0] = -1.0f;
1011 else if (value[0] > 1.0f) value[0] = 1.0f;
1012 if (value[1] < -1.0f) value[1] = -1.0f;
1013 else if (value[1] > 1.0f) value[1] = 1.0f;
1014 if (value[2] < -1.0f) value[2] = -1.0f;
1015 else if (value[2] > 1.0f) value[2] = 1.0f;
1016 if (value[3] < -1.0f) value[3] = -1.0f;
1017 else if (value[3] > 1.0f) value[3] = 1.0f;
1020 list_add_head(&shader->constantsF, &lconst->entry);
1022 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
1023 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
1025 shader->lconst_inf_or_nan = TRUE;
1028 else if (ins.handler_idx == WINED3DSIH_DEFI)
1030 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1031 if (!lconst) return E_OUTOFMEMORY;
1033 lconst->idx = ins.dst[0].reg.idx[0].offset;
1034 memcpy(lconst->value, ins.src[0].reg.immconst_data, 4 * sizeof(DWORD));
1036 list_add_head(&shader->constantsI, &lconst->entry);
1037 reg_maps->local_int_consts |= (1u << lconst->idx);
1039 else if (ins.handler_idx == WINED3DSIH_DEFB)
1041 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1042 if (!lconst) return E_OUTOFMEMORY;
1044 lconst->idx = ins.dst[0].reg.idx[0].offset;
1045 memcpy(lconst->value, ins.src[0].reg.immconst_data, sizeof(DWORD));
1047 list_add_head(&shader->constantsB, &lconst->entry);
1048 reg_maps->local_bool_consts |= (1u << lconst->idx);
1050 /* For subroutine prototypes. */
1051 else if (ins.handler_idx == WINED3DSIH_LABEL)
1053 reg_maps->labels |= 1u << ins.src[0].reg.idx[0].offset;
1055 /* Set texture, address, temporary registers. */
1056 else
1058 BOOL color0_mov = FALSE;
1059 unsigned int i;
1061 /* This will loop over all the registers and try to
1062 * make a bitmask of the ones we're interested in.
1064 * Relative addressing tokens are ignored, but that's
1065 * okay, since we'll catch any address registers when
1066 * they are initialized (required by spec). */
1067 for (i = 0; i < ins.dst_count; ++i)
1069 if (!shader_record_register_usage(shader, reg_maps, &ins.dst[i].reg,
1070 shader_version.type, constf_size))
1071 return WINED3DERR_INVALIDCALL;
1073 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
1075 UINT idx = ins.dst[i].reg.idx[0].offset;
1077 switch (ins.dst[i].reg.type)
1079 case WINED3DSPR_RASTOUT:
1080 if (shader_version.major >= 3)
1081 break;
1082 switch (idx)
1084 case 0: /* oPos */
1085 reg_maps->output_registers |= 1u << 10;
1086 shader_signature_from_usage(&output_signature_elements[10],
1087 WINED3D_DECL_USAGE_POSITION, 0, 10, WINED3DSP_WRITEMASK_ALL);
1088 break;
1090 case 1: /* oFog */
1091 reg_maps->output_registers |= 1u << 11;
1092 shader_signature_from_usage(&output_signature_elements[11],
1093 WINED3D_DECL_USAGE_FOG, 0, 11, WINED3DSP_WRITEMASK_0);
1094 break;
1096 case 2: /* oPts */
1097 reg_maps->output_registers |= 1u << 11;
1098 shader_signature_from_usage(&output_signature_elements[11],
1099 WINED3D_DECL_USAGE_PSIZE, 0, 11, WINED3DSP_WRITEMASK_1);
1100 break;
1102 break;
1104 case WINED3DSPR_ATTROUT:
1105 if (shader_version.major >= 3)
1106 break;
1107 if (idx < 2)
1109 idx += 8;
1110 if (reg_maps->output_registers & (1u << idx))
1112 output_signature_elements[idx].mask |= ins.dst[i].write_mask;
1114 else
1116 reg_maps->output_registers |= 1u << idx;
1117 shader_signature_from_usage(&output_signature_elements[idx],
1118 WINED3D_DECL_USAGE_COLOR, idx - 8, idx, ins.dst[i].write_mask);
1121 break;
1123 case WINED3DSPR_TEXCRDOUT:
1124 if (shader_version.major >= 3)
1126 reg_maps->u.output_registers_mask[idx] |= ins.dst[i].write_mask;
1127 break;
1129 reg_maps->u.texcoord_mask[idx] |= ins.dst[i].write_mask;
1130 if (reg_maps->output_registers & (1u << idx))
1132 output_signature_elements[idx].mask |= ins.dst[i].write_mask;
1134 else
1136 reg_maps->output_registers |= 1u << idx;
1137 shader_signature_from_usage(&output_signature_elements[idx],
1138 WINED3D_DECL_USAGE_TEXCOORD, idx, idx, ins.dst[i].write_mask);
1140 break;
1142 default:
1143 break;
1147 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1149 if (ins.dst[i].reg.type == WINED3DSPR_COLOROUT && !ins.dst[i].reg.idx[0].offset)
1151 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
1152 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
1153 * the mov and perform the sRGB write correction from the source register.
1155 * However, if the mov is only partial, we can't do this, and if the write
1156 * comes from an instruction other than MOV it is hard to do as well. If
1157 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
1158 shader->u.ps.color0_mov = FALSE;
1159 if (ins.handler_idx == WINED3DSIH_MOV
1160 && ins.dst[i].write_mask == WINED3DSP_WRITEMASK_ALL)
1162 /* Used later when the source register is read. */
1163 color0_mov = TRUE;
1166 /* Also drop the MOV marker if the source register is overwritten prior to the shader
1167 * end
1169 else if (ins.dst[i].reg.type == WINED3DSPR_TEMP
1170 && ins.dst[i].reg.idx[0].offset == shader->u.ps.color0_reg)
1172 shader->u.ps.color0_mov = FALSE;
1176 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
1177 if (shader_version.major == 1
1178 && (ins.handler_idx == WINED3DSIH_TEX
1179 || ins.handler_idx == WINED3DSIH_TEXBEM
1180 || ins.handler_idx == WINED3DSIH_TEXBEML
1181 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
1182 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
1183 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
1184 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
1185 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
1186 || ins.handler_idx == WINED3DSIH_TEXREG2AR
1187 || ins.handler_idx == WINED3DSIH_TEXREG2GB
1188 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
1190 unsigned int reg_idx = ins.dst[i].reg.idx[0].offset;
1192 TRACE("Setting fake 2D resource for 1.x pixelshader.\n");
1193 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
1194 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_FLOAT;
1195 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
1197 /* texbem is only valid with < 1.4 pixel shaders */
1198 if (ins.handler_idx == WINED3DSIH_TEXBEM
1199 || ins.handler_idx == WINED3DSIH_TEXBEML)
1201 reg_maps->bumpmat |= 1u << reg_idx;
1202 if (ins.handler_idx == WINED3DSIH_TEXBEML)
1204 reg_maps->luminanceparams |= 1u << reg_idx;
1208 else if (ins.handler_idx == WINED3DSIH_BEM)
1210 reg_maps->bumpmat |= 1u << ins.dst[i].reg.idx[0].offset;
1214 if (ins.handler_idx == WINED3DSIH_NRM) reg_maps->usesnrm = 1;
1215 else if (ins.handler_idx == WINED3DSIH_DSY) reg_maps->usesdsy = 1;
1216 else if (ins.handler_idx == WINED3DSIH_DSX) reg_maps->usesdsx = 1;
1217 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
1218 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
1219 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
1220 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
1221 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
1222 else if (ins.handler_idx == WINED3DSIH_POW) reg_maps->usespow = 1;
1223 else if (ins.handler_idx == WINED3DSIH_LOOP
1224 || ins.handler_idx == WINED3DSIH_REP)
1226 ++cur_loop_depth;
1227 if (cur_loop_depth > max_loop_depth)
1228 max_loop_depth = cur_loop_depth;
1230 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
1231 || ins.handler_idx == WINED3DSIH_ENDREP)
1233 --cur_loop_depth;
1235 else if (ins.handler_idx == WINED3DSIH_SAMPLE
1236 || ins.handler_idx == WINED3DSIH_SAMPLE_B
1237 || ins.handler_idx == WINED3DSIH_SAMPLE_C_LZ
1238 || ins.handler_idx == WINED3DSIH_SAMPLE_GRAD
1239 || ins.handler_idx == WINED3DSIH_SAMPLE_LOD)
1241 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1242 ins.src[2].reg.idx[0].offset, reg_maps->sampler_map.count);
1244 else if (ins.handler_idx == WINED3DSIH_LD
1245 || ins.handler_idx == WINED3DSIH_RESINFO)
1247 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1248 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1251 if (ins.predicate)
1252 if (!shader_record_register_usage(shader, reg_maps, &ins.predicate->reg,
1253 shader_version.type, constf_size))
1254 return WINED3DERR_INVALIDCALL;
1256 for (i = 0; i < ins.src_count; ++i)
1258 unsigned int count = get_instr_extra_regcount(ins.handler_idx, i);
1259 struct wined3d_shader_register reg = ins.src[i].reg;
1261 if (!shader_record_register_usage(shader, reg_maps, &ins.src[i].reg,
1262 shader_version.type, constf_size))
1263 return WINED3DERR_INVALIDCALL;
1264 while (count)
1266 ++reg.idx[0].offset;
1267 if (!shader_record_register_usage(shader, reg_maps, &reg,
1268 shader_version.type, constf_size))
1269 return WINED3DERR_INVALIDCALL;
1270 --count;
1273 if (color0_mov)
1275 if (ins.src[i].reg.type == WINED3DSPR_TEMP
1276 && ins.src[i].swizzle == WINED3DSP_NOSWIZZLE)
1278 shader->u.ps.color0_mov = TRUE;
1279 shader->u.ps.color0_reg = ins.src[i].reg.idx[0].offset;
1285 reg_maps->loop_depth = max_loop_depth;
1287 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1288 * R0 is written to the render target. */
1289 if (shader_version.major < 2 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1290 reg_maps->rt_mask |= (1u << 0);
1292 shader->functionLength = ((const char *)ptr - (const char *)byte_code);
1294 if (input_signature->elements)
1296 for (i = 0; i < input_signature->element_count; ++i)
1298 reg_maps->input_registers |= 1u << input_signature->elements[i].register_idx;
1299 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1300 && input_signature->elements[i].sysval_semantic == WINED3D_SV_POSITION)
1301 reg_maps->vpos = 1;
1304 else if (!input_signature->elements && reg_maps->input_registers)
1306 unsigned int count = wined3d_popcount(reg_maps->input_registers);
1307 struct wined3d_shader_signature_element *e;
1308 unsigned int i;
1310 if (!(input_signature->elements = wined3d_calloc(count, sizeof(*input_signature->elements))))
1311 return E_OUTOFMEMORY;
1312 input_signature->element_count = count;
1314 e = input_signature->elements;
1315 for (i = 0; i < ARRAY_SIZE(input_signature_elements); ++i)
1317 if (!(reg_maps->input_registers & (1u << i)))
1318 continue;
1319 input_signature_elements[i].register_idx = i;
1320 *e++ = input_signature_elements[i];
1324 if (output_signature->elements)
1326 for (i = 0; i < output_signature->element_count; ++i)
1328 reg_maps->output_registers |= 1u << output_signature->elements[i].register_idx;
1331 else if (reg_maps->output_registers)
1333 unsigned int count = wined3d_popcount(reg_maps->output_registers);
1334 struct wined3d_shader_signature_element *e;
1336 if (!(output_signature->elements = wined3d_calloc(count, sizeof(*output_signature->elements))))
1337 return E_OUTOFMEMORY;
1338 output_signature->element_count = count;
1340 e = output_signature->elements;
1341 for (i = 0; i < ARRAY_SIZE(output_signature_elements); ++i)
1343 if (!(reg_maps->output_registers & (1u << i)))
1344 continue;
1345 *e++ = output_signature_elements[i];
1349 return WINED3D_OK;
1352 static void shader_cleanup_reg_maps(struct wined3d_shader_reg_maps *reg_maps)
1354 struct wined3d_shader_indexable_temp *reg, *reg_next;
1356 HeapFree(GetProcessHeap(), 0, reg_maps->constf);
1357 HeapFree(GetProcessHeap(), 0, reg_maps->sampler_map.entries);
1359 LIST_FOR_EACH_ENTRY_SAFE(reg, reg_next, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
1360 HeapFree(GetProcessHeap(), 0, reg);
1361 list_init(&reg_maps->indexable_temps);
1364 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
1366 DWORD map = 1u << max;
1367 map |= map - 1;
1368 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
1370 return wined3d_log2i(map);
1373 static void shader_dump_global_flags(struct wined3d_string_buffer *buffer, DWORD global_flags)
1375 if (global_flags & WINED3DSGF_REFACTORING_ALLOWED)
1377 shader_addline(buffer, "refactoringAllowed");
1378 global_flags &= ~WINED3DSGF_REFACTORING_ALLOWED;
1379 if (global_flags)
1380 shader_addline(buffer, " | ");
1383 if (global_flags & WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS)
1385 shader_addline(buffer, "enableRawAndStructuredBuffers");
1386 global_flags &= ~WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS;
1389 if (global_flags)
1390 shader_addline(buffer, "unknown_flags(%#x)", global_flags);
1393 static void shader_dump_tessellator_domain(struct wined3d_string_buffer *buffer,
1394 enum wined3d_tessellator_domain domain)
1396 switch (domain)
1398 case WINED3D_TESSELLATOR_DOMAIN_LINE:
1399 shader_addline(buffer, "line");
1400 break;
1401 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
1402 shader_addline(buffer, "triangle");
1403 break;
1404 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
1405 shader_addline(buffer, "quad");
1406 break;
1407 default:
1408 shader_addline(buffer, "unknown_tessellator_domain(%#x)", domain);
1409 break;
1413 static void shader_dump_tessellator_output_primitive(struct wined3d_string_buffer *buffer,
1414 enum wined3d_tessellator_output_primitive output_primitive)
1416 switch (output_primitive)
1418 case WINED3D_TESSELLATOR_OUTPUT_POINT:
1419 shader_addline(buffer, "point");
1420 break;
1421 case WINED3D_TESSELLATOR_OUTPUT_LINE:
1422 shader_addline(buffer, "line");
1423 break;
1424 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
1425 shader_addline(buffer, "triangle_cw");
1426 break;
1427 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
1428 shader_addline(buffer, "triangle_ccw");
1429 break;
1430 default:
1431 shader_addline(buffer, "unknown_tessellator_output_primitive(%#x)", output_primitive);
1432 break;
1436 static void shader_dump_tessellator_partitioning(struct wined3d_string_buffer *buffer,
1437 enum wined3d_tessellator_partitioning partitioning)
1439 switch (partitioning)
1441 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
1442 shader_addline(buffer, "integer");
1443 break;
1444 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
1445 shader_addline(buffer, "pow2");
1446 break;
1447 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
1448 shader_addline(buffer, "fractional_odd");
1449 break;
1450 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
1451 shader_addline(buffer, "fractional_even");
1452 break;
1453 default:
1454 shader_addline(buffer, "unknown_tessellator_partitioning(%#x)", partitioning);
1455 break;
1459 static void shader_dump_sysval_semantic(struct wined3d_string_buffer *buffer, enum wined3d_sysval_semantic semantic)
1461 unsigned int i;
1463 for (i = 0; i < ARRAY_SIZE(sysval_semantic_names); ++i)
1465 if (sysval_semantic_names[i].sysval_semantic == semantic)
1467 shader_addline(buffer, "%s", sysval_semantic_names[i].sysval_name);
1468 return;
1472 shader_addline(buffer, "unknown_sysval_semantic(%#x)", semantic);
1475 static void shader_dump_decl_usage(struct wined3d_string_buffer *buffer,
1476 const struct wined3d_shader_semantic *semantic, const struct wined3d_shader_version *shader_version)
1478 shader_addline(buffer, "dcl");
1480 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
1482 switch (semantic->resource_type)
1484 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1485 shader_addline(buffer, "_2d");
1486 break;
1488 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1489 shader_addline(buffer, "_3d");
1490 break;
1492 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1493 shader_addline(buffer, "_cube");
1494 break;
1496 default:
1497 shader_addline(buffer, "_unknown_resource_type(%#x)", semantic->resource_type);
1498 break;
1501 else if (semantic->reg.reg.type == WINED3DSPR_RESOURCE || semantic->reg.reg.type == WINED3DSPR_UAV)
1503 if (semantic->reg.reg.type == WINED3DSPR_RESOURCE)
1504 shader_addline(buffer, "_resource_");
1505 else
1506 shader_addline(buffer, "_uav_");
1507 switch (semantic->resource_type)
1509 case WINED3D_SHADER_RESOURCE_BUFFER:
1510 shader_addline(buffer, "buffer");
1511 break;
1513 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1514 shader_addline(buffer, "texture1d");
1515 break;
1517 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1518 shader_addline(buffer, "texture2d");
1519 break;
1521 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
1522 shader_addline(buffer, "texture2dms");
1523 break;
1525 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1526 shader_addline(buffer, "texture3d");
1527 break;
1529 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1530 shader_addline(buffer, "texturecube");
1531 break;
1533 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
1534 shader_addline(buffer, "texture1darray");
1535 break;
1537 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
1538 shader_addline(buffer, "texture2darray");
1539 break;
1541 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
1542 shader_addline(buffer, "texture2dmsarray");
1543 break;
1545 default:
1546 shader_addline(buffer, "unknown");
1547 break;
1549 switch (semantic->resource_data_type)
1551 case WINED3D_DATA_FLOAT:
1552 shader_addline(buffer, " (float)");
1553 break;
1555 case WINED3D_DATA_INT:
1556 shader_addline(buffer, " (int)");
1557 break;
1559 case WINED3D_DATA_UINT:
1560 shader_addline(buffer, " (uint)");
1561 break;
1563 case WINED3D_DATA_UNORM:
1564 shader_addline(buffer, " (unorm)");
1565 break;
1567 case WINED3D_DATA_SNORM:
1568 shader_addline(buffer, " (snorm)");
1569 break;
1571 default:
1572 shader_addline(buffer, " (unknown)");
1573 break;
1576 else
1578 /* Pixel shaders 3.0 don't have usage semantics. */
1579 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL)
1580 return;
1581 else
1582 shader_addline(buffer, "_");
1584 switch (semantic->usage)
1586 case WINED3D_DECL_USAGE_POSITION:
1587 shader_addline(buffer, "position%u", semantic->usage_idx);
1588 break;
1590 case WINED3D_DECL_USAGE_BLEND_INDICES:
1591 shader_addline(buffer, "blend");
1592 break;
1594 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
1595 shader_addline(buffer, "weight");
1596 break;
1598 case WINED3D_DECL_USAGE_NORMAL:
1599 shader_addline(buffer, "normal%u", semantic->usage_idx);
1600 break;
1602 case WINED3D_DECL_USAGE_PSIZE:
1603 shader_addline(buffer, "psize");
1604 break;
1606 case WINED3D_DECL_USAGE_COLOR:
1607 if (!semantic->usage_idx)
1608 shader_addline(buffer, "color");
1609 else
1610 shader_addline(buffer, "specular%u", (semantic->usage_idx - 1));
1611 break;
1613 case WINED3D_DECL_USAGE_TEXCOORD:
1614 shader_addline(buffer, "texture%u", semantic->usage_idx);
1615 break;
1617 case WINED3D_DECL_USAGE_TANGENT:
1618 shader_addline(buffer, "tangent");
1619 break;
1621 case WINED3D_DECL_USAGE_BINORMAL:
1622 shader_addline(buffer, "binormal");
1623 break;
1625 case WINED3D_DECL_USAGE_TESS_FACTOR:
1626 shader_addline(buffer, "tessfactor");
1627 break;
1629 case WINED3D_DECL_USAGE_POSITIONT:
1630 shader_addline(buffer, "positionT%u", semantic->usage_idx);
1631 break;
1633 case WINED3D_DECL_USAGE_FOG:
1634 shader_addline(buffer, "fog");
1635 break;
1637 case WINED3D_DECL_USAGE_DEPTH:
1638 shader_addline(buffer, "depth");
1639 break;
1641 case WINED3D_DECL_USAGE_SAMPLE:
1642 shader_addline(buffer, "sample");
1643 break;
1645 default:
1646 shader_addline(buffer, "<unknown_semantic(%#x)>", semantic->usage);
1647 FIXME("unknown_semantics(0x%08x)", semantic->usage);
1652 static void shader_dump_register(struct wined3d_string_buffer *buffer,
1653 const struct wined3d_shader_register *reg, const struct wined3d_shader_version *shader_version)
1655 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
1656 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
1657 UINT offset = reg->idx[0].offset;
1659 switch (reg->type)
1661 case WINED3DSPR_TEMP:
1662 shader_addline(buffer, "r");
1663 break;
1665 case WINED3DSPR_INPUT:
1666 shader_addline(buffer, "v");
1667 break;
1669 case WINED3DSPR_CONST:
1670 case WINED3DSPR_CONST2:
1671 case WINED3DSPR_CONST3:
1672 case WINED3DSPR_CONST4:
1673 shader_addline(buffer, "c");
1674 offset = shader_get_float_offset(reg->type, offset);
1675 break;
1677 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
1678 shader_addline(buffer, "%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
1679 break;
1681 case WINED3DSPR_RASTOUT:
1682 shader_addline(buffer, "%s", rastout_reg_names[offset]);
1683 break;
1685 case WINED3DSPR_COLOROUT:
1686 shader_addline(buffer, "oC");
1687 break;
1689 case WINED3DSPR_DEPTHOUT:
1690 shader_addline(buffer, "oDepth");
1691 break;
1693 case WINED3DSPR_ATTROUT:
1694 shader_addline(buffer, "oD");
1695 break;
1697 case WINED3DSPR_TEXCRDOUT:
1698 /* Vertex shaders >= 3.0 use general purpose output registers
1699 * (WINED3DSPR_OUTPUT), which can include an address token. */
1700 if (shader_version->major >= 3)
1701 shader_addline(buffer, "o");
1702 else
1703 shader_addline(buffer, "oT");
1704 break;
1706 case WINED3DSPR_CONSTINT:
1707 shader_addline(buffer, "i");
1708 break;
1710 case WINED3DSPR_CONSTBOOL:
1711 shader_addline(buffer, "b");
1712 break;
1714 case WINED3DSPR_LABEL:
1715 shader_addline(buffer, "l");
1716 break;
1718 case WINED3DSPR_LOOP:
1719 shader_addline(buffer, "aL");
1720 break;
1722 case WINED3DSPR_SAMPLER:
1723 shader_addline(buffer, "s");
1724 break;
1726 case WINED3DSPR_MISCTYPE:
1727 if (offset > 1)
1729 FIXME("Unhandled misctype register %u.\n", offset);
1730 shader_addline(buffer, "<unhandled misctype %#x>", offset);
1732 else
1734 shader_addline(buffer, "%s", misctype_reg_names[offset]);
1736 break;
1738 case WINED3DSPR_PREDICATE:
1739 shader_addline(buffer, "p");
1740 break;
1742 case WINED3DSPR_IMMCONST:
1743 shader_addline(buffer, "l");
1744 break;
1746 case WINED3DSPR_CONSTBUFFER:
1747 shader_addline(buffer, "cb");
1748 break;
1750 case WINED3DSPR_IMMCONSTBUFFER:
1751 shader_addline(buffer, "icb");
1752 break;
1754 case WINED3DSPR_PRIMID:
1755 shader_addline(buffer, "primID");
1756 break;
1758 case WINED3DSPR_NULL:
1759 shader_addline(buffer, "null");
1760 break;
1762 case WINED3DSPR_RESOURCE:
1763 shader_addline(buffer, "t");
1764 break;
1766 case WINED3DSPR_UAV:
1767 shader_addline(buffer, "u");
1768 break;
1770 case WINED3DSPR_OUTPOINTID:
1771 shader_addline(buffer, "vOutputControlPointID");
1772 break;
1774 case WINED3DSPR_FORKINSTID:
1775 shader_addline(buffer, "vForkInstanceId");
1776 break;
1778 case WINED3DSPR_INCONTROLPOINT:
1779 shader_addline(buffer, "vicp");
1780 break;
1782 case WINED3DSPR_PATCHCONST:
1783 shader_addline(buffer, "vpc");
1784 break;
1786 case WINED3DSPR_TESSCOORD:
1787 shader_addline(buffer, "vDomainLocation");
1788 break;
1790 case WINED3DSPR_GROUPSHAREDMEM:
1791 shader_addline(buffer, "g");
1792 break;
1794 case WINED3DSPR_THREADID:
1795 shader_addline(buffer, "vThreadID");
1796 break;
1798 case WINED3DSPR_THREADGROUPID:
1799 shader_addline(buffer, "vThreadGroupID");
1800 break;
1802 case WINED3DSPR_LOCALTHREADID:
1803 shader_addline(buffer, "vThreadIDInGroup");
1804 break;
1806 case WINED3DSPR_LOCALTHREADINDEX:
1807 shader_addline(buffer, "vThreadIDInGroupFlattened");
1808 break;
1810 case WINED3DSPR_IDXTEMP:
1811 shader_addline(buffer, "x");
1812 break;
1814 case WINED3DSPR_STREAM:
1815 shader_addline(buffer, "m");
1816 break;
1818 default:
1819 shader_addline(buffer, "<unhandled_rtype(%#x)>", reg->type);
1820 break;
1823 if (reg->type == WINED3DSPR_IMMCONST)
1825 shader_addline(buffer, "(");
1826 switch (reg->immconst_type)
1828 case WINED3D_IMMCONST_SCALAR:
1829 switch (reg->data_type)
1831 case WINED3D_DATA_FLOAT:
1832 shader_addline(buffer, "%.8e", *(const float *)reg->immconst_data);
1833 break;
1834 case WINED3D_DATA_INT:
1835 shader_addline(buffer, "%d", reg->immconst_data[0]);
1836 break;
1837 case WINED3D_DATA_RESOURCE:
1838 case WINED3D_DATA_SAMPLER:
1839 case WINED3D_DATA_UINT:
1840 shader_addline(buffer, "%u", reg->immconst_data[0]);
1841 break;
1842 default:
1843 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
1844 break;
1846 break;
1848 case WINED3D_IMMCONST_VEC4:
1849 switch (reg->data_type)
1851 case WINED3D_DATA_FLOAT:
1852 shader_addline(buffer, "%.8e, %.8e, %.8e, %.8e",
1853 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
1854 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
1855 break;
1856 case WINED3D_DATA_INT:
1857 shader_addline(buffer, "%d, %d, %d, %d",
1858 reg->immconst_data[0], reg->immconst_data[1],
1859 reg->immconst_data[2], reg->immconst_data[3]);
1860 break;
1861 case WINED3D_DATA_RESOURCE:
1862 case WINED3D_DATA_SAMPLER:
1863 case WINED3D_DATA_UINT:
1864 shader_addline(buffer, "%u, %u, %u, %u",
1865 reg->immconst_data[0], reg->immconst_data[1],
1866 reg->immconst_data[2], reg->immconst_data[3]);
1867 break;
1868 default:
1869 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
1870 break;
1872 break;
1874 default:
1875 shader_addline(buffer, "<unhandled immconst_type %#x>", reg->immconst_type);
1876 break;
1878 shader_addline(buffer, ")");
1880 else if (reg->type != WINED3DSPR_RASTOUT
1881 && reg->type != WINED3DSPR_MISCTYPE
1882 && reg->type != WINED3DSPR_NULL)
1884 if (offset != ~0u)
1886 shader_addline(buffer, "[");
1887 if (reg->idx[0].rel_addr)
1889 shader_dump_src_param(buffer, reg->idx[0].rel_addr, shader_version);
1890 shader_addline(buffer, " + ");
1892 shader_addline(buffer, "%u]", offset);
1894 if (reg->idx[1].offset != ~0u)
1896 shader_addline(buffer, "[");
1897 if (reg->idx[1].rel_addr)
1899 shader_dump_src_param(buffer, reg->idx[1].rel_addr, shader_version);
1900 shader_addline(buffer, " + ");
1902 shader_addline(buffer, "%u]", reg->idx[1].offset);
1908 static void shader_dump_dst_param(struct wined3d_string_buffer *buffer,
1909 const struct wined3d_shader_dst_param *param, const struct wined3d_shader_version *shader_version)
1911 DWORD write_mask = param->write_mask;
1913 shader_dump_register(buffer, &param->reg, shader_version);
1915 if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
1917 static const char write_mask_chars[] = "xyzw";
1919 shader_addline(buffer, ".");
1920 if (write_mask & WINED3DSP_WRITEMASK_0)
1921 shader_addline(buffer, "%c", write_mask_chars[0]);
1922 if (write_mask & WINED3DSP_WRITEMASK_1)
1923 shader_addline(buffer, "%c", write_mask_chars[1]);
1924 if (write_mask & WINED3DSP_WRITEMASK_2)
1925 shader_addline(buffer, "%c", write_mask_chars[2]);
1926 if (write_mask & WINED3DSP_WRITEMASK_3)
1927 shader_addline(buffer, "%c", write_mask_chars[3]);
1931 static void shader_dump_src_param(struct wined3d_string_buffer *buffer,
1932 const struct wined3d_shader_src_param *param, const struct wined3d_shader_version *shader_version)
1934 enum wined3d_shader_src_modifier src_modifier = param->modifiers;
1935 DWORD swizzle = param->swizzle;
1937 if (src_modifier == WINED3DSPSM_NEG
1938 || src_modifier == WINED3DSPSM_BIASNEG
1939 || src_modifier == WINED3DSPSM_SIGNNEG
1940 || src_modifier == WINED3DSPSM_X2NEG
1941 || src_modifier == WINED3DSPSM_ABSNEG)
1942 shader_addline(buffer, "-");
1943 else if (src_modifier == WINED3DSPSM_COMP)
1944 shader_addline(buffer, "1-");
1945 else if (src_modifier == WINED3DSPSM_NOT)
1946 shader_addline(buffer, "!");
1948 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
1949 shader_addline(buffer, "abs(");
1951 shader_dump_register(buffer, &param->reg, shader_version);
1953 switch (src_modifier)
1955 case WINED3DSPSM_NONE: break;
1956 case WINED3DSPSM_NEG: break;
1957 case WINED3DSPSM_NOT: break;
1958 case WINED3DSPSM_BIAS: shader_addline(buffer, "_bias"); break;
1959 case WINED3DSPSM_BIASNEG: shader_addline(buffer, "_bias"); break;
1960 case WINED3DSPSM_SIGN: shader_addline(buffer, "_bx2"); break;
1961 case WINED3DSPSM_SIGNNEG: shader_addline(buffer, "_bx2"); break;
1962 case WINED3DSPSM_COMP: break;
1963 case WINED3DSPSM_X2: shader_addline(buffer, "_x2"); break;
1964 case WINED3DSPSM_X2NEG: shader_addline(buffer, "_x2"); break;
1965 case WINED3DSPSM_DZ: shader_addline(buffer, "_dz"); break;
1966 case WINED3DSPSM_DW: shader_addline(buffer, "_dw"); break;
1967 case WINED3DSPSM_ABSNEG: shader_addline(buffer, ")"); break;
1968 case WINED3DSPSM_ABS: shader_addline(buffer, ")"); break;
1969 default: shader_addline(buffer, "_unknown_modifier(%#x)", src_modifier);
1972 if (swizzle != WINED3DSP_NOSWIZZLE)
1974 static const char swizzle_chars[] = "xyzw";
1975 DWORD swizzle_x = swizzle & 0x03;
1976 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1977 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1978 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1980 if (swizzle_x == swizzle_y
1981 && swizzle_x == swizzle_z
1982 && swizzle_x == swizzle_w)
1984 shader_addline(buffer, ".%c", swizzle_chars[swizzle_x]);
1986 else
1988 shader_addline(buffer, ".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
1989 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
1994 /* Shared code in order to generate the bulk of the shader string.
1995 * NOTE: A description of how to parse tokens can be found on MSDN. */
1996 void shader_generate_main(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
1997 const struct wined3d_shader_reg_maps *reg_maps, const DWORD *byte_code, void *backend_ctx)
1999 struct wined3d_device *device = shader->device;
2000 const struct wined3d_shader_frontend *fe = shader->frontend;
2001 void *fe_data = shader->frontend_data;
2002 struct wined3d_shader_version shader_version;
2003 struct wined3d_shader_loop_state loop_state;
2004 struct wined3d_shader_instruction ins;
2005 struct wined3d_shader_tex_mx tex_mx;
2006 struct wined3d_shader_context ctx;
2007 const DWORD *ptr = byte_code;
2009 /* Initialize current parsing state. */
2010 tex_mx.current_row = 0;
2011 loop_state.current_depth = 0;
2012 loop_state.current_reg = 0;
2014 ctx.shader = shader;
2015 ctx.gl_info = &device->adapter->gl_info;
2016 ctx.reg_maps = reg_maps;
2017 ctx.buffer = buffer;
2018 ctx.tex_mx = &tex_mx;
2019 ctx.loop_state = &loop_state;
2020 ctx.backend_data = backend_ctx;
2021 ins.ctx = &ctx;
2023 fe->shader_read_header(fe_data, &ptr, &shader_version);
2025 while (!fe->shader_is_end(fe_data, &ptr))
2027 /* Read opcode. */
2028 fe->shader_read_instruction(fe_data, &ptr, &ins);
2030 /* Unknown opcode and its parameters. */
2031 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
2033 TRACE("Skipping unrecognized instruction.\n");
2034 continue;
2037 if (ins.predicate)
2038 FIXME("Predicates not implemented.\n");
2040 /* Call appropriate function for output target */
2041 device->shader_backend->shader_handle_instruction(&ins);
2045 static void shader_dump_ins_modifiers(struct wined3d_string_buffer *buffer,
2046 const struct wined3d_shader_dst_param *dst)
2048 DWORD mmask = dst->modifiers;
2050 switch (dst->shift)
2052 case 0: break;
2053 case 13: shader_addline(buffer, "_d8"); break;
2054 case 14: shader_addline(buffer, "_d4"); break;
2055 case 15: shader_addline(buffer, "_d2"); break;
2056 case 1: shader_addline(buffer, "_x2"); break;
2057 case 2: shader_addline(buffer, "_x4"); break;
2058 case 3: shader_addline(buffer, "_x8"); break;
2059 default: shader_addline(buffer, "_unhandled_shift(%d)", dst->shift); break;
2062 if (mmask & WINED3DSPDM_SATURATE) shader_addline(buffer, "_sat");
2063 if (mmask & WINED3DSPDM_PARTIALPRECISION) shader_addline(buffer, "_pp");
2064 if (mmask & WINED3DSPDM_MSAMPCENTROID) shader_addline(buffer, "_centroid");
2066 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
2067 if (mmask) FIXME("_unrecognized_modifier(%#x)", mmask);
2070 static void shader_dump_primitive_type(struct wined3d_string_buffer *buffer,
2071 enum wined3d_primitive_type primitive_type)
2073 switch (primitive_type)
2075 case WINED3D_PT_UNDEFINED:
2076 shader_addline(buffer, "undefined");
2077 break;
2078 case WINED3D_PT_POINTLIST:
2079 shader_addline(buffer, "pointlist");
2080 break;
2081 case WINED3D_PT_LINELIST:
2082 shader_addline(buffer, "linelist");
2083 break;
2084 case WINED3D_PT_LINESTRIP:
2085 shader_addline(buffer, "linestrip");
2086 break;
2087 case WINED3D_PT_TRIANGLELIST:
2088 shader_addline(buffer, "trianglelist");
2089 break;
2090 case WINED3D_PT_TRIANGLESTRIP:
2091 shader_addline(buffer, "trianglestrip");
2092 break;
2093 case WINED3D_PT_TRIANGLEFAN:
2094 shader_addline(buffer, "trianglefan");
2095 break;
2096 case WINED3D_PT_LINELIST_ADJ:
2097 shader_addline(buffer, "linelist_adj");
2098 break;
2099 case WINED3D_PT_LINESTRIP_ADJ:
2100 shader_addline(buffer, "linestrip_adj");
2101 break;
2102 case WINED3D_PT_TRIANGLELIST_ADJ:
2103 shader_addline(buffer, "trianglelist_adj");
2104 break;
2105 case WINED3D_PT_TRIANGLESTRIP_ADJ:
2106 shader_addline(buffer, "trianglestrip_adj");
2107 break;
2108 default:
2109 shader_addline(buffer, "<unrecognized_primitive_type %#x>", primitive_type);
2110 break;
2114 static void shader_dump_interpolation_mode(struct wined3d_string_buffer *buffer,
2115 enum wined3d_shader_interpolation_mode interpolation_mode)
2117 switch (interpolation_mode)
2119 case WINED3DSIM_CONSTANT:
2120 shader_addline(buffer, "constant");
2121 break;
2122 case WINED3DSIM_LINEAR:
2123 shader_addline(buffer, "linear");
2124 break;
2125 case WINED3DSIM_LINEAR_CENTROID:
2126 shader_addline(buffer, "linear centroid");
2127 break;
2128 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2129 shader_addline(buffer, "linear noperspective");
2130 break;
2131 case WINED3DSIM_LINEAR_SAMPLE:
2132 shader_addline(buffer, "linear sample");
2133 break;
2134 case WINED3DSIM_LINEAR_NOPERSPECTIVE_CENTROID:
2135 shader_addline(buffer, "linear noperspective centroid");
2136 break;
2137 case WINED3DSIM_LINEAR_NOPERSPECTIVE_SAMPLE:
2138 shader_addline(buffer, "linear noperspective sample");
2139 break;
2140 default:
2141 shader_addline(buffer, "<unrecognized_interpolation_mode %#x>", interpolation_mode);
2142 break;
2146 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *byte_code)
2148 struct wined3d_shader_version shader_version;
2149 struct wined3d_string_buffer buffer;
2150 const DWORD *ptr = byte_code;
2151 const char *type_prefix;
2152 const char *p, *q;
2153 DWORD i;
2155 if (!string_buffer_init(&buffer))
2157 ERR("Failed to initialize string buffer.\n");
2158 return;
2161 TRACE("Parsing %p.\n", byte_code);
2163 fe->shader_read_header(fe_data, &ptr, &shader_version);
2165 switch (shader_version.type)
2167 case WINED3D_SHADER_TYPE_VERTEX:
2168 type_prefix = "vs";
2169 break;
2171 case WINED3D_SHADER_TYPE_HULL:
2172 type_prefix = "hs";
2173 break;
2175 case WINED3D_SHADER_TYPE_DOMAIN:
2176 type_prefix = "ds";
2177 break;
2179 case WINED3D_SHADER_TYPE_GEOMETRY:
2180 type_prefix = "gs";
2181 break;
2183 case WINED3D_SHADER_TYPE_PIXEL:
2184 type_prefix = "ps";
2185 break;
2187 case WINED3D_SHADER_TYPE_COMPUTE:
2188 type_prefix = "cs";
2189 break;
2191 default:
2192 FIXME("Unhandled shader type %#x.\n", shader_version.type);
2193 type_prefix = "unknown";
2194 break;
2197 shader_addline(&buffer, "%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
2199 while (!fe->shader_is_end(fe_data, &ptr))
2201 struct wined3d_shader_instruction ins;
2203 fe->shader_read_instruction(fe_data, &ptr, &ins);
2204 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
2206 WARN("Skipping unrecognized instruction.\n");
2207 shader_addline(&buffer, "<unrecognized instruction>\n");
2208 continue;
2211 if (ins.handler_idx == WINED3DSIH_DCL || ins.handler_idx == WINED3DSIH_DCL_UAV_TYPED)
2213 shader_dump_decl_usage(&buffer, &ins.declaration.semantic, &shader_version);
2214 shader_dump_ins_modifiers(&buffer, &ins.declaration.semantic.reg);
2215 shader_addline(&buffer, " ");
2216 shader_dump_dst_param(&buffer, &ins.declaration.semantic.reg, &shader_version);
2218 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
2220 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2221 shader_dump_src_param(&buffer, &ins.declaration.src, &shader_version);
2222 shader_addline(&buffer, ", %s",
2223 ins.flags & WINED3DSI_INDEXED_DYNAMIC ? "dynamicIndexed" : "immediateIndexed");
2225 else if (ins.handler_idx == WINED3DSIH_DCL_GLOBAL_FLAGS)
2227 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2228 shader_dump_global_flags(&buffer, ins.flags);
2230 else if (ins.handler_idx == WINED3DSIH_DCL_HS_MAX_TESSFACTOR)
2232 shader_addline(&buffer, "%s %.8e", shader_opcode_names[ins.handler_idx],
2233 ins.declaration.max_tessellation_factor);
2235 else if (ins.handler_idx == WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER)
2237 shader_addline(&buffer, "%s {\n", shader_opcode_names[ins.handler_idx]);
2238 for (i = 0; i < ins.declaration.icb->vec4_count; ++i)
2240 shader_addline(&buffer, " {0x%08x, 0x%08x, 0x%08x, 0x%08x},\n",
2241 ins.declaration.icb->data[4 * i + 0],
2242 ins.declaration.icb->data[4 * i + 1],
2243 ins.declaration.icb->data[4 * i + 2],
2244 ins.declaration.icb->data[4 * i + 3]);
2246 shader_addline(&buffer, "}");
2248 else if (ins.handler_idx == WINED3DSIH_DCL_INDEXABLE_TEMP)
2250 shader_addline(&buffer, "%s x[%u][%u], %u", shader_opcode_names[ins.handler_idx],
2251 ins.declaration.indexable_temp.register_idx,
2252 ins.declaration.indexable_temp.register_size,
2253 ins.declaration.indexable_temp.component_count);
2255 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS)
2257 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2258 shader_dump_interpolation_mode(&buffer, ins.flags);
2259 shader_addline(&buffer, " ");
2260 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2262 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SGV
2263 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SGV
2264 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SIV
2265 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_SIV)
2267 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2268 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2269 shader_addline(&buffer, ", ");
2270 shader_dump_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2272 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SIV)
2274 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2275 shader_dump_interpolation_mode(&buffer, ins.flags);
2276 shader_addline(&buffer, " ");
2277 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2278 shader_addline(&buffer, ", ");
2279 shader_dump_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2281 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT
2282 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT)
2284 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2285 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2287 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE
2288 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
2290 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2291 shader_dump_primitive_type(&buffer, ins.declaration.primitive_type);
2293 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_STRUCTURED)
2295 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2296 shader_dump_dst_param(&buffer, &ins.declaration.structured_resource.reg, &shader_version);
2297 shader_addline(&buffer, ", %u", ins.declaration.structured_resource.byte_stride);
2299 else if (ins.handler_idx == WINED3DSIH_DCL_SAMPLER)
2301 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2302 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2303 if (ins.flags == WINED3DSI_SAMPLER_COMPARISON_MODE)
2304 shader_addline(&buffer, ", comparisonMode");
2306 else if (ins.handler_idx == WINED3DSIH_DCL_TEMPS
2307 || ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT
2308 || ins.handler_idx == WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT
2309 || ins.handler_idx == WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT
2310 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT)
2312 shader_addline(&buffer, "%s %u", shader_opcode_names[ins.handler_idx], ins.declaration.count);
2314 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_DOMAIN)
2316 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2317 shader_dump_tessellator_domain(&buffer, ins.declaration.tessellator_domain);
2319 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE)
2321 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2322 shader_dump_tessellator_output_primitive(&buffer, ins.declaration.tessellator_output_primitive);
2324 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_PARTITIONING)
2326 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2327 shader_dump_tessellator_partitioning(&buffer, ins.declaration.tessellator_partitioning);
2329 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_RAW)
2331 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2332 shader_dump_dst_param(&buffer, &ins.declaration.tgsm_raw.reg, &shader_version);
2333 shader_addline(&buffer, ", %u", ins.declaration.tgsm_raw.byte_count);
2335 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_STRUCTURED)
2337 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2338 shader_dump_dst_param(&buffer, &ins.declaration.tgsm_structured.reg, &shader_version);
2339 shader_addline(&buffer, ", %u, %u", ins.declaration.tgsm_structured.byte_stride,
2340 ins.declaration.tgsm_structured.structure_count);
2342 else if (ins.handler_idx == WINED3DSIH_DCL_THREAD_GROUP)
2344 shader_addline(&buffer, "%s %u, %u, %u", shader_opcode_names[ins.handler_idx],
2345 ins.declaration.thread_group_size.x,
2346 ins.declaration.thread_group_size.y,
2347 ins.declaration.thread_group_size.z);
2349 else if (ins.handler_idx == WINED3DSIH_DEF)
2351 shader_addline(&buffer, "def c%u = %.8e, %.8e, %.8e, %.8e", shader_get_float_offset(ins.dst[0].reg.type,
2352 ins.dst[0].reg.idx[0].offset),
2353 *(const float *)&ins.src[0].reg.immconst_data[0],
2354 *(const float *)&ins.src[0].reg.immconst_data[1],
2355 *(const float *)&ins.src[0].reg.immconst_data[2],
2356 *(const float *)&ins.src[0].reg.immconst_data[3]);
2358 else if (ins.handler_idx == WINED3DSIH_DEFI)
2360 shader_addline(&buffer, "defi i%u = %d, %d, %d, %d", ins.dst[0].reg.idx[0].offset,
2361 ins.src[0].reg.immconst_data[0],
2362 ins.src[0].reg.immconst_data[1],
2363 ins.src[0].reg.immconst_data[2],
2364 ins.src[0].reg.immconst_data[3]);
2366 else if (ins.handler_idx == WINED3DSIH_DEFB)
2368 shader_addline(&buffer, "defb b%u = %s",
2369 ins.dst[0].reg.idx[0].offset, ins.src[0].reg.immconst_data[0] ? "true" : "false");
2371 else
2373 if (ins.predicate)
2375 shader_addline(&buffer, "(");
2376 shader_dump_src_param(&buffer, ins.predicate, &shader_version);
2377 shader_addline(&buffer, ") ");
2380 /* PixWin marks instructions with the coissue flag with a '+' */
2381 if (ins.coissue)
2382 shader_addline(&buffer, "+");
2384 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2386 if (ins.handler_idx == WINED3DSIH_BREAKP
2387 || ins.handler_idx == WINED3DSIH_IF)
2389 switch (ins.flags)
2391 case WINED3D_SHADER_CONDITIONAL_OP_NZ: shader_addline(&buffer, "_nz"); break;
2392 case WINED3D_SHADER_CONDITIONAL_OP_Z: shader_addline(&buffer, "_z"); break;
2393 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags); break;
2396 else if (ins.handler_idx == WINED3DSIH_IFC
2397 || ins.handler_idx == WINED3DSIH_BREAKC)
2399 switch (ins.flags)
2401 case WINED3D_SHADER_REL_OP_GT: shader_addline(&buffer, "_gt"); break;
2402 case WINED3D_SHADER_REL_OP_EQ: shader_addline(&buffer, "_eq"); break;
2403 case WINED3D_SHADER_REL_OP_GE: shader_addline(&buffer, "_ge"); break;
2404 case WINED3D_SHADER_REL_OP_LT: shader_addline(&buffer, "_lt"); break;
2405 case WINED3D_SHADER_REL_OP_NE: shader_addline(&buffer, "_ne"); break;
2406 case WINED3D_SHADER_REL_OP_LE: shader_addline(&buffer, "_le"); break;
2407 default: shader_addline(&buffer, "_(%u)", ins.flags);
2410 else if (ins.handler_idx == WINED3DSIH_TEX
2411 && shader_version.major >= 2
2412 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
2414 shader_addline(&buffer, "p");
2416 else if (ins.handler_idx == WINED3DSIH_RESINFO
2417 && ins.flags)
2419 switch (ins.flags)
2421 case WINED3DSI_RESINFO_RCP_FLOAT: shader_addline(&buffer, "_rcpFloat"); break;
2422 case WINED3DSI_RESINFO_UINT: shader_addline(&buffer, "_uint"); break;
2423 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags);
2427 if (wined3d_shader_instruction_has_texel_offset(&ins))
2428 shader_addline(&buffer, "(%d,%d,%d)", ins.texel_offset.u, ins.texel_offset.v, ins.texel_offset.w);
2430 for (i = 0; i < ins.dst_count; ++i)
2432 shader_dump_ins_modifiers(&buffer, &ins.dst[i]);
2433 shader_addline(&buffer, !i ? " " : ", ");
2434 shader_dump_dst_param(&buffer, &ins.dst[i], &shader_version);
2437 /* Other source tokens */
2438 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
2440 shader_addline(&buffer, !i ? " " : ", ");
2441 shader_dump_src_param(&buffer, &ins.src[i - ins.dst_count], &shader_version);
2444 shader_addline(&buffer, "\n");
2447 for (p = buffer.buffer; *p; p = q)
2449 if (!(q = strstr(p, "\n")))
2450 q = p + strlen(p);
2451 else
2452 ++q;
2453 TRACE(" %.*s", (int)(q - p), p);
2456 string_buffer_free(&buffer);
2459 static void shader_cleanup(struct wined3d_shader *shader)
2461 HeapFree(GetProcessHeap(), 0, shader->output_signature.elements);
2462 HeapFree(GetProcessHeap(), 0, shader->input_signature.elements);
2463 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
2464 shader->device->shader_backend->shader_destroy(shader);
2465 shader_cleanup_reg_maps(&shader->reg_maps);
2466 HeapFree(GetProcessHeap(), 0, shader->function);
2467 shader_delete_constant_list(&shader->constantsF);
2468 shader_delete_constant_list(&shader->constantsB);
2469 shader_delete_constant_list(&shader->constantsI);
2470 list_remove(&shader->shader_list_entry);
2472 if (shader->frontend && shader->frontend_data)
2473 shader->frontend->shader_free(shader->frontend_data);
2476 struct shader_none_priv
2478 const struct wined3d_vertex_pipe_ops *vertex_pipe;
2479 const struct fragment_pipeline *fragment_pipe;
2480 BOOL ffp_proj_control;
2483 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
2484 static void shader_none_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
2485 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size) {}
2486 static void shader_none_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info) {}
2487 static void shader_none_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count) {}
2488 static void shader_none_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count) {}
2489 static void shader_none_load_constants(void *shader_priv, struct wined3d_context *context,
2490 const struct wined3d_state *state) {}
2491 static void shader_none_destroy(struct wined3d_shader *shader) {}
2492 static void shader_none_free_context_data(struct wined3d_context *context) {}
2493 static void shader_none_init_context_state(struct wined3d_context *context) {}
2495 /* Context activation is done by the caller. */
2496 static void shader_none_select(void *shader_priv, struct wined3d_context *context,
2497 const struct wined3d_state *state)
2499 const struct wined3d_gl_info *gl_info = context->gl_info;
2500 struct shader_none_priv *priv = shader_priv;
2502 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
2503 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
2506 /* Context activation is done by the caller. */
2507 static void shader_none_disable(void *shader_priv, struct wined3d_context *context)
2509 struct shader_none_priv *priv = shader_priv;
2510 const struct wined3d_gl_info *gl_info = context->gl_info;
2512 priv->vertex_pipe->vp_enable(gl_info, FALSE);
2513 priv->fragment_pipe->enable_extension(gl_info, FALSE);
2515 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
2516 | (1u << WINED3D_SHADER_TYPE_VERTEX)
2517 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
2518 | (1u << WINED3D_SHADER_TYPE_HULL)
2519 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
2520 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
2523 static HRESULT shader_none_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
2524 const struct fragment_pipeline *fragment_pipe)
2526 struct fragment_caps fragment_caps;
2527 void *vertex_priv, *fragment_priv;
2528 struct shader_none_priv *priv;
2530 if (!(priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv))))
2531 return E_OUTOFMEMORY;
2533 if (!(vertex_priv = vertex_pipe->vp_alloc(&none_shader_backend, priv)))
2535 ERR("Failed to initialize vertex pipe.\n");
2536 HeapFree(GetProcessHeap(), 0, priv);
2537 return E_FAIL;
2540 if (!(fragment_priv = fragment_pipe->alloc_private(&none_shader_backend, priv)))
2542 ERR("Failed to initialize fragment pipe.\n");
2543 vertex_pipe->vp_free(device);
2544 HeapFree(GetProcessHeap(), 0, priv);
2545 return E_FAIL;
2548 priv->vertex_pipe = vertex_pipe;
2549 priv->fragment_pipe = fragment_pipe;
2550 fragment_pipe->get_caps(&device->adapter->gl_info, &fragment_caps);
2551 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
2553 device->vertex_priv = vertex_priv;
2554 device->fragment_priv = fragment_priv;
2555 device->shader_priv = priv;
2557 return WINED3D_OK;
2560 static void shader_none_free(struct wined3d_device *device)
2562 struct shader_none_priv *priv = device->shader_priv;
2564 priv->fragment_pipe->free_private(device);
2565 priv->vertex_pipe->vp_free(device);
2566 HeapFree(GetProcessHeap(), 0, priv);
2569 static BOOL shader_none_allocate_context_data(struct wined3d_context *context)
2571 return TRUE;
2574 static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
2576 /* Set the shader caps to 0 for the none shader backend */
2577 caps->vs_version = 0;
2578 caps->hs_version = 0;
2579 caps->ds_version = 0;
2580 caps->gs_version = 0;
2581 caps->ps_version = 0;
2582 caps->cs_version = 0;
2583 caps->vs_uniform_count = 0;
2584 caps->ps_uniform_count = 0;
2585 caps->ps_1x_max_value = 0.0f;
2586 caps->varying_count = 0;
2587 caps->wined3d_caps = 0;
2590 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
2592 /* We "support" every possible fixup, since we don't support any shader
2593 * model, and will never have to actually sample a texture. */
2594 return TRUE;
2597 static BOOL shader_none_has_ffp_proj_control(void *shader_priv)
2599 struct shader_none_priv *priv = shader_priv;
2601 return priv->ffp_proj_control;
2604 const struct wined3d_shader_backend_ops none_shader_backend =
2606 shader_none_handle_instruction,
2607 shader_none_select,
2608 shader_none_disable,
2609 shader_none_select_depth_blt,
2610 shader_none_deselect_depth_blt,
2611 shader_none_update_float_vertex_constants,
2612 shader_none_update_float_pixel_constants,
2613 shader_none_load_constants,
2614 shader_none_destroy,
2615 shader_none_alloc,
2616 shader_none_free,
2617 shader_none_allocate_context_data,
2618 shader_none_free_context_data,
2619 shader_none_init_context_state,
2620 shader_none_get_caps,
2621 shader_none_color_fixup_supported,
2622 shader_none_has_ffp_proj_control,
2625 static HRESULT shader_set_function(struct wined3d_shader *shader, const DWORD *byte_code,
2626 DWORD float_const_count, enum wined3d_shader_type type, unsigned int max_version)
2628 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2629 const struct wined3d_shader_frontend *fe;
2630 HRESULT hr;
2631 unsigned int backend_version;
2632 const struct wined3d_d3d_info *d3d_info = &shader->device->adapter->d3d_info;
2634 TRACE("shader %p, byte_code %p, float_const_count %u, type %#x, max_version %u.\n",
2635 shader, byte_code, float_const_count, type, max_version);
2637 list_init(&shader->constantsF);
2638 list_init(&shader->constantsB);
2639 list_init(&shader->constantsI);
2640 shader->lconst_inf_or_nan = FALSE;
2641 list_init(&reg_maps->indexable_temps);
2643 fe = shader_select_frontend(*byte_code);
2644 if (!fe)
2646 FIXME("Unable to find frontend for shader.\n");
2647 return WINED3DERR_INVALIDCALL;
2649 shader->frontend = fe;
2650 shader->frontend_data = fe->shader_init(byte_code, &shader->output_signature);
2651 if (!shader->frontend_data)
2653 FIXME("Failed to initialize frontend.\n");
2654 return WINED3DERR_INVALIDCALL;
2657 /* First pass: trace shader. */
2658 if (TRACE_ON(d3d_shader))
2659 shader_trace_init(fe, shader->frontend_data, byte_code);
2661 /* Second pass: figure out which registers are used, what the semantics are, etc. */
2662 if (FAILED(hr = shader_get_registers_used(shader, fe, reg_maps, &shader->input_signature,
2663 &shader->output_signature, byte_code, float_const_count)))
2664 return hr;
2666 if (reg_maps->shader_version.type != type)
2668 WARN("Wrong shader type %d.\n", reg_maps->shader_version.type);
2669 return WINED3DERR_INVALIDCALL;
2671 if (reg_maps->shader_version.major > max_version)
2673 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps->shader_version.major);
2674 return WINED3DERR_INVALIDCALL;
2676 switch (type)
2678 case WINED3D_SHADER_TYPE_VERTEX:
2679 backend_version = d3d_info->limits.vs_version;
2680 break;
2681 case WINED3D_SHADER_TYPE_HULL:
2682 backend_version = d3d_info->limits.hs_version;
2683 break;
2684 case WINED3D_SHADER_TYPE_DOMAIN:
2685 backend_version = d3d_info->limits.ds_version;
2686 break;
2687 case WINED3D_SHADER_TYPE_GEOMETRY:
2688 backend_version = d3d_info->limits.gs_version;
2689 break;
2690 case WINED3D_SHADER_TYPE_PIXEL:
2691 backend_version = d3d_info->limits.ps_version;
2692 break;
2693 case WINED3D_SHADER_TYPE_COMPUTE:
2694 backend_version = d3d_info->limits.cs_version;
2695 break;
2696 default:
2697 FIXME("No backend version-checking for this shader type.\n");
2698 backend_version = 0;
2700 if (reg_maps->shader_version.major > backend_version)
2702 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
2703 reg_maps->shader_version.major, reg_maps->shader_version.minor);
2704 return WINED3DERR_INVALIDCALL;
2707 if (!(shader->function = HeapAlloc(GetProcessHeap(), 0, shader->functionLength)))
2708 return E_OUTOFMEMORY;
2709 memcpy(shader->function, byte_code, shader->functionLength);
2711 return WINED3D_OK;
2714 ULONG CDECL wined3d_shader_incref(struct wined3d_shader *shader)
2716 ULONG refcount = InterlockedIncrement(&shader->ref);
2718 TRACE("%p increasing refcount to %u.\n", shader, refcount);
2720 return refcount;
2723 static void wined3d_shader_destroy_object(void *object)
2725 shader_cleanup(object);
2726 HeapFree(GetProcessHeap(), 0, object);
2729 ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
2731 ULONG refcount = InterlockedDecrement(&shader->ref);
2733 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
2735 if (!refcount)
2737 shader->parent_ops->wined3d_object_destroyed(shader->parent);
2738 wined3d_cs_emit_destroy_object(shader->device->cs, wined3d_shader_destroy_object, shader);
2741 return refcount;
2744 void * CDECL wined3d_shader_get_parent(const struct wined3d_shader *shader)
2746 TRACE("shader %p.\n", shader);
2748 return shader->parent;
2751 HRESULT CDECL wined3d_shader_get_byte_code(const struct wined3d_shader *shader,
2752 void *byte_code, UINT *byte_code_size)
2754 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader, byte_code, byte_code_size);
2756 if (!byte_code)
2758 *byte_code_size = shader->functionLength;
2759 return WINED3D_OK;
2762 if (*byte_code_size < shader->functionLength)
2764 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
2765 * than the required size we should write the required size and
2766 * return D3DERR_MOREDATA. That's not actually true. */
2767 return WINED3DERR_INVALIDCALL;
2770 memcpy(byte_code, shader->function, shader->functionLength);
2772 return WINED3D_OK;
2775 /* Set local constants for d3d8 shaders. */
2776 HRESULT CDECL wined3d_shader_set_local_constants_float(struct wined3d_shader *shader,
2777 UINT start_idx, const float *src_data, UINT count)
2779 UINT end_idx = start_idx + count;
2780 UINT i;
2782 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader, start_idx, src_data, count);
2784 if (end_idx > shader->limits->constant_float)
2786 WARN("end_idx %u > float constants limit %u.\n",
2787 end_idx, shader->limits->constant_float);
2788 end_idx = shader->limits->constant_float;
2791 for (i = start_idx; i < end_idx; ++i)
2793 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
2794 float *value;
2795 if (!lconst)
2796 return E_OUTOFMEMORY;
2798 lconst->idx = i;
2799 value = (float *)lconst->value;
2800 memcpy(value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
2801 list_add_head(&shader->constantsF, &lconst->entry);
2803 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
2804 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
2806 shader->lconst_inf_or_nan = TRUE;
2810 return WINED3D_OK;
2813 void find_vs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
2814 WORD swizzle_map, struct vs_compile_args *args, const struct wined3d_d3d_info *d3d_info)
2816 args->fog_src = state->render_states[WINED3D_RS_FOGTABLEMODE]
2817 == WINED3D_FOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
2818 args->clip_enabled = state->render_states[WINED3D_RS_CLIPPING]
2819 && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
2820 args->point_size = state->gl_primitive_type == GL_POINTS;
2821 args->per_vertex_point_size = shader->reg_maps.point_size;
2822 args->next_shader_type = state->shader[WINED3D_SHADER_TYPE_HULL] ? WINED3D_SHADER_TYPE_HULL
2823 : state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ? WINED3D_SHADER_TYPE_GEOMETRY
2824 : WINED3D_SHADER_TYPE_PIXEL;
2825 if (shader->reg_maps.shader_version.major >= 4)
2826 args->next_shader_input_count = state->shader[WINED3D_SHADER_TYPE_HULL]
2827 ? state->shader[WINED3D_SHADER_TYPE_HULL]->limits->packed_input
2828 : state->shader[WINED3D_SHADER_TYPE_GEOMETRY]
2829 ? state->shader[WINED3D_SHADER_TYPE_GEOMETRY]->limits->packed_input
2830 : state->shader[WINED3D_SHADER_TYPE_PIXEL]
2831 ? state->shader[WINED3D_SHADER_TYPE_PIXEL]->limits->packed_input : 0;
2832 else
2833 args->next_shader_input_count = 0;
2834 args->swizzle_map = swizzle_map;
2835 if (d3d_info->emulated_flatshading)
2836 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
2837 else
2838 args->flatshading = 0;
2841 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
2843 if (usage_idx1 != usage_idx2)
2844 return FALSE;
2845 if (usage1 == usage2)
2846 return TRUE;
2847 if (usage1 == WINED3D_DECL_USAGE_POSITION && usage2 == WINED3D_DECL_USAGE_POSITIONT)
2848 return TRUE;
2849 if (usage2 == WINED3D_DECL_USAGE_POSITION && usage1 == WINED3D_DECL_USAGE_POSITIONT)
2850 return TRUE;
2852 return FALSE;
2855 BOOL vshader_get_input(const struct wined3d_shader *shader,
2856 BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
2858 WORD map = shader->reg_maps.input_registers;
2859 unsigned int i;
2861 for (i = 0; map; map >>= 1, ++i)
2863 if (!(map & 1)) continue;
2865 if (match_usage(shader->u.vs.attributes[i].usage,
2866 shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
2868 *regnum = i;
2869 return TRUE;
2872 return FALSE;
2875 static HRESULT shader_signature_copy(struct wined3d_shader_signature *dst,
2876 const struct wined3d_shader_signature *src, char **signature_strings)
2878 struct wined3d_shader_signature_element *e;
2879 unsigned int i;
2880 SIZE_T len;
2881 char *ptr;
2883 if (!src->element_count)
2884 return WINED3D_OK;
2886 ptr = *signature_strings;
2888 dst->element_count = src->element_count;
2889 if (!(dst->elements = wined3d_calloc(dst->element_count, sizeof(*dst->elements))))
2890 return E_OUTOFMEMORY;
2892 for (i = 0; i < src->element_count; ++i)
2894 e = &src->elements[i];
2895 dst->elements[i] = *e;
2897 len = strlen(e->semantic_name);
2898 memcpy(ptr, e->semantic_name, len + 1);
2899 dst->elements[i].semantic_name = ptr;
2900 ptr += len + 1;
2903 *signature_strings = ptr;
2905 return WINED3D_OK;
2908 static HRESULT shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2909 const struct wined3d_shader_desc *desc, DWORD float_const_count, enum wined3d_shader_type type,
2910 void *parent, const struct wined3d_parent_ops *parent_ops)
2912 struct wined3d_shader_signature_element *e;
2913 SIZE_T total, len;
2914 unsigned int i;
2915 HRESULT hr;
2916 char *ptr;
2918 if (!desc->byte_code)
2919 return WINED3DERR_INVALIDCALL;
2921 shader->ref = 1;
2922 shader->device = device;
2923 shader->parent = parent;
2924 shader->parent_ops = parent_ops;
2926 total = 0;
2927 for (i = 0; i < desc->input_signature.element_count; ++i)
2929 e = &desc->input_signature.elements[i];
2930 len = strlen(e->semantic_name);
2931 if (len >= ~(SIZE_T)0 - total)
2932 return E_OUTOFMEMORY;
2934 total += len + 1;
2936 for (i = 0; i < desc->output_signature.element_count; ++i)
2938 e = &desc->output_signature.elements[i];
2939 len = strlen(e->semantic_name);
2940 if (len >= ~(SIZE_T)0 - total)
2941 return E_OUTOFMEMORY;
2943 total += len + 1;
2945 if (total && !(shader->signature_strings = HeapAlloc(GetProcessHeap(), 0, total)))
2946 return E_OUTOFMEMORY;
2947 ptr = shader->signature_strings;
2949 if (FAILED(hr = shader_signature_copy(&shader->input_signature, &desc->input_signature, &ptr)))
2951 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
2952 return hr;
2954 if (FAILED(hr = shader_signature_copy(&shader->output_signature, &desc->output_signature, &ptr)))
2956 HeapFree(GetProcessHeap(), 0, shader->input_signature.elements);
2957 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
2958 return hr;
2961 list_init(&shader->linked_programs);
2962 list_add_head(&device->shaders, &shader->shader_list_entry);
2964 if (FAILED(hr = shader_set_function(shader, desc->byte_code,
2965 float_const_count, type, desc->max_version)))
2967 WARN("Failed to set function, hr %#x.\n", hr);
2968 shader_cleanup(shader);
2971 shader->load_local_constsF = shader->lconst_inf_or_nan;
2973 return hr;
2976 static HRESULT vertex_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2977 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
2979 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2980 unsigned int i;
2981 HRESULT hr;
2983 if (FAILED(hr = shader_init(shader, device, desc, device->adapter->d3d_info.limits.vs_uniform_count,
2984 WINED3D_SHADER_TYPE_VERTEX, parent, parent_ops)))
2985 return hr;
2987 for (i = 0; i < shader->input_signature.element_count; ++i)
2989 const struct wined3d_shader_signature_element *input = &shader->input_signature.elements[i];
2991 if (!(reg_maps->input_registers & (1u << input->register_idx)) || !input->semantic_name)
2992 continue;
2994 shader->u.vs.attributes[input->register_idx].usage =
2995 shader_usage_from_semantic_name(input->semantic_name);
2996 shader->u.vs.attributes[input->register_idx].usage_idx = input->semantic_idx;
2999 if (reg_maps->usesrelconstF && !list_empty(&shader->constantsF))
3000 shader->load_local_constsF = TRUE;
3002 return WINED3D_OK;
3005 static HRESULT domain_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3006 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3008 return shader_init(shader, device, desc, 0, WINED3D_SHADER_TYPE_DOMAIN, parent, parent_ops);
3011 static HRESULT hull_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3012 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3014 return shader_init(shader, device, desc, 0, WINED3D_SHADER_TYPE_HULL, parent, parent_ops);
3017 static HRESULT geometry_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3018 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3020 return shader_init(shader, device, desc, 0, WINED3D_SHADER_TYPE_GEOMETRY, parent, parent_ops);
3023 void find_gs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3024 struct gs_compile_args *args)
3026 args->ps_input_count = state->shader[WINED3D_SHADER_TYPE_PIXEL]
3027 ? state->shader[WINED3D_SHADER_TYPE_PIXEL]->limits->packed_input : 0;
3030 void find_ps_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3031 BOOL position_transformed, struct ps_compile_args *args, const struct wined3d_context *context)
3033 const struct wined3d_gl_info *gl_info = context->gl_info;
3034 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3035 const struct wined3d_texture *texture;
3036 UINT i;
3038 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
3039 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, state->fb))
3041 static unsigned int warned = 0;
3043 args->srgb_correction = 1;
3044 if (state->render_states[WINED3D_RS_ALPHABLENDENABLE] && !warned++)
3045 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
3046 "support, expect rendering artifacts.\n");
3049 if (shader->reg_maps.shader_version.major == 1
3050 && shader->reg_maps.shader_version.minor <= 3)
3052 for (i = 0; i < shader->limits->sampler; ++i)
3054 DWORD flags = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
3056 if (flags & WINED3D_TTFF_PROJECTED)
3058 DWORD tex_transform = flags & ~WINED3D_TTFF_PROJECTED;
3060 if (!state->shader[WINED3D_SHADER_TYPE_VERTEX])
3062 enum wined3d_shader_resource_type resource_type = shader->reg_maps.resource_info[i].type;
3063 unsigned int j;
3064 unsigned int index = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
3065 DWORD max_valid = WINED3D_TTFF_COUNT4;
3067 for (j = 0; j < state->vertex_declaration->element_count; ++j)
3069 struct wined3d_vertex_declaration_element *element =
3070 &state->vertex_declaration->elements[j];
3072 if (element->usage == WINED3D_DECL_USAGE_TEXCOORD
3073 && element->usage_idx == index)
3075 max_valid = element->format->component_count;
3076 break;
3079 if (!tex_transform || tex_transform > max_valid)
3081 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
3082 tex_transform, max_valid);
3083 tex_transform = max_valid;
3085 if ((resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1D && tex_transform > WINED3D_TTFF_COUNT1)
3086 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D
3087 && tex_transform > WINED3D_TTFF_COUNT2)
3088 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_3D
3089 && tex_transform > WINED3D_TTFF_COUNT3))
3090 tex_transform |= WINED3D_PSARGS_PROJECTED;
3091 else
3093 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
3094 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
3095 i, tex_transform, resource_type);
3098 else
3099 tex_transform = WINED3D_TTFF_COUNT4 | WINED3D_PSARGS_PROJECTED;
3101 args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
3105 if (shader->reg_maps.shader_version.major == 1
3106 && shader->reg_maps.shader_version.minor <= 4)
3108 for (i = 0; i < shader->limits->sampler; ++i)
3110 const struct wined3d_texture *texture = state->textures[i];
3112 if (!shader->reg_maps.resource_info[i].type)
3113 continue;
3115 /* Treat unbound textures as 2D. The dummy texture will provide
3116 * the proper sample value. The tex_types bitmap defaults to
3117 * 2D because of the memset. */
3118 if (!texture)
3119 continue;
3121 switch (texture->target)
3123 /* RECT textures are distinguished from 2D textures via np2_fixup */
3124 case GL_TEXTURE_RECTANGLE_ARB:
3125 case GL_TEXTURE_2D:
3126 break;
3128 case GL_TEXTURE_3D:
3129 args->tex_types |= WINED3D_SHADER_TEX_3D << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
3130 break;
3132 case GL_TEXTURE_CUBE_MAP_ARB:
3133 args->tex_types |= WINED3D_SHADER_TEX_CUBE << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
3134 break;
3139 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
3141 if (!shader->reg_maps.resource_info[i].type)
3142 continue;
3144 texture = state->textures[i];
3145 if (!texture)
3147 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
3148 continue;
3150 if (can_use_texture_swizzle(gl_info, texture->resource.format))
3151 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
3152 else
3153 args->color_fixup[i] = texture->resource.format->color_fixup;
3155 if (texture->resource.format_flags & WINED3DFMT_FLAG_SHADOW)
3156 args->shadow |= 1u << i;
3158 /* Flag samplers that need NP2 texcoord fixup. */
3159 if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
3160 args->np2_fixup |= (1u << i);
3163 /* In SM4+ we use dcl_sampler in order to determine if we should use shadow sampler. */
3164 if (shader->reg_maps.shader_version.major >= 4)
3165 args->shadow = 0;
3167 if (shader->reg_maps.shader_version.major >= 3)
3169 if (position_transformed)
3170 args->vp_mode = pretransformed;
3171 else if (use_vs(state))
3172 args->vp_mode = vertexshader;
3173 else
3174 args->vp_mode = fixedfunction;
3175 args->fog = WINED3D_FFP_PS_FOG_OFF;
3177 else
3179 args->vp_mode = vertexshader;
3180 if (state->render_states[WINED3D_RS_FOGENABLE])
3182 switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
3184 case WINED3D_FOG_NONE:
3185 if (position_transformed || use_vs(state))
3187 args->fog = WINED3D_FFP_PS_FOG_LINEAR;
3188 break;
3191 switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
3193 case WINED3D_FOG_NONE: /* Fall through. */
3194 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
3195 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
3196 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
3198 break;
3200 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
3201 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
3202 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
3205 else
3207 args->fog = WINED3D_FFP_PS_FOG_OFF;
3211 if (context->d3d_info->limits.varying_count < wined3d_max_compat_varyings(context->gl_info))
3213 const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3215 args->texcoords_initialized = 0;
3216 for (i = 0; i < MAX_TEXTURES; ++i)
3218 if (vs)
3220 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.output_registers & (1u << i))
3221 args->texcoords_initialized |= 1u << i;
3223 else
3225 const struct wined3d_stream_info *si = &context->stream_info;
3226 unsigned int coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
3228 if ((state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX] >> WINED3D_FFP_TCI_SHIFT)
3229 & WINED3D_FFP_TCI_MASK
3230 || (coord_idx < MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))))
3231 args->texcoords_initialized |= 1u << i;
3235 else
3237 args->texcoords_initialized = (1u << MAX_TEXTURES) - 1;
3240 args->pointsprite = state->render_states[WINED3D_RS_POINTSPRITEENABLE]
3241 && state->gl_primitive_type == GL_POINTS;
3243 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3244 args->alpha_test_func = WINED3D_CMP_ALWAYS - 1;
3245 else
3246 args->alpha_test_func = (state->render_states[WINED3D_RS_ALPHATESTENABLE]
3247 ? wined3d_sanitize_cmp_func(state->render_states[WINED3D_RS_ALPHAFUNC])
3248 : WINED3D_CMP_ALWAYS) - 1;
3250 if (d3d_info->emulated_flatshading)
3251 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
3254 static HRESULT pixel_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3255 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3257 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3258 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
3259 HRESULT hr;
3261 if (FAILED(hr = shader_init(shader, device, desc, device->adapter->d3d_info.limits.ps_uniform_count,
3262 WINED3D_SHADER_TYPE_PIXEL, parent, parent_ops)))
3263 return hr;
3265 for (i = 0; i < MAX_REG_INPUT; ++i)
3267 if (shader->u.ps.input_reg_used[i])
3269 ++num_regs_used;
3270 highest_reg_used = i;
3274 /* Don't do any register mapping magic if it is not needed, or if we can't
3275 * achieve anything anyway */
3276 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
3277 || num_regs_used > (gl_info->limits.glsl_varyings / 4)
3278 || shader->reg_maps.shader_version.major >= 4)
3280 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
3282 /* This happens with relative addressing. The input mapper function
3283 * warns about this if the higher registers are declared too, so
3284 * don't write a FIXME here */
3285 WARN("More varying registers used than supported\n");
3288 for (i = 0; i < MAX_REG_INPUT; ++i)
3290 shader->u.ps.input_reg_map[i] = i;
3293 shader->u.ps.declared_in_count = highest_reg_used + 1;
3295 else
3297 shader->u.ps.declared_in_count = 0;
3298 for (i = 0; i < MAX_REG_INPUT; ++i)
3300 if (shader->u.ps.input_reg_used[i])
3301 shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
3302 else shader->u.ps.input_reg_map[i] = ~0U;
3306 return WINED3D_OK;
3309 void pixelshader_update_resource_types(struct wined3d_shader *shader, WORD tex_types)
3311 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3312 struct wined3d_shader_resource_info *resource_info = reg_maps->resource_info;
3313 unsigned int i;
3315 if (reg_maps->shader_version.major != 1) return;
3317 for (i = 0; i < shader->limits->sampler; ++i)
3319 /* We don't sample from this sampler. */
3320 if (!resource_info[i].type)
3321 continue;
3323 switch ((tex_types >> i * WINED3D_PSARGS_TEXTYPE_SHIFT) & WINED3D_PSARGS_TEXTYPE_MASK)
3325 case WINED3D_SHADER_TEX_2D:
3326 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3327 break;
3329 case WINED3D_SHADER_TEX_3D:
3330 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_3D;
3331 break;
3333 case WINED3D_SHADER_TEX_CUBE:
3334 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_CUBE;
3335 break;
3340 static HRESULT compute_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3341 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3343 return shader_init(shader, device, desc, 0, WINED3D_SHADER_TYPE_COMPUTE, parent, parent_ops);
3346 HRESULT CDECL wined3d_shader_create_cs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3347 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3349 struct wined3d_shader *object;
3350 HRESULT hr;
3352 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3353 device, desc, parent, parent_ops, shader);
3355 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
3356 return E_OUTOFMEMORY;
3358 if (FAILED(hr = compute_shader_init(object, device, desc, parent, parent_ops)))
3360 WARN("Failed to initialize compute shader, hr %#x.\n", hr);
3361 HeapFree(GetProcessHeap(), 0, object);
3362 return hr;
3365 TRACE("Created compute shader %p.\n", object);
3366 *shader = object;
3368 return WINED3D_OK;
3371 HRESULT CDECL wined3d_shader_create_ds(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3372 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3374 struct wined3d_shader *object;
3375 HRESULT hr;
3377 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3378 device, desc, parent, parent_ops, shader);
3380 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
3381 return E_OUTOFMEMORY;
3383 if (FAILED(hr = domain_shader_init(object, device, desc, parent, parent_ops)))
3385 WARN("Failed to initialize domain shader, hr %#x.\n", hr);
3386 HeapFree(GetProcessHeap(), 0, object);
3387 return hr;
3390 TRACE("Created domain shader %p.\n", object);
3391 *shader = object;
3393 return WINED3D_OK;
3396 HRESULT CDECL wined3d_shader_create_gs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3397 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3399 struct wined3d_shader *object;
3400 HRESULT hr;
3402 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3403 device, desc, parent, parent_ops, shader);
3405 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3406 if (!object)
3407 return E_OUTOFMEMORY;
3409 if (FAILED(hr = geometry_shader_init(object, device, desc, parent, parent_ops)))
3411 WARN("Failed to initialize geometry shader, hr %#x.\n", hr);
3412 HeapFree(GetProcessHeap(), 0, object);
3413 return hr;
3416 TRACE("Created geometry shader %p.\n", object);
3417 *shader = object;
3419 return WINED3D_OK;
3422 HRESULT CDECL wined3d_shader_create_hs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3423 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3425 struct wined3d_shader *object;
3426 HRESULT hr;
3428 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3429 device, desc, parent, parent_ops, shader);
3431 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
3432 return E_OUTOFMEMORY;
3434 if (FAILED(hr = hull_shader_init(object, device, desc, parent, parent_ops)))
3436 WARN("Failed to initialize hull shader, hr %#x.\n", hr);
3437 HeapFree(GetProcessHeap(), 0, object);
3438 return hr;
3441 TRACE("Created hull shader %p.\n", object);
3442 *shader = object;
3444 return WINED3D_OK;
3447 HRESULT CDECL wined3d_shader_create_ps(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3448 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3450 struct wined3d_shader *object;
3451 HRESULT hr;
3453 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3454 device, desc, parent, parent_ops, shader);
3456 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3457 if (!object)
3458 return E_OUTOFMEMORY;
3460 if (FAILED(hr = pixel_shader_init(object, device, desc, parent, parent_ops)))
3462 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3463 HeapFree(GetProcessHeap(), 0, object);
3464 return hr;
3467 TRACE("Created pixel shader %p.\n", object);
3468 *shader = object;
3470 return WINED3D_OK;
3473 HRESULT CDECL wined3d_shader_create_vs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3474 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3476 struct wined3d_shader *object;
3477 HRESULT hr;
3479 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3480 device, desc, parent, parent_ops, shader);
3482 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3483 if (!object)
3484 return E_OUTOFMEMORY;
3486 if (FAILED(hr = vertex_shader_init(object, device, desc, parent, parent_ops)))
3488 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
3489 HeapFree(GetProcessHeap(), 0, object);
3490 return hr;
3493 TRACE("Created vertex shader %p.\n", object);
3494 *shader = object;
3496 return WINED3D_OK;