oleaut32: Use VariantInit() rather than open coding it.
[wine.git] / dlls / wined3d / shader.c
blob3681a8d62129eb7b27f58d242f57cc09a7cd3481
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_ATOMIC_AND */ "atomic_and",
47 /* WINED3DSIH_ATOMIC_CMP_STORE */ "atomic_cmp_store",
48 /* WINED3DSIH_ATOMIC_IADD */ "atomic_iadd",
49 /* WINED3DSIH_ATOMIC_IMAX */ "atomic_imax",
50 /* WINED3DSIH_ATOMIC_IMIN */ "atomic_imin",
51 /* WINED3DSIH_ATOMIC_OR */ "atomic_or",
52 /* WINED3DSIH_ATOMIC_UMAX */ "atomic_umax",
53 /* WINED3DSIH_ATOMIC_UMIN */ "atomic_umin",
54 /* WINED3DSIH_ATOMIC_XOR */ "atomic_xor",
55 /* WINED3DSIH_BEM */ "bem",
56 /* WINED3DSIH_BFI */ "bfi",
57 /* WINED3DSIH_BFREV */ "bfrev",
58 /* WINED3DSIH_BREAK */ "break",
59 /* WINED3DSIH_BREAKC */ "breakc",
60 /* WINED3DSIH_BREAKP */ "breakp",
61 /* WINED3DSIH_BUFINFO */ "bufinfo",
62 /* WINED3DSIH_CALL */ "call",
63 /* WINED3DSIH_CALLNZ */ "callnz",
64 /* WINED3DSIH_CASE */ "case",
65 /* WINED3DSIH_CMP */ "cmp",
66 /* WINED3DSIH_CND */ "cnd",
67 /* WINED3DSIH_CONTINUE */ "continue",
68 /* WINED3DSIH_COUNTBITS */ "countbits",
69 /* WINED3DSIH_CRS */ "crs",
70 /* WINED3DSIH_CUT */ "cut",
71 /* WINED3DSIH_CUT_STREAM */ "cut_stream",
72 /* WINED3DSIH_DCL */ "dcl",
73 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
74 /* WINED3DSIH_DCL_FUNCTION_BODY */ "dcl_function_body",
75 /* WINED3DSIH_DCL_FUNCTION_TABLE */ "dcl_function_table",
76 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ "dcl_globalFlags",
77 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ "dcl_hs_fork_phase_instance_count",
78 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ "dcl_hs_join_phase_instance_count",
79 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ "dcl_hs_max_tessfactor",
80 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ "dcl_immediateConstantBuffer",
81 /* WINED3DSIH_DCL_INDEX_RANGE */ "dcl_index_range",
82 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ "dcl_indexableTemp",
83 /* WINED3DSIH_DCL_INPUT */ "dcl_input",
84 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ "dcl_input_control_point_count",
85 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
86 /* WINED3DSIH_DCL_INPUT_PS */ "dcl_input_ps",
87 /* WINED3DSIH_DCL_INPUT_PS_SGV */ "dcl_input_ps_sgv",
88 /* WINED3DSIH_DCL_INPUT_PS_SIV */ "dcl_input_ps_siv",
89 /* WINED3DSIH_DCL_INPUT_SGV */ "dcl_input_sgv",
90 /* WINED3DSIH_DCL_INPUT_SIV */ "dcl_input_siv",
91 /* WINED3DSIH_DCL_INTERFACE */ "dcl_interface",
92 /* WINED3DSIH_DCL_OUTPUT */ "dcl_output",
93 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ "dcl_output_control_point_count",
94 /* WINED3DSIH_DCL_OUTPUT_SIV */ "dcl_output_siv",
95 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
96 /* WINED3DSIH_DCL_RESOURCE_RAW */ "dcl_resource_raw",
97 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ "dcl_resource_structured",
98 /* WINED3DSIH_DCL_SAMPLER */ "dcl_sampler",
99 /* WINED3DSIH_DCL_STREAM */ "dcl_stream",
100 /* WINED3DSIH_DCL_TEMPS */ "dcl_temps",
101 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ "dcl_tessellator_domain",
102 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ "dcl_tessellator_output_primitive",
103 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ "dcl_tessellator_partitioning",
104 /* WINED3DSIH_DCL_TGSM_RAW */ "dcl_tgsm_raw",
105 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ "dcl_tgsm_structured",
106 /* WINED3DSIH_DCL_THREAD_GROUP */ "dcl_thread_group",
107 /* WINED3DSIH_DCL_UAV_RAW */ "dcl_uav_raw",
108 /* WINED3DSIH_DCL_UAV_STRUCTURED */ "dcl_uav_structured",
109 /* WINED3DSIH_DCL_UAV_TYPED */ "dcl_uav_typed",
110 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
111 /* WINED3DSIH_DEF */ "def",
112 /* WINED3DSIH_DEFAULT */ "default",
113 /* WINED3DSIH_DEFB */ "defb",
114 /* WINED3DSIH_DEFI */ "defi",
115 /* WINED3DSIH_DIV */ "div",
116 /* WINED3DSIH_DP2 */ "dp2",
117 /* WINED3DSIH_DP2ADD */ "dp2add",
118 /* WINED3DSIH_DP3 */ "dp3",
119 /* WINED3DSIH_DP4 */ "dp4",
120 /* WINED3DSIH_DST */ "dst",
121 /* WINED3DSIH_DSX */ "dsx",
122 /* WINED3DSIH_DSX_COARSE */ "deriv_rtx_coarse",
123 /* WINED3DSIH_DSX_FINE */ "deriv_rtx_fine",
124 /* WINED3DSIH_DSY */ "dsy",
125 /* WINED3DSIH_DSY_COARSE */ "deriv_rty_coarse",
126 /* WINED3DSIH_DSY_FINE */ "deriv_rty_fine",
127 /* WINED3DSIH_ELSE */ "else",
128 /* WINED3DSIH_EMIT */ "emit",
129 /* WINED3DSIH_EMIT_STREAM */ "emit_stream",
130 /* WINED3DSIH_ENDIF */ "endif",
131 /* WINED3DSIH_ENDLOOP */ "endloop",
132 /* WINED3DSIH_ENDREP */ "endrep",
133 /* WINED3DSIH_ENDSWITCH */ "endswitch",
134 /* WINED3DSIH_EQ */ "eq",
135 /* WINED3DSIH_EXP */ "exp",
136 /* WINED3DSIH_EXPP */ "expp",
137 /* WINED3DSIH_F16TOF32 */ "f16tof32",
138 /* WINED3DSIH_F32TOF16 */ "f32tof16",
139 /* WINED3DSIH_FCALL */ "fcall",
140 /* WINED3DSIH_FIRSTBIT_HI */ "firstbit_hi",
141 /* WINED3DSIH_FIRSTBIT_LO */ "firstbit_lo",
142 /* WINED3DSIH_FIRSTBIT_SHI */ "firstbit_shi",
143 /* WINED3DSIH_FRC */ "frc",
144 /* WINED3DSIH_FTOI */ "ftoi",
145 /* WINED3DSIH_FTOU */ "ftou",
146 /* WINED3DSIH_GATHER4 */ "gather4",
147 /* WINED3DSIH_GATHER4_C */ "gather4_c",
148 /* WINED3DSIH_GATHER4_PO */ "gather4_po",
149 /* WINED3DSIH_GE */ "ge",
150 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ "hs_control_point_phase",
151 /* WINED3DSIH_HS_DECLS */ "hs_decls",
152 /* WINED3DSIH_HS_FORK_PHASE */ "hs_fork_phase",
153 /* WINED3DSIH_HS_JOIN_PHASE */ "hs_join_phase",
154 /* WINED3DSIH_IADD */ "iadd",
155 /* WINED3DSIH_IBFE */ "ibfe",
156 /* WINED3DSIH_IEQ */ "ieq",
157 /* WINED3DSIH_IF */ "if",
158 /* WINED3DSIH_IFC */ "ifc",
159 /* WINED3DSIH_IGE */ "ige",
160 /* WINED3DSIH_ILT */ "ilt",
161 /* WINED3DSIH_IMAD */ "imad",
162 /* WINED3DSIH_IMAX */ "imax",
163 /* WINED3DSIH_IMIN */ "imin",
164 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ "imm_atomic_alloc",
165 /* WINED3DSIH_IMM_ATOMIC_AND */ "imm_atomic_and",
166 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ "imm_atomic_cmp_exch",
167 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ "imm_atomic_consume",
168 /* WINED3DSIH_IMM_ATOMIC_EXCH */ "imm_atomic_exch",
169 /* WINED3DSIH_IMM_ATOMIC_IADD */ "imm_atomic_iadd",
170 /* WINED3DSIH_IMM_ATOMIC_IMAX */ "imm_atomic_imax",
171 /* WINED3DSIH_IMM_ATOMIC_IMIN */ "imm_atomic_imin",
172 /* WINED3DSIH_IMM_ATOMIC_OR */ "imm_atomic_or",
173 /* WINED3DSIH_IMM_ATOMIC_UMAX */ "imm_atomic_umax",
174 /* WINED3DSIH_IMM_ATOMIC_UMIN */ "imm_atomic_umin",
175 /* WINED3DSIH_IMM_ATOMIC_XOR */ "imm_atomic_xor",
176 /* WINED3DSIH_IMUL */ "imul",
177 /* WINED3DSIH_INE */ "ine",
178 /* WINED3DSIH_INEG */ "ineg",
179 /* WINED3DSIH_ISHL */ "ishl",
180 /* WINED3DSIH_ISHR */ "ishr",
181 /* WINED3DSIH_ITOF */ "itof",
182 /* WINED3DSIH_LABEL */ "label",
183 /* WINED3DSIH_LD */ "ld",
184 /* WINED3DSIH_LD2DMS */ "ld2dms",
185 /* WINED3DSIH_LD_RAW */ "ld_raw",
186 /* WINED3DSIH_LD_STRUCTURED */ "ld_structured",
187 /* WINED3DSIH_LD_UAV_TYPED */ "ld_uav_typed",
188 /* WINED3DSIH_LIT */ "lit",
189 /* WINED3DSIH_LOD */ "lod",
190 /* WINED3DSIH_LOG */ "log",
191 /* WINED3DSIH_LOGP */ "logp",
192 /* WINED3DSIH_LOOP */ "loop",
193 /* WINED3DSIH_LRP */ "lrp",
194 /* WINED3DSIH_LT */ "lt",
195 /* WINED3DSIH_M3x2 */ "m3x2",
196 /* WINED3DSIH_M3x3 */ "m3x3",
197 /* WINED3DSIH_M3x4 */ "m3x4",
198 /* WINED3DSIH_M4x3 */ "m4x3",
199 /* WINED3DSIH_M4x4 */ "m4x4",
200 /* WINED3DSIH_MAD */ "mad",
201 /* WINED3DSIH_MAX */ "max",
202 /* WINED3DSIH_MIN */ "min",
203 /* WINED3DSIH_MOV */ "mov",
204 /* WINED3DSIH_MOVA */ "mova",
205 /* WINED3DSIH_MOVC */ "movc",
206 /* WINED3DSIH_MUL */ "mul",
207 /* WINED3DSIH_NE */ "ne",
208 /* WINED3DSIH_NOP */ "nop",
209 /* WINED3DSIH_NOT */ "not",
210 /* WINED3DSIH_NRM */ "nrm",
211 /* WINED3DSIH_OR */ "or",
212 /* WINED3DSIH_PHASE */ "phase",
213 /* WINED3DSIH_POW */ "pow",
214 /* WINED3DSIH_RCP */ "rcp",
215 /* WINED3DSIH_REP */ "rep",
216 /* WINED3DSIH_RESINFO */ "resinfo",
217 /* WINED3DSIH_RET */ "ret",
218 /* WINED3DSIH_RETP */ "retp",
219 /* WINED3DSIH_ROUND_NE */ "round_ne",
220 /* WINED3DSIH_ROUND_NI */ "round_ni",
221 /* WINED3DSIH_ROUND_PI */ "round_pi",
222 /* WINED3DSIH_ROUND_Z */ "round_z",
223 /* WINED3DSIH_RSQ */ "rsq",
224 /* WINED3DSIH_SAMPLE */ "sample",
225 /* WINED3DSIH_SAMPLE_B */ "sample_b",
226 /* WINED3DSIH_SAMPLE_C */ "sample_c",
227 /* WINED3DSIH_SAMPLE_C_LZ */ "sample_c_lz",
228 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
229 /* WINED3DSIH_SAMPLE_INFO */ "sample_info",
230 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
231 /* WINED3DSIH_SAMPLE_POS */ "sample_pos",
232 /* WINED3DSIH_SETP */ "setp",
233 /* WINED3DSIH_SGE */ "sge",
234 /* WINED3DSIH_SGN */ "sgn",
235 /* WINED3DSIH_SINCOS */ "sincos",
236 /* WINED3DSIH_SLT */ "slt",
237 /* WINED3DSIH_SQRT */ "sqrt",
238 /* WINED3DSIH_STORE_RAW */ "store_raw",
239 /* WINED3DSIH_STORE_STRUCTURED */ "store_structured",
240 /* WINED3DSIH_STORE_UAV_TYPED */ "store_uav_typed",
241 /* WINED3DSIH_SUB */ "sub",
242 /* WINED3DSIH_SWAPC */ "swapc",
243 /* WINED3DSIH_SWITCH */ "switch",
244 /* WINED3DSIH_SYNC */ "sync",
245 /* WINED3DSIH_TEX */ "texld",
246 /* WINED3DSIH_TEXBEM */ "texbem",
247 /* WINED3DSIH_TEXBEML */ "texbeml",
248 /* WINED3DSIH_TEXCOORD */ "texcrd",
249 /* WINED3DSIH_TEXDEPTH */ "texdepth",
250 /* WINED3DSIH_TEXDP3 */ "texdp3",
251 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
252 /* WINED3DSIH_TEXKILL */ "texkill",
253 /* WINED3DSIH_TEXLDD */ "texldd",
254 /* WINED3DSIH_TEXLDL */ "texldl",
255 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
256 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
257 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
258 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
259 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
260 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
261 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
262 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
263 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
264 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
265 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
266 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
267 /* WINED3DSIH_UBFE */ "ubfe",
268 /* WINED3DSIH_UDIV */ "udiv",
269 /* WINED3DSIH_UGE */ "uge",
270 /* WINED3DSIH_ULT */ "ult",
271 /* WINED3DSIH_UMAX */ "umax",
272 /* WINED3DSIH_UMIN */ "umin",
273 /* WINED3DSIH_UMUL */ "umul",
274 /* WINED3DSIH_USHR */ "ushr",
275 /* WINED3DSIH_UTOF */ "utof",
276 /* WINED3DSIH_XOR */ "xor",
279 static const char * const semantic_names[] =
281 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
282 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
283 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
284 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
285 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
286 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
287 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
288 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
289 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
290 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
291 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
292 /* WINED3D_DECL_USAGE_FOG */ "FOG",
293 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
294 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
297 static const struct
299 enum wined3d_sysval_semantic sysval_semantic;
300 const char *sysval_name;
302 sysval_semantic_names[] =
304 {WINED3D_SV_POSITION, "SV_Position"},
305 {WINED3D_SV_CLIP_DISTANCE, "SV_ClipDistance"},
306 {WINED3D_SV_CULL_DISTANCE, "SV_CullDistance"},
307 {WINED3D_SV_RENDER_TARGET_ARRAY_INDEX, "SV_RenderTargetArrayIndex"},
308 {WINED3D_SV_VIEWPORT_ARRAY_INDEX, "SV_ViewportArrayIndex"},
309 {WINED3D_SV_VERTEX_ID, "SV_VertexID"},
310 {WINED3D_SV_INSTANCE_ID, "SV_InstanceID"},
311 {WINED3D_SV_PRIMITIVE_ID, "SV_PrimitiveID"},
312 {WINED3D_SV_IS_FRONT_FACE, "SV_IsFrontFace"},
313 {WINED3D_SV_SAMPLE_INDEX, "SV_SampleIndex"},
314 {WINED3D_SV_QUAD_U0_TESS_FACTOR, "finalQuadUeq0EdgeTessFactor"},
315 {WINED3D_SV_QUAD_V0_TESS_FACTOR, "finalQuadVeq0EdgeTessFactor"},
316 {WINED3D_SV_QUAD_U1_TESS_FACTOR, "finalQuadUeq1EdgeTessFactor"},
317 {WINED3D_SV_QUAD_V1_TESS_FACTOR, "finalQuadVeq1EdgeTessFactor"},
318 {WINED3D_SV_QUAD_U_INNER_TESS_FACTOR, "finalQuadUInsideTessFactor"},
319 {WINED3D_SV_QUAD_V_INNER_TESS_FACTOR, "finalQuadVInsideTessFactor"},
320 {WINED3D_SV_TRIANGLE_U_TESS_FACTOR, "finalTriUeq0EdgeTessFactor"},
321 {WINED3D_SV_TRIANGLE_V_TESS_FACTOR, "finalTriVeq0EdgeTessFactor"},
322 {WINED3D_SV_TRIANGLE_W_TESS_FACTOR, "finalTriWeq0EdgeTessFactor"},
323 {WINED3D_SV_TRIANGLE_INNER_TESS_FACTOR, "finalTriInsideTessFactor"},
324 {WINED3D_SV_LINE_DETAIL_TESS_FACTOR, "finalLineDetailTessFactor"},
325 {WINED3D_SV_LINE_DENSITY_TESS_FACTOR, "finalLineDensityTessFactor"},
328 static void shader_dump_src_param(struct wined3d_string_buffer *buffer,
329 const struct wined3d_shader_src_param *param, const struct wined3d_shader_version *shader_version);
331 const char *debug_d3dshaderinstructionhandler(enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx)
333 if (handler_idx >= ARRAY_SIZE(shader_opcode_names))
334 return wine_dbg_sprintf("UNRECOGNIZED(%#x)", handler_idx);
336 return shader_opcode_names[handler_idx];
339 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage)
341 if (usage >= ARRAY_SIZE(semantic_names))
343 FIXME("Unrecognized usage %#x.\n", usage);
344 return "UNRECOGNIZED";
347 return semantic_names[usage];
350 static enum wined3d_decl_usage shader_usage_from_semantic_name(const char *name)
352 unsigned int i;
354 for (i = 0; i < ARRAY_SIZE(semantic_names); ++i)
356 if (!strcmp(name, semantic_names[i]))
357 return i;
360 return ~0U;
363 static enum wined3d_sysval_semantic shader_sysval_semantic_from_usage(enum wined3d_decl_usage usage)
365 switch (usage)
367 case WINED3D_DECL_USAGE_POSITION:
368 return WINED3D_SV_POSITION;
369 default:
370 return 0;
374 BOOL shader_match_semantic(const char *semantic_name, enum wined3d_decl_usage usage)
376 return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
379 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
380 const struct wined3d_shader_semantic *s)
382 e->semantic_name = shader_semantic_name_from_usage(s->usage);
383 e->semantic_idx = s->usage_idx;
384 e->stream_idx = 0;
385 e->sysval_semantic = shader_sysval_semantic_from_usage(s->usage);
386 e->component_type = WINED3D_TYPE_FLOAT;
387 e->register_idx = s->reg.reg.idx[0].offset;
388 e->mask = s->reg.write_mask;
391 static void shader_signature_from_usage(struct wined3d_shader_signature_element *e,
392 enum wined3d_decl_usage usage, UINT usage_idx, UINT reg_idx, DWORD write_mask)
394 e->semantic_name = shader_semantic_name_from_usage(usage);
395 e->semantic_idx = usage_idx;
396 e->stream_idx = 0;
397 e->sysval_semantic = shader_sysval_semantic_from_usage(usage);
398 e->component_type = WINED3D_TYPE_FLOAT;
399 e->register_idx = reg_idx;
400 e->mask = write_mask;
403 static const struct wined3d_shader_frontend *shader_select_frontend(enum wined3d_shader_byte_code_format format)
405 switch (format)
407 case WINED3D_SHADER_BYTE_CODE_FORMAT_SM1:
408 return &sm1_shader_frontend;
410 case WINED3D_SHADER_BYTE_CODE_FORMAT_SM4:
411 return &sm4_shader_frontend;
413 default:
414 WARN("Invalid byte code format %#x specified.\n", format);
415 return NULL;
419 void string_buffer_clear(struct wined3d_string_buffer *buffer)
421 buffer->buffer[0] = '\0';
422 buffer->content_size = 0;
425 BOOL string_buffer_init(struct wined3d_string_buffer *buffer)
427 buffer->buffer_size = 32;
428 if (!(buffer->buffer = HeapAlloc(GetProcessHeap(), 0, buffer->buffer_size)))
430 ERR("Failed to allocate shader buffer memory.\n");
431 return FALSE;
434 string_buffer_clear(buffer);
435 return TRUE;
438 void string_buffer_free(struct wined3d_string_buffer *buffer)
440 HeapFree(GetProcessHeap(), 0, buffer->buffer);
443 BOOL string_buffer_resize(struct wined3d_string_buffer *buffer, int rc)
445 char *new_buffer;
446 unsigned int new_buffer_size = buffer->buffer_size * 2;
448 while (rc > 0 && (unsigned int)rc >= new_buffer_size - buffer->content_size)
449 new_buffer_size *= 2;
450 if (!(new_buffer = HeapReAlloc(GetProcessHeap(), 0, buffer->buffer, new_buffer_size)))
452 ERR("Failed to grow buffer.\n");
453 buffer->buffer[buffer->content_size] = '\0';
454 return FALSE;
456 buffer->buffer = new_buffer;
457 buffer->buffer_size = new_buffer_size;
458 return TRUE;
461 int shader_vaddline(struct wined3d_string_buffer *buffer, const char *format, va_list args)
463 unsigned int rem;
464 int rc;
466 rem = buffer->buffer_size - buffer->content_size;
467 rc = vsnprintf(&buffer->buffer[buffer->content_size], rem, format, args);
468 if (rc < 0 /* C89 */ || (unsigned int)rc >= rem /* C99 */)
469 return rc;
471 buffer->content_size += rc;
472 return 0;
475 int shader_addline(struct wined3d_string_buffer *buffer, const char *format, ...)
477 va_list args;
478 int ret;
480 for (;;)
482 va_start(args, format);
483 ret = shader_vaddline(buffer, format, args);
484 va_end(args);
485 if (!ret)
486 return ret;
487 if (!string_buffer_resize(buffer, ret))
488 return -1;
492 struct wined3d_string_buffer *string_buffer_get(struct wined3d_string_buffer_list *list)
494 struct wined3d_string_buffer *buffer;
496 if (list_empty(&list->list))
498 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer));
499 if (!buffer || !string_buffer_init(buffer))
501 ERR("Couldn't allocate buffer for temporary string.\n");
502 HeapFree(GetProcessHeap(), 0, buffer);
503 return NULL;
506 else
508 buffer = LIST_ENTRY(list_head(&list->list), struct wined3d_string_buffer, entry);
509 list_remove(&buffer->entry);
511 string_buffer_clear(buffer);
512 return buffer;
515 static int string_buffer_vsprintf(struct wined3d_string_buffer *buffer, const char *format, va_list args)
517 if (!buffer)
518 return 0;
519 string_buffer_clear(buffer);
520 return shader_vaddline(buffer, format, args);
523 void string_buffer_sprintf(struct wined3d_string_buffer *buffer, const char *format, ...)
525 va_list args;
526 int ret;
528 for (;;)
530 va_start(args, format);
531 ret = string_buffer_vsprintf(buffer, format, args);
532 va_end(args);
533 if (!ret)
534 return;
535 if (!string_buffer_resize(buffer, ret))
536 return;
540 void string_buffer_release(struct wined3d_string_buffer_list *list, struct wined3d_string_buffer *buffer)
542 if (!buffer)
543 return;
544 list_add_head(&list->list, &buffer->entry);
547 void string_buffer_list_init(struct wined3d_string_buffer_list *list)
549 list_init(&list->list);
552 void string_buffer_list_cleanup(struct wined3d_string_buffer_list *list)
554 struct wined3d_string_buffer *buffer, *buffer_next;
556 LIST_FOR_EACH_ENTRY_SAFE(buffer, buffer_next, &list->list, struct wined3d_string_buffer, entry)
558 string_buffer_free(buffer);
559 HeapFree(GetProcessHeap(), 0, buffer);
561 list_init(&list->list);
564 /* Convert floating point offset relative to a register file to an absolute
565 * offset for float constants. */
566 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type, UINT register_idx)
568 switch (register_type)
570 case WINED3DSPR_CONST: return register_idx;
571 case WINED3DSPR_CONST2: return 2048 + register_idx;
572 case WINED3DSPR_CONST3: return 4096 + register_idx;
573 case WINED3DSPR_CONST4: return 6144 + register_idx;
574 default:
575 FIXME("Unsupported register type: %u.\n", register_type);
576 return register_idx;
580 static void shader_delete_constant_list(struct list *clist)
582 struct wined3d_shader_lconst *constant, *constant_next;
584 LIST_FOR_EACH_ENTRY_SAFE(constant, constant_next, clist, struct wined3d_shader_lconst, entry)
585 HeapFree(GetProcessHeap(), 0, constant);
586 list_init(clist);
589 static void shader_set_limits(struct wined3d_shader *shader)
591 static const struct limits_entry
593 unsigned int min_version;
594 unsigned int max_version;
595 struct wined3d_shader_limits limits;
597 vs_limits[] =
599 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
600 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
601 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
602 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
603 * even though they are capable of supporting much more (GL
604 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
605 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
606 * shaders to 256. */
607 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
608 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
609 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 0}},
612 hs_limits[] =
614 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
615 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
617 ds_limits[] =
619 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
620 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
622 gs_limits[] =
624 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
625 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
626 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
629 ps_limits[] =
631 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
632 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
633 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
634 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
635 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
636 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 10}},
637 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 32}},
640 cs_limits[] =
642 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
643 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 0}},
645 const struct limits_entry *limits_array;
646 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
647 shader->reg_maps.shader_version.minor);
648 int i = 0;
650 switch (shader->reg_maps.shader_version.type)
652 default:
653 FIXME("Unexpected shader type %u found.\n", shader->reg_maps.shader_version.type);
654 /* Fall-through. */
655 case WINED3D_SHADER_TYPE_VERTEX:
656 limits_array = vs_limits;
657 break;
658 case WINED3D_SHADER_TYPE_HULL:
659 limits_array = hs_limits;
660 break;
661 case WINED3D_SHADER_TYPE_DOMAIN:
662 limits_array = ds_limits;
663 break;
664 case WINED3D_SHADER_TYPE_GEOMETRY:
665 limits_array = gs_limits;
666 break;
667 case WINED3D_SHADER_TYPE_PIXEL:
668 limits_array = ps_limits;
669 break;
670 case WINED3D_SHADER_TYPE_COMPUTE:
671 limits_array = cs_limits;
672 break;
675 while (limits_array[i].min_version && limits_array[i].min_version <= shader_version)
677 if (shader_version <= limits_array[i].max_version)
679 shader->limits = &limits_array[i].limits;
680 break;
682 ++i;
684 if (!shader->limits)
686 FIXME("Unexpected shader version \"%u.%u\".\n",
687 shader->reg_maps.shader_version.major,
688 shader->reg_maps.shader_version.minor);
689 shader->limits = &limits_array[max(0, i - 1)].limits;
693 static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
695 DWORD idx, shift;
696 idx = bit >> 5;
697 shift = bit & 0x1f;
698 bitmap[idx] |= (1u << shift);
701 static BOOL shader_record_register_usage(struct wined3d_shader *shader, struct wined3d_shader_reg_maps *reg_maps,
702 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type, unsigned int constf_size)
704 switch (reg->type)
706 case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
707 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
708 reg_maps->texcoord |= 1u << reg->idx[0].offset;
709 else
710 reg_maps->address |= 1u << reg->idx[0].offset;
711 break;
713 case WINED3DSPR_TEMP:
714 reg_maps->temporary |= 1u << reg->idx[0].offset;
715 break;
717 case WINED3DSPR_INPUT:
718 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
720 /* If relative addressing is used, we must assume that all
721 * registers are used. Even if it is a construct like v3[aL],
722 * we can't assume that v0, v1 and v2 aren't read because aL
723 * can be negative. */
724 if (reg->idx[0].rel_addr)
725 shader->u.ps.input_reg_used = ~0u;
726 else
727 shader->u.ps.input_reg_used |= 1u << reg->idx[0].offset;
729 else
730 reg_maps->input_registers |= 1u << reg->idx[0].offset;
731 break;
733 case WINED3DSPR_RASTOUT:
734 if (reg->idx[0].offset == 1)
735 reg_maps->fog = 1;
736 if (reg->idx[0].offset == 2)
737 reg_maps->point_size = 1;
738 break;
740 case WINED3DSPR_MISCTYPE:
741 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
743 if (!reg->idx[0].offset)
744 reg_maps->vpos = 1;
745 else if (reg->idx[0].offset == 1)
746 reg_maps->usesfacing = 1;
748 break;
750 case WINED3DSPR_CONST:
751 if (reg->idx[0].rel_addr)
753 if (reg->idx[0].offset < reg_maps->min_rel_offset)
754 reg_maps->min_rel_offset = reg->idx[0].offset;
755 if (reg->idx[0].offset > reg_maps->max_rel_offset)
756 reg_maps->max_rel_offset = reg->idx[0].offset;
757 reg_maps->usesrelconstF = TRUE;
759 else
761 if (reg->idx[0].offset >= min(shader->limits->constant_float, constf_size))
763 WARN("Shader using float constant %u which is not supported.\n", reg->idx[0].offset);
764 return FALSE;
766 else
768 set_bitmap_bit(reg_maps->constf, reg->idx[0].offset);
771 break;
773 case WINED3DSPR_CONSTINT:
774 if (reg->idx[0].offset >= shader->limits->constant_int)
776 WARN("Shader using integer constant %u which is not supported.\n", reg->idx[0].offset);
777 return FALSE;
779 else
781 reg_maps->integer_constants |= (1u << reg->idx[0].offset);
783 break;
785 case WINED3DSPR_CONSTBOOL:
786 if (reg->idx[0].offset >= shader->limits->constant_bool)
788 WARN("Shader using bool constant %u which is not supported.\n", reg->idx[0].offset);
789 return FALSE;
791 else
793 reg_maps->boolean_constants |= (1u << reg->idx[0].offset);
795 break;
797 case WINED3DSPR_COLOROUT:
798 reg_maps->rt_mask |= (1u << reg->idx[0].offset);
799 break;
801 default:
802 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
803 reg->type, reg->idx[0].offset, reg->idx[1].offset);
804 break;
806 return TRUE;
809 static void shader_record_sample(struct wined3d_shader_reg_maps *reg_maps,
810 unsigned int resource_idx, unsigned int sampler_idx, unsigned int bind_idx)
812 struct wined3d_shader_sampler_map_entry *entries, *entry;
813 struct wined3d_shader_sampler_map *map;
814 unsigned int i;
816 map = &reg_maps->sampler_map;
817 entries = map->entries;
818 for (i = 0; i < map->count; ++i)
820 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
821 return;
824 if (!map->size)
826 if (!(entries = wined3d_calloc(4, sizeof(*entries))))
828 ERR("Failed to allocate sampler map entries.\n");
829 return;
831 map->size = 4;
832 map->entries = entries;
834 else if (map->count == map->size)
836 size_t new_size = map->size * 2;
838 if (sizeof(*entries) * new_size <= sizeof(*entries) * map->size
839 || !(entries = HeapReAlloc(GetProcessHeap(), 0, entries, sizeof(*entries) * new_size)))
841 ERR("Failed to resize sampler map entries.\n");
842 return;
844 map->size = new_size;
845 map->entries = entries;
848 entry = &entries[map->count++];
849 entry->resource_idx = resource_idx;
850 entry->sampler_idx = sampler_idx;
851 entry->bind_idx = bind_idx;
854 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
856 switch (instr)
858 case WINED3DSIH_M4x4:
859 case WINED3DSIH_M3x4:
860 return param == 1 ? 3 : 0;
862 case WINED3DSIH_M4x3:
863 case WINED3DSIH_M3x3:
864 return param == 1 ? 2 : 0;
866 case WINED3DSIH_M3x2:
867 return param == 1 ? 1 : 0;
869 default:
870 return 0;
874 static HRESULT shader_reg_maps_add_tgsm(struct wined3d_shader_reg_maps *reg_maps,
875 unsigned int register_idx, unsigned int size, unsigned int stride)
877 struct wined3d_shader_tgsm *tgsm;
879 if (register_idx >= MAX_TGSM_REGISTERS)
881 ERR("Invalid TGSM register index %u.\n", register_idx);
882 return S_OK;
884 if (reg_maps->shader_version.type != WINED3D_SHADER_TYPE_COMPUTE)
886 FIXME("TGSM declarations are allowed only in compute shaders.\n");
887 return S_OK;
890 if (!wined3d_array_reserve((void **)&reg_maps->tgsm, &reg_maps->tgsm_capacity,
891 register_idx + 1, sizeof(*reg_maps->tgsm)))
892 return E_OUTOFMEMORY;
894 reg_maps->tgsm_count = max(register_idx + 1, reg_maps->tgsm_count);
895 tgsm = &reg_maps->tgsm[register_idx];
896 tgsm->size = size;
897 tgsm->stride = stride;
898 return S_OK;
901 /* Note that this does not count the loop register as an address register. */
902 static HRESULT shader_get_registers_used(struct wined3d_shader *shader, const struct wined3d_shader_frontend *fe,
903 struct wined3d_shader_reg_maps *reg_maps, struct wined3d_shader_signature *input_signature,
904 struct wined3d_shader_signature *output_signature, DWORD constf_size)
906 struct wined3d_shader_signature_element input_signature_elements[max(MAX_ATTRIBS, MAX_REG_INPUT)];
907 struct wined3d_shader_signature_element output_signature_elements[MAX_REG_OUTPUT];
908 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
909 void *fe_data = shader->frontend_data;
910 struct wined3d_shader_version shader_version;
911 const DWORD *ptr;
912 unsigned int i;
913 HRESULT hr;
915 memset(reg_maps, 0, sizeof(*reg_maps));
916 memset(input_signature_elements, 0, sizeof(input_signature_elements));
917 memset(output_signature_elements, 0, sizeof(output_signature_elements));
918 reg_maps->min_rel_offset = ~0U;
919 list_init(&reg_maps->indexable_temps);
921 fe->shader_read_header(fe_data, &ptr, &shader_version);
922 reg_maps->shader_version = shader_version;
924 shader_set_limits(shader);
926 if (!(reg_maps->constf = wined3d_calloc(((min(shader->limits->constant_float, constf_size) + 31) / 32),
927 sizeof(*reg_maps->constf))))
929 ERR("Failed to allocate constant map memory.\n");
930 return E_OUTOFMEMORY;
933 while (!fe->shader_is_end(fe_data, &ptr))
935 struct wined3d_shader_instruction ins;
937 /* Fetch opcode. */
938 fe->shader_read_instruction(fe_data, &ptr, &ins);
940 /* Unhandled opcode, and its parameters. */
941 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
943 WARN("Encountered unrecognised or invalid instruction.\n");
944 return WINED3DERR_INVALIDCALL;
947 /* Handle declarations. */
948 if (ins.handler_idx == WINED3DSIH_DCL
949 || ins.handler_idx == WINED3DSIH_DCL_UAV_TYPED)
951 struct wined3d_shader_semantic *semantic = &ins.declaration.semantic;
952 unsigned int reg_idx = semantic->reg.reg.idx[0].offset;
954 switch (semantic->reg.reg.type)
956 /* Mark input registers used. */
957 case WINED3DSPR_INPUT:
958 if (reg_idx >= MAX_REG_INPUT)
960 ERR("Invalid input register index %u.\n", reg_idx);
961 break;
963 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL && shader_version.major == 3
964 && semantic->usage == WINED3D_DECL_USAGE_POSITION && !semantic->usage_idx)
965 return WINED3DERR_INVALIDCALL;
966 reg_maps->input_registers |= 1u << reg_idx;
967 shader_signature_from_semantic(&input_signature_elements[reg_idx], semantic);
968 break;
970 /* Vertex shader: mark 3.0 output registers used, save token. */
971 case WINED3DSPR_OUTPUT:
972 if (reg_idx >= MAX_REG_OUTPUT)
974 ERR("Invalid output register index %u.\n", reg_idx);
975 break;
977 reg_maps->output_registers |= 1u << reg_idx;
978 shader_signature_from_semantic(&output_signature_elements[reg_idx], semantic);
979 if (semantic->usage == WINED3D_DECL_USAGE_FOG)
980 reg_maps->fog = 1;
981 if (semantic->usage == WINED3D_DECL_USAGE_PSIZE)
982 reg_maps->point_size = 1;
983 break;
985 case WINED3DSPR_SAMPLER:
986 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
987 case WINED3DSPR_RESOURCE:
988 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
990 ERR("Invalid resource index %u.\n", reg_idx);
991 break;
993 reg_maps->resource_info[reg_idx].type = semantic->resource_type;
994 reg_maps->resource_info[reg_idx].data_type = semantic->resource_data_type;
995 break;
997 case WINED3DSPR_UAV:
998 if (reg_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
1000 ERR("Invalid UAV resource index %u.\n", reg_idx);
1001 break;
1003 reg_maps->uav_resource_info[reg_idx].type = semantic->resource_type;
1004 reg_maps->uav_resource_info[reg_idx].data_type = semantic->resource_data_type;
1005 if (ins.flags)
1006 FIXME("Ignoring typed UAV flags %#x.\n", ins.flags);
1007 break;
1009 default:
1010 TRACE("Not recording DCL register type %#x.\n", semantic->reg.reg.type);
1011 break;
1014 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
1016 struct wined3d_shader_register *reg = &ins.declaration.src.reg;
1017 if (reg->idx[0].offset >= WINED3D_MAX_CBS)
1018 ERR("Invalid CB index %u.\n", reg->idx[0].offset);
1019 else
1020 reg_maps->cb_sizes[reg->idx[0].offset] = reg->idx[1].offset;
1022 else if (ins.handler_idx == WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER)
1024 if (reg_maps->icb)
1025 FIXME("Multiple immediate constant buffers.\n");
1026 reg_maps->icb = ins.declaration.icb;
1028 else if (ins.handler_idx == WINED3DSIH_DCL_INDEXABLE_TEMP)
1030 struct wined3d_shader_indexable_temp *reg;
1032 if (!(reg = HeapAlloc(GetProcessHeap(), 0, sizeof(*reg))))
1033 return E_OUTOFMEMORY;
1035 *reg = ins.declaration.indexable_temp;
1036 list_add_tail(&reg_maps->indexable_temps, &reg->entry);
1038 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE)
1040 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1041 shader->u.gs.input_type = ins.declaration.primitive_type;
1042 else
1043 FIXME("Invalid instruction %#x for shader type %#x.\n",
1044 ins.handler_idx, shader_version.type);
1046 else if (ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
1048 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1049 shader->u.gs.output_type = ins.declaration.primitive_type;
1050 else
1051 FIXME("Invalid instruction %#x for shader type %#x.\n",
1052 ins.handler_idx, shader_version.type);
1054 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_RAW)
1056 unsigned int reg_idx = ins.declaration.dst.reg.idx[0].offset;
1057 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
1059 ERR("Invalid resource index %u.\n", reg_idx);
1060 break;
1062 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_BUFFER;
1063 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1064 reg_maps->resource_info[reg_idx].flags = WINED3D_VIEW_BUFFER_RAW;
1066 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_STRUCTURED)
1068 unsigned int reg_idx = ins.declaration.structured_resource.reg.reg.idx[0].offset;
1069 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
1071 ERR("Invalid resource index %u.\n", reg_idx);
1072 break;
1074 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_BUFFER;
1075 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1076 reg_maps->resource_info[reg_idx].flags = 0;
1077 reg_maps->resource_info[reg_idx].stride = ins.declaration.structured_resource.byte_stride / 4;
1079 else if (ins.handler_idx == WINED3DSIH_DCL_SAMPLER)
1081 if (ins.flags & WINED3DSI_SAMPLER_COMPARISON_MODE)
1082 reg_maps->sampler_comparison_mode |= (1u << ins.declaration.dst.reg.idx[0].offset);
1084 else if (ins.handler_idx == WINED3DSIH_DCL_TEMPS)
1086 reg_maps->temporary_count = ins.declaration.count;
1088 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_RAW)
1090 if (FAILED(hr = shader_reg_maps_add_tgsm(reg_maps, ins.declaration.tgsm_raw.reg.reg.idx[0].offset,
1091 ins.declaration.tgsm_raw.byte_count / 4, 0)))
1092 return hr;
1094 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_STRUCTURED)
1096 unsigned int stride = ins.declaration.tgsm_structured.byte_stride / 4;
1097 unsigned int size = stride * ins.declaration.tgsm_structured.structure_count;
1098 if (FAILED(hr = shader_reg_maps_add_tgsm(reg_maps,
1099 ins.declaration.tgsm_structured.reg.reg.idx[0].offset, size, stride)))
1100 return hr;
1102 else if (ins.handler_idx == WINED3DSIH_DCL_THREAD_GROUP)
1104 if (shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
1106 shader->u.cs.thread_group_size = ins.declaration.thread_group_size;
1108 else
1110 FIXME("Invalid instruction %#x for shader type %#x.\n",
1111 ins.handler_idx, shader_version.type);
1114 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_RAW)
1116 unsigned int reg_idx = ins.declaration.dst.reg.idx[0].offset;
1117 if (reg_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
1119 ERR("Invalid UAV resource index %u.\n", reg_idx);
1120 break;
1122 if (ins.flags)
1123 FIXME("Ignoring raw UAV flags %#x.\n", ins.flags);
1124 reg_maps->uav_resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_BUFFER;
1125 reg_maps->uav_resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1126 reg_maps->uav_resource_info[reg_idx].flags = WINED3D_VIEW_BUFFER_RAW;
1128 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_STRUCTURED)
1130 unsigned int reg_idx = ins.declaration.structured_resource.reg.reg.idx[0].offset;
1131 if (reg_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
1133 ERR("Invalid UAV resource index %u.\n", reg_idx);
1134 break;
1136 if (ins.flags)
1137 FIXME("Ignoring structured UAV flags %#x.\n", ins.flags);
1138 reg_maps->uav_resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_BUFFER;
1139 reg_maps->uav_resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1140 reg_maps->uav_resource_info[reg_idx].flags = 0;
1141 reg_maps->uav_resource_info[reg_idx].stride = ins.declaration.structured_resource.byte_stride / 4;
1143 else if (ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
1145 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1146 shader->u.gs.vertices_out = ins.declaration.count;
1147 else
1148 FIXME("Invalid instruction %#x for shader type %#x.\n",
1149 ins.handler_idx, shader_version.type);
1151 else if (ins.handler_idx == WINED3DSIH_DEF)
1153 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1154 float *value;
1155 if (!lconst) return E_OUTOFMEMORY;
1157 lconst->idx = ins.dst[0].reg.idx[0].offset;
1158 memcpy(lconst->value, ins.src[0].reg.u.immconst_data, 4 * sizeof(DWORD));
1159 value = (float *)lconst->value;
1161 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
1162 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1164 if (value[0] < -1.0f) value[0] = -1.0f;
1165 else if (value[0] > 1.0f) value[0] = 1.0f;
1166 if (value[1] < -1.0f) value[1] = -1.0f;
1167 else if (value[1] > 1.0f) value[1] = 1.0f;
1168 if (value[2] < -1.0f) value[2] = -1.0f;
1169 else if (value[2] > 1.0f) value[2] = 1.0f;
1170 if (value[3] < -1.0f) value[3] = -1.0f;
1171 else if (value[3] > 1.0f) value[3] = 1.0f;
1174 list_add_head(&shader->constantsF, &lconst->entry);
1176 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
1177 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
1179 shader->lconst_inf_or_nan = TRUE;
1182 else if (ins.handler_idx == WINED3DSIH_DEFI)
1184 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1185 if (!lconst) return E_OUTOFMEMORY;
1187 lconst->idx = ins.dst[0].reg.idx[0].offset;
1188 memcpy(lconst->value, ins.src[0].reg.u.immconst_data, 4 * sizeof(DWORD));
1190 list_add_head(&shader->constantsI, &lconst->entry);
1191 reg_maps->local_int_consts |= (1u << lconst->idx);
1193 else if (ins.handler_idx == WINED3DSIH_DEFB)
1195 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1196 if (!lconst) return E_OUTOFMEMORY;
1198 lconst->idx = ins.dst[0].reg.idx[0].offset;
1199 memcpy(lconst->value, ins.src[0].reg.u.immconst_data, sizeof(DWORD));
1201 list_add_head(&shader->constantsB, &lconst->entry);
1202 reg_maps->local_bool_consts |= (1u << lconst->idx);
1204 /* For subroutine prototypes. */
1205 else if (ins.handler_idx == WINED3DSIH_LABEL)
1207 reg_maps->labels |= 1u << ins.src[0].reg.idx[0].offset;
1209 /* Set texture, address, temporary registers. */
1210 else
1212 BOOL color0_mov = FALSE;
1213 unsigned int i;
1215 /* This will loop over all the registers and try to
1216 * make a bitmask of the ones we're interested in.
1218 * Relative addressing tokens are ignored, but that's
1219 * okay, since we'll catch any address registers when
1220 * they are initialized (required by spec). */
1221 for (i = 0; i < ins.dst_count; ++i)
1223 if (!shader_record_register_usage(shader, reg_maps, &ins.dst[i].reg,
1224 shader_version.type, constf_size))
1225 return WINED3DERR_INVALIDCALL;
1227 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
1229 UINT idx = ins.dst[i].reg.idx[0].offset;
1231 switch (ins.dst[i].reg.type)
1233 case WINED3DSPR_RASTOUT:
1234 if (shader_version.major >= 3)
1235 break;
1236 switch (idx)
1238 case 0: /* oPos */
1239 reg_maps->output_registers |= 1u << 10;
1240 shader_signature_from_usage(&output_signature_elements[10],
1241 WINED3D_DECL_USAGE_POSITION, 0, 10, WINED3DSP_WRITEMASK_ALL);
1242 break;
1244 case 1: /* oFog */
1245 reg_maps->output_registers |= 1u << 11;
1246 shader_signature_from_usage(&output_signature_elements[11],
1247 WINED3D_DECL_USAGE_FOG, 0, 11, WINED3DSP_WRITEMASK_0);
1248 break;
1250 case 2: /* oPts */
1251 reg_maps->output_registers |= 1u << 11;
1252 shader_signature_from_usage(&output_signature_elements[11],
1253 WINED3D_DECL_USAGE_PSIZE, 0, 11, WINED3DSP_WRITEMASK_1);
1254 break;
1256 break;
1258 case WINED3DSPR_ATTROUT:
1259 if (shader_version.major >= 3)
1260 break;
1261 if (idx < 2)
1263 idx += 8;
1264 if (reg_maps->output_registers & (1u << idx))
1266 output_signature_elements[idx].mask |= ins.dst[i].write_mask;
1268 else
1270 reg_maps->output_registers |= 1u << idx;
1271 shader_signature_from_usage(&output_signature_elements[idx],
1272 WINED3D_DECL_USAGE_COLOR, idx - 8, idx, ins.dst[i].write_mask);
1275 break;
1277 case WINED3DSPR_TEXCRDOUT:
1278 if (shader_version.major >= 3)
1280 if (idx >= ARRAY_SIZE(reg_maps->u.output_registers_mask))
1282 WARN("Invalid output register index %u.\n", idx);
1283 break;
1285 reg_maps->u.output_registers_mask[idx] |= ins.dst[i].write_mask;
1286 break;
1288 if (idx >= ARRAY_SIZE(reg_maps->u.texcoord_mask))
1290 WARN("Invalid texcoord index %u.\n", idx);
1291 break;
1293 reg_maps->u.texcoord_mask[idx] |= ins.dst[i].write_mask;
1294 if (reg_maps->output_registers & (1u << idx))
1296 output_signature_elements[idx].mask |= ins.dst[i].write_mask;
1298 else
1300 reg_maps->output_registers |= 1u << idx;
1301 shader_signature_from_usage(&output_signature_elements[idx],
1302 WINED3D_DECL_USAGE_TEXCOORD, idx, idx, ins.dst[i].write_mask);
1304 break;
1306 default:
1307 break;
1311 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1313 if (ins.dst[i].reg.type == WINED3DSPR_COLOROUT && !ins.dst[i].reg.idx[0].offset)
1315 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
1316 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
1317 * the mov and perform the sRGB write correction from the source register.
1319 * However, if the mov is only partial, we can't do this, and if the write
1320 * comes from an instruction other than MOV it is hard to do as well. If
1321 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
1322 shader->u.ps.color0_mov = FALSE;
1323 if (ins.handler_idx == WINED3DSIH_MOV
1324 && ins.dst[i].write_mask == WINED3DSP_WRITEMASK_ALL)
1326 /* Used later when the source register is read. */
1327 color0_mov = TRUE;
1330 /* Also drop the MOV marker if the source register is overwritten prior to the shader
1331 * end
1333 else if (ins.dst[i].reg.type == WINED3DSPR_TEMP
1334 && ins.dst[i].reg.idx[0].offset == shader->u.ps.color0_reg)
1336 shader->u.ps.color0_mov = FALSE;
1340 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
1341 if (shader_version.major == 1
1342 && (ins.handler_idx == WINED3DSIH_TEX
1343 || ins.handler_idx == WINED3DSIH_TEXBEM
1344 || ins.handler_idx == WINED3DSIH_TEXBEML
1345 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
1346 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
1347 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
1348 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
1349 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
1350 || ins.handler_idx == WINED3DSIH_TEXREG2AR
1351 || ins.handler_idx == WINED3DSIH_TEXREG2GB
1352 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
1354 unsigned int reg_idx = ins.dst[i].reg.idx[0].offset;
1356 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
1358 WARN("Invalid 1.x sampler index %u.\n", reg_idx);
1359 continue;
1362 TRACE("Setting fake 2D resource for 1.x pixelshader.\n");
1363 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
1364 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_FLOAT;
1365 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
1367 /* texbem is only valid with < 1.4 pixel shaders */
1368 if (ins.handler_idx == WINED3DSIH_TEXBEM
1369 || ins.handler_idx == WINED3DSIH_TEXBEML)
1371 reg_maps->bumpmat |= 1u << reg_idx;
1372 if (ins.handler_idx == WINED3DSIH_TEXBEML)
1374 reg_maps->luminanceparams |= 1u << reg_idx;
1378 else if (ins.handler_idx == WINED3DSIH_BEM)
1380 reg_maps->bumpmat |= 1u << ins.dst[i].reg.idx[0].offset;
1384 if (ins.handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC || ins.handler_idx == WINED3DSIH_IMM_ATOMIC_CONSUME)
1386 unsigned int reg_idx = ins.src[0].reg.idx[0].offset;
1387 if (reg_idx >= MAX_UNORDERED_ACCESS_VIEWS)
1389 ERR("Invalid UAV index %u.\n", reg_idx);
1390 break;
1392 reg_maps->uav_counter_mask |= (1u << reg_idx);
1394 else if ((WINED3DSIH_ATOMIC_AND <= ins.handler_idx && ins.handler_idx <= WINED3DSIH_ATOMIC_XOR)
1395 || (WINED3DSIH_IMM_ATOMIC_AND <= ins.handler_idx && ins.handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR)
1396 || (ins.handler_idx == WINED3DSIH_BUFINFO && ins.src[0].reg.type == WINED3DSPR_UAV)
1397 || ins.handler_idx == WINED3DSIH_LD_UAV_TYPED
1398 || (ins.handler_idx == WINED3DSIH_LD_RAW && ins.src[1].reg.type == WINED3DSPR_UAV)
1399 || (ins.handler_idx == WINED3DSIH_LD_STRUCTURED && ins.src[2].reg.type == WINED3DSPR_UAV))
1401 unsigned int reg_idx;
1402 if (ins.handler_idx == WINED3DSIH_LD_UAV_TYPED || ins.handler_idx == WINED3DSIH_LD_RAW)
1403 reg_idx = ins.src[1].reg.idx[0].offset;
1404 else if (ins.handler_idx == WINED3DSIH_LD_STRUCTURED)
1405 reg_idx = ins.src[2].reg.idx[0].offset;
1406 else if (WINED3DSIH_ATOMIC_AND <= ins.handler_idx && ins.handler_idx <= WINED3DSIH_ATOMIC_XOR)
1407 reg_idx = ins.dst[0].reg.idx[0].offset;
1408 else if (ins.handler_idx == WINED3DSIH_BUFINFO)
1409 reg_idx = ins.src[0].reg.idx[0].offset;
1410 else
1411 reg_idx = ins.dst[1].reg.idx[0].offset;
1412 if (reg_idx >= MAX_UNORDERED_ACCESS_VIEWS)
1414 ERR("Invalid UAV index %u.\n", reg_idx);
1415 break;
1417 reg_maps->uav_read_mask |= (1u << reg_idx);
1419 else if (ins.handler_idx == WINED3DSIH_NRM)
1421 reg_maps->usesnrm = 1;
1423 else if (ins.handler_idx == WINED3DSIH_DSY
1424 || ins.handler_idx == WINED3DSIH_DSY_COARSE
1425 || ins.handler_idx == WINED3DSIH_DSY_FINE)
1427 reg_maps->usesdsy = 1;
1429 else if (ins.handler_idx == WINED3DSIH_DSX
1430 || ins.handler_idx == WINED3DSIH_DSX_COARSE
1431 || ins.handler_idx == WINED3DSIH_DSX_FINE)
1433 reg_maps->usesdsx = 1;
1435 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
1436 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
1437 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
1438 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
1439 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
1440 else if (ins.handler_idx == WINED3DSIH_POW) reg_maps->usespow = 1;
1441 else if (ins.handler_idx == WINED3DSIH_LOOP
1442 || ins.handler_idx == WINED3DSIH_REP)
1444 ++cur_loop_depth;
1445 if (cur_loop_depth > max_loop_depth)
1446 max_loop_depth = cur_loop_depth;
1448 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
1449 || ins.handler_idx == WINED3DSIH_ENDREP)
1451 --cur_loop_depth;
1453 else if (ins.handler_idx == WINED3DSIH_GATHER4
1454 || ins.handler_idx == WINED3DSIH_GATHER4_C
1455 || ins.handler_idx == WINED3DSIH_SAMPLE
1456 || ins.handler_idx == WINED3DSIH_SAMPLE_B
1457 || ins.handler_idx == WINED3DSIH_SAMPLE_C
1458 || ins.handler_idx == WINED3DSIH_SAMPLE_C_LZ
1459 || ins.handler_idx == WINED3DSIH_SAMPLE_GRAD
1460 || ins.handler_idx == WINED3DSIH_SAMPLE_LOD)
1462 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1463 ins.src[2].reg.idx[0].offset, reg_maps->sampler_map.count);
1465 else if (ins.handler_idx == WINED3DSIH_GATHER4_PO)
1467 shader_record_sample(reg_maps, ins.src[2].reg.idx[0].offset,
1468 ins.src[3].reg.idx[0].offset, reg_maps->sampler_map.count);
1470 else if (ins.handler_idx == WINED3DSIH_BUFINFO && ins.src[0].reg.type == WINED3DSPR_RESOURCE)
1472 shader_record_sample(reg_maps, ins.src[0].reg.idx[0].offset,
1473 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1475 else if (ins.handler_idx == WINED3DSIH_LD
1476 || (ins.handler_idx == WINED3DSIH_LD_RAW && ins.src[1].reg.type == WINED3DSPR_RESOURCE)
1477 || (ins.handler_idx == WINED3DSIH_RESINFO && ins.src[1].reg.type == WINED3DSPR_RESOURCE))
1479 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1480 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1482 else if (ins.handler_idx == WINED3DSIH_LD_STRUCTURED
1483 && ins.src[2].reg.type == WINED3DSPR_RESOURCE)
1485 shader_record_sample(reg_maps, ins.src[2].reg.idx[0].offset,
1486 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1489 if (ins.predicate)
1490 if (!shader_record_register_usage(shader, reg_maps, &ins.predicate->reg,
1491 shader_version.type, constf_size))
1492 return WINED3DERR_INVALIDCALL;
1494 for (i = 0; i < ins.src_count; ++i)
1496 unsigned int count = get_instr_extra_regcount(ins.handler_idx, i);
1497 struct wined3d_shader_register reg = ins.src[i].reg;
1499 if (!shader_record_register_usage(shader, reg_maps, &ins.src[i].reg,
1500 shader_version.type, constf_size))
1501 return WINED3DERR_INVALIDCALL;
1502 while (count)
1504 ++reg.idx[0].offset;
1505 if (!shader_record_register_usage(shader, reg_maps, &reg,
1506 shader_version.type, constf_size))
1507 return WINED3DERR_INVALIDCALL;
1508 --count;
1511 if (color0_mov)
1513 if (ins.src[i].reg.type == WINED3DSPR_TEMP
1514 && ins.src[i].swizzle == WINED3DSP_NOSWIZZLE)
1516 shader->u.ps.color0_mov = TRUE;
1517 shader->u.ps.color0_reg = ins.src[i].reg.idx[0].offset;
1523 reg_maps->loop_depth = max_loop_depth;
1525 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1526 * R0 is written to the render target. */
1527 if (shader_version.major < 2 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1528 reg_maps->rt_mask |= (1u << 0);
1530 if (input_signature->elements)
1532 for (i = 0; i < input_signature->element_count; ++i)
1534 reg_maps->input_registers |= 1u << input_signature->elements[i].register_idx;
1535 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1537 if (input_signature->elements[i].sysval_semantic == WINED3D_SV_POSITION)
1538 reg_maps->vpos = 1;
1539 else if (input_signature->elements[i].sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
1540 reg_maps->usesfacing = 1;
1544 else if (!input_signature->elements && reg_maps->input_registers)
1546 unsigned int count = wined3d_popcount(reg_maps->input_registers);
1547 struct wined3d_shader_signature_element *e;
1548 unsigned int i;
1550 if (!(input_signature->elements = wined3d_calloc(count, sizeof(*input_signature->elements))))
1551 return E_OUTOFMEMORY;
1552 input_signature->element_count = count;
1554 e = input_signature->elements;
1555 for (i = 0; i < ARRAY_SIZE(input_signature_elements); ++i)
1557 if (!(reg_maps->input_registers & (1u << i)))
1558 continue;
1559 input_signature_elements[i].register_idx = i;
1560 *e++ = input_signature_elements[i];
1564 if (output_signature->elements)
1566 for (i = 0; i < output_signature->element_count; ++i)
1568 reg_maps->output_registers |= 1u << output_signature->elements[i].register_idx;
1571 else if (reg_maps->output_registers)
1573 unsigned int count = wined3d_popcount(reg_maps->output_registers);
1574 struct wined3d_shader_signature_element *e;
1576 if (!(output_signature->elements = wined3d_calloc(count, sizeof(*output_signature->elements))))
1577 return E_OUTOFMEMORY;
1578 output_signature->element_count = count;
1580 e = output_signature->elements;
1581 for (i = 0; i < ARRAY_SIZE(output_signature_elements); ++i)
1583 if (!(reg_maps->output_registers & (1u << i)))
1584 continue;
1585 *e++ = output_signature_elements[i];
1589 return WINED3D_OK;
1592 static void shader_cleanup_reg_maps(struct wined3d_shader_reg_maps *reg_maps)
1594 struct wined3d_shader_indexable_temp *reg, *reg_next;
1596 HeapFree(GetProcessHeap(), 0, reg_maps->constf);
1597 HeapFree(GetProcessHeap(), 0, reg_maps->sampler_map.entries);
1599 LIST_FOR_EACH_ENTRY_SAFE(reg, reg_next, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
1600 HeapFree(GetProcessHeap(), 0, reg);
1601 list_init(&reg_maps->indexable_temps);
1603 HeapFree(GetProcessHeap(), 0, reg_maps->tgsm);
1606 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
1608 DWORD map = 1u << max;
1609 map |= map - 1;
1610 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
1612 return wined3d_log2i(map);
1615 static void shader_dump_global_flags(struct wined3d_string_buffer *buffer, DWORD global_flags)
1617 if (global_flags & WINED3DSGF_REFACTORING_ALLOWED)
1619 shader_addline(buffer, "refactoringAllowed");
1620 global_flags &= ~WINED3DSGF_REFACTORING_ALLOWED;
1621 if (global_flags)
1622 shader_addline(buffer, " | ");
1625 if (global_flags & WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS)
1627 shader_addline(buffer, "enableRawAndStructuredBuffers");
1628 global_flags &= ~WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS;
1631 if (global_flags)
1632 shader_addline(buffer, "unknown_flags(%#x)", global_flags);
1635 static void shader_dump_sync_flags(struct wined3d_string_buffer *buffer, DWORD sync_flags)
1637 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
1639 shader_addline(buffer, "_g");
1640 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
1642 if (sync_flags & WINED3DSSF_THREAD_GROUP)
1644 shader_addline(buffer, "_t");
1645 sync_flags &= ~WINED3DSSF_THREAD_GROUP;
1648 if (sync_flags)
1649 shader_addline(buffer, "_unknown_flags(%#x)", sync_flags);
1652 static void shader_dump_uav_flags(struct wined3d_string_buffer *buffer, DWORD uav_flags)
1654 if (uav_flags & WINED3DSUF_GLOBALLY_COHERENT)
1656 shader_addline(buffer, "_glc");
1657 uav_flags &= ~WINED3DSUF_GLOBALLY_COHERENT;
1659 if (uav_flags & WINED3DSUF_ORDER_PRESERVING_COUNTER)
1661 shader_addline(buffer, "_opc");
1662 uav_flags &= ~WINED3DSUF_ORDER_PRESERVING_COUNTER;
1665 if (uav_flags)
1666 shader_addline(buffer, "_unknown_flags(%#x)", uav_flags);
1669 static void shader_dump_tessellator_domain(struct wined3d_string_buffer *buffer,
1670 enum wined3d_tessellator_domain domain)
1672 switch (domain)
1674 case WINED3D_TESSELLATOR_DOMAIN_LINE:
1675 shader_addline(buffer, "line");
1676 break;
1677 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
1678 shader_addline(buffer, "triangle");
1679 break;
1680 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
1681 shader_addline(buffer, "quad");
1682 break;
1683 default:
1684 shader_addline(buffer, "unknown_tessellator_domain(%#x)", domain);
1685 break;
1689 static void shader_dump_tessellator_output_primitive(struct wined3d_string_buffer *buffer,
1690 enum wined3d_tessellator_output_primitive output_primitive)
1692 switch (output_primitive)
1694 case WINED3D_TESSELLATOR_OUTPUT_POINT:
1695 shader_addline(buffer, "point");
1696 break;
1697 case WINED3D_TESSELLATOR_OUTPUT_LINE:
1698 shader_addline(buffer, "line");
1699 break;
1700 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
1701 shader_addline(buffer, "triangle_cw");
1702 break;
1703 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
1704 shader_addline(buffer, "triangle_ccw");
1705 break;
1706 default:
1707 shader_addline(buffer, "unknown_tessellator_output_primitive(%#x)", output_primitive);
1708 break;
1712 static void shader_dump_tessellator_partitioning(struct wined3d_string_buffer *buffer,
1713 enum wined3d_tessellator_partitioning partitioning)
1715 switch (partitioning)
1717 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
1718 shader_addline(buffer, "integer");
1719 break;
1720 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
1721 shader_addline(buffer, "pow2");
1722 break;
1723 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
1724 shader_addline(buffer, "fractional_odd");
1725 break;
1726 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
1727 shader_addline(buffer, "fractional_even");
1728 break;
1729 default:
1730 shader_addline(buffer, "unknown_tessellator_partitioning(%#x)", partitioning);
1731 break;
1735 static void shader_dump_sysval_semantic(struct wined3d_string_buffer *buffer, enum wined3d_sysval_semantic semantic)
1737 unsigned int i;
1739 for (i = 0; i < ARRAY_SIZE(sysval_semantic_names); ++i)
1741 if (sysval_semantic_names[i].sysval_semantic == semantic)
1743 shader_addline(buffer, "%s", sysval_semantic_names[i].sysval_name);
1744 return;
1748 shader_addline(buffer, "unknown_sysval_semantic(%#x)", semantic);
1751 static void shader_dump_decl_usage(struct wined3d_string_buffer *buffer,
1752 const struct wined3d_shader_semantic *semantic, unsigned int flags,
1753 const struct wined3d_shader_version *shader_version)
1755 shader_addline(buffer, "dcl");
1757 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
1759 switch (semantic->resource_type)
1761 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1762 shader_addline(buffer, "_2d");
1763 break;
1765 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1766 shader_addline(buffer, "_3d");
1767 break;
1769 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1770 shader_addline(buffer, "_cube");
1771 break;
1773 default:
1774 shader_addline(buffer, "_unknown_resource_type(%#x)", semantic->resource_type);
1775 break;
1778 else if (semantic->reg.reg.type == WINED3DSPR_RESOURCE || semantic->reg.reg.type == WINED3DSPR_UAV)
1780 if (semantic->reg.reg.type == WINED3DSPR_RESOURCE)
1781 shader_addline(buffer, "_resource_");
1782 else
1783 shader_addline(buffer, "_uav_");
1784 switch (semantic->resource_type)
1786 case WINED3D_SHADER_RESOURCE_BUFFER:
1787 shader_addline(buffer, "buffer");
1788 break;
1790 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1791 shader_addline(buffer, "texture1d");
1792 break;
1794 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1795 shader_addline(buffer, "texture2d");
1796 break;
1798 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
1799 shader_addline(buffer, "texture2dms");
1800 break;
1802 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1803 shader_addline(buffer, "texture3d");
1804 break;
1806 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1807 shader_addline(buffer, "texturecube");
1808 break;
1810 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
1811 shader_addline(buffer, "texture1darray");
1812 break;
1814 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
1815 shader_addline(buffer, "texture2darray");
1816 break;
1818 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
1819 shader_addline(buffer, "texture2dmsarray");
1820 break;
1822 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
1823 shader_addline(buffer, "texturecubearray");
1824 break;
1826 default:
1827 shader_addline(buffer, "unknown");
1828 break;
1830 if (semantic->reg.reg.type == WINED3DSPR_UAV)
1831 shader_dump_uav_flags(buffer, flags);
1832 switch (semantic->resource_data_type)
1834 case WINED3D_DATA_FLOAT:
1835 shader_addline(buffer, " (float)");
1836 break;
1838 case WINED3D_DATA_INT:
1839 shader_addline(buffer, " (int)");
1840 break;
1842 case WINED3D_DATA_UINT:
1843 shader_addline(buffer, " (uint)");
1844 break;
1846 case WINED3D_DATA_UNORM:
1847 shader_addline(buffer, " (unorm)");
1848 break;
1850 case WINED3D_DATA_SNORM:
1851 shader_addline(buffer, " (snorm)");
1852 break;
1854 default:
1855 shader_addline(buffer, " (unknown)");
1856 break;
1859 else
1861 /* Pixel shaders 3.0 don't have usage semantics. */
1862 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL)
1863 return;
1864 else
1865 shader_addline(buffer, "_");
1867 switch (semantic->usage)
1869 case WINED3D_DECL_USAGE_POSITION:
1870 shader_addline(buffer, "position%u", semantic->usage_idx);
1871 break;
1873 case WINED3D_DECL_USAGE_BLEND_INDICES:
1874 shader_addline(buffer, "blend");
1875 break;
1877 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
1878 shader_addline(buffer, "weight");
1879 break;
1881 case WINED3D_DECL_USAGE_NORMAL:
1882 shader_addline(buffer, "normal%u", semantic->usage_idx);
1883 break;
1885 case WINED3D_DECL_USAGE_PSIZE:
1886 shader_addline(buffer, "psize");
1887 break;
1889 case WINED3D_DECL_USAGE_COLOR:
1890 if (!semantic->usage_idx)
1891 shader_addline(buffer, "color");
1892 else
1893 shader_addline(buffer, "specular%u", (semantic->usage_idx - 1));
1894 break;
1896 case WINED3D_DECL_USAGE_TEXCOORD:
1897 shader_addline(buffer, "texture%u", semantic->usage_idx);
1898 break;
1900 case WINED3D_DECL_USAGE_TANGENT:
1901 shader_addline(buffer, "tangent");
1902 break;
1904 case WINED3D_DECL_USAGE_BINORMAL:
1905 shader_addline(buffer, "binormal");
1906 break;
1908 case WINED3D_DECL_USAGE_TESS_FACTOR:
1909 shader_addline(buffer, "tessfactor");
1910 break;
1912 case WINED3D_DECL_USAGE_POSITIONT:
1913 shader_addline(buffer, "positionT%u", semantic->usage_idx);
1914 break;
1916 case WINED3D_DECL_USAGE_FOG:
1917 shader_addline(buffer, "fog");
1918 break;
1920 case WINED3D_DECL_USAGE_DEPTH:
1921 shader_addline(buffer, "depth");
1922 break;
1924 case WINED3D_DECL_USAGE_SAMPLE:
1925 shader_addline(buffer, "sample");
1926 break;
1928 default:
1929 shader_addline(buffer, "<unknown_semantic(%#x)>", semantic->usage);
1930 FIXME("Unrecognised semantic usage %#x.\n", semantic->usage);
1935 static void shader_dump_register(struct wined3d_string_buffer *buffer,
1936 const struct wined3d_shader_register *reg, const struct wined3d_shader_version *shader_version)
1938 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
1939 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
1940 UINT offset = reg->idx[0].offset;
1942 switch (reg->type)
1944 case WINED3DSPR_TEMP:
1945 shader_addline(buffer, "r");
1946 break;
1948 case WINED3DSPR_INPUT:
1949 shader_addline(buffer, "v");
1950 break;
1952 case WINED3DSPR_CONST:
1953 case WINED3DSPR_CONST2:
1954 case WINED3DSPR_CONST3:
1955 case WINED3DSPR_CONST4:
1956 shader_addline(buffer, "c");
1957 offset = shader_get_float_offset(reg->type, offset);
1958 break;
1960 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
1961 shader_addline(buffer, "%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
1962 break;
1964 case WINED3DSPR_RASTOUT:
1965 shader_addline(buffer, "%s", rastout_reg_names[offset]);
1966 break;
1968 case WINED3DSPR_COLOROUT:
1969 shader_addline(buffer, "oC");
1970 break;
1972 case WINED3DSPR_DEPTHOUT:
1973 shader_addline(buffer, "oDepth");
1974 break;
1976 case WINED3DSPR_ATTROUT:
1977 shader_addline(buffer, "oD");
1978 break;
1980 case WINED3DSPR_TEXCRDOUT:
1981 /* Vertex shaders >= 3.0 use general purpose output registers
1982 * (WINED3DSPR_OUTPUT), which can include an address token. */
1983 if (shader_version->major >= 3)
1984 shader_addline(buffer, "o");
1985 else
1986 shader_addline(buffer, "oT");
1987 break;
1989 case WINED3DSPR_CONSTINT:
1990 shader_addline(buffer, "i");
1991 break;
1993 case WINED3DSPR_CONSTBOOL:
1994 shader_addline(buffer, "b");
1995 break;
1997 case WINED3DSPR_LABEL:
1998 shader_addline(buffer, "l");
1999 break;
2001 case WINED3DSPR_LOOP:
2002 shader_addline(buffer, "aL");
2003 break;
2005 case WINED3DSPR_SAMPLER:
2006 shader_addline(buffer, "s");
2007 break;
2009 case WINED3DSPR_MISCTYPE:
2010 if (offset > 1)
2012 FIXME("Unhandled misctype register %u.\n", offset);
2013 shader_addline(buffer, "<unhandled misctype %#x>", offset);
2015 else
2017 shader_addline(buffer, "%s", misctype_reg_names[offset]);
2019 break;
2021 case WINED3DSPR_PREDICATE:
2022 shader_addline(buffer, "p");
2023 break;
2025 case WINED3DSPR_IMMCONST:
2026 shader_addline(buffer, "l");
2027 break;
2029 case WINED3DSPR_CONSTBUFFER:
2030 shader_addline(buffer, "cb");
2031 break;
2033 case WINED3DSPR_IMMCONSTBUFFER:
2034 shader_addline(buffer, "icb");
2035 break;
2037 case WINED3DSPR_PRIMID:
2038 shader_addline(buffer, "primID");
2039 break;
2041 case WINED3DSPR_NULL:
2042 shader_addline(buffer, "null");
2043 break;
2045 case WINED3DSPR_RESOURCE:
2046 shader_addline(buffer, "t");
2047 break;
2049 case WINED3DSPR_UAV:
2050 shader_addline(buffer, "u");
2051 break;
2053 case WINED3DSPR_OUTPOINTID:
2054 shader_addline(buffer, "vOutputControlPointID");
2055 break;
2057 case WINED3DSPR_FORKINSTID:
2058 shader_addline(buffer, "vForkInstanceId");
2059 break;
2061 case WINED3DSPR_JOININSTID:
2062 shader_addline(buffer, "vJoinInstanceId");
2063 break;
2065 case WINED3DSPR_INCONTROLPOINT:
2066 shader_addline(buffer, "vicp");
2067 break;
2069 case WINED3DSPR_OUTCONTROLPOINT:
2070 shader_addline(buffer, "vocp");
2071 break;
2073 case WINED3DSPR_PATCHCONST:
2074 shader_addline(buffer, "vpc");
2075 break;
2077 case WINED3DSPR_TESSCOORD:
2078 shader_addline(buffer, "vDomainLocation");
2079 break;
2081 case WINED3DSPR_GROUPSHAREDMEM:
2082 shader_addline(buffer, "g");
2083 break;
2085 case WINED3DSPR_THREADID:
2086 shader_addline(buffer, "vThreadID");
2087 break;
2089 case WINED3DSPR_THREADGROUPID:
2090 shader_addline(buffer, "vThreadGroupID");
2091 break;
2093 case WINED3DSPR_LOCALTHREADID:
2094 shader_addline(buffer, "vThreadIDInGroup");
2095 break;
2097 case WINED3DSPR_LOCALTHREADINDEX:
2098 shader_addline(buffer, "vThreadIDInGroupFlattened");
2099 break;
2101 case WINED3DSPR_IDXTEMP:
2102 shader_addline(buffer, "x");
2103 break;
2105 case WINED3DSPR_STREAM:
2106 shader_addline(buffer, "m");
2107 break;
2109 case WINED3DSPR_FUNCTIONBODY:
2110 shader_addline(buffer, "fb");
2111 break;
2113 case WINED3DSPR_FUNCTIONPOINTER:
2114 shader_addline(buffer, "fp");
2115 break;
2117 case WINED3DSPR_COVERAGE:
2118 shader_addline(buffer, "vCoverage");
2119 break;
2121 case WINED3DSPR_SAMPLEMASK:
2122 shader_addline(buffer, "oMask");
2123 break;
2125 default:
2126 shader_addline(buffer, "<unhandled_rtype(%#x)>", reg->type);
2127 break;
2130 if (reg->type == WINED3DSPR_IMMCONST)
2132 shader_addline(buffer, "(");
2133 switch (reg->immconst_type)
2135 case WINED3D_IMMCONST_SCALAR:
2136 switch (reg->data_type)
2138 case WINED3D_DATA_FLOAT:
2139 shader_addline(buffer, "%.8e", *(const float *)reg->u.immconst_data);
2140 break;
2141 case WINED3D_DATA_INT:
2142 shader_addline(buffer, "%d", reg->u.immconst_data[0]);
2143 break;
2144 case WINED3D_DATA_RESOURCE:
2145 case WINED3D_DATA_SAMPLER:
2146 case WINED3D_DATA_UINT:
2147 shader_addline(buffer, "%u", reg->u.immconst_data[0]);
2148 break;
2149 default:
2150 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
2151 break;
2153 break;
2155 case WINED3D_IMMCONST_VEC4:
2156 switch (reg->data_type)
2158 case WINED3D_DATA_FLOAT:
2159 shader_addline(buffer, "%.8e, %.8e, %.8e, %.8e",
2160 *(const float *)&reg->u.immconst_data[0], *(const float *)&reg->u.immconst_data[1],
2161 *(const float *)&reg->u.immconst_data[2], *(const float *)&reg->u.immconst_data[3]);
2162 break;
2163 case WINED3D_DATA_INT:
2164 shader_addline(buffer, "%d, %d, %d, %d",
2165 reg->u.immconst_data[0], reg->u.immconst_data[1],
2166 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2167 break;
2168 case WINED3D_DATA_RESOURCE:
2169 case WINED3D_DATA_SAMPLER:
2170 case WINED3D_DATA_UINT:
2171 shader_addline(buffer, "%u, %u, %u, %u",
2172 reg->u.immconst_data[0], reg->u.immconst_data[1],
2173 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2174 break;
2175 default:
2176 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
2177 break;
2179 break;
2181 default:
2182 shader_addline(buffer, "<unhandled immconst_type %#x>", reg->immconst_type);
2183 break;
2185 shader_addline(buffer, ")");
2187 else if (reg->type != WINED3DSPR_RASTOUT
2188 && reg->type != WINED3DSPR_MISCTYPE
2189 && reg->type != WINED3DSPR_NULL)
2191 if (offset != ~0u)
2193 shader_addline(buffer, "[");
2194 if (reg->idx[0].rel_addr)
2196 shader_dump_src_param(buffer, reg->idx[0].rel_addr, shader_version);
2197 shader_addline(buffer, " + ");
2199 shader_addline(buffer, "%u]", offset);
2201 if (reg->idx[1].offset != ~0u)
2203 shader_addline(buffer, "[");
2204 if (reg->idx[1].rel_addr)
2206 shader_dump_src_param(buffer, reg->idx[1].rel_addr, shader_version);
2207 shader_addline(buffer, " + ");
2209 shader_addline(buffer, "%u]", reg->idx[1].offset);
2213 if (reg->type == WINED3DSPR_FUNCTIONPOINTER)
2214 shader_addline(buffer, "[%u]", reg->u.fp_body_idx);
2218 static void shader_dump_dst_param(struct wined3d_string_buffer *buffer,
2219 const struct wined3d_shader_dst_param *param, const struct wined3d_shader_version *shader_version)
2221 DWORD write_mask = param->write_mask;
2223 shader_dump_register(buffer, &param->reg, shader_version);
2225 if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
2227 static const char write_mask_chars[] = "xyzw";
2229 shader_addline(buffer, ".");
2230 if (write_mask & WINED3DSP_WRITEMASK_0)
2231 shader_addline(buffer, "%c", write_mask_chars[0]);
2232 if (write_mask & WINED3DSP_WRITEMASK_1)
2233 shader_addline(buffer, "%c", write_mask_chars[1]);
2234 if (write_mask & WINED3DSP_WRITEMASK_2)
2235 shader_addline(buffer, "%c", write_mask_chars[2]);
2236 if (write_mask & WINED3DSP_WRITEMASK_3)
2237 shader_addline(buffer, "%c", write_mask_chars[3]);
2241 static void shader_dump_src_param(struct wined3d_string_buffer *buffer,
2242 const struct wined3d_shader_src_param *param, const struct wined3d_shader_version *shader_version)
2244 enum wined3d_shader_src_modifier src_modifier = param->modifiers;
2245 DWORD swizzle = param->swizzle;
2247 if (src_modifier == WINED3DSPSM_NEG
2248 || src_modifier == WINED3DSPSM_BIASNEG
2249 || src_modifier == WINED3DSPSM_SIGNNEG
2250 || src_modifier == WINED3DSPSM_X2NEG
2251 || src_modifier == WINED3DSPSM_ABSNEG)
2252 shader_addline(buffer, "-");
2253 else if (src_modifier == WINED3DSPSM_COMP)
2254 shader_addline(buffer, "1-");
2255 else if (src_modifier == WINED3DSPSM_NOT)
2256 shader_addline(buffer, "!");
2258 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
2259 shader_addline(buffer, "abs(");
2261 shader_dump_register(buffer, &param->reg, shader_version);
2263 switch (src_modifier)
2265 case WINED3DSPSM_NONE: break;
2266 case WINED3DSPSM_NEG: break;
2267 case WINED3DSPSM_NOT: break;
2268 case WINED3DSPSM_BIAS: shader_addline(buffer, "_bias"); break;
2269 case WINED3DSPSM_BIASNEG: shader_addline(buffer, "_bias"); break;
2270 case WINED3DSPSM_SIGN: shader_addline(buffer, "_bx2"); break;
2271 case WINED3DSPSM_SIGNNEG: shader_addline(buffer, "_bx2"); break;
2272 case WINED3DSPSM_COMP: break;
2273 case WINED3DSPSM_X2: shader_addline(buffer, "_x2"); break;
2274 case WINED3DSPSM_X2NEG: shader_addline(buffer, "_x2"); break;
2275 case WINED3DSPSM_DZ: shader_addline(buffer, "_dz"); break;
2276 case WINED3DSPSM_DW: shader_addline(buffer, "_dw"); break;
2277 case WINED3DSPSM_ABSNEG: shader_addline(buffer, ")"); break;
2278 case WINED3DSPSM_ABS: shader_addline(buffer, ")"); break;
2279 default: shader_addline(buffer, "_unknown_modifier(%#x)", src_modifier);
2282 if (swizzle != WINED3DSP_NOSWIZZLE)
2284 static const char swizzle_chars[] = "xyzw";
2285 DWORD swizzle_x = swizzle & 0x03;
2286 DWORD swizzle_y = (swizzle >> 2) & 0x03;
2287 DWORD swizzle_z = (swizzle >> 4) & 0x03;
2288 DWORD swizzle_w = (swizzle >> 6) & 0x03;
2290 if (swizzle_x == swizzle_y
2291 && swizzle_x == swizzle_z
2292 && swizzle_x == swizzle_w)
2294 shader_addline(buffer, ".%c", swizzle_chars[swizzle_x]);
2296 else
2298 shader_addline(buffer, ".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
2299 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
2304 /* Shared code in order to generate the bulk of the shader string.
2305 * NOTE: A description of how to parse tokens can be found on MSDN. */
2306 HRESULT shader_generate_main(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
2307 const struct wined3d_shader_reg_maps *reg_maps, void *backend_ctx)
2309 struct wined3d_device *device = shader->device;
2310 const struct wined3d_shader_frontend *fe = shader->frontend;
2311 void *fe_data = shader->frontend_data;
2312 struct wined3d_shader_version shader_version;
2313 struct wined3d_shader_parser_state state;
2314 struct wined3d_shader_instruction ins;
2315 struct wined3d_shader_tex_mx tex_mx;
2316 struct wined3d_shader_context ctx;
2317 const DWORD *ptr;
2319 /* Initialize current parsing state. */
2320 tex_mx.current_row = 0;
2321 state.current_loop_depth = 0;
2322 state.current_loop_reg = 0;
2323 state.in_subroutine = FALSE;
2325 ctx.shader = shader;
2326 ctx.gl_info = &device->adapter->gl_info;
2327 ctx.reg_maps = reg_maps;
2328 ctx.buffer = buffer;
2329 ctx.tex_mx = &tex_mx;
2330 ctx.state = &state;
2331 ctx.backend_data = backend_ctx;
2332 ins.ctx = &ctx;
2334 fe->shader_read_header(fe_data, &ptr, &shader_version);
2336 while (!fe->shader_is_end(fe_data, &ptr))
2338 /* Read opcode. */
2339 fe->shader_read_instruction(fe_data, &ptr, &ins);
2341 /* Unknown opcode and its parameters. */
2342 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
2344 WARN("Encountered unrecognised or invalid instruction.\n");
2345 return WINED3DERR_INVALIDCALL;
2348 if (ins.predicate)
2349 FIXME("Predicates not implemented.\n");
2351 /* Call appropriate function for output target */
2352 device->shader_backend->shader_handle_instruction(&ins);
2355 return WINED3D_OK;
2358 static void shader_dump_ins_modifiers(struct wined3d_string_buffer *buffer,
2359 const struct wined3d_shader_dst_param *dst)
2361 DWORD mmask = dst->modifiers;
2363 switch (dst->shift)
2365 case 0: break;
2366 case 13: shader_addline(buffer, "_d8"); break;
2367 case 14: shader_addline(buffer, "_d4"); break;
2368 case 15: shader_addline(buffer, "_d2"); break;
2369 case 1: shader_addline(buffer, "_x2"); break;
2370 case 2: shader_addline(buffer, "_x4"); break;
2371 case 3: shader_addline(buffer, "_x8"); break;
2372 default: shader_addline(buffer, "_unhandled_shift(%d)", dst->shift); break;
2375 if (mmask & WINED3DSPDM_SATURATE) shader_addline(buffer, "_sat");
2376 if (mmask & WINED3DSPDM_PARTIALPRECISION) shader_addline(buffer, "_pp");
2377 if (mmask & WINED3DSPDM_MSAMPCENTROID) shader_addline(buffer, "_centroid");
2379 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
2380 if (mmask) FIXME("Unrecognised modifier %#x.\n", mmask);
2383 static void shader_dump_primitive_type(struct wined3d_string_buffer *buffer,
2384 enum wined3d_primitive_type primitive_type)
2386 switch (primitive_type)
2388 case WINED3D_PT_UNDEFINED:
2389 shader_addline(buffer, "undefined");
2390 break;
2391 case WINED3D_PT_POINTLIST:
2392 shader_addline(buffer, "pointlist");
2393 break;
2394 case WINED3D_PT_LINELIST:
2395 shader_addline(buffer, "linelist");
2396 break;
2397 case WINED3D_PT_LINESTRIP:
2398 shader_addline(buffer, "linestrip");
2399 break;
2400 case WINED3D_PT_TRIANGLELIST:
2401 shader_addline(buffer, "trianglelist");
2402 break;
2403 case WINED3D_PT_TRIANGLESTRIP:
2404 shader_addline(buffer, "trianglestrip");
2405 break;
2406 case WINED3D_PT_TRIANGLEFAN:
2407 shader_addline(buffer, "trianglefan");
2408 break;
2409 case WINED3D_PT_LINELIST_ADJ:
2410 shader_addline(buffer, "linelist_adj");
2411 break;
2412 case WINED3D_PT_LINESTRIP_ADJ:
2413 shader_addline(buffer, "linestrip_adj");
2414 break;
2415 case WINED3D_PT_TRIANGLELIST_ADJ:
2416 shader_addline(buffer, "trianglelist_adj");
2417 break;
2418 case WINED3D_PT_TRIANGLESTRIP_ADJ:
2419 shader_addline(buffer, "trianglestrip_adj");
2420 break;
2421 default:
2422 shader_addline(buffer, "<unrecognized_primitive_type %#x>", primitive_type);
2423 break;
2427 static void shader_dump_interpolation_mode(struct wined3d_string_buffer *buffer,
2428 enum wined3d_shader_interpolation_mode interpolation_mode)
2430 switch (interpolation_mode)
2432 case WINED3DSIM_CONSTANT:
2433 shader_addline(buffer, "constant");
2434 break;
2435 case WINED3DSIM_LINEAR:
2436 shader_addline(buffer, "linear");
2437 break;
2438 case WINED3DSIM_LINEAR_CENTROID:
2439 shader_addline(buffer, "linear centroid");
2440 break;
2441 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2442 shader_addline(buffer, "linear noperspective");
2443 break;
2444 case WINED3DSIM_LINEAR_SAMPLE:
2445 shader_addline(buffer, "linear sample");
2446 break;
2447 case WINED3DSIM_LINEAR_NOPERSPECTIVE_CENTROID:
2448 shader_addline(buffer, "linear noperspective centroid");
2449 break;
2450 case WINED3DSIM_LINEAR_NOPERSPECTIVE_SAMPLE:
2451 shader_addline(buffer, "linear noperspective sample");
2452 break;
2453 default:
2454 shader_addline(buffer, "<unrecognized_interpolation_mode %#x>", interpolation_mode);
2455 break;
2459 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data)
2461 struct wined3d_shader_version shader_version;
2462 struct wined3d_string_buffer buffer;
2463 const char *type_prefix;
2464 const char *p, *q;
2465 const DWORD *ptr;
2466 DWORD i;
2468 if (!string_buffer_init(&buffer))
2470 ERR("Failed to initialize string buffer.\n");
2471 return;
2474 fe->shader_read_header(fe_data, &ptr, &shader_version);
2476 TRACE("Parsing %p.\n", ptr);
2478 switch (shader_version.type)
2480 case WINED3D_SHADER_TYPE_VERTEX:
2481 type_prefix = "vs";
2482 break;
2484 case WINED3D_SHADER_TYPE_HULL:
2485 type_prefix = "hs";
2486 break;
2488 case WINED3D_SHADER_TYPE_DOMAIN:
2489 type_prefix = "ds";
2490 break;
2492 case WINED3D_SHADER_TYPE_GEOMETRY:
2493 type_prefix = "gs";
2494 break;
2496 case WINED3D_SHADER_TYPE_PIXEL:
2497 type_prefix = "ps";
2498 break;
2500 case WINED3D_SHADER_TYPE_COMPUTE:
2501 type_prefix = "cs";
2502 break;
2504 default:
2505 FIXME("Unhandled shader type %#x.\n", shader_version.type);
2506 type_prefix = "unknown";
2507 break;
2510 shader_addline(&buffer, "%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
2512 while (!fe->shader_is_end(fe_data, &ptr))
2514 struct wined3d_shader_instruction ins;
2516 fe->shader_read_instruction(fe_data, &ptr, &ins);
2517 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
2519 WARN("Skipping unrecognized instruction.\n");
2520 shader_addline(&buffer, "<unrecognized instruction>\n");
2521 continue;
2524 if (ins.handler_idx == WINED3DSIH_DCL || ins.handler_idx == WINED3DSIH_DCL_UAV_TYPED)
2526 shader_dump_decl_usage(&buffer, &ins.declaration.semantic, ins.flags, &shader_version);
2527 shader_dump_ins_modifiers(&buffer, &ins.declaration.semantic.reg);
2528 shader_addline(&buffer, " ");
2529 shader_dump_dst_param(&buffer, &ins.declaration.semantic.reg, &shader_version);
2531 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
2533 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2534 shader_dump_src_param(&buffer, &ins.declaration.src, &shader_version);
2535 shader_addline(&buffer, ", %s",
2536 ins.flags & WINED3DSI_INDEXED_DYNAMIC ? "dynamicIndexed" : "immediateIndexed");
2538 else if (ins.handler_idx == WINED3DSIH_DCL_FUNCTION_BODY)
2540 shader_addline(&buffer, "%s fb%u",
2541 shader_opcode_names[ins.handler_idx], ins.declaration.index);
2543 else if (ins.handler_idx == WINED3DSIH_DCL_FUNCTION_TABLE)
2545 shader_addline(&buffer, "%s ft%u = {...}",
2546 shader_opcode_names[ins.handler_idx], ins.declaration.index);
2548 else if (ins.handler_idx == WINED3DSIH_DCL_GLOBAL_FLAGS)
2550 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2551 shader_dump_global_flags(&buffer, ins.flags);
2553 else if (ins.handler_idx == WINED3DSIH_DCL_HS_MAX_TESSFACTOR)
2555 shader_addline(&buffer, "%s %.8e", shader_opcode_names[ins.handler_idx],
2556 ins.declaration.max_tessellation_factor);
2558 else if (ins.handler_idx == WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER)
2560 shader_addline(&buffer, "%s {\n", shader_opcode_names[ins.handler_idx]);
2561 for (i = 0; i < ins.declaration.icb->vec4_count; ++i)
2563 shader_addline(&buffer, " {0x%08x, 0x%08x, 0x%08x, 0x%08x},\n",
2564 ins.declaration.icb->data[4 * i + 0],
2565 ins.declaration.icb->data[4 * i + 1],
2566 ins.declaration.icb->data[4 * i + 2],
2567 ins.declaration.icb->data[4 * i + 3]);
2569 shader_addline(&buffer, "}");
2571 else if (ins.handler_idx == WINED3DSIH_DCL_INDEX_RANGE)
2573 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2574 shader_dump_dst_param(&buffer, &ins.declaration.index_range.first_register, &shader_version);
2575 shader_addline(&buffer, " %u", ins.declaration.index_range.last_register);
2577 else if (ins.handler_idx == WINED3DSIH_DCL_INDEXABLE_TEMP)
2579 shader_addline(&buffer, "%s x[%u][%u], %u", shader_opcode_names[ins.handler_idx],
2580 ins.declaration.indexable_temp.register_idx,
2581 ins.declaration.indexable_temp.register_size,
2582 ins.declaration.indexable_temp.component_count);
2584 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS)
2586 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2587 shader_dump_interpolation_mode(&buffer, ins.flags);
2588 shader_addline(&buffer, " ");
2589 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2591 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SGV
2592 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SGV
2593 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SIV
2594 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_SIV)
2596 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2597 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2598 shader_addline(&buffer, ", ");
2599 shader_dump_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2601 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SIV)
2603 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2604 shader_dump_interpolation_mode(&buffer, ins.flags);
2605 shader_addline(&buffer, " ");
2606 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2607 shader_addline(&buffer, ", ");
2608 shader_dump_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2610 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT
2611 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT)
2613 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2614 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2616 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE
2617 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
2619 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2620 shader_dump_primitive_type(&buffer, ins.declaration.primitive_type);
2622 else if (ins.handler_idx == WINED3DSIH_DCL_INTERFACE)
2624 shader_addline(&buffer, "%s fp[%u][%u][%u] = {...}",
2625 shader_opcode_names[ins.handler_idx], ins.declaration.fp.index,
2626 ins.declaration.fp.array_size, ins.declaration.fp.body_count);
2628 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_RAW)
2630 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2631 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2633 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_STRUCTURED)
2635 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2636 shader_dump_dst_param(&buffer, &ins.declaration.structured_resource.reg, &shader_version);
2637 shader_addline(&buffer, ", %u", ins.declaration.structured_resource.byte_stride);
2639 else if (ins.handler_idx == WINED3DSIH_DCL_SAMPLER)
2641 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2642 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2643 if (ins.flags == WINED3DSI_SAMPLER_COMPARISON_MODE)
2644 shader_addline(&buffer, ", comparisonMode");
2646 else if (ins.handler_idx == WINED3DSIH_DCL_TEMPS
2647 || ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT
2648 || ins.handler_idx == WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT
2649 || ins.handler_idx == WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT
2650 || ins.handler_idx == WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT
2651 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT)
2653 shader_addline(&buffer, "%s %u", shader_opcode_names[ins.handler_idx], ins.declaration.count);
2655 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_DOMAIN)
2657 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2658 shader_dump_tessellator_domain(&buffer, ins.declaration.tessellator_domain);
2660 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE)
2662 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2663 shader_dump_tessellator_output_primitive(&buffer, ins.declaration.tessellator_output_primitive);
2665 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_PARTITIONING)
2667 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2668 shader_dump_tessellator_partitioning(&buffer, ins.declaration.tessellator_partitioning);
2670 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_RAW)
2672 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2673 shader_dump_dst_param(&buffer, &ins.declaration.tgsm_raw.reg, &shader_version);
2674 shader_addline(&buffer, ", %u", ins.declaration.tgsm_raw.byte_count);
2676 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_STRUCTURED)
2678 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2679 shader_dump_dst_param(&buffer, &ins.declaration.tgsm_structured.reg, &shader_version);
2680 shader_addline(&buffer, ", %u, %u", ins.declaration.tgsm_structured.byte_stride,
2681 ins.declaration.tgsm_structured.structure_count);
2683 else if (ins.handler_idx == WINED3DSIH_DCL_THREAD_GROUP)
2685 shader_addline(&buffer, "%s %u, %u, %u", shader_opcode_names[ins.handler_idx],
2686 ins.declaration.thread_group_size.x,
2687 ins.declaration.thread_group_size.y,
2688 ins.declaration.thread_group_size.z);
2690 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_RAW)
2692 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2693 shader_dump_uav_flags(&buffer, ins.flags);
2694 shader_addline(&buffer, " ");
2695 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2697 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_STRUCTURED)
2699 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2700 shader_dump_uav_flags(&buffer, ins.flags);
2701 shader_addline(&buffer, " ");
2702 shader_dump_dst_param(&buffer, &ins.declaration.structured_resource.reg, &shader_version);
2703 shader_addline(&buffer, ", %u", ins.declaration.structured_resource.byte_stride);
2705 else if (ins.handler_idx == WINED3DSIH_DEF)
2707 shader_addline(&buffer, "def c%u = %.8e, %.8e, %.8e, %.8e", shader_get_float_offset(ins.dst[0].reg.type,
2708 ins.dst[0].reg.idx[0].offset),
2709 *(const float *)&ins.src[0].reg.u.immconst_data[0],
2710 *(const float *)&ins.src[0].reg.u.immconst_data[1],
2711 *(const float *)&ins.src[0].reg.u.immconst_data[2],
2712 *(const float *)&ins.src[0].reg.u.immconst_data[3]);
2714 else if (ins.handler_idx == WINED3DSIH_DEFI)
2716 shader_addline(&buffer, "defi i%u = %d, %d, %d, %d", ins.dst[0].reg.idx[0].offset,
2717 ins.src[0].reg.u.immconst_data[0],
2718 ins.src[0].reg.u.immconst_data[1],
2719 ins.src[0].reg.u.immconst_data[2],
2720 ins.src[0].reg.u.immconst_data[3]);
2722 else if (ins.handler_idx == WINED3DSIH_DEFB)
2724 shader_addline(&buffer, "defb b%u = %s",
2725 ins.dst[0].reg.idx[0].offset, ins.src[0].reg.u.immconst_data[0] ? "true" : "false");
2727 else
2729 if (ins.predicate)
2731 shader_addline(&buffer, "(");
2732 shader_dump_src_param(&buffer, ins.predicate, &shader_version);
2733 shader_addline(&buffer, ") ");
2736 /* PixWin marks instructions with the coissue flag with a '+' */
2737 if (ins.coissue)
2738 shader_addline(&buffer, "+");
2740 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2742 if (ins.handler_idx == WINED3DSIH_BREAKP
2743 || ins.handler_idx == WINED3DSIH_IF
2744 || ins.handler_idx == WINED3DSIH_RETP)
2746 switch (ins.flags)
2748 case WINED3D_SHADER_CONDITIONAL_OP_NZ: shader_addline(&buffer, "_nz"); break;
2749 case WINED3D_SHADER_CONDITIONAL_OP_Z: shader_addline(&buffer, "_z"); break;
2750 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags); break;
2753 else if (ins.handler_idx == WINED3DSIH_IFC
2754 || ins.handler_idx == WINED3DSIH_BREAKC)
2756 switch (ins.flags)
2758 case WINED3D_SHADER_REL_OP_GT: shader_addline(&buffer, "_gt"); break;
2759 case WINED3D_SHADER_REL_OP_EQ: shader_addline(&buffer, "_eq"); break;
2760 case WINED3D_SHADER_REL_OP_GE: shader_addline(&buffer, "_ge"); break;
2761 case WINED3D_SHADER_REL_OP_LT: shader_addline(&buffer, "_lt"); break;
2762 case WINED3D_SHADER_REL_OP_NE: shader_addline(&buffer, "_ne"); break;
2763 case WINED3D_SHADER_REL_OP_LE: shader_addline(&buffer, "_le"); break;
2764 default: shader_addline(&buffer, "_(%u)", ins.flags);
2767 else if (ins.handler_idx == WINED3DSIH_TEX
2768 && shader_version.major >= 2
2769 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
2771 shader_addline(&buffer, "p");
2773 else if (ins.handler_idx == WINED3DSIH_RESINFO && ins.flags)
2775 switch (ins.flags)
2777 case WINED3DSI_RESINFO_RCP_FLOAT: shader_addline(&buffer, "_rcpFloat"); break;
2778 case WINED3DSI_RESINFO_UINT: shader_addline(&buffer, "_uint"); break;
2779 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags);
2782 else if (ins.handler_idx == WINED3DSIH_SAMPLE_INFO && ins.flags)
2784 switch (ins.flags)
2786 case WINED3DSI_SAMPLE_INFO_UINT: shader_addline(&buffer, "_uint"); break;
2787 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags);
2790 else if (ins.handler_idx == WINED3DSIH_SYNC)
2792 shader_dump_sync_flags(&buffer, ins.flags);
2795 if (wined3d_shader_instruction_has_texel_offset(&ins))
2796 shader_addline(&buffer, "(%d,%d,%d)", ins.texel_offset.u, ins.texel_offset.v, ins.texel_offset.w);
2798 for (i = 0; i < ins.dst_count; ++i)
2800 shader_dump_ins_modifiers(&buffer, &ins.dst[i]);
2801 shader_addline(&buffer, !i ? " " : ", ");
2802 shader_dump_dst_param(&buffer, &ins.dst[i], &shader_version);
2805 /* Other source tokens */
2806 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
2808 shader_addline(&buffer, !i ? " " : ", ");
2809 shader_dump_src_param(&buffer, &ins.src[i - ins.dst_count], &shader_version);
2812 shader_addline(&buffer, "\n");
2815 for (p = buffer.buffer; *p; p = q)
2817 if (!(q = strstr(p, "\n")))
2818 q = p + strlen(p);
2819 else
2820 ++q;
2821 TRACE(" %.*s", (int)(q - p), p);
2824 string_buffer_free(&buffer);
2827 static void shader_cleanup(struct wined3d_shader *shader)
2829 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
2830 HeapFree(GetProcessHeap(), 0, shader->u.gs.so_desc.elements);
2832 HeapFree(GetProcessHeap(), 0, shader->output_signature.elements);
2833 HeapFree(GetProcessHeap(), 0, shader->input_signature.elements);
2834 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
2835 shader->device->shader_backend->shader_destroy(shader);
2836 shader_cleanup_reg_maps(&shader->reg_maps);
2837 HeapFree(GetProcessHeap(), 0, shader->function);
2838 shader_delete_constant_list(&shader->constantsF);
2839 shader_delete_constant_list(&shader->constantsB);
2840 shader_delete_constant_list(&shader->constantsI);
2841 list_remove(&shader->shader_list_entry);
2843 if (shader->frontend && shader->frontend_data)
2844 shader->frontend->shader_free(shader->frontend_data);
2847 struct shader_none_priv
2849 const struct wined3d_vertex_pipe_ops *vertex_pipe;
2850 const struct fragment_pipeline *fragment_pipe;
2851 BOOL ffp_proj_control;
2854 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
2855 static void shader_none_precompile(void *shader_priv, struct wined3d_shader *shader) {}
2856 static void shader_none_select_compute(void *shader_priv, struct wined3d_context *context,
2857 const struct wined3d_state *state) {}
2858 static void shader_none_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count) {}
2859 static void shader_none_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count) {}
2860 static void shader_none_load_constants(void *shader_priv, struct wined3d_context *context,
2861 const struct wined3d_state *state) {}
2862 static void shader_none_destroy(struct wined3d_shader *shader) {}
2863 static void shader_none_free_context_data(struct wined3d_context *context) {}
2864 static void shader_none_init_context_state(struct wined3d_context *context) {}
2866 /* Context activation is done by the caller. */
2867 static void shader_none_select(void *shader_priv, struct wined3d_context *context,
2868 const struct wined3d_state *state)
2870 const struct wined3d_gl_info *gl_info = context->gl_info;
2871 struct shader_none_priv *priv = shader_priv;
2873 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
2874 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
2877 /* Context activation is done by the caller. */
2878 static void shader_none_disable(void *shader_priv, struct wined3d_context *context)
2880 struct shader_none_priv *priv = shader_priv;
2881 const struct wined3d_gl_info *gl_info = context->gl_info;
2883 priv->vertex_pipe->vp_enable(gl_info, FALSE);
2884 priv->fragment_pipe->enable_extension(gl_info, FALSE);
2886 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
2887 | (1u << WINED3D_SHADER_TYPE_VERTEX)
2888 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
2889 | (1u << WINED3D_SHADER_TYPE_HULL)
2890 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
2891 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
2894 static HRESULT shader_none_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
2895 const struct fragment_pipeline *fragment_pipe)
2897 struct fragment_caps fragment_caps;
2898 void *vertex_priv, *fragment_priv;
2899 struct shader_none_priv *priv;
2901 if (!(priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv))))
2902 return E_OUTOFMEMORY;
2904 if (!(vertex_priv = vertex_pipe->vp_alloc(&none_shader_backend, priv)))
2906 ERR("Failed to initialize vertex pipe.\n");
2907 HeapFree(GetProcessHeap(), 0, priv);
2908 return E_FAIL;
2911 if (!(fragment_priv = fragment_pipe->alloc_private(&none_shader_backend, priv)))
2913 ERR("Failed to initialize fragment pipe.\n");
2914 vertex_pipe->vp_free(device);
2915 HeapFree(GetProcessHeap(), 0, priv);
2916 return E_FAIL;
2919 priv->vertex_pipe = vertex_pipe;
2920 priv->fragment_pipe = fragment_pipe;
2921 fragment_pipe->get_caps(&device->adapter->gl_info, &fragment_caps);
2922 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
2924 device->vertex_priv = vertex_priv;
2925 device->fragment_priv = fragment_priv;
2926 device->shader_priv = priv;
2928 return WINED3D_OK;
2931 static void shader_none_free(struct wined3d_device *device)
2933 struct shader_none_priv *priv = device->shader_priv;
2935 priv->fragment_pipe->free_private(device);
2936 priv->vertex_pipe->vp_free(device);
2937 HeapFree(GetProcessHeap(), 0, priv);
2940 static BOOL shader_none_allocate_context_data(struct wined3d_context *context)
2942 return TRUE;
2945 static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
2947 /* Set the shader caps to 0 for the none shader backend */
2948 caps->vs_version = 0;
2949 caps->hs_version = 0;
2950 caps->ds_version = 0;
2951 caps->gs_version = 0;
2952 caps->ps_version = 0;
2953 caps->cs_version = 0;
2954 caps->vs_uniform_count = 0;
2955 caps->ps_uniform_count = 0;
2956 caps->ps_1x_max_value = 0.0f;
2957 caps->varying_count = 0;
2958 caps->wined3d_caps = 0;
2961 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
2963 /* We "support" every possible fixup, since we don't support any shader
2964 * model, and will never have to actually sample a texture. */
2965 return TRUE;
2968 static BOOL shader_none_has_ffp_proj_control(void *shader_priv)
2970 struct shader_none_priv *priv = shader_priv;
2972 return priv->ffp_proj_control;
2975 const struct wined3d_shader_backend_ops none_shader_backend =
2977 shader_none_handle_instruction,
2978 shader_none_precompile,
2979 shader_none_select,
2980 shader_none_select_compute,
2981 shader_none_disable,
2982 shader_none_update_float_vertex_constants,
2983 shader_none_update_float_pixel_constants,
2984 shader_none_load_constants,
2985 shader_none_destroy,
2986 shader_none_alloc,
2987 shader_none_free,
2988 shader_none_allocate_context_data,
2989 shader_none_free_context_data,
2990 shader_none_init_context_state,
2991 shader_none_get_caps,
2992 shader_none_color_fixup_supported,
2993 shader_none_has_ffp_proj_control,
2996 static HRESULT shader_set_function(struct wined3d_shader *shader, DWORD float_const_count,
2997 enum wined3d_shader_type type, unsigned int max_version)
2999 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3000 const struct wined3d_shader_frontend *fe;
3001 HRESULT hr;
3002 unsigned int backend_version;
3003 const struct wined3d_d3d_info *d3d_info = &shader->device->adapter->d3d_info;
3005 TRACE("shader %p, float_const_count %u, type %#x, max_version %u.\n",
3006 shader, float_const_count, type, max_version);
3008 fe = shader->frontend;
3009 if (!(shader->frontend_data = fe->shader_init(shader->function,
3010 shader->functionLength, &shader->output_signature)))
3012 FIXME("Failed to initialize frontend.\n");
3013 return WINED3DERR_INVALIDCALL;
3016 /* First pass: trace shader. */
3017 if (TRACE_ON(d3d_shader))
3018 shader_trace_init(fe, shader->frontend_data);
3020 /* Second pass: figure out which registers are used, what the semantics are, etc. */
3021 if (FAILED(hr = shader_get_registers_used(shader, fe, reg_maps, &shader->input_signature,
3022 &shader->output_signature, float_const_count)))
3023 return hr;
3025 if (reg_maps->shader_version.type != type)
3027 WARN("Wrong shader type %d.\n", reg_maps->shader_version.type);
3028 return WINED3DERR_INVALIDCALL;
3030 if (reg_maps->shader_version.major > max_version)
3032 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps->shader_version.major);
3033 return WINED3DERR_INVALIDCALL;
3035 switch (type)
3037 case WINED3D_SHADER_TYPE_VERTEX:
3038 backend_version = d3d_info->limits.vs_version;
3039 break;
3040 case WINED3D_SHADER_TYPE_HULL:
3041 backend_version = d3d_info->limits.hs_version;
3042 break;
3043 case WINED3D_SHADER_TYPE_DOMAIN:
3044 backend_version = d3d_info->limits.ds_version;
3045 break;
3046 case WINED3D_SHADER_TYPE_GEOMETRY:
3047 backend_version = d3d_info->limits.gs_version;
3048 break;
3049 case WINED3D_SHADER_TYPE_PIXEL:
3050 backend_version = d3d_info->limits.ps_version;
3051 break;
3052 case WINED3D_SHADER_TYPE_COMPUTE:
3053 backend_version = d3d_info->limits.cs_version;
3054 break;
3055 default:
3056 FIXME("No backend version-checking for this shader type.\n");
3057 backend_version = 0;
3059 if (reg_maps->shader_version.major > backend_version)
3061 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
3062 reg_maps->shader_version.major, reg_maps->shader_version.minor);
3063 return WINED3DERR_INVALIDCALL;
3066 return WINED3D_OK;
3069 ULONG CDECL wined3d_shader_incref(struct wined3d_shader *shader)
3071 ULONG refcount = InterlockedIncrement(&shader->ref);
3073 TRACE("%p increasing refcount to %u.\n", shader, refcount);
3075 return refcount;
3078 static void wined3d_shader_init_object(void *object)
3080 struct wined3d_shader *shader = object;
3081 struct wined3d_device *device = shader->device;
3083 list_add_head(&device->shaders, &shader->shader_list_entry);
3085 device->shader_backend->shader_precompile(device->shader_priv, shader);
3088 static void wined3d_shader_destroy_object(void *object)
3090 shader_cleanup(object);
3091 HeapFree(GetProcessHeap(), 0, object);
3094 ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
3096 ULONG refcount = InterlockedDecrement(&shader->ref);
3098 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
3100 if (!refcount)
3102 shader->parent_ops->wined3d_object_destroyed(shader->parent);
3103 wined3d_cs_destroy_object(shader->device->cs, wined3d_shader_destroy_object, shader);
3106 return refcount;
3109 void * CDECL wined3d_shader_get_parent(const struct wined3d_shader *shader)
3111 TRACE("shader %p.\n", shader);
3113 return shader->parent;
3116 HRESULT CDECL wined3d_shader_get_byte_code(const struct wined3d_shader *shader,
3117 void *byte_code, UINT *byte_code_size)
3119 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader, byte_code, byte_code_size);
3121 if (!byte_code)
3123 *byte_code_size = shader->functionLength;
3124 return WINED3D_OK;
3127 if (*byte_code_size < shader->functionLength)
3129 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
3130 * than the required size we should write the required size and
3131 * return D3DERR_MOREDATA. That's not actually true. */
3132 return WINED3DERR_INVALIDCALL;
3135 memcpy(byte_code, shader->function, shader->functionLength);
3137 return WINED3D_OK;
3140 /* Set local constants for d3d8 shaders. */
3141 HRESULT CDECL wined3d_shader_set_local_constants_float(struct wined3d_shader *shader,
3142 UINT start_idx, const float *src_data, UINT count)
3144 UINT end_idx = start_idx + count;
3145 UINT i;
3147 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader, start_idx, src_data, count);
3149 if (end_idx > shader->limits->constant_float)
3151 WARN("end_idx %u > float constants limit %u.\n",
3152 end_idx, shader->limits->constant_float);
3153 end_idx = shader->limits->constant_float;
3156 for (i = start_idx; i < end_idx; ++i)
3158 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
3159 float *value;
3160 if (!lconst)
3161 return E_OUTOFMEMORY;
3163 lconst->idx = i;
3164 value = (float *)lconst->value;
3165 memcpy(value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
3166 list_add_head(&shader->constantsF, &lconst->entry);
3168 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
3169 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
3171 shader->lconst_inf_or_nan = TRUE;
3175 return WINED3D_OK;
3178 void find_vs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3179 WORD swizzle_map, struct vs_compile_args *args, const struct wined3d_d3d_info *d3d_info)
3181 args->fog_src = state->render_states[WINED3D_RS_FOGTABLEMODE]
3182 == WINED3D_FOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
3183 args->clip_enabled = state->render_states[WINED3D_RS_CLIPPING]
3184 && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
3185 args->point_size = state->gl_primitive_type == GL_POINTS;
3186 args->per_vertex_point_size = shader->reg_maps.point_size;
3187 args->next_shader_type = state->shader[WINED3D_SHADER_TYPE_HULL] ? WINED3D_SHADER_TYPE_HULL
3188 : state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ? WINED3D_SHADER_TYPE_GEOMETRY
3189 : WINED3D_SHADER_TYPE_PIXEL;
3190 if (shader->reg_maps.shader_version.major >= 4)
3191 args->next_shader_input_count = state->shader[WINED3D_SHADER_TYPE_HULL]
3192 ? state->shader[WINED3D_SHADER_TYPE_HULL]->limits->packed_input
3193 : state->shader[WINED3D_SHADER_TYPE_GEOMETRY]
3194 ? state->shader[WINED3D_SHADER_TYPE_GEOMETRY]->limits->packed_input
3195 : state->shader[WINED3D_SHADER_TYPE_PIXEL]
3196 ? state->shader[WINED3D_SHADER_TYPE_PIXEL]->limits->packed_input : 0;
3197 else
3198 args->next_shader_input_count = 0;
3199 args->swizzle_map = swizzle_map;
3200 if (d3d_info->emulated_flatshading)
3201 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
3202 else
3203 args->flatshading = 0;
3206 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
3208 if (usage_idx1 != usage_idx2)
3209 return FALSE;
3210 if (usage1 == usage2)
3211 return TRUE;
3212 if (usage1 == WINED3D_DECL_USAGE_POSITION && usage2 == WINED3D_DECL_USAGE_POSITIONT)
3213 return TRUE;
3214 if (usage2 == WINED3D_DECL_USAGE_POSITION && usage1 == WINED3D_DECL_USAGE_POSITIONT)
3215 return TRUE;
3217 return FALSE;
3220 BOOL vshader_get_input(const struct wined3d_shader *shader,
3221 BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
3223 WORD map = shader->reg_maps.input_registers;
3224 unsigned int i;
3226 for (i = 0; map; map >>= 1, ++i)
3228 if (!(map & 1)) continue;
3230 if (match_usage(shader->u.vs.attributes[i].usage,
3231 shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
3233 *regnum = i;
3234 return TRUE;
3237 return FALSE;
3240 static HRESULT shader_signature_copy(struct wined3d_shader_signature *dst,
3241 const struct wined3d_shader_signature *src, char **signature_strings)
3243 struct wined3d_shader_signature_element *e;
3244 unsigned int i;
3245 SIZE_T len;
3246 char *ptr;
3248 if (!src->element_count)
3249 return WINED3D_OK;
3251 ptr = *signature_strings;
3253 dst->element_count = src->element_count;
3254 if (!(dst->elements = wined3d_calloc(dst->element_count, sizeof(*dst->elements))))
3255 return E_OUTOFMEMORY;
3257 for (i = 0; i < src->element_count; ++i)
3259 e = &src->elements[i];
3260 dst->elements[i] = *e;
3262 len = strlen(e->semantic_name);
3263 memcpy(ptr, e->semantic_name, len + 1);
3264 dst->elements[i].semantic_name = ptr;
3265 ptr += len + 1;
3268 *signature_strings = ptr;
3270 return WINED3D_OK;
3273 static HRESULT shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3274 const struct wined3d_shader_desc *desc, DWORD float_const_count, enum wined3d_shader_type type,
3275 void *parent, const struct wined3d_parent_ops *parent_ops)
3277 struct wined3d_shader_signature_element *e;
3278 size_t byte_code_size;
3279 SIZE_T total, len;
3280 unsigned int i;
3281 HRESULT hr;
3282 char *ptr;
3284 TRACE("byte_code %p, byte_code_size %#lx, format %#x, max_version %#x.\n",
3285 desc->byte_code, (long)desc->byte_code_size, desc->format, desc->max_version);
3287 if (!desc->byte_code)
3288 return WINED3DERR_INVALIDCALL;
3290 if (!(shader->frontend = shader_select_frontend(desc->format)))
3292 FIXME("Unable to find frontend for shader.\n");
3293 return WINED3DERR_INVALIDCALL;
3296 shader->ref = 1;
3297 shader->device = device;
3298 shader->parent = parent;
3299 shader->parent_ops = parent_ops;
3301 total = 0;
3302 for (i = 0; i < desc->input_signature.element_count; ++i)
3304 e = &desc->input_signature.elements[i];
3305 len = strlen(e->semantic_name);
3306 if (len >= ~(SIZE_T)0 - total)
3307 return E_OUTOFMEMORY;
3309 total += len + 1;
3311 for (i = 0; i < desc->output_signature.element_count; ++i)
3313 e = &desc->output_signature.elements[i];
3314 len = strlen(e->semantic_name);
3315 if (len >= ~(SIZE_T)0 - total)
3316 return E_OUTOFMEMORY;
3318 total += len + 1;
3320 if (total && !(shader->signature_strings = HeapAlloc(GetProcessHeap(), 0, total)))
3321 return E_OUTOFMEMORY;
3322 ptr = shader->signature_strings;
3324 if (FAILED(hr = shader_signature_copy(&shader->input_signature, &desc->input_signature, &ptr)))
3326 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
3327 return hr;
3329 if (FAILED(hr = shader_signature_copy(&shader->output_signature, &desc->output_signature, &ptr)))
3331 HeapFree(GetProcessHeap(), 0, shader->input_signature.elements);
3332 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
3333 return hr;
3336 list_init(&shader->linked_programs);
3337 list_init(&shader->constantsF);
3338 list_init(&shader->constantsB);
3339 list_init(&shader->constantsI);
3340 shader->lconst_inf_or_nan = FALSE;
3341 list_init(&shader->reg_maps.indexable_temps);
3342 list_init(&shader->shader_list_entry);
3344 byte_code_size = desc->byte_code_size;
3345 if (byte_code_size == ~(size_t)0)
3347 const struct wined3d_shader_frontend *fe = shader->frontend;
3348 struct wined3d_shader_version shader_version;
3349 struct wined3d_shader_instruction ins;
3350 const DWORD *ptr;
3351 void *fe_data;
3353 if (!(fe_data = fe->shader_init(desc->byte_code, byte_code_size, &shader->output_signature)))
3355 WARN("Failed to initialise frontend data.\n");
3356 shader_cleanup(shader);
3357 return WINED3DERR_INVALIDCALL;
3360 fe->shader_read_header(fe_data, &ptr, &shader_version);
3361 while (!fe->shader_is_end(fe_data, &ptr))
3362 fe->shader_read_instruction(fe_data, &ptr, &ins);
3364 fe->shader_free(fe_data);
3366 byte_code_size = (ptr - desc->byte_code) * sizeof(*ptr);
3369 if (!(shader->function = HeapAlloc(GetProcessHeap(), 0, byte_code_size)))
3371 shader_cleanup(shader);
3372 return E_OUTOFMEMORY;
3374 memcpy(shader->function, desc->byte_code, byte_code_size);
3375 shader->functionLength = byte_code_size;
3377 if (FAILED(hr = shader_set_function(shader, float_const_count, type, desc->max_version)))
3379 WARN("Failed to set function, hr %#x.\n", hr);
3380 shader_cleanup(shader);
3381 return hr;
3384 shader->load_local_constsF = shader->lconst_inf_or_nan;
3386 wined3d_cs_init_object(shader->device->cs, wined3d_shader_init_object, shader);
3388 return hr;
3391 static HRESULT vertex_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3392 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3394 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3395 unsigned int i;
3396 HRESULT hr;
3398 if (FAILED(hr = shader_init(shader, device, desc, device->adapter->d3d_info.limits.vs_uniform_count,
3399 WINED3D_SHADER_TYPE_VERTEX, parent, parent_ops)))
3400 return hr;
3402 for (i = 0; i < shader->input_signature.element_count; ++i)
3404 const struct wined3d_shader_signature_element *input = &shader->input_signature.elements[i];
3406 if (!(reg_maps->input_registers & (1u << input->register_idx)) || !input->semantic_name)
3407 continue;
3409 shader->u.vs.attributes[input->register_idx].usage =
3410 shader_usage_from_semantic_name(input->semantic_name);
3411 shader->u.vs.attributes[input->register_idx].usage_idx = input->semantic_idx;
3414 if (reg_maps->usesrelconstF && !list_empty(&shader->constantsF))
3415 shader->load_local_constsF = TRUE;
3417 return WINED3D_OK;
3420 static HRESULT domain_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3421 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3423 return shader_init(shader, device, desc, 0, WINED3D_SHADER_TYPE_DOMAIN, parent, parent_ops);
3426 static HRESULT hull_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3427 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3429 return shader_init(shader, device, desc, 0, WINED3D_SHADER_TYPE_HULL, parent, parent_ops);
3432 static HRESULT geometry_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3433 const struct wined3d_shader_desc *desc, const struct wined3d_stream_output_desc *so_desc,
3434 void *parent, const struct wined3d_parent_ops *parent_ops)
3436 HRESULT hr;
3438 if (FAILED(hr = shader_init(shader, device, desc, 0, WINED3D_SHADER_TYPE_GEOMETRY, parent, parent_ops)))
3439 return hr;
3441 if (so_desc)
3443 struct wined3d_stream_output_desc *d = &shader->u.gs.so_desc;
3444 *d = *so_desc;
3445 if (!(d->elements = wined3d_calloc(so_desc->element_count, sizeof(*d->elements))))
3447 wined3d_cs_destroy_object(shader->device->cs, wined3d_shader_destroy_object, shader);
3448 return E_OUTOFMEMORY;
3450 memcpy(d->elements, so_desc->elements, so_desc->element_count * sizeof(*d->elements));
3453 return WINED3D_OK;
3456 void find_gs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3457 struct gs_compile_args *args)
3459 args->output_count = state->shader[WINED3D_SHADER_TYPE_PIXEL]
3460 ? state->shader[WINED3D_SHADER_TYPE_PIXEL]->limits->packed_input : shader->limits->packed_output;
3463 void find_ps_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3464 BOOL position_transformed, struct ps_compile_args *args, const struct wined3d_context *context)
3466 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3467 const struct wined3d_gl_info *gl_info = context->gl_info;
3468 const struct wined3d_texture *texture;
3469 unsigned int i;
3471 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
3472 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, state->fb))
3474 static unsigned int warned = 0;
3476 args->srgb_correction = 1;
3477 if (state->render_states[WINED3D_RS_ALPHABLENDENABLE] && !warned++)
3478 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
3479 "support, expect rendering artifacts.\n");
3482 if (shader->reg_maps.shader_version.major == 1
3483 && shader->reg_maps.shader_version.minor <= 3)
3485 for (i = 0; i < shader->limits->sampler; ++i)
3487 DWORD flags = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
3489 if (flags & WINED3D_TTFF_PROJECTED)
3491 DWORD tex_transform = flags & ~WINED3D_TTFF_PROJECTED;
3493 if (!state->shader[WINED3D_SHADER_TYPE_VERTEX])
3495 enum wined3d_shader_resource_type resource_type = shader->reg_maps.resource_info[i].type;
3496 unsigned int j;
3497 unsigned int index = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
3498 DWORD max_valid = WINED3D_TTFF_COUNT4;
3500 for (j = 0; j < state->vertex_declaration->element_count; ++j)
3502 struct wined3d_vertex_declaration_element *element =
3503 &state->vertex_declaration->elements[j];
3505 if (element->usage == WINED3D_DECL_USAGE_TEXCOORD
3506 && element->usage_idx == index)
3508 max_valid = element->format->component_count;
3509 break;
3512 if (!tex_transform || tex_transform > max_valid)
3514 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
3515 tex_transform, max_valid);
3516 tex_transform = max_valid;
3518 if ((resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1D && tex_transform > WINED3D_TTFF_COUNT1)
3519 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D
3520 && tex_transform > WINED3D_TTFF_COUNT2)
3521 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_3D
3522 && tex_transform > WINED3D_TTFF_COUNT3))
3523 tex_transform |= WINED3D_PSARGS_PROJECTED;
3524 else
3526 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
3527 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
3528 i, tex_transform, resource_type);
3531 else
3532 tex_transform = WINED3D_TTFF_COUNT4 | WINED3D_PSARGS_PROJECTED;
3534 args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
3538 if (shader->reg_maps.shader_version.major == 1
3539 && shader->reg_maps.shader_version.minor <= 4)
3541 for (i = 0; i < shader->limits->sampler; ++i)
3543 const struct wined3d_texture *texture = state->textures[i];
3545 if (!shader->reg_maps.resource_info[i].type)
3546 continue;
3548 /* Treat unbound textures as 2D. The dummy texture will provide
3549 * the proper sample value. The tex_types bitmap defaults to
3550 * 2D because of the memset. */
3551 if (!texture)
3552 continue;
3554 switch (texture->target)
3556 /* RECT textures are distinguished from 2D textures via np2_fixup */
3557 case GL_TEXTURE_RECTANGLE_ARB:
3558 case GL_TEXTURE_2D:
3559 break;
3561 case GL_TEXTURE_3D:
3562 args->tex_types |= WINED3D_SHADER_TEX_3D << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
3563 break;
3565 case GL_TEXTURE_CUBE_MAP_ARB:
3566 args->tex_types |= WINED3D_SHADER_TEX_CUBE << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
3567 break;
3572 if (shader->reg_maps.shader_version.major >= 4)
3574 /* In SM4+ we use dcl_sampler in order to determine if we should use shadow sampler. */
3575 args->shadow = 0;
3576 for (i = 0 ; i < MAX_FRAGMENT_SAMPLERS; ++i)
3577 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
3578 args->np2_fixup = 0;
3580 else
3582 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
3584 if (!shader->reg_maps.resource_info[i].type)
3585 continue;
3587 texture = state->textures[i];
3588 if (!texture)
3590 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
3591 continue;
3593 if (can_use_texture_swizzle(gl_info, texture->resource.format))
3594 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
3595 else
3596 args->color_fixup[i] = texture->resource.format->color_fixup;
3598 if (texture->resource.format_flags & WINED3DFMT_FLAG_SHADOW)
3599 args->shadow |= 1u << i;
3601 /* Flag samplers that need NP2 texcoord fixup. */
3602 if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
3603 args->np2_fixup |= (1u << i);
3607 if (shader->reg_maps.shader_version.major >= 3)
3609 if (position_transformed)
3610 args->vp_mode = pretransformed;
3611 else if (use_vs(state))
3612 args->vp_mode = vertexshader;
3613 else
3614 args->vp_mode = fixedfunction;
3615 args->fog = WINED3D_FFP_PS_FOG_OFF;
3617 else
3619 args->vp_mode = vertexshader;
3620 if (state->render_states[WINED3D_RS_FOGENABLE])
3622 switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
3624 case WINED3D_FOG_NONE:
3625 if (position_transformed || use_vs(state))
3627 args->fog = WINED3D_FFP_PS_FOG_LINEAR;
3628 break;
3631 switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
3633 case WINED3D_FOG_NONE: /* Fall through. */
3634 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
3635 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
3636 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
3638 break;
3640 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
3641 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
3642 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
3645 else
3647 args->fog = WINED3D_FFP_PS_FOG_OFF;
3651 if (context->d3d_info->limits.varying_count < wined3d_max_compat_varyings(context->gl_info))
3653 const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3655 args->texcoords_initialized = 0;
3656 for (i = 0; i < MAX_TEXTURES; ++i)
3658 if (vs)
3660 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.output_registers & (1u << i))
3661 args->texcoords_initialized |= 1u << i;
3663 else
3665 const struct wined3d_stream_info *si = &context->stream_info;
3666 unsigned int coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
3668 if ((state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX] >> WINED3D_FFP_TCI_SHIFT)
3669 & WINED3D_FFP_TCI_MASK
3670 || (coord_idx < MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))))
3671 args->texcoords_initialized |= 1u << i;
3675 else
3677 args->texcoords_initialized = (1u << MAX_TEXTURES) - 1;
3680 args->pointsprite = state->render_states[WINED3D_RS_POINTSPRITEENABLE]
3681 && state->gl_primitive_type == GL_POINTS;
3683 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3684 args->alpha_test_func = WINED3D_CMP_ALWAYS - 1;
3685 else
3686 args->alpha_test_func = (state->render_states[WINED3D_RS_ALPHATESTENABLE]
3687 ? wined3d_sanitize_cmp_func(state->render_states[WINED3D_RS_ALPHAFUNC])
3688 : WINED3D_CMP_ALWAYS) - 1;
3690 if (d3d_info->emulated_flatshading)
3691 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
3693 args->render_offscreen = shader->reg_maps.vpos && gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS]
3694 ? context->render_offscreen : 0;
3697 static HRESULT pixel_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3698 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3700 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3701 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
3702 HRESULT hr;
3704 if (FAILED(hr = shader_init(shader, device, desc, device->adapter->d3d_info.limits.ps_uniform_count,
3705 WINED3D_SHADER_TYPE_PIXEL, parent, parent_ops)))
3706 return hr;
3708 for (i = 0; i < MAX_REG_INPUT; ++i)
3710 if (shader->u.ps.input_reg_used & (1u << i))
3712 ++num_regs_used;
3713 highest_reg_used = i;
3717 /* Don't do any register mapping magic if it is not needed, or if we can't
3718 * achieve anything anyway */
3719 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
3720 || num_regs_used > (gl_info->limits.glsl_varyings / 4)
3721 || shader->reg_maps.shader_version.major >= 4)
3723 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
3725 /* This happens with relative addressing. The input mapper function
3726 * warns about this if the higher registers are declared too, so
3727 * don't write a FIXME here */
3728 WARN("More varying registers used than supported\n");
3731 for (i = 0; i < MAX_REG_INPUT; ++i)
3733 shader->u.ps.input_reg_map[i] = i;
3736 shader->u.ps.declared_in_count = highest_reg_used + 1;
3738 else
3740 shader->u.ps.declared_in_count = 0;
3741 for (i = 0; i < MAX_REG_INPUT; ++i)
3743 if (shader->u.ps.input_reg_used & (1u << i))
3744 shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
3745 else shader->u.ps.input_reg_map[i] = ~0U;
3749 return WINED3D_OK;
3752 void pixelshader_update_resource_types(struct wined3d_shader *shader, WORD tex_types)
3754 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3755 struct wined3d_shader_resource_info *resource_info = reg_maps->resource_info;
3756 unsigned int i;
3758 if (reg_maps->shader_version.major != 1) return;
3760 for (i = 0; i < shader->limits->sampler; ++i)
3762 /* We don't sample from this sampler. */
3763 if (!resource_info[i].type)
3764 continue;
3766 switch ((tex_types >> i * WINED3D_PSARGS_TEXTYPE_SHIFT) & WINED3D_PSARGS_TEXTYPE_MASK)
3768 case WINED3D_SHADER_TEX_2D:
3769 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3770 break;
3772 case WINED3D_SHADER_TEX_3D:
3773 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_3D;
3774 break;
3776 case WINED3D_SHADER_TEX_CUBE:
3777 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_CUBE;
3778 break;
3783 static HRESULT compute_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3784 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3786 return shader_init(shader, device, desc, 0, WINED3D_SHADER_TYPE_COMPUTE, parent, parent_ops);
3789 HRESULT CDECL wined3d_shader_create_cs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3790 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3792 struct wined3d_shader *object;
3793 HRESULT hr;
3795 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3796 device, desc, parent, parent_ops, shader);
3798 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
3799 return E_OUTOFMEMORY;
3801 if (FAILED(hr = compute_shader_init(object, device, desc, parent, parent_ops)))
3803 WARN("Failed to initialize compute shader, hr %#x.\n", hr);
3804 HeapFree(GetProcessHeap(), 0, object);
3805 return hr;
3808 TRACE("Created compute shader %p.\n", object);
3809 *shader = object;
3811 return WINED3D_OK;
3814 HRESULT CDECL wined3d_shader_create_ds(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3815 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3817 struct wined3d_shader *object;
3818 HRESULT hr;
3820 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3821 device, desc, parent, parent_ops, shader);
3823 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
3824 return E_OUTOFMEMORY;
3826 if (FAILED(hr = domain_shader_init(object, device, desc, parent, parent_ops)))
3828 WARN("Failed to initialize domain shader, hr %#x.\n", hr);
3829 HeapFree(GetProcessHeap(), 0, object);
3830 return hr;
3833 TRACE("Created domain shader %p.\n", object);
3834 *shader = object;
3836 return WINED3D_OK;
3839 HRESULT CDECL wined3d_shader_create_gs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3840 const struct wined3d_stream_output_desc *so_desc, void *parent,
3841 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3843 struct wined3d_shader *object;
3844 HRESULT hr;
3846 TRACE("device %p, desc %p, so_desc %p, parent %p, parent_ops %p, shader %p.\n",
3847 device, desc, so_desc, parent, parent_ops, shader);
3849 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
3850 return E_OUTOFMEMORY;
3852 if (FAILED(hr = geometry_shader_init(object, device, desc, so_desc, parent, parent_ops)))
3854 WARN("Failed to initialize geometry shader, hr %#x.\n", hr);
3855 HeapFree(GetProcessHeap(), 0, object);
3856 return hr;
3859 TRACE("Created geometry shader %p.\n", object);
3860 *shader = object;
3862 return WINED3D_OK;
3865 HRESULT CDECL wined3d_shader_create_hs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3866 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3868 struct wined3d_shader *object;
3869 HRESULT hr;
3871 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3872 device, desc, parent, parent_ops, shader);
3874 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
3875 return E_OUTOFMEMORY;
3877 if (FAILED(hr = hull_shader_init(object, device, desc, parent, parent_ops)))
3879 WARN("Failed to initialize hull shader, hr %#x.\n", hr);
3880 HeapFree(GetProcessHeap(), 0, object);
3881 return hr;
3884 TRACE("Created hull shader %p.\n", object);
3885 *shader = object;
3887 return WINED3D_OK;
3890 HRESULT CDECL wined3d_shader_create_ps(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3891 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3893 struct wined3d_shader *object;
3894 HRESULT hr;
3896 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3897 device, desc, parent, parent_ops, shader);
3899 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3900 if (!object)
3901 return E_OUTOFMEMORY;
3903 if (FAILED(hr = pixel_shader_init(object, device, desc, parent, parent_ops)))
3905 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3906 HeapFree(GetProcessHeap(), 0, object);
3907 return hr;
3910 TRACE("Created pixel shader %p.\n", object);
3911 *shader = object;
3913 return WINED3D_OK;
3916 HRESULT CDECL wined3d_shader_create_vs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3917 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3919 struct wined3d_shader *object;
3920 HRESULT hr;
3922 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3923 device, desc, parent, parent_ops, shader);
3925 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3926 if (!object)
3927 return E_OUTOFMEMORY;
3929 if (FAILED(hr = vertex_shader_init(object, device, desc, parent, parent_ops)))
3931 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
3932 HeapFree(GetProcessHeap(), 0, object);
3933 return hr;
3936 TRACE("Created vertex shader %p.\n", object);
3937 *shader = object;
3939 return WINED3D_OK;