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
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wined3d_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
32 WINE_DECLARE_DEBUG_CHANNEL(d3d
);
34 static void shader_dump_param(const DWORD param
, const DWORD addr_token
, int input
, DWORD shader_version
);
36 static inline BOOL
shader_is_version_token(DWORD token
) {
37 return shader_is_pshader_version(token
) ||
38 shader_is_vshader_version(token
);
41 void shader_buffer_init(struct SHADER_BUFFER
*buffer
)
43 buffer
->buffer
= HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE
);
44 buffer
->buffer
[0] = '\0';
47 buffer
->newline
= TRUE
;
50 void shader_buffer_free(struct SHADER_BUFFER
*buffer
)
52 HeapFree(GetProcessHeap(), 0, buffer
->buffer
);
55 int shader_vaddline(SHADER_BUFFER
* buffer
, const char *format
, va_list args
)
57 char* base
= buffer
->buffer
+ buffer
->bsize
;
60 rc
= vsnprintf(base
, SHADER_PGMSIZE
- 1 - buffer
->bsize
, format
, args
);
62 if (rc
< 0 || /* C89 */
63 rc
> SHADER_PGMSIZE
- 1 - buffer
->bsize
) { /* C99 */
65 ERR("The buffer allocated for the shader program string "
66 "is too small at %d bytes.\n", SHADER_PGMSIZE
);
67 buffer
->bsize
= SHADER_PGMSIZE
- 1;
71 if (buffer
->newline
) {
72 TRACE("GL HW (%u, %u) : %s", buffer
->lineNo
+ 1, buffer
->bsize
, base
);
73 buffer
->newline
= FALSE
;
79 if (buffer
->buffer
[buffer
->bsize
-1] == '\n') {
81 buffer
->newline
= TRUE
;
86 int shader_addline(SHADER_BUFFER
* buffer
, const char *format
, ...)
91 va_start(args
, format
);
92 ret
= shader_vaddline(buffer
, format
, args
);
98 void shader_init(struct IWineD3DBaseShaderClass
*shader
,
99 IWineD3DDevice
*device
, const SHADER_OPCODE
*instruction_table
)
102 shader
->device
= device
;
103 shader
->shader_ins
= instruction_table
;
104 list_init(&shader
->linked_programs
);
107 const SHADER_OPCODE
*shader_get_opcode(const SHADER_OPCODE
*opcode_table
, DWORD shader_version
, DWORD code
)
111 /** TODO: use dichotomic search */
112 while (opcode_table
[i
].name
)
114 if ((code
& WINED3DSI_OPCODE_MASK
) == opcode_table
[i
].opcode
115 && shader_version
>= opcode_table
[i
].min_version
116 && (!opcode_table
[i
].max_version
|| shader_version
<= opcode_table
[i
].max_version
))
118 return &opcode_table
[i
];
123 FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n",
124 code
, code
, code
& WINED3DSI_OPCODE_MASK
, shader_version
);
129 /* Read a parameter opcode from the input stream,
130 * and possibly a relative addressing token.
131 * Return the number of tokens read */
132 static int shader_get_param(const DWORD
*pToken
, DWORD shader_version
, DWORD
*param
, DWORD
*addr_token
)
134 /* PS >= 3.0 have relative addressing (with token)
135 * VS >= 2.0 have relative addressing (with token)
136 * VS >= 1.0 < 2.0 have relative addressing (without token)
137 * The version check below should work in general */
139 char rel_token
= WINED3DSHADER_VERSION_MAJOR(shader_version
) >= 2 &&
140 ((*pToken
& WINED3DSHADER_ADDRESSMODE_MASK
) == WINED3DSHADER_ADDRMODE_RELATIVE
);
143 *addr_token
= rel_token
? *(pToken
+ 1): 0;
144 return rel_token
? 2:1;
147 /* Return the number of parameters to skip for an opcode */
148 static inline int shader_skip_opcode(const SHADER_OPCODE
*curOpcode
, DWORD opcode_token
, DWORD shader_version
)
150 /* Shaders >= 2.0 may contain address tokens, but fortunately they
151 * have a useful length mask - use it here. Shaders 1.0 contain no such tokens */
152 return (WINED3DSHADER_VERSION_MAJOR(shader_version
) >= 2)
153 ? ((opcode_token
& WINED3DSI_INSTLENGTH_MASK
) >> WINED3DSI_INSTLENGTH_SHIFT
) : curOpcode
->num_params
;
156 /* Read the parameters of an unrecognized opcode from the input stream
157 * Return the number of tokens read.
159 * Note: This function assumes source or destination token format.
160 * It will not work with specially-formatted tokens like DEF or DCL,
161 * but hopefully those would be recognized */
162 static int shader_skip_unrecognized(const DWORD
*pToken
, DWORD shader_version
)
167 /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
168 while (*pToken
& 0x80000000) {
170 DWORD param
, addr_token
;
171 tokens_read
+= shader_get_param(pToken
, shader_version
, ¶m
, &addr_token
);
172 pToken
+= tokens_read
;
174 FIXME("Unrecognized opcode param: token=0x%08x "
175 "addr_token=0x%08x name=", param
, addr_token
);
176 shader_dump_param(param
, addr_token
, i
, shader_version
);
183 /* Convert floating point offset relative
184 * to a register file to an absolute offset for float constants */
185 static unsigned int shader_get_float_offset(const DWORD reg
)
187 unsigned int regnum
= reg
& WINED3DSP_REGNUM_MASK
;
188 int regtype
= shader_get_regtype(reg
);
191 case WINED3DSPR_CONST
: return regnum
;
192 case WINED3DSPR_CONST2
: return 2048 + regnum
;
193 case WINED3DSPR_CONST3
: return 4096 + regnum
;
194 case WINED3DSPR_CONST4
: return 6144 + regnum
;
196 FIXME("Unsupported register type: %d\n", regtype
);
201 static void shader_delete_constant_list(struct list
* clist
) {
204 struct local_constant
* constant
;
206 ptr
= list_head(clist
);
208 constant
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
209 ptr
= list_next(clist
, ptr
);
210 HeapFree(GetProcessHeap(), 0, constant
);
215 static void shader_parse_dst_param(DWORD param
, DWORD addr_param
, struct wined3d_shader_dst_param
*dst
)
217 dst
->register_type
= ((param
& WINED3DSP_REGTYPE_MASK
) >> WINED3DSP_REGTYPE_SHIFT
)
218 | ((param
& WINED3DSP_REGTYPE_MASK2
) >> WINED3DSP_REGTYPE_SHIFT2
);
219 dst
->register_idx
= param
& WINED3DSP_REGNUM_MASK
;
220 dst
->write_mask
= param
& WINED3DSP_WRITEMASK_ALL
;
221 dst
->modifiers
= param
& WINED3DSP_DSTMOD_MASK
;
222 dst
->shift
= (param
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
223 dst
->has_rel_addr
= param
& WINED3DSHADER_ADDRMODE_RELATIVE
;
224 dst
->addr_token
= addr_param
;
227 /* Note that this does not count the loop register
228 * as an address register. */
230 HRESULT
shader_get_registers_used(IWineD3DBaseShader
*iface
, struct shader_reg_maps
*reg_maps
,
231 struct wined3d_shader_semantic
*semantics_in
, struct wined3d_shader_semantic
*semantics_out
,
232 const DWORD
*byte_code
)
234 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
235 const SHADER_OPCODE
*shader_ins
= This
->baseShader
.shader_ins
;
236 DWORD shader_version
;
237 unsigned int cur_loop_depth
= 0, max_loop_depth
= 0;
238 const DWORD
* pToken
= byte_code
;
241 /* There are some minor differences between pixel and vertex shaders */
243 memset(reg_maps
->bumpmat
, 0, sizeof(reg_maps
->bumpmat
));
244 memset(reg_maps
->luminanceparams
, 0, sizeof(reg_maps
->luminanceparams
));
246 /* get_registers_used is called on every compile on some 1.x shaders, which can result
247 * in stacking up a collection of local constants. Delete the old constants if existing
249 shader_delete_constant_list(&This
->baseShader
.constantsF
);
250 shader_delete_constant_list(&This
->baseShader
.constantsB
);
251 shader_delete_constant_list(&This
->baseShader
.constantsI
);
253 /* The version token is supposed to be the first token */
254 if (!shader_is_version_token(*pToken
))
256 FIXME("First token is not a version token, invalid shader.\n");
257 return WINED3DERR_INVALIDCALL
;
259 reg_maps
->shader_version
= shader_version
= *pToken
++;
260 pshader
= shader_is_pshader_version(shader_version
);
262 while (WINED3DVS_END() != *pToken
) {
263 CONST SHADER_OPCODE
* curOpcode
;
267 if (shader_is_comment(*pToken
))
269 DWORD comment_len
= (*pToken
& WINED3DSI_COMMENTSIZE_MASK
) >> WINED3DSI_COMMENTSIZE_SHIFT
;
271 pToken
+= comment_len
;
276 opcode_token
= *pToken
++;
277 curOpcode
= shader_get_opcode(shader_ins
, shader_version
, opcode_token
);
279 /* Unhandled opcode, and its parameters */
280 if (NULL
== curOpcode
) {
281 while (*pToken
& 0x80000000)
284 /* Handle declarations */
285 } else if (WINED3DSIO_DCL
== curOpcode
->opcode
) {
287 DWORD usage
= *pToken
++;
288 DWORD param
= *pToken
++;
289 DWORD regtype
= shader_get_regtype(param
);
290 unsigned int regnum
= param
& WINED3DSP_REGNUM_MASK
;
292 /* Vshader: mark attributes used
293 Pshader: mark 3.0 input registers used, save token */
294 if (WINED3DSPR_INPUT
== regtype
) {
297 reg_maps
->attributes
[regnum
] = 1;
299 reg_maps
->packed_input
[regnum
] = 1;
301 semantics_in
[regnum
].usage
= (usage
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
302 semantics_in
[regnum
].usage_idx
=
303 (usage
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
304 shader_parse_dst_param(param
, 0, &semantics_in
[regnum
].reg
);
306 /* Vshader: mark 3.0 output registers used, save token */
307 } else if (WINED3DSPR_OUTPUT
== regtype
) {
308 reg_maps
->packed_output
[regnum
] = 1;
309 semantics_out
[regnum
].usage
= (usage
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
310 semantics_out
[regnum
].usage_idx
=
311 (usage
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
312 shader_parse_dst_param(param
, 0, &semantics_out
[regnum
].reg
);
314 if (usage
& (WINED3DDECLUSAGE_FOG
<< WINED3DSP_DCL_USAGE_SHIFT
))
317 /* Save sampler usage token */
318 } else if (WINED3DSPR_SAMPLER
== regtype
)
319 reg_maps
->samplers
[regnum
] = usage
;
321 } else if (WINED3DSIO_DEF
== curOpcode
->opcode
) {
323 local_constant
* lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
324 if (!lconst
) return E_OUTOFMEMORY
;
325 lconst
->idx
= *pToken
& WINED3DSP_REGNUM_MASK
;
326 memcpy(lconst
->value
, pToken
+ 1, 4 * sizeof(DWORD
));
328 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
329 if (WINED3DSHADER_VERSION_MAJOR(shader_version
) == 1 && pshader
)
331 float *value
= (float *) lconst
->value
;
332 if(value
[0] < -1.0) value
[0] = -1.0;
333 else if(value
[0] > 1.0) value
[0] = 1.0;
334 if(value
[1] < -1.0) value
[1] = -1.0;
335 else if(value
[1] > 1.0) value
[1] = 1.0;
336 if(value
[2] < -1.0) value
[2] = -1.0;
337 else if(value
[2] > 1.0) value
[2] = 1.0;
338 if(value
[3] < -1.0) value
[3] = -1.0;
339 else if(value
[3] > 1.0) value
[3] = 1.0;
342 list_add_head(&This
->baseShader
.constantsF
, &lconst
->entry
);
343 pToken
+= curOpcode
->num_params
;
345 } else if (WINED3DSIO_DEFI
== curOpcode
->opcode
) {
347 local_constant
* lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
348 if (!lconst
) return E_OUTOFMEMORY
;
349 lconst
->idx
= *pToken
& WINED3DSP_REGNUM_MASK
;
350 memcpy(lconst
->value
, pToken
+ 1, 4 * sizeof(DWORD
));
351 list_add_head(&This
->baseShader
.constantsI
, &lconst
->entry
);
352 pToken
+= curOpcode
->num_params
;
354 } else if (WINED3DSIO_DEFB
== curOpcode
->opcode
) {
356 local_constant
* lconst
= HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant
));
357 if (!lconst
) return E_OUTOFMEMORY
;
358 lconst
->idx
= *pToken
& WINED3DSP_REGNUM_MASK
;
359 memcpy(lconst
->value
, pToken
+ 1, 1 * sizeof(DWORD
));
360 list_add_head(&This
->baseShader
.constantsB
, &lconst
->entry
);
361 pToken
+= curOpcode
->num_params
;
363 /* If there's a loop in the shader */
364 } else if (WINED3DSIO_LOOP
== curOpcode
->opcode
||
365 WINED3DSIO_REP
== curOpcode
->opcode
) {
367 if(cur_loop_depth
> max_loop_depth
)
368 max_loop_depth
= cur_loop_depth
;
369 pToken
+= curOpcode
->num_params
;
371 /* Rep and Loop always use an integer constant for the control parameters */
372 This
->baseShader
.uses_int_consts
= TRUE
;
373 } else if (WINED3DSIO_ENDLOOP
== curOpcode
->opcode
||
374 WINED3DSIO_ENDREP
== curOpcode
->opcode
) {
377 /* For subroutine prototypes */
378 } else if (WINED3DSIO_LABEL
== curOpcode
->opcode
) {
380 DWORD snum
= *pToken
& WINED3DSP_REGNUM_MASK
;
381 reg_maps
->labels
[snum
] = 1;
382 pToken
+= curOpcode
->num_params
;
384 /* Set texture, address, temporary registers */
388 /* Declare 1.X samplers implicitly, based on the destination reg. number */
389 if (WINED3DSHADER_VERSION_MAJOR(shader_version
) == 1
390 && pshader
/* Filter different instructions with the same enum values in VS */
391 && (WINED3DSIO_TEX
== curOpcode
->opcode
392 || WINED3DSIO_TEXBEM
== curOpcode
->opcode
393 || WINED3DSIO_TEXBEML
== curOpcode
->opcode
394 || WINED3DSIO_TEXDP3TEX
== curOpcode
->opcode
395 || WINED3DSIO_TEXM3x2TEX
== curOpcode
->opcode
396 || WINED3DSIO_TEXM3x3SPEC
== curOpcode
->opcode
397 || WINED3DSIO_TEXM3x3TEX
== curOpcode
->opcode
398 || WINED3DSIO_TEXM3x3VSPEC
== curOpcode
->opcode
399 || WINED3DSIO_TEXREG2AR
== curOpcode
->opcode
400 || WINED3DSIO_TEXREG2GB
== curOpcode
->opcode
401 || WINED3DSIO_TEXREG2RGB
== curOpcode
->opcode
))
403 /* Fake sampler usage, only set reserved bit and ttype */
404 DWORD sampler_code
= *pToken
& WINED3DSP_REGNUM_MASK
;
406 TRACE("Setting fake 2D sampler for 1.x pixelshader\n");
407 reg_maps
->samplers
[sampler_code
] = (0x1 << 31) | WINED3DSTT_2D
;
409 /* texbem is only valid with < 1.4 pixel shaders */
410 if(WINED3DSIO_TEXBEM
== curOpcode
->opcode
||
411 WINED3DSIO_TEXBEML
== curOpcode
->opcode
) {
412 reg_maps
->bumpmat
[sampler_code
] = TRUE
;
413 if(WINED3DSIO_TEXBEML
== curOpcode
->opcode
) {
414 reg_maps
->luminanceparams
[sampler_code
] = TRUE
;
418 if(WINED3DSIO_NRM
== curOpcode
->opcode
) {
419 reg_maps
->usesnrm
= 1;
420 } else if(WINED3DSIO_BEM
== curOpcode
->opcode
&& pshader
) {
421 DWORD regnum
= *pToken
& WINED3DSP_REGNUM_MASK
;
422 reg_maps
->bumpmat
[regnum
] = TRUE
;
423 } else if(WINED3DSIO_DSY
== curOpcode
->opcode
) {
424 reg_maps
->usesdsy
= 1;
427 /* This will loop over all the registers and try to
428 * make a bitmask of the ones we're interested in.
430 * Relative addressing tokens are ignored, but that's
431 * okay, since we'll catch any address registers when
432 * they are initialized (required by spec) */
434 limit
= (opcode_token
& WINED3DSHADER_INSTRUCTION_PREDICATED
)?
435 curOpcode
->num_params
+ 1: curOpcode
->num_params
;
437 for (i
= 0; i
< limit
; ++i
) {
439 DWORD param
, addr_token
, reg
, regtype
;
440 pToken
+= shader_get_param(pToken
, shader_version
, ¶m
, &addr_token
);
442 regtype
= shader_get_regtype(param
);
443 reg
= param
& WINED3DSP_REGNUM_MASK
;
445 if (WINED3DSPR_TEXTURE
== regtype
) { /* vs: WINED3DSPR_ADDR */
448 reg_maps
->texcoord
[reg
] = 1;
450 reg_maps
->address
[reg
] = 1;
453 else if (WINED3DSPR_TEMP
== regtype
)
454 reg_maps
->temporary
[reg
] = 1;
456 else if (WINED3DSPR_INPUT
== regtype
) {
458 reg_maps
->attributes
[reg
] = 1;
460 if(param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
461 /* If relative addressing is used, we must assume that all registers
462 * are used. Even if it is a construct like v3[aL], we can't assume
463 * that v0, v1 and v2 aren't read because aL can be negative
466 for(i
= 0; i
< MAX_REG_INPUT
; i
++) {
467 ((IWineD3DPixelShaderImpl
*) This
)->input_reg_used
[i
] = TRUE
;
470 ((IWineD3DPixelShaderImpl
*) This
)->input_reg_used
[reg
] = TRUE
;
475 else if (WINED3DSPR_RASTOUT
== regtype
&& reg
== 1)
478 else if (WINED3DSPR_MISCTYPE
== regtype
&& reg
== 0 && pshader
)
481 else if(WINED3DSPR_CONST
== regtype
) {
482 if(param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
484 if(reg
<= ((IWineD3DVertexShaderImpl
*) This
)->min_rel_offset
) {
485 ((IWineD3DVertexShaderImpl
*) This
)->min_rel_offset
= reg
;
486 } else if(reg
>= ((IWineD3DVertexShaderImpl
*) This
)->max_rel_offset
) {
487 ((IWineD3DVertexShaderImpl
*) This
)->max_rel_offset
= reg
;
490 reg_maps
->usesrelconstF
= TRUE
;
493 else if(WINED3DSPR_CONSTINT
== regtype
) {
494 This
->baseShader
.uses_int_consts
= TRUE
;
496 else if(WINED3DSPR_CONSTBOOL
== regtype
) {
497 This
->baseShader
.uses_bool_consts
= TRUE
;
500 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and is used
501 * in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel shaders because TECRDOUT
502 * isn't used in them, but future register types might cause issues
504 else if (WINED3DSPR_TEXCRDOUT
== regtype
&& i
== 0 /* Only look at writes */
505 && !pshader
&& WINED3DSHADER_VERSION_MAJOR(shader_version
) < 3)
507 reg_maps
->texcoord_mask
[reg
] |= shader_get_writemask(param
);
513 reg_maps
->loop_depth
= max_loop_depth
;
515 This
->baseShader
.functionLength
= ((char *)pToken
- (char *)byte_code
);
520 static void shader_dump_decl_usage(DWORD decl
, DWORD param
, DWORD shader_version
)
522 DWORD regtype
= shader_get_regtype(param
);
526 if (regtype
== WINED3DSPR_SAMPLER
) {
527 DWORD ttype
= decl
& WINED3DSP_TEXTURETYPE_MASK
;
530 case WINED3DSTT_2D
: TRACE("_2d"); break;
531 case WINED3DSTT_CUBE
: TRACE("_cube"); break;
532 case WINED3DSTT_VOLUME
: TRACE("_volume"); break;
533 default: TRACE("_unknown_ttype(0x%08x)", ttype
);
538 DWORD usage
= decl
& WINED3DSP_DCL_USAGE_MASK
;
539 DWORD idx
= (decl
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
541 /* Pixel shaders 3.0 don't have usage semantics */
542 if (shader_is_pshader_version(shader_version
) && shader_version
< WINED3DPS_VERSION(3,0))
548 case WINED3DDECLUSAGE_POSITION
:
549 TRACE("position%d", idx
);
551 case WINED3DDECLUSAGE_BLENDINDICES
:
554 case WINED3DDECLUSAGE_BLENDWEIGHT
:
557 case WINED3DDECLUSAGE_NORMAL
:
558 TRACE("normal%d", idx
);
560 case WINED3DDECLUSAGE_PSIZE
:
563 case WINED3DDECLUSAGE_COLOR
:
567 TRACE("specular%d", (idx
- 1));
570 case WINED3DDECLUSAGE_TEXCOORD
:
571 TRACE("texture%d", idx
);
573 case WINED3DDECLUSAGE_TANGENT
:
576 case WINED3DDECLUSAGE_BINORMAL
:
579 case WINED3DDECLUSAGE_TESSFACTOR
:
582 case WINED3DDECLUSAGE_POSITIONT
:
583 TRACE("positionT%d", idx
);
585 case WINED3DDECLUSAGE_FOG
:
588 case WINED3DDECLUSAGE_DEPTH
:
591 case WINED3DDECLUSAGE_SAMPLE
:
595 FIXME("unknown_semantics(0x%08x)", usage
);
600 static void shader_dump_arr_entry(const DWORD param
, const DWORD addr_token
,
601 unsigned int reg
, int input
, DWORD shader_version
)
604 ((param
& WINED3DSHADER_ADDRESSMODE_MASK
) == WINED3DSHADER_ADDRMODE_RELATIVE
);
609 shader_dump_param(addr_token
, 0, input
, shader_version
);
619 static void shader_dump_param(const DWORD param
, const DWORD addr_token
, int input
, DWORD shader_version
)
621 static const char * const rastout_reg_names
[] = { "oPos", "oFog", "oPts" };
622 static const char * const misctype_reg_names
[] = { "vPos", "vFace"};
623 const char *swizzle_reg_chars
= "xyzw";
625 DWORD reg
= param
& WINED3DSP_REGNUM_MASK
;
626 DWORD regtype
= shader_get_regtype(param
);
627 DWORD modifier
= param
& WINED3DSP_SRCMOD_MASK
;
629 /* There are some minor differences between pixel and vertex shaders */
630 char pshader
= shader_is_pshader_version(shader_version
);
633 if ( (modifier
== WINED3DSPSM_NEG
) ||
634 (modifier
== WINED3DSPSM_BIASNEG
) ||
635 (modifier
== WINED3DSPSM_SIGNNEG
) ||
636 (modifier
== WINED3DSPSM_X2NEG
) ||
637 (modifier
== WINED3DSPSM_ABSNEG
) )
639 else if (modifier
== WINED3DSPSM_COMP
)
641 else if (modifier
== WINED3DSPSM_NOT
)
644 if (modifier
== WINED3DSPSM_ABS
|| modifier
== WINED3DSPSM_ABSNEG
)
649 case WINED3DSPR_TEMP
:
652 case WINED3DSPR_INPUT
:
654 shader_dump_arr_entry(param
, addr_token
, reg
, input
, shader_version
);
656 case WINED3DSPR_CONST
:
657 case WINED3DSPR_CONST2
:
658 case WINED3DSPR_CONST3
:
659 case WINED3DSPR_CONST4
:
661 shader_dump_arr_entry(param
, addr_token
, shader_get_float_offset(param
), input
, shader_version
);
663 case WINED3DSPR_TEXTURE
: /* vs: case D3DSPR_ADDR */
664 TRACE("%c%u", (pshader
? 't':'a'), reg
);
666 case WINED3DSPR_RASTOUT
:
667 TRACE("%s", rastout_reg_names
[reg
]);
669 case WINED3DSPR_COLOROUT
:
672 case WINED3DSPR_DEPTHOUT
:
675 case WINED3DSPR_ATTROUT
:
678 case WINED3DSPR_TEXCRDOUT
:
680 /* Vertex shaders >= 3.0 use general purpose output registers
681 * (WINED3DSPR_OUTPUT), which can include an address token */
683 if (WINED3DSHADER_VERSION_MAJOR(shader_version
) >= 3) {
685 shader_dump_arr_entry(param
, addr_token
, reg
, input
, shader_version
);
690 case WINED3DSPR_CONSTINT
:
692 shader_dump_arr_entry(param
, addr_token
, reg
, input
, shader_version
);
694 case WINED3DSPR_CONSTBOOL
:
696 shader_dump_arr_entry(param
, addr_token
, reg
, input
, shader_version
);
698 case WINED3DSPR_LABEL
:
701 case WINED3DSPR_LOOP
:
704 case WINED3DSPR_SAMPLER
:
707 case WINED3DSPR_MISCTYPE
:
709 FIXME("Unhandled misctype register %d\n", reg
);
711 TRACE("%s", misctype_reg_names
[reg
]);
714 case WINED3DSPR_PREDICATE
:
718 TRACE("unhandled_rtype(%#x)", regtype
);
723 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
725 if ((param
& WINED3DSP_WRITEMASK_ALL
) != WINED3DSP_WRITEMASK_ALL
) {
727 if (param
& WINED3DSP_WRITEMASK_0
) TRACE("%c", swizzle_reg_chars
[0]);
728 if (param
& WINED3DSP_WRITEMASK_1
) TRACE("%c", swizzle_reg_chars
[1]);
729 if (param
& WINED3DSP_WRITEMASK_2
) TRACE("%c", swizzle_reg_chars
[2]);
730 if (param
& WINED3DSP_WRITEMASK_3
) TRACE("%c", swizzle_reg_chars
[3]);
735 DWORD swizzle
= (param
& WINED3DSP_SWIZZLE_MASK
) >> WINED3DSP_SWIZZLE_SHIFT
;
736 DWORD swizzle_x
= swizzle
& 0x03;
737 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
738 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
739 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
743 case WINED3DSPSM_NONE
: break;
744 case WINED3DSPSM_NEG
: break;
745 case WINED3DSPSM_NOT
: break;
746 case WINED3DSPSM_BIAS
: TRACE("_bias"); break;
747 case WINED3DSPSM_BIASNEG
: TRACE("_bias"); break;
748 case WINED3DSPSM_SIGN
: TRACE("_bx2"); break;
749 case WINED3DSPSM_SIGNNEG
: TRACE("_bx2"); break;
750 case WINED3DSPSM_COMP
: break;
751 case WINED3DSPSM_X2
: TRACE("_x2"); break;
752 case WINED3DSPSM_X2NEG
: TRACE("_x2"); break;
753 case WINED3DSPSM_DZ
: TRACE("_dz"); break;
754 case WINED3DSPSM_DW
: TRACE("_dw"); break;
755 case WINED3DSPSM_ABSNEG
: TRACE(")"); break;
756 case WINED3DSPSM_ABS
: TRACE(")"); break;
758 TRACE("_unknown_modifier(%#x)", modifier
>> WINED3DSP_SRCMOD_SHIFT
);
763 * swizzle bits fields:
766 if ((WINED3DVS_NOSWIZZLE
>> WINED3DVS_SWIZZLE_SHIFT
) != swizzle
) {
767 if (swizzle_x
== swizzle_y
&&
768 swizzle_x
== swizzle_z
&&
769 swizzle_x
== swizzle_w
) {
770 TRACE(".%c", swizzle_reg_chars
[swizzle_x
]);
773 swizzle_reg_chars
[swizzle_x
],
774 swizzle_reg_chars
[swizzle_y
],
775 swizzle_reg_chars
[swizzle_z
],
776 swizzle_reg_chars
[swizzle_w
]);
782 /* Shared code in order to generate the bulk of the shader string.
783 * NOTE: A description of how to parse tokens can be found on msdn */
784 void shader_generate_main(IWineD3DBaseShader
*iface
, SHADER_BUFFER
* buffer
,
785 const shader_reg_maps
* reg_maps
, CONST DWORD
* pFunction
)
787 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
788 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
; /* To access shader backend callbacks */
789 const SHADER_OPCODE
*opcode_table
= This
->baseShader
.shader_ins
;
790 const SHADER_HANDLER
*handler_table
= device
->shader_backend
->shader_instruction_handler_table
;
791 DWORD shader_version
= reg_maps
->shader_version
;
792 struct wined3d_shader_dst_param dst_param
;
793 struct wined3d_shader_instruction ins
;
794 struct wined3d_shader_context ctx
;
795 const DWORD
*pToken
= pFunction
;
796 const SHADER_OPCODE
*curOpcode
;
797 SHADER_HANDLER hw_fct
;
800 /* Initialize current parsing state */
802 ctx
.reg_maps
= reg_maps
;
806 ins
.dst
= &dst_param
;
807 This
->baseShader
.parse_state
.current_row
= 0;
809 while (WINED3DPS_END() != *pToken
)
813 /* Skip version token */
814 if (shader_is_version_token(*pToken
))
820 /* Skip comment tokens */
821 if (shader_is_comment(*pToken
))
823 pToken
+= (*pToken
& WINED3DSI_COMMENTSIZE_MASK
) >> WINED3DSI_COMMENTSIZE_SHIFT
;
829 opcode_token
= *pToken
++;
830 curOpcode
= shader_get_opcode(opcode_table
, shader_version
, opcode_token
);
832 /* Unknown opcode and its parameters */
835 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token
);
836 pToken
+= shader_skip_unrecognized(pToken
, shader_version
);
841 if (WINED3DSIO_DCL
== curOpcode
->opcode
842 || WINED3DSIO_NOP
== curOpcode
->opcode
843 || WINED3DSIO_DEF
== curOpcode
->opcode
844 || WINED3DSIO_DEFI
== curOpcode
->opcode
845 || WINED3DSIO_DEFB
== curOpcode
->opcode
846 || WINED3DSIO_PHASE
== curOpcode
->opcode
847 || WINED3DSIO_RET
== curOpcode
->opcode
)
849 pToken
+= shader_skip_opcode(curOpcode
, opcode_token
, shader_version
);
854 hw_fct
= handler_table
[curOpcode
->handler_idx
];
856 /* Unhandled opcode */
859 FIXME("Can't handle opcode %s in hwShader\n", curOpcode
->name
);
860 pToken
+= shader_skip_opcode(curOpcode
, opcode_token
, shader_version
);
864 ins
.handler_idx
= curOpcode
->handler_idx
;
865 ins
.flags
= opcode_token
& WINED3D_OPCODESPECIFICCONTROL_MASK
;
866 ins
.coissue
= opcode_token
& WINED3DSI_COISSUE
;
868 /* Destination token */
869 ins
.dst_count
= curOpcode
->dst_token
? 1 : 0;
872 DWORD param
, addr_param
= 0;
873 pToken
+= shader_get_param(pToken
, shader_version
, ¶m
, &addr_param
);
874 shader_parse_dst_param(param
, addr_param
, &dst_param
);
877 /* Predication token */
878 if (opcode_token
& WINED3DSHADER_INSTRUCTION_PREDICATED
) ins
.predicate
= *pToken
++;
880 /* Other source tokens */
881 ins
.src_count
= curOpcode
->num_params
- curOpcode
->dst_token
;
882 for (i
= 0; i
< ins
.src_count
; ++i
)
884 DWORD param
, addr_token
= 0;
885 pToken
+= shader_get_param(pToken
, shader_version
, ¶m
, &addr_token
);
887 ins
.src_addr
[i
] = addr_token
;
890 /* Call appropriate function for output target */
893 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
894 /* FIXME: This should be internal to the shader backend.
895 * Also, right now this is the only reason "shader_mode" exists. */
896 if (This
->baseShader
.shader_mode
== SHADER_GLSL
) shader_glsl_add_instruction_modifiers(&ins
);
900 static void shader_dump_ins_modifiers(const DWORD output
)
902 DWORD shift
= (output
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
903 DWORD mmask
= output
& WINED3DSP_DSTMOD_MASK
;
907 case 13: TRACE("_d8"); break;
908 case 14: TRACE("_d4"); break;
909 case 15: TRACE("_d2"); break;
910 case 1: TRACE("_x2"); break;
911 case 2: TRACE("_x4"); break;
912 case 3: TRACE("_x8"); break;
913 default: TRACE("_unhandled_shift(%d)", shift
); break;
916 if (mmask
& WINED3DSPDM_SATURATE
) TRACE("_sat");
917 if (mmask
& WINED3DSPDM_PARTIALPRECISION
) TRACE("_pp");
918 if (mmask
& WINED3DSPDM_MSAMPCENTROID
) TRACE("_centroid");
920 mmask
&= ~(WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_MSAMPCENTROID
);
922 FIXME("_unrecognized_modifier(%#x)", mmask
>> WINED3DSP_DSTMOD_SHIFT
);
925 void shader_trace_init(const DWORD
*pFunction
, const SHADER_OPCODE
*opcode_table
)
927 const DWORD
* pToken
= pFunction
;
928 const SHADER_OPCODE
* curOpcode
= NULL
;
929 DWORD shader_version
;
933 TRACE("Parsing %p\n", pFunction
);
935 /* The version token is supposed to be the first token */
936 if (!shader_is_version_token(*pToken
))
938 FIXME("First token is not a version token, invalid shader.\n");
941 shader_version
= *pToken
++;
942 TRACE("%s_%u_%u\n", shader_is_pshader_version(shader_version
) ? "ps": "vs",
943 WINED3DSHADER_VERSION_MAJOR(shader_version
), WINED3DSHADER_VERSION_MINOR(shader_version
));
945 while (WINED3DVS_END() != *pToken
)
947 if (shader_is_comment(*pToken
)) /* comment */
949 DWORD comment_len
= (*pToken
& WINED3DSI_COMMENTSIZE_MASK
) >> WINED3DSI_COMMENTSIZE_SHIFT
;
951 TRACE("//%s\n", (const char*)pToken
);
952 pToken
+= comment_len
;
955 opcode_token
= *pToken
++;
956 curOpcode
= shader_get_opcode(opcode_table
, shader_version
, opcode_token
);
961 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token
);
962 tokens_read
= shader_skip_unrecognized(pToken
, shader_version
);
963 pToken
+= tokens_read
;
967 if (curOpcode
->opcode
== WINED3DSIO_DCL
)
969 DWORD usage
= *pToken
;
970 DWORD param
= *(pToken
+ 1);
972 shader_dump_decl_usage(usage
, param
, shader_version
);
973 shader_dump_ins_modifiers(param
);
975 shader_dump_param(param
, 0, 0, shader_version
);
978 else if (curOpcode
->opcode
== WINED3DSIO_DEF
)
980 unsigned int offset
= shader_get_float_offset(*pToken
);
982 TRACE("def c%u = %f, %f, %f, %f", offset
,
983 *(const float *)(pToken
+ 1),
984 *(const float *)(pToken
+ 2),
985 *(const float *)(pToken
+ 3),
986 *(const float *)(pToken
+ 4));
989 else if (curOpcode
->opcode
== WINED3DSIO_DEFI
)
991 TRACE("defi i%u = %d, %d, %d, %d", *pToken
& WINED3DSP_REGNUM_MASK
,
998 else if (curOpcode
->opcode
== WINED3DSIO_DEFB
)
1000 TRACE("defb b%u = %s", *pToken
& WINED3DSP_REGNUM_MASK
,
1001 *(pToken
+ 1)? "true": "false");
1006 DWORD param
, addr_token
;
1009 /* Print out predication source token first - it follows
1010 * the destination token. */
1011 if (opcode_token
& WINED3DSHADER_INSTRUCTION_PREDICATED
)
1014 shader_dump_param(*(pToken
+ 2), 0, 1, shader_version
);
1017 if (opcode_token
& WINED3DSI_COISSUE
)
1019 /* PixWin marks instructions with the coissue flag with a '+' */
1023 TRACE("%s", curOpcode
->name
);
1025 if (curOpcode
->opcode
== WINED3DSIO_IFC
1026 || curOpcode
->opcode
== WINED3DSIO_BREAKC
)
1028 DWORD op
= (opcode_token
& INST_CONTROLS_MASK
) >> INST_CONTROLS_SHIFT
;
1032 case COMPARISON_GT
: TRACE("_gt"); break;
1033 case COMPARISON_EQ
: TRACE("_eq"); break;
1034 case COMPARISON_GE
: TRACE("_ge"); break;
1035 case COMPARISON_LT
: TRACE("_lt"); break;
1036 case COMPARISON_NE
: TRACE("_ne"); break;
1037 case COMPARISON_LE
: TRACE("_le"); break;
1038 default: TRACE("_(%u)", op
);
1041 else if (curOpcode
->opcode
== WINED3DSIO_TEX
1042 && shader_version
>= WINED3DPS_VERSION(2,0)
1043 && (opcode_token
& WINED3DSI_TEXLD_PROJECT
))
1048 /* Destination token */
1049 if (curOpcode
->dst_token
)
1051 tokens_read
= shader_get_param(pToken
, shader_version
, ¶m
, &addr_token
);
1052 pToken
+= tokens_read
;
1054 shader_dump_ins_modifiers(param
);
1056 shader_dump_param(param
, addr_token
, 0, shader_version
);
1059 /* Predication token - already printed out, just skip it */
1060 if (opcode_token
& WINED3DSHADER_INSTRUCTION_PREDICATED
)
1065 /* Other source tokens */
1066 for (i
= curOpcode
->dst_token
; i
< curOpcode
->num_params
; ++i
)
1068 tokens_read
= shader_get_param(pToken
, shader_version
, ¶m
, &addr_token
);
1069 pToken
+= tokens_read
;
1071 TRACE((i
== 0)? " " : ", ");
1072 shader_dump_param(param
, addr_token
, 1, shader_version
);
1080 void shader_cleanup(IWineD3DBaseShader
*iface
)
1082 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)iface
;
1084 ((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->shader_backend
->shader_destroy(iface
);
1085 HeapFree(GetProcessHeap(), 0, This
->baseShader
.function
);
1086 shader_delete_constant_list(&This
->baseShader
.constantsF
);
1087 shader_delete_constant_list(&This
->baseShader
.constantsB
);
1088 shader_delete_constant_list(&This
->baseShader
.constantsI
);
1089 list_remove(&This
->baseShader
.shader_list_entry
);
1092 static const SHADER_HANDLER shader_none_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] = {0};
1093 static void shader_none_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {}
1094 static void shader_none_select_depth_blt(IWineD3DDevice
*iface
, enum tex_types tex_type
) {}
1095 static void shader_none_deselect_depth_blt(IWineD3DDevice
*iface
) {}
1096 static void shader_none_update_float_vertex_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
) {}
1097 static void shader_none_update_float_pixel_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
) {}
1098 static void shader_none_load_constants(IWineD3DDevice
*iface
, char usePS
, char useVS
) {}
1099 static void shader_none_load_np2fixup_constants(IWineD3DDevice
*iface
, char usePS
, char useVS
) {}
1100 static void shader_none_destroy(IWineD3DBaseShader
*iface
) {}
1101 static HRESULT
shader_none_alloc(IWineD3DDevice
*iface
) {return WINED3D_OK
;}
1102 static void shader_none_free(IWineD3DDevice
*iface
) {}
1103 static BOOL
shader_none_dirty_const(IWineD3DDevice
*iface
) {return FALSE
;}
1104 static GLuint
shader_none_generate_pshader(IWineD3DPixelShader
*iface
, SHADER_BUFFER
*buffer
, const struct ps_compile_args
*args
) {
1105 FIXME("NONE shader backend asked to generate a pixel shader\n");
1108 static GLuint
shader_none_generate_vshader(IWineD3DVertexShader
*iface
, SHADER_BUFFER
*buffer
, const struct vs_compile_args
*args
) {
1109 FIXME("NONE shader backend asked to generate a vertex shader\n");
1113 #define GLINFO_LOCATION (*gl_info)
1114 static void shader_none_get_caps(WINED3DDEVTYPE devtype
, const WineD3D_GL_Info
*gl_info
, struct shader_caps
*pCaps
)
1116 /* Set the shader caps to 0 for the none shader backend */
1117 pCaps
->VertexShaderVersion
= 0;
1118 pCaps
->PixelShaderVersion
= 0;
1119 pCaps
->PixelShader1xMaxValue
= 0.0;
1121 #undef GLINFO_LOCATION
1122 static BOOL
shader_none_color_fixup_supported(struct color_fixup_desc fixup
)
1124 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
1126 TRACE("Checking support for fixup:\n");
1127 dump_color_fixup_desc(fixup
);
1130 /* Faked to make some apps happy. */
1131 if (!is_yuv_fixup(fixup
))
1137 TRACE("[FAILED]\n");
1141 const shader_backend_t none_shader_backend
= {
1142 shader_none_instruction_handler_table
,
1144 shader_none_select_depth_blt
,
1145 shader_none_deselect_depth_blt
,
1146 shader_none_update_float_vertex_constants
,
1147 shader_none_update_float_pixel_constants
,
1148 shader_none_load_constants
,
1149 shader_none_load_np2fixup_constants
,
1150 shader_none_destroy
,
1153 shader_none_dirty_const
,
1154 shader_none_generate_pshader
,
1155 shader_none_generate_vshader
,
1156 shader_none_get_caps
,
1157 shader_none_color_fixup_supported
,