directmanipulation: Return S_OK form viewport_SetViewportOptions stub.
[wine/zf.git] / dlls / wined3d / shader.c
blob8843e6883fc416028a9a17298eccd28ceac8bf32
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 unsigned int reg_idx;
1653 if (ins.handler_idx == WINED3DSIH_LD_UAV_TYPED || ins.handler_idx == WINED3DSIH_LD_RAW)
1654 reg_idx = ins.src[1].reg.idx[0].offset;
1655 else if (ins.handler_idx == WINED3DSIH_LD_STRUCTURED)
1656 reg_idx = ins.src[2].reg.idx[0].offset;
1657 else if (WINED3DSIH_ATOMIC_AND <= ins.handler_idx && ins.handler_idx <= WINED3DSIH_ATOMIC_XOR)
1658 reg_idx = ins.dst[0].reg.idx[0].offset;
1659 else if (ins.handler_idx == WINED3DSIH_BUFINFO)
1660 reg_idx = ins.src[0].reg.idx[0].offset;
1661 else
1662 reg_idx = ins.dst[1].reg.idx[0].offset;
1663 if (reg_idx >= MAX_UNORDERED_ACCESS_VIEWS)
1665 ERR("Invalid UAV index %u.\n", reg_idx);
1666 break;
1668 reg_maps->uav_read_mask |= (1u << reg_idx);
1670 else if (ins.handler_idx == WINED3DSIH_NRM)
1672 reg_maps->usesnrm = 1;
1674 else if (ins.handler_idx == WINED3DSIH_DSY
1675 || ins.handler_idx == WINED3DSIH_DSY_COARSE
1676 || ins.handler_idx == WINED3DSIH_DSY_FINE)
1678 reg_maps->usesdsy = 1;
1680 else if (ins.handler_idx == WINED3DSIH_DSX
1681 || ins.handler_idx == WINED3DSIH_DSX_COARSE
1682 || ins.handler_idx == WINED3DSIH_DSX_FINE)
1684 reg_maps->usesdsx = 1;
1686 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
1687 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
1688 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
1689 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
1690 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
1691 else if (ins.handler_idx == WINED3DSIH_POW) reg_maps->usespow = 1;
1692 else if (ins.handler_idx == WINED3DSIH_LOOP
1693 || ins.handler_idx == WINED3DSIH_REP)
1695 ++cur_loop_depth;
1696 if (cur_loop_depth > max_loop_depth)
1697 max_loop_depth = cur_loop_depth;
1699 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
1700 || ins.handler_idx == WINED3DSIH_ENDREP)
1702 --cur_loop_depth;
1704 else if (ins.handler_idx == WINED3DSIH_GATHER4
1705 || ins.handler_idx == WINED3DSIH_GATHER4_C
1706 || ins.handler_idx == WINED3DSIH_SAMPLE
1707 || ins.handler_idx == WINED3DSIH_SAMPLE_B
1708 || ins.handler_idx == WINED3DSIH_SAMPLE_C
1709 || ins.handler_idx == WINED3DSIH_SAMPLE_C_LZ
1710 || ins.handler_idx == WINED3DSIH_SAMPLE_GRAD
1711 || ins.handler_idx == WINED3DSIH_SAMPLE_LOD)
1713 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1714 ins.src[2].reg.idx[0].offset, reg_maps->sampler_map.count);
1716 else if (ins.handler_idx == WINED3DSIH_GATHER4_PO
1717 || ins.handler_idx == WINED3DSIH_GATHER4_PO_C)
1719 shader_record_sample(reg_maps, ins.src[2].reg.idx[0].offset,
1720 ins.src[3].reg.idx[0].offset, reg_maps->sampler_map.count);
1722 else if ((ins.handler_idx == WINED3DSIH_BUFINFO && ins.src[0].reg.type == WINED3DSPR_RESOURCE)
1723 || (ins.handler_idx == WINED3DSIH_SAMPLE_INFO && ins.src[0].reg.type == WINED3DSPR_RESOURCE))
1725 shader_record_sample(reg_maps, ins.src[0].reg.idx[0].offset,
1726 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1728 else if (ins.handler_idx == WINED3DSIH_LD
1729 || ins.handler_idx == WINED3DSIH_LD2DMS
1730 || (ins.handler_idx == WINED3DSIH_LD_RAW && ins.src[1].reg.type == WINED3DSPR_RESOURCE)
1731 || (ins.handler_idx == WINED3DSIH_RESINFO && ins.src[1].reg.type == WINED3DSPR_RESOURCE))
1733 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1734 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1736 else if (ins.handler_idx == WINED3DSIH_LD_STRUCTURED
1737 && ins.src[2].reg.type == WINED3DSPR_RESOURCE)
1739 shader_record_sample(reg_maps, ins.src[2].reg.idx[0].offset,
1740 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1743 if (ins.predicate)
1744 if (!shader_record_register_usage(shader, reg_maps, &ins.predicate->reg,
1745 shader_version.type, constf_size))
1746 return WINED3DERR_INVALIDCALL;
1748 for (i = 0; i < ins.src_count; ++i)
1750 unsigned int count = get_instr_extra_regcount(ins.handler_idx, i);
1751 struct wined3d_shader_register reg = ins.src[i].reg;
1753 if (!shader_record_register_usage(shader, reg_maps, &ins.src[i].reg,
1754 shader_version.type, constf_size))
1755 return WINED3DERR_INVALIDCALL;
1756 while (count)
1758 ++reg.idx[0].offset;
1759 if (!shader_record_register_usage(shader, reg_maps, &reg,
1760 shader_version.type, constf_size))
1761 return WINED3DERR_INVALIDCALL;
1762 --count;
1765 if (color0_mov)
1767 if (ins.src[i].reg.type == WINED3DSPR_TEMP
1768 && ins.src[i].swizzle == WINED3DSP_NOSWIZZLE)
1770 shader->u.ps.color0_mov = TRUE;
1771 shader->u.ps.color0_reg = ins.src[i].reg.idx[0].offset;
1777 prev_ins = current_ins;
1779 reg_maps->loop_depth = max_loop_depth;
1781 if (phase)
1783 phase->end = prev_ins;
1784 phase = NULL;
1787 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1788 * R0 is written to the render target. */
1789 if (shader_version.major < 2 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1790 reg_maps->rt_mask |= (1u << 0);
1792 if (input_signature->elements)
1794 for (i = 0; i < input_signature->element_count; ++i)
1796 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
1798 if (input_signature->elements[i].register_idx >= ARRAY_SIZE(shader->u.vs.attributes))
1800 WARN("Invalid input signature register index %u.\n", input_signature->elements[i].register_idx);
1801 return WINED3DERR_INVALIDCALL;
1804 else if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1806 if (input_signature->elements[i].sysval_semantic == WINED3D_SV_POSITION)
1807 reg_maps->vpos = 1;
1808 else if (input_signature->elements[i].sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
1809 reg_maps->usesfacing = 1;
1811 reg_maps->input_registers |= 1u << input_signature->elements[i].register_idx;
1814 else if (!input_signature->elements && reg_maps->input_registers)
1816 unsigned int count = wined3d_popcount(reg_maps->input_registers);
1817 struct wined3d_shader_signature_element *e;
1818 unsigned int i;
1820 if (!(input_signature->elements = heap_calloc(count, sizeof(*input_signature->elements))))
1821 return E_OUTOFMEMORY;
1822 input_signature->element_count = count;
1824 e = input_signature->elements;
1825 for (i = 0; i < ARRAY_SIZE(input_signature_elements); ++i)
1827 if (!(reg_maps->input_registers & (1u << i)))
1828 continue;
1829 input_signature_elements[i].register_idx = i;
1830 *e++ = input_signature_elements[i];
1834 if (output_signature->elements)
1836 if (FAILED(hr = shader_scan_output_signature(shader)))
1837 return hr;
1839 else if (reg_maps->output_registers)
1841 unsigned int count = wined3d_popcount(reg_maps->output_registers);
1842 struct wined3d_shader_signature_element *e;
1844 if (!(output_signature->elements = heap_calloc(count, sizeof(*output_signature->elements))))
1845 return E_OUTOFMEMORY;
1846 output_signature->element_count = count;
1848 e = output_signature->elements;
1849 for (i = 0; i < ARRAY_SIZE(output_signature_elements); ++i)
1851 if (!(reg_maps->output_registers & (1u << i)))
1852 continue;
1853 *e++ = output_signature_elements[i];
1857 return WINED3D_OK;
1860 static void shader_cleanup_reg_maps(struct wined3d_shader_reg_maps *reg_maps)
1862 struct wined3d_shader_indexable_temp *reg, *reg_next;
1864 heap_free(reg_maps->constf);
1865 heap_free(reg_maps->sampler_map.entries);
1867 LIST_FOR_EACH_ENTRY_SAFE(reg, reg_next, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
1868 heap_free(reg);
1869 list_init(&reg_maps->indexable_temps);
1871 heap_free(reg_maps->tgsm);
1874 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
1876 DWORD map = 1u << max;
1877 map |= map - 1;
1878 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
1880 return wined3d_log2i(map);
1883 static void shader_dump_global_flags(struct wined3d_string_buffer *buffer, DWORD global_flags)
1885 if (global_flags & WINED3DSGF_REFACTORING_ALLOWED)
1887 shader_addline(buffer, "refactoringAllowed");
1888 global_flags &= ~WINED3DSGF_REFACTORING_ALLOWED;
1889 if (global_flags)
1890 shader_addline(buffer, " | ");
1893 if (global_flags & WINED3DSGF_FORCE_EARLY_DEPTH_STENCIL)
1895 shader_addline(buffer, "forceEarlyDepthStencil");
1896 global_flags &= ~WINED3DSGF_FORCE_EARLY_DEPTH_STENCIL;
1897 if (global_flags)
1898 shader_addline(buffer, " | ");
1901 if (global_flags & WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS)
1903 shader_addline(buffer, "enableRawAndStructuredBuffers");
1904 global_flags &= ~WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS;
1907 if (global_flags)
1908 shader_addline(buffer, "unknown_flags(%#x)", global_flags);
1911 static void shader_dump_sync_flags(struct wined3d_string_buffer *buffer, DWORD sync_flags)
1913 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
1915 shader_addline(buffer, "_g");
1916 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
1918 if (sync_flags & WINED3DSSF_THREAD_GROUP)
1920 shader_addline(buffer, "_t");
1921 sync_flags &= ~WINED3DSSF_THREAD_GROUP;
1924 if (sync_flags)
1925 shader_addline(buffer, "_unknown_flags(%#x)", sync_flags);
1928 static void shader_dump_precise_flags(struct wined3d_string_buffer *buffer, DWORD flags)
1930 if (!(flags & WINED3DSI_PRECISE_XYZW))
1931 return;
1933 shader_addline(buffer, " [precise");
1934 if (flags != WINED3DSI_PRECISE_XYZW)
1936 shader_addline(buffer, "(%s%s%s%s)",
1937 flags & WINED3DSI_PRECISE_X ? "x" : "",
1938 flags & WINED3DSI_PRECISE_Y ? "y" : "",
1939 flags & WINED3DSI_PRECISE_Z ? "z" : "",
1940 flags & WINED3DSI_PRECISE_W ? "w" : "");
1942 shader_addline(buffer, "]");
1945 static void shader_dump_uav_flags(struct wined3d_string_buffer *buffer, DWORD uav_flags)
1947 if (uav_flags & WINED3DSUF_GLOBALLY_COHERENT)
1949 shader_addline(buffer, "_glc");
1950 uav_flags &= ~WINED3DSUF_GLOBALLY_COHERENT;
1952 if (uav_flags & WINED3DSUF_ORDER_PRESERVING_COUNTER)
1954 shader_addline(buffer, "_opc");
1955 uav_flags &= ~WINED3DSUF_ORDER_PRESERVING_COUNTER;
1958 if (uav_flags)
1959 shader_addline(buffer, "_unknown_flags(%#x)", uav_flags);
1962 static void shader_dump_tessellator_domain(struct wined3d_string_buffer *buffer,
1963 enum wined3d_tessellator_domain domain)
1965 switch (domain)
1967 case WINED3D_TESSELLATOR_DOMAIN_LINE:
1968 shader_addline(buffer, "line");
1969 break;
1970 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
1971 shader_addline(buffer, "triangle");
1972 break;
1973 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
1974 shader_addline(buffer, "quad");
1975 break;
1976 default:
1977 shader_addline(buffer, "unknown_tessellator_domain(%#x)", domain);
1978 break;
1982 static void shader_dump_tessellator_output_primitive(struct wined3d_string_buffer *buffer,
1983 enum wined3d_tessellator_output_primitive output_primitive)
1985 switch (output_primitive)
1987 case WINED3D_TESSELLATOR_OUTPUT_POINT:
1988 shader_addline(buffer, "point");
1989 break;
1990 case WINED3D_TESSELLATOR_OUTPUT_LINE:
1991 shader_addline(buffer, "line");
1992 break;
1993 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
1994 shader_addline(buffer, "triangle_cw");
1995 break;
1996 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
1997 shader_addline(buffer, "triangle_ccw");
1998 break;
1999 default:
2000 shader_addline(buffer, "unknown_tessellator_output_primitive(%#x)", output_primitive);
2001 break;
2005 static void shader_dump_tessellator_partitioning(struct wined3d_string_buffer *buffer,
2006 enum wined3d_tessellator_partitioning partitioning)
2008 switch (partitioning)
2010 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
2011 shader_addline(buffer, "integer");
2012 break;
2013 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
2014 shader_addline(buffer, "pow2");
2015 break;
2016 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
2017 shader_addline(buffer, "fractional_odd");
2018 break;
2019 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
2020 shader_addline(buffer, "fractional_even");
2021 break;
2022 default:
2023 shader_addline(buffer, "unknown_tessellator_partitioning(%#x)", partitioning);
2024 break;
2028 static void shader_dump_shader_input_sysval_semantic(struct wined3d_string_buffer *buffer,
2029 enum wined3d_shader_input_sysval_semantic semantic)
2031 unsigned int i;
2033 for (i = 0; i < ARRAY_SIZE(shader_input_sysval_semantic_names); ++i)
2035 if (shader_input_sysval_semantic_names[i].sysval_semantic == semantic)
2037 shader_addline(buffer, "%s", shader_input_sysval_semantic_names[i].sysval_name);
2038 return;
2042 shader_addline(buffer, "unknown_shader_input_sysval_semantic(%#x)", semantic);
2045 static void shader_dump_resource_type(struct wined3d_string_buffer *buffer, enum wined3d_shader_resource_type type)
2047 static const char *const resource_type_names[] =
2049 /* WINED3D_SHADER_RESOURCE_NONE */ "none",
2050 /* WINED3D_SHADER_RESOURCE_BUFFER */ "buffer",
2051 /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */ "texture1d",
2052 /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */ "texture2d",
2053 /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */ "texture2dms",
2054 /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */ "texture3d",
2055 /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */ "texturecube",
2056 /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */ "texture1darray",
2057 /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */ "texture2darray",
2058 /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */ "texture2dmsarray",
2059 /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */ "texturecubearray",
2062 if (type < ARRAY_SIZE(resource_type_names))
2063 shader_addline(buffer, "%s", resource_type_names[type]);
2064 else
2065 shader_addline(buffer, "unknown");
2068 static void shader_dump_data_type(struct wined3d_string_buffer *buffer, enum wined3d_data_type type)
2070 static const char *const data_type_names[] =
2072 /* WINED3D_DATA_FLOAT */ "(float)",
2073 /* WINED3D_DATA_INT */ "(int)",
2074 /* WINED3D_DATA_RESOURCE */ "(resource)",
2075 /* WINED3D_DATA_SAMPLER */ "(sampler)",
2076 /* WINED3D_DATA_UAV */ "(uav)",
2077 /* WINED3D_DATA_UINT */ "(uint)",
2078 /* WINED3D_DATA_UNORM */ "(unorm)",
2079 /* WINED3D_DATA_SNORM */ "(snorm)",
2080 /* WINED3D_DATA_OPAQUE */ "(opaque)",
2083 if (type < ARRAY_SIZE(data_type_names))
2084 shader_addline(buffer, "%s", data_type_names[type]);
2085 else
2086 shader_addline(buffer, "(unknown)");
2089 static void shader_dump_decl_usage(struct wined3d_string_buffer *buffer,
2090 const struct wined3d_shader_semantic *semantic, unsigned int flags,
2091 const struct wined3d_shader_version *shader_version)
2093 shader_addline(buffer, "dcl");
2095 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
2097 switch (semantic->resource_type)
2099 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2100 shader_addline(buffer, "_2d");
2101 break;
2103 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2104 shader_addline(buffer, "_3d");
2105 break;
2107 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2108 shader_addline(buffer, "_cube");
2109 break;
2111 default:
2112 shader_addline(buffer, "_unknown_resource_type(%#x)", semantic->resource_type);
2113 break;
2116 else if (semantic->reg.reg.type == WINED3DSPR_RESOURCE || semantic->reg.reg.type == WINED3DSPR_UAV)
2118 if (semantic->reg.reg.type == WINED3DSPR_RESOURCE)
2119 shader_addline(buffer, "_resource_");
2120 else
2121 shader_addline(buffer, "_uav_");
2122 shader_dump_resource_type(buffer, semantic->resource_type);
2123 if (semantic->reg.reg.type == WINED3DSPR_UAV)
2124 shader_dump_uav_flags(buffer, flags);
2125 shader_dump_data_type(buffer, semantic->resource_data_type);
2127 else
2129 /* Pixel shaders 3.0 don't have usage semantics. */
2130 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL)
2131 return;
2132 else
2133 shader_addline(buffer, "_");
2135 switch (semantic->usage)
2137 case WINED3D_DECL_USAGE_POSITION:
2138 shader_addline(buffer, "position%u", semantic->usage_idx);
2139 break;
2141 case WINED3D_DECL_USAGE_BLEND_INDICES:
2142 shader_addline(buffer, "blend");
2143 break;
2145 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
2146 shader_addline(buffer, "weight");
2147 break;
2149 case WINED3D_DECL_USAGE_NORMAL:
2150 shader_addline(buffer, "normal%u", semantic->usage_idx);
2151 break;
2153 case WINED3D_DECL_USAGE_PSIZE:
2154 shader_addline(buffer, "psize");
2155 break;
2157 case WINED3D_DECL_USAGE_COLOR:
2158 if (!semantic->usage_idx)
2159 shader_addline(buffer, "color");
2160 else
2161 shader_addline(buffer, "specular%u", (semantic->usage_idx - 1));
2162 break;
2164 case WINED3D_DECL_USAGE_TEXCOORD:
2165 shader_addline(buffer, "texture%u", semantic->usage_idx);
2166 break;
2168 case WINED3D_DECL_USAGE_TANGENT:
2169 shader_addline(buffer, "tangent");
2170 break;
2172 case WINED3D_DECL_USAGE_BINORMAL:
2173 shader_addline(buffer, "binormal");
2174 break;
2176 case WINED3D_DECL_USAGE_TESS_FACTOR:
2177 shader_addline(buffer, "tessfactor");
2178 break;
2180 case WINED3D_DECL_USAGE_POSITIONT:
2181 shader_addline(buffer, "positionT%u", semantic->usage_idx);
2182 break;
2184 case WINED3D_DECL_USAGE_FOG:
2185 shader_addline(buffer, "fog");
2186 break;
2188 case WINED3D_DECL_USAGE_DEPTH:
2189 shader_addline(buffer, "depth");
2190 break;
2192 case WINED3D_DECL_USAGE_SAMPLE:
2193 shader_addline(buffer, "sample");
2194 break;
2196 default:
2197 shader_addline(buffer, "<unknown_semantic(%#x)>", semantic->usage);
2198 FIXME("Unrecognised semantic usage %#x.\n", semantic->usage);
2203 static void shader_dump_register(struct wined3d_string_buffer *buffer,
2204 const struct wined3d_shader_register *reg, const struct wined3d_shader_version *shader_version)
2206 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
2207 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
2208 UINT offset = reg->idx[0].offset;
2210 switch (reg->type)
2212 case WINED3DSPR_TEMP:
2213 shader_addline(buffer, "r");
2214 break;
2216 case WINED3DSPR_INPUT:
2217 shader_addline(buffer, "v");
2218 break;
2220 case WINED3DSPR_CONST:
2221 case WINED3DSPR_CONST2:
2222 case WINED3DSPR_CONST3:
2223 case WINED3DSPR_CONST4:
2224 shader_addline(buffer, "c");
2225 offset = shader_get_float_offset(reg->type, offset);
2226 break;
2228 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
2229 shader_addline(buffer, "%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
2230 break;
2232 case WINED3DSPR_RASTOUT:
2233 shader_addline(buffer, "%s", rastout_reg_names[offset]);
2234 break;
2236 case WINED3DSPR_COLOROUT:
2237 shader_addline(buffer, "oC");
2238 break;
2240 case WINED3DSPR_DEPTHOUT:
2241 shader_addline(buffer, "oDepth");
2242 break;
2244 case WINED3DSPR_DEPTHOUTGE:
2245 shader_addline(buffer, "oDepthGE");
2246 break;
2248 case WINED3DSPR_DEPTHOUTLE:
2249 shader_addline(buffer, "oDepthLE");
2250 break;
2252 case WINED3DSPR_ATTROUT:
2253 shader_addline(buffer, "oD");
2254 break;
2256 case WINED3DSPR_TEXCRDOUT:
2257 /* Vertex shaders >= 3.0 use general purpose output registers
2258 * (WINED3DSPR_OUTPUT), which can include an address token. */
2259 if (shader_version->major >= 3)
2260 shader_addline(buffer, "o");
2261 else
2262 shader_addline(buffer, "oT");
2263 break;
2265 case WINED3DSPR_CONSTINT:
2266 shader_addline(buffer, "i");
2267 break;
2269 case WINED3DSPR_CONSTBOOL:
2270 shader_addline(buffer, "b");
2271 break;
2273 case WINED3DSPR_LABEL:
2274 shader_addline(buffer, "l");
2275 break;
2277 case WINED3DSPR_LOOP:
2278 shader_addline(buffer, "aL");
2279 break;
2281 case WINED3DSPR_SAMPLER:
2282 shader_addline(buffer, "s");
2283 break;
2285 case WINED3DSPR_MISCTYPE:
2286 if (offset > 1)
2288 FIXME("Unhandled misctype register %u.\n", offset);
2289 shader_addline(buffer, "<unhandled misctype %#x>", offset);
2291 else
2293 shader_addline(buffer, "%s", misctype_reg_names[offset]);
2295 break;
2297 case WINED3DSPR_PREDICATE:
2298 shader_addline(buffer, "p");
2299 break;
2301 case WINED3DSPR_IMMCONST:
2302 shader_addline(buffer, "l");
2303 break;
2305 case WINED3DSPR_CONSTBUFFER:
2306 shader_addline(buffer, "cb");
2307 break;
2309 case WINED3DSPR_IMMCONSTBUFFER:
2310 shader_addline(buffer, "icb");
2311 break;
2313 case WINED3DSPR_PRIMID:
2314 shader_addline(buffer, "primID");
2315 break;
2317 case WINED3DSPR_NULL:
2318 shader_addline(buffer, "null");
2319 break;
2321 case WINED3DSPR_RASTERIZER:
2322 shader_addline(buffer, "rasterizer");
2323 break;
2325 case WINED3DSPR_RESOURCE:
2326 shader_addline(buffer, "t");
2327 break;
2329 case WINED3DSPR_UAV:
2330 shader_addline(buffer, "u");
2331 break;
2333 case WINED3DSPR_OUTPOINTID:
2334 shader_addline(buffer, "vOutputControlPointID");
2335 break;
2337 case WINED3DSPR_FORKINSTID:
2338 shader_addline(buffer, "vForkInstanceId");
2339 break;
2341 case WINED3DSPR_JOININSTID:
2342 shader_addline(buffer, "vJoinInstanceId");
2343 break;
2345 case WINED3DSPR_INCONTROLPOINT:
2346 shader_addline(buffer, "vicp");
2347 break;
2349 case WINED3DSPR_OUTCONTROLPOINT:
2350 shader_addline(buffer, "vocp");
2351 break;
2353 case WINED3DSPR_PATCHCONST:
2354 shader_addline(buffer, "vpc");
2355 break;
2357 case WINED3DSPR_TESSCOORD:
2358 shader_addline(buffer, "vDomainLocation");
2359 break;
2361 case WINED3DSPR_GROUPSHAREDMEM:
2362 shader_addline(buffer, "g");
2363 break;
2365 case WINED3DSPR_THREADID:
2366 shader_addline(buffer, "vThreadID");
2367 break;
2369 case WINED3DSPR_THREADGROUPID:
2370 shader_addline(buffer, "vThreadGroupID");
2371 break;
2373 case WINED3DSPR_LOCALTHREADID:
2374 shader_addline(buffer, "vThreadIDInGroup");
2375 break;
2377 case WINED3DSPR_LOCALTHREADINDEX:
2378 shader_addline(buffer, "vThreadIDInGroupFlattened");
2379 break;
2381 case WINED3DSPR_IDXTEMP:
2382 shader_addline(buffer, "x");
2383 break;
2385 case WINED3DSPR_STREAM:
2386 shader_addline(buffer, "m");
2387 break;
2389 case WINED3DSPR_FUNCTIONBODY:
2390 shader_addline(buffer, "fb");
2391 break;
2393 case WINED3DSPR_FUNCTIONPOINTER:
2394 shader_addline(buffer, "fp");
2395 break;
2397 case WINED3DSPR_COVERAGE:
2398 shader_addline(buffer, "vCoverage");
2399 break;
2401 case WINED3DSPR_SAMPLEMASK:
2402 shader_addline(buffer, "oMask");
2403 break;
2405 case WINED3DSPR_GSINSTID:
2406 shader_addline(buffer, "vGSInstanceID");
2407 break;
2409 default:
2410 shader_addline(buffer, "<unhandled_rtype(%#x)>", reg->type);
2411 break;
2414 if (reg->type == WINED3DSPR_IMMCONST)
2416 shader_addline(buffer, "(");
2417 switch (reg->immconst_type)
2419 case WINED3D_IMMCONST_SCALAR:
2420 switch (reg->data_type)
2422 case WINED3D_DATA_FLOAT:
2423 shader_addline(buffer, "%.8e", *(const float *)reg->u.immconst_data);
2424 break;
2425 case WINED3D_DATA_INT:
2426 shader_addline(buffer, "%d", reg->u.immconst_data[0]);
2427 break;
2428 case WINED3D_DATA_RESOURCE:
2429 case WINED3D_DATA_SAMPLER:
2430 case WINED3D_DATA_UINT:
2431 shader_addline(buffer, "%u", reg->u.immconst_data[0]);
2432 break;
2433 default:
2434 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
2435 break;
2437 break;
2439 case WINED3D_IMMCONST_VEC4:
2440 switch (reg->data_type)
2442 case WINED3D_DATA_FLOAT:
2443 shader_addline(buffer, "%.8e, %.8e, %.8e, %.8e",
2444 *(const float *)&reg->u.immconst_data[0], *(const float *)&reg->u.immconst_data[1],
2445 *(const float *)&reg->u.immconst_data[2], *(const float *)&reg->u.immconst_data[3]);
2446 break;
2447 case WINED3D_DATA_INT:
2448 shader_addline(buffer, "%d, %d, %d, %d",
2449 reg->u.immconst_data[0], reg->u.immconst_data[1],
2450 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2451 break;
2452 case WINED3D_DATA_RESOURCE:
2453 case WINED3D_DATA_SAMPLER:
2454 case WINED3D_DATA_UINT:
2455 shader_addline(buffer, "%u, %u, %u, %u",
2456 reg->u.immconst_data[0], reg->u.immconst_data[1],
2457 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2458 break;
2459 default:
2460 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
2461 break;
2463 break;
2465 default:
2466 shader_addline(buffer, "<unhandled immconst_type %#x>", reg->immconst_type);
2467 break;
2469 shader_addline(buffer, ")");
2471 else if (reg->type != WINED3DSPR_RASTOUT
2472 && reg->type != WINED3DSPR_MISCTYPE
2473 && reg->type != WINED3DSPR_NULL)
2475 if (offset != ~0u)
2477 shader_addline(buffer, "[");
2478 if (reg->idx[0].rel_addr)
2480 shader_dump_src_param(buffer, reg->idx[0].rel_addr, shader_version);
2481 shader_addline(buffer, " + ");
2483 shader_addline(buffer, "%u]", offset);
2485 if (reg->idx[1].offset != ~0u)
2487 shader_addline(buffer, "[");
2488 if (reg->idx[1].rel_addr)
2490 shader_dump_src_param(buffer, reg->idx[1].rel_addr, shader_version);
2491 shader_addline(buffer, " + ");
2493 shader_addline(buffer, "%u]", reg->idx[1].offset);
2497 if (reg->type == WINED3DSPR_FUNCTIONPOINTER)
2498 shader_addline(buffer, "[%u]", reg->u.fp_body_idx);
2502 static void shader_dump_dst_param(struct wined3d_string_buffer *buffer,
2503 const struct wined3d_shader_dst_param *param, const struct wined3d_shader_version *shader_version)
2505 DWORD write_mask = param->write_mask;
2507 shader_dump_register(buffer, &param->reg, shader_version);
2509 if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
2511 static const char write_mask_chars[] = "xyzw";
2513 shader_addline(buffer, ".");
2514 if (write_mask & WINED3DSP_WRITEMASK_0)
2515 shader_addline(buffer, "%c", write_mask_chars[0]);
2516 if (write_mask & WINED3DSP_WRITEMASK_1)
2517 shader_addline(buffer, "%c", write_mask_chars[1]);
2518 if (write_mask & WINED3DSP_WRITEMASK_2)
2519 shader_addline(buffer, "%c", write_mask_chars[2]);
2520 if (write_mask & WINED3DSP_WRITEMASK_3)
2521 shader_addline(buffer, "%c", write_mask_chars[3]);
2525 static void shader_dump_src_param(struct wined3d_string_buffer *buffer,
2526 const struct wined3d_shader_src_param *param, const struct wined3d_shader_version *shader_version)
2528 enum wined3d_shader_src_modifier src_modifier = param->modifiers;
2529 DWORD swizzle = param->swizzle;
2531 if (src_modifier == WINED3DSPSM_NEG
2532 || src_modifier == WINED3DSPSM_BIASNEG
2533 || src_modifier == WINED3DSPSM_SIGNNEG
2534 || src_modifier == WINED3DSPSM_X2NEG
2535 || src_modifier == WINED3DSPSM_ABSNEG)
2536 shader_addline(buffer, "-");
2537 else if (src_modifier == WINED3DSPSM_COMP)
2538 shader_addline(buffer, "1-");
2539 else if (src_modifier == WINED3DSPSM_NOT)
2540 shader_addline(buffer, "!");
2542 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
2543 shader_addline(buffer, "abs(");
2545 shader_dump_register(buffer, &param->reg, shader_version);
2547 switch (src_modifier)
2549 case WINED3DSPSM_NONE: break;
2550 case WINED3DSPSM_NEG: break;
2551 case WINED3DSPSM_NOT: break;
2552 case WINED3DSPSM_BIAS: shader_addline(buffer, "_bias"); break;
2553 case WINED3DSPSM_BIASNEG: shader_addline(buffer, "_bias"); break;
2554 case WINED3DSPSM_SIGN: shader_addline(buffer, "_bx2"); break;
2555 case WINED3DSPSM_SIGNNEG: shader_addline(buffer, "_bx2"); break;
2556 case WINED3DSPSM_COMP: break;
2557 case WINED3DSPSM_X2: shader_addline(buffer, "_x2"); break;
2558 case WINED3DSPSM_X2NEG: shader_addline(buffer, "_x2"); break;
2559 case WINED3DSPSM_DZ: shader_addline(buffer, "_dz"); break;
2560 case WINED3DSPSM_DW: shader_addline(buffer, "_dw"); break;
2561 case WINED3DSPSM_ABSNEG: shader_addline(buffer, ")"); break;
2562 case WINED3DSPSM_ABS: shader_addline(buffer, ")"); break;
2563 default: shader_addline(buffer, "_unknown_modifier(%#x)", src_modifier);
2566 if (swizzle != WINED3DSP_NOSWIZZLE)
2568 static const char swizzle_chars[] = "xyzw";
2569 DWORD swizzle_x = swizzle & 0x03;
2570 DWORD swizzle_y = (swizzle >> 2) & 0x03;
2571 DWORD swizzle_z = (swizzle >> 4) & 0x03;
2572 DWORD swizzle_w = (swizzle >> 6) & 0x03;
2574 if (swizzle_x == swizzle_y
2575 && swizzle_x == swizzle_z
2576 && swizzle_x == swizzle_w)
2578 shader_addline(buffer, ".%c", swizzle_chars[swizzle_x]);
2580 else
2582 shader_addline(buffer, ".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
2583 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
2588 /* Shared code in order to generate the bulk of the shader string. */
2589 HRESULT shader_generate_code(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
2590 const struct wined3d_shader_reg_maps *reg_maps, void *backend_ctx,
2591 const DWORD *start, const DWORD *end)
2593 struct wined3d_device *device = shader->device;
2594 const struct wined3d_shader_frontend *fe = shader->frontend;
2595 void *fe_data = shader->frontend_data;
2596 struct wined3d_shader_version shader_version;
2597 struct wined3d_shader_parser_state state;
2598 struct wined3d_shader_instruction ins;
2599 struct wined3d_shader_tex_mx tex_mx;
2600 struct wined3d_shader_context ctx;
2601 const DWORD *ptr;
2603 /* Initialize current parsing state. */
2604 tex_mx.current_row = 0;
2605 state.current_loop_depth = 0;
2606 state.current_loop_reg = 0;
2607 state.in_subroutine = FALSE;
2609 ctx.shader = shader;
2610 ctx.reg_maps = reg_maps;
2611 ctx.buffer = buffer;
2612 ctx.tex_mx = &tex_mx;
2613 ctx.state = &state;
2614 ctx.backend_data = backend_ctx;
2615 ins.ctx = &ctx;
2617 fe->shader_read_header(fe_data, &ptr, &shader_version);
2618 if (start)
2619 ptr = start;
2621 while (!fe->shader_is_end(fe_data, &ptr) && ptr != end)
2623 /* Read opcode. */
2624 fe->shader_read_instruction(fe_data, &ptr, &ins);
2626 /* Unknown opcode and its parameters. */
2627 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
2629 WARN("Encountered unrecognised or invalid instruction.\n");
2630 return WINED3DERR_INVALIDCALL;
2633 if (ins.predicate)
2634 FIXME("Predicates not implemented.\n");
2636 /* Call appropriate function for output target */
2637 device->shader_backend->shader_handle_instruction(&ins);
2640 return WINED3D_OK;
2643 static void shader_dump_ins_modifiers(struct wined3d_string_buffer *buffer,
2644 const struct wined3d_shader_dst_param *dst)
2646 DWORD mmask = dst->modifiers;
2648 switch (dst->shift)
2650 case 0: break;
2651 case 13: shader_addline(buffer, "_d8"); break;
2652 case 14: shader_addline(buffer, "_d4"); break;
2653 case 15: shader_addline(buffer, "_d2"); break;
2654 case 1: shader_addline(buffer, "_x2"); break;
2655 case 2: shader_addline(buffer, "_x4"); break;
2656 case 3: shader_addline(buffer, "_x8"); break;
2657 default: shader_addline(buffer, "_unhandled_shift(%d)", dst->shift); break;
2660 if (mmask & WINED3DSPDM_SATURATE) shader_addline(buffer, "_sat");
2661 if (mmask & WINED3DSPDM_PARTIALPRECISION) shader_addline(buffer, "_pp");
2662 if (mmask & WINED3DSPDM_MSAMPCENTROID) shader_addline(buffer, "_centroid");
2664 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
2665 if (mmask) FIXME("Unrecognised modifier %#x.\n", mmask);
2668 static void shader_dump_primitive_type(struct wined3d_string_buffer *buffer,
2669 const struct wined3d_shader_primitive_type *primitive_type)
2671 switch (primitive_type->type)
2673 case WINED3D_PT_UNDEFINED:
2674 shader_addline(buffer, "undefined");
2675 break;
2676 case WINED3D_PT_POINTLIST:
2677 shader_addline(buffer, "pointlist");
2678 break;
2679 case WINED3D_PT_LINELIST:
2680 shader_addline(buffer, "linelist");
2681 break;
2682 case WINED3D_PT_LINESTRIP:
2683 shader_addline(buffer, "linestrip");
2684 break;
2685 case WINED3D_PT_TRIANGLELIST:
2686 shader_addline(buffer, "trianglelist");
2687 break;
2688 case WINED3D_PT_TRIANGLESTRIP:
2689 shader_addline(buffer, "trianglestrip");
2690 break;
2691 case WINED3D_PT_TRIANGLEFAN:
2692 shader_addline(buffer, "trianglefan");
2693 break;
2694 case WINED3D_PT_LINELIST_ADJ:
2695 shader_addline(buffer, "linelist_adj");
2696 break;
2697 case WINED3D_PT_LINESTRIP_ADJ:
2698 shader_addline(buffer, "linestrip_adj");
2699 break;
2700 case WINED3D_PT_TRIANGLELIST_ADJ:
2701 shader_addline(buffer, "trianglelist_adj");
2702 break;
2703 case WINED3D_PT_TRIANGLESTRIP_ADJ:
2704 shader_addline(buffer, "trianglestrip_adj");
2705 break;
2706 case WINED3D_PT_PATCH:
2707 shader_addline(buffer, "patch%u", primitive_type->patch_vertex_count);
2708 break;
2709 default:
2710 shader_addline(buffer, "<unrecognized_primitive_type %#x>", primitive_type->type);
2711 break;
2715 static void shader_dump_interpolation_mode(struct wined3d_string_buffer *buffer,
2716 enum wined3d_shader_interpolation_mode interpolation_mode)
2718 switch (interpolation_mode)
2720 case WINED3DSIM_CONSTANT:
2721 shader_addline(buffer, "constant");
2722 break;
2723 case WINED3DSIM_LINEAR:
2724 shader_addline(buffer, "linear");
2725 break;
2726 case WINED3DSIM_LINEAR_CENTROID:
2727 shader_addline(buffer, "linear centroid");
2728 break;
2729 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2730 shader_addline(buffer, "linear noperspective");
2731 break;
2732 case WINED3DSIM_LINEAR_SAMPLE:
2733 shader_addline(buffer, "linear sample");
2734 break;
2735 case WINED3DSIM_LINEAR_NOPERSPECTIVE_CENTROID:
2736 shader_addline(buffer, "linear noperspective centroid");
2737 break;
2738 case WINED3DSIM_LINEAR_NOPERSPECTIVE_SAMPLE:
2739 shader_addline(buffer, "linear noperspective sample");
2740 break;
2741 default:
2742 shader_addline(buffer, "<unrecognized_interpolation_mode %#x>", interpolation_mode);
2743 break;
2747 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data)
2749 struct wined3d_shader_version shader_version;
2750 struct wined3d_string_buffer buffer;
2751 const char *type_prefix;
2752 const char *p, *q;
2753 const DWORD *ptr;
2754 DWORD i;
2756 if (!string_buffer_init(&buffer))
2758 ERR("Failed to initialize string buffer.\n");
2759 return;
2762 fe->shader_read_header(fe_data, &ptr, &shader_version);
2764 TRACE("Parsing %p.\n", ptr);
2766 switch (shader_version.type)
2768 case WINED3D_SHADER_TYPE_VERTEX:
2769 type_prefix = "vs";
2770 break;
2772 case WINED3D_SHADER_TYPE_HULL:
2773 type_prefix = "hs";
2774 break;
2776 case WINED3D_SHADER_TYPE_DOMAIN:
2777 type_prefix = "ds";
2778 break;
2780 case WINED3D_SHADER_TYPE_GEOMETRY:
2781 type_prefix = "gs";
2782 break;
2784 case WINED3D_SHADER_TYPE_PIXEL:
2785 type_prefix = "ps";
2786 break;
2788 case WINED3D_SHADER_TYPE_COMPUTE:
2789 type_prefix = "cs";
2790 break;
2792 default:
2793 FIXME("Unhandled shader type %#x.\n", shader_version.type);
2794 type_prefix = "unknown";
2795 break;
2798 shader_addline(&buffer, "%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
2800 while (!fe->shader_is_end(fe_data, &ptr))
2802 struct wined3d_shader_instruction ins;
2804 fe->shader_read_instruction(fe_data, &ptr, &ins);
2805 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
2807 WARN("Skipping unrecognized instruction.\n");
2808 shader_addline(&buffer, "<unrecognized instruction>\n");
2809 continue;
2812 if (ins.handler_idx == WINED3DSIH_DCL || ins.handler_idx == WINED3DSIH_DCL_UAV_TYPED)
2814 shader_dump_decl_usage(&buffer, &ins.declaration.semantic, ins.flags, &shader_version);
2815 shader_dump_ins_modifiers(&buffer, &ins.declaration.semantic.reg);
2816 shader_addline(&buffer, " ");
2817 shader_dump_dst_param(&buffer, &ins.declaration.semantic.reg, &shader_version);
2819 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
2821 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2822 shader_dump_src_param(&buffer, &ins.declaration.src, &shader_version);
2823 shader_addline(&buffer, ", %s",
2824 ins.flags & WINED3DSI_INDEXED_DYNAMIC ? "dynamicIndexed" : "immediateIndexed");
2826 else if (ins.handler_idx == WINED3DSIH_DCL_FUNCTION_BODY)
2828 shader_addline(&buffer, "%s fb%u",
2829 shader_opcode_names[ins.handler_idx], ins.declaration.index);
2831 else if (ins.handler_idx == WINED3DSIH_DCL_FUNCTION_TABLE)
2833 shader_addline(&buffer, "%s ft%u = {...}",
2834 shader_opcode_names[ins.handler_idx], ins.declaration.index);
2836 else if (ins.handler_idx == WINED3DSIH_DCL_GLOBAL_FLAGS)
2838 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2839 shader_dump_global_flags(&buffer, ins.flags);
2841 else if (ins.handler_idx == WINED3DSIH_DCL_HS_MAX_TESSFACTOR)
2843 shader_addline(&buffer, "%s %.8e", shader_opcode_names[ins.handler_idx],
2844 ins.declaration.max_tessellation_factor);
2846 else if (ins.handler_idx == WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER)
2848 shader_addline(&buffer, "%s {\n", shader_opcode_names[ins.handler_idx]);
2849 for (i = 0; i < ins.declaration.icb->vec4_count; ++i)
2851 shader_addline(&buffer, " {0x%08x, 0x%08x, 0x%08x, 0x%08x},\n",
2852 ins.declaration.icb->data[4 * i + 0],
2853 ins.declaration.icb->data[4 * i + 1],
2854 ins.declaration.icb->data[4 * i + 2],
2855 ins.declaration.icb->data[4 * i + 3]);
2857 shader_addline(&buffer, "}");
2859 else if (ins.handler_idx == WINED3DSIH_DCL_INDEX_RANGE)
2861 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2862 shader_dump_dst_param(&buffer, &ins.declaration.index_range.first_register, &shader_version);
2863 shader_addline(&buffer, " %u", ins.declaration.index_range.last_register);
2865 else if (ins.handler_idx == WINED3DSIH_DCL_INDEXABLE_TEMP)
2867 shader_addline(&buffer, "%s x[%u][%u], %u", shader_opcode_names[ins.handler_idx],
2868 ins.declaration.indexable_temp.register_idx,
2869 ins.declaration.indexable_temp.register_size,
2870 ins.declaration.indexable_temp.component_count);
2872 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS)
2874 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2875 shader_dump_interpolation_mode(&buffer, ins.flags);
2876 shader_addline(&buffer, " ");
2877 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2879 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SGV
2880 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SGV
2881 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SIV
2882 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_SIV)
2884 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2885 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2886 shader_addline(&buffer, ", ");
2887 shader_dump_shader_input_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2889 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SIV)
2891 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2892 shader_dump_interpolation_mode(&buffer, ins.flags);
2893 shader_addline(&buffer, " ");
2894 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2895 shader_addline(&buffer, ", ");
2896 shader_dump_shader_input_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2898 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT
2899 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT)
2901 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2902 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2904 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE
2905 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
2907 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2908 shader_dump_primitive_type(&buffer, &ins.declaration.primitive_type);
2910 else if (ins.handler_idx == WINED3DSIH_DCL_INTERFACE)
2912 shader_addline(&buffer, "%s fp[%u][%u][%u] = {...}",
2913 shader_opcode_names[ins.handler_idx], ins.declaration.fp.index,
2914 ins.declaration.fp.array_size, ins.declaration.fp.body_count);
2916 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_RAW)
2918 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2919 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2921 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_STRUCTURED)
2923 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2924 shader_dump_dst_param(&buffer, &ins.declaration.structured_resource.reg, &shader_version);
2925 shader_addline(&buffer, ", %u", ins.declaration.structured_resource.byte_stride);
2927 else if (ins.handler_idx == WINED3DSIH_DCL_SAMPLER)
2929 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2930 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2931 if (ins.flags == WINED3DSI_SAMPLER_COMPARISON_MODE)
2932 shader_addline(&buffer, ", comparisonMode");
2934 else if (ins.handler_idx == WINED3DSIH_DCL_TEMPS
2935 || ins.handler_idx == WINED3DSIH_DCL_GS_INSTANCES
2936 || ins.handler_idx == WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT
2937 || ins.handler_idx == WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT
2938 || ins.handler_idx == WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT
2939 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT
2940 || ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
2942 shader_addline(&buffer, "%s %u", shader_opcode_names[ins.handler_idx], ins.declaration.count);
2944 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_DOMAIN)
2946 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2947 shader_dump_tessellator_domain(&buffer, ins.declaration.tessellator_domain);
2949 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE)
2951 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2952 shader_dump_tessellator_output_primitive(&buffer, ins.declaration.tessellator_output_primitive);
2954 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_PARTITIONING)
2956 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2957 shader_dump_tessellator_partitioning(&buffer, ins.declaration.tessellator_partitioning);
2959 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_RAW)
2961 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2962 shader_dump_dst_param(&buffer, &ins.declaration.tgsm_raw.reg, &shader_version);
2963 shader_addline(&buffer, ", %u", ins.declaration.tgsm_raw.byte_count);
2965 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_STRUCTURED)
2967 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2968 shader_dump_dst_param(&buffer, &ins.declaration.tgsm_structured.reg, &shader_version);
2969 shader_addline(&buffer, ", %u, %u", ins.declaration.tgsm_structured.byte_stride,
2970 ins.declaration.tgsm_structured.structure_count);
2972 else if (ins.handler_idx == WINED3DSIH_DCL_THREAD_GROUP)
2974 shader_addline(&buffer, "%s %u, %u, %u", shader_opcode_names[ins.handler_idx],
2975 ins.declaration.thread_group_size.x,
2976 ins.declaration.thread_group_size.y,
2977 ins.declaration.thread_group_size.z);
2979 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_RAW)
2981 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2982 shader_dump_uav_flags(&buffer, ins.flags);
2983 shader_addline(&buffer, " ");
2984 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2986 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_STRUCTURED)
2988 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2989 shader_dump_uav_flags(&buffer, ins.flags);
2990 shader_addline(&buffer, " ");
2991 shader_dump_dst_param(&buffer, &ins.declaration.structured_resource.reg, &shader_version);
2992 shader_addline(&buffer, ", %u", ins.declaration.structured_resource.byte_stride);
2994 else if (ins.handler_idx == WINED3DSIH_DEF)
2996 shader_addline(&buffer, "def c%u = %.8e, %.8e, %.8e, %.8e", shader_get_float_offset(ins.dst[0].reg.type,
2997 ins.dst[0].reg.idx[0].offset),
2998 *(const float *)&ins.src[0].reg.u.immconst_data[0],
2999 *(const float *)&ins.src[0].reg.u.immconst_data[1],
3000 *(const float *)&ins.src[0].reg.u.immconst_data[2],
3001 *(const float *)&ins.src[0].reg.u.immconst_data[3]);
3003 else if (ins.handler_idx == WINED3DSIH_DEFI)
3005 shader_addline(&buffer, "defi i%u = %d, %d, %d, %d", ins.dst[0].reg.idx[0].offset,
3006 ins.src[0].reg.u.immconst_data[0],
3007 ins.src[0].reg.u.immconst_data[1],
3008 ins.src[0].reg.u.immconst_data[2],
3009 ins.src[0].reg.u.immconst_data[3]);
3011 else if (ins.handler_idx == WINED3DSIH_DEFB)
3013 shader_addline(&buffer, "defb b%u = %s",
3014 ins.dst[0].reg.idx[0].offset, ins.src[0].reg.u.immconst_data[0] ? "true" : "false");
3016 else
3018 if (ins.predicate)
3020 shader_addline(&buffer, "(");
3021 shader_dump_src_param(&buffer, ins.predicate, &shader_version);
3022 shader_addline(&buffer, ") ");
3025 /* PixWin marks instructions with the coissue flag with a '+' */
3026 if (ins.coissue)
3027 shader_addline(&buffer, "+");
3029 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
3031 if (ins.handler_idx == WINED3DSIH_BREAKP
3032 || ins.handler_idx == WINED3DSIH_CONTINUEP
3033 || ins.handler_idx == WINED3DSIH_IF
3034 || ins.handler_idx == WINED3DSIH_RETP
3035 || ins.handler_idx == WINED3DSIH_TEXKILL)
3037 switch (ins.flags)
3039 case WINED3D_SHADER_CONDITIONAL_OP_NZ: shader_addline(&buffer, "_nz"); break;
3040 case WINED3D_SHADER_CONDITIONAL_OP_Z: shader_addline(&buffer, "_z"); break;
3041 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags); break;
3044 else if (ins.handler_idx == WINED3DSIH_IFC
3045 || ins.handler_idx == WINED3DSIH_BREAKC)
3047 switch (ins.flags)
3049 case WINED3D_SHADER_REL_OP_GT: shader_addline(&buffer, "_gt"); break;
3050 case WINED3D_SHADER_REL_OP_EQ: shader_addline(&buffer, "_eq"); break;
3051 case WINED3D_SHADER_REL_OP_GE: shader_addline(&buffer, "_ge"); break;
3052 case WINED3D_SHADER_REL_OP_LT: shader_addline(&buffer, "_lt"); break;
3053 case WINED3D_SHADER_REL_OP_NE: shader_addline(&buffer, "_ne"); break;
3054 case WINED3D_SHADER_REL_OP_LE: shader_addline(&buffer, "_le"); break;
3055 default: shader_addline(&buffer, "_(%u)", ins.flags);
3058 else if (ins.handler_idx == WINED3DSIH_TEX
3059 && shader_version.major >= 2
3060 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
3062 shader_addline(&buffer, "p");
3064 else if (ins.handler_idx == WINED3DSIH_RESINFO && ins.flags)
3066 switch (ins.flags)
3068 case WINED3DSI_RESINFO_RCP_FLOAT: shader_addline(&buffer, "_rcpFloat"); break;
3069 case WINED3DSI_RESINFO_UINT: shader_addline(&buffer, "_uint"); break;
3070 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags);
3073 else if (ins.handler_idx == WINED3DSIH_SAMPLE_INFO && ins.flags)
3075 switch (ins.flags)
3077 case WINED3DSI_SAMPLE_INFO_UINT: shader_addline(&buffer, "_uint"); break;
3078 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags);
3081 else if (ins.handler_idx == WINED3DSIH_SYNC)
3083 shader_dump_sync_flags(&buffer, ins.flags);
3085 else
3087 shader_dump_precise_flags(&buffer, ins.flags);
3090 if (wined3d_shader_instruction_has_texel_offset(&ins))
3091 shader_addline(&buffer, "(%d,%d,%d)", ins.texel_offset.u, ins.texel_offset.v, ins.texel_offset.w);
3093 if (ins.resource_type != WINED3D_SHADER_RESOURCE_NONE)
3095 shader_addline(&buffer, "(");
3096 shader_dump_resource_type(&buffer, ins.resource_type);
3097 shader_addline(&buffer, ")");
3100 if (ins.resource_data_type != WINED3D_DATA_FLOAT)
3101 shader_dump_data_type(&buffer, ins.resource_data_type);
3103 for (i = 0; i < ins.dst_count; ++i)
3105 shader_dump_ins_modifiers(&buffer, &ins.dst[i]);
3106 shader_addline(&buffer, !i ? " " : ", ");
3107 shader_dump_dst_param(&buffer, &ins.dst[i], &shader_version);
3110 /* Other source tokens */
3111 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
3113 shader_addline(&buffer, !i ? " " : ", ");
3114 shader_dump_src_param(&buffer, &ins.src[i - ins.dst_count], &shader_version);
3117 shader_addline(&buffer, "\n");
3120 for (p = buffer.buffer; *p; p = q)
3122 if (!(q = strstr(p, "\n")))
3123 q = p + strlen(p);
3124 else
3125 ++q;
3126 TRACE(" %.*s", (int)(q - p), p);
3129 string_buffer_free(&buffer);
3132 static void shader_cleanup(struct wined3d_shader *shader)
3134 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_HULL)
3136 heap_free(shader->u.hs.phases.control_point);
3137 heap_free(shader->u.hs.phases.fork);
3138 heap_free(shader->u.hs.phases.join);
3141 heap_free(shader->patch_constant_signature.elements);
3142 heap_free(shader->output_signature.elements);
3143 heap_free(shader->input_signature.elements);
3144 shader->device->shader_backend->shader_destroy(shader);
3145 shader_cleanup_reg_maps(&shader->reg_maps);
3146 heap_free(shader->byte_code);
3147 shader_delete_constant_list(&shader->constantsF);
3148 shader_delete_constant_list(&shader->constantsB);
3149 shader_delete_constant_list(&shader->constantsI);
3150 list_remove(&shader->shader_list_entry);
3152 if (shader->frontend && shader->frontend_data)
3153 shader->frontend->shader_free(shader->frontend_data);
3156 struct shader_none_priv
3158 const struct wined3d_vertex_pipe_ops *vertex_pipe;
3159 const struct wined3d_fragment_pipe_ops *fragment_pipe;
3160 BOOL ffp_proj_control;
3163 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
3164 static void shader_none_precompile(void *shader_priv, struct wined3d_shader *shader) {}
3165 static void shader_none_select_compute(void *shader_priv, struct wined3d_context *context,
3166 const struct wined3d_state *state) {}
3167 static void shader_none_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count) {}
3168 static void shader_none_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count) {}
3169 static void shader_none_load_constants(void *shader_priv, struct wined3d_context *context,
3170 const struct wined3d_state *state) {}
3171 static void shader_none_destroy(struct wined3d_shader *shader) {}
3172 static void shader_none_free_context_data(struct wined3d_context *context) {}
3173 static void shader_none_init_context_state(struct wined3d_context *context) {}
3175 /* Context activation is done by the caller. */
3176 static void shader_none_select(void *shader_priv, struct wined3d_context *context,
3177 const struct wined3d_state *state)
3179 struct shader_none_priv *priv = shader_priv;
3181 priv->vertex_pipe->vp_enable(context, !use_vs(state));
3182 priv->fragment_pipe->fp_enable(context, !use_ps(state));
3185 /* Context activation is done by the caller. */
3186 static void shader_none_disable(void *shader_priv, struct wined3d_context *context)
3188 struct shader_none_priv *priv = shader_priv;
3190 priv->vertex_pipe->vp_enable(context, FALSE);
3191 priv->fragment_pipe->fp_enable(context, FALSE);
3193 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
3194 | (1u << WINED3D_SHADER_TYPE_VERTEX)
3195 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
3196 | (1u << WINED3D_SHADER_TYPE_HULL)
3197 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
3198 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
3201 static HRESULT shader_none_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
3202 const struct wined3d_fragment_pipe_ops *fragment_pipe)
3204 struct fragment_caps fragment_caps;
3205 void *vertex_priv, *fragment_priv;
3206 struct shader_none_priv *priv;
3208 if (!(priv = heap_alloc(sizeof(*priv))))
3209 return E_OUTOFMEMORY;
3211 if (!(vertex_priv = vertex_pipe->vp_alloc(&none_shader_backend, priv)))
3213 ERR("Failed to initialize vertex pipe.\n");
3214 heap_free(priv);
3215 return E_FAIL;
3218 if (!(fragment_priv = fragment_pipe->alloc_private(&none_shader_backend, priv)))
3220 ERR("Failed to initialize fragment pipe.\n");
3221 vertex_pipe->vp_free(device, NULL);
3222 heap_free(priv);
3223 return E_FAIL;
3226 priv->vertex_pipe = vertex_pipe;
3227 priv->fragment_pipe = fragment_pipe;
3228 fragment_pipe->get_caps(device->adapter, &fragment_caps);
3229 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
3231 device->vertex_priv = vertex_priv;
3232 device->fragment_priv = fragment_priv;
3233 device->shader_priv = priv;
3235 return WINED3D_OK;
3238 static void shader_none_free(struct wined3d_device *device, struct wined3d_context *context)
3240 struct shader_none_priv *priv = device->shader_priv;
3242 priv->fragment_pipe->free_private(device, context);
3243 priv->vertex_pipe->vp_free(device, context);
3244 heap_free(priv);
3247 static BOOL shader_none_allocate_context_data(struct wined3d_context *context)
3249 return TRUE;
3252 static void shader_none_get_caps(const struct wined3d_adapter *adapter, struct shader_caps *caps)
3254 /* Set the shader caps to 0 for the none shader backend */
3255 memset(caps, 0, sizeof(*caps));
3258 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
3260 /* We "support" every possible fixup, since we don't support any shader
3261 * model, and will never have to actually sample a texture. */
3262 return TRUE;
3265 static BOOL shader_none_has_ffp_proj_control(void *shader_priv)
3267 struct shader_none_priv *priv = shader_priv;
3269 return priv->ffp_proj_control;
3272 const struct wined3d_shader_backend_ops none_shader_backend =
3274 shader_none_handle_instruction,
3275 shader_none_precompile,
3276 shader_none_select,
3277 shader_none_select_compute,
3278 shader_none_disable,
3279 shader_none_update_float_vertex_constants,
3280 shader_none_update_float_pixel_constants,
3281 shader_none_load_constants,
3282 shader_none_destroy,
3283 shader_none_alloc,
3284 shader_none_free,
3285 shader_none_allocate_context_data,
3286 shader_none_free_context_data,
3287 shader_none_init_context_state,
3288 shader_none_get_caps,
3289 shader_none_color_fixup_supported,
3290 shader_none_has_ffp_proj_control,
3293 static unsigned int shader_max_version_from_feature_level(enum wined3d_feature_level level)
3295 switch (level)
3297 case WINED3D_FEATURE_LEVEL_11_1:
3298 case WINED3D_FEATURE_LEVEL_11:
3299 return 5;
3300 case WINED3D_FEATURE_LEVEL_10_1:
3301 case WINED3D_FEATURE_LEVEL_10:
3302 return 4;
3303 case WINED3D_FEATURE_LEVEL_9_3:
3304 return 3;
3305 case WINED3D_FEATURE_LEVEL_9_2:
3306 case WINED3D_FEATURE_LEVEL_9_1:
3307 return 2;
3308 default:
3309 return 1;
3313 static HRESULT shader_set_function(struct wined3d_shader *shader, struct wined3d_device *device,
3314 enum wined3d_shader_type type, unsigned int float_const_count)
3316 const struct wined3d_d3d_info *d3d_info = &shader->device->adapter->d3d_info;
3317 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3318 const struct wined3d_shader_version *version = &reg_maps->shader_version;
3319 const struct wined3d_shader_frontend *fe;
3320 unsigned int backend_version;
3321 HRESULT hr;
3323 TRACE("shader %p, device %p, type %s, float_const_count %u.\n",
3324 shader, device, debug_shader_type(type), float_const_count);
3326 fe = shader->frontend;
3327 if (!(shader->frontend_data = fe->shader_init(shader->function,
3328 shader->functionLength, &shader->output_signature)))
3330 FIXME("Failed to initialize frontend.\n");
3331 return WINED3DERR_INVALIDCALL;
3334 /* First pass: trace shader. */
3335 if (TRACE_ON(d3d_shader))
3336 shader_trace_init(fe, shader->frontend_data);
3338 /* Second pass: figure out which registers are used, what the semantics are, etc. */
3339 if (FAILED(hr = shader_get_registers_used(shader, float_const_count)))
3340 return hr;
3342 if (version->type != type)
3344 WARN("Wrong shader type %s.\n", debug_shader_type(reg_maps->shader_version.type));
3345 return WINED3DERR_INVALIDCALL;
3347 if (version->major > shader_max_version_from_feature_level(device->feature_level))
3349 WARN("Shader version %u not supported by this device.\n", version->major);
3350 return WINED3DERR_INVALIDCALL;
3352 switch (type)
3354 case WINED3D_SHADER_TYPE_VERTEX:
3355 backend_version = d3d_info->limits.vs_version;
3356 break;
3357 case WINED3D_SHADER_TYPE_HULL:
3358 backend_version = d3d_info->limits.hs_version;
3359 break;
3360 case WINED3D_SHADER_TYPE_DOMAIN:
3361 backend_version = d3d_info->limits.ds_version;
3362 break;
3363 case WINED3D_SHADER_TYPE_GEOMETRY:
3364 backend_version = d3d_info->limits.gs_version;
3365 break;
3366 case WINED3D_SHADER_TYPE_PIXEL:
3367 backend_version = d3d_info->limits.ps_version;
3368 break;
3369 case WINED3D_SHADER_TYPE_COMPUTE:
3370 backend_version = d3d_info->limits.cs_version;
3371 break;
3372 default:
3373 FIXME("No backend version-checking for this shader type.\n");
3374 backend_version = 0;
3376 if (version->major > backend_version)
3378 WARN("Shader version %u.%u not supported by the current shader backend.\n",
3379 version->major, version->minor);
3380 return WINED3DERR_INVALIDCALL;
3383 shader->load_local_constsF = shader->lconst_inf_or_nan;
3385 return WINED3D_OK;
3388 ULONG CDECL wined3d_shader_incref(struct wined3d_shader *shader)
3390 ULONG refcount = InterlockedIncrement(&shader->ref);
3392 TRACE("%p increasing refcount to %u.\n", shader, refcount);
3394 return refcount;
3397 static void wined3d_shader_init_object(void *object)
3399 struct wined3d_shader *shader = object;
3400 struct wined3d_device *device = shader->device;
3402 list_add_head(&device->shaders, &shader->shader_list_entry);
3404 device->shader_backend->shader_precompile(device->shader_priv, shader);
3407 static void wined3d_shader_destroy_object(void *object)
3409 shader_cleanup(object);
3410 heap_free(object);
3413 ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
3415 ULONG refcount = InterlockedDecrement(&shader->ref);
3417 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
3419 if (!refcount)
3421 shader->parent_ops->wined3d_object_destroyed(shader->parent);
3422 wined3d_cs_destroy_object(shader->device->cs, wined3d_shader_destroy_object, shader);
3425 return refcount;
3428 void * CDECL wined3d_shader_get_parent(const struct wined3d_shader *shader)
3430 TRACE("shader %p.\n", shader);
3432 return shader->parent;
3435 HRESULT CDECL wined3d_shader_get_byte_code(const struct wined3d_shader *shader,
3436 void *byte_code, UINT *byte_code_size)
3438 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader, byte_code, byte_code_size);
3440 if (!byte_code)
3442 *byte_code_size = shader->byte_code_size;
3443 return WINED3D_OK;
3446 if (*byte_code_size < shader->byte_code_size)
3448 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
3449 * than the required size we should write the required size and
3450 * return D3DERR_MOREDATA. That's not actually true. */
3451 return WINED3DERR_INVALIDCALL;
3454 memcpy(byte_code, shader->byte_code, shader->byte_code_size);
3456 return WINED3D_OK;
3459 /* Set local constants for d3d8 shaders. */
3460 HRESULT CDECL wined3d_shader_set_local_constants_float(struct wined3d_shader *shader,
3461 UINT start_idx, const float *src_data, UINT count)
3463 UINT end_idx = start_idx + count;
3464 UINT i;
3466 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader, start_idx, src_data, count);
3468 if (end_idx > shader->limits->constant_float)
3470 WARN("end_idx %u > float constants limit %u.\n",
3471 end_idx, shader->limits->constant_float);
3472 end_idx = shader->limits->constant_float;
3475 for (i = start_idx; i < end_idx; ++i)
3477 struct wined3d_shader_lconst *lconst;
3478 float *value;
3480 if (!(lconst = heap_alloc(sizeof(*lconst))))
3481 return E_OUTOFMEMORY;
3483 lconst->idx = i;
3484 value = (float *)lconst->value;
3485 memcpy(value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
3486 list_add_head(&shader->constantsF, &lconst->entry);
3488 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
3489 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
3491 shader->lconst_inf_or_nan = TRUE;
3495 return WINED3D_OK;
3498 static void init_interpolation_compile_args(DWORD *interpolation_args,
3499 const struct wined3d_shader *pixel_shader, const struct wined3d_d3d_info *d3d_info)
3501 if (!d3d_info->shader_output_interpolation || !pixel_shader
3502 || pixel_shader->reg_maps.shader_version.major < 4)
3504 memset(interpolation_args, 0, sizeof(pixel_shader->u.ps.interpolation_mode));
3505 return;
3508 memcpy(interpolation_args, pixel_shader->u.ps.interpolation_mode,
3509 sizeof(pixel_shader->u.ps.interpolation_mode));
3512 void find_vs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3513 WORD swizzle_map, struct vs_compile_args *args, const struct wined3d_context *context)
3515 const struct wined3d_shader *geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
3516 const struct wined3d_shader *pixel_shader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3517 const struct wined3d_shader *hull_shader = state->shader[WINED3D_SHADER_TYPE_HULL];
3518 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3520 args->fog_src = state->render_states[WINED3D_RS_FOGTABLEMODE]
3521 == WINED3D_FOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
3522 args->clip_enabled = state->render_states[WINED3D_RS_CLIPPING]
3523 && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
3524 args->point_size = state->primitive_type == WINED3D_PT_POINTLIST;
3525 args->per_vertex_point_size = shader->reg_maps.point_size;
3526 args->next_shader_type = hull_shader ? WINED3D_SHADER_TYPE_HULL
3527 : geometry_shader ? WINED3D_SHADER_TYPE_GEOMETRY : WINED3D_SHADER_TYPE_PIXEL;
3528 if (shader->reg_maps.shader_version.major >= 4)
3529 args->next_shader_input_count = hull_shader ? hull_shader->limits->packed_input
3530 : geometry_shader ? geometry_shader->limits->packed_input
3531 : pixel_shader ? pixel_shader->limits->packed_input : 0;
3532 else
3533 args->next_shader_input_count = 0;
3534 args->swizzle_map = swizzle_map;
3535 if (d3d_info->emulated_flatshading)
3536 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
3537 else
3538 args->flatshading = 0;
3540 init_interpolation_compile_args(args->interpolation_mode,
3541 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL ? pixel_shader : NULL, d3d_info);
3544 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
3546 if (usage_idx1 != usage_idx2)
3547 return FALSE;
3548 if (usage1 == usage2)
3549 return TRUE;
3550 if (usage1 == WINED3D_DECL_USAGE_POSITION && usage2 == WINED3D_DECL_USAGE_POSITIONT)
3551 return TRUE;
3552 if (usage2 == WINED3D_DECL_USAGE_POSITION && usage1 == WINED3D_DECL_USAGE_POSITIONT)
3553 return TRUE;
3555 return FALSE;
3558 BOOL vshader_get_input(const struct wined3d_shader *shader,
3559 BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
3561 WORD map = shader->reg_maps.input_registers;
3562 unsigned int i;
3564 for (i = 0; map; map >>= 1, ++i)
3566 if (!(map & 1)) continue;
3568 if (match_usage(shader->u.vs.attributes[i].usage,
3569 shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
3571 *regnum = i;
3572 return TRUE;
3575 return FALSE;
3578 static HRESULT shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3579 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3581 HRESULT hr;
3583 TRACE("byte_code %p, byte_code_size %#lx.\n", desc->byte_code, (long)desc->byte_code_size);
3585 if (!desc->byte_code)
3586 return WINED3DERR_INVALIDCALL;
3588 shader->ref = 1;
3589 shader->device = device;
3590 shader->parent = parent;
3591 shader->parent_ops = parent_ops;
3593 list_init(&shader->linked_programs);
3594 list_init(&shader->constantsF);
3595 list_init(&shader->constantsB);
3596 list_init(&shader->constantsI);
3597 shader->lconst_inf_or_nan = FALSE;
3598 list_init(&shader->reg_maps.indexable_temps);
3599 list_init(&shader->shader_list_entry);
3601 if (desc->byte_code_size == ~(size_t)0)
3603 struct wined3d_shader_version shader_version;
3604 const struct wined3d_shader_frontend *fe;
3605 struct wined3d_shader_instruction ins;
3606 const DWORD *ptr;
3607 void *fe_data;
3609 if (!(shader->frontend = shader_select_frontend(WINED3D_SHADER_BYTE_CODE_FORMAT_SM1)))
3611 FIXME("Unable to find frontend for shader.\n");
3612 hr = WINED3DERR_INVALIDCALL;
3613 goto fail;
3616 fe = shader->frontend;
3617 if (!(fe_data = fe->shader_init(desc->byte_code, desc->byte_code_size, &shader->output_signature)))
3619 WARN("Failed to initialise frontend data.\n");
3620 hr = WINED3DERR_INVALIDCALL;
3621 goto fail;
3624 fe->shader_read_header(fe_data, &ptr, &shader_version);
3625 while (!fe->shader_is_end(fe_data, &ptr))
3626 fe->shader_read_instruction(fe_data, &ptr, &ins);
3628 fe->shader_free(fe_data);
3630 shader->byte_code_size = (ptr - desc->byte_code) * sizeof(*ptr);
3632 if (!(shader->byte_code = heap_alloc(shader->byte_code_size)))
3634 hr = E_OUTOFMEMORY;
3635 goto fail;
3637 memcpy(shader->byte_code, desc->byte_code, shader->byte_code_size);
3639 shader->function = shader->byte_code;
3640 shader->functionLength = shader->byte_code_size;
3642 else
3644 enum wined3d_shader_byte_code_format format;
3645 unsigned int max_version;
3647 if (!(shader->byte_code = heap_alloc(desc->byte_code_size)))
3649 hr = E_OUTOFMEMORY;
3650 goto fail;
3652 memcpy(shader->byte_code, desc->byte_code, desc->byte_code_size);
3653 shader->byte_code_size = desc->byte_code_size;
3655 max_version = shader_max_version_from_feature_level(device->feature_level);
3656 if (FAILED(hr = shader_extract_from_dxbc(shader, max_version, &format)))
3657 goto fail;
3659 if (!(shader->frontend = shader_select_frontend(format)))
3661 FIXME("Unable to find frontend for shader.\n");
3662 hr = WINED3DERR_INVALIDCALL;
3663 goto fail;
3667 return WINED3D_OK;
3669 fail:
3670 shader_cleanup(shader);
3671 return hr;
3674 static HRESULT vertex_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3675 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3677 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3678 unsigned int i;
3679 HRESULT hr;
3681 if (FAILED(hr = shader_init(shader, device, desc, parent, parent_ops)))
3682 return hr;
3684 if (FAILED(hr = shader_set_function(shader, device,
3685 WINED3D_SHADER_TYPE_VERTEX, device->adapter->d3d_info.limits.vs_uniform_count)))
3687 shader_cleanup(shader);
3688 return hr;
3691 for (i = 0; i < shader->input_signature.element_count; ++i)
3693 const struct wined3d_shader_signature_element *input = &shader->input_signature.elements[i];
3695 if (!(reg_maps->input_registers & (1u << input->register_idx)) || !input->semantic_name)
3696 continue;
3698 shader->u.vs.attributes[input->register_idx].usage =
3699 shader_usage_from_semantic_name(input->semantic_name);
3700 shader->u.vs.attributes[input->register_idx].usage_idx = input->semantic_idx;
3703 if (reg_maps->usesrelconstF && !list_empty(&shader->constantsF))
3704 shader->load_local_constsF = TRUE;
3706 return WINED3D_OK;
3709 static struct wined3d_shader_signature_element *shader_find_signature_element(const struct wined3d_shader_signature *s,
3710 unsigned int stream_idx, const char *semantic_name, unsigned int semantic_idx)
3712 struct wined3d_shader_signature_element *e = s->elements;
3713 unsigned int i;
3715 for (i = 0; i < s->element_count; ++i)
3717 if (e[i].stream_idx == stream_idx
3718 && !_strnicmp(e[i].semantic_name, semantic_name, -1)
3719 && e[i].semantic_idx == semantic_idx)
3720 return &e[i];
3723 return NULL;
3726 BOOL shader_get_stream_output_register_info(const struct wined3d_shader *shader,
3727 const struct wined3d_stream_output_element *so_element, unsigned int *register_idx, unsigned int *component_idx)
3729 const struct wined3d_shader_signature_element *output;
3730 unsigned int idx;
3732 if (!(output = shader_find_signature_element(&shader->output_signature,
3733 so_element->stream_idx, so_element->semantic_name, so_element->semantic_idx)))
3734 return FALSE;
3736 for (idx = 0; idx < 4; ++idx)
3738 if (output->mask & (1u << idx))
3739 break;
3741 idx += so_element->component_idx;
3743 *register_idx = output->register_idx;
3744 *component_idx = idx;
3745 return TRUE;
3748 static HRESULT geometry_shader_init_so_desc(struct wined3d_geometry_shader *gs, struct wined3d_device *device,
3749 const struct wined3d_stream_output_desc *so_desc)
3751 struct wined3d_so_desc_entry *s;
3752 struct wine_rb_entry *entry;
3753 unsigned int i;
3754 size_t size;
3755 char *name;
3757 if ((entry = wine_rb_get(&device->so_descs, so_desc)))
3759 gs->so_desc = &WINE_RB_ENTRY_VALUE(entry, struct wined3d_so_desc_entry, entry)->desc;
3760 return WINED3D_OK;
3763 size = FIELD_OFFSET(struct wined3d_so_desc_entry, elements[so_desc->element_count]);
3764 for (i = 0; i < so_desc->element_count; ++i)
3766 const char *n = so_desc->elements[i].semantic_name;
3768 if (n)
3769 size += strlen(n) + 1;
3771 if (!(s = heap_alloc(size)))
3772 return E_OUTOFMEMORY;
3774 s->desc = *so_desc;
3776 memcpy(s->elements, so_desc->elements, so_desc->element_count * sizeof(*s->elements));
3777 s->desc.elements = s->elements;
3779 name = (char *)&s->elements[s->desc.element_count];
3780 for (i = 0; i < so_desc->element_count; ++i)
3782 struct wined3d_stream_output_element *e = &s->elements[i];
3784 if (!e->semantic_name)
3785 continue;
3787 size = strlen(e->semantic_name) + 1;
3788 memcpy(name, e->semantic_name, size);
3789 e->semantic_name = name;
3790 name += size;
3793 if (wine_rb_put(&device->so_descs, &s->desc, &s->entry) == -1)
3795 heap_free(s);
3796 return E_FAIL;
3798 gs->so_desc = &s->desc;
3800 return WINED3D_OK;
3803 static HRESULT geometry_shader_init_stream_output(struct wined3d_shader *shader,
3804 const struct wined3d_stream_output_desc *so_desc)
3806 const struct wined3d_shader_frontend *fe = shader->frontend;
3807 const struct wined3d_shader_signature_element *output;
3808 unsigned int i, component_idx, register_idx, mask;
3809 struct wined3d_shader_version shader_version;
3810 const DWORD *ptr;
3811 void *fe_data;
3812 HRESULT hr;
3814 if (!so_desc)
3815 return WINED3D_OK;
3817 if (!(fe_data = fe->shader_init(shader->function, shader->functionLength, &shader->output_signature)))
3819 WARN("Failed to initialise frontend data.\n");
3820 return WINED3DERR_INVALIDCALL;
3822 fe->shader_read_header(fe_data, &ptr, &shader_version);
3823 fe->shader_free(fe_data);
3825 switch (shader_version.type)
3827 case WINED3D_SHADER_TYPE_VERTEX:
3828 case WINED3D_SHADER_TYPE_DOMAIN:
3829 shader->function = NULL;
3830 shader->functionLength = 0;
3831 break;
3832 case WINED3D_SHADER_TYPE_GEOMETRY:
3833 break;
3834 default:
3835 WARN("Wrong shader type %s.\n", debug_shader_type(shader_version.type));
3836 return E_INVALIDARG;
3839 if (!shader->function)
3841 shader->reg_maps.shader_version = shader_version;
3842 shader->reg_maps.shader_version.type = WINED3D_SHADER_TYPE_GEOMETRY;
3843 shader_set_limits(shader);
3844 if (FAILED(hr = shader_scan_output_signature(shader)))
3845 return hr;
3848 for (i = 0; i < so_desc->element_count; ++i)
3850 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
3852 if (!e->semantic_name)
3853 continue;
3854 if (!(output = shader_find_signature_element(&shader->output_signature,
3855 e->stream_idx, e->semantic_name, e->semantic_idx))
3856 || !shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
3858 WARN("Failed to find output signature element for stream output entry.\n");
3859 return E_INVALIDARG;
3862 mask = ((1u << e->component_count) - 1) << component_idx;
3863 if ((output->mask & 0xff & mask) != mask)
3865 WARN("Invalid component range %u-%u (mask %#x), output mask %#x.\n",
3866 component_idx, e->component_count, mask, output->mask & 0xff);
3867 return E_INVALIDARG;
3871 if (FAILED(hr = geometry_shader_init_so_desc(&shader->u.gs, shader->device, so_desc)))
3873 WARN("Failed to initialise stream output description, hr %#x.\n", hr);
3874 return hr;
3877 return WINED3D_OK;
3880 static HRESULT geometry_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3881 const struct wined3d_shader_desc *desc, const struct wined3d_stream_output_desc *so_desc,
3882 void *parent, const struct wined3d_parent_ops *parent_ops)
3884 HRESULT hr;
3886 if (FAILED(hr = shader_init(shader, device, desc, parent, parent_ops)))
3887 return hr;
3889 if (FAILED(hr = geometry_shader_init_stream_output(shader, so_desc)))
3890 goto fail;
3892 if (shader->function
3893 && FAILED(hr = shader_set_function(shader, device, WINED3D_SHADER_TYPE_GEOMETRY, 0)))
3894 goto fail;
3896 return WINED3D_OK;
3898 fail:
3899 shader_cleanup(shader);
3900 return hr;
3903 void find_ds_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3904 struct ds_compile_args *args, const struct wined3d_context *context)
3906 const struct wined3d_shader *geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
3907 const struct wined3d_shader *pixel_shader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3908 const struct wined3d_shader *hull_shader = state->shader[WINED3D_SHADER_TYPE_HULL];
3910 args->tessellator_output_primitive = hull_shader->u.hs.tessellator_output_primitive;
3911 args->tessellator_partitioning = hull_shader->u.hs.tessellator_partitioning;
3913 args->output_count = geometry_shader ? geometry_shader->limits->packed_input
3914 : pixel_shader ? pixel_shader->limits->packed_input : shader->limits->packed_output;
3915 args->next_shader_type = geometry_shader ? WINED3D_SHADER_TYPE_GEOMETRY : WINED3D_SHADER_TYPE_PIXEL;
3917 args->render_offscreen = context->render_offscreen;
3919 init_interpolation_compile_args(args->interpolation_mode,
3920 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL ? pixel_shader : NULL, context->d3d_info);
3922 args->padding = 0;
3925 void find_gs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3926 struct gs_compile_args *args, const struct wined3d_context *context)
3928 const struct wined3d_shader *pixel_shader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3930 args->output_count = pixel_shader ? pixel_shader->limits->packed_input : shader->limits->packed_output;
3932 if (!(args->primitive_type = shader->u.gs.input_type))
3933 args->primitive_type = state->primitive_type;
3935 init_interpolation_compile_args(args->interpolation_mode, pixel_shader, context->d3d_info);
3938 void find_ps_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3939 BOOL position_transformed, struct ps_compile_args *args, const struct wined3d_context *context)
3941 const struct wined3d_gl_info *gl_info = &context->device->adapter->gl_info;
3942 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3943 struct wined3d_texture *texture;
3944 unsigned int i;
3946 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
3947 if (!d3d_info->srgb_write_control && needs_srgb_write(d3d_info, state, &state->fb))
3949 static unsigned int warned = 0;
3951 args->srgb_correction = 1;
3952 if (state->blend_state && state->blend_state->desc.rt[0].enable && !warned++)
3953 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
3954 "support, expect rendering artifacts.\n");
3957 if (shader->reg_maps.shader_version.major == 1
3958 && shader->reg_maps.shader_version.minor <= 3)
3960 for (i = 0; i < shader->limits->sampler; ++i)
3962 DWORD flags = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
3964 if (flags & WINED3D_TTFF_PROJECTED)
3966 DWORD tex_transform = flags & ~WINED3D_TTFF_PROJECTED;
3968 if (!state->shader[WINED3D_SHADER_TYPE_VERTEX])
3970 enum wined3d_shader_resource_type resource_type = shader->reg_maps.resource_info[i].type;
3971 unsigned int j;
3972 unsigned int index = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
3973 DWORD max_valid = WINED3D_TTFF_COUNT4;
3975 for (j = 0; j < state->vertex_declaration->element_count; ++j)
3977 struct wined3d_vertex_declaration_element *element =
3978 &state->vertex_declaration->elements[j];
3980 if (element->usage == WINED3D_DECL_USAGE_TEXCOORD
3981 && element->usage_idx == index)
3983 max_valid = element->format->component_count;
3984 break;
3987 if (!tex_transform || tex_transform > max_valid)
3989 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
3990 tex_transform, max_valid);
3991 tex_transform = max_valid;
3993 if ((resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1D && tex_transform > WINED3D_TTFF_COUNT1)
3994 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D
3995 && tex_transform > WINED3D_TTFF_COUNT2)
3996 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_3D
3997 && tex_transform > WINED3D_TTFF_COUNT3))
3998 tex_transform |= WINED3D_PSARGS_PROJECTED;
3999 else
4001 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
4002 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
4003 i, tex_transform, resource_type);
4006 else
4007 tex_transform = WINED3D_TTFF_COUNT4 | WINED3D_PSARGS_PROJECTED;
4009 args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
4013 if (shader->reg_maps.shader_version.major == 1
4014 && shader->reg_maps.shader_version.minor <= 4)
4016 for (i = 0; i < shader->limits->sampler; ++i)
4018 if (!shader->reg_maps.resource_info[i].type)
4019 continue;
4021 /* Treat unbound textures as 2D. The dummy texture will provide
4022 * the proper sample value. The tex_types bitmap defaults to
4023 * 2D because of the memset. */
4024 if (!(texture = state->textures[i]))
4025 continue;
4027 switch (wined3d_texture_gl(texture)->target)
4029 /* RECT textures are distinguished from 2D textures via np2_fixup */
4030 default:
4031 break;
4033 case GL_TEXTURE_3D:
4034 args->tex_types |= WINED3D_SHADER_TEX_3D << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
4035 break;
4037 case GL_TEXTURE_CUBE_MAP_ARB:
4038 args->tex_types |= WINED3D_SHADER_TEX_CUBE << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
4039 break;
4043 else if (shader->reg_maps.shader_version.major <= 3)
4045 for (i = 0; i < shader->limits->sampler; ++i)
4047 enum wined3d_shader_resource_type resource_type;
4048 enum wined3d_shader_tex_types tex_type;
4050 if (!(resource_type = shader->reg_maps.resource_info[i].type))
4051 continue;
4053 switch (resource_type)
4055 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
4056 tex_type = WINED3D_SHADER_TEX_3D;
4057 break;
4058 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
4059 tex_type = WINED3D_SHADER_TEX_CUBE;
4060 break;
4061 default:
4062 tex_type = WINED3D_SHADER_TEX_2D;
4063 break;
4066 if ((texture = state->textures[i]))
4068 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
4069 && resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_3D
4070 && !(texture->resource.usage & WINED3DUSAGE_LEGACY_CUBEMAP))
4071 tex_type = WINED3D_SHADER_TEX_2D;
4072 else if (texture->resource.type == WINED3D_RTYPE_TEXTURE_3D
4073 && resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D)
4074 tex_type = WINED3D_SHADER_TEX_3D;
4076 args->tex_types |= tex_type << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
4080 if (shader->reg_maps.shader_version.major >= 4)
4082 /* In SM4+ we use dcl_sampler in order to determine if we should use shadow sampler. */
4083 args->shadow = 0;
4084 for (i = 0 ; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
4085 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
4086 args->np2_fixup = 0;
4088 else
4090 for (i = 0; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
4092 if (!shader->reg_maps.resource_info[i].type)
4093 continue;
4095 texture = state->textures[i];
4096 if (!texture)
4098 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
4099 continue;
4101 if (can_use_texture_swizzle(d3d_info, texture->resource.format))
4102 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
4103 else
4104 args->color_fixup[i] = texture->resource.format->color_fixup;
4106 if (texture->resource.format_flags & WINED3DFMT_FLAG_SHADOW)
4107 args->shadow |= 1u << i;
4109 /* Flag samplers that need NP2 texcoord fixup. */
4110 if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
4111 args->np2_fixup |= (1u << i);
4115 if (shader->reg_maps.shader_version.major >= 3)
4117 if (position_transformed)
4118 args->vp_mode = WINED3D_VP_MODE_NONE;
4119 else if (use_vs(state))
4120 args->vp_mode = WINED3D_VP_MODE_SHADER;
4121 else
4122 args->vp_mode = WINED3D_VP_MODE_FF;
4123 args->fog = WINED3D_FFP_PS_FOG_OFF;
4125 else
4127 args->vp_mode = WINED3D_VP_MODE_SHADER;
4128 if (state->render_states[WINED3D_RS_FOGENABLE])
4130 switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
4132 case WINED3D_FOG_NONE:
4133 if (position_transformed || use_vs(state))
4135 args->fog = WINED3D_FFP_PS_FOG_LINEAR;
4136 break;
4139 switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
4141 case WINED3D_FOG_NONE: /* Fall through. */
4142 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
4143 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
4144 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
4146 break;
4148 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
4149 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
4150 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
4153 else
4155 args->fog = WINED3D_FFP_PS_FOG_OFF;
4159 if (!d3d_info->full_ffp_varyings)
4161 const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
4163 args->texcoords_initialized = 0;
4164 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
4166 if (vs)
4168 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.output_registers & (1u << i))
4169 args->texcoords_initialized |= 1u << i;
4171 else
4173 const struct wined3d_stream_info *si = &context->stream_info;
4174 unsigned int coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
4176 if ((state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX] >> WINED3D_FFP_TCI_SHIFT)
4177 & WINED3D_FFP_TCI_MASK
4178 || (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))))
4179 args->texcoords_initialized |= 1u << i;
4183 else
4185 args->texcoords_initialized = (1u << WINED3D_MAX_TEXTURES) - 1;
4188 args->pointsprite = state->render_states[WINED3D_RS_POINTSPRITEENABLE]
4189 && state->primitive_type == WINED3D_PT_POINTLIST;
4191 if (d3d_info->ffp_alpha_test)
4192 args->alpha_test_func = WINED3D_CMP_ALWAYS - 1;
4193 else
4194 args->alpha_test_func = (state->render_states[WINED3D_RS_ALPHATESTENABLE]
4195 ? wined3d_sanitize_cmp_func(state->render_states[WINED3D_RS_ALPHAFUNC])
4196 : WINED3D_CMP_ALWAYS) - 1;
4198 if (d3d_info->emulated_flatshading)
4199 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
4201 args->render_offscreen = shader->reg_maps.vpos && gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS]
4202 ? context->render_offscreen : 0;
4204 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
4206 struct wined3d_rendertarget_view *rtv = state->fb.render_targets[i];
4207 if (rtv && rtv->format->id == WINED3DFMT_A8_UNORM && !is_identity_fixup(rtv->format->color_fixup))
4208 args->rt_alpha_swizzle |= 1u << i;
4211 args->dual_source_blend = state->blend_state && state->blend_state->dual_source;
4214 static HRESULT pixel_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
4215 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
4217 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4218 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
4219 HRESULT hr;
4221 if (FAILED(hr = shader_init(shader, device, desc, parent, parent_ops)))
4222 return hr;
4224 if (FAILED(hr = shader_set_function(shader, device,
4225 WINED3D_SHADER_TYPE_PIXEL, device->adapter->d3d_info.limits.ps_uniform_count)))
4227 shader_cleanup(shader);
4228 return hr;
4231 for (i = 0; i < MAX_REG_INPUT; ++i)
4233 if (shader->u.ps.input_reg_used & (1u << i))
4235 ++num_regs_used;
4236 highest_reg_used = i;
4240 /* Don't do any register mapping magic if it is not needed, or if we can't
4241 * achieve anything anyway */
4242 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
4243 || num_regs_used > (gl_info->limits.glsl_varyings / 4)
4244 || shader->reg_maps.shader_version.major >= 4)
4246 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
4248 /* This happens with relative addressing. The input mapper function
4249 * warns about this if the higher registers are declared too, so
4250 * don't write a FIXME here */
4251 WARN("More varying registers used than supported\n");
4254 for (i = 0; i < MAX_REG_INPUT; ++i)
4256 shader->u.ps.input_reg_map[i] = i;
4259 shader->u.ps.declared_in_count = highest_reg_used + 1;
4261 else
4263 shader->u.ps.declared_in_count = 0;
4264 for (i = 0; i < MAX_REG_INPUT; ++i)
4266 if (shader->u.ps.input_reg_used & (1u << i))
4267 shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
4268 else shader->u.ps.input_reg_map[i] = ~0U;
4272 return WINED3D_OK;
4275 enum wined3d_shader_resource_type pixelshader_get_resource_type(const struct wined3d_shader_reg_maps *reg_maps,
4276 unsigned int resource_idx, DWORD tex_types)
4278 static enum wined3d_shader_resource_type shader_resource_type_from_shader_tex_types[] =
4280 WINED3D_SHADER_RESOURCE_TEXTURE_2D, /* WINED3D_SHADER_TEX_2D */
4281 WINED3D_SHADER_RESOURCE_TEXTURE_3D, /* WINED3D_SHADER_TEX_3D */
4282 WINED3D_SHADER_RESOURCE_TEXTURE_CUBE, /* WINED3D_SHADER_TEX_CUBE */
4285 unsigned int idx;
4287 if (reg_maps->shader_version.major > 3)
4288 return reg_maps->resource_info[resource_idx].type;
4290 if (!reg_maps->resource_info[resource_idx].type)
4291 return 0;
4293 idx = (tex_types >> resource_idx * WINED3D_PSARGS_TEXTYPE_SHIFT) & WINED3D_PSARGS_TEXTYPE_MASK;
4294 assert(idx < ARRAY_SIZE(shader_resource_type_from_shader_tex_types));
4295 return shader_resource_type_from_shader_tex_types[idx];
4298 HRESULT CDECL wined3d_shader_create_cs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4299 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4301 struct wined3d_shader *object;
4302 HRESULT hr;
4304 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4305 device, desc, parent, parent_ops, shader);
4307 if (!(object = heap_alloc_zero(sizeof(*object))))
4308 return E_OUTOFMEMORY;
4310 if (FAILED(hr = shader_init(object, device, desc, parent, parent_ops)))
4312 WARN("Failed to initialize compute shader, hr %#x.\n", hr);
4313 heap_free(object);
4314 return hr;
4317 if (FAILED(hr = shader_set_function(object, device, WINED3D_SHADER_TYPE_COMPUTE, 0)))
4319 shader_cleanup(object);
4320 heap_free(object);
4321 return hr;
4324 wined3d_cs_init_object(device->cs, wined3d_shader_init_object, object);
4326 TRACE("Created compute shader %p.\n", object);
4327 *shader = object;
4329 return WINED3D_OK;
4332 HRESULT CDECL wined3d_shader_create_ds(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4333 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4335 struct wined3d_shader *object;
4336 HRESULT hr;
4338 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4339 device, desc, parent, parent_ops, shader);
4341 if (!(object = heap_alloc_zero(sizeof(*object))))
4342 return E_OUTOFMEMORY;
4344 if (FAILED(hr = shader_init(object, device, desc, parent, parent_ops)))
4346 WARN("Failed to initialize domain shader, hr %#x.\n", hr);
4347 heap_free(object);
4348 return hr;
4351 if (FAILED(hr = shader_set_function(object, device, WINED3D_SHADER_TYPE_DOMAIN, 0)))
4353 shader_cleanup(object);
4354 heap_free(object);
4355 return hr;
4358 wined3d_cs_init_object(device->cs, wined3d_shader_init_object, object);
4360 TRACE("Created domain shader %p.\n", object);
4361 *shader = object;
4363 return WINED3D_OK;
4366 HRESULT CDECL wined3d_shader_create_gs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4367 const struct wined3d_stream_output_desc *so_desc, void *parent,
4368 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4370 struct wined3d_shader *object;
4371 HRESULT hr;
4373 TRACE("device %p, desc %p, so_desc %p, parent %p, parent_ops %p, shader %p.\n",
4374 device, desc, so_desc, parent, parent_ops, shader);
4376 if (!(object = heap_alloc_zero(sizeof(*object))))
4377 return E_OUTOFMEMORY;
4379 if (FAILED(hr = geometry_shader_init(object, device, desc, so_desc, parent, parent_ops)))
4381 WARN("Failed to initialize geometry shader, hr %#x.\n", hr);
4382 heap_free(object);
4383 return hr;
4386 wined3d_cs_init_object(device->cs, wined3d_shader_init_object, object);
4388 TRACE("Created geometry shader %p.\n", object);
4389 *shader = object;
4391 return WINED3D_OK;
4394 HRESULT CDECL wined3d_shader_create_hs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4395 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4397 struct wined3d_shader *object;
4398 HRESULT hr;
4400 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4401 device, desc, parent, parent_ops, shader);
4403 if (!(object = heap_alloc_zero(sizeof(*object))))
4404 return E_OUTOFMEMORY;
4406 if (FAILED(hr = shader_init(object, device, desc, parent, parent_ops)))
4408 WARN("Failed to initialize hull shader, hr %#x.\n", hr);
4409 heap_free(object);
4410 return hr;
4413 if (FAILED(hr = shader_set_function(object, device, WINED3D_SHADER_TYPE_HULL, 0)))
4415 shader_cleanup(object);
4416 heap_free(object);
4417 return hr;
4420 wined3d_cs_init_object(device->cs, wined3d_shader_init_object, object);
4422 TRACE("Created hull shader %p.\n", object);
4423 *shader = object;
4425 return WINED3D_OK;
4428 HRESULT CDECL wined3d_shader_create_ps(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4429 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4431 struct wined3d_shader *object;
4432 HRESULT hr;
4434 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4435 device, desc, parent, parent_ops, shader);
4437 if (!(object = heap_alloc_zero(sizeof(*object))))
4438 return E_OUTOFMEMORY;
4440 if (FAILED(hr = pixel_shader_init(object, device, desc, parent, parent_ops)))
4442 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
4443 heap_free(object);
4444 return hr;
4447 wined3d_cs_init_object(device->cs, wined3d_shader_init_object, object);
4449 TRACE("Created pixel shader %p.\n", object);
4450 *shader = object;
4452 return WINED3D_OK;
4455 HRESULT CDECL wined3d_shader_create_vs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4456 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4458 struct wined3d_shader *object;
4459 HRESULT hr;
4461 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4462 device, desc, parent, parent_ops, shader);
4464 if (!(object = heap_alloc_zero(sizeof(*object))))
4465 return E_OUTOFMEMORY;
4467 if (FAILED(hr = vertex_shader_init(object, device, desc, parent, parent_ops)))
4469 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
4470 heap_free(object);
4471 return hr;
4474 wined3d_cs_init_object(device->cs, wined3d_shader_init_object, object);
4476 TRACE("Created vertex shader %p.\n", object);
4477 *shader = object;
4479 return WINED3D_OK;