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
;
415 ptr
= list_head(clist
);
418 constant
= LIST_ENTRY(ptr
, struct wined3d_shader_lconst
, entry
);
419 ptr
= list_next(clist
, ptr
);
420 HeapFree(GetProcessHeap(), 0, constant
);
425 static void shader_set_limits(struct wined3d_shader
*shader
)
427 static const struct limits_entry
429 unsigned int min_version
;
430 unsigned int max_version
;
431 struct wined3d_shader_limits limits
;
435 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
436 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
437 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
438 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
439 * even though they are capable of supporting much more (GL
440 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
441 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
443 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
444 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
449 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
450 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
455 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
456 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
457 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
458 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
459 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
460 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 12}},
461 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 0, 32}},
464 const struct limits_entry
*limits_array
;
465 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
466 shader
->reg_maps
.shader_version
.minor
);
469 switch (shader
->reg_maps
.shader_version
.type
)
472 FIXME("Unexpected shader type %u found.\n", shader
->reg_maps
.shader_version
.type
);
474 case WINED3D_SHADER_TYPE_VERTEX
:
475 limits_array
= vs_limits
;
477 case WINED3D_SHADER_TYPE_GEOMETRY
:
478 limits_array
= gs_limits
;
480 case WINED3D_SHADER_TYPE_PIXEL
:
481 limits_array
= ps_limits
;
485 while (limits_array
[i
].min_version
&& limits_array
[i
].min_version
<= shader_version
)
487 if (shader_version
<= limits_array
[i
].max_version
)
489 shader
->limits
= &limits_array
[i
].limits
;
496 FIXME("Unexpected shader version \"%u.%u\".\n",
497 shader
->reg_maps
.shader_version
.major
,
498 shader
->reg_maps
.shader_version
.minor
);
499 shader
->limits
= &limits_array
[max(0, i
- 1)].limits
;
503 static inline void set_bitmap_bit(DWORD
*bitmap
, DWORD bit
)
508 bitmap
[idx
] |= (1 << shift
);
511 static BOOL
shader_record_register_usage(struct wined3d_shader
*shader
, struct wined3d_shader_reg_maps
*reg_maps
,
512 const struct wined3d_shader_register
*reg
, enum wined3d_shader_type shader_type
, unsigned int constf_size
)
516 case WINED3DSPR_TEXTURE
: /* WINED3DSPR_ADDR */
517 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
518 reg_maps
->texcoord
|= 1 << reg
->idx
[0].offset
;
520 reg_maps
->address
|= 1 << reg
->idx
[0].offset
;
523 case WINED3DSPR_TEMP
:
524 reg_maps
->temporary
|= 1 << reg
->idx
[0].offset
;
527 case WINED3DSPR_INPUT
:
528 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
530 if (reg
->idx
[0].rel_addr
)
532 /* If relative addressing is used, we must assume that all registers
533 * are used. Even if it is a construct like v3[aL], we can't assume
534 * that v0, v1 and v2 aren't read because aL can be negative */
536 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
538 shader
->u
.ps
.input_reg_used
[i
] = TRUE
;
543 shader
->u
.ps
.input_reg_used
[reg
->idx
[0].offset
] = TRUE
;
547 reg_maps
->input_registers
|= 1 << reg
->idx
[0].offset
;
550 case WINED3DSPR_RASTOUT
:
551 if (reg
->idx
[0].offset
== 1)
553 if (reg
->idx
[0].offset
== 2)
554 reg_maps
->point_size
= 1;
557 case WINED3DSPR_MISCTYPE
:
558 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
560 if (!reg
->idx
[0].offset
)
562 else if (reg
->idx
[0].offset
== 1)
563 reg_maps
->usesfacing
= 1;
567 case WINED3DSPR_CONST
:
568 if (reg
->idx
[0].rel_addr
)
570 if (reg
->idx
[0].offset
< reg_maps
->min_rel_offset
)
571 reg_maps
->min_rel_offset
= reg
->idx
[0].offset
;
572 if (reg
->idx
[0].offset
> reg_maps
->max_rel_offset
)
573 reg_maps
->max_rel_offset
= reg
->idx
[0].offset
;
574 reg_maps
->usesrelconstF
= TRUE
;
578 if (reg
->idx
[0].offset
>= min(shader
->limits
->constant_float
, constf_size
))
580 WARN("Shader using float constant %u which is not supported.\n", reg
->idx
[0].offset
);
585 set_bitmap_bit(reg_maps
->constf
, reg
->idx
[0].offset
);
590 case WINED3DSPR_CONSTINT
:
591 if (reg
->idx
[0].offset
>= shader
->limits
->constant_int
)
593 WARN("Shader using integer constant %u which is not supported.\n", reg
->idx
[0].offset
);
598 reg_maps
->integer_constants
|= (1 << reg
->idx
[0].offset
);
602 case WINED3DSPR_CONSTBOOL
:
603 if (reg
->idx
[0].offset
>= shader
->limits
->constant_bool
)
605 WARN("Shader using bool constant %u which is not supported.\n", reg
->idx
[0].offset
);
610 reg_maps
->boolean_constants
|= (1 << reg
->idx
[0].offset
);
614 case WINED3DSPR_COLOROUT
:
615 reg_maps
->rt_mask
|= (1 << reg
->idx
[0].offset
);
619 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
620 reg
->type
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
626 static void shader_record_sample(struct wined3d_shader_reg_maps
*reg_maps
,
627 unsigned int resource_idx
, unsigned int sampler_idx
, unsigned int bind_idx
)
629 struct wined3d_shader_sampler_map_entry
*entries
, *entry
;
630 struct wined3d_shader_sampler_map
*map
;
633 map
= ®_maps
->sampler_map
;
634 entries
= map
->entries
;
635 for (i
= 0; i
< map
->count
; ++i
)
637 if (entries
[i
].resource_idx
== resource_idx
&& entries
[i
].sampler_idx
== sampler_idx
)
643 if (!(entries
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entries
) * 4)))
645 ERR("Failed to allocate sampler map entries.\n");
649 map
->entries
= entries
;
651 else if (map
->count
== map
->size
)
653 size_t new_size
= map
->size
* 2;
655 if (sizeof(*entries
) * new_size
<= sizeof(*entries
) * map
->size
656 || !(entries
= HeapReAlloc(GetProcessHeap(), 0, entries
, sizeof(*entries
) * new_size
)))
658 ERR("Failed to resize sampler map entries.\n");
661 map
->size
= new_size
;
662 map
->entries
= entries
;
665 entry
= &entries
[map
->count
++];
666 entry
->resource_idx
= resource_idx
;
667 entry
->sampler_idx
= sampler_idx
;
668 entry
->bind_idx
= bind_idx
;
671 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr
, unsigned int param
)
675 case WINED3DSIH_M4x4
:
676 case WINED3DSIH_M3x4
:
677 return param
== 1 ? 3 : 0;
679 case WINED3DSIH_M4x3
:
680 case WINED3DSIH_M3x3
:
681 return param
== 1 ? 2 : 0;
683 case WINED3DSIH_M3x2
:
684 return param
== 1 ? 1 : 0;
691 /* Note that this does not count the loop register as an address register. */
692 static HRESULT
shader_get_registers_used(struct wined3d_shader
*shader
, const struct wined3d_shader_frontend
*fe
,
693 struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_shader_signature
*input_signature
,
694 struct wined3d_shader_signature
*output_signature
, const DWORD
*byte_code
, DWORD constf_size
)
696 struct wined3d_shader_signature_element input_signature_elements
[max(MAX_ATTRIBS
, MAX_REG_INPUT
)];
697 struct wined3d_shader_signature_element output_signature_elements
[MAX_REG_OUTPUT
];
698 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
699 void *fe_data
= shader
->frontend_data
;
700 struct wined3d_shader_version shader_version
;
701 const DWORD
*ptr
= byte_code
;
704 memset(reg_maps
, 0, sizeof(*reg_maps
));
705 memset(input_signature_elements
, 0, sizeof(input_signature_elements
));
706 memset(output_signature_elements
, 0, sizeof(output_signature_elements
));
707 reg_maps
->min_rel_offset
= ~0U;
709 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
710 reg_maps
->shader_version
= shader_version
;
712 shader_set_limits(shader
);
714 reg_maps
->constf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
715 sizeof(*reg_maps
->constf
) * ((min(shader
->limits
->constant_float
, constf_size
) + 31) / 32));
716 if (!reg_maps
->constf
)
718 ERR("Failed to allocate constant map memory.\n");
719 return E_OUTOFMEMORY
;
722 while (!fe
->shader_is_end(fe_data
, &ptr
))
724 struct wined3d_shader_instruction ins
;
727 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
729 /* Unhandled opcode, and its parameters. */
730 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
732 TRACE("Skipping unrecognized instruction.\n");
736 /* Handle declarations. */
737 if (ins
.handler_idx
== WINED3DSIH_DCL
)
739 struct wined3d_shader_semantic
*semantic
= &ins
.declaration
.semantic
;
740 unsigned int reg_idx
= semantic
->reg
.reg
.idx
[0].offset
;
742 switch (semantic
->reg
.reg
.type
)
744 /* Mark input registers used. */
745 case WINED3DSPR_INPUT
:
746 if (reg_idx
>= MAX_REG_INPUT
)
748 ERR("Invalid input register index %u.\n", reg_idx
);
751 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
&& shader_version
.major
== 3
752 && semantic
->usage
== WINED3D_DECL_USAGE_POSITION
&& !semantic
->usage_idx
)
753 return WINED3DERR_INVALIDCALL
;
754 reg_maps
->input_registers
|= 1 << reg_idx
;
755 shader_signature_from_semantic(&input_signature_elements
[reg_idx
], semantic
);
758 /* Vertex shader: mark 3.0 output registers used, save token. */
759 case WINED3DSPR_OUTPUT
:
760 if (reg_idx
>= MAX_REG_OUTPUT
)
762 ERR("Invalid output register index %u.\n", reg_idx
);
765 reg_maps
->output_registers
|= 1 << reg_idx
;
766 shader_signature_from_semantic(&output_signature_elements
[reg_idx
], semantic
);
767 if (semantic
->usage
== WINED3D_DECL_USAGE_FOG
)
769 if (semantic
->usage
== WINED3D_DECL_USAGE_PSIZE
)
770 reg_maps
->point_size
= 1;
773 case WINED3DSPR_SAMPLER
:
774 shader_record_sample(reg_maps
, reg_idx
, reg_idx
, reg_idx
);
775 case WINED3DSPR_RESOURCE
:
776 if (reg_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
778 ERR("Invalid resource index %u.\n", reg_idx
);
781 reg_maps
->resource_info
[reg_idx
].type
= semantic
->resource_type
;
782 reg_maps
->resource_info
[reg_idx
].data_type
= semantic
->resource_data_type
;
786 TRACE("Not recording DCL register type %#x.\n", semantic
->reg
.reg
.type
);
790 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
792 struct wined3d_shader_register
*reg
= &ins
.declaration
.src
.reg
;
793 if (reg
->idx
[0].offset
>= WINED3D_MAX_CBS
)
794 ERR("Invalid CB index %u.\n", reg
->idx
[0].offset
);
796 reg_maps
->cb_sizes
[reg
->idx
[0].offset
] = reg
->idx
[1].offset
;
798 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
)
800 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
801 shader
->u
.gs
.input_type
= ins
.declaration
.primitive_type
;
803 FIXME("Invalid instruction %#x for shader type %#x.\n",
804 ins
.handler_idx
, shader_version
.type
);
806 else if (ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
808 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
809 shader
->u
.gs
.output_type
= ins
.declaration
.primitive_type
;
811 FIXME("Invalid instruction %#x for shader type %#x.\n",
812 ins
.handler_idx
, shader_version
.type
);
814 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
816 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
817 shader
->u
.gs
.vertices_out
= ins
.declaration
.count
;
819 FIXME("Invalid instruction %#x for shader type %#x.\n",
820 ins
.handler_idx
, shader_version
.type
);
822 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
824 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
826 if (!lconst
) return E_OUTOFMEMORY
;
828 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
829 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
830 value
= (float *)lconst
->value
;
832 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
833 if (shader_version
.major
== 1 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
835 if (value
[0] < -1.0f
) value
[0] = -1.0f
;
836 else if (value
[0] > 1.0f
) value
[0] = 1.0f
;
837 if (value
[1] < -1.0f
) value
[1] = -1.0f
;
838 else if (value
[1] > 1.0f
) value
[1] = 1.0f
;
839 if (value
[2] < -1.0f
) value
[2] = -1.0f
;
840 else if (value
[2] > 1.0f
) value
[2] = 1.0f
;
841 if (value
[3] < -1.0f
) value
[3] = -1.0f
;
842 else if (value
[3] > 1.0f
) value
[3] = 1.0f
;
845 list_add_head(&shader
->constantsF
, &lconst
->entry
);
847 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
848 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
850 shader
->lconst_inf_or_nan
= TRUE
;
853 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
855 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
856 if (!lconst
) return E_OUTOFMEMORY
;
858 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
859 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
861 list_add_head(&shader
->constantsI
, &lconst
->entry
);
862 reg_maps
->local_int_consts
|= (1 << lconst
->idx
);
864 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
866 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
867 if (!lconst
) return E_OUTOFMEMORY
;
869 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
870 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, sizeof(DWORD
));
872 list_add_head(&shader
->constantsB
, &lconst
->entry
);
873 reg_maps
->local_bool_consts
|= (1 << lconst
->idx
);
875 /* For subroutine prototypes. */
876 else if (ins
.handler_idx
== WINED3DSIH_LABEL
)
878 reg_maps
->labels
|= 1 << ins
.src
[0].reg
.idx
[0].offset
;
880 /* Set texture, address, temporary registers. */
883 BOOL color0_mov
= FALSE
;
886 /* This will loop over all the registers and try to
887 * make a bitmask of the ones we're interested in.
889 * Relative addressing tokens are ignored, but that's
890 * okay, since we'll catch any address registers when
891 * they are initialized (required by spec). */
892 for (i
= 0; i
< ins
.dst_count
; ++i
)
894 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.dst
[i
].reg
,
895 shader_version
.type
, constf_size
))
896 return WINED3DERR_INVALIDCALL
;
898 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
899 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
900 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
901 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
&& shader_version
.major
< 3)
903 UINT idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
905 switch (ins
.dst
[i
].reg
.type
)
907 case WINED3DSPR_RASTOUT
:
911 reg_maps
->output_registers
|= 1 << 10;
912 shader_signature_from_usage(&output_signature_elements
[10],
913 WINED3D_DECL_USAGE_POSITION
, 0, 10, WINED3DSP_WRITEMASK_ALL
);
917 reg_maps
->output_registers
|= 1 << 11;
918 shader_signature_from_usage(&output_signature_elements
[11],
919 WINED3D_DECL_USAGE_FOG
, 0, 11, WINED3DSP_WRITEMASK_0
);
923 reg_maps
->output_registers
|= 1 << 11;
924 shader_signature_from_usage(&output_signature_elements
[11],
925 WINED3D_DECL_USAGE_PSIZE
, 0, 11, WINED3DSP_WRITEMASK_1
);
930 case WINED3DSPR_ATTROUT
:
934 if (reg_maps
->output_registers
& (1 << idx
))
936 output_signature_elements
[idx
].mask
|= ins
.dst
[i
].write_mask
;
940 reg_maps
->output_registers
|= 1 << idx
;
941 shader_signature_from_usage(&output_signature_elements
[idx
],
942 WINED3D_DECL_USAGE_COLOR
, idx
- 8, idx
, ins
.dst
[i
].write_mask
);
947 case WINED3DSPR_TEXCRDOUT
:
949 reg_maps
->texcoord_mask
[idx
] |= ins
.dst
[i
].write_mask
;
950 if (reg_maps
->output_registers
& (1 << idx
))
952 output_signature_elements
[idx
].mask
|= ins
.dst
[i
].write_mask
;
956 reg_maps
->output_registers
|= 1 << idx
;
957 shader_signature_from_usage(&output_signature_elements
[idx
],
958 WINED3D_DECL_USAGE_TEXCOORD
, idx
, idx
, ins
.dst
[i
].write_mask
);
967 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
969 if (ins
.dst
[i
].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
.dst
[i
].reg
.idx
[0].offset
)
971 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
972 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
973 * the mov and perform the sRGB write correction from the source register.
975 * However, if the mov is only partial, we can't do this, and if the write
976 * comes from an instruction other than MOV it is hard to do as well. If
977 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
978 shader
->u
.ps
.color0_mov
= FALSE
;
979 if (ins
.handler_idx
== WINED3DSIH_MOV
980 && ins
.dst
[i
].write_mask
== WINED3DSP_WRITEMASK_ALL
)
982 /* Used later when the source register is read. */
986 /* Also drop the MOV marker if the source register is overwritten prior to the shader
989 else if (ins
.dst
[i
].reg
.type
== WINED3DSPR_TEMP
990 && ins
.dst
[i
].reg
.idx
[0].offset
== shader
->u
.ps
.color0_reg
)
992 shader
->u
.ps
.color0_mov
= FALSE
;
996 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
997 if (shader_version
.major
== 1
998 && (ins
.handler_idx
== WINED3DSIH_TEX
999 || ins
.handler_idx
== WINED3DSIH_TEXBEM
1000 || ins
.handler_idx
== WINED3DSIH_TEXBEML
1001 || ins
.handler_idx
== WINED3DSIH_TEXDP3TEX
1002 || ins
.handler_idx
== WINED3DSIH_TEXM3x2TEX
1003 || ins
.handler_idx
== WINED3DSIH_TEXM3x3SPEC
1004 || ins
.handler_idx
== WINED3DSIH_TEXM3x3TEX
1005 || ins
.handler_idx
== WINED3DSIH_TEXM3x3VSPEC
1006 || ins
.handler_idx
== WINED3DSIH_TEXREG2AR
1007 || ins
.handler_idx
== WINED3DSIH_TEXREG2GB
1008 || ins
.handler_idx
== WINED3DSIH_TEXREG2RGB
))
1010 unsigned int reg_idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
1012 TRACE("Setting fake 2D resource for 1.x pixelshader.\n");
1013 reg_maps
->resource_info
[reg_idx
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
1014 reg_maps
->resource_info
[reg_idx
].data_type
= WINED3D_DATA_FLOAT
;
1015 shader_record_sample(reg_maps
, reg_idx
, reg_idx
, reg_idx
);
1017 /* texbem is only valid with < 1.4 pixel shaders */
1018 if (ins
.handler_idx
== WINED3DSIH_TEXBEM
1019 || ins
.handler_idx
== WINED3DSIH_TEXBEML
)
1021 reg_maps
->bumpmat
|= 1 << reg_idx
;
1022 if (ins
.handler_idx
== WINED3DSIH_TEXBEML
)
1024 reg_maps
->luminanceparams
|= 1 << reg_idx
;
1028 else if (ins
.handler_idx
== WINED3DSIH_BEM
)
1030 reg_maps
->bumpmat
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
1034 if (ins
.handler_idx
== WINED3DSIH_NRM
) reg_maps
->usesnrm
= 1;
1035 else if (ins
.handler_idx
== WINED3DSIH_DSY
) reg_maps
->usesdsy
= 1;
1036 else if (ins
.handler_idx
== WINED3DSIH_DSX
) reg_maps
->usesdsx
= 1;
1037 else if (ins
.handler_idx
== WINED3DSIH_TEXLDD
) reg_maps
->usestexldd
= 1;
1038 else if (ins
.handler_idx
== WINED3DSIH_TEXLDL
) reg_maps
->usestexldl
= 1;
1039 else if (ins
.handler_idx
== WINED3DSIH_MOVA
) reg_maps
->usesmova
= 1;
1040 else if (ins
.handler_idx
== WINED3DSIH_IFC
) reg_maps
->usesifc
= 1;
1041 else if (ins
.handler_idx
== WINED3DSIH_CALL
) reg_maps
->usescall
= 1;
1042 else if (ins
.handler_idx
== WINED3DSIH_POW
) reg_maps
->usespow
= 1;
1043 else if (ins
.handler_idx
== WINED3DSIH_LOOP
1044 || ins
.handler_idx
== WINED3DSIH_REP
)
1047 if (cur_loop_depth
> max_loop_depth
)
1048 max_loop_depth
= cur_loop_depth
;
1050 else if (ins
.handler_idx
== WINED3DSIH_ENDLOOP
1051 || ins
.handler_idx
== WINED3DSIH_ENDREP
)
1055 else if (ins
.handler_idx
== WINED3DSIH_SAMPLE
1056 || ins
.handler_idx
== WINED3DSIH_SAMPLE_GRAD
1057 || ins
.handler_idx
== WINED3DSIH_SAMPLE_LOD
)
1059 shader_record_sample(reg_maps
, ins
.src
[1].reg
.idx
[0].offset
,
1060 ins
.src
[2].reg
.idx
[0].offset
, reg_maps
->sampler_map
.count
);
1064 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.predicate
->reg
,
1065 shader_version
.type
, constf_size
))
1066 return WINED3DERR_INVALIDCALL
;
1068 for (i
= 0; i
< ins
.src_count
; ++i
)
1070 unsigned int count
= get_instr_extra_regcount(ins
.handler_idx
, i
);
1071 struct wined3d_shader_register reg
= ins
.src
[i
].reg
;
1073 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.src
[i
].reg
,
1074 shader_version
.type
, constf_size
))
1075 return WINED3DERR_INVALIDCALL
;
1078 ++reg
.idx
[0].offset
;
1079 if (!shader_record_register_usage(shader
, reg_maps
, ®
,
1080 shader_version
.type
, constf_size
))
1081 return WINED3DERR_INVALIDCALL
;
1087 if (ins
.src
[i
].reg
.type
== WINED3DSPR_TEMP
1088 && ins
.src
[i
].swizzle
== WINED3DSP_NOSWIZZLE
)
1090 shader
->u
.ps
.color0_mov
= TRUE
;
1091 shader
->u
.ps
.color0_reg
= ins
.src
[i
].reg
.idx
[0].offset
;
1097 reg_maps
->loop_depth
= max_loop_depth
;
1099 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1100 * R0 is written to the render target. */
1101 if (shader_version
.major
< 2 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
1102 reg_maps
->rt_mask
|= (1 << 0);
1104 shader
->functionLength
= ((const char *)ptr
- (const char *)byte_code
);
1106 if (input_signature
->elements
)
1108 for (i
= 0; i
< input_signature
->element_count
; ++i
)
1110 reg_maps
->input_registers
|= 1 << input_signature
->elements
[i
].register_idx
;
1111 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
1112 && input_signature
->elements
[i
].sysval_semantic
== WINED3D_SV_POSITION
)
1116 else if (!input_signature
->elements
&& reg_maps
->input_registers
)
1118 unsigned int count
= count_bits(reg_maps
->input_registers
);
1119 struct wined3d_shader_signature_element
*e
;
1122 if (!(input_signature
->elements
= HeapAlloc(GetProcessHeap(), 0, sizeof(*input_signature
->elements
) * count
)))
1123 return E_OUTOFMEMORY
;
1124 input_signature
->element_count
= count
;
1126 e
= input_signature
->elements
;
1127 for (i
= 0; i
< ARRAY_SIZE(input_signature_elements
); ++i
)
1129 if (!(reg_maps
->input_registers
& (1 << i
)))
1131 input_signature_elements
[i
].register_idx
= i
;
1132 *e
++ = input_signature_elements
[i
];
1136 if (output_signature
->elements
)
1138 for (i
= 0; i
< output_signature
->element_count
; ++i
)
1140 reg_maps
->output_registers
|= 1 << output_signature
->elements
[i
].register_idx
;
1143 else if (reg_maps
->output_registers
)
1145 unsigned int count
= count_bits(reg_maps
->output_registers
);
1146 struct wined3d_shader_signature_element
*e
;
1148 if (!(output_signature
->elements
= HeapAlloc(GetProcessHeap(), 0, sizeof(*output_signature
->elements
) * count
)))
1149 return E_OUTOFMEMORY
;
1150 output_signature
->element_count
= count
;
1152 e
= output_signature
->elements
;
1153 for (i
= 0; i
< ARRAY_SIZE(output_signature_elements
); ++i
)
1155 if (!(reg_maps
->output_registers
& (1 << i
)))
1157 *e
++ = output_signature_elements
[i
];
1164 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int max
)
1166 DWORD map
= 1 << max
;
1168 map
&= reg_maps
->shader_version
.major
< 3 ? ~reg_maps
->texcoord
: ~reg_maps
->input_registers
;
1170 return wined3d_log2i(map
);
1173 static void shader_dump_decl_usage(const struct wined3d_shader_semantic
*semantic
,
1174 const struct wined3d_shader_version
*shader_version
)
1178 if (semantic
->reg
.reg
.type
== WINED3DSPR_SAMPLER
)
1180 switch (semantic
->resource_type
)
1182 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
1186 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
1190 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
1195 TRACE("_unknown_ttype(0x%08x)", semantic
->resource_type
);
1199 else if (semantic
->reg
.reg
.type
== WINED3DSPR_RESOURCE
)
1201 TRACE("_resource_");
1202 switch (semantic
->resource_type
)
1204 case WINED3D_SHADER_RESOURCE_BUFFER
:
1208 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
1212 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
1216 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
:
1217 TRACE("texture2dms");
1220 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
1224 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
1225 TRACE("texturecube");
1228 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
:
1229 TRACE("texture1darray");
1232 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
1233 TRACE("texture2darray");
1236 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY
:
1237 TRACE("texture2dmsarray");
1244 switch (semantic
->resource_data_type
)
1246 case WINED3D_DATA_FLOAT
:
1250 case WINED3D_DATA_INT
:
1254 case WINED3D_DATA_UINT
:
1258 case WINED3D_DATA_UNORM
:
1262 case WINED3D_DATA_SNORM
:
1267 TRACE(" (unknown)");
1273 /* Pixel shaders 3.0 don't have usage semantics. */
1274 if (shader_version
->major
< 3 && shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
) return;
1277 switch (semantic
->usage
)
1279 case WINED3D_DECL_USAGE_POSITION
:
1280 TRACE("position%u", semantic
->usage_idx
);
1283 case WINED3D_DECL_USAGE_BLEND_INDICES
:
1287 case WINED3D_DECL_USAGE_BLEND_WEIGHT
:
1291 case WINED3D_DECL_USAGE_NORMAL
:
1292 TRACE("normal%u", semantic
->usage_idx
);
1295 case WINED3D_DECL_USAGE_PSIZE
:
1299 case WINED3D_DECL_USAGE_COLOR
:
1300 if (!semantic
->usage_idx
) TRACE("color");
1301 else TRACE("specular%u", (semantic
->usage_idx
- 1));
1304 case WINED3D_DECL_USAGE_TEXCOORD
:
1305 TRACE("texture%u", semantic
->usage_idx
);
1308 case WINED3D_DECL_USAGE_TANGENT
:
1312 case WINED3D_DECL_USAGE_BINORMAL
:
1316 case WINED3D_DECL_USAGE_TESS_FACTOR
:
1317 TRACE("tessfactor");
1320 case WINED3D_DECL_USAGE_POSITIONT
:
1321 TRACE("positionT%u", semantic
->usage_idx
);
1324 case WINED3D_DECL_USAGE_FOG
:
1328 case WINED3D_DECL_USAGE_DEPTH
:
1332 case WINED3D_DECL_USAGE_SAMPLE
:
1337 FIXME("unknown_semantics(0x%08x)", semantic
->usage
);
1342 static void shader_dump_register(const struct wined3d_shader_register
*reg
,
1343 const struct wined3d_shader_version
*shader_version
)
1345 static const char * const rastout_reg_names
[] = {"oPos", "oFog", "oPts"};
1346 static const char * const misctype_reg_names
[] = {"vPos", "vFace"};
1347 UINT offset
= reg
->idx
[0].offset
;
1351 case WINED3DSPR_TEMP
:
1355 case WINED3DSPR_INPUT
:
1359 case WINED3DSPR_CONST
:
1360 case WINED3DSPR_CONST2
:
1361 case WINED3DSPR_CONST3
:
1362 case WINED3DSPR_CONST4
:
1364 offset
= shader_get_float_offset(reg
->type
, offset
);
1367 case WINED3DSPR_TEXTURE
: /* vs: case WINED3DSPR_ADDR */
1368 TRACE("%c", shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
? 't' : 'a');
1371 case WINED3DSPR_RASTOUT
:
1372 TRACE("%s", rastout_reg_names
[offset
]);
1375 case WINED3DSPR_COLOROUT
:
1379 case WINED3DSPR_DEPTHOUT
:
1383 case WINED3DSPR_ATTROUT
:
1387 case WINED3DSPR_TEXCRDOUT
:
1388 /* Vertex shaders >= 3.0 use general purpose output registers
1389 * (WINED3DSPR_OUTPUT), which can include an address token. */
1390 if (shader_version
->major
>= 3) TRACE("o");
1394 case WINED3DSPR_CONSTINT
:
1398 case WINED3DSPR_CONSTBOOL
:
1402 case WINED3DSPR_LABEL
:
1406 case WINED3DSPR_LOOP
:
1410 case WINED3DSPR_SAMPLER
:
1414 case WINED3DSPR_MISCTYPE
:
1416 FIXME("Unhandled misctype register %u.\n", offset
);
1418 TRACE("%s", misctype_reg_names
[offset
]);
1421 case WINED3DSPR_PREDICATE
:
1425 case WINED3DSPR_IMMCONST
:
1429 case WINED3DSPR_CONSTBUFFER
:
1433 case WINED3DSPR_PRIMID
:
1437 case WINED3DSPR_NULL
:
1441 case WINED3DSPR_RESOURCE
:
1446 TRACE("unhandled_rtype(%#x)", reg
->type
);
1450 if (reg
->type
== WINED3DSPR_IMMCONST
)
1453 switch (reg
->immconst_type
)
1455 case WINED3D_IMMCONST_SCALAR
:
1456 switch (reg
->data_type
)
1458 case WINED3D_DATA_FLOAT
:
1459 TRACE("%.8e", *(const float *)reg
->immconst_data
);
1461 case WINED3D_DATA_INT
:
1462 TRACE("%d", reg
->immconst_data
[0]);
1464 case WINED3D_DATA_RESOURCE
:
1465 case WINED3D_DATA_SAMPLER
:
1466 case WINED3D_DATA_UINT
:
1467 TRACE("%u", reg
->immconst_data
[0]);
1470 TRACE("<unhandled data type %#x>", reg
->data_type
);
1475 case WINED3D_IMMCONST_VEC4
:
1476 switch (reg
->data_type
)
1478 case WINED3D_DATA_FLOAT
:
1479 TRACE("%.8e, %.8e, %.8e, %.8e",
1480 *(const float *)®
->immconst_data
[0], *(const float *)®
->immconst_data
[1],
1481 *(const float *)®
->immconst_data
[2], *(const float *)®
->immconst_data
[3]);
1483 case WINED3D_DATA_INT
:
1484 TRACE("%d, %d, %d, %d",
1485 reg
->immconst_data
[0], reg
->immconst_data
[1],
1486 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1488 case WINED3D_DATA_RESOURCE
:
1489 case WINED3D_DATA_SAMPLER
:
1490 case WINED3D_DATA_UINT
:
1491 TRACE("%u, %u, %u, %u",
1492 reg
->immconst_data
[0], reg
->immconst_data
[1],
1493 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1496 TRACE("<unhandled data type %#x>", reg
->data_type
);
1502 TRACE("<unhandled immconst_type %#x>", reg
->immconst_type
);
1507 else if (reg
->type
!= WINED3DSPR_RASTOUT
1508 && reg
->type
!= WINED3DSPR_MISCTYPE
1509 && reg
->type
!= WINED3DSPR_NULL
)
1514 if (reg
->idx
[0].rel_addr
)
1516 shader_dump_src_param(reg
->idx
[0].rel_addr
, shader_version
);
1519 TRACE("%u]", offset
);
1521 if (reg
->idx
[1].offset
!= ~0U)
1524 if (reg
->idx
[1].rel_addr
)
1526 shader_dump_src_param(reg
->idx
[1].rel_addr
, shader_version
);
1529 TRACE("%u]", reg
->idx
[1].offset
);
1535 void shader_dump_dst_param(const struct wined3d_shader_dst_param
*param
,
1536 const struct wined3d_shader_version
*shader_version
)
1538 DWORD write_mask
= param
->write_mask
;
1540 shader_dump_register(¶m
->reg
, shader_version
);
1542 if (write_mask
&& write_mask
!= WINED3DSP_WRITEMASK_ALL
)
1544 static const char write_mask_chars
[] = "xyzw";
1547 if (write_mask
& WINED3DSP_WRITEMASK_0
) TRACE("%c", write_mask_chars
[0]);
1548 if (write_mask
& WINED3DSP_WRITEMASK_1
) TRACE("%c", write_mask_chars
[1]);
1549 if (write_mask
& WINED3DSP_WRITEMASK_2
) TRACE("%c", write_mask_chars
[2]);
1550 if (write_mask
& WINED3DSP_WRITEMASK_3
) TRACE("%c", write_mask_chars
[3]);
1554 void shader_dump_src_param(const struct wined3d_shader_src_param
*param
,
1555 const struct wined3d_shader_version
*shader_version
)
1557 enum wined3d_shader_src_modifier src_modifier
= param
->modifiers
;
1558 DWORD swizzle
= param
->swizzle
;
1560 if (src_modifier
== WINED3DSPSM_NEG
1561 || src_modifier
== WINED3DSPSM_BIASNEG
1562 || src_modifier
== WINED3DSPSM_SIGNNEG
1563 || src_modifier
== WINED3DSPSM_X2NEG
1564 || src_modifier
== WINED3DSPSM_ABSNEG
)
1566 else if (src_modifier
== WINED3DSPSM_COMP
)
1568 else if (src_modifier
== WINED3DSPSM_NOT
)
1571 if (src_modifier
== WINED3DSPSM_ABS
|| src_modifier
== WINED3DSPSM_ABSNEG
)
1574 shader_dump_register(¶m
->reg
, shader_version
);
1576 switch (src_modifier
)
1578 case WINED3DSPSM_NONE
: break;
1579 case WINED3DSPSM_NEG
: break;
1580 case WINED3DSPSM_NOT
: break;
1581 case WINED3DSPSM_BIAS
: TRACE("_bias"); break;
1582 case WINED3DSPSM_BIASNEG
: TRACE("_bias"); break;
1583 case WINED3DSPSM_SIGN
: TRACE("_bx2"); break;
1584 case WINED3DSPSM_SIGNNEG
: TRACE("_bx2"); break;
1585 case WINED3DSPSM_COMP
: break;
1586 case WINED3DSPSM_X2
: TRACE("_x2"); break;
1587 case WINED3DSPSM_X2NEG
: TRACE("_x2"); break;
1588 case WINED3DSPSM_DZ
: TRACE("_dz"); break;
1589 case WINED3DSPSM_DW
: TRACE("_dw"); break;
1590 case WINED3DSPSM_ABSNEG
: TRACE(")"); break;
1591 case WINED3DSPSM_ABS
: TRACE(")"); break;
1592 default: TRACE("_unknown_modifier(%#x)", src_modifier
);
1595 if (swizzle
!= WINED3DSP_NOSWIZZLE
)
1597 static const char swizzle_chars
[] = "xyzw";
1598 DWORD swizzle_x
= swizzle
& 0x03;
1599 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
1600 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
1601 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
1603 if (swizzle_x
== swizzle_y
1604 && swizzle_x
== swizzle_z
1605 && swizzle_x
== swizzle_w
)
1607 TRACE(".%c", swizzle_chars
[swizzle_x
]);
1611 TRACE(".%c%c%c%c", swizzle_chars
[swizzle_x
], swizzle_chars
[swizzle_y
],
1612 swizzle_chars
[swizzle_z
], swizzle_chars
[swizzle_w
]);
1617 /* Shared code in order to generate the bulk of the shader string.
1618 * NOTE: A description of how to parse tokens can be found on MSDN. */
1619 void shader_generate_main(const struct wined3d_shader
*shader
, struct wined3d_string_buffer
*buffer
,
1620 const struct wined3d_shader_reg_maps
*reg_maps
, const DWORD
*byte_code
, void *backend_ctx
)
1622 struct wined3d_device
*device
= shader
->device
;
1623 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
1624 void *fe_data
= shader
->frontend_data
;
1625 struct wined3d_shader_version shader_version
;
1626 struct wined3d_shader_loop_state loop_state
;
1627 struct wined3d_shader_instruction ins
;
1628 struct wined3d_shader_tex_mx tex_mx
;
1629 struct wined3d_shader_context ctx
;
1630 const DWORD
*ptr
= byte_code
;
1632 /* Initialize current parsing state. */
1633 tex_mx
.current_row
= 0;
1634 loop_state
.current_depth
= 0;
1635 loop_state
.current_reg
= 0;
1637 ctx
.shader
= shader
;
1638 ctx
.gl_info
= &device
->adapter
->gl_info
;
1639 ctx
.reg_maps
= reg_maps
;
1640 ctx
.buffer
= buffer
;
1641 ctx
.tex_mx
= &tex_mx
;
1642 ctx
.loop_state
= &loop_state
;
1643 ctx
.backend_data
= backend_ctx
;
1646 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1648 while (!fe
->shader_is_end(fe_data
, &ptr
))
1651 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1653 /* Unknown opcode and its parameters. */
1654 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1656 TRACE("Skipping unrecognized instruction.\n");
1661 FIXME("Predicates not implemented.\n");
1663 /* Call appropriate function for output target */
1664 device
->shader_backend
->shader_handle_instruction(&ins
);
1668 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param
*dst
)
1670 DWORD mmask
= dst
->modifiers
;
1675 case 13: TRACE("_d8"); break;
1676 case 14: TRACE("_d4"); break;
1677 case 15: TRACE("_d2"); break;
1678 case 1: TRACE("_x2"); break;
1679 case 2: TRACE("_x4"); break;
1680 case 3: TRACE("_x8"); break;
1681 default: TRACE("_unhandled_shift(%d)", dst
->shift
); break;
1684 if (mmask
& WINED3DSPDM_SATURATE
) TRACE("_sat");
1685 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) TRACE("_pp");
1686 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) TRACE("_centroid");
1688 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
1689 if (mmask
) FIXME("_unrecognized_modifier(%#x)", mmask
);
1692 static void shader_dump_primitive_type(enum wined3d_primitive_type primitive_type
)
1694 switch (primitive_type
)
1696 case WINED3D_PT_UNDEFINED
:
1699 case WINED3D_PT_POINTLIST
:
1702 case WINED3D_PT_LINELIST
:
1705 case WINED3D_PT_LINESTRIP
:
1708 case WINED3D_PT_TRIANGLELIST
:
1709 TRACE("trianglelist");
1711 case WINED3D_PT_TRIANGLESTRIP
:
1712 TRACE("trianglestrip");
1714 case WINED3D_PT_TRIANGLEFAN
:
1715 TRACE("trianglefan");
1717 case WINED3D_PT_LINELIST_ADJ
:
1718 TRACE("linelist_adj");
1720 case WINED3D_PT_LINESTRIP_ADJ
:
1721 TRACE("linestrip_adj");
1723 case WINED3D_PT_TRIANGLELIST_ADJ
:
1724 TRACE("trianglelist_adj");
1726 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
1727 TRACE("trianglestrip_adj");
1730 TRACE("<unrecognized_primitive_type %#x>", primitive_type
);
1735 static void shader_trace_init(const struct wined3d_shader_frontend
*fe
, void *fe_data
, const DWORD
*byte_code
)
1737 struct wined3d_shader_version shader_version
;
1738 const DWORD
*ptr
= byte_code
;
1739 const char *type_prefix
;
1742 TRACE("Parsing %p.\n", byte_code
);
1744 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1746 switch (shader_version
.type
)
1748 case WINED3D_SHADER_TYPE_VERTEX
:
1752 case WINED3D_SHADER_TYPE_GEOMETRY
:
1756 case WINED3D_SHADER_TYPE_PIXEL
:
1761 FIXME("Unhandled shader type %#x.\n", shader_version
.type
);
1762 type_prefix
= "unknown";
1766 TRACE("%s_%u_%u\n", type_prefix
, shader_version
.major
, shader_version
.minor
);
1768 while (!fe
->shader_is_end(fe_data
, &ptr
))
1770 struct wined3d_shader_instruction ins
;
1772 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1773 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1775 TRACE("Skipping unrecognized instruction.\n");
1779 if (ins
.handler_idx
== WINED3DSIH_DCL
)
1781 shader_dump_decl_usage(&ins
.declaration
.semantic
, &shader_version
);
1782 shader_dump_ins_modifiers(&ins
.declaration
.semantic
.reg
);
1784 shader_dump_dst_param(&ins
.declaration
.semantic
.reg
, &shader_version
);
1786 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
1788 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1789 shader_dump_src_param(&ins
.declaration
.src
, &shader_version
);
1790 TRACE(", %s", ins
.flags
& WINED3DSI_INDEXED_DYNAMIC
? "dynamicIndexed" : "immediateIndexed");
1792 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
1793 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
1795 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1796 shader_dump_primitive_type(ins
.declaration
.primitive_type
);
1798 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
1800 TRACE("%s %u", shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.count
);
1802 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
1804 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(ins
.dst
[0].reg
.type
,
1805 ins
.dst
[0].reg
.idx
[0].offset
),
1806 *(const float *)&ins
.src
[0].reg
.immconst_data
[0],
1807 *(const float *)&ins
.src
[0].reg
.immconst_data
[1],
1808 *(const float *)&ins
.src
[0].reg
.immconst_data
[2],
1809 *(const float *)&ins
.src
[0].reg
.immconst_data
[3]);
1811 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
1813 TRACE("defi i%u = %d, %d, %d, %d", ins
.dst
[0].reg
.idx
[0].offset
,
1814 ins
.src
[0].reg
.immconst_data
[0],
1815 ins
.src
[0].reg
.immconst_data
[1],
1816 ins
.src
[0].reg
.immconst_data
[2],
1817 ins
.src
[0].reg
.immconst_data
[3]);
1819 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
1821 TRACE("defb b%u = %s", ins
.dst
[0].reg
.idx
[0].offset
, ins
.src
[0].reg
.immconst_data
[0] ? "true" : "false");
1828 shader_dump_src_param(ins
.predicate
, &shader_version
);
1832 /* PixWin marks instructions with the coissue flag with a '+' */
1833 if (ins
.coissue
) TRACE("+");
1835 TRACE("%s", shader_opcode_names
[ins
.handler_idx
]);
1837 if (ins
.handler_idx
== WINED3DSIH_IFC
1838 || ins
.handler_idx
== WINED3DSIH_BREAKC
)
1842 case WINED3D_SHADER_REL_OP_GT
: TRACE("_gt"); break;
1843 case WINED3D_SHADER_REL_OP_EQ
: TRACE("_eq"); break;
1844 case WINED3D_SHADER_REL_OP_GE
: TRACE("_ge"); break;
1845 case WINED3D_SHADER_REL_OP_LT
: TRACE("_lt"); break;
1846 case WINED3D_SHADER_REL_OP_NE
: TRACE("_ne"); break;
1847 case WINED3D_SHADER_REL_OP_LE
: TRACE("_le"); break;
1848 default: TRACE("_(%u)", ins
.flags
);
1851 else if (ins
.handler_idx
== WINED3DSIH_TEX
1852 && shader_version
.major
>= 2
1853 && (ins
.flags
& WINED3DSI_TEXLD_PROJECT
))
1858 for (i
= 0; i
< ins
.dst_count
; ++i
)
1860 shader_dump_ins_modifiers(&ins
.dst
[i
]);
1861 TRACE(!i
? " " : ", ");
1862 shader_dump_dst_param(&ins
.dst
[i
], &shader_version
);
1865 /* Other source tokens */
1866 for (i
= ins
.dst_count
; i
< (ins
.dst_count
+ ins
.src_count
); ++i
)
1868 TRACE(!i
? " " : ", ");
1869 shader_dump_src_param(&ins
.src
[i
- ins
.dst_count
], &shader_version
);
1876 static void shader_cleanup(struct wined3d_shader
*shader
)
1878 HeapFree(GetProcessHeap(), 0, shader
->output_signature
.elements
);
1879 HeapFree(GetProcessHeap(), 0, shader
->input_signature
.elements
);
1880 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
1881 shader
->device
->shader_backend
->shader_destroy(shader
);
1882 HeapFree(GetProcessHeap(), 0, shader
->reg_maps
.constf
);
1883 HeapFree(GetProcessHeap(), 0, shader
->reg_maps
.sampler_map
.entries
);
1884 HeapFree(GetProcessHeap(), 0, shader
->function
);
1885 shader_delete_constant_list(&shader
->constantsF
);
1886 shader_delete_constant_list(&shader
->constantsB
);
1887 shader_delete_constant_list(&shader
->constantsI
);
1888 list_remove(&shader
->shader_list_entry
);
1890 if (shader
->frontend
&& shader
->frontend_data
)
1891 shader
->frontend
->shader_free(shader
->frontend_data
);
1894 struct shader_none_priv
1896 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
1897 const struct fragment_pipeline
*fragment_pipe
;
1898 BOOL ffp_proj_control
;
1901 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
1902 static void shader_none_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
1903 enum wined3d_gl_resource_type tex_type
, const SIZE
*ds_mask_size
) {}
1904 static void shader_none_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
) {}
1905 static void shader_none_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1906 static void shader_none_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1907 static void shader_none_load_constants(void *shader_priv
, struct wined3d_context
*context
,
1908 const struct wined3d_state
*state
) {}
1909 static void shader_none_destroy(struct wined3d_shader
*shader
) {}
1910 static void shader_none_free_context_data(struct wined3d_context
*context
) {}
1911 static void shader_none_init_context_state(struct wined3d_context
*context
) {}
1913 /* Context activation is done by the caller. */
1914 static void shader_none_select(void *shader_priv
, struct wined3d_context
*context
,
1915 const struct wined3d_state
*state
)
1917 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1918 struct shader_none_priv
*priv
= shader_priv
;
1920 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
1921 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
1924 /* Context activation is done by the caller. */
1925 static void shader_none_disable(void *shader_priv
, struct wined3d_context
*context
)
1927 struct shader_none_priv
*priv
= shader_priv
;
1928 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1930 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
1931 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
1933 context
->shader_update_mask
= (1 << WINED3D_SHADER_TYPE_PIXEL
)
1934 | (1 << WINED3D_SHADER_TYPE_VERTEX
)
1935 | (1 << WINED3D_SHADER_TYPE_GEOMETRY
);
1938 static HRESULT
shader_none_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
1939 const struct fragment_pipeline
*fragment_pipe
)
1941 struct fragment_caps fragment_caps
;
1942 void *vertex_priv
, *fragment_priv
;
1943 struct shader_none_priv
*priv
;
1945 if (!(priv
= HeapAlloc(GetProcessHeap(), 0, sizeof(*priv
))))
1946 return E_OUTOFMEMORY
;
1948 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&none_shader_backend
, priv
)))
1950 ERR("Failed to initialize vertex pipe.\n");
1951 HeapFree(GetProcessHeap(), 0, priv
);
1955 if (!(fragment_priv
= fragment_pipe
->alloc_private(&none_shader_backend
, priv
)))
1957 ERR("Failed to initialize fragment pipe.\n");
1958 vertex_pipe
->vp_free(device
);
1959 HeapFree(GetProcessHeap(), 0, priv
);
1963 priv
->vertex_pipe
= vertex_pipe
;
1964 priv
->fragment_pipe
= fragment_pipe
;
1965 fragment_pipe
->get_caps(&device
->adapter
->gl_info
, &fragment_caps
);
1966 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
1968 device
->vertex_priv
= vertex_priv
;
1969 device
->fragment_priv
= fragment_priv
;
1970 device
->shader_priv
= priv
;
1975 static void shader_none_free(struct wined3d_device
*device
)
1977 struct shader_none_priv
*priv
= device
->shader_priv
;
1979 priv
->fragment_pipe
->free_private(device
);
1980 priv
->vertex_pipe
->vp_free(device
);
1981 HeapFree(GetProcessHeap(), 0, priv
);
1984 static BOOL
shader_none_allocate_context_data(struct wined3d_context
*context
)
1989 static void shader_none_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
1991 /* Set the shader caps to 0 for the none shader backend */
1992 caps
->vs_version
= 0;
1993 caps
->gs_version
= 0;
1994 caps
->ps_version
= 0;
1995 caps
->vs_uniform_count
= 0;
1996 caps
->ps_uniform_count
= 0;
1997 caps
->ps_1x_max_value
= 0.0f
;
1998 caps
->wined3d_caps
= 0;
2001 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
2003 /* We "support" every possible fixup, since we don't support any shader
2004 * model, and will never have to actually sample a texture. */
2008 static BOOL
shader_none_has_ffp_proj_control(void *shader_priv
)
2010 struct shader_none_priv
*priv
= shader_priv
;
2012 return priv
->ffp_proj_control
;
2015 const struct wined3d_shader_backend_ops none_shader_backend
=
2017 shader_none_handle_instruction
,
2019 shader_none_disable
,
2020 shader_none_select_depth_blt
,
2021 shader_none_deselect_depth_blt
,
2022 shader_none_update_float_vertex_constants
,
2023 shader_none_update_float_pixel_constants
,
2024 shader_none_load_constants
,
2025 shader_none_destroy
,
2028 shader_none_allocate_context_data
,
2029 shader_none_free_context_data
,
2030 shader_none_init_context_state
,
2031 shader_none_get_caps
,
2032 shader_none_color_fixup_supported
,
2033 shader_none_has_ffp_proj_control
,
2036 static HRESULT
shader_set_function(struct wined3d_shader
*shader
, const DWORD
*byte_code
,
2037 const struct wined3d_shader_signature
*output_signature
, DWORD float_const_count
,
2038 enum wined3d_shader_type type
, unsigned int max_version
)
2040 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2041 const struct wined3d_shader_frontend
*fe
;
2043 unsigned int backend_version
;
2044 const struct wined3d_d3d_info
*d3d_info
= &shader
->device
->adapter
->d3d_info
;
2046 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
2047 shader
, byte_code
, output_signature
, float_const_count
);
2049 fe
= shader_select_frontend(*byte_code
);
2052 FIXME("Unable to find frontend for shader.\n");
2053 return WINED3DERR_INVALIDCALL
;
2055 shader
->frontend
= fe
;
2056 shader
->frontend_data
= fe
->shader_init(byte_code
, output_signature
);
2057 if (!shader
->frontend_data
)
2059 FIXME("Failed to initialize frontend.\n");
2060 return WINED3DERR_INVALIDCALL
;
2063 /* First pass: trace shader. */
2064 if (TRACE_ON(d3d_shader
))
2065 shader_trace_init(fe
, shader
->frontend_data
, byte_code
);
2067 /* Initialize immediate constant lists. */
2068 list_init(&shader
->constantsF
);
2069 list_init(&shader
->constantsB
);
2070 list_init(&shader
->constantsI
);
2071 shader
->lconst_inf_or_nan
= FALSE
;
2073 /* Second pass: figure out which registers are used, what the semantics are, etc. */
2074 if (FAILED(hr
= shader_get_registers_used(shader
, fe
, reg_maps
, &shader
->input_signature
,
2075 &shader
->output_signature
, byte_code
, float_const_count
)))
2078 if (reg_maps
->shader_version
.type
!= type
)
2080 WARN("Wrong shader type %d.\n", reg_maps
->shader_version
.type
);
2081 return WINED3DERR_INVALIDCALL
;
2083 if (reg_maps
->shader_version
.major
> max_version
)
2085 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps
->shader_version
.major
);
2086 return WINED3DERR_INVALIDCALL
;
2090 case WINED3D_SHADER_TYPE_VERTEX
:
2091 backend_version
= d3d_info
->limits
.vs_version
;
2093 case WINED3D_SHADER_TYPE_GEOMETRY
:
2094 backend_version
= d3d_info
->limits
.gs_version
;
2096 case WINED3D_SHADER_TYPE_PIXEL
:
2097 backend_version
= d3d_info
->limits
.ps_version
;
2100 FIXME("No backend version-checking for this shader type\n");
2101 backend_version
= 0;
2103 if (reg_maps
->shader_version
.major
> backend_version
)
2105 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
2106 reg_maps
->shader_version
.major
, reg_maps
->shader_version
.minor
);
2107 return WINED3DERR_INVALIDCALL
;
2110 shader
->function
= HeapAlloc(GetProcessHeap(), 0, shader
->functionLength
);
2111 if (!shader
->function
)
2112 return E_OUTOFMEMORY
;
2113 memcpy(shader
->function
, byte_code
, shader
->functionLength
);
2118 ULONG CDECL
wined3d_shader_incref(struct wined3d_shader
*shader
)
2120 ULONG refcount
= InterlockedIncrement(&shader
->ref
);
2122 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
2127 ULONG CDECL
wined3d_shader_decref(struct wined3d_shader
*shader
)
2129 ULONG refcount
= InterlockedDecrement(&shader
->ref
);
2131 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
2135 shader_cleanup(shader
);
2136 shader
->parent_ops
->wined3d_object_destroyed(shader
->parent
);
2137 HeapFree(GetProcessHeap(), 0, shader
);
2143 void * CDECL
wined3d_shader_get_parent(const struct wined3d_shader
*shader
)
2145 TRACE("shader %p.\n", shader
);
2147 return shader
->parent
;
2150 HRESULT CDECL
wined3d_shader_get_byte_code(const struct wined3d_shader
*shader
,
2151 void *byte_code
, UINT
*byte_code_size
)
2153 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader
, byte_code
, byte_code_size
);
2157 *byte_code_size
= shader
->functionLength
;
2161 if (*byte_code_size
< shader
->functionLength
)
2163 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
2164 * than the required size we should write the required size and
2165 * return D3DERR_MOREDATA. That's not actually true. */
2166 return WINED3DERR_INVALIDCALL
;
2169 memcpy(byte_code
, shader
->function
, shader
->functionLength
);
2174 /* Set local constants for d3d8 shaders. */
2175 HRESULT CDECL
wined3d_shader_set_local_constants_float(struct wined3d_shader
*shader
,
2176 UINT start_idx
, const float *src_data
, UINT count
)
2178 UINT end_idx
= start_idx
+ count
;
2181 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader
, start_idx
, src_data
, count
);
2183 if (end_idx
> shader
->limits
->constant_float
)
2185 WARN("end_idx %u > float constants limit %u.\n",
2186 end_idx
, shader
->limits
->constant_float
);
2187 end_idx
= shader
->limits
->constant_float
;
2190 for (i
= start_idx
; i
< end_idx
; ++i
)
2192 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
2195 return E_OUTOFMEMORY
;
2198 value
= (float *)lconst
->value
;
2199 memcpy(value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
2200 list_add_head(&shader
->constantsF
, &lconst
->entry
);
2202 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
2203 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
2205 shader
->lconst_inf_or_nan
= TRUE
;
2212 void find_vs_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2213 WORD swizzle_map
, struct vs_compile_args
*args
)
2215 args
->fog_src
= state
->render_states
[WINED3D_RS_FOGTABLEMODE
]
2216 == WINED3D_FOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
2217 args
->clip_enabled
= state
->render_states
[WINED3D_RS_CLIPPING
]
2218 && state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
];
2219 args
->point_size
= state
->gl_primitive_type
== GL_POINTS
;
2220 args
->per_vertex_point_size
= shader
->reg_maps
.point_size
;
2221 args
->swizzle_map
= swizzle_map
;
2224 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
2226 if (usage_idx1
!= usage_idx2
)
2228 if (usage1
== usage2
)
2230 if (usage1
== WINED3D_DECL_USAGE_POSITION
&& usage2
== WINED3D_DECL_USAGE_POSITIONT
)
2232 if (usage2
== WINED3D_DECL_USAGE_POSITION
&& usage1
== WINED3D_DECL_USAGE_POSITIONT
)
2238 BOOL
vshader_get_input(const struct wined3d_shader
*shader
,
2239 BYTE usage_req
, BYTE usage_idx_req
, unsigned int *regnum
)
2241 WORD map
= shader
->reg_maps
.input_registers
;
2244 for (i
= 0; map
; map
>>= 1, ++i
)
2246 if (!(map
& 1)) continue;
2248 if (match_usage(shader
->u
.vs
.attributes
[i
].usage
,
2249 shader
->u
.vs
.attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
2258 static HRESULT
shader_signature_copy(struct wined3d_shader_signature
*dst
,
2259 const struct wined3d_shader_signature
*src
, char **signature_strings
)
2261 struct wined3d_shader_signature_element
*e
;
2266 ptr
= *signature_strings
;
2268 dst
->element_count
= src
->element_count
;
2269 if (!(dst
->elements
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dst
->elements
) * dst
->element_count
)))
2270 return E_OUTOFMEMORY
;
2272 for (i
= 0; i
< src
->element_count
; ++i
)
2274 e
= &src
->elements
[i
];
2275 dst
->elements
[i
] = *e
;
2277 len
= strlen(e
->semantic_name
);
2278 memcpy(ptr
, e
->semantic_name
, len
+ 1);
2279 dst
->elements
[i
].semantic_name
= ptr
;
2283 *signature_strings
= ptr
;
2288 static HRESULT
shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2289 const struct wined3d_shader_desc
*desc
, DWORD float_const_count
, enum wined3d_shader_type type
,
2290 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2292 struct wined3d_shader_signature_element
*e
;
2298 if (!desc
->byte_code
)
2299 return WINED3DERR_INVALIDCALL
;
2302 shader
->device
= device
;
2303 shader
->parent
= parent
;
2304 shader
->parent_ops
= parent_ops
;
2307 if (desc
->input_signature
)
2309 for (i
= 0; i
< desc
->input_signature
->element_count
; ++i
)
2311 e
= &desc
->input_signature
->elements
[i
];
2312 len
= strlen(e
->semantic_name
);
2313 if (len
>= ~(SIZE_T
)0 - total
)
2314 return E_OUTOFMEMORY
;
2319 if (desc
->output_signature
)
2321 for (i
= 0; i
< desc
->output_signature
->element_count
; ++i
)
2323 e
= &desc
->output_signature
->elements
[i
];
2324 len
= strlen(e
->semantic_name
);
2325 if (len
>= ~(SIZE_T
)0 - total
)
2326 return E_OUTOFMEMORY
;
2331 if (!(shader
->signature_strings
= HeapAlloc(GetProcessHeap(), 0, total
)))
2332 return E_OUTOFMEMORY
;
2333 ptr
= shader
->signature_strings
;
2335 if (desc
->input_signature
&& FAILED(hr
= shader_signature_copy(&shader
->input_signature
,
2336 desc
->input_signature
, &ptr
)))
2338 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
2341 if (desc
->output_signature
&& FAILED(hr
= shader_signature_copy(&shader
->output_signature
,
2342 desc
->output_signature
, &ptr
)))
2344 HeapFree(GetProcessHeap(), 0, shader
->input_signature
.elements
);
2345 HeapFree(GetProcessHeap(), 0, shader
->signature_strings
);
2349 list_init(&shader
->linked_programs
);
2350 list_add_head(&device
->shaders
, &shader
->shader_list_entry
);
2352 if (FAILED(hr
= shader_set_function(shader
, desc
->byte_code
, desc
->output_signature
,
2353 float_const_count
, type
, desc
->max_version
)))
2355 WARN("Failed to set function, hr %#x.\n", hr
);
2356 shader_cleanup(shader
);
2362 static HRESULT
vertexshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2363 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2365 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2369 if (FAILED(hr
= shader_init(shader
, device
, desc
, device
->adapter
->d3d_info
.limits
.vs_uniform_count
,
2370 WINED3D_SHADER_TYPE_VERTEX
, parent
, parent_ops
)))
2373 for (i
= 0; i
< shader
->input_signature
.element_count
; ++i
)
2375 const struct wined3d_shader_signature_element
*input
= &shader
->input_signature
.elements
[i
];
2377 if (!(reg_maps
->input_registers
& (1 << input
->register_idx
)) || !input
->semantic_name
)
2380 shader
->u
.vs
.attributes
[input
->register_idx
].usage
=
2381 shader_usage_from_semantic_name(input
->semantic_name
);
2382 shader
->u
.vs
.attributes
[input
->register_idx
].usage_idx
= input
->semantic_idx
;
2385 shader
->load_local_constsF
= (reg_maps
->usesrelconstF
&& !list_empty(&shader
->constantsF
)) ||
2386 shader
->lconst_inf_or_nan
;
2391 static HRESULT
geometryshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2392 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2396 if (FAILED(hr
= shader_init(shader
, device
, desc
, 0,
2397 WINED3D_SHADER_TYPE_GEOMETRY
, parent
, parent_ops
)))
2400 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2405 void find_ps_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2406 BOOL position_transformed
, struct ps_compile_args
*args
, const struct wined3d_gl_info
*gl_info
)
2408 const struct wined3d_texture
*texture
;
2411 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
2412 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && state
->render_states
[WINED3D_RS_SRGBWRITEENABLE
])
2414 unsigned int rt_fmt_flags
= state
->fb
->render_targets
[0]->format_flags
;
2415 if (rt_fmt_flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
2417 static unsigned int warned
= 0;
2419 args
->srgb_correction
= 1;
2420 if (state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] && !warned
++)
2421 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
2422 "support, expect rendering artifacts.\n");
2426 if (shader
->reg_maps
.shader_version
.major
== 1
2427 && shader
->reg_maps
.shader_version
.minor
<= 3)
2429 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2431 DWORD flags
= state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
];
2433 if (flags
& WINED3D_TTFF_PROJECTED
)
2435 DWORD tex_transform
= flags
& ~WINED3D_TTFF_PROJECTED
;
2437 if (!state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
2439 enum wined3d_shader_resource_type resource_type
= shader
->reg_maps
.resource_info
[i
].type
;
2441 unsigned int index
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
2442 DWORD max_valid
= WINED3D_TTFF_COUNT4
;
2444 for (j
= 0; j
< state
->vertex_declaration
->element_count
; ++j
)
2446 struct wined3d_vertex_declaration_element
*element
=
2447 &state
->vertex_declaration
->elements
[j
];
2449 if (element
->usage
== WINED3D_DECL_USAGE_TEXCOORD
2450 && element
->usage_idx
== index
)
2452 max_valid
= element
->format
->component_count
;
2456 if (!tex_transform
|| tex_transform
> max_valid
)
2458 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2459 tex_transform
, max_valid
);
2460 tex_transform
= max_valid
;
2462 if ((resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_1D
&& tex_transform
> WINED3D_TTFF_COUNT1
)
2463 || (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2D
2464 && tex_transform
> WINED3D_TTFF_COUNT2
)
2465 || (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_3D
2466 && tex_transform
> WINED3D_TTFF_COUNT3
))
2467 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
2470 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2471 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2472 i
, tex_transform
, resource_type
);
2476 tex_transform
= WINED3D_TTFF_COUNT4
| WINED3D_PSARGS_PROJECTED
;
2478 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
2482 if (shader
->reg_maps
.shader_version
.major
== 1
2483 && shader
->reg_maps
.shader_version
.minor
<= 4)
2485 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2487 const struct wined3d_texture
*texture
= state
->textures
[i
];
2489 if (!shader
->reg_maps
.resource_info
[i
].type
)
2492 /* Treat unbound textures as 2D. The dummy texture will provide
2493 * the proper sample value. The tex_types bitmap defaults to
2494 * 2D because of the memset. */
2498 switch (texture
->target
)
2500 /* RECT textures are distinguished from 2D textures via np2_fixup */
2501 case GL_TEXTURE_RECTANGLE_ARB
:
2506 args
->tex_types
|= WINED3D_SHADER_TEX_3D
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
2509 case GL_TEXTURE_CUBE_MAP_ARB
:
2510 args
->tex_types
|= WINED3D_SHADER_TEX_CUBE
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
2516 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2518 if (!shader
->reg_maps
.resource_info
[i
].type
)
2521 texture
= state
->textures
[i
];
2524 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
2527 args
->color_fixup
[i
] = texture
->resource
.format
->color_fixup
;
2529 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_SHADOW
)
2530 args
->shadow
|= 1 << i
;
2532 /* Flag samplers that need NP2 texcoord fixup. */
2533 if (!(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
2534 args
->np2_fixup
|= (1 << i
);
2536 if (shader
->reg_maps
.shader_version
.major
>= 3)
2538 if (position_transformed
)
2539 args
->vp_mode
= pretransformed
;
2540 else if (use_vs(state
))
2541 args
->vp_mode
= vertexshader
;
2543 args
->vp_mode
= fixedfunction
;
2544 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
2548 args
->vp_mode
= vertexshader
;
2549 if (state
->render_states
[WINED3D_RS_FOGENABLE
])
2551 switch (state
->render_states
[WINED3D_RS_FOGTABLEMODE
])
2553 case WINED3D_FOG_NONE
:
2554 if (position_transformed
|| use_vs(state
))
2556 args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
;
2560 switch (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
])
2562 case WINED3D_FOG_NONE
: /* Fall through. */
2563 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
2564 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
2565 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
2569 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
2570 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
2571 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
2576 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
2580 args
->pointsprite
= state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
]
2581 && state
->gl_primitive_type
== GL_POINTS
;
2584 static HRESULT
pixelshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2585 const struct wined3d_shader_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2587 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2588 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
2591 if (FAILED(hr
= shader_init(shader
, device
, desc
, device
->adapter
->d3d_info
.limits
.ps_uniform_count
,
2592 WINED3D_SHADER_TYPE_PIXEL
, parent
, parent_ops
)))
2595 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2597 if (shader
->u
.ps
.input_reg_used
[i
])
2600 highest_reg_used
= i
;
2604 /* Don't do any register mapping magic if it is not needed, or if we can't
2605 * achieve anything anyway */
2606 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
2607 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2609 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2611 /* This happens with relative addressing. The input mapper function
2612 * warns about this if the higher registers are declared too, so
2613 * don't write a FIXME here */
2614 WARN("More varying registers used than supported\n");
2617 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2619 shader
->u
.ps
.input_reg_map
[i
] = i
;
2622 shader
->u
.ps
.declared_in_count
= highest_reg_used
+ 1;
2626 shader
->u
.ps
.declared_in_count
= 0;
2627 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2629 if (shader
->u
.ps
.input_reg_used
[i
])
2630 shader
->u
.ps
.input_reg_map
[i
] = shader
->u
.ps
.declared_in_count
++;
2631 else shader
->u
.ps
.input_reg_map
[i
] = ~0U;
2635 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2640 void pixelshader_update_resource_types(struct wined3d_shader
*shader
, WORD tex_types
)
2642 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2643 struct wined3d_shader_resource_info
*resource_info
= reg_maps
->resource_info
;
2646 if (reg_maps
->shader_version
.major
!= 1) return;
2648 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2650 /* We don't sample from this sampler. */
2651 if (!resource_info
[i
].type
)
2654 switch ((tex_types
>> i
* WINED3D_PSARGS_TEXTYPE_SHIFT
) & WINED3D_PSARGS_TEXTYPE_MASK
)
2656 case WINED3D_SHADER_TEX_2D
:
2657 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
2660 case WINED3D_SHADER_TEX_3D
:
2661 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_3D
;
2664 case WINED3D_SHADER_TEX_CUBE
:
2665 resource_info
[i
].type
= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
;
2671 HRESULT CDECL
wined3d_shader_create_gs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
2672 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
2674 struct wined3d_shader
*object
;
2677 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
2678 device
, desc
, parent
, parent_ops
, shader
);
2680 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2682 return E_OUTOFMEMORY
;
2684 if (FAILED(hr
= geometryshader_init(object
, device
, desc
, parent
, parent_ops
)))
2686 WARN("Failed to initialize geometry shader, hr %#x.\n", hr
);
2687 HeapFree(GetProcessHeap(), 0, object
);
2691 TRACE("Created geometry shader %p.\n", object
);
2697 HRESULT CDECL
wined3d_shader_create_ps(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
2698 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
2700 struct wined3d_shader
*object
;
2703 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
2704 device
, desc
, parent
, parent_ops
, shader
);
2706 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2708 return E_OUTOFMEMORY
;
2710 if (FAILED(hr
= pixelshader_init(object
, device
, desc
, parent
, parent_ops
)))
2712 WARN("Failed to initialize pixel shader, hr %#x.\n", hr
);
2713 HeapFree(GetProcessHeap(), 0, object
);
2717 TRACE("Created pixel shader %p.\n", object
);
2723 HRESULT CDECL
wined3d_shader_create_vs(struct wined3d_device
*device
, const struct wined3d_shader_desc
*desc
,
2724 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
)
2726 struct wined3d_shader
*object
;
2729 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
2730 device
, desc
, parent
, parent_ops
, shader
);
2732 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2734 return E_OUTOFMEMORY
;
2736 if (FAILED(hr
= vertexshader_init(object
, device
, desc
, parent
, parent_ops
)))
2738 WARN("Failed to initialize vertex shader, hr %#x.\n", hr
);
2739 HeapFree(GetProcessHeap(), 0, object
);
2743 TRACE("Created vertex shader %p.\n", object
);