wined3d: Set FBO stencil attachments for relevant depth stencil formats.
[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 static const char *shader_opcode_names[] =
36 {
37     /* WINED3DSIH_ABS           */ "abs",
38     /* WINED3DSIH_ADD           */ "add",
39     /* WINED3DSIH_BEM           */ "bem",
40     /* WINED3DSIH_BREAK         */ "break",
41     /* WINED3DSIH_BREAKC        */ "breakc",
42     /* WINED3DSIH_BREAKP        */ "breakp",
43     /* WINED3DSIH_CALL          */ "call",
44     /* WINED3DSIH_CALLNZ        */ "callnz",
45     /* WINED3DSIH_CMP           */ "cmp",
46     /* WINED3DSIH_CND           */ "cnd",
47     /* WINED3DSIH_CRS           */ "crs",
48     /* WINED3DSIH_DCL           */ "dcl",
49     /* WINED3DSIH_DEF           */ "def",
50     /* WINED3DSIH_DEFB          */ "defb",
51     /* WINED3DSIH_DEFI          */ "defi",
52     /* WINED3DSIH_DP2ADD        */ "dp2add",
53     /* WINED3DSIH_DP3           */ "dp3",
54     /* WINED3DSIH_DP4           */ "dp4",
55     /* WINED3DSIH_DST           */ "dst",
56     /* WINED3DSIH_DSX           */ "dsx",
57     /* WINED3DSIH_DSY           */ "dsy",
58     /* WINED3DSIH_ELSE          */ "else",
59     /* WINED3DSIH_ENDIF         */ "endif",
60     /* WINED3DSIH_ENDLOOP       */ "endloop",
61     /* WINED3DSIH_ENDREP        */ "endrep",
62     /* WINED3DSIH_EXP           */ "exp",
63     /* WINED3DSIH_EXPP          */ "expp",
64     /* WINED3DSIH_FRC           */ "frc",
65     /* WINED3DSIH_IF            */ "if",
66     /* WINED3DSIH_IFC           */ "ifc",
67     /* WINED3DSIH_LABEL         */ "label",
68     /* WINED3DSIH_LIT           */ "lit",
69     /* WINED3DSIH_LOG           */ "log",
70     /* WINED3DSIH_LOGP          */ "logp",
71     /* WINED3DSIH_LOOP          */ "loop",
72     /* WINED3DSIH_LRP           */ "lrp",
73     /* WINED3DSIH_M3x2          */ "m3x2",
74     /* WINED3DSIH_M3x3          */ "m3x3",
75     /* WINED3DSIH_M3x4          */ "m3x4",
76     /* WINED3DSIH_M4x3          */ "m4x3",
77     /* WINED3DSIH_M4x4          */ "m4x4",
78     /* WINED3DSIH_MAD           */ "mad",
79     /* WINED3DSIH_MAX           */ "max",
80     /* WINED3DSIH_MIN           */ "min",
81     /* WINED3DSIH_MOV           */ "mov",
82     /* WINED3DSIH_MOVA          */ "mova",
83     /* WINED3DSIH_MUL           */ "mul",
84     /* WINED3DSIH_NOP           */ "nop",
85     /* WINED3DSIH_NRM           */ "nrm",
86     /* WINED3DSIH_PHASE         */ "phase",
87     /* WINED3DSIH_POW           */ "pow",
88     /* WINED3DSIH_RCP           */ "rcp",
89     /* WINED3DSIH_REP           */ "rep",
90     /* WINED3DSIH_RET           */ "ret",
91     /* WINED3DSIH_RSQ           */ "rsq",
92     /* WINED3DSIH_SETP          */ "setp",
93     /* WINED3DSIH_SGE           */ "sge",
94     /* WINED3DSIH_SGN           */ "sgn",
95     /* WINED3DSIH_SINCOS        */ "sincos",
96     /* WINED3DSIH_SLT           */ "slt",
97     /* WINED3DSIH_SUB           */ "sub",
98     /* WINED3DSIH_TEX           */ "texld",
99     /* WINED3DSIH_TEXBEM        */ "texbem",
100     /* WINED3DSIH_TEXBEML       */ "texbeml",
101     /* WINED3DSIH_TEXCOORD      */ "texcrd",
102     /* WINED3DSIH_TEXDEPTH      */ "texdepth",
103     /* WINED3DSIH_TEXDP3        */ "texdp3",
104     /* WINED3DSIH_TEXDP3TEX     */ "texdp3tex",
105     /* WINED3DSIH_TEXKILL       */ "texkill",
106     /* WINED3DSIH_TEXLDD        */ "texldd",
107     /* WINED3DSIH_TEXLDL        */ "texldl",
108     /* WINED3DSIH_TEXM3x2DEPTH  */ "texm3x2depth",
109     /* WINED3DSIH_TEXM3x2PAD    */ "texm3x2pad",
110     /* WINED3DSIH_TEXM3x2TEX    */ "texm3x2tex",
111     /* WINED3DSIH_TEXM3x3       */ "texm3x3",
112     /* WINED3DSIH_TEXM3x3DIFF   */ "texm3x3diff",
113     /* WINED3DSIH_TEXM3x3PAD    */ "texm3x3pad",
114     /* WINED3DSIH_TEXM3x3SPEC   */ "texm3x3spec",
115     /* WINED3DSIH_TEXM3x3TEX    */ "texm3x3tex",
116     /* WINED3DSIH_TEXM3x3VSPEC  */ "texm3x3vspec",
117     /* WINED3DSIH_TEXREG2AR     */ "texreg2ar",
118     /* WINED3DSIH_TEXREG2GB     */ "texreg2gb",
119     /* WINED3DSIH_TEXREG2RGB    */ "texreg2rgb",
120 };
121
122 const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token)
123 {
124     switch (version_token >> 16)
125     {
126         case WINED3D_SM1_VS:
127         case WINED3D_SM1_PS:
128             return &sm1_shader_frontend;
129
130         case WINED3D_SM4_PS:
131         case WINED3D_SM4_VS:
132         case WINED3D_SM4_GS:
133             return &sm4_shader_frontend;
134
135         default:
136             FIXME("Unrecognised version token %#x\n", version_token);
137             return NULL;
138     }
139 }
140
141 static inline BOOL shader_is_version_token(DWORD token) {
142     return shader_is_pshader_version(token) ||
143            shader_is_vshader_version(token);
144 }
145
146 void shader_buffer_init(struct SHADER_BUFFER *buffer)
147 {
148     buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
149     buffer->buffer[0] = '\0';
150     buffer->bsize = 0;
151     buffer->lineNo = 0;
152     buffer->newline = TRUE;
153 }
154
155 void shader_buffer_free(struct SHADER_BUFFER *buffer)
156 {
157     HeapFree(GetProcessHeap(), 0, buffer->buffer);
158 }
159
160 int shader_vaddline(SHADER_BUFFER* buffer, const char *format, va_list args)
161 {
162     char* base = buffer->buffer + buffer->bsize;
163     int rc;
164
165     rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
166
167     if (rc < 0 ||                                   /* C89 */ 
168         rc > SHADER_PGMSIZE - 1 - buffer->bsize) {  /* C99 */
169
170         ERR("The buffer allocated for the shader program string "
171             "is too small at %d bytes.\n", SHADER_PGMSIZE);
172         buffer->bsize = SHADER_PGMSIZE - 1;
173         return -1;
174     }
175
176     if (buffer->newline) {
177         TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
178         buffer->newline = FALSE;
179     } else {
180         TRACE("%s", base);
181     }
182
183     buffer->bsize += rc;
184     if (buffer->buffer[buffer->bsize-1] == '\n') {
185         buffer->lineNo++;
186         buffer->newline = TRUE;
187     }
188     return 0;
189 }
190
191 int shader_addline(SHADER_BUFFER* buffer, const char *format, ...)
192 {
193     int ret;
194     va_list args;
195
196     va_start(args, format);
197     ret = shader_vaddline(buffer, format, args);
198     va_end(args);
199
200     return ret;
201 }
202
203 void shader_init(struct IWineD3DBaseShaderClass *shader, IWineD3DDevice *device)
204 {
205     shader->ref = 1;
206     shader->device = device;
207     list_init(&shader->linked_programs);
208 }
209
210 /* Convert floating point offset relative
211  * to a register file to an absolute offset for float constants */
212 static unsigned int shader_get_float_offset(WINED3DSHADER_PARAM_REGISTER_TYPE register_type, UINT register_idx)
213 {
214     switch (register_type)
215     {
216         case WINED3DSPR_CONST: return register_idx;
217         case WINED3DSPR_CONST2: return 2048 + register_idx;
218         case WINED3DSPR_CONST3: return 4096 + register_idx;
219         case WINED3DSPR_CONST4: return 6144 + register_idx;
220         default:
221             FIXME("Unsupported register type: %d\n", register_type);
222             return register_idx;
223     }
224 }
225
226 static void shader_delete_constant_list(struct list* clist) {
227
228     struct list *ptr;
229     struct local_constant* constant;
230
231     ptr = list_head(clist);
232     while (ptr) {
233         constant = LIST_ENTRY(ptr, struct local_constant, entry);
234         ptr = list_next(clist, ptr);
235         HeapFree(GetProcessHeap(), 0, constant);
236     }
237     list_init(clist);
238 }
239
240 static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
241 {
242     DWORD idx, shift;
243     idx = bit >> 5;
244     shift = bit & 0x1f;
245     bitmap[idx] |= (1 << shift);
246 }
247
248 static void shader_record_register_usage(IWineD3DBaseShaderImpl *This, struct shader_reg_maps *reg_maps,
249         const struct wined3d_shader_register *reg, BOOL pshader)
250 {
251     switch (reg->type)
252     {
253         case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
254             if (pshader) reg_maps->texcoord[reg->idx] = 1;
255             else reg_maps->address[reg->idx] = 1;
256             break;
257
258         case WINED3DSPR_TEMP:
259             reg_maps->temporary[reg->idx] = 1;
260             break;
261
262         case WINED3DSPR_INPUT:
263             if (!pshader) reg_maps->input_registers |= 1 << reg->idx;
264             else
265             {
266                 if (reg->rel_addr)
267                 {
268                     /* If relative addressing is used, we must assume that all registers
269                      * are used. Even if it is a construct like v3[aL], we can't assume
270                      * that v0, v1 and v2 aren't read because aL can be negative */
271                     unsigned int i;
272                     for (i = 0; i < MAX_REG_INPUT; ++i)
273                     {
274                         ((IWineD3DPixelShaderImpl *)This)->input_reg_used[i] = TRUE;
275                     }
276                 }
277                 else
278                 {
279                     ((IWineD3DPixelShaderImpl *)This)->input_reg_used[reg->idx] = TRUE;
280                 }
281             }
282             break;
283
284         case WINED3DSPR_RASTOUT:
285             if (reg->idx == 1) reg_maps->fog = 1;
286             break;
287
288         case WINED3DSPR_MISCTYPE:
289             if (pshader && reg->idx == 0) reg_maps->vpos = 1;
290             break;
291
292         case WINED3DSPR_CONST:
293             if (reg->rel_addr)
294             {
295                 if (!pshader)
296                 {
297                     if (reg->idx <= ((IWineD3DVertexShaderImpl *)This)->min_rel_offset)
298                         ((IWineD3DVertexShaderImpl *)This)->min_rel_offset = reg->idx;
299                     else if (reg->idx >= ((IWineD3DVertexShaderImpl *)This)->max_rel_offset)
300                         ((IWineD3DVertexShaderImpl *)This)->max_rel_offset = reg->idx;
301                 }
302                 reg_maps->usesrelconstF = TRUE;
303             }
304             else
305             {
306                 set_bitmap_bit(reg_maps->constf, reg->idx);
307             }
308             break;
309
310         case WINED3DSPR_CONSTINT:
311             reg_maps->integer_constants |= (1 << reg->idx);
312             break;
313
314         case WINED3DSPR_CONSTBOOL:
315             reg_maps->boolean_constants |= (1 << reg->idx);
316             break;
317
318         default:
319             TRACE("Not recording register of type %#x and idx %u\n", reg->type, reg->idx);
320             break;
321     }
322 }
323
324 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
325 {
326     switch(instr)
327     {
328         case WINED3DSIH_M4x4:
329         case WINED3DSIH_M3x4:
330             return param == 1 ? 3 : 0;
331
332         case WINED3DSIH_M4x3:
333         case WINED3DSIH_M3x3:
334             return param == 1 ? 2 : 0;
335
336         case WINED3DSIH_M3x2:
337             return param == 1 ? 1 : 0;
338
339         default:
340             return 0;
341     }
342 }
343
344 static const char *shader_semantic_name_from_usage(WINED3DDECLUSAGE usage)
345 {
346     static const char *semantic_names[] =
347     {
348         /* WINED3DDECLUSAGE_POSITION        */ "SV_POSITION",
349         /* WINED3DDECLUSAGE_BLENDWEIGHT     */ "BLENDWEIGHT",
350         /* WINED3DDECLUSAGE_BLENDINDICES    */ "BLENDINDICES",
351         /* WINED3DDECLUSAGE_NORMAL          */ "NORMAL",
352         /* WINED3DDECLUSAGE_PSIZE           */ "PSIZE",
353         /* WINED3DDECLUSAGE_TEXCOORD        */ "TEXCOORD",
354         /* WINED3DDECLUSAGE_TANGENT         */ "TANGENT",
355         /* WINED3DDECLUSAGE_BINORMAL        */ "BINORMAL",
356         /* WINED3DDECLUSAGE_TESSFACTOR      */ "TESSFACTOR",
357         /* WINED3DDECLUSAGE_POSITIONT       */ "POSITIONT",
358         /* WINED3DDECLUSAGE_COLOR           */ "COLOR",
359         /* WINED3DDECLUSAGE_FOG             */ "FOG",
360         /* WINED3DDECLUSAGE_DEPTH           */ "DEPTH",
361         /* WINED3DDECLUSAGE_SAMPLE          */ "SAMPLE",
362     };
363
364     if (usage >= sizeof(semantic_names) / sizeof(*semantic_names))
365     {
366         FIXME("Unrecognized usage %#x\n", usage);
367         return "UNRECOGNIZED";
368     }
369
370     return semantic_names[usage];
371 }
372
373 BOOL shader_match_semantic(const char *semantic_name, WINED3DDECLUSAGE usage)
374 {
375     return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
376 }
377
378 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
379         const struct wined3d_shader_semantic *s)
380 {
381     e->semantic_name = shader_semantic_name_from_usage(s->usage);
382     e->semantic_idx = s->usage_idx;
383     e->sysval_semantic = 0;
384     e->component_type = 0;
385     e->register_idx = s->reg.reg.idx;
386     e->mask = s->reg.write_mask;
387 }
388
389 /* Note that this does not count the loop register
390  * as an address register. */
391
392 HRESULT shader_get_registers_used(IWineD3DBaseShader *iface, const struct wined3d_shader_frontend *fe,
393         struct shader_reg_maps *reg_maps, struct wined3d_shader_attribute *attributes,
394         struct wined3d_shader_signature_element *input_signature,
395         struct wined3d_shader_signature_element *output_signature, const DWORD *byte_code, DWORD constf_size)
396 {
397     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
398     void *fe_data = This->baseShader.frontend_data;
399     struct wined3d_shader_version shader_version;
400     unsigned int cur_loop_depth = 0, max_loop_depth = 0;
401     const DWORD* pToken = byte_code;
402     char pshader;
403
404     /* There are some minor differences between pixel and vertex shaders */
405
406     memset(reg_maps, 0, sizeof(*reg_maps));
407
408     /* get_registers_used is called on every compile on some 1.x shaders, which can result
409      * in stacking up a collection of local constants. Delete the old constants if existing
410      */
411     shader_delete_constant_list(&This->baseShader.constantsF);
412     shader_delete_constant_list(&This->baseShader.constantsB);
413     shader_delete_constant_list(&This->baseShader.constantsI);
414
415     fe->shader_read_header(fe_data, &pToken, &shader_version);
416     reg_maps->shader_version = shader_version;
417     pshader = shader_is_pshader_version(shader_version.type);
418
419     reg_maps->constf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
420                                  sizeof(*reg_maps->constf) * ((constf_size + 31) / 32));
421     if(!reg_maps->constf) {
422         ERR("Out of memory\n");
423         return E_OUTOFMEMORY;
424     }
425
426     while (!fe->shader_is_end(fe_data, &pToken))
427     {
428         struct wined3d_shader_instruction ins;
429         const char *comment;
430         UINT param_size;
431
432         /* Skip comments */
433         fe->shader_read_comment(&pToken, &comment);
434         if (comment) continue;
435
436         /* Fetch opcode */
437         fe->shader_read_opcode(fe_data, &pToken, &ins, &param_size);
438
439         /* Unhandled opcode, and its parameters */
440         if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
441         {
442             TRACE("Skipping unrecognized instruction.\n");
443             pToken += param_size;
444             continue;
445         }
446
447         /* Handle declarations */
448         if (ins.handler_idx == WINED3DSIH_DCL)
449         {
450             struct wined3d_shader_semantic semantic;
451
452             fe->shader_read_semantic(&pToken, &semantic);
453
454             switch (semantic.reg.reg.type)
455             {
456                 /* Vshader: mark attributes used
457                  * Pshader: mark 3.0 input registers used, save token */
458                 case WINED3DSPR_INPUT:
459                     reg_maps->input_registers |= 1 << semantic.reg.reg.idx;
460                     if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
461                     {
462                         attributes[semantic.reg.reg.idx].usage = semantic.usage;
463                         attributes[semantic.reg.reg.idx].usage_idx = semantic.usage_idx;
464                     }
465                     else
466                     {
467                         shader_signature_from_semantic(&input_signature[semantic.reg.reg.idx], &semantic);
468                     }
469                     break;
470
471                 /* Vshader: mark 3.0 output registers used, save token */
472                 case WINED3DSPR_OUTPUT:
473                     reg_maps->output_registers |= 1 << semantic.reg.reg.idx;
474                     shader_signature_from_semantic(&output_signature[semantic.reg.reg.idx], &semantic);
475                     if (semantic.usage == WINED3DDECLUSAGE_FOG) reg_maps->fog = 1;
476                     break;
477
478                 /* Save sampler usage token */
479                 case WINED3DSPR_SAMPLER:
480                     reg_maps->sampler_type[semantic.reg.reg.idx] = semantic.sampler_type;
481                     break;
482
483                 default:
484                     TRACE("Not recording DCL register type %#x.\n", semantic.reg.reg.type);
485                     break;
486             }
487         }
488         else if (ins.handler_idx == WINED3DSIH_DEF)
489         {
490             struct wined3d_shader_dst_param dst;
491             struct wined3d_shader_src_param rel_addr;
492
493             local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
494             if (!lconst) return E_OUTOFMEMORY;
495
496             fe->shader_read_dst_param(fe_data, &pToken, &dst, &rel_addr);
497             lconst->idx = dst.reg.idx;
498
499             memcpy(lconst->value, pToken, 4 * sizeof(DWORD));
500             pToken += 4;
501
502             /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
503             if (shader_version.major == 1 && pshader)
504             {
505                 float *value = (float *) lconst->value;
506                 if(value[0] < -1.0) value[0] = -1.0;
507                 else if(value[0] >  1.0) value[0] =  1.0;
508                 if(value[1] < -1.0) value[1] = -1.0;
509                 else if(value[1] >  1.0) value[1] =  1.0;
510                 if(value[2] < -1.0) value[2] = -1.0;
511                 else if(value[2] >  1.0) value[2] =  1.0;
512                 if(value[3] < -1.0) value[3] = -1.0;
513                 else if(value[3] >  1.0) value[3] =  1.0;
514             }
515
516             list_add_head(&This->baseShader.constantsF, &lconst->entry);
517         }
518         else if (ins.handler_idx == WINED3DSIH_DEFI)
519         {
520             struct wined3d_shader_dst_param dst;
521             struct wined3d_shader_src_param rel_addr;
522
523             local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
524             if (!lconst) return E_OUTOFMEMORY;
525
526             fe->shader_read_dst_param(fe_data, &pToken, &dst, &rel_addr);
527             lconst->idx = dst.reg.idx;
528
529             memcpy(lconst->value, pToken, 4 * sizeof(DWORD));
530             pToken += 4;
531
532             list_add_head(&This->baseShader.constantsI, &lconst->entry);
533         }
534         else if (ins.handler_idx == WINED3DSIH_DEFB)
535         {
536             struct wined3d_shader_dst_param dst;
537             struct wined3d_shader_src_param rel_addr;
538
539             local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
540             if (!lconst) return E_OUTOFMEMORY;
541
542             fe->shader_read_dst_param(fe_data, &pToken, &dst, &rel_addr);
543             lconst->idx = dst.reg.idx;
544
545             memcpy(lconst->value, pToken, sizeof(DWORD));
546             ++pToken;
547
548             list_add_head(&This->baseShader.constantsB, &lconst->entry);
549             reg_maps->local_bool_consts |= (1 << dst.reg.idx);
550         }
551         /* If there's a loop in the shader */
552         else if (ins.handler_idx == WINED3DSIH_LOOP
553                 || ins.handler_idx == WINED3DSIH_REP)
554         {
555             struct wined3d_shader_src_param src, rel_addr;
556
557             fe->shader_read_src_param(fe_data, &pToken, &src, &rel_addr);
558
559             /* Rep and Loop always use an integer constant for the control parameters */
560             if (ins.handler_idx == WINED3DSIH_REP)
561             {
562                 reg_maps->integer_constants |= 1 << src.reg.idx;
563             }
564             else
565             {
566                 fe->shader_read_src_param(fe_data, &pToken, &src, &rel_addr);
567                 reg_maps->integer_constants |= 1 << src.reg.idx;
568             }
569
570             cur_loop_depth++;
571             if(cur_loop_depth > max_loop_depth)
572                 max_loop_depth = cur_loop_depth;
573         }
574         else if (ins.handler_idx == WINED3DSIH_ENDLOOP
575                 || ins.handler_idx == WINED3DSIH_ENDREP)
576         {
577             cur_loop_depth--;
578         }
579         /* For subroutine prototypes */
580         else if (ins.handler_idx == WINED3DSIH_LABEL)
581         {
582             struct wined3d_shader_src_param src, rel_addr;
583
584             fe->shader_read_src_param(fe_data, &pToken, &src, &rel_addr);
585             reg_maps->labels[src.reg.idx] = 1;
586         }
587         /* Set texture, address, temporary registers */
588         else
589         {
590             int i, limit;
591             BOOL color0_mov = FALSE;
592
593             /* This will loop over all the registers and try to
594              * make a bitmask of the ones we're interested in.
595              *
596              * Relative addressing tokens are ignored, but that's
597              * okay, since we'll catch any address registers when
598              * they are initialized (required by spec) */
599
600             if (ins.dst_count)
601             {
602                 struct wined3d_shader_dst_param dst_param;
603                 struct wined3d_shader_src_param dst_rel_addr;
604
605                 fe->shader_read_dst_param(fe_data, &pToken, &dst_param, &dst_rel_addr);
606
607                 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
608                  * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
609                  * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
610                 if (!pshader && shader_version.major < 3 && dst_param.reg.type == WINED3DSPR_TEXCRDOUT)
611                 {
612                     reg_maps->texcoord_mask[dst_param.reg.type] |= dst_param.write_mask;
613                 }
614                 else
615                 {
616                     if(pshader && dst_param.reg.type == WINED3DSPR_COLOROUT && dst_param.reg.idx == 0)
617                     {
618                         IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) This;
619                         ps->color0_mov = FALSE;
620                     }
621                     shader_record_register_usage(This, reg_maps, &dst_param.reg, pshader);
622                 }
623
624                 /* Declare 1.X samplers implicitly, based on the destination reg. number */
625                 if (shader_version.major == 1
626                         && pshader /* Filter different instructions with the same enum values in VS */
627                         && (ins.handler_idx == WINED3DSIH_TEX
628                             || ins.handler_idx == WINED3DSIH_TEXBEM
629                             || ins.handler_idx == WINED3DSIH_TEXBEML
630                             || ins.handler_idx == WINED3DSIH_TEXDP3TEX
631                             || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
632                             || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
633                             || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
634                             || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
635                             || ins.handler_idx == WINED3DSIH_TEXREG2AR
636                             || ins.handler_idx == WINED3DSIH_TEXREG2GB
637                             || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
638                 {
639                     /* Fake sampler usage, only set reserved bit and ttype */
640                     DWORD sampler_code = dst_param.reg.idx;
641
642                     TRACE("Setting fake 2D sampler for 1.x pixelshader\n");
643                     reg_maps->sampler_type[sampler_code] = WINED3DSTT_2D;
644
645                     /* texbem is only valid with < 1.4 pixel shaders */
646                     if (ins.handler_idx == WINED3DSIH_TEXBEM
647                             || ins.handler_idx == WINED3DSIH_TEXBEML)
648                     {
649                         reg_maps->bumpmat[sampler_code] = TRUE;
650                         if (ins.handler_idx == WINED3DSIH_TEXBEML)
651                         {
652                             reg_maps->luminanceparams[sampler_code] = TRUE;
653                         }
654                     }
655                 }
656                 else if (pshader && ins.handler_idx == WINED3DSIH_BEM)
657                 {
658                     reg_maps->bumpmat[dst_param.reg.idx] = TRUE;
659                 }
660                 else if(pshader && ins.handler_idx == WINED3DSIH_MOV)
661                 {
662                     /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
663                      * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
664                      * the mov and perform the sRGB write correction from the source register.
665                      *
666                      * However, if the mov is only partial, we can't do this, and if the write
667                      * comes from an instruction other than MOV it is hard to do as well. If
668                      * COLOROUT 0 is overwritten partially later, the marker is dropped again
669                      */
670                     if(dst_param.reg.type == WINED3DSPR_COLOROUT && dst_param.reg.idx == 0)
671                     {
672                         /* Used later when the source register is read */
673                         color0_mov = TRUE;
674                     }
675                 }
676             }
677
678             if (ins.handler_idx == WINED3DSIH_NRM)
679             {
680                 reg_maps->usesnrm = 1;
681             }
682             else if (ins.handler_idx == WINED3DSIH_DSY)
683             {
684                 reg_maps->usesdsy = 1;
685             }
686             else if (ins.handler_idx == WINED3DSIH_DSX)
687             {
688                 reg_maps->usesdsx = 1;
689             }
690             else if(ins.handler_idx == WINED3DSIH_TEXLDD)
691             {
692                 reg_maps->usestexldd = 1;
693             }
694             else if(ins.handler_idx == WINED3DSIH_MOVA)
695             {
696                 reg_maps->usesmova = 1;
697             }
698
699             limit = ins.src_count + (ins.predicate ? 1 : 0);
700             for (i = 0; i < limit; ++i)
701             {
702                 struct wined3d_shader_src_param src_param, src_rel_addr;
703                 unsigned int count;
704
705                 fe->shader_read_src_param(fe_data, &pToken, &src_param, &src_rel_addr);
706                 count = get_instr_extra_regcount(ins.handler_idx, i);
707
708                 shader_record_register_usage(This, reg_maps, &src_param.reg, pshader);
709                 while (count)
710                 {
711                     ++src_param.reg.idx;
712                     shader_record_register_usage(This, reg_maps, &src_param.reg, pshader);
713                     --count;
714                 }
715
716                 if(color0_mov)
717                 {
718                     IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) This;
719                     if(src_param.reg.type == WINED3DSPR_TEMP &&
720                        src_param.swizzle == WINED3DSP_NOSWIZZLE)
721                     {
722                         ps->color0_mov = TRUE;
723                         ps->color0_reg = src_param.reg.idx;
724                     }
725                 }
726             }
727         }
728     }
729     reg_maps->loop_depth = max_loop_depth;
730
731     This->baseShader.functionLength = ((char *)pToken - (char *)byte_code);
732
733     return WINED3D_OK;
734 }
735
736 static void shader_dump_decl_usage(const struct wined3d_shader_semantic *semantic,
737         const struct wined3d_shader_version *shader_version)
738 {
739     TRACE("dcl");
740
741     if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
742     {
743         switch (semantic->sampler_type)
744         {
745             case WINED3DSTT_2D: TRACE("_2d"); break;
746             case WINED3DSTT_CUBE: TRACE("_cube"); break;
747             case WINED3DSTT_VOLUME: TRACE("_volume"); break;
748             default: TRACE("_unknown_ttype(0x%08x)", semantic->sampler_type);
749         }
750     }
751     else
752     {
753         /* Pixel shaders 3.0 don't have usage semantics */
754         if (shader_is_pshader_version(shader_version->type) && shader_version->major < 3)
755             return;
756         else
757             TRACE("_");
758
759         switch (semantic->usage)
760         {
761             case WINED3DDECLUSAGE_POSITION:
762                 TRACE("position%d", semantic->usage_idx);
763                 break;
764             case WINED3DDECLUSAGE_BLENDINDICES:
765                 TRACE("blend");
766                 break;
767             case WINED3DDECLUSAGE_BLENDWEIGHT:
768                 TRACE("weight");
769                 break;
770             case WINED3DDECLUSAGE_NORMAL:
771                 TRACE("normal%d", semantic->usage_idx);
772                 break;
773             case WINED3DDECLUSAGE_PSIZE:
774                 TRACE("psize");
775                 break;
776             case WINED3DDECLUSAGE_COLOR:
777                 if (semantic->usage_idx == 0) TRACE("color");
778                 else TRACE("specular%d", (semantic->usage_idx - 1));
779                 break;
780             case WINED3DDECLUSAGE_TEXCOORD:
781                 TRACE("texture%d", semantic->usage_idx);
782                 break;
783             case WINED3DDECLUSAGE_TANGENT:
784                 TRACE("tangent");
785                 break;
786             case WINED3DDECLUSAGE_BINORMAL:
787                 TRACE("binormal");
788                 break;
789             case WINED3DDECLUSAGE_TESSFACTOR:
790                 TRACE("tessfactor");
791                 break;
792             case WINED3DDECLUSAGE_POSITIONT:
793                 TRACE("positionT%d", semantic->usage_idx);
794                 break;
795             case WINED3DDECLUSAGE_FOG:
796                 TRACE("fog");
797                 break;
798             case WINED3DDECLUSAGE_DEPTH:
799                 TRACE("depth");
800                 break;
801             case WINED3DDECLUSAGE_SAMPLE:
802                 TRACE("sample");
803                 break;
804             default:
805                 FIXME("unknown_semantics(0x%08x)", semantic->usage);
806         }
807     }
808 }
809
810 static void shader_dump_register(const struct wined3d_shader_register *reg,
811         const struct wined3d_shader_version *shader_version)
812 {
813     static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
814     static const char * const misctype_reg_names[] = {"vPos", "vFace"};
815     UINT offset = reg->idx;
816
817     switch (reg->type)
818     {
819         case WINED3DSPR_TEMP:
820             TRACE("r");
821             break;
822
823         case WINED3DSPR_INPUT:
824             TRACE("v");
825             break;
826
827         case WINED3DSPR_CONST:
828         case WINED3DSPR_CONST2:
829         case WINED3DSPR_CONST3:
830         case WINED3DSPR_CONST4:
831             TRACE("c");
832             offset = shader_get_float_offset(reg->type, reg->idx);
833             break;
834
835         case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
836             TRACE("%c", shader_is_pshader_version(shader_version->type) ? 't' : 'a');
837             break;
838
839         case WINED3DSPR_RASTOUT:
840             TRACE("%s", rastout_reg_names[reg->idx]);
841             break;
842
843         case WINED3DSPR_COLOROUT:
844             TRACE("oC");
845             break;
846
847         case WINED3DSPR_DEPTHOUT:
848             TRACE("oDepth");
849             break;
850
851         case WINED3DSPR_ATTROUT:
852             TRACE("oD");
853             break;
854
855         case WINED3DSPR_TEXCRDOUT:
856             /* Vertex shaders >= 3.0 use general purpose output registers
857              * (WINED3DSPR_OUTPUT), which can include an address token */
858             if (shader_version->major >= 3) TRACE("o");
859             else TRACE("oT");
860             break;
861
862         case WINED3DSPR_CONSTINT:
863             TRACE("i");
864             break;
865
866         case WINED3DSPR_CONSTBOOL:
867             TRACE("b");
868             break;
869
870         case WINED3DSPR_LABEL:
871             TRACE("l");
872             break;
873
874         case WINED3DSPR_LOOP:
875             TRACE("aL");
876             break;
877
878         case WINED3DSPR_SAMPLER:
879             TRACE("s");
880             break;
881
882         case WINED3DSPR_MISCTYPE:
883             if (reg->idx > 1) FIXME("Unhandled misctype register %d\n", reg->idx);
884             else TRACE("%s", misctype_reg_names[reg->idx]);
885             break;
886
887         case WINED3DSPR_PREDICATE:
888             TRACE("p");
889             break;
890
891         case WINED3DSPR_IMMCONST:
892             TRACE("l");
893             break;
894
895         default:
896             TRACE("unhandled_rtype(%#x)", reg->type);
897             break;
898     }
899
900     if (reg->type == WINED3DSPR_IMMCONST)
901     {
902         TRACE("(");
903         switch (reg->immconst_type)
904         {
905             case WINED3D_IMMCONST_FLOAT:
906                 TRACE("%.8e", *(float *)reg->immconst_data);
907                 break;
908
909             case WINED3D_IMMCONST_FLOAT4:
910                 TRACE("%.8e, %.8e, %.8e, %.8e",
911                         *(float *)&reg->immconst_data[0], *(float *)&reg->immconst_data[1],
912                         *(float *)&reg->immconst_data[2], *(float *)&reg->immconst_data[3]);
913                 break;
914
915             default:
916                 TRACE("<unhandled immconst_type %#x>", reg->immconst_type);
917                 break;
918         }
919         TRACE(")");
920     }
921     else if (reg->type != WINED3DSPR_RASTOUT && reg->type != WINED3DSPR_MISCTYPE)
922     {
923         if (reg->rel_addr)
924         {
925             TRACE("[");
926             shader_dump_src_param(reg->rel_addr, shader_version);
927             TRACE(" + ");
928         }
929         TRACE("%u", offset);
930         if (reg->rel_addr) TRACE("]");
931     }
932 }
933
934 void shader_dump_dst_param(const struct wined3d_shader_dst_param *param,
935         const struct wined3d_shader_version *shader_version)
936 {
937     DWORD write_mask = param->write_mask;
938
939     shader_dump_register(&param->reg, shader_version);
940
941     if (write_mask != WINED3DSP_WRITEMASK_ALL)
942     {
943         static const char *write_mask_chars = "xyzw";
944
945         TRACE(".");
946         if (write_mask & WINED3DSP_WRITEMASK_0) TRACE("%c", write_mask_chars[0]);
947         if (write_mask & WINED3DSP_WRITEMASK_1) TRACE("%c", write_mask_chars[1]);
948         if (write_mask & WINED3DSP_WRITEMASK_2) TRACE("%c", write_mask_chars[2]);
949         if (write_mask & WINED3DSP_WRITEMASK_3) TRACE("%c", write_mask_chars[3]);
950     }
951 }
952
953 void shader_dump_src_param(const struct wined3d_shader_src_param *param,
954         const struct wined3d_shader_version *shader_version)
955 {
956     DWORD src_modifier = param->modifiers;
957     DWORD swizzle = param->swizzle;
958
959     if (src_modifier == WINED3DSPSM_NEG
960             || src_modifier == WINED3DSPSM_BIASNEG
961             || src_modifier == WINED3DSPSM_SIGNNEG
962             || src_modifier == WINED3DSPSM_X2NEG
963             || src_modifier == WINED3DSPSM_ABSNEG)
964         TRACE("-");
965     else if (src_modifier == WINED3DSPSM_COMP)
966         TRACE("1-");
967     else if (src_modifier == WINED3DSPSM_NOT)
968         TRACE("!");
969
970     if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
971         TRACE("abs(");
972
973     shader_dump_register(&param->reg, shader_version);
974
975     if (src_modifier)
976     {
977         switch (src_modifier)
978         {
979             case WINED3DSPSM_NONE:    break;
980             case WINED3DSPSM_NEG:     break;
981             case WINED3DSPSM_NOT:     break;
982             case WINED3DSPSM_BIAS:    TRACE("_bias"); break;
983             case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
984             case WINED3DSPSM_SIGN:    TRACE("_bx2"); break;
985             case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
986             case WINED3DSPSM_COMP:    break;
987             case WINED3DSPSM_X2:      TRACE("_x2"); break;
988             case WINED3DSPSM_X2NEG:   TRACE("_x2"); break;
989             case WINED3DSPSM_DZ:      TRACE("_dz"); break;
990             case WINED3DSPSM_DW:      TRACE("_dw"); break;
991             case WINED3DSPSM_ABSNEG:  TRACE(")"); break;
992             case WINED3DSPSM_ABS:     TRACE(")"); break;
993             default:
994                                       TRACE("_unknown_modifier(%#x)", src_modifier);
995         }
996     }
997
998     if (swizzle != WINED3DSP_NOSWIZZLE)
999     {
1000         static const char *swizzle_chars = "xyzw";
1001         DWORD swizzle_x = swizzle & 0x03;
1002         DWORD swizzle_y = (swizzle >> 2) & 0x03;
1003         DWORD swizzle_z = (swizzle >> 4) & 0x03;
1004         DWORD swizzle_w = (swizzle >> 6) & 0x03;
1005
1006         if (swizzle_x == swizzle_y
1007                 && swizzle_x == swizzle_z
1008                 && swizzle_x == swizzle_w)
1009         {
1010             TRACE(".%c", swizzle_chars[swizzle_x]);
1011         }
1012         else
1013         {
1014             TRACE(".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
1015                     swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
1016         }
1017     }
1018 }
1019
1020 /* Shared code in order to generate the bulk of the shader string.
1021  * NOTE: A description of how to parse tokens can be found on msdn */
1022 void shader_generate_main(IWineD3DBaseShader *iface, SHADER_BUFFER *buffer,
1023         const shader_reg_maps *reg_maps, const DWORD *pFunction, void *backend_ctx)
1024 {
1025     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
1026     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device; /* To access shader backend callbacks */
1027     const struct wined3d_shader_frontend *fe = This->baseShader.frontend;
1028     void *fe_data = This->baseShader.frontend_data;
1029     struct wined3d_shader_src_param src_rel_addr[4];
1030     struct wined3d_shader_src_param src_param[4];
1031     struct wined3d_shader_version shader_version;
1032     struct wined3d_shader_src_param dst_rel_addr;
1033     struct wined3d_shader_dst_param dst_param;
1034     struct wined3d_shader_instruction ins;
1035     struct wined3d_shader_context ctx;
1036     const DWORD *pToken = pFunction;
1037     DWORD i;
1038
1039     /* Initialize current parsing state */
1040     ctx.shader = iface;
1041     ctx.reg_maps = reg_maps;
1042     ctx.buffer = buffer;
1043     ctx.backend_data = backend_ctx;
1044
1045     ins.ctx = &ctx;
1046     ins.dst = &dst_param;
1047     ins.src = src_param;
1048     This->baseShader.parse_state.current_row = 0;
1049
1050     fe->shader_read_header(fe_data, &pToken, &shader_version);
1051
1052     while (!fe->shader_is_end(fe_data, &pToken))
1053     {
1054         const char *comment;
1055         UINT param_size;
1056
1057         /* Skip comment tokens */
1058         fe->shader_read_comment(&pToken, &comment);
1059         if (comment) continue;
1060
1061         /* Read opcode */
1062         fe->shader_read_opcode(fe_data, &pToken, &ins, &param_size);
1063
1064         /* Unknown opcode and its parameters */
1065         if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1066         {
1067             TRACE("Skipping unrecognized instruction.\n");
1068             pToken += param_size;
1069             continue;
1070         }
1071
1072         /* Nothing to do */
1073         if (ins.handler_idx == WINED3DSIH_DCL
1074                 || ins.handler_idx == WINED3DSIH_NOP
1075                 || ins.handler_idx == WINED3DSIH_DEF
1076                 || ins.handler_idx == WINED3DSIH_DEFI
1077                 || ins.handler_idx == WINED3DSIH_DEFB
1078                 || ins.handler_idx == WINED3DSIH_PHASE
1079                 || ins.handler_idx == WINED3DSIH_RET)
1080         {
1081             pToken += param_size;
1082             continue;
1083         }
1084
1085         /* Destination token */
1086         if (ins.dst_count) fe->shader_read_dst_param(fe_data, &pToken, &dst_param, &dst_rel_addr);
1087
1088         /* Predication token */
1089         if (ins.predicate) ins.predicate = *pToken++;
1090
1091         /* Other source tokens */
1092         for (i = 0; i < ins.src_count; ++i)
1093         {
1094             fe->shader_read_src_param(fe_data, &pToken, &src_param[i], &src_rel_addr[i]);
1095         }
1096
1097         /* Call appropriate function for output target */
1098         device->shader_backend->shader_handle_instruction(&ins);
1099     }
1100 }
1101
1102 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param *dst)
1103 {
1104     DWORD mmask = dst->modifiers;
1105
1106     switch (dst->shift)
1107     {
1108         case 0: break;
1109         case 13: TRACE("_d8"); break;
1110         case 14: TRACE("_d4"); break;
1111         case 15: TRACE("_d2"); break;
1112         case 1: TRACE("_x2"); break;
1113         case 2: TRACE("_x4"); break;
1114         case 3: TRACE("_x8"); break;
1115         default: TRACE("_unhandled_shift(%d)", dst->shift); break;
1116     }
1117
1118     if (mmask & WINED3DSPDM_SATURATE)         TRACE("_sat");
1119     if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
1120     if (mmask & WINED3DSPDM_MSAMPCENTROID)    TRACE("_centroid");
1121
1122     mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
1123     if (mmask)
1124         FIXME("_unrecognized_modifier(%#x)", mmask);
1125 }
1126
1127 void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *pFunction)
1128 {
1129     struct wined3d_shader_version shader_version;
1130     const DWORD* pToken = pFunction;
1131     DWORD i;
1132
1133     TRACE("Parsing %p\n", pFunction);
1134
1135     fe->shader_read_header(fe_data, &pToken, &shader_version);
1136
1137     TRACE("%s_%u_%u\n", shader_is_pshader_version(shader_version.type) ? "ps": "vs",
1138             shader_version.major, shader_version.minor);
1139
1140     while (!fe->shader_is_end(fe_data, &pToken))
1141     {
1142         struct wined3d_shader_instruction ins;
1143         const char *comment;
1144         UINT param_size;
1145
1146         /* comment */
1147         fe->shader_read_comment(&pToken, &comment);
1148         if (comment)
1149         {
1150             TRACE("//%s\n", comment);
1151             continue;
1152         }
1153
1154         fe->shader_read_opcode(fe_data, &pToken, &ins, &param_size);
1155         if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1156         {
1157             TRACE("Skipping unrecognized instruction.\n");
1158             pToken += param_size;
1159             continue;
1160         }
1161
1162         if (ins.handler_idx == WINED3DSIH_DCL)
1163         {
1164             struct wined3d_shader_semantic semantic;
1165
1166             fe->shader_read_semantic(&pToken, &semantic);
1167
1168             shader_dump_decl_usage(&semantic, &shader_version);
1169             shader_dump_ins_modifiers(&semantic.reg);
1170             TRACE(" ");
1171             shader_dump_dst_param(&semantic.reg, &shader_version);
1172         }
1173         else if (ins.handler_idx == WINED3DSIH_DEF)
1174         {
1175             struct wined3d_shader_dst_param dst;
1176             struct wined3d_shader_src_param rel_addr;
1177
1178             fe->shader_read_dst_param(fe_data, &pToken, &dst, &rel_addr);
1179
1180             TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(dst.reg.type, dst.reg.idx),
1181                     *(const float *)(pToken),
1182                     *(const float *)(pToken + 1),
1183                     *(const float *)(pToken + 2),
1184                     *(const float *)(pToken + 3));
1185             pToken += 4;
1186         }
1187         else if (ins.handler_idx == WINED3DSIH_DEFI)
1188         {
1189             struct wined3d_shader_dst_param dst;
1190             struct wined3d_shader_src_param rel_addr;
1191
1192             fe->shader_read_dst_param(fe_data, &pToken, &dst, &rel_addr);
1193
1194             TRACE("defi i%u = %d, %d, %d, %d", dst.reg.idx,
1195                     *(pToken),
1196                     *(pToken + 1),
1197                     *(pToken + 2),
1198                     *(pToken + 3));
1199             pToken += 4;
1200         }
1201         else if (ins.handler_idx == WINED3DSIH_DEFB)
1202         {
1203             struct wined3d_shader_dst_param dst;
1204             struct wined3d_shader_src_param rel_addr;
1205
1206             fe->shader_read_dst_param(fe_data, &pToken, &dst, &rel_addr);
1207
1208             TRACE("defb b%u = %s", dst.reg.idx, *pToken ? "true" : "false");
1209             ++pToken;
1210         }
1211         else
1212         {
1213             struct wined3d_shader_src_param dst_rel_addr, src_rel_addr;
1214             struct wined3d_shader_dst_param dst_param;
1215             struct wined3d_shader_src_param src_param;
1216
1217             if (ins.dst_count)
1218             {
1219                 fe->shader_read_dst_param(fe_data, &pToken, &dst_param, &dst_rel_addr);
1220             }
1221
1222             /* Print out predication source token first - it follows
1223              * the destination token. */
1224             if (ins.predicate)
1225             {
1226                 fe->shader_read_src_param(fe_data, &pToken, &src_param, &src_rel_addr);
1227                 TRACE("(");
1228                 shader_dump_src_param(&src_param, &shader_version);
1229                 TRACE(") ");
1230             }
1231
1232             /* PixWin marks instructions with the coissue flag with a '+' */
1233             if (ins.coissue) TRACE("+");
1234
1235             TRACE("%s", shader_opcode_names[ins.handler_idx]);
1236
1237             if (ins.handler_idx == WINED3DSIH_IFC
1238                     || ins.handler_idx == WINED3DSIH_BREAKC)
1239             {
1240                 switch (ins.flags)
1241                 {
1242                     case COMPARISON_GT: TRACE("_gt"); break;
1243                     case COMPARISON_EQ: TRACE("_eq"); break;
1244                     case COMPARISON_GE: TRACE("_ge"); break;
1245                     case COMPARISON_LT: TRACE("_lt"); break;
1246                     case COMPARISON_NE: TRACE("_ne"); break;
1247                     case COMPARISON_LE: TRACE("_le"); break;
1248                     default: TRACE("_(%u)", ins.flags);
1249                 }
1250             }
1251             else if (ins.handler_idx == WINED3DSIH_TEX
1252                     && shader_version.major >= 2
1253                     && (ins.flags & WINED3DSI_TEXLD_PROJECT))
1254             {
1255                 TRACE("p");
1256             }
1257
1258             /* We already read the destination token, print it. */
1259             if (ins.dst_count)
1260             {
1261                 shader_dump_ins_modifiers(&dst_param);
1262                 TRACE(" ");
1263                 shader_dump_dst_param(&dst_param, &shader_version);
1264             }
1265
1266             /* Other source tokens */
1267             for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
1268             {
1269                 fe->shader_read_src_param(fe_data, &pToken, &src_param, &src_rel_addr);
1270                 TRACE(!i ? " " : ", ");
1271                 shader_dump_src_param(&src_param, &shader_version);
1272             }
1273         }
1274         TRACE("\n");
1275     }
1276 }
1277
1278 void shader_cleanup(IWineD3DBaseShader *iface)
1279 {
1280     IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1281
1282     ((IWineD3DDeviceImpl *)This->baseShader.device)->shader_backend->shader_destroy(iface);
1283     HeapFree(GetProcessHeap(), 0, This->baseShader.reg_maps.constf);
1284     HeapFree(GetProcessHeap(), 0, This->baseShader.function);
1285     shader_delete_constant_list(&This->baseShader.constantsF);
1286     shader_delete_constant_list(&This->baseShader.constantsB);
1287     shader_delete_constant_list(&This->baseShader.constantsI);
1288     list_remove(&This->baseShader.shader_list_entry);
1289
1290     if (This->baseShader.frontend && This->baseShader.frontend_data)
1291     {
1292         This->baseShader.frontend->shader_free(This->baseShader.frontend_data);
1293     }
1294 }
1295
1296 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
1297 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
1298 static void shader_none_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {}
1299 static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1300 static void shader_none_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1301 static void shader_none_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1302 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1303 static void shader_none_load_np2fixup_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1304 static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1305 static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1306 static void shader_none_free(IWineD3DDevice *iface) {}
1307 static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1308
1309 #define GLINFO_LOCATION      (*gl_info)
1310 static void shader_none_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
1311 {
1312     /* Set the shader caps to 0 for the none shader backend */
1313     pCaps->VertexShaderVersion  = 0;
1314     pCaps->PixelShaderVersion    = 0;
1315     pCaps->PixelShader1xMaxValue = 0.0;
1316 }
1317 #undef GLINFO_LOCATION
1318 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1319 {
1320     if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1321     {
1322         TRACE("Checking support for fixup:\n");
1323         dump_color_fixup_desc(fixup);
1324     }
1325
1326     /* Faked to make some apps happy. */
1327     if (!is_yuv_fixup(fixup))
1328     {
1329         TRACE("[OK]\n");
1330         return TRUE;
1331     }
1332
1333     TRACE("[FAILED]\n");
1334     return FALSE;
1335 }
1336
1337 const shader_backend_t none_shader_backend = {
1338     shader_none_handle_instruction,
1339     shader_none_select,
1340     shader_none_select_depth_blt,
1341     shader_none_deselect_depth_blt,
1342     shader_none_update_float_vertex_constants,
1343     shader_none_update_float_pixel_constants,
1344     shader_none_load_constants,
1345     shader_none_load_np2fixup_constants,
1346     shader_none_destroy,
1347     shader_none_alloc,
1348     shader_none_free,
1349     shader_none_dirty_const,
1350     shader_none_get_caps,
1351     shader_none_color_fixup_supported,
1352 };