wined3d: Explicitly pass the shader version to shader_dump_decl_usage().
[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  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25
26 #include "config.h"
27 #include <string.h>
28 #include <stdio.h>
29 #include "wined3d_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
32 WINE_DECLARE_DEBUG_CHANNEL(d3d);
33
34 #define GLNAME_REQUIRE_GLSL  ((const char *)1)
35
36 static void shader_dump_param(IWineD3DBaseShader *iface, const DWORD param, const DWORD addr_token, int input);
37
38 static inline BOOL shader_is_version_token(DWORD token) {
39     return shader_is_pshader_version(token) ||
40            shader_is_vshader_version(token);
41 }
42
43 int shader_addline(
44     SHADER_BUFFER* buffer,  
45     const char *format, ...) {
46
47     char* base = buffer->buffer + buffer->bsize;
48     int rc;
49
50     va_list args;
51     va_start(args, format);
52     rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
53     va_end(args);
54
55     if (rc < 0 ||                                   /* C89 */ 
56         rc > SHADER_PGMSIZE - 1 - buffer->bsize) {  /* C99 */
57
58         ERR("The buffer allocated for the shader program string "
59             "is too small at %d bytes.\n", SHADER_PGMSIZE);
60         buffer->bsize = SHADER_PGMSIZE - 1;
61         return -1;
62     }
63
64     if (buffer->newline) {
65         TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
66         buffer->newline = FALSE;
67     } else {
68         TRACE("%s", base);
69     }
70
71     buffer->bsize += rc;
72     if (buffer->buffer[buffer->bsize-1] == '\n') {
73         buffer->lineNo++;
74         buffer->newline = TRUE;
75     }
76     return 0;
77 }
78
79 const SHADER_OPCODE *shader_get_opcode(const SHADER_OPCODE *opcode_table, DWORD shader_version, DWORD code)
80 {
81     DWORD i = 0;
82
83     /** TODO: use dichotomic search */
84     while (opcode_table[i].name)
85     {
86         if ((code & WINED3DSI_OPCODE_MASK) == opcode_table[i].opcode
87                 && shader_version >= opcode_table[i].min_version
88                 && (!opcode_table[i].max_version || shader_version <= opcode_table[i].max_version))
89         {
90             return &opcode_table[i];
91         }
92         ++i;
93     }
94
95     FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n",
96             code, code, code & WINED3DSI_OPCODE_MASK, shader_version);
97
98     return NULL;
99 }
100
101 /* Read a parameter opcode from the input stream,
102  * and possibly a relative addressing token.
103  * Return the number of tokens read */
104 static int shader_get_param(IWineD3DBaseShader *iface, const DWORD *pToken, DWORD *param, DWORD *addr_token)
105 {
106     /* PS >= 3.0 have relative addressing (with token)
107      * VS >= 2.0 have relative addressing (with token)
108      * VS >= 1.0 < 2.0 have relative addressing (without token)
109      * The version check below should work in general */
110
111     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
112     char rel_token = WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2 &&
113         ((*pToken & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
114
115     *param = *pToken;
116     *addr_token = rel_token? *(pToken + 1): 0;
117     return rel_token? 2:1;
118 }
119
120 /* Return the number of parameters to skip for an opcode */
121 static inline int shader_skip_opcode(
122     IWineD3DBaseShaderImpl* This,
123     const SHADER_OPCODE* curOpcode,
124     DWORD opcode_token) {
125
126    /* Shaders >= 2.0 may contain address tokens, but fortunately they
127     * have a useful length mask - use it here. Shaders 1.0 contain no such tokens */
128
129     return (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2)?
130         ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT):
131         curOpcode->num_params;
132 }
133
134 /* Read the parameters of an unrecognized opcode from the input stream
135  * Return the number of tokens read. 
136  * 
137  * Note: This function assumes source or destination token format.
138  * It will not work with specially-formatted tokens like DEF or DCL, 
139  * but hopefully those would be recognized */
140 static int shader_skip_unrecognized(IWineD3DBaseShader *iface, const DWORD *pToken)
141 {
142     int tokens_read = 0;
143     int i = 0;
144
145     /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
146     while (*pToken & 0x80000000) {
147
148         DWORD param, addr_token;
149         tokens_read += shader_get_param(iface, pToken, &param, &addr_token);
150         pToken += tokens_read;
151
152         FIXME("Unrecognized opcode param: token=0x%08x "
153             "addr_token=0x%08x name=", param, addr_token);
154         shader_dump_param(iface, param, addr_token, i);
155         FIXME("\n");
156         ++i;
157     }
158     return tokens_read;
159 }
160
161 /* Convert floating point offset relative
162  * to a register file to an absolute offset for float constants */
163 static unsigned int shader_get_float_offset(const DWORD reg)
164 {
165      unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
166      int regtype = shader_get_regtype(reg);
167
168      switch (regtype) {
169         case WINED3DSPR_CONST: return regnum;
170         case WINED3DSPR_CONST2: return 2048 + regnum;
171         case WINED3DSPR_CONST3: return 4096 + regnum;
172         case WINED3DSPR_CONST4: return 6144 + regnum;
173         default:
174             FIXME("Unsupported register type: %d\n", regtype);
175             return regnum;
176      }
177 }
178
179 static void shader_delete_constant_list(struct list* clist) {
180
181     struct list *ptr;
182     struct local_constant* constant;
183
184     ptr = list_head(clist);
185     while (ptr) {
186         constant = LIST_ENTRY(ptr, struct local_constant, entry);
187         ptr = list_next(clist, ptr);
188         HeapFree(GetProcessHeap(), 0, constant);
189     }
190     list_init(clist);
191 }
192
193 /* Note that this does not count the loop register
194  * as an address register. */
195
196 HRESULT shader_get_registers_used(
197     IWineD3DBaseShader *iface,
198     shader_reg_maps* reg_maps,
199     semantic* semantics_in,
200     semantic* semantics_out,
201     CONST DWORD* pToken,
202     IWineD3DStateBlockImpl *stateBlock) {
203
204     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
205     const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;
206     DWORD shader_version = This->baseShader.hex_version;
207     unsigned int cur_loop_depth = 0, max_loop_depth = 0;
208
209     /* There are some minor differences between pixel and vertex shaders */
210     char pshader = shader_is_pshader_version(This->baseShader.hex_version);
211
212     memset(reg_maps->bumpmat, 0, sizeof(reg_maps->bumpmat));
213     memset(reg_maps->luminanceparams, 0, sizeof(reg_maps->luminanceparams));
214
215     if (pToken == NULL)
216         return WINED3D_OK;
217
218     /* get_registers_used is called on every compile on some 1.x shaders, which can result
219      * in stacking up a collection of local constants. Delete the old constants if existing
220      */
221     shader_delete_constant_list(&This->baseShader.constantsF);
222     shader_delete_constant_list(&This->baseShader.constantsB);
223     shader_delete_constant_list(&This->baseShader.constantsI);
224
225     while (WINED3DVS_END() != *pToken) {
226         CONST SHADER_OPCODE* curOpcode;
227         DWORD opcode_token;
228
229         /* Skip version */
230         if (shader_is_version_token(*pToken)) {
231              ++pToken;
232              continue;
233
234         /* Skip comments */
235         } else if (shader_is_comment(*pToken)) {
236              DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
237              ++pToken;
238              pToken += comment_len;
239              continue;
240         }
241
242         /* Fetch opcode */
243         opcode_token = *pToken++;
244         curOpcode = shader_get_opcode(shader_ins, shader_version, opcode_token);
245
246         /* Unhandled opcode, and its parameters */
247         if (NULL == curOpcode) {
248            while (*pToken & 0x80000000)
249                ++pToken;
250
251         /* Handle declarations */
252         } else if (WINED3DSIO_DCL == curOpcode->opcode) {
253
254             DWORD usage = *pToken++;
255             DWORD param = *pToken++;
256             DWORD regtype = shader_get_regtype(param);
257             unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
258
259             /* Vshader: mark attributes used
260                Pshader: mark 3.0 input registers used, save token */
261             if (WINED3DSPR_INPUT == regtype) {
262
263                 if (!pshader)
264                     reg_maps->attributes[regnum] = 1;
265                 else
266                     reg_maps->packed_input[regnum] = 1;
267
268                 semantics_in[regnum].usage = usage;
269                 semantics_in[regnum].reg = param;
270
271             /* Vshader: mark 3.0 output registers used, save token */
272             } else if (WINED3DSPR_OUTPUT == regtype) {
273                 reg_maps->packed_output[regnum] = 1;
274                 semantics_out[regnum].usage = usage;
275                 semantics_out[regnum].reg = param;
276                 if (usage & (WINED3DDECLUSAGE_FOG << WINED3DSP_DCL_USAGE_SHIFT))
277                     reg_maps->fog = 1;
278
279             /* Save sampler usage token */
280             } else if (WINED3DSPR_SAMPLER == regtype)
281                 reg_maps->samplers[regnum] = usage;
282
283         } else if (WINED3DSIO_DEF == curOpcode->opcode) {
284
285             local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
286             if (!lconst) return E_OUTOFMEMORY;
287             lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
288             memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
289
290             /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
291             if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 && pshader) {
292                 float *value = (float *) lconst->value;
293                 if(value[0] < -1.0) value[0] = -1.0;
294                 else if(value[0] >  1.0) value[0] =  1.0;
295                 if(value[1] < -1.0) value[1] = -1.0;
296                 else if(value[1] >  1.0) value[1] =  1.0;
297                 if(value[2] < -1.0) value[2] = -1.0;
298                 else if(value[2] >  1.0) value[2] =  1.0;
299                 if(value[3] < -1.0) value[3] = -1.0;
300                 else if(value[3] >  1.0) value[3] =  1.0;
301             }
302
303             list_add_head(&This->baseShader.constantsF, &lconst->entry);
304             pToken += curOpcode->num_params;
305
306         } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
307
308             local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
309             if (!lconst) return E_OUTOFMEMORY;
310             lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
311             memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
312             list_add_head(&This->baseShader.constantsI, &lconst->entry);
313             pToken += curOpcode->num_params;
314
315         } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
316
317             local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
318             if (!lconst) return E_OUTOFMEMORY;
319             lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
320             memcpy(lconst->value, pToken + 1, 1 * sizeof(DWORD));
321             list_add_head(&This->baseShader.constantsB, &lconst->entry);
322             pToken += curOpcode->num_params;
323
324         /* If there's a loop in the shader */
325         } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
326                    WINED3DSIO_REP == curOpcode->opcode) {
327             cur_loop_depth++;
328             if(cur_loop_depth > max_loop_depth)
329                 max_loop_depth = cur_loop_depth;
330             pToken += curOpcode->num_params;
331
332         } else if (WINED3DSIO_ENDLOOP == curOpcode->opcode ||
333                    WINED3DSIO_ENDREP == curOpcode->opcode) {
334             cur_loop_depth--;
335
336         /* For subroutine prototypes */
337         } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
338
339             DWORD snum = *pToken & WINED3DSP_REGNUM_MASK; 
340             reg_maps->labels[snum] = 1;
341             pToken += curOpcode->num_params;
342
343         /* Set texture, address, temporary registers */
344         } else {
345             int i, limit;
346
347             /* Declare 1.X samplers implicitly, based on the destination reg. number */
348             if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
349                 pshader /* Filter different instructions with the same enum values in VS */ &&
350                 (WINED3DSIO_TEX == curOpcode->opcode ||
351                  WINED3DSIO_TEXBEM == curOpcode->opcode ||
352                  WINED3DSIO_TEXBEML == curOpcode->opcode ||
353                  WINED3DSIO_TEXDP3TEX == curOpcode->opcode ||
354                  WINED3DSIO_TEXM3x2TEX == curOpcode->opcode ||
355                  WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode ||
356                  WINED3DSIO_TEXM3x3TEX == curOpcode->opcode ||
357                  WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode ||
358                  WINED3DSIO_TEXREG2AR == curOpcode->opcode ||
359                  WINED3DSIO_TEXREG2GB == curOpcode->opcode ||
360                  WINED3DSIO_TEXREG2RGB == curOpcode->opcode)) {
361
362                 /* Fake sampler usage, only set reserved bit and ttype */
363                 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
364
365                 if(!stateBlock->textures[sampler_code]) {
366                     ERR("No texture bound to sampler %d\n", sampler_code);
367                     reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
368                 } else {
369                     int texType = IWineD3DBaseTexture_GetTextureDimensions(stateBlock->textures[sampler_code]);
370                     switch(texType) {
371                         /* We have to select between texture rectangles and 2D textures later because 2.0 and
372                          * 3.0 shaders only have WINED3DSTT_2D as well
373                          */
374                         case GL_TEXTURE_RECTANGLE_ARB:
375                         case GL_TEXTURE_2D:
376                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
377                             break;
378
379                         case GL_TEXTURE_3D:
380                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_VOLUME;
381                             break;
382
383                         case GL_TEXTURE_CUBE_MAP_ARB:
384                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_CUBE;
385                             break;
386
387                         default:
388                             ERR("Unexpected gl texture type found: %d\n", texType);
389                             reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
390                     }
391                 }
392
393                 /* texbem is only valid with < 1.4 pixel shaders */
394                 if(WINED3DSIO_TEXBEM  == curOpcode->opcode ||
395                     WINED3DSIO_TEXBEML == curOpcode->opcode) {
396                     reg_maps->bumpmat[sampler_code] = TRUE;
397                     if(WINED3DSIO_TEXBEML == curOpcode->opcode) {
398                         reg_maps->luminanceparams[sampler_code] = TRUE;
399                     }
400                 }
401             }
402             if(WINED3DSIO_NRM  == curOpcode->opcode) {
403                 reg_maps->usesnrm = 1;
404             } else if(WINED3DSIO_BEM == curOpcode->opcode && pshader) {
405                 DWORD regnum = *pToken & WINED3DSP_REGNUM_MASK;
406                 reg_maps->bumpmat[regnum] = TRUE;
407             } else if(WINED3DSIO_DSY  == curOpcode->opcode) {
408                 reg_maps->usesdsy = 1;
409             }
410
411             /* This will loop over all the registers and try to
412              * make a bitmask of the ones we're interested in. 
413              *
414              * Relative addressing tokens are ignored, but that's 
415              * okay, since we'll catch any address registers when 
416              * they are initialized (required by spec) */
417
418             limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
419                 curOpcode->num_params + 1: curOpcode->num_params;
420
421             for (i = 0; i < limit; ++i) {
422
423                 DWORD param, addr_token, reg, regtype;
424                 pToken += shader_get_param(iface, pToken, &param, &addr_token);
425
426                 regtype = shader_get_regtype(param);
427                 reg = param & WINED3DSP_REGNUM_MASK;
428
429                 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
430
431                     if (pshader)
432                         reg_maps->texcoord[reg] = 1;
433                     else
434                         reg_maps->address[reg] = 1;
435                 }
436
437                 else if (WINED3DSPR_TEMP == regtype)
438                     reg_maps->temporary[reg] = 1;
439
440                 else if (WINED3DSPR_INPUT == regtype) {
441                     if( !pshader)
442                         reg_maps->attributes[reg] = 1;
443                     else {
444                         if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
445                             /* If relative addressing is used, we must assume that all registers
446                              * are used. Even if it is a construct like v3[aL], we can't assume
447                              * that v0, v1 and v2 aren't read because aL can be negative
448                              */
449                             unsigned int i;
450                             for(i = 0; i < MAX_REG_INPUT; i++) {
451                                 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[i] = TRUE;
452                             }
453                         } else {
454                             ((IWineD3DPixelShaderImpl *) This)->input_reg_used[reg] = TRUE;
455                         }
456                     }
457                 }
458
459                 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
460                     reg_maps->fog = 1;
461
462                 else if (WINED3DSPR_MISCTYPE == regtype && reg == 0 && pshader)
463                     reg_maps->vpos = 1;
464
465                 else if(WINED3DSPR_CONST == regtype) {
466                     if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
467                         if(!pshader) {
468                             if(reg <= ((IWineD3DVertexShaderImpl *) This)->min_rel_offset) {
469                                 ((IWineD3DVertexShaderImpl *) This)->min_rel_offset = reg;
470                             } else if(reg >= ((IWineD3DVertexShaderImpl *) This)->max_rel_offset) {
471                                 ((IWineD3DVertexShaderImpl *) This)->max_rel_offset = reg;
472                             }
473                         }
474                         reg_maps->usesrelconstF = TRUE;
475                     }
476                 }
477
478                 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and is used
479                  * in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel shaders because TECRDOUT
480                  * isn't used in them, but future register types might cause issues
481                  */
482                 else if(WINED3DSPR_TEXCRDOUT == regtype && i == 0 /* Only look at writes */ &&
483                         !pshader && WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) < 3) {
484                     reg_maps->texcoord_mask[reg] |= shader_get_writemask(param);
485                 }
486             }
487         }
488     }
489     reg_maps->loop_depth = max_loop_depth;
490
491     return WINED3D_OK;
492 }
493
494 static void shader_dump_decl_usage(DWORD decl, DWORD param, DWORD shader_version)
495 {
496     DWORD regtype = shader_get_regtype(param);
497
498     TRACE("dcl");
499
500     if (regtype == WINED3DSPR_SAMPLER) {
501         DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
502
503         switch (ttype) {
504             case WINED3DSTT_2D: TRACE("_2d"); break;
505             case WINED3DSTT_CUBE: TRACE("_cube"); break;
506             case WINED3DSTT_VOLUME: TRACE("_volume"); break;
507             default: TRACE("_unknown_ttype(0x%08x)", ttype);
508        }
509
510     } else { 
511
512         DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
513         DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
514
515         /* Pixel shaders 3.0 don't have usage semantics */
516         if (shader_is_pshader_version(shader_version) && shader_version < WINED3DPS_VERSION(3,0))
517             return;
518         else
519             TRACE("_");
520
521         switch(usage) {
522         case WINED3DDECLUSAGE_POSITION:
523             TRACE("position%d", idx);
524             break;
525         case WINED3DDECLUSAGE_BLENDINDICES:
526             TRACE("blend");
527             break;
528         case WINED3DDECLUSAGE_BLENDWEIGHT:
529             TRACE("weight");
530             break;
531         case WINED3DDECLUSAGE_NORMAL:
532             TRACE("normal%d", idx);
533             break;
534         case WINED3DDECLUSAGE_PSIZE:
535             TRACE("psize");
536             break;
537         case WINED3DDECLUSAGE_COLOR:
538             if(idx == 0)  {
539                 TRACE("color");
540             } else {
541                 TRACE("specular%d", (idx - 1));
542             }
543             break;
544         case WINED3DDECLUSAGE_TEXCOORD:
545             TRACE("texture%d", idx);
546             break;
547         case WINED3DDECLUSAGE_TANGENT:
548             TRACE("tangent");
549             break;
550         case WINED3DDECLUSAGE_BINORMAL:
551             TRACE("binormal");
552             break;
553         case WINED3DDECLUSAGE_TESSFACTOR:
554             TRACE("tessfactor");
555             break;
556         case WINED3DDECLUSAGE_POSITIONT:
557             TRACE("positionT%d", idx);
558             break;
559         case WINED3DDECLUSAGE_FOG:
560             TRACE("fog");
561             break;
562         case WINED3DDECLUSAGE_DEPTH:
563             TRACE("depth");
564             break;
565         case WINED3DDECLUSAGE_SAMPLE:
566             TRACE("sample");
567             break;
568         default:
569             FIXME("unknown_semantics(0x%08x)", usage);
570         }
571     }
572 }
573
574 static void shader_dump_arr_entry(
575     IWineD3DBaseShader *iface,
576     const DWORD param,
577     const DWORD addr_token,
578     unsigned int reg,
579     int input) {
580
581     char relative =
582         ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
583
584     if (relative) {
585         TRACE("[");
586         if (addr_token)
587             shader_dump_param(iface, addr_token, 0, input);
588         else
589             TRACE("a0.x");
590         TRACE(" + ");
591      }
592      TRACE("%u", reg);
593      if (relative)
594          TRACE("]");
595 }
596
597 static void shader_dump_param(IWineD3DBaseShader *iface, const DWORD param, const DWORD addr_token, int input)
598 {
599     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
600     static const char * const rastout_reg_names[] = { "oPos", "oFog", "oPts" };
601     static const char * const misctype_reg_names[] = { "vPos", "vFace"};
602     char swizzle_reg_chars[4];
603
604     DWORD reg = param & WINED3DSP_REGNUM_MASK;
605     DWORD regtype = shader_get_regtype(param);
606     DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
607
608     /* There are some minor differences between pixel and vertex shaders */
609     char pshader = shader_is_pshader_version(This->baseShader.hex_version);
610
611     /* For one, we'd prefer color components to be shown for pshaders.
612      * FIXME: use the swizzle function for this */
613
614     swizzle_reg_chars[0] = pshader? 'r': 'x';
615     swizzle_reg_chars[1] = pshader? 'g': 'y';
616     swizzle_reg_chars[2] = pshader? 'b': 'z';
617     swizzle_reg_chars[3] = pshader? 'a': 'w';
618
619     if (input) {
620         if ( (modifier == WINED3DSPSM_NEG) ||
621              (modifier == WINED3DSPSM_BIASNEG) ||
622              (modifier == WINED3DSPSM_SIGNNEG) ||
623              (modifier == WINED3DSPSM_X2NEG) ||
624              (modifier == WINED3DSPSM_ABSNEG) )
625             TRACE("-");
626         else if (modifier == WINED3DSPSM_COMP)
627             TRACE("1-");
628         else if (modifier == WINED3DSPSM_NOT)
629             TRACE("!");
630
631         if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG) 
632             TRACE("abs(");
633     }
634
635     switch (regtype) {
636         case WINED3DSPR_TEMP:
637             TRACE("r%u", reg);
638             break;
639         case WINED3DSPR_INPUT:
640             TRACE("v");
641             shader_dump_arr_entry(iface, param, addr_token, reg, input);
642             break;
643         case WINED3DSPR_CONST:
644         case WINED3DSPR_CONST2:
645         case WINED3DSPR_CONST3:
646         case WINED3DSPR_CONST4:
647             TRACE("c");
648             shader_dump_arr_entry(iface, param, addr_token, shader_get_float_offset(param), input);
649             break;
650         case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
651             TRACE("%c%u", (pshader? 't':'a'), reg);
652             break;        
653         case WINED3DSPR_RASTOUT:
654             TRACE("%s", rastout_reg_names[reg]);
655             break;
656         case WINED3DSPR_COLOROUT:
657             TRACE("oC%u", reg);
658             break;
659         case WINED3DSPR_DEPTHOUT:
660             TRACE("oDepth");
661             break;
662         case WINED3DSPR_ATTROUT:
663             TRACE("oD%u", reg);
664             break;
665         case WINED3DSPR_TEXCRDOUT: 
666
667             /* Vertex shaders >= 3.0 use general purpose output registers
668              * (WINED3DSPR_OUTPUT), which can include an address token */
669
670             if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
671                 TRACE("o");
672                 shader_dump_arr_entry(iface, param, addr_token, reg, input);
673             }
674             else 
675                TRACE("oT%u", reg);
676             break;
677         case WINED3DSPR_CONSTINT:
678             TRACE("i");
679             shader_dump_arr_entry(iface, param, addr_token, reg, input);
680             break;
681         case WINED3DSPR_CONSTBOOL:
682             TRACE("b");
683             shader_dump_arr_entry(iface, param, addr_token, reg, input);
684             break;
685         case WINED3DSPR_LABEL:
686             TRACE("l%u", reg);
687             break;
688         case WINED3DSPR_LOOP:
689             TRACE("aL");
690             break;
691         case WINED3DSPR_SAMPLER:
692             TRACE("s%u", reg);
693             break;
694         case WINED3DSPR_MISCTYPE:
695             if (reg > 1) {
696                 FIXME("Unhandled misctype register %d\n", reg);
697             } else {
698                 TRACE("%s", misctype_reg_names[reg]);
699             }
700             break;
701         case WINED3DSPR_PREDICATE:
702             TRACE("p%u", reg);
703             break;
704         default:
705             TRACE("unhandled_rtype(%#x)", regtype);
706             break;
707    }
708
709    if (!input) {
710        /* operand output (for modifiers and shift, see dump_ins_modifiers) */
711
712        if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
713            TRACE(".");
714            if (param & WINED3DSP_WRITEMASK_0) TRACE("%c", swizzle_reg_chars[0]);
715            if (param & WINED3DSP_WRITEMASK_1) TRACE("%c", swizzle_reg_chars[1]);
716            if (param & WINED3DSP_WRITEMASK_2) TRACE("%c", swizzle_reg_chars[2]);
717            if (param & WINED3DSP_WRITEMASK_3) TRACE("%c", swizzle_reg_chars[3]);
718        }
719
720    } else {
721         /** operand input */
722         DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
723         DWORD swizzle_r = swizzle & 0x03;
724         DWORD swizzle_g = (swizzle >> 2) & 0x03;
725         DWORD swizzle_b = (swizzle >> 4) & 0x03;
726         DWORD swizzle_a = (swizzle >> 6) & 0x03;
727
728         if (0 != modifier) {
729             switch (modifier) {
730                 case WINED3DSPSM_NONE:    break;
731                 case WINED3DSPSM_NEG:     break;
732                 case WINED3DSPSM_NOT:     break;
733                 case WINED3DSPSM_BIAS:    TRACE("_bias"); break;
734                 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
735                 case WINED3DSPSM_SIGN:    TRACE("_bx2"); break;
736                 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
737                 case WINED3DSPSM_COMP:    break;
738                 case WINED3DSPSM_X2:      TRACE("_x2"); break;
739                 case WINED3DSPSM_X2NEG:   TRACE("_x2"); break;
740                 case WINED3DSPSM_DZ:      TRACE("_dz"); break;
741                 case WINED3DSPSM_DW:      TRACE("_dw"); break;
742                 case WINED3DSPSM_ABSNEG:  TRACE(")"); break;
743                 case WINED3DSPSM_ABS:     TRACE(")"); break;
744                 default:
745                     TRACE("_unknown_modifier(%#x)", modifier >> WINED3DSP_SRCMOD_SHIFT);
746             }
747         }
748
749         /**
750         * swizzle bits fields:
751         *  RRGGBBAA
752         */
753         if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) != swizzle) {
754             if (swizzle_r == swizzle_g &&
755                 swizzle_r == swizzle_b &&
756                 swizzle_r == swizzle_a) {
757                     TRACE(".%c", swizzle_reg_chars[swizzle_r]);
758             } else {
759                 TRACE(".%c%c%c%c",
760                 swizzle_reg_chars[swizzle_r],
761                 swizzle_reg_chars[swizzle_g],
762                 swizzle_reg_chars[swizzle_b],
763                 swizzle_reg_chars[swizzle_a]);
764             }
765         }
766     }
767 }
768
769 static void shader_color_correction(IWineD3DBaseShaderImpl *shader,
770         IWineD3DDeviceImpl *device, const struct SHADER_OPCODE_ARG *arg)
771 {
772     DWORD hex_version = shader->baseShader.hex_version;
773     IWineD3DBaseTextureImpl *texture;
774     struct color_fixup_desc fixup;
775     BOOL recorded = FALSE;
776     DWORD sampler_idx;
777     UINT i;
778
779     switch(arg->opcode->opcode)
780     {
781         case WINED3DSIO_TEX:
782             if (hex_version < WINED3DPS_VERSION(2,0)) sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
783             else sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
784             break;
785
786         case WINED3DSIO_TEXLDL:
787             FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
788             return;
789
790         case WINED3DSIO_TEXDP3TEX:
791         case WINED3DSIO_TEXM3x3TEX:
792         case WINED3DSIO_TEXM3x3SPEC:
793         case WINED3DSIO_TEXM3x3VSPEC:
794         case WINED3DSIO_TEXBEM:
795         case WINED3DSIO_TEXREG2AR:
796         case WINED3DSIO_TEXREG2GB:
797         case WINED3DSIO_TEXREG2RGB:
798             sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
799             break;
800
801         default:
802             /* Not a texture sampling instruction, nothing to do */
803             return;
804     };
805
806     texture = (IWineD3DBaseTextureImpl *)device->stateBlock->textures[sampler_idx];
807     if (texture) fixup = texture->baseTexture.shader_color_fixup;
808     else fixup = COLOR_FIXUP_IDENTITY;
809
810     /* before doing anything, record the sampler with the format in the format conversion list,
811      * but check if it's not there already */
812     for (i = 0; i < shader->baseShader.num_sampled_samplers; ++i)
813     {
814         if (shader->baseShader.sampled_samplers[i] == sampler_idx)
815         {
816             recorded = TRUE;
817             break;
818         }
819     }
820
821     if (!recorded)
822     {
823         shader->baseShader.sampled_samplers[shader->baseShader.num_sampled_samplers] = sampler_idx;
824         ++shader->baseShader.num_sampled_samplers;
825     }
826
827     device->shader_backend->shader_color_correction(arg, fixup);
828 }
829
830 /** Shared code in order to generate the bulk of the shader string.
831     Use the shader_header_fct & shader_footer_fct to add strings
832     that are specific to pixel or vertex functions
833     NOTE: A description of how to parse tokens can be found on msdn */
834 void shader_generate_main(IWineD3DBaseShader *iface, SHADER_BUFFER* buffer,
835         const shader_reg_maps* reg_maps, CONST DWORD* pFunction)
836 {
837     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
838     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device; /* To access shader backend callbacks */
839     const SHADER_OPCODE *opcode_table = This->baseShader.shader_ins;
840     const SHADER_HANDLER *handler_table = device->shader_backend->shader_instruction_handler_table;
841     DWORD shader_version = This->baseShader.hex_version;
842     const DWORD *pToken = pFunction;
843     const SHADER_OPCODE *curOpcode = NULL;
844     SHADER_HANDLER hw_fct = NULL;
845     DWORD i;
846     SHADER_OPCODE_ARG hw_arg;
847
848     /* Initialize current parsing state */
849     hw_arg.shader = iface;
850     hw_arg.buffer = buffer;
851     hw_arg.reg_maps = reg_maps;
852     This->baseShader.parse_state.current_row = 0;
853
854     /* Second pass, process opcodes */
855     if (NULL != pToken) {
856         while (WINED3DPS_END() != *pToken) {
857
858             /* Skip version token */
859             if (shader_is_version_token(*pToken)) {
860                 ++pToken;
861                 continue;
862             }
863
864             /* Skip comment tokens */
865             if (shader_is_comment(*pToken)) {
866                 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
867                 ++pToken;
868                 TRACE("#%s\n", (const char*)pToken);
869                 pToken += comment_len;
870                 continue;
871             }
872
873             /* Read opcode */
874             hw_arg.opcode_token = *pToken++;
875             curOpcode = shader_get_opcode(opcode_table, shader_version, hw_arg.opcode_token);
876
877             /* Select handler */
878             if (curOpcode == NULL)
879                 hw_fct = NULL;
880             else
881                 hw_fct = handler_table[curOpcode->handler_idx];
882
883             /* Unknown opcode and its parameters */
884             if (NULL == curOpcode) {
885                 FIXME("Unrecognized opcode: token=0x%08x\n", hw_arg.opcode_token);
886                 pToken += shader_skip_unrecognized(iface, pToken); 
887
888             /* Nothing to do */
889             } else if (WINED3DSIO_DCL == curOpcode->opcode ||
890                        WINED3DSIO_NOP == curOpcode->opcode ||
891                        WINED3DSIO_DEF == curOpcode->opcode ||
892                        WINED3DSIO_DEFI == curOpcode->opcode ||
893                        WINED3DSIO_DEFB == curOpcode->opcode ||
894                        WINED3DSIO_PHASE == curOpcode->opcode ||
895                        WINED3DSIO_RET == curOpcode->opcode) {
896
897                 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
898
899             /* If a generator function is set for current shader target, use it */
900             } else if (hw_fct != NULL) {
901
902                 hw_arg.opcode = curOpcode;
903
904                 /* Destination token */
905                 if (curOpcode->dst_token) {
906
907                     DWORD param, addr_token = 0;
908                     pToken += shader_get_param(iface, pToken, &param, &addr_token);
909                     hw_arg.dst = param;
910                     hw_arg.dst_addr = addr_token;
911                 }
912
913                 /* Predication token */
914                 if (hw_arg.opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) 
915                     hw_arg.predicate = *pToken++;
916
917                 /* Other source tokens */
918                 for (i = 0; i < (curOpcode->num_params - curOpcode->dst_token); i++) {
919
920                     DWORD param, addr_token = 0; 
921                     pToken += shader_get_param(iface, pToken, &param, &addr_token);
922                     hw_arg.src[i] = param;
923                     hw_arg.src_addr[i] = addr_token;
924                 }
925
926                 /* Call appropriate function for output target */
927                 hw_fct(&hw_arg);
928
929                 /* Add color correction if needed */
930                 shader_color_correction(This, device, &hw_arg);
931
932                 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
933                 /* FIXME: This should be internal to the shader backend.
934                  * Also, right now this is the only reason "shader_mode" exists. */
935                 if (This->baseShader.shader_mode == SHADER_GLSL)
936                     shader_glsl_add_instruction_modifiers(&hw_arg);
937
938             /* Unhandled opcode */
939             } else {
940
941                 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
942                 pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
943             }
944         }
945         /* TODO: What about result.depth? */
946
947     }
948 }
949
950 static void shader_dump_ins_modifiers(const DWORD output)
951 {
952     DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
953     DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
954
955     switch (shift) {
956         case 0: break;
957         case 13: TRACE("_d8"); break;
958         case 14: TRACE("_d4"); break;
959         case 15: TRACE("_d2"); break;
960         case 1: TRACE("_x2"); break;
961         case 2: TRACE("_x4"); break;
962         case 3: TRACE("_x8"); break;
963         default: TRACE("_unhandled_shift(%d)", shift); break;
964     }
965
966     if (mmask & WINED3DSPDM_SATURATE)         TRACE("_sat");
967     if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
968     if (mmask & WINED3DSPDM_MSAMPCENTROID)    TRACE("_centroid");
969
970     mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
971     if (mmask)
972         FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
973 }
974
975 /* First pass: trace shader, initialize length and version */
976 void shader_trace_init(
977     IWineD3DBaseShader *iface,
978     const DWORD* pFunction) {
979
980     IWineD3DBaseShaderImpl *This =(IWineD3DBaseShaderImpl *)iface;
981
982     const DWORD* pToken = pFunction;
983     const SHADER_OPCODE* curOpcode = NULL;
984     DWORD opcode_token;
985     unsigned int len = 0;
986     DWORD i;
987
988     TRACE("(%p) : Parsing program\n", This);
989
990     if (NULL != pToken) {
991         while (WINED3DVS_END() != *pToken) {
992             if (shader_is_version_token(*pToken)) { /** version */
993                 This->baseShader.hex_version = *pToken;
994                 TRACE("%s_%u_%u\n", shader_is_pshader_version(This->baseShader.hex_version)? "ps": "vs",
995                     WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version),
996                     WINED3DSHADER_VERSION_MINOR(This->baseShader.hex_version));
997                 ++pToken;
998                 ++len;
999                 continue;
1000             }
1001             if (shader_is_comment(*pToken)) { /** comment */
1002                 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
1003                 ++pToken;
1004                 TRACE("//%s\n", (const char*)pToken);
1005                 pToken += comment_len;
1006                 len += comment_len + 1;
1007                 continue;
1008             }
1009             opcode_token = *pToken++;
1010             curOpcode = shader_get_opcode(This->baseShader.shader_ins, This->baseShader.hex_version, opcode_token);
1011             len++;
1012
1013             if (NULL == curOpcode) {
1014                 int tokens_read;
1015                 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
1016                 tokens_read = shader_skip_unrecognized(iface, pToken);
1017                 pToken += tokens_read;
1018                 len += tokens_read;
1019
1020             } else {
1021                 if (curOpcode->opcode == WINED3DSIO_DCL) {
1022
1023                     DWORD usage = *pToken;
1024                     DWORD param = *(pToken + 1);
1025
1026                     shader_dump_decl_usage(usage, param, This->baseShader.hex_version);
1027                     shader_dump_ins_modifiers(param);
1028                     TRACE(" ");
1029                     shader_dump_param(iface, param, 0, 0);
1030                     pToken += 2;
1031                     len += 2;
1032
1033                 } else if (curOpcode->opcode == WINED3DSIO_DEF) {
1034
1035                         unsigned int offset = shader_get_float_offset(*pToken);
1036
1037                         TRACE("def c%u = %f, %f, %f, %f", offset,
1038                             *(const float *)(pToken + 1),
1039                             *(const float *)(pToken + 2),
1040                             *(const float *)(pToken + 3),
1041                             *(const float *)(pToken + 4));
1042
1043                         pToken += 5;
1044                         len += 5;
1045                 } else if (curOpcode->opcode == WINED3DSIO_DEFI) {
1046
1047                         TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
1048                             *(pToken + 1),
1049                             *(pToken + 2),
1050                             *(pToken + 3),
1051                             *(pToken + 4));
1052
1053                         pToken += 5;
1054                         len += 5;
1055
1056                 } else if (curOpcode->opcode == WINED3DSIO_DEFB) {
1057
1058                         TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
1059                             *(pToken + 1)? "true": "false");
1060
1061                         pToken += 2;
1062                         len += 2;
1063
1064                 } else {
1065
1066                     DWORD param, addr_token;
1067                     int tokens_read;
1068
1069                     /* Print out predication source token first - it follows
1070                      * the destination token. */
1071                     if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
1072                         TRACE("(");
1073                         shader_dump_param(iface, *(pToken + 2), 0, 1);
1074                         TRACE(") ");
1075                     }
1076                     if (opcode_token & WINED3DSI_COISSUE) {
1077                         /* PixWin marks instructions with the coissue flag with a '+' */
1078                         TRACE("+");
1079                     }
1080
1081                     TRACE("%s", curOpcode->name);
1082
1083                     if (curOpcode->opcode == WINED3DSIO_IFC ||
1084                         curOpcode->opcode == WINED3DSIO_BREAKC) {
1085
1086                         DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1087                         switch (op) {
1088                             case COMPARISON_GT: TRACE("_gt"); break;
1089                             case COMPARISON_EQ: TRACE("_eq"); break;
1090                             case COMPARISON_GE: TRACE("_ge"); break;
1091                             case COMPARISON_LT: TRACE("_lt"); break;
1092                             case COMPARISON_NE: TRACE("_ne"); break;
1093                             case COMPARISON_LE: TRACE("_le"); break;
1094                             default:
1095                                 TRACE("_(%u)", op);
1096                         }
1097                     } else if (curOpcode->opcode == WINED3DSIO_TEX &&
1098                                This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
1099                         if(opcode_token & WINED3DSI_TEXLD_PROJECT) TRACE("p");
1100                     }
1101
1102                     /* Destination token */
1103                     if (curOpcode->dst_token) {
1104
1105                         /* Destination token */
1106                         tokens_read = shader_get_param(iface, pToken, &param, &addr_token);
1107                         pToken += tokens_read;
1108                         len += tokens_read;
1109
1110                         shader_dump_ins_modifiers(param);
1111                         TRACE(" ");
1112                         shader_dump_param(iface, param, addr_token, 0);
1113                     }
1114
1115                     /* Predication token - already printed out, just skip it */
1116                     if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
1117                         pToken++;
1118                         len++;
1119                     }
1120
1121                     /* Other source tokens */
1122                     for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i) {
1123
1124                         tokens_read = shader_get_param(iface, pToken, &param, &addr_token);
1125                         pToken += tokens_read;
1126                         len += tokens_read;
1127
1128                         TRACE((i == 0)? " " : ", ");
1129                         shader_dump_param(iface, param, addr_token, 1);
1130                     }
1131                 }
1132                 TRACE("\n");
1133             }
1134         }
1135         This->baseShader.functionLength = (len + 1) * sizeof(DWORD);
1136     } else {
1137         This->baseShader.functionLength = 1; /* no Function defined use fixed function vertex processing */
1138     }
1139 }
1140
1141 static const SHADER_HANDLER shader_none_instruction_handler_table[WINED3DSIH_TABLE_SIZE] = {0};
1142 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
1143 static void shader_none_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {}
1144 static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1145 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1146 static void shader_none_cleanup(IWineD3DDevice *iface) {}
1147 static void shader_none_color_correction(const struct SHADER_OPCODE_ARG *arg, struct color_fixup_desc fixup) {}
1148 static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1149 static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1150 static void shader_none_free(IWineD3DDevice *iface) {}
1151 static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1152 static GLuint shader_none_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
1153     FIXME("NONE shader backend asked to generate a pixel shader\n");
1154     return 0;
1155 }
1156 static void shader_none_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
1157     FIXME("NONE shader backend asked to generate a vertex shader\n");
1158 }
1159
1160 #define GLINFO_LOCATION      (*gl_info)
1161 static void shader_none_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
1162 {
1163     /* Set the shader caps to 0 for the none shader backend */
1164     pCaps->VertexShaderVersion  = 0;
1165     pCaps->PixelShaderVersion    = 0;
1166     pCaps->PixelShader1xMaxValue = 0.0;
1167 }
1168 #undef GLINFO_LOCATION
1169 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1170 {
1171     if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1172     {
1173         TRACE("Checking support for fixup:\n");
1174         dump_color_fixup_desc(fixup);
1175     }
1176
1177     /* Faked to make some apps happy. */
1178     if (!is_yuv_fixup(fixup))
1179     {
1180         TRACE("[OK]\n");
1181         return TRUE;
1182     }
1183
1184     TRACE("[FAILED]\n");
1185     return FALSE;
1186 }
1187
1188 const shader_backend_t none_shader_backend = {
1189     shader_none_instruction_handler_table,
1190     shader_none_select,
1191     shader_none_select_depth_blt,
1192     shader_none_deselect_depth_blt,
1193     shader_none_load_constants,
1194     shader_none_cleanup,
1195     shader_none_color_correction,
1196     shader_none_destroy,
1197     shader_none_alloc,
1198     shader_none_free,
1199     shader_none_dirty_const,
1200     shader_none_generate_pshader,
1201     shader_none_generate_vshader,
1202     shader_none_get_caps,
1203     shader_none_color_fixup_supported,
1204 };
1205
1206 /* *******************************************
1207    IWineD3DPixelShader IUnknown parts follow
1208    ******************************************* */
1209 HRESULT  WINAPI IWineD3DBaseShaderImpl_QueryInterface(IWineD3DBaseShader *iface, REFIID riid, LPVOID *ppobj)
1210 {
1211     IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1212     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
1213     if (IsEqualGUID(riid, &IID_IUnknown)
1214         || IsEqualGUID(riid, &IID_IWineD3DBase)
1215         || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1216         || IsEqualGUID(riid, &IID_IWineD3DPixelShader)) {
1217         IUnknown_AddRef(iface);
1218         *ppobj = This;
1219         return S_OK;
1220     }
1221     *ppobj = NULL;
1222     return E_NOINTERFACE;
1223 }
1224
1225 ULONG  WINAPI IWineD3DBaseShaderImpl_AddRef(IWineD3DBaseShader *iface) {
1226     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
1227     TRACE("(%p) : AddRef increasing from %d\n", This, This->baseShader.ref);
1228     return InterlockedIncrement(&This->baseShader.ref);
1229 }
1230
1231 ULONG  WINAPI IWineD3DBaseShaderImpl_Release(IWineD3DBaseShader *iface) {
1232     IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1233     IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
1234     ULONG ref;
1235     TRACE("(%p) : Releasing from %d\n", This, This->baseShader.ref);
1236     ref = InterlockedDecrement(&This->baseShader.ref);
1237     if (ref == 0) {
1238         deviceImpl->shader_backend->shader_destroy(iface);
1239         HeapFree(GetProcessHeap(), 0, This->baseShader.function);
1240         shader_delete_constant_list(&This->baseShader.constantsF);
1241         shader_delete_constant_list(&This->baseShader.constantsB);
1242         shader_delete_constant_list(&This->baseShader.constantsI);
1243         list_remove(&This->baseShader.shader_list_entry);
1244         HeapFree(GetProcessHeap(), 0, This);
1245     }
1246     return ref;
1247 }