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
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
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
32 #define GLNAME_REQUIRE_GLSL ((const char *)1)
34 inline static BOOL
shader_is_version_token(DWORD token
) {
35 return shader_is_pshader_version(token
) ||
36 shader_is_vshader_version(token
);
40 SHADER_BUFFER
* buffer
,
41 const char *format
, ...) {
43 char* base
= buffer
->buffer
+ buffer
->bsize
;
47 va_start(args
, format
);
48 rc
= vsnprintf(base
, SHADER_PGMSIZE
- 1 - buffer
->bsize
, format
, args
);
51 if (rc
< 0 || /* C89 */
52 rc
> SHADER_PGMSIZE
- 1 - buffer
->bsize
) { /* C99 */
54 ERR("The buffer allocated for the shader program string "
55 "is too small at %d bytes.\n", SHADER_PGMSIZE
);
56 buffer
->bsize
= SHADER_PGMSIZE
- 1;
62 TRACE("GL HW (%u, %u) : %s", buffer
->lineNo
, buffer
->bsize
, base
);
66 const SHADER_OPCODE
* shader_get_opcode(
67 IWineD3DBaseShader
*iface
, const DWORD code
) {
69 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) iface
;
72 DWORD hex_version
= This
->baseShader
.hex_version
;
73 const SHADER_OPCODE
*shader_ins
= This
->baseShader
.shader_ins
;
75 /** TODO: use dichotomic search */
76 while (NULL
!= shader_ins
[i
].name
) {
77 if (((code
& D3DSI_OPCODE_MASK
) == shader_ins
[i
].opcode
) &&
78 (((hex_version
>= shader_ins
[i
].min_version
) && (hex_version
<= shader_ins
[i
].max_version
)) ||
79 ((shader_ins
[i
].min_version
== 0) && (shader_ins
[i
].max_version
== 0)))) {
80 return &shader_ins
[i
];
84 FIXME("Unsupported opcode %#lx(%ld) masked %#lx, shader version %#lx\n",
85 code
, code
, code
& D3DSI_OPCODE_MASK
, hex_version
);
89 /* Read a parameter opcode from the input stream,
90 * and possibly a relative addressing token.
91 * Return the number of tokens read */
93 IWineD3DBaseShader
* iface
,
98 /* PS >= 3.0 have relative addressing (with token)
99 * VS >= 2.0 have relative addressing (with token)
100 * VS >= 1.0 < 2.0 have relative addressing (without token)
101 * The version check below should work in general */
103 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
104 char rel_token
= D3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 2 &&
105 ((*pToken
& D3DSHADER_ADDRESSMODE_MASK
) == D3DSHADER_ADDRMODE_RELATIVE
);
108 *addr_token
= rel_token
? *(pToken
+ 1): 0;
109 return rel_token
? 2:1;
112 /* Return the number of parameters to skip for an opcode */
113 static inline int shader_skip_opcode(
114 IWineD3DBaseShaderImpl
* This
,
115 const SHADER_OPCODE
* curOpcode
,
116 DWORD opcode_token
) {
118 /* Shaders >= 2.0 may contain address tokens, but fortunately they
119 * have a useful legnth mask - use it here. Shaders 1.0 contain no such tokens */
121 return (D3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 2)?
122 ((opcode_token
& D3DSI_INSTLENGTH_MASK
) >> D3DSI_INSTLENGTH_SHIFT
):
123 curOpcode
->num_params
;
126 /* Read the parameters of an unrecognized opcode from the input stream
127 * Return the number of tokens read.
129 * Note: This function assumes source or destination token format.
130 * It will not work with specially-formatted tokens like DEF or DCL,
131 * but hopefully those would be recognized */
133 int shader_skip_unrecognized(
134 IWineD3DBaseShader
* iface
,
135 const DWORD
* pToken
) {
140 /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
141 while (*pToken
& 0x80000000) {
143 DWORD param
, addr_token
;
144 tokens_read
+= shader_get_param(iface
, pToken
, ¶m
, &addr_token
);
145 pToken
+= tokens_read
;
147 FIXME("Unrecognized opcode param: token=%08lX "
148 "addr_token=%08lX name=", param
, addr_token
);
149 shader_dump_param(iface
, param
, addr_token
, i
);
156 /* Convert floating point offset relative
157 * to a register file to an absolute offset for float constants */
159 unsigned int shader_get_float_offset(const DWORD reg
) {
161 unsigned int regnum
= reg
& D3DSP_REGNUM_MASK
;
162 int regtype
= shader_get_regtype(reg
);
165 case D3DSPR_CONST
: return regnum
;
166 case D3DSPR_CONST2
: return 2048 + regnum
;
167 case D3DSPR_CONST3
: return 4096 + regnum
;
168 case D3DSPR_CONST4
: return 6144 + regnum
;
170 FIXME("Unsupported register type: %d\n", regtype
);
175 /* Note that this does not count the loop register
176 * as an address register. */
178 HRESULT
shader_get_registers_used(
179 IWineD3DBaseShader
*iface
,
180 shader_reg_maps
* reg_maps
,
181 semantic
* semantics_in
,
182 semantic
* semantics_out
,
184 IWineD3DStateBlockImpl
*stateBlock
) {
186 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
188 /* There are some minor differences between pixel and vertex shaders */
189 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
194 while (D3DVS_END() != *pToken
) {
195 CONST SHADER_OPCODE
* curOpcode
;
199 if (shader_is_version_token(*pToken
)) {
204 } else if (shader_is_comment(*pToken
)) {
205 DWORD comment_len
= (*pToken
& D3DSI_COMMENTSIZE_MASK
) >> D3DSI_COMMENTSIZE_SHIFT
;
207 pToken
+= comment_len
;
212 opcode_token
= *pToken
++;
213 curOpcode
= shader_get_opcode(iface
, opcode_token
);
215 /* Unhandled opcode, and its parameters */
216 if (NULL
== curOpcode
) {
217 while (*pToken
& 0x80000000)
220 /* Handle declarations */
221 } else if (D3DSIO_DCL
== curOpcode
->opcode
) {
223 DWORD usage
= *pToken
++;
224 DWORD param
= *pToken
++;
225 DWORD regtype
= shader_get_regtype(param
);
226 unsigned int regnum
= param
& D3DSP_REGNUM_MASK
;
228 /* Vshader: mark attributes used
229 Pshader: mark 3.0 input registers used, save token */
230 if (D3DSPR_INPUT
== regtype
) {
233 reg_maps
->attributes
[regnum
] = 1;
235 reg_maps
->packed_input
[regnum
] = 1;
237 semantics_in
[regnum
].usage
= usage
;
238 semantics_in
[regnum
].reg
= param
;
240 /* Vshader: mark 3.0 output registers used, save token */
241 } else if (D3DSPR_OUTPUT
== regtype
) {
242 reg_maps
->packed_output
[regnum
] = 1;
243 semantics_out
[regnum
].usage
= usage
;
244 semantics_out
[regnum
].reg
= param
;
246 /* Save sampler usage token */
247 } else if (D3DSPR_SAMPLER
== regtype
)
248 reg_maps
->samplers
[regnum
] = usage
;
250 } else if (D3DSIO_DEF
== curOpcode
->opcode
) {
252 local_constant
* lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
253 if (!lconst
) return E_OUTOFMEMORY
;
254 lconst
->idx
= *pToken
& D3DSP_REGNUM_MASK
;
255 memcpy(&lconst
->value
, pToken
+ 1, 4 * sizeof(DWORD
));
256 list_add_head(&This
->baseShader
.constantsF
, &lconst
->entry
);
257 pToken
+= curOpcode
->num_params
;
259 } else if (D3DSIO_DEFI
== curOpcode
->opcode
) {
261 local_constant
* lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
262 if (!lconst
) return E_OUTOFMEMORY
;
263 lconst
->idx
= *pToken
& D3DSP_REGNUM_MASK
;
264 memcpy(&lconst
->value
, pToken
+ 1, 4 * sizeof(DWORD
));
265 list_add_head(&This
->baseShader
.constantsI
, &lconst
->entry
);
266 pToken
+= curOpcode
->num_params
;
268 } else if (D3DSIO_DEFB
== curOpcode
->opcode
) {
270 local_constant
* lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
271 if (!lconst
) return E_OUTOFMEMORY
;
272 lconst
->idx
= *pToken
& D3DSP_REGNUM_MASK
;
273 memcpy(&lconst
->value
, pToken
+ 1, 1 * sizeof(DWORD
));
274 list_add_head(&This
->baseShader
.constantsB
, &lconst
->entry
);
275 pToken
+= curOpcode
->num_params
;
277 /* If there's a loop in the shader */
278 } else if (D3DSIO_LOOP
== curOpcode
->opcode
||
279 D3DSIO_REP
== curOpcode
->opcode
) {
281 pToken
+= curOpcode
->num_params
;
283 /* For subroutine prototypes */
284 } else if (D3DSIO_LABEL
== curOpcode
->opcode
) {
286 DWORD snum
= *pToken
& D3DSP_REGNUM_MASK
;
287 reg_maps
->labels
[snum
] = 1;
288 pToken
+= curOpcode
->num_params
;
290 /* Set texture, address, temporary registers */
294 /* Declare 1.X samplers implicitly, based on the destination reg. number */
295 if (D3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) == 1 &&
296 (D3DSIO_TEX
== curOpcode
->opcode
||
297 D3DSIO_TEXBEM
== curOpcode
->opcode
||
298 D3DSIO_TEXM3x2TEX
== curOpcode
->opcode
||
299 D3DSIO_TEXM3x3TEX
== curOpcode
->opcode
)) {
301 /* Fake sampler usage, only set reserved bit and ttype */
302 DWORD sampler_code
= *pToken
& D3DSP_REGNUM_MASK
;
304 if(!stateBlock
->textures
[sampler_code
]) {
305 ERR("No texture bound to sampler %ld\n", sampler_code
);
306 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_2D
;
308 int texType
= IWineD3DBaseTexture_GetTextureDimensions(stateBlock
->textures
[sampler_code
]);
311 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_1D
;
315 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_2D
;
319 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_VOLUME
;
322 case GLTEXTURECUBEMAP
:
323 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_CUBE
;
327 ERR("Unexpected gl texture type found: %d\n", texType
);
328 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_2D
;
332 } else if (D3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) == 1 &&
333 (D3DSIO_TEXM3x3SPEC
== curOpcode
->opcode
||
334 D3DSIO_TEXM3x3VSPEC
== curOpcode
->opcode
)) {
336 /* 3D sampler usage, only set reserved bit and ttype
337 * FIXME: This could be either Cube or Volume, but we wouldn't know unless
338 * we waited to generate the shader until the textures were all bound.
339 * For now, use Cube textures because they are more common. */
340 DWORD sampler_code
= *pToken
& D3DSP_REGNUM_MASK
;
341 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_CUBE
;
342 } else if (D3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) == 1 &&
343 (D3DSIO_TEXDP3TEX
== curOpcode
->opcode
)) {
345 /* 1D Sampler usage */
346 DWORD sampler_code
= *pToken
& D3DSP_REGNUM_MASK
;
347 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_1D
;
350 /* This will loop over all the registers and try to
351 * make a bitmask of the ones we're interested in.
353 * Relative addressing tokens are ignored, but that's
354 * okay, since we'll catch any address registers when
355 * they are initialized (required by spec) */
357 limit
= (opcode_token
& D3DSHADER_INSTRUCTION_PREDICATED
)?
358 curOpcode
->num_params
+ 1: curOpcode
->num_params
;
360 for (i
= 0; i
< limit
; ++i
) {
362 DWORD param
, addr_token
, reg
, regtype
;
363 pToken
+= shader_get_param(iface
, pToken
, ¶m
, &addr_token
);
365 regtype
= shader_get_regtype(param
);
366 reg
= param
& D3DSP_REGNUM_MASK
;
368 if (D3DSPR_TEXTURE
== regtype
) { /* vs: D3DSPR_ADDR */
371 reg_maps
->texcoord
[reg
] = 1;
373 reg_maps
->address
[reg
] = 1;
376 else if (D3DSPR_TEMP
== regtype
)
377 reg_maps
->temporary
[reg
] = 1;
379 else if (D3DSPR_INPUT
== regtype
&& !pshader
)
380 reg_maps
->attributes
[reg
] = 1;
382 else if (D3DSPR_RASTOUT
== regtype
&& reg
== 1)
391 static void shader_dump_decl_usage(
392 IWineD3DBaseShaderImpl
* This
,
396 DWORD regtype
= shader_get_regtype(param
);
400 if (regtype
== D3DSPR_SAMPLER
) {
401 DWORD ttype
= decl
& WINED3DSP_TEXTURETYPE_MASK
;
404 case WINED3DSTT_2D
: TRACE("_2d"); break;
405 case WINED3DSTT_CUBE
: TRACE("_cube"); break;
406 case WINED3DSTT_VOLUME
: TRACE("_volume"); break;
407 default: TRACE("_unknown_ttype(%08lx)", ttype
);
412 DWORD usage
= decl
& D3DSP_DCL_USAGE_MASK
;
413 DWORD idx
= (decl
& D3DSP_DCL_USAGEINDEX_MASK
) >> D3DSP_DCL_USAGEINDEX_SHIFT
;
415 /* Pixel shaders 3.0 don't have usage semantics */
416 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
417 if (pshader
&& This
->baseShader
.hex_version
< D3DPS_VERSION(3,0))
423 case D3DDECLUSAGE_POSITION
:
424 TRACE("%s%ld", "position", idx
);
426 case D3DDECLUSAGE_BLENDINDICES
:
427 TRACE("%s", "blend");
429 case D3DDECLUSAGE_BLENDWEIGHT
:
430 TRACE("%s", "weight");
432 case D3DDECLUSAGE_NORMAL
:
433 TRACE("%s%ld", "normal", idx
);
435 case D3DDECLUSAGE_PSIZE
:
436 TRACE("%s", "psize");
438 case D3DDECLUSAGE_COLOR
:
440 TRACE("%s", "color");
442 TRACE("%s%ld", "specular", (idx
- 1));
445 case D3DDECLUSAGE_TEXCOORD
:
446 TRACE("%s%ld", "texture", idx
);
448 case D3DDECLUSAGE_TANGENT
:
449 TRACE("%s", "tangent");
451 case D3DDECLUSAGE_BINORMAL
:
452 TRACE("%s", "binormal");
454 case D3DDECLUSAGE_TESSFACTOR
:
455 TRACE("%s", "tessfactor");
457 case D3DDECLUSAGE_POSITIONT
:
458 TRACE("%s%ld", "positionT", idx
);
460 case D3DDECLUSAGE_FOG
:
463 case D3DDECLUSAGE_DEPTH
:
464 TRACE("%s", "depth");
466 case D3DDECLUSAGE_SAMPLE
:
467 TRACE("%s", "sample");
470 FIXME("unknown_semantics(%08lx)", usage
);
475 static void shader_dump_arr_entry(
476 IWineD3DBaseShader
*iface
,
478 const DWORD addr_token
,
483 ((param
& D3DSHADER_ADDRESSMODE_MASK
) == D3DSHADER_ADDRMODE_RELATIVE
);
488 shader_dump_param(iface
, addr_token
, 0, input
);
498 void shader_dump_param(
499 IWineD3DBaseShader
*iface
,
501 const DWORD addr_token
,
504 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
505 static const char* rastout_reg_names
[] = { "oPos", "oFog", "oPts" };
506 char swizzle_reg_chars
[4];
508 DWORD reg
= param
& D3DSP_REGNUM_MASK
;
509 DWORD regtype
= shader_get_regtype(param
);
510 DWORD modifier
= param
& D3DSP_SRCMOD_MASK
;
512 /* There are some minor differences between pixel and vertex shaders */
513 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
515 /* For one, we'd prefer color components to be shown for pshaders.
516 * FIXME: use the swizzle function for this */
518 swizzle_reg_chars
[0] = pshader
? 'r': 'x';
519 swizzle_reg_chars
[1] = pshader
? 'g': 'y';
520 swizzle_reg_chars
[2] = pshader
? 'b': 'z';
521 swizzle_reg_chars
[3] = pshader
? 'a': 'w';
524 if ( (modifier
== D3DSPSM_NEG
) ||
525 (modifier
== D3DSPSM_BIASNEG
) ||
526 (modifier
== D3DSPSM_SIGNNEG
) ||
527 (modifier
== D3DSPSM_X2NEG
) ||
528 (modifier
== D3DSPSM_ABSNEG
) )
530 else if (modifier
== D3DSPSM_COMP
)
532 else if (modifier
== D3DSPSM_NOT
)
535 if (modifier
== D3DSPSM_ABS
|| modifier
== D3DSPSM_ABSNEG
)
545 shader_dump_arr_entry(iface
, param
, addr_token
, reg
, input
);
552 shader_dump_arr_entry(iface
, param
, addr_token
, shader_get_float_offset(param
), input
);
554 case D3DSPR_TEXTURE
: /* vs: case D3DSPR_ADDR */
555 TRACE("%c%lu", (pshader
? 't':'a'), reg
);
558 TRACE("%s", rastout_reg_names
[reg
]);
560 case D3DSPR_COLOROUT
:
563 case D3DSPR_DEPTHOUT
:
569 case D3DSPR_TEXCRDOUT
:
571 /* Vertex shaders >= 3.0 use general purpose output registers
572 * (D3DSPR_OUTPUT), which can include an address token */
574 if (D3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3) {
576 shader_dump_arr_entry(iface
, param
, addr_token
, reg
, input
);
581 case D3DSPR_CONSTINT
:
583 shader_dump_arr_entry(iface
, param
, addr_token
, reg
, input
);
585 case D3DSPR_CONSTBOOL
:
587 shader_dump_arr_entry(iface
, param
, addr_token
, reg
, input
);
598 case D3DSPR_PREDICATE
:
602 TRACE("unhandled_rtype(%#lx)", regtype
);
607 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
609 if ((param
& D3DSP_WRITEMASK_ALL
) != D3DSP_WRITEMASK_ALL
) {
611 if (param
& D3DSP_WRITEMASK_0
) TRACE("%c", swizzle_reg_chars
[0]);
612 if (param
& D3DSP_WRITEMASK_1
) TRACE("%c", swizzle_reg_chars
[1]);
613 if (param
& D3DSP_WRITEMASK_2
) TRACE("%c", swizzle_reg_chars
[2]);
614 if (param
& D3DSP_WRITEMASK_3
) TRACE("%c", swizzle_reg_chars
[3]);
619 DWORD swizzle
= (param
& D3DSP_SWIZZLE_MASK
) >> D3DSP_SWIZZLE_SHIFT
;
620 DWORD swizzle_r
= swizzle
& 0x03;
621 DWORD swizzle_g
= (swizzle
>> 2) & 0x03;
622 DWORD swizzle_b
= (swizzle
>> 4) & 0x03;
623 DWORD swizzle_a
= (swizzle
>> 6) & 0x03;
627 case D3DSPSM_NONE
: break;
628 case D3DSPSM_NEG
: break;
629 case D3DSPSM_NOT
: break;
630 case D3DSPSM_BIAS
: TRACE("_bias"); break;
631 case D3DSPSM_BIASNEG
: TRACE("_bias"); break;
632 case D3DSPSM_SIGN
: TRACE("_bx2"); break;
633 case D3DSPSM_SIGNNEG
: TRACE("_bx2"); break;
634 case D3DSPSM_COMP
: break;
635 case D3DSPSM_X2
: TRACE("_x2"); break;
636 case D3DSPSM_X2NEG
: TRACE("_x2"); break;
637 case D3DSPSM_DZ
: TRACE("_dz"); break;
638 case D3DSPSM_DW
: TRACE("_dw"); break;
639 case D3DSPSM_ABSNEG
: TRACE(")"); break;
640 case D3DSPSM_ABS
: TRACE(")"); break;
642 TRACE("_unknown_modifier(%#lx)", modifier
>> D3DSP_SRCMOD_SHIFT
);
647 * swizzle bits fields:
650 if ((D3DVS_NOSWIZZLE
>> D3DVS_SWIZZLE_SHIFT
) != swizzle
) { /* ! D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
651 if (swizzle_r
== swizzle_g
&&
652 swizzle_r
== swizzle_b
&&
653 swizzle_r
== swizzle_a
) {
654 TRACE(".%c", swizzle_reg_chars
[swizzle_r
]);
657 swizzle_reg_chars
[swizzle_r
],
658 swizzle_reg_chars
[swizzle_g
],
659 swizzle_reg_chars
[swizzle_b
],
660 swizzle_reg_chars
[swizzle_a
]);
666 /** Shared code in order to generate the bulk of the shader string.
667 Use the shader_header_fct & shader_footer_fct to add strings
668 that are specific to pixel or vertex functions
669 NOTE: A description of how to parse tokens can be found at:
670 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/graphics/hh/graphics/usermodedisplaydriver_shader_cc8e4e05-f5c3-4ec0-8853-8ce07c1551b2.xml.asp */
671 void shader_generate_main(
672 IWineD3DBaseShader
*iface
,
673 SHADER_BUFFER
* buffer
,
674 shader_reg_maps
* reg_maps
,
675 CONST DWORD
* pFunction
) {
677 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
678 const DWORD
*pToken
= pFunction
;
679 const SHADER_OPCODE
*curOpcode
= NULL
;
680 SHADER_HANDLER hw_fct
= NULL
;
682 SHADER_OPCODE_ARG hw_arg
;
684 /* Initialize current parsing state */
685 hw_arg
.shader
= iface
;
686 hw_arg
.buffer
= buffer
;
687 hw_arg
.reg_maps
= reg_maps
;
688 This
->baseShader
.parse_state
.current_row
= 0;
690 /* Second pass, process opcodes */
691 if (NULL
!= pToken
) {
692 while (D3DPS_END() != *pToken
) {
694 /* Skip version token */
695 if (shader_is_version_token(*pToken
)) {
700 /* Skip comment tokens */
701 if (shader_is_comment(*pToken
)) {
702 DWORD comment_len
= (*pToken
& D3DSI_COMMENTSIZE_MASK
) >> D3DSI_COMMENTSIZE_SHIFT
;
704 TRACE("#%s\n", (char*)pToken
);
705 pToken
+= comment_len
;
710 hw_arg
.opcode_token
= *pToken
++;
711 curOpcode
= shader_get_opcode(iface
, hw_arg
.opcode_token
);
714 if (curOpcode
== NULL
)
716 else if (This
->baseShader
.shader_mode
== SHADER_GLSL
)
717 hw_fct
= curOpcode
->hw_glsl_fct
;
718 else if (This
->baseShader
.shader_mode
== SHADER_ARB
)
719 hw_fct
= curOpcode
->hw_fct
;
721 /* Unknown opcode and its parameters */
722 if (NULL
== curOpcode
) {
723 FIXME("Unrecognized opcode: token=%08lX\n", hw_arg
.opcode_token
);
724 pToken
+= shader_skip_unrecognized(iface
, pToken
);
727 } else if (D3DSIO_DCL
== curOpcode
->opcode
||
728 D3DSIO_NOP
== curOpcode
->opcode
||
729 D3DSIO_DEF
== curOpcode
->opcode
||
730 D3DSIO_DEFI
== curOpcode
->opcode
||
731 D3DSIO_DEFB
== curOpcode
->opcode
||
732 D3DSIO_PHASE
== curOpcode
->opcode
||
733 D3DSIO_RET
== curOpcode
->opcode
) {
735 pToken
+= shader_skip_opcode(This
, curOpcode
, hw_arg
.opcode_token
);
737 /* If a generator function is set for current shader target, use it */
738 } else if (hw_fct
!= NULL
) {
740 hw_arg
.opcode
= curOpcode
;
742 /* Destination token */
743 if (curOpcode
->dst_token
) {
745 DWORD param
, addr_token
= 0;
746 pToken
+= shader_get_param(iface
, pToken
, ¶m
, &addr_token
);
748 hw_arg
.dst_addr
= addr_token
;
751 /* Predication token */
752 if (hw_arg
.opcode_token
& D3DSHADER_INSTRUCTION_PREDICATED
)
753 hw_arg
.predicate
= *pToken
++;
755 /* Other source tokens */
756 for (i
= 0; i
< (curOpcode
->num_params
- curOpcode
->dst_token
); i
++) {
758 DWORD param
, addr_token
= 0;
759 pToken
+= shader_get_param(iface
, pToken
, ¶m
, &addr_token
);
760 hw_arg
.src
[i
] = param
;
761 hw_arg
.src_addr
[i
] = addr_token
;
764 /* Call appropriate function for output target */
767 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
768 if (This
->baseShader
.shader_mode
== SHADER_GLSL
)
769 shader_glsl_add_instruction_modifiers(&hw_arg
);
771 /* Unhandled opcode */
774 FIXME("Can't handle opcode %s in hwShader\n", curOpcode
->name
);
775 pToken
+= shader_skip_opcode(This
, curOpcode
, hw_arg
.opcode_token
);
778 /* TODO: What about result.depth? */
783 void shader_dump_ins_modifiers(const DWORD output
) {
785 DWORD shift
= (output
& D3DSP_DSTSHIFT_MASK
) >> D3DSP_DSTSHIFT_SHIFT
;
786 DWORD mmask
= output
& D3DSP_DSTMOD_MASK
;
790 case 13: TRACE("_d8"); break;
791 case 14: TRACE("_d4"); break;
792 case 15: TRACE("_d2"); break;
793 case 1: TRACE("_x2"); break;
794 case 2: TRACE("_x4"); break;
795 case 3: TRACE("_x8"); break;
796 default: TRACE("_unhandled_shift(%ld)", shift
); break;
799 if (mmask
& D3DSPDM_SATURATE
) TRACE("_sat");
800 if (mmask
& D3DSPDM_PARTIALPRECISION
) TRACE("_pp");
801 if (mmask
& D3DSPDM_MSAMPCENTROID
) TRACE("_centroid");
803 mmask
&= ~(D3DSPDM_SATURATE
| D3DSPDM_PARTIALPRECISION
| D3DSPDM_MSAMPCENTROID
);
805 FIXME("_unrecognized_modifier(%#lx)", mmask
>> D3DSP_DSTMOD_SHIFT
);
808 /* First pass: trace shader, initialize length and version */
809 void shader_trace_init(
810 IWineD3DBaseShader
*iface
,
811 const DWORD
* pFunction
) {
813 IWineD3DBaseShaderImpl
*This
=(IWineD3DBaseShaderImpl
*)iface
;
815 const DWORD
* pToken
= pFunction
;
816 const SHADER_OPCODE
* curOpcode
= NULL
;
818 unsigned int len
= 0;
821 TRACE("(%p) : Parsing programme\n", This
);
823 if (NULL
!= pToken
) {
824 while (D3DVS_END() != *pToken
) {
825 if (shader_is_version_token(*pToken
)) { /** version */
826 This
->baseShader
.hex_version
= *pToken
;
827 TRACE("%s_%lu_%lu\n", shader_is_pshader_version(This
->baseShader
.hex_version
)? "ps": "vs",
828 D3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
),
829 D3DSHADER_VERSION_MINOR(This
->baseShader
.hex_version
));
834 if (shader_is_comment(*pToken
)) { /** comment */
835 DWORD comment_len
= (*pToken
& D3DSI_COMMENTSIZE_MASK
) >> D3DSI_COMMENTSIZE_SHIFT
;
837 TRACE("//%s\n", (char*)pToken
);
838 pToken
+= comment_len
;
839 len
+= comment_len
+ 1;
842 opcode_token
= *pToken
++;
843 curOpcode
= shader_get_opcode(iface
, opcode_token
);
846 if (NULL
== curOpcode
) {
848 FIXME("Unrecognized opcode: token=%08lX\n", opcode_token
);
849 tokens_read
= shader_skip_unrecognized(iface
, pToken
);
850 pToken
+= tokens_read
;
854 if (curOpcode
->opcode
== D3DSIO_DCL
) {
856 DWORD usage
= *pToken
;
857 DWORD param
= *(pToken
+ 1);
859 shader_dump_decl_usage(This
, usage
, param
);
860 shader_dump_ins_modifiers(param
);
862 shader_dump_param(iface
, param
, 0, 0);
866 } else if (curOpcode
->opcode
== D3DSIO_DEF
) {
868 unsigned int offset
= shader_get_float_offset(*pToken
);
870 TRACE("def c%u = %f, %f, %f, %f", offset
,
871 *(float *)(pToken
+ 1),
872 *(float *)(pToken
+ 2),
873 *(float *)(pToken
+ 3),
874 *(float *)(pToken
+ 4));
878 } else if (curOpcode
->opcode
== D3DSIO_DEFI
) {
880 TRACE("defi i%lu = %ld, %ld, %ld, %ld", *pToken
& D3DSP_REGNUM_MASK
,
881 (long) *(pToken
+ 1),
882 (long) *(pToken
+ 2),
883 (long) *(pToken
+ 3),
884 (long) *(pToken
+ 4));
889 } else if (curOpcode
->opcode
== D3DSIO_DEFB
) {
891 TRACE("defb b%lu = %s", *pToken
& D3DSP_REGNUM_MASK
,
892 *(pToken
+ 1)? "true": "false");
899 DWORD param
, addr_token
;
902 /* Print out predication source token first - it follows
903 * the destination token. */
904 if (opcode_token
& D3DSHADER_INSTRUCTION_PREDICATED
) {
906 shader_dump_param(iface
, *(pToken
+ 2), 0, 1);
910 TRACE("%s", curOpcode
->name
);
912 if (curOpcode
->opcode
== D3DSIO_IFC
||
913 curOpcode
->opcode
== D3DSIO_BREAKC
) {
915 DWORD op
= (opcode_token
& INST_CONTROLS_MASK
) >> INST_CONTROLS_SHIFT
;
917 case COMPARISON_GT
: TRACE("_gt"); break;
918 case COMPARISON_EQ
: TRACE("_eq"); break;
919 case COMPARISON_GE
: TRACE("_ge"); break;
920 case COMPARISON_LT
: TRACE("_lt"); break;
921 case COMPARISON_NE
: TRACE("_ne"); break;
922 case COMPARISON_LE
: TRACE("_le"); break;
928 /* Destination token */
929 if (curOpcode
->dst_token
) {
931 /* Destination token */
932 tokens_read
= shader_get_param(iface
, pToken
, ¶m
, &addr_token
);
933 pToken
+= tokens_read
;
936 shader_dump_ins_modifiers(param
);
938 shader_dump_param(iface
, param
, addr_token
, 0);
941 /* Predication token - already printed out, just skip it */
942 if (opcode_token
& D3DSHADER_INSTRUCTION_PREDICATED
) {
947 /* Other source tokens */
948 for (i
= curOpcode
->dst_token
; i
< curOpcode
->num_params
; ++i
) {
950 tokens_read
= shader_get_param(iface
, pToken
, ¶m
, &addr_token
);
951 pToken
+= tokens_read
;
954 TRACE((i
== 0)? " " : ", ");
955 shader_dump_param(iface
, param
, addr_token
, 1);
961 This
->baseShader
.functionLength
= (len
+ 1) * sizeof(DWORD
);
963 This
->baseShader
.functionLength
= 1; /* no Function defined use fixed function vertex processing */
967 void shader_delete_constant_list(
968 struct list
* clist
) {
971 struct local_constant
* constant
;
973 ptr
= list_head(clist
);
975 constant
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
976 ptr
= list_next(clist
, ptr
);
977 HeapFree(GetProcessHeap(), 0, constant
);