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