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