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