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
);
35 WINE_DECLARE_DEBUG_CHANNEL(d3d
);
37 static const char * const shader_opcode_names
[] =
39 /* WINED3DSIH_ABS */ "abs",
40 /* WINED3DSIH_ADD */ "add",
41 /* WINED3DSIH_AND */ "and",
42 /* WINED3DSIH_BEM */ "bem",
43 /* WINED3DSIH_BREAK */ "break",
44 /* WINED3DSIH_BREAKC */ "breakc",
45 /* WINED3DSIH_BREAKP */ "breakp",
46 /* WINED3DSIH_CALL */ "call",
47 /* WINED3DSIH_CALLNZ */ "callnz",
48 /* WINED3DSIH_CMP */ "cmp",
49 /* WINED3DSIH_CND */ "cnd",
50 /* WINED3DSIH_CRS */ "crs",
51 /* WINED3DSIH_CUT */ "cut",
52 /* WINED3DSIH_DCL */ "dcl",
53 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
54 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
55 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
56 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
57 /* WINED3DSIH_DEF */ "def",
58 /* WINED3DSIH_DEFB */ "defb",
59 /* WINED3DSIH_DEFI */ "defi",
60 /* WINED3DSIH_DIV */ "div",
61 /* WINED3DSIH_DP2ADD */ "dp2add",
62 /* WINED3DSIH_DP3 */ "dp3",
63 /* WINED3DSIH_DP4 */ "dp4",
64 /* WINED3DSIH_DST */ "dst",
65 /* WINED3DSIH_DSX */ "dsx",
66 /* WINED3DSIH_DSY */ "dsy",
67 /* WINED3DSIH_ELSE */ "else",
68 /* WINED3DSIH_EMIT */ "emit",
69 /* WINED3DSIH_ENDIF */ "endif",
70 /* WINED3DSIH_ENDLOOP */ "endloop",
71 /* WINED3DSIH_ENDREP */ "endrep",
72 /* WINED3DSIH_EQ */ "eq",
73 /* WINED3DSIH_EXP */ "exp",
74 /* WINED3DSIH_EXPP */ "expp",
75 /* WINED3DSIH_FRC */ "frc",
76 /* WINED3DSIH_FTOI */ "ftoi",
77 /* WINED3DSIH_GE */ "ge",
78 /* WINED3DSIH_IADD */ "iadd",
79 /* WINED3DSIH_IEQ */ "ieq",
80 /* WINED3DSIH_IF */ "if",
81 /* WINED3DSIH_IFC */ "ifc",
82 /* WINED3DSIH_IGE */ "ige",
83 /* WINED3DSIH_IMUL */ "imul",
84 /* WINED3DSIH_ITOF */ "itof",
85 /* WINED3DSIH_LABEL */ "label",
86 /* WINED3DSIH_LD */ "ld",
87 /* WINED3DSIH_LIT */ "lit",
88 /* WINED3DSIH_LOG */ "log",
89 /* WINED3DSIH_LOGP */ "logp",
90 /* WINED3DSIH_LOOP */ "loop",
91 /* WINED3DSIH_LRP */ "lrp",
92 /* WINED3DSIH_LT */ "lt",
93 /* WINED3DSIH_M3x2 */ "m3x2",
94 /* WINED3DSIH_M3x3 */ "m3x3",
95 /* WINED3DSIH_M3x4 */ "m3x4",
96 /* WINED3DSIH_M4x3 */ "m4x3",
97 /* WINED3DSIH_M4x4 */ "m4x4",
98 /* WINED3DSIH_MAD */ "mad",
99 /* WINED3DSIH_MAX */ "max",
100 /* WINED3DSIH_MIN */ "min",
101 /* WINED3DSIH_MOV */ "mov",
102 /* WINED3DSIH_MOVA */ "mova",
103 /* WINED3DSIH_MOVC */ "movc",
104 /* WINED3DSIH_MUL */ "mul",
105 /* WINED3DSIH_NOP */ "nop",
106 /* WINED3DSIH_NRM */ "nrm",
107 /* WINED3DSIH_PHASE */ "phase",
108 /* WINED3DSIH_POW */ "pow",
109 /* WINED3DSIH_RCP */ "rcp",
110 /* WINED3DSIH_REP */ "rep",
111 /* WINED3DSIH_RET */ "ret",
112 /* WINED3DSIH_ROUND_NI */ "round_ni",
113 /* WINED3DSIH_RSQ */ "rsq",
114 /* WINED3DSIH_SAMPLE */ "sample",
115 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
116 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
117 /* WINED3DSIH_SETP */ "setp",
118 /* WINED3DSIH_SGE */ "sge",
119 /* WINED3DSIH_SGN */ "sgn",
120 /* WINED3DSIH_SINCOS */ "sincos",
121 /* WINED3DSIH_SLT */ "slt",
122 /* WINED3DSIH_SQRT */ "sqrt",
123 /* WINED3DSIH_SUB */ "sub",
124 /* WINED3DSIH_TEX */ "texld",
125 /* WINED3DSIH_TEXBEM */ "texbem",
126 /* WINED3DSIH_TEXBEML */ "texbeml",
127 /* WINED3DSIH_TEXCOORD */ "texcrd",
128 /* WINED3DSIH_TEXDEPTH */ "texdepth",
129 /* WINED3DSIH_TEXDP3 */ "texdp3",
130 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
131 /* WINED3DSIH_TEXKILL */ "texkill",
132 /* WINED3DSIH_TEXLDD */ "texldd",
133 /* WINED3DSIH_TEXLDL */ "texldl",
134 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
135 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
136 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
137 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
138 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
139 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
140 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
141 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
142 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
143 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
144 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
145 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
146 /* WINED3DSIH_UDIV */ "udiv",
147 /* WINED3DSIH_USHR */ "ushr",
148 /* WINED3DSIH_UTOF */ "utof",
149 /* WINED3DSIH_XOR */ "xor",
152 static const char * const semantic_names
[] =
154 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
155 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
156 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
157 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
158 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
159 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
160 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
161 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
162 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
163 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
164 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
165 /* WINED3D_DECL_USAGE_FOG */ "FOG",
166 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
167 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
170 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage
)
172 if (usage
>= sizeof(semantic_names
) / sizeof(*semantic_names
))
174 FIXME("Unrecognized usage %#x.\n", usage
);
175 return "UNRECOGNIZED";
178 return semantic_names
[usage
];
181 static enum wined3d_decl_usage
shader_usage_from_semantic_name(const char *name
)
185 for (i
= 0; i
< sizeof(semantic_names
) / sizeof(*semantic_names
); ++i
)
187 if (!strcmp(name
, semantic_names
[i
])) return i
;
193 BOOL
shader_match_semantic(const char *semantic_name
, enum wined3d_decl_usage usage
)
195 return !strcmp(semantic_name
, shader_semantic_name_from_usage(usage
));
198 static void shader_signature_from_semantic(struct wined3d_shader_signature_element
*e
,
199 const struct wined3d_shader_semantic
*s
)
201 e
->semantic_name
= shader_semantic_name_from_usage(s
->usage
);
202 e
->semantic_idx
= s
->usage_idx
;
203 e
->sysval_semantic
= 0;
204 e
->component_type
= 0;
205 e
->register_idx
= s
->reg
.reg
.idx
;
206 e
->mask
= s
->reg
.write_mask
;
209 static void shader_signature_from_usage(struct wined3d_shader_signature_element
*e
,
210 enum wined3d_decl_usage usage
, UINT usage_idx
, UINT reg_idx
, DWORD write_mask
)
212 e
->semantic_name
= shader_semantic_name_from_usage(usage
);
213 e
->semantic_idx
= usage_idx
;
214 e
->sysval_semantic
= 0;
215 e
->component_type
= 0;
216 e
->register_idx
= reg_idx
;
217 e
->mask
= write_mask
;
220 static const struct wined3d_shader_frontend
*shader_select_frontend(DWORD version_token
)
222 switch (version_token
>> 16)
226 return &sm1_shader_frontend
;
231 return &sm4_shader_frontend
;
234 FIXME("Unrecognised version token %#x\n", version_token
);
239 void shader_buffer_clear(struct wined3d_shader_buffer
*buffer
)
241 buffer
->buffer
[0] = '\0';
244 buffer
->newline
= TRUE
;
247 BOOL
shader_buffer_init(struct wined3d_shader_buffer
*buffer
)
249 buffer
->buffer
= HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE
);
252 ERR("Failed to allocate shader buffer memory.\n");
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
->bsize
;
270 rc
= vsnprintf(base
, SHADER_PGMSIZE
- 1 - buffer
->bsize
, format
, args
);
272 if (rc
< 0 /* C89 */ || (unsigned int)rc
> SHADER_PGMSIZE
- 1 - buffer
->bsize
/* C99 */)
274 ERR("The buffer allocated for the shader program string "
275 "is too small at %d bytes.\n", SHADER_PGMSIZE
);
276 buffer
->bsize
= SHADER_PGMSIZE
- 1;
282 TRACE("GL HW (%u, %u) : %s", buffer
->lineNo
+ 1, buffer
->bsize
, base
);
283 buffer
->newline
= FALSE
;
291 if (buffer
->buffer
[buffer
->bsize
-1] == '\n')
294 buffer
->newline
= TRUE
;
300 int shader_addline(struct wined3d_shader_buffer
*buffer
, const char *format
, ...)
305 va_start(args
, format
);
306 ret
= shader_vaddline(buffer
, format
, args
);
312 static void shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
313 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
316 shader
->device
= device
;
317 shader
->parent
= parent
;
318 shader
->parent_ops
= parent_ops
;
319 list_init(&shader
->linked_programs
);
320 list_add_head(&device
->shaders
, &shader
->shader_list_entry
);
323 /* Convert floating point offset relative to a register file to an absolute
324 * offset for float constants. */
325 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type
, UINT register_idx
)
327 switch (register_type
)
329 case WINED3DSPR_CONST
: return register_idx
;
330 case WINED3DSPR_CONST2
: return 2048 + register_idx
;
331 case WINED3DSPR_CONST3
: return 4096 + register_idx
;
332 case WINED3DSPR_CONST4
: return 6144 + register_idx
;
334 FIXME("Unsupported register type: %u.\n", register_type
);
339 static void shader_delete_constant_list(struct list
*clist
)
341 struct wined3d_shader_lconst
*constant
;
344 ptr
= list_head(clist
);
347 constant
= LIST_ENTRY(ptr
, struct wined3d_shader_lconst
, entry
);
348 ptr
= list_next(clist
, ptr
);
349 HeapFree(GetProcessHeap(), 0, constant
);
354 static inline void set_bitmap_bit(DWORD
*bitmap
, DWORD bit
)
359 bitmap
[idx
] |= (1 << shift
);
362 static void shader_record_register_usage(struct wined3d_shader
*shader
, struct wined3d_shader_reg_maps
*reg_maps
,
363 const struct wined3d_shader_register
*reg
, enum wined3d_shader_type shader_type
)
367 case WINED3DSPR_TEXTURE
: /* WINED3DSPR_ADDR */
368 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
) reg_maps
->texcoord
|= 1 << reg
->idx
;
369 else reg_maps
->address
|= 1 << reg
->idx
;
372 case WINED3DSPR_TEMP
:
373 reg_maps
->temporary
|= 1 << reg
->idx
;
376 case WINED3DSPR_INPUT
:
377 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
381 /* If relative addressing is used, we must assume that all registers
382 * are used. Even if it is a construct like v3[aL], we can't assume
383 * that v0, v1 and v2 aren't read because aL can be negative */
385 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
387 shader
->u
.ps
.input_reg_used
[i
] = TRUE
;
392 shader
->u
.ps
.input_reg_used
[reg
->idx
] = TRUE
;
395 else reg_maps
->input_registers
|= 1 << reg
->idx
;
398 case WINED3DSPR_RASTOUT
:
399 if (reg
->idx
== 1) reg_maps
->fog
= 1;
402 case WINED3DSPR_MISCTYPE
:
403 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
405 if (!reg
->idx
) reg_maps
->vpos
= 1;
406 else if (reg
->idx
== 1) reg_maps
->usesfacing
= 1;
410 case WINED3DSPR_CONST
:
413 if (reg
->idx
< reg_maps
->min_rel_offset
) reg_maps
->min_rel_offset
= reg
->idx
;
414 if (reg
->idx
> reg_maps
->max_rel_offset
) reg_maps
->max_rel_offset
= reg
->idx
;
415 reg_maps
->usesrelconstF
= TRUE
;
419 set_bitmap_bit(reg_maps
->constf
, reg
->idx
);
423 case WINED3DSPR_CONSTINT
:
424 reg_maps
->integer_constants
|= (1 << reg
->idx
);
427 case WINED3DSPR_CONSTBOOL
:
428 reg_maps
->boolean_constants
|= (1 << reg
->idx
);
431 case WINED3DSPR_COLOROUT
:
432 reg_maps
->rt_mask
|= (1 << reg
->idx
);
436 TRACE("Not recording register of type %#x and idx %u\n", reg
->type
, reg
->idx
);
441 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr
, unsigned int param
)
445 case WINED3DSIH_M4x4
:
446 case WINED3DSIH_M3x4
:
447 return param
== 1 ? 3 : 0;
449 case WINED3DSIH_M4x3
:
450 case WINED3DSIH_M3x3
:
451 return param
== 1 ? 2 : 0;
453 case WINED3DSIH_M3x2
:
454 return param
== 1 ? 1 : 0;
461 /* Note that this does not count the loop register as an address register. */
462 static HRESULT
shader_get_registers_used(struct wined3d_shader
*shader
, const struct wined3d_shader_frontend
*fe
,
463 struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_shader_signature_element
*input_signature
,
464 struct wined3d_shader_signature_element
*output_signature
, const DWORD
*byte_code
, DWORD constf_size
)
466 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
467 void *fe_data
= shader
->frontend_data
;
468 struct wined3d_shader_version shader_version
;
469 const DWORD
*ptr
= byte_code
;
471 memset(reg_maps
, 0, sizeof(*reg_maps
));
472 reg_maps
->min_rel_offset
= ~0U;
474 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
475 reg_maps
->shader_version
= shader_version
;
477 reg_maps
->constf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
478 sizeof(*reg_maps
->constf
) * ((constf_size
+ 31) / 32));
479 if (!reg_maps
->constf
)
481 ERR("Failed to allocate constant map memory.\n");
482 return E_OUTOFMEMORY
;
485 while (!fe
->shader_is_end(fe_data
, &ptr
))
487 struct wined3d_shader_instruction ins
;
492 fe
->shader_read_comment(&ptr
, &comment
, &comment_size
);
493 if (comment
) continue;
496 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
498 /* Unhandled opcode, and its parameters. */
499 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
501 TRACE("Skipping unrecognized instruction.\n");
505 /* Handle declarations. */
506 if (ins
.handler_idx
== WINED3DSIH_DCL
)
508 struct wined3d_shader_semantic
*semantic
= &ins
.declaration
.semantic
;
510 switch (semantic
->reg
.reg
.type
)
512 /* Mark input registers used. */
513 case WINED3DSPR_INPUT
:
514 reg_maps
->input_registers
|= 1 << semantic
->reg
.reg
.idx
;
515 shader_signature_from_semantic(&input_signature
[semantic
->reg
.reg
.idx
], semantic
);
518 /* Vertex shader: mark 3.0 output registers used, save token. */
519 case WINED3DSPR_OUTPUT
:
520 reg_maps
->output_registers
|= 1 << semantic
->reg
.reg
.idx
;
521 shader_signature_from_semantic(&output_signature
[semantic
->reg
.reg
.idx
], semantic
);
522 if (semantic
->usage
== WINED3D_DECL_USAGE_FOG
)
526 /* Save sampler usage token. */
527 case WINED3DSPR_SAMPLER
:
528 reg_maps
->sampler_type
[semantic
->reg
.reg
.idx
] = semantic
->sampler_type
;
532 TRACE("Not recording DCL register type %#x.\n", semantic
->reg
.reg
.type
);
536 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
538 struct wined3d_shader_register
*reg
= &ins
.declaration
.src
.reg
;
539 if (reg
->idx
>= WINED3D_MAX_CBS
)
540 ERR("Invalid CB index %u.\n", reg
->idx
);
542 reg_maps
->cb_sizes
[reg
->idx
] = reg
->array_idx
;
544 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
546 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
547 if (!lconst
) return E_OUTOFMEMORY
;
549 lconst
->idx
= ins
.dst
[0].reg
.idx
;
550 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
552 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
553 if (shader_version
.major
== 1 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
555 float *value
= (float *)lconst
->value
;
556 if (value
[0] < -1.0f
) value
[0] = -1.0f
;
557 else if (value
[0] > 1.0f
) value
[0] = 1.0f
;
558 if (value
[1] < -1.0f
) value
[1] = -1.0f
;
559 else if (value
[1] > 1.0f
) value
[1] = 1.0f
;
560 if (value
[2] < -1.0f
) value
[2] = -1.0f
;
561 else if (value
[2] > 1.0f
) value
[2] = 1.0f
;
562 if (value
[3] < -1.0f
) value
[3] = -1.0f
;
563 else if (value
[3] > 1.0f
) value
[3] = 1.0f
;
566 list_add_head(&shader
->constantsF
, &lconst
->entry
);
568 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
570 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
571 if (!lconst
) return E_OUTOFMEMORY
;
573 lconst
->idx
= ins
.dst
[0].reg
.idx
;
574 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
576 list_add_head(&shader
->constantsI
, &lconst
->entry
);
577 reg_maps
->local_int_consts
|= (1 << lconst
->idx
);
579 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
581 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
582 if (!lconst
) return E_OUTOFMEMORY
;
584 lconst
->idx
= ins
.dst
[0].reg
.idx
;
585 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, sizeof(DWORD
));
587 list_add_head(&shader
->constantsB
, &lconst
->entry
);
588 reg_maps
->local_bool_consts
|= (1 << lconst
->idx
);
590 /* For subroutine prototypes. */
591 else if (ins
.handler_idx
== WINED3DSIH_LABEL
)
593 reg_maps
->labels
|= 1 << ins
.src
[0].reg
.idx
;
595 /* Set texture, address, temporary registers. */
598 BOOL color0_mov
= FALSE
;
601 /* This will loop over all the registers and try to
602 * make a bitmask of the ones we're interested in.
604 * Relative addressing tokens are ignored, but that's
605 * okay, since we'll catch any address registers when
606 * they are initialized (required by spec). */
607 for (i
= 0; i
< ins
.dst_count
; ++i
)
609 shader_record_register_usage(shader
, reg_maps
, &ins
.dst
[i
].reg
, shader_version
.type
);
611 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
612 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
613 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
614 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
&& shader_version
.major
< 3)
616 UINT idx
= ins
.dst
[i
].reg
.idx
;
618 switch (ins
.dst
[i
].reg
.type
)
620 case WINED3DSPR_RASTOUT
:
624 reg_maps
->output_registers
|= 1 << 10;
625 shader_signature_from_usage(&output_signature
[10],
626 WINED3D_DECL_USAGE_POSITION
, 0, 10, WINED3DSP_WRITEMASK_ALL
);
630 reg_maps
->output_registers
|= 1 << 11;
631 shader_signature_from_usage(&output_signature
[11],
632 WINED3D_DECL_USAGE_FOG
, 0, 11, WINED3DSP_WRITEMASK_0
);
636 reg_maps
->output_registers
|= 1 << 11;
637 shader_signature_from_usage(&output_signature
[11],
638 WINED3D_DECL_USAGE_PSIZE
, 0, 11, WINED3DSP_WRITEMASK_1
);
643 case WINED3DSPR_ATTROUT
:
647 if (reg_maps
->output_registers
& (1 << idx
))
649 output_signature
[idx
].mask
|= ins
.dst
[i
].write_mask
;
653 reg_maps
->output_registers
|= 1 << idx
;
654 shader_signature_from_usage(&output_signature
[idx
],
655 WINED3D_DECL_USAGE_COLOR
, idx
- 8, idx
, ins
.dst
[i
].write_mask
);
660 case WINED3DSPR_TEXCRDOUT
:
662 reg_maps
->texcoord_mask
[idx
] |= ins
.dst
[i
].write_mask
;
663 if (reg_maps
->output_registers
& (1 << idx
))
665 output_signature
[idx
].mask
|= ins
.dst
[i
].write_mask
;
669 reg_maps
->output_registers
|= 1 << idx
;
670 shader_signature_from_usage(&output_signature
[idx
],
671 WINED3D_DECL_USAGE_TEXCOORD
, idx
, idx
, ins
.dst
[i
].write_mask
);
680 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
682 if (ins
.dst
[i
].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
.dst
[i
].reg
.idx
)
684 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
685 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
686 * the mov and perform the sRGB write correction from the source register.
688 * However, if the mov is only partial, we can't do this, and if the write
689 * comes from an instruction other than MOV it is hard to do as well. If
690 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
691 shader
->u
.ps
.color0_mov
= FALSE
;
692 if (ins
.handler_idx
== WINED3DSIH_MOV
693 && ins
.dst
[i
].write_mask
== WINED3DSP_WRITEMASK_ALL
)
695 /* Used later when the source register is read. */
699 /* Also drop the MOV marker if the source register is overwritten prior to the shader
702 else if (ins
.dst
[i
].reg
.type
== WINED3DSPR_TEMP
703 && ins
.dst
[i
].reg
.idx
== shader
->u
.ps
.color0_reg
)
705 shader
->u
.ps
.color0_mov
= FALSE
;
709 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
710 if (shader_version
.major
== 1
711 && (ins
.handler_idx
== WINED3DSIH_TEX
712 || ins
.handler_idx
== WINED3DSIH_TEXBEM
713 || ins
.handler_idx
== WINED3DSIH_TEXBEML
714 || ins
.handler_idx
== WINED3DSIH_TEXDP3TEX
715 || ins
.handler_idx
== WINED3DSIH_TEXM3x2TEX
716 || ins
.handler_idx
== WINED3DSIH_TEXM3x3SPEC
717 || ins
.handler_idx
== WINED3DSIH_TEXM3x3TEX
718 || ins
.handler_idx
== WINED3DSIH_TEXM3x3VSPEC
719 || ins
.handler_idx
== WINED3DSIH_TEXREG2AR
720 || ins
.handler_idx
== WINED3DSIH_TEXREG2GB
721 || ins
.handler_idx
== WINED3DSIH_TEXREG2RGB
))
723 /* Fake sampler usage, only set reserved bit and type. */
724 DWORD sampler_code
= ins
.dst
[i
].reg
.idx
;
726 TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
727 reg_maps
->sampler_type
[sampler_code
] = WINED3DSTT_2D
;
729 /* texbem is only valid with < 1.4 pixel shaders */
730 if (ins
.handler_idx
== WINED3DSIH_TEXBEM
731 || ins
.handler_idx
== WINED3DSIH_TEXBEML
)
733 reg_maps
->bumpmat
|= 1 << ins
.dst
[i
].reg
.idx
;
734 if (ins
.handler_idx
== WINED3DSIH_TEXBEML
)
736 reg_maps
->luminanceparams
|= 1 << ins
.dst
[i
].reg
.idx
;
740 else if (ins
.handler_idx
== WINED3DSIH_BEM
)
742 reg_maps
->bumpmat
|= 1 << ins
.dst
[i
].reg
.idx
;
746 if (ins
.handler_idx
== WINED3DSIH_NRM
) reg_maps
->usesnrm
= 1;
747 else if (ins
.handler_idx
== WINED3DSIH_DSY
) reg_maps
->usesdsy
= 1;
748 else if (ins
.handler_idx
== WINED3DSIH_DSX
) reg_maps
->usesdsx
= 1;
749 else if (ins
.handler_idx
== WINED3DSIH_TEXLDD
) reg_maps
->usestexldd
= 1;
750 else if (ins
.handler_idx
== WINED3DSIH_TEXLDL
) reg_maps
->usestexldl
= 1;
751 else if (ins
.handler_idx
== WINED3DSIH_MOVA
) reg_maps
->usesmova
= 1;
752 else if (ins
.handler_idx
== WINED3DSIH_IFC
) reg_maps
->usesifc
= 1;
753 else if (ins
.handler_idx
== WINED3DSIH_CALL
) reg_maps
->usescall
= 1;
754 else if (ins
.handler_idx
== WINED3DSIH_POW
) reg_maps
->usespow
= 1;
755 else if (ins
.handler_idx
== WINED3DSIH_LOOP
756 || ins
.handler_idx
== WINED3DSIH_REP
)
759 if (cur_loop_depth
> max_loop_depth
)
760 max_loop_depth
= cur_loop_depth
;
762 else if (ins
.handler_idx
== WINED3DSIH_ENDLOOP
763 || ins
.handler_idx
== WINED3DSIH_ENDREP
)
767 shader_record_register_usage(shader
, reg_maps
, &ins
.predicate
->reg
, shader_version
.type
);
769 for (i
= 0; i
< ins
.src_count
; ++i
)
771 unsigned int count
= get_instr_extra_regcount(ins
.handler_idx
, i
);
772 struct wined3d_shader_register reg
= ins
.src
[i
].reg
;
774 shader_record_register_usage(shader
, reg_maps
, &ins
.src
[i
].reg
, shader_version
.type
);
778 shader_record_register_usage(shader
, reg_maps
, ®
, shader_version
.type
);
784 if (ins
.src
[i
].reg
.type
== WINED3DSPR_TEMP
785 && ins
.src
[i
].swizzle
== WINED3DSP_NOSWIZZLE
)
787 shader
->u
.ps
.color0_mov
= TRUE
;
788 shader
->u
.ps
.color0_reg
= ins
.src
[i
].reg
.idx
;
794 reg_maps
->loop_depth
= max_loop_depth
;
796 /* PS before 2.0 don't have explicit color outputs. Instead the value of
797 * R0 is written to the render target. */
798 if (shader_version
.major
< 2 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
799 reg_maps
->rt_mask
|= (1 << 0);
801 shader
->functionLength
= ((const char *)ptr
- (const char *)byte_code
);
806 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int max
)
808 DWORD map
= 1 << max
;
810 map
&= reg_maps
->shader_version
.major
< 3 ? ~reg_maps
->texcoord
: ~reg_maps
->input_registers
;
812 return wined3d_log2i(map
);
815 static void shader_dump_decl_usage(const struct wined3d_shader_semantic
*semantic
,
816 const struct wined3d_shader_version
*shader_version
)
820 if (semantic
->reg
.reg
.type
== WINED3DSPR_SAMPLER
)
822 switch (semantic
->sampler_type
)
824 case WINED3DSTT_2D
: TRACE("_2d"); break;
825 case WINED3DSTT_CUBE
: TRACE("_cube"); break;
826 case WINED3DSTT_VOLUME
: TRACE("_volume"); break;
827 default: TRACE("_unknown_ttype(0x%08x)", semantic
->sampler_type
);
832 /* Pixel shaders 3.0 don't have usage semantics. */
833 if (shader_version
->major
< 3 && shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
) return;
836 switch (semantic
->usage
)
838 case WINED3D_DECL_USAGE_POSITION
:
839 TRACE("position%u", semantic
->usage_idx
);
842 case WINED3D_DECL_USAGE_BLEND_INDICES
:
846 case WINED3D_DECL_USAGE_BLEND_WEIGHT
:
850 case WINED3D_DECL_USAGE_NORMAL
:
851 TRACE("normal%u", semantic
->usage_idx
);
854 case WINED3D_DECL_USAGE_PSIZE
:
858 case WINED3D_DECL_USAGE_COLOR
:
859 if (!semantic
->usage_idx
) TRACE("color");
860 else TRACE("specular%u", (semantic
->usage_idx
- 1));
863 case WINED3D_DECL_USAGE_TEXCOORD
:
864 TRACE("texture%u", semantic
->usage_idx
);
867 case WINED3D_DECL_USAGE_TANGENT
:
871 case WINED3D_DECL_USAGE_BINORMAL
:
875 case WINED3D_DECL_USAGE_TESS_FACTOR
:
879 case WINED3D_DECL_USAGE_POSITIONT
:
880 TRACE("positionT%u", semantic
->usage_idx
);
883 case WINED3D_DECL_USAGE_FOG
:
887 case WINED3D_DECL_USAGE_DEPTH
:
891 case WINED3D_DECL_USAGE_SAMPLE
:
896 FIXME("unknown_semantics(0x%08x)", semantic
->usage
);
901 static void shader_dump_register(const struct wined3d_shader_register
*reg
,
902 const struct wined3d_shader_version
*shader_version
)
904 static const char * const rastout_reg_names
[] = {"oPos", "oFog", "oPts"};
905 static const char * const misctype_reg_names
[] = {"vPos", "vFace"};
906 UINT offset
= reg
->idx
;
910 case WINED3DSPR_TEMP
:
914 case WINED3DSPR_INPUT
:
918 case WINED3DSPR_CONST
:
919 case WINED3DSPR_CONST2
:
920 case WINED3DSPR_CONST3
:
921 case WINED3DSPR_CONST4
:
923 offset
= shader_get_float_offset(reg
->type
, reg
->idx
);
926 case WINED3DSPR_TEXTURE
: /* vs: case WINED3DSPR_ADDR */
927 TRACE("%c", shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
? 't' : 'a');
930 case WINED3DSPR_RASTOUT
:
931 TRACE("%s", rastout_reg_names
[reg
->idx
]);
934 case WINED3DSPR_COLOROUT
:
938 case WINED3DSPR_DEPTHOUT
:
942 case WINED3DSPR_ATTROUT
:
946 case WINED3DSPR_TEXCRDOUT
:
947 /* Vertex shaders >= 3.0 use general purpose output registers
948 * (WINED3DSPR_OUTPUT), which can include an address token. */
949 if (shader_version
->major
>= 3) TRACE("o");
953 case WINED3DSPR_CONSTINT
:
957 case WINED3DSPR_CONSTBOOL
:
961 case WINED3DSPR_LABEL
:
965 case WINED3DSPR_LOOP
:
969 case WINED3DSPR_SAMPLER
:
973 case WINED3DSPR_MISCTYPE
:
974 if (reg
->idx
> 1) FIXME("Unhandled misctype register %u.\n", reg
->idx
);
975 else TRACE("%s", misctype_reg_names
[reg
->idx
]);
978 case WINED3DSPR_PREDICATE
:
982 case WINED3DSPR_IMMCONST
:
986 case WINED3DSPR_CONSTBUFFER
:
990 case WINED3DSPR_NULL
:
994 case WINED3DSPR_RESOURCE
:
999 TRACE("unhandled_rtype(%#x)", reg
->type
);
1003 if (reg
->type
== WINED3DSPR_IMMCONST
)
1006 switch (reg
->immconst_type
)
1008 case WINED3D_IMMCONST_SCALAR
:
1009 switch (reg
->data_type
)
1011 case WINED3D_DATA_FLOAT
:
1012 TRACE("%.8e", *(const float *)reg
->immconst_data
);
1014 case WINED3D_DATA_INT
:
1015 TRACE("%d", reg
->immconst_data
[0]);
1017 case WINED3D_DATA_RESOURCE
:
1018 case WINED3D_DATA_SAMPLER
:
1019 case WINED3D_DATA_UINT
:
1020 TRACE("%u", reg
->immconst_data
[0]);
1023 TRACE("<unhandled data type %#x>", reg
->data_type
);
1028 case WINED3D_IMMCONST_VEC4
:
1029 switch (reg
->data_type
)
1031 case WINED3D_DATA_FLOAT
:
1032 TRACE("%.8e, %.8e, %.8e, %.8e",
1033 *(const float *)®
->immconst_data
[0], *(const float *)®
->immconst_data
[1],
1034 *(const float *)®
->immconst_data
[2], *(const float *)®
->immconst_data
[3]);
1036 case WINED3D_DATA_INT
:
1037 TRACE("%d, %d, %d, %d",
1038 reg
->immconst_data
[0], reg
->immconst_data
[1],
1039 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1041 case WINED3D_DATA_RESOURCE
:
1042 case WINED3D_DATA_SAMPLER
:
1043 case WINED3D_DATA_UINT
:
1044 TRACE("%u, %u, %u, %u",
1045 reg
->immconst_data
[0], reg
->immconst_data
[1],
1046 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1049 TRACE("<unhandled data type %#x>", reg
->data_type
);
1055 TRACE("<unhandled immconst_type %#x>", reg
->immconst_type
);
1060 else if (reg
->type
!= WINED3DSPR_RASTOUT
1061 && reg
->type
!= WINED3DSPR_MISCTYPE
1062 && reg
->type
!= WINED3DSPR_NULL
)
1069 shader_dump_src_param(reg
->rel_addr
, shader_version
);
1072 TRACE("%u]", offset
);
1074 if (reg
->array_idx
!= ~0U)
1077 if (reg
->array_rel_addr
)
1079 shader_dump_src_param(reg
->array_rel_addr
, shader_version
);
1082 TRACE("%u]", reg
->array_idx
);
1088 void shader_dump_dst_param(const struct wined3d_shader_dst_param
*param
,
1089 const struct wined3d_shader_version
*shader_version
)
1091 DWORD write_mask
= param
->write_mask
;
1093 shader_dump_register(¶m
->reg
, shader_version
);
1095 if (write_mask
&& write_mask
!= WINED3DSP_WRITEMASK_ALL
)
1097 static const char *write_mask_chars
= "xyzw";
1100 if (write_mask
& WINED3DSP_WRITEMASK_0
) TRACE("%c", write_mask_chars
[0]);
1101 if (write_mask
& WINED3DSP_WRITEMASK_1
) TRACE("%c", write_mask_chars
[1]);
1102 if (write_mask
& WINED3DSP_WRITEMASK_2
) TRACE("%c", write_mask_chars
[2]);
1103 if (write_mask
& WINED3DSP_WRITEMASK_3
) TRACE("%c", write_mask_chars
[3]);
1107 void shader_dump_src_param(const struct wined3d_shader_src_param
*param
,
1108 const struct wined3d_shader_version
*shader_version
)
1110 enum wined3d_shader_src_modifier src_modifier
= param
->modifiers
;
1111 DWORD swizzle
= param
->swizzle
;
1113 if (src_modifier
== WINED3DSPSM_NEG
1114 || src_modifier
== WINED3DSPSM_BIASNEG
1115 || src_modifier
== WINED3DSPSM_SIGNNEG
1116 || src_modifier
== WINED3DSPSM_X2NEG
1117 || src_modifier
== WINED3DSPSM_ABSNEG
)
1119 else if (src_modifier
== WINED3DSPSM_COMP
)
1121 else if (src_modifier
== WINED3DSPSM_NOT
)
1124 if (src_modifier
== WINED3DSPSM_ABS
|| src_modifier
== WINED3DSPSM_ABSNEG
)
1127 shader_dump_register(¶m
->reg
, shader_version
);
1131 switch (src_modifier
)
1133 case WINED3DSPSM_NONE
: break;
1134 case WINED3DSPSM_NEG
: break;
1135 case WINED3DSPSM_NOT
: break;
1136 case WINED3DSPSM_BIAS
: TRACE("_bias"); break;
1137 case WINED3DSPSM_BIASNEG
: TRACE("_bias"); break;
1138 case WINED3DSPSM_SIGN
: TRACE("_bx2"); break;
1139 case WINED3DSPSM_SIGNNEG
: TRACE("_bx2"); break;
1140 case WINED3DSPSM_COMP
: break;
1141 case WINED3DSPSM_X2
: TRACE("_x2"); break;
1142 case WINED3DSPSM_X2NEG
: TRACE("_x2"); break;
1143 case WINED3DSPSM_DZ
: TRACE("_dz"); break;
1144 case WINED3DSPSM_DW
: TRACE("_dw"); break;
1145 case WINED3DSPSM_ABSNEG
: TRACE(")"); break;
1146 case WINED3DSPSM_ABS
: TRACE(")"); break;
1147 default: TRACE("_unknown_modifier(%#x)", src_modifier
);
1151 if (swizzle
!= WINED3DSP_NOSWIZZLE
)
1153 static const char *swizzle_chars
= "xyzw";
1154 DWORD swizzle_x
= swizzle
& 0x03;
1155 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
1156 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
1157 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
1159 if (swizzle_x
== swizzle_y
1160 && swizzle_x
== swizzle_z
1161 && swizzle_x
== swizzle_w
)
1163 TRACE(".%c", swizzle_chars
[swizzle_x
]);
1167 TRACE(".%c%c%c%c", swizzle_chars
[swizzle_x
], swizzle_chars
[swizzle_y
],
1168 swizzle_chars
[swizzle_z
], swizzle_chars
[swizzle_w
]);
1173 /* Shared code in order to generate the bulk of the shader string.
1174 * NOTE: A description of how to parse tokens can be found on MSDN. */
1175 void shader_generate_main(const struct wined3d_shader
*shader
, struct wined3d_shader_buffer
*buffer
,
1176 const struct wined3d_shader_reg_maps
*reg_maps
, const DWORD
*byte_code
, void *backend_ctx
)
1178 struct wined3d_device
*device
= shader
->device
;
1179 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
1180 void *fe_data
= shader
->frontend_data
;
1181 struct wined3d_shader_version shader_version
;
1182 struct wined3d_shader_loop_state loop_state
;
1183 struct wined3d_shader_instruction ins
;
1184 struct wined3d_shader_tex_mx tex_mx
;
1185 struct wined3d_shader_context ctx
;
1186 const DWORD
*ptr
= byte_code
;
1188 /* Initialize current parsing state. */
1189 tex_mx
.current_row
= 0;
1190 loop_state
.current_depth
= 0;
1191 loop_state
.current_reg
= 0;
1193 ctx
.shader
= shader
;
1194 ctx
.gl_info
= &device
->adapter
->gl_info
;
1195 ctx
.reg_maps
= reg_maps
;
1196 ctx
.buffer
= buffer
;
1197 ctx
.tex_mx
= &tex_mx
;
1198 ctx
.loop_state
= &loop_state
;
1199 ctx
.backend_data
= backend_ctx
;
1202 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1204 while (!fe
->shader_is_end(fe_data
, &ptr
))
1206 const char *comment
;
1209 /* Skip comment tokens. */
1210 fe
->shader_read_comment(&ptr
, &comment
, &comment_size
);
1211 if (comment
) continue;
1214 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1216 /* Unknown opcode and its parameters. */
1217 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1219 TRACE("Skipping unrecognized instruction.\n");
1224 FIXME("Predicates not implemented.\n");
1226 /* Call appropriate function for output target */
1227 device
->shader_backend
->shader_handle_instruction(&ins
);
1231 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param
*dst
)
1233 DWORD mmask
= dst
->modifiers
;
1238 case 13: TRACE("_d8"); break;
1239 case 14: TRACE("_d4"); break;
1240 case 15: TRACE("_d2"); break;
1241 case 1: TRACE("_x2"); break;
1242 case 2: TRACE("_x4"); break;
1243 case 3: TRACE("_x8"); break;
1244 default: TRACE("_unhandled_shift(%d)", dst
->shift
); break;
1247 if (mmask
& WINED3DSPDM_SATURATE
) TRACE("_sat");
1248 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) TRACE("_pp");
1249 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) TRACE("_centroid");
1251 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
1252 if (mmask
) FIXME("_unrecognized_modifier(%#x)", mmask
);
1255 static void shader_dump_primitive_type(enum wined3d_primitive_type primitive_type
)
1257 switch (primitive_type
)
1259 case WINED3D_PT_UNDEFINED
:
1262 case WINED3D_PT_POINTLIST
:
1265 case WINED3D_PT_LINELIST
:
1268 case WINED3D_PT_LINESTRIP
:
1271 case WINED3D_PT_TRIANGLELIST
:
1272 TRACE("trianglelist");
1274 case WINED3D_PT_TRIANGLESTRIP
:
1275 TRACE("trianglestrip");
1277 case WINED3D_PT_TRIANGLEFAN
:
1278 TRACE("trianglefan");
1280 case WINED3D_PT_LINELIST_ADJ
:
1281 TRACE("linelist_adj");
1283 case WINED3D_PT_LINESTRIP_ADJ
:
1284 TRACE("linestrip_adj");
1286 case WINED3D_PT_TRIANGLELIST_ADJ
:
1287 TRACE("trianglelist_adj");
1289 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
1290 TRACE("trianglestrip_adj");
1293 TRACE("<unrecognized_primitive_type %#x>", primitive_type
);
1298 static void shader_trace_init(const struct wined3d_shader_frontend
*fe
, void *fe_data
, const DWORD
*byte_code
)
1300 struct wined3d_shader_version shader_version
;
1301 const DWORD
*ptr
= byte_code
;
1302 const char *type_prefix
;
1305 TRACE("Parsing %p.\n", byte_code
);
1307 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1309 switch (shader_version
.type
)
1311 case WINED3D_SHADER_TYPE_VERTEX
:
1315 case WINED3D_SHADER_TYPE_GEOMETRY
:
1319 case WINED3D_SHADER_TYPE_PIXEL
:
1324 FIXME("Unhandled shader type %#x.\n", shader_version
.type
);
1325 type_prefix
= "unknown";
1329 TRACE("%s_%u_%u\n", type_prefix
, shader_version
.major
, shader_version
.minor
);
1331 while (!fe
->shader_is_end(fe_data
, &ptr
))
1333 struct wined3d_shader_instruction ins
;
1334 const char *comment
;
1338 fe
->shader_read_comment(&ptr
, &comment
, &comment_size
);
1341 if (comment_size
> 4 && *(const DWORD
*)comment
== WINEMAKEFOURCC('T', 'E', 'X', 'T'))
1343 const char *end
= comment
+ comment_size
;
1344 const char *ptr
= comment
+ 4;
1345 const char *line
= ptr
;
1352 UINT len
= ptr
- line
;
1353 if (len
&& *(ptr
- 1) == '\r') --len
;
1354 TRACE("// %s\n", debugstr_an(line
, len
));
1359 if (line
!= ptr
) TRACE("// %s\n", debugstr_an(line
, ptr
- line
));
1361 else TRACE("// %s\n", debugstr_an(comment
, comment_size
));
1365 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1366 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1368 TRACE("Skipping unrecognized instruction.\n");
1372 if (ins
.handler_idx
== WINED3DSIH_DCL
)
1374 shader_dump_decl_usage(&ins
.declaration
.semantic
, &shader_version
);
1375 shader_dump_ins_modifiers(&ins
.declaration
.semantic
.reg
);
1377 shader_dump_dst_param(&ins
.declaration
.semantic
.reg
, &shader_version
);
1379 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
1381 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1382 shader_dump_src_param(&ins
.declaration
.src
, &shader_version
);
1383 TRACE(", %s", ins
.flags
& WINED3DSI_INDEXED_DYNAMIC
? "dynamicIndexed" : "immediateIndexed");
1385 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
1386 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
1388 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1389 shader_dump_primitive_type(ins
.declaration
.primitive_type
);
1391 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
1393 TRACE("%s %u", shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.count
);
1395 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
1397 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(ins
.dst
[0].reg
.type
, ins
.dst
[0].reg
.idx
),
1398 *(const float *)&ins
.src
[0].reg
.immconst_data
[0],
1399 *(const float *)&ins
.src
[0].reg
.immconst_data
[1],
1400 *(const float *)&ins
.src
[0].reg
.immconst_data
[2],
1401 *(const float *)&ins
.src
[0].reg
.immconst_data
[3]);
1403 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
1405 TRACE("defi i%u = %d, %d, %d, %d", ins
.dst
[0].reg
.idx
,
1406 ins
.src
[0].reg
.immconst_data
[0],
1407 ins
.src
[0].reg
.immconst_data
[1],
1408 ins
.src
[0].reg
.immconst_data
[2],
1409 ins
.src
[0].reg
.immconst_data
[3]);
1411 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
1413 TRACE("defb b%u = %s", ins
.dst
[0].reg
.idx
, ins
.src
[0].reg
.immconst_data
[0] ? "true" : "false");
1420 shader_dump_src_param(ins
.predicate
, &shader_version
);
1424 /* PixWin marks instructions with the coissue flag with a '+' */
1425 if (ins
.coissue
) TRACE("+");
1427 TRACE("%s", shader_opcode_names
[ins
.handler_idx
]);
1429 if (ins
.handler_idx
== WINED3DSIH_IFC
1430 || ins
.handler_idx
== WINED3DSIH_BREAKC
)
1434 case WINED3D_SHADER_REL_OP_GT
: TRACE("_gt"); break;
1435 case WINED3D_SHADER_REL_OP_EQ
: TRACE("_eq"); break;
1436 case WINED3D_SHADER_REL_OP_GE
: TRACE("_ge"); break;
1437 case WINED3D_SHADER_REL_OP_LT
: TRACE("_lt"); break;
1438 case WINED3D_SHADER_REL_OP_NE
: TRACE("_ne"); break;
1439 case WINED3D_SHADER_REL_OP_LE
: TRACE("_le"); break;
1440 default: TRACE("_(%u)", ins
.flags
);
1443 else if (ins
.handler_idx
== WINED3DSIH_TEX
1444 && shader_version
.major
>= 2
1445 && (ins
.flags
& WINED3DSI_TEXLD_PROJECT
))
1450 for (i
= 0; i
< ins
.dst_count
; ++i
)
1452 shader_dump_ins_modifiers(&ins
.dst
[i
]);
1453 TRACE(!i
? " " : ", ");
1454 shader_dump_dst_param(&ins
.dst
[i
], &shader_version
);
1457 /* Other source tokens */
1458 for (i
= ins
.dst_count
; i
< (ins
.dst_count
+ ins
.src_count
); ++i
)
1460 TRACE(!i
? " " : ", ");
1461 shader_dump_src_param(&ins
.src
[i
- ins
.dst_count
], &shader_version
);
1468 static void shader_cleanup(struct wined3d_shader
*shader
)
1470 shader
->device
->shader_backend
->shader_destroy(shader
);
1471 HeapFree(GetProcessHeap(), 0, shader
->reg_maps
.constf
);
1472 HeapFree(GetProcessHeap(), 0, shader
->function
);
1473 shader_delete_constant_list(&shader
->constantsF
);
1474 shader_delete_constant_list(&shader
->constantsB
);
1475 shader_delete_constant_list(&shader
->constantsI
);
1476 list_remove(&shader
->shader_list_entry
);
1478 if (shader
->frontend
&& shader
->frontend_data
)
1479 shader
->frontend
->shader_free(shader
->frontend_data
);
1482 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
1483 static void shader_none_select(const struct wined3d_context
*context
, BOOL usePS
, BOOL useVS
) {}
1484 static void shader_none_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
1485 enum tex_types tex_type
, const SIZE
*ds_mask_size
) {}
1486 static void shader_none_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
) {}
1487 static void shader_none_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1488 static void shader_none_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1489 static void shader_none_load_constants(const struct wined3d_context
*context
, BOOL usePS
, BOOL useVS
) {}
1490 static void shader_none_load_np2fixup_constants(void *shader_priv
,
1491 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
) {}
1492 static void shader_none_destroy(struct wined3d_shader
*shader
) {}
1493 static HRESULT
shader_none_alloc(struct wined3d_device
*device
) {return WINED3D_OK
;}
1494 static void shader_none_free(struct wined3d_device
*device
) {}
1495 static void shader_none_context_destroyed(void *shader_priv
, const struct wined3d_context
*context
) {}
1497 static void shader_none_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
1499 /* Set the shader caps to 0 for the none shader backend */
1500 caps
->vs_version
= 0;
1501 caps
->gs_version
= 0;
1502 caps
->ps_version
= 0;
1503 caps
->vs_uniform_count
= 0;
1504 caps
->ps_uniform_count
= 0;
1505 caps
->ps_1x_max_value
= 0.0f
;
1506 caps
->vs_clipping
= FALSE
;
1509 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
1511 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
1513 TRACE("Checking support for fixup:\n");
1514 dump_color_fixup_desc(fixup
);
1517 /* Faked to make some apps happy. */
1518 if (!is_complex_fixup(fixup
))
1524 TRACE("[FAILED]\n");
1528 const struct wined3d_shader_backend_ops none_shader_backend
=
1530 shader_none_handle_instruction
,
1532 shader_none_select_depth_blt
,
1533 shader_none_deselect_depth_blt
,
1534 shader_none_update_float_vertex_constants
,
1535 shader_none_update_float_pixel_constants
,
1536 shader_none_load_constants
,
1537 shader_none_load_np2fixup_constants
,
1538 shader_none_destroy
,
1541 shader_none_context_destroyed
,
1542 shader_none_get_caps
,
1543 shader_none_color_fixup_supported
,
1546 static HRESULT
shader_set_function(struct wined3d_shader
*shader
, const DWORD
*byte_code
,
1547 const struct wined3d_shader_signature
*output_signature
, DWORD float_const_count
,
1548 enum wined3d_shader_type type
, unsigned int max_version
)
1550 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1551 const struct wined3d_shader_frontend
*fe
;
1553 unsigned int backend_version
;
1555 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1556 shader
, byte_code
, output_signature
, float_const_count
);
1558 fe
= shader_select_frontend(*byte_code
);
1561 FIXME("Unable to find frontend for shader.\n");
1562 return WINED3DERR_INVALIDCALL
;
1564 shader
->frontend
= fe
;
1565 shader
->frontend_data
= fe
->shader_init(byte_code
, output_signature
);
1566 if (!shader
->frontend_data
)
1568 FIXME("Failed to initialize frontend.\n");
1569 return WINED3DERR_INVALIDCALL
;
1572 /* First pass: trace shader. */
1573 if (TRACE_ON(d3d_shader
))
1574 shader_trace_init(fe
, shader
->frontend_data
, byte_code
);
1576 /* Initialize immediate constant lists. */
1577 list_init(&shader
->constantsF
);
1578 list_init(&shader
->constantsB
);
1579 list_init(&shader
->constantsI
);
1581 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1582 hr
= shader_get_registers_used(shader
, fe
,
1583 reg_maps
, shader
->input_signature
, shader
->output_signature
,
1584 byte_code
, float_const_count
);
1585 if (FAILED(hr
)) return hr
;
1587 if (reg_maps
->shader_version
.type
!= type
)
1589 WARN("Wrong shader type %d.\n", reg_maps
->shader_version
.type
);
1590 return WINED3DERR_INVALIDCALL
;
1592 if (reg_maps
->shader_version
.major
> max_version
)
1594 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps
->shader_version
.major
);
1595 return WINED3DERR_INVALIDCALL
;
1599 case WINED3D_SHADER_TYPE_VERTEX
:
1600 backend_version
= shader
->device
->vs_version
;
1602 case WINED3D_SHADER_TYPE_GEOMETRY
:
1603 backend_version
= shader
->device
->gs_version
;
1605 case WINED3D_SHADER_TYPE_PIXEL
:
1606 backend_version
= shader
->device
->ps_version
;
1609 FIXME("No backend version-checking for this shader type\n");
1610 backend_version
= 0;
1612 if (reg_maps
->shader_version
.major
> backend_version
)
1614 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
1615 reg_maps
->shader_version
.major
, reg_maps
->shader_version
.minor
);
1616 return WINED3DERR_INVALIDCALL
;
1619 shader
->function
= HeapAlloc(GetProcessHeap(), 0, shader
->functionLength
);
1620 if (!shader
->function
)
1621 return E_OUTOFMEMORY
;
1622 memcpy(shader
->function
, byte_code
, shader
->functionLength
);
1627 ULONG CDECL
wined3d_shader_incref(struct wined3d_shader
*shader
)
1629 ULONG refcount
= InterlockedIncrement(&shader
->ref
);
1631 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
1636 /* Do not call while under the GL lock. */
1637 ULONG CDECL
wined3d_shader_decref(struct wined3d_shader
*shader
)
1639 ULONG refcount
= InterlockedDecrement(&shader
->ref
);
1641 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
1645 shader_cleanup(shader
);
1646 shader
->parent_ops
->wined3d_object_destroyed(shader
->parent
);
1647 HeapFree(GetProcessHeap(), 0, shader
);
1653 void * CDECL
wined3d_shader_get_parent(const struct wined3d_shader
*shader
)
1655 TRACE("shader %p.\n", shader
);
1657 return shader
->parent
;
1660 HRESULT CDECL
wined3d_shader_get_byte_code(const struct wined3d_shader
*shader
,
1661 void *byte_code
, UINT
*byte_code_size
)
1663 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader
, byte_code
, byte_code_size
);
1667 *byte_code_size
= shader
->functionLength
;
1671 if (*byte_code_size
< shader
->functionLength
)
1673 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
1674 * than the required size we should write the required size and
1675 * return D3DERR_MOREDATA. That's not actually true. */
1676 return WINED3DERR_INVALIDCALL
;
1679 memcpy(byte_code
, shader
->function
, shader
->functionLength
);
1684 /* Set local constants for d3d8 shaders. */
1685 HRESULT CDECL
wined3d_shader_set_local_constants_float(struct wined3d_shader
*shader
,
1686 UINT start_idx
, const float *src_data
, UINT count
)
1688 UINT end_idx
= start_idx
+ count
;
1691 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader
, start_idx
, src_data
, count
);
1693 if (end_idx
> shader
->limits
.constant_float
)
1695 WARN("end_idx %u > float constants limit %u.\n",
1696 end_idx
, shader
->limits
.constant_float
);
1697 end_idx
= shader
->limits
.constant_float
;
1700 for (i
= start_idx
; i
< end_idx
; ++i
)
1702 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
1704 return E_OUTOFMEMORY
;
1707 memcpy(lconst
->value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
1708 list_add_head(&shader
->constantsF
, &lconst
->entry
);
1714 void find_vs_compile_args(const struct wined3d_state
*state
,
1715 const struct wined3d_shader
*shader
, struct vs_compile_args
*args
)
1717 args
->fog_src
= state
->render_states
[WINED3D_RS_FOGTABLEMODE
]
1718 == WINED3D_FOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
1719 args
->clip_enabled
= state
->render_states
[WINED3D_RS_CLIPPING
]
1720 && state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
];
1721 args
->swizzle_map
= shader
->device
->strided_streams
.swizzle_map
;
1724 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
1726 if (usage_idx1
!= usage_idx2
)
1728 if (usage1
== usage2
)
1730 if (usage1
== WINED3D_DECL_USAGE_POSITION
&& usage2
== WINED3D_DECL_USAGE_POSITIONT
)
1732 if (usage2
== WINED3D_DECL_USAGE_POSITION
&& usage1
== WINED3D_DECL_USAGE_POSITIONT
)
1738 BOOL
vshader_get_input(const struct wined3d_shader
*shader
,
1739 BYTE usage_req
, BYTE usage_idx_req
, unsigned int *regnum
)
1741 WORD map
= shader
->reg_maps
.input_registers
;
1744 for (i
= 0; map
; map
>>= 1, ++i
)
1746 if (!(map
& 1)) continue;
1748 if (match_usage(shader
->u
.vs
.attributes
[i
].usage
,
1749 shader
->u
.vs
.attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
1758 static void vertexshader_set_limits(struct wined3d_shader
*shader
)
1760 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
1761 shader
->reg_maps
.shader_version
.minor
);
1762 struct wined3d_device
*device
= shader
->device
;
1764 shader
->limits
.packed_input
= 0;
1766 switch (shader_version
)
1768 case WINED3D_SHADER_VERSION(1, 0):
1769 case WINED3D_SHADER_VERSION(1, 1):
1770 shader
->limits
.constant_bool
= 0;
1771 shader
->limits
.constant_int
= 0;
1772 shader
->limits
.packed_output
= 12;
1773 shader
->limits
.sampler
= 0;
1774 /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1775 * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1777 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1780 case WINED3D_SHADER_VERSION(2, 0):
1781 case WINED3D_SHADER_VERSION(2, 1):
1782 shader
->limits
.constant_bool
= 16;
1783 shader
->limits
.constant_int
= 16;
1784 shader
->limits
.packed_output
= 12;
1785 shader
->limits
.sampler
= 0;
1786 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1789 case WINED3D_SHADER_VERSION(3, 0):
1790 shader
->limits
.constant_bool
= 16;
1791 shader
->limits
.constant_int
= 16;
1792 shader
->limits
.packed_output
= 12;
1793 shader
->limits
.sampler
= 4;
1794 /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1795 * even though they are capable of supporting much more (GL
1796 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1797 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1798 * shaders to 256. */
1799 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1802 case WINED3D_SHADER_VERSION(4, 0):
1803 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
1804 shader
->limits
.constant_int
= 0;
1805 shader
->limits
.constant_float
= 0;
1806 shader
->limits
.constant_bool
= 0;
1807 shader
->limits
.packed_output
= 16;
1808 shader
->limits
.packed_input
= 0;
1812 shader
->limits
.constant_bool
= 16;
1813 shader
->limits
.constant_int
= 16;
1814 shader
->limits
.packed_output
= 12;
1815 shader
->limits
.sampler
= 0;
1816 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1817 FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1818 shader
->reg_maps
.shader_version
.major
,
1819 shader
->reg_maps
.shader_version
.minor
);
1823 static HRESULT
vertexshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
1824 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
1825 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
1827 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1832 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
1834 shader_init(shader
, device
, parent
, parent_ops
);
1835 hr
= shader_set_function(shader
, byte_code
, output_signature
, device
->d3d_vshader_constantF
,
1836 WINED3D_SHADER_TYPE_VERTEX
, max_version
);
1839 WARN("Failed to set function, hr %#x.\n", hr
);
1840 shader_cleanup(shader
);
1844 map
= reg_maps
->input_registers
;
1845 for (i
= 0; map
; map
>>= 1, ++i
)
1847 if (!(map
& 1) || !shader
->input_signature
[i
].semantic_name
)
1850 shader
->u
.vs
.attributes
[i
].usage
=
1851 shader_usage_from_semantic_name(shader
->input_signature
[i
].semantic_name
);
1852 shader
->u
.vs
.attributes
[i
].usage_idx
= shader
->input_signature
[i
].semantic_idx
;
1855 if (output_signature
)
1857 for (i
= 0; i
< output_signature
->element_count
; ++i
)
1859 struct wined3d_shader_signature_element
*e
= &output_signature
->elements
[i
];
1860 reg_maps
->output_registers
|= 1 << e
->register_idx
;
1861 shader
->output_signature
[e
->register_idx
] = *e
;
1865 vertexshader_set_limits(shader
);
1867 shader
->load_local_constsF
= reg_maps
->usesrelconstF
1868 && !list_empty(&shader
->constantsF
);
1873 static void geometryshader_set_limits(struct wined3d_shader
*shader
)
1875 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
1876 shader
->reg_maps
.shader_version
.minor
);
1878 switch (shader_version
)
1880 case WINED3D_SHADER_VERSION(4, 0):
1881 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
1882 shader
->limits
.constant_int
= 0;
1883 shader
->limits
.constant_float
= 0;
1884 shader
->limits
.constant_bool
= 0;
1885 shader
->limits
.packed_output
= 32;
1886 shader
->limits
.packed_input
= 16;
1890 memset(&shader
->limits
, 0, sizeof(shader
->limits
));
1891 FIXME("Unhandled geometry shader version \"%u.%u\".\n",
1892 shader
->reg_maps
.shader_version
.major
,
1893 shader
->reg_maps
.shader_version
.minor
);
1897 static HRESULT
geometryshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
1898 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
1899 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
1903 shader_init(shader
, device
, parent
, parent_ops
);
1904 hr
= shader_set_function(shader
, byte_code
, output_signature
, 0,
1905 WINED3D_SHADER_TYPE_GEOMETRY
, max_version
);
1908 WARN("Failed to set function, hr %#x.\n", hr
);
1909 shader_cleanup(shader
);
1913 geometryshader_set_limits(shader
);
1915 shader
->load_local_constsF
= FALSE
;
1920 void find_ps_compile_args(const struct wined3d_state
*state
,
1921 const struct wined3d_shader
*shader
, struct ps_compile_args
*args
)
1923 struct wined3d_device
*device
= shader
->device
;
1924 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1925 const struct wined3d_texture
*texture
;
1928 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
1929 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && state
->render_states
[WINED3D_RS_SRGBWRITEENABLE
])
1931 const struct wined3d_surface
*rt
= state
->fb
->render_targets
[0];
1932 if (rt
->resource
.format
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
1934 static unsigned int warned
= 0;
1936 args
->srgb_correction
= 1;
1937 if (state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] && !warned
++)
1938 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
1939 "support, expect rendering artifacts.\n");
1943 if (shader
->reg_maps
.shader_version
.major
== 1
1944 && shader
->reg_maps
.shader_version
.minor
<= 3)
1946 for (i
= 0; i
< 4; ++i
)
1948 DWORD flags
= state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
];
1950 if (flags
& WINED3D_TTFF_PROJECTED
)
1952 DWORD tex_transform
= flags
& ~WINED3D_TTFF_PROJECTED
;
1954 if (!state
->vertex_shader
)
1957 unsigned int index
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
1958 DWORD max_valid
= WINED3D_TTFF_COUNT4
;
1959 enum wined3d_sampler_texture_type sampler_type
= shader
->reg_maps
.sampler_type
[i
];
1961 for (j
= 0; j
< state
->vertex_declaration
->element_count
; ++j
)
1963 struct wined3d_vertex_declaration_element
*element
=
1964 &state
->vertex_declaration
->elements
[j
];
1966 if (element
->usage
== WINED3D_DECL_USAGE_TEXCOORD
1967 && element
->usage_idx
== index
)
1969 max_valid
= element
->format
->component_count
;
1973 if (!tex_transform
|| tex_transform
> max_valid
)
1975 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
1976 tex_transform
, max_valid
);
1977 tex_transform
= max_valid
;
1979 if ((sampler_type
== WINED3DSTT_1D
&& tex_transform
> WINED3D_TTFF_COUNT1
)
1980 || (sampler_type
== WINED3DSTT_2D
&& tex_transform
> WINED3D_TTFF_COUNT2
)
1981 || (sampler_type
== WINED3DSTT_VOLUME
&& tex_transform
> WINED3D_TTFF_COUNT3
))
1982 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
1985 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
1986 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
1987 i
, tex_transform
, sampler_type
);
1991 tex_transform
= WINED3D_TTFF_COUNT4
| WINED3D_PSARGS_PROJECTED
;
1993 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
1998 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2000 if (!shader
->reg_maps
.sampler_type
[i
])
2003 texture
= state
->textures
[i
];
2006 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
2009 args
->color_fixup
[i
] = texture
->resource
.format
->color_fixup
;
2011 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
2012 args
->shadow
|= 1 << i
;
2014 /* Flag samplers that need NP2 texcoord fixup. */
2015 if (!(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
2016 args
->np2_fixup
|= (1 << i
);
2018 if (shader
->reg_maps
.shader_version
.major
>= 3)
2020 if (device
->strided_streams
.position_transformed
)
2022 args
->vp_mode
= pretransformed
;
2024 else if (use_vs(state
))
2026 args
->vp_mode
= vertexshader
;
2030 args
->vp_mode
= fixedfunction
;
2032 args
->fog
= FOG_OFF
;
2036 args
->vp_mode
= vertexshader
;
2037 if (state
->render_states
[WINED3D_RS_FOGENABLE
])
2039 switch (state
->render_states
[WINED3D_RS_FOGTABLEMODE
])
2041 case WINED3D_FOG_NONE
:
2042 if (device
->strided_streams
.position_transformed
|| use_vs(state
))
2044 args
->fog
= FOG_LINEAR
;
2048 switch (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
])
2050 case WINED3D_FOG_NONE
: /* Fall through. */
2051 case WINED3D_FOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
2052 case WINED3D_FOG_EXP
: args
->fog
= FOG_EXP
; break;
2053 case WINED3D_FOG_EXP2
: args
->fog
= FOG_EXP2
; break;
2057 case WINED3D_FOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
2058 case WINED3D_FOG_EXP
: args
->fog
= FOG_EXP
; break;
2059 case WINED3D_FOG_EXP2
: args
->fog
= FOG_EXP2
; break;
2064 args
->fog
= FOG_OFF
;
2069 static void pixelshader_set_limits(struct wined3d_shader
*shader
)
2071 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
2072 shader
->reg_maps
.shader_version
.minor
);
2074 shader
->limits
.packed_output
= 0;
2076 switch (shader_version
)
2078 case WINED3D_SHADER_VERSION(1, 0):
2079 case WINED3D_SHADER_VERSION(1, 1):
2080 case WINED3D_SHADER_VERSION(1, 2):
2081 case WINED3D_SHADER_VERSION(1, 3):
2082 shader
->limits
.constant_float
= 8;
2083 shader
->limits
.constant_int
= 0;
2084 shader
->limits
.constant_bool
= 0;
2085 shader
->limits
.sampler
= 4;
2086 shader
->limits
.packed_input
= 0;
2089 case WINED3D_SHADER_VERSION(1, 4):
2090 shader
->limits
.constant_float
= 8;
2091 shader
->limits
.constant_int
= 0;
2092 shader
->limits
.constant_bool
= 0;
2093 shader
->limits
.sampler
= 6;
2094 shader
->limits
.packed_input
= 0;
2097 /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2098 case WINED3D_SHADER_VERSION(2, 0):
2099 shader
->limits
.constant_float
= 32;
2100 shader
->limits
.constant_int
= 16;
2101 shader
->limits
.constant_bool
= 16;
2102 shader
->limits
.sampler
= 16;
2103 shader
->limits
.packed_input
= 0;
2106 case WINED3D_SHADER_VERSION(2, 1):
2107 shader
->limits
.constant_float
= 32;
2108 shader
->limits
.constant_int
= 16;
2109 shader
->limits
.constant_bool
= 16;
2110 shader
->limits
.sampler
= 16;
2111 shader
->limits
.packed_input
= 0;
2114 case WINED3D_SHADER_VERSION(3, 0):
2115 shader
->limits
.constant_float
= 224;
2116 shader
->limits
.constant_int
= 16;
2117 shader
->limits
.constant_bool
= 16;
2118 shader
->limits
.sampler
= 16;
2119 shader
->limits
.packed_input
= 12;
2122 case WINED3D_SHADER_VERSION(4, 0):
2123 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
2124 shader
->limits
.constant_int
= 0;
2125 shader
->limits
.constant_float
= 0;
2126 shader
->limits
.constant_bool
= 0;
2127 shader
->limits
.packed_input
= 32;
2131 shader
->limits
.constant_float
= 32;
2132 shader
->limits
.constant_int
= 16;
2133 shader
->limits
.constant_bool
= 16;
2134 shader
->limits
.sampler
= 16;
2135 shader
->limits
.packed_input
= 0;
2136 FIXME("Unrecognized pixel shader version %u.%u\n",
2137 shader
->reg_maps
.shader_version
.major
,
2138 shader
->reg_maps
.shader_version
.minor
);
2142 static HRESULT
pixelshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2143 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
2144 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
2146 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2147 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
2150 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
2152 shader_init(shader
, device
, parent
, parent_ops
);
2153 hr
= shader_set_function(shader
, byte_code
, output_signature
, device
->d3d_pshader_constantF
,
2154 WINED3D_SHADER_TYPE_PIXEL
, max_version
);
2157 WARN("Failed to set function, hr %#x.\n", hr
);
2158 shader_cleanup(shader
);
2162 pixelshader_set_limits(shader
);
2164 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2166 if (shader
->u
.ps
.input_reg_used
[i
])
2169 highest_reg_used
= i
;
2173 /* Don't do any register mapping magic if it is not needed, or if we can't
2174 * achieve anything anyway */
2175 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
2176 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2178 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2180 /* This happens with relative addressing. The input mapper function
2181 * warns about this if the higher registers are declared too, so
2182 * don't write a FIXME here */
2183 WARN("More varying registers used than supported\n");
2186 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2188 shader
->u
.ps
.input_reg_map
[i
] = i
;
2191 shader
->u
.ps
.declared_in_count
= highest_reg_used
+ 1;
2195 shader
->u
.ps
.declared_in_count
= 0;
2196 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2198 if (shader
->u
.ps
.input_reg_used
[i
])
2199 shader
->u
.ps
.input_reg_map
[i
] = shader
->u
.ps
.declared_in_count
++;
2200 else shader
->u
.ps
.input_reg_map
[i
] = ~0U;
2204 shader
->load_local_constsF
= FALSE
;
2209 void pixelshader_update_samplers(struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_texture
* const *textures
)
2211 enum wined3d_sampler_texture_type
*sampler_type
= reg_maps
->sampler_type
;
2214 if (reg_maps
->shader_version
.major
!= 1) return;
2216 for (i
= 0; i
< max(MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
); ++i
)
2218 /* We don't sample from this sampler. */
2219 if (!sampler_type
[i
]) continue;
2223 WARN("No texture bound to sampler %u, using 2D.\n", i
);
2224 sampler_type
[i
] = WINED3DSTT_2D
;
2228 switch (textures
[i
]->target
)
2230 case GL_TEXTURE_RECTANGLE_ARB
:
2232 /* We have to select between texture rectangles and 2D
2233 * textures later because 2.0 and 3.0 shaders only have
2234 * WINED3DSTT_2D as well. */
2235 sampler_type
[i
] = WINED3DSTT_2D
;
2239 sampler_type
[i
] = WINED3DSTT_VOLUME
;
2242 case GL_TEXTURE_CUBE_MAP_ARB
:
2243 sampler_type
[i
] = WINED3DSTT_CUBE
;
2247 FIXME("Unrecognized texture type %#x, using 2D.\n", textures
[i
]->target
);
2248 sampler_type
[i
] = WINED3DSTT_2D
;
2253 HRESULT CDECL
wined3d_shader_create_gs(struct wined3d_device
*device
, const DWORD
*byte_code
,
2254 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2255 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2257 struct wined3d_shader
*object
;
2260 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2261 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2263 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2266 ERR("Failed to allocate shader memory.\n");
2267 return E_OUTOFMEMORY
;
2270 hr
= geometryshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2273 WARN("Failed to initialize geometry shader, hr %#x.\n", hr
);
2274 HeapFree(GetProcessHeap(), 0, object
);
2278 TRACE("Created geometry shader %p.\n", object
);
2284 HRESULT CDECL
wined3d_shader_create_ps(struct wined3d_device
*device
, const DWORD
*byte_code
,
2285 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2286 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2288 struct wined3d_shader
*object
;
2291 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2292 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2294 if (device
->ps_selected_mode
== SHADER_NONE
)
2295 return WINED3DERR_INVALIDCALL
;
2297 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2300 ERR("Failed to allocate shader memory.\n");
2301 return E_OUTOFMEMORY
;
2304 hr
= pixelshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2307 WARN("Failed to initialize pixel shader, hr %#x.\n", hr
);
2308 HeapFree(GetProcessHeap(), 0, object
);
2312 TRACE("Created pixel shader %p.\n", object
);
2318 HRESULT CDECL
wined3d_shader_create_vs(struct wined3d_device
*device
, const DWORD
*byte_code
,
2319 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2320 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2322 struct wined3d_shader
*object
;
2325 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2326 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2328 if (device
->vs_selected_mode
== SHADER_NONE
)
2329 return WINED3DERR_INVALIDCALL
;
2331 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2334 ERR("Failed to allocate shader memory.\n");
2335 return E_OUTOFMEMORY
;
2338 hr
= vertexshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2341 WARN("Failed to initialize vertex shader, hr %#x.\n", hr
);
2342 HeapFree(GetProcessHeap(), 0, object
);
2346 TRACE("Created vertex shader %p.\n", object
);