2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2005 Oliver Stieber
6 * Copyright 2006 Ivan Gyurdiev
7 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2009-2010 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
31 #include "wined3d_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
34 WINE_DECLARE_DEBUG_CHANNEL(d3d
);
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_DEF */ "def",
53 /* WINED3DSIH_DEFB */ "defb",
54 /* WINED3DSIH_DEFI */ "defi",
55 /* WINED3DSIH_DIV */ "div",
56 /* WINED3DSIH_DP2ADD */ "dp2add",
57 /* WINED3DSIH_DP3 */ "dp3",
58 /* WINED3DSIH_DP4 */ "dp4",
59 /* WINED3DSIH_DST */ "dst",
60 /* WINED3DSIH_DSX */ "dsx",
61 /* WINED3DSIH_DSY */ "dsy",
62 /* WINED3DSIH_ELSE */ "else",
63 /* WINED3DSIH_EMIT */ "emit",
64 /* WINED3DSIH_ENDIF */ "endif",
65 /* WINED3DSIH_ENDLOOP */ "endloop",
66 /* WINED3DSIH_ENDREP */ "endrep",
67 /* WINED3DSIH_EXP */ "exp",
68 /* WINED3DSIH_EXPP */ "expp",
69 /* WINED3DSIH_FRC */ "frc",
70 /* WINED3DSIH_FTOI */ "ftoi",
71 /* WINED3DSIH_IADD */ "iadd",
72 /* WINED3DSIH_IEQ */ "ieq",
73 /* WINED3DSIH_IF */ "if",
74 /* WINED3DSIH_IFC */ "ifc",
75 /* WINED3DSIH_IGE */ "ige",
76 /* WINED3DSIH_IMUL */ "imul",
77 /* WINED3DSIH_ITOF */ "itof",
78 /* WINED3DSIH_LABEL */ "label",
79 /* WINED3DSIH_LD */ "ld",
80 /* WINED3DSIH_LIT */ "lit",
81 /* WINED3DSIH_LOG */ "log",
82 /* WINED3DSIH_LOGP */ "logp",
83 /* WINED3DSIH_LOOP */ "loop",
84 /* WINED3DSIH_LRP */ "lrp",
85 /* WINED3DSIH_LT */ "lt",
86 /* WINED3DSIH_M3x2 */ "m3x2",
87 /* WINED3DSIH_M3x3 */ "m3x3",
88 /* WINED3DSIH_M3x4 */ "m3x4",
89 /* WINED3DSIH_M4x3 */ "m4x3",
90 /* WINED3DSIH_M4x4 */ "m4x4",
91 /* WINED3DSIH_MAD */ "mad",
92 /* WINED3DSIH_MAX */ "max",
93 /* WINED3DSIH_MIN */ "min",
94 /* WINED3DSIH_MOV */ "mov",
95 /* WINED3DSIH_MOVA */ "mova",
96 /* WINED3DSIH_MOVC */ "movc",
97 /* WINED3DSIH_MUL */ "mul",
98 /* WINED3DSIH_NOP */ "nop",
99 /* WINED3DSIH_NRM */ "nrm",
100 /* WINED3DSIH_PHASE */ "phase",
101 /* WINED3DSIH_POW */ "pow",
102 /* WINED3DSIH_RCP */ "rcp",
103 /* WINED3DSIH_REP */ "rep",
104 /* WINED3DSIH_RET */ "ret",
105 /* WINED3DSIH_RSQ */ "rsq",
106 /* WINED3DSIH_SAMPLE */ "sample",
107 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
108 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
109 /* WINED3DSIH_SETP */ "setp",
110 /* WINED3DSIH_SGE */ "sge",
111 /* WINED3DSIH_SGN */ "sgn",
112 /* WINED3DSIH_SINCOS */ "sincos",
113 /* WINED3DSIH_SLT */ "slt",
114 /* WINED3DSIH_SQRT */ "sqrt",
115 /* WINED3DSIH_SUB */ "sub",
116 /* WINED3DSIH_TEX */ "texld",
117 /* WINED3DSIH_TEXBEM */ "texbem",
118 /* WINED3DSIH_TEXBEML */ "texbeml",
119 /* WINED3DSIH_TEXCOORD */ "texcrd",
120 /* WINED3DSIH_TEXDEPTH */ "texdepth",
121 /* WINED3DSIH_TEXDP3 */ "texdp3",
122 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
123 /* WINED3DSIH_TEXKILL */ "texkill",
124 /* WINED3DSIH_TEXLDD */ "texldd",
125 /* WINED3DSIH_TEXLDL */ "texldl",
126 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
127 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
128 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
129 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
130 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
131 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
132 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
133 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
134 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
135 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
136 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
137 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
138 /* WINED3DSIH_UTOF */ "utof",
141 static const char * const semantic_names
[] =
143 /* WINED3DDECLUSAGE_POSITION */ "SV_POSITION",
144 /* WINED3DDECLUSAGE_BLENDWEIGHT */ "BLENDWEIGHT",
145 /* WINED3DDECLUSAGE_BLENDINDICES */ "BLENDINDICES",
146 /* WINED3DDECLUSAGE_NORMAL */ "NORMAL",
147 /* WINED3DDECLUSAGE_PSIZE */ "PSIZE",
148 /* WINED3DDECLUSAGE_TEXCOORD */ "TEXCOORD",
149 /* WINED3DDECLUSAGE_TANGENT */ "TANGENT",
150 /* WINED3DDECLUSAGE_BINORMAL */ "BINORMAL",
151 /* WINED3DDECLUSAGE_TESSFACTOR */ "TESSFACTOR",
152 /* WINED3DDECLUSAGE_POSITIONT */ "POSITIONT",
153 /* WINED3DDECLUSAGE_COLOR */ "COLOR",
154 /* WINED3DDECLUSAGE_FOG */ "FOG",
155 /* WINED3DDECLUSAGE_DEPTH */ "DEPTH",
156 /* WINED3DDECLUSAGE_SAMPLE */ "SAMPLE",
159 static const char *shader_semantic_name_from_usage(WINED3DDECLUSAGE usage
)
161 if (usage
>= sizeof(semantic_names
) / sizeof(*semantic_names
))
163 FIXME("Unrecognized usage %#x.\n", usage
);
164 return "UNRECOGNIZED";
167 return semantic_names
[usage
];
170 static WINED3DDECLUSAGE
shader_usage_from_semantic_name(const char *name
)
174 for (i
= 0; i
< sizeof(semantic_names
) / sizeof(*semantic_names
); ++i
)
176 if (!strcmp(name
, semantic_names
[i
])) return i
;
182 BOOL
shader_match_semantic(const char *semantic_name
, WINED3DDECLUSAGE usage
)
184 return !strcmp(semantic_name
, shader_semantic_name_from_usage(usage
));
187 static void shader_signature_from_semantic(struct wined3d_shader_signature_element
*e
,
188 const struct wined3d_shader_semantic
*s
)
190 e
->semantic_name
= shader_semantic_name_from_usage(s
->usage
);
191 e
->semantic_idx
= s
->usage_idx
;
192 e
->sysval_semantic
= 0;
193 e
->component_type
= 0;
194 e
->register_idx
= s
->reg
.reg
.idx
;
195 e
->mask
= s
->reg
.write_mask
;
198 static void shader_signature_from_usage(struct wined3d_shader_signature_element
*e
,
199 WINED3DDECLUSAGE usage
, UINT usage_idx
, UINT reg_idx
, DWORD write_mask
)
201 e
->semantic_name
= shader_semantic_name_from_usage(usage
);
202 e
->semantic_idx
= usage_idx
;
203 e
->sysval_semantic
= 0;
204 e
->component_type
= 0;
205 e
->register_idx
= reg_idx
;
206 e
->mask
= write_mask
;
209 static const struct wined3d_shader_frontend
*shader_select_frontend(DWORD version_token
)
211 switch (version_token
>> 16)
215 return &sm1_shader_frontend
;
220 return &sm4_shader_frontend
;
223 FIXME("Unrecognised version token %#x\n", version_token
);
228 void shader_buffer_clear(struct wined3d_shader_buffer
*buffer
)
230 buffer
->buffer
[0] = '\0';
233 buffer
->newline
= TRUE
;
236 BOOL
shader_buffer_init(struct wined3d_shader_buffer
*buffer
)
238 buffer
->buffer
= HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE
);
241 ERR("Failed to allocate shader buffer memory.\n");
245 shader_buffer_clear(buffer
);
249 void shader_buffer_free(struct wined3d_shader_buffer
*buffer
)
251 HeapFree(GetProcessHeap(), 0, buffer
->buffer
);
254 int shader_vaddline(struct wined3d_shader_buffer
*buffer
, const char *format
, va_list args
)
256 char *base
= buffer
->buffer
+ buffer
->bsize
;
259 rc
= vsnprintf(base
, SHADER_PGMSIZE
- 1 - buffer
->bsize
, format
, args
);
261 if (rc
< 0 /* C89 */ || (unsigned int)rc
> SHADER_PGMSIZE
- 1 - buffer
->bsize
/* C99 */)
263 ERR("The buffer allocated for the shader program string "
264 "is too small at %d bytes.\n", SHADER_PGMSIZE
);
265 buffer
->bsize
= SHADER_PGMSIZE
- 1;
271 TRACE("GL HW (%u, %u) : %s", buffer
->lineNo
+ 1, buffer
->bsize
, base
);
272 buffer
->newline
= FALSE
;
280 if (buffer
->buffer
[buffer
->bsize
-1] == '\n')
283 buffer
->newline
= TRUE
;
289 int shader_addline(struct wined3d_shader_buffer
*buffer
, const char *format
, ...)
294 va_start(args
, format
);
295 ret
= shader_vaddline(buffer
, format
, args
);
301 static void shader_init(struct IWineD3DBaseShaderClass
*shader
, IWineD3DDeviceImpl
*device
,
302 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
305 shader
->device
= (IWineD3DDevice
*)device
;
306 shader
->parent
= parent
;
307 shader
->parent_ops
= parent_ops
;
308 list_init(&shader
->linked_programs
);
309 list_add_head(&device
->shaders
, &shader
->shader_list_entry
);
312 /* Convert floating point offset relative to a register file to an absolute
313 * offset for float constants. */
314 static unsigned int shader_get_float_offset(WINED3DSHADER_PARAM_REGISTER_TYPE register_type
, UINT register_idx
)
316 switch (register_type
)
318 case WINED3DSPR_CONST
: return register_idx
;
319 case WINED3DSPR_CONST2
: return 2048 + register_idx
;
320 case WINED3DSPR_CONST3
: return 4096 + register_idx
;
321 case WINED3DSPR_CONST4
: return 6144 + register_idx
;
323 FIXME("Unsupported register type: %u.\n", register_type
);
328 static void shader_delete_constant_list(struct list
*clist
)
330 struct local_constant
*constant
;
333 ptr
= list_head(clist
);
336 constant
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
337 ptr
= list_next(clist
, ptr
);
338 HeapFree(GetProcessHeap(), 0, constant
);
343 static inline void set_bitmap_bit(DWORD
*bitmap
, DWORD bit
)
348 bitmap
[idx
] |= (1 << shift
);
351 static void shader_record_register_usage(IWineD3DBaseShaderImpl
*shader
, struct shader_reg_maps
*reg_maps
,
352 const struct wined3d_shader_register
*reg
, enum wined3d_shader_type shader_type
)
356 case WINED3DSPR_TEXTURE
: /* WINED3DSPR_ADDR */
357 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
) reg_maps
->texcoord
|= 1 << reg
->idx
;
358 else reg_maps
->address
|= 1 << reg
->idx
;
361 case WINED3DSPR_TEMP
:
362 reg_maps
->temporary
|= 1 << reg
->idx
;
365 case WINED3DSPR_INPUT
:
366 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
370 /* If relative addressing is used, we must assume that all registers
371 * are used. Even if it is a construct like v3[aL], we can't assume
372 * that v0, v1 and v2 aren't read because aL can be negative */
374 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
376 ((IWineD3DPixelShaderImpl
*)shader
)->input_reg_used
[i
] = TRUE
;
381 ((IWineD3DPixelShaderImpl
*)shader
)->input_reg_used
[reg
->idx
] = TRUE
;
384 else reg_maps
->input_registers
|= 1 << reg
->idx
;
387 case WINED3DSPR_RASTOUT
:
388 if (reg
->idx
== 1) reg_maps
->fog
= 1;
391 case WINED3DSPR_MISCTYPE
:
392 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
394 if (!reg
->idx
) reg_maps
->vpos
= 1;
395 else if (reg
->idx
== 1) reg_maps
->usesfacing
= 1;
399 case WINED3DSPR_CONST
:
402 if (shader_type
!= WINED3D_SHADER_TYPE_PIXEL
)
404 if (reg
->idx
< ((IWineD3DVertexShaderImpl
*)shader
)->min_rel_offset
)
406 ((IWineD3DVertexShaderImpl
*)shader
)->min_rel_offset
= reg
->idx
;
408 if (reg
->idx
> ((IWineD3DVertexShaderImpl
*)shader
)->max_rel_offset
)
410 ((IWineD3DVertexShaderImpl
*)shader
)->max_rel_offset
= reg
->idx
;
413 reg_maps
->usesrelconstF
= TRUE
;
417 set_bitmap_bit(reg_maps
->constf
, reg
->idx
);
421 case WINED3DSPR_CONSTINT
:
422 reg_maps
->integer_constants
|= (1 << reg
->idx
);
425 case WINED3DSPR_CONSTBOOL
:
426 reg_maps
->boolean_constants
|= (1 << reg
->idx
);
429 case WINED3DSPR_COLOROUT
:
430 reg_maps
->highest_render_target
= max(reg_maps
->highest_render_target
, reg
->idx
);
434 TRACE("Not recording register of type %#x and idx %u\n", reg
->type
, reg
->idx
);
439 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr
, unsigned int param
)
443 case WINED3DSIH_M4x4
:
444 case WINED3DSIH_M3x4
:
445 return param
== 1 ? 3 : 0;
447 case WINED3DSIH_M4x3
:
448 case WINED3DSIH_M3x3
:
449 return param
== 1 ? 2 : 0;
451 case WINED3DSIH_M3x2
:
452 return param
== 1 ? 1 : 0;
459 /* Note that this does not count the loop register as an address register. */
460 static HRESULT
shader_get_registers_used(IWineD3DBaseShader
*iface
, const struct wined3d_shader_frontend
*fe
,
461 struct shader_reg_maps
*reg_maps
, struct wined3d_shader_signature_element
*input_signature
,
462 struct wined3d_shader_signature_element
*output_signature
, const DWORD
*byte_code
, DWORD constf_size
)
464 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)iface
;
465 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
466 void *fe_data
= shader
->baseShader
.frontend_data
;
467 struct wined3d_shader_version shader_version
;
468 const DWORD
*ptr
= byte_code
;
470 memset(reg_maps
, 0, sizeof(*reg_maps
));
472 /* get_registers_used() is called on every compile on some 1.x shaders,
473 * which can result in stacking up a collection of local constants.
474 * Delete the old constants if existing. */
475 shader_delete_constant_list(&shader
->baseShader
.constantsF
);
476 shader_delete_constant_list(&shader
->baseShader
.constantsB
);
477 shader_delete_constant_list(&shader
->baseShader
.constantsI
);
479 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
480 reg_maps
->shader_version
= shader_version
;
482 reg_maps
->constf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
483 sizeof(*reg_maps
->constf
) * ((constf_size
+ 31) / 32));
484 if (!reg_maps
->constf
)
486 ERR("Failed to allocate constant map memory.\n");
487 return E_OUTOFMEMORY
;
490 while (!fe
->shader_is_end(fe_data
, &ptr
))
492 struct wined3d_shader_instruction ins
;
498 fe
->shader_read_comment(&ptr
, &comment
, &comment_size
);
499 if (comment
) continue;
502 fe
->shader_read_opcode(fe_data
, &ptr
, &ins
, ¶m_size
);
504 /* Unhandled opcode, and its parameters. */
505 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
507 TRACE("Skipping unrecognized instruction.\n");
512 /* Handle declarations. */
513 if (ins
.handler_idx
== WINED3DSIH_DCL
)
515 struct wined3d_shader_semantic semantic
;
517 fe
->shader_read_semantic(&ptr
, &semantic
);
519 switch (semantic
.reg
.reg
.type
)
521 /* Mark input registers used. */
522 case WINED3DSPR_INPUT
:
523 reg_maps
->input_registers
|= 1 << semantic
.reg
.reg
.idx
;
524 shader_signature_from_semantic(&input_signature
[semantic
.reg
.reg
.idx
], &semantic
);
527 /* Vertex shader: mark 3.0 output registers used, save token. */
528 case WINED3DSPR_OUTPUT
:
529 reg_maps
->output_registers
|= 1 << semantic
.reg
.reg
.idx
;
530 shader_signature_from_semantic(&output_signature
[semantic
.reg
.reg
.idx
], &semantic
);
531 if (semantic
.usage
== WINED3DDECLUSAGE_FOG
) reg_maps
->fog
= 1;
534 /* Save sampler usage token. */
535 case WINED3DSPR_SAMPLER
:
536 reg_maps
->sampler_type
[semantic
.reg
.reg
.idx
] = semantic
.sampler_type
;
540 TRACE("Not recording DCL register type %#x.\n", semantic
.reg
.reg
.type
);
544 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
546 struct wined3d_shader_src_param rel_addr
;
547 struct wined3d_shader_dst_param dst
;
549 local_constant
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
550 if (!lconst
) return E_OUTOFMEMORY
;
552 fe
->shader_read_dst_param(fe_data
, &ptr
, &dst
, &rel_addr
);
553 lconst
->idx
= dst
.reg
.idx
;
555 memcpy(lconst
->value
, ptr
, 4 * sizeof(DWORD
));
558 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
559 if (shader_version
.major
== 1 && shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
561 float *value
= (float *)lconst
->value
;
562 if (value
[0] < -1.0f
) value
[0] = -1.0f
;
563 else if (value
[0] > 1.0f
) value
[0] = 1.0f
;
564 if (value
[1] < -1.0f
) value
[1] = -1.0f
;
565 else if (value
[1] > 1.0f
) value
[1] = 1.0f
;
566 if (value
[2] < -1.0f
) value
[2] = -1.0f
;
567 else if (value
[2] > 1.0f
) value
[2] = 1.0f
;
568 if (value
[3] < -1.0f
) value
[3] = -1.0f
;
569 else if (value
[3] > 1.0f
) value
[3] = 1.0f
;
572 list_add_head(&shader
->baseShader
.constantsF
, &lconst
->entry
);
574 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
576 struct wined3d_shader_src_param rel_addr
;
577 struct wined3d_shader_dst_param dst
;
579 local_constant
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
580 if (!lconst
) return E_OUTOFMEMORY
;
582 fe
->shader_read_dst_param(fe_data
, &ptr
, &dst
, &rel_addr
);
583 lconst
->idx
= dst
.reg
.idx
;
585 memcpy(lconst
->value
, ptr
, 4 * sizeof(DWORD
));
588 list_add_head(&shader
->baseShader
.constantsI
, &lconst
->entry
);
589 reg_maps
->local_int_consts
|= (1 << dst
.reg
.idx
);
591 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
593 struct wined3d_shader_src_param rel_addr
;
594 struct wined3d_shader_dst_param dst
;
596 local_constant
*lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
597 if (!lconst
) return E_OUTOFMEMORY
;
599 fe
->shader_read_dst_param(fe_data
, &ptr
, &dst
, &rel_addr
);
600 lconst
->idx
= dst
.reg
.idx
;
602 memcpy(lconst
->value
, ptr
, sizeof(DWORD
));
605 list_add_head(&shader
->baseShader
.constantsB
, &lconst
->entry
);
606 reg_maps
->local_bool_consts
|= (1 << dst
.reg
.idx
);
608 /* If there's a loop in the shader. */
609 else if (ins
.handler_idx
== WINED3DSIH_LOOP
610 || ins
.handler_idx
== WINED3DSIH_REP
)
612 struct wined3d_shader_src_param src
, rel_addr
;
614 fe
->shader_read_src_param(fe_data
, &ptr
, &src
, &rel_addr
);
616 /* Rep and Loop always use an integer constant for the control parameters. */
617 if (ins
.handler_idx
== WINED3DSIH_REP
)
619 reg_maps
->integer_constants
|= 1 << src
.reg
.idx
;
623 fe
->shader_read_src_param(fe_data
, &ptr
, &src
, &rel_addr
);
624 reg_maps
->integer_constants
|= 1 << src
.reg
.idx
;
628 if (cur_loop_depth
> max_loop_depth
) max_loop_depth
= cur_loop_depth
;
630 else if (ins
.handler_idx
== WINED3DSIH_ENDLOOP
631 || ins
.handler_idx
== WINED3DSIH_ENDREP
)
635 /* For subroutine prototypes. */
636 else if (ins
.handler_idx
== WINED3DSIH_LABEL
)
638 struct wined3d_shader_src_param src
, rel_addr
;
640 fe
->shader_read_src_param(fe_data
, &ptr
, &src
, &rel_addr
);
641 reg_maps
->labels
|= 1 << src
.reg
.idx
;
643 /* Set texture, address, temporary registers. */
646 BOOL color0_mov
= FALSE
;
649 /* This will loop over all the registers and try to
650 * make a bitmask of the ones we're interested in.
652 * Relative addressing tokens are ignored, but that's
653 * okay, since we'll catch any address registers when
654 * they are initialized (required by spec). */
655 for (i
= 0; i
< ins
.dst_count
; ++i
)
657 struct wined3d_shader_src_param dst_rel_addr
;
658 struct wined3d_shader_dst_param dst_param
;
660 fe
->shader_read_dst_param(fe_data
, &ptr
, &dst_param
, &dst_rel_addr
);
662 shader_record_register_usage(shader
, reg_maps
, &dst_param
.reg
, shader_version
.type
);
664 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
665 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
666 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
667 if (shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
&& shader_version
.major
< 3)
669 UINT idx
= dst_param
.reg
.idx
;
671 switch (dst_param
.reg
.type
)
673 case WINED3DSPR_RASTOUT
:
677 reg_maps
->output_registers
|= 1 << 10;
678 shader_signature_from_usage(&output_signature
[10],
679 WINED3DDECLUSAGE_POSITION
, 0, 10, WINED3DSP_WRITEMASK_ALL
);
683 reg_maps
->output_registers
|= 1 << 11;
684 shader_signature_from_usage(&output_signature
[11],
685 WINED3DDECLUSAGE_FOG
, 0, 11, WINED3DSP_WRITEMASK_0
);
689 reg_maps
->output_registers
|= 1 << 11;
690 shader_signature_from_usage(&output_signature
[11],
691 WINED3DDECLUSAGE_PSIZE
, 0, 11, WINED3DSP_WRITEMASK_1
);
696 case WINED3DSPR_ATTROUT
:
700 if (reg_maps
->output_registers
& (1 << idx
))
702 output_signature
[idx
].mask
|= dst_param
.write_mask
;
706 reg_maps
->output_registers
|= 1 << idx
;
707 shader_signature_from_usage(&output_signature
[idx
],
708 WINED3DDECLUSAGE_COLOR
, idx
- 8, idx
, dst_param
.write_mask
);
713 case WINED3DSPR_TEXCRDOUT
:
715 reg_maps
->texcoord_mask
[idx
] |= dst_param
.write_mask
;
716 if (reg_maps
->output_registers
& (1 << idx
))
718 output_signature
[idx
].mask
|= dst_param
.write_mask
;
722 reg_maps
->output_registers
|= 1 << idx
;
723 shader_signature_from_usage(&output_signature
[idx
],
724 WINED3DDECLUSAGE_TEXCOORD
, idx
, idx
, dst_param
.write_mask
);
733 if (shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
735 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*)shader
;
737 if (dst_param
.reg
.type
== WINED3DSPR_COLOROUT
&& !dst_param
.reg
.idx
)
739 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
740 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
741 * the mov and perform the sRGB write correction from the source register.
743 * However, if the mov is only partial, we can't do this, and if the write
744 * comes from an instruction other than MOV it is hard to do as well. If
745 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
747 ps
->color0_mov
= FALSE
;
748 if (ins
.handler_idx
== WINED3DSIH_MOV
749 && dst_param
.write_mask
== WINED3DSP_WRITEMASK_ALL
)
751 /* Used later when the source register is read. */
755 /* Also drop the MOV marker if the source register is overwritten prior to the shader
758 else if (dst_param
.reg
.type
== WINED3DSPR_TEMP
&& dst_param
.reg
.idx
== ps
->color0_reg
)
760 ps
->color0_mov
= FALSE
;
764 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
765 if (shader_version
.major
== 1
766 && (ins
.handler_idx
== WINED3DSIH_TEX
767 || ins
.handler_idx
== WINED3DSIH_TEXBEM
768 || ins
.handler_idx
== WINED3DSIH_TEXBEML
769 || ins
.handler_idx
== WINED3DSIH_TEXDP3TEX
770 || ins
.handler_idx
== WINED3DSIH_TEXM3x2TEX
771 || ins
.handler_idx
== WINED3DSIH_TEXM3x3SPEC
772 || ins
.handler_idx
== WINED3DSIH_TEXM3x3TEX
773 || ins
.handler_idx
== WINED3DSIH_TEXM3x3VSPEC
774 || ins
.handler_idx
== WINED3DSIH_TEXREG2AR
775 || ins
.handler_idx
== WINED3DSIH_TEXREG2GB
776 || ins
.handler_idx
== WINED3DSIH_TEXREG2RGB
))
778 /* Fake sampler usage, only set reserved bit and type. */
779 DWORD sampler_code
= dst_param
.reg
.idx
;
781 TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
782 reg_maps
->sampler_type
[sampler_code
] = WINED3DSTT_2D
;
784 /* texbem is only valid with < 1.4 pixel shaders */
785 if (ins
.handler_idx
== WINED3DSIH_TEXBEM
786 || ins
.handler_idx
== WINED3DSIH_TEXBEML
)
788 reg_maps
->bumpmat
|= 1 << dst_param
.reg
.idx
;
789 if (ins
.handler_idx
== WINED3DSIH_TEXBEML
)
791 reg_maps
->luminanceparams
|= 1 << dst_param
.reg
.idx
;
795 else if (ins
.handler_idx
== WINED3DSIH_BEM
)
797 reg_maps
->bumpmat
|= 1 << dst_param
.reg
.idx
;
801 if (ins
.handler_idx
== WINED3DSIH_NRM
) reg_maps
->usesnrm
= 1;
802 else if (ins
.handler_idx
== WINED3DSIH_DSY
) reg_maps
->usesdsy
= 1;
803 else if (ins
.handler_idx
== WINED3DSIH_DSX
) reg_maps
->usesdsx
= 1;
804 else if (ins
.handler_idx
== WINED3DSIH_TEXLDD
) reg_maps
->usestexldd
= 1;
805 else if (ins
.handler_idx
== WINED3DSIH_TEXLDL
) reg_maps
->usestexldl
= 1;
806 else if (ins
.handler_idx
== WINED3DSIH_MOVA
) reg_maps
->usesmova
= 1;
807 else if (ins
.handler_idx
== WINED3DSIH_IFC
) reg_maps
->usesifc
= 1;
808 else if (ins
.handler_idx
== WINED3DSIH_CALL
) reg_maps
->usescall
= 1;
809 else if (ins
.handler_idx
== WINED3DSIH_RCP
) reg_maps
->usesrcp
= 1;
811 limit
= ins
.src_count
+ (ins
.predicate
? 1 : 0);
812 for (i
= 0; i
< limit
; ++i
)
814 struct wined3d_shader_src_param src_param
, src_rel_addr
;
817 fe
->shader_read_src_param(fe_data
, &ptr
, &src_param
, &src_rel_addr
);
818 count
= get_instr_extra_regcount(ins
.handler_idx
, i
);
820 shader_record_register_usage(shader
, reg_maps
, &src_param
.reg
, shader_version
.type
);
824 shader_record_register_usage(shader
, reg_maps
, &src_param
.reg
, shader_version
.type
);
830 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*)shader
;
831 if (src_param
.reg
.type
== WINED3DSPR_TEMP
832 && src_param
.swizzle
== WINED3DSP_NOSWIZZLE
)
834 ps
->color0_mov
= TRUE
;
835 ps
->color0_reg
= src_param
.reg
.idx
;
841 reg_maps
->loop_depth
= max_loop_depth
;
843 shader
->baseShader
.functionLength
= ((const char *)ptr
- (const char *)byte_code
);
848 unsigned int shader_find_free_input_register(const struct shader_reg_maps
*reg_maps
, unsigned int max
)
850 DWORD map
= 1 << max
;
852 map
&= reg_maps
->shader_version
.major
< 3 ? ~reg_maps
->texcoord
: ~reg_maps
->input_registers
;
854 return wined3d_log2i(map
);
857 static void shader_dump_decl_usage(const struct wined3d_shader_semantic
*semantic
,
858 const struct wined3d_shader_version
*shader_version
)
862 if (semantic
->reg
.reg
.type
== WINED3DSPR_SAMPLER
)
864 switch (semantic
->sampler_type
)
866 case WINED3DSTT_2D
: TRACE("_2d"); break;
867 case WINED3DSTT_CUBE
: TRACE("_cube"); break;
868 case WINED3DSTT_VOLUME
: TRACE("_volume"); break;
869 default: TRACE("_unknown_ttype(0x%08x)", semantic
->sampler_type
);
874 /* Pixel shaders 3.0 don't have usage semantics. */
875 if (shader_version
->major
< 3 && shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
) return;
878 switch (semantic
->usage
)
880 case WINED3DDECLUSAGE_POSITION
:
881 TRACE("position%u", semantic
->usage_idx
);
884 case WINED3DDECLUSAGE_BLENDINDICES
:
888 case WINED3DDECLUSAGE_BLENDWEIGHT
:
892 case WINED3DDECLUSAGE_NORMAL
:
893 TRACE("normal%u", semantic
->usage_idx
);
896 case WINED3DDECLUSAGE_PSIZE
:
900 case WINED3DDECLUSAGE_COLOR
:
901 if (!semantic
->usage_idx
) TRACE("color");
902 else TRACE("specular%u", (semantic
->usage_idx
- 1));
905 case WINED3DDECLUSAGE_TEXCOORD
:
906 TRACE("texture%u", semantic
->usage_idx
);
909 case WINED3DDECLUSAGE_TANGENT
:
913 case WINED3DDECLUSAGE_BINORMAL
:
917 case WINED3DDECLUSAGE_TESSFACTOR
:
921 case WINED3DDECLUSAGE_POSITIONT
:
922 TRACE("positionT%u", semantic
->usage_idx
);
925 case WINED3DDECLUSAGE_FOG
:
929 case WINED3DDECLUSAGE_DEPTH
:
933 case WINED3DDECLUSAGE_SAMPLE
:
938 FIXME("unknown_semantics(0x%08x)", semantic
->usage
);
943 static void shader_dump_register(const struct wined3d_shader_register
*reg
,
944 const struct wined3d_shader_version
*shader_version
)
946 static const char * const rastout_reg_names
[] = {"oPos", "oFog", "oPts"};
947 static const char * const misctype_reg_names
[] = {"vPos", "vFace"};
948 UINT offset
= reg
->idx
;
952 case WINED3DSPR_TEMP
:
956 case WINED3DSPR_INPUT
:
960 case WINED3DSPR_CONST
:
961 case WINED3DSPR_CONST2
:
962 case WINED3DSPR_CONST3
:
963 case WINED3DSPR_CONST4
:
965 offset
= shader_get_float_offset(reg
->type
, reg
->idx
);
968 case WINED3DSPR_TEXTURE
: /* vs: case WINED3DSPR_ADDR */
969 TRACE("%c", shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
? 't' : 'a');
972 case WINED3DSPR_RASTOUT
:
973 TRACE("%s", rastout_reg_names
[reg
->idx
]);
976 case WINED3DSPR_COLOROUT
:
980 case WINED3DSPR_DEPTHOUT
:
984 case WINED3DSPR_ATTROUT
:
988 case WINED3DSPR_TEXCRDOUT
:
989 /* Vertex shaders >= 3.0 use general purpose output registers
990 * (WINED3DSPR_OUTPUT), which can include an address token. */
991 if (shader_version
->major
>= 3) TRACE("o");
995 case WINED3DSPR_CONSTINT
:
999 case WINED3DSPR_CONSTBOOL
:
1003 case WINED3DSPR_LABEL
:
1007 case WINED3DSPR_LOOP
:
1011 case WINED3DSPR_SAMPLER
:
1015 case WINED3DSPR_MISCTYPE
:
1016 if (reg
->idx
> 1) FIXME("Unhandled misctype register %u.\n", reg
->idx
);
1017 else TRACE("%s", misctype_reg_names
[reg
->idx
]);
1020 case WINED3DSPR_PREDICATE
:
1024 case WINED3DSPR_IMMCONST
:
1028 case WINED3DSPR_CONSTBUFFER
:
1032 case WINED3DSPR_NULL
:
1036 case WINED3DSPR_RESOURCE
:
1041 TRACE("unhandled_rtype(%#x)", reg
->type
);
1045 if (reg
->type
== WINED3DSPR_IMMCONST
)
1048 switch (reg
->immconst_type
)
1050 case WINED3D_IMMCONST_SCALAR
:
1051 TRACE("%.8e", *(const float *)reg
->immconst_data
);
1054 case WINED3D_IMMCONST_VEC4
:
1055 TRACE("%.8e, %.8e, %.8e, %.8e",
1056 *(const float *)®
->immconst_data
[0], *(const float *)®
->immconst_data
[1],
1057 *(const float *)®
->immconst_data
[2], *(const float *)®
->immconst_data
[3]);
1061 TRACE("<unhandled immconst_type %#x>", reg
->immconst_type
);
1066 else if (reg
->type
!= WINED3DSPR_RASTOUT
1067 && reg
->type
!= WINED3DSPR_MISCTYPE
1068 && reg
->type
!= WINED3DSPR_NULL
)
1070 if (reg
->array_idx
!= ~0U)
1072 TRACE("%u[%u", offset
, reg
->array_idx
);
1076 shader_dump_src_param(reg
->rel_addr
, shader_version
);
1085 shader_dump_src_param(reg
->rel_addr
, shader_version
);
1088 TRACE("%u", offset
);
1089 if (reg
->rel_addr
) TRACE("]");
1094 void shader_dump_dst_param(const struct wined3d_shader_dst_param
*param
,
1095 const struct wined3d_shader_version
*shader_version
)
1097 DWORD write_mask
= param
->write_mask
;
1099 shader_dump_register(¶m
->reg
, shader_version
);
1101 if (write_mask
&& write_mask
!= WINED3DSP_WRITEMASK_ALL
)
1103 static const char *write_mask_chars
= "xyzw";
1106 if (write_mask
& WINED3DSP_WRITEMASK_0
) TRACE("%c", write_mask_chars
[0]);
1107 if (write_mask
& WINED3DSP_WRITEMASK_1
) TRACE("%c", write_mask_chars
[1]);
1108 if (write_mask
& WINED3DSP_WRITEMASK_2
) TRACE("%c", write_mask_chars
[2]);
1109 if (write_mask
& WINED3DSP_WRITEMASK_3
) TRACE("%c", write_mask_chars
[3]);
1113 void shader_dump_src_param(const struct wined3d_shader_src_param
*param
,
1114 const struct wined3d_shader_version
*shader_version
)
1116 DWORD src_modifier
= param
->modifiers
;
1117 DWORD swizzle
= param
->swizzle
;
1119 if (src_modifier
== WINED3DSPSM_NEG
1120 || src_modifier
== WINED3DSPSM_BIASNEG
1121 || src_modifier
== WINED3DSPSM_SIGNNEG
1122 || src_modifier
== WINED3DSPSM_X2NEG
1123 || src_modifier
== WINED3DSPSM_ABSNEG
)
1125 else if (src_modifier
== WINED3DSPSM_COMP
)
1127 else if (src_modifier
== WINED3DSPSM_NOT
)
1130 if (src_modifier
== WINED3DSPSM_ABS
|| src_modifier
== WINED3DSPSM_ABSNEG
)
1133 shader_dump_register(¶m
->reg
, shader_version
);
1137 switch (src_modifier
)
1139 case WINED3DSPSM_NONE
: break;
1140 case WINED3DSPSM_NEG
: break;
1141 case WINED3DSPSM_NOT
: break;
1142 case WINED3DSPSM_BIAS
: TRACE("_bias"); break;
1143 case WINED3DSPSM_BIASNEG
: TRACE("_bias"); break;
1144 case WINED3DSPSM_SIGN
: TRACE("_bx2"); break;
1145 case WINED3DSPSM_SIGNNEG
: TRACE("_bx2"); break;
1146 case WINED3DSPSM_COMP
: break;
1147 case WINED3DSPSM_X2
: TRACE("_x2"); break;
1148 case WINED3DSPSM_X2NEG
: TRACE("_x2"); break;
1149 case WINED3DSPSM_DZ
: TRACE("_dz"); break;
1150 case WINED3DSPSM_DW
: TRACE("_dw"); break;
1151 case WINED3DSPSM_ABSNEG
: TRACE(")"); break;
1152 case WINED3DSPSM_ABS
: TRACE(")"); break;
1153 default: TRACE("_unknown_modifier(%#x)", src_modifier
);
1157 if (swizzle
!= WINED3DSP_NOSWIZZLE
)
1159 static const char *swizzle_chars
= "xyzw";
1160 DWORD swizzle_x
= swizzle
& 0x03;
1161 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
1162 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
1163 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
1165 if (swizzle_x
== swizzle_y
1166 && swizzle_x
== swizzle_z
1167 && swizzle_x
== swizzle_w
)
1169 TRACE(".%c", swizzle_chars
[swizzle_x
]);
1173 TRACE(".%c%c%c%c", swizzle_chars
[swizzle_x
], swizzle_chars
[swizzle_y
],
1174 swizzle_chars
[swizzle_z
], swizzle_chars
[swizzle_w
]);
1179 /* Shared code in order to generate the bulk of the shader string.
1180 * NOTE: A description of how to parse tokens can be found on MSDN. */
1181 void shader_generate_main(IWineD3DBaseShader
*iface
, struct wined3d_shader_buffer
*buffer
,
1182 const shader_reg_maps
*reg_maps
, const DWORD
*byte_code
, void *backend_ctx
)
1184 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)iface
;
1185 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)shader
->baseShader
.device
;
1186 const struct wined3d_shader_frontend
*fe
= shader
->baseShader
.frontend
;
1187 void *fe_data
= shader
->baseShader
.frontend_data
;
1188 struct wined3d_shader_src_param dst_rel_addr
[2];
1189 struct wined3d_shader_src_param src_rel_addr
[4];
1190 struct wined3d_shader_dst_param dst_param
[2];
1191 struct wined3d_shader_src_param src_param
[4];
1192 struct wined3d_shader_version shader_version
;
1193 struct wined3d_shader_loop_state loop_state
;
1194 struct wined3d_shader_instruction ins
;
1195 struct wined3d_shader_tex_mx tex_mx
;
1196 struct wined3d_shader_context ctx
;
1197 const DWORD
*ptr
= byte_code
;
1200 /* Initialize current parsing state. */
1201 tex_mx
.current_row
= 0;
1202 loop_state
.current_depth
= 0;
1203 loop_state
.current_reg
= 0;
1206 ctx
.gl_info
= &device
->adapter
->gl_info
;
1207 ctx
.reg_maps
= reg_maps
;
1208 ctx
.buffer
= buffer
;
1209 ctx
.tex_mx
= &tex_mx
;
1210 ctx
.loop_state
= &loop_state
;
1211 ctx
.backend_data
= backend_ctx
;
1214 ins
.dst
= dst_param
;
1215 ins
.src
= src_param
;
1217 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1219 while (!fe
->shader_is_end(fe_data
, &ptr
))
1221 const char *comment
;
1225 /* Skip comment tokens. */
1226 fe
->shader_read_comment(&ptr
, &comment
, &comment_size
);
1227 if (comment
) continue;
1230 fe
->shader_read_opcode(fe_data
, &ptr
, &ins
, ¶m_size
);
1232 /* Unknown opcode and its parameters. */
1233 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1235 TRACE("Skipping unrecognized instruction.\n");
1240 /* Nothing to do. */
1241 if (ins
.handler_idx
== WINED3DSIH_DCL
1242 || ins
.handler_idx
== WINED3DSIH_NOP
1243 || ins
.handler_idx
== WINED3DSIH_DEF
1244 || ins
.handler_idx
== WINED3DSIH_DEFI
1245 || ins
.handler_idx
== WINED3DSIH_DEFB
1246 || ins
.handler_idx
== WINED3DSIH_PHASE
)
1252 /* Destination tokens */
1253 for (i
= 0; i
< ins
.dst_count
; ++i
)
1255 fe
->shader_read_dst_param(fe_data
, &ptr
, &dst_param
[i
], &dst_rel_addr
[i
]);
1258 /* Predication token */
1261 FIXME("Predicates not implemented.\n");
1262 ins
.predicate
= *ptr
++;
1265 /* Other source tokens */
1266 for (i
= 0; i
< ins
.src_count
; ++i
)
1268 fe
->shader_read_src_param(fe_data
, &ptr
, &src_param
[i
], &src_rel_addr
[i
]);
1271 /* Call appropriate function for output target */
1272 device
->shader_backend
->shader_handle_instruction(&ins
);
1276 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param
*dst
)
1278 DWORD mmask
= dst
->modifiers
;
1283 case 13: TRACE("_d8"); break;
1284 case 14: TRACE("_d4"); break;
1285 case 15: TRACE("_d2"); break;
1286 case 1: TRACE("_x2"); break;
1287 case 2: TRACE("_x4"); break;
1288 case 3: TRACE("_x8"); break;
1289 default: TRACE("_unhandled_shift(%d)", dst
->shift
); break;
1292 if (mmask
& WINED3DSPDM_SATURATE
) TRACE("_sat");
1293 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) TRACE("_pp");
1294 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) TRACE("_centroid");
1296 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
1297 if (mmask
) FIXME("_unrecognized_modifier(%#x)", mmask
);
1300 static void shader_trace_init(const struct wined3d_shader_frontend
*fe
, void *fe_data
, const DWORD
*byte_code
)
1302 struct wined3d_shader_version shader_version
;
1303 const DWORD
*ptr
= byte_code
;
1304 const char *type_prefix
;
1307 TRACE("Parsing %p.\n", byte_code
);
1309 fe
->shader_read_header(fe_data
, &ptr
, &shader_version
);
1311 switch (shader_version
.type
)
1313 case WINED3D_SHADER_TYPE_VERTEX
:
1317 case WINED3D_SHADER_TYPE_GEOMETRY
:
1321 case WINED3D_SHADER_TYPE_PIXEL
:
1326 FIXME("Unhandled shader type %#x.\n", shader_version
.type
);
1327 type_prefix
= "unknown";
1331 TRACE("%s_%u_%u\n", type_prefix
, shader_version
.major
, shader_version
.minor
);
1333 while (!fe
->shader_is_end(fe_data
, &ptr
))
1335 struct wined3d_shader_instruction ins
;
1336 const char *comment
;
1341 fe
->shader_read_comment(&ptr
, &comment
, &comment_size
);
1344 if (comment_size
> 4 && *(const DWORD
*)comment
== WINEMAKEFOURCC('T', 'E', 'X', 'T'))
1346 const char *end
= comment
+ comment_size
;
1347 const char *ptr
= comment
+ 4;
1348 const char *line
= ptr
;
1355 UINT len
= ptr
- line
;
1356 if (len
&& *(ptr
- 1) == '\r') --len
;
1357 TRACE("// %s\n", debugstr_an(line
, len
));
1362 if (line
!= ptr
) TRACE("// %s\n", debugstr_an(line
, ptr
- line
));
1364 else TRACE("// %s\n", debugstr_an(comment
, comment_size
));
1368 fe
->shader_read_opcode(fe_data
, &ptr
, &ins
, ¶m_size
);
1369 if (ins
.handler_idx
== WINED3DSIH_TABLE_SIZE
)
1371 TRACE("Skipping unrecognized instruction.\n");
1376 if (ins
.handler_idx
== WINED3DSIH_DCL
)
1378 struct wined3d_shader_semantic semantic
;
1380 fe
->shader_read_semantic(&ptr
, &semantic
);
1382 shader_dump_decl_usage(&semantic
, &shader_version
);
1383 shader_dump_ins_modifiers(&semantic
.reg
);
1385 shader_dump_dst_param(&semantic
.reg
, &shader_version
);
1387 else if (ins
.handler_idx
== WINED3DSIH_DEF
)
1389 struct wined3d_shader_src_param rel_addr
;
1390 struct wined3d_shader_dst_param dst
;
1392 fe
->shader_read_dst_param(fe_data
, &ptr
, &dst
, &rel_addr
);
1394 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(dst
.reg
.type
, dst
.reg
.idx
),
1395 *(const float *)(ptr
),
1396 *(const float *)(ptr
+ 1),
1397 *(const float *)(ptr
+ 2),
1398 *(const float *)(ptr
+ 3));
1401 else if (ins
.handler_idx
== WINED3DSIH_DEFI
)
1403 struct wined3d_shader_src_param rel_addr
;
1404 struct wined3d_shader_dst_param dst
;
1406 fe
->shader_read_dst_param(fe_data
, &ptr
, &dst
, &rel_addr
);
1408 TRACE("defi i%u = %d, %d, %d, %d", dst
.reg
.idx
,
1415 else if (ins
.handler_idx
== WINED3DSIH_DEFB
)
1417 struct wined3d_shader_src_param rel_addr
;
1418 struct wined3d_shader_dst_param dst
;
1420 fe
->shader_read_dst_param(fe_data
, &ptr
, &dst
, &rel_addr
);
1422 TRACE("defb b%u = %s", dst
.reg
.idx
, *ptr
? "true" : "false");
1427 struct wined3d_shader_src_param dst_rel_addr
[2];
1428 struct wined3d_shader_src_param src_rel_addr
;
1429 struct wined3d_shader_dst_param dst_param
[2];
1430 struct wined3d_shader_src_param src_param
;
1432 for (i
= 0; i
< ins
.dst_count
; ++i
)
1434 fe
->shader_read_dst_param(fe_data
, &ptr
, &dst_param
[i
], &dst_rel_addr
[i
]);
1437 /* Print out predication source token first - it follows
1438 * the destination token. */
1441 fe
->shader_read_src_param(fe_data
, &ptr
, &src_param
, &src_rel_addr
);
1443 shader_dump_src_param(&src_param
, &shader_version
);
1447 /* PixWin marks instructions with the coissue flag with a '+' */
1448 if (ins
.coissue
) TRACE("+");
1450 TRACE("%s", shader_opcode_names
[ins
.handler_idx
]);
1452 if (ins
.handler_idx
== WINED3DSIH_IFC
1453 || ins
.handler_idx
== WINED3DSIH_BREAKC
)
1457 case COMPARISON_GT
: TRACE("_gt"); break;
1458 case COMPARISON_EQ
: TRACE("_eq"); break;
1459 case COMPARISON_GE
: TRACE("_ge"); break;
1460 case COMPARISON_LT
: TRACE("_lt"); break;
1461 case COMPARISON_NE
: TRACE("_ne"); break;
1462 case COMPARISON_LE
: TRACE("_le"); break;
1463 default: TRACE("_(%u)", ins
.flags
);
1466 else if (ins
.handler_idx
== WINED3DSIH_TEX
1467 && shader_version
.major
>= 2
1468 && (ins
.flags
& WINED3DSI_TEXLD_PROJECT
))
1473 /* We already read the destination tokens, print them. */
1474 for (i
= 0; i
< ins
.dst_count
; ++i
)
1476 shader_dump_ins_modifiers(&dst_param
[i
]);
1477 TRACE(!i
? " " : ", ");
1478 shader_dump_dst_param(&dst_param
[i
], &shader_version
);
1481 /* Other source tokens */
1482 for (i
= ins
.dst_count
; i
< (ins
.dst_count
+ ins
.src_count
); ++i
)
1484 fe
->shader_read_src_param(fe_data
, &ptr
, &src_param
, &src_rel_addr
);
1485 TRACE(!i
? " " : ", ");
1486 shader_dump_src_param(&src_param
, &shader_version
);
1493 static void shader_cleanup(IWineD3DBaseShader
*iface
)
1495 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)iface
;
1497 ((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->shader_backend
->shader_destroy(iface
);
1498 HeapFree(GetProcessHeap(), 0, shader
->baseShader
.reg_maps
.constf
);
1499 HeapFree(GetProcessHeap(), 0, shader
->baseShader
.function
);
1500 shader_delete_constant_list(&shader
->baseShader
.constantsF
);
1501 shader_delete_constant_list(&shader
->baseShader
.constantsB
);
1502 shader_delete_constant_list(&shader
->baseShader
.constantsI
);
1503 list_remove(&shader
->baseShader
.shader_list_entry
);
1505 if (shader
->baseShader
.frontend
&& shader
->baseShader
.frontend_data
)
1507 shader
->baseShader
.frontend
->shader_free(shader
->baseShader
.frontend_data
);
1511 static void shader_none_handle_instruction(const struct wined3d_shader_instruction
*ins
) {}
1512 static void shader_none_select(const struct wined3d_context
*context
, BOOL usePS
, BOOL useVS
) {}
1513 static void shader_none_select_depth_blt(IWineD3DDevice
*iface
, enum tex_types tex_type
, const SIZE
*ds_mask_size
) {}
1514 static void shader_none_deselect_depth_blt(IWineD3DDevice
*iface
) {}
1515 static void shader_none_update_float_vertex_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
) {}
1516 static void shader_none_update_float_pixel_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
) {}
1517 static void shader_none_load_constants(const struct wined3d_context
*context
, char usePS
, char useVS
) {}
1518 static void shader_none_load_np2fixup_constants(void *shader_priv
,
1519 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
) {}
1520 static void shader_none_destroy(IWineD3DBaseShader
*iface
) {}
1521 static HRESULT
shader_none_alloc(IWineD3DDevice
*iface
) {return WINED3D_OK
;}
1522 static void shader_none_free(IWineD3DDevice
*iface
) {}
1523 static BOOL
shader_none_dirty_const(IWineD3DDevice
*iface
) {return FALSE
;}
1525 static void shader_none_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
1527 /* Set the shader caps to 0 for the none shader backend */
1528 caps
->VertexShaderVersion
= 0;
1529 caps
->MaxVertexShaderConst
= 0;
1530 caps
->PixelShaderVersion
= 0;
1531 caps
->PixelShader1xMaxValue
= 0.0f
;
1532 caps
->MaxPixelShaderConst
= 0;
1533 caps
->VSClipping
= FALSE
;
1536 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
1538 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
1540 TRACE("Checking support for fixup:\n");
1541 dump_color_fixup_desc(fixup
);
1544 /* Faked to make some apps happy. */
1545 if (!is_complex_fixup(fixup
))
1551 TRACE("[FAILED]\n");
1555 const shader_backend_t none_shader_backend
= {
1556 shader_none_handle_instruction
,
1558 shader_none_select_depth_blt
,
1559 shader_none_deselect_depth_blt
,
1560 shader_none_update_float_vertex_constants
,
1561 shader_none_update_float_pixel_constants
,
1562 shader_none_load_constants
,
1563 shader_none_load_np2fixup_constants
,
1564 shader_none_destroy
,
1567 shader_none_dirty_const
,
1568 shader_none_get_caps
,
1569 shader_none_color_fixup_supported
,
1572 static HRESULT
shader_get_function(IWineD3DBaseShaderImpl
*shader
, void *data
, UINT
*data_size
)
1576 *data_size
= shader
->baseShader
.functionLength
;
1580 if (*data_size
< shader
->baseShader
.functionLength
)
1582 /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
1583 * than the required size we should write the required size and
1584 * return D3DERR_MOREDATA. That's not actually true. */
1585 return WINED3DERR_INVALIDCALL
;
1588 memcpy(data
, shader
->baseShader
.function
, shader
->baseShader
.functionLength
);
1593 static HRESULT
shader_set_function(IWineD3DBaseShaderImpl
*shader
, const DWORD
*byte_code
,
1594 const struct wined3d_shader_signature
*output_signature
, DWORD float_const_count
)
1596 struct shader_reg_maps
*reg_maps
= &shader
->baseShader
.reg_maps
;
1597 const struct wined3d_shader_frontend
*fe
;
1600 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1601 shader
, byte_code
, output_signature
, float_const_count
);
1603 fe
= shader_select_frontend(*byte_code
);
1606 FIXME("Unable to find frontend for shader.\n");
1607 return WINED3DERR_INVALIDCALL
;
1609 shader
->baseShader
.frontend
= fe
;
1610 shader
->baseShader
.frontend_data
= fe
->shader_init(byte_code
, output_signature
);
1611 if (!shader
->baseShader
.frontend_data
)
1613 FIXME("Failed to initialize frontend.\n");
1614 return WINED3DERR_INVALIDCALL
;
1617 /* First pass: trace shader. */
1618 if (TRACE_ON(d3d_shader
)) shader_trace_init(fe
, shader
->baseShader
.frontend_data
, byte_code
);
1620 /* Initialize immediate constant lists. */
1621 list_init(&shader
->baseShader
.constantsF
);
1622 list_init(&shader
->baseShader
.constantsB
);
1623 list_init(&shader
->baseShader
.constantsI
);
1625 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1626 hr
= shader_get_registers_used((IWineD3DBaseShader
*)shader
, fe
,
1627 reg_maps
, shader
->baseShader
.input_signature
, shader
->baseShader
.output_signature
,
1628 byte_code
, float_const_count
);
1629 if (FAILED(hr
)) return hr
;
1631 shader
->baseShader
.function
= HeapAlloc(GetProcessHeap(), 0, shader
->baseShader
.functionLength
);
1632 if (!shader
->baseShader
.function
) return E_OUTOFMEMORY
;
1633 memcpy(shader
->baseShader
.function
, byte_code
, shader
->baseShader
.functionLength
);
1638 static HRESULT STDMETHODCALLTYPE
vertexshader_QueryInterface(IWineD3DVertexShader
*iface
, REFIID riid
, void **object
)
1640 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), object
);
1642 if (IsEqualGUID(riid
, &IID_IWineD3DVertexShader
)
1643 || IsEqualGUID(riid
, &IID_IWineD3DBaseShader
)
1644 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
1645 || IsEqualGUID(riid
, &IID_IUnknown
))
1647 IUnknown_AddRef(iface
);
1652 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
1655 return E_NOINTERFACE
;
1658 static ULONG STDMETHODCALLTYPE
vertexshader_AddRef(IWineD3DVertexShader
*iface
)
1660 IWineD3DVertexShaderImpl
*shader
= (IWineD3DVertexShaderImpl
*)iface
;
1661 ULONG refcount
= InterlockedIncrement(&shader
->baseShader
.ref
);
1663 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
1668 /* Do not call while under the GL lock. */
1669 static ULONG STDMETHODCALLTYPE
vertexshader_Release(IWineD3DVertexShader
*iface
)
1671 IWineD3DVertexShaderImpl
*shader
= (IWineD3DVertexShaderImpl
*)iface
;
1672 ULONG refcount
= InterlockedDecrement(&shader
->baseShader
.ref
);
1674 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
1678 shader_cleanup((IWineD3DBaseShader
*)iface
);
1679 shader
->baseShader
.parent_ops
->wined3d_object_destroyed(shader
->baseShader
.parent
);
1680 HeapFree(GetProcessHeap(), 0, shader
);
1686 static void * STDMETHODCALLTYPE
vertexshader_GetParent(IWineD3DVertexShader
*iface
)
1688 TRACE("iface %p.\n", iface
);
1690 return ((IWineD3DBaseShaderImpl
*)iface
)->baseShader
.parent
;
1693 static HRESULT STDMETHODCALLTYPE
vertexshader_GetFunction(IWineD3DVertexShader
*iface
, void *data
, UINT
*data_size
)
1695 TRACE("iface %p, data %p, data_size %p.\n", iface
, data
, data_size
);
1697 return shader_get_function((IWineD3DBaseShaderImpl
*)iface
, data
, data_size
);
1700 /* Set local constants for d3d8 shaders. */
1701 static HRESULT STDMETHODCALLTYPE
vertexshader_SetLocalConstantsF(IWineD3DVertexShader
*iface
,
1702 UINT start_idx
, const float *src_data
, UINT count
)
1704 IWineD3DVertexShaderImpl
*shader
=(IWineD3DVertexShaderImpl
*)iface
;
1705 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)shader
->baseShader
.device
;
1708 TRACE("iface %p, start_idx %u, src_data %p, count %u.\n", iface
, start_idx
, src_data
, count
);
1710 end_idx
= start_idx
+ count
;
1711 if (end_idx
> device
->d3d_vshader_constantF
)
1713 WARN("end_idx %u > float constants limit %u.\n", end_idx
, device
->d3d_vshader_constantF
);
1714 end_idx
= device
->d3d_vshader_constantF
;
1717 for (i
= start_idx
; i
< end_idx
; ++i
)
1719 local_constant
* lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
1720 if (!lconst
) return E_OUTOFMEMORY
;
1723 memcpy(lconst
->value
, src_data
+ (i
- start_idx
) * 4 /* 4 components */, 4 * sizeof(float));
1724 list_add_head(&shader
->baseShader
.constantsF
, &lconst
->entry
);
1730 static const IWineD3DVertexShaderVtbl IWineD3DVertexShader_Vtbl
=
1732 /* IUnknown methods */
1733 vertexshader_QueryInterface
,
1734 vertexshader_AddRef
,
1735 vertexshader_Release
,
1736 /* IWineD3DBase methods */
1737 vertexshader_GetParent
,
1738 /* IWineD3DBaseShader methods */
1739 vertexshader_GetFunction
,
1740 /* IWineD3DVertexShader methods */
1741 vertexshader_SetLocalConstantsF
,
1744 void find_vs_compile_args(const struct wined3d_state
*state
,
1745 IWineD3DVertexShaderImpl
*shader
, struct vs_compile_args
*args
)
1747 args
->fog_src
= state
->render_states
[WINED3DRS_FOGTABLEMODE
]
1748 == WINED3DFOG_NONE
? VS_FOG_COORD
: VS_FOG_Z
;
1749 args
->clip_enabled
= state
->render_states
[WINED3DRS_CLIPPING
]
1750 && state
->render_states
[WINED3DRS_CLIPPLANEENABLE
];
1751 args
->swizzle_map
= ((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->strided_streams
.swizzle_map
;
1754 static BOOL
match_usage(BYTE usage1
, BYTE usage_idx1
, BYTE usage2
, BYTE usage_idx2
)
1756 if (usage_idx1
!= usage_idx2
) return FALSE
;
1757 if (usage1
== usage2
) return TRUE
;
1758 if (usage1
== WINED3DDECLUSAGE_POSITION
&& usage2
== WINED3DDECLUSAGE_POSITIONT
) return TRUE
;
1759 if (usage2
== WINED3DDECLUSAGE_POSITION
&& usage1
== WINED3DDECLUSAGE_POSITIONT
) return TRUE
;
1764 BOOL
vshader_get_input(struct IWineD3DVertexShaderImpl
*shader
,
1765 BYTE usage_req
, BYTE usage_idx_req
, unsigned int *regnum
)
1767 WORD map
= shader
->baseShader
.reg_maps
.input_registers
;
1770 for (i
= 0; map
; map
>>= 1, ++i
)
1772 if (!(map
& 1)) continue;
1774 if (match_usage(shader
->attributes
[i
].usage
,
1775 shader
->attributes
[i
].usage_idx
, usage_req
, usage_idx_req
))
1784 static void vertexshader_set_limits(IWineD3DVertexShaderImpl
*shader
)
1786 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->baseShader
.reg_maps
.shader_version
.major
,
1787 shader
->baseShader
.reg_maps
.shader_version
.minor
);
1788 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)shader
->baseShader
.device
;
1790 shader
->baseShader
.limits
.texcoord
= 0;
1791 shader
->baseShader
.limits
.attributes
= 16;
1792 shader
->baseShader
.limits
.packed_input
= 0;
1794 switch (shader_version
)
1796 case WINED3D_SHADER_VERSION(1, 0):
1797 case WINED3D_SHADER_VERSION(1, 1):
1798 shader
->baseShader
.limits
.temporary
= 12;
1799 shader
->baseShader
.limits
.constant_bool
= 0;
1800 shader
->baseShader
.limits
.constant_int
= 0;
1801 shader
->baseShader
.limits
.address
= 1;
1802 shader
->baseShader
.limits
.packed_output
= 12;
1803 shader
->baseShader
.limits
.sampler
= 0;
1804 shader
->baseShader
.limits
.label
= 0;
1805 /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1806 * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1808 shader
->baseShader
.limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1811 case WINED3D_SHADER_VERSION(2, 0):
1812 case WINED3D_SHADER_VERSION(2, 1):
1813 shader
->baseShader
.limits
.temporary
= 12;
1814 shader
->baseShader
.limits
.constant_bool
= 16;
1815 shader
->baseShader
.limits
.constant_int
= 16;
1816 shader
->baseShader
.limits
.address
= 1;
1817 shader
->baseShader
.limits
.packed_output
= 12;
1818 shader
->baseShader
.limits
.sampler
= 0;
1819 shader
->baseShader
.limits
.label
= 16;
1820 shader
->baseShader
.limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1823 case WINED3D_SHADER_VERSION(4, 0):
1824 FIXME("Using 3.0 limits for 4.0 shader.\n");
1827 case WINED3D_SHADER_VERSION(3, 0):
1828 shader
->baseShader
.limits
.temporary
= 32;
1829 shader
->baseShader
.limits
.constant_bool
= 32;
1830 shader
->baseShader
.limits
.constant_int
= 32;
1831 shader
->baseShader
.limits
.address
= 1;
1832 shader
->baseShader
.limits
.packed_output
= 12;
1833 shader
->baseShader
.limits
.sampler
= 4;
1834 shader
->baseShader
.limits
.label
= 16; /* FIXME: 2048 */
1835 /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1836 * even though they are capable of supporting much more (GL
1837 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1838 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1839 * shaders to 256. */
1840 shader
->baseShader
.limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1844 shader
->baseShader
.limits
.temporary
= 12;
1845 shader
->baseShader
.limits
.constant_bool
= 16;
1846 shader
->baseShader
.limits
.constant_int
= 16;
1847 shader
->baseShader
.limits
.address
= 1;
1848 shader
->baseShader
.limits
.packed_output
= 12;
1849 shader
->baseShader
.limits
.sampler
= 0;
1850 shader
->baseShader
.limits
.label
= 16;
1851 shader
->baseShader
.limits
.constant_float
= min(256, device
->d3d_vshader_constantF
);
1852 FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1853 shader
->baseShader
.reg_maps
.shader_version
.major
,
1854 shader
->baseShader
.reg_maps
.shader_version
.minor
);
1858 HRESULT
vertexshader_init(IWineD3DVertexShaderImpl
*shader
, IWineD3DDeviceImpl
*device
,
1859 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
1860 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1862 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1863 struct shader_reg_maps
*reg_maps
= &shader
->baseShader
.reg_maps
;
1868 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
1870 shader
->lpVtbl
= &IWineD3DVertexShader_Vtbl
;
1871 shader_init(&shader
->baseShader
, device
, parent
, parent_ops
);
1873 hr
= shader_set_function((IWineD3DBaseShaderImpl
*)shader
, byte_code
,
1874 output_signature
, device
->d3d_vshader_constantF
);
1877 WARN("Failed to set function, hr %#x.\n", hr
);
1878 shader_cleanup((IWineD3DBaseShader
*)shader
);
1882 map
= shader
->baseShader
.reg_maps
.input_registers
;
1883 for (i
= 0; map
; map
>>= 1, ++i
)
1885 if (!(map
& 1) || !shader
->baseShader
.input_signature
[i
].semantic_name
) continue;
1887 shader
->attributes
[i
].usage
=
1888 shader_usage_from_semantic_name(shader
->baseShader
.input_signature
[i
].semantic_name
);
1889 shader
->attributes
[i
].usage_idx
= shader
->baseShader
.input_signature
[i
].semantic_idx
;
1892 if (output_signature
)
1894 for (i
= 0; i
< output_signature
->element_count
; ++i
)
1896 struct wined3d_shader_signature_element
*e
= &output_signature
->elements
[i
];
1897 reg_maps
->output_registers
|= 1 << e
->register_idx
;
1898 shader
->baseShader
.output_signature
[e
->register_idx
] = *e
;
1902 vertexshader_set_limits(shader
);
1904 if (device
->vs_selected_mode
== SHADER_ARB
1905 && (gl_info
->quirks
& WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT
)
1906 && shader
->min_rel_offset
<= shader
->max_rel_offset
)
1908 if (shader
->max_rel_offset
- shader
->min_rel_offset
> 127)
1910 FIXME("The difference between the minimum and maximum relative offset is > 127.\n");
1911 FIXME("Which this OpenGL implementation does not support. Try using GLSL.\n");
1912 FIXME("Min: %d, Max: %d.\n", shader
->min_rel_offset
, shader
->max_rel_offset
);
1914 else if (shader
->max_rel_offset
- shader
->min_rel_offset
> 63)
1916 shader
->rel_offset
= shader
->min_rel_offset
+ 63;
1918 else if (shader
->max_rel_offset
> 63)
1920 shader
->rel_offset
= shader
->min_rel_offset
;
1924 shader
->rel_offset
= 0;
1928 shader
->baseShader
.load_local_constsF
= shader
->baseShader
.reg_maps
.usesrelconstF
1929 && !list_empty(&shader
->baseShader
.constantsF
);
1934 static HRESULT STDMETHODCALLTYPE
geometryshader_QueryInterface(IWineD3DGeometryShader
*iface
,
1935 REFIID riid
, void **object
)
1937 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), object
);
1939 if (IsEqualGUID(riid
, &IID_IWineD3DGeometryShader
)
1940 || IsEqualGUID(riid
, &IID_IWineD3DBaseShader
)
1941 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
1942 || IsEqualGUID(riid
, &IID_IUnknown
))
1944 IUnknown_AddRef(iface
);
1949 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
1952 return E_NOINTERFACE
;
1955 static ULONG STDMETHODCALLTYPE
geometryshader_AddRef(IWineD3DGeometryShader
*iface
)
1957 struct wined3d_geometryshader
*shader
= (struct wined3d_geometryshader
*)iface
;
1958 ULONG refcount
= InterlockedIncrement(&shader
->base_shader
.ref
);
1960 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
1965 /* Do not call while under the GL lock. */
1966 static ULONG STDMETHODCALLTYPE
geometryshader_Release(IWineD3DGeometryShader
*iface
)
1968 struct wined3d_geometryshader
*shader
= (struct wined3d_geometryshader
*)iface
;
1969 ULONG refcount
= InterlockedDecrement(&shader
->base_shader
.ref
);
1971 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
1975 shader_cleanup((IWineD3DBaseShader
*)iface
);
1976 shader
->base_shader
.parent_ops
->wined3d_object_destroyed(shader
->base_shader
.parent
);
1977 HeapFree(GetProcessHeap(), 0, shader
);
1983 static void * STDMETHODCALLTYPE
geometryshader_GetParent(IWineD3DGeometryShader
*iface
)
1985 TRACE("iface %p.\n", iface
);
1987 return ((IWineD3DBaseShaderImpl
*)iface
)->baseShader
.parent
;
1990 static HRESULT STDMETHODCALLTYPE
geometryshader_GetFunction(IWineD3DGeometryShader
*iface
, void *data
, UINT
*data_size
)
1992 TRACE("iface %p, data %p, data_size %p.\n", iface
, data
, data_size
);
1994 return shader_get_function((IWineD3DBaseShaderImpl
*)iface
, data
, data_size
);
1997 static const IWineD3DGeometryShaderVtbl wined3d_geometryshader_vtbl
=
1999 /* IUnknown methods */
2000 geometryshader_QueryInterface
,
2001 geometryshader_AddRef
,
2002 geometryshader_Release
,
2003 /* IWineD3DBase methods */
2004 geometryshader_GetParent
,
2005 /* IWineD3DBaseShader methods */
2006 geometryshader_GetFunction
,
2009 HRESULT
geometryshader_init(struct wined3d_geometryshader
*shader
, IWineD3DDeviceImpl
*device
,
2010 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
2011 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2015 shader
->vtbl
= &wined3d_geometryshader_vtbl
;
2016 shader_init(&shader
->base_shader
, device
, parent
, parent_ops
);
2018 hr
= shader_set_function((IWineD3DBaseShaderImpl
*)shader
, byte_code
, output_signature
, 0);
2021 WARN("Failed to set function, hr %#x.\n", hr
);
2022 shader_cleanup((IWineD3DBaseShader
*)shader
);
2026 shader
->base_shader
.load_local_constsF
= FALSE
;
2031 static HRESULT STDMETHODCALLTYPE
pixelshader_QueryInterface(IWineD3DPixelShader
*iface
, REFIID riid
, void **object
)
2033 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), object
);
2035 if (IsEqualGUID(riid
, &IID_IWineD3DPixelShader
)
2036 || IsEqualGUID(riid
, &IID_IWineD3DBaseShader
)
2037 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
2038 || IsEqualGUID(riid
, &IID_IUnknown
))
2040 IUnknown_AddRef(iface
);
2045 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
2048 return E_NOINTERFACE
;
2051 static ULONG STDMETHODCALLTYPE
pixelshader_AddRef(IWineD3DPixelShader
*iface
)
2053 IWineD3DPixelShaderImpl
*shader
= (IWineD3DPixelShaderImpl
*)iface
;
2054 ULONG refcount
= InterlockedIncrement(&shader
->baseShader
.ref
);
2056 TRACE("%p increasing refcount to %u.\n", shader
, refcount
);
2061 /* Do not call while under the GL lock. */
2062 static ULONG STDMETHODCALLTYPE
pixelshader_Release(IWineD3DPixelShader
*iface
)
2064 IWineD3DPixelShaderImpl
*shader
= (IWineD3DPixelShaderImpl
*)iface
;
2065 ULONG refcount
= InterlockedDecrement(&shader
->baseShader
.ref
);
2067 TRACE("%p decreasing refcount to %u.\n", shader
, refcount
);
2071 shader_cleanup((IWineD3DBaseShader
*)iface
);
2072 shader
->baseShader
.parent_ops
->wined3d_object_destroyed(shader
->baseShader
.parent
);
2073 HeapFree(GetProcessHeap(), 0, shader
);
2079 static void * STDMETHODCALLTYPE
pixelshader_GetParent(IWineD3DPixelShader
*iface
)
2081 TRACE("iface %p.\n", iface
);
2083 return ((IWineD3DBaseShaderImpl
*)iface
)->baseShader
.parent
;
2086 static HRESULT STDMETHODCALLTYPE
pixelshader_GetFunction(IWineD3DPixelShader
*iface
, void *data
, UINT
*data_size
)
2088 TRACE("iface %p, data %p, data_size %p.\n", iface
, data
, data_size
);
2090 return shader_get_function((IWineD3DBaseShaderImpl
*)iface
, data
, data_size
);
2093 static const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl
=
2095 /* IUnknown methods */
2096 pixelshader_QueryInterface
,
2098 pixelshader_Release
,
2099 /* IWineD3DBase methods */
2100 pixelshader_GetParent
,
2101 /* IWineD3DBaseShader methods */
2102 pixelshader_GetFunction
2105 void find_ps_compile_args(const struct wined3d_state
*state
,
2106 IWineD3DPixelShaderImpl
*shader
, struct ps_compile_args
*args
)
2108 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)shader
->baseShader
.device
;
2109 IWineD3DBaseTextureImpl
*texture
;
2112 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set. */
2113 if (state
->render_states
[WINED3DRS_SRGBWRITEENABLE
])
2115 IWineD3DSurfaceImpl
*rt
= device
->render_targets
[0];
2116 if (rt
->resource
.format
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
) args
->srgb_correction
= 1;
2119 if (shader
->baseShader
.reg_maps
.shader_version
.major
== 1
2120 && shader
->baseShader
.reg_maps
.shader_version
.minor
<= 3)
2122 for (i
= 0; i
< 4; ++i
)
2124 DWORD flags
= state
->texture_states
[i
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
2125 DWORD tex_transform
= flags
& ~WINED3DTTFF_PROJECTED
;
2126 if (flags
& WINED3DTTFF_PROJECTED
)
2127 tex_transform
|= WINED3D_PSARGS_PROJECTED
;
2128 args
->tex_transform
|= tex_transform
<< i
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
2132 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2134 if (!shader
->baseShader
.reg_maps
.sampler_type
[i
]) continue;
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
->baseTexture
.pow2Matrix_identity
)
2149 args
->np2_fixup
|= (1 << i
);
2152 if (shader
->baseShader
.reg_maps
.shader_version
.major
>= 3)
2154 if (device
->strided_streams
.position_transformed
)
2156 args
->vp_mode
= pretransformed
;
2158 else if (use_vs(state
))
2160 args
->vp_mode
= vertexshader
;
2164 args
->vp_mode
= fixedfunction
;
2166 args
->fog
= FOG_OFF
;
2170 args
->vp_mode
= vertexshader
;
2171 if (state
->render_states
[WINED3DRS_FOGENABLE
])
2173 switch (state
->render_states
[WINED3DRS_FOGTABLEMODE
])
2175 case WINED3DFOG_NONE
:
2176 if (device
->strided_streams
.position_transformed
|| use_vs(state
))
2178 args
->fog
= FOG_LINEAR
;
2182 switch (state
->render_states
[WINED3DRS_FOGVERTEXMODE
])
2184 case WINED3DFOG_NONE
: /* Fall through. */
2185 case WINED3DFOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
2186 case WINED3DFOG_EXP
: args
->fog
= FOG_EXP
; break;
2187 case WINED3DFOG_EXP2
: args
->fog
= FOG_EXP2
; break;
2191 case WINED3DFOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
2192 case WINED3DFOG_EXP
: args
->fog
= FOG_EXP
; break;
2193 case WINED3DFOG_EXP2
: args
->fog
= FOG_EXP2
; break;
2198 args
->fog
= FOG_OFF
;
2203 static void pixelshader_set_limits(IWineD3DPixelShaderImpl
*shader
)
2205 DWORD shader_version
= WINED3D_SHADER_VERSION(shader
->baseShader
.reg_maps
.shader_version
.major
,
2206 shader
->baseShader
.reg_maps
.shader_version
.minor
);
2208 shader
->baseShader
.limits
.attributes
= 0;
2209 shader
->baseShader
.limits
.address
= 0;
2210 shader
->baseShader
.limits
.packed_output
= 0;
2212 switch (shader_version
)
2214 case WINED3D_SHADER_VERSION(1, 0):
2215 case WINED3D_SHADER_VERSION(1, 1):
2216 case WINED3D_SHADER_VERSION(1, 2):
2217 case WINED3D_SHADER_VERSION(1, 3):
2218 shader
->baseShader
.limits
.temporary
= 2;
2219 shader
->baseShader
.limits
.constant_float
= 8;
2220 shader
->baseShader
.limits
.constant_int
= 0;
2221 shader
->baseShader
.limits
.constant_bool
= 0;
2222 shader
->baseShader
.limits
.texcoord
= 4;
2223 shader
->baseShader
.limits
.sampler
= 4;
2224 shader
->baseShader
.limits
.packed_input
= 0;
2225 shader
->baseShader
.limits
.label
= 0;
2228 case WINED3D_SHADER_VERSION(1, 4):
2229 shader
->baseShader
.limits
.temporary
= 6;
2230 shader
->baseShader
.limits
.constant_float
= 8;
2231 shader
->baseShader
.limits
.constant_int
= 0;
2232 shader
->baseShader
.limits
.constant_bool
= 0;
2233 shader
->baseShader
.limits
.texcoord
= 6;
2234 shader
->baseShader
.limits
.sampler
= 6;
2235 shader
->baseShader
.limits
.packed_input
= 0;
2236 shader
->baseShader
.limits
.label
= 0;
2239 /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2240 case WINED3D_SHADER_VERSION(2, 0):
2241 shader
->baseShader
.limits
.temporary
= 32;
2242 shader
->baseShader
.limits
.constant_float
= 32;
2243 shader
->baseShader
.limits
.constant_int
= 16;
2244 shader
->baseShader
.limits
.constant_bool
= 16;
2245 shader
->baseShader
.limits
.texcoord
= 8;
2246 shader
->baseShader
.limits
.sampler
= 16;
2247 shader
->baseShader
.limits
.packed_input
= 0;
2250 case WINED3D_SHADER_VERSION(2, 1):
2251 shader
->baseShader
.limits
.temporary
= 32;
2252 shader
->baseShader
.limits
.constant_float
= 32;
2253 shader
->baseShader
.limits
.constant_int
= 16;
2254 shader
->baseShader
.limits
.constant_bool
= 16;
2255 shader
->baseShader
.limits
.texcoord
= 8;
2256 shader
->baseShader
.limits
.sampler
= 16;
2257 shader
->baseShader
.limits
.packed_input
= 0;
2258 shader
->baseShader
.limits
.label
= 16;
2261 case WINED3D_SHADER_VERSION(4, 0):
2262 FIXME("Using 3.0 limits for 4.0 shader.\n");
2265 case WINED3D_SHADER_VERSION(3, 0):
2266 shader
->baseShader
.limits
.temporary
= 32;
2267 shader
->baseShader
.limits
.constant_float
= 224;
2268 shader
->baseShader
.limits
.constant_int
= 16;
2269 shader
->baseShader
.limits
.constant_bool
= 16;
2270 shader
->baseShader
.limits
.texcoord
= 0;
2271 shader
->baseShader
.limits
.sampler
= 16;
2272 shader
->baseShader
.limits
.packed_input
= 12;
2273 shader
->baseShader
.limits
.label
= 16; /* FIXME: 2048 */
2277 shader
->baseShader
.limits
.temporary
= 32;
2278 shader
->baseShader
.limits
.constant_float
= 32;
2279 shader
->baseShader
.limits
.constant_int
= 16;
2280 shader
->baseShader
.limits
.constant_bool
= 16;
2281 shader
->baseShader
.limits
.texcoord
= 8;
2282 shader
->baseShader
.limits
.sampler
= 16;
2283 shader
->baseShader
.limits
.packed_input
= 0;
2284 shader
->baseShader
.limits
.label
= 0;
2285 FIXME("Unrecognized pixel shader version %u.%u\n",
2286 shader
->baseShader
.reg_maps
.shader_version
.major
,
2287 shader
->baseShader
.reg_maps
.shader_version
.minor
);
2291 HRESULT
pixelshader_init(IWineD3DPixelShaderImpl
*shader
, IWineD3DDeviceImpl
*device
,
2292 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
2293 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2295 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2296 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
2299 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
2301 shader
->lpVtbl
= &IWineD3DPixelShader_Vtbl
;
2302 shader_init(&shader
->baseShader
, device
, parent
, parent_ops
);
2304 hr
= shader_set_function((IWineD3DBaseShaderImpl
*)shader
, byte_code
,
2305 output_signature
, device
->d3d_pshader_constantF
);
2308 WARN("Failed to set function, hr %#x.\n", hr
);
2309 shader_cleanup((IWineD3DBaseShader
*)shader
);
2313 pixelshader_set_limits(shader
);
2315 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2317 if (shader
->input_reg_used
[i
])
2320 highest_reg_used
= i
;
2324 /* Don't do any register mapping magic if it is not needed, or if we can't
2325 * achieve anything anyway */
2326 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
2327 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2329 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
2331 /* This happens with relative addressing. The input mapper function
2332 * warns about this if the higher registers are declared too, so
2333 * don't write a FIXME here */
2334 WARN("More varying registers used than supported\n");
2337 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2339 shader
->input_reg_map
[i
] = i
;
2342 shader
->declared_in_count
= highest_reg_used
+ 1;
2346 shader
->declared_in_count
= 0;
2347 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
2349 if (shader
->input_reg_used
[i
]) shader
->input_reg_map
[i
] = shader
->declared_in_count
++;
2350 else shader
->input_reg_map
[i
] = ~0U;
2354 shader
->baseShader
.load_local_constsF
= FALSE
;
2359 void pixelshader_update_samplers(struct shader_reg_maps
*reg_maps
, IWineD3DBaseTexture
* const *textures
)
2361 WINED3DSAMPLER_TEXTURE_TYPE
*sampler_type
= reg_maps
->sampler_type
;
2364 if (reg_maps
->shader_version
.major
!= 1) return;
2366 for (i
= 0; i
< max(MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
); ++i
)
2368 /* We don't sample from this sampler. */
2369 if (!sampler_type
[i
]) continue;
2373 WARN("No texture bound to sampler %u, using 2D.\n", i
);
2374 sampler_type
[i
] = WINED3DSTT_2D
;
2378 switch (((IWineD3DBaseTextureImpl
*)textures
[i
])->baseTexture
.target
)
2380 case GL_TEXTURE_RECTANGLE_ARB
:
2382 /* We have to select between texture rectangles and 2D
2383 * textures later because 2.0 and 3.0 shaders only have
2384 * WINED3DSTT_2D as well. */
2385 sampler_type
[i
] = WINED3DSTT_2D
;
2389 sampler_type
[i
] = WINED3DSTT_VOLUME
;
2392 case GL_TEXTURE_CUBE_MAP_ARB
:
2393 sampler_type
[i
] = WINED3DSTT_CUBE
;
2397 FIXME("Unrecognized texture type %#x, using 2D.\n",
2398 ((IWineD3DBaseTextureImpl
*)textures
[i
])->baseTexture
.target
);
2399 sampler_type
[i
] = WINED3DSTT_2D
;