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);
44 SHADER_BUFFER* buffer,
45 const char *format, ...) {
47 char* base = buffer->buffer + buffer->bsize;
51 va_start(args, format);
52 rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
55 if (rc < 0 || /* C89 */
56 rc > SHADER_PGMSIZE - 1 - buffer->bsize) { /* C99 */
58 ERR("The buffer allocated for the shader program string "
59 "is too small at %d bytes.\n", SHADER_PGMSIZE);
60 buffer->bsize = SHADER_PGMSIZE - 1;
64 if (buffer->newline) {
65 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
66 buffer->newline = FALSE;
72 if (buffer->buffer[buffer->bsize-1] == '\n') {
74 buffer->newline = TRUE;
79 const SHADER_OPCODE *shader_get_opcode(const SHADER_OPCODE *opcode_table, DWORD shader_version, DWORD code)
83 /** TODO: use dichotomic search */
84 while (opcode_table[i].name)
86 if ((code & WINED3DSI_OPCODE_MASK) == opcode_table[i].opcode
87 && shader_version >= opcode_table[i].min_version
88 && (!opcode_table[i].max_version || shader_version <= opcode_table[i].max_version))
90 return &opcode_table[i];
95 FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n",
96 code, code, code & WINED3DSI_OPCODE_MASK, shader_version);
101 /* Read a parameter opcode from the input stream,
102 * and possibly a relative addressing token.
103 * Return the number of tokens read */
104 static int shader_get_param(IWineD3DBaseShader *iface, const DWORD *pToken, DWORD *param, DWORD *addr_token)
106 /* PS >= 3.0 have relative addressing (with token)
107 * VS >= 2.0 have relative addressing (with token)
108 * VS >= 1.0 < 2.0 have relative addressing (without token)
109 * The version check below should work in general */
111 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
112 char rel_token = WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2 &&
113 ((*pToken & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
116 *addr_token = rel_token? *(pToken + 1): 0;
117 return rel_token? 2:1;
120 /* Return the number of parameters to skip for an opcode */
121 static inline int shader_skip_opcode(
122 IWineD3DBaseShaderImpl* This,
123 const SHADER_OPCODE* curOpcode,
124 DWORD opcode_token) {
126 /* Shaders >= 2.0 may contain address tokens, but fortunately they
127 * have a useful length mask - use it here. Shaders 1.0 contain no such tokens */
129 return (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2)?
130 ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT):
131 curOpcode->num_params;
134 /* Read the parameters of an unrecognized opcode from the input stream
135 * Return the number of tokens read.
137 * Note: This function assumes source or destination token format.
138 * It will not work with specially-formatted tokens like DEF or DCL,
139 * but hopefully those would be recognized */
140 static int shader_skip_unrecognized(IWineD3DBaseShader *iface, const DWORD *pToken)
142 DWORD shader_version = ((IWineD3DBaseShaderImpl *)iface)->baseShader.hex_version;
146 /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
147 while (*pToken & 0x80000000) {
149 DWORD param, addr_token;
150 tokens_read += shader_get_param(iface, pToken, ¶m, &addr_token);
151 pToken += tokens_read;
153 FIXME("Unrecognized opcode param: token=0x%08x "
154 "addr_token=0x%08x name=", param, addr_token);
155 shader_dump_param(param, addr_token, i, shader_version);
162 /* Convert floating point offset relative
163 * to a register file to an absolute offset for float constants */
164 static unsigned int shader_get_float_offset(const DWORD reg)
166 unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
167 int regtype = shader_get_regtype(reg);
170 case WINED3DSPR_CONST: return regnum;
171 case WINED3DSPR_CONST2: return 2048 + regnum;
172 case WINED3DSPR_CONST3: return 4096 + regnum;
173 case WINED3DSPR_CONST4: return 6144 + regnum;
175 FIXME("Unsupported register type: %d\n", regtype);
180 static void shader_delete_constant_list(struct list* clist) {
183 struct local_constant* constant;
185 ptr = list_head(clist);
187 constant = LIST_ENTRY(ptr, struct local_constant, entry);
188 ptr = list_next(clist, ptr);
189 HeapFree(GetProcessHeap(), 0, constant);
194 /* Note that this does not count the loop register
195 * as an address register. */
197 HRESULT shader_get_registers_used(
198 IWineD3DBaseShader *iface,
199 shader_reg_maps* reg_maps,
200 semantic* semantics_in,
201 semantic* semantics_out,
203 IWineD3DStateBlockImpl *stateBlock) {
205 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
206 const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;
207 DWORD shader_version = This->baseShader.hex_version;
208 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
210 /* There are some minor differences between pixel and vertex shaders */
211 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
213 memset(reg_maps->bumpmat, 0, sizeof(reg_maps->bumpmat));
214 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 while (WINED3DVS_END() != *pToken) {
227 CONST SHADER_OPCODE* curOpcode;
231 if (shader_is_version_token(*pToken)) {
236 } else if (shader_is_comment(*pToken)) {
237 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
239 pToken += comment_len;
244 opcode_token = *pToken++;
245 curOpcode = shader_get_opcode(shader_ins, shader_version, opcode_token);
247 /* Unhandled opcode, and its parameters */
248 if (NULL == curOpcode) {
249 while (*pToken & 0x80000000)
252 /* Handle declarations */
253 } else if (WINED3DSIO_DCL == curOpcode->opcode) {
255 DWORD usage = *pToken++;
256 DWORD param = *pToken++;
257 DWORD regtype = shader_get_regtype(param);
258 unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
260 /* Vshader: mark attributes used
261 Pshader: mark 3.0 input registers used, save token */
262 if (WINED3DSPR_INPUT == regtype) {
265 reg_maps->attributes[regnum] = 1;
267 reg_maps->packed_input[regnum] = 1;
269 semantics_in[regnum].usage = usage;
270 semantics_in[regnum].reg = param;
272 /* Vshader: mark 3.0 output registers used, save token */
273 } else if (WINED3DSPR_OUTPUT == regtype) {
274 reg_maps->packed_output[regnum] = 1;
275 semantics_out[regnum].usage = usage;
276 semantics_out[regnum].reg = param;
277 if (usage & (WINED3DDECLUSAGE_FOG << WINED3DSP_DCL_USAGE_SHIFT))
280 /* Save sampler usage token */
281 } else if (WINED3DSPR_SAMPLER == regtype)
282 reg_maps->samplers[regnum] = usage;
284 } else if (WINED3DSIO_DEF == curOpcode->opcode) {
286 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
287 if (!lconst) return E_OUTOFMEMORY;
288 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
289 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
291 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
292 if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 && pshader) {
293 float *value = (float *) lconst->value;
294 if(value[0] < -1.0) value[0] = -1.0;
295 else if(value[0] > 1.0) value[0] = 1.0;
296 if(value[1] < -1.0) value[1] = -1.0;
297 else if(value[1] > 1.0) value[1] = 1.0;
298 if(value[2] < -1.0) value[2] = -1.0;
299 else if(value[2] > 1.0) value[2] = 1.0;
300 if(value[3] < -1.0) value[3] = -1.0;
301 else if(value[3] > 1.0) value[3] = 1.0;
304 list_add_head(&This->baseShader.constantsF, &lconst->entry);
305 pToken += curOpcode->num_params;
307 } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
309 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
310 if (!lconst) return E_OUTOFMEMORY;
311 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
312 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
313 list_add_head(&This->baseShader.constantsI, &lconst->entry);
314 pToken += curOpcode->num_params;
316 } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
318 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
319 if (!lconst) return E_OUTOFMEMORY;
320 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
321 memcpy(lconst->value, pToken + 1, 1 * sizeof(DWORD));
322 list_add_head(&This->baseShader.constantsB, &lconst->entry);
323 pToken += curOpcode->num_params;
325 /* If there's a loop in the shader */
326 } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
327 WINED3DSIO_REP == curOpcode->opcode) {
329 if(cur_loop_depth > max_loop_depth)
330 max_loop_depth = cur_loop_depth;
331 pToken += curOpcode->num_params;
333 } else if (WINED3DSIO_ENDLOOP == curOpcode->opcode ||
334 WINED3DSIO_ENDREP == curOpcode->opcode) {
337 /* For subroutine prototypes */
338 } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
340 DWORD snum = *pToken & WINED3DSP_REGNUM_MASK;
341 reg_maps->labels[snum] = 1;
342 pToken += curOpcode->num_params;
344 /* Set texture, address, temporary registers */
348 /* Declare 1.X samplers implicitly, based on the destination reg. number */
349 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
350 pshader /* Filter different instructions with the same enum values in VS */ &&
351 (WINED3DSIO_TEX == curOpcode->opcode ||
352 WINED3DSIO_TEXBEM == curOpcode->opcode ||
353 WINED3DSIO_TEXBEML == curOpcode->opcode ||
354 WINED3DSIO_TEXDP3TEX == curOpcode->opcode ||
355 WINED3DSIO_TEXM3x2TEX == curOpcode->opcode ||
356 WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode ||
357 WINED3DSIO_TEXM3x3TEX == curOpcode->opcode ||
358 WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode ||
359 WINED3DSIO_TEXREG2AR == curOpcode->opcode ||
360 WINED3DSIO_TEXREG2GB == curOpcode->opcode ||
361 WINED3DSIO_TEXREG2RGB == curOpcode->opcode)) {
363 /* Fake sampler usage, only set reserved bit and ttype */
364 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
366 if(!stateBlock->textures[sampler_code]) {
367 ERR("No texture bound to sampler %d\n", sampler_code);
368 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
370 int texType = IWineD3DBaseTexture_GetTextureDimensions(stateBlock->textures[sampler_code]);
372 /* We have to select between texture rectangles and 2D textures later because 2.0 and
373 * 3.0 shaders only have WINED3DSTT_2D as well
375 case GL_TEXTURE_RECTANGLE_ARB:
377 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
381 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_VOLUME;
384 case GL_TEXTURE_CUBE_MAP_ARB:
385 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_CUBE;
389 ERR("Unexpected gl texture type found: %d\n", texType);
390 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
394 /* texbem is only valid with < 1.4 pixel shaders */
395 if(WINED3DSIO_TEXBEM == curOpcode->opcode ||
396 WINED3DSIO_TEXBEML == curOpcode->opcode) {
397 reg_maps->bumpmat[sampler_code] = TRUE;
398 if(WINED3DSIO_TEXBEML == curOpcode->opcode) {
399 reg_maps->luminanceparams[sampler_code] = TRUE;
403 if(WINED3DSIO_NRM == curOpcode->opcode) {
404 reg_maps->usesnrm = 1;
405 } else if(WINED3DSIO_BEM == curOpcode->opcode && pshader) {
406 DWORD regnum = *pToken & WINED3DSP_REGNUM_MASK;
407 reg_maps->bumpmat[regnum] = TRUE;
408 } else if(WINED3DSIO_DSY == curOpcode->opcode) {
409 reg_maps->usesdsy = 1;
412 /* This will loop over all the registers and try to
413 * make a bitmask of the ones we're interested in.
415 * Relative addressing tokens are ignored, but that's
416 * okay, since we'll catch any address registers when
417 * they are initialized (required by spec) */
419 limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
420 curOpcode->num_params + 1: curOpcode->num_params;
422 for (i = 0; i < limit; ++i) {
424 DWORD param, addr_token, reg, regtype;
425 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
427 regtype = shader_get_regtype(param);
428 reg = param & WINED3DSP_REGNUM_MASK;
430 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
433 reg_maps->texcoord[reg] = 1;
435 reg_maps->address[reg] = 1;
438 else if (WINED3DSPR_TEMP == regtype)
439 reg_maps->temporary[reg] = 1;
441 else if (WINED3DSPR_INPUT == regtype) {
443 reg_maps->attributes[reg] = 1;
445 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
446 /* If relative addressing is used, we must assume that all registers
447 * are used. Even if it is a construct like v3[aL], we can't assume
448 * that v0, v1 and v2 aren't read because aL can be negative
451 for(i = 0; i < MAX_REG_INPUT; i++) {
452 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[i] = TRUE;
455 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[reg] = TRUE;
460 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
463 else if (WINED3DSPR_MISCTYPE == regtype && reg == 0 && pshader)
466 else if(WINED3DSPR_CONST == regtype) {
467 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
469 if(reg <= ((IWineD3DVertexShaderImpl *) This)->min_rel_offset) {
470 ((IWineD3DVertexShaderImpl *) This)->min_rel_offset = reg;
471 } else if(reg >= ((IWineD3DVertexShaderImpl *) This)->max_rel_offset) {
472 ((IWineD3DVertexShaderImpl *) This)->max_rel_offset = reg;
475 reg_maps->usesrelconstF = TRUE;
479 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and is used
480 * in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel shaders because TECRDOUT
481 * isn't used in them, but future register types might cause issues
483 else if(WINED3DSPR_TEXCRDOUT == regtype && i == 0 /* Only look at writes */ &&
484 !pshader && WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) < 3) {
485 reg_maps->texcoord_mask[reg] |= shader_get_writemask(param);
490 reg_maps->loop_depth = max_loop_depth;
495 static void shader_dump_decl_usage(DWORD decl, DWORD param, DWORD shader_version)
497 DWORD regtype = shader_get_regtype(param);
501 if (regtype == WINED3DSPR_SAMPLER) {
502 DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
505 case WINED3DSTT_2D: TRACE("_2d"); break;
506 case WINED3DSTT_CUBE: TRACE("_cube"); break;
507 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
508 default: TRACE("_unknown_ttype(0x%08x)", ttype);
513 DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
514 DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
516 /* Pixel shaders 3.0 don't have usage semantics */
517 if (shader_is_pshader_version(shader_version) && shader_version < WINED3DPS_VERSION(3,0))
523 case WINED3DDECLUSAGE_POSITION:
524 TRACE("position%d", idx);
526 case WINED3DDECLUSAGE_BLENDINDICES:
529 case WINED3DDECLUSAGE_BLENDWEIGHT:
532 case WINED3DDECLUSAGE_NORMAL:
533 TRACE("normal%d", idx);
535 case WINED3DDECLUSAGE_PSIZE:
538 case WINED3DDECLUSAGE_COLOR:
542 TRACE("specular%d", (idx - 1));
545 case WINED3DDECLUSAGE_TEXCOORD:
546 TRACE("texture%d", idx);
548 case WINED3DDECLUSAGE_TANGENT:
551 case WINED3DDECLUSAGE_BINORMAL:
554 case WINED3DDECLUSAGE_TESSFACTOR:
557 case WINED3DDECLUSAGE_POSITIONT:
558 TRACE("positionT%d", idx);
560 case WINED3DDECLUSAGE_FOG:
563 case WINED3DDECLUSAGE_DEPTH:
566 case WINED3DDECLUSAGE_SAMPLE:
570 FIXME("unknown_semantics(0x%08x)", usage);
575 static void shader_dump_arr_entry(const DWORD param, const DWORD addr_token,
576 unsigned int reg, int input, DWORD shader_version)
579 ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
584 shader_dump_param(addr_token, 0, input, shader_version);
594 static void shader_dump_param(const DWORD param, const DWORD addr_token, int input, DWORD shader_version)
596 static const char * const rastout_reg_names[] = { "oPos", "oFog", "oPts" };
597 static const char * const misctype_reg_names[] = { "vPos", "vFace"};
598 char swizzle_reg_chars[4];
600 DWORD reg = param & WINED3DSP_REGNUM_MASK;
601 DWORD regtype = shader_get_regtype(param);
602 DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
604 /* There are some minor differences between pixel and vertex shaders */
605 char pshader = shader_is_pshader_version(shader_version);
607 /* For one, we'd prefer color components to be shown for pshaders.
608 * FIXME: use the swizzle function for this */
610 swizzle_reg_chars[0] = pshader? 'r': 'x';
611 swizzle_reg_chars[1] = pshader? 'g': 'y';
612 swizzle_reg_chars[2] = pshader? 'b': 'z';
613 swizzle_reg_chars[3] = pshader? 'a': 'w';
616 if ( (modifier == WINED3DSPSM_NEG) ||
617 (modifier == WINED3DSPSM_BIASNEG) ||
618 (modifier == WINED3DSPSM_SIGNNEG) ||
619 (modifier == WINED3DSPSM_X2NEG) ||
620 (modifier == WINED3DSPSM_ABSNEG) )
622 else if (modifier == WINED3DSPSM_COMP)
624 else if (modifier == WINED3DSPSM_NOT)
627 if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG)
632 case WINED3DSPR_TEMP:
635 case WINED3DSPR_INPUT:
637 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
639 case WINED3DSPR_CONST:
640 case WINED3DSPR_CONST2:
641 case WINED3DSPR_CONST3:
642 case WINED3DSPR_CONST4:
644 shader_dump_arr_entry(param, addr_token, shader_get_float_offset(param), input, shader_version);
646 case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
647 TRACE("%c%u", (pshader? 't':'a'), reg);
649 case WINED3DSPR_RASTOUT:
650 TRACE("%s", rastout_reg_names[reg]);
652 case WINED3DSPR_COLOROUT:
655 case WINED3DSPR_DEPTHOUT:
658 case WINED3DSPR_ATTROUT:
661 case WINED3DSPR_TEXCRDOUT:
663 /* Vertex shaders >= 3.0 use general purpose output registers
664 * (WINED3DSPR_OUTPUT), which can include an address token */
666 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3) {
668 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
673 case WINED3DSPR_CONSTINT:
675 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
677 case WINED3DSPR_CONSTBOOL:
679 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
681 case WINED3DSPR_LABEL:
684 case WINED3DSPR_LOOP:
687 case WINED3DSPR_SAMPLER:
690 case WINED3DSPR_MISCTYPE:
692 FIXME("Unhandled misctype register %d\n", reg);
694 TRACE("%s", misctype_reg_names[reg]);
697 case WINED3DSPR_PREDICATE:
701 TRACE("unhandled_rtype(%#x)", regtype);
706 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
708 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
710 if (param & WINED3DSP_WRITEMASK_0) TRACE("%c", swizzle_reg_chars[0]);
711 if (param & WINED3DSP_WRITEMASK_1) TRACE("%c", swizzle_reg_chars[1]);
712 if (param & WINED3DSP_WRITEMASK_2) TRACE("%c", swizzle_reg_chars[2]);
713 if (param & WINED3DSP_WRITEMASK_3) TRACE("%c", swizzle_reg_chars[3]);
718 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
719 DWORD swizzle_r = swizzle & 0x03;
720 DWORD swizzle_g = (swizzle >> 2) & 0x03;
721 DWORD swizzle_b = (swizzle >> 4) & 0x03;
722 DWORD swizzle_a = (swizzle >> 6) & 0x03;
726 case WINED3DSPSM_NONE: break;
727 case WINED3DSPSM_NEG: break;
728 case WINED3DSPSM_NOT: break;
729 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
730 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
731 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
732 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
733 case WINED3DSPSM_COMP: break;
734 case WINED3DSPSM_X2: TRACE("_x2"); break;
735 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
736 case WINED3DSPSM_DZ: TRACE("_dz"); break;
737 case WINED3DSPSM_DW: TRACE("_dw"); break;
738 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
739 case WINED3DSPSM_ABS: TRACE(")"); break;
741 TRACE("_unknown_modifier(%#x)", modifier >> WINED3DSP_SRCMOD_SHIFT);
746 * swizzle bits fields:
749 if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) != swizzle) {
750 if (swizzle_r == swizzle_g &&
751 swizzle_r == swizzle_b &&
752 swizzle_r == swizzle_a) {
753 TRACE(".%c", swizzle_reg_chars[swizzle_r]);
756 swizzle_reg_chars[swizzle_r],
757 swizzle_reg_chars[swizzle_g],
758 swizzle_reg_chars[swizzle_b],
759 swizzle_reg_chars[swizzle_a]);
765 static void shader_color_correction(IWineD3DBaseShaderImpl *shader,
766 IWineD3DDeviceImpl *device, const struct SHADER_OPCODE_ARG *arg)
768 DWORD hex_version = shader->baseShader.hex_version;
769 IWineD3DBaseTextureImpl *texture;
770 struct color_fixup_desc fixup;
771 BOOL recorded = FALSE;
775 switch(arg->opcode->opcode)
778 if (hex_version < WINED3DPS_VERSION(2,0)) sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
779 else sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
782 case WINED3DSIO_TEXLDL:
783 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
786 case WINED3DSIO_TEXDP3TEX:
787 case WINED3DSIO_TEXM3x3TEX:
788 case WINED3DSIO_TEXM3x3SPEC:
789 case WINED3DSIO_TEXM3x3VSPEC:
790 case WINED3DSIO_TEXBEM:
791 case WINED3DSIO_TEXREG2AR:
792 case WINED3DSIO_TEXREG2GB:
793 case WINED3DSIO_TEXREG2RGB:
794 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
798 /* Not a texture sampling instruction, nothing to do */
802 texture = (IWineD3DBaseTextureImpl *)device->stateBlock->textures[sampler_idx];
803 if (texture) fixup = texture->baseTexture.shader_color_fixup;
804 else fixup = COLOR_FIXUP_IDENTITY;
806 /* before doing anything, record the sampler with the format in the format conversion list,
807 * but check if it's not there already */
808 for (i = 0; i < shader->baseShader.num_sampled_samplers; ++i)
810 if (shader->baseShader.sampled_samplers[i] == sampler_idx)
819 shader->baseShader.sampled_samplers[shader->baseShader.num_sampled_samplers] = sampler_idx;
820 ++shader->baseShader.num_sampled_samplers;
823 device->shader_backend->shader_color_correction(arg, fixup);
826 /** Shared code in order to generate the bulk of the shader string.
827 Use the shader_header_fct & shader_footer_fct to add strings
828 that are specific to pixel or vertex functions
829 NOTE: A description of how to parse tokens can be found on msdn */
830 void shader_generate_main(IWineD3DBaseShader *iface, SHADER_BUFFER* buffer,
831 const shader_reg_maps* reg_maps, CONST DWORD* pFunction)
833 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
834 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device; /* To access shader backend callbacks */
835 const SHADER_OPCODE *opcode_table = This->baseShader.shader_ins;
836 const SHADER_HANDLER *handler_table = device->shader_backend->shader_instruction_handler_table;
837 DWORD shader_version = This->baseShader.hex_version;
838 const DWORD *pToken = pFunction;
839 const SHADER_OPCODE *curOpcode = NULL;
840 SHADER_HANDLER hw_fct = NULL;
842 SHADER_OPCODE_ARG hw_arg;
844 /* Initialize current parsing state */
845 hw_arg.shader = iface;
846 hw_arg.buffer = buffer;
847 hw_arg.reg_maps = reg_maps;
848 This->baseShader.parse_state.current_row = 0;
850 /* Second pass, process opcodes */
851 if (NULL != pToken) {
852 while (WINED3DPS_END() != *pToken) {
854 /* Skip version token */
855 if (shader_is_version_token(*pToken)) {
860 /* Skip comment tokens */
861 if (shader_is_comment(*pToken)) {
862 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
864 TRACE("#%s\n", (const char*)pToken);
865 pToken += comment_len;
870 hw_arg.opcode_token = *pToken++;
871 curOpcode = shader_get_opcode(opcode_table, shader_version, hw_arg.opcode_token);
874 if (curOpcode == NULL)
877 hw_fct = handler_table[curOpcode->handler_idx];
879 /* Unknown opcode and its parameters */
880 if (NULL == curOpcode) {
881 FIXME("Unrecognized opcode: token=0x%08x\n", hw_arg.opcode_token);
882 pToken += shader_skip_unrecognized(iface, pToken);
885 } else if (WINED3DSIO_DCL == curOpcode->opcode ||
886 WINED3DSIO_NOP == curOpcode->opcode ||
887 WINED3DSIO_DEF == curOpcode->opcode ||
888 WINED3DSIO_DEFI == curOpcode->opcode ||
889 WINED3DSIO_DEFB == curOpcode->opcode ||
890 WINED3DSIO_PHASE == curOpcode->opcode ||
891 WINED3DSIO_RET == curOpcode->opcode) {
893 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
895 /* If a generator function is set for current shader target, use it */
896 } else if (hw_fct != NULL) {
898 hw_arg.opcode = curOpcode;
900 /* Destination token */
901 if (curOpcode->dst_token) {
903 DWORD param, addr_token = 0;
904 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
906 hw_arg.dst_addr = addr_token;
909 /* Predication token */
910 if (hw_arg.opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
911 hw_arg.predicate = *pToken++;
913 /* Other source tokens */
914 for (i = 0; i < (curOpcode->num_params - curOpcode->dst_token); i++) {
916 DWORD param, addr_token = 0;
917 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
918 hw_arg.src[i] = param;
919 hw_arg.src_addr[i] = addr_token;
922 /* Call appropriate function for output target */
925 /* Add color correction if needed */
926 shader_color_correction(This, device, &hw_arg);
928 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
929 /* FIXME: This should be internal to the shader backend.
930 * Also, right now this is the only reason "shader_mode" exists. */
931 if (This->baseShader.shader_mode == SHADER_GLSL)
932 shader_glsl_add_instruction_modifiers(&hw_arg);
934 /* Unhandled opcode */
937 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
938 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
941 /* TODO: What about result.depth? */
946 static void shader_dump_ins_modifiers(const DWORD output)
948 DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
949 DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
953 case 13: TRACE("_d8"); break;
954 case 14: TRACE("_d4"); break;
955 case 15: TRACE("_d2"); break;
956 case 1: TRACE("_x2"); break;
957 case 2: TRACE("_x4"); break;
958 case 3: TRACE("_x8"); break;
959 default: TRACE("_unhandled_shift(%d)", shift); break;
962 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
963 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
964 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
966 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
968 FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
971 /* First pass: trace shader, initialize length and version */
972 void shader_trace_init(
973 IWineD3DBaseShader *iface,
974 const DWORD* pFunction) {
976 IWineD3DBaseShaderImpl *This =(IWineD3DBaseShaderImpl *)iface;
978 const DWORD* pToken = pFunction;
979 const SHADER_OPCODE* curOpcode = NULL;
981 unsigned int len = 0;
984 TRACE("(%p) : Parsing program\n", This);
986 if (NULL != pToken) {
987 while (WINED3DVS_END() != *pToken) {
988 if (shader_is_version_token(*pToken)) { /** version */
989 This->baseShader.hex_version = *pToken;
990 TRACE("%s_%u_%u\n", shader_is_pshader_version(This->baseShader.hex_version)? "ps": "vs",
991 WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version),
992 WINED3DSHADER_VERSION_MINOR(This->baseShader.hex_version));
997 if (shader_is_comment(*pToken)) { /** comment */
998 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
1000 TRACE("//%s\n", (const char*)pToken);
1001 pToken += comment_len;
1002 len += comment_len + 1;
1005 opcode_token = *pToken++;
1006 curOpcode = shader_get_opcode(This->baseShader.shader_ins, This->baseShader.hex_version, opcode_token);
1009 if (NULL == curOpcode) {
1011 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
1012 tokens_read = shader_skip_unrecognized(iface, pToken);
1013 pToken += tokens_read;
1017 if (curOpcode->opcode == WINED3DSIO_DCL) {
1019 DWORD usage = *pToken;
1020 DWORD param = *(pToken + 1);
1022 shader_dump_decl_usage(usage, param, This->baseShader.hex_version);
1023 shader_dump_ins_modifiers(param);
1025 shader_dump_param(param, 0, 0, This->baseShader.hex_version);
1029 } else if (curOpcode->opcode == WINED3DSIO_DEF) {
1031 unsigned int offset = shader_get_float_offset(*pToken);
1033 TRACE("def c%u = %f, %f, %f, %f", offset,
1034 *(const float *)(pToken + 1),
1035 *(const float *)(pToken + 2),
1036 *(const float *)(pToken + 3),
1037 *(const float *)(pToken + 4));
1041 } else if (curOpcode->opcode == WINED3DSIO_DEFI) {
1043 TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
1052 } else if (curOpcode->opcode == WINED3DSIO_DEFB) {
1054 TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
1055 *(pToken + 1)? "true": "false");
1062 DWORD param, addr_token;
1065 /* Print out predication source token first - it follows
1066 * the destination token. */
1067 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
1069 shader_dump_param(*(pToken + 2), 0, 1, This->baseShader.hex_version);
1072 if (opcode_token & WINED3DSI_COISSUE) {
1073 /* PixWin marks instructions with the coissue flag with a '+' */
1077 TRACE("%s", curOpcode->name);
1079 if (curOpcode->opcode == WINED3DSIO_IFC ||
1080 curOpcode->opcode == WINED3DSIO_BREAKC) {
1082 DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1084 case COMPARISON_GT: TRACE("_gt"); break;
1085 case COMPARISON_EQ: TRACE("_eq"); break;
1086 case COMPARISON_GE: TRACE("_ge"); break;
1087 case COMPARISON_LT: TRACE("_lt"); break;
1088 case COMPARISON_NE: TRACE("_ne"); break;
1089 case COMPARISON_LE: TRACE("_le"); break;
1093 } else if (curOpcode->opcode == WINED3DSIO_TEX &&
1094 This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
1095 if(opcode_token & WINED3DSI_TEXLD_PROJECT) TRACE("p");
1098 /* Destination token */
1099 if (curOpcode->dst_token) {
1101 /* Destination token */
1102 tokens_read = shader_get_param(iface, pToken, ¶m, &addr_token);
1103 pToken += tokens_read;
1106 shader_dump_ins_modifiers(param);
1108 shader_dump_param(param, addr_token, 0, This->baseShader.hex_version);
1111 /* Predication token - already printed out, just skip it */
1112 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
1117 /* Other source tokens */
1118 for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i) {
1120 tokens_read = shader_get_param(iface, pToken, ¶m, &addr_token);
1121 pToken += tokens_read;
1124 TRACE((i == 0)? " " : ", ");
1125 shader_dump_param(param, addr_token, 1, This->baseShader.hex_version);
1131 This->baseShader.functionLength = (len + 1) * sizeof(DWORD);
1133 This->baseShader.functionLength = 1; /* no Function defined use fixed function vertex processing */
1137 static const SHADER_HANDLER shader_none_instruction_handler_table[WINED3DSIH_TABLE_SIZE] = {0};
1138 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
1139 static void shader_none_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {}
1140 static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1141 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1142 static void shader_none_cleanup(IWineD3DDevice *iface) {}
1143 static void shader_none_color_correction(const struct SHADER_OPCODE_ARG *arg, struct color_fixup_desc fixup) {}
1144 static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1145 static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1146 static void shader_none_free(IWineD3DDevice *iface) {}
1147 static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1148 static GLuint shader_none_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
1149 FIXME("NONE shader backend asked to generate a pixel shader\n");
1152 static void shader_none_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
1153 FIXME("NONE shader backend asked to generate a vertex shader\n");
1156 #define GLINFO_LOCATION (*gl_info)
1157 static void shader_none_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
1159 /* Set the shader caps to 0 for the none shader backend */
1160 pCaps->VertexShaderVersion = 0;
1161 pCaps->PixelShaderVersion = 0;
1162 pCaps->PixelShader1xMaxValue = 0.0;
1164 #undef GLINFO_LOCATION
1165 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1167 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1169 TRACE("Checking support for fixup:\n");
1170 dump_color_fixup_desc(fixup);
1173 /* Faked to make some apps happy. */
1174 if (!is_yuv_fixup(fixup))
1180 TRACE("[FAILED]\n");
1184 const shader_backend_t none_shader_backend = {
1185 shader_none_instruction_handler_table,
1187 shader_none_select_depth_blt,
1188 shader_none_deselect_depth_blt,
1189 shader_none_load_constants,
1190 shader_none_cleanup,
1191 shader_none_color_correction,
1192 shader_none_destroy,
1195 shader_none_dirty_const,
1196 shader_none_generate_pshader,
1197 shader_none_generate_vshader,
1198 shader_none_get_caps,
1199 shader_none_color_fixup_supported,
1202 /* *******************************************
1203 IWineD3DPixelShader IUnknown parts follow
1204 ******************************************* */
1205 HRESULT WINAPI IWineD3DBaseShaderImpl_QueryInterface(IWineD3DBaseShader *iface, REFIID riid, LPVOID *ppobj)
1207 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1208 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
1209 if (IsEqualGUID(riid, &IID_IUnknown)
1210 || IsEqualGUID(riid, &IID_IWineD3DBase)
1211 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1212 || IsEqualGUID(riid, &IID_IWineD3DPixelShader)) {
1213 IUnknown_AddRef(iface);
1218 return E_NOINTERFACE;
1221 ULONG WINAPI IWineD3DBaseShaderImpl_AddRef(IWineD3DBaseShader *iface) {
1222 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
1223 TRACE("(%p) : AddRef increasing from %d\n", This, This->baseShader.ref);
1224 return InterlockedIncrement(&This->baseShader.ref);
1227 ULONG WINAPI IWineD3DBaseShaderImpl_Release(IWineD3DBaseShader *iface) {
1228 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1229 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
1231 TRACE("(%p) : Releasing from %d\n", This, This->baseShader.ref);
1232 ref = InterlockedDecrement(&This->baseShader.ref);
1234 deviceImpl->shader_backend->shader_destroy(iface);
1235 HeapFree(GetProcessHeap(), 0, This->baseShader.function);
1236 shader_delete_constant_list(&This->baseShader.constantsF);
1237 shader_delete_constant_list(&This->baseShader.constantsB);
1238 shader_delete_constant_list(&This->baseShader.constantsI);
1239 list_remove(&This->baseShader.shader_list_entry);
1240 HeapFree(GetProcessHeap(), 0, This);