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 #define GLNAME_REQUIRE_GLSL ((const char *)1)
36 static void shader_dump_param(const DWORD param, const DWORD addr_token, int input, DWORD shader_version);
38 static inline BOOL shader_is_version_token(DWORD token) {
39 return shader_is_pshader_version(token) ||
40 shader_is_vshader_version(token);
43 void shader_buffer_init(struct SHADER_BUFFER *buffer)
45 buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
46 buffer->buffer[0] = '\0';
49 buffer->newline = TRUE;
52 void shader_buffer_free(struct SHADER_BUFFER *buffer)
54 HeapFree(GetProcessHeap(), 0, buffer->buffer);
58 SHADER_BUFFER* buffer,
59 const char *format, ...) {
61 char* base = buffer->buffer + buffer->bsize;
65 va_start(args, format);
66 rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
69 if (rc < 0 || /* C89 */
70 rc > SHADER_PGMSIZE - 1 - buffer->bsize) { /* C99 */
72 ERR("The buffer allocated for the shader program string "
73 "is too small at %d bytes.\n", SHADER_PGMSIZE);
74 buffer->bsize = SHADER_PGMSIZE - 1;
78 if (buffer->newline) {
79 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
80 buffer->newline = FALSE;
86 if (buffer->buffer[buffer->bsize-1] == '\n') {
88 buffer->newline = TRUE;
93 const SHADER_OPCODE *shader_get_opcode(const SHADER_OPCODE *opcode_table, DWORD shader_version, DWORD code)
97 /** TODO: use dichotomic search */
98 while (opcode_table[i].name)
100 if ((code & WINED3DSI_OPCODE_MASK) == opcode_table[i].opcode
101 && shader_version >= opcode_table[i].min_version
102 && (!opcode_table[i].max_version || shader_version <= opcode_table[i].max_version))
104 return &opcode_table[i];
109 FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n",
110 code, code, code & WINED3DSI_OPCODE_MASK, shader_version);
115 /* Read a parameter opcode from the input stream,
116 * and possibly a relative addressing token.
117 * Return the number of tokens read */
118 static int shader_get_param(const DWORD *pToken, DWORD shader_version, DWORD *param, DWORD *addr_token)
120 /* PS >= 3.0 have relative addressing (with token)
121 * VS >= 2.0 have relative addressing (with token)
122 * VS >= 1.0 < 2.0 have relative addressing (without token)
123 * The version check below should work in general */
125 char rel_token = WINED3DSHADER_VERSION_MAJOR(shader_version) >= 2 &&
126 ((*pToken & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
129 *addr_token = rel_token? *(pToken + 1): 0;
130 return rel_token? 2:1;
133 /* Return the number of parameters to skip for an opcode */
134 static inline int shader_skip_opcode(const SHADER_OPCODE *curOpcode, DWORD opcode_token, DWORD shader_version)
136 /* Shaders >= 2.0 may contain address tokens, but fortunately they
137 * have a useful length mask - use it here. Shaders 1.0 contain no such tokens */
138 return (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 2)
139 ? ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT) : curOpcode->num_params;
142 /* Read the parameters of an unrecognized opcode from the input stream
143 * Return the number of tokens read.
145 * Note: This function assumes source or destination token format.
146 * It will not work with specially-formatted tokens like DEF or DCL,
147 * but hopefully those would be recognized */
148 static int shader_skip_unrecognized(const DWORD *pToken, DWORD shader_version)
153 /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
154 while (*pToken & 0x80000000) {
156 DWORD param, addr_token;
157 tokens_read += shader_get_param(pToken, shader_version, ¶m, &addr_token);
158 pToken += tokens_read;
160 FIXME("Unrecognized opcode param: token=0x%08x "
161 "addr_token=0x%08x name=", param, addr_token);
162 shader_dump_param(param, addr_token, i, shader_version);
169 /* Convert floating point offset relative
170 * to a register file to an absolute offset for float constants */
171 static unsigned int shader_get_float_offset(const DWORD reg)
173 unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
174 int regtype = shader_get_regtype(reg);
177 case WINED3DSPR_CONST: return regnum;
178 case WINED3DSPR_CONST2: return 2048 + regnum;
179 case WINED3DSPR_CONST3: return 4096 + regnum;
180 case WINED3DSPR_CONST4: return 6144 + regnum;
182 FIXME("Unsupported register type: %d\n", regtype);
187 static void shader_delete_constant_list(struct list* clist) {
190 struct local_constant* constant;
192 ptr = list_head(clist);
194 constant = LIST_ENTRY(ptr, struct local_constant, entry);
195 ptr = list_next(clist, ptr);
196 HeapFree(GetProcessHeap(), 0, constant);
201 /* Note that this does not count the loop register
202 * as an address register. */
204 HRESULT shader_get_registers_used(IWineD3DBaseShader *iface, struct shader_reg_maps *reg_maps,
205 struct semantic *semantics_in, struct semantic *semantics_out, const DWORD *byte_code)
207 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
208 const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;
209 DWORD shader_version;
210 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
211 const DWORD* pToken = byte_code;
214 /* There are some minor differences between pixel and vertex shaders */
216 memset(reg_maps->bumpmat, 0, sizeof(reg_maps->bumpmat));
217 memset(reg_maps->luminanceparams, 0, sizeof(reg_maps->luminanceparams));
219 /* get_registers_used is called on every compile on some 1.x shaders, which can result
220 * in stacking up a collection of local constants. Delete the old constants if existing
222 shader_delete_constant_list(&This->baseShader.constantsF);
223 shader_delete_constant_list(&This->baseShader.constantsB);
224 shader_delete_constant_list(&This->baseShader.constantsI);
226 /* The version token is supposed to be the first token */
227 if (!shader_is_version_token(*pToken))
229 FIXME("First token is not a version token, invalid shader.\n");
230 return WINED3DERR_INVALIDCALL;
232 reg_maps->shader_version = shader_version = *pToken++;
233 pshader = shader_is_pshader_version(shader_version);
235 while (WINED3DVS_END() != *pToken) {
236 CONST SHADER_OPCODE* curOpcode;
240 if (shader_is_comment(*pToken))
242 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
244 pToken += comment_len;
249 opcode_token = *pToken++;
250 curOpcode = shader_get_opcode(shader_ins, shader_version, opcode_token);
252 /* Unhandled opcode, and its parameters */
253 if (NULL == curOpcode) {
254 while (*pToken & 0x80000000)
257 /* Handle declarations */
258 } else if (WINED3DSIO_DCL == curOpcode->opcode) {
260 DWORD usage = *pToken++;
261 DWORD param = *pToken++;
262 DWORD regtype = shader_get_regtype(param);
263 unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
265 /* Vshader: mark attributes used
266 Pshader: mark 3.0 input registers used, save token */
267 if (WINED3DSPR_INPUT == regtype) {
270 reg_maps->attributes[regnum] = 1;
272 reg_maps->packed_input[regnum] = 1;
274 semantics_in[regnum].usage = usage;
275 semantics_in[regnum].reg = param;
277 /* Vshader: mark 3.0 output registers used, save token */
278 } else if (WINED3DSPR_OUTPUT == regtype) {
279 reg_maps->packed_output[regnum] = 1;
280 semantics_out[regnum].usage = usage;
281 semantics_out[regnum].reg = param;
282 if (usage & (WINED3DDECLUSAGE_FOG << WINED3DSP_DCL_USAGE_SHIFT))
285 /* Save sampler usage token */
286 } else if (WINED3DSPR_SAMPLER == regtype)
287 reg_maps->samplers[regnum] = usage;
289 } else if (WINED3DSIO_DEF == curOpcode->opcode) {
291 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
292 if (!lconst) return E_OUTOFMEMORY;
293 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
294 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
296 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
297 if (WINED3DSHADER_VERSION_MAJOR(shader_version) == 1 && pshader)
299 float *value = (float *) lconst->value;
300 if(value[0] < -1.0) value[0] = -1.0;
301 else if(value[0] > 1.0) value[0] = 1.0;
302 if(value[1] < -1.0) value[1] = -1.0;
303 else if(value[1] > 1.0) value[1] = 1.0;
304 if(value[2] < -1.0) value[2] = -1.0;
305 else if(value[2] > 1.0) value[2] = 1.0;
306 if(value[3] < -1.0) value[3] = -1.0;
307 else if(value[3] > 1.0) value[3] = 1.0;
310 list_add_head(&This->baseShader.constantsF, &lconst->entry);
311 pToken += curOpcode->num_params;
313 } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
315 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
316 if (!lconst) return E_OUTOFMEMORY;
317 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
318 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
319 list_add_head(&This->baseShader.constantsI, &lconst->entry);
320 pToken += curOpcode->num_params;
322 } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
324 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
325 if (!lconst) return E_OUTOFMEMORY;
326 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
327 memcpy(lconst->value, pToken + 1, 1 * sizeof(DWORD));
328 list_add_head(&This->baseShader.constantsB, &lconst->entry);
329 pToken += curOpcode->num_params;
331 /* If there's a loop in the shader */
332 } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
333 WINED3DSIO_REP == curOpcode->opcode) {
335 if(cur_loop_depth > max_loop_depth)
336 max_loop_depth = cur_loop_depth;
337 pToken += curOpcode->num_params;
339 /* Rep and Loop always use an integer constant for the control parameters */
340 This->baseShader.uses_int_consts = TRUE;
341 } else if (WINED3DSIO_ENDLOOP == curOpcode->opcode ||
342 WINED3DSIO_ENDREP == curOpcode->opcode) {
345 /* For subroutine prototypes */
346 } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
348 DWORD snum = *pToken & WINED3DSP_REGNUM_MASK;
349 reg_maps->labels[snum] = 1;
350 pToken += curOpcode->num_params;
352 /* Set texture, address, temporary registers */
356 /* Declare 1.X samplers implicitly, based on the destination reg. number */
357 if (WINED3DSHADER_VERSION_MAJOR(shader_version) == 1
358 && pshader /* Filter different instructions with the same enum values in VS */
359 && (WINED3DSIO_TEX == curOpcode->opcode
360 || WINED3DSIO_TEXBEM == curOpcode->opcode
361 || WINED3DSIO_TEXBEML == curOpcode->opcode
362 || WINED3DSIO_TEXDP3TEX == curOpcode->opcode
363 || WINED3DSIO_TEXM3x2TEX == curOpcode->opcode
364 || WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode
365 || WINED3DSIO_TEXM3x3TEX == curOpcode->opcode
366 || WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode
367 || WINED3DSIO_TEXREG2AR == curOpcode->opcode
368 || WINED3DSIO_TEXREG2GB == curOpcode->opcode
369 || WINED3DSIO_TEXREG2RGB == curOpcode->opcode))
371 /* Fake sampler usage, only set reserved bit and ttype */
372 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
374 TRACE("Setting fake 2D sampler for 1.x pixelshader\n");
375 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
377 /* texbem is only valid with < 1.4 pixel shaders */
378 if(WINED3DSIO_TEXBEM == curOpcode->opcode ||
379 WINED3DSIO_TEXBEML == curOpcode->opcode) {
380 reg_maps->bumpmat[sampler_code] = TRUE;
381 if(WINED3DSIO_TEXBEML == curOpcode->opcode) {
382 reg_maps->luminanceparams[sampler_code] = TRUE;
386 if(WINED3DSIO_NRM == curOpcode->opcode) {
387 reg_maps->usesnrm = 1;
388 } else if(WINED3DSIO_BEM == curOpcode->opcode && pshader) {
389 DWORD regnum = *pToken & WINED3DSP_REGNUM_MASK;
390 reg_maps->bumpmat[regnum] = TRUE;
391 } else if(WINED3DSIO_DSY == curOpcode->opcode) {
392 reg_maps->usesdsy = 1;
395 /* This will loop over all the registers and try to
396 * make a bitmask of the ones we're interested in.
398 * Relative addressing tokens are ignored, but that's
399 * okay, since we'll catch any address registers when
400 * they are initialized (required by spec) */
402 limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
403 curOpcode->num_params + 1: curOpcode->num_params;
405 for (i = 0; i < limit; ++i) {
407 DWORD param, addr_token, reg, regtype;
408 pToken += shader_get_param(pToken, shader_version, ¶m, &addr_token);
410 regtype = shader_get_regtype(param);
411 reg = param & WINED3DSP_REGNUM_MASK;
413 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
416 reg_maps->texcoord[reg] = 1;
418 reg_maps->address[reg] = 1;
421 else if (WINED3DSPR_TEMP == regtype)
422 reg_maps->temporary[reg] = 1;
424 else if (WINED3DSPR_INPUT == regtype) {
426 reg_maps->attributes[reg] = 1;
428 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
429 /* If relative addressing is used, we must assume that all registers
430 * are used. Even if it is a construct like v3[aL], we can't assume
431 * that v0, v1 and v2 aren't read because aL can be negative
434 for(i = 0; i < MAX_REG_INPUT; i++) {
435 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[i] = TRUE;
438 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[reg] = TRUE;
443 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
446 else if (WINED3DSPR_MISCTYPE == regtype && reg == 0 && pshader)
449 else if(WINED3DSPR_CONST == regtype) {
450 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
452 if(reg <= ((IWineD3DVertexShaderImpl *) This)->min_rel_offset) {
453 ((IWineD3DVertexShaderImpl *) This)->min_rel_offset = reg;
454 } else if(reg >= ((IWineD3DVertexShaderImpl *) This)->max_rel_offset) {
455 ((IWineD3DVertexShaderImpl *) This)->max_rel_offset = reg;
458 reg_maps->usesrelconstF = TRUE;
461 else if(WINED3DSPR_CONSTINT == regtype) {
462 This->baseShader.uses_int_consts = TRUE;
464 else if(WINED3DSPR_CONSTBOOL == regtype) {
465 This->baseShader.uses_bool_consts = TRUE;
468 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and is used
469 * in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel shaders because TECRDOUT
470 * isn't used in them, but future register types might cause issues
472 else if (WINED3DSPR_TEXCRDOUT == regtype && i == 0 /* Only look at writes */
473 && !pshader && WINED3DSHADER_VERSION_MAJOR(shader_version) < 3)
475 reg_maps->texcoord_mask[reg] |= shader_get_writemask(param);
481 reg_maps->loop_depth = max_loop_depth;
483 This->baseShader.functionLength = ((char *)pToken - (char *)byte_code);
488 static void shader_dump_decl_usage(DWORD decl, DWORD param, DWORD shader_version)
490 DWORD regtype = shader_get_regtype(param);
494 if (regtype == WINED3DSPR_SAMPLER) {
495 DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
498 case WINED3DSTT_2D: TRACE("_2d"); break;
499 case WINED3DSTT_CUBE: TRACE("_cube"); break;
500 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
501 default: TRACE("_unknown_ttype(0x%08x)", ttype);
506 DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
507 DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
509 /* Pixel shaders 3.0 don't have usage semantics */
510 if (shader_is_pshader_version(shader_version) && shader_version < WINED3DPS_VERSION(3,0))
516 case WINED3DDECLUSAGE_POSITION:
517 TRACE("position%d", idx);
519 case WINED3DDECLUSAGE_BLENDINDICES:
522 case WINED3DDECLUSAGE_BLENDWEIGHT:
525 case WINED3DDECLUSAGE_NORMAL:
526 TRACE("normal%d", idx);
528 case WINED3DDECLUSAGE_PSIZE:
531 case WINED3DDECLUSAGE_COLOR:
535 TRACE("specular%d", (idx - 1));
538 case WINED3DDECLUSAGE_TEXCOORD:
539 TRACE("texture%d", idx);
541 case WINED3DDECLUSAGE_TANGENT:
544 case WINED3DDECLUSAGE_BINORMAL:
547 case WINED3DDECLUSAGE_TESSFACTOR:
550 case WINED3DDECLUSAGE_POSITIONT:
551 TRACE("positionT%d", idx);
553 case WINED3DDECLUSAGE_FOG:
556 case WINED3DDECLUSAGE_DEPTH:
559 case WINED3DDECLUSAGE_SAMPLE:
563 FIXME("unknown_semantics(0x%08x)", usage);
568 static void shader_dump_arr_entry(const DWORD param, const DWORD addr_token,
569 unsigned int reg, int input, DWORD shader_version)
572 ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
577 shader_dump_param(addr_token, 0, input, shader_version);
587 static void shader_dump_param(const DWORD param, const DWORD addr_token, int input, DWORD shader_version)
589 static const char * const rastout_reg_names[] = { "oPos", "oFog", "oPts" };
590 static const char * const misctype_reg_names[] = { "vPos", "vFace"};
591 const char *swizzle_reg_chars = "xyzw";
593 DWORD reg = param & WINED3DSP_REGNUM_MASK;
594 DWORD regtype = shader_get_regtype(param);
595 DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
597 /* There are some minor differences between pixel and vertex shaders */
598 char pshader = shader_is_pshader_version(shader_version);
601 if ( (modifier == WINED3DSPSM_NEG) ||
602 (modifier == WINED3DSPSM_BIASNEG) ||
603 (modifier == WINED3DSPSM_SIGNNEG) ||
604 (modifier == WINED3DSPSM_X2NEG) ||
605 (modifier == WINED3DSPSM_ABSNEG) )
607 else if (modifier == WINED3DSPSM_COMP)
609 else if (modifier == WINED3DSPSM_NOT)
612 if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG)
617 case WINED3DSPR_TEMP:
620 case WINED3DSPR_INPUT:
622 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
624 case WINED3DSPR_CONST:
625 case WINED3DSPR_CONST2:
626 case WINED3DSPR_CONST3:
627 case WINED3DSPR_CONST4:
629 shader_dump_arr_entry(param, addr_token, shader_get_float_offset(param), input, shader_version);
631 case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
632 TRACE("%c%u", (pshader? 't':'a'), reg);
634 case WINED3DSPR_RASTOUT:
635 TRACE("%s", rastout_reg_names[reg]);
637 case WINED3DSPR_COLOROUT:
640 case WINED3DSPR_DEPTHOUT:
643 case WINED3DSPR_ATTROUT:
646 case WINED3DSPR_TEXCRDOUT:
648 /* Vertex shaders >= 3.0 use general purpose output registers
649 * (WINED3DSPR_OUTPUT), which can include an address token */
651 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3) {
653 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
658 case WINED3DSPR_CONSTINT:
660 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
662 case WINED3DSPR_CONSTBOOL:
664 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
666 case WINED3DSPR_LABEL:
669 case WINED3DSPR_LOOP:
672 case WINED3DSPR_SAMPLER:
675 case WINED3DSPR_MISCTYPE:
677 FIXME("Unhandled misctype register %d\n", reg);
679 TRACE("%s", misctype_reg_names[reg]);
682 case WINED3DSPR_PREDICATE:
686 TRACE("unhandled_rtype(%#x)", regtype);
691 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
693 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
695 if (param & WINED3DSP_WRITEMASK_0) TRACE("%c", swizzle_reg_chars[0]);
696 if (param & WINED3DSP_WRITEMASK_1) TRACE("%c", swizzle_reg_chars[1]);
697 if (param & WINED3DSP_WRITEMASK_2) TRACE("%c", swizzle_reg_chars[2]);
698 if (param & WINED3DSP_WRITEMASK_3) TRACE("%c", swizzle_reg_chars[3]);
703 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
704 DWORD swizzle_x = swizzle & 0x03;
705 DWORD swizzle_y = (swizzle >> 2) & 0x03;
706 DWORD swizzle_z = (swizzle >> 4) & 0x03;
707 DWORD swizzle_w = (swizzle >> 6) & 0x03;
711 case WINED3DSPSM_NONE: break;
712 case WINED3DSPSM_NEG: break;
713 case WINED3DSPSM_NOT: break;
714 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
715 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
716 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
717 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
718 case WINED3DSPSM_COMP: break;
719 case WINED3DSPSM_X2: TRACE("_x2"); break;
720 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
721 case WINED3DSPSM_DZ: TRACE("_dz"); break;
722 case WINED3DSPSM_DW: TRACE("_dw"); break;
723 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
724 case WINED3DSPSM_ABS: TRACE(")"); break;
726 TRACE("_unknown_modifier(%#x)", modifier >> WINED3DSP_SRCMOD_SHIFT);
731 * swizzle bits fields:
734 if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) != swizzle) {
735 if (swizzle_x == swizzle_y &&
736 swizzle_x == swizzle_z &&
737 swizzle_x == swizzle_w) {
738 TRACE(".%c", swizzle_reg_chars[swizzle_x]);
741 swizzle_reg_chars[swizzle_x],
742 swizzle_reg_chars[swizzle_y],
743 swizzle_reg_chars[swizzle_z],
744 swizzle_reg_chars[swizzle_w]);
750 static void shader_color_correction(IWineD3DBaseShaderImpl *shader,
751 IWineD3DDeviceImpl *device, const struct SHADER_OPCODE_ARG *arg, DWORD shader_version)
753 IWineD3DBaseTextureImpl *texture;
754 struct color_fixup_desc fixup;
755 BOOL recorded = FALSE;
759 switch(arg->opcode->opcode)
762 if (WINED3DSHADER_VERSION_MAJOR(shader_version) < 2) sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
763 else sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
766 case WINED3DSIO_TEXLDL:
767 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
770 case WINED3DSIO_TEXDP3TEX:
771 case WINED3DSIO_TEXM3x3TEX:
772 case WINED3DSIO_TEXM3x3SPEC:
773 case WINED3DSIO_TEXM3x3VSPEC:
774 case WINED3DSIO_TEXBEM:
775 case WINED3DSIO_TEXREG2AR:
776 case WINED3DSIO_TEXREG2GB:
777 case WINED3DSIO_TEXREG2RGB:
778 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
782 /* Not a texture sampling instruction, nothing to do */
786 texture = (IWineD3DBaseTextureImpl *)device->stateBlock->textures[sampler_idx];
787 if (texture) fixup = texture->baseTexture.shader_color_fixup;
788 else fixup = COLOR_FIXUP_IDENTITY;
790 /* before doing anything, record the sampler with the format in the format conversion list,
791 * but check if it's not there already */
792 for (i = 0; i < shader->baseShader.num_sampled_samplers; ++i)
794 if (shader->baseShader.sampled_samplers[i] == sampler_idx)
803 shader->baseShader.sampled_samplers[shader->baseShader.num_sampled_samplers] = sampler_idx;
804 ++shader->baseShader.num_sampled_samplers;
807 device->shader_backend->shader_color_correction(arg, fixup);
810 /* Shared code in order to generate the bulk of the shader string.
811 * NOTE: A description of how to parse tokens can be found on msdn */
812 void shader_generate_main(IWineD3DBaseShader *iface, SHADER_BUFFER* buffer,
813 const shader_reg_maps* reg_maps, CONST DWORD* pFunction)
815 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
816 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device; /* To access shader backend callbacks */
817 const SHADER_OPCODE *opcode_table = This->baseShader.shader_ins;
818 const SHADER_HANDLER *handler_table = device->shader_backend->shader_instruction_handler_table;
819 DWORD shader_version = reg_maps->shader_version;
820 const DWORD *pToken = pFunction;
821 const SHADER_OPCODE *curOpcode;
822 SHADER_HANDLER hw_fct;
824 SHADER_OPCODE_ARG hw_arg;
826 /* Initialize current parsing state */
827 hw_arg.shader = iface;
828 hw_arg.buffer = buffer;
829 hw_arg.reg_maps = reg_maps;
830 This->baseShader.parse_state.current_row = 0;
832 while (WINED3DPS_END() != *pToken)
834 /* Skip version token */
835 if (shader_is_version_token(*pToken))
841 /* Skip comment tokens */
842 if (shader_is_comment(*pToken))
844 pToken += (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
850 hw_arg.opcode_token = *pToken++;
851 curOpcode = shader_get_opcode(opcode_table, shader_version, hw_arg.opcode_token);
853 /* Unknown opcode and its parameters */
856 FIXME("Unrecognized opcode: token=0x%08x\n", hw_arg.opcode_token);
857 pToken += shader_skip_unrecognized(pToken, shader_version);
862 if (WINED3DSIO_DCL == curOpcode->opcode
863 || WINED3DSIO_NOP == curOpcode->opcode
864 || WINED3DSIO_DEF == curOpcode->opcode
865 || WINED3DSIO_DEFI == curOpcode->opcode
866 || WINED3DSIO_DEFB == curOpcode->opcode
867 || WINED3DSIO_PHASE == curOpcode->opcode
868 || WINED3DSIO_RET == curOpcode->opcode)
870 pToken += shader_skip_opcode(curOpcode, hw_arg.opcode_token, shader_version);
875 hw_fct = handler_table[curOpcode->handler_idx];
877 /* Unhandled opcode */
880 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
881 pToken += shader_skip_opcode(curOpcode, hw_arg.opcode_token, shader_version);
885 hw_arg.opcode = curOpcode;
887 /* Destination token */
888 if (curOpcode->dst_token)
890 DWORD param, addr_token = 0;
891 pToken += shader_get_param(pToken, shader_version, ¶m, &addr_token);
893 hw_arg.dst_addr = addr_token;
896 /* Predication token */
897 if (hw_arg.opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) hw_arg.predicate = *pToken++;
899 /* Other source tokens */
900 for (i = 0; i < (curOpcode->num_params - curOpcode->dst_token); ++i)
902 DWORD param, addr_token = 0;
903 pToken += shader_get_param(pToken, shader_version, ¶m, &addr_token);
904 hw_arg.src[i] = param;
905 hw_arg.src_addr[i] = addr_token;
908 /* Call appropriate function for output target */
911 /* Add color correction if needed */
912 shader_color_correction(This, device, &hw_arg, shader_version);
914 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
915 /* FIXME: This should be internal to the shader backend.
916 * Also, right now this is the only reason "shader_mode" exists. */
917 if (This->baseShader.shader_mode == SHADER_GLSL) shader_glsl_add_instruction_modifiers(&hw_arg);
921 static void shader_dump_ins_modifiers(const DWORD output)
923 DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
924 DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
928 case 13: TRACE("_d8"); break;
929 case 14: TRACE("_d4"); break;
930 case 15: TRACE("_d2"); break;
931 case 1: TRACE("_x2"); break;
932 case 2: TRACE("_x4"); break;
933 case 3: TRACE("_x8"); break;
934 default: TRACE("_unhandled_shift(%d)", shift); break;
937 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
938 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
939 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
941 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
943 FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
946 void shader_trace_init(const DWORD *pFunction, const SHADER_OPCODE *opcode_table)
948 const DWORD* pToken = pFunction;
949 const SHADER_OPCODE* curOpcode = NULL;
950 DWORD shader_version;
954 TRACE("Parsing %p\n", pFunction);
956 /* The version token is supposed to be the first token */
957 if (!shader_is_version_token(*pToken))
959 FIXME("First token is not a version token, invalid shader.\n");
962 shader_version = *pToken++;
963 TRACE("%s_%u_%u\n", shader_is_pshader_version(shader_version) ? "ps": "vs",
964 WINED3DSHADER_VERSION_MAJOR(shader_version), WINED3DSHADER_VERSION_MINOR(shader_version));
966 while (WINED3DVS_END() != *pToken)
968 if (shader_is_comment(*pToken)) /* comment */
970 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
972 TRACE("//%s\n", (const char*)pToken);
973 pToken += comment_len;
976 opcode_token = *pToken++;
977 curOpcode = shader_get_opcode(opcode_table, shader_version, opcode_token);
982 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
983 tokens_read = shader_skip_unrecognized(pToken, shader_version);
984 pToken += tokens_read;
988 if (curOpcode->opcode == WINED3DSIO_DCL)
990 DWORD usage = *pToken;
991 DWORD param = *(pToken + 1);
993 shader_dump_decl_usage(usage, param, shader_version);
994 shader_dump_ins_modifiers(param);
996 shader_dump_param(param, 0, 0, shader_version);
999 else if (curOpcode->opcode == WINED3DSIO_DEF)
1001 unsigned int offset = shader_get_float_offset(*pToken);
1003 TRACE("def c%u = %f, %f, %f, %f", offset,
1004 *(const float *)(pToken + 1),
1005 *(const float *)(pToken + 2),
1006 *(const float *)(pToken + 3),
1007 *(const float *)(pToken + 4));
1010 else if (curOpcode->opcode == WINED3DSIO_DEFI)
1012 TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
1019 else if (curOpcode->opcode == WINED3DSIO_DEFB)
1021 TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
1022 *(pToken + 1)? "true": "false");
1027 DWORD param, addr_token;
1030 /* Print out predication source token first - it follows
1031 * the destination token. */
1032 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
1035 shader_dump_param(*(pToken + 2), 0, 1, shader_version);
1038 if (opcode_token & WINED3DSI_COISSUE)
1040 /* PixWin marks instructions with the coissue flag with a '+' */
1044 TRACE("%s", curOpcode->name);
1046 if (curOpcode->opcode == WINED3DSIO_IFC
1047 || curOpcode->opcode == WINED3DSIO_BREAKC)
1049 DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1053 case COMPARISON_GT: TRACE("_gt"); break;
1054 case COMPARISON_EQ: TRACE("_eq"); break;
1055 case COMPARISON_GE: TRACE("_ge"); break;
1056 case COMPARISON_LT: TRACE("_lt"); break;
1057 case COMPARISON_NE: TRACE("_ne"); break;
1058 case COMPARISON_LE: TRACE("_le"); break;
1059 default: TRACE("_(%u)", op);
1062 else if (curOpcode->opcode == WINED3DSIO_TEX
1063 && shader_version >= WINED3DPS_VERSION(2,0)
1064 && (opcode_token & WINED3DSI_TEXLD_PROJECT))
1069 /* Destination token */
1070 if (curOpcode->dst_token)
1072 tokens_read = shader_get_param(pToken, shader_version, ¶m, &addr_token);
1073 pToken += tokens_read;
1075 shader_dump_ins_modifiers(param);
1077 shader_dump_param(param, addr_token, 0, shader_version);
1080 /* Predication token - already printed out, just skip it */
1081 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
1086 /* Other source tokens */
1087 for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i)
1089 tokens_read = shader_get_param(pToken, shader_version, ¶m, &addr_token);
1090 pToken += tokens_read;
1092 TRACE((i == 0)? " " : ", ");
1093 shader_dump_param(param, addr_token, 1, shader_version);
1101 void shader_cleanup(IWineD3DBaseShader *iface)
1103 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1105 ((IWineD3DDeviceImpl *)This->baseShader.device)->shader_backend->shader_destroy(iface);
1106 HeapFree(GetProcessHeap(), 0, This->baseShader.function);
1107 shader_delete_constant_list(&This->baseShader.constantsF);
1108 shader_delete_constant_list(&This->baseShader.constantsB);
1109 shader_delete_constant_list(&This->baseShader.constantsI);
1110 list_remove(&This->baseShader.shader_list_entry);
1113 static const SHADER_HANDLER shader_none_instruction_handler_table[WINED3DSIH_TABLE_SIZE] = {0};
1114 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
1115 static void shader_none_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {}
1116 static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1117 static void shader_none_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1118 static void shader_none_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1119 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1120 static void shader_none_color_correction(const struct SHADER_OPCODE_ARG *arg, struct color_fixup_desc fixup) {}
1121 static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1122 static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1123 static void shader_none_free(IWineD3DDevice *iface) {}
1124 static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1125 static GLuint shader_none_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer, const struct ps_compile_args *args) {
1126 FIXME("NONE shader backend asked to generate a pixel shader\n");
1129 static void shader_none_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
1130 FIXME("NONE shader backend asked to generate a vertex shader\n");
1133 #define GLINFO_LOCATION (*gl_info)
1134 static void shader_none_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
1136 /* Set the shader caps to 0 for the none shader backend */
1137 pCaps->VertexShaderVersion = 0;
1138 pCaps->PixelShaderVersion = 0;
1139 pCaps->PixelShader1xMaxValue = 0.0;
1141 #undef GLINFO_LOCATION
1142 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1144 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1146 TRACE("Checking support for fixup:\n");
1147 dump_color_fixup_desc(fixup);
1150 /* Faked to make some apps happy. */
1151 if (!is_yuv_fixup(fixup))
1157 TRACE("[FAILED]\n");
1161 const shader_backend_t none_shader_backend = {
1162 shader_none_instruction_handler_table,
1164 shader_none_select_depth_blt,
1165 shader_none_deselect_depth_blt,
1166 shader_none_update_float_vertex_constants,
1167 shader_none_update_float_pixel_constants,
1168 shader_none_load_constants,
1169 shader_none_color_correction,
1170 shader_none_destroy,
1173 shader_none_dirty_const,
1174 shader_none_generate_pshader,
1175 shader_none_generate_vshader,
1176 shader_none_get_caps,
1177 shader_none_color_fixup_supported,