wined3d: Catch RCP 0.0 in ARB.
[wine] / dlls / wined3d / shader.c
1 /*
2  * Copyright 2002-2003 Jason Edmeades
3  * Copyright 2002-2003 Raphael Junqueira
4  * Copyright 2004 Christian Costa
5  * Copyright 2005 Oliver Stieber
6  * Copyright 2006 Ivan Gyurdiev
7  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8  * Copyright 2009 Henri Verbeet for CodeWeavers
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26
27 #include <math.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "wined3d_private.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
34 WINE_DECLARE_DEBUG_CHANNEL(d3d);
35
36 static const char *shader_opcode_names[] =
37 {
38     /* WINED3DSIH_ABS           */ "abs",
39     /* WINED3DSIH_ADD           */ "add",
40     /* WINED3DSIH_BEM           */ "bem",
41     /* WINED3DSIH_BREAK         */ "break",
42     /* WINED3DSIH_BREAKC        */ "breakc",
43     /* WINED3DSIH_BREAKP        */ "breakp",
44     /* WINED3DSIH_CALL          */ "call",
45     /* WINED3DSIH_CALLNZ        */ "callnz",
46     /* WINED3DSIH_CMP           */ "cmp",
47     /* WINED3DSIH_CND           */ "cnd",
48     /* WINED3DSIH_CRS           */ "crs",
49     /* WINED3DSIH_CUT           */ "cut",
50     /* WINED3DSIH_DCL           */ "dcl",
51     /* WINED3DSIH_DEF           */ "def",
52     /* WINED3DSIH_DEFB          */ "defb",
53     /* WINED3DSIH_DEFI          */ "defi",
54     /* WINED3DSIH_DP2ADD        */ "dp2add",
55     /* WINED3DSIH_DP3           */ "dp3",
56     /* WINED3DSIH_DP4           */ "dp4",
57     /* WINED3DSIH_DST           */ "dst",
58     /* WINED3DSIH_DSX           */ "dsx",
59     /* WINED3DSIH_DSY           */ "dsy",
60     /* WINED3DSIH_ELSE          */ "else",
61     /* WINED3DSIH_EMIT          */ "emit",
62     /* WINED3DSIH_ENDIF         */ "endif",
63     /* WINED3DSIH_ENDLOOP       */ "endloop",
64     /* WINED3DSIH_ENDREP        */ "endrep",
65     /* WINED3DSIH_EXP           */ "exp",
66     /* WINED3DSIH_EXPP          */ "expp",
67     /* WINED3DSIH_FRC           */ "frc",
68     /* WINED3DSIH_IADD          */ "iadd",
69     /* WINED3DSIH_IF            */ "if",
70     /* WINED3DSIH_IFC           */ "ifc",
71     /* WINED3DSIH_IGE           */ "ige",
72     /* WINED3DSIH_LABEL         */ "label",
73     /* WINED3DSIH_LIT           */ "lit",
74     /* WINED3DSIH_LOG           */ "log",
75     /* WINED3DSIH_LOGP          */ "logp",
76     /* WINED3DSIH_LOOP          */ "loop",
77     /* WINED3DSIH_LRP           */ "lrp",
78     /* WINED3DSIH_LT            */ "lt",
79     /* WINED3DSIH_M3x2          */ "m3x2",
80     /* WINED3DSIH_M3x3          */ "m3x3",
81     /* WINED3DSIH_M3x4          */ "m3x4",
82     /* WINED3DSIH_M4x3          */ "m4x3",
83     /* WINED3DSIH_M4x4          */ "m4x4",
84     /* WINED3DSIH_MAD           */ "mad",
85     /* WINED3DSIH_MAX           */ "max",
86     /* WINED3DSIH_MIN           */ "min",
87     /* WINED3DSIH_MOV           */ "mov",
88     /* WINED3DSIH_MOVA          */ "mova",
89     /* WINED3DSIH_MUL           */ "mul",
90     /* WINED3DSIH_NOP           */ "nop",
91     /* WINED3DSIH_NRM           */ "nrm",
92     /* WINED3DSIH_PHASE         */ "phase",
93     /* WINED3DSIH_POW           */ "pow",
94     /* WINED3DSIH_RCP           */ "rcp",
95     /* WINED3DSIH_REP           */ "rep",
96     /* WINED3DSIH_RET           */ "ret",
97     /* WINED3DSIH_RSQ           */ "rsq",
98     /* WINED3DSIH_SETP          */ "setp",
99     /* WINED3DSIH_SGE           */ "sge",
100     /* WINED3DSIH_SGN           */ "sgn",
101     /* WINED3DSIH_SINCOS        */ "sincos",
102     /* WINED3DSIH_SLT           */ "slt",
103     /* WINED3DSIH_SUB           */ "sub",
104     /* WINED3DSIH_TEX           */ "texld",
105     /* WINED3DSIH_TEXBEM        */ "texbem",
106     /* WINED3DSIH_TEXBEML       */ "texbeml",
107     /* WINED3DSIH_TEXCOORD      */ "texcrd",
108     /* WINED3DSIH_TEXDEPTH      */ "texdepth",
109     /* WINED3DSIH_TEXDP3        */ "texdp3",
110     /* WINED3DSIH_TEXDP3TEX     */ "texdp3tex",
111     /* WINED3DSIH_TEXKILL       */ "texkill",
112     /* WINED3DSIH_TEXLDD        */ "texldd",
113     /* WINED3DSIH_TEXLDL        */ "texldl",
114     /* WINED3DSIH_TEXM3x2DEPTH  */ "texm3x2depth",
115     /* WINED3DSIH_TEXM3x2PAD    */ "texm3x2pad",
116     /* WINED3DSIH_TEXM3x2TEX    */ "texm3x2tex",
117     /* WINED3DSIH_TEXM3x3       */ "texm3x3",
118     /* WINED3DSIH_TEXM3x3DIFF   */ "texm3x3diff",
119     /* WINED3DSIH_TEXM3x3PAD    */ "texm3x3pad",
120     /* WINED3DSIH_TEXM3x3SPEC   */ "texm3x3spec",
121     /* WINED3DSIH_TEXM3x3TEX    */ "texm3x3tex",
122     /* WINED3DSIH_TEXM3x3VSPEC  */ "texm3x3vspec",
123     /* WINED3DSIH_TEXREG2AR     */ "texreg2ar",
124     /* WINED3DSIH_TEXREG2GB     */ "texreg2gb",
125     /* WINED3DSIH_TEXREG2RGB    */ "texreg2rgb",
126 };
127
128 static const char *semantic_names[] =
129 {
130     /* WINED3DDECLUSAGE_POSITION        */ "SV_POSITION",
131     /* WINED3DDECLUSAGE_BLENDWEIGHT     */ "BLENDWEIGHT",
132     /* WINED3DDECLUSAGE_BLENDINDICES    */ "BLENDINDICES",
133     /* WINED3DDECLUSAGE_NORMAL          */ "NORMAL",
134     /* WINED3DDECLUSAGE_PSIZE           */ "PSIZE",
135     /* WINED3DDECLUSAGE_TEXCOORD        */ "TEXCOORD",
136     /* WINED3DDECLUSAGE_TANGENT         */ "TANGENT",
137     /* WINED3DDECLUSAGE_BINORMAL        */ "BINORMAL",
138     /* WINED3DDECLUSAGE_TESSFACTOR      */ "TESSFACTOR",
139     /* WINED3DDECLUSAGE_POSITIONT       */ "POSITIONT",
140     /* WINED3DDECLUSAGE_COLOR           */ "COLOR",
141     /* WINED3DDECLUSAGE_FOG             */ "FOG",
142     /* WINED3DDECLUSAGE_DEPTH           */ "DEPTH",
143     /* WINED3DDECLUSAGE_SAMPLE          */ "SAMPLE",
144 };
145
146 static const char *shader_semantic_name_from_usage(WINED3DDECLUSAGE usage)
147 {
148     if (usage >= sizeof(semantic_names) / sizeof(*semantic_names))
149     {
150         FIXME("Unrecognized usage %#x.\n", usage);
151         return "UNRECOGNIZED";
152     }
153
154     return semantic_names[usage];
155 }
156
157 static WINED3DDECLUSAGE shader_usage_from_semantic_name(const char *name)
158 {
159     unsigned int i;
160
161     for (i = 0; i < sizeof(semantic_names) / sizeof(*semantic_names); ++i)
162     {
163         if (!strcmp(name, semantic_names[i])) return i;
164     }
165
166     return ~0U;
167 }
168
169 BOOL shader_match_semantic(const char *semantic_name, WINED3DDECLUSAGE usage)
170 {
171     return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
172 }
173
174 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
175         const struct wined3d_shader_semantic *s)
176 {
177     e->semantic_name = shader_semantic_name_from_usage(s->usage);
178     e->semantic_idx = s->usage_idx;
179     e->sysval_semantic = 0;
180     e->component_type = 0;
181     e->register_idx = s->reg.reg.idx;
182     e->mask = s->reg.write_mask;
183 }
184
185 static const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token)
186 {
187     switch (version_token >> 16)
188     {
189         case WINED3D_SM1_VS:
190         case WINED3D_SM1_PS:
191             return &sm1_shader_frontend;
192
193         case WINED3D_SM4_PS:
194         case WINED3D_SM4_VS:
195         case WINED3D_SM4_GS:
196             return &sm4_shader_frontend;
197
198         default:
199             FIXME("Unrecognised version token %#x\n", version_token);
200             return NULL;
201     }
202 }
203
204 void shader_buffer_clear(struct wined3d_shader_buffer *buffer)
205 {
206     buffer->buffer[0] = '\0';
207     buffer->bsize = 0;
208     buffer->lineNo = 0;
209     buffer->newline = TRUE;
210 }
211
212 BOOL shader_buffer_init(struct wined3d_shader_buffer *buffer)
213 {
214     buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
215     if (!buffer->buffer)
216     {
217         ERR("Failed to allocate shader buffer memory.\n");
218         return FALSE;
219     }
220
221     shader_buffer_clear(buffer);
222     return TRUE;
223 }
224
225 void shader_buffer_free(struct wined3d_shader_buffer *buffer)
226 {
227     HeapFree(GetProcessHeap(), 0, buffer->buffer);
228 }
229
230 int shader_vaddline(struct wined3d_shader_buffer *buffer, const char *format, va_list args)
231 {
232     char *base = buffer->buffer + buffer->bsize;
233     int rc;
234
235     rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
236
237     if (rc < 0 /* C89 */ || (unsigned int)rc > SHADER_PGMSIZE - 1 - buffer->bsize /* C99 */)
238     {
239         ERR("The buffer allocated for the shader program string "
240             "is too small at %d bytes.\n", SHADER_PGMSIZE);
241         buffer->bsize = SHADER_PGMSIZE - 1;
242         return -1;
243     }
244
245     if (buffer->newline)
246     {
247         TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
248         buffer->newline = FALSE;
249     }
250     else
251     {
252         TRACE("%s", base);
253     }
254
255     buffer->bsize += rc;
256     if (buffer->buffer[buffer->bsize-1] == '\n')
257     {
258         ++buffer->lineNo;
259         buffer->newline = TRUE;
260     }
261
262     return 0;
263 }
264
265 int shader_addline(struct wined3d_shader_buffer *buffer, const char *format, ...)
266 {
267     va_list args;
268     int ret;
269
270     va_start(args, format);
271     ret = shader_vaddline(buffer, format, args);
272     va_end(args);
273
274     return ret;
275 }
276
277 static void shader_init(struct IWineD3DBaseShaderClass *shader, IWineD3DDeviceImpl *device,
278         IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
279 {
280     shader->ref = 1;
281     shader->device = (IWineD3DDevice *)device;
282     shader->parent = parent;
283     shader->parent_ops = parent_ops;
284     list_init(&shader->linked_programs);
285     list_add_head(&device->shaders, &shader->shader_list_entry);
286 }
287
288 /* Convert floating point offset relative to a register file to an absolute
289  * offset for float constants. */
290 static unsigned int shader_get_float_offset(WINED3DSHADER_PARAM_REGISTER_TYPE register_type, UINT register_idx)
291 {
292     switch (register_type)
293     {
294         case WINED3DSPR_CONST: return register_idx;
295         case WINED3DSPR_CONST2: return 2048 + register_idx;
296         case WINED3DSPR_CONST3: return 4096 + register_idx;
297         case WINED3DSPR_CONST4: return 6144 + register_idx;
298         default:
299             FIXME("Unsupported register type: %u.\n", register_type);
300             return register_idx;
301     }
302 }
303
304 static void shader_delete_constant_list(struct list *clist)
305 {
306     struct local_constant *constant;
307     struct list *ptr;
308
309     ptr = list_head(clist);
310     while (ptr)
311     {
312         constant = LIST_ENTRY(ptr, struct local_constant, entry);
313         ptr = list_next(clist, ptr);
314         HeapFree(GetProcessHeap(), 0, constant);
315     }
316     list_init(clist);
317 }
318
319 static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
320 {
321     DWORD idx, shift;
322     idx = bit >> 5;
323     shift = bit & 0x1f;
324     bitmap[idx] |= (1 << shift);
325 }
326
327 static void shader_record_register_usage(IWineD3DBaseShaderImpl *shader, struct shader_reg_maps *reg_maps,
328         const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type)
329 {
330     switch (reg->type)
331     {
332         case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
333             if (shader_type == WINED3D_SHADER_TYPE_PIXEL) reg_maps->texcoord |= 1 << reg->idx;
334             else reg_maps->address |= 1 << reg->idx;
335             break;
336
337         case WINED3DSPR_TEMP:
338             reg_maps->temporary |= 1 << reg->idx;
339             break;
340
341         case WINED3DSPR_INPUT:
342             if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
343             {
344                 if (reg->rel_addr)
345                 {
346                     /* If relative addressing is used, we must assume that all registers
347                      * are used. Even if it is a construct like v3[aL], we can't assume
348                      * that v0, v1 and v2 aren't read because aL can be negative */
349                     unsigned int i;
350                     for (i = 0; i < MAX_REG_INPUT; ++i)
351                     {
352                         ((IWineD3DPixelShaderImpl *)shader)->input_reg_used[i] = TRUE;
353                     }
354                 }
355                 else
356                 {
357                     ((IWineD3DPixelShaderImpl *)shader)->input_reg_used[reg->idx] = TRUE;
358                 }
359             }
360             else reg_maps->input_registers |= 1 << reg->idx;
361             break;
362
363         case WINED3DSPR_RASTOUT:
364             if (reg->idx == 1) reg_maps->fog = 1;
365             break;
366
367         case WINED3DSPR_MISCTYPE:
368             if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
369             {
370                 if (reg->idx == 0) reg_maps->vpos = 1;
371                 else if (reg->idx == 1) reg_maps->usesfacing = 1;
372             }
373             break;
374
375         case WINED3DSPR_CONST:
376             if (reg->rel_addr)
377             {
378                 if (shader_type != WINED3D_SHADER_TYPE_PIXEL)
379                 {
380                     if (reg->idx < ((IWineD3DVertexShaderImpl *)shader)->min_rel_offset)
381                     {
382                         ((IWineD3DVertexShaderImpl *)shader)->min_rel_offset = reg->idx;
383                     }
384                     if (reg->idx > ((IWineD3DVertexShaderImpl *)shader)->max_rel_offset)
385                     {
386                         ((IWineD3DVertexShaderImpl *)shader)->max_rel_offset = reg->idx;
387                     }
388                 }
389                 reg_maps->usesrelconstF = TRUE;
390             }
391             else
392             {
393                 set_bitmap_bit(reg_maps->constf, reg->idx);
394             }
395             break;
396
397         case WINED3DSPR_CONSTINT:
398             reg_maps->integer_constants |= (1 << reg->idx);
399             break;
400
401         case WINED3DSPR_CONSTBOOL:
402             reg_maps->boolean_constants |= (1 << reg->idx);
403             break;
404
405         case WINED3DSPR_COLOROUT:
406             reg_maps->highest_render_target = max(reg_maps->highest_render_target, reg->idx);
407             break;
408
409         default:
410             TRACE("Not recording register of type %#x and idx %u\n", reg->type, reg->idx);
411             break;
412     }
413 }
414
415 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
416 {
417     switch (instr)
418     {
419         case WINED3DSIH_M4x4:
420         case WINED3DSIH_M3x4:
421             return param == 1 ? 3 : 0;
422
423         case WINED3DSIH_M4x3:
424         case WINED3DSIH_M3x3:
425             return param == 1 ? 2 : 0;
426
427         case WINED3DSIH_M3x2:
428             return param == 1 ? 1 : 0;
429
430         default:
431             return 0;
432     }
433 }
434
435 /* Note that this does not count the loop register as an address register. */
436 static HRESULT shader_get_registers_used(IWineD3DBaseShader *iface, const struct wined3d_shader_frontend *fe,
437         struct shader_reg_maps *reg_maps, struct wined3d_shader_signature_element *input_signature,
438         struct wined3d_shader_signature_element *output_signature, const DWORD *byte_code, DWORD constf_size)
439 {
440     IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
441     unsigned int cur_loop_depth = 0, max_loop_depth = 0;
442     void *fe_data = shader->baseShader.frontend_data;
443     struct wined3d_shader_version shader_version;
444     const DWORD *ptr = byte_code;
445
446     memset(reg_maps, 0, sizeof(*reg_maps));
447
448     /* get_registers_used() is called on every compile on some 1.x shaders,
449      * which can result in stacking up a collection of local constants.
450      * Delete the old constants if existing. */
451     shader_delete_constant_list(&shader->baseShader.constantsF);
452     shader_delete_constant_list(&shader->baseShader.constantsB);
453     shader_delete_constant_list(&shader->baseShader.constantsI);
454
455     fe->shader_read_header(fe_data, &ptr, &shader_version);
456     reg_maps->shader_version = shader_version;
457
458     reg_maps->constf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
459             sizeof(*reg_maps->constf) * ((constf_size + 31) / 32));
460     if (!reg_maps->constf)
461     {
462         ERR("Failed to allocate constant map memory.\n");
463         return E_OUTOFMEMORY;
464     }
465
466     while (!fe->shader_is_end(fe_data, &ptr))
467     {
468         struct wined3d_shader_instruction ins;
469         const char *comment;
470         UINT comment_size;
471         UINT param_size;
472
473         /* Skip comments. */
474         fe->shader_read_comment(&ptr, &comment, &comment_size);
475         if (comment) continue;
476
477         /* Fetch opcode. */
478         fe->shader_read_opcode(fe_data, &ptr, &ins, &param_size);
479
480         /* Unhandled opcode, and its parameters. */
481         if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
482         {
483             TRACE("Skipping unrecognized instruction.\n");
484             ptr += param_size;
485             continue;
486         }
487
488         /* Handle declarations. */
489         if (ins.handler_idx == WINED3DSIH_DCL)
490         {
491             struct wined3d_shader_semantic semantic;
492
493             fe->shader_read_semantic(&ptr, &semantic);
494
495             switch (semantic.reg.reg.type)
496             {
497                 /* Mark input registers used. */
498                 case WINED3DSPR_INPUT:
499                     reg_maps->input_registers |= 1 << semantic.reg.reg.idx;
500                     shader_signature_from_semantic(&input_signature[semantic.reg.reg.idx], &semantic);
501                     break;
502
503                 /* Vertex shader: mark 3.0 output registers used, save token. */
504                 case WINED3DSPR_OUTPUT:
505                     reg_maps->output_registers |= 1 << semantic.reg.reg.idx;
506                     shader_signature_from_semantic(&output_signature[semantic.reg.reg.idx], &semantic);
507                     if (semantic.usage == WINED3DDECLUSAGE_FOG) reg_maps->fog = 1;
508                     break;
509
510                 /* Save sampler usage token. */
511                 case WINED3DSPR_SAMPLER:
512                     reg_maps->sampler_type[semantic.reg.reg.idx] = semantic.sampler_type;
513                     break;
514
515                 default:
516                     TRACE("Not recording DCL register type %#x.\n", semantic.reg.reg.type);
517                     break;
518             }
519         }
520         else if (ins.handler_idx == WINED3DSIH_DEF)
521         {
522             struct wined3d_shader_src_param rel_addr;
523             struct wined3d_shader_dst_param dst;
524
525             local_constant *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
526             if (!lconst) return E_OUTOFMEMORY;
527
528             fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
529             lconst->idx = dst.reg.idx;
530
531             memcpy(lconst->value, ptr, 4 * sizeof(DWORD));
532             ptr += 4;
533
534             /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
535             if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
536             {
537                 float *value = (float *)lconst->value;
538                 if (value[0] < -1.0f) value[0] = -1.0f;
539                 else if (value[0] > 1.0f) value[0] = 1.0f;
540                 if (value[1] < -1.0f) value[1] = -1.0f;
541                 else if (value[1] > 1.0f) value[1] = 1.0f;
542                 if (value[2] < -1.0f) value[2] = -1.0f;
543                 else if (value[2] > 1.0f) value[2] = 1.0f;
544                 if (value[3] < -1.0f) value[3] = -1.0f;
545                 else if (value[3] > 1.0f) value[3] = 1.0f;
546             }
547
548             list_add_head(&shader->baseShader.constantsF, &lconst->entry);
549         }
550         else if (ins.handler_idx == WINED3DSIH_DEFI)
551         {
552             struct wined3d_shader_src_param rel_addr;
553             struct wined3d_shader_dst_param dst;
554
555             local_constant *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
556             if (!lconst) return E_OUTOFMEMORY;
557
558             fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
559             lconst->idx = dst.reg.idx;
560
561             memcpy(lconst->value, ptr, 4 * sizeof(DWORD));
562             ptr += 4;
563
564             list_add_head(&shader->baseShader.constantsI, &lconst->entry);
565             reg_maps->local_int_consts |= (1 << dst.reg.idx);
566         }
567         else if (ins.handler_idx == WINED3DSIH_DEFB)
568         {
569             struct wined3d_shader_src_param rel_addr;
570             struct wined3d_shader_dst_param dst;
571
572             local_constant *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
573             if (!lconst) return E_OUTOFMEMORY;
574
575             fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
576             lconst->idx = dst.reg.idx;
577
578             memcpy(lconst->value, ptr, sizeof(DWORD));
579             ++ptr;
580
581             list_add_head(&shader->baseShader.constantsB, &lconst->entry);
582             reg_maps->local_bool_consts |= (1 << dst.reg.idx);
583         }
584         /* If there's a loop in the shader. */
585         else if (ins.handler_idx == WINED3DSIH_LOOP
586                 || ins.handler_idx == WINED3DSIH_REP)
587         {
588             struct wined3d_shader_src_param src, rel_addr;
589
590             fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
591
592             /* Rep and Loop always use an integer constant for the control parameters. */
593             if (ins.handler_idx == WINED3DSIH_REP)
594             {
595                 reg_maps->integer_constants |= 1 << src.reg.idx;
596             }
597             else
598             {
599                 fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
600                 reg_maps->integer_constants |= 1 << src.reg.idx;
601             }
602
603             cur_loop_depth++;
604             if (cur_loop_depth > max_loop_depth) max_loop_depth = cur_loop_depth;
605         }
606         else if (ins.handler_idx == WINED3DSIH_ENDLOOP
607                 || ins.handler_idx == WINED3DSIH_ENDREP)
608         {
609             cur_loop_depth--;
610         }
611         /* For subroutine prototypes. */
612         else if (ins.handler_idx == WINED3DSIH_LABEL)
613         {
614             struct wined3d_shader_src_param src, rel_addr;
615
616             fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
617             reg_maps->labels |= 1 << src.reg.idx;
618         }
619         /* Set texture, address, temporary registers. */
620         else
621         {
622             BOOL color0_mov = FALSE;
623             int i, limit;
624
625             /* This will loop over all the registers and try to
626              * make a bitmask of the ones we're interested in.
627              *
628              * Relative addressing tokens are ignored, but that's
629              * okay, since we'll catch any address registers when
630              * they are initialized (required by spec). */
631             if (ins.dst_count)
632             {
633                 struct wined3d_shader_src_param dst_rel_addr;
634                 struct wined3d_shader_dst_param dst_param;
635
636                 fe->shader_read_dst_param(fe_data, &ptr, &dst_param, &dst_rel_addr);
637
638                 shader_record_register_usage(shader, reg_maps, &dst_param.reg, shader_version.type);
639
640                 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
641                  * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
642                  * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
643                 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX && shader_version.major < 3
644                         && dst_param.reg.type == WINED3DSPR_TEXCRDOUT)
645                 {
646                     reg_maps->texcoord_mask[dst_param.reg.idx] |= dst_param.write_mask;
647                 }
648
649                 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
650                 {
651                     IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *)shader;
652
653                     if (dst_param.reg.type == WINED3DSPR_COLOROUT && dst_param.reg.idx == 0)
654                     {
655                     /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
656                      * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
657                      * the mov and perform the sRGB write correction from the source register.
658                      *
659                      * However, if the mov is only partial, we can't do this, and if the write
660                      * comes from an instruction other than MOV it is hard to do as well. If
661                      * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
662
663                         ps->color0_mov = FALSE;
664                         if (ins.handler_idx == WINED3DSIH_MOV)
665                         {
666                             /* Used later when the source register is read. */
667                             color0_mov = TRUE;
668                         }
669                     }
670                     /* Also drop the MOV marker if the source register is overwritten prior to the shader
671                      * end
672                      */
673                     else if (dst_param.reg.type == WINED3DSPR_TEMP && dst_param.reg.idx == ps->color0_reg)
674                     {
675                         ps->color0_mov = FALSE;
676                     }
677                 }
678
679                 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
680                 if (shader_version.major == 1
681                         && (ins.handler_idx == WINED3DSIH_TEX
682                             || ins.handler_idx == WINED3DSIH_TEXBEM
683                             || ins.handler_idx == WINED3DSIH_TEXBEML
684                             || ins.handler_idx == WINED3DSIH_TEXDP3TEX
685                             || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
686                             || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
687                             || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
688                             || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
689                             || ins.handler_idx == WINED3DSIH_TEXREG2AR
690                             || ins.handler_idx == WINED3DSIH_TEXREG2GB
691                             || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
692                 {
693                     /* Fake sampler usage, only set reserved bit and type. */
694                     DWORD sampler_code = dst_param.reg.idx;
695
696                     TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
697                     reg_maps->sampler_type[sampler_code] = WINED3DSTT_2D;
698
699                     /* texbem is only valid with < 1.4 pixel shaders */
700                     if (ins.handler_idx == WINED3DSIH_TEXBEM
701                             || ins.handler_idx == WINED3DSIH_TEXBEML)
702                     {
703                         reg_maps->bumpmat |= 1 << dst_param.reg.idx;
704                         if (ins.handler_idx == WINED3DSIH_TEXBEML)
705                         {
706                             reg_maps->luminanceparams |= 1 << dst_param.reg.idx;
707                         }
708                     }
709                 }
710                 else if (ins.handler_idx == WINED3DSIH_BEM)
711                 {
712                     reg_maps->bumpmat |= 1 << dst_param.reg.idx;
713                 }
714             }
715
716             if (ins.handler_idx == WINED3DSIH_NRM) reg_maps->usesnrm = 1;
717             else if (ins.handler_idx == WINED3DSIH_DSY) reg_maps->usesdsy = 1;
718             else if (ins.handler_idx == WINED3DSIH_DSX) reg_maps->usesdsx = 1;
719             else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
720             else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
721             else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
722             else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
723             else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
724             else if (ins.handler_idx == WINED3DSIH_RCP) reg_maps->usesrcp = 1;
725
726             limit = ins.src_count + (ins.predicate ? 1 : 0);
727             for (i = 0; i < limit; ++i)
728             {
729                 struct wined3d_shader_src_param src_param, src_rel_addr;
730                 unsigned int count;
731
732                 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
733                 count = get_instr_extra_regcount(ins.handler_idx, i);
734
735                 shader_record_register_usage(shader, reg_maps, &src_param.reg, shader_version.type);
736                 while (count)
737                 {
738                     ++src_param.reg.idx;
739                     shader_record_register_usage(shader, reg_maps, &src_param.reg, shader_version.type);
740                     --count;
741                 }
742
743                 if (color0_mov)
744                 {
745                     IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *)shader;
746                     if (src_param.reg.type == WINED3DSPR_TEMP
747                             && src_param.swizzle == WINED3DSP_NOSWIZZLE)
748                     {
749                         ps->color0_mov = TRUE;
750                         ps->color0_reg = src_param.reg.idx;
751                     }
752                 }
753             }
754         }
755     }
756     reg_maps->loop_depth = max_loop_depth;
757
758     shader->baseShader.functionLength = ((const char *)ptr - (const char *)byte_code);
759
760     return WINED3D_OK;
761 }
762
763 unsigned int shader_find_free_input_register(const struct shader_reg_maps *reg_maps, unsigned int max)
764 {
765     DWORD map = 1 << max;
766     map |= map - 1;
767     map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
768
769     return wined3d_log2i(map);
770 }
771
772 static void shader_dump_decl_usage(const struct wined3d_shader_semantic *semantic,
773         const struct wined3d_shader_version *shader_version)
774 {
775     TRACE("dcl");
776
777     if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
778     {
779         switch (semantic->sampler_type)
780         {
781             case WINED3DSTT_2D: TRACE("_2d"); break;
782             case WINED3DSTT_CUBE: TRACE("_cube"); break;
783             case WINED3DSTT_VOLUME: TRACE("_volume"); break;
784             default: TRACE("_unknown_ttype(0x%08x)", semantic->sampler_type);
785         }
786     }
787     else
788     {
789         /* Pixel shaders 3.0 don't have usage semantics. */
790         if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL) return;
791         else TRACE("_");
792
793         switch (semantic->usage)
794         {
795             case WINED3DDECLUSAGE_POSITION:
796                 TRACE("position%u", semantic->usage_idx);
797                 break;
798
799             case WINED3DDECLUSAGE_BLENDINDICES:
800                 TRACE("blend");
801                 break;
802
803             case WINED3DDECLUSAGE_BLENDWEIGHT:
804                 TRACE("weight");
805                 break;
806
807             case WINED3DDECLUSAGE_NORMAL:
808                 TRACE("normal%u", semantic->usage_idx);
809                 break;
810
811             case WINED3DDECLUSAGE_PSIZE:
812                 TRACE("psize");
813                 break;
814
815             case WINED3DDECLUSAGE_COLOR:
816                 if (semantic->usage_idx == 0) TRACE("color");
817                 else TRACE("specular%u", (semantic->usage_idx - 1));
818                 break;
819
820             case WINED3DDECLUSAGE_TEXCOORD:
821                 TRACE("texture%u", semantic->usage_idx);
822                 break;
823
824             case WINED3DDECLUSAGE_TANGENT:
825                 TRACE("tangent");
826                 break;
827
828             case WINED3DDECLUSAGE_BINORMAL:
829                 TRACE("binormal");
830                 break;
831
832             case WINED3DDECLUSAGE_TESSFACTOR:
833                 TRACE("tessfactor");
834                 break;
835
836             case WINED3DDECLUSAGE_POSITIONT:
837                 TRACE("positionT%u", semantic->usage_idx);
838                 break;
839
840             case WINED3DDECLUSAGE_FOG:
841                 TRACE("fog");
842                 break;
843
844             case WINED3DDECLUSAGE_DEPTH:
845                 TRACE("depth");
846                 break;
847
848             case WINED3DDECLUSAGE_SAMPLE:
849                 TRACE("sample");
850                 break;
851
852             default:
853                 FIXME("unknown_semantics(0x%08x)", semantic->usage);
854         }
855     }
856 }
857
858 static void shader_dump_register(const struct wined3d_shader_register *reg,
859         const struct wined3d_shader_version *shader_version)
860 {
861     static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
862     static const char * const misctype_reg_names[] = {"vPos", "vFace"};
863     UINT offset = reg->idx;
864
865     switch (reg->type)
866     {
867         case WINED3DSPR_TEMP:
868             TRACE("r");
869             break;
870
871         case WINED3DSPR_INPUT:
872             TRACE("v");
873             break;
874
875         case WINED3DSPR_CONST:
876         case WINED3DSPR_CONST2:
877         case WINED3DSPR_CONST3:
878         case WINED3DSPR_CONST4:
879             TRACE("c");
880             offset = shader_get_float_offset(reg->type, reg->idx);
881             break;
882
883         case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
884             TRACE("%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
885             break;
886
887         case WINED3DSPR_RASTOUT:
888             TRACE("%s", rastout_reg_names[reg->idx]);
889             break;
890
891         case WINED3DSPR_COLOROUT:
892             TRACE("oC");
893             break;
894
895         case WINED3DSPR_DEPTHOUT:
896             TRACE("oDepth");
897             break;
898
899         case WINED3DSPR_ATTROUT:
900             TRACE("oD");
901             break;
902
903         case WINED3DSPR_TEXCRDOUT:
904             /* Vertex shaders >= 3.0 use general purpose output registers
905              * (WINED3DSPR_OUTPUT), which can include an address token. */
906             if (shader_version->major >= 3) TRACE("o");
907             else TRACE("oT");
908             break;
909
910         case WINED3DSPR_CONSTINT:
911             TRACE("i");
912             break;
913
914         case WINED3DSPR_CONSTBOOL:
915             TRACE("b");
916             break;
917
918         case WINED3DSPR_LABEL:
919             TRACE("l");
920             break;
921
922         case WINED3DSPR_LOOP:
923             TRACE("aL");
924             break;
925
926         case WINED3DSPR_SAMPLER:
927             TRACE("s");
928             break;
929
930         case WINED3DSPR_MISCTYPE:
931             if (reg->idx > 1) FIXME("Unhandled misctype register %u.\n", reg->idx);
932             else TRACE("%s", misctype_reg_names[reg->idx]);
933             break;
934
935         case WINED3DSPR_PREDICATE:
936             TRACE("p");
937             break;
938
939         case WINED3DSPR_IMMCONST:
940             TRACE("l");
941             break;
942
943         case WINED3DSPR_CONSTBUFFER:
944             TRACE("cb");
945             break;
946
947         default:
948             TRACE("unhandled_rtype(%#x)", reg->type);
949             break;
950     }
951
952     if (reg->type == WINED3DSPR_IMMCONST)
953     {
954         TRACE("(");
955         switch (reg->immconst_type)
956         {
957             case WINED3D_IMMCONST_FLOAT:
958                 TRACE("%.8e", *(const float *)reg->immconst_data);
959                 break;
960
961             case WINED3D_IMMCONST_FLOAT4:
962                 TRACE("%.8e, %.8e, %.8e, %.8e",
963                         *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
964                         *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
965                 break;
966
967             default:
968                 TRACE("<unhandled immconst_type %#x>", reg->immconst_type);
969                 break;
970         }
971         TRACE(")");
972     }
973     else if (reg->type != WINED3DSPR_RASTOUT && reg->type != WINED3DSPR_MISCTYPE)
974     {
975         if (reg->array_idx != ~0U)
976         {
977             TRACE("%u[%u", offset, reg->array_idx);
978             if (reg->rel_addr)
979             {
980                 TRACE(" + ");
981                 shader_dump_src_param(reg->rel_addr, shader_version);
982             }
983             TRACE("]");
984         }
985         else
986         {
987             if (reg->rel_addr)
988             {
989                 TRACE("[");
990                 shader_dump_src_param(reg->rel_addr, shader_version);
991                 TRACE(" + ");
992             }
993             TRACE("%u", offset);
994             if (reg->rel_addr) TRACE("]");
995         }
996     }
997 }
998
999 void shader_dump_dst_param(const struct wined3d_shader_dst_param *param,
1000         const struct wined3d_shader_version *shader_version)
1001 {
1002     DWORD write_mask = param->write_mask;
1003
1004     shader_dump_register(&param->reg, shader_version);
1005
1006     if (write_mask != WINED3DSP_WRITEMASK_ALL)
1007     {
1008         static const char *write_mask_chars = "xyzw";
1009
1010         TRACE(".");
1011         if (write_mask & WINED3DSP_WRITEMASK_0) TRACE("%c", write_mask_chars[0]);
1012         if (write_mask & WINED3DSP_WRITEMASK_1) TRACE("%c", write_mask_chars[1]);
1013         if (write_mask & WINED3DSP_WRITEMASK_2) TRACE("%c", write_mask_chars[2]);
1014         if (write_mask & WINED3DSP_WRITEMASK_3) TRACE("%c", write_mask_chars[3]);
1015     }
1016 }
1017
1018 void shader_dump_src_param(const struct wined3d_shader_src_param *param,
1019         const struct wined3d_shader_version *shader_version)
1020 {
1021     DWORD src_modifier = param->modifiers;
1022     DWORD swizzle = param->swizzle;
1023
1024     if (src_modifier == WINED3DSPSM_NEG
1025             || src_modifier == WINED3DSPSM_BIASNEG
1026             || src_modifier == WINED3DSPSM_SIGNNEG
1027             || src_modifier == WINED3DSPSM_X2NEG
1028             || src_modifier == WINED3DSPSM_ABSNEG)
1029         TRACE("-");
1030     else if (src_modifier == WINED3DSPSM_COMP)
1031         TRACE("1-");
1032     else if (src_modifier == WINED3DSPSM_NOT)
1033         TRACE("!");
1034
1035     if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
1036         TRACE("abs(");
1037
1038     shader_dump_register(&param->reg, shader_version);
1039
1040     if (src_modifier)
1041     {
1042         switch (src_modifier)
1043         {
1044             case WINED3DSPSM_NONE:    break;
1045             case WINED3DSPSM_NEG:     break;
1046             case WINED3DSPSM_NOT:     break;
1047             case WINED3DSPSM_BIAS:    TRACE("_bias"); break;
1048             case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
1049             case WINED3DSPSM_SIGN:    TRACE("_bx2"); break;
1050             case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
1051             case WINED3DSPSM_COMP:    break;
1052             case WINED3DSPSM_X2:      TRACE("_x2"); break;
1053             case WINED3DSPSM_X2NEG:   TRACE("_x2"); break;
1054             case WINED3DSPSM_DZ:      TRACE("_dz"); break;
1055             case WINED3DSPSM_DW:      TRACE("_dw"); break;
1056             case WINED3DSPSM_ABSNEG:  TRACE(")"); break;
1057             case WINED3DSPSM_ABS:     TRACE(")"); break;
1058             default:                  TRACE("_unknown_modifier(%#x)", src_modifier);
1059         }
1060     }
1061
1062     if (swizzle != WINED3DSP_NOSWIZZLE)
1063     {
1064         static const char *swizzle_chars = "xyzw";
1065         DWORD swizzle_x = swizzle & 0x03;
1066         DWORD swizzle_y = (swizzle >> 2) & 0x03;
1067         DWORD swizzle_z = (swizzle >> 4) & 0x03;
1068         DWORD swizzle_w = (swizzle >> 6) & 0x03;
1069
1070         if (swizzle_x == swizzle_y
1071                 && swizzle_x == swizzle_z
1072                 && swizzle_x == swizzle_w)
1073         {
1074             TRACE(".%c", swizzle_chars[swizzle_x]);
1075         }
1076         else
1077         {
1078             TRACE(".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
1079                     swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
1080         }
1081     }
1082 }
1083
1084 /* Shared code in order to generate the bulk of the shader string.
1085  * NOTE: A description of how to parse tokens can be found on MSDN. */
1086 void shader_generate_main(IWineD3DBaseShader *iface, struct wined3d_shader_buffer *buffer,
1087         const shader_reg_maps *reg_maps, const DWORD *byte_code, void *backend_ctx)
1088 {
1089     IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
1090     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)shader->baseShader.device;
1091     const struct wined3d_shader_frontend *fe = shader->baseShader.frontend;
1092     void *fe_data = shader->baseShader.frontend_data;
1093     struct wined3d_shader_src_param src_rel_addr[4];
1094     struct wined3d_shader_src_param src_param[4];
1095     struct wined3d_shader_version shader_version;
1096     struct wined3d_shader_src_param dst_rel_addr;
1097     struct wined3d_shader_dst_param dst_param;
1098     struct wined3d_shader_instruction ins;
1099     struct wined3d_shader_context ctx;
1100     const DWORD *ptr = byte_code;
1101     DWORD i;
1102
1103     /* Initialize current parsing state. */
1104     ctx.shader = iface;
1105     ctx.gl_info = &device->adapter->gl_info;
1106     ctx.reg_maps = reg_maps;
1107     ctx.buffer = buffer;
1108     ctx.backend_data = backend_ctx;
1109
1110     ins.ctx = &ctx;
1111     ins.dst = &dst_param;
1112     ins.src = src_param;
1113     shader->baseShader.parse_state.current_row = 0;
1114
1115     fe->shader_read_header(fe_data, &ptr, &shader_version);
1116
1117     while (!fe->shader_is_end(fe_data, &ptr))
1118     {
1119         const char *comment;
1120         UINT comment_size;
1121         UINT param_size;
1122
1123         /* Skip comment tokens. */
1124         fe->shader_read_comment(&ptr, &comment, &comment_size);
1125         if (comment) continue;
1126
1127         /* Read opcode. */
1128         fe->shader_read_opcode(fe_data, &ptr, &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             ptr += 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             ptr += param_size;
1147             continue;
1148         }
1149
1150         /* Destination token */
1151         if (ins.dst_count) fe->shader_read_dst_param(fe_data, &ptr, &dst_param, &dst_rel_addr);
1152
1153         /* Predication token */
1154         if (ins.predicate)
1155         {
1156             FIXME("Predicates not implemented.\n");
1157             ins.predicate = *ptr++;
1158         }
1159
1160         /* Other source tokens */
1161         for (i = 0; i < ins.src_count; ++i)
1162         {
1163             fe->shader_read_src_param(fe_data, &ptr, &src_param[i], &src_rel_addr[i]);
1164         }
1165
1166         /* Call appropriate function for output target */
1167         device->shader_backend->shader_handle_instruction(&ins);
1168     }
1169 }
1170
1171 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param *dst)
1172 {
1173     DWORD mmask = dst->modifiers;
1174
1175     switch (dst->shift)
1176     {
1177         case 0: break;
1178         case 13: TRACE("_d8"); break;
1179         case 14: TRACE("_d4"); break;
1180         case 15: TRACE("_d2"); break;
1181         case 1: TRACE("_x2"); break;
1182         case 2: TRACE("_x4"); break;
1183         case 3: TRACE("_x8"); break;
1184         default: TRACE("_unhandled_shift(%d)", dst->shift); break;
1185     }
1186
1187     if (mmask & WINED3DSPDM_SATURATE)         TRACE("_sat");
1188     if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
1189     if (mmask & WINED3DSPDM_MSAMPCENTROID)    TRACE("_centroid");
1190
1191     mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
1192     if (mmask) FIXME("_unrecognized_modifier(%#x)", mmask);
1193 }
1194
1195 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *byte_code)
1196 {
1197     struct wined3d_shader_version shader_version;
1198     const DWORD *ptr = byte_code;
1199     const char *type_prefix;
1200     DWORD i;
1201
1202     TRACE("Parsing %p.\n", byte_code);
1203
1204     fe->shader_read_header(fe_data, &ptr, &shader_version);
1205
1206     switch (shader_version.type)
1207     {
1208         case WINED3D_SHADER_TYPE_VERTEX:
1209             type_prefix = "vs";
1210             break;
1211
1212         case WINED3D_SHADER_TYPE_GEOMETRY:
1213             type_prefix = "gs";
1214             break;
1215
1216         case WINED3D_SHADER_TYPE_PIXEL:
1217             type_prefix = "ps";
1218             break;
1219
1220         default:
1221             FIXME("Unhandled shader type %#x.\n", shader_version.type);
1222             type_prefix = "unknown";
1223             break;
1224     }
1225
1226     TRACE("%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
1227
1228     while (!fe->shader_is_end(fe_data, &ptr))
1229     {
1230         struct wined3d_shader_instruction ins;
1231         const char *comment;
1232         UINT comment_size;
1233         UINT param_size;
1234
1235         /* comment */
1236         fe->shader_read_comment(&ptr, &comment, &comment_size);
1237         if (comment)
1238         {
1239             if (comment_size > 4 && *(const DWORD *)comment == WINEMAKEFOURCC('T', 'E', 'X', 'T'))
1240             {
1241                 const char *end = comment + comment_size;
1242                 const char *ptr = comment + 4;
1243                 const char *line = ptr;
1244
1245                 TRACE("// TEXT\n");
1246                 while (ptr != end)
1247                 {
1248                     if (*ptr == '\n')
1249                     {
1250                         UINT len = ptr - line;
1251                         if (len && *(ptr - 1) == '\r') --len;
1252                         TRACE("// %s\n", debugstr_an(line, len));
1253                         line = ++ptr;
1254                     }
1255                     else ++ptr;
1256                 }
1257                 if (line != ptr) TRACE("// %s\n", debugstr_an(line, ptr - line));
1258             }
1259             else TRACE("// %s\n", debugstr_an(comment, comment_size));
1260             continue;
1261         }
1262
1263         fe->shader_read_opcode(fe_data, &ptr, &ins, &param_size);
1264         if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1265         {
1266             TRACE("Skipping unrecognized instruction.\n");
1267             ptr += param_size;
1268             continue;
1269         }
1270
1271         if (ins.handler_idx == WINED3DSIH_DCL)
1272         {
1273             struct wined3d_shader_semantic semantic;
1274
1275             fe->shader_read_semantic(&ptr, &semantic);
1276
1277             shader_dump_decl_usage(&semantic, &shader_version);
1278             shader_dump_ins_modifiers(&semantic.reg);
1279             TRACE(" ");
1280             shader_dump_dst_param(&semantic.reg, &shader_version);
1281         }
1282         else if (ins.handler_idx == WINED3DSIH_DEF)
1283         {
1284             struct wined3d_shader_src_param rel_addr;
1285             struct wined3d_shader_dst_param dst;
1286
1287             fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1288
1289             TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(dst.reg.type, dst.reg.idx),
1290                     *(const float *)(ptr),
1291                     *(const float *)(ptr + 1),
1292                     *(const float *)(ptr + 2),
1293                     *(const float *)(ptr + 3));
1294             ptr += 4;
1295         }
1296         else if (ins.handler_idx == WINED3DSIH_DEFI)
1297         {
1298             struct wined3d_shader_src_param rel_addr;
1299             struct wined3d_shader_dst_param dst;
1300
1301             fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1302
1303             TRACE("defi i%u = %d, %d, %d, %d", dst.reg.idx,
1304                     *(ptr),
1305                     *(ptr + 1),
1306                     *(ptr + 2),
1307                     *(ptr + 3));
1308             ptr += 4;
1309         }
1310         else if (ins.handler_idx == WINED3DSIH_DEFB)
1311         {
1312             struct wined3d_shader_src_param rel_addr;
1313             struct wined3d_shader_dst_param dst;
1314
1315             fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1316
1317             TRACE("defb b%u = %s", dst.reg.idx, *ptr ? "true" : "false");
1318             ++ptr;
1319         }
1320         else
1321         {
1322             struct wined3d_shader_src_param dst_rel_addr, src_rel_addr;
1323             struct wined3d_shader_dst_param dst_param;
1324             struct wined3d_shader_src_param src_param;
1325
1326             if (ins.dst_count)
1327             {
1328                 fe->shader_read_dst_param(fe_data, &ptr, &dst_param, &dst_rel_addr);
1329             }
1330
1331             /* Print out predication source token first - it follows
1332              * the destination token. */
1333             if (ins.predicate)
1334             {
1335                 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
1336                 TRACE("(");
1337                 shader_dump_src_param(&src_param, &shader_version);
1338                 TRACE(") ");
1339             }
1340
1341             /* PixWin marks instructions with the coissue flag with a '+' */
1342             if (ins.coissue) TRACE("+");
1343
1344             TRACE("%s", shader_opcode_names[ins.handler_idx]);
1345
1346             if (ins.handler_idx == WINED3DSIH_IFC
1347                     || ins.handler_idx == WINED3DSIH_BREAKC)
1348             {
1349                 switch (ins.flags)
1350                 {
1351                     case COMPARISON_GT: TRACE("_gt"); break;
1352                     case COMPARISON_EQ: TRACE("_eq"); break;
1353                     case COMPARISON_GE: TRACE("_ge"); break;
1354                     case COMPARISON_LT: TRACE("_lt"); break;
1355                     case COMPARISON_NE: TRACE("_ne"); break;
1356                     case COMPARISON_LE: TRACE("_le"); break;
1357                     default: TRACE("_(%u)", ins.flags);
1358                 }
1359             }
1360             else if (ins.handler_idx == WINED3DSIH_TEX
1361                     && shader_version.major >= 2
1362                     && (ins.flags & WINED3DSI_TEXLD_PROJECT))
1363             {
1364                 TRACE("p");
1365             }
1366
1367             /* We already read the destination token, print it. */
1368             if (ins.dst_count)
1369             {
1370                 shader_dump_ins_modifiers(&dst_param);
1371                 TRACE(" ");
1372                 shader_dump_dst_param(&dst_param, &shader_version);
1373             }
1374
1375             /* Other source tokens */
1376             for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
1377             {
1378                 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
1379                 TRACE(!i ? " " : ", ");
1380                 shader_dump_src_param(&src_param, &shader_version);
1381             }
1382         }
1383         TRACE("\n");
1384     }
1385 }
1386
1387 static void shader_cleanup(IWineD3DBaseShader *iface)
1388 {
1389     IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
1390
1391     ((IWineD3DDeviceImpl *)shader->baseShader.device)->shader_backend->shader_destroy(iface);
1392     HeapFree(GetProcessHeap(), 0, shader->baseShader.reg_maps.constf);
1393     HeapFree(GetProcessHeap(), 0, shader->baseShader.function);
1394     shader_delete_constant_list(&shader->baseShader.constantsF);
1395     shader_delete_constant_list(&shader->baseShader.constantsB);
1396     shader_delete_constant_list(&shader->baseShader.constantsI);
1397     list_remove(&shader->baseShader.shader_list_entry);
1398
1399     if (shader->baseShader.frontend && shader->baseShader.frontend_data)
1400     {
1401         shader->baseShader.frontend->shader_free(shader->baseShader.frontend_data);
1402     }
1403 }
1404
1405 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
1406 static void shader_none_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS) {}
1407 static void shader_none_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type, const SIZE *ds_mask_size) {}
1408 static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1409 static void shader_none_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1410 static void shader_none_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1411 static void shader_none_load_constants(const struct wined3d_context *context, char usePS, char useVS) {}
1412 static void shader_none_load_np2fixup_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1413 static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1414 static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1415 static void shader_none_free(IWineD3DDevice *iface) {}
1416 static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1417
1418 static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
1419 {
1420     /* Set the shader caps to 0 for the none shader backend */
1421     caps->VertexShaderVersion = 0;
1422     caps->MaxVertexShaderConst = 0;
1423     caps->PixelShaderVersion = 0;
1424     caps->PixelShader1xMaxValue = 0.0f;
1425     caps->MaxPixelShaderConst = 0;
1426     caps->VSClipping = FALSE;
1427 }
1428
1429 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1430 {
1431     if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1432     {
1433         TRACE("Checking support for fixup:\n");
1434         dump_color_fixup_desc(fixup);
1435     }
1436
1437     /* Faked to make some apps happy. */
1438     if (!is_complex_fixup(fixup))
1439     {
1440         TRACE("[OK]\n");
1441         return TRUE;
1442     }
1443
1444     TRACE("[FAILED]\n");
1445     return FALSE;
1446 }
1447
1448 const shader_backend_t none_shader_backend = {
1449     shader_none_handle_instruction,
1450     shader_none_select,
1451     shader_none_select_depth_blt,
1452     shader_none_deselect_depth_blt,
1453     shader_none_update_float_vertex_constants,
1454     shader_none_update_float_pixel_constants,
1455     shader_none_load_constants,
1456     shader_none_load_np2fixup_constants,
1457     shader_none_destroy,
1458     shader_none_alloc,
1459     shader_none_free,
1460     shader_none_dirty_const,
1461     shader_none_get_caps,
1462     shader_none_color_fixup_supported,
1463 };
1464
1465 static void shader_get_parent(IWineD3DBaseShaderImpl *shader, IUnknown **parent)
1466 {
1467     *parent = shader->baseShader.parent;
1468     IUnknown_AddRef(*parent);
1469     TRACE("shader %p, returning %p.\n", shader, *parent);
1470 }
1471
1472 static HRESULT shader_get_function(IWineD3DBaseShaderImpl *shader, void *data, UINT *data_size)
1473 {
1474     if (!data)
1475     {
1476         *data_size = shader->baseShader.functionLength;
1477         return WINED3D_OK;
1478     }
1479
1480     if (*data_size < shader->baseShader.functionLength)
1481     {
1482         /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
1483          * than the required size we should write the required size and
1484          * return D3DERR_MOREDATA. That's not actually true. */
1485         return WINED3DERR_INVALIDCALL;
1486     }
1487
1488     memcpy(data, shader->baseShader.function, shader->baseShader.functionLength);
1489
1490     return WINED3D_OK;
1491 }
1492
1493 static HRESULT shader_set_function(IWineD3DBaseShaderImpl *shader, const DWORD *byte_code,
1494         const struct wined3d_shader_signature *output_signature, DWORD float_const_count)
1495 {
1496     struct shader_reg_maps *reg_maps = &shader->baseShader.reg_maps;
1497     const struct wined3d_shader_frontend *fe;
1498     HRESULT hr;
1499
1500     TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1501             shader, byte_code, output_signature, float_const_count);
1502
1503     fe = shader_select_frontend(*byte_code);
1504     if (!fe)
1505     {
1506         FIXME("Unable to find frontend for shader.\n");
1507         return WINED3DERR_INVALIDCALL;
1508     }
1509     shader->baseShader.frontend = fe;
1510     shader->baseShader.frontend_data = fe->shader_init(byte_code, output_signature);
1511     if (!shader->baseShader.frontend_data)
1512     {
1513         FIXME("Failed to initialize frontend.\n");
1514         return WINED3DERR_INVALIDCALL;
1515     }
1516
1517     /* First pass: trace shader. */
1518     if (TRACE_ON(d3d_shader)) shader_trace_init(fe, shader->baseShader.frontend_data, byte_code);
1519
1520     /* Initialize immediate constant lists. */
1521     list_init(&shader->baseShader.constantsF);
1522     list_init(&shader->baseShader.constantsB);
1523     list_init(&shader->baseShader.constantsI);
1524
1525     /* Second pass: figure out which registers are used, what the semantics are, etc. */
1526     hr = shader_get_registers_used((IWineD3DBaseShader *)shader, fe,
1527             reg_maps, shader->baseShader.input_signature, shader->baseShader.output_signature,
1528             byte_code, float_const_count);
1529     if (FAILED(hr)) return hr;
1530
1531     shader->baseShader.function = HeapAlloc(GetProcessHeap(), 0, shader->baseShader.functionLength);
1532     if (!shader->baseShader.function) return E_OUTOFMEMORY;
1533     memcpy(shader->baseShader.function, byte_code, shader->baseShader.functionLength);
1534
1535     return WINED3D_OK;
1536 }
1537
1538 static HRESULT STDMETHODCALLTYPE vertexshader_QueryInterface(IWineD3DVertexShader *iface, REFIID riid, void **object)
1539 {
1540     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1541
1542     if (IsEqualGUID(riid, &IID_IWineD3DVertexShader)
1543             || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1544             || IsEqualGUID(riid, &IID_IWineD3DBase)
1545             || IsEqualGUID(riid, &IID_IUnknown))
1546     {
1547         IUnknown_AddRef(iface);
1548         *object = iface;
1549         return S_OK;
1550     }
1551
1552     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1553
1554     *object = NULL;
1555     return E_NOINTERFACE;
1556 }
1557
1558 static ULONG STDMETHODCALLTYPE vertexshader_AddRef(IWineD3DVertexShader *iface)
1559 {
1560     IWineD3DVertexShaderImpl *shader = (IWineD3DVertexShaderImpl *)iface;
1561     ULONG refcount = InterlockedIncrement(&shader->baseShader.ref);
1562
1563     TRACE("%p increasing refcount to %u.\n", shader, refcount);
1564
1565     return refcount;
1566 }
1567
1568 static ULONG STDMETHODCALLTYPE vertexshader_Release(IWineD3DVertexShader *iface)
1569 {
1570     IWineD3DVertexShaderImpl *shader = (IWineD3DVertexShaderImpl *)iface;
1571     ULONG refcount = InterlockedDecrement(&shader->baseShader.ref);
1572
1573     TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1574
1575     if (!refcount)
1576     {
1577         shader_cleanup((IWineD3DBaseShader *)iface);
1578         shader->baseShader.parent_ops->wined3d_object_destroyed(shader->baseShader.parent);
1579         HeapFree(GetProcessHeap(), 0, shader);
1580     }
1581
1582     return refcount;
1583 }
1584
1585 static HRESULT STDMETHODCALLTYPE vertexshader_GetParent(IWineD3DVertexShader *iface, IUnknown **parent)
1586 {
1587     TRACE("iface %p, parent %p.\n", iface, parent);
1588
1589     shader_get_parent((IWineD3DBaseShaderImpl *)iface, parent);
1590
1591     return WINED3D_OK;
1592 }
1593
1594 static HRESULT STDMETHODCALLTYPE vertexshader_GetFunction(IWineD3DVertexShader *iface, void *data, UINT *data_size)
1595 {
1596     TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
1597
1598     return shader_get_function((IWineD3DBaseShaderImpl *)iface, data, data_size);
1599 }
1600
1601 /* Set local constants for d3d8 shaders. */
1602 static HRESULT STDMETHODCALLTYPE vertexshader_SetLocalConstantsF(IWineD3DVertexShader *iface,
1603         UINT start_idx, const float *src_data, UINT count)
1604 {
1605     IWineD3DVertexShaderImpl *shader =(IWineD3DVertexShaderImpl *)iface;
1606     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)shader->baseShader.device;
1607     UINT i, end_idx;
1608
1609     TRACE("iface %p, start_idx %u, src_data %p, count %u.\n", iface, start_idx, src_data, count);
1610
1611     end_idx = start_idx + count;
1612     if (end_idx > device->d3d_vshader_constantF)
1613     {
1614         WARN("end_idx %u > float constants limit %u.\n", end_idx, device->d3d_vshader_constantF);
1615         end_idx = device->d3d_vshader_constantF;
1616     }
1617
1618     for (i = start_idx; i < end_idx; ++i)
1619     {
1620         local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
1621         if (!lconst) return E_OUTOFMEMORY;
1622
1623         lconst->idx = i;
1624         memcpy(lconst->value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
1625         list_add_head(&shader->baseShader.constantsF, &lconst->entry);
1626     }
1627
1628     return WINED3D_OK;
1629 }
1630
1631 static const IWineD3DVertexShaderVtbl IWineD3DVertexShader_Vtbl =
1632 {
1633     /* IUnknown methods */
1634     vertexshader_QueryInterface,
1635     vertexshader_AddRef,
1636     vertexshader_Release,
1637     /* IWineD3DBase methods */
1638     vertexshader_GetParent,
1639     /* IWineD3DBaseShader methods */
1640     vertexshader_GetFunction,
1641     /* IWineD3DVertexShader methods */
1642     vertexshader_SetLocalConstantsF,
1643 };
1644
1645 void find_vs_compile_args(IWineD3DVertexShaderImpl *shader,
1646         IWineD3DStateBlockImpl *stateblock, struct vs_compile_args *args)
1647 {
1648     args->fog_src = stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
1649     args->clip_enabled = stateblock->renderState[WINED3DRS_CLIPPING]
1650             && stateblock->renderState[WINED3DRS_CLIPPLANEENABLE];
1651     args->swizzle_map = ((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.swizzle_map;
1652 }
1653
1654 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
1655 {
1656     if (usage_idx1 != usage_idx2) return FALSE;
1657     if (usage1 == usage2) return TRUE;
1658     if (usage1 == WINED3DDECLUSAGE_POSITION && usage2 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
1659     if (usage2 == WINED3DDECLUSAGE_POSITION && usage1 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
1660
1661     return FALSE;
1662 }
1663
1664 BOOL vshader_get_input(IWineD3DVertexShader *iface, BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
1665 {
1666     IWineD3DVertexShaderImpl *shader = (IWineD3DVertexShaderImpl *)iface;
1667     WORD map = shader->baseShader.reg_maps.input_registers;
1668     unsigned int i;
1669
1670     for (i = 0; map; map >>= 1, ++i)
1671     {
1672         if (!(map & 1)) continue;
1673
1674         if (match_usage(shader->attributes[i].usage,
1675                 shader->attributes[i].usage_idx, usage_req, usage_idx_req))
1676         {
1677             *regnum = i;
1678             return TRUE;
1679         }
1680     }
1681     return FALSE;
1682 }
1683
1684 static void vertexshader_set_limits(IWineD3DVertexShaderImpl *shader)
1685 {
1686     DWORD shader_version = WINED3D_SHADER_VERSION(shader->baseShader.reg_maps.shader_version.major,
1687             shader->baseShader.reg_maps.shader_version.minor);
1688     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)shader->baseShader.device;
1689
1690     shader->baseShader.limits.texcoord = 0;
1691     shader->baseShader.limits.attributes = 16;
1692     shader->baseShader.limits.packed_input = 0;
1693
1694     switch (shader_version)
1695     {
1696         case WINED3D_SHADER_VERSION(1, 0):
1697         case WINED3D_SHADER_VERSION(1, 1):
1698             shader->baseShader.limits.temporary = 12;
1699             shader->baseShader.limits.constant_bool = 0;
1700             shader->baseShader.limits.constant_int = 0;
1701             shader->baseShader.limits.address = 1;
1702             shader->baseShader.limits.packed_output = 0;
1703             shader->baseShader.limits.sampler = 0;
1704             shader->baseShader.limits.label = 0;
1705             /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1706              * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1707              * constants? */
1708             shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1709             break;
1710
1711         case WINED3D_SHADER_VERSION(2, 0):
1712         case WINED3D_SHADER_VERSION(2, 1):
1713             shader->baseShader.limits.temporary = 12;
1714             shader->baseShader.limits.constant_bool = 16;
1715             shader->baseShader.limits.constant_int = 16;
1716             shader->baseShader.limits.address = 1;
1717             shader->baseShader.limits.packed_output = 0;
1718             shader->baseShader.limits.sampler = 0;
1719             shader->baseShader.limits.label = 16;
1720             shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1721             break;
1722
1723         case WINED3D_SHADER_VERSION(4, 0):
1724             FIXME("Using 3.0 limits for 4.0 shader.\n");
1725             /* Fall through. */
1726
1727         case WINED3D_SHADER_VERSION(3, 0):
1728             shader->baseShader.limits.temporary = 32;
1729             shader->baseShader.limits.constant_bool = 32;
1730             shader->baseShader.limits.constant_int = 32;
1731             shader->baseShader.limits.address = 1;
1732             shader->baseShader.limits.packed_output = 12;
1733             shader->baseShader.limits.sampler = 4;
1734             shader->baseShader.limits.label = 16; /* FIXME: 2048 */
1735             /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1736              * even though they are capable of supporting much more (GL
1737              * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1738              * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1739              * shaders to 256. */
1740             shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1741             break;
1742
1743         default:
1744             shader->baseShader.limits.temporary = 12;
1745             shader->baseShader.limits.constant_bool = 16;
1746             shader->baseShader.limits.constant_int = 16;
1747             shader->baseShader.limits.address = 1;
1748             shader->baseShader.limits.packed_output = 0;
1749             shader->baseShader.limits.sampler = 0;
1750             shader->baseShader.limits.label = 16;
1751             shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1752             FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1753                     shader->baseShader.reg_maps.shader_version.major,
1754                     shader->baseShader.reg_maps.shader_version.minor);
1755     }
1756 }
1757
1758 HRESULT vertexshader_init(IWineD3DVertexShaderImpl *shader, IWineD3DDeviceImpl *device,
1759         const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1760         IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
1761 {
1762     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1763     struct shader_reg_maps *reg_maps = &shader->baseShader.reg_maps;
1764     unsigned int i;
1765     HRESULT hr;
1766     WORD map;
1767
1768     if (!byte_code) return WINED3DERR_INVALIDCALL;
1769
1770     shader->lpVtbl = &IWineD3DVertexShader_Vtbl;
1771     shader_init(&shader->baseShader, device, parent, parent_ops);
1772
1773     hr = shader_set_function((IWineD3DBaseShaderImpl *)shader, byte_code,
1774             output_signature, device->d3d_vshader_constantF);
1775     if (FAILED(hr))
1776     {
1777         WARN("Failed to set function, hr %#x.\n", hr);
1778         shader_cleanup((IWineD3DBaseShader *)shader);
1779         return hr;
1780     }
1781
1782     map = shader->baseShader.reg_maps.input_registers;
1783     for (i = 0; map; map >>= 1, ++i)
1784     {
1785         if (!(map & 1) || !shader->baseShader.input_signature[i].semantic_name) continue;
1786
1787         shader->attributes[i].usage =
1788                 shader_usage_from_semantic_name(shader->baseShader.input_signature[i].semantic_name);
1789         shader->attributes[i].usage_idx = shader->baseShader.input_signature[i].semantic_idx;
1790     }
1791
1792     if (output_signature)
1793     {
1794         for (i = 0; i < output_signature->element_count; ++i)
1795         {
1796             struct wined3d_shader_signature_element *e = &output_signature->elements[i];
1797             reg_maps->output_registers |= 1 << e->register_idx;
1798             shader->baseShader.output_signature[e->register_idx] = *e;
1799         }
1800     }
1801
1802     vertexshader_set_limits(shader);
1803
1804     if (device->vs_selected_mode == SHADER_ARB
1805             && (gl_info->quirks & WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT)
1806             && shader->min_rel_offset <= shader->max_rel_offset)
1807     {
1808         if (shader->max_rel_offset - shader->min_rel_offset > 127)
1809         {
1810             FIXME("The difference between the minimum and maximum relative offset is > 127.\n");
1811             FIXME("Which this OpenGL implementation does not support. Try using GLSL.\n");
1812             FIXME("Min: %d, Max: %d.\n", shader->min_rel_offset, shader->max_rel_offset);
1813         }
1814         else if (shader->max_rel_offset - shader->min_rel_offset > 63)
1815         {
1816             shader->rel_offset = shader->min_rel_offset + 63;
1817         }
1818         else if (shader->max_rel_offset > 63)
1819         {
1820             shader->rel_offset = shader->min_rel_offset;
1821         }
1822         else
1823         {
1824             shader->rel_offset = 0;
1825         }
1826     }
1827
1828     shader->baseShader.load_local_constsF = shader->baseShader.reg_maps.usesrelconstF
1829             && !list_empty(&shader->baseShader.constantsF);
1830
1831     return WINED3D_OK;
1832 }
1833
1834 static HRESULT STDMETHODCALLTYPE geometryshader_QueryInterface(IWineD3DGeometryShader *iface,
1835         REFIID riid, void **object)
1836 {
1837     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1838
1839     if (IsEqualGUID(riid, &IID_IWineD3DGeometryShader)
1840             || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1841             || IsEqualGUID(riid, &IID_IWineD3DBase)
1842             || IsEqualGUID(riid, &IID_IUnknown))
1843     {
1844         IUnknown_AddRef(iface);
1845         *object = iface;
1846         return S_OK;
1847     }
1848
1849     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1850
1851     *object = NULL;
1852     return E_NOINTERFACE;
1853 }
1854
1855 static ULONG STDMETHODCALLTYPE geometryshader_AddRef(IWineD3DGeometryShader *iface)
1856 {
1857     struct wined3d_geometryshader *shader = (struct wined3d_geometryshader *)iface;
1858     ULONG refcount = InterlockedIncrement(&shader->base_shader.ref);
1859
1860     TRACE("%p increasing refcount to %u.\n", shader, refcount);
1861
1862     return refcount;
1863 }
1864
1865 static ULONG STDMETHODCALLTYPE geometryshader_Release(IWineD3DGeometryShader *iface)
1866 {
1867     struct wined3d_geometryshader *shader = (struct wined3d_geometryshader *)iface;
1868     ULONG refcount = InterlockedDecrement(&shader->base_shader.ref);
1869
1870     TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1871
1872     if (!refcount)
1873     {
1874         shader_cleanup((IWineD3DBaseShader *)iface);
1875         shader->base_shader.parent_ops->wined3d_object_destroyed(shader->base_shader.parent);
1876         HeapFree(GetProcessHeap(), 0, shader);
1877     }
1878
1879     return refcount;
1880 }
1881
1882 static HRESULT STDMETHODCALLTYPE geometryshader_GetParent(IWineD3DGeometryShader *iface, IUnknown **parent)
1883 {
1884     TRACE("iface %p, parent %p.\n", iface, parent);
1885
1886     shader_get_parent((IWineD3DBaseShaderImpl *)iface, parent);
1887
1888     return WINED3D_OK;
1889 }
1890
1891 static HRESULT STDMETHODCALLTYPE geometryshader_GetFunction(IWineD3DGeometryShader *iface, void *data, UINT *data_size)
1892 {
1893     TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
1894
1895     return shader_get_function((IWineD3DBaseShaderImpl *)iface, data, data_size);
1896 }
1897
1898 static const IWineD3DGeometryShaderVtbl wined3d_geometryshader_vtbl =
1899 {
1900     /* IUnknown methods */
1901     geometryshader_QueryInterface,
1902     geometryshader_AddRef,
1903     geometryshader_Release,
1904     /* IWineD3DBase methods */
1905     geometryshader_GetParent,
1906     /* IWineD3DBaseShader methods */
1907     geometryshader_GetFunction,
1908 };
1909
1910 HRESULT geometryshader_init(struct wined3d_geometryshader *shader, IWineD3DDeviceImpl *device,
1911         const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1912         IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
1913 {
1914     HRESULT hr;
1915
1916     shader->vtbl = &wined3d_geometryshader_vtbl;
1917     shader_init(&shader->base_shader, device, parent, parent_ops);
1918
1919     hr = shader_set_function((IWineD3DBaseShaderImpl *)shader, byte_code, output_signature, 0);
1920     if (FAILED(hr))
1921     {
1922         WARN("Failed to set function, hr %#x.\n", hr);
1923         shader_cleanup((IWineD3DBaseShader *)shader);
1924         return hr;
1925     }
1926
1927     shader->base_shader.load_local_constsF = FALSE;
1928
1929     return WINED3D_OK;
1930 }
1931
1932 static HRESULT STDMETHODCALLTYPE pixelshader_QueryInterface(IWineD3DPixelShader *iface, REFIID riid, void **object)
1933 {
1934     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1935
1936     if (IsEqualGUID(riid, &IID_IWineD3DPixelShader)
1937             || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1938             || IsEqualGUID(riid, &IID_IWineD3DBase)
1939             || IsEqualGUID(riid, &IID_IUnknown))
1940     {
1941         IUnknown_AddRef(iface);
1942         *object = iface;
1943         return S_OK;
1944     }
1945
1946     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1947
1948     *object = NULL;
1949     return E_NOINTERFACE;
1950 }
1951
1952 static ULONG STDMETHODCALLTYPE pixelshader_AddRef(IWineD3DPixelShader *iface)
1953 {
1954     IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)iface;
1955     ULONG refcount = InterlockedIncrement(&shader->baseShader.ref);
1956
1957     TRACE("%p increasing refcount to %u.\n", shader, refcount);
1958
1959     return refcount;
1960 }
1961
1962 static ULONG STDMETHODCALLTYPE pixelshader_Release(IWineD3DPixelShader *iface)
1963 {
1964     IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)iface;
1965     ULONG refcount = InterlockedDecrement(&shader->baseShader.ref);
1966
1967     TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1968
1969     if (!refcount)
1970     {
1971         shader_cleanup((IWineD3DBaseShader *)iface);
1972         shader->baseShader.parent_ops->wined3d_object_destroyed(shader->baseShader.parent);
1973         HeapFree(GetProcessHeap(), 0, shader);
1974     }
1975
1976     return refcount;
1977 }
1978
1979 static HRESULT STDMETHODCALLTYPE pixelshader_GetParent(IWineD3DPixelShader *iface, IUnknown **parent)
1980 {
1981     TRACE("iface %p, parent %p.\n", iface, parent);
1982
1983     shader_get_parent((IWineD3DBaseShaderImpl *)iface, parent);
1984
1985     return WINED3D_OK;
1986 }
1987
1988 static HRESULT STDMETHODCALLTYPE pixelshader_GetFunction(IWineD3DPixelShader *iface, void *data, UINT *data_size)
1989 {
1990     TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
1991
1992     return shader_get_function((IWineD3DBaseShaderImpl *)iface, data, data_size);
1993 }
1994
1995 static const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
1996 {
1997     /* IUnknown methods */
1998     pixelshader_QueryInterface,
1999     pixelshader_AddRef,
2000     pixelshader_Release,
2001     /* IWineD3DBase methods */
2002     pixelshader_GetParent,
2003     /* IWineD3DBaseShader methods */
2004     pixelshader_GetFunction
2005 };
2006
2007 void find_ps_compile_args(IWineD3DPixelShaderImpl *shader,
2008         IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args)
2009 {
2010     IWineD3DBaseTextureImpl *texture;
2011     UINT i;
2012
2013     memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
2014     args->srgb_correction = stateblock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
2015     args->np2_fixup = 0;
2016
2017     for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2018     {
2019         if (!shader->baseShader.reg_maps.sampler_type[i]) continue;
2020         texture = (IWineD3DBaseTextureImpl *)stateblock->textures[i];
2021         if (!texture)
2022         {
2023             args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
2024             continue;
2025         }
2026         args->color_fixup[i] = texture->resource.format_desc->color_fixup;
2027
2028         if (texture->resource.format_desc->Flags & WINED3DFMT_FLAG_SHADOW)
2029             args->shadow |= 1 << i;
2030
2031         /* Flag samplers that need NP2 texcoord fixup. */
2032         if (!texture->baseTexture.pow2Matrix_identity)
2033         {
2034             args->np2_fixup |= (1 << i);
2035         }
2036     }
2037     if (shader->baseShader.reg_maps.shader_version.major >= 3)
2038     {
2039         if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed)
2040         {
2041             args->vp_mode = pretransformed;
2042         }
2043         else if (use_vs(stateblock))
2044         {
2045             args->vp_mode = vertexshader;
2046         }
2047         else
2048         {
2049             args->vp_mode = fixedfunction;
2050         }
2051         args->fog = FOG_OFF;
2052     }
2053     else
2054     {
2055         args->vp_mode = vertexshader;
2056         if (stateblock->renderState[WINED3DRS_FOGENABLE])
2057         {
2058             switch (stateblock->renderState[WINED3DRS_FOGTABLEMODE])
2059             {
2060                 case WINED3DFOG_NONE:
2061                     if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed
2062                             || use_vs(stateblock))
2063                     {
2064                         args->fog = FOG_LINEAR;
2065                         break;
2066                     }
2067
2068                     switch (stateblock->renderState[WINED3DRS_FOGVERTEXMODE])
2069                     {
2070                         case WINED3DFOG_NONE: /* Fall through. */
2071                         case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
2072                         case WINED3DFOG_EXP:    args->fog = FOG_EXP;    break;
2073                         case WINED3DFOG_EXP2:   args->fog = FOG_EXP2;   break;
2074                     }
2075                     break;
2076
2077                 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
2078                 case WINED3DFOG_EXP:    args->fog = FOG_EXP;    break;
2079                 case WINED3DFOG_EXP2:   args->fog = FOG_EXP2;   break;
2080             }
2081         }
2082         else
2083         {
2084             args->fog = FOG_OFF;
2085         }
2086     }
2087 }
2088
2089 static void pixelshader_set_limits(IWineD3DPixelShaderImpl *shader)
2090 {
2091     DWORD shader_version = WINED3D_SHADER_VERSION(shader->baseShader.reg_maps.shader_version.major,
2092             shader->baseShader.reg_maps.shader_version.minor);
2093
2094     shader->baseShader.limits.attributes = 0;
2095     shader->baseShader.limits.address = 0;
2096     shader->baseShader.limits.packed_output = 0;
2097
2098     switch (shader_version)
2099     {
2100         case WINED3D_SHADER_VERSION(1, 0):
2101         case WINED3D_SHADER_VERSION(1, 1):
2102         case WINED3D_SHADER_VERSION(1, 2):
2103         case WINED3D_SHADER_VERSION(1, 3):
2104             shader->baseShader.limits.temporary = 2;
2105             shader->baseShader.limits.constant_float = 8;
2106             shader->baseShader.limits.constant_int = 0;
2107             shader->baseShader.limits.constant_bool = 0;
2108             shader->baseShader.limits.texcoord = 4;
2109             shader->baseShader.limits.sampler = 4;
2110             shader->baseShader.limits.packed_input = 0;
2111             shader->baseShader.limits.label = 0;
2112             break;
2113
2114         case WINED3D_SHADER_VERSION(1, 4):
2115             shader->baseShader.limits.temporary = 6;
2116             shader->baseShader.limits.constant_float = 8;
2117             shader->baseShader.limits.constant_int = 0;
2118             shader->baseShader.limits.constant_bool = 0;
2119             shader->baseShader.limits.texcoord = 6;
2120             shader->baseShader.limits.sampler = 6;
2121             shader->baseShader.limits.packed_input = 0;
2122             shader->baseShader.limits.label = 0;
2123             break;
2124
2125         /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2126         case WINED3D_SHADER_VERSION(2, 0):
2127             shader->baseShader.limits.temporary = 32;
2128             shader->baseShader.limits.constant_float = 32;
2129             shader->baseShader.limits.constant_int = 16;
2130             shader->baseShader.limits.constant_bool = 16;
2131             shader->baseShader.limits.texcoord = 8;
2132             shader->baseShader.limits.sampler = 16;
2133             shader->baseShader.limits.packed_input = 0;
2134             break;
2135
2136         case WINED3D_SHADER_VERSION(2, 1):
2137             shader->baseShader.limits.temporary = 32;
2138             shader->baseShader.limits.constant_float = 32;
2139             shader->baseShader.limits.constant_int = 16;
2140             shader->baseShader.limits.constant_bool = 16;
2141             shader->baseShader.limits.texcoord = 8;
2142             shader->baseShader.limits.sampler = 16;
2143             shader->baseShader.limits.packed_input = 0;
2144             shader->baseShader.limits.label = 16;
2145             break;
2146
2147         case WINED3D_SHADER_VERSION(4, 0):
2148             FIXME("Using 3.0 limits for 4.0 shader.\n");
2149             /* Fall through. */
2150
2151         case WINED3D_SHADER_VERSION(3, 0):
2152             shader->baseShader.limits.temporary = 32;
2153             shader->baseShader.limits.constant_float = 224;
2154             shader->baseShader.limits.constant_int = 16;
2155             shader->baseShader.limits.constant_bool = 16;
2156             shader->baseShader.limits.texcoord = 0;
2157             shader->baseShader.limits.sampler = 16;
2158             shader->baseShader.limits.packed_input = 12;
2159             shader->baseShader.limits.label = 16; /* FIXME: 2048 */
2160             break;
2161
2162         default:
2163             shader->baseShader.limits.temporary = 32;
2164             shader->baseShader.limits.constant_float = 32;
2165             shader->baseShader.limits.constant_int = 16;
2166             shader->baseShader.limits.constant_bool = 16;
2167             shader->baseShader.limits.texcoord = 8;
2168             shader->baseShader.limits.sampler = 16;
2169             shader->baseShader.limits.packed_input = 0;
2170             shader->baseShader.limits.label = 0;
2171             FIXME("Unrecognized pixel shader version %u.%u\n",
2172                     shader->baseShader.reg_maps.shader_version.major,
2173                     shader->baseShader.reg_maps.shader_version.minor);
2174     }
2175 }
2176
2177 HRESULT pixelshader_init(IWineD3DPixelShaderImpl *shader, IWineD3DDeviceImpl *device,
2178         const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
2179         IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
2180 {
2181     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2182     unsigned int i, highest_reg_used = 0, num_regs_used = 0;
2183     HRESULT hr;
2184
2185     if (!byte_code) return WINED3DERR_INVALIDCALL;
2186
2187     shader->lpVtbl = &IWineD3DPixelShader_Vtbl;
2188     shader_init(&shader->baseShader, device, parent, parent_ops);
2189
2190     hr = shader_set_function((IWineD3DBaseShaderImpl *)shader, byte_code,
2191             output_signature, device->d3d_pshader_constantF);
2192     if (FAILED(hr))
2193     {
2194         WARN("Failed to set function, hr %#x.\n", hr);
2195         shader_cleanup((IWineD3DBaseShader *)shader);
2196         return hr;
2197     }
2198
2199     pixelshader_set_limits(shader);
2200
2201     for (i = 0; i < MAX_REG_INPUT; ++i)
2202     {
2203         if (shader->input_reg_used[i])
2204         {
2205             ++num_regs_used;
2206             highest_reg_used = i;
2207         }
2208     }
2209
2210     /* Don't do any register mapping magic if it is not needed, or if we can't
2211      * achieve anything anyway */
2212     if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
2213             || num_regs_used > (gl_info->limits.glsl_varyings / 4))
2214     {
2215         if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
2216         {
2217             /* This happens with relative addressing. The input mapper function
2218              * warns about this if the higher registers are declared too, so
2219              * don't write a FIXME here */
2220             WARN("More varying registers used than supported\n");
2221         }
2222
2223         for (i = 0; i < MAX_REG_INPUT; ++i)
2224         {
2225             shader->input_reg_map[i] = i;
2226         }
2227
2228         shader->declared_in_count = highest_reg_used + 1;
2229     }
2230     else
2231     {
2232         shader->declared_in_count = 0;
2233         for (i = 0; i < MAX_REG_INPUT; ++i)
2234         {
2235             if (shader->input_reg_used[i]) shader->input_reg_map[i] = shader->declared_in_count++;
2236             else shader->input_reg_map[i] = ~0U;
2237         }
2238     }
2239
2240     shader->baseShader.load_local_constsF = FALSE;
2241
2242     return WINED3D_OK;
2243 }
2244
2245 void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures)
2246 {
2247     WINED3DSAMPLER_TEXTURE_TYPE *sampler_type = reg_maps->sampler_type;
2248     unsigned int i;
2249
2250     if (reg_maps->shader_version.major != 1) return;
2251
2252     for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
2253     {
2254         /* We don't sample from this sampler. */
2255         if (!sampler_type[i]) continue;
2256
2257         if (!textures[i])
2258         {
2259             WARN("No texture bound to sampler %u, using 2D.\n", i);
2260             sampler_type[i] = WINED3DSTT_2D;
2261             continue;
2262         }
2263
2264         switch (IWineD3DBaseTexture_GetTextureDimensions(textures[i]))
2265         {
2266             case GL_TEXTURE_RECTANGLE_ARB:
2267             case GL_TEXTURE_2D:
2268                 /* We have to select between texture rectangles and 2D
2269                  * textures later because 2.0 and 3.0 shaders only have
2270                  * WINED3DSTT_2D as well. */
2271                 sampler_type[i] = WINED3DSTT_2D;
2272                 break;
2273
2274             case GL_TEXTURE_3D:
2275                 sampler_type[i] = WINED3DSTT_VOLUME;
2276                 break;
2277
2278             case GL_TEXTURE_CUBE_MAP_ARB:
2279                 sampler_type[i] = WINED3DSTT_CUBE;
2280                 break;
2281
2282             default:
2283                 FIXME("Unrecognized texture type %#x, using 2D.\n",
2284                         IWineD3DBaseTexture_GetTextureDimensions(textures[i]));
2285                 sampler_type[i] = WINED3DSTT_2D;
2286         }
2287     }
2288 }