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(IWineD3DBaseShader *iface, const DWORD param, const DWORD addr_token, int input);
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)
145 /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
146 while (*pToken & 0x80000000) {
148 DWORD param, addr_token;
149 tokens_read += shader_get_param(iface, pToken, ¶m, &addr_token);
150 pToken += tokens_read;
152 FIXME("Unrecognized opcode param: token=0x%08x "
153 "addr_token=0x%08x name=", param, addr_token);
154 shader_dump_param(iface, param, addr_token, i);
161 /* Convert floating point offset relative
162 * to a register file to an absolute offset for float constants */
163 static unsigned int shader_get_float_offset(const DWORD reg)
165 unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
166 int regtype = shader_get_regtype(reg);
169 case WINED3DSPR_CONST: return regnum;
170 case WINED3DSPR_CONST2: return 2048 + regnum;
171 case WINED3DSPR_CONST3: return 4096 + regnum;
172 case WINED3DSPR_CONST4: return 6144 + regnum;
174 FIXME("Unsupported register type: %d\n", regtype);
179 static void shader_delete_constant_list(struct list* clist) {
182 struct local_constant* constant;
184 ptr = list_head(clist);
186 constant = LIST_ENTRY(ptr, struct local_constant, entry);
187 ptr = list_next(clist, ptr);
188 HeapFree(GetProcessHeap(), 0, constant);
193 /* Note that this does not count the loop register
194 * as an address register. */
196 HRESULT shader_get_registers_used(
197 IWineD3DBaseShader *iface,
198 shader_reg_maps* reg_maps,
199 semantic* semantics_in,
200 semantic* semantics_out,
202 IWineD3DStateBlockImpl *stateBlock) {
204 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
205 const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;
206 DWORD shader_version = This->baseShader.hex_version;
207 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
209 /* There are some minor differences between pixel and vertex shaders */
210 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
212 memset(reg_maps->bumpmat, 0, sizeof(reg_maps->bumpmat));
213 memset(reg_maps->luminanceparams, 0, sizeof(reg_maps->luminanceparams));
218 /* get_registers_used is called on every compile on some 1.x shaders, which can result
219 * in stacking up a collection of local constants. Delete the old constants if existing
221 shader_delete_constant_list(&This->baseShader.constantsF);
222 shader_delete_constant_list(&This->baseShader.constantsB);
223 shader_delete_constant_list(&This->baseShader.constantsI);
225 while (WINED3DVS_END() != *pToken) {
226 CONST SHADER_OPCODE* curOpcode;
230 if (shader_is_version_token(*pToken)) {
235 } else if (shader_is_comment(*pToken)) {
236 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
238 pToken += comment_len;
243 opcode_token = *pToken++;
244 curOpcode = shader_get_opcode(shader_ins, shader_version, opcode_token);
246 /* Unhandled opcode, and its parameters */
247 if (NULL == curOpcode) {
248 while (*pToken & 0x80000000)
251 /* Handle declarations */
252 } else if (WINED3DSIO_DCL == curOpcode->opcode) {
254 DWORD usage = *pToken++;
255 DWORD param = *pToken++;
256 DWORD regtype = shader_get_regtype(param);
257 unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
259 /* Vshader: mark attributes used
260 Pshader: mark 3.0 input registers used, save token */
261 if (WINED3DSPR_INPUT == regtype) {
264 reg_maps->attributes[regnum] = 1;
266 reg_maps->packed_input[regnum] = 1;
268 semantics_in[regnum].usage = usage;
269 semantics_in[regnum].reg = param;
271 /* Vshader: mark 3.0 output registers used, save token */
272 } else if (WINED3DSPR_OUTPUT == regtype) {
273 reg_maps->packed_output[regnum] = 1;
274 semantics_out[regnum].usage = usage;
275 semantics_out[regnum].reg = param;
276 if (usage & (WINED3DDECLUSAGE_FOG << WINED3DSP_DCL_USAGE_SHIFT))
279 /* Save sampler usage token */
280 } else if (WINED3DSPR_SAMPLER == regtype)
281 reg_maps->samplers[regnum] = usage;
283 } else if (WINED3DSIO_DEF == curOpcode->opcode) {
285 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
286 if (!lconst) return E_OUTOFMEMORY;
287 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
288 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
290 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
291 if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 && pshader) {
292 float *value = (float *) lconst->value;
293 if(value[0] < -1.0) value[0] = -1.0;
294 else if(value[0] > 1.0) value[0] = 1.0;
295 if(value[1] < -1.0) value[1] = -1.0;
296 else if(value[1] > 1.0) value[1] = 1.0;
297 if(value[2] < -1.0) value[2] = -1.0;
298 else if(value[2] > 1.0) value[2] = 1.0;
299 if(value[3] < -1.0) value[3] = -1.0;
300 else if(value[3] > 1.0) value[3] = 1.0;
303 list_add_head(&This->baseShader.constantsF, &lconst->entry);
304 pToken += curOpcode->num_params;
306 } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
308 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
309 if (!lconst) return E_OUTOFMEMORY;
310 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
311 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
312 list_add_head(&This->baseShader.constantsI, &lconst->entry);
313 pToken += curOpcode->num_params;
315 } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
317 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
318 if (!lconst) return E_OUTOFMEMORY;
319 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
320 memcpy(lconst->value, pToken + 1, 1 * sizeof(DWORD));
321 list_add_head(&This->baseShader.constantsB, &lconst->entry);
322 pToken += curOpcode->num_params;
324 /* If there's a loop in the shader */
325 } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
326 WINED3DSIO_REP == curOpcode->opcode) {
328 if(cur_loop_depth > max_loop_depth)
329 max_loop_depth = cur_loop_depth;
330 pToken += curOpcode->num_params;
332 } else if (WINED3DSIO_ENDLOOP == curOpcode->opcode ||
333 WINED3DSIO_ENDREP == curOpcode->opcode) {
336 /* For subroutine prototypes */
337 } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
339 DWORD snum = *pToken & WINED3DSP_REGNUM_MASK;
340 reg_maps->labels[snum] = 1;
341 pToken += curOpcode->num_params;
343 /* Set texture, address, temporary registers */
347 /* Declare 1.X samplers implicitly, based on the destination reg. number */
348 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
349 pshader /* Filter different instructions with the same enum values in VS */ &&
350 (WINED3DSIO_TEX == curOpcode->opcode ||
351 WINED3DSIO_TEXBEM == curOpcode->opcode ||
352 WINED3DSIO_TEXBEML == curOpcode->opcode ||
353 WINED3DSIO_TEXDP3TEX == curOpcode->opcode ||
354 WINED3DSIO_TEXM3x2TEX == curOpcode->opcode ||
355 WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode ||
356 WINED3DSIO_TEXM3x3TEX == curOpcode->opcode ||
357 WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode ||
358 WINED3DSIO_TEXREG2AR == curOpcode->opcode ||
359 WINED3DSIO_TEXREG2GB == curOpcode->opcode ||
360 WINED3DSIO_TEXREG2RGB == curOpcode->opcode)) {
362 /* Fake sampler usage, only set reserved bit and ttype */
363 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
365 if(!stateBlock->textures[sampler_code]) {
366 ERR("No texture bound to sampler %d\n", sampler_code);
367 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
369 int texType = IWineD3DBaseTexture_GetTextureDimensions(stateBlock->textures[sampler_code]);
371 /* We have to select between texture rectangles and 2D textures later because 2.0 and
372 * 3.0 shaders only have WINED3DSTT_2D as well
374 case GL_TEXTURE_RECTANGLE_ARB:
376 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
380 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_VOLUME;
383 case GL_TEXTURE_CUBE_MAP_ARB:
384 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_CUBE;
388 ERR("Unexpected gl texture type found: %d\n", texType);
389 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
393 /* texbem is only valid with < 1.4 pixel shaders */
394 if(WINED3DSIO_TEXBEM == curOpcode->opcode ||
395 WINED3DSIO_TEXBEML == curOpcode->opcode) {
396 reg_maps->bumpmat[sampler_code] = TRUE;
397 if(WINED3DSIO_TEXBEML == curOpcode->opcode) {
398 reg_maps->luminanceparams[sampler_code] = TRUE;
402 if(WINED3DSIO_NRM == curOpcode->opcode) {
403 reg_maps->usesnrm = 1;
404 } else if(WINED3DSIO_BEM == curOpcode->opcode && pshader) {
405 DWORD regnum = *pToken & WINED3DSP_REGNUM_MASK;
406 reg_maps->bumpmat[regnum] = TRUE;
407 } else if(WINED3DSIO_DSY == curOpcode->opcode) {
408 reg_maps->usesdsy = 1;
411 /* This will loop over all the registers and try to
412 * make a bitmask of the ones we're interested in.
414 * Relative addressing tokens are ignored, but that's
415 * okay, since we'll catch any address registers when
416 * they are initialized (required by spec) */
418 limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
419 curOpcode->num_params + 1: curOpcode->num_params;
421 for (i = 0; i < limit; ++i) {
423 DWORD param, addr_token, reg, regtype;
424 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
426 regtype = shader_get_regtype(param);
427 reg = param & WINED3DSP_REGNUM_MASK;
429 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
432 reg_maps->texcoord[reg] = 1;
434 reg_maps->address[reg] = 1;
437 else if (WINED3DSPR_TEMP == regtype)
438 reg_maps->temporary[reg] = 1;
440 else if (WINED3DSPR_INPUT == regtype) {
442 reg_maps->attributes[reg] = 1;
444 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
445 /* If relative addressing is used, we must assume that all registers
446 * are used. Even if it is a construct like v3[aL], we can't assume
447 * that v0, v1 and v2 aren't read because aL can be negative
450 for(i = 0; i < MAX_REG_INPUT; i++) {
451 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[i] = TRUE;
454 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[reg] = TRUE;
459 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
462 else if (WINED3DSPR_MISCTYPE == regtype && reg == 0 && pshader)
465 else if(WINED3DSPR_CONST == regtype) {
466 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
468 if(reg <= ((IWineD3DVertexShaderImpl *) This)->min_rel_offset) {
469 ((IWineD3DVertexShaderImpl *) This)->min_rel_offset = reg;
470 } else if(reg >= ((IWineD3DVertexShaderImpl *) This)->max_rel_offset) {
471 ((IWineD3DVertexShaderImpl *) This)->max_rel_offset = reg;
474 reg_maps->usesrelconstF = TRUE;
478 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and is used
479 * in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel shaders because TECRDOUT
480 * isn't used in them, but future register types might cause issues
482 else if(WINED3DSPR_TEXCRDOUT == regtype && i == 0 /* Only look at writes */ &&
483 !pshader && WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) < 3) {
484 reg_maps->texcoord_mask[reg] |= shader_get_writemask(param);
489 reg_maps->loop_depth = max_loop_depth;
494 static void shader_dump_decl_usage(DWORD decl, DWORD param, DWORD shader_version)
496 DWORD regtype = shader_get_regtype(param);
500 if (regtype == WINED3DSPR_SAMPLER) {
501 DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
504 case WINED3DSTT_2D: TRACE("_2d"); break;
505 case WINED3DSTT_CUBE: TRACE("_cube"); break;
506 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
507 default: TRACE("_unknown_ttype(0x%08x)", ttype);
512 DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
513 DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
515 /* Pixel shaders 3.0 don't have usage semantics */
516 if (shader_is_pshader_version(shader_version) && shader_version < WINED3DPS_VERSION(3,0))
522 case WINED3DDECLUSAGE_POSITION:
523 TRACE("position%d", idx);
525 case WINED3DDECLUSAGE_BLENDINDICES:
528 case WINED3DDECLUSAGE_BLENDWEIGHT:
531 case WINED3DDECLUSAGE_NORMAL:
532 TRACE("normal%d", idx);
534 case WINED3DDECLUSAGE_PSIZE:
537 case WINED3DDECLUSAGE_COLOR:
541 TRACE("specular%d", (idx - 1));
544 case WINED3DDECLUSAGE_TEXCOORD:
545 TRACE("texture%d", idx);
547 case WINED3DDECLUSAGE_TANGENT:
550 case WINED3DDECLUSAGE_BINORMAL:
553 case WINED3DDECLUSAGE_TESSFACTOR:
556 case WINED3DDECLUSAGE_POSITIONT:
557 TRACE("positionT%d", idx);
559 case WINED3DDECLUSAGE_FOG:
562 case WINED3DDECLUSAGE_DEPTH:
565 case WINED3DDECLUSAGE_SAMPLE:
569 FIXME("unknown_semantics(0x%08x)", usage);
574 static void shader_dump_arr_entry(
575 IWineD3DBaseShader *iface,
577 const DWORD addr_token,
582 ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
587 shader_dump_param(iface, addr_token, 0, input);
597 static void shader_dump_param(IWineD3DBaseShader *iface, const DWORD param, const DWORD addr_token, int input)
599 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
600 static const char * const rastout_reg_names[] = { "oPos", "oFog", "oPts" };
601 static const char * const misctype_reg_names[] = { "vPos", "vFace"};
602 char swizzle_reg_chars[4];
604 DWORD reg = param & WINED3DSP_REGNUM_MASK;
605 DWORD regtype = shader_get_regtype(param);
606 DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
608 /* There are some minor differences between pixel and vertex shaders */
609 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
611 /* For one, we'd prefer color components to be shown for pshaders.
612 * FIXME: use the swizzle function for this */
614 swizzle_reg_chars[0] = pshader? 'r': 'x';
615 swizzle_reg_chars[1] = pshader? 'g': 'y';
616 swizzle_reg_chars[2] = pshader? 'b': 'z';
617 swizzle_reg_chars[3] = pshader? 'a': 'w';
620 if ( (modifier == WINED3DSPSM_NEG) ||
621 (modifier == WINED3DSPSM_BIASNEG) ||
622 (modifier == WINED3DSPSM_SIGNNEG) ||
623 (modifier == WINED3DSPSM_X2NEG) ||
624 (modifier == WINED3DSPSM_ABSNEG) )
626 else if (modifier == WINED3DSPSM_COMP)
628 else if (modifier == WINED3DSPSM_NOT)
631 if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG)
636 case WINED3DSPR_TEMP:
639 case WINED3DSPR_INPUT:
641 shader_dump_arr_entry(iface, param, addr_token, reg, input);
643 case WINED3DSPR_CONST:
644 case WINED3DSPR_CONST2:
645 case WINED3DSPR_CONST3:
646 case WINED3DSPR_CONST4:
648 shader_dump_arr_entry(iface, param, addr_token, shader_get_float_offset(param), input);
650 case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
651 TRACE("%c%u", (pshader? 't':'a'), reg);
653 case WINED3DSPR_RASTOUT:
654 TRACE("%s", rastout_reg_names[reg]);
656 case WINED3DSPR_COLOROUT:
659 case WINED3DSPR_DEPTHOUT:
662 case WINED3DSPR_ATTROUT:
665 case WINED3DSPR_TEXCRDOUT:
667 /* Vertex shaders >= 3.0 use general purpose output registers
668 * (WINED3DSPR_OUTPUT), which can include an address token */
670 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
672 shader_dump_arr_entry(iface, param, addr_token, reg, input);
677 case WINED3DSPR_CONSTINT:
679 shader_dump_arr_entry(iface, param, addr_token, reg, input);
681 case WINED3DSPR_CONSTBOOL:
683 shader_dump_arr_entry(iface, param, addr_token, reg, input);
685 case WINED3DSPR_LABEL:
688 case WINED3DSPR_LOOP:
691 case WINED3DSPR_SAMPLER:
694 case WINED3DSPR_MISCTYPE:
696 FIXME("Unhandled misctype register %d\n", reg);
698 TRACE("%s", misctype_reg_names[reg]);
701 case WINED3DSPR_PREDICATE:
705 TRACE("unhandled_rtype(%#x)", regtype);
710 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
712 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
714 if (param & WINED3DSP_WRITEMASK_0) TRACE("%c", swizzle_reg_chars[0]);
715 if (param & WINED3DSP_WRITEMASK_1) TRACE("%c", swizzle_reg_chars[1]);
716 if (param & WINED3DSP_WRITEMASK_2) TRACE("%c", swizzle_reg_chars[2]);
717 if (param & WINED3DSP_WRITEMASK_3) TRACE("%c", swizzle_reg_chars[3]);
722 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
723 DWORD swizzle_r = swizzle & 0x03;
724 DWORD swizzle_g = (swizzle >> 2) & 0x03;
725 DWORD swizzle_b = (swizzle >> 4) & 0x03;
726 DWORD swizzle_a = (swizzle >> 6) & 0x03;
730 case WINED3DSPSM_NONE: break;
731 case WINED3DSPSM_NEG: break;
732 case WINED3DSPSM_NOT: break;
733 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
734 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
735 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
736 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
737 case WINED3DSPSM_COMP: break;
738 case WINED3DSPSM_X2: TRACE("_x2"); break;
739 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
740 case WINED3DSPSM_DZ: TRACE("_dz"); break;
741 case WINED3DSPSM_DW: TRACE("_dw"); break;
742 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
743 case WINED3DSPSM_ABS: TRACE(")"); break;
745 TRACE("_unknown_modifier(%#x)", modifier >> WINED3DSP_SRCMOD_SHIFT);
750 * swizzle bits fields:
753 if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) != swizzle) {
754 if (swizzle_r == swizzle_g &&
755 swizzle_r == swizzle_b &&
756 swizzle_r == swizzle_a) {
757 TRACE(".%c", swizzle_reg_chars[swizzle_r]);
760 swizzle_reg_chars[swizzle_r],
761 swizzle_reg_chars[swizzle_g],
762 swizzle_reg_chars[swizzle_b],
763 swizzle_reg_chars[swizzle_a]);
769 static void shader_color_correction(IWineD3DBaseShaderImpl *shader,
770 IWineD3DDeviceImpl *device, const struct SHADER_OPCODE_ARG *arg)
772 DWORD hex_version = shader->baseShader.hex_version;
773 IWineD3DBaseTextureImpl *texture;
774 struct color_fixup_desc fixup;
775 BOOL recorded = FALSE;
779 switch(arg->opcode->opcode)
782 if (hex_version < WINED3DPS_VERSION(2,0)) sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
783 else sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
786 case WINED3DSIO_TEXLDL:
787 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
790 case WINED3DSIO_TEXDP3TEX:
791 case WINED3DSIO_TEXM3x3TEX:
792 case WINED3DSIO_TEXM3x3SPEC:
793 case WINED3DSIO_TEXM3x3VSPEC:
794 case WINED3DSIO_TEXBEM:
795 case WINED3DSIO_TEXREG2AR:
796 case WINED3DSIO_TEXREG2GB:
797 case WINED3DSIO_TEXREG2RGB:
798 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
802 /* Not a texture sampling instruction, nothing to do */
806 texture = (IWineD3DBaseTextureImpl *)device->stateBlock->textures[sampler_idx];
807 if (texture) fixup = texture->baseTexture.shader_color_fixup;
808 else fixup = COLOR_FIXUP_IDENTITY;
810 /* before doing anything, record the sampler with the format in the format conversion list,
811 * but check if it's not there already */
812 for (i = 0; i < shader->baseShader.num_sampled_samplers; ++i)
814 if (shader->baseShader.sampled_samplers[i] == sampler_idx)
823 shader->baseShader.sampled_samplers[shader->baseShader.num_sampled_samplers] = sampler_idx;
824 ++shader->baseShader.num_sampled_samplers;
827 device->shader_backend->shader_color_correction(arg, fixup);
830 /** Shared code in order to generate the bulk of the shader string.
831 Use the shader_header_fct & shader_footer_fct to add strings
832 that are specific to pixel or vertex functions
833 NOTE: A description of how to parse tokens can be found on msdn */
834 void shader_generate_main(IWineD3DBaseShader *iface, SHADER_BUFFER* buffer,
835 const shader_reg_maps* reg_maps, CONST DWORD* pFunction)
837 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
838 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device; /* To access shader backend callbacks */
839 const SHADER_OPCODE *opcode_table = This->baseShader.shader_ins;
840 const SHADER_HANDLER *handler_table = device->shader_backend->shader_instruction_handler_table;
841 DWORD shader_version = This->baseShader.hex_version;
842 const DWORD *pToken = pFunction;
843 const SHADER_OPCODE *curOpcode = NULL;
844 SHADER_HANDLER hw_fct = NULL;
846 SHADER_OPCODE_ARG hw_arg;
848 /* Initialize current parsing state */
849 hw_arg.shader = iface;
850 hw_arg.buffer = buffer;
851 hw_arg.reg_maps = reg_maps;
852 This->baseShader.parse_state.current_row = 0;
854 /* Second pass, process opcodes */
855 if (NULL != pToken) {
856 while (WINED3DPS_END() != *pToken) {
858 /* Skip version token */
859 if (shader_is_version_token(*pToken)) {
864 /* Skip comment tokens */
865 if (shader_is_comment(*pToken)) {
866 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
868 TRACE("#%s\n", (const char*)pToken);
869 pToken += comment_len;
874 hw_arg.opcode_token = *pToken++;
875 curOpcode = shader_get_opcode(opcode_table, shader_version, hw_arg.opcode_token);
878 if (curOpcode == NULL)
881 hw_fct = handler_table[curOpcode->handler_idx];
883 /* Unknown opcode and its parameters */
884 if (NULL == curOpcode) {
885 FIXME("Unrecognized opcode: token=0x%08x\n", hw_arg.opcode_token);
886 pToken += shader_skip_unrecognized(iface, pToken);
889 } else if (WINED3DSIO_DCL == curOpcode->opcode ||
890 WINED3DSIO_NOP == curOpcode->opcode ||
891 WINED3DSIO_DEF == curOpcode->opcode ||
892 WINED3DSIO_DEFI == curOpcode->opcode ||
893 WINED3DSIO_DEFB == curOpcode->opcode ||
894 WINED3DSIO_PHASE == curOpcode->opcode ||
895 WINED3DSIO_RET == curOpcode->opcode) {
897 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
899 /* If a generator function is set for current shader target, use it */
900 } else if (hw_fct != NULL) {
902 hw_arg.opcode = curOpcode;
904 /* Destination token */
905 if (curOpcode->dst_token) {
907 DWORD param, addr_token = 0;
908 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
910 hw_arg.dst_addr = addr_token;
913 /* Predication token */
914 if (hw_arg.opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
915 hw_arg.predicate = *pToken++;
917 /* Other source tokens */
918 for (i = 0; i < (curOpcode->num_params - curOpcode->dst_token); i++) {
920 DWORD param, addr_token = 0;
921 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
922 hw_arg.src[i] = param;
923 hw_arg.src_addr[i] = addr_token;
926 /* Call appropriate function for output target */
929 /* Add color correction if needed */
930 shader_color_correction(This, device, &hw_arg);
932 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
933 /* FIXME: This should be internal to the shader backend.
934 * Also, right now this is the only reason "shader_mode" exists. */
935 if (This->baseShader.shader_mode == SHADER_GLSL)
936 shader_glsl_add_instruction_modifiers(&hw_arg);
938 /* Unhandled opcode */
941 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
942 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
945 /* TODO: What about result.depth? */
950 static void shader_dump_ins_modifiers(const DWORD output)
952 DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
953 DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
957 case 13: TRACE("_d8"); break;
958 case 14: TRACE("_d4"); break;
959 case 15: TRACE("_d2"); break;
960 case 1: TRACE("_x2"); break;
961 case 2: TRACE("_x4"); break;
962 case 3: TRACE("_x8"); break;
963 default: TRACE("_unhandled_shift(%d)", shift); break;
966 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
967 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
968 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
970 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
972 FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
975 /* First pass: trace shader, initialize length and version */
976 void shader_trace_init(
977 IWineD3DBaseShader *iface,
978 const DWORD* pFunction) {
980 IWineD3DBaseShaderImpl *This =(IWineD3DBaseShaderImpl *)iface;
982 const DWORD* pToken = pFunction;
983 const SHADER_OPCODE* curOpcode = NULL;
985 unsigned int len = 0;
988 TRACE("(%p) : Parsing program\n", This);
990 if (NULL != pToken) {
991 while (WINED3DVS_END() != *pToken) {
992 if (shader_is_version_token(*pToken)) { /** version */
993 This->baseShader.hex_version = *pToken;
994 TRACE("%s_%u_%u\n", shader_is_pshader_version(This->baseShader.hex_version)? "ps": "vs",
995 WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version),
996 WINED3DSHADER_VERSION_MINOR(This->baseShader.hex_version));
1001 if (shader_is_comment(*pToken)) { /** comment */
1002 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
1004 TRACE("//%s\n", (const char*)pToken);
1005 pToken += comment_len;
1006 len += comment_len + 1;
1009 opcode_token = *pToken++;
1010 curOpcode = shader_get_opcode(This->baseShader.shader_ins, This->baseShader.hex_version, opcode_token);
1013 if (NULL == curOpcode) {
1015 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
1016 tokens_read = shader_skip_unrecognized(iface, pToken);
1017 pToken += tokens_read;
1021 if (curOpcode->opcode == WINED3DSIO_DCL) {
1023 DWORD usage = *pToken;
1024 DWORD param = *(pToken + 1);
1026 shader_dump_decl_usage(usage, param, This->baseShader.hex_version);
1027 shader_dump_ins_modifiers(param);
1029 shader_dump_param(iface, param, 0, 0);
1033 } else if (curOpcode->opcode == WINED3DSIO_DEF) {
1035 unsigned int offset = shader_get_float_offset(*pToken);
1037 TRACE("def c%u = %f, %f, %f, %f", offset,
1038 *(const float *)(pToken + 1),
1039 *(const float *)(pToken + 2),
1040 *(const float *)(pToken + 3),
1041 *(const float *)(pToken + 4));
1045 } else if (curOpcode->opcode == WINED3DSIO_DEFI) {
1047 TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
1056 } else if (curOpcode->opcode == WINED3DSIO_DEFB) {
1058 TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
1059 *(pToken + 1)? "true": "false");
1066 DWORD param, addr_token;
1069 /* Print out predication source token first - it follows
1070 * the destination token. */
1071 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
1073 shader_dump_param(iface, *(pToken + 2), 0, 1);
1076 if (opcode_token & WINED3DSI_COISSUE) {
1077 /* PixWin marks instructions with the coissue flag with a '+' */
1081 TRACE("%s", curOpcode->name);
1083 if (curOpcode->opcode == WINED3DSIO_IFC ||
1084 curOpcode->opcode == WINED3DSIO_BREAKC) {
1086 DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1088 case COMPARISON_GT: TRACE("_gt"); break;
1089 case COMPARISON_EQ: TRACE("_eq"); break;
1090 case COMPARISON_GE: TRACE("_ge"); break;
1091 case COMPARISON_LT: TRACE("_lt"); break;
1092 case COMPARISON_NE: TRACE("_ne"); break;
1093 case COMPARISON_LE: TRACE("_le"); break;
1097 } else if (curOpcode->opcode == WINED3DSIO_TEX &&
1098 This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
1099 if(opcode_token & WINED3DSI_TEXLD_PROJECT) TRACE("p");
1102 /* Destination token */
1103 if (curOpcode->dst_token) {
1105 /* Destination token */
1106 tokens_read = shader_get_param(iface, pToken, ¶m, &addr_token);
1107 pToken += tokens_read;
1110 shader_dump_ins_modifiers(param);
1112 shader_dump_param(iface, param, addr_token, 0);
1115 /* Predication token - already printed out, just skip it */
1116 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
1121 /* Other source tokens */
1122 for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i) {
1124 tokens_read = shader_get_param(iface, pToken, ¶m, &addr_token);
1125 pToken += tokens_read;
1128 TRACE((i == 0)? " " : ", ");
1129 shader_dump_param(iface, param, addr_token, 1);
1135 This->baseShader.functionLength = (len + 1) * sizeof(DWORD);
1137 This->baseShader.functionLength = 1; /* no Function defined use fixed function vertex processing */
1141 static const SHADER_HANDLER shader_none_instruction_handler_table[WINED3DSIH_TABLE_SIZE] = {0};
1142 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
1143 static void shader_none_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {}
1144 static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1145 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1146 static void shader_none_cleanup(IWineD3DDevice *iface) {}
1147 static void shader_none_color_correction(const struct SHADER_OPCODE_ARG *arg, struct color_fixup_desc fixup) {}
1148 static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1149 static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1150 static void shader_none_free(IWineD3DDevice *iface) {}
1151 static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1152 static GLuint shader_none_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
1153 FIXME("NONE shader backend asked to generate a pixel shader\n");
1156 static void shader_none_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
1157 FIXME("NONE shader backend asked to generate a vertex shader\n");
1160 #define GLINFO_LOCATION (*gl_info)
1161 static void shader_none_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
1163 /* Set the shader caps to 0 for the none shader backend */
1164 pCaps->VertexShaderVersion = 0;
1165 pCaps->PixelShaderVersion = 0;
1166 pCaps->PixelShader1xMaxValue = 0.0;
1168 #undef GLINFO_LOCATION
1169 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1171 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1173 TRACE("Checking support for fixup:\n");
1174 dump_color_fixup_desc(fixup);
1177 /* Faked to make some apps happy. */
1178 if (!is_yuv_fixup(fixup))
1184 TRACE("[FAILED]\n");
1188 const shader_backend_t none_shader_backend = {
1189 shader_none_instruction_handler_table,
1191 shader_none_select_depth_blt,
1192 shader_none_deselect_depth_blt,
1193 shader_none_load_constants,
1194 shader_none_cleanup,
1195 shader_none_color_correction,
1196 shader_none_destroy,
1199 shader_none_dirty_const,
1200 shader_none_generate_pshader,
1201 shader_none_generate_vshader,
1202 shader_none_get_caps,
1203 shader_none_color_fixup_supported,
1206 /* *******************************************
1207 IWineD3DPixelShader IUnknown parts follow
1208 ******************************************* */
1209 HRESULT WINAPI IWineD3DBaseShaderImpl_QueryInterface(IWineD3DBaseShader *iface, REFIID riid, LPVOID *ppobj)
1211 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1212 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
1213 if (IsEqualGUID(riid, &IID_IUnknown)
1214 || IsEqualGUID(riid, &IID_IWineD3DBase)
1215 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1216 || IsEqualGUID(riid, &IID_IWineD3DPixelShader)) {
1217 IUnknown_AddRef(iface);
1222 return E_NOINTERFACE;
1225 ULONG WINAPI IWineD3DBaseShaderImpl_AddRef(IWineD3DBaseShader *iface) {
1226 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
1227 TRACE("(%p) : AddRef increasing from %d\n", This, This->baseShader.ref);
1228 return InterlockedIncrement(&This->baseShader.ref);
1231 ULONG WINAPI IWineD3DBaseShaderImpl_Release(IWineD3DBaseShader *iface) {
1232 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1233 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
1235 TRACE("(%p) : Releasing from %d\n", This, This->baseShader.ref);
1236 ref = InterlockedDecrement(&This->baseShader.ref);
1238 deviceImpl->shader_backend->shader_destroy(iface);
1239 HeapFree(GetProcessHeap(), 0, This->baseShader.function);
1240 shader_delete_constant_list(&This->baseShader.constantsF);
1241 shader_delete_constant_list(&This->baseShader.constantsB);
1242 shader_delete_constant_list(&This->baseShader.constantsI);
1243 list_remove(&This->baseShader.shader_list_entry);
1244 HeapFree(GetProcessHeap(), 0, This);