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
[0].offset
;
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
)
369 reg_maps
->texcoord
|= 1 << reg
->idx
[0].offset
;
371 reg_maps
->address
|= 1 << reg
->idx
[0].offset
;
374 case WINED3DSPR_TEMP
:
375 reg_maps
->temporary
|= 1 << reg
->idx
[0].offset
;
378 case WINED3DSPR_INPUT
:
379 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
381 if (reg
->idx
[0].rel_addr
)
383 /* If relative addressing is used, we must assume that all registers
384 * are used. Even if it is a construct like v3[aL], we can't assume
385 * that v0, v1 and v2 aren't read because aL can be negative */
387 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
389 shader
->u
.ps
.input_reg_used
[i
] = TRUE
;
394 shader
->u
.ps
.input_reg_used
[reg
->idx
[0].offset
] = TRUE
;
398 reg_maps
->input_registers
|= 1 << reg
->idx
[0].offset
;
401 case WINED3DSPR_RASTOUT
:
402 if (reg
->idx
[0].offset
== 1)
406 case WINED3DSPR_MISCTYPE
:
407 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
409 if (!reg
->idx
[0].offset
)
411 else if (reg
->idx
[0].offset
== 1)
412 reg_maps
->usesfacing
= 1;
416 case WINED3DSPR_CONST
:
417 if (reg
->idx
[0].rel_addr
)
419 if (reg
->idx
[0].offset
< reg_maps
->min_rel_offset
)
420 reg_maps
->min_rel_offset
= reg
->idx
[0].offset
;
421 if (reg
->idx
[0].offset
> reg_maps
->max_rel_offset
)
422 reg_maps
->max_rel_offset
= reg
->idx
[0].offset
;
423 reg_maps
->usesrelconstF
= TRUE
;
427 set_bitmap_bit(reg_maps
->constf
, reg
->idx
[0].offset
);
431 case WINED3DSPR_CONSTINT
:
432 reg_maps
->integer_constants
|= (1 << reg
->idx
[0].offset
);
435 case WINED3DSPR_CONSTBOOL
:
436 reg_maps
->boolean_constants
|= (1 << reg
->idx
[0].offset
);
439 case WINED3DSPR_COLOROUT
:
440 reg_maps
->rt_mask
|= (1 << reg
->idx
[0].offset
);
444 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
445 reg
->type
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
450 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr
, unsigned int param
)
454 case WINED3DSIH_M4x4
:
455 case WINED3DSIH_M3x4
:
456 return param
== 1 ? 3 : 0;
458 case WINED3DSIH_M4x3
:
459 case WINED3DSIH_M3x3
:
460 return param
== 1 ? 2 : 0;
462 case WINED3DSIH_M3x2
:
463 return param
== 1 ? 1 : 0;
470 /* Note that this does not count the loop register as an address register. */
471 static HRESULT
shader_get_registers_used(struct wined3d_shader
*shader
, const struct wined3d_shader_frontend
*fe
,
472 struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_shader_signature_element
*input_signature
,
473 struct wined3d_shader_signature_element
*output_signature
, const DWORD
*byte_code
, DWORD constf_size
)
475 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
476 void *fe_data
= shader
->frontend_data
;
477 struct wined3d_shader_version shader_version
;
478 const DWORD
*ptr
= byte_code
;
480 memset(reg_maps
, 0, sizeof(*reg_maps
));
481 reg_maps
->min_rel_offset
= ~0U;
483 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
484 reg_maps
->shader_version
= shader_version
;
486 reg_maps
->constf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
487 sizeof(*reg_maps
->constf
) * ((constf_size
+ 31) / 32));
488 if (!reg_maps
->constf
)
490 ERR("Failed to allocate constant map memory.\n");
491 return E_OUTOFMEMORY
;
494 while (!fe
->shader_is_end(fe_data
, &ptr
))
496 struct wined3d_shader_instruction ins
;
499 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
501 /* Unhandled opcode, and its parameters. */
502 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
504 TRACE("Skipping unrecognized instruction.\n");
508 /* Handle declarations. */
509 if (ins
.handler_idx
== WINED3DSIH_DCL
)
511 struct wined3d_shader_semantic
*semantic
= &ins
.declaration
.semantic
;
513 switch (semantic
->reg
.reg
.type
)
515 /* Mark input registers used. */
516 case WINED3DSPR_INPUT
:
517 reg_maps
->input_registers
|= 1 << semantic
->reg
.reg
.idx
[0].offset
;
518 shader_signature_from_semantic(&input_signature
[semantic
->reg
.reg
.idx
[0].offset
], semantic
);
521 /* Vertex shader: mark 3.0 output registers used, save token. */
522 case WINED3DSPR_OUTPUT
:
523 reg_maps
->output_registers
|= 1 << semantic
->reg
.reg
.idx
[0].offset
;
524 shader_signature_from_semantic(&output_signature
[semantic
->reg
.reg
.idx
[0].offset
], semantic
);
525 if (semantic
->usage
== WINED3D_DECL_USAGE_FOG
)
529 /* Save sampler usage token. */
530 case WINED3DSPR_SAMPLER
:
531 reg_maps
->sampler_type
[semantic
->reg
.reg
.idx
[0].offset
] = semantic
->sampler_type
;
535 TRACE("Not recording DCL register type %#x.\n", semantic
->reg
.reg
.type
);
539 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
541 struct wined3d_shader_register
*reg
= &ins
.declaration
.src
.reg
;
542 if (reg
->idx
[0].offset
>= WINED3D_MAX_CBS
)
543 ERR("Invalid CB index %u.\n", reg
->idx
[0].offset
);
545 reg_maps
->cb_sizes
[reg
->idx
[0].offset
] = reg
->idx
[1].offset
;
547 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
)
549 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
550 shader
->u
.gs
.input_type
= ins
.declaration
.primitive_type
;
552 FIXME("Invalid instruction %#x for shader type %#x.\n",
553 ins
.handler_idx
, shader_version
.type
);
555 else if (ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
557 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
558 shader
->u
.gs
.output_type
= ins
.declaration
.primitive_type
;
560 FIXME("Invalid instruction %#x for shader type %#x.\n",
561 ins
.handler_idx
, shader_version
.type
);
563 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
565 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
566 shader
->u
.gs
.vertices_out
= ins
.declaration
.count
;
568 FIXME("Invalid instruction %#x for shader type %#x.\n",
569 ins
.handler_idx
, shader_version
.type
);
571 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
573 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
574 if (!lconst
) return E_OUTOFMEMORY
;
576 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
577 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
579 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
580 if (shader_version
.major
== 1 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
582 float *value
= (float *)lconst
->value
;
583 if (value
[0] < -1.0f
) value
[0] = -1.0f
;
584 else if (value
[0] > 1.0f
) value
[0] = 1.0f
;
585 if (value
[1] < -1.0f
) value
[1] = -1.0f
;
586 else if (value
[1] > 1.0f
) value
[1] = 1.0f
;
587 if (value
[2] < -1.0f
) value
[2] = -1.0f
;
588 else if (value
[2] > 1.0f
) value
[2] = 1.0f
;
589 if (value
[3] < -1.0f
) value
[3] = -1.0f
;
590 else if (value
[3] > 1.0f
) value
[3] = 1.0f
;
593 list_add_head(&shader
->constantsF
, &lconst
->entry
);
595 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
597 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
598 if (!lconst
) return E_OUTOFMEMORY
;
600 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
601 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
603 list_add_head(&shader
->constantsI
, &lconst
->entry
);
604 reg_maps
->local_int_consts
|= (1 << lconst
->idx
);
606 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
608 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
609 if (!lconst
) return E_OUTOFMEMORY
;
611 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
612 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, sizeof(DWORD
));
614 list_add_head(&shader
->constantsB
, &lconst
->entry
);
615 reg_maps
->local_bool_consts
|= (1 << lconst
->idx
);
617 /* For subroutine prototypes. */
618 else if (ins
.handler_idx
== WINED3DSIH_LABEL
)
620 reg_maps
->labels
|= 1 << ins
.src
[0].reg
.idx
[0].offset
;
622 /* Set texture, address, temporary registers. */
625 BOOL color0_mov
= FALSE
;
628 /* This will loop over all the registers and try to
629 * make a bitmask of the ones we're interested in.
631 * Relative addressing tokens are ignored, but that's
632 * okay, since we'll catch any address registers when
633 * they are initialized (required by spec). */
634 for (i
= 0; i
< ins
.dst_count
; ++i
)
636 shader_record_register_usage(shader
, reg_maps
, &ins
.dst
[i
].reg
, shader_version
.type
);
638 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
639 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
640 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
641 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
&& shader_version
.major
< 3)
643 UINT idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
645 switch (ins
.dst
[i
].reg
.type
)
647 case WINED3DSPR_RASTOUT
:
651 reg_maps
->output_registers
|= 1 << 10;
652 shader_signature_from_usage(&output_signature
[10],
653 WINED3D_DECL_USAGE_POSITION
, 0, 10, WINED3DSP_WRITEMASK_ALL
);
657 reg_maps
->output_registers
|= 1 << 11;
658 shader_signature_from_usage(&output_signature
[11],
659 WINED3D_DECL_USAGE_FOG
, 0, 11, WINED3DSP_WRITEMASK_0
);
663 reg_maps
->output_registers
|= 1 << 11;
664 shader_signature_from_usage(&output_signature
[11],
665 WINED3D_DECL_USAGE_PSIZE
, 0, 11, WINED3DSP_WRITEMASK_1
);
670 case WINED3DSPR_ATTROUT
:
674 if (reg_maps
->output_registers
& (1 << idx
))
676 output_signature
[idx
].mask
|= ins
.dst
[i
].write_mask
;
680 reg_maps
->output_registers
|= 1 << idx
;
681 shader_signature_from_usage(&output_signature
[idx
],
682 WINED3D_DECL_USAGE_COLOR
, idx
- 8, idx
, ins
.dst
[i
].write_mask
);
687 case WINED3DSPR_TEXCRDOUT
:
689 reg_maps
->texcoord_mask
[idx
] |= ins
.dst
[i
].write_mask
;
690 if (reg_maps
->output_registers
& (1 << idx
))
692 output_signature
[idx
].mask
|= ins
.dst
[i
].write_mask
;
696 reg_maps
->output_registers
|= 1 << idx
;
697 shader_signature_from_usage(&output_signature
[idx
],
698 WINED3D_DECL_USAGE_TEXCOORD
, idx
, idx
, ins
.dst
[i
].write_mask
);
707 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
709 if (ins
.dst
[i
].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
.dst
[i
].reg
.idx
[0].offset
)
711 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
712 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
713 * the mov and perform the sRGB write correction from the source register.
715 * However, if the mov is only partial, we can't do this, and if the write
716 * comes from an instruction other than MOV it is hard to do as well. If
717 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
718 shader
->u
.ps
.color0_mov
= FALSE
;
719 if (ins
.handler_idx
== WINED3DSIH_MOV
720 && ins
.dst
[i
].write_mask
== WINED3DSP_WRITEMASK_ALL
)
722 /* Used later when the source register is read. */
726 /* Also drop the MOV marker if the source register is overwritten prior to the shader
729 else if (ins
.dst
[i
].reg
.type
== WINED3DSPR_TEMP
730 && ins
.dst
[i
].reg
.idx
[0].offset
== shader
->u
.ps
.color0_reg
)
732 shader
->u
.ps
.color0_mov
= FALSE
;
736 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
737 if (shader_version
.major
== 1
738 && (ins
.handler_idx
== WINED3DSIH_TEX
739 || ins
.handler_idx
== WINED3DSIH_TEXBEM
740 || ins
.handler_idx
== WINED3DSIH_TEXBEML
741 || ins
.handler_idx
== WINED3DSIH_TEXDP3TEX
742 || ins
.handler_idx
== WINED3DSIH_TEXM3x2TEX
743 || ins
.handler_idx
== WINED3DSIH_TEXM3x3SPEC
744 || ins
.handler_idx
== WINED3DSIH_TEXM3x3TEX
745 || ins
.handler_idx
== WINED3DSIH_TEXM3x3VSPEC
746 || ins
.handler_idx
== WINED3DSIH_TEXREG2AR
747 || ins
.handler_idx
== WINED3DSIH_TEXREG2GB
748 || ins
.handler_idx
== WINED3DSIH_TEXREG2RGB
))
750 /* Fake sampler usage, only set reserved bit and type. */
751 DWORD sampler_code
= ins
.dst
[i
].reg
.idx
[0].offset
;
753 TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
754 reg_maps
->sampler_type
[sampler_code
] = WINED3DSTT_2D
;
756 /* texbem is only valid with < 1.4 pixel shaders */
757 if (ins
.handler_idx
== WINED3DSIH_TEXBEM
758 || ins
.handler_idx
== WINED3DSIH_TEXBEML
)
760 reg_maps
->bumpmat
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
761 if (ins
.handler_idx
== WINED3DSIH_TEXBEML
)
763 reg_maps
->luminanceparams
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
767 else if (ins
.handler_idx
== WINED3DSIH_BEM
)
769 reg_maps
->bumpmat
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
773 if (ins
.handler_idx
== WINED3DSIH_NRM
) reg_maps
->usesnrm
= 1;
774 else if (ins
.handler_idx
== WINED3DSIH_DSY
) reg_maps
->usesdsy
= 1;
775 else if (ins
.handler_idx
== WINED3DSIH_DSX
) reg_maps
->usesdsx
= 1;
776 else if (ins
.handler_idx
== WINED3DSIH_TEXLDD
) reg_maps
->usestexldd
= 1;
777 else if (ins
.handler_idx
== WINED3DSIH_TEXLDL
) reg_maps
->usestexldl
= 1;
778 else if (ins
.handler_idx
== WINED3DSIH_MOVA
) reg_maps
->usesmova
= 1;
779 else if (ins
.handler_idx
== WINED3DSIH_IFC
) reg_maps
->usesifc
= 1;
780 else if (ins
.handler_idx
== WINED3DSIH_CALL
) reg_maps
->usescall
= 1;
781 else if (ins
.handler_idx
== WINED3DSIH_POW
) reg_maps
->usespow
= 1;
782 else if (ins
.handler_idx
== WINED3DSIH_LOOP
783 || ins
.handler_idx
== WINED3DSIH_REP
)
786 if (cur_loop_depth
> max_loop_depth
)
787 max_loop_depth
= cur_loop_depth
;
789 else if (ins
.handler_idx
== WINED3DSIH_ENDLOOP
790 || ins
.handler_idx
== WINED3DSIH_ENDREP
)
794 shader_record_register_usage(shader
, reg_maps
, &ins
.predicate
->reg
, shader_version
.type
);
796 for (i
= 0; i
< ins
.src_count
; ++i
)
798 unsigned int count
= get_instr_extra_regcount(ins
.handler_idx
, i
);
799 struct wined3d_shader_register reg
= ins
.src
[i
].reg
;
801 shader_record_register_usage(shader
, reg_maps
, &ins
.src
[i
].reg
, shader_version
.type
);
805 shader_record_register_usage(shader
, reg_maps
, ®
, shader_version
.type
);
811 if (ins
.src
[i
].reg
.type
== WINED3DSPR_TEMP
812 && ins
.src
[i
].swizzle
== WINED3DSP_NOSWIZZLE
)
814 shader
->u
.ps
.color0_mov
= TRUE
;
815 shader
->u
.ps
.color0_reg
= ins
.src
[i
].reg
.idx
[0].offset
;
821 reg_maps
->loop_depth
= max_loop_depth
;
823 /* PS before 2.0 don't have explicit color outputs. Instead the value of
824 * R0 is written to the render target. */
825 if (shader_version
.major
< 2 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
826 reg_maps
->rt_mask
|= (1 << 0);
828 shader
->functionLength
= ((const char *)ptr
- (const char *)byte_code
);
833 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int max
)
835 DWORD map
= 1 << max
;
837 map
&= reg_maps
->shader_version
.major
< 3 ? ~reg_maps
->texcoord
: ~reg_maps
->input_registers
;
839 return wined3d_log2i(map
);
842 static void shader_dump_decl_usage(const struct wined3d_shader_semantic
*semantic
,
843 const struct wined3d_shader_version
*shader_version
)
847 if (semantic
->reg
.reg
.type
== WINED3DSPR_SAMPLER
)
849 switch (semantic
->sampler_type
)
851 case WINED3DSTT_2D
: TRACE("_2d"); break;
852 case WINED3DSTT_CUBE
: TRACE("_cube"); break;
853 case WINED3DSTT_VOLUME
: TRACE("_volume"); break;
854 default: TRACE("_unknown_ttype(0x%08x)", semantic
->sampler_type
);
859 /* Pixel shaders 3.0 don't have usage semantics. */
860 if (shader_version
->major
< 3 && shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
) return;
863 switch (semantic
->usage
)
865 case WINED3D_DECL_USAGE_POSITION
:
866 TRACE("position%u", semantic
->usage_idx
);
869 case WINED3D_DECL_USAGE_BLEND_INDICES
:
873 case WINED3D_DECL_USAGE_BLEND_WEIGHT
:
877 case WINED3D_DECL_USAGE_NORMAL
:
878 TRACE("normal%u", semantic
->usage_idx
);
881 case WINED3D_DECL_USAGE_PSIZE
:
885 case WINED3D_DECL_USAGE_COLOR
:
886 if (!semantic
->usage_idx
) TRACE("color");
887 else TRACE("specular%u", (semantic
->usage_idx
- 1));
890 case WINED3D_DECL_USAGE_TEXCOORD
:
891 TRACE("texture%u", semantic
->usage_idx
);
894 case WINED3D_DECL_USAGE_TANGENT
:
898 case WINED3D_DECL_USAGE_BINORMAL
:
902 case WINED3D_DECL_USAGE_TESS_FACTOR
:
906 case WINED3D_DECL_USAGE_POSITIONT
:
907 TRACE("positionT%u", semantic
->usage_idx
);
910 case WINED3D_DECL_USAGE_FOG
:
914 case WINED3D_DECL_USAGE_DEPTH
:
918 case WINED3D_DECL_USAGE_SAMPLE
:
923 FIXME("unknown_semantics(0x%08x)", semantic
->usage
);
928 static void shader_dump_register(const struct wined3d_shader_register
*reg
,
929 const struct wined3d_shader_version
*shader_version
)
931 static const char * const rastout_reg_names
[] = {"oPos", "oFog", "oPts"};
932 static const char * const misctype_reg_names
[] = {"vPos", "vFace"};
933 UINT offset
= reg
->idx
[0].offset
;
937 case WINED3DSPR_TEMP
:
941 case WINED3DSPR_INPUT
:
945 case WINED3DSPR_CONST
:
946 case WINED3DSPR_CONST2
:
947 case WINED3DSPR_CONST3
:
948 case WINED3DSPR_CONST4
:
950 offset
= shader_get_float_offset(reg
->type
, offset
);
953 case WINED3DSPR_TEXTURE
: /* vs: case WINED3DSPR_ADDR */
954 TRACE("%c", shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
? 't' : 'a');
957 case WINED3DSPR_RASTOUT
:
958 TRACE("%s", rastout_reg_names
[offset
]);
961 case WINED3DSPR_COLOROUT
:
965 case WINED3DSPR_DEPTHOUT
:
969 case WINED3DSPR_ATTROUT
:
973 case WINED3DSPR_TEXCRDOUT
:
974 /* Vertex shaders >= 3.0 use general purpose output registers
975 * (WINED3DSPR_OUTPUT), which can include an address token. */
976 if (shader_version
->major
>= 3) TRACE("o");
980 case WINED3DSPR_CONSTINT
:
984 case WINED3DSPR_CONSTBOOL
:
988 case WINED3DSPR_LABEL
:
992 case WINED3DSPR_LOOP
:
996 case WINED3DSPR_SAMPLER
:
1000 case WINED3DSPR_MISCTYPE
:
1002 FIXME("Unhandled misctype register %u.\n", offset
);
1004 TRACE("%s", misctype_reg_names
[offset
]);
1007 case WINED3DSPR_PREDICATE
:
1011 case WINED3DSPR_IMMCONST
:
1015 case WINED3DSPR_CONSTBUFFER
:
1019 case WINED3DSPR_PRIMID
:
1023 case WINED3DSPR_NULL
:
1027 case WINED3DSPR_RESOURCE
:
1032 TRACE("unhandled_rtype(%#x)", reg
->type
);
1036 if (reg
->type
== WINED3DSPR_IMMCONST
)
1039 switch (reg
->immconst_type
)
1041 case WINED3D_IMMCONST_SCALAR
:
1042 switch (reg
->data_type
)
1044 case WINED3D_DATA_FLOAT
:
1045 TRACE("%.8e", *(const float *)reg
->immconst_data
);
1047 case WINED3D_DATA_INT
:
1048 TRACE("%d", reg
->immconst_data
[0]);
1050 case WINED3D_DATA_RESOURCE
:
1051 case WINED3D_DATA_SAMPLER
:
1052 case WINED3D_DATA_UINT
:
1053 TRACE("%u", reg
->immconst_data
[0]);
1056 TRACE("<unhandled data type %#x>", reg
->data_type
);
1061 case WINED3D_IMMCONST_VEC4
:
1062 switch (reg
->data_type
)
1064 case WINED3D_DATA_FLOAT
:
1065 TRACE("%.8e, %.8e, %.8e, %.8e",
1066 *(const float *)®
->immconst_data
[0], *(const float *)®
->immconst_data
[1],
1067 *(const float *)®
->immconst_data
[2], *(const float *)®
->immconst_data
[3]);
1069 case WINED3D_DATA_INT
:
1070 TRACE("%d, %d, %d, %d",
1071 reg
->immconst_data
[0], reg
->immconst_data
[1],
1072 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1074 case WINED3D_DATA_RESOURCE
:
1075 case WINED3D_DATA_SAMPLER
:
1076 case WINED3D_DATA_UINT
:
1077 TRACE("%u, %u, %u, %u",
1078 reg
->immconst_data
[0], reg
->immconst_data
[1],
1079 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1082 TRACE("<unhandled data type %#x>", reg
->data_type
);
1088 TRACE("<unhandled immconst_type %#x>", reg
->immconst_type
);
1093 else if (reg
->type
!= WINED3DSPR_RASTOUT
1094 && reg
->type
!= WINED3DSPR_MISCTYPE
1095 && reg
->type
!= WINED3DSPR_NULL
)
1100 if (reg
->idx
[0].rel_addr
)
1102 shader_dump_src_param(reg
->idx
[0].rel_addr
, shader_version
);
1105 TRACE("%u]", offset
);
1107 if (reg
->idx
[1].offset
!= ~0U)
1110 if (reg
->idx
[1].rel_addr
)
1112 shader_dump_src_param(reg
->idx
[1].rel_addr
, shader_version
);
1115 TRACE("%u]", reg
->idx
[1].offset
);
1121 void shader_dump_dst_param(const struct wined3d_shader_dst_param
*param
,
1122 const struct wined3d_shader_version
*shader_version
)
1124 DWORD write_mask
= param
->write_mask
;
1126 shader_dump_register(¶m
->reg
, shader_version
);
1128 if (write_mask
&& write_mask
!= WINED3DSP_WRITEMASK_ALL
)
1130 static const char *write_mask_chars
= "xyzw";
1133 if (write_mask
& WINED3DSP_WRITEMASK_0
) TRACE("%c", write_mask_chars
[0]);
1134 if (write_mask
& WINED3DSP_WRITEMASK_1
) TRACE("%c", write_mask_chars
[1]);
1135 if (write_mask
& WINED3DSP_WRITEMASK_2
) TRACE("%c", write_mask_chars
[2]);
1136 if (write_mask
& WINED3DSP_WRITEMASK_3
) TRACE("%c", write_mask_chars
[3]);
1140 void shader_dump_src_param(const struct wined3d_shader_src_param
*param
,
1141 const struct wined3d_shader_version
*shader_version
)
1143 enum wined3d_shader_src_modifier src_modifier
= param
->modifiers
;
1144 DWORD swizzle
= param
->swizzle
;
1146 if (src_modifier
== WINED3DSPSM_NEG
1147 || src_modifier
== WINED3DSPSM_BIASNEG
1148 || src_modifier
== WINED3DSPSM_SIGNNEG
1149 || src_modifier
== WINED3DSPSM_X2NEG
1150 || src_modifier
== WINED3DSPSM_ABSNEG
)
1152 else if (src_modifier
== WINED3DSPSM_COMP
)
1154 else if (src_modifier
== WINED3DSPSM_NOT
)
1157 if (src_modifier
== WINED3DSPSM_ABS
|| src_modifier
== WINED3DSPSM_ABSNEG
)
1160 shader_dump_register(¶m
->reg
, shader_version
);
1164 switch (src_modifier
)
1166 case WINED3DSPSM_NONE
: break;
1167 case WINED3DSPSM_NEG
: break;
1168 case WINED3DSPSM_NOT
: break;
1169 case WINED3DSPSM_BIAS
: TRACE("_bias"); break;
1170 case WINED3DSPSM_BIASNEG
: TRACE("_bias"); break;
1171 case WINED3DSPSM_SIGN
: TRACE("_bx2"); break;
1172 case WINED3DSPSM_SIGNNEG
: TRACE("_bx2"); break;
1173 case WINED3DSPSM_COMP
: break;
1174 case WINED3DSPSM_X2
: TRACE("_x2"); break;
1175 case WINED3DSPSM_X2NEG
: TRACE("_x2"); break;
1176 case WINED3DSPSM_DZ
: TRACE("_dz"); break;
1177 case WINED3DSPSM_DW
: TRACE("_dw"); break;
1178 case WINED3DSPSM_ABSNEG
: TRACE(")"); break;
1179 case WINED3DSPSM_ABS
: TRACE(")"); break;
1180 default: TRACE("_unknown_modifier(%#x)", src_modifier
);
1184 if (swizzle
!= WINED3DSP_NOSWIZZLE
)
1186 static const char *swizzle_chars
= "xyzw";
1187 DWORD swizzle_x
= swizzle
& 0x03;
1188 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
1189 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
1190 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
1192 if (swizzle_x
== swizzle_y
1193 && swizzle_x
== swizzle_z
1194 && swizzle_x
== swizzle_w
)
1196 TRACE(".%c", swizzle_chars
[swizzle_x
]);
1200 TRACE(".%c%c%c%c", swizzle_chars
[swizzle_x
], swizzle_chars
[swizzle_y
],
1201 swizzle_chars
[swizzle_z
], swizzle_chars
[swizzle_w
]);
1206 /* Shared code in order to generate the bulk of the shader string.
1207 * NOTE: A description of how to parse tokens can be found on MSDN. */
1208 void shader_generate_main(const struct wined3d_shader
*shader
, struct wined3d_shader_buffer
*buffer
,
1209 const struct wined3d_shader_reg_maps
*reg_maps
, const DWORD
*byte_code
, void *backend_ctx
)
1211 struct wined3d_device
*device
= shader
->device
;
1212 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
1213 void *fe_data
= shader
->frontend_data
;
1214 struct wined3d_shader_version shader_version
;
1215 struct wined3d_shader_loop_state loop_state
;
1216 struct wined3d_shader_instruction ins
;
1217 struct wined3d_shader_tex_mx tex_mx
;
1218 struct wined3d_shader_context ctx
;
1219 const DWORD
*ptr
= byte_code
;
1221 /* Initialize current parsing state. */
1222 tex_mx
.current_row
= 0;
1223 loop_state
.current_depth
= 0;
1224 loop_state
.current_reg
= 0;
1226 ctx
.shader
= shader
;
1227 ctx
.gl_info
= &device
->adapter
->gl_info
;
1228 ctx
.reg_maps
= reg_maps
;
1229 ctx
.buffer
= buffer
;
1230 ctx
.tex_mx
= &tex_mx
;
1231 ctx
.loop_state
= &loop_state
;
1232 ctx
.backend_data
= backend_ctx
;
1235 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1237 while (!fe
->shader_is_end(fe_data
, &ptr
))
1240 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1242 /* Unknown opcode and its parameters. */
1243 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1245 TRACE("Skipping unrecognized instruction.\n");
1250 FIXME("Predicates not implemented.\n");
1252 /* Call appropriate function for output target */
1253 device
->shader_backend
->shader_handle_instruction(&ins
);
1257 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param
*dst
)
1259 DWORD mmask
= dst
->modifiers
;
1264 case 13: TRACE("_d8"); break;
1265 case 14: TRACE("_d4"); break;
1266 case 15: TRACE("_d2"); break;
1267 case 1: TRACE("_x2"); break;
1268 case 2: TRACE("_x4"); break;
1269 case 3: TRACE("_x8"); break;
1270 default: TRACE("_unhandled_shift(%d)", dst
->shift
); break;
1273 if (mmask
& WINED3DSPDM_SATURATE
) TRACE("_sat");
1274 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) TRACE("_pp");
1275 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) TRACE("_centroid");
1277 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
1278 if (mmask
) FIXME("_unrecognized_modifier(%#x)", mmask
);
1281 static void shader_dump_primitive_type(enum wined3d_primitive_type primitive_type
)
1283 switch (primitive_type
)
1285 case WINED3D_PT_UNDEFINED
:
1288 case WINED3D_PT_POINTLIST
:
1291 case WINED3D_PT_LINELIST
:
1294 case WINED3D_PT_LINESTRIP
:
1297 case WINED3D_PT_TRIANGLELIST
:
1298 TRACE("trianglelist");
1300 case WINED3D_PT_TRIANGLESTRIP
:
1301 TRACE("trianglestrip");
1303 case WINED3D_PT_TRIANGLEFAN
:
1304 TRACE("trianglefan");
1306 case WINED3D_PT_LINELIST_ADJ
:
1307 TRACE("linelist_adj");
1309 case WINED3D_PT_LINESTRIP_ADJ
:
1310 TRACE("linestrip_adj");
1312 case WINED3D_PT_TRIANGLELIST_ADJ
:
1313 TRACE("trianglelist_adj");
1315 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
1316 TRACE("trianglestrip_adj");
1319 TRACE("<unrecognized_primitive_type %#x>", primitive_type
);
1324 static void shader_trace_init(const struct wined3d_shader_frontend
*fe
, void *fe_data
, const DWORD
*byte_code
)
1326 struct wined3d_shader_version shader_version
;
1327 const DWORD
*ptr
= byte_code
;
1328 const char *type_prefix
;
1331 TRACE("Parsing %p.\n", byte_code
);
1333 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1335 switch (shader_version
.type
)
1337 case WINED3D_SHADER_TYPE_VERTEX
:
1341 case WINED3D_SHADER_TYPE_GEOMETRY
:
1345 case WINED3D_SHADER_TYPE_PIXEL
:
1350 FIXME("Unhandled shader type %#x.\n", shader_version
.type
);
1351 type_prefix
= "unknown";
1355 TRACE("%s_%u_%u\n", type_prefix
, shader_version
.major
, shader_version
.minor
);
1357 while (!fe
->shader_is_end(fe_data
, &ptr
))
1359 struct wined3d_shader_instruction ins
;
1361 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1362 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1364 TRACE("Skipping unrecognized instruction.\n");
1368 if (ins
.handler_idx
== WINED3DSIH_DCL
)
1370 shader_dump_decl_usage(&ins
.declaration
.semantic
, &shader_version
);
1371 shader_dump_ins_modifiers(&ins
.declaration
.semantic
.reg
);
1373 shader_dump_dst_param(&ins
.declaration
.semantic
.reg
, &shader_version
);
1375 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
1377 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1378 shader_dump_src_param(&ins
.declaration
.src
, &shader_version
);
1379 TRACE(", %s", ins
.flags
& WINED3DSI_INDEXED_DYNAMIC
? "dynamicIndexed" : "immediateIndexed");
1381 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
1382 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
1384 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1385 shader_dump_primitive_type(ins
.declaration
.primitive_type
);
1387 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
1389 TRACE("%s %u", shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.count
);
1391 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
1393 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(ins
.dst
[0].reg
.type
,
1394 ins
.dst
[0].reg
.idx
[0].offset
),
1395 *(const float *)&ins
.src
[0].reg
.immconst_data
[0],
1396 *(const float *)&ins
.src
[0].reg
.immconst_data
[1],
1397 *(const float *)&ins
.src
[0].reg
.immconst_data
[2],
1398 *(const float *)&ins
.src
[0].reg
.immconst_data
[3]);
1400 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
1402 TRACE("defi i%u = %d, %d, %d, %d", ins
.dst
[0].reg
.idx
[0].offset
,
1403 ins
.src
[0].reg
.immconst_data
[0],
1404 ins
.src
[0].reg
.immconst_data
[1],
1405 ins
.src
[0].reg
.immconst_data
[2],
1406 ins
.src
[0].reg
.immconst_data
[3]);
1408 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
1410 TRACE("defb b%u = %s", ins
.dst
[0].reg
.idx
[0].offset
, ins
.src
[0].reg
.immconst_data
[0] ? "true" : "false");
1417 shader_dump_src_param(ins
.predicate
, &shader_version
);
1421 /* PixWin marks instructions with the coissue flag with a '+' */
1422 if (ins
.coissue
) TRACE("+");
1424 TRACE("%s", shader_opcode_names
[ins
.handler_idx
]);
1426 if (ins
.handler_idx
== WINED3DSIH_IFC
1427 || ins
.handler_idx
== WINED3DSIH_BREAKC
)
1431 case WINED3D_SHADER_REL_OP_GT
: TRACE("_gt"); break;
1432 case WINED3D_SHADER_REL_OP_EQ
: TRACE("_eq"); break;
1433 case WINED3D_SHADER_REL_OP_GE
: TRACE("_ge"); break;
1434 case WINED3D_SHADER_REL_OP_LT
: TRACE("_lt"); break;
1435 case WINED3D_SHADER_REL_OP_NE
: TRACE("_ne"); break;
1436 case WINED3D_SHADER_REL_OP_LE
: TRACE("_le"); break;
1437 default: TRACE("_(%u)", ins
.flags
);
1440 else if (ins
.handler_idx
== WINED3DSIH_TEX
1441 && shader_version
.major
>= 2
1442 && (ins
.flags
& WINED3DSI_TEXLD_PROJECT
))
1447 for (i
= 0; i
< ins
.dst_count
; ++i
)
1449 shader_dump_ins_modifiers(&ins
.dst
[i
]);
1450 TRACE(!i
? " " : ", ");
1451 shader_dump_dst_param(&ins
.dst
[i
], &shader_version
);
1454 /* Other source tokens */
1455 for (i
= ins
.dst_count
; i
< (ins
.dst_count
+ ins
.src_count
); ++i
)
1457 TRACE(!i
? " " : ", ");
1458 shader_dump_src_param(&ins
.src
[i
- ins
.dst_count
], &shader_version
);
1465 static void shader_cleanup(struct wined3d_shader
*shader
)
1467 shader
->device
->shader_backend
->shader_destroy(shader
);
1468 HeapFree(GetProcessHeap(), 0, shader
->reg_maps
.constf
);
1469 HeapFree(GetProcessHeap(), 0, shader
->function
);
1470 shader_delete_constant_list(&shader
->constantsF
);
1471 shader_delete_constant_list(&shader
->constantsB
);
1472 shader_delete_constant_list(&shader
->constantsI
);
1473 list_remove(&shader
->shader_list_entry
);
1475 if (shader
->frontend
&& shader
->frontend_data
)
1476 shader
->frontend
->shader_free(shader
->frontend_data
);
1479 struct shader_none_priv
1481 const struct fragment_pipeline
*fragment_pipe
;
1482 BOOL ffp_proj_control
;
1485 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
1486 static void shader_none_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
1487 enum tex_types tex_type
, const SIZE
*ds_mask_size
) {}
1488 static void shader_none_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
) {}
1489 static void shader_none_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1490 static void shader_none_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1491 static void shader_none_load_constants(const struct wined3d_context
*context
, BOOL usePS
, BOOL useVS
) {}
1492 static void shader_none_load_np2fixup_constants(void *shader_priv
,
1493 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
) {}
1494 static void shader_none_destroy(struct wined3d_shader
*shader
) {}
1495 static void shader_none_context_destroyed(void *shader_priv
, const struct wined3d_context
*context
) {}
1497 static void shader_none_select(const struct wined3d_context
*context
, enum wined3d_shader_mode vertex_mode
,
1498 enum wined3d_shader_mode fragment_mode
)
1500 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1501 struct wined3d_device
*device
= context
->swapchain
->device
;
1502 struct shader_none_priv
*priv
= device
->shader_priv
;
1504 priv
->fragment_pipe
->enable_extension(gl_info
, fragment_mode
== WINED3D_SHADER_MODE_FFP
);
1507 static HRESULT
shader_none_alloc(struct wined3d_device
*device
, const struct fragment_pipeline
*fragment_pipe
)
1509 struct fragment_caps fragment_caps
;
1510 struct shader_none_priv
*priv
;
1511 void *fragment_priv
;
1513 if (!(priv
= HeapAlloc(GetProcessHeap(), 0, sizeof(*priv
))))
1515 ERR("Failed to allocate private data.\n");
1516 return E_OUTOFMEMORY
;
1519 if (!(fragment_priv
= fragment_pipe
->alloc_private(&none_shader_backend
, priv
)))
1521 ERR("Failed to initialize fragment pipe.\n");
1522 HeapFree(GetProcessHeap(), 0, priv
);
1526 fragment_pipe
->get_caps(&device
->adapter
->gl_info
, &fragment_caps
);
1527 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
1528 device
->fragment_priv
= fragment_priv
;
1529 priv
->fragment_pipe
= fragment_pipe
;
1530 device
->shader_priv
= priv
;
1535 static void shader_none_free(struct wined3d_device
*device
)
1537 struct shader_none_priv
*priv
= device
->shader_priv
;
1539 priv
->fragment_pipe
->free_private(device
);
1540 HeapFree(GetProcessHeap(), 0, priv
);
1543 static void shader_none_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
1545 /* Set the shader caps to 0 for the none shader backend */
1546 caps
->vs_version
= 0;
1547 caps
->gs_version
= 0;
1548 caps
->ps_version
= 0;
1549 caps
->vs_uniform_count
= 0;
1550 caps
->ps_uniform_count
= 0;
1551 caps
->ps_1x_max_value
= 0.0f
;
1552 caps
->wined3d_caps
= 0;
1555 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
1557 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
1559 TRACE("Checking support for fixup:\n");
1560 dump_color_fixup_desc(fixup
);
1563 /* Faked to make some apps happy. */
1564 if (!is_complex_fixup(fixup
))
1570 TRACE("[FAILED]\n");
1574 static BOOL
shader_none_has_ffp_proj_control(void *shader_priv
)
1576 struct shader_none_priv
*priv
= shader_priv
;
1578 return priv
->ffp_proj_control
;
1581 const struct wined3d_shader_backend_ops none_shader_backend
=
1583 shader_none_handle_instruction
,
1585 shader_none_select_depth_blt
,
1586 shader_none_deselect_depth_blt
,
1587 shader_none_update_float_vertex_constants
,
1588 shader_none_update_float_pixel_constants
,
1589 shader_none_load_constants
,
1590 shader_none_load_np2fixup_constants
,
1591 shader_none_destroy
,
1594 shader_none_context_destroyed
,
1595 shader_none_get_caps
,
1596 shader_none_color_fixup_supported
,
1597 shader_none_has_ffp_proj_control
,
1600 static HRESULT
shader_set_function(struct wined3d_shader
*shader
, const DWORD
*byte_code
,
1601 const struct wined3d_shader_signature
*output_signature
, DWORD float_const_count
,
1602 enum wined3d_shader_type type
, unsigned int max_version
)
1604 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1605 const struct wined3d_shader_frontend
*fe
;
1607 unsigned int backend_version
;
1609 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1610 shader
, byte_code
, output_signature
, float_const_count
);
1612 fe
= shader_select_frontend(*byte_code
);
1615 FIXME("Unable to find frontend for shader.\n");
1616 return WINED3DERR_INVALIDCALL
;
1618 shader
->frontend
= fe
;
1619 shader
->frontend_data
= fe
->shader_init(byte_code
, output_signature
);
1620 if (!shader
->frontend_data
)
1622 FIXME("Failed to initialize frontend.\n");
1623 return WINED3DERR_INVALIDCALL
;
1626 /* First pass: trace shader. */
1627 if (TRACE_ON(d3d_shader
))
1628 shader_trace_init(fe
, shader
->frontend_data
, byte_code
);
1630 /* Initialize immediate constant lists. */
1631 list_init(&shader
->constantsF
);
1632 list_init(&shader
->constantsB
);
1633 list_init(&shader
->constantsI
);
1635 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1636 hr
= shader_get_registers_used(shader
, fe
,
1637 reg_maps
, shader
->input_signature
, shader
->output_signature
,
1638 byte_code
, float_const_count
);
1639 if (FAILED(hr
)) return hr
;
1641 if (reg_maps
->shader_version
.type
!= type
)
1643 WARN("Wrong shader type %d.\n", reg_maps
->shader_version
.type
);
1644 return WINED3DERR_INVALIDCALL
;
1646 if (reg_maps
->shader_version
.major
> max_version
)
1648 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps
->shader_version
.major
);
1649 return WINED3DERR_INVALIDCALL
;
1653 case WINED3D_SHADER_TYPE_VERTEX
:
1654 backend_version
= shader
->device
->vs_version
;
1656 case WINED3D_SHADER_TYPE_GEOMETRY
:
1657 backend_version
= shader
->device
->gs_version
;
1659 case WINED3D_SHADER_TYPE_PIXEL
:
1660 backend_version
= shader
->device
->ps_version
;
1663 FIXME("No backend version-checking for this shader type\n");
1664 backend_version
= 0;
1666 if (reg_maps
->shader_version
.major
> backend_version
)
1668 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
1669 reg_maps
->shader_version
.major
, reg_maps
->shader_version
.minor
);
1670 return WINED3DERR_INVALIDCALL
;
1673 shader
->function
= HeapAlloc(GetProcessHeap(), 0, shader
->functionLength
);
1674 if (!shader
->function
)
1675 return E_OUTOFMEMORY
;
1676 memcpy(shader
->function
, byte_code
, shader
->functionLength
);
1681 ULONG CDECL
wined3d_shader_incref(struct wined3d_shader
*shader
)
1683 ULONG refcount
= InterlockedIncrement(&shader
->ref
);
1685 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
1690 /* Do not call while under the GL lock. */
1691 ULONG CDECL
wined3d_shader_decref(struct wined3d_shader
*shader
)
1693 ULONG refcount
= InterlockedDecrement(&shader
->ref
);
1695 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
1699 shader_cleanup(shader
);
1700 shader
->parent_ops
->wined3d_object_destroyed(shader
->parent
);
1701 HeapFree(GetProcessHeap(), 0, shader
);
1707 void * CDECL
wined3d_shader_get_parent(const struct wined3d_shader
*shader
)
1709 TRACE("shader %p.\n", shader
);
1711 return shader
->parent
;
1714 HRESULT CDECL
wined3d_shader_get_byte_code(const struct wined3d_shader
*shader
,
1715 void *byte_code
, UINT
*byte_code_size
)
1717 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader
, byte_code
, byte_code_size
);
1721 *byte_code_size
= shader
->functionLength
;
1725 if (*byte_code_size
< shader
->functionLength
)
1727 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
1728 * than the required size we should write the required size and
1729 * return D3DERR_MOREDATA. That's not actually true. */
1730 return WINED3DERR_INVALIDCALL
;
1733 memcpy(byte_code
, shader
->function
, shader
->functionLength
);
1738 /* Set local constants for d3d8 shaders. */
1739 HRESULT CDECL
wined3d_shader_set_local_constants_float(struct wined3d_shader
*shader
,
1740 UINT start_idx
, const float *src_data
, UINT count
)
1742 UINT end_idx
= start_idx
+ count
;
1745 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader
, start_idx
, src_data
, count
);
1747 if (end_idx
> shader
->limits
.constant_float
)
1749 WARN("end_idx %u > float constants limit %u.\n",
1750 end_idx
, shader
->limits
.constant_float
);
1751 end_idx
= shader
->limits
.constant_float
;
1754 for (i
= start_idx
; i
< end_idx
; ++i
)
1756 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
1758 return E_OUTOFMEMORY
;
1761 memcpy(lconst
->value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
1762 list_add_head(&shader
->constantsF
, &lconst
->entry
);
1768 void find_vs_compile_args(const struct wined3d_state
*state
,
1769 const struct wined3d_shader
*shader
, struct vs_compile_args
*args
)
1771 args
->fog_src
= state
->render_states
[WINED3D_RS_FOGTABLEMODE
]
1772 == WINED3D_FOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
1773 args
->clip_enabled
= state
->render_states
[WINED3D_RS_CLIPPING
]
1774 && state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
];
1775 args
->swizzle_map
= shader
->device
->strided_streams
.swizzle_map
;
1778 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
1780 if (usage_idx1
!= usage_idx2
)
1782 if (usage1
== usage2
)
1784 if (usage1
== WINED3D_DECL_USAGE_POSITION
&& usage2
== WINED3D_DECL_USAGE_POSITIONT
)
1786 if (usage2
== WINED3D_DECL_USAGE_POSITION
&& usage1
== WINED3D_DECL_USAGE_POSITIONT
)
1792 BOOL
vshader_get_input(const struct wined3d_shader
*shader
,
1793 BYTE usage_req
, BYTE usage_idx_req
, unsigned int *regnum
)
1795 WORD map
= shader
->reg_maps
.input_registers
;
1798 for (i
= 0; map
; map
>>= 1, ++i
)
1800 if (!(map
& 1)) continue;
1802 if (match_usage(shader
->u
.vs
.attributes
[i
].usage
,
1803 shader
->u
.vs
.attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
1812 static void vertexshader_set_limits(struct wined3d_shader
*shader
)
1814 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
1815 shader
->reg_maps
.shader_version
.minor
);
1816 struct wined3d_device
*device
= shader
->device
;
1818 shader
->limits
.packed_input
= 0;
1820 switch (shader_version
)
1822 case WINED3D_SHADER_VERSION(1, 0):
1823 case WINED3D_SHADER_VERSION(1, 1):
1824 shader
->limits
.constant_bool
= 0;
1825 shader
->limits
.constant_int
= 0;
1826 shader
->limits
.packed_output
= 12;
1827 shader
->limits
.sampler
= 0;
1828 /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1829 * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1831 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1834 case WINED3D_SHADER_VERSION(2, 0):
1835 case WINED3D_SHADER_VERSION(2, 1):
1836 shader
->limits
.constant_bool
= 16;
1837 shader
->limits
.constant_int
= 16;
1838 shader
->limits
.packed_output
= 12;
1839 shader
->limits
.sampler
= 0;
1840 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1843 case WINED3D_SHADER_VERSION(3, 0):
1844 shader
->limits
.constant_bool
= 16;
1845 shader
->limits
.constant_int
= 16;
1846 shader
->limits
.packed_output
= 12;
1847 shader
->limits
.sampler
= 4;
1848 /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1849 * even though they are capable of supporting much more (GL
1850 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1851 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1852 * shaders to 256. */
1853 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1856 case WINED3D_SHADER_VERSION(4, 0):
1857 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
1858 shader
->limits
.constant_int
= 0;
1859 shader
->limits
.constant_float
= 0;
1860 shader
->limits
.constant_bool
= 0;
1861 shader
->limits
.packed_output
= 16;
1862 shader
->limits
.packed_input
= 0;
1866 shader
->limits
.constant_bool
= 16;
1867 shader
->limits
.constant_int
= 16;
1868 shader
->limits
.packed_output
= 12;
1869 shader
->limits
.sampler
= 0;
1870 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1871 FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1872 shader
->reg_maps
.shader_version
.major
,
1873 shader
->reg_maps
.shader_version
.minor
);
1877 static HRESULT
vertexshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
1878 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
1879 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
1881 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1886 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
1888 shader_init(shader
, device
, parent
, parent_ops
);
1889 hr
= shader_set_function(shader
, byte_code
, output_signature
, device
->d3d_vshader_constantF
,
1890 WINED3D_SHADER_TYPE_VERTEX
, max_version
);
1893 WARN("Failed to set function, hr %#x.\n", hr
);
1894 shader_cleanup(shader
);
1898 map
= reg_maps
->input_registers
;
1899 for (i
= 0; map
; map
>>= 1, ++i
)
1901 if (!(map
& 1) || !shader
->input_signature
[i
].semantic_name
)
1904 shader
->u
.vs
.attributes
[i
].usage
=
1905 shader_usage_from_semantic_name(shader
->input_signature
[i
].semantic_name
);
1906 shader
->u
.vs
.attributes
[i
].usage_idx
= shader
->input_signature
[i
].semantic_idx
;
1909 if (output_signature
)
1911 for (i
= 0; i
< output_signature
->element_count
; ++i
)
1913 struct wined3d_shader_signature_element
*e
= &output_signature
->elements
[i
];
1914 reg_maps
->output_registers
|= 1 << e
->register_idx
;
1915 shader
->output_signature
[e
->register_idx
] = *e
;
1919 vertexshader_set_limits(shader
);
1921 shader
->load_local_constsF
= reg_maps
->usesrelconstF
1922 && !list_empty(&shader
->constantsF
);
1927 static void geometryshader_set_limits(struct wined3d_shader
*shader
)
1929 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
1930 shader
->reg_maps
.shader_version
.minor
);
1932 switch (shader_version
)
1934 case WINED3D_SHADER_VERSION(4, 0):
1935 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
1936 shader
->limits
.constant_int
= 0;
1937 shader
->limits
.constant_float
= 0;
1938 shader
->limits
.constant_bool
= 0;
1939 shader
->limits
.packed_output
= 32;
1940 shader
->limits
.packed_input
= 16;
1944 memset(&shader
->limits
, 0, sizeof(shader
->limits
));
1945 FIXME("Unhandled geometry shader version \"%u.%u\".\n",
1946 shader
->reg_maps
.shader_version
.major
,
1947 shader
->reg_maps
.shader_version
.minor
);
1951 static HRESULT
geometryshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
1952 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
1953 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
1957 shader_init(shader
, device
, parent
, parent_ops
);
1958 hr
= shader_set_function(shader
, byte_code
, output_signature
, 0,
1959 WINED3D_SHADER_TYPE_GEOMETRY
, max_version
);
1962 WARN("Failed to set function, hr %#x.\n", hr
);
1963 shader_cleanup(shader
);
1967 geometryshader_set_limits(shader
);
1969 shader
->load_local_constsF
= FALSE
;
1974 void find_ps_compile_args(const struct wined3d_state
*state
,
1975 const struct wined3d_shader
*shader
, struct ps_compile_args
*args
)
1977 struct wined3d_device
*device
= shader
->device
;
1978 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1979 const struct wined3d_texture
*texture
;
1982 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
1983 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && state
->render_states
[WINED3D_RS_SRGBWRITEENABLE
])
1985 const struct wined3d_surface
*rt
= state
->fb
->render_targets
[0];
1986 if (rt
->resource
.format
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
1988 static unsigned int warned
= 0;
1990 args
->srgb_correction
= 1;
1991 if (state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] && !warned
++)
1992 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
1993 "support, expect rendering artifacts.\n");
1997 if (shader
->reg_maps
.shader_version
.major
== 1
1998 && shader
->reg_maps
.shader_version
.minor
<= 3)
2000 for (i
= 0; i
< 4; ++i
)
2002 DWORD flags
= state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
];
2004 if (flags
& WINED3D_TTFF_PROJECTED
)
2006 DWORD tex_transform
= flags
& ~WINED3D_TTFF_PROJECTED
;
2008 if (!state
->vertex_shader
)
2011 unsigned int index
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
2012 DWORD max_valid
= WINED3D_TTFF_COUNT4
;
2013 enum wined3d_sampler_texture_type sampler_type
= shader
->reg_maps
.sampler_type
[i
];
2015 for (j
= 0; j
< state
->vertex_declaration
->element_count
; ++j
)
2017 struct wined3d_vertex_declaration_element
*element
=
2018 &state
->vertex_declaration
->elements
[j
];
2020 if (element
->usage
== WINED3D_DECL_USAGE_TEXCOORD
2021 && element
->usage_idx
== index
)
2023 max_valid
= element
->format
->component_count
;
2027 if (!tex_transform
|| tex_transform
> max_valid
)
2029 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2030 tex_transform
, max_valid
);
2031 tex_transform
= max_valid
;
2033 if ((sampler_type
== WINED3DSTT_1D
&& tex_transform
> WINED3D_TTFF_COUNT1
)
2034 || (sampler_type
== WINED3DSTT_2D
&& tex_transform
> WINED3D_TTFF_COUNT2
)
2035 || (sampler_type
== WINED3DSTT_VOLUME
&& tex_transform
> WINED3D_TTFF_COUNT3
))
2036 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
2039 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2040 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2041 i
, tex_transform
, sampler_type
);
2045 tex_transform
= WINED3D_TTFF_COUNT4
| WINED3D_PSARGS_PROJECTED
;
2047 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
2052 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2054 if (!shader
->reg_maps
.sampler_type
[i
])
2057 texture
= state
->textures
[i
];
2060 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
2063 args
->color_fixup
[i
] = texture
->resource
.format
->color_fixup
;
2065 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
2066 args
->shadow
|= 1 << i
;
2068 /* Flag samplers that need NP2 texcoord fixup. */
2069 if (!(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
2070 args
->np2_fixup
|= (1 << i
);
2072 if (shader
->reg_maps
.shader_version
.major
>= 3)
2074 if (device
->strided_streams
.position_transformed
)
2076 args
->vp_mode
= pretransformed
;
2078 else if (use_vs(state
))
2080 args
->vp_mode
= vertexshader
;
2084 args
->vp_mode
= fixedfunction
;
2086 args
->fog
= FOG_OFF
;
2090 args
->vp_mode
= vertexshader
;
2091 if (state
->render_states
[WINED3D_RS_FOGENABLE
])
2093 switch (state
->render_states
[WINED3D_RS_FOGTABLEMODE
])
2095 case WINED3D_FOG_NONE
:
2096 if (device
->strided_streams
.position_transformed
|| use_vs(state
))
2098 args
->fog
= FOG_LINEAR
;
2102 switch (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
])
2104 case WINED3D_FOG_NONE
: /* Fall through. */
2105 case WINED3D_FOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
2106 case WINED3D_FOG_EXP
: args
->fog
= FOG_EXP
; break;
2107 case WINED3D_FOG_EXP2
: args
->fog
= FOG_EXP2
; break;
2111 case WINED3D_FOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
2112 case WINED3D_FOG_EXP
: args
->fog
= FOG_EXP
; break;
2113 case WINED3D_FOG_EXP2
: args
->fog
= FOG_EXP2
; break;
2118 args
->fog
= FOG_OFF
;
2123 static void pixelshader_set_limits(struct wined3d_shader
*shader
)
2125 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
2126 shader
->reg_maps
.shader_version
.minor
);
2128 shader
->limits
.packed_output
= 0;
2130 switch (shader_version
)
2132 case WINED3D_SHADER_VERSION(1, 0):
2133 case WINED3D_SHADER_VERSION(1, 1):
2134 case WINED3D_SHADER_VERSION(1, 2):
2135 case WINED3D_SHADER_VERSION(1, 3):
2136 shader
->limits
.constant_float
= 8;
2137 shader
->limits
.constant_int
= 0;
2138 shader
->limits
.constant_bool
= 0;
2139 shader
->limits
.sampler
= 4;
2140 shader
->limits
.packed_input
= 0;
2143 case WINED3D_SHADER_VERSION(1, 4):
2144 shader
->limits
.constant_float
= 8;
2145 shader
->limits
.constant_int
= 0;
2146 shader
->limits
.constant_bool
= 0;
2147 shader
->limits
.sampler
= 6;
2148 shader
->limits
.packed_input
= 0;
2151 /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2152 case WINED3D_SHADER_VERSION(2, 0):
2153 shader
->limits
.constant_float
= 32;
2154 shader
->limits
.constant_int
= 16;
2155 shader
->limits
.constant_bool
= 16;
2156 shader
->limits
.sampler
= 16;
2157 shader
->limits
.packed_input
= 0;
2160 case WINED3D_SHADER_VERSION(2, 1):
2161 shader
->limits
.constant_float
= 32;
2162 shader
->limits
.constant_int
= 16;
2163 shader
->limits
.constant_bool
= 16;
2164 shader
->limits
.sampler
= 16;
2165 shader
->limits
.packed_input
= 0;
2168 case WINED3D_SHADER_VERSION(3, 0):
2169 shader
->limits
.constant_float
= 224;
2170 shader
->limits
.constant_int
= 16;
2171 shader
->limits
.constant_bool
= 16;
2172 shader
->limits
.sampler
= 16;
2173 shader
->limits
.packed_input
= 12;
2176 case WINED3D_SHADER_VERSION(4, 0):
2177 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
2178 shader
->limits
.constant_int
= 0;
2179 shader
->limits
.constant_float
= 0;
2180 shader
->limits
.constant_bool
= 0;
2181 shader
->limits
.packed_input
= 32;
2185 shader
->limits
.constant_float
= 32;
2186 shader
->limits
.constant_int
= 16;
2187 shader
->limits
.constant_bool
= 16;
2188 shader
->limits
.sampler
= 16;
2189 shader
->limits
.packed_input
= 0;
2190 FIXME("Unrecognized pixel shader version %u.%u\n",
2191 shader
->reg_maps
.shader_version
.major
,
2192 shader
->reg_maps
.shader_version
.minor
);
2196 static HRESULT
pixelshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2197 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
2198 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
2200 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2201 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
2204 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
2206 shader_init(shader
, device
, parent
, parent_ops
);
2207 hr
= shader_set_function(shader
, byte_code
, output_signature
, device
->d3d_pshader_constantF
,
2208 WINED3D_SHADER_TYPE_PIXEL
, max_version
);
2211 WARN("Failed to set function, hr %#x.\n", hr
);
2212 shader_cleanup(shader
);
2216 pixelshader_set_limits(shader
);
2218 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2220 if (shader
->u
.ps
.input_reg_used
[i
])
2223 highest_reg_used
= i
;
2227 /* Don't do any register mapping magic if it is not needed, or if we can't
2228 * achieve anything anyway */
2229 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
2230 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2232 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2234 /* This happens with relative addressing. The input mapper function
2235 * warns about this if the higher registers are declared too, so
2236 * don't write a FIXME here */
2237 WARN("More varying registers used than supported\n");
2240 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2242 shader
->u
.ps
.input_reg_map
[i
] = i
;
2245 shader
->u
.ps
.declared_in_count
= highest_reg_used
+ 1;
2249 shader
->u
.ps
.declared_in_count
= 0;
2250 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2252 if (shader
->u
.ps
.input_reg_used
[i
])
2253 shader
->u
.ps
.input_reg_map
[i
] = shader
->u
.ps
.declared_in_count
++;
2254 else shader
->u
.ps
.input_reg_map
[i
] = ~0U;
2258 shader
->load_local_constsF
= FALSE
;
2263 void pixelshader_update_samplers(struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_texture
* const *textures
)
2265 enum wined3d_sampler_texture_type
*sampler_type
= reg_maps
->sampler_type
;
2268 if (reg_maps
->shader_version
.major
!= 1) return;
2270 for (i
= 0; i
< max(MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
); ++i
)
2272 /* We don't sample from this sampler. */
2273 if (!sampler_type
[i
]) continue;
2277 WARN("No texture bound to sampler %u, using 2D.\n", i
);
2278 sampler_type
[i
] = WINED3DSTT_2D
;
2282 switch (textures
[i
]->target
)
2284 case GL_TEXTURE_RECTANGLE_ARB
:
2286 /* We have to select between texture rectangles and 2D
2287 * textures later because 2.0 and 3.0 shaders only have
2288 * WINED3DSTT_2D as well. */
2289 sampler_type
[i
] = WINED3DSTT_2D
;
2293 sampler_type
[i
] = WINED3DSTT_VOLUME
;
2296 case GL_TEXTURE_CUBE_MAP_ARB
:
2297 sampler_type
[i
] = WINED3DSTT_CUBE
;
2301 FIXME("Unrecognized texture type %#x, using 2D.\n", textures
[i
]->target
);
2302 sampler_type
[i
] = WINED3DSTT_2D
;
2307 HRESULT CDECL
wined3d_shader_create_gs(struct wined3d_device
*device
, const DWORD
*byte_code
,
2308 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2309 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2311 struct wined3d_shader
*object
;
2314 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2315 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2317 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2320 ERR("Failed to allocate shader memory.\n");
2321 return E_OUTOFMEMORY
;
2324 hr
= geometryshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2327 WARN("Failed to initialize geometry shader, hr %#x.\n", hr
);
2328 HeapFree(GetProcessHeap(), 0, object
);
2332 TRACE("Created geometry shader %p.\n", object
);
2338 HRESULT CDECL
wined3d_shader_create_ps(struct wined3d_device
*device
, const DWORD
*byte_code
,
2339 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2340 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2342 struct wined3d_shader
*object
;
2345 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2346 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2348 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2351 ERR("Failed to allocate shader memory.\n");
2352 return E_OUTOFMEMORY
;
2355 hr
= pixelshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2358 WARN("Failed to initialize pixel shader, hr %#x.\n", hr
);
2359 HeapFree(GetProcessHeap(), 0, object
);
2363 TRACE("Created pixel shader %p.\n", object
);
2369 HRESULT CDECL
wined3d_shader_create_vs(struct wined3d_device
*device
, const DWORD
*byte_code
,
2370 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2371 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2373 struct wined3d_shader
*object
;
2376 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2377 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2379 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2382 ERR("Failed to allocate shader memory.\n");
2383 return E_OUTOFMEMORY
;
2386 hr
= vertexshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2389 WARN("Failed to initialize vertex shader, hr %#x.\n", hr
);
2390 HeapFree(GetProcessHeap(), 0, object
);
2394 TRACE("Created vertex shader %p.\n", object
);