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