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