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_OR */ "atomic_or",
50 /* WINED3DSIH_ATOMIC_UMAX */ "atomic_umax",
51 /* WINED3DSIH_ATOMIC_UMIN */ "atomic_umin",
52 /* WINED3DSIH_ATOMIC_XOR */ "atomic_xor",
53 /* WINED3DSIH_BEM */ "bem",
54 /* WINED3DSIH_BFI */ "bfi",
55 /* WINED3DSIH_BFREV */ "bfrev",
56 /* WINED3DSIH_BREAK */ "break",
57 /* WINED3DSIH_BREAKC */ "breakc",
58 /* WINED3DSIH_BREAKP */ "breakp",
59 /* WINED3DSIH_BUFINFO */ "bufinfo",
60 /* WINED3DSIH_CALL */ "call",
61 /* WINED3DSIH_CALLNZ */ "callnz",
62 /* WINED3DSIH_CASE */ "case",
63 /* WINED3DSIH_CMP */ "cmp",
64 /* WINED3DSIH_CND */ "cnd",
65 /* WINED3DSIH_CONTINUE */ "continue",
66 /* WINED3DSIH_CRS */ "crs",
67 /* WINED3DSIH_CUT */ "cut",
68 /* WINED3DSIH_CUT_STREAM */ "cut_stream",
69 /* WINED3DSIH_DCL */ "dcl",
70 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
71 /* WINED3DSIH_DCL_FUNCTION_BODY */ "dcl_function_body",
72 /* WINED3DSIH_DCL_FUNCTION_TABLE */ "dcl_function_table",
73 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ "dcl_globalFlags",
74 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ "dcl_hs_fork_phase_instance_count",
75 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ "dcl_hs_max_tessfactor",
76 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ "dcl_immediateConstantBuffer",
77 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ "dcl_indexableTemp",
78 /* WINED3DSIH_DCL_INPUT */ "dcl_input",
79 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ "dcl_input_control_point_count",
80 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
81 /* WINED3DSIH_DCL_INPUT_PS */ "dcl_input_ps",
82 /* WINED3DSIH_DCL_INPUT_PS_SGV */ "dcl_input_ps_sgv",
83 /* WINED3DSIH_DCL_INPUT_PS_SIV */ "dcl_input_ps_siv",
84 /* WINED3DSIH_DCL_INPUT_SGV */ "dcl_input_sgv",
85 /* WINED3DSIH_DCL_INPUT_SIV */ "dcl_input_siv",
86 /* WINED3DSIH_DCL_INTERFACE */ "dcl_interface",
87 /* WINED3DSIH_DCL_OUTPUT */ "dcl_output",
88 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ "dcl_output_control_point_count",
89 /* WINED3DSIH_DCL_OUTPUT_SIV */ "dcl_output_siv",
90 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
91 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ "dcl_resource_structured",
92 /* WINED3DSIH_DCL_SAMPLER */ "dcl_sampler",
93 /* WINED3DSIH_DCL_STREAM */ "dcl_stream",
94 /* WINED3DSIH_DCL_TEMPS */ "dcl_temps",
95 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ "dcl_tessellator_domain",
96 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ "dcl_tessellator_output_primitive",
97 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ "dcl_tessellator_partitioning",
98 /* WINED3DSIH_DCL_TGSM_RAW */ "dcl_tgsm_raw",
99 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ "dcl_tgsm_structured",
100 /* WINED3DSIH_DCL_THREAD_GROUP */ "dcl_thread_group",
101 /* WINED3DSIH_DCL_UAV_RAW */ "dcl_uav_raw",
102 /* WINED3DSIH_DCL_UAV_STRUCTURED */ "dcl_uav_structured",
103 /* WINED3DSIH_DCL_UAV_TYPED */ "dcl_uav_typed",
104 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
105 /* WINED3DSIH_DEF */ "def",
106 /* WINED3DSIH_DEFAULT */ "default",
107 /* WINED3DSIH_DEFB */ "defb",
108 /* WINED3DSIH_DEFI */ "defi",
109 /* WINED3DSIH_DIV */ "div",
110 /* WINED3DSIH_DP2 */ "dp2",
111 /* WINED3DSIH_DP2ADD */ "dp2add",
112 /* WINED3DSIH_DP3 */ "dp3",
113 /* WINED3DSIH_DP4 */ "dp4",
114 /* WINED3DSIH_DST */ "dst",
115 /* WINED3DSIH_DSX */ "dsx",
116 /* WINED3DSIH_DSX_COARSE */ "deriv_rtx_coarse",
117 /* WINED3DSIH_DSX_FINE */ "deriv_rtx_fine",
118 /* WINED3DSIH_DSY */ "dsy",
119 /* WINED3DSIH_DSY_COARSE */ "deriv_rty_coarse",
120 /* WINED3DSIH_DSY_FINE */ "deriv_rty_fine",
121 /* WINED3DSIH_ELSE */ "else",
122 /* WINED3DSIH_EMIT */ "emit",
123 /* WINED3DSIH_EMIT_STREAM */ "emit_stream",
124 /* WINED3DSIH_ENDIF */ "endif",
125 /* WINED3DSIH_ENDLOOP */ "endloop",
126 /* WINED3DSIH_ENDREP */ "endrep",
127 /* WINED3DSIH_ENDSWITCH */ "endswitch",
128 /* WINED3DSIH_EQ */ "eq",
129 /* WINED3DSIH_EXP */ "exp",
130 /* WINED3DSIH_EXPP */ "expp",
131 /* WINED3DSIH_FCALL */ "fcall",
132 /* WINED3DSIH_FRC */ "frc",
133 /* WINED3DSIH_FTOI */ "ftoi",
134 /* WINED3DSIH_FTOU */ "ftou",
135 /* WINED3DSIH_GATHER4 */ "gather4",
136 /* WINED3DSIH_GATHER4_C */ "gather4_c",
137 /* WINED3DSIH_GE */ "ge",
138 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ "hs_control_point_phase",
139 /* WINED3DSIH_HS_DECLS */ "hs_decls",
140 /* WINED3DSIH_HS_FORK_PHASE */ "hs_fork_phase",
141 /* WINED3DSIH_HS_JOIN_PHASE */ "hs_join_phase",
142 /* WINED3DSIH_IADD */ "iadd",
143 /* WINED3DSIH_IEQ */ "ieq",
144 /* WINED3DSIH_IF */ "if",
145 /* WINED3DSIH_IFC */ "ifc",
146 /* WINED3DSIH_IGE */ "ige",
147 /* WINED3DSIH_ILT */ "ilt",
148 /* WINED3DSIH_IMAD */ "imad",
149 /* WINED3DSIH_IMAX */ "imax",
150 /* WINED3DSIH_IMIN */ "imin",
151 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ "imm_atomic_alloc",
152 /* WINED3DSIH_IMM_ATOMIC_AND */ "imm_atomic_and",
153 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ "imm_atomic_cmp_exch",
154 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ "imm_atomic_consume",
155 /* WINED3DSIH_IMM_ATOMIC_EXCH */ "imm_atomic_exch",
156 /* WINED3DSIH_IMM_ATOMIC_OR */ "imm_atomic_or",
157 /* WINED3DSIH_IMM_ATOMIC_UMAX */ "imm_atomic_umax",
158 /* WINED3DSIH_IMM_ATOMIC_UMIN */ "imm_atomic_umin",
159 /* WINED3DSIH_IMM_ATOMIC_XOR */ "imm_atomic_xor",
160 /* WINED3DSIH_IMUL */ "imul",
161 /* WINED3DSIH_INE */ "ine",
162 /* WINED3DSIH_INEG */ "ineg",
163 /* WINED3DSIH_ISHL */ "ishl",
164 /* WINED3DSIH_ISHR */ "ishr",
165 /* WINED3DSIH_ITOF */ "itof",
166 /* WINED3DSIH_LABEL */ "label",
167 /* WINED3DSIH_LD */ "ld",
168 /* WINED3DSIH_LD2DMS */ "ld2dms",
169 /* WINED3DSIH_LD_RAW */ "ld_raw",
170 /* WINED3DSIH_LD_STRUCTURED */ "ld_structured",
171 /* WINED3DSIH_LD_UAV_TYPED */ "ld_uav_typed",
172 /* WINED3DSIH_LIT */ "lit",
173 /* WINED3DSIH_LOD */ "lod",
174 /* WINED3DSIH_LOG */ "log",
175 /* WINED3DSIH_LOGP */ "logp",
176 /* WINED3DSIH_LOOP */ "loop",
177 /* WINED3DSIH_LRP */ "lrp",
178 /* WINED3DSIH_LT */ "lt",
179 /* WINED3DSIH_M3x2 */ "m3x2",
180 /* WINED3DSIH_M3x3 */ "m3x3",
181 /* WINED3DSIH_M3x4 */ "m3x4",
182 /* WINED3DSIH_M4x3 */ "m4x3",
183 /* WINED3DSIH_M4x4 */ "m4x4",
184 /* WINED3DSIH_MAD */ "mad",
185 /* WINED3DSIH_MAX */ "max",
186 /* WINED3DSIH_MIN */ "min",
187 /* WINED3DSIH_MOV */ "mov",
188 /* WINED3DSIH_MOVA */ "mova",
189 /* WINED3DSIH_MOVC */ "movc",
190 /* WINED3DSIH_MUL */ "mul",
191 /* WINED3DSIH_NE */ "ne",
192 /* WINED3DSIH_NOP */ "nop",
193 /* WINED3DSIH_NOT */ "not",
194 /* WINED3DSIH_NRM */ "nrm",
195 /* WINED3DSIH_OR */ "or",
196 /* WINED3DSIH_PHASE */ "phase",
197 /* WINED3DSIH_POW */ "pow",
198 /* WINED3DSIH_RCP */ "rcp",
199 /* WINED3DSIH_REP */ "rep",
200 /* WINED3DSIH_RESINFO */ "resinfo",
201 /* WINED3DSIH_RET */ "ret",
202 /* WINED3DSIH_ROUND_NE */ "round_ne",
203 /* WINED3DSIH_ROUND_NI */ "round_ni",
204 /* WINED3DSIH_ROUND_PI */ "round_pi",
205 /* WINED3DSIH_ROUND_Z */ "round_z",
206 /* WINED3DSIH_RSQ */ "rsq",
207 /* WINED3DSIH_SAMPLE */ "sample",
208 /* WINED3DSIH_SAMPLE_B */ "sample_b",
209 /* WINED3DSIH_SAMPLE_C */ "sample_c",
210 /* WINED3DSIH_SAMPLE_C_LZ */ "sample_c_lz",
211 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
212 /* WINED3DSIH_SAMPLE_INFO */ "sample_info",
213 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
214 /* WINED3DSIH_SAMPLE_POS */ "sample_pos",
215 /* WINED3DSIH_SETP */ "setp",
216 /* WINED3DSIH_SGE */ "sge",
217 /* WINED3DSIH_SGN */ "sgn",
218 /* WINED3DSIH_SINCOS */ "sincos",
219 /* WINED3DSIH_SLT */ "slt",
220 /* WINED3DSIH_SQRT */ "sqrt",
221 /* WINED3DSIH_STORE_RAW */ "store_raw",
222 /* WINED3DSIH_STORE_STRUCTURED */ "store_structured",
223 /* WINED3DSIH_STORE_UAV_TYPED */ "store_uav_typed",
224 /* WINED3DSIH_SUB */ "sub",
225 /* WINED3DSIH_SWAPC */ "swapc",
226 /* WINED3DSIH_SWITCH */ "switch",
227 /* WINED3DSIH_SYNC */ "sync",
228 /* WINED3DSIH_TEX */ "texld",
229 /* WINED3DSIH_TEXBEM */ "texbem",
230 /* WINED3DSIH_TEXBEML */ "texbeml",
231 /* WINED3DSIH_TEXCOORD */ "texcrd",
232 /* WINED3DSIH_TEXDEPTH */ "texdepth",
233 /* WINED3DSIH_TEXDP3 */ "texdp3",
234 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
235 /* WINED3DSIH_TEXKILL */ "texkill",
236 /* WINED3DSIH_TEXLDD */ "texldd",
237 /* WINED3DSIH_TEXLDL */ "texldl",
238 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
239 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
240 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
241 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
242 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
243 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
244 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
245 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
246 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
247 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
248 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
249 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
250 /* WINED3DSIH_UBFE */ "ubfe",
251 /* WINED3DSIH_UDIV */ "udiv",
252 /* WINED3DSIH_UGE */ "uge",
253 /* WINED3DSIH_ULT */ "ult",
254 /* WINED3DSIH_UMAX */ "umax",
255 /* WINED3DSIH_UMIN */ "umin",
256 /* WINED3DSIH_USHR */ "ushr",
257 /* WINED3DSIH_UTOF */ "utof",
258 /* WINED3DSIH_XOR */ "xor",
261 static const char * const semantic_names
[] =
263 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
264 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
265 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
266 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
267 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
268 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
269 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
270 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
271 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
272 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
273 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
274 /* WINED3D_DECL_USAGE_FOG */ "FOG",
275 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
276 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
281 enum wined3d_sysval_semantic sysval_semantic
;
282 const char *sysval_name
;
284 sysval_semantic_names
[] =
286 {WINED3D_SV_POSITION
, "SV_Position"},
287 {WINED3D_SV_CLIP_DISTANCE
, "SV_ClipDistance"},
288 {WINED3D_SV_CULL_DISTANCE
, "SV_CullDistance"},
289 {WINED3D_SV_RENDER_TARGET_ARRAY_INDEX
, "SV_RenderTargetArrayIndex"},
290 {WINED3D_SV_VIEWPORT_ARRAY_INDEX
, "SV_ViewportArrayIndex"},
291 {WINED3D_SV_VERTEX_ID
, "SV_VertexID"},
292 {WINED3D_SV_INSTANCE_ID
, "SV_InstanceID"},
293 {WINED3D_SV_PRIMITIVE_ID
, "SV_PrimitiveID"},
294 {WINED3D_SV_IS_FRONT_FACE
, "SV_IsFrontFace"},
295 {WINED3D_SV_SAMPLE_INDEX
, "SV_SampleIndex"},
296 {WINED3D_SV_QUAD_U0_TESS_FACTOR
, "finalQuadUeq0EdgeTessFactor"},
297 {WINED3D_SV_QUAD_V0_TESS_FACTOR
, "finalQuadVeq0EdgeTessFactor"},
298 {WINED3D_SV_QUAD_U1_TESS_FACTOR
, "finalQuadUeq1EdgeTessFactor"},
299 {WINED3D_SV_QUAD_V1_TESS_FACTOR
, "finalQuadVeq1EdgeTessFactor"},
300 {WINED3D_SV_QUAD_U_INNER_TESS_FACTOR
, "finalQuadUInsideTessFactor"},
301 {WINED3D_SV_QUAD_V_INNER_TESS_FACTOR
, "finalQuadVInsideTessFactor"},
302 {WINED3D_SV_TRIANGLE_U_TESS_FACTOR
, "finalTriUeq0EdgeTessFactor"},
303 {WINED3D_SV_TRIANGLE_V_TESS_FACTOR
, "finalTriVeq0EdgeTessFactor"},
304 {WINED3D_SV_TRIANGLE_W_TESS_FACTOR
, "finalTriWeq0EdgeTessFactor"},
305 {WINED3D_SV_TRIANGLE_INNER_TESS_FACTOR
, "finalTriInsideTessFactor"},
306 {WINED3D_SV_LINE_DETAIL_TESS_FACTOR
, "finalLineDetailTessFactor"},
307 {WINED3D_SV_LINE_DENSITY_TESS_FACTOR
, "finalLineDensityTessFactor"},
310 static void shader_dump_src_param(struct wined3d_string_buffer
*buffer
,
311 const struct wined3d_shader_src_param
*param
, const struct wined3d_shader_version
*shader_version
);
313 const char *debug_d3dshaderinstructionhandler(enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx
)
315 if (handler_idx
>= ARRAY_SIZE(shader_opcode_names
))
316 return wine_dbg_sprintf("UNRECOGNIZED(%#x)", handler_idx
);
318 return shader_opcode_names
[handler_idx
];
321 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage
)
323 if (usage
>= ARRAY_SIZE(semantic_names
))
325 FIXME("Unrecognized usage %#x.\n", usage
);
326 return "UNRECOGNIZED";
329 return semantic_names
[usage
];
332 static enum wined3d_decl_usage
shader_usage_from_semantic_name(const char *name
)
336 for (i
= 0; i
< ARRAY_SIZE(semantic_names
); ++i
)
338 if (!strcmp(name
, semantic_names
[i
]))
345 static enum wined3d_sysval_semantic
shader_sysval_semantic_from_usage(enum wined3d_decl_usage usage
)
349 case WINED3D_DECL_USAGE_POSITION
:
350 return WINED3D_SV_POSITION
;
356 BOOL
shader_match_semantic(const char *semantic_name
, enum wined3d_decl_usage usage
)
358 return !strcmp(semantic_name
, shader_semantic_name_from_usage(usage
));
361 static void shader_signature_from_semantic(struct wined3d_shader_signature_element
*e
,
362 const struct wined3d_shader_semantic
*s
)
364 e
->semantic_name
= shader_semantic_name_from_usage(s
->usage
);
365 e
->semantic_idx
= s
->usage_idx
;
366 e
->sysval_semantic
= shader_sysval_semantic_from_usage(s
->usage
);
367 e
->component_type
= WINED3D_TYPE_FLOAT
;
368 e
->register_idx
= s
->reg
.reg
.idx
[0].offset
;
369 e
->mask
= s
->reg
.write_mask
;
372 static void shader_signature_from_usage(struct wined3d_shader_signature_element
*e
,
373 enum wined3d_decl_usage usage
, UINT usage_idx
, UINT reg_idx
, DWORD write_mask
)
375 e
->semantic_name
= shader_semantic_name_from_usage(usage
);
376 e
->semantic_idx
= usage_idx
;
377 e
->sysval_semantic
= shader_sysval_semantic_from_usage(usage
);
378 e
->component_type
= WINED3D_TYPE_FLOAT
;
379 e
->register_idx
= reg_idx
;
380 e
->mask
= write_mask
;
383 static const struct wined3d_shader_frontend
*shader_select_frontend(DWORD version_token
)
385 switch (version_token
>> 16)
389 return &sm1_shader_frontend
;
397 return &sm4_shader_frontend
;
400 FIXME("Unrecognised version token %#x.\n", version_token
);
405 void string_buffer_clear(struct wined3d_string_buffer
*buffer
)
407 buffer
->buffer
[0] = '\0';
408 buffer
->content_size
= 0;
411 BOOL
string_buffer_init(struct wined3d_string_buffer
*buffer
)
413 buffer
->buffer_size
= 32;
414 if (!(buffer
->buffer
= HeapAlloc(GetProcessHeap(), 0, buffer
->buffer_size
)))
416 ERR("Failed to allocate shader buffer memory.\n");
420 string_buffer_clear(buffer
);
424 void string_buffer_free(struct wined3d_string_buffer
*buffer
)
426 HeapFree(GetProcessHeap(), 0, buffer
->buffer
);
429 BOOL
string_buffer_resize(struct wined3d_string_buffer
*buffer
, int rc
)
432 unsigned int new_buffer_size
= buffer
->buffer_size
* 2;
434 while (rc
> 0 && (unsigned int)rc
>= new_buffer_size
- buffer
->content_size
)
435 new_buffer_size
*= 2;
436 if (!(new_buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
->buffer
, new_buffer_size
)))
438 ERR("Failed to grow buffer.\n");
439 buffer
->buffer
[buffer
->content_size
] = '\0';
442 buffer
->buffer
= new_buffer
;
443 buffer
->buffer_size
= new_buffer_size
;
447 int shader_vaddline(struct wined3d_string_buffer
*buffer
, const char *format
, va_list args
)
452 rem
= buffer
->buffer_size
- buffer
->content_size
;
453 rc
= vsnprintf(&buffer
->buffer
[buffer
->content_size
], rem
, format
, args
);
454 if (rc
< 0 /* C89 */ || (unsigned int)rc
>= rem
/* C99 */)
457 buffer
->content_size
+= rc
;
461 int shader_addline(struct wined3d_string_buffer
*buffer
, const char *format
, ...)
468 va_start(args
, format
);
469 ret
= shader_vaddline(buffer
, format
, args
);
473 if (!string_buffer_resize(buffer
, ret
))
478 struct wined3d_string_buffer
*string_buffer_get(struct wined3d_string_buffer_list
*list
)
480 struct wined3d_string_buffer
*buffer
;
482 if (list_empty(&list
->list
))
484 buffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer
));
485 if (!buffer
|| !string_buffer_init(buffer
))
487 ERR("Couldn't allocate buffer for temporary string.\n");
488 HeapFree(GetProcessHeap(), 0, buffer
);
494 buffer
= LIST_ENTRY(list_head(&list
->list
), struct wined3d_string_buffer
, entry
);
495 list_remove(&buffer
->entry
);
497 string_buffer_clear(buffer
);
501 static int string_buffer_vsprintf(struct wined3d_string_buffer
*buffer
, const char *format
, va_list args
)
505 string_buffer_clear(buffer
);
506 return shader_vaddline(buffer
, format
, args
);
509 void string_buffer_sprintf(struct wined3d_string_buffer
*buffer
, const char *format
, ...)
516 va_start(args
, format
);
517 ret
= string_buffer_vsprintf(buffer
, format
, args
);
521 if (!string_buffer_resize(buffer
, ret
))
526 void string_buffer_release(struct wined3d_string_buffer_list
*list
, struct wined3d_string_buffer
*buffer
)
530 list_add_head(&list
->list
, &buffer
->entry
);
533 void string_buffer_list_init(struct wined3d_string_buffer_list
*list
)
535 list_init(&list
->list
);
538 void string_buffer_list_cleanup(struct wined3d_string_buffer_list
*list
)
540 struct wined3d_string_buffer
*buffer
, *buffer_next
;
542 LIST_FOR_EACH_ENTRY_SAFE(buffer
, buffer_next
, &list
->list
, struct wined3d_string_buffer
, entry
)
544 string_buffer_free(buffer
);
545 HeapFree(GetProcessHeap(), 0, buffer
);
547 list_init(&list
->list
);
550 /* Convert floating point offset relative to a register file to an absolute
551 * offset for float constants. */
552 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type
, UINT register_idx
)
554 switch (register_type
)
556 case WINED3DSPR_CONST
: return register_idx
;
557 case WINED3DSPR_CONST2
: return 2048 + register_idx
;
558 case WINED3DSPR_CONST3
: return 4096 + register_idx
;
559 case WINED3DSPR_CONST4
: return 6144 + register_idx
;
561 FIXME("Unsupported register type: %u.\n", register_type
);
566 static void shader_delete_constant_list(struct list
*clist
)
568 struct wined3d_shader_lconst
*constant
, *constant_next
;
570 LIST_FOR_EACH_ENTRY_SAFE(constant
, constant_next
, clist
, struct wined3d_shader_lconst
, entry
)
571 HeapFree(GetProcessHeap(), 0, constant
);
575 static void shader_set_limits(struct wined3d_shader
*shader
)
577 static const struct limits_entry
579 unsigned int min_version
;
580 unsigned int max_version
;
581 struct wined3d_shader_limits limits
;
585 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
586 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
587 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
588 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
589 * even though they are capable of supporting much more (GL
590 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
591 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
593 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
594 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
595 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 0}},
600 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
601 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
605 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
606 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
610 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
611 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
612 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
617 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
618 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
619 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
620 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
621 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
622 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 10}},
623 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 32}},
628 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
629 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 0}},
631 const struct limits_entry
*limits_array
;
632 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
633 shader
->reg_maps
.shader_version
.minor
);
636 switch (shader
->reg_maps
.shader_version
.type
)
639 FIXME("Unexpected shader type %u found.\n", shader
->reg_maps
.shader_version
.type
);
641 case WINED3D_SHADER_TYPE_VERTEX
:
642 limits_array
= vs_limits
;
644 case WINED3D_SHADER_TYPE_HULL
:
645 limits_array
= hs_limits
;
647 case WINED3D_SHADER_TYPE_DOMAIN
:
648 limits_array
= ds_limits
;
650 case WINED3D_SHADER_TYPE_GEOMETRY
:
651 limits_array
= gs_limits
;
653 case WINED3D_SHADER_TYPE_PIXEL
:
654 limits_array
= ps_limits
;
656 case WINED3D_SHADER_TYPE_COMPUTE
:
657 limits_array
= cs_limits
;
661 while (limits_array
[i
].min_version
&& limits_array
[i
].min_version
<= shader_version
)
663 if (shader_version
<= limits_array
[i
].max_version
)
665 shader
->limits
= &limits_array
[i
].limits
;
672 FIXME("Unexpected shader version \"%u.%u\".\n",
673 shader
->reg_maps
.shader_version
.major
,
674 shader
->reg_maps
.shader_version
.minor
);
675 shader
->limits
= &limits_array
[max(0, i
- 1)].limits
;
679 static inline void set_bitmap_bit(DWORD
*bitmap
, DWORD bit
)
684 bitmap
[idx
] |= (1u << shift
);
687 static BOOL
shader_record_register_usage(struct wined3d_shader
*shader
, struct wined3d_shader_reg_maps
*reg_maps
,
688 const struct wined3d_shader_register
*reg
, enum wined3d_shader_type shader_type
, unsigned int constf_size
)
692 case WINED3DSPR_TEXTURE
: /* WINED3DSPR_ADDR */
693 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
694 reg_maps
->texcoord
|= 1u << reg
->idx
[0].offset
;
696 reg_maps
->address
|= 1u << reg
->idx
[0].offset
;
699 case WINED3DSPR_TEMP
:
700 reg_maps
->temporary
|= 1u << reg
->idx
[0].offset
;
703 case WINED3DSPR_INPUT
:
704 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
706 if (reg
->idx
[0].rel_addr
)
708 /* If relative addressing is used, we must assume that all registers
709 * are used. Even if it is a construct like v3[aL], we can't assume
710 * that v0, v1 and v2 aren't read because aL can be negative */
712 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
714 shader
->u
.ps
.input_reg_used
[i
] = TRUE
;
719 shader
->u
.ps
.input_reg_used
[reg
->idx
[0].offset
] = TRUE
;
723 reg_maps
->input_registers
|= 1u << reg
->idx
[0].offset
;
726 case WINED3DSPR_RASTOUT
:
727 if (reg
->idx
[0].offset
== 1)
729 if (reg
->idx
[0].offset
== 2)
730 reg_maps
->point_size
= 1;
733 case WINED3DSPR_MISCTYPE
:
734 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
736 if (!reg
->idx
[0].offset
)
738 else if (reg
->idx
[0].offset
== 1)
739 reg_maps
->usesfacing
= 1;
743 case WINED3DSPR_CONST
:
744 if (reg
->idx
[0].rel_addr
)
746 if (reg
->idx
[0].offset
< reg_maps
->min_rel_offset
)
747 reg_maps
->min_rel_offset
= reg
->idx
[0].offset
;
748 if (reg
->idx
[0].offset
> reg_maps
->max_rel_offset
)
749 reg_maps
->max_rel_offset
= reg
->idx
[0].offset
;
750 reg_maps
->usesrelconstF
= TRUE
;
754 if (reg
->idx
[0].offset
>= min(shader
->limits
->constant_float
, constf_size
))
756 WARN("Shader using float constant %u which is not supported.\n", reg
->idx
[0].offset
);
761 set_bitmap_bit(reg_maps
->constf
, reg
->idx
[0].offset
);
766 case WINED3DSPR_CONSTINT
:
767 if (reg
->idx
[0].offset
>= shader
->limits
->constant_int
)
769 WARN("Shader using integer constant %u which is not supported.\n", reg
->idx
[0].offset
);
774 reg_maps
->integer_constants
|= (1u << reg
->idx
[0].offset
);
778 case WINED3DSPR_CONSTBOOL
:
779 if (reg
->idx
[0].offset
>= shader
->limits
->constant_bool
)
781 WARN("Shader using bool constant %u which is not supported.\n", reg
->idx
[0].offset
);
786 reg_maps
->boolean_constants
|= (1u << reg
->idx
[0].offset
);
790 case WINED3DSPR_COLOROUT
:
791 reg_maps
->rt_mask
|= (1u << reg
->idx
[0].offset
);
795 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
796 reg
->type
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
802 static void shader_record_sample(struct wined3d_shader_reg_maps
*reg_maps
,
803 unsigned int resource_idx
, unsigned int sampler_idx
, unsigned int bind_idx
)
805 struct wined3d_shader_sampler_map_entry
*entries
, *entry
;
806 struct wined3d_shader_sampler_map
*map
;
809 map
= ®_maps
->sampler_map
;
810 entries
= map
->entries
;
811 for (i
= 0; i
< map
->count
; ++i
)
813 if (entries
[i
].resource_idx
== resource_idx
&& entries
[i
].sampler_idx
== sampler_idx
)
819 if (!(entries
= wined3d_calloc(4, sizeof(*entries
))))
821 ERR("Failed to allocate sampler map entries.\n");
825 map
->entries
= entries
;
827 else if (map
->count
== map
->size
)
829 size_t new_size
= map
->size
* 2;
831 if (sizeof(*entries
) * new_size
<= sizeof(*entries
) * map
->size
832 || !(entries
= HeapReAlloc(GetProcessHeap(), 0, entries
, sizeof(*entries
) * new_size
)))
834 ERR("Failed to resize sampler map entries.\n");
837 map
->size
= new_size
;
838 map
->entries
= entries
;
841 entry
= &entries
[map
->count
++];
842 entry
->resource_idx
= resource_idx
;
843 entry
->sampler_idx
= sampler_idx
;
844 entry
->bind_idx
= bind_idx
;
847 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr
, unsigned int param
)
851 case WINED3DSIH_M4x4
:
852 case WINED3DSIH_M3x4
:
853 return param
== 1 ? 3 : 0;
855 case WINED3DSIH_M4x3
:
856 case WINED3DSIH_M3x3
:
857 return param
== 1 ? 2 : 0;
859 case WINED3DSIH_M3x2
:
860 return param
== 1 ? 1 : 0;
867 /* Note that this does not count the loop register as an address register. */
868 static HRESULT
shader_get_registers_used(struct wined3d_shader
*shader
, const struct wined3d_shader_frontend
*fe
,
869 struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_shader_signature
*input_signature
,
870 struct wined3d_shader_signature
*output_signature
, const DWORD
*byte_code
, DWORD constf_size
)
872 struct wined3d_shader_signature_element input_signature_elements
[max(MAX_ATTRIBS
, MAX_REG_INPUT
)];
873 struct wined3d_shader_signature_element output_signature_elements
[MAX_REG_OUTPUT
];
874 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
875 void *fe_data
= shader
->frontend_data
;
876 struct wined3d_shader_version shader_version
;
877 const DWORD
*ptr
= byte_code
;
880 memset(reg_maps
, 0, sizeof(*reg_maps
));
881 memset(input_signature_elements
, 0, sizeof(input_signature_elements
));
882 memset(output_signature_elements
, 0, sizeof(output_signature_elements
));
883 reg_maps
->min_rel_offset
= ~0U;
884 list_init(®_maps
->indexable_temps
);
886 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
887 reg_maps
->shader_version
= shader_version
;
889 shader_set_limits(shader
);
891 if (!(reg_maps
->constf
= wined3d_calloc(((min(shader
->limits
->constant_float
, constf_size
) + 31) / 32),
892 sizeof(*reg_maps
->constf
))))
894 ERR("Failed to allocate constant map memory.\n");
895 return E_OUTOFMEMORY
;
898 while (!fe
->shader_is_end(fe_data
, &ptr
))
900 struct wined3d_shader_instruction ins
;
903 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
905 /* Unhandled opcode, and its parameters. */
906 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
908 TRACE("Skipping unrecognized instruction.\n");
912 /* Handle declarations. */
913 if (ins
.handler_idx
== WINED3DSIH_DCL
914 || ins
.handler_idx
== WINED3DSIH_DCL_UAV_TYPED
)
916 struct wined3d_shader_semantic
*semantic
= &ins
.declaration
.semantic
;
917 unsigned int reg_idx
= semantic
->reg
.reg
.idx
[0].offset
;
919 switch (semantic
->reg
.reg
.type
)
921 /* Mark input registers used. */
922 case WINED3DSPR_INPUT
:
923 if (reg_idx
>= MAX_REG_INPUT
)
925 ERR("Invalid input register index %u.\n", reg_idx
);
928 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
&& shader_version
.major
== 3
929 && semantic
->usage
== WINED3D_DECL_USAGE_POSITION
&& !semantic
->usage_idx
)
930 return WINED3DERR_INVALIDCALL
;
931 reg_maps
->input_registers
|= 1u << reg_idx
;
932 shader_signature_from_semantic(&input_signature_elements
[reg_idx
], semantic
);
935 /* Vertex shader: mark 3.0 output registers used, save token. */
936 case WINED3DSPR_OUTPUT
:
937 if (reg_idx
>= MAX_REG_OUTPUT
)
939 ERR("Invalid output register index %u.\n", reg_idx
);
942 reg_maps
->output_registers
|= 1u << reg_idx
;
943 shader_signature_from_semantic(&output_signature_elements
[reg_idx
], semantic
);
944 if (semantic
->usage
== WINED3D_DECL_USAGE_FOG
)
946 if (semantic
->usage
== WINED3D_DECL_USAGE_PSIZE
)
947 reg_maps
->point_size
= 1;
950 case WINED3DSPR_SAMPLER
:
951 shader_record_sample(reg_maps
, reg_idx
, reg_idx
, reg_idx
);
952 case WINED3DSPR_RESOURCE
:
953 if (reg_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
955 ERR("Invalid resource index %u.\n", reg_idx
);
958 reg_maps
->resource_info
[reg_idx
].type
= semantic
->resource_type
;
959 reg_maps
->resource_info
[reg_idx
].data_type
= semantic
->resource_data_type
;
963 if (reg_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
965 ERR("Invalid UAV resource index %u.\n", reg_idx
);
968 reg_maps
->uav_resource_info
[reg_idx
].type
= semantic
->resource_type
;
969 reg_maps
->uav_resource_info
[reg_idx
].data_type
= semantic
->resource_data_type
;
973 TRACE("Not recording DCL register type %#x.\n", semantic
->reg
.reg
.type
);
977 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
979 struct wined3d_shader_register
*reg
= &ins
.declaration
.src
.reg
;
980 if (reg
->idx
[0].offset
>= WINED3D_MAX_CBS
)
981 ERR("Invalid CB index %u.\n", reg
->idx
[0].offset
);
983 reg_maps
->cb_sizes
[reg
->idx
[0].offset
] = reg
->idx
[1].offset
;
985 else if (ins
.handler_idx
== WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER
)
988 FIXME("Multiple immediate constant buffers.\n");
989 reg_maps
->icb
= ins
.declaration
.icb
;
991 else if (ins
.handler_idx
== WINED3DSIH_DCL_INDEXABLE_TEMP
)
993 struct wined3d_shader_indexable_temp
*reg
;
995 if (!(reg
= HeapAlloc(GetProcessHeap(), 0, sizeof(*reg
))))
996 return E_OUTOFMEMORY
;
998 *reg
= ins
.declaration
.indexable_temp
;
999 list_add_tail(®_maps
->indexable_temps
, ®
->entry
);
1001 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
)
1003 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1004 shader
->u
.gs
.input_type
= ins
.declaration
.primitive_type
;
1006 FIXME("Invalid instruction %#x for shader type %#x.\n",
1007 ins
.handler_idx
, shader_version
.type
);
1009 else if (ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
1011 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1012 shader
->u
.gs
.output_type
= ins
.declaration
.primitive_type
;
1014 FIXME("Invalid instruction %#x for shader type %#x.\n",
1015 ins
.handler_idx
, shader_version
.type
);
1017 else if (ins
.handler_idx
== WINED3DSIH_DCL_SAMPLER
)
1019 if (ins
.flags
& WINED3DSI_SAMPLER_COMPARISON_MODE
)
1020 reg_maps
->sampler_comparison_mode
|= (1u << ins
.declaration
.dst
.reg
.idx
[0].offset
);
1022 else if (ins
.handler_idx
== WINED3DSIH_DCL_TEMPS
)
1024 reg_maps
->temporary_count
= ins
.declaration
.count
;
1026 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
1028 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1029 shader
->u
.gs
.vertices_out
= ins
.declaration
.count
;
1031 FIXME("Invalid instruction %#x for shader type %#x.\n",
1032 ins
.handler_idx
, shader_version
.type
);
1034 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
1036 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
1038 if (!lconst
) return E_OUTOFMEMORY
;
1040 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
1041 memcpy(lconst
->value
, ins
.src
[0].reg
.u
.immconst_data
, 4 * sizeof(DWORD
));
1042 value
= (float *)lconst
->value
;
1044 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
1045 if (shader_version
.major
== 1 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1047 if (value
[0] < -1.0f
) value
[0] = -1.0f
;
1048 else if (value
[0] > 1.0f
) value
[0] = 1.0f
;
1049 if (value
[1] < -1.0f
) value
[1] = -1.0f
;
1050 else if (value
[1] > 1.0f
) value
[1] = 1.0f
;
1051 if (value
[2] < -1.0f
) value
[2] = -1.0f
;
1052 else if (value
[2] > 1.0f
) value
[2] = 1.0f
;
1053 if (value
[3] < -1.0f
) value
[3] = -1.0f
;
1054 else if (value
[3] > 1.0f
) value
[3] = 1.0f
;
1057 list_add_head(&shader
->constantsF
, &lconst
->entry
);
1059 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
1060 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
1062 shader
->lconst_inf_or_nan
= TRUE
;
1065 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
1067 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
1068 if (!lconst
) return E_OUTOFMEMORY
;
1070 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
1071 memcpy(lconst
->value
, ins
.src
[0].reg
.u
.immconst_data
, 4 * sizeof(DWORD
));
1073 list_add_head(&shader
->constantsI
, &lconst
->entry
);
1074 reg_maps
->local_int_consts
|= (1u << lconst
->idx
);
1076 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
1078 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
1079 if (!lconst
) return E_OUTOFMEMORY
;
1081 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
1082 memcpy(lconst
->value
, ins
.src
[0].reg
.u
.immconst_data
, sizeof(DWORD
));
1084 list_add_head(&shader
->constantsB
, &lconst
->entry
);
1085 reg_maps
->local_bool_consts
|= (1u << lconst
->idx
);
1087 /* For subroutine prototypes. */
1088 else if (ins
.handler_idx
== WINED3DSIH_LABEL
)
1090 reg_maps
->labels
|= 1u << ins
.src
[0].reg
.idx
[0].offset
;
1092 /* Set texture, address, temporary registers. */
1095 BOOL color0_mov
= FALSE
;
1098 /* This will loop over all the registers and try to
1099 * make a bitmask of the ones we're interested in.
1101 * Relative addressing tokens are ignored, but that's
1102 * okay, since we'll catch any address registers when
1103 * they are initialized (required by spec). */
1104 for (i
= 0; i
< ins
.dst_count
; ++i
)
1106 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.dst
[i
].reg
,
1107 shader_version
.type
, constf_size
))
1108 return WINED3DERR_INVALIDCALL
;
1110 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
)
1112 UINT idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
1114 switch (ins
.dst
[i
].reg
.type
)
1116 case WINED3DSPR_RASTOUT
:
1117 if (shader_version
.major
>= 3)
1122 reg_maps
->output_registers
|= 1u << 10;
1123 shader_signature_from_usage(&output_signature_elements
[10],
1124 WINED3D_DECL_USAGE_POSITION
, 0, 10, WINED3DSP_WRITEMASK_ALL
);
1128 reg_maps
->output_registers
|= 1u << 11;
1129 shader_signature_from_usage(&output_signature_elements
[11],
1130 WINED3D_DECL_USAGE_FOG
, 0, 11, WINED3DSP_WRITEMASK_0
);
1134 reg_maps
->output_registers
|= 1u << 11;
1135 shader_signature_from_usage(&output_signature_elements
[11],
1136 WINED3D_DECL_USAGE_PSIZE
, 0, 11, WINED3DSP_WRITEMASK_1
);
1141 case WINED3DSPR_ATTROUT
:
1142 if (shader_version
.major
>= 3)
1147 if (reg_maps
->output_registers
& (1u << idx
))
1149 output_signature_elements
[idx
].mask
|= ins
.dst
[i
].write_mask
;
1153 reg_maps
->output_registers
|= 1u << idx
;
1154 shader_signature_from_usage(&output_signature_elements
[idx
],
1155 WINED3D_DECL_USAGE_COLOR
, idx
- 8, idx
, ins
.dst
[i
].write_mask
);
1160 case WINED3DSPR_TEXCRDOUT
:
1161 if (shader_version
.major
>= 3)
1163 reg_maps
->u
.output_registers_mask
[idx
] |= ins
.dst
[i
].write_mask
;
1166 reg_maps
->u
.texcoord_mask
[idx
] |= ins
.dst
[i
].write_mask
;
1167 if (reg_maps
->output_registers
& (1u << idx
))
1169 output_signature_elements
[idx
].mask
|= ins
.dst
[i
].write_mask
;
1173 reg_maps
->output_registers
|= 1u << idx
;
1174 shader_signature_from_usage(&output_signature_elements
[idx
],
1175 WINED3D_DECL_USAGE_TEXCOORD
, idx
, idx
, ins
.dst
[i
].write_mask
);
1184 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1186 if (ins
.dst
[i
].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
.dst
[i
].reg
.idx
[0].offset
)
1188 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
1189 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
1190 * the mov and perform the sRGB write correction from the source register.
1192 * However, if the mov is only partial, we can't do this, and if the write
1193 * comes from an instruction other than MOV it is hard to do as well. If
1194 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
1195 shader
->u
.ps
.color0_mov
= FALSE
;
1196 if (ins
.handler_idx
== WINED3DSIH_MOV
1197 && ins
.dst
[i
].write_mask
== WINED3DSP_WRITEMASK_ALL
)
1199 /* Used later when the source register is read. */
1203 /* Also drop the MOV marker if the source register is overwritten prior to the shader
1206 else if (ins
.dst
[i
].reg
.type
== WINED3DSPR_TEMP
1207 && ins
.dst
[i
].reg
.idx
[0].offset
== shader
->u
.ps
.color0_reg
)
1209 shader
->u
.ps
.color0_mov
= FALSE
;
1213 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
1214 if (shader_version
.major
== 1
1215 && (ins
.handler_idx
== WINED3DSIH_TEX
1216 || ins
.handler_idx
== WINED3DSIH_TEXBEM
1217 || ins
.handler_idx
== WINED3DSIH_TEXBEML
1218 || ins
.handler_idx
== WINED3DSIH_TEXDP3TEX
1219 || ins
.handler_idx
== WINED3DSIH_TEXM3x2TEX
1220 || ins
.handler_idx
== WINED3DSIH_TEXM3x3SPEC
1221 || ins
.handler_idx
== WINED3DSIH_TEXM3x3TEX
1222 || ins
.handler_idx
== WINED3DSIH_TEXM3x3VSPEC
1223 || ins
.handler_idx
== WINED3DSIH_TEXREG2AR
1224 || ins
.handler_idx
== WINED3DSIH_TEXREG2GB
1225 || ins
.handler_idx
== WINED3DSIH_TEXREG2RGB
))
1227 unsigned int reg_idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
1229 TRACE("Setting fake 2D resource for 1.x pixelshader.\n");
1230 reg_maps
->resource_info
[reg_idx
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
1231 reg_maps
->resource_info
[reg_idx
].data_type
= WINED3D_DATA_FLOAT
;
1232 shader_record_sample(reg_maps
, reg_idx
, reg_idx
, reg_idx
);
1234 /* texbem is only valid with < 1.4 pixel shaders */
1235 if (ins
.handler_idx
== WINED3DSIH_TEXBEM
1236 || ins
.handler_idx
== WINED3DSIH_TEXBEML
)
1238 reg_maps
->bumpmat
|= 1u << reg_idx
;
1239 if (ins
.handler_idx
== WINED3DSIH_TEXBEML
)
1241 reg_maps
->luminanceparams
|= 1u << reg_idx
;
1245 else if (ins
.handler_idx
== WINED3DSIH_BEM
)
1247 reg_maps
->bumpmat
|= 1u << ins
.dst
[i
].reg
.idx
[0].offset
;
1251 if (ins
.handler_idx
== WINED3DSIH_ATOMIC_IADD
1252 || ins
.handler_idx
== WINED3DSIH_LD_UAV_TYPED
)
1254 unsigned int reg_idx
;
1255 if (ins
.handler_idx
== WINED3DSIH_ATOMIC_IADD
)
1256 reg_idx
= ins
.dst
[0].reg
.idx
[0].offset
;
1258 reg_idx
= ins
.src
[1].reg
.idx
[0].offset
;
1259 if (reg_idx
>= MAX_UNORDERED_ACCESS_VIEWS
)
1261 ERR("Invalid UAV index %u.\n", reg_idx
);
1264 reg_maps
->uav_read_mask
|= (1u << reg_idx
);
1266 else if (ins
.handler_idx
== WINED3DSIH_NRM
)
1268 reg_maps
->usesnrm
= 1;
1270 else if (ins
.handler_idx
== WINED3DSIH_DSY
1271 || ins
.handler_idx
== WINED3DSIH_DSY_COARSE
1272 || ins
.handler_idx
== WINED3DSIH_DSY_FINE
)
1274 reg_maps
->usesdsy
= 1;
1276 else if (ins
.handler_idx
== WINED3DSIH_DSX
1277 || ins
.handler_idx
== WINED3DSIH_DSX_COARSE
1278 || ins
.handler_idx
== WINED3DSIH_DSX_FINE
)
1280 reg_maps
->usesdsx
= 1;
1282 else if (ins
.handler_idx
== WINED3DSIH_TEXLDD
) reg_maps
->usestexldd
= 1;
1283 else if (ins
.handler_idx
== WINED3DSIH_TEXLDL
) reg_maps
->usestexldl
= 1;
1284 else if (ins
.handler_idx
== WINED3DSIH_MOVA
) reg_maps
->usesmova
= 1;
1285 else if (ins
.handler_idx
== WINED3DSIH_IFC
) reg_maps
->usesifc
= 1;
1286 else if (ins
.handler_idx
== WINED3DSIH_CALL
) reg_maps
->usescall
= 1;
1287 else if (ins
.handler_idx
== WINED3DSIH_POW
) reg_maps
->usespow
= 1;
1288 else if (ins
.handler_idx
== WINED3DSIH_LOOP
1289 || ins
.handler_idx
== WINED3DSIH_REP
)
1292 if (cur_loop_depth
> max_loop_depth
)
1293 max_loop_depth
= cur_loop_depth
;
1295 else if (ins
.handler_idx
== WINED3DSIH_ENDLOOP
1296 || ins
.handler_idx
== WINED3DSIH_ENDREP
)
1300 else if (ins
.handler_idx
== WINED3DSIH_SAMPLE
1301 || ins
.handler_idx
== WINED3DSIH_SAMPLE_B
1302 || ins
.handler_idx
== WINED3DSIH_SAMPLE_C
1303 || ins
.handler_idx
== WINED3DSIH_SAMPLE_C_LZ
1304 || ins
.handler_idx
== WINED3DSIH_SAMPLE_GRAD
1305 || ins
.handler_idx
== WINED3DSIH_SAMPLE_LOD
)
1307 shader_record_sample(reg_maps
, ins
.src
[1].reg
.idx
[0].offset
,
1308 ins
.src
[2].reg
.idx
[0].offset
, reg_maps
->sampler_map
.count
);
1310 else if (ins
.handler_idx
== WINED3DSIH_LD
1311 || (ins
.handler_idx
== WINED3DSIH_RESINFO
&& ins
.src
[1].reg
.type
== WINED3DSPR_RESOURCE
))
1313 shader_record_sample(reg_maps
, ins
.src
[1].reg
.idx
[0].offset
,
1314 WINED3D_SAMPLER_DEFAULT
, reg_maps
->sampler_map
.count
);
1318 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.predicate
->reg
,
1319 shader_version
.type
, constf_size
))
1320 return WINED3DERR_INVALIDCALL
;
1322 for (i
= 0; i
< ins
.src_count
; ++i
)
1324 unsigned int count
= get_instr_extra_regcount(ins
.handler_idx
, i
);
1325 struct wined3d_shader_register reg
= ins
.src
[i
].reg
;
1327 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.src
[i
].reg
,
1328 shader_version
.type
, constf_size
))
1329 return WINED3DERR_INVALIDCALL
;
1332 ++reg
.idx
[0].offset
;
1333 if (!shader_record_register_usage(shader
, reg_maps
, ®
,
1334 shader_version
.type
, constf_size
))
1335 return WINED3DERR_INVALIDCALL
;
1341 if (ins
.src
[i
].reg
.type
== WINED3DSPR_TEMP
1342 && ins
.src
[i
].swizzle
== WINED3DSP_NOSWIZZLE
)
1344 shader
->u
.ps
.color0_mov
= TRUE
;
1345 shader
->u
.ps
.color0_reg
= ins
.src
[i
].reg
.idx
[0].offset
;
1351 reg_maps
->loop_depth
= max_loop_depth
;
1353 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1354 * R0 is written to the render target. */
1355 if (shader_version
.major
< 2 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1356 reg_maps
->rt_mask
|= (1u << 0);
1358 shader
->functionLength
= ((const char *)ptr
- (const char *)byte_code
);
1360 if (input_signature
->elements
)
1362 for (i
= 0; i
< input_signature
->element_count
; ++i
)
1364 reg_maps
->input_registers
|= 1u << input_signature
->elements
[i
].register_idx
;
1365 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1367 if (input_signature
->elements
[i
].sysval_semantic
== WINED3D_SV_POSITION
)
1369 else if (input_signature
->elements
[i
].sysval_semantic
== WINED3D_SV_IS_FRONT_FACE
)
1370 reg_maps
->usesfacing
= 1;
1374 else if (!input_signature
->elements
&& reg_maps
->input_registers
)
1376 unsigned int count
= wined3d_popcount(reg_maps
->input_registers
);
1377 struct wined3d_shader_signature_element
*e
;
1380 if (!(input_signature
->elements
= wined3d_calloc(count
, sizeof(*input_signature
->elements
))))
1381 return E_OUTOFMEMORY
;
1382 input_signature
->element_count
= count
;
1384 e
= input_signature
->elements
;
1385 for (i
= 0; i
< ARRAY_SIZE(input_signature_elements
); ++i
)
1387 if (!(reg_maps
->input_registers
& (1u << i
)))
1389 input_signature_elements
[i
].register_idx
= i
;
1390 *e
++ = input_signature_elements
[i
];
1394 if (output_signature
->elements
)
1396 for (i
= 0; i
< output_signature
->element_count
; ++i
)
1398 reg_maps
->output_registers
|= 1u << output_signature
->elements
[i
].register_idx
;
1401 else if (reg_maps
->output_registers
)
1403 unsigned int count
= wined3d_popcount(reg_maps
->output_registers
);
1404 struct wined3d_shader_signature_element
*e
;
1406 if (!(output_signature
->elements
= wined3d_calloc(count
, sizeof(*output_signature
->elements
))))
1407 return E_OUTOFMEMORY
;
1408 output_signature
->element_count
= count
;
1410 e
= output_signature
->elements
;
1411 for (i
= 0; i
< ARRAY_SIZE(output_signature_elements
); ++i
)
1413 if (!(reg_maps
->output_registers
& (1u << i
)))
1415 *e
++ = output_signature_elements
[i
];
1422 static void shader_cleanup_reg_maps(struct wined3d_shader_reg_maps
*reg_maps
)
1424 struct wined3d_shader_indexable_temp
*reg
, *reg_next
;
1426 HeapFree(GetProcessHeap(), 0, reg_maps
->constf
);
1427 HeapFree(GetProcessHeap(), 0, reg_maps
->sampler_map
.entries
);
1429 LIST_FOR_EACH_ENTRY_SAFE(reg
, reg_next
, ®_maps
->indexable_temps
, struct wined3d_shader_indexable_temp
, entry
)
1430 HeapFree(GetProcessHeap(), 0, reg
);
1431 list_init(®_maps
->indexable_temps
);
1434 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int max
)
1436 DWORD map
= 1u << max
;
1438 map
&= reg_maps
->shader_version
.major
< 3 ? ~reg_maps
->texcoord
: ~reg_maps
->input_registers
;
1440 return wined3d_log2i(map
);
1443 static void shader_dump_global_flags(struct wined3d_string_buffer
*buffer
, DWORD global_flags
)
1445 if (global_flags
& WINED3DSGF_REFACTORING_ALLOWED
)
1447 shader_addline(buffer
, "refactoringAllowed");
1448 global_flags
&= ~WINED3DSGF_REFACTORING_ALLOWED
;
1450 shader_addline(buffer
, " | ");
1453 if (global_flags
& WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS
)
1455 shader_addline(buffer
, "enableRawAndStructuredBuffers");
1456 global_flags
&= ~WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS
;
1460 shader_addline(buffer
, "unknown_flags(%#x)", global_flags
);
1463 static void shader_dump_sync_flags(struct wined3d_string_buffer
*buffer
, DWORD sync_flags
)
1465 if (sync_flags
& WINED3DSSF_GROUP_SHARED_MEMORY
)
1467 shader_addline(buffer
, "_g");
1468 sync_flags
&= ~WINED3DSSF_GROUP_SHARED_MEMORY
;
1470 if (sync_flags
& WINED3DSSF_THREAD_GROUP
)
1472 shader_addline(buffer
, "_t");
1473 sync_flags
&= ~WINED3DSSF_THREAD_GROUP
;
1477 shader_addline(buffer
, "_unknown_flags(%#x)", sync_flags
);
1480 static void shader_dump_uav_flags(struct wined3d_string_buffer
*buffer
, DWORD uav_flags
)
1482 if (uav_flags
& WINED3DSUF_GLOBALLY_COHERENT
)
1484 shader_addline(buffer
, "_glc");
1485 uav_flags
&= ~WINED3DSUF_GLOBALLY_COHERENT
;
1487 if (uav_flags
& WINED3DSUF_ORDER_PRESERVING_COUNTER
)
1489 shader_addline(buffer
, "_opc");
1490 uav_flags
&= ~WINED3DSUF_ORDER_PRESERVING_COUNTER
;
1494 shader_addline(buffer
, "_unknown_flags(%#x)", uav_flags
);
1497 static void shader_dump_tessellator_domain(struct wined3d_string_buffer
*buffer
,
1498 enum wined3d_tessellator_domain domain
)
1502 case WINED3D_TESSELLATOR_DOMAIN_LINE
:
1503 shader_addline(buffer
, "line");
1505 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE
:
1506 shader_addline(buffer
, "triangle");
1508 case WINED3D_TESSELLATOR_DOMAIN_QUAD
:
1509 shader_addline(buffer
, "quad");
1512 shader_addline(buffer
, "unknown_tessellator_domain(%#x)", domain
);
1517 static void shader_dump_tessellator_output_primitive(struct wined3d_string_buffer
*buffer
,
1518 enum wined3d_tessellator_output_primitive output_primitive
)
1520 switch (output_primitive
)
1522 case WINED3D_TESSELLATOR_OUTPUT_POINT
:
1523 shader_addline(buffer
, "point");
1525 case WINED3D_TESSELLATOR_OUTPUT_LINE
:
1526 shader_addline(buffer
, "line");
1528 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW
:
1529 shader_addline(buffer
, "triangle_cw");
1531 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW
:
1532 shader_addline(buffer
, "triangle_ccw");
1535 shader_addline(buffer
, "unknown_tessellator_output_primitive(%#x)", output_primitive
);
1540 static void shader_dump_tessellator_partitioning(struct wined3d_string_buffer
*buffer
,
1541 enum wined3d_tessellator_partitioning partitioning
)
1543 switch (partitioning
)
1545 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER
:
1546 shader_addline(buffer
, "integer");
1548 case WINED3D_TESSELLATOR_PARTITIONING_POW2
:
1549 shader_addline(buffer
, "pow2");
1551 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD
:
1552 shader_addline(buffer
, "fractional_odd");
1554 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN
:
1555 shader_addline(buffer
, "fractional_even");
1558 shader_addline(buffer
, "unknown_tessellator_partitioning(%#x)", partitioning
);
1563 static void shader_dump_sysval_semantic(struct wined3d_string_buffer
*buffer
, enum wined3d_sysval_semantic semantic
)
1567 for (i
= 0; i
< ARRAY_SIZE(sysval_semantic_names
); ++i
)
1569 if (sysval_semantic_names
[i
].sysval_semantic
== semantic
)
1571 shader_addline(buffer
, "%s", sysval_semantic_names
[i
].sysval_name
);
1576 shader_addline(buffer
, "unknown_sysval_semantic(%#x)", semantic
);
1579 static void shader_dump_decl_usage(struct wined3d_string_buffer
*buffer
,
1580 const struct wined3d_shader_semantic
*semantic
, const struct wined3d_shader_version
*shader_version
)
1582 shader_addline(buffer
, "dcl");
1584 if (semantic
->reg
.reg
.type
== WINED3DSPR_SAMPLER
)
1586 switch (semantic
->resource_type
)
1588 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
1589 shader_addline(buffer
, "_2d");
1592 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
1593 shader_addline(buffer
, "_3d");
1596 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
1597 shader_addline(buffer
, "_cube");
1601 shader_addline(buffer
, "_unknown_resource_type(%#x)", semantic
->resource_type
);
1605 else if (semantic
->reg
.reg
.type
== WINED3DSPR_RESOURCE
|| semantic
->reg
.reg
.type
== WINED3DSPR_UAV
)
1607 if (semantic
->reg
.reg
.type
== WINED3DSPR_RESOURCE
)
1608 shader_addline(buffer
, "_resource_");
1610 shader_addline(buffer
, "_uav_");
1611 switch (semantic
->resource_type
)
1613 case WINED3D_SHADER_RESOURCE_BUFFER
:
1614 shader_addline(buffer
, "buffer");
1617 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
1618 shader_addline(buffer
, "texture1d");
1621 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
1622 shader_addline(buffer
, "texture2d");
1625 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
:
1626 shader_addline(buffer
, "texture2dms");
1629 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
1630 shader_addline(buffer
, "texture3d");
1633 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
1634 shader_addline(buffer
, "texturecube");
1637 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
:
1638 shader_addline(buffer
, "texture1darray");
1641 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
1642 shader_addline(buffer
, "texture2darray");
1645 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY
:
1646 shader_addline(buffer
, "texture2dmsarray");
1650 shader_addline(buffer
, "unknown");
1653 switch (semantic
->resource_data_type
)
1655 case WINED3D_DATA_FLOAT
:
1656 shader_addline(buffer
, " (float)");
1659 case WINED3D_DATA_INT
:
1660 shader_addline(buffer
, " (int)");
1663 case WINED3D_DATA_UINT
:
1664 shader_addline(buffer
, " (uint)");
1667 case WINED3D_DATA_UNORM
:
1668 shader_addline(buffer
, " (unorm)");
1671 case WINED3D_DATA_SNORM
:
1672 shader_addline(buffer
, " (snorm)");
1676 shader_addline(buffer
, " (unknown)");
1682 /* Pixel shaders 3.0 don't have usage semantics. */
1683 if (shader_version
->major
< 3 && shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
1686 shader_addline(buffer
, "_");
1688 switch (semantic
->usage
)
1690 case WINED3D_DECL_USAGE_POSITION
:
1691 shader_addline(buffer
, "position%u", semantic
->usage_idx
);
1694 case WINED3D_DECL_USAGE_BLEND_INDICES
:
1695 shader_addline(buffer
, "blend");
1698 case WINED3D_DECL_USAGE_BLEND_WEIGHT
:
1699 shader_addline(buffer
, "weight");
1702 case WINED3D_DECL_USAGE_NORMAL
:
1703 shader_addline(buffer
, "normal%u", semantic
->usage_idx
);
1706 case WINED3D_DECL_USAGE_PSIZE
:
1707 shader_addline(buffer
, "psize");
1710 case WINED3D_DECL_USAGE_COLOR
:
1711 if (!semantic
->usage_idx
)
1712 shader_addline(buffer
, "color");
1714 shader_addline(buffer
, "specular%u", (semantic
->usage_idx
- 1));
1717 case WINED3D_DECL_USAGE_TEXCOORD
:
1718 shader_addline(buffer
, "texture%u", semantic
->usage_idx
);
1721 case WINED3D_DECL_USAGE_TANGENT
:
1722 shader_addline(buffer
, "tangent");
1725 case WINED3D_DECL_USAGE_BINORMAL
:
1726 shader_addline(buffer
, "binormal");
1729 case WINED3D_DECL_USAGE_TESS_FACTOR
:
1730 shader_addline(buffer
, "tessfactor");
1733 case WINED3D_DECL_USAGE_POSITIONT
:
1734 shader_addline(buffer
, "positionT%u", semantic
->usage_idx
);
1737 case WINED3D_DECL_USAGE_FOG
:
1738 shader_addline(buffer
, "fog");
1741 case WINED3D_DECL_USAGE_DEPTH
:
1742 shader_addline(buffer
, "depth");
1745 case WINED3D_DECL_USAGE_SAMPLE
:
1746 shader_addline(buffer
, "sample");
1750 shader_addline(buffer
, "<unknown_semantic(%#x)>", semantic
->usage
);
1751 FIXME("Unrecognised semantic usage %#x.\n", semantic
->usage
);
1756 static void shader_dump_register(struct wined3d_string_buffer
*buffer
,
1757 const struct wined3d_shader_register
*reg
, const struct wined3d_shader_version
*shader_version
)
1759 static const char * const rastout_reg_names
[] = {"oPos", "oFog", "oPts"};
1760 static const char * const misctype_reg_names
[] = {"vPos", "vFace"};
1761 UINT offset
= reg
->idx
[0].offset
;
1765 case WINED3DSPR_TEMP
:
1766 shader_addline(buffer
, "r");
1769 case WINED3DSPR_INPUT
:
1770 shader_addline(buffer
, "v");
1773 case WINED3DSPR_CONST
:
1774 case WINED3DSPR_CONST2
:
1775 case WINED3DSPR_CONST3
:
1776 case WINED3DSPR_CONST4
:
1777 shader_addline(buffer
, "c");
1778 offset
= shader_get_float_offset(reg
->type
, offset
);
1781 case WINED3DSPR_TEXTURE
: /* vs: case WINED3DSPR_ADDR */
1782 shader_addline(buffer
, "%c", shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
? 't' : 'a');
1785 case WINED3DSPR_RASTOUT
:
1786 shader_addline(buffer
, "%s", rastout_reg_names
[offset
]);
1789 case WINED3DSPR_COLOROUT
:
1790 shader_addline(buffer
, "oC");
1793 case WINED3DSPR_DEPTHOUT
:
1794 shader_addline(buffer
, "oDepth");
1797 case WINED3DSPR_ATTROUT
:
1798 shader_addline(buffer
, "oD");
1801 case WINED3DSPR_TEXCRDOUT
:
1802 /* Vertex shaders >= 3.0 use general purpose output registers
1803 * (WINED3DSPR_OUTPUT), which can include an address token. */
1804 if (shader_version
->major
>= 3)
1805 shader_addline(buffer
, "o");
1807 shader_addline(buffer
, "oT");
1810 case WINED3DSPR_CONSTINT
:
1811 shader_addline(buffer
, "i");
1814 case WINED3DSPR_CONSTBOOL
:
1815 shader_addline(buffer
, "b");
1818 case WINED3DSPR_LABEL
:
1819 shader_addline(buffer
, "l");
1822 case WINED3DSPR_LOOP
:
1823 shader_addline(buffer
, "aL");
1826 case WINED3DSPR_SAMPLER
:
1827 shader_addline(buffer
, "s");
1830 case WINED3DSPR_MISCTYPE
:
1833 FIXME("Unhandled misctype register %u.\n", offset
);
1834 shader_addline(buffer
, "<unhandled misctype %#x>", offset
);
1838 shader_addline(buffer
, "%s", misctype_reg_names
[offset
]);
1842 case WINED3DSPR_PREDICATE
:
1843 shader_addline(buffer
, "p");
1846 case WINED3DSPR_IMMCONST
:
1847 shader_addline(buffer
, "l");
1850 case WINED3DSPR_CONSTBUFFER
:
1851 shader_addline(buffer
, "cb");
1854 case WINED3DSPR_IMMCONSTBUFFER
:
1855 shader_addline(buffer
, "icb");
1858 case WINED3DSPR_PRIMID
:
1859 shader_addline(buffer
, "primID");
1862 case WINED3DSPR_NULL
:
1863 shader_addline(buffer
, "null");
1866 case WINED3DSPR_RESOURCE
:
1867 shader_addline(buffer
, "t");
1870 case WINED3DSPR_UAV
:
1871 shader_addline(buffer
, "u");
1874 case WINED3DSPR_OUTPOINTID
:
1875 shader_addline(buffer
, "vOutputControlPointID");
1878 case WINED3DSPR_FORKINSTID
:
1879 shader_addline(buffer
, "vForkInstanceId");
1882 case WINED3DSPR_INCONTROLPOINT
:
1883 shader_addline(buffer
, "vicp");
1886 case WINED3DSPR_OUTCONTROLPOINT
:
1887 shader_addline(buffer
, "vocp");
1890 case WINED3DSPR_PATCHCONST
:
1891 shader_addline(buffer
, "vpc");
1894 case WINED3DSPR_TESSCOORD
:
1895 shader_addline(buffer
, "vDomainLocation");
1898 case WINED3DSPR_GROUPSHAREDMEM
:
1899 shader_addline(buffer
, "g");
1902 case WINED3DSPR_THREADID
:
1903 shader_addline(buffer
, "vThreadID");
1906 case WINED3DSPR_THREADGROUPID
:
1907 shader_addline(buffer
, "vThreadGroupID");
1910 case WINED3DSPR_LOCALTHREADID
:
1911 shader_addline(buffer
, "vThreadIDInGroup");
1914 case WINED3DSPR_LOCALTHREADINDEX
:
1915 shader_addline(buffer
, "vThreadIDInGroupFlattened");
1918 case WINED3DSPR_IDXTEMP
:
1919 shader_addline(buffer
, "x");
1922 case WINED3DSPR_STREAM
:
1923 shader_addline(buffer
, "m");
1926 case WINED3DSPR_FUNCTIONBODY
:
1927 shader_addline(buffer
, "fb");
1930 case WINED3DSPR_FUNCTIONPOINTER
:
1931 shader_addline(buffer
, "fp");
1934 case WINED3DSPR_COVERAGE
:
1935 shader_addline(buffer
, "vCoverage");
1938 case WINED3DSPR_SAMPLEMASK
:
1939 shader_addline(buffer
, "oMask");
1943 shader_addline(buffer
, "<unhandled_rtype(%#x)>", reg
->type
);
1947 if (reg
->type
== WINED3DSPR_IMMCONST
)
1949 shader_addline(buffer
, "(");
1950 switch (reg
->immconst_type
)
1952 case WINED3D_IMMCONST_SCALAR
:
1953 switch (reg
->data_type
)
1955 case WINED3D_DATA_FLOAT
:
1956 shader_addline(buffer
, "%.8e", *(const float *)reg
->u
.immconst_data
);
1958 case WINED3D_DATA_INT
:
1959 shader_addline(buffer
, "%d", reg
->u
.immconst_data
[0]);
1961 case WINED3D_DATA_RESOURCE
:
1962 case WINED3D_DATA_SAMPLER
:
1963 case WINED3D_DATA_UINT
:
1964 shader_addline(buffer
, "%u", reg
->u
.immconst_data
[0]);
1967 shader_addline(buffer
, "<unhandled data type %#x>", reg
->data_type
);
1972 case WINED3D_IMMCONST_VEC4
:
1973 switch (reg
->data_type
)
1975 case WINED3D_DATA_FLOAT
:
1976 shader_addline(buffer
, "%.8e, %.8e, %.8e, %.8e",
1977 *(const float *)®
->u
.immconst_data
[0], *(const float *)®
->u
.immconst_data
[1],
1978 *(const float *)®
->u
.immconst_data
[2], *(const float *)®
->u
.immconst_data
[3]);
1980 case WINED3D_DATA_INT
:
1981 shader_addline(buffer
, "%d, %d, %d, %d",
1982 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
1983 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
1985 case WINED3D_DATA_RESOURCE
:
1986 case WINED3D_DATA_SAMPLER
:
1987 case WINED3D_DATA_UINT
:
1988 shader_addline(buffer
, "%u, %u, %u, %u",
1989 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
1990 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
1993 shader_addline(buffer
, "<unhandled data type %#x>", reg
->data_type
);
1999 shader_addline(buffer
, "<unhandled immconst_type %#x>", reg
->immconst_type
);
2002 shader_addline(buffer
, ")");
2004 else if (reg
->type
!= WINED3DSPR_RASTOUT
2005 && reg
->type
!= WINED3DSPR_MISCTYPE
2006 && reg
->type
!= WINED3DSPR_NULL
)
2010 shader_addline(buffer
, "[");
2011 if (reg
->idx
[0].rel_addr
)
2013 shader_dump_src_param(buffer
, reg
->idx
[0].rel_addr
, shader_version
);
2014 shader_addline(buffer
, " + ");
2016 shader_addline(buffer
, "%u]", offset
);
2018 if (reg
->idx
[1].offset
!= ~0u)
2020 shader_addline(buffer
, "[");
2021 if (reg
->idx
[1].rel_addr
)
2023 shader_dump_src_param(buffer
, reg
->idx
[1].rel_addr
, shader_version
);
2024 shader_addline(buffer
, " + ");
2026 shader_addline(buffer
, "%u]", reg
->idx
[1].offset
);
2030 if (reg
->type
== WINED3DSPR_FUNCTIONPOINTER
)
2031 shader_addline(buffer
, "[%u]", reg
->u
.fp_body_idx
);
2035 static void shader_dump_dst_param(struct wined3d_string_buffer
*buffer
,
2036 const struct wined3d_shader_dst_param
*param
, const struct wined3d_shader_version
*shader_version
)
2038 DWORD write_mask
= param
->write_mask
;
2040 shader_dump_register(buffer
, ¶m
->reg
, shader_version
);
2042 if (write_mask
&& write_mask
!= WINED3DSP_WRITEMASK_ALL
)
2044 static const char write_mask_chars
[] = "xyzw";
2046 shader_addline(buffer
, ".");
2047 if (write_mask
& WINED3DSP_WRITEMASK_0
)
2048 shader_addline(buffer
, "%c", write_mask_chars
[0]);
2049 if (write_mask
& WINED3DSP_WRITEMASK_1
)
2050 shader_addline(buffer
, "%c", write_mask_chars
[1]);
2051 if (write_mask
& WINED3DSP_WRITEMASK_2
)
2052 shader_addline(buffer
, "%c", write_mask_chars
[2]);
2053 if (write_mask
& WINED3DSP_WRITEMASK_3
)
2054 shader_addline(buffer
, "%c", write_mask_chars
[3]);
2058 static void shader_dump_src_param(struct wined3d_string_buffer
*buffer
,
2059 const struct wined3d_shader_src_param
*param
, const struct wined3d_shader_version
*shader_version
)
2061 enum wined3d_shader_src_modifier src_modifier
= param
->modifiers
;
2062 DWORD swizzle
= param
->swizzle
;
2064 if (src_modifier
== WINED3DSPSM_NEG
2065 || src_modifier
== WINED3DSPSM_BIASNEG
2066 || src_modifier
== WINED3DSPSM_SIGNNEG
2067 || src_modifier
== WINED3DSPSM_X2NEG
2068 || src_modifier
== WINED3DSPSM_ABSNEG
)
2069 shader_addline(buffer
, "-");
2070 else if (src_modifier
== WINED3DSPSM_COMP
)
2071 shader_addline(buffer
, "1-");
2072 else if (src_modifier
== WINED3DSPSM_NOT
)
2073 shader_addline(buffer
, "!");
2075 if (src_modifier
== WINED3DSPSM_ABS
|| src_modifier
== WINED3DSPSM_ABSNEG
)
2076 shader_addline(buffer
, "abs(");
2078 shader_dump_register(buffer
, ¶m
->reg
, shader_version
);
2080 switch (src_modifier
)
2082 case WINED3DSPSM_NONE
: break;
2083 case WINED3DSPSM_NEG
: break;
2084 case WINED3DSPSM_NOT
: break;
2085 case WINED3DSPSM_BIAS
: shader_addline(buffer
, "_bias"); break;
2086 case WINED3DSPSM_BIASNEG
: shader_addline(buffer
, "_bias"); break;
2087 case WINED3DSPSM_SIGN
: shader_addline(buffer
, "_bx2"); break;
2088 case WINED3DSPSM_SIGNNEG
: shader_addline(buffer
, "_bx2"); break;
2089 case WINED3DSPSM_COMP
: break;
2090 case WINED3DSPSM_X2
: shader_addline(buffer
, "_x2"); break;
2091 case WINED3DSPSM_X2NEG
: shader_addline(buffer
, "_x2"); break;
2092 case WINED3DSPSM_DZ
: shader_addline(buffer
, "_dz"); break;
2093 case WINED3DSPSM_DW
: shader_addline(buffer
, "_dw"); break;
2094 case WINED3DSPSM_ABSNEG
: shader_addline(buffer
, ")"); break;
2095 case WINED3DSPSM_ABS
: shader_addline(buffer
, ")"); break;
2096 default: shader_addline(buffer
, "_unknown_modifier(%#x)", src_modifier
);
2099 if (swizzle
!= WINED3DSP_NOSWIZZLE
)
2101 static const char swizzle_chars
[] = "xyzw";
2102 DWORD swizzle_x
= swizzle
& 0x03;
2103 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
2104 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
2105 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
2107 if (swizzle_x
== swizzle_y
2108 && swizzle_x
== swizzle_z
2109 && swizzle_x
== swizzle_w
)
2111 shader_addline(buffer
, ".%c", swizzle_chars
[swizzle_x
]);
2115 shader_addline(buffer
, ".%c%c%c%c", swizzle_chars
[swizzle_x
], swizzle_chars
[swizzle_y
],
2116 swizzle_chars
[swizzle_z
], swizzle_chars
[swizzle_w
]);
2121 /* Shared code in order to generate the bulk of the shader string.
2122 * NOTE: A description of how to parse tokens can be found on MSDN. */
2123 void shader_generate_main(const struct wined3d_shader
*shader
, struct wined3d_string_buffer
*buffer
,
2124 const struct wined3d_shader_reg_maps
*reg_maps
, const DWORD
*byte_code
, void *backend_ctx
)
2126 struct wined3d_device
*device
= shader
->device
;
2127 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
2128 void *fe_data
= shader
->frontend_data
;
2129 struct wined3d_shader_version shader_version
;
2130 struct wined3d_shader_parser_state state
;
2131 struct wined3d_shader_instruction ins
;
2132 struct wined3d_shader_tex_mx tex_mx
;
2133 struct wined3d_shader_context ctx
;
2134 const DWORD
*ptr
= byte_code
;
2136 /* Initialize current parsing state. */
2137 tex_mx
.current_row
= 0;
2138 state
.current_loop_depth
= 0;
2139 state
.current_loop_reg
= 0;
2140 state
.in_subroutine
= FALSE
;
2142 ctx
.shader
= shader
;
2143 ctx
.gl_info
= &device
->adapter
->gl_info
;
2144 ctx
.reg_maps
= reg_maps
;
2145 ctx
.buffer
= buffer
;
2146 ctx
.tex_mx
= &tex_mx
;
2148 ctx
.backend_data
= backend_ctx
;
2151 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
2153 while (!fe
->shader_is_end(fe_data
, &ptr
))
2156 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
2158 /* Unknown opcode and its parameters. */
2159 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
2161 TRACE("Skipping unrecognized instruction.\n");
2166 FIXME("Predicates not implemented.\n");
2168 /* Call appropriate function for output target */
2169 device
->shader_backend
->shader_handle_instruction(&ins
);
2173 static void shader_dump_ins_modifiers(struct wined3d_string_buffer
*buffer
,
2174 const struct wined3d_shader_dst_param
*dst
)
2176 DWORD mmask
= dst
->modifiers
;
2181 case 13: shader_addline(buffer
, "_d8"); break;
2182 case 14: shader_addline(buffer
, "_d4"); break;
2183 case 15: shader_addline(buffer
, "_d2"); break;
2184 case 1: shader_addline(buffer
, "_x2"); break;
2185 case 2: shader_addline(buffer
, "_x4"); break;
2186 case 3: shader_addline(buffer
, "_x8"); break;
2187 default: shader_addline(buffer
, "_unhandled_shift(%d)", dst
->shift
); break;
2190 if (mmask
& WINED3DSPDM_SATURATE
) shader_addline(buffer
, "_sat");
2191 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) shader_addline(buffer
, "_pp");
2192 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) shader_addline(buffer
, "_centroid");
2194 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
2195 if (mmask
) FIXME("Unrecognised modifier %#x.\n", mmask
);
2198 static void shader_dump_primitive_type(struct wined3d_string_buffer
*buffer
,
2199 enum wined3d_primitive_type primitive_type
)
2201 switch (primitive_type
)
2203 case WINED3D_PT_UNDEFINED
:
2204 shader_addline(buffer
, "undefined");
2206 case WINED3D_PT_POINTLIST
:
2207 shader_addline(buffer
, "pointlist");
2209 case WINED3D_PT_LINELIST
:
2210 shader_addline(buffer
, "linelist");
2212 case WINED3D_PT_LINESTRIP
:
2213 shader_addline(buffer
, "linestrip");
2215 case WINED3D_PT_TRIANGLELIST
:
2216 shader_addline(buffer
, "trianglelist");
2218 case WINED3D_PT_TRIANGLESTRIP
:
2219 shader_addline(buffer
, "trianglestrip");
2221 case WINED3D_PT_TRIANGLEFAN
:
2222 shader_addline(buffer
, "trianglefan");
2224 case WINED3D_PT_LINELIST_ADJ
:
2225 shader_addline(buffer
, "linelist_adj");
2227 case WINED3D_PT_LINESTRIP_ADJ
:
2228 shader_addline(buffer
, "linestrip_adj");
2230 case WINED3D_PT_TRIANGLELIST_ADJ
:
2231 shader_addline(buffer
, "trianglelist_adj");
2233 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
2234 shader_addline(buffer
, "trianglestrip_adj");
2237 shader_addline(buffer
, "<unrecognized_primitive_type %#x>", primitive_type
);
2242 static void shader_dump_interpolation_mode(struct wined3d_string_buffer
*buffer
,
2243 enum wined3d_shader_interpolation_mode interpolation_mode
)
2245 switch (interpolation_mode
)
2247 case WINED3DSIM_CONSTANT
:
2248 shader_addline(buffer
, "constant");
2250 case WINED3DSIM_LINEAR
:
2251 shader_addline(buffer
, "linear");
2253 case WINED3DSIM_LINEAR_CENTROID
:
2254 shader_addline(buffer
, "linear centroid");
2256 case WINED3DSIM_LINEAR_NOPERSPECTIVE
:
2257 shader_addline(buffer
, "linear noperspective");
2259 case WINED3DSIM_LINEAR_SAMPLE
:
2260 shader_addline(buffer
, "linear sample");
2262 case WINED3DSIM_LINEAR_NOPERSPECTIVE_CENTROID
:
2263 shader_addline(buffer
, "linear noperspective centroid");
2265 case WINED3DSIM_LINEAR_NOPERSPECTIVE_SAMPLE
:
2266 shader_addline(buffer
, "linear noperspective sample");
2269 shader_addline(buffer
, "<unrecognized_interpolation_mode %#x>", interpolation_mode
);
2274 static void shader_trace_init(const struct wined3d_shader_frontend
*fe
, void *fe_data
, const DWORD
*byte_code
)
2276 struct wined3d_shader_version shader_version
;
2277 struct wined3d_string_buffer buffer
;
2278 const DWORD
*ptr
= byte_code
;
2279 const char *type_prefix
;
2283 if (!string_buffer_init(&buffer
))
2285 ERR("Failed to initialize string buffer.\n");
2289 TRACE("Parsing %p.\n", byte_code
);
2291 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
2293 switch (shader_version
.type
)
2295 case WINED3D_SHADER_TYPE_VERTEX
:
2299 case WINED3D_SHADER_TYPE_HULL
:
2303 case WINED3D_SHADER_TYPE_DOMAIN
:
2307 case WINED3D_SHADER_TYPE_GEOMETRY
:
2311 case WINED3D_SHADER_TYPE_PIXEL
:
2315 case WINED3D_SHADER_TYPE_COMPUTE
:
2320 FIXME("Unhandled shader type %#x.\n", shader_version
.type
);
2321 type_prefix
= "unknown";
2325 shader_addline(&buffer
, "%s_%u_%u\n", type_prefix
, shader_version
.major
, shader_version
.minor
);
2327 while (!fe
->shader_is_end(fe_data
, &ptr
))
2329 struct wined3d_shader_instruction ins
;
2331 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
2332 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
2334 WARN("Skipping unrecognized instruction.\n");
2335 shader_addline(&buffer
, "<unrecognized instruction>\n");
2339 if (ins
.handler_idx
== WINED3DSIH_DCL
|| ins
.handler_idx
== WINED3DSIH_DCL_UAV_TYPED
)
2341 shader_dump_decl_usage(&buffer
, &ins
.declaration
.semantic
, &shader_version
);
2342 shader_dump_ins_modifiers(&buffer
, &ins
.declaration
.semantic
.reg
);
2343 shader_addline(&buffer
, " ");
2344 shader_dump_dst_param(&buffer
, &ins
.declaration
.semantic
.reg
, &shader_version
);
2346 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
2348 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2349 shader_dump_src_param(&buffer
, &ins
.declaration
.src
, &shader_version
);
2350 shader_addline(&buffer
, ", %s",
2351 ins
.flags
& WINED3DSI_INDEXED_DYNAMIC
? "dynamicIndexed" : "immediateIndexed");
2353 else if (ins
.handler_idx
== WINED3DSIH_DCL_FUNCTION_BODY
)
2355 shader_addline(&buffer
, "%s fb%u",
2356 shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.index
);
2358 else if (ins
.handler_idx
== WINED3DSIH_DCL_FUNCTION_TABLE
)
2360 shader_addline(&buffer
, "%s ft%u = {...}",
2361 shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.index
);
2363 else if (ins
.handler_idx
== WINED3DSIH_DCL_GLOBAL_FLAGS
)
2365 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2366 shader_dump_global_flags(&buffer
, ins
.flags
);
2368 else if (ins
.handler_idx
== WINED3DSIH_DCL_HS_MAX_TESSFACTOR
)
2370 shader_addline(&buffer
, "%s %.8e", shader_opcode_names
[ins
.handler_idx
],
2371 ins
.declaration
.max_tessellation_factor
);
2373 else if (ins
.handler_idx
== WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER
)
2375 shader_addline(&buffer
, "%s {\n", shader_opcode_names
[ins
.handler_idx
]);
2376 for (i
= 0; i
< ins
.declaration
.icb
->vec4_count
; ++i
)
2378 shader_addline(&buffer
, " {0x%08x, 0x%08x, 0x%08x, 0x%08x},\n",
2379 ins
.declaration
.icb
->data
[4 * i
+ 0],
2380 ins
.declaration
.icb
->data
[4 * i
+ 1],
2381 ins
.declaration
.icb
->data
[4 * i
+ 2],
2382 ins
.declaration
.icb
->data
[4 * i
+ 3]);
2384 shader_addline(&buffer
, "}");
2386 else if (ins
.handler_idx
== WINED3DSIH_DCL_INDEXABLE_TEMP
)
2388 shader_addline(&buffer
, "%s x[%u][%u], %u", shader_opcode_names
[ins
.handler_idx
],
2389 ins
.declaration
.indexable_temp
.register_idx
,
2390 ins
.declaration
.indexable_temp
.register_size
,
2391 ins
.declaration
.indexable_temp
.component_count
);
2393 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PS
)
2395 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2396 shader_dump_interpolation_mode(&buffer
, ins
.flags
);
2397 shader_addline(&buffer
, " ");
2398 shader_dump_dst_param(&buffer
, &ins
.declaration
.dst
, &shader_version
);
2400 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PS_SGV
2401 || ins
.handler_idx
== WINED3DSIH_DCL_INPUT_SGV
2402 || ins
.handler_idx
== WINED3DSIH_DCL_INPUT_SIV
2403 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_SIV
)
2405 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2406 shader_dump_dst_param(&buffer
, &ins
.declaration
.register_semantic
.reg
, &shader_version
);
2407 shader_addline(&buffer
, ", ");
2408 shader_dump_sysval_semantic(&buffer
, ins
.declaration
.register_semantic
.sysval_semantic
);
2410 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PS_SIV
)
2412 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2413 shader_dump_interpolation_mode(&buffer
, ins
.flags
);
2414 shader_addline(&buffer
, " ");
2415 shader_dump_dst_param(&buffer
, &ins
.declaration
.register_semantic
.reg
, &shader_version
);
2416 shader_addline(&buffer
, ", ");
2417 shader_dump_sysval_semantic(&buffer
, ins
.declaration
.register_semantic
.sysval_semantic
);
2419 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT
2420 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT
)
2422 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2423 shader_dump_dst_param(&buffer
, &ins
.declaration
.dst
, &shader_version
);
2425 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
2426 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
2428 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2429 shader_dump_primitive_type(&buffer
, ins
.declaration
.primitive_type
);
2431 else if (ins
.handler_idx
== WINED3DSIH_DCL_INTERFACE
)
2433 shader_addline(&buffer
, "%s fp[%u][%u][%u] = {...}",
2434 shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.fp
.index
,
2435 ins
.declaration
.fp
.array_size
, ins
.declaration
.fp
.body_count
);
2437 else if (ins
.handler_idx
== WINED3DSIH_DCL_RESOURCE_STRUCTURED
)
2439 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2440 shader_dump_dst_param(&buffer
, &ins
.declaration
.structured_resource
.reg
, &shader_version
);
2441 shader_addline(&buffer
, ", %u", ins
.declaration
.structured_resource
.byte_stride
);
2443 else if (ins
.handler_idx
== WINED3DSIH_DCL_SAMPLER
)
2445 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2446 shader_dump_dst_param(&buffer
, &ins
.declaration
.dst
, &shader_version
);
2447 if (ins
.flags
== WINED3DSI_SAMPLER_COMPARISON_MODE
)
2448 shader_addline(&buffer
, ", comparisonMode");
2450 else if (ins
.handler_idx
== WINED3DSIH_DCL_TEMPS
2451 || ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
2452 || ins
.handler_idx
== WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT
2453 || ins
.handler_idx
== WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT
2454 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT
)
2456 shader_addline(&buffer
, "%s %u", shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.count
);
2458 else if (ins
.handler_idx
== WINED3DSIH_DCL_TESSELLATOR_DOMAIN
)
2460 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2461 shader_dump_tessellator_domain(&buffer
, ins
.declaration
.tessellator_domain
);
2463 else if (ins
.handler_idx
== WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE
)
2465 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2466 shader_dump_tessellator_output_primitive(&buffer
, ins
.declaration
.tessellator_output_primitive
);
2468 else if (ins
.handler_idx
== WINED3DSIH_DCL_TESSELLATOR_PARTITIONING
)
2470 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2471 shader_dump_tessellator_partitioning(&buffer
, ins
.declaration
.tessellator_partitioning
);
2473 else if (ins
.handler_idx
== WINED3DSIH_DCL_TGSM_RAW
)
2475 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2476 shader_dump_dst_param(&buffer
, &ins
.declaration
.tgsm_raw
.reg
, &shader_version
);
2477 shader_addline(&buffer
, ", %u", ins
.declaration
.tgsm_raw
.byte_count
);
2479 else if (ins
.handler_idx
== WINED3DSIH_DCL_TGSM_STRUCTURED
)
2481 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2482 shader_dump_dst_param(&buffer
, &ins
.declaration
.tgsm_structured
.reg
, &shader_version
);
2483 shader_addline(&buffer
, ", %u, %u", ins
.declaration
.tgsm_structured
.byte_stride
,
2484 ins
.declaration
.tgsm_structured
.structure_count
);
2486 else if (ins
.handler_idx
== WINED3DSIH_DCL_THREAD_GROUP
)
2488 shader_addline(&buffer
, "%s %u, %u, %u", shader_opcode_names
[ins
.handler_idx
],
2489 ins
.declaration
.thread_group_size
.x
,
2490 ins
.declaration
.thread_group_size
.y
,
2491 ins
.declaration
.thread_group_size
.z
);
2493 else if (ins
.handler_idx
== WINED3DSIH_DCL_UAV_RAW
)
2495 shader_addline(&buffer
, "%s", shader_opcode_names
[ins
.handler_idx
]);
2496 shader_dump_uav_flags(&buffer
, ins
.flags
);
2497 shader_addline(&buffer
, " ");
2498 shader_dump_dst_param(&buffer
, &ins
.declaration
.dst
, &shader_version
);
2500 else if (ins
.handler_idx
== WINED3DSIH_DCL_UAV_STRUCTURED
)
2502 shader_addline(&buffer
, "%s", shader_opcode_names
[ins
.handler_idx
]);
2503 shader_dump_uav_flags(&buffer
, ins
.flags
);
2504 shader_addline(&buffer
, " ");
2505 shader_dump_dst_param(&buffer
, &ins
.declaration
.structured_resource
.reg
, &shader_version
);
2506 shader_addline(&buffer
, ", %u", ins
.declaration
.structured_resource
.byte_stride
);
2508 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
2510 shader_addline(&buffer
, "def c%u = %.8e, %.8e, %.8e, %.8e", shader_get_float_offset(ins
.dst
[0].reg
.type
,
2511 ins
.dst
[0].reg
.idx
[0].offset
),
2512 *(const float *)&ins
.src
[0].reg
.u
.immconst_data
[0],
2513 *(const float *)&ins
.src
[0].reg
.u
.immconst_data
[1],
2514 *(const float *)&ins
.src
[0].reg
.u
.immconst_data
[2],
2515 *(const float *)&ins
.src
[0].reg
.u
.immconst_data
[3]);
2517 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
2519 shader_addline(&buffer
, "defi i%u = %d, %d, %d, %d", ins
.dst
[0].reg
.idx
[0].offset
,
2520 ins
.src
[0].reg
.u
.immconst_data
[0],
2521 ins
.src
[0].reg
.u
.immconst_data
[1],
2522 ins
.src
[0].reg
.u
.immconst_data
[2],
2523 ins
.src
[0].reg
.u
.immconst_data
[3]);
2525 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
2527 shader_addline(&buffer
, "defb b%u = %s",
2528 ins
.dst
[0].reg
.idx
[0].offset
, ins
.src
[0].reg
.u
.immconst_data
[0] ? "true" : "false");
2534 shader_addline(&buffer
, "(");
2535 shader_dump_src_param(&buffer
, ins
.predicate
, &shader_version
);
2536 shader_addline(&buffer
, ") ");
2539 /* PixWin marks instructions with the coissue flag with a '+' */
2541 shader_addline(&buffer
, "+");
2543 shader_addline(&buffer
, "%s", shader_opcode_names
[ins
.handler_idx
]);
2545 if (ins
.handler_idx
== WINED3DSIH_BREAKP
2546 || ins
.handler_idx
== WINED3DSIH_IF
)
2550 case WINED3D_SHADER_CONDITIONAL_OP_NZ
: shader_addline(&buffer
, "_nz"); break;
2551 case WINED3D_SHADER_CONDITIONAL_OP_Z
: shader_addline(&buffer
, "_z"); break;
2552 default: shader_addline(&buffer
, "_unrecognized(%#x)", ins
.flags
); break;
2555 else if (ins
.handler_idx
== WINED3DSIH_IFC
2556 || ins
.handler_idx
== WINED3DSIH_BREAKC
)
2560 case WINED3D_SHADER_REL_OP_GT
: shader_addline(&buffer
, "_gt"); break;
2561 case WINED3D_SHADER_REL_OP_EQ
: shader_addline(&buffer
, "_eq"); break;
2562 case WINED3D_SHADER_REL_OP_GE
: shader_addline(&buffer
, "_ge"); break;
2563 case WINED3D_SHADER_REL_OP_LT
: shader_addline(&buffer
, "_lt"); break;
2564 case WINED3D_SHADER_REL_OP_NE
: shader_addline(&buffer
, "_ne"); break;
2565 case WINED3D_SHADER_REL_OP_LE
: shader_addline(&buffer
, "_le"); break;
2566 default: shader_addline(&buffer
, "_(%u)", ins
.flags
);
2569 else if (ins
.handler_idx
== WINED3DSIH_TEX
2570 && shader_version
.major
>= 2
2571 && (ins
.flags
& WINED3DSI_TEXLD_PROJECT
))
2573 shader_addline(&buffer
, "p");
2575 else if (ins
.handler_idx
== WINED3DSIH_RESINFO
&& ins
.flags
)
2579 case WINED3DSI_RESINFO_RCP_FLOAT
: shader_addline(&buffer
, "_rcpFloat"); break;
2580 case WINED3DSI_RESINFO_UINT
: shader_addline(&buffer
, "_uint"); break;
2581 default: shader_addline(&buffer
, "_unrecognized(%#x)", ins
.flags
);
2584 else if (ins
.handler_idx
== WINED3DSIH_SAMPLE_INFO
&& ins
.flags
)
2588 case WINED3DSI_SAMPLE_INFO_UINT
: shader_addline(&buffer
, "_uint"); break;
2589 default: shader_addline(&buffer
, "_unrecognized(%#x)", ins
.flags
);
2592 else if (ins
.handler_idx
== WINED3DSIH_SYNC
)
2594 shader_dump_sync_flags(&buffer
, ins
.flags
);
2597 if (wined3d_shader_instruction_has_texel_offset(&ins
))
2598 shader_addline(&buffer
, "(%d,%d,%d)", ins
.texel_offset
.u
, ins
.texel_offset
.v
, ins
.texel_offset
.w
);
2600 for (i
= 0; i
< ins
.dst_count
; ++i
)
2602 shader_dump_ins_modifiers(&buffer
, &ins
.dst
[i
]);
2603 shader_addline(&buffer
, !i
? " " : ", ");
2604 shader_dump_dst_param(&buffer
, &ins
.dst
[i
], &shader_version
);
2607 /* Other source tokens */
2608 for (i
= ins
.dst_count
; i
< (ins
.dst_count
+ ins
.src_count
); ++i
)
2610 shader_addline(&buffer
, !i
? " " : ", ");
2611 shader_dump_src_param(&buffer
, &ins
.src
[i
- ins
.dst_count
], &shader_version
);
2614 shader_addline(&buffer
, "\n");
2617 for (p
= buffer
.buffer
; *p
; p
= q
)
2619 if (!(q
= strstr(p
, "\n")))
2623 TRACE(" %.*s", (int)(q
- p
), p
);
2626 string_buffer_free(&buffer
);
2629 static void shader_cleanup(struct wined3d_shader
*shader
)
2631 HeapFree(GetProcessHeap(), 0, shader
->output_signature
.elements
);
2632 HeapFree(GetProcessHeap(), 0, shader
->input_signature
.elements
);
2633 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
2634 shader
->device
->shader_backend
->shader_destroy(shader
);
2635 shader_cleanup_reg_maps(&shader
->reg_maps
);
2636 HeapFree(GetProcessHeap(), 0, shader
->function
);
2637 shader_delete_constant_list(&shader
->constantsF
);
2638 shader_delete_constant_list(&shader
->constantsB
);
2639 shader_delete_constant_list(&shader
->constantsI
);
2640 list_remove(&shader
->shader_list_entry
);
2642 if (shader
->frontend
&& shader
->frontend_data
)
2643 shader
->frontend
->shader_free(shader
->frontend_data
);
2646 struct shader_none_priv
2648 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
2649 const struct fragment_pipeline
*fragment_pipe
;
2650 BOOL ffp_proj_control
;
2653 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
2654 static void shader_none_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
2655 enum wined3d_gl_resource_type tex_type
, const SIZE
*ds_mask_size
) {}
2656 static void shader_none_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
) {}
2657 static void shader_none_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
2658 static void shader_none_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
2659 static void shader_none_load_constants(void *shader_priv
, struct wined3d_context
*context
,
2660 const struct wined3d_state
*state
) {}
2661 static void shader_none_destroy(struct wined3d_shader
*shader
) {}
2662 static void shader_none_free_context_data(struct wined3d_context
*context
) {}
2663 static void shader_none_init_context_state(struct wined3d_context
*context
) {}
2665 /* Context activation is done by the caller. */
2666 static void shader_none_select(void *shader_priv
, struct wined3d_context
*context
,
2667 const struct wined3d_state
*state
)
2669 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2670 struct shader_none_priv
*priv
= shader_priv
;
2672 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
2673 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
2676 /* Context activation is done by the caller. */
2677 static void shader_none_disable(void *shader_priv
, struct wined3d_context
*context
)
2679 struct shader_none_priv
*priv
= shader_priv
;
2680 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2682 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
2683 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
2685 context
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
2686 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
2687 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
2688 | (1u << WINED3D_SHADER_TYPE_HULL
)
2689 | (1u << WINED3D_SHADER_TYPE_DOMAIN
)
2690 | (1u << WINED3D_SHADER_TYPE_COMPUTE
);
2693 static HRESULT
shader_none_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
2694 const struct fragment_pipeline
*fragment_pipe
)
2696 struct fragment_caps fragment_caps
;
2697 void *vertex_priv
, *fragment_priv
;
2698 struct shader_none_priv
*priv
;
2700 if (!(priv
= HeapAlloc(GetProcessHeap(), 0, sizeof(*priv
))))
2701 return E_OUTOFMEMORY
;
2703 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&none_shader_backend
, priv
)))
2705 ERR("Failed to initialize vertex pipe.\n");
2706 HeapFree(GetProcessHeap(), 0, priv
);
2710 if (!(fragment_priv
= fragment_pipe
->alloc_private(&none_shader_backend
, priv
)))
2712 ERR("Failed to initialize fragment pipe.\n");
2713 vertex_pipe
->vp_free(device
);
2714 HeapFree(GetProcessHeap(), 0, priv
);
2718 priv
->vertex_pipe
= vertex_pipe
;
2719 priv
->fragment_pipe
= fragment_pipe
;
2720 fragment_pipe
->get_caps(&device
->adapter
->gl_info
, &fragment_caps
);
2721 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
2723 device
->vertex_priv
= vertex_priv
;
2724 device
->fragment_priv
= fragment_priv
;
2725 device
->shader_priv
= priv
;
2730 static void shader_none_free(struct wined3d_device
*device
)
2732 struct shader_none_priv
*priv
= device
->shader_priv
;
2734 priv
->fragment_pipe
->free_private(device
);
2735 priv
->vertex_pipe
->vp_free(device
);
2736 HeapFree(GetProcessHeap(), 0, priv
);
2739 static BOOL
shader_none_allocate_context_data(struct wined3d_context
*context
)
2744 static void shader_none_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
2746 /* Set the shader caps to 0 for the none shader backend */
2747 caps
->vs_version
= 0;
2748 caps
->hs_version
= 0;
2749 caps
->ds_version
= 0;
2750 caps
->gs_version
= 0;
2751 caps
->ps_version
= 0;
2752 caps
->cs_version
= 0;
2753 caps
->vs_uniform_count
= 0;
2754 caps
->ps_uniform_count
= 0;
2755 caps
->ps_1x_max_value
= 0.0f
;
2756 caps
->varying_count
= 0;
2757 caps
->wined3d_caps
= 0;
2760 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
2762 /* We "support" every possible fixup, since we don't support any shader
2763 * model, and will never have to actually sample a texture. */
2767 static BOOL
shader_none_has_ffp_proj_control(void *shader_priv
)
2769 struct shader_none_priv
*priv
= shader_priv
;
2771 return priv
->ffp_proj_control
;
2774 const struct wined3d_shader_backend_ops none_shader_backend
=
2776 shader_none_handle_instruction
,
2778 shader_none_disable
,
2779 shader_none_select_depth_blt
,
2780 shader_none_deselect_depth_blt
,
2781 shader_none_update_float_vertex_constants
,
2782 shader_none_update_float_pixel_constants
,
2783 shader_none_load_constants
,
2784 shader_none_destroy
,
2787 shader_none_allocate_context_data
,
2788 shader_none_free_context_data
,
2789 shader_none_init_context_state
,
2790 shader_none_get_caps
,
2791 shader_none_color_fixup_supported
,
2792 shader_none_has_ffp_proj_control
,
2795 static HRESULT
shader_set_function(struct wined3d_shader
*shader
, const DWORD
*byte_code
,
2796 DWORD float_const_count
, enum wined3d_shader_type type
, unsigned int max_version
)
2798 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2799 const struct wined3d_shader_frontend
*fe
;
2801 unsigned int backend_version
;
2802 const struct wined3d_d3d_info
*d3d_info
= &shader
->device
->adapter
->d3d_info
;
2804 TRACE("shader %p, byte_code %p, float_const_count %u, type %#x, max_version %u.\n",
2805 shader
, byte_code
, float_const_count
, type
, max_version
);
2807 list_init(&shader
->constantsF
);
2808 list_init(&shader
->constantsB
);
2809 list_init(&shader
->constantsI
);
2810 shader
->lconst_inf_or_nan
= FALSE
;
2811 list_init(®_maps
->indexable_temps
);
2813 fe
= shader_select_frontend(*byte_code
);
2816 FIXME("Unable to find frontend for shader.\n");
2817 return WINED3DERR_INVALIDCALL
;
2819 shader
->frontend
= fe
;
2820 shader
->frontend_data
= fe
->shader_init(byte_code
, &shader
->output_signature
);
2821 if (!shader
->frontend_data
)
2823 FIXME("Failed to initialize frontend.\n");
2824 return WINED3DERR_INVALIDCALL
;
2827 /* First pass: trace shader. */
2828 if (TRACE_ON(d3d_shader
))
2829 shader_trace_init(fe
, shader
->frontend_data
, byte_code
);
2831 /* Second pass: figure out which registers are used, what the semantics are, etc. */
2832 if (FAILED(hr
= shader_get_registers_used(shader
, fe
, reg_maps
, &shader
->input_signature
,
2833 &shader
->output_signature
, byte_code
, float_const_count
)))
2836 if (reg_maps
->shader_version
.type
!= type
)
2838 WARN("Wrong shader type %d.\n", reg_maps
->shader_version
.type
);
2839 return WINED3DERR_INVALIDCALL
;
2841 if (reg_maps
->shader_version
.major
> max_version
)
2843 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps
->shader_version
.major
);
2844 return WINED3DERR_INVALIDCALL
;
2848 case WINED3D_SHADER_TYPE_VERTEX
:
2849 backend_version
= d3d_info
->limits
.vs_version
;
2851 case WINED3D_SHADER_TYPE_HULL
:
2852 backend_version
= d3d_info
->limits
.hs_version
;
2854 case WINED3D_SHADER_TYPE_DOMAIN
:
2855 backend_version
= d3d_info
->limits
.ds_version
;
2857 case WINED3D_SHADER_TYPE_GEOMETRY
:
2858 backend_version
= d3d_info
->limits
.gs_version
;
2860 case WINED3D_SHADER_TYPE_PIXEL
:
2861 backend_version
= d3d_info
->limits
.ps_version
;
2863 case WINED3D_SHADER_TYPE_COMPUTE
:
2864 backend_version
= d3d_info
->limits
.cs_version
;
2867 FIXME("No backend version-checking for this shader type.\n");
2868 backend_version
= 0;
2870 if (reg_maps
->shader_version
.major
> backend_version
)
2872 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
2873 reg_maps
->shader_version
.major
, reg_maps
->shader_version
.minor
);
2874 return WINED3DERR_INVALIDCALL
;
2877 if (!(shader
->function
= HeapAlloc(GetProcessHeap(), 0, shader
->functionLength
)))
2878 return E_OUTOFMEMORY
;
2879 memcpy(shader
->function
, byte_code
, shader
->functionLength
);
2884 ULONG CDECL
wined3d_shader_incref(struct wined3d_shader
*shader
)
2886 ULONG refcount
= InterlockedIncrement(&shader
->ref
);
2888 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
2893 static void wined3d_shader_destroy_object(void *object
)
2895 shader_cleanup(object
);
2896 HeapFree(GetProcessHeap(), 0, object
);
2899 ULONG CDECL
wined3d_shader_decref(struct wined3d_shader
*shader
)
2901 ULONG refcount
= InterlockedDecrement(&shader
->ref
);
2903 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
2907 shader
->parent_ops
->wined3d_object_destroyed(shader
->parent
);
2908 wined3d_cs_emit_destroy_object(shader
->device
->cs
, wined3d_shader_destroy_object
, shader
);
2914 void * CDECL
wined3d_shader_get_parent(const struct wined3d_shader
*shader
)
2916 TRACE("shader %p.\n", shader
);
2918 return shader
->parent
;
2921 HRESULT CDECL
wined3d_shader_get_byte_code(const struct wined3d_shader
*shader
,
2922 void *byte_code
, UINT
*byte_code_size
)
2924 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader
, byte_code
, byte_code_size
);
2928 *byte_code_size
= shader
->functionLength
;
2932 if (*byte_code_size
< shader
->functionLength
)
2934 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
2935 * than the required size we should write the required size and
2936 * return D3DERR_MOREDATA. That's not actually true. */
2937 return WINED3DERR_INVALIDCALL
;
2940 memcpy(byte_code
, shader
->function
, shader
->functionLength
);
2945 /* Set local constants for d3d8 shaders. */
2946 HRESULT CDECL
wined3d_shader_set_local_constants_float(struct wined3d_shader
*shader
,
2947 UINT start_idx
, const float *src_data
, UINT count
)
2949 UINT end_idx
= start_idx
+ count
;
2952 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader
, start_idx
, src_data
, count
);
2954 if (end_idx
> shader
->limits
->constant_float
)
2956 WARN("end_idx %u > float constants limit %u.\n",
2957 end_idx
, shader
->limits
->constant_float
);
2958 end_idx
= shader
->limits
->constant_float
;
2961 for (i
= start_idx
; i
< end_idx
; ++i
)
2963 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
2966 return E_OUTOFMEMORY
;
2969 value
= (float *)lconst
->value
;
2970 memcpy(value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
2971 list_add_head(&shader
->constantsF
, &lconst
->entry
);
2973 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
2974 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
2976 shader
->lconst_inf_or_nan
= TRUE
;
2983 void find_vs_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2984 WORD swizzle_map
, struct vs_compile_args
*args
, const struct wined3d_d3d_info
*d3d_info
)
2986 args
->fog_src
= state
->render_states
[WINED3D_RS_FOGTABLEMODE
]
2987 == WINED3D_FOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
2988 args
->clip_enabled
= state
->render_states
[WINED3D_RS_CLIPPING
]
2989 && state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
];
2990 args
->point_size
= state
->gl_primitive_type
== GL_POINTS
;
2991 args
->per_vertex_point_size
= shader
->reg_maps
.point_size
;
2992 args
->next_shader_type
= state
->shader
[WINED3D_SHADER_TYPE_HULL
] ? WINED3D_SHADER_TYPE_HULL
2993 : state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
] ? WINED3D_SHADER_TYPE_GEOMETRY
2994 : WINED3D_SHADER_TYPE_PIXEL
;
2995 if (shader
->reg_maps
.shader_version
.major
>= 4)
2996 args
->next_shader_input_count
= state
->shader
[WINED3D_SHADER_TYPE_HULL
]
2997 ? state
->shader
[WINED3D_SHADER_TYPE_HULL
]->limits
->packed_input
2998 : state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
]
2999 ? state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
]->limits
->packed_input
3000 : state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]
3001 ? state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->limits
->packed_input
: 0;
3003 args
->next_shader_input_count
= 0;
3004 args
->swizzle_map
= swizzle_map
;
3005 if (d3d_info
->emulated_flatshading
)
3006 args
->flatshading
= state
->render_states
[WINED3D_RS_SHADEMODE
] == WINED3D_SHADE_FLAT
;
3008 args
->flatshading
= 0;
3011 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
3013 if (usage_idx1
!= usage_idx2
)
3015 if (usage1
== usage2
)
3017 if (usage1
== WINED3D_DECL_USAGE_POSITION
&& usage2
== WINED3D_DECL_USAGE_POSITIONT
)
3019 if (usage2
== WINED3D_DECL_USAGE_POSITION
&& usage1
== WINED3D_DECL_USAGE_POSITIONT
)
3025 BOOL
vshader_get_input(const struct wined3d_shader
*shader
,
3026 BYTE usage_req
, BYTE usage_idx_req
, unsigned int *regnum
)
3028 WORD map
= shader
->reg_maps
.input_registers
;
3031 for (i
= 0; map
; map
>>= 1, ++i
)
3033 if (!(map
& 1)) continue;
3035 if (match_usage(shader
->u
.vs
.attributes
[i
].usage
,
3036 shader
->u
.vs
.attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
3045 static HRESULT
shader_signature_copy(struct wined3d_shader_signature
*dst
,
3046 const struct wined3d_shader_signature
*src
, char **signature_strings
)
3048 struct wined3d_shader_signature_element
*e
;
3053 if (!src
->element_count
)
3056 ptr
= *signature_strings
;
3058 dst
->element_count
= src
->element_count
;
3059 if (!(dst
->elements
= wined3d_calloc(dst
->element_count
, sizeof(*dst
->elements
))))
3060 return E_OUTOFMEMORY
;
3062 for (i
= 0; i
< src
->element_count
; ++i
)
3064 e
= &src
->elements
[i
];
3065 dst
->elements
[i
] = *e
;
3067 len
= strlen(e
->semantic_name
);
3068 memcpy(ptr
, e
->semantic_name
, len
+ 1);
3069 dst
->elements
[i
].semantic_name
= ptr
;
3073 *signature_strings
= ptr
;
3078 static HRESULT
shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3079 const struct wined3d_shader_desc
*desc
, DWORD float_const_count
, enum wined3d_shader_type type
,
3080 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3082 struct wined3d_shader_signature_element
*e
;
3088 if (!desc
->byte_code
)
3089 return WINED3DERR_INVALIDCALL
;
3092 shader
->device
= device
;
3093 shader
->parent
= parent
;
3094 shader
->parent_ops
= parent_ops
;
3097 for (i
= 0; i
< desc
->input_signature
.element_count
; ++i
)
3099 e
= &desc
->input_signature
.elements
[i
];
3100 len
= strlen(e
->semantic_name
);
3101 if (len
>= ~(SIZE_T
)0 - total
)
3102 return E_OUTOFMEMORY
;
3106 for (i
= 0; i
< desc
->output_signature
.element_count
; ++i
)
3108 e
= &desc
->output_signature
.elements
[i
];
3109 len
= strlen(e
->semantic_name
);
3110 if (len
>= ~(SIZE_T
)0 - total
)
3111 return E_OUTOFMEMORY
;
3115 if (total
&& !(shader
->signature_strings
= HeapAlloc(GetProcessHeap(), 0, total
)))
3116 return E_OUTOFMEMORY
;
3117 ptr
= shader
->signature_strings
;
3119 if (FAILED(hr
= shader_signature_copy(&shader
->input_signature
, &desc
->input_signature
, &ptr
)))
3121 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
3124 if (FAILED(hr
= shader_signature_copy(&shader
->output_signature
, &desc
->output_signature
, &ptr
)))
3126 HeapFree(GetProcessHeap(), 0, shader
->input_signature
.elements
);
3127 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
3131 list_init(&shader
->linked_programs
);
3132 list_add_head(&device
->shaders
, &shader
->shader_list_entry
);
3134 if (FAILED(hr
= shader_set_function(shader
, desc
->byte_code
,
3135 float_const_count
, type
, desc
->max_version
)))
3137 WARN("Failed to set function, hr %#x.\n", hr
);
3138 shader_cleanup(shader
);
3141 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
3146 static HRESULT
vertex_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3147 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3149 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
3153 if (FAILED(hr
= shader_init(shader
, device
, desc
, device
->adapter
->d3d_info
.limits
.vs_uniform_count
,
3154 WINED3D_SHADER_TYPE_VERTEX
, parent
, parent_ops
)))
3157 for (i
= 0; i
< shader
->input_signature
.element_count
; ++i
)
3159 const struct wined3d_shader_signature_element
*input
= &shader
->input_signature
.elements
[i
];
3161 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)) || !input
->semantic_name
)
3164 shader
->u
.vs
.attributes
[input
->register_idx
].usage
=
3165 shader_usage_from_semantic_name(input
->semantic_name
);
3166 shader
->u
.vs
.attributes
[input
->register_idx
].usage_idx
= input
->semantic_idx
;
3169 if (reg_maps
->usesrelconstF
&& !list_empty(&shader
->constantsF
))
3170 shader
->load_local_constsF
= TRUE
;
3175 static HRESULT
domain_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3176 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3178 return shader_init(shader
, device
, desc
, 0, WINED3D_SHADER_TYPE_DOMAIN
, parent
, parent_ops
);
3181 static HRESULT
hull_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3182 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3184 return shader_init(shader
, device
, desc
, 0, WINED3D_SHADER_TYPE_HULL
, parent
, parent_ops
);
3187 static HRESULT
geometry_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3188 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3190 return shader_init(shader
, device
, desc
, 0, WINED3D_SHADER_TYPE_GEOMETRY
, parent
, parent_ops
);
3193 void find_gs_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
3194 struct gs_compile_args
*args
)
3196 args
->ps_input_count
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]
3197 ? state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->limits
->packed_input
: 0;
3200 void find_ps_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
3201 BOOL position_transformed
, struct ps_compile_args
*args
, const struct wined3d_context
*context
)
3203 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3204 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3205 const struct wined3d_texture
*texture
;
3208 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
3209 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && needs_srgb_write(context
, state
, state
->fb
))
3211 static unsigned int warned
= 0;
3213 args
->srgb_correction
= 1;
3214 if (state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] && !warned
++)
3215 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
3216 "support, expect rendering artifacts.\n");
3219 if (shader
->reg_maps
.shader_version
.major
== 1
3220 && shader
->reg_maps
.shader_version
.minor
<= 3)
3222 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
3224 DWORD flags
= state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
];
3226 if (flags
& WINED3D_TTFF_PROJECTED
)
3228 DWORD tex_transform
= flags
& ~WINED3D_TTFF_PROJECTED
;
3230 if (!state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
3232 enum wined3d_shader_resource_type resource_type
= shader
->reg_maps
.resource_info
[i
].type
;
3234 unsigned int index
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
3235 DWORD max_valid
= WINED3D_TTFF_COUNT4
;
3237 for (j
= 0; j
< state
->vertex_declaration
->element_count
; ++j
)
3239 struct wined3d_vertex_declaration_element
*element
=
3240 &state
->vertex_declaration
->elements
[j
];
3242 if (element
->usage
== WINED3D_DECL_USAGE_TEXCOORD
3243 && element
->usage_idx
== index
)
3245 max_valid
= element
->format
->component_count
;
3249 if (!tex_transform
|| tex_transform
> max_valid
)
3251 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
3252 tex_transform
, max_valid
);
3253 tex_transform
= max_valid
;
3255 if ((resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_1D
&& tex_transform
> WINED3D_TTFF_COUNT1
)
3256 || (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2D
3257 && tex_transform
> WINED3D_TTFF_COUNT2
)
3258 || (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_3D
3259 && tex_transform
> WINED3D_TTFF_COUNT3
))
3260 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
3263 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
3264 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
3265 i
, tex_transform
, resource_type
);
3269 tex_transform
= WINED3D_TTFF_COUNT4
| WINED3D_PSARGS_PROJECTED
;
3271 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
3275 if (shader
->reg_maps
.shader_version
.major
== 1
3276 && shader
->reg_maps
.shader_version
.minor
<= 4)
3278 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
3280 const struct wined3d_texture
*texture
= state
->textures
[i
];
3282 if (!shader
->reg_maps
.resource_info
[i
].type
)
3285 /* Treat unbound textures as 2D. The dummy texture will provide
3286 * the proper sample value. The tex_types bitmap defaults to
3287 * 2D because of the memset. */
3291 switch (texture
->target
)
3293 /* RECT textures are distinguished from 2D textures via np2_fixup */
3294 case GL_TEXTURE_RECTANGLE_ARB
:
3299 args
->tex_types
|= WINED3D_SHADER_TEX_3D
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
3302 case GL_TEXTURE_CUBE_MAP_ARB
:
3303 args
->tex_types
|= WINED3D_SHADER_TEX_CUBE
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
3309 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3311 if (!shader
->reg_maps
.resource_info
[i
].type
)
3314 texture
= state
->textures
[i
];
3317 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
3320 if (can_use_texture_swizzle(gl_info
, texture
->resource
.format
))
3321 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
3323 args
->color_fixup
[i
] = texture
->resource
.format
->color_fixup
;
3325 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_SHADOW
)
3326 args
->shadow
|= 1u << i
;
3328 /* Flag samplers that need NP2 texcoord fixup. */
3329 if (!(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
3330 args
->np2_fixup
|= (1u << i
);
3333 /* In SM4+ we use dcl_sampler in order to determine if we should use shadow sampler. */
3334 if (shader
->reg_maps
.shader_version
.major
>= 4)
3337 if (shader
->reg_maps
.shader_version
.major
>= 3)
3339 if (position_transformed
)
3340 args
->vp_mode
= pretransformed
;
3341 else if (use_vs(state
))
3342 args
->vp_mode
= vertexshader
;
3344 args
->vp_mode
= fixedfunction
;
3345 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
3349 args
->vp_mode
= vertexshader
;
3350 if (state
->render_states
[WINED3D_RS_FOGENABLE
])
3352 switch (state
->render_states
[WINED3D_RS_FOGTABLEMODE
])
3354 case WINED3D_FOG_NONE
:
3355 if (position_transformed
|| use_vs(state
))
3357 args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
;
3361 switch (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
])
3363 case WINED3D_FOG_NONE
: /* Fall through. */
3364 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
3365 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
3366 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
3370 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
3371 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
3372 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
3377 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
3381 if (context
->d3d_info
->limits
.varying_count
< wined3d_max_compat_varyings(context
->gl_info
))
3383 const struct wined3d_shader
*vs
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
3385 args
->texcoords_initialized
= 0;
3386 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
3390 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.output_registers
& (1u << i
))
3391 args
->texcoords_initialized
|= 1u << i
;
3395 const struct wined3d_stream_info
*si
= &context
->stream_info
;
3396 unsigned int coord_idx
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
3398 if ((state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
] >> WINED3D_FFP_TCI_SHIFT
)
3399 & WINED3D_FFP_TCI_MASK
3400 || (coord_idx
< MAX_TEXTURES
&& (si
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ coord_idx
)))))
3401 args
->texcoords_initialized
|= 1u << i
;
3407 args
->texcoords_initialized
= (1u << MAX_TEXTURES
) - 1;
3410 args
->pointsprite
= state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
]
3411 && state
->gl_primitive_type
== GL_POINTS
;
3413 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
3414 args
->alpha_test_func
= WINED3D_CMP_ALWAYS
- 1;
3416 args
->alpha_test_func
= (state
->render_states
[WINED3D_RS_ALPHATESTENABLE
]
3417 ? wined3d_sanitize_cmp_func(state
->render_states
[WINED3D_RS_ALPHAFUNC
])
3418 : WINED3D_CMP_ALWAYS
) - 1;
3420 if (d3d_info
->emulated_flatshading
)
3421 args
->flatshading
= state
->render_states
[WINED3D_RS_SHADEMODE
] == WINED3D_SHADE_FLAT
;
3423 args
->render_offscreen
= shader
->reg_maps
.vpos
&& gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
]
3424 ? context
->render_offscreen
: 0;
3427 static HRESULT
pixel_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3428 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3430 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
3431 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
3434 if (FAILED(hr
= shader_init(shader
, device
, desc
, device
->adapter
->d3d_info
.limits
.ps_uniform_count
,
3435 WINED3D_SHADER_TYPE_PIXEL
, parent
, parent_ops
)))
3438 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
3440 if (shader
->u
.ps
.input_reg_used
[i
])
3443 highest_reg_used
= i
;
3447 /* Don't do any register mapping magic if it is not needed, or if we can't
3448 * achieve anything anyway */
3449 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
3450 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4)
3451 || shader
->reg_maps
.shader_version
.major
>= 4)
3453 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
3455 /* This happens with relative addressing. The input mapper function
3456 * warns about this if the higher registers are declared too, so
3457 * don't write a FIXME here */
3458 WARN("More varying registers used than supported\n");
3461 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
3463 shader
->u
.ps
.input_reg_map
[i
] = i
;
3466 shader
->u
.ps
.declared_in_count
= highest_reg_used
+ 1;
3470 shader
->u
.ps
.declared_in_count
= 0;
3471 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
3473 if (shader
->u
.ps
.input_reg_used
[i
])
3474 shader
->u
.ps
.input_reg_map
[i
] = shader
->u
.ps
.declared_in_count
++;
3475 else shader
->u
.ps
.input_reg_map
[i
] = ~0U;
3482 void pixelshader_update_resource_types(struct wined3d_shader
*shader
, WORD tex_types
)
3484 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
3485 struct wined3d_shader_resource_info
*resource_info
= reg_maps
->resource_info
;
3488 if (reg_maps
->shader_version
.major
!= 1) return;
3490 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
3492 /* We don't sample from this sampler. */
3493 if (!resource_info
[i
].type
)
3496 switch ((tex_types
>> i
* WINED3D_PSARGS_TEXTYPE_SHIFT
) & WINED3D_PSARGS_TEXTYPE_MASK
)
3498 case WINED3D_SHADER_TEX_2D
:
3499 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
3502 case WINED3D_SHADER_TEX_3D
:
3503 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_3D
;
3506 case WINED3D_SHADER_TEX_CUBE
:
3507 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
;
3513 static HRESULT
compute_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3514 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3516 return shader_init(shader
, device
, desc
, 0, WINED3D_SHADER_TYPE_COMPUTE
, parent
, parent_ops
);
3519 HRESULT CDECL
wined3d_shader_create_cs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3520 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3522 struct wined3d_shader
*object
;
3525 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3526 device
, desc
, parent
, parent_ops
, shader
);
3528 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
3529 return E_OUTOFMEMORY
;
3531 if (FAILED(hr
= compute_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3533 WARN("Failed to initialize compute shader, hr %#x.\n", hr
);
3534 HeapFree(GetProcessHeap(), 0, object
);
3538 TRACE("Created compute shader %p.\n", object
);
3544 HRESULT CDECL
wined3d_shader_create_ds(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3545 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3547 struct wined3d_shader
*object
;
3550 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3551 device
, desc
, parent
, parent_ops
, shader
);
3553 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
3554 return E_OUTOFMEMORY
;
3556 if (FAILED(hr
= domain_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3558 WARN("Failed to initialize domain shader, hr %#x.\n", hr
);
3559 HeapFree(GetProcessHeap(), 0, object
);
3563 TRACE("Created domain shader %p.\n", object
);
3569 HRESULT CDECL
wined3d_shader_create_gs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3570 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3572 struct wined3d_shader
*object
;
3575 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3576 device
, desc
, parent
, parent_ops
, shader
);
3578 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
3580 return E_OUTOFMEMORY
;
3582 if (FAILED(hr
= geometry_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3584 WARN("Failed to initialize geometry shader, hr %#x.\n", hr
);
3585 HeapFree(GetProcessHeap(), 0, object
);
3589 TRACE("Created geometry shader %p.\n", object
);
3595 HRESULT CDECL
wined3d_shader_create_hs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3596 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3598 struct wined3d_shader
*object
;
3601 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3602 device
, desc
, parent
, parent_ops
, shader
);
3604 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
3605 return E_OUTOFMEMORY
;
3607 if (FAILED(hr
= hull_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3609 WARN("Failed to initialize hull shader, hr %#x.\n", hr
);
3610 HeapFree(GetProcessHeap(), 0, object
);
3614 TRACE("Created hull shader %p.\n", object
);
3620 HRESULT CDECL
wined3d_shader_create_ps(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3621 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3623 struct wined3d_shader
*object
;
3626 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3627 device
, desc
, parent
, parent_ops
, shader
);
3629 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
3631 return E_OUTOFMEMORY
;
3633 if (FAILED(hr
= pixel_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3635 WARN("Failed to initialize pixel shader, hr %#x.\n", hr
);
3636 HeapFree(GetProcessHeap(), 0, object
);
3640 TRACE("Created pixel shader %p.\n", object
);
3646 HRESULT CDECL
wined3d_shader_create_vs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3647 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3649 struct wined3d_shader
*object
;
3652 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3653 device
, desc
, parent
, parent_ops
, shader
);
3655 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
3657 return E_OUTOFMEMORY
;
3659 if (FAILED(hr
= vertex_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3661 WARN("Failed to initialize vertex shader, hr %#x.\n", hr
);
3662 HeapFree(GetProcessHeap(), 0, object
);
3666 TRACE("Created vertex shader %p.\n", object
);