user32: ToUnicodeEx should return 0 for an unknown key.
[wine/dibdrv.git] / dlls / wined3d / baseshader.c
blob277edbfea85802de97c740f68ec9be80faca4987
1 /*
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
25 #include "config.h"
26 #include <string.h>
27 #include <stdio.h>
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
32 #define GLNAME_REQUIRE_GLSL ((const char *)1)
34 inline static BOOL shader_is_version_token(DWORD token) {
35 return shader_is_pshader_version(token) ||
36 shader_is_vshader_version(token);
39 int shader_addline(
40 SHADER_BUFFER* buffer,
41 const char *format, ...) {
43 char* base = buffer->buffer + buffer->bsize;
44 int rc;
46 va_list args;
47 va_start(args, format);
48 rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
49 va_end(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;
57 return -1;
60 buffer->bsize += rc;
61 buffer->lineNo++;
62 TRACE("GL HW (%u, %u) : %s", buffer->lineNo, buffer->bsize, base);
63 return 0;
66 const SHADER_OPCODE* shader_get_opcode(
67 IWineD3DBaseShader *iface, const DWORD code) {
69 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl*) iface;
71 DWORD i = 0;
72 DWORD hex_version = This->baseShader.hex_version;
73 const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;
75 /** TODO: use dichotomic search */
76 while (NULL != shader_ins[i].name) {
77 if (((code & WINED3DSI_OPCODE_MASK) == shader_ins[i].opcode) &&
78 (((hex_version >= shader_ins[i].min_version) && (hex_version <= shader_ins[i].max_version)) ||
79 ((shader_ins[i].min_version == 0) && (shader_ins[i].max_version == 0)))) {
80 return &shader_ins[i];
82 ++i;
84 FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n",
85 code, code, code & WINED3DSI_OPCODE_MASK, hex_version);
86 return NULL;
89 /* Read a parameter opcode from the input stream,
90 * and possibly a relative addressing token.
91 * Return the number of tokens read */
92 int shader_get_param(
93 IWineD3DBaseShader* iface,
94 const DWORD* pToken,
95 DWORD* param,
96 DWORD* addr_token) {
98 /* PS >= 3.0 have relative addressing (with token)
99 * VS >= 2.0 have relative addressing (with token)
100 * VS >= 1.0 < 2.0 have relative addressing (without token)
101 * The version check below should work in general */
103 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
104 char rel_token = WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2 &&
105 ((*pToken & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
107 *param = *pToken;
108 *addr_token = rel_token? *(pToken + 1): 0;
109 return rel_token? 2:1;
112 /* Return the number of parameters to skip for an opcode */
113 static inline int shader_skip_opcode(
114 IWineD3DBaseShaderImpl* This,
115 const SHADER_OPCODE* curOpcode,
116 DWORD opcode_token) {
118 /* Shaders >= 2.0 may contain address tokens, but fortunately they
119 * have a useful legnth mask - use it here. Shaders 1.0 contain no such tokens */
121 return (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2)?
122 ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT):
123 curOpcode->num_params;
126 /* Read the parameters of an unrecognized opcode from the input stream
127 * Return the number of tokens read.
129 * Note: This function assumes source or destination token format.
130 * It will not work with specially-formatted tokens like DEF or DCL,
131 * but hopefully those would be recognized */
133 int shader_skip_unrecognized(
134 IWineD3DBaseShader* iface,
135 const DWORD* pToken) {
137 int tokens_read = 0;
138 int i = 0;
140 /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
141 while (*pToken & 0x80000000) {
143 DWORD param, addr_token;
144 tokens_read += shader_get_param(iface, pToken, &param, &addr_token);
145 pToken += tokens_read;
147 FIXME("Unrecognized opcode param: token=%08x "
148 "addr_token=%08x name=", param, addr_token);
149 shader_dump_param(iface, param, addr_token, i);
150 FIXME("\n");
151 ++i;
153 return tokens_read;
156 /* Convert floating point offset relative
157 * to a register file to an absolute offset for float constants */
159 unsigned int shader_get_float_offset(const DWORD reg) {
161 unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
162 int regtype = shader_get_regtype(reg);
164 switch (regtype) {
165 case WINED3DSPR_CONST: return regnum;
166 case WINED3DSPR_CONST2: return 2048 + regnum;
167 case WINED3DSPR_CONST3: return 4096 + regnum;
168 case WINED3DSPR_CONST4: return 6144 + regnum;
169 default:
170 FIXME("Unsupported register type: %d\n", regtype);
171 return regnum;
175 /* Note that this does not count the loop register
176 * as an address register. */
178 HRESULT shader_get_registers_used(
179 IWineD3DBaseShader *iface,
180 shader_reg_maps* reg_maps,
181 semantic* semantics_in,
182 semantic* semantics_out,
183 CONST DWORD* pToken,
184 IWineD3DStateBlockImpl *stateBlock) {
186 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
188 /* There are some minor differences between pixel and vertex shaders */
189 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
191 if (pToken == NULL)
192 return WINED3D_OK;
194 while (WINED3DVS_END() != *pToken) {
195 CONST SHADER_OPCODE* curOpcode;
196 DWORD opcode_token;
198 /* Skip version */
199 if (shader_is_version_token(*pToken)) {
200 ++pToken;
201 continue;
203 /* Skip comments */
204 } else if (shader_is_comment(*pToken)) {
205 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
206 ++pToken;
207 pToken += comment_len;
208 continue;
211 /* Fetch opcode */
212 opcode_token = *pToken++;
213 curOpcode = shader_get_opcode(iface, opcode_token);
215 /* Unhandled opcode, and its parameters */
216 if (NULL == curOpcode) {
217 while (*pToken & 0x80000000)
218 ++pToken;
220 /* Handle declarations */
221 } else if (WINED3DSIO_DCL == curOpcode->opcode) {
223 DWORD usage = *pToken++;
224 DWORD param = *pToken++;
225 DWORD regtype = shader_get_regtype(param);
226 unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
228 /* Vshader: mark attributes used
229 Pshader: mark 3.0 input registers used, save token */
230 if (WINED3DSPR_INPUT == regtype) {
232 if (!pshader)
233 reg_maps->attributes[regnum] = 1;
234 else
235 reg_maps->packed_input[regnum] = 1;
237 semantics_in[regnum].usage = usage;
238 semantics_in[regnum].reg = param;
240 /* Vshader: mark 3.0 output registers used, save token */
241 } else if (WINED3DSPR_OUTPUT == regtype) {
242 reg_maps->packed_output[regnum] = 1;
243 semantics_out[regnum].usage = usage;
244 semantics_out[regnum].reg = param;
246 /* Save sampler usage token */
247 } else if (WINED3DSPR_SAMPLER == regtype)
248 reg_maps->samplers[regnum] = usage;
250 } else if (WINED3DSIO_DEF == curOpcode->opcode) {
252 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
253 if (!lconst) return E_OUTOFMEMORY;
254 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
255 memcpy(&lconst->value, pToken + 1, 4 * sizeof(DWORD));
256 list_add_head(&This->baseShader.constantsF, &lconst->entry);
257 pToken += curOpcode->num_params;
259 } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
261 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
262 if (!lconst) return E_OUTOFMEMORY;
263 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
264 memcpy(&lconst->value, pToken + 1, 4 * sizeof(DWORD));
265 list_add_head(&This->baseShader.constantsI, &lconst->entry);
266 pToken += curOpcode->num_params;
268 } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
270 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
271 if (!lconst) return E_OUTOFMEMORY;
272 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
273 memcpy(&lconst->value, pToken + 1, 1 * sizeof(DWORD));
274 list_add_head(&This->baseShader.constantsB, &lconst->entry);
275 pToken += curOpcode->num_params;
277 /* If there's a loop in the shader */
278 } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
279 WINED3DSIO_REP == curOpcode->opcode) {
280 reg_maps->loop = 1;
281 pToken += curOpcode->num_params;
283 /* For subroutine prototypes */
284 } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
286 DWORD snum = *pToken & WINED3DSP_REGNUM_MASK;
287 reg_maps->labels[snum] = 1;
288 pToken += curOpcode->num_params;
290 /* Set texture, address, temporary registers */
291 } else {
292 int i, limit;
294 /* Declare 1.X samplers implicitly, based on the destination reg. number */
295 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
296 (WINED3DSIO_TEX == curOpcode->opcode ||
297 WINED3DSIO_TEXBEM == curOpcode->opcode ||
298 WINED3DSIO_TEXM3x2TEX == curOpcode->opcode ||
299 WINED3DSIO_TEXM3x3TEX == curOpcode->opcode)) {
301 /* Fake sampler usage, only set reserved bit and ttype */
302 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
304 if(!stateBlock->textures[sampler_code]) {
305 ERR("No texture bound to sampler %d\n", sampler_code);
306 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
307 } else {
308 int texType = IWineD3DBaseTexture_GetTextureDimensions(stateBlock->textures[sampler_code]);
309 switch(texType) {
310 case GL_TEXTURE_1D:
311 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_1D;
312 break;
314 case GL_TEXTURE_2D:
315 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
316 break;
318 case GL_TEXTURE_3D:
319 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_VOLUME;
320 break;
322 case GLTEXTURECUBEMAP:
323 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_CUBE;
324 break;
326 default:
327 ERR("Unexpected gl texture type found: %d\n", texType);
328 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
332 } else if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
333 (WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode ||
334 WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode)) {
336 /* 3D sampler usage, only set reserved bit and ttype
337 * FIXME: This could be either Cube or Volume, but we wouldn't know unless
338 * we waited to generate the shader until the textures were all bound.
339 * For now, use Cube textures because they are more common. */
340 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
341 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_CUBE;
342 } else if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
343 (WINED3DSIO_TEXDP3TEX == curOpcode->opcode)) {
345 /* 1D Sampler usage */
346 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
347 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_1D;
350 /* This will loop over all the registers and try to
351 * make a bitmask of the ones we're interested in.
353 * Relative addressing tokens are ignored, but that's
354 * okay, since we'll catch any address registers when
355 * they are initialized (required by spec) */
357 limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
358 curOpcode->num_params + 1: curOpcode->num_params;
360 for (i = 0; i < limit; ++i) {
362 DWORD param, addr_token, reg, regtype;
363 pToken += shader_get_param(iface, pToken, &param, &addr_token);
365 regtype = shader_get_regtype(param);
366 reg = param & WINED3DSP_REGNUM_MASK;
368 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
370 if (pshader)
371 reg_maps->texcoord[reg] = 1;
372 else
373 reg_maps->address[reg] = 1;
376 else if (WINED3DSPR_TEMP == regtype)
377 reg_maps->temporary[reg] = 1;
379 else if (WINED3DSPR_INPUT == regtype && !pshader)
380 reg_maps->attributes[reg] = 1;
382 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
383 reg_maps->fog = 1;
388 return WINED3D_OK;
391 static void shader_dump_decl_usage(
392 IWineD3DBaseShaderImpl* This,
393 DWORD decl,
394 DWORD param) {
396 DWORD regtype = shader_get_regtype(param);
398 TRACE("dcl");
400 if (regtype == WINED3DSPR_SAMPLER) {
401 DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
403 switch (ttype) {
404 case WINED3DSTT_2D: TRACE("_2d"); break;
405 case WINED3DSTT_CUBE: TRACE("_cube"); break;
406 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
407 default: TRACE("_unknown_ttype(%08x)", ttype);
410 } else {
412 DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
413 DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
415 /* Pixel shaders 3.0 don't have usage semantics */
416 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
417 if (pshader && This->baseShader.hex_version < WINED3DPS_VERSION(3,0))
418 return;
419 else
420 TRACE("_");
422 switch(usage) {
423 case D3DDECLUSAGE_POSITION:
424 TRACE("position%d", idx);
425 break;
426 case D3DDECLUSAGE_BLENDINDICES:
427 TRACE("blend");
428 break;
429 case D3DDECLUSAGE_BLENDWEIGHT:
430 TRACE("weight");
431 break;
432 case D3DDECLUSAGE_NORMAL:
433 TRACE("normal%d", idx);
434 break;
435 case D3DDECLUSAGE_PSIZE:
436 TRACE("psize");
437 break;
438 case D3DDECLUSAGE_COLOR:
439 if(idx == 0) {
440 TRACE("color");
441 } else {
442 TRACE("specular%d", (idx - 1));
444 break;
445 case D3DDECLUSAGE_TEXCOORD:
446 TRACE("texture%d", idx);
447 break;
448 case D3DDECLUSAGE_TANGENT:
449 TRACE("tangent");
450 break;
451 case D3DDECLUSAGE_BINORMAL:
452 TRACE("binormal");
453 break;
454 case D3DDECLUSAGE_TESSFACTOR:
455 TRACE("tessfactor");
456 break;
457 case D3DDECLUSAGE_POSITIONT:
458 TRACE("positionT%d", idx);
459 break;
460 case D3DDECLUSAGE_FOG:
461 TRACE("fog");
462 break;
463 case D3DDECLUSAGE_DEPTH:
464 TRACE("depth");
465 break;
466 case D3DDECLUSAGE_SAMPLE:
467 TRACE("sample");
468 break;
469 default:
470 FIXME("unknown_semantics(%08x)", usage);
475 static void shader_dump_arr_entry(
476 IWineD3DBaseShader *iface,
477 const DWORD param,
478 const DWORD addr_token,
479 unsigned int reg,
480 int input) {
482 char relative =
483 ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
485 if (relative) {
486 TRACE("[");
487 if (addr_token)
488 shader_dump_param(iface, addr_token, 0, input);
489 else
490 TRACE("a0.x");
491 TRACE(" + ");
493 TRACE("%u", reg);
494 if (relative)
495 TRACE("]");
498 void shader_dump_param(
499 IWineD3DBaseShader *iface,
500 const DWORD param,
501 const DWORD addr_token,
502 int input) {
504 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
505 static const char* rastout_reg_names[] = { "oPos", "oFog", "oPts" };
506 char swizzle_reg_chars[4];
508 DWORD reg = param & WINED3DSP_REGNUM_MASK;
509 DWORD regtype = shader_get_regtype(param);
510 DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
512 /* There are some minor differences between pixel and vertex shaders */
513 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
515 /* For one, we'd prefer color components to be shown for pshaders.
516 * FIXME: use the swizzle function for this */
518 swizzle_reg_chars[0] = pshader? 'r': 'x';
519 swizzle_reg_chars[1] = pshader? 'g': 'y';
520 swizzle_reg_chars[2] = pshader? 'b': 'z';
521 swizzle_reg_chars[3] = pshader? 'a': 'w';
523 if (input) {
524 if ( (modifier == WINED3DSPSM_NEG) ||
525 (modifier == WINED3DSPSM_BIASNEG) ||
526 (modifier == WINED3DSPSM_SIGNNEG) ||
527 (modifier == WINED3DSPSM_X2NEG) ||
528 (modifier == WINED3DSPSM_ABSNEG) )
529 TRACE("-");
530 else if (modifier == WINED3DSPSM_COMP)
531 TRACE("1-");
532 else if (modifier == WINED3DSPSM_NOT)
533 TRACE("!");
535 if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG)
536 TRACE("abs(");
539 switch (regtype) {
540 case WINED3DSPR_TEMP:
541 TRACE("r%u", reg);
542 break;
543 case WINED3DSPR_INPUT:
544 TRACE("v");
545 shader_dump_arr_entry(iface, param, addr_token, reg, input);
546 break;
547 case WINED3DSPR_CONST:
548 case WINED3DSPR_CONST2:
549 case WINED3DSPR_CONST3:
550 case WINED3DSPR_CONST4:
551 TRACE("c");
552 shader_dump_arr_entry(iface, param, addr_token, shader_get_float_offset(param), input);
553 break;
554 case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
555 TRACE("%c%u", (pshader? 't':'a'), reg);
556 break;
557 case WINED3DSPR_RASTOUT:
558 TRACE("%s", rastout_reg_names[reg]);
559 break;
560 case WINED3DSPR_COLOROUT:
561 TRACE("oC%u", reg);
562 break;
563 case WINED3DSPR_DEPTHOUT:
564 TRACE("oDepth");
565 break;
566 case WINED3DSPR_ATTROUT:
567 TRACE("oD%u", reg);
568 break;
569 case WINED3DSPR_TEXCRDOUT:
571 /* Vertex shaders >= 3.0 use general purpose output registers
572 * (WINED3DSPR_OUTPUT), which can include an address token */
574 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
575 TRACE("o");
576 shader_dump_arr_entry(iface, param, addr_token, reg, input);
578 else
579 TRACE("oT%u", reg);
580 break;
581 case WINED3DSPR_CONSTINT:
582 TRACE("i");
583 shader_dump_arr_entry(iface, param, addr_token, reg, input);
584 break;
585 case WINED3DSPR_CONSTBOOL:
586 TRACE("b");
587 shader_dump_arr_entry(iface, param, addr_token, reg, input);
588 break;
589 case WINED3DSPR_LABEL:
590 TRACE("l%u", reg);
591 break;
592 case WINED3DSPR_LOOP:
593 TRACE("aL");
594 break;
595 case WINED3DSPR_SAMPLER:
596 TRACE("s%u", reg);
597 break;
598 case WINED3DSPR_PREDICATE:
599 TRACE("p%u", reg);
600 break;
601 default:
602 TRACE("unhandled_rtype(%#x)", regtype);
603 break;
606 if (!input) {
607 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
609 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
610 TRACE(".");
611 if (param & WINED3DSP_WRITEMASK_0) TRACE("%c", swizzle_reg_chars[0]);
612 if (param & WINED3DSP_WRITEMASK_1) TRACE("%c", swizzle_reg_chars[1]);
613 if (param & WINED3DSP_WRITEMASK_2) TRACE("%c", swizzle_reg_chars[2]);
614 if (param & WINED3DSP_WRITEMASK_3) TRACE("%c", swizzle_reg_chars[3]);
617 } else {
618 /** operand input */
619 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
620 DWORD swizzle_r = swizzle & 0x03;
621 DWORD swizzle_g = (swizzle >> 2) & 0x03;
622 DWORD swizzle_b = (swizzle >> 4) & 0x03;
623 DWORD swizzle_a = (swizzle >> 6) & 0x03;
625 if (0 != modifier) {
626 switch (modifier) {
627 case WINED3DSPSM_NONE: break;
628 case WINED3DSPSM_NEG: break;
629 case WINED3DSPSM_NOT: break;
630 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
631 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
632 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
633 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
634 case WINED3DSPSM_COMP: break;
635 case WINED3DSPSM_X2: TRACE("_x2"); break;
636 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
637 case WINED3DSPSM_DZ: TRACE("_dz"); break;
638 case WINED3DSPSM_DW: TRACE("_dw"); break;
639 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
640 case WINED3DSPSM_ABS: TRACE(")"); break;
641 default:
642 TRACE("_unknown_modifier(%#x)", modifier >> WINED3DSP_SRCMOD_SHIFT);
647 * swizzle bits fields:
648 * RRGGBBAA
650 if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) != swizzle) {
651 if (swizzle_r == swizzle_g &&
652 swizzle_r == swizzle_b &&
653 swizzle_r == swizzle_a) {
654 TRACE(".%c", swizzle_reg_chars[swizzle_r]);
655 } else {
656 TRACE(".%c%c%c%c",
657 swizzle_reg_chars[swizzle_r],
658 swizzle_reg_chars[swizzle_g],
659 swizzle_reg_chars[swizzle_b],
660 swizzle_reg_chars[swizzle_a]);
666 /** Shared code in order to generate the bulk of the shader string.
667 Use the shader_header_fct & shader_footer_fct to add strings
668 that are specific to pixel or vertex functions
669 NOTE: A description of how to parse tokens can be found at:
670 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/graphics/hh/graphics/usermodedisplaydriver_shader_cc8e4e05-f5c3-4ec0-8853-8ce07c1551b2.xml.asp */
671 void shader_generate_main(
672 IWineD3DBaseShader *iface,
673 SHADER_BUFFER* buffer,
674 shader_reg_maps* reg_maps,
675 CONST DWORD* pFunction) {
677 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
678 const DWORD *pToken = pFunction;
679 const SHADER_OPCODE *curOpcode = NULL;
680 SHADER_HANDLER hw_fct = NULL;
681 DWORD i;
682 SHADER_OPCODE_ARG hw_arg;
684 /* Initialize current parsing state */
685 hw_arg.shader = iface;
686 hw_arg.buffer = buffer;
687 hw_arg.reg_maps = reg_maps;
688 This->baseShader.parse_state.current_row = 0;
690 /* Second pass, process opcodes */
691 if (NULL != pToken) {
692 while (WINED3DPS_END() != *pToken) {
694 /* Skip version token */
695 if (shader_is_version_token(*pToken)) {
696 ++pToken;
697 continue;
700 /* Skip comment tokens */
701 if (shader_is_comment(*pToken)) {
702 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
703 ++pToken;
704 TRACE("#%s\n", (char*)pToken);
705 pToken += comment_len;
706 continue;
709 /* Read opcode */
710 hw_arg.opcode_token = *pToken++;
711 curOpcode = shader_get_opcode(iface, hw_arg.opcode_token);
713 /* Select handler */
714 if (curOpcode == NULL)
715 hw_fct = NULL;
716 else if (This->baseShader.shader_mode == SHADER_GLSL)
717 hw_fct = curOpcode->hw_glsl_fct;
718 else if (This->baseShader.shader_mode == SHADER_ARB)
719 hw_fct = curOpcode->hw_fct;
721 /* Unknown opcode and its parameters */
722 if (NULL == curOpcode) {
723 FIXME("Unrecognized opcode: token=%08x\n", hw_arg.opcode_token);
724 pToken += shader_skip_unrecognized(iface, pToken);
726 /* Nothing to do */
727 } else if (WINED3DSIO_DCL == curOpcode->opcode ||
728 WINED3DSIO_NOP == curOpcode->opcode ||
729 WINED3DSIO_DEF == curOpcode->opcode ||
730 WINED3DSIO_DEFI == curOpcode->opcode ||
731 WINED3DSIO_DEFB == curOpcode->opcode ||
732 WINED3DSIO_PHASE == curOpcode->opcode ||
733 WINED3DSIO_RET == curOpcode->opcode) {
735 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
737 /* If a generator function is set for current shader target, use it */
738 } else if (hw_fct != NULL) {
740 hw_arg.opcode = curOpcode;
742 /* Destination token */
743 if (curOpcode->dst_token) {
745 DWORD param, addr_token = 0;
746 pToken += shader_get_param(iface, pToken, &param, &addr_token);
747 hw_arg.dst = param;
748 hw_arg.dst_addr = addr_token;
751 /* Predication token */
752 if (hw_arg.opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
753 hw_arg.predicate = *pToken++;
755 /* Other source tokens */
756 for (i = 0; i < (curOpcode->num_params - curOpcode->dst_token); i++) {
758 DWORD param, addr_token = 0;
759 pToken += shader_get_param(iface, pToken, &param, &addr_token);
760 hw_arg.src[i] = param;
761 hw_arg.src_addr[i] = addr_token;
764 /* Call appropriate function for output target */
765 hw_fct(&hw_arg);
767 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
768 if (This->baseShader.shader_mode == SHADER_GLSL)
769 shader_glsl_add_instruction_modifiers(&hw_arg);
771 /* Unhandled opcode */
772 } else {
774 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
775 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
778 /* TODO: What about result.depth? */
783 void shader_dump_ins_modifiers(const DWORD output) {
785 DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
786 DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
788 switch (shift) {
789 case 0: break;
790 case 13: TRACE("_d8"); break;
791 case 14: TRACE("_d4"); break;
792 case 15: TRACE("_d2"); break;
793 case 1: TRACE("_x2"); break;
794 case 2: TRACE("_x4"); break;
795 case 3: TRACE("_x8"); break;
796 default: TRACE("_unhandled_shift(%d)", shift); break;
799 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
800 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
801 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
803 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
804 if (mmask)
805 FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
808 /* First pass: trace shader, initialize length and version */
809 void shader_trace_init(
810 IWineD3DBaseShader *iface,
811 const DWORD* pFunction) {
813 IWineD3DBaseShaderImpl *This =(IWineD3DBaseShaderImpl *)iface;
815 const DWORD* pToken = pFunction;
816 const SHADER_OPCODE* curOpcode = NULL;
817 DWORD opcode_token;
818 unsigned int len = 0;
819 DWORD i;
821 TRACE("(%p) : Parsing programme\n", This);
823 if (NULL != pToken) {
824 while (WINED3DVS_END() != *pToken) {
825 if (shader_is_version_token(*pToken)) { /** version */
826 This->baseShader.hex_version = *pToken;
827 TRACE("%s_%u_%u\n", shader_is_pshader_version(This->baseShader.hex_version)? "ps": "vs",
828 WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version),
829 WINED3DSHADER_VERSION_MINOR(This->baseShader.hex_version));
830 ++pToken;
831 ++len;
832 continue;
834 if (shader_is_comment(*pToken)) { /** comment */
835 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
836 ++pToken;
837 TRACE("//%s\n", (char*)pToken);
838 pToken += comment_len;
839 len += comment_len + 1;
840 continue;
842 opcode_token = *pToken++;
843 curOpcode = shader_get_opcode(iface, opcode_token);
844 len++;
846 if (NULL == curOpcode) {
847 int tokens_read;
848 FIXME("Unrecognized opcode: token=%08x\n", opcode_token);
849 tokens_read = shader_skip_unrecognized(iface, pToken);
850 pToken += tokens_read;
851 len += tokens_read;
853 } else {
854 if (curOpcode->opcode == WINED3DSIO_DCL) {
856 DWORD usage = *pToken;
857 DWORD param = *(pToken + 1);
859 shader_dump_decl_usage(This, usage, param);
860 shader_dump_ins_modifiers(param);
861 TRACE(" ");
862 shader_dump_param(iface, param, 0, 0);
863 pToken += 2;
864 len += 2;
866 } else if (curOpcode->opcode == WINED3DSIO_DEF) {
868 unsigned int offset = shader_get_float_offset(*pToken);
870 TRACE("def c%u = %f, %f, %f, %f", offset,
871 *(float *)(pToken + 1),
872 *(float *)(pToken + 2),
873 *(float *)(pToken + 3),
874 *(float *)(pToken + 4));
876 pToken += 5;
877 len += 5;
878 } else if (curOpcode->opcode == WINED3DSIO_DEFI) {
880 TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
881 *(pToken + 1),
882 *(pToken + 2),
883 *(pToken + 3),
884 *(pToken + 4));
886 pToken += 5;
887 len += 5;
889 } else if (curOpcode->opcode == WINED3DSIO_DEFB) {
891 TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
892 *(pToken + 1)? "true": "false");
894 pToken += 2;
895 len += 2;
897 } else {
899 DWORD param, addr_token;
900 int tokens_read;
902 /* Print out predication source token first - it follows
903 * the destination token. */
904 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
905 TRACE("(");
906 shader_dump_param(iface, *(pToken + 2), 0, 1);
907 TRACE(") ");
910 TRACE("%s", curOpcode->name);
912 if (curOpcode->opcode == WINED3DSIO_IFC ||
913 curOpcode->opcode == WINED3DSIO_BREAKC) {
915 DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
916 switch (op) {
917 case COMPARISON_GT: TRACE("_gt"); break;
918 case COMPARISON_EQ: TRACE("_eq"); break;
919 case COMPARISON_GE: TRACE("_ge"); break;
920 case COMPARISON_LT: TRACE("_lt"); break;
921 case COMPARISON_NE: TRACE("_ne"); break;
922 case COMPARISON_LE: TRACE("_le"); break;
923 default:
924 TRACE("_(%u)", op);
928 /* Destination token */
929 if (curOpcode->dst_token) {
931 /* Destination token */
932 tokens_read = shader_get_param(iface, pToken, &param, &addr_token);
933 pToken += tokens_read;
934 len += tokens_read;
936 shader_dump_ins_modifiers(param);
937 TRACE(" ");
938 shader_dump_param(iface, param, addr_token, 0);
941 /* Predication token - already printed out, just skip it */
942 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
943 pToken++;
944 len++;
947 /* Other source tokens */
948 for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i) {
950 tokens_read = shader_get_param(iface, pToken, &param, &addr_token);
951 pToken += tokens_read;
952 len += tokens_read;
954 TRACE((i == 0)? " " : ", ");
955 shader_dump_param(iface, param, addr_token, 1);
958 TRACE("\n");
961 This->baseShader.functionLength = (len + 1) * sizeof(DWORD);
962 } else {
963 This->baseShader.functionLength = 1; /* no Function defined use fixed function vertex processing */
967 void shader_delete_constant_list(
968 struct list* clist) {
970 struct list *ptr;
971 struct local_constant* constant;
973 ptr = list_head(clist);
974 while (ptr) {
975 constant = LIST_ENTRY(ptr, struct local_constant, entry);
976 ptr = list_next(clist, ptr);
977 HeapFree(GetProcessHeap(), 0, constant);