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
26 #include "wine/port.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
};
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_CRS */ "crs",
69 /* WINED3DSIH_CUT */ "cut",
70 /* WINED3DSIH_CUT_STREAM */ "cut_stream",
71 /* WINED3DSIH_DCL */ "dcl",
72 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
73 /* WINED3DSIH_DCL_FUNCTION_BODY */ "dcl_function_body",
74 /* WINED3DSIH_DCL_FUNCTION_TABLE */ "dcl_function_table",
75 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ "dcl_globalFlags",
76 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ "dcl_hs_fork_phase_instance_count",
77 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ "dcl_hs_max_tessfactor",
78 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ "dcl_immediateConstantBuffer",
79 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ "dcl_indexableTemp",
80 /* WINED3DSIH_DCL_INPUT */ "dcl_input",
81 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ "dcl_input_control_point_count",
82 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
83 /* WINED3DSIH_DCL_INPUT_PS */ "dcl_input_ps",
84 /* WINED3DSIH_DCL_INPUT_PS_SGV */ "dcl_input_ps_sgv",
85 /* WINED3DSIH_DCL_INPUT_PS_SIV */ "dcl_input_ps_siv",
86 /* WINED3DSIH_DCL_INPUT_SGV */ "dcl_input_sgv",
87 /* WINED3DSIH_DCL_INPUT_SIV */ "dcl_input_siv",
88 /* WINED3DSIH_DCL_INTERFACE */ "dcl_interface",
89 /* WINED3DSIH_DCL_OUTPUT */ "dcl_output",
90 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ "dcl_output_control_point_count",
91 /* WINED3DSIH_DCL_OUTPUT_SIV */ "dcl_output_siv",
92 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
93 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ "dcl_resource_structured",
94 /* WINED3DSIH_DCL_SAMPLER */ "dcl_sampler",
95 /* WINED3DSIH_DCL_STREAM */ "dcl_stream",
96 /* WINED3DSIH_DCL_TEMPS */ "dcl_temps",
97 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ "dcl_tessellator_domain",
98 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ "dcl_tessellator_output_primitive",
99 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ "dcl_tessellator_partitioning",
100 /* WINED3DSIH_DCL_TGSM_RAW */ "dcl_tgsm_raw",
101 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ "dcl_tgsm_structured",
102 /* WINED3DSIH_DCL_THREAD_GROUP */ "dcl_thread_group",
103 /* WINED3DSIH_DCL_UAV_RAW */ "dcl_uav_raw",
104 /* WINED3DSIH_DCL_UAV_STRUCTURED */ "dcl_uav_structured",
105 /* WINED3DSIH_DCL_UAV_TYPED */ "dcl_uav_typed",
106 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
107 /* WINED3DSIH_DEF */ "def",
108 /* WINED3DSIH_DEFAULT */ "default",
109 /* WINED3DSIH_DEFB */ "defb",
110 /* WINED3DSIH_DEFI */ "defi",
111 /* WINED3DSIH_DIV */ "div",
112 /* WINED3DSIH_DP2 */ "dp2",
113 /* WINED3DSIH_DP2ADD */ "dp2add",
114 /* WINED3DSIH_DP3 */ "dp3",
115 /* WINED3DSIH_DP4 */ "dp4",
116 /* WINED3DSIH_DST */ "dst",
117 /* WINED3DSIH_DSX */ "dsx",
118 /* WINED3DSIH_DSX_COARSE */ "deriv_rtx_coarse",
119 /* WINED3DSIH_DSX_FINE */ "deriv_rtx_fine",
120 /* WINED3DSIH_DSY */ "dsy",
121 /* WINED3DSIH_DSY_COARSE */ "deriv_rty_coarse",
122 /* WINED3DSIH_DSY_FINE */ "deriv_rty_fine",
123 /* WINED3DSIH_ELSE */ "else",
124 /* WINED3DSIH_EMIT */ "emit",
125 /* WINED3DSIH_EMIT_STREAM */ "emit_stream",
126 /* WINED3DSIH_ENDIF */ "endif",
127 /* WINED3DSIH_ENDLOOP */ "endloop",
128 /* WINED3DSIH_ENDREP */ "endrep",
129 /* WINED3DSIH_ENDSWITCH */ "endswitch",
130 /* WINED3DSIH_EQ */ "eq",
131 /* WINED3DSIH_EXP */ "exp",
132 /* WINED3DSIH_EXPP */ "expp",
133 /* WINED3DSIH_FCALL */ "fcall",
134 /* WINED3DSIH_FRC */ "frc",
135 /* WINED3DSIH_FTOI */ "ftoi",
136 /* WINED3DSIH_FTOU */ "ftou",
137 /* WINED3DSIH_GATHER4 */ "gather4",
138 /* WINED3DSIH_GATHER4_C */ "gather4_c",
139 /* WINED3DSIH_GE */ "ge",
140 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ "hs_control_point_phase",
141 /* WINED3DSIH_HS_DECLS */ "hs_decls",
142 /* WINED3DSIH_HS_FORK_PHASE */ "hs_fork_phase",
143 /* WINED3DSIH_HS_JOIN_PHASE */ "hs_join_phase",
144 /* WINED3DSIH_IADD */ "iadd",
145 /* WINED3DSIH_IEQ */ "ieq",
146 /* WINED3DSIH_IF */ "if",
147 /* WINED3DSIH_IFC */ "ifc",
148 /* WINED3DSIH_IGE */ "ige",
149 /* WINED3DSIH_ILT */ "ilt",
150 /* WINED3DSIH_IMAD */ "imad",
151 /* WINED3DSIH_IMAX */ "imax",
152 /* WINED3DSIH_IMIN */ "imin",
153 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ "imm_atomic_alloc",
154 /* WINED3DSIH_IMM_ATOMIC_AND */ "imm_atomic_and",
155 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ "imm_atomic_cmp_exch",
156 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ "imm_atomic_consume",
157 /* WINED3DSIH_IMM_ATOMIC_EXCH */ "imm_atomic_exch",
158 /* WINED3DSIH_IMM_ATOMIC_OR */ "imm_atomic_or",
159 /* WINED3DSIH_IMM_ATOMIC_UMAX */ "imm_atomic_umax",
160 /* WINED3DSIH_IMM_ATOMIC_UMIN */ "imm_atomic_umin",
161 /* WINED3DSIH_IMM_ATOMIC_XOR */ "imm_atomic_xor",
162 /* WINED3DSIH_IMUL */ "imul",
163 /* WINED3DSIH_INE */ "ine",
164 /* WINED3DSIH_INEG */ "ineg",
165 /* WINED3DSIH_ISHL */ "ishl",
166 /* WINED3DSIH_ISHR */ "ishr",
167 /* WINED3DSIH_ITOF */ "itof",
168 /* WINED3DSIH_LABEL */ "label",
169 /* WINED3DSIH_LD */ "ld",
170 /* WINED3DSIH_LD2DMS */ "ld2dms",
171 /* WINED3DSIH_LD_RAW */ "ld_raw",
172 /* WINED3DSIH_LD_STRUCTURED */ "ld_structured",
173 /* WINED3DSIH_LD_UAV_TYPED */ "ld_uav_typed",
174 /* WINED3DSIH_LIT */ "lit",
175 /* WINED3DSIH_LOD */ "lod",
176 /* WINED3DSIH_LOG */ "log",
177 /* WINED3DSIH_LOGP */ "logp",
178 /* WINED3DSIH_LOOP */ "loop",
179 /* WINED3DSIH_LRP */ "lrp",
180 /* WINED3DSIH_LT */ "lt",
181 /* WINED3DSIH_M3x2 */ "m3x2",
182 /* WINED3DSIH_M3x3 */ "m3x3",
183 /* WINED3DSIH_M3x4 */ "m3x4",
184 /* WINED3DSIH_M4x3 */ "m4x3",
185 /* WINED3DSIH_M4x4 */ "m4x4",
186 /* WINED3DSIH_MAD */ "mad",
187 /* WINED3DSIH_MAX */ "max",
188 /* WINED3DSIH_MIN */ "min",
189 /* WINED3DSIH_MOV */ "mov",
190 /* WINED3DSIH_MOVA */ "mova",
191 /* WINED3DSIH_MOVC */ "movc",
192 /* WINED3DSIH_MUL */ "mul",
193 /* WINED3DSIH_NE */ "ne",
194 /* WINED3DSIH_NOP */ "nop",
195 /* WINED3DSIH_NOT */ "not",
196 /* WINED3DSIH_NRM */ "nrm",
197 /* WINED3DSIH_OR */ "or",
198 /* WINED3DSIH_PHASE */ "phase",
199 /* WINED3DSIH_POW */ "pow",
200 /* WINED3DSIH_RCP */ "rcp",
201 /* WINED3DSIH_REP */ "rep",
202 /* WINED3DSIH_RESINFO */ "resinfo",
203 /* WINED3DSIH_RET */ "ret",
204 /* WINED3DSIH_ROUND_NE */ "round_ne",
205 /* WINED3DSIH_ROUND_NI */ "round_ni",
206 /* WINED3DSIH_ROUND_PI */ "round_pi",
207 /* WINED3DSIH_ROUND_Z */ "round_z",
208 /* WINED3DSIH_RSQ */ "rsq",
209 /* WINED3DSIH_SAMPLE */ "sample",
210 /* WINED3DSIH_SAMPLE_B */ "sample_b",
211 /* WINED3DSIH_SAMPLE_C */ "sample_c",
212 /* WINED3DSIH_SAMPLE_C_LZ */ "sample_c_lz",
213 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
214 /* WINED3DSIH_SAMPLE_INFO */ "sample_info",
215 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
216 /* WINED3DSIH_SAMPLE_POS */ "sample_pos",
217 /* WINED3DSIH_SETP */ "setp",
218 /* WINED3DSIH_SGE */ "sge",
219 /* WINED3DSIH_SGN */ "sgn",
220 /* WINED3DSIH_SINCOS */ "sincos",
221 /* WINED3DSIH_SLT */ "slt",
222 /* WINED3DSIH_SQRT */ "sqrt",
223 /* WINED3DSIH_STORE_RAW */ "store_raw",
224 /* WINED3DSIH_STORE_STRUCTURED */ "store_structured",
225 /* WINED3DSIH_STORE_UAV_TYPED */ "store_uav_typed",
226 /* WINED3DSIH_SUB */ "sub",
227 /* WINED3DSIH_SWAPC */ "swapc",
228 /* WINED3DSIH_SWITCH */ "switch",
229 /* WINED3DSIH_SYNC */ "sync",
230 /* WINED3DSIH_TEX */ "texld",
231 /* WINED3DSIH_TEXBEM */ "texbem",
232 /* WINED3DSIH_TEXBEML */ "texbeml",
233 /* WINED3DSIH_TEXCOORD */ "texcrd",
234 /* WINED3DSIH_TEXDEPTH */ "texdepth",
235 /* WINED3DSIH_TEXDP3 */ "texdp3",
236 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
237 /* WINED3DSIH_TEXKILL */ "texkill",
238 /* WINED3DSIH_TEXLDD */ "texldd",
239 /* WINED3DSIH_TEXLDL */ "texldl",
240 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
241 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
242 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
243 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
244 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
245 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
246 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
247 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
248 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
249 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
250 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
251 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
252 /* WINED3DSIH_UBFE */ "ubfe",
253 /* WINED3DSIH_UDIV */ "udiv",
254 /* WINED3DSIH_UGE */ "uge",
255 /* WINED3DSIH_ULT */ "ult",
256 /* WINED3DSIH_UMAX */ "umax",
257 /* WINED3DSIH_UMIN */ "umin",
258 /* WINED3DSIH_USHR */ "ushr",
259 /* WINED3DSIH_UTOF */ "utof",
260 /* WINED3DSIH_XOR */ "xor",
263 static const char * const semantic_names
[] =
265 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
266 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
267 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
268 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
269 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
270 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
271 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
272 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
273 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
274 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
275 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
276 /* WINED3D_DECL_USAGE_FOG */ "FOG",
277 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
278 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
283 enum wined3d_sysval_semantic sysval_semantic
;
284 const char *sysval_name
;
286 sysval_semantic_names
[] =
288 {WINED3D_SV_POSITION
, "SV_Position"},
289 {WINED3D_SV_CLIP_DISTANCE
, "SV_ClipDistance"},
290 {WINED3D_SV_CULL_DISTANCE
, "SV_CullDistance"},
291 {WINED3D_SV_RENDER_TARGET_ARRAY_INDEX
, "SV_RenderTargetArrayIndex"},
292 {WINED3D_SV_VIEWPORT_ARRAY_INDEX
, "SV_ViewportArrayIndex"},
293 {WINED3D_SV_VERTEX_ID
, "SV_VertexID"},
294 {WINED3D_SV_INSTANCE_ID
, "SV_InstanceID"},
295 {WINED3D_SV_PRIMITIVE_ID
, "SV_PrimitiveID"},
296 {WINED3D_SV_IS_FRONT_FACE
, "SV_IsFrontFace"},
297 {WINED3D_SV_SAMPLE_INDEX
, "SV_SampleIndex"},
298 {WINED3D_SV_QUAD_U0_TESS_FACTOR
, "finalQuadUeq0EdgeTessFactor"},
299 {WINED3D_SV_QUAD_V0_TESS_FACTOR
, "finalQuadVeq0EdgeTessFactor"},
300 {WINED3D_SV_QUAD_U1_TESS_FACTOR
, "finalQuadUeq1EdgeTessFactor"},
301 {WINED3D_SV_QUAD_V1_TESS_FACTOR
, "finalQuadVeq1EdgeTessFactor"},
302 {WINED3D_SV_QUAD_U_INNER_TESS_FACTOR
, "finalQuadUInsideTessFactor"},
303 {WINED3D_SV_QUAD_V_INNER_TESS_FACTOR
, "finalQuadVInsideTessFactor"},
304 {WINED3D_SV_TRIANGLE_U_TESS_FACTOR
, "finalTriUeq0EdgeTessFactor"},
305 {WINED3D_SV_TRIANGLE_V_TESS_FACTOR
, "finalTriVeq0EdgeTessFactor"},
306 {WINED3D_SV_TRIANGLE_W_TESS_FACTOR
, "finalTriWeq0EdgeTessFactor"},
307 {WINED3D_SV_TRIANGLE_INNER_TESS_FACTOR
, "finalTriInsideTessFactor"},
308 {WINED3D_SV_LINE_DETAIL_TESS_FACTOR
, "finalLineDetailTessFactor"},
309 {WINED3D_SV_LINE_DENSITY_TESS_FACTOR
, "finalLineDensityTessFactor"},
312 static void shader_dump_src_param(struct wined3d_string_buffer
*buffer
,
313 const struct wined3d_shader_src_param
*param
, const struct wined3d_shader_version
*shader_version
);
315 const char *debug_d3dshaderinstructionhandler(enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx
)
317 if (handler_idx
>= ARRAY_SIZE(shader_opcode_names
))
318 return wine_dbg_sprintf("UNRECOGNIZED(%#x)", handler_idx
);
320 return shader_opcode_names
[handler_idx
];
323 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage
)
325 if (usage
>= ARRAY_SIZE(semantic_names
))
327 FIXME("Unrecognized usage %#x.\n", usage
);
328 return "UNRECOGNIZED";
331 return semantic_names
[usage
];
334 static enum wined3d_decl_usage
shader_usage_from_semantic_name(const char *name
)
338 for (i
= 0; i
< ARRAY_SIZE(semantic_names
); ++i
)
340 if (!strcmp(name
, semantic_names
[i
]))
347 static enum wined3d_sysval_semantic
shader_sysval_semantic_from_usage(enum wined3d_decl_usage usage
)
351 case WINED3D_DECL_USAGE_POSITION
:
352 return WINED3D_SV_POSITION
;
358 BOOL
shader_match_semantic(const char *semantic_name
, enum wined3d_decl_usage usage
)
360 return !strcmp(semantic_name
, shader_semantic_name_from_usage(usage
));
363 static void shader_signature_from_semantic(struct wined3d_shader_signature_element
*e
,
364 const struct wined3d_shader_semantic
*s
)
366 e
->semantic_name
= shader_semantic_name_from_usage(s
->usage
);
367 e
->semantic_idx
= s
->usage_idx
;
368 e
->sysval_semantic
= shader_sysval_semantic_from_usage(s
->usage
);
369 e
->component_type
= WINED3D_TYPE_FLOAT
;
370 e
->register_idx
= s
->reg
.reg
.idx
[0].offset
;
371 e
->mask
= s
->reg
.write_mask
;
374 static void shader_signature_from_usage(struct wined3d_shader_signature_element
*e
,
375 enum wined3d_decl_usage usage
, UINT usage_idx
, UINT reg_idx
, DWORD write_mask
)
377 e
->semantic_name
= shader_semantic_name_from_usage(usage
);
378 e
->semantic_idx
= usage_idx
;
379 e
->sysval_semantic
= shader_sysval_semantic_from_usage(usage
);
380 e
->component_type
= WINED3D_TYPE_FLOAT
;
381 e
->register_idx
= reg_idx
;
382 e
->mask
= write_mask
;
385 static const struct wined3d_shader_frontend
*shader_select_frontend(DWORD version_token
)
387 switch (version_token
>> 16)
391 return &sm1_shader_frontend
;
399 return &sm4_shader_frontend
;
402 FIXME("Unrecognised version token %#x.\n", version_token
);
407 void string_buffer_clear(struct wined3d_string_buffer
*buffer
)
409 buffer
->buffer
[0] = '\0';
410 buffer
->content_size
= 0;
413 BOOL
string_buffer_init(struct wined3d_string_buffer
*buffer
)
415 buffer
->buffer_size
= 32;
416 if (!(buffer
->buffer
= HeapAlloc(GetProcessHeap(), 0, buffer
->buffer_size
)))
418 ERR("Failed to allocate shader buffer memory.\n");
422 string_buffer_clear(buffer
);
426 void string_buffer_free(struct wined3d_string_buffer
*buffer
)
428 HeapFree(GetProcessHeap(), 0, buffer
->buffer
);
431 BOOL
string_buffer_resize(struct wined3d_string_buffer
*buffer
, int rc
)
434 unsigned int new_buffer_size
= buffer
->buffer_size
* 2;
436 while (rc
> 0 && (unsigned int)rc
>= new_buffer_size
- buffer
->content_size
)
437 new_buffer_size
*= 2;
438 if (!(new_buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
->buffer
, new_buffer_size
)))
440 ERR("Failed to grow buffer.\n");
441 buffer
->buffer
[buffer
->content_size
] = '\0';
444 buffer
->buffer
= new_buffer
;
445 buffer
->buffer_size
= new_buffer_size
;
449 int shader_vaddline(struct wined3d_string_buffer
*buffer
, const char *format
, va_list args
)
454 rem
= buffer
->buffer_size
- buffer
->content_size
;
455 rc
= vsnprintf(&buffer
->buffer
[buffer
->content_size
], rem
, format
, args
);
456 if (rc
< 0 /* C89 */ || (unsigned int)rc
>= rem
/* C99 */)
459 buffer
->content_size
+= rc
;
463 int shader_addline(struct wined3d_string_buffer
*buffer
, const char *format
, ...)
470 va_start(args
, format
);
471 ret
= shader_vaddline(buffer
, format
, args
);
475 if (!string_buffer_resize(buffer
, ret
))
480 struct wined3d_string_buffer
*string_buffer_get(struct wined3d_string_buffer_list
*list
)
482 struct wined3d_string_buffer
*buffer
;
484 if (list_empty(&list
->list
))
486 buffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer
));
487 if (!buffer
|| !string_buffer_init(buffer
))
489 ERR("Couldn't allocate buffer for temporary string.\n");
490 HeapFree(GetProcessHeap(), 0, buffer
);
496 buffer
= LIST_ENTRY(list_head(&list
->list
), struct wined3d_string_buffer
, entry
);
497 list_remove(&buffer
->entry
);
499 string_buffer_clear(buffer
);
503 static int string_buffer_vsprintf(struct wined3d_string_buffer
*buffer
, const char *format
, va_list args
)
507 string_buffer_clear(buffer
);
508 return shader_vaddline(buffer
, format
, args
);
511 void string_buffer_sprintf(struct wined3d_string_buffer
*buffer
, const char *format
, ...)
518 va_start(args
, format
);
519 ret
= string_buffer_vsprintf(buffer
, format
, args
);
523 if (!string_buffer_resize(buffer
, ret
))
528 void string_buffer_release(struct wined3d_string_buffer_list
*list
, struct wined3d_string_buffer
*buffer
)
532 list_add_head(&list
->list
, &buffer
->entry
);
535 void string_buffer_list_init(struct wined3d_string_buffer_list
*list
)
537 list_init(&list
->list
);
540 void string_buffer_list_cleanup(struct wined3d_string_buffer_list
*list
)
542 struct wined3d_string_buffer
*buffer
, *buffer_next
;
544 LIST_FOR_EACH_ENTRY_SAFE(buffer
, buffer_next
, &list
->list
, struct wined3d_string_buffer
, entry
)
546 string_buffer_free(buffer
);
547 HeapFree(GetProcessHeap(), 0, buffer
);
549 list_init(&list
->list
);
552 /* Convert floating point offset relative to a register file to an absolute
553 * offset for float constants. */
554 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type
, UINT register_idx
)
556 switch (register_type
)
558 case WINED3DSPR_CONST
: return register_idx
;
559 case WINED3DSPR_CONST2
: return 2048 + register_idx
;
560 case WINED3DSPR_CONST3
: return 4096 + register_idx
;
561 case WINED3DSPR_CONST4
: return 6144 + register_idx
;
563 FIXME("Unsupported register type: %u.\n", register_type
);
568 static void shader_delete_constant_list(struct list
*clist
)
570 struct wined3d_shader_lconst
*constant
, *constant_next
;
572 LIST_FOR_EACH_ENTRY_SAFE(constant
, constant_next
, clist
, struct wined3d_shader_lconst
, entry
)
573 HeapFree(GetProcessHeap(), 0, constant
);
577 static void shader_set_limits(struct wined3d_shader
*shader
)
579 static const struct limits_entry
581 unsigned int min_version
;
582 unsigned int max_version
;
583 struct wined3d_shader_limits limits
;
587 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
588 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
589 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
590 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
591 * even though they are capable of supporting much more (GL
592 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
593 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
595 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
596 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
597 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 0}},
602 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
603 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
607 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
608 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
612 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
613 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
614 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
619 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
620 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
621 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
622 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
623 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
624 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 10}},
625 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 32}},
630 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
631 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 0}},
633 const struct limits_entry
*limits_array
;
634 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
635 shader
->reg_maps
.shader_version
.minor
);
638 switch (shader
->reg_maps
.shader_version
.type
)
641 FIXME("Unexpected shader type %u found.\n", shader
->reg_maps
.shader_version
.type
);
643 case WINED3D_SHADER_TYPE_VERTEX
:
644 limits_array
= vs_limits
;
646 case WINED3D_SHADER_TYPE_HULL
:
647 limits_array
= hs_limits
;
649 case WINED3D_SHADER_TYPE_DOMAIN
:
650 limits_array
= ds_limits
;
652 case WINED3D_SHADER_TYPE_GEOMETRY
:
653 limits_array
= gs_limits
;
655 case WINED3D_SHADER_TYPE_PIXEL
:
656 limits_array
= ps_limits
;
658 case WINED3D_SHADER_TYPE_COMPUTE
:
659 limits_array
= cs_limits
;
663 while (limits_array
[i
].min_version
&& limits_array
[i
].min_version
<= shader_version
)
665 if (shader_version
<= limits_array
[i
].max_version
)
667 shader
->limits
= &limits_array
[i
].limits
;
674 FIXME("Unexpected shader version \"%u.%u\".\n",
675 shader
->reg_maps
.shader_version
.major
,
676 shader
->reg_maps
.shader_version
.minor
);
677 shader
->limits
= &limits_array
[max(0, i
- 1)].limits
;
681 static inline void set_bitmap_bit(DWORD
*bitmap
, DWORD bit
)
686 bitmap
[idx
] |= (1u << shift
);
689 static BOOL
shader_record_register_usage(struct wined3d_shader
*shader
, struct wined3d_shader_reg_maps
*reg_maps
,
690 const struct wined3d_shader_register
*reg
, enum wined3d_shader_type shader_type
, unsigned int constf_size
)
694 case WINED3DSPR_TEXTURE
: /* WINED3DSPR_ADDR */
695 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
696 reg_maps
->texcoord
|= 1u << reg
->idx
[0].offset
;
698 reg_maps
->address
|= 1u << reg
->idx
[0].offset
;
701 case WINED3DSPR_TEMP
:
702 reg_maps
->temporary
|= 1u << reg
->idx
[0].offset
;
705 case WINED3DSPR_INPUT
:
706 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
708 if (reg
->idx
[0].rel_addr
)
710 /* If relative addressing is used, we must assume that all registers
711 * are used. Even if it is a construct like v3[aL], we can't assume
712 * that v0, v1 and v2 aren't read because aL can be negative */
714 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
716 shader
->u
.ps
.input_reg_used
[i
] = TRUE
;
721 shader
->u
.ps
.input_reg_used
[reg
->idx
[0].offset
] = TRUE
;
725 reg_maps
->input_registers
|= 1u << reg
->idx
[0].offset
;
728 case WINED3DSPR_RASTOUT
:
729 if (reg
->idx
[0].offset
== 1)
731 if (reg
->idx
[0].offset
== 2)
732 reg_maps
->point_size
= 1;
735 case WINED3DSPR_MISCTYPE
:
736 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
738 if (!reg
->idx
[0].offset
)
740 else if (reg
->idx
[0].offset
== 1)
741 reg_maps
->usesfacing
= 1;
745 case WINED3DSPR_CONST
:
746 if (reg
->idx
[0].rel_addr
)
748 if (reg
->idx
[0].offset
< reg_maps
->min_rel_offset
)
749 reg_maps
->min_rel_offset
= reg
->idx
[0].offset
;
750 if (reg
->idx
[0].offset
> reg_maps
->max_rel_offset
)
751 reg_maps
->max_rel_offset
= reg
->idx
[0].offset
;
752 reg_maps
->usesrelconstF
= TRUE
;
756 if (reg
->idx
[0].offset
>= min(shader
->limits
->constant_float
, constf_size
))
758 WARN("Shader using float constant %u which is not supported.\n", reg
->idx
[0].offset
);
763 set_bitmap_bit(reg_maps
->constf
, reg
->idx
[0].offset
);
768 case WINED3DSPR_CONSTINT
:
769 if (reg
->idx
[0].offset
>= shader
->limits
->constant_int
)
771 WARN("Shader using integer constant %u which is not supported.\n", reg
->idx
[0].offset
);
776 reg_maps
->integer_constants
|= (1u << reg
->idx
[0].offset
);
780 case WINED3DSPR_CONSTBOOL
:
781 if (reg
->idx
[0].offset
>= shader
->limits
->constant_bool
)
783 WARN("Shader using bool constant %u which is not supported.\n", reg
->idx
[0].offset
);
788 reg_maps
->boolean_constants
|= (1u << reg
->idx
[0].offset
);
792 case WINED3DSPR_COLOROUT
:
793 reg_maps
->rt_mask
|= (1u << reg
->idx
[0].offset
);
797 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
798 reg
->type
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
804 static void shader_record_sample(struct wined3d_shader_reg_maps
*reg_maps
,
805 unsigned int resource_idx
, unsigned int sampler_idx
, unsigned int bind_idx
)
807 struct wined3d_shader_sampler_map_entry
*entries
, *entry
;
808 struct wined3d_shader_sampler_map
*map
;
811 map
= ®_maps
->sampler_map
;
812 entries
= map
->entries
;
813 for (i
= 0; i
< map
->count
; ++i
)
815 if (entries
[i
].resource_idx
== resource_idx
&& entries
[i
].sampler_idx
== sampler_idx
)
821 if (!(entries
= wined3d_calloc(4, sizeof(*entries
))))
823 ERR("Failed to allocate sampler map entries.\n");
827 map
->entries
= entries
;
829 else if (map
->count
== map
->size
)
831 size_t new_size
= map
->size
* 2;
833 if (sizeof(*entries
) * new_size
<= sizeof(*entries
) * map
->size
834 || !(entries
= HeapReAlloc(GetProcessHeap(), 0, entries
, sizeof(*entries
) * new_size
)))
836 ERR("Failed to resize sampler map entries.\n");
839 map
->size
= new_size
;
840 map
->entries
= entries
;
843 entry
= &entries
[map
->count
++];
844 entry
->resource_idx
= resource_idx
;
845 entry
->sampler_idx
= sampler_idx
;
846 entry
->bind_idx
= bind_idx
;
849 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr
, unsigned int param
)
853 case WINED3DSIH_M4x4
:
854 case WINED3DSIH_M3x4
:
855 return param
== 1 ? 3 : 0;
857 case WINED3DSIH_M4x3
:
858 case WINED3DSIH_M3x3
:
859 return param
== 1 ? 2 : 0;
861 case WINED3DSIH_M3x2
:
862 return param
== 1 ? 1 : 0;
869 /* Note that this does not count the loop register as an address register. */
870 static HRESULT
shader_get_registers_used(struct wined3d_shader
*shader
, const struct wined3d_shader_frontend
*fe
,
871 struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_shader_signature
*input_signature
,
872 struct wined3d_shader_signature
*output_signature
, const DWORD
*byte_code
, DWORD constf_size
)
874 struct wined3d_shader_signature_element input_signature_elements
[max(MAX_ATTRIBS
, MAX_REG_INPUT
)];
875 struct wined3d_shader_signature_element output_signature_elements
[MAX_REG_OUTPUT
];
876 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
877 void *fe_data
= shader
->frontend_data
;
878 struct wined3d_shader_version shader_version
;
879 const DWORD
*ptr
= byte_code
;
882 memset(reg_maps
, 0, sizeof(*reg_maps
));
883 memset(input_signature_elements
, 0, sizeof(input_signature_elements
));
884 memset(output_signature_elements
, 0, sizeof(output_signature_elements
));
885 reg_maps
->min_rel_offset
= ~0U;
886 list_init(®_maps
->indexable_temps
);
888 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
889 reg_maps
->shader_version
= shader_version
;
891 shader_set_limits(shader
);
893 if (!(reg_maps
->constf
= wined3d_calloc(((min(shader
->limits
->constant_float
, constf_size
) + 31) / 32),
894 sizeof(*reg_maps
->constf
))))
896 ERR("Failed to allocate constant map memory.\n");
897 return E_OUTOFMEMORY
;
900 while (!fe
->shader_is_end(fe_data
, &ptr
))
902 struct wined3d_shader_instruction ins
;
905 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
907 /* Unhandled opcode, and its parameters. */
908 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
910 TRACE("Skipping unrecognized instruction.\n");
914 /* Handle declarations. */
915 if (ins
.handler_idx
== WINED3DSIH_DCL
916 || ins
.handler_idx
== WINED3DSIH_DCL_UAV_TYPED
)
918 struct wined3d_shader_semantic
*semantic
= &ins
.declaration
.semantic
;
919 unsigned int reg_idx
= semantic
->reg
.reg
.idx
[0].offset
;
921 switch (semantic
->reg
.reg
.type
)
923 /* Mark input registers used. */
924 case WINED3DSPR_INPUT
:
925 if (reg_idx
>= MAX_REG_INPUT
)
927 ERR("Invalid input register index %u.\n", reg_idx
);
930 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
&& shader_version
.major
== 3
931 && semantic
->usage
== WINED3D_DECL_USAGE_POSITION
&& !semantic
->usage_idx
)
932 return WINED3DERR_INVALIDCALL
;
933 reg_maps
->input_registers
|= 1u << reg_idx
;
934 shader_signature_from_semantic(&input_signature_elements
[reg_idx
], semantic
);
937 /* Vertex shader: mark 3.0 output registers used, save token. */
938 case WINED3DSPR_OUTPUT
:
939 if (reg_idx
>= MAX_REG_OUTPUT
)
941 ERR("Invalid output register index %u.\n", reg_idx
);
944 reg_maps
->output_registers
|= 1u << reg_idx
;
945 shader_signature_from_semantic(&output_signature_elements
[reg_idx
], semantic
);
946 if (semantic
->usage
== WINED3D_DECL_USAGE_FOG
)
948 if (semantic
->usage
== WINED3D_DECL_USAGE_PSIZE
)
949 reg_maps
->point_size
= 1;
952 case WINED3DSPR_SAMPLER
:
953 shader_record_sample(reg_maps
, reg_idx
, reg_idx
, reg_idx
);
954 case WINED3DSPR_RESOURCE
:
955 if (reg_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
957 ERR("Invalid resource index %u.\n", reg_idx
);
960 reg_maps
->resource_info
[reg_idx
].type
= semantic
->resource_type
;
961 reg_maps
->resource_info
[reg_idx
].data_type
= semantic
->resource_data_type
;
965 if (reg_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
967 ERR("Invalid UAV resource index %u.\n", reg_idx
);
970 reg_maps
->uav_resource_info
[reg_idx
].type
= semantic
->resource_type
;
971 reg_maps
->uav_resource_info
[reg_idx
].data_type
= semantic
->resource_data_type
;
975 TRACE("Not recording DCL register type %#x.\n", semantic
->reg
.reg
.type
);
979 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
981 struct wined3d_shader_register
*reg
= &ins
.declaration
.src
.reg
;
982 if (reg
->idx
[0].offset
>= WINED3D_MAX_CBS
)
983 ERR("Invalid CB index %u.\n", reg
->idx
[0].offset
);
985 reg_maps
->cb_sizes
[reg
->idx
[0].offset
] = reg
->idx
[1].offset
;
987 else if (ins
.handler_idx
== WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER
)
990 FIXME("Multiple immediate constant buffers.\n");
991 reg_maps
->icb
= ins
.declaration
.icb
;
993 else if (ins
.handler_idx
== WINED3DSIH_DCL_INDEXABLE_TEMP
)
995 struct wined3d_shader_indexable_temp
*reg
;
997 if (!(reg
= HeapAlloc(GetProcessHeap(), 0, sizeof(*reg
))))
998 return E_OUTOFMEMORY
;
1000 *reg
= ins
.declaration
.indexable_temp
;
1001 list_add_tail(®_maps
->indexable_temps
, ®
->entry
);
1003 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
)
1005 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1006 shader
->u
.gs
.input_type
= ins
.declaration
.primitive_type
;
1008 FIXME("Invalid instruction %#x for shader type %#x.\n",
1009 ins
.handler_idx
, shader_version
.type
);
1011 else if (ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
1013 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1014 shader
->u
.gs
.output_type
= ins
.declaration
.primitive_type
;
1016 FIXME("Invalid instruction %#x for shader type %#x.\n",
1017 ins
.handler_idx
, shader_version
.type
);
1019 else if (ins
.handler_idx
== WINED3DSIH_DCL_SAMPLER
)
1021 if (ins
.flags
& WINED3DSI_SAMPLER_COMPARISON_MODE
)
1022 reg_maps
->sampler_comparison_mode
|= (1u << ins
.declaration
.dst
.reg
.idx
[0].offset
);
1024 else if (ins
.handler_idx
== WINED3DSIH_DCL_TEMPS
)
1026 reg_maps
->temporary_count
= ins
.declaration
.count
;
1028 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
1030 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1031 shader
->u
.gs
.vertices_out
= ins
.declaration
.count
;
1033 FIXME("Invalid instruction %#x for shader type %#x.\n",
1034 ins
.handler_idx
, shader_version
.type
);
1036 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
1038 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
1040 if (!lconst
) return E_OUTOFMEMORY
;
1042 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
1043 memcpy(lconst
->value
, ins
.src
[0].reg
.u
.immconst_data
, 4 * sizeof(DWORD
));
1044 value
= (float *)lconst
->value
;
1046 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
1047 if (shader_version
.major
== 1 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1049 if (value
[0] < -1.0f
) value
[0] = -1.0f
;
1050 else if (value
[0] > 1.0f
) value
[0] = 1.0f
;
1051 if (value
[1] < -1.0f
) value
[1] = -1.0f
;
1052 else if (value
[1] > 1.0f
) value
[1] = 1.0f
;
1053 if (value
[2] < -1.0f
) value
[2] = -1.0f
;
1054 else if (value
[2] > 1.0f
) value
[2] = 1.0f
;
1055 if (value
[3] < -1.0f
) value
[3] = -1.0f
;
1056 else if (value
[3] > 1.0f
) value
[3] = 1.0f
;
1059 list_add_head(&shader
->constantsF
, &lconst
->entry
);
1061 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
1062 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
1064 shader
->lconst_inf_or_nan
= TRUE
;
1067 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
1069 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
1070 if (!lconst
) return E_OUTOFMEMORY
;
1072 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
1073 memcpy(lconst
->value
, ins
.src
[0].reg
.u
.immconst_data
, 4 * sizeof(DWORD
));
1075 list_add_head(&shader
->constantsI
, &lconst
->entry
);
1076 reg_maps
->local_int_consts
|= (1u << lconst
->idx
);
1078 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
1080 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
1081 if (!lconst
) return E_OUTOFMEMORY
;
1083 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
1084 memcpy(lconst
->value
, ins
.src
[0].reg
.u
.immconst_data
, sizeof(DWORD
));
1086 list_add_head(&shader
->constantsB
, &lconst
->entry
);
1087 reg_maps
->local_bool_consts
|= (1u << lconst
->idx
);
1089 /* For subroutine prototypes. */
1090 else if (ins
.handler_idx
== WINED3DSIH_LABEL
)
1092 reg_maps
->labels
|= 1u << ins
.src
[0].reg
.idx
[0].offset
;
1094 /* Set texture, address, temporary registers. */
1097 BOOL color0_mov
= FALSE
;
1100 /* This will loop over all the registers and try to
1101 * make a bitmask of the ones we're interested in.
1103 * Relative addressing tokens are ignored, but that's
1104 * okay, since we'll catch any address registers when
1105 * they are initialized (required by spec). */
1106 for (i
= 0; i
< ins
.dst_count
; ++i
)
1108 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.dst
[i
].reg
,
1109 shader_version
.type
, constf_size
))
1110 return WINED3DERR_INVALIDCALL
;
1112 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
)
1114 UINT idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
1116 switch (ins
.dst
[i
].reg
.type
)
1118 case WINED3DSPR_RASTOUT
:
1119 if (shader_version
.major
>= 3)
1124 reg_maps
->output_registers
|= 1u << 10;
1125 shader_signature_from_usage(&output_signature_elements
[10],
1126 WINED3D_DECL_USAGE_POSITION
, 0, 10, WINED3DSP_WRITEMASK_ALL
);
1130 reg_maps
->output_registers
|= 1u << 11;
1131 shader_signature_from_usage(&output_signature_elements
[11],
1132 WINED3D_DECL_USAGE_FOG
, 0, 11, WINED3DSP_WRITEMASK_0
);
1136 reg_maps
->output_registers
|= 1u << 11;
1137 shader_signature_from_usage(&output_signature_elements
[11],
1138 WINED3D_DECL_USAGE_PSIZE
, 0, 11, WINED3DSP_WRITEMASK_1
);
1143 case WINED3DSPR_ATTROUT
:
1144 if (shader_version
.major
>= 3)
1149 if (reg_maps
->output_registers
& (1u << idx
))
1151 output_signature_elements
[idx
].mask
|= ins
.dst
[i
].write_mask
;
1155 reg_maps
->output_registers
|= 1u << idx
;
1156 shader_signature_from_usage(&output_signature_elements
[idx
],
1157 WINED3D_DECL_USAGE_COLOR
, idx
- 8, idx
, ins
.dst
[i
].write_mask
);
1162 case WINED3DSPR_TEXCRDOUT
:
1163 if (shader_version
.major
>= 3)
1165 reg_maps
->u
.output_registers_mask
[idx
] |= ins
.dst
[i
].write_mask
;
1168 reg_maps
->u
.texcoord_mask
[idx
] |= ins
.dst
[i
].write_mask
;
1169 if (reg_maps
->output_registers
& (1u << idx
))
1171 output_signature_elements
[idx
].mask
|= ins
.dst
[i
].write_mask
;
1175 reg_maps
->output_registers
|= 1u << idx
;
1176 shader_signature_from_usage(&output_signature_elements
[idx
],
1177 WINED3D_DECL_USAGE_TEXCOORD
, idx
, idx
, ins
.dst
[i
].write_mask
);
1186 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1188 if (ins
.dst
[i
].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
.dst
[i
].reg
.idx
[0].offset
)
1190 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
1191 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
1192 * the mov and perform the sRGB write correction from the source register.
1194 * However, if the mov is only partial, we can't do this, and if the write
1195 * comes from an instruction other than MOV it is hard to do as well. If
1196 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
1197 shader
->u
.ps
.color0_mov
= FALSE
;
1198 if (ins
.handler_idx
== WINED3DSIH_MOV
1199 && ins
.dst
[i
].write_mask
== WINED3DSP_WRITEMASK_ALL
)
1201 /* Used later when the source register is read. */
1205 /* Also drop the MOV marker if the source register is overwritten prior to the shader
1208 else if (ins
.dst
[i
].reg
.type
== WINED3DSPR_TEMP
1209 && ins
.dst
[i
].reg
.idx
[0].offset
== shader
->u
.ps
.color0_reg
)
1211 shader
->u
.ps
.color0_mov
= FALSE
;
1215 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
1216 if (shader_version
.major
== 1
1217 && (ins
.handler_idx
== WINED3DSIH_TEX
1218 || ins
.handler_idx
== WINED3DSIH_TEXBEM
1219 || ins
.handler_idx
== WINED3DSIH_TEXBEML
1220 || ins
.handler_idx
== WINED3DSIH_TEXDP3TEX
1221 || ins
.handler_idx
== WINED3DSIH_TEXM3x2TEX
1222 || ins
.handler_idx
== WINED3DSIH_TEXM3x3SPEC
1223 || ins
.handler_idx
== WINED3DSIH_TEXM3x3TEX
1224 || ins
.handler_idx
== WINED3DSIH_TEXM3x3VSPEC
1225 || ins
.handler_idx
== WINED3DSIH_TEXREG2AR
1226 || ins
.handler_idx
== WINED3DSIH_TEXREG2GB
1227 || ins
.handler_idx
== WINED3DSIH_TEXREG2RGB
))
1229 unsigned int reg_idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
1231 TRACE("Setting fake 2D resource for 1.x pixelshader.\n");
1232 reg_maps
->resource_info
[reg_idx
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
1233 reg_maps
->resource_info
[reg_idx
].data_type
= WINED3D_DATA_FLOAT
;
1234 shader_record_sample(reg_maps
, reg_idx
, reg_idx
, reg_idx
);
1236 /* texbem is only valid with < 1.4 pixel shaders */
1237 if (ins
.handler_idx
== WINED3DSIH_TEXBEM
1238 || ins
.handler_idx
== WINED3DSIH_TEXBEML
)
1240 reg_maps
->bumpmat
|= 1u << reg_idx
;
1241 if (ins
.handler_idx
== WINED3DSIH_TEXBEML
)
1243 reg_maps
->luminanceparams
|= 1u << reg_idx
;
1247 else if (ins
.handler_idx
== WINED3DSIH_BEM
)
1249 reg_maps
->bumpmat
|= 1u << ins
.dst
[i
].reg
.idx
[0].offset
;
1253 if (ins
.handler_idx
== WINED3DSIH_ATOMIC_IADD
1254 || ins
.handler_idx
== WINED3DSIH_LD_UAV_TYPED
)
1256 unsigned int reg_idx
;
1257 if (ins
.handler_idx
== WINED3DSIH_ATOMIC_IADD
)
1258 reg_idx
= ins
.dst
[0].reg
.idx
[0].offset
;
1260 reg_idx
= ins
.src
[1].reg
.idx
[0].offset
;
1261 if (reg_idx
>= MAX_UNORDERED_ACCESS_VIEWS
)
1263 ERR("Invalid UAV index %u.\n", reg_idx
);
1266 reg_maps
->uav_read_mask
|= (1u << reg_idx
);
1268 else if (ins
.handler_idx
== WINED3DSIH_NRM
)
1270 reg_maps
->usesnrm
= 1;
1272 else if (ins
.handler_idx
== WINED3DSIH_DSY
1273 || ins
.handler_idx
== WINED3DSIH_DSY_COARSE
1274 || ins
.handler_idx
== WINED3DSIH_DSY_FINE
)
1276 reg_maps
->usesdsy
= 1;
1278 else if (ins
.handler_idx
== WINED3DSIH_DSX
1279 || ins
.handler_idx
== WINED3DSIH_DSX_COARSE
1280 || ins
.handler_idx
== WINED3DSIH_DSX_FINE
)
1282 reg_maps
->usesdsx
= 1;
1284 else if (ins
.handler_idx
== WINED3DSIH_TEXLDD
) reg_maps
->usestexldd
= 1;
1285 else if (ins
.handler_idx
== WINED3DSIH_TEXLDL
) reg_maps
->usestexldl
= 1;
1286 else if (ins
.handler_idx
== WINED3DSIH_MOVA
) reg_maps
->usesmova
= 1;
1287 else if (ins
.handler_idx
== WINED3DSIH_IFC
) reg_maps
->usesifc
= 1;
1288 else if (ins
.handler_idx
== WINED3DSIH_CALL
) reg_maps
->usescall
= 1;
1289 else if (ins
.handler_idx
== WINED3DSIH_POW
) reg_maps
->usespow
= 1;
1290 else if (ins
.handler_idx
== WINED3DSIH_LOOP
1291 || ins
.handler_idx
== WINED3DSIH_REP
)
1294 if (cur_loop_depth
> max_loop_depth
)
1295 max_loop_depth
= cur_loop_depth
;
1297 else if (ins
.handler_idx
== WINED3DSIH_ENDLOOP
1298 || ins
.handler_idx
== WINED3DSIH_ENDREP
)
1302 else if (ins
.handler_idx
== WINED3DSIH_SAMPLE
1303 || ins
.handler_idx
== WINED3DSIH_SAMPLE_B
1304 || ins
.handler_idx
== WINED3DSIH_SAMPLE_C
1305 || ins
.handler_idx
== WINED3DSIH_SAMPLE_C_LZ
1306 || ins
.handler_idx
== WINED3DSIH_SAMPLE_GRAD
1307 || ins
.handler_idx
== WINED3DSIH_SAMPLE_LOD
)
1309 shader_record_sample(reg_maps
, ins
.src
[1].reg
.idx
[0].offset
,
1310 ins
.src
[2].reg
.idx
[0].offset
, reg_maps
->sampler_map
.count
);
1312 else if (ins
.handler_idx
== WINED3DSIH_LD
1313 || (ins
.handler_idx
== WINED3DSIH_RESINFO
&& ins
.src
[1].reg
.type
== WINED3DSPR_RESOURCE
))
1315 shader_record_sample(reg_maps
, ins
.src
[1].reg
.idx
[0].offset
,
1316 WINED3D_SAMPLER_DEFAULT
, reg_maps
->sampler_map
.count
);
1320 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.predicate
->reg
,
1321 shader_version
.type
, constf_size
))
1322 return WINED3DERR_INVALIDCALL
;
1324 for (i
= 0; i
< ins
.src_count
; ++i
)
1326 unsigned int count
= get_instr_extra_regcount(ins
.handler_idx
, i
);
1327 struct wined3d_shader_register reg
= ins
.src
[i
].reg
;
1329 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.src
[i
].reg
,
1330 shader_version
.type
, constf_size
))
1331 return WINED3DERR_INVALIDCALL
;
1334 ++reg
.idx
[0].offset
;
1335 if (!shader_record_register_usage(shader
, reg_maps
, ®
,
1336 shader_version
.type
, constf_size
))
1337 return WINED3DERR_INVALIDCALL
;
1343 if (ins
.src
[i
].reg
.type
== WINED3DSPR_TEMP
1344 && ins
.src
[i
].swizzle
== WINED3DSP_NOSWIZZLE
)
1346 shader
->u
.ps
.color0_mov
= TRUE
;
1347 shader
->u
.ps
.color0_reg
= ins
.src
[i
].reg
.idx
[0].offset
;
1353 reg_maps
->loop_depth
= max_loop_depth
;
1355 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1356 * R0 is written to the render target. */
1357 if (shader_version
.major
< 2 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1358 reg_maps
->rt_mask
|= (1u << 0);
1360 shader
->functionLength
= ((const char *)ptr
- (const char *)byte_code
);
1362 if (input_signature
->elements
)
1364 for (i
= 0; i
< input_signature
->element_count
; ++i
)
1366 reg_maps
->input_registers
|= 1u << input_signature
->elements
[i
].register_idx
;
1367 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1369 if (input_signature
->elements
[i
].sysval_semantic
== WINED3D_SV_POSITION
)
1371 else if (input_signature
->elements
[i
].sysval_semantic
== WINED3D_SV_IS_FRONT_FACE
)
1372 reg_maps
->usesfacing
= 1;
1376 else if (!input_signature
->elements
&& reg_maps
->input_registers
)
1378 unsigned int count
= wined3d_popcount(reg_maps
->input_registers
);
1379 struct wined3d_shader_signature_element
*e
;
1382 if (!(input_signature
->elements
= wined3d_calloc(count
, sizeof(*input_signature
->elements
))))
1383 return E_OUTOFMEMORY
;
1384 input_signature
->element_count
= count
;
1386 e
= input_signature
->elements
;
1387 for (i
= 0; i
< ARRAY_SIZE(input_signature_elements
); ++i
)
1389 if (!(reg_maps
->input_registers
& (1u << i
)))
1391 input_signature_elements
[i
].register_idx
= i
;
1392 *e
++ = input_signature_elements
[i
];
1396 if (output_signature
->elements
)
1398 for (i
= 0; i
< output_signature
->element_count
; ++i
)
1400 reg_maps
->output_registers
|= 1u << output_signature
->elements
[i
].register_idx
;
1403 else if (reg_maps
->output_registers
)
1405 unsigned int count
= wined3d_popcount(reg_maps
->output_registers
);
1406 struct wined3d_shader_signature_element
*e
;
1408 if (!(output_signature
->elements
= wined3d_calloc(count
, sizeof(*output_signature
->elements
))))
1409 return E_OUTOFMEMORY
;
1410 output_signature
->element_count
= count
;
1412 e
= output_signature
->elements
;
1413 for (i
= 0; i
< ARRAY_SIZE(output_signature_elements
); ++i
)
1415 if (!(reg_maps
->output_registers
& (1u << i
)))
1417 *e
++ = output_signature_elements
[i
];
1424 static void shader_cleanup_reg_maps(struct wined3d_shader_reg_maps
*reg_maps
)
1426 struct wined3d_shader_indexable_temp
*reg
, *reg_next
;
1428 HeapFree(GetProcessHeap(), 0, reg_maps
->constf
);
1429 HeapFree(GetProcessHeap(), 0, reg_maps
->sampler_map
.entries
);
1431 LIST_FOR_EACH_ENTRY_SAFE(reg
, reg_next
, ®_maps
->indexable_temps
, struct wined3d_shader_indexable_temp
, entry
)
1432 HeapFree(GetProcessHeap(), 0, reg
);
1433 list_init(®_maps
->indexable_temps
);
1436 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int max
)
1438 DWORD map
= 1u << max
;
1440 map
&= reg_maps
->shader_version
.major
< 3 ? ~reg_maps
->texcoord
: ~reg_maps
->input_registers
;
1442 return wined3d_log2i(map
);
1445 static void shader_dump_global_flags(struct wined3d_string_buffer
*buffer
, DWORD global_flags
)
1447 if (global_flags
& WINED3DSGF_REFACTORING_ALLOWED
)
1449 shader_addline(buffer
, "refactoringAllowed");
1450 global_flags
&= ~WINED3DSGF_REFACTORING_ALLOWED
;
1452 shader_addline(buffer
, " | ");
1455 if (global_flags
& WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS
)
1457 shader_addline(buffer
, "enableRawAndStructuredBuffers");
1458 global_flags
&= ~WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS
;
1462 shader_addline(buffer
, "unknown_flags(%#x)", global_flags
);
1465 static void shader_dump_sync_flags(struct wined3d_string_buffer
*buffer
, DWORD sync_flags
)
1467 if (sync_flags
& WINED3DSSF_GROUP_SHARED_MEMORY
)
1469 shader_addline(buffer
, "_g");
1470 sync_flags
&= ~WINED3DSSF_GROUP_SHARED_MEMORY
;
1472 if (sync_flags
& WINED3DSSF_THREAD_GROUP
)
1474 shader_addline(buffer
, "_t");
1475 sync_flags
&= ~WINED3DSSF_THREAD_GROUP
;
1479 shader_addline(buffer
, "_unknown_flags(%#x)", sync_flags
);
1482 static void shader_dump_uav_flags(struct wined3d_string_buffer
*buffer
, DWORD uav_flags
)
1484 if (uav_flags
& WINED3DSUF_GLOBALLY_COHERENT
)
1486 shader_addline(buffer
, "_glc");
1487 uav_flags
&= ~WINED3DSUF_GLOBALLY_COHERENT
;
1489 if (uav_flags
& WINED3DSUF_ORDER_PRESERVING_COUNTER
)
1491 shader_addline(buffer
, "_opc");
1492 uav_flags
&= ~WINED3DSUF_ORDER_PRESERVING_COUNTER
;
1496 shader_addline(buffer
, "_unknown_flags(%#x)", uav_flags
);
1499 static void shader_dump_tessellator_domain(struct wined3d_string_buffer
*buffer
,
1500 enum wined3d_tessellator_domain domain
)
1504 case WINED3D_TESSELLATOR_DOMAIN_LINE
:
1505 shader_addline(buffer
, "line");
1507 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE
:
1508 shader_addline(buffer
, "triangle");
1510 case WINED3D_TESSELLATOR_DOMAIN_QUAD
:
1511 shader_addline(buffer
, "quad");
1514 shader_addline(buffer
, "unknown_tessellator_domain(%#x)", domain
);
1519 static void shader_dump_tessellator_output_primitive(struct wined3d_string_buffer
*buffer
,
1520 enum wined3d_tessellator_output_primitive output_primitive
)
1522 switch (output_primitive
)
1524 case WINED3D_TESSELLATOR_OUTPUT_POINT
:
1525 shader_addline(buffer
, "point");
1527 case WINED3D_TESSELLATOR_OUTPUT_LINE
:
1528 shader_addline(buffer
, "line");
1530 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW
:
1531 shader_addline(buffer
, "triangle_cw");
1533 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW
:
1534 shader_addline(buffer
, "triangle_ccw");
1537 shader_addline(buffer
, "unknown_tessellator_output_primitive(%#x)", output_primitive
);
1542 static void shader_dump_tessellator_partitioning(struct wined3d_string_buffer
*buffer
,
1543 enum wined3d_tessellator_partitioning partitioning
)
1545 switch (partitioning
)
1547 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER
:
1548 shader_addline(buffer
, "integer");
1550 case WINED3D_TESSELLATOR_PARTITIONING_POW2
:
1551 shader_addline(buffer
, "pow2");
1553 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD
:
1554 shader_addline(buffer
, "fractional_odd");
1556 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN
:
1557 shader_addline(buffer
, "fractional_even");
1560 shader_addline(buffer
, "unknown_tessellator_partitioning(%#x)", partitioning
);
1565 static void shader_dump_sysval_semantic(struct wined3d_string_buffer
*buffer
, enum wined3d_sysval_semantic semantic
)
1569 for (i
= 0; i
< ARRAY_SIZE(sysval_semantic_names
); ++i
)
1571 if (sysval_semantic_names
[i
].sysval_semantic
== semantic
)
1573 shader_addline(buffer
, "%s", sysval_semantic_names
[i
].sysval_name
);
1578 shader_addline(buffer
, "unknown_sysval_semantic(%#x)", semantic
);
1581 static void shader_dump_decl_usage(struct wined3d_string_buffer
*buffer
,
1582 const struct wined3d_shader_semantic
*semantic
, const struct wined3d_shader_version
*shader_version
)
1584 shader_addline(buffer
, "dcl");
1586 if (semantic
->reg
.reg
.type
== WINED3DSPR_SAMPLER
)
1588 switch (semantic
->resource_type
)
1590 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
1591 shader_addline(buffer
, "_2d");
1594 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
1595 shader_addline(buffer
, "_3d");
1598 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
1599 shader_addline(buffer
, "_cube");
1603 shader_addline(buffer
, "_unknown_resource_type(%#x)", semantic
->resource_type
);
1607 else if (semantic
->reg
.reg
.type
== WINED3DSPR_RESOURCE
|| semantic
->reg
.reg
.type
== WINED3DSPR_UAV
)
1609 if (semantic
->reg
.reg
.type
== WINED3DSPR_RESOURCE
)
1610 shader_addline(buffer
, "_resource_");
1612 shader_addline(buffer
, "_uav_");
1613 switch (semantic
->resource_type
)
1615 case WINED3D_SHADER_RESOURCE_BUFFER
:
1616 shader_addline(buffer
, "buffer");
1619 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
1620 shader_addline(buffer
, "texture1d");
1623 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
1624 shader_addline(buffer
, "texture2d");
1627 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
:
1628 shader_addline(buffer
, "texture2dms");
1631 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
1632 shader_addline(buffer
, "texture3d");
1635 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
1636 shader_addline(buffer
, "texturecube");
1639 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
:
1640 shader_addline(buffer
, "texture1darray");
1643 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
1644 shader_addline(buffer
, "texture2darray");
1647 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY
:
1648 shader_addline(buffer
, "texture2dmsarray");
1652 shader_addline(buffer
, "unknown");
1655 switch (semantic
->resource_data_type
)
1657 case WINED3D_DATA_FLOAT
:
1658 shader_addline(buffer
, " (float)");
1661 case WINED3D_DATA_INT
:
1662 shader_addline(buffer
, " (int)");
1665 case WINED3D_DATA_UINT
:
1666 shader_addline(buffer
, " (uint)");
1669 case WINED3D_DATA_UNORM
:
1670 shader_addline(buffer
, " (unorm)");
1673 case WINED3D_DATA_SNORM
:
1674 shader_addline(buffer
, " (snorm)");
1678 shader_addline(buffer
, " (unknown)");
1684 /* Pixel shaders 3.0 don't have usage semantics. */
1685 if (shader_version
->major
< 3 && shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
1688 shader_addline(buffer
, "_");
1690 switch (semantic
->usage
)
1692 case WINED3D_DECL_USAGE_POSITION
:
1693 shader_addline(buffer
, "position%u", semantic
->usage_idx
);
1696 case WINED3D_DECL_USAGE_BLEND_INDICES
:
1697 shader_addline(buffer
, "blend");
1700 case WINED3D_DECL_USAGE_BLEND_WEIGHT
:
1701 shader_addline(buffer
, "weight");
1704 case WINED3D_DECL_USAGE_NORMAL
:
1705 shader_addline(buffer
, "normal%u", semantic
->usage_idx
);
1708 case WINED3D_DECL_USAGE_PSIZE
:
1709 shader_addline(buffer
, "psize");
1712 case WINED3D_DECL_USAGE_COLOR
:
1713 if (!semantic
->usage_idx
)
1714 shader_addline(buffer
, "color");
1716 shader_addline(buffer
, "specular%u", (semantic
->usage_idx
- 1));
1719 case WINED3D_DECL_USAGE_TEXCOORD
:
1720 shader_addline(buffer
, "texture%u", semantic
->usage_idx
);
1723 case WINED3D_DECL_USAGE_TANGENT
:
1724 shader_addline(buffer
, "tangent");
1727 case WINED3D_DECL_USAGE_BINORMAL
:
1728 shader_addline(buffer
, "binormal");
1731 case WINED3D_DECL_USAGE_TESS_FACTOR
:
1732 shader_addline(buffer
, "tessfactor");
1735 case WINED3D_DECL_USAGE_POSITIONT
:
1736 shader_addline(buffer
, "positionT%u", semantic
->usage_idx
);
1739 case WINED3D_DECL_USAGE_FOG
:
1740 shader_addline(buffer
, "fog");
1743 case WINED3D_DECL_USAGE_DEPTH
:
1744 shader_addline(buffer
, "depth");
1747 case WINED3D_DECL_USAGE_SAMPLE
:
1748 shader_addline(buffer
, "sample");
1752 shader_addline(buffer
, "<unknown_semantic(%#x)>", semantic
->usage
);
1753 FIXME("Unrecognised semantic usage %#x.\n", semantic
->usage
);
1758 static void shader_dump_register(struct wined3d_string_buffer
*buffer
,
1759 const struct wined3d_shader_register
*reg
, const struct wined3d_shader_version
*shader_version
)
1761 static const char * const rastout_reg_names
[] = {"oPos", "oFog", "oPts"};
1762 static const char * const misctype_reg_names
[] = {"vPos", "vFace"};
1763 UINT offset
= reg
->idx
[0].offset
;
1767 case WINED3DSPR_TEMP
:
1768 shader_addline(buffer
, "r");
1771 case WINED3DSPR_INPUT
:
1772 shader_addline(buffer
, "v");
1775 case WINED3DSPR_CONST
:
1776 case WINED3DSPR_CONST2
:
1777 case WINED3DSPR_CONST3
:
1778 case WINED3DSPR_CONST4
:
1779 shader_addline(buffer
, "c");
1780 offset
= shader_get_float_offset(reg
->type
, offset
);
1783 case WINED3DSPR_TEXTURE
: /* vs: case WINED3DSPR_ADDR */
1784 shader_addline(buffer
, "%c", shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
? 't' : 'a');
1787 case WINED3DSPR_RASTOUT
:
1788 shader_addline(buffer
, "%s", rastout_reg_names
[offset
]);
1791 case WINED3DSPR_COLOROUT
:
1792 shader_addline(buffer
, "oC");
1795 case WINED3DSPR_DEPTHOUT
:
1796 shader_addline(buffer
, "oDepth");
1799 case WINED3DSPR_ATTROUT
:
1800 shader_addline(buffer
, "oD");
1803 case WINED3DSPR_TEXCRDOUT
:
1804 /* Vertex shaders >= 3.0 use general purpose output registers
1805 * (WINED3DSPR_OUTPUT), which can include an address token. */
1806 if (shader_version
->major
>= 3)
1807 shader_addline(buffer
, "o");
1809 shader_addline(buffer
, "oT");
1812 case WINED3DSPR_CONSTINT
:
1813 shader_addline(buffer
, "i");
1816 case WINED3DSPR_CONSTBOOL
:
1817 shader_addline(buffer
, "b");
1820 case WINED3DSPR_LABEL
:
1821 shader_addline(buffer
, "l");
1824 case WINED3DSPR_LOOP
:
1825 shader_addline(buffer
, "aL");
1828 case WINED3DSPR_SAMPLER
:
1829 shader_addline(buffer
, "s");
1832 case WINED3DSPR_MISCTYPE
:
1835 FIXME("Unhandled misctype register %u.\n", offset
);
1836 shader_addline(buffer
, "<unhandled misctype %#x>", offset
);
1840 shader_addline(buffer
, "%s", misctype_reg_names
[offset
]);
1844 case WINED3DSPR_PREDICATE
:
1845 shader_addline(buffer
, "p");
1848 case WINED3DSPR_IMMCONST
:
1849 shader_addline(buffer
, "l");
1852 case WINED3DSPR_CONSTBUFFER
:
1853 shader_addline(buffer
, "cb");
1856 case WINED3DSPR_IMMCONSTBUFFER
:
1857 shader_addline(buffer
, "icb");
1860 case WINED3DSPR_PRIMID
:
1861 shader_addline(buffer
, "primID");
1864 case WINED3DSPR_NULL
:
1865 shader_addline(buffer
, "null");
1868 case WINED3DSPR_RESOURCE
:
1869 shader_addline(buffer
, "t");
1872 case WINED3DSPR_UAV
:
1873 shader_addline(buffer
, "u");
1876 case WINED3DSPR_OUTPOINTID
:
1877 shader_addline(buffer
, "vOutputControlPointID");
1880 case WINED3DSPR_FORKINSTID
:
1881 shader_addline(buffer
, "vForkInstanceId");
1884 case WINED3DSPR_INCONTROLPOINT
:
1885 shader_addline(buffer
, "vicp");
1888 case WINED3DSPR_OUTCONTROLPOINT
:
1889 shader_addline(buffer
, "vocp");
1892 case WINED3DSPR_PATCHCONST
:
1893 shader_addline(buffer
, "vpc");
1896 case WINED3DSPR_TESSCOORD
:
1897 shader_addline(buffer
, "vDomainLocation");
1900 case WINED3DSPR_GROUPSHAREDMEM
:
1901 shader_addline(buffer
, "g");
1904 case WINED3DSPR_THREADID
:
1905 shader_addline(buffer
, "vThreadID");
1908 case WINED3DSPR_THREADGROUPID
:
1909 shader_addline(buffer
, "vThreadGroupID");
1912 case WINED3DSPR_LOCALTHREADID
:
1913 shader_addline(buffer
, "vThreadIDInGroup");
1916 case WINED3DSPR_LOCALTHREADINDEX
:
1917 shader_addline(buffer
, "vThreadIDInGroupFlattened");
1920 case WINED3DSPR_IDXTEMP
:
1921 shader_addline(buffer
, "x");
1924 case WINED3DSPR_STREAM
:
1925 shader_addline(buffer
, "m");
1928 case WINED3DSPR_FUNCTIONBODY
:
1929 shader_addline(buffer
, "fb");
1932 case WINED3DSPR_FUNCTIONPOINTER
:
1933 shader_addline(buffer
, "fp");
1936 case WINED3DSPR_COVERAGE
:
1937 shader_addline(buffer
, "vCoverage");
1940 case WINED3DSPR_SAMPLEMASK
:
1941 shader_addline(buffer
, "oMask");
1945 shader_addline(buffer
, "<unhandled_rtype(%#x)>", reg
->type
);
1949 if (reg
->type
== WINED3DSPR_IMMCONST
)
1951 shader_addline(buffer
, "(");
1952 switch (reg
->immconst_type
)
1954 case WINED3D_IMMCONST_SCALAR
:
1955 switch (reg
->data_type
)
1957 case WINED3D_DATA_FLOAT
:
1958 shader_addline(buffer
, "%.8e", *(const float *)reg
->u
.immconst_data
);
1960 case WINED3D_DATA_INT
:
1961 shader_addline(buffer
, "%d", reg
->u
.immconst_data
[0]);
1963 case WINED3D_DATA_RESOURCE
:
1964 case WINED3D_DATA_SAMPLER
:
1965 case WINED3D_DATA_UINT
:
1966 shader_addline(buffer
, "%u", reg
->u
.immconst_data
[0]);
1969 shader_addline(buffer
, "<unhandled data type %#x>", reg
->data_type
);
1974 case WINED3D_IMMCONST_VEC4
:
1975 switch (reg
->data_type
)
1977 case WINED3D_DATA_FLOAT
:
1978 shader_addline(buffer
, "%.8e, %.8e, %.8e, %.8e",
1979 *(const float *)®
->u
.immconst_data
[0], *(const float *)®
->u
.immconst_data
[1],
1980 *(const float *)®
->u
.immconst_data
[2], *(const float *)®
->u
.immconst_data
[3]);
1982 case WINED3D_DATA_INT
:
1983 shader_addline(buffer
, "%d, %d, %d, %d",
1984 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
1985 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
1987 case WINED3D_DATA_RESOURCE
:
1988 case WINED3D_DATA_SAMPLER
:
1989 case WINED3D_DATA_UINT
:
1990 shader_addline(buffer
, "%u, %u, %u, %u",
1991 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
1992 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
1995 shader_addline(buffer
, "<unhandled data type %#x>", reg
->data_type
);
2001 shader_addline(buffer
, "<unhandled immconst_type %#x>", reg
->immconst_type
);
2004 shader_addline(buffer
, ")");
2006 else if (reg
->type
!= WINED3DSPR_RASTOUT
2007 && reg
->type
!= WINED3DSPR_MISCTYPE
2008 && reg
->type
!= WINED3DSPR_NULL
)
2012 shader_addline(buffer
, "[");
2013 if (reg
->idx
[0].rel_addr
)
2015 shader_dump_src_param(buffer
, reg
->idx
[0].rel_addr
, shader_version
);
2016 shader_addline(buffer
, " + ");
2018 shader_addline(buffer
, "%u]", offset
);
2020 if (reg
->idx
[1].offset
!= ~0u)
2022 shader_addline(buffer
, "[");
2023 if (reg
->idx
[1].rel_addr
)
2025 shader_dump_src_param(buffer
, reg
->idx
[1].rel_addr
, shader_version
);
2026 shader_addline(buffer
, " + ");
2028 shader_addline(buffer
, "%u]", reg
->idx
[1].offset
);
2032 if (reg
->type
== WINED3DSPR_FUNCTIONPOINTER
)
2033 shader_addline(buffer
, "[%u]", reg
->u
.fp_body_idx
);
2037 static void shader_dump_dst_param(struct wined3d_string_buffer
*buffer
,
2038 const struct wined3d_shader_dst_param
*param
, const struct wined3d_shader_version
*shader_version
)
2040 DWORD write_mask
= param
->write_mask
;
2042 shader_dump_register(buffer
, ¶m
->reg
, shader_version
);
2044 if (write_mask
&& write_mask
!= WINED3DSP_WRITEMASK_ALL
)
2046 static const char write_mask_chars
[] = "xyzw";
2048 shader_addline(buffer
, ".");
2049 if (write_mask
& WINED3DSP_WRITEMASK_0
)
2050 shader_addline(buffer
, "%c", write_mask_chars
[0]);
2051 if (write_mask
& WINED3DSP_WRITEMASK_1
)
2052 shader_addline(buffer
, "%c", write_mask_chars
[1]);
2053 if (write_mask
& WINED3DSP_WRITEMASK_2
)
2054 shader_addline(buffer
, "%c", write_mask_chars
[2]);
2055 if (write_mask
& WINED3DSP_WRITEMASK_3
)
2056 shader_addline(buffer
, "%c", write_mask_chars
[3]);
2060 static void shader_dump_src_param(struct wined3d_string_buffer
*buffer
,
2061 const struct wined3d_shader_src_param
*param
, const struct wined3d_shader_version
*shader_version
)
2063 enum wined3d_shader_src_modifier src_modifier
= param
->modifiers
;
2064 DWORD swizzle
= param
->swizzle
;
2066 if (src_modifier
== WINED3DSPSM_NEG
2067 || src_modifier
== WINED3DSPSM_BIASNEG
2068 || src_modifier
== WINED3DSPSM_SIGNNEG
2069 || src_modifier
== WINED3DSPSM_X2NEG
2070 || src_modifier
== WINED3DSPSM_ABSNEG
)
2071 shader_addline(buffer
, "-");
2072 else if (src_modifier
== WINED3DSPSM_COMP
)
2073 shader_addline(buffer
, "1-");
2074 else if (src_modifier
== WINED3DSPSM_NOT
)
2075 shader_addline(buffer
, "!");
2077 if (src_modifier
== WINED3DSPSM_ABS
|| src_modifier
== WINED3DSPSM_ABSNEG
)
2078 shader_addline(buffer
, "abs(");
2080 shader_dump_register(buffer
, ¶m
->reg
, shader_version
);
2082 switch (src_modifier
)
2084 case WINED3DSPSM_NONE
: break;
2085 case WINED3DSPSM_NEG
: break;
2086 case WINED3DSPSM_NOT
: break;
2087 case WINED3DSPSM_BIAS
: shader_addline(buffer
, "_bias"); break;
2088 case WINED3DSPSM_BIASNEG
: shader_addline(buffer
, "_bias"); break;
2089 case WINED3DSPSM_SIGN
: shader_addline(buffer
, "_bx2"); break;
2090 case WINED3DSPSM_SIGNNEG
: shader_addline(buffer
, "_bx2"); break;
2091 case WINED3DSPSM_COMP
: break;
2092 case WINED3DSPSM_X2
: shader_addline(buffer
, "_x2"); break;
2093 case WINED3DSPSM_X2NEG
: shader_addline(buffer
, "_x2"); break;
2094 case WINED3DSPSM_DZ
: shader_addline(buffer
, "_dz"); break;
2095 case WINED3DSPSM_DW
: shader_addline(buffer
, "_dw"); break;
2096 case WINED3DSPSM_ABSNEG
: shader_addline(buffer
, ")"); break;
2097 case WINED3DSPSM_ABS
: shader_addline(buffer
, ")"); break;
2098 default: shader_addline(buffer
, "_unknown_modifier(%#x)", src_modifier
);
2101 if (swizzle
!= WINED3DSP_NOSWIZZLE
)
2103 static const char swizzle_chars
[] = "xyzw";
2104 DWORD swizzle_x
= swizzle
& 0x03;
2105 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
2106 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
2107 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
2109 if (swizzle_x
== swizzle_y
2110 && swizzle_x
== swizzle_z
2111 && swizzle_x
== swizzle_w
)
2113 shader_addline(buffer
, ".%c", swizzle_chars
[swizzle_x
]);
2117 shader_addline(buffer
, ".%c%c%c%c", swizzle_chars
[swizzle_x
], swizzle_chars
[swizzle_y
],
2118 swizzle_chars
[swizzle_z
], swizzle_chars
[swizzle_w
]);
2123 /* Shared code in order to generate the bulk of the shader string.
2124 * NOTE: A description of how to parse tokens can be found on MSDN. */
2125 void shader_generate_main(const struct wined3d_shader
*shader
, struct wined3d_string_buffer
*buffer
,
2126 const struct wined3d_shader_reg_maps
*reg_maps
, const DWORD
*byte_code
, void *backend_ctx
)
2128 struct wined3d_device
*device
= shader
->device
;
2129 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
2130 void *fe_data
= shader
->frontend_data
;
2131 struct wined3d_shader_version shader_version
;
2132 struct wined3d_shader_parser_state state
;
2133 struct wined3d_shader_instruction ins
;
2134 struct wined3d_shader_tex_mx tex_mx
;
2135 struct wined3d_shader_context ctx
;
2136 const DWORD
*ptr
= byte_code
;
2138 /* Initialize current parsing state. */
2139 tex_mx
.current_row
= 0;
2140 state
.current_loop_depth
= 0;
2141 state
.current_loop_reg
= 0;
2142 state
.in_subroutine
= FALSE
;
2144 ctx
.shader
= shader
;
2145 ctx
.gl_info
= &device
->adapter
->gl_info
;
2146 ctx
.reg_maps
= reg_maps
;
2147 ctx
.buffer
= buffer
;
2148 ctx
.tex_mx
= &tex_mx
;
2150 ctx
.backend_data
= backend_ctx
;
2153 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
2155 while (!fe
->shader_is_end(fe_data
, &ptr
))
2158 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
2160 /* Unknown opcode and its parameters. */
2161 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
2163 TRACE("Skipping unrecognized instruction.\n");
2168 FIXME("Predicates not implemented.\n");
2170 /* Call appropriate function for output target */
2171 device
->shader_backend
->shader_handle_instruction(&ins
);
2175 static void shader_dump_ins_modifiers(struct wined3d_string_buffer
*buffer
,
2176 const struct wined3d_shader_dst_param
*dst
)
2178 DWORD mmask
= dst
->modifiers
;
2183 case 13: shader_addline(buffer
, "_d8"); break;
2184 case 14: shader_addline(buffer
, "_d4"); break;
2185 case 15: shader_addline(buffer
, "_d2"); break;
2186 case 1: shader_addline(buffer
, "_x2"); break;
2187 case 2: shader_addline(buffer
, "_x4"); break;
2188 case 3: shader_addline(buffer
, "_x8"); break;
2189 default: shader_addline(buffer
, "_unhandled_shift(%d)", dst
->shift
); break;
2192 if (mmask
& WINED3DSPDM_SATURATE
) shader_addline(buffer
, "_sat");
2193 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) shader_addline(buffer
, "_pp");
2194 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) shader_addline(buffer
, "_centroid");
2196 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
2197 if (mmask
) FIXME("Unrecognised modifier %#x.\n", mmask
);
2200 static void shader_dump_primitive_type(struct wined3d_string_buffer
*buffer
,
2201 enum wined3d_primitive_type primitive_type
)
2203 switch (primitive_type
)
2205 case WINED3D_PT_UNDEFINED
:
2206 shader_addline(buffer
, "undefined");
2208 case WINED3D_PT_POINTLIST
:
2209 shader_addline(buffer
, "pointlist");
2211 case WINED3D_PT_LINELIST
:
2212 shader_addline(buffer
, "linelist");
2214 case WINED3D_PT_LINESTRIP
:
2215 shader_addline(buffer
, "linestrip");
2217 case WINED3D_PT_TRIANGLELIST
:
2218 shader_addline(buffer
, "trianglelist");
2220 case WINED3D_PT_TRIANGLESTRIP
:
2221 shader_addline(buffer
, "trianglestrip");
2223 case WINED3D_PT_TRIANGLEFAN
:
2224 shader_addline(buffer
, "trianglefan");
2226 case WINED3D_PT_LINELIST_ADJ
:
2227 shader_addline(buffer
, "linelist_adj");
2229 case WINED3D_PT_LINESTRIP_ADJ
:
2230 shader_addline(buffer
, "linestrip_adj");
2232 case WINED3D_PT_TRIANGLELIST_ADJ
:
2233 shader_addline(buffer
, "trianglelist_adj");
2235 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
2236 shader_addline(buffer
, "trianglestrip_adj");
2239 shader_addline(buffer
, "<unrecognized_primitive_type %#x>", primitive_type
);
2244 static void shader_dump_interpolation_mode(struct wined3d_string_buffer
*buffer
,
2245 enum wined3d_shader_interpolation_mode interpolation_mode
)
2247 switch (interpolation_mode
)
2249 case WINED3DSIM_CONSTANT
:
2250 shader_addline(buffer
, "constant");
2252 case WINED3DSIM_LINEAR
:
2253 shader_addline(buffer
, "linear");
2255 case WINED3DSIM_LINEAR_CENTROID
:
2256 shader_addline(buffer
, "linear centroid");
2258 case WINED3DSIM_LINEAR_NOPERSPECTIVE
:
2259 shader_addline(buffer
, "linear noperspective");
2261 case WINED3DSIM_LINEAR_SAMPLE
:
2262 shader_addline(buffer
, "linear sample");
2264 case WINED3DSIM_LINEAR_NOPERSPECTIVE_CENTROID
:
2265 shader_addline(buffer
, "linear noperspective centroid");
2267 case WINED3DSIM_LINEAR_NOPERSPECTIVE_SAMPLE
:
2268 shader_addline(buffer
, "linear noperspective sample");
2271 shader_addline(buffer
, "<unrecognized_interpolation_mode %#x>", interpolation_mode
);
2276 static void shader_trace_init(const struct wined3d_shader_frontend
*fe
, void *fe_data
, const DWORD
*byte_code
)
2278 struct wined3d_shader_version shader_version
;
2279 struct wined3d_string_buffer buffer
;
2280 const DWORD
*ptr
= byte_code
;
2281 const char *type_prefix
;
2285 if (!string_buffer_init(&buffer
))
2287 ERR("Failed to initialize string buffer.\n");
2291 TRACE("Parsing %p.\n", byte_code
);
2293 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
2295 switch (shader_version
.type
)
2297 case WINED3D_SHADER_TYPE_VERTEX
:
2301 case WINED3D_SHADER_TYPE_HULL
:
2305 case WINED3D_SHADER_TYPE_DOMAIN
:
2309 case WINED3D_SHADER_TYPE_GEOMETRY
:
2313 case WINED3D_SHADER_TYPE_PIXEL
:
2317 case WINED3D_SHADER_TYPE_COMPUTE
:
2322 FIXME("Unhandled shader type %#x.\n", shader_version
.type
);
2323 type_prefix
= "unknown";
2327 shader_addline(&buffer
, "%s_%u_%u\n", type_prefix
, shader_version
.major
, shader_version
.minor
);
2329 while (!fe
->shader_is_end(fe_data
, &ptr
))
2331 struct wined3d_shader_instruction ins
;
2333 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
2334 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
2336 WARN("Skipping unrecognized instruction.\n");
2337 shader_addline(&buffer
, "<unrecognized instruction>\n");
2341 if (ins
.handler_idx
== WINED3DSIH_DCL
|| ins
.handler_idx
== WINED3DSIH_DCL_UAV_TYPED
)
2343 shader_dump_decl_usage(&buffer
, &ins
.declaration
.semantic
, &shader_version
);
2344 shader_dump_ins_modifiers(&buffer
, &ins
.declaration
.semantic
.reg
);
2345 shader_addline(&buffer
, " ");
2346 shader_dump_dst_param(&buffer
, &ins
.declaration
.semantic
.reg
, &shader_version
);
2348 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
2350 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2351 shader_dump_src_param(&buffer
, &ins
.declaration
.src
, &shader_version
);
2352 shader_addline(&buffer
, ", %s",
2353 ins
.flags
& WINED3DSI_INDEXED_DYNAMIC
? "dynamicIndexed" : "immediateIndexed");
2355 else if (ins
.handler_idx
== WINED3DSIH_DCL_FUNCTION_BODY
)
2357 shader_addline(&buffer
, "%s fb%u",
2358 shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.index
);
2360 else if (ins
.handler_idx
== WINED3DSIH_DCL_FUNCTION_TABLE
)
2362 shader_addline(&buffer
, "%s ft%u = {...}",
2363 shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.index
);
2365 else if (ins
.handler_idx
== WINED3DSIH_DCL_GLOBAL_FLAGS
)
2367 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2368 shader_dump_global_flags(&buffer
, ins
.flags
);
2370 else if (ins
.handler_idx
== WINED3DSIH_DCL_HS_MAX_TESSFACTOR
)
2372 shader_addline(&buffer
, "%s %.8e", shader_opcode_names
[ins
.handler_idx
],
2373 ins
.declaration
.max_tessellation_factor
);
2375 else if (ins
.handler_idx
== WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER
)
2377 shader_addline(&buffer
, "%s {\n", shader_opcode_names
[ins
.handler_idx
]);
2378 for (i
= 0; i
< ins
.declaration
.icb
->vec4_count
; ++i
)
2380 shader_addline(&buffer
, " {0x%08x, 0x%08x, 0x%08x, 0x%08x},\n",
2381 ins
.declaration
.icb
->data
[4 * i
+ 0],
2382 ins
.declaration
.icb
->data
[4 * i
+ 1],
2383 ins
.declaration
.icb
->data
[4 * i
+ 2],
2384 ins
.declaration
.icb
->data
[4 * i
+ 3]);
2386 shader_addline(&buffer
, "}");
2388 else if (ins
.handler_idx
== WINED3DSIH_DCL_INDEXABLE_TEMP
)
2390 shader_addline(&buffer
, "%s x[%u][%u], %u", shader_opcode_names
[ins
.handler_idx
],
2391 ins
.declaration
.indexable_temp
.register_idx
,
2392 ins
.declaration
.indexable_temp
.register_size
,
2393 ins
.declaration
.indexable_temp
.component_count
);
2395 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PS
)
2397 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2398 shader_dump_interpolation_mode(&buffer
, ins
.flags
);
2399 shader_addline(&buffer
, " ");
2400 shader_dump_dst_param(&buffer
, &ins
.declaration
.dst
, &shader_version
);
2402 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PS_SGV
2403 || ins
.handler_idx
== WINED3DSIH_DCL_INPUT_SGV
2404 || ins
.handler_idx
== WINED3DSIH_DCL_INPUT_SIV
2405 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_SIV
)
2407 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2408 shader_dump_dst_param(&buffer
, &ins
.declaration
.register_semantic
.reg
, &shader_version
);
2409 shader_addline(&buffer
, ", ");
2410 shader_dump_sysval_semantic(&buffer
, ins
.declaration
.register_semantic
.sysval_semantic
);
2412 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PS_SIV
)
2414 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2415 shader_dump_interpolation_mode(&buffer
, ins
.flags
);
2416 shader_addline(&buffer
, " ");
2417 shader_dump_dst_param(&buffer
, &ins
.declaration
.register_semantic
.reg
, &shader_version
);
2418 shader_addline(&buffer
, ", ");
2419 shader_dump_sysval_semantic(&buffer
, ins
.declaration
.register_semantic
.sysval_semantic
);
2421 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT
2422 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT
)
2424 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2425 shader_dump_dst_param(&buffer
, &ins
.declaration
.dst
, &shader_version
);
2427 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
2428 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
2430 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2431 shader_dump_primitive_type(&buffer
, ins
.declaration
.primitive_type
);
2433 else if (ins
.handler_idx
== WINED3DSIH_DCL_INTERFACE
)
2435 shader_addline(&buffer
, "%s fp[%u][%u][%u] = {...}",
2436 shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.fp
.index
,
2437 ins
.declaration
.fp
.array_size
, ins
.declaration
.fp
.body_count
);
2439 else if (ins
.handler_idx
== WINED3DSIH_DCL_RESOURCE_STRUCTURED
)
2441 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2442 shader_dump_dst_param(&buffer
, &ins
.declaration
.structured_resource
.reg
, &shader_version
);
2443 shader_addline(&buffer
, ", %u", ins
.declaration
.structured_resource
.byte_stride
);
2445 else if (ins
.handler_idx
== WINED3DSIH_DCL_SAMPLER
)
2447 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2448 shader_dump_dst_param(&buffer
, &ins
.declaration
.dst
, &shader_version
);
2449 if (ins
.flags
== WINED3DSI_SAMPLER_COMPARISON_MODE
)
2450 shader_addline(&buffer
, ", comparisonMode");
2452 else if (ins
.handler_idx
== WINED3DSIH_DCL_TEMPS
2453 || ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
2454 || ins
.handler_idx
== WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT
2455 || ins
.handler_idx
== WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT
2456 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT
)
2458 shader_addline(&buffer
, "%s %u", shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.count
);
2460 else if (ins
.handler_idx
== WINED3DSIH_DCL_TESSELLATOR_DOMAIN
)
2462 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2463 shader_dump_tessellator_domain(&buffer
, ins
.declaration
.tessellator_domain
);
2465 else if (ins
.handler_idx
== WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE
)
2467 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2468 shader_dump_tessellator_output_primitive(&buffer
, ins
.declaration
.tessellator_output_primitive
);
2470 else if (ins
.handler_idx
== WINED3DSIH_DCL_TESSELLATOR_PARTITIONING
)
2472 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2473 shader_dump_tessellator_partitioning(&buffer
, ins
.declaration
.tessellator_partitioning
);
2475 else if (ins
.handler_idx
== WINED3DSIH_DCL_TGSM_RAW
)
2477 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2478 shader_dump_dst_param(&buffer
, &ins
.declaration
.tgsm_raw
.reg
, &shader_version
);
2479 shader_addline(&buffer
, ", %u", ins
.declaration
.tgsm_raw
.byte_count
);
2481 else if (ins
.handler_idx
== WINED3DSIH_DCL_TGSM_STRUCTURED
)
2483 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2484 shader_dump_dst_param(&buffer
, &ins
.declaration
.tgsm_structured
.reg
, &shader_version
);
2485 shader_addline(&buffer
, ", %u, %u", ins
.declaration
.tgsm_structured
.byte_stride
,
2486 ins
.declaration
.tgsm_structured
.structure_count
);
2488 else if (ins
.handler_idx
== WINED3DSIH_DCL_THREAD_GROUP
)
2490 shader_addline(&buffer
, "%s %u, %u, %u", shader_opcode_names
[ins
.handler_idx
],
2491 ins
.declaration
.thread_group_size
.x
,
2492 ins
.declaration
.thread_group_size
.y
,
2493 ins
.declaration
.thread_group_size
.z
);
2495 else if (ins
.handler_idx
== WINED3DSIH_DCL_UAV_RAW
)
2497 shader_addline(&buffer
, "%s", shader_opcode_names
[ins
.handler_idx
]);
2498 shader_dump_uav_flags(&buffer
, ins
.flags
);
2499 shader_addline(&buffer
, " ");
2500 shader_dump_dst_param(&buffer
, &ins
.declaration
.dst
, &shader_version
);
2502 else if (ins
.handler_idx
== WINED3DSIH_DCL_UAV_STRUCTURED
)
2504 shader_addline(&buffer
, "%s", shader_opcode_names
[ins
.handler_idx
]);
2505 shader_dump_uav_flags(&buffer
, ins
.flags
);
2506 shader_addline(&buffer
, " ");
2507 shader_dump_dst_param(&buffer
, &ins
.declaration
.structured_resource
.reg
, &shader_version
);
2508 shader_addline(&buffer
, ", %u", ins
.declaration
.structured_resource
.byte_stride
);
2510 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
2512 shader_addline(&buffer
, "def c%u = %.8e, %.8e, %.8e, %.8e", shader_get_float_offset(ins
.dst
[0].reg
.type
,
2513 ins
.dst
[0].reg
.idx
[0].offset
),
2514 *(const float *)&ins
.src
[0].reg
.u
.immconst_data
[0],
2515 *(const float *)&ins
.src
[0].reg
.u
.immconst_data
[1],
2516 *(const float *)&ins
.src
[0].reg
.u
.immconst_data
[2],
2517 *(const float *)&ins
.src
[0].reg
.u
.immconst_data
[3]);
2519 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
2521 shader_addline(&buffer
, "defi i%u = %d, %d, %d, %d", ins
.dst
[0].reg
.idx
[0].offset
,
2522 ins
.src
[0].reg
.u
.immconst_data
[0],
2523 ins
.src
[0].reg
.u
.immconst_data
[1],
2524 ins
.src
[0].reg
.u
.immconst_data
[2],
2525 ins
.src
[0].reg
.u
.immconst_data
[3]);
2527 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
2529 shader_addline(&buffer
, "defb b%u = %s",
2530 ins
.dst
[0].reg
.idx
[0].offset
, ins
.src
[0].reg
.u
.immconst_data
[0] ? "true" : "false");
2536 shader_addline(&buffer
, "(");
2537 shader_dump_src_param(&buffer
, ins
.predicate
, &shader_version
);
2538 shader_addline(&buffer
, ") ");
2541 /* PixWin marks instructions with the coissue flag with a '+' */
2543 shader_addline(&buffer
, "+");
2545 shader_addline(&buffer
, "%s", shader_opcode_names
[ins
.handler_idx
]);
2547 if (ins
.handler_idx
== WINED3DSIH_BREAKP
2548 || ins
.handler_idx
== WINED3DSIH_IF
)
2552 case WINED3D_SHADER_CONDITIONAL_OP_NZ
: shader_addline(&buffer
, "_nz"); break;
2553 case WINED3D_SHADER_CONDITIONAL_OP_Z
: shader_addline(&buffer
, "_z"); break;
2554 default: shader_addline(&buffer
, "_unrecognized(%#x)", ins
.flags
); break;
2557 else if (ins
.handler_idx
== WINED3DSIH_IFC
2558 || ins
.handler_idx
== WINED3DSIH_BREAKC
)
2562 case WINED3D_SHADER_REL_OP_GT
: shader_addline(&buffer
, "_gt"); break;
2563 case WINED3D_SHADER_REL_OP_EQ
: shader_addline(&buffer
, "_eq"); break;
2564 case WINED3D_SHADER_REL_OP_GE
: shader_addline(&buffer
, "_ge"); break;
2565 case WINED3D_SHADER_REL_OP_LT
: shader_addline(&buffer
, "_lt"); break;
2566 case WINED3D_SHADER_REL_OP_NE
: shader_addline(&buffer
, "_ne"); break;
2567 case WINED3D_SHADER_REL_OP_LE
: shader_addline(&buffer
, "_le"); break;
2568 default: shader_addline(&buffer
, "_(%u)", ins
.flags
);
2571 else if (ins
.handler_idx
== WINED3DSIH_TEX
2572 && shader_version
.major
>= 2
2573 && (ins
.flags
& WINED3DSI_TEXLD_PROJECT
))
2575 shader_addline(&buffer
, "p");
2577 else if (ins
.handler_idx
== WINED3DSIH_RESINFO
&& ins
.flags
)
2581 case WINED3DSI_RESINFO_RCP_FLOAT
: shader_addline(&buffer
, "_rcpFloat"); break;
2582 case WINED3DSI_RESINFO_UINT
: shader_addline(&buffer
, "_uint"); break;
2583 default: shader_addline(&buffer
, "_unrecognized(%#x)", ins
.flags
);
2586 else if (ins
.handler_idx
== WINED3DSIH_SAMPLE_INFO
&& ins
.flags
)
2590 case WINED3DSI_SAMPLE_INFO_UINT
: shader_addline(&buffer
, "_uint"); break;
2591 default: shader_addline(&buffer
, "_unrecognized(%#x)", ins
.flags
);
2594 else if (ins
.handler_idx
== WINED3DSIH_SYNC
)
2596 shader_dump_sync_flags(&buffer
, ins
.flags
);
2599 if (wined3d_shader_instruction_has_texel_offset(&ins
))
2600 shader_addline(&buffer
, "(%d,%d,%d)", ins
.texel_offset
.u
, ins
.texel_offset
.v
, ins
.texel_offset
.w
);
2602 for (i
= 0; i
< ins
.dst_count
; ++i
)
2604 shader_dump_ins_modifiers(&buffer
, &ins
.dst
[i
]);
2605 shader_addline(&buffer
, !i
? " " : ", ");
2606 shader_dump_dst_param(&buffer
, &ins
.dst
[i
], &shader_version
);
2609 /* Other source tokens */
2610 for (i
= ins
.dst_count
; i
< (ins
.dst_count
+ ins
.src_count
); ++i
)
2612 shader_addline(&buffer
, !i
? " " : ", ");
2613 shader_dump_src_param(&buffer
, &ins
.src
[i
- ins
.dst_count
], &shader_version
);
2616 shader_addline(&buffer
, "\n");
2619 for (p
= buffer
.buffer
; *p
; p
= q
)
2621 if (!(q
= strstr(p
, "\n")))
2625 TRACE(" %.*s", (int)(q
- p
), p
);
2628 string_buffer_free(&buffer
);
2631 static void shader_cleanup(struct wined3d_shader
*shader
)
2633 HeapFree(GetProcessHeap(), 0, shader
->output_signature
.elements
);
2634 HeapFree(GetProcessHeap(), 0, shader
->input_signature
.elements
);
2635 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
2636 shader
->device
->shader_backend
->shader_destroy(shader
);
2637 shader_cleanup_reg_maps(&shader
->reg_maps
);
2638 HeapFree(GetProcessHeap(), 0, shader
->function
);
2639 shader_delete_constant_list(&shader
->constantsF
);
2640 shader_delete_constant_list(&shader
->constantsB
);
2641 shader_delete_constant_list(&shader
->constantsI
);
2642 list_remove(&shader
->shader_list_entry
);
2644 if (shader
->frontend
&& shader
->frontend_data
)
2645 shader
->frontend
->shader_free(shader
->frontend_data
);
2648 struct shader_none_priv
2650 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
2651 const struct fragment_pipeline
*fragment_pipe
;
2652 BOOL ffp_proj_control
;
2655 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
2656 static void shader_none_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
2657 enum wined3d_gl_resource_type tex_type
, const SIZE
*ds_mask_size
) {}
2658 static void shader_none_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
) {}
2659 static void shader_none_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
2660 static void shader_none_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
2661 static void shader_none_load_constants(void *shader_priv
, struct wined3d_context
*context
,
2662 const struct wined3d_state
*state
) {}
2663 static void shader_none_destroy(struct wined3d_shader
*shader
) {}
2664 static void shader_none_free_context_data(struct wined3d_context
*context
) {}
2665 static void shader_none_init_context_state(struct wined3d_context
*context
) {}
2667 /* Context activation is done by the caller. */
2668 static void shader_none_select(void *shader_priv
, struct wined3d_context
*context
,
2669 const struct wined3d_state
*state
)
2671 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2672 struct shader_none_priv
*priv
= shader_priv
;
2674 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
2675 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
2678 /* Context activation is done by the caller. */
2679 static void shader_none_disable(void *shader_priv
, struct wined3d_context
*context
)
2681 struct shader_none_priv
*priv
= shader_priv
;
2682 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2684 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
2685 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
2687 context
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
2688 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
2689 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
2690 | (1u << WINED3D_SHADER_TYPE_HULL
)
2691 | (1u << WINED3D_SHADER_TYPE_DOMAIN
)
2692 | (1u << WINED3D_SHADER_TYPE_COMPUTE
);
2695 static HRESULT
shader_none_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
2696 const struct fragment_pipeline
*fragment_pipe
)
2698 struct fragment_caps fragment_caps
;
2699 void *vertex_priv
, *fragment_priv
;
2700 struct shader_none_priv
*priv
;
2702 if (!(priv
= HeapAlloc(GetProcessHeap(), 0, sizeof(*priv
))))
2703 return E_OUTOFMEMORY
;
2705 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&none_shader_backend
, priv
)))
2707 ERR("Failed to initialize vertex pipe.\n");
2708 HeapFree(GetProcessHeap(), 0, priv
);
2712 if (!(fragment_priv
= fragment_pipe
->alloc_private(&none_shader_backend
, priv
)))
2714 ERR("Failed to initialize fragment pipe.\n");
2715 vertex_pipe
->vp_free(device
);
2716 HeapFree(GetProcessHeap(), 0, priv
);
2720 priv
->vertex_pipe
= vertex_pipe
;
2721 priv
->fragment_pipe
= fragment_pipe
;
2722 fragment_pipe
->get_caps(&device
->adapter
->gl_info
, &fragment_caps
);
2723 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
2725 device
->vertex_priv
= vertex_priv
;
2726 device
->fragment_priv
= fragment_priv
;
2727 device
->shader_priv
= priv
;
2732 static void shader_none_free(struct wined3d_device
*device
)
2734 struct shader_none_priv
*priv
= device
->shader_priv
;
2736 priv
->fragment_pipe
->free_private(device
);
2737 priv
->vertex_pipe
->vp_free(device
);
2738 HeapFree(GetProcessHeap(), 0, priv
);
2741 static BOOL
shader_none_allocate_context_data(struct wined3d_context
*context
)
2746 static void shader_none_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
2748 /* Set the shader caps to 0 for the none shader backend */
2749 caps
->vs_version
= 0;
2750 caps
->hs_version
= 0;
2751 caps
->ds_version
= 0;
2752 caps
->gs_version
= 0;
2753 caps
->ps_version
= 0;
2754 caps
->cs_version
= 0;
2755 caps
->vs_uniform_count
= 0;
2756 caps
->ps_uniform_count
= 0;
2757 caps
->ps_1x_max_value
= 0.0f
;
2758 caps
->varying_count
= 0;
2759 caps
->wined3d_caps
= 0;
2762 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
2764 /* We "support" every possible fixup, since we don't support any shader
2765 * model, and will never have to actually sample a texture. */
2769 static BOOL
shader_none_has_ffp_proj_control(void *shader_priv
)
2771 struct shader_none_priv
*priv
= shader_priv
;
2773 return priv
->ffp_proj_control
;
2776 const struct wined3d_shader_backend_ops none_shader_backend
=
2778 shader_none_handle_instruction
,
2780 shader_none_disable
,
2781 shader_none_select_depth_blt
,
2782 shader_none_deselect_depth_blt
,
2783 shader_none_update_float_vertex_constants
,
2784 shader_none_update_float_pixel_constants
,
2785 shader_none_load_constants
,
2786 shader_none_destroy
,
2789 shader_none_allocate_context_data
,
2790 shader_none_free_context_data
,
2791 shader_none_init_context_state
,
2792 shader_none_get_caps
,
2793 shader_none_color_fixup_supported
,
2794 shader_none_has_ffp_proj_control
,
2797 static HRESULT
shader_set_function(struct wined3d_shader
*shader
, const DWORD
*byte_code
,
2798 DWORD float_const_count
, enum wined3d_shader_type type
, unsigned int max_version
)
2800 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2801 const struct wined3d_shader_frontend
*fe
;
2803 unsigned int backend_version
;
2804 const struct wined3d_d3d_info
*d3d_info
= &shader
->device
->adapter
->d3d_info
;
2806 TRACE("shader %p, byte_code %p, float_const_count %u, type %#x, max_version %u.\n",
2807 shader
, byte_code
, float_const_count
, type
, max_version
);
2809 list_init(&shader
->constantsF
);
2810 list_init(&shader
->constantsB
);
2811 list_init(&shader
->constantsI
);
2812 shader
->lconst_inf_or_nan
= FALSE
;
2813 list_init(®_maps
->indexable_temps
);
2815 fe
= shader_select_frontend(*byte_code
);
2818 FIXME("Unable to find frontend for shader.\n");
2819 return WINED3DERR_INVALIDCALL
;
2821 shader
->frontend
= fe
;
2822 shader
->frontend_data
= fe
->shader_init(byte_code
, &shader
->output_signature
);
2823 if (!shader
->frontend_data
)
2825 FIXME("Failed to initialize frontend.\n");
2826 return WINED3DERR_INVALIDCALL
;
2829 /* First pass: trace shader. */
2830 if (TRACE_ON(d3d_shader
))
2831 shader_trace_init(fe
, shader
->frontend_data
, byte_code
);
2833 /* Second pass: figure out which registers are used, what the semantics are, etc. */
2834 if (FAILED(hr
= shader_get_registers_used(shader
, fe
, reg_maps
, &shader
->input_signature
,
2835 &shader
->output_signature
, byte_code
, float_const_count
)))
2838 if (reg_maps
->shader_version
.type
!= type
)
2840 WARN("Wrong shader type %d.\n", reg_maps
->shader_version
.type
);
2841 return WINED3DERR_INVALIDCALL
;
2843 if (reg_maps
->shader_version
.major
> max_version
)
2845 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps
->shader_version
.major
);
2846 return WINED3DERR_INVALIDCALL
;
2850 case WINED3D_SHADER_TYPE_VERTEX
:
2851 backend_version
= d3d_info
->limits
.vs_version
;
2853 case WINED3D_SHADER_TYPE_HULL
:
2854 backend_version
= d3d_info
->limits
.hs_version
;
2856 case WINED3D_SHADER_TYPE_DOMAIN
:
2857 backend_version
= d3d_info
->limits
.ds_version
;
2859 case WINED3D_SHADER_TYPE_GEOMETRY
:
2860 backend_version
= d3d_info
->limits
.gs_version
;
2862 case WINED3D_SHADER_TYPE_PIXEL
:
2863 backend_version
= d3d_info
->limits
.ps_version
;
2865 case WINED3D_SHADER_TYPE_COMPUTE
:
2866 backend_version
= d3d_info
->limits
.cs_version
;
2869 FIXME("No backend version-checking for this shader type.\n");
2870 backend_version
= 0;
2872 if (reg_maps
->shader_version
.major
> backend_version
)
2874 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
2875 reg_maps
->shader_version
.major
, reg_maps
->shader_version
.minor
);
2876 return WINED3DERR_INVALIDCALL
;
2879 if (!(shader
->function
= HeapAlloc(GetProcessHeap(), 0, shader
->functionLength
)))
2880 return E_OUTOFMEMORY
;
2881 memcpy(shader
->function
, byte_code
, shader
->functionLength
);
2886 ULONG CDECL
wined3d_shader_incref(struct wined3d_shader
*shader
)
2888 ULONG refcount
= InterlockedIncrement(&shader
->ref
);
2890 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
2895 static void wined3d_shader_destroy_object(void *object
)
2897 shader_cleanup(object
);
2898 HeapFree(GetProcessHeap(), 0, object
);
2901 ULONG CDECL
wined3d_shader_decref(struct wined3d_shader
*shader
)
2903 ULONG refcount
= InterlockedDecrement(&shader
->ref
);
2905 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
2909 shader
->parent_ops
->wined3d_object_destroyed(shader
->parent
);
2910 wined3d_cs_emit_destroy_object(shader
->device
->cs
, wined3d_shader_destroy_object
, shader
);
2916 void * CDECL
wined3d_shader_get_parent(const struct wined3d_shader
*shader
)
2918 TRACE("shader %p.\n", shader
);
2920 return shader
->parent
;
2923 HRESULT CDECL
wined3d_shader_get_byte_code(const struct wined3d_shader
*shader
,
2924 void *byte_code
, UINT
*byte_code_size
)
2926 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader
, byte_code
, byte_code_size
);
2930 *byte_code_size
= shader
->functionLength
;
2934 if (*byte_code_size
< shader
->functionLength
)
2936 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
2937 * than the required size we should write the required size and
2938 * return D3DERR_MOREDATA. That's not actually true. */
2939 return WINED3DERR_INVALIDCALL
;
2942 memcpy(byte_code
, shader
->function
, shader
->functionLength
);
2947 /* Set local constants for d3d8 shaders. */
2948 HRESULT CDECL
wined3d_shader_set_local_constants_float(struct wined3d_shader
*shader
,
2949 UINT start_idx
, const float *src_data
, UINT count
)
2951 UINT end_idx
= start_idx
+ count
;
2954 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader
, start_idx
, src_data
, count
);
2956 if (end_idx
> shader
->limits
->constant_float
)
2958 WARN("end_idx %u > float constants limit %u.\n",
2959 end_idx
, shader
->limits
->constant_float
);
2960 end_idx
= shader
->limits
->constant_float
;
2963 for (i
= start_idx
; i
< end_idx
; ++i
)
2965 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
2968 return E_OUTOFMEMORY
;
2971 value
= (float *)lconst
->value
;
2972 memcpy(value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
2973 list_add_head(&shader
->constantsF
, &lconst
->entry
);
2975 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
2976 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
2978 shader
->lconst_inf_or_nan
= TRUE
;
2985 void find_vs_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2986 WORD swizzle_map
, struct vs_compile_args
*args
, const struct wined3d_d3d_info
*d3d_info
)
2988 args
->fog_src
= state
->render_states
[WINED3D_RS_FOGTABLEMODE
]
2989 == WINED3D_FOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
2990 args
->clip_enabled
= state
->render_states
[WINED3D_RS_CLIPPING
]
2991 && state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
];
2992 args
->point_size
= state
->gl_primitive_type
== GL_POINTS
;
2993 args
->per_vertex_point_size
= shader
->reg_maps
.point_size
;
2994 args
->next_shader_type
= state
->shader
[WINED3D_SHADER_TYPE_HULL
] ? WINED3D_SHADER_TYPE_HULL
2995 : state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
] ? WINED3D_SHADER_TYPE_GEOMETRY
2996 : WINED3D_SHADER_TYPE_PIXEL
;
2997 if (shader
->reg_maps
.shader_version
.major
>= 4)
2998 args
->next_shader_input_count
= state
->shader
[WINED3D_SHADER_TYPE_HULL
]
2999 ? state
->shader
[WINED3D_SHADER_TYPE_HULL
]->limits
->packed_input
3000 : state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
]
3001 ? state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
]->limits
->packed_input
3002 : state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]
3003 ? state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->limits
->packed_input
: 0;
3005 args
->next_shader_input_count
= 0;
3006 args
->swizzle_map
= swizzle_map
;
3007 if (d3d_info
->emulated_flatshading
)
3008 args
->flatshading
= state
->render_states
[WINED3D_RS_SHADEMODE
] == WINED3D_SHADE_FLAT
;
3010 args
->flatshading
= 0;
3013 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
3015 if (usage_idx1
!= usage_idx2
)
3017 if (usage1
== usage2
)
3019 if (usage1
== WINED3D_DECL_USAGE_POSITION
&& usage2
== WINED3D_DECL_USAGE_POSITIONT
)
3021 if (usage2
== WINED3D_DECL_USAGE_POSITION
&& usage1
== WINED3D_DECL_USAGE_POSITIONT
)
3027 BOOL
vshader_get_input(const struct wined3d_shader
*shader
,
3028 BYTE usage_req
, BYTE usage_idx_req
, unsigned int *regnum
)
3030 WORD map
= shader
->reg_maps
.input_registers
;
3033 for (i
= 0; map
; map
>>= 1, ++i
)
3035 if (!(map
& 1)) continue;
3037 if (match_usage(shader
->u
.vs
.attributes
[i
].usage
,
3038 shader
->u
.vs
.attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
3047 static HRESULT
shader_signature_copy(struct wined3d_shader_signature
*dst
,
3048 const struct wined3d_shader_signature
*src
, char **signature_strings
)
3050 struct wined3d_shader_signature_element
*e
;
3055 if (!src
->element_count
)
3058 ptr
= *signature_strings
;
3060 dst
->element_count
= src
->element_count
;
3061 if (!(dst
->elements
= wined3d_calloc(dst
->element_count
, sizeof(*dst
->elements
))))
3062 return E_OUTOFMEMORY
;
3064 for (i
= 0; i
< src
->element_count
; ++i
)
3066 e
= &src
->elements
[i
];
3067 dst
->elements
[i
] = *e
;
3069 len
= strlen(e
->semantic_name
);
3070 memcpy(ptr
, e
->semantic_name
, len
+ 1);
3071 dst
->elements
[i
].semantic_name
= ptr
;
3075 *signature_strings
= ptr
;
3080 static HRESULT
shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3081 const struct wined3d_shader_desc
*desc
, DWORD float_const_count
, enum wined3d_shader_type type
,
3082 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3084 struct wined3d_shader_signature_element
*e
;
3090 if (!desc
->byte_code
)
3091 return WINED3DERR_INVALIDCALL
;
3094 shader
->device
= device
;
3095 shader
->parent
= parent
;
3096 shader
->parent_ops
= parent_ops
;
3099 for (i
= 0; i
< desc
->input_signature
.element_count
; ++i
)
3101 e
= &desc
->input_signature
.elements
[i
];
3102 len
= strlen(e
->semantic_name
);
3103 if (len
>= ~(SIZE_T
)0 - total
)
3104 return E_OUTOFMEMORY
;
3108 for (i
= 0; i
< desc
->output_signature
.element_count
; ++i
)
3110 e
= &desc
->output_signature
.elements
[i
];
3111 len
= strlen(e
->semantic_name
);
3112 if (len
>= ~(SIZE_T
)0 - total
)
3113 return E_OUTOFMEMORY
;
3117 if (total
&& !(shader
->signature_strings
= HeapAlloc(GetProcessHeap(), 0, total
)))
3118 return E_OUTOFMEMORY
;
3119 ptr
= shader
->signature_strings
;
3121 if (FAILED(hr
= shader_signature_copy(&shader
->input_signature
, &desc
->input_signature
, &ptr
)))
3123 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
3126 if (FAILED(hr
= shader_signature_copy(&shader
->output_signature
, &desc
->output_signature
, &ptr
)))
3128 HeapFree(GetProcessHeap(), 0, shader
->input_signature
.elements
);
3129 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
3133 list_init(&shader
->linked_programs
);
3134 list_add_head(&device
->shaders
, &shader
->shader_list_entry
);
3136 if (FAILED(hr
= shader_set_function(shader
, desc
->byte_code
,
3137 float_const_count
, type
, desc
->max_version
)))
3139 WARN("Failed to set function, hr %#x.\n", hr
);
3140 shader_cleanup(shader
);
3143 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
3148 static HRESULT
vertex_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3149 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3151 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
3155 if (FAILED(hr
= shader_init(shader
, device
, desc
, device
->adapter
->d3d_info
.limits
.vs_uniform_count
,
3156 WINED3D_SHADER_TYPE_VERTEX
, parent
, parent_ops
)))
3159 for (i
= 0; i
< shader
->input_signature
.element_count
; ++i
)
3161 const struct wined3d_shader_signature_element
*input
= &shader
->input_signature
.elements
[i
];
3163 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)) || !input
->semantic_name
)
3166 shader
->u
.vs
.attributes
[input
->register_idx
].usage
=
3167 shader_usage_from_semantic_name(input
->semantic_name
);
3168 shader
->u
.vs
.attributes
[input
->register_idx
].usage_idx
= input
->semantic_idx
;
3171 if (reg_maps
->usesrelconstF
&& !list_empty(&shader
->constantsF
))
3172 shader
->load_local_constsF
= TRUE
;
3177 static HRESULT
domain_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3178 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3180 return shader_init(shader
, device
, desc
, 0, WINED3D_SHADER_TYPE_DOMAIN
, parent
, parent_ops
);
3183 static HRESULT
hull_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3184 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3186 return shader_init(shader
, device
, desc
, 0, WINED3D_SHADER_TYPE_HULL
, parent
, parent_ops
);
3189 static HRESULT
geometry_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3190 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3192 return shader_init(shader
, device
, desc
, 0, WINED3D_SHADER_TYPE_GEOMETRY
, parent
, parent_ops
);
3195 void find_gs_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
3196 struct gs_compile_args
*args
)
3198 args
->ps_input_count
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]
3199 ? state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->limits
->packed_input
: 0;
3202 void find_ps_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
3203 BOOL position_transformed
, struct ps_compile_args
*args
, const struct wined3d_context
*context
)
3205 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3206 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3207 const struct wined3d_texture
*texture
;
3210 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
3211 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && needs_srgb_write(context
, state
, state
->fb
))
3213 static unsigned int warned
= 0;
3215 args
->srgb_correction
= 1;
3216 if (state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] && !warned
++)
3217 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
3218 "support, expect rendering artifacts.\n");
3221 if (shader
->reg_maps
.shader_version
.major
== 1
3222 && shader
->reg_maps
.shader_version
.minor
<= 3)
3224 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
3226 DWORD flags
= state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
];
3228 if (flags
& WINED3D_TTFF_PROJECTED
)
3230 DWORD tex_transform
= flags
& ~WINED3D_TTFF_PROJECTED
;
3232 if (!state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
3234 enum wined3d_shader_resource_type resource_type
= shader
->reg_maps
.resource_info
[i
].type
;
3236 unsigned int index
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
3237 DWORD max_valid
= WINED3D_TTFF_COUNT4
;
3239 for (j
= 0; j
< state
->vertex_declaration
->element_count
; ++j
)
3241 struct wined3d_vertex_declaration_element
*element
=
3242 &state
->vertex_declaration
->elements
[j
];
3244 if (element
->usage
== WINED3D_DECL_USAGE_TEXCOORD
3245 && element
->usage_idx
== index
)
3247 max_valid
= element
->format
->component_count
;
3251 if (!tex_transform
|| tex_transform
> max_valid
)
3253 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
3254 tex_transform
, max_valid
);
3255 tex_transform
= max_valid
;
3257 if ((resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_1D
&& tex_transform
> WINED3D_TTFF_COUNT1
)
3258 || (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2D
3259 && tex_transform
> WINED3D_TTFF_COUNT2
)
3260 || (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_3D
3261 && tex_transform
> WINED3D_TTFF_COUNT3
))
3262 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
3265 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
3266 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
3267 i
, tex_transform
, resource_type
);
3271 tex_transform
= WINED3D_TTFF_COUNT4
| WINED3D_PSARGS_PROJECTED
;
3273 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
3277 if (shader
->reg_maps
.shader_version
.major
== 1
3278 && shader
->reg_maps
.shader_version
.minor
<= 4)
3280 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
3282 const struct wined3d_texture
*texture
= state
->textures
[i
];
3284 if (!shader
->reg_maps
.resource_info
[i
].type
)
3287 /* Treat unbound textures as 2D. The dummy texture will provide
3288 * the proper sample value. The tex_types bitmap defaults to
3289 * 2D because of the memset. */
3293 switch (texture
->target
)
3295 /* RECT textures are distinguished from 2D textures via np2_fixup */
3296 case GL_TEXTURE_RECTANGLE_ARB
:
3301 args
->tex_types
|= WINED3D_SHADER_TEX_3D
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
3304 case GL_TEXTURE_CUBE_MAP_ARB
:
3305 args
->tex_types
|= WINED3D_SHADER_TEX_CUBE
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
3311 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3313 if (!shader
->reg_maps
.resource_info
[i
].type
)
3316 texture
= state
->textures
[i
];
3319 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
3322 if (can_use_texture_swizzle(gl_info
, texture
->resource
.format
))
3323 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
3325 args
->color_fixup
[i
] = texture
->resource
.format
->color_fixup
;
3327 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_SHADOW
)
3328 args
->shadow
|= 1u << i
;
3330 /* Flag samplers that need NP2 texcoord fixup. */
3331 if (!(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
3332 args
->np2_fixup
|= (1u << i
);
3335 /* In SM4+ we use dcl_sampler in order to determine if we should use shadow sampler. */
3336 if (shader
->reg_maps
.shader_version
.major
>= 4)
3339 if (shader
->reg_maps
.shader_version
.major
>= 3)
3341 if (position_transformed
)
3342 args
->vp_mode
= pretransformed
;
3343 else if (use_vs(state
))
3344 args
->vp_mode
= vertexshader
;
3346 args
->vp_mode
= fixedfunction
;
3347 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
3351 args
->vp_mode
= vertexshader
;
3352 if (state
->render_states
[WINED3D_RS_FOGENABLE
])
3354 switch (state
->render_states
[WINED3D_RS_FOGTABLEMODE
])
3356 case WINED3D_FOG_NONE
:
3357 if (position_transformed
|| use_vs(state
))
3359 args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
;
3363 switch (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
])
3365 case WINED3D_FOG_NONE
: /* Fall through. */
3366 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
3367 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
3368 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
3372 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
3373 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
3374 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
3379 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
3383 if (context
->d3d_info
->limits
.varying_count
< wined3d_max_compat_varyings(context
->gl_info
))
3385 const struct wined3d_shader
*vs
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
3387 args
->texcoords_initialized
= 0;
3388 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
3392 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.output_registers
& (1u << i
))
3393 args
->texcoords_initialized
|= 1u << i
;
3397 const struct wined3d_stream_info
*si
= &context
->stream_info
;
3398 unsigned int coord_idx
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
3400 if ((state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
] >> WINED3D_FFP_TCI_SHIFT
)
3401 & WINED3D_FFP_TCI_MASK
3402 || (coord_idx
< MAX_TEXTURES
&& (si
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ coord_idx
)))))
3403 args
->texcoords_initialized
|= 1u << i
;
3409 args
->texcoords_initialized
= (1u << MAX_TEXTURES
) - 1;
3412 args
->pointsprite
= state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
]
3413 && state
->gl_primitive_type
== GL_POINTS
;
3415 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
3416 args
->alpha_test_func
= WINED3D_CMP_ALWAYS
- 1;
3418 args
->alpha_test_func
= (state
->render_states
[WINED3D_RS_ALPHATESTENABLE
]
3419 ? wined3d_sanitize_cmp_func(state
->render_states
[WINED3D_RS_ALPHAFUNC
])
3420 : WINED3D_CMP_ALWAYS
) - 1;
3422 if (d3d_info
->emulated_flatshading
)
3423 args
->flatshading
= state
->render_states
[WINED3D_RS_SHADEMODE
] == WINED3D_SHADE_FLAT
;
3425 args
->render_offscreen
= shader
->reg_maps
.vpos
&& gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
]
3426 ? context
->render_offscreen
: 0;
3429 static HRESULT
pixel_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3430 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3432 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
3433 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
3436 if (FAILED(hr
= shader_init(shader
, device
, desc
, device
->adapter
->d3d_info
.limits
.ps_uniform_count
,
3437 WINED3D_SHADER_TYPE_PIXEL
, parent
, parent_ops
)))
3440 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
3442 if (shader
->u
.ps
.input_reg_used
[i
])
3445 highest_reg_used
= i
;
3449 /* Don't do any register mapping magic if it is not needed, or if we can't
3450 * achieve anything anyway */
3451 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
3452 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4)
3453 || shader
->reg_maps
.shader_version
.major
>= 4)
3455 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
3457 /* This happens with relative addressing. The input mapper function
3458 * warns about this if the higher registers are declared too, so
3459 * don't write a FIXME here */
3460 WARN("More varying registers used than supported\n");
3463 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
3465 shader
->u
.ps
.input_reg_map
[i
] = i
;
3468 shader
->u
.ps
.declared_in_count
= highest_reg_used
+ 1;
3472 shader
->u
.ps
.declared_in_count
= 0;
3473 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
3475 if (shader
->u
.ps
.input_reg_used
[i
])
3476 shader
->u
.ps
.input_reg_map
[i
] = shader
->u
.ps
.declared_in_count
++;
3477 else shader
->u
.ps
.input_reg_map
[i
] = ~0U;
3484 void pixelshader_update_resource_types(struct wined3d_shader
*shader
, WORD tex_types
)
3486 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
3487 struct wined3d_shader_resource_info
*resource_info
= reg_maps
->resource_info
;
3490 if (reg_maps
->shader_version
.major
!= 1) return;
3492 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
3494 /* We don't sample from this sampler. */
3495 if (!resource_info
[i
].type
)
3498 switch ((tex_types
>> i
* WINED3D_PSARGS_TEXTYPE_SHIFT
) & WINED3D_PSARGS_TEXTYPE_MASK
)
3500 case WINED3D_SHADER_TEX_2D
:
3501 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
3504 case WINED3D_SHADER_TEX_3D
:
3505 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_3D
;
3508 case WINED3D_SHADER_TEX_CUBE
:
3509 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
;
3515 static HRESULT
compute_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3516 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3518 return shader_init(shader
, device
, desc
, 0, WINED3D_SHADER_TYPE_COMPUTE
, parent
, parent_ops
);
3521 HRESULT CDECL
wined3d_shader_create_cs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3522 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3524 struct wined3d_shader
*object
;
3527 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3528 device
, desc
, parent
, parent_ops
, shader
);
3530 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
3531 return E_OUTOFMEMORY
;
3533 if (FAILED(hr
= compute_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3535 WARN("Failed to initialize compute shader, hr %#x.\n", hr
);
3536 HeapFree(GetProcessHeap(), 0, object
);
3540 TRACE("Created compute shader %p.\n", object
);
3546 HRESULT CDECL
wined3d_shader_create_ds(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3547 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3549 struct wined3d_shader
*object
;
3552 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3553 device
, desc
, parent
, parent_ops
, shader
);
3555 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
3556 return E_OUTOFMEMORY
;
3558 if (FAILED(hr
= domain_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3560 WARN("Failed to initialize domain shader, hr %#x.\n", hr
);
3561 HeapFree(GetProcessHeap(), 0, object
);
3565 TRACE("Created domain shader %p.\n", object
);
3571 HRESULT CDECL
wined3d_shader_create_gs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3572 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3574 struct wined3d_shader
*object
;
3577 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3578 device
, desc
, parent
, parent_ops
, shader
);
3580 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
3582 return E_OUTOFMEMORY
;
3584 if (FAILED(hr
= geometry_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3586 WARN("Failed to initialize geometry shader, hr %#x.\n", hr
);
3587 HeapFree(GetProcessHeap(), 0, object
);
3591 TRACE("Created geometry shader %p.\n", object
);
3597 HRESULT CDECL
wined3d_shader_create_hs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3598 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3600 struct wined3d_shader
*object
;
3603 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3604 device
, desc
, parent
, parent_ops
, shader
);
3606 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
3607 return E_OUTOFMEMORY
;
3609 if (FAILED(hr
= hull_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3611 WARN("Failed to initialize hull shader, hr %#x.\n", hr
);
3612 HeapFree(GetProcessHeap(), 0, object
);
3616 TRACE("Created hull shader %p.\n", object
);
3622 HRESULT CDECL
wined3d_shader_create_ps(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3623 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3625 struct wined3d_shader
*object
;
3628 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3629 device
, desc
, parent
, parent_ops
, shader
);
3631 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
3633 return E_OUTOFMEMORY
;
3635 if (FAILED(hr
= pixel_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3637 WARN("Failed to initialize pixel shader, hr %#x.\n", hr
);
3638 HeapFree(GetProcessHeap(), 0, object
);
3642 TRACE("Created pixel shader %p.\n", object
);
3648 HRESULT CDECL
wined3d_shader_create_vs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3649 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3651 struct wined3d_shader
*object
;
3654 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3655 device
, desc
, parent
, parent_ops
, shader
);
3657 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
3659 return E_OUTOFMEMORY
;
3661 if (FAILED(hr
= vertex_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3663 WARN("Failed to initialize vertex shader, hr %#x.\n", hr
);
3664 HeapFree(GetProcessHeap(), 0, object
);
3668 TRACE("Created vertex shader %p.\n", object
);