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);
36 #define WINED3DSP_DCL_USAGE_SHIFT 0
37 #define WINED3DSP_DCL_USAGE_MASK (0xf << WINED3DSP_DCL_USAGE_SHIFT)
38 #define WINED3DSP_DCL_USAGEINDEX_SHIFT 16
39 #define WINED3DSP_DCL_USAGEINDEX_MASK (0xf << WINED3DSP_DCL_USAGEINDEX_SHIFT)
41 /* Opcode-related masks */
42 #define WINED3DSI_OPCODE_MASK 0x0000ffff
44 #define WINED3D_OPCODESPECIFICCONTROL_SHIFT 16
45 #define WINED3D_OPCODESPECIFICCONTROL_MASK (0xff << WINED3D_OPCODESPECIFICCONTROL_SHIFT)
47 #define WINED3DSI_INSTLENGTH_SHIFT 24
48 #define WINED3DSI_INSTLENGTH_MASK (0xf << WINED3DSI_INSTLENGTH_SHIFT)
50 #define WINED3DSI_COISSUE (1 << 30)
52 #define WINED3DSI_COMMENTSIZE_SHIFT 16
53 #define WINED3DSI_COMMENTSIZE_MASK (0x7fff << WINED3DSI_COMMENTSIZE_SHIFT)
55 #define WINED3DSHADER_INSTRUCTION_PREDICATED (1 << 28)
57 /* Register number mask */
58 #define WINED3DSP_REGNUM_MASK 0x000007ff
60 /* Register type masks */
61 #define WINED3DSP_REGTYPE_SHIFT 28
62 #define WINED3DSP_REGTYPE_MASK (0x7 << WINED3DSP_REGTYPE_SHIFT)
63 #define WINED3DSP_REGTYPE_SHIFT2 8
64 #define WINED3DSP_REGTYPE_MASK2 (0x18 << WINED3DSP_REGTYPE_SHIFT2)
66 /* Relative addressing mask */
67 #define WINED3DSHADER_ADDRESSMODE_SHIFT 13
68 #define WINED3DSHADER_ADDRESSMODE_MASK (1 << WINED3DSHADER_ADDRESSMODE_SHIFT)
70 /* Destination modifier mask */
71 #define WINED3DSP_DSTMOD_SHIFT 20
72 #define WINED3DSP_DSTMOD_MASK (0xf << WINED3DSP_DSTMOD_SHIFT)
74 /* Destination shift mask */
75 #define WINED3DSP_DSTSHIFT_SHIFT 24
76 #define WINED3DSP_DSTSHIFT_MASK (0xf << WINED3DSP_DSTSHIFT_SHIFT)
79 #define WINED3DSP_SWIZZLE_SHIFT 16
80 #define WINED3DSP_SWIZZLE_MASK (0xff << WINED3DSP_SWIZZLE_SHIFT)
82 /* Source modifier mask */
83 #define WINED3DSP_SRCMOD_SHIFT 24
84 #define WINED3DSP_SRCMOD_MASK (0xf << WINED3DSP_SRCMOD_SHIFT)
86 typedef enum _WINED3DSHADER_ADDRESSMODE_TYPE
88 WINED3DSHADER_ADDRMODE_ABSOLUTE = 0 << WINED3DSHADER_ADDRESSMODE_SHIFT,
89 WINED3DSHADER_ADDRMODE_RELATIVE = 1 << WINED3DSHADER_ADDRESSMODE_SHIFT,
90 WINED3DSHADER_ADDRMODE_FORCE_DWORD = 0x7fffffff,
91 } WINED3DSHADER_ADDRESSMODE_TYPE;
93 static void shader_dump_param(const DWORD param, const DWORD addr_token, int input, DWORD shader_version);
95 static inline BOOL shader_is_version_token(DWORD token) {
96 return shader_is_pshader_version(token) ||
97 shader_is_vshader_version(token);
100 void shader_buffer_init(struct SHADER_BUFFER *buffer)
102 buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
103 buffer->buffer[0] = '\0';
106 buffer->newline = TRUE;
109 void shader_buffer_free(struct SHADER_BUFFER *buffer)
111 HeapFree(GetProcessHeap(), 0, buffer->buffer);
114 int shader_vaddline(SHADER_BUFFER* buffer, const char *format, va_list args)
116 char* base = buffer->buffer + buffer->bsize;
119 rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
121 if (rc < 0 || /* C89 */
122 rc > SHADER_PGMSIZE - 1 - buffer->bsize) { /* C99 */
124 ERR("The buffer allocated for the shader program string "
125 "is too small at %d bytes.\n", SHADER_PGMSIZE);
126 buffer->bsize = SHADER_PGMSIZE - 1;
130 if (buffer->newline) {
131 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
132 buffer->newline = FALSE;
138 if (buffer->buffer[buffer->bsize-1] == '\n') {
140 buffer->newline = TRUE;
145 int shader_addline(SHADER_BUFFER* buffer, const char *format, ...)
150 va_start(args, format);
151 ret = shader_vaddline(buffer, format, args);
157 void shader_init(struct IWineD3DBaseShaderClass *shader,
158 IWineD3DDevice *device, const SHADER_OPCODE *instruction_table)
161 shader->device = device;
162 shader->shader_ins = instruction_table;
163 list_init(&shader->linked_programs);
166 static inline WINED3DSHADER_PARAM_REGISTER_TYPE shader_get_regtype(DWORD param)
168 return ((param & WINED3DSP_REGTYPE_MASK) >> WINED3DSP_REGTYPE_SHIFT)
169 | ((param & WINED3DSP_REGTYPE_MASK2) >> WINED3DSP_REGTYPE_SHIFT2);
172 static inline DWORD shader_get_writemask(DWORD param)
174 return param & WINED3DSP_WRITEMASK_ALL;
177 static inline BOOL shader_is_comment(DWORD token)
179 return WINED3DSIO_COMMENT == (token & WINED3DSI_OPCODE_MASK);
182 static const SHADER_OPCODE *shader_get_opcode(const SHADER_OPCODE *opcode_table, DWORD shader_version, DWORD code)
186 /** TODO: use dichotomic search */
187 while (opcode_table[i].name)
189 if ((code & WINED3DSI_OPCODE_MASK) == opcode_table[i].opcode
190 && shader_version >= opcode_table[i].min_version
191 && (!opcode_table[i].max_version || shader_version <= opcode_table[i].max_version))
193 return &opcode_table[i];
198 FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n",
199 code, code, code & WINED3DSI_OPCODE_MASK, shader_version);
204 /* Read a parameter opcode from the input stream,
205 * and possibly a relative addressing token.
206 * Return the number of tokens read */
207 static int shader_get_param(const DWORD *pToken, DWORD shader_version, DWORD *param, DWORD *addr_token)
213 /* PS >= 3.0 have relative addressing (with token)
214 * VS >= 2.0 have relative addressing (with token)
215 * VS >= 1.0 < 2.0 have relative addressing (without token)
216 * The version check below should work in general */
217 if (*pToken & WINED3DSHADER_ADDRMODE_RELATIVE)
219 if (WINED3DSHADER_VERSION_MAJOR(shader_version) < 2)
221 *addr_token = (1 << 31)
222 | ((WINED3DSPR_ADDR << WINED3DSP_REGTYPE_SHIFT2) & WINED3DSP_REGTYPE_MASK2)
223 | ((WINED3DSPR_ADDR << WINED3DSP_REGTYPE_SHIFT) & WINED3DSP_REGTYPE_MASK)
224 | (WINED3DSP_NOSWIZZLE << WINED3DSP_SWIZZLE_SHIFT);
228 *addr_token = *(pToken + 1);
236 /* Return the number of parameters to skip for an opcode */
237 static inline int shader_skip_opcode(const SHADER_OPCODE *curOpcode, DWORD opcode_token, DWORD shader_version)
239 /* Shaders >= 2.0 may contain address tokens, but fortunately they
240 * have a useful length mask - use it here. Shaders 1.0 contain no such tokens */
241 return (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 2)
242 ? ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT) : curOpcode->num_params;
245 /* Read the parameters of an unrecognized opcode from the input stream
246 * Return the number of tokens read.
248 * Note: This function assumes source or destination token format.
249 * It will not work with specially-formatted tokens like DEF or DCL,
250 * but hopefully those would be recognized */
251 static int shader_skip_unrecognized(const DWORD *pToken, DWORD shader_version)
256 /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
257 while (*pToken & 0x80000000) {
259 DWORD param, addr_token = 0;
260 tokens_read += shader_get_param(pToken, shader_version, ¶m, &addr_token);
261 pToken += tokens_read;
263 FIXME("Unrecognized opcode param: token=0x%08x "
264 "addr_token=0x%08x name=", param, addr_token);
265 shader_dump_param(param, addr_token, i, shader_version);
272 /* Convert floating point offset relative
273 * to a register file to an absolute offset for float constants */
274 static unsigned int shader_get_float_offset(const DWORD reg)
276 unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
277 int regtype = shader_get_regtype(reg);
280 case WINED3DSPR_CONST: return regnum;
281 case WINED3DSPR_CONST2: return 2048 + regnum;
282 case WINED3DSPR_CONST3: return 4096 + regnum;
283 case WINED3DSPR_CONST4: return 6144 + regnum;
285 FIXME("Unsupported register type: %d\n", regtype);
290 static void shader_delete_constant_list(struct list* clist) {
293 struct local_constant* constant;
295 ptr = list_head(clist);
297 constant = LIST_ENTRY(ptr, struct local_constant, entry);
298 ptr = list_next(clist, ptr);
299 HeapFree(GetProcessHeap(), 0, constant);
304 static void shader_parse_dst_param(DWORD param, const struct wined3d_shader_src_param *rel_addr,
305 struct wined3d_shader_dst_param *dst)
307 dst->register_type = ((param & WINED3DSP_REGTYPE_MASK) >> WINED3DSP_REGTYPE_SHIFT)
308 | ((param & WINED3DSP_REGTYPE_MASK2) >> WINED3DSP_REGTYPE_SHIFT2);
309 dst->register_idx = param & WINED3DSP_REGNUM_MASK;
310 dst->write_mask = param & WINED3DSP_WRITEMASK_ALL;
311 dst->modifiers = (param & WINED3DSP_DSTMOD_MASK) >> WINED3DSP_DSTMOD_SHIFT;
312 dst->shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
313 dst->rel_addr = rel_addr;
316 static void shader_parse_src_param(DWORD param, const struct wined3d_shader_src_param *rel_addr,
317 struct wined3d_shader_src_param *src)
319 src->register_type = ((param & WINED3DSP_REGTYPE_MASK) >> WINED3DSP_REGTYPE_SHIFT)
320 | ((param & WINED3DSP_REGTYPE_MASK2) >> WINED3DSP_REGTYPE_SHIFT2);
321 src->register_idx = param & WINED3DSP_REGNUM_MASK;
322 src->swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
323 src->modifiers = (param & WINED3DSP_SRCMOD_MASK) >> WINED3DSP_SRCMOD_SHIFT;
324 src->rel_addr = rel_addr;
327 /* Note that this does not count the loop register
328 * as an address register. */
330 HRESULT shader_get_registers_used(IWineD3DBaseShader *iface, struct shader_reg_maps *reg_maps,
331 struct wined3d_shader_semantic *semantics_in, struct wined3d_shader_semantic *semantics_out,
332 const DWORD *byte_code)
334 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
335 const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;
336 DWORD shader_version;
337 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
338 const DWORD* pToken = byte_code;
341 /* There are some minor differences between pixel and vertex shaders */
343 memset(reg_maps->bumpmat, 0, sizeof(reg_maps->bumpmat));
344 memset(reg_maps->luminanceparams, 0, sizeof(reg_maps->luminanceparams));
346 /* get_registers_used is called on every compile on some 1.x shaders, which can result
347 * in stacking up a collection of local constants. Delete the old constants if existing
349 shader_delete_constant_list(&This->baseShader.constantsF);
350 shader_delete_constant_list(&This->baseShader.constantsB);
351 shader_delete_constant_list(&This->baseShader.constantsI);
353 /* The version token is supposed to be the first token */
354 if (!shader_is_version_token(*pToken))
356 FIXME("First token is not a version token, invalid shader.\n");
357 return WINED3DERR_INVALIDCALL;
359 reg_maps->shader_version = shader_version = *pToken++;
360 pshader = shader_is_pshader_version(shader_version);
362 while (WINED3DVS_END() != *pToken) {
363 CONST SHADER_OPCODE* curOpcode;
367 if (shader_is_comment(*pToken))
369 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
371 pToken += comment_len;
376 opcode_token = *pToken++;
377 curOpcode = shader_get_opcode(shader_ins, shader_version, opcode_token);
379 /* Unhandled opcode, and its parameters */
380 if (NULL == curOpcode) {
381 while (*pToken & 0x80000000)
384 /* Handle declarations */
385 } else if (WINED3DSIO_DCL == curOpcode->opcode) {
387 DWORD usage = *pToken++;
388 DWORD param = *pToken++;
389 DWORD regtype = shader_get_regtype(param);
390 unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
392 /* Vshader: mark attributes used
393 Pshader: mark 3.0 input registers used, save token */
394 if (WINED3DSPR_INPUT == regtype) {
397 reg_maps->attributes[regnum] = 1;
399 reg_maps->packed_input[regnum] = 1;
401 semantics_in[regnum].usage = (usage & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
402 semantics_in[regnum].usage_idx =
403 (usage & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
404 shader_parse_dst_param(param, NULL, &semantics_in[regnum].reg);
406 /* Vshader: mark 3.0 output registers used, save token */
407 } else if (WINED3DSPR_OUTPUT == regtype) {
408 reg_maps->packed_output[regnum] = 1;
409 semantics_out[regnum].usage = (usage & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
410 semantics_out[regnum].usage_idx =
411 (usage & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
412 shader_parse_dst_param(param, NULL, &semantics_out[regnum].reg);
414 if (usage & (WINED3DDECLUSAGE_FOG << WINED3DSP_DCL_USAGE_SHIFT))
417 /* Save sampler usage token */
418 } else if (WINED3DSPR_SAMPLER == regtype)
419 reg_maps->samplers[regnum] = usage;
421 } else if (WINED3DSIO_DEF == curOpcode->opcode) {
423 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
424 if (!lconst) return E_OUTOFMEMORY;
425 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
426 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
428 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
429 if (WINED3DSHADER_VERSION_MAJOR(shader_version) == 1 && pshader)
431 float *value = (float *) lconst->value;
432 if(value[0] < -1.0) value[0] = -1.0;
433 else if(value[0] > 1.0) value[0] = 1.0;
434 if(value[1] < -1.0) value[1] = -1.0;
435 else if(value[1] > 1.0) value[1] = 1.0;
436 if(value[2] < -1.0) value[2] = -1.0;
437 else if(value[2] > 1.0) value[2] = 1.0;
438 if(value[3] < -1.0) value[3] = -1.0;
439 else if(value[3] > 1.0) value[3] = 1.0;
442 list_add_head(&This->baseShader.constantsF, &lconst->entry);
443 pToken += curOpcode->num_params;
445 } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
447 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
448 if (!lconst) return E_OUTOFMEMORY;
449 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
450 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
451 list_add_head(&This->baseShader.constantsI, &lconst->entry);
452 pToken += curOpcode->num_params;
454 } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
456 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
457 if (!lconst) return E_OUTOFMEMORY;
458 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
459 memcpy(lconst->value, pToken + 1, 1 * sizeof(DWORD));
460 list_add_head(&This->baseShader.constantsB, &lconst->entry);
461 pToken += curOpcode->num_params;
463 /* If there's a loop in the shader */
464 } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
465 WINED3DSIO_REP == curOpcode->opcode) {
467 if(cur_loop_depth > max_loop_depth)
468 max_loop_depth = cur_loop_depth;
469 pToken += curOpcode->num_params;
471 /* Rep and Loop always use an integer constant for the control parameters */
472 This->baseShader.uses_int_consts = TRUE;
473 } else if (WINED3DSIO_ENDLOOP == curOpcode->opcode ||
474 WINED3DSIO_ENDREP == curOpcode->opcode) {
477 /* For subroutine prototypes */
478 } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
480 DWORD snum = *pToken & WINED3DSP_REGNUM_MASK;
481 reg_maps->labels[snum] = 1;
482 pToken += curOpcode->num_params;
484 /* Set texture, address, temporary registers */
488 /* Declare 1.X samplers implicitly, based on the destination reg. number */
489 if (WINED3DSHADER_VERSION_MAJOR(shader_version) == 1
490 && pshader /* Filter different instructions with the same enum values in VS */
491 && (WINED3DSIO_TEX == curOpcode->opcode
492 || WINED3DSIO_TEXBEM == curOpcode->opcode
493 || WINED3DSIO_TEXBEML == curOpcode->opcode
494 || WINED3DSIO_TEXDP3TEX == curOpcode->opcode
495 || WINED3DSIO_TEXM3x2TEX == curOpcode->opcode
496 || WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode
497 || WINED3DSIO_TEXM3x3TEX == curOpcode->opcode
498 || WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode
499 || WINED3DSIO_TEXREG2AR == curOpcode->opcode
500 || WINED3DSIO_TEXREG2GB == curOpcode->opcode
501 || WINED3DSIO_TEXREG2RGB == curOpcode->opcode))
503 /* Fake sampler usage, only set reserved bit and ttype */
504 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
506 TRACE("Setting fake 2D sampler for 1.x pixelshader\n");
507 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
509 /* texbem is only valid with < 1.4 pixel shaders */
510 if(WINED3DSIO_TEXBEM == curOpcode->opcode ||
511 WINED3DSIO_TEXBEML == curOpcode->opcode) {
512 reg_maps->bumpmat[sampler_code] = TRUE;
513 if(WINED3DSIO_TEXBEML == curOpcode->opcode) {
514 reg_maps->luminanceparams[sampler_code] = TRUE;
518 if(WINED3DSIO_NRM == curOpcode->opcode) {
519 reg_maps->usesnrm = 1;
520 } else if(WINED3DSIO_BEM == curOpcode->opcode && pshader) {
521 DWORD regnum = *pToken & WINED3DSP_REGNUM_MASK;
522 reg_maps->bumpmat[regnum] = TRUE;
523 } else if(WINED3DSIO_DSY == curOpcode->opcode) {
524 reg_maps->usesdsy = 1;
527 /* This will loop over all the registers and try to
528 * make a bitmask of the ones we're interested in.
530 * Relative addressing tokens are ignored, but that's
531 * okay, since we'll catch any address registers when
532 * they are initialized (required by spec) */
534 limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
535 curOpcode->num_params + 1: curOpcode->num_params;
537 for (i = 0; i < limit; ++i) {
539 DWORD param, addr_token, reg, regtype;
540 pToken += shader_get_param(pToken, shader_version, ¶m, &addr_token);
542 regtype = shader_get_regtype(param);
543 reg = param & WINED3DSP_REGNUM_MASK;
545 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
548 reg_maps->texcoord[reg] = 1;
550 reg_maps->address[reg] = 1;
553 else if (WINED3DSPR_TEMP == regtype)
554 reg_maps->temporary[reg] = 1;
556 else if (WINED3DSPR_INPUT == regtype) {
558 reg_maps->attributes[reg] = 1;
560 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
561 /* If relative addressing is used, we must assume that all registers
562 * are used. Even if it is a construct like v3[aL], we can't assume
563 * that v0, v1 and v2 aren't read because aL can be negative
566 for(i = 0; i < MAX_REG_INPUT; i++) {
567 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[i] = TRUE;
570 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[reg] = TRUE;
575 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
578 else if (WINED3DSPR_MISCTYPE == regtype && reg == 0 && pshader)
581 else if(WINED3DSPR_CONST == regtype) {
582 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
584 if(reg <= ((IWineD3DVertexShaderImpl *) This)->min_rel_offset) {
585 ((IWineD3DVertexShaderImpl *) This)->min_rel_offset = reg;
586 } else if(reg >= ((IWineD3DVertexShaderImpl *) This)->max_rel_offset) {
587 ((IWineD3DVertexShaderImpl *) This)->max_rel_offset = reg;
590 reg_maps->usesrelconstF = TRUE;
593 else if(WINED3DSPR_CONSTINT == regtype) {
594 This->baseShader.uses_int_consts = TRUE;
596 else if(WINED3DSPR_CONSTBOOL == regtype) {
597 This->baseShader.uses_bool_consts = TRUE;
600 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and is used
601 * in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel shaders because TECRDOUT
602 * isn't used in them, but future register types might cause issues
604 else if (WINED3DSPR_TEXCRDOUT == regtype && i == 0 /* Only look at writes */
605 && !pshader && WINED3DSHADER_VERSION_MAJOR(shader_version) < 3)
607 reg_maps->texcoord_mask[reg] |= shader_get_writemask(param);
613 reg_maps->loop_depth = max_loop_depth;
615 This->baseShader.functionLength = ((char *)pToken - (char *)byte_code);
620 static void shader_dump_decl_usage(DWORD decl, DWORD param, DWORD shader_version)
622 DWORD regtype = shader_get_regtype(param);
626 if (regtype == WINED3DSPR_SAMPLER) {
627 DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
630 case WINED3DSTT_2D: TRACE("_2d"); break;
631 case WINED3DSTT_CUBE: TRACE("_cube"); break;
632 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
633 default: TRACE("_unknown_ttype(0x%08x)", ttype);
638 DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
639 DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
641 /* Pixel shaders 3.0 don't have usage semantics */
642 if (shader_is_pshader_version(shader_version) && shader_version < WINED3DPS_VERSION(3,0))
648 case WINED3DDECLUSAGE_POSITION:
649 TRACE("position%d", idx);
651 case WINED3DDECLUSAGE_BLENDINDICES:
654 case WINED3DDECLUSAGE_BLENDWEIGHT:
657 case WINED3DDECLUSAGE_NORMAL:
658 TRACE("normal%d", idx);
660 case WINED3DDECLUSAGE_PSIZE:
663 case WINED3DDECLUSAGE_COLOR:
667 TRACE("specular%d", (idx - 1));
670 case WINED3DDECLUSAGE_TEXCOORD:
671 TRACE("texture%d", idx);
673 case WINED3DDECLUSAGE_TANGENT:
676 case WINED3DDECLUSAGE_BINORMAL:
679 case WINED3DDECLUSAGE_TESSFACTOR:
682 case WINED3DDECLUSAGE_POSITIONT:
683 TRACE("positionT%d", idx);
685 case WINED3DDECLUSAGE_FOG:
688 case WINED3DDECLUSAGE_DEPTH:
691 case WINED3DDECLUSAGE_SAMPLE:
695 FIXME("unknown_semantics(0x%08x)", usage);
700 static void shader_dump_arr_entry(const DWORD param, const DWORD addr_token,
701 unsigned int reg, int input, DWORD shader_version)
704 ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
709 shader_dump_param(addr_token, 0, input, shader_version);
719 static void shader_dump_param(const DWORD param, const DWORD addr_token, int input, DWORD shader_version)
721 static const char * const rastout_reg_names[] = { "oPos", "oFog", "oPts" };
722 static const char * const misctype_reg_names[] = { "vPos", "vFace"};
723 const char *swizzle_reg_chars = "xyzw";
725 DWORD reg = param & WINED3DSP_REGNUM_MASK;
726 DWORD regtype = shader_get_regtype(param);
727 DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
729 /* There are some minor differences between pixel and vertex shaders */
730 char pshader = shader_is_pshader_version(shader_version);
733 if ( (modifier == WINED3DSPSM_NEG) ||
734 (modifier == WINED3DSPSM_BIASNEG) ||
735 (modifier == WINED3DSPSM_SIGNNEG) ||
736 (modifier == WINED3DSPSM_X2NEG) ||
737 (modifier == WINED3DSPSM_ABSNEG) )
739 else if (modifier == WINED3DSPSM_COMP)
741 else if (modifier == WINED3DSPSM_NOT)
744 if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG)
749 case WINED3DSPR_TEMP:
752 case WINED3DSPR_INPUT:
754 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
756 case WINED3DSPR_CONST:
757 case WINED3DSPR_CONST2:
758 case WINED3DSPR_CONST3:
759 case WINED3DSPR_CONST4:
761 shader_dump_arr_entry(param, addr_token, shader_get_float_offset(param), input, shader_version);
763 case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
764 TRACE("%c%u", (pshader? 't':'a'), reg);
766 case WINED3DSPR_RASTOUT:
767 TRACE("%s", rastout_reg_names[reg]);
769 case WINED3DSPR_COLOROUT:
772 case WINED3DSPR_DEPTHOUT:
775 case WINED3DSPR_ATTROUT:
778 case WINED3DSPR_TEXCRDOUT:
780 /* Vertex shaders >= 3.0 use general purpose output registers
781 * (WINED3DSPR_OUTPUT), which can include an address token */
783 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3) {
785 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
790 case WINED3DSPR_CONSTINT:
792 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
794 case WINED3DSPR_CONSTBOOL:
796 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
798 case WINED3DSPR_LABEL:
801 case WINED3DSPR_LOOP:
804 case WINED3DSPR_SAMPLER:
807 case WINED3DSPR_MISCTYPE:
809 FIXME("Unhandled misctype register %d\n", reg);
811 TRACE("%s", misctype_reg_names[reg]);
814 case WINED3DSPR_PREDICATE:
818 TRACE("unhandled_rtype(%#x)", regtype);
823 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
825 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
827 if (param & WINED3DSP_WRITEMASK_0) TRACE("%c", swizzle_reg_chars[0]);
828 if (param & WINED3DSP_WRITEMASK_1) TRACE("%c", swizzle_reg_chars[1]);
829 if (param & WINED3DSP_WRITEMASK_2) TRACE("%c", swizzle_reg_chars[2]);
830 if (param & WINED3DSP_WRITEMASK_3) TRACE("%c", swizzle_reg_chars[3]);
835 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
836 DWORD swizzle_x = swizzle & 0x03;
837 DWORD swizzle_y = (swizzle >> 2) & 0x03;
838 DWORD swizzle_z = (swizzle >> 4) & 0x03;
839 DWORD swizzle_w = (swizzle >> 6) & 0x03;
843 case WINED3DSPSM_NONE: break;
844 case WINED3DSPSM_NEG: break;
845 case WINED3DSPSM_NOT: break;
846 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
847 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
848 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
849 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
850 case WINED3DSPSM_COMP: break;
851 case WINED3DSPSM_X2: TRACE("_x2"); break;
852 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
853 case WINED3DSPSM_DZ: TRACE("_dz"); break;
854 case WINED3DSPSM_DW: TRACE("_dw"); break;
855 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
856 case WINED3DSPSM_ABS: TRACE(")"); break;
858 TRACE("_unknown_modifier(%#x)", modifier);
863 * swizzle bits fields:
866 if (swizzle != WINED3DSP_NOSWIZZLE)
868 if (swizzle_x == swizzle_y &&
869 swizzle_x == swizzle_z &&
870 swizzle_x == swizzle_w) {
871 TRACE(".%c", swizzle_reg_chars[swizzle_x]);
874 swizzle_reg_chars[swizzle_x],
875 swizzle_reg_chars[swizzle_y],
876 swizzle_reg_chars[swizzle_z],
877 swizzle_reg_chars[swizzle_w]);
883 /* Shared code in order to generate the bulk of the shader string.
884 * NOTE: A description of how to parse tokens can be found on msdn */
885 void shader_generate_main(IWineD3DBaseShader *iface, SHADER_BUFFER* buffer,
886 const shader_reg_maps* reg_maps, CONST DWORD* pFunction)
888 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
889 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device; /* To access shader backend callbacks */
890 const SHADER_OPCODE *opcode_table = This->baseShader.shader_ins;
891 const SHADER_HANDLER *handler_table = device->shader_backend->shader_instruction_handler_table;
892 DWORD shader_version = reg_maps->shader_version;
893 struct wined3d_shader_src_param src_rel_addr[4];
894 struct wined3d_shader_src_param src_param[4];
895 struct wined3d_shader_src_param dst_rel_addr;
896 struct wined3d_shader_dst_param dst_param;
897 struct wined3d_shader_instruction ins;
898 struct wined3d_shader_context ctx;
899 const DWORD *pToken = pFunction;
900 const SHADER_OPCODE *curOpcode;
901 SHADER_HANDLER hw_fct;
904 /* Initialize current parsing state */
906 ctx.reg_maps = reg_maps;
910 ins.dst = &dst_param;
912 This->baseShader.parse_state.current_row = 0;
914 while (WINED3DPS_END() != *pToken)
918 /* Skip version token */
919 if (shader_is_version_token(*pToken))
925 /* Skip comment tokens */
926 if (shader_is_comment(*pToken))
928 pToken += (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
934 opcode_token = *pToken++;
935 curOpcode = shader_get_opcode(opcode_table, shader_version, opcode_token);
937 /* Unknown opcode and its parameters */
940 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
941 pToken += shader_skip_unrecognized(pToken, shader_version);
946 if (WINED3DSIO_DCL == curOpcode->opcode
947 || WINED3DSIO_NOP == curOpcode->opcode
948 || WINED3DSIO_DEF == curOpcode->opcode
949 || WINED3DSIO_DEFI == curOpcode->opcode
950 || WINED3DSIO_DEFB == curOpcode->opcode
951 || WINED3DSIO_PHASE == curOpcode->opcode
952 || WINED3DSIO_RET == curOpcode->opcode)
954 pToken += shader_skip_opcode(curOpcode, opcode_token, shader_version);
959 hw_fct = handler_table[curOpcode->handler_idx];
961 /* Unhandled opcode */
964 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
965 pToken += shader_skip_opcode(curOpcode, opcode_token, shader_version);
969 ins.handler_idx = curOpcode->handler_idx;
970 ins.flags = opcode_token & WINED3D_OPCODESPECIFICCONTROL_MASK;
971 ins.coissue = opcode_token & WINED3DSI_COISSUE;
973 /* Destination token */
974 ins.dst_count = curOpcode->dst_token ? 1 : 0;
977 DWORD param, addr_param;
978 pToken += shader_get_param(pToken, shader_version, ¶m, &addr_param);
980 if (param & WINED3DSHADER_ADDRMODE_RELATIVE)
982 shader_parse_src_param(addr_param, NULL, &dst_rel_addr);
983 shader_parse_dst_param(param, &dst_rel_addr, &dst_param);
987 shader_parse_dst_param(param, NULL, &dst_param);
991 /* Predication token */
992 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) ins.predicate = *pToken++;
994 /* Other source tokens */
995 ins.src_count = curOpcode->num_params - curOpcode->dst_token;
996 for (i = 0; i < ins.src_count; ++i)
998 DWORD param, addr_param;
999 pToken += shader_get_param(pToken, shader_version, ¶m, &addr_param);
1000 if (param & WINED3DSHADER_ADDRMODE_RELATIVE)
1002 shader_parse_src_param(addr_param, NULL, &src_rel_addr[i]);
1003 shader_parse_src_param(param, &src_rel_addr[i], &src_param[i]);
1007 shader_parse_src_param(param, NULL, &src_param[i]);
1011 /* Call appropriate function for output target */
1014 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
1015 /* FIXME: This should be internal to the shader backend.
1016 * Also, right now this is the only reason "shader_mode" exists. */
1017 if (This->baseShader.shader_mode == SHADER_GLSL) shader_glsl_add_instruction_modifiers(&ins);
1021 static void shader_dump_ins_modifiers(const DWORD output)
1023 DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1024 DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
1028 case 13: TRACE("_d8"); break;
1029 case 14: TRACE("_d4"); break;
1030 case 15: TRACE("_d2"); break;
1031 case 1: TRACE("_x2"); break;
1032 case 2: TRACE("_x4"); break;
1033 case 3: TRACE("_x8"); break;
1034 default: TRACE("_unhandled_shift(%d)", shift); break;
1037 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
1038 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
1039 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
1041 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
1043 FIXME("_unrecognized_modifier(%#x)", mmask);
1046 void shader_trace_init(const DWORD *pFunction, const SHADER_OPCODE *opcode_table)
1048 const DWORD* pToken = pFunction;
1049 const SHADER_OPCODE* curOpcode = NULL;
1050 DWORD shader_version;
1054 TRACE("Parsing %p\n", pFunction);
1056 /* The version token is supposed to be the first token */
1057 if (!shader_is_version_token(*pToken))
1059 FIXME("First token is not a version token, invalid shader.\n");
1062 shader_version = *pToken++;
1063 TRACE("%s_%u_%u\n", shader_is_pshader_version(shader_version) ? "ps": "vs",
1064 WINED3DSHADER_VERSION_MAJOR(shader_version), WINED3DSHADER_VERSION_MINOR(shader_version));
1066 while (WINED3DVS_END() != *pToken)
1068 if (shader_is_comment(*pToken)) /* comment */
1070 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
1072 TRACE("//%s\n", (const char*)pToken);
1073 pToken += comment_len;
1076 opcode_token = *pToken++;
1077 curOpcode = shader_get_opcode(opcode_table, shader_version, opcode_token);
1082 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
1083 tokens_read = shader_skip_unrecognized(pToken, shader_version);
1084 pToken += tokens_read;
1088 if (curOpcode->opcode == WINED3DSIO_DCL)
1090 DWORD usage = *pToken;
1091 DWORD param = *(pToken + 1);
1093 shader_dump_decl_usage(usage, param, shader_version);
1094 shader_dump_ins_modifiers(param);
1096 shader_dump_param(param, 0, 0, shader_version);
1099 else if (curOpcode->opcode == WINED3DSIO_DEF)
1101 unsigned int offset = shader_get_float_offset(*pToken);
1103 TRACE("def c%u = %f, %f, %f, %f", offset,
1104 *(const float *)(pToken + 1),
1105 *(const float *)(pToken + 2),
1106 *(const float *)(pToken + 3),
1107 *(const float *)(pToken + 4));
1110 else if (curOpcode->opcode == WINED3DSIO_DEFI)
1112 TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
1119 else if (curOpcode->opcode == WINED3DSIO_DEFB)
1121 TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
1122 *(pToken + 1)? "true": "false");
1127 DWORD param, addr_token = 0;
1130 /* Print out predication source token first - it follows
1131 * the destination token. */
1132 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
1135 shader_dump_param(*(pToken + 2), 0, 1, shader_version);
1138 if (opcode_token & WINED3DSI_COISSUE)
1140 /* PixWin marks instructions with the coissue flag with a '+' */
1144 TRACE("%s", curOpcode->name);
1146 if (curOpcode->opcode == WINED3DSIO_IFC
1147 || curOpcode->opcode == WINED3DSIO_BREAKC)
1149 DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1153 case COMPARISON_GT: TRACE("_gt"); break;
1154 case COMPARISON_EQ: TRACE("_eq"); break;
1155 case COMPARISON_GE: TRACE("_ge"); break;
1156 case COMPARISON_LT: TRACE("_lt"); break;
1157 case COMPARISON_NE: TRACE("_ne"); break;
1158 case COMPARISON_LE: TRACE("_le"); break;
1159 default: TRACE("_(%u)", op);
1162 else if (curOpcode->opcode == WINED3DSIO_TEX
1163 && shader_version >= WINED3DPS_VERSION(2,0)
1164 && (opcode_token & WINED3DSI_TEXLD_PROJECT))
1169 /* Destination token */
1170 if (curOpcode->dst_token)
1172 tokens_read = shader_get_param(pToken, shader_version, ¶m, &addr_token);
1173 pToken += tokens_read;
1175 shader_dump_ins_modifiers(param);
1177 shader_dump_param(param, addr_token, 0, shader_version);
1180 /* Predication token - already printed out, just skip it */
1181 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
1186 /* Other source tokens */
1187 for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i)
1189 tokens_read = shader_get_param(pToken, shader_version, ¶m, &addr_token);
1190 pToken += tokens_read;
1192 TRACE((i == 0)? " " : ", ");
1193 shader_dump_param(param, addr_token, 1, shader_version);
1201 void shader_cleanup(IWineD3DBaseShader *iface)
1203 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1205 ((IWineD3DDeviceImpl *)This->baseShader.device)->shader_backend->shader_destroy(iface);
1206 HeapFree(GetProcessHeap(), 0, This->baseShader.function);
1207 shader_delete_constant_list(&This->baseShader.constantsF);
1208 shader_delete_constant_list(&This->baseShader.constantsB);
1209 shader_delete_constant_list(&This->baseShader.constantsI);
1210 list_remove(&This->baseShader.shader_list_entry);
1213 static const SHADER_HANDLER shader_none_instruction_handler_table[WINED3DSIH_TABLE_SIZE] = {0};
1214 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
1215 static void shader_none_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {}
1216 static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1217 static void shader_none_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1218 static void shader_none_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1219 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1220 static void shader_none_load_np2fixup_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1221 static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1222 static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1223 static void shader_none_free(IWineD3DDevice *iface) {}
1224 static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1225 static GLuint shader_none_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer, const struct ps_compile_args *args) {
1226 FIXME("NONE shader backend asked to generate a pixel shader\n");
1229 static GLuint shader_none_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer, const struct vs_compile_args *args) {
1230 FIXME("NONE shader backend asked to generate a vertex shader\n");
1234 #define GLINFO_LOCATION (*gl_info)
1235 static void shader_none_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
1237 /* Set the shader caps to 0 for the none shader backend */
1238 pCaps->VertexShaderVersion = 0;
1239 pCaps->PixelShaderVersion = 0;
1240 pCaps->PixelShader1xMaxValue = 0.0;
1242 #undef GLINFO_LOCATION
1243 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1245 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1247 TRACE("Checking support for fixup:\n");
1248 dump_color_fixup_desc(fixup);
1251 /* Faked to make some apps happy. */
1252 if (!is_yuv_fixup(fixup))
1258 TRACE("[FAILED]\n");
1262 const shader_backend_t none_shader_backend = {
1263 shader_none_instruction_handler_table,
1265 shader_none_select_depth_blt,
1266 shader_none_deselect_depth_blt,
1267 shader_none_update_float_vertex_constants,
1268 shader_none_update_float_pixel_constants,
1269 shader_none_load_constants,
1270 shader_none_load_np2fixup_constants,
1271 shader_none_destroy,
1274 shader_none_dirty_const,
1275 shader_none_generate_pshader,
1276 shader_none_generate_vshader,
1277 shader_none_get_caps,
1278 shader_none_color_fixup_supported,