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_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
59 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
60 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
61 /* WINED3DSIH_DEF */ "def",
62 /* WINED3DSIH_DEFB */ "defb",
63 /* WINED3DSIH_DEFI */ "defi",
64 /* WINED3DSIH_DIV */ "div",
65 /* WINED3DSIH_DP2 */ "dp2",
66 /* WINED3DSIH_DP2ADD */ "dp2add",
67 /* WINED3DSIH_DP3 */ "dp3",
68 /* WINED3DSIH_DP4 */ "dp4",
69 /* WINED3DSIH_DST */ "dst",
70 /* WINED3DSIH_DSX */ "dsx",
71 /* WINED3DSIH_DSY */ "dsy",
72 /* WINED3DSIH_ELSE */ "else",
73 /* WINED3DSIH_EMIT */ "emit",
74 /* WINED3DSIH_ENDIF */ "endif",
75 /* WINED3DSIH_ENDLOOP */ "endloop",
76 /* WINED3DSIH_ENDREP */ "endrep",
77 /* WINED3DSIH_EQ */ "eq",
78 /* WINED3DSIH_EXP */ "exp",
79 /* WINED3DSIH_EXPP */ "expp",
80 /* WINED3DSIH_FRC */ "frc",
81 /* WINED3DSIH_FTOI */ "ftoi",
82 /* WINED3DSIH_GE */ "ge",
83 /* WINED3DSIH_IADD */ "iadd",
84 /* WINED3DSIH_IEQ */ "ieq",
85 /* WINED3DSIH_IF */ "if",
86 /* WINED3DSIH_IFC */ "ifc",
87 /* WINED3DSIH_IGE */ "ige",
88 /* WINED3DSIH_IMUL */ "imul",
89 /* WINED3DSIH_ISHL */ "ishl",
90 /* WINED3DSIH_ITOF */ "itof",
91 /* WINED3DSIH_LABEL */ "label",
92 /* WINED3DSIH_LD */ "ld",
93 /* WINED3DSIH_LIT */ "lit",
94 /* WINED3DSIH_LOG */ "log",
95 /* WINED3DSIH_LOGP */ "logp",
96 /* WINED3DSIH_LOOP */ "loop",
97 /* WINED3DSIH_LRP */ "lrp",
98 /* WINED3DSIH_LT */ "lt",
99 /* WINED3DSIH_M3x2 */ "m3x2",
100 /* WINED3DSIH_M3x3 */ "m3x3",
101 /* WINED3DSIH_M3x4 */ "m3x4",
102 /* WINED3DSIH_M4x3 */ "m4x3",
103 /* WINED3DSIH_M4x4 */ "m4x4",
104 /* WINED3DSIH_MAD */ "mad",
105 /* WINED3DSIH_MAX */ "max",
106 /* WINED3DSIH_MIN */ "min",
107 /* WINED3DSIH_MOV */ "mov",
108 /* WINED3DSIH_MOVA */ "mova",
109 /* WINED3DSIH_MOVC */ "movc",
110 /* WINED3DSIH_MUL */ "mul",
111 /* WINED3DSIH_NE */ "ne",
112 /* WINED3DSIH_NOP */ "nop",
113 /* WINED3DSIH_NRM */ "nrm",
114 /* WINED3DSIH_OR */ "or",
115 /* WINED3DSIH_PHASE */ "phase",
116 /* WINED3DSIH_POW */ "pow",
117 /* WINED3DSIH_RCP */ "rcp",
118 /* WINED3DSIH_REP */ "rep",
119 /* WINED3DSIH_RET */ "ret",
120 /* WINED3DSIH_ROUND_NI */ "round_ni",
121 /* WINED3DSIH_RSQ */ "rsq",
122 /* WINED3DSIH_SAMPLE */ "sample",
123 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
124 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
125 /* WINED3DSIH_SETP */ "setp",
126 /* WINED3DSIH_SGE */ "sge",
127 /* WINED3DSIH_SGN */ "sgn",
128 /* WINED3DSIH_SINCOS */ "sincos",
129 /* WINED3DSIH_SLT */ "slt",
130 /* WINED3DSIH_SQRT */ "sqrt",
131 /* WINED3DSIH_SUB */ "sub",
132 /* WINED3DSIH_TEX */ "texld",
133 /* WINED3DSIH_TEXBEM */ "texbem",
134 /* WINED3DSIH_TEXBEML */ "texbeml",
135 /* WINED3DSIH_TEXCOORD */ "texcrd",
136 /* WINED3DSIH_TEXDEPTH */ "texdepth",
137 /* WINED3DSIH_TEXDP3 */ "texdp3",
138 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
139 /* WINED3DSIH_TEXKILL */ "texkill",
140 /* WINED3DSIH_TEXLDD */ "texldd",
141 /* WINED3DSIH_TEXLDL */ "texldl",
142 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
143 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
144 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
145 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
146 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
147 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
148 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
149 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
150 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
151 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
152 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
153 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
154 /* WINED3DSIH_UDIV */ "udiv",
155 /* WINED3DSIH_UGE */ "uge",
156 /* WINED3DSIH_USHR */ "ushr",
157 /* WINED3DSIH_UTOF */ "utof",
158 /* WINED3DSIH_XOR */ "xor",
161 static const char * const semantic_names
[] =
163 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
164 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
165 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
166 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
167 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
168 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
169 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
170 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
171 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
172 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
173 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
174 /* WINED3D_DECL_USAGE_FOG */ "FOG",
175 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
176 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
179 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage
)
181 if (usage
>= sizeof(semantic_names
) / sizeof(*semantic_names
))
183 FIXME("Unrecognized usage %#x.\n", usage
);
184 return "UNRECOGNIZED";
187 return semantic_names
[usage
];
190 static enum wined3d_decl_usage
shader_usage_from_semantic_name(const char *name
)
194 for (i
= 0; i
< sizeof(semantic_names
) / sizeof(*semantic_names
); ++i
)
196 if (!strcmp(name
, semantic_names
[i
])) return i
;
202 BOOL
shader_match_semantic(const char *semantic_name
, enum wined3d_decl_usage usage
)
204 return !strcmp(semantic_name
, shader_semantic_name_from_usage(usage
));
207 static void shader_signature_from_semantic(struct wined3d_shader_signature_element
*e
,
208 const struct wined3d_shader_semantic
*s
)
210 e
->semantic_name
= shader_semantic_name_from_usage(s
->usage
);
211 e
->semantic_idx
= s
->usage_idx
;
212 e
->sysval_semantic
= 0;
213 e
->component_type
= 0;
214 e
->register_idx
= s
->reg
.reg
.idx
[0].offset
;
215 e
->mask
= s
->reg
.write_mask
;
218 static void shader_signature_from_usage(struct wined3d_shader_signature_element
*e
,
219 enum wined3d_decl_usage usage
, UINT usage_idx
, UINT reg_idx
, DWORD write_mask
)
221 e
->semantic_name
= shader_semantic_name_from_usage(usage
);
222 e
->semantic_idx
= usage_idx
;
223 e
->sysval_semantic
= 0;
224 e
->component_type
= 0;
225 e
->register_idx
= reg_idx
;
226 e
->mask
= write_mask
;
229 static const struct wined3d_shader_frontend
*shader_select_frontend(DWORD version_token
)
231 switch (version_token
>> 16)
235 return &sm1_shader_frontend
;
240 return &sm4_shader_frontend
;
243 FIXME("Unrecognised version token %#x\n", version_token
);
248 void string_buffer_clear(struct wined3d_string_buffer
*buffer
)
250 buffer
->buffer
[0] = '\0';
251 buffer
->content_size
= 0;
254 BOOL
string_buffer_init(struct wined3d_string_buffer
*buffer
)
256 buffer
->buffer_size
= 32;
257 if (!(buffer
->buffer
= HeapAlloc(GetProcessHeap(), 0, buffer
->buffer_size
)))
259 ERR("Failed to allocate shader buffer memory.\n");
263 string_buffer_clear(buffer
);
267 void string_buffer_free(struct wined3d_string_buffer
*buffer
)
269 HeapFree(GetProcessHeap(), 0, buffer
->buffer
);
272 BOOL
string_buffer_resize(struct wined3d_string_buffer
*buffer
, int rc
)
275 unsigned int new_buffer_size
= buffer
->buffer_size
* 2;
277 while (rc
> 0 && (unsigned int)rc
>= new_buffer_size
- buffer
->content_size
)
278 new_buffer_size
*= 2;
279 if (!(new_buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
->buffer
, new_buffer_size
)))
281 ERR("Failed to grow buffer.\n");
282 buffer
->buffer
[buffer
->content_size
] = '\0';
285 buffer
->buffer
= new_buffer
;
286 buffer
->buffer_size
= new_buffer_size
;
290 int shader_vaddline(struct wined3d_string_buffer
*buffer
, const char *format
, va_list args
)
295 rem
= buffer
->buffer_size
- buffer
->content_size
;
296 rc
= vsnprintf(&buffer
->buffer
[buffer
->content_size
], rem
, format
, args
);
297 if (rc
< 0 /* C89 */ || (unsigned int)rc
>= rem
/* C99 */)
300 buffer
->content_size
+= rc
;
304 int shader_addline(struct wined3d_string_buffer
*buffer
, const char *format
, ...)
311 va_start(args
, format
);
312 ret
= shader_vaddline(buffer
, format
, args
);
316 if (!string_buffer_resize(buffer
, ret
))
321 struct wined3d_string_buffer
*string_buffer_get(struct wined3d_string_buffer_list
*list
)
323 struct wined3d_string_buffer
*buffer
;
325 if (list_empty(&list
->list
))
327 buffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer
));
328 if (!buffer
|| !string_buffer_init(buffer
))
330 ERR("Couldn't allocate buffer for temporary string.\n");
332 HeapFree(GetProcessHeap(), 0, buffer
);
338 buffer
= LIST_ENTRY(list_head(&list
->list
), struct wined3d_string_buffer
, entry
);
339 list_remove(&buffer
->entry
);
341 string_buffer_clear(buffer
);
345 static int string_buffer_vsprintf(struct wined3d_string_buffer
*buffer
, const char *format
, va_list args
)
349 string_buffer_clear(buffer
);
350 return shader_vaddline(buffer
, format
, args
);
353 void string_buffer_sprintf(struct wined3d_string_buffer
*buffer
, const char *format
, ...)
360 va_start(args
, format
);
361 ret
= string_buffer_vsprintf(buffer
, format
, args
);
365 if (!string_buffer_resize(buffer
, ret
))
370 void string_buffer_release(struct wined3d_string_buffer_list
*list
, struct wined3d_string_buffer
*buffer
)
374 list_add_head(&list
->list
, &buffer
->entry
);
377 void string_buffer_list_init(struct wined3d_string_buffer_list
*list
)
379 list_init(&list
->list
);
382 void string_buffer_list_cleanup(struct wined3d_string_buffer_list
*list
)
384 struct wined3d_string_buffer
*buffer
, *buffer_next
;
386 LIST_FOR_EACH_ENTRY_SAFE(buffer
, buffer_next
, &list
->list
, struct wined3d_string_buffer
, entry
)
388 string_buffer_free(buffer
);
389 HeapFree(GetProcessHeap(), 0, buffer
);
391 list_init(&list
->list
);
394 /* Convert floating point offset relative to a register file to an absolute
395 * offset for float constants. */
396 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type
, UINT register_idx
)
398 switch (register_type
)
400 case WINED3DSPR_CONST
: return register_idx
;
401 case WINED3DSPR_CONST2
: return 2048 + register_idx
;
402 case WINED3DSPR_CONST3
: return 4096 + register_idx
;
403 case WINED3DSPR_CONST4
: return 6144 + register_idx
;
405 FIXME("Unsupported register type: %u.\n", register_type
);
410 static void shader_delete_constant_list(struct list
*clist
)
412 struct wined3d_shader_lconst
*constant
, *constant_next
;
414 LIST_FOR_EACH_ENTRY_SAFE(constant
, constant_next
, clist
, struct wined3d_shader_lconst
, entry
)
415 HeapFree(GetProcessHeap(), 0, constant
);
419 static void shader_set_limits(struct wined3d_shader
*shader
)
421 static const struct limits_entry
423 unsigned int min_version
;
424 unsigned int max_version
;
425 struct wined3d_shader_limits limits
;
429 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
430 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
431 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
432 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
433 * even though they are capable of supporting much more (GL
434 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
435 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
437 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
438 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
443 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
444 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
449 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
450 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
451 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
452 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
453 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
454 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 12}},
455 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 0, 32}},
458 const struct limits_entry
*limits_array
;
459 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
460 shader
->reg_maps
.shader_version
.minor
);
463 switch (shader
->reg_maps
.shader_version
.type
)
466 FIXME("Unexpected shader type %u found.\n", shader
->reg_maps
.shader_version
.type
);
468 case WINED3D_SHADER_TYPE_VERTEX
:
469 limits_array
= vs_limits
;
471 case WINED3D_SHADER_TYPE_GEOMETRY
:
472 limits_array
= gs_limits
;
474 case WINED3D_SHADER_TYPE_PIXEL
:
475 limits_array
= ps_limits
;
479 while (limits_array
[i
].min_version
&& limits_array
[i
].min_version
<= shader_version
)
481 if (shader_version
<= limits_array
[i
].max_version
)
483 shader
->limits
= &limits_array
[i
].limits
;
490 FIXME("Unexpected shader version \"%u.%u\".\n",
491 shader
->reg_maps
.shader_version
.major
,
492 shader
->reg_maps
.shader_version
.minor
);
493 shader
->limits
= &limits_array
[max(0, i
- 1)].limits
;
497 static inline void set_bitmap_bit(DWORD
*bitmap
, DWORD bit
)
502 bitmap
[idx
] |= (1u << shift
);
505 static BOOL
shader_record_register_usage(struct wined3d_shader
*shader
, struct wined3d_shader_reg_maps
*reg_maps
,
506 const struct wined3d_shader_register
*reg
, enum wined3d_shader_type shader_type
, unsigned int constf_size
)
510 case WINED3DSPR_TEXTURE
: /* WINED3DSPR_ADDR */
511 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
512 reg_maps
->texcoord
|= 1u << reg
->idx
[0].offset
;
514 reg_maps
->address
|= 1u << reg
->idx
[0].offset
;
517 case WINED3DSPR_TEMP
:
518 reg_maps
->temporary
|= 1u << reg
->idx
[0].offset
;
521 case WINED3DSPR_INPUT
:
522 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
524 if (reg
->idx
[0].rel_addr
)
526 /* If relative addressing is used, we must assume that all registers
527 * are used. Even if it is a construct like v3[aL], we can't assume
528 * that v0, v1 and v2 aren't read because aL can be negative */
530 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
532 shader
->u
.ps
.input_reg_used
[i
] = TRUE
;
537 shader
->u
.ps
.input_reg_used
[reg
->idx
[0].offset
] = TRUE
;
541 reg_maps
->input_registers
|= 1u << reg
->idx
[0].offset
;
544 case WINED3DSPR_RASTOUT
:
545 if (reg
->idx
[0].offset
== 1)
547 if (reg
->idx
[0].offset
== 2)
548 reg_maps
->point_size
= 1;
551 case WINED3DSPR_MISCTYPE
:
552 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
554 if (!reg
->idx
[0].offset
)
556 else if (reg
->idx
[0].offset
== 1)
557 reg_maps
->usesfacing
= 1;
561 case WINED3DSPR_CONST
:
562 if (reg
->idx
[0].rel_addr
)
564 if (reg
->idx
[0].offset
< reg_maps
->min_rel_offset
)
565 reg_maps
->min_rel_offset
= reg
->idx
[0].offset
;
566 if (reg
->idx
[0].offset
> reg_maps
->max_rel_offset
)
567 reg_maps
->max_rel_offset
= reg
->idx
[0].offset
;
568 reg_maps
->usesrelconstF
= TRUE
;
572 if (reg
->idx
[0].offset
>= min(shader
->limits
->constant_float
, constf_size
))
574 WARN("Shader using float constant %u which is not supported.\n", reg
->idx
[0].offset
);
579 set_bitmap_bit(reg_maps
->constf
, reg
->idx
[0].offset
);
584 case WINED3DSPR_CONSTINT
:
585 if (reg
->idx
[0].offset
>= shader
->limits
->constant_int
)
587 WARN("Shader using integer constant %u which is not supported.\n", reg
->idx
[0].offset
);
592 reg_maps
->integer_constants
|= (1u << reg
->idx
[0].offset
);
596 case WINED3DSPR_CONSTBOOL
:
597 if (reg
->idx
[0].offset
>= shader
->limits
->constant_bool
)
599 WARN("Shader using bool constant %u which is not supported.\n", reg
->idx
[0].offset
);
604 reg_maps
->boolean_constants
|= (1u << reg
->idx
[0].offset
);
608 case WINED3DSPR_COLOROUT
:
609 reg_maps
->rt_mask
|= (1u << reg
->idx
[0].offset
);
613 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
614 reg
->type
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
620 static void shader_record_sample(struct wined3d_shader_reg_maps
*reg_maps
,
621 unsigned int resource_idx
, unsigned int sampler_idx
, unsigned int bind_idx
)
623 struct wined3d_shader_sampler_map_entry
*entries
, *entry
;
624 struct wined3d_shader_sampler_map
*map
;
627 map
= ®_maps
->sampler_map
;
628 entries
= map
->entries
;
629 for (i
= 0; i
< map
->count
; ++i
)
631 if (entries
[i
].resource_idx
== resource_idx
&& entries
[i
].sampler_idx
== sampler_idx
)
637 if (!(entries
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entries
) * 4)))
639 ERR("Failed to allocate sampler map entries.\n");
643 map
->entries
= entries
;
645 else if (map
->count
== map
->size
)
647 size_t new_size
= map
->size
* 2;
649 if (sizeof(*entries
) * new_size
<= sizeof(*entries
) * map
->size
650 || !(entries
= HeapReAlloc(GetProcessHeap(), 0, entries
, sizeof(*entries
) * new_size
)))
652 ERR("Failed to resize sampler map entries.\n");
655 map
->size
= new_size
;
656 map
->entries
= entries
;
659 entry
= &entries
[map
->count
++];
660 entry
->resource_idx
= resource_idx
;
661 entry
->sampler_idx
= sampler_idx
;
662 entry
->bind_idx
= bind_idx
;
665 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr
, unsigned int param
)
669 case WINED3DSIH_M4x4
:
670 case WINED3DSIH_M3x4
:
671 return param
== 1 ? 3 : 0;
673 case WINED3DSIH_M4x3
:
674 case WINED3DSIH_M3x3
:
675 return param
== 1 ? 2 : 0;
677 case WINED3DSIH_M3x2
:
678 return param
== 1 ? 1 : 0;
685 /* Note that this does not count the loop register as an address register. */
686 static HRESULT
shader_get_registers_used(struct wined3d_shader
*shader
, const struct wined3d_shader_frontend
*fe
,
687 struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_shader_signature
*input_signature
,
688 struct wined3d_shader_signature
*output_signature
, const DWORD
*byte_code
, DWORD constf_size
)
690 struct wined3d_shader_signature_element input_signature_elements
[max(MAX_ATTRIBS
, MAX_REG_INPUT
)];
691 struct wined3d_shader_signature_element output_signature_elements
[MAX_REG_OUTPUT
];
692 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
693 void *fe_data
= shader
->frontend_data
;
694 struct wined3d_shader_version shader_version
;
695 const DWORD
*ptr
= byte_code
;
698 memset(reg_maps
, 0, sizeof(*reg_maps
));
699 memset(input_signature_elements
, 0, sizeof(input_signature_elements
));
700 memset(output_signature_elements
, 0, sizeof(output_signature_elements
));
701 reg_maps
->min_rel_offset
= ~0U;
703 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
704 reg_maps
->shader_version
= shader_version
;
706 shader_set_limits(shader
);
708 reg_maps
->constf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
709 sizeof(*reg_maps
->constf
) * ((min(shader
->limits
->constant_float
, constf_size
) + 31) / 32));
710 if (!reg_maps
->constf
)
712 ERR("Failed to allocate constant map memory.\n");
713 return E_OUTOFMEMORY
;
716 while (!fe
->shader_is_end(fe_data
, &ptr
))
718 struct wined3d_shader_instruction ins
;
721 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
723 /* Unhandled opcode, and its parameters. */
724 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
726 TRACE("Skipping unrecognized instruction.\n");
730 /* Handle declarations. */
731 if (ins
.handler_idx
== WINED3DSIH_DCL
)
733 struct wined3d_shader_semantic
*semantic
= &ins
.declaration
.semantic
;
734 unsigned int reg_idx
= semantic
->reg
.reg
.idx
[0].offset
;
736 switch (semantic
->reg
.reg
.type
)
738 /* Mark input registers used. */
739 case WINED3DSPR_INPUT
:
740 if (reg_idx
>= MAX_REG_INPUT
)
742 ERR("Invalid input register index %u.\n", reg_idx
);
745 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
&& shader_version
.major
== 3
746 && semantic
->usage
== WINED3D_DECL_USAGE_POSITION
&& !semantic
->usage_idx
)
747 return WINED3DERR_INVALIDCALL
;
748 reg_maps
->input_registers
|= 1u << reg_idx
;
749 shader_signature_from_semantic(&input_signature_elements
[reg_idx
], semantic
);
752 /* Vertex shader: mark 3.0 output registers used, save token. */
753 case WINED3DSPR_OUTPUT
:
754 if (reg_idx
>= MAX_REG_OUTPUT
)
756 ERR("Invalid output register index %u.\n", reg_idx
);
759 reg_maps
->output_registers
|= 1u << reg_idx
;
760 shader_signature_from_semantic(&output_signature_elements
[reg_idx
], semantic
);
761 if (semantic
->usage
== WINED3D_DECL_USAGE_FOG
)
763 if (semantic
->usage
== WINED3D_DECL_USAGE_PSIZE
)
764 reg_maps
->point_size
= 1;
767 case WINED3DSPR_SAMPLER
:
768 shader_record_sample(reg_maps
, reg_idx
, reg_idx
, reg_idx
);
769 case WINED3DSPR_RESOURCE
:
770 if (reg_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
772 ERR("Invalid resource index %u.\n", reg_idx
);
775 reg_maps
->resource_info
[reg_idx
].type
= semantic
->resource_type
;
776 reg_maps
->resource_info
[reg_idx
].data_type
= semantic
->resource_data_type
;
780 TRACE("Not recording DCL register type %#x.\n", semantic
->reg
.reg
.type
);
784 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
786 struct wined3d_shader_register
*reg
= &ins
.declaration
.src
.reg
;
787 if (reg
->idx
[0].offset
>= WINED3D_MAX_CBS
)
788 ERR("Invalid CB index %u.\n", reg
->idx
[0].offset
);
790 reg_maps
->cb_sizes
[reg
->idx
[0].offset
] = reg
->idx
[1].offset
;
792 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
)
794 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
795 shader
->u
.gs
.input_type
= ins
.declaration
.primitive_type
;
797 FIXME("Invalid instruction %#x for shader type %#x.\n",
798 ins
.handler_idx
, shader_version
.type
);
800 else if (ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
802 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
803 shader
->u
.gs
.output_type
= ins
.declaration
.primitive_type
;
805 FIXME("Invalid instruction %#x for shader type %#x.\n",
806 ins
.handler_idx
, shader_version
.type
);
808 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
810 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
811 shader
->u
.gs
.vertices_out
= ins
.declaration
.count
;
813 FIXME("Invalid instruction %#x for shader type %#x.\n",
814 ins
.handler_idx
, shader_version
.type
);
816 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
818 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
820 if (!lconst
) return E_OUTOFMEMORY
;
822 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
823 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
824 value
= (float *)lconst
->value
;
826 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
827 if (shader_version
.major
== 1 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
829 if (value
[0] < -1.0f
) value
[0] = -1.0f
;
830 else if (value
[0] > 1.0f
) value
[0] = 1.0f
;
831 if (value
[1] < -1.0f
) value
[1] = -1.0f
;
832 else if (value
[1] > 1.0f
) value
[1] = 1.0f
;
833 if (value
[2] < -1.0f
) value
[2] = -1.0f
;
834 else if (value
[2] > 1.0f
) value
[2] = 1.0f
;
835 if (value
[3] < -1.0f
) value
[3] = -1.0f
;
836 else if (value
[3] > 1.0f
) value
[3] = 1.0f
;
839 list_add_head(&shader
->constantsF
, &lconst
->entry
);
841 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
842 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
844 shader
->lconst_inf_or_nan
= TRUE
;
847 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
849 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
850 if (!lconst
) return E_OUTOFMEMORY
;
852 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
853 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
855 list_add_head(&shader
->constantsI
, &lconst
->entry
);
856 reg_maps
->local_int_consts
|= (1u << lconst
->idx
);
858 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
860 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
861 if (!lconst
) return E_OUTOFMEMORY
;
863 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
864 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, sizeof(DWORD
));
866 list_add_head(&shader
->constantsB
, &lconst
->entry
);
867 reg_maps
->local_bool_consts
|= (1u << lconst
->idx
);
869 /* For subroutine prototypes. */
870 else if (ins
.handler_idx
== WINED3DSIH_LABEL
)
872 reg_maps
->labels
|= 1u << ins
.src
[0].reg
.idx
[0].offset
;
874 /* Set texture, address, temporary registers. */
877 BOOL color0_mov
= FALSE
;
880 /* This will loop over all the registers and try to
881 * make a bitmask of the ones we're interested in.
883 * Relative addressing tokens are ignored, but that's
884 * okay, since we'll catch any address registers when
885 * they are initialized (required by spec). */
886 for (i
= 0; i
< ins
.dst_count
; ++i
)
888 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.dst
[i
].reg
,
889 shader_version
.type
, constf_size
))
890 return WINED3DERR_INVALIDCALL
;
892 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
893 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
894 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
895 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
&& shader_version
.major
< 3)
897 UINT idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
899 switch (ins
.dst
[i
].reg
.type
)
901 case WINED3DSPR_RASTOUT
:
905 reg_maps
->output_registers
|= 1u << 10;
906 shader_signature_from_usage(&output_signature_elements
[10],
907 WINED3D_DECL_USAGE_POSITION
, 0, 10, WINED3DSP_WRITEMASK_ALL
);
911 reg_maps
->output_registers
|= 1u << 11;
912 shader_signature_from_usage(&output_signature_elements
[11],
913 WINED3D_DECL_USAGE_FOG
, 0, 11, WINED3DSP_WRITEMASK_0
);
917 reg_maps
->output_registers
|= 1u << 11;
918 shader_signature_from_usage(&output_signature_elements
[11],
919 WINED3D_DECL_USAGE_PSIZE
, 0, 11, WINED3DSP_WRITEMASK_1
);
924 case WINED3DSPR_ATTROUT
:
928 if (reg_maps
->output_registers
& (1u << idx
))
930 output_signature_elements
[idx
].mask
|= ins
.dst
[i
].write_mask
;
934 reg_maps
->output_registers
|= 1u << idx
;
935 shader_signature_from_usage(&output_signature_elements
[idx
],
936 WINED3D_DECL_USAGE_COLOR
, idx
- 8, idx
, ins
.dst
[i
].write_mask
);
941 case WINED3DSPR_TEXCRDOUT
:
943 reg_maps
->texcoord_mask
[idx
] |= ins
.dst
[i
].write_mask
;
944 if (reg_maps
->output_registers
& (1u << idx
))
946 output_signature_elements
[idx
].mask
|= ins
.dst
[i
].write_mask
;
950 reg_maps
->output_registers
|= 1u << idx
;
951 shader_signature_from_usage(&output_signature_elements
[idx
],
952 WINED3D_DECL_USAGE_TEXCOORD
, idx
, idx
, ins
.dst
[i
].write_mask
);
961 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
963 if (ins
.dst
[i
].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
.dst
[i
].reg
.idx
[0].offset
)
965 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
966 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
967 * the mov and perform the sRGB write correction from the source register.
969 * However, if the mov is only partial, we can't do this, and if the write
970 * comes from an instruction other than MOV it is hard to do as well. If
971 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
972 shader
->u
.ps
.color0_mov
= FALSE
;
973 if (ins
.handler_idx
== WINED3DSIH_MOV
974 && ins
.dst
[i
].write_mask
== WINED3DSP_WRITEMASK_ALL
)
976 /* Used later when the source register is read. */
980 /* Also drop the MOV marker if the source register is overwritten prior to the shader
983 else if (ins
.dst
[i
].reg
.type
== WINED3DSPR_TEMP
984 && ins
.dst
[i
].reg
.idx
[0].offset
== shader
->u
.ps
.color0_reg
)
986 shader
->u
.ps
.color0_mov
= FALSE
;
990 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
991 if (shader_version
.major
== 1
992 && (ins
.handler_idx
== WINED3DSIH_TEX
993 || ins
.handler_idx
== WINED3DSIH_TEXBEM
994 || ins
.handler_idx
== WINED3DSIH_TEXBEML
995 || ins
.handler_idx
== WINED3DSIH_TEXDP3TEX
996 || ins
.handler_idx
== WINED3DSIH_TEXM3x2TEX
997 || ins
.handler_idx
== WINED3DSIH_TEXM3x3SPEC
998 || ins
.handler_idx
== WINED3DSIH_TEXM3x3TEX
999 || ins
.handler_idx
== WINED3DSIH_TEXM3x3VSPEC
1000 || ins
.handler_idx
== WINED3DSIH_TEXREG2AR
1001 || ins
.handler_idx
== WINED3DSIH_TEXREG2GB
1002 || ins
.handler_idx
== WINED3DSIH_TEXREG2RGB
))
1004 unsigned int reg_idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
1006 TRACE("Setting fake 2D resource for 1.x pixelshader.\n");
1007 reg_maps
->resource_info
[reg_idx
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
1008 reg_maps
->resource_info
[reg_idx
].data_type
= WINED3D_DATA_FLOAT
;
1009 shader_record_sample(reg_maps
, reg_idx
, reg_idx
, reg_idx
);
1011 /* texbem is only valid with < 1.4 pixel shaders */
1012 if (ins
.handler_idx
== WINED3DSIH_TEXBEM
1013 || ins
.handler_idx
== WINED3DSIH_TEXBEML
)
1015 reg_maps
->bumpmat
|= 1u << reg_idx
;
1016 if (ins
.handler_idx
== WINED3DSIH_TEXBEML
)
1018 reg_maps
->luminanceparams
|= 1u << reg_idx
;
1022 else if (ins
.handler_idx
== WINED3DSIH_BEM
)
1024 reg_maps
->bumpmat
|= 1u << ins
.dst
[i
].reg
.idx
[0].offset
;
1028 if (ins
.handler_idx
== WINED3DSIH_NRM
) reg_maps
->usesnrm
= 1;
1029 else if (ins
.handler_idx
== WINED3DSIH_DSY
) reg_maps
->usesdsy
= 1;
1030 else if (ins
.handler_idx
== WINED3DSIH_DSX
) reg_maps
->usesdsx
= 1;
1031 else if (ins
.handler_idx
== WINED3DSIH_TEXLDD
) reg_maps
->usestexldd
= 1;
1032 else if (ins
.handler_idx
== WINED3DSIH_TEXLDL
) reg_maps
->usestexldl
= 1;
1033 else if (ins
.handler_idx
== WINED3DSIH_MOVA
) reg_maps
->usesmova
= 1;
1034 else if (ins
.handler_idx
== WINED3DSIH_IFC
) reg_maps
->usesifc
= 1;
1035 else if (ins
.handler_idx
== WINED3DSIH_CALL
) reg_maps
->usescall
= 1;
1036 else if (ins
.handler_idx
== WINED3DSIH_POW
) reg_maps
->usespow
= 1;
1037 else if (ins
.handler_idx
== WINED3DSIH_LOOP
1038 || ins
.handler_idx
== WINED3DSIH_REP
)
1041 if (cur_loop_depth
> max_loop_depth
)
1042 max_loop_depth
= cur_loop_depth
;
1044 else if (ins
.handler_idx
== WINED3DSIH_ENDLOOP
1045 || ins
.handler_idx
== WINED3DSIH_ENDREP
)
1049 else if (ins
.handler_idx
== WINED3DSIH_SAMPLE
1050 || ins
.handler_idx
== WINED3DSIH_SAMPLE_GRAD
1051 || ins
.handler_idx
== WINED3DSIH_SAMPLE_LOD
)
1053 shader_record_sample(reg_maps
, ins
.src
[1].reg
.idx
[0].offset
,
1054 ins
.src
[2].reg
.idx
[0].offset
, reg_maps
->sampler_map
.count
);
1058 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.predicate
->reg
,
1059 shader_version
.type
, constf_size
))
1060 return WINED3DERR_INVALIDCALL
;
1062 for (i
= 0; i
< ins
.src_count
; ++i
)
1064 unsigned int count
= get_instr_extra_regcount(ins
.handler_idx
, i
);
1065 struct wined3d_shader_register reg
= ins
.src
[i
].reg
;
1067 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.src
[i
].reg
,
1068 shader_version
.type
, constf_size
))
1069 return WINED3DERR_INVALIDCALL
;
1072 ++reg
.idx
[0].offset
;
1073 if (!shader_record_register_usage(shader
, reg_maps
, ®
,
1074 shader_version
.type
, constf_size
))
1075 return WINED3DERR_INVALIDCALL
;
1081 if (ins
.src
[i
].reg
.type
== WINED3DSPR_TEMP
1082 && ins
.src
[i
].swizzle
== WINED3DSP_NOSWIZZLE
)
1084 shader
->u
.ps
.color0_mov
= TRUE
;
1085 shader
->u
.ps
.color0_reg
= ins
.src
[i
].reg
.idx
[0].offset
;
1091 reg_maps
->loop_depth
= max_loop_depth
;
1093 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1094 * R0 is written to the render target. */
1095 if (shader_version
.major
< 2 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1096 reg_maps
->rt_mask
|= (1u << 0);
1098 shader
->functionLength
= ((const char *)ptr
- (const char *)byte_code
);
1100 if (input_signature
->elements
)
1102 for (i
= 0; i
< input_signature
->element_count
; ++i
)
1104 reg_maps
->input_registers
|= 1u << input_signature
->elements
[i
].register_idx
;
1105 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
1106 && input_signature
->elements
[i
].sysval_semantic
== WINED3D_SV_POSITION
)
1110 else if (!input_signature
->elements
&& reg_maps
->input_registers
)
1112 unsigned int count
= count_bits(reg_maps
->input_registers
);
1113 struct wined3d_shader_signature_element
*e
;
1116 if (!(input_signature
->elements
= HeapAlloc(GetProcessHeap(), 0, sizeof(*input_signature
->elements
) * count
)))
1117 return E_OUTOFMEMORY
;
1118 input_signature
->element_count
= count
;
1120 e
= input_signature
->elements
;
1121 for (i
= 0; i
< ARRAY_SIZE(input_signature_elements
); ++i
)
1123 if (!(reg_maps
->input_registers
& (1u << i
)))
1125 input_signature_elements
[i
].register_idx
= i
;
1126 *e
++ = input_signature_elements
[i
];
1130 if (output_signature
->elements
)
1132 for (i
= 0; i
< output_signature
->element_count
; ++i
)
1134 reg_maps
->output_registers
|= 1u << output_signature
->elements
[i
].register_idx
;
1137 else if (reg_maps
->output_registers
)
1139 unsigned int count
= count_bits(reg_maps
->output_registers
);
1140 struct wined3d_shader_signature_element
*e
;
1142 if (!(output_signature
->elements
= HeapAlloc(GetProcessHeap(), 0, sizeof(*output_signature
->elements
) * count
)))
1143 return E_OUTOFMEMORY
;
1144 output_signature
->element_count
= count
;
1146 e
= output_signature
->elements
;
1147 for (i
= 0; i
< ARRAY_SIZE(output_signature_elements
); ++i
)
1149 if (!(reg_maps
->output_registers
& (1u << i
)))
1151 *e
++ = output_signature_elements
[i
];
1158 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int max
)
1160 DWORD map
= 1u << max
;
1162 map
&= reg_maps
->shader_version
.major
< 3 ? ~reg_maps
->texcoord
: ~reg_maps
->input_registers
;
1164 return wined3d_log2i(map
);
1167 static void shader_dump_decl_usage(const struct wined3d_shader_semantic
*semantic
,
1168 const struct wined3d_shader_version
*shader_version
)
1172 if (semantic
->reg
.reg
.type
== WINED3DSPR_SAMPLER
)
1174 switch (semantic
->resource_type
)
1176 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
1180 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
1184 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
1189 TRACE("_unknown_ttype(0x%08x)", semantic
->resource_type
);
1193 else if (semantic
->reg
.reg
.type
== WINED3DSPR_RESOURCE
)
1195 TRACE("_resource_");
1196 switch (semantic
->resource_type
)
1198 case WINED3D_SHADER_RESOURCE_BUFFER
:
1202 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
1206 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
1210 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
:
1211 TRACE("texture2dms");
1214 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
1218 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
1219 TRACE("texturecube");
1222 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
:
1223 TRACE("texture1darray");
1226 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
1227 TRACE("texture2darray");
1230 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY
:
1231 TRACE("texture2dmsarray");
1238 switch (semantic
->resource_data_type
)
1240 case WINED3D_DATA_FLOAT
:
1244 case WINED3D_DATA_INT
:
1248 case WINED3D_DATA_UINT
:
1252 case WINED3D_DATA_UNORM
:
1256 case WINED3D_DATA_SNORM
:
1261 TRACE(" (unknown)");
1267 /* Pixel shaders 3.0 don't have usage semantics. */
1268 if (shader_version
->major
< 3 && shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
) return;
1271 switch (semantic
->usage
)
1273 case WINED3D_DECL_USAGE_POSITION
:
1274 TRACE("position%u", semantic
->usage_idx
);
1277 case WINED3D_DECL_USAGE_BLEND_INDICES
:
1281 case WINED3D_DECL_USAGE_BLEND_WEIGHT
:
1285 case WINED3D_DECL_USAGE_NORMAL
:
1286 TRACE("normal%u", semantic
->usage_idx
);
1289 case WINED3D_DECL_USAGE_PSIZE
:
1293 case WINED3D_DECL_USAGE_COLOR
:
1294 if (!semantic
->usage_idx
) TRACE("color");
1295 else TRACE("specular%u", (semantic
->usage_idx
- 1));
1298 case WINED3D_DECL_USAGE_TEXCOORD
:
1299 TRACE("texture%u", semantic
->usage_idx
);
1302 case WINED3D_DECL_USAGE_TANGENT
:
1306 case WINED3D_DECL_USAGE_BINORMAL
:
1310 case WINED3D_DECL_USAGE_TESS_FACTOR
:
1311 TRACE("tessfactor");
1314 case WINED3D_DECL_USAGE_POSITIONT
:
1315 TRACE("positionT%u", semantic
->usage_idx
);
1318 case WINED3D_DECL_USAGE_FOG
:
1322 case WINED3D_DECL_USAGE_DEPTH
:
1326 case WINED3D_DECL_USAGE_SAMPLE
:
1331 FIXME("unknown_semantics(0x%08x)", semantic
->usage
);
1336 static void shader_dump_register(const struct wined3d_shader_register
*reg
,
1337 const struct wined3d_shader_version
*shader_version
)
1339 static const char * const rastout_reg_names
[] = {"oPos", "oFog", "oPts"};
1340 static const char * const misctype_reg_names
[] = {"vPos", "vFace"};
1341 UINT offset
= reg
->idx
[0].offset
;
1345 case WINED3DSPR_TEMP
:
1349 case WINED3DSPR_INPUT
:
1353 case WINED3DSPR_CONST
:
1354 case WINED3DSPR_CONST2
:
1355 case WINED3DSPR_CONST3
:
1356 case WINED3DSPR_CONST4
:
1358 offset
= shader_get_float_offset(reg
->type
, offset
);
1361 case WINED3DSPR_TEXTURE
: /* vs: case WINED3DSPR_ADDR */
1362 TRACE("%c", shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
? 't' : 'a');
1365 case WINED3DSPR_RASTOUT
:
1366 TRACE("%s", rastout_reg_names
[offset
]);
1369 case WINED3DSPR_COLOROUT
:
1373 case WINED3DSPR_DEPTHOUT
:
1377 case WINED3DSPR_ATTROUT
:
1381 case WINED3DSPR_TEXCRDOUT
:
1382 /* Vertex shaders >= 3.0 use general purpose output registers
1383 * (WINED3DSPR_OUTPUT), which can include an address token. */
1384 if (shader_version
->major
>= 3) TRACE("o");
1388 case WINED3DSPR_CONSTINT
:
1392 case WINED3DSPR_CONSTBOOL
:
1396 case WINED3DSPR_LABEL
:
1400 case WINED3DSPR_LOOP
:
1404 case WINED3DSPR_SAMPLER
:
1408 case WINED3DSPR_MISCTYPE
:
1410 FIXME("Unhandled misctype register %u.\n", offset
);
1412 TRACE("%s", misctype_reg_names
[offset
]);
1415 case WINED3DSPR_PREDICATE
:
1419 case WINED3DSPR_IMMCONST
:
1423 case WINED3DSPR_CONSTBUFFER
:
1427 case WINED3DSPR_PRIMID
:
1431 case WINED3DSPR_NULL
:
1435 case WINED3DSPR_RESOURCE
:
1440 TRACE("unhandled_rtype(%#x)", reg
->type
);
1444 if (reg
->type
== WINED3DSPR_IMMCONST
)
1447 switch (reg
->immconst_type
)
1449 case WINED3D_IMMCONST_SCALAR
:
1450 switch (reg
->data_type
)
1452 case WINED3D_DATA_FLOAT
:
1453 TRACE("%.8e", *(const float *)reg
->immconst_data
);
1455 case WINED3D_DATA_INT
:
1456 TRACE("%d", reg
->immconst_data
[0]);
1458 case WINED3D_DATA_RESOURCE
:
1459 case WINED3D_DATA_SAMPLER
:
1460 case WINED3D_DATA_UINT
:
1461 TRACE("%u", reg
->immconst_data
[0]);
1464 TRACE("<unhandled data type %#x>", reg
->data_type
);
1469 case WINED3D_IMMCONST_VEC4
:
1470 switch (reg
->data_type
)
1472 case WINED3D_DATA_FLOAT
:
1473 TRACE("%.8e, %.8e, %.8e, %.8e",
1474 *(const float *)®
->immconst_data
[0], *(const float *)®
->immconst_data
[1],
1475 *(const float *)®
->immconst_data
[2], *(const float *)®
->immconst_data
[3]);
1477 case WINED3D_DATA_INT
:
1478 TRACE("%d, %d, %d, %d",
1479 reg
->immconst_data
[0], reg
->immconst_data
[1],
1480 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1482 case WINED3D_DATA_RESOURCE
:
1483 case WINED3D_DATA_SAMPLER
:
1484 case WINED3D_DATA_UINT
:
1485 TRACE("%u, %u, %u, %u",
1486 reg
->immconst_data
[0], reg
->immconst_data
[1],
1487 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1490 TRACE("<unhandled data type %#x>", reg
->data_type
);
1496 TRACE("<unhandled immconst_type %#x>", reg
->immconst_type
);
1501 else if (reg
->type
!= WINED3DSPR_RASTOUT
1502 && reg
->type
!= WINED3DSPR_MISCTYPE
1503 && reg
->type
!= WINED3DSPR_NULL
)
1508 if (reg
->idx
[0].rel_addr
)
1510 shader_dump_src_param(reg
->idx
[0].rel_addr
, shader_version
);
1513 TRACE("%u]", offset
);
1515 if (reg
->idx
[1].offset
!= ~0U)
1518 if (reg
->idx
[1].rel_addr
)
1520 shader_dump_src_param(reg
->idx
[1].rel_addr
, shader_version
);
1523 TRACE("%u]", reg
->idx
[1].offset
);
1529 void shader_dump_dst_param(const struct wined3d_shader_dst_param
*param
,
1530 const struct wined3d_shader_version
*shader_version
)
1532 DWORD write_mask
= param
->write_mask
;
1534 shader_dump_register(¶m
->reg
, shader_version
);
1536 if (write_mask
&& write_mask
!= WINED3DSP_WRITEMASK_ALL
)
1538 static const char write_mask_chars
[] = "xyzw";
1541 if (write_mask
& WINED3DSP_WRITEMASK_0
) TRACE("%c", write_mask_chars
[0]);
1542 if (write_mask
& WINED3DSP_WRITEMASK_1
) TRACE("%c", write_mask_chars
[1]);
1543 if (write_mask
& WINED3DSP_WRITEMASK_2
) TRACE("%c", write_mask_chars
[2]);
1544 if (write_mask
& WINED3DSP_WRITEMASK_3
) TRACE("%c", write_mask_chars
[3]);
1548 void shader_dump_src_param(const struct wined3d_shader_src_param
*param
,
1549 const struct wined3d_shader_version
*shader_version
)
1551 enum wined3d_shader_src_modifier src_modifier
= param
->modifiers
;
1552 DWORD swizzle
= param
->swizzle
;
1554 if (src_modifier
== WINED3DSPSM_NEG
1555 || src_modifier
== WINED3DSPSM_BIASNEG
1556 || src_modifier
== WINED3DSPSM_SIGNNEG
1557 || src_modifier
== WINED3DSPSM_X2NEG
1558 || src_modifier
== WINED3DSPSM_ABSNEG
)
1560 else if (src_modifier
== WINED3DSPSM_COMP
)
1562 else if (src_modifier
== WINED3DSPSM_NOT
)
1565 if (src_modifier
== WINED3DSPSM_ABS
|| src_modifier
== WINED3DSPSM_ABSNEG
)
1568 shader_dump_register(¶m
->reg
, shader_version
);
1570 switch (src_modifier
)
1572 case WINED3DSPSM_NONE
: break;
1573 case WINED3DSPSM_NEG
: break;
1574 case WINED3DSPSM_NOT
: break;
1575 case WINED3DSPSM_BIAS
: TRACE("_bias"); break;
1576 case WINED3DSPSM_BIASNEG
: TRACE("_bias"); break;
1577 case WINED3DSPSM_SIGN
: TRACE("_bx2"); break;
1578 case WINED3DSPSM_SIGNNEG
: TRACE("_bx2"); break;
1579 case WINED3DSPSM_COMP
: break;
1580 case WINED3DSPSM_X2
: TRACE("_x2"); break;
1581 case WINED3DSPSM_X2NEG
: TRACE("_x2"); break;
1582 case WINED3DSPSM_DZ
: TRACE("_dz"); break;
1583 case WINED3DSPSM_DW
: TRACE("_dw"); break;
1584 case WINED3DSPSM_ABSNEG
: TRACE(")"); break;
1585 case WINED3DSPSM_ABS
: TRACE(")"); break;
1586 default: TRACE("_unknown_modifier(%#x)", src_modifier
);
1589 if (swizzle
!= WINED3DSP_NOSWIZZLE
)
1591 static const char swizzle_chars
[] = "xyzw";
1592 DWORD swizzle_x
= swizzle
& 0x03;
1593 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
1594 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
1595 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
1597 if (swizzle_x
== swizzle_y
1598 && swizzle_x
== swizzle_z
1599 && swizzle_x
== swizzle_w
)
1601 TRACE(".%c", swizzle_chars
[swizzle_x
]);
1605 TRACE(".%c%c%c%c", swizzle_chars
[swizzle_x
], swizzle_chars
[swizzle_y
],
1606 swizzle_chars
[swizzle_z
], swizzle_chars
[swizzle_w
]);
1611 /* Shared code in order to generate the bulk of the shader string.
1612 * NOTE: A description of how to parse tokens can be found on MSDN. */
1613 void shader_generate_main(const struct wined3d_shader
*shader
, struct wined3d_string_buffer
*buffer
,
1614 const struct wined3d_shader_reg_maps
*reg_maps
, const DWORD
*byte_code
, void *backend_ctx
)
1616 struct wined3d_device
*device
= shader
->device
;
1617 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
1618 void *fe_data
= shader
->frontend_data
;
1619 struct wined3d_shader_version shader_version
;
1620 struct wined3d_shader_loop_state loop_state
;
1621 struct wined3d_shader_instruction ins
;
1622 struct wined3d_shader_tex_mx tex_mx
;
1623 struct wined3d_shader_context ctx
;
1624 const DWORD
*ptr
= byte_code
;
1626 /* Initialize current parsing state. */
1627 tex_mx
.current_row
= 0;
1628 loop_state
.current_depth
= 0;
1629 loop_state
.current_reg
= 0;
1631 ctx
.shader
= shader
;
1632 ctx
.gl_info
= &device
->adapter
->gl_info
;
1633 ctx
.reg_maps
= reg_maps
;
1634 ctx
.buffer
= buffer
;
1635 ctx
.tex_mx
= &tex_mx
;
1636 ctx
.loop_state
= &loop_state
;
1637 ctx
.backend_data
= backend_ctx
;
1640 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1642 while (!fe
->shader_is_end(fe_data
, &ptr
))
1645 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1647 /* Unknown opcode and its parameters. */
1648 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1650 TRACE("Skipping unrecognized instruction.\n");
1655 FIXME("Predicates not implemented.\n");
1657 /* Call appropriate function for output target */
1658 device
->shader_backend
->shader_handle_instruction(&ins
);
1662 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param
*dst
)
1664 DWORD mmask
= dst
->modifiers
;
1669 case 13: TRACE("_d8"); break;
1670 case 14: TRACE("_d4"); break;
1671 case 15: TRACE("_d2"); break;
1672 case 1: TRACE("_x2"); break;
1673 case 2: TRACE("_x4"); break;
1674 case 3: TRACE("_x8"); break;
1675 default: TRACE("_unhandled_shift(%d)", dst
->shift
); break;
1678 if (mmask
& WINED3DSPDM_SATURATE
) TRACE("_sat");
1679 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) TRACE("_pp");
1680 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) TRACE("_centroid");
1682 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
1683 if (mmask
) FIXME("_unrecognized_modifier(%#x)", mmask
);
1686 static void shader_dump_primitive_type(enum wined3d_primitive_type primitive_type
)
1688 switch (primitive_type
)
1690 case WINED3D_PT_UNDEFINED
:
1693 case WINED3D_PT_POINTLIST
:
1696 case WINED3D_PT_LINELIST
:
1699 case WINED3D_PT_LINESTRIP
:
1702 case WINED3D_PT_TRIANGLELIST
:
1703 TRACE("trianglelist");
1705 case WINED3D_PT_TRIANGLESTRIP
:
1706 TRACE("trianglestrip");
1708 case WINED3D_PT_TRIANGLEFAN
:
1709 TRACE("trianglefan");
1711 case WINED3D_PT_LINELIST_ADJ
:
1712 TRACE("linelist_adj");
1714 case WINED3D_PT_LINESTRIP_ADJ
:
1715 TRACE("linestrip_adj");
1717 case WINED3D_PT_TRIANGLELIST_ADJ
:
1718 TRACE("trianglelist_adj");
1720 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
1721 TRACE("trianglestrip_adj");
1724 TRACE("<unrecognized_primitive_type %#x>", primitive_type
);
1729 static void shader_trace_init(const struct wined3d_shader_frontend
*fe
, void *fe_data
, const DWORD
*byte_code
)
1731 struct wined3d_shader_version shader_version
;
1732 const DWORD
*ptr
= byte_code
;
1733 const char *type_prefix
;
1736 TRACE("Parsing %p.\n", byte_code
);
1738 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1740 switch (shader_version
.type
)
1742 case WINED3D_SHADER_TYPE_VERTEX
:
1746 case WINED3D_SHADER_TYPE_GEOMETRY
:
1750 case WINED3D_SHADER_TYPE_PIXEL
:
1755 FIXME("Unhandled shader type %#x.\n", shader_version
.type
);
1756 type_prefix
= "unknown";
1760 TRACE("%s_%u_%u\n", type_prefix
, shader_version
.major
, shader_version
.minor
);
1762 while (!fe
->shader_is_end(fe_data
, &ptr
))
1764 struct wined3d_shader_instruction ins
;
1766 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1767 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1769 TRACE("Skipping unrecognized instruction.\n");
1773 if (ins
.handler_idx
== WINED3DSIH_DCL
)
1775 shader_dump_decl_usage(&ins
.declaration
.semantic
, &shader_version
);
1776 shader_dump_ins_modifiers(&ins
.declaration
.semantic
.reg
);
1778 shader_dump_dst_param(&ins
.declaration
.semantic
.reg
, &shader_version
);
1780 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
1782 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1783 shader_dump_src_param(&ins
.declaration
.src
, &shader_version
);
1784 TRACE(", %s", ins
.flags
& WINED3DSI_INDEXED_DYNAMIC
? "dynamicIndexed" : "immediateIndexed");
1786 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
1787 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
1789 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1790 shader_dump_primitive_type(ins
.declaration
.primitive_type
);
1792 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
1794 TRACE("%s %u", shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.count
);
1796 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
1798 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(ins
.dst
[0].reg
.type
,
1799 ins
.dst
[0].reg
.idx
[0].offset
),
1800 *(const float *)&ins
.src
[0].reg
.immconst_data
[0],
1801 *(const float *)&ins
.src
[0].reg
.immconst_data
[1],
1802 *(const float *)&ins
.src
[0].reg
.immconst_data
[2],
1803 *(const float *)&ins
.src
[0].reg
.immconst_data
[3]);
1805 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
1807 TRACE("defi i%u = %d, %d, %d, %d", ins
.dst
[0].reg
.idx
[0].offset
,
1808 ins
.src
[0].reg
.immconst_data
[0],
1809 ins
.src
[0].reg
.immconst_data
[1],
1810 ins
.src
[0].reg
.immconst_data
[2],
1811 ins
.src
[0].reg
.immconst_data
[3]);
1813 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
1815 TRACE("defb b%u = %s", ins
.dst
[0].reg
.idx
[0].offset
, ins
.src
[0].reg
.immconst_data
[0] ? "true" : "false");
1822 shader_dump_src_param(ins
.predicate
, &shader_version
);
1826 /* PixWin marks instructions with the coissue flag with a '+' */
1827 if (ins
.coissue
) TRACE("+");
1829 TRACE("%s", shader_opcode_names
[ins
.handler_idx
]);
1831 if (ins
.handler_idx
== WINED3DSIH_IFC
1832 || ins
.handler_idx
== WINED3DSIH_BREAKC
)
1836 case WINED3D_SHADER_REL_OP_GT
: TRACE("_gt"); break;
1837 case WINED3D_SHADER_REL_OP_EQ
: TRACE("_eq"); break;
1838 case WINED3D_SHADER_REL_OP_GE
: TRACE("_ge"); break;
1839 case WINED3D_SHADER_REL_OP_LT
: TRACE("_lt"); break;
1840 case WINED3D_SHADER_REL_OP_NE
: TRACE("_ne"); break;
1841 case WINED3D_SHADER_REL_OP_LE
: TRACE("_le"); break;
1842 default: TRACE("_(%u)", ins
.flags
);
1845 else if (ins
.handler_idx
== WINED3DSIH_TEX
1846 && shader_version
.major
>= 2
1847 && (ins
.flags
& WINED3DSI_TEXLD_PROJECT
))
1852 for (i
= 0; i
< ins
.dst_count
; ++i
)
1854 shader_dump_ins_modifiers(&ins
.dst
[i
]);
1855 TRACE(!i
? " " : ", ");
1856 shader_dump_dst_param(&ins
.dst
[i
], &shader_version
);
1859 /* Other source tokens */
1860 for (i
= ins
.dst_count
; i
< (ins
.dst_count
+ ins
.src_count
); ++i
)
1862 TRACE(!i
? " " : ", ");
1863 shader_dump_src_param(&ins
.src
[i
- ins
.dst_count
], &shader_version
);
1870 static void shader_cleanup(struct wined3d_shader
*shader
)
1872 HeapFree(GetProcessHeap(), 0, shader
->output_signature
.elements
);
1873 HeapFree(GetProcessHeap(), 0, shader
->input_signature
.elements
);
1874 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
1875 shader
->device
->shader_backend
->shader_destroy(shader
);
1876 HeapFree(GetProcessHeap(), 0, shader
->reg_maps
.constf
);
1877 HeapFree(GetProcessHeap(), 0, shader
->reg_maps
.sampler_map
.entries
);
1878 HeapFree(GetProcessHeap(), 0, shader
->function
);
1879 shader_delete_constant_list(&shader
->constantsF
);
1880 shader_delete_constant_list(&shader
->constantsB
);
1881 shader_delete_constant_list(&shader
->constantsI
);
1882 list_remove(&shader
->shader_list_entry
);
1884 if (shader
->frontend
&& shader
->frontend_data
)
1885 shader
->frontend
->shader_free(shader
->frontend_data
);
1888 struct shader_none_priv
1890 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
1891 const struct fragment_pipeline
*fragment_pipe
;
1892 BOOL ffp_proj_control
;
1895 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
1896 static void shader_none_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
1897 enum wined3d_gl_resource_type tex_type
, const SIZE
*ds_mask_size
) {}
1898 static void shader_none_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
) {}
1899 static void shader_none_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1900 static void shader_none_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1901 static void shader_none_load_constants(void *shader_priv
, struct wined3d_context
*context
,
1902 const struct wined3d_state
*state
) {}
1903 static void shader_none_destroy(struct wined3d_shader
*shader
) {}
1904 static void shader_none_free_context_data(struct wined3d_context
*context
) {}
1905 static void shader_none_init_context_state(struct wined3d_context
*context
) {}
1907 /* Context activation is done by the caller. */
1908 static void shader_none_select(void *shader_priv
, struct wined3d_context
*context
,
1909 const struct wined3d_state
*state
)
1911 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1912 struct shader_none_priv
*priv
= shader_priv
;
1914 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
1915 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
1918 /* Context activation is done by the caller. */
1919 static void shader_none_disable(void *shader_priv
, struct wined3d_context
*context
)
1921 struct shader_none_priv
*priv
= shader_priv
;
1922 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1924 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
1925 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
1927 context
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
1928 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
1929 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
);
1932 static HRESULT
shader_none_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
1933 const struct fragment_pipeline
*fragment_pipe
)
1935 struct fragment_caps fragment_caps
;
1936 void *vertex_priv
, *fragment_priv
;
1937 struct shader_none_priv
*priv
;
1939 if (!(priv
= HeapAlloc(GetProcessHeap(), 0, sizeof(*priv
))))
1940 return E_OUTOFMEMORY
;
1942 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&none_shader_backend
, priv
)))
1944 ERR("Failed to initialize vertex pipe.\n");
1945 HeapFree(GetProcessHeap(), 0, priv
);
1949 if (!(fragment_priv
= fragment_pipe
->alloc_private(&none_shader_backend
, priv
)))
1951 ERR("Failed to initialize fragment pipe.\n");
1952 vertex_pipe
->vp_free(device
);
1953 HeapFree(GetProcessHeap(), 0, priv
);
1957 priv
->vertex_pipe
= vertex_pipe
;
1958 priv
->fragment_pipe
= fragment_pipe
;
1959 fragment_pipe
->get_caps(&device
->adapter
->gl_info
, &fragment_caps
);
1960 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
1962 device
->vertex_priv
= vertex_priv
;
1963 device
->fragment_priv
= fragment_priv
;
1964 device
->shader_priv
= priv
;
1969 static void shader_none_free(struct wined3d_device
*device
)
1971 struct shader_none_priv
*priv
= device
->shader_priv
;
1973 priv
->fragment_pipe
->free_private(device
);
1974 priv
->vertex_pipe
->vp_free(device
);
1975 HeapFree(GetProcessHeap(), 0, priv
);
1978 static BOOL
shader_none_allocate_context_data(struct wined3d_context
*context
)
1983 static void shader_none_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
1985 /* Set the shader caps to 0 for the none shader backend */
1986 caps
->vs_version
= 0;
1987 caps
->gs_version
= 0;
1988 caps
->ps_version
= 0;
1989 caps
->vs_uniform_count
= 0;
1990 caps
->ps_uniform_count
= 0;
1991 caps
->ps_1x_max_value
= 0.0f
;
1992 caps
->wined3d_caps
= 0;
1995 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
1997 /* We "support" every possible fixup, since we don't support any shader
1998 * model, and will never have to actually sample a texture. */
2002 static BOOL
shader_none_has_ffp_proj_control(void *shader_priv
)
2004 struct shader_none_priv
*priv
= shader_priv
;
2006 return priv
->ffp_proj_control
;
2009 const struct wined3d_shader_backend_ops none_shader_backend
=
2011 shader_none_handle_instruction
,
2013 shader_none_disable
,
2014 shader_none_select_depth_blt
,
2015 shader_none_deselect_depth_blt
,
2016 shader_none_update_float_vertex_constants
,
2017 shader_none_update_float_pixel_constants
,
2018 shader_none_load_constants
,
2019 shader_none_destroy
,
2022 shader_none_allocate_context_data
,
2023 shader_none_free_context_data
,
2024 shader_none_init_context_state
,
2025 shader_none_get_caps
,
2026 shader_none_color_fixup_supported
,
2027 shader_none_has_ffp_proj_control
,
2030 static HRESULT
shader_set_function(struct wined3d_shader
*shader
, const DWORD
*byte_code
,
2031 const struct wined3d_shader_signature
*output_signature
, DWORD float_const_count
,
2032 enum wined3d_shader_type type
, unsigned int max_version
)
2034 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2035 const struct wined3d_shader_frontend
*fe
;
2037 unsigned int backend_version
;
2038 const struct wined3d_d3d_info
*d3d_info
= &shader
->device
->adapter
->d3d_info
;
2040 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u, type %#x, max_version %u.\n",
2041 shader
, byte_code
, output_signature
, float_const_count
, type
, max_version
);
2043 list_init(&shader
->constantsF
);
2044 list_init(&shader
->constantsB
);
2045 list_init(&shader
->constantsI
);
2046 shader
->lconst_inf_or_nan
= FALSE
;
2048 fe
= shader_select_frontend(*byte_code
);
2051 FIXME("Unable to find frontend for shader.\n");
2052 return WINED3DERR_INVALIDCALL
;
2054 shader
->frontend
= fe
;
2055 shader
->frontend_data
= fe
->shader_init(byte_code
, output_signature
);
2056 if (!shader
->frontend_data
)
2058 FIXME("Failed to initialize frontend.\n");
2059 return WINED3DERR_INVALIDCALL
;
2062 /* First pass: trace shader. */
2063 if (TRACE_ON(d3d_shader
))
2064 shader_trace_init(fe
, shader
->frontend_data
, byte_code
);
2066 /* Second pass: figure out which registers are used, what the semantics are, etc. */
2067 if (FAILED(hr
= shader_get_registers_used(shader
, fe
, reg_maps
, &shader
->input_signature
,
2068 &shader
->output_signature
, byte_code
, float_const_count
)))
2071 if (reg_maps
->shader_version
.type
!= type
)
2073 WARN("Wrong shader type %d.\n", reg_maps
->shader_version
.type
);
2074 return WINED3DERR_INVALIDCALL
;
2076 if (reg_maps
->shader_version
.major
> max_version
)
2078 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps
->shader_version
.major
);
2079 return WINED3DERR_INVALIDCALL
;
2083 case WINED3D_SHADER_TYPE_VERTEX
:
2084 backend_version
= d3d_info
->limits
.vs_version
;
2086 case WINED3D_SHADER_TYPE_GEOMETRY
:
2087 backend_version
= d3d_info
->limits
.gs_version
;
2089 case WINED3D_SHADER_TYPE_PIXEL
:
2090 backend_version
= d3d_info
->limits
.ps_version
;
2093 FIXME("No backend version-checking for this shader type\n");
2094 backend_version
= 0;
2096 if (reg_maps
->shader_version
.major
> backend_version
)
2098 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
2099 reg_maps
->shader_version
.major
, reg_maps
->shader_version
.minor
);
2100 return WINED3DERR_INVALIDCALL
;
2103 shader
->function
= HeapAlloc(GetProcessHeap(), 0, shader
->functionLength
);
2104 if (!shader
->function
)
2105 return E_OUTOFMEMORY
;
2106 memcpy(shader
->function
, byte_code
, shader
->functionLength
);
2111 ULONG CDECL
wined3d_shader_incref(struct wined3d_shader
*shader
)
2113 ULONG refcount
= InterlockedIncrement(&shader
->ref
);
2115 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
2120 ULONG CDECL
wined3d_shader_decref(struct wined3d_shader
*shader
)
2122 ULONG refcount
= InterlockedDecrement(&shader
->ref
);
2124 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
2128 shader_cleanup(shader
);
2129 shader
->parent_ops
->wined3d_object_destroyed(shader
->parent
);
2130 HeapFree(GetProcessHeap(), 0, shader
);
2136 void * CDECL
wined3d_shader_get_parent(const struct wined3d_shader
*shader
)
2138 TRACE("shader %p.\n", shader
);
2140 return shader
->parent
;
2143 HRESULT CDECL
wined3d_shader_get_byte_code(const struct wined3d_shader
*shader
,
2144 void *byte_code
, UINT
*byte_code_size
)
2146 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader
, byte_code
, byte_code_size
);
2150 *byte_code_size
= shader
->functionLength
;
2154 if (*byte_code_size
< shader
->functionLength
)
2156 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
2157 * than the required size we should write the required size and
2158 * return D3DERR_MOREDATA. That's not actually true. */
2159 return WINED3DERR_INVALIDCALL
;
2162 memcpy(byte_code
, shader
->function
, shader
->functionLength
);
2167 /* Set local constants for d3d8 shaders. */
2168 HRESULT CDECL
wined3d_shader_set_local_constants_float(struct wined3d_shader
*shader
,
2169 UINT start_idx
, const float *src_data
, UINT count
)
2171 UINT end_idx
= start_idx
+ count
;
2174 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader
, start_idx
, src_data
, count
);
2176 if (end_idx
> shader
->limits
->constant_float
)
2178 WARN("end_idx %u > float constants limit %u.\n",
2179 end_idx
, shader
->limits
->constant_float
);
2180 end_idx
= shader
->limits
->constant_float
;
2183 for (i
= start_idx
; i
< end_idx
; ++i
)
2185 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
2188 return E_OUTOFMEMORY
;
2191 value
= (float *)lconst
->value
;
2192 memcpy(value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
2193 list_add_head(&shader
->constantsF
, &lconst
->entry
);
2195 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
2196 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
2198 shader
->lconst_inf_or_nan
= TRUE
;
2205 void find_vs_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2206 WORD swizzle_map
, struct vs_compile_args
*args
)
2208 args
->fog_src
= state
->render_states
[WINED3D_RS_FOGTABLEMODE
]
2209 == WINED3D_FOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
2210 args
->clip_enabled
= state
->render_states
[WINED3D_RS_CLIPPING
]
2211 && state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
];
2212 args
->point_size
= state
->gl_primitive_type
== GL_POINTS
;
2213 args
->per_vertex_point_size
= shader
->reg_maps
.point_size
;
2214 args
->swizzle_map
= swizzle_map
;
2217 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
2219 if (usage_idx1
!= usage_idx2
)
2221 if (usage1
== usage2
)
2223 if (usage1
== WINED3D_DECL_USAGE_POSITION
&& usage2
== WINED3D_DECL_USAGE_POSITIONT
)
2225 if (usage2
== WINED3D_DECL_USAGE_POSITION
&& usage1
== WINED3D_DECL_USAGE_POSITIONT
)
2231 BOOL
vshader_get_input(const struct wined3d_shader
*shader
,
2232 BYTE usage_req
, BYTE usage_idx_req
, unsigned int *regnum
)
2234 WORD map
= shader
->reg_maps
.input_registers
;
2237 for (i
= 0; map
; map
>>= 1, ++i
)
2239 if (!(map
& 1)) continue;
2241 if (match_usage(shader
->u
.vs
.attributes
[i
].usage
,
2242 shader
->u
.vs
.attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
2251 static HRESULT
shader_signature_copy(struct wined3d_shader_signature
*dst
,
2252 const struct wined3d_shader_signature
*src
, char **signature_strings
)
2254 struct wined3d_shader_signature_element
*e
;
2259 ptr
= *signature_strings
;
2261 dst
->element_count
= src
->element_count
;
2262 if (!(dst
->elements
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dst
->elements
) * dst
->element_count
)))
2263 return E_OUTOFMEMORY
;
2265 for (i
= 0; i
< src
->element_count
; ++i
)
2267 e
= &src
->elements
[i
];
2268 dst
->elements
[i
] = *e
;
2270 len
= strlen(e
->semantic_name
);
2271 memcpy(ptr
, e
->semantic_name
, len
+ 1);
2272 dst
->elements
[i
].semantic_name
= ptr
;
2276 *signature_strings
= ptr
;
2281 static HRESULT
shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2282 const struct wined3d_shader_desc
*desc
, DWORD float_const_count
, enum wined3d_shader_type type
,
2283 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2285 struct wined3d_shader_signature_element
*e
;
2291 if (!desc
->byte_code
)
2292 return WINED3DERR_INVALIDCALL
;
2295 shader
->device
= device
;
2296 shader
->parent
= parent
;
2297 shader
->parent_ops
= parent_ops
;
2300 if (desc
->input_signature
)
2302 for (i
= 0; i
< desc
->input_signature
->element_count
; ++i
)
2304 e
= &desc
->input_signature
->elements
[i
];
2305 len
= strlen(e
->semantic_name
);
2306 if (len
>= ~(SIZE_T
)0 - total
)
2307 return E_OUTOFMEMORY
;
2312 if (desc
->output_signature
)
2314 for (i
= 0; i
< desc
->output_signature
->element_count
; ++i
)
2316 e
= &desc
->output_signature
->elements
[i
];
2317 len
= strlen(e
->semantic_name
);
2318 if (len
>= ~(SIZE_T
)0 - total
)
2319 return E_OUTOFMEMORY
;
2324 if (!(shader
->signature_strings
= HeapAlloc(GetProcessHeap(), 0, total
)))
2325 return E_OUTOFMEMORY
;
2326 ptr
= shader
->signature_strings
;
2328 if (desc
->input_signature
&& FAILED(hr
= shader_signature_copy(&shader
->input_signature
,
2329 desc
->input_signature
, &ptr
)))
2331 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
2334 if (desc
->output_signature
&& FAILED(hr
= shader_signature_copy(&shader
->output_signature
,
2335 desc
->output_signature
, &ptr
)))
2337 HeapFree(GetProcessHeap(), 0, shader
->input_signature
.elements
);
2338 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
2342 list_init(&shader
->linked_programs
);
2343 list_add_head(&device
->shaders
, &shader
->shader_list_entry
);
2345 if (FAILED(hr
= shader_set_function(shader
, desc
->byte_code
, desc
->output_signature
,
2346 float_const_count
, type
, desc
->max_version
)))
2348 WARN("Failed to set function, hr %#x.\n", hr
);
2349 shader_cleanup(shader
);
2355 static HRESULT
vertexshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2356 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2358 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2362 if (FAILED(hr
= shader_init(shader
, device
, desc
, device
->adapter
->d3d_info
.limits
.vs_uniform_count
,
2363 WINED3D_SHADER_TYPE_VERTEX
, parent
, parent_ops
)))
2366 for (i
= 0; i
< shader
->input_signature
.element_count
; ++i
)
2368 const struct wined3d_shader_signature_element
*input
= &shader
->input_signature
.elements
[i
];
2370 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)) || !input
->semantic_name
)
2373 shader
->u
.vs
.attributes
[input
->register_idx
].usage
=
2374 shader_usage_from_semantic_name(input
->semantic_name
);
2375 shader
->u
.vs
.attributes
[input
->register_idx
].usage_idx
= input
->semantic_idx
;
2378 shader
->load_local_constsF
= (reg_maps
->usesrelconstF
&& !list_empty(&shader
->constantsF
)) ||
2379 shader
->lconst_inf_or_nan
;
2384 static HRESULT
geometryshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2385 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2389 if (FAILED(hr
= shader_init(shader
, device
, desc
, 0,
2390 WINED3D_SHADER_TYPE_GEOMETRY
, parent
, parent_ops
)))
2393 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2398 void find_ps_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2399 BOOL position_transformed
, struct ps_compile_args
*args
, const struct wined3d_gl_info
*gl_info
)
2401 const struct wined3d_texture
*texture
;
2404 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
2405 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && state
->render_states
[WINED3D_RS_SRGBWRITEENABLE
])
2407 unsigned int rt_fmt_flags
= state
->fb
->render_targets
[0]->format_flags
;
2408 if (rt_fmt_flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
2410 static unsigned int warned
= 0;
2412 args
->srgb_correction
= 1;
2413 if (state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] && !warned
++)
2414 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
2415 "support, expect rendering artifacts.\n");
2419 if (shader
->reg_maps
.shader_version
.major
== 1
2420 && shader
->reg_maps
.shader_version
.minor
<= 3)
2422 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2424 DWORD flags
= state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
];
2426 if (flags
& WINED3D_TTFF_PROJECTED
)
2428 DWORD tex_transform
= flags
& ~WINED3D_TTFF_PROJECTED
;
2430 if (!state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
2432 enum wined3d_shader_resource_type resource_type
= shader
->reg_maps
.resource_info
[i
].type
;
2434 unsigned int index
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
2435 DWORD max_valid
= WINED3D_TTFF_COUNT4
;
2437 for (j
= 0; j
< state
->vertex_declaration
->element_count
; ++j
)
2439 struct wined3d_vertex_declaration_element
*element
=
2440 &state
->vertex_declaration
->elements
[j
];
2442 if (element
->usage
== WINED3D_DECL_USAGE_TEXCOORD
2443 && element
->usage_idx
== index
)
2445 max_valid
= element
->format
->component_count
;
2449 if (!tex_transform
|| tex_transform
> max_valid
)
2451 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2452 tex_transform
, max_valid
);
2453 tex_transform
= max_valid
;
2455 if ((resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_1D
&& tex_transform
> WINED3D_TTFF_COUNT1
)
2456 || (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2D
2457 && tex_transform
> WINED3D_TTFF_COUNT2
)
2458 || (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_3D
2459 && tex_transform
> WINED3D_TTFF_COUNT3
))
2460 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
2463 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2464 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2465 i
, tex_transform
, resource_type
);
2469 tex_transform
= WINED3D_TTFF_COUNT4
| WINED3D_PSARGS_PROJECTED
;
2471 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
2475 if (shader
->reg_maps
.shader_version
.major
== 1
2476 && shader
->reg_maps
.shader_version
.minor
<= 4)
2478 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2480 const struct wined3d_texture
*texture
= state
->textures
[i
];
2482 if (!shader
->reg_maps
.resource_info
[i
].type
)
2485 /* Treat unbound textures as 2D. The dummy texture will provide
2486 * the proper sample value. The tex_types bitmap defaults to
2487 * 2D because of the memset. */
2491 switch (texture
->target
)
2493 /* RECT textures are distinguished from 2D textures via np2_fixup */
2494 case GL_TEXTURE_RECTANGLE_ARB
:
2499 args
->tex_types
|= WINED3D_SHADER_TEX_3D
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
2502 case GL_TEXTURE_CUBE_MAP_ARB
:
2503 args
->tex_types
|= WINED3D_SHADER_TEX_CUBE
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
2509 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2511 if (!shader
->reg_maps
.resource_info
[i
].type
)
2514 texture
= state
->textures
[i
];
2517 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
2520 args
->color_fixup
[i
] = texture
->resource
.format
->color_fixup
;
2522 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_SHADOW
)
2523 args
->shadow
|= 1u << i
;
2525 /* Flag samplers that need NP2 texcoord fixup. */
2526 if (!(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
2527 args
->np2_fixup
|= (1u << i
);
2529 if (shader
->reg_maps
.shader_version
.major
>= 3)
2531 if (position_transformed
)
2532 args
->vp_mode
= pretransformed
;
2533 else if (use_vs(state
))
2534 args
->vp_mode
= vertexshader
;
2536 args
->vp_mode
= fixedfunction
;
2537 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
2541 args
->vp_mode
= vertexshader
;
2542 if (state
->render_states
[WINED3D_RS_FOGENABLE
])
2544 switch (state
->render_states
[WINED3D_RS_FOGTABLEMODE
])
2546 case WINED3D_FOG_NONE
:
2547 if (position_transformed
|| use_vs(state
))
2549 args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
;
2553 switch (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
])
2555 case WINED3D_FOG_NONE
: /* Fall through. */
2556 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
2557 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
2558 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
2562 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
2563 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
2564 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
2569 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
2573 args
->pointsprite
= state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
]
2574 && state
->gl_primitive_type
== GL_POINTS
;
2577 static HRESULT
pixelshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2578 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2580 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2581 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
2584 if (FAILED(hr
= shader_init(shader
, device
, desc
, device
->adapter
->d3d_info
.limits
.ps_uniform_count
,
2585 WINED3D_SHADER_TYPE_PIXEL
, parent
, parent_ops
)))
2588 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2590 if (shader
->u
.ps
.input_reg_used
[i
])
2593 highest_reg_used
= i
;
2597 /* Don't do any register mapping magic if it is not needed, or if we can't
2598 * achieve anything anyway */
2599 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
2600 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2602 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2604 /* This happens with relative addressing. The input mapper function
2605 * warns about this if the higher registers are declared too, so
2606 * don't write a FIXME here */
2607 WARN("More varying registers used than supported\n");
2610 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2612 shader
->u
.ps
.input_reg_map
[i
] = i
;
2615 shader
->u
.ps
.declared_in_count
= highest_reg_used
+ 1;
2619 shader
->u
.ps
.declared_in_count
= 0;
2620 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2622 if (shader
->u
.ps
.input_reg_used
[i
])
2623 shader
->u
.ps
.input_reg_map
[i
] = shader
->u
.ps
.declared_in_count
++;
2624 else shader
->u
.ps
.input_reg_map
[i
] = ~0U;
2628 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2633 void pixelshader_update_resource_types(struct wined3d_shader
*shader
, WORD tex_types
)
2635 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2636 struct wined3d_shader_resource_info
*resource_info
= reg_maps
->resource_info
;
2639 if (reg_maps
->shader_version
.major
!= 1) return;
2641 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2643 /* We don't sample from this sampler. */
2644 if (!resource_info
[i
].type
)
2647 switch ((tex_types
>> i
* WINED3D_PSARGS_TEXTYPE_SHIFT
) & WINED3D_PSARGS_TEXTYPE_MASK
)
2649 case WINED3D_SHADER_TEX_2D
:
2650 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
2653 case WINED3D_SHADER_TEX_3D
:
2654 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_3D
;
2657 case WINED3D_SHADER_TEX_CUBE
:
2658 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
;
2664 HRESULT CDECL
wined3d_shader_create_gs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
2665 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
2667 struct wined3d_shader
*object
;
2670 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
2671 device
, desc
, parent
, parent_ops
, shader
);
2673 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2675 return E_OUTOFMEMORY
;
2677 if (FAILED(hr
= geometryshader_init(object
, device
, desc
, parent
, parent_ops
)))
2679 WARN("Failed to initialize geometry shader, hr %#x.\n", hr
);
2680 HeapFree(GetProcessHeap(), 0, object
);
2684 TRACE("Created geometry shader %p.\n", object
);
2690 HRESULT CDECL
wined3d_shader_create_ps(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
2691 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
2693 struct wined3d_shader
*object
;
2696 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
2697 device
, desc
, parent
, parent_ops
, shader
);
2699 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2701 return E_OUTOFMEMORY
;
2703 if (FAILED(hr
= pixelshader_init(object
, device
, desc
, parent
, parent_ops
)))
2705 WARN("Failed to initialize pixel shader, hr %#x.\n", hr
);
2706 HeapFree(GetProcessHeap(), 0, object
);
2710 TRACE("Created pixel shader %p.\n", object
);
2716 HRESULT CDECL
wined3d_shader_create_vs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
2717 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
2719 struct wined3d_shader
*object
;
2722 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
2723 device
, desc
, parent
, parent_ops
, shader
);
2725 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2727 return E_OUTOFMEMORY
;
2729 if (FAILED(hr
= vertexshader_init(object
, device
, desc
, parent
, parent_ops
)))
2731 WARN("Failed to initialize vertex shader, hr %#x.\n", hr
);
2732 HeapFree(GetProcessHeap(), 0, object
);
2736 TRACE("Created vertex shader %p.\n", object
);