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
& WINED3DSI_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 %#x(%d) masked %#x, shader version %#x\n",
85 code
, code
, code
& WINED3DSI_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
= WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 2 &&
105 ((*pToken
& WINED3DSHADER_ADDRESSMODE_MASK
) == WINED3DSHADER_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 (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 2)?
122 ((opcode_token
& WINED3DSI_INSTLENGTH_MASK
) >> WINED3DSI_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=%08x "
148 "addr_token=%08x 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
& WINED3DSP_REGNUM_MASK
;
162 int regtype
= shader_get_regtype(reg
);
165 case WINED3DSPR_CONST
: return regnum
;
166 case WINED3DSPR_CONST2
: return 2048 + regnum
;
167 case WINED3DSPR_CONST3
: return 4096 + regnum
;
168 case WINED3DSPR_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 (WINED3DVS_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
& WINED3DSI_COMMENTSIZE_MASK
) >> WINED3DSI_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 (WINED3DSIO_DCL
== curOpcode
->opcode
) {
223 DWORD usage
= *pToken
++;
224 DWORD param
= *pToken
++;
225 DWORD regtype
= shader_get_regtype(param
);
226 unsigned int regnum
= param
& WINED3DSP_REGNUM_MASK
;
228 /* Vshader: mark attributes used
229 Pshader: mark 3.0 input registers used, save token */
230 if (WINED3DSPR_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 (WINED3DSPR_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 (WINED3DSPR_SAMPLER
== regtype
)
248 reg_maps
->samplers
[regnum
] = usage
;
250 } else if (WINED3DSIO_DEF
== curOpcode
->opcode
) {
252 local_constant
* lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
253 if (!lconst
) return E_OUTOFMEMORY
;
254 lconst
->idx
= *pToken
& WINED3DSP_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 (WINED3DSIO_DEFI
== curOpcode
->opcode
) {
261 local_constant
* lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
262 if (!lconst
) return E_OUTOFMEMORY
;
263 lconst
->idx
= *pToken
& WINED3DSP_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 (WINED3DSIO_DEFB
== curOpcode
->opcode
) {
270 local_constant
* lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
271 if (!lconst
) return E_OUTOFMEMORY
;
272 lconst
->idx
= *pToken
& WINED3DSP_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 (WINED3DSIO_LOOP
== curOpcode
->opcode
||
279 WINED3DSIO_REP
== curOpcode
->opcode
) {
281 pToken
+= curOpcode
->num_params
;
283 /* For subroutine prototypes */
284 } else if (WINED3DSIO_LABEL
== curOpcode
->opcode
) {
286 DWORD snum
= *pToken
& WINED3DSP_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 (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) == 1 &&
296 (WINED3DSIO_TEX
== curOpcode
->opcode
||
297 WINED3DSIO_TEXBEM
== curOpcode
->opcode
||
298 WINED3DSIO_TEXBEML
== curOpcode
->opcode
||
299 WINED3DSIO_TEXDP3TEX
== curOpcode
->opcode
||
300 WINED3DSIO_TEXM3x2TEX
== curOpcode
->opcode
||
301 WINED3DSIO_TEXM3x3SPEC
== curOpcode
->opcode
||
302 WINED3DSIO_TEXM3x3TEX
== curOpcode
->opcode
||
303 WINED3DSIO_TEXM3x3VSPEC
== curOpcode
->opcode
||
304 WINED3DSIO_TEXREG2AR
== curOpcode
->opcode
||
305 WINED3DSIO_TEXREG2GB
== curOpcode
->opcode
||
306 WINED3DSIO_TEXREG2RGB
== curOpcode
->opcode
)) {
308 /* Fake sampler usage, only set reserved bit and ttype */
309 DWORD sampler_code
= *pToken
& WINED3DSP_REGNUM_MASK
;
311 if(!stateBlock
->textures
[sampler_code
]) {
312 ERR("No texture bound to sampler %d\n", sampler_code
);
313 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_2D
;
315 int texType
= IWineD3DBaseTexture_GetTextureDimensions(stateBlock
->textures
[sampler_code
]);
318 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_1D
;
322 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_2D
;
326 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_VOLUME
;
329 case GL_TEXTURE_CUBE_MAP_ARB
:
330 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_CUBE
;
334 ERR("Unexpected gl texture type found: %d\n", texType
);
335 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_2D
;
340 /* This will loop over all the registers and try to
341 * make a bitmask of the ones we're interested in.
343 * Relative addressing tokens are ignored, but that's
344 * okay, since we'll catch any address registers when
345 * they are initialized (required by spec) */
347 limit
= (opcode_token
& WINED3DSHADER_INSTRUCTION_PREDICATED
)?
348 curOpcode
->num_params
+ 1: curOpcode
->num_params
;
350 for (i
= 0; i
< limit
; ++i
) {
352 DWORD param
, addr_token
, reg
, regtype
;
353 pToken
+= shader_get_param(iface
, pToken
, ¶m
, &addr_token
);
355 regtype
= shader_get_regtype(param
);
356 reg
= param
& WINED3DSP_REGNUM_MASK
;
358 if (WINED3DSPR_TEXTURE
== regtype
) { /* vs: WINED3DSPR_ADDR */
361 reg_maps
->texcoord
[reg
] = 1;
363 reg_maps
->address
[reg
] = 1;
366 else if (WINED3DSPR_TEMP
== regtype
)
367 reg_maps
->temporary
[reg
] = 1;
369 else if (WINED3DSPR_INPUT
== regtype
&& !pshader
)
370 reg_maps
->attributes
[reg
] = 1;
372 else if (WINED3DSPR_RASTOUT
== regtype
&& reg
== 1)
381 static void shader_dump_decl_usage(
382 IWineD3DBaseShaderImpl
* This
,
386 DWORD regtype
= shader_get_regtype(param
);
390 if (regtype
== WINED3DSPR_SAMPLER
) {
391 DWORD ttype
= decl
& WINED3DSP_TEXTURETYPE_MASK
;
394 case WINED3DSTT_2D
: TRACE("_2d"); break;
395 case WINED3DSTT_CUBE
: TRACE("_cube"); break;
396 case WINED3DSTT_VOLUME
: TRACE("_volume"); break;
397 default: TRACE("_unknown_ttype(%08x)", ttype
);
402 DWORD usage
= decl
& WINED3DSP_DCL_USAGE_MASK
;
403 DWORD idx
= (decl
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
405 /* Pixel shaders 3.0 don't have usage semantics */
406 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
407 if (pshader
&& This
->baseShader
.hex_version
< WINED3DPS_VERSION(3,0))
413 case D3DDECLUSAGE_POSITION
:
414 TRACE("position%d", idx
);
416 case D3DDECLUSAGE_BLENDINDICES
:
419 case D3DDECLUSAGE_BLENDWEIGHT
:
422 case D3DDECLUSAGE_NORMAL
:
423 TRACE("normal%d", idx
);
425 case D3DDECLUSAGE_PSIZE
:
428 case D3DDECLUSAGE_COLOR
:
432 TRACE("specular%d", (idx
- 1));
435 case D3DDECLUSAGE_TEXCOORD
:
436 TRACE("texture%d", idx
);
438 case D3DDECLUSAGE_TANGENT
:
441 case D3DDECLUSAGE_BINORMAL
:
444 case D3DDECLUSAGE_TESSFACTOR
:
447 case D3DDECLUSAGE_POSITIONT
:
448 TRACE("positionT%d", idx
);
450 case D3DDECLUSAGE_FOG
:
453 case D3DDECLUSAGE_DEPTH
:
456 case D3DDECLUSAGE_SAMPLE
:
460 FIXME("unknown_semantics(%08x)", usage
);
465 static void shader_dump_arr_entry(
466 IWineD3DBaseShader
*iface
,
468 const DWORD addr_token
,
473 ((param
& WINED3DSHADER_ADDRESSMODE_MASK
) == WINED3DSHADER_ADDRMODE_RELATIVE
);
478 shader_dump_param(iface
, addr_token
, 0, input
);
488 void shader_dump_param(
489 IWineD3DBaseShader
*iface
,
491 const DWORD addr_token
,
494 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
495 static const char * const rastout_reg_names
[] = { "oPos", "oFog", "oPts" };
496 char swizzle_reg_chars
[4];
498 DWORD reg
= param
& WINED3DSP_REGNUM_MASK
;
499 DWORD regtype
= shader_get_regtype(param
);
500 DWORD modifier
= param
& WINED3DSP_SRCMOD_MASK
;
502 /* There are some minor differences between pixel and vertex shaders */
503 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
505 /* For one, we'd prefer color components to be shown for pshaders.
506 * FIXME: use the swizzle function for this */
508 swizzle_reg_chars
[0] = pshader
? 'r': 'x';
509 swizzle_reg_chars
[1] = pshader
? 'g': 'y';
510 swizzle_reg_chars
[2] = pshader
? 'b': 'z';
511 swizzle_reg_chars
[3] = pshader
? 'a': 'w';
514 if ( (modifier
== WINED3DSPSM_NEG
) ||
515 (modifier
== WINED3DSPSM_BIASNEG
) ||
516 (modifier
== WINED3DSPSM_SIGNNEG
) ||
517 (modifier
== WINED3DSPSM_X2NEG
) ||
518 (modifier
== WINED3DSPSM_ABSNEG
) )
520 else if (modifier
== WINED3DSPSM_COMP
)
522 else if (modifier
== WINED3DSPSM_NOT
)
525 if (modifier
== WINED3DSPSM_ABS
|| modifier
== WINED3DSPSM_ABSNEG
)
530 case WINED3DSPR_TEMP
:
533 case WINED3DSPR_INPUT
:
535 shader_dump_arr_entry(iface
, param
, addr_token
, reg
, input
);
537 case WINED3DSPR_CONST
:
538 case WINED3DSPR_CONST2
:
539 case WINED3DSPR_CONST3
:
540 case WINED3DSPR_CONST4
:
542 shader_dump_arr_entry(iface
, param
, addr_token
, shader_get_float_offset(param
), input
);
544 case WINED3DSPR_TEXTURE
: /* vs: case D3DSPR_ADDR */
545 TRACE("%c%u", (pshader
? 't':'a'), reg
);
547 case WINED3DSPR_RASTOUT
:
548 TRACE("%s", rastout_reg_names
[reg
]);
550 case WINED3DSPR_COLOROUT
:
553 case WINED3DSPR_DEPTHOUT
:
556 case WINED3DSPR_ATTROUT
:
559 case WINED3DSPR_TEXCRDOUT
:
561 /* Vertex shaders >= 3.0 use general purpose output registers
562 * (WINED3DSPR_OUTPUT), which can include an address token */
564 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3) {
566 shader_dump_arr_entry(iface
, param
, addr_token
, reg
, input
);
571 case WINED3DSPR_CONSTINT
:
573 shader_dump_arr_entry(iface
, param
, addr_token
, reg
, input
);
575 case WINED3DSPR_CONSTBOOL
:
577 shader_dump_arr_entry(iface
, param
, addr_token
, reg
, input
);
579 case WINED3DSPR_LABEL
:
582 case WINED3DSPR_LOOP
:
585 case WINED3DSPR_SAMPLER
:
588 case WINED3DSPR_PREDICATE
:
592 TRACE("unhandled_rtype(%#x)", regtype
);
597 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
599 if ((param
& WINED3DSP_WRITEMASK_ALL
) != WINED3DSP_WRITEMASK_ALL
) {
601 if (param
& WINED3DSP_WRITEMASK_0
) TRACE("%c", swizzle_reg_chars
[0]);
602 if (param
& WINED3DSP_WRITEMASK_1
) TRACE("%c", swizzle_reg_chars
[1]);
603 if (param
& WINED3DSP_WRITEMASK_2
) TRACE("%c", swizzle_reg_chars
[2]);
604 if (param
& WINED3DSP_WRITEMASK_3
) TRACE("%c", swizzle_reg_chars
[3]);
609 DWORD swizzle
= (param
& WINED3DSP_SWIZZLE_MASK
) >> WINED3DSP_SWIZZLE_SHIFT
;
610 DWORD swizzle_r
= swizzle
& 0x03;
611 DWORD swizzle_g
= (swizzle
>> 2) & 0x03;
612 DWORD swizzle_b
= (swizzle
>> 4) & 0x03;
613 DWORD swizzle_a
= (swizzle
>> 6) & 0x03;
617 case WINED3DSPSM_NONE
: break;
618 case WINED3DSPSM_NEG
: break;
619 case WINED3DSPSM_NOT
: break;
620 case WINED3DSPSM_BIAS
: TRACE("_bias"); break;
621 case WINED3DSPSM_BIASNEG
: TRACE("_bias"); break;
622 case WINED3DSPSM_SIGN
: TRACE("_bx2"); break;
623 case WINED3DSPSM_SIGNNEG
: TRACE("_bx2"); break;
624 case WINED3DSPSM_COMP
: break;
625 case WINED3DSPSM_X2
: TRACE("_x2"); break;
626 case WINED3DSPSM_X2NEG
: TRACE("_x2"); break;
627 case WINED3DSPSM_DZ
: TRACE("_dz"); break;
628 case WINED3DSPSM_DW
: TRACE("_dw"); break;
629 case WINED3DSPSM_ABSNEG
: TRACE(")"); break;
630 case WINED3DSPSM_ABS
: TRACE(")"); break;
632 TRACE("_unknown_modifier(%#x)", modifier
>> WINED3DSP_SRCMOD_SHIFT
);
637 * swizzle bits fields:
640 if ((WINED3DVS_NOSWIZZLE
>> WINED3DVS_SWIZZLE_SHIFT
) != swizzle
) {
641 if (swizzle_r
== swizzle_g
&&
642 swizzle_r
== swizzle_b
&&
643 swizzle_r
== swizzle_a
) {
644 TRACE(".%c", swizzle_reg_chars
[swizzle_r
]);
647 swizzle_reg_chars
[swizzle_r
],
648 swizzle_reg_chars
[swizzle_g
],
649 swizzle_reg_chars
[swizzle_b
],
650 swizzle_reg_chars
[swizzle_a
]);
656 /** Shared code in order to generate the bulk of the shader string.
657 Use the shader_header_fct & shader_footer_fct to add strings
658 that are specific to pixel or vertex functions
659 NOTE: A description of how to parse tokens can be found at:
660 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/graphics/hh/graphics/usermodedisplaydriver_shader_cc8e4e05-f5c3-4ec0-8853-8ce07c1551b2.xml.asp */
661 void shader_generate_main(
662 IWineD3DBaseShader
*iface
,
663 SHADER_BUFFER
* buffer
,
664 shader_reg_maps
* reg_maps
,
665 CONST DWORD
* pFunction
) {
667 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
668 const DWORD
*pToken
= pFunction
;
669 const SHADER_OPCODE
*curOpcode
= NULL
;
670 SHADER_HANDLER hw_fct
= NULL
;
672 SHADER_OPCODE_ARG hw_arg
;
674 /* Initialize current parsing state */
675 hw_arg
.shader
= iface
;
676 hw_arg
.buffer
= buffer
;
677 hw_arg
.reg_maps
= reg_maps
;
678 This
->baseShader
.parse_state
.current_row
= 0;
680 /* Second pass, process opcodes */
681 if (NULL
!= pToken
) {
682 while (WINED3DPS_END() != *pToken
) {
684 /* Skip version token */
685 if (shader_is_version_token(*pToken
)) {
690 /* Skip comment tokens */
691 if (shader_is_comment(*pToken
)) {
692 DWORD comment_len
= (*pToken
& WINED3DSI_COMMENTSIZE_MASK
) >> WINED3DSI_COMMENTSIZE_SHIFT
;
694 TRACE("#%s\n", (const char*)pToken
);
695 pToken
+= comment_len
;
700 hw_arg
.opcode_token
= *pToken
++;
701 curOpcode
= shader_get_opcode(iface
, hw_arg
.opcode_token
);
704 if (curOpcode
== NULL
)
706 else if (This
->baseShader
.shader_mode
== SHADER_GLSL
)
707 hw_fct
= curOpcode
->hw_glsl_fct
;
708 else if (This
->baseShader
.shader_mode
== SHADER_ARB
)
709 hw_fct
= curOpcode
->hw_fct
;
711 /* Unknown opcode and its parameters */
712 if (NULL
== curOpcode
) {
713 FIXME("Unrecognized opcode: token=%08x\n", hw_arg
.opcode_token
);
714 pToken
+= shader_skip_unrecognized(iface
, pToken
);
717 } else if (WINED3DSIO_DCL
== curOpcode
->opcode
||
718 WINED3DSIO_NOP
== curOpcode
->opcode
||
719 WINED3DSIO_DEF
== curOpcode
->opcode
||
720 WINED3DSIO_DEFI
== curOpcode
->opcode
||
721 WINED3DSIO_DEFB
== curOpcode
->opcode
||
722 WINED3DSIO_PHASE
== curOpcode
->opcode
||
723 WINED3DSIO_RET
== curOpcode
->opcode
) {
725 pToken
+= shader_skip_opcode(This
, curOpcode
, hw_arg
.opcode_token
);
727 /* If a generator function is set for current shader target, use it */
728 } else if (hw_fct
!= NULL
) {
730 hw_arg
.opcode
= curOpcode
;
732 /* Destination token */
733 if (curOpcode
->dst_token
) {
735 DWORD param
, addr_token
= 0;
736 pToken
+= shader_get_param(iface
, pToken
, ¶m
, &addr_token
);
738 hw_arg
.dst_addr
= addr_token
;
741 /* Predication token */
742 if (hw_arg
.opcode_token
& WINED3DSHADER_INSTRUCTION_PREDICATED
)
743 hw_arg
.predicate
= *pToken
++;
745 /* Other source tokens */
746 for (i
= 0; i
< (curOpcode
->num_params
- curOpcode
->dst_token
); i
++) {
748 DWORD param
, addr_token
= 0;
749 pToken
+= shader_get_param(iface
, pToken
, ¶m
, &addr_token
);
750 hw_arg
.src
[i
] = param
;
751 hw_arg
.src_addr
[i
] = addr_token
;
754 /* Call appropriate function for output target */
757 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
758 if (This
->baseShader
.shader_mode
== SHADER_GLSL
)
759 shader_glsl_add_instruction_modifiers(&hw_arg
);
761 /* Unhandled opcode */
764 FIXME("Can't handle opcode %s in hwShader\n", curOpcode
->name
);
765 pToken
+= shader_skip_opcode(This
, curOpcode
, hw_arg
.opcode_token
);
768 /* TODO: What about result.depth? */
773 void shader_dump_ins_modifiers(const DWORD output
) {
775 DWORD shift
= (output
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
776 DWORD mmask
= output
& WINED3DSP_DSTMOD_MASK
;
780 case 13: TRACE("_d8"); break;
781 case 14: TRACE("_d4"); break;
782 case 15: TRACE("_d2"); break;
783 case 1: TRACE("_x2"); break;
784 case 2: TRACE("_x4"); break;
785 case 3: TRACE("_x8"); break;
786 default: TRACE("_unhandled_shift(%d)", shift
); break;
789 if (mmask
& WINED3DSPDM_SATURATE
) TRACE("_sat");
790 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) TRACE("_pp");
791 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) TRACE("_centroid");
793 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
795 FIXME("_unrecognized_modifier(%#x)", mmask
>> WINED3DSP_DSTMOD_SHIFT
);
798 /* First pass: trace shader, initialize length and version */
799 void shader_trace_init(
800 IWineD3DBaseShader
*iface
,
801 const DWORD
* pFunction
) {
803 IWineD3DBaseShaderImpl
*This
=(IWineD3DBaseShaderImpl
*)iface
;
805 const DWORD
* pToken
= pFunction
;
806 const SHADER_OPCODE
* curOpcode
= NULL
;
808 unsigned int len
= 0;
811 TRACE("(%p) : Parsing programme\n", This
);
813 if (NULL
!= pToken
) {
814 while (WINED3DVS_END() != *pToken
) {
815 if (shader_is_version_token(*pToken
)) { /** version */
816 This
->baseShader
.hex_version
= *pToken
;
817 TRACE("%s_%u_%u\n", shader_is_pshader_version(This
->baseShader
.hex_version
)? "ps": "vs",
818 WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
),
819 WINED3DSHADER_VERSION_MINOR(This
->baseShader
.hex_version
));
824 if (shader_is_comment(*pToken
)) { /** comment */
825 DWORD comment_len
= (*pToken
& WINED3DSI_COMMENTSIZE_MASK
) >> WINED3DSI_COMMENTSIZE_SHIFT
;
827 TRACE("//%s\n", (const char*)pToken
);
828 pToken
+= comment_len
;
829 len
+= comment_len
+ 1;
832 opcode_token
= *pToken
++;
833 curOpcode
= shader_get_opcode(iface
, opcode_token
);
836 if (NULL
== curOpcode
) {
838 FIXME("Unrecognized opcode: token=%08x\n", opcode_token
);
839 tokens_read
= shader_skip_unrecognized(iface
, pToken
);
840 pToken
+= tokens_read
;
844 if (curOpcode
->opcode
== WINED3DSIO_DCL
) {
846 DWORD usage
= *pToken
;
847 DWORD param
= *(pToken
+ 1);
849 shader_dump_decl_usage(This
, usage
, param
);
850 shader_dump_ins_modifiers(param
);
852 shader_dump_param(iface
, param
, 0, 0);
856 } else if (curOpcode
->opcode
== WINED3DSIO_DEF
) {
858 unsigned int offset
= shader_get_float_offset(*pToken
);
860 TRACE("def c%u = %f, %f, %f, %f", offset
,
861 *(const float *)(pToken
+ 1),
862 *(const float *)(pToken
+ 2),
863 *(const float *)(pToken
+ 3),
864 *(const float *)(pToken
+ 4));
868 } else if (curOpcode
->opcode
== WINED3DSIO_DEFI
) {
870 TRACE("defi i%u = %d, %d, %d, %d", *pToken
& WINED3DSP_REGNUM_MASK
,
879 } else if (curOpcode
->opcode
== WINED3DSIO_DEFB
) {
881 TRACE("defb b%u = %s", *pToken
& WINED3DSP_REGNUM_MASK
,
882 *(pToken
+ 1)? "true": "false");
889 DWORD param
, addr_token
;
892 /* Print out predication source token first - it follows
893 * the destination token. */
894 if (opcode_token
& WINED3DSHADER_INSTRUCTION_PREDICATED
) {
896 shader_dump_param(iface
, *(pToken
+ 2), 0, 1);
900 TRACE("%s", curOpcode
->name
);
902 if (curOpcode
->opcode
== WINED3DSIO_IFC
||
903 curOpcode
->opcode
== WINED3DSIO_BREAKC
) {
905 DWORD op
= (opcode_token
& INST_CONTROLS_MASK
) >> INST_CONTROLS_SHIFT
;
907 case COMPARISON_GT
: TRACE("_gt"); break;
908 case COMPARISON_EQ
: TRACE("_eq"); break;
909 case COMPARISON_GE
: TRACE("_ge"); break;
910 case COMPARISON_LT
: TRACE("_lt"); break;
911 case COMPARISON_NE
: TRACE("_ne"); break;
912 case COMPARISON_LE
: TRACE("_le"); break;
918 /* Destination token */
919 if (curOpcode
->dst_token
) {
921 /* Destination token */
922 tokens_read
= shader_get_param(iface
, pToken
, ¶m
, &addr_token
);
923 pToken
+= tokens_read
;
926 shader_dump_ins_modifiers(param
);
928 shader_dump_param(iface
, param
, addr_token
, 0);
931 /* Predication token - already printed out, just skip it */
932 if (opcode_token
& WINED3DSHADER_INSTRUCTION_PREDICATED
) {
937 /* Other source tokens */
938 for (i
= curOpcode
->dst_token
; i
< curOpcode
->num_params
; ++i
) {
940 tokens_read
= shader_get_param(iface
, pToken
, ¶m
, &addr_token
);
941 pToken
+= tokens_read
;
944 TRACE((i
== 0)? " " : ", ");
945 shader_dump_param(iface
, param
, addr_token
, 1);
951 This
->baseShader
.functionLength
= (len
+ 1) * sizeof(DWORD
);
953 This
->baseShader
.functionLength
= 1; /* no Function defined use fixed function vertex processing */
957 void shader_delete_constant_list(
958 struct list
* clist
) {
961 struct local_constant
* constant
;
963 ptr
= list_head(clist
);
965 constant
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
966 ptr
= list_next(clist
, ptr
);
967 HeapFree(GetProcessHeap(), 0, constant
);
971 static void shader_none_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {}
972 static void shader_none_select_depth_blt(IWineD3DDevice
*iface
) {}
973 static void shader_none_load_constants(IWineD3DDevice
*iface
, char usePS
, char useVS
) {}
974 static void shader_none_cleanup(BOOL usePS
, BOOL useVS
) {}
976 const shader_backend_t none_shader_backend
= {
978 &shader_none_select_depth_blt
,
979 &shader_none_load_constants
,