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=%08x "
158 "addr_token=%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(%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(%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 DWORD *pToken = pFunction;
794 const SHADER_OPCODE *curOpcode = NULL;
795 SHADER_HANDLER hw_fct = NULL;
797 SHADER_OPCODE_ARG hw_arg;
799 /* Initialize current parsing state */
800 hw_arg.shader = iface;
801 hw_arg.buffer = buffer;
802 hw_arg.reg_maps = reg_maps;
803 This->baseShader.parse_state.current_row = 0;
805 /* Second pass, process opcodes */
806 if (NULL != pToken) {
807 while (WINED3DPS_END() != *pToken) {
809 /* Skip version token */
810 if (shader_is_version_token(*pToken)) {
815 /* Skip comment tokens */
816 if (shader_is_comment(*pToken)) {
817 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
819 TRACE("#%s\n", (const char*)pToken);
820 pToken += comment_len;
825 hw_arg.opcode_token = *pToken++;
826 curOpcode = shader_get_opcode(iface, hw_arg.opcode_token);
829 if (curOpcode == NULL)
831 else if (This->baseShader.shader_mode == SHADER_GLSL)
832 hw_fct = curOpcode->hw_glsl_fct;
833 else if (This->baseShader.shader_mode == SHADER_ARB)
834 hw_fct = curOpcode->hw_fct;
836 /* Unknown opcode and its parameters */
837 if (NULL == curOpcode) {
838 FIXME("Unrecognized opcode: token=%08x\n", hw_arg.opcode_token);
839 pToken += shader_skip_unrecognized(iface, pToken);
842 } else if (WINED3DSIO_DCL == curOpcode->opcode ||
843 WINED3DSIO_NOP == curOpcode->opcode ||
844 WINED3DSIO_DEF == curOpcode->opcode ||
845 WINED3DSIO_DEFI == curOpcode->opcode ||
846 WINED3DSIO_DEFB == curOpcode->opcode ||
847 WINED3DSIO_PHASE == curOpcode->opcode ||
848 WINED3DSIO_RET == curOpcode->opcode) {
850 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
852 /* If a generator function is set for current shader target, use it */
853 } else if (hw_fct != NULL) {
855 hw_arg.opcode = curOpcode;
857 /* Destination token */
858 if (curOpcode->dst_token) {
860 DWORD param, addr_token = 0;
861 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
863 hw_arg.dst_addr = addr_token;
866 /* Predication token */
867 if (hw_arg.opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
868 hw_arg.predicate = *pToken++;
870 /* Other source tokens */
871 for (i = 0; i < (curOpcode->num_params - curOpcode->dst_token); i++) {
873 DWORD param, addr_token = 0;
874 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
875 hw_arg.src[i] = param;
876 hw_arg.src_addr[i] = addr_token;
879 /* Call appropriate function for output target */
882 /* Add color correction if needed */
883 device->shader_backend->shader_color_correction(&hw_arg);
885 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
886 if (This->baseShader.shader_mode == SHADER_GLSL)
887 shader_glsl_add_instruction_modifiers(&hw_arg);
889 /* Unhandled opcode */
892 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
893 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
896 /* TODO: What about result.depth? */
901 void shader_dump_ins_modifiers(const DWORD output) {
903 DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
904 DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
908 case 13: TRACE("_d8"); break;
909 case 14: TRACE("_d4"); break;
910 case 15: TRACE("_d2"); break;
911 case 1: TRACE("_x2"); break;
912 case 2: TRACE("_x4"); break;
913 case 3: TRACE("_x8"); break;
914 default: TRACE("_unhandled_shift(%d)", shift); break;
917 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
918 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
919 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
921 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
923 FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
926 /* First pass: trace shader, initialize length and version */
927 void shader_trace_init(
928 IWineD3DBaseShader *iface,
929 const DWORD* pFunction) {
931 IWineD3DBaseShaderImpl *This =(IWineD3DBaseShaderImpl *)iface;
933 const DWORD* pToken = pFunction;
934 const SHADER_OPCODE* curOpcode = NULL;
936 unsigned int len = 0;
939 TRACE("(%p) : Parsing programme\n", This);
941 if (NULL != pToken) {
942 while (WINED3DVS_END() != *pToken) {
943 if (shader_is_version_token(*pToken)) { /** version */
944 This->baseShader.hex_version = *pToken;
945 TRACE("%s_%u_%u\n", shader_is_pshader_version(This->baseShader.hex_version)? "ps": "vs",
946 WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version),
947 WINED3DSHADER_VERSION_MINOR(This->baseShader.hex_version));
952 if (shader_is_comment(*pToken)) { /** comment */
953 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
955 TRACE("//%s\n", (const char*)pToken);
956 pToken += comment_len;
957 len += comment_len + 1;
960 opcode_token = *pToken++;
961 curOpcode = shader_get_opcode(iface, opcode_token);
964 if (NULL == curOpcode) {
966 FIXME("Unrecognized opcode: token=%08x\n", opcode_token);
967 tokens_read = shader_skip_unrecognized(iface, pToken);
968 pToken += tokens_read;
972 if (curOpcode->opcode == WINED3DSIO_DCL) {
974 DWORD usage = *pToken;
975 DWORD param = *(pToken + 1);
977 shader_dump_decl_usage(This, usage, param);
978 shader_dump_ins_modifiers(param);
980 shader_dump_param(iface, param, 0, 0);
984 } else if (curOpcode->opcode == WINED3DSIO_DEF) {
986 unsigned int offset = shader_get_float_offset(*pToken);
988 TRACE("def c%u = %f, %f, %f, %f", offset,
989 *(const float *)(pToken + 1),
990 *(const float *)(pToken + 2),
991 *(const float *)(pToken + 3),
992 *(const float *)(pToken + 4));
996 } else if (curOpcode->opcode == WINED3DSIO_DEFI) {
998 TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
1007 } else if (curOpcode->opcode == WINED3DSIO_DEFB) {
1009 TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
1010 *(pToken + 1)? "true": "false");
1017 DWORD param, addr_token;
1020 /* Print out predication source token first - it follows
1021 * the destination token. */
1022 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
1024 shader_dump_param(iface, *(pToken + 2), 0, 1);
1027 if (opcode_token & WINED3DSI_COISSUE) {
1028 /* PixWin marks instructions with the coissue flag with a '+' */
1032 TRACE("%s", curOpcode->name);
1034 if (curOpcode->opcode == WINED3DSIO_IFC ||
1035 curOpcode->opcode == WINED3DSIO_BREAKC) {
1037 DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1039 case COMPARISON_GT: TRACE("_gt"); break;
1040 case COMPARISON_EQ: TRACE("_eq"); break;
1041 case COMPARISON_GE: TRACE("_ge"); break;
1042 case COMPARISON_LT: TRACE("_lt"); break;
1043 case COMPARISON_NE: TRACE("_ne"); break;
1044 case COMPARISON_LE: TRACE("_le"); break;
1048 } else if (curOpcode->opcode == WINED3DSIO_TEX &&
1049 This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
1050 if(opcode_token & WINED3DSI_TEXLD_PROJECT) TRACE("p");
1053 /* Destination token */
1054 if (curOpcode->dst_token) {
1056 /* Destination token */
1057 tokens_read = shader_get_param(iface, pToken, ¶m, &addr_token);
1058 pToken += tokens_read;
1061 shader_dump_ins_modifiers(param);
1063 shader_dump_param(iface, param, addr_token, 0);
1066 /* Predication token - already printed out, just skip it */
1067 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
1072 /* Other source tokens */
1073 for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i) {
1075 tokens_read = shader_get_param(iface, pToken, ¶m, &addr_token);
1076 pToken += tokens_read;
1079 TRACE((i == 0)? " " : ", ");
1080 shader_dump_param(iface, param, addr_token, 1);
1086 This->baseShader.functionLength = (len + 1) * sizeof(DWORD);
1088 This->baseShader.functionLength = 1; /* no Function defined use fixed function vertex processing */
1092 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
1093 static void shader_none_select_depth_blt(IWineD3DDevice *iface) {}
1094 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1095 static void shader_none_cleanup(IWineD3DDevice *iface) {}
1096 static void shader_none_color_correction(SHADER_OPCODE_ARG* arg) {}
1097 static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1098 static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1099 static void shader_none_free(IWineD3DDevice *iface) {}
1100 static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1101 static void shader_none_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
1102 FIXME("NONE shader backend asked to generate a pixel shader\n");
1104 static void shader_none_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
1105 FIXME("NONE shader backend asked to generate a vertex shader\n");
1108 #define GLINFO_LOCATION (*gl_info)
1109 static void shader_none_get_caps(WINED3DDEVTYPE devtype, WineD3D_GL_Info *gl_info, struct shader_caps *pCaps) {
1110 pCaps->TextureOpCaps = WINED3DTEXOPCAPS_ADD |
1111 WINED3DTEXOPCAPS_ADDSIGNED |
1112 WINED3DTEXOPCAPS_ADDSIGNED2X |
1113 WINED3DTEXOPCAPS_MODULATE |
1114 WINED3DTEXOPCAPS_MODULATE2X |
1115 WINED3DTEXOPCAPS_MODULATE4X |
1116 WINED3DTEXOPCAPS_SELECTARG1 |
1117 WINED3DTEXOPCAPS_SELECTARG2 |
1118 WINED3DTEXOPCAPS_DISABLE;
1120 if (GL_SUPPORT(ARB_TEXTURE_ENV_COMBINE) ||
1121 GL_SUPPORT(EXT_TEXTURE_ENV_COMBINE) ||
1122 GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
1123 pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA |
1124 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA |
1125 WINED3DTEXOPCAPS_BLENDFACTORALPHA |
1126 WINED3DTEXOPCAPS_BLENDCURRENTALPHA |
1127 WINED3DTEXOPCAPS_LERP |
1128 WINED3DTEXOPCAPS_SUBTRACT;
1130 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3) ||
1131 GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
1132 pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_ADDSMOOTH |
1133 WINED3DTEXOPCAPS_MULTIPLYADD |
1134 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
1135 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
1136 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM;
1138 if (GL_SUPPORT(ARB_TEXTURE_ENV_DOT3))
1139 pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_DOTPRODUCT3;
1141 if (GL_SUPPORT(NV_REGISTER_COMBINERS)) {
1142 pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR |
1143 WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA;
1146 if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
1147 pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_BUMPENVMAP;
1148 } else if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
1149 /* Bump mapping is supported already in NV_TEXTURE_SHADER, but that extension does
1150 * not support 3D textures. This asks for trouble if an app uses both bump mapping
1151 * and 3D textures. It also allows us to keep the code simpler by having texture
1152 * shaders constantly enabled.
1154 pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_BUMPENVMAP;
1155 /* TODO: Luminance bump map? */
1160 pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
1161 WINED3DTEXOPCAPS_PREMODULATE */
1164 pCaps->MaxTextureBlendStages = GL_LIMITS(texture_stages);
1165 pCaps->MaxSimultaneousTextures = GL_LIMITS(textures);
1167 /* Set the shader caps to 0 for the none shader backend */
1168 pCaps->VertexShaderVersion = 0;
1169 pCaps->PixelShaderVersion = 0;
1170 pCaps->PixelShader1xMaxValue = 0.0;
1172 if (GL_SUPPORT(NV_REGISTER_COMBINERS)) {
1173 pCaps->PrimitiveMiscCaps |= WINED3DPMISCCAPS_TSSARGTEMP;
1176 /* The caps below can be supported but aren't handled yet in utils.c 'd3dta_to_combiner_input', disable them until support is fixed */
1178 if (GL_SUPPORT(NV_REGISTER_COMBINERS2))
1179 pCaps->PrimitiveMiscCaps |= WINED3DPMISCCAPS_PERSTAGECONSTANT;
1184 static void shader_none_fragment_enable(IWineD3DDevice *iface, BOOL enable) {
1185 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
1186 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1188 if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
1190 glEnable(GL_TEXTURE_SHADER_NV);
1191 checkGLcall("glEnable(GL_TEXTURE_SHADER_NV)");
1193 glDisable(GL_TEXTURE_SHADER_NV);
1194 checkGLcall("glDisable(GL_TEXTURE_SHADER_NV)");
1199 #undef GLINFO_LOCATION
1201 const shader_backend_t none_shader_backend = {
1203 shader_none_select_depth_blt,
1204 shader_none_load_constants,
1205 shader_none_cleanup,
1206 shader_none_color_correction,
1207 shader_none_destroy,
1210 shader_none_dirty_const,
1211 shader_none_generate_pshader,
1212 shader_none_generate_vshader,
1213 shader_none_get_caps,
1214 shader_none_fragment_enable,
1217 /* *******************************************
1218 IWineD3DPixelShader IUnknown parts follow
1219 ******************************************* */
1220 HRESULT WINAPI IWineD3DBaseShaderImpl_QueryInterface(IWineD3DBaseShader *iface, REFIID riid, LPVOID *ppobj)
1222 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1223 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
1224 if (IsEqualGUID(riid, &IID_IUnknown)
1225 || IsEqualGUID(riid, &IID_IWineD3DBase)
1226 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1227 || IsEqualGUID(riid, &IID_IWineD3DPixelShader)) {
1228 IUnknown_AddRef(iface);
1233 return E_NOINTERFACE;
1236 ULONG WINAPI IWineD3DBaseShaderImpl_AddRef(IWineD3DBaseShader *iface) {
1237 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
1238 TRACE("(%p) : AddRef increasing from %d\n", This, This->baseShader.ref);
1239 return InterlockedIncrement(&This->baseShader.ref);
1242 ULONG WINAPI IWineD3DBaseShaderImpl_Release(IWineD3DBaseShader *iface) {
1243 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1244 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
1246 TRACE("(%p) : Releasing from %d\n", This, This->baseShader.ref);
1247 ref = InterlockedDecrement(&This->baseShader.ref);
1249 deviceImpl->shader_backend->shader_destroy(iface);
1250 HeapFree(GetProcessHeap(), 0, This->baseShader.function);
1251 shader_delete_constant_list(&This->baseShader.constantsF);
1252 shader_delete_constant_list(&This->baseShader.constantsB);
1253 shader_delete_constant_list(&This->baseShader.constantsI);
1254 list_remove(&This->baseShader.shader_list_entry);
1255 HeapFree(GetProcessHeap(), 0, This);