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