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
;
1484 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
1485 static void shader_none_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
1486 enum tex_types tex_type
, const SIZE
*ds_mask_size
) {}
1487 static void shader_none_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
) {}
1488 static void shader_none_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1489 static void shader_none_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1490 static void shader_none_load_constants(const struct wined3d_context
*context
, BOOL usePS
, BOOL useVS
) {}
1491 static void shader_none_load_np2fixup_constants(void *shader_priv
,
1492 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
) {}
1493 static void shader_none_destroy(struct wined3d_shader
*shader
) {}
1494 static void shader_none_context_destroyed(void *shader_priv
, const struct wined3d_context
*context
) {}
1496 static void shader_none_select(const struct wined3d_context
*context
, enum wined3d_shader_mode vertex_mode
,
1497 enum wined3d_shader_mode fragment_mode
)
1499 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1500 struct wined3d_device
*device
= context
->swapchain
->device
;
1501 struct shader_none_priv
*priv
= device
->shader_priv
;
1503 priv
->fragment_pipe
->enable_extension(gl_info
, fragment_mode
== WINED3D_SHADER_MODE_FFP
);
1506 static HRESULT
shader_none_alloc(struct wined3d_device
*device
, const struct fragment_pipeline
*fragment_pipe
)
1508 struct shader_none_priv
*priv
;
1509 void *fragment_priv
;
1511 if (!(priv
= HeapAlloc(GetProcessHeap(), 0, sizeof(*priv
))))
1513 ERR("Failed to allocate private data.\n");
1514 return E_OUTOFMEMORY
;
1517 if (!(fragment_priv
= fragment_pipe
->alloc_private(&none_shader_backend
, priv
)))
1519 ERR("Failed to initialize fragment pipe.\n");
1520 HeapFree(GetProcessHeap(), 0, priv
);
1524 device
->fragment_priv
= fragment_priv
;
1525 priv
->fragment_pipe
= fragment_pipe
;
1526 device
->shader_priv
= priv
;
1531 static void shader_none_free(struct wined3d_device
*device
)
1533 struct shader_none_priv
*priv
= device
->shader_priv
;
1535 priv
->fragment_pipe
->free_private(device
);
1536 HeapFree(GetProcessHeap(), 0, priv
);
1539 static void shader_none_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
1541 /* Set the shader caps to 0 for the none shader backend */
1542 caps
->vs_version
= 0;
1543 caps
->gs_version
= 0;
1544 caps
->ps_version
= 0;
1545 caps
->vs_uniform_count
= 0;
1546 caps
->ps_uniform_count
= 0;
1547 caps
->ps_1x_max_value
= 0.0f
;
1548 caps
->vs_clipping
= FALSE
;
1551 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
1553 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
1555 TRACE("Checking support for fixup:\n");
1556 dump_color_fixup_desc(fixup
);
1559 /* Faked to make some apps happy. */
1560 if (!is_complex_fixup(fixup
))
1566 TRACE("[FAILED]\n");
1570 static BOOL
shader_none_has_ffp_proj_control(void *shader_priv
)
1572 struct shader_none_priv
*priv
= shader_priv
;
1574 return priv
->fragment_pipe
->ffp_proj_control
;
1577 const struct wined3d_shader_backend_ops none_shader_backend
=
1579 shader_none_handle_instruction
,
1581 shader_none_select_depth_blt
,
1582 shader_none_deselect_depth_blt
,
1583 shader_none_update_float_vertex_constants
,
1584 shader_none_update_float_pixel_constants
,
1585 shader_none_load_constants
,
1586 shader_none_load_np2fixup_constants
,
1587 shader_none_destroy
,
1590 shader_none_context_destroyed
,
1591 shader_none_get_caps
,
1592 shader_none_color_fixup_supported
,
1593 shader_none_has_ffp_proj_control
,
1596 static HRESULT
shader_set_function(struct wined3d_shader
*shader
, const DWORD
*byte_code
,
1597 const struct wined3d_shader_signature
*output_signature
, DWORD float_const_count
,
1598 enum wined3d_shader_type type
, unsigned int max_version
)
1600 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1601 const struct wined3d_shader_frontend
*fe
;
1603 unsigned int backend_version
;
1605 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1606 shader
, byte_code
, output_signature
, float_const_count
);
1608 fe
= shader_select_frontend(*byte_code
);
1611 FIXME("Unable to find frontend for shader.\n");
1612 return WINED3DERR_INVALIDCALL
;
1614 shader
->frontend
= fe
;
1615 shader
->frontend_data
= fe
->shader_init(byte_code
, output_signature
);
1616 if (!shader
->frontend_data
)
1618 FIXME("Failed to initialize frontend.\n");
1619 return WINED3DERR_INVALIDCALL
;
1622 /* First pass: trace shader. */
1623 if (TRACE_ON(d3d_shader
))
1624 shader_trace_init(fe
, shader
->frontend_data
, byte_code
);
1626 /* Initialize immediate constant lists. */
1627 list_init(&shader
->constantsF
);
1628 list_init(&shader
->constantsB
);
1629 list_init(&shader
->constantsI
);
1631 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1632 hr
= shader_get_registers_used(shader
, fe
,
1633 reg_maps
, shader
->input_signature
, shader
->output_signature
,
1634 byte_code
, float_const_count
);
1635 if (FAILED(hr
)) return hr
;
1637 if (reg_maps
->shader_version
.type
!= type
)
1639 WARN("Wrong shader type %d.\n", reg_maps
->shader_version
.type
);
1640 return WINED3DERR_INVALIDCALL
;
1642 if (reg_maps
->shader_version
.major
> max_version
)
1644 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps
->shader_version
.major
);
1645 return WINED3DERR_INVALIDCALL
;
1649 case WINED3D_SHADER_TYPE_VERTEX
:
1650 backend_version
= shader
->device
->vs_version
;
1652 case WINED3D_SHADER_TYPE_GEOMETRY
:
1653 backend_version
= shader
->device
->gs_version
;
1655 case WINED3D_SHADER_TYPE_PIXEL
:
1656 backend_version
= shader
->device
->ps_version
;
1659 FIXME("No backend version-checking for this shader type\n");
1660 backend_version
= 0;
1662 if (reg_maps
->shader_version
.major
> backend_version
)
1664 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
1665 reg_maps
->shader_version
.major
, reg_maps
->shader_version
.minor
);
1666 return WINED3DERR_INVALIDCALL
;
1669 shader
->function
= HeapAlloc(GetProcessHeap(), 0, shader
->functionLength
);
1670 if (!shader
->function
)
1671 return E_OUTOFMEMORY
;
1672 memcpy(shader
->function
, byte_code
, shader
->functionLength
);
1677 ULONG CDECL
wined3d_shader_incref(struct wined3d_shader
*shader
)
1679 ULONG refcount
= InterlockedIncrement(&shader
->ref
);
1681 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
1686 /* Do not call while under the GL lock. */
1687 ULONG CDECL
wined3d_shader_decref(struct wined3d_shader
*shader
)
1689 ULONG refcount
= InterlockedDecrement(&shader
->ref
);
1691 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
1695 shader_cleanup(shader
);
1696 shader
->parent_ops
->wined3d_object_destroyed(shader
->parent
);
1697 HeapFree(GetProcessHeap(), 0, shader
);
1703 void * CDECL
wined3d_shader_get_parent(const struct wined3d_shader
*shader
)
1705 TRACE("shader %p.\n", shader
);
1707 return shader
->parent
;
1710 HRESULT CDECL
wined3d_shader_get_byte_code(const struct wined3d_shader
*shader
,
1711 void *byte_code
, UINT
*byte_code_size
)
1713 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader
, byte_code
, byte_code_size
);
1717 *byte_code_size
= shader
->functionLength
;
1721 if (*byte_code_size
< shader
->functionLength
)
1723 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
1724 * than the required size we should write the required size and
1725 * return D3DERR_MOREDATA. That's not actually true. */
1726 return WINED3DERR_INVALIDCALL
;
1729 memcpy(byte_code
, shader
->function
, shader
->functionLength
);
1734 /* Set local constants for d3d8 shaders. */
1735 HRESULT CDECL
wined3d_shader_set_local_constants_float(struct wined3d_shader
*shader
,
1736 UINT start_idx
, const float *src_data
, UINT count
)
1738 UINT end_idx
= start_idx
+ count
;
1741 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader
, start_idx
, src_data
, count
);
1743 if (end_idx
> shader
->limits
.constant_float
)
1745 WARN("end_idx %u > float constants limit %u.\n",
1746 end_idx
, shader
->limits
.constant_float
);
1747 end_idx
= shader
->limits
.constant_float
;
1750 for (i
= start_idx
; i
< end_idx
; ++i
)
1752 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
1754 return E_OUTOFMEMORY
;
1757 memcpy(lconst
->value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
1758 list_add_head(&shader
->constantsF
, &lconst
->entry
);
1764 void find_vs_compile_args(const struct wined3d_state
*state
,
1765 const struct wined3d_shader
*shader
, struct vs_compile_args
*args
)
1767 args
->fog_src
= state
->render_states
[WINED3D_RS_FOGTABLEMODE
]
1768 == WINED3D_FOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
1769 args
->clip_enabled
= state
->render_states
[WINED3D_RS_CLIPPING
]
1770 && state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
];
1771 args
->swizzle_map
= shader
->device
->strided_streams
.swizzle_map
;
1774 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
1776 if (usage_idx1
!= usage_idx2
)
1778 if (usage1
== usage2
)
1780 if (usage1
== WINED3D_DECL_USAGE_POSITION
&& usage2
== WINED3D_DECL_USAGE_POSITIONT
)
1782 if (usage2
== WINED3D_DECL_USAGE_POSITION
&& usage1
== WINED3D_DECL_USAGE_POSITIONT
)
1788 BOOL
vshader_get_input(const struct wined3d_shader
*shader
,
1789 BYTE usage_req
, BYTE usage_idx_req
, unsigned int *regnum
)
1791 WORD map
= shader
->reg_maps
.input_registers
;
1794 for (i
= 0; map
; map
>>= 1, ++i
)
1796 if (!(map
& 1)) continue;
1798 if (match_usage(shader
->u
.vs
.attributes
[i
].usage
,
1799 shader
->u
.vs
.attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
1808 static void vertexshader_set_limits(struct wined3d_shader
*shader
)
1810 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
1811 shader
->reg_maps
.shader_version
.minor
);
1812 struct wined3d_device
*device
= shader
->device
;
1814 shader
->limits
.packed_input
= 0;
1816 switch (shader_version
)
1818 case WINED3D_SHADER_VERSION(1, 0):
1819 case WINED3D_SHADER_VERSION(1, 1):
1820 shader
->limits
.constant_bool
= 0;
1821 shader
->limits
.constant_int
= 0;
1822 shader
->limits
.packed_output
= 12;
1823 shader
->limits
.sampler
= 0;
1824 /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1825 * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1827 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1830 case WINED3D_SHADER_VERSION(2, 0):
1831 case WINED3D_SHADER_VERSION(2, 1):
1832 shader
->limits
.constant_bool
= 16;
1833 shader
->limits
.constant_int
= 16;
1834 shader
->limits
.packed_output
= 12;
1835 shader
->limits
.sampler
= 0;
1836 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1839 case WINED3D_SHADER_VERSION(3, 0):
1840 shader
->limits
.constant_bool
= 16;
1841 shader
->limits
.constant_int
= 16;
1842 shader
->limits
.packed_output
= 12;
1843 shader
->limits
.sampler
= 4;
1844 /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1845 * even though they are capable of supporting much more (GL
1846 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1847 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1848 * shaders to 256. */
1849 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1852 case WINED3D_SHADER_VERSION(4, 0):
1853 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
1854 shader
->limits
.constant_int
= 0;
1855 shader
->limits
.constant_float
= 0;
1856 shader
->limits
.constant_bool
= 0;
1857 shader
->limits
.packed_output
= 16;
1858 shader
->limits
.packed_input
= 0;
1862 shader
->limits
.constant_bool
= 16;
1863 shader
->limits
.constant_int
= 16;
1864 shader
->limits
.packed_output
= 12;
1865 shader
->limits
.sampler
= 0;
1866 shader
->limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1867 FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1868 shader
->reg_maps
.shader_version
.major
,
1869 shader
->reg_maps
.shader_version
.minor
);
1873 static HRESULT
vertexshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
1874 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
1875 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
1877 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1882 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
1884 shader_init(shader
, device
, parent
, parent_ops
);
1885 hr
= shader_set_function(shader
, byte_code
, output_signature
, device
->d3d_vshader_constantF
,
1886 WINED3D_SHADER_TYPE_VERTEX
, max_version
);
1889 WARN("Failed to set function, hr %#x.\n", hr
);
1890 shader_cleanup(shader
);
1894 map
= reg_maps
->input_registers
;
1895 for (i
= 0; map
; map
>>= 1, ++i
)
1897 if (!(map
& 1) || !shader
->input_signature
[i
].semantic_name
)
1900 shader
->u
.vs
.attributes
[i
].usage
=
1901 shader_usage_from_semantic_name(shader
->input_signature
[i
].semantic_name
);
1902 shader
->u
.vs
.attributes
[i
].usage_idx
= shader
->input_signature
[i
].semantic_idx
;
1905 if (output_signature
)
1907 for (i
= 0; i
< output_signature
->element_count
; ++i
)
1909 struct wined3d_shader_signature_element
*e
= &output_signature
->elements
[i
];
1910 reg_maps
->output_registers
|= 1 << e
->register_idx
;
1911 shader
->output_signature
[e
->register_idx
] = *e
;
1915 vertexshader_set_limits(shader
);
1917 shader
->load_local_constsF
= reg_maps
->usesrelconstF
1918 && !list_empty(&shader
->constantsF
);
1923 static void geometryshader_set_limits(struct wined3d_shader
*shader
)
1925 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
1926 shader
->reg_maps
.shader_version
.minor
);
1928 switch (shader_version
)
1930 case WINED3D_SHADER_VERSION(4, 0):
1931 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
1932 shader
->limits
.constant_int
= 0;
1933 shader
->limits
.constant_float
= 0;
1934 shader
->limits
.constant_bool
= 0;
1935 shader
->limits
.packed_output
= 32;
1936 shader
->limits
.packed_input
= 16;
1940 memset(&shader
->limits
, 0, sizeof(shader
->limits
));
1941 FIXME("Unhandled geometry shader version \"%u.%u\".\n",
1942 shader
->reg_maps
.shader_version
.major
,
1943 shader
->reg_maps
.shader_version
.minor
);
1947 static HRESULT
geometryshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
1948 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
1949 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
1953 shader_init(shader
, device
, parent
, parent_ops
);
1954 hr
= shader_set_function(shader
, byte_code
, output_signature
, 0,
1955 WINED3D_SHADER_TYPE_GEOMETRY
, max_version
);
1958 WARN("Failed to set function, hr %#x.\n", hr
);
1959 shader_cleanup(shader
);
1963 geometryshader_set_limits(shader
);
1965 shader
->load_local_constsF
= FALSE
;
1970 void find_ps_compile_args(const struct wined3d_state
*state
,
1971 const struct wined3d_shader
*shader
, struct ps_compile_args
*args
)
1973 struct wined3d_device
*device
= shader
->device
;
1974 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1975 const struct wined3d_texture
*texture
;
1978 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
1979 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && state
->render_states
[WINED3D_RS_SRGBWRITEENABLE
])
1981 const struct wined3d_surface
*rt
= state
->fb
->render_targets
[0];
1982 if (rt
->resource
.format
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
1984 static unsigned int warned
= 0;
1986 args
->srgb_correction
= 1;
1987 if (state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] && !warned
++)
1988 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
1989 "support, expect rendering artifacts.\n");
1993 if (shader
->reg_maps
.shader_version
.major
== 1
1994 && shader
->reg_maps
.shader_version
.minor
<= 3)
1996 for (i
= 0; i
< 4; ++i
)
1998 DWORD flags
= state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
];
2000 if (flags
& WINED3D_TTFF_PROJECTED
)
2002 DWORD tex_transform
= flags
& ~WINED3D_TTFF_PROJECTED
;
2004 if (!state
->vertex_shader
)
2007 unsigned int index
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
2008 DWORD max_valid
= WINED3D_TTFF_COUNT4
;
2009 enum wined3d_sampler_texture_type sampler_type
= shader
->reg_maps
.sampler_type
[i
];
2011 for (j
= 0; j
< state
->vertex_declaration
->element_count
; ++j
)
2013 struct wined3d_vertex_declaration_element
*element
=
2014 &state
->vertex_declaration
->elements
[j
];
2016 if (element
->usage
== WINED3D_DECL_USAGE_TEXCOORD
2017 && element
->usage_idx
== index
)
2019 max_valid
= element
->format
->component_count
;
2023 if (!tex_transform
|| tex_transform
> max_valid
)
2025 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2026 tex_transform
, max_valid
);
2027 tex_transform
= max_valid
;
2029 if ((sampler_type
== WINED3DSTT_1D
&& tex_transform
> WINED3D_TTFF_COUNT1
)
2030 || (sampler_type
== WINED3DSTT_2D
&& tex_transform
> WINED3D_TTFF_COUNT2
)
2031 || (sampler_type
== WINED3DSTT_VOLUME
&& tex_transform
> WINED3D_TTFF_COUNT3
))
2032 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
2035 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2036 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2037 i
, tex_transform
, sampler_type
);
2041 tex_transform
= WINED3D_TTFF_COUNT4
| WINED3D_PSARGS_PROJECTED
;
2043 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
2048 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2050 if (!shader
->reg_maps
.sampler_type
[i
])
2053 texture
= state
->textures
[i
];
2056 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
2059 args
->color_fixup
[i
] = texture
->resource
.format
->color_fixup
;
2061 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
2062 args
->shadow
|= 1 << i
;
2064 /* Flag samplers that need NP2 texcoord fixup. */
2065 if (!(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
2066 args
->np2_fixup
|= (1 << i
);
2068 if (shader
->reg_maps
.shader_version
.major
>= 3)
2070 if (device
->strided_streams
.position_transformed
)
2072 args
->vp_mode
= pretransformed
;
2074 else if (use_vs(state
))
2076 args
->vp_mode
= vertexshader
;
2080 args
->vp_mode
= fixedfunction
;
2082 args
->fog
= FOG_OFF
;
2086 args
->vp_mode
= vertexshader
;
2087 if (state
->render_states
[WINED3D_RS_FOGENABLE
])
2089 switch (state
->render_states
[WINED3D_RS_FOGTABLEMODE
])
2091 case WINED3D_FOG_NONE
:
2092 if (device
->strided_streams
.position_transformed
|| use_vs(state
))
2094 args
->fog
= FOG_LINEAR
;
2098 switch (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
])
2100 case WINED3D_FOG_NONE
: /* Fall through. */
2101 case WINED3D_FOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
2102 case WINED3D_FOG_EXP
: args
->fog
= FOG_EXP
; break;
2103 case WINED3D_FOG_EXP2
: args
->fog
= FOG_EXP2
; break;
2107 case WINED3D_FOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
2108 case WINED3D_FOG_EXP
: args
->fog
= FOG_EXP
; break;
2109 case WINED3D_FOG_EXP2
: args
->fog
= FOG_EXP2
; break;
2114 args
->fog
= FOG_OFF
;
2119 static void pixelshader_set_limits(struct wined3d_shader
*shader
)
2121 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
2122 shader
->reg_maps
.shader_version
.minor
);
2124 shader
->limits
.packed_output
= 0;
2126 switch (shader_version
)
2128 case WINED3D_SHADER_VERSION(1, 0):
2129 case WINED3D_SHADER_VERSION(1, 1):
2130 case WINED3D_SHADER_VERSION(1, 2):
2131 case WINED3D_SHADER_VERSION(1, 3):
2132 shader
->limits
.constant_float
= 8;
2133 shader
->limits
.constant_int
= 0;
2134 shader
->limits
.constant_bool
= 0;
2135 shader
->limits
.sampler
= 4;
2136 shader
->limits
.packed_input
= 0;
2139 case WINED3D_SHADER_VERSION(1, 4):
2140 shader
->limits
.constant_float
= 8;
2141 shader
->limits
.constant_int
= 0;
2142 shader
->limits
.constant_bool
= 0;
2143 shader
->limits
.sampler
= 6;
2144 shader
->limits
.packed_input
= 0;
2147 /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2148 case WINED3D_SHADER_VERSION(2, 0):
2149 shader
->limits
.constant_float
= 32;
2150 shader
->limits
.constant_int
= 16;
2151 shader
->limits
.constant_bool
= 16;
2152 shader
->limits
.sampler
= 16;
2153 shader
->limits
.packed_input
= 0;
2156 case WINED3D_SHADER_VERSION(2, 1):
2157 shader
->limits
.constant_float
= 32;
2158 shader
->limits
.constant_int
= 16;
2159 shader
->limits
.constant_bool
= 16;
2160 shader
->limits
.sampler
= 16;
2161 shader
->limits
.packed_input
= 0;
2164 case WINED3D_SHADER_VERSION(3, 0):
2165 shader
->limits
.constant_float
= 224;
2166 shader
->limits
.constant_int
= 16;
2167 shader
->limits
.constant_bool
= 16;
2168 shader
->limits
.sampler
= 16;
2169 shader
->limits
.packed_input
= 12;
2172 case WINED3D_SHADER_VERSION(4, 0):
2173 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
2174 shader
->limits
.constant_int
= 0;
2175 shader
->limits
.constant_float
= 0;
2176 shader
->limits
.constant_bool
= 0;
2177 shader
->limits
.packed_input
= 32;
2181 shader
->limits
.constant_float
= 32;
2182 shader
->limits
.constant_int
= 16;
2183 shader
->limits
.constant_bool
= 16;
2184 shader
->limits
.sampler
= 16;
2185 shader
->limits
.packed_input
= 0;
2186 FIXME("Unrecognized pixel shader version %u.%u\n",
2187 shader
->reg_maps
.shader_version
.major
,
2188 shader
->reg_maps
.shader_version
.minor
);
2192 static HRESULT
pixelshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2193 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
2194 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
2196 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2197 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
2200 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
2202 shader_init(shader
, device
, parent
, parent_ops
);
2203 hr
= shader_set_function(shader
, byte_code
, output_signature
, device
->d3d_pshader_constantF
,
2204 WINED3D_SHADER_TYPE_PIXEL
, max_version
);
2207 WARN("Failed to set function, hr %#x.\n", hr
);
2208 shader_cleanup(shader
);
2212 pixelshader_set_limits(shader
);
2214 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2216 if (shader
->u
.ps
.input_reg_used
[i
])
2219 highest_reg_used
= i
;
2223 /* Don't do any register mapping magic if it is not needed, or if we can't
2224 * achieve anything anyway */
2225 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
2226 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2228 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2230 /* This happens with relative addressing. The input mapper function
2231 * warns about this if the higher registers are declared too, so
2232 * don't write a FIXME here */
2233 WARN("More varying registers used than supported\n");
2236 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2238 shader
->u
.ps
.input_reg_map
[i
] = i
;
2241 shader
->u
.ps
.declared_in_count
= highest_reg_used
+ 1;
2245 shader
->u
.ps
.declared_in_count
= 0;
2246 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2248 if (shader
->u
.ps
.input_reg_used
[i
])
2249 shader
->u
.ps
.input_reg_map
[i
] = shader
->u
.ps
.declared_in_count
++;
2250 else shader
->u
.ps
.input_reg_map
[i
] = ~0U;
2254 shader
->load_local_constsF
= FALSE
;
2259 void pixelshader_update_samplers(struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_texture
* const *textures
)
2261 enum wined3d_sampler_texture_type
*sampler_type
= reg_maps
->sampler_type
;
2264 if (reg_maps
->shader_version
.major
!= 1) return;
2266 for (i
= 0; i
< max(MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
); ++i
)
2268 /* We don't sample from this sampler. */
2269 if (!sampler_type
[i
]) continue;
2273 WARN("No texture bound to sampler %u, using 2D.\n", i
);
2274 sampler_type
[i
] = WINED3DSTT_2D
;
2278 switch (textures
[i
]->target
)
2280 case GL_TEXTURE_RECTANGLE_ARB
:
2282 /* We have to select between texture rectangles and 2D
2283 * textures later because 2.0 and 3.0 shaders only have
2284 * WINED3DSTT_2D as well. */
2285 sampler_type
[i
] = WINED3DSTT_2D
;
2289 sampler_type
[i
] = WINED3DSTT_VOLUME
;
2292 case GL_TEXTURE_CUBE_MAP_ARB
:
2293 sampler_type
[i
] = WINED3DSTT_CUBE
;
2297 FIXME("Unrecognized texture type %#x, using 2D.\n", textures
[i
]->target
);
2298 sampler_type
[i
] = WINED3DSTT_2D
;
2303 HRESULT CDECL
wined3d_shader_create_gs(struct wined3d_device
*device
, const DWORD
*byte_code
,
2304 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2305 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2307 struct wined3d_shader
*object
;
2310 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2311 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2313 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2316 ERR("Failed to allocate shader memory.\n");
2317 return E_OUTOFMEMORY
;
2320 hr
= geometryshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2323 WARN("Failed to initialize geometry shader, hr %#x.\n", hr
);
2324 HeapFree(GetProcessHeap(), 0, object
);
2328 TRACE("Created geometry shader %p.\n", object
);
2334 HRESULT CDECL
wined3d_shader_create_ps(struct wined3d_device
*device
, const DWORD
*byte_code
,
2335 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2336 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2338 struct wined3d_shader
*object
;
2341 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2342 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2344 if (device
->ps_selected_mode
== SHADER_NONE
)
2345 return WINED3DERR_INVALIDCALL
;
2347 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2350 ERR("Failed to allocate shader memory.\n");
2351 return E_OUTOFMEMORY
;
2354 hr
= pixelshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2357 WARN("Failed to initialize pixel shader, hr %#x.\n", hr
);
2358 HeapFree(GetProcessHeap(), 0, object
);
2362 TRACE("Created pixel shader %p.\n", object
);
2368 HRESULT CDECL
wined3d_shader_create_vs(struct wined3d_device
*device
, const DWORD
*byte_code
,
2369 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2370 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2372 struct wined3d_shader
*object
;
2375 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2376 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2378 if (device
->vs_selected_mode
== SHADER_NONE
)
2379 return WINED3DERR_INVALIDCALL
;
2381 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2384 ERR("Failed to allocate shader memory.\n");
2385 return E_OUTOFMEMORY
;
2388 hr
= vertexshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2391 WARN("Failed to initialize vertex shader, hr %#x.\n", hr
);
2392 HeapFree(GetProcessHeap(), 0, object
);
2396 TRACE("Created vertex shader %p.\n", object
);