2 * shaders implementation
4 * Copyright 2002-2003 Jason Edmeades
5 * Copyright 2002-2003 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2006 Ivan Gyurdiev
9 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
10 * Copyright 2009 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wined3d_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
33 WINE_DECLARE_DEBUG_CHANNEL(d3d
);
35 static void shader_dump_param(const DWORD param
, const DWORD addr_token
, int input
, DWORD shader_version
);
37 static inline BOOL
shader_is_version_token(DWORD token
) {
38 return shader_is_pshader_version(token
) ||
39 shader_is_vshader_version(token
);
42 void shader_buffer_init(struct SHADER_BUFFER
*buffer
)
44 buffer
->buffer
= HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE
);
45 buffer
->buffer
[0] = '\0';
48 buffer
->newline
= TRUE
;
51 void shader_buffer_free(struct SHADER_BUFFER
*buffer
)
53 HeapFree(GetProcessHeap(), 0, buffer
->buffer
);
56 int shader_vaddline(SHADER_BUFFER
* buffer
, const char *format
, va_list args
)
58 char* base
= buffer
->buffer
+ buffer
->bsize
;
61 rc
= vsnprintf(base
, SHADER_PGMSIZE
- 1 - buffer
->bsize
, format
, args
);
63 if (rc
< 0 || /* C89 */
64 rc
> SHADER_PGMSIZE
- 1 - buffer
->bsize
) { /* C99 */
66 ERR("The buffer allocated for the shader program string "
67 "is too small at %d bytes.\n", SHADER_PGMSIZE
);
68 buffer
->bsize
= SHADER_PGMSIZE
- 1;
72 if (buffer
->newline
) {
73 TRACE("GL HW (%u, %u) : %s", buffer
->lineNo
+ 1, buffer
->bsize
, base
);
74 buffer
->newline
= FALSE
;
80 if (buffer
->buffer
[buffer
->bsize
-1] == '\n') {
82 buffer
->newline
= TRUE
;
87 int shader_addline(SHADER_BUFFER
* buffer
, const char *format
, ...)
92 va_start(args
, format
);
93 ret
= shader_vaddline(buffer
, format
, args
);
99 void shader_init(struct IWineD3DBaseShaderClass
*shader
,
100 IWineD3DDevice
*device
, const SHADER_OPCODE
*instruction_table
)
103 shader
->device
= device
;
104 shader
->shader_ins
= instruction_table
;
105 list_init(&shader
->linked_programs
);
108 const SHADER_OPCODE
*shader_get_opcode(const SHADER_OPCODE
*opcode_table
, DWORD shader_version
, DWORD code
)
112 /** TODO: use dichotomic search */
113 while (opcode_table
[i
].name
)
115 if ((code
& WINED3DSI_OPCODE_MASK
) == opcode_table
[i
].opcode
116 && shader_version
>= opcode_table
[i
].min_version
117 && (!opcode_table
[i
].max_version
|| shader_version
<= opcode_table
[i
].max_version
))
119 return &opcode_table
[i
];
124 FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n",
125 code
, code
, code
& WINED3DSI_OPCODE_MASK
, shader_version
);
130 /* Read a parameter opcode from the input stream,
131 * and possibly a relative addressing token.
132 * Return the number of tokens read */
133 static int shader_get_param(const DWORD
*pToken
, DWORD shader_version
, DWORD
*param
, DWORD
*addr_token
)
139 /* PS >= 3.0 have relative addressing (with token)
140 * VS >= 2.0 have relative addressing (with token)
141 * VS >= 1.0 < 2.0 have relative addressing (without token)
142 * The version check below should work in general */
143 if (*pToken
& WINED3DSHADER_ADDRMODE_RELATIVE
)
145 if (WINED3DSHADER_VERSION_MAJOR(shader_version
) < 2)
147 *addr_token
= (1 << 31)
148 | ((WINED3DSPR_ADDR
<< WINED3DSP_REGTYPE_SHIFT2
) & WINED3DSP_REGTYPE_MASK2
)
149 | ((WINED3DSPR_ADDR
<< WINED3DSP_REGTYPE_SHIFT
) & WINED3DSP_REGTYPE_MASK
)
150 | (WINED3DSP_NOSWIZZLE
<< WINED3DSP_SWIZZLE_SHIFT
);
154 *addr_token
= *(pToken
+ 1);
162 /* Return the number of parameters to skip for an opcode */
163 static inline int shader_skip_opcode(const SHADER_OPCODE
*curOpcode
, DWORD opcode_token
, DWORD shader_version
)
165 /* Shaders >= 2.0 may contain address tokens, but fortunately they
166 * have a useful length mask - use it here. Shaders 1.0 contain no such tokens */
167 return (WINED3DSHADER_VERSION_MAJOR(shader_version
) >= 2)
168 ? ((opcode_token
& WINED3DSI_INSTLENGTH_MASK
) >> WINED3DSI_INSTLENGTH_SHIFT
) : curOpcode
->num_params
;
171 /* Read the parameters of an unrecognized opcode from the input stream
172 * Return the number of tokens read.
174 * Note: This function assumes source or destination token format.
175 * It will not work with specially-formatted tokens like DEF or DCL,
176 * but hopefully those would be recognized */
177 static int shader_skip_unrecognized(const DWORD
*pToken
, DWORD shader_version
)
182 /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
183 while (*pToken
& 0x80000000) {
185 DWORD param
, addr_token
= 0;
186 tokens_read
+= shader_get_param(pToken
, shader_version
, ¶m
, &addr_token
);
187 pToken
+= tokens_read
;
189 FIXME("Unrecognized opcode param: token=0x%08x "
190 "addr_token=0x%08x name=", param
, addr_token
);
191 shader_dump_param(param
, addr_token
, i
, shader_version
);
198 /* Convert floating point offset relative
199 * to a register file to an absolute offset for float constants */
200 static unsigned int shader_get_float_offset(const DWORD reg
)
202 unsigned int regnum
= reg
& WINED3DSP_REGNUM_MASK
;
203 int regtype
= shader_get_regtype(reg
);
206 case WINED3DSPR_CONST
: return regnum
;
207 case WINED3DSPR_CONST2
: return 2048 + regnum
;
208 case WINED3DSPR_CONST3
: return 4096 + regnum
;
209 case WINED3DSPR_CONST4
: return 6144 + regnum
;
211 FIXME("Unsupported register type: %d\n", regtype
);
216 static void shader_delete_constant_list(struct list
* clist
) {
219 struct local_constant
* constant
;
221 ptr
= list_head(clist
);
223 constant
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
224 ptr
= list_next(clist
, ptr
);
225 HeapFree(GetProcessHeap(), 0, constant
);
230 static void shader_parse_dst_param(DWORD param
, const struct wined3d_shader_src_param
*rel_addr
,
231 struct wined3d_shader_dst_param
*dst
)
233 dst
->register_type
= ((param
& WINED3DSP_REGTYPE_MASK
) >> WINED3DSP_REGTYPE_SHIFT
)
234 | ((param
& WINED3DSP_REGTYPE_MASK2
) >> WINED3DSP_REGTYPE_SHIFT2
);
235 dst
->register_idx
= param
& WINED3DSP_REGNUM_MASK
;
236 dst
->write_mask
= param
& WINED3DSP_WRITEMASK_ALL
;
237 dst
->modifiers
= param
& WINED3DSP_DSTMOD_MASK
;
238 dst
->shift
= (param
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
239 dst
->rel_addr
= rel_addr
;
242 static void shader_parse_src_param(DWORD param
, const struct wined3d_shader_src_param
*rel_addr
,
243 struct wined3d_shader_src_param
*src
)
245 src
->register_type
= ((param
& WINED3DSP_REGTYPE_MASK
) >> WINED3DSP_REGTYPE_SHIFT
)
246 | ((param
& WINED3DSP_REGTYPE_MASK2
) >> WINED3DSP_REGTYPE_SHIFT2
);
247 src
->register_idx
= param
& WINED3DSP_REGNUM_MASK
;
248 src
->swizzle
= (param
& WINED3DSP_SWIZZLE_MASK
) >> WINED3DSP_SWIZZLE_SHIFT
;
249 src
->modifiers
= (param
& WINED3DSP_SRCMOD_MASK
) >> WINED3DSP_SRCMOD_SHIFT
;
250 src
->rel_addr
= rel_addr
;
253 /* Note that this does not count the loop register
254 * as an address register. */
256 HRESULT
shader_get_registers_used(IWineD3DBaseShader
*iface
, struct shader_reg_maps
*reg_maps
,
257 struct wined3d_shader_semantic
*semantics_in
, struct wined3d_shader_semantic
*semantics_out
,
258 const DWORD
*byte_code
)
260 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
261 const SHADER_OPCODE
*shader_ins
= This
->baseShader
.shader_ins
;
262 DWORD shader_version
;
263 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
264 const DWORD
* pToken
= byte_code
;
267 /* There are some minor differences between pixel and vertex shaders */
269 memset(reg_maps
->bumpmat
, 0, sizeof(reg_maps
->bumpmat
));
270 memset(reg_maps
->luminanceparams
, 0, sizeof(reg_maps
->luminanceparams
));
272 /* get_registers_used is called on every compile on some 1.x shaders, which can result
273 * in stacking up a collection of local constants. Delete the old constants if existing
275 shader_delete_constant_list(&This
->baseShader
.constantsF
);
276 shader_delete_constant_list(&This
->baseShader
.constantsB
);
277 shader_delete_constant_list(&This
->baseShader
.constantsI
);
279 /* The version token is supposed to be the first token */
280 if (!shader_is_version_token(*pToken
))
282 FIXME("First token is not a version token, invalid shader.\n");
283 return WINED3DERR_INVALIDCALL
;
285 reg_maps
->shader_version
= shader_version
= *pToken
++;
286 pshader
= shader_is_pshader_version(shader_version
);
288 while (WINED3DVS_END() != *pToken
) {
289 CONST SHADER_OPCODE
* curOpcode
;
293 if (shader_is_comment(*pToken
))
295 DWORD comment_len
= (*pToken
& WINED3DSI_COMMENTSIZE_MASK
) >> WINED3DSI_COMMENTSIZE_SHIFT
;
297 pToken
+= comment_len
;
302 opcode_token
= *pToken
++;
303 curOpcode
= shader_get_opcode(shader_ins
, shader_version
, opcode_token
);
305 /* Unhandled opcode, and its parameters */
306 if (NULL
== curOpcode
) {
307 while (*pToken
& 0x80000000)
310 /* Handle declarations */
311 } else if (WINED3DSIO_DCL
== curOpcode
->opcode
) {
313 DWORD usage
= *pToken
++;
314 DWORD param
= *pToken
++;
315 DWORD regtype
= shader_get_regtype(param
);
316 unsigned int regnum
= param
& WINED3DSP_REGNUM_MASK
;
318 /* Vshader: mark attributes used
319 Pshader: mark 3.0 input registers used, save token */
320 if (WINED3DSPR_INPUT
== regtype
) {
323 reg_maps
->attributes
[regnum
] = 1;
325 reg_maps
->packed_input
[regnum
] = 1;
327 semantics_in
[regnum
].usage
= (usage
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
328 semantics_in
[regnum
].usage_idx
=
329 (usage
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
330 shader_parse_dst_param(param
, NULL
, &semantics_in
[regnum
].reg
);
332 /* Vshader: mark 3.0 output registers used, save token */
333 } else if (WINED3DSPR_OUTPUT
== regtype
) {
334 reg_maps
->packed_output
[regnum
] = 1;
335 semantics_out
[regnum
].usage
= (usage
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
336 semantics_out
[regnum
].usage_idx
=
337 (usage
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
338 shader_parse_dst_param(param
, NULL
, &semantics_out
[regnum
].reg
);
340 if (usage
& (WINED3DDECLUSAGE_FOG
<< WINED3DSP_DCL_USAGE_SHIFT
))
343 /* Save sampler usage token */
344 } else if (WINED3DSPR_SAMPLER
== regtype
)
345 reg_maps
->samplers
[regnum
] = usage
;
347 } else if (WINED3DSIO_DEF
== curOpcode
->opcode
) {
349 local_constant
* lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
350 if (!lconst
) return E_OUTOFMEMORY
;
351 lconst
->idx
= *pToken
& WINED3DSP_REGNUM_MASK
;
352 memcpy(lconst
->value
, pToken
+ 1, 4 * sizeof(DWORD
));
354 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
355 if (WINED3DSHADER_VERSION_MAJOR(shader_version
) == 1 && pshader
)
357 float *value
= (float *) lconst
->value
;
358 if(value
[0] < -1.0) value
[0] = -1.0;
359 else if(value
[0] > 1.0) value
[0] = 1.0;
360 if(value
[1] < -1.0) value
[1] = -1.0;
361 else if(value
[1] > 1.0) value
[1] = 1.0;
362 if(value
[2] < -1.0) value
[2] = -1.0;
363 else if(value
[2] > 1.0) value
[2] = 1.0;
364 if(value
[3] < -1.0) value
[3] = -1.0;
365 else if(value
[3] > 1.0) value
[3] = 1.0;
368 list_add_head(&This
->baseShader
.constantsF
, &lconst
->entry
);
369 pToken
+= curOpcode
->num_params
;
371 } else if (WINED3DSIO_DEFI
== curOpcode
->opcode
) {
373 local_constant
* lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
374 if (!lconst
) return E_OUTOFMEMORY
;
375 lconst
->idx
= *pToken
& WINED3DSP_REGNUM_MASK
;
376 memcpy(lconst
->value
, pToken
+ 1, 4 * sizeof(DWORD
));
377 list_add_head(&This
->baseShader
.constantsI
, &lconst
->entry
);
378 pToken
+= curOpcode
->num_params
;
380 } else if (WINED3DSIO_DEFB
== curOpcode
->opcode
) {
382 local_constant
* lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
383 if (!lconst
) return E_OUTOFMEMORY
;
384 lconst
->idx
= *pToken
& WINED3DSP_REGNUM_MASK
;
385 memcpy(lconst
->value
, pToken
+ 1, 1 * sizeof(DWORD
));
386 list_add_head(&This
->baseShader
.constantsB
, &lconst
->entry
);
387 pToken
+= curOpcode
->num_params
;
389 /* If there's a loop in the shader */
390 } else if (WINED3DSIO_LOOP
== curOpcode
->opcode
||
391 WINED3DSIO_REP
== curOpcode
->opcode
) {
393 if(cur_loop_depth
> max_loop_depth
)
394 max_loop_depth
= cur_loop_depth
;
395 pToken
+= curOpcode
->num_params
;
397 /* Rep and Loop always use an integer constant for the control parameters */
398 This
->baseShader
.uses_int_consts
= TRUE
;
399 } else if (WINED3DSIO_ENDLOOP
== curOpcode
->opcode
||
400 WINED3DSIO_ENDREP
== curOpcode
->opcode
) {
403 /* For subroutine prototypes */
404 } else if (WINED3DSIO_LABEL
== curOpcode
->opcode
) {
406 DWORD snum
= *pToken
& WINED3DSP_REGNUM_MASK
;
407 reg_maps
->labels
[snum
] = 1;
408 pToken
+= curOpcode
->num_params
;
410 /* Set texture, address, temporary registers */
414 /* Declare 1.X samplers implicitly, based on the destination reg. number */
415 if (WINED3DSHADER_VERSION_MAJOR(shader_version
) == 1
416 && pshader
/* Filter different instructions with the same enum values in VS */
417 && (WINED3DSIO_TEX
== curOpcode
->opcode
418 || WINED3DSIO_TEXBEM
== curOpcode
->opcode
419 || WINED3DSIO_TEXBEML
== curOpcode
->opcode
420 || WINED3DSIO_TEXDP3TEX
== curOpcode
->opcode
421 || WINED3DSIO_TEXM3x2TEX
== curOpcode
->opcode
422 || WINED3DSIO_TEXM3x3SPEC
== curOpcode
->opcode
423 || WINED3DSIO_TEXM3x3TEX
== curOpcode
->opcode
424 || WINED3DSIO_TEXM3x3VSPEC
== curOpcode
->opcode
425 || WINED3DSIO_TEXREG2AR
== curOpcode
->opcode
426 || WINED3DSIO_TEXREG2GB
== curOpcode
->opcode
427 || WINED3DSIO_TEXREG2RGB
== curOpcode
->opcode
))
429 /* Fake sampler usage, only set reserved bit and ttype */
430 DWORD sampler_code
= *pToken
& WINED3DSP_REGNUM_MASK
;
432 TRACE("Setting fake 2D sampler for 1.x pixelshader\n");
433 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_2D
;
435 /* texbem is only valid with < 1.4 pixel shaders */
436 if(WINED3DSIO_TEXBEM
== curOpcode
->opcode
||
437 WINED3DSIO_TEXBEML
== curOpcode
->opcode
) {
438 reg_maps
->bumpmat
[sampler_code
] = TRUE
;
439 if(WINED3DSIO_TEXBEML
== curOpcode
->opcode
) {
440 reg_maps
->luminanceparams
[sampler_code
] = TRUE
;
444 if(WINED3DSIO_NRM
== curOpcode
->opcode
) {
445 reg_maps
->usesnrm
= 1;
446 } else if(WINED3DSIO_BEM
== curOpcode
->opcode
&& pshader
) {
447 DWORD regnum
= *pToken
& WINED3DSP_REGNUM_MASK
;
448 reg_maps
->bumpmat
[regnum
] = TRUE
;
449 } else if(WINED3DSIO_DSY
== curOpcode
->opcode
) {
450 reg_maps
->usesdsy
= 1;
453 /* This will loop over all the registers and try to
454 * make a bitmask of the ones we're interested in.
456 * Relative addressing tokens are ignored, but that's
457 * okay, since we'll catch any address registers when
458 * they are initialized (required by spec) */
460 limit
= (opcode_token
& WINED3DSHADER_INSTRUCTION_PREDICATED
)?
461 curOpcode
->num_params
+ 1: curOpcode
->num_params
;
463 for (i
= 0; i
< limit
; ++i
) {
465 DWORD param
, addr_token
, reg
, regtype
;
466 pToken
+= shader_get_param(pToken
, shader_version
, ¶m
, &addr_token
);
468 regtype
= shader_get_regtype(param
);
469 reg
= param
& WINED3DSP_REGNUM_MASK
;
471 if (WINED3DSPR_TEXTURE
== regtype
) { /* vs: WINED3DSPR_ADDR */
474 reg_maps
->texcoord
[reg
] = 1;
476 reg_maps
->address
[reg
] = 1;
479 else if (WINED3DSPR_TEMP
== regtype
)
480 reg_maps
->temporary
[reg
] = 1;
482 else if (WINED3DSPR_INPUT
== regtype
) {
484 reg_maps
->attributes
[reg
] = 1;
486 if(param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
487 /* If relative addressing is used, we must assume that all registers
488 * are used. Even if it is a construct like v3[aL], we can't assume
489 * that v0, v1 and v2 aren't read because aL can be negative
492 for(i
= 0; i
< MAX_REG_INPUT
; i
++) {
493 ((IWineD3DPixelShaderImpl
*) This
)->input_reg_used
[i
] = TRUE
;
496 ((IWineD3DPixelShaderImpl
*) This
)->input_reg_used
[reg
] = TRUE
;
501 else if (WINED3DSPR_RASTOUT
== regtype
&& reg
== 1)
504 else if (WINED3DSPR_MISCTYPE
== regtype
&& reg
== 0 && pshader
)
507 else if(WINED3DSPR_CONST
== regtype
) {
508 if(param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
510 if(reg
<= ((IWineD3DVertexShaderImpl
*) This
)->min_rel_offset
) {
511 ((IWineD3DVertexShaderImpl
*) This
)->min_rel_offset
= reg
;
512 } else if(reg
>= ((IWineD3DVertexShaderImpl
*) This
)->max_rel_offset
) {
513 ((IWineD3DVertexShaderImpl
*) This
)->max_rel_offset
= reg
;
516 reg_maps
->usesrelconstF
= TRUE
;
519 else if(WINED3DSPR_CONSTINT
== regtype
) {
520 This
->baseShader
.uses_int_consts
= TRUE
;
522 else if(WINED3DSPR_CONSTBOOL
== regtype
) {
523 This
->baseShader
.uses_bool_consts
= TRUE
;
526 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and is used
527 * in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel shaders because TECRDOUT
528 * isn't used in them, but future register types might cause issues
530 else if (WINED3DSPR_TEXCRDOUT
== regtype
&& i
== 0 /* Only look at writes */
531 && !pshader
&& WINED3DSHADER_VERSION_MAJOR(shader_version
) < 3)
533 reg_maps
->texcoord_mask
[reg
] |= shader_get_writemask(param
);
539 reg_maps
->loop_depth
= max_loop_depth
;
541 This
->baseShader
.functionLength
= ((char *)pToken
- (char *)byte_code
);
546 static void shader_dump_decl_usage(DWORD decl
, DWORD param
, DWORD shader_version
)
548 DWORD regtype
= shader_get_regtype(param
);
552 if (regtype
== WINED3DSPR_SAMPLER
) {
553 DWORD ttype
= decl
& WINED3DSP_TEXTURETYPE_MASK
;
556 case WINED3DSTT_2D
: TRACE("_2d"); break;
557 case WINED3DSTT_CUBE
: TRACE("_cube"); break;
558 case WINED3DSTT_VOLUME
: TRACE("_volume"); break;
559 default: TRACE("_unknown_ttype(0x%08x)", ttype
);
564 DWORD usage
= decl
& WINED3DSP_DCL_USAGE_MASK
;
565 DWORD idx
= (decl
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
567 /* Pixel shaders 3.0 don't have usage semantics */
568 if (shader_is_pshader_version(shader_version
) && shader_version
< WINED3DPS_VERSION(3,0))
574 case WINED3DDECLUSAGE_POSITION
:
575 TRACE("position%d", idx
);
577 case WINED3DDECLUSAGE_BLENDINDICES
:
580 case WINED3DDECLUSAGE_BLENDWEIGHT
:
583 case WINED3DDECLUSAGE_NORMAL
:
584 TRACE("normal%d", idx
);
586 case WINED3DDECLUSAGE_PSIZE
:
589 case WINED3DDECLUSAGE_COLOR
:
593 TRACE("specular%d", (idx
- 1));
596 case WINED3DDECLUSAGE_TEXCOORD
:
597 TRACE("texture%d", idx
);
599 case WINED3DDECLUSAGE_TANGENT
:
602 case WINED3DDECLUSAGE_BINORMAL
:
605 case WINED3DDECLUSAGE_TESSFACTOR
:
608 case WINED3DDECLUSAGE_POSITIONT
:
609 TRACE("positionT%d", idx
);
611 case WINED3DDECLUSAGE_FOG
:
614 case WINED3DDECLUSAGE_DEPTH
:
617 case WINED3DDECLUSAGE_SAMPLE
:
621 FIXME("unknown_semantics(0x%08x)", usage
);
626 static void shader_dump_arr_entry(const DWORD param
, const DWORD addr_token
,
627 unsigned int reg
, int input
, DWORD shader_version
)
630 ((param
& WINED3DSHADER_ADDRESSMODE_MASK
) == WINED3DSHADER_ADDRMODE_RELATIVE
);
635 shader_dump_param(addr_token
, 0, input
, shader_version
);
645 static void shader_dump_param(const DWORD param
, const DWORD addr_token
, int input
, DWORD shader_version
)
647 static const char * const rastout_reg_names
[] = { "oPos", "oFog", "oPts" };
648 static const char * const misctype_reg_names
[] = { "vPos", "vFace"};
649 const char *swizzle_reg_chars
= "xyzw";
651 DWORD reg
= param
& WINED3DSP_REGNUM_MASK
;
652 DWORD regtype
= shader_get_regtype(param
);
653 DWORD modifier
= param
& WINED3DSP_SRCMOD_MASK
;
655 /* There are some minor differences between pixel and vertex shaders */
656 char pshader
= shader_is_pshader_version(shader_version
);
659 if ( (modifier
== WINED3DSPSM_NEG
) ||
660 (modifier
== WINED3DSPSM_BIASNEG
) ||
661 (modifier
== WINED3DSPSM_SIGNNEG
) ||
662 (modifier
== WINED3DSPSM_X2NEG
) ||
663 (modifier
== WINED3DSPSM_ABSNEG
) )
665 else if (modifier
== WINED3DSPSM_COMP
)
667 else if (modifier
== WINED3DSPSM_NOT
)
670 if (modifier
== WINED3DSPSM_ABS
|| modifier
== WINED3DSPSM_ABSNEG
)
675 case WINED3DSPR_TEMP
:
678 case WINED3DSPR_INPUT
:
680 shader_dump_arr_entry(param
, addr_token
, reg
, input
, shader_version
);
682 case WINED3DSPR_CONST
:
683 case WINED3DSPR_CONST2
:
684 case WINED3DSPR_CONST3
:
685 case WINED3DSPR_CONST4
:
687 shader_dump_arr_entry(param
, addr_token
, shader_get_float_offset(param
), input
, shader_version
);
689 case WINED3DSPR_TEXTURE
: /* vs: case D3DSPR_ADDR */
690 TRACE("%c%u", (pshader
? 't':'a'), reg
);
692 case WINED3DSPR_RASTOUT
:
693 TRACE("%s", rastout_reg_names
[reg
]);
695 case WINED3DSPR_COLOROUT
:
698 case WINED3DSPR_DEPTHOUT
:
701 case WINED3DSPR_ATTROUT
:
704 case WINED3DSPR_TEXCRDOUT
:
706 /* Vertex shaders >= 3.0 use general purpose output registers
707 * (WINED3DSPR_OUTPUT), which can include an address token */
709 if (WINED3DSHADER_VERSION_MAJOR(shader_version
) >= 3) {
711 shader_dump_arr_entry(param
, addr_token
, reg
, input
, shader_version
);
716 case WINED3DSPR_CONSTINT
:
718 shader_dump_arr_entry(param
, addr_token
, reg
, input
, shader_version
);
720 case WINED3DSPR_CONSTBOOL
:
722 shader_dump_arr_entry(param
, addr_token
, reg
, input
, shader_version
);
724 case WINED3DSPR_LABEL
:
727 case WINED3DSPR_LOOP
:
730 case WINED3DSPR_SAMPLER
:
733 case WINED3DSPR_MISCTYPE
:
735 FIXME("Unhandled misctype register %d\n", reg
);
737 TRACE("%s", misctype_reg_names
[reg
]);
740 case WINED3DSPR_PREDICATE
:
744 TRACE("unhandled_rtype(%#x)", regtype
);
749 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
751 if ((param
& WINED3DSP_WRITEMASK_ALL
) != WINED3DSP_WRITEMASK_ALL
) {
753 if (param
& WINED3DSP_WRITEMASK_0
) TRACE("%c", swizzle_reg_chars
[0]);
754 if (param
& WINED3DSP_WRITEMASK_1
) TRACE("%c", swizzle_reg_chars
[1]);
755 if (param
& WINED3DSP_WRITEMASK_2
) TRACE("%c", swizzle_reg_chars
[2]);
756 if (param
& WINED3DSP_WRITEMASK_3
) TRACE("%c", swizzle_reg_chars
[3]);
761 DWORD swizzle
= (param
& WINED3DSP_SWIZZLE_MASK
) >> WINED3DSP_SWIZZLE_SHIFT
;
762 DWORD swizzle_x
= swizzle
& 0x03;
763 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
764 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
765 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
769 case WINED3DSPSM_NONE
: break;
770 case WINED3DSPSM_NEG
: break;
771 case WINED3DSPSM_NOT
: break;
772 case WINED3DSPSM_BIAS
: TRACE("_bias"); break;
773 case WINED3DSPSM_BIASNEG
: TRACE("_bias"); break;
774 case WINED3DSPSM_SIGN
: TRACE("_bx2"); break;
775 case WINED3DSPSM_SIGNNEG
: TRACE("_bx2"); break;
776 case WINED3DSPSM_COMP
: break;
777 case WINED3DSPSM_X2
: TRACE("_x2"); break;
778 case WINED3DSPSM_X2NEG
: TRACE("_x2"); break;
779 case WINED3DSPSM_DZ
: TRACE("_dz"); break;
780 case WINED3DSPSM_DW
: TRACE("_dw"); break;
781 case WINED3DSPSM_ABSNEG
: TRACE(")"); break;
782 case WINED3DSPSM_ABS
: TRACE(")"); break;
784 TRACE("_unknown_modifier(%#x)", modifier
);
789 * swizzle bits fields:
792 if (swizzle
!= WINED3DSP_NOSWIZZLE
)
794 if (swizzle_x
== swizzle_y
&&
795 swizzle_x
== swizzle_z
&&
796 swizzle_x
== swizzle_w
) {
797 TRACE(".%c", swizzle_reg_chars
[swizzle_x
]);
800 swizzle_reg_chars
[swizzle_x
],
801 swizzle_reg_chars
[swizzle_y
],
802 swizzle_reg_chars
[swizzle_z
],
803 swizzle_reg_chars
[swizzle_w
]);
809 /* Shared code in order to generate the bulk of the shader string.
810 * NOTE: A description of how to parse tokens can be found on msdn */
811 void shader_generate_main(IWineD3DBaseShader
*iface
, SHADER_BUFFER
* buffer
,
812 const shader_reg_maps
* reg_maps
, CONST DWORD
* pFunction
)
814 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
815 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
; /* To access shader backend callbacks */
816 const SHADER_OPCODE
*opcode_table
= This
->baseShader
.shader_ins
;
817 const SHADER_HANDLER
*handler_table
= device
->shader_backend
->shader_instruction_handler_table
;
818 DWORD shader_version
= reg_maps
->shader_version
;
819 struct wined3d_shader_src_param src_rel_addr
[4];
820 struct wined3d_shader_src_param src_param
[4];
821 struct wined3d_shader_src_param dst_rel_addr
;
822 struct wined3d_shader_dst_param dst_param
;
823 struct wined3d_shader_instruction ins
;
824 struct wined3d_shader_context ctx
;
825 const DWORD
*pToken
= pFunction
;
826 const SHADER_OPCODE
*curOpcode
;
827 SHADER_HANDLER hw_fct
;
830 /* Initialize current parsing state */
832 ctx
.reg_maps
= reg_maps
;
836 ins
.dst
= &dst_param
;
838 This
->baseShader
.parse_state
.current_row
= 0;
840 while (WINED3DPS_END() != *pToken
)
844 /* Skip version token */
845 if (shader_is_version_token(*pToken
))
851 /* Skip comment tokens */
852 if (shader_is_comment(*pToken
))
854 pToken
+= (*pToken
& WINED3DSI_COMMENTSIZE_MASK
) >> WINED3DSI_COMMENTSIZE_SHIFT
;
860 opcode_token
= *pToken
++;
861 curOpcode
= shader_get_opcode(opcode_table
, shader_version
, opcode_token
);
863 /* Unknown opcode and its parameters */
866 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token
);
867 pToken
+= shader_skip_unrecognized(pToken
, shader_version
);
872 if (WINED3DSIO_DCL
== curOpcode
->opcode
873 || WINED3DSIO_NOP
== curOpcode
->opcode
874 || WINED3DSIO_DEF
== curOpcode
->opcode
875 || WINED3DSIO_DEFI
== curOpcode
->opcode
876 || WINED3DSIO_DEFB
== curOpcode
->opcode
877 || WINED3DSIO_PHASE
== curOpcode
->opcode
878 || WINED3DSIO_RET
== curOpcode
->opcode
)
880 pToken
+= shader_skip_opcode(curOpcode
, opcode_token
, shader_version
);
885 hw_fct
= handler_table
[curOpcode
->handler_idx
];
887 /* Unhandled opcode */
890 FIXME("Can't handle opcode %s in hwShader\n", curOpcode
->name
);
891 pToken
+= shader_skip_opcode(curOpcode
, opcode_token
, shader_version
);
895 ins
.handler_idx
= curOpcode
->handler_idx
;
896 ins
.flags
= opcode_token
& WINED3D_OPCODESPECIFICCONTROL_MASK
;
897 ins
.coissue
= opcode_token
& WINED3DSI_COISSUE
;
899 /* Destination token */
900 ins
.dst_count
= curOpcode
->dst_token
? 1 : 0;
903 DWORD param
, addr_param
;
904 pToken
+= shader_get_param(pToken
, shader_version
, ¶m
, &addr_param
);
906 if (param
& WINED3DSHADER_ADDRMODE_RELATIVE
)
908 shader_parse_src_param(addr_param
, NULL
, &dst_rel_addr
);
909 shader_parse_dst_param(param
, &dst_rel_addr
, &dst_param
);
913 shader_parse_dst_param(param
, NULL
, &dst_param
);
917 /* Predication token */
918 if (opcode_token
& WINED3DSHADER_INSTRUCTION_PREDICATED
) ins
.predicate
= *pToken
++;
920 /* Other source tokens */
921 ins
.src_count
= curOpcode
->num_params
- curOpcode
->dst_token
;
922 for (i
= 0; i
< ins
.src_count
; ++i
)
924 DWORD param
, addr_param
;
925 pToken
+= shader_get_param(pToken
, shader_version
, ¶m
, &addr_param
);
926 if (param
& WINED3DSHADER_ADDRMODE_RELATIVE
)
928 shader_parse_src_param(addr_param
, NULL
, &src_rel_addr
[i
]);
929 shader_parse_src_param(param
, &src_rel_addr
[i
], &src_param
[i
]);
933 shader_parse_src_param(param
, NULL
, &src_param
[i
]);
937 /* Call appropriate function for output target */
940 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
941 /* FIXME: This should be internal to the shader backend.
942 * Also, right now this is the only reason "shader_mode" exists. */
943 if (This
->baseShader
.shader_mode
== SHADER_GLSL
) shader_glsl_add_instruction_modifiers(&ins
);
947 static void shader_dump_ins_modifiers(const DWORD output
)
949 DWORD shift
= (output
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
950 DWORD mmask
= output
& WINED3DSP_DSTMOD_MASK
;
954 case 13: TRACE("_d8"); break;
955 case 14: TRACE("_d4"); break;
956 case 15: TRACE("_d2"); break;
957 case 1: TRACE("_x2"); break;
958 case 2: TRACE("_x4"); break;
959 case 3: TRACE("_x8"); break;
960 default: TRACE("_unhandled_shift(%d)", shift
); break;
963 if (mmask
& WINED3DSPDM_SATURATE
) TRACE("_sat");
964 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) TRACE("_pp");
965 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) TRACE("_centroid");
967 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
969 FIXME("_unrecognized_modifier(%#x)", mmask
>> WINED3DSP_DSTMOD_SHIFT
);
972 void shader_trace_init(const DWORD
*pFunction
, const SHADER_OPCODE
*opcode_table
)
974 const DWORD
* pToken
= pFunction
;
975 const SHADER_OPCODE
* curOpcode
= NULL
;
976 DWORD shader_version
;
980 TRACE("Parsing %p\n", pFunction
);
982 /* The version token is supposed to be the first token */
983 if (!shader_is_version_token(*pToken
))
985 FIXME("First token is not a version token, invalid shader.\n");
988 shader_version
= *pToken
++;
989 TRACE("%s_%u_%u\n", shader_is_pshader_version(shader_version
) ? "ps": "vs",
990 WINED3DSHADER_VERSION_MAJOR(shader_version
), WINED3DSHADER_VERSION_MINOR(shader_version
));
992 while (WINED3DVS_END() != *pToken
)
994 if (shader_is_comment(*pToken
)) /* comment */
996 DWORD comment_len
= (*pToken
& WINED3DSI_COMMENTSIZE_MASK
) >> WINED3DSI_COMMENTSIZE_SHIFT
;
998 TRACE("//%s\n", (const char*)pToken
);
999 pToken
+= comment_len
;
1002 opcode_token
= *pToken
++;
1003 curOpcode
= shader_get_opcode(opcode_table
, shader_version
, opcode_token
);
1008 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token
);
1009 tokens_read
= shader_skip_unrecognized(pToken
, shader_version
);
1010 pToken
+= tokens_read
;
1014 if (curOpcode
->opcode
== WINED3DSIO_DCL
)
1016 DWORD usage
= *pToken
;
1017 DWORD param
= *(pToken
+ 1);
1019 shader_dump_decl_usage(usage
, param
, shader_version
);
1020 shader_dump_ins_modifiers(param
);
1022 shader_dump_param(param
, 0, 0, shader_version
);
1025 else if (curOpcode
->opcode
== WINED3DSIO_DEF
)
1027 unsigned int offset
= shader_get_float_offset(*pToken
);
1029 TRACE("def c%u = %f, %f, %f, %f", offset
,
1030 *(const float *)(pToken
+ 1),
1031 *(const float *)(pToken
+ 2),
1032 *(const float *)(pToken
+ 3),
1033 *(const float *)(pToken
+ 4));
1036 else if (curOpcode
->opcode
== WINED3DSIO_DEFI
)
1038 TRACE("defi i%u = %d, %d, %d, %d", *pToken
& WINED3DSP_REGNUM_MASK
,
1045 else if (curOpcode
->opcode
== WINED3DSIO_DEFB
)
1047 TRACE("defb b%u = %s", *pToken
& WINED3DSP_REGNUM_MASK
,
1048 *(pToken
+ 1)? "true": "false");
1053 DWORD param
, addr_token
= 0;
1056 /* Print out predication source token first - it follows
1057 * the destination token. */
1058 if (opcode_token
& WINED3DSHADER_INSTRUCTION_PREDICATED
)
1061 shader_dump_param(*(pToken
+ 2), 0, 1, shader_version
);
1064 if (opcode_token
& WINED3DSI_COISSUE
)
1066 /* PixWin marks instructions with the coissue flag with a '+' */
1070 TRACE("%s", curOpcode
->name
);
1072 if (curOpcode
->opcode
== WINED3DSIO_IFC
1073 || curOpcode
->opcode
== WINED3DSIO_BREAKC
)
1075 DWORD op
= (opcode_token
& INST_CONTROLS_MASK
) >> INST_CONTROLS_SHIFT
;
1079 case COMPARISON_GT
: TRACE("_gt"); break;
1080 case COMPARISON_EQ
: TRACE("_eq"); break;
1081 case COMPARISON_GE
: TRACE("_ge"); break;
1082 case COMPARISON_LT
: TRACE("_lt"); break;
1083 case COMPARISON_NE
: TRACE("_ne"); break;
1084 case COMPARISON_LE
: TRACE("_le"); break;
1085 default: TRACE("_(%u)", op
);
1088 else if (curOpcode
->opcode
== WINED3DSIO_TEX
1089 && shader_version
>= WINED3DPS_VERSION(2,0)
1090 && (opcode_token
& WINED3DSI_TEXLD_PROJECT
))
1095 /* Destination token */
1096 if (curOpcode
->dst_token
)
1098 tokens_read
= shader_get_param(pToken
, shader_version
, ¶m
, &addr_token
);
1099 pToken
+= tokens_read
;
1101 shader_dump_ins_modifiers(param
);
1103 shader_dump_param(param
, addr_token
, 0, shader_version
);
1106 /* Predication token - already printed out, just skip it */
1107 if (opcode_token
& WINED3DSHADER_INSTRUCTION_PREDICATED
)
1112 /* Other source tokens */
1113 for (i
= curOpcode
->dst_token
; i
< curOpcode
->num_params
; ++i
)
1115 tokens_read
= shader_get_param(pToken
, shader_version
, ¶m
, &addr_token
);
1116 pToken
+= tokens_read
;
1118 TRACE((i
== 0)? " " : ", ");
1119 shader_dump_param(param
, addr_token
, 1, shader_version
);
1127 void shader_cleanup(IWineD3DBaseShader
*iface
)
1129 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)iface
;
1131 ((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->shader_backend
->shader_destroy(iface
);
1132 HeapFree(GetProcessHeap(), 0, This
->baseShader
.function
);
1133 shader_delete_constant_list(&This
->baseShader
.constantsF
);
1134 shader_delete_constant_list(&This
->baseShader
.constantsB
);
1135 shader_delete_constant_list(&This
->baseShader
.constantsI
);
1136 list_remove(&This
->baseShader
.shader_list_entry
);
1139 static const SHADER_HANDLER shader_none_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] = {0};
1140 static void shader_none_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {}
1141 static void shader_none_select_depth_blt(IWineD3DDevice
*iface
, enum tex_types tex_type
) {}
1142 static void shader_none_deselect_depth_blt(IWineD3DDevice
*iface
) {}
1143 static void shader_none_update_float_vertex_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
) {}
1144 static void shader_none_update_float_pixel_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
) {}
1145 static void shader_none_load_constants(IWineD3DDevice
*iface
, char usePS
, char useVS
) {}
1146 static void shader_none_load_np2fixup_constants(IWineD3DDevice
*iface
, char usePS
, char useVS
) {}
1147 static void shader_none_destroy(IWineD3DBaseShader
*iface
) {}
1148 static HRESULT
shader_none_alloc(IWineD3DDevice
*iface
) {return WINED3D_OK
;}
1149 static void shader_none_free(IWineD3DDevice
*iface
) {}
1150 static BOOL
shader_none_dirty_const(IWineD3DDevice
*iface
) {return FALSE
;}
1151 static GLuint
shader_none_generate_pshader(IWineD3DPixelShader
*iface
, SHADER_BUFFER
*buffer
, const struct ps_compile_args
*args
) {
1152 FIXME("NONE shader backend asked to generate a pixel shader\n");
1155 static GLuint
shader_none_generate_vshader(IWineD3DVertexShader
*iface
, SHADER_BUFFER
*buffer
, const struct vs_compile_args
*args
) {
1156 FIXME("NONE shader backend asked to generate a vertex shader\n");
1160 #define GLINFO_LOCATION (*gl_info)
1161 static void shader_none_get_caps(WINED3DDEVTYPE devtype
, const WineD3D_GL_Info
*gl_info
, struct shader_caps
*pCaps
)
1163 /* Set the shader caps to 0 for the none shader backend */
1164 pCaps
->VertexShaderVersion
= 0;
1165 pCaps
->PixelShaderVersion
= 0;
1166 pCaps
->PixelShader1xMaxValue
= 0.0;
1168 #undef GLINFO_LOCATION
1169 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
1171 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
1173 TRACE("Checking support for fixup:\n");
1174 dump_color_fixup_desc(fixup
);
1177 /* Faked to make some apps happy. */
1178 if (!is_yuv_fixup(fixup
))
1184 TRACE("[FAILED]\n");
1188 const shader_backend_t none_shader_backend
= {
1189 shader_none_instruction_handler_table
,
1191 shader_none_select_depth_blt
,
1192 shader_none_deselect_depth_blt
,
1193 shader_none_update_float_vertex_constants
,
1194 shader_none_update_float_pixel_constants
,
1195 shader_none_load_constants
,
1196 shader_none_load_np2fixup_constants
,
1197 shader_none_destroy
,
1200 shader_none_dirty_const
,
1201 shader_none_generate_pshader
,
1202 shader_none_generate_vshader
,
1203 shader_none_get_caps
,
1204 shader_none_color_fixup_supported
,