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