wined3d: Move clipping states to the state table.
[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     buffer->bsize += rc;
61     buffer->lineNo++;
62     TRACE("GL HW (%u, %u) : %s", buffer->lineNo, buffer->bsize, base); 
63     return 0;
64 }
65
66 const SHADER_OPCODE* shader_get_opcode(
67     IWineD3DBaseShader *iface, const DWORD code) {
68
69     IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl*) iface;
70
71     DWORD i = 0;
72     DWORD hex_version = This->baseShader.hex_version;
73     const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;
74
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];
81         }
82         ++i;
83     }
84     FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n", 
85        code, code, code & WINED3DSI_OPCODE_MASK, hex_version);
86     return NULL;
87 }
88
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) {
97
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 */
102
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);
106
107     *param = *pToken;
108     *addr_token = rel_token? *(pToken + 1): 0;
109     return rel_token? 2:1;
110 }
111
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) {
117
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 */
120
121     return (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2)?
122         ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT):
123         curOpcode->num_params;
124 }
125
126 /* Read the parameters of an unrecognized opcode from the input stream
127  * Return the number of tokens read. 
128  * 
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 */
132
133 int shader_skip_unrecognized(
134     IWineD3DBaseShader* iface,
135     const DWORD* pToken) {
136
137     int tokens_read = 0;
138     int i = 0;
139
140     /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
141     while (*pToken & 0x80000000) {
142
143         DWORD param, addr_token;
144         tokens_read += shader_get_param(iface, pToken, &param, &addr_token);
145         pToken += tokens_read;
146
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;
152     }
153     return tokens_read;
154 }
155
156 /* Convert floating point offset relative
157  * to a register file to an absolute offset for float constants */
158
159 unsigned int shader_get_float_offset(const DWORD reg) {
160
161      unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
162      int regtype = shader_get_regtype(reg);
163
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;
172      }
173 }
174
175 /* Note that this does not count the loop register
176  * as an address register. */
177
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) {
185
186     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
187
188     /* There are some minor differences between pixel and vertex shaders */
189     char pshader = shader_is_pshader_version(This->baseShader.hex_version);
190
191     if (pToken == NULL)
192         return WINED3D_OK;
193
194     while (WINED3DVS_END() != *pToken) {
195         CONST SHADER_OPCODE* curOpcode;
196         DWORD opcode_token;
197
198         /* Skip version */
199         if (shader_is_version_token(*pToken)) {
200              ++pToken;
201              continue;
202
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;
209         }
210
211         /* Fetch opcode */
212         opcode_token = *pToken++;
213         curOpcode = shader_get_opcode(iface, opcode_token);
214
215         /* Unhandled opcode, and its parameters */
216         if (NULL == curOpcode) {
217            while (*pToken & 0x80000000)
218                ++pToken;
219
220         /* Handle declarations */
221         } else if (WINED3DSIO_DCL == curOpcode->opcode) {
222
223             DWORD usage = *pToken++;
224             DWORD param = *pToken++;
225             DWORD regtype = shader_get_regtype(param);
226             unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
227
228             /* Vshader: mark attributes used
229                Pshader: mark 3.0 input registers used, save token */
230             if (WINED3DSPR_INPUT == regtype) {
231
232                 if (!pshader)
233                     reg_maps->attributes[regnum] = 1;
234                 else
235                     reg_maps->packed_input[regnum] = 1;
236
237                 semantics_in[regnum].usage = usage;
238                 semantics_in[regnum].reg = param;
239
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;
245
246             /* Save sampler usage token */
247             } else if (WINED3DSPR_SAMPLER == regtype)
248                 reg_maps->samplers[regnum] = usage;
249
250         } else if (WINED3DSIO_DEF == curOpcode->opcode) {
251
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;
258
259         } else if (WINED3DSIO_DEFI == 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.constantsI, &lconst->entry);
266             pToken += curOpcode->num_params;
267
268         } else if (WINED3DSIO_DEFB == 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, 1 * sizeof(DWORD));
274             list_add_head(&This->baseShader.constantsB, &lconst->entry);
275             pToken += curOpcode->num_params;
276
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;
282    
283         /* For subroutine prototypes */
284         } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
285
286             DWORD snum = *pToken & WINED3DSP_REGNUM_MASK; 
287             reg_maps->labels[snum] = 1;
288             pToken += curOpcode->num_params;
289  
290         /* Set texture, address, temporary registers */
291         } else {
292             int i, limit;
293
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)) {
300
301                 /* Fake sampler usage, only set reserved bit and ttype */
302                 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
303
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;
313
314                         case GL_TEXTURE_2D:
315                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
316                             break;
317
318                         case GL_TEXTURE_3D:
319                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_VOLUME;
320                             break;
321
322                         case GL_TEXTURE_CUBE_MAP_ARB:
323                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_CUBE;
324                             break;
325
326                         default:
327                             ERR("Unexpected gl texture type found: %d\n", texType);
328                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
329                     }
330                 }
331
332             } else if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
333                 (WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode ||
334                  WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode)) {
335
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)) {
344                 
345                 /* 1D Sampler usage */
346                 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
347                 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_1D;
348             }
349
350             /* This will loop over all the registers and try to
351              * make a bitmask of the ones we're interested in. 
352              *
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) */
356
357             limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
358                 curOpcode->num_params + 1: curOpcode->num_params;
359
360             for (i = 0; i < limit; ++i) {
361
362                 DWORD param, addr_token, reg, regtype;
363                 pToken += shader_get_param(iface, pToken, &param, &addr_token);
364
365                 regtype = shader_get_regtype(param);
366                 reg = param & WINED3DSP_REGNUM_MASK;
367
368                 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
369
370                     if (pshader)
371                         reg_maps->texcoord[reg] = 1;
372                     else
373                         reg_maps->address[reg] = 1;
374                 }
375
376                 else if (WINED3DSPR_TEMP == regtype)
377                     reg_maps->temporary[reg] = 1;
378
379                 else if (WINED3DSPR_INPUT == regtype && !pshader)
380                     reg_maps->attributes[reg] = 1;
381
382                 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
383                     reg_maps->fog = 1;
384              }
385         }
386     }
387
388     return WINED3D_OK;
389 }
390
391 static void shader_dump_decl_usage(
392     IWineD3DBaseShaderImpl* This,
393     DWORD decl, 
394     DWORD param) {
395
396     DWORD regtype = shader_get_regtype(param);
397
398     TRACE("dcl");
399
400     if (regtype == WINED3DSPR_SAMPLER) {
401         DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
402
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); 
408        }
409
410     } else { 
411
412         DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
413         DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
414
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("_");
421
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));
443             }
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);
471         }
472     }
473 }
474
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) {
481
482     char relative =
483         ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
484
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(" + ");
492      }
493      TRACE("%u", reg);
494      if (relative)
495          TRACE("]");
496 }
497
498 void shader_dump_param(
499     IWineD3DBaseShader *iface,
500     const DWORD param, 
501     const DWORD addr_token,
502     int input) {
503
504     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
505     static const char* rastout_reg_names[] = { "oPos", "oFog", "oPts" };
506     char swizzle_reg_chars[4];
507
508     DWORD reg = param & WINED3DSP_REGNUM_MASK;
509     DWORD regtype = shader_get_regtype(param);
510     DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
511
512     /* There are some minor differences between pixel and vertex shaders */
513     char pshader = shader_is_pshader_version(This->baseShader.hex_version);
514
515     /* For one, we'd prefer color components to be shown for pshaders.
516      * FIXME: use the swizzle function for this */
517
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';
522
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("!");
534
535         if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG) 
536             TRACE("abs(");
537     }
538
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: 
570
571             /* Vertex shaders >= 3.0 use general purpose output registers
572              * (WINED3DSPR_OUTPUT), which can include an address token */
573
574             if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
575                 TRACE("o");
576                 shader_dump_arr_entry(iface, param, addr_token, reg, input);
577             }
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;
604    }
605
606    if (!input) {
607        /* operand output (for modifiers and shift, see dump_ins_modifiers) */
608
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]);
615        }
616
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;
624
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);
643             }
644         }
645
646         /**
647         * swizzle bits fields:
648         *  RRGGBBAA
649         */
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]);
661             }
662         }
663     }
664 }
665
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) {
676
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;
683
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;
689
690     /* Second pass, process opcodes */
691     if (NULL != pToken) {
692         while (WINED3DPS_END() != *pToken) {
693
694             /* Skip version token */
695             if (shader_is_version_token(*pToken)) {
696                 ++pToken;
697                 continue;
698             }
699
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", (const char*)pToken);
705                 pToken += comment_len;
706                 continue;
707             }
708
709             /* Read opcode */
710             hw_arg.opcode_token = *pToken++;
711             curOpcode = shader_get_opcode(iface, hw_arg.opcode_token);
712
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;
720
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); 
725
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) {
734
735                 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
736
737             /* If a generator function is set for current shader target, use it */
738             } else if (hw_fct != NULL) {
739
740                 hw_arg.opcode = curOpcode;
741
742                 /* Destination token */
743                 if (curOpcode->dst_token) {
744
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;
749                 }
750
751                 /* Predication token */
752                 if (hw_arg.opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) 
753                     hw_arg.predicate = *pToken++;
754
755                 /* Other source tokens */
756                 for (i = 0; i < (curOpcode->num_params - curOpcode->dst_token); i++) {
757
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;
762                 }
763
764                 /* Call appropriate function for output target */
765                 hw_fct(&hw_arg);
766
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);
770
771             /* Unhandled opcode */
772             } else {
773
774                 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
775                 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
776             }
777         }
778         /* TODO: What about result.depth? */
779
780     }
781 }
782
783 void shader_dump_ins_modifiers(const DWORD output) {
784
785     DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
786     DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
787
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;
797     }
798
799     if (mmask & WINED3DSPDM_SATURATE)         TRACE("_sat");
800     if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
801     if (mmask & WINED3DSPDM_MSAMPCENTROID)    TRACE("_centroid");
802
803     mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
804     if (mmask)
805         FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
806 }
807
808 /* First pass: trace shader, initialize length and version */
809 void shader_trace_init(
810     IWineD3DBaseShader *iface,
811     const DWORD* pFunction) {
812
813     IWineD3DBaseShaderImpl *This =(IWineD3DBaseShaderImpl *)iface;
814
815     const DWORD* pToken = pFunction;
816     const SHADER_OPCODE* curOpcode = NULL;
817     DWORD opcode_token;
818     unsigned int len = 0;
819     DWORD i;
820
821     TRACE("(%p) : Parsing programme\n", This);
822
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;
833             }
834             if (shader_is_comment(*pToken)) { /** comment */
835                 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
836                 ++pToken;
837                 TRACE("//%s\n", (const char*)pToken);
838                 pToken += comment_len;
839                 len += comment_len + 1;
840                 continue;
841             }
842             opcode_token = *pToken++;
843             curOpcode = shader_get_opcode(iface, opcode_token);
844             len++;
845
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;
852
853             } else {
854                 if (curOpcode->opcode == WINED3DSIO_DCL) {
855
856                     DWORD usage = *pToken;
857                     DWORD param = *(pToken + 1);
858
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;
865
866                 } else if (curOpcode->opcode == WINED3DSIO_DEF) {
867
868                         unsigned int offset = shader_get_float_offset(*pToken);
869
870                         TRACE("def c%u = %f, %f, %f, %f", offset,
871                             *(const float *)(pToken + 1),
872                             *(const float *)(pToken + 2),
873                             *(const float *)(pToken + 3),
874                             *(const float *)(pToken + 4));
875
876                         pToken += 5;
877                         len += 5;
878                 } else if (curOpcode->opcode == WINED3DSIO_DEFI) {
879
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));
885
886                         pToken += 5;
887                         len += 5;
888
889                 } else if (curOpcode->opcode == WINED3DSIO_DEFB) {
890
891                         TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
892                             *(pToken + 1)? "true": "false");
893
894                         pToken += 2;
895                         len += 2;
896
897                 } else {
898
899                     DWORD param, addr_token;
900                     int tokens_read;
901
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(") ");
908                     }
909
910                     TRACE("%s", curOpcode->name);
911
912                     if (curOpcode->opcode == WINED3DSIO_IFC ||
913                         curOpcode->opcode == WINED3DSIO_BREAKC) {
914
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);
925                         }
926                     }
927
928                     /* Destination token */
929                     if (curOpcode->dst_token) {
930
931                         /* Destination token */
932                         tokens_read = shader_get_param(iface, pToken, &param, &addr_token);
933                         pToken += tokens_read;
934                         len += tokens_read;
935
936                         shader_dump_ins_modifiers(param);
937                         TRACE(" ");
938                         shader_dump_param(iface, param, addr_token, 0);
939                     }
940
941                     /* Predication token - already printed out, just skip it */
942                     if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
943                         pToken++;
944                         len++;
945                     }
946
947                     /* Other source tokens */
948                     for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i) {
949
950                         tokens_read = shader_get_param(iface, pToken, &param, &addr_token);
951                         pToken += tokens_read;
952                         len += tokens_read;
953
954                         TRACE((i == 0)? " " : ", ");
955                         shader_dump_param(iface, param, addr_token, 1);
956                     }
957                 }
958                 TRACE("\n");
959             }
960         }
961         This->baseShader.functionLength = (len + 1) * sizeof(DWORD);
962     } else {
963         This->baseShader.functionLength = 1; /* no Function defined use fixed function vertex processing */
964     }
965 }
966
967 void shader_delete_constant_list(
968     struct list* clist) {
969
970     struct list *ptr;
971     struct local_constant* constant;
972
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);
978     }
979 }
980
981 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
982 static void shader_none_select_depth_blt(IWineD3DDevice *iface) {}
983 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
984 static void shader_none_cleanup(BOOL usePS, BOOL useVS) {}
985
986 const shader_backend_t none_shader_backend = {
987     &shader_none_select,
988     &shader_none_select_depth_blt,
989     &shader_none_load_constants,
990     &shader_none_cleanup
991 };