wined3d: Validate the SM4 token count (AFL).
[wine.git] / dlls / wined3d / shader.c
blob181cebddf7345324a44bc533c57817448b0eb692
1 /*
2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2005 Oliver Stieber
6 * Copyright 2006 Ivan Gyurdiev
7 * Copyright 2007-2008, 2013 Stefan Dösinger for CodeWeavers
8 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <math.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 /* pow, mul_high, sub_high, mul_low */
37 const float wined3d_srgb_const0[] = {0.41666f, 1.055f, 0.055f, 12.92f};
38 /* cmp */
39 const float wined3d_srgb_const1[] = {0.0031308f, 0.0f, 0.0f, 0.0f};
41 static const char * const shader_opcode_names[] =
43 /* WINED3DSIH_ABS */ "abs",
44 /* WINED3DSIH_ADD */ "add",
45 /* WINED3DSIH_AND */ "and",
46 /* WINED3DSIH_ATOMIC_AND */ "atomic_and",
47 /* WINED3DSIH_ATOMIC_CMP_STORE */ "atomic_cmp_store",
48 /* WINED3DSIH_ATOMIC_IADD */ "atomic_iadd",
49 /* WINED3DSIH_ATOMIC_IMAX */ "atomic_imax",
50 /* WINED3DSIH_ATOMIC_IMIN */ "atomic_imin",
51 /* WINED3DSIH_ATOMIC_OR */ "atomic_or",
52 /* WINED3DSIH_ATOMIC_UMAX */ "atomic_umax",
53 /* WINED3DSIH_ATOMIC_UMIN */ "atomic_umin",
54 /* WINED3DSIH_ATOMIC_XOR */ "atomic_xor",
55 /* WINED3DSIH_BEM */ "bem",
56 /* WINED3DSIH_BFI */ "bfi",
57 /* WINED3DSIH_BFREV */ "bfrev",
58 /* WINED3DSIH_BREAK */ "break",
59 /* WINED3DSIH_BREAKC */ "breakc",
60 /* WINED3DSIH_BREAKP */ "breakp",
61 /* WINED3DSIH_BUFINFO */ "bufinfo",
62 /* WINED3DSIH_CALL */ "call",
63 /* WINED3DSIH_CALLNZ */ "callnz",
64 /* WINED3DSIH_CASE */ "case",
65 /* WINED3DSIH_CMP */ "cmp",
66 /* WINED3DSIH_CND */ "cnd",
67 /* WINED3DSIH_CONTINUE */ "continue",
68 /* WINED3DSIH_COUNTBITS */ "countbits",
69 /* WINED3DSIH_CRS */ "crs",
70 /* WINED3DSIH_CUT */ "cut",
71 /* WINED3DSIH_CUT_STREAM */ "cut_stream",
72 /* WINED3DSIH_DCL */ "dcl",
73 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
74 /* WINED3DSIH_DCL_FUNCTION_BODY */ "dcl_function_body",
75 /* WINED3DSIH_DCL_FUNCTION_TABLE */ "dcl_function_table",
76 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ "dcl_globalFlags",
77 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ "dcl_hs_fork_phase_instance_count",
78 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ "dcl_hs_max_tessfactor",
79 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ "dcl_immediateConstantBuffer",
80 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ "dcl_indexableTemp",
81 /* WINED3DSIH_DCL_INPUT */ "dcl_input",
82 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ "dcl_input_control_point_count",
83 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
84 /* WINED3DSIH_DCL_INPUT_PS */ "dcl_input_ps",
85 /* WINED3DSIH_DCL_INPUT_PS_SGV */ "dcl_input_ps_sgv",
86 /* WINED3DSIH_DCL_INPUT_PS_SIV */ "dcl_input_ps_siv",
87 /* WINED3DSIH_DCL_INPUT_SGV */ "dcl_input_sgv",
88 /* WINED3DSIH_DCL_INPUT_SIV */ "dcl_input_siv",
89 /* WINED3DSIH_DCL_INTERFACE */ "dcl_interface",
90 /* WINED3DSIH_DCL_OUTPUT */ "dcl_output",
91 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ "dcl_output_control_point_count",
92 /* WINED3DSIH_DCL_OUTPUT_SIV */ "dcl_output_siv",
93 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
94 /* WINED3DSIH_DCL_RESOURCE_RAW */ "dcl_resource_raw",
95 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ "dcl_resource_structured",
96 /* WINED3DSIH_DCL_SAMPLER */ "dcl_sampler",
97 /* WINED3DSIH_DCL_STREAM */ "dcl_stream",
98 /* WINED3DSIH_DCL_TEMPS */ "dcl_temps",
99 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ "dcl_tessellator_domain",
100 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ "dcl_tessellator_output_primitive",
101 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ "dcl_tessellator_partitioning",
102 /* WINED3DSIH_DCL_TGSM_RAW */ "dcl_tgsm_raw",
103 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ "dcl_tgsm_structured",
104 /* WINED3DSIH_DCL_THREAD_GROUP */ "dcl_thread_group",
105 /* WINED3DSIH_DCL_UAV_RAW */ "dcl_uav_raw",
106 /* WINED3DSIH_DCL_UAV_STRUCTURED */ "dcl_uav_structured",
107 /* WINED3DSIH_DCL_UAV_TYPED */ "dcl_uav_typed",
108 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
109 /* WINED3DSIH_DEF */ "def",
110 /* WINED3DSIH_DEFAULT */ "default",
111 /* WINED3DSIH_DEFB */ "defb",
112 /* WINED3DSIH_DEFI */ "defi",
113 /* WINED3DSIH_DIV */ "div",
114 /* WINED3DSIH_DP2 */ "dp2",
115 /* WINED3DSIH_DP2ADD */ "dp2add",
116 /* WINED3DSIH_DP3 */ "dp3",
117 /* WINED3DSIH_DP4 */ "dp4",
118 /* WINED3DSIH_DST */ "dst",
119 /* WINED3DSIH_DSX */ "dsx",
120 /* WINED3DSIH_DSX_COARSE */ "deriv_rtx_coarse",
121 /* WINED3DSIH_DSX_FINE */ "deriv_rtx_fine",
122 /* WINED3DSIH_DSY */ "dsy",
123 /* WINED3DSIH_DSY_COARSE */ "deriv_rty_coarse",
124 /* WINED3DSIH_DSY_FINE */ "deriv_rty_fine",
125 /* WINED3DSIH_ELSE */ "else",
126 /* WINED3DSIH_EMIT */ "emit",
127 /* WINED3DSIH_EMIT_STREAM */ "emit_stream",
128 /* WINED3DSIH_ENDIF */ "endif",
129 /* WINED3DSIH_ENDLOOP */ "endloop",
130 /* WINED3DSIH_ENDREP */ "endrep",
131 /* WINED3DSIH_ENDSWITCH */ "endswitch",
132 /* WINED3DSIH_EQ */ "eq",
133 /* WINED3DSIH_EXP */ "exp",
134 /* WINED3DSIH_EXPP */ "expp",
135 /* WINED3DSIH_F16TOF32 */ "f16tof32",
136 /* WINED3DSIH_F32TOF16 */ "f32tof16",
137 /* WINED3DSIH_FCALL */ "fcall",
138 /* WINED3DSIH_FIRSTBIT_HI */ "firstbit_hi",
139 /* WINED3DSIH_FIRSTBIT_LO */ "firstbit_lo",
140 /* WINED3DSIH_FIRSTBIT_SHI */ "firstbit_shi",
141 /* WINED3DSIH_FRC */ "frc",
142 /* WINED3DSIH_FTOI */ "ftoi",
143 /* WINED3DSIH_FTOU */ "ftou",
144 /* WINED3DSIH_GATHER4 */ "gather4",
145 /* WINED3DSIH_GATHER4_C */ "gather4_c",
146 /* WINED3DSIH_GE */ "ge",
147 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ "hs_control_point_phase",
148 /* WINED3DSIH_HS_DECLS */ "hs_decls",
149 /* WINED3DSIH_HS_FORK_PHASE */ "hs_fork_phase",
150 /* WINED3DSIH_HS_JOIN_PHASE */ "hs_join_phase",
151 /* WINED3DSIH_IADD */ "iadd",
152 /* WINED3DSIH_IEQ */ "ieq",
153 /* WINED3DSIH_IF */ "if",
154 /* WINED3DSIH_IFC */ "ifc",
155 /* WINED3DSIH_IGE */ "ige",
156 /* WINED3DSIH_ILT */ "ilt",
157 /* WINED3DSIH_IMAD */ "imad",
158 /* WINED3DSIH_IMAX */ "imax",
159 /* WINED3DSIH_IMIN */ "imin",
160 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ "imm_atomic_alloc",
161 /* WINED3DSIH_IMM_ATOMIC_AND */ "imm_atomic_and",
162 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ "imm_atomic_cmp_exch",
163 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ "imm_atomic_consume",
164 /* WINED3DSIH_IMM_ATOMIC_EXCH */ "imm_atomic_exch",
165 /* WINED3DSIH_IMM_ATOMIC_IADD */ "imm_atomic_iadd",
166 /* WINED3DSIH_IMM_ATOMIC_IMAX */ "imm_atomic_imax",
167 /* WINED3DSIH_IMM_ATOMIC_IMIN */ "imm_atomic_imin",
168 /* WINED3DSIH_IMM_ATOMIC_OR */ "imm_atomic_or",
169 /* WINED3DSIH_IMM_ATOMIC_UMAX */ "imm_atomic_umax",
170 /* WINED3DSIH_IMM_ATOMIC_UMIN */ "imm_atomic_umin",
171 /* WINED3DSIH_IMM_ATOMIC_XOR */ "imm_atomic_xor",
172 /* WINED3DSIH_IMUL */ "imul",
173 /* WINED3DSIH_INE */ "ine",
174 /* WINED3DSIH_INEG */ "ineg",
175 /* WINED3DSIH_ISHL */ "ishl",
176 /* WINED3DSIH_ISHR */ "ishr",
177 /* WINED3DSIH_ITOF */ "itof",
178 /* WINED3DSIH_LABEL */ "label",
179 /* WINED3DSIH_LD */ "ld",
180 /* WINED3DSIH_LD2DMS */ "ld2dms",
181 /* WINED3DSIH_LD_RAW */ "ld_raw",
182 /* WINED3DSIH_LD_STRUCTURED */ "ld_structured",
183 /* WINED3DSIH_LD_UAV_TYPED */ "ld_uav_typed",
184 /* WINED3DSIH_LIT */ "lit",
185 /* WINED3DSIH_LOD */ "lod",
186 /* WINED3DSIH_LOG */ "log",
187 /* WINED3DSIH_LOGP */ "logp",
188 /* WINED3DSIH_LOOP */ "loop",
189 /* WINED3DSIH_LRP */ "lrp",
190 /* WINED3DSIH_LT */ "lt",
191 /* WINED3DSIH_M3x2 */ "m3x2",
192 /* WINED3DSIH_M3x3 */ "m3x3",
193 /* WINED3DSIH_M3x4 */ "m3x4",
194 /* WINED3DSIH_M4x3 */ "m4x3",
195 /* WINED3DSIH_M4x4 */ "m4x4",
196 /* WINED3DSIH_MAD */ "mad",
197 /* WINED3DSIH_MAX */ "max",
198 /* WINED3DSIH_MIN */ "min",
199 /* WINED3DSIH_MOV */ "mov",
200 /* WINED3DSIH_MOVA */ "mova",
201 /* WINED3DSIH_MOVC */ "movc",
202 /* WINED3DSIH_MUL */ "mul",
203 /* WINED3DSIH_NE */ "ne",
204 /* WINED3DSIH_NOP */ "nop",
205 /* WINED3DSIH_NOT */ "not",
206 /* WINED3DSIH_NRM */ "nrm",
207 /* WINED3DSIH_OR */ "or",
208 /* WINED3DSIH_PHASE */ "phase",
209 /* WINED3DSIH_POW */ "pow",
210 /* WINED3DSIH_RCP */ "rcp",
211 /* WINED3DSIH_REP */ "rep",
212 /* WINED3DSIH_RESINFO */ "resinfo",
213 /* WINED3DSIH_RET */ "ret",
214 /* WINED3DSIH_ROUND_NE */ "round_ne",
215 /* WINED3DSIH_ROUND_NI */ "round_ni",
216 /* WINED3DSIH_ROUND_PI */ "round_pi",
217 /* WINED3DSIH_ROUND_Z */ "round_z",
218 /* WINED3DSIH_RSQ */ "rsq",
219 /* WINED3DSIH_SAMPLE */ "sample",
220 /* WINED3DSIH_SAMPLE_B */ "sample_b",
221 /* WINED3DSIH_SAMPLE_C */ "sample_c",
222 /* WINED3DSIH_SAMPLE_C_LZ */ "sample_c_lz",
223 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
224 /* WINED3DSIH_SAMPLE_INFO */ "sample_info",
225 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
226 /* WINED3DSIH_SAMPLE_POS */ "sample_pos",
227 /* WINED3DSIH_SETP */ "setp",
228 /* WINED3DSIH_SGE */ "sge",
229 /* WINED3DSIH_SGN */ "sgn",
230 /* WINED3DSIH_SINCOS */ "sincos",
231 /* WINED3DSIH_SLT */ "slt",
232 /* WINED3DSIH_SQRT */ "sqrt",
233 /* WINED3DSIH_STORE_RAW */ "store_raw",
234 /* WINED3DSIH_STORE_STRUCTURED */ "store_structured",
235 /* WINED3DSIH_STORE_UAV_TYPED */ "store_uav_typed",
236 /* WINED3DSIH_SUB */ "sub",
237 /* WINED3DSIH_SWAPC */ "swapc",
238 /* WINED3DSIH_SWITCH */ "switch",
239 /* WINED3DSIH_SYNC */ "sync",
240 /* WINED3DSIH_TEX */ "texld",
241 /* WINED3DSIH_TEXBEM */ "texbem",
242 /* WINED3DSIH_TEXBEML */ "texbeml",
243 /* WINED3DSIH_TEXCOORD */ "texcrd",
244 /* WINED3DSIH_TEXDEPTH */ "texdepth",
245 /* WINED3DSIH_TEXDP3 */ "texdp3",
246 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
247 /* WINED3DSIH_TEXKILL */ "texkill",
248 /* WINED3DSIH_TEXLDD */ "texldd",
249 /* WINED3DSIH_TEXLDL */ "texldl",
250 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
251 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
252 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
253 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
254 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
255 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
256 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
257 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
258 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
259 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
260 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
261 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
262 /* WINED3DSIH_UBFE */ "ubfe",
263 /* WINED3DSIH_UDIV */ "udiv",
264 /* WINED3DSIH_UGE */ "uge",
265 /* WINED3DSIH_ULT */ "ult",
266 /* WINED3DSIH_UMAX */ "umax",
267 /* WINED3DSIH_UMIN */ "umin",
268 /* WINED3DSIH_USHR */ "ushr",
269 /* WINED3DSIH_UTOF */ "utof",
270 /* WINED3DSIH_XOR */ "xor",
273 static const char * const semantic_names[] =
275 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
276 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
277 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
278 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
279 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
280 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
281 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
282 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
283 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
284 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
285 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
286 /* WINED3D_DECL_USAGE_FOG */ "FOG",
287 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
288 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
291 static const struct
293 enum wined3d_sysval_semantic sysval_semantic;
294 const char *sysval_name;
296 sysval_semantic_names[] =
298 {WINED3D_SV_POSITION, "SV_Position"},
299 {WINED3D_SV_CLIP_DISTANCE, "SV_ClipDistance"},
300 {WINED3D_SV_CULL_DISTANCE, "SV_CullDistance"},
301 {WINED3D_SV_RENDER_TARGET_ARRAY_INDEX, "SV_RenderTargetArrayIndex"},
302 {WINED3D_SV_VIEWPORT_ARRAY_INDEX, "SV_ViewportArrayIndex"},
303 {WINED3D_SV_VERTEX_ID, "SV_VertexID"},
304 {WINED3D_SV_INSTANCE_ID, "SV_InstanceID"},
305 {WINED3D_SV_PRIMITIVE_ID, "SV_PrimitiveID"},
306 {WINED3D_SV_IS_FRONT_FACE, "SV_IsFrontFace"},
307 {WINED3D_SV_SAMPLE_INDEX, "SV_SampleIndex"},
308 {WINED3D_SV_QUAD_U0_TESS_FACTOR, "finalQuadUeq0EdgeTessFactor"},
309 {WINED3D_SV_QUAD_V0_TESS_FACTOR, "finalQuadVeq0EdgeTessFactor"},
310 {WINED3D_SV_QUAD_U1_TESS_FACTOR, "finalQuadUeq1EdgeTessFactor"},
311 {WINED3D_SV_QUAD_V1_TESS_FACTOR, "finalQuadVeq1EdgeTessFactor"},
312 {WINED3D_SV_QUAD_U_INNER_TESS_FACTOR, "finalQuadUInsideTessFactor"},
313 {WINED3D_SV_QUAD_V_INNER_TESS_FACTOR, "finalQuadVInsideTessFactor"},
314 {WINED3D_SV_TRIANGLE_U_TESS_FACTOR, "finalTriUeq0EdgeTessFactor"},
315 {WINED3D_SV_TRIANGLE_V_TESS_FACTOR, "finalTriVeq0EdgeTessFactor"},
316 {WINED3D_SV_TRIANGLE_W_TESS_FACTOR, "finalTriWeq0EdgeTessFactor"},
317 {WINED3D_SV_TRIANGLE_INNER_TESS_FACTOR, "finalTriInsideTessFactor"},
318 {WINED3D_SV_LINE_DETAIL_TESS_FACTOR, "finalLineDetailTessFactor"},
319 {WINED3D_SV_LINE_DENSITY_TESS_FACTOR, "finalLineDensityTessFactor"},
322 static void shader_dump_src_param(struct wined3d_string_buffer *buffer,
323 const struct wined3d_shader_src_param *param, const struct wined3d_shader_version *shader_version);
325 const char *debug_d3dshaderinstructionhandler(enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx)
327 if (handler_idx >= ARRAY_SIZE(shader_opcode_names))
328 return wine_dbg_sprintf("UNRECOGNIZED(%#x)", handler_idx);
330 return shader_opcode_names[handler_idx];
333 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage)
335 if (usage >= ARRAY_SIZE(semantic_names))
337 FIXME("Unrecognized usage %#x.\n", usage);
338 return "UNRECOGNIZED";
341 return semantic_names[usage];
344 static enum wined3d_decl_usage shader_usage_from_semantic_name(const char *name)
346 unsigned int i;
348 for (i = 0; i < ARRAY_SIZE(semantic_names); ++i)
350 if (!strcmp(name, semantic_names[i]))
351 return i;
354 return ~0U;
357 static enum wined3d_sysval_semantic shader_sysval_semantic_from_usage(enum wined3d_decl_usage usage)
359 switch (usage)
361 case WINED3D_DECL_USAGE_POSITION:
362 return WINED3D_SV_POSITION;
363 default:
364 return 0;
368 BOOL shader_match_semantic(const char *semantic_name, enum wined3d_decl_usage usage)
370 return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
373 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
374 const struct wined3d_shader_semantic *s)
376 e->semantic_name = shader_semantic_name_from_usage(s->usage);
377 e->semantic_idx = s->usage_idx;
378 e->sysval_semantic = shader_sysval_semantic_from_usage(s->usage);
379 e->component_type = WINED3D_TYPE_FLOAT;
380 e->register_idx = s->reg.reg.idx[0].offset;
381 e->mask = s->reg.write_mask;
384 static void shader_signature_from_usage(struct wined3d_shader_signature_element *e,
385 enum wined3d_decl_usage usage, UINT usage_idx, UINT reg_idx, DWORD write_mask)
387 e->semantic_name = shader_semantic_name_from_usage(usage);
388 e->semantic_idx = usage_idx;
389 e->sysval_semantic = shader_sysval_semantic_from_usage(usage);
390 e->component_type = WINED3D_TYPE_FLOAT;
391 e->register_idx = reg_idx;
392 e->mask = write_mask;
395 static const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token)
397 switch (version_token >> 16)
399 case WINED3D_SM1_VS:
400 case WINED3D_SM1_PS:
401 return &sm1_shader_frontend;
403 case WINED3D_SM4_PS:
404 case WINED3D_SM4_VS:
405 case WINED3D_SM4_GS:
406 case WINED3D_SM5_HS:
407 case WINED3D_SM5_DS:
408 case WINED3D_SM5_CS:
409 return &sm4_shader_frontend;
411 default:
412 FIXME("Unrecognised version token %#x.\n", version_token);
413 return NULL;
417 void string_buffer_clear(struct wined3d_string_buffer *buffer)
419 buffer->buffer[0] = '\0';
420 buffer->content_size = 0;
423 BOOL string_buffer_init(struct wined3d_string_buffer *buffer)
425 buffer->buffer_size = 32;
426 if (!(buffer->buffer = HeapAlloc(GetProcessHeap(), 0, buffer->buffer_size)))
428 ERR("Failed to allocate shader buffer memory.\n");
429 return FALSE;
432 string_buffer_clear(buffer);
433 return TRUE;
436 void string_buffer_free(struct wined3d_string_buffer *buffer)
438 HeapFree(GetProcessHeap(), 0, buffer->buffer);
441 BOOL string_buffer_resize(struct wined3d_string_buffer *buffer, int rc)
443 char *new_buffer;
444 unsigned int new_buffer_size = buffer->buffer_size * 2;
446 while (rc > 0 && (unsigned int)rc >= new_buffer_size - buffer->content_size)
447 new_buffer_size *= 2;
448 if (!(new_buffer = HeapReAlloc(GetProcessHeap(), 0, buffer->buffer, new_buffer_size)))
450 ERR("Failed to grow buffer.\n");
451 buffer->buffer[buffer->content_size] = '\0';
452 return FALSE;
454 buffer->buffer = new_buffer;
455 buffer->buffer_size = new_buffer_size;
456 return TRUE;
459 int shader_vaddline(struct wined3d_string_buffer *buffer, const char *format, va_list args)
461 unsigned int rem;
462 int rc;
464 rem = buffer->buffer_size - buffer->content_size;
465 rc = vsnprintf(&buffer->buffer[buffer->content_size], rem, format, args);
466 if (rc < 0 /* C89 */ || (unsigned int)rc >= rem /* C99 */)
467 return rc;
469 buffer->content_size += rc;
470 return 0;
473 int shader_addline(struct wined3d_string_buffer *buffer, const char *format, ...)
475 va_list args;
476 int ret;
478 for (;;)
480 va_start(args, format);
481 ret = shader_vaddline(buffer, format, args);
482 va_end(args);
483 if (!ret)
484 return ret;
485 if (!string_buffer_resize(buffer, ret))
486 return -1;
490 struct wined3d_string_buffer *string_buffer_get(struct wined3d_string_buffer_list *list)
492 struct wined3d_string_buffer *buffer;
494 if (list_empty(&list->list))
496 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer));
497 if (!buffer || !string_buffer_init(buffer))
499 ERR("Couldn't allocate buffer for temporary string.\n");
500 HeapFree(GetProcessHeap(), 0, buffer);
501 return NULL;
504 else
506 buffer = LIST_ENTRY(list_head(&list->list), struct wined3d_string_buffer, entry);
507 list_remove(&buffer->entry);
509 string_buffer_clear(buffer);
510 return buffer;
513 static int string_buffer_vsprintf(struct wined3d_string_buffer *buffer, const char *format, va_list args)
515 if (!buffer)
516 return 0;
517 string_buffer_clear(buffer);
518 return shader_vaddline(buffer, format, args);
521 void string_buffer_sprintf(struct wined3d_string_buffer *buffer, const char *format, ...)
523 va_list args;
524 int ret;
526 for (;;)
528 va_start(args, format);
529 ret = string_buffer_vsprintf(buffer, format, args);
530 va_end(args);
531 if (!ret)
532 return;
533 if (!string_buffer_resize(buffer, ret))
534 return;
538 void string_buffer_release(struct wined3d_string_buffer_list *list, struct wined3d_string_buffer *buffer)
540 if (!buffer)
541 return;
542 list_add_head(&list->list, &buffer->entry);
545 void string_buffer_list_init(struct wined3d_string_buffer_list *list)
547 list_init(&list->list);
550 void string_buffer_list_cleanup(struct wined3d_string_buffer_list *list)
552 struct wined3d_string_buffer *buffer, *buffer_next;
554 LIST_FOR_EACH_ENTRY_SAFE(buffer, buffer_next, &list->list, struct wined3d_string_buffer, entry)
556 string_buffer_free(buffer);
557 HeapFree(GetProcessHeap(), 0, buffer);
559 list_init(&list->list);
562 /* Convert floating point offset relative to a register file to an absolute
563 * offset for float constants. */
564 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type, UINT register_idx)
566 switch (register_type)
568 case WINED3DSPR_CONST: return register_idx;
569 case WINED3DSPR_CONST2: return 2048 + register_idx;
570 case WINED3DSPR_CONST3: return 4096 + register_idx;
571 case WINED3DSPR_CONST4: return 6144 + register_idx;
572 default:
573 FIXME("Unsupported register type: %u.\n", register_type);
574 return register_idx;
578 static void shader_delete_constant_list(struct list *clist)
580 struct wined3d_shader_lconst *constant, *constant_next;
582 LIST_FOR_EACH_ENTRY_SAFE(constant, constant_next, clist, struct wined3d_shader_lconst, entry)
583 HeapFree(GetProcessHeap(), 0, constant);
584 list_init(clist);
587 static void shader_set_limits(struct wined3d_shader *shader)
589 static const struct limits_entry
591 unsigned int min_version;
592 unsigned int max_version;
593 struct wined3d_shader_limits limits;
595 vs_limits[] =
597 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
598 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
599 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
600 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
601 * even though they are capable of supporting much more (GL
602 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
603 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
604 * shaders to 256. */
605 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
606 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
607 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 0}},
610 hs_limits[] =
612 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
613 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
615 ds_limits[] =
617 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
618 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
620 gs_limits[] =
622 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
623 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
624 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
627 ps_limits[] =
629 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
630 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
631 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
632 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
633 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
634 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 10}},
635 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 32}},
638 cs_limits[] =
640 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
641 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 0}},
643 const struct limits_entry *limits_array;
644 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
645 shader->reg_maps.shader_version.minor);
646 int i = 0;
648 switch (shader->reg_maps.shader_version.type)
650 default:
651 FIXME("Unexpected shader type %u found.\n", shader->reg_maps.shader_version.type);
652 /* Fall-through. */
653 case WINED3D_SHADER_TYPE_VERTEX:
654 limits_array = vs_limits;
655 break;
656 case WINED3D_SHADER_TYPE_HULL:
657 limits_array = hs_limits;
658 break;
659 case WINED3D_SHADER_TYPE_DOMAIN:
660 limits_array = ds_limits;
661 break;
662 case WINED3D_SHADER_TYPE_GEOMETRY:
663 limits_array = gs_limits;
664 break;
665 case WINED3D_SHADER_TYPE_PIXEL:
666 limits_array = ps_limits;
667 break;
668 case WINED3D_SHADER_TYPE_COMPUTE:
669 limits_array = cs_limits;
670 break;
673 while (limits_array[i].min_version && limits_array[i].min_version <= shader_version)
675 if (shader_version <= limits_array[i].max_version)
677 shader->limits = &limits_array[i].limits;
678 break;
680 ++i;
682 if (!shader->limits)
684 FIXME("Unexpected shader version \"%u.%u\".\n",
685 shader->reg_maps.shader_version.major,
686 shader->reg_maps.shader_version.minor);
687 shader->limits = &limits_array[max(0, i - 1)].limits;
691 static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
693 DWORD idx, shift;
694 idx = bit >> 5;
695 shift = bit & 0x1f;
696 bitmap[idx] |= (1u << shift);
699 static BOOL shader_record_register_usage(struct wined3d_shader *shader, struct wined3d_shader_reg_maps *reg_maps,
700 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type, unsigned int constf_size)
702 switch (reg->type)
704 case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
705 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
706 reg_maps->texcoord |= 1u << reg->idx[0].offset;
707 else
708 reg_maps->address |= 1u << reg->idx[0].offset;
709 break;
711 case WINED3DSPR_TEMP:
712 reg_maps->temporary |= 1u << reg->idx[0].offset;
713 break;
715 case WINED3DSPR_INPUT:
716 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
718 if (reg->idx[0].rel_addr)
720 /* If relative addressing is used, we must assume that all registers
721 * are used. Even if it is a construct like v3[aL], we can't assume
722 * that v0, v1 and v2 aren't read because aL can be negative */
723 unsigned int i;
724 for (i = 0; i < MAX_REG_INPUT; ++i)
726 shader->u.ps.input_reg_used[i] = TRUE;
729 else
731 shader->u.ps.input_reg_used[reg->idx[0].offset] = TRUE;
734 else
735 reg_maps->input_registers |= 1u << reg->idx[0].offset;
736 break;
738 case WINED3DSPR_RASTOUT:
739 if (reg->idx[0].offset == 1)
740 reg_maps->fog = 1;
741 if (reg->idx[0].offset == 2)
742 reg_maps->point_size = 1;
743 break;
745 case WINED3DSPR_MISCTYPE:
746 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
748 if (!reg->idx[0].offset)
749 reg_maps->vpos = 1;
750 else if (reg->idx[0].offset == 1)
751 reg_maps->usesfacing = 1;
753 break;
755 case WINED3DSPR_CONST:
756 if (reg->idx[0].rel_addr)
758 if (reg->idx[0].offset < reg_maps->min_rel_offset)
759 reg_maps->min_rel_offset = reg->idx[0].offset;
760 if (reg->idx[0].offset > reg_maps->max_rel_offset)
761 reg_maps->max_rel_offset = reg->idx[0].offset;
762 reg_maps->usesrelconstF = TRUE;
764 else
766 if (reg->idx[0].offset >= min(shader->limits->constant_float, constf_size))
768 WARN("Shader using float constant %u which is not supported.\n", reg->idx[0].offset);
769 return FALSE;
771 else
773 set_bitmap_bit(reg_maps->constf, reg->idx[0].offset);
776 break;
778 case WINED3DSPR_CONSTINT:
779 if (reg->idx[0].offset >= shader->limits->constant_int)
781 WARN("Shader using integer constant %u which is not supported.\n", reg->idx[0].offset);
782 return FALSE;
784 else
786 reg_maps->integer_constants |= (1u << reg->idx[0].offset);
788 break;
790 case WINED3DSPR_CONSTBOOL:
791 if (reg->idx[0].offset >= shader->limits->constant_bool)
793 WARN("Shader using bool constant %u which is not supported.\n", reg->idx[0].offset);
794 return FALSE;
796 else
798 reg_maps->boolean_constants |= (1u << reg->idx[0].offset);
800 break;
802 case WINED3DSPR_COLOROUT:
803 reg_maps->rt_mask |= (1u << reg->idx[0].offset);
804 break;
806 default:
807 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
808 reg->type, reg->idx[0].offset, reg->idx[1].offset);
809 break;
811 return TRUE;
814 static void shader_record_sample(struct wined3d_shader_reg_maps *reg_maps,
815 unsigned int resource_idx, unsigned int sampler_idx, unsigned int bind_idx)
817 struct wined3d_shader_sampler_map_entry *entries, *entry;
818 struct wined3d_shader_sampler_map *map;
819 unsigned int i;
821 map = &reg_maps->sampler_map;
822 entries = map->entries;
823 for (i = 0; i < map->count; ++i)
825 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
826 return;
829 if (!map->size)
831 if (!(entries = wined3d_calloc(4, sizeof(*entries))))
833 ERR("Failed to allocate sampler map entries.\n");
834 return;
836 map->size = 4;
837 map->entries = entries;
839 else if (map->count == map->size)
841 size_t new_size = map->size * 2;
843 if (sizeof(*entries) * new_size <= sizeof(*entries) * map->size
844 || !(entries = HeapReAlloc(GetProcessHeap(), 0, entries, sizeof(*entries) * new_size)))
846 ERR("Failed to resize sampler map entries.\n");
847 return;
849 map->size = new_size;
850 map->entries = entries;
853 entry = &entries[map->count++];
854 entry->resource_idx = resource_idx;
855 entry->sampler_idx = sampler_idx;
856 entry->bind_idx = bind_idx;
859 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
861 switch (instr)
863 case WINED3DSIH_M4x4:
864 case WINED3DSIH_M3x4:
865 return param == 1 ? 3 : 0;
867 case WINED3DSIH_M4x3:
868 case WINED3DSIH_M3x3:
869 return param == 1 ? 2 : 0;
871 case WINED3DSIH_M3x2:
872 return param == 1 ? 1 : 0;
874 default:
875 return 0;
879 /* Note that this does not count the loop register as an address register. */
880 static HRESULT shader_get_registers_used(struct wined3d_shader *shader, const struct wined3d_shader_frontend *fe,
881 struct wined3d_shader_reg_maps *reg_maps, struct wined3d_shader_signature *input_signature,
882 struct wined3d_shader_signature *output_signature, const DWORD *byte_code, DWORD constf_size)
884 struct wined3d_shader_signature_element input_signature_elements[max(MAX_ATTRIBS, MAX_REG_INPUT)];
885 struct wined3d_shader_signature_element output_signature_elements[MAX_REG_OUTPUT];
886 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
887 void *fe_data = shader->frontend_data;
888 struct wined3d_shader_version shader_version;
889 const DWORD *ptr;
890 unsigned int i;
892 memset(reg_maps, 0, sizeof(*reg_maps));
893 memset(input_signature_elements, 0, sizeof(input_signature_elements));
894 memset(output_signature_elements, 0, sizeof(output_signature_elements));
895 reg_maps->min_rel_offset = ~0U;
896 list_init(&reg_maps->indexable_temps);
898 fe->shader_read_header(fe_data, &ptr, &shader_version);
899 reg_maps->shader_version = shader_version;
901 shader_set_limits(shader);
903 if (!(reg_maps->constf = wined3d_calloc(((min(shader->limits->constant_float, constf_size) + 31) / 32),
904 sizeof(*reg_maps->constf))))
906 ERR("Failed to allocate constant map memory.\n");
907 return E_OUTOFMEMORY;
910 while (!fe->shader_is_end(fe_data, &ptr))
912 struct wined3d_shader_instruction ins;
914 /* Fetch opcode. */
915 fe->shader_read_instruction(fe_data, &ptr, &ins);
917 /* Unhandled opcode, and its parameters. */
918 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
920 TRACE("Skipping unrecognized instruction.\n");
921 continue;
924 /* Handle declarations. */
925 if (ins.handler_idx == WINED3DSIH_DCL
926 || ins.handler_idx == WINED3DSIH_DCL_UAV_TYPED)
928 struct wined3d_shader_semantic *semantic = &ins.declaration.semantic;
929 unsigned int reg_idx = semantic->reg.reg.idx[0].offset;
931 switch (semantic->reg.reg.type)
933 /* Mark input registers used. */
934 case WINED3DSPR_INPUT:
935 if (reg_idx >= MAX_REG_INPUT)
937 ERR("Invalid input register index %u.\n", reg_idx);
938 break;
940 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL && shader_version.major == 3
941 && semantic->usage == WINED3D_DECL_USAGE_POSITION && !semantic->usage_idx)
942 return WINED3DERR_INVALIDCALL;
943 reg_maps->input_registers |= 1u << reg_idx;
944 shader_signature_from_semantic(&input_signature_elements[reg_idx], semantic);
945 break;
947 /* Vertex shader: mark 3.0 output registers used, save token. */
948 case WINED3DSPR_OUTPUT:
949 if (reg_idx >= MAX_REG_OUTPUT)
951 ERR("Invalid output register index %u.\n", reg_idx);
952 break;
954 reg_maps->output_registers |= 1u << reg_idx;
955 shader_signature_from_semantic(&output_signature_elements[reg_idx], semantic);
956 if (semantic->usage == WINED3D_DECL_USAGE_FOG)
957 reg_maps->fog = 1;
958 if (semantic->usage == WINED3D_DECL_USAGE_PSIZE)
959 reg_maps->point_size = 1;
960 break;
962 case WINED3DSPR_SAMPLER:
963 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
964 case WINED3DSPR_RESOURCE:
965 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
967 ERR("Invalid resource index %u.\n", reg_idx);
968 break;
970 reg_maps->resource_info[reg_idx].type = semantic->resource_type;
971 reg_maps->resource_info[reg_idx].data_type = semantic->resource_data_type;
972 break;
974 case WINED3DSPR_UAV:
975 if (reg_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
977 ERR("Invalid UAV resource index %u.\n", reg_idx);
978 break;
980 reg_maps->uav_resource_info[reg_idx].type = semantic->resource_type;
981 reg_maps->uav_resource_info[reg_idx].data_type = semantic->resource_data_type;
982 break;
984 default:
985 TRACE("Not recording DCL register type %#x.\n", semantic->reg.reg.type);
986 break;
989 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
991 struct wined3d_shader_register *reg = &ins.declaration.src.reg;
992 if (reg->idx[0].offset >= WINED3D_MAX_CBS)
993 ERR("Invalid CB index %u.\n", reg->idx[0].offset);
994 else
995 reg_maps->cb_sizes[reg->idx[0].offset] = reg->idx[1].offset;
997 else if (ins.handler_idx == WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER)
999 if (reg_maps->icb)
1000 FIXME("Multiple immediate constant buffers.\n");
1001 reg_maps->icb = ins.declaration.icb;
1003 else if (ins.handler_idx == WINED3DSIH_DCL_INDEXABLE_TEMP)
1005 struct wined3d_shader_indexable_temp *reg;
1007 if (!(reg = HeapAlloc(GetProcessHeap(), 0, sizeof(*reg))))
1008 return E_OUTOFMEMORY;
1010 *reg = ins.declaration.indexable_temp;
1011 list_add_tail(&reg_maps->indexable_temps, &reg->entry);
1013 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE)
1015 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1016 shader->u.gs.input_type = ins.declaration.primitive_type;
1017 else
1018 FIXME("Invalid instruction %#x for shader type %#x.\n",
1019 ins.handler_idx, shader_version.type);
1021 else if (ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
1023 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1024 shader->u.gs.output_type = ins.declaration.primitive_type;
1025 else
1026 FIXME("Invalid instruction %#x for shader type %#x.\n",
1027 ins.handler_idx, shader_version.type);
1029 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_RAW)
1031 unsigned int reg_idx = ins.declaration.dst.reg.idx[0].offset;
1032 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
1034 ERR("Invalid resource index %u.\n", reg_idx);
1035 break;
1037 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_BUFFER;
1038 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1039 reg_maps->resource_info[reg_idx].flags = WINED3D_VIEW_BUFFER_RAW;
1041 else if (ins.handler_idx == WINED3DSIH_DCL_SAMPLER)
1043 if (ins.flags & WINED3DSI_SAMPLER_COMPARISON_MODE)
1044 reg_maps->sampler_comparison_mode |= (1u << ins.declaration.dst.reg.idx[0].offset);
1046 else if (ins.handler_idx == WINED3DSIH_DCL_TEMPS)
1048 reg_maps->temporary_count = ins.declaration.count;
1050 else if (ins.handler_idx == WINED3DSIH_DCL_THREAD_GROUP)
1052 if (shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
1054 shader->u.cs.thread_group_size = ins.declaration.thread_group_size;
1056 else
1058 FIXME("Invalid instruction %#x for shader type %#x.\n",
1059 ins.handler_idx, shader_version.type);
1062 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_RAW)
1064 unsigned int reg_idx = ins.declaration.dst.reg.idx[0].offset;
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 = WINED3D_SHADER_RESOURCE_BUFFER;
1071 reg_maps->uav_resource_info[reg_idx].data_type = WINED3D_DATA_UINT;
1072 reg_maps->uav_resource_info[reg_idx].flags = WINED3D_VIEW_BUFFER_RAW;
1074 else if (ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
1076 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
1077 shader->u.gs.vertices_out = ins.declaration.count;
1078 else
1079 FIXME("Invalid instruction %#x for shader type %#x.\n",
1080 ins.handler_idx, shader_version.type);
1082 else if (ins.handler_idx == WINED3DSIH_DEF)
1084 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1085 float *value;
1086 if (!lconst) return E_OUTOFMEMORY;
1088 lconst->idx = ins.dst[0].reg.idx[0].offset;
1089 memcpy(lconst->value, ins.src[0].reg.u.immconst_data, 4 * sizeof(DWORD));
1090 value = (float *)lconst->value;
1092 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
1093 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1095 if (value[0] < -1.0f) value[0] = -1.0f;
1096 else if (value[0] > 1.0f) value[0] = 1.0f;
1097 if (value[1] < -1.0f) value[1] = -1.0f;
1098 else if (value[1] > 1.0f) value[1] = 1.0f;
1099 if (value[2] < -1.0f) value[2] = -1.0f;
1100 else if (value[2] > 1.0f) value[2] = 1.0f;
1101 if (value[3] < -1.0f) value[3] = -1.0f;
1102 else if (value[3] > 1.0f) value[3] = 1.0f;
1105 list_add_head(&shader->constantsF, &lconst->entry);
1107 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
1108 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
1110 shader->lconst_inf_or_nan = TRUE;
1113 else if (ins.handler_idx == WINED3DSIH_DEFI)
1115 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1116 if (!lconst) return E_OUTOFMEMORY;
1118 lconst->idx = ins.dst[0].reg.idx[0].offset;
1119 memcpy(lconst->value, ins.src[0].reg.u.immconst_data, 4 * sizeof(DWORD));
1121 list_add_head(&shader->constantsI, &lconst->entry);
1122 reg_maps->local_int_consts |= (1u << lconst->idx);
1124 else if (ins.handler_idx == WINED3DSIH_DEFB)
1126 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1127 if (!lconst) return E_OUTOFMEMORY;
1129 lconst->idx = ins.dst[0].reg.idx[0].offset;
1130 memcpy(lconst->value, ins.src[0].reg.u.immconst_data, sizeof(DWORD));
1132 list_add_head(&shader->constantsB, &lconst->entry);
1133 reg_maps->local_bool_consts |= (1u << lconst->idx);
1135 /* For subroutine prototypes. */
1136 else if (ins.handler_idx == WINED3DSIH_LABEL)
1138 reg_maps->labels |= 1u << ins.src[0].reg.idx[0].offset;
1140 /* Set texture, address, temporary registers. */
1141 else
1143 BOOL color0_mov = FALSE;
1144 unsigned int i;
1146 /* This will loop over all the registers and try to
1147 * make a bitmask of the ones we're interested in.
1149 * Relative addressing tokens are ignored, but that's
1150 * okay, since we'll catch any address registers when
1151 * they are initialized (required by spec). */
1152 for (i = 0; i < ins.dst_count; ++i)
1154 if (!shader_record_register_usage(shader, reg_maps, &ins.dst[i].reg,
1155 shader_version.type, constf_size))
1156 return WINED3DERR_INVALIDCALL;
1158 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
1160 UINT idx = ins.dst[i].reg.idx[0].offset;
1162 switch (ins.dst[i].reg.type)
1164 case WINED3DSPR_RASTOUT:
1165 if (shader_version.major >= 3)
1166 break;
1167 switch (idx)
1169 case 0: /* oPos */
1170 reg_maps->output_registers |= 1u << 10;
1171 shader_signature_from_usage(&output_signature_elements[10],
1172 WINED3D_DECL_USAGE_POSITION, 0, 10, WINED3DSP_WRITEMASK_ALL);
1173 break;
1175 case 1: /* oFog */
1176 reg_maps->output_registers |= 1u << 11;
1177 shader_signature_from_usage(&output_signature_elements[11],
1178 WINED3D_DECL_USAGE_FOG, 0, 11, WINED3DSP_WRITEMASK_0);
1179 break;
1181 case 2: /* oPts */
1182 reg_maps->output_registers |= 1u << 11;
1183 shader_signature_from_usage(&output_signature_elements[11],
1184 WINED3D_DECL_USAGE_PSIZE, 0, 11, WINED3DSP_WRITEMASK_1);
1185 break;
1187 break;
1189 case WINED3DSPR_ATTROUT:
1190 if (shader_version.major >= 3)
1191 break;
1192 if (idx < 2)
1194 idx += 8;
1195 if (reg_maps->output_registers & (1u << idx))
1197 output_signature_elements[idx].mask |= ins.dst[i].write_mask;
1199 else
1201 reg_maps->output_registers |= 1u << idx;
1202 shader_signature_from_usage(&output_signature_elements[idx],
1203 WINED3D_DECL_USAGE_COLOR, idx - 8, idx, ins.dst[i].write_mask);
1206 break;
1208 case WINED3DSPR_TEXCRDOUT:
1209 if (shader_version.major >= 3)
1211 reg_maps->u.output_registers_mask[idx] |= ins.dst[i].write_mask;
1212 break;
1214 reg_maps->u.texcoord_mask[idx] |= ins.dst[i].write_mask;
1215 if (reg_maps->output_registers & (1u << idx))
1217 output_signature_elements[idx].mask |= ins.dst[i].write_mask;
1219 else
1221 reg_maps->output_registers |= 1u << idx;
1222 shader_signature_from_usage(&output_signature_elements[idx],
1223 WINED3D_DECL_USAGE_TEXCOORD, idx, idx, ins.dst[i].write_mask);
1225 break;
1227 default:
1228 break;
1232 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1234 if (ins.dst[i].reg.type == WINED3DSPR_COLOROUT && !ins.dst[i].reg.idx[0].offset)
1236 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
1237 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
1238 * the mov and perform the sRGB write correction from the source register.
1240 * However, if the mov is only partial, we can't do this, and if the write
1241 * comes from an instruction other than MOV it is hard to do as well. If
1242 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
1243 shader->u.ps.color0_mov = FALSE;
1244 if (ins.handler_idx == WINED3DSIH_MOV
1245 && ins.dst[i].write_mask == WINED3DSP_WRITEMASK_ALL)
1247 /* Used later when the source register is read. */
1248 color0_mov = TRUE;
1251 /* Also drop the MOV marker if the source register is overwritten prior to the shader
1252 * end
1254 else if (ins.dst[i].reg.type == WINED3DSPR_TEMP
1255 && ins.dst[i].reg.idx[0].offset == shader->u.ps.color0_reg)
1257 shader->u.ps.color0_mov = FALSE;
1261 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
1262 if (shader_version.major == 1
1263 && (ins.handler_idx == WINED3DSIH_TEX
1264 || ins.handler_idx == WINED3DSIH_TEXBEM
1265 || ins.handler_idx == WINED3DSIH_TEXBEML
1266 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
1267 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
1268 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
1269 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
1270 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
1271 || ins.handler_idx == WINED3DSIH_TEXREG2AR
1272 || ins.handler_idx == WINED3DSIH_TEXREG2GB
1273 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
1275 unsigned int reg_idx = ins.dst[i].reg.idx[0].offset;
1277 TRACE("Setting fake 2D resource for 1.x pixelshader.\n");
1278 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
1279 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_FLOAT;
1280 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
1282 /* texbem is only valid with < 1.4 pixel shaders */
1283 if (ins.handler_idx == WINED3DSIH_TEXBEM
1284 || ins.handler_idx == WINED3DSIH_TEXBEML)
1286 reg_maps->bumpmat |= 1u << reg_idx;
1287 if (ins.handler_idx == WINED3DSIH_TEXBEML)
1289 reg_maps->luminanceparams |= 1u << reg_idx;
1293 else if (ins.handler_idx == WINED3DSIH_BEM)
1295 reg_maps->bumpmat |= 1u << ins.dst[i].reg.idx[0].offset;
1299 if ((WINED3DSIH_ATOMIC_AND <= ins.handler_idx && ins.handler_idx <= WINED3DSIH_ATOMIC_XOR)
1300 || (WINED3DSIH_IMM_ATOMIC_AND <= ins.handler_idx
1301 && ins.handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR
1302 && ins.handler_idx != WINED3DSIH_IMM_ATOMIC_CONSUME)
1303 || ins.handler_idx == WINED3DSIH_LD_UAV_TYPED)
1305 unsigned int reg_idx;
1306 if (ins.handler_idx == WINED3DSIH_LD_UAV_TYPED)
1307 reg_idx = ins.src[1].reg.idx[0].offset;
1308 else if (WINED3DSIH_ATOMIC_AND <= ins.handler_idx && ins.handler_idx <= WINED3DSIH_ATOMIC_XOR)
1309 reg_idx = ins.dst[0].reg.idx[0].offset;
1310 else
1311 reg_idx = ins.dst[1].reg.idx[0].offset;
1312 if (reg_idx >= MAX_UNORDERED_ACCESS_VIEWS)
1314 ERR("Invalid UAV index %u.\n", reg_idx);
1315 break;
1317 reg_maps->uav_read_mask |= (1u << reg_idx);
1319 else if (ins.handler_idx == WINED3DSIH_NRM)
1321 reg_maps->usesnrm = 1;
1323 else if (ins.handler_idx == WINED3DSIH_DSY
1324 || ins.handler_idx == WINED3DSIH_DSY_COARSE
1325 || ins.handler_idx == WINED3DSIH_DSY_FINE)
1327 reg_maps->usesdsy = 1;
1329 else if (ins.handler_idx == WINED3DSIH_DSX
1330 || ins.handler_idx == WINED3DSIH_DSX_COARSE
1331 || ins.handler_idx == WINED3DSIH_DSX_FINE)
1333 reg_maps->usesdsx = 1;
1335 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
1336 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
1337 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
1338 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
1339 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
1340 else if (ins.handler_idx == WINED3DSIH_POW) reg_maps->usespow = 1;
1341 else if (ins.handler_idx == WINED3DSIH_LOOP
1342 || ins.handler_idx == WINED3DSIH_REP)
1344 ++cur_loop_depth;
1345 if (cur_loop_depth > max_loop_depth)
1346 max_loop_depth = cur_loop_depth;
1348 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
1349 || ins.handler_idx == WINED3DSIH_ENDREP)
1351 --cur_loop_depth;
1353 else if (ins.handler_idx == WINED3DSIH_SAMPLE
1354 || ins.handler_idx == WINED3DSIH_SAMPLE_B
1355 || ins.handler_idx == WINED3DSIH_SAMPLE_C
1356 || ins.handler_idx == WINED3DSIH_SAMPLE_C_LZ
1357 || ins.handler_idx == WINED3DSIH_SAMPLE_GRAD
1358 || ins.handler_idx == WINED3DSIH_SAMPLE_LOD)
1360 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1361 ins.src[2].reg.idx[0].offset, reg_maps->sampler_map.count);
1363 else if (ins.handler_idx == WINED3DSIH_LD
1364 || (ins.handler_idx == WINED3DSIH_LD_RAW && ins.src[1].reg.type == WINED3DSPR_RESOURCE)
1365 || (ins.handler_idx == WINED3DSIH_RESINFO && ins.src[1].reg.type == WINED3DSPR_RESOURCE))
1367 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1368 WINED3D_SAMPLER_DEFAULT, reg_maps->sampler_map.count);
1371 if (ins.predicate)
1372 if (!shader_record_register_usage(shader, reg_maps, &ins.predicate->reg,
1373 shader_version.type, constf_size))
1374 return WINED3DERR_INVALIDCALL;
1376 for (i = 0; i < ins.src_count; ++i)
1378 unsigned int count = get_instr_extra_regcount(ins.handler_idx, i);
1379 struct wined3d_shader_register reg = ins.src[i].reg;
1381 if (!shader_record_register_usage(shader, reg_maps, &ins.src[i].reg,
1382 shader_version.type, constf_size))
1383 return WINED3DERR_INVALIDCALL;
1384 while (count)
1386 ++reg.idx[0].offset;
1387 if (!shader_record_register_usage(shader, reg_maps, &reg,
1388 shader_version.type, constf_size))
1389 return WINED3DERR_INVALIDCALL;
1390 --count;
1393 if (color0_mov)
1395 if (ins.src[i].reg.type == WINED3DSPR_TEMP
1396 && ins.src[i].swizzle == WINED3DSP_NOSWIZZLE)
1398 shader->u.ps.color0_mov = TRUE;
1399 shader->u.ps.color0_reg = ins.src[i].reg.idx[0].offset;
1405 reg_maps->loop_depth = max_loop_depth;
1407 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1408 * R0 is written to the render target. */
1409 if (shader_version.major < 2 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1410 reg_maps->rt_mask |= (1u << 0);
1412 shader->functionLength = ((const char *)ptr - (const char *)byte_code);
1414 if (input_signature->elements)
1416 for (i = 0; i < input_signature->element_count; ++i)
1418 reg_maps->input_registers |= 1u << input_signature->elements[i].register_idx;
1419 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1421 if (input_signature->elements[i].sysval_semantic == WINED3D_SV_POSITION)
1422 reg_maps->vpos = 1;
1423 else if (input_signature->elements[i].sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
1424 reg_maps->usesfacing = 1;
1428 else if (!input_signature->elements && reg_maps->input_registers)
1430 unsigned int count = wined3d_popcount(reg_maps->input_registers);
1431 struct wined3d_shader_signature_element *e;
1432 unsigned int i;
1434 if (!(input_signature->elements = wined3d_calloc(count, sizeof(*input_signature->elements))))
1435 return E_OUTOFMEMORY;
1436 input_signature->element_count = count;
1438 e = input_signature->elements;
1439 for (i = 0; i < ARRAY_SIZE(input_signature_elements); ++i)
1441 if (!(reg_maps->input_registers & (1u << i)))
1442 continue;
1443 input_signature_elements[i].register_idx = i;
1444 *e++ = input_signature_elements[i];
1448 if (output_signature->elements)
1450 for (i = 0; i < output_signature->element_count; ++i)
1452 reg_maps->output_registers |= 1u << output_signature->elements[i].register_idx;
1455 else if (reg_maps->output_registers)
1457 unsigned int count = wined3d_popcount(reg_maps->output_registers);
1458 struct wined3d_shader_signature_element *e;
1460 if (!(output_signature->elements = wined3d_calloc(count, sizeof(*output_signature->elements))))
1461 return E_OUTOFMEMORY;
1462 output_signature->element_count = count;
1464 e = output_signature->elements;
1465 for (i = 0; i < ARRAY_SIZE(output_signature_elements); ++i)
1467 if (!(reg_maps->output_registers & (1u << i)))
1468 continue;
1469 *e++ = output_signature_elements[i];
1473 return WINED3D_OK;
1476 static void shader_cleanup_reg_maps(struct wined3d_shader_reg_maps *reg_maps)
1478 struct wined3d_shader_indexable_temp *reg, *reg_next;
1480 HeapFree(GetProcessHeap(), 0, reg_maps->constf);
1481 HeapFree(GetProcessHeap(), 0, reg_maps->sampler_map.entries);
1483 LIST_FOR_EACH_ENTRY_SAFE(reg, reg_next, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
1484 HeapFree(GetProcessHeap(), 0, reg);
1485 list_init(&reg_maps->indexable_temps);
1488 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
1490 DWORD map = 1u << max;
1491 map |= map - 1;
1492 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
1494 return wined3d_log2i(map);
1497 static void shader_dump_global_flags(struct wined3d_string_buffer *buffer, DWORD global_flags)
1499 if (global_flags & WINED3DSGF_REFACTORING_ALLOWED)
1501 shader_addline(buffer, "refactoringAllowed");
1502 global_flags &= ~WINED3DSGF_REFACTORING_ALLOWED;
1503 if (global_flags)
1504 shader_addline(buffer, " | ");
1507 if (global_flags & WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS)
1509 shader_addline(buffer, "enableRawAndStructuredBuffers");
1510 global_flags &= ~WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS;
1513 if (global_flags)
1514 shader_addline(buffer, "unknown_flags(%#x)", global_flags);
1517 static void shader_dump_sync_flags(struct wined3d_string_buffer *buffer, DWORD sync_flags)
1519 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
1521 shader_addline(buffer, "_g");
1522 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
1524 if (sync_flags & WINED3DSSF_THREAD_GROUP)
1526 shader_addline(buffer, "_t");
1527 sync_flags &= ~WINED3DSSF_THREAD_GROUP;
1530 if (sync_flags)
1531 shader_addline(buffer, "_unknown_flags(%#x)", sync_flags);
1534 static void shader_dump_uav_flags(struct wined3d_string_buffer *buffer, DWORD uav_flags)
1536 if (uav_flags & WINED3DSUF_GLOBALLY_COHERENT)
1538 shader_addline(buffer, "_glc");
1539 uav_flags &= ~WINED3DSUF_GLOBALLY_COHERENT;
1541 if (uav_flags & WINED3DSUF_ORDER_PRESERVING_COUNTER)
1543 shader_addline(buffer, "_opc");
1544 uav_flags &= ~WINED3DSUF_ORDER_PRESERVING_COUNTER;
1547 if (uav_flags)
1548 shader_addline(buffer, "_unknown_flags(%#x)", uav_flags);
1551 static void shader_dump_tessellator_domain(struct wined3d_string_buffer *buffer,
1552 enum wined3d_tessellator_domain domain)
1554 switch (domain)
1556 case WINED3D_TESSELLATOR_DOMAIN_LINE:
1557 shader_addline(buffer, "line");
1558 break;
1559 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
1560 shader_addline(buffer, "triangle");
1561 break;
1562 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
1563 shader_addline(buffer, "quad");
1564 break;
1565 default:
1566 shader_addline(buffer, "unknown_tessellator_domain(%#x)", domain);
1567 break;
1571 static void shader_dump_tessellator_output_primitive(struct wined3d_string_buffer *buffer,
1572 enum wined3d_tessellator_output_primitive output_primitive)
1574 switch (output_primitive)
1576 case WINED3D_TESSELLATOR_OUTPUT_POINT:
1577 shader_addline(buffer, "point");
1578 break;
1579 case WINED3D_TESSELLATOR_OUTPUT_LINE:
1580 shader_addline(buffer, "line");
1581 break;
1582 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
1583 shader_addline(buffer, "triangle_cw");
1584 break;
1585 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
1586 shader_addline(buffer, "triangle_ccw");
1587 break;
1588 default:
1589 shader_addline(buffer, "unknown_tessellator_output_primitive(%#x)", output_primitive);
1590 break;
1594 static void shader_dump_tessellator_partitioning(struct wined3d_string_buffer *buffer,
1595 enum wined3d_tessellator_partitioning partitioning)
1597 switch (partitioning)
1599 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
1600 shader_addline(buffer, "integer");
1601 break;
1602 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
1603 shader_addline(buffer, "pow2");
1604 break;
1605 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
1606 shader_addline(buffer, "fractional_odd");
1607 break;
1608 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
1609 shader_addline(buffer, "fractional_even");
1610 break;
1611 default:
1612 shader_addline(buffer, "unknown_tessellator_partitioning(%#x)", partitioning);
1613 break;
1617 static void shader_dump_sysval_semantic(struct wined3d_string_buffer *buffer, enum wined3d_sysval_semantic semantic)
1619 unsigned int i;
1621 for (i = 0; i < ARRAY_SIZE(sysval_semantic_names); ++i)
1623 if (sysval_semantic_names[i].sysval_semantic == semantic)
1625 shader_addline(buffer, "%s", sysval_semantic_names[i].sysval_name);
1626 return;
1630 shader_addline(buffer, "unknown_sysval_semantic(%#x)", semantic);
1633 static void shader_dump_decl_usage(struct wined3d_string_buffer *buffer,
1634 const struct wined3d_shader_semantic *semantic, const struct wined3d_shader_version *shader_version)
1636 shader_addline(buffer, "dcl");
1638 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
1640 switch (semantic->resource_type)
1642 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1643 shader_addline(buffer, "_2d");
1644 break;
1646 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1647 shader_addline(buffer, "_3d");
1648 break;
1650 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1651 shader_addline(buffer, "_cube");
1652 break;
1654 default:
1655 shader_addline(buffer, "_unknown_resource_type(%#x)", semantic->resource_type);
1656 break;
1659 else if (semantic->reg.reg.type == WINED3DSPR_RESOURCE || semantic->reg.reg.type == WINED3DSPR_UAV)
1661 if (semantic->reg.reg.type == WINED3DSPR_RESOURCE)
1662 shader_addline(buffer, "_resource_");
1663 else
1664 shader_addline(buffer, "_uav_");
1665 switch (semantic->resource_type)
1667 case WINED3D_SHADER_RESOURCE_BUFFER:
1668 shader_addline(buffer, "buffer");
1669 break;
1671 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1672 shader_addline(buffer, "texture1d");
1673 break;
1675 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1676 shader_addline(buffer, "texture2d");
1677 break;
1679 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
1680 shader_addline(buffer, "texture2dms");
1681 break;
1683 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1684 shader_addline(buffer, "texture3d");
1685 break;
1687 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1688 shader_addline(buffer, "texturecube");
1689 break;
1691 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
1692 shader_addline(buffer, "texture1darray");
1693 break;
1695 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
1696 shader_addline(buffer, "texture2darray");
1697 break;
1699 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
1700 shader_addline(buffer, "texture2dmsarray");
1701 break;
1703 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
1704 shader_addline(buffer, "texturecubearray");
1705 break;
1707 default:
1708 shader_addline(buffer, "unknown");
1709 break;
1711 switch (semantic->resource_data_type)
1713 case WINED3D_DATA_FLOAT:
1714 shader_addline(buffer, " (float)");
1715 break;
1717 case WINED3D_DATA_INT:
1718 shader_addline(buffer, " (int)");
1719 break;
1721 case WINED3D_DATA_UINT:
1722 shader_addline(buffer, " (uint)");
1723 break;
1725 case WINED3D_DATA_UNORM:
1726 shader_addline(buffer, " (unorm)");
1727 break;
1729 case WINED3D_DATA_SNORM:
1730 shader_addline(buffer, " (snorm)");
1731 break;
1733 default:
1734 shader_addline(buffer, " (unknown)");
1735 break;
1738 else
1740 /* Pixel shaders 3.0 don't have usage semantics. */
1741 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL)
1742 return;
1743 else
1744 shader_addline(buffer, "_");
1746 switch (semantic->usage)
1748 case WINED3D_DECL_USAGE_POSITION:
1749 shader_addline(buffer, "position%u", semantic->usage_idx);
1750 break;
1752 case WINED3D_DECL_USAGE_BLEND_INDICES:
1753 shader_addline(buffer, "blend");
1754 break;
1756 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
1757 shader_addline(buffer, "weight");
1758 break;
1760 case WINED3D_DECL_USAGE_NORMAL:
1761 shader_addline(buffer, "normal%u", semantic->usage_idx);
1762 break;
1764 case WINED3D_DECL_USAGE_PSIZE:
1765 shader_addline(buffer, "psize");
1766 break;
1768 case WINED3D_DECL_USAGE_COLOR:
1769 if (!semantic->usage_idx)
1770 shader_addline(buffer, "color");
1771 else
1772 shader_addline(buffer, "specular%u", (semantic->usage_idx - 1));
1773 break;
1775 case WINED3D_DECL_USAGE_TEXCOORD:
1776 shader_addline(buffer, "texture%u", semantic->usage_idx);
1777 break;
1779 case WINED3D_DECL_USAGE_TANGENT:
1780 shader_addline(buffer, "tangent");
1781 break;
1783 case WINED3D_DECL_USAGE_BINORMAL:
1784 shader_addline(buffer, "binormal");
1785 break;
1787 case WINED3D_DECL_USAGE_TESS_FACTOR:
1788 shader_addline(buffer, "tessfactor");
1789 break;
1791 case WINED3D_DECL_USAGE_POSITIONT:
1792 shader_addline(buffer, "positionT%u", semantic->usage_idx);
1793 break;
1795 case WINED3D_DECL_USAGE_FOG:
1796 shader_addline(buffer, "fog");
1797 break;
1799 case WINED3D_DECL_USAGE_DEPTH:
1800 shader_addline(buffer, "depth");
1801 break;
1803 case WINED3D_DECL_USAGE_SAMPLE:
1804 shader_addline(buffer, "sample");
1805 break;
1807 default:
1808 shader_addline(buffer, "<unknown_semantic(%#x)>", semantic->usage);
1809 FIXME("Unrecognised semantic usage %#x.\n", semantic->usage);
1814 static void shader_dump_register(struct wined3d_string_buffer *buffer,
1815 const struct wined3d_shader_register *reg, const struct wined3d_shader_version *shader_version)
1817 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
1818 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
1819 UINT offset = reg->idx[0].offset;
1821 switch (reg->type)
1823 case WINED3DSPR_TEMP:
1824 shader_addline(buffer, "r");
1825 break;
1827 case WINED3DSPR_INPUT:
1828 shader_addline(buffer, "v");
1829 break;
1831 case WINED3DSPR_CONST:
1832 case WINED3DSPR_CONST2:
1833 case WINED3DSPR_CONST3:
1834 case WINED3DSPR_CONST4:
1835 shader_addline(buffer, "c");
1836 offset = shader_get_float_offset(reg->type, offset);
1837 break;
1839 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
1840 shader_addline(buffer, "%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
1841 break;
1843 case WINED3DSPR_RASTOUT:
1844 shader_addline(buffer, "%s", rastout_reg_names[offset]);
1845 break;
1847 case WINED3DSPR_COLOROUT:
1848 shader_addline(buffer, "oC");
1849 break;
1851 case WINED3DSPR_DEPTHOUT:
1852 shader_addline(buffer, "oDepth");
1853 break;
1855 case WINED3DSPR_ATTROUT:
1856 shader_addline(buffer, "oD");
1857 break;
1859 case WINED3DSPR_TEXCRDOUT:
1860 /* Vertex shaders >= 3.0 use general purpose output registers
1861 * (WINED3DSPR_OUTPUT), which can include an address token. */
1862 if (shader_version->major >= 3)
1863 shader_addline(buffer, "o");
1864 else
1865 shader_addline(buffer, "oT");
1866 break;
1868 case WINED3DSPR_CONSTINT:
1869 shader_addline(buffer, "i");
1870 break;
1872 case WINED3DSPR_CONSTBOOL:
1873 shader_addline(buffer, "b");
1874 break;
1876 case WINED3DSPR_LABEL:
1877 shader_addline(buffer, "l");
1878 break;
1880 case WINED3DSPR_LOOP:
1881 shader_addline(buffer, "aL");
1882 break;
1884 case WINED3DSPR_SAMPLER:
1885 shader_addline(buffer, "s");
1886 break;
1888 case WINED3DSPR_MISCTYPE:
1889 if (offset > 1)
1891 FIXME("Unhandled misctype register %u.\n", offset);
1892 shader_addline(buffer, "<unhandled misctype %#x>", offset);
1894 else
1896 shader_addline(buffer, "%s", misctype_reg_names[offset]);
1898 break;
1900 case WINED3DSPR_PREDICATE:
1901 shader_addline(buffer, "p");
1902 break;
1904 case WINED3DSPR_IMMCONST:
1905 shader_addline(buffer, "l");
1906 break;
1908 case WINED3DSPR_CONSTBUFFER:
1909 shader_addline(buffer, "cb");
1910 break;
1912 case WINED3DSPR_IMMCONSTBUFFER:
1913 shader_addline(buffer, "icb");
1914 break;
1916 case WINED3DSPR_PRIMID:
1917 shader_addline(buffer, "primID");
1918 break;
1920 case WINED3DSPR_NULL:
1921 shader_addline(buffer, "null");
1922 break;
1924 case WINED3DSPR_RESOURCE:
1925 shader_addline(buffer, "t");
1926 break;
1928 case WINED3DSPR_UAV:
1929 shader_addline(buffer, "u");
1930 break;
1932 case WINED3DSPR_OUTPOINTID:
1933 shader_addline(buffer, "vOutputControlPointID");
1934 break;
1936 case WINED3DSPR_FORKINSTID:
1937 shader_addline(buffer, "vForkInstanceId");
1938 break;
1940 case WINED3DSPR_INCONTROLPOINT:
1941 shader_addline(buffer, "vicp");
1942 break;
1944 case WINED3DSPR_OUTCONTROLPOINT:
1945 shader_addline(buffer, "vocp");
1946 break;
1948 case WINED3DSPR_PATCHCONST:
1949 shader_addline(buffer, "vpc");
1950 break;
1952 case WINED3DSPR_TESSCOORD:
1953 shader_addline(buffer, "vDomainLocation");
1954 break;
1956 case WINED3DSPR_GROUPSHAREDMEM:
1957 shader_addline(buffer, "g");
1958 break;
1960 case WINED3DSPR_THREADID:
1961 shader_addline(buffer, "vThreadID");
1962 break;
1964 case WINED3DSPR_THREADGROUPID:
1965 shader_addline(buffer, "vThreadGroupID");
1966 break;
1968 case WINED3DSPR_LOCALTHREADID:
1969 shader_addline(buffer, "vThreadIDInGroup");
1970 break;
1972 case WINED3DSPR_LOCALTHREADINDEX:
1973 shader_addline(buffer, "vThreadIDInGroupFlattened");
1974 break;
1976 case WINED3DSPR_IDXTEMP:
1977 shader_addline(buffer, "x");
1978 break;
1980 case WINED3DSPR_STREAM:
1981 shader_addline(buffer, "m");
1982 break;
1984 case WINED3DSPR_FUNCTIONBODY:
1985 shader_addline(buffer, "fb");
1986 break;
1988 case WINED3DSPR_FUNCTIONPOINTER:
1989 shader_addline(buffer, "fp");
1990 break;
1992 case WINED3DSPR_COVERAGE:
1993 shader_addline(buffer, "vCoverage");
1994 break;
1996 case WINED3DSPR_SAMPLEMASK:
1997 shader_addline(buffer, "oMask");
1998 break;
2000 default:
2001 shader_addline(buffer, "<unhandled_rtype(%#x)>", reg->type);
2002 break;
2005 if (reg->type == WINED3DSPR_IMMCONST)
2007 shader_addline(buffer, "(");
2008 switch (reg->immconst_type)
2010 case WINED3D_IMMCONST_SCALAR:
2011 switch (reg->data_type)
2013 case WINED3D_DATA_FLOAT:
2014 shader_addline(buffer, "%.8e", *(const float *)reg->u.immconst_data);
2015 break;
2016 case WINED3D_DATA_INT:
2017 shader_addline(buffer, "%d", reg->u.immconst_data[0]);
2018 break;
2019 case WINED3D_DATA_RESOURCE:
2020 case WINED3D_DATA_SAMPLER:
2021 case WINED3D_DATA_UINT:
2022 shader_addline(buffer, "%u", reg->u.immconst_data[0]);
2023 break;
2024 default:
2025 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
2026 break;
2028 break;
2030 case WINED3D_IMMCONST_VEC4:
2031 switch (reg->data_type)
2033 case WINED3D_DATA_FLOAT:
2034 shader_addline(buffer, "%.8e, %.8e, %.8e, %.8e",
2035 *(const float *)&reg->u.immconst_data[0], *(const float *)&reg->u.immconst_data[1],
2036 *(const float *)&reg->u.immconst_data[2], *(const float *)&reg->u.immconst_data[3]);
2037 break;
2038 case WINED3D_DATA_INT:
2039 shader_addline(buffer, "%d, %d, %d, %d",
2040 reg->u.immconst_data[0], reg->u.immconst_data[1],
2041 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2042 break;
2043 case WINED3D_DATA_RESOURCE:
2044 case WINED3D_DATA_SAMPLER:
2045 case WINED3D_DATA_UINT:
2046 shader_addline(buffer, "%u, %u, %u, %u",
2047 reg->u.immconst_data[0], reg->u.immconst_data[1],
2048 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2049 break;
2050 default:
2051 shader_addline(buffer, "<unhandled data type %#x>", reg->data_type);
2052 break;
2054 break;
2056 default:
2057 shader_addline(buffer, "<unhandled immconst_type %#x>", reg->immconst_type);
2058 break;
2060 shader_addline(buffer, ")");
2062 else if (reg->type != WINED3DSPR_RASTOUT
2063 && reg->type != WINED3DSPR_MISCTYPE
2064 && reg->type != WINED3DSPR_NULL)
2066 if (offset != ~0u)
2068 shader_addline(buffer, "[");
2069 if (reg->idx[0].rel_addr)
2071 shader_dump_src_param(buffer, reg->idx[0].rel_addr, shader_version);
2072 shader_addline(buffer, " + ");
2074 shader_addline(buffer, "%u]", offset);
2076 if (reg->idx[1].offset != ~0u)
2078 shader_addline(buffer, "[");
2079 if (reg->idx[1].rel_addr)
2081 shader_dump_src_param(buffer, reg->idx[1].rel_addr, shader_version);
2082 shader_addline(buffer, " + ");
2084 shader_addline(buffer, "%u]", reg->idx[1].offset);
2088 if (reg->type == WINED3DSPR_FUNCTIONPOINTER)
2089 shader_addline(buffer, "[%u]", reg->u.fp_body_idx);
2093 static void shader_dump_dst_param(struct wined3d_string_buffer *buffer,
2094 const struct wined3d_shader_dst_param *param, const struct wined3d_shader_version *shader_version)
2096 DWORD write_mask = param->write_mask;
2098 shader_dump_register(buffer, &param->reg, shader_version);
2100 if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
2102 static const char write_mask_chars[] = "xyzw";
2104 shader_addline(buffer, ".");
2105 if (write_mask & WINED3DSP_WRITEMASK_0)
2106 shader_addline(buffer, "%c", write_mask_chars[0]);
2107 if (write_mask & WINED3DSP_WRITEMASK_1)
2108 shader_addline(buffer, "%c", write_mask_chars[1]);
2109 if (write_mask & WINED3DSP_WRITEMASK_2)
2110 shader_addline(buffer, "%c", write_mask_chars[2]);
2111 if (write_mask & WINED3DSP_WRITEMASK_3)
2112 shader_addline(buffer, "%c", write_mask_chars[3]);
2116 static void shader_dump_src_param(struct wined3d_string_buffer *buffer,
2117 const struct wined3d_shader_src_param *param, const struct wined3d_shader_version *shader_version)
2119 enum wined3d_shader_src_modifier src_modifier = param->modifiers;
2120 DWORD swizzle = param->swizzle;
2122 if (src_modifier == WINED3DSPSM_NEG
2123 || src_modifier == WINED3DSPSM_BIASNEG
2124 || src_modifier == WINED3DSPSM_SIGNNEG
2125 || src_modifier == WINED3DSPSM_X2NEG
2126 || src_modifier == WINED3DSPSM_ABSNEG)
2127 shader_addline(buffer, "-");
2128 else if (src_modifier == WINED3DSPSM_COMP)
2129 shader_addline(buffer, "1-");
2130 else if (src_modifier == WINED3DSPSM_NOT)
2131 shader_addline(buffer, "!");
2133 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
2134 shader_addline(buffer, "abs(");
2136 shader_dump_register(buffer, &param->reg, shader_version);
2138 switch (src_modifier)
2140 case WINED3DSPSM_NONE: break;
2141 case WINED3DSPSM_NEG: break;
2142 case WINED3DSPSM_NOT: break;
2143 case WINED3DSPSM_BIAS: shader_addline(buffer, "_bias"); break;
2144 case WINED3DSPSM_BIASNEG: shader_addline(buffer, "_bias"); break;
2145 case WINED3DSPSM_SIGN: shader_addline(buffer, "_bx2"); break;
2146 case WINED3DSPSM_SIGNNEG: shader_addline(buffer, "_bx2"); break;
2147 case WINED3DSPSM_COMP: break;
2148 case WINED3DSPSM_X2: shader_addline(buffer, "_x2"); break;
2149 case WINED3DSPSM_X2NEG: shader_addline(buffer, "_x2"); break;
2150 case WINED3DSPSM_DZ: shader_addline(buffer, "_dz"); break;
2151 case WINED3DSPSM_DW: shader_addline(buffer, "_dw"); break;
2152 case WINED3DSPSM_ABSNEG: shader_addline(buffer, ")"); break;
2153 case WINED3DSPSM_ABS: shader_addline(buffer, ")"); break;
2154 default: shader_addline(buffer, "_unknown_modifier(%#x)", src_modifier);
2157 if (swizzle != WINED3DSP_NOSWIZZLE)
2159 static const char swizzle_chars[] = "xyzw";
2160 DWORD swizzle_x = swizzle & 0x03;
2161 DWORD swizzle_y = (swizzle >> 2) & 0x03;
2162 DWORD swizzle_z = (swizzle >> 4) & 0x03;
2163 DWORD swizzle_w = (swizzle >> 6) & 0x03;
2165 if (swizzle_x == swizzle_y
2166 && swizzle_x == swizzle_z
2167 && swizzle_x == swizzle_w)
2169 shader_addline(buffer, ".%c", swizzle_chars[swizzle_x]);
2171 else
2173 shader_addline(buffer, ".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
2174 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
2179 /* Shared code in order to generate the bulk of the shader string.
2180 * NOTE: A description of how to parse tokens can be found on MSDN. */
2181 void shader_generate_main(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
2182 const struct wined3d_shader_reg_maps *reg_maps, void *backend_ctx)
2184 struct wined3d_device *device = shader->device;
2185 const struct wined3d_shader_frontend *fe = shader->frontend;
2186 void *fe_data = shader->frontend_data;
2187 struct wined3d_shader_version shader_version;
2188 struct wined3d_shader_parser_state state;
2189 struct wined3d_shader_instruction ins;
2190 struct wined3d_shader_tex_mx tex_mx;
2191 struct wined3d_shader_context ctx;
2192 const DWORD *ptr;
2194 /* Initialize current parsing state. */
2195 tex_mx.current_row = 0;
2196 state.current_loop_depth = 0;
2197 state.current_loop_reg = 0;
2198 state.in_subroutine = FALSE;
2200 ctx.shader = shader;
2201 ctx.gl_info = &device->adapter->gl_info;
2202 ctx.reg_maps = reg_maps;
2203 ctx.buffer = buffer;
2204 ctx.tex_mx = &tex_mx;
2205 ctx.state = &state;
2206 ctx.backend_data = backend_ctx;
2207 ins.ctx = &ctx;
2209 fe->shader_read_header(fe_data, &ptr, &shader_version);
2211 while (!fe->shader_is_end(fe_data, &ptr))
2213 /* Read opcode. */
2214 fe->shader_read_instruction(fe_data, &ptr, &ins);
2216 /* Unknown opcode and its parameters. */
2217 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
2219 TRACE("Skipping unrecognized instruction.\n");
2220 continue;
2223 if (ins.predicate)
2224 FIXME("Predicates not implemented.\n");
2226 /* Call appropriate function for output target */
2227 device->shader_backend->shader_handle_instruction(&ins);
2231 static void shader_dump_ins_modifiers(struct wined3d_string_buffer *buffer,
2232 const struct wined3d_shader_dst_param *dst)
2234 DWORD mmask = dst->modifiers;
2236 switch (dst->shift)
2238 case 0: break;
2239 case 13: shader_addline(buffer, "_d8"); break;
2240 case 14: shader_addline(buffer, "_d4"); break;
2241 case 15: shader_addline(buffer, "_d2"); break;
2242 case 1: shader_addline(buffer, "_x2"); break;
2243 case 2: shader_addline(buffer, "_x4"); break;
2244 case 3: shader_addline(buffer, "_x8"); break;
2245 default: shader_addline(buffer, "_unhandled_shift(%d)", dst->shift); break;
2248 if (mmask & WINED3DSPDM_SATURATE) shader_addline(buffer, "_sat");
2249 if (mmask & WINED3DSPDM_PARTIALPRECISION) shader_addline(buffer, "_pp");
2250 if (mmask & WINED3DSPDM_MSAMPCENTROID) shader_addline(buffer, "_centroid");
2252 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
2253 if (mmask) FIXME("Unrecognised modifier %#x.\n", mmask);
2256 static void shader_dump_primitive_type(struct wined3d_string_buffer *buffer,
2257 enum wined3d_primitive_type primitive_type)
2259 switch (primitive_type)
2261 case WINED3D_PT_UNDEFINED:
2262 shader_addline(buffer, "undefined");
2263 break;
2264 case WINED3D_PT_POINTLIST:
2265 shader_addline(buffer, "pointlist");
2266 break;
2267 case WINED3D_PT_LINELIST:
2268 shader_addline(buffer, "linelist");
2269 break;
2270 case WINED3D_PT_LINESTRIP:
2271 shader_addline(buffer, "linestrip");
2272 break;
2273 case WINED3D_PT_TRIANGLELIST:
2274 shader_addline(buffer, "trianglelist");
2275 break;
2276 case WINED3D_PT_TRIANGLESTRIP:
2277 shader_addline(buffer, "trianglestrip");
2278 break;
2279 case WINED3D_PT_TRIANGLEFAN:
2280 shader_addline(buffer, "trianglefan");
2281 break;
2282 case WINED3D_PT_LINELIST_ADJ:
2283 shader_addline(buffer, "linelist_adj");
2284 break;
2285 case WINED3D_PT_LINESTRIP_ADJ:
2286 shader_addline(buffer, "linestrip_adj");
2287 break;
2288 case WINED3D_PT_TRIANGLELIST_ADJ:
2289 shader_addline(buffer, "trianglelist_adj");
2290 break;
2291 case WINED3D_PT_TRIANGLESTRIP_ADJ:
2292 shader_addline(buffer, "trianglestrip_adj");
2293 break;
2294 default:
2295 shader_addline(buffer, "<unrecognized_primitive_type %#x>", primitive_type);
2296 break;
2300 static void shader_dump_interpolation_mode(struct wined3d_string_buffer *buffer,
2301 enum wined3d_shader_interpolation_mode interpolation_mode)
2303 switch (interpolation_mode)
2305 case WINED3DSIM_CONSTANT:
2306 shader_addline(buffer, "constant");
2307 break;
2308 case WINED3DSIM_LINEAR:
2309 shader_addline(buffer, "linear");
2310 break;
2311 case WINED3DSIM_LINEAR_CENTROID:
2312 shader_addline(buffer, "linear centroid");
2313 break;
2314 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2315 shader_addline(buffer, "linear noperspective");
2316 break;
2317 case WINED3DSIM_LINEAR_SAMPLE:
2318 shader_addline(buffer, "linear sample");
2319 break;
2320 case WINED3DSIM_LINEAR_NOPERSPECTIVE_CENTROID:
2321 shader_addline(buffer, "linear noperspective centroid");
2322 break;
2323 case WINED3DSIM_LINEAR_NOPERSPECTIVE_SAMPLE:
2324 shader_addline(buffer, "linear noperspective sample");
2325 break;
2326 default:
2327 shader_addline(buffer, "<unrecognized_interpolation_mode %#x>", interpolation_mode);
2328 break;
2332 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data)
2334 struct wined3d_shader_version shader_version;
2335 struct wined3d_string_buffer buffer;
2336 const char *type_prefix;
2337 const char *p, *q;
2338 const DWORD *ptr;
2339 DWORD i;
2341 if (!string_buffer_init(&buffer))
2343 ERR("Failed to initialize string buffer.\n");
2344 return;
2347 fe->shader_read_header(fe_data, &ptr, &shader_version);
2349 TRACE("Parsing %p.\n", ptr);
2351 switch (shader_version.type)
2353 case WINED3D_SHADER_TYPE_VERTEX:
2354 type_prefix = "vs";
2355 break;
2357 case WINED3D_SHADER_TYPE_HULL:
2358 type_prefix = "hs";
2359 break;
2361 case WINED3D_SHADER_TYPE_DOMAIN:
2362 type_prefix = "ds";
2363 break;
2365 case WINED3D_SHADER_TYPE_GEOMETRY:
2366 type_prefix = "gs";
2367 break;
2369 case WINED3D_SHADER_TYPE_PIXEL:
2370 type_prefix = "ps";
2371 break;
2373 case WINED3D_SHADER_TYPE_COMPUTE:
2374 type_prefix = "cs";
2375 break;
2377 default:
2378 FIXME("Unhandled shader type %#x.\n", shader_version.type);
2379 type_prefix = "unknown";
2380 break;
2383 shader_addline(&buffer, "%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
2385 while (!fe->shader_is_end(fe_data, &ptr))
2387 struct wined3d_shader_instruction ins;
2389 fe->shader_read_instruction(fe_data, &ptr, &ins);
2390 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
2392 WARN("Skipping unrecognized instruction.\n");
2393 shader_addline(&buffer, "<unrecognized instruction>\n");
2394 continue;
2397 if (ins.handler_idx == WINED3DSIH_DCL || ins.handler_idx == WINED3DSIH_DCL_UAV_TYPED)
2399 shader_dump_decl_usage(&buffer, &ins.declaration.semantic, &shader_version);
2400 shader_dump_ins_modifiers(&buffer, &ins.declaration.semantic.reg);
2401 shader_addline(&buffer, " ");
2402 shader_dump_dst_param(&buffer, &ins.declaration.semantic.reg, &shader_version);
2404 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
2406 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2407 shader_dump_src_param(&buffer, &ins.declaration.src, &shader_version);
2408 shader_addline(&buffer, ", %s",
2409 ins.flags & WINED3DSI_INDEXED_DYNAMIC ? "dynamicIndexed" : "immediateIndexed");
2411 else if (ins.handler_idx == WINED3DSIH_DCL_FUNCTION_BODY)
2413 shader_addline(&buffer, "%s fb%u",
2414 shader_opcode_names[ins.handler_idx], ins.declaration.index);
2416 else if (ins.handler_idx == WINED3DSIH_DCL_FUNCTION_TABLE)
2418 shader_addline(&buffer, "%s ft%u = {...}",
2419 shader_opcode_names[ins.handler_idx], ins.declaration.index);
2421 else if (ins.handler_idx == WINED3DSIH_DCL_GLOBAL_FLAGS)
2423 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2424 shader_dump_global_flags(&buffer, ins.flags);
2426 else if (ins.handler_idx == WINED3DSIH_DCL_HS_MAX_TESSFACTOR)
2428 shader_addline(&buffer, "%s %.8e", shader_opcode_names[ins.handler_idx],
2429 ins.declaration.max_tessellation_factor);
2431 else if (ins.handler_idx == WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER)
2433 shader_addline(&buffer, "%s {\n", shader_opcode_names[ins.handler_idx]);
2434 for (i = 0; i < ins.declaration.icb->vec4_count; ++i)
2436 shader_addline(&buffer, " {0x%08x, 0x%08x, 0x%08x, 0x%08x},\n",
2437 ins.declaration.icb->data[4 * i + 0],
2438 ins.declaration.icb->data[4 * i + 1],
2439 ins.declaration.icb->data[4 * i + 2],
2440 ins.declaration.icb->data[4 * i + 3]);
2442 shader_addline(&buffer, "}");
2444 else if (ins.handler_idx == WINED3DSIH_DCL_INDEXABLE_TEMP)
2446 shader_addline(&buffer, "%s x[%u][%u], %u", shader_opcode_names[ins.handler_idx],
2447 ins.declaration.indexable_temp.register_idx,
2448 ins.declaration.indexable_temp.register_size,
2449 ins.declaration.indexable_temp.component_count);
2451 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS)
2453 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2454 shader_dump_interpolation_mode(&buffer, ins.flags);
2455 shader_addline(&buffer, " ");
2456 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2458 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SGV
2459 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SGV
2460 || ins.handler_idx == WINED3DSIH_DCL_INPUT_SIV
2461 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_SIV)
2463 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2464 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2465 shader_addline(&buffer, ", ");
2466 shader_dump_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2468 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PS_SIV)
2470 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2471 shader_dump_interpolation_mode(&buffer, ins.flags);
2472 shader_addline(&buffer, " ");
2473 shader_dump_dst_param(&buffer, &ins.declaration.register_semantic.reg, &shader_version);
2474 shader_addline(&buffer, ", ");
2475 shader_dump_sysval_semantic(&buffer, ins.declaration.register_semantic.sysval_semantic);
2477 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT
2478 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT)
2480 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2481 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2483 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE
2484 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
2486 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2487 shader_dump_primitive_type(&buffer, ins.declaration.primitive_type);
2489 else if (ins.handler_idx == WINED3DSIH_DCL_INTERFACE)
2491 shader_addline(&buffer, "%s fp[%u][%u][%u] = {...}",
2492 shader_opcode_names[ins.handler_idx], ins.declaration.fp.index,
2493 ins.declaration.fp.array_size, ins.declaration.fp.body_count);
2495 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_RAW)
2497 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2498 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2500 else if (ins.handler_idx == WINED3DSIH_DCL_RESOURCE_STRUCTURED)
2502 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2503 shader_dump_dst_param(&buffer, &ins.declaration.structured_resource.reg, &shader_version);
2504 shader_addline(&buffer, ", %u", ins.declaration.structured_resource.byte_stride);
2506 else if (ins.handler_idx == WINED3DSIH_DCL_SAMPLER)
2508 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2509 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2510 if (ins.flags == WINED3DSI_SAMPLER_COMPARISON_MODE)
2511 shader_addline(&buffer, ", comparisonMode");
2513 else if (ins.handler_idx == WINED3DSIH_DCL_TEMPS
2514 || ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT
2515 || ins.handler_idx == WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT
2516 || ins.handler_idx == WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT
2517 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT)
2519 shader_addline(&buffer, "%s %u", shader_opcode_names[ins.handler_idx], ins.declaration.count);
2521 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_DOMAIN)
2523 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2524 shader_dump_tessellator_domain(&buffer, ins.declaration.tessellator_domain);
2526 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE)
2528 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2529 shader_dump_tessellator_output_primitive(&buffer, ins.declaration.tessellator_output_primitive);
2531 else if (ins.handler_idx == WINED3DSIH_DCL_TESSELLATOR_PARTITIONING)
2533 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2534 shader_dump_tessellator_partitioning(&buffer, ins.declaration.tessellator_partitioning);
2536 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_RAW)
2538 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2539 shader_dump_dst_param(&buffer, &ins.declaration.tgsm_raw.reg, &shader_version);
2540 shader_addline(&buffer, ", %u", ins.declaration.tgsm_raw.byte_count);
2542 else if (ins.handler_idx == WINED3DSIH_DCL_TGSM_STRUCTURED)
2544 shader_addline(&buffer, "%s ", shader_opcode_names[ins.handler_idx]);
2545 shader_dump_dst_param(&buffer, &ins.declaration.tgsm_structured.reg, &shader_version);
2546 shader_addline(&buffer, ", %u, %u", ins.declaration.tgsm_structured.byte_stride,
2547 ins.declaration.tgsm_structured.structure_count);
2549 else if (ins.handler_idx == WINED3DSIH_DCL_THREAD_GROUP)
2551 shader_addline(&buffer, "%s %u, %u, %u", shader_opcode_names[ins.handler_idx],
2552 ins.declaration.thread_group_size.x,
2553 ins.declaration.thread_group_size.y,
2554 ins.declaration.thread_group_size.z);
2556 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_RAW)
2558 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2559 shader_dump_uav_flags(&buffer, ins.flags);
2560 shader_addline(&buffer, " ");
2561 shader_dump_dst_param(&buffer, &ins.declaration.dst, &shader_version);
2563 else if (ins.handler_idx == WINED3DSIH_DCL_UAV_STRUCTURED)
2565 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2566 shader_dump_uav_flags(&buffer, ins.flags);
2567 shader_addline(&buffer, " ");
2568 shader_dump_dst_param(&buffer, &ins.declaration.structured_resource.reg, &shader_version);
2569 shader_addline(&buffer, ", %u", ins.declaration.structured_resource.byte_stride);
2571 else if (ins.handler_idx == WINED3DSIH_DEF)
2573 shader_addline(&buffer, "def c%u = %.8e, %.8e, %.8e, %.8e", shader_get_float_offset(ins.dst[0].reg.type,
2574 ins.dst[0].reg.idx[0].offset),
2575 *(const float *)&ins.src[0].reg.u.immconst_data[0],
2576 *(const float *)&ins.src[0].reg.u.immconst_data[1],
2577 *(const float *)&ins.src[0].reg.u.immconst_data[2],
2578 *(const float *)&ins.src[0].reg.u.immconst_data[3]);
2580 else if (ins.handler_idx == WINED3DSIH_DEFI)
2582 shader_addline(&buffer, "defi i%u = %d, %d, %d, %d", ins.dst[0].reg.idx[0].offset,
2583 ins.src[0].reg.u.immconst_data[0],
2584 ins.src[0].reg.u.immconst_data[1],
2585 ins.src[0].reg.u.immconst_data[2],
2586 ins.src[0].reg.u.immconst_data[3]);
2588 else if (ins.handler_idx == WINED3DSIH_DEFB)
2590 shader_addline(&buffer, "defb b%u = %s",
2591 ins.dst[0].reg.idx[0].offset, ins.src[0].reg.u.immconst_data[0] ? "true" : "false");
2593 else
2595 if (ins.predicate)
2597 shader_addline(&buffer, "(");
2598 shader_dump_src_param(&buffer, ins.predicate, &shader_version);
2599 shader_addline(&buffer, ") ");
2602 /* PixWin marks instructions with the coissue flag with a '+' */
2603 if (ins.coissue)
2604 shader_addline(&buffer, "+");
2606 shader_addline(&buffer, "%s", shader_opcode_names[ins.handler_idx]);
2608 if (ins.handler_idx == WINED3DSIH_BREAKP
2609 || ins.handler_idx == WINED3DSIH_IF)
2611 switch (ins.flags)
2613 case WINED3D_SHADER_CONDITIONAL_OP_NZ: shader_addline(&buffer, "_nz"); break;
2614 case WINED3D_SHADER_CONDITIONAL_OP_Z: shader_addline(&buffer, "_z"); break;
2615 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags); break;
2618 else if (ins.handler_idx == WINED3DSIH_IFC
2619 || ins.handler_idx == WINED3DSIH_BREAKC)
2621 switch (ins.flags)
2623 case WINED3D_SHADER_REL_OP_GT: shader_addline(&buffer, "_gt"); break;
2624 case WINED3D_SHADER_REL_OP_EQ: shader_addline(&buffer, "_eq"); break;
2625 case WINED3D_SHADER_REL_OP_GE: shader_addline(&buffer, "_ge"); break;
2626 case WINED3D_SHADER_REL_OP_LT: shader_addline(&buffer, "_lt"); break;
2627 case WINED3D_SHADER_REL_OP_NE: shader_addline(&buffer, "_ne"); break;
2628 case WINED3D_SHADER_REL_OP_LE: shader_addline(&buffer, "_le"); break;
2629 default: shader_addline(&buffer, "_(%u)", ins.flags);
2632 else if (ins.handler_idx == WINED3DSIH_TEX
2633 && shader_version.major >= 2
2634 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
2636 shader_addline(&buffer, "p");
2638 else if (ins.handler_idx == WINED3DSIH_RESINFO && ins.flags)
2640 switch (ins.flags)
2642 case WINED3DSI_RESINFO_RCP_FLOAT: shader_addline(&buffer, "_rcpFloat"); break;
2643 case WINED3DSI_RESINFO_UINT: shader_addline(&buffer, "_uint"); break;
2644 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags);
2647 else if (ins.handler_idx == WINED3DSIH_SAMPLE_INFO && ins.flags)
2649 switch (ins.flags)
2651 case WINED3DSI_SAMPLE_INFO_UINT: shader_addline(&buffer, "_uint"); break;
2652 default: shader_addline(&buffer, "_unrecognized(%#x)", ins.flags);
2655 else if (ins.handler_idx == WINED3DSIH_SYNC)
2657 shader_dump_sync_flags(&buffer, ins.flags);
2660 if (wined3d_shader_instruction_has_texel_offset(&ins))
2661 shader_addline(&buffer, "(%d,%d,%d)", ins.texel_offset.u, ins.texel_offset.v, ins.texel_offset.w);
2663 for (i = 0; i < ins.dst_count; ++i)
2665 shader_dump_ins_modifiers(&buffer, &ins.dst[i]);
2666 shader_addline(&buffer, !i ? " " : ", ");
2667 shader_dump_dst_param(&buffer, &ins.dst[i], &shader_version);
2670 /* Other source tokens */
2671 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
2673 shader_addline(&buffer, !i ? " " : ", ");
2674 shader_dump_src_param(&buffer, &ins.src[i - ins.dst_count], &shader_version);
2677 shader_addline(&buffer, "\n");
2680 for (p = buffer.buffer; *p; p = q)
2682 if (!(q = strstr(p, "\n")))
2683 q = p + strlen(p);
2684 else
2685 ++q;
2686 TRACE(" %.*s", (int)(q - p), p);
2689 string_buffer_free(&buffer);
2692 static void shader_cleanup(struct wined3d_shader *shader)
2694 HeapFree(GetProcessHeap(), 0, shader->output_signature.elements);
2695 HeapFree(GetProcessHeap(), 0, shader->input_signature.elements);
2696 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
2697 shader->device->shader_backend->shader_destroy(shader);
2698 shader_cleanup_reg_maps(&shader->reg_maps);
2699 HeapFree(GetProcessHeap(), 0, shader->function);
2700 shader_delete_constant_list(&shader->constantsF);
2701 shader_delete_constant_list(&shader->constantsB);
2702 shader_delete_constant_list(&shader->constantsI);
2703 list_remove(&shader->shader_list_entry);
2705 if (shader->frontend && shader->frontend_data)
2706 shader->frontend->shader_free(shader->frontend_data);
2709 struct shader_none_priv
2711 const struct wined3d_vertex_pipe_ops *vertex_pipe;
2712 const struct fragment_pipeline *fragment_pipe;
2713 BOOL ffp_proj_control;
2716 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
2717 static void shader_none_select_compute(void *shader_priv, struct wined3d_context *context,
2718 const struct wined3d_state *state) {}
2719 static void shader_none_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count) {}
2720 static void shader_none_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count) {}
2721 static void shader_none_load_constants(void *shader_priv, struct wined3d_context *context,
2722 const struct wined3d_state *state) {}
2723 static void shader_none_destroy(struct wined3d_shader *shader) {}
2724 static void shader_none_free_context_data(struct wined3d_context *context) {}
2725 static void shader_none_init_context_state(struct wined3d_context *context) {}
2727 /* Context activation is done by the caller. */
2728 static void shader_none_select(void *shader_priv, struct wined3d_context *context,
2729 const struct wined3d_state *state)
2731 const struct wined3d_gl_info *gl_info = context->gl_info;
2732 struct shader_none_priv *priv = shader_priv;
2734 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
2735 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
2738 /* Context activation is done by the caller. */
2739 static void shader_none_disable(void *shader_priv, struct wined3d_context *context)
2741 struct shader_none_priv *priv = shader_priv;
2742 const struct wined3d_gl_info *gl_info = context->gl_info;
2744 priv->vertex_pipe->vp_enable(gl_info, FALSE);
2745 priv->fragment_pipe->enable_extension(gl_info, FALSE);
2747 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
2748 | (1u << WINED3D_SHADER_TYPE_VERTEX)
2749 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
2750 | (1u << WINED3D_SHADER_TYPE_HULL)
2751 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
2752 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
2755 static HRESULT shader_none_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
2756 const struct fragment_pipeline *fragment_pipe)
2758 struct fragment_caps fragment_caps;
2759 void *vertex_priv, *fragment_priv;
2760 struct shader_none_priv *priv;
2762 if (!(priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv))))
2763 return E_OUTOFMEMORY;
2765 if (!(vertex_priv = vertex_pipe->vp_alloc(&none_shader_backend, priv)))
2767 ERR("Failed to initialize vertex pipe.\n");
2768 HeapFree(GetProcessHeap(), 0, priv);
2769 return E_FAIL;
2772 if (!(fragment_priv = fragment_pipe->alloc_private(&none_shader_backend, priv)))
2774 ERR("Failed to initialize fragment pipe.\n");
2775 vertex_pipe->vp_free(device);
2776 HeapFree(GetProcessHeap(), 0, priv);
2777 return E_FAIL;
2780 priv->vertex_pipe = vertex_pipe;
2781 priv->fragment_pipe = fragment_pipe;
2782 fragment_pipe->get_caps(&device->adapter->gl_info, &fragment_caps);
2783 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
2785 device->vertex_priv = vertex_priv;
2786 device->fragment_priv = fragment_priv;
2787 device->shader_priv = priv;
2789 return WINED3D_OK;
2792 static void shader_none_free(struct wined3d_device *device)
2794 struct shader_none_priv *priv = device->shader_priv;
2796 priv->fragment_pipe->free_private(device);
2797 priv->vertex_pipe->vp_free(device);
2798 HeapFree(GetProcessHeap(), 0, priv);
2801 static BOOL shader_none_allocate_context_data(struct wined3d_context *context)
2803 return TRUE;
2806 static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
2808 /* Set the shader caps to 0 for the none shader backend */
2809 caps->vs_version = 0;
2810 caps->hs_version = 0;
2811 caps->ds_version = 0;
2812 caps->gs_version = 0;
2813 caps->ps_version = 0;
2814 caps->cs_version = 0;
2815 caps->vs_uniform_count = 0;
2816 caps->ps_uniform_count = 0;
2817 caps->ps_1x_max_value = 0.0f;
2818 caps->varying_count = 0;
2819 caps->wined3d_caps = 0;
2822 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
2824 /* We "support" every possible fixup, since we don't support any shader
2825 * model, and will never have to actually sample a texture. */
2826 return TRUE;
2829 static BOOL shader_none_has_ffp_proj_control(void *shader_priv)
2831 struct shader_none_priv *priv = shader_priv;
2833 return priv->ffp_proj_control;
2836 const struct wined3d_shader_backend_ops none_shader_backend =
2838 shader_none_handle_instruction,
2839 shader_none_select,
2840 shader_none_select_compute,
2841 shader_none_disable,
2842 shader_none_update_float_vertex_constants,
2843 shader_none_update_float_pixel_constants,
2844 shader_none_load_constants,
2845 shader_none_destroy,
2846 shader_none_alloc,
2847 shader_none_free,
2848 shader_none_allocate_context_data,
2849 shader_none_free_context_data,
2850 shader_none_init_context_state,
2851 shader_none_get_caps,
2852 shader_none_color_fixup_supported,
2853 shader_none_has_ffp_proj_control,
2856 static HRESULT shader_set_function(struct wined3d_shader *shader, const DWORD *byte_code,
2857 size_t byte_code_size, DWORD float_const_count, enum wined3d_shader_type type, unsigned int max_version)
2859 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2860 const struct wined3d_shader_frontend *fe;
2861 HRESULT hr;
2862 unsigned int backend_version;
2863 const struct wined3d_d3d_info *d3d_info = &shader->device->adapter->d3d_info;
2865 TRACE("shader %p, byte_code %p, byte_code_size %#lx, float_const_count %u, type %#x, max_version %u.\n",
2866 shader, byte_code, (long)byte_code_size, float_const_count, type, max_version);
2868 list_init(&shader->constantsF);
2869 list_init(&shader->constantsB);
2870 list_init(&shader->constantsI);
2871 shader->lconst_inf_or_nan = FALSE;
2872 list_init(&reg_maps->indexable_temps);
2874 fe = shader_select_frontend(*byte_code);
2875 if (!fe)
2877 FIXME("Unable to find frontend for shader.\n");
2878 return WINED3DERR_INVALIDCALL;
2880 shader->frontend = fe;
2881 shader->frontend_data = fe->shader_init(byte_code, byte_code_size, &shader->output_signature);
2882 if (!shader->frontend_data)
2884 FIXME("Failed to initialize frontend.\n");
2885 return WINED3DERR_INVALIDCALL;
2888 /* First pass: trace shader. */
2889 if (TRACE_ON(d3d_shader))
2890 shader_trace_init(fe, shader->frontend_data);
2892 /* Second pass: figure out which registers are used, what the semantics are, etc. */
2893 if (FAILED(hr = shader_get_registers_used(shader, fe, reg_maps, &shader->input_signature,
2894 &shader->output_signature, byte_code, float_const_count)))
2895 return hr;
2897 if (reg_maps->shader_version.type != type)
2899 WARN("Wrong shader type %d.\n", reg_maps->shader_version.type);
2900 return WINED3DERR_INVALIDCALL;
2902 if (reg_maps->shader_version.major > max_version)
2904 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps->shader_version.major);
2905 return WINED3DERR_INVALIDCALL;
2907 switch (type)
2909 case WINED3D_SHADER_TYPE_VERTEX:
2910 backend_version = d3d_info->limits.vs_version;
2911 break;
2912 case WINED3D_SHADER_TYPE_HULL:
2913 backend_version = d3d_info->limits.hs_version;
2914 break;
2915 case WINED3D_SHADER_TYPE_DOMAIN:
2916 backend_version = d3d_info->limits.ds_version;
2917 break;
2918 case WINED3D_SHADER_TYPE_GEOMETRY:
2919 backend_version = d3d_info->limits.gs_version;
2920 break;
2921 case WINED3D_SHADER_TYPE_PIXEL:
2922 backend_version = d3d_info->limits.ps_version;
2923 break;
2924 case WINED3D_SHADER_TYPE_COMPUTE:
2925 backend_version = d3d_info->limits.cs_version;
2926 break;
2927 default:
2928 FIXME("No backend version-checking for this shader type.\n");
2929 backend_version = 0;
2931 if (reg_maps->shader_version.major > backend_version)
2933 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
2934 reg_maps->shader_version.major, reg_maps->shader_version.minor);
2935 return WINED3DERR_INVALIDCALL;
2938 if (!(shader->function = HeapAlloc(GetProcessHeap(), 0, shader->functionLength)))
2939 return E_OUTOFMEMORY;
2940 memcpy(shader->function, byte_code, shader->functionLength);
2942 return WINED3D_OK;
2945 ULONG CDECL wined3d_shader_incref(struct wined3d_shader *shader)
2947 ULONG refcount = InterlockedIncrement(&shader->ref);
2949 TRACE("%p increasing refcount to %u.\n", shader, refcount);
2951 return refcount;
2954 static void wined3d_shader_destroy_object(void *object)
2956 shader_cleanup(object);
2957 HeapFree(GetProcessHeap(), 0, object);
2960 ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
2962 ULONG refcount = InterlockedDecrement(&shader->ref);
2964 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
2966 if (!refcount)
2968 shader->parent_ops->wined3d_object_destroyed(shader->parent);
2969 wined3d_cs_destroy_object(shader->device->cs, wined3d_shader_destroy_object, shader);
2972 return refcount;
2975 void * CDECL wined3d_shader_get_parent(const struct wined3d_shader *shader)
2977 TRACE("shader %p.\n", shader);
2979 return shader->parent;
2982 HRESULT CDECL wined3d_shader_get_byte_code(const struct wined3d_shader *shader,
2983 void *byte_code, UINT *byte_code_size)
2985 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader, byte_code, byte_code_size);
2987 if (!byte_code)
2989 *byte_code_size = shader->functionLength;
2990 return WINED3D_OK;
2993 if (*byte_code_size < shader->functionLength)
2995 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
2996 * than the required size we should write the required size and
2997 * return D3DERR_MOREDATA. That's not actually true. */
2998 return WINED3DERR_INVALIDCALL;
3001 memcpy(byte_code, shader->function, shader->functionLength);
3003 return WINED3D_OK;
3006 /* Set local constants for d3d8 shaders. */
3007 HRESULT CDECL wined3d_shader_set_local_constants_float(struct wined3d_shader *shader,
3008 UINT start_idx, const float *src_data, UINT count)
3010 UINT end_idx = start_idx + count;
3011 UINT i;
3013 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader, start_idx, src_data, count);
3015 if (end_idx > shader->limits->constant_float)
3017 WARN("end_idx %u > float constants limit %u.\n",
3018 end_idx, shader->limits->constant_float);
3019 end_idx = shader->limits->constant_float;
3022 for (i = start_idx; i < end_idx; ++i)
3024 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
3025 float *value;
3026 if (!lconst)
3027 return E_OUTOFMEMORY;
3029 lconst->idx = i;
3030 value = (float *)lconst->value;
3031 memcpy(value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
3032 list_add_head(&shader->constantsF, &lconst->entry);
3034 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
3035 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
3037 shader->lconst_inf_or_nan = TRUE;
3041 return WINED3D_OK;
3044 void find_vs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3045 WORD swizzle_map, struct vs_compile_args *args, const struct wined3d_d3d_info *d3d_info)
3047 args->fog_src = state->render_states[WINED3D_RS_FOGTABLEMODE]
3048 == WINED3D_FOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
3049 args->clip_enabled = state->render_states[WINED3D_RS_CLIPPING]
3050 && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
3051 args->point_size = state->gl_primitive_type == GL_POINTS;
3052 args->per_vertex_point_size = shader->reg_maps.point_size;
3053 args->next_shader_type = state->shader[WINED3D_SHADER_TYPE_HULL] ? WINED3D_SHADER_TYPE_HULL
3054 : state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ? WINED3D_SHADER_TYPE_GEOMETRY
3055 : WINED3D_SHADER_TYPE_PIXEL;
3056 if (shader->reg_maps.shader_version.major >= 4)
3057 args->next_shader_input_count = state->shader[WINED3D_SHADER_TYPE_HULL]
3058 ? state->shader[WINED3D_SHADER_TYPE_HULL]->limits->packed_input
3059 : state->shader[WINED3D_SHADER_TYPE_GEOMETRY]
3060 ? state->shader[WINED3D_SHADER_TYPE_GEOMETRY]->limits->packed_input
3061 : state->shader[WINED3D_SHADER_TYPE_PIXEL]
3062 ? state->shader[WINED3D_SHADER_TYPE_PIXEL]->limits->packed_input : 0;
3063 else
3064 args->next_shader_input_count = 0;
3065 args->swizzle_map = swizzle_map;
3066 if (d3d_info->emulated_flatshading)
3067 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
3068 else
3069 args->flatshading = 0;
3072 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
3074 if (usage_idx1 != usage_idx2)
3075 return FALSE;
3076 if (usage1 == usage2)
3077 return TRUE;
3078 if (usage1 == WINED3D_DECL_USAGE_POSITION && usage2 == WINED3D_DECL_USAGE_POSITIONT)
3079 return TRUE;
3080 if (usage2 == WINED3D_DECL_USAGE_POSITION && usage1 == WINED3D_DECL_USAGE_POSITIONT)
3081 return TRUE;
3083 return FALSE;
3086 BOOL vshader_get_input(const struct wined3d_shader *shader,
3087 BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
3089 WORD map = shader->reg_maps.input_registers;
3090 unsigned int i;
3092 for (i = 0; map; map >>= 1, ++i)
3094 if (!(map & 1)) continue;
3096 if (match_usage(shader->u.vs.attributes[i].usage,
3097 shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
3099 *regnum = i;
3100 return TRUE;
3103 return FALSE;
3106 static HRESULT shader_signature_copy(struct wined3d_shader_signature *dst,
3107 const struct wined3d_shader_signature *src, char **signature_strings)
3109 struct wined3d_shader_signature_element *e;
3110 unsigned int i;
3111 SIZE_T len;
3112 char *ptr;
3114 if (!src->element_count)
3115 return WINED3D_OK;
3117 ptr = *signature_strings;
3119 dst->element_count = src->element_count;
3120 if (!(dst->elements = wined3d_calloc(dst->element_count, sizeof(*dst->elements))))
3121 return E_OUTOFMEMORY;
3123 for (i = 0; i < src->element_count; ++i)
3125 e = &src->elements[i];
3126 dst->elements[i] = *e;
3128 len = strlen(e->semantic_name);
3129 memcpy(ptr, e->semantic_name, len + 1);
3130 dst->elements[i].semantic_name = ptr;
3131 ptr += len + 1;
3134 *signature_strings = ptr;
3136 return WINED3D_OK;
3139 static HRESULT shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3140 const struct wined3d_shader_desc *desc, DWORD float_const_count, enum wined3d_shader_type type,
3141 void *parent, const struct wined3d_parent_ops *parent_ops)
3143 struct wined3d_shader_signature_element *e;
3144 SIZE_T total, len;
3145 unsigned int i;
3146 HRESULT hr;
3147 char *ptr;
3149 if (!desc->byte_code)
3150 return WINED3DERR_INVALIDCALL;
3152 shader->ref = 1;
3153 shader->device = device;
3154 shader->parent = parent;
3155 shader->parent_ops = parent_ops;
3157 total = 0;
3158 for (i = 0; i < desc->input_signature.element_count; ++i)
3160 e = &desc->input_signature.elements[i];
3161 len = strlen(e->semantic_name);
3162 if (len >= ~(SIZE_T)0 - total)
3163 return E_OUTOFMEMORY;
3165 total += len + 1;
3167 for (i = 0; i < desc->output_signature.element_count; ++i)
3169 e = &desc->output_signature.elements[i];
3170 len = strlen(e->semantic_name);
3171 if (len >= ~(SIZE_T)0 - total)
3172 return E_OUTOFMEMORY;
3174 total += len + 1;
3176 if (total && !(shader->signature_strings = HeapAlloc(GetProcessHeap(), 0, total)))
3177 return E_OUTOFMEMORY;
3178 ptr = shader->signature_strings;
3180 if (FAILED(hr = shader_signature_copy(&shader->input_signature, &desc->input_signature, &ptr)))
3182 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
3183 return hr;
3185 if (FAILED(hr = shader_signature_copy(&shader->output_signature, &desc->output_signature, &ptr)))
3187 HeapFree(GetProcessHeap(), 0, shader->input_signature.elements);
3188 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
3189 return hr;
3192 list_init(&shader->linked_programs);
3193 list_add_head(&device->shaders, &shader->shader_list_entry);
3195 if (FAILED(hr = shader_set_function(shader, desc->byte_code,
3196 desc->byte_code_size, float_const_count, type, desc->max_version)))
3198 WARN("Failed to set function, hr %#x.\n", hr);
3199 shader_cleanup(shader);
3202 shader->load_local_constsF = shader->lconst_inf_or_nan;
3204 return hr;
3207 static HRESULT vertex_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3208 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3210 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3211 unsigned int i;
3212 HRESULT hr;
3214 if (FAILED(hr = shader_init(shader, device, desc, device->adapter->d3d_info.limits.vs_uniform_count,
3215 WINED3D_SHADER_TYPE_VERTEX, parent, parent_ops)))
3216 return hr;
3218 for (i = 0; i < shader->input_signature.element_count; ++i)
3220 const struct wined3d_shader_signature_element *input = &shader->input_signature.elements[i];
3222 if (!(reg_maps->input_registers & (1u << input->register_idx)) || !input->semantic_name)
3223 continue;
3225 shader->u.vs.attributes[input->register_idx].usage =
3226 shader_usage_from_semantic_name(input->semantic_name);
3227 shader->u.vs.attributes[input->register_idx].usage_idx = input->semantic_idx;
3230 if (reg_maps->usesrelconstF && !list_empty(&shader->constantsF))
3231 shader->load_local_constsF = TRUE;
3233 return WINED3D_OK;
3236 static HRESULT domain_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3237 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3239 return shader_init(shader, device, desc, 0, WINED3D_SHADER_TYPE_DOMAIN, parent, parent_ops);
3242 static HRESULT hull_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3243 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3245 return shader_init(shader, device, desc, 0, WINED3D_SHADER_TYPE_HULL, parent, parent_ops);
3248 static HRESULT geometry_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3249 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3251 return shader_init(shader, device, desc, 0, WINED3D_SHADER_TYPE_GEOMETRY, parent, parent_ops);
3254 void find_gs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3255 struct gs_compile_args *args)
3257 args->ps_input_count = state->shader[WINED3D_SHADER_TYPE_PIXEL]
3258 ? state->shader[WINED3D_SHADER_TYPE_PIXEL]->limits->packed_input : 0;
3261 void find_ps_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
3262 BOOL position_transformed, struct ps_compile_args *args, const struct wined3d_context *context)
3264 const struct wined3d_gl_info *gl_info = context->gl_info;
3265 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3266 const struct wined3d_texture *texture;
3267 UINT i;
3269 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
3270 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, state->fb))
3272 static unsigned int warned = 0;
3274 args->srgb_correction = 1;
3275 if (state->render_states[WINED3D_RS_ALPHABLENDENABLE] && !warned++)
3276 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
3277 "support, expect rendering artifacts.\n");
3280 if (shader->reg_maps.shader_version.major == 1
3281 && shader->reg_maps.shader_version.minor <= 3)
3283 for (i = 0; i < shader->limits->sampler; ++i)
3285 DWORD flags = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
3287 if (flags & WINED3D_TTFF_PROJECTED)
3289 DWORD tex_transform = flags & ~WINED3D_TTFF_PROJECTED;
3291 if (!state->shader[WINED3D_SHADER_TYPE_VERTEX])
3293 enum wined3d_shader_resource_type resource_type = shader->reg_maps.resource_info[i].type;
3294 unsigned int j;
3295 unsigned int index = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
3296 DWORD max_valid = WINED3D_TTFF_COUNT4;
3298 for (j = 0; j < state->vertex_declaration->element_count; ++j)
3300 struct wined3d_vertex_declaration_element *element =
3301 &state->vertex_declaration->elements[j];
3303 if (element->usage == WINED3D_DECL_USAGE_TEXCOORD
3304 && element->usage_idx == index)
3306 max_valid = element->format->component_count;
3307 break;
3310 if (!tex_transform || tex_transform > max_valid)
3312 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
3313 tex_transform, max_valid);
3314 tex_transform = max_valid;
3316 if ((resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1D && tex_transform > WINED3D_TTFF_COUNT1)
3317 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D
3318 && tex_transform > WINED3D_TTFF_COUNT2)
3319 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_3D
3320 && tex_transform > WINED3D_TTFF_COUNT3))
3321 tex_transform |= WINED3D_PSARGS_PROJECTED;
3322 else
3324 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
3325 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
3326 i, tex_transform, resource_type);
3329 else
3330 tex_transform = WINED3D_TTFF_COUNT4 | WINED3D_PSARGS_PROJECTED;
3332 args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
3336 if (shader->reg_maps.shader_version.major == 1
3337 && shader->reg_maps.shader_version.minor <= 4)
3339 for (i = 0; i < shader->limits->sampler; ++i)
3341 const struct wined3d_texture *texture = state->textures[i];
3343 if (!shader->reg_maps.resource_info[i].type)
3344 continue;
3346 /* Treat unbound textures as 2D. The dummy texture will provide
3347 * the proper sample value. The tex_types bitmap defaults to
3348 * 2D because of the memset. */
3349 if (!texture)
3350 continue;
3352 switch (texture->target)
3354 /* RECT textures are distinguished from 2D textures via np2_fixup */
3355 case GL_TEXTURE_RECTANGLE_ARB:
3356 case GL_TEXTURE_2D:
3357 break;
3359 case GL_TEXTURE_3D:
3360 args->tex_types |= WINED3D_SHADER_TEX_3D << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
3361 break;
3363 case GL_TEXTURE_CUBE_MAP_ARB:
3364 args->tex_types |= WINED3D_SHADER_TEX_CUBE << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
3365 break;
3370 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
3372 if (!shader->reg_maps.resource_info[i].type)
3373 continue;
3375 texture = state->textures[i];
3376 if (!texture)
3378 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
3379 continue;
3381 if (can_use_texture_swizzle(gl_info, texture->resource.format))
3382 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
3383 else
3384 args->color_fixup[i] = texture->resource.format->color_fixup;
3386 if (texture->resource.format_flags & WINED3DFMT_FLAG_SHADOW)
3387 args->shadow |= 1u << i;
3389 /* Flag samplers that need NP2 texcoord fixup. */
3390 if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
3391 args->np2_fixup |= (1u << i);
3394 /* In SM4+ we use dcl_sampler in order to determine if we should use shadow sampler. */
3395 if (shader->reg_maps.shader_version.major >= 4)
3396 args->shadow = 0;
3398 if (shader->reg_maps.shader_version.major >= 3)
3400 if (position_transformed)
3401 args->vp_mode = pretransformed;
3402 else if (use_vs(state))
3403 args->vp_mode = vertexshader;
3404 else
3405 args->vp_mode = fixedfunction;
3406 args->fog = WINED3D_FFP_PS_FOG_OFF;
3408 else
3410 args->vp_mode = vertexshader;
3411 if (state->render_states[WINED3D_RS_FOGENABLE])
3413 switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
3415 case WINED3D_FOG_NONE:
3416 if (position_transformed || use_vs(state))
3418 args->fog = WINED3D_FFP_PS_FOG_LINEAR;
3419 break;
3422 switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
3424 case WINED3D_FOG_NONE: /* Fall through. */
3425 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
3426 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
3427 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
3429 break;
3431 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
3432 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
3433 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
3436 else
3438 args->fog = WINED3D_FFP_PS_FOG_OFF;
3442 if (context->d3d_info->limits.varying_count < wined3d_max_compat_varyings(context->gl_info))
3444 const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3446 args->texcoords_initialized = 0;
3447 for (i = 0; i < MAX_TEXTURES; ++i)
3449 if (vs)
3451 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.output_registers & (1u << i))
3452 args->texcoords_initialized |= 1u << i;
3454 else
3456 const struct wined3d_stream_info *si = &context->stream_info;
3457 unsigned int coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
3459 if ((state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX] >> WINED3D_FFP_TCI_SHIFT)
3460 & WINED3D_FFP_TCI_MASK
3461 || (coord_idx < MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))))
3462 args->texcoords_initialized |= 1u << i;
3466 else
3468 args->texcoords_initialized = (1u << MAX_TEXTURES) - 1;
3471 args->pointsprite = state->render_states[WINED3D_RS_POINTSPRITEENABLE]
3472 && state->gl_primitive_type == GL_POINTS;
3474 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3475 args->alpha_test_func = WINED3D_CMP_ALWAYS - 1;
3476 else
3477 args->alpha_test_func = (state->render_states[WINED3D_RS_ALPHATESTENABLE]
3478 ? wined3d_sanitize_cmp_func(state->render_states[WINED3D_RS_ALPHAFUNC])
3479 : WINED3D_CMP_ALWAYS) - 1;
3481 if (d3d_info->emulated_flatshading)
3482 args->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
3484 args->render_offscreen = shader->reg_maps.vpos && gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS]
3485 ? context->render_offscreen : 0;
3488 static HRESULT pixel_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3489 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3491 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3492 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
3493 HRESULT hr;
3495 if (FAILED(hr = shader_init(shader, device, desc, device->adapter->d3d_info.limits.ps_uniform_count,
3496 WINED3D_SHADER_TYPE_PIXEL, parent, parent_ops)))
3497 return hr;
3499 for (i = 0; i < MAX_REG_INPUT; ++i)
3501 if (shader->u.ps.input_reg_used[i])
3503 ++num_regs_used;
3504 highest_reg_used = i;
3508 /* Don't do any register mapping magic if it is not needed, or if we can't
3509 * achieve anything anyway */
3510 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
3511 || num_regs_used > (gl_info->limits.glsl_varyings / 4)
3512 || shader->reg_maps.shader_version.major >= 4)
3514 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
3516 /* This happens with relative addressing. The input mapper function
3517 * warns about this if the higher registers are declared too, so
3518 * don't write a FIXME here */
3519 WARN("More varying registers used than supported\n");
3522 for (i = 0; i < MAX_REG_INPUT; ++i)
3524 shader->u.ps.input_reg_map[i] = i;
3527 shader->u.ps.declared_in_count = highest_reg_used + 1;
3529 else
3531 shader->u.ps.declared_in_count = 0;
3532 for (i = 0; i < MAX_REG_INPUT; ++i)
3534 if (shader->u.ps.input_reg_used[i])
3535 shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
3536 else shader->u.ps.input_reg_map[i] = ~0U;
3540 return WINED3D_OK;
3543 void pixelshader_update_resource_types(struct wined3d_shader *shader, WORD tex_types)
3545 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3546 struct wined3d_shader_resource_info *resource_info = reg_maps->resource_info;
3547 unsigned int i;
3549 if (reg_maps->shader_version.major != 1) return;
3551 for (i = 0; i < shader->limits->sampler; ++i)
3553 /* We don't sample from this sampler. */
3554 if (!resource_info[i].type)
3555 continue;
3557 switch ((tex_types >> i * WINED3D_PSARGS_TEXTYPE_SHIFT) & WINED3D_PSARGS_TEXTYPE_MASK)
3559 case WINED3D_SHADER_TEX_2D:
3560 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3561 break;
3563 case WINED3D_SHADER_TEX_3D:
3564 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_3D;
3565 break;
3567 case WINED3D_SHADER_TEX_CUBE:
3568 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_CUBE;
3569 break;
3574 static HRESULT compute_shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
3575 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
3577 return shader_init(shader, device, desc, 0, WINED3D_SHADER_TYPE_COMPUTE, parent, parent_ops);
3580 HRESULT CDECL wined3d_shader_create_cs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3581 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3583 struct wined3d_shader *object;
3584 HRESULT hr;
3586 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3587 device, desc, parent, parent_ops, shader);
3589 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
3590 return E_OUTOFMEMORY;
3592 if (FAILED(hr = compute_shader_init(object, device, desc, parent, parent_ops)))
3594 WARN("Failed to initialize compute shader, hr %#x.\n", hr);
3595 HeapFree(GetProcessHeap(), 0, object);
3596 return hr;
3599 TRACE("Created compute shader %p.\n", object);
3600 *shader = object;
3602 return WINED3D_OK;
3605 HRESULT CDECL wined3d_shader_create_ds(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3606 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3608 struct wined3d_shader *object;
3609 HRESULT hr;
3611 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3612 device, desc, parent, parent_ops, shader);
3614 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
3615 return E_OUTOFMEMORY;
3617 if (FAILED(hr = domain_shader_init(object, device, desc, parent, parent_ops)))
3619 WARN("Failed to initialize domain shader, hr %#x.\n", hr);
3620 HeapFree(GetProcessHeap(), 0, object);
3621 return hr;
3624 TRACE("Created domain shader %p.\n", object);
3625 *shader = object;
3627 return WINED3D_OK;
3630 HRESULT CDECL wined3d_shader_create_gs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3631 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3633 struct wined3d_shader *object;
3634 HRESULT hr;
3636 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3637 device, desc, parent, parent_ops, shader);
3639 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3640 if (!object)
3641 return E_OUTOFMEMORY;
3643 if (FAILED(hr = geometry_shader_init(object, device, desc, parent, parent_ops)))
3645 WARN("Failed to initialize geometry shader, hr %#x.\n", hr);
3646 HeapFree(GetProcessHeap(), 0, object);
3647 return hr;
3650 TRACE("Created geometry shader %p.\n", object);
3651 *shader = object;
3653 return WINED3D_OK;
3656 HRESULT CDECL wined3d_shader_create_hs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3657 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3659 struct wined3d_shader *object;
3660 HRESULT hr;
3662 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3663 device, desc, parent, parent_ops, shader);
3665 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
3666 return E_OUTOFMEMORY;
3668 if (FAILED(hr = hull_shader_init(object, device, desc, parent, parent_ops)))
3670 WARN("Failed to initialize hull shader, hr %#x.\n", hr);
3671 HeapFree(GetProcessHeap(), 0, object);
3672 return hr;
3675 TRACE("Created hull shader %p.\n", object);
3676 *shader = object;
3678 return WINED3D_OK;
3681 HRESULT CDECL wined3d_shader_create_ps(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3682 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3684 struct wined3d_shader *object;
3685 HRESULT hr;
3687 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3688 device, desc, parent, parent_ops, shader);
3690 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3691 if (!object)
3692 return E_OUTOFMEMORY;
3694 if (FAILED(hr = pixel_shader_init(object, device, desc, parent, parent_ops)))
3696 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3697 HeapFree(GetProcessHeap(), 0, object);
3698 return hr;
3701 TRACE("Created pixel shader %p.\n", object);
3702 *shader = object;
3704 return WINED3D_OK;
3707 HRESULT CDECL wined3d_shader_create_vs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
3708 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
3710 struct wined3d_shader *object;
3711 HRESULT hr;
3713 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3714 device, desc, parent, parent_ops, shader);
3716 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3717 if (!object)
3718 return E_OUTOFMEMORY;
3720 if (FAILED(hr = vertex_shader_init(object, device, desc, parent, parent_ops)))
3722 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
3723 HeapFree(GetProcessHeap(), 0, object);
3724 return hr;
3727 TRACE("Created vertex shader %p.\n", object);
3728 *shader = object;
3730 return WINED3D_OK;