msvfw32: Add Swedish translation.
[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 static inline 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     unsigned int cur_loop_depth = 0, max_loop_depth = 0;
197
198     /* There are some minor differences between pixel and vertex shaders */
199     char pshader = shader_is_pshader_version(This->baseShader.hex_version);
200
201     reg_maps->bumpmat = -1;
202     reg_maps->luminanceparams = -1;
203
204     if (pToken == NULL)
205         return WINED3D_OK;
206
207     while (WINED3DVS_END() != *pToken) {
208         CONST SHADER_OPCODE* curOpcode;
209         DWORD opcode_token;
210
211         /* Skip version */
212         if (shader_is_version_token(*pToken)) {
213              ++pToken;
214              continue;
215
216         /* Skip comments */
217         } else if (shader_is_comment(*pToken)) {
218              DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
219              ++pToken;
220              pToken += comment_len;
221              continue;
222         }
223
224         /* Fetch opcode */
225         opcode_token = *pToken++;
226         curOpcode = shader_get_opcode(iface, opcode_token);
227
228         /* Unhandled opcode, and its parameters */
229         if (NULL == curOpcode) {
230            while (*pToken & 0x80000000)
231                ++pToken;
232
233         /* Handle declarations */
234         } else if (WINED3DSIO_DCL == curOpcode->opcode) {
235
236             DWORD usage = *pToken++;
237             DWORD param = *pToken++;
238             DWORD regtype = shader_get_regtype(param);
239             unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
240
241             /* Vshader: mark attributes used
242                Pshader: mark 3.0 input registers used, save token */
243             if (WINED3DSPR_INPUT == regtype) {
244
245                 if (!pshader)
246                     reg_maps->attributes[regnum] = 1;
247                 else
248                     reg_maps->packed_input[regnum] = 1;
249
250                 semantics_in[regnum].usage = usage;
251                 semantics_in[regnum].reg = param;
252
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))
259                     reg_maps->fog = 1;
260
261             /* Save sampler usage token */
262             } else if (WINED3DSPR_SAMPLER == regtype)
263                 reg_maps->samplers[regnum] = usage;
264
265         } else if (WINED3DSIO_DEF == curOpcode->opcode) {
266
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));
271
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;
283             }
284
285             list_add_head(&This->baseShader.constantsF, &lconst->entry);
286             pToken += curOpcode->num_params;
287
288         } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
289
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;
296
297         } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
298
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;
305
306         /* If there's a loop in the shader */
307         } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
308                    WINED3DSIO_REP == curOpcode->opcode) {
309             cur_loop_depth++;
310             if(cur_loop_depth > max_loop_depth)
311                 max_loop_depth = cur_loop_depth;
312             pToken += curOpcode->num_params;
313
314         } else if (WINED3DSIO_ENDLOOP == curOpcode->opcode ||
315                    WINED3DSIO_ENDREP == curOpcode->opcode) {
316             cur_loop_depth--;
317
318         /* For subroutine prototypes */
319         } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
320
321             DWORD snum = *pToken & WINED3DSP_REGNUM_MASK; 
322             reg_maps->labels[snum] = 1;
323             pToken += curOpcode->num_params;
324
325         /* Set texture, address, temporary registers */
326         } else {
327             int i, limit;
328
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)) {
342
343                 /* Fake sampler usage, only set reserved bit and ttype */
344                 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
345
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;
349                 } else {
350                     int texType = IWineD3DBaseTexture_GetTextureDimensions(stateBlock->textures[sampler_code]);
351                     switch(texType) {
352                         case GL_TEXTURE_2D:
353                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
354                             break;
355
356                         case GL_TEXTURE_3D:
357                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_VOLUME;
358                             break;
359
360                         case GL_TEXTURE_CUBE_MAP_ARB:
361                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_CUBE;
362                             break;
363
364                         default:
365                             ERR("Unexpected gl texture type found: %d\n", texType);
366                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
367                     }
368                 }
369
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");
375                     } else {
376                         reg_maps->bumpmat = sampler_code;
377                         if(WINED3DSIO_TEXBEML == curOpcode->opcode) {
378                             reg_maps->luminanceparams = sampler_code;
379                         }
380                     }
381                 }
382             }
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");
389                 } else {
390                     reg_maps->bumpmat = regnum;
391                 }
392             }
393
394             /* This will loop over all the registers and try to
395              * make a bitmask of the ones we're interested in. 
396              *
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) */
400
401             limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
402                 curOpcode->num_params + 1: curOpcode->num_params;
403
404             for (i = 0; i < limit; ++i) {
405
406                 DWORD param, addr_token, reg, regtype;
407                 pToken += shader_get_param(iface, pToken, &param, &addr_token);
408
409                 regtype = shader_get_regtype(param);
410                 reg = param & WINED3DSP_REGNUM_MASK;
411
412                 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
413
414                     if (pshader)
415                         reg_maps->texcoord[reg] = 1;
416                     else
417                         reg_maps->address[reg] = 1;
418                 }
419
420                 else if (WINED3DSPR_TEMP == regtype)
421                     reg_maps->temporary[reg] = 1;
422
423                 else if (WINED3DSPR_INPUT == regtype && !pshader)
424                     reg_maps->attributes[reg] = 1;
425
426                 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
427                     reg_maps->fog = 1;
428
429                 else if (WINED3DSPR_MISCTYPE == regtype && reg == 0 && pshader)
430                     reg_maps->vpos = 1;
431             }
432         }
433     }
434     reg_maps->loop_depth = max_loop_depth;
435
436     return WINED3D_OK;
437 }
438
439 static void shader_dump_decl_usage(
440     IWineD3DBaseShaderImpl* This,
441     DWORD decl, 
442     DWORD param) {
443
444     DWORD regtype = shader_get_regtype(param);
445
446     TRACE("dcl");
447
448     if (regtype == WINED3DSPR_SAMPLER) {
449         DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
450
451         switch (ttype) {
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); 
456        }
457
458     } else { 
459
460         DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
461         DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
462
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))
466             return;
467         else
468             TRACE("_");
469
470         switch(usage) {
471         case WINED3DDECLUSAGE_POSITION:
472             TRACE("position%d", idx);
473             break;
474         case WINED3DDECLUSAGE_BLENDINDICES:
475             TRACE("blend");
476             break;
477         case WINED3DDECLUSAGE_BLENDWEIGHT:
478             TRACE("weight");
479             break;
480         case WINED3DDECLUSAGE_NORMAL:
481             TRACE("normal%d", idx);
482             break;
483         case WINED3DDECLUSAGE_PSIZE:
484             TRACE("psize");
485             break;
486         case WINED3DDECLUSAGE_COLOR:
487             if(idx == 0)  {
488                 TRACE("color");
489             } else {
490                 TRACE("specular%d", (idx - 1));
491             }
492             break;
493         case WINED3DDECLUSAGE_TEXCOORD:
494             TRACE("texture%d", idx);
495             break;
496         case WINED3DDECLUSAGE_TANGENT:
497             TRACE("tangent");
498             break;
499         case WINED3DDECLUSAGE_BINORMAL:
500             TRACE("binormal");
501             break;
502         case WINED3DDECLUSAGE_TESSFACTOR:
503             TRACE("tessfactor");
504             break;
505         case WINED3DDECLUSAGE_POSITIONT:
506             TRACE("positionT%d", idx);
507             break;
508         case WINED3DDECLUSAGE_FOG:
509             TRACE("fog");
510             break;
511         case WINED3DDECLUSAGE_DEPTH:
512             TRACE("depth");
513             break;
514         case WINED3DDECLUSAGE_SAMPLE:
515             TRACE("sample");
516             break;
517         default:
518             FIXME("unknown_semantics(%08x)", usage);
519         }
520     }
521 }
522
523 static void shader_dump_arr_entry(
524     IWineD3DBaseShader *iface,
525     const DWORD param,
526     const DWORD addr_token,
527     unsigned int reg,
528     int input) {
529
530     char relative =
531         ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
532
533     if (relative) {
534         TRACE("[");
535         if (addr_token)
536             shader_dump_param(iface, addr_token, 0, input);
537         else
538             TRACE("a0.x");
539         TRACE(" + ");
540      }
541      TRACE("%u", reg);
542      if (relative)
543          TRACE("]");
544 }
545
546 void shader_dump_param(
547     IWineD3DBaseShader *iface,
548     const DWORD param, 
549     const DWORD addr_token,
550     int input) {
551
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];
556
557     DWORD reg = param & WINED3DSP_REGNUM_MASK;
558     DWORD regtype = shader_get_regtype(param);
559     DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
560
561     /* There are some minor differences between pixel and vertex shaders */
562     char pshader = shader_is_pshader_version(This->baseShader.hex_version);
563
564     /* For one, we'd prefer color components to be shown for pshaders.
565      * FIXME: use the swizzle function for this */
566
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';
571
572     if (input) {
573         if ( (modifier == WINED3DSPSM_NEG) ||
574              (modifier == WINED3DSPSM_BIASNEG) ||
575              (modifier == WINED3DSPSM_SIGNNEG) ||
576              (modifier == WINED3DSPSM_X2NEG) ||
577              (modifier == WINED3DSPSM_ABSNEG) )
578             TRACE("-");
579         else if (modifier == WINED3DSPSM_COMP)
580             TRACE("1-");
581         else if (modifier == WINED3DSPSM_NOT)
582             TRACE("!");
583
584         if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG) 
585             TRACE("abs(");
586     }
587
588     switch (regtype) {
589         case WINED3DSPR_TEMP:
590             TRACE("r%u", reg);
591             break;
592         case WINED3DSPR_INPUT:
593             TRACE("v");
594             shader_dump_arr_entry(iface, param, addr_token, reg, input);
595             break;
596         case WINED3DSPR_CONST:
597         case WINED3DSPR_CONST2:
598         case WINED3DSPR_CONST3:
599         case WINED3DSPR_CONST4:
600             TRACE("c");
601             shader_dump_arr_entry(iface, param, addr_token, shader_get_float_offset(param), input);
602             break;
603         case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
604             TRACE("%c%u", (pshader? 't':'a'), reg);
605             break;        
606         case WINED3DSPR_RASTOUT:
607             TRACE("%s", rastout_reg_names[reg]);
608             break;
609         case WINED3DSPR_COLOROUT:
610             TRACE("oC%u", reg);
611             break;
612         case WINED3DSPR_DEPTHOUT:
613             TRACE("oDepth");
614             break;
615         case WINED3DSPR_ATTROUT:
616             TRACE("oD%u", reg);
617             break;
618         case WINED3DSPR_TEXCRDOUT: 
619
620             /* Vertex shaders >= 3.0 use general purpose output registers
621              * (WINED3DSPR_OUTPUT), which can include an address token */
622
623             if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
624                 TRACE("o");
625                 shader_dump_arr_entry(iface, param, addr_token, reg, input);
626             }
627             else 
628                TRACE("oT%u", reg);
629             break;
630         case WINED3DSPR_CONSTINT:
631             TRACE("i");
632             shader_dump_arr_entry(iface, param, addr_token, reg, input);
633             break;
634         case WINED3DSPR_CONSTBOOL:
635             TRACE("b");
636             shader_dump_arr_entry(iface, param, addr_token, reg, input);
637             break;
638         case WINED3DSPR_LABEL:
639             TRACE("l%u", reg);
640             break;
641         case WINED3DSPR_LOOP:
642             TRACE("aL");
643             break;
644         case WINED3DSPR_SAMPLER:
645             TRACE("s%u", reg);
646             break;
647         case WINED3DSPR_MISCTYPE:
648             if (reg > 1) {
649                 FIXME("Unhandled misctype register %d\n", reg);
650             } else {
651                 TRACE("%s", misctype_reg_names[reg]);
652             }
653             break;
654         case WINED3DSPR_PREDICATE:
655             TRACE("p%u", reg);
656             break;
657         default:
658             TRACE("unhandled_rtype(%#x)", regtype);
659             break;
660    }
661
662    if (!input) {
663        /* operand output (for modifiers and shift, see dump_ins_modifiers) */
664
665        if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
666            TRACE(".");
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]);
671        }
672
673    } else {
674         /** operand input */
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;
680
681         if (0 != modifier) {
682             switch (modifier) {
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;
697                 default:
698                     TRACE("_unknown_modifier(%#x)", modifier >> WINED3DSP_SRCMOD_SHIFT);
699             }
700         }
701
702         /**
703         * swizzle bits fields:
704         *  RRGGBBAA
705         */
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]);
711             } else {
712                 TRACE(".%c%c%c%c",
713                 swizzle_reg_chars[swizzle_r],
714                 swizzle_reg_chars[swizzle_g],
715                 swizzle_reg_chars[swizzle_b],
716                 swizzle_reg_chars[swizzle_a]);
717             }
718         }
719     }
720 }
721
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) {
732
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;
738     DWORD i;
739     SHADER_OPCODE_ARG hw_arg;
740
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;
746
747     /* Second pass, process opcodes */
748     if (NULL != pToken) {
749         while (WINED3DPS_END() != *pToken) {
750
751             /* Skip version token */
752             if (shader_is_version_token(*pToken)) {
753                 ++pToken;
754                 continue;
755             }
756
757             /* Skip comment tokens */
758             if (shader_is_comment(*pToken)) {
759                 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
760                 ++pToken;
761                 TRACE("#%s\n", (const char*)pToken);
762                 pToken += comment_len;
763                 continue;
764             }
765
766             /* Read opcode */
767             hw_arg.opcode_token = *pToken++;
768             curOpcode = shader_get_opcode(iface, hw_arg.opcode_token);
769
770             /* Select handler */
771             if (curOpcode == NULL)
772                 hw_fct = 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;
777
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); 
782
783             /* Nothing to do */
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) {
791
792                 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
793
794             /* If a generator function is set for current shader target, use it */
795             } else if (hw_fct != NULL) {
796
797                 hw_arg.opcode = curOpcode;
798
799                 /* Destination token */
800                 if (curOpcode->dst_token) {
801
802                     DWORD param, addr_token = 0;
803                     pToken += shader_get_param(iface, pToken, &param, &addr_token);
804                     hw_arg.dst = param;
805                     hw_arg.dst_addr = addr_token;
806                 }
807
808                 /* Predication token */
809                 if (hw_arg.opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) 
810                     hw_arg.predicate = *pToken++;
811
812                 /* Other source tokens */
813                 for (i = 0; i < (curOpcode->num_params - curOpcode->dst_token); i++) {
814
815                     DWORD param, addr_token = 0; 
816                     pToken += shader_get_param(iface, pToken, &param, &addr_token);
817                     hw_arg.src[i] = param;
818                     hw_arg.src_addr[i] = addr_token;
819                 }
820
821                 /* Call appropriate function for output target */
822                 hw_fct(&hw_arg);
823
824                 /* Add color correction if needed */
825                 device->shader_backend->shader_color_correction(&hw_arg);
826
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);
830
831             /* Unhandled opcode */
832             } else {
833
834                 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
835                 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
836             }
837         }
838         /* TODO: What about result.depth? */
839
840     }
841 }
842
843 void shader_dump_ins_modifiers(const DWORD output) {
844
845     DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
846     DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
847
848     switch (shift) {
849         case 0: break;
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;
857     }
858
859     if (mmask & WINED3DSPDM_SATURATE)         TRACE("_sat");
860     if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
861     if (mmask & WINED3DSPDM_MSAMPCENTROID)    TRACE("_centroid");
862
863     mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
864     if (mmask)
865         FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
866 }
867
868 /* First pass: trace shader, initialize length and version */
869 void shader_trace_init(
870     IWineD3DBaseShader *iface,
871     const DWORD* pFunction) {
872
873     IWineD3DBaseShaderImpl *This =(IWineD3DBaseShaderImpl *)iface;
874
875     const DWORD* pToken = pFunction;
876     const SHADER_OPCODE* curOpcode = NULL;
877     DWORD opcode_token;
878     unsigned int len = 0;
879     DWORD i;
880
881     TRACE("(%p) : Parsing programme\n", This);
882
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));
890                 ++pToken;
891                 ++len;
892                 continue;
893             }
894             if (shader_is_comment(*pToken)) { /** comment */
895                 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
896                 ++pToken;
897                 TRACE("//%s\n", (const char*)pToken);
898                 pToken += comment_len;
899                 len += comment_len + 1;
900                 continue;
901             }
902             opcode_token = *pToken++;
903             curOpcode = shader_get_opcode(iface, opcode_token);
904             len++;
905
906             if (NULL == curOpcode) {
907                 int tokens_read;
908                 FIXME("Unrecognized opcode: token=%08x\n", opcode_token);
909                 tokens_read = shader_skip_unrecognized(iface, pToken);
910                 pToken += tokens_read;
911                 len += tokens_read;
912
913             } else {
914                 if (curOpcode->opcode == WINED3DSIO_DCL) {
915
916                     DWORD usage = *pToken;
917                     DWORD param = *(pToken + 1);
918
919                     shader_dump_decl_usage(This, usage, param);
920                     shader_dump_ins_modifiers(param);
921                     TRACE(" ");
922                     shader_dump_param(iface, param, 0, 0);
923                     pToken += 2;
924                     len += 2;
925
926                 } else if (curOpcode->opcode == WINED3DSIO_DEF) {
927
928                         unsigned int offset = shader_get_float_offset(*pToken);
929
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));
935
936                         pToken += 5;
937                         len += 5;
938                 } else if (curOpcode->opcode == WINED3DSIO_DEFI) {
939
940                         TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
941                             *(pToken + 1),
942                             *(pToken + 2),
943                             *(pToken + 3),
944                             *(pToken + 4));
945
946                         pToken += 5;
947                         len += 5;
948
949                 } else if (curOpcode->opcode == WINED3DSIO_DEFB) {
950
951                         TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
952                             *(pToken + 1)? "true": "false");
953
954                         pToken += 2;
955                         len += 2;
956
957                 } else {
958
959                     DWORD param, addr_token;
960                     int tokens_read;
961
962                     /* Print out predication source token first - it follows
963                      * the destination token. */
964                     if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
965                         TRACE("(");
966                         shader_dump_param(iface, *(pToken + 2), 0, 1);
967                         TRACE(") ");
968                     }
969                     if (opcode_token & WINED3DSI_COISSUE) {
970                         /* PixWin marks instructions with the coissue flag with a '+' */
971                         TRACE("+");
972                     }
973
974                     TRACE("%s", curOpcode->name);
975
976                     if (curOpcode->opcode == WINED3DSIO_IFC ||
977                         curOpcode->opcode == WINED3DSIO_BREAKC) {
978
979                         DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
980                         switch (op) {
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;
987                             default:
988                                 TRACE("_(%u)", op);
989                         }
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");
993                     }
994
995                     /* Destination token */
996                     if (curOpcode->dst_token) {
997
998                         /* Destination token */
999                         tokens_read = shader_get_param(iface, pToken, &param, &addr_token);
1000                         pToken += tokens_read;
1001                         len += tokens_read;
1002
1003                         shader_dump_ins_modifiers(param);
1004                         TRACE(" ");
1005                         shader_dump_param(iface, param, addr_token, 0);
1006                     }
1007
1008                     /* Predication token - already printed out, just skip it */
1009                     if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
1010                         pToken++;
1011                         len++;
1012                     }
1013
1014                     /* Other source tokens */
1015                     for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i) {
1016
1017                         tokens_read = shader_get_param(iface, pToken, &param, &addr_token);
1018                         pToken += tokens_read;
1019                         len += tokens_read;
1020
1021                         TRACE((i == 0)? " " : ", ");
1022                         shader_dump_param(iface, param, addr_token, 1);
1023                     }
1024                 }
1025                 TRACE("\n");
1026             }
1027         }
1028         This->baseShader.functionLength = (len + 1) * sizeof(DWORD);
1029     } else {
1030         This->baseShader.functionLength = 1; /* no Function defined use fixed function vertex processing */
1031     }
1032 }
1033
1034 void shader_delete_constant_list(
1035     struct list* clist) {
1036
1037     struct list *ptr;
1038     struct local_constant* constant;
1039
1040     ptr = list_head(clist);
1041     while (ptr) {
1042         constant = LIST_ENTRY(ptr, struct local_constant, entry);
1043         ptr = list_next(clist, ptr);
1044         HeapFree(GetProcessHeap(), 0, constant);
1045     }
1046 }
1047
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) {}
1052
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
1058 };