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