wevtapi: Add EvtOpenLog stub.
[wine.git] / dlls / wined3d / shader.c
blob3707d1587f9d4dad7d6fcb90bf4e7c378bac5ef5
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 /* pow, mul_high, sub_high, mul_low */
36 const float wined3d_srgb_const0[] = {0.41666f, 1.055f, 0.055f, 12.92f};
37 /* cmp */
38 const float wined3d_srgb_const1[] = {0.0031308f, 0.0f, 0.0f, 0.0f};
40 static const char * const shader_opcode_names[] =
42 /* WINED3DSIH_ABS */ "abs",
43 /* WINED3DSIH_ADD */ "add",
44 /* WINED3DSIH_AND */ "and",
45 /* WINED3DSIH_ATOMIC_AND */ "atomic_and",
46 /* WINED3DSIH_ATOMIC_CMP_STORE */ "atomic_cmp_store",
47 /* WINED3DSIH_ATOMIC_IADD */ "atomic_iadd",
48 /* WINED3DSIH_ATOMIC_IMAX */ "atomic_imax",
49 /* WINED3DSIH_ATOMIC_IMIN */ "atomic_imin",
50 /* WINED3DSIH_ATOMIC_OR */ "atomic_or",
51 /* WINED3DSIH_ATOMIC_UMAX */ "atomic_umax",
52 /* WINED3DSIH_ATOMIC_UMIN */ "atomic_umin",
53 /* WINED3DSIH_ATOMIC_XOR */ "atomic_xor",
54 /* WINED3DSIH_BEM */ "bem",
55 /* WINED3DSIH_BFI */ "bfi",
56 /* WINED3DSIH_BFREV */ "bfrev",
57 /* WINED3DSIH_BREAK */ "break",
58 /* WINED3DSIH_BREAKC */ "breakc",
59 /* WINED3DSIH_BREAKP */ "breakp",
60 /* WINED3DSIH_BUFINFO */ "bufinfo",
61 /* WINED3DSIH_CALL */ "call",
62 /* WINED3DSIH_CALLNZ */ "callnz",
63 /* WINED3DSIH_CASE */ "case",
64 /* WINED3DSIH_CMP */ "cmp",
65 /* WINED3DSIH_CND */ "cnd",
66 /* WINED3DSIH_CONTINUE */ "continue",
67 /* WINED3DSIH_CONTINUEP */ "continuec",
68 /* WINED3DSIH_COUNTBITS */ "countbits",
69 /* WINED3DSIH_CRS */ "crs",
70 /* WINED3DSIH_CUT */ "cut",
71 /* WINED3DSIH_CUT_STREAM */ "cut_stream",
72 /* WINED3DSIH_DCL */ "dcl",
73 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
74 /* WINED3DSIH_DCL_FUNCTION_BODY */ "dcl_function_body",
75 /* WINED3DSIH_DCL_FUNCTION_TABLE */ "dcl_function_table",
76 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ "dcl_globalFlags",
77 /* WINED3DSIH_DCL_GS_INSTANCES */ "dcl_gs_instances",
78 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ "dcl_hs_fork_phase_instance_count",
79 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ "dcl_hs_join_phase_instance_count",
80 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ "dcl_hs_max_tessfactor",
81 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ "dcl_immediateConstantBuffer",
82 /* WINED3DSIH_DCL_INDEX_RANGE */ "dcl_index_range",
83 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ "dcl_indexableTemp",
84 /* WINED3DSIH_DCL_INPUT */ "dcl_input",
85 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ "dcl_input_control_point_count",
86 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
87 /* WINED3DSIH_DCL_INPUT_PS */ "dcl_input_ps",
88 /* WINED3DSIH_DCL_INPUT_PS_SGV */ "dcl_input_ps_sgv",
89 /* WINED3DSIH_DCL_INPUT_PS_SIV */ "dcl_input_ps_siv",
90 /* WINED3DSIH_DCL_INPUT_SGV */ "dcl_input_sgv",
91 /* WINED3DSIH_DCL_INPUT_SIV */ "dcl_input_siv",
92 /* WINED3DSIH_DCL_INTERFACE */ "dcl_interface",
93 /* WINED3DSIH_DCL_OUTPUT */ "dcl_output",
94 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ "dcl_output_control_point_count",
95 /* WINED3DSIH_DCL_OUTPUT_SIV */ "dcl_output_siv",
96 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
97 /* WINED3DSIH_DCL_RESOURCE_RAW */ "dcl_resource_raw",
98 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ "dcl_resource_structured",
99 /* WINED3DSIH_DCL_SAMPLER */ "dcl_sampler",
100 /* WINED3DSIH_DCL_STREAM */ "dcl_stream",
101 /* WINED3DSIH_DCL_TEMPS */ "dcl_temps",
102 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ "dcl_tessellator_domain",
103 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ "dcl_tessellator_output_primitive",
104 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ "dcl_tessellator_partitioning",
105 /* WINED3DSIH_DCL_TGSM_RAW */ "dcl_tgsm_raw",
106 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ "dcl_tgsm_structured",
107 /* WINED3DSIH_DCL_THREAD_GROUP */ "dcl_thread_group",
108 /* WINED3DSIH_DCL_UAV_RAW */ "dcl_uav_raw",
109 /* WINED3DSIH_DCL_UAV_STRUCTURED */ "dcl_uav_structured",
110 /* WINED3DSIH_DCL_UAV_TYPED */ "dcl_uav_typed",
111 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
112 /* WINED3DSIH_DEF */ "def",
113 /* WINED3DSIH_DEFAULT */ "default",
114 /* WINED3DSIH_DEFB */ "defb",
115 /* WINED3DSIH_DEFI */ "defi",
116 /* WINED3DSIH_DIV */ "div",
117 /* WINED3DSIH_DP2 */ "dp2",
118 /* WINED3DSIH_DP2ADD */ "dp2add",
119 /* WINED3DSIH_DP3 */ "dp3",
120 /* WINED3DSIH_DP4 */ "dp4",
121 /* WINED3DSIH_DST */ "dst",
122 /* WINED3DSIH_DSX */ "dsx",
123 /* WINED3DSIH_DSX_COARSE */ "deriv_rtx_coarse",
124 /* WINED3DSIH_DSX_FINE */ "deriv_rtx_fine",
125 /* WINED3DSIH_DSY */ "dsy",
126 /* WINED3DSIH_DSY_COARSE */ "deriv_rty_coarse",
127 /* WINED3DSIH_DSY_FINE */ "deriv_rty_fine",
128 /* WINED3DSIH_ELSE */ "else",
129 /* WINED3DSIH_EMIT */ "emit",
130 /* WINED3DSIH_EMIT_STREAM */ "emit_stream",
131 /* WINED3DSIH_ENDIF */ "endif",
132 /* WINED3DSIH_ENDLOOP */ "endloop",
133 /* WINED3DSIH_ENDREP */ "endrep",
134 /* WINED3DSIH_ENDSWITCH */ "endswitch",
135 /* WINED3DSIH_EQ */ "eq",
136 /* WINED3DSIH_EXP */ "exp",
137 /* WINED3DSIH_EXPP */ "expp",
138 /* WINED3DSIH_F16TOF32 */ "f16tof32",
139 /* WINED3DSIH_F32TOF16 */ "f32tof16",
140 /* WINED3DSIH_FCALL */ "fcall",
141 /* WINED3DSIH_FIRSTBIT_HI */ "firstbit_hi",
142 /* WINED3DSIH_FIRSTBIT_LO */ "firstbit_lo",
143 /* WINED3DSIH_FIRSTBIT_SHI */ "firstbit_shi",
144 /* WINED3DSIH_FRC */ "frc",
145 /* WINED3DSIH_FTOI */ "ftoi",
146 /* WINED3DSIH_FTOU */ "ftou",
147 /* WINED3DSIH_GATHER4 */ "gather4",
148 /* WINED3DSIH_GATHER4_C */ "gather4_c",
149 /* WINED3DSIH_GATHER4_PO */ "gather4_po",
150 /* WINED3DSIH_GATHER4_PO_C */ "gather4_po_c",
151 /* WINED3DSIH_GE */ "ge",
152 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ "hs_control_point_phase",
153 /* WINED3DSIH_HS_DECLS */ "hs_decls",
154 /* WINED3DSIH_HS_FORK_PHASE */ "hs_fork_phase",
155 /* WINED3DSIH_HS_JOIN_PHASE */ "hs_join_phase",
156 /* WINED3DSIH_IADD */ "iadd",
157 /* WINED3DSIH_IBFE */ "ibfe",
158 /* WINED3DSIH_IEQ */ "ieq",
159 /* WINED3DSIH_IF */ "if",
160 /* WINED3DSIH_IFC */ "ifc",
161 /* WINED3DSIH_IGE */ "ige",
162 /* WINED3DSIH_ILT */ "ilt",
163 /* WINED3DSIH_IMAD */ "imad",
164 /* WINED3DSIH_IMAX */ "imax",
165 /* WINED3DSIH_IMIN */ "imin",
166 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ "imm_atomic_alloc",
167 /* WINED3DSIH_IMM_ATOMIC_AND */ "imm_atomic_and",
168 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ "imm_atomic_cmp_exch",
169 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ "imm_atomic_consume",
170 /* WINED3DSIH_IMM_ATOMIC_EXCH */ "imm_atomic_exch",
171 /* WINED3DSIH_IMM_ATOMIC_IADD */ "imm_atomic_iadd",
172 /* WINED3DSIH_IMM_ATOMIC_IMAX */ "imm_atomic_imax",
173 /* WINED3DSIH_IMM_ATOMIC_IMIN */ "imm_atomic_imin",
174 /* WINED3DSIH_IMM_ATOMIC_OR */ "imm_atomic_or",
175 /* WINED3DSIH_IMM_ATOMIC_UMAX */ "imm_atomic_umax",
176 /* WINED3DSIH_IMM_ATOMIC_UMIN */ "imm_atomic_umin",
177 /* WINED3DSIH_IMM_ATOMIC_XOR */ "imm_atomic_xor",
178 /* WINED3DSIH_IMUL */ "imul",
179 /* WINED3DSIH_INE */ "ine",
180 /* WINED3DSIH_INEG */ "ineg",
181 /* WINED3DSIH_ISHL */ "ishl",
182 /* WINED3DSIH_ISHR */ "ishr",
183 /* WINED3DSIH_ITOF */ "itof",
184 /* WINED3DSIH_LABEL */ "label",
185 /* WINED3DSIH_LD */ "ld",
186 /* WINED3DSIH_LD2DMS */ "ld2dms",
187 /* WINED3DSIH_LD_RAW */ "ld_raw",
188 /* WINED3DSIH_LD_STRUCTURED */ "ld_structured",
189 /* WINED3DSIH_LD_UAV_TYPED */ "ld_uav_typed",
190 /* WINED3DSIH_LIT */ "lit",
191 /* WINED3DSIH_LOD */ "lod",
192 /* WINED3DSIH_LOG */ "log",
193 /* WINED3DSIH_LOGP */ "logp",
194 /* WINED3DSIH_LOOP */ "loop",
195 /* WINED3DSIH_LRP */ "lrp",
196 /* WINED3DSIH_LT */ "lt",
197 /* WINED3DSIH_M3x2 */ "m3x2",
198 /* WINED3DSIH_M3x3 */ "m3x3",
199 /* WINED3DSIH_M3x4 */ "m3x4",
200 /* WINED3DSIH_M4x3 */ "m4x3",
201 /* WINED3DSIH_M4x4 */ "m4x4",
202 /* WINED3DSIH_MAD */ "mad",
203 /* WINED3DSIH_MAX */ "max",
204 /* WINED3DSIH_MIN */ "min",
205 /* WINED3DSIH_MOV */ "mov",
206 /* WINED3DSIH_MOVA */ "mova",
207 /* WINED3DSIH_MOVC */ "movc",
208 /* WINED3DSIH_MUL */ "mul",
209 /* WINED3DSIH_NE */ "ne",
210 /* WINED3DSIH_NOP */ "nop",
211 /* WINED3DSIH_NOT */ "not",
212 /* WINED3DSIH_NRM */ "nrm",
213 /* WINED3DSIH_OR */ "or",
214 /* WINED3DSIH_PHASE */ "phase",
215 /* WINED3DSIH_POW */ "pow",
216 /* WINED3DSIH_RCP */ "rcp",
217 /* WINED3DSIH_REP */ "rep",
218 /* WINED3DSIH_RESINFO */ "resinfo",
219 /* WINED3DSIH_RET */ "ret",
220 /* WINED3DSIH_RETP */ "retp",
221 /* WINED3DSIH_ROUND_NE */ "round_ne",
222 /* WINED3DSIH_ROUND_NI */ "round_ni",
223 /* WINED3DSIH_ROUND_PI */ "round_pi",
224 /* WINED3DSIH_ROUND_Z */ "round_z",
225 /* WINED3DSIH_RSQ */ "rsq",
226 /* WINED3DSIH_SAMPLE */ "sample",
227 /* WINED3DSIH_SAMPLE_B */ "sample_b",
228 /* WINED3DSIH_SAMPLE_C */ "sample_c",
229 /* WINED3DSIH_SAMPLE_C_LZ */ "sample_c_lz",
230 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
231 /* WINED3DSIH_SAMPLE_INFO */ "sample_info",
232 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
233 /* WINED3DSIH_SAMPLE_POS */ "sample_pos",
234 /* WINED3DSIH_SETP */ "setp",
235 /* WINED3DSIH_SGE */ "sge",
236 /* WINED3DSIH_SGN */ "sgn",
237 /* WINED3DSIH_SINCOS */ "sincos",
238 /* WINED3DSIH_SLT */ "slt",
239 /* WINED3DSIH_SQRT */ "sqrt",
240 /* WINED3DSIH_STORE_RAW */ "store_raw",
241 /* WINED3DSIH_STORE_STRUCTURED */ "store_structured",
242 /* WINED3DSIH_STORE_UAV_TYPED */ "store_uav_typed",
243 /* WINED3DSIH_SUB */ "sub",
244 /* WINED3DSIH_SWAPC */ "swapc",
245 /* WINED3DSIH_SWITCH */ "switch",
246 /* WINED3DSIH_SYNC */ "sync",
247 /* WINED3DSIH_TEX */ "texld",
248 /* WINED3DSIH_TEXBEM */ "texbem",
249 /* WINED3DSIH_TEXBEML */ "texbeml",
250 /* WINED3DSIH_TEXCOORD */ "texcrd",
251 /* WINED3DSIH_TEXDEPTH */ "texdepth",
252 /* WINED3DSIH_TEXDP3 */ "texdp3",
253 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
254 /* WINED3DSIH_TEXKILL */ "texkill",
255 /* WINED3DSIH_TEXLDD */ "texldd",
256 /* WINED3DSIH_TEXLDL */ "texldl",
257 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
258 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
259 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
260 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
261 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
262 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
263 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
264 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
265 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
266 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
267 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
268 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
269 /* WINED3DSIH_UBFE */ "ubfe",
270 /* WINED3DSIH_UDIV */ "udiv",
271 /* WINED3DSIH_UGE */ "uge",
272 /* WINED3DSIH_ULT */ "ult",
273 /* WINED3DSIH_UMAX */ "umax",
274 /* WINED3DSIH_UMIN */ "umin",
275 /* WINED3DSIH_UMUL */ "umul",
276 /* WINED3DSIH_USHR */ "ushr",
277 /* WINED3DSIH_UTOF */ "utof",
278 /* WINED3DSIH_XOR */ "xor",
281 static const char * const semantic_names[] =
283 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
284 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
285 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
286 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
287 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
288 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
289 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
290 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
291 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
292 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
293 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
294 /* WINED3D_DECL_USAGE_FOG */ "FOG",
295 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
296 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
299 static const struct
301 enum wined3d_shader_input_sysval_semantic sysval_semantic;
302 const char *sysval_name;
304 shader_input_sysval_semantic_names[] =
306 {WINED3D_SIV_POSITION, "position"},
307 {WINED3D_SIV_CLIP_DISTANCE, "clip_distance"},
308 {WINED3D_SIV_CULL_DISTANCE, "cull_distance"},
309 {WINED3D_SIV_RENDER_TARGET_ARRAY_INDEX, "render_target_array_index"},
310 {WINED3D_SIV_VIEWPORT_ARRAY_INDEX, "viewport_array_index"},
311 {WINED3D_SIV_VERTEX_ID, "vertex_id"},
312 {WINED3D_SIV_INSTANCE_ID, "instance_id"},
313 {WINED3D_SIV_PRIMITIVE_ID, "primitive_id"},
314 {WINED3D_SIV_IS_FRONT_FACE, "is_front_face"},
315 {WINED3D_SIV_SAMPLE_INDEX, "sample_index"},
316 {WINED3D_SIV_QUAD_U0_TESS_FACTOR, "finalQuadUeq0EdgeTessFactor"},
317 {WINED3D_SIV_QUAD_V0_TESS_FACTOR, "finalQuadVeq0EdgeTessFactor"},
318 {WINED3D_SIV_QUAD_U1_TESS_FACTOR, "finalQuadUeq1EdgeTessFactor"},
319 {WINED3D_SIV_QUAD_V1_TESS_FACTOR, "finalQuadVeq1EdgeTessFactor"},
320 {WINED3D_SIV_QUAD_U_INNER_TESS_FACTOR, "finalQuadUInsideTessFactor"},
321 {WINED3D_SIV_QUAD_V_INNER_TESS_FACTOR, "finalQuadVInsideTessFactor"},
322 {WINED3D_SIV_TRIANGLE_U_TESS_FACTOR, "finalTriUeq0EdgeTessFactor"},
323 {WINED3D_SIV_TRIANGLE_V_TESS_FACTOR, "finalTriVeq0EdgeTessFactor"},
324 {WINED3D_SIV_TRIANGLE_W_TESS_FACTOR, "finalTriWeq0EdgeTessFactor"},
325 {WINED3D_SIV_TRIANGLE_INNER_TESS_FACTOR, "finalTriInsideTessFactor"},
326 {WINED3D_SIV_LINE_DETAIL_TESS_FACTOR, "finalLineDetailTessFactor"},
327 {WINED3D_SIV_LINE_DENSITY_TESS_FACTOR, "finalLineDensityTessFactor"},
330 static void shader_dump_src_param(struct wined3d_string_buffer *buffer,
331 const struct wined3d_shader_src_param *param, const struct wined3d_shader_version *shader_version);
333 const char *debug_d3dshaderinstructionhandler(enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx)
335 if (handler_idx >= ARRAY_SIZE(shader_opcode_names))
336 return wine_dbg_sprintf("UNRECOGNIZED(%#x)", handler_idx);
338 return shader_opcode_names[handler_idx];
341 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage)
343 if (usage >= ARRAY_SIZE(semantic_names))
345 FIXME("Unrecognized usage %#x.\n", usage);
346 return "UNRECOGNIZED";
349 return semantic_names[usage];
352 static enum wined3d_decl_usage shader_usage_from_semantic_name(const char *name)
354 unsigned int i;
356 for (i = 0; i < ARRAY_SIZE(semantic_names); ++i)
358 if (!strcmp(name, semantic_names[i]))
359 return i;
362 return ~0U;
365 static enum wined3d_sysval_semantic shader_sysval_semantic_from_usage(enum wined3d_decl_usage usage)
367 switch (usage)
369 case WINED3D_DECL_USAGE_POSITION:
370 return WINED3D_SV_POSITION;
371 default:
372 return 0;
376 BOOL shader_match_semantic(const char *semantic_name, enum wined3d_decl_usage usage)
378 return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
381 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
382 const struct wined3d_shader_semantic *s)
384 e->semantic_name = shader_semantic_name_from_usage(s->usage);
385 e->semantic_idx = s->usage_idx;
386 e->stream_idx = 0;
387 e->sysval_semantic = shader_sysval_semantic_from_usage(s->usage);
388 e->component_type = WINED3D_TYPE_FLOAT;
389 e->register_idx = s->reg.reg.idx[0].offset;
390 e->mask = s->reg.write_mask;
393 static void shader_signature_from_usage(struct wined3d_shader_signature_element *e,
394 enum wined3d_decl_usage usage, UINT usage_idx, UINT reg_idx, DWORD write_mask)
396 e->semantic_name = shader_semantic_name_from_usage(usage);
397 e->semantic_idx = usage_idx;
398 e->stream_idx = 0;
399 e->sysval_semantic = shader_sysval_semantic_from_usage(usage);
400 e->component_type = WINED3D_TYPE_FLOAT;
401 e->register_idx = reg_idx;
402 e->mask = write_mask;
405 static const struct wined3d_shader_frontend *shader_select_frontend(enum wined3d_shader_byte_code_format format)
407 switch (format)
409 case WINED3D_SHADER_BYTE_CODE_FORMAT_SM1:
410 return &sm1_shader_frontend;
412 case WINED3D_SHADER_BYTE_CODE_FORMAT_SM4:
413 return &sm4_shader_frontend;
415 default:
416 WARN("Invalid byte code format %#x specified.\n", format);
417 return NULL;
421 void string_buffer_clear(struct wined3d_string_buffer *buffer)
423 buffer->buffer[0] = '\0';
424 buffer->content_size = 0;
427 BOOL string_buffer_init(struct wined3d_string_buffer *buffer)
429 buffer->buffer_size = 32;
430 if (!(buffer->buffer = HeapAlloc(GetProcessHeap(), 0, buffer->buffer_size)))
432 ERR("Failed to allocate shader buffer memory.\n");
433 return FALSE;
436 string_buffer_clear(buffer);
437 return TRUE;
440 void string_buffer_free(struct wined3d_string_buffer *buffer)
442 HeapFree(GetProcessHeap(), 0, buffer->buffer);
445 BOOL string_buffer_resize(struct wined3d_string_buffer *buffer, int rc)
447 char *new_buffer;
448 unsigned int new_buffer_size = buffer->buffer_size * 2;
450 while (rc > 0 && (unsigned int)rc >= new_buffer_size - buffer->content_size)
451 new_buffer_size *= 2;
452 if (!(new_buffer = HeapReAlloc(GetProcessHeap(), 0, buffer->buffer, new_buffer_size)))
454 ERR("Failed to grow buffer.\n");
455 buffer->buffer[buffer->content_size] = '\0';
456 return FALSE;
458 buffer->buffer = new_buffer;
459 buffer->buffer_size = new_buffer_size;
460 return TRUE;
463 int shader_vaddline(struct wined3d_string_buffer *buffer, const char *format, va_list args)
465 unsigned int rem;
466 int rc;
468 rem = buffer->buffer_size - buffer->content_size;
469 rc = vsnprintf(&buffer->buffer[buffer->content_size], rem, format, args);
470 if (rc < 0 /* C89 */ || (unsigned int)rc >= rem /* C99 */)
471 return rc;
473 buffer->content_size += rc;
474 return 0;
477 int shader_addline(struct wined3d_string_buffer *buffer, const char *format, ...)
479 va_list args;
480 int ret;
482 for (;;)
484 va_start(args, format);
485 ret = shader_vaddline(buffer, format, args);
486 va_end(args);
487 if (!ret)
488 return ret;
489 if (!string_buffer_resize(buffer, ret))
490 return -1;
494 struct wined3d_string_buffer *string_buffer_get(struct wined3d_string_buffer_list *list)
496 struct wined3d_string_buffer *buffer;
498 if (list_empty(&list->list))
500 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer));
501 if (!buffer || !string_buffer_init(buffer))
503 ERR("Couldn't allocate buffer for temporary string.\n");
504 HeapFree(GetProcessHeap(), 0, buffer);
505 return NULL;
508 else
510 buffer = LIST_ENTRY(list_head(&list->list), struct wined3d_string_buffer, entry);
511 list_remove(&buffer->entry);
513 string_buffer_clear(buffer);
514 return buffer;
517 static int string_buffer_vsprintf(struct wined3d_string_buffer *buffer, const char *format, va_list args)
519 if (!buffer)
520 return 0;
521 string_buffer_clear(buffer);
522 return shader_vaddline(buffer, format, args);
525 void string_buffer_sprintf(struct wined3d_string_buffer *buffer, const char *format, ...)
527 va_list args;
528 int ret;
530 for (;;)
532 va_start(args, format);
533 ret = string_buffer_vsprintf(buffer, format, args);
534 va_end(args);
535 if (!ret)
536 return;
537 if (!string_buffer_resize(buffer, ret))
538 return;
542 void string_buffer_release(struct wined3d_string_buffer_list *list, struct wined3d_string_buffer *buffer)
544 if (!buffer)
545 return;
546 list_add_head(&list->list, &buffer->entry);
549 void string_buffer_list_init(struct wined3d_string_buffer_list *list)
551 list_init(&list->list);
554 void string_buffer_list_cleanup(struct wined3d_string_buffer_list *list)
556 struct wined3d_string_buffer *buffer, *buffer_next;
558 LIST_FOR_EACH_ENTRY_SAFE(buffer, buffer_next, &list->list, struct wined3d_string_buffer, entry)
560 string_buffer_free(buffer);
561 HeapFree(GetProcessHeap(), 0, buffer);
563 list_init(&list->list);
566 /* Convert floating point offset relative to a register file to an absolute
567 * offset for float constants. */
568 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type, UINT register_idx)
570 switch (register_type)
572 case WINED3DSPR_CONST: return register_idx;
573 case WINED3DSPR_CONST2: return 2048 + register_idx;
574 case WINED3DSPR_CONST3: return 4096 + register_idx;
575 case WINED3DSPR_CONST4: return 6144 + register_idx;
576 default:
577 FIXME("Unsupported register type: %u.\n", register_type);
578 return register_idx;
582 static void shader_delete_constant_list(struct list *clist)
584 struct wined3d_shader_lconst *constant, *constant_next;
586 LIST_FOR_EACH_ENTRY_SAFE(constant, constant_next, clist, struct wined3d_shader_lconst, entry)
587 HeapFree(GetProcessHeap(), 0, constant);
588 list_init(clist);
591 static void shader_set_limits(struct wined3d_shader *shader)
593 static const struct limits_entry
595 unsigned int min_version;
596 unsigned int max_version;
597 struct wined3d_shader_limits limits;
599 vs_limits[] =
601 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
602 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
603 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
604 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
605 * even though they are capable of supporting much more (GL
606 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
607 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
608 * shaders to 256. */
609 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
610 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
611 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 0}},
614 hs_limits[] =
616 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
617 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
619 ds_limits[] =
621 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
622 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
624 gs_limits[] =
626 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
627 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
628 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
631 ps_limits[] =
633 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
634 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
635 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
636 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
637 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
638 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 10}},
639 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 32}},
642 cs_limits[] =
644 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
645 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 0}},
647 const struct limits_entry *limits_array;
648 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
649 shader->reg_maps.shader_version.minor);
650 int i = 0;
652 switch (shader->reg_maps.shader_version.type)
654 default:
655 FIXME("Unexpected shader type %u found.\n", shader->reg_maps.shader_version.type);
656 /* Fall-through. */
657 case WINED3D_SHADER_TYPE_VERTEX:
658 limits_array = vs_limits;
659 break;
660 case WINED3D_SHADER_TYPE_HULL:
661 limits_array = hs_limits;
662 break;
663 case WINED3D_SHADER_TYPE_DOMAIN:
664 limits_array = ds_limits;
665 break;
666 case WINED3D_SHADER_TYPE_GEOMETRY:
667 limits_array = gs_limits;
668 break;
669 case WINED3D_SHADER_TYPE_PIXEL:
670 limits_array = ps_limits;
671 break;
672 case WINED3D_SHADER_TYPE_COMPUTE:
673 limits_array = cs_limits;
674 break;
677 while (limits_array[i].min_version && limits_array[i].min_version <= shader_version)
679 if (shader_version <= limits_array[i].max_version)
681 shader->limits = &limits_array[i].limits;
682 break;
684 ++i;
686 if (!shader->limits)
688 FIXME("Unexpected shader version \"%u.%u\".\n",
689 shader->reg_maps.shader_version.major,
690 shader->reg_maps.shader_version.minor);
691 shader->limits = &limits_array[max(0, i - 1)].limits;
695 static BOOL shader_record_register_usage(struct wined3d_shader *shader, struct wined3d_shader_reg_maps *reg_maps,
696 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type, unsigned int constf_size)
698 switch (reg->type)
700 case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
701 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
702 reg_maps->texcoord |= 1u << reg->idx[0].offset;
703 else
704 reg_maps->address |= 1u << reg->idx[0].offset;
705 break;
707 case WINED3DSPR_TEMP:
708 reg_maps->temporary |= 1u << reg->idx[0].offset;
709 break;
711 case WINED3DSPR_INPUT:
712 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
714 /* If relative addressing is used, we must assume that all
715 * registers are used. Even if it is a construct like v3[aL],
716 * we can't assume that v0, v1 and v2 aren't read because aL
717 * can be negative. */
718 if (reg->idx[0].rel_addr)
719 shader->u.ps.input_reg_used = ~0u;
720 else
721 shader->u.ps.input_reg_used |= 1u << reg->idx[0].offset;
723 else
724 reg_maps->input_registers |= 1u << reg->idx[0].offset;
725 break;
727 case WINED3DSPR_RASTOUT:
728 if (reg->idx[0].offset == 1)
729 reg_maps->fog = 1;
730 if (reg->idx[0].offset == 2)
731 reg_maps->point_size = 1;
732 break;
734 case WINED3DSPR_MISCTYPE:
735 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
737 if (!reg->idx[0].offset)
738 reg_maps->vpos = 1;
739 else if (reg->idx[0].offset == 1)
740 reg_maps->usesfacing = 1;
742 break;
744 case WINED3DSPR_CONST:
745 if (reg->idx[0].rel_addr)
747 if (reg->idx[0].offset < reg_maps->min_rel_offset)
748 reg_maps->min_rel_offset = reg->idx[0].offset;
749 if (reg->idx[0].offset > reg_maps->max_rel_offset)
750 reg_maps->max_rel_offset = reg->idx[0].offset;
751 reg_maps->usesrelconstF = TRUE;
753 else
755 if (reg->idx[0].offset >= min(shader->limits->constant_float, constf_size))
757 WARN("Shader using float constant %u which is not supported.\n", reg->idx[0].offset);
758 return FALSE;
760 else
762 wined3d_insert_bits(reg_maps->constf, reg->idx[0].offset, 1, 0x1);
765 break;
767 case WINED3DSPR_CONSTINT:
768 if (reg->idx[0].offset >= shader->limits->constant_int)
770 WARN("Shader using integer constant %u which is not supported.\n", reg->idx[0].offset);
771 return FALSE;
773 else
775 reg_maps->integer_constants |= (1u << reg->idx[0].offset);
777 break;
779 case WINED3DSPR_CONSTBOOL:
780 if (reg->idx[0].offset >= shader->limits->constant_bool)
782 WARN("Shader using bool constant %u which is not supported.\n", reg->idx[0].offset);
783 return FALSE;
785 else
787 reg_maps->boolean_constants |= (1u << reg->idx[0].offset);
789 break;
791 case WINED3DSPR_COLOROUT:
792 reg_maps->rt_mask |= (1u << reg->idx[0].offset);
793 break;
795 case WINED3DSPR_OUTCONTROLPOINT:
796 reg_maps->vocp = 1;
797 break;
799 default:
800 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
801 reg->type, reg->idx[0].offset, reg->idx[1].offset);
802 break;
804 return TRUE;
807 static void shader_record_sample(struct wined3d_shader_reg_maps *reg_maps,
808 unsigned int resource_idx, unsigned int sampler_idx, unsigned int bind_idx)
810 struct wined3d_shader_sampler_map_entry *entries, *entry;
811 struct wined3d_shader_sampler_map *map;
812 unsigned int i;
814 map = &reg_maps->sampler_map;
815 entries = map->entries;
816 for (i = 0; i < map->count; ++i)
818 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
819 return;
822 if (!map->size)
824 if (!(entries = wined3d_calloc(4, sizeof(*entries))))
826 ERR("Failed to allocate sampler map entries.\n");
827 return;
829 map->size = 4;
830 map->entries = entries;
832 else if (map->count == map->size)
834 size_t new_size = map->size * 2;
836 if (sizeof(*entries) * new_size <= sizeof(*entries) * map->size
837 || !(entries = HeapReAlloc(GetProcessHeap(), 0, entries, sizeof(*entries) * new_size)))
839 ERR("Failed to resize sampler map entries.\n");
840 return;
842 map->size = new_size;
843 map->entries = entries;
846 entry = &entries[map->count++];
847 entry->resource_idx = resource_idx;
848 entry->sampler_idx = sampler_idx;
849 entry->bind_idx = bind_idx;
852 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
854 switch (instr)
856 case WINED3DSIH_M4x4:
857 case WINED3DSIH_M3x4:
858 return param == 1 ? 3 : 0;
860 case WINED3DSIH_M4x3:
861 case WINED3DSIH_M3x3:
862 return param == 1 ? 2 : 0;
864 case WINED3DSIH_M3x2:
865 return param == 1 ? 1 : 0;
867 default:
868 return 0;
872 static HRESULT shader_reg_maps_add_tgsm(struct wined3d_shader_reg_maps *reg_maps,
873 unsigned int register_idx, unsigned int size, unsigned int stride)
875 struct wined3d_shader_tgsm *tgsm;
877 if (register_idx >= MAX_TGSM_REGISTERS)
879 ERR("Invalid TGSM register index %u.\n", register_idx);
880 return S_OK;
882 if (reg_maps->shader_version.type != WINED3D_SHADER_TYPE_COMPUTE)
884 FIXME("TGSM declarations are allowed only in compute shaders.\n");
885 return S_OK;
888 if (!wined3d_array_reserve((void **)&reg_maps->tgsm, &reg_maps->tgsm_capacity,
889 register_idx + 1, sizeof(*reg_maps->tgsm)))
890 return E_OUTOFMEMORY;
892 reg_maps->tgsm_count = max(register_idx + 1, reg_maps->tgsm_count);
893 tgsm = &reg_maps->tgsm[register_idx];
894 tgsm->size = size;
895 tgsm->stride = stride;
896 return S_OK;
899 static HRESULT shader_record_shader_phase(struct wined3d_shader *shader,
900 struct wined3d_shader_phase **current_phase, const struct wined3d_shader_instruction *ins,
901 const DWORD *current_instruction_ptr, const DWORD *previous_instruction_ptr)
903 struct wined3d_shader_phase *phase;
905 if ((phase = *current_phase))
907 phase->end = previous_instruction_ptr;
908 *current_phase = NULL;
911 if (shader->reg_maps.shader_version.type != WINED3D_SHADER_TYPE_HULL)
913 ERR("Unexpected shader type %#x.\n", shader->reg_maps.shader_version.type);
914 return E_FAIL;
917 switch (ins->handler_idx)
919 case WINED3DSIH_HS_CONTROL_POINT_PHASE:
920 if (shader->u.hs.phases.control_point)
922 FIXME("Multiple control point phases.\n");
923 HeapFree(GetProcessHeap(), 0, shader->u.hs.phases.control_point);
925 if (!(shader->u.hs.phases.control_point = HeapAlloc(GetProcessHeap(),
926 HEAP_ZERO_MEMORY, sizeof(*shader->u.hs.phases.control_point))))
927 return E_OUTOFMEMORY;
928 phase = shader->u.hs.phases.control_point;
929 break;
930 case WINED3DSIH_HS_FORK_PHASE:
931 if (!wined3d_array_reserve((void **)&shader->u.hs.phases.fork,
932 &shader->u.hs.phases.fork_size, shader->u.hs.phases.fork_count + 1,
933 sizeof(*shader->u.hs.phases.fork)))
934 return E_OUTOFMEMORY;
935 phase = &shader->u.hs.phases.fork[shader->u.hs.phases.fork_count++];
936 break;
937 case WINED3DSIH_HS_JOIN_PHASE:
938 if (!wined3d_array_reserve((void **)&shader->u.hs.phases.join,
939 &shader->u.hs.phases.join_size, shader->u.hs.phases.join_count + 1,
940 sizeof(*shader->u.hs.phases.join)))
941 return E_OUTOFMEMORY;
942 phase = &shader->u.hs.phases.join[shader->u.hs.phases.join_count++];
943 break;
944 default:
945 ERR("Unexpected opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
946 return E_FAIL;
949 phase->start = current_instruction_ptr;
950 *current_phase = phase;
952 return WINED3D_OK;
955 static void wined3d_insert_interpolation_mode(DWORD *packed_interpolation_mode,
956 unsigned int register_idx, enum wined3d_shader_interpolation_mode mode)
958 if (mode > WINED3DSIM_LINEAR_NOPERSPECTIVE_SAMPLE)
959 FIXME("Unexpected interpolation mode %#x.\n", mode);
961 wined3d_insert_bits(packed_interpolation_mode,
962 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT, mode);
965 /* Note that this does not count the loop register as an address register. */
966 static HRESULT shader_get_registers_used(struct wined3d_shader *shader, const struct wined3d_shader_frontend *fe,
967 struct wined3d_shader_reg_maps *reg_maps, struct wined3d_shader_signature *input_signature,
968 struct wined3d_shader_signature *output_signature, DWORD constf_size)
970 struct wined3d_shader_signature_element input_signature_elements[max(MAX_ATTRIBS, MAX_REG_INPUT)];
971 struct wined3d_shader_signature_element output_signature_elements[MAX_REG_OUTPUT];
972 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
973 struct wined3d_shader_version shader_version;
974 struct wined3d_shader_phase *phase = NULL;
975 const DWORD *ptr, *prev_ins, *current_ins;
976 void *fe_data = shader->frontend_data;
977 unsigned int i;
978 HRESULT hr;
980 memset(reg_maps, 0, sizeof(*reg_maps));
981 memset(input_signature_elements, 0, sizeof(input_signature_elements));
982 memset(output_signature_elements, 0, sizeof(output_signature_elements));
983 reg_maps->min_rel_offset = ~0U;
984 list_init(&reg_maps->indexable_temps);
986 fe->shader_read_header(fe_data, &ptr, &shader_version);
987 prev_ins = current_ins = ptr;
988 reg_maps->shader_version = shader_version;
990 shader_set_limits(shader);
992 if (!(reg_maps->constf = wined3d_calloc(((min(shader->limits->constant_float, constf_size) + 31) / 32),
993 sizeof(*reg_maps->constf))))
995 ERR("Failed to allocate constant map memory.\n");
996 return E_OUTOFMEMORY;
999 while (!fe->shader_is_end(fe_data, &ptr))
1001 struct wined3d_shader_instruction ins;
1003 current_ins = ptr;
1004 /* Fetch opcode. */
1005 fe->shader_read_instruction(fe_data, &ptr, &ins);
1007 /* Unhandled opcode, and its parameters. */
1008 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1010 WARN("Encountered unrecognised or invalid instruction.\n");
1011 return WINED3DERR_INVALIDCALL;
1014 /* Handle declarations. */
1015 if (ins.handler_idx == WINED3DSIH_DCL
1016 || ins.handler_idx == WINED3DSIH_DCL_UAV_TYPED)
1018 struct wined3d_shader_semantic *semantic = &ins.declaration.semantic;
1019 unsigned int reg_idx = semantic->reg.reg.idx[0].offset;
1021 switch (semantic->reg.reg.type)
1023 /* Mark input registers used. */
1024 case WINED3DSPR_INPUT:
1025 if (reg_idx >= MAX_REG_INPUT)
1027 ERR("Invalid input register index %u.\n", reg_idx);
1028 break;
1030 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL && shader_version.major == 3
1031 && semantic->usage == WINED3D_DECL_USAGE_POSITION && !semantic->usage_idx)
1032 return WINED3DERR_INVALIDCALL;
1033 reg_maps->input_registers |= 1u << reg_idx;
1034 shader_signature_from_semantic(&input_signature_elements[reg_idx], semantic);
1035 break;
1037 /* Vertex shader: mark 3.0 output registers used, save token. */
1038 case WINED3DSPR_OUTPUT:
1039 if (reg_idx >= MAX_REG_OUTPUT)
1041 ERR("Invalid output register index %u.\n", reg_idx);
1042 break;
1044 reg_maps->output_registers |= 1u << reg_idx;
1045 shader_signature_from_semantic(&output_signature_elements[reg_idx], semantic);
1046 if (semantic->usage == WINED3D_DECL_USAGE_FOG)
1047 reg_maps->fog = 1;
1048 if (semantic->usage == WINED3D_DECL_USAGE_PSIZE)
1049 reg_maps->point_size = 1;
1050 break;
1052 case WINED3DSPR_SAMPLER:
1053 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
1054 case WINED3DSPR_RESOURCE:
1055 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
1057 ERR("Invalid resource index %u.\n", reg_idx);
1058 break;
1060 reg_maps->resource_info[reg_idx].type = semantic->resource_type;
1061 reg_maps->resource_info[reg_idx].data_type = semantic->resource_data_type;
1062 break;
1064 case WINED3DSPR_UAV:
1065 if (reg_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
1067 ERR("Invalid UAV resource index %u.\n", reg_idx);
1068 break;
1070 reg_maps->uav_resource_info[reg_idx].type = semantic->resource_type;
1071 reg_maps->uav_resource_info[reg_idx].data_type = semantic->resource_data_type;
1072 if (ins.flags)
1073 FIXME("Ignoring typed UAV flags %#x.\n", ins.flags);
1074 break;
1076 default:
1077 TRACE("Not recording DCL register type %#x.\n", semantic->reg.reg.type);
1078 break;
1081 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
1083 struct wined3d_shader_register *reg = &ins.declaration.src.reg;
1084 if (reg->idx[0].offset >= WINED3D_MAX_CBS)
1085 ERR("Invalid CB index %u.\n", reg->idx[0].offset);
1086 else
1087 reg_maps->cb_sizes[reg->idx[0].offset] = reg->idx[1].offset;
1089 else if (ins.handler_idx == WINED3DSIH_DCL_GLOBAL_FLAGS)
1091 if (ins.flags & WINED3DSGF_FORCE_EARLY_DEPTH_STENCIL)
1093 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1094 shader->u.ps.force_early_depth_stencil = TRUE;
1095 else
1096 FIXME("Invalid instruction %#x for shader type %#x.\n",
1097 ins.handler_idx, shader_version.type);
1099 else
1101 WARN("Ignoring global flags %#x.\n", ins.flags);
1104 else if (ins.handler_idx == WINED3DSIH_DCL_GS_INSTANCES)
1106 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1107 shader->u.gs.instance_count = ins.declaration.count;
1108 else
1109 FIXME("Invalid instruction %#x for shader type %#x.\n",
1110 ins.handler_idx, shader_version.type);
1112 else if (ins.handler_idx == WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT
1113 || ins.handler_idx == WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT)
1115 if (phase)
1116 phase->instance_count = ins.declaration.count;
1117 else
1118 FIXME("Instruction %s outside of shader phase.\n",
1119 debug_d3dshaderinstructionhandler(ins.handler_idx));
1121 else if (ins.handler_idx == WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER)
1123 if (reg_maps->icb)
1124 FIXME("Multiple immediate constant buffers.\n");
1125 reg_maps->icb = ins.declaration.icb;
1127 else if (ins.handler_idx == WINED3DSIH_DCL_INDEXABLE_TEMP)
1129 if (phase)
1131 FIXME("Indexable temporary registers not supported.\n");
1133 else
1135 struct wined3d_shader_indexable_temp *reg;
1137 if (!(reg = HeapAlloc(GetProcessHeap(), 0, sizeof(*reg))))
1138 return E_OUTOFMEMORY;
1140 *reg = ins.declaration.indexable_temp;
1141 list_add_tail(&reg_maps->indexable_temps, &reg->entry);
1144 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE)
1146 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1147 shader->u.gs.input_type = ins.declaration.primitive_type.type;
1148 else
1149 FIXME("Invalid instruction %#x for shader type %#x.\n",
1150 ins.handler_idx, shader_version.type);
1152 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS)
1154 unsigned int reg_idx = ins.declaration.dst.reg.idx[0].offset;
1155 if (reg_idx >= MAX_REG_INPUT)
1157 ERR("Invalid register index %u.\n", reg_idx);
1158 break;
1160 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1161 wined3d_insert_interpolation_mode(shader->u.ps.interpolation_mode, reg_idx, ins.flags);
1162 else
1163 FIXME("Invalid instruction %#x for shader type %#x.\n",
1164 ins.handler_idx, shader_version.type);
1166 else if (ins.handler_idx == WINED3DSIH_DCL_OUTPUT)
1168 if (ins.declaration.dst.reg.type == WINED3DSPR_DEPTHOUT
1169 || ins.declaration.dst.reg.type == WINED3DSPR_DEPTHOUTGE
1170 || ins.declaration.dst.reg.type == WINED3DSPR_DEPTHOUTLE)
1172 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1173 shader->u.ps.depth_output = ins.declaration.dst.reg.type;
1174 else
1175 FIXME("Invalid instruction %#x for shader type %#x.\n",
1176 ins.handler_idx, shader_version.type);
1179 else if (ins.handler_idx == WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT)
1181 if (shader_version.type == WINED3D_SHADER_TYPE_HULL)
1182 shader->u.hs.output_vertex_count = ins.declaration.count;
1183 else
1184 FIXME("Invalid instruction %#x for shader type %#x.\n", ins.handler_idx, shader_version.type);
1186 else if (ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
1188 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1189 shader->u.gs.output_type = ins.declaration.primitive_type.type;
1190 else
1191 FIXME("Invalid instruction %#x for shader type %#x.\n",
1192 ins.handler_idx, shader_version.type);
1194 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_RAW)
1196 unsigned int reg_idx = ins.declaration.dst.reg.idx[0].offset;
1197 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
1199 ERR("Invalid resource index %u.\n", reg_idx);
1200 break;
1202 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_BUFFER;
1203 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1204 reg_maps->resource_info[reg_idx].flags = WINED3D_VIEW_BUFFER_RAW;
1206 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_STRUCTURED)
1208 unsigned int reg_idx = ins.declaration.structured_resource.reg.reg.idx[0].offset;
1209 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
1211 ERR("Invalid resource index %u.\n", reg_idx);
1212 break;
1214 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_BUFFER;
1215 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1216 reg_maps->resource_info[reg_idx].flags = 0;
1217 reg_maps->resource_info[reg_idx].stride = ins.declaration.structured_resource.byte_stride / 4;
1219 else if (ins.handler_idx == WINED3DSIH_DCL_SAMPLER)
1221 if (ins.flags & WINED3DSI_SAMPLER_COMPARISON_MODE)
1222 reg_maps->sampler_comparison_mode |= (1u << ins.declaration.dst.reg.idx[0].offset);
1224 else if (ins.handler_idx == WINED3DSIH_DCL_TEMPS)
1226 if (phase)
1227 phase->temporary_count = ins.declaration.count;
1228 else
1229 reg_maps->temporary_count = ins.declaration.count;
1231 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_DOMAIN)
1233 if (shader_version.type == WINED3D_SHADER_TYPE_DOMAIN)
1234 shader->u.ds.tessellator_domain = ins.declaration.tessellator_domain;
1235 else if (shader_version.type != WINED3D_SHADER_TYPE_HULL)
1236 FIXME("Invalid instruction %#x for shader type %#x.\n", ins.handler_idx, shader_version.type);
1238 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE)
1240 if (shader_version.type == WINED3D_SHADER_TYPE_HULL)
1241 shader->u.hs.tessellator_output_primitive = ins.declaration.tessellator_output_primitive;
1242 else
1243 FIXME("Invalid instruction %#x for shader type %#x.\n", ins.handler_idx, shader_version.type);
1245 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_PARTITIONING)
1247 if (shader_version.type == WINED3D_SHADER_TYPE_HULL)
1248 shader->u.hs.tessellator_partitioning = ins.declaration.tessellator_partitioning;
1249 else
1250 FIXME("Invalid instruction %#x for shader type %#x.\n", ins.handler_idx, shader_version.type);
1252 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_RAW)
1254 if (FAILED(hr = shader_reg_maps_add_tgsm(reg_maps, ins.declaration.tgsm_raw.reg.reg.idx[0].offset,
1255 ins.declaration.tgsm_raw.byte_count / 4, 0)))
1256 return hr;
1258 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_STRUCTURED)
1260 unsigned int stride = ins.declaration.tgsm_structured.byte_stride / 4;
1261 unsigned int size = stride * ins.declaration.tgsm_structured.structure_count;
1262 if (FAILED(hr = shader_reg_maps_add_tgsm(reg_maps,
1263 ins.declaration.tgsm_structured.reg.reg.idx[0].offset, size, stride)))
1264 return hr;
1266 else if (ins.handler_idx == WINED3DSIH_DCL_THREAD_GROUP)
1268 if (shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
1270 shader->u.cs.thread_group_size = ins.declaration.thread_group_size;
1272 else
1274 FIXME("Invalid instruction %#x for shader type %#x.\n",
1275 ins.handler_idx, shader_version.type);
1278 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_RAW)
1280 unsigned int reg_idx = ins.declaration.dst.reg.idx[0].offset;
1281 if (reg_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
1283 ERR("Invalid UAV resource index %u.\n", reg_idx);
1284 break;
1286 if (ins.flags)
1287 FIXME("Ignoring raw UAV flags %#x.\n", ins.flags);
1288 reg_maps->uav_resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_BUFFER;
1289 reg_maps->uav_resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1290 reg_maps->uav_resource_info[reg_idx].flags = WINED3D_VIEW_BUFFER_RAW;
1292 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_STRUCTURED)
1294 unsigned int reg_idx = ins.declaration.structured_resource.reg.reg.idx[0].offset;
1295 if (reg_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
1297 ERR("Invalid UAV resource index %u.\n", reg_idx);
1298 break;
1300 if (ins.flags)
1301 FIXME("Ignoring structured UAV flags %#x.\n", ins.flags);
1302 reg_maps->uav_resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_BUFFER;
1303 reg_maps->uav_resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1304 reg_maps->uav_resource_info[reg_idx].flags = 0;
1305 reg_maps->uav_resource_info[reg_idx].stride = ins.declaration.structured_resource.byte_stride / 4;
1307 else if (ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
1309 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1310 shader->u.gs.vertices_out = ins.declaration.count;
1311 else
1312 FIXME("Invalid instruction %#x for shader type %#x.\n",
1313 ins.handler_idx, shader_version.type);
1315 else if (ins.handler_idx == WINED3DSIH_DEF)
1317 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1318 float *value;
1319 if (!lconst) return E_OUTOFMEMORY;
1321 lconst->idx = ins.dst[0].reg.idx[0].offset;
1322 memcpy(lconst->value, ins.src[0].reg.u.immconst_data, 4 * sizeof(DWORD));
1323 value = (float *)lconst->value;
1325 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
1326 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1328 if (value[0] < -1.0f) value[0] = -1.0f;
1329 else if (value[0] > 1.0f) value[0] = 1.0f;
1330 if (value[1] < -1.0f) value[1] = -1.0f;
1331 else if (value[1] > 1.0f) value[1] = 1.0f;
1332 if (value[2] < -1.0f) value[2] = -1.0f;
1333 else if (value[2] > 1.0f) value[2] = 1.0f;
1334 if (value[3] < -1.0f) value[3] = -1.0f;
1335 else if (value[3] > 1.0f) value[3] = 1.0f;
1338 list_add_head(&shader->constantsF, &lconst->entry);
1340 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
1341 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
1343 shader->lconst_inf_or_nan = TRUE;
1346 else if (ins.handler_idx == WINED3DSIH_DEFI)
1348 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1349 if (!lconst) return E_OUTOFMEMORY;
1351 lconst->idx = ins.dst[0].reg.idx[0].offset;
1352 memcpy(lconst->value, ins.src[0].reg.u.immconst_data, 4 * sizeof(DWORD));
1354 list_add_head(&shader->constantsI, &lconst->entry);
1355 reg_maps->local_int_consts |= (1u << lconst->idx);
1357 else if (ins.handler_idx == WINED3DSIH_DEFB)
1359 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1360 if (!lconst) return E_OUTOFMEMORY;
1362 lconst->idx = ins.dst[0].reg.idx[0].offset;
1363 memcpy(lconst->value, ins.src[0].reg.u.immconst_data, sizeof(DWORD));
1365 list_add_head(&shader->constantsB, &lconst->entry);
1366 reg_maps->local_bool_consts |= (1u << lconst->idx);
1368 /* Handle shader phases. */
1369 else if (ins.handler_idx == WINED3DSIH_HS_CONTROL_POINT_PHASE
1370 || ins.handler_idx == WINED3DSIH_HS_FORK_PHASE
1371 || ins.handler_idx == WINED3DSIH_HS_JOIN_PHASE)
1373 if (FAILED(hr = shader_record_shader_phase(shader, &phase, &ins, current_ins, prev_ins)))
1374 return hr;
1376 /* For subroutine prototypes. */
1377 else if (ins.handler_idx == WINED3DSIH_LABEL)
1379 reg_maps->labels |= 1u << ins.src[0].reg.idx[0].offset;
1381 /* Set texture, address, temporary registers. */
1382 else
1384 BOOL color0_mov = FALSE;
1385 unsigned int i;
1387 /* This will loop over all the registers and try to
1388 * make a bitmask of the ones we're interested in.
1390 * Relative addressing tokens are ignored, but that's
1391 * okay, since we'll catch any address registers when
1392 * they are initialized (required by spec). */
1393 for (i = 0; i < ins.dst_count; ++i)
1395 if (!shader_record_register_usage(shader, reg_maps, &ins.dst[i].reg,
1396 shader_version.type, constf_size))
1397 return WINED3DERR_INVALIDCALL;
1399 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
1401 UINT idx = ins.dst[i].reg.idx[0].offset;
1403 switch (ins.dst[i].reg.type)
1405 case WINED3DSPR_RASTOUT:
1406 if (shader_version.major >= 3)
1407 break;
1408 switch (idx)
1410 case 0: /* oPos */
1411 reg_maps->output_registers |= 1u << 10;
1412 shader_signature_from_usage(&output_signature_elements[10],
1413 WINED3D_DECL_USAGE_POSITION, 0, 10, WINED3DSP_WRITEMASK_ALL);
1414 break;
1416 case 1: /* oFog */
1417 reg_maps->output_registers |= 1u << 11;
1418 shader_signature_from_usage(&output_signature_elements[11],
1419 WINED3D_DECL_USAGE_FOG, 0, 11, WINED3DSP_WRITEMASK_0);
1420 break;
1422 case 2: /* oPts */
1423 reg_maps->output_registers |= 1u << 11;
1424 shader_signature_from_usage(&output_signature_elements[11],
1425 WINED3D_DECL_USAGE_PSIZE, 0, 11, WINED3DSP_WRITEMASK_1);
1426 break;
1428 break;
1430 case WINED3DSPR_ATTROUT:
1431 if (shader_version.major >= 3)
1432 break;
1433 if (idx < 2)
1435 idx += 8;
1436 if (reg_maps->output_registers & (1u << idx))
1438 output_signature_elements[idx].mask |= ins.dst[i].write_mask;
1440 else
1442 reg_maps->output_registers |= 1u << idx;
1443 shader_signature_from_usage(&output_signature_elements[idx],
1444 WINED3D_DECL_USAGE_COLOR, idx - 8, idx, ins.dst[i].write_mask);
1447 break;
1449 case WINED3DSPR_TEXCRDOUT: /* WINED3DSPR_OUTPUT */
1450 if (shader_version.major >= 3)
1452 if (idx >= ARRAY_SIZE(reg_maps->u.output_registers_mask))
1454 WARN("Invalid output register index %u.\n", idx);
1455 break;
1457 reg_maps->u.output_registers_mask[idx] |= ins.dst[i].write_mask;
1458 break;
1460 if (idx >= ARRAY_SIZE(reg_maps->u.texcoord_mask))
1462 WARN("Invalid texcoord index %u.\n", idx);
1463 break;
1465 reg_maps->u.texcoord_mask[idx] |= ins.dst[i].write_mask;
1466 if (reg_maps->output_registers & (1u << idx))
1468 output_signature_elements[idx].mask |= ins.dst[i].write_mask;
1470 else
1472 reg_maps->output_registers |= 1u << idx;
1473 shader_signature_from_usage(&output_signature_elements[idx],
1474 WINED3D_DECL_USAGE_TEXCOORD, idx, idx, ins.dst[i].write_mask);
1476 break;
1478 default:
1479 break;
1483 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1485 if (ins.dst[i].reg.type == WINED3DSPR_COLOROUT && !ins.dst[i].reg.idx[0].offset)
1487 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
1488 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
1489 * the mov and perform the sRGB write correction from the source register.
1491 * However, if the mov is only partial, we can't do this, and if the write
1492 * comes from an instruction other than MOV it is hard to do as well. If
1493 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
1494 shader->u.ps.color0_mov = FALSE;
1495 if (ins.handler_idx == WINED3DSIH_MOV
1496 && ins.dst[i].write_mask == WINED3DSP_WRITEMASK_ALL)
1498 /* Used later when the source register is read. */
1499 color0_mov = TRUE;
1502 /* Also drop the MOV marker if the source register is overwritten prior to the shader
1503 * end
1505 else if (ins.dst[i].reg.type == WINED3DSPR_TEMP
1506 && ins.dst[i].reg.idx[0].offset == shader->u.ps.color0_reg)
1508 shader->u.ps.color0_mov = FALSE;
1512 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
1513 if (shader_version.major == 1
1514 && (ins.handler_idx == WINED3DSIH_TEX
1515 || ins.handler_idx == WINED3DSIH_TEXBEM
1516 || ins.handler_idx == WINED3DSIH_TEXBEML
1517 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
1518 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
1519 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
1520 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
1521 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
1522 || ins.handler_idx == WINED3DSIH_TEXREG2AR
1523 || ins.handler_idx == WINED3DSIH_TEXREG2GB
1524 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
1526 unsigned int reg_idx = ins.dst[i].reg.idx[0].offset;
1528 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
1530 WARN("Invalid 1.x sampler index %u.\n", reg_idx);
1531 continue;
1534 TRACE("Setting fake 2D resource for 1.x pixelshader.\n");
1535 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
1536 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_FLOAT;
1537 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
1539 /* texbem is only valid with < 1.4 pixel shaders */
1540 if (ins.handler_idx == WINED3DSIH_TEXBEM
1541 || ins.handler_idx == WINED3DSIH_TEXBEML)
1543 reg_maps->bumpmat |= 1u << reg_idx;
1544 if (ins.handler_idx == WINED3DSIH_TEXBEML)
1546 reg_maps->luminanceparams |= 1u << reg_idx;
1550 else if (ins.handler_idx == WINED3DSIH_BEM)
1552 reg_maps->bumpmat |= 1u << ins.dst[i].reg.idx[0].offset;
1556 if (ins.handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC || ins.handler_idx == WINED3DSIH_IMM_ATOMIC_CONSUME)
1558 unsigned int reg_idx = ins.src[0].reg.idx[0].offset;
1559 if (reg_idx >= MAX_UNORDERED_ACCESS_VIEWS)
1561 ERR("Invalid UAV index %u.\n", reg_idx);
1562 break;
1564 reg_maps->uav_counter_mask |= (1u << reg_idx);
1566 else if ((WINED3DSIH_ATOMIC_AND <= ins.handler_idx && ins.handler_idx <= WINED3DSIH_ATOMIC_XOR)
1567 || (WINED3DSIH_IMM_ATOMIC_AND <= ins.handler_idx && ins.handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR)
1568 || (ins.handler_idx == WINED3DSIH_BUFINFO && ins.src[0].reg.type == WINED3DSPR_UAV)
1569 || ins.handler_idx == WINED3DSIH_LD_UAV_TYPED
1570 || (ins.handler_idx == WINED3DSIH_LD_RAW && ins.src[1].reg.type == WINED3DSPR_UAV)
1571 || (ins.handler_idx == WINED3DSIH_LD_STRUCTURED && ins.src[2].reg.type == WINED3DSPR_UAV))
1573 unsigned int reg_idx;
1574 if (ins.handler_idx == WINED3DSIH_LD_UAV_TYPED || ins.handler_idx == WINED3DSIH_LD_RAW)
1575 reg_idx = ins.src[1].reg.idx[0].offset;
1576 else if (ins.handler_idx == WINED3DSIH_LD_STRUCTURED)
1577 reg_idx = ins.src[2].reg.idx[0].offset;
1578 else if (WINED3DSIH_ATOMIC_AND <= ins.handler_idx && ins.handler_idx <= WINED3DSIH_ATOMIC_XOR)
1579 reg_idx = ins.dst[0].reg.idx[0].offset;
1580 else if (ins.handler_idx == WINED3DSIH_BUFINFO)
1581 reg_idx = ins.src[0].reg.idx[0].offset;
1582 else
1583 reg_idx = ins.dst[1].reg.idx[0].offset;
1584 if (reg_idx >= MAX_UNORDERED_ACCESS_VIEWS)
1586 ERR("Invalid UAV index %u.\n", reg_idx);
1587 break;
1589 reg_maps->uav_read_mask |= (1u << reg_idx);
1591 else if (ins.handler_idx == WINED3DSIH_NRM)
1593 reg_maps->usesnrm = 1;
1595 else if (ins.handler_idx == WINED3DSIH_DSY
1596 || ins.handler_idx == WINED3DSIH_DSY_COARSE
1597 || ins.handler_idx == WINED3DSIH_DSY_FINE)
1599 reg_maps->usesdsy = 1;
1601 else if (ins.handler_idx == WINED3DSIH_DSX
1602 || ins.handler_idx == WINED3DSIH_DSX_COARSE
1603 || ins.handler_idx == WINED3DSIH_DSX_FINE)
1605 reg_maps->usesdsx = 1;
1607 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
1608 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
1609 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
1610 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
1611 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
1612 else if (ins.handler_idx == WINED3DSIH_POW) reg_maps->usespow = 1;
1613 else if (ins.handler_idx == WINED3DSIH_LOOP
1614 || ins.handler_idx == WINED3DSIH_REP)
1616 ++cur_loop_depth;
1617 if (cur_loop_depth > max_loop_depth)
1618 max_loop_depth = cur_loop_depth;
1620 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
1621 || ins.handler_idx == WINED3DSIH_ENDREP)
1623 --cur_loop_depth;
1625 else if (ins.handler_idx == WINED3DSIH_GATHER4
1626 || ins.handler_idx == WINED3DSIH_GATHER4_C
1627 || ins.handler_idx == WINED3DSIH_SAMPLE
1628 || ins.handler_idx == WINED3DSIH_SAMPLE_B
1629 || ins.handler_idx == WINED3DSIH_SAMPLE_C
1630 || ins.handler_idx == WINED3DSIH_SAMPLE_C_LZ
1631 || ins.handler_idx == WINED3DSIH_SAMPLE_GRAD
1632 || ins.handler_idx == WINED3DSIH_SAMPLE_LOD)
1634 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1635 ins.src[2].reg.idx[0].offset, reg_maps->sampler_map.count);
1637 else if (ins.handler_idx == WINED3DSIH_GATHER4_PO
1638 || ins.handler_idx == WINED3DSIH_GATHER4_PO_C)
1640 shader_record_sample(reg_maps, ins.src[2].reg.idx[0].offset,
1641 ins.src[3].reg.idx[0].offset, reg_maps->sampler_map.count);
1643 else if (ins.handler_idx == WINED3DSIH_BUFINFO && ins.src[0].reg.type == WINED3DSPR_RESOURCE)
1645 shader_record_sample(reg_maps, ins.src[0].reg.idx[0].offset,
1646 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1648 else if (ins.handler_idx == WINED3DSIH_LD
1649 || (ins.handler_idx == WINED3DSIH_LD_RAW && ins.src[1].reg.type == WINED3DSPR_RESOURCE)
1650 || (ins.handler_idx == WINED3DSIH_RESINFO && ins.src[1].reg.type == WINED3DSPR_RESOURCE))
1652 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1653 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1655 else if (ins.handler_idx == WINED3DSIH_LD_STRUCTURED
1656 && ins.src[2].reg.type == WINED3DSPR_RESOURCE)
1658 shader_record_sample(reg_maps, ins.src[2].reg.idx[0].offset,
1659 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1662 if (ins.predicate)
1663 if (!shader_record_register_usage(shader, reg_maps, &ins.predicate->reg,
1664 shader_version.type, constf_size))
1665 return WINED3DERR_INVALIDCALL;
1667 for (i = 0; i < ins.src_count; ++i)
1669 unsigned int count = get_instr_extra_regcount(ins.handler_idx, i);
1670 struct wined3d_shader_register reg = ins.src[i].reg;
1672 if (!shader_record_register_usage(shader, reg_maps, &ins.src[i].reg,
1673 shader_version.type, constf_size))
1674 return WINED3DERR_INVALIDCALL;
1675 while (count)
1677 ++reg.idx[0].offset;
1678 if (!shader_record_register_usage(shader, reg_maps, &reg,
1679 shader_version.type, constf_size))
1680 return WINED3DERR_INVALIDCALL;
1681 --count;
1684 if (color0_mov)
1686 if (ins.src[i].reg.type == WINED3DSPR_TEMP
1687 && ins.src[i].swizzle == WINED3DSP_NOSWIZZLE)
1689 shader->u.ps.color0_mov = TRUE;
1690 shader->u.ps.color0_reg = ins.src[i].reg.idx[0].offset;
1696 prev_ins = current_ins;
1698 reg_maps->loop_depth = max_loop_depth;
1700 if (phase)
1702 phase->end = prev_ins;
1703 phase = NULL;
1706 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1707 * R0 is written to the render target. */
1708 if (shader_version.major < 2 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1709 reg_maps->rt_mask |= (1u << 0);
1711 if (input_signature->elements)
1713 for (i = 0; i < input_signature->element_count; ++i)
1715 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
1717 if (input_signature->elements[i].register_idx >= ARRAY_SIZE(shader->u.vs.attributes))
1719 WARN("Invalid input signature register index %u.\n", input_signature->elements[i].register_idx);
1720 return WINED3DERR_INVALIDCALL;
1723 else if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1725 if (input_signature->elements[i].sysval_semantic == WINED3D_SV_POSITION)
1726 reg_maps->vpos = 1;
1727 else if (input_signature->elements[i].sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
1728 reg_maps->usesfacing = 1;
1730 reg_maps->input_registers |= 1u << input_signature->elements[i].register_idx;
1733 else if (!input_signature->elements && reg_maps->input_registers)
1735 unsigned int count = wined3d_popcount(reg_maps->input_registers);
1736 struct wined3d_shader_signature_element *e;
1737 unsigned int i;
1739 if (!(input_signature->elements = wined3d_calloc(count, sizeof(*input_signature->elements))))
1740 return E_OUTOFMEMORY;
1741 input_signature->element_count = count;
1743 e = input_signature->elements;
1744 for (i = 0; i < ARRAY_SIZE(input_signature_elements); ++i)
1746 if (!(reg_maps->input_registers & (1u << i)))
1747 continue;
1748 input_signature_elements[i].register_idx = i;
1749 *e++ = input_signature_elements[i];
1753 if (output_signature->elements)
1755 for (i = 0; i < output_signature->element_count; ++i)
1757 reg_maps->output_registers |= 1u << output_signature->elements[i].register_idx;
1760 else if (reg_maps->output_registers)
1762 unsigned int count = wined3d_popcount(reg_maps->output_registers);
1763 struct wined3d_shader_signature_element *e;
1765 if (!(output_signature->elements = wined3d_calloc(count, sizeof(*output_signature->elements))))
1766 return E_OUTOFMEMORY;
1767 output_signature->element_count = count;
1769 e = output_signature->elements;
1770 for (i = 0; i < ARRAY_SIZE(output_signature_elements); ++i)
1772 if (!(reg_maps->output_registers & (1u << i)))
1773 continue;
1774 *e++ = output_signature_elements[i];
1778 return WINED3D_OK;
1781 static void shader_cleanup_reg_maps(struct wined3d_shader_reg_maps *reg_maps)
1783 struct wined3d_shader_indexable_temp *reg, *reg_next;
1785 HeapFree(GetProcessHeap(), 0, reg_maps->constf);
1786 HeapFree(GetProcessHeap(), 0, reg_maps->sampler_map.entries);
1788 LIST_FOR_EACH_ENTRY_SAFE(reg, reg_next, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
1789 HeapFree(GetProcessHeap(), 0, reg);
1790 list_init(&reg_maps->indexable_temps);
1792 HeapFree(GetProcessHeap(), 0, reg_maps->tgsm);
1795 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
1797 DWORD map = 1u << max;
1798 map |= map - 1;
1799 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
1801 return wined3d_log2i(map);
1804 static void shader_dump_global_flags(struct wined3d_string_buffer *buffer, DWORD global_flags)
1806 if (global_flags & WINED3DSGF_REFACTORING_ALLOWED)
1808 shader_addline(buffer, "refactoringAllowed");
1809 global_flags &= ~WINED3DSGF_REFACTORING_ALLOWED;
1810 if (global_flags)
1811 shader_addline(buffer, " | ");
1814 if (global_flags & WINED3DSGF_FORCE_EARLY_DEPTH_STENCIL)
1816 shader_addline(buffer, "forceEarlyDepthStencil");
1817 global_flags &= ~WINED3DSGF_FORCE_EARLY_DEPTH_STENCIL;
1818 if (global_flags)
1819 shader_addline(buffer, " | ");
1822 if (global_flags & WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS)
1824 shader_addline(buffer, "enableRawAndStructuredBuffers");
1825 global_flags &= ~WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS;
1828 if (global_flags)
1829 shader_addline(buffer, "unknown_flags(%#x)", global_flags);
1832 static void shader_dump_sync_flags(struct wined3d_string_buffer *buffer, DWORD sync_flags)
1834 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
1836 shader_addline(buffer, "_g");
1837 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
1839 if (sync_flags & WINED3DSSF_THREAD_GROUP)
1841 shader_addline(buffer, "_t");
1842 sync_flags &= ~WINED3DSSF_THREAD_GROUP;
1845 if (sync_flags)
1846 shader_addline(buffer, "_unknown_flags(%#x)", sync_flags);
1849 static void shader_dump_uav_flags(struct wined3d_string_buffer *buffer, DWORD uav_flags)
1851 if (uav_flags & WINED3DSUF_GLOBALLY_COHERENT)
1853 shader_addline(buffer, "_glc");
1854 uav_flags &= ~WINED3DSUF_GLOBALLY_COHERENT;
1856 if (uav_flags & WINED3DSUF_ORDER_PRESERVING_COUNTER)
1858 shader_addline(buffer, "_opc");
1859 uav_flags &= ~WINED3DSUF_ORDER_PRESERVING_COUNTER;
1862 if (uav_flags)
1863 shader_addline(buffer, "_unknown_flags(%#x)", uav_flags);
1866 static void shader_dump_tessellator_domain(struct wined3d_string_buffer *buffer,
1867 enum wined3d_tessellator_domain domain)
1869 switch (domain)
1871 case WINED3D_TESSELLATOR_DOMAIN_LINE:
1872 shader_addline(buffer, "line");
1873 break;
1874 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
1875 shader_addline(buffer, "triangle");
1876 break;
1877 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
1878 shader_addline(buffer, "quad");
1879 break;
1880 default:
1881 shader_addline(buffer, "unknown_tessellator_domain(%#x)", domain);
1882 break;
1886 static void shader_dump_tessellator_output_primitive(struct wined3d_string_buffer *buffer,
1887 enum wined3d_tessellator_output_primitive output_primitive)
1889 switch (output_primitive)
1891 case WINED3D_TESSELLATOR_OUTPUT_POINT:
1892 shader_addline(buffer, "point");
1893 break;
1894 case WINED3D_TESSELLATOR_OUTPUT_LINE:
1895 shader_addline(buffer, "line");
1896 break;
1897 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
1898 shader_addline(buffer, "triangle_cw");
1899 break;
1900 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
1901 shader_addline(buffer, "triangle_ccw");
1902 break;
1903 default:
1904 shader_addline(buffer, "unknown_tessellator_output_primitive(%#x)", output_primitive);
1905 break;
1909 static void shader_dump_tessellator_partitioning(struct wined3d_string_buffer *buffer,
1910 enum wined3d_tessellator_partitioning partitioning)
1912 switch (partitioning)
1914 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
1915 shader_addline(buffer, "integer");
1916 break;
1917 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
1918 shader_addline(buffer, "pow2");
1919 break;
1920 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
1921 shader_addline(buffer, "fractional_odd");
1922 break;
1923 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
1924 shader_addline(buffer, "fractional_even");
1925 break;
1926 default:
1927 shader_addline(buffer, "unknown_tessellator_partitioning(%#x)", partitioning);
1928 break;
1932 static void shader_dump_shader_input_sysval_semantic(struct wined3d_string_buffer *buffer,
1933 enum wined3d_shader_input_sysval_semantic semantic)
1935 unsigned int i;
1937 for (i = 0; i < ARRAY_SIZE(shader_input_sysval_semantic_names); ++i)
1939 if (shader_input_sysval_semantic_names[i].sysval_semantic == semantic)
1941 shader_addline(buffer, "%s", shader_input_sysval_semantic_names[i].sysval_name);
1942 return;
1946 shader_addline(buffer, "unknown_shader_input_sysval_semantic(%#x)", semantic);
1949 static void shader_dump_decl_usage(struct wined3d_string_buffer *buffer,
1950 const struct wined3d_shader_semantic *semantic, unsigned int flags,
1951 const struct wined3d_shader_version *shader_version)
1953 shader_addline(buffer, "dcl");
1955 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
1957 switch (semantic->resource_type)
1959 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1960 shader_addline(buffer, "_2d");
1961 break;
1963 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1964 shader_addline(buffer, "_3d");
1965 break;
1967 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1968 shader_addline(buffer, "_cube");
1969 break;
1971 default:
1972 shader_addline(buffer, "_unknown_resource_type(%#x)", semantic->resource_type);
1973 break;
1976 else if (semantic->reg.reg.type == WINED3DSPR_RESOURCE || semantic->reg.reg.type == WINED3DSPR_UAV)
1978 if (semantic->reg.reg.type == WINED3DSPR_RESOURCE)
1979 shader_addline(buffer, "_resource_");
1980 else
1981 shader_addline(buffer, "_uav_");
1982 switch (semantic->resource_type)
1984 case WINED3D_SHADER_RESOURCE_BUFFER:
1985 shader_addline(buffer, "buffer");
1986 break;
1988 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1989 shader_addline(buffer, "texture1d");
1990 break;
1992 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1993 shader_addline(buffer, "texture2d");
1994 break;
1996 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
1997 shader_addline(buffer, "texture2dms");
1998 break;
2000 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2001 shader_addline(buffer, "texture3d");
2002 break;
2004 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2005 shader_addline(buffer, "texturecube");
2006 break;
2008 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2009 shader_addline(buffer, "texture1darray");
2010 break;
2012 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2013 shader_addline(buffer, "texture2darray");
2014 break;
2016 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2017 shader_addline(buffer, "texture2dmsarray");
2018 break;
2020 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2021 shader_addline(buffer, "texturecubearray");
2022 break;
2024 default:
2025 shader_addline(buffer, "unknown");
2026 break;
2028 if (semantic->reg.reg.type == WINED3DSPR_UAV)
2029 shader_dump_uav_flags(buffer, flags);
2030 switch (semantic->resource_data_type)
2032 case WINED3D_DATA_FLOAT:
2033 shader_addline(buffer, " (float)");
2034 break;
2036 case WINED3D_DATA_INT:
2037 shader_addline(buffer, " (int)");
2038 break;
2040 case WINED3D_DATA_UINT:
2041 shader_addline(buffer, " (uint)");
2042 break;
2044 case WINED3D_DATA_UNORM:
2045 shader_addline(buffer, " (unorm)");
2046 break;
2048 case WINED3D_DATA_SNORM:
2049 shader_addline(buffer, " (snorm)");
2050 break;
2052 default:
2053 shader_addline(buffer, " (unknown)");
2054 break;
2057 else
2059 /* Pixel shaders 3.0 don't have usage semantics. */
2060 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL)
2061 return;
2062 else
2063 shader_addline(buffer, "_");
2065 switch (semantic->usage)
2067 case WINED3D_DECL_USAGE_POSITION:
2068 shader_addline(buffer, "position%u", semantic->usage_idx);
2069 break;
2071 case WINED3D_DECL_USAGE_BLEND_INDICES:
2072 shader_addline(buffer, "blend");
2073 break;
2075 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
2076 shader_addline(buffer, "weight");
2077 break;
2079 case WINED3D_DECL_USAGE_NORMAL:
2080 shader_addline(buffer, "normal%u", semantic->usage_idx);
2081 break;
2083 case WINED3D_DECL_USAGE_PSIZE:
2084 shader_addline(buffer, "psize");
2085 break;
2087 case WINED3D_DECL_USAGE_COLOR:
2088 if (!semantic->usage_idx)
2089 shader_addline(buffer, "color");
2090 else
2091 shader_addline(buffer, "specular%u", (semantic->usage_idx - 1));
2092 break;
2094 case WINED3D_DECL_USAGE_TEXCOORD:
2095 shader_addline(buffer, "texture%u", semantic->usage_idx);
2096 break;
2098 case WINED3D_DECL_USAGE_TANGENT:
2099 shader_addline(buffer, "tangent");
2100 break;
2102 case WINED3D_DECL_USAGE_BINORMAL:
2103 shader_addline(buffer, "binormal");
2104 break;
2106 case WINED3D_DECL_USAGE_TESS_FACTOR:
2107 shader_addline(buffer, "tessfactor");
2108 break;
2110 case WINED3D_DECL_USAGE_POSITIONT:
2111 shader_addline(buffer, "positionT%u", semantic->usage_idx);
2112 break;
2114 case WINED3D_DECL_USAGE_FOG:
2115 shader_addline(buffer, "fog");
2116 break;
2118 case WINED3D_DECL_USAGE_DEPTH:
2119 shader_addline(buffer, "depth");
2120 break;
2122 case WINED3D_DECL_USAGE_SAMPLE:
2123 shader_addline(buffer, "sample");
2124 break;
2126 default:
2127 shader_addline(buffer, "<unknown_semantic(%#x)>", semantic->usage);
2128 FIXME("Unrecognised semantic usage %#x.\n", semantic->usage);
2133 static void shader_dump_register(struct wined3d_string_buffer *buffer,
2134 const struct wined3d_shader_register *reg, const struct wined3d_shader_version *shader_version)
2136 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
2137 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
2138 UINT offset = reg->idx[0].offset;
2140 switch (reg->type)
2142 case WINED3DSPR_TEMP:
2143 shader_addline(buffer, "r");
2144 break;
2146 case WINED3DSPR_INPUT:
2147 shader_addline(buffer, "v");
2148 break;
2150 case WINED3DSPR_CONST:
2151 case WINED3DSPR_CONST2:
2152 case WINED3DSPR_CONST3:
2153 case WINED3DSPR_CONST4:
2154 shader_addline(buffer, "c");
2155 offset = shader_get_float_offset(reg->type, offset);
2156 break;
2158 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
2159 shader_addline(buffer, "%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
2160 break;
2162 case WINED3DSPR_RASTOUT:
2163 shader_addline(buffer, "%s", rastout_reg_names[offset]);
2164 break;
2166 case WINED3DSPR_COLOROUT:
2167 shader_addline(buffer, "oC");
2168 break;
2170 case WINED3DSPR_DEPTHOUT:
2171 shader_addline(buffer, "oDepth");
2172 break;
2174 case WINED3DSPR_DEPTHOUTGE:
2175 shader_addline(buffer, "oDepthGE");
2176 break;
2178 case WINED3DSPR_DEPTHOUTLE:
2179 shader_addline(buffer, "oDepthLE");
2180 break;
2182 case WINED3DSPR_ATTROUT:
2183 shader_addline(buffer, "oD");
2184 break;
2186 case WINED3DSPR_TEXCRDOUT:
2187 /* Vertex shaders >= 3.0 use general purpose output registers
2188 * (WINED3DSPR_OUTPUT), which can include an address token. */
2189 if (shader_version->major >= 3)
2190 shader_addline(buffer, "o");
2191 else
2192 shader_addline(buffer, "oT");
2193 break;
2195 case WINED3DSPR_CONSTINT:
2196 shader_addline(buffer, "i");
2197 break;
2199 case WINED3DSPR_CONSTBOOL:
2200 shader_addline(buffer, "b");
2201 break;
2203 case WINED3DSPR_LABEL:
2204 shader_addline(buffer, "l");
2205 break;
2207 case WINED3DSPR_LOOP:
2208 shader_addline(buffer, "aL");
2209 break;
2211 case WINED3DSPR_SAMPLER:
2212 shader_addline(buffer, "s");
2213 break;
2215 case WINED3DSPR_MISCTYPE:
2216 if (offset > 1)
2218 FIXME("Unhandled misctype register %u.\n", offset);
2219 shader_addline(buffer, "<unhandled misctype %#x>", offset);
2221 else
2223 shader_addline(buffer, "%s", misctype_reg_names[offset]);
2225 break;
2227 case WINED3DSPR_PREDICATE:
2228 shader_addline(buffer, "p");
2229 break;
2231 case WINED3DSPR_IMMCONST:
2232 shader_addline(buffer, "l");
2233 break;
2235 case WINED3DSPR_CONSTBUFFER:
2236 shader_addline(buffer, "cb");
2237 break;
2239 case WINED3DSPR_IMMCONSTBUFFER:
2240 shader_addline(buffer, "icb");
2241 break;
2243 case WINED3DSPR_PRIMID:
2244 shader_addline(buffer, "primID");
2245 break;
2247 case WINED3DSPR_NULL:
2248 shader_addline(buffer, "null");
2249 break;
2251 case WINED3DSPR_RESOURCE:
2252 shader_addline(buffer, "t");
2253 break;
2255 case WINED3DSPR_UAV:
2256 shader_addline(buffer, "u");
2257 break;
2259 case WINED3DSPR_OUTPOINTID:
2260 shader_addline(buffer, "vOutputControlPointID");
2261 break;
2263 case WINED3DSPR_FORKINSTID:
2264 shader_addline(buffer, "vForkInstanceId");
2265 break;
2267 case WINED3DSPR_JOININSTID:
2268 shader_addline(buffer, "vJoinInstanceId");
2269 break;
2271 case WINED3DSPR_INCONTROLPOINT:
2272 shader_addline(buffer, "vicp");
2273 break;
2275 case WINED3DSPR_OUTCONTROLPOINT:
2276 shader_addline(buffer, "vocp");
2277 break;
2279 case WINED3DSPR_PATCHCONST:
2280 shader_addline(buffer, "vpc");
2281 break;
2283 case WINED3DSPR_TESSCOORD:
2284 shader_addline(buffer, "vDomainLocation");
2285 break;
2287 case WINED3DSPR_GROUPSHAREDMEM:
2288 shader_addline(buffer, "g");
2289 break;
2291 case WINED3DSPR_THREADID:
2292 shader_addline(buffer, "vThreadID");
2293 break;
2295 case WINED3DSPR_THREADGROUPID:
2296 shader_addline(buffer, "vThreadGroupID");
2297 break;
2299 case WINED3DSPR_LOCALTHREADID:
2300 shader_addline(buffer, "vThreadIDInGroup");
2301 break;
2303 case WINED3DSPR_LOCALTHREADINDEX:
2304 shader_addline(buffer, "vThreadIDInGroupFlattened");
2305 break;
2307 case WINED3DSPR_IDXTEMP:
2308 shader_addline(buffer, "x");
2309 break;
2311 case WINED3DSPR_STREAM:
2312 shader_addline(buffer, "m");
2313 break;
2315 case WINED3DSPR_FUNCTIONBODY:
2316 shader_addline(buffer, "fb");
2317 break;
2319 case WINED3DSPR_FUNCTIONPOINTER:
2320 shader_addline(buffer, "fp");
2321 break;
2323 case WINED3DSPR_COVERAGE:
2324 shader_addline(buffer, "vCoverage");
2325 break;
2327 case WINED3DSPR_SAMPLEMASK:
2328 shader_addline(buffer, "oMask");
2329 break;
2331 case WINED3DSPR_GSINSTID:
2332 shader_addline(buffer, "vGSInstanceID");
2333 break;
2335 default:
2336 shader_addline(buffer, "<unhandled_rtype(%#x)>", reg->type);
2337 break;
2340 if (reg->type == WINED3DSPR_IMMCONST)
2342 shader_addline(buffer, "(");
2343 switch (reg->immconst_type)
2345 case WINED3D_IMMCONST_SCALAR:
2346 switch (reg->data_type)
2348 case WINED3D_DATA_FLOAT:
2349 shader_addline(buffer, "%.8e", *(const float *)reg->u.immconst_data);
2350 break;
2351 case WINED3D_DATA_INT:
2352 shader_addline(buffer, "%d", reg->u.immconst_data[0]);
2353 break;
2354 case WINED3D_DATA_RESOURCE:
2355 case WINED3D_DATA_SAMPLER:
2356 case WINED3D_DATA_UINT:
2357 shader_addline(buffer, "%u", reg->u.immconst_data[0]);
2358 break;
2359 default:
2360 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
2361 break;
2363 break;
2365 case WINED3D_IMMCONST_VEC4:
2366 switch (reg->data_type)
2368 case WINED3D_DATA_FLOAT:
2369 shader_addline(buffer, "%.8e, %.8e, %.8e, %.8e",
2370 *(const float *)&reg->u.immconst_data[0], *(const float *)&reg->u.immconst_data[1],
2371 *(const float *)&reg->u.immconst_data[2], *(const float *)&reg->u.immconst_data[3]);
2372 break;
2373 case WINED3D_DATA_INT:
2374 shader_addline(buffer, "%d, %d, %d, %d",
2375 reg->u.immconst_data[0], reg->u.immconst_data[1],
2376 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2377 break;
2378 case WINED3D_DATA_RESOURCE:
2379 case WINED3D_DATA_SAMPLER:
2380 case WINED3D_DATA_UINT:
2381 shader_addline(buffer, "%u, %u, %u, %u",
2382 reg->u.immconst_data[0], reg->u.immconst_data[1],
2383 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2384 break;
2385 default:
2386 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
2387 break;
2389 break;
2391 default:
2392 shader_addline(buffer, "<unhandled immconst_type %#x>", reg->immconst_type);
2393 break;
2395 shader_addline(buffer, ")");
2397 else if (reg->type != WINED3DSPR_RASTOUT
2398 && reg->type != WINED3DSPR_MISCTYPE
2399 && reg->type != WINED3DSPR_NULL)
2401 if (offset != ~0u)
2403 shader_addline(buffer, "[");
2404 if (reg->idx[0].rel_addr)
2406 shader_dump_src_param(buffer, reg->idx[0].rel_addr, shader_version);
2407 shader_addline(buffer, " + ");
2409 shader_addline(buffer, "%u]", offset);
2411 if (reg->idx[1].offset != ~0u)
2413 shader_addline(buffer, "[");
2414 if (reg->idx[1].rel_addr)
2416 shader_dump_src_param(buffer, reg->idx[1].rel_addr, shader_version);
2417 shader_addline(buffer, " + ");
2419 shader_addline(buffer, "%u]", reg->idx[1].offset);
2423 if (reg->type == WINED3DSPR_FUNCTIONPOINTER)
2424 shader_addline(buffer, "[%u]", reg->u.fp_body_idx);
2428 static void shader_dump_dst_param(struct wined3d_string_buffer *buffer,
2429 const struct wined3d_shader_dst_param *param, const struct wined3d_shader_version *shader_version)
2431 DWORD write_mask = param->write_mask;
2433 shader_dump_register(buffer, &param->reg, shader_version);
2435 if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
2437 static const char write_mask_chars[] = "xyzw";
2439 shader_addline(buffer, ".");
2440 if (write_mask & WINED3DSP_WRITEMASK_0)
2441 shader_addline(buffer, "%c", write_mask_chars[0]);
2442 if (write_mask & WINED3DSP_WRITEMASK_1)
2443 shader_addline(buffer, "%c", write_mask_chars[1]);
2444 if (write_mask & WINED3DSP_WRITEMASK_2)
2445 shader_addline(buffer, "%c", write_mask_chars[2]);
2446 if (write_mask & WINED3DSP_WRITEMASK_3)
2447 shader_addline(buffer, "%c", write_mask_chars[3]);
2451 static void shader_dump_src_param(struct wined3d_string_buffer *buffer,
2452 const struct wined3d_shader_src_param *param, const struct wined3d_shader_version *shader_version)
2454 enum wined3d_shader_src_modifier src_modifier = param->modifiers;
2455 DWORD swizzle = param->swizzle;
2457 if (src_modifier == WINED3DSPSM_NEG
2458 || src_modifier == WINED3DSPSM_BIASNEG
2459 || src_modifier == WINED3DSPSM_SIGNNEG
2460 || src_modifier == WINED3DSPSM_X2NEG
2461 || src_modifier == WINED3DSPSM_ABSNEG)
2462 shader_addline(buffer, "-");
2463 else if (src_modifier == WINED3DSPSM_COMP)
2464 shader_addline(buffer, "1-");
2465 else if (src_modifier == WINED3DSPSM_NOT)
2466 shader_addline(buffer, "!");
2468 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
2469 shader_addline(buffer, "abs(");
2471 shader_dump_register(buffer, &param->reg, shader_version);
2473 switch (src_modifier)
2475 case WINED3DSPSM_NONE: break;
2476 case WINED3DSPSM_NEG: break;
2477 case WINED3DSPSM_NOT: break;
2478 case WINED3DSPSM_BIAS: shader_addline(buffer, "_bias"); break;
2479 case WINED3DSPSM_BIASNEG: shader_addline(buffer, "_bias"); break;
2480 case WINED3DSPSM_SIGN: shader_addline(buffer, "_bx2"); break;
2481 case WINED3DSPSM_SIGNNEG: shader_addline(buffer, "_bx2"); break;
2482 case WINED3DSPSM_COMP: break;
2483 case WINED3DSPSM_X2: shader_addline(buffer, "_x2"); break;
2484 case WINED3DSPSM_X2NEG: shader_addline(buffer, "_x2"); break;
2485 case WINED3DSPSM_DZ: shader_addline(buffer, "_dz"); break;
2486 case WINED3DSPSM_DW: shader_addline(buffer, "_dw"); break;
2487 case WINED3DSPSM_ABSNEG: shader_addline(buffer, ")"); break;
2488 case WINED3DSPSM_ABS: shader_addline(buffer, ")"); break;
2489 default: shader_addline(buffer, "_unknown_modifier(%#x)", src_modifier);
2492 if (swizzle != WINED3DSP_NOSWIZZLE)
2494 static const char swizzle_chars[] = "xyzw";
2495 DWORD swizzle_x = swizzle & 0x03;
2496 DWORD swizzle_y = (swizzle >> 2) & 0x03;
2497 DWORD swizzle_z = (swizzle >> 4) & 0x03;
2498 DWORD swizzle_w = (swizzle >> 6) & 0x03;
2500 if (swizzle_x == swizzle_y
2501 && swizzle_x == swizzle_z
2502 && swizzle_x == swizzle_w)
2504 shader_addline(buffer, ".%c", swizzle_chars[swizzle_x]);
2506 else
2508 shader_addline(buffer, ".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
2509 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
2514 /* Shared code in order to generate the bulk of the shader string. */
2515 HRESULT shader_generate_code(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
2516 const struct wined3d_shader_reg_maps *reg_maps, void *backend_ctx,
2517 const DWORD *start, const DWORD *end)
2519 struct wined3d_device *device = shader->device;
2520 const struct wined3d_shader_frontend *fe = shader->frontend;
2521 void *fe_data = shader->frontend_data;
2522 struct wined3d_shader_version shader_version;
2523 struct wined3d_shader_parser_state state;
2524 struct wined3d_shader_instruction ins;
2525 struct wined3d_shader_tex_mx tex_mx;
2526 struct wined3d_shader_context ctx;
2527 const DWORD *ptr;
2529 /* Initialize current parsing state. */
2530 tex_mx.current_row = 0;
2531 state.current_loop_depth = 0;
2532 state.current_loop_reg = 0;
2533 state.in_subroutine = FALSE;
2535 ctx.shader = shader;
2536 ctx.gl_info = &device->adapter->gl_info;
2537 ctx.reg_maps = reg_maps;
2538 ctx.buffer = buffer;
2539 ctx.tex_mx = &tex_mx;
2540 ctx.state = &state;
2541 ctx.backend_data = backend_ctx;
2542 ins.ctx = &ctx;
2544 fe->shader_read_header(fe_data, &ptr, &shader_version);
2545 if (start)
2546 ptr = start;
2548 while (!fe->shader_is_end(fe_data, &ptr) && ptr != end)
2550 /* Read opcode. */
2551 fe->shader_read_instruction(fe_data, &ptr, &ins);
2553 /* Unknown opcode and its parameters. */
2554 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
2556 WARN("Encountered unrecognised or invalid instruction.\n");
2557 return WINED3DERR_INVALIDCALL;
2560 if (ins.predicate)
2561 FIXME("Predicates not implemented.\n");
2563 /* Call appropriate function for output target */
2564 device->shader_backend->shader_handle_instruction(&ins);
2567 return WINED3D_OK;
2570 static void shader_dump_ins_modifiers(struct wined3d_string_buffer *buffer,
2571 const struct wined3d_shader_dst_param *dst)
2573 DWORD mmask = dst->modifiers;
2575 switch (dst->shift)
2577 case 0: break;
2578 case 13: shader_addline(buffer, "_d8"); break;
2579 case 14: shader_addline(buffer, "_d4"); break;
2580 case 15: shader_addline(buffer, "_d2"); break;
2581 case 1: shader_addline(buffer, "_x2"); break;
2582 case 2: shader_addline(buffer, "_x4"); break;
2583 case 3: shader_addline(buffer, "_x8"); break;
2584 default: shader_addline(buffer, "_unhandled_shift(%d)", dst->shift); break;
2587 if (mmask & WINED3DSPDM_SATURATE) shader_addline(buffer, "_sat");
2588 if (mmask & WINED3DSPDM_PARTIALPRECISION) shader_addline(buffer, "_pp");
2589 if (mmask & WINED3DSPDM_MSAMPCENTROID) shader_addline(buffer, "_centroid");
2591 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
2592 if (mmask) FIXME("Unrecognised modifier %#x.\n", mmask);
2595 static void shader_dump_primitive_type(struct wined3d_string_buffer *buffer,
2596 const struct wined3d_shader_primitive_type *primitive_type)
2598 switch (primitive_type->type)
2600 case WINED3D_PT_UNDEFINED:
2601 shader_addline(buffer, "undefined");
2602 break;
2603 case WINED3D_PT_POINTLIST:
2604 shader_addline(buffer, "pointlist");
2605 break;
2606 case WINED3D_PT_LINELIST:
2607 shader_addline(buffer, "linelist");
2608 break;
2609 case WINED3D_PT_LINESTRIP:
2610 shader_addline(buffer, "linestrip");
2611 break;
2612 case WINED3D_PT_TRIANGLELIST:
2613 shader_addline(buffer, "trianglelist");
2614 break;
2615 case WINED3D_PT_TRIANGLESTRIP:
2616 shader_addline(buffer, "trianglestrip");
2617 break;
2618 case WINED3D_PT_TRIANGLEFAN:
2619 shader_addline(buffer, "trianglefan");
2620 break;
2621 case WINED3D_PT_LINELIST_ADJ:
2622 shader_addline(buffer, "linelist_adj");
2623 break;
2624 case WINED3D_PT_LINESTRIP_ADJ:
2625 shader_addline(buffer, "linestrip_adj");
2626 break;
2627 case WINED3D_PT_TRIANGLELIST_ADJ:
2628 shader_addline(buffer, "trianglelist_adj");
2629 break;
2630 case WINED3D_PT_TRIANGLESTRIP_ADJ:
2631 shader_addline(buffer, "trianglestrip_adj");
2632 break;
2633 case WINED3D_PT_PATCH:
2634 shader_addline(buffer, "patch%u", primitive_type->patch_vertex_count);
2635 break;
2636 default:
2637 shader_addline(buffer, "<unrecognized_primitive_type %#x>", primitive_type->type);
2638 break;
2642 static void shader_dump_interpolation_mode(struct wined3d_string_buffer *buffer,
2643 enum wined3d_shader_interpolation_mode interpolation_mode)
2645 switch (interpolation_mode)
2647 case WINED3DSIM_CONSTANT:
2648 shader_addline(buffer, "constant");
2649 break;
2650 case WINED3DSIM_LINEAR:
2651 shader_addline(buffer, "linear");
2652 break;
2653 case WINED3DSIM_LINEAR_CENTROID:
2654 shader_addline(buffer, "linear centroid");
2655 break;
2656 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2657 shader_addline(buffer, "linear noperspective");
2658 break;
2659 case WINED3DSIM_LINEAR_SAMPLE:
2660 shader_addline(buffer, "linear sample");
2661 break;
2662 case WINED3DSIM_LINEAR_NOPERSPECTIVE_CENTROID:
2663 shader_addline(buffer, "linear noperspective centroid");
2664 break;
2665 case WINED3DSIM_LINEAR_NOPERSPECTIVE_SAMPLE:
2666 shader_addline(buffer, "linear noperspective sample");
2667 break;
2668 default:
2669 shader_addline(buffer, "<unrecognized_interpolation_mode %#x>", interpolation_mode);
2670 break;
2674 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data)
2676 struct wined3d_shader_version shader_version;
2677 struct wined3d_string_buffer buffer;
2678 const char *type_prefix;
2679 const char *p, *q;
2680 const DWORD *ptr;
2681 DWORD i;
2683 if (!string_buffer_init(&buffer))
2685 ERR("Failed to initialize string buffer.\n");
2686 return;
2689 fe->shader_read_header(fe_data, &ptr, &shader_version);
2691 TRACE("Parsing %p.\n", ptr);
2693 switch (shader_version.type)
2695 case WINED3D_SHADER_TYPE_VERTEX:
2696 type_prefix = "vs";
2697 break;
2699 case WINED3D_SHADER_TYPE_HULL:
2700 type_prefix = "hs";
2701 break;
2703 case WINED3D_SHADER_TYPE_DOMAIN:
2704 type_prefix = "ds";
2705 break;
2707 case WINED3D_SHADER_TYPE_GEOMETRY:
2708 type_prefix = "gs";
2709 break;
2711 case WINED3D_SHADER_TYPE_PIXEL:
2712 type_prefix = "ps";
2713 break;
2715 case WINED3D_SHADER_TYPE_COMPUTE:
2716 type_prefix = "cs";
2717 break;
2719 default:
2720 FIXME("Unhandled shader type %#x.\n", shader_version.type);
2721 type_prefix = "unknown";
2722 break;
2725 shader_addline(&buffer, "%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
2727 while (!fe->shader_is_end(fe_data, &ptr))
2729 struct wined3d_shader_instruction ins;
2731 fe->shader_read_instruction(fe_data, &ptr, &ins);
2732 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
2734 WARN("Skipping unrecognized instruction.\n");
2735 shader_addline(&buffer, "<unrecognized instruction>\n");
2736 continue;
2739 if (ins.handler_idx == WINED3DSIH_DCL || ins.handler_idx == WINED3DSIH_DCL_UAV_TYPED)
2741 shader_dump_decl_usage(&buffer, &ins.declaration.semantic, ins.flags, &shader_version);
2742 shader_dump_ins_modifiers(&buffer, &ins.declaration.semantic.reg);
2743 shader_addline(&buffer, " ");
2744 shader_dump_dst_param(&buffer, &ins.declaration.semantic.reg, &shader_version);
2746 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
2748 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2749 shader_dump_src_param(&buffer, &ins.declaration.src, &shader_version);
2750 shader_addline(&buffer, ", %s",
2751 ins.flags & WINED3DSI_INDEXED_DYNAMIC ? "dynamicIndexed" : "immediateIndexed");
2753 else if (ins.handler_idx == WINED3DSIH_DCL_FUNCTION_BODY)
2755 shader_addline(&buffer, "%s fb%u",
2756 shader_opcode_names[ins.handler_idx], ins.declaration.index);
2758 else if (ins.handler_idx == WINED3DSIH_DCL_FUNCTION_TABLE)
2760 shader_addline(&buffer, "%s ft%u = {...}",
2761 shader_opcode_names[ins.handler_idx], ins.declaration.index);
2763 else if (ins.handler_idx == WINED3DSIH_DCL_GLOBAL_FLAGS)
2765 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2766 shader_dump_global_flags(&buffer, ins.flags);
2768 else if (ins.handler_idx == WINED3DSIH_DCL_HS_MAX_TESSFACTOR)
2770 shader_addline(&buffer, "%s %.8e", shader_opcode_names[ins.handler_idx],
2771 ins.declaration.max_tessellation_factor);
2773 else if (ins.handler_idx == WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER)
2775 shader_addline(&buffer, "%s {\n", shader_opcode_names[ins.handler_idx]);
2776 for (i = 0; i < ins.declaration.icb->vec4_count; ++i)
2778 shader_addline(&buffer, " {0x%08x, 0x%08x, 0x%08x, 0x%08x},\n",
2779 ins.declaration.icb->data[4 * i + 0],
2780 ins.declaration.icb->data[4 * i + 1],
2781 ins.declaration.icb->data[4 * i + 2],
2782 ins.declaration.icb->data[4 * i + 3]);
2784 shader_addline(&buffer, "}");
2786 else if (ins.handler_idx == WINED3DSIH_DCL_INDEX_RANGE)
2788 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2789 shader_dump_dst_param(&buffer, &ins.declaration.index_range.first_register, &shader_version);
2790 shader_addline(&buffer, " %u", ins.declaration.index_range.last_register);
2792 else if (ins.handler_idx == WINED3DSIH_DCL_INDEXABLE_TEMP)
2794 shader_addline(&buffer, "%s x[%u][%u], %u", shader_opcode_names[ins.handler_idx],
2795 ins.declaration.indexable_temp.register_idx,
2796 ins.declaration.indexable_temp.register_size,
2797 ins.declaration.indexable_temp.component_count);
2799 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS)
2801 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2802 shader_dump_interpolation_mode(&buffer, ins.flags);
2803 shader_addline(&buffer, " ");
2804 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2806 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SGV
2807 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SGV
2808 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SIV
2809 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_SIV)
2811 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2812 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2813 shader_addline(&buffer, ", ");
2814 shader_dump_shader_input_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2816 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SIV)
2818 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2819 shader_dump_interpolation_mode(&buffer, ins.flags);
2820 shader_addline(&buffer, " ");
2821 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2822 shader_addline(&buffer, ", ");
2823 shader_dump_shader_input_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2825 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT
2826 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT)
2828 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2829 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2831 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE
2832 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
2834 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2835 shader_dump_primitive_type(&buffer, &ins.declaration.primitive_type);
2837 else if (ins.handler_idx == WINED3DSIH_DCL_INTERFACE)
2839 shader_addline(&buffer, "%s fp[%u][%u][%u] = {...}",
2840 shader_opcode_names[ins.handler_idx], ins.declaration.fp.index,
2841 ins.declaration.fp.array_size, ins.declaration.fp.body_count);
2843 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_RAW)
2845 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2846 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2848 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_STRUCTURED)
2850 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2851 shader_dump_dst_param(&buffer, &ins.declaration.structured_resource.reg, &shader_version);
2852 shader_addline(&buffer, ", %u", ins.declaration.structured_resource.byte_stride);
2854 else if (ins.handler_idx == WINED3DSIH_DCL_SAMPLER)
2856 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2857 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2858 if (ins.flags == WINED3DSI_SAMPLER_COMPARISON_MODE)
2859 shader_addline(&buffer, ", comparisonMode");
2861 else if (ins.handler_idx == WINED3DSIH_DCL_TEMPS
2862 || ins.handler_idx == WINED3DSIH_DCL_GS_INSTANCES
2863 || ins.handler_idx == WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT
2864 || ins.handler_idx == WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT
2865 || ins.handler_idx == WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT
2866 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT
2867 || ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
2869 shader_addline(&buffer, "%s %u", shader_opcode_names[ins.handler_idx], ins.declaration.count);
2871 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_DOMAIN)
2873 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2874 shader_dump_tessellator_domain(&buffer, ins.declaration.tessellator_domain);
2876 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE)
2878 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2879 shader_dump_tessellator_output_primitive(&buffer, ins.declaration.tessellator_output_primitive);
2881 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_PARTITIONING)
2883 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2884 shader_dump_tessellator_partitioning(&buffer, ins.declaration.tessellator_partitioning);
2886 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_RAW)
2888 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2889 shader_dump_dst_param(&buffer, &ins.declaration.tgsm_raw.reg, &shader_version);
2890 shader_addline(&buffer, ", %u", ins.declaration.tgsm_raw.byte_count);
2892 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_STRUCTURED)
2894 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2895 shader_dump_dst_param(&buffer, &ins.declaration.tgsm_structured.reg, &shader_version);
2896 shader_addline(&buffer, ", %u, %u", ins.declaration.tgsm_structured.byte_stride,
2897 ins.declaration.tgsm_structured.structure_count);
2899 else if (ins.handler_idx == WINED3DSIH_DCL_THREAD_GROUP)
2901 shader_addline(&buffer, "%s %u, %u, %u", shader_opcode_names[ins.handler_idx],
2902 ins.declaration.thread_group_size.x,
2903 ins.declaration.thread_group_size.y,
2904 ins.declaration.thread_group_size.z);
2906 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_RAW)
2908 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2909 shader_dump_uav_flags(&buffer, ins.flags);
2910 shader_addline(&buffer, " ");
2911 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2913 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_STRUCTURED)
2915 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2916 shader_dump_uav_flags(&buffer, ins.flags);
2917 shader_addline(&buffer, " ");
2918 shader_dump_dst_param(&buffer, &ins.declaration.structured_resource.reg, &shader_version);
2919 shader_addline(&buffer, ", %u", ins.declaration.structured_resource.byte_stride);
2921 else if (ins.handler_idx == WINED3DSIH_DEF)
2923 shader_addline(&buffer, "def c%u = %.8e, %.8e, %.8e, %.8e", shader_get_float_offset(ins.dst[0].reg.type,
2924 ins.dst[0].reg.idx[0].offset),
2925 *(const float *)&ins.src[0].reg.u.immconst_data[0],
2926 *(const float *)&ins.src[0].reg.u.immconst_data[1],
2927 *(const float *)&ins.src[0].reg.u.immconst_data[2],
2928 *(const float *)&ins.src[0].reg.u.immconst_data[3]);
2930 else if (ins.handler_idx == WINED3DSIH_DEFI)
2932 shader_addline(&buffer, "defi i%u = %d, %d, %d, %d", ins.dst[0].reg.idx[0].offset,
2933 ins.src[0].reg.u.immconst_data[0],
2934 ins.src[0].reg.u.immconst_data[1],
2935 ins.src[0].reg.u.immconst_data[2],
2936 ins.src[0].reg.u.immconst_data[3]);
2938 else if (ins.handler_idx == WINED3DSIH_DEFB)
2940 shader_addline(&buffer, "defb b%u = %s",
2941 ins.dst[0].reg.idx[0].offset, ins.src[0].reg.u.immconst_data[0] ? "true" : "false");
2943 else
2945 if (ins.predicate)
2947 shader_addline(&buffer, "(");
2948 shader_dump_src_param(&buffer, ins.predicate, &shader_version);
2949 shader_addline(&buffer, ") ");
2952 /* PixWin marks instructions with the coissue flag with a '+' */
2953 if (ins.coissue)
2954 shader_addline(&buffer, "+");
2956 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2958 if (ins.handler_idx == WINED3DSIH_BREAKP
2959 || ins.handler_idx == WINED3DSIH_CONTINUEP
2960 || ins.handler_idx == WINED3DSIH_IF
2961 || ins.handler_idx == WINED3DSIH_RETP)
2963 switch (ins.flags)
2965 case WINED3D_SHADER_CONDITIONAL_OP_NZ: shader_addline(&buffer, "_nz"); break;
2966 case WINED3D_SHADER_CONDITIONAL_OP_Z: shader_addline(&buffer, "_z"); break;
2967 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags); break;
2970 else if (ins.handler_idx == WINED3DSIH_IFC
2971 || ins.handler_idx == WINED3DSIH_BREAKC)
2973 switch (ins.flags)
2975 case WINED3D_SHADER_REL_OP_GT: shader_addline(&buffer, "_gt"); break;
2976 case WINED3D_SHADER_REL_OP_EQ: shader_addline(&buffer, "_eq"); break;
2977 case WINED3D_SHADER_REL_OP_GE: shader_addline(&buffer, "_ge"); break;
2978 case WINED3D_SHADER_REL_OP_LT: shader_addline(&buffer, "_lt"); break;
2979 case WINED3D_SHADER_REL_OP_NE: shader_addline(&buffer, "_ne"); break;
2980 case WINED3D_SHADER_REL_OP_LE: shader_addline(&buffer, "_le"); break;
2981 default: shader_addline(&buffer, "_(%u)", ins.flags);
2984 else if (ins.handler_idx == WINED3DSIH_TEX
2985 && shader_version.major >= 2
2986 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
2988 shader_addline(&buffer, "p");
2990 else if (ins.handler_idx == WINED3DSIH_RESINFO && ins.flags)
2992 switch (ins.flags)
2994 case WINED3DSI_RESINFO_RCP_FLOAT: shader_addline(&buffer, "_rcpFloat"); break;
2995 case WINED3DSI_RESINFO_UINT: shader_addline(&buffer, "_uint"); break;
2996 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags);
2999 else if (ins.handler_idx == WINED3DSIH_SAMPLE_INFO && ins.flags)
3001 switch (ins.flags)
3003 case WINED3DSI_SAMPLE_INFO_UINT: shader_addline(&buffer, "_uint"); break;
3004 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags);
3007 else if (ins.handler_idx == WINED3DSIH_SYNC)
3009 shader_dump_sync_flags(&buffer, ins.flags);
3012 if (wined3d_shader_instruction_has_texel_offset(&ins))
3013 shader_addline(&buffer, "(%d,%d,%d)", ins.texel_offset.u, ins.texel_offset.v, ins.texel_offset.w);
3015 for (i = 0; i < ins.dst_count; ++i)
3017 shader_dump_ins_modifiers(&buffer, &ins.dst[i]);
3018 shader_addline(&buffer, !i ? " " : ", ");
3019 shader_dump_dst_param(&buffer, &ins.dst[i], &shader_version);
3022 /* Other source tokens */
3023 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
3025 shader_addline(&buffer, !i ? " " : ", ");
3026 shader_dump_src_param(&buffer, &ins.src[i - ins.dst_count], &shader_version);
3029 shader_addline(&buffer, "\n");
3032 for (p = buffer.buffer; *p; p = q)
3034 if (!(q = strstr(p, "\n")))
3035 q = p + strlen(p);
3036 else
3037 ++q;
3038 TRACE(" %.*s", (int)(q - p), p);
3041 string_buffer_free(&buffer);
3044 static void shader_cleanup(struct wined3d_shader *shader)
3046 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_HULL)
3048 HeapFree(GetProcessHeap(), 0, shader->u.hs.phases.control_point);
3049 HeapFree(GetProcessHeap(), 0, shader->u.hs.phases.fork);
3050 HeapFree(GetProcessHeap(), 0, shader->u.hs.phases.join);
3052 else if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
3054 HeapFree(GetProcessHeap(), 0, shader->u.gs.so_desc.elements);
3057 HeapFree(GetProcessHeap(), 0, shader->patch_constant_signature.elements);
3058 HeapFree(GetProcessHeap(), 0, shader->output_signature.elements);
3059 HeapFree(GetProcessHeap(), 0, shader->input_signature.elements);
3060 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
3061 shader->device->shader_backend->shader_destroy(shader);
3062 shader_cleanup_reg_maps(&shader->reg_maps);
3063 HeapFree(GetProcessHeap(), 0, shader->function);
3064 shader_delete_constant_list(&shader->constantsF);
3065 shader_delete_constant_list(&shader->constantsB);
3066 shader_delete_constant_list(&shader->constantsI);
3067 list_remove(&shader->shader_list_entry);
3069 if (shader->frontend && shader->frontend_data)
3070 shader->frontend->shader_free(shader->frontend_data);
3073 struct shader_none_priv
3075 const struct wined3d_vertex_pipe_ops *vertex_pipe;
3076 const struct fragment_pipeline *fragment_pipe;
3077 BOOL ffp_proj_control;
3080 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
3081 static void shader_none_precompile(void *shader_priv, struct wined3d_shader *shader) {}
3082 static void shader_none_select_compute(void *shader_priv, struct wined3d_context *context,
3083 const struct wined3d_state *state) {}
3084 static void shader_none_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count) {}
3085 static void shader_none_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count) {}
3086 static void shader_none_load_constants(void *shader_priv, struct wined3d_context *context,
3087 const struct wined3d_state *state) {}
3088 static void shader_none_destroy(struct wined3d_shader *shader) {}
3089 static void shader_none_free_context_data(struct wined3d_context *context) {}
3090 static void shader_none_init_context_state(struct wined3d_context *context) {}
3092 /* Context activation is done by the caller. */
3093 static void shader_none_select(void *shader_priv, struct wined3d_context *context,
3094 const struct wined3d_state *state)
3096 const struct wined3d_gl_info *gl_info = context->gl_info;
3097 struct shader_none_priv *priv = shader_priv;
3099 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
3100 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
3103 /* Context activation is done by the caller. */
3104 static void shader_none_disable(void *shader_priv, struct wined3d_context *context)
3106 struct shader_none_priv *priv = shader_priv;
3107 const struct wined3d_gl_info *gl_info = context->gl_info;
3109 priv->vertex_pipe->vp_enable(gl_info, FALSE);
3110 priv->fragment_pipe->enable_extension(gl_info, FALSE);
3112 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
3113 | (1u << WINED3D_SHADER_TYPE_VERTEX)
3114 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
3115 | (1u << WINED3D_SHADER_TYPE_HULL)
3116 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
3117 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
3120 static HRESULT shader_none_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
3121 const struct fragment_pipeline *fragment_pipe)
3123 struct fragment_caps fragment_caps;
3124 void *vertex_priv, *fragment_priv;
3125 struct shader_none_priv *priv;
3127 if (!(priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv))))
3128 return E_OUTOFMEMORY;
3130 if (!(vertex_priv = vertex_pipe->vp_alloc(&none_shader_backend, priv)))
3132 ERR("Failed to initialize vertex pipe.\n");
3133 HeapFree(GetProcessHeap(), 0, priv);
3134 return E_FAIL;
3137 if (!(fragment_priv = fragment_pipe->alloc_private(&none_shader_backend, priv)))
3139 ERR("Failed to initialize fragment pipe.\n");
3140 vertex_pipe->vp_free(device);
3141 HeapFree(GetProcessHeap(), 0, priv);
3142 return E_FAIL;
3145 priv->vertex_pipe = vertex_pipe;
3146 priv->fragment_pipe = fragment_pipe;
3147 fragment_pipe->get_caps(&device->adapter->gl_info, &fragment_caps);
3148 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
3150 device->vertex_priv = vertex_priv;
3151 device->fragment_priv = fragment_priv;
3152 device->shader_priv = priv;
3154 return WINED3D_OK;
3157 static void shader_none_free(struct wined3d_device *device)
3159 struct shader_none_priv *priv = device->shader_priv;
3161 priv->fragment_pipe->free_private(device);
3162 priv->vertex_pipe->vp_free(device);
3163 HeapFree(GetProcessHeap(), 0, priv);
3166 static BOOL shader_none_allocate_context_data(struct wined3d_context *context)
3168 return TRUE;
3171 static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
3173 /* Set the shader caps to 0 for the none shader backend */
3174 caps->vs_version = 0;
3175 caps->hs_version = 0;
3176 caps->ds_version = 0;
3177 caps->gs_version = 0;
3178 caps->ps_version = 0;
3179 caps->cs_version = 0;
3180 caps->vs_uniform_count = 0;
3181 caps->ps_uniform_count = 0;
3182 caps->ps_1x_max_value = 0.0f;
3183 caps->varying_count = 0;
3184 caps->wined3d_caps = 0;
3187 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
3189 /* We "support" every possible fixup, since we don't support any shader
3190 * model, and will never have to actually sample a texture. */
3191 return TRUE;
3194 static BOOL shader_none_has_ffp_proj_control(void *shader_priv)
3196 struct shader_none_priv *priv = shader_priv;
3198 return priv->ffp_proj_control;
3201 const struct wined3d_shader_backend_ops none_shader_backend =
3203 shader_none_handle_instruction,
3204 shader_none_precompile,
3205 shader_none_select,
3206 shader_none_select_compute,
3207 shader_none_disable,
3208 shader_none_update_float_vertex_constants,
3209 shader_none_update_float_pixel_constants,
3210 shader_none_load_constants,
3211 shader_none_destroy,
3212 shader_none_alloc,
3213 shader_none_free,
3214 shader_none_allocate_context_data,
3215 shader_none_free_context_data,
3216 shader_none_init_context_state,
3217 shader_none_get_caps,
3218 shader_none_color_fixup_supported,
3219 shader_none_has_ffp_proj_control,
3222 static HRESULT shader_set_function(struct wined3d_shader *shader, DWORD float_const_count,
3223 enum wined3d_shader_type type, unsigned int max_version)
3225 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3226 const struct wined3d_shader_frontend *fe;
3227 HRESULT hr;
3228 unsigned int backend_version;
3229 const struct wined3d_d3d_info *d3d_info = &shader->device->adapter->d3d_info;
3231 TRACE("shader %p, float_const_count %u, type %#x, max_version %u.\n",
3232 shader, float_const_count, type, max_version);
3234 fe = shader->frontend;
3235 if (!(shader->frontend_data = fe->shader_init(shader->function,
3236 shader->functionLength, &shader->output_signature)))
3238 FIXME("Failed to initialize frontend.\n");
3239 return WINED3DERR_INVALIDCALL;
3242 /* First pass: trace shader. */
3243 if (TRACE_ON(d3d_shader))
3244 shader_trace_init(fe, shader->frontend_data);
3246 /* Second pass: figure out which registers are used, what the semantics are, etc. */
3247 if (FAILED(hr = shader_get_registers_used(shader, fe, reg_maps, &shader->input_signature,
3248 &shader->output_signature, float_const_count)))
3249 return hr;
3251 if (reg_maps->shader_version.type != type)
3253 WARN("Wrong shader type %d.\n", reg_maps->shader_version.type);
3254 return WINED3DERR_INVALIDCALL;
3256 if (reg_maps->shader_version.major > max_version)
3258 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps->shader_version.major);
3259 return WINED3DERR_INVALIDCALL;
3261 switch (type)
3263 case WINED3D_SHADER_TYPE_VERTEX:
3264 backend_version = d3d_info->limits.vs_version;
3265 break;
3266 case WINED3D_SHADER_TYPE_HULL:
3267 backend_version = d3d_info->limits.hs_version;
3268 break;
3269 case WINED3D_SHADER_TYPE_DOMAIN:
3270 backend_version = d3d_info->limits.ds_version;
3271 break;
3272 case WINED3D_SHADER_TYPE_GEOMETRY:
3273 backend_version = d3d_info->limits.gs_version;
3274 break;
3275 case WINED3D_SHADER_TYPE_PIXEL:
3276 backend_version = d3d_info->limits.ps_version;
3277 break;
3278 case WINED3D_SHADER_TYPE_COMPUTE:
3279 backend_version = d3d_info->limits.cs_version;
3280 break;
3281 default:
3282 FIXME("No backend version-checking for this shader type.\n");
3283 backend_version = 0;
3285 if (reg_maps->shader_version.major > backend_version)
3287 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
3288 reg_maps->shader_version.major, reg_maps->shader_version.minor);
3289 return WINED3DERR_INVALIDCALL;
3292 return WINED3D_OK;
3295 ULONG CDECL wined3d_shader_incref(struct wined3d_shader *shader)
3297 ULONG refcount = InterlockedIncrement(&shader->ref);
3299 TRACE("%p increasing refcount to %u.\n", shader, refcount);
3301 return refcount;
3304 static void wined3d_shader_init_object(void *object)
3306 struct wined3d_shader *shader = object;
3307 struct wined3d_device *device = shader->device;
3309 list_add_head(&device->shaders, &shader->shader_list_entry);
3311 device->shader_backend->shader_precompile(device->shader_priv, shader);
3314 static void wined3d_shader_destroy_object(void *object)
3316 shader_cleanup(object);
3317 HeapFree(GetProcessHeap(), 0, object);
3320 ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
3322 ULONG refcount = InterlockedDecrement(&shader->ref);
3324 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
3326 if (!refcount)
3328 shader->parent_ops->wined3d_object_destroyed(shader->parent);
3329 wined3d_cs_destroy_object(shader->device->cs, wined3d_shader_destroy_object, shader);
3332 return refcount;
3335 void * CDECL wined3d_shader_get_parent(const struct wined3d_shader *shader)
3337 TRACE("shader %p.\n", shader);
3339 return shader->parent;
3342 HRESULT CDECL wined3d_shader_get_byte_code(const struct wined3d_shader *shader,
3343 void *byte_code, UINT *byte_code_size)
3345 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader, byte_code, byte_code_size);
3347 if (!byte_code)
3349 *byte_code_size = shader->functionLength;
3350 return WINED3D_OK;
3353 if (*byte_code_size < shader->functionLength)
3355 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
3356 * than the required size we should write the required size and
3357 * return D3DERR_MOREDATA. That's not actually true. */
3358 return WINED3DERR_INVALIDCALL;
3361 memcpy(byte_code, shader->function, shader->functionLength);
3363 return WINED3D_OK;
3366 /* Set local constants for d3d8 shaders. */
3367 HRESULT CDECL wined3d_shader_set_local_constants_float(struct wined3d_shader *shader,
3368 UINT start_idx, const float *src_data, UINT count)
3370 UINT end_idx = start_idx + count;
3371 UINT i;
3373 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader, start_idx, src_data, count);
3375 if (end_idx > shader->limits->constant_float)
3377 WARN("end_idx %u > float constants limit %u.\n",
3378 end_idx, shader->limits->constant_float);
3379 end_idx = shader->limits->constant_float;
3382 for (i = start_idx; i < end_idx; ++i)
3384 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
3385 float *value;
3386 if (!lconst)
3387 return E_OUTOFMEMORY;
3389 lconst->idx = i;
3390 value = (float *)lconst->value;
3391 memcpy(value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
3392 list_add_head(&shader->constantsF, &lconst->entry);
3394 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
3395 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
3397 shader->lconst_inf_or_nan = TRUE;
3401 return WINED3D_OK;
3404 static void init_interpolation_compile_args(DWORD *interpolation_args,
3405 const struct wined3d_shader *pixel_shader, const struct wined3d_gl_info *gl_info)
3407 if (!needs_interpolation_qualifiers_for_shader_outputs(gl_info)
3408 || !pixel_shader || pixel_shader->reg_maps.shader_version.major < 4)
3410 memset(interpolation_args, 0, sizeof(pixel_shader->u.ps.interpolation_mode));
3411 return;
3414 memcpy(interpolation_args, pixel_shader->u.ps.interpolation_mode,
3415 sizeof(pixel_shader->u.ps.interpolation_mode));
3418 void find_vs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3419 WORD swizzle_map, struct vs_compile_args *args, const struct wined3d_context *context)
3421 const struct wined3d_shader *geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
3422 const struct wined3d_shader *pixel_shader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3423 const struct wined3d_shader *hull_shader = state->shader[WINED3D_SHADER_TYPE_HULL];
3424 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3425 const struct wined3d_gl_info *gl_info = context->gl_info;
3427 args->fog_src = state->render_states[WINED3D_RS_FOGTABLEMODE]
3428 == WINED3D_FOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
3429 args->clip_enabled = state->render_states[WINED3D_RS_CLIPPING]
3430 && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
3431 args->point_size = state->gl_primitive_type == GL_POINTS;
3432 args->per_vertex_point_size = shader->reg_maps.point_size;
3433 args->next_shader_type = hull_shader? WINED3D_SHADER_TYPE_HULL
3434 : geometry_shader ? WINED3D_SHADER_TYPE_GEOMETRY : WINED3D_SHADER_TYPE_PIXEL;
3435 if (shader->reg_maps.shader_version.major >= 4)
3436 args->next_shader_input_count = hull_shader ? hull_shader->limits->packed_input
3437 : geometry_shader ? geometry_shader->limits->packed_input
3438 : pixel_shader ? pixel_shader->limits->packed_input : 0;
3439 else
3440 args->next_shader_input_count = 0;
3441 args->swizzle_map = swizzle_map;
3442 if (d3d_info->emulated_flatshading)
3443 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
3444 else
3445 args->flatshading = 0;
3447 init_interpolation_compile_args(args->interpolation_mode,
3448 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL ? pixel_shader : NULL, gl_info);
3451 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
3453 if (usage_idx1 != usage_idx2)
3454 return FALSE;
3455 if (usage1 == usage2)
3456 return TRUE;
3457 if (usage1 == WINED3D_DECL_USAGE_POSITION && usage2 == WINED3D_DECL_USAGE_POSITIONT)
3458 return TRUE;
3459 if (usage2 == WINED3D_DECL_USAGE_POSITION && usage1 == WINED3D_DECL_USAGE_POSITIONT)
3460 return TRUE;
3462 return FALSE;
3465 BOOL vshader_get_input(const struct wined3d_shader *shader,
3466 BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
3468 WORD map = shader->reg_maps.input_registers;
3469 unsigned int i;
3471 for (i = 0; map; map >>= 1, ++i)
3473 if (!(map & 1)) continue;
3475 if (match_usage(shader->u.vs.attributes[i].usage,
3476 shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
3478 *regnum = i;
3479 return TRUE;
3482 return FALSE;
3485 static HRESULT shader_signature_calculate_strings_length(const struct wined3d_shader_signature *signature,
3486 SIZE_T *total)
3488 struct wined3d_shader_signature_element *e;
3489 unsigned int i;
3490 SIZE_T len;
3492 for (i = 0; i < signature->element_count; ++i)
3494 e = &signature->elements[i];
3495 len = strlen(e->semantic_name);
3496 if (len >= ~(SIZE_T)0 - *total)
3497 return E_OUTOFMEMORY;
3499 *total += len + 1;
3501 return WINED3D_OK;
3504 static HRESULT shader_signature_copy(struct wined3d_shader_signature *dst,
3505 const struct wined3d_shader_signature *src, char **signature_strings)
3507 struct wined3d_shader_signature_element *e;
3508 unsigned int i;
3509 SIZE_T len;
3510 char *ptr;
3512 if (!src->element_count)
3513 return WINED3D_OK;
3515 ptr = *signature_strings;
3517 dst->element_count = src->element_count;
3518 if (!(dst->elements = wined3d_calloc(dst->element_count, sizeof(*dst->elements))))
3519 return E_OUTOFMEMORY;
3521 for (i = 0; i < src->element_count; ++i)
3523 e = &src->elements[i];
3524 dst->elements[i] = *e;
3526 len = strlen(e->semantic_name);
3527 memcpy(ptr, e->semantic_name, len + 1);
3528 dst->elements[i].semantic_name = ptr;
3529 ptr += len + 1;
3532 *signature_strings = ptr;
3534 return WINED3D_OK;
3537 static HRESULT shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3538 const struct wined3d_shader_desc *desc, DWORD float_const_count, enum wined3d_shader_type type,
3539 void *parent, const struct wined3d_parent_ops *parent_ops)
3541 size_t byte_code_size;
3542 SIZE_T total;
3543 HRESULT hr;
3544 char *ptr;
3546 TRACE("byte_code %p, byte_code_size %#lx, format %#x, max_version %#x.\n",
3547 desc->byte_code, (long)desc->byte_code_size, desc->format, desc->max_version);
3549 if (!desc->byte_code)
3550 return WINED3DERR_INVALIDCALL;
3552 if (!(shader->frontend = shader_select_frontend(desc->format)))
3554 FIXME("Unable to find frontend for shader.\n");
3555 return WINED3DERR_INVALIDCALL;
3558 shader->ref = 1;
3559 shader->device = device;
3560 shader->parent = parent;
3561 shader->parent_ops = parent_ops;
3563 total = 0;
3564 if (FAILED(hr = shader_signature_calculate_strings_length(&desc->input_signature, &total)))
3565 return hr;
3566 if (FAILED(hr = shader_signature_calculate_strings_length(&desc->output_signature, &total)))
3567 return hr;
3568 if (FAILED(hr = shader_signature_calculate_strings_length(&desc->patch_constant_signature, &total)))
3569 return hr;
3570 if (total && !(shader->signature_strings = HeapAlloc(GetProcessHeap(), 0, total)))
3571 return E_OUTOFMEMORY;
3572 ptr = shader->signature_strings;
3574 if (FAILED(hr = shader_signature_copy(&shader->input_signature, &desc->input_signature, &ptr)))
3576 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
3577 return hr;
3579 if (FAILED(hr = shader_signature_copy(&shader->output_signature, &desc->output_signature, &ptr)))
3581 HeapFree(GetProcessHeap(), 0, shader->input_signature.elements);
3582 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
3583 return hr;
3585 if (FAILED(hr = shader_signature_copy(&shader->patch_constant_signature, &desc->patch_constant_signature, &ptr)))
3587 HeapFree(GetProcessHeap(), 0, shader->output_signature.elements);
3588 HeapFree(GetProcessHeap(), 0, shader->input_signature.elements);
3589 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
3590 return hr;
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 byte_code_size = desc->byte_code_size;
3602 if (byte_code_size == ~(size_t)0)
3604 const struct wined3d_shader_frontend *fe = shader->frontend;
3605 struct wined3d_shader_version shader_version;
3606 struct wined3d_shader_instruction ins;
3607 const DWORD *ptr;
3608 void *fe_data;
3610 if (!(fe_data = fe->shader_init(desc->byte_code, byte_code_size, &shader->output_signature)))
3612 WARN("Failed to initialise frontend data.\n");
3613 shader_cleanup(shader);
3614 return WINED3DERR_INVALIDCALL;
3617 fe->shader_read_header(fe_data, &ptr, &shader_version);
3618 while (!fe->shader_is_end(fe_data, &ptr))
3619 fe->shader_read_instruction(fe_data, &ptr, &ins);
3621 fe->shader_free(fe_data);
3623 byte_code_size = (ptr - desc->byte_code) * sizeof(*ptr);
3626 if (!(shader->function = HeapAlloc(GetProcessHeap(), 0, byte_code_size)))
3628 shader_cleanup(shader);
3629 return E_OUTOFMEMORY;
3631 memcpy(shader->function, desc->byte_code, byte_code_size);
3632 shader->functionLength = byte_code_size;
3634 if (FAILED(hr = shader_set_function(shader, float_const_count, type, desc->max_version)))
3636 WARN("Failed to set function, hr %#x.\n", hr);
3637 shader_cleanup(shader);
3638 return hr;
3641 shader->load_local_constsF = shader->lconst_inf_or_nan;
3643 wined3d_cs_init_object(shader->device->cs, wined3d_shader_init_object, shader);
3645 return hr;
3648 static HRESULT vertex_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3649 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3651 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3652 unsigned int i;
3653 HRESULT hr;
3655 if (FAILED(hr = shader_init(shader, device, desc, device->adapter->d3d_info.limits.vs_uniform_count,
3656 WINED3D_SHADER_TYPE_VERTEX, parent, parent_ops)))
3657 return hr;
3659 for (i = 0; i < shader->input_signature.element_count; ++i)
3661 const struct wined3d_shader_signature_element *input = &shader->input_signature.elements[i];
3663 if (!(reg_maps->input_registers & (1u << input->register_idx)) || !input->semantic_name)
3664 continue;
3666 shader->u.vs.attributes[input->register_idx].usage =
3667 shader_usage_from_semantic_name(input->semantic_name);
3668 shader->u.vs.attributes[input->register_idx].usage_idx = input->semantic_idx;
3671 if (reg_maps->usesrelconstF && !list_empty(&shader->constantsF))
3672 shader->load_local_constsF = TRUE;
3674 return WINED3D_OK;
3677 static HRESULT geometry_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3678 const struct wined3d_shader_desc *desc, const struct wined3d_stream_output_desc *so_desc,
3679 void *parent, const struct wined3d_parent_ops *parent_ops)
3681 struct wined3d_stream_output_element *elements = NULL;
3682 HRESULT hr;
3684 if (so_desc && !(elements = wined3d_calloc(so_desc->element_count, sizeof(*elements))))
3685 return E_OUTOFMEMORY;
3687 if (FAILED(hr = shader_init(shader, device, desc, 0, WINED3D_SHADER_TYPE_GEOMETRY, parent, parent_ops)))
3689 HeapFree(GetProcessHeap(), 0, elements);
3690 return hr;
3693 if (so_desc)
3695 shader->u.gs.so_desc = *so_desc;
3696 shader->u.gs.so_desc.elements = elements;
3697 memcpy(elements, so_desc->elements, so_desc->element_count * sizeof(*elements));
3700 return WINED3D_OK;
3703 void find_ds_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3704 struct ds_compile_args *args, const struct wined3d_context *context)
3706 const struct wined3d_shader *geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
3707 const struct wined3d_shader *pixel_shader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3708 const struct wined3d_shader *hull_shader = state->shader[WINED3D_SHADER_TYPE_HULL];
3709 const struct wined3d_gl_info *gl_info = context->gl_info;
3711 args->tessellator_output_primitive = hull_shader->u.hs.tessellator_output_primitive;
3712 args->tessellator_partitioning = hull_shader->u.hs.tessellator_partitioning;
3714 args->output_count = geometry_shader ? geometry_shader->limits->packed_input
3715 : pixel_shader ? pixel_shader->limits->packed_input : shader->limits->packed_output;
3716 args->next_shader_type = geometry_shader ? WINED3D_SHADER_TYPE_GEOMETRY : WINED3D_SHADER_TYPE_PIXEL;
3718 args->render_offscreen = context->render_offscreen;
3720 init_interpolation_compile_args(args->interpolation_mode,
3721 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL ? pixel_shader : NULL, gl_info);
3723 args->padding = 0;
3726 void find_gs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3727 struct gs_compile_args *args, const struct wined3d_context *context)
3729 const struct wined3d_shader *pixel_shader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3730 const struct wined3d_gl_info *gl_info = context->gl_info;
3732 args->output_count = pixel_shader ? pixel_shader->limits->packed_input : shader->limits->packed_output;
3734 init_interpolation_compile_args(args->interpolation_mode, pixel_shader, gl_info);
3737 void find_ps_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3738 BOOL position_transformed, struct ps_compile_args *args, const struct wined3d_context *context)
3740 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3741 const struct wined3d_gl_info *gl_info = context->gl_info;
3742 const struct wined3d_texture *texture;
3743 unsigned int i;
3745 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
3746 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, state->fb))
3748 static unsigned int warned = 0;
3750 args->srgb_correction = 1;
3751 if (state->render_states[WINED3D_RS_ALPHABLENDENABLE] && !warned++)
3752 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
3753 "support, expect rendering artifacts.\n");
3756 if (shader->reg_maps.shader_version.major == 1
3757 && shader->reg_maps.shader_version.minor <= 3)
3759 for (i = 0; i < shader->limits->sampler; ++i)
3761 DWORD flags = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
3763 if (flags & WINED3D_TTFF_PROJECTED)
3765 DWORD tex_transform = flags & ~WINED3D_TTFF_PROJECTED;
3767 if (!state->shader[WINED3D_SHADER_TYPE_VERTEX])
3769 enum wined3d_shader_resource_type resource_type = shader->reg_maps.resource_info[i].type;
3770 unsigned int j;
3771 unsigned int index = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
3772 DWORD max_valid = WINED3D_TTFF_COUNT4;
3774 for (j = 0; j < state->vertex_declaration->element_count; ++j)
3776 struct wined3d_vertex_declaration_element *element =
3777 &state->vertex_declaration->elements[j];
3779 if (element->usage == WINED3D_DECL_USAGE_TEXCOORD
3780 && element->usage_idx == index)
3782 max_valid = element->format->component_count;
3783 break;
3786 if (!tex_transform || tex_transform > max_valid)
3788 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
3789 tex_transform, max_valid);
3790 tex_transform = max_valid;
3792 if ((resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1D && tex_transform > WINED3D_TTFF_COUNT1)
3793 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D
3794 && tex_transform > WINED3D_TTFF_COUNT2)
3795 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_3D
3796 && tex_transform > WINED3D_TTFF_COUNT3))
3797 tex_transform |= WINED3D_PSARGS_PROJECTED;
3798 else
3800 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
3801 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
3802 i, tex_transform, resource_type);
3805 else
3806 tex_transform = WINED3D_TTFF_COUNT4 | WINED3D_PSARGS_PROJECTED;
3808 args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
3812 if (shader->reg_maps.shader_version.major == 1
3813 && shader->reg_maps.shader_version.minor <= 4)
3815 for (i = 0; i < shader->limits->sampler; ++i)
3817 const struct wined3d_texture *texture = state->textures[i];
3819 if (!shader->reg_maps.resource_info[i].type)
3820 continue;
3822 /* Treat unbound textures as 2D. The dummy texture will provide
3823 * the proper sample value. The tex_types bitmap defaults to
3824 * 2D because of the memset. */
3825 if (!texture)
3826 continue;
3828 switch (texture->target)
3830 /* RECT textures are distinguished from 2D textures via np2_fixup */
3831 case GL_TEXTURE_RECTANGLE_ARB:
3832 case GL_TEXTURE_2D:
3833 break;
3835 case GL_TEXTURE_3D:
3836 args->tex_types |= WINED3D_SHADER_TEX_3D << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
3837 break;
3839 case GL_TEXTURE_CUBE_MAP_ARB:
3840 args->tex_types |= WINED3D_SHADER_TEX_CUBE << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
3841 break;
3846 if (shader->reg_maps.shader_version.major >= 4)
3848 /* In SM4+ we use dcl_sampler in order to determine if we should use shadow sampler. */
3849 args->shadow = 0;
3850 for (i = 0 ; i < MAX_FRAGMENT_SAMPLERS; ++i)
3851 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
3852 args->np2_fixup = 0;
3854 else
3856 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
3858 if (!shader->reg_maps.resource_info[i].type)
3859 continue;
3861 texture = state->textures[i];
3862 if (!texture)
3864 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
3865 continue;
3867 if (can_use_texture_swizzle(gl_info, texture->resource.format))
3868 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
3869 else
3870 args->color_fixup[i] = texture->resource.format->color_fixup;
3872 if (texture->resource.format_flags & WINED3DFMT_FLAG_SHADOW)
3873 args->shadow |= 1u << i;
3875 /* Flag samplers that need NP2 texcoord fixup. */
3876 if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
3877 args->np2_fixup |= (1u << i);
3881 if (shader->reg_maps.shader_version.major >= 3)
3883 if (position_transformed)
3884 args->vp_mode = pretransformed;
3885 else if (use_vs(state))
3886 args->vp_mode = vertexshader;
3887 else
3888 args->vp_mode = fixedfunction;
3889 args->fog = WINED3D_FFP_PS_FOG_OFF;
3891 else
3893 args->vp_mode = vertexshader;
3894 if (state->render_states[WINED3D_RS_FOGENABLE])
3896 switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
3898 case WINED3D_FOG_NONE:
3899 if (position_transformed || use_vs(state))
3901 args->fog = WINED3D_FFP_PS_FOG_LINEAR;
3902 break;
3905 switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
3907 case WINED3D_FOG_NONE: /* Fall through. */
3908 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
3909 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
3910 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
3912 break;
3914 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
3915 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
3916 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
3919 else
3921 args->fog = WINED3D_FFP_PS_FOG_OFF;
3925 if (context->d3d_info->limits.varying_count < wined3d_max_compat_varyings(context->gl_info))
3927 const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3929 args->texcoords_initialized = 0;
3930 for (i = 0; i < MAX_TEXTURES; ++i)
3932 if (vs)
3934 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.output_registers & (1u << i))
3935 args->texcoords_initialized |= 1u << i;
3937 else
3939 const struct wined3d_stream_info *si = &context->stream_info;
3940 unsigned int coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
3942 if ((state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX] >> WINED3D_FFP_TCI_SHIFT)
3943 & WINED3D_FFP_TCI_MASK
3944 || (coord_idx < MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))))
3945 args->texcoords_initialized |= 1u << i;
3949 else
3951 args->texcoords_initialized = (1u << MAX_TEXTURES) - 1;
3954 args->pointsprite = state->render_states[WINED3D_RS_POINTSPRITEENABLE]
3955 && state->gl_primitive_type == GL_POINTS;
3957 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3958 args->alpha_test_func = WINED3D_CMP_ALWAYS - 1;
3959 else
3960 args->alpha_test_func = (state->render_states[WINED3D_RS_ALPHATESTENABLE]
3961 ? wined3d_sanitize_cmp_func(state->render_states[WINED3D_RS_ALPHAFUNC])
3962 : WINED3D_CMP_ALWAYS) - 1;
3964 if (d3d_info->emulated_flatshading)
3965 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
3967 args->render_offscreen = shader->reg_maps.vpos && gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS]
3968 ? context->render_offscreen : 0;
3971 static HRESULT pixel_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3972 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3974 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3975 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
3976 HRESULT hr;
3978 if (FAILED(hr = shader_init(shader, device, desc, device->adapter->d3d_info.limits.ps_uniform_count,
3979 WINED3D_SHADER_TYPE_PIXEL, parent, parent_ops)))
3980 return hr;
3982 for (i = 0; i < MAX_REG_INPUT; ++i)
3984 if (shader->u.ps.input_reg_used & (1u << i))
3986 ++num_regs_used;
3987 highest_reg_used = i;
3991 /* Don't do any register mapping magic if it is not needed, or if we can't
3992 * achieve anything anyway */
3993 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
3994 || num_regs_used > (gl_info->limits.glsl_varyings / 4)
3995 || shader->reg_maps.shader_version.major >= 4)
3997 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
3999 /* This happens with relative addressing. The input mapper function
4000 * warns about this if the higher registers are declared too, so
4001 * don't write a FIXME here */
4002 WARN("More varying registers used than supported\n");
4005 for (i = 0; i < MAX_REG_INPUT; ++i)
4007 shader->u.ps.input_reg_map[i] = i;
4010 shader->u.ps.declared_in_count = highest_reg_used + 1;
4012 else
4014 shader->u.ps.declared_in_count = 0;
4015 for (i = 0; i < MAX_REG_INPUT; ++i)
4017 if (shader->u.ps.input_reg_used & (1u << i))
4018 shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
4019 else shader->u.ps.input_reg_map[i] = ~0U;
4023 return WINED3D_OK;
4026 void pixelshader_update_resource_types(struct wined3d_shader *shader, WORD tex_types)
4028 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4029 struct wined3d_shader_resource_info *resource_info = reg_maps->resource_info;
4030 unsigned int i;
4032 if (reg_maps->shader_version.major != 1) return;
4034 for (i = 0; i < shader->limits->sampler; ++i)
4036 /* We don't sample from this sampler. */
4037 if (!resource_info[i].type)
4038 continue;
4040 switch ((tex_types >> i * WINED3D_PSARGS_TEXTYPE_SHIFT) & WINED3D_PSARGS_TEXTYPE_MASK)
4042 case WINED3D_SHADER_TEX_2D:
4043 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4044 break;
4046 case WINED3D_SHADER_TEX_3D:
4047 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_3D;
4048 break;
4050 case WINED3D_SHADER_TEX_CUBE:
4051 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_CUBE;
4052 break;
4057 HRESULT CDECL wined3d_shader_create_cs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4058 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4060 struct wined3d_shader *object;
4061 HRESULT hr;
4063 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4064 device, desc, parent, parent_ops, shader);
4066 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
4067 return E_OUTOFMEMORY;
4069 if (FAILED(hr = shader_init(object, device, desc, 0, WINED3D_SHADER_TYPE_COMPUTE, parent, parent_ops)))
4071 WARN("Failed to initialize compute shader, hr %#x.\n", hr);
4072 HeapFree(GetProcessHeap(), 0, object);
4073 return hr;
4076 TRACE("Created compute shader %p.\n", object);
4077 *shader = object;
4079 return WINED3D_OK;
4082 HRESULT CDECL wined3d_shader_create_ds(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4083 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4085 struct wined3d_shader *object;
4086 HRESULT hr;
4088 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4089 device, desc, parent, parent_ops, shader);
4091 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
4092 return E_OUTOFMEMORY;
4094 if (FAILED(hr = shader_init(object, device, desc, 0, WINED3D_SHADER_TYPE_DOMAIN, parent, parent_ops)))
4096 WARN("Failed to initialize domain shader, hr %#x.\n", hr);
4097 HeapFree(GetProcessHeap(), 0, object);
4098 return hr;
4101 TRACE("Created domain shader %p.\n", object);
4102 *shader = object;
4104 return WINED3D_OK;
4107 HRESULT CDECL wined3d_shader_create_gs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4108 const struct wined3d_stream_output_desc *so_desc, void *parent,
4109 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4111 struct wined3d_shader *object;
4112 HRESULT hr;
4114 TRACE("device %p, desc %p, so_desc %p, parent %p, parent_ops %p, shader %p.\n",
4115 device, desc, so_desc, parent, parent_ops, shader);
4117 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
4118 return E_OUTOFMEMORY;
4120 if (FAILED(hr = geometry_shader_init(object, device, desc, so_desc, parent, parent_ops)))
4122 WARN("Failed to initialize geometry shader, hr %#x.\n", hr);
4123 HeapFree(GetProcessHeap(), 0, object);
4124 return hr;
4127 TRACE("Created geometry shader %p.\n", object);
4128 *shader = object;
4130 return WINED3D_OK;
4133 HRESULT CDECL wined3d_shader_create_hs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4134 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4136 struct wined3d_shader *object;
4137 HRESULT hr;
4139 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4140 device, desc, parent, parent_ops, shader);
4142 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
4143 return E_OUTOFMEMORY;
4145 if (FAILED(hr = shader_init(object, device, desc, 0, WINED3D_SHADER_TYPE_HULL, parent, parent_ops)))
4147 WARN("Failed to initialize hull shader, hr %#x.\n", hr);
4148 HeapFree(GetProcessHeap(), 0, object);
4149 return hr;
4152 TRACE("Created hull shader %p.\n", object);
4153 *shader = object;
4155 return WINED3D_OK;
4158 HRESULT CDECL wined3d_shader_create_ps(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4159 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4161 struct wined3d_shader *object;
4162 HRESULT hr;
4164 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4165 device, desc, parent, parent_ops, shader);
4167 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4168 if (!object)
4169 return E_OUTOFMEMORY;
4171 if (FAILED(hr = pixel_shader_init(object, device, desc, parent, parent_ops)))
4173 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
4174 HeapFree(GetProcessHeap(), 0, object);
4175 return hr;
4178 TRACE("Created pixel shader %p.\n", object);
4179 *shader = object;
4181 return WINED3D_OK;
4184 HRESULT CDECL wined3d_shader_create_vs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
4185 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
4187 struct wined3d_shader *object;
4188 HRESULT hr;
4190 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
4191 device, desc, parent, parent_ops, shader);
4193 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4194 if (!object)
4195 return E_OUTOFMEMORY;
4197 if (FAILED(hr = vertex_shader_init(object, device, desc, parent, parent_ops)))
4199 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
4200 HeapFree(GetProcessHeap(), 0, object);
4201 return hr;
4204 TRACE("Created vertex shader %p.\n", object);
4205 *shader = object;
4207 return WINED3D_OK;