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_NOP */ "nop",
112 /* WINED3DSIH_NRM */ "nrm",
113 /* WINED3DSIH_PHASE */ "phase",
114 /* WINED3DSIH_POW */ "pow",
115 /* WINED3DSIH_RCP */ "rcp",
116 /* WINED3DSIH_REP */ "rep",
117 /* WINED3DSIH_RET */ "ret",
118 /* WINED3DSIH_ROUND_NI */ "round_ni",
119 /* WINED3DSIH_RSQ */ "rsq",
120 /* WINED3DSIH_SAMPLE */ "sample",
121 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
122 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
123 /* WINED3DSIH_SETP */ "setp",
124 /* WINED3DSIH_SGE */ "sge",
125 /* WINED3DSIH_SGN */ "sgn",
126 /* WINED3DSIH_SINCOS */ "sincos",
127 /* WINED3DSIH_SLT */ "slt",
128 /* WINED3DSIH_SQRT */ "sqrt",
129 /* WINED3DSIH_SUB */ "sub",
130 /* WINED3DSIH_TEX */ "texld",
131 /* WINED3DSIH_TEXBEM */ "texbem",
132 /* WINED3DSIH_TEXBEML */ "texbeml",
133 /* WINED3DSIH_TEXCOORD */ "texcrd",
134 /* WINED3DSIH_TEXDEPTH */ "texdepth",
135 /* WINED3DSIH_TEXDP3 */ "texdp3",
136 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
137 /* WINED3DSIH_TEXKILL */ "texkill",
138 /* WINED3DSIH_TEXLDD */ "texldd",
139 /* WINED3DSIH_TEXLDL */ "texldl",
140 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
141 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
142 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
143 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
144 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
145 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
146 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
147 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
148 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
149 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
150 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
151 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
152 /* WINED3DSIH_UDIV */ "udiv",
153 /* WINED3DSIH_USHR */ "ushr",
154 /* WINED3DSIH_UTOF */ "utof",
155 /* WINED3DSIH_XOR */ "xor",
158 static const char * const semantic_names
[] =
160 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
161 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
162 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
163 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
164 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
165 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
166 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
167 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
168 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
169 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
170 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
171 /* WINED3D_DECL_USAGE_FOG */ "FOG",
172 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
173 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
176 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage
)
178 if (usage
>= sizeof(semantic_names
) / sizeof(*semantic_names
))
180 FIXME("Unrecognized usage %#x.\n", usage
);
181 return "UNRECOGNIZED";
184 return semantic_names
[usage
];
187 static enum wined3d_decl_usage
shader_usage_from_semantic_name(const char *name
)
191 for (i
= 0; i
< sizeof(semantic_names
) / sizeof(*semantic_names
); ++i
)
193 if (!strcmp(name
, semantic_names
[i
])) return i
;
199 BOOL
shader_match_semantic(const char *semantic_name
, enum wined3d_decl_usage usage
)
201 return !strcmp(semantic_name
, shader_semantic_name_from_usage(usage
));
204 static void shader_signature_from_semantic(struct wined3d_shader_signature_element
*e
,
205 const struct wined3d_shader_semantic
*s
)
207 e
->semantic_name
= shader_semantic_name_from_usage(s
->usage
);
208 e
->semantic_idx
= s
->usage_idx
;
209 e
->sysval_semantic
= 0;
210 e
->component_type
= 0;
211 e
->register_idx
= s
->reg
.reg
.idx
[0].offset
;
212 e
->mask
= s
->reg
.write_mask
;
215 static void shader_signature_from_usage(struct wined3d_shader_signature_element
*e
,
216 enum wined3d_decl_usage usage
, UINT usage_idx
, UINT reg_idx
, DWORD write_mask
)
218 e
->semantic_name
= shader_semantic_name_from_usage(usage
);
219 e
->semantic_idx
= usage_idx
;
220 e
->sysval_semantic
= 0;
221 e
->component_type
= 0;
222 e
->register_idx
= reg_idx
;
223 e
->mask
= write_mask
;
226 static const struct wined3d_shader_frontend
*shader_select_frontend(DWORD version_token
)
228 switch (version_token
>> 16)
232 return &sm1_shader_frontend
;
237 return &sm4_shader_frontend
;
240 FIXME("Unrecognised version token %#x\n", version_token
);
245 void shader_buffer_clear(struct wined3d_shader_buffer
*buffer
)
247 buffer
->buffer
[0] = '\0';
248 buffer
->content_size
= 0;
250 buffer
->newline
= TRUE
;
253 BOOL
shader_buffer_init(struct wined3d_shader_buffer
*buffer
)
255 buffer
->buffer_size
= 16384;
256 if (!(buffer
->buffer
= HeapAlloc(GetProcessHeap(), 0, buffer
->buffer_size
)))
258 ERR("Failed to allocate shader buffer memory.\n");
262 shader_buffer_clear(buffer
);
266 void shader_buffer_free(struct wined3d_shader_buffer
*buffer
)
268 HeapFree(GetProcessHeap(), 0, buffer
->buffer
);
271 int shader_vaddline(struct wined3d_shader_buffer
*buffer
, const char *format
, va_list args
)
273 char *base
= buffer
->buffer
+ buffer
->content_size
;
279 rc
= vsnprintf(base
, buffer
->buffer_size
- buffer
->content_size
, format
, args
);
280 if (rc
< 0 /* C89 */ || (unsigned int)rc
>= buffer
->buffer_size
- buffer
->content_size
/* C99 */)
282 new_buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
->buffer
, buffer
->buffer_size
* 2);
285 ERR("The buffer allocated for the shader program string is too small at %d bytes.\n", buffer
->buffer_size
);
286 buffer
->content_size
= buffer
->buffer_size
- 1;
289 buffer
->buffer
= new_buffer
;
290 buffer
->buffer_size
= buffer
->buffer_size
* 2;
291 base
= buffer
->buffer
+ buffer
->content_size
;
301 TRACE("GL HW (%u, %u) : %s", buffer
->lineNo
+ 1, buffer
->content_size
, base
);
302 buffer
->newline
= FALSE
;
309 buffer
->content_size
+= rc
;
310 if (buffer
->buffer
[buffer
->content_size
-1] == '\n')
313 buffer
->newline
= TRUE
;
319 int shader_addline(struct wined3d_shader_buffer
*buffer
, const char *format
, ...)
324 va_start(args
, format
);
325 ret
= shader_vaddline(buffer
, format
, args
);
331 static void shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
332 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
335 shader
->device
= device
;
336 shader
->parent
= parent
;
337 shader
->parent_ops
= parent_ops
;
338 list_init(&shader
->linked_programs
);
339 list_add_head(&device
->shaders
, &shader
->shader_list_entry
);
342 /* Convert floating point offset relative to a register file to an absolute
343 * offset for float constants. */
344 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type
, UINT register_idx
)
346 switch (register_type
)
348 case WINED3DSPR_CONST
: return register_idx
;
349 case WINED3DSPR_CONST2
: return 2048 + register_idx
;
350 case WINED3DSPR_CONST3
: return 4096 + register_idx
;
351 case WINED3DSPR_CONST4
: return 6144 + register_idx
;
353 FIXME("Unsupported register type: %u.\n", register_type
);
358 static void shader_delete_constant_list(struct list
*clist
)
360 struct wined3d_shader_lconst
*constant
;
363 ptr
= list_head(clist
);
366 constant
= LIST_ENTRY(ptr
, struct wined3d_shader_lconst
, entry
);
367 ptr
= list_next(clist
, ptr
);
368 HeapFree(GetProcessHeap(), 0, constant
);
373 static void shader_set_limits(struct wined3d_shader
*shader
)
375 static const struct limits_entry
377 unsigned int min_version
;
378 unsigned int max_version
;
379 struct wined3d_shader_limits limits
;
383 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
384 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
385 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
386 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
387 * even though they are capable of supporting much more (GL
388 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
389 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
391 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
392 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
397 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
398 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
403 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
404 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
405 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
406 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
407 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
408 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 12}},
409 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 0, 32}},
412 const struct limits_entry
*limits_array
;
413 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
414 shader
->reg_maps
.shader_version
.minor
);
417 switch (shader
->reg_maps
.shader_version
.type
)
420 FIXME("Unexpected shader type %u found.\n", shader
->reg_maps
.shader_version
.type
);
422 case WINED3D_SHADER_TYPE_VERTEX
:
423 limits_array
= vs_limits
;
425 case WINED3D_SHADER_TYPE_GEOMETRY
:
426 limits_array
= gs_limits
;
428 case WINED3D_SHADER_TYPE_PIXEL
:
429 limits_array
= ps_limits
;
433 while (limits_array
[i
].min_version
&& limits_array
[i
].min_version
<= shader_version
)
435 if (shader_version
<= limits_array
[i
].max_version
)
437 shader
->limits
= &limits_array
[i
].limits
;
444 FIXME("Unexpected shader version \"%u.%u\".\n",
445 shader
->reg_maps
.shader_version
.major
,
446 shader
->reg_maps
.shader_version
.minor
);
447 shader
->limits
= &limits_array
[max(0, i
- 1)].limits
;
451 static inline void set_bitmap_bit(DWORD
*bitmap
, DWORD bit
)
456 bitmap
[idx
] |= (1 << shift
);
459 static BOOL
shader_record_register_usage(struct wined3d_shader
*shader
, struct wined3d_shader_reg_maps
*reg_maps
,
460 const struct wined3d_shader_register
*reg
, enum wined3d_shader_type shader_type
, unsigned int constf_size
)
464 case WINED3DSPR_TEXTURE
: /* WINED3DSPR_ADDR */
465 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
466 reg_maps
->texcoord
|= 1 << reg
->idx
[0].offset
;
468 reg_maps
->address
|= 1 << reg
->idx
[0].offset
;
471 case WINED3DSPR_TEMP
:
472 reg_maps
->temporary
|= 1 << reg
->idx
[0].offset
;
475 case WINED3DSPR_INPUT
:
476 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
478 if (reg
->idx
[0].rel_addr
)
480 /* If relative addressing is used, we must assume that all registers
481 * are used. Even if it is a construct like v3[aL], we can't assume
482 * that v0, v1 and v2 aren't read because aL can be negative */
484 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
486 shader
->u
.ps
.input_reg_used
[i
] = TRUE
;
491 shader
->u
.ps
.input_reg_used
[reg
->idx
[0].offset
] = TRUE
;
495 reg_maps
->input_registers
|= 1 << reg
->idx
[0].offset
;
498 case WINED3DSPR_RASTOUT
:
499 if (reg
->idx
[0].offset
== 1)
503 case WINED3DSPR_MISCTYPE
:
504 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
506 if (!reg
->idx
[0].offset
)
508 else if (reg
->idx
[0].offset
== 1)
509 reg_maps
->usesfacing
= 1;
513 case WINED3DSPR_CONST
:
514 if (reg
->idx
[0].rel_addr
)
516 if (reg
->idx
[0].offset
< reg_maps
->min_rel_offset
)
517 reg_maps
->min_rel_offset
= reg
->idx
[0].offset
;
518 if (reg
->idx
[0].offset
> reg_maps
->max_rel_offset
)
519 reg_maps
->max_rel_offset
= reg
->idx
[0].offset
;
520 reg_maps
->usesrelconstF
= TRUE
;
524 if (reg
->idx
[0].offset
>= min(shader
->limits
->constant_float
, constf_size
))
526 WARN("Shader using float constant %u which is not supported.\n", reg
->idx
[0].offset
);
531 set_bitmap_bit(reg_maps
->constf
, reg
->idx
[0].offset
);
536 case WINED3DSPR_CONSTINT
:
537 if (reg
->idx
[0].offset
>= shader
->limits
->constant_int
)
539 WARN("Shader using integer constant %u which is not supported.\n", reg
->idx
[0].offset
);
544 reg_maps
->integer_constants
|= (1 << reg
->idx
[0].offset
);
548 case WINED3DSPR_CONSTBOOL
:
549 if (reg
->idx
[0].offset
>= shader
->limits
->constant_bool
)
551 WARN("Shader using bool constant %u which is not supported.\n", reg
->idx
[0].offset
);
556 reg_maps
->boolean_constants
|= (1 << reg
->idx
[0].offset
);
560 case WINED3DSPR_COLOROUT
:
561 reg_maps
->rt_mask
|= (1 << reg
->idx
[0].offset
);
565 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
566 reg
->type
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
572 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr
, unsigned int param
)
576 case WINED3DSIH_M4x4
:
577 case WINED3DSIH_M3x4
:
578 return param
== 1 ? 3 : 0;
580 case WINED3DSIH_M4x3
:
581 case WINED3DSIH_M3x3
:
582 return param
== 1 ? 2 : 0;
584 case WINED3DSIH_M3x2
:
585 return param
== 1 ? 1 : 0;
592 /* Note that this does not count the loop register as an address register. */
593 static HRESULT
shader_get_registers_used(struct wined3d_shader
*shader
, const struct wined3d_shader_frontend
*fe
,
594 struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_shader_signature_element
*input_signature
,
595 struct wined3d_shader_signature_element
*output_signature
, const DWORD
*byte_code
, DWORD constf_size
)
597 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
598 void *fe_data
= shader
->frontend_data
;
599 struct wined3d_shader_version shader_version
;
600 const DWORD
*ptr
= byte_code
;
602 memset(reg_maps
, 0, sizeof(*reg_maps
));
603 reg_maps
->min_rel_offset
= ~0U;
605 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
606 reg_maps
->shader_version
= shader_version
;
608 shader_set_limits(shader
);
610 reg_maps
->constf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
611 sizeof(*reg_maps
->constf
) * ((min(shader
->limits
->constant_float
, constf_size
) + 31) / 32));
612 if (!reg_maps
->constf
)
614 ERR("Failed to allocate constant map memory.\n");
615 return E_OUTOFMEMORY
;
618 while (!fe
->shader_is_end(fe_data
, &ptr
))
620 struct wined3d_shader_instruction ins
;
623 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
625 /* Unhandled opcode, and its parameters. */
626 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
628 TRACE("Skipping unrecognized instruction.\n");
632 /* Handle declarations. */
633 if (ins
.handler_idx
== WINED3DSIH_DCL
)
635 struct wined3d_shader_semantic
*semantic
= &ins
.declaration
.semantic
;
637 switch (semantic
->reg
.reg
.type
)
639 /* Mark input registers used. */
640 case WINED3DSPR_INPUT
:
641 reg_maps
->input_registers
|= 1 << semantic
->reg
.reg
.idx
[0].offset
;
642 shader_signature_from_semantic(&input_signature
[semantic
->reg
.reg
.idx
[0].offset
], semantic
);
645 /* Vertex shader: mark 3.0 output registers used, save token. */
646 case WINED3DSPR_OUTPUT
:
647 reg_maps
->output_registers
|= 1 << semantic
->reg
.reg
.idx
[0].offset
;
648 shader_signature_from_semantic(&output_signature
[semantic
->reg
.reg
.idx
[0].offset
], semantic
);
649 if (semantic
->usage
== WINED3D_DECL_USAGE_FOG
)
653 /* Save sampler usage token. */
654 case WINED3DSPR_SAMPLER
:
655 reg_maps
->sampler_type
[semantic
->reg
.reg
.idx
[0].offset
] = semantic
->sampler_type
;
659 TRACE("Not recording DCL register type %#x.\n", semantic
->reg
.reg
.type
);
663 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
665 struct wined3d_shader_register
*reg
= &ins
.declaration
.src
.reg
;
666 if (reg
->idx
[0].offset
>= WINED3D_MAX_CBS
)
667 ERR("Invalid CB index %u.\n", reg
->idx
[0].offset
);
669 reg_maps
->cb_sizes
[reg
->idx
[0].offset
] = reg
->idx
[1].offset
;
671 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
)
673 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
674 shader
->u
.gs
.input_type
= ins
.declaration
.primitive_type
;
676 FIXME("Invalid instruction %#x for shader type %#x.\n",
677 ins
.handler_idx
, shader_version
.type
);
679 else if (ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
681 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
682 shader
->u
.gs
.output_type
= ins
.declaration
.primitive_type
;
684 FIXME("Invalid instruction %#x for shader type %#x.\n",
685 ins
.handler_idx
, shader_version
.type
);
687 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
689 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
690 shader
->u
.gs
.vertices_out
= ins
.declaration
.count
;
692 FIXME("Invalid instruction %#x for shader type %#x.\n",
693 ins
.handler_idx
, shader_version
.type
);
695 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
697 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
699 if (!lconst
) return E_OUTOFMEMORY
;
701 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
702 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
703 value
= (float *)lconst
->value
;
705 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
706 if (shader_version
.major
== 1 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
708 if (value
[0] < -1.0f
) value
[0] = -1.0f
;
709 else if (value
[0] > 1.0f
) value
[0] = 1.0f
;
710 if (value
[1] < -1.0f
) value
[1] = -1.0f
;
711 else if (value
[1] > 1.0f
) value
[1] = 1.0f
;
712 if (value
[2] < -1.0f
) value
[2] = -1.0f
;
713 else if (value
[2] > 1.0f
) value
[2] = 1.0f
;
714 if (value
[3] < -1.0f
) value
[3] = -1.0f
;
715 else if (value
[3] > 1.0f
) value
[3] = 1.0f
;
718 list_add_head(&shader
->constantsF
, &lconst
->entry
);
720 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
721 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
723 shader
->lconst_inf_or_nan
= TRUE
;
726 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
728 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
729 if (!lconst
) return E_OUTOFMEMORY
;
731 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
732 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
734 list_add_head(&shader
->constantsI
, &lconst
->entry
);
735 reg_maps
->local_int_consts
|= (1 << lconst
->idx
);
737 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
739 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
740 if (!lconst
) return E_OUTOFMEMORY
;
742 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
743 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, sizeof(DWORD
));
745 list_add_head(&shader
->constantsB
, &lconst
->entry
);
746 reg_maps
->local_bool_consts
|= (1 << lconst
->idx
);
748 /* For subroutine prototypes. */
749 else if (ins
.handler_idx
== WINED3DSIH_LABEL
)
751 reg_maps
->labels
|= 1 << ins
.src
[0].reg
.idx
[0].offset
;
753 /* Set texture, address, temporary registers. */
756 BOOL color0_mov
= FALSE
;
759 /* This will loop over all the registers and try to
760 * make a bitmask of the ones we're interested in.
762 * Relative addressing tokens are ignored, but that's
763 * okay, since we'll catch any address registers when
764 * they are initialized (required by spec). */
765 for (i
= 0; i
< ins
.dst_count
; ++i
)
767 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.dst
[i
].reg
,
768 shader_version
.type
, constf_size
))
769 return WINED3DERR_INVALIDCALL
;
771 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
772 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
773 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
774 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
&& shader_version
.major
< 3)
776 UINT idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
778 switch (ins
.dst
[i
].reg
.type
)
780 case WINED3DSPR_RASTOUT
:
784 reg_maps
->output_registers
|= 1 << 10;
785 shader_signature_from_usage(&output_signature
[10],
786 WINED3D_DECL_USAGE_POSITION
, 0, 10, WINED3DSP_WRITEMASK_ALL
);
790 reg_maps
->output_registers
|= 1 << 11;
791 shader_signature_from_usage(&output_signature
[11],
792 WINED3D_DECL_USAGE_FOG
, 0, 11, WINED3DSP_WRITEMASK_0
);
796 reg_maps
->output_registers
|= 1 << 11;
797 shader_signature_from_usage(&output_signature
[11],
798 WINED3D_DECL_USAGE_PSIZE
, 0, 11, WINED3DSP_WRITEMASK_1
);
803 case WINED3DSPR_ATTROUT
:
807 if (reg_maps
->output_registers
& (1 << idx
))
809 output_signature
[idx
].mask
|= ins
.dst
[i
].write_mask
;
813 reg_maps
->output_registers
|= 1 << idx
;
814 shader_signature_from_usage(&output_signature
[idx
],
815 WINED3D_DECL_USAGE_COLOR
, idx
- 8, idx
, ins
.dst
[i
].write_mask
);
820 case WINED3DSPR_TEXCRDOUT
:
822 reg_maps
->texcoord_mask
[idx
] |= ins
.dst
[i
].write_mask
;
823 if (reg_maps
->output_registers
& (1 << idx
))
825 output_signature
[idx
].mask
|= ins
.dst
[i
].write_mask
;
829 reg_maps
->output_registers
|= 1 << idx
;
830 shader_signature_from_usage(&output_signature
[idx
],
831 WINED3D_DECL_USAGE_TEXCOORD
, idx
, idx
, ins
.dst
[i
].write_mask
);
840 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
842 if (ins
.dst
[i
].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
.dst
[i
].reg
.idx
[0].offset
)
844 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
845 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
846 * the mov and perform the sRGB write correction from the source register.
848 * However, if the mov is only partial, we can't do this, and if the write
849 * comes from an instruction other than MOV it is hard to do as well. If
850 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
851 shader
->u
.ps
.color0_mov
= FALSE
;
852 if (ins
.handler_idx
== WINED3DSIH_MOV
853 && ins
.dst
[i
].write_mask
== WINED3DSP_WRITEMASK_ALL
)
855 /* Used later when the source register is read. */
859 /* Also drop the MOV marker if the source register is overwritten prior to the shader
862 else if (ins
.dst
[i
].reg
.type
== WINED3DSPR_TEMP
863 && ins
.dst
[i
].reg
.idx
[0].offset
== shader
->u
.ps
.color0_reg
)
865 shader
->u
.ps
.color0_mov
= FALSE
;
869 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
870 if (shader_version
.major
== 1
871 && (ins
.handler_idx
== WINED3DSIH_TEX
872 || ins
.handler_idx
== WINED3DSIH_TEXBEM
873 || ins
.handler_idx
== WINED3DSIH_TEXBEML
874 || ins
.handler_idx
== WINED3DSIH_TEXDP3TEX
875 || ins
.handler_idx
== WINED3DSIH_TEXM3x2TEX
876 || ins
.handler_idx
== WINED3DSIH_TEXM3x3SPEC
877 || ins
.handler_idx
== WINED3DSIH_TEXM3x3TEX
878 || ins
.handler_idx
== WINED3DSIH_TEXM3x3VSPEC
879 || ins
.handler_idx
== WINED3DSIH_TEXREG2AR
880 || ins
.handler_idx
== WINED3DSIH_TEXREG2GB
881 || ins
.handler_idx
== WINED3DSIH_TEXREG2RGB
))
883 /* Fake sampler usage, only set reserved bit and type. */
884 DWORD sampler_code
= ins
.dst
[i
].reg
.idx
[0].offset
;
886 TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
887 reg_maps
->sampler_type
[sampler_code
] = WINED3DSTT_2D
;
889 /* texbem is only valid with < 1.4 pixel shaders */
890 if (ins
.handler_idx
== WINED3DSIH_TEXBEM
891 || ins
.handler_idx
== WINED3DSIH_TEXBEML
)
893 reg_maps
->bumpmat
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
894 if (ins
.handler_idx
== WINED3DSIH_TEXBEML
)
896 reg_maps
->luminanceparams
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
900 else if (ins
.handler_idx
== WINED3DSIH_BEM
)
902 reg_maps
->bumpmat
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
906 if (ins
.handler_idx
== WINED3DSIH_NRM
) reg_maps
->usesnrm
= 1;
907 else if (ins
.handler_idx
== WINED3DSIH_DSY
) reg_maps
->usesdsy
= 1;
908 else if (ins
.handler_idx
== WINED3DSIH_DSX
) reg_maps
->usesdsx
= 1;
909 else if (ins
.handler_idx
== WINED3DSIH_TEXLDD
) reg_maps
->usestexldd
= 1;
910 else if (ins
.handler_idx
== WINED3DSIH_TEXLDL
) reg_maps
->usestexldl
= 1;
911 else if (ins
.handler_idx
== WINED3DSIH_MOVA
) reg_maps
->usesmova
= 1;
912 else if (ins
.handler_idx
== WINED3DSIH_IFC
) reg_maps
->usesifc
= 1;
913 else if (ins
.handler_idx
== WINED3DSIH_CALL
) reg_maps
->usescall
= 1;
914 else if (ins
.handler_idx
== WINED3DSIH_POW
) reg_maps
->usespow
= 1;
915 else if (ins
.handler_idx
== WINED3DSIH_LOOP
916 || ins
.handler_idx
== WINED3DSIH_REP
)
919 if (cur_loop_depth
> max_loop_depth
)
920 max_loop_depth
= cur_loop_depth
;
922 else if (ins
.handler_idx
== WINED3DSIH_ENDLOOP
923 || ins
.handler_idx
== WINED3DSIH_ENDREP
)
927 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.predicate
->reg
,
928 shader_version
.type
, constf_size
))
929 return WINED3DERR_INVALIDCALL
;
931 for (i
= 0; i
< ins
.src_count
; ++i
)
933 unsigned int count
= get_instr_extra_regcount(ins
.handler_idx
, i
);
934 struct wined3d_shader_register reg
= ins
.src
[i
].reg
;
936 if (!shader_record_register_usage(shader
, reg_maps
, &ins
.src
[i
].reg
,
937 shader_version
.type
, constf_size
))
938 return WINED3DERR_INVALIDCALL
;
942 if (!shader_record_register_usage(shader
, reg_maps
, ®
,
943 shader_version
.type
, constf_size
))
944 return WINED3DERR_INVALIDCALL
;
950 if (ins
.src
[i
].reg
.type
== WINED3DSPR_TEMP
951 && ins
.src
[i
].swizzle
== WINED3DSP_NOSWIZZLE
)
953 shader
->u
.ps
.color0_mov
= TRUE
;
954 shader
->u
.ps
.color0_reg
= ins
.src
[i
].reg
.idx
[0].offset
;
960 reg_maps
->loop_depth
= max_loop_depth
;
962 /* PS before 2.0 don't have explicit color outputs. Instead the value of
963 * R0 is written to the render target. */
964 if (shader_version
.major
< 2 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
965 reg_maps
->rt_mask
|= (1 << 0);
967 shader
->functionLength
= ((const char *)ptr
- (const char *)byte_code
);
972 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int max
)
974 DWORD map
= 1 << max
;
976 map
&= reg_maps
->shader_version
.major
< 3 ? ~reg_maps
->texcoord
: ~reg_maps
->input_registers
;
978 return wined3d_log2i(map
);
981 static void shader_dump_decl_usage(const struct wined3d_shader_semantic
*semantic
,
982 const struct wined3d_shader_version
*shader_version
)
986 if (semantic
->reg
.reg
.type
== WINED3DSPR_SAMPLER
)
988 switch (semantic
->sampler_type
)
990 case WINED3DSTT_2D
: TRACE("_2d"); break;
991 case WINED3DSTT_CUBE
: TRACE("_cube"); break;
992 case WINED3DSTT_VOLUME
: TRACE("_volume"); break;
993 default: TRACE("_unknown_ttype(0x%08x)", semantic
->sampler_type
);
998 /* Pixel shaders 3.0 don't have usage semantics. */
999 if (shader_version
->major
< 3 && shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
) return;
1002 switch (semantic
->usage
)
1004 case WINED3D_DECL_USAGE_POSITION
:
1005 TRACE("position%u", semantic
->usage_idx
);
1008 case WINED3D_DECL_USAGE_BLEND_INDICES
:
1012 case WINED3D_DECL_USAGE_BLEND_WEIGHT
:
1016 case WINED3D_DECL_USAGE_NORMAL
:
1017 TRACE("normal%u", semantic
->usage_idx
);
1020 case WINED3D_DECL_USAGE_PSIZE
:
1024 case WINED3D_DECL_USAGE_COLOR
:
1025 if (!semantic
->usage_idx
) TRACE("color");
1026 else TRACE("specular%u", (semantic
->usage_idx
- 1));
1029 case WINED3D_DECL_USAGE_TEXCOORD
:
1030 TRACE("texture%u", semantic
->usage_idx
);
1033 case WINED3D_DECL_USAGE_TANGENT
:
1037 case WINED3D_DECL_USAGE_BINORMAL
:
1041 case WINED3D_DECL_USAGE_TESS_FACTOR
:
1042 TRACE("tessfactor");
1045 case WINED3D_DECL_USAGE_POSITIONT
:
1046 TRACE("positionT%u", semantic
->usage_idx
);
1049 case WINED3D_DECL_USAGE_FOG
:
1053 case WINED3D_DECL_USAGE_DEPTH
:
1057 case WINED3D_DECL_USAGE_SAMPLE
:
1062 FIXME("unknown_semantics(0x%08x)", semantic
->usage
);
1067 static void shader_dump_register(const struct wined3d_shader_register
*reg
,
1068 const struct wined3d_shader_version
*shader_version
)
1070 static const char * const rastout_reg_names
[] = {"oPos", "oFog", "oPts"};
1071 static const char * const misctype_reg_names
[] = {"vPos", "vFace"};
1072 UINT offset
= reg
->idx
[0].offset
;
1076 case WINED3DSPR_TEMP
:
1080 case WINED3DSPR_INPUT
:
1084 case WINED3DSPR_CONST
:
1085 case WINED3DSPR_CONST2
:
1086 case WINED3DSPR_CONST3
:
1087 case WINED3DSPR_CONST4
:
1089 offset
= shader_get_float_offset(reg
->type
, offset
);
1092 case WINED3DSPR_TEXTURE
: /* vs: case WINED3DSPR_ADDR */
1093 TRACE("%c", shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
? 't' : 'a');
1096 case WINED3DSPR_RASTOUT
:
1097 TRACE("%s", rastout_reg_names
[offset
]);
1100 case WINED3DSPR_COLOROUT
:
1104 case WINED3DSPR_DEPTHOUT
:
1108 case WINED3DSPR_ATTROUT
:
1112 case WINED3DSPR_TEXCRDOUT
:
1113 /* Vertex shaders >= 3.0 use general purpose output registers
1114 * (WINED3DSPR_OUTPUT), which can include an address token. */
1115 if (shader_version
->major
>= 3) TRACE("o");
1119 case WINED3DSPR_CONSTINT
:
1123 case WINED3DSPR_CONSTBOOL
:
1127 case WINED3DSPR_LABEL
:
1131 case WINED3DSPR_LOOP
:
1135 case WINED3DSPR_SAMPLER
:
1139 case WINED3DSPR_MISCTYPE
:
1141 FIXME("Unhandled misctype register %u.\n", offset
);
1143 TRACE("%s", misctype_reg_names
[offset
]);
1146 case WINED3DSPR_PREDICATE
:
1150 case WINED3DSPR_IMMCONST
:
1154 case WINED3DSPR_CONSTBUFFER
:
1158 case WINED3DSPR_PRIMID
:
1162 case WINED3DSPR_NULL
:
1166 case WINED3DSPR_RESOURCE
:
1171 TRACE("unhandled_rtype(%#x)", reg
->type
);
1175 if (reg
->type
== WINED3DSPR_IMMCONST
)
1178 switch (reg
->immconst_type
)
1180 case WINED3D_IMMCONST_SCALAR
:
1181 switch (reg
->data_type
)
1183 case WINED3D_DATA_FLOAT
:
1184 TRACE("%.8e", *(const float *)reg
->immconst_data
);
1186 case WINED3D_DATA_INT
:
1187 TRACE("%d", reg
->immconst_data
[0]);
1189 case WINED3D_DATA_RESOURCE
:
1190 case WINED3D_DATA_SAMPLER
:
1191 case WINED3D_DATA_UINT
:
1192 TRACE("%u", reg
->immconst_data
[0]);
1195 TRACE("<unhandled data type %#x>", reg
->data_type
);
1200 case WINED3D_IMMCONST_VEC4
:
1201 switch (reg
->data_type
)
1203 case WINED3D_DATA_FLOAT
:
1204 TRACE("%.8e, %.8e, %.8e, %.8e",
1205 *(const float *)®
->immconst_data
[0], *(const float *)®
->immconst_data
[1],
1206 *(const float *)®
->immconst_data
[2], *(const float *)®
->immconst_data
[3]);
1208 case WINED3D_DATA_INT
:
1209 TRACE("%d, %d, %d, %d",
1210 reg
->immconst_data
[0], reg
->immconst_data
[1],
1211 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1213 case WINED3D_DATA_RESOURCE
:
1214 case WINED3D_DATA_SAMPLER
:
1215 case WINED3D_DATA_UINT
:
1216 TRACE("%u, %u, %u, %u",
1217 reg
->immconst_data
[0], reg
->immconst_data
[1],
1218 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1221 TRACE("<unhandled data type %#x>", reg
->data_type
);
1227 TRACE("<unhandled immconst_type %#x>", reg
->immconst_type
);
1232 else if (reg
->type
!= WINED3DSPR_RASTOUT
1233 && reg
->type
!= WINED3DSPR_MISCTYPE
1234 && reg
->type
!= WINED3DSPR_NULL
)
1239 if (reg
->idx
[0].rel_addr
)
1241 shader_dump_src_param(reg
->idx
[0].rel_addr
, shader_version
);
1244 TRACE("%u]", offset
);
1246 if (reg
->idx
[1].offset
!= ~0U)
1249 if (reg
->idx
[1].rel_addr
)
1251 shader_dump_src_param(reg
->idx
[1].rel_addr
, shader_version
);
1254 TRACE("%u]", reg
->idx
[1].offset
);
1260 void shader_dump_dst_param(const struct wined3d_shader_dst_param
*param
,
1261 const struct wined3d_shader_version
*shader_version
)
1263 DWORD write_mask
= param
->write_mask
;
1265 shader_dump_register(¶m
->reg
, shader_version
);
1267 if (write_mask
&& write_mask
!= WINED3DSP_WRITEMASK_ALL
)
1269 static const char write_mask_chars
[] = "xyzw";
1272 if (write_mask
& WINED3DSP_WRITEMASK_0
) TRACE("%c", write_mask_chars
[0]);
1273 if (write_mask
& WINED3DSP_WRITEMASK_1
) TRACE("%c", write_mask_chars
[1]);
1274 if (write_mask
& WINED3DSP_WRITEMASK_2
) TRACE("%c", write_mask_chars
[2]);
1275 if (write_mask
& WINED3DSP_WRITEMASK_3
) TRACE("%c", write_mask_chars
[3]);
1279 void shader_dump_src_param(const struct wined3d_shader_src_param
*param
,
1280 const struct wined3d_shader_version
*shader_version
)
1282 enum wined3d_shader_src_modifier src_modifier
= param
->modifiers
;
1283 DWORD swizzle
= param
->swizzle
;
1285 if (src_modifier
== WINED3DSPSM_NEG
1286 || src_modifier
== WINED3DSPSM_BIASNEG
1287 || src_modifier
== WINED3DSPSM_SIGNNEG
1288 || src_modifier
== WINED3DSPSM_X2NEG
1289 || src_modifier
== WINED3DSPSM_ABSNEG
)
1291 else if (src_modifier
== WINED3DSPSM_COMP
)
1293 else if (src_modifier
== WINED3DSPSM_NOT
)
1296 if (src_modifier
== WINED3DSPSM_ABS
|| src_modifier
== WINED3DSPSM_ABSNEG
)
1299 shader_dump_register(¶m
->reg
, shader_version
);
1303 switch (src_modifier
)
1305 case WINED3DSPSM_NONE
: break;
1306 case WINED3DSPSM_NEG
: break;
1307 case WINED3DSPSM_NOT
: break;
1308 case WINED3DSPSM_BIAS
: TRACE("_bias"); break;
1309 case WINED3DSPSM_BIASNEG
: TRACE("_bias"); break;
1310 case WINED3DSPSM_SIGN
: TRACE("_bx2"); break;
1311 case WINED3DSPSM_SIGNNEG
: TRACE("_bx2"); break;
1312 case WINED3DSPSM_COMP
: break;
1313 case WINED3DSPSM_X2
: TRACE("_x2"); break;
1314 case WINED3DSPSM_X2NEG
: TRACE("_x2"); break;
1315 case WINED3DSPSM_DZ
: TRACE("_dz"); break;
1316 case WINED3DSPSM_DW
: TRACE("_dw"); break;
1317 case WINED3DSPSM_ABSNEG
: TRACE(")"); break;
1318 case WINED3DSPSM_ABS
: TRACE(")"); break;
1319 default: TRACE("_unknown_modifier(%#x)", src_modifier
);
1323 if (swizzle
!= WINED3DSP_NOSWIZZLE
)
1325 static const char swizzle_chars
[] = "xyzw";
1326 DWORD swizzle_x
= swizzle
& 0x03;
1327 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
1328 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
1329 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
1331 if (swizzle_x
== swizzle_y
1332 && swizzle_x
== swizzle_z
1333 && swizzle_x
== swizzle_w
)
1335 TRACE(".%c", swizzle_chars
[swizzle_x
]);
1339 TRACE(".%c%c%c%c", swizzle_chars
[swizzle_x
], swizzle_chars
[swizzle_y
],
1340 swizzle_chars
[swizzle_z
], swizzle_chars
[swizzle_w
]);
1345 /* Shared code in order to generate the bulk of the shader string.
1346 * NOTE: A description of how to parse tokens can be found on MSDN. */
1347 void shader_generate_main(const struct wined3d_shader
*shader
, struct wined3d_shader_buffer
*buffer
,
1348 const struct wined3d_shader_reg_maps
*reg_maps
, const DWORD
*byte_code
, void *backend_ctx
)
1350 struct wined3d_device
*device
= shader
->device
;
1351 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
1352 void *fe_data
= shader
->frontend_data
;
1353 struct wined3d_shader_version shader_version
;
1354 struct wined3d_shader_loop_state loop_state
;
1355 struct wined3d_shader_instruction ins
;
1356 struct wined3d_shader_tex_mx tex_mx
;
1357 struct wined3d_shader_context ctx
;
1358 const DWORD
*ptr
= byte_code
;
1360 /* Initialize current parsing state. */
1361 tex_mx
.current_row
= 0;
1362 loop_state
.current_depth
= 0;
1363 loop_state
.current_reg
= 0;
1365 ctx
.shader
= shader
;
1366 ctx
.gl_info
= &device
->adapter
->gl_info
;
1367 ctx
.reg_maps
= reg_maps
;
1368 ctx
.buffer
= buffer
;
1369 ctx
.tex_mx
= &tex_mx
;
1370 ctx
.loop_state
= &loop_state
;
1371 ctx
.backend_data
= backend_ctx
;
1374 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1376 while (!fe
->shader_is_end(fe_data
, &ptr
))
1379 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1381 /* Unknown opcode and its parameters. */
1382 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1384 TRACE("Skipping unrecognized instruction.\n");
1389 FIXME("Predicates not implemented.\n");
1391 /* Call appropriate function for output target */
1392 device
->shader_backend
->shader_handle_instruction(&ins
);
1396 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param
*dst
)
1398 DWORD mmask
= dst
->modifiers
;
1403 case 13: TRACE("_d8"); break;
1404 case 14: TRACE("_d4"); break;
1405 case 15: TRACE("_d2"); break;
1406 case 1: TRACE("_x2"); break;
1407 case 2: TRACE("_x4"); break;
1408 case 3: TRACE("_x8"); break;
1409 default: TRACE("_unhandled_shift(%d)", dst
->shift
); break;
1412 if (mmask
& WINED3DSPDM_SATURATE
) TRACE("_sat");
1413 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) TRACE("_pp");
1414 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) TRACE("_centroid");
1416 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
1417 if (mmask
) FIXME("_unrecognized_modifier(%#x)", mmask
);
1420 static void shader_dump_primitive_type(enum wined3d_primitive_type primitive_type
)
1422 switch (primitive_type
)
1424 case WINED3D_PT_UNDEFINED
:
1427 case WINED3D_PT_POINTLIST
:
1430 case WINED3D_PT_LINELIST
:
1433 case WINED3D_PT_LINESTRIP
:
1436 case WINED3D_PT_TRIANGLELIST
:
1437 TRACE("trianglelist");
1439 case WINED3D_PT_TRIANGLESTRIP
:
1440 TRACE("trianglestrip");
1442 case WINED3D_PT_TRIANGLEFAN
:
1443 TRACE("trianglefan");
1445 case WINED3D_PT_LINELIST_ADJ
:
1446 TRACE("linelist_adj");
1448 case WINED3D_PT_LINESTRIP_ADJ
:
1449 TRACE("linestrip_adj");
1451 case WINED3D_PT_TRIANGLELIST_ADJ
:
1452 TRACE("trianglelist_adj");
1454 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
1455 TRACE("trianglestrip_adj");
1458 TRACE("<unrecognized_primitive_type %#x>", primitive_type
);
1463 static void shader_trace_init(const struct wined3d_shader_frontend
*fe
, void *fe_data
, const DWORD
*byte_code
)
1465 struct wined3d_shader_version shader_version
;
1466 const DWORD
*ptr
= byte_code
;
1467 const char *type_prefix
;
1470 TRACE("Parsing %p.\n", byte_code
);
1472 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1474 switch (shader_version
.type
)
1476 case WINED3D_SHADER_TYPE_VERTEX
:
1480 case WINED3D_SHADER_TYPE_GEOMETRY
:
1484 case WINED3D_SHADER_TYPE_PIXEL
:
1489 FIXME("Unhandled shader type %#x.\n", shader_version
.type
);
1490 type_prefix
= "unknown";
1494 TRACE("%s_%u_%u\n", type_prefix
, shader_version
.major
, shader_version
.minor
);
1496 while (!fe
->shader_is_end(fe_data
, &ptr
))
1498 struct wined3d_shader_instruction ins
;
1500 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1501 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1503 TRACE("Skipping unrecognized instruction.\n");
1507 if (ins
.handler_idx
== WINED3DSIH_DCL
)
1509 shader_dump_decl_usage(&ins
.declaration
.semantic
, &shader_version
);
1510 shader_dump_ins_modifiers(&ins
.declaration
.semantic
.reg
);
1512 shader_dump_dst_param(&ins
.declaration
.semantic
.reg
, &shader_version
);
1514 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
1516 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1517 shader_dump_src_param(&ins
.declaration
.src
, &shader_version
);
1518 TRACE(", %s", ins
.flags
& WINED3DSI_INDEXED_DYNAMIC
? "dynamicIndexed" : "immediateIndexed");
1520 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
1521 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
1523 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1524 shader_dump_primitive_type(ins
.declaration
.primitive_type
);
1526 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
1528 TRACE("%s %u", shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.count
);
1530 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
1532 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(ins
.dst
[0].reg
.type
,
1533 ins
.dst
[0].reg
.idx
[0].offset
),
1534 *(const float *)&ins
.src
[0].reg
.immconst_data
[0],
1535 *(const float *)&ins
.src
[0].reg
.immconst_data
[1],
1536 *(const float *)&ins
.src
[0].reg
.immconst_data
[2],
1537 *(const float *)&ins
.src
[0].reg
.immconst_data
[3]);
1539 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
1541 TRACE("defi i%u = %d, %d, %d, %d", ins
.dst
[0].reg
.idx
[0].offset
,
1542 ins
.src
[0].reg
.immconst_data
[0],
1543 ins
.src
[0].reg
.immconst_data
[1],
1544 ins
.src
[0].reg
.immconst_data
[2],
1545 ins
.src
[0].reg
.immconst_data
[3]);
1547 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
1549 TRACE("defb b%u = %s", ins
.dst
[0].reg
.idx
[0].offset
, ins
.src
[0].reg
.immconst_data
[0] ? "true" : "false");
1556 shader_dump_src_param(ins
.predicate
, &shader_version
);
1560 /* PixWin marks instructions with the coissue flag with a '+' */
1561 if (ins
.coissue
) TRACE("+");
1563 TRACE("%s", shader_opcode_names
[ins
.handler_idx
]);
1565 if (ins
.handler_idx
== WINED3DSIH_IFC
1566 || ins
.handler_idx
== WINED3DSIH_BREAKC
)
1570 case WINED3D_SHADER_REL_OP_GT
: TRACE("_gt"); break;
1571 case WINED3D_SHADER_REL_OP_EQ
: TRACE("_eq"); break;
1572 case WINED3D_SHADER_REL_OP_GE
: TRACE("_ge"); break;
1573 case WINED3D_SHADER_REL_OP_LT
: TRACE("_lt"); break;
1574 case WINED3D_SHADER_REL_OP_NE
: TRACE("_ne"); break;
1575 case WINED3D_SHADER_REL_OP_LE
: TRACE("_le"); break;
1576 default: TRACE("_(%u)", ins
.flags
);
1579 else if (ins
.handler_idx
== WINED3DSIH_TEX
1580 && shader_version
.major
>= 2
1581 && (ins
.flags
& WINED3DSI_TEXLD_PROJECT
))
1586 for (i
= 0; i
< ins
.dst_count
; ++i
)
1588 shader_dump_ins_modifiers(&ins
.dst
[i
]);
1589 TRACE(!i
? " " : ", ");
1590 shader_dump_dst_param(&ins
.dst
[i
], &shader_version
);
1593 /* Other source tokens */
1594 for (i
= ins
.dst_count
; i
< (ins
.dst_count
+ ins
.src_count
); ++i
)
1596 TRACE(!i
? " " : ", ");
1597 shader_dump_src_param(&ins
.src
[i
- ins
.dst_count
], &shader_version
);
1604 static void shader_cleanup(struct wined3d_shader
*shader
)
1606 shader
->device
->shader_backend
->shader_destroy(shader
);
1607 HeapFree(GetProcessHeap(), 0, shader
->reg_maps
.constf
);
1608 HeapFree(GetProcessHeap(), 0, shader
->function
);
1609 shader_delete_constant_list(&shader
->constantsF
);
1610 shader_delete_constant_list(&shader
->constantsB
);
1611 shader_delete_constant_list(&shader
->constantsI
);
1612 list_remove(&shader
->shader_list_entry
);
1614 if (shader
->frontend
&& shader
->frontend_data
)
1615 shader
->frontend
->shader_free(shader
->frontend_data
);
1618 struct shader_none_priv
1620 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
1621 const struct fragment_pipeline
*fragment_pipe
;
1622 BOOL ffp_proj_control
;
1625 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
1626 static void shader_none_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
1627 enum tex_types tex_type
, const SIZE
*ds_mask_size
) {}
1628 static void shader_none_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
) {}
1629 static void shader_none_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1630 static void shader_none_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1631 static void shader_none_load_constants(void *shader_priv
, struct wined3d_context
*context
,
1632 const struct wined3d_state
*state
) {}
1633 static void shader_none_destroy(struct wined3d_shader
*shader
) {}
1634 static void shader_none_free_context_data(struct wined3d_context
*context
) {}
1636 /* Context activation is done by the caller. */
1637 static void shader_none_select(void *shader_priv
, struct wined3d_context
*context
,
1638 const struct wined3d_state
*state
)
1640 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1641 struct shader_none_priv
*priv
= shader_priv
;
1643 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
1644 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
1647 /* Context activation is done by the caller. */
1648 static void shader_none_disable(void *shader_priv
, struct wined3d_context
*context
)
1650 struct shader_none_priv
*priv
= shader_priv
;
1651 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1653 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
1654 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
1656 context
->shader_update_mask
= (1 << WINED3D_SHADER_TYPE_PIXEL
)
1657 | (1 << WINED3D_SHADER_TYPE_VERTEX
)
1658 | (1 << WINED3D_SHADER_TYPE_GEOMETRY
);
1661 static HRESULT
shader_none_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
1662 const struct fragment_pipeline
*fragment_pipe
)
1664 struct fragment_caps fragment_caps
;
1665 void *vertex_priv
, *fragment_priv
;
1666 struct shader_none_priv
*priv
;
1668 if (!(priv
= HeapAlloc(GetProcessHeap(), 0, sizeof(*priv
))))
1669 return E_OUTOFMEMORY
;
1671 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&none_shader_backend
, priv
)))
1673 ERR("Failed to initialize vertex pipe.\n");
1674 HeapFree(GetProcessHeap(), 0, priv
);
1678 if (!(fragment_priv
= fragment_pipe
->alloc_private(&none_shader_backend
, priv
)))
1680 ERR("Failed to initialize fragment pipe.\n");
1681 vertex_pipe
->vp_free(device
);
1682 HeapFree(GetProcessHeap(), 0, priv
);
1686 priv
->vertex_pipe
= vertex_pipe
;
1687 priv
->fragment_pipe
= fragment_pipe
;
1688 fragment_pipe
->get_caps(&device
->adapter
->gl_info
, &fragment_caps
);
1689 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
1691 device
->vertex_priv
= vertex_priv
;
1692 device
->fragment_priv
= fragment_priv
;
1693 device
->shader_priv
= priv
;
1698 static void shader_none_free(struct wined3d_device
*device
)
1700 struct shader_none_priv
*priv
= device
->shader_priv
;
1702 priv
->fragment_pipe
->free_private(device
);
1703 priv
->vertex_pipe
->vp_free(device
);
1704 HeapFree(GetProcessHeap(), 0, priv
);
1707 static BOOL
shader_none_allocate_context_data(struct wined3d_context
*context
)
1712 static void shader_none_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
1714 /* Set the shader caps to 0 for the none shader backend */
1715 caps
->vs_version
= 0;
1716 caps
->gs_version
= 0;
1717 caps
->ps_version
= 0;
1718 caps
->vs_uniform_count
= 0;
1719 caps
->ps_uniform_count
= 0;
1720 caps
->ps_1x_max_value
= 0.0f
;
1721 caps
->wined3d_caps
= 0;
1724 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
1726 /* We "support" every possible fixup, since we don't support any shader
1727 * model, and will never have to actually sample a texture. */
1731 static BOOL
shader_none_has_ffp_proj_control(void *shader_priv
)
1733 struct shader_none_priv
*priv
= shader_priv
;
1735 return priv
->ffp_proj_control
;
1738 const struct wined3d_shader_backend_ops none_shader_backend
=
1740 shader_none_handle_instruction
,
1742 shader_none_disable
,
1743 shader_none_select_depth_blt
,
1744 shader_none_deselect_depth_blt
,
1745 shader_none_update_float_vertex_constants
,
1746 shader_none_update_float_pixel_constants
,
1747 shader_none_load_constants
,
1748 shader_none_destroy
,
1751 shader_none_allocate_context_data
,
1752 shader_none_free_context_data
,
1753 shader_none_get_caps
,
1754 shader_none_color_fixup_supported
,
1755 shader_none_has_ffp_proj_control
,
1758 static HRESULT
shader_set_function(struct wined3d_shader
*shader
, const DWORD
*byte_code
,
1759 const struct wined3d_shader_signature
*output_signature
, DWORD float_const_count
,
1760 enum wined3d_shader_type type
, unsigned int max_version
)
1762 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1763 const struct wined3d_shader_frontend
*fe
;
1765 unsigned int backend_version
;
1766 const struct wined3d_d3d_info
*d3d_info
= &shader
->device
->adapter
->d3d_info
;
1768 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1769 shader
, byte_code
, output_signature
, float_const_count
);
1771 fe
= shader_select_frontend(*byte_code
);
1774 FIXME("Unable to find frontend for shader.\n");
1775 return WINED3DERR_INVALIDCALL
;
1777 shader
->frontend
= fe
;
1778 shader
->frontend_data
= fe
->shader_init(byte_code
, output_signature
);
1779 if (!shader
->frontend_data
)
1781 FIXME("Failed to initialize frontend.\n");
1782 return WINED3DERR_INVALIDCALL
;
1785 /* First pass: trace shader. */
1786 if (TRACE_ON(d3d_shader
))
1787 shader_trace_init(fe
, shader
->frontend_data
, byte_code
);
1789 /* Initialize immediate constant lists. */
1790 list_init(&shader
->constantsF
);
1791 list_init(&shader
->constantsB
);
1792 list_init(&shader
->constantsI
);
1793 shader
->lconst_inf_or_nan
= FALSE
;
1795 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1796 hr
= shader_get_registers_used(shader
, fe
,
1797 reg_maps
, shader
->input_signature
, shader
->output_signature
,
1798 byte_code
, float_const_count
);
1799 if (FAILED(hr
)) return hr
;
1801 if (reg_maps
->shader_version
.type
!= type
)
1803 WARN("Wrong shader type %d.\n", reg_maps
->shader_version
.type
);
1804 return WINED3DERR_INVALIDCALL
;
1806 if (reg_maps
->shader_version
.major
> max_version
)
1808 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps
->shader_version
.major
);
1809 return WINED3DERR_INVALIDCALL
;
1813 case WINED3D_SHADER_TYPE_VERTEX
:
1814 backend_version
= d3d_info
->limits
.vs_version
;
1816 case WINED3D_SHADER_TYPE_GEOMETRY
:
1817 backend_version
= d3d_info
->limits
.gs_version
;
1819 case WINED3D_SHADER_TYPE_PIXEL
:
1820 backend_version
= d3d_info
->limits
.ps_version
;
1823 FIXME("No backend version-checking for this shader type\n");
1824 backend_version
= 0;
1826 if (reg_maps
->shader_version
.major
> backend_version
)
1828 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
1829 reg_maps
->shader_version
.major
, reg_maps
->shader_version
.minor
);
1830 return WINED3DERR_INVALIDCALL
;
1833 shader
->function
= HeapAlloc(GetProcessHeap(), 0, shader
->functionLength
);
1834 if (!shader
->function
)
1835 return E_OUTOFMEMORY
;
1836 memcpy(shader
->function
, byte_code
, shader
->functionLength
);
1841 ULONG CDECL
wined3d_shader_incref(struct wined3d_shader
*shader
)
1843 ULONG refcount
= InterlockedIncrement(&shader
->ref
);
1845 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
1850 ULONG CDECL
wined3d_shader_decref(struct wined3d_shader
*shader
)
1852 ULONG refcount
= InterlockedDecrement(&shader
->ref
);
1854 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
1858 shader_cleanup(shader
);
1859 shader
->parent_ops
->wined3d_object_destroyed(shader
->parent
);
1860 HeapFree(GetProcessHeap(), 0, shader
);
1866 void * CDECL
wined3d_shader_get_parent(const struct wined3d_shader
*shader
)
1868 TRACE("shader %p.\n", shader
);
1870 return shader
->parent
;
1873 HRESULT CDECL
wined3d_shader_get_byte_code(const struct wined3d_shader
*shader
,
1874 void *byte_code
, UINT
*byte_code_size
)
1876 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader
, byte_code
, byte_code_size
);
1880 *byte_code_size
= shader
->functionLength
;
1884 if (*byte_code_size
< shader
->functionLength
)
1886 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
1887 * than the required size we should write the required size and
1888 * return D3DERR_MOREDATA. That's not actually true. */
1889 return WINED3DERR_INVALIDCALL
;
1892 memcpy(byte_code
, shader
->function
, shader
->functionLength
);
1897 /* Set local constants for d3d8 shaders. */
1898 HRESULT CDECL
wined3d_shader_set_local_constants_float(struct wined3d_shader
*shader
,
1899 UINT start_idx
, const float *src_data
, UINT count
)
1901 UINT end_idx
= start_idx
+ count
;
1904 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader
, start_idx
, src_data
, count
);
1906 if (end_idx
> shader
->limits
->constant_float
)
1908 WARN("end_idx %u > float constants limit %u.\n",
1909 end_idx
, shader
->limits
->constant_float
);
1910 end_idx
= shader
->limits
->constant_float
;
1913 for (i
= start_idx
; i
< end_idx
; ++i
)
1915 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
1918 return E_OUTOFMEMORY
;
1921 value
= (float *)lconst
->value
;
1922 memcpy(value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
1923 list_add_head(&shader
->constantsF
, &lconst
->entry
);
1925 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
1926 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
1928 shader
->lconst_inf_or_nan
= TRUE
;
1935 void find_vs_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
1936 WORD swizzle_map
, struct vs_compile_args
*args
)
1938 args
->fog_src
= state
->render_states
[WINED3D_RS_FOGTABLEMODE
]
1939 == WINED3D_FOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
1940 args
->clip_enabled
= state
->render_states
[WINED3D_RS_CLIPPING
]
1941 && state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
];
1942 args
->swizzle_map
= swizzle_map
;
1945 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
1947 if (usage_idx1
!= usage_idx2
)
1949 if (usage1
== usage2
)
1951 if (usage1
== WINED3D_DECL_USAGE_POSITION
&& usage2
== WINED3D_DECL_USAGE_POSITIONT
)
1953 if (usage2
== WINED3D_DECL_USAGE_POSITION
&& usage1
== WINED3D_DECL_USAGE_POSITIONT
)
1959 BOOL
vshader_get_input(const struct wined3d_shader
*shader
,
1960 BYTE usage_req
, BYTE usage_idx_req
, unsigned int *regnum
)
1962 WORD map
= shader
->reg_maps
.input_registers
;
1965 for (i
= 0; map
; map
>>= 1, ++i
)
1967 if (!(map
& 1)) continue;
1969 if (match_usage(shader
->u
.vs
.attributes
[i
].usage
,
1970 shader
->u
.vs
.attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
1979 static HRESULT
vertexshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
1980 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
1981 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
1983 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1987 const DWORD vs_uniform_count
= device
->adapter
->d3d_info
.limits
.vs_uniform_count
;
1989 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
1991 shader_init(shader
, device
, parent
, parent_ops
);
1992 hr
= shader_set_function(shader
, byte_code
, output_signature
, vs_uniform_count
,
1993 WINED3D_SHADER_TYPE_VERTEX
, max_version
);
1996 WARN("Failed to set function, hr %#x.\n", hr
);
1997 shader_cleanup(shader
);
2001 map
= reg_maps
->input_registers
;
2002 for (i
= 0; map
; map
>>= 1, ++i
)
2004 if (!(map
& 1) || !shader
->input_signature
[i
].semantic_name
)
2007 shader
->u
.vs
.attributes
[i
].usage
=
2008 shader_usage_from_semantic_name(shader
->input_signature
[i
].semantic_name
);
2009 shader
->u
.vs
.attributes
[i
].usage_idx
= shader
->input_signature
[i
].semantic_idx
;
2012 if (output_signature
)
2014 for (i
= 0; i
< output_signature
->element_count
; ++i
)
2016 struct wined3d_shader_signature_element
*e
= &output_signature
->elements
[i
];
2017 reg_maps
->output_registers
|= 1 << e
->register_idx
;
2018 shader
->output_signature
[e
->register_idx
] = *e
;
2022 shader
->load_local_constsF
= (reg_maps
->usesrelconstF
&& !list_empty(&shader
->constantsF
)) ||
2023 shader
->lconst_inf_or_nan
;
2028 static HRESULT
geometryshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2029 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
2030 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
2034 shader_init(shader
, device
, parent
, parent_ops
);
2035 hr
= shader_set_function(shader
, byte_code
, output_signature
, 0,
2036 WINED3D_SHADER_TYPE_GEOMETRY
, max_version
);
2039 WARN("Failed to set function, hr %#x.\n", hr
);
2040 shader_cleanup(shader
);
2044 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2049 void find_ps_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2050 BOOL position_transformed
, struct ps_compile_args
*args
, const struct wined3d_gl_info
*gl_info
)
2052 const struct wined3d_texture
*texture
;
2055 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
2056 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && state
->render_states
[WINED3D_RS_SRGBWRITEENABLE
])
2058 const struct wined3d_format
*rt_format
= state
->fb
->render_targets
[0]->format
;
2059 if (rt_format
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
2061 static unsigned int warned
= 0;
2063 args
->srgb_correction
= 1;
2064 if (state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] && !warned
++)
2065 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
2066 "support, expect rendering artifacts.\n");
2070 if (shader
->reg_maps
.shader_version
.major
== 1
2071 && shader
->reg_maps
.shader_version
.minor
<= 3)
2073 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2075 DWORD flags
= state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
];
2077 if (flags
& WINED3D_TTFF_PROJECTED
)
2079 DWORD tex_transform
= flags
& ~WINED3D_TTFF_PROJECTED
;
2081 if (!state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
2084 unsigned int index
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
2085 DWORD max_valid
= WINED3D_TTFF_COUNT4
;
2086 enum wined3d_sampler_texture_type sampler_type
= shader
->reg_maps
.sampler_type
[i
];
2088 for (j
= 0; j
< state
->vertex_declaration
->element_count
; ++j
)
2090 struct wined3d_vertex_declaration_element
*element
=
2091 &state
->vertex_declaration
->elements
[j
];
2093 if (element
->usage
== WINED3D_DECL_USAGE_TEXCOORD
2094 && element
->usage_idx
== index
)
2096 max_valid
= element
->format
->component_count
;
2100 if (!tex_transform
|| tex_transform
> max_valid
)
2102 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2103 tex_transform
, max_valid
);
2104 tex_transform
= max_valid
;
2106 if ((sampler_type
== WINED3DSTT_1D
&& tex_transform
> WINED3D_TTFF_COUNT1
)
2107 || (sampler_type
== WINED3DSTT_2D
&& tex_transform
> WINED3D_TTFF_COUNT2
)
2108 || (sampler_type
== WINED3DSTT_VOLUME
&& tex_transform
> WINED3D_TTFF_COUNT3
))
2109 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
2112 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2113 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2114 i
, tex_transform
, sampler_type
);
2118 tex_transform
= WINED3D_TTFF_COUNT4
| WINED3D_PSARGS_PROJECTED
;
2120 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
2124 if (shader
->reg_maps
.shader_version
.major
== 1
2125 && shader
->reg_maps
.shader_version
.minor
<= 4)
2127 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2129 const struct wined3d_texture
*texture
= state
->textures
[i
];
2131 if (!shader
->reg_maps
.sampler_type
[i
])
2134 /* Treat unbound textures as 2D. The dummy texture will provide
2135 * the proper sample value. The tex_types bitmap defaults to
2136 * 2D because of the memset. */
2140 switch (texture
->target
)
2142 /* RECT textures are distinguished from 2D textures via np2_fixup */
2143 case GL_TEXTURE_RECTANGLE_ARB
:
2148 args
->tex_types
|= WINED3D_SHADER_TEX_3D
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
2151 case GL_TEXTURE_CUBE_MAP_ARB
:
2152 args
->tex_types
|= WINED3D_SHADER_TEX_CUBE
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
2158 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2160 if (!shader
->reg_maps
.sampler_type
[i
])
2163 texture
= state
->textures
[i
];
2166 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
2169 args
->color_fixup
[i
] = texture
->resource
.format
->color_fixup
;
2171 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
2172 args
->shadow
|= 1 << i
;
2174 /* Flag samplers that need NP2 texcoord fixup. */
2175 if (!(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
2176 args
->np2_fixup
|= (1 << i
);
2178 if (shader
->reg_maps
.shader_version
.major
>= 3)
2180 if (position_transformed
)
2181 args
->vp_mode
= pretransformed
;
2182 else if (use_vs(state
))
2183 args
->vp_mode
= vertexshader
;
2185 args
->vp_mode
= fixedfunction
;
2186 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
2190 args
->vp_mode
= vertexshader
;
2191 if (state
->render_states
[WINED3D_RS_FOGENABLE
])
2193 switch (state
->render_states
[WINED3D_RS_FOGTABLEMODE
])
2195 case WINED3D_FOG_NONE
:
2196 if (position_transformed
|| use_vs(state
))
2198 args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
;
2202 switch (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
])
2204 case WINED3D_FOG_NONE
: /* Fall through. */
2205 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
2206 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
2207 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
2211 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
2212 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
2213 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
2218 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
2223 static HRESULT
pixelshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2224 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
2225 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
2227 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2228 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
2230 const DWORD ps_uniform_count
= device
->adapter
->d3d_info
.limits
.ps_uniform_count
;
2232 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
2234 shader_init(shader
, device
, parent
, parent_ops
);
2235 hr
= shader_set_function(shader
, byte_code
, output_signature
, ps_uniform_count
,
2236 WINED3D_SHADER_TYPE_PIXEL
, max_version
);
2239 WARN("Failed to set function, hr %#x.\n", hr
);
2240 shader_cleanup(shader
);
2244 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2246 if (shader
->u
.ps
.input_reg_used
[i
])
2249 highest_reg_used
= i
;
2253 /* Don't do any register mapping magic if it is not needed, or if we can't
2254 * achieve anything anyway */
2255 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
2256 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2258 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2260 /* This happens with relative addressing. The input mapper function
2261 * warns about this if the higher registers are declared too, so
2262 * don't write a FIXME here */
2263 WARN("More varying registers used than supported\n");
2266 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2268 shader
->u
.ps
.input_reg_map
[i
] = i
;
2271 shader
->u
.ps
.declared_in_count
= highest_reg_used
+ 1;
2275 shader
->u
.ps
.declared_in_count
= 0;
2276 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2278 if (shader
->u
.ps
.input_reg_used
[i
])
2279 shader
->u
.ps
.input_reg_map
[i
] = shader
->u
.ps
.declared_in_count
++;
2280 else shader
->u
.ps
.input_reg_map
[i
] = ~0U;
2284 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2289 void pixelshader_update_samplers(struct wined3d_shader
*shader
, WORD tex_types
)
2291 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2292 enum wined3d_sampler_texture_type
*sampler_type
= reg_maps
->sampler_type
;
2295 if (reg_maps
->shader_version
.major
!= 1) return;
2297 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2299 /* We don't sample from this sampler. */
2300 if (!sampler_type
[i
]) continue;
2302 switch ((tex_types
>> i
* WINED3D_PSARGS_TEXTYPE_SHIFT
) & WINED3D_PSARGS_TEXTYPE_MASK
)
2304 case WINED3D_SHADER_TEX_2D
:
2305 sampler_type
[i
] = WINED3DSTT_2D
;
2308 case WINED3D_SHADER_TEX_3D
:
2309 sampler_type
[i
] = WINED3DSTT_VOLUME
;
2312 case WINED3D_SHADER_TEX_CUBE
:
2313 sampler_type
[i
] = WINED3DSTT_CUBE
;
2319 HRESULT CDECL
wined3d_shader_create_gs(struct wined3d_device
*device
, const DWORD
*byte_code
,
2320 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2321 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2323 struct wined3d_shader
*object
;
2326 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2327 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2329 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2331 return E_OUTOFMEMORY
;
2333 hr
= geometryshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2336 WARN("Failed to initialize geometry shader, hr %#x.\n", hr
);
2337 HeapFree(GetProcessHeap(), 0, object
);
2341 TRACE("Created geometry shader %p.\n", object
);
2347 HRESULT CDECL
wined3d_shader_create_ps(struct wined3d_device
*device
, const DWORD
*byte_code
,
2348 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2349 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2351 struct wined3d_shader
*object
;
2354 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2355 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2357 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2359 return E_OUTOFMEMORY
;
2361 hr
= pixelshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2364 WARN("Failed to initialize pixel shader, hr %#x.\n", hr
);
2365 HeapFree(GetProcessHeap(), 0, object
);
2369 TRACE("Created pixel shader %p.\n", object
);
2375 HRESULT CDECL
wined3d_shader_create_vs(struct wined3d_device
*device
, const DWORD
*byte_code
,
2376 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2377 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2379 struct wined3d_shader
*object
;
2382 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2383 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2385 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2387 return E_OUTOFMEMORY
;
2389 hr
= vertexshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2392 WARN("Failed to initialize vertex shader, hr %#x.\n", hr
);
2393 HeapFree(GetProcessHeap(), 0, object
);
2397 TRACE("Created vertex shader %p.\n", object
);