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 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 static const char * const shader_opcode_names
[] =
38 /* WINED3DSIH_ABS */ "abs",
39 /* WINED3DSIH_ADD */ "add",
40 /* WINED3DSIH_AND */ "and",
41 /* WINED3DSIH_BEM */ "bem",
42 /* WINED3DSIH_BREAK */ "break",
43 /* WINED3DSIH_BREAKC */ "breakc",
44 /* WINED3DSIH_BREAKP */ "breakp",
45 /* WINED3DSIH_CALL */ "call",
46 /* WINED3DSIH_CALLNZ */ "callnz",
47 /* WINED3DSIH_CMP */ "cmp",
48 /* WINED3DSIH_CND */ "cnd",
49 /* WINED3DSIH_CRS */ "crs",
50 /* WINED3DSIH_CUT */ "cut",
51 /* WINED3DSIH_DCL */ "dcl",
52 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
53 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
54 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
55 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
56 /* WINED3DSIH_DEF */ "def",
57 /* WINED3DSIH_DEFB */ "defb",
58 /* WINED3DSIH_DEFI */ "defi",
59 /* WINED3DSIH_DIV */ "div",
60 /* WINED3DSIH_DP2ADD */ "dp2add",
61 /* WINED3DSIH_DP3 */ "dp3",
62 /* WINED3DSIH_DP4 */ "dp4",
63 /* WINED3DSIH_DST */ "dst",
64 /* WINED3DSIH_DSX */ "dsx",
65 /* WINED3DSIH_DSY */ "dsy",
66 /* WINED3DSIH_ELSE */ "else",
67 /* WINED3DSIH_EMIT */ "emit",
68 /* WINED3DSIH_ENDIF */ "endif",
69 /* WINED3DSIH_ENDLOOP */ "endloop",
70 /* WINED3DSIH_ENDREP */ "endrep",
71 /* WINED3DSIH_EQ */ "eq",
72 /* WINED3DSIH_EXP */ "exp",
73 /* WINED3DSIH_EXPP */ "expp",
74 /* WINED3DSIH_FRC */ "frc",
75 /* WINED3DSIH_FTOI */ "ftoi",
76 /* WINED3DSIH_GE */ "ge",
77 /* WINED3DSIH_IADD */ "iadd",
78 /* WINED3DSIH_IEQ */ "ieq",
79 /* WINED3DSIH_IF */ "if",
80 /* WINED3DSIH_IFC */ "ifc",
81 /* WINED3DSIH_IGE */ "ige",
82 /* WINED3DSIH_IMUL */ "imul",
83 /* WINED3DSIH_ITOF */ "itof",
84 /* WINED3DSIH_LABEL */ "label",
85 /* WINED3DSIH_LD */ "ld",
86 /* WINED3DSIH_LIT */ "lit",
87 /* WINED3DSIH_LOG */ "log",
88 /* WINED3DSIH_LOGP */ "logp",
89 /* WINED3DSIH_LOOP */ "loop",
90 /* WINED3DSIH_LRP */ "lrp",
91 /* WINED3DSIH_LT */ "lt",
92 /* WINED3DSIH_M3x2 */ "m3x2",
93 /* WINED3DSIH_M3x3 */ "m3x3",
94 /* WINED3DSIH_M3x4 */ "m3x4",
95 /* WINED3DSIH_M4x3 */ "m4x3",
96 /* WINED3DSIH_M4x4 */ "m4x4",
97 /* WINED3DSIH_MAD */ "mad",
98 /* WINED3DSIH_MAX */ "max",
99 /* WINED3DSIH_MIN */ "min",
100 /* WINED3DSIH_MOV */ "mov",
101 /* WINED3DSIH_MOVA */ "mova",
102 /* WINED3DSIH_MOVC */ "movc",
103 /* WINED3DSIH_MUL */ "mul",
104 /* WINED3DSIH_NOP */ "nop",
105 /* WINED3DSIH_NRM */ "nrm",
106 /* WINED3DSIH_PHASE */ "phase",
107 /* WINED3DSIH_POW */ "pow",
108 /* WINED3DSIH_RCP */ "rcp",
109 /* WINED3DSIH_REP */ "rep",
110 /* WINED3DSIH_RET */ "ret",
111 /* WINED3DSIH_ROUND_NI */ "round_ni",
112 /* WINED3DSIH_RSQ */ "rsq",
113 /* WINED3DSIH_SAMPLE */ "sample",
114 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
115 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
116 /* WINED3DSIH_SETP */ "setp",
117 /* WINED3DSIH_SGE */ "sge",
118 /* WINED3DSIH_SGN */ "sgn",
119 /* WINED3DSIH_SINCOS */ "sincos",
120 /* WINED3DSIH_SLT */ "slt",
121 /* WINED3DSIH_SQRT */ "sqrt",
122 /* WINED3DSIH_SUB */ "sub",
123 /* WINED3DSIH_TEX */ "texld",
124 /* WINED3DSIH_TEXBEM */ "texbem",
125 /* WINED3DSIH_TEXBEML */ "texbeml",
126 /* WINED3DSIH_TEXCOORD */ "texcrd",
127 /* WINED3DSIH_TEXDEPTH */ "texdepth",
128 /* WINED3DSIH_TEXDP3 */ "texdp3",
129 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
130 /* WINED3DSIH_TEXKILL */ "texkill",
131 /* WINED3DSIH_TEXLDD */ "texldd",
132 /* WINED3DSIH_TEXLDL */ "texldl",
133 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
134 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
135 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
136 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
137 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
138 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
139 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
140 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
141 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
142 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
143 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
144 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
145 /* WINED3DSIH_UDIV */ "udiv",
146 /* WINED3DSIH_USHR */ "ushr",
147 /* WINED3DSIH_UTOF */ "utof",
148 /* WINED3DSIH_XOR */ "xor",
151 static const char * const semantic_names
[] =
153 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
154 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
155 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
156 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
157 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
158 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
159 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
160 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
161 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
162 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
163 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
164 /* WINED3D_DECL_USAGE_FOG */ "FOG",
165 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
166 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
169 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage
)
171 if (usage
>= sizeof(semantic_names
) / sizeof(*semantic_names
))
173 FIXME("Unrecognized usage %#x.\n", usage
);
174 return "UNRECOGNIZED";
177 return semantic_names
[usage
];
180 static enum wined3d_decl_usage
shader_usage_from_semantic_name(const char *name
)
184 for (i
= 0; i
< sizeof(semantic_names
) / sizeof(*semantic_names
); ++i
)
186 if (!strcmp(name
, semantic_names
[i
])) return i
;
192 BOOL
shader_match_semantic(const char *semantic_name
, enum wined3d_decl_usage usage
)
194 return !strcmp(semantic_name
, shader_semantic_name_from_usage(usage
));
197 static void shader_signature_from_semantic(struct wined3d_shader_signature_element
*e
,
198 const struct wined3d_shader_semantic
*s
)
200 e
->semantic_name
= shader_semantic_name_from_usage(s
->usage
);
201 e
->semantic_idx
= s
->usage_idx
;
202 e
->sysval_semantic
= 0;
203 e
->component_type
= 0;
204 e
->register_idx
= s
->reg
.reg
.idx
[0].offset
;
205 e
->mask
= s
->reg
.write_mask
;
208 static void shader_signature_from_usage(struct wined3d_shader_signature_element
*e
,
209 enum wined3d_decl_usage usage
, UINT usage_idx
, UINT reg_idx
, DWORD write_mask
)
211 e
->semantic_name
= shader_semantic_name_from_usage(usage
);
212 e
->semantic_idx
= usage_idx
;
213 e
->sysval_semantic
= 0;
214 e
->component_type
= 0;
215 e
->register_idx
= reg_idx
;
216 e
->mask
= write_mask
;
219 static const struct wined3d_shader_frontend
*shader_select_frontend(DWORD version_token
)
221 switch (version_token
>> 16)
225 return &sm1_shader_frontend
;
230 return &sm4_shader_frontend
;
233 FIXME("Unrecognised version token %#x\n", version_token
);
238 void shader_buffer_clear(struct wined3d_shader_buffer
*buffer
)
240 buffer
->buffer
[0] = '\0';
241 buffer
->content_size
= 0;
243 buffer
->newline
= TRUE
;
246 BOOL
shader_buffer_init(struct wined3d_shader_buffer
*buffer
)
248 buffer
->buffer
= HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE
);
251 ERR("Failed to allocate shader buffer memory.\n");
254 buffer
->buffer_size
= SHADER_PGMSIZE
;
256 shader_buffer_clear(buffer
);
260 void shader_buffer_free(struct wined3d_shader_buffer
*buffer
)
262 HeapFree(GetProcessHeap(), 0, buffer
->buffer
);
265 int shader_vaddline(struct wined3d_shader_buffer
*buffer
, const char *format
, va_list args
)
267 char *base
= buffer
->buffer
+ buffer
->content_size
;
273 rc
= vsnprintf(base
, buffer
->buffer_size
- buffer
->content_size
, format
, args
);
274 if (rc
< 0 /* C89 */ || (unsigned int)rc
>= buffer
->buffer_size
- buffer
->content_size
/* C99 */)
276 new_buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
->buffer
, buffer
->buffer_size
* 2);
279 ERR("The buffer allocated for the shader program string is too small at %d bytes.\n", buffer
->buffer_size
);
280 buffer
->content_size
= buffer
->buffer_size
- 1;
283 buffer
->buffer
= new_buffer
;
284 buffer
->buffer_size
= buffer
->buffer_size
* 2;
285 base
= buffer
->buffer
+ buffer
->content_size
;
295 TRACE("GL HW (%u, %u) : %s", buffer
->lineNo
+ 1, buffer
->content_size
, base
);
296 buffer
->newline
= FALSE
;
303 buffer
->content_size
+= rc
;
304 if (buffer
->buffer
[buffer
->content_size
-1] == '\n')
307 buffer
->newline
= TRUE
;
313 int shader_addline(struct wined3d_shader_buffer
*buffer
, const char *format
, ...)
318 va_start(args
, format
);
319 ret
= shader_vaddline(buffer
, format
, args
);
325 static void shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
326 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
329 shader
->device
= device
;
330 shader
->parent
= parent
;
331 shader
->parent_ops
= parent_ops
;
332 list_init(&shader
->linked_programs
);
333 list_add_head(&device
->shaders
, &shader
->shader_list_entry
);
336 /* Convert floating point offset relative to a register file to an absolute
337 * offset for float constants. */
338 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type
, UINT register_idx
)
340 switch (register_type
)
342 case WINED3DSPR_CONST
: return register_idx
;
343 case WINED3DSPR_CONST2
: return 2048 + register_idx
;
344 case WINED3DSPR_CONST3
: return 4096 + register_idx
;
345 case WINED3DSPR_CONST4
: return 6144 + register_idx
;
347 FIXME("Unsupported register type: %u.\n", register_type
);
352 static void shader_delete_constant_list(struct list
*clist
)
354 struct wined3d_shader_lconst
*constant
;
357 ptr
= list_head(clist
);
360 constant
= LIST_ENTRY(ptr
, struct wined3d_shader_lconst
, entry
);
361 ptr
= list_next(clist
, ptr
);
362 HeapFree(GetProcessHeap(), 0, constant
);
367 static inline void set_bitmap_bit(DWORD
*bitmap
, DWORD bit
)
372 bitmap
[idx
] |= (1 << shift
);
375 static void shader_record_register_usage(struct wined3d_shader
*shader
, struct wined3d_shader_reg_maps
*reg_maps
,
376 const struct wined3d_shader_register
*reg
, enum wined3d_shader_type shader_type
)
380 case WINED3DSPR_TEXTURE
: /* WINED3DSPR_ADDR */
381 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
382 reg_maps
->texcoord
|= 1 << reg
->idx
[0].offset
;
384 reg_maps
->address
|= 1 << reg
->idx
[0].offset
;
387 case WINED3DSPR_TEMP
:
388 reg_maps
->temporary
|= 1 << reg
->idx
[0].offset
;
391 case WINED3DSPR_INPUT
:
392 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
394 if (reg
->idx
[0].rel_addr
)
396 /* If relative addressing is used, we must assume that all registers
397 * are used. Even if it is a construct like v3[aL], we can't assume
398 * that v0, v1 and v2 aren't read because aL can be negative */
400 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
402 shader
->u
.ps
.input_reg_used
[i
] = TRUE
;
407 shader
->u
.ps
.input_reg_used
[reg
->idx
[0].offset
] = TRUE
;
411 reg_maps
->input_registers
|= 1 << reg
->idx
[0].offset
;
414 case WINED3DSPR_RASTOUT
:
415 if (reg
->idx
[0].offset
== 1)
419 case WINED3DSPR_MISCTYPE
:
420 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
422 if (!reg
->idx
[0].offset
)
424 else if (reg
->idx
[0].offset
== 1)
425 reg_maps
->usesfacing
= 1;
429 case WINED3DSPR_CONST
:
430 if (reg
->idx
[0].rel_addr
)
432 if (reg
->idx
[0].offset
< reg_maps
->min_rel_offset
)
433 reg_maps
->min_rel_offset
= reg
->idx
[0].offset
;
434 if (reg
->idx
[0].offset
> reg_maps
->max_rel_offset
)
435 reg_maps
->max_rel_offset
= reg
->idx
[0].offset
;
436 reg_maps
->usesrelconstF
= TRUE
;
440 set_bitmap_bit(reg_maps
->constf
, reg
->idx
[0].offset
);
444 case WINED3DSPR_CONSTINT
:
445 reg_maps
->integer_constants
|= (1 << reg
->idx
[0].offset
);
448 case WINED3DSPR_CONSTBOOL
:
449 reg_maps
->boolean_constants
|= (1 << reg
->idx
[0].offset
);
452 case WINED3DSPR_COLOROUT
:
453 reg_maps
->rt_mask
|= (1 << reg
->idx
[0].offset
);
457 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
458 reg
->type
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
463 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr
, unsigned int param
)
467 case WINED3DSIH_M4x4
:
468 case WINED3DSIH_M3x4
:
469 return param
== 1 ? 3 : 0;
471 case WINED3DSIH_M4x3
:
472 case WINED3DSIH_M3x3
:
473 return param
== 1 ? 2 : 0;
475 case WINED3DSIH_M3x2
:
476 return param
== 1 ? 1 : 0;
483 /* Note that this does not count the loop register as an address register. */
484 static HRESULT
shader_get_registers_used(struct wined3d_shader
*shader
, const struct wined3d_shader_frontend
*fe
,
485 struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_shader_signature_element
*input_signature
,
486 struct wined3d_shader_signature_element
*output_signature
, const DWORD
*byte_code
, DWORD constf_size
)
488 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
489 void *fe_data
= shader
->frontend_data
;
490 struct wined3d_shader_version shader_version
;
491 const DWORD
*ptr
= byte_code
;
493 memset(reg_maps
, 0, sizeof(*reg_maps
));
494 reg_maps
->min_rel_offset
= ~0U;
496 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
497 reg_maps
->shader_version
= shader_version
;
499 reg_maps
->constf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
500 sizeof(*reg_maps
->constf
) * ((constf_size
+ 31) / 32));
501 if (!reg_maps
->constf
)
503 ERR("Failed to allocate constant map memory.\n");
504 return E_OUTOFMEMORY
;
507 while (!fe
->shader_is_end(fe_data
, &ptr
))
509 struct wined3d_shader_instruction ins
;
512 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
514 /* Unhandled opcode, and its parameters. */
515 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
517 TRACE("Skipping unrecognized instruction.\n");
521 /* Handle declarations. */
522 if (ins
.handler_idx
== WINED3DSIH_DCL
)
524 struct wined3d_shader_semantic
*semantic
= &ins
.declaration
.semantic
;
526 switch (semantic
->reg
.reg
.type
)
528 /* Mark input registers used. */
529 case WINED3DSPR_INPUT
:
530 reg_maps
->input_registers
|= 1 << semantic
->reg
.reg
.idx
[0].offset
;
531 shader_signature_from_semantic(&input_signature
[semantic
->reg
.reg
.idx
[0].offset
], semantic
);
534 /* Vertex shader: mark 3.0 output registers used, save token. */
535 case WINED3DSPR_OUTPUT
:
536 reg_maps
->output_registers
|= 1 << semantic
->reg
.reg
.idx
[0].offset
;
537 shader_signature_from_semantic(&output_signature
[semantic
->reg
.reg
.idx
[0].offset
], semantic
);
538 if (semantic
->usage
== WINED3D_DECL_USAGE_FOG
)
542 /* Save sampler usage token. */
543 case WINED3DSPR_SAMPLER
:
544 reg_maps
->sampler_type
[semantic
->reg
.reg
.idx
[0].offset
] = semantic
->sampler_type
;
548 TRACE("Not recording DCL register type %#x.\n", semantic
->reg
.reg
.type
);
552 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
554 struct wined3d_shader_register
*reg
= &ins
.declaration
.src
.reg
;
555 if (reg
->idx
[0].offset
>= WINED3D_MAX_CBS
)
556 ERR("Invalid CB index %u.\n", reg
->idx
[0].offset
);
558 reg_maps
->cb_sizes
[reg
->idx
[0].offset
] = reg
->idx
[1].offset
;
560 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
)
562 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
563 shader
->u
.gs
.input_type
= ins
.declaration
.primitive_type
;
565 FIXME("Invalid instruction %#x for shader type %#x.\n",
566 ins
.handler_idx
, shader_version
.type
);
568 else if (ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
570 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
571 shader
->u
.gs
.output_type
= ins
.declaration
.primitive_type
;
573 FIXME("Invalid instruction %#x for shader type %#x.\n",
574 ins
.handler_idx
, shader_version
.type
);
576 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
578 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
579 shader
->u
.gs
.vertices_out
= ins
.declaration
.count
;
581 FIXME("Invalid instruction %#x for shader type %#x.\n",
582 ins
.handler_idx
, shader_version
.type
);
584 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
586 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
588 if (!lconst
) return E_OUTOFMEMORY
;
590 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
591 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
592 value
= (float *)lconst
->value
;
594 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
595 if (shader_version
.major
== 1 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
597 if (value
[0] < -1.0f
) value
[0] = -1.0f
;
598 else if (value
[0] > 1.0f
) value
[0] = 1.0f
;
599 if (value
[1] < -1.0f
) value
[1] = -1.0f
;
600 else if (value
[1] > 1.0f
) value
[1] = 1.0f
;
601 if (value
[2] < -1.0f
) value
[2] = -1.0f
;
602 else if (value
[2] > 1.0f
) value
[2] = 1.0f
;
603 if (value
[3] < -1.0f
) value
[3] = -1.0f
;
604 else if (value
[3] > 1.0f
) value
[3] = 1.0f
;
607 list_add_head(&shader
->constantsF
, &lconst
->entry
);
609 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
610 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
612 shader
->lconst_inf_or_nan
= TRUE
;
615 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
617 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
618 if (!lconst
) return E_OUTOFMEMORY
;
620 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
621 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
623 list_add_head(&shader
->constantsI
, &lconst
->entry
);
624 reg_maps
->local_int_consts
|= (1 << lconst
->idx
);
626 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
628 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
629 if (!lconst
) return E_OUTOFMEMORY
;
631 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
632 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, sizeof(DWORD
));
634 list_add_head(&shader
->constantsB
, &lconst
->entry
);
635 reg_maps
->local_bool_consts
|= (1 << lconst
->idx
);
637 /* For subroutine prototypes. */
638 else if (ins
.handler_idx
== WINED3DSIH_LABEL
)
640 reg_maps
->labels
|= 1 << ins
.src
[0].reg
.idx
[0].offset
;
642 /* Set texture, address, temporary registers. */
645 BOOL color0_mov
= FALSE
;
648 /* This will loop over all the registers and try to
649 * make a bitmask of the ones we're interested in.
651 * Relative addressing tokens are ignored, but that's
652 * okay, since we'll catch any address registers when
653 * they are initialized (required by spec). */
654 for (i
= 0; i
< ins
.dst_count
; ++i
)
656 shader_record_register_usage(shader
, reg_maps
, &ins
.dst
[i
].reg
, shader_version
.type
);
658 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
659 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
660 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
661 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
&& shader_version
.major
< 3)
663 UINT idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
665 switch (ins
.dst
[i
].reg
.type
)
667 case WINED3DSPR_RASTOUT
:
671 reg_maps
->output_registers
|= 1 << 10;
672 shader_signature_from_usage(&output_signature
[10],
673 WINED3D_DECL_USAGE_POSITION
, 0, 10, WINED3DSP_WRITEMASK_ALL
);
677 reg_maps
->output_registers
|= 1 << 11;
678 shader_signature_from_usage(&output_signature
[11],
679 WINED3D_DECL_USAGE_FOG
, 0, 11, WINED3DSP_WRITEMASK_0
);
683 reg_maps
->output_registers
|= 1 << 11;
684 shader_signature_from_usage(&output_signature
[11],
685 WINED3D_DECL_USAGE_PSIZE
, 0, 11, WINED3DSP_WRITEMASK_1
);
690 case WINED3DSPR_ATTROUT
:
694 if (reg_maps
->output_registers
& (1 << idx
))
696 output_signature
[idx
].mask
|= ins
.dst
[i
].write_mask
;
700 reg_maps
->output_registers
|= 1 << idx
;
701 shader_signature_from_usage(&output_signature
[idx
],
702 WINED3D_DECL_USAGE_COLOR
, idx
- 8, idx
, ins
.dst
[i
].write_mask
);
707 case WINED3DSPR_TEXCRDOUT
:
709 reg_maps
->texcoord_mask
[idx
] |= ins
.dst
[i
].write_mask
;
710 if (reg_maps
->output_registers
& (1 << idx
))
712 output_signature
[idx
].mask
|= ins
.dst
[i
].write_mask
;
716 reg_maps
->output_registers
|= 1 << idx
;
717 shader_signature_from_usage(&output_signature
[idx
],
718 WINED3D_DECL_USAGE_TEXCOORD
, idx
, idx
, ins
.dst
[i
].write_mask
);
727 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
729 if (ins
.dst
[i
].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
.dst
[i
].reg
.idx
[0].offset
)
731 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
732 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
733 * the mov and perform the sRGB write correction from the source register.
735 * However, if the mov is only partial, we can't do this, and if the write
736 * comes from an instruction other than MOV it is hard to do as well. If
737 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
738 shader
->u
.ps
.color0_mov
= FALSE
;
739 if (ins
.handler_idx
== WINED3DSIH_MOV
740 && ins
.dst
[i
].write_mask
== WINED3DSP_WRITEMASK_ALL
)
742 /* Used later when the source register is read. */
746 /* Also drop the MOV marker if the source register is overwritten prior to the shader
749 else if (ins
.dst
[i
].reg
.type
== WINED3DSPR_TEMP
750 && ins
.dst
[i
].reg
.idx
[0].offset
== shader
->u
.ps
.color0_reg
)
752 shader
->u
.ps
.color0_mov
= FALSE
;
756 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
757 if (shader_version
.major
== 1
758 && (ins
.handler_idx
== WINED3DSIH_TEX
759 || ins
.handler_idx
== WINED3DSIH_TEXBEM
760 || ins
.handler_idx
== WINED3DSIH_TEXBEML
761 || ins
.handler_idx
== WINED3DSIH_TEXDP3TEX
762 || ins
.handler_idx
== WINED3DSIH_TEXM3x2TEX
763 || ins
.handler_idx
== WINED3DSIH_TEXM3x3SPEC
764 || ins
.handler_idx
== WINED3DSIH_TEXM3x3TEX
765 || ins
.handler_idx
== WINED3DSIH_TEXM3x3VSPEC
766 || ins
.handler_idx
== WINED3DSIH_TEXREG2AR
767 || ins
.handler_idx
== WINED3DSIH_TEXREG2GB
768 || ins
.handler_idx
== WINED3DSIH_TEXREG2RGB
))
770 /* Fake sampler usage, only set reserved bit and type. */
771 DWORD sampler_code
= ins
.dst
[i
].reg
.idx
[0].offset
;
773 TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
774 reg_maps
->sampler_type
[sampler_code
] = WINED3DSTT_2D
;
776 /* texbem is only valid with < 1.4 pixel shaders */
777 if (ins
.handler_idx
== WINED3DSIH_TEXBEM
778 || ins
.handler_idx
== WINED3DSIH_TEXBEML
)
780 reg_maps
->bumpmat
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
781 if (ins
.handler_idx
== WINED3DSIH_TEXBEML
)
783 reg_maps
->luminanceparams
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
787 else if (ins
.handler_idx
== WINED3DSIH_BEM
)
789 reg_maps
->bumpmat
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
793 if (ins
.handler_idx
== WINED3DSIH_NRM
) reg_maps
->usesnrm
= 1;
794 else if (ins
.handler_idx
== WINED3DSIH_DSY
) reg_maps
->usesdsy
= 1;
795 else if (ins
.handler_idx
== WINED3DSIH_DSX
) reg_maps
->usesdsx
= 1;
796 else if (ins
.handler_idx
== WINED3DSIH_TEXLDD
) reg_maps
->usestexldd
= 1;
797 else if (ins
.handler_idx
== WINED3DSIH_TEXLDL
) reg_maps
->usestexldl
= 1;
798 else if (ins
.handler_idx
== WINED3DSIH_MOVA
) reg_maps
->usesmova
= 1;
799 else if (ins
.handler_idx
== WINED3DSIH_IFC
) reg_maps
->usesifc
= 1;
800 else if (ins
.handler_idx
== WINED3DSIH_CALL
) reg_maps
->usescall
= 1;
801 else if (ins
.handler_idx
== WINED3DSIH_POW
) reg_maps
->usespow
= 1;
802 else if (ins
.handler_idx
== WINED3DSIH_LOOP
803 || ins
.handler_idx
== WINED3DSIH_REP
)
806 if (cur_loop_depth
> max_loop_depth
)
807 max_loop_depth
= cur_loop_depth
;
809 else if (ins
.handler_idx
== WINED3DSIH_ENDLOOP
810 || ins
.handler_idx
== WINED3DSIH_ENDREP
)
814 shader_record_register_usage(shader
, reg_maps
, &ins
.predicate
->reg
, shader_version
.type
);
816 for (i
= 0; i
< ins
.src_count
; ++i
)
818 unsigned int count
= get_instr_extra_regcount(ins
.handler_idx
, i
);
819 struct wined3d_shader_register reg
= ins
.src
[i
].reg
;
821 shader_record_register_usage(shader
, reg_maps
, &ins
.src
[i
].reg
, shader_version
.type
);
825 shader_record_register_usage(shader
, reg_maps
, ®
, shader_version
.type
);
831 if (ins
.src
[i
].reg
.type
== WINED3DSPR_TEMP
832 && ins
.src
[i
].swizzle
== WINED3DSP_NOSWIZZLE
)
834 shader
->u
.ps
.color0_mov
= TRUE
;
835 shader
->u
.ps
.color0_reg
= ins
.src
[i
].reg
.idx
[0].offset
;
841 reg_maps
->loop_depth
= max_loop_depth
;
843 /* PS before 2.0 don't have explicit color outputs. Instead the value of
844 * R0 is written to the render target. */
845 if (shader_version
.major
< 2 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
846 reg_maps
->rt_mask
|= (1 << 0);
848 shader
->functionLength
= ((const char *)ptr
- (const char *)byte_code
);
853 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int max
)
855 DWORD map
= 1 << max
;
857 map
&= reg_maps
->shader_version
.major
< 3 ? ~reg_maps
->texcoord
: ~reg_maps
->input_registers
;
859 return wined3d_log2i(map
);
862 static void shader_dump_decl_usage(const struct wined3d_shader_semantic
*semantic
,
863 const struct wined3d_shader_version
*shader_version
)
867 if (semantic
->reg
.reg
.type
== WINED3DSPR_SAMPLER
)
869 switch (semantic
->sampler_type
)
871 case WINED3DSTT_2D
: TRACE("_2d"); break;
872 case WINED3DSTT_CUBE
: TRACE("_cube"); break;
873 case WINED3DSTT_VOLUME
: TRACE("_volume"); break;
874 default: TRACE("_unknown_ttype(0x%08x)", semantic
->sampler_type
);
879 /* Pixel shaders 3.0 don't have usage semantics. */
880 if (shader_version
->major
< 3 && shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
) return;
883 switch (semantic
->usage
)
885 case WINED3D_DECL_USAGE_POSITION
:
886 TRACE("position%u", semantic
->usage_idx
);
889 case WINED3D_DECL_USAGE_BLEND_INDICES
:
893 case WINED3D_DECL_USAGE_BLEND_WEIGHT
:
897 case WINED3D_DECL_USAGE_NORMAL
:
898 TRACE("normal%u", semantic
->usage_idx
);
901 case WINED3D_DECL_USAGE_PSIZE
:
905 case WINED3D_DECL_USAGE_COLOR
:
906 if (!semantic
->usage_idx
) TRACE("color");
907 else TRACE("specular%u", (semantic
->usage_idx
- 1));
910 case WINED3D_DECL_USAGE_TEXCOORD
:
911 TRACE("texture%u", semantic
->usage_idx
);
914 case WINED3D_DECL_USAGE_TANGENT
:
918 case WINED3D_DECL_USAGE_BINORMAL
:
922 case WINED3D_DECL_USAGE_TESS_FACTOR
:
926 case WINED3D_DECL_USAGE_POSITIONT
:
927 TRACE("positionT%u", semantic
->usage_idx
);
930 case WINED3D_DECL_USAGE_FOG
:
934 case WINED3D_DECL_USAGE_DEPTH
:
938 case WINED3D_DECL_USAGE_SAMPLE
:
943 FIXME("unknown_semantics(0x%08x)", semantic
->usage
);
948 static void shader_dump_register(const struct wined3d_shader_register
*reg
,
949 const struct wined3d_shader_version
*shader_version
)
951 static const char * const rastout_reg_names
[] = {"oPos", "oFog", "oPts"};
952 static const char * const misctype_reg_names
[] = {"vPos", "vFace"};
953 UINT offset
= reg
->idx
[0].offset
;
957 case WINED3DSPR_TEMP
:
961 case WINED3DSPR_INPUT
:
965 case WINED3DSPR_CONST
:
966 case WINED3DSPR_CONST2
:
967 case WINED3DSPR_CONST3
:
968 case WINED3DSPR_CONST4
:
970 offset
= shader_get_float_offset(reg
->type
, offset
);
973 case WINED3DSPR_TEXTURE
: /* vs: case WINED3DSPR_ADDR */
974 TRACE("%c", shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
? 't' : 'a');
977 case WINED3DSPR_RASTOUT
:
978 TRACE("%s", rastout_reg_names
[offset
]);
981 case WINED3DSPR_COLOROUT
:
985 case WINED3DSPR_DEPTHOUT
:
989 case WINED3DSPR_ATTROUT
:
993 case WINED3DSPR_TEXCRDOUT
:
994 /* Vertex shaders >= 3.0 use general purpose output registers
995 * (WINED3DSPR_OUTPUT), which can include an address token. */
996 if (shader_version
->major
>= 3) TRACE("o");
1000 case WINED3DSPR_CONSTINT
:
1004 case WINED3DSPR_CONSTBOOL
:
1008 case WINED3DSPR_LABEL
:
1012 case WINED3DSPR_LOOP
:
1016 case WINED3DSPR_SAMPLER
:
1020 case WINED3DSPR_MISCTYPE
:
1022 FIXME("Unhandled misctype register %u.\n", offset
);
1024 TRACE("%s", misctype_reg_names
[offset
]);
1027 case WINED3DSPR_PREDICATE
:
1031 case WINED3DSPR_IMMCONST
:
1035 case WINED3DSPR_CONSTBUFFER
:
1039 case WINED3DSPR_PRIMID
:
1043 case WINED3DSPR_NULL
:
1047 case WINED3DSPR_RESOURCE
:
1052 TRACE("unhandled_rtype(%#x)", reg
->type
);
1056 if (reg
->type
== WINED3DSPR_IMMCONST
)
1059 switch (reg
->immconst_type
)
1061 case WINED3D_IMMCONST_SCALAR
:
1062 switch (reg
->data_type
)
1064 case WINED3D_DATA_FLOAT
:
1065 TRACE("%.8e", *(const float *)reg
->immconst_data
);
1067 case WINED3D_DATA_INT
:
1068 TRACE("%d", reg
->immconst_data
[0]);
1070 case WINED3D_DATA_RESOURCE
:
1071 case WINED3D_DATA_SAMPLER
:
1072 case WINED3D_DATA_UINT
:
1073 TRACE("%u", reg
->immconst_data
[0]);
1076 TRACE("<unhandled data type %#x>", reg
->data_type
);
1081 case WINED3D_IMMCONST_VEC4
:
1082 switch (reg
->data_type
)
1084 case WINED3D_DATA_FLOAT
:
1085 TRACE("%.8e, %.8e, %.8e, %.8e",
1086 *(const float *)®
->immconst_data
[0], *(const float *)®
->immconst_data
[1],
1087 *(const float *)®
->immconst_data
[2], *(const float *)®
->immconst_data
[3]);
1089 case WINED3D_DATA_INT
:
1090 TRACE("%d, %d, %d, %d",
1091 reg
->immconst_data
[0], reg
->immconst_data
[1],
1092 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1094 case WINED3D_DATA_RESOURCE
:
1095 case WINED3D_DATA_SAMPLER
:
1096 case WINED3D_DATA_UINT
:
1097 TRACE("%u, %u, %u, %u",
1098 reg
->immconst_data
[0], reg
->immconst_data
[1],
1099 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1102 TRACE("<unhandled data type %#x>", reg
->data_type
);
1108 TRACE("<unhandled immconst_type %#x>", reg
->immconst_type
);
1113 else if (reg
->type
!= WINED3DSPR_RASTOUT
1114 && reg
->type
!= WINED3DSPR_MISCTYPE
1115 && reg
->type
!= WINED3DSPR_NULL
)
1120 if (reg
->idx
[0].rel_addr
)
1122 shader_dump_src_param(reg
->idx
[0].rel_addr
, shader_version
);
1125 TRACE("%u]", offset
);
1127 if (reg
->idx
[1].offset
!= ~0U)
1130 if (reg
->idx
[1].rel_addr
)
1132 shader_dump_src_param(reg
->idx
[1].rel_addr
, shader_version
);
1135 TRACE("%u]", reg
->idx
[1].offset
);
1141 void shader_dump_dst_param(const struct wined3d_shader_dst_param
*param
,
1142 const struct wined3d_shader_version
*shader_version
)
1144 DWORD write_mask
= param
->write_mask
;
1146 shader_dump_register(¶m
->reg
, shader_version
);
1148 if (write_mask
&& write_mask
!= WINED3DSP_WRITEMASK_ALL
)
1150 static const char *write_mask_chars
= "xyzw";
1153 if (write_mask
& WINED3DSP_WRITEMASK_0
) TRACE("%c", write_mask_chars
[0]);
1154 if (write_mask
& WINED3DSP_WRITEMASK_1
) TRACE("%c", write_mask_chars
[1]);
1155 if (write_mask
& WINED3DSP_WRITEMASK_2
) TRACE("%c", write_mask_chars
[2]);
1156 if (write_mask
& WINED3DSP_WRITEMASK_3
) TRACE("%c", write_mask_chars
[3]);
1160 void shader_dump_src_param(const struct wined3d_shader_src_param
*param
,
1161 const struct wined3d_shader_version
*shader_version
)
1163 enum wined3d_shader_src_modifier src_modifier
= param
->modifiers
;
1164 DWORD swizzle
= param
->swizzle
;
1166 if (src_modifier
== WINED3DSPSM_NEG
1167 || src_modifier
== WINED3DSPSM_BIASNEG
1168 || src_modifier
== WINED3DSPSM_SIGNNEG
1169 || src_modifier
== WINED3DSPSM_X2NEG
1170 || src_modifier
== WINED3DSPSM_ABSNEG
)
1172 else if (src_modifier
== WINED3DSPSM_COMP
)
1174 else if (src_modifier
== WINED3DSPSM_NOT
)
1177 if (src_modifier
== WINED3DSPSM_ABS
|| src_modifier
== WINED3DSPSM_ABSNEG
)
1180 shader_dump_register(¶m
->reg
, shader_version
);
1184 switch (src_modifier
)
1186 case WINED3DSPSM_NONE
: break;
1187 case WINED3DSPSM_NEG
: break;
1188 case WINED3DSPSM_NOT
: break;
1189 case WINED3DSPSM_BIAS
: TRACE("_bias"); break;
1190 case WINED3DSPSM_BIASNEG
: TRACE("_bias"); break;
1191 case WINED3DSPSM_SIGN
: TRACE("_bx2"); break;
1192 case WINED3DSPSM_SIGNNEG
: TRACE("_bx2"); break;
1193 case WINED3DSPSM_COMP
: break;
1194 case WINED3DSPSM_X2
: TRACE("_x2"); break;
1195 case WINED3DSPSM_X2NEG
: TRACE("_x2"); break;
1196 case WINED3DSPSM_DZ
: TRACE("_dz"); break;
1197 case WINED3DSPSM_DW
: TRACE("_dw"); break;
1198 case WINED3DSPSM_ABSNEG
: TRACE(")"); break;
1199 case WINED3DSPSM_ABS
: TRACE(")"); break;
1200 default: TRACE("_unknown_modifier(%#x)", src_modifier
);
1204 if (swizzle
!= WINED3DSP_NOSWIZZLE
)
1206 static const char *swizzle_chars
= "xyzw";
1207 DWORD swizzle_x
= swizzle
& 0x03;
1208 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
1209 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
1210 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
1212 if (swizzle_x
== swizzle_y
1213 && swizzle_x
== swizzle_z
1214 && swizzle_x
== swizzle_w
)
1216 TRACE(".%c", swizzle_chars
[swizzle_x
]);
1220 TRACE(".%c%c%c%c", swizzle_chars
[swizzle_x
], swizzle_chars
[swizzle_y
],
1221 swizzle_chars
[swizzle_z
], swizzle_chars
[swizzle_w
]);
1226 /* Shared code in order to generate the bulk of the shader string.
1227 * NOTE: A description of how to parse tokens can be found on MSDN. */
1228 void shader_generate_main(const struct wined3d_shader
*shader
, struct wined3d_shader_buffer
*buffer
,
1229 const struct wined3d_shader_reg_maps
*reg_maps
, const DWORD
*byte_code
, void *backend_ctx
)
1231 struct wined3d_device
*device
= shader
->device
;
1232 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
1233 void *fe_data
= shader
->frontend_data
;
1234 struct wined3d_shader_version shader_version
;
1235 struct wined3d_shader_loop_state loop_state
;
1236 struct wined3d_shader_instruction ins
;
1237 struct wined3d_shader_tex_mx tex_mx
;
1238 struct wined3d_shader_context ctx
;
1239 const DWORD
*ptr
= byte_code
;
1241 /* Initialize current parsing state. */
1242 tex_mx
.current_row
= 0;
1243 loop_state
.current_depth
= 0;
1244 loop_state
.current_reg
= 0;
1246 ctx
.shader
= shader
;
1247 ctx
.gl_info
= &device
->adapter
->gl_info
;
1248 ctx
.reg_maps
= reg_maps
;
1249 ctx
.buffer
= buffer
;
1250 ctx
.tex_mx
= &tex_mx
;
1251 ctx
.loop_state
= &loop_state
;
1252 ctx
.backend_data
= backend_ctx
;
1255 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1257 while (!fe
->shader_is_end(fe_data
, &ptr
))
1260 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1262 /* Unknown opcode and its parameters. */
1263 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1265 TRACE("Skipping unrecognized instruction.\n");
1270 FIXME("Predicates not implemented.\n");
1272 /* Call appropriate function for output target */
1273 device
->shader_backend
->shader_handle_instruction(&ins
);
1277 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param
*dst
)
1279 DWORD mmask
= dst
->modifiers
;
1284 case 13: TRACE("_d8"); break;
1285 case 14: TRACE("_d4"); break;
1286 case 15: TRACE("_d2"); break;
1287 case 1: TRACE("_x2"); break;
1288 case 2: TRACE("_x4"); break;
1289 case 3: TRACE("_x8"); break;
1290 default: TRACE("_unhandled_shift(%d)", dst
->shift
); break;
1293 if (mmask
& WINED3DSPDM_SATURATE
) TRACE("_sat");
1294 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) TRACE("_pp");
1295 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) TRACE("_centroid");
1297 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
1298 if (mmask
) FIXME("_unrecognized_modifier(%#x)", mmask
);
1301 static void shader_dump_primitive_type(enum wined3d_primitive_type primitive_type
)
1303 switch (primitive_type
)
1305 case WINED3D_PT_UNDEFINED
:
1308 case WINED3D_PT_POINTLIST
:
1311 case WINED3D_PT_LINELIST
:
1314 case WINED3D_PT_LINESTRIP
:
1317 case WINED3D_PT_TRIANGLELIST
:
1318 TRACE("trianglelist");
1320 case WINED3D_PT_TRIANGLESTRIP
:
1321 TRACE("trianglestrip");
1323 case WINED3D_PT_TRIANGLEFAN
:
1324 TRACE("trianglefan");
1326 case WINED3D_PT_LINELIST_ADJ
:
1327 TRACE("linelist_adj");
1329 case WINED3D_PT_LINESTRIP_ADJ
:
1330 TRACE("linestrip_adj");
1332 case WINED3D_PT_TRIANGLELIST_ADJ
:
1333 TRACE("trianglelist_adj");
1335 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
1336 TRACE("trianglestrip_adj");
1339 TRACE("<unrecognized_primitive_type %#x>", primitive_type
);
1344 static void shader_trace_init(const struct wined3d_shader_frontend
*fe
, void *fe_data
, const DWORD
*byte_code
)
1346 struct wined3d_shader_version shader_version
;
1347 const DWORD
*ptr
= byte_code
;
1348 const char *type_prefix
;
1351 TRACE("Parsing %p.\n", byte_code
);
1353 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1355 switch (shader_version
.type
)
1357 case WINED3D_SHADER_TYPE_VERTEX
:
1361 case WINED3D_SHADER_TYPE_GEOMETRY
:
1365 case WINED3D_SHADER_TYPE_PIXEL
:
1370 FIXME("Unhandled shader type %#x.\n", shader_version
.type
);
1371 type_prefix
= "unknown";
1375 TRACE("%s_%u_%u\n", type_prefix
, shader_version
.major
, shader_version
.minor
);
1377 while (!fe
->shader_is_end(fe_data
, &ptr
))
1379 struct wined3d_shader_instruction ins
;
1381 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1382 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1384 TRACE("Skipping unrecognized instruction.\n");
1388 if (ins
.handler_idx
== WINED3DSIH_DCL
)
1390 shader_dump_decl_usage(&ins
.declaration
.semantic
, &shader_version
);
1391 shader_dump_ins_modifiers(&ins
.declaration
.semantic
.reg
);
1393 shader_dump_dst_param(&ins
.declaration
.semantic
.reg
, &shader_version
);
1395 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
1397 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1398 shader_dump_src_param(&ins
.declaration
.src
, &shader_version
);
1399 TRACE(", %s", ins
.flags
& WINED3DSI_INDEXED_DYNAMIC
? "dynamicIndexed" : "immediateIndexed");
1401 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
1402 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
1404 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1405 shader_dump_primitive_type(ins
.declaration
.primitive_type
);
1407 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
1409 TRACE("%s %u", shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.count
);
1411 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
1413 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(ins
.dst
[0].reg
.type
,
1414 ins
.dst
[0].reg
.idx
[0].offset
),
1415 *(const float *)&ins
.src
[0].reg
.immconst_data
[0],
1416 *(const float *)&ins
.src
[0].reg
.immconst_data
[1],
1417 *(const float *)&ins
.src
[0].reg
.immconst_data
[2],
1418 *(const float *)&ins
.src
[0].reg
.immconst_data
[3]);
1420 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
1422 TRACE("defi i%u = %d, %d, %d, %d", ins
.dst
[0].reg
.idx
[0].offset
,
1423 ins
.src
[0].reg
.immconst_data
[0],
1424 ins
.src
[0].reg
.immconst_data
[1],
1425 ins
.src
[0].reg
.immconst_data
[2],
1426 ins
.src
[0].reg
.immconst_data
[3]);
1428 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
1430 TRACE("defb b%u = %s", ins
.dst
[0].reg
.idx
[0].offset
, ins
.src
[0].reg
.immconst_data
[0] ? "true" : "false");
1437 shader_dump_src_param(ins
.predicate
, &shader_version
);
1441 /* PixWin marks instructions with the coissue flag with a '+' */
1442 if (ins
.coissue
) TRACE("+");
1444 TRACE("%s", shader_opcode_names
[ins
.handler_idx
]);
1446 if (ins
.handler_idx
== WINED3DSIH_IFC
1447 || ins
.handler_idx
== WINED3DSIH_BREAKC
)
1451 case WINED3D_SHADER_REL_OP_GT
: TRACE("_gt"); break;
1452 case WINED3D_SHADER_REL_OP_EQ
: TRACE("_eq"); break;
1453 case WINED3D_SHADER_REL_OP_GE
: TRACE("_ge"); break;
1454 case WINED3D_SHADER_REL_OP_LT
: TRACE("_lt"); break;
1455 case WINED3D_SHADER_REL_OP_NE
: TRACE("_ne"); break;
1456 case WINED3D_SHADER_REL_OP_LE
: TRACE("_le"); break;
1457 default: TRACE("_(%u)", ins
.flags
);
1460 else if (ins
.handler_idx
== WINED3DSIH_TEX
1461 && shader_version
.major
>= 2
1462 && (ins
.flags
& WINED3DSI_TEXLD_PROJECT
))
1467 for (i
= 0; i
< ins
.dst_count
; ++i
)
1469 shader_dump_ins_modifiers(&ins
.dst
[i
]);
1470 TRACE(!i
? " " : ", ");
1471 shader_dump_dst_param(&ins
.dst
[i
], &shader_version
);
1474 /* Other source tokens */
1475 for (i
= ins
.dst_count
; i
< (ins
.dst_count
+ ins
.src_count
); ++i
)
1477 TRACE(!i
? " " : ", ");
1478 shader_dump_src_param(&ins
.src
[i
- ins
.dst_count
], &shader_version
);
1485 static void shader_cleanup(struct wined3d_shader
*shader
)
1487 shader
->device
->shader_backend
->shader_destroy(shader
);
1488 HeapFree(GetProcessHeap(), 0, shader
->reg_maps
.constf
);
1489 HeapFree(GetProcessHeap(), 0, shader
->function
);
1490 shader_delete_constant_list(&shader
->constantsF
);
1491 shader_delete_constant_list(&shader
->constantsB
);
1492 shader_delete_constant_list(&shader
->constantsI
);
1493 list_remove(&shader
->shader_list_entry
);
1495 if (shader
->frontend
&& shader
->frontend_data
)
1496 shader
->frontend
->shader_free(shader
->frontend_data
);
1499 struct shader_none_priv
1501 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
1502 const struct fragment_pipeline
*fragment_pipe
;
1503 BOOL ffp_proj_control
;
1506 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
1507 static void shader_none_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
1508 enum tex_types tex_type
, const SIZE
*ds_mask_size
) {}
1509 static void shader_none_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
) {}
1510 static void shader_none_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1511 static void shader_none_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1512 static void shader_none_load_constants(const struct wined3d_context
*context
, BOOL usePS
, BOOL useVS
) {}
1513 static void shader_none_load_np2fixup_constants(void *shader_priv
,
1514 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
) {}
1515 static void shader_none_destroy(struct wined3d_shader
*shader
) {}
1516 static void shader_none_context_destroyed(void *shader_priv
, const struct wined3d_context
*context
) {}
1518 static void shader_none_select(const struct wined3d_context
*context
, enum wined3d_shader_mode vertex_mode
,
1519 enum wined3d_shader_mode fragment_mode
)
1521 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1522 struct wined3d_device
*device
= context
->swapchain
->device
;
1523 struct shader_none_priv
*priv
= device
->shader_priv
;
1525 priv
->vertex_pipe
->vp_enable(gl_info
, vertex_mode
== WINED3D_SHADER_MODE_FFP
);
1526 priv
->fragment_pipe
->enable_extension(gl_info
, fragment_mode
== WINED3D_SHADER_MODE_FFP
);
1529 static HRESULT
shader_none_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
1530 const struct fragment_pipeline
*fragment_pipe
)
1532 struct fragment_caps fragment_caps
;
1533 void *vertex_priv
, *fragment_priv
;
1534 struct shader_none_priv
*priv
;
1536 if (!(priv
= HeapAlloc(GetProcessHeap(), 0, sizeof(*priv
))))
1537 return E_OUTOFMEMORY
;
1539 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&none_shader_backend
, priv
)))
1541 ERR("Failed to initialize vertex pipe.\n");
1542 HeapFree(GetProcessHeap(), 0, priv
);
1546 if (!(fragment_priv
= fragment_pipe
->alloc_private(&none_shader_backend
, priv
)))
1548 ERR("Failed to initialize fragment pipe.\n");
1549 vertex_pipe
->vp_free(device
);
1550 HeapFree(GetProcessHeap(), 0, priv
);
1554 priv
->vertex_pipe
= vertex_pipe
;
1555 priv
->fragment_pipe
= fragment_pipe
;
1556 fragment_pipe
->get_caps(&device
->adapter
->gl_info
, &fragment_caps
);
1557 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
1559 device
->vertex_priv
= vertex_priv
;
1560 device
->fragment_priv
= fragment_priv
;
1561 device
->shader_priv
= priv
;
1566 static void shader_none_free(struct wined3d_device
*device
)
1568 struct shader_none_priv
*priv
= device
->shader_priv
;
1570 priv
->fragment_pipe
->free_private(device
);
1571 priv
->vertex_pipe
->vp_free(device
);
1572 HeapFree(GetProcessHeap(), 0, priv
);
1575 static void shader_none_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
1577 /* Set the shader caps to 0 for the none shader backend */
1578 caps
->vs_version
= 0;
1579 caps
->gs_version
= 0;
1580 caps
->ps_version
= 0;
1581 caps
->vs_uniform_count
= 0;
1582 caps
->ps_uniform_count
= 0;
1583 caps
->ps_1x_max_value
= 0.0f
;
1584 caps
->wined3d_caps
= 0;
1587 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
1589 /* We "support" every possible fixup, since we don't support any shader
1590 * model, and will never have to actually sample a texture. */
1594 static BOOL
shader_none_has_ffp_proj_control(void *shader_priv
)
1596 struct shader_none_priv
*priv
= shader_priv
;
1598 return priv
->ffp_proj_control
;
1601 const struct wined3d_shader_backend_ops none_shader_backend
=
1603 shader_none_handle_instruction
,
1605 shader_none_select_depth_blt
,
1606 shader_none_deselect_depth_blt
,
1607 shader_none_update_float_vertex_constants
,
1608 shader_none_update_float_pixel_constants
,
1609 shader_none_load_constants
,
1610 shader_none_load_np2fixup_constants
,
1611 shader_none_destroy
,
1614 shader_none_context_destroyed
,
1615 shader_none_get_caps
,
1616 shader_none_color_fixup_supported
,
1617 shader_none_has_ffp_proj_control
,
1620 static HRESULT
shader_set_function(struct wined3d_shader
*shader
, const DWORD
*byte_code
,
1621 const struct wined3d_shader_signature
*output_signature
, DWORD float_const_count
,
1622 enum wined3d_shader_type type
, unsigned int max_version
)
1624 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1625 const struct wined3d_shader_frontend
*fe
;
1627 unsigned int backend_version
;
1629 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1630 shader
, byte_code
, output_signature
, float_const_count
);
1632 fe
= shader_select_frontend(*byte_code
);
1635 FIXME("Unable to find frontend for shader.\n");
1636 return WINED3DERR_INVALIDCALL
;
1638 shader
->frontend
= fe
;
1639 shader
->frontend_data
= fe
->shader_init(byte_code
, output_signature
);
1640 if (!shader
->frontend_data
)
1642 FIXME("Failed to initialize frontend.\n");
1643 return WINED3DERR_INVALIDCALL
;
1646 /* First pass: trace shader. */
1647 if (TRACE_ON(d3d_shader
))
1648 shader_trace_init(fe
, shader
->frontend_data
, byte_code
);
1650 /* Initialize immediate constant lists. */
1651 list_init(&shader
->constantsF
);
1652 list_init(&shader
->constantsB
);
1653 list_init(&shader
->constantsI
);
1654 shader
->lconst_inf_or_nan
= FALSE
;
1656 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1657 hr
= shader_get_registers_used(shader
, fe
,
1658 reg_maps
, shader
->input_signature
, shader
->output_signature
,
1659 byte_code
, float_const_count
);
1660 if (FAILED(hr
)) return hr
;
1662 if (reg_maps
->shader_version
.type
!= type
)
1664 WARN("Wrong shader type %d.\n", reg_maps
->shader_version
.type
);
1665 return WINED3DERR_INVALIDCALL
;
1667 if (reg_maps
->shader_version
.major
> max_version
)
1669 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps
->shader_version
.major
);
1670 return WINED3DERR_INVALIDCALL
;
1674 case WINED3D_SHADER_TYPE_VERTEX
:
1675 backend_version
= shader
->device
->vs_version
;
1677 case WINED3D_SHADER_TYPE_GEOMETRY
:
1678 backend_version
= shader
->device
->gs_version
;
1680 case WINED3D_SHADER_TYPE_PIXEL
:
1681 backend_version
= shader
->device
->ps_version
;
1684 FIXME("No backend version-checking for this shader type\n");
1685 backend_version
= 0;
1687 if (reg_maps
->shader_version
.major
> backend_version
)
1689 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
1690 reg_maps
->shader_version
.major
, reg_maps
->shader_version
.minor
);
1691 return WINED3DERR_INVALIDCALL
;
1694 shader
->function
= HeapAlloc(GetProcessHeap(), 0, shader
->functionLength
);
1695 if (!shader
->function
)
1696 return E_OUTOFMEMORY
;
1697 memcpy(shader
->function
, byte_code
, shader
->functionLength
);
1702 ULONG CDECL
wined3d_shader_incref(struct wined3d_shader
*shader
)
1704 ULONG refcount
= InterlockedIncrement(&shader
->ref
);
1706 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
1711 /* Do not call while under the GL lock. */
1712 ULONG CDECL
wined3d_shader_decref(struct wined3d_shader
*shader
)
1714 ULONG refcount
= InterlockedDecrement(&shader
->ref
);
1716 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
1720 shader_cleanup(shader
);
1721 shader
->parent_ops
->wined3d_object_destroyed(shader
->parent
);
1722 HeapFree(GetProcessHeap(), 0, shader
);
1728 void * CDECL
wined3d_shader_get_parent(const struct wined3d_shader
*shader
)
1730 TRACE("shader %p.\n", shader
);
1732 return shader
->parent
;
1735 HRESULT CDECL
wined3d_shader_get_byte_code(const struct wined3d_shader
*shader
,
1736 void *byte_code
, UINT
*byte_code_size
)
1738 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader
, byte_code
, byte_code_size
);
1742 *byte_code_size
= shader
->functionLength
;
1746 if (*byte_code_size
< shader
->functionLength
)
1748 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
1749 * than the required size we should write the required size and
1750 * return D3DERR_MOREDATA. That's not actually true. */
1751 return WINED3DERR_INVALIDCALL
;
1754 memcpy(byte_code
, shader
->function
, shader
->functionLength
);
1759 /* Set local constants for d3d8 shaders. */
1760 HRESULT CDECL
wined3d_shader_set_local_constants_float(struct wined3d_shader
*shader
,
1761 UINT start_idx
, const float *src_data
, UINT count
)
1763 UINT end_idx
= start_idx
+ count
;
1766 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader
, start_idx
, src_data
, count
);
1768 if (end_idx
> shader
->limits
.constant_float
)
1770 WARN("end_idx %u > float constants limit %u.\n",
1771 end_idx
, shader
->limits
.constant_float
);
1772 end_idx
= shader
->limits
.constant_float
;
1775 for (i
= start_idx
; i
< end_idx
; ++i
)
1777 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
1780 return E_OUTOFMEMORY
;
1783 value
= (float *)lconst
->value
;
1784 memcpy(value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
1785 list_add_head(&shader
->constantsF
, &lconst
->entry
);
1787 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
1788 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
1790 shader
->lconst_inf_or_nan
= TRUE
;
1797 void find_vs_compile_args(const struct wined3d_state
*state
,
1798 const struct wined3d_shader
*shader
, struct vs_compile_args
*args
)
1800 args
->fog_src
= state
->render_states
[WINED3D_RS_FOGTABLEMODE
]
1801 == WINED3D_FOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
1802 args
->clip_enabled
= state
->render_states
[WINED3D_RS_CLIPPING
]
1803 && state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
];
1804 args
->swizzle_map
= shader
->device
->stream_info
.swizzle_map
;
1807 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
1809 if (usage_idx1
!= usage_idx2
)
1811 if (usage1
== usage2
)
1813 if (usage1
== WINED3D_DECL_USAGE_POSITION
&& usage2
== WINED3D_DECL_USAGE_POSITIONT
)
1815 if (usage2
== WINED3D_DECL_USAGE_POSITION
&& usage1
== WINED3D_DECL_USAGE_POSITIONT
)
1821 BOOL
vshader_get_input(const struct wined3d_shader
*shader
,
1822 BYTE usage_req
, BYTE usage_idx_req
, unsigned int *regnum
)
1824 WORD map
= shader
->reg_maps
.input_registers
;
1827 for (i
= 0; map
; map
>>= 1, ++i
)
1829 if (!(map
& 1)) continue;
1831 if (match_usage(shader
->u
.vs
.attributes
[i
].usage
,
1832 shader
->u
.vs
.attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
1841 static void vertexshader_set_limits(struct wined3d_shader
*shader
)
1843 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
1844 shader
->reg_maps
.shader_version
.minor
);
1845 struct wined3d_device
*device
= shader
->device
;
1847 shader
->limits
.packed_input
= 0;
1849 switch (shader_version
)
1851 case WINED3D_SHADER_VERSION(1, 0):
1852 case WINED3D_SHADER_VERSION(1, 1):
1853 shader
->limits
.constant_bool
= 0;
1854 shader
->limits
.constant_int
= 0;
1855 shader
->limits
.packed_output
= 12;
1856 shader
->limits
.sampler
= 0;
1857 /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1858 * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1860 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1863 case WINED3D_SHADER_VERSION(2, 0):
1864 case WINED3D_SHADER_VERSION(2, 1):
1865 shader
->limits
.constant_bool
= 16;
1866 shader
->limits
.constant_int
= 16;
1867 shader
->limits
.packed_output
= 12;
1868 shader
->limits
.sampler
= 0;
1869 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1872 case WINED3D_SHADER_VERSION(3, 0):
1873 shader
->limits
.constant_bool
= 16;
1874 shader
->limits
.constant_int
= 16;
1875 shader
->limits
.packed_output
= 12;
1876 shader
->limits
.sampler
= 4;
1877 /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1878 * even though they are capable of supporting much more (GL
1879 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1880 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1881 * shaders to 256. */
1882 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1885 case WINED3D_SHADER_VERSION(4, 0):
1886 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
1887 shader
->limits
.constant_int
= 0;
1888 shader
->limits
.constant_float
= 0;
1889 shader
->limits
.constant_bool
= 0;
1890 shader
->limits
.packed_output
= 16;
1891 shader
->limits
.packed_input
= 0;
1895 shader
->limits
.constant_bool
= 16;
1896 shader
->limits
.constant_int
= 16;
1897 shader
->limits
.packed_output
= 12;
1898 shader
->limits
.sampler
= 0;
1899 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1900 FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1901 shader
->reg_maps
.shader_version
.major
,
1902 shader
->reg_maps
.shader_version
.minor
);
1906 static HRESULT
vertexshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
1907 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
1908 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
1910 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1915 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
1917 shader_init(shader
, device
, parent
, parent_ops
);
1918 hr
= shader_set_function(shader
, byte_code
, output_signature
, device
->d3d_vshader_constantF
,
1919 WINED3D_SHADER_TYPE_VERTEX
, max_version
);
1922 WARN("Failed to set function, hr %#x.\n", hr
);
1923 shader_cleanup(shader
);
1927 map
= reg_maps
->input_registers
;
1928 for (i
= 0; map
; map
>>= 1, ++i
)
1930 if (!(map
& 1) || !shader
->input_signature
[i
].semantic_name
)
1933 shader
->u
.vs
.attributes
[i
].usage
=
1934 shader_usage_from_semantic_name(shader
->input_signature
[i
].semantic_name
);
1935 shader
->u
.vs
.attributes
[i
].usage_idx
= shader
->input_signature
[i
].semantic_idx
;
1938 if (output_signature
)
1940 for (i
= 0; i
< output_signature
->element_count
; ++i
)
1942 struct wined3d_shader_signature_element
*e
= &output_signature
->elements
[i
];
1943 reg_maps
->output_registers
|= 1 << e
->register_idx
;
1944 shader
->output_signature
[e
->register_idx
] = *e
;
1948 vertexshader_set_limits(shader
);
1950 shader
->load_local_constsF
= (reg_maps
->usesrelconstF
&& !list_empty(&shader
->constantsF
)) ||
1951 shader
->lconst_inf_or_nan
;
1956 static void geometryshader_set_limits(struct wined3d_shader
*shader
)
1958 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
1959 shader
->reg_maps
.shader_version
.minor
);
1961 switch (shader_version
)
1963 case WINED3D_SHADER_VERSION(4, 0):
1964 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
1965 shader
->limits
.constant_int
= 0;
1966 shader
->limits
.constant_float
= 0;
1967 shader
->limits
.constant_bool
= 0;
1968 shader
->limits
.packed_output
= 32;
1969 shader
->limits
.packed_input
= 16;
1973 memset(&shader
->limits
, 0, sizeof(shader
->limits
));
1974 FIXME("Unhandled geometry shader version \"%u.%u\".\n",
1975 shader
->reg_maps
.shader_version
.major
,
1976 shader
->reg_maps
.shader_version
.minor
);
1980 static HRESULT
geometryshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
1981 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
1982 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
1986 shader_init(shader
, device
, parent
, parent_ops
);
1987 hr
= shader_set_function(shader
, byte_code
, output_signature
, 0,
1988 WINED3D_SHADER_TYPE_GEOMETRY
, max_version
);
1991 WARN("Failed to set function, hr %#x.\n", hr
);
1992 shader_cleanup(shader
);
1996 geometryshader_set_limits(shader
);
1998 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2003 void find_ps_compile_args(const struct wined3d_state
*state
,
2004 const struct wined3d_shader
*shader
, struct ps_compile_args
*args
)
2006 struct wined3d_device
*device
= shader
->device
;
2007 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2008 const struct wined3d_texture
*texture
;
2011 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
2012 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && state
->render_states
[WINED3D_RS_SRGBWRITEENABLE
])
2014 const struct wined3d_surface
*rt
= state
->fb
->render_targets
[0];
2015 if (rt
->resource
.format
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
2017 static unsigned int warned
= 0;
2019 args
->srgb_correction
= 1;
2020 if (state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] && !warned
++)
2021 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
2022 "support, expect rendering artifacts.\n");
2026 if (shader
->reg_maps
.shader_version
.major
== 1
2027 && shader
->reg_maps
.shader_version
.minor
<= 3)
2029 for (i
= 0; i
< shader
->limits
.sampler
; ++i
)
2031 DWORD flags
= state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
];
2033 if (flags
& WINED3D_TTFF_PROJECTED
)
2035 DWORD tex_transform
= flags
& ~WINED3D_TTFF_PROJECTED
;
2037 if (!state
->vertex_shader
)
2040 unsigned int index
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
2041 DWORD max_valid
= WINED3D_TTFF_COUNT4
;
2042 enum wined3d_sampler_texture_type sampler_type
= shader
->reg_maps
.sampler_type
[i
];
2044 for (j
= 0; j
< state
->vertex_declaration
->element_count
; ++j
)
2046 struct wined3d_vertex_declaration_element
*element
=
2047 &state
->vertex_declaration
->elements
[j
];
2049 if (element
->usage
== WINED3D_DECL_USAGE_TEXCOORD
2050 && element
->usage_idx
== index
)
2052 max_valid
= element
->format
->component_count
;
2056 if (!tex_transform
|| tex_transform
> max_valid
)
2058 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2059 tex_transform
, max_valid
);
2060 tex_transform
= max_valid
;
2062 if ((sampler_type
== WINED3DSTT_1D
&& tex_transform
> WINED3D_TTFF_COUNT1
)
2063 || (sampler_type
== WINED3DSTT_2D
&& tex_transform
> WINED3D_TTFF_COUNT2
)
2064 || (sampler_type
== WINED3DSTT_VOLUME
&& tex_transform
> WINED3D_TTFF_COUNT3
))
2065 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
2068 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2069 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2070 i
, tex_transform
, sampler_type
);
2074 tex_transform
= WINED3D_TTFF_COUNT4
| WINED3D_PSARGS_PROJECTED
;
2076 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
2080 if (shader
->reg_maps
.shader_version
.major
== 1
2081 && shader
->reg_maps
.shader_version
.minor
<= 4)
2083 for (i
= 0; i
< shader
->limits
.sampler
; ++i
)
2085 const struct wined3d_texture
*texture
= state
->textures
[i
];
2087 if (!shader
->reg_maps
.sampler_type
[i
])
2090 /* Treat unbound textures as 2D. The dummy texture will provide
2091 * the proper sample value. The tex_types bitmap defaults to
2092 * 2D because of the memset. */
2096 switch (texture
->target
)
2098 /* RECT textures are distinguished from 2D textures via np2_fixup */
2099 case GL_TEXTURE_RECTANGLE_ARB
:
2104 args
->tex_types
|= WINED3D_SHADER_TEX_3D
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
2107 case GL_TEXTURE_CUBE_MAP_ARB
:
2108 args
->tex_types
|= WINED3D_SHADER_TEX_CUBE
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
2114 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2116 if (!shader
->reg_maps
.sampler_type
[i
])
2119 texture
= state
->textures
[i
];
2122 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
2125 args
->color_fixup
[i
] = texture
->resource
.format
->color_fixup
;
2127 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
2128 args
->shadow
|= 1 << i
;
2130 /* Flag samplers that need NP2 texcoord fixup. */
2131 if (!(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
2132 args
->np2_fixup
|= (1 << i
);
2134 if (shader
->reg_maps
.shader_version
.major
>= 3)
2136 if (device
->stream_info
.position_transformed
)
2137 args
->vp_mode
= pretransformed
;
2138 else if (use_vs(state
))
2139 args
->vp_mode
= vertexshader
;
2141 args
->vp_mode
= fixedfunction
;
2142 args
->fog
= FOG_OFF
;
2146 args
->vp_mode
= vertexshader
;
2147 if (state
->render_states
[WINED3D_RS_FOGENABLE
])
2149 switch (state
->render_states
[WINED3D_RS_FOGTABLEMODE
])
2151 case WINED3D_FOG_NONE
:
2152 if (device
->stream_info
.position_transformed
|| use_vs(state
))
2154 args
->fog
= FOG_LINEAR
;
2158 switch (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
])
2160 case WINED3D_FOG_NONE
: /* Fall through. */
2161 case WINED3D_FOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
2162 case WINED3D_FOG_EXP
: args
->fog
= FOG_EXP
; break;
2163 case WINED3D_FOG_EXP2
: args
->fog
= FOG_EXP2
; break;
2167 case WINED3D_FOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
2168 case WINED3D_FOG_EXP
: args
->fog
= FOG_EXP
; break;
2169 case WINED3D_FOG_EXP2
: args
->fog
= FOG_EXP2
; break;
2174 args
->fog
= FOG_OFF
;
2179 static void pixelshader_set_limits(struct wined3d_shader
*shader
)
2181 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
2182 shader
->reg_maps
.shader_version
.minor
);
2184 shader
->limits
.packed_output
= 0;
2186 switch (shader_version
)
2188 case WINED3D_SHADER_VERSION(1, 0):
2189 case WINED3D_SHADER_VERSION(1, 1):
2190 case WINED3D_SHADER_VERSION(1, 2):
2191 case WINED3D_SHADER_VERSION(1, 3):
2192 shader
->limits
.constant_float
= 8;
2193 shader
->limits
.constant_int
= 0;
2194 shader
->limits
.constant_bool
= 0;
2195 shader
->limits
.sampler
= 4;
2196 shader
->limits
.packed_input
= 0;
2199 case WINED3D_SHADER_VERSION(1, 4):
2200 shader
->limits
.constant_float
= 8;
2201 shader
->limits
.constant_int
= 0;
2202 shader
->limits
.constant_bool
= 0;
2203 shader
->limits
.sampler
= 6;
2204 shader
->limits
.packed_input
= 0;
2207 /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2208 case WINED3D_SHADER_VERSION(2, 0):
2209 shader
->limits
.constant_float
= 32;
2210 shader
->limits
.constant_int
= 16;
2211 shader
->limits
.constant_bool
= 16;
2212 shader
->limits
.sampler
= 16;
2213 shader
->limits
.packed_input
= 0;
2216 case WINED3D_SHADER_VERSION(2, 1):
2217 shader
->limits
.constant_float
= 32;
2218 shader
->limits
.constant_int
= 16;
2219 shader
->limits
.constant_bool
= 16;
2220 shader
->limits
.sampler
= 16;
2221 shader
->limits
.packed_input
= 0;
2224 case WINED3D_SHADER_VERSION(3, 0):
2225 shader
->limits
.constant_float
= 224;
2226 shader
->limits
.constant_int
= 16;
2227 shader
->limits
.constant_bool
= 16;
2228 shader
->limits
.sampler
= 16;
2229 shader
->limits
.packed_input
= 12;
2232 case WINED3D_SHADER_VERSION(4, 0):
2233 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
2234 shader
->limits
.constant_int
= 0;
2235 shader
->limits
.constant_float
= 0;
2236 shader
->limits
.constant_bool
= 0;
2237 shader
->limits
.packed_input
= 32;
2241 shader
->limits
.constant_float
= 32;
2242 shader
->limits
.constant_int
= 16;
2243 shader
->limits
.constant_bool
= 16;
2244 shader
->limits
.sampler
= 16;
2245 shader
->limits
.packed_input
= 0;
2246 FIXME("Unrecognized pixel shader version %u.%u\n",
2247 shader
->reg_maps
.shader_version
.major
,
2248 shader
->reg_maps
.shader_version
.minor
);
2252 static HRESULT
pixelshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2253 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
2254 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
2256 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2257 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
2260 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
2262 shader_init(shader
, device
, parent
, parent_ops
);
2263 hr
= shader_set_function(shader
, byte_code
, output_signature
, device
->d3d_pshader_constantF
,
2264 WINED3D_SHADER_TYPE_PIXEL
, max_version
);
2267 WARN("Failed to set function, hr %#x.\n", hr
);
2268 shader_cleanup(shader
);
2272 pixelshader_set_limits(shader
);
2274 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2276 if (shader
->u
.ps
.input_reg_used
[i
])
2279 highest_reg_used
= i
;
2283 /* Don't do any register mapping magic if it is not needed, or if we can't
2284 * achieve anything anyway */
2285 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
2286 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2288 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2290 /* This happens with relative addressing. The input mapper function
2291 * warns about this if the higher registers are declared too, so
2292 * don't write a FIXME here */
2293 WARN("More varying registers used than supported\n");
2296 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2298 shader
->u
.ps
.input_reg_map
[i
] = i
;
2301 shader
->u
.ps
.declared_in_count
= highest_reg_used
+ 1;
2305 shader
->u
.ps
.declared_in_count
= 0;
2306 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2308 if (shader
->u
.ps
.input_reg_used
[i
])
2309 shader
->u
.ps
.input_reg_map
[i
] = shader
->u
.ps
.declared_in_count
++;
2310 else shader
->u
.ps
.input_reg_map
[i
] = ~0U;
2314 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2319 void pixelshader_update_samplers(struct wined3d_shader
*shader
, WORD tex_types
)
2321 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2322 enum wined3d_sampler_texture_type
*sampler_type
= reg_maps
->sampler_type
;
2325 if (reg_maps
->shader_version
.major
!= 1) return;
2327 for (i
= 0; i
< shader
->limits
.sampler
; ++i
)
2329 /* We don't sample from this sampler. */
2330 if (!sampler_type
[i
]) continue;
2332 switch ((tex_types
>> i
* WINED3D_PSARGS_TEXTYPE_SHIFT
) & WINED3D_PSARGS_TEXTYPE_MASK
)
2334 case WINED3D_SHADER_TEX_2D
:
2335 sampler_type
[i
] = WINED3DSTT_2D
;
2338 case WINED3D_SHADER_TEX_3D
:
2339 sampler_type
[i
] = WINED3DSTT_VOLUME
;
2342 case WINED3D_SHADER_TEX_CUBE
:
2343 sampler_type
[i
] = WINED3DSTT_CUBE
;
2349 HRESULT CDECL
wined3d_shader_create_gs(struct wined3d_device
*device
, const DWORD
*byte_code
,
2350 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2351 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2353 struct wined3d_shader
*object
;
2356 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2357 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2359 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2361 return E_OUTOFMEMORY
;
2363 hr
= geometryshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2366 WARN("Failed to initialize geometry shader, hr %#x.\n", hr
);
2367 HeapFree(GetProcessHeap(), 0, object
);
2371 TRACE("Created geometry shader %p.\n", object
);
2377 HRESULT CDECL
wined3d_shader_create_ps(struct wined3d_device
*device
, const DWORD
*byte_code
,
2378 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2379 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2381 struct wined3d_shader
*object
;
2384 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2385 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2387 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2389 return E_OUTOFMEMORY
;
2391 hr
= pixelshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2394 WARN("Failed to initialize pixel shader, hr %#x.\n", hr
);
2395 HeapFree(GetProcessHeap(), 0, object
);
2399 TRACE("Created pixel shader %p.\n", object
);
2405 HRESULT CDECL
wined3d_shader_create_vs(struct wined3d_device
*device
, const DWORD
*byte_code
,
2406 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2407 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2409 struct wined3d_shader
*object
;
2412 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2413 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2415 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2417 return E_OUTOFMEMORY
;
2419 hr
= vertexshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2422 WARN("Failed to initialize vertex shader, hr %#x.\n", hr
);
2423 HeapFree(GetProcessHeap(), 0, object
);
2427 TRACE("Created vertex shader %p.\n", object
);