wined3d: Always select the correct shader pair in the vertexdeclaration() state handler.
[wine] / dlls / wined3d / baseshader.c
1 /*
2  * shaders implementation
3  *
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  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "config.h"
26 #include <string.h>
27 #include <stdio.h>
28 #include "wined3d_private.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
31
32 #define GLNAME_REQUIRE_GLSL  ((const char *)1)
33
34 inline static BOOL shader_is_version_token(DWORD token) {
35     return shader_is_pshader_version(token) ||
36            shader_is_vshader_version(token);
37 }
38
39 int shader_addline(
40     SHADER_BUFFER* buffer,  
41     const char *format, ...) {
42
43     char* base = buffer->buffer + buffer->bsize;
44     int rc;
45
46     va_list args;
47     va_start(args, format);
48     rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
49     va_end(args);
50
51     if (rc < 0 ||                                   /* C89 */ 
52         rc > SHADER_PGMSIZE - 1 - buffer->bsize) {  /* C99 */
53
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;
58     }
59
60     if (buffer->newline) {
61         TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
62         buffer->newline = FALSE;
63     } else {
64         TRACE("%s", base);
65     }
66
67     buffer->bsize += rc;
68     if (buffer->buffer[buffer->bsize-1] == '\n') {
69         buffer->lineNo++;
70         buffer->newline = TRUE;
71     }
72     return 0;
73 }
74
75 const SHADER_OPCODE* shader_get_opcode(
76     IWineD3DBaseShader *iface, const DWORD code) {
77
78     IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl*) iface;
79
80     DWORD i = 0;
81     DWORD hex_version = This->baseShader.hex_version;
82     const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;
83
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];
90         }
91         ++i;
92     }
93     FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n", 
94        code, code, code & WINED3DSI_OPCODE_MASK, hex_version);
95     return NULL;
96 }
97
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,
103     const DWORD* pToken,
104     DWORD* param,
105     DWORD* addr_token) {
106
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 */
111
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);
115
116     *param = *pToken;
117     *addr_token = rel_token? *(pToken + 1): 0;
118     return rel_token? 2:1;
119 }
120
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) {
126
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 */
129
130     return (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2)?
131         ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT):
132         curOpcode->num_params;
133 }
134
135 /* Read the parameters of an unrecognized opcode from the input stream
136  * Return the number of tokens read. 
137  * 
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 */
141
142 int shader_skip_unrecognized(
143     IWineD3DBaseShader* iface,
144     const DWORD* pToken) {
145
146     int tokens_read = 0;
147     int i = 0;
148
149     /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
150     while (*pToken & 0x80000000) {
151
152         DWORD param, addr_token;
153         tokens_read += shader_get_param(iface, pToken, &param, &addr_token);
154         pToken += tokens_read;
155
156         FIXME("Unrecognized opcode param: token=%08x "
157             "addr_token=%08x name=", param, addr_token);
158         shader_dump_param(iface, param, addr_token, i);
159         FIXME("\n");
160         ++i;
161     }
162     return tokens_read;
163 }
164
165 /* Convert floating point offset relative
166  * to a register file to an absolute offset for float constants */
167
168 unsigned int shader_get_float_offset(const DWORD reg) {
169
170      unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
171      int regtype = shader_get_regtype(reg);
172
173      switch (regtype) {
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;
178         default:
179             FIXME("Unsupported register type: %d\n", regtype);
180             return regnum;
181      }
182 }
183
184 /* Note that this does not count the loop register
185  * as an address register. */
186
187 HRESULT shader_get_registers_used(
188     IWineD3DBaseShader *iface,
189     shader_reg_maps* reg_maps,
190     semantic* semantics_in,
191     semantic* semantics_out,
192     CONST DWORD* pToken,
193     IWineD3DStateBlockImpl *stateBlock) {
194
195     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
196
197     /* There are some minor differences between pixel and vertex shaders */
198     char pshader = shader_is_pshader_version(This->baseShader.hex_version);
199
200     if (pToken == NULL)
201         return WINED3D_OK;
202
203     while (WINED3DVS_END() != *pToken) {
204         CONST SHADER_OPCODE* curOpcode;
205         DWORD opcode_token;
206
207         /* Skip version */
208         if (shader_is_version_token(*pToken)) {
209              ++pToken;
210              continue;
211
212         /* Skip comments */
213         } else if (shader_is_comment(*pToken)) {
214              DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
215              ++pToken;
216              pToken += comment_len;
217              continue;
218         }
219
220         /* Fetch opcode */
221         opcode_token = *pToken++;
222         curOpcode = shader_get_opcode(iface, opcode_token);
223
224         /* Unhandled opcode, and its parameters */
225         if (NULL == curOpcode) {
226            while (*pToken & 0x80000000)
227                ++pToken;
228
229         /* Handle declarations */
230         } else if (WINED3DSIO_DCL == curOpcode->opcode) {
231
232             DWORD usage = *pToken++;
233             DWORD param = *pToken++;
234             DWORD regtype = shader_get_regtype(param);
235             unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
236
237             /* Vshader: mark attributes used
238                Pshader: mark 3.0 input registers used, save token */
239             if (WINED3DSPR_INPUT == regtype) {
240
241                 if (!pshader)
242                     reg_maps->attributes[regnum] = 1;
243                 else
244                     reg_maps->packed_input[regnum] = 1;
245
246                 semantics_in[regnum].usage = usage;
247                 semantics_in[regnum].reg = param;
248
249             /* Vshader: mark 3.0 output registers used, save token */
250             } else if (WINED3DSPR_OUTPUT == regtype) {
251                 reg_maps->packed_output[regnum] = 1;
252                 semantics_out[regnum].usage = usage;
253                 semantics_out[regnum].reg = param;
254
255             /* Save sampler usage token */
256             } else if (WINED3DSPR_SAMPLER == regtype)
257                 reg_maps->samplers[regnum] = usage;
258
259         } else if (WINED3DSIO_DEF == curOpcode->opcode) {
260
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.constantsF, &lconst->entry);
266             pToken += curOpcode->num_params;
267
268         } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
269
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, 4 * sizeof(DWORD));
274             list_add_head(&This->baseShader.constantsI, &lconst->entry);
275             pToken += curOpcode->num_params;
276
277         } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
278
279             local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
280             if (!lconst) return E_OUTOFMEMORY;
281             lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
282             memcpy(&lconst->value, pToken + 1, 1 * sizeof(DWORD));
283             list_add_head(&This->baseShader.constantsB, &lconst->entry);
284             pToken += curOpcode->num_params;
285
286         /* If there's a loop in the shader */
287         } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
288                    WINED3DSIO_REP == curOpcode->opcode) {
289             reg_maps->loop = 1;
290             pToken += curOpcode->num_params;
291    
292         /* For subroutine prototypes */
293         } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
294
295             DWORD snum = *pToken & WINED3DSP_REGNUM_MASK; 
296             reg_maps->labels[snum] = 1;
297             pToken += curOpcode->num_params;
298  
299         /* Set texture, address, temporary registers */
300         } else {
301             int i, limit;
302
303             /* Declare 1.X samplers implicitly, based on the destination reg. number */
304             if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 && 
305                 (WINED3DSIO_TEX == curOpcode->opcode ||
306                  WINED3DSIO_TEXBEM == curOpcode->opcode ||
307                  WINED3DSIO_TEXBEML == curOpcode->opcode ||
308                  WINED3DSIO_TEXDP3TEX == curOpcode->opcode ||
309                  WINED3DSIO_TEXM3x2TEX == curOpcode->opcode ||
310                  WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode ||
311                  WINED3DSIO_TEXM3x3TEX == curOpcode->opcode ||
312                  WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode ||
313                  WINED3DSIO_TEXREG2AR == curOpcode->opcode ||
314                  WINED3DSIO_TEXREG2GB == curOpcode->opcode ||
315                  WINED3DSIO_TEXREG2RGB == curOpcode->opcode)) {
316
317                 /* Fake sampler usage, only set reserved bit and ttype */
318                 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
319
320                 if(!stateBlock->textures[sampler_code]) {
321                     ERR("No texture bound to sampler %d\n", sampler_code);
322                     reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
323                 } else {
324                     int texType = IWineD3DBaseTexture_GetTextureDimensions(stateBlock->textures[sampler_code]);
325                     switch(texType) {
326                         case GL_TEXTURE_1D:
327                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_1D;
328                             break;
329
330                         case GL_TEXTURE_2D:
331                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
332                             break;
333
334                         case GL_TEXTURE_3D:
335                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_VOLUME;
336                             break;
337
338                         case GL_TEXTURE_CUBE_MAP_ARB:
339                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_CUBE;
340                             break;
341
342                         default:
343                             ERR("Unexpected gl texture type found: %d\n", texType);
344                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
345                     }
346                 }
347             }
348
349             /* This will loop over all the registers and try to
350              * make a bitmask of the ones we're interested in. 
351              *
352              * Relative addressing tokens are ignored, but that's 
353              * okay, since we'll catch any address registers when 
354              * they are initialized (required by spec) */
355
356             limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
357                 curOpcode->num_params + 1: curOpcode->num_params;
358
359             for (i = 0; i < limit; ++i) {
360
361                 DWORD param, addr_token, reg, regtype;
362                 pToken += shader_get_param(iface, pToken, &param, &addr_token);
363
364                 regtype = shader_get_regtype(param);
365                 reg = param & WINED3DSP_REGNUM_MASK;
366
367                 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
368
369                     if (pshader)
370                         reg_maps->texcoord[reg] = 1;
371                     else
372                         reg_maps->address[reg] = 1;
373                 }
374
375                 else if (WINED3DSPR_TEMP == regtype)
376                     reg_maps->temporary[reg] = 1;
377
378                 else if (WINED3DSPR_INPUT == regtype && !pshader)
379                     reg_maps->attributes[reg] = 1;
380
381                 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
382                     reg_maps->fog = 1;
383              }
384         }
385     }
386
387     return WINED3D_OK;
388 }
389
390 static void shader_dump_decl_usage(
391     IWineD3DBaseShaderImpl* This,
392     DWORD decl, 
393     DWORD param) {
394
395     DWORD regtype = shader_get_regtype(param);
396
397     TRACE("dcl");
398
399     if (regtype == WINED3DSPR_SAMPLER) {
400         DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
401
402         switch (ttype) {
403             case WINED3DSTT_2D: TRACE("_2d"); break;
404             case WINED3DSTT_CUBE: TRACE("_cube"); break;
405             case WINED3DSTT_VOLUME: TRACE("_volume"); break;
406             default: TRACE("_unknown_ttype(%08x)", ttype); 
407        }
408
409     } else { 
410
411         DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
412         DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
413
414         /* Pixel shaders 3.0 don't have usage semantics */
415         char pshader = shader_is_pshader_version(This->baseShader.hex_version);
416         if (pshader && This->baseShader.hex_version < WINED3DPS_VERSION(3,0))
417             return;
418         else
419             TRACE("_");
420
421         switch(usage) {
422         case D3DDECLUSAGE_POSITION:
423             TRACE("position%d", idx);
424             break;
425         case D3DDECLUSAGE_BLENDINDICES:
426             TRACE("blend");
427             break;
428         case D3DDECLUSAGE_BLENDWEIGHT:
429             TRACE("weight");
430             break;
431         case D3DDECLUSAGE_NORMAL:
432             TRACE("normal%d", idx);
433             break;
434         case D3DDECLUSAGE_PSIZE:
435             TRACE("psize");
436             break;
437         case D3DDECLUSAGE_COLOR:
438             if(idx == 0)  {
439                 TRACE("color");
440             } else {
441                 TRACE("specular%d", (idx - 1));
442             }
443             break;
444         case D3DDECLUSAGE_TEXCOORD:
445             TRACE("texture%d", idx);
446             break;
447         case D3DDECLUSAGE_TANGENT:
448             TRACE("tangent");
449             break;
450         case D3DDECLUSAGE_BINORMAL:
451             TRACE("binormal");
452             break;
453         case D3DDECLUSAGE_TESSFACTOR:
454             TRACE("tessfactor");
455             break;
456         case D3DDECLUSAGE_POSITIONT:
457             TRACE("positionT%d", idx);
458             break;
459         case D3DDECLUSAGE_FOG:
460             TRACE("fog");
461             break;
462         case D3DDECLUSAGE_DEPTH:
463             TRACE("depth");
464             break;
465         case D3DDECLUSAGE_SAMPLE:
466             TRACE("sample");
467             break;
468         default:
469             FIXME("unknown_semantics(%08x)", usage);
470         }
471     }
472 }
473
474 static void shader_dump_arr_entry(
475     IWineD3DBaseShader *iface,
476     const DWORD param,
477     const DWORD addr_token,
478     unsigned int reg,
479     int input) {
480
481     char relative =
482         ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
483
484     if (relative) {
485         TRACE("[");
486         if (addr_token)
487             shader_dump_param(iface, addr_token, 0, input);
488         else
489             TRACE("a0.x");
490         TRACE(" + ");
491      }
492      TRACE("%u", reg);
493      if (relative)
494          TRACE("]");
495 }
496
497 void shader_dump_param(
498     IWineD3DBaseShader *iface,
499     const DWORD param, 
500     const DWORD addr_token,
501     int input) {
502
503     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
504     static const char * const rastout_reg_names[] = { "oPos", "oFog", "oPts" };
505     char swizzle_reg_chars[4];
506
507     DWORD reg = param & WINED3DSP_REGNUM_MASK;
508     DWORD regtype = shader_get_regtype(param);
509     DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
510
511     /* There are some minor differences between pixel and vertex shaders */
512     char pshader = shader_is_pshader_version(This->baseShader.hex_version);
513
514     /* For one, we'd prefer color components to be shown for pshaders.
515      * FIXME: use the swizzle function for this */
516
517     swizzle_reg_chars[0] = pshader? 'r': 'x';
518     swizzle_reg_chars[1] = pshader? 'g': 'y';
519     swizzle_reg_chars[2] = pshader? 'b': 'z';
520     swizzle_reg_chars[3] = pshader? 'a': 'w';
521
522     if (input) {
523         if ( (modifier == WINED3DSPSM_NEG) ||
524              (modifier == WINED3DSPSM_BIASNEG) ||
525              (modifier == WINED3DSPSM_SIGNNEG) ||
526              (modifier == WINED3DSPSM_X2NEG) ||
527              (modifier == WINED3DSPSM_ABSNEG) )
528             TRACE("-");
529         else if (modifier == WINED3DSPSM_COMP)
530             TRACE("1-");
531         else if (modifier == WINED3DSPSM_NOT)
532             TRACE("!");
533
534         if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG) 
535             TRACE("abs(");
536     }
537
538     switch (regtype) {
539         case WINED3DSPR_TEMP:
540             TRACE("r%u", reg);
541             break;
542         case WINED3DSPR_INPUT:
543             TRACE("v");
544             shader_dump_arr_entry(iface, param, addr_token, reg, input);
545             break;
546         case WINED3DSPR_CONST:
547         case WINED3DSPR_CONST2:
548         case WINED3DSPR_CONST3:
549         case WINED3DSPR_CONST4:
550             TRACE("c");
551             shader_dump_arr_entry(iface, param, addr_token, shader_get_float_offset(param), input);
552             break;
553         case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
554             TRACE("%c%u", (pshader? 't':'a'), reg);
555             break;        
556         case WINED3DSPR_RASTOUT:
557             TRACE("%s", rastout_reg_names[reg]);
558             break;
559         case WINED3DSPR_COLOROUT:
560             TRACE("oC%u", reg);
561             break;
562         case WINED3DSPR_DEPTHOUT:
563             TRACE("oDepth");
564             break;
565         case WINED3DSPR_ATTROUT:
566             TRACE("oD%u", reg);
567             break;
568         case WINED3DSPR_TEXCRDOUT: 
569
570             /* Vertex shaders >= 3.0 use general purpose output registers
571              * (WINED3DSPR_OUTPUT), which can include an address token */
572
573             if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
574                 TRACE("o");
575                 shader_dump_arr_entry(iface, param, addr_token, reg, input);
576             }
577             else 
578                TRACE("oT%u", reg);
579             break;
580         case WINED3DSPR_CONSTINT:
581             TRACE("i");
582             shader_dump_arr_entry(iface, param, addr_token, reg, input);
583             break;
584         case WINED3DSPR_CONSTBOOL:
585             TRACE("b");
586             shader_dump_arr_entry(iface, param, addr_token, reg, input);
587             break;
588         case WINED3DSPR_LABEL:
589             TRACE("l%u", reg);
590             break;
591         case WINED3DSPR_LOOP:
592             TRACE("aL");
593             break;
594         case WINED3DSPR_SAMPLER:
595             TRACE("s%u", reg);
596             break;
597         case WINED3DSPR_PREDICATE:
598             TRACE("p%u", reg);
599             break;
600         default:
601             TRACE("unhandled_rtype(%#x)", regtype);
602             break;
603    }
604
605    if (!input) {
606        /* operand output (for modifiers and shift, see dump_ins_modifiers) */
607
608        if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
609            TRACE(".");
610            if (param & WINED3DSP_WRITEMASK_0) TRACE("%c", swizzle_reg_chars[0]);
611            if (param & WINED3DSP_WRITEMASK_1) TRACE("%c", swizzle_reg_chars[1]);
612            if (param & WINED3DSP_WRITEMASK_2) TRACE("%c", swizzle_reg_chars[2]);
613            if (param & WINED3DSP_WRITEMASK_3) TRACE("%c", swizzle_reg_chars[3]);
614        }
615
616    } else {
617         /** operand input */
618         DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
619         DWORD swizzle_r = swizzle & 0x03;
620         DWORD swizzle_g = (swizzle >> 2) & 0x03;
621         DWORD swizzle_b = (swizzle >> 4) & 0x03;
622         DWORD swizzle_a = (swizzle >> 6) & 0x03;
623
624         if (0 != modifier) {
625             switch (modifier) {
626                 case WINED3DSPSM_NONE:    break;
627                 case WINED3DSPSM_NEG:     break;
628                 case WINED3DSPSM_NOT:     break;
629                 case WINED3DSPSM_BIAS:    TRACE("_bias"); break;
630                 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
631                 case WINED3DSPSM_SIGN:    TRACE("_bx2"); break;
632                 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
633                 case WINED3DSPSM_COMP:    break;
634                 case WINED3DSPSM_X2:      TRACE("_x2"); break;
635                 case WINED3DSPSM_X2NEG:   TRACE("_x2"); break;
636                 case WINED3DSPSM_DZ:      TRACE("_dz"); break;
637                 case WINED3DSPSM_DW:      TRACE("_dw"); break;
638                 case WINED3DSPSM_ABSNEG:  TRACE(")"); break;
639                 case WINED3DSPSM_ABS:     TRACE(")"); break;
640                 default:
641                     TRACE("_unknown_modifier(%#x)", modifier >> WINED3DSP_SRCMOD_SHIFT);
642             }
643         }
644
645         /**
646         * swizzle bits fields:
647         *  RRGGBBAA
648         */
649         if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) != swizzle) {
650             if (swizzle_r == swizzle_g &&
651                 swizzle_r == swizzle_b &&
652                 swizzle_r == swizzle_a) {
653                     TRACE(".%c", swizzle_reg_chars[swizzle_r]);
654             } else {
655                 TRACE(".%c%c%c%c",
656                 swizzle_reg_chars[swizzle_r],
657                 swizzle_reg_chars[swizzle_g],
658                 swizzle_reg_chars[swizzle_b],
659                 swizzle_reg_chars[swizzle_a]);
660             }
661         }
662     }
663 }
664
665 /** Shared code in order to generate the bulk of the shader string.
666     Use the shader_header_fct & shader_footer_fct to add strings
667     that are specific to pixel or vertex functions
668     NOTE: A description of how to parse tokens can be found at:
669           http://msdn.microsoft.com/library/default.asp?url=/library/en-us/graphics/hh/graphics/usermodedisplaydriver_shader_cc8e4e05-f5c3-4ec0-8853-8ce07c1551b2.xml.asp */
670 void shader_generate_main(
671     IWineD3DBaseShader *iface,
672     SHADER_BUFFER* buffer,
673     shader_reg_maps* reg_maps,
674     CONST DWORD* pFunction) {
675
676     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
677     const DWORD *pToken = pFunction;
678     const SHADER_OPCODE *curOpcode = NULL;
679     SHADER_HANDLER hw_fct = NULL;
680     DWORD i;
681     SHADER_OPCODE_ARG hw_arg;
682
683     /* Initialize current parsing state */
684     hw_arg.shader = iface;
685     hw_arg.buffer = buffer;
686     hw_arg.reg_maps = reg_maps;
687     This->baseShader.parse_state.current_row = 0;
688
689     /* Second pass, process opcodes */
690     if (NULL != pToken) {
691         while (WINED3DPS_END() != *pToken) {
692
693             /* Skip version token */
694             if (shader_is_version_token(*pToken)) {
695                 ++pToken;
696                 continue;
697             }
698
699             /* Skip comment tokens */
700             if (shader_is_comment(*pToken)) {
701                 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
702                 ++pToken;
703                 TRACE("#%s\n", (const char*)pToken);
704                 pToken += comment_len;
705                 continue;
706             }
707
708             /* Read opcode */
709             hw_arg.opcode_token = *pToken++;
710             curOpcode = shader_get_opcode(iface, hw_arg.opcode_token);
711
712             /* Select handler */
713             if (curOpcode == NULL)
714                 hw_fct = NULL;
715             else if (This->baseShader.shader_mode == SHADER_GLSL)
716                 hw_fct = curOpcode->hw_glsl_fct;
717             else if (This->baseShader.shader_mode == SHADER_ARB)
718                 hw_fct = curOpcode->hw_fct;
719
720             /* Unknown opcode and its parameters */
721             if (NULL == curOpcode) {
722                 FIXME("Unrecognized opcode: token=%08x\n", hw_arg.opcode_token);
723                 pToken += shader_skip_unrecognized(iface, pToken); 
724
725             /* Nothing to do */
726             } else if (WINED3DSIO_DCL == curOpcode->opcode ||
727                        WINED3DSIO_NOP == curOpcode->opcode ||
728                        WINED3DSIO_DEF == curOpcode->opcode ||
729                        WINED3DSIO_DEFI == curOpcode->opcode ||
730                        WINED3DSIO_DEFB == curOpcode->opcode ||
731                        WINED3DSIO_PHASE == curOpcode->opcode ||
732                        WINED3DSIO_RET == curOpcode->opcode) {
733
734                 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
735
736             /* If a generator function is set for current shader target, use it */
737             } else if (hw_fct != NULL) {
738
739                 hw_arg.opcode = curOpcode;
740
741                 /* Destination token */
742                 if (curOpcode->dst_token) {
743
744                     DWORD param, addr_token = 0;
745                     pToken += shader_get_param(iface, pToken, &param, &addr_token);
746                     hw_arg.dst = param;
747                     hw_arg.dst_addr = addr_token;
748                 }
749
750                 /* Predication token */
751                 if (hw_arg.opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) 
752                     hw_arg.predicate = *pToken++;
753
754                 /* Other source tokens */
755                 for (i = 0; i < (curOpcode->num_params - curOpcode->dst_token); i++) {
756
757                     DWORD param, addr_token = 0; 
758                     pToken += shader_get_param(iface, pToken, &param, &addr_token);
759                     hw_arg.src[i] = param;
760                     hw_arg.src_addr[i] = addr_token;
761                 }
762
763                 /* Call appropriate function for output target */
764                 hw_fct(&hw_arg);
765
766                 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
767                 if (This->baseShader.shader_mode == SHADER_GLSL)
768                     shader_glsl_add_instruction_modifiers(&hw_arg);
769
770             /* Unhandled opcode */
771             } else {
772
773                 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
774                 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
775             }
776         }
777         /* TODO: What about result.depth? */
778
779     }
780 }
781
782 void shader_dump_ins_modifiers(const DWORD output) {
783
784     DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
785     DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
786
787     switch (shift) {
788         case 0: break;
789         case 13: TRACE("_d8"); break;
790         case 14: TRACE("_d4"); break;
791         case 15: TRACE("_d2"); break;
792         case 1: TRACE("_x2"); break;
793         case 2: TRACE("_x4"); break;
794         case 3: TRACE("_x8"); break;
795         default: TRACE("_unhandled_shift(%d)", shift); break;
796     }
797
798     if (mmask & WINED3DSPDM_SATURATE)         TRACE("_sat");
799     if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
800     if (mmask & WINED3DSPDM_MSAMPCENTROID)    TRACE("_centroid");
801
802     mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
803     if (mmask)
804         FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
805 }
806
807 /* First pass: trace shader, initialize length and version */
808 void shader_trace_init(
809     IWineD3DBaseShader *iface,
810     const DWORD* pFunction) {
811
812     IWineD3DBaseShaderImpl *This =(IWineD3DBaseShaderImpl *)iface;
813
814     const DWORD* pToken = pFunction;
815     const SHADER_OPCODE* curOpcode = NULL;
816     DWORD opcode_token;
817     unsigned int len = 0;
818     DWORD i;
819
820     TRACE("(%p) : Parsing programme\n", This);
821
822     if (NULL != pToken) {
823         while (WINED3DVS_END() != *pToken) {
824             if (shader_is_version_token(*pToken)) { /** version */
825                 This->baseShader.hex_version = *pToken;
826                 TRACE("%s_%u_%u\n", shader_is_pshader_version(This->baseShader.hex_version)? "ps": "vs",
827                     WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version),
828                     WINED3DSHADER_VERSION_MINOR(This->baseShader.hex_version));
829                 ++pToken;
830                 ++len;
831                 continue;
832             }
833             if (shader_is_comment(*pToken)) { /** comment */
834                 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
835                 ++pToken;
836                 TRACE("//%s\n", (const char*)pToken);
837                 pToken += comment_len;
838                 len += comment_len + 1;
839                 continue;
840             }
841             opcode_token = *pToken++;
842             curOpcode = shader_get_opcode(iface, opcode_token);
843             len++;
844
845             if (NULL == curOpcode) {
846                 int tokens_read;
847                 FIXME("Unrecognized opcode: token=%08x\n", opcode_token);
848                 tokens_read = shader_skip_unrecognized(iface, pToken);
849                 pToken += tokens_read;
850                 len += tokens_read;
851
852             } else {
853                 if (curOpcode->opcode == WINED3DSIO_DCL) {
854
855                     DWORD usage = *pToken;
856                     DWORD param = *(pToken + 1);
857
858                     shader_dump_decl_usage(This, usage, param);
859                     shader_dump_ins_modifiers(param);
860                     TRACE(" ");
861                     shader_dump_param(iface, param, 0, 0);
862                     pToken += 2;
863                     len += 2;
864
865                 } else if (curOpcode->opcode == WINED3DSIO_DEF) {
866
867                         unsigned int offset = shader_get_float_offset(*pToken);
868
869                         TRACE("def c%u = %f, %f, %f, %f", offset,
870                             *(const float *)(pToken + 1),
871                             *(const float *)(pToken + 2),
872                             *(const float *)(pToken + 3),
873                             *(const float *)(pToken + 4));
874
875                         pToken += 5;
876                         len += 5;
877                 } else if (curOpcode->opcode == WINED3DSIO_DEFI) {
878
879                         TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
880                             *(pToken + 1),
881                             *(pToken + 2),
882                             *(pToken + 3),
883                             *(pToken + 4));
884
885                         pToken += 5;
886                         len += 5;
887
888                 } else if (curOpcode->opcode == WINED3DSIO_DEFB) {
889
890                         TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
891                             *(pToken + 1)? "true": "false");
892
893                         pToken += 2;
894                         len += 2;
895
896                 } else {
897
898                     DWORD param, addr_token;
899                     int tokens_read;
900
901                     /* Print out predication source token first - it follows
902                      * the destination token. */
903                     if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
904                         TRACE("(");
905                         shader_dump_param(iface, *(pToken + 2), 0, 1);
906                         TRACE(") ");
907                     }
908
909                     TRACE("%s", curOpcode->name);
910
911                     if (curOpcode->opcode == WINED3DSIO_IFC ||
912                         curOpcode->opcode == WINED3DSIO_BREAKC) {
913
914                         DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
915                         switch (op) {
916                             case COMPARISON_GT: TRACE("_gt"); break;
917                             case COMPARISON_EQ: TRACE("_eq"); break;
918                             case COMPARISON_GE: TRACE("_ge"); break;
919                             case COMPARISON_LT: TRACE("_lt"); break;
920                             case COMPARISON_NE: TRACE("_ne"); break;
921                             case COMPARISON_LE: TRACE("_le"); break;
922                             default:
923                                 TRACE("_(%u)", op);
924                         }
925                     }
926
927                     /* Destination token */
928                     if (curOpcode->dst_token) {
929
930                         /* Destination token */
931                         tokens_read = shader_get_param(iface, pToken, &param, &addr_token);
932                         pToken += tokens_read;
933                         len += tokens_read;
934
935                         shader_dump_ins_modifiers(param);
936                         TRACE(" ");
937                         shader_dump_param(iface, param, addr_token, 0);
938                     }
939
940                     /* Predication token - already printed out, just skip it */
941                     if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
942                         pToken++;
943                         len++;
944                     }
945
946                     /* Other source tokens */
947                     for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i) {
948
949                         tokens_read = shader_get_param(iface, pToken, &param, &addr_token);
950                         pToken += tokens_read;
951                         len += tokens_read;
952
953                         TRACE((i == 0)? " " : ", ");
954                         shader_dump_param(iface, param, addr_token, 1);
955                     }
956                 }
957                 TRACE("\n");
958             }
959         }
960         This->baseShader.functionLength = (len + 1) * sizeof(DWORD);
961     } else {
962         This->baseShader.functionLength = 1; /* no Function defined use fixed function vertex processing */
963     }
964 }
965
966 void shader_delete_constant_list(
967     struct list* clist) {
968
969     struct list *ptr;
970     struct local_constant* constant;
971
972     ptr = list_head(clist);
973     while (ptr) {
974         constant = LIST_ENTRY(ptr, struct local_constant, entry);
975         ptr = list_next(clist, ptr);
976         HeapFree(GetProcessHeap(), 0, constant);
977     }
978 }
979
980 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
981 static void shader_none_select_depth_blt(IWineD3DDevice *iface) {}
982 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
983 static void shader_none_cleanup(IWineD3DDevice *iface) {}
984
985 const shader_backend_t none_shader_backend = {
986     &shader_none_select,
987     &shader_none_select_depth_blt,
988     &shader_none_load_constants,
989     &shader_none_cleanup
990 };