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
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
32 const struct wined3d_vec4 wined3d_srgb_const
[] =
34 /* pow, mul_high, sub_high, mul_low */
35 {0.41666f
, 1.055f
, 0.055f
, 12.92f
},
37 {0.0031308f
, 0.0f
, 0.0f
, 0.0f
},
40 static const char * const shader_opcode_names
[] =
42 /* WINED3DSIH_ABS */ "abs",
43 /* WINED3DSIH_ADD */ "add",
44 /* WINED3DSIH_AND */ "and",
45 /* WINED3DSIH_ATOMIC_AND */ "atomic_and",
46 /* WINED3DSIH_ATOMIC_CMP_STORE */ "atomic_cmp_store",
47 /* WINED3DSIH_ATOMIC_IADD */ "atomic_iadd",
48 /* WINED3DSIH_ATOMIC_IMAX */ "atomic_imax",
49 /* WINED3DSIH_ATOMIC_IMIN */ "atomic_imin",
50 /* WINED3DSIH_ATOMIC_OR */ "atomic_or",
51 /* WINED3DSIH_ATOMIC_UMAX */ "atomic_umax",
52 /* WINED3DSIH_ATOMIC_UMIN */ "atomic_umin",
53 /* WINED3DSIH_ATOMIC_XOR */ "atomic_xor",
54 /* WINED3DSIH_BEM */ "bem",
55 /* WINED3DSIH_BFI */ "bfi",
56 /* WINED3DSIH_BFREV */ "bfrev",
57 /* WINED3DSIH_BREAK */ "break",
58 /* WINED3DSIH_BREAKC */ "breakc",
59 /* WINED3DSIH_BREAKP */ "breakp",
60 /* WINED3DSIH_BUFINFO */ "bufinfo",
61 /* WINED3DSIH_CALL */ "call",
62 /* WINED3DSIH_CALLNZ */ "callnz",
63 /* WINED3DSIH_CASE */ "case",
64 /* WINED3DSIH_CMP */ "cmp",
65 /* WINED3DSIH_CND */ "cnd",
66 /* WINED3DSIH_CONTINUE */ "continue",
67 /* WINED3DSIH_CONTINUEP */ "continuec",
68 /* WINED3DSIH_COUNTBITS */ "countbits",
69 /* WINED3DSIH_CRS */ "crs",
70 /* WINED3DSIH_CUT */ "cut",
71 /* WINED3DSIH_CUT_STREAM */ "cut_stream",
72 /* WINED3DSIH_DCL */ "dcl",
73 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
74 /* WINED3DSIH_DCL_FUNCTION_BODY */ "dcl_function_body",
75 /* WINED3DSIH_DCL_FUNCTION_TABLE */ "dcl_function_table",
76 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ "dcl_globalFlags",
77 /* WINED3DSIH_DCL_GS_INSTANCES */ "dcl_gs_instances",
78 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ "dcl_hs_fork_phase_instance_count",
79 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ "dcl_hs_join_phase_instance_count",
80 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ "dcl_hs_max_tessfactor",
81 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ "dcl_immediateConstantBuffer",
82 /* WINED3DSIH_DCL_INDEX_RANGE */ "dcl_index_range",
83 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ "dcl_indexableTemp",
84 /* WINED3DSIH_DCL_INPUT */ "dcl_input",
85 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ "dcl_input_control_point_count",
86 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
87 /* WINED3DSIH_DCL_INPUT_PS */ "dcl_input_ps",
88 /* WINED3DSIH_DCL_INPUT_PS_SGV */ "dcl_input_ps_sgv",
89 /* WINED3DSIH_DCL_INPUT_PS_SIV */ "dcl_input_ps_siv",
90 /* WINED3DSIH_DCL_INPUT_SGV */ "dcl_input_sgv",
91 /* WINED3DSIH_DCL_INPUT_SIV */ "dcl_input_siv",
92 /* WINED3DSIH_DCL_INTERFACE */ "dcl_interface",
93 /* WINED3DSIH_DCL_OUTPUT */ "dcl_output",
94 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ "dcl_output_control_point_count",
95 /* WINED3DSIH_DCL_OUTPUT_SIV */ "dcl_output_siv",
96 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
97 /* WINED3DSIH_DCL_RESOURCE_RAW */ "dcl_resource_raw",
98 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ "dcl_resource_structured",
99 /* WINED3DSIH_DCL_SAMPLER */ "dcl_sampler",
100 /* WINED3DSIH_DCL_STREAM */ "dcl_stream",
101 /* WINED3DSIH_DCL_TEMPS */ "dcl_temps",
102 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ "dcl_tessellator_domain",
103 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ "dcl_tessellator_output_primitive",
104 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ "dcl_tessellator_partitioning",
105 /* WINED3DSIH_DCL_TGSM_RAW */ "dcl_tgsm_raw",
106 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ "dcl_tgsm_structured",
107 /* WINED3DSIH_DCL_THREAD_GROUP */ "dcl_thread_group",
108 /* WINED3DSIH_DCL_UAV_RAW */ "dcl_uav_raw",
109 /* WINED3DSIH_DCL_UAV_STRUCTURED */ "dcl_uav_structured",
110 /* WINED3DSIH_DCL_UAV_TYPED */ "dcl_uav_typed",
111 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
112 /* WINED3DSIH_DEF */ "def",
113 /* WINED3DSIH_DEFAULT */ "default",
114 /* WINED3DSIH_DEFB */ "defb",
115 /* WINED3DSIH_DEFI */ "defi",
116 /* WINED3DSIH_DIV */ "div",
117 /* WINED3DSIH_DP2 */ "dp2",
118 /* WINED3DSIH_DP2ADD */ "dp2add",
119 /* WINED3DSIH_DP3 */ "dp3",
120 /* WINED3DSIH_DP4 */ "dp4",
121 /* WINED3DSIH_DST */ "dst",
122 /* WINED3DSIH_DSX */ "dsx",
123 /* WINED3DSIH_DSX_COARSE */ "deriv_rtx_coarse",
124 /* WINED3DSIH_DSX_FINE */ "deriv_rtx_fine",
125 /* WINED3DSIH_DSY */ "dsy",
126 /* WINED3DSIH_DSY_COARSE */ "deriv_rty_coarse",
127 /* WINED3DSIH_DSY_FINE */ "deriv_rty_fine",
128 /* WINED3DSIH_ELSE */ "else",
129 /* WINED3DSIH_EMIT */ "emit",
130 /* WINED3DSIH_EMIT_STREAM */ "emit_stream",
131 /* WINED3DSIH_ENDIF */ "endif",
132 /* WINED3DSIH_ENDLOOP */ "endloop",
133 /* WINED3DSIH_ENDREP */ "endrep",
134 /* WINED3DSIH_ENDSWITCH */ "endswitch",
135 /* WINED3DSIH_EQ */ "eq",
136 /* WINED3DSIH_EVAL_CENTROID */ "eval_centroid",
137 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ "eval_sample_index",
138 /* WINED3DSIH_EXP */ "exp",
139 /* WINED3DSIH_EXPP */ "expp",
140 /* WINED3DSIH_F16TOF32 */ "f16tof32",
141 /* WINED3DSIH_F32TOF16 */ "f32tof16",
142 /* WINED3DSIH_FCALL */ "fcall",
143 /* WINED3DSIH_FIRSTBIT_HI */ "firstbit_hi",
144 /* WINED3DSIH_FIRSTBIT_LO */ "firstbit_lo",
145 /* WINED3DSIH_FIRSTBIT_SHI */ "firstbit_shi",
146 /* WINED3DSIH_FRC */ "frc",
147 /* WINED3DSIH_FTOI */ "ftoi",
148 /* WINED3DSIH_FTOU */ "ftou",
149 /* WINED3DSIH_GATHER4 */ "gather4",
150 /* WINED3DSIH_GATHER4_C */ "gather4_c",
151 /* WINED3DSIH_GATHER4_PO */ "gather4_po",
152 /* WINED3DSIH_GATHER4_PO_C */ "gather4_po_c",
153 /* WINED3DSIH_GE */ "ge",
154 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ "hs_control_point_phase",
155 /* WINED3DSIH_HS_DECLS */ "hs_decls",
156 /* WINED3DSIH_HS_FORK_PHASE */ "hs_fork_phase",
157 /* WINED3DSIH_HS_JOIN_PHASE */ "hs_join_phase",
158 /* WINED3DSIH_IADD */ "iadd",
159 /* WINED3DSIH_IBFE */ "ibfe",
160 /* WINED3DSIH_IEQ */ "ieq",
161 /* WINED3DSIH_IF */ "if",
162 /* WINED3DSIH_IFC */ "ifc",
163 /* WINED3DSIH_IGE */ "ige",
164 /* WINED3DSIH_ILT */ "ilt",
165 /* WINED3DSIH_IMAD */ "imad",
166 /* WINED3DSIH_IMAX */ "imax",
167 /* WINED3DSIH_IMIN */ "imin",
168 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ "imm_atomic_alloc",
169 /* WINED3DSIH_IMM_ATOMIC_AND */ "imm_atomic_and",
170 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ "imm_atomic_cmp_exch",
171 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ "imm_atomic_consume",
172 /* WINED3DSIH_IMM_ATOMIC_EXCH */ "imm_atomic_exch",
173 /* WINED3DSIH_IMM_ATOMIC_IADD */ "imm_atomic_iadd",
174 /* WINED3DSIH_IMM_ATOMIC_IMAX */ "imm_atomic_imax",
175 /* WINED3DSIH_IMM_ATOMIC_IMIN */ "imm_atomic_imin",
176 /* WINED3DSIH_IMM_ATOMIC_OR */ "imm_atomic_or",
177 /* WINED3DSIH_IMM_ATOMIC_UMAX */ "imm_atomic_umax",
178 /* WINED3DSIH_IMM_ATOMIC_UMIN */ "imm_atomic_umin",
179 /* WINED3DSIH_IMM_ATOMIC_XOR */ "imm_atomic_xor",
180 /* WINED3DSIH_IMUL */ "imul",
181 /* WINED3DSIH_INE */ "ine",
182 /* WINED3DSIH_INEG */ "ineg",
183 /* WINED3DSIH_ISHL */ "ishl",
184 /* WINED3DSIH_ISHR */ "ishr",
185 /* WINED3DSIH_ITOF */ "itof",
186 /* WINED3DSIH_LABEL */ "label",
187 /* WINED3DSIH_LD */ "ld",
188 /* WINED3DSIH_LD2DMS */ "ld2dms",
189 /* WINED3DSIH_LD_RAW */ "ld_raw",
190 /* WINED3DSIH_LD_STRUCTURED */ "ld_structured",
191 /* WINED3DSIH_LD_UAV_TYPED */ "ld_uav_typed",
192 /* WINED3DSIH_LIT */ "lit",
193 /* WINED3DSIH_LOD */ "lod",
194 /* WINED3DSIH_LOG */ "log",
195 /* WINED3DSIH_LOGP */ "logp",
196 /* WINED3DSIH_LOOP */ "loop",
197 /* WINED3DSIH_LRP */ "lrp",
198 /* WINED3DSIH_LT */ "lt",
199 /* WINED3DSIH_M3x2 */ "m3x2",
200 /* WINED3DSIH_M3x3 */ "m3x3",
201 /* WINED3DSIH_M3x4 */ "m3x4",
202 /* WINED3DSIH_M4x3 */ "m4x3",
203 /* WINED3DSIH_M4x4 */ "m4x4",
204 /* WINED3DSIH_MAD */ "mad",
205 /* WINED3DSIH_MAX */ "max",
206 /* WINED3DSIH_MIN */ "min",
207 /* WINED3DSIH_MOV */ "mov",
208 /* WINED3DSIH_MOVA */ "mova",
209 /* WINED3DSIH_MOVC */ "movc",
210 /* WINED3DSIH_MUL */ "mul",
211 /* WINED3DSIH_NE */ "ne",
212 /* WINED3DSIH_NOP */ "nop",
213 /* WINED3DSIH_NOT */ "not",
214 /* WINED3DSIH_NRM */ "nrm",
215 /* WINED3DSIH_OR */ "or",
216 /* WINED3DSIH_PHASE */ "phase",
217 /* WINED3DSIH_POW */ "pow",
218 /* WINED3DSIH_RCP */ "rcp",
219 /* WINED3DSIH_REP */ "rep",
220 /* WINED3DSIH_RESINFO */ "resinfo",
221 /* WINED3DSIH_RET */ "ret",
222 /* WINED3DSIH_RETP */ "retp",
223 /* WINED3DSIH_ROUND_NE */ "round_ne",
224 /* WINED3DSIH_ROUND_NI */ "round_ni",
225 /* WINED3DSIH_ROUND_PI */ "round_pi",
226 /* WINED3DSIH_ROUND_Z */ "round_z",
227 /* WINED3DSIH_RSQ */ "rsq",
228 /* WINED3DSIH_SAMPLE */ "sample",
229 /* WINED3DSIH_SAMPLE_B */ "sample_b",
230 /* WINED3DSIH_SAMPLE_C */ "sample_c",
231 /* WINED3DSIH_SAMPLE_C_LZ */ "sample_c_lz",
232 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
233 /* WINED3DSIH_SAMPLE_INFO */ "sample_info",
234 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
235 /* WINED3DSIH_SAMPLE_POS */ "sample_pos",
236 /* WINED3DSIH_SETP */ "setp",
237 /* WINED3DSIH_SGE */ "sge",
238 /* WINED3DSIH_SGN */ "sgn",
239 /* WINED3DSIH_SINCOS */ "sincos",
240 /* WINED3DSIH_SLT */ "slt",
241 /* WINED3DSIH_SQRT */ "sqrt",
242 /* WINED3DSIH_STORE_RAW */ "store_raw",
243 /* WINED3DSIH_STORE_STRUCTURED */ "store_structured",
244 /* WINED3DSIH_STORE_UAV_TYPED */ "store_uav_typed",
245 /* WINED3DSIH_SUB */ "sub",
246 /* WINED3DSIH_SWAPC */ "swapc",
247 /* WINED3DSIH_SWITCH */ "switch",
248 /* WINED3DSIH_SYNC */ "sync",
249 /* WINED3DSIH_TEX */ "texld",
250 /* WINED3DSIH_TEXBEM */ "texbem",
251 /* WINED3DSIH_TEXBEML */ "texbeml",
252 /* WINED3DSIH_TEXCOORD */ "texcrd",
253 /* WINED3DSIH_TEXDEPTH */ "texdepth",
254 /* WINED3DSIH_TEXDP3 */ "texdp3",
255 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
256 /* WINED3DSIH_TEXKILL */ "texkill",
257 /* WINED3DSIH_TEXLDD */ "texldd",
258 /* WINED3DSIH_TEXLDL */ "texldl",
259 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
260 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
261 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
262 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
263 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
264 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
265 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
266 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
267 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
268 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
269 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
270 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
271 /* WINED3DSIH_UBFE */ "ubfe",
272 /* WINED3DSIH_UDIV */ "udiv",
273 /* WINED3DSIH_UGE */ "uge",
274 /* WINED3DSIH_ULT */ "ult",
275 /* WINED3DSIH_UMAX */ "umax",
276 /* WINED3DSIH_UMIN */ "umin",
277 /* WINED3DSIH_UMUL */ "umul",
278 /* WINED3DSIH_USHR */ "ushr",
279 /* WINED3DSIH_UTOF */ "utof",
280 /* WINED3DSIH_XOR */ "xor",
283 static const char * const semantic_names
[] =
285 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
286 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
287 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
288 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
289 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
290 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
291 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
292 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
293 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
294 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
295 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
296 /* WINED3D_DECL_USAGE_FOG */ "FOG",
297 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
298 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
301 const char *debug_d3dshaderinstructionhandler(enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx
)
303 if (handler_idx
>= ARRAY_SIZE(shader_opcode_names
))
304 return wine_dbg_sprintf("UNRECOGNIZED(%#x)", handler_idx
);
306 return shader_opcode_names
[handler_idx
];
309 enum vkd3d_shader_visibility
vkd3d_shader_visibility_from_wined3d(enum wined3d_shader_type shader_type
)
313 case WINED3D_SHADER_TYPE_VERTEX
:
314 return VKD3D_SHADER_VISIBILITY_VERTEX
;
315 case WINED3D_SHADER_TYPE_HULL
:
316 return VKD3D_SHADER_VISIBILITY_HULL
;
317 case WINED3D_SHADER_TYPE_DOMAIN
:
318 return VKD3D_SHADER_VISIBILITY_DOMAIN
;
319 case WINED3D_SHADER_TYPE_GEOMETRY
:
320 return VKD3D_SHADER_VISIBILITY_GEOMETRY
;
321 case WINED3D_SHADER_TYPE_PIXEL
:
322 return VKD3D_SHADER_VISIBILITY_PIXEL
;
323 case WINED3D_SHADER_TYPE_COMPUTE
:
324 return VKD3D_SHADER_VISIBILITY_COMPUTE
;
326 ERR("Invalid shader type %s.\n", debug_shader_type(shader_type
));
327 return VKD3D_SHADER_VISIBILITY_ALL
;
331 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage
)
333 if (usage
>= ARRAY_SIZE(semantic_names
))
335 FIXME("Unrecognized usage %#x.\n", usage
);
336 return "UNRECOGNIZED";
339 return semantic_names
[usage
];
342 static enum wined3d_decl_usage
shader_usage_from_semantic_name(const char *name
)
346 for (i
= 0; i
< ARRAY_SIZE(semantic_names
); ++i
)
348 if (!strcmp(name
, semantic_names
[i
]))
355 static enum wined3d_sysval_semantic
shader_sysval_semantic_from_usage(enum wined3d_decl_usage usage
)
359 case WINED3D_DECL_USAGE_POSITION
:
360 return WINED3D_SV_POSITION
;
366 BOOL
shader_match_semantic(const char *semantic_name
, enum wined3d_decl_usage usage
)
368 return !strcmp(semantic_name
, shader_semantic_name_from_usage(usage
));
371 static void shader_signature_from_semantic(struct wined3d_shader_signature_element
*e
,
372 const struct wined3d_shader_semantic
*s
)
374 e
->semantic_name
= shader_semantic_name_from_usage(s
->usage
);
375 e
->semantic_idx
= s
->usage_idx
;
377 e
->sysval_semantic
= shader_sysval_semantic_from_usage(s
->usage
);
378 e
->component_type
= WINED3D_TYPE_FLOAT
;
379 e
->register_idx
= s
->reg
.reg
.idx
[0].offset
;
380 e
->mask
= s
->reg
.write_mask
;
383 static void shader_signature_from_usage(struct wined3d_shader_signature_element
*e
,
384 enum wined3d_decl_usage usage
, UINT usage_idx
, UINT reg_idx
, DWORD write_mask
)
386 e
->semantic_name
= shader_semantic_name_from_usage(usage
);
387 e
->semantic_idx
= usage_idx
;
389 e
->sysval_semantic
= shader_sysval_semantic_from_usage(usage
);
390 e
->component_type
= WINED3D_TYPE_FLOAT
;
391 e
->register_idx
= reg_idx
;
392 e
->mask
= write_mask
;
395 static const struct wined3d_shader_frontend
*shader_select_frontend(enum vkd3d_shader_source_type source_type
)
399 case VKD3D_SHADER_SOURCE_D3D_BYTECODE
:
400 return &sm1_shader_frontend
;
402 case VKD3D_SHADER_SOURCE_DXBC_TPF
:
403 return &sm4_shader_frontend
;
406 WARN("Invalid source type %#x specified.\n", source_type
);
411 void string_buffer_clear(struct wined3d_string_buffer
*buffer
)
413 buffer
->buffer
[0] = '\0';
414 buffer
->content_size
= 0;
417 BOOL
string_buffer_init(struct wined3d_string_buffer
*buffer
)
419 buffer
->buffer_size
= 32;
420 if (!(buffer
->buffer
= malloc(buffer
->buffer_size
)))
422 ERR("Failed to allocate shader buffer memory.\n");
426 string_buffer_clear(buffer
);
430 void string_buffer_free(struct wined3d_string_buffer
*buffer
)
432 free(buffer
->buffer
);
435 BOOL
string_buffer_resize(struct wined3d_string_buffer
*buffer
, int rc
)
438 unsigned int new_buffer_size
= buffer
->buffer_size
* 2;
440 while (rc
> 0 && (unsigned int)rc
>= new_buffer_size
- buffer
->content_size
)
441 new_buffer_size
*= 2;
442 if (!(new_buffer
= realloc(buffer
->buffer
, new_buffer_size
)))
444 ERR("Failed to grow buffer.\n");
445 buffer
->buffer
[buffer
->content_size
] = '\0';
448 buffer
->buffer
= new_buffer
;
449 buffer
->buffer_size
= new_buffer_size
;
453 int shader_vaddline(struct wined3d_string_buffer
*buffer
, const char *format
, va_list args
)
458 rem
= buffer
->buffer_size
- buffer
->content_size
;
459 rc
= vsnprintf(&buffer
->buffer
[buffer
->content_size
], rem
, format
, args
);
460 if (rc
< 0 /* C89 */ || (unsigned int)rc
>= rem
/* C99 */)
463 buffer
->content_size
+= rc
;
467 int shader_addline(struct wined3d_string_buffer
*buffer
, const char *format
, ...)
474 va_start(args
, format
);
475 ret
= shader_vaddline(buffer
, format
, args
);
479 if (!string_buffer_resize(buffer
, ret
))
484 struct wined3d_string_buffer
*string_buffer_get(struct wined3d_string_buffer_list
*list
)
486 struct wined3d_string_buffer
*buffer
;
488 if (list_empty(&list
->list
))
490 buffer
= malloc(sizeof(*buffer
));
491 if (!buffer
|| !string_buffer_init(buffer
))
493 ERR("Couldn't allocate buffer for temporary string.\n");
500 buffer
= LIST_ENTRY(list_head(&list
->list
), struct wined3d_string_buffer
, entry
);
501 list_remove(&buffer
->entry
);
503 string_buffer_clear(buffer
);
507 static int string_buffer_vsprintf(struct wined3d_string_buffer
*buffer
, const char *format
, va_list args
)
511 string_buffer_clear(buffer
);
512 return shader_vaddline(buffer
, format
, args
);
515 void string_buffer_sprintf(struct wined3d_string_buffer
*buffer
, const char *format
, ...)
522 va_start(args
, format
);
523 ret
= string_buffer_vsprintf(buffer
, format
, args
);
527 if (!string_buffer_resize(buffer
, ret
))
532 void string_buffer_release(struct wined3d_string_buffer_list
*list
, struct wined3d_string_buffer
*buffer
)
536 list_add_head(&list
->list
, &buffer
->entry
);
539 void string_buffer_list_init(struct wined3d_string_buffer_list
*list
)
541 list_init(&list
->list
);
544 void string_buffer_list_cleanup(struct wined3d_string_buffer_list
*list
)
546 struct wined3d_string_buffer
*buffer
, *buffer_next
;
548 LIST_FOR_EACH_ENTRY_SAFE(buffer
, buffer_next
, &list
->list
, struct wined3d_string_buffer
, entry
)
550 string_buffer_free(buffer
);
553 list_init(&list
->list
);
556 static void shader_delete_constant_list(struct list
*clist
)
558 struct wined3d_shader_lconst
*constant
, *constant_next
;
560 LIST_FOR_EACH_ENTRY_SAFE(constant
, constant_next
, clist
, struct wined3d_shader_lconst
, entry
)
565 static void shader_set_limits(struct wined3d_shader
*shader
)
567 static const struct limits_entry
569 unsigned int min_version
;
570 unsigned int max_version
;
571 struct wined3d_shader_limits limits
;
575 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
576 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
577 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
578 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
579 * even though they are capable of supporting much more (GL
580 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
581 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
583 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
584 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
585 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 0}},
590 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
591 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
595 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
596 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
600 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
601 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
602 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
607 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
608 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
609 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
610 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
611 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
612 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 10}},
613 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 32}},
618 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
619 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 0}},
621 const struct limits_entry
*limits_array
;
622 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
623 shader
->reg_maps
.shader_version
.minor
);
626 switch (shader
->reg_maps
.shader_version
.type
)
629 FIXME("Unexpected shader type %u found.\n", shader
->reg_maps
.shader_version
.type
);
631 case WINED3D_SHADER_TYPE_VERTEX
:
632 limits_array
= vs_limits
;
634 case WINED3D_SHADER_TYPE_HULL
:
635 limits_array
= hs_limits
;
637 case WINED3D_SHADER_TYPE_DOMAIN
:
638 limits_array
= ds_limits
;
640 case WINED3D_SHADER_TYPE_GEOMETRY
:
641 limits_array
= gs_limits
;
643 case WINED3D_SHADER_TYPE_PIXEL
:
644 limits_array
= ps_limits
;
646 case WINED3D_SHADER_TYPE_COMPUTE
:
647 limits_array
= cs_limits
;
651 while (limits_array
[i
].min_version
&& limits_array
[i
].min_version
<= shader_version
)
653 if (shader_version
<= limits_array
[i
].max_version
)
655 shader
->limits
= &limits_array
[i
].limits
;
662 FIXME("Unexpected shader version \"%u.%u\" (shader type %u).\n",
663 shader
->reg_maps
.shader_version
.major
,
664 shader
->reg_maps
.shader_version
.minor
,
665 shader
->reg_maps
.shader_version
.type
);
666 shader
->limits
= &limits_array
[max(0, i
- 1)].limits
;
670 static BOOL
shader_record_register_usage(struct wined3d_shader
*shader
, struct wined3d_shader_reg_maps
*reg_maps
,
671 const struct wined3d_shader_register
*reg
, enum wined3d_shader_type shader_type
, unsigned int constf_size
)
675 case WINED3DSPR_TEXTURE
: /* WINED3DSPR_ADDR */
676 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
677 reg_maps
->texcoord
|= 1u << reg
->idx
[0].offset
;
679 reg_maps
->address
|= 1u << reg
->idx
[0].offset
;
682 case WINED3DSPR_TEMP
:
683 reg_maps
->temporary
|= 1u << reg
->idx
[0].offset
;
686 case WINED3DSPR_INPUT
:
687 if (reg
->idx
[0].rel_addr
)
688 reg_maps
->input_rel_addressing
= 1;
689 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
691 /* If relative addressing is used, we must assume that all
692 * registers are used. Even if it is a construct like v3[aL],
693 * we can't assume that v0, v1 and v2 aren't read because aL
694 * can be negative. */
695 if (reg
->idx
[0].rel_addr
)
696 shader
->u
.ps
.input_reg_used
= ~0u;
698 shader
->u
.ps
.input_reg_used
|= 1u << reg
->idx
[0].offset
;
702 reg_maps
->input_registers
|= 1u << reg
->idx
[0].offset
;
706 case WINED3DSPR_RASTOUT
:
707 if (reg
->idx
[0].offset
== 1)
709 if (reg
->idx
[0].offset
== 2)
710 reg_maps
->point_size
= 1;
713 case WINED3DSPR_MISCTYPE
:
714 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
716 if (!reg
->idx
[0].offset
)
718 else if (reg
->idx
[0].offset
== 1)
719 reg_maps
->usesfacing
= 1;
723 case WINED3DSPR_CONST
:
724 if (reg
->idx
[0].rel_addr
)
726 if (reg
->idx
[0].offset
< reg_maps
->min_rel_offset
)
727 reg_maps
->min_rel_offset
= reg
->idx
[0].offset
;
728 if (reg
->idx
[0].offset
> reg_maps
->max_rel_offset
)
729 reg_maps
->max_rel_offset
= reg
->idx
[0].offset
;
730 reg_maps
->usesrelconstF
= TRUE
;
734 if (reg
->idx
[0].offset
>= min(shader
->limits
->constant_float
, constf_size
))
736 WARN("Shader using float constant %u which is not supported.\n", reg
->idx
[0].offset
);
741 wined3d_insert_bits(reg_maps
->constf
, reg
->idx
[0].offset
, 1, 0x1);
746 case WINED3DSPR_CONSTINT
:
747 if (reg
->idx
[0].offset
>= shader
->limits
->constant_int
)
749 WARN("Shader using integer constant %u which is not supported.\n", reg
->idx
[0].offset
);
754 reg_maps
->integer_constants
|= (1u << reg
->idx
[0].offset
);
758 case WINED3DSPR_CONSTBOOL
:
759 if (reg
->idx
[0].offset
>= shader
->limits
->constant_bool
)
761 WARN("Shader using bool constant %u which is not supported.\n", reg
->idx
[0].offset
);
766 reg_maps
->boolean_constants
|= (1u << reg
->idx
[0].offset
);
770 case WINED3DSPR_COLOROUT
:
771 reg_maps
->rt_mask
|= (1u << reg
->idx
[0].offset
);
774 case WINED3DSPR_OUTCONTROLPOINT
:
778 case WINED3DSPR_SAMPLEMASK
:
779 reg_maps
->sample_mask
= 1;
782 case WINED3DSPR_STENCILREF
:
783 reg_maps
->stencil_ref
= 1;
787 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
788 reg
->type
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
794 static void shader_record_sample(struct wined3d_shader_reg_maps
*reg_maps
,
795 unsigned int resource_idx
, unsigned int sampler_idx
, unsigned int bind_idx
)
797 struct wined3d_shader_sampler_map_entry
*entries
, *entry
;
798 struct wined3d_shader_sampler_map
*map
;
801 map
= ®_maps
->sampler_map
;
802 entries
= map
->entries
;
803 for (i
= 0; i
< map
->count
; ++i
)
805 if (entries
[i
].resource_idx
== resource_idx
&& entries
[i
].sampler_idx
== sampler_idx
)
811 if (!(entries
= calloc(4, sizeof(*entries
))))
813 ERR("Failed to allocate sampler map entries.\n");
817 map
->entries
= entries
;
819 else if (map
->count
== map
->size
)
821 size_t new_size
= map
->size
* 2;
823 if (sizeof(*entries
) * new_size
<= sizeof(*entries
) * map
->size
824 || !(entries
= realloc(entries
, sizeof(*entries
) * new_size
)))
826 ERR("Failed to resize sampler map entries.\n");
829 map
->size
= new_size
;
830 map
->entries
= entries
;
833 entry
= &entries
[map
->count
++];
834 entry
->resource_idx
= resource_idx
;
835 entry
->sampler_idx
= sampler_idx
;
836 entry
->bind_idx
= bind_idx
;
839 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr
, unsigned int param
)
843 case WINED3DSIH_M4x4
:
844 case WINED3DSIH_M3x4
:
845 return param
== 1 ? 3 : 0;
847 case WINED3DSIH_M4x3
:
848 case WINED3DSIH_M3x3
:
849 return param
== 1 ? 2 : 0;
851 case WINED3DSIH_M3x2
:
852 return param
== 1 ? 1 : 0;
859 static HRESULT
shader_reg_maps_add_tgsm(struct wined3d_shader_reg_maps
*reg_maps
,
860 unsigned int register_idx
, unsigned int size
, unsigned int stride
)
862 struct wined3d_shader_tgsm
*tgsm
;
864 if (register_idx
>= MAX_TGSM_REGISTERS
)
866 ERR("Invalid TGSM register index %u.\n", register_idx
);
869 if (reg_maps
->shader_version
.type
!= WINED3D_SHADER_TYPE_COMPUTE
)
871 FIXME("TGSM declarations are allowed only in compute shaders.\n");
875 if (!wined3d_array_reserve((void **)®_maps
->tgsm
, ®_maps
->tgsm_capacity
,
876 register_idx
+ 1, sizeof(*reg_maps
->tgsm
)))
877 return E_OUTOFMEMORY
;
879 reg_maps
->tgsm_count
= max(register_idx
+ 1, reg_maps
->tgsm_count
);
880 tgsm
= ®_maps
->tgsm
[register_idx
];
882 tgsm
->stride
= stride
;
886 static HRESULT
shader_record_shader_phase(struct wined3d_shader
*shader
,
887 struct wined3d_shader_phase
**current_phase
, const struct wined3d_shader_instruction
*ins
,
888 const DWORD
*current_instruction_ptr
, const DWORD
*previous_instruction_ptr
)
890 struct wined3d_shader_phase
*phase
;
892 if ((phase
= *current_phase
))
894 phase
->end
= previous_instruction_ptr
;
895 *current_phase
= NULL
;
898 if (shader
->reg_maps
.shader_version
.type
!= WINED3D_SHADER_TYPE_HULL
)
900 ERR("Unexpected shader type %s.\n", debug_shader_type(shader
->reg_maps
.shader_version
.type
));
904 switch (ins
->handler_idx
)
906 case WINED3DSIH_HS_CONTROL_POINT_PHASE
:
907 if (shader
->u
.hs
.phases
.control_point
)
909 FIXME("Multiple control point phases.\n");
910 free(shader
->u
.hs
.phases
.control_point
);
912 if (!(shader
->u
.hs
.phases
.control_point
= calloc(1, sizeof(*shader
->u
.hs
.phases
.control_point
))))
913 return E_OUTOFMEMORY
;
914 phase
= shader
->u
.hs
.phases
.control_point
;
916 case WINED3DSIH_HS_FORK_PHASE
:
917 if (!wined3d_array_reserve((void **)&shader
->u
.hs
.phases
.fork
,
918 &shader
->u
.hs
.phases
.fork_size
, shader
->u
.hs
.phases
.fork_count
+ 1,
919 sizeof(*shader
->u
.hs
.phases
.fork
)))
920 return E_OUTOFMEMORY
;
921 phase
= &shader
->u
.hs
.phases
.fork
[shader
->u
.hs
.phases
.fork_count
++];
923 case WINED3DSIH_HS_JOIN_PHASE
:
924 if (!wined3d_array_reserve((void **)&shader
->u
.hs
.phases
.join
,
925 &shader
->u
.hs
.phases
.join_size
, shader
->u
.hs
.phases
.join_count
+ 1,
926 sizeof(*shader
->u
.hs
.phases
.join
)))
927 return E_OUTOFMEMORY
;
928 phase
= &shader
->u
.hs
.phases
.join
[shader
->u
.hs
.phases
.join_count
++];
931 ERR("Unexpected opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
935 phase
->start
= current_instruction_ptr
;
936 *current_phase
= phase
;
941 static HRESULT
shader_calculate_clip_or_cull_distance_mask(
942 const struct wined3d_shader_signature_element
*e
, unsigned int *mask
)
944 /* Clip and cull distances are packed in 4 component registers. 0 and 1 are
945 * the only allowed semantic indices.
947 if (e
->semantic_idx
>= WINED3D_MAX_CLIP_DISTANCES
/ 4)
950 WARN("Invalid clip/cull distance index %u.\n", e
->semantic_idx
);
951 return WINED3DERR_INVALIDCALL
;
954 *mask
= (e
->mask
& WINED3DSP_WRITEMASK_ALL
) << (4 * e
->semantic_idx
);
958 static void wined3d_insert_interpolation_mode(uint32_t *packed_interpolation_mode
,
959 unsigned int register_idx
, enum wined3d_shader_interpolation_mode mode
)
961 if (mode
> WINED3DSIM_LINEAR_NOPERSPECTIVE_SAMPLE
)
962 FIXME("Unexpected interpolation mode %#x.\n", mode
);
964 wined3d_insert_bits(packed_interpolation_mode
,
965 register_idx
* WINED3D_PACKED_INTERPOLATION_BIT_COUNT
, WINED3D_PACKED_INTERPOLATION_BIT_COUNT
, mode
);
968 static HRESULT
shader_scan_output_signature(struct wined3d_shader
*shader
)
970 const struct wined3d_shader_signature
*output_signature
= &shader
->output_signature
;
971 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
975 for (i
= 0; i
< output_signature
->element_count
; ++i
)
977 const struct wined3d_shader_signature_element
*e
= &output_signature
->elements
[i
];
980 reg_maps
->output_registers
|= 1u << e
->register_idx
;
981 if (e
->sysval_semantic
== WINED3D_SV_CLIP_DISTANCE
)
983 if (FAILED(hr
= shader_calculate_clip_or_cull_distance_mask(e
, &mask
)))
985 reg_maps
->clip_distance_mask
|= mask
;
987 else if (e
->sysval_semantic
== WINED3D_SV_CULL_DISTANCE
)
989 if (FAILED(hr
= shader_calculate_clip_or_cull_distance_mask(e
, &mask
)))
991 reg_maps
->cull_distance_mask
|= mask
;
993 else if (e
->sysval_semantic
== WINED3D_SV_VIEWPORT_ARRAY_INDEX
)
995 reg_maps
->viewport_array
= 1;
1002 /* Note that this does not count the loop register as an address register. */
1003 static HRESULT
shader_get_registers_used(struct wined3d_shader
*shader
, DWORD constf_size
)
1005 struct wined3d_shader_signature_element input_signature_elements
[max(MAX_ATTRIBS
, MAX_REG_INPUT
)];
1006 struct wined3d_shader_signature_element output_signature_elements
[MAX_REG_OUTPUT
];
1007 struct wined3d_shader_signature
*output_signature
= &shader
->output_signature
;
1008 struct wined3d_shader_signature
*input_signature
= &shader
->input_signature
;
1009 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1010 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
1011 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
1012 struct wined3d_shader_version shader_version
;
1013 struct wined3d_shader_phase
*phase
= NULL
;
1014 const DWORD
*ptr
, *prev_ins
, *current_ins
;
1015 void *fe_data
= shader
->frontend_data
;
1019 memset(reg_maps
, 0, sizeof(*reg_maps
));
1020 memset(input_signature_elements
, 0, sizeof(input_signature_elements
));
1021 memset(output_signature_elements
, 0, sizeof(output_signature_elements
));
1022 reg_maps
->min_rel_offset
= ~0U;
1023 list_init(®_maps
->indexable_temps
);
1025 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1026 prev_ins
= current_ins
= ptr
;
1027 reg_maps
->shader_version
= shader_version
;
1029 shader_set_limits(shader
);
1031 if (!(reg_maps
->constf
= calloc(((min(shader
->limits
->constant_float
, constf_size
) + 31) / 32),
1032 sizeof(*reg_maps
->constf
))))
1034 ERR("Failed to allocate constant map memory.\n");
1035 return E_OUTOFMEMORY
;
1038 while (!fe
->shader_is_end(fe_data
, &ptr
))
1040 struct wined3d_shader_instruction ins
;
1044 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1046 /* Unhandled opcode, and its parameters. */
1047 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1049 WARN("Encountered unrecognised or invalid instruction.\n");
1050 return WINED3DERR_INVALIDCALL
;
1053 /* Handle declarations. */
1054 if (ins
.handler_idx
== WINED3DSIH_DCL
1055 || ins
.handler_idx
== WINED3DSIH_DCL_UAV_TYPED
)
1057 struct wined3d_shader_semantic
*semantic
= &ins
.declaration
.semantic
;
1058 unsigned int reg_idx
= semantic
->reg
.reg
.idx
[0].offset
;
1060 switch (semantic
->reg
.reg
.type
)
1062 /* Mark input registers used. */
1063 case WINED3DSPR_INPUT
:
1064 if (reg_idx
>= MAX_REG_INPUT
)
1066 ERR("Invalid input register index %u.\n", reg_idx
);
1069 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
&& shader_version
.major
== 3
1070 && semantic
->usage
== WINED3D_DECL_USAGE_POSITION
&& !semantic
->usage_idx
)
1071 return WINED3DERR_INVALIDCALL
;
1072 reg_maps
->input_registers
|= 1u << reg_idx
;
1073 shader_signature_from_semantic(&input_signature_elements
[reg_idx
], semantic
);
1076 /* Vertex shader: mark 3.0 output registers used, save token. */
1077 case WINED3DSPR_OUTPUT
:
1078 if (reg_idx
>= MAX_REG_OUTPUT
)
1080 ERR("Invalid output register index %u.\n", reg_idx
);
1083 reg_maps
->output_registers
|= 1u << reg_idx
;
1084 shader_signature_from_semantic(&output_signature_elements
[reg_idx
], semantic
);
1085 if (semantic
->usage
== WINED3D_DECL_USAGE_FOG
)
1087 if (semantic
->usage
== WINED3D_DECL_USAGE_PSIZE
)
1088 reg_maps
->point_size
= 1;
1091 case WINED3DSPR_SAMPLER
:
1092 shader_record_sample(reg_maps
, reg_idx
, reg_idx
, reg_idx
);
1093 case WINED3DSPR_RESOURCE
:
1094 if (reg_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
1096 ERR("Invalid resource index %u.\n", reg_idx
);
1099 reg_maps
->resource_info
[reg_idx
].type
= semantic
->resource_type
;
1100 if (semantic
->resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
&& semantic
->sample_count
== 1)
1101 reg_maps
->resource_info
[reg_idx
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
1102 if (semantic
->resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY
&& semantic
->sample_count
== 1)
1103 reg_maps
->resource_info
[reg_idx
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
;
1104 reg_maps
->resource_info
[reg_idx
].data_type
= semantic
->resource_data_type
;
1105 wined3d_bitmap_set(reg_maps
->resource_map
, reg_idx
);
1108 case WINED3DSPR_UAV
:
1109 if (reg_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
1111 ERR("Invalid UAV resource index %u.\n", reg_idx
);
1114 reg_maps
->uav_resource_info
[reg_idx
].type
= semantic
->resource_type
;
1115 reg_maps
->uav_resource_info
[reg_idx
].data_type
= semantic
->resource_data_type
;
1117 FIXME("Ignoring typed UAV flags %#x.\n", ins
.flags
);
1121 TRACE("Not recording DCL register type %#x.\n", semantic
->reg
.reg
.type
);
1125 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
1127 struct wined3d_shader_register
*reg
= &ins
.declaration
.src
.reg
;
1128 if (reg
->idx
[0].offset
>= WINED3D_MAX_CBS
)
1130 ERR("Invalid CB index %u.\n", reg
->idx
[0].offset
);
1134 reg_maps
->cb_sizes
[reg
->idx
[0].offset
] = reg
->idx
[1].offset
;
1135 wined3d_bitmap_set(®_maps
->cb_map
, reg
->idx
[0].offset
);
1138 else if (ins
.handler_idx
== WINED3DSIH_DCL_GLOBAL_FLAGS
)
1140 if (ins
.flags
& WINED3DSGF_FORCE_EARLY_DEPTH_STENCIL
)
1142 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1143 shader
->u
.ps
.force_early_depth_stencil
= TRUE
;
1145 FIXME("Invalid instruction %#x for shader type %#x.\n",
1146 ins
.handler_idx
, shader_version
.type
);
1150 WARN("Ignoring global flags %#x.\n", ins
.flags
);
1153 else if (ins
.handler_idx
== WINED3DSIH_DCL_GS_INSTANCES
)
1155 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1156 shader
->u
.gs
.instance_count
= ins
.declaration
.count
;
1158 FIXME("Invalid instruction %#x for shader type %#x.\n",
1159 ins
.handler_idx
, shader_version
.type
);
1161 else if (ins
.handler_idx
== WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT
1162 || ins
.handler_idx
== WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT
)
1165 phase
->instance_count
= ins
.declaration
.count
;
1167 FIXME("Instruction %s outside of shader phase.\n",
1168 debug_d3dshaderinstructionhandler(ins
.handler_idx
));
1170 else if (ins
.handler_idx
== WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER
)
1173 FIXME("Multiple immediate constant buffers.\n");
1174 reg_maps
->icb
= ins
.declaration
.icb
;
1176 else if (ins
.handler_idx
== WINED3DSIH_DCL_INDEXABLE_TEMP
)
1180 FIXME("Indexable temporary registers not supported.\n");
1184 struct wined3d_shader_indexable_temp
*reg
;
1186 if (!(reg
= malloc(sizeof(*reg
))))
1187 return E_OUTOFMEMORY
;
1189 *reg
= ins
.declaration
.indexable_temp
;
1190 list_add_tail(®_maps
->indexable_temps
, ®
->entry
);
1193 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
)
1195 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1196 shader
->u
.gs
.input_type
= ins
.declaration
.primitive_type
.type
;
1198 FIXME("Invalid instruction %#x for shader type %#x.\n",
1199 ins
.handler_idx
, shader_version
.type
);
1201 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PS
)
1203 unsigned int reg_idx
= ins
.declaration
.dst
.reg
.idx
[0].offset
;
1204 if (reg_idx
>= MAX_REG_INPUT
)
1206 ERR("Invalid register index %u.\n", reg_idx
);
1209 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1210 wined3d_insert_interpolation_mode(shader
->u
.ps
.interpolation_mode
, reg_idx
, ins
.flags
);
1212 FIXME("Invalid instruction %#x for shader type %#x.\n",
1213 ins
.handler_idx
, shader_version
.type
);
1215 else if (ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT
)
1217 if (ins
.declaration
.dst
.reg
.type
== WINED3DSPR_DEPTHOUT
1218 || ins
.declaration
.dst
.reg
.type
== WINED3DSPR_DEPTHOUTGE
1219 || ins
.declaration
.dst
.reg
.type
== WINED3DSPR_DEPTHOUTLE
)
1221 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1222 shader
->u
.ps
.depth_output
= ins
.declaration
.dst
.reg
.type
;
1224 FIXME("Invalid instruction %#x for shader type %#x.\n",
1225 ins
.handler_idx
, shader_version
.type
);
1228 else if (ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT
)
1230 if (shader_version
.type
== WINED3D_SHADER_TYPE_HULL
)
1231 shader
->u
.hs
.output_vertex_count
= ins
.declaration
.count
;
1233 FIXME("Invalid instruction %#x for shader type %#x.\n", ins
.handler_idx
, shader_version
.type
);
1235 else if (ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
1237 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1238 shader
->u
.gs
.output_type
= ins
.declaration
.primitive_type
.type
;
1240 FIXME("Invalid instruction %#x for shader type %#x.\n",
1241 ins
.handler_idx
, shader_version
.type
);
1243 else if (ins
.handler_idx
== WINED3DSIH_DCL_RESOURCE_RAW
)
1245 unsigned int reg_idx
= ins
.declaration
.dst
.reg
.idx
[0].offset
;
1246 if (reg_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
1248 ERR("Invalid resource index %u.\n", reg_idx
);
1251 reg_maps
->resource_info
[reg_idx
].type
= WINED3D_SHADER_RESOURCE_BUFFER
;
1252 reg_maps
->resource_info
[reg_idx
].data_type
= WINED3D_DATA_UINT
;
1253 reg_maps
->resource_info
[reg_idx
].flags
= WINED3D_VIEW_BUFFER_RAW
;
1254 wined3d_bitmap_set(reg_maps
->resource_map
, reg_idx
);
1256 else if (ins
.handler_idx
== WINED3DSIH_DCL_RESOURCE_STRUCTURED
)
1258 unsigned int reg_idx
= ins
.declaration
.structured_resource
.reg
.reg
.idx
[0].offset
;
1259 if (reg_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
1261 ERR("Invalid resource index %u.\n", reg_idx
);
1264 reg_maps
->resource_info
[reg_idx
].type
= WINED3D_SHADER_RESOURCE_BUFFER
;
1265 reg_maps
->resource_info
[reg_idx
].data_type
= WINED3D_DATA_UINT
;
1266 reg_maps
->resource_info
[reg_idx
].flags
= 0;
1267 reg_maps
->resource_info
[reg_idx
].stride
= ins
.declaration
.structured_resource
.byte_stride
/ 4;
1268 wined3d_bitmap_set(reg_maps
->resource_map
, reg_idx
);
1270 else if (ins
.handler_idx
== WINED3DSIH_DCL_SAMPLER
)
1272 if (ins
.flags
& WINED3DSI_SAMPLER_COMPARISON_MODE
)
1273 reg_maps
->sampler_comparison_mode
|= (1u << ins
.declaration
.dst
.reg
.idx
[0].offset
);
1275 else if (ins
.handler_idx
== WINED3DSIH_DCL_TEMPS
)
1278 phase
->temporary_count
= ins
.declaration
.count
;
1280 reg_maps
->temporary_count
= ins
.declaration
.count
;
1282 else if (ins
.handler_idx
== WINED3DSIH_DCL_TESSELLATOR_DOMAIN
)
1284 if (shader_version
.type
== WINED3D_SHADER_TYPE_DOMAIN
)
1285 shader
->u
.ds
.tessellator_domain
= ins
.declaration
.tessellator_domain
;
1286 else if (shader_version
.type
!= WINED3D_SHADER_TYPE_HULL
)
1287 FIXME("Invalid instruction %#x for shader type %#x.\n", ins
.handler_idx
, shader_version
.type
);
1289 else if (ins
.handler_idx
== WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE
)
1291 if (shader_version
.type
== WINED3D_SHADER_TYPE_HULL
)
1292 shader
->u
.hs
.tessellator_output_primitive
= ins
.declaration
.tessellator_output_primitive
;
1294 FIXME("Invalid instruction %#x for shader type %#x.\n", ins
.handler_idx
, shader_version
.type
);
1296 else if (ins
.handler_idx
== WINED3DSIH_DCL_TESSELLATOR_PARTITIONING
)
1298 if (shader_version
.type
== WINED3D_SHADER_TYPE_HULL
)
1299 shader
->u
.hs
.tessellator_partitioning
= ins
.declaration
.tessellator_partitioning
;
1301 FIXME("Invalid instruction %#x for shader type %#x.\n", ins
.handler_idx
, shader_version
.type
);
1303 else if (ins
.handler_idx
== WINED3DSIH_DCL_TGSM_RAW
)
1305 if (FAILED(hr
= shader_reg_maps_add_tgsm(reg_maps
, ins
.declaration
.tgsm_raw
.reg
.reg
.idx
[0].offset
,
1306 ins
.declaration
.tgsm_raw
.byte_count
/ 4, 0)))
1309 else if (ins
.handler_idx
== WINED3DSIH_DCL_TGSM_STRUCTURED
)
1311 unsigned int stride
= ins
.declaration
.tgsm_structured
.byte_stride
/ 4;
1312 unsigned int size
= stride
* ins
.declaration
.tgsm_structured
.structure_count
;
1313 if (FAILED(hr
= shader_reg_maps_add_tgsm(reg_maps
,
1314 ins
.declaration
.tgsm_structured
.reg
.reg
.idx
[0].offset
, size
, stride
)))
1317 else if (ins
.handler_idx
== WINED3DSIH_DCL_THREAD_GROUP
)
1319 if (shader_version
.type
== WINED3D_SHADER_TYPE_COMPUTE
)
1321 shader
->u
.cs
.thread_group_size
= ins
.declaration
.thread_group_size
;
1325 FIXME("Invalid instruction %#x for shader type %#x.\n",
1326 ins
.handler_idx
, shader_version
.type
);
1329 else if (ins
.handler_idx
== WINED3DSIH_DCL_UAV_RAW
)
1331 unsigned int reg_idx
= ins
.declaration
.dst
.reg
.idx
[0].offset
;
1332 if (reg_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
1334 ERR("Invalid UAV resource index %u.\n", reg_idx
);
1338 FIXME("Ignoring raw UAV flags %#x.\n", ins
.flags
);
1339 reg_maps
->uav_resource_info
[reg_idx
].type
= WINED3D_SHADER_RESOURCE_BUFFER
;
1340 reg_maps
->uav_resource_info
[reg_idx
].data_type
= WINED3D_DATA_UINT
;
1341 reg_maps
->uav_resource_info
[reg_idx
].flags
= WINED3D_VIEW_BUFFER_RAW
;
1343 else if (ins
.handler_idx
== WINED3DSIH_DCL_UAV_STRUCTURED
)
1345 unsigned int reg_idx
= ins
.declaration
.structured_resource
.reg
.reg
.idx
[0].offset
;
1346 if (reg_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
1348 ERR("Invalid UAV resource index %u.\n", reg_idx
);
1352 FIXME("Ignoring structured UAV flags %#x.\n", ins
.flags
);
1353 reg_maps
->uav_resource_info
[reg_idx
].type
= WINED3D_SHADER_RESOURCE_BUFFER
;
1354 reg_maps
->uav_resource_info
[reg_idx
].data_type
= WINED3D_DATA_UINT
;
1355 reg_maps
->uav_resource_info
[reg_idx
].flags
= 0;
1356 reg_maps
->uav_resource_info
[reg_idx
].stride
= ins
.declaration
.structured_resource
.byte_stride
/ 4;
1358 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
1360 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1361 shader
->u
.gs
.vertices_out
= ins
.declaration
.count
;
1363 FIXME("Invalid instruction %#x for shader type %#x.\n",
1364 ins
.handler_idx
, shader_version
.type
);
1366 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
1368 struct wined3d_shader_lconst
*lconst
;
1371 if (!(lconst
= malloc(sizeof(*lconst
))))
1372 return E_OUTOFMEMORY
;
1374 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
1375 memcpy(lconst
->value
, ins
.src
[0].reg
.u
.immconst_data
, 4 * sizeof(DWORD
));
1376 value
= (float *)lconst
->value
;
1378 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
1379 if (shader_version
.major
== 1 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1381 if (value
[0] < -1.0f
) value
[0] = -1.0f
;
1382 else if (value
[0] > 1.0f
) value
[0] = 1.0f
;
1383 if (value
[1] < -1.0f
) value
[1] = -1.0f
;
1384 else if (value
[1] > 1.0f
) value
[1] = 1.0f
;
1385 if (value
[2] < -1.0f
) value
[2] = -1.0f
;
1386 else if (value
[2] > 1.0f
) value
[2] = 1.0f
;
1387 if (value
[3] < -1.0f
) value
[3] = -1.0f
;
1388 else if (value
[3] > 1.0f
) value
[3] = 1.0f
;
1391 list_add_head(&shader
->constantsF
, &lconst
->entry
);
1393 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
1394 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
1396 shader
->lconst_inf_or_nan
= TRUE
;
1399 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
1401 struct wined3d_shader_lconst
*lconst
;
1403 if (!(lconst
= malloc(sizeof(*lconst
))))
1404 return E_OUTOFMEMORY
;
1406 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
1407 memcpy(lconst
->value
, ins
.src
[0].reg
.u
.immconst_data
, 4 * sizeof(DWORD
));
1409 list_add_head(&shader
->constantsI
, &lconst
->entry
);
1410 reg_maps
->local_int_consts
|= (1u << lconst
->idx
);
1412 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
1414 struct wined3d_shader_lconst
*lconst
;
1416 if (!(lconst
= malloc(sizeof(*lconst
))))
1417 return E_OUTOFMEMORY
;
1419 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
1420 memcpy(lconst
->value
, ins
.src
[0].reg
.u
.immconst_data
, sizeof(DWORD
));
1422 list_add_head(&shader
->constantsB
, &lconst
->entry
);
1423 reg_maps
->local_bool_consts
|= (1u << lconst
->idx
);
1425 /* Handle shader phases. */
1426 else if (ins
.handler_idx
== WINED3DSIH_HS_CONTROL_POINT_PHASE
1427 || ins
.handler_idx
== WINED3DSIH_HS_FORK_PHASE
1428 || ins
.handler_idx
== WINED3DSIH_HS_JOIN_PHASE
)
1430 if (FAILED(hr
= shader_record_shader_phase(shader
, &phase
, &ins
, current_ins
, prev_ins
)))
1433 /* For subroutine prototypes. */
1434 else if (ins
.handler_idx
== WINED3DSIH_LABEL
)
1436 reg_maps
->labels
|= 1u << ins
.src
[0].reg
.idx
[0].offset
;
1438 /* Set texture, address, temporary registers. */
1441 BOOL color0_mov
= FALSE
;
1444 /* This will loop over all the registers and try to
1445 * make a bitmask of the ones we're interested in.
1447 * Relative addressing tokens are ignored, but that's
1448 * okay, since we'll catch any address registers when
1449 * they are initialized (required by spec). */
1450 for (i
= 0; i
< ins
.dst_count
; ++i
)
1452 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.dst
[i
].reg
,
1453 shader_version
.type
, constf_size
))
1454 return WINED3DERR_INVALIDCALL
;
1456 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
)
1458 UINT idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
1460 switch (ins
.dst
[i
].reg
.type
)
1462 case WINED3DSPR_RASTOUT
:
1463 if (shader_version
.major
>= 3)
1468 reg_maps
->output_registers
|= 1u << 10;
1469 shader_signature_from_usage(&output_signature_elements
[10],
1470 WINED3D_DECL_USAGE_POSITION
, 0, 10, WINED3DSP_WRITEMASK_ALL
);
1474 reg_maps
->output_registers
|= 1u << 11;
1475 shader_signature_from_usage(&output_signature_elements
[11],
1476 WINED3D_DECL_USAGE_FOG
, 0, 11, WINED3DSP_WRITEMASK_0
);
1480 reg_maps
->output_registers
|= 1u << 11;
1481 shader_signature_from_usage(&output_signature_elements
[11],
1482 WINED3D_DECL_USAGE_PSIZE
, 0, 11, WINED3DSP_WRITEMASK_1
);
1487 case WINED3DSPR_ATTROUT
:
1488 if (shader_version
.major
>= 3)
1493 if (reg_maps
->output_registers
& (1u << idx
))
1495 output_signature_elements
[idx
].mask
|= ins
.dst
[i
].write_mask
;
1499 reg_maps
->output_registers
|= 1u << idx
;
1500 shader_signature_from_usage(&output_signature_elements
[idx
],
1501 WINED3D_DECL_USAGE_COLOR
, idx
- 8, idx
, ins
.dst
[i
].write_mask
);
1506 case WINED3DSPR_TEXCRDOUT
: /* WINED3DSPR_OUTPUT */
1507 if (shader_version
.major
>= 3)
1509 if (idx
>= ARRAY_SIZE(reg_maps
->u
.output_registers_mask
))
1511 WARN("Invalid output register index %u.\n", idx
);
1514 reg_maps
->u
.output_registers_mask
[idx
] |= ins
.dst
[i
].write_mask
;
1517 if (idx
>= ARRAY_SIZE(reg_maps
->u
.texcoord_mask
))
1519 WARN("Invalid texcoord index %u.\n", idx
);
1522 reg_maps
->u
.texcoord_mask
[idx
] |= ins
.dst
[i
].write_mask
;
1523 if (reg_maps
->output_registers
& (1u << idx
))
1525 output_signature_elements
[idx
].mask
|= ins
.dst
[i
].write_mask
;
1529 reg_maps
->output_registers
|= 1u << idx
;
1530 shader_signature_from_usage(&output_signature_elements
[idx
],
1531 WINED3D_DECL_USAGE_TEXCOORD
, idx
, idx
, ins
.dst
[i
].write_mask
);
1540 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1542 if (ins
.dst
[i
].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
.dst
[i
].reg
.idx
[0].offset
)
1544 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
1545 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
1546 * the mov and perform the sRGB write correction from the source register.
1548 * However, if the mov is only partial, we can't do this, and if the write
1549 * comes from an instruction other than MOV it is hard to do as well. If
1550 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
1551 shader
->u
.ps
.color0_mov
= FALSE
;
1552 if (ins
.handler_idx
== WINED3DSIH_MOV
1553 && ins
.dst
[i
].write_mask
== WINED3DSP_WRITEMASK_ALL
)
1555 /* Used later when the source register is read. */
1559 /* Also drop the MOV marker if the source register is overwritten prior to the shader
1562 else if (ins
.dst
[i
].reg
.type
== WINED3DSPR_TEMP
1563 && ins
.dst
[i
].reg
.idx
[0].offset
== shader
->u
.ps
.color0_reg
)
1565 shader
->u
.ps
.color0_mov
= FALSE
;
1569 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
1570 if (shader_version
.major
== 1
1571 && (ins
.handler_idx
== WINED3DSIH_TEX
1572 || ins
.handler_idx
== WINED3DSIH_TEXBEM
1573 || ins
.handler_idx
== WINED3DSIH_TEXBEML
1574 || ins
.handler_idx
== WINED3DSIH_TEXDP3TEX
1575 || ins
.handler_idx
== WINED3DSIH_TEXM3x2TEX
1576 || ins
.handler_idx
== WINED3DSIH_TEXM3x3SPEC
1577 || ins
.handler_idx
== WINED3DSIH_TEXM3x3TEX
1578 || ins
.handler_idx
== WINED3DSIH_TEXM3x3VSPEC
1579 || ins
.handler_idx
== WINED3DSIH_TEXREG2AR
1580 || ins
.handler_idx
== WINED3DSIH_TEXREG2GB
1581 || ins
.handler_idx
== WINED3DSIH_TEXREG2RGB
))
1583 unsigned int reg_idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
1585 if (reg_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
1587 WARN("Invalid 1.x sampler index %u.\n", reg_idx
);
1591 TRACE("Setting fake 2D resource for 1.x pixelshader.\n");
1592 reg_maps
->resource_info
[reg_idx
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
1593 reg_maps
->resource_info
[reg_idx
].data_type
= WINED3D_DATA_FLOAT
;
1594 shader_record_sample(reg_maps
, reg_idx
, reg_idx
, reg_idx
);
1595 wined3d_bitmap_set(reg_maps
->resource_map
, reg_idx
);
1597 /* texbem is only valid with < 1.4 pixel shaders */
1598 if (ins
.handler_idx
== WINED3DSIH_TEXBEM
1599 || ins
.handler_idx
== WINED3DSIH_TEXBEML
)
1601 reg_maps
->bumpmat
|= 1u << reg_idx
;
1602 if (ins
.handler_idx
== WINED3DSIH_TEXBEML
)
1604 reg_maps
->luminanceparams
|= 1u << reg_idx
;
1608 else if (ins
.handler_idx
== WINED3DSIH_BEM
)
1610 reg_maps
->bumpmat
|= 1u << ins
.dst
[i
].reg
.idx
[0].offset
;
1614 if (ins
.handler_idx
== WINED3DSIH_IMM_ATOMIC_ALLOC
|| ins
.handler_idx
== WINED3DSIH_IMM_ATOMIC_CONSUME
)
1616 unsigned int reg_idx
= ins
.src
[0].reg
.idx
[0].offset
;
1617 if (reg_idx
>= MAX_UNORDERED_ACCESS_VIEWS
)
1619 ERR("Invalid UAV index %u.\n", reg_idx
);
1622 reg_maps
->uav_counter_mask
|= (1u << reg_idx
);
1624 else if ((WINED3DSIH_ATOMIC_AND
<= ins
.handler_idx
&& ins
.handler_idx
<= WINED3DSIH_ATOMIC_XOR
)
1625 || (WINED3DSIH_IMM_ATOMIC_AND
<= ins
.handler_idx
&& ins
.handler_idx
<= WINED3DSIH_IMM_ATOMIC_XOR
)
1626 || (ins
.handler_idx
== WINED3DSIH_BUFINFO
&& ins
.src
[0].reg
.type
== WINED3DSPR_UAV
)
1627 || ins
.handler_idx
== WINED3DSIH_LD_UAV_TYPED
1628 || (ins
.handler_idx
== WINED3DSIH_LD_RAW
&& ins
.src
[1].reg
.type
== WINED3DSPR_UAV
)
1629 || (ins
.handler_idx
== WINED3DSIH_LD_STRUCTURED
&& ins
.src
[2].reg
.type
== WINED3DSPR_UAV
))
1631 const struct wined3d_shader_register
*reg
;
1633 if (ins
.handler_idx
== WINED3DSIH_LD_UAV_TYPED
|| ins
.handler_idx
== WINED3DSIH_LD_RAW
)
1634 reg
= &ins
.src
[1].reg
;
1635 else if (ins
.handler_idx
== WINED3DSIH_LD_STRUCTURED
)
1636 reg
= &ins
.src
[2].reg
;
1637 else if (WINED3DSIH_ATOMIC_AND
<= ins
.handler_idx
&& ins
.handler_idx
<= WINED3DSIH_ATOMIC_XOR
)
1638 reg
= &ins
.dst
[0].reg
;
1639 else if (ins
.handler_idx
== WINED3DSIH_BUFINFO
)
1640 reg
= &ins
.src
[0].reg
;
1642 reg
= &ins
.dst
[1].reg
;
1644 if (reg
->type
== WINED3DSPR_UAV
)
1646 if (reg
->idx
[0].offset
>= MAX_UNORDERED_ACCESS_VIEWS
)
1648 ERR("Invalid UAV index %u.\n", reg
->idx
[0].offset
);
1651 reg_maps
->uav_read_mask
|= (1u << reg
->idx
[0].offset
);
1654 else if (ins
.handler_idx
== WINED3DSIH_NRM
)
1656 reg_maps
->usesnrm
= 1;
1658 else if (ins
.handler_idx
== WINED3DSIH_DSY
1659 || ins
.handler_idx
== WINED3DSIH_DSY_COARSE
1660 || ins
.handler_idx
== WINED3DSIH_DSY_FINE
)
1662 reg_maps
->usesdsy
= 1;
1664 else if (ins
.handler_idx
== WINED3DSIH_DSX
1665 || ins
.handler_idx
== WINED3DSIH_DSX_COARSE
1666 || ins
.handler_idx
== WINED3DSIH_DSX_FINE
)
1668 reg_maps
->usesdsx
= 1;
1670 else if (ins
.handler_idx
== WINED3DSIH_TEXLDD
) reg_maps
->usestexldd
= 1;
1671 else if (ins
.handler_idx
== WINED3DSIH_TEXLDL
) reg_maps
->usestexldl
= 1;
1672 else if (ins
.handler_idx
== WINED3DSIH_MOVA
) reg_maps
->usesmova
= 1;
1673 else if (ins
.handler_idx
== WINED3DSIH_IFC
) reg_maps
->usesifc
= 1;
1674 else if (ins
.handler_idx
== WINED3DSIH_CALL
) reg_maps
->usescall
= 1;
1675 else if (ins
.handler_idx
== WINED3DSIH_POW
) reg_maps
->usespow
= 1;
1676 else if (ins
.handler_idx
== WINED3DSIH_LOOP
1677 || ins
.handler_idx
== WINED3DSIH_REP
)
1680 if (cur_loop_depth
> max_loop_depth
)
1681 max_loop_depth
= cur_loop_depth
;
1683 else if (ins
.handler_idx
== WINED3DSIH_ENDLOOP
1684 || ins
.handler_idx
== WINED3DSIH_ENDREP
)
1688 else if (ins
.handler_idx
== WINED3DSIH_GATHER4
1689 || ins
.handler_idx
== WINED3DSIH_GATHER4_C
1690 || ins
.handler_idx
== WINED3DSIH_SAMPLE
1691 || ins
.handler_idx
== WINED3DSIH_SAMPLE_B
1692 || ins
.handler_idx
== WINED3DSIH_SAMPLE_C
1693 || ins
.handler_idx
== WINED3DSIH_SAMPLE_C_LZ
1694 || ins
.handler_idx
== WINED3DSIH_SAMPLE_GRAD
1695 || ins
.handler_idx
== WINED3DSIH_SAMPLE_LOD
)
1697 shader_record_sample(reg_maps
, ins
.src
[1].reg
.idx
[0].offset
,
1698 ins
.src
[2].reg
.idx
[0].offset
, reg_maps
->sampler_map
.count
);
1700 else if (ins
.handler_idx
== WINED3DSIH_GATHER4_PO
1701 || ins
.handler_idx
== WINED3DSIH_GATHER4_PO_C
)
1703 shader_record_sample(reg_maps
, ins
.src
[2].reg
.idx
[0].offset
,
1704 ins
.src
[3].reg
.idx
[0].offset
, reg_maps
->sampler_map
.count
);
1706 else if ((ins
.handler_idx
== WINED3DSIH_BUFINFO
&& ins
.src
[0].reg
.type
== WINED3DSPR_RESOURCE
)
1707 || (ins
.handler_idx
== WINED3DSIH_SAMPLE_INFO
&& ins
.src
[0].reg
.type
== WINED3DSPR_RESOURCE
))
1709 shader_record_sample(reg_maps
, ins
.src
[0].reg
.idx
[0].offset
,
1710 WINED3D_SAMPLER_DEFAULT
, reg_maps
->sampler_map
.count
);
1712 else if (ins
.handler_idx
== WINED3DSIH_LD
1713 || ins
.handler_idx
== WINED3DSIH_LD2DMS
1714 || (ins
.handler_idx
== WINED3DSIH_LD_RAW
&& ins
.src
[1].reg
.type
== WINED3DSPR_RESOURCE
)
1715 || (ins
.handler_idx
== WINED3DSIH_RESINFO
&& ins
.src
[1].reg
.type
== WINED3DSPR_RESOURCE
))
1717 shader_record_sample(reg_maps
, ins
.src
[1].reg
.idx
[0].offset
,
1718 WINED3D_SAMPLER_DEFAULT
, reg_maps
->sampler_map
.count
);
1720 else if (ins
.handler_idx
== WINED3DSIH_LD_STRUCTURED
1721 && ins
.src
[2].reg
.type
== WINED3DSPR_RESOURCE
)
1723 shader_record_sample(reg_maps
, ins
.src
[2].reg
.idx
[0].offset
,
1724 WINED3D_SAMPLER_DEFAULT
, reg_maps
->sampler_map
.count
);
1728 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.predicate
->reg
,
1729 shader_version
.type
, constf_size
))
1730 return WINED3DERR_INVALIDCALL
;
1732 for (i
= 0; i
< ins
.src_count
; ++i
)
1734 unsigned int count
= get_instr_extra_regcount(ins
.handler_idx
, i
);
1735 struct wined3d_shader_register reg
= ins
.src
[i
].reg
;
1737 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.src
[i
].reg
,
1738 shader_version
.type
, constf_size
))
1739 return WINED3DERR_INVALIDCALL
;
1742 ++reg
.idx
[0].offset
;
1743 if (!shader_record_register_usage(shader
, reg_maps
, ®
,
1744 shader_version
.type
, constf_size
))
1745 return WINED3DERR_INVALIDCALL
;
1751 if (ins
.src
[i
].reg
.type
== WINED3DSPR_TEMP
1752 && ins
.src
[i
].swizzle
== WINED3DSP_NOSWIZZLE
)
1754 shader
->u
.ps
.color0_mov
= TRUE
;
1755 shader
->u
.ps
.color0_reg
= ins
.src
[i
].reg
.idx
[0].offset
;
1761 prev_ins
= current_ins
;
1763 reg_maps
->loop_depth
= max_loop_depth
;
1767 phase
->end
= prev_ins
;
1771 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1772 * R0 is written to the render target. */
1773 if (shader_version
.major
< 2 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1774 reg_maps
->rt_mask
|= (1u << 0);
1776 if (input_signature
->elements
)
1778 for (i
= 0; i
< input_signature
->element_count
; ++i
)
1780 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
)
1782 if (input_signature
->elements
[i
].register_idx
>= ARRAY_SIZE(shader
->u
.vs
.attributes
))
1784 WARN("Invalid input signature register index %u.\n", input_signature
->elements
[i
].register_idx
);
1785 return WINED3DERR_INVALIDCALL
;
1788 else if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1790 if (input_signature
->elements
[i
].sysval_semantic
== WINED3D_SV_POSITION
)
1792 else if (input_signature
->elements
[i
].sysval_semantic
== WINED3D_SV_IS_FRONT_FACE
)
1793 reg_maps
->usesfacing
= 1;
1795 reg_maps
->input_registers
|= 1u << input_signature
->elements
[i
].register_idx
;
1798 else if (!input_signature
->elements
&& reg_maps
->input_registers
)
1800 unsigned int count
= wined3d_popcount(reg_maps
->input_registers
);
1801 struct wined3d_shader_signature_element
*e
;
1804 if (!(input_signature
->elements
= calloc(count
, sizeof(*input_signature
->elements
))))
1805 return E_OUTOFMEMORY
;
1806 input_signature
->element_count
= count
;
1808 e
= input_signature
->elements
;
1809 for (i
= 0; i
< ARRAY_SIZE(input_signature_elements
); ++i
)
1811 if (!(reg_maps
->input_registers
& (1u << i
)))
1813 input_signature_elements
[i
].register_idx
= i
;
1814 *e
++ = input_signature_elements
[i
];
1818 if (output_signature
->elements
)
1820 if (FAILED(hr
= shader_scan_output_signature(shader
)))
1823 else if (reg_maps
->output_registers
)
1825 unsigned int count
= wined3d_popcount(reg_maps
->output_registers
);
1826 struct wined3d_shader_signature_element
*e
;
1828 if (!(output_signature
->elements
= calloc(count
, sizeof(*output_signature
->elements
))))
1829 return E_OUTOFMEMORY
;
1830 output_signature
->element_count
= count
;
1832 e
= output_signature
->elements
;
1833 for (i
= 0; i
< ARRAY_SIZE(output_signature_elements
); ++i
)
1835 if (!(reg_maps
->output_registers
& (1u << i
)))
1837 *e
++ = output_signature_elements
[i
];
1844 static void shader_cleanup_reg_maps(struct wined3d_shader_reg_maps
*reg_maps
)
1846 struct wined3d_shader_indexable_temp
*reg
, *reg_next
;
1848 free(reg_maps
->constf
);
1849 free(reg_maps
->sampler_map
.entries
);
1851 LIST_FOR_EACH_ENTRY_SAFE(reg
, reg_next
, ®_maps
->indexable_temps
, struct wined3d_shader_indexable_temp
, entry
)
1853 list_init(®_maps
->indexable_temps
);
1855 free(reg_maps
->tgsm
);
1858 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int max
)
1860 DWORD map
= 1u << max
;
1862 map
&= reg_maps
->shader_version
.major
< 3 ? ~reg_maps
->texcoord
: ~reg_maps
->input_registers
;
1864 return wined3d_log2i(map
);
1867 /* Shared code in order to generate the bulk of the shader string. */
1868 HRESULT
shader_generate_code(const struct wined3d_shader
*shader
, struct wined3d_string_buffer
*buffer
,
1869 const struct wined3d_shader_reg_maps
*reg_maps
, void *backend_ctx
,
1870 const DWORD
*start
, const DWORD
*end
)
1872 struct wined3d_device
*device
= shader
->device
;
1873 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
1874 void *fe_data
= shader
->frontend_data
;
1875 struct wined3d_shader_version shader_version
;
1876 struct wined3d_shader_parser_state state
;
1877 struct wined3d_shader_instruction ins
;
1878 struct wined3d_shader_tex_mx tex_mx
;
1879 struct wined3d_shader_context ctx
;
1882 /* Initialize current parsing state. */
1883 tex_mx
.current_row
= 0;
1884 state
.current_loop_depth
= 0;
1885 state
.current_loop_reg
= 0;
1886 state
.in_subroutine
= FALSE
;
1888 ctx
.shader
= shader
;
1889 ctx
.reg_maps
= reg_maps
;
1890 ctx
.buffer
= buffer
;
1891 ctx
.tex_mx
= &tex_mx
;
1893 ctx
.backend_data
= backend_ctx
;
1896 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1900 while (!fe
->shader_is_end(fe_data
, &ptr
) && ptr
!= end
)
1903 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1905 /* Unknown opcode and its parameters. */
1906 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1908 WARN("Encountered unrecognised or invalid instruction.\n");
1909 return WINED3DERR_INVALIDCALL
;
1913 FIXME("Predicates not implemented.\n");
1915 /* Call appropriate function for output target */
1916 device
->shader_backend
->shader_handle_instruction(&ins
);
1922 static void shader_cleanup(struct wined3d_shader
*shader
)
1924 if (shader
->reg_maps
.shader_version
.type
== WINED3D_SHADER_TYPE_HULL
)
1926 free(shader
->u
.hs
.phases
.control_point
);
1927 free(shader
->u
.hs
.phases
.fork
);
1928 free(shader
->u
.hs
.phases
.join
);
1931 free(shader
->patch_constant_signature
.elements
);
1932 free(shader
->output_signature
.elements
);
1933 free(shader
->input_signature
.elements
);
1934 shader
->device
->shader_backend
->shader_destroy(shader
);
1935 shader_cleanup_reg_maps(&shader
->reg_maps
);
1936 free(shader
->byte_code
);
1937 shader_delete_constant_list(&shader
->constantsF
);
1938 shader_delete_constant_list(&shader
->constantsB
);
1939 shader_delete_constant_list(&shader
->constantsI
);
1940 list_remove(&shader
->shader_list_entry
);
1942 if (shader
->frontend
&& shader
->frontend_data
)
1943 shader
->frontend
->shader_free(shader
->frontend_data
);
1946 struct shader_none_priv
1948 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
1949 const struct wined3d_fragment_pipe_ops
*fragment_pipe
;
1952 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
1953 static void shader_none_precompile(void *shader_priv
, struct wined3d_shader
*shader
) {}
1954 static void shader_none_apply_compute_state(void *shader_priv
, struct wined3d_context
*context
,
1955 const struct wined3d_state
*state
) {}
1956 static void shader_none_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1957 static void shader_none_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1958 static void shader_none_destroy(struct wined3d_shader
*shader
) {}
1959 static void shader_none_free_context_data(struct wined3d_context
*context
) {}
1960 static void shader_none_init_context_state(struct wined3d_context
*context
) {}
1962 /* Context activation is done by the caller. */
1963 static void shader_none_apply_draw_state(void *shader_priv
, struct wined3d_context
*context
,
1964 const struct wined3d_state
*state
)
1966 struct shader_none_priv
*priv
= shader_priv
;
1968 priv
->vertex_pipe
->vp_apply_draw_state(context
, state
);
1969 priv
->fragment_pipe
->fp_apply_draw_state(context
, state
);
1972 /* Context activation is done by the caller. */
1973 static void shader_none_disable(void *shader_priv
, struct wined3d_context
*context
)
1975 struct shader_none_priv
*priv
= shader_priv
;
1977 priv
->vertex_pipe
->vp_disable(context
);
1978 priv
->fragment_pipe
->fp_disable(context
);
1980 context
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
1981 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
1982 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
1983 | (1u << WINED3D_SHADER_TYPE_HULL
)
1984 | (1u << WINED3D_SHADER_TYPE_DOMAIN
)
1985 | (1u << WINED3D_SHADER_TYPE_COMPUTE
);
1988 static HRESULT
shader_none_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
1989 const struct wined3d_fragment_pipe_ops
*fragment_pipe
)
1991 void *vertex_priv
, *fragment_priv
;
1992 struct shader_none_priv
*priv
;
1994 if (!(priv
= malloc(sizeof(*priv
))))
1995 return E_OUTOFMEMORY
;
1997 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&none_shader_backend
, priv
)))
1999 ERR("Failed to initialize vertex pipe.\n");
2004 if (!(fragment_priv
= fragment_pipe
->alloc_private(&none_shader_backend
, priv
)))
2006 ERR("Failed to initialize fragment pipe.\n");
2007 vertex_pipe
->vp_free(device
, NULL
);
2012 priv
->vertex_pipe
= vertex_pipe
;
2013 priv
->fragment_pipe
= fragment_pipe
;
2015 device
->vertex_priv
= vertex_priv
;
2016 device
->fragment_priv
= fragment_priv
;
2017 device
->shader_priv
= priv
;
2022 static void shader_none_free(struct wined3d_device
*device
, struct wined3d_context
*context
)
2024 struct shader_none_priv
*priv
= device
->shader_priv
;
2026 priv
->fragment_pipe
->free_private(device
, context
);
2027 priv
->vertex_pipe
->vp_free(device
, context
);
2031 static BOOL
shader_none_allocate_context_data(struct wined3d_context
*context
)
2036 static void shader_none_get_caps(const struct wined3d_adapter
*adapter
, struct shader_caps
*caps
)
2038 /* Set the shader caps to 0 for the none shader backend */
2039 memset(caps
, 0, sizeof(*caps
));
2042 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
2044 /* We "support" every possible fixup, since we don't support any shader
2045 * model, and will never have to actually sample a texture. */
2049 static uint64_t shader_none_shader_compile(struct wined3d_context
*context
, const struct wined3d_shader_desc
*shader_desc
,
2050 enum wined3d_shader_type shader_type
)
2055 const struct wined3d_shader_backend_ops none_shader_backend
=
2057 shader_none_handle_instruction
,
2058 shader_none_precompile
,
2059 shader_none_apply_draw_state
,
2060 shader_none_apply_compute_state
,
2061 shader_none_disable
,
2062 shader_none_update_float_vertex_constants
,
2063 shader_none_update_float_pixel_constants
,
2064 shader_none_destroy
,
2067 shader_none_allocate_context_data
,
2068 shader_none_free_context_data
,
2069 shader_none_init_context_state
,
2070 shader_none_get_caps
,
2071 shader_none_color_fixup_supported
,
2072 shader_none_shader_compile
,
2075 static unsigned int shader_max_version_from_feature_level(enum wined3d_feature_level level
)
2079 case WINED3D_FEATURE_LEVEL_11_1
:
2080 case WINED3D_FEATURE_LEVEL_11
:
2082 case WINED3D_FEATURE_LEVEL_10_1
:
2083 case WINED3D_FEATURE_LEVEL_10
:
2085 case WINED3D_FEATURE_LEVEL_9_3
:
2087 case WINED3D_FEATURE_LEVEL_9_2
:
2088 case WINED3D_FEATURE_LEVEL_9_1
:
2095 static HRESULT
shader_set_function(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2096 enum wined3d_shader_type type
, unsigned int float_const_count
)
2098 const struct wined3d_d3d_info
*d3d_info
= &shader
->device
->adapter
->d3d_info
;
2099 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2100 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
2101 const struct wined3d_shader_frontend
*fe
;
2102 unsigned int backend_version
;
2105 TRACE("shader %p, device %p, type %s, float_const_count %u.\n",
2106 shader
, device
, debug_shader_type(type
), float_const_count
);
2108 fe
= shader
->frontend
;
2109 if (!(shader
->frontend_data
= fe
->shader_init(shader
->function
,
2110 shader
->functionLength
, &shader
->output_signature
)))
2112 FIXME("Failed to initialize frontend.\n");
2113 return WINED3DERR_INVALIDCALL
;
2116 if (FAILED(hr
= shader_get_registers_used(shader
, float_const_count
)))
2119 if (version
->type
!= type
)
2121 WARN("Wrong shader type %s.\n", debug_shader_type(reg_maps
->shader_version
.type
));
2122 return WINED3DERR_INVALIDCALL
;
2124 if (version
->major
> shader_max_version_from_feature_level(device
->cs
->c
.state
->feature_level
))
2126 WARN("Shader version %u not supported by this device.\n", version
->major
);
2127 return WINED3DERR_INVALIDCALL
;
2131 case WINED3D_SHADER_TYPE_VERTEX
:
2132 backend_version
= d3d_info
->limits
.vs_version
;
2134 case WINED3D_SHADER_TYPE_HULL
:
2135 backend_version
= d3d_info
->limits
.hs_version
;
2137 case WINED3D_SHADER_TYPE_DOMAIN
:
2138 backend_version
= d3d_info
->limits
.ds_version
;
2140 case WINED3D_SHADER_TYPE_GEOMETRY
:
2141 backend_version
= d3d_info
->limits
.gs_version
;
2143 case WINED3D_SHADER_TYPE_PIXEL
:
2144 backend_version
= d3d_info
->limits
.ps_version
;
2146 case WINED3D_SHADER_TYPE_COMPUTE
:
2147 backend_version
= d3d_info
->limits
.cs_version
;
2150 FIXME("No backend version-checking for this shader type.\n");
2151 backend_version
= 0;
2153 if (version
->major
> backend_version
)
2155 WARN("Shader version %u.%u not supported by the current shader backend.\n",
2156 version
->major
, version
->minor
);
2157 return WINED3DERR_INVALIDCALL
;
2160 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2165 ULONG CDECL
wined3d_shader_incref(struct wined3d_shader
*shader
)
2167 unsigned int refcount
= InterlockedIncrement(&shader
->ref
);
2169 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
2174 static void wined3d_shader_init_object(void *object
)
2176 struct wined3d_shader
*shader
= object
;
2177 struct wined3d_device
*device
= shader
->device
;
2179 TRACE("shader %p.\n", shader
);
2181 list_add_head(&device
->shaders
, &shader
->shader_list_entry
);
2183 device
->shader_backend
->shader_precompile(device
->shader_priv
, shader
);
2186 static void wined3d_shader_destroy_object(void *object
)
2188 TRACE("object %p.\n", object
);
2190 shader_cleanup(object
);
2194 ULONG CDECL
wined3d_shader_decref(struct wined3d_shader
*shader
)
2196 unsigned int refcount
= InterlockedDecrement(&shader
->ref
);
2198 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
2202 wined3d_mutex_lock();
2203 shader
->parent_ops
->wined3d_object_destroyed(shader
->parent
);
2204 wined3d_cs_destroy_object(shader
->device
->cs
, wined3d_shader_destroy_object
, shader
);
2205 wined3d_mutex_unlock();
2211 void * CDECL
wined3d_shader_get_parent(const struct wined3d_shader
*shader
)
2213 TRACE("shader %p.\n", shader
);
2215 return shader
->parent
;
2218 HRESULT CDECL
wined3d_shader_get_byte_code(const struct wined3d_shader
*shader
,
2219 void *byte_code
, UINT
*byte_code_size
)
2221 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader
, byte_code
, byte_code_size
);
2225 *byte_code_size
= shader
->byte_code_size
;
2229 if (*byte_code_size
< shader
->byte_code_size
)
2231 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
2232 * than the required size we should write the required size and
2233 * return D3DERR_MOREDATA. That's not actually true. */
2234 return WINED3DERR_INVALIDCALL
;
2237 memcpy(byte_code
, shader
->byte_code
, shader
->byte_code_size
);
2242 /* Set local constants for d3d8 shaders. */
2243 HRESULT CDECL
wined3d_shader_set_local_constants_float(struct wined3d_shader
*shader
,
2244 UINT start_idx
, const float *src_data
, UINT count
)
2246 UINT end_idx
= start_idx
+ count
;
2249 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader
, start_idx
, src_data
, count
);
2251 if (end_idx
> shader
->limits
->constant_float
)
2253 WARN("end_idx %u > float constants limit %u.\n",
2254 end_idx
, shader
->limits
->constant_float
);
2255 end_idx
= shader
->limits
->constant_float
;
2258 for (i
= start_idx
; i
< end_idx
; ++i
)
2260 struct wined3d_shader_lconst
*lconst
;
2263 if (!(lconst
= malloc(sizeof(*lconst
))))
2264 return E_OUTOFMEMORY
;
2267 value
= (float *)lconst
->value
;
2268 memcpy(value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
2269 list_add_head(&shader
->constantsF
, &lconst
->entry
);
2271 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
2272 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
2274 shader
->lconst_inf_or_nan
= TRUE
;
2281 static void init_interpolation_compile_args(uint32_t *interpolation_args
,
2282 const struct wined3d_shader
*pixel_shader
, const struct wined3d_d3d_info
*d3d_info
)
2284 if (!d3d_info
->shader_output_interpolation
|| !pixel_shader
2285 || pixel_shader
->reg_maps
.shader_version
.major
< 4)
2287 memset(interpolation_args
, 0, sizeof(pixel_shader
->u
.ps
.interpolation_mode
));
2291 memcpy(interpolation_args
, pixel_shader
->u
.ps
.interpolation_mode
,
2292 sizeof(pixel_shader
->u
.ps
.interpolation_mode
));
2295 void find_vs_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2296 struct vs_compile_args
*args
, const struct wined3d_context
*context
)
2298 const struct wined3d_shader
*geometry_shader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
2299 const struct wined3d_shader
*pixel_shader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2300 const struct wined3d_shader
*hull_shader
= state
->shader
[WINED3D_SHADER_TYPE_HULL
];
2301 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2302 WORD swizzle_map
= context
->stream_info
.swizzle_map
;
2304 args
->fog_src
= state
->render_states
[WINED3D_RS_FOGTABLEMODE
]
2305 == WINED3D_FOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
2306 args
->clip_enabled
= state
->render_states
[WINED3D_RS_CLIPPING
]
2307 && state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
];
2308 args
->point_size
= state
->primitive_type
== WINED3D_PT_POINTLIST
;
2309 args
->per_vertex_point_size
= shader
->reg_maps
.point_size
;
2310 args
->next_shader_type
= hull_shader
? WINED3D_SHADER_TYPE_HULL
2311 : geometry_shader
? WINED3D_SHADER_TYPE_GEOMETRY
: WINED3D_SHADER_TYPE_PIXEL
;
2312 if (shader
->reg_maps
.shader_version
.major
>= 4)
2313 args
->next_shader_input_count
= hull_shader
? hull_shader
->limits
->packed_input
2314 : geometry_shader
? geometry_shader
->limits
->packed_input
2315 : pixel_shader
? pixel_shader
->limits
->packed_input
: 0;
2317 args
->next_shader_input_count
= 0;
2318 args
->swizzle_map
= swizzle_map
;
2319 if (d3d_info
->emulated_flatshading
)
2320 args
->flatshading
= state
->render_states
[WINED3D_RS_SHADEMODE
] == WINED3D_SHADE_FLAT
;
2322 args
->flatshading
= 0;
2324 init_interpolation_compile_args(args
->interpolation_mode
,
2325 args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
? pixel_shader
: NULL
, d3d_info
);
2328 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
2330 if (usage_idx1
!= usage_idx2
)
2332 if (usage1
== usage2
)
2334 if (usage1
== WINED3D_DECL_USAGE_POSITION
&& usage2
== WINED3D_DECL_USAGE_POSITIONT
)
2336 if (usage2
== WINED3D_DECL_USAGE_POSITION
&& usage1
== WINED3D_DECL_USAGE_POSITIONT
)
2342 bool vshader_get_input(const struct wined3d_shader
*shader
,
2343 uint8_t usage_req
, uint8_t usage_idx_req
, unsigned int *regnum
)
2345 uint32_t map
= shader
->reg_maps
.input_registers
& 0xffff;
2350 i
= wined3d_bit_scan(&map
);
2351 if (match_usage(shader
->u
.vs
.attributes
[i
].usage
,
2352 shader
->u
.vs
.attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
2362 static void shader_trace(const void *code
, size_t size
, enum vkd3d_shader_source_type source_type
)
2364 struct vkd3d_shader_compile_info info
;
2365 struct vkd3d_shader_code d3d_asm
;
2366 const char *ptr
, *end
, *line
;
2370 static const struct vkd3d_shader_compile_option compile_options
[] =
2372 {VKD3D_SHADER_COMPILE_OPTION_API_VERSION
, VKD3D_SHADER_API_VERSION_1_6
},
2375 info
.type
= VKD3D_SHADER_STRUCTURE_TYPE_COMPILE_INFO
;
2377 info
.source
.code
= code
;
2378 info
.source
.size
= size
;
2379 info
.source_type
= source_type
;
2380 info
.target_type
= VKD3D_SHADER_TARGET_D3D_ASM
;
2381 info
.options
= compile_options
;
2382 info
.option_count
= ARRAY_SIZE(compile_options
);
2383 info
.log_level
= VKD3D_SHADER_LOG_WARNING
;
2384 info
.source_name
= NULL
;
2386 ret
= vkd3d_shader_compile(&info
, &d3d_asm
, &messages
);
2387 if (messages
&& *messages
&& FIXME_ON(d3d_shader
))
2389 FIXME("Shader log:\n");
2391 end
= ptr
+ strlen(ptr
);
2392 while ((line
= wined3d_get_line(&ptr
, end
)))
2394 FIXME(" %.*s", (int)(ptr
- line
), line
);
2398 vkd3d_shader_free_messages(messages
);
2402 ERR("Failed to disassemble, ret %d.\n", ret
);
2407 end
= ptr
+ d3d_asm
.size
;
2408 while ((line
= wined3d_get_line(&ptr
, end
)))
2410 TRACE(" %.*s", (int)(ptr
- line
), line
);
2414 vkd3d_shader_free_shader_code(&d3d_asm
);
2417 static HRESULT
shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2418 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2422 TRACE("byte_code %p, byte_code_size %#lx.\n", desc
->byte_code
, (long)desc
->byte_code_size
);
2424 if (!desc
->byte_code
)
2425 return WINED3DERR_INVALIDCALL
;
2428 shader
->device
= device
;
2429 shader
->parent
= parent
;
2430 shader
->parent_ops
= parent_ops
;
2432 list_init(&shader
->linked_programs
);
2433 list_init(&shader
->constantsF
);
2434 list_init(&shader
->constantsB
);
2435 list_init(&shader
->constantsI
);
2436 shader
->lconst_inf_or_nan
= FALSE
;
2437 list_init(&shader
->reg_maps
.indexable_temps
);
2438 list_init(&shader
->shader_list_entry
);
2440 if (desc
->byte_code_size
== ~(size_t)0)
2442 struct wined3d_shader_version shader_version
;
2443 const struct wined3d_shader_frontend
*fe
;
2444 struct wined3d_shader_instruction ins
;
2448 shader
->source_type
= VKD3D_SHADER_SOURCE_D3D_BYTECODE
;
2449 if (!(shader
->frontend
= shader_select_frontend(shader
->source_type
)))
2451 FIXME("Unable to find frontend for shader.\n");
2452 hr
= WINED3DERR_INVALIDCALL
;
2456 fe
= shader
->frontend
;
2457 if (!(fe_data
= fe
->shader_init(desc
->byte_code
, desc
->byte_code_size
, &shader
->output_signature
)))
2459 WARN("Failed to initialise frontend data.\n");
2460 hr
= WINED3DERR_INVALIDCALL
;
2464 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
2465 while (!fe
->shader_is_end(fe_data
, &ptr
))
2466 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
2468 fe
->shader_free(fe_data
);
2470 shader
->byte_code_size
= (ptr
- desc
->byte_code
) * sizeof(*ptr
);
2472 if (!(shader
->byte_code
= malloc(shader
->byte_code_size
)))
2477 memcpy(shader
->byte_code
, desc
->byte_code
, shader
->byte_code_size
);
2479 shader
->function
= shader
->byte_code
;
2480 shader
->functionLength
= shader
->byte_code_size
;
2484 unsigned int max_version
;
2486 if (!(shader
->byte_code
= malloc(desc
->byte_code_size
)))
2491 memcpy(shader
->byte_code
, desc
->byte_code
, desc
->byte_code_size
);
2492 shader
->byte_code_size
= desc
->byte_code_size
;
2494 max_version
= shader_max_version_from_feature_level(device
->cs
->c
.state
->feature_level
);
2495 if (FAILED(hr
= wined3d_shader_extract_from_dxbc(shader
, max_version
, &shader
->source_type
)))
2498 if (!(shader
->frontend
= shader_select_frontend(shader
->source_type
)))
2500 FIXME("Unable to find frontend for shader.\n");
2501 hr
= WINED3DERR_INVALIDCALL
;
2506 if (TRACE_ON(d3d_shader
))
2508 if (shader
->source_type
== VKD3D_SHADER_SOURCE_D3D_BYTECODE
)
2509 shader_trace(shader
->function
, shader
->functionLength
, shader
->source_type
);
2511 shader_trace(shader
->byte_code
, shader
->byte_code_size
, shader
->source_type
);
2518 shader_cleanup(shader
);
2522 static HRESULT
vertex_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2523 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2525 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2529 if (FAILED(hr
= shader_init(shader
, device
, desc
, parent
, parent_ops
)))
2532 if (FAILED(hr
= shader_set_function(shader
, device
,
2533 WINED3D_SHADER_TYPE_VERTEX
, device
->adapter
->d3d_info
.limits
.vs_uniform_count
)))
2535 shader_cleanup(shader
);
2539 for (i
= 0; i
< shader
->input_signature
.element_count
; ++i
)
2541 const struct wined3d_shader_signature_element
*input
= &shader
->input_signature
.elements
[i
];
2543 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)) || !input
->semantic_name
)
2546 shader
->u
.vs
.attributes
[input
->register_idx
].usage
=
2547 shader_usage_from_semantic_name(input
->semantic_name
);
2548 shader
->u
.vs
.attributes
[input
->register_idx
].usage_idx
= input
->semantic_idx
;
2551 if (reg_maps
->usesrelconstF
&& !list_empty(&shader
->constantsF
))
2552 shader
->load_local_constsF
= TRUE
;
2557 static struct wined3d_shader_signature_element
*shader_find_signature_element(const struct wined3d_shader_signature
*s
,
2558 unsigned int stream_idx
, const char *semantic_name
, unsigned int semantic_idx
)
2560 struct wined3d_shader_signature_element
*e
= s
->elements
;
2563 for (i
= 0; i
< s
->element_count
; ++i
)
2565 if (e
[i
].stream_idx
== stream_idx
2566 && !stricmp(e
[i
].semantic_name
, semantic_name
)
2567 && e
[i
].semantic_idx
== semantic_idx
)
2574 BOOL
shader_get_stream_output_register_info(const struct wined3d_shader
*shader
,
2575 const struct wined3d_stream_output_element
*so_element
, unsigned int *register_idx
, unsigned int *component_idx
)
2577 const struct wined3d_shader_signature_element
*output
;
2580 if (!(output
= shader_find_signature_element(&shader
->output_signature
,
2581 so_element
->stream_idx
, so_element
->semantic_name
, so_element
->semantic_idx
)))
2584 for (idx
= 0; idx
< 4; ++idx
)
2586 if (output
->mask
& (1u << idx
))
2589 idx
+= so_element
->component_idx
;
2591 *register_idx
= output
->register_idx
;
2592 *component_idx
= idx
;
2596 static HRESULT
geometry_shader_init_so_desc(struct wined3d_geometry_shader
*gs
, struct wined3d_device
*device
,
2597 const struct wined3d_stream_output_desc
*so_desc
)
2599 struct wined3d_so_desc_entry
*s
;
2600 struct wine_rb_entry
*entry
;
2605 if ((entry
= wine_rb_get(&device
->so_descs
, so_desc
)))
2607 gs
->so_desc
= &WINE_RB_ENTRY_VALUE(entry
, struct wined3d_so_desc_entry
, entry
)->desc
;
2611 size
= FIELD_OFFSET(struct wined3d_so_desc_entry
, elements
[so_desc
->element_count
]);
2612 for (i
= 0; i
< so_desc
->element_count
; ++i
)
2614 const char *n
= so_desc
->elements
[i
].semantic_name
;
2617 size
+= strlen(n
) + 1;
2619 if (!(s
= malloc(size
)))
2620 return E_OUTOFMEMORY
;
2624 memcpy(s
->elements
, so_desc
->elements
, so_desc
->element_count
* sizeof(*s
->elements
));
2625 s
->desc
.elements
= s
->elements
;
2627 name
= (char *)&s
->elements
[s
->desc
.element_count
];
2628 for (i
= 0; i
< so_desc
->element_count
; ++i
)
2630 struct wined3d_stream_output_element
*e
= &s
->elements
[i
];
2632 if (!e
->semantic_name
)
2635 size
= strlen(e
->semantic_name
) + 1;
2636 memcpy(name
, e
->semantic_name
, size
);
2637 e
->semantic_name
= name
;
2641 if (wine_rb_put(&device
->so_descs
, &s
->desc
, &s
->entry
) == -1)
2646 gs
->so_desc
= &s
->desc
;
2651 static HRESULT
geometry_shader_init_stream_output(struct wined3d_shader
*shader
,
2652 const struct wined3d_stream_output_desc
*so_desc
)
2654 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
2655 const struct wined3d_shader_signature_element
*output
;
2656 unsigned int i
, component_idx
, register_idx
, mask
;
2657 struct wined3d_shader_version shader_version
;
2665 if (!(fe_data
= fe
->shader_init(shader
->function
, shader
->functionLength
, &shader
->output_signature
)))
2667 WARN("Failed to initialise frontend data.\n");
2668 return WINED3DERR_INVALIDCALL
;
2670 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
2671 fe
->shader_free(fe_data
);
2673 switch (shader_version
.type
)
2675 case WINED3D_SHADER_TYPE_VERTEX
:
2676 case WINED3D_SHADER_TYPE_DOMAIN
:
2677 shader
->function
= NULL
;
2678 shader
->functionLength
= 0;
2680 case WINED3D_SHADER_TYPE_GEOMETRY
:
2683 WARN("Wrong shader type %s.\n", debug_shader_type(shader_version
.type
));
2684 return E_INVALIDARG
;
2687 if (!shader
->function
)
2689 shader
->reg_maps
.shader_version
= shader_version
;
2690 shader
->reg_maps
.shader_version
.type
= WINED3D_SHADER_TYPE_GEOMETRY
;
2691 shader_set_limits(shader
);
2692 if (FAILED(hr
= shader_scan_output_signature(shader
)))
2696 for (i
= 0; i
< so_desc
->element_count
; ++i
)
2698 const struct wined3d_stream_output_element
*e
= &so_desc
->elements
[i
];
2700 if (!e
->semantic_name
)
2702 if (!(output
= shader_find_signature_element(&shader
->output_signature
,
2703 e
->stream_idx
, e
->semantic_name
, e
->semantic_idx
))
2704 || !shader_get_stream_output_register_info(shader
, e
, ®ister_idx
, &component_idx
))
2706 WARN("Failed to find output signature element for stream output entry.\n");
2707 return E_INVALIDARG
;
2710 mask
= wined3d_mask_from_size(e
->component_count
) << component_idx
;
2711 if ((output
->mask
& 0xff & mask
) != mask
)
2713 WARN("Invalid component range %u-%u (mask %#x), output mask %#x.\n",
2714 component_idx
, e
->component_count
, mask
, output
->mask
& 0xff);
2715 return E_INVALIDARG
;
2719 if (FAILED(hr
= geometry_shader_init_so_desc(&shader
->u
.gs
, shader
->device
, so_desc
)))
2721 WARN("Failed to initialise stream output description, hr %#lx.\n", hr
);
2728 static HRESULT
geometry_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2729 const struct wined3d_shader_desc
*desc
, const struct wined3d_stream_output_desc
*so_desc
,
2730 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2734 if (FAILED(hr
= shader_init(shader
, device
, desc
, parent
, parent_ops
)))
2737 if (FAILED(hr
= geometry_shader_init_stream_output(shader
, so_desc
)))
2740 if (shader
->function
2741 && FAILED(hr
= shader_set_function(shader
, device
, WINED3D_SHADER_TYPE_GEOMETRY
, 0)))
2747 shader_cleanup(shader
);
2751 void find_ds_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2752 struct ds_compile_args
*args
, const struct wined3d_context
*context
)
2754 const struct wined3d_shader
*geometry_shader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
2755 const struct wined3d_shader
*pixel_shader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2756 const struct wined3d_shader
*hull_shader
= state
->shader
[WINED3D_SHADER_TYPE_HULL
];
2758 args
->tessellator_output_primitive
= hull_shader
->u
.hs
.tessellator_output_primitive
;
2759 args
->tessellator_partitioning
= hull_shader
->u
.hs
.tessellator_partitioning
;
2761 args
->output_count
= geometry_shader
? geometry_shader
->limits
->packed_input
2762 : pixel_shader
? pixel_shader
->limits
->packed_input
: shader
->limits
->packed_output
;
2763 args
->next_shader_type
= geometry_shader
? WINED3D_SHADER_TYPE_GEOMETRY
: WINED3D_SHADER_TYPE_PIXEL
;
2765 args
->render_offscreen
= context
->render_offscreen
;
2767 init_interpolation_compile_args(args
->interpolation_mode
,
2768 args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
? pixel_shader
: NULL
, context
->d3d_info
);
2773 void find_gs_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2774 struct gs_compile_args
*args
, const struct wined3d_context
*context
)
2776 const struct wined3d_shader
*pixel_shader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2778 args
->output_count
= pixel_shader
? pixel_shader
->limits
->packed_input
: shader
->limits
->packed_output
;
2780 if (!(args
->primitive_type
= shader
->u
.gs
.input_type
))
2781 args
->primitive_type
= state
->primitive_type
;
2783 init_interpolation_compile_args(args
->interpolation_mode
, pixel_shader
, context
->d3d_info
);
2786 void find_ps_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2787 BOOL position_transformed
, struct ps_compile_args
*args
, const struct wined3d_context
*context
)
2789 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2790 struct wined3d_shader_resource_view
*view
;
2791 struct wined3d_texture
*texture
;
2794 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
2795 if (!d3d_info
->srgb_write_control
&& needs_srgb_write(d3d_info
, state
, &state
->fb
))
2797 static unsigned int warned
= 0;
2799 args
->srgb_correction
= 1;
2800 if (state
->blend_state
&& state
->blend_state
->desc
.rt
[0].enable
&& !warned
++)
2801 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
2802 "support, expect rendering artifacts.\n");
2805 if (shader
->reg_maps
.shader_version
.major
== 1
2806 && shader
->reg_maps
.shader_version
.minor
<= 3)
2808 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2810 uint32_t flags
= state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
];
2812 if (flags
& WINED3D_TTFF_PROJECTED
)
2814 uint32_t tex_transform
= flags
& ~WINED3D_TTFF_PROJECTED
;
2816 if (!state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
2818 enum wined3d_shader_resource_type resource_type
= shader
->reg_maps
.resource_info
[i
].type
;
2820 unsigned int index
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
2821 uint32_t max_valid
= WINED3D_TTFF_COUNT4
;
2823 for (j
= 0; j
< state
->vertex_declaration
->element_count
; ++j
)
2825 struct wined3d_vertex_declaration_element
*element
=
2826 &state
->vertex_declaration
->elements
[j
];
2828 if (element
->usage
== WINED3D_DECL_USAGE_TEXCOORD
2829 && element
->usage_idx
== index
)
2831 max_valid
= element
->format
->component_count
;
2835 if (!tex_transform
|| tex_transform
> max_valid
)
2837 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2838 tex_transform
, max_valid
);
2839 tex_transform
= max_valid
;
2841 if ((resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_1D
&& tex_transform
> WINED3D_TTFF_COUNT1
)
2842 || (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2D
2843 && tex_transform
> WINED3D_TTFF_COUNT2
)
2844 || (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_3D
2845 && tex_transform
> WINED3D_TTFF_COUNT3
))
2846 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
2849 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2850 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2851 i
, tex_transform
, resource_type
);
2855 tex_transform
= WINED3D_TTFF_COUNT4
| WINED3D_PSARGS_PROJECTED
;
2857 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
2861 if (shader
->reg_maps
.shader_version
.major
== 1
2862 && shader
->reg_maps
.shader_version
.minor
<= 4)
2864 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2866 enum wined3d_shader_tex_types type
= WINED3D_SHADER_TEX_2D
;
2868 if (!shader
->reg_maps
.resource_info
[i
].type
)
2871 /* Treat unbound textures as 2D. The dummy texture will provide
2872 * the proper sample value. The tex_types bitmap defaults to
2873 * 2D because of the memset. */
2874 if (!(view
= state
->shader_resource_view
[WINED3D_SHADER_TYPE_PIXEL
][i
]))
2876 texture
= texture_from_resource(view
->resource
);
2878 switch (texture
->resource
.type
)
2880 case WINED3D_RTYPE_NONE
:
2881 case WINED3D_RTYPE_BUFFER
:
2882 case WINED3D_RTYPE_TEXTURE_1D
:
2885 case WINED3D_RTYPE_TEXTURE_2D
:
2886 if (texture
->resource
.usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
2887 type
= WINED3D_SHADER_TEX_CUBE
;
2890 case WINED3D_RTYPE_TEXTURE_3D
:
2891 type
= WINED3D_SHADER_TEX_3D
;
2895 args
->tex_types
|= (type
<< (i
* WINED3D_PSARGS_TEXTYPE_SHIFT
));
2898 else if (shader
->reg_maps
.shader_version
.major
<= 3)
2900 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2902 enum wined3d_shader_resource_type resource_type
;
2903 enum wined3d_shader_tex_types tex_type
;
2905 if (!(resource_type
= shader
->reg_maps
.resource_info
[i
].type
))
2908 switch (resource_type
)
2910 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
2911 tex_type
= WINED3D_SHADER_TEX_3D
;
2913 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
2914 tex_type
= WINED3D_SHADER_TEX_CUBE
;
2917 tex_type
= WINED3D_SHADER_TEX_2D
;
2921 if ((view
= state
->shader_resource_view
[WINED3D_SHADER_TYPE_PIXEL
][i
]))
2923 texture
= texture_from_resource(view
->resource
);
2924 /* Star Wars: The Old Republic uses mismatched samplers for rendering water. */
2925 if (texture
->resource
.type
== WINED3D_RTYPE_TEXTURE_2D
2926 && resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_3D
2927 && !(texture
->resource
.usage
& WINED3DUSAGE_LEGACY_CUBEMAP
))
2928 tex_type
= WINED3D_SHADER_TEX_2D
;
2929 else if (texture
->resource
.type
== WINED3D_RTYPE_TEXTURE_3D
2930 && resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2D
)
2931 tex_type
= WINED3D_SHADER_TEX_3D
;
2933 args
->tex_types
|= tex_type
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
2937 if (shader
->reg_maps
.shader_version
.major
>= 4)
2939 /* In SM4+ we use dcl_sampler in order to determine if we should use shadow sampler. */
2941 for (i
= 0 ; i
< WINED3D_MAX_FRAGMENT_SAMPLERS
; ++i
)
2942 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
2943 args
->np2_fixup
= 0;
2947 for (i
= 0; i
< WINED3D_MAX_FRAGMENT_SAMPLERS
; ++i
)
2949 if (!shader
->reg_maps
.resource_info
[i
].type
)
2952 if (!(view
= state
->shader_resource_view
[WINED3D_SHADER_TYPE_PIXEL
][i
]))
2954 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
2957 texture
= texture_from_resource(view
->resource
);
2959 if (can_use_texture_swizzle(d3d_info
, texture
->resource
.format
))
2960 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
2962 args
->color_fixup
[i
] = texture
->resource
.format
->color_fixup
;
2964 if (texture
->resource
.format_caps
& WINED3D_FORMAT_CAP_SHADOW
)
2965 args
->shadow
|= 1u << i
;
2967 /* Flag samplers that need NP2 texcoord fixup. */
2968 if (!(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
2969 args
->np2_fixup
|= (1u << i
);
2973 if (shader
->reg_maps
.shader_version
.major
>= 3)
2975 if (position_transformed
)
2976 args
->vp_mode
= WINED3D_VP_MODE_NONE
;
2977 else if (use_vs(state
))
2978 args
->vp_mode
= WINED3D_VP_MODE_SHADER
;
2980 args
->vp_mode
= WINED3D_VP_MODE_FF
;
2981 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
2985 args
->vp_mode
= WINED3D_VP_MODE_SHADER
;
2986 if (state
->render_states
[WINED3D_RS_FOGENABLE
])
2988 switch (state
->render_states
[WINED3D_RS_FOGTABLEMODE
])
2990 case WINED3D_FOG_NONE
:
2991 if (position_transformed
|| use_vs(state
))
2993 args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
;
2997 switch (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
])
2999 case WINED3D_FOG_NONE
: /* Fall through. */
3000 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
3001 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
3002 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
3006 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
3007 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
3008 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
3013 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
3017 if (!d3d_info
->full_ffp_varyings
)
3019 const struct wined3d_shader
*vs
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
3021 args
->texcoords_initialized
= 0;
3022 for (i
= 0; i
< WINED3D_MAX_FFP_TEXTURES
; ++i
)
3026 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.output_registers
& (1u << i
))
3027 args
->texcoords_initialized
|= 1u << i
;
3031 const struct wined3d_stream_info
*si
= &context
->stream_info
;
3032 unsigned int coord_idx
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
3034 if ((state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
] >> WINED3D_FFP_TCI_SHIFT
)
3035 & WINED3D_FFP_TCI_MASK
3036 || (coord_idx
< WINED3D_MAX_FFP_TEXTURES
&& (si
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ coord_idx
)))))
3037 args
->texcoords_initialized
|= 1u << i
;
3043 args
->texcoords_initialized
= wined3d_mask_from_size(WINED3D_MAX_FFP_TEXTURES
);
3046 args
->pointsprite
= state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
]
3047 && state
->primitive_type
== WINED3D_PT_POINTLIST
;
3049 if (d3d_info
->ffp_alpha_test
)
3050 args
->alpha_test_func
= WINED3D_CMP_ALWAYS
- 1;
3052 args
->alpha_test_func
= (state
->render_states
[WINED3D_RS_ALPHATESTENABLE
]
3053 ? wined3d_sanitize_cmp_func(state
->render_states
[WINED3D_RS_ALPHAFUNC
])
3054 : WINED3D_CMP_ALWAYS
) - 1;
3056 if (d3d_info
->emulated_flatshading
)
3057 args
->flatshading
= state
->render_states
[WINED3D_RS_SHADEMODE
] == WINED3D_SHADE_FLAT
;
3059 args
->y_correction
= (shader
->reg_maps
.vpos
&& d3d_info
->frag_coord_correction
)
3060 || (shader
->reg_maps
.usesdsy
&& wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
3061 ? !context
->render_offscreen
: 0;
3063 for (i
= 0; i
< ARRAY_SIZE(state
->fb
.render_targets
); ++i
)
3065 struct wined3d_rendertarget_view
*rtv
= state
->fb
.render_targets
[i
];
3066 if (rtv
&& rtv
->format
->id
== WINED3DFMT_A8_UNORM
&& !is_identity_fixup(rtv
->format
->color_fixup
))
3067 args
->rt_alpha_swizzle
|= 1u << i
;
3070 args
->dual_source_blend
= state
->blend_state
&& state
->blend_state
->dual_source
;
3073 static HRESULT
pixel_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3074 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3076 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
3077 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
3080 if (FAILED(hr
= shader_init(shader
, device
, desc
, parent
, parent_ops
)))
3083 if (FAILED(hr
= shader_set_function(shader
, device
,
3084 WINED3D_SHADER_TYPE_PIXEL
, d3d_info
->limits
.ps_uniform_count
)))
3086 shader_cleanup(shader
);
3090 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
3092 if (shader
->u
.ps
.input_reg_used
& (1u << i
))
3095 highest_reg_used
= i
;
3099 /* Don't do any register mapping magic if it is not needed, or if we can't
3100 * achieve anything anyway */
3101 if (highest_reg_used
< (d3d_info
->limits
.varying_count
/ 4)
3102 || num_regs_used
> (d3d_info
->limits
.varying_count
/ 4)
3103 || shader
->reg_maps
.shader_version
.major
>= 4)
3105 if (num_regs_used
> (d3d_info
->limits
.varying_count
/ 4))
3107 /* This happens with relative addressing. The input mapper function
3108 * warns about this if the higher registers are declared too, so
3109 * don't write a FIXME here */
3110 WARN("More varying registers used than supported\n");
3113 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
3115 shader
->u
.ps
.input_reg_map
[i
] = i
;
3118 shader
->u
.ps
.declared_in_count
= highest_reg_used
+ 1;
3122 shader
->u
.ps
.declared_in_count
= 0;
3123 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
3125 if (shader
->u
.ps
.input_reg_used
& (1u << i
))
3126 shader
->u
.ps
.input_reg_map
[i
] = shader
->u
.ps
.declared_in_count
++;
3127 else shader
->u
.ps
.input_reg_map
[i
] = ~0U;
3134 enum wined3d_shader_resource_type
pixelshader_get_resource_type(const struct wined3d_shader_reg_maps
*reg_maps
,
3135 unsigned int resource_idx
, DWORD tex_types
)
3137 static enum wined3d_shader_resource_type shader_resource_type_from_shader_tex_types
[] =
3139 WINED3D_SHADER_RESOURCE_TEXTURE_2D
, /* WINED3D_SHADER_TEX_2D */
3140 WINED3D_SHADER_RESOURCE_TEXTURE_3D
, /* WINED3D_SHADER_TEX_3D */
3141 WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
, /* WINED3D_SHADER_TEX_CUBE */
3146 if (reg_maps
->shader_version
.major
> 3)
3147 return reg_maps
->resource_info
[resource_idx
].type
;
3149 if (!reg_maps
->resource_info
[resource_idx
].type
)
3152 idx
= (tex_types
>> resource_idx
* WINED3D_PSARGS_TEXTYPE_SHIFT
) & WINED3D_PSARGS_TEXTYPE_MASK
;
3153 assert(idx
< ARRAY_SIZE(shader_resource_type_from_shader_tex_types
));
3154 return shader_resource_type_from_shader_tex_types
[idx
];
3157 HRESULT CDECL
wined3d_shader_create_cs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3158 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3160 struct wined3d_shader
*object
;
3163 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3164 device
, desc
, parent
, parent_ops
, shader
);
3166 if (!(object
= calloc(1, sizeof(*object
))))
3167 return E_OUTOFMEMORY
;
3169 if (FAILED(hr
= shader_init(object
, device
, desc
, parent
, parent_ops
)))
3171 WARN("Failed to initialize compute shader, hr %#lx.\n", hr
);
3176 if (FAILED(hr
= shader_set_function(object
, device
, WINED3D_SHADER_TYPE_COMPUTE
, 0)))
3178 shader_cleanup(object
);
3183 wined3d_cs_init_object(device
->cs
, wined3d_shader_init_object
, object
);
3185 TRACE("Created compute shader %p.\n", object
);
3191 HRESULT CDECL
wined3d_shader_create_ds(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3192 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3194 struct wined3d_shader
*object
;
3197 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3198 device
, desc
, parent
, parent_ops
, shader
);
3200 if (!(object
= calloc(1, sizeof(*object
))))
3201 return E_OUTOFMEMORY
;
3203 if (FAILED(hr
= shader_init(object
, device
, desc
, parent
, parent_ops
)))
3205 WARN("Failed to initialize domain shader, hr %#lx.\n", hr
);
3210 if (FAILED(hr
= shader_set_function(object
, device
, WINED3D_SHADER_TYPE_DOMAIN
, 0)))
3212 shader_cleanup(object
);
3217 wined3d_cs_init_object(device
->cs
, wined3d_shader_init_object
, object
);
3219 TRACE("Created domain shader %p.\n", object
);
3225 HRESULT CDECL
wined3d_shader_create_gs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3226 const struct wined3d_stream_output_desc
*so_desc
, void *parent
,
3227 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3229 struct wined3d_shader
*object
;
3232 TRACE("device %p, desc %p, so_desc %p, parent %p, parent_ops %p, shader %p.\n",
3233 device
, desc
, so_desc
, parent
, parent_ops
, shader
);
3235 if (!(object
= calloc(1, sizeof(*object
))))
3236 return E_OUTOFMEMORY
;
3238 if (FAILED(hr
= geometry_shader_init(object
, device
, desc
, so_desc
, parent
, parent_ops
)))
3240 WARN("Failed to initialize geometry shader, hr %#lx.\n", hr
);
3245 wined3d_cs_init_object(device
->cs
, wined3d_shader_init_object
, object
);
3247 TRACE("Created geometry shader %p.\n", object
);
3253 HRESULT CDECL
wined3d_shader_create_hs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3254 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3256 struct wined3d_shader
*object
;
3259 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3260 device
, desc
, parent
, parent_ops
, shader
);
3262 if (!(object
= calloc(1, sizeof(*object
))))
3263 return E_OUTOFMEMORY
;
3265 if (FAILED(hr
= shader_init(object
, device
, desc
, parent
, parent_ops
)))
3267 WARN("Failed to initialize hull shader, hr %#lx.\n", hr
);
3272 if (FAILED(hr
= shader_set_function(object
, device
, WINED3D_SHADER_TYPE_HULL
, 0)))
3274 shader_cleanup(object
);
3279 wined3d_cs_init_object(device
->cs
, wined3d_shader_init_object
, object
);
3281 TRACE("Created hull shader %p.\n", object
);
3287 HRESULT CDECL
wined3d_shader_create_ps(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3288 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3290 struct wined3d_shader
*object
;
3293 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3294 device
, desc
, parent
, parent_ops
, shader
);
3296 if (!(object
= calloc(1, sizeof(*object
))))
3297 return E_OUTOFMEMORY
;
3299 if (FAILED(hr
= pixel_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3301 WARN("Failed to initialize pixel shader, hr %#lx.\n", hr
);
3306 wined3d_cs_init_object(device
->cs
, wined3d_shader_init_object
, object
);
3308 TRACE("Created pixel shader %p.\n", object
);
3314 HRESULT CDECL
wined3d_shader_create_vs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3315 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3317 struct wined3d_shader
*object
;
3320 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3321 device
, desc
, parent
, parent_ops
, shader
);
3323 if (!(object
= calloc(1, sizeof(*object
))))
3324 return E_OUTOFMEMORY
;
3326 if (FAILED(hr
= vertex_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3328 WARN("Failed to initialize vertex shader, hr %#lx.\n", hr
);
3333 wined3d_cs_init_object(device
->cs
, wined3d_shader_init_object
, object
);
3335 TRACE("Created vertex shader %p.\n", object
);