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 /* pow, mul_high, sub_high, mul_low */
37 const float wined3d_srgb_const0
[] = {0.41666f
, 1.055f
, 0.055f
, 12.92f
};
39 const float wined3d_srgb_const1
[] = {0.0031308f
, 0.0f
, 0.0f
, 0.0f
};
41 static const char * const shader_opcode_names
[] =
43 /* WINED3DSIH_ABS */ "abs",
44 /* WINED3DSIH_ADD */ "add",
45 /* WINED3DSIH_AND */ "and",
46 /* WINED3DSIH_BEM */ "bem",
47 /* WINED3DSIH_BREAK */ "break",
48 /* WINED3DSIH_BREAKC */ "breakc",
49 /* WINED3DSIH_BREAKP */ "breakp",
50 /* WINED3DSIH_CALL */ "call",
51 /* WINED3DSIH_CALLNZ */ "callnz",
52 /* WINED3DSIH_CMP */ "cmp",
53 /* WINED3DSIH_CND */ "cnd",
54 /* WINED3DSIH_CRS */ "crs",
55 /* WINED3DSIH_CUT */ "cut",
56 /* WINED3DSIH_DCL */ "dcl",
57 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
58 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
59 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
60 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
61 /* WINED3DSIH_DEF */ "def",
62 /* WINED3DSIH_DEFB */ "defb",
63 /* WINED3DSIH_DEFI */ "defi",
64 /* WINED3DSIH_DIV */ "div",
65 /* WINED3DSIH_DP2ADD */ "dp2add",
66 /* WINED3DSIH_DP3 */ "dp3",
67 /* WINED3DSIH_DP4 */ "dp4",
68 /* WINED3DSIH_DST */ "dst",
69 /* WINED3DSIH_DSX */ "dsx",
70 /* WINED3DSIH_DSY */ "dsy",
71 /* WINED3DSIH_ELSE */ "else",
72 /* WINED3DSIH_EMIT */ "emit",
73 /* WINED3DSIH_ENDIF */ "endif",
74 /* WINED3DSIH_ENDLOOP */ "endloop",
75 /* WINED3DSIH_ENDREP */ "endrep",
76 /* WINED3DSIH_EQ */ "eq",
77 /* WINED3DSIH_EXP */ "exp",
78 /* WINED3DSIH_EXPP */ "expp",
79 /* WINED3DSIH_FRC */ "frc",
80 /* WINED3DSIH_FTOI */ "ftoi",
81 /* WINED3DSIH_GE */ "ge",
82 /* WINED3DSIH_IADD */ "iadd",
83 /* WINED3DSIH_IEQ */ "ieq",
84 /* WINED3DSIH_IF */ "if",
85 /* WINED3DSIH_IFC */ "ifc",
86 /* WINED3DSIH_IGE */ "ige",
87 /* WINED3DSIH_IMUL */ "imul",
88 /* WINED3DSIH_ITOF */ "itof",
89 /* WINED3DSIH_LABEL */ "label",
90 /* WINED3DSIH_LD */ "ld",
91 /* WINED3DSIH_LIT */ "lit",
92 /* WINED3DSIH_LOG */ "log",
93 /* WINED3DSIH_LOGP */ "logp",
94 /* WINED3DSIH_LOOP */ "loop",
95 /* WINED3DSIH_LRP */ "lrp",
96 /* WINED3DSIH_LT */ "lt",
97 /* WINED3DSIH_M3x2 */ "m3x2",
98 /* WINED3DSIH_M3x3 */ "m3x3",
99 /* WINED3DSIH_M3x4 */ "m3x4",
100 /* WINED3DSIH_M4x3 */ "m4x3",
101 /* WINED3DSIH_M4x4 */ "m4x4",
102 /* WINED3DSIH_MAD */ "mad",
103 /* WINED3DSIH_MAX */ "max",
104 /* WINED3DSIH_MIN */ "min",
105 /* WINED3DSIH_MOV */ "mov",
106 /* WINED3DSIH_MOVA */ "mova",
107 /* WINED3DSIH_MOVC */ "movc",
108 /* WINED3DSIH_MUL */ "mul",
109 /* WINED3DSIH_NOP */ "nop",
110 /* WINED3DSIH_NRM */ "nrm",
111 /* WINED3DSIH_PHASE */ "phase",
112 /* WINED3DSIH_POW */ "pow",
113 /* WINED3DSIH_RCP */ "rcp",
114 /* WINED3DSIH_REP */ "rep",
115 /* WINED3DSIH_RET */ "ret",
116 /* WINED3DSIH_ROUND_NI */ "round_ni",
117 /* WINED3DSIH_RSQ */ "rsq",
118 /* WINED3DSIH_SAMPLE */ "sample",
119 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
120 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
121 /* WINED3DSIH_SETP */ "setp",
122 /* WINED3DSIH_SGE */ "sge",
123 /* WINED3DSIH_SGN */ "sgn",
124 /* WINED3DSIH_SINCOS */ "sincos",
125 /* WINED3DSIH_SLT */ "slt",
126 /* WINED3DSIH_SQRT */ "sqrt",
127 /* WINED3DSIH_SUB */ "sub",
128 /* WINED3DSIH_TEX */ "texld",
129 /* WINED3DSIH_TEXBEM */ "texbem",
130 /* WINED3DSIH_TEXBEML */ "texbeml",
131 /* WINED3DSIH_TEXCOORD */ "texcrd",
132 /* WINED3DSIH_TEXDEPTH */ "texdepth",
133 /* WINED3DSIH_TEXDP3 */ "texdp3",
134 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
135 /* WINED3DSIH_TEXKILL */ "texkill",
136 /* WINED3DSIH_TEXLDD */ "texldd",
137 /* WINED3DSIH_TEXLDL */ "texldl",
138 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
139 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
140 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
141 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
142 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
143 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
144 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
145 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
146 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
147 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
148 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
149 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
150 /* WINED3DSIH_UDIV */ "udiv",
151 /* WINED3DSIH_USHR */ "ushr",
152 /* WINED3DSIH_UTOF */ "utof",
153 /* WINED3DSIH_XOR */ "xor",
156 static const char * const semantic_names
[] =
158 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
159 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
160 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
161 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
162 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
163 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
164 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
165 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
166 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
167 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
168 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
169 /* WINED3D_DECL_USAGE_FOG */ "FOG",
170 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
171 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
174 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage
)
176 if (usage
>= sizeof(semantic_names
) / sizeof(*semantic_names
))
178 FIXME("Unrecognized usage %#x.\n", usage
);
179 return "UNRECOGNIZED";
182 return semantic_names
[usage
];
185 static enum wined3d_decl_usage
shader_usage_from_semantic_name(const char *name
)
189 for (i
= 0; i
< sizeof(semantic_names
) / sizeof(*semantic_names
); ++i
)
191 if (!strcmp(name
, semantic_names
[i
])) return i
;
197 BOOL
shader_match_semantic(const char *semantic_name
, enum wined3d_decl_usage usage
)
199 return !strcmp(semantic_name
, shader_semantic_name_from_usage(usage
));
202 static void shader_signature_from_semantic(struct wined3d_shader_signature_element
*e
,
203 const struct wined3d_shader_semantic
*s
)
205 e
->semantic_name
= shader_semantic_name_from_usage(s
->usage
);
206 e
->semantic_idx
= s
->usage_idx
;
207 e
->sysval_semantic
= 0;
208 e
->component_type
= 0;
209 e
->register_idx
= s
->reg
.reg
.idx
[0].offset
;
210 e
->mask
= s
->reg
.write_mask
;
213 static void shader_signature_from_usage(struct wined3d_shader_signature_element
*e
,
214 enum wined3d_decl_usage usage
, UINT usage_idx
, UINT reg_idx
, DWORD write_mask
)
216 e
->semantic_name
= shader_semantic_name_from_usage(usage
);
217 e
->semantic_idx
= usage_idx
;
218 e
->sysval_semantic
= 0;
219 e
->component_type
= 0;
220 e
->register_idx
= reg_idx
;
221 e
->mask
= write_mask
;
224 static const struct wined3d_shader_frontend
*shader_select_frontend(DWORD version_token
)
226 switch (version_token
>> 16)
230 return &sm1_shader_frontend
;
235 return &sm4_shader_frontend
;
238 FIXME("Unrecognised version token %#x\n", version_token
);
243 void shader_buffer_clear(struct wined3d_shader_buffer
*buffer
)
245 buffer
->buffer
[0] = '\0';
246 buffer
->content_size
= 0;
248 buffer
->newline
= TRUE
;
251 BOOL
shader_buffer_init(struct wined3d_shader_buffer
*buffer
)
253 buffer
->buffer_size
= 16384;
254 if (!(buffer
->buffer
= HeapAlloc(GetProcessHeap(), 0, buffer
->buffer_size
)))
256 ERR("Failed to allocate shader buffer memory.\n");
260 shader_buffer_clear(buffer
);
264 void shader_buffer_free(struct wined3d_shader_buffer
*buffer
)
266 HeapFree(GetProcessHeap(), 0, buffer
->buffer
);
269 int shader_vaddline(struct wined3d_shader_buffer
*buffer
, const char *format
, va_list args
)
271 char *base
= buffer
->buffer
+ buffer
->content_size
;
277 rc
= vsnprintf(base
, buffer
->buffer_size
- buffer
->content_size
, format
, args
);
278 if (rc
< 0 /* C89 */ || (unsigned int)rc
>= buffer
->buffer_size
- buffer
->content_size
/* C99 */)
280 new_buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
->buffer
, buffer
->buffer_size
* 2);
283 ERR("The buffer allocated for the shader program string is too small at %d bytes.\n", buffer
->buffer_size
);
284 buffer
->content_size
= buffer
->buffer_size
- 1;
287 buffer
->buffer
= new_buffer
;
288 buffer
->buffer_size
= buffer
->buffer_size
* 2;
289 base
= buffer
->buffer
+ buffer
->content_size
;
299 TRACE("GL HW (%u, %u) : %s", buffer
->lineNo
+ 1, buffer
->content_size
, base
);
300 buffer
->newline
= FALSE
;
307 buffer
->content_size
+= rc
;
308 if (buffer
->buffer
[buffer
->content_size
-1] == '\n')
311 buffer
->newline
= TRUE
;
317 int shader_addline(struct wined3d_shader_buffer
*buffer
, const char *format
, ...)
322 va_start(args
, format
);
323 ret
= shader_vaddline(buffer
, format
, args
);
329 static void shader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
330 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
333 shader
->device
= device
;
334 shader
->parent
= parent
;
335 shader
->parent_ops
= parent_ops
;
336 list_init(&shader
->linked_programs
);
337 list_add_head(&device
->shaders
, &shader
->shader_list_entry
);
340 /* Convert floating point offset relative to a register file to an absolute
341 * offset for float constants. */
342 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type
, UINT register_idx
)
344 switch (register_type
)
346 case WINED3DSPR_CONST
: return register_idx
;
347 case WINED3DSPR_CONST2
: return 2048 + register_idx
;
348 case WINED3DSPR_CONST3
: return 4096 + register_idx
;
349 case WINED3DSPR_CONST4
: return 6144 + register_idx
;
351 FIXME("Unsupported register type: %u.\n", register_type
);
356 static void shader_delete_constant_list(struct list
*clist
)
358 struct wined3d_shader_lconst
*constant
;
361 ptr
= list_head(clist
);
364 constant
= LIST_ENTRY(ptr
, struct wined3d_shader_lconst
, entry
);
365 ptr
= list_next(clist
, ptr
);
366 HeapFree(GetProcessHeap(), 0, constant
);
371 static inline void set_bitmap_bit(DWORD
*bitmap
, DWORD bit
)
376 bitmap
[idx
] |= (1 << shift
);
379 static void shader_record_register_usage(struct wined3d_shader
*shader
, struct wined3d_shader_reg_maps
*reg_maps
,
380 const struct wined3d_shader_register
*reg
, enum wined3d_shader_type shader_type
)
384 case WINED3DSPR_TEXTURE
: /* WINED3DSPR_ADDR */
385 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
386 reg_maps
->texcoord
|= 1 << reg
->idx
[0].offset
;
388 reg_maps
->address
|= 1 << reg
->idx
[0].offset
;
391 case WINED3DSPR_TEMP
:
392 reg_maps
->temporary
|= 1 << reg
->idx
[0].offset
;
395 case WINED3DSPR_INPUT
:
396 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
398 if (reg
->idx
[0].rel_addr
)
400 /* If relative addressing is used, we must assume that all registers
401 * are used. Even if it is a construct like v3[aL], we can't assume
402 * that v0, v1 and v2 aren't read because aL can be negative */
404 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
406 shader
->u
.ps
.input_reg_used
[i
] = TRUE
;
411 shader
->u
.ps
.input_reg_used
[reg
->idx
[0].offset
] = TRUE
;
415 reg_maps
->input_registers
|= 1 << reg
->idx
[0].offset
;
418 case WINED3DSPR_RASTOUT
:
419 if (reg
->idx
[0].offset
== 1)
423 case WINED3DSPR_MISCTYPE
:
424 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
426 if (!reg
->idx
[0].offset
)
428 else if (reg
->idx
[0].offset
== 1)
429 reg_maps
->usesfacing
= 1;
433 case WINED3DSPR_CONST
:
434 if (reg
->idx
[0].rel_addr
)
436 if (reg
->idx
[0].offset
< reg_maps
->min_rel_offset
)
437 reg_maps
->min_rel_offset
= reg
->idx
[0].offset
;
438 if (reg
->idx
[0].offset
> reg_maps
->max_rel_offset
)
439 reg_maps
->max_rel_offset
= reg
->idx
[0].offset
;
440 reg_maps
->usesrelconstF
= TRUE
;
444 set_bitmap_bit(reg_maps
->constf
, reg
->idx
[0].offset
);
448 case WINED3DSPR_CONSTINT
:
449 reg_maps
->integer_constants
|= (1 << reg
->idx
[0].offset
);
452 case WINED3DSPR_CONSTBOOL
:
453 reg_maps
->boolean_constants
|= (1 << reg
->idx
[0].offset
);
456 case WINED3DSPR_COLOROUT
:
457 reg_maps
->rt_mask
|= (1 << reg
->idx
[0].offset
);
461 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
462 reg
->type
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
467 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr
, unsigned int param
)
471 case WINED3DSIH_M4x4
:
472 case WINED3DSIH_M3x4
:
473 return param
== 1 ? 3 : 0;
475 case WINED3DSIH_M4x3
:
476 case WINED3DSIH_M3x3
:
477 return param
== 1 ? 2 : 0;
479 case WINED3DSIH_M3x2
:
480 return param
== 1 ? 1 : 0;
487 /* Note that this does not count the loop register as an address register. */
488 static HRESULT
shader_get_registers_used(struct wined3d_shader
*shader
, const struct wined3d_shader_frontend
*fe
,
489 struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_shader_signature_element
*input_signature
,
490 struct wined3d_shader_signature_element
*output_signature
, const DWORD
*byte_code
, DWORD constf_size
)
492 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
493 void *fe_data
= shader
->frontend_data
;
494 struct wined3d_shader_version shader_version
;
495 const DWORD
*ptr
= byte_code
;
497 memset(reg_maps
, 0, sizeof(*reg_maps
));
498 reg_maps
->min_rel_offset
= ~0U;
500 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
501 reg_maps
->shader_version
= shader_version
;
503 reg_maps
->constf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
504 sizeof(*reg_maps
->constf
) * ((constf_size
+ 31) / 32));
505 if (!reg_maps
->constf
)
507 ERR("Failed to allocate constant map memory.\n");
508 return E_OUTOFMEMORY
;
511 while (!fe
->shader_is_end(fe_data
, &ptr
))
513 struct wined3d_shader_instruction ins
;
516 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
518 /* Unhandled opcode, and its parameters. */
519 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
521 TRACE("Skipping unrecognized instruction.\n");
525 /* Handle declarations. */
526 if (ins
.handler_idx
== WINED3DSIH_DCL
)
528 struct wined3d_shader_semantic
*semantic
= &ins
.declaration
.semantic
;
530 switch (semantic
->reg
.reg
.type
)
532 /* Mark input registers used. */
533 case WINED3DSPR_INPUT
:
534 reg_maps
->input_registers
|= 1 << semantic
->reg
.reg
.idx
[0].offset
;
535 shader_signature_from_semantic(&input_signature
[semantic
->reg
.reg
.idx
[0].offset
], semantic
);
538 /* Vertex shader: mark 3.0 output registers used, save token. */
539 case WINED3DSPR_OUTPUT
:
540 reg_maps
->output_registers
|= 1 << semantic
->reg
.reg
.idx
[0].offset
;
541 shader_signature_from_semantic(&output_signature
[semantic
->reg
.reg
.idx
[0].offset
], semantic
);
542 if (semantic
->usage
== WINED3D_DECL_USAGE_FOG
)
546 /* Save sampler usage token. */
547 case WINED3DSPR_SAMPLER
:
548 reg_maps
->sampler_type
[semantic
->reg
.reg
.idx
[0].offset
] = semantic
->sampler_type
;
552 TRACE("Not recording DCL register type %#x.\n", semantic
->reg
.reg
.type
);
556 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
558 struct wined3d_shader_register
*reg
= &ins
.declaration
.src
.reg
;
559 if (reg
->idx
[0].offset
>= WINED3D_MAX_CBS
)
560 ERR("Invalid CB index %u.\n", reg
->idx
[0].offset
);
562 reg_maps
->cb_sizes
[reg
->idx
[0].offset
] = reg
->idx
[1].offset
;
564 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
)
566 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
567 shader
->u
.gs
.input_type
= ins
.declaration
.primitive_type
;
569 FIXME("Invalid instruction %#x for shader type %#x.\n",
570 ins
.handler_idx
, shader_version
.type
);
572 else if (ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
574 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
575 shader
->u
.gs
.output_type
= ins
.declaration
.primitive_type
;
577 FIXME("Invalid instruction %#x for shader type %#x.\n",
578 ins
.handler_idx
, shader_version
.type
);
580 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
582 if (shader_version
.type
== WINED3D_SHADER_TYPE_GEOMETRY
)
583 shader
->u
.gs
.vertices_out
= ins
.declaration
.count
;
585 FIXME("Invalid instruction %#x for shader type %#x.\n",
586 ins
.handler_idx
, shader_version
.type
);
588 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
590 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
592 if (!lconst
) return E_OUTOFMEMORY
;
594 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
595 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
596 value
= (float *)lconst
->value
;
598 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
599 if (shader_version
.major
== 1 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
601 if (value
[0] < -1.0f
) value
[0] = -1.0f
;
602 else if (value
[0] > 1.0f
) value
[0] = 1.0f
;
603 if (value
[1] < -1.0f
) value
[1] = -1.0f
;
604 else if (value
[1] > 1.0f
) value
[1] = 1.0f
;
605 if (value
[2] < -1.0f
) value
[2] = -1.0f
;
606 else if (value
[2] > 1.0f
) value
[2] = 1.0f
;
607 if (value
[3] < -1.0f
) value
[3] = -1.0f
;
608 else if (value
[3] > 1.0f
) value
[3] = 1.0f
;
611 list_add_head(&shader
->constantsF
, &lconst
->entry
);
613 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
614 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
616 shader
->lconst_inf_or_nan
= TRUE
;
619 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
621 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
622 if (!lconst
) return E_OUTOFMEMORY
;
624 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
625 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, 4 * sizeof(DWORD
));
627 list_add_head(&shader
->constantsI
, &lconst
->entry
);
628 reg_maps
->local_int_consts
|= (1 << lconst
->idx
);
630 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
632 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
633 if (!lconst
) return E_OUTOFMEMORY
;
635 lconst
->idx
= ins
.dst
[0].reg
.idx
[0].offset
;
636 memcpy(lconst
->value
, ins
.src
[0].reg
.immconst_data
, sizeof(DWORD
));
638 list_add_head(&shader
->constantsB
, &lconst
->entry
);
639 reg_maps
->local_bool_consts
|= (1 << lconst
->idx
);
641 /* For subroutine prototypes. */
642 else if (ins
.handler_idx
== WINED3DSIH_LABEL
)
644 reg_maps
->labels
|= 1 << ins
.src
[0].reg
.idx
[0].offset
;
646 /* Set texture, address, temporary registers. */
649 BOOL color0_mov
= FALSE
;
652 /* This will loop over all the registers and try to
653 * make a bitmask of the ones we're interested in.
655 * Relative addressing tokens are ignored, but that's
656 * okay, since we'll catch any address registers when
657 * they are initialized (required by spec). */
658 for (i
= 0; i
< ins
.dst_count
; ++i
)
660 shader_record_register_usage(shader
, reg_maps
, &ins
.dst
[i
].reg
, shader_version
.type
);
662 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
663 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
664 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
665 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
&& shader_version
.major
< 3)
667 UINT idx
= ins
.dst
[i
].reg
.idx
[0].offset
;
669 switch (ins
.dst
[i
].reg
.type
)
671 case WINED3DSPR_RASTOUT
:
675 reg_maps
->output_registers
|= 1 << 10;
676 shader_signature_from_usage(&output_signature
[10],
677 WINED3D_DECL_USAGE_POSITION
, 0, 10, WINED3DSP_WRITEMASK_ALL
);
681 reg_maps
->output_registers
|= 1 << 11;
682 shader_signature_from_usage(&output_signature
[11],
683 WINED3D_DECL_USAGE_FOG
, 0, 11, WINED3DSP_WRITEMASK_0
);
687 reg_maps
->output_registers
|= 1 << 11;
688 shader_signature_from_usage(&output_signature
[11],
689 WINED3D_DECL_USAGE_PSIZE
, 0, 11, WINED3DSP_WRITEMASK_1
);
694 case WINED3DSPR_ATTROUT
:
698 if (reg_maps
->output_registers
& (1 << idx
))
700 output_signature
[idx
].mask
|= ins
.dst
[i
].write_mask
;
704 reg_maps
->output_registers
|= 1 << idx
;
705 shader_signature_from_usage(&output_signature
[idx
],
706 WINED3D_DECL_USAGE_COLOR
, idx
- 8, idx
, ins
.dst
[i
].write_mask
);
711 case WINED3DSPR_TEXCRDOUT
:
713 reg_maps
->texcoord_mask
[idx
] |= ins
.dst
[i
].write_mask
;
714 if (reg_maps
->output_registers
& (1 << idx
))
716 output_signature
[idx
].mask
|= ins
.dst
[i
].write_mask
;
720 reg_maps
->output_registers
|= 1 << idx
;
721 shader_signature_from_usage(&output_signature
[idx
],
722 WINED3D_DECL_USAGE_TEXCOORD
, idx
, idx
, ins
.dst
[i
].write_mask
);
731 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
733 if (ins
.dst
[i
].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
.dst
[i
].reg
.idx
[0].offset
)
735 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
736 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
737 * the mov and perform the sRGB write correction from the source register.
739 * However, if the mov is only partial, we can't do this, and if the write
740 * comes from an instruction other than MOV it is hard to do as well. If
741 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
742 shader
->u
.ps
.color0_mov
= FALSE
;
743 if (ins
.handler_idx
== WINED3DSIH_MOV
744 && ins
.dst
[i
].write_mask
== WINED3DSP_WRITEMASK_ALL
)
746 /* Used later when the source register is read. */
750 /* Also drop the MOV marker if the source register is overwritten prior to the shader
753 else if (ins
.dst
[i
].reg
.type
== WINED3DSPR_TEMP
754 && ins
.dst
[i
].reg
.idx
[0].offset
== shader
->u
.ps
.color0_reg
)
756 shader
->u
.ps
.color0_mov
= FALSE
;
760 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
761 if (shader_version
.major
== 1
762 && (ins
.handler_idx
== WINED3DSIH_TEX
763 || ins
.handler_idx
== WINED3DSIH_TEXBEM
764 || ins
.handler_idx
== WINED3DSIH_TEXBEML
765 || ins
.handler_idx
== WINED3DSIH_TEXDP3TEX
766 || ins
.handler_idx
== WINED3DSIH_TEXM3x2TEX
767 || ins
.handler_idx
== WINED3DSIH_TEXM3x3SPEC
768 || ins
.handler_idx
== WINED3DSIH_TEXM3x3TEX
769 || ins
.handler_idx
== WINED3DSIH_TEXM3x3VSPEC
770 || ins
.handler_idx
== WINED3DSIH_TEXREG2AR
771 || ins
.handler_idx
== WINED3DSIH_TEXREG2GB
772 || ins
.handler_idx
== WINED3DSIH_TEXREG2RGB
))
774 /* Fake sampler usage, only set reserved bit and type. */
775 DWORD sampler_code
= ins
.dst
[i
].reg
.idx
[0].offset
;
777 TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
778 reg_maps
->sampler_type
[sampler_code
] = WINED3DSTT_2D
;
780 /* texbem is only valid with < 1.4 pixel shaders */
781 if (ins
.handler_idx
== WINED3DSIH_TEXBEM
782 || ins
.handler_idx
== WINED3DSIH_TEXBEML
)
784 reg_maps
->bumpmat
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
785 if (ins
.handler_idx
== WINED3DSIH_TEXBEML
)
787 reg_maps
->luminanceparams
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
791 else if (ins
.handler_idx
== WINED3DSIH_BEM
)
793 reg_maps
->bumpmat
|= 1 << ins
.dst
[i
].reg
.idx
[0].offset
;
797 if (ins
.handler_idx
== WINED3DSIH_NRM
) reg_maps
->usesnrm
= 1;
798 else if (ins
.handler_idx
== WINED3DSIH_DSY
) reg_maps
->usesdsy
= 1;
799 else if (ins
.handler_idx
== WINED3DSIH_DSX
) reg_maps
->usesdsx
= 1;
800 else if (ins
.handler_idx
== WINED3DSIH_TEXLDD
) reg_maps
->usestexldd
= 1;
801 else if (ins
.handler_idx
== WINED3DSIH_TEXLDL
) reg_maps
->usestexldl
= 1;
802 else if (ins
.handler_idx
== WINED3DSIH_MOVA
) reg_maps
->usesmova
= 1;
803 else if (ins
.handler_idx
== WINED3DSIH_IFC
) reg_maps
->usesifc
= 1;
804 else if (ins
.handler_idx
== WINED3DSIH_CALL
) reg_maps
->usescall
= 1;
805 else if (ins
.handler_idx
== WINED3DSIH_POW
) reg_maps
->usespow
= 1;
806 else if (ins
.handler_idx
== WINED3DSIH_LOOP
807 || ins
.handler_idx
== WINED3DSIH_REP
)
810 if (cur_loop_depth
> max_loop_depth
)
811 max_loop_depth
= cur_loop_depth
;
813 else if (ins
.handler_idx
== WINED3DSIH_ENDLOOP
814 || ins
.handler_idx
== WINED3DSIH_ENDREP
)
818 shader_record_register_usage(shader
, reg_maps
, &ins
.predicate
->reg
, shader_version
.type
);
820 for (i
= 0; i
< ins
.src_count
; ++i
)
822 unsigned int count
= get_instr_extra_regcount(ins
.handler_idx
, i
);
823 struct wined3d_shader_register reg
= ins
.src
[i
].reg
;
825 shader_record_register_usage(shader
, reg_maps
, &ins
.src
[i
].reg
, shader_version
.type
);
829 shader_record_register_usage(shader
, reg_maps
, ®
, shader_version
.type
);
835 if (ins
.src
[i
].reg
.type
== WINED3DSPR_TEMP
836 && ins
.src
[i
].swizzle
== WINED3DSP_NOSWIZZLE
)
838 shader
->u
.ps
.color0_mov
= TRUE
;
839 shader
->u
.ps
.color0_reg
= ins
.src
[i
].reg
.idx
[0].offset
;
845 reg_maps
->loop_depth
= max_loop_depth
;
847 /* PS before 2.0 don't have explicit color outputs. Instead the value of
848 * R0 is written to the render target. */
849 if (shader_version
.major
< 2 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
850 reg_maps
->rt_mask
|= (1 << 0);
852 shader
->functionLength
= ((const char *)ptr
- (const char *)byte_code
);
857 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int max
)
859 DWORD map
= 1 << max
;
861 map
&= reg_maps
->shader_version
.major
< 3 ? ~reg_maps
->texcoord
: ~reg_maps
->input_registers
;
863 return wined3d_log2i(map
);
866 static void shader_dump_decl_usage(const struct wined3d_shader_semantic
*semantic
,
867 const struct wined3d_shader_version
*shader_version
)
871 if (semantic
->reg
.reg
.type
== WINED3DSPR_SAMPLER
)
873 switch (semantic
->sampler_type
)
875 case WINED3DSTT_2D
: TRACE("_2d"); break;
876 case WINED3DSTT_CUBE
: TRACE("_cube"); break;
877 case WINED3DSTT_VOLUME
: TRACE("_volume"); break;
878 default: TRACE("_unknown_ttype(0x%08x)", semantic
->sampler_type
);
883 /* Pixel shaders 3.0 don't have usage semantics. */
884 if (shader_version
->major
< 3 && shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
) return;
887 switch (semantic
->usage
)
889 case WINED3D_DECL_USAGE_POSITION
:
890 TRACE("position%u", semantic
->usage_idx
);
893 case WINED3D_DECL_USAGE_BLEND_INDICES
:
897 case WINED3D_DECL_USAGE_BLEND_WEIGHT
:
901 case WINED3D_DECL_USAGE_NORMAL
:
902 TRACE("normal%u", semantic
->usage_idx
);
905 case WINED3D_DECL_USAGE_PSIZE
:
909 case WINED3D_DECL_USAGE_COLOR
:
910 if (!semantic
->usage_idx
) TRACE("color");
911 else TRACE("specular%u", (semantic
->usage_idx
- 1));
914 case WINED3D_DECL_USAGE_TEXCOORD
:
915 TRACE("texture%u", semantic
->usage_idx
);
918 case WINED3D_DECL_USAGE_TANGENT
:
922 case WINED3D_DECL_USAGE_BINORMAL
:
926 case WINED3D_DECL_USAGE_TESS_FACTOR
:
930 case WINED3D_DECL_USAGE_POSITIONT
:
931 TRACE("positionT%u", semantic
->usage_idx
);
934 case WINED3D_DECL_USAGE_FOG
:
938 case WINED3D_DECL_USAGE_DEPTH
:
942 case WINED3D_DECL_USAGE_SAMPLE
:
947 FIXME("unknown_semantics(0x%08x)", semantic
->usage
);
952 static void shader_dump_register(const struct wined3d_shader_register
*reg
,
953 const struct wined3d_shader_version
*shader_version
)
955 static const char * const rastout_reg_names
[] = {"oPos", "oFog", "oPts"};
956 static const char * const misctype_reg_names
[] = {"vPos", "vFace"};
957 UINT offset
= reg
->idx
[0].offset
;
961 case WINED3DSPR_TEMP
:
965 case WINED3DSPR_INPUT
:
969 case WINED3DSPR_CONST
:
970 case WINED3DSPR_CONST2
:
971 case WINED3DSPR_CONST3
:
972 case WINED3DSPR_CONST4
:
974 offset
= shader_get_float_offset(reg
->type
, offset
);
977 case WINED3DSPR_TEXTURE
: /* vs: case WINED3DSPR_ADDR */
978 TRACE("%c", shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
? 't' : 'a');
981 case WINED3DSPR_RASTOUT
:
982 TRACE("%s", rastout_reg_names
[offset
]);
985 case WINED3DSPR_COLOROUT
:
989 case WINED3DSPR_DEPTHOUT
:
993 case WINED3DSPR_ATTROUT
:
997 case WINED3DSPR_TEXCRDOUT
:
998 /* Vertex shaders >= 3.0 use general purpose output registers
999 * (WINED3DSPR_OUTPUT), which can include an address token. */
1000 if (shader_version
->major
>= 3) TRACE("o");
1004 case WINED3DSPR_CONSTINT
:
1008 case WINED3DSPR_CONSTBOOL
:
1012 case WINED3DSPR_LABEL
:
1016 case WINED3DSPR_LOOP
:
1020 case WINED3DSPR_SAMPLER
:
1024 case WINED3DSPR_MISCTYPE
:
1026 FIXME("Unhandled misctype register %u.\n", offset
);
1028 TRACE("%s", misctype_reg_names
[offset
]);
1031 case WINED3DSPR_PREDICATE
:
1035 case WINED3DSPR_IMMCONST
:
1039 case WINED3DSPR_CONSTBUFFER
:
1043 case WINED3DSPR_PRIMID
:
1047 case WINED3DSPR_NULL
:
1051 case WINED3DSPR_RESOURCE
:
1056 TRACE("unhandled_rtype(%#x)", reg
->type
);
1060 if (reg
->type
== WINED3DSPR_IMMCONST
)
1063 switch (reg
->immconst_type
)
1065 case WINED3D_IMMCONST_SCALAR
:
1066 switch (reg
->data_type
)
1068 case WINED3D_DATA_FLOAT
:
1069 TRACE("%.8e", *(const float *)reg
->immconst_data
);
1071 case WINED3D_DATA_INT
:
1072 TRACE("%d", reg
->immconst_data
[0]);
1074 case WINED3D_DATA_RESOURCE
:
1075 case WINED3D_DATA_SAMPLER
:
1076 case WINED3D_DATA_UINT
:
1077 TRACE("%u", reg
->immconst_data
[0]);
1080 TRACE("<unhandled data type %#x>", reg
->data_type
);
1085 case WINED3D_IMMCONST_VEC4
:
1086 switch (reg
->data_type
)
1088 case WINED3D_DATA_FLOAT
:
1089 TRACE("%.8e, %.8e, %.8e, %.8e",
1090 *(const float *)®
->immconst_data
[0], *(const float *)®
->immconst_data
[1],
1091 *(const float *)®
->immconst_data
[2], *(const float *)®
->immconst_data
[3]);
1093 case WINED3D_DATA_INT
:
1094 TRACE("%d, %d, %d, %d",
1095 reg
->immconst_data
[0], reg
->immconst_data
[1],
1096 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1098 case WINED3D_DATA_RESOURCE
:
1099 case WINED3D_DATA_SAMPLER
:
1100 case WINED3D_DATA_UINT
:
1101 TRACE("%u, %u, %u, %u",
1102 reg
->immconst_data
[0], reg
->immconst_data
[1],
1103 reg
->immconst_data
[2], reg
->immconst_data
[3]);
1106 TRACE("<unhandled data type %#x>", reg
->data_type
);
1112 TRACE("<unhandled immconst_type %#x>", reg
->immconst_type
);
1117 else if (reg
->type
!= WINED3DSPR_RASTOUT
1118 && reg
->type
!= WINED3DSPR_MISCTYPE
1119 && reg
->type
!= WINED3DSPR_NULL
)
1124 if (reg
->idx
[0].rel_addr
)
1126 shader_dump_src_param(reg
->idx
[0].rel_addr
, shader_version
);
1129 TRACE("%u]", offset
);
1131 if (reg
->idx
[1].offset
!= ~0U)
1134 if (reg
->idx
[1].rel_addr
)
1136 shader_dump_src_param(reg
->idx
[1].rel_addr
, shader_version
);
1139 TRACE("%u]", reg
->idx
[1].offset
);
1145 void shader_dump_dst_param(const struct wined3d_shader_dst_param
*param
,
1146 const struct wined3d_shader_version
*shader_version
)
1148 DWORD write_mask
= param
->write_mask
;
1150 shader_dump_register(¶m
->reg
, shader_version
);
1152 if (write_mask
&& write_mask
!= WINED3DSP_WRITEMASK_ALL
)
1154 static const char *write_mask_chars
= "xyzw";
1157 if (write_mask
& WINED3DSP_WRITEMASK_0
) TRACE("%c", write_mask_chars
[0]);
1158 if (write_mask
& WINED3DSP_WRITEMASK_1
) TRACE("%c", write_mask_chars
[1]);
1159 if (write_mask
& WINED3DSP_WRITEMASK_2
) TRACE("%c", write_mask_chars
[2]);
1160 if (write_mask
& WINED3DSP_WRITEMASK_3
) TRACE("%c", write_mask_chars
[3]);
1164 void shader_dump_src_param(const struct wined3d_shader_src_param
*param
,
1165 const struct wined3d_shader_version
*shader_version
)
1167 enum wined3d_shader_src_modifier src_modifier
= param
->modifiers
;
1168 DWORD swizzle
= param
->swizzle
;
1170 if (src_modifier
== WINED3DSPSM_NEG
1171 || src_modifier
== WINED3DSPSM_BIASNEG
1172 || src_modifier
== WINED3DSPSM_SIGNNEG
1173 || src_modifier
== WINED3DSPSM_X2NEG
1174 || src_modifier
== WINED3DSPSM_ABSNEG
)
1176 else if (src_modifier
== WINED3DSPSM_COMP
)
1178 else if (src_modifier
== WINED3DSPSM_NOT
)
1181 if (src_modifier
== WINED3DSPSM_ABS
|| src_modifier
== WINED3DSPSM_ABSNEG
)
1184 shader_dump_register(¶m
->reg
, shader_version
);
1188 switch (src_modifier
)
1190 case WINED3DSPSM_NONE
: break;
1191 case WINED3DSPSM_NEG
: break;
1192 case WINED3DSPSM_NOT
: break;
1193 case WINED3DSPSM_BIAS
: TRACE("_bias"); break;
1194 case WINED3DSPSM_BIASNEG
: TRACE("_bias"); break;
1195 case WINED3DSPSM_SIGN
: TRACE("_bx2"); break;
1196 case WINED3DSPSM_SIGNNEG
: TRACE("_bx2"); break;
1197 case WINED3DSPSM_COMP
: break;
1198 case WINED3DSPSM_X2
: TRACE("_x2"); break;
1199 case WINED3DSPSM_X2NEG
: TRACE("_x2"); break;
1200 case WINED3DSPSM_DZ
: TRACE("_dz"); break;
1201 case WINED3DSPSM_DW
: TRACE("_dw"); break;
1202 case WINED3DSPSM_ABSNEG
: TRACE(")"); break;
1203 case WINED3DSPSM_ABS
: TRACE(")"); break;
1204 default: TRACE("_unknown_modifier(%#x)", src_modifier
);
1208 if (swizzle
!= WINED3DSP_NOSWIZZLE
)
1210 static const char *swizzle_chars
= "xyzw";
1211 DWORD swizzle_x
= swizzle
& 0x03;
1212 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
1213 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
1214 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
1216 if (swizzle_x
== swizzle_y
1217 && swizzle_x
== swizzle_z
1218 && swizzle_x
== swizzle_w
)
1220 TRACE(".%c", swizzle_chars
[swizzle_x
]);
1224 TRACE(".%c%c%c%c", swizzle_chars
[swizzle_x
], swizzle_chars
[swizzle_y
],
1225 swizzle_chars
[swizzle_z
], swizzle_chars
[swizzle_w
]);
1230 /* Shared code in order to generate the bulk of the shader string.
1231 * NOTE: A description of how to parse tokens can be found on MSDN. */
1232 void shader_generate_main(const struct wined3d_shader
*shader
, struct wined3d_shader_buffer
*buffer
,
1233 const struct wined3d_shader_reg_maps
*reg_maps
, const DWORD
*byte_code
, void *backend_ctx
)
1235 struct wined3d_device
*device
= shader
->device
;
1236 const struct wined3d_shader_frontend
*fe
= shader
->frontend
;
1237 void *fe_data
= shader
->frontend_data
;
1238 struct wined3d_shader_version shader_version
;
1239 struct wined3d_shader_loop_state loop_state
;
1240 struct wined3d_shader_instruction ins
;
1241 struct wined3d_shader_tex_mx tex_mx
;
1242 struct wined3d_shader_context ctx
;
1243 const DWORD
*ptr
= byte_code
;
1245 /* Initialize current parsing state. */
1246 tex_mx
.current_row
= 0;
1247 loop_state
.current_depth
= 0;
1248 loop_state
.current_reg
= 0;
1250 ctx
.shader
= shader
;
1251 ctx
.gl_info
= &device
->adapter
->gl_info
;
1252 ctx
.reg_maps
= reg_maps
;
1253 ctx
.buffer
= buffer
;
1254 ctx
.tex_mx
= &tex_mx
;
1255 ctx
.loop_state
= &loop_state
;
1256 ctx
.backend_data
= backend_ctx
;
1259 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1261 while (!fe
->shader_is_end(fe_data
, &ptr
))
1264 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1266 /* Unknown opcode and its parameters. */
1267 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1269 TRACE("Skipping unrecognized instruction.\n");
1274 FIXME("Predicates not implemented.\n");
1276 /* Call appropriate function for output target */
1277 device
->shader_backend
->shader_handle_instruction(&ins
);
1281 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param
*dst
)
1283 DWORD mmask
= dst
->modifiers
;
1288 case 13: TRACE("_d8"); break;
1289 case 14: TRACE("_d4"); break;
1290 case 15: TRACE("_d2"); break;
1291 case 1: TRACE("_x2"); break;
1292 case 2: TRACE("_x4"); break;
1293 case 3: TRACE("_x8"); break;
1294 default: TRACE("_unhandled_shift(%d)", dst
->shift
); break;
1297 if (mmask
& WINED3DSPDM_SATURATE
) TRACE("_sat");
1298 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) TRACE("_pp");
1299 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) TRACE("_centroid");
1301 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
1302 if (mmask
) FIXME("_unrecognized_modifier(%#x)", mmask
);
1305 static void shader_dump_primitive_type(enum wined3d_primitive_type primitive_type
)
1307 switch (primitive_type
)
1309 case WINED3D_PT_UNDEFINED
:
1312 case WINED3D_PT_POINTLIST
:
1315 case WINED3D_PT_LINELIST
:
1318 case WINED3D_PT_LINESTRIP
:
1321 case WINED3D_PT_TRIANGLELIST
:
1322 TRACE("trianglelist");
1324 case WINED3D_PT_TRIANGLESTRIP
:
1325 TRACE("trianglestrip");
1327 case WINED3D_PT_TRIANGLEFAN
:
1328 TRACE("trianglefan");
1330 case WINED3D_PT_LINELIST_ADJ
:
1331 TRACE("linelist_adj");
1333 case WINED3D_PT_LINESTRIP_ADJ
:
1334 TRACE("linestrip_adj");
1336 case WINED3D_PT_TRIANGLELIST_ADJ
:
1337 TRACE("trianglelist_adj");
1339 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
1340 TRACE("trianglestrip_adj");
1343 TRACE("<unrecognized_primitive_type %#x>", primitive_type
);
1348 static void shader_trace_init(const struct wined3d_shader_frontend
*fe
, void *fe_data
, const DWORD
*byte_code
)
1350 struct wined3d_shader_version shader_version
;
1351 const DWORD
*ptr
= byte_code
;
1352 const char *type_prefix
;
1355 TRACE("Parsing %p.\n", byte_code
);
1357 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1359 switch (shader_version
.type
)
1361 case WINED3D_SHADER_TYPE_VERTEX
:
1365 case WINED3D_SHADER_TYPE_GEOMETRY
:
1369 case WINED3D_SHADER_TYPE_PIXEL
:
1374 FIXME("Unhandled shader type %#x.\n", shader_version
.type
);
1375 type_prefix
= "unknown";
1379 TRACE("%s_%u_%u\n", type_prefix
, shader_version
.major
, shader_version
.minor
);
1381 while (!fe
->shader_is_end(fe_data
, &ptr
))
1383 struct wined3d_shader_instruction ins
;
1385 fe
->shader_read_instruction(fe_data
, &ptr
, &ins
);
1386 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1388 TRACE("Skipping unrecognized instruction.\n");
1392 if (ins
.handler_idx
== WINED3DSIH_DCL
)
1394 shader_dump_decl_usage(&ins
.declaration
.semantic
, &shader_version
);
1395 shader_dump_ins_modifiers(&ins
.declaration
.semantic
.reg
);
1397 shader_dump_dst_param(&ins
.declaration
.semantic
.reg
, &shader_version
);
1399 else if (ins
.handler_idx
== WINED3DSIH_DCL_CONSTANT_BUFFER
)
1401 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1402 shader_dump_src_param(&ins
.declaration
.src
, &shader_version
);
1403 TRACE(", %s", ins
.flags
& WINED3DSI_INDEXED_DYNAMIC
? "dynamicIndexed" : "immediateIndexed");
1405 else if (ins
.handler_idx
== WINED3DSIH_DCL_INPUT_PRIMITIVE
1406 || ins
.handler_idx
== WINED3DSIH_DCL_OUTPUT_TOPOLOGY
)
1408 TRACE("%s ", shader_opcode_names
[ins
.handler_idx
]);
1409 shader_dump_primitive_type(ins
.declaration
.primitive_type
);
1411 else if (ins
.handler_idx
== WINED3DSIH_DCL_VERTICES_OUT
)
1413 TRACE("%s %u", shader_opcode_names
[ins
.handler_idx
], ins
.declaration
.count
);
1415 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
1417 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(ins
.dst
[0].reg
.type
,
1418 ins
.dst
[0].reg
.idx
[0].offset
),
1419 *(const float *)&ins
.src
[0].reg
.immconst_data
[0],
1420 *(const float *)&ins
.src
[0].reg
.immconst_data
[1],
1421 *(const float *)&ins
.src
[0].reg
.immconst_data
[2],
1422 *(const float *)&ins
.src
[0].reg
.immconst_data
[3]);
1424 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
1426 TRACE("defi i%u = %d, %d, %d, %d", ins
.dst
[0].reg
.idx
[0].offset
,
1427 ins
.src
[0].reg
.immconst_data
[0],
1428 ins
.src
[0].reg
.immconst_data
[1],
1429 ins
.src
[0].reg
.immconst_data
[2],
1430 ins
.src
[0].reg
.immconst_data
[3]);
1432 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
1434 TRACE("defb b%u = %s", ins
.dst
[0].reg
.idx
[0].offset
, ins
.src
[0].reg
.immconst_data
[0] ? "true" : "false");
1441 shader_dump_src_param(ins
.predicate
, &shader_version
);
1445 /* PixWin marks instructions with the coissue flag with a '+' */
1446 if (ins
.coissue
) TRACE("+");
1448 TRACE("%s", shader_opcode_names
[ins
.handler_idx
]);
1450 if (ins
.handler_idx
== WINED3DSIH_IFC
1451 || ins
.handler_idx
== WINED3DSIH_BREAKC
)
1455 case WINED3D_SHADER_REL_OP_GT
: TRACE("_gt"); break;
1456 case WINED3D_SHADER_REL_OP_EQ
: TRACE("_eq"); break;
1457 case WINED3D_SHADER_REL_OP_GE
: TRACE("_ge"); break;
1458 case WINED3D_SHADER_REL_OP_LT
: TRACE("_lt"); break;
1459 case WINED3D_SHADER_REL_OP_NE
: TRACE("_ne"); break;
1460 case WINED3D_SHADER_REL_OP_LE
: TRACE("_le"); break;
1461 default: TRACE("_(%u)", ins
.flags
);
1464 else if (ins
.handler_idx
== WINED3DSIH_TEX
1465 && shader_version
.major
>= 2
1466 && (ins
.flags
& WINED3DSI_TEXLD_PROJECT
))
1471 for (i
= 0; i
< ins
.dst_count
; ++i
)
1473 shader_dump_ins_modifiers(&ins
.dst
[i
]);
1474 TRACE(!i
? " " : ", ");
1475 shader_dump_dst_param(&ins
.dst
[i
], &shader_version
);
1478 /* Other source tokens */
1479 for (i
= ins
.dst_count
; i
< (ins
.dst_count
+ ins
.src_count
); ++i
)
1481 TRACE(!i
? " " : ", ");
1482 shader_dump_src_param(&ins
.src
[i
- ins
.dst_count
], &shader_version
);
1489 static void shader_cleanup(struct wined3d_shader
*shader
)
1491 shader
->device
->shader_backend
->shader_destroy(shader
);
1492 HeapFree(GetProcessHeap(), 0, shader
->reg_maps
.constf
);
1493 HeapFree(GetProcessHeap(), 0, shader
->function
);
1494 shader_delete_constant_list(&shader
->constantsF
);
1495 shader_delete_constant_list(&shader
->constantsB
);
1496 shader_delete_constant_list(&shader
->constantsI
);
1497 list_remove(&shader
->shader_list_entry
);
1499 if (shader
->frontend
&& shader
->frontend_data
)
1500 shader
->frontend
->shader_free(shader
->frontend_data
);
1503 struct shader_none_priv
1505 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
1506 const struct fragment_pipeline
*fragment_pipe
;
1507 BOOL ffp_proj_control
;
1510 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
1511 static void shader_none_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
1512 enum tex_types tex_type
, const SIZE
*ds_mask_size
) {}
1513 static void shader_none_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
) {}
1514 static void shader_none_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1515 static void shader_none_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
) {}
1516 static void shader_none_load_constants(void *shader_priv
, struct wined3d_context
*context
,
1517 const struct wined3d_state
*state
) {}
1518 static void shader_none_destroy(struct wined3d_shader
*shader
) {}
1519 static void shader_none_free_context_data(struct wined3d_context
*context
) {}
1521 /* Context activation is done by the caller. */
1522 static void shader_none_select(void *shader_priv
, struct wined3d_context
*context
,
1523 const struct wined3d_state
*state
)
1525 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1526 struct shader_none_priv
*priv
= shader_priv
;
1528 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
1529 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
1532 /* Context activation is done by the caller. */
1533 static void shader_none_disable(void *shader_priv
, struct wined3d_context
*context
)
1535 struct shader_none_priv
*priv
= shader_priv
;
1536 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1538 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
1539 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
1541 context
->shader_update_mask
= (1 << WINED3D_SHADER_TYPE_PIXEL
)
1542 | (1 << WINED3D_SHADER_TYPE_VERTEX
)
1543 | (1 << WINED3D_SHADER_TYPE_GEOMETRY
);
1546 static HRESULT
shader_none_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
1547 const struct fragment_pipeline
*fragment_pipe
)
1549 struct fragment_caps fragment_caps
;
1550 void *vertex_priv
, *fragment_priv
;
1551 struct shader_none_priv
*priv
;
1553 if (!(priv
= HeapAlloc(GetProcessHeap(), 0, sizeof(*priv
))))
1554 return E_OUTOFMEMORY
;
1556 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&none_shader_backend
, priv
)))
1558 ERR("Failed to initialize vertex pipe.\n");
1559 HeapFree(GetProcessHeap(), 0, priv
);
1563 if (!(fragment_priv
= fragment_pipe
->alloc_private(&none_shader_backend
, priv
)))
1565 ERR("Failed to initialize fragment pipe.\n");
1566 vertex_pipe
->vp_free(device
);
1567 HeapFree(GetProcessHeap(), 0, priv
);
1571 priv
->vertex_pipe
= vertex_pipe
;
1572 priv
->fragment_pipe
= fragment_pipe
;
1573 fragment_pipe
->get_caps(&device
->adapter
->gl_info
, &fragment_caps
);
1574 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
1576 device
->vertex_priv
= vertex_priv
;
1577 device
->fragment_priv
= fragment_priv
;
1578 device
->shader_priv
= priv
;
1583 static void shader_none_free(struct wined3d_device
*device
)
1585 struct shader_none_priv
*priv
= device
->shader_priv
;
1587 priv
->fragment_pipe
->free_private(device
);
1588 priv
->vertex_pipe
->vp_free(device
);
1589 HeapFree(GetProcessHeap(), 0, priv
);
1592 static BOOL
shader_none_allocate_context_data(struct wined3d_context
*context
)
1597 static void shader_none_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
1599 /* Set the shader caps to 0 for the none shader backend */
1600 caps
->vs_version
= 0;
1601 caps
->gs_version
= 0;
1602 caps
->ps_version
= 0;
1603 caps
->vs_uniform_count
= 0;
1604 caps
->ps_uniform_count
= 0;
1605 caps
->ps_1x_max_value
= 0.0f
;
1606 caps
->wined3d_caps
= 0;
1609 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
1611 /* We "support" every possible fixup, since we don't support any shader
1612 * model, and will never have to actually sample a texture. */
1616 static BOOL
shader_none_has_ffp_proj_control(void *shader_priv
)
1618 struct shader_none_priv
*priv
= shader_priv
;
1620 return priv
->ffp_proj_control
;
1623 const struct wined3d_shader_backend_ops none_shader_backend
=
1625 shader_none_handle_instruction
,
1627 shader_none_disable
,
1628 shader_none_select_depth_blt
,
1629 shader_none_deselect_depth_blt
,
1630 shader_none_update_float_vertex_constants
,
1631 shader_none_update_float_pixel_constants
,
1632 shader_none_load_constants
,
1633 shader_none_destroy
,
1636 shader_none_allocate_context_data
,
1637 shader_none_free_context_data
,
1638 shader_none_get_caps
,
1639 shader_none_color_fixup_supported
,
1640 shader_none_has_ffp_proj_control
,
1643 static HRESULT
shader_set_function(struct wined3d_shader
*shader
, const DWORD
*byte_code
,
1644 const struct wined3d_shader_signature
*output_signature
, DWORD float_const_count
,
1645 enum wined3d_shader_type type
, unsigned int max_version
)
1647 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1648 const struct wined3d_shader_frontend
*fe
;
1650 unsigned int backend_version
;
1651 const struct wined3d_d3d_info
*d3d_info
= &shader
->device
->adapter
->d3d_info
;
1653 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1654 shader
, byte_code
, output_signature
, float_const_count
);
1656 fe
= shader_select_frontend(*byte_code
);
1659 FIXME("Unable to find frontend for shader.\n");
1660 return WINED3DERR_INVALIDCALL
;
1662 shader
->frontend
= fe
;
1663 shader
->frontend_data
= fe
->shader_init(byte_code
, output_signature
);
1664 if (!shader
->frontend_data
)
1666 FIXME("Failed to initialize frontend.\n");
1667 return WINED3DERR_INVALIDCALL
;
1670 /* First pass: trace shader. */
1671 if (TRACE_ON(d3d_shader
))
1672 shader_trace_init(fe
, shader
->frontend_data
, byte_code
);
1674 /* Initialize immediate constant lists. */
1675 list_init(&shader
->constantsF
);
1676 list_init(&shader
->constantsB
);
1677 list_init(&shader
->constantsI
);
1678 shader
->lconst_inf_or_nan
= FALSE
;
1680 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1681 hr
= shader_get_registers_used(shader
, fe
,
1682 reg_maps
, shader
->input_signature
, shader
->output_signature
,
1683 byte_code
, float_const_count
);
1684 if (FAILED(hr
)) return hr
;
1686 if (reg_maps
->shader_version
.type
!= type
)
1688 WARN("Wrong shader type %d.\n", reg_maps
->shader_version
.type
);
1689 return WINED3DERR_INVALIDCALL
;
1691 if (reg_maps
->shader_version
.major
> max_version
)
1693 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps
->shader_version
.major
);
1694 return WINED3DERR_INVALIDCALL
;
1698 case WINED3D_SHADER_TYPE_VERTEX
:
1699 backend_version
= d3d_info
->limits
.vs_version
;
1701 case WINED3D_SHADER_TYPE_GEOMETRY
:
1702 backend_version
= d3d_info
->limits
.gs_version
;
1704 case WINED3D_SHADER_TYPE_PIXEL
:
1705 backend_version
= d3d_info
->limits
.ps_version
;
1708 FIXME("No backend version-checking for this shader type\n");
1709 backend_version
= 0;
1711 if (reg_maps
->shader_version
.major
> backend_version
)
1713 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
1714 reg_maps
->shader_version
.major
, reg_maps
->shader_version
.minor
);
1715 return WINED3DERR_INVALIDCALL
;
1718 shader
->function
= HeapAlloc(GetProcessHeap(), 0, shader
->functionLength
);
1719 if (!shader
->function
)
1720 return E_OUTOFMEMORY
;
1721 memcpy(shader
->function
, byte_code
, shader
->functionLength
);
1726 ULONG CDECL
wined3d_shader_incref(struct wined3d_shader
*shader
)
1728 ULONG refcount
= InterlockedIncrement(&shader
->ref
);
1730 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
1735 ULONG CDECL
wined3d_shader_decref(struct wined3d_shader
*shader
)
1737 ULONG refcount
= InterlockedDecrement(&shader
->ref
);
1739 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
1743 shader_cleanup(shader
);
1744 shader
->parent_ops
->wined3d_object_destroyed(shader
->parent
);
1745 HeapFree(GetProcessHeap(), 0, shader
);
1751 void * CDECL
wined3d_shader_get_parent(const struct wined3d_shader
*shader
)
1753 TRACE("shader %p.\n", shader
);
1755 return shader
->parent
;
1758 HRESULT CDECL
wined3d_shader_get_byte_code(const struct wined3d_shader
*shader
,
1759 void *byte_code
, UINT
*byte_code_size
)
1761 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader
, byte_code
, byte_code_size
);
1765 *byte_code_size
= shader
->functionLength
;
1769 if (*byte_code_size
< shader
->functionLength
)
1771 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
1772 * than the required size we should write the required size and
1773 * return D3DERR_MOREDATA. That's not actually true. */
1774 return WINED3DERR_INVALIDCALL
;
1777 memcpy(byte_code
, shader
->function
, shader
->functionLength
);
1782 /* Set local constants for d3d8 shaders. */
1783 HRESULT CDECL
wined3d_shader_set_local_constants_float(struct wined3d_shader
*shader
,
1784 UINT start_idx
, const float *src_data
, UINT count
)
1786 UINT end_idx
= start_idx
+ count
;
1789 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader
, start_idx
, src_data
, count
);
1791 if (end_idx
> shader
->limits
.constant_float
)
1793 WARN("end_idx %u > float constants limit %u.\n",
1794 end_idx
, shader
->limits
.constant_float
);
1795 end_idx
= shader
->limits
.constant_float
;
1798 for (i
= start_idx
; i
< end_idx
; ++i
)
1800 struct wined3d_shader_lconst
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst
));
1803 return E_OUTOFMEMORY
;
1806 value
= (float *)lconst
->value
;
1807 memcpy(value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
1808 list_add_head(&shader
->constantsF
, &lconst
->entry
);
1810 if (isinf(value
[0]) || isnan(value
[0]) || isinf(value
[1]) || isnan(value
[1])
1811 || isinf(value
[2]) || isnan(value
[2]) || isinf(value
[3]) || isnan(value
[3]))
1813 shader
->lconst_inf_or_nan
= TRUE
;
1820 void find_vs_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
1821 WORD swizzle_map
, struct vs_compile_args
*args
)
1823 args
->fog_src
= state
->render_states
[WINED3D_RS_FOGTABLEMODE
]
1824 == WINED3D_FOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
1825 args
->clip_enabled
= state
->render_states
[WINED3D_RS_CLIPPING
]
1826 && state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
];
1827 args
->swizzle_map
= swizzle_map
;
1830 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
1832 if (usage_idx1
!= usage_idx2
)
1834 if (usage1
== usage2
)
1836 if (usage1
== WINED3D_DECL_USAGE_POSITION
&& usage2
== WINED3D_DECL_USAGE_POSITIONT
)
1838 if (usage2
== WINED3D_DECL_USAGE_POSITION
&& usage1
== WINED3D_DECL_USAGE_POSITIONT
)
1844 BOOL
vshader_get_input(const struct wined3d_shader
*shader
,
1845 BYTE usage_req
, BYTE usage_idx_req
, unsigned int *regnum
)
1847 WORD map
= shader
->reg_maps
.input_registers
;
1850 for (i
= 0; map
; map
>>= 1, ++i
)
1852 if (!(map
& 1)) continue;
1854 if (match_usage(shader
->u
.vs
.attributes
[i
].usage
,
1855 shader
->u
.vs
.attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
1864 static void vertexshader_set_limits(struct wined3d_shader
*shader
)
1866 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
1867 shader
->reg_maps
.shader_version
.minor
);
1868 struct wined3d_device
*device
= shader
->device
;
1869 const DWORD vs_uniform_count
= device
->adapter
->d3d_info
.limits
.vs_uniform_count
;
1871 shader
->limits
.packed_input
= 0;
1873 switch (shader_version
)
1875 case WINED3D_SHADER_VERSION(1, 0):
1876 case WINED3D_SHADER_VERSION(1, 1):
1877 shader
->limits
.constant_bool
= 0;
1878 shader
->limits
.constant_int
= 0;
1879 shader
->limits
.packed_output
= 12;
1880 shader
->limits
.sampler
= 0;
1881 /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1882 * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1884 shader
->limits
.constant_float
= min(256, vs_uniform_count
);
1887 case WINED3D_SHADER_VERSION(2, 0):
1888 case WINED3D_SHADER_VERSION(2, 1):
1889 shader
->limits
.constant_bool
= 16;
1890 shader
->limits
.constant_int
= 16;
1891 shader
->limits
.packed_output
= 12;
1892 shader
->limits
.sampler
= 0;
1893 shader
->limits
.constant_float
= min(256, vs_uniform_count
);
1896 case WINED3D_SHADER_VERSION(3, 0):
1897 shader
->limits
.constant_bool
= 16;
1898 shader
->limits
.constant_int
= 16;
1899 shader
->limits
.packed_output
= 12;
1900 shader
->limits
.sampler
= 4;
1901 /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1902 * even though they are capable of supporting much more (GL
1903 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1904 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1905 * shaders to 256. */
1906 shader
->limits
.constant_float
= min(256, vs_uniform_count
);
1909 case WINED3D_SHADER_VERSION(4, 0):
1910 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
1911 shader
->limits
.constant_int
= 0;
1912 shader
->limits
.constant_float
= 0;
1913 shader
->limits
.constant_bool
= 0;
1914 shader
->limits
.packed_output
= 16;
1915 shader
->limits
.packed_input
= 0;
1919 shader
->limits
.constant_bool
= 16;
1920 shader
->limits
.constant_int
= 16;
1921 shader
->limits
.packed_output
= 12;
1922 shader
->limits
.sampler
= 0;
1923 shader
->limits
.constant_float
= min(256, vs_uniform_count
);
1924 FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1925 shader
->reg_maps
.shader_version
.major
,
1926 shader
->reg_maps
.shader_version
.minor
);
1930 static HRESULT
vertexshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
1931 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
1932 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
1934 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1938 const DWORD vs_uniform_count
= device
->adapter
->d3d_info
.limits
.vs_uniform_count
;
1940 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
1942 shader_init(shader
, device
, parent
, parent_ops
);
1943 hr
= shader_set_function(shader
, byte_code
, output_signature
, vs_uniform_count
,
1944 WINED3D_SHADER_TYPE_VERTEX
, max_version
);
1947 WARN("Failed to set function, hr %#x.\n", hr
);
1948 shader_cleanup(shader
);
1952 map
= reg_maps
->input_registers
;
1953 for (i
= 0; map
; map
>>= 1, ++i
)
1955 if (!(map
& 1) || !shader
->input_signature
[i
].semantic_name
)
1958 shader
->u
.vs
.attributes
[i
].usage
=
1959 shader_usage_from_semantic_name(shader
->input_signature
[i
].semantic_name
);
1960 shader
->u
.vs
.attributes
[i
].usage_idx
= shader
->input_signature
[i
].semantic_idx
;
1963 if (output_signature
)
1965 for (i
= 0; i
< output_signature
->element_count
; ++i
)
1967 struct wined3d_shader_signature_element
*e
= &output_signature
->elements
[i
];
1968 reg_maps
->output_registers
|= 1 << e
->register_idx
;
1969 shader
->output_signature
[e
->register_idx
] = *e
;
1973 vertexshader_set_limits(shader
);
1975 shader
->load_local_constsF
= (reg_maps
->usesrelconstF
&& !list_empty(&shader
->constantsF
)) ||
1976 shader
->lconst_inf_or_nan
;
1981 static void geometryshader_set_limits(struct wined3d_shader
*shader
)
1983 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
1984 shader
->reg_maps
.shader_version
.minor
);
1986 switch (shader_version
)
1988 case WINED3D_SHADER_VERSION(4, 0):
1989 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
1990 shader
->limits
.constant_int
= 0;
1991 shader
->limits
.constant_float
= 0;
1992 shader
->limits
.constant_bool
= 0;
1993 shader
->limits
.packed_output
= 32;
1994 shader
->limits
.packed_input
= 16;
1998 memset(&shader
->limits
, 0, sizeof(shader
->limits
));
1999 FIXME("Unhandled geometry shader version \"%u.%u\".\n",
2000 shader
->reg_maps
.shader_version
.major
,
2001 shader
->reg_maps
.shader_version
.minor
);
2005 static HRESULT
geometryshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2006 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
2007 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
2011 shader_init(shader
, device
, parent
, parent_ops
);
2012 hr
= shader_set_function(shader
, byte_code
, output_signature
, 0,
2013 WINED3D_SHADER_TYPE_GEOMETRY
, max_version
);
2016 WARN("Failed to set function, hr %#x.\n", hr
);
2017 shader_cleanup(shader
);
2021 geometryshader_set_limits(shader
);
2023 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2028 void find_ps_compile_args(const struct wined3d_state
*state
, const struct wined3d_shader
*shader
,
2029 BOOL position_transformed
, struct ps_compile_args
*args
, const struct wined3d_gl_info
*gl_info
)
2031 const struct wined3d_texture
*texture
;
2034 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
2035 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && state
->render_states
[WINED3D_RS_SRGBWRITEENABLE
])
2037 const struct wined3d_surface
*rt
= state
->fb
->render_targets
[0];
2038 if (rt
->resource
.format
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
2040 static unsigned int warned
= 0;
2042 args
->srgb_correction
= 1;
2043 if (state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] && !warned
++)
2044 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
2045 "support, expect rendering artifacts.\n");
2049 if (shader
->reg_maps
.shader_version
.major
== 1
2050 && shader
->reg_maps
.shader_version
.minor
<= 3)
2052 for (i
= 0; i
< shader
->limits
.sampler
; ++i
)
2054 DWORD flags
= state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
];
2056 if (flags
& WINED3D_TTFF_PROJECTED
)
2058 DWORD tex_transform
= flags
& ~WINED3D_TTFF_PROJECTED
;
2060 if (!state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
2063 unsigned int index
= state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
];
2064 DWORD max_valid
= WINED3D_TTFF_COUNT4
;
2065 enum wined3d_sampler_texture_type sampler_type
= shader
->reg_maps
.sampler_type
[i
];
2067 for (j
= 0; j
< state
->vertex_declaration
->element_count
; ++j
)
2069 struct wined3d_vertex_declaration_element
*element
=
2070 &state
->vertex_declaration
->elements
[j
];
2072 if (element
->usage
== WINED3D_DECL_USAGE_TEXCOORD
2073 && element
->usage_idx
== index
)
2075 max_valid
= element
->format
->component_count
;
2079 if (!tex_transform
|| tex_transform
> max_valid
)
2081 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2082 tex_transform
, max_valid
);
2083 tex_transform
= max_valid
;
2085 if ((sampler_type
== WINED3DSTT_1D
&& tex_transform
> WINED3D_TTFF_COUNT1
)
2086 || (sampler_type
== WINED3DSTT_2D
&& tex_transform
> WINED3D_TTFF_COUNT2
)
2087 || (sampler_type
== WINED3DSTT_VOLUME
&& tex_transform
> WINED3D_TTFF_COUNT3
))
2088 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
2091 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2092 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2093 i
, tex_transform
, sampler_type
);
2097 tex_transform
= WINED3D_TTFF_COUNT4
| WINED3D_PSARGS_PROJECTED
;
2099 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
2103 if (shader
->reg_maps
.shader_version
.major
== 1
2104 && shader
->reg_maps
.shader_version
.minor
<= 4)
2106 for (i
= 0; i
< shader
->limits
.sampler
; ++i
)
2108 const struct wined3d_texture
*texture
= state
->textures
[i
];
2110 if (!shader
->reg_maps
.sampler_type
[i
])
2113 /* Treat unbound textures as 2D. The dummy texture will provide
2114 * the proper sample value. The tex_types bitmap defaults to
2115 * 2D because of the memset. */
2119 switch (texture
->target
)
2121 /* RECT textures are distinguished from 2D textures via np2_fixup */
2122 case GL_TEXTURE_RECTANGLE_ARB
:
2127 args
->tex_types
|= WINED3D_SHADER_TEX_3D
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
2130 case GL_TEXTURE_CUBE_MAP_ARB
:
2131 args
->tex_types
|= WINED3D_SHADER_TEX_CUBE
<< i
* WINED3D_PSARGS_TEXTYPE_SHIFT
;
2137 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2139 if (!shader
->reg_maps
.sampler_type
[i
])
2142 texture
= state
->textures
[i
];
2145 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
2148 args
->color_fixup
[i
] = texture
->resource
.format
->color_fixup
;
2150 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
2151 args
->shadow
|= 1 << i
;
2153 /* Flag samplers that need NP2 texcoord fixup. */
2154 if (!(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
2155 args
->np2_fixup
|= (1 << i
);
2157 if (shader
->reg_maps
.shader_version
.major
>= 3)
2159 if (position_transformed
)
2160 args
->vp_mode
= pretransformed
;
2161 else if (use_vs(state
))
2162 args
->vp_mode
= vertexshader
;
2164 args
->vp_mode
= fixedfunction
;
2165 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
2169 args
->vp_mode
= vertexshader
;
2170 if (state
->render_states
[WINED3D_RS_FOGENABLE
])
2172 switch (state
->render_states
[WINED3D_RS_FOGTABLEMODE
])
2174 case WINED3D_FOG_NONE
:
2175 if (position_transformed
|| use_vs(state
))
2177 args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
;
2181 switch (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
])
2183 case WINED3D_FOG_NONE
: /* Fall through. */
2184 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
2185 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
2186 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
2190 case WINED3D_FOG_LINEAR
: args
->fog
= WINED3D_FFP_PS_FOG_LINEAR
; break;
2191 case WINED3D_FOG_EXP
: args
->fog
= WINED3D_FFP_PS_FOG_EXP
; break;
2192 case WINED3D_FOG_EXP2
: args
->fog
= WINED3D_FFP_PS_FOG_EXP2
; break;
2197 args
->fog
= WINED3D_FFP_PS_FOG_OFF
;
2202 static void pixelshader_set_limits(struct wined3d_shader
*shader
)
2204 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->reg_maps
.shader_version
.major
,
2205 shader
->reg_maps
.shader_version
.minor
);
2207 shader
->limits
.packed_output
= 0;
2209 switch (shader_version
)
2211 case WINED3D_SHADER_VERSION(1, 0):
2212 case WINED3D_SHADER_VERSION(1, 1):
2213 case WINED3D_SHADER_VERSION(1, 2):
2214 case WINED3D_SHADER_VERSION(1, 3):
2215 shader
->limits
.constant_float
= 8;
2216 shader
->limits
.constant_int
= 0;
2217 shader
->limits
.constant_bool
= 0;
2218 shader
->limits
.sampler
= 4;
2219 shader
->limits
.packed_input
= 0;
2222 case WINED3D_SHADER_VERSION(1, 4):
2223 shader
->limits
.constant_float
= 8;
2224 shader
->limits
.constant_int
= 0;
2225 shader
->limits
.constant_bool
= 0;
2226 shader
->limits
.sampler
= 6;
2227 shader
->limits
.packed_input
= 0;
2230 /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2231 case WINED3D_SHADER_VERSION(2, 0):
2232 shader
->limits
.constant_float
= 32;
2233 shader
->limits
.constant_int
= 16;
2234 shader
->limits
.constant_bool
= 16;
2235 shader
->limits
.sampler
= 16;
2236 shader
->limits
.packed_input
= 0;
2239 case WINED3D_SHADER_VERSION(2, 1):
2240 shader
->limits
.constant_float
= 32;
2241 shader
->limits
.constant_int
= 16;
2242 shader
->limits
.constant_bool
= 16;
2243 shader
->limits
.sampler
= 16;
2244 shader
->limits
.packed_input
= 0;
2247 case WINED3D_SHADER_VERSION(3, 0):
2248 shader
->limits
.constant_float
= 224;
2249 shader
->limits
.constant_int
= 16;
2250 shader
->limits
.constant_bool
= 16;
2251 shader
->limits
.sampler
= 16;
2252 shader
->limits
.packed_input
= 12;
2255 case WINED3D_SHADER_VERSION(4, 0):
2256 shader
->limits
.sampler
= 16; /* FIXME: 128 resources, 16 sampler states */
2257 shader
->limits
.constant_int
= 0;
2258 shader
->limits
.constant_float
= 0;
2259 shader
->limits
.constant_bool
= 0;
2260 shader
->limits
.packed_input
= 32;
2264 shader
->limits
.constant_float
= 32;
2265 shader
->limits
.constant_int
= 16;
2266 shader
->limits
.constant_bool
= 16;
2267 shader
->limits
.sampler
= 16;
2268 shader
->limits
.packed_input
= 0;
2269 FIXME("Unrecognized pixel shader version %u.%u\n",
2270 shader
->reg_maps
.shader_version
.major
,
2271 shader
->reg_maps
.shader_version
.minor
);
2275 static HRESULT
pixelshader_init(struct wined3d_shader
*shader
, struct wined3d_device
*device
,
2276 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
2277 void *parent
, const struct wined3d_parent_ops
*parent_ops
, unsigned int max_version
)
2279 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2280 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
2282 const DWORD ps_uniform_count
= device
->adapter
->d3d_info
.limits
.ps_uniform_count
;
2284 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
2286 shader_init(shader
, device
, parent
, parent_ops
);
2287 hr
= shader_set_function(shader
, byte_code
, output_signature
, ps_uniform_count
,
2288 WINED3D_SHADER_TYPE_PIXEL
, max_version
);
2291 WARN("Failed to set function, hr %#x.\n", hr
);
2292 shader_cleanup(shader
);
2296 pixelshader_set_limits(shader
);
2298 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2300 if (shader
->u
.ps
.input_reg_used
[i
])
2303 highest_reg_used
= i
;
2307 /* Don't do any register mapping magic if it is not needed, or if we can't
2308 * achieve anything anyway */
2309 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
2310 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2312 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2314 /* This happens with relative addressing. The input mapper function
2315 * warns about this if the higher registers are declared too, so
2316 * don't write a FIXME here */
2317 WARN("More varying registers used than supported\n");
2320 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2322 shader
->u
.ps
.input_reg_map
[i
] = i
;
2325 shader
->u
.ps
.declared_in_count
= highest_reg_used
+ 1;
2329 shader
->u
.ps
.declared_in_count
= 0;
2330 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2332 if (shader
->u
.ps
.input_reg_used
[i
])
2333 shader
->u
.ps
.input_reg_map
[i
] = shader
->u
.ps
.declared_in_count
++;
2334 else shader
->u
.ps
.input_reg_map
[i
] = ~0U;
2338 shader
->load_local_constsF
= shader
->lconst_inf_or_nan
;
2343 void pixelshader_update_samplers(struct wined3d_shader
*shader
, WORD tex_types
)
2345 struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
2346 enum wined3d_sampler_texture_type
*sampler_type
= reg_maps
->sampler_type
;
2349 if (reg_maps
->shader_version
.major
!= 1) return;
2351 for (i
= 0; i
< shader
->limits
.sampler
; ++i
)
2353 /* We don't sample from this sampler. */
2354 if (!sampler_type
[i
]) continue;
2356 switch ((tex_types
>> i
* WINED3D_PSARGS_TEXTYPE_SHIFT
) & WINED3D_PSARGS_TEXTYPE_MASK
)
2358 case WINED3D_SHADER_TEX_2D
:
2359 sampler_type
[i
] = WINED3DSTT_2D
;
2362 case WINED3D_SHADER_TEX_3D
:
2363 sampler_type
[i
] = WINED3DSTT_VOLUME
;
2366 case WINED3D_SHADER_TEX_CUBE
:
2367 sampler_type
[i
] = WINED3DSTT_CUBE
;
2373 HRESULT CDECL
wined3d_shader_create_gs(struct wined3d_device
*device
, const DWORD
*byte_code
,
2374 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2375 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2377 struct wined3d_shader
*object
;
2380 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2381 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2383 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2385 return E_OUTOFMEMORY
;
2387 hr
= geometryshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2390 WARN("Failed to initialize geometry shader, hr %#x.\n", hr
);
2391 HeapFree(GetProcessHeap(), 0, object
);
2395 TRACE("Created geometry shader %p.\n", object
);
2401 HRESULT CDECL
wined3d_shader_create_ps(struct wined3d_device
*device
, const DWORD
*byte_code
,
2402 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2403 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2405 struct wined3d_shader
*object
;
2408 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2409 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2411 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2413 return E_OUTOFMEMORY
;
2415 hr
= pixelshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2418 WARN("Failed to initialize pixel shader, hr %#x.\n", hr
);
2419 HeapFree(GetProcessHeap(), 0, object
);
2423 TRACE("Created pixel shader %p.\n", object
);
2429 HRESULT CDECL
wined3d_shader_create_vs(struct wined3d_device
*device
, const DWORD
*byte_code
,
2430 const struct wined3d_shader_signature
*output_signature
, void *parent
,
2431 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader
**shader
, unsigned int max_version
)
2433 struct wined3d_shader
*object
;
2436 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2437 device
, byte_code
, output_signature
, parent
, parent_ops
, shader
);
2439 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
2441 return E_OUTOFMEMORY
;
2443 hr
= vertexshader_init(object
, device
, byte_code
, output_signature
, parent
, parent_ops
, max_version
);
2446 WARN("Failed to initialize vertex shader, hr %#x.\n", hr
);
2447 HeapFree(GetProcessHeap(), 0, object
);
2451 TRACE("Created vertex shader %p.\n", object
);