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