2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2005 Oliver Stieber
6 * Copyright 2006 Ivan Gyurdiev
7 * Copyright 2007-2008, 2013 Stefan Dösinger for CodeWeavers
8 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/port.h"
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
36 /* pow, mul_high, sub_high, mul_low */
37 const float wined3d_srgb_const0
[] = {0.41666f
, 1.055f
, 0.055f
, 12.92f
};
39 const float wined3d_srgb_const1
[] = {0.0031308f
, 0.0f
, 0.0f
, 0.0f
};
41 static const char * const shader_opcode_names
[] =
43 /* WINED3DSIH_ABS */ "abs",
44 /* WINED3DSIH_ADD */ "add",
45 /* WINED3DSIH_AND */ "and",
46 /* WINED3DSIH_BEM */ "bem",
47 /* WINED3DSIH_BREAK */ "break",
48 /* WINED3DSIH_BREAKC */ "breakc",
49 /* WINED3DSIH_BREAKP */ "breakp",
50 /* WINED3DSIH_CALL */ "call",
51 /* WINED3DSIH_CALLNZ */ "callnz",
52 /* WINED3DSIH_CMP */ "cmp",
53 /* WINED3DSIH_CND */ "cnd",
54 /* WINED3DSIH_CRS */ "crs",
55 /* WINED3DSIH_CUT */ "cut",
56 /* WINED3DSIH_DCL */ "dcl",
57 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
58 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ "dcl_globalFlags",
59 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ "dcl_hs_fork_phase_instance_count",
60 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ "dcl_hs_max_tessfactor",
61 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ "dcl_immediateConstantBuffer",
62 /* WINED3DSIH_DCL_INPUT */ "dcl_input",
63 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ "dcl_input_control_point_count",
64 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
65 /* WINED3DSIH_DCL_INPUT_PS */ "dcl_input_ps",
66 /* WINED3DSIH_DCL_INPUT_PS_SGV */ "dcl_input_ps_sgv",
67 /* WINED3DSIH_DCL_INPUT_PS_SIV */ "dcl_input_ps_siv",
68 /* WINED3DSIH_DCL_INPUT_SGV */ "dcl_input_sgv",
69 /* WINED3DSIH_DCL_INPUT_SIV */ "dcl_input_siv",
70 /* WINED3DSIH_DCL_OUTPUT */ "dcl_output",
71 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ "dcl_output_control_point_count",
72 /* WINED3DSIH_DCL_OUTPUT_SIV */ "dcl_output_siv",
73 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
74 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ "dcl_resource_structured",
75 /* WINED3DSIH_DCL_SAMPLER */ "dcl_sampler",
76 /* WINED3DSIH_DCL_TEMPS */ "dcl_temps",
77 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ "dcl_tessellator_domain",
78 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ "dcl_tessellator_output_primitive",
79 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ "dcl_tessellator_partitioning",
80 /* WINED3DSIH_DCL_UAV_TYPED */ "dcl_uav_typed",
81 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
82 /* WINED3DSIH_DEF */ "def",
83 /* WINED3DSIH_DEFB */ "defb",
84 /* WINED3DSIH_DEFI */ "defi",
85 /* WINED3DSIH_DIV */ "div",
86 /* WINED3DSIH_DP2 */ "dp2",
87 /* WINED3DSIH_DP2ADD */ "dp2add",
88 /* WINED3DSIH_DP3 */ "dp3",
89 /* WINED3DSIH_DP4 */ "dp4",
90 /* WINED3DSIH_DST */ "dst",
91 /* WINED3DSIH_DSX */ "dsx",
92 /* WINED3DSIH_DSX_COARSE */ "deriv_rtx_coarse",
93 /* WINED3DSIH_DSX_FINE */ "deriv_rtx_fine",
94 /* WINED3DSIH_DSY */ "dsy",
95 /* WINED3DSIH_DSY_COARSE */ "deriv_rty_coarse",
96 /* WINED3DSIH_DSY_FINE */ "deriv_rty_fine",
97 /* WINED3DSIH_ELSE */ "else",
98 /* WINED3DSIH_EMIT */ "emit",
99 /* WINED3DSIH_ENDIF */ "endif",
100 /* WINED3DSIH_ENDLOOP */ "endloop",
101 /* WINED3DSIH_ENDREP */ "endrep",
102 /* WINED3DSIH_EQ */ "eq",
103 /* WINED3DSIH_EXP */ "exp",
104 /* WINED3DSIH_EXPP */ "expp",
105 /* WINED3DSIH_FRC */ "frc",
106 /* WINED3DSIH_FTOI */ "ftoi",
107 /* WINED3DSIH_FTOU */ "ftou",
108 /* WINED3DSIH_GE */ "ge",
109 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ "hs_control_point_phase",
110 /* WINED3DSIH_HS_DECLS */ "hs_decls",
111 /* WINED3DSIH_HS_FORK_PHASE */ "hs_fork_phase",
112 /* WINED3DSIH_HS_JOIN_PHASE */ "hs_join_phase",
113 /* WINED3DSIH_IADD */ "iadd",
114 /* WINED3DSIH_IEQ */ "ieq",
115 /* WINED3DSIH_IF */ "if",
116 /* WINED3DSIH_IFC */ "ifc",
117 /* WINED3DSIH_IGE */ "ige",
118 /* WINED3DSIH_ILT */ "ilt",
119 /* WINED3DSIH_IMAD */ "imad",
120 /* WINED3DSIH_IMAX */ "imax",
121 /* WINED3DSIH_IMIN */ "imin",
122 /* WINED3DSIH_IMUL */ "imul",
123 /* WINED3DSIH_INE */ "ine",
124 /* WINED3DSIH_INEG */ "ineg",
125 /* WINED3DSIH_ISHL */ "ishl",
126 /* WINED3DSIH_ITOF */ "itof",
127 /* WINED3DSIH_LABEL */ "label",
128 /* WINED3DSIH_LD */ "ld",
129 /* WINED3DSIH_LD2DMS */ "ld2dms",
130 /* WINED3DSIH_LD_STRUCTURED */ "ld_structured",
131 /* WINED3DSIH_LIT */ "lit",
132 /* WINED3DSIH_LOG */ "log",
133 /* WINED3DSIH_LOGP */ "logp",
134 /* WINED3DSIH_LOOP */ "loop",
135 /* WINED3DSIH_LRP */ "lrp",
136 /* WINED3DSIH_LT */ "lt",
137 /* WINED3DSIH_M3x2 */ "m3x2",
138 /* WINED3DSIH_M3x3 */ "m3x3",
139 /* WINED3DSIH_M3x4 */ "m3x4",
140 /* WINED3DSIH_M4x3 */ "m4x3",
141 /* WINED3DSIH_M4x4 */ "m4x4",
142 /* WINED3DSIH_MAD */ "mad",
143 /* WINED3DSIH_MAX */ "max",
144 /* WINED3DSIH_MIN */ "min",
145 /* WINED3DSIH_MOV */ "mov",
146 /* WINED3DSIH_MOVA */ "mova",
147 /* WINED3DSIH_MOVC */ "movc",
148 /* WINED3DSIH_MUL */ "mul",
149 /* WINED3DSIH_NE */ "ne",
150 /* WINED3DSIH_NOP */ "nop",
151 /* WINED3DSIH_NOT */ "not",
152 /* WINED3DSIH_NRM */ "nrm",
153 /* WINED3DSIH_OR */ "or",
154 /* WINED3DSIH_PHASE */ "phase",
155 /* WINED3DSIH_POW */ "pow",
156 /* WINED3DSIH_RCP */ "rcp",
157 /* WINED3DSIH_REP */ "rep",
158 /* WINED3DSIH_RESINFO */ "resinfo",
159 /* WINED3DSIH_RET */ "ret",
160 /* WINED3DSIH_ROUND_NE */ "round_ne",
161 /* WINED3DSIH_ROUND_NI */ "round_ni",
162 /* WINED3DSIH_ROUND_PI */ "round_pi",
163 /* WINED3DSIH_ROUND_Z */ "round_z",
164 /* WINED3DSIH_RSQ */ "rsq",
165 /* WINED3DSIH_SAMPLE */ "sample",
166 /* WINED3DSIH_SAMPLE_B */ "sample_b",
167 /* WINED3DSIH_SAMPLE_C */ "sample_c",
168 /* WINED3DSIH_SAMPLE_C_LZ */ "sample_c_lz",
169 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
170 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
171 /* WINED3DSIH_SETP */ "setp",
172 /* WINED3DSIH_SGE */ "sge",
173 /* WINED3DSIH_SGN */ "sgn",
174 /* WINED3DSIH_SINCOS */ "sincos",
175 /* WINED3DSIH_SLT */ "slt",
176 /* WINED3DSIH_SQRT */ "sqrt",
177 /* WINED3DSIH_STORE_UAV_TYPED */ "store_uav_typed",
178 /* WINED3DSIH_SUB */ "sub",
179 /* WINED3DSIH_TEX */ "texld",
180 /* WINED3DSIH_TEXBEM */ "texbem",
181 /* WINED3DSIH_TEXBEML */ "texbeml",
182 /* WINED3DSIH_TEXCOORD */ "texcrd",
183 /* WINED3DSIH_TEXDEPTH */ "texdepth",
184 /* WINED3DSIH_TEXDP3 */ "texdp3",
185 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
186 /* WINED3DSIH_TEXKILL */ "texkill",
187 /* WINED3DSIH_TEXLDD */ "texldd",
188 /* WINED3DSIH_TEXLDL */ "texldl",
189 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
190 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
191 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
192 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
193 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
194 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
195 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
196 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
197 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
198 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
199 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
200 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
201 /* WINED3DSIH_UDIV */ "udiv",
202 /* WINED3DSIH_UGE */ "uge",
203 /* WINED3DSIH_ULT */ "ult",
204 /* WINED3DSIH_USHR */ "ushr",
205 /* WINED3DSIH_UTOF */ "utof",
206 /* WINED3DSIH_XOR */ "xor",
209 static const char * const semantic_names
[] =
211 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
212 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
213 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
214 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
215 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
216 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
217 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
218 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
219 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
220 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
221 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
222 /* WINED3D_DECL_USAGE_FOG */ "FOG",
223 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
224 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
229 enum wined3d_sysval_semantic sysval_semantic
;
230 const char *sysval_name
;
232 sysval_semantic_names
[] =
234 {WINED3D_SV_POSITION
, "SV_Position"},
235 {WINED3D_SV_VERTEX_ID
, "SV_VertexID"},
236 {WINED3D_SV_INSTANCE_ID
, "SV_InstanceID"},
237 {WINED3D_SV_PRIMITIVE_ID
, "SV_PrimitiveID"},
238 {WINED3D_SV_IS_FRONT_FACE
, "SV_IsFrontFace"},
239 {WINED3D_SV_SAMPLE_INDEX
, "SV_SampleIndex"},
240 {WINED3D_SV_QUAD_U0_TESS_FACTOR
, "finalQuadUeq0EdgeTessFactor"},
241 {WINED3D_SV_QUAD_V0_TESS_FACTOR
, "finalQuadVeq0EdgeTessFactor"},
242 {WINED3D_SV_QUAD_U1_TESS_FACTOR
, "finalQuadUeq1EdgeTessFactor"},
243 {WINED3D_SV_QUAD_V1_TESS_FACTOR
, "finalQuadVeq1EdgeTessFactor"},
244 {WINED3D_SV_QUAD_U_INNER_TESS_FACTOR
, "finalQuadUInsideTessFactor"},
245 {WINED3D_SV_QUAD_V_INNER_TESS_FACTOR
, "finalQuadVInsideTessFactor"},
246 {WINED3D_SV_TRIANGLE_U_TESS_FACTOR
, "finalTriUeq0EdgeTessFactor"},
247 {WINED3D_SV_TRIANGLE_V_TESS_FACTOR
, "finalTriVeq0EdgeTessFactor"},
248 {WINED3D_SV_TRIANGLE_W_TESS_FACTOR
, "finalTriWeq0EdgeTessFactor"},
249 {WINED3D_SV_TRIANGLE_INNER_TESS_FACTOR
, "finalTriInsideTessFactor"},
250 {WINED3D_SV_LINE_DETAIL_TESS_FACTOR
, "finalLineDetailTessFactor"},
251 {WINED3D_SV_LINE_DENSITY_TESS_FACTOR
, "finalLineDensityTessFactor"},
254 static void shader_dump_src_param(struct wined3d_string_buffer
*buffer
,
255 const struct wined3d_shader_src_param
*param
, const struct wined3d_shader_version
*shader_version
);
257 const char *debug_d3dshaderinstructionhandler(enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx
)
259 if (handler_idx
>= ARRAY_SIZE(shader_opcode_names
))
260 return wine_dbg_sprintf("UNRECOGNIZED(%#x)", handler_idx
);
262 return shader_opcode_names
[handler_idx
];
265 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage
)
267 if (usage
>= ARRAY_SIZE(semantic_names
))
269 FIXME("Unrecognized usage %#x.\n", usage
);
270 return "UNRECOGNIZED";
273 return semantic_names
[usage
];
276 static enum wined3d_decl_usage
shader_usage_from_semantic_name(const char *name
)
280 for (i
= 0; i
< ARRAY_SIZE(semantic_names
); ++i
)
282 if (!strcmp(name
, semantic_names
[i
]))
289 static enum wined3d_sysval_semantic
shader_sysval_semantic_from_usage(enum wined3d_decl_usage usage
)
293 case WINED3D_DECL_USAGE_POSITION
:
294 return WINED3D_SV_POSITION
;
300 BOOL
shader_match_semantic(const char *semantic_name
, enum wined3d_decl_usage usage
)
302 return !strcmp(semantic_name
, shader_semantic_name_from_usage(usage
));
305 static void shader_signature_from_semantic(struct wined3d_shader_signature_element
*e
,
306 const struct wined3d_shader_semantic
*s
)
308 e
->semantic_name
= shader_semantic_name_from_usage(s
->usage
);
309 e
->semantic_idx
= s
->usage_idx
;
310 e
->sysval_semantic
= shader_sysval_semantic_from_usage(s
->usage
);
311 e
->component_type
= 0;
312 e
->register_idx
= s
->reg
.reg
.idx
[0].offset
;
313 e
->mask
= s
->reg
.write_mask
;
316 static void shader_signature_from_usage(struct wined3d_shader_signature_element
*e
,
317 enum wined3d_decl_usage usage
, UINT usage_idx
, UINT reg_idx
, DWORD write_mask
)
319 e
->semantic_name
= shader_semantic_name_from_usage(usage
);
320 e
->semantic_idx
= usage_idx
;
321 e
->sysval_semantic
= shader_sysval_semantic_from_usage(usage
);
322 e
->component_type
= 0;
323 e
->register_idx
= reg_idx
;
324 e
->mask
= write_mask
;
327 static const struct wined3d_shader_frontend
*shader_select_frontend(DWORD version_token
)
329 switch (version_token
>> 16)
333 return &sm1_shader_frontend
;
340 return &sm4_shader_frontend
;
343 FIXME("Unrecognised version token %#x.\n", version_token
);
348 void string_buffer_clear(struct wined3d_string_buffer
*buffer
)
350 buffer
->buffer
[0] = '\0';
351 buffer
->content_size
= 0;
354 BOOL
string_buffer_init(struct wined3d_string_buffer
*buffer
)
356 buffer
->buffer_size
= 32;
357 if (!(buffer
->buffer
= HeapAlloc(GetProcessHeap(), 0, buffer
->buffer_size
)))
359 ERR("Failed to allocate shader buffer memory.\n");
363 string_buffer_clear(buffer
);
367 void string_buffer_free(struct wined3d_string_buffer
*buffer
)
369 HeapFree(GetProcessHeap(), 0, buffer
->buffer
);
372 BOOL
string_buffer_resize(struct wined3d_string_buffer
*buffer
, int rc
)
375 unsigned int new_buffer_size
= buffer
->buffer_size
* 2;
377 while (rc
> 0 && (unsigned int)rc
>= new_buffer_size
- buffer
->content_size
)
378 new_buffer_size
*= 2;
379 if (!(new_buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
->buffer
, new_buffer_size
)))
381 ERR("Failed to grow buffer.\n");
382 buffer
->buffer
[buffer
->content_size
] = '\0';
385 buffer
->buffer
= new_buffer
;
386 buffer
->buffer_size
= new_buffer_size
;
390 int shader_vaddline(struct wined3d_string_buffer
*buffer
, const char *format
, va_list args
)
395 rem
= buffer
->buffer_size
- buffer
->content_size
;
396 rc
= vsnprintf(&buffer
->buffer
[buffer
->content_size
], rem
, format
, args
);
397 if (rc
< 0 /* C89 */ || (unsigned int)rc
>= rem
/* C99 */)
400 buffer
->content_size
+= rc
;
404 int shader_addline(struct wined3d_string_buffer
*buffer
, const char *format
, ...)
411 va_start(args
, format
);
412 ret
= shader_vaddline(buffer
, format
, args
);
416 if (!string_buffer_resize(buffer
, ret
))
421 struct wined3d_string_buffer
*string_buffer_get(struct wined3d_string_buffer_list
*list
)
423 struct wined3d_string_buffer
*buffer
;
425 if (list_empty(&list
->list
))
427 buffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer
));
428 if (!buffer
|| !string_buffer_init(buffer
))
430 ERR("Couldn't allocate buffer for temporary string.\n");
432 HeapFree(GetProcessHeap(), 0, buffer
);
438 buffer
= LIST_ENTRY(list_head(&list
->list
), struct wined3d_string_buffer
, entry
);
439 list_remove(&buffer
->entry
);
441 string_buffer_clear(buffer
);
445 static int string_buffer_vsprintf(struct wined3d_string_buffer
*buffer
, const char *format
, va_list args
)
449 string_buffer_clear(buffer
);
450 return shader_vaddline(buffer
, format
, args
);
453 void string_buffer_sprintf(struct wined3d_string_buffer
*buffer
, const char *format
, ...)
460 va_start(args
, format
);
461 ret
= string_buffer_vsprintf(buffer
, format
, args
);
465 if (!string_buffer_resize(buffer
, ret
))
470 void string_buffer_release(struct wined3d_string_buffer_list
*list
, struct wined3d_string_buffer
*buffer
)
474 list_add_head(&list
->list
, &buffer
->entry
);
477 void string_buffer_list_init(struct wined3d_string_buffer_list
*list
)
479 list_init(&list
->list
);
482 void string_buffer_list_cleanup(struct wined3d_string_buffer_list
*list
)
484 struct wined3d_string_buffer
*buffer
, *buffer_next
;
486 LIST_FOR_EACH_ENTRY_SAFE(buffer
, buffer_next
, &list
->list
, struct wined3d_string_buffer
, entry
)
488 string_buffer_free(buffer
);
489 HeapFree(GetProcessHeap(), 0, buffer
);
491 list_init(&list
->list
);
494 /* Convert floating point offset relative to a register file to an absolute
495 * offset for float constants. */
496 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type
, UINT register_idx
)
498 switch (register_type
)
500 case WINED3DSPR_CONST
: return register_idx
;
501 case WINED3DSPR_CONST2
: return 2048 + register_idx
;
502 case WINED3DSPR_CONST3
: return 4096 + register_idx
;
503 case WINED3DSPR_CONST4
: return 6144 + register_idx
;
505 FIXME("Unsupported register type: %u.\n", register_type
);
510 static void shader_delete_constant_list(struct list
*clist
)
512 struct wined3d_shader_lconst
*constant
, *constant_next
;
514 LIST_FOR_EACH_ENTRY_SAFE(constant
, constant_next
, clist
, struct wined3d_shader_lconst
, entry
)
515 HeapFree(GetProcessHeap(), 0, constant
);
519 static void shader_set_limits(struct wined3d_shader
*shader
)
521 static const struct limits_entry
523 unsigned int min_version
;
524 unsigned int max_version
;
525 struct wined3d_shader_limits limits
;
529 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
530 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
531 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
532 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
533 * even though they are capable of supporting much more (GL
534 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
535 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
537 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
538 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
539 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 0}},
544 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
545 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
549 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packet_input */
550 {WINED3D_SHADER_VERSION(5, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
554 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
555 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
556 {WINED3D_SHADER_VERSION(4, 1), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 32, 32}},
561 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
562 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
563 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
564 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
565 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
566 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 12}},
567 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(5, 0), {16, 0, 0, 0, 0, 32}},
570 const struct limits_entry
*limits_array
;
571 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
572 shader
->reg_maps
.shader_version
.minor
);
575 switch (shader
->reg_maps
.shader_version
.type
)
578 FIXME("Unexpected shader type %u found.\n", shader
->reg_maps
.shader_version
.type
);
580 case WINED3D_SHADER_TYPE_VERTEX
:
581 limits_array
= vs_limits
;
583 case WINED3D_SHADER_TYPE_HULL
:
584 limits_array
= hs_limits
;
586 case WINED3D_SHADER_TYPE_DOMAIN
:
587 limits_array
= ds_limits
;
589 case WINED3D_SHADER_TYPE_GEOMETRY
:
590 limits_array
= gs_limits
;
592 case WINED3D_SHADER_TYPE_PIXEL
:
593 limits_array
= ps_limits
;
597 while (limits_array
[i
].min_version
&& limits_array
[i
].min_version
<= shader_version
)
599 if (shader_version
<= limits_array
[i
].max_version
)
601 shader
->limits
= &limits_array
[i
].limits
;
608 FIXME("Unexpected shader version \"%u.%u\".\n",
609 shader
->reg_maps
.shader_version
.major
,
610 shader
->reg_maps
.shader_version
.minor
);
611 shader
->limits
= &limits_array
[max(0, i
- 1)].limits
;
615 static inline void set_bitmap_bit(DWORD
*bitmap
, DWORD bit
)
620 bitmap
[idx
] |= (1u << shift
);
623 static BOOL
shader_record_register_usage(struct wined3d_shader
*shader
, struct wined3d_shader_reg_maps
*reg_maps
,
624 const struct wined3d_shader_register
*reg
, enum wined3d_shader_type shader_type
, unsigned int constf_size
)
628 case WINED3DSPR_TEXTURE
: /* WINED3DSPR_ADDR */
629 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
630 reg_maps
->texcoord
|= 1u << reg
->idx
[0].offset
;
632 reg_maps
->address
|= 1u << reg
->idx
[0].offset
;
635 case WINED3DSPR_TEMP
:
636 reg_maps
->temporary
|= 1u << reg
->idx
[0].offset
;
639 case WINED3DSPR_INPUT
:
640 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
642 if (reg
->idx
[0].rel_addr
)
644 /* If relative addressing is used, we must assume that all registers
645 * are used. Even if it is a construct like v3[aL], we can't assume
646 * that v0, v1 and v2 aren't read because aL can be negative */
648 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
650 shader
->u
.ps
.input_reg_used
[i
] = TRUE
;
655 shader
->u
.ps
.input_reg_used
[reg
->idx
[0].offset
] = TRUE
;
659 reg_maps
->input_registers
|= 1u << reg
->idx
[0].offset
;
662 case WINED3DSPR_RASTOUT
:
663 if (reg
->idx
[0].offset
== 1)
665 if (reg
->idx
[0].offset
== 2)
666 reg_maps
->point_size
= 1;
669 case WINED3DSPR_MISCTYPE
:
670 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
672 if (!reg
->idx
[0].offset
)
674 else if (reg
->idx
[0].offset
== 1)
675 reg_maps
->usesfacing
= 1;
679 case WINED3DSPR_CONST
:
680 if (reg
->idx
[0].rel_addr
)
682 if (reg
->idx
[0].offset
< reg_maps
->min_rel_offset
)
683 reg_maps
->min_rel_offset
= reg
->idx
[0].offset
;
684 if (reg
->idx
[0].offset
> reg_maps
->max_rel_offset
)
685 reg_maps
->max_rel_offset
= reg
->idx
[0].offset
;
686 reg_maps
->usesrelconstF
= TRUE
;
690 if (reg
->idx
[0].offset
>= min(shader
->limits
->constant_float
, constf_size
))
692 WARN("Shader using float constant %u which is not supported.\n", reg
->idx
[0].offset
);
697 set_bitmap_bit(reg_maps
->constf
, reg
->idx
[0].offset
);
702 case WINED3DSPR_CONSTINT
:
703 if (reg
->idx
[0].offset
>= shader
->limits
->constant_int
)
705 WARN("Shader using integer constant %u which is not supported.\n", reg
->idx
[0].offset
);
710 reg_maps
->integer_constants
|= (1u << reg
->idx
[0].offset
);
714 case WINED3DSPR_CONSTBOOL
:
715 if (reg
->idx
[0].offset
>= shader
->limits
->constant_bool
)
717 WARN("Shader using bool constant %u which is not supported.\n", reg
->idx
[0].offset
);
722 reg_maps
->boolean_constants
|= (1u << reg
->idx
[0].offset
);
726 case WINED3DSPR_COLOROUT
:
727 reg_maps
->rt_mask
|= (1u << reg
->idx
[0].offset
);
731 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
732 reg
->type
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
738 static void shader_record_sample(struct wined3d_shader_reg_maps
*reg_maps
,
739 unsigned int resource_idx
, unsigned int sampler_idx
, unsigned int bind_idx
)
741 struct wined3d_shader_sampler_map_entry
*entries
, *entry
;
742 struct wined3d_shader_sampler_map
*map
;
745 map
= ®_maps
->sampler_map
;
746 entries
= map
->entries
;
747 for (i
= 0; i
< map
->count
; ++i
)
749 if (entries
[i
].resource_idx
== resource_idx
&& entries
[i
].sampler_idx
== sampler_idx
)
755 if (!(entries
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entries
) * 4)))
757 ERR("Failed to allocate sampler map entries.\n");
761 map
->entries
= entries
;
763 else if (map
->count
== map
->size
)
765 size_t new_size
= map
->size
* 2;
767 if (sizeof(*entries
) * new_size
<= sizeof(*entries
) * map
->size
768 || !(entries
= HeapReAlloc(GetProcessHeap(), 0, entries
, sizeof(*entries
) * new_size
)))
770 ERR("Failed to resize sampler map entries.\n");
773 map
->size
= new_size
;
774 map
->entries
= entries
;
777 entry
= &entries
[map
->count
++];
778 entry
->resource_idx
= resource_idx
;
779 entry
->sampler_idx
= sampler_idx
;
780 entry
->bind_idx
= bind_idx
;
783 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr
, unsigned int param
)
787 case WINED3DSIH_M4x4
:
788 case WINED3DSIH_M3x4
:
789 return param
== 1 ? 3 : 0;
791 case WINED3DSIH_M4x3
:
792 case WINED3DSIH_M3x3
:
793 return param
== 1 ? 2 : 0;
795 case WINED3DSIH_M3x2
:
796 return param
== 1 ? 1 : 0;
803 /* Note that this does not count the loop register as an address register. */
804 static HRESULT
shader_get_registers_used(struct wined3d_shader
*shader
, const struct wined3d_shader_frontend
*fe
,
805 struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_shader_signature
*input_signature
,
806 struct wined3d_shader_signature
*output_signature
, const DWORD
*byte_code
, DWORD constf_size
)
808 struct wined3d_shader_signature_element input_signature_elements
[max(MAX_ATTRIBS
, MAX_REG_INPUT
)];
809 struct wined3d_shader_signature_element output_signature_elements
[MAX_REG_OUTPUT
];
810 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
811 void *fe_data
= shader
->frontend_data
;
812 struct wined3d_shader_version shader_version
;
813 const DWORD
*ptr
= byte_code
;
816 memset(reg_maps
, 0, sizeof(*reg_maps
));
817 memset(input_signature_elements
, 0, sizeof(input_signature_elements
));
818 memset(output_signature_elements
, 0, sizeof(output_signature_elements
));
819 reg_maps
->min_rel_offset
= ~0U;
821 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
822 reg_maps
->shader_version
= shader_version
;
824 shader_set_limits(shader
);
826 reg_maps
->constf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
827 sizeof(*reg_maps
->constf
) * ((min(shader
->limits
->constant_float
, constf_size
) + 31) / 32));
828 if (!reg_maps
->constf
)
830 ERR("Failed to allocate constant map memory.\n");
831 return E_OUTOFMEMORY
;
834 while (!fe
->shader_is_end(fe_data
, &ptr
))
836 struct wined3d_shader_instruction ins
;
839 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
841 /* Unhandled opcode, and its parameters. */
842 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
844 TRACE("Skipping unrecognized instruction.\n");
848 /* Handle declarations. */
849 if (ins
.handler_idx
== WINED3DSIH_DCL
)
851 struct wined3d_shader_semantic
*semantic
= &ins
.declaration
.semantic
;
852 unsigned int reg_idx
= semantic
->reg
.reg
.idx
[0].offset
;
854 switch (semantic
->reg
.reg
.type
)
856 /* Mark input registers used. */
857 case WINED3DSPR_INPUT
:
858 if (reg_idx
>= MAX_REG_INPUT
)
860 ERR("Invalid input register index %u.\n", reg_idx
);
863 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
&& shader_version
.major
== 3
864 && semantic
->usage
== WINED3D_DECL_USAGE_POSITION
&& !semantic
->usage_idx
)
865 return WINED3DERR_INVALIDCALL
;
866 reg_maps
->input_registers
|= 1u << reg_idx
;
867 shader_signature_from_semantic(&input_signature_elements
[reg_idx
], semantic
);
870 /* Vertex shader: mark 3.0 output registers used, save token. */
871 case WINED3DSPR_OUTPUT
:
872 if (reg_idx
>= MAX_REG_OUTPUT
)
874 ERR("Invalid output register index %u.\n", reg_idx
);
877 reg_maps
->output_registers
|= 1u << reg_idx
;
878 shader_signature_from_semantic(&output_signature_elements
[reg_idx
], semantic
);
879 if (semantic
->usage
== WINED3D_DECL_USAGE_FOG
)
881 if (semantic
->usage
== WINED3D_DECL_USAGE_PSIZE
)
882 reg_maps
->point_size
= 1;
885 case WINED3DSPR_SAMPLER
:
886 shader_record_sample(reg_maps
, reg_idx
, reg_idx
, reg_idx
);
887 case WINED3DSPR_RESOURCE
:
888 if (reg_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
890 ERR("Invalid resource index %u.\n", reg_idx
);
893 reg_maps
->resource_info
[reg_idx
].type
= semantic
->resource_type
;
894 reg_maps
->resource_info
[reg_idx
].data_type
= semantic
->resource_data_type
;
898 TRACE("Not recording DCL register type %#x.\n", semantic
->reg
.reg
.type
);
902 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
904 struct wined3d_shader_register
*reg
= &ins
.declaration
.src
.reg
;
905 if (reg
->idx
[0].offset
>= WINED3D_MAX_CBS
)
906 ERR("Invalid CB index %u.\n", reg
->idx
[0].offset
);
908 reg_maps
->cb_sizes
[reg
->idx
[0].offset
] = reg
->idx
[1].offset
;
910 else if (ins
.handler_idx
== WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER
)
913 FIXME("Multiple immediate constant buffers.\n");
914 reg_maps
->icb
= ins
.declaration
.icb
;
916 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
)
918 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
919 shader
->u
.gs
.input_type
= ins
.declaration
.primitive_type
;
921 FIXME("Invalid instruction %#x for shader type %#x.\n",
922 ins
.handler_idx
, shader_version
.type
);
924 else if (ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
926 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
927 shader
->u
.gs
.output_type
= ins
.declaration
.primitive_type
;
929 FIXME("Invalid instruction %#x for shader type %#x.\n",
930 ins
.handler_idx
, shader_version
.type
);
932 else if (ins
.handler_idx
== WINED3DSIH_DCL_SAMPLER
)
934 if (ins
.flags
& WINED3DSI_SAMPLER_COMPARISON_MODE
)
935 reg_maps
->sampler_comparison_mode
|= (1u << ins
.declaration
.dst
.reg
.idx
[0].offset
);
937 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
939 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
940 shader
->u
.gs
.vertices_out
= ins
.declaration
.count
;
942 FIXME("Invalid instruction %#x for shader type %#x.\n",
943 ins
.handler_idx
, shader_version
.type
);
945 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
947 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
949 if (!lconst
) return E_OUTOFMEMORY
;
951 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
952 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
953 value
= (float *)lconst
->value
;
955 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
956 if (shader_version
.major
== 1 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
958 if (value
[0] < -1.0f
) value
[0] = -1.0f
;
959 else if (value
[0] > 1.0f
) value
[0] = 1.0f
;
960 if (value
[1] < -1.0f
) value
[1] = -1.0f
;
961 else if (value
[1] > 1.0f
) value
[1] = 1.0f
;
962 if (value
[2] < -1.0f
) value
[2] = -1.0f
;
963 else if (value
[2] > 1.0f
) value
[2] = 1.0f
;
964 if (value
[3] < -1.0f
) value
[3] = -1.0f
;
965 else if (value
[3] > 1.0f
) value
[3] = 1.0f
;
968 list_add_head(&shader
->constantsF
, &lconst
->entry
);
970 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
971 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
973 shader
->lconst_inf_or_nan
= TRUE
;
976 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
978 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
979 if (!lconst
) return E_OUTOFMEMORY
;
981 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
982 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
984 list_add_head(&shader
->constantsI
, &lconst
->entry
);
985 reg_maps
->local_int_consts
|= (1u << lconst
->idx
);
987 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
989 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
990 if (!lconst
) return E_OUTOFMEMORY
;
992 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
993 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, sizeof(DWORD
));
995 list_add_head(&shader
->constantsB
, &lconst
->entry
);
996 reg_maps
->local_bool_consts
|= (1u << lconst
->idx
);
998 /* For subroutine prototypes. */
999 else if (ins
.handler_idx
== WINED3DSIH_LABEL
)
1001 reg_maps
->labels
|= 1u << ins
.src
[0].reg
.idx
[0].offset
;
1003 /* Set texture, address, temporary registers. */
1006 BOOL color0_mov
= FALSE
;
1009 /* This will loop over all the registers and try to
1010 * make a bitmask of the ones we're interested in.
1012 * Relative addressing tokens are ignored, but that's
1013 * okay, since we'll catch any address registers when
1014 * they are initialized (required by spec). */
1015 for (i
= 0; i
< ins
.dst_count
; ++i
)
1017 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.dst
[i
].reg
,
1018 shader_version
.type
, constf_size
))
1019 return WINED3DERR_INVALIDCALL
;
1021 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
)
1023 UINT idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
1025 switch (ins
.dst
[i
].reg
.type
)
1027 case WINED3DSPR_RASTOUT
:
1028 if (shader_version
.major
>= 3)
1033 reg_maps
->output_registers
|= 1u << 10;
1034 shader_signature_from_usage(&output_signature_elements
[10],
1035 WINED3D_DECL_USAGE_POSITION
, 0, 10, WINED3DSP_WRITEMASK_ALL
);
1039 reg_maps
->output_registers
|= 1u << 11;
1040 shader_signature_from_usage(&output_signature_elements
[11],
1041 WINED3D_DECL_USAGE_FOG
, 0, 11, WINED3DSP_WRITEMASK_0
);
1045 reg_maps
->output_registers
|= 1u << 11;
1046 shader_signature_from_usage(&output_signature_elements
[11],
1047 WINED3D_DECL_USAGE_PSIZE
, 0, 11, WINED3DSP_WRITEMASK_1
);
1052 case WINED3DSPR_ATTROUT
:
1053 if (shader_version
.major
>= 3)
1058 if (reg_maps
->output_registers
& (1u << idx
))
1060 output_signature_elements
[idx
].mask
|= ins
.dst
[i
].write_mask
;
1064 reg_maps
->output_registers
|= 1u << idx
;
1065 shader_signature_from_usage(&output_signature_elements
[idx
],
1066 WINED3D_DECL_USAGE_COLOR
, idx
- 8, idx
, ins
.dst
[i
].write_mask
);
1071 case WINED3DSPR_TEXCRDOUT
:
1072 if (shader_version
.major
>= 3)
1074 reg_maps
->u
.output_registers_mask
[idx
] |= ins
.dst
[i
].write_mask
;
1077 reg_maps
->u
.texcoord_mask
[idx
] |= ins
.dst
[i
].write_mask
;
1078 if (reg_maps
->output_registers
& (1u << idx
))
1080 output_signature_elements
[idx
].mask
|= ins
.dst
[i
].write_mask
;
1084 reg_maps
->output_registers
|= 1u << idx
;
1085 shader_signature_from_usage(&output_signature_elements
[idx
],
1086 WINED3D_DECL_USAGE_TEXCOORD
, idx
, idx
, ins
.dst
[i
].write_mask
);
1095 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1097 if (ins
.dst
[i
].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
.dst
[i
].reg
.idx
[0].offset
)
1099 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
1100 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
1101 * the mov and perform the sRGB write correction from the source register.
1103 * However, if the mov is only partial, we can't do this, and if the write
1104 * comes from an instruction other than MOV it is hard to do as well. If
1105 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
1106 shader
->u
.ps
.color0_mov
= FALSE
;
1107 if (ins
.handler_idx
== WINED3DSIH_MOV
1108 && ins
.dst
[i
].write_mask
== WINED3DSP_WRITEMASK_ALL
)
1110 /* Used later when the source register is read. */
1114 /* Also drop the MOV marker if the source register is overwritten prior to the shader
1117 else if (ins
.dst
[i
].reg
.type
== WINED3DSPR_TEMP
1118 && ins
.dst
[i
].reg
.idx
[0].offset
== shader
->u
.ps
.color0_reg
)
1120 shader
->u
.ps
.color0_mov
= FALSE
;
1124 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
1125 if (shader_version
.major
== 1
1126 && (ins
.handler_idx
== WINED3DSIH_TEX
1127 || ins
.handler_idx
== WINED3DSIH_TEXBEM
1128 || ins
.handler_idx
== WINED3DSIH_TEXBEML
1129 || ins
.handler_idx
== WINED3DSIH_TEXDP3TEX
1130 || ins
.handler_idx
== WINED3DSIH_TEXM3x2TEX
1131 || ins
.handler_idx
== WINED3DSIH_TEXM3x3SPEC
1132 || ins
.handler_idx
== WINED3DSIH_TEXM3x3TEX
1133 || ins
.handler_idx
== WINED3DSIH_TEXM3x3VSPEC
1134 || ins
.handler_idx
== WINED3DSIH_TEXREG2AR
1135 || ins
.handler_idx
== WINED3DSIH_TEXREG2GB
1136 || ins
.handler_idx
== WINED3DSIH_TEXREG2RGB
))
1138 unsigned int reg_idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
1140 TRACE("Setting fake 2D resource for 1.x pixelshader.\n");
1141 reg_maps
->resource_info
[reg_idx
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
1142 reg_maps
->resource_info
[reg_idx
].data_type
= WINED3D_DATA_FLOAT
;
1143 shader_record_sample(reg_maps
, reg_idx
, reg_idx
, reg_idx
);
1145 /* texbem is only valid with < 1.4 pixel shaders */
1146 if (ins
.handler_idx
== WINED3DSIH_TEXBEM
1147 || ins
.handler_idx
== WINED3DSIH_TEXBEML
)
1149 reg_maps
->bumpmat
|= 1u << reg_idx
;
1150 if (ins
.handler_idx
== WINED3DSIH_TEXBEML
)
1152 reg_maps
->luminanceparams
|= 1u << reg_idx
;
1156 else if (ins
.handler_idx
== WINED3DSIH_BEM
)
1158 reg_maps
->bumpmat
|= 1u << ins
.dst
[i
].reg
.idx
[0].offset
;
1162 if (ins
.handler_idx
== WINED3DSIH_NRM
) reg_maps
->usesnrm
= 1;
1163 else if (ins
.handler_idx
== WINED3DSIH_DSY
) reg_maps
->usesdsy
= 1;
1164 else if (ins
.handler_idx
== WINED3DSIH_DSX
) reg_maps
->usesdsx
= 1;
1165 else if (ins
.handler_idx
== WINED3DSIH_TEXLDD
) reg_maps
->usestexldd
= 1;
1166 else if (ins
.handler_idx
== WINED3DSIH_TEXLDL
) reg_maps
->usestexldl
= 1;
1167 else if (ins
.handler_idx
== WINED3DSIH_MOVA
) reg_maps
->usesmova
= 1;
1168 else if (ins
.handler_idx
== WINED3DSIH_IFC
) reg_maps
->usesifc
= 1;
1169 else if (ins
.handler_idx
== WINED3DSIH_CALL
) reg_maps
->usescall
= 1;
1170 else if (ins
.handler_idx
== WINED3DSIH_POW
) reg_maps
->usespow
= 1;
1171 else if (ins
.handler_idx
== WINED3DSIH_LOOP
1172 || ins
.handler_idx
== WINED3DSIH_REP
)
1175 if (cur_loop_depth
> max_loop_depth
)
1176 max_loop_depth
= cur_loop_depth
;
1178 else if (ins
.handler_idx
== WINED3DSIH_ENDLOOP
1179 || ins
.handler_idx
== WINED3DSIH_ENDREP
)
1183 else if (ins
.handler_idx
== WINED3DSIH_SAMPLE
1184 || ins
.handler_idx
== WINED3DSIH_SAMPLE_B
1185 || ins
.handler_idx
== WINED3DSIH_SAMPLE_C_LZ
1186 || ins
.handler_idx
== WINED3DSIH_SAMPLE_GRAD
1187 || ins
.handler_idx
== WINED3DSIH_SAMPLE_LOD
)
1189 shader_record_sample(reg_maps
, ins
.src
[1].reg
.idx
[0].offset
,
1190 ins
.src
[2].reg
.idx
[0].offset
, reg_maps
->sampler_map
.count
);
1192 else if (ins
.handler_idx
== WINED3DSIH_LD
1193 || ins
.handler_idx
== WINED3DSIH_RESINFO
)
1195 shader_record_sample(reg_maps
, ins
.src
[1].reg
.idx
[0].offset
,
1196 WINED3D_SAMPLER_DEFAULT
, reg_maps
->sampler_map
.count
);
1200 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.predicate
->reg
,
1201 shader_version
.type
, constf_size
))
1202 return WINED3DERR_INVALIDCALL
;
1204 for (i
= 0; i
< ins
.src_count
; ++i
)
1206 unsigned int count
= get_instr_extra_regcount(ins
.handler_idx
, i
);
1207 struct wined3d_shader_register reg
= ins
.src
[i
].reg
;
1209 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.src
[i
].reg
,
1210 shader_version
.type
, constf_size
))
1211 return WINED3DERR_INVALIDCALL
;
1214 ++reg
.idx
[0].offset
;
1215 if (!shader_record_register_usage(shader
, reg_maps
, ®
,
1216 shader_version
.type
, constf_size
))
1217 return WINED3DERR_INVALIDCALL
;
1223 if (ins
.src
[i
].reg
.type
== WINED3DSPR_TEMP
1224 && ins
.src
[i
].swizzle
== WINED3DSP_NOSWIZZLE
)
1226 shader
->u
.ps
.color0_mov
= TRUE
;
1227 shader
->u
.ps
.color0_reg
= ins
.src
[i
].reg
.idx
[0].offset
;
1233 reg_maps
->loop_depth
= max_loop_depth
;
1235 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1236 * R0 is written to the render target. */
1237 if (shader_version
.major
< 2 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1238 reg_maps
->rt_mask
|= (1u << 0);
1240 shader
->functionLength
= ((const char *)ptr
- (const char *)byte_code
);
1242 if (input_signature
->elements
)
1244 for (i
= 0; i
< input_signature
->element_count
; ++i
)
1246 reg_maps
->input_registers
|= 1u << input_signature
->elements
[i
].register_idx
;
1247 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
1248 && input_signature
->elements
[i
].sysval_semantic
== WINED3D_SV_POSITION
)
1252 else if (!input_signature
->elements
&& reg_maps
->input_registers
)
1254 unsigned int count
= wined3d_popcount(reg_maps
->input_registers
);
1255 struct wined3d_shader_signature_element
*e
;
1258 if (!(input_signature
->elements
= HeapAlloc(GetProcessHeap(), 0, sizeof(*input_signature
->elements
) * count
)))
1259 return E_OUTOFMEMORY
;
1260 input_signature
->element_count
= count
;
1262 e
= input_signature
->elements
;
1263 for (i
= 0; i
< ARRAY_SIZE(input_signature_elements
); ++i
)
1265 if (!(reg_maps
->input_registers
& (1u << i
)))
1267 input_signature_elements
[i
].register_idx
= i
;
1268 *e
++ = input_signature_elements
[i
];
1272 if (output_signature
->elements
)
1274 for (i
= 0; i
< output_signature
->element_count
; ++i
)
1276 reg_maps
->output_registers
|= 1u << output_signature
->elements
[i
].register_idx
;
1279 else if (reg_maps
->output_registers
)
1281 unsigned int count
= wined3d_popcount(reg_maps
->output_registers
);
1282 struct wined3d_shader_signature_element
*e
;
1284 if (!(output_signature
->elements
= HeapAlloc(GetProcessHeap(), 0, sizeof(*output_signature
->elements
) * count
)))
1285 return E_OUTOFMEMORY
;
1286 output_signature
->element_count
= count
;
1288 e
= output_signature
->elements
;
1289 for (i
= 0; i
< ARRAY_SIZE(output_signature_elements
); ++i
)
1291 if (!(reg_maps
->output_registers
& (1u << i
)))
1293 *e
++ = output_signature_elements
[i
];
1300 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int max
)
1302 DWORD map
= 1u << max
;
1304 map
&= reg_maps
->shader_version
.major
< 3 ? ~reg_maps
->texcoord
: ~reg_maps
->input_registers
;
1306 return wined3d_log2i(map
);
1309 static void shader_dump_global_flags(struct wined3d_string_buffer
*buffer
, DWORD global_flags
)
1311 if (global_flags
& WINED3DSGF_REFACTORING_ALLOWED
)
1313 shader_addline(buffer
, "refactoringAllowed");
1314 global_flags
&= ~WINED3DSGF_REFACTORING_ALLOWED
;
1316 shader_addline(buffer
, " | ");
1319 if (global_flags
& WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS
)
1321 shader_addline(buffer
, "enableRawAndStructuredBuffers");
1322 global_flags
&= ~WINED3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS
;
1326 shader_addline(buffer
, "unknown_flags(%#x)", global_flags
);
1329 static void shader_dump_tessellator_domain(struct wined3d_string_buffer
*buffer
,
1330 enum wined3d_tessellator_domain domain
)
1334 case WINED3D_TESSELLATOR_DOMAIN_LINE
:
1335 shader_addline(buffer
, "line");
1337 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE
:
1338 shader_addline(buffer
, "triangle");
1340 case WINED3D_TESSELLATOR_DOMAIN_QUAD
:
1341 shader_addline(buffer
, "quad");
1344 shader_addline(buffer
, "unknown_tessellator_domain(%#x)", domain
);
1349 static void shader_dump_tessellator_output_primitive(struct wined3d_string_buffer
*buffer
,
1350 enum wined3d_tessellator_output_primitive output_primitive
)
1352 switch (output_primitive
)
1354 case WINED3D_TESSELLATOR_OUTPUT_POINT
:
1355 shader_addline(buffer
, "point");
1357 case WINED3D_TESSELLATOR_OUTPUT_LINE
:
1358 shader_addline(buffer
, "line");
1360 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW
:
1361 shader_addline(buffer
, "triangle_cw");
1363 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW
:
1364 shader_addline(buffer
, "triangle_ccw");
1367 shader_addline(buffer
, "unknown_tessellator_output_primitive(%#x)", output_primitive
);
1372 static void shader_dump_tessellator_partitioning(struct wined3d_string_buffer
*buffer
,
1373 enum wined3d_tessellator_partitioning partitioning
)
1375 switch (partitioning
)
1377 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER
:
1378 shader_addline(buffer
, "integer");
1380 case WINED3D_TESSELLATOR_PARTITIONING_POW2
:
1381 shader_addline(buffer
, "pow2");
1383 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD
:
1384 shader_addline(buffer
, "fractional_odd");
1386 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN
:
1387 shader_addline(buffer
, "fractional_even");
1390 shader_addline(buffer
, "unknown_tessellator_partitioning(%#x)", partitioning
);
1395 static void shader_dump_sysval_semantic(struct wined3d_string_buffer
*buffer
, enum wined3d_sysval_semantic semantic
)
1399 for (i
= 0; i
< ARRAY_SIZE(sysval_semantic_names
); ++i
)
1401 if (sysval_semantic_names
[i
].sysval_semantic
== semantic
)
1403 shader_addline(buffer
, "%s", sysval_semantic_names
[i
].sysval_name
);
1408 shader_addline(buffer
, "unknown_sysval_semantic(%#x)", semantic
);
1411 static void shader_dump_decl_usage(struct wined3d_string_buffer
*buffer
,
1412 const struct wined3d_shader_semantic
*semantic
, const struct wined3d_shader_version
*shader_version
)
1414 shader_addline(buffer
, "dcl");
1416 if (semantic
->reg
.reg
.type
== WINED3DSPR_SAMPLER
)
1418 switch (semantic
->resource_type
)
1420 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
1421 shader_addline(buffer
, "_2d");
1424 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
1425 shader_addline(buffer
, "_3d");
1428 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
1429 shader_addline(buffer
, "_cube");
1433 shader_addline(buffer
, "_unknown_resource_type(%#x)", semantic
->resource_type
);
1437 else if (semantic
->reg
.reg
.type
== WINED3DSPR_RESOURCE
|| semantic
->reg
.reg
.type
== WINED3DSPR_UAV
)
1439 if (semantic
->reg
.reg
.type
== WINED3DSPR_RESOURCE
)
1440 shader_addline(buffer
, "_resource_");
1442 shader_addline(buffer
, "_uav_");
1443 switch (semantic
->resource_type
)
1445 case WINED3D_SHADER_RESOURCE_BUFFER
:
1446 shader_addline(buffer
, "buffer");
1449 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
1450 shader_addline(buffer
, "texture1d");
1453 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
1454 shader_addline(buffer
, "texture2d");
1457 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
:
1458 shader_addline(buffer
, "texture2dms");
1461 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
1462 shader_addline(buffer
, "texture3d");
1465 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
1466 shader_addline(buffer
, "texturecube");
1469 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
:
1470 shader_addline(buffer
, "texture1darray");
1473 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
1474 shader_addline(buffer
, "texture2darray");
1477 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY
:
1478 shader_addline(buffer
, "texture2dmsarray");
1482 shader_addline(buffer
, "unknown");
1485 switch (semantic
->resource_data_type
)
1487 case WINED3D_DATA_FLOAT
:
1488 shader_addline(buffer
, " (float)");
1491 case WINED3D_DATA_INT
:
1492 shader_addline(buffer
, " (int)");
1495 case WINED3D_DATA_UINT
:
1496 shader_addline(buffer
, " (uint)");
1499 case WINED3D_DATA_UNORM
:
1500 shader_addline(buffer
, " (unorm)");
1503 case WINED3D_DATA_SNORM
:
1504 shader_addline(buffer
, " (snorm)");
1508 shader_addline(buffer
, " (unknown)");
1514 /* Pixel shaders 3.0 don't have usage semantics. */
1515 if (shader_version
->major
< 3 && shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
1518 shader_addline(buffer
, "_");
1520 switch (semantic
->usage
)
1522 case WINED3D_DECL_USAGE_POSITION
:
1523 shader_addline(buffer
, "position%u", semantic
->usage_idx
);
1526 case WINED3D_DECL_USAGE_BLEND_INDICES
:
1527 shader_addline(buffer
, "blend");
1530 case WINED3D_DECL_USAGE_BLEND_WEIGHT
:
1531 shader_addline(buffer
, "weight");
1534 case WINED3D_DECL_USAGE_NORMAL
:
1535 shader_addline(buffer
, "normal%u", semantic
->usage_idx
);
1538 case WINED3D_DECL_USAGE_PSIZE
:
1539 shader_addline(buffer
, "psize");
1542 case WINED3D_DECL_USAGE_COLOR
:
1543 if (!semantic
->usage_idx
)
1544 shader_addline(buffer
, "color");
1546 shader_addline(buffer
, "specular%u", (semantic
->usage_idx
- 1));
1549 case WINED3D_DECL_USAGE_TEXCOORD
:
1550 shader_addline(buffer
, "texture%u", semantic
->usage_idx
);
1553 case WINED3D_DECL_USAGE_TANGENT
:
1554 shader_addline(buffer
, "tangent");
1557 case WINED3D_DECL_USAGE_BINORMAL
:
1558 shader_addline(buffer
, "binormal");
1561 case WINED3D_DECL_USAGE_TESS_FACTOR
:
1562 shader_addline(buffer
, "tessfactor");
1565 case WINED3D_DECL_USAGE_POSITIONT
:
1566 shader_addline(buffer
, "positionT%u", semantic
->usage_idx
);
1569 case WINED3D_DECL_USAGE_FOG
:
1570 shader_addline(buffer
, "fog");
1573 case WINED3D_DECL_USAGE_DEPTH
:
1574 shader_addline(buffer
, "depth");
1577 case WINED3D_DECL_USAGE_SAMPLE
:
1578 shader_addline(buffer
, "sample");
1582 shader_addline(buffer
, "<unknown_semantic(%#x)>", semantic
->usage
);
1583 FIXME("unknown_semantics(0x%08x)", semantic
->usage
);
1588 static void shader_dump_register(struct wined3d_string_buffer
*buffer
,
1589 const struct wined3d_shader_register
*reg
, const struct wined3d_shader_version
*shader_version
)
1591 static const char * const rastout_reg_names
[] = {"oPos", "oFog", "oPts"};
1592 static const char * const misctype_reg_names
[] = {"vPos", "vFace"};
1593 UINT offset
= reg
->idx
[0].offset
;
1597 case WINED3DSPR_TEMP
:
1598 shader_addline(buffer
, "r");
1601 case WINED3DSPR_INPUT
:
1602 shader_addline(buffer
, "v");
1605 case WINED3DSPR_CONST
:
1606 case WINED3DSPR_CONST2
:
1607 case WINED3DSPR_CONST3
:
1608 case WINED3DSPR_CONST4
:
1609 shader_addline(buffer
, "c");
1610 offset
= shader_get_float_offset(reg
->type
, offset
);
1613 case WINED3DSPR_TEXTURE
: /* vs: case WINED3DSPR_ADDR */
1614 shader_addline(buffer
, "%c", shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
? 't' : 'a');
1617 case WINED3DSPR_RASTOUT
:
1618 shader_addline(buffer
, "%s", rastout_reg_names
[offset
]);
1621 case WINED3DSPR_COLOROUT
:
1622 shader_addline(buffer
, "oC");
1625 case WINED3DSPR_DEPTHOUT
:
1626 shader_addline(buffer
, "oDepth");
1629 case WINED3DSPR_ATTROUT
:
1630 shader_addline(buffer
, "oD");
1633 case WINED3DSPR_TEXCRDOUT
:
1634 /* Vertex shaders >= 3.0 use general purpose output registers
1635 * (WINED3DSPR_OUTPUT), which can include an address token. */
1636 if (shader_version
->major
>= 3)
1637 shader_addline(buffer
, "o");
1639 shader_addline(buffer
, "oT");
1642 case WINED3DSPR_CONSTINT
:
1643 shader_addline(buffer
, "i");
1646 case WINED3DSPR_CONSTBOOL
:
1647 shader_addline(buffer
, "b");
1650 case WINED3DSPR_LABEL
:
1651 shader_addline(buffer
, "l");
1654 case WINED3DSPR_LOOP
:
1655 shader_addline(buffer
, "aL");
1658 case WINED3DSPR_SAMPLER
:
1659 shader_addline(buffer
, "s");
1662 case WINED3DSPR_MISCTYPE
:
1665 FIXME("Unhandled misctype register %u.\n", offset
);
1666 shader_addline(buffer
, "<unhandled misctype %#x>", offset
);
1670 shader_addline(buffer
, "%s", misctype_reg_names
[offset
]);
1674 case WINED3DSPR_PREDICATE
:
1675 shader_addline(buffer
, "p");
1678 case WINED3DSPR_IMMCONST
:
1679 shader_addline(buffer
, "l");
1682 case WINED3DSPR_CONSTBUFFER
:
1683 shader_addline(buffer
, "cb");
1686 case WINED3DSPR_IMMCONSTBUFFER
:
1687 shader_addline(buffer
, "icb");
1690 case WINED3DSPR_PRIMID
:
1691 shader_addline(buffer
, "primID");
1694 case WINED3DSPR_NULL
:
1695 shader_addline(buffer
, "null");
1698 case WINED3DSPR_RESOURCE
:
1699 shader_addline(buffer
, "t");
1702 case WINED3DSPR_UAV
:
1703 shader_addline(buffer
, "u");
1706 case WINED3DSPR_OUTPOINTID
:
1707 shader_addline(buffer
, "vOutputControlPointID");
1710 case WINED3DSPR_FORKINSTID
:
1711 shader_addline(buffer
, "vForkInstanceId");
1714 case WINED3DSPR_INCONTROLPOINT
:
1715 shader_addline(buffer
, "vicp");
1718 case WINED3DSPR_PATCHCONST
:
1719 shader_addline(buffer
, "vpc");
1722 case WINED3DSPR_TESSCOORD
:
1723 shader_addline(buffer
, "vDomainLocation");
1727 shader_addline(buffer
, "<unhandled_rtype(%#x)>", reg
->type
);
1731 if (reg
->type
== WINED3DSPR_IMMCONST
)
1733 shader_addline(buffer
, "(");
1734 switch (reg
->immconst_type
)
1736 case WINED3D_IMMCONST_SCALAR
:
1737 switch (reg
->data_type
)
1739 case WINED3D_DATA_FLOAT
:
1740 shader_addline(buffer
, "%.8e", *(const float *)reg
->immconst_data
);
1742 case WINED3D_DATA_INT
:
1743 shader_addline(buffer
, "%d", reg
->immconst_data
[0]);
1745 case WINED3D_DATA_RESOURCE
:
1746 case WINED3D_DATA_SAMPLER
:
1747 case WINED3D_DATA_UINT
:
1748 shader_addline(buffer
, "%u", reg
->immconst_data
[0]);
1751 shader_addline(buffer
, "<unhandled data type %#x>", reg
->data_type
);
1756 case WINED3D_IMMCONST_VEC4
:
1757 switch (reg
->data_type
)
1759 case WINED3D_DATA_FLOAT
:
1760 shader_addline(buffer
, "%.8e, %.8e, %.8e, %.8e",
1761 *(const float *)®
->immconst_data
[0], *(const float *)®
->immconst_data
[1],
1762 *(const float *)®
->immconst_data
[2], *(const float *)®
->immconst_data
[3]);
1764 case WINED3D_DATA_INT
:
1765 shader_addline(buffer
, "%d, %d, %d, %d",
1766 reg
->immconst_data
[0], reg
->immconst_data
[1],
1767 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1769 case WINED3D_DATA_RESOURCE
:
1770 case WINED3D_DATA_SAMPLER
:
1771 case WINED3D_DATA_UINT
:
1772 shader_addline(buffer
, "%u, %u, %u, %u",
1773 reg
->immconst_data
[0], reg
->immconst_data
[1],
1774 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1777 shader_addline(buffer
, "<unhandled data type %#x>", reg
->data_type
);
1783 shader_addline(buffer
, "<unhandled immconst_type %#x>", reg
->immconst_type
);
1786 shader_addline(buffer
, ")");
1788 else if (reg
->type
!= WINED3DSPR_RASTOUT
1789 && reg
->type
!= WINED3DSPR_MISCTYPE
1790 && reg
->type
!= WINED3DSPR_NULL
)
1794 shader_addline(buffer
, "[");
1795 if (reg
->idx
[0].rel_addr
)
1797 shader_dump_src_param(buffer
, reg
->idx
[0].rel_addr
, shader_version
);
1798 shader_addline(buffer
, " + ");
1800 shader_addline(buffer
, "%u]", offset
);
1802 if (reg
->idx
[1].offset
!= ~0u)
1804 shader_addline(buffer
, "[");
1805 if (reg
->idx
[1].rel_addr
)
1807 shader_dump_src_param(buffer
, reg
->idx
[1].rel_addr
, shader_version
);
1808 shader_addline(buffer
, " + ");
1810 shader_addline(buffer
, "%u]", reg
->idx
[1].offset
);
1816 static void shader_dump_dst_param(struct wined3d_string_buffer
*buffer
,
1817 const struct wined3d_shader_dst_param
*param
, const struct wined3d_shader_version
*shader_version
)
1819 DWORD write_mask
= param
->write_mask
;
1821 shader_dump_register(buffer
, ¶m
->reg
, shader_version
);
1823 if (write_mask
&& write_mask
!= WINED3DSP_WRITEMASK_ALL
)
1825 static const char write_mask_chars
[] = "xyzw";
1827 shader_addline(buffer
, ".");
1828 if (write_mask
& WINED3DSP_WRITEMASK_0
)
1829 shader_addline(buffer
, "%c", write_mask_chars
[0]);
1830 if (write_mask
& WINED3DSP_WRITEMASK_1
)
1831 shader_addline(buffer
, "%c", write_mask_chars
[1]);
1832 if (write_mask
& WINED3DSP_WRITEMASK_2
)
1833 shader_addline(buffer
, "%c", write_mask_chars
[2]);
1834 if (write_mask
& WINED3DSP_WRITEMASK_3
)
1835 shader_addline(buffer
, "%c", write_mask_chars
[3]);
1839 static void shader_dump_src_param(struct wined3d_string_buffer
*buffer
,
1840 const struct wined3d_shader_src_param
*param
, const struct wined3d_shader_version
*shader_version
)
1842 enum wined3d_shader_src_modifier src_modifier
= param
->modifiers
;
1843 DWORD swizzle
= param
->swizzle
;
1845 if (src_modifier
== WINED3DSPSM_NEG
1846 || src_modifier
== WINED3DSPSM_BIASNEG
1847 || src_modifier
== WINED3DSPSM_SIGNNEG
1848 || src_modifier
== WINED3DSPSM_X2NEG
1849 || src_modifier
== WINED3DSPSM_ABSNEG
)
1850 shader_addline(buffer
, "-");
1851 else if (src_modifier
== WINED3DSPSM_COMP
)
1852 shader_addline(buffer
, "1-");
1853 else if (src_modifier
== WINED3DSPSM_NOT
)
1854 shader_addline(buffer
, "!");
1856 if (src_modifier
== WINED3DSPSM_ABS
|| src_modifier
== WINED3DSPSM_ABSNEG
)
1857 shader_addline(buffer
, "abs(");
1859 shader_dump_register(buffer
, ¶m
->reg
, shader_version
);
1861 switch (src_modifier
)
1863 case WINED3DSPSM_NONE
: break;
1864 case WINED3DSPSM_NEG
: break;
1865 case WINED3DSPSM_NOT
: break;
1866 case WINED3DSPSM_BIAS
: shader_addline(buffer
, "_bias"); break;
1867 case WINED3DSPSM_BIASNEG
: shader_addline(buffer
, "_bias"); break;
1868 case WINED3DSPSM_SIGN
: shader_addline(buffer
, "_bx2"); break;
1869 case WINED3DSPSM_SIGNNEG
: shader_addline(buffer
, "_bx2"); break;
1870 case WINED3DSPSM_COMP
: break;
1871 case WINED3DSPSM_X2
: shader_addline(buffer
, "_x2"); break;
1872 case WINED3DSPSM_X2NEG
: shader_addline(buffer
, "_x2"); break;
1873 case WINED3DSPSM_DZ
: shader_addline(buffer
, "_dz"); break;
1874 case WINED3DSPSM_DW
: shader_addline(buffer
, "_dw"); break;
1875 case WINED3DSPSM_ABSNEG
: shader_addline(buffer
, ")"); break;
1876 case WINED3DSPSM_ABS
: shader_addline(buffer
, ")"); break;
1877 default: shader_addline(buffer
, "_unknown_modifier(%#x)", src_modifier
);
1880 if (swizzle
!= WINED3DSP_NOSWIZZLE
)
1882 static const char swizzle_chars
[] = "xyzw";
1883 DWORD swizzle_x
= swizzle
& 0x03;
1884 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
1885 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
1886 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
1888 if (swizzle_x
== swizzle_y
1889 && swizzle_x
== swizzle_z
1890 && swizzle_x
== swizzle_w
)
1892 shader_addline(buffer
, ".%c", swizzle_chars
[swizzle_x
]);
1896 shader_addline(buffer
, ".%c%c%c%c", swizzle_chars
[swizzle_x
], swizzle_chars
[swizzle_y
],
1897 swizzle_chars
[swizzle_z
], swizzle_chars
[swizzle_w
]);
1902 /* Shared code in order to generate the bulk of the shader string.
1903 * NOTE: A description of how to parse tokens can be found on MSDN. */
1904 void shader_generate_main(const struct wined3d_shader
*shader
, struct wined3d_string_buffer
*buffer
,
1905 const struct wined3d_shader_reg_maps
*reg_maps
, const DWORD
*byte_code
, void *backend_ctx
)
1907 struct wined3d_device
*device
= shader
->device
;
1908 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
1909 void *fe_data
= shader
->frontend_data
;
1910 struct wined3d_shader_version shader_version
;
1911 struct wined3d_shader_loop_state loop_state
;
1912 struct wined3d_shader_instruction ins
;
1913 struct wined3d_shader_tex_mx tex_mx
;
1914 struct wined3d_shader_context ctx
;
1915 const DWORD
*ptr
= byte_code
;
1917 /* Initialize current parsing state. */
1918 tex_mx
.current_row
= 0;
1919 loop_state
.current_depth
= 0;
1920 loop_state
.current_reg
= 0;
1922 ctx
.shader
= shader
;
1923 ctx
.gl_info
= &device
->adapter
->gl_info
;
1924 ctx
.reg_maps
= reg_maps
;
1925 ctx
.buffer
= buffer
;
1926 ctx
.tex_mx
= &tex_mx
;
1927 ctx
.loop_state
= &loop_state
;
1928 ctx
.backend_data
= backend_ctx
;
1931 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1933 while (!fe
->shader_is_end(fe_data
, &ptr
))
1936 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1938 /* Unknown opcode and its parameters. */
1939 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1941 TRACE("Skipping unrecognized instruction.\n");
1946 FIXME("Predicates not implemented.\n");
1948 /* Call appropriate function for output target */
1949 device
->shader_backend
->shader_handle_instruction(&ins
);
1953 static void shader_dump_ins_modifiers(struct wined3d_string_buffer
*buffer
,
1954 const struct wined3d_shader_dst_param
*dst
)
1956 DWORD mmask
= dst
->modifiers
;
1961 case 13: shader_addline(buffer
, "_d8"); break;
1962 case 14: shader_addline(buffer
, "_d4"); break;
1963 case 15: shader_addline(buffer
, "_d2"); break;
1964 case 1: shader_addline(buffer
, "_x2"); break;
1965 case 2: shader_addline(buffer
, "_x4"); break;
1966 case 3: shader_addline(buffer
, "_x8"); break;
1967 default: shader_addline(buffer
, "_unhandled_shift(%d)", dst
->shift
); break;
1970 if (mmask
& WINED3DSPDM_SATURATE
) shader_addline(buffer
, "_sat");
1971 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) shader_addline(buffer
, "_pp");
1972 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) shader_addline(buffer
, "_centroid");
1974 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
1975 if (mmask
) FIXME("_unrecognized_modifier(%#x)", mmask
);
1978 static void shader_dump_primitive_type(struct wined3d_string_buffer
*buffer
,
1979 enum wined3d_primitive_type primitive_type
)
1981 switch (primitive_type
)
1983 case WINED3D_PT_UNDEFINED
:
1984 shader_addline(buffer
, "undefined");
1986 case WINED3D_PT_POINTLIST
:
1987 shader_addline(buffer
, "pointlist");
1989 case WINED3D_PT_LINELIST
:
1990 shader_addline(buffer
, "linelist");
1992 case WINED3D_PT_LINESTRIP
:
1993 shader_addline(buffer
, "linestrip");
1995 case WINED3D_PT_TRIANGLELIST
:
1996 shader_addline(buffer
, "trianglelist");
1998 case WINED3D_PT_TRIANGLESTRIP
:
1999 shader_addline(buffer
, "trianglestrip");
2001 case WINED3D_PT_TRIANGLEFAN
:
2002 shader_addline(buffer
, "trianglefan");
2004 case WINED3D_PT_LINELIST_ADJ
:
2005 shader_addline(buffer
, "linelist_adj");
2007 case WINED3D_PT_LINESTRIP_ADJ
:
2008 shader_addline(buffer
, "linestrip_adj");
2010 case WINED3D_PT_TRIANGLELIST_ADJ
:
2011 shader_addline(buffer
, "trianglelist_adj");
2013 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
2014 shader_addline(buffer
, "trianglestrip_adj");
2017 shader_addline(buffer
, "<unrecognized_primitive_type %#x>", primitive_type
);
2022 static void shader_dump_interpolation_mode(struct wined3d_string_buffer
*buffer
,
2023 enum wined3d_shader_interpolation_mode interpolation_mode
)
2025 switch (interpolation_mode
)
2027 case WINED3DSIM_CONSTANT
:
2028 shader_addline(buffer
, "constant");
2030 case WINED3DSIM_LINEAR
:
2031 shader_addline(buffer
, "linear");
2033 case WINED3DSIM_LINEAR_CENTROID
:
2034 shader_addline(buffer
, "linear centroid");
2036 case WINED3DSIM_LINEAR_NOPERSPECTIVE
:
2037 shader_addline(buffer
, "linear noperspective");
2039 case WINED3DSIM_LINEAR_SAMPLE
:
2040 shader_addline(buffer
, "linear sample");
2042 case WINED3DSIM_LINEAR_NOPERSPECTIVE_CENTROID
:
2043 shader_addline(buffer
, "linear noperspective centroid");
2045 case WINED3DSIM_LINEAR_NOPERSPECTIVE_SAMPLE
:
2046 shader_addline(buffer
, "linear noperspective sample");
2049 shader_addline(buffer
, "<unrecognized_interpolation_mode %#x>", interpolation_mode
);
2054 static void shader_trace_init(const struct wined3d_shader_frontend
*fe
, void *fe_data
, const DWORD
*byte_code
)
2056 struct wined3d_shader_version shader_version
;
2057 struct wined3d_string_buffer buffer
;
2058 const DWORD
*ptr
= byte_code
;
2059 const char *type_prefix
;
2063 if (!string_buffer_init(&buffer
))
2065 ERR("Failed to initialize string buffer.\n");
2069 TRACE("Parsing %p.\n", byte_code
);
2071 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
2073 switch (shader_version
.type
)
2075 case WINED3D_SHADER_TYPE_VERTEX
:
2079 case WINED3D_SHADER_TYPE_HULL
:
2083 case WINED3D_SHADER_TYPE_DOMAIN
:
2087 case WINED3D_SHADER_TYPE_GEOMETRY
:
2091 case WINED3D_SHADER_TYPE_PIXEL
:
2096 FIXME("Unhandled shader type %#x.\n", shader_version
.type
);
2097 type_prefix
= "unknown";
2101 shader_addline(&buffer
, "%s_%u_%u\n", type_prefix
, shader_version
.major
, shader_version
.minor
);
2103 while (!fe
->shader_is_end(fe_data
, &ptr
))
2105 struct wined3d_shader_instruction ins
;
2107 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
2108 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
2110 WARN("Skipping unrecognized instruction.\n");
2111 shader_addline(&buffer
, "<unrecognized instruction>\n");
2115 if (ins
.handler_idx
== WINED3DSIH_DCL
|| ins
.handler_idx
== WINED3DSIH_DCL_UAV_TYPED
)
2117 shader_dump_decl_usage(&buffer
, &ins
.declaration
.semantic
, &shader_version
);
2118 shader_dump_ins_modifiers(&buffer
, &ins
.declaration
.semantic
.reg
);
2119 shader_addline(&buffer
, " ");
2120 shader_dump_dst_param(&buffer
, &ins
.declaration
.semantic
.reg
, &shader_version
);
2122 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
2124 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2125 shader_dump_src_param(&buffer
, &ins
.declaration
.src
, &shader_version
);
2126 shader_addline(&buffer
, ", %s",
2127 ins
.flags
& WINED3DSI_INDEXED_DYNAMIC
? "dynamicIndexed" : "immediateIndexed");
2129 else if (ins
.handler_idx
== WINED3DSIH_DCL_GLOBAL_FLAGS
)
2131 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2132 shader_dump_global_flags(&buffer
, ins
.flags
);
2134 else if (ins
.handler_idx
== WINED3DSIH_DCL_HS_MAX_TESSFACTOR
)
2136 shader_addline(&buffer
, "%s %.8e", shader_opcode_names
[ins
.handler_idx
],
2137 ins
.declaration
.max_tessellation_factor
);
2139 else if (ins
.handler_idx
== WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER
)
2141 shader_addline(&buffer
, "%s {\n", shader_opcode_names
[ins
.handler_idx
]);
2142 for (i
= 0; i
< ins
.declaration
.icb
->element_count
/ 4; ++i
)
2144 shader_addline(&buffer
, " {0x%08x, 0x%08x, 0x%08x, 0x%08x},\n",
2145 ins
.declaration
.icb
->data
[4 * i
+ 0],
2146 ins
.declaration
.icb
->data
[4 * i
+ 1],
2147 ins
.declaration
.icb
->data
[4 * i
+ 2],
2148 ins
.declaration
.icb
->data
[4 * i
+ 3]);
2150 shader_addline(&buffer
, "}");
2152 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PS
)
2154 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2155 shader_dump_interpolation_mode(&buffer
, ins
.flags
);
2156 shader_addline(&buffer
, " ");
2157 shader_dump_dst_param(&buffer
, &ins
.declaration
.dst
, &shader_version
);
2159 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PS_SGV
2160 || ins
.handler_idx
== WINED3DSIH_DCL_INPUT_SGV
2161 || ins
.handler_idx
== WINED3DSIH_DCL_INPUT_SIV
2162 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_SIV
)
2164 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2165 shader_dump_dst_param(&buffer
, &ins
.declaration
.register_semantic
.reg
, &shader_version
);
2166 shader_addline(&buffer
, ", ");
2167 shader_dump_sysval_semantic(&buffer
, ins
.declaration
.register_semantic
.sysval_semantic
);
2169 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PS_SIV
)
2171 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2172 shader_dump_interpolation_mode(&buffer
, ins
.flags
);
2173 shader_addline(&buffer
, " ");
2174 shader_dump_dst_param(&buffer
, &ins
.declaration
.register_semantic
.reg
, &shader_version
);
2175 shader_addline(&buffer
, ", ");
2176 shader_dump_sysval_semantic(&buffer
, ins
.declaration
.register_semantic
.sysval_semantic
);
2178 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT
2179 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT
)
2181 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2182 shader_dump_dst_param(&buffer
, &ins
.declaration
.dst
, &shader_version
);
2184 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
2185 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
2187 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2188 shader_dump_primitive_type(&buffer
, ins
.declaration
.primitive_type
);
2190 else if (ins
.handler_idx
== WINED3DSIH_DCL_RESOURCE_STRUCTURED
)
2192 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2193 shader_dump_dst_param(&buffer
, &ins
.declaration
.structured_resource
.reg
, &shader_version
);
2194 shader_addline(&buffer
, ", %u", ins
.declaration
.structured_resource
.byte_stride
);
2196 else if (ins
.handler_idx
== WINED3DSIH_DCL_SAMPLER
)
2198 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2199 shader_dump_dst_param(&buffer
, &ins
.declaration
.dst
, &shader_version
);
2200 if (ins
.flags
== WINED3DSI_SAMPLER_COMPARISON_MODE
)
2201 shader_addline(&buffer
, ", comparisonMode");
2203 else if (ins
.handler_idx
== WINED3DSIH_DCL_TEMPS
2204 || ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
2205 || ins
.handler_idx
== WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT
2206 || ins
.handler_idx
== WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT
2207 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT
)
2209 shader_addline(&buffer
, "%s %u", shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.count
);
2211 else if (ins
.handler_idx
== WINED3DSIH_DCL_TESSELLATOR_DOMAIN
)
2213 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2214 shader_dump_tessellator_domain(&buffer
, ins
.declaration
.tessellator_domain
);
2216 else if (ins
.handler_idx
== WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE
)
2218 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2219 shader_dump_tessellator_output_primitive(&buffer
, ins
.declaration
.tessellator_output_primitive
);
2221 else if (ins
.handler_idx
== WINED3DSIH_DCL_TESSELLATOR_PARTITIONING
)
2223 shader_addline(&buffer
, "%s ", shader_opcode_names
[ins
.handler_idx
]);
2224 shader_dump_tessellator_partitioning(&buffer
, ins
.declaration
.tessellator_partitioning
);
2226 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
2228 shader_addline(&buffer
, "def c%u = %.8e, %.8e, %.8e, %.8e", shader_get_float_offset(ins
.dst
[0].reg
.type
,
2229 ins
.dst
[0].reg
.idx
[0].offset
),
2230 *(const float *)&ins
.src
[0].reg
.immconst_data
[0],
2231 *(const float *)&ins
.src
[0].reg
.immconst_data
[1],
2232 *(const float *)&ins
.src
[0].reg
.immconst_data
[2],
2233 *(const float *)&ins
.src
[0].reg
.immconst_data
[3]);
2235 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
2237 shader_addline(&buffer
, "defi i%u = %d, %d, %d, %d", ins
.dst
[0].reg
.idx
[0].offset
,
2238 ins
.src
[0].reg
.immconst_data
[0],
2239 ins
.src
[0].reg
.immconst_data
[1],
2240 ins
.src
[0].reg
.immconst_data
[2],
2241 ins
.src
[0].reg
.immconst_data
[3]);
2243 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
2245 shader_addline(&buffer
, "defb b%u = %s",
2246 ins
.dst
[0].reg
.idx
[0].offset
, ins
.src
[0].reg
.immconst_data
[0] ? "true" : "false");
2252 shader_addline(&buffer
, "(");
2253 shader_dump_src_param(&buffer
, ins
.predicate
, &shader_version
);
2254 shader_addline(&buffer
, ") ");
2257 /* PixWin marks instructions with the coissue flag with a '+' */
2259 shader_addline(&buffer
, "+");
2261 shader_addline(&buffer
, "%s", shader_opcode_names
[ins
.handler_idx
]);
2263 if (ins
.handler_idx
== WINED3DSIH_IFC
2264 || ins
.handler_idx
== WINED3DSIH_BREAKC
)
2268 case WINED3D_SHADER_REL_OP_GT
: shader_addline(&buffer
, "_gt"); break;
2269 case WINED3D_SHADER_REL_OP_EQ
: shader_addline(&buffer
, "_eq"); break;
2270 case WINED3D_SHADER_REL_OP_GE
: shader_addline(&buffer
, "_ge"); break;
2271 case WINED3D_SHADER_REL_OP_LT
: shader_addline(&buffer
, "_lt"); break;
2272 case WINED3D_SHADER_REL_OP_NE
: shader_addline(&buffer
, "_ne"); break;
2273 case WINED3D_SHADER_REL_OP_LE
: shader_addline(&buffer
, "_le"); break;
2274 default: shader_addline(&buffer
, "_(%u)", ins
.flags
);
2277 else if (ins
.handler_idx
== WINED3DSIH_TEX
2278 && shader_version
.major
>= 2
2279 && (ins
.flags
& WINED3DSI_TEXLD_PROJECT
))
2281 shader_addline(&buffer
, "p");
2283 else if (ins
.handler_idx
== WINED3DSIH_RESINFO
2288 case WINED3DSI_RESINFO_RCP_FLOAT
: shader_addline(&buffer
, "_rcpFloat"); break;
2289 case WINED3DSI_RESINFO_UINT
: shader_addline(&buffer
, "_uint"); break;
2290 default: shader_addline(&buffer
, "_unrecognized(%#x)", ins
.flags
);
2294 if (wined3d_shader_instruction_has_texel_offset(&ins
))
2295 shader_addline(&buffer
, "(%d,%d,%d)", ins
.texel_offset
.u
, ins
.texel_offset
.v
, ins
.texel_offset
.w
);
2297 for (i
= 0; i
< ins
.dst_count
; ++i
)
2299 shader_dump_ins_modifiers(&buffer
, &ins
.dst
[i
]);
2300 shader_addline(&buffer
, !i
? " " : ", ");
2301 shader_dump_dst_param(&buffer
, &ins
.dst
[i
], &shader_version
);
2304 /* Other source tokens */
2305 for (i
= ins
.dst_count
; i
< (ins
.dst_count
+ ins
.src_count
); ++i
)
2307 shader_addline(&buffer
, !i
? " " : ", ");
2308 shader_dump_src_param(&buffer
, &ins
.src
[i
- ins
.dst_count
], &shader_version
);
2311 shader_addline(&buffer
, "\n");
2314 for (p
= buffer
.buffer
; *p
; p
= q
)
2316 if (!(q
= strstr(p
, "\n")))
2320 TRACE(" %.*s", (int)(q
- p
), p
);
2323 string_buffer_free(&buffer
);
2326 static void shader_cleanup(struct wined3d_shader
*shader
)
2328 HeapFree(GetProcessHeap(), 0, shader
->output_signature
.elements
);
2329 HeapFree(GetProcessHeap(), 0, shader
->input_signature
.elements
);
2330 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
2331 shader
->device
->shader_backend
->shader_destroy(shader
);
2332 HeapFree(GetProcessHeap(), 0, shader
->reg_maps
.constf
);
2333 HeapFree(GetProcessHeap(), 0, shader
->reg_maps
.sampler_map
.entries
);
2334 HeapFree(GetProcessHeap(), 0, shader
->function
);
2335 shader_delete_constant_list(&shader
->constantsF
);
2336 shader_delete_constant_list(&shader
->constantsB
);
2337 shader_delete_constant_list(&shader
->constantsI
);
2338 list_remove(&shader
->shader_list_entry
);
2340 if (shader
->frontend
&& shader
->frontend_data
)
2341 shader
->frontend
->shader_free(shader
->frontend_data
);
2344 struct shader_none_priv
2346 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
2347 const struct fragment_pipeline
*fragment_pipe
;
2348 BOOL ffp_proj_control
;
2351 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
2352 static void shader_none_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
2353 enum wined3d_gl_resource_type tex_type
, const SIZE
*ds_mask_size
) {}
2354 static void shader_none_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
) {}
2355 static void shader_none_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
2356 static void shader_none_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
2357 static void shader_none_load_constants(void *shader_priv
, struct wined3d_context
*context
,
2358 const struct wined3d_state
*state
) {}
2359 static void shader_none_destroy(struct wined3d_shader
*shader
) {}
2360 static void shader_none_free_context_data(struct wined3d_context
*context
) {}
2361 static void shader_none_init_context_state(struct wined3d_context
*context
) {}
2363 /* Context activation is done by the caller. */
2364 static void shader_none_select(void *shader_priv
, struct wined3d_context
*context
,
2365 const struct wined3d_state
*state
)
2367 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2368 struct shader_none_priv
*priv
= shader_priv
;
2370 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
2371 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
2374 /* Context activation is done by the caller. */
2375 static void shader_none_disable(void *shader_priv
, struct wined3d_context
*context
)
2377 struct shader_none_priv
*priv
= shader_priv
;
2378 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2380 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
2381 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
2383 context
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
2384 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
2385 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
2386 | (1u << WINED3D_SHADER_TYPE_HULL
)
2387 | (1u << WINED3D_SHADER_TYPE_DOMAIN
);
2390 static HRESULT
shader_none_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
2391 const struct fragment_pipeline
*fragment_pipe
)
2393 struct fragment_caps fragment_caps
;
2394 void *vertex_priv
, *fragment_priv
;
2395 struct shader_none_priv
*priv
;
2397 if (!(priv
= HeapAlloc(GetProcessHeap(), 0, sizeof(*priv
))))
2398 return E_OUTOFMEMORY
;
2400 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&none_shader_backend
, priv
)))
2402 ERR("Failed to initialize vertex pipe.\n");
2403 HeapFree(GetProcessHeap(), 0, priv
);
2407 if (!(fragment_priv
= fragment_pipe
->alloc_private(&none_shader_backend
, priv
)))
2409 ERR("Failed to initialize fragment pipe.\n");
2410 vertex_pipe
->vp_free(device
);
2411 HeapFree(GetProcessHeap(), 0, priv
);
2415 priv
->vertex_pipe
= vertex_pipe
;
2416 priv
->fragment_pipe
= fragment_pipe
;
2417 fragment_pipe
->get_caps(&device
->adapter
->gl_info
, &fragment_caps
);
2418 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
2420 device
->vertex_priv
= vertex_priv
;
2421 device
->fragment_priv
= fragment_priv
;
2422 device
->shader_priv
= priv
;
2427 static void shader_none_free(struct wined3d_device
*device
)
2429 struct shader_none_priv
*priv
= device
->shader_priv
;
2431 priv
->fragment_pipe
->free_private(device
);
2432 priv
->vertex_pipe
->vp_free(device
);
2433 HeapFree(GetProcessHeap(), 0, priv
);
2436 static BOOL
shader_none_allocate_context_data(struct wined3d_context
*context
)
2441 static void shader_none_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
2443 /* Set the shader caps to 0 for the none shader backend */
2444 caps
->vs_version
= 0;
2445 caps
->hs_version
= 0;
2446 caps
->ds_version
= 0;
2447 caps
->gs_version
= 0;
2448 caps
->ps_version
= 0;
2449 caps
->vs_uniform_count
= 0;
2450 caps
->ps_uniform_count
= 0;
2451 caps
->ps_1x_max_value
= 0.0f
;
2452 caps
->varying_count
= 0;
2453 caps
->wined3d_caps
= 0;
2456 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
2458 /* We "support" every possible fixup, since we don't support any shader
2459 * model, and will never have to actually sample a texture. */
2463 static BOOL
shader_none_has_ffp_proj_control(void *shader_priv
)
2465 struct shader_none_priv
*priv
= shader_priv
;
2467 return priv
->ffp_proj_control
;
2470 const struct wined3d_shader_backend_ops none_shader_backend
=
2472 shader_none_handle_instruction
,
2474 shader_none_disable
,
2475 shader_none_select_depth_blt
,
2476 shader_none_deselect_depth_blt
,
2477 shader_none_update_float_vertex_constants
,
2478 shader_none_update_float_pixel_constants
,
2479 shader_none_load_constants
,
2480 shader_none_destroy
,
2483 shader_none_allocate_context_data
,
2484 shader_none_free_context_data
,
2485 shader_none_init_context_state
,
2486 shader_none_get_caps
,
2487 shader_none_color_fixup_supported
,
2488 shader_none_has_ffp_proj_control
,
2491 static HRESULT
shader_set_function(struct wined3d_shader
*shader
, const DWORD
*byte_code
,
2492 const struct wined3d_shader_signature
*output_signature
, DWORD float_const_count
,
2493 enum wined3d_shader_type type
, unsigned int max_version
)
2495 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2496 const struct wined3d_shader_frontend
*fe
;
2498 unsigned int backend_version
;
2499 const struct wined3d_d3d_info
*d3d_info
= &shader
->device
->adapter
->d3d_info
;
2501 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u, type %#x, max_version %u.\n",
2502 shader
, byte_code
, output_signature
, float_const_count
, type
, max_version
);
2504 list_init(&shader
->constantsF
);
2505 list_init(&shader
->constantsB
);
2506 list_init(&shader
->constantsI
);
2507 shader
->lconst_inf_or_nan
= FALSE
;
2509 fe
= shader_select_frontend(*byte_code
);
2512 FIXME("Unable to find frontend for shader.\n");
2513 return WINED3DERR_INVALIDCALL
;
2515 shader
->frontend
= fe
;
2516 shader
->frontend_data
= fe
->shader_init(byte_code
, output_signature
);
2517 if (!shader
->frontend_data
)
2519 FIXME("Failed to initialize frontend.\n");
2520 return WINED3DERR_INVALIDCALL
;
2523 /* First pass: trace shader. */
2524 if (TRACE_ON(d3d_shader
))
2525 shader_trace_init(fe
, shader
->frontend_data
, byte_code
);
2527 /* Second pass: figure out which registers are used, what the semantics are, etc. */
2528 if (FAILED(hr
= shader_get_registers_used(shader
, fe
, reg_maps
, &shader
->input_signature
,
2529 &shader
->output_signature
, byte_code
, float_const_count
)))
2532 if (reg_maps
->shader_version
.type
!= type
)
2534 WARN("Wrong shader type %d.\n", reg_maps
->shader_version
.type
);
2535 return WINED3DERR_INVALIDCALL
;
2537 if (reg_maps
->shader_version
.major
> max_version
)
2539 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps
->shader_version
.major
);
2540 return WINED3DERR_INVALIDCALL
;
2544 case WINED3D_SHADER_TYPE_VERTEX
:
2545 backend_version
= d3d_info
->limits
.vs_version
;
2547 case WINED3D_SHADER_TYPE_HULL
:
2548 backend_version
= d3d_info
->limits
.hs_version
;
2550 case WINED3D_SHADER_TYPE_DOMAIN
:
2551 backend_version
= d3d_info
->limits
.ds_version
;
2553 case WINED3D_SHADER_TYPE_GEOMETRY
:
2554 backend_version
= d3d_info
->limits
.gs_version
;
2556 case WINED3D_SHADER_TYPE_PIXEL
:
2557 backend_version
= d3d_info
->limits
.ps_version
;
2560 FIXME("No backend version-checking for this shader type\n");
2561 backend_version
= 0;
2563 if (reg_maps
->shader_version
.major
> backend_version
)
2565 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
2566 reg_maps
->shader_version
.major
, reg_maps
->shader_version
.minor
);
2567 return WINED3DERR_INVALIDCALL
;
2570 shader
->function
= HeapAlloc(GetProcessHeap(), 0, shader
->functionLength
);
2571 if (!shader
->function
)
2572 return E_OUTOFMEMORY
;
2573 memcpy(shader
->function
, byte_code
, shader
->functionLength
);
2578 ULONG CDECL
wined3d_shader_incref(struct wined3d_shader
*shader
)
2580 ULONG refcount
= InterlockedIncrement(&shader
->ref
);
2582 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
2587 ULONG CDECL
wined3d_shader_decref(struct wined3d_shader
*shader
)
2589 ULONG refcount
= InterlockedDecrement(&shader
->ref
);
2591 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
2595 shader_cleanup(shader
);
2596 shader
->parent_ops
->wined3d_object_destroyed(shader
->parent
);
2597 HeapFree(GetProcessHeap(), 0, shader
);
2603 void * CDECL
wined3d_shader_get_parent(const struct wined3d_shader
*shader
)
2605 TRACE("shader %p.\n", shader
);
2607 return shader
->parent
;
2610 HRESULT CDECL
wined3d_shader_get_byte_code(const struct wined3d_shader
*shader
,
2611 void *byte_code
, UINT
*byte_code_size
)
2613 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader
, byte_code
, byte_code_size
);
2617 *byte_code_size
= shader
->functionLength
;
2621 if (*byte_code_size
< shader
->functionLength
)
2623 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
2624 * than the required size we should write the required size and
2625 * return D3DERR_MOREDATA. That's not actually true. */
2626 return WINED3DERR_INVALIDCALL
;
2629 memcpy(byte_code
, shader
->function
, shader
->functionLength
);
2634 /* Set local constants for d3d8 shaders. */
2635 HRESULT CDECL
wined3d_shader_set_local_constants_float(struct wined3d_shader
*shader
,
2636 UINT start_idx
, const float *src_data
, UINT count
)
2638 UINT end_idx
= start_idx
+ count
;
2641 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader
, start_idx
, src_data
, count
);
2643 if (end_idx
> shader
->limits
->constant_float
)
2645 WARN("end_idx %u > float constants limit %u.\n",
2646 end_idx
, shader
->limits
->constant_float
);
2647 end_idx
= shader
->limits
->constant_float
;
2650 for (i
= start_idx
; i
< end_idx
; ++i
)
2652 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
2655 return E_OUTOFMEMORY
;
2658 value
= (float *)lconst
->value
;
2659 memcpy(value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
2660 list_add_head(&shader
->constantsF
, &lconst
->entry
);
2662 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
2663 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
2665 shader
->lconst_inf_or_nan
= TRUE
;
2672 void find_vs_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2673 WORD swizzle_map
, struct vs_compile_args
*args
, const struct wined3d_d3d_info
*d3d_info
)
2675 args
->fog_src
= state
->render_states
[WINED3D_RS_FOGTABLEMODE
]
2676 == WINED3D_FOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
2677 args
->clip_enabled
= state
->render_states
[WINED3D_RS_CLIPPING
]
2678 && state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
];
2679 args
->point_size
= state
->gl_primitive_type
== GL_POINTS
;
2680 args
->per_vertex_point_size
= shader
->reg_maps
.point_size
;
2681 if (shader
->reg_maps
.shader_version
.major
>= 4)
2682 args
->next_shader_input_count
= state
->shader
[WINED3D_SHADER_TYPE_HULL
]
2683 ? state
->shader
[WINED3D_SHADER_TYPE_HULL
]->limits
->packed_input
2684 : state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
]
2685 ? state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
]->limits
->packed_input
2686 : state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]
2687 ? state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->limits
->packed_input
: 0;
2689 args
->next_shader_input_count
= 0;
2690 args
->swizzle_map
= swizzle_map
;
2691 if (d3d_info
->emulated_flatshading
)
2692 args
->flatshading
= state
->render_states
[WINED3D_RS_SHADEMODE
] == WINED3D_SHADE_FLAT
;
2694 args
->flatshading
= 0;
2697 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
2699 if (usage_idx1
!= usage_idx2
)
2701 if (usage1
== usage2
)
2703 if (usage1
== WINED3D_DECL_USAGE_POSITION
&& usage2
== WINED3D_DECL_USAGE_POSITIONT
)
2705 if (usage2
== WINED3D_DECL_USAGE_POSITION
&& usage1
== WINED3D_DECL_USAGE_POSITIONT
)
2711 BOOL
vshader_get_input(const struct wined3d_shader
*shader
,
2712 BYTE usage_req
, BYTE usage_idx_req
, unsigned int *regnum
)
2714 WORD map
= shader
->reg_maps
.input_registers
;
2717 for (i
= 0; map
; map
>>= 1, ++i
)
2719 if (!(map
& 1)) continue;
2721 if (match_usage(shader
->u
.vs
.attributes
[i
].usage
,
2722 shader
->u
.vs
.attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
2731 static HRESULT
shader_signature_copy(struct wined3d_shader_signature
*dst
,
2732 const struct wined3d_shader_signature
*src
, char **signature_strings
)
2734 struct wined3d_shader_signature_element
*e
;
2739 ptr
= *signature_strings
;
2741 dst
->element_count
= src
->element_count
;
2742 if (!(dst
->elements
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dst
->elements
) * dst
->element_count
)))
2743 return E_OUTOFMEMORY
;
2745 for (i
= 0; i
< src
->element_count
; ++i
)
2747 e
= &src
->elements
[i
];
2748 dst
->elements
[i
] = *e
;
2750 len
= strlen(e
->semantic_name
);
2751 memcpy(ptr
, e
->semantic_name
, len
+ 1);
2752 dst
->elements
[i
].semantic_name
= ptr
;
2756 *signature_strings
= ptr
;
2761 static HRESULT
shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2762 const struct wined3d_shader_desc
*desc
, DWORD float_const_count
, enum wined3d_shader_type type
,
2763 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2765 struct wined3d_shader_signature_element
*e
;
2771 if (!desc
->byte_code
)
2772 return WINED3DERR_INVALIDCALL
;
2775 shader
->device
= device
;
2776 shader
->parent
= parent
;
2777 shader
->parent_ops
= parent_ops
;
2780 if (desc
->input_signature
)
2782 for (i
= 0; i
< desc
->input_signature
->element_count
; ++i
)
2784 e
= &desc
->input_signature
->elements
[i
];
2785 len
= strlen(e
->semantic_name
);
2786 if (len
>= ~(SIZE_T
)0 - total
)
2787 return E_OUTOFMEMORY
;
2792 if (desc
->output_signature
)
2794 for (i
= 0; i
< desc
->output_signature
->element_count
; ++i
)
2796 e
= &desc
->output_signature
->elements
[i
];
2797 len
= strlen(e
->semantic_name
);
2798 if (len
>= ~(SIZE_T
)0 - total
)
2799 return E_OUTOFMEMORY
;
2804 if (!(shader
->signature_strings
= HeapAlloc(GetProcessHeap(), 0, total
)))
2805 return E_OUTOFMEMORY
;
2806 ptr
= shader
->signature_strings
;
2808 if (desc
->input_signature
&& FAILED(hr
= shader_signature_copy(&shader
->input_signature
,
2809 desc
->input_signature
, &ptr
)))
2811 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
2814 if (desc
->output_signature
&& FAILED(hr
= shader_signature_copy(&shader
->output_signature
,
2815 desc
->output_signature
, &ptr
)))
2817 HeapFree(GetProcessHeap(), 0, shader
->input_signature
.elements
);
2818 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
2822 list_init(&shader
->linked_programs
);
2823 list_add_head(&device
->shaders
, &shader
->shader_list_entry
);
2825 if (FAILED(hr
= shader_set_function(shader
, desc
->byte_code
, desc
->output_signature
,
2826 float_const_count
, type
, desc
->max_version
)))
2828 WARN("Failed to set function, hr %#x.\n", hr
);
2829 shader_cleanup(shader
);
2835 static HRESULT
vertex_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2836 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2838 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2842 if (FAILED(hr
= shader_init(shader
, device
, desc
, device
->adapter
->d3d_info
.limits
.vs_uniform_count
,
2843 WINED3D_SHADER_TYPE_VERTEX
, parent
, parent_ops
)))
2846 for (i
= 0; i
< shader
->input_signature
.element_count
; ++i
)
2848 const struct wined3d_shader_signature_element
*input
= &shader
->input_signature
.elements
[i
];
2850 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)) || !input
->semantic_name
)
2853 shader
->u
.vs
.attributes
[input
->register_idx
].usage
=
2854 shader_usage_from_semantic_name(input
->semantic_name
);
2855 shader
->u
.vs
.attributes
[input
->register_idx
].usage_idx
= input
->semantic_idx
;
2858 shader
->load_local_constsF
= (reg_maps
->usesrelconstF
&& !list_empty(&shader
->constantsF
)) ||
2859 shader
->lconst_inf_or_nan
;
2864 static HRESULT
domain_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2865 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2869 if (FAILED(hr
= shader_init(shader
, device
, desc
, 0, WINED3D_SHADER_TYPE_DOMAIN
, parent
, parent_ops
)))
2872 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2877 static HRESULT
hull_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2878 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2882 if (FAILED(hr
= shader_init(shader
, device
, desc
, 0, WINED3D_SHADER_TYPE_HULL
, parent
, parent_ops
)))
2885 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2890 static HRESULT
geometry_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2891 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2895 if (FAILED(hr
= shader_init(shader
, device
, desc
, 0,
2896 WINED3D_SHADER_TYPE_GEOMETRY
, parent
, parent_ops
)))
2899 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2904 void find_ps_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2905 BOOL position_transformed
, struct ps_compile_args
*args
, const struct wined3d_context
*context
)
2907 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2908 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2909 const struct wined3d_texture
*texture
;
2912 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
2913 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && needs_srgb_write(context
, state
, state
->fb
))
2915 static unsigned int warned
= 0;
2917 args
->srgb_correction
= 1;
2918 if (state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] && !warned
++)
2919 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
2920 "support, expect rendering artifacts.\n");
2923 if (shader
->reg_maps
.shader_version
.major
== 1
2924 && shader
->reg_maps
.shader_version
.minor
<= 3)
2926 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2928 DWORD flags
= state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
];
2930 if (flags
& WINED3D_TTFF_PROJECTED
)
2932 DWORD tex_transform
= flags
& ~WINED3D_TTFF_PROJECTED
;
2934 if (!state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
2936 enum wined3d_shader_resource_type resource_type
= shader
->reg_maps
.resource_info
[i
].type
;
2938 unsigned int index
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
2939 DWORD max_valid
= WINED3D_TTFF_COUNT4
;
2941 for (j
= 0; j
< state
->vertex_declaration
->element_count
; ++j
)
2943 struct wined3d_vertex_declaration_element
*element
=
2944 &state
->vertex_declaration
->elements
[j
];
2946 if (element
->usage
== WINED3D_DECL_USAGE_TEXCOORD
2947 && element
->usage_idx
== index
)
2949 max_valid
= element
->format
->component_count
;
2953 if (!tex_transform
|| tex_transform
> max_valid
)
2955 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2956 tex_transform
, max_valid
);
2957 tex_transform
= max_valid
;
2959 if ((resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_1D
&& tex_transform
> WINED3D_TTFF_COUNT1
)
2960 || (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2D
2961 && tex_transform
> WINED3D_TTFF_COUNT2
)
2962 || (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_3D
2963 && tex_transform
> WINED3D_TTFF_COUNT3
))
2964 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
2967 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2968 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2969 i
, tex_transform
, resource_type
);
2973 tex_transform
= WINED3D_TTFF_COUNT4
| WINED3D_PSARGS_PROJECTED
;
2975 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
2979 if (shader
->reg_maps
.shader_version
.major
== 1
2980 && shader
->reg_maps
.shader_version
.minor
<= 4)
2982 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2984 const struct wined3d_texture
*texture
= state
->textures
[i
];
2986 if (!shader
->reg_maps
.resource_info
[i
].type
)
2989 /* Treat unbound textures as 2D. The dummy texture will provide
2990 * the proper sample value. The tex_types bitmap defaults to
2991 * 2D because of the memset. */
2995 switch (texture
->target
)
2997 /* RECT textures are distinguished from 2D textures via np2_fixup */
2998 case GL_TEXTURE_RECTANGLE_ARB
:
3003 args
->tex_types
|= WINED3D_SHADER_TEX_3D
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
3006 case GL_TEXTURE_CUBE_MAP_ARB
:
3007 args
->tex_types
|= WINED3D_SHADER_TEX_CUBE
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
3013 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3015 if (!shader
->reg_maps
.resource_info
[i
].type
)
3018 texture
= state
->textures
[i
];
3021 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
3024 if (can_use_texture_swizzle(gl_info
, texture
->resource
.format
))
3025 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
3027 args
->color_fixup
[i
] = texture
->resource
.format
->color_fixup
;
3029 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_SHADOW
)
3030 args
->shadow
|= 1u << i
;
3032 /* Flag samplers that need NP2 texcoord fixup. */
3033 if (!(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
3034 args
->np2_fixup
|= (1u << i
);
3037 /* In SM4+ we use dcl_sampler in order to determine if we should use shadow sampler. */
3038 if (shader
->reg_maps
.shader_version
.major
>= 4)
3041 if (shader
->reg_maps
.shader_version
.major
>= 3)
3043 if (position_transformed
)
3044 args
->vp_mode
= pretransformed
;
3045 else if (use_vs(state
))
3046 args
->vp_mode
= vertexshader
;
3048 args
->vp_mode
= fixedfunction
;
3049 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
3053 args
->vp_mode
= vertexshader
;
3054 if (state
->render_states
[WINED3D_RS_FOGENABLE
])
3056 switch (state
->render_states
[WINED3D_RS_FOGTABLEMODE
])
3058 case WINED3D_FOG_NONE
:
3059 if (position_transformed
|| use_vs(state
))
3061 args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
;
3065 switch (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
])
3067 case WINED3D_FOG_NONE
: /* Fall through. */
3068 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
3069 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
3070 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
3074 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
3075 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
3076 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
3081 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
3085 if (context
->d3d_info
->limits
.varying_count
< wined3d_max_compat_varyings(context
->gl_info
))
3087 const struct wined3d_shader
*vs
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
3089 args
->texcoords_initialized
= 0;
3090 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
3094 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.output_registers
& (1u << i
))
3095 args
->texcoords_initialized
|= 1u << i
;
3099 const struct wined3d_stream_info
*si
= &context
->stream_info
;
3100 unsigned int coord_idx
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
3102 if ((state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
] >> WINED3D_FFP_TCI_SHIFT
)
3103 & WINED3D_FFP_TCI_MASK
3104 || (coord_idx
< MAX_TEXTURES
&& (si
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ coord_idx
)))))
3105 args
->texcoords_initialized
|= 1u << i
;
3111 args
->texcoords_initialized
= (1u << MAX_TEXTURES
) - 1;
3114 args
->pointsprite
= state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
]
3115 && state
->gl_primitive_type
== GL_POINTS
;
3117 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
3118 args
->alpha_test_func
= WINED3D_CMP_ALWAYS
- 1;
3120 args
->alpha_test_func
= (state
->render_states
[WINED3D_RS_ALPHATESTENABLE
]
3121 ? wined3d_sanitize_cmp_func(state
->render_states
[WINED3D_RS_ALPHAFUNC
])
3122 : WINED3D_CMP_ALWAYS
) - 1;
3124 if (d3d_info
->emulated_flatshading
)
3125 args
->flatshading
= state
->render_states
[WINED3D_RS_SHADEMODE
] == WINED3D_SHADE_FLAT
;
3128 static HRESULT
pixel_shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
3129 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
3131 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
3132 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
3135 if (FAILED(hr
= shader_init(shader
, device
, desc
, device
->adapter
->d3d_info
.limits
.ps_uniform_count
,
3136 WINED3D_SHADER_TYPE_PIXEL
, parent
, parent_ops
)))
3139 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
3141 if (shader
->u
.ps
.input_reg_used
[i
])
3144 highest_reg_used
= i
;
3148 /* Don't do any register mapping magic if it is not needed, or if we can't
3149 * achieve anything anyway */
3150 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
3151 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4)
3152 || shader
->reg_maps
.shader_version
.major
>= 4)
3154 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
3156 /* This happens with relative addressing. The input mapper function
3157 * warns about this if the higher registers are declared too, so
3158 * don't write a FIXME here */
3159 WARN("More varying registers used than supported\n");
3162 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
3164 shader
->u
.ps
.input_reg_map
[i
] = i
;
3167 shader
->u
.ps
.declared_in_count
= highest_reg_used
+ 1;
3171 shader
->u
.ps
.declared_in_count
= 0;
3172 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
3174 if (shader
->u
.ps
.input_reg_used
[i
])
3175 shader
->u
.ps
.input_reg_map
[i
] = shader
->u
.ps
.declared_in_count
++;
3176 else shader
->u
.ps
.input_reg_map
[i
] = ~0U;
3180 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
3185 void pixelshader_update_resource_types(struct wined3d_shader
*shader
, WORD tex_types
)
3187 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
3188 struct wined3d_shader_resource_info
*resource_info
= reg_maps
->resource_info
;
3191 if (reg_maps
->shader_version
.major
!= 1) return;
3193 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
3195 /* We don't sample from this sampler. */
3196 if (!resource_info
[i
].type
)
3199 switch ((tex_types
>> i
* WINED3D_PSARGS_TEXTYPE_SHIFT
) & WINED3D_PSARGS_TEXTYPE_MASK
)
3201 case WINED3D_SHADER_TEX_2D
:
3202 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
3205 case WINED3D_SHADER_TEX_3D
:
3206 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_3D
;
3209 case WINED3D_SHADER_TEX_CUBE
:
3210 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
;
3216 HRESULT CDECL
wined3d_shader_create_ds(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3217 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3219 struct wined3d_shader
*object
;
3222 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3223 device
, desc
, parent
, parent_ops
, shader
);
3225 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
3226 return E_OUTOFMEMORY
;
3228 if (FAILED(hr
= domain_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3230 WARN("Failed to initialize domain shader, hr %#x.\n", hr
);
3231 HeapFree(GetProcessHeap(), 0, object
);
3235 TRACE("Created domain shader %p.\n", object
);
3241 HRESULT CDECL
wined3d_shader_create_gs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3242 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3244 struct wined3d_shader
*object
;
3247 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3248 device
, desc
, parent
, parent_ops
, shader
);
3250 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
3252 return E_OUTOFMEMORY
;
3254 if (FAILED(hr
= geometry_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3256 WARN("Failed to initialize geometry shader, hr %#x.\n", hr
);
3257 HeapFree(GetProcessHeap(), 0, object
);
3261 TRACE("Created geometry shader %p.\n", object
);
3267 HRESULT CDECL
wined3d_shader_create_hs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3268 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3270 struct wined3d_shader
*object
;
3273 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3274 device
, desc
, parent
, parent_ops
, shader
);
3276 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
3277 return E_OUTOFMEMORY
;
3279 if (FAILED(hr
= hull_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3281 WARN("Failed to initialize hull shader, hr %#x.\n", hr
);
3282 HeapFree(GetProcessHeap(), 0, object
);
3286 TRACE("Created hull shader %p.\n", object
);
3292 HRESULT CDECL
wined3d_shader_create_ps(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3293 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3295 struct wined3d_shader
*object
;
3298 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3299 device
, desc
, parent
, parent_ops
, shader
);
3301 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
3303 return E_OUTOFMEMORY
;
3305 if (FAILED(hr
= pixel_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3307 WARN("Failed to initialize pixel shader, hr %#x.\n", hr
);
3308 HeapFree(GetProcessHeap(), 0, object
);
3312 TRACE("Created pixel shader %p.\n", object
);
3318 HRESULT CDECL
wined3d_shader_create_vs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
3319 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
3321 struct wined3d_shader
*object
;
3324 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
3325 device
, desc
, parent
, parent_ops
, shader
);
3327 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
3329 return E_OUTOFMEMORY
;
3331 if (FAILED(hr
= vertex_shader_init(object
, device
, desc
, parent
, parent_ops
)))
3333 WARN("Failed to initialize vertex shader, hr %#x.\n", hr
);
3334 HeapFree(GetProcessHeap(), 0, object
);
3338 TRACE("Created vertex shader %p.\n", object
);