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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
32 #define GLNAME_REQUIRE_GLSL ((const char *)1)
34 static inline BOOL shader_is_version_token(DWORD token) {
35 return shader_is_pshader_version(token) ||
36 shader_is_vshader_version(token);
40 SHADER_BUFFER* buffer,
41 const char *format, ...) {
43 char* base = buffer->buffer + buffer->bsize;
47 va_start(args, format);
48 rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
51 if (rc < 0 || /* C89 */
52 rc > SHADER_PGMSIZE - 1 - buffer->bsize) { /* C99 */
54 ERR("The buffer allocated for the shader program string "
55 "is too small at %d bytes.\n", SHADER_PGMSIZE);
56 buffer->bsize = SHADER_PGMSIZE - 1;
60 if (buffer->newline) {
61 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
62 buffer->newline = FALSE;
68 if (buffer->buffer[buffer->bsize-1] == '\n') {
70 buffer->newline = TRUE;
75 const SHADER_OPCODE* shader_get_opcode(
76 IWineD3DBaseShader *iface, const DWORD code) {
78 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl*) iface;
81 DWORD hex_version = This->baseShader.hex_version;
82 const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;
84 /** TODO: use dichotomic search */
85 while (NULL != shader_ins[i].name) {
86 if (((code & WINED3DSI_OPCODE_MASK) == shader_ins[i].opcode) &&
87 (((hex_version >= shader_ins[i].min_version) && (hex_version <= shader_ins[i].max_version)) ||
88 ((shader_ins[i].min_version == 0) && (shader_ins[i].max_version == 0)))) {
89 return &shader_ins[i];
93 FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n",
94 code, code, code & WINED3DSI_OPCODE_MASK, hex_version);
98 /* Read a parameter opcode from the input stream,
99 * and possibly a relative addressing token.
100 * Return the number of tokens read */
101 int shader_get_param(
102 IWineD3DBaseShader* iface,
107 /* PS >= 3.0 have relative addressing (with token)
108 * VS >= 2.0 have relative addressing (with token)
109 * VS >= 1.0 < 2.0 have relative addressing (without token)
110 * The version check below should work in general */
112 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
113 char rel_token = WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2 &&
114 ((*pToken & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
117 *addr_token = rel_token? *(pToken + 1): 0;
118 return rel_token? 2:1;
121 /* Return the number of parameters to skip for an opcode */
122 static inline int shader_skip_opcode(
123 IWineD3DBaseShaderImpl* This,
124 const SHADER_OPCODE* curOpcode,
125 DWORD opcode_token) {
127 /* Shaders >= 2.0 may contain address tokens, but fortunately they
128 * have a useful legnth mask - use it here. Shaders 1.0 contain no such tokens */
130 return (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2)?
131 ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT):
132 curOpcode->num_params;
135 /* Read the parameters of an unrecognized opcode from the input stream
136 * Return the number of tokens read.
138 * Note: This function assumes source or destination token format.
139 * It will not work with specially-formatted tokens like DEF or DCL,
140 * but hopefully those would be recognized */
142 int shader_skip_unrecognized(
143 IWineD3DBaseShader* iface,
144 const DWORD* pToken) {
149 /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
150 while (*pToken & 0x80000000) {
152 DWORD param, addr_token;
153 tokens_read += shader_get_param(iface, pToken, ¶m, &addr_token);
154 pToken += tokens_read;
156 FIXME("Unrecognized opcode param: token=%08x "
157 "addr_token=%08x name=", param, addr_token);
158 shader_dump_param(iface, param, addr_token, i);
165 /* Convert floating point offset relative
166 * to a register file to an absolute offset for float constants */
168 unsigned int shader_get_float_offset(const DWORD reg) {
170 unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
171 int regtype = shader_get_regtype(reg);
174 case WINED3DSPR_CONST: return regnum;
175 case WINED3DSPR_CONST2: return 2048 + regnum;
176 case WINED3DSPR_CONST3: return 4096 + regnum;
177 case WINED3DSPR_CONST4: return 6144 + regnum;
179 FIXME("Unsupported register type: %d\n", regtype);
184 /* Note that this does not count the loop register
185 * as an address register. */
187 HRESULT shader_get_registers_used(
188 IWineD3DBaseShader *iface,
189 shader_reg_maps* reg_maps,
190 semantic* semantics_in,
191 semantic* semantics_out,
193 IWineD3DStateBlockImpl *stateBlock) {
195 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
196 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
198 /* There are some minor differences between pixel and vertex shaders */
199 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
201 reg_maps->bumpmat = -1;
202 reg_maps->luminanceparams = -1;
207 while (WINED3DVS_END() != *pToken) {
208 CONST SHADER_OPCODE* curOpcode;
212 if (shader_is_version_token(*pToken)) {
217 } else if (shader_is_comment(*pToken)) {
218 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
220 pToken += comment_len;
225 opcode_token = *pToken++;
226 curOpcode = shader_get_opcode(iface, opcode_token);
228 /* Unhandled opcode, and its parameters */
229 if (NULL == curOpcode) {
230 while (*pToken & 0x80000000)
233 /* Handle declarations */
234 } else if (WINED3DSIO_DCL == curOpcode->opcode) {
236 DWORD usage = *pToken++;
237 DWORD param = *pToken++;
238 DWORD regtype = shader_get_regtype(param);
239 unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
241 /* Vshader: mark attributes used
242 Pshader: mark 3.0 input registers used, save token */
243 if (WINED3DSPR_INPUT == regtype) {
246 reg_maps->attributes[regnum] = 1;
248 reg_maps->packed_input[regnum] = 1;
250 semantics_in[regnum].usage = usage;
251 semantics_in[regnum].reg = param;
253 /* Vshader: mark 3.0 output registers used, save token */
254 } else if (WINED3DSPR_OUTPUT == regtype) {
255 reg_maps->packed_output[regnum] = 1;
256 semantics_out[regnum].usage = usage;
257 semantics_out[regnum].reg = param;
258 if (usage & (WINED3DDECLUSAGE_FOG << WINED3DSP_DCL_USAGE_SHIFT))
261 /* Save sampler usage token */
262 } else if (WINED3DSPR_SAMPLER == regtype)
263 reg_maps->samplers[regnum] = usage;
265 } else if (WINED3DSIO_DEF == curOpcode->opcode) {
267 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
268 if (!lconst) return E_OUTOFMEMORY;
269 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
270 memcpy(&lconst->value, pToken + 1, 4 * sizeof(DWORD));
272 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
273 if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 && pshader) {
274 float *value = (float *) lconst->value;
275 if(value[0] < -1.0) value[0] = -1.0;
276 else if(value[0] > 1.0) value[0] = 1.0;
277 if(value[1] < -1.0) value[1] = -1.0;
278 else if(value[1] > 1.0) value[1] = 1.0;
279 if(value[2] < -1.0) value[2] = -1.0;
280 else if(value[2] > 1.0) value[2] = 1.0;
281 if(value[3] < -1.0) value[3] = -1.0;
282 else if(value[3] > 1.0) value[3] = 1.0;
285 list_add_head(&This->baseShader.constantsF, &lconst->entry);
286 pToken += curOpcode->num_params;
288 } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
290 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
291 if (!lconst) return E_OUTOFMEMORY;
292 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
293 memcpy(&lconst->value, pToken + 1, 4 * sizeof(DWORD));
294 list_add_head(&This->baseShader.constantsI, &lconst->entry);
295 pToken += curOpcode->num_params;
297 } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
299 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
300 if (!lconst) return E_OUTOFMEMORY;
301 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
302 memcpy(&lconst->value, pToken + 1, 1 * sizeof(DWORD));
303 list_add_head(&This->baseShader.constantsB, &lconst->entry);
304 pToken += curOpcode->num_params;
306 /* If there's a loop in the shader */
307 } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
308 WINED3DSIO_REP == curOpcode->opcode) {
310 if(cur_loop_depth > max_loop_depth)
311 max_loop_depth = cur_loop_depth;
312 pToken += curOpcode->num_params;
314 } else if (WINED3DSIO_ENDLOOP == curOpcode->opcode ||
315 WINED3DSIO_ENDREP == curOpcode->opcode) {
318 /* For subroutine prototypes */
319 } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
321 DWORD snum = *pToken & WINED3DSP_REGNUM_MASK;
322 reg_maps->labels[snum] = 1;
323 pToken += curOpcode->num_params;
325 /* Set texture, address, temporary registers */
329 /* Declare 1.X samplers implicitly, based on the destination reg. number */
330 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
331 (WINED3DSIO_TEX == curOpcode->opcode ||
332 WINED3DSIO_TEXBEM == curOpcode->opcode ||
333 WINED3DSIO_TEXBEML == curOpcode->opcode ||
334 WINED3DSIO_TEXDP3TEX == curOpcode->opcode ||
335 WINED3DSIO_TEXM3x2TEX == curOpcode->opcode ||
336 WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode ||
337 WINED3DSIO_TEXM3x3TEX == curOpcode->opcode ||
338 WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode ||
339 WINED3DSIO_TEXREG2AR == curOpcode->opcode ||
340 WINED3DSIO_TEXREG2GB == curOpcode->opcode ||
341 WINED3DSIO_TEXREG2RGB == curOpcode->opcode)) {
343 /* Fake sampler usage, only set reserved bit and ttype */
344 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
346 if(!stateBlock->textures[sampler_code]) {
347 ERR("No texture bound to sampler %d\n", sampler_code);
348 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
350 int texType = IWineD3DBaseTexture_GetTextureDimensions(stateBlock->textures[sampler_code]);
353 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
357 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_VOLUME;
360 case GL_TEXTURE_CUBE_MAP_ARB:
361 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_CUBE;
365 ERR("Unexpected gl texture type found: %d\n", texType);
366 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
370 /* texbem is only valid with < 1.4 pixel shaders */
371 if(WINED3DSIO_TEXBEM == curOpcode->opcode ||
372 WINED3DSIO_TEXBEML == curOpcode->opcode) {
373 if(reg_maps->bumpmat != -1 && reg_maps->bumpmat != sampler_code) {
374 FIXME("Pixel shader uses texbem instruction on more than 1 sampler\n");
376 reg_maps->bumpmat = sampler_code;
377 if(WINED3DSIO_TEXBEML == curOpcode->opcode) {
378 reg_maps->luminanceparams = sampler_code;
383 if(WINED3DSIO_NRM == curOpcode->opcode) {
384 reg_maps->usesnrm = 1;
385 } else if(WINED3DSIO_BEM == curOpcode->opcode) {
386 DWORD regnum = *pToken & WINED3DSP_REGNUM_MASK;
387 if(reg_maps->bumpmat != -1 && reg_maps->bumpmat != regnum) {
388 FIXME("Pixel shader uses bem or texbem instruction on more than 1 sampler\n");
390 reg_maps->bumpmat = regnum;
394 /* This will loop over all the registers and try to
395 * make a bitmask of the ones we're interested in.
397 * Relative addressing tokens are ignored, but that's
398 * okay, since we'll catch any address registers when
399 * they are initialized (required by spec) */
401 limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
402 curOpcode->num_params + 1: curOpcode->num_params;
404 for (i = 0; i < limit; ++i) {
406 DWORD param, addr_token, reg, regtype;
407 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
409 regtype = shader_get_regtype(param);
410 reg = param & WINED3DSP_REGNUM_MASK;
412 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
415 reg_maps->texcoord[reg] = 1;
417 reg_maps->address[reg] = 1;
420 else if (WINED3DSPR_TEMP == regtype)
421 reg_maps->temporary[reg] = 1;
423 else if (WINED3DSPR_INPUT == regtype && !pshader)
424 reg_maps->attributes[reg] = 1;
426 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
429 else if (WINED3DSPR_MISCTYPE == regtype && reg == 0 && pshader)
434 reg_maps->loop_depth = max_loop_depth;
439 static void shader_dump_decl_usage(
440 IWineD3DBaseShaderImpl* This,
444 DWORD regtype = shader_get_regtype(param);
448 if (regtype == WINED3DSPR_SAMPLER) {
449 DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
452 case WINED3DSTT_2D: TRACE("_2d"); break;
453 case WINED3DSTT_CUBE: TRACE("_cube"); break;
454 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
455 default: TRACE("_unknown_ttype(%08x)", ttype);
460 DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
461 DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
463 /* Pixel shaders 3.0 don't have usage semantics */
464 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
465 if (pshader && This->baseShader.hex_version < WINED3DPS_VERSION(3,0))
471 case WINED3DDECLUSAGE_POSITION:
472 TRACE("position%d", idx);
474 case WINED3DDECLUSAGE_BLENDINDICES:
477 case WINED3DDECLUSAGE_BLENDWEIGHT:
480 case WINED3DDECLUSAGE_NORMAL:
481 TRACE("normal%d", idx);
483 case WINED3DDECLUSAGE_PSIZE:
486 case WINED3DDECLUSAGE_COLOR:
490 TRACE("specular%d", (idx - 1));
493 case WINED3DDECLUSAGE_TEXCOORD:
494 TRACE("texture%d", idx);
496 case WINED3DDECLUSAGE_TANGENT:
499 case WINED3DDECLUSAGE_BINORMAL:
502 case WINED3DDECLUSAGE_TESSFACTOR:
505 case WINED3DDECLUSAGE_POSITIONT:
506 TRACE("positionT%d", idx);
508 case WINED3DDECLUSAGE_FOG:
511 case WINED3DDECLUSAGE_DEPTH:
514 case WINED3DDECLUSAGE_SAMPLE:
518 FIXME("unknown_semantics(%08x)", usage);
523 static void shader_dump_arr_entry(
524 IWineD3DBaseShader *iface,
526 const DWORD addr_token,
531 ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
536 shader_dump_param(iface, addr_token, 0, input);
546 void shader_dump_param(
547 IWineD3DBaseShader *iface,
549 const DWORD addr_token,
552 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
553 static const char * const rastout_reg_names[] = { "oPos", "oFog", "oPts" };
554 static const char * const misctype_reg_names[] = { "vPos", "vFace"};
555 char swizzle_reg_chars[4];
557 DWORD reg = param & WINED3DSP_REGNUM_MASK;
558 DWORD regtype = shader_get_regtype(param);
559 DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
561 /* There are some minor differences between pixel and vertex shaders */
562 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
564 /* For one, we'd prefer color components to be shown for pshaders.
565 * FIXME: use the swizzle function for this */
567 swizzle_reg_chars[0] = pshader? 'r': 'x';
568 swizzle_reg_chars[1] = pshader? 'g': 'y';
569 swizzle_reg_chars[2] = pshader? 'b': 'z';
570 swizzle_reg_chars[3] = pshader? 'a': 'w';
573 if ( (modifier == WINED3DSPSM_NEG) ||
574 (modifier == WINED3DSPSM_BIASNEG) ||
575 (modifier == WINED3DSPSM_SIGNNEG) ||
576 (modifier == WINED3DSPSM_X2NEG) ||
577 (modifier == WINED3DSPSM_ABSNEG) )
579 else if (modifier == WINED3DSPSM_COMP)
581 else if (modifier == WINED3DSPSM_NOT)
584 if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG)
589 case WINED3DSPR_TEMP:
592 case WINED3DSPR_INPUT:
594 shader_dump_arr_entry(iface, param, addr_token, reg, input);
596 case WINED3DSPR_CONST:
597 case WINED3DSPR_CONST2:
598 case WINED3DSPR_CONST3:
599 case WINED3DSPR_CONST4:
601 shader_dump_arr_entry(iface, param, addr_token, shader_get_float_offset(param), input);
603 case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
604 TRACE("%c%u", (pshader? 't':'a'), reg);
606 case WINED3DSPR_RASTOUT:
607 TRACE("%s", rastout_reg_names[reg]);
609 case WINED3DSPR_COLOROUT:
612 case WINED3DSPR_DEPTHOUT:
615 case WINED3DSPR_ATTROUT:
618 case WINED3DSPR_TEXCRDOUT:
620 /* Vertex shaders >= 3.0 use general purpose output registers
621 * (WINED3DSPR_OUTPUT), which can include an address token */
623 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
625 shader_dump_arr_entry(iface, param, addr_token, reg, input);
630 case WINED3DSPR_CONSTINT:
632 shader_dump_arr_entry(iface, param, addr_token, reg, input);
634 case WINED3DSPR_CONSTBOOL:
636 shader_dump_arr_entry(iface, param, addr_token, reg, input);
638 case WINED3DSPR_LABEL:
641 case WINED3DSPR_LOOP:
644 case WINED3DSPR_SAMPLER:
647 case WINED3DSPR_MISCTYPE:
649 FIXME("Unhandled misctype register %d\n", reg);
651 TRACE("%s", misctype_reg_names[reg]);
654 case WINED3DSPR_PREDICATE:
658 TRACE("unhandled_rtype(%#x)", regtype);
663 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
665 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
667 if (param & WINED3DSP_WRITEMASK_0) TRACE("%c", swizzle_reg_chars[0]);
668 if (param & WINED3DSP_WRITEMASK_1) TRACE("%c", swizzle_reg_chars[1]);
669 if (param & WINED3DSP_WRITEMASK_2) TRACE("%c", swizzle_reg_chars[2]);
670 if (param & WINED3DSP_WRITEMASK_3) TRACE("%c", swizzle_reg_chars[3]);
675 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
676 DWORD swizzle_r = swizzle & 0x03;
677 DWORD swizzle_g = (swizzle >> 2) & 0x03;
678 DWORD swizzle_b = (swizzle >> 4) & 0x03;
679 DWORD swizzle_a = (swizzle >> 6) & 0x03;
683 case WINED3DSPSM_NONE: break;
684 case WINED3DSPSM_NEG: break;
685 case WINED3DSPSM_NOT: break;
686 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
687 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
688 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
689 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
690 case WINED3DSPSM_COMP: break;
691 case WINED3DSPSM_X2: TRACE("_x2"); break;
692 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
693 case WINED3DSPSM_DZ: TRACE("_dz"); break;
694 case WINED3DSPSM_DW: TRACE("_dw"); break;
695 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
696 case WINED3DSPSM_ABS: TRACE(")"); break;
698 TRACE("_unknown_modifier(%#x)", modifier >> WINED3DSP_SRCMOD_SHIFT);
703 * swizzle bits fields:
706 if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) != swizzle) {
707 if (swizzle_r == swizzle_g &&
708 swizzle_r == swizzle_b &&
709 swizzle_r == swizzle_a) {
710 TRACE(".%c", swizzle_reg_chars[swizzle_r]);
713 swizzle_reg_chars[swizzle_r],
714 swizzle_reg_chars[swizzle_g],
715 swizzle_reg_chars[swizzle_b],
716 swizzle_reg_chars[swizzle_a]);
722 /** Shared code in order to generate the bulk of the shader string.
723 Use the shader_header_fct & shader_footer_fct to add strings
724 that are specific to pixel or vertex functions
725 NOTE: A description of how to parse tokens can be found at:
726 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/graphics/hh/graphics/usermodedisplaydriver_shader_cc8e4e05-f5c3-4ec0-8853-8ce07c1551b2.xml.asp */
727 void shader_generate_main(
728 IWineD3DBaseShader *iface,
729 SHADER_BUFFER* buffer,
730 shader_reg_maps* reg_maps,
731 CONST DWORD* pFunction) {
733 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
734 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device; /* To access shader backend callbacks */
735 const DWORD *pToken = pFunction;
736 const SHADER_OPCODE *curOpcode = NULL;
737 SHADER_HANDLER hw_fct = NULL;
739 SHADER_OPCODE_ARG hw_arg;
741 /* Initialize current parsing state */
742 hw_arg.shader = iface;
743 hw_arg.buffer = buffer;
744 hw_arg.reg_maps = reg_maps;
745 This->baseShader.parse_state.current_row = 0;
747 /* Second pass, process opcodes */
748 if (NULL != pToken) {
749 while (WINED3DPS_END() != *pToken) {
751 /* Skip version token */
752 if (shader_is_version_token(*pToken)) {
757 /* Skip comment tokens */
758 if (shader_is_comment(*pToken)) {
759 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
761 TRACE("#%s\n", (const char*)pToken);
762 pToken += comment_len;
767 hw_arg.opcode_token = *pToken++;
768 curOpcode = shader_get_opcode(iface, hw_arg.opcode_token);
771 if (curOpcode == NULL)
773 else if (This->baseShader.shader_mode == SHADER_GLSL)
774 hw_fct = curOpcode->hw_glsl_fct;
775 else if (This->baseShader.shader_mode == SHADER_ARB)
776 hw_fct = curOpcode->hw_fct;
778 /* Unknown opcode and its parameters */
779 if (NULL == curOpcode) {
780 FIXME("Unrecognized opcode: token=%08x\n", hw_arg.opcode_token);
781 pToken += shader_skip_unrecognized(iface, pToken);
784 } else if (WINED3DSIO_DCL == curOpcode->opcode ||
785 WINED3DSIO_NOP == curOpcode->opcode ||
786 WINED3DSIO_DEF == curOpcode->opcode ||
787 WINED3DSIO_DEFI == curOpcode->opcode ||
788 WINED3DSIO_DEFB == curOpcode->opcode ||
789 WINED3DSIO_PHASE == curOpcode->opcode ||
790 WINED3DSIO_RET == curOpcode->opcode) {
792 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
794 /* If a generator function is set for current shader target, use it */
795 } else if (hw_fct != NULL) {
797 hw_arg.opcode = curOpcode;
799 /* Destination token */
800 if (curOpcode->dst_token) {
802 DWORD param, addr_token = 0;
803 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
805 hw_arg.dst_addr = addr_token;
808 /* Predication token */
809 if (hw_arg.opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
810 hw_arg.predicate = *pToken++;
812 /* Other source tokens */
813 for (i = 0; i < (curOpcode->num_params - curOpcode->dst_token); i++) {
815 DWORD param, addr_token = 0;
816 pToken += shader_get_param(iface, pToken, ¶m, &addr_token);
817 hw_arg.src[i] = param;
818 hw_arg.src_addr[i] = addr_token;
821 /* Call appropriate function for output target */
824 /* Add color correction if needed */
825 device->shader_backend->shader_color_correction(&hw_arg);
827 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
828 if (This->baseShader.shader_mode == SHADER_GLSL)
829 shader_glsl_add_instruction_modifiers(&hw_arg);
831 /* Unhandled opcode */
834 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
835 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
838 /* TODO: What about result.depth? */
843 void shader_dump_ins_modifiers(const DWORD output) {
845 DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
846 DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
850 case 13: TRACE("_d8"); break;
851 case 14: TRACE("_d4"); break;
852 case 15: TRACE("_d2"); break;
853 case 1: TRACE("_x2"); break;
854 case 2: TRACE("_x4"); break;
855 case 3: TRACE("_x8"); break;
856 default: TRACE("_unhandled_shift(%d)", shift); break;
859 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
860 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
861 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
863 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
865 FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
868 /* First pass: trace shader, initialize length and version */
869 void shader_trace_init(
870 IWineD3DBaseShader *iface,
871 const DWORD* pFunction) {
873 IWineD3DBaseShaderImpl *This =(IWineD3DBaseShaderImpl *)iface;
875 const DWORD* pToken = pFunction;
876 const SHADER_OPCODE* curOpcode = NULL;
878 unsigned int len = 0;
881 TRACE("(%p) : Parsing programme\n", This);
883 if (NULL != pToken) {
884 while (WINED3DVS_END() != *pToken) {
885 if (shader_is_version_token(*pToken)) { /** version */
886 This->baseShader.hex_version = *pToken;
887 TRACE("%s_%u_%u\n", shader_is_pshader_version(This->baseShader.hex_version)? "ps": "vs",
888 WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version),
889 WINED3DSHADER_VERSION_MINOR(This->baseShader.hex_version));
894 if (shader_is_comment(*pToken)) { /** comment */
895 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
897 TRACE("//%s\n", (const char*)pToken);
898 pToken += comment_len;
899 len += comment_len + 1;
902 opcode_token = *pToken++;
903 curOpcode = shader_get_opcode(iface, opcode_token);
906 if (NULL == curOpcode) {
908 FIXME("Unrecognized opcode: token=%08x\n", opcode_token);
909 tokens_read = shader_skip_unrecognized(iface, pToken);
910 pToken += tokens_read;
914 if (curOpcode->opcode == WINED3DSIO_DCL) {
916 DWORD usage = *pToken;
917 DWORD param = *(pToken + 1);
919 shader_dump_decl_usage(This, usage, param);
920 shader_dump_ins_modifiers(param);
922 shader_dump_param(iface, param, 0, 0);
926 } else if (curOpcode->opcode == WINED3DSIO_DEF) {
928 unsigned int offset = shader_get_float_offset(*pToken);
930 TRACE("def c%u = %f, %f, %f, %f", offset,
931 *(const float *)(pToken + 1),
932 *(const float *)(pToken + 2),
933 *(const float *)(pToken + 3),
934 *(const float *)(pToken + 4));
938 } else if (curOpcode->opcode == WINED3DSIO_DEFI) {
940 TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
949 } else if (curOpcode->opcode == WINED3DSIO_DEFB) {
951 TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
952 *(pToken + 1)? "true": "false");
959 DWORD param, addr_token;
962 /* Print out predication source token first - it follows
963 * the destination token. */
964 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
966 shader_dump_param(iface, *(pToken + 2), 0, 1);
969 if (opcode_token & WINED3DSI_COISSUE) {
970 /* PixWin marks instructions with the coissue flag with a '+' */
974 TRACE("%s", curOpcode->name);
976 if (curOpcode->opcode == WINED3DSIO_IFC ||
977 curOpcode->opcode == WINED3DSIO_BREAKC) {
979 DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
981 case COMPARISON_GT: TRACE("_gt"); break;
982 case COMPARISON_EQ: TRACE("_eq"); break;
983 case COMPARISON_GE: TRACE("_ge"); break;
984 case COMPARISON_LT: TRACE("_lt"); break;
985 case COMPARISON_NE: TRACE("_ne"); break;
986 case COMPARISON_LE: TRACE("_le"); break;
990 } else if (curOpcode->opcode == WINED3DSIO_TEX &&
991 This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
992 if(opcode_token & WINED3DSI_TEXLD_PROJECT) TRACE("p");
995 /* Destination token */
996 if (curOpcode->dst_token) {
998 /* Destination token */
999 tokens_read = shader_get_param(iface, pToken, ¶m, &addr_token);
1000 pToken += tokens_read;
1003 shader_dump_ins_modifiers(param);
1005 shader_dump_param(iface, param, addr_token, 0);
1008 /* Predication token - already printed out, just skip it */
1009 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
1014 /* Other source tokens */
1015 for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i) {
1017 tokens_read = shader_get_param(iface, pToken, ¶m, &addr_token);
1018 pToken += tokens_read;
1021 TRACE((i == 0)? " " : ", ");
1022 shader_dump_param(iface, param, addr_token, 1);
1028 This->baseShader.functionLength = (len + 1) * sizeof(DWORD);
1030 This->baseShader.functionLength = 1; /* no Function defined use fixed function vertex processing */
1034 void shader_delete_constant_list(
1035 struct list* clist) {
1038 struct local_constant* constant;
1040 ptr = list_head(clist);
1042 constant = LIST_ENTRY(ptr, struct local_constant, entry);
1043 ptr = list_next(clist, ptr);
1044 HeapFree(GetProcessHeap(), 0, constant);
1048 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
1049 static void shader_none_select_depth_blt(IWineD3DDevice *iface) {}
1050 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1051 static void shader_none_cleanup(IWineD3DDevice *iface) {}
1053 const shader_backend_t none_shader_backend = {
1054 &shader_none_select,
1055 &shader_none_select_depth_blt,
1056 &shader_none_load_constants,
1057 &shader_none_cleanup