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 inline void set_bitmap_bit(DWORD
*bitmap
, DWORD bit
)
378 bitmap
[idx
] |= (1 << shift
);
381 static void shader_record_register_usage(struct wined3d_shader
*shader
, struct wined3d_shader_reg_maps
*reg_maps
,
382 const struct wined3d_shader_register
*reg
, enum wined3d_shader_type shader_type
)
386 case WINED3DSPR_TEXTURE
: /* WINED3DSPR_ADDR */
387 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
388 reg_maps
->texcoord
|= 1 << reg
->idx
[0].offset
;
390 reg_maps
->address
|= 1 << reg
->idx
[0].offset
;
393 case WINED3DSPR_TEMP
:
394 reg_maps
->temporary
|= 1 << reg
->idx
[0].offset
;
397 case WINED3DSPR_INPUT
:
398 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
400 if (reg
->idx
[0].rel_addr
)
402 /* If relative addressing is used, we must assume that all registers
403 * are used. Even if it is a construct like v3[aL], we can't assume
404 * that v0, v1 and v2 aren't read because aL can be negative */
406 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
408 shader
->u
.ps
.input_reg_used
[i
] = TRUE
;
413 shader
->u
.ps
.input_reg_used
[reg
->idx
[0].offset
] = TRUE
;
417 reg_maps
->input_registers
|= 1 << reg
->idx
[0].offset
;
420 case WINED3DSPR_RASTOUT
:
421 if (reg
->idx
[0].offset
== 1)
425 case WINED3DSPR_MISCTYPE
:
426 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
428 if (!reg
->idx
[0].offset
)
430 else if (reg
->idx
[0].offset
== 1)
431 reg_maps
->usesfacing
= 1;
435 case WINED3DSPR_CONST
:
436 if (reg
->idx
[0].rel_addr
)
438 if (reg
->idx
[0].offset
< reg_maps
->min_rel_offset
)
439 reg_maps
->min_rel_offset
= reg
->idx
[0].offset
;
440 if (reg
->idx
[0].offset
> reg_maps
->max_rel_offset
)
441 reg_maps
->max_rel_offset
= reg
->idx
[0].offset
;
442 reg_maps
->usesrelconstF
= TRUE
;
446 set_bitmap_bit(reg_maps
->constf
, reg
->idx
[0].offset
);
450 case WINED3DSPR_CONSTINT
:
451 reg_maps
->integer_constants
|= (1 << reg
->idx
[0].offset
);
454 case WINED3DSPR_CONSTBOOL
:
455 reg_maps
->boolean_constants
|= (1 << reg
->idx
[0].offset
);
458 case WINED3DSPR_COLOROUT
:
459 reg_maps
->rt_mask
|= (1 << reg
->idx
[0].offset
);
463 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
464 reg
->type
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
469 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr
, unsigned int param
)
473 case WINED3DSIH_M4x4
:
474 case WINED3DSIH_M3x4
:
475 return param
== 1 ? 3 : 0;
477 case WINED3DSIH_M4x3
:
478 case WINED3DSIH_M3x3
:
479 return param
== 1 ? 2 : 0;
481 case WINED3DSIH_M3x2
:
482 return param
== 1 ? 1 : 0;
489 /* Note that this does not count the loop register as an address register. */
490 static HRESULT
shader_get_registers_used(struct wined3d_shader
*shader
, const struct wined3d_shader_frontend
*fe
,
491 struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_shader_signature_element
*input_signature
,
492 struct wined3d_shader_signature_element
*output_signature
, const DWORD
*byte_code
, DWORD constf_size
)
494 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
495 void *fe_data
= shader
->frontend_data
;
496 struct wined3d_shader_version shader_version
;
497 const DWORD
*ptr
= byte_code
;
499 memset(reg_maps
, 0, sizeof(*reg_maps
));
500 reg_maps
->min_rel_offset
= ~0U;
502 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
503 reg_maps
->shader_version
= shader_version
;
505 reg_maps
->constf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
506 sizeof(*reg_maps
->constf
) * ((constf_size
+ 31) / 32));
507 if (!reg_maps
->constf
)
509 ERR("Failed to allocate constant map memory.\n");
510 return E_OUTOFMEMORY
;
513 while (!fe
->shader_is_end(fe_data
, &ptr
))
515 struct wined3d_shader_instruction ins
;
518 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
520 /* Unhandled opcode, and its parameters. */
521 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
523 TRACE("Skipping unrecognized instruction.\n");
527 /* Handle declarations. */
528 if (ins
.handler_idx
== WINED3DSIH_DCL
)
530 struct wined3d_shader_semantic
*semantic
= &ins
.declaration
.semantic
;
532 switch (semantic
->reg
.reg
.type
)
534 /* Mark input registers used. */
535 case WINED3DSPR_INPUT
:
536 reg_maps
->input_registers
|= 1 << semantic
->reg
.reg
.idx
[0].offset
;
537 shader_signature_from_semantic(&input_signature
[semantic
->reg
.reg
.idx
[0].offset
], semantic
);
540 /* Vertex shader: mark 3.0 output registers used, save token. */
541 case WINED3DSPR_OUTPUT
:
542 reg_maps
->output_registers
|= 1 << semantic
->reg
.reg
.idx
[0].offset
;
543 shader_signature_from_semantic(&output_signature
[semantic
->reg
.reg
.idx
[0].offset
], semantic
);
544 if (semantic
->usage
== WINED3D_DECL_USAGE_FOG
)
548 /* Save sampler usage token. */
549 case WINED3DSPR_SAMPLER
:
550 reg_maps
->sampler_type
[semantic
->reg
.reg
.idx
[0].offset
] = semantic
->sampler_type
;
554 TRACE("Not recording DCL register type %#x.\n", semantic
->reg
.reg
.type
);
558 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
560 struct wined3d_shader_register
*reg
= &ins
.declaration
.src
.reg
;
561 if (reg
->idx
[0].offset
>= WINED3D_MAX_CBS
)
562 ERR("Invalid CB index %u.\n", reg
->idx
[0].offset
);
564 reg_maps
->cb_sizes
[reg
->idx
[0].offset
] = reg
->idx
[1].offset
;
566 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
)
568 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
569 shader
->u
.gs
.input_type
= ins
.declaration
.primitive_type
;
571 FIXME("Invalid instruction %#x for shader type %#x.\n",
572 ins
.handler_idx
, shader_version
.type
);
574 else if (ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
576 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
577 shader
->u
.gs
.output_type
= ins
.declaration
.primitive_type
;
579 FIXME("Invalid instruction %#x for shader type %#x.\n",
580 ins
.handler_idx
, shader_version
.type
);
582 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
584 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
585 shader
->u
.gs
.vertices_out
= ins
.declaration
.count
;
587 FIXME("Invalid instruction %#x for shader type %#x.\n",
588 ins
.handler_idx
, shader_version
.type
);
590 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
592 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
594 if (!lconst
) return E_OUTOFMEMORY
;
596 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
597 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
598 value
= (float *)lconst
->value
;
600 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
601 if (shader_version
.major
== 1 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
603 if (value
[0] < -1.0f
) value
[0] = -1.0f
;
604 else if (value
[0] > 1.0f
) value
[0] = 1.0f
;
605 if (value
[1] < -1.0f
) value
[1] = -1.0f
;
606 else if (value
[1] > 1.0f
) value
[1] = 1.0f
;
607 if (value
[2] < -1.0f
) value
[2] = -1.0f
;
608 else if (value
[2] > 1.0f
) value
[2] = 1.0f
;
609 if (value
[3] < -1.0f
) value
[3] = -1.0f
;
610 else if (value
[3] > 1.0f
) value
[3] = 1.0f
;
613 list_add_head(&shader
->constantsF
, &lconst
->entry
);
615 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
616 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
618 shader
->lconst_inf_or_nan
= TRUE
;
621 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
623 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
624 if (!lconst
) return E_OUTOFMEMORY
;
626 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
627 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
629 list_add_head(&shader
->constantsI
, &lconst
->entry
);
630 reg_maps
->local_int_consts
|= (1 << lconst
->idx
);
632 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
634 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
635 if (!lconst
) return E_OUTOFMEMORY
;
637 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
638 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, sizeof(DWORD
));
640 list_add_head(&shader
->constantsB
, &lconst
->entry
);
641 reg_maps
->local_bool_consts
|= (1 << lconst
->idx
);
643 /* For subroutine prototypes. */
644 else if (ins
.handler_idx
== WINED3DSIH_LABEL
)
646 reg_maps
->labels
|= 1 << ins
.src
[0].reg
.idx
[0].offset
;
648 /* Set texture, address, temporary registers. */
651 BOOL color0_mov
= FALSE
;
654 /* This will loop over all the registers and try to
655 * make a bitmask of the ones we're interested in.
657 * Relative addressing tokens are ignored, but that's
658 * okay, since we'll catch any address registers when
659 * they are initialized (required by spec). */
660 for (i
= 0; i
< ins
.dst_count
; ++i
)
662 shader_record_register_usage(shader
, reg_maps
, &ins
.dst
[i
].reg
, shader_version
.type
);
664 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
665 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
666 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
667 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
&& shader_version
.major
< 3)
669 UINT idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
671 switch (ins
.dst
[i
].reg
.type
)
673 case WINED3DSPR_RASTOUT
:
677 reg_maps
->output_registers
|= 1 << 10;
678 shader_signature_from_usage(&output_signature
[10],
679 WINED3D_DECL_USAGE_POSITION
, 0, 10, WINED3DSP_WRITEMASK_ALL
);
683 reg_maps
->output_registers
|= 1 << 11;
684 shader_signature_from_usage(&output_signature
[11],
685 WINED3D_DECL_USAGE_FOG
, 0, 11, WINED3DSP_WRITEMASK_0
);
689 reg_maps
->output_registers
|= 1 << 11;
690 shader_signature_from_usage(&output_signature
[11],
691 WINED3D_DECL_USAGE_PSIZE
, 0, 11, WINED3DSP_WRITEMASK_1
);
696 case WINED3DSPR_ATTROUT
:
700 if (reg_maps
->output_registers
& (1 << idx
))
702 output_signature
[idx
].mask
|= ins
.dst
[i
].write_mask
;
706 reg_maps
->output_registers
|= 1 << idx
;
707 shader_signature_from_usage(&output_signature
[idx
],
708 WINED3D_DECL_USAGE_COLOR
, idx
- 8, idx
, ins
.dst
[i
].write_mask
);
713 case WINED3DSPR_TEXCRDOUT
:
715 reg_maps
->texcoord_mask
[idx
] |= ins
.dst
[i
].write_mask
;
716 if (reg_maps
->output_registers
& (1 << idx
))
718 output_signature
[idx
].mask
|= ins
.dst
[i
].write_mask
;
722 reg_maps
->output_registers
|= 1 << idx
;
723 shader_signature_from_usage(&output_signature
[idx
],
724 WINED3D_DECL_USAGE_TEXCOORD
, idx
, idx
, ins
.dst
[i
].write_mask
);
733 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
735 if (ins
.dst
[i
].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
.dst
[i
].reg
.idx
[0].offset
)
737 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
738 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
739 * the mov and perform the sRGB write correction from the source register.
741 * However, if the mov is only partial, we can't do this, and if the write
742 * comes from an instruction other than MOV it is hard to do as well. If
743 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
744 shader
->u
.ps
.color0_mov
= FALSE
;
745 if (ins
.handler_idx
== WINED3DSIH_MOV
746 && ins
.dst
[i
].write_mask
== WINED3DSP_WRITEMASK_ALL
)
748 /* Used later when the source register is read. */
752 /* Also drop the MOV marker if the source register is overwritten prior to the shader
755 else if (ins
.dst
[i
].reg
.type
== WINED3DSPR_TEMP
756 && ins
.dst
[i
].reg
.idx
[0].offset
== shader
->u
.ps
.color0_reg
)
758 shader
->u
.ps
.color0_mov
= FALSE
;
762 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
763 if (shader_version
.major
== 1
764 && (ins
.handler_idx
== WINED3DSIH_TEX
765 || ins
.handler_idx
== WINED3DSIH_TEXBEM
766 || ins
.handler_idx
== WINED3DSIH_TEXBEML
767 || ins
.handler_idx
== WINED3DSIH_TEXDP3TEX
768 || ins
.handler_idx
== WINED3DSIH_TEXM3x2TEX
769 || ins
.handler_idx
== WINED3DSIH_TEXM3x3SPEC
770 || ins
.handler_idx
== WINED3DSIH_TEXM3x3TEX
771 || ins
.handler_idx
== WINED3DSIH_TEXM3x3VSPEC
772 || ins
.handler_idx
== WINED3DSIH_TEXREG2AR
773 || ins
.handler_idx
== WINED3DSIH_TEXREG2GB
774 || ins
.handler_idx
== WINED3DSIH_TEXREG2RGB
))
776 /* Fake sampler usage, only set reserved bit and type. */
777 DWORD sampler_code
= ins
.dst
[i
].reg
.idx
[0].offset
;
779 TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
780 reg_maps
->sampler_type
[sampler_code
] = WINED3DSTT_2D
;
782 /* texbem is only valid with < 1.4 pixel shaders */
783 if (ins
.handler_idx
== WINED3DSIH_TEXBEM
784 || ins
.handler_idx
== WINED3DSIH_TEXBEML
)
786 reg_maps
->bumpmat
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
787 if (ins
.handler_idx
== WINED3DSIH_TEXBEML
)
789 reg_maps
->luminanceparams
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
793 else if (ins
.handler_idx
== WINED3DSIH_BEM
)
795 reg_maps
->bumpmat
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
799 if (ins
.handler_idx
== WINED3DSIH_NRM
) reg_maps
->usesnrm
= 1;
800 else if (ins
.handler_idx
== WINED3DSIH_DSY
) reg_maps
->usesdsy
= 1;
801 else if (ins
.handler_idx
== WINED3DSIH_DSX
) reg_maps
->usesdsx
= 1;
802 else if (ins
.handler_idx
== WINED3DSIH_TEXLDD
) reg_maps
->usestexldd
= 1;
803 else if (ins
.handler_idx
== WINED3DSIH_TEXLDL
) reg_maps
->usestexldl
= 1;
804 else if (ins
.handler_idx
== WINED3DSIH_MOVA
) reg_maps
->usesmova
= 1;
805 else if (ins
.handler_idx
== WINED3DSIH_IFC
) reg_maps
->usesifc
= 1;
806 else if (ins
.handler_idx
== WINED3DSIH_CALL
) reg_maps
->usescall
= 1;
807 else if (ins
.handler_idx
== WINED3DSIH_POW
) reg_maps
->usespow
= 1;
808 else if (ins
.handler_idx
== WINED3DSIH_LOOP
809 || ins
.handler_idx
== WINED3DSIH_REP
)
812 if (cur_loop_depth
> max_loop_depth
)
813 max_loop_depth
= cur_loop_depth
;
815 else if (ins
.handler_idx
== WINED3DSIH_ENDLOOP
816 || ins
.handler_idx
== WINED3DSIH_ENDREP
)
820 shader_record_register_usage(shader
, reg_maps
, &ins
.predicate
->reg
, shader_version
.type
);
822 for (i
= 0; i
< ins
.src_count
; ++i
)
824 unsigned int count
= get_instr_extra_regcount(ins
.handler_idx
, i
);
825 struct wined3d_shader_register reg
= ins
.src
[i
].reg
;
827 shader_record_register_usage(shader
, reg_maps
, &ins
.src
[i
].reg
, shader_version
.type
);
831 shader_record_register_usage(shader
, reg_maps
, ®
, shader_version
.type
);
837 if (ins
.src
[i
].reg
.type
== WINED3DSPR_TEMP
838 && ins
.src
[i
].swizzle
== WINED3DSP_NOSWIZZLE
)
840 shader
->u
.ps
.color0_mov
= TRUE
;
841 shader
->u
.ps
.color0_reg
= ins
.src
[i
].reg
.idx
[0].offset
;
847 reg_maps
->loop_depth
= max_loop_depth
;
849 /* PS before 2.0 don't have explicit color outputs. Instead the value of
850 * R0 is written to the render target. */
851 if (shader_version
.major
< 2 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
852 reg_maps
->rt_mask
|= (1 << 0);
854 shader
->functionLength
= ((const char *)ptr
- (const char *)byte_code
);
859 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int max
)
861 DWORD map
= 1 << max
;
863 map
&= reg_maps
->shader_version
.major
< 3 ? ~reg_maps
->texcoord
: ~reg_maps
->input_registers
;
865 return wined3d_log2i(map
);
868 static void shader_dump_decl_usage(const struct wined3d_shader_semantic
*semantic
,
869 const struct wined3d_shader_version
*shader_version
)
873 if (semantic
->reg
.reg
.type
== WINED3DSPR_SAMPLER
)
875 switch (semantic
->sampler_type
)
877 case WINED3DSTT_2D
: TRACE("_2d"); break;
878 case WINED3DSTT_CUBE
: TRACE("_cube"); break;
879 case WINED3DSTT_VOLUME
: TRACE("_volume"); break;
880 default: TRACE("_unknown_ttype(0x%08x)", semantic
->sampler_type
);
885 /* Pixel shaders 3.0 don't have usage semantics. */
886 if (shader_version
->major
< 3 && shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
) return;
889 switch (semantic
->usage
)
891 case WINED3D_DECL_USAGE_POSITION
:
892 TRACE("position%u", semantic
->usage_idx
);
895 case WINED3D_DECL_USAGE_BLEND_INDICES
:
899 case WINED3D_DECL_USAGE_BLEND_WEIGHT
:
903 case WINED3D_DECL_USAGE_NORMAL
:
904 TRACE("normal%u", semantic
->usage_idx
);
907 case WINED3D_DECL_USAGE_PSIZE
:
911 case WINED3D_DECL_USAGE_COLOR
:
912 if (!semantic
->usage_idx
) TRACE("color");
913 else TRACE("specular%u", (semantic
->usage_idx
- 1));
916 case WINED3D_DECL_USAGE_TEXCOORD
:
917 TRACE("texture%u", semantic
->usage_idx
);
920 case WINED3D_DECL_USAGE_TANGENT
:
924 case WINED3D_DECL_USAGE_BINORMAL
:
928 case WINED3D_DECL_USAGE_TESS_FACTOR
:
932 case WINED3D_DECL_USAGE_POSITIONT
:
933 TRACE("positionT%u", semantic
->usage_idx
);
936 case WINED3D_DECL_USAGE_FOG
:
940 case WINED3D_DECL_USAGE_DEPTH
:
944 case WINED3D_DECL_USAGE_SAMPLE
:
949 FIXME("unknown_semantics(0x%08x)", semantic
->usage
);
954 static void shader_dump_register(const struct wined3d_shader_register
*reg
,
955 const struct wined3d_shader_version
*shader_version
)
957 static const char * const rastout_reg_names
[] = {"oPos", "oFog", "oPts"};
958 static const char * const misctype_reg_names
[] = {"vPos", "vFace"};
959 UINT offset
= reg
->idx
[0].offset
;
963 case WINED3DSPR_TEMP
:
967 case WINED3DSPR_INPUT
:
971 case WINED3DSPR_CONST
:
972 case WINED3DSPR_CONST2
:
973 case WINED3DSPR_CONST3
:
974 case WINED3DSPR_CONST4
:
976 offset
= shader_get_float_offset(reg
->type
, offset
);
979 case WINED3DSPR_TEXTURE
: /* vs: case WINED3DSPR_ADDR */
980 TRACE("%c", shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
? 't' : 'a');
983 case WINED3DSPR_RASTOUT
:
984 TRACE("%s", rastout_reg_names
[offset
]);
987 case WINED3DSPR_COLOROUT
:
991 case WINED3DSPR_DEPTHOUT
:
995 case WINED3DSPR_ATTROUT
:
999 case WINED3DSPR_TEXCRDOUT
:
1000 /* Vertex shaders >= 3.0 use general purpose output registers
1001 * (WINED3DSPR_OUTPUT), which can include an address token. */
1002 if (shader_version
->major
>= 3) TRACE("o");
1006 case WINED3DSPR_CONSTINT
:
1010 case WINED3DSPR_CONSTBOOL
:
1014 case WINED3DSPR_LABEL
:
1018 case WINED3DSPR_LOOP
:
1022 case WINED3DSPR_SAMPLER
:
1026 case WINED3DSPR_MISCTYPE
:
1028 FIXME("Unhandled misctype register %u.\n", offset
);
1030 TRACE("%s", misctype_reg_names
[offset
]);
1033 case WINED3DSPR_PREDICATE
:
1037 case WINED3DSPR_IMMCONST
:
1041 case WINED3DSPR_CONSTBUFFER
:
1045 case WINED3DSPR_PRIMID
:
1049 case WINED3DSPR_NULL
:
1053 case WINED3DSPR_RESOURCE
:
1058 TRACE("unhandled_rtype(%#x)", reg
->type
);
1062 if (reg
->type
== WINED3DSPR_IMMCONST
)
1065 switch (reg
->immconst_type
)
1067 case WINED3D_IMMCONST_SCALAR
:
1068 switch (reg
->data_type
)
1070 case WINED3D_DATA_FLOAT
:
1071 TRACE("%.8e", *(const float *)reg
->immconst_data
);
1073 case WINED3D_DATA_INT
:
1074 TRACE("%d", reg
->immconst_data
[0]);
1076 case WINED3D_DATA_RESOURCE
:
1077 case WINED3D_DATA_SAMPLER
:
1078 case WINED3D_DATA_UINT
:
1079 TRACE("%u", reg
->immconst_data
[0]);
1082 TRACE("<unhandled data type %#x>", reg
->data_type
);
1087 case WINED3D_IMMCONST_VEC4
:
1088 switch (reg
->data_type
)
1090 case WINED3D_DATA_FLOAT
:
1091 TRACE("%.8e, %.8e, %.8e, %.8e",
1092 *(const float *)®
->immconst_data
[0], *(const float *)®
->immconst_data
[1],
1093 *(const float *)®
->immconst_data
[2], *(const float *)®
->immconst_data
[3]);
1095 case WINED3D_DATA_INT
:
1096 TRACE("%d, %d, %d, %d",
1097 reg
->immconst_data
[0], reg
->immconst_data
[1],
1098 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1100 case WINED3D_DATA_RESOURCE
:
1101 case WINED3D_DATA_SAMPLER
:
1102 case WINED3D_DATA_UINT
:
1103 TRACE("%u, %u, %u, %u",
1104 reg
->immconst_data
[0], reg
->immconst_data
[1],
1105 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1108 TRACE("<unhandled data type %#x>", reg
->data_type
);
1114 TRACE("<unhandled immconst_type %#x>", reg
->immconst_type
);
1119 else if (reg
->type
!= WINED3DSPR_RASTOUT
1120 && reg
->type
!= WINED3DSPR_MISCTYPE
1121 && reg
->type
!= WINED3DSPR_NULL
)
1126 if (reg
->idx
[0].rel_addr
)
1128 shader_dump_src_param(reg
->idx
[0].rel_addr
, shader_version
);
1131 TRACE("%u]", offset
);
1133 if (reg
->idx
[1].offset
!= ~0U)
1136 if (reg
->idx
[1].rel_addr
)
1138 shader_dump_src_param(reg
->idx
[1].rel_addr
, shader_version
);
1141 TRACE("%u]", reg
->idx
[1].offset
);
1147 void shader_dump_dst_param(const struct wined3d_shader_dst_param
*param
,
1148 const struct wined3d_shader_version
*shader_version
)
1150 DWORD write_mask
= param
->write_mask
;
1152 shader_dump_register(¶m
->reg
, shader_version
);
1154 if (write_mask
&& write_mask
!= WINED3DSP_WRITEMASK_ALL
)
1156 static const char write_mask_chars
[] = "xyzw";
1159 if (write_mask
& WINED3DSP_WRITEMASK_0
) TRACE("%c", write_mask_chars
[0]);
1160 if (write_mask
& WINED3DSP_WRITEMASK_1
) TRACE("%c", write_mask_chars
[1]);
1161 if (write_mask
& WINED3DSP_WRITEMASK_2
) TRACE("%c", write_mask_chars
[2]);
1162 if (write_mask
& WINED3DSP_WRITEMASK_3
) TRACE("%c", write_mask_chars
[3]);
1166 void shader_dump_src_param(const struct wined3d_shader_src_param
*param
,
1167 const struct wined3d_shader_version
*shader_version
)
1169 enum wined3d_shader_src_modifier src_modifier
= param
->modifiers
;
1170 DWORD swizzle
= param
->swizzle
;
1172 if (src_modifier
== WINED3DSPSM_NEG
1173 || src_modifier
== WINED3DSPSM_BIASNEG
1174 || src_modifier
== WINED3DSPSM_SIGNNEG
1175 || src_modifier
== WINED3DSPSM_X2NEG
1176 || src_modifier
== WINED3DSPSM_ABSNEG
)
1178 else if (src_modifier
== WINED3DSPSM_COMP
)
1180 else if (src_modifier
== WINED3DSPSM_NOT
)
1183 if (src_modifier
== WINED3DSPSM_ABS
|| src_modifier
== WINED3DSPSM_ABSNEG
)
1186 shader_dump_register(¶m
->reg
, shader_version
);
1190 switch (src_modifier
)
1192 case WINED3DSPSM_NONE
: break;
1193 case WINED3DSPSM_NEG
: break;
1194 case WINED3DSPSM_NOT
: break;
1195 case WINED3DSPSM_BIAS
: TRACE("_bias"); break;
1196 case WINED3DSPSM_BIASNEG
: TRACE("_bias"); break;
1197 case WINED3DSPSM_SIGN
: TRACE("_bx2"); break;
1198 case WINED3DSPSM_SIGNNEG
: TRACE("_bx2"); break;
1199 case WINED3DSPSM_COMP
: break;
1200 case WINED3DSPSM_X2
: TRACE("_x2"); break;
1201 case WINED3DSPSM_X2NEG
: TRACE("_x2"); break;
1202 case WINED3DSPSM_DZ
: TRACE("_dz"); break;
1203 case WINED3DSPSM_DW
: TRACE("_dw"); break;
1204 case WINED3DSPSM_ABSNEG
: TRACE(")"); break;
1205 case WINED3DSPSM_ABS
: TRACE(")"); break;
1206 default: TRACE("_unknown_modifier(%#x)", src_modifier
);
1210 if (swizzle
!= WINED3DSP_NOSWIZZLE
)
1212 static const char swizzle_chars
[] = "xyzw";
1213 DWORD swizzle_x
= swizzle
& 0x03;
1214 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
1215 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
1216 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
1218 if (swizzle_x
== swizzle_y
1219 && swizzle_x
== swizzle_z
1220 && swizzle_x
== swizzle_w
)
1222 TRACE(".%c", swizzle_chars
[swizzle_x
]);
1226 TRACE(".%c%c%c%c", swizzle_chars
[swizzle_x
], swizzle_chars
[swizzle_y
],
1227 swizzle_chars
[swizzle_z
], swizzle_chars
[swizzle_w
]);
1232 /* Shared code in order to generate the bulk of the shader string.
1233 * NOTE: A description of how to parse tokens can be found on MSDN. */
1234 void shader_generate_main(const struct wined3d_shader
*shader
, struct wined3d_shader_buffer
*buffer
,
1235 const struct wined3d_shader_reg_maps
*reg_maps
, const DWORD
*byte_code
, void *backend_ctx
)
1237 struct wined3d_device
*device
= shader
->device
;
1238 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
1239 void *fe_data
= shader
->frontend_data
;
1240 struct wined3d_shader_version shader_version
;
1241 struct wined3d_shader_loop_state loop_state
;
1242 struct wined3d_shader_instruction ins
;
1243 struct wined3d_shader_tex_mx tex_mx
;
1244 struct wined3d_shader_context ctx
;
1245 const DWORD
*ptr
= byte_code
;
1247 /* Initialize current parsing state. */
1248 tex_mx
.current_row
= 0;
1249 loop_state
.current_depth
= 0;
1250 loop_state
.current_reg
= 0;
1252 ctx
.shader
= shader
;
1253 ctx
.gl_info
= &device
->adapter
->gl_info
;
1254 ctx
.reg_maps
= reg_maps
;
1255 ctx
.buffer
= buffer
;
1256 ctx
.tex_mx
= &tex_mx
;
1257 ctx
.loop_state
= &loop_state
;
1258 ctx
.backend_data
= backend_ctx
;
1261 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1263 while (!fe
->shader_is_end(fe_data
, &ptr
))
1266 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1268 /* Unknown opcode and its parameters. */
1269 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1271 TRACE("Skipping unrecognized instruction.\n");
1276 FIXME("Predicates not implemented.\n");
1278 /* Call appropriate function for output target */
1279 device
->shader_backend
->shader_handle_instruction(&ins
);
1283 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param
*dst
)
1285 DWORD mmask
= dst
->modifiers
;
1290 case 13: TRACE("_d8"); break;
1291 case 14: TRACE("_d4"); break;
1292 case 15: TRACE("_d2"); break;
1293 case 1: TRACE("_x2"); break;
1294 case 2: TRACE("_x4"); break;
1295 case 3: TRACE("_x8"); break;
1296 default: TRACE("_unhandled_shift(%d)", dst
->shift
); break;
1299 if (mmask
& WINED3DSPDM_SATURATE
) TRACE("_sat");
1300 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) TRACE("_pp");
1301 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) TRACE("_centroid");
1303 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
1304 if (mmask
) FIXME("_unrecognized_modifier(%#x)", mmask
);
1307 static void shader_dump_primitive_type(enum wined3d_primitive_type primitive_type
)
1309 switch (primitive_type
)
1311 case WINED3D_PT_UNDEFINED
:
1314 case WINED3D_PT_POINTLIST
:
1317 case WINED3D_PT_LINELIST
:
1320 case WINED3D_PT_LINESTRIP
:
1323 case WINED3D_PT_TRIANGLELIST
:
1324 TRACE("trianglelist");
1326 case WINED3D_PT_TRIANGLESTRIP
:
1327 TRACE("trianglestrip");
1329 case WINED3D_PT_TRIANGLEFAN
:
1330 TRACE("trianglefan");
1332 case WINED3D_PT_LINELIST_ADJ
:
1333 TRACE("linelist_adj");
1335 case WINED3D_PT_LINESTRIP_ADJ
:
1336 TRACE("linestrip_adj");
1338 case WINED3D_PT_TRIANGLELIST_ADJ
:
1339 TRACE("trianglelist_adj");
1341 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
1342 TRACE("trianglestrip_adj");
1345 TRACE("<unrecognized_primitive_type %#x>", primitive_type
);
1350 static void shader_trace_init(const struct wined3d_shader_frontend
*fe
, void *fe_data
, const DWORD
*byte_code
)
1352 struct wined3d_shader_version shader_version
;
1353 const DWORD
*ptr
= byte_code
;
1354 const char *type_prefix
;
1357 TRACE("Parsing %p.\n", byte_code
);
1359 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1361 switch (shader_version
.type
)
1363 case WINED3D_SHADER_TYPE_VERTEX
:
1367 case WINED3D_SHADER_TYPE_GEOMETRY
:
1371 case WINED3D_SHADER_TYPE_PIXEL
:
1376 FIXME("Unhandled shader type %#x.\n", shader_version
.type
);
1377 type_prefix
= "unknown";
1381 TRACE("%s_%u_%u\n", type_prefix
, shader_version
.major
, shader_version
.minor
);
1383 while (!fe
->shader_is_end(fe_data
, &ptr
))
1385 struct wined3d_shader_instruction ins
;
1387 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1388 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1390 TRACE("Skipping unrecognized instruction.\n");
1394 if (ins
.handler_idx
== WINED3DSIH_DCL
)
1396 shader_dump_decl_usage(&ins
.declaration
.semantic
, &shader_version
);
1397 shader_dump_ins_modifiers(&ins
.declaration
.semantic
.reg
);
1399 shader_dump_dst_param(&ins
.declaration
.semantic
.reg
, &shader_version
);
1401 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
1403 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1404 shader_dump_src_param(&ins
.declaration
.src
, &shader_version
);
1405 TRACE(", %s", ins
.flags
& WINED3DSI_INDEXED_DYNAMIC
? "dynamicIndexed" : "immediateIndexed");
1407 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
1408 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
1410 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1411 shader_dump_primitive_type(ins
.declaration
.primitive_type
);
1413 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
1415 TRACE("%s %u", shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.count
);
1417 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
1419 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(ins
.dst
[0].reg
.type
,
1420 ins
.dst
[0].reg
.idx
[0].offset
),
1421 *(const float *)&ins
.src
[0].reg
.immconst_data
[0],
1422 *(const float *)&ins
.src
[0].reg
.immconst_data
[1],
1423 *(const float *)&ins
.src
[0].reg
.immconst_data
[2],
1424 *(const float *)&ins
.src
[0].reg
.immconst_data
[3]);
1426 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
1428 TRACE("defi i%u = %d, %d, %d, %d", ins
.dst
[0].reg
.idx
[0].offset
,
1429 ins
.src
[0].reg
.immconst_data
[0],
1430 ins
.src
[0].reg
.immconst_data
[1],
1431 ins
.src
[0].reg
.immconst_data
[2],
1432 ins
.src
[0].reg
.immconst_data
[3]);
1434 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
1436 TRACE("defb b%u = %s", ins
.dst
[0].reg
.idx
[0].offset
, ins
.src
[0].reg
.immconst_data
[0] ? "true" : "false");
1443 shader_dump_src_param(ins
.predicate
, &shader_version
);
1447 /* PixWin marks instructions with the coissue flag with a '+' */
1448 if (ins
.coissue
) TRACE("+");
1450 TRACE("%s", shader_opcode_names
[ins
.handler_idx
]);
1452 if (ins
.handler_idx
== WINED3DSIH_IFC
1453 || ins
.handler_idx
== WINED3DSIH_BREAKC
)
1457 case WINED3D_SHADER_REL_OP_GT
: TRACE("_gt"); break;
1458 case WINED3D_SHADER_REL_OP_EQ
: TRACE("_eq"); break;
1459 case WINED3D_SHADER_REL_OP_GE
: TRACE("_ge"); break;
1460 case WINED3D_SHADER_REL_OP_LT
: TRACE("_lt"); break;
1461 case WINED3D_SHADER_REL_OP_NE
: TRACE("_ne"); break;
1462 case WINED3D_SHADER_REL_OP_LE
: TRACE("_le"); break;
1463 default: TRACE("_(%u)", ins
.flags
);
1466 else if (ins
.handler_idx
== WINED3DSIH_TEX
1467 && shader_version
.major
>= 2
1468 && (ins
.flags
& WINED3DSI_TEXLD_PROJECT
))
1473 for (i
= 0; i
< ins
.dst_count
; ++i
)
1475 shader_dump_ins_modifiers(&ins
.dst
[i
]);
1476 TRACE(!i
? " " : ", ");
1477 shader_dump_dst_param(&ins
.dst
[i
], &shader_version
);
1480 /* Other source tokens */
1481 for (i
= ins
.dst_count
; i
< (ins
.dst_count
+ ins
.src_count
); ++i
)
1483 TRACE(!i
? " " : ", ");
1484 shader_dump_src_param(&ins
.src
[i
- ins
.dst_count
], &shader_version
);
1491 static void shader_cleanup(struct wined3d_shader
*shader
)
1493 shader
->device
->shader_backend
->shader_destroy(shader
);
1494 HeapFree(GetProcessHeap(), 0, shader
->reg_maps
.constf
);
1495 HeapFree(GetProcessHeap(), 0, shader
->function
);
1496 shader_delete_constant_list(&shader
->constantsF
);
1497 shader_delete_constant_list(&shader
->constantsB
);
1498 shader_delete_constant_list(&shader
->constantsI
);
1499 list_remove(&shader
->shader_list_entry
);
1501 if (shader
->frontend
&& shader
->frontend_data
)
1502 shader
->frontend
->shader_free(shader
->frontend_data
);
1505 struct shader_none_priv
1507 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
1508 const struct fragment_pipeline
*fragment_pipe
;
1509 BOOL ffp_proj_control
;
1512 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
1513 static void shader_none_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
1514 enum tex_types tex_type
, const SIZE
*ds_mask_size
) {}
1515 static void shader_none_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
) {}
1516 static void shader_none_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1517 static void shader_none_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1518 static void shader_none_load_constants(void *shader_priv
, struct wined3d_context
*context
,
1519 const struct wined3d_state
*state
) {}
1520 static void shader_none_destroy(struct wined3d_shader
*shader
) {}
1521 static void shader_none_free_context_data(struct wined3d_context
*context
) {}
1523 /* Context activation is done by the caller. */
1524 static void shader_none_select(void *shader_priv
, struct wined3d_context
*context
,
1525 const struct wined3d_state
*state
)
1527 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1528 struct shader_none_priv
*priv
= shader_priv
;
1530 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
1531 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
1534 /* Context activation is done by the caller. */
1535 static void shader_none_disable(void *shader_priv
, struct wined3d_context
*context
)
1537 struct shader_none_priv
*priv
= shader_priv
;
1538 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1540 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
1541 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
1543 context
->shader_update_mask
= (1 << WINED3D_SHADER_TYPE_PIXEL
)
1544 | (1 << WINED3D_SHADER_TYPE_VERTEX
)
1545 | (1 << WINED3D_SHADER_TYPE_GEOMETRY
);
1548 static HRESULT
shader_none_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
1549 const struct fragment_pipeline
*fragment_pipe
)
1551 struct fragment_caps fragment_caps
;
1552 void *vertex_priv
, *fragment_priv
;
1553 struct shader_none_priv
*priv
;
1555 if (!(priv
= HeapAlloc(GetProcessHeap(), 0, sizeof(*priv
))))
1556 return E_OUTOFMEMORY
;
1558 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&none_shader_backend
, priv
)))
1560 ERR("Failed to initialize vertex pipe.\n");
1561 HeapFree(GetProcessHeap(), 0, priv
);
1565 if (!(fragment_priv
= fragment_pipe
->alloc_private(&none_shader_backend
, priv
)))
1567 ERR("Failed to initialize fragment pipe.\n");
1568 vertex_pipe
->vp_free(device
);
1569 HeapFree(GetProcessHeap(), 0, priv
);
1573 priv
->vertex_pipe
= vertex_pipe
;
1574 priv
->fragment_pipe
= fragment_pipe
;
1575 fragment_pipe
->get_caps(&device
->adapter
->gl_info
, &fragment_caps
);
1576 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
1578 device
->vertex_priv
= vertex_priv
;
1579 device
->fragment_priv
= fragment_priv
;
1580 device
->shader_priv
= priv
;
1585 static void shader_none_free(struct wined3d_device
*device
)
1587 struct shader_none_priv
*priv
= device
->shader_priv
;
1589 priv
->fragment_pipe
->free_private(device
);
1590 priv
->vertex_pipe
->vp_free(device
);
1591 HeapFree(GetProcessHeap(), 0, priv
);
1594 static BOOL
shader_none_allocate_context_data(struct wined3d_context
*context
)
1599 static void shader_none_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
1601 /* Set the shader caps to 0 for the none shader backend */
1602 caps
->vs_version
= 0;
1603 caps
->gs_version
= 0;
1604 caps
->ps_version
= 0;
1605 caps
->vs_uniform_count
= 0;
1606 caps
->ps_uniform_count
= 0;
1607 caps
->ps_1x_max_value
= 0.0f
;
1608 caps
->wined3d_caps
= 0;
1611 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
1613 /* We "support" every possible fixup, since we don't support any shader
1614 * model, and will never have to actually sample a texture. */
1618 static BOOL
shader_none_has_ffp_proj_control(void *shader_priv
)
1620 struct shader_none_priv
*priv
= shader_priv
;
1622 return priv
->ffp_proj_control
;
1625 const struct wined3d_shader_backend_ops none_shader_backend
=
1627 shader_none_handle_instruction
,
1629 shader_none_disable
,
1630 shader_none_select_depth_blt
,
1631 shader_none_deselect_depth_blt
,
1632 shader_none_update_float_vertex_constants
,
1633 shader_none_update_float_pixel_constants
,
1634 shader_none_load_constants
,
1635 shader_none_destroy
,
1638 shader_none_allocate_context_data
,
1639 shader_none_free_context_data
,
1640 shader_none_get_caps
,
1641 shader_none_color_fixup_supported
,
1642 shader_none_has_ffp_proj_control
,
1645 static HRESULT
shader_set_function(struct wined3d_shader
*shader
, const DWORD
*byte_code
,
1646 const struct wined3d_shader_signature
*output_signature
, DWORD float_const_count
,
1647 enum wined3d_shader_type type
, unsigned int max_version
)
1649 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1650 const struct wined3d_shader_frontend
*fe
;
1652 unsigned int backend_version
;
1653 const struct wined3d_d3d_info
*d3d_info
= &shader
->device
->adapter
->d3d_info
;
1655 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1656 shader
, byte_code
, output_signature
, float_const_count
);
1658 fe
= shader_select_frontend(*byte_code
);
1661 FIXME("Unable to find frontend for shader.\n");
1662 return WINED3DERR_INVALIDCALL
;
1664 shader
->frontend
= fe
;
1665 shader
->frontend_data
= fe
->shader_init(byte_code
, output_signature
);
1666 if (!shader
->frontend_data
)
1668 FIXME("Failed to initialize frontend.\n");
1669 return WINED3DERR_INVALIDCALL
;
1672 /* First pass: trace shader. */
1673 if (TRACE_ON(d3d_shader
))
1674 shader_trace_init(fe
, shader
->frontend_data
, byte_code
);
1676 /* Initialize immediate constant lists. */
1677 list_init(&shader
->constantsF
);
1678 list_init(&shader
->constantsB
);
1679 list_init(&shader
->constantsI
);
1680 shader
->lconst_inf_or_nan
= FALSE
;
1682 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1683 hr
= shader_get_registers_used(shader
, fe
,
1684 reg_maps
, shader
->input_signature
, shader
->output_signature
,
1685 byte_code
, float_const_count
);
1686 if (FAILED(hr
)) return hr
;
1688 if (reg_maps
->shader_version
.type
!= type
)
1690 WARN("Wrong shader type %d.\n", reg_maps
->shader_version
.type
);
1691 return WINED3DERR_INVALIDCALL
;
1693 if (reg_maps
->shader_version
.major
> max_version
)
1695 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps
->shader_version
.major
);
1696 return WINED3DERR_INVALIDCALL
;
1700 case WINED3D_SHADER_TYPE_VERTEX
:
1701 backend_version
= d3d_info
->limits
.vs_version
;
1703 case WINED3D_SHADER_TYPE_GEOMETRY
:
1704 backend_version
= d3d_info
->limits
.gs_version
;
1706 case WINED3D_SHADER_TYPE_PIXEL
:
1707 backend_version
= d3d_info
->limits
.ps_version
;
1710 FIXME("No backend version-checking for this shader type\n");
1711 backend_version
= 0;
1713 if (reg_maps
->shader_version
.major
> backend_version
)
1715 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
1716 reg_maps
->shader_version
.major
, reg_maps
->shader_version
.minor
);
1717 return WINED3DERR_INVALIDCALL
;
1720 shader
->function
= HeapAlloc(GetProcessHeap(), 0, shader
->functionLength
);
1721 if (!shader
->function
)
1722 return E_OUTOFMEMORY
;
1723 memcpy(shader
->function
, byte_code
, shader
->functionLength
);
1728 ULONG CDECL
wined3d_shader_incref(struct wined3d_shader
*shader
)
1730 ULONG refcount
= InterlockedIncrement(&shader
->ref
);
1732 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
1737 ULONG CDECL
wined3d_shader_decref(struct wined3d_shader
*shader
)
1739 ULONG refcount
= InterlockedDecrement(&shader
->ref
);
1741 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
1745 shader_cleanup(shader
);
1746 shader
->parent_ops
->wined3d_object_destroyed(shader
->parent
);
1747 HeapFree(GetProcessHeap(), 0, shader
);
1753 void * CDECL
wined3d_shader_get_parent(const struct wined3d_shader
*shader
)
1755 TRACE("shader %p.\n", shader
);
1757 return shader
->parent
;
1760 HRESULT CDECL
wined3d_shader_get_byte_code(const struct wined3d_shader
*shader
,
1761 void *byte_code
, UINT
*byte_code_size
)
1763 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader
, byte_code
, byte_code_size
);
1767 *byte_code_size
= shader
->functionLength
;
1771 if (*byte_code_size
< shader
->functionLength
)
1773 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
1774 * than the required size we should write the required size and
1775 * return D3DERR_MOREDATA. That's not actually true. */
1776 return WINED3DERR_INVALIDCALL
;
1779 memcpy(byte_code
, shader
->function
, shader
->functionLength
);
1784 /* Set local constants for d3d8 shaders. */
1785 HRESULT CDECL
wined3d_shader_set_local_constants_float(struct wined3d_shader
*shader
,
1786 UINT start_idx
, const float *src_data
, UINT count
)
1788 UINT end_idx
= start_idx
+ count
;
1791 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader
, start_idx
, src_data
, count
);
1793 if (end_idx
> shader
->limits
.constant_float
)
1795 WARN("end_idx %u > float constants limit %u.\n",
1796 end_idx
, shader
->limits
.constant_float
);
1797 end_idx
= shader
->limits
.constant_float
;
1800 for (i
= start_idx
; i
< end_idx
; ++i
)
1802 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
1805 return E_OUTOFMEMORY
;
1808 value
= (float *)lconst
->value
;
1809 memcpy(value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
1810 list_add_head(&shader
->constantsF
, &lconst
->entry
);
1812 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
1813 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
1815 shader
->lconst_inf_or_nan
= TRUE
;
1822 void find_vs_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
1823 WORD swizzle_map
, struct vs_compile_args
*args
)
1825 args
->fog_src
= state
->render_states
[WINED3D_RS_FOGTABLEMODE
]
1826 == WINED3D_FOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
1827 args
->clip_enabled
= state
->render_states
[WINED3D_RS_CLIPPING
]
1828 && state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
];
1829 args
->swizzle_map
= swizzle_map
;
1832 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
1834 if (usage_idx1
!= usage_idx2
)
1836 if (usage1
== usage2
)
1838 if (usage1
== WINED3D_DECL_USAGE_POSITION
&& usage2
== WINED3D_DECL_USAGE_POSITIONT
)
1840 if (usage2
== WINED3D_DECL_USAGE_POSITION
&& usage1
== WINED3D_DECL_USAGE_POSITIONT
)
1846 BOOL
vshader_get_input(const struct wined3d_shader
*shader
,
1847 BYTE usage_req
, BYTE usage_idx_req
, unsigned int *regnum
)
1849 WORD map
= shader
->reg_maps
.input_registers
;
1852 for (i
= 0; map
; map
>>= 1, ++i
)
1854 if (!(map
& 1)) continue;
1856 if (match_usage(shader
->u
.vs
.attributes
[i
].usage
,
1857 shader
->u
.vs
.attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
1866 static void vertexshader_set_limits(struct wined3d_shader
*shader
)
1868 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
1869 shader
->reg_maps
.shader_version
.minor
);
1870 struct wined3d_device
*device
= shader
->device
;
1871 const DWORD vs_uniform_count
= device
->adapter
->d3d_info
.limits
.vs_uniform_count
;
1873 shader
->limits
.packed_input
= 0;
1875 switch (shader_version
)
1877 case WINED3D_SHADER_VERSION(1, 0):
1878 case WINED3D_SHADER_VERSION(1, 1):
1879 shader
->limits
.constant_bool
= 0;
1880 shader
->limits
.constant_int
= 0;
1881 shader
->limits
.packed_output
= 12;
1882 shader
->limits
.sampler
= 0;
1883 /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1884 * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1886 shader
->limits
.constant_float
= min(256, vs_uniform_count
);
1889 case WINED3D_SHADER_VERSION(2, 0):
1890 case WINED3D_SHADER_VERSION(2, 1):
1891 shader
->limits
.constant_bool
= 16;
1892 shader
->limits
.constant_int
= 16;
1893 shader
->limits
.packed_output
= 12;
1894 shader
->limits
.sampler
= 0;
1895 shader
->limits
.constant_float
= min(256, vs_uniform_count
);
1898 case WINED3D_SHADER_VERSION(3, 0):
1899 shader
->limits
.constant_bool
= 16;
1900 shader
->limits
.constant_int
= 16;
1901 shader
->limits
.packed_output
= 12;
1902 shader
->limits
.sampler
= 4;
1903 /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1904 * even though they are capable of supporting much more (GL
1905 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1906 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1907 * shaders to 256. */
1908 shader
->limits
.constant_float
= min(256, vs_uniform_count
);
1911 case WINED3D_SHADER_VERSION(4, 0):
1912 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
1913 shader
->limits
.constant_int
= 0;
1914 shader
->limits
.constant_float
= 0;
1915 shader
->limits
.constant_bool
= 0;
1916 shader
->limits
.packed_output
= 16;
1917 shader
->limits
.packed_input
= 0;
1921 shader
->limits
.constant_bool
= 16;
1922 shader
->limits
.constant_int
= 16;
1923 shader
->limits
.packed_output
= 12;
1924 shader
->limits
.sampler
= 0;
1925 shader
->limits
.constant_float
= min(256, vs_uniform_count
);
1926 FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1927 shader
->reg_maps
.shader_version
.major
,
1928 shader
->reg_maps
.shader_version
.minor
);
1932 static HRESULT
vertexshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
1933 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
1934 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
1936 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1940 const DWORD vs_uniform_count
= device
->adapter
->d3d_info
.limits
.vs_uniform_count
;
1942 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
1944 shader_init(shader
, device
, parent
, parent_ops
);
1945 hr
= shader_set_function(shader
, byte_code
, output_signature
, vs_uniform_count
,
1946 WINED3D_SHADER_TYPE_VERTEX
, max_version
);
1949 WARN("Failed to set function, hr %#x.\n", hr
);
1950 shader_cleanup(shader
);
1954 map
= reg_maps
->input_registers
;
1955 for (i
= 0; map
; map
>>= 1, ++i
)
1957 if (!(map
& 1) || !shader
->input_signature
[i
].semantic_name
)
1960 shader
->u
.vs
.attributes
[i
].usage
=
1961 shader_usage_from_semantic_name(shader
->input_signature
[i
].semantic_name
);
1962 shader
->u
.vs
.attributes
[i
].usage_idx
= shader
->input_signature
[i
].semantic_idx
;
1965 if (output_signature
)
1967 for (i
= 0; i
< output_signature
->element_count
; ++i
)
1969 struct wined3d_shader_signature_element
*e
= &output_signature
->elements
[i
];
1970 reg_maps
->output_registers
|= 1 << e
->register_idx
;
1971 shader
->output_signature
[e
->register_idx
] = *e
;
1975 vertexshader_set_limits(shader
);
1977 shader
->load_local_constsF
= (reg_maps
->usesrelconstF
&& !list_empty(&shader
->constantsF
)) ||
1978 shader
->lconst_inf_or_nan
;
1983 static void geometryshader_set_limits(struct wined3d_shader
*shader
)
1985 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
1986 shader
->reg_maps
.shader_version
.minor
);
1988 switch (shader_version
)
1990 case WINED3D_SHADER_VERSION(4, 0):
1991 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
1992 shader
->limits
.constant_int
= 0;
1993 shader
->limits
.constant_float
= 0;
1994 shader
->limits
.constant_bool
= 0;
1995 shader
->limits
.packed_output
= 32;
1996 shader
->limits
.packed_input
= 16;
2000 memset(&shader
->limits
, 0, sizeof(shader
->limits
));
2001 FIXME("Unhandled geometry shader version \"%u.%u\".\n",
2002 shader
->reg_maps
.shader_version
.major
,
2003 shader
->reg_maps
.shader_version
.minor
);
2007 static HRESULT
geometryshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2008 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
2009 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
2013 shader_init(shader
, device
, parent
, parent_ops
);
2014 hr
= shader_set_function(shader
, byte_code
, output_signature
, 0,
2015 WINED3D_SHADER_TYPE_GEOMETRY
, max_version
);
2018 WARN("Failed to set function, hr %#x.\n", hr
);
2019 shader_cleanup(shader
);
2023 geometryshader_set_limits(shader
);
2025 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2030 void find_ps_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2031 BOOL position_transformed
, struct ps_compile_args
*args
, const struct wined3d_gl_info
*gl_info
)
2033 const struct wined3d_texture
*texture
;
2036 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
2037 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && state
->render_states
[WINED3D_RS_SRGBWRITEENABLE
])
2039 const struct wined3d_surface
*rt
= state
->fb
->render_targets
[0];
2040 if (rt
->resource
.format
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
2042 static unsigned int warned
= 0;
2044 args
->srgb_correction
= 1;
2045 if (state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] && !warned
++)
2046 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
2047 "support, expect rendering artifacts.\n");
2051 if (shader
->reg_maps
.shader_version
.major
== 1
2052 && shader
->reg_maps
.shader_version
.minor
<= 3)
2054 for (i
= 0; i
< shader
->limits
.sampler
; ++i
)
2056 DWORD flags
= state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
];
2058 if (flags
& WINED3D_TTFF_PROJECTED
)
2060 DWORD tex_transform
= flags
& ~WINED3D_TTFF_PROJECTED
;
2062 if (!state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
2065 unsigned int index
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
2066 DWORD max_valid
= WINED3D_TTFF_COUNT4
;
2067 enum wined3d_sampler_texture_type sampler_type
= shader
->reg_maps
.sampler_type
[i
];
2069 for (j
= 0; j
< state
->vertex_declaration
->element_count
; ++j
)
2071 struct wined3d_vertex_declaration_element
*element
=
2072 &state
->vertex_declaration
->elements
[j
];
2074 if (element
->usage
== WINED3D_DECL_USAGE_TEXCOORD
2075 && element
->usage_idx
== index
)
2077 max_valid
= element
->format
->component_count
;
2081 if (!tex_transform
|| tex_transform
> max_valid
)
2083 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2084 tex_transform
, max_valid
);
2085 tex_transform
= max_valid
;
2087 if ((sampler_type
== WINED3DSTT_1D
&& tex_transform
> WINED3D_TTFF_COUNT1
)
2088 || (sampler_type
== WINED3DSTT_2D
&& tex_transform
> WINED3D_TTFF_COUNT2
)
2089 || (sampler_type
== WINED3DSTT_VOLUME
&& tex_transform
> WINED3D_TTFF_COUNT3
))
2090 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
2093 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2094 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2095 i
, tex_transform
, sampler_type
);
2099 tex_transform
= WINED3D_TTFF_COUNT4
| WINED3D_PSARGS_PROJECTED
;
2101 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
2105 if (shader
->reg_maps
.shader_version
.major
== 1
2106 && shader
->reg_maps
.shader_version
.minor
<= 4)
2108 for (i
= 0; i
< shader
->limits
.sampler
; ++i
)
2110 const struct wined3d_texture
*texture
= state
->textures
[i
];
2112 if (!shader
->reg_maps
.sampler_type
[i
])
2115 /* Treat unbound textures as 2D. The dummy texture will provide
2116 * the proper sample value. The tex_types bitmap defaults to
2117 * 2D because of the memset. */
2121 switch (texture
->target
)
2123 /* RECT textures are distinguished from 2D textures via np2_fixup */
2124 case GL_TEXTURE_RECTANGLE_ARB
:
2129 args
->tex_types
|= WINED3D_SHADER_TEX_3D
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
2132 case GL_TEXTURE_CUBE_MAP_ARB
:
2133 args
->tex_types
|= WINED3D_SHADER_TEX_CUBE
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
2139 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2141 if (!shader
->reg_maps
.sampler_type
[i
])
2144 texture
= state
->textures
[i
];
2147 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
2150 args
->color_fixup
[i
] = texture
->resource
.format
->color_fixup
;
2152 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
2153 args
->shadow
|= 1 << i
;
2155 /* Flag samplers that need NP2 texcoord fixup. */
2156 if (!(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
2157 args
->np2_fixup
|= (1 << i
);
2159 if (shader
->reg_maps
.shader_version
.major
>= 3)
2161 if (position_transformed
)
2162 args
->vp_mode
= pretransformed
;
2163 else if (use_vs(state
))
2164 args
->vp_mode
= vertexshader
;
2166 args
->vp_mode
= fixedfunction
;
2167 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
2171 args
->vp_mode
= vertexshader
;
2172 if (state
->render_states
[WINED3D_RS_FOGENABLE
])
2174 switch (state
->render_states
[WINED3D_RS_FOGTABLEMODE
])
2176 case WINED3D_FOG_NONE
:
2177 if (position_transformed
|| use_vs(state
))
2179 args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
;
2183 switch (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
])
2185 case WINED3D_FOG_NONE
: /* Fall through. */
2186 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
2187 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
2188 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
2192 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
2193 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
2194 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
2199 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
2204 static void pixelshader_set_limits(struct wined3d_shader
*shader
)
2206 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
2207 shader
->reg_maps
.shader_version
.minor
);
2209 shader
->limits
.packed_output
= 0;
2211 switch (shader_version
)
2213 case WINED3D_SHADER_VERSION(1, 0):
2214 case WINED3D_SHADER_VERSION(1, 1):
2215 case WINED3D_SHADER_VERSION(1, 2):
2216 case WINED3D_SHADER_VERSION(1, 3):
2217 shader
->limits
.constant_float
= 8;
2218 shader
->limits
.constant_int
= 0;
2219 shader
->limits
.constant_bool
= 0;
2220 shader
->limits
.sampler
= 4;
2221 shader
->limits
.packed_input
= 0;
2224 case WINED3D_SHADER_VERSION(1, 4):
2225 shader
->limits
.constant_float
= 8;
2226 shader
->limits
.constant_int
= 0;
2227 shader
->limits
.constant_bool
= 0;
2228 shader
->limits
.sampler
= 6;
2229 shader
->limits
.packed_input
= 0;
2232 /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2233 case WINED3D_SHADER_VERSION(2, 0):
2234 shader
->limits
.constant_float
= 32;
2235 shader
->limits
.constant_int
= 16;
2236 shader
->limits
.constant_bool
= 16;
2237 shader
->limits
.sampler
= 16;
2238 shader
->limits
.packed_input
= 0;
2241 case WINED3D_SHADER_VERSION(2, 1):
2242 shader
->limits
.constant_float
= 32;
2243 shader
->limits
.constant_int
= 16;
2244 shader
->limits
.constant_bool
= 16;
2245 shader
->limits
.sampler
= 16;
2246 shader
->limits
.packed_input
= 0;
2249 case WINED3D_SHADER_VERSION(3, 0):
2250 shader
->limits
.constant_float
= 224;
2251 shader
->limits
.constant_int
= 16;
2252 shader
->limits
.constant_bool
= 16;
2253 shader
->limits
.sampler
= 16;
2254 shader
->limits
.packed_input
= 12;
2257 case WINED3D_SHADER_VERSION(4, 0):
2258 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
2259 shader
->limits
.constant_int
= 0;
2260 shader
->limits
.constant_float
= 0;
2261 shader
->limits
.constant_bool
= 0;
2262 shader
->limits
.packed_input
= 32;
2266 shader
->limits
.constant_float
= 32;
2267 shader
->limits
.constant_int
= 16;
2268 shader
->limits
.constant_bool
= 16;
2269 shader
->limits
.sampler
= 16;
2270 shader
->limits
.packed_input
= 0;
2271 FIXME("Unrecognized pixel shader version %u.%u\n",
2272 shader
->reg_maps
.shader_version
.major
,
2273 shader
->reg_maps
.shader_version
.minor
);
2277 static HRESULT
pixelshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2278 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
2279 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
2281 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2282 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
2284 const DWORD ps_uniform_count
= device
->adapter
->d3d_info
.limits
.ps_uniform_count
;
2286 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
2288 shader_init(shader
, device
, parent
, parent_ops
);
2289 hr
= shader_set_function(shader
, byte_code
, output_signature
, ps_uniform_count
,
2290 WINED3D_SHADER_TYPE_PIXEL
, max_version
);
2293 WARN("Failed to set function, hr %#x.\n", hr
);
2294 shader_cleanup(shader
);
2298 pixelshader_set_limits(shader
);
2300 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2302 if (shader
->u
.ps
.input_reg_used
[i
])
2305 highest_reg_used
= i
;
2309 /* Don't do any register mapping magic if it is not needed, or if we can't
2310 * achieve anything anyway */
2311 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
2312 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2314 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2316 /* This happens with relative addressing. The input mapper function
2317 * warns about this if the higher registers are declared too, so
2318 * don't write a FIXME here */
2319 WARN("More varying registers used than supported\n");
2322 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2324 shader
->u
.ps
.input_reg_map
[i
] = i
;
2327 shader
->u
.ps
.declared_in_count
= highest_reg_used
+ 1;
2331 shader
->u
.ps
.declared_in_count
= 0;
2332 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2334 if (shader
->u
.ps
.input_reg_used
[i
])
2335 shader
->u
.ps
.input_reg_map
[i
] = shader
->u
.ps
.declared_in_count
++;
2336 else shader
->u
.ps
.input_reg_map
[i
] = ~0U;
2340 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2345 void pixelshader_update_samplers(struct wined3d_shader
*shader
, WORD tex_types
)
2347 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2348 enum wined3d_sampler_texture_type
*sampler_type
= reg_maps
->sampler_type
;
2351 if (reg_maps
->shader_version
.major
!= 1) return;
2353 for (i
= 0; i
< shader
->limits
.sampler
; ++i
)
2355 /* We don't sample from this sampler. */
2356 if (!sampler_type
[i
]) continue;
2358 switch ((tex_types
>> i
* WINED3D_PSARGS_TEXTYPE_SHIFT
) & WINED3D_PSARGS_TEXTYPE_MASK
)
2360 case WINED3D_SHADER_TEX_2D
:
2361 sampler_type
[i
] = WINED3DSTT_2D
;
2364 case WINED3D_SHADER_TEX_3D
:
2365 sampler_type
[i
] = WINED3DSTT_VOLUME
;
2368 case WINED3D_SHADER_TEX_CUBE
:
2369 sampler_type
[i
] = WINED3DSTT_CUBE
;
2375 HRESULT CDECL
wined3d_shader_create_gs(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
= geometryshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2392 WARN("Failed to initialize geometry shader, hr %#x.\n", hr
);
2393 HeapFree(GetProcessHeap(), 0, object
);
2397 TRACE("Created geometry shader %p.\n", object
);
2403 HRESULT CDECL
wined3d_shader_create_ps(struct wined3d_device
*device
, const DWORD
*byte_code
,
2404 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2405 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2407 struct wined3d_shader
*object
;
2410 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2411 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2413 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2415 return E_OUTOFMEMORY
;
2417 hr
= pixelshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2420 WARN("Failed to initialize pixel shader, hr %#x.\n", hr
);
2421 HeapFree(GetProcessHeap(), 0, object
);
2425 TRACE("Created pixel shader %p.\n", object
);
2431 HRESULT CDECL
wined3d_shader_create_vs(struct wined3d_device
*device
, const DWORD
*byte_code
,
2432 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2433 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2435 struct wined3d_shader
*object
;
2438 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2439 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2441 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2443 return E_OUTOFMEMORY
;
2445 hr
= vertexshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2448 WARN("Failed to initialize vertex shader, hr %#x.\n", hr
);
2449 HeapFree(GetProcessHeap(), 0, object
);
2453 TRACE("Created vertex shader %p.\n", object
);