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);
33 #define GLNAME_REQUIRE_GLSL ((const char *)1)
35 static inline BOOL shader_is_version_token(DWORD token) {
36 return shader_is_pshader_version(token) ||
37 shader_is_vshader_version(token);
41 SHADER_BUFFER* buffer,
42 const char *format, ...) {
44 char* base = buffer->buffer + buffer->bsize;
48 va_start(args, format);
49 rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
52 if (rc < 0 || /* C89 */
53 rc > SHADER_PGMSIZE - 1 - buffer->bsize) { /* C99 */
55 ERR("The buffer allocated for the shader program string "
56 "is too small at %d bytes.\n", SHADER_PGMSIZE);
57 buffer->bsize = SHADER_PGMSIZE - 1;
61 if (buffer->newline) {
62 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
63 buffer->newline = FALSE;
69 if (buffer->buffer[buffer->bsize-1] == '\n') {
71 buffer->newline = TRUE;
76 const SHADER_OPCODE* shader_get_opcode(
77 IWineD3DBaseShader *iface, const DWORD code) {
79 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl*) iface;
82 DWORD hex_version = This->baseShader.hex_version;
83 const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;
85 /** TODO: use dichotomic search */
86 while (NULL != shader_ins[i].name) {
87 if (((code & WINED3DSI_OPCODE_MASK) == shader_ins[i].opcode) &&
88 (((hex_version >= shader_ins[i].min_version) && (hex_version <= shader_ins[i].max_version)) ||
89 ((shader_ins[i].min_version == 0) && (shader_ins[i].max_version == 0)))) {
90 return &shader_ins[i];
94 FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n",
95 code, code, code & WINED3DSI_OPCODE_MASK, hex_version);
99 /* Read a parameter opcode from the input stream,
100 * and possibly a relative addressing token.
101 * Return the number of tokens read */
102 int shader_get_param(
103 IWineD3DBaseShader* iface,
108 /* PS >= 3.0 have relative addressing (with token)
109 * VS >= 2.0 have relative addressing (with token)
110 * VS >= 1.0 < 2.0 have relative addressing (without token)
111 * The version check below should work in general */
113 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
114 char rel_token = WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2 &&
115 ((*pToken & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
118 *addr_token = rel_token? *(pToken + 1): 0;
119 return rel_token? 2:1;
122 /* Return the number of parameters to skip for an opcode */
123 static inline int shader_skip_opcode(
124 IWineD3DBaseShaderImpl* This,
125 const SHADER_OPCODE* curOpcode,
126 DWORD opcode_token) {
128 /* Shaders >= 2.0 may contain address tokens, but fortunately they
129 * have a useful length mask - use it here. Shaders 1.0 contain no such tokens */
131 return (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2)?
132 ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT):
133 curOpcode->num_params;
136 /* Read the parameters of an unrecognized opcode from the input stream
137 * Return the number of tokens read.
139 * Note: This function assumes source or destination token format.
140 * It will not work with specially-formatted tokens like DEF or DCL,
141 * but hopefully those would be recognized */
143 int shader_skip_unrecognized(
144 IWineD3DBaseShader* iface,
145 const DWORD* pToken) {
150 /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
151 while (*pToken & 0x80000000) {
153 DWORD param, addr_token;
154 tokens_read += shader_get_param(iface, pToken, ¶m, &addr_token);
155 pToken += tokens_read;
157 FIXME("Unrecognized opcode param: token=0x%08x "
158 "addr_token=0x%08x name=", param, addr_token);
159 shader_dump_param(iface, param, addr_token, i);
166 /* Convert floating point offset relative
167 * to a register file to an absolute offset for float constants */
169 unsigned int shader_get_float_offset(const DWORD reg) {
171 unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
172 int regtype = shader_get_regtype(reg);
175 case WINED3DSPR_CONST: return regnum;
176 case WINED3DSPR_CONST2: return 2048 + regnum;
177 case WINED3DSPR_CONST3: return 4096 + regnum;
178 case WINED3DSPR_CONST4: return 6144 + regnum;
180 FIXME("Unsupported register type: %d\n", regtype);
185 static void shader_delete_constant_list(struct list* clist) {
188 struct local_constant* constant;
190 ptr = list_head(clist);
192 constant = LIST_ENTRY(ptr, struct local_constant, entry);
193 ptr = list_next(clist, ptr);
194 HeapFree(GetProcessHeap(), 0, constant);
199 /* Note that this does not count the loop register
200 * as an address register. */
202 HRESULT shader_get_registers_used(
203 IWineD3DBaseShader *iface,
204 shader_reg_maps* reg_maps,
205 semantic* semantics_in,
206 semantic* semantics_out,
208 IWineD3DStateBlockImpl *stateBlock) {
210 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
211 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
213 /* There are some minor differences between pixel and vertex shaders */
214 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
216 memset(reg_maps->bumpmat, 0, sizeof(reg_maps->bumpmat));
217 memset(reg_maps->luminanceparams, 0, sizeof(reg_maps->luminanceparams));
222 /* get_registers_used is called on every compile on some 1.x shaders, which can result
223 * in stacking up a collection of local constants. Delete the old constants if existing
225 shader_delete_constant_list(&This->baseShader.constantsF);
226 shader_delete_constant_list(&This->baseShader.constantsB);
227 shader_delete_constant_list(&This->baseShader.constantsI);
229 while (WINED3DVS_END() != *pToken) {
230 CONST SHADER_OPCODE* curOpcode;
234 if (shader_is_version_token(*pToken)) {
239 } else if (shader_is_comment(*pToken)) {
240 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
242 pToken += comment_len;
247 opcode_token = *pToken++;
248 curOpcode = shader_get_opcode(iface, opcode_token);
250 /* Unhandled opcode, and its parameters */
251 if (NULL == curOpcode) {
252 while (*pToken & 0x80000000)
255 /* Handle declarations */
256 } else if (WINED3DSIO_DCL == curOpcode->opcode) {
258 DWORD usage = *pToken++;
259 DWORD param = *pToken++;
260 DWORD regtype = shader_get_regtype(param);
261 unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
263 /* Vshader: mark attributes used
264 Pshader: mark 3.0 input registers used, save token */
265 if (WINED3DSPR_INPUT == regtype) {
268 reg_maps->attributes[regnum] = 1;
270 reg_maps->packed_input[regnum] = 1;
272 semantics_in[regnum].usage = usage;
273 semantics_in[regnum].reg = param;
275 /* Vshader: mark 3.0 output registers used, save token */
276 } else if (WINED3DSPR_OUTPUT == regtype) {
277 reg_maps->packed_output[regnum] = 1;
278 semantics_out[regnum].usage = usage;
279 semantics_out[regnum].reg = param;
280 if (usage & (WINED3DDECLUSAGE_FOG << WINED3DSP_DCL_USAGE_SHIFT))
283 /* Save sampler usage token */
284 } else if (WINED3DSPR_SAMPLER == regtype)
285 reg_maps->samplers[regnum] = usage;
287 } else if (WINED3DSIO_DEF == curOpcode->opcode) {
289 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
290 if (!lconst) return E_OUTOFMEMORY;
291 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
292 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
294 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
295 if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 && pshader) {
296 float *value = (float *) lconst->value;
297 if(value[0] < -1.0) value[0] = -1.0;
298 else if(value[0] > 1.0) value[0] = 1.0;
299 if(value[1] < -1.0) value[1] = -1.0;
300 else if(value[1] > 1.0) value[1] = 1.0;
301 if(value[2] < -1.0) value[2] = -1.0;
302 else if(value[2] > 1.0) value[2] = 1.0;
303 if(value[3] < -1.0) value[3] = -1.0;
304 else if(value[3] > 1.0) value[3] = 1.0;
307 list_add_head(&This->baseShader.constantsF, &lconst->entry);
308 pToken += curOpcode->num_params;
310 } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
312 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
313 if (!lconst) return E_OUTOFMEMORY;
314 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
315 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
316 list_add_head(&This->baseShader.constantsI, &lconst->entry);
317 pToken += curOpcode->num_params;
319 } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
321 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
322 if (!lconst) return E_OUTOFMEMORY;
323 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
324 memcpy(lconst->value, pToken + 1, 1 * sizeof(DWORD));
325 list_add_head(&This->baseShader.constantsB, &lconst->entry);
326 pToken += curOpcode->num_params;
328 /* If there's a loop in the shader */
329 } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
330 WINED3DSIO_REP == curOpcode->opcode) {
332 if(cur_loop_depth > max_loop_depth)
333 max_loop_depth = cur_loop_depth;
334 pToken += curOpcode->num_params;
336 } else if (WINED3DSIO_ENDLOOP == curOpcode->opcode ||
337 WINED3DSIO_ENDREP == curOpcode->opcode) {
340 /* For subroutine prototypes */
341 } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
343 DWORD snum = *pToken & WINED3DSP_REGNUM_MASK;
344 reg_maps->labels[snum] = 1;
345 pToken += curOpcode->num_params;
347 /* Set texture, address, temporary registers */
351 /* Declare 1.X samplers implicitly, based on the destination reg. number */
352 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
353 pshader /* Filter different instructions with the same enum values in VS */ &&
354 (WINED3DSIO_TEX == curOpcode->opcode ||
355 WINED3DSIO_TEXBEM == curOpcode->opcode ||
356 WINED3DSIO_TEXBEML == curOpcode->opcode ||
357 WINED3DSIO_TEXDP3TEX == curOpcode->opcode ||
358 WINED3DSIO_TEXM3x2TEX == curOpcode->opcode ||
359 WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode ||
360 WINED3DSIO_TEXM3x3TEX == curOpcode->opcode ||
361 WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode ||
362 WINED3DSIO_TEXREG2AR == curOpcode->opcode ||
363 WINED3DSIO_TEXREG2GB == curOpcode->opcode ||
364 WINED3DSIO_TEXREG2RGB == curOpcode->opcode)) {
366 /* Fake sampler usage, only set reserved bit and ttype */
367 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
369 if(!stateBlock->textures[sampler_code]) {
370 ERR("No texture bound to sampler %d\n", sampler_code);
371 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
373 int texType = IWineD3DBaseTexture_GetTextureDimensions(stateBlock->textures[sampler_code]);
375 /* We have to select between texture rectangles and 2D textures later because 2.0 and
376 * 3.0 shaders only have WINED3DSTT_2D as well
378 case GL_TEXTURE_RECTANGLE_ARB:
380 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
384 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_VOLUME;
387 case GL_TEXTURE_CUBE_MAP_ARB:
388 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_CUBE;
392 ERR("Unexpected gl texture type found: %d\n", texType);
393 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
397 /* texbem is only valid with < 1.4 pixel shaders */
398 if(WINED3DSIO_TEXBEM == curOpcode->opcode ||
399 WINED3DSIO_TEXBEML == curOpcode->opcode) {
400 reg_maps->bumpmat[sampler_code] = TRUE;
401 if(WINED3DSIO_TEXBEML == curOpcode->opcode) {
402 reg_maps->luminanceparams[sampler_code] = TRUE;
406 if(WINED3DSIO_NRM == curOpcode->opcode) {
407 reg_maps->usesnrm = 1;
408 } else if(WINED3DSIO_BEM == curOpcode->opcode && pshader) {
409 DWORD regnum = *pToken & WINED3DSP_REGNUM_MASK;
410 reg_maps->bumpmat[regnum] = TRUE;
411 } else if(WINED3DSIO_DSY == curOpcode->opcode) {
412 reg_maps->usesdsy = 1;
415 /* This will loop over all the registers and try to
416 * make a bitmask of the ones we're interested in.
418 * Relative addressing tokens are ignored, but that's
419 * okay, since we'll catch any address registers when
420 * they are initialized (required by spec) */
422 limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
423 curOpcode->num_params + 1: curOpcode->num_params;
425 for (i = 0; i < limit; ++i) {
427 DWORD param, addr_token, reg, regtype;
428 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
430 regtype = shader_get_regtype(param);
431 reg = param & WINED3DSP_REGNUM_MASK;
433 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
436 reg_maps->texcoord[reg] = 1;
438 reg_maps->address[reg] = 1;
441 else if (WINED3DSPR_TEMP == regtype)
442 reg_maps->temporary[reg] = 1;
444 else if (WINED3DSPR_INPUT == regtype) {
446 reg_maps->attributes[reg] = 1;
448 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
449 /* If relative addressing is used, we must assume that all registers
450 * are used. Even if it is a construct like v3[aL], we can't assume
451 * that v0, v1 and v2 aren't read because aL can be negative
454 for(i = 0; i < MAX_REG_INPUT; i++) {
455 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[i] = TRUE;
458 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[reg] = TRUE;
463 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
466 else if (WINED3DSPR_MISCTYPE == regtype && reg == 0 && pshader)
469 else if(WINED3DSPR_CONST == regtype) {
470 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
472 if(reg <= ((IWineD3DVertexShaderImpl *) This)->min_rel_offset) {
473 ((IWineD3DVertexShaderImpl *) This)->min_rel_offset = reg;
474 } else if(reg >= ((IWineD3DVertexShaderImpl *) This)->max_rel_offset) {
475 ((IWineD3DVertexShaderImpl *) This)->max_rel_offset = reg;
478 reg_maps->usesrelconstF = TRUE;
482 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and is used
483 * in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel shaders because TECRDOUT
484 * isn't used in them, but future register types might cause issues
486 else if(WINED3DSPR_TEXCRDOUT == regtype && i == 0 /* Only look at writes */ &&
487 !pshader && WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) < 3) {
488 reg_maps->texcoord_mask[reg] |= shader_get_writemask(param);
493 reg_maps->loop_depth = max_loop_depth;
498 static void shader_dump_decl_usage(
499 IWineD3DBaseShaderImpl* This,
503 DWORD regtype = shader_get_regtype(param);
507 if (regtype == WINED3DSPR_SAMPLER) {
508 DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
511 case WINED3DSTT_2D: TRACE("_2d"); break;
512 case WINED3DSTT_CUBE: TRACE("_cube"); break;
513 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
514 default: TRACE("_unknown_ttype(0x%08x)", ttype);
519 DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
520 DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
522 /* Pixel shaders 3.0 don't have usage semantics */
523 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
524 if (pshader && This->baseShader.hex_version < WINED3DPS_VERSION(3,0))
530 case WINED3DDECLUSAGE_POSITION:
531 TRACE("position%d", idx);
533 case WINED3DDECLUSAGE_BLENDINDICES:
536 case WINED3DDECLUSAGE_BLENDWEIGHT:
539 case WINED3DDECLUSAGE_NORMAL:
540 TRACE("normal%d", idx);
542 case WINED3DDECLUSAGE_PSIZE:
545 case WINED3DDECLUSAGE_COLOR:
549 TRACE("specular%d", (idx - 1));
552 case WINED3DDECLUSAGE_TEXCOORD:
553 TRACE("texture%d", idx);
555 case WINED3DDECLUSAGE_TANGENT:
558 case WINED3DDECLUSAGE_BINORMAL:
561 case WINED3DDECLUSAGE_TESSFACTOR:
564 case WINED3DDECLUSAGE_POSITIONT:
565 TRACE("positionT%d", idx);
567 case WINED3DDECLUSAGE_FOG:
570 case WINED3DDECLUSAGE_DEPTH:
573 case WINED3DDECLUSAGE_SAMPLE:
577 FIXME("unknown_semantics(0x%08x)", usage);
582 static void shader_dump_arr_entry(
583 IWineD3DBaseShader *iface,
585 const DWORD addr_token,
590 ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
595 shader_dump_param(iface, addr_token, 0, input);
605 void shader_dump_param(
606 IWineD3DBaseShader *iface,
608 const DWORD addr_token,
611 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
612 static const char * const rastout_reg_names[] = { "oPos", "oFog", "oPts" };
613 static const char * const misctype_reg_names[] = { "vPos", "vFace"};
614 char swizzle_reg_chars[4];
616 DWORD reg = param & WINED3DSP_REGNUM_MASK;
617 DWORD regtype = shader_get_regtype(param);
618 DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
620 /* There are some minor differences between pixel and vertex shaders */
621 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
623 /* For one, we'd prefer color components to be shown for pshaders.
624 * FIXME: use the swizzle function for this */
626 swizzle_reg_chars[0] = pshader? 'r': 'x';
627 swizzle_reg_chars[1] = pshader? 'g': 'y';
628 swizzle_reg_chars[2] = pshader? 'b': 'z';
629 swizzle_reg_chars[3] = pshader? 'a': 'w';
632 if ( (modifier == WINED3DSPSM_NEG) ||
633 (modifier == WINED3DSPSM_BIASNEG) ||
634 (modifier == WINED3DSPSM_SIGNNEG) ||
635 (modifier == WINED3DSPSM_X2NEG) ||
636 (modifier == WINED3DSPSM_ABSNEG) )
638 else if (modifier == WINED3DSPSM_COMP)
640 else if (modifier == WINED3DSPSM_NOT)
643 if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG)
648 case WINED3DSPR_TEMP:
651 case WINED3DSPR_INPUT:
653 shader_dump_arr_entry(iface, param, addr_token, reg, input);
655 case WINED3DSPR_CONST:
656 case WINED3DSPR_CONST2:
657 case WINED3DSPR_CONST3:
658 case WINED3DSPR_CONST4:
660 shader_dump_arr_entry(iface, param, addr_token, shader_get_float_offset(param), input);
662 case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
663 TRACE("%c%u", (pshader? 't':'a'), reg);
665 case WINED3DSPR_RASTOUT:
666 TRACE("%s", rastout_reg_names[reg]);
668 case WINED3DSPR_COLOROUT:
671 case WINED3DSPR_DEPTHOUT:
674 case WINED3DSPR_ATTROUT:
677 case WINED3DSPR_TEXCRDOUT:
679 /* Vertex shaders >= 3.0 use general purpose output registers
680 * (WINED3DSPR_OUTPUT), which can include an address token */
682 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
684 shader_dump_arr_entry(iface, param, addr_token, reg, input);
689 case WINED3DSPR_CONSTINT:
691 shader_dump_arr_entry(iface, param, addr_token, reg, input);
693 case WINED3DSPR_CONSTBOOL:
695 shader_dump_arr_entry(iface, param, addr_token, reg, input);
697 case WINED3DSPR_LABEL:
700 case WINED3DSPR_LOOP:
703 case WINED3DSPR_SAMPLER:
706 case WINED3DSPR_MISCTYPE:
708 FIXME("Unhandled misctype register %d\n", reg);
710 TRACE("%s", misctype_reg_names[reg]);
713 case WINED3DSPR_PREDICATE:
717 TRACE("unhandled_rtype(%#x)", regtype);
722 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
724 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
726 if (param & WINED3DSP_WRITEMASK_0) TRACE("%c", swizzle_reg_chars[0]);
727 if (param & WINED3DSP_WRITEMASK_1) TRACE("%c", swizzle_reg_chars[1]);
728 if (param & WINED3DSP_WRITEMASK_2) TRACE("%c", swizzle_reg_chars[2]);
729 if (param & WINED3DSP_WRITEMASK_3) TRACE("%c", swizzle_reg_chars[3]);
734 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
735 DWORD swizzle_r = swizzle & 0x03;
736 DWORD swizzle_g = (swizzle >> 2) & 0x03;
737 DWORD swizzle_b = (swizzle >> 4) & 0x03;
738 DWORD swizzle_a = (swizzle >> 6) & 0x03;
742 case WINED3DSPSM_NONE: break;
743 case WINED3DSPSM_NEG: break;
744 case WINED3DSPSM_NOT: break;
745 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
746 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
747 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
748 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
749 case WINED3DSPSM_COMP: break;
750 case WINED3DSPSM_X2: TRACE("_x2"); break;
751 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
752 case WINED3DSPSM_DZ: TRACE("_dz"); break;
753 case WINED3DSPSM_DW: TRACE("_dw"); break;
754 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
755 case WINED3DSPSM_ABS: TRACE(")"); break;
757 TRACE("_unknown_modifier(%#x)", modifier >> WINED3DSP_SRCMOD_SHIFT);
762 * swizzle bits fields:
765 if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) != swizzle) {
766 if (swizzle_r == swizzle_g &&
767 swizzle_r == swizzle_b &&
768 swizzle_r == swizzle_a) {
769 TRACE(".%c", swizzle_reg_chars[swizzle_r]);
772 swizzle_reg_chars[swizzle_r],
773 swizzle_reg_chars[swizzle_g],
774 swizzle_reg_chars[swizzle_b],
775 swizzle_reg_chars[swizzle_a]);
781 /** Shared code in order to generate the bulk of the shader string.
782 Use the shader_header_fct & shader_footer_fct to add strings
783 that are specific to pixel or vertex functions
784 NOTE: A description of how to parse tokens can be found on msdn */
785 void shader_generate_main(
786 IWineD3DBaseShader *iface,
787 SHADER_BUFFER* buffer,
788 shader_reg_maps* reg_maps,
789 CONST DWORD* pFunction) {
791 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
792 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device; /* To access shader backend callbacks */
793 const SHADER_HANDLER *handler_table = device->shader_backend->shader_instruction_handler_table;
794 const DWORD *pToken = pFunction;
795 const SHADER_OPCODE *curOpcode = NULL;
796 SHADER_HANDLER hw_fct = NULL;
798 SHADER_OPCODE_ARG hw_arg;
800 /* Initialize current parsing state */
801 hw_arg.shader = iface;
802 hw_arg.buffer = buffer;
803 hw_arg.reg_maps = reg_maps;
804 This->baseShader.parse_state.current_row = 0;
806 /* Second pass, process opcodes */
807 if (NULL != pToken) {
808 while (WINED3DPS_END() != *pToken) {
810 /* Skip version token */
811 if (shader_is_version_token(*pToken)) {
816 /* Skip comment tokens */
817 if (shader_is_comment(*pToken)) {
818 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
820 TRACE("#%s\n", (const char*)pToken);
821 pToken += comment_len;
826 hw_arg.opcode_token = *pToken++;
827 curOpcode = shader_get_opcode(iface, hw_arg.opcode_token);
830 if (curOpcode == NULL)
833 hw_fct = handler_table[curOpcode->handler_idx];
835 /* Unknown opcode and its parameters */
836 if (NULL == curOpcode) {
837 FIXME("Unrecognized opcode: token=0x%08x\n", hw_arg.opcode_token);
838 pToken += shader_skip_unrecognized(iface, pToken);
841 } else if (WINED3DSIO_DCL == curOpcode->opcode ||
842 WINED3DSIO_NOP == curOpcode->opcode ||
843 WINED3DSIO_DEF == curOpcode->opcode ||
844 WINED3DSIO_DEFI == curOpcode->opcode ||
845 WINED3DSIO_DEFB == curOpcode->opcode ||
846 WINED3DSIO_PHASE == curOpcode->opcode ||
847 WINED3DSIO_RET == curOpcode->opcode) {
849 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
851 /* If a generator function is set for current shader target, use it */
852 } else if (hw_fct != NULL) {
854 hw_arg.opcode = curOpcode;
856 /* Destination token */
857 if (curOpcode->dst_token) {
859 DWORD param, addr_token = 0;
860 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
862 hw_arg.dst_addr = addr_token;
865 /* Predication token */
866 if (hw_arg.opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
867 hw_arg.predicate = *pToken++;
869 /* Other source tokens */
870 for (i = 0; i < (curOpcode->num_params - curOpcode->dst_token); i++) {
872 DWORD param, addr_token = 0;
873 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
874 hw_arg.src[i] = param;
875 hw_arg.src_addr[i] = addr_token;
878 /* Call appropriate function for output target */
881 /* Add color correction if needed */
882 device->shader_backend->shader_color_correction(&hw_arg);
884 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
885 /* FIXME: This should be internal to the shader backend.
886 * Also, right now this is the only reason "shader_mode" exists. */
887 if (This->baseShader.shader_mode == SHADER_GLSL)
888 shader_glsl_add_instruction_modifiers(&hw_arg);
890 /* Unhandled opcode */
893 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
894 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
897 /* TODO: What about result.depth? */
902 void shader_dump_ins_modifiers(const DWORD output) {
904 DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
905 DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
909 case 13: TRACE("_d8"); break;
910 case 14: TRACE("_d4"); break;
911 case 15: TRACE("_d2"); break;
912 case 1: TRACE("_x2"); break;
913 case 2: TRACE("_x4"); break;
914 case 3: TRACE("_x8"); break;
915 default: TRACE("_unhandled_shift(%d)", shift); break;
918 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
919 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
920 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
922 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
924 FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
927 /* First pass: trace shader, initialize length and version */
928 void shader_trace_init(
929 IWineD3DBaseShader *iface,
930 const DWORD* pFunction) {
932 IWineD3DBaseShaderImpl *This =(IWineD3DBaseShaderImpl *)iface;
934 const DWORD* pToken = pFunction;
935 const SHADER_OPCODE* curOpcode = NULL;
937 unsigned int len = 0;
940 TRACE("(%p) : Parsing programme\n", This);
942 if (NULL != pToken) {
943 while (WINED3DVS_END() != *pToken) {
944 if (shader_is_version_token(*pToken)) { /** version */
945 This->baseShader.hex_version = *pToken;
946 TRACE("%s_%u_%u\n", shader_is_pshader_version(This->baseShader.hex_version)? "ps": "vs",
947 WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version),
948 WINED3DSHADER_VERSION_MINOR(This->baseShader.hex_version));
953 if (shader_is_comment(*pToken)) { /** comment */
954 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
956 TRACE("//%s\n", (const char*)pToken);
957 pToken += comment_len;
958 len += comment_len + 1;
961 opcode_token = *pToken++;
962 curOpcode = shader_get_opcode(iface, opcode_token);
965 if (NULL == curOpcode) {
967 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
968 tokens_read = shader_skip_unrecognized(iface, pToken);
969 pToken += tokens_read;
973 if (curOpcode->opcode == WINED3DSIO_DCL) {
975 DWORD usage = *pToken;
976 DWORD param = *(pToken + 1);
978 shader_dump_decl_usage(This, usage, param);
979 shader_dump_ins_modifiers(param);
981 shader_dump_param(iface, param, 0, 0);
985 } else if (curOpcode->opcode == WINED3DSIO_DEF) {
987 unsigned int offset = shader_get_float_offset(*pToken);
989 TRACE("def c%u = %f, %f, %f, %f", offset,
990 *(const float *)(pToken + 1),
991 *(const float *)(pToken + 2),
992 *(const float *)(pToken + 3),
993 *(const float *)(pToken + 4));
997 } else if (curOpcode->opcode == WINED3DSIO_DEFI) {
999 TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
1008 } else if (curOpcode->opcode == WINED3DSIO_DEFB) {
1010 TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
1011 *(pToken + 1)? "true": "false");
1018 DWORD param, addr_token;
1021 /* Print out predication source token first - it follows
1022 * the destination token. */
1023 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
1025 shader_dump_param(iface, *(pToken + 2), 0, 1);
1028 if (opcode_token & WINED3DSI_COISSUE) {
1029 /* PixWin marks instructions with the coissue flag with a '+' */
1033 TRACE("%s", curOpcode->name);
1035 if (curOpcode->opcode == WINED3DSIO_IFC ||
1036 curOpcode->opcode == WINED3DSIO_BREAKC) {
1038 DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1040 case COMPARISON_GT: TRACE("_gt"); break;
1041 case COMPARISON_EQ: TRACE("_eq"); break;
1042 case COMPARISON_GE: TRACE("_ge"); break;
1043 case COMPARISON_LT: TRACE("_lt"); break;
1044 case COMPARISON_NE: TRACE("_ne"); break;
1045 case COMPARISON_LE: TRACE("_le"); break;
1049 } else if (curOpcode->opcode == WINED3DSIO_TEX &&
1050 This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
1051 if(opcode_token & WINED3DSI_TEXLD_PROJECT) TRACE("p");
1054 /* Destination token */
1055 if (curOpcode->dst_token) {
1057 /* Destination token */
1058 tokens_read = shader_get_param(iface, pToken, ¶m, &addr_token);
1059 pToken += tokens_read;
1062 shader_dump_ins_modifiers(param);
1064 shader_dump_param(iface, param, addr_token, 0);
1067 /* Predication token - already printed out, just skip it */
1068 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
1073 /* Other source tokens */
1074 for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i) {
1076 tokens_read = shader_get_param(iface, pToken, ¶m, &addr_token);
1077 pToken += tokens_read;
1080 TRACE((i == 0)? " " : ", ");
1081 shader_dump_param(iface, param, addr_token, 1);
1087 This->baseShader.functionLength = (len + 1) * sizeof(DWORD);
1089 This->baseShader.functionLength = 1; /* no Function defined use fixed function vertex processing */
1093 static const SHADER_HANDLER shader_none_instruction_handler_table[WINED3DSIH_TABLE_SIZE] = {0};
1094 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
1095 static void shader_none_select_depth_blt(IWineD3DDevice *iface) {}
1096 static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1097 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1098 static void shader_none_cleanup(IWineD3DDevice *iface) {}
1099 static void shader_none_color_correction(SHADER_OPCODE_ARG* arg) {}
1100 static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1101 static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1102 static void shader_none_free(IWineD3DDevice *iface) {}
1103 static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1104 static void shader_none_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
1105 FIXME("NONE shader backend asked to generate a pixel shader\n");
1107 static void shader_none_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
1108 FIXME("NONE shader backend asked to generate a vertex shader\n");
1111 #define GLINFO_LOCATION (*gl_info)
1112 static void shader_none_get_caps(WINED3DDEVTYPE devtype, WineD3D_GL_Info *gl_info, struct shader_caps *pCaps) {
1113 /* Set the shader caps to 0 for the none shader backend */
1114 pCaps->VertexShaderVersion = 0;
1115 pCaps->PixelShaderVersion = 0;
1116 pCaps->PixelShader1xMaxValue = 0.0;
1118 #undef GLINFO_LOCATION
1119 static BOOL shader_none_conv_supported(WINED3DFORMAT fmt) {
1120 TRACE("Checking shader format support for format %s", debug_d3dformat(fmt));
1122 /* Faked to make some apps happy. */
1123 case WINED3DFMT_V8U8:
1124 case WINED3DFMT_V16U16:
1125 case WINED3DFMT_L6V5U5:
1126 case WINED3DFMT_X8L8V8U8:
1127 case WINED3DFMT_Q8W8V8U8:
1131 TRACE("[FAILED]\n");
1136 const shader_backend_t none_shader_backend = {
1137 shader_none_instruction_handler_table,
1139 shader_none_select_depth_blt,
1140 shader_none_deselect_depth_blt,
1141 shader_none_load_constants,
1142 shader_none_cleanup,
1143 shader_none_color_correction,
1144 shader_none_destroy,
1147 shader_none_dirty_const,
1148 shader_none_generate_pshader,
1149 shader_none_generate_vshader,
1150 shader_none_get_caps,
1151 shader_none_conv_supported
1154 /* *******************************************
1155 IWineD3DPixelShader IUnknown parts follow
1156 ******************************************* */
1157 HRESULT WINAPI IWineD3DBaseShaderImpl_QueryInterface(IWineD3DBaseShader *iface, REFIID riid, LPVOID *ppobj)
1159 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1160 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
1161 if (IsEqualGUID(riid, &IID_IUnknown)
1162 || IsEqualGUID(riid, &IID_IWineD3DBase)
1163 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1164 || IsEqualGUID(riid, &IID_IWineD3DPixelShader)) {
1165 IUnknown_AddRef(iface);
1170 return E_NOINTERFACE;
1173 ULONG WINAPI IWineD3DBaseShaderImpl_AddRef(IWineD3DBaseShader *iface) {
1174 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
1175 TRACE("(%p) : AddRef increasing from %d\n", This, This->baseShader.ref);
1176 return InterlockedIncrement(&This->baseShader.ref);
1179 ULONG WINAPI IWineD3DBaseShaderImpl_Release(IWineD3DBaseShader *iface) {
1180 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1181 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
1183 TRACE("(%p) : Releasing from %d\n", This, This->baseShader.ref);
1184 ref = InterlockedDecrement(&This->baseShader.ref);
1186 deviceImpl->shader_backend->shader_destroy(iface);
1187 HeapFree(GetProcessHeap(), 0, This->baseShader.function);
1188 shader_delete_constant_list(&This->baseShader.constantsF);
1189 shader_delete_constant_list(&This->baseShader.constantsB);
1190 shader_delete_constant_list(&This->baseShader.constantsI);
1191 list_remove(&This->baseShader.shader_list_entry);
1192 HeapFree(GetProcessHeap(), 0, This);