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