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