rasapi32: Update spec file.
[wine/zf.git] / dlls / wined3d / shader.c
blob42a3c1088600c4977bfaf7fcedccf64b1529de5b
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 <stdio.h>
29 #include <string.h>
31 #include "wined3d_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
35 const struct wined3d_vec4 wined3d_srgb_const[] =
37 /* pow, mul_high, sub_high, mul_low */
38 {0.41666f, 1.055f, 0.055f, 12.92f},
39 /* cmp */
40 {0.0031308f, 0.0f, 0.0f, 0.0f},
43 static const char * const shader_opcode_names[] =
45 /* WINED3DSIH_ABS */ "abs",
46 /* WINED3DSIH_ADD */ "add",
47 /* WINED3DSIH_AND */ "and",
48 /* WINED3DSIH_ATOMIC_AND */ "atomic_and",
49 /* WINED3DSIH_ATOMIC_CMP_STORE */ "atomic_cmp_store",
50 /* WINED3DSIH_ATOMIC_IADD */ "atomic_iadd",
51 /* WINED3DSIH_ATOMIC_IMAX */ "atomic_imax",
52 /* WINED3DSIH_ATOMIC_IMIN */ "atomic_imin",
53 /* WINED3DSIH_ATOMIC_OR */ "atomic_or",
54 /* WINED3DSIH_ATOMIC_UMAX */ "atomic_umax",
55 /* WINED3DSIH_ATOMIC_UMIN */ "atomic_umin",
56 /* WINED3DSIH_ATOMIC_XOR */ "atomic_xor",
57 /* WINED3DSIH_BEM */ "bem",
58 /* WINED3DSIH_BFI */ "bfi",
59 /* WINED3DSIH_BFREV */ "bfrev",
60 /* WINED3DSIH_BREAK */ "break",
61 /* WINED3DSIH_BREAKC */ "breakc",
62 /* WINED3DSIH_BREAKP */ "breakp",
63 /* WINED3DSIH_BUFINFO */ "bufinfo",
64 /* WINED3DSIH_CALL */ "call",
65 /* WINED3DSIH_CALLNZ */ "callnz",
66 /* WINED3DSIH_CASE */ "case",
67 /* WINED3DSIH_CMP */ "cmp",
68 /* WINED3DSIH_CND */ "cnd",
69 /* WINED3DSIH_CONTINUE */ "continue",
70 /* WINED3DSIH_CONTINUEP */ "continuec",
71 /* WINED3DSIH_COUNTBITS */ "countbits",
72 /* WINED3DSIH_CRS */ "crs",
73 /* WINED3DSIH_CUT */ "cut",
74 /* WINED3DSIH_CUT_STREAM */ "cut_stream",
75 /* WINED3DSIH_DCL */ "dcl",
76 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
77 /* WINED3DSIH_DCL_FUNCTION_BODY */ "dcl_function_body",
78 /* WINED3DSIH_DCL_FUNCTION_TABLE */ "dcl_function_table",
79 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ "dcl_globalFlags",
80 /* WINED3DSIH_DCL_GS_INSTANCES */ "dcl_gs_instances",
81 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ "dcl_hs_fork_phase_instance_count",
82 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ "dcl_hs_join_phase_instance_count",
83 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ "dcl_hs_max_tessfactor",
84 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ "dcl_immediateConstantBuffer",
85 /* WINED3DSIH_DCL_INDEX_RANGE */ "dcl_index_range",
86 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ "dcl_indexableTemp",
87 /* WINED3DSIH_DCL_INPUT */ "dcl_input",
88 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ "dcl_input_control_point_count",
89 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
90 /* WINED3DSIH_DCL_INPUT_PS */ "dcl_input_ps",
91 /* WINED3DSIH_DCL_INPUT_PS_SGV */ "dcl_input_ps_sgv",
92 /* WINED3DSIH_DCL_INPUT_PS_SIV */ "dcl_input_ps_siv",
93 /* WINED3DSIH_DCL_INPUT_SGV */ "dcl_input_sgv",
94 /* WINED3DSIH_DCL_INPUT_SIV */ "dcl_input_siv",
95 /* WINED3DSIH_DCL_INTERFACE */ "dcl_interface",
96 /* WINED3DSIH_DCL_OUTPUT */ "dcl_output",
97 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ "dcl_output_control_point_count",
98 /* WINED3DSIH_DCL_OUTPUT_SIV */ "dcl_output_siv",
99 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
100 /* WINED3DSIH_DCL_RESOURCE_RAW */ "dcl_resource_raw",
101 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ "dcl_resource_structured",
102 /* WINED3DSIH_DCL_SAMPLER */ "dcl_sampler",
103 /* WINED3DSIH_DCL_STREAM */ "dcl_stream",
104 /* WINED3DSIH_DCL_TEMPS */ "dcl_temps",
105 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ "dcl_tessellator_domain",
106 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ "dcl_tessellator_output_primitive",
107 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ "dcl_tessellator_partitioning",
108 /* WINED3DSIH_DCL_TGSM_RAW */ "dcl_tgsm_raw",
109 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ "dcl_tgsm_structured",
110 /* WINED3DSIH_DCL_THREAD_GROUP */ "dcl_thread_group",
111 /* WINED3DSIH_DCL_UAV_RAW */ "dcl_uav_raw",
112 /* WINED3DSIH_DCL_UAV_STRUCTURED */ "dcl_uav_structured",
113 /* WINED3DSIH_DCL_UAV_TYPED */ "dcl_uav_typed",
114 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
115 /* WINED3DSIH_DEF */ "def",
116 /* WINED3DSIH_DEFAULT */ "default",
117 /* WINED3DSIH_DEFB */ "defb",
118 /* WINED3DSIH_DEFI */ "defi",
119 /* WINED3DSIH_DIV */ "div",
120 /* WINED3DSIH_DP2 */ "dp2",
121 /* WINED3DSIH_DP2ADD */ "dp2add",
122 /* WINED3DSIH_DP3 */ "dp3",
123 /* WINED3DSIH_DP4 */ "dp4",
124 /* WINED3DSIH_DST */ "dst",
125 /* WINED3DSIH_DSX */ "dsx",
126 /* WINED3DSIH_DSX_COARSE */ "deriv_rtx_coarse",
127 /* WINED3DSIH_DSX_FINE */ "deriv_rtx_fine",
128 /* WINED3DSIH_DSY */ "dsy",
129 /* WINED3DSIH_DSY_COARSE */ "deriv_rty_coarse",
130 /* WINED3DSIH_DSY_FINE */ "deriv_rty_fine",
131 /* WINED3DSIH_ELSE */ "else",
132 /* WINED3DSIH_EMIT */ "emit",
133 /* WINED3DSIH_EMIT_STREAM */ "emit_stream",
134 /* WINED3DSIH_ENDIF */ "endif",
135 /* WINED3DSIH_ENDLOOP */ "endloop",
136 /* WINED3DSIH_ENDREP */ "endrep",
137 /* WINED3DSIH_ENDSWITCH */ "endswitch",
138 /* WINED3DSIH_EQ */ "eq",
139 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ "eval_sample_index",
140 /* WINED3DSIH_EXP */ "exp",
141 /* WINED3DSIH_EXPP */ "expp",
142 /* WINED3DSIH_F16TOF32 */ "f16tof32",
143 /* WINED3DSIH_F32TOF16 */ "f32tof16",
144 /* WINED3DSIH_FCALL */ "fcall",
145 /* WINED3DSIH_FIRSTBIT_HI */ "firstbit_hi",
146 /* WINED3DSIH_FIRSTBIT_LO */ "firstbit_lo",
147 /* WINED3DSIH_FIRSTBIT_SHI */ "firstbit_shi",
148 /* WINED3DSIH_FRC */ "frc",
149 /* WINED3DSIH_FTOI */ "ftoi",
150 /* WINED3DSIH_FTOU */ "ftou",
151 /* WINED3DSIH_GATHER4 */ "gather4",
152 /* WINED3DSIH_GATHER4_C */ "gather4_c",
153 /* WINED3DSIH_GATHER4_PO */ "gather4_po",
154 /* WINED3DSIH_GATHER4_PO_C */ "gather4_po_c",
155 /* WINED3DSIH_GE */ "ge",
156 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ "hs_control_point_phase",
157 /* WINED3DSIH_HS_DECLS */ "hs_decls",
158 /* WINED3DSIH_HS_FORK_PHASE */ "hs_fork_phase",
159 /* WINED3DSIH_HS_JOIN_PHASE */ "hs_join_phase",
160 /* WINED3DSIH_IADD */ "iadd",
161 /* WINED3DSIH_IBFE */ "ibfe",
162 /* WINED3DSIH_IEQ */ "ieq",
163 /* WINED3DSIH_IF */ "if",
164 /* WINED3DSIH_IFC */ "ifc",
165 /* WINED3DSIH_IGE */ "ige",
166 /* WINED3DSIH_ILT */ "ilt",
167 /* WINED3DSIH_IMAD */ "imad",
168 /* WINED3DSIH_IMAX */ "imax",
169 /* WINED3DSIH_IMIN */ "imin",
170 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ "imm_atomic_alloc",
171 /* WINED3DSIH_IMM_ATOMIC_AND */ "imm_atomic_and",
172 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ "imm_atomic_cmp_exch",
173 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ "imm_atomic_consume",
174 /* WINED3DSIH_IMM_ATOMIC_EXCH */ "imm_atomic_exch",
175 /* WINED3DSIH_IMM_ATOMIC_IADD */ "imm_atomic_iadd",
176 /* WINED3DSIH_IMM_ATOMIC_IMAX */ "imm_atomic_imax",
177 /* WINED3DSIH_IMM_ATOMIC_IMIN */ "imm_atomic_imin",
178 /* WINED3DSIH_IMM_ATOMIC_OR */ "imm_atomic_or",
179 /* WINED3DSIH_IMM_ATOMIC_UMAX */ "imm_atomic_umax",
180 /* WINED3DSIH_IMM_ATOMIC_UMIN */ "imm_atomic_umin",
181 /* WINED3DSIH_IMM_ATOMIC_XOR */ "imm_atomic_xor",
182 /* WINED3DSIH_IMUL */ "imul",
183 /* WINED3DSIH_INE */ "ine",
184 /* WINED3DSIH_INEG */ "ineg",
185 /* WINED3DSIH_ISHL */ "ishl",
186 /* WINED3DSIH_ISHR */ "ishr",
187 /* WINED3DSIH_ITOF */ "itof",
188 /* WINED3DSIH_LABEL */ "label",
189 /* WINED3DSIH_LD */ "ld",
190 /* WINED3DSIH_LD2DMS */ "ld2dms",
191 /* WINED3DSIH_LD_RAW */ "ld_raw",
192 /* WINED3DSIH_LD_STRUCTURED */ "ld_structured",
193 /* WINED3DSIH_LD_UAV_TYPED */ "ld_uav_typed",
194 /* WINED3DSIH_LIT */ "lit",
195 /* WINED3DSIH_LOD */ "lod",
196 /* WINED3DSIH_LOG */ "log",
197 /* WINED3DSIH_LOGP */ "logp",
198 /* WINED3DSIH_LOOP */ "loop",
199 /* WINED3DSIH_LRP */ "lrp",
200 /* WINED3DSIH_LT */ "lt",
201 /* WINED3DSIH_M3x2 */ "m3x2",
202 /* WINED3DSIH_M3x3 */ "m3x3",
203 /* WINED3DSIH_M3x4 */ "m3x4",
204 /* WINED3DSIH_M4x3 */ "m4x3",
205 /* WINED3DSIH_M4x4 */ "m4x4",
206 /* WINED3DSIH_MAD */ "mad",
207 /* WINED3DSIH_MAX */ "max",
208 /* WINED3DSIH_MIN */ "min",
209 /* WINED3DSIH_MOV */ "mov",
210 /* WINED3DSIH_MOVA */ "mova",
211 /* WINED3DSIH_MOVC */ "movc",
212 /* WINED3DSIH_MUL */ "mul",
213 /* WINED3DSIH_NE */ "ne",
214 /* WINED3DSIH_NOP */ "nop",
215 /* WINED3DSIH_NOT */ "not",
216 /* WINED3DSIH_NRM */ "nrm",
217 /* WINED3DSIH_OR */ "or",
218 /* WINED3DSIH_PHASE */ "phase",
219 /* WINED3DSIH_POW */ "pow",
220 /* WINED3DSIH_RCP */ "rcp",
221 /* WINED3DSIH_REP */ "rep",
222 /* WINED3DSIH_RESINFO */ "resinfo",
223 /* WINED3DSIH_RET */ "ret",
224 /* WINED3DSIH_RETP */ "retp",
225 /* WINED3DSIH_ROUND_NE */ "round_ne",
226 /* WINED3DSIH_ROUND_NI */ "round_ni",
227 /* WINED3DSIH_ROUND_PI */ "round_pi",
228 /* WINED3DSIH_ROUND_Z */ "round_z",
229 /* WINED3DSIH_RSQ */ "rsq",
230 /* WINED3DSIH_SAMPLE */ "sample",
231 /* WINED3DSIH_SAMPLE_B */ "sample_b",
232 /* WINED3DSIH_SAMPLE_C */ "sample_c",
233 /* WINED3DSIH_SAMPLE_C_LZ */ "sample_c_lz",
234 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
235 /* WINED3DSIH_SAMPLE_INFO */ "sample_info",
236 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
237 /* WINED3DSIH_SAMPLE_POS */ "sample_pos",
238 /* WINED3DSIH_SETP */ "setp",
239 /* WINED3DSIH_SGE */ "sge",
240 /* WINED3DSIH_SGN */ "sgn",
241 /* WINED3DSIH_SINCOS */ "sincos",
242 /* WINED3DSIH_SLT */ "slt",
243 /* WINED3DSIH_SQRT */ "sqrt",
244 /* WINED3DSIH_STORE_RAW */ "store_raw",
245 /* WINED3DSIH_STORE_STRUCTURED */ "store_structured",
246 /* WINED3DSIH_STORE_UAV_TYPED */ "store_uav_typed",
247 /* WINED3DSIH_SUB */ "sub",
248 /* WINED3DSIH_SWAPC */ "swapc",
249 /* WINED3DSIH_SWITCH */ "switch",
250 /* WINED3DSIH_SYNC */ "sync",
251 /* WINED3DSIH_TEX */ "texld",
252 /* WINED3DSIH_TEXBEM */ "texbem",
253 /* WINED3DSIH_TEXBEML */ "texbeml",
254 /* WINED3DSIH_TEXCOORD */ "texcrd",
255 /* WINED3DSIH_TEXDEPTH */ "texdepth",
256 /* WINED3DSIH_TEXDP3 */ "texdp3",
257 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
258 /* WINED3DSIH_TEXKILL */ "texkill",
259 /* WINED3DSIH_TEXLDD */ "texldd",
260 /* WINED3DSIH_TEXLDL */ "texldl",
261 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
262 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
263 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
264 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
265 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
266 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
267 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
268 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
269 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
270 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
271 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
272 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
273 /* WINED3DSIH_UBFE */ "ubfe",
274 /* WINED3DSIH_UDIV */ "udiv",
275 /* WINED3DSIH_UGE */ "uge",
276 /* WINED3DSIH_ULT */ "ult",
277 /* WINED3DSIH_UMAX */ "umax",
278 /* WINED3DSIH_UMIN */ "umin",
279 /* WINED3DSIH_UMUL */ "umul",
280 /* WINED3DSIH_USHR */ "ushr",
281 /* WINED3DSIH_UTOF */ "utof",
282 /* WINED3DSIH_XOR */ "xor",
285 static const char * const semantic_names[] =
287 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
288 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
289 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
290 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
291 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
292 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
293 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
294 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
295 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
296 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
297 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
298 /* WINED3D_DECL_USAGE_FOG */ "FOG",
299 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
300 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
303 static const struct
305 enum wined3d_shader_input_sysval_semantic sysval_semantic;
306 const char *sysval_name;
308 shader_input_sysval_semantic_names[] =
310 {WINED3D_SIV_POSITION, "position"},
311 {WINED3D_SIV_CLIP_DISTANCE, "clip_distance"},
312 {WINED3D_SIV_CULL_DISTANCE, "cull_distance"},
313 {WINED3D_SIV_RENDER_TARGET_ARRAY_INDEX, "render_target_array_index"},
314 {WINED3D_SIV_VIEWPORT_ARRAY_INDEX, "viewport_array_index"},
315 {WINED3D_SIV_VERTEX_ID, "vertex_id"},
316 {WINED3D_SIV_INSTANCE_ID, "instance_id"},
317 {WINED3D_SIV_PRIMITIVE_ID, "primitive_id"},
318 {WINED3D_SIV_IS_FRONT_FACE, "is_front_face"},
319 {WINED3D_SIV_SAMPLE_INDEX, "sample_index"},
320 {WINED3D_SIV_QUAD_U0_TESS_FACTOR, "finalQuadUeq0EdgeTessFactor"},
321 {WINED3D_SIV_QUAD_V0_TESS_FACTOR, "finalQuadVeq0EdgeTessFactor"},
322 {WINED3D_SIV_QUAD_U1_TESS_FACTOR, "finalQuadUeq1EdgeTessFactor"},
323 {WINED3D_SIV_QUAD_V1_TESS_FACTOR, "finalQuadVeq1EdgeTessFactor"},
324 {WINED3D_SIV_QUAD_U_INNER_TESS_FACTOR, "finalQuadUInsideTessFactor"},
325 {WINED3D_SIV_QUAD_V_INNER_TESS_FACTOR, "finalQuadVInsideTessFactor"},
326 {WINED3D_SIV_TRIANGLE_U_TESS_FACTOR, "finalTriUeq0EdgeTessFactor"},
327 {WINED3D_SIV_TRIANGLE_V_TESS_FACTOR, "finalTriVeq0EdgeTessFactor"},
328 {WINED3D_SIV_TRIANGLE_W_TESS_FACTOR, "finalTriWeq0EdgeTessFactor"},
329 {WINED3D_SIV_TRIANGLE_INNER_TESS_FACTOR, "finalTriInsideTessFactor"},
330 {WINED3D_SIV_LINE_DETAIL_TESS_FACTOR, "finalLineDetailTessFactor"},
331 {WINED3D_SIV_LINE_DENSITY_TESS_FACTOR, "finalLineDensityTessFactor"},
334 static void shader_dump_src_param(struct wined3d_string_buffer *buffer,
335 const struct wined3d_shader_src_param *param, const struct wined3d_shader_version *shader_version);
337 const char *debug_d3dshaderinstructionhandler(enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx)
339 if (handler_idx >= ARRAY_SIZE(shader_opcode_names))
340 return wine_dbg_sprintf("UNRECOGNIZED(%#x)", handler_idx);
342 return shader_opcode_names[handler_idx];
345 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage)
347 if (usage >= ARRAY_SIZE(semantic_names))
349 FIXME("Unrecognized usage %#x.\n", usage);
350 return "UNRECOGNIZED";
353 return semantic_names[usage];
356 static enum wined3d_decl_usage shader_usage_from_semantic_name(const char *name)
358 unsigned int i;
360 for (i = 0; i < ARRAY_SIZE(semantic_names); ++i)
362 if (!strcmp(name, semantic_names[i]))
363 return i;
366 return ~0U;
369 static enum wined3d_sysval_semantic shader_sysval_semantic_from_usage(enum wined3d_decl_usage usage)
371 switch (usage)
373 case WINED3D_DECL_USAGE_POSITION:
374 return WINED3D_SV_POSITION;
375 default:
376 return 0;
380 BOOL shader_match_semantic(const char *semantic_name, enum wined3d_decl_usage usage)
382 return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
385 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
386 const struct wined3d_shader_semantic *s)
388 e->semantic_name = shader_semantic_name_from_usage(s->usage);
389 e->semantic_idx = s->usage_idx;
390 e->stream_idx = 0;
391 e->sysval_semantic = shader_sysval_semantic_from_usage(s->usage);
392 e->component_type = WINED3D_TYPE_FLOAT;
393 e->register_idx = s->reg.reg.idx[0].offset;
394 e->mask = s->reg.write_mask;
397 static void shader_signature_from_usage(struct wined3d_shader_signature_element *e,
398 enum wined3d_decl_usage usage, UINT usage_idx, UINT reg_idx, DWORD write_mask)
400 e->semantic_name = shader_semantic_name_from_usage(usage);
401 e->semantic_idx = usage_idx;
402 e->stream_idx = 0;
403 e->sysval_semantic = shader_sysval_semantic_from_usage(usage);
404 e->component_type = WINED3D_TYPE_FLOAT;
405 e->register_idx = reg_idx;
406 e->mask = write_mask;
409 static const struct wined3d_shader_frontend *shader_select_frontend(enum wined3d_shader_byte_code_format format)
411 switch (format)
413 case WINED3D_SHADER_BYTE_CODE_FORMAT_SM1:
414 return &sm1_shader_frontend;
416 case WINED3D_SHADER_BYTE_CODE_FORMAT_SM4:
417 return &sm4_shader_frontend;
419 default:
420 WARN("Invalid byte code format %#x specified.\n", format);
421 return NULL;
425 void string_buffer_clear(struct wined3d_string_buffer *buffer)
427 buffer->buffer[0] = '\0';
428 buffer->content_size = 0;
431 BOOL string_buffer_init(struct wined3d_string_buffer *buffer)
433 buffer->buffer_size = 32;
434 if (!(buffer->buffer = heap_alloc(buffer->buffer_size)))
436 ERR("Failed to allocate shader buffer memory.\n");
437 return FALSE;
440 string_buffer_clear(buffer);
441 return TRUE;
444 void string_buffer_free(struct wined3d_string_buffer *buffer)
446 heap_free(buffer->buffer);
449 BOOL string_buffer_resize(struct wined3d_string_buffer *buffer, int rc)
451 char *new_buffer;
452 unsigned int new_buffer_size = buffer->buffer_size * 2;
454 while (rc > 0 && (unsigned int)rc >= new_buffer_size - buffer->content_size)
455 new_buffer_size *= 2;
456 if (!(new_buffer = heap_realloc(buffer->buffer, new_buffer_size)))
458 ERR("Failed to grow buffer.\n");
459 buffer->buffer[buffer->content_size] = '\0';
460 return FALSE;
462 buffer->buffer = new_buffer;
463 buffer->buffer_size = new_buffer_size;
464 return TRUE;
467 int shader_vaddline(struct wined3d_string_buffer *buffer, const char *format, va_list args)
469 unsigned int rem;
470 int rc;
472 rem = buffer->buffer_size - buffer->content_size;
473 rc = vsnprintf(&buffer->buffer[buffer->content_size], rem, format, args);
474 if (rc < 0 /* C89 */ || (unsigned int)rc >= rem /* C99 */)
475 return rc;
477 buffer->content_size += rc;
478 return 0;
481 int shader_addline(struct wined3d_string_buffer *buffer, const char *format, ...)
483 va_list args;
484 int ret;
486 for (;;)
488 va_start(args, format);
489 ret = shader_vaddline(buffer, format, args);
490 va_end(args);
491 if (!ret)
492 return ret;
493 if (!string_buffer_resize(buffer, ret))
494 return -1;
498 struct wined3d_string_buffer *string_buffer_get(struct wined3d_string_buffer_list *list)
500 struct wined3d_string_buffer *buffer;
502 if (list_empty(&list->list))
504 buffer = heap_alloc(sizeof(*buffer));
505 if (!buffer || !string_buffer_init(buffer))
507 ERR("Couldn't allocate buffer for temporary string.\n");
508 heap_free(buffer);
509 return NULL;
512 else
514 buffer = LIST_ENTRY(list_head(&list->list), struct wined3d_string_buffer, entry);
515 list_remove(&buffer->entry);
517 string_buffer_clear(buffer);
518 return buffer;
521 static int string_buffer_vsprintf(struct wined3d_string_buffer *buffer, const char *format, va_list args)
523 if (!buffer)
524 return 0;
525 string_buffer_clear(buffer);
526 return shader_vaddline(buffer, format, args);
529 void string_buffer_sprintf(struct wined3d_string_buffer *buffer, const char *format, ...)
531 va_list args;
532 int ret;
534 for (;;)
536 va_start(args, format);
537 ret = string_buffer_vsprintf(buffer, format, args);
538 va_end(args);
539 if (!ret)
540 return;
541 if (!string_buffer_resize(buffer, ret))
542 return;
546 void string_buffer_release(struct wined3d_string_buffer_list *list, struct wined3d_string_buffer *buffer)
548 if (!buffer)
549 return;
550 list_add_head(&list->list, &buffer->entry);
553 void string_buffer_list_init(struct wined3d_string_buffer_list *list)
555 list_init(&list->list);
558 void string_buffer_list_cleanup(struct wined3d_string_buffer_list *list)
560 struct wined3d_string_buffer *buffer, *buffer_next;
562 LIST_FOR_EACH_ENTRY_SAFE(buffer, buffer_next, &list->list, struct wined3d_string_buffer, entry)
564 string_buffer_free(buffer);
565 heap_free(buffer);
567 list_init(&list->list);
570 /* Convert floating point offset relative to a register file to an absolute
571 * offset for float constants. */
572 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type, UINT register_idx)
574 switch (register_type)
576 case WINED3DSPR_CONST: return register_idx;
577 case WINED3DSPR_CONST2: return 2048 + register_idx;
578 case WINED3DSPR_CONST3: return 4096 + register_idx;
579 case WINED3DSPR_CONST4: return 6144 + register_idx;
580 default:
581 FIXME("Unsupported register type: %u.\n", register_type);
582 return register_idx;
586 static void shader_delete_constant_list(struct list *clist)
588 struct wined3d_shader_lconst *constant, *constant_next;
590 LIST_FOR_EACH_ENTRY_SAFE(constant, constant_next, clist, struct wined3d_shader_lconst, entry)
591 heap_free(constant);
592 list_init(clist);
595 static void shader_set_limits(struct wined3d_shader *shader)
597 static const struct limits_entry
599 unsigned int min_version;
600 unsigned int max_version;
601 struct wined3d_shader_limits limits;
603 vs_limits[] =
605 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
606 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
607 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
608 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
609 * even though they are capable of supporting much more (GL
610 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
611 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
612 * shaders to 256. */
613 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
614 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
615 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 0}},
618 hs_limits[] =
620 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
621 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
623 ds_limits[] =
625 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
626 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
628 gs_limits[] =
630 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
631 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
632 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
635 ps_limits[] =
637 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
638 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
639 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
640 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
641 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
642 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 10}},
643 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 32}},
646 cs_limits[] =
648 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
649 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 0}},
651 const struct limits_entry *limits_array;
652 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
653 shader->reg_maps.shader_version.minor);
654 int i = 0;
656 switch (shader->reg_maps.shader_version.type)
658 default:
659 FIXME("Unexpected shader type %u found.\n", shader->reg_maps.shader_version.type);
660 /* Fall-through. */
661 case WINED3D_SHADER_TYPE_VERTEX:
662 limits_array = vs_limits;
663 break;
664 case WINED3D_SHADER_TYPE_HULL:
665 limits_array = hs_limits;
666 break;
667 case WINED3D_SHADER_TYPE_DOMAIN:
668 limits_array = ds_limits;
669 break;
670 case WINED3D_SHADER_TYPE_GEOMETRY:
671 limits_array = gs_limits;
672 break;
673 case WINED3D_SHADER_TYPE_PIXEL:
674 limits_array = ps_limits;
675 break;
676 case WINED3D_SHADER_TYPE_COMPUTE:
677 limits_array = cs_limits;
678 break;
681 while (limits_array[i].min_version && limits_array[i].min_version <= shader_version)
683 if (shader_version <= limits_array[i].max_version)
685 shader->limits = &limits_array[i].limits;
686 break;
688 ++i;
690 if (!shader->limits)
692 FIXME("Unexpected shader version \"%u.%u\".\n",
693 shader->reg_maps.shader_version.major,
694 shader->reg_maps.shader_version.minor);
695 shader->limits = &limits_array[max(0, i - 1)].limits;
699 static BOOL shader_record_register_usage(struct wined3d_shader *shader, struct wined3d_shader_reg_maps *reg_maps,
700 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type, unsigned int constf_size)
702 switch (reg->type)
704 case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
705 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
706 reg_maps->texcoord |= 1u << reg->idx[0].offset;
707 else
708 reg_maps->address |= 1u << reg->idx[0].offset;
709 break;
711 case WINED3DSPR_TEMP:
712 reg_maps->temporary |= 1u << reg->idx[0].offset;
713 break;
715 case WINED3DSPR_INPUT:
716 if (reg->idx[0].rel_addr)
717 reg_maps->input_rel_addressing = 1;
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
731 reg_maps->input_registers |= 1u << reg->idx[0].offset;
733 break;
735 case WINED3DSPR_RASTOUT:
736 if (reg->idx[0].offset == 1)
737 reg_maps->fog = 1;
738 if (reg->idx[0].offset == 2)
739 reg_maps->point_size = 1;
740 break;
742 case WINED3DSPR_MISCTYPE:
743 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
745 if (!reg->idx[0].offset)
746 reg_maps->vpos = 1;
747 else if (reg->idx[0].offset == 1)
748 reg_maps->usesfacing = 1;
750 break;
752 case WINED3DSPR_CONST:
753 if (reg->idx[0].rel_addr)
755 if (reg->idx[0].offset < reg_maps->min_rel_offset)
756 reg_maps->min_rel_offset = reg->idx[0].offset;
757 if (reg->idx[0].offset > reg_maps->max_rel_offset)
758 reg_maps->max_rel_offset = reg->idx[0].offset;
759 reg_maps->usesrelconstF = TRUE;
761 else
763 if (reg->idx[0].offset >= min(shader->limits->constant_float, constf_size))
765 WARN("Shader using float constant %u which is not supported.\n", reg->idx[0].offset);
766 return FALSE;
768 else
770 wined3d_insert_bits(reg_maps->constf, reg->idx[0].offset, 1, 0x1);
773 break;
775 case WINED3DSPR_CONSTINT:
776 if (reg->idx[0].offset >= shader->limits->constant_int)
778 WARN("Shader using integer constant %u which is not supported.\n", reg->idx[0].offset);
779 return FALSE;
781 else
783 reg_maps->integer_constants |= (1u << reg->idx[0].offset);
785 break;
787 case WINED3DSPR_CONSTBOOL:
788 if (reg->idx[0].offset >= shader->limits->constant_bool)
790 WARN("Shader using bool constant %u which is not supported.\n", reg->idx[0].offset);
791 return FALSE;
793 else
795 reg_maps->boolean_constants |= (1u << reg->idx[0].offset);
797 break;
799 case WINED3DSPR_COLOROUT:
800 reg_maps->rt_mask |= (1u << reg->idx[0].offset);
801 break;
803 case WINED3DSPR_OUTCONTROLPOINT:
804 reg_maps->vocp = 1;
805 break;
807 case WINED3DSPR_SAMPLEMASK:
808 reg_maps->sample_mask = 1;
809 break;
811 default:
812 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
813 reg->type, reg->idx[0].offset, reg->idx[1].offset);
814 break;
816 return TRUE;
819 static void shader_record_sample(struct wined3d_shader_reg_maps *reg_maps,
820 unsigned int resource_idx, unsigned int sampler_idx, unsigned int bind_idx)
822 struct wined3d_shader_sampler_map_entry *entries, *entry;
823 struct wined3d_shader_sampler_map *map;
824 unsigned int i;
826 map = &reg_maps->sampler_map;
827 entries = map->entries;
828 for (i = 0; i < map->count; ++i)
830 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
831 return;
834 if (!map->size)
836 if (!(entries = heap_calloc(4, sizeof(*entries))))
838 ERR("Failed to allocate sampler map entries.\n");
839 return;
841 map->size = 4;
842 map->entries = entries;
844 else if (map->count == map->size)
846 size_t new_size = map->size * 2;
848 if (sizeof(*entries) * new_size <= sizeof(*entries) * map->size
849 || !(entries = heap_realloc(entries, sizeof(*entries) * new_size)))
851 ERR("Failed to resize sampler map entries.\n");
852 return;
854 map->size = new_size;
855 map->entries = entries;
858 entry = &entries[map->count++];
859 entry->resource_idx = resource_idx;
860 entry->sampler_idx = sampler_idx;
861 entry->bind_idx = bind_idx;
864 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
866 switch (instr)
868 case WINED3DSIH_M4x4:
869 case WINED3DSIH_M3x4:
870 return param == 1 ? 3 : 0;
872 case WINED3DSIH_M4x3:
873 case WINED3DSIH_M3x3:
874 return param == 1 ? 2 : 0;
876 case WINED3DSIH_M3x2:
877 return param == 1 ? 1 : 0;
879 default:
880 return 0;
884 static HRESULT shader_reg_maps_add_tgsm(struct wined3d_shader_reg_maps *reg_maps,
885 unsigned int register_idx, unsigned int size, unsigned int stride)
887 struct wined3d_shader_tgsm *tgsm;
889 if (register_idx >= MAX_TGSM_REGISTERS)
891 ERR("Invalid TGSM register index %u.\n", register_idx);
892 return S_OK;
894 if (reg_maps->shader_version.type != WINED3D_SHADER_TYPE_COMPUTE)
896 FIXME("TGSM declarations are allowed only in compute shaders.\n");
897 return S_OK;
900 if (!wined3d_array_reserve((void **)&reg_maps->tgsm, &reg_maps->tgsm_capacity,
901 register_idx + 1, sizeof(*reg_maps->tgsm)))
902 return E_OUTOFMEMORY;
904 reg_maps->tgsm_count = max(register_idx + 1, reg_maps->tgsm_count);
905 tgsm = &reg_maps->tgsm[register_idx];
906 tgsm->size = size;
907 tgsm->stride = stride;
908 return S_OK;
911 static HRESULT shader_record_shader_phase(struct wined3d_shader *shader,
912 struct wined3d_shader_phase **current_phase, const struct wined3d_shader_instruction *ins,
913 const DWORD *current_instruction_ptr, const DWORD *previous_instruction_ptr)
915 struct wined3d_shader_phase *phase;
917 if ((phase = *current_phase))
919 phase->end = previous_instruction_ptr;
920 *current_phase = NULL;
923 if (shader->reg_maps.shader_version.type != WINED3D_SHADER_TYPE_HULL)
925 ERR("Unexpected shader type %s.\n", debug_shader_type(shader->reg_maps.shader_version.type));
926 return E_FAIL;
929 switch (ins->handler_idx)
931 case WINED3DSIH_HS_CONTROL_POINT_PHASE:
932 if (shader->u.hs.phases.control_point)
934 FIXME("Multiple control point phases.\n");
935 heap_free(shader->u.hs.phases.control_point);
937 if (!(shader->u.hs.phases.control_point = heap_alloc_zero(sizeof(*shader->u.hs.phases.control_point))))
938 return E_OUTOFMEMORY;
939 phase = shader->u.hs.phases.control_point;
940 break;
941 case WINED3DSIH_HS_FORK_PHASE:
942 if (!wined3d_array_reserve((void **)&shader->u.hs.phases.fork,
943 &shader->u.hs.phases.fork_size, shader->u.hs.phases.fork_count + 1,
944 sizeof(*shader->u.hs.phases.fork)))
945 return E_OUTOFMEMORY;
946 phase = &shader->u.hs.phases.fork[shader->u.hs.phases.fork_count++];
947 break;
948 case WINED3DSIH_HS_JOIN_PHASE:
949 if (!wined3d_array_reserve((void **)&shader->u.hs.phases.join,
950 &shader->u.hs.phases.join_size, shader->u.hs.phases.join_count + 1,
951 sizeof(*shader->u.hs.phases.join)))
952 return E_OUTOFMEMORY;
953 phase = &shader->u.hs.phases.join[shader->u.hs.phases.join_count++];
954 break;
955 default:
956 ERR("Unexpected opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
957 return E_FAIL;
960 phase->start = current_instruction_ptr;
961 *current_phase = phase;
963 return WINED3D_OK;
966 static HRESULT shader_calculate_clip_or_cull_distance_mask(
967 const struct wined3d_shader_signature_element *e, unsigned int *mask)
969 /* Clip and cull distances are packed in 4 component registers. 0 and 1 are
970 * the only allowed semantic indices.
972 if (e->semantic_idx >= WINED3D_MAX_CLIP_DISTANCES / 4)
974 *mask = 0;
975 WARN("Invalid clip/cull distance index %u.\n", e->semantic_idx);
976 return WINED3DERR_INVALIDCALL;
979 *mask = (e->mask & WINED3DSP_WRITEMASK_ALL) << (4 * e->semantic_idx);
980 return WINED3D_OK;
983 static void wined3d_insert_interpolation_mode(DWORD *packed_interpolation_mode,
984 unsigned int register_idx, enum wined3d_shader_interpolation_mode mode)
986 if (mode > WINED3DSIM_LINEAR_NOPERSPECTIVE_SAMPLE)
987 FIXME("Unexpected interpolation mode %#x.\n", mode);
989 wined3d_insert_bits(packed_interpolation_mode,
990 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT, mode);
993 static HRESULT shader_scan_output_signature(struct wined3d_shader *shader)
995 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
996 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
997 unsigned int i;
998 HRESULT hr;
1000 for (i = 0; i < output_signature->element_count; ++i)
1002 const struct wined3d_shader_signature_element *e = &output_signature->elements[i];
1003 unsigned int mask;
1005 reg_maps->output_registers |= 1u << e->register_idx;
1006 if (e->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
1008 if (FAILED(hr = shader_calculate_clip_or_cull_distance_mask(e, &mask)))
1009 return hr;
1010 reg_maps->clip_distance_mask |= mask;
1012 else if (e->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
1014 if (FAILED(hr = shader_calculate_clip_or_cull_distance_mask(e, &mask)))
1015 return hr;
1016 reg_maps->cull_distance_mask |= mask;
1018 else if (e->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX)
1020 reg_maps->viewport_array = 1;
1024 return WINED3D_OK;
1027 /* Note that this does not count the loop register as an address register. */
1028 static HRESULT shader_get_registers_used(struct wined3d_shader *shader, DWORD constf_size)
1030 struct wined3d_shader_signature_element input_signature_elements[max(MAX_ATTRIBS, MAX_REG_INPUT)];
1031 struct wined3d_shader_signature_element output_signature_elements[MAX_REG_OUTPUT];
1032 struct wined3d_shader_signature *output_signature = &shader->output_signature;
1033 struct wined3d_shader_signature *input_signature = &shader->input_signature;
1034 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1035 const struct wined3d_shader_frontend *fe = shader->frontend;
1036 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
1037 struct wined3d_shader_version shader_version;
1038 struct wined3d_shader_phase *phase = NULL;
1039 const DWORD *ptr, *prev_ins, *current_ins;
1040 void *fe_data = shader->frontend_data;
1041 unsigned int i;
1042 HRESULT hr;
1044 memset(reg_maps, 0, sizeof(*reg_maps));
1045 memset(input_signature_elements, 0, sizeof(input_signature_elements));
1046 memset(output_signature_elements, 0, sizeof(output_signature_elements));
1047 reg_maps->min_rel_offset = ~0U;
1048 list_init(&reg_maps->indexable_temps);
1050 fe->shader_read_header(fe_data, &ptr, &shader_version);
1051 prev_ins = current_ins = ptr;
1052 reg_maps->shader_version = shader_version;
1054 shader_set_limits(shader);
1056 if (!(reg_maps->constf = heap_calloc(((min(shader->limits->constant_float, constf_size) + 31) / 32),
1057 sizeof(*reg_maps->constf))))
1059 ERR("Failed to allocate constant map memory.\n");
1060 return E_OUTOFMEMORY;
1063 while (!fe->shader_is_end(fe_data, &ptr))
1065 struct wined3d_shader_instruction ins;
1067 current_ins = ptr;
1068 /* Fetch opcode. */
1069 fe->shader_read_instruction(fe_data, &ptr, &ins);
1071 /* Unhandled opcode, and its parameters. */
1072 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1074 WARN("Encountered unrecognised or invalid instruction.\n");
1075 return WINED3DERR_INVALIDCALL;
1078 /* Handle declarations. */
1079 if (ins.handler_idx == WINED3DSIH_DCL
1080 || ins.handler_idx == WINED3DSIH_DCL_UAV_TYPED)
1082 struct wined3d_shader_semantic *semantic = &ins.declaration.semantic;
1083 unsigned int reg_idx = semantic->reg.reg.idx[0].offset;
1085 switch (semantic->reg.reg.type)
1087 /* Mark input registers used. */
1088 case WINED3DSPR_INPUT:
1089 if (reg_idx >= MAX_REG_INPUT)
1091 ERR("Invalid input register index %u.\n", reg_idx);
1092 break;
1094 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL && shader_version.major == 3
1095 && semantic->usage == WINED3D_DECL_USAGE_POSITION && !semantic->usage_idx)
1096 return WINED3DERR_INVALIDCALL;
1097 reg_maps->input_registers |= 1u << reg_idx;
1098 shader_signature_from_semantic(&input_signature_elements[reg_idx], semantic);
1099 break;
1101 /* Vertex shader: mark 3.0 output registers used, save token. */
1102 case WINED3DSPR_OUTPUT:
1103 if (reg_idx >= MAX_REG_OUTPUT)
1105 ERR("Invalid output register index %u.\n", reg_idx);
1106 break;
1108 reg_maps->output_registers |= 1u << reg_idx;
1109 shader_signature_from_semantic(&output_signature_elements[reg_idx], semantic);
1110 if (semantic->usage == WINED3D_DECL_USAGE_FOG)
1111 reg_maps->fog = 1;
1112 if (semantic->usage == WINED3D_DECL_USAGE_PSIZE)
1113 reg_maps->point_size = 1;
1114 break;
1116 case WINED3DSPR_SAMPLER:
1117 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
1118 case WINED3DSPR_RESOURCE:
1119 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
1121 ERR("Invalid resource index %u.\n", reg_idx);
1122 break;
1124 reg_maps->resource_info[reg_idx].type = semantic->resource_type;
1125 reg_maps->resource_info[reg_idx].data_type = semantic->resource_data_type;
1126 wined3d_bitmap_set(reg_maps->resource_map, reg_idx);
1127 break;
1129 case WINED3DSPR_UAV:
1130 if (reg_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
1132 ERR("Invalid UAV resource index %u.\n", reg_idx);
1133 break;
1135 reg_maps->uav_resource_info[reg_idx].type = semantic->resource_type;
1136 reg_maps->uav_resource_info[reg_idx].data_type = semantic->resource_data_type;
1137 if (ins.flags)
1138 FIXME("Ignoring typed UAV flags %#x.\n", ins.flags);
1139 break;
1141 default:
1142 TRACE("Not recording DCL register type %#x.\n", semantic->reg.reg.type);
1143 break;
1146 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
1148 struct wined3d_shader_register *reg = &ins.declaration.src.reg;
1149 if (reg->idx[0].offset >= WINED3D_MAX_CBS)
1151 ERR("Invalid CB index %u.\n", reg->idx[0].offset);
1153 else
1155 reg_maps->cb_sizes[reg->idx[0].offset] = reg->idx[1].offset;
1156 wined3d_bitmap_set(&reg_maps->cb_map, reg->idx[0].offset);
1159 else if (ins.handler_idx == WINED3DSIH_DCL_GLOBAL_FLAGS)
1161 if (ins.flags & WINED3DSGF_FORCE_EARLY_DEPTH_STENCIL)
1163 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1164 shader->u.ps.force_early_depth_stencil = TRUE;
1165 else
1166 FIXME("Invalid instruction %#x for shader type %#x.\n",
1167 ins.handler_idx, shader_version.type);
1169 else
1171 WARN("Ignoring global flags %#x.\n", ins.flags);
1174 else if (ins.handler_idx == WINED3DSIH_DCL_GS_INSTANCES)
1176 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1177 shader->u.gs.instance_count = ins.declaration.count;
1178 else
1179 FIXME("Invalid instruction %#x for shader type %#x.\n",
1180 ins.handler_idx, shader_version.type);
1182 else if (ins.handler_idx == WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT
1183 || ins.handler_idx == WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT)
1185 if (phase)
1186 phase->instance_count = ins.declaration.count;
1187 else
1188 FIXME("Instruction %s outside of shader phase.\n",
1189 debug_d3dshaderinstructionhandler(ins.handler_idx));
1191 else if (ins.handler_idx == WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER)
1193 if (reg_maps->icb)
1194 FIXME("Multiple immediate constant buffers.\n");
1195 reg_maps->icb = ins.declaration.icb;
1197 else if (ins.handler_idx == WINED3DSIH_DCL_INDEXABLE_TEMP)
1199 if (phase)
1201 FIXME("Indexable temporary registers not supported.\n");
1203 else
1205 struct wined3d_shader_indexable_temp *reg;
1207 if (!(reg = heap_alloc(sizeof(*reg))))
1208 return E_OUTOFMEMORY;
1210 *reg = ins.declaration.indexable_temp;
1211 list_add_tail(&reg_maps->indexable_temps, &reg->entry);
1214 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE)
1216 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1217 shader->u.gs.input_type = ins.declaration.primitive_type.type;
1218 else
1219 FIXME("Invalid instruction %#x for shader type %#x.\n",
1220 ins.handler_idx, shader_version.type);
1222 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS)
1224 unsigned int reg_idx = ins.declaration.dst.reg.idx[0].offset;
1225 if (reg_idx >= MAX_REG_INPUT)
1227 ERR("Invalid register index %u.\n", reg_idx);
1228 break;
1230 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1231 wined3d_insert_interpolation_mode(shader->u.ps.interpolation_mode, reg_idx, ins.flags);
1232 else
1233 FIXME("Invalid instruction %#x for shader type %#x.\n",
1234 ins.handler_idx, shader_version.type);
1236 else if (ins.handler_idx == WINED3DSIH_DCL_OUTPUT)
1238 if (ins.declaration.dst.reg.type == WINED3DSPR_DEPTHOUT
1239 || ins.declaration.dst.reg.type == WINED3DSPR_DEPTHOUTGE
1240 || ins.declaration.dst.reg.type == WINED3DSPR_DEPTHOUTLE)
1242 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1243 shader->u.ps.depth_output = ins.declaration.dst.reg.type;
1244 else
1245 FIXME("Invalid instruction %#x for shader type %#x.\n",
1246 ins.handler_idx, shader_version.type);
1249 else if (ins.handler_idx == WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT)
1251 if (shader_version.type == WINED3D_SHADER_TYPE_HULL)
1252 shader->u.hs.output_vertex_count = ins.declaration.count;
1253 else
1254 FIXME("Invalid instruction %#x for shader type %#x.\n", ins.handler_idx, shader_version.type);
1256 else if (ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
1258 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1259 shader->u.gs.output_type = ins.declaration.primitive_type.type;
1260 else
1261 FIXME("Invalid instruction %#x for shader type %#x.\n",
1262 ins.handler_idx, shader_version.type);
1264 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_RAW)
1266 unsigned int reg_idx = ins.declaration.dst.reg.idx[0].offset;
1267 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
1269 ERR("Invalid resource index %u.\n", reg_idx);
1270 break;
1272 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_BUFFER;
1273 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1274 reg_maps->resource_info[reg_idx].flags = WINED3D_VIEW_BUFFER_RAW;
1275 wined3d_bitmap_set(reg_maps->resource_map, reg_idx);
1277 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_STRUCTURED)
1279 unsigned int reg_idx = ins.declaration.structured_resource.reg.reg.idx[0].offset;
1280 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
1282 ERR("Invalid resource index %u.\n", reg_idx);
1283 break;
1285 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_BUFFER;
1286 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1287 reg_maps->resource_info[reg_idx].flags = 0;
1288 reg_maps->resource_info[reg_idx].stride = ins.declaration.structured_resource.byte_stride / 4;
1289 wined3d_bitmap_set(reg_maps->resource_map, reg_idx);
1291 else if (ins.handler_idx == WINED3DSIH_DCL_SAMPLER)
1293 if (ins.flags & WINED3DSI_SAMPLER_COMPARISON_MODE)
1294 reg_maps->sampler_comparison_mode |= (1u << ins.declaration.dst.reg.idx[0].offset);
1296 else if (ins.handler_idx == WINED3DSIH_DCL_TEMPS)
1298 if (phase)
1299 phase->temporary_count = ins.declaration.count;
1300 else
1301 reg_maps->temporary_count = ins.declaration.count;
1303 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_DOMAIN)
1305 if (shader_version.type == WINED3D_SHADER_TYPE_DOMAIN)
1306 shader->u.ds.tessellator_domain = ins.declaration.tessellator_domain;
1307 else if (shader_version.type != WINED3D_SHADER_TYPE_HULL)
1308 FIXME("Invalid instruction %#x for shader type %#x.\n", ins.handler_idx, shader_version.type);
1310 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE)
1312 if (shader_version.type == WINED3D_SHADER_TYPE_HULL)
1313 shader->u.hs.tessellator_output_primitive = ins.declaration.tessellator_output_primitive;
1314 else
1315 FIXME("Invalid instruction %#x for shader type %#x.\n", ins.handler_idx, shader_version.type);
1317 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_PARTITIONING)
1319 if (shader_version.type == WINED3D_SHADER_TYPE_HULL)
1320 shader->u.hs.tessellator_partitioning = ins.declaration.tessellator_partitioning;
1321 else
1322 FIXME("Invalid instruction %#x for shader type %#x.\n", ins.handler_idx, shader_version.type);
1324 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_RAW)
1326 if (FAILED(hr = shader_reg_maps_add_tgsm(reg_maps, ins.declaration.tgsm_raw.reg.reg.idx[0].offset,
1327 ins.declaration.tgsm_raw.byte_count / 4, 0)))
1328 return hr;
1330 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_STRUCTURED)
1332 unsigned int stride = ins.declaration.tgsm_structured.byte_stride / 4;
1333 unsigned int size = stride * ins.declaration.tgsm_structured.structure_count;
1334 if (FAILED(hr = shader_reg_maps_add_tgsm(reg_maps,
1335 ins.declaration.tgsm_structured.reg.reg.idx[0].offset, size, stride)))
1336 return hr;
1338 else if (ins.handler_idx == WINED3DSIH_DCL_THREAD_GROUP)
1340 if (shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
1342 shader->u.cs.thread_group_size = ins.declaration.thread_group_size;
1344 else
1346 FIXME("Invalid instruction %#x for shader type %#x.\n",
1347 ins.handler_idx, shader_version.type);
1350 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_RAW)
1352 unsigned int reg_idx = ins.declaration.dst.reg.idx[0].offset;
1353 if (reg_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
1355 ERR("Invalid UAV resource index %u.\n", reg_idx);
1356 break;
1358 if (ins.flags)
1359 FIXME("Ignoring raw UAV flags %#x.\n", ins.flags);
1360 reg_maps->uav_resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_BUFFER;
1361 reg_maps->uav_resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1362 reg_maps->uav_resource_info[reg_idx].flags = WINED3D_VIEW_BUFFER_RAW;
1364 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_STRUCTURED)
1366 unsigned int reg_idx = ins.declaration.structured_resource.reg.reg.idx[0].offset;
1367 if (reg_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
1369 ERR("Invalid UAV resource index %u.\n", reg_idx);
1370 break;
1372 if (ins.flags)
1373 FIXME("Ignoring structured UAV flags %#x.\n", ins.flags);
1374 reg_maps->uav_resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_BUFFER;
1375 reg_maps->uav_resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1376 reg_maps->uav_resource_info[reg_idx].flags = 0;
1377 reg_maps->uav_resource_info[reg_idx].stride = ins.declaration.structured_resource.byte_stride / 4;
1379 else if (ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
1381 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1382 shader->u.gs.vertices_out = ins.declaration.count;
1383 else
1384 FIXME("Invalid instruction %#x for shader type %#x.\n",
1385 ins.handler_idx, shader_version.type);
1387 else if (ins.handler_idx == WINED3DSIH_DEF)
1389 struct wined3d_shader_lconst *lconst;
1390 float *value;
1392 if (!(lconst = heap_alloc(sizeof(*lconst))))
1393 return E_OUTOFMEMORY;
1395 lconst->idx = ins.dst[0].reg.idx[0].offset;
1396 memcpy(lconst->value, ins.src[0].reg.u.immconst_data, 4 * sizeof(DWORD));
1397 value = (float *)lconst->value;
1399 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
1400 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1402 if (value[0] < -1.0f) value[0] = -1.0f;
1403 else if (value[0] > 1.0f) value[0] = 1.0f;
1404 if (value[1] < -1.0f) value[1] = -1.0f;
1405 else if (value[1] > 1.0f) value[1] = 1.0f;
1406 if (value[2] < -1.0f) value[2] = -1.0f;
1407 else if (value[2] > 1.0f) value[2] = 1.0f;
1408 if (value[3] < -1.0f) value[3] = -1.0f;
1409 else if (value[3] > 1.0f) value[3] = 1.0f;
1412 list_add_head(&shader->constantsF, &lconst->entry);
1414 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
1415 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
1417 shader->lconst_inf_or_nan = TRUE;
1420 else if (ins.handler_idx == WINED3DSIH_DEFI)
1422 struct wined3d_shader_lconst *lconst;
1424 if (!(lconst = heap_alloc(sizeof(*lconst))))
1425 return E_OUTOFMEMORY;
1427 lconst->idx = ins.dst[0].reg.idx[0].offset;
1428 memcpy(lconst->value, ins.src[0].reg.u.immconst_data, 4 * sizeof(DWORD));
1430 list_add_head(&shader->constantsI, &lconst->entry);
1431 reg_maps->local_int_consts |= (1u << lconst->idx);
1433 else if (ins.handler_idx == WINED3DSIH_DEFB)
1435 struct wined3d_shader_lconst *lconst;
1437 if (!(lconst = heap_alloc(sizeof(*lconst))))
1438 return E_OUTOFMEMORY;
1440 lconst->idx = ins.dst[0].reg.idx[0].offset;
1441 memcpy(lconst->value, ins.src[0].reg.u.immconst_data, sizeof(DWORD));
1443 list_add_head(&shader->constantsB, &lconst->entry);
1444 reg_maps->local_bool_consts |= (1u << lconst->idx);
1446 /* Handle shader phases. */
1447 else if (ins.handler_idx == WINED3DSIH_HS_CONTROL_POINT_PHASE
1448 || ins.handler_idx == WINED3DSIH_HS_FORK_PHASE
1449 || ins.handler_idx == WINED3DSIH_HS_JOIN_PHASE)
1451 if (FAILED(hr = shader_record_shader_phase(shader, &phase, &ins, current_ins, prev_ins)))
1452 return hr;
1454 /* For subroutine prototypes. */
1455 else if (ins.handler_idx == WINED3DSIH_LABEL)
1457 reg_maps->labels |= 1u << ins.src[0].reg.idx[0].offset;
1459 /* Set texture, address, temporary registers. */
1460 else
1462 BOOL color0_mov = FALSE;
1463 unsigned int i;
1465 /* This will loop over all the registers and try to
1466 * make a bitmask of the ones we're interested in.
1468 * Relative addressing tokens are ignored, but that's
1469 * okay, since we'll catch any address registers when
1470 * they are initialized (required by spec). */
1471 for (i = 0; i < ins.dst_count; ++i)
1473 if (!shader_record_register_usage(shader, reg_maps, &ins.dst[i].reg,
1474 shader_version.type, constf_size))
1475 return WINED3DERR_INVALIDCALL;
1477 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
1479 UINT idx = ins.dst[i].reg.idx[0].offset;
1481 switch (ins.dst[i].reg.type)
1483 case WINED3DSPR_RASTOUT:
1484 if (shader_version.major >= 3)
1485 break;
1486 switch (idx)
1488 case 0: /* oPos */
1489 reg_maps->output_registers |= 1u << 10;
1490 shader_signature_from_usage(&output_signature_elements[10],
1491 WINED3D_DECL_USAGE_POSITION, 0, 10, WINED3DSP_WRITEMASK_ALL);
1492 break;
1494 case 1: /* oFog */
1495 reg_maps->output_registers |= 1u << 11;
1496 shader_signature_from_usage(&output_signature_elements[11],
1497 WINED3D_DECL_USAGE_FOG, 0, 11, WINED3DSP_WRITEMASK_0);
1498 break;
1500 case 2: /* oPts */
1501 reg_maps->output_registers |= 1u << 11;
1502 shader_signature_from_usage(&output_signature_elements[11],
1503 WINED3D_DECL_USAGE_PSIZE, 0, 11, WINED3DSP_WRITEMASK_1);
1504 break;
1506 break;
1508 case WINED3DSPR_ATTROUT:
1509 if (shader_version.major >= 3)
1510 break;
1511 if (idx < 2)
1513 idx += 8;
1514 if (reg_maps->output_registers & (1u << idx))
1516 output_signature_elements[idx].mask |= ins.dst[i].write_mask;
1518 else
1520 reg_maps->output_registers |= 1u << idx;
1521 shader_signature_from_usage(&output_signature_elements[idx],
1522 WINED3D_DECL_USAGE_COLOR, idx - 8, idx, ins.dst[i].write_mask);
1525 break;
1527 case WINED3DSPR_TEXCRDOUT: /* WINED3DSPR_OUTPUT */
1528 if (shader_version.major >= 3)
1530 if (idx >= ARRAY_SIZE(reg_maps->u.output_registers_mask))
1532 WARN("Invalid output register index %u.\n", idx);
1533 break;
1535 reg_maps->u.output_registers_mask[idx] |= ins.dst[i].write_mask;
1536 break;
1538 if (idx >= ARRAY_SIZE(reg_maps->u.texcoord_mask))
1540 WARN("Invalid texcoord index %u.\n", idx);
1541 break;
1543 reg_maps->u.texcoord_mask[idx] |= ins.dst[i].write_mask;
1544 if (reg_maps->output_registers & (1u << idx))
1546 output_signature_elements[idx].mask |= ins.dst[i].write_mask;
1548 else
1550 reg_maps->output_registers |= 1u << idx;
1551 shader_signature_from_usage(&output_signature_elements[idx],
1552 WINED3D_DECL_USAGE_TEXCOORD, idx, idx, ins.dst[i].write_mask);
1554 break;
1556 default:
1557 break;
1561 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1563 if (ins.dst[i].reg.type == WINED3DSPR_COLOROUT && !ins.dst[i].reg.idx[0].offset)
1565 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
1566 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
1567 * the mov and perform the sRGB write correction from the source register.
1569 * However, if the mov is only partial, we can't do this, and if the write
1570 * comes from an instruction other than MOV it is hard to do as well. If
1571 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
1572 shader->u.ps.color0_mov = FALSE;
1573 if (ins.handler_idx == WINED3DSIH_MOV
1574 && ins.dst[i].write_mask == WINED3DSP_WRITEMASK_ALL)
1576 /* Used later when the source register is read. */
1577 color0_mov = TRUE;
1580 /* Also drop the MOV marker if the source register is overwritten prior to the shader
1581 * end
1583 else if (ins.dst[i].reg.type == WINED3DSPR_TEMP
1584 && ins.dst[i].reg.idx[0].offset == shader->u.ps.color0_reg)
1586 shader->u.ps.color0_mov = FALSE;
1590 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
1591 if (shader_version.major == 1
1592 && (ins.handler_idx == WINED3DSIH_TEX
1593 || ins.handler_idx == WINED3DSIH_TEXBEM
1594 || ins.handler_idx == WINED3DSIH_TEXBEML
1595 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
1596 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
1597 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
1598 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
1599 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
1600 || ins.handler_idx == WINED3DSIH_TEXREG2AR
1601 || ins.handler_idx == WINED3DSIH_TEXREG2GB
1602 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
1604 unsigned int reg_idx = ins.dst[i].reg.idx[0].offset;
1606 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
1608 WARN("Invalid 1.x sampler index %u.\n", reg_idx);
1609 continue;
1612 TRACE("Setting fake 2D resource for 1.x pixelshader.\n");
1613 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
1614 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_FLOAT;
1615 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
1616 wined3d_bitmap_set(reg_maps->resource_map, reg_idx);
1618 /* texbem is only valid with < 1.4 pixel shaders */
1619 if (ins.handler_idx == WINED3DSIH_TEXBEM
1620 || ins.handler_idx == WINED3DSIH_TEXBEML)
1622 reg_maps->bumpmat |= 1u << reg_idx;
1623 if (ins.handler_idx == WINED3DSIH_TEXBEML)
1625 reg_maps->luminanceparams |= 1u << reg_idx;
1629 else if (ins.handler_idx == WINED3DSIH_BEM)
1631 reg_maps->bumpmat |= 1u << ins.dst[i].reg.idx[0].offset;
1635 if (ins.handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC || ins.handler_idx == WINED3DSIH_IMM_ATOMIC_CONSUME)
1637 unsigned int reg_idx = ins.src[0].reg.idx[0].offset;
1638 if (reg_idx >= MAX_UNORDERED_ACCESS_VIEWS)
1640 ERR("Invalid UAV index %u.\n", reg_idx);
1641 break;
1643 reg_maps->uav_counter_mask |= (1u << reg_idx);
1645 else if ((WINED3DSIH_ATOMIC_AND <= ins.handler_idx && ins.handler_idx <= WINED3DSIH_ATOMIC_XOR)
1646 || (WINED3DSIH_IMM_ATOMIC_AND <= ins.handler_idx && ins.handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR)
1647 || (ins.handler_idx == WINED3DSIH_BUFINFO && ins.src[0].reg.type == WINED3DSPR_UAV)
1648 || ins.handler_idx == WINED3DSIH_LD_UAV_TYPED
1649 || (ins.handler_idx == WINED3DSIH_LD_RAW && ins.src[1].reg.type == WINED3DSPR_UAV)
1650 || (ins.handler_idx == WINED3DSIH_LD_STRUCTURED && ins.src[2].reg.type == WINED3DSPR_UAV))
1652 const struct wined3d_shader_register *reg;
1654 if (ins.handler_idx == WINED3DSIH_LD_UAV_TYPED || ins.handler_idx == WINED3DSIH_LD_RAW)
1655 reg = &ins.src[1].reg;
1656 else if (ins.handler_idx == WINED3DSIH_LD_STRUCTURED)
1657 reg = &ins.src[2].reg;
1658 else if (WINED3DSIH_ATOMIC_AND <= ins.handler_idx && ins.handler_idx <= WINED3DSIH_ATOMIC_XOR)
1659 reg = &ins.dst[0].reg;
1660 else if (ins.handler_idx == WINED3DSIH_BUFINFO)
1661 reg = &ins.src[0].reg;
1662 else
1663 reg = &ins.dst[1].reg;
1665 if (reg->type == WINED3DSPR_UAV)
1667 if (reg->idx[0].offset >= MAX_UNORDERED_ACCESS_VIEWS)
1669 ERR("Invalid UAV index %u.\n", reg->idx[0].offset);
1670 break;
1672 reg_maps->uav_read_mask |= (1u << reg->idx[0].offset);
1675 else if (ins.handler_idx == WINED3DSIH_NRM)
1677 reg_maps->usesnrm = 1;
1679 else if (ins.handler_idx == WINED3DSIH_DSY
1680 || ins.handler_idx == WINED3DSIH_DSY_COARSE
1681 || ins.handler_idx == WINED3DSIH_DSY_FINE)
1683 reg_maps->usesdsy = 1;
1685 else if (ins.handler_idx == WINED3DSIH_DSX
1686 || ins.handler_idx == WINED3DSIH_DSX_COARSE
1687 || ins.handler_idx == WINED3DSIH_DSX_FINE)
1689 reg_maps->usesdsx = 1;
1691 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
1692 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
1693 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
1694 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
1695 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
1696 else if (ins.handler_idx == WINED3DSIH_POW) reg_maps->usespow = 1;
1697 else if (ins.handler_idx == WINED3DSIH_LOOP
1698 || ins.handler_idx == WINED3DSIH_REP)
1700 ++cur_loop_depth;
1701 if (cur_loop_depth > max_loop_depth)
1702 max_loop_depth = cur_loop_depth;
1704 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
1705 || ins.handler_idx == WINED3DSIH_ENDREP)
1707 --cur_loop_depth;
1709 else if (ins.handler_idx == WINED3DSIH_GATHER4
1710 || ins.handler_idx == WINED3DSIH_GATHER4_C
1711 || ins.handler_idx == WINED3DSIH_SAMPLE
1712 || ins.handler_idx == WINED3DSIH_SAMPLE_B
1713 || ins.handler_idx == WINED3DSIH_SAMPLE_C
1714 || ins.handler_idx == WINED3DSIH_SAMPLE_C_LZ
1715 || ins.handler_idx == WINED3DSIH_SAMPLE_GRAD
1716 || ins.handler_idx == WINED3DSIH_SAMPLE_LOD)
1718 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1719 ins.src[2].reg.idx[0].offset, reg_maps->sampler_map.count);
1721 else if (ins.handler_idx == WINED3DSIH_GATHER4_PO
1722 || ins.handler_idx == WINED3DSIH_GATHER4_PO_C)
1724 shader_record_sample(reg_maps, ins.src[2].reg.idx[0].offset,
1725 ins.src[3].reg.idx[0].offset, reg_maps->sampler_map.count);
1727 else if ((ins.handler_idx == WINED3DSIH_BUFINFO && ins.src[0].reg.type == WINED3DSPR_RESOURCE)
1728 || (ins.handler_idx == WINED3DSIH_SAMPLE_INFO && ins.src[0].reg.type == WINED3DSPR_RESOURCE))
1730 shader_record_sample(reg_maps, ins.src[0].reg.idx[0].offset,
1731 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1733 else if (ins.handler_idx == WINED3DSIH_LD
1734 || ins.handler_idx == WINED3DSIH_LD2DMS
1735 || (ins.handler_idx == WINED3DSIH_LD_RAW && ins.src[1].reg.type == WINED3DSPR_RESOURCE)
1736 || (ins.handler_idx == WINED3DSIH_RESINFO && ins.src[1].reg.type == WINED3DSPR_RESOURCE))
1738 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1739 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1741 else if (ins.handler_idx == WINED3DSIH_LD_STRUCTURED
1742 && ins.src[2].reg.type == WINED3DSPR_RESOURCE)
1744 shader_record_sample(reg_maps, ins.src[2].reg.idx[0].offset,
1745 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1748 if (ins.predicate)
1749 if (!shader_record_register_usage(shader, reg_maps, &ins.predicate->reg,
1750 shader_version.type, constf_size))
1751 return WINED3DERR_INVALIDCALL;
1753 for (i = 0; i < ins.src_count; ++i)
1755 unsigned int count = get_instr_extra_regcount(ins.handler_idx, i);
1756 struct wined3d_shader_register reg = ins.src[i].reg;
1758 if (!shader_record_register_usage(shader, reg_maps, &ins.src[i].reg,
1759 shader_version.type, constf_size))
1760 return WINED3DERR_INVALIDCALL;
1761 while (count)
1763 ++reg.idx[0].offset;
1764 if (!shader_record_register_usage(shader, reg_maps, &reg,
1765 shader_version.type, constf_size))
1766 return WINED3DERR_INVALIDCALL;
1767 --count;
1770 if (color0_mov)
1772 if (ins.src[i].reg.type == WINED3DSPR_TEMP
1773 && ins.src[i].swizzle == WINED3DSP_NOSWIZZLE)
1775 shader->u.ps.color0_mov = TRUE;
1776 shader->u.ps.color0_reg = ins.src[i].reg.idx[0].offset;
1782 prev_ins = current_ins;
1784 reg_maps->loop_depth = max_loop_depth;
1786 if (phase)
1788 phase->end = prev_ins;
1789 phase = NULL;
1792 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1793 * R0 is written to the render target. */
1794 if (shader_version.major < 2 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1795 reg_maps->rt_mask |= (1u << 0);
1797 if (input_signature->elements)
1799 for (i = 0; i < input_signature->element_count; ++i)
1801 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
1803 if (input_signature->elements[i].register_idx >= ARRAY_SIZE(shader->u.vs.attributes))
1805 WARN("Invalid input signature register index %u.\n", input_signature->elements[i].register_idx);
1806 return WINED3DERR_INVALIDCALL;
1809 else if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1811 if (input_signature->elements[i].sysval_semantic == WINED3D_SV_POSITION)
1812 reg_maps->vpos = 1;
1813 else if (input_signature->elements[i].sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
1814 reg_maps->usesfacing = 1;
1816 reg_maps->input_registers |= 1u << input_signature->elements[i].register_idx;
1819 else if (!input_signature->elements && reg_maps->input_registers)
1821 unsigned int count = wined3d_popcount(reg_maps->input_registers);
1822 struct wined3d_shader_signature_element *e;
1823 unsigned int i;
1825 if (!(input_signature->elements = heap_calloc(count, sizeof(*input_signature->elements))))
1826 return E_OUTOFMEMORY;
1827 input_signature->element_count = count;
1829 e = input_signature->elements;
1830 for (i = 0; i < ARRAY_SIZE(input_signature_elements); ++i)
1832 if (!(reg_maps->input_registers & (1u << i)))
1833 continue;
1834 input_signature_elements[i].register_idx = i;
1835 *e++ = input_signature_elements[i];
1839 if (output_signature->elements)
1841 if (FAILED(hr = shader_scan_output_signature(shader)))
1842 return hr;
1844 else if (reg_maps->output_registers)
1846 unsigned int count = wined3d_popcount(reg_maps->output_registers);
1847 struct wined3d_shader_signature_element *e;
1849 if (!(output_signature->elements = heap_calloc(count, sizeof(*output_signature->elements))))
1850 return E_OUTOFMEMORY;
1851 output_signature->element_count = count;
1853 e = output_signature->elements;
1854 for (i = 0; i < ARRAY_SIZE(output_signature_elements); ++i)
1856 if (!(reg_maps->output_registers & (1u << i)))
1857 continue;
1858 *e++ = output_signature_elements[i];
1862 return WINED3D_OK;
1865 static void shader_cleanup_reg_maps(struct wined3d_shader_reg_maps *reg_maps)
1867 struct wined3d_shader_indexable_temp *reg, *reg_next;
1869 heap_free(reg_maps->constf);
1870 heap_free(reg_maps->sampler_map.entries);
1872 LIST_FOR_EACH_ENTRY_SAFE(reg, reg_next, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
1873 heap_free(reg);
1874 list_init(&reg_maps->indexable_temps);
1876 heap_free(reg_maps->tgsm);
1879 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
1881 DWORD map = 1u << max;
1882 map |= map - 1;
1883 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
1885 return wined3d_log2i(map);
1888 static void shader_dump_global_flags(struct wined3d_string_buffer *buffer, DWORD global_flags)
1890 if (global_flags & WINED3DSGF_REFACTORING_ALLOWED)
1892 shader_addline(buffer, "refactoringAllowed");
1893 global_flags &= ~WINED3DSGF_REFACTORING_ALLOWED;
1894 if (global_flags)
1895 shader_addline(buffer, " | ");
1898 if (global_flags & WINED3DSGF_FORCE_EARLY_DEPTH_STENCIL)
1900 shader_addline(buffer, "forceEarlyDepthStencil");
1901 global_flags &= ~WINED3DSGF_FORCE_EARLY_DEPTH_STENCIL;
1902 if (global_flags)
1903 shader_addline(buffer, " | ");
1906 if (global_flags & WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS)
1908 shader_addline(buffer, "enableRawAndStructuredBuffers");
1909 global_flags &= ~WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS;
1912 if (global_flags)
1913 shader_addline(buffer, "unknown_flags(%#x)", global_flags);
1916 static void shader_dump_sync_flags(struct wined3d_string_buffer *buffer, DWORD sync_flags)
1918 if (sync_flags & WINED3DSSF_GLOBAL_UAV)
1920 shader_addline(buffer, "_uglobal");
1921 sync_flags &= ~WINED3DSSF_GLOBAL_UAV;
1923 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
1925 shader_addline(buffer, "_g");
1926 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
1928 if (sync_flags & WINED3DSSF_THREAD_GROUP)
1930 shader_addline(buffer, "_t");
1931 sync_flags &= ~WINED3DSSF_THREAD_GROUP;
1934 if (sync_flags)
1935 shader_addline(buffer, "_unknown_flags(%#x)", sync_flags);
1938 static void shader_dump_precise_flags(struct wined3d_string_buffer *buffer, DWORD flags)
1940 if (!(flags & WINED3DSI_PRECISE_XYZW))
1941 return;
1943 shader_addline(buffer, " [precise");
1944 if (flags != WINED3DSI_PRECISE_XYZW)
1946 shader_addline(buffer, "(%s%s%s%s)",
1947 flags & WINED3DSI_PRECISE_X ? "x" : "",
1948 flags & WINED3DSI_PRECISE_Y ? "y" : "",
1949 flags & WINED3DSI_PRECISE_Z ? "z" : "",
1950 flags & WINED3DSI_PRECISE_W ? "w" : "");
1952 shader_addline(buffer, "]");
1955 static void shader_dump_uav_flags(struct wined3d_string_buffer *buffer, DWORD uav_flags)
1957 if (uav_flags & WINED3DSUF_GLOBALLY_COHERENT)
1959 shader_addline(buffer, "_glc");
1960 uav_flags &= ~WINED3DSUF_GLOBALLY_COHERENT;
1962 if (uav_flags & WINED3DSUF_ORDER_PRESERVING_COUNTER)
1964 shader_addline(buffer, "_opc");
1965 uav_flags &= ~WINED3DSUF_ORDER_PRESERVING_COUNTER;
1968 if (uav_flags)
1969 shader_addline(buffer, "_unknown_flags(%#x)", uav_flags);
1972 static void shader_dump_tessellator_domain(struct wined3d_string_buffer *buffer,
1973 enum wined3d_tessellator_domain domain)
1975 switch (domain)
1977 case WINED3D_TESSELLATOR_DOMAIN_LINE:
1978 shader_addline(buffer, "line");
1979 break;
1980 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
1981 shader_addline(buffer, "triangle");
1982 break;
1983 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
1984 shader_addline(buffer, "quad");
1985 break;
1986 default:
1987 shader_addline(buffer, "unknown_tessellator_domain(%#x)", domain);
1988 break;
1992 static void shader_dump_tessellator_output_primitive(struct wined3d_string_buffer *buffer,
1993 enum wined3d_tessellator_output_primitive output_primitive)
1995 switch (output_primitive)
1997 case WINED3D_TESSELLATOR_OUTPUT_POINT:
1998 shader_addline(buffer, "point");
1999 break;
2000 case WINED3D_TESSELLATOR_OUTPUT_LINE:
2001 shader_addline(buffer, "line");
2002 break;
2003 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
2004 shader_addline(buffer, "triangle_cw");
2005 break;
2006 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
2007 shader_addline(buffer, "triangle_ccw");
2008 break;
2009 default:
2010 shader_addline(buffer, "unknown_tessellator_output_primitive(%#x)", output_primitive);
2011 break;
2015 static void shader_dump_tessellator_partitioning(struct wined3d_string_buffer *buffer,
2016 enum wined3d_tessellator_partitioning partitioning)
2018 switch (partitioning)
2020 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
2021 shader_addline(buffer, "integer");
2022 break;
2023 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
2024 shader_addline(buffer, "pow2");
2025 break;
2026 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
2027 shader_addline(buffer, "fractional_odd");
2028 break;
2029 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
2030 shader_addline(buffer, "fractional_even");
2031 break;
2032 default:
2033 shader_addline(buffer, "unknown_tessellator_partitioning(%#x)", partitioning);
2034 break;
2038 static void shader_dump_shader_input_sysval_semantic(struct wined3d_string_buffer *buffer,
2039 enum wined3d_shader_input_sysval_semantic semantic)
2041 unsigned int i;
2043 for (i = 0; i < ARRAY_SIZE(shader_input_sysval_semantic_names); ++i)
2045 if (shader_input_sysval_semantic_names[i].sysval_semantic == semantic)
2047 shader_addline(buffer, "%s", shader_input_sysval_semantic_names[i].sysval_name);
2048 return;
2052 shader_addline(buffer, "unknown_shader_input_sysval_semantic(%#x)", semantic);
2055 static void shader_dump_resource_type(struct wined3d_string_buffer *buffer, enum wined3d_shader_resource_type type)
2057 static const char *const resource_type_names[] =
2059 /* WINED3D_SHADER_RESOURCE_NONE */ "none",
2060 /* WINED3D_SHADER_RESOURCE_BUFFER */ "buffer",
2061 /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */ "texture1d",
2062 /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */ "texture2d",
2063 /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */ "texture2dms",
2064 /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */ "texture3d",
2065 /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */ "texturecube",
2066 /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */ "texture1darray",
2067 /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */ "texture2darray",
2068 /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */ "texture2dmsarray",
2069 /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */ "texturecubearray",
2072 if (type < ARRAY_SIZE(resource_type_names))
2073 shader_addline(buffer, "%s", resource_type_names[type]);
2074 else
2075 shader_addline(buffer, "unknown");
2078 static void shader_dump_data_type(struct wined3d_string_buffer *buffer, enum wined3d_data_type type)
2080 static const char *const data_type_names[] =
2082 /* WINED3D_DATA_FLOAT */ "(float)",
2083 /* WINED3D_DATA_INT */ "(int)",
2084 /* WINED3D_DATA_RESOURCE */ "(resource)",
2085 /* WINED3D_DATA_SAMPLER */ "(sampler)",
2086 /* WINED3D_DATA_UAV */ "(uav)",
2087 /* WINED3D_DATA_UINT */ "(uint)",
2088 /* WINED3D_DATA_UNORM */ "(unorm)",
2089 /* WINED3D_DATA_SNORM */ "(snorm)",
2090 /* WINED3D_DATA_OPAQUE */ "(opaque)",
2093 if (type < ARRAY_SIZE(data_type_names))
2094 shader_addline(buffer, "%s", data_type_names[type]);
2095 else
2096 shader_addline(buffer, "(unknown)");
2099 static void shader_dump_decl_usage(struct wined3d_string_buffer *buffer,
2100 const struct wined3d_shader_semantic *semantic, unsigned int flags,
2101 const struct wined3d_shader_version *shader_version)
2103 shader_addline(buffer, "dcl");
2105 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
2107 switch (semantic->resource_type)
2109 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2110 shader_addline(buffer, "_2d");
2111 break;
2113 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2114 shader_addline(buffer, "_3d");
2115 break;
2117 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2118 shader_addline(buffer, "_cube");
2119 break;
2121 default:
2122 shader_addline(buffer, "_unknown_resource_type(%#x)", semantic->resource_type);
2123 break;
2126 else if (semantic->reg.reg.type == WINED3DSPR_RESOURCE || semantic->reg.reg.type == WINED3DSPR_UAV)
2128 if (semantic->reg.reg.type == WINED3DSPR_RESOURCE)
2129 shader_addline(buffer, "_resource_");
2130 else
2131 shader_addline(buffer, "_uav_");
2132 shader_dump_resource_type(buffer, semantic->resource_type);
2133 if (semantic->reg.reg.type == WINED3DSPR_UAV)
2134 shader_dump_uav_flags(buffer, flags);
2135 shader_dump_data_type(buffer, semantic->resource_data_type);
2137 else
2139 /* Pixel shaders 3.0 don't have usage semantics. */
2140 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL)
2141 return;
2142 else
2143 shader_addline(buffer, "_");
2145 switch (semantic->usage)
2147 case WINED3D_DECL_USAGE_POSITION:
2148 shader_addline(buffer, "position%u", semantic->usage_idx);
2149 break;
2151 case WINED3D_DECL_USAGE_BLEND_INDICES:
2152 shader_addline(buffer, "blend");
2153 break;
2155 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
2156 shader_addline(buffer, "weight");
2157 break;
2159 case WINED3D_DECL_USAGE_NORMAL:
2160 shader_addline(buffer, "normal%u", semantic->usage_idx);
2161 break;
2163 case WINED3D_DECL_USAGE_PSIZE:
2164 shader_addline(buffer, "psize");
2165 break;
2167 case WINED3D_DECL_USAGE_COLOR:
2168 if (!semantic->usage_idx)
2169 shader_addline(buffer, "color");
2170 else
2171 shader_addline(buffer, "specular%u", (semantic->usage_idx - 1));
2172 break;
2174 case WINED3D_DECL_USAGE_TEXCOORD:
2175 shader_addline(buffer, "texture%u", semantic->usage_idx);
2176 break;
2178 case WINED3D_DECL_USAGE_TANGENT:
2179 shader_addline(buffer, "tangent");
2180 break;
2182 case WINED3D_DECL_USAGE_BINORMAL:
2183 shader_addline(buffer, "binormal");
2184 break;
2186 case WINED3D_DECL_USAGE_TESS_FACTOR:
2187 shader_addline(buffer, "tessfactor");
2188 break;
2190 case WINED3D_DECL_USAGE_POSITIONT:
2191 shader_addline(buffer, "positionT%u", semantic->usage_idx);
2192 break;
2194 case WINED3D_DECL_USAGE_FOG:
2195 shader_addline(buffer, "fog");
2196 break;
2198 case WINED3D_DECL_USAGE_DEPTH:
2199 shader_addline(buffer, "depth");
2200 break;
2202 case WINED3D_DECL_USAGE_SAMPLE:
2203 shader_addline(buffer, "sample");
2204 break;
2206 default:
2207 shader_addline(buffer, "<unknown_semantic(%#x)>", semantic->usage);
2208 FIXME("Unrecognised semantic usage %#x.\n", semantic->usage);
2213 static void shader_dump_register(struct wined3d_string_buffer *buffer,
2214 const struct wined3d_shader_register *reg, const struct wined3d_shader_version *shader_version)
2216 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
2217 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
2218 UINT offset = reg->idx[0].offset;
2220 switch (reg->type)
2222 case WINED3DSPR_TEMP:
2223 shader_addline(buffer, "r");
2224 break;
2226 case WINED3DSPR_INPUT:
2227 shader_addline(buffer, "v");
2228 break;
2230 case WINED3DSPR_CONST:
2231 case WINED3DSPR_CONST2:
2232 case WINED3DSPR_CONST3:
2233 case WINED3DSPR_CONST4:
2234 shader_addline(buffer, "c");
2235 offset = shader_get_float_offset(reg->type, offset);
2236 break;
2238 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
2239 shader_addline(buffer, "%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
2240 break;
2242 case WINED3DSPR_RASTOUT:
2243 shader_addline(buffer, "%s", rastout_reg_names[offset]);
2244 break;
2246 case WINED3DSPR_COLOROUT:
2247 shader_addline(buffer, "oC");
2248 break;
2250 case WINED3DSPR_DEPTHOUT:
2251 shader_addline(buffer, "oDepth");
2252 break;
2254 case WINED3DSPR_DEPTHOUTGE:
2255 shader_addline(buffer, "oDepthGE");
2256 break;
2258 case WINED3DSPR_DEPTHOUTLE:
2259 shader_addline(buffer, "oDepthLE");
2260 break;
2262 case WINED3DSPR_ATTROUT:
2263 shader_addline(buffer, "oD");
2264 break;
2266 case WINED3DSPR_TEXCRDOUT:
2267 /* Vertex shaders >= 3.0 use general purpose output registers
2268 * (WINED3DSPR_OUTPUT), which can include an address token. */
2269 if (shader_version->major >= 3)
2270 shader_addline(buffer, "o");
2271 else
2272 shader_addline(buffer, "oT");
2273 break;
2275 case WINED3DSPR_CONSTINT:
2276 shader_addline(buffer, "i");
2277 break;
2279 case WINED3DSPR_CONSTBOOL:
2280 shader_addline(buffer, "b");
2281 break;
2283 case WINED3DSPR_LABEL:
2284 shader_addline(buffer, "l");
2285 break;
2287 case WINED3DSPR_LOOP:
2288 shader_addline(buffer, "aL");
2289 break;
2291 case WINED3DSPR_SAMPLER:
2292 shader_addline(buffer, "s");
2293 break;
2295 case WINED3DSPR_MISCTYPE:
2296 if (offset > 1)
2298 FIXME("Unhandled misctype register %u.\n", offset);
2299 shader_addline(buffer, "<unhandled misctype %#x>", offset);
2301 else
2303 shader_addline(buffer, "%s", misctype_reg_names[offset]);
2305 break;
2307 case WINED3DSPR_PREDICATE:
2308 shader_addline(buffer, "p");
2309 break;
2311 case WINED3DSPR_IMMCONST:
2312 shader_addline(buffer, "l");
2313 break;
2315 case WINED3DSPR_CONSTBUFFER:
2316 shader_addline(buffer, "cb");
2317 break;
2319 case WINED3DSPR_IMMCONSTBUFFER:
2320 shader_addline(buffer, "icb");
2321 break;
2323 case WINED3DSPR_PRIMID:
2324 shader_addline(buffer, "primID");
2325 break;
2327 case WINED3DSPR_NULL:
2328 shader_addline(buffer, "null");
2329 break;
2331 case WINED3DSPR_RASTERIZER:
2332 shader_addline(buffer, "rasterizer");
2333 break;
2335 case WINED3DSPR_RESOURCE:
2336 shader_addline(buffer, "t");
2337 break;
2339 case WINED3DSPR_UAV:
2340 shader_addline(buffer, "u");
2341 break;
2343 case WINED3DSPR_OUTPOINTID:
2344 shader_addline(buffer, "vOutputControlPointID");
2345 break;
2347 case WINED3DSPR_FORKINSTID:
2348 shader_addline(buffer, "vForkInstanceId");
2349 break;
2351 case WINED3DSPR_JOININSTID:
2352 shader_addline(buffer, "vJoinInstanceId");
2353 break;
2355 case WINED3DSPR_INCONTROLPOINT:
2356 shader_addline(buffer, "vicp");
2357 break;
2359 case WINED3DSPR_OUTCONTROLPOINT:
2360 shader_addline(buffer, "vocp");
2361 break;
2363 case WINED3DSPR_PATCHCONST:
2364 shader_addline(buffer, "vpc");
2365 break;
2367 case WINED3DSPR_TESSCOORD:
2368 shader_addline(buffer, "vDomainLocation");
2369 break;
2371 case WINED3DSPR_GROUPSHAREDMEM:
2372 shader_addline(buffer, "g");
2373 break;
2375 case WINED3DSPR_THREADID:
2376 shader_addline(buffer, "vThreadID");
2377 break;
2379 case WINED3DSPR_THREADGROUPID:
2380 shader_addline(buffer, "vThreadGroupID");
2381 break;
2383 case WINED3DSPR_LOCALTHREADID:
2384 shader_addline(buffer, "vThreadIDInGroup");
2385 break;
2387 case WINED3DSPR_LOCALTHREADINDEX:
2388 shader_addline(buffer, "vThreadIDInGroupFlattened");
2389 break;
2391 case WINED3DSPR_IDXTEMP:
2392 shader_addline(buffer, "x");
2393 break;
2395 case WINED3DSPR_STREAM:
2396 shader_addline(buffer, "m");
2397 break;
2399 case WINED3DSPR_FUNCTIONBODY:
2400 shader_addline(buffer, "fb");
2401 break;
2403 case WINED3DSPR_FUNCTIONPOINTER:
2404 shader_addline(buffer, "fp");
2405 break;
2407 case WINED3DSPR_COVERAGE:
2408 shader_addline(buffer, "vCoverage");
2409 break;
2411 case WINED3DSPR_SAMPLEMASK:
2412 shader_addline(buffer, "oMask");
2413 break;
2415 case WINED3DSPR_GSINSTID:
2416 shader_addline(buffer, "vGSInstanceID");
2417 break;
2419 default:
2420 shader_addline(buffer, "<unhandled_rtype(%#x)>", reg->type);
2421 break;
2424 if (reg->type == WINED3DSPR_IMMCONST)
2426 shader_addline(buffer, "(");
2427 switch (reg->immconst_type)
2429 case WINED3D_IMMCONST_SCALAR:
2430 switch (reg->data_type)
2432 case WINED3D_DATA_FLOAT:
2433 shader_addline(buffer, "%.8e", *(const float *)reg->u.immconst_data);
2434 break;
2435 case WINED3D_DATA_INT:
2436 shader_addline(buffer, "%d", reg->u.immconst_data[0]);
2437 break;
2438 case WINED3D_DATA_RESOURCE:
2439 case WINED3D_DATA_SAMPLER:
2440 case WINED3D_DATA_UINT:
2441 shader_addline(buffer, "%u", reg->u.immconst_data[0]);
2442 break;
2443 default:
2444 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
2445 break;
2447 break;
2449 case WINED3D_IMMCONST_VEC4:
2450 switch (reg->data_type)
2452 case WINED3D_DATA_FLOAT:
2453 shader_addline(buffer, "%.8e, %.8e, %.8e, %.8e",
2454 *(const float *)&reg->u.immconst_data[0], *(const float *)&reg->u.immconst_data[1],
2455 *(const float *)&reg->u.immconst_data[2], *(const float *)&reg->u.immconst_data[3]);
2456 break;
2457 case WINED3D_DATA_INT:
2458 shader_addline(buffer, "%d, %d, %d, %d",
2459 reg->u.immconst_data[0], reg->u.immconst_data[1],
2460 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2461 break;
2462 case WINED3D_DATA_RESOURCE:
2463 case WINED3D_DATA_SAMPLER:
2464 case WINED3D_DATA_UINT:
2465 shader_addline(buffer, "%u, %u, %u, %u",
2466 reg->u.immconst_data[0], reg->u.immconst_data[1],
2467 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2468 break;
2469 default:
2470 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
2471 break;
2473 break;
2475 default:
2476 shader_addline(buffer, "<unhandled immconst_type %#x>", reg->immconst_type);
2477 break;
2479 shader_addline(buffer, ")");
2481 else if (reg->type != WINED3DSPR_RASTOUT
2482 && reg->type != WINED3DSPR_MISCTYPE
2483 && reg->type != WINED3DSPR_NULL)
2485 if (offset != ~0u)
2487 shader_addline(buffer, "[");
2488 if (reg->idx[0].rel_addr)
2490 shader_dump_src_param(buffer, reg->idx[0].rel_addr, shader_version);
2491 shader_addline(buffer, " + ");
2493 shader_addline(buffer, "%u]", offset);
2495 if (reg->idx[1].offset != ~0u)
2497 shader_addline(buffer, "[");
2498 if (reg->idx[1].rel_addr)
2500 shader_dump_src_param(buffer, reg->idx[1].rel_addr, shader_version);
2501 shader_addline(buffer, " + ");
2503 shader_addline(buffer, "%u]", reg->idx[1].offset);
2507 if (reg->type == WINED3DSPR_FUNCTIONPOINTER)
2508 shader_addline(buffer, "[%u]", reg->u.fp_body_idx);
2512 static void shader_dump_dst_param(struct wined3d_string_buffer *buffer,
2513 const struct wined3d_shader_dst_param *param, const struct wined3d_shader_version *shader_version)
2515 DWORD write_mask = param->write_mask;
2517 shader_dump_register(buffer, &param->reg, shader_version);
2519 if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
2521 static const char write_mask_chars[] = "xyzw";
2523 shader_addline(buffer, ".");
2524 if (write_mask & WINED3DSP_WRITEMASK_0)
2525 shader_addline(buffer, "%c", write_mask_chars[0]);
2526 if (write_mask & WINED3DSP_WRITEMASK_1)
2527 shader_addline(buffer, "%c", write_mask_chars[1]);
2528 if (write_mask & WINED3DSP_WRITEMASK_2)
2529 shader_addline(buffer, "%c", write_mask_chars[2]);
2530 if (write_mask & WINED3DSP_WRITEMASK_3)
2531 shader_addline(buffer, "%c", write_mask_chars[3]);
2535 static void shader_dump_src_param(struct wined3d_string_buffer *buffer,
2536 const struct wined3d_shader_src_param *param, const struct wined3d_shader_version *shader_version)
2538 enum wined3d_shader_src_modifier src_modifier = param->modifiers;
2539 DWORD swizzle = param->swizzle;
2541 if (src_modifier == WINED3DSPSM_NEG
2542 || src_modifier == WINED3DSPSM_BIASNEG
2543 || src_modifier == WINED3DSPSM_SIGNNEG
2544 || src_modifier == WINED3DSPSM_X2NEG
2545 || src_modifier == WINED3DSPSM_ABSNEG)
2546 shader_addline(buffer, "-");
2547 else if (src_modifier == WINED3DSPSM_COMP)
2548 shader_addline(buffer, "1-");
2549 else if (src_modifier == WINED3DSPSM_NOT)
2550 shader_addline(buffer, "!");
2552 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
2553 shader_addline(buffer, "abs(");
2555 shader_dump_register(buffer, &param->reg, shader_version);
2557 switch (src_modifier)
2559 case WINED3DSPSM_NONE: break;
2560 case WINED3DSPSM_NEG: break;
2561 case WINED3DSPSM_NOT: break;
2562 case WINED3DSPSM_BIAS: shader_addline(buffer, "_bias"); break;
2563 case WINED3DSPSM_BIASNEG: shader_addline(buffer, "_bias"); break;
2564 case WINED3DSPSM_SIGN: shader_addline(buffer, "_bx2"); break;
2565 case WINED3DSPSM_SIGNNEG: shader_addline(buffer, "_bx2"); break;
2566 case WINED3DSPSM_COMP: break;
2567 case WINED3DSPSM_X2: shader_addline(buffer, "_x2"); break;
2568 case WINED3DSPSM_X2NEG: shader_addline(buffer, "_x2"); break;
2569 case WINED3DSPSM_DZ: shader_addline(buffer, "_dz"); break;
2570 case WINED3DSPSM_DW: shader_addline(buffer, "_dw"); break;
2571 case WINED3DSPSM_ABSNEG: shader_addline(buffer, ")"); break;
2572 case WINED3DSPSM_ABS: shader_addline(buffer, ")"); break;
2573 default: shader_addline(buffer, "_unknown_modifier(%#x)", src_modifier);
2576 if (swizzle != WINED3DSP_NOSWIZZLE)
2578 static const char swizzle_chars[] = "xyzw";
2579 DWORD swizzle_x = swizzle & 0x03;
2580 DWORD swizzle_y = (swizzle >> 2) & 0x03;
2581 DWORD swizzle_z = (swizzle >> 4) & 0x03;
2582 DWORD swizzle_w = (swizzle >> 6) & 0x03;
2584 if (swizzle_x == swizzle_y
2585 && swizzle_x == swizzle_z
2586 && swizzle_x == swizzle_w)
2588 shader_addline(buffer, ".%c", swizzle_chars[swizzle_x]);
2590 else
2592 shader_addline(buffer, ".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
2593 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
2598 /* Shared code in order to generate the bulk of the shader string. */
2599 HRESULT shader_generate_code(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
2600 const struct wined3d_shader_reg_maps *reg_maps, void *backend_ctx,
2601 const DWORD *start, const DWORD *end)
2603 struct wined3d_device *device = shader->device;
2604 const struct wined3d_shader_frontend *fe = shader->frontend;
2605 void *fe_data = shader->frontend_data;
2606 struct wined3d_shader_version shader_version;
2607 struct wined3d_shader_parser_state state;
2608 struct wined3d_shader_instruction ins;
2609 struct wined3d_shader_tex_mx tex_mx;
2610 struct wined3d_shader_context ctx;
2611 const DWORD *ptr;
2613 /* Initialize current parsing state. */
2614 tex_mx.current_row = 0;
2615 state.current_loop_depth = 0;
2616 state.current_loop_reg = 0;
2617 state.in_subroutine = FALSE;
2619 ctx.shader = shader;
2620 ctx.reg_maps = reg_maps;
2621 ctx.buffer = buffer;
2622 ctx.tex_mx = &tex_mx;
2623 ctx.state = &state;
2624 ctx.backend_data = backend_ctx;
2625 ins.ctx = &ctx;
2627 fe->shader_read_header(fe_data, &ptr, &shader_version);
2628 if (start)
2629 ptr = start;
2631 while (!fe->shader_is_end(fe_data, &ptr) && ptr != end)
2633 /* Read opcode. */
2634 fe->shader_read_instruction(fe_data, &ptr, &ins);
2636 /* Unknown opcode and its parameters. */
2637 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
2639 WARN("Encountered unrecognised or invalid instruction.\n");
2640 return WINED3DERR_INVALIDCALL;
2643 if (ins.predicate)
2644 FIXME("Predicates not implemented.\n");
2646 /* Call appropriate function for output target */
2647 device->shader_backend->shader_handle_instruction(&ins);
2650 return WINED3D_OK;
2653 static void shader_dump_ins_modifiers(struct wined3d_string_buffer *buffer,
2654 const struct wined3d_shader_dst_param *dst)
2656 DWORD mmask = dst->modifiers;
2658 switch (dst->shift)
2660 case 0: break;
2661 case 13: shader_addline(buffer, "_d8"); break;
2662 case 14: shader_addline(buffer, "_d4"); break;
2663 case 15: shader_addline(buffer, "_d2"); break;
2664 case 1: shader_addline(buffer, "_x2"); break;
2665 case 2: shader_addline(buffer, "_x4"); break;
2666 case 3: shader_addline(buffer, "_x8"); break;
2667 default: shader_addline(buffer, "_unhandled_shift(%d)", dst->shift); break;
2670 if (mmask & WINED3DSPDM_SATURATE) shader_addline(buffer, "_sat");
2671 if (mmask & WINED3DSPDM_PARTIALPRECISION) shader_addline(buffer, "_pp");
2672 if (mmask & WINED3DSPDM_MSAMPCENTROID) shader_addline(buffer, "_centroid");
2674 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
2675 if (mmask) FIXME("Unrecognised modifier %#x.\n", mmask);
2678 static void shader_dump_primitive_type(struct wined3d_string_buffer *buffer,
2679 const struct wined3d_shader_primitive_type *primitive_type)
2681 switch (primitive_type->type)
2683 case WINED3D_PT_UNDEFINED:
2684 shader_addline(buffer, "undefined");
2685 break;
2686 case WINED3D_PT_POINTLIST:
2687 shader_addline(buffer, "pointlist");
2688 break;
2689 case WINED3D_PT_LINELIST:
2690 shader_addline(buffer, "linelist");
2691 break;
2692 case WINED3D_PT_LINESTRIP:
2693 shader_addline(buffer, "linestrip");
2694 break;
2695 case WINED3D_PT_TRIANGLELIST:
2696 shader_addline(buffer, "trianglelist");
2697 break;
2698 case WINED3D_PT_TRIANGLESTRIP:
2699 shader_addline(buffer, "trianglestrip");
2700 break;
2701 case WINED3D_PT_TRIANGLEFAN:
2702 shader_addline(buffer, "trianglefan");
2703 break;
2704 case WINED3D_PT_LINELIST_ADJ:
2705 shader_addline(buffer, "linelist_adj");
2706 break;
2707 case WINED3D_PT_LINESTRIP_ADJ:
2708 shader_addline(buffer, "linestrip_adj");
2709 break;
2710 case WINED3D_PT_TRIANGLELIST_ADJ:
2711 shader_addline(buffer, "trianglelist_adj");
2712 break;
2713 case WINED3D_PT_TRIANGLESTRIP_ADJ:
2714 shader_addline(buffer, "trianglestrip_adj");
2715 break;
2716 case WINED3D_PT_PATCH:
2717 shader_addline(buffer, "patch%u", primitive_type->patch_vertex_count);
2718 break;
2719 default:
2720 shader_addline(buffer, "<unrecognized_primitive_type %#x>", primitive_type->type);
2721 break;
2725 static void shader_dump_interpolation_mode(struct wined3d_string_buffer *buffer,
2726 enum wined3d_shader_interpolation_mode interpolation_mode)
2728 switch (interpolation_mode)
2730 case WINED3DSIM_CONSTANT:
2731 shader_addline(buffer, "constant");
2732 break;
2733 case WINED3DSIM_LINEAR:
2734 shader_addline(buffer, "linear");
2735 break;
2736 case WINED3DSIM_LINEAR_CENTROID:
2737 shader_addline(buffer, "linear centroid");
2738 break;
2739 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2740 shader_addline(buffer, "linear noperspective");
2741 break;
2742 case WINED3DSIM_LINEAR_SAMPLE:
2743 shader_addline(buffer, "linear sample");
2744 break;
2745 case WINED3DSIM_LINEAR_NOPERSPECTIVE_CENTROID:
2746 shader_addline(buffer, "linear noperspective centroid");
2747 break;
2748 case WINED3DSIM_LINEAR_NOPERSPECTIVE_SAMPLE:
2749 shader_addline(buffer, "linear noperspective sample");
2750 break;
2751 default:
2752 shader_addline(buffer, "<unrecognized_interpolation_mode %#x>", interpolation_mode);
2753 break;
2757 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data)
2759 struct wined3d_shader_version shader_version;
2760 struct wined3d_string_buffer buffer;
2761 const char *type_prefix;
2762 const char *p, *q;
2763 const DWORD *ptr;
2764 DWORD i;
2766 if (!string_buffer_init(&buffer))
2768 ERR("Failed to initialize string buffer.\n");
2769 return;
2772 fe->shader_read_header(fe_data, &ptr, &shader_version);
2774 TRACE("Parsing %p.\n", ptr);
2776 switch (shader_version.type)
2778 case WINED3D_SHADER_TYPE_VERTEX:
2779 type_prefix = "vs";
2780 break;
2782 case WINED3D_SHADER_TYPE_HULL:
2783 type_prefix = "hs";
2784 break;
2786 case WINED3D_SHADER_TYPE_DOMAIN:
2787 type_prefix = "ds";
2788 break;
2790 case WINED3D_SHADER_TYPE_GEOMETRY:
2791 type_prefix = "gs";
2792 break;
2794 case WINED3D_SHADER_TYPE_PIXEL:
2795 type_prefix = "ps";
2796 break;
2798 case WINED3D_SHADER_TYPE_COMPUTE:
2799 type_prefix = "cs";
2800 break;
2802 default:
2803 FIXME("Unhandled shader type %#x.\n", shader_version.type);
2804 type_prefix = "unknown";
2805 break;
2808 shader_addline(&buffer, "%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
2810 while (!fe->shader_is_end(fe_data, &ptr))
2812 struct wined3d_shader_instruction ins;
2814 fe->shader_read_instruction(fe_data, &ptr, &ins);
2815 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
2817 WARN("Skipping unrecognized instruction.\n");
2818 shader_addline(&buffer, "<unrecognized instruction>\n");
2819 continue;
2822 if (ins.handler_idx == WINED3DSIH_DCL || ins.handler_idx == WINED3DSIH_DCL_UAV_TYPED)
2824 shader_dump_decl_usage(&buffer, &ins.declaration.semantic, ins.flags, &shader_version);
2825 shader_dump_ins_modifiers(&buffer, &ins.declaration.semantic.reg);
2826 shader_addline(&buffer, " ");
2827 shader_dump_dst_param(&buffer, &ins.declaration.semantic.reg, &shader_version);
2829 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
2831 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2832 shader_dump_src_param(&buffer, &ins.declaration.src, &shader_version);
2833 shader_addline(&buffer, ", %s",
2834 ins.flags & WINED3DSI_INDEXED_DYNAMIC ? "dynamicIndexed" : "immediateIndexed");
2836 else if (ins.handler_idx == WINED3DSIH_DCL_FUNCTION_BODY)
2838 shader_addline(&buffer, "%s fb%u",
2839 shader_opcode_names[ins.handler_idx], ins.declaration.index);
2841 else if (ins.handler_idx == WINED3DSIH_DCL_FUNCTION_TABLE)
2843 shader_addline(&buffer, "%s ft%u = {...}",
2844 shader_opcode_names[ins.handler_idx], ins.declaration.index);
2846 else if (ins.handler_idx == WINED3DSIH_DCL_GLOBAL_FLAGS)
2848 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2849 shader_dump_global_flags(&buffer, ins.flags);
2851 else if (ins.handler_idx == WINED3DSIH_DCL_HS_MAX_TESSFACTOR)
2853 shader_addline(&buffer, "%s %.8e", shader_opcode_names[ins.handler_idx],
2854 ins.declaration.max_tessellation_factor);
2856 else if (ins.handler_idx == WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER)
2858 shader_addline(&buffer, "%s {\n", shader_opcode_names[ins.handler_idx]);
2859 for (i = 0; i < ins.declaration.icb->vec4_count; ++i)
2861 shader_addline(&buffer, " {0x%08x, 0x%08x, 0x%08x, 0x%08x},\n",
2862 ins.declaration.icb->data[4 * i + 0],
2863 ins.declaration.icb->data[4 * i + 1],
2864 ins.declaration.icb->data[4 * i + 2],
2865 ins.declaration.icb->data[4 * i + 3]);
2867 shader_addline(&buffer, "}");
2869 else if (ins.handler_idx == WINED3DSIH_DCL_INDEX_RANGE)
2871 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2872 shader_dump_dst_param(&buffer, &ins.declaration.index_range.first_register, &shader_version);
2873 shader_addline(&buffer, " %u", ins.declaration.index_range.last_register);
2875 else if (ins.handler_idx == WINED3DSIH_DCL_INDEXABLE_TEMP)
2877 shader_addline(&buffer, "%s x[%u][%u], %u", shader_opcode_names[ins.handler_idx],
2878 ins.declaration.indexable_temp.register_idx,
2879 ins.declaration.indexable_temp.register_size,
2880 ins.declaration.indexable_temp.component_count);
2882 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS)
2884 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2885 shader_dump_interpolation_mode(&buffer, ins.flags);
2886 shader_addline(&buffer, " ");
2887 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2889 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SGV
2890 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SGV
2891 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SIV
2892 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_SIV)
2894 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2895 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2896 shader_addline(&buffer, ", ");
2897 shader_dump_shader_input_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2899 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SIV)
2901 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2902 shader_dump_interpolation_mode(&buffer, ins.flags);
2903 shader_addline(&buffer, " ");
2904 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2905 shader_addline(&buffer, ", ");
2906 shader_dump_shader_input_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2908 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT
2909 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT)
2911 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2912 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2914 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE
2915 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
2917 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2918 shader_dump_primitive_type(&buffer, &ins.declaration.primitive_type);
2920 else if (ins.handler_idx == WINED3DSIH_DCL_INTERFACE)
2922 shader_addline(&buffer, "%s fp[%u][%u][%u] = {...}",
2923 shader_opcode_names[ins.handler_idx], ins.declaration.fp.index,
2924 ins.declaration.fp.array_size, ins.declaration.fp.body_count);
2926 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_RAW)
2928 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2929 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2931 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_STRUCTURED)
2933 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2934 shader_dump_dst_param(&buffer, &ins.declaration.structured_resource.reg, &shader_version);
2935 shader_addline(&buffer, ", %u", ins.declaration.structured_resource.byte_stride);
2937 else if (ins.handler_idx == WINED3DSIH_DCL_SAMPLER)
2939 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2940 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2941 if (ins.flags == WINED3DSI_SAMPLER_COMPARISON_MODE)
2942 shader_addline(&buffer, ", comparisonMode");
2944 else if (ins.handler_idx == WINED3DSIH_DCL_TEMPS
2945 || ins.handler_idx == WINED3DSIH_DCL_GS_INSTANCES
2946 || ins.handler_idx == WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT
2947 || ins.handler_idx == WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT
2948 || ins.handler_idx == WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT
2949 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT
2950 || ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
2952 shader_addline(&buffer, "%s %u", shader_opcode_names[ins.handler_idx], ins.declaration.count);
2954 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_DOMAIN)
2956 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2957 shader_dump_tessellator_domain(&buffer, ins.declaration.tessellator_domain);
2959 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE)
2961 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2962 shader_dump_tessellator_output_primitive(&buffer, ins.declaration.tessellator_output_primitive);
2964 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_PARTITIONING)
2966 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2967 shader_dump_tessellator_partitioning(&buffer, ins.declaration.tessellator_partitioning);
2969 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_RAW)
2971 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2972 shader_dump_dst_param(&buffer, &ins.declaration.tgsm_raw.reg, &shader_version);
2973 shader_addline(&buffer, ", %u", ins.declaration.tgsm_raw.byte_count);
2975 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_STRUCTURED)
2977 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2978 shader_dump_dst_param(&buffer, &ins.declaration.tgsm_structured.reg, &shader_version);
2979 shader_addline(&buffer, ", %u, %u", ins.declaration.tgsm_structured.byte_stride,
2980 ins.declaration.tgsm_structured.structure_count);
2982 else if (ins.handler_idx == WINED3DSIH_DCL_THREAD_GROUP)
2984 shader_addline(&buffer, "%s %u, %u, %u", shader_opcode_names[ins.handler_idx],
2985 ins.declaration.thread_group_size.x,
2986 ins.declaration.thread_group_size.y,
2987 ins.declaration.thread_group_size.z);
2989 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_RAW)
2991 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2992 shader_dump_uav_flags(&buffer, ins.flags);
2993 shader_addline(&buffer, " ");
2994 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2996 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_STRUCTURED)
2998 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2999 shader_dump_uav_flags(&buffer, ins.flags);
3000 shader_addline(&buffer, " ");
3001 shader_dump_dst_param(&buffer, &ins.declaration.structured_resource.reg, &shader_version);
3002 shader_addline(&buffer, ", %u", ins.declaration.structured_resource.byte_stride);
3004 else if (ins.handler_idx == WINED3DSIH_DEF)
3006 shader_addline(&buffer, "def c%u = %.8e, %.8e, %.8e, %.8e", shader_get_float_offset(ins.dst[0].reg.type,
3007 ins.dst[0].reg.idx[0].offset),
3008 *(const float *)&ins.src[0].reg.u.immconst_data[0],
3009 *(const float *)&ins.src[0].reg.u.immconst_data[1],
3010 *(const float *)&ins.src[0].reg.u.immconst_data[2],
3011 *(const float *)&ins.src[0].reg.u.immconst_data[3]);
3013 else if (ins.handler_idx == WINED3DSIH_DEFI)
3015 shader_addline(&buffer, "defi i%u = %d, %d, %d, %d", ins.dst[0].reg.idx[0].offset,
3016 ins.src[0].reg.u.immconst_data[0],
3017 ins.src[0].reg.u.immconst_data[1],
3018 ins.src[0].reg.u.immconst_data[2],
3019 ins.src[0].reg.u.immconst_data[3]);
3021 else if (ins.handler_idx == WINED3DSIH_DEFB)
3023 shader_addline(&buffer, "defb b%u = %s",
3024 ins.dst[0].reg.idx[0].offset, ins.src[0].reg.u.immconst_data[0] ? "true" : "false");
3026 else
3028 if (ins.predicate)
3030 shader_addline(&buffer, "(");
3031 shader_dump_src_param(&buffer, ins.predicate, &shader_version);
3032 shader_addline(&buffer, ") ");
3035 /* PixWin marks instructions with the coissue flag with a '+' */
3036 if (ins.coissue)
3037 shader_addline(&buffer, "+");
3039 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
3041 if (ins.handler_idx == WINED3DSIH_BREAKP
3042 || ins.handler_idx == WINED3DSIH_CONTINUEP
3043 || ins.handler_idx == WINED3DSIH_IF
3044 || ins.handler_idx == WINED3DSIH_RETP
3045 || ins.handler_idx == WINED3DSIH_TEXKILL)
3047 switch (ins.flags)
3049 case WINED3D_SHADER_CONDITIONAL_OP_NZ: shader_addline(&buffer, "_nz"); break;
3050 case WINED3D_SHADER_CONDITIONAL_OP_Z: shader_addline(&buffer, "_z"); break;
3051 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags); break;
3054 else if (ins.handler_idx == WINED3DSIH_IFC
3055 || ins.handler_idx == WINED3DSIH_BREAKC)
3057 switch (ins.flags)
3059 case WINED3D_SHADER_REL_OP_GT: shader_addline(&buffer, "_gt"); break;
3060 case WINED3D_SHADER_REL_OP_EQ: shader_addline(&buffer, "_eq"); break;
3061 case WINED3D_SHADER_REL_OP_GE: shader_addline(&buffer, "_ge"); break;
3062 case WINED3D_SHADER_REL_OP_LT: shader_addline(&buffer, "_lt"); break;
3063 case WINED3D_SHADER_REL_OP_NE: shader_addline(&buffer, "_ne"); break;
3064 case WINED3D_SHADER_REL_OP_LE: shader_addline(&buffer, "_le"); break;
3065 default: shader_addline(&buffer, "_(%u)", ins.flags);
3068 else if (ins.handler_idx == WINED3DSIH_TEX
3069 && shader_version.major >= 2
3070 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
3072 shader_addline(&buffer, "p");
3074 else if (ins.handler_idx == WINED3DSIH_RESINFO && ins.flags)
3076 switch (ins.flags)
3078 case WINED3DSI_RESINFO_RCP_FLOAT: shader_addline(&buffer, "_rcpFloat"); break;
3079 case WINED3DSI_RESINFO_UINT: shader_addline(&buffer, "_uint"); break;
3080 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags);
3083 else if (ins.handler_idx == WINED3DSIH_SAMPLE_INFO && ins.flags)
3085 switch (ins.flags)
3087 case WINED3DSI_SAMPLE_INFO_UINT: shader_addline(&buffer, "_uint"); break;
3088 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags);
3091 else if (ins.handler_idx == WINED3DSIH_SYNC)
3093 shader_dump_sync_flags(&buffer, ins.flags);
3095 else
3097 shader_dump_precise_flags(&buffer, ins.flags);
3100 if (wined3d_shader_instruction_has_texel_offset(&ins))
3101 shader_addline(&buffer, "(%d,%d,%d)", ins.texel_offset.u, ins.texel_offset.v, ins.texel_offset.w);
3103 if (ins.resource_type != WINED3D_SHADER_RESOURCE_NONE)
3105 shader_addline(&buffer, "(");
3106 shader_dump_resource_type(&buffer, ins.resource_type);
3107 shader_addline(&buffer, ")");
3110 if (ins.resource_data_type != WINED3D_DATA_FLOAT)
3111 shader_dump_data_type(&buffer, ins.resource_data_type);
3113 for (i = 0; i < ins.dst_count; ++i)
3115 shader_dump_ins_modifiers(&buffer, &ins.dst[i]);
3116 shader_addline(&buffer, !i ? " " : ", ");
3117 shader_dump_dst_param(&buffer, &ins.dst[i], &shader_version);
3120 /* Other source tokens */
3121 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
3123 shader_addline(&buffer, !i ? " " : ", ");
3124 shader_dump_src_param(&buffer, &ins.src[i - ins.dst_count], &shader_version);
3127 shader_addline(&buffer, "\n");
3130 for (p = buffer.buffer; *p; p = q)
3132 if (!(q = strstr(p, "\n")))
3133 q = p + strlen(p);
3134 else
3135 ++q;
3136 TRACE(" %.*s", (int)(q - p), p);
3139 string_buffer_free(&buffer);
3142 static void shader_cleanup(struct wined3d_shader *shader)
3144 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_HULL)
3146 heap_free(shader->u.hs.phases.control_point);
3147 heap_free(shader->u.hs.phases.fork);
3148 heap_free(shader->u.hs.phases.join);
3151 heap_free(shader->patch_constant_signature.elements);
3152 heap_free(shader->output_signature.elements);
3153 heap_free(shader->input_signature.elements);
3154 shader->device->shader_backend->shader_destroy(shader);
3155 shader_cleanup_reg_maps(&shader->reg_maps);
3156 heap_free(shader->byte_code);
3157 shader_delete_constant_list(&shader->constantsF);
3158 shader_delete_constant_list(&shader->constantsB);
3159 shader_delete_constant_list(&shader->constantsI);
3160 list_remove(&shader->shader_list_entry);
3162 if (shader->frontend && shader->frontend_data)
3163 shader->frontend->shader_free(shader->frontend_data);
3166 struct shader_none_priv
3168 const struct wined3d_vertex_pipe_ops *vertex_pipe;
3169 const struct wined3d_fragment_pipe_ops *fragment_pipe;
3170 BOOL ffp_proj_control;
3173 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
3174 static void shader_none_precompile(void *shader_priv, struct wined3d_shader *shader) {}
3175 static void shader_none_select_compute(void *shader_priv, struct wined3d_context *context,
3176 const struct wined3d_state *state) {}
3177 static void shader_none_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count) {}
3178 static void shader_none_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count) {}
3179 static void shader_none_load_constants(void *shader_priv, struct wined3d_context *context,
3180 const struct wined3d_state *state) {}
3181 static void shader_none_destroy(struct wined3d_shader *shader) {}
3182 static void shader_none_free_context_data(struct wined3d_context *context) {}
3183 static void shader_none_init_context_state(struct wined3d_context *context) {}
3185 /* Context activation is done by the caller. */
3186 static void shader_none_select(void *shader_priv, struct wined3d_context *context,
3187 const struct wined3d_state *state)
3189 struct shader_none_priv *priv = shader_priv;
3191 priv->vertex_pipe->vp_enable(context, !use_vs(state));
3192 priv->fragment_pipe->fp_enable(context, !use_ps(state));
3195 /* Context activation is done by the caller. */
3196 static void shader_none_disable(void *shader_priv, struct wined3d_context *context)
3198 struct shader_none_priv *priv = shader_priv;
3200 priv->vertex_pipe->vp_enable(context, FALSE);
3201 priv->fragment_pipe->fp_enable(context, FALSE);
3203 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
3204 | (1u << WINED3D_SHADER_TYPE_VERTEX)
3205 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
3206 | (1u << WINED3D_SHADER_TYPE_HULL)
3207 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
3208 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
3211 static HRESULT shader_none_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
3212 const struct wined3d_fragment_pipe_ops *fragment_pipe)
3214 struct fragment_caps fragment_caps;
3215 void *vertex_priv, *fragment_priv;
3216 struct shader_none_priv *priv;
3218 if (!(priv = heap_alloc(sizeof(*priv))))
3219 return E_OUTOFMEMORY;
3221 if (!(vertex_priv = vertex_pipe->vp_alloc(&none_shader_backend, priv)))
3223 ERR("Failed to initialize vertex pipe.\n");
3224 heap_free(priv);
3225 return E_FAIL;
3228 if (!(fragment_priv = fragment_pipe->alloc_private(&none_shader_backend, priv)))
3230 ERR("Failed to initialize fragment pipe.\n");
3231 vertex_pipe->vp_free(device, NULL);
3232 heap_free(priv);
3233 return E_FAIL;
3236 priv->vertex_pipe = vertex_pipe;
3237 priv->fragment_pipe = fragment_pipe;
3238 fragment_pipe->get_caps(device->adapter, &fragment_caps);
3239 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
3241 device->vertex_priv = vertex_priv;
3242 device->fragment_priv = fragment_priv;
3243 device->shader_priv = priv;
3245 return WINED3D_OK;
3248 static void shader_none_free(struct wined3d_device *device, struct wined3d_context *context)
3250 struct shader_none_priv *priv = device->shader_priv;
3252 priv->fragment_pipe->free_private(device, context);
3253 priv->vertex_pipe->vp_free(device, context);
3254 heap_free(priv);
3257 static BOOL shader_none_allocate_context_data(struct wined3d_context *context)
3259 return TRUE;
3262 static void shader_none_get_caps(const struct wined3d_adapter *adapter, struct shader_caps *caps)
3264 /* Set the shader caps to 0 for the none shader backend */
3265 memset(caps, 0, sizeof(*caps));
3268 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
3270 /* We "support" every possible fixup, since we don't support any shader
3271 * model, and will never have to actually sample a texture. */
3272 return TRUE;
3275 static BOOL shader_none_has_ffp_proj_control(void *shader_priv)
3277 struct shader_none_priv *priv = shader_priv;
3279 return priv->ffp_proj_control;
3282 const struct wined3d_shader_backend_ops none_shader_backend =
3284 shader_none_handle_instruction,
3285 shader_none_precompile,
3286 shader_none_select,
3287 shader_none_select_compute,
3288 shader_none_disable,
3289 shader_none_update_float_vertex_constants,
3290 shader_none_update_float_pixel_constants,
3291 shader_none_load_constants,
3292 shader_none_destroy,
3293 shader_none_alloc,
3294 shader_none_free,
3295 shader_none_allocate_context_data,
3296 shader_none_free_context_data,
3297 shader_none_init_context_state,
3298 shader_none_get_caps,
3299 shader_none_color_fixup_supported,
3300 shader_none_has_ffp_proj_control,
3303 static unsigned int shader_max_version_from_feature_level(enum wined3d_feature_level level)
3305 switch (level)
3307 case WINED3D_FEATURE_LEVEL_11_1:
3308 case WINED3D_FEATURE_LEVEL_11:
3309 return 5;
3310 case WINED3D_FEATURE_LEVEL_10_1:
3311 case WINED3D_FEATURE_LEVEL_10:
3312 return 4;
3313 case WINED3D_FEATURE_LEVEL_9_3:
3314 return 3;
3315 case WINED3D_FEATURE_LEVEL_9_2:
3316 case WINED3D_FEATURE_LEVEL_9_1:
3317 return 2;
3318 default:
3319 return 1;
3323 static HRESULT shader_set_function(struct wined3d_shader *shader, struct wined3d_device *device,
3324 enum wined3d_shader_type type, unsigned int float_const_count)
3326 const struct wined3d_d3d_info *d3d_info = &shader->device->adapter->d3d_info;
3327 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3328 const struct wined3d_shader_version *version = &reg_maps->shader_version;
3329 const struct wined3d_shader_frontend *fe;
3330 unsigned int backend_version;
3331 HRESULT hr;
3333 TRACE("shader %p, device %p, type %s, float_const_count %u.\n",
3334 shader, device, debug_shader_type(type), float_const_count);
3336 fe = shader->frontend;
3337 if (!(shader->frontend_data = fe->shader_init(shader->function,
3338 shader->functionLength, &shader->output_signature)))
3340 FIXME("Failed to initialize frontend.\n");
3341 return WINED3DERR_INVALIDCALL;
3344 /* First pass: trace shader. */
3345 if (TRACE_ON(d3d_shader))
3346 shader_trace_init(fe, shader->frontend_data);
3348 /* Second pass: figure out which registers are used, what the semantics are, etc. */
3349 if (FAILED(hr = shader_get_registers_used(shader, float_const_count)))
3350 return hr;
3352 if (version->type != type)
3354 WARN("Wrong shader type %s.\n", debug_shader_type(reg_maps->shader_version.type));
3355 return WINED3DERR_INVALIDCALL;
3357 if (version->major > shader_max_version_from_feature_level(device->cs->c.state->feature_level))
3359 WARN("Shader version %u not supported by this device.\n", version->major);
3360 return WINED3DERR_INVALIDCALL;
3362 switch (type)
3364 case WINED3D_SHADER_TYPE_VERTEX:
3365 backend_version = d3d_info->limits.vs_version;
3366 break;
3367 case WINED3D_SHADER_TYPE_HULL:
3368 backend_version = d3d_info->limits.hs_version;
3369 break;
3370 case WINED3D_SHADER_TYPE_DOMAIN:
3371 backend_version = d3d_info->limits.ds_version;
3372 break;
3373 case WINED3D_SHADER_TYPE_GEOMETRY:
3374 backend_version = d3d_info->limits.gs_version;
3375 break;
3376 case WINED3D_SHADER_TYPE_PIXEL:
3377 backend_version = d3d_info->limits.ps_version;
3378 break;
3379 case WINED3D_SHADER_TYPE_COMPUTE:
3380 backend_version = d3d_info->limits.cs_version;
3381 break;
3382 default:
3383 FIXME("No backend version-checking for this shader type.\n");
3384 backend_version = 0;
3386 if (version->major > backend_version)
3388 WARN("Shader version %u.%u not supported by the current shader backend.\n",
3389 version->major, version->minor);
3390 return WINED3DERR_INVALIDCALL;
3393 shader->load_local_constsF = shader->lconst_inf_or_nan;
3395 return WINED3D_OK;
3398 ULONG CDECL wined3d_shader_incref(struct wined3d_shader *shader)
3400 ULONG refcount = InterlockedIncrement(&shader->ref);
3402 TRACE("%p increasing refcount to %u.\n", shader, refcount);
3404 return refcount;
3407 static void wined3d_shader_init_object(void *object)
3409 struct wined3d_shader *shader = object;
3410 struct wined3d_device *device = shader->device;
3412 TRACE("shader %p.\n", shader);
3414 list_add_head(&device->shaders, &shader->shader_list_entry);
3416 device->shader_backend->shader_precompile(device->shader_priv, shader);
3419 static void wined3d_shader_destroy_object(void *object)
3421 TRACE("object %p.\n", object);
3423 shader_cleanup(object);
3424 heap_free(object);
3427 ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
3429 ULONG refcount = InterlockedDecrement(&shader->ref);
3431 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
3433 if (!refcount)
3435 shader->parent_ops->wined3d_object_destroyed(shader->parent);
3436 wined3d_cs_destroy_object(shader->device->cs, wined3d_shader_destroy_object, shader);
3439 return refcount;
3442 void * CDECL wined3d_shader_get_parent(const struct wined3d_shader *shader)
3444 TRACE("shader %p.\n", shader);
3446 return shader->parent;
3449 HRESULT CDECL wined3d_shader_get_byte_code(const struct wined3d_shader *shader,
3450 void *byte_code, UINT *byte_code_size)
3452 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader, byte_code, byte_code_size);
3454 if (!byte_code)
3456 *byte_code_size = shader->byte_code_size;
3457 return WINED3D_OK;
3460 if (*byte_code_size < shader->byte_code_size)
3462 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
3463 * than the required size we should write the required size and
3464 * return D3DERR_MOREDATA. That's not actually true. */
3465 return WINED3DERR_INVALIDCALL;
3468 memcpy(byte_code, shader->byte_code, shader->byte_code_size);
3470 return WINED3D_OK;
3473 /* Set local constants for d3d8 shaders. */
3474 HRESULT CDECL wined3d_shader_set_local_constants_float(struct wined3d_shader *shader,
3475 UINT start_idx, const float *src_data, UINT count)
3477 UINT end_idx = start_idx + count;
3478 UINT i;
3480 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader, start_idx, src_data, count);
3482 if (end_idx > shader->limits->constant_float)
3484 WARN("end_idx %u > float constants limit %u.\n",
3485 end_idx, shader->limits->constant_float);
3486 end_idx = shader->limits->constant_float;
3489 for (i = start_idx; i < end_idx; ++i)
3491 struct wined3d_shader_lconst *lconst;
3492 float *value;
3494 if (!(lconst = heap_alloc(sizeof(*lconst))))
3495 return E_OUTOFMEMORY;
3497 lconst->idx = i;
3498 value = (float *)lconst->value;
3499 memcpy(value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
3500 list_add_head(&shader->constantsF, &lconst->entry);
3502 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
3503 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
3505 shader->lconst_inf_or_nan = TRUE;
3509 return WINED3D_OK;
3512 static void init_interpolation_compile_args(DWORD *interpolation_args,
3513 const struct wined3d_shader *pixel_shader, const struct wined3d_d3d_info *d3d_info)
3515 if (!d3d_info->shader_output_interpolation || !pixel_shader
3516 || pixel_shader->reg_maps.shader_version.major < 4)
3518 memset(interpolation_args, 0, sizeof(pixel_shader->u.ps.interpolation_mode));
3519 return;
3522 memcpy(interpolation_args, pixel_shader->u.ps.interpolation_mode,
3523 sizeof(pixel_shader->u.ps.interpolation_mode));
3526 void find_vs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3527 WORD swizzle_map, struct vs_compile_args *args, const struct wined3d_context *context)
3529 const struct wined3d_shader *geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
3530 const struct wined3d_shader *pixel_shader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3531 const struct wined3d_shader *hull_shader = state->shader[WINED3D_SHADER_TYPE_HULL];
3532 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3534 args->fog_src = state->render_states[WINED3D_RS_FOGTABLEMODE]
3535 == WINED3D_FOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
3536 args->clip_enabled = state->render_states[WINED3D_RS_CLIPPING]
3537 && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
3538 args->point_size = state->primitive_type == WINED3D_PT_POINTLIST;
3539 args->per_vertex_point_size = shader->reg_maps.point_size;
3540 args->next_shader_type = hull_shader ? WINED3D_SHADER_TYPE_HULL
3541 : geometry_shader ? WINED3D_SHADER_TYPE_GEOMETRY : WINED3D_SHADER_TYPE_PIXEL;
3542 if (shader->reg_maps.shader_version.major >= 4)
3543 args->next_shader_input_count = hull_shader ? hull_shader->limits->packed_input
3544 : geometry_shader ? geometry_shader->limits->packed_input
3545 : pixel_shader ? pixel_shader->limits->packed_input : 0;
3546 else
3547 args->next_shader_input_count = 0;
3548 args->swizzle_map = swizzle_map;
3549 if (d3d_info->emulated_flatshading)
3550 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
3551 else
3552 args->flatshading = 0;
3554 init_interpolation_compile_args(args->interpolation_mode,
3555 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL ? pixel_shader : NULL, d3d_info);
3558 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
3560 if (usage_idx1 != usage_idx2)
3561 return FALSE;
3562 if (usage1 == usage2)
3563 return TRUE;
3564 if (usage1 == WINED3D_DECL_USAGE_POSITION && usage2 == WINED3D_DECL_USAGE_POSITIONT)
3565 return TRUE;
3566 if (usage2 == WINED3D_DECL_USAGE_POSITION && usage1 == WINED3D_DECL_USAGE_POSITIONT)
3567 return TRUE;
3569 return FALSE;
3572 BOOL vshader_get_input(const struct wined3d_shader *shader,
3573 BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
3575 WORD map = shader->reg_maps.input_registers;
3576 unsigned int i;
3578 for (i = 0; map; map >>= 1, ++i)
3580 if (!(map & 1)) continue;
3582 if (match_usage(shader->u.vs.attributes[i].usage,
3583 shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
3585 *regnum = i;
3586 return TRUE;
3589 return FALSE;
3592 static HRESULT shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3593 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3595 HRESULT hr;
3597 TRACE("byte_code %p, byte_code_size %#lx.\n", desc->byte_code, (long)desc->byte_code_size);
3599 if (!desc->byte_code)
3600 return WINED3DERR_INVALIDCALL;
3602 shader->ref = 1;
3603 shader->device = device;
3604 shader->parent = parent;
3605 shader->parent_ops = parent_ops;
3607 list_init(&shader->linked_programs);
3608 list_init(&shader->constantsF);
3609 list_init(&shader->constantsB);
3610 list_init(&shader->constantsI);
3611 shader->lconst_inf_or_nan = FALSE;
3612 list_init(&shader->reg_maps.indexable_temps);
3613 list_init(&shader->shader_list_entry);
3615 if (desc->byte_code_size == ~(size_t)0)
3617 struct wined3d_shader_version shader_version;
3618 const struct wined3d_shader_frontend *fe;
3619 struct wined3d_shader_instruction ins;
3620 const DWORD *ptr;
3621 void *fe_data;
3623 if (!(shader->frontend = shader_select_frontend(WINED3D_SHADER_BYTE_CODE_FORMAT_SM1)))
3625 FIXME("Unable to find frontend for shader.\n");
3626 hr = WINED3DERR_INVALIDCALL;
3627 goto fail;
3630 fe = shader->frontend;
3631 if (!(fe_data = fe->shader_init(desc->byte_code, desc->byte_code_size, &shader->output_signature)))
3633 WARN("Failed to initialise frontend data.\n");
3634 hr = WINED3DERR_INVALIDCALL;
3635 goto fail;
3638 fe->shader_read_header(fe_data, &ptr, &shader_version);
3639 while (!fe->shader_is_end(fe_data, &ptr))
3640 fe->shader_read_instruction(fe_data, &ptr, &ins);
3642 fe->shader_free(fe_data);
3644 shader->byte_code_size = (ptr - desc->byte_code) * sizeof(*ptr);
3646 if (!(shader->byte_code = heap_alloc(shader->byte_code_size)))
3648 hr = E_OUTOFMEMORY;
3649 goto fail;
3651 memcpy(shader->byte_code, desc->byte_code, shader->byte_code_size);
3653 shader->function = shader->byte_code;
3654 shader->functionLength = shader->byte_code_size;
3656 else
3658 enum wined3d_shader_byte_code_format format;
3659 unsigned int max_version;
3661 if (!(shader->byte_code = heap_alloc(desc->byte_code_size)))
3663 hr = E_OUTOFMEMORY;
3664 goto fail;
3666 memcpy(shader->byte_code, desc->byte_code, desc->byte_code_size);
3667 shader->byte_code_size = desc->byte_code_size;
3669 max_version = shader_max_version_from_feature_level(device->cs->c.state->feature_level);
3670 if (FAILED(hr = shader_extract_from_dxbc(shader, max_version, &format)))
3671 goto fail;
3673 if (!(shader->frontend = shader_select_frontend(format)))
3675 FIXME("Unable to find frontend for shader.\n");
3676 hr = WINED3DERR_INVALIDCALL;
3677 goto fail;
3681 return WINED3D_OK;
3683 fail:
3684 shader_cleanup(shader);
3685 return hr;
3688 static HRESULT vertex_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3689 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3691 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3692 unsigned int i;
3693 HRESULT hr;
3695 if (FAILED(hr = shader_init(shader, device, desc, parent, parent_ops)))
3696 return hr;
3698 if (FAILED(hr = shader_set_function(shader, device,
3699 WINED3D_SHADER_TYPE_VERTEX, device->adapter->d3d_info.limits.vs_uniform_count)))
3701 shader_cleanup(shader);
3702 return hr;
3705 for (i = 0; i < shader->input_signature.element_count; ++i)
3707 const struct wined3d_shader_signature_element *input = &shader->input_signature.elements[i];
3709 if (!(reg_maps->input_registers & (1u << input->register_idx)) || !input->semantic_name)
3710 continue;
3712 shader->u.vs.attributes[input->register_idx].usage =
3713 shader_usage_from_semantic_name(input->semantic_name);
3714 shader->u.vs.attributes[input->register_idx].usage_idx = input->semantic_idx;
3717 if (reg_maps->usesrelconstF && !list_empty(&shader->constantsF))
3718 shader->load_local_constsF = TRUE;
3720 return WINED3D_OK;
3723 static struct wined3d_shader_signature_element *shader_find_signature_element(const struct wined3d_shader_signature *s,
3724 unsigned int stream_idx, const char *semantic_name, unsigned int semantic_idx)
3726 struct wined3d_shader_signature_element *e = s->elements;
3727 unsigned int i;
3729 for (i = 0; i < s->element_count; ++i)
3731 if (e[i].stream_idx == stream_idx
3732 && !_strnicmp(e[i].semantic_name, semantic_name, -1)
3733 && e[i].semantic_idx == semantic_idx)
3734 return &e[i];
3737 return NULL;
3740 BOOL shader_get_stream_output_register_info(const struct wined3d_shader *shader,
3741 const struct wined3d_stream_output_element *so_element, unsigned int *register_idx, unsigned int *component_idx)
3743 const struct wined3d_shader_signature_element *output;
3744 unsigned int idx;
3746 if (!(output = shader_find_signature_element(&shader->output_signature,
3747 so_element->stream_idx, so_element->semantic_name, so_element->semantic_idx)))
3748 return FALSE;
3750 for (idx = 0; idx < 4; ++idx)
3752 if (output->mask & (1u << idx))
3753 break;
3755 idx += so_element->component_idx;
3757 *register_idx = output->register_idx;
3758 *component_idx = idx;
3759 return TRUE;
3762 static HRESULT geometry_shader_init_so_desc(struct wined3d_geometry_shader *gs, struct wined3d_device *device,
3763 const struct wined3d_stream_output_desc *so_desc)
3765 struct wined3d_so_desc_entry *s;
3766 struct wine_rb_entry *entry;
3767 unsigned int i;
3768 size_t size;
3769 char *name;
3771 if ((entry = wine_rb_get(&device->so_descs, so_desc)))
3773 gs->so_desc = &WINE_RB_ENTRY_VALUE(entry, struct wined3d_so_desc_entry, entry)->desc;
3774 return WINED3D_OK;
3777 size = FIELD_OFFSET(struct wined3d_so_desc_entry, elements[so_desc->element_count]);
3778 for (i = 0; i < so_desc->element_count; ++i)
3780 const char *n = so_desc->elements[i].semantic_name;
3782 if (n)
3783 size += strlen(n) + 1;
3785 if (!(s = heap_alloc(size)))
3786 return E_OUTOFMEMORY;
3788 s->desc = *so_desc;
3790 memcpy(s->elements, so_desc->elements, so_desc->element_count * sizeof(*s->elements));
3791 s->desc.elements = s->elements;
3793 name = (char *)&s->elements[s->desc.element_count];
3794 for (i = 0; i < so_desc->element_count; ++i)
3796 struct wined3d_stream_output_element *e = &s->elements[i];
3798 if (!e->semantic_name)
3799 continue;
3801 size = strlen(e->semantic_name) + 1;
3802 memcpy(name, e->semantic_name, size);
3803 e->semantic_name = name;
3804 name += size;
3807 if (wine_rb_put(&device->so_descs, &s->desc, &s->entry) == -1)
3809 heap_free(s);
3810 return E_FAIL;
3812 gs->so_desc = &s->desc;
3814 return WINED3D_OK;
3817 static HRESULT geometry_shader_init_stream_output(struct wined3d_shader *shader,
3818 const struct wined3d_stream_output_desc *so_desc)
3820 const struct wined3d_shader_frontend *fe = shader->frontend;
3821 const struct wined3d_shader_signature_element *output;
3822 unsigned int i, component_idx, register_idx, mask;
3823 struct wined3d_shader_version shader_version;
3824 const DWORD *ptr;
3825 void *fe_data;
3826 HRESULT hr;
3828 if (!so_desc)
3829 return WINED3D_OK;
3831 if (!(fe_data = fe->shader_init(shader->function, shader->functionLength, &shader->output_signature)))
3833 WARN("Failed to initialise frontend data.\n");
3834 return WINED3DERR_INVALIDCALL;
3836 fe->shader_read_header(fe_data, &ptr, &shader_version);
3837 fe->shader_free(fe_data);
3839 switch (shader_version.type)
3841 case WINED3D_SHADER_TYPE_VERTEX:
3842 case WINED3D_SHADER_TYPE_DOMAIN:
3843 shader->function = NULL;
3844 shader->functionLength = 0;
3845 break;
3846 case WINED3D_SHADER_TYPE_GEOMETRY:
3847 break;
3848 default:
3849 WARN("Wrong shader type %s.\n", debug_shader_type(shader_version.type));
3850 return E_INVALIDARG;
3853 if (!shader->function)
3855 shader->reg_maps.shader_version = shader_version;
3856 shader->reg_maps.shader_version.type = WINED3D_SHADER_TYPE_GEOMETRY;
3857 shader_set_limits(shader);
3858 if (FAILED(hr = shader_scan_output_signature(shader)))
3859 return hr;
3862 for (i = 0; i < so_desc->element_count; ++i)
3864 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
3866 if (!e->semantic_name)
3867 continue;
3868 if (!(output = shader_find_signature_element(&shader->output_signature,
3869 e->stream_idx, e->semantic_name, e->semantic_idx))
3870 || !shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
3872 WARN("Failed to find output signature element for stream output entry.\n");
3873 return E_INVALIDARG;
3876 mask = ((1u << e->component_count) - 1) << component_idx;
3877 if ((output->mask & 0xff & mask) != mask)
3879 WARN("Invalid component range %u-%u (mask %#x), output mask %#x.\n",
3880 component_idx, e->component_count, mask, output->mask & 0xff);
3881 return E_INVALIDARG;
3885 if (FAILED(hr = geometry_shader_init_so_desc(&shader->u.gs, shader->device, so_desc)))
3887 WARN("Failed to initialise stream output description, hr %#x.\n", hr);
3888 return hr;
3891 return WINED3D_OK;
3894 static HRESULT geometry_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3895 const struct wined3d_shader_desc *desc, const struct wined3d_stream_output_desc *so_desc,
3896 void *parent, const struct wined3d_parent_ops *parent_ops)
3898 HRESULT hr;
3900 if (FAILED(hr = shader_init(shader, device, desc, parent, parent_ops)))
3901 return hr;
3903 if (FAILED(hr = geometry_shader_init_stream_output(shader, so_desc)))
3904 goto fail;
3906 if (shader->function
3907 && FAILED(hr = shader_set_function(shader, device, WINED3D_SHADER_TYPE_GEOMETRY, 0)))
3908 goto fail;
3910 return WINED3D_OK;
3912 fail:
3913 shader_cleanup(shader);
3914 return hr;
3917 void find_ds_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3918 struct ds_compile_args *args, const struct wined3d_context *context)
3920 const struct wined3d_shader *geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
3921 const struct wined3d_shader *pixel_shader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3922 const struct wined3d_shader *hull_shader = state->shader[WINED3D_SHADER_TYPE_HULL];
3924 args->tessellator_output_primitive = hull_shader->u.hs.tessellator_output_primitive;
3925 args->tessellator_partitioning = hull_shader->u.hs.tessellator_partitioning;
3927 args->output_count = geometry_shader ? geometry_shader->limits->packed_input
3928 : pixel_shader ? pixel_shader->limits->packed_input : shader->limits->packed_output;
3929 args->next_shader_type = geometry_shader ? WINED3D_SHADER_TYPE_GEOMETRY : WINED3D_SHADER_TYPE_PIXEL;
3931 args->render_offscreen = context->render_offscreen;
3933 init_interpolation_compile_args(args->interpolation_mode,
3934 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL ? pixel_shader : NULL, context->d3d_info);
3936 args->padding = 0;
3939 void find_gs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3940 struct gs_compile_args *args, const struct wined3d_context *context)
3942 const struct wined3d_shader *pixel_shader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3944 args->output_count = pixel_shader ? pixel_shader->limits->packed_input : shader->limits->packed_output;
3946 if (!(args->primitive_type = shader->u.gs.input_type))
3947 args->primitive_type = state->primitive_type;
3949 init_interpolation_compile_args(args->interpolation_mode, pixel_shader, context->d3d_info);
3952 void find_ps_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3953 BOOL position_transformed, struct ps_compile_args *args, const struct wined3d_context *context)
3955 const struct wined3d_gl_info *gl_info = &context->device->adapter->gl_info;
3956 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3957 struct wined3d_texture *texture;
3958 unsigned int i;
3960 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
3961 if (!d3d_info->srgb_write_control && needs_srgb_write(d3d_info, state, &state->fb))
3963 static unsigned int warned = 0;
3965 args->srgb_correction = 1;
3966 if (state->blend_state && state->blend_state->desc.rt[0].enable && !warned++)
3967 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
3968 "support, expect rendering artifacts.\n");
3971 if (shader->reg_maps.shader_version.major == 1
3972 && shader->reg_maps.shader_version.minor <= 3)
3974 for (i = 0; i < shader->limits->sampler; ++i)
3976 DWORD flags = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
3978 if (flags & WINED3D_TTFF_PROJECTED)
3980 DWORD tex_transform = flags & ~WINED3D_TTFF_PROJECTED;
3982 if (!state->shader[WINED3D_SHADER_TYPE_VERTEX])
3984 enum wined3d_shader_resource_type resource_type = shader->reg_maps.resource_info[i].type;
3985 unsigned int j;
3986 unsigned int index = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
3987 DWORD max_valid = WINED3D_TTFF_COUNT4;
3989 for (j = 0; j < state->vertex_declaration->element_count; ++j)
3991 struct wined3d_vertex_declaration_element *element =
3992 &state->vertex_declaration->elements[j];
3994 if (element->usage == WINED3D_DECL_USAGE_TEXCOORD
3995 && element->usage_idx == index)
3997 max_valid = element->format->component_count;
3998 break;
4001 if (!tex_transform || tex_transform > max_valid)
4003 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
4004 tex_transform, max_valid);
4005 tex_transform = max_valid;
4007 if ((resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1D && tex_transform > WINED3D_TTFF_COUNT1)
4008 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D
4009 && tex_transform > WINED3D_TTFF_COUNT2)
4010 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_3D
4011 && tex_transform > WINED3D_TTFF_COUNT3))
4012 tex_transform |= WINED3D_PSARGS_PROJECTED;
4013 else
4015 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
4016 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
4017 i, tex_transform, resource_type);
4020 else
4021 tex_transform = WINED3D_TTFF_COUNT4 | WINED3D_PSARGS_PROJECTED;
4023 args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
4027 if (shader->reg_maps.shader_version.major == 1
4028 && shader->reg_maps.shader_version.minor <= 4)
4030 for (i = 0; i < shader->limits->sampler; ++i)
4032 if (!shader->reg_maps.resource_info[i].type)
4033 continue;
4035 /* Treat unbound textures as 2D. The dummy texture will provide
4036 * the proper sample value. The tex_types bitmap defaults to
4037 * 2D because of the memset. */
4038 if (!(texture = state->textures[i]))
4039 continue;
4041 switch (wined3d_texture_gl(texture)->target)
4043 /* RECT textures are distinguished from 2D textures via np2_fixup */
4044 default:
4045 break;
4047 case GL_TEXTURE_3D:
4048 args->tex_types |= WINED3D_SHADER_TEX_3D << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
4049 break;
4051 case GL_TEXTURE_CUBE_MAP_ARB:
4052 args->tex_types |= WINED3D_SHADER_TEX_CUBE << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
4053 break;
4057 else if (shader->reg_maps.shader_version.major <= 3)
4059 for (i = 0; i < shader->limits->sampler; ++i)
4061 enum wined3d_shader_resource_type resource_type;
4062 enum wined3d_shader_tex_types tex_type;
4064 if (!(resource_type = shader->reg_maps.resource_info[i].type))
4065 continue;
4067 switch (resource_type)
4069 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
4070 tex_type = WINED3D_SHADER_TEX_3D;
4071 break;
4072 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
4073 tex_type = WINED3D_SHADER_TEX_CUBE;
4074 break;
4075 default:
4076 tex_type = WINED3D_SHADER_TEX_2D;
4077 break;
4080 if ((texture = state->textures[i]))
4082 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
4083 && resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_3D
4084 && !(texture->resource.usage & WINED3DUSAGE_LEGACY_CUBEMAP))
4085 tex_type = WINED3D_SHADER_TEX_2D;
4086 else if (texture->resource.type == WINED3D_RTYPE_TEXTURE_3D
4087 && resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D)
4088 tex_type = WINED3D_SHADER_TEX_3D;
4090 args->tex_types |= tex_type << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
4094 if (shader->reg_maps.shader_version.major >= 4)
4096 /* In SM4+ we use dcl_sampler in order to determine if we should use shadow sampler. */
4097 args->shadow = 0;
4098 for (i = 0 ; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
4099 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
4100 args->np2_fixup = 0;
4102 else
4104 for (i = 0; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
4106 if (!shader->reg_maps.resource_info[i].type)
4107 continue;
4109 texture = state->textures[i];
4110 if (!texture)
4112 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
4113 continue;
4115 if (can_use_texture_swizzle(d3d_info, texture->resource.format))
4116 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
4117 else
4118 args->color_fixup[i] = texture->resource.format->color_fixup;
4120 if (texture->resource.format_flags & WINED3DFMT_FLAG_SHADOW)
4121 args->shadow |= 1u << i;
4123 /* Flag samplers that need NP2 texcoord fixup. */
4124 if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
4125 args->np2_fixup |= (1u << i);
4129 if (shader->reg_maps.shader_version.major >= 3)
4131 if (position_transformed)
4132 args->vp_mode = WINED3D_VP_MODE_NONE;
4133 else if (use_vs(state))
4134 args->vp_mode = WINED3D_VP_MODE_SHADER;
4135 else
4136 args->vp_mode = WINED3D_VP_MODE_FF;
4137 args->fog = WINED3D_FFP_PS_FOG_OFF;
4139 else
4141 args->vp_mode = WINED3D_VP_MODE_SHADER;
4142 if (state->render_states[WINED3D_RS_FOGENABLE])
4144 switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
4146 case WINED3D_FOG_NONE:
4147 if (position_transformed || use_vs(state))
4149 args->fog = WINED3D_FFP_PS_FOG_LINEAR;
4150 break;
4153 switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
4155 case WINED3D_FOG_NONE: /* Fall through. */
4156 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
4157 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
4158 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
4160 break;
4162 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
4163 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
4164 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
4167 else
4169 args->fog = WINED3D_FFP_PS_FOG_OFF;
4173 if (!d3d_info->full_ffp_varyings)
4175 const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
4177 args->texcoords_initialized = 0;
4178 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
4180 if (vs)
4182 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.output_registers & (1u << i))
4183 args->texcoords_initialized |= 1u << i;
4185 else
4187 const struct wined3d_stream_info *si = &context->stream_info;
4188 unsigned int coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
4190 if ((state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX] >> WINED3D_FFP_TCI_SHIFT)
4191 & WINED3D_FFP_TCI_MASK
4192 || (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))))
4193 args->texcoords_initialized |= 1u << i;
4197 else
4199 args->texcoords_initialized = (1u << WINED3D_MAX_TEXTURES) - 1;
4202 args->pointsprite = state->render_states[WINED3D_RS_POINTSPRITEENABLE]
4203 && state->primitive_type == WINED3D_PT_POINTLIST;
4205 if (d3d_info->ffp_alpha_test)
4206 args->alpha_test_func = WINED3D_CMP_ALWAYS - 1;
4207 else
4208 args->alpha_test_func = (state->render_states[WINED3D_RS_ALPHATESTENABLE]
4209 ? wined3d_sanitize_cmp_func(state->render_states[WINED3D_RS_ALPHAFUNC])
4210 : WINED3D_CMP_ALWAYS) - 1;
4212 if (d3d_info->emulated_flatshading)
4213 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
4215 args->render_offscreen = shader->reg_maps.vpos && gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS]
4216 ? context->render_offscreen : 0;
4218 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
4220 struct wined3d_rendertarget_view *rtv = state->fb.render_targets[i];
4221 if (rtv && rtv->format->id == WINED3DFMT_A8_UNORM && !is_identity_fixup(rtv->format->color_fixup))
4222 args->rt_alpha_swizzle |= 1u << i;
4225 args->dual_source_blend = state->blend_state && state->blend_state->dual_source;
4228 static HRESULT pixel_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
4229 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
4231 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4232 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
4233 HRESULT hr;
4235 if (FAILED(hr = shader_init(shader, device, desc, parent, parent_ops)))
4236 return hr;
4238 if (FAILED(hr = shader_set_function(shader, device,
4239 WINED3D_SHADER_TYPE_PIXEL, device->adapter->d3d_info.limits.ps_uniform_count)))
4241 shader_cleanup(shader);
4242 return hr;
4245 for (i = 0; i < MAX_REG_INPUT; ++i)
4247 if (shader->u.ps.input_reg_used & (1u << i))
4249 ++num_regs_used;
4250 highest_reg_used = i;
4254 /* Don't do any register mapping magic if it is not needed, or if we can't
4255 * achieve anything anyway */
4256 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
4257 || num_regs_used > (gl_info->limits.glsl_varyings / 4)
4258 || shader->reg_maps.shader_version.major >= 4)
4260 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
4262 /* This happens with relative addressing. The input mapper function
4263 * warns about this if the higher registers are declared too, so
4264 * don't write a FIXME here */
4265 WARN("More varying registers used than supported\n");
4268 for (i = 0; i < MAX_REG_INPUT; ++i)
4270 shader->u.ps.input_reg_map[i] = i;
4273 shader->u.ps.declared_in_count = highest_reg_used + 1;
4275 else
4277 shader->u.ps.declared_in_count = 0;
4278 for (i = 0; i < MAX_REG_INPUT; ++i)
4280 if (shader->u.ps.input_reg_used & (1u << i))
4281 shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
4282 else shader->u.ps.input_reg_map[i] = ~0U;
4286 return WINED3D_OK;
4289 enum wined3d_shader_resource_type pixelshader_get_resource_type(const struct wined3d_shader_reg_maps *reg_maps,
4290 unsigned int resource_idx, DWORD tex_types)
4292 static enum wined3d_shader_resource_type shader_resource_type_from_shader_tex_types[] =
4294 WINED3D_SHADER_RESOURCE_TEXTURE_2D, /* WINED3D_SHADER_TEX_2D */
4295 WINED3D_SHADER_RESOURCE_TEXTURE_3D, /* WINED3D_SHADER_TEX_3D */
4296 WINED3D_SHADER_RESOURCE_TEXTURE_CUBE, /* WINED3D_SHADER_TEX_CUBE */
4299 unsigned int idx;
4301 if (reg_maps->shader_version.major > 3)
4302 return reg_maps->resource_info[resource_idx].type;
4304 if (!reg_maps->resource_info[resource_idx].type)
4305 return 0;
4307 idx = (tex_types >> resource_idx * WINED3D_PSARGS_TEXTYPE_SHIFT) & WINED3D_PSARGS_TEXTYPE_MASK;
4308 assert(idx < ARRAY_SIZE(shader_resource_type_from_shader_tex_types));
4309 return shader_resource_type_from_shader_tex_types[idx];
4312 HRESULT CDECL wined3d_shader_create_cs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4313 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4315 struct wined3d_shader *object;
4316 HRESULT hr;
4318 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4319 device, desc, parent, parent_ops, shader);
4321 if (!(object = heap_alloc_zero(sizeof(*object))))
4322 return E_OUTOFMEMORY;
4324 if (FAILED(hr = shader_init(object, device, desc, parent, parent_ops)))
4326 WARN("Failed to initialize compute shader, hr %#x.\n", hr);
4327 heap_free(object);
4328 return hr;
4331 if (FAILED(hr = shader_set_function(object, device, WINED3D_SHADER_TYPE_COMPUTE, 0)))
4333 shader_cleanup(object);
4334 heap_free(object);
4335 return hr;
4338 wined3d_cs_init_object(device->cs, wined3d_shader_init_object, object);
4340 TRACE("Created compute shader %p.\n", object);
4341 *shader = object;
4343 return WINED3D_OK;
4346 HRESULT CDECL wined3d_shader_create_ds(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4347 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4349 struct wined3d_shader *object;
4350 HRESULT hr;
4352 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4353 device, desc, parent, parent_ops, shader);
4355 if (!(object = heap_alloc_zero(sizeof(*object))))
4356 return E_OUTOFMEMORY;
4358 if (FAILED(hr = shader_init(object, device, desc, parent, parent_ops)))
4360 WARN("Failed to initialize domain shader, hr %#x.\n", hr);
4361 heap_free(object);
4362 return hr;
4365 if (FAILED(hr = shader_set_function(object, device, WINED3D_SHADER_TYPE_DOMAIN, 0)))
4367 shader_cleanup(object);
4368 heap_free(object);
4369 return hr;
4372 wined3d_cs_init_object(device->cs, wined3d_shader_init_object, object);
4374 TRACE("Created domain shader %p.\n", object);
4375 *shader = object;
4377 return WINED3D_OK;
4380 HRESULT CDECL wined3d_shader_create_gs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4381 const struct wined3d_stream_output_desc *so_desc, void *parent,
4382 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4384 struct wined3d_shader *object;
4385 HRESULT hr;
4387 TRACE("device %p, desc %p, so_desc %p, parent %p, parent_ops %p, shader %p.\n",
4388 device, desc, so_desc, parent, parent_ops, shader);
4390 if (!(object = heap_alloc_zero(sizeof(*object))))
4391 return E_OUTOFMEMORY;
4393 if (FAILED(hr = geometry_shader_init(object, device, desc, so_desc, parent, parent_ops)))
4395 WARN("Failed to initialize geometry shader, hr %#x.\n", hr);
4396 heap_free(object);
4397 return hr;
4400 wined3d_cs_init_object(device->cs, wined3d_shader_init_object, object);
4402 TRACE("Created geometry shader %p.\n", object);
4403 *shader = object;
4405 return WINED3D_OK;
4408 HRESULT CDECL wined3d_shader_create_hs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4409 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4411 struct wined3d_shader *object;
4412 HRESULT hr;
4414 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4415 device, desc, parent, parent_ops, shader);
4417 if (!(object = heap_alloc_zero(sizeof(*object))))
4418 return E_OUTOFMEMORY;
4420 if (FAILED(hr = shader_init(object, device, desc, parent, parent_ops)))
4422 WARN("Failed to initialize hull shader, hr %#x.\n", hr);
4423 heap_free(object);
4424 return hr;
4427 if (FAILED(hr = shader_set_function(object, device, WINED3D_SHADER_TYPE_HULL, 0)))
4429 shader_cleanup(object);
4430 heap_free(object);
4431 return hr;
4434 wined3d_cs_init_object(device->cs, wined3d_shader_init_object, object);
4436 TRACE("Created hull shader %p.\n", object);
4437 *shader = object;
4439 return WINED3D_OK;
4442 HRESULT CDECL wined3d_shader_create_ps(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4443 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4445 struct wined3d_shader *object;
4446 HRESULT hr;
4448 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4449 device, desc, parent, parent_ops, shader);
4451 if (!(object = heap_alloc_zero(sizeof(*object))))
4452 return E_OUTOFMEMORY;
4454 if (FAILED(hr = pixel_shader_init(object, device, desc, parent, parent_ops)))
4456 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
4457 heap_free(object);
4458 return hr;
4461 wined3d_cs_init_object(device->cs, wined3d_shader_init_object, object);
4463 TRACE("Created pixel shader %p.\n", object);
4464 *shader = object;
4466 return WINED3D_OK;
4469 HRESULT CDECL wined3d_shader_create_vs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4470 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4472 struct wined3d_shader *object;
4473 HRESULT hr;
4475 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4476 device, desc, parent, parent_ops, shader);
4478 if (!(object = heap_alloc_zero(sizeof(*object))))
4479 return E_OUTOFMEMORY;
4481 if (FAILED(hr = vertex_shader_init(object, device, desc, parent, parent_ops)))
4483 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
4484 heap_free(object);
4485 return hr;
4488 wined3d_cs_init_object(device->cs, wined3d_shader_init_object, object);
4490 TRACE("Created vertex shader %p.\n", object);
4491 *shader = object;
4493 return WINED3D_OK;