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