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