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