x3daudio1_5: Add stub dll.
[wine.git] / dlls / wined3d / shader.c
blob28f3b179f595bad92eb2f904f6045098b8146a6e
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, 2013 Stefan Dösinger for CodeWeavers
8 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
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.
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.
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <math.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 /* pow, mul_high, sub_high, mul_low */
37 const float wined3d_srgb_const0[] = {0.41666f, 1.055f, 0.055f, 12.92f};
38 /* cmp */
39 const float wined3d_srgb_const1[] = {0.0031308f, 0.0f, 0.0f, 0.0f};
41 static const char * const shader_opcode_names[] =
43 /* WINED3DSIH_ABS */ "abs",
44 /* WINED3DSIH_ADD */ "add",
45 /* WINED3DSIH_AND */ "and",
46 /* WINED3DSIH_BEM */ "bem",
47 /* WINED3DSIH_BREAK */ "break",
48 /* WINED3DSIH_BREAKC */ "breakc",
49 /* WINED3DSIH_BREAKP */ "breakp",
50 /* WINED3DSIH_CALL */ "call",
51 /* WINED3DSIH_CALLNZ */ "callnz",
52 /* WINED3DSIH_CMP */ "cmp",
53 /* WINED3DSIH_CND */ "cnd",
54 /* WINED3DSIH_CRS */ "crs",
55 /* WINED3DSIH_CUT */ "cut",
56 /* WINED3DSIH_DCL */ "dcl",
57 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
58 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
59 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
60 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
61 /* WINED3DSIH_DEF */ "def",
62 /* WINED3DSIH_DEFB */ "defb",
63 /* WINED3DSIH_DEFI */ "defi",
64 /* WINED3DSIH_DIV */ "div",
65 /* WINED3DSIH_DP2 */ "dp2",
66 /* WINED3DSIH_DP2ADD */ "dp2add",
67 /* WINED3DSIH_DP3 */ "dp3",
68 /* WINED3DSIH_DP4 */ "dp4",
69 /* WINED3DSIH_DST */ "dst",
70 /* WINED3DSIH_DSX */ "dsx",
71 /* WINED3DSIH_DSY */ "dsy",
72 /* WINED3DSIH_ELSE */ "else",
73 /* WINED3DSIH_EMIT */ "emit",
74 /* WINED3DSIH_ENDIF */ "endif",
75 /* WINED3DSIH_ENDLOOP */ "endloop",
76 /* WINED3DSIH_ENDREP */ "endrep",
77 /* WINED3DSIH_EQ */ "eq",
78 /* WINED3DSIH_EXP */ "exp",
79 /* WINED3DSIH_EXPP */ "expp",
80 /* WINED3DSIH_FRC */ "frc",
81 /* WINED3DSIH_FTOI */ "ftoi",
82 /* WINED3DSIH_GE */ "ge",
83 /* WINED3DSIH_IADD */ "iadd",
84 /* WINED3DSIH_IEQ */ "ieq",
85 /* WINED3DSIH_IF */ "if",
86 /* WINED3DSIH_IFC */ "ifc",
87 /* WINED3DSIH_IGE */ "ige",
88 /* WINED3DSIH_IMUL */ "imul",
89 /* WINED3DSIH_ISHL */ "ishl",
90 /* WINED3DSIH_ITOF */ "itof",
91 /* WINED3DSIH_LABEL */ "label",
92 /* WINED3DSIH_LD */ "ld",
93 /* WINED3DSIH_LIT */ "lit",
94 /* WINED3DSIH_LOG */ "log",
95 /* WINED3DSIH_LOGP */ "logp",
96 /* WINED3DSIH_LOOP */ "loop",
97 /* WINED3DSIH_LRP */ "lrp",
98 /* WINED3DSIH_LT */ "lt",
99 /* WINED3DSIH_M3x2 */ "m3x2",
100 /* WINED3DSIH_M3x3 */ "m3x3",
101 /* WINED3DSIH_M3x4 */ "m3x4",
102 /* WINED3DSIH_M4x3 */ "m4x3",
103 /* WINED3DSIH_M4x4 */ "m4x4",
104 /* WINED3DSIH_MAD */ "mad",
105 /* WINED3DSIH_MAX */ "max",
106 /* WINED3DSIH_MIN */ "min",
107 /* WINED3DSIH_MOV */ "mov",
108 /* WINED3DSIH_MOVA */ "mova",
109 /* WINED3DSIH_MOVC */ "movc",
110 /* WINED3DSIH_MUL */ "mul",
111 /* WINED3DSIH_NE */ "ne",
112 /* WINED3DSIH_NOP */ "nop",
113 /* WINED3DSIH_NRM */ "nrm",
114 /* WINED3DSIH_OR */ "or",
115 /* WINED3DSIH_PHASE */ "phase",
116 /* WINED3DSIH_POW */ "pow",
117 /* WINED3DSIH_RCP */ "rcp",
118 /* WINED3DSIH_REP */ "rep",
119 /* WINED3DSIH_RET */ "ret",
120 /* WINED3DSIH_ROUND_NI */ "round_ni",
121 /* WINED3DSIH_RSQ */ "rsq",
122 /* WINED3DSIH_SAMPLE */ "sample",
123 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
124 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
125 /* WINED3DSIH_SETP */ "setp",
126 /* WINED3DSIH_SGE */ "sge",
127 /* WINED3DSIH_SGN */ "sgn",
128 /* WINED3DSIH_SINCOS */ "sincos",
129 /* WINED3DSIH_SLT */ "slt",
130 /* WINED3DSIH_SQRT */ "sqrt",
131 /* WINED3DSIH_SUB */ "sub",
132 /* WINED3DSIH_TEX */ "texld",
133 /* WINED3DSIH_TEXBEM */ "texbem",
134 /* WINED3DSIH_TEXBEML */ "texbeml",
135 /* WINED3DSIH_TEXCOORD */ "texcrd",
136 /* WINED3DSIH_TEXDEPTH */ "texdepth",
137 /* WINED3DSIH_TEXDP3 */ "texdp3",
138 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
139 /* WINED3DSIH_TEXKILL */ "texkill",
140 /* WINED3DSIH_TEXLDD */ "texldd",
141 /* WINED3DSIH_TEXLDL */ "texldl",
142 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
143 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
144 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
145 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
146 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
147 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
148 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
149 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
150 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
151 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
152 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
153 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
154 /* WINED3DSIH_UDIV */ "udiv",
155 /* WINED3DSIH_UGE */ "uge",
156 /* WINED3DSIH_USHR */ "ushr",
157 /* WINED3DSIH_UTOF */ "utof",
158 /* WINED3DSIH_XOR */ "xor",
161 static const char * const semantic_names[] =
163 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
164 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
165 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
166 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
167 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
168 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
169 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
170 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
171 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
172 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
173 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
174 /* WINED3D_DECL_USAGE_FOG */ "FOG",
175 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
176 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
179 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage)
181 if (usage >= sizeof(semantic_names) / sizeof(*semantic_names))
183 FIXME("Unrecognized usage %#x.\n", usage);
184 return "UNRECOGNIZED";
187 return semantic_names[usage];
190 static enum wined3d_decl_usage shader_usage_from_semantic_name(const char *name)
192 unsigned int i;
194 for (i = 0; i < sizeof(semantic_names) / sizeof(*semantic_names); ++i)
196 if (!strcmp(name, semantic_names[i])) return i;
199 return ~0U;
202 BOOL shader_match_semantic(const char *semantic_name, enum wined3d_decl_usage usage)
204 return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
207 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
208 const struct wined3d_shader_semantic *s)
210 e->semantic_name = shader_semantic_name_from_usage(s->usage);
211 e->semantic_idx = s->usage_idx;
212 e->sysval_semantic = 0;
213 e->component_type = 0;
214 e->register_idx = s->reg.reg.idx[0].offset;
215 e->mask = s->reg.write_mask;
218 static void shader_signature_from_usage(struct wined3d_shader_signature_element *e,
219 enum wined3d_decl_usage usage, UINT usage_idx, UINT reg_idx, DWORD write_mask)
221 e->semantic_name = shader_semantic_name_from_usage(usage);
222 e->semantic_idx = usage_idx;
223 e->sysval_semantic = 0;
224 e->component_type = 0;
225 e->register_idx = reg_idx;
226 e->mask = write_mask;
229 static const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token)
231 switch (version_token >> 16)
233 case WINED3D_SM1_VS:
234 case WINED3D_SM1_PS:
235 return &sm1_shader_frontend;
237 case WINED3D_SM4_PS:
238 case WINED3D_SM4_VS:
239 case WINED3D_SM4_GS:
240 return &sm4_shader_frontend;
242 default:
243 FIXME("Unrecognised version token %#x\n", version_token);
244 return NULL;
248 void shader_buffer_clear(struct wined3d_shader_buffer *buffer)
250 buffer->buffer[0] = '\0';
251 buffer->content_size = 0;
252 buffer->lineNo = 0;
253 buffer->newline = TRUE;
256 BOOL shader_buffer_init(struct wined3d_shader_buffer *buffer)
258 buffer->buffer_size = 16384;
259 if (!(buffer->buffer = HeapAlloc(GetProcessHeap(), 0, buffer->buffer_size)))
261 ERR("Failed to allocate shader buffer memory.\n");
262 return FALSE;
265 shader_buffer_clear(buffer);
266 return TRUE;
269 void shader_buffer_free(struct wined3d_shader_buffer *buffer)
271 HeapFree(GetProcessHeap(), 0, buffer->buffer);
274 int shader_vaddline(struct wined3d_shader_buffer *buffer, const char *format, va_list args)
276 char *base = buffer->buffer + buffer->content_size;
277 int rc;
278 char *new_buffer;
280 while(1)
282 rc = vsnprintf(base, buffer->buffer_size - buffer->content_size, format, args);
283 if (rc < 0 /* C89 */ || (unsigned int)rc >= buffer->buffer_size - buffer->content_size /* C99 */)
285 new_buffer = HeapReAlloc(GetProcessHeap(), 0, buffer->buffer, buffer->buffer_size * 2);
286 if (!new_buffer)
288 ERR("The buffer allocated for the shader program string is too small at %d bytes.\n", buffer->buffer_size);
289 buffer->content_size = buffer->buffer_size - 1;
290 return -1;
292 buffer->buffer = new_buffer;
293 buffer->buffer_size = buffer->buffer_size * 2;
294 base = buffer->buffer + buffer->content_size;
296 else
298 break;
302 if (buffer->newline)
304 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->content_size, base);
305 buffer->newline = FALSE;
307 else
309 TRACE("%s", base);
312 buffer->content_size += rc;
313 if (buffer->buffer[buffer->content_size-1] == '\n')
315 ++buffer->lineNo;
316 buffer->newline = TRUE;
319 return 0;
322 int shader_addline(struct wined3d_shader_buffer *buffer, const char *format, ...)
324 va_list args;
325 int ret;
327 va_start(args, format);
328 ret = shader_vaddline(buffer, format, args);
329 va_end(args);
331 return ret;
334 static void shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
335 void *parent, const struct wined3d_parent_ops *parent_ops)
337 shader->ref = 1;
338 shader->device = device;
339 shader->parent = parent;
340 shader->parent_ops = parent_ops;
341 list_init(&shader->linked_programs);
342 list_add_head(&device->shaders, &shader->shader_list_entry);
345 /* Convert floating point offset relative to a register file to an absolute
346 * offset for float constants. */
347 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type, UINT register_idx)
349 switch (register_type)
351 case WINED3DSPR_CONST: return register_idx;
352 case WINED3DSPR_CONST2: return 2048 + register_idx;
353 case WINED3DSPR_CONST3: return 4096 + register_idx;
354 case WINED3DSPR_CONST4: return 6144 + register_idx;
355 default:
356 FIXME("Unsupported register type: %u.\n", register_type);
357 return register_idx;
361 static void shader_delete_constant_list(struct list *clist)
363 struct wined3d_shader_lconst *constant;
364 struct list *ptr;
366 ptr = list_head(clist);
367 while (ptr)
369 constant = LIST_ENTRY(ptr, struct wined3d_shader_lconst, entry);
370 ptr = list_next(clist, ptr);
371 HeapFree(GetProcessHeap(), 0, constant);
373 list_init(clist);
376 static void shader_set_limits(struct wined3d_shader *shader)
378 static const struct limits_entry
380 unsigned int min_version;
381 unsigned int max_version;
382 struct wined3d_shader_limits limits;
384 vs_limits[] =
386 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
387 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 1), { 0, 0, 256, 0, 12, 0}},
388 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 1), { 0, 16, 256, 16, 12, 0}},
389 /* DX10 cards on Windows advertise a D3D9 constant limit of 256
390 * even though they are capable of supporting much more (GL
391 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
392 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
393 * shaders to 256. */
394 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), { 4, 16, 256, 16, 12, 0}},
395 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 16, 0}},
398 gs_limits[] =
400 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
401 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 32, 16}},
404 ps_limits[] =
406 /* min_version, max_version, sampler, constant_int, constant_float, constant_bool, packed_output, packed_input */
407 {WINED3D_SHADER_VERSION(1, 0), WINED3D_SHADER_VERSION(1, 3), { 4, 0, 8, 0, 0, 0}},
408 {WINED3D_SHADER_VERSION(1, 4), WINED3D_SHADER_VERSION(1, 4), { 6, 0, 8, 0, 0, 0}},
409 {WINED3D_SHADER_VERSION(2, 0), WINED3D_SHADER_VERSION(2, 0), {16, 0, 32, 0, 0, 0}},
410 {WINED3D_SHADER_VERSION(2, 1), WINED3D_SHADER_VERSION(2, 1), {16, 16, 32, 16, 0, 0}},
411 {WINED3D_SHADER_VERSION(3, 0), WINED3D_SHADER_VERSION(3, 0), {16, 16, 224, 16, 0, 12}},
412 {WINED3D_SHADER_VERSION(4, 0), WINED3D_SHADER_VERSION(4, 0), {16, 0, 0, 0, 0, 32}},
415 const struct limits_entry *limits_array;
416 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
417 shader->reg_maps.shader_version.minor);
418 int i = 0;
420 switch (shader->reg_maps.shader_version.type)
422 default:
423 FIXME("Unexpected shader type %u found.\n", shader->reg_maps.shader_version.type);
424 /* Fall-through. */
425 case WINED3D_SHADER_TYPE_VERTEX:
426 limits_array = vs_limits;
427 break;
428 case WINED3D_SHADER_TYPE_GEOMETRY:
429 limits_array = gs_limits;
430 break;
431 case WINED3D_SHADER_TYPE_PIXEL:
432 limits_array = ps_limits;
433 break;
436 while (limits_array[i].min_version && limits_array[i].min_version <= shader_version)
438 if (shader_version <= limits_array[i].max_version)
440 shader->limits = &limits_array[i].limits;
441 break;
443 ++i;
445 if (!shader->limits)
447 FIXME("Unexpected shader version \"%u.%u\".\n",
448 shader->reg_maps.shader_version.major,
449 shader->reg_maps.shader_version.minor);
450 shader->limits = &limits_array[max(0, i - 1)].limits;
454 static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
456 DWORD idx, shift;
457 idx = bit >> 5;
458 shift = bit & 0x1f;
459 bitmap[idx] |= (1 << shift);
462 static BOOL shader_record_register_usage(struct wined3d_shader *shader, struct wined3d_shader_reg_maps *reg_maps,
463 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type, unsigned int constf_size)
465 switch (reg->type)
467 case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
468 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
469 reg_maps->texcoord |= 1 << reg->idx[0].offset;
470 else
471 reg_maps->address |= 1 << reg->idx[0].offset;
472 break;
474 case WINED3DSPR_TEMP:
475 reg_maps->temporary |= 1 << reg->idx[0].offset;
476 break;
478 case WINED3DSPR_INPUT:
479 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
481 if (reg->idx[0].rel_addr)
483 /* If relative addressing is used, we must assume that all registers
484 * are used. Even if it is a construct like v3[aL], we can't assume
485 * that v0, v1 and v2 aren't read because aL can be negative */
486 unsigned int i;
487 for (i = 0; i < MAX_REG_INPUT; ++i)
489 shader->u.ps.input_reg_used[i] = TRUE;
492 else
494 shader->u.ps.input_reg_used[reg->idx[0].offset] = TRUE;
497 else
498 reg_maps->input_registers |= 1 << reg->idx[0].offset;
499 break;
501 case WINED3DSPR_RASTOUT:
502 if (reg->idx[0].offset == 1)
503 reg_maps->fog = 1;
504 break;
506 case WINED3DSPR_MISCTYPE:
507 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
509 if (!reg->idx[0].offset)
510 reg_maps->vpos = 1;
511 else if (reg->idx[0].offset == 1)
512 reg_maps->usesfacing = 1;
514 break;
516 case WINED3DSPR_CONST:
517 if (reg->idx[0].rel_addr)
519 if (reg->idx[0].offset < reg_maps->min_rel_offset)
520 reg_maps->min_rel_offset = reg->idx[0].offset;
521 if (reg->idx[0].offset > reg_maps->max_rel_offset)
522 reg_maps->max_rel_offset = reg->idx[0].offset;
523 reg_maps->usesrelconstF = TRUE;
525 else
527 if (reg->idx[0].offset >= min(shader->limits->constant_float, constf_size))
529 WARN("Shader using float constant %u which is not supported.\n", reg->idx[0].offset);
530 return FALSE;
532 else
534 set_bitmap_bit(reg_maps->constf, reg->idx[0].offset);
537 break;
539 case WINED3DSPR_CONSTINT:
540 if (reg->idx[0].offset >= shader->limits->constant_int)
542 WARN("Shader using integer constant %u which is not supported.\n", reg->idx[0].offset);
543 return FALSE;
545 else
547 reg_maps->integer_constants |= (1 << reg->idx[0].offset);
549 break;
551 case WINED3DSPR_CONSTBOOL:
552 if (reg->idx[0].offset >= shader->limits->constant_bool)
554 WARN("Shader using bool constant %u which is not supported.\n", reg->idx[0].offset);
555 return FALSE;
557 else
559 reg_maps->boolean_constants |= (1 << reg->idx[0].offset);
561 break;
563 case WINED3DSPR_COLOROUT:
564 reg_maps->rt_mask |= (1 << reg->idx[0].offset);
565 break;
567 default:
568 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
569 reg->type, reg->idx[0].offset, reg->idx[1].offset);
570 break;
572 return TRUE;
575 static void shader_record_sample(struct wined3d_shader_reg_maps *reg_maps,
576 unsigned int resource_idx, unsigned int sampler_idx, unsigned int bind_idx)
578 struct wined3d_shader_sampler_map_entry *entries, *entry;
579 struct wined3d_shader_sampler_map *map;
580 unsigned int i;
582 map = &reg_maps->sampler_map;
583 entries = map->entries;
584 for (i = 0; i < map->count; ++i)
586 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
587 return;
590 if (!map->size)
592 if (!(entries = HeapAlloc(GetProcessHeap(), 0, sizeof(*entries) * 4)))
594 ERR("Failed to allocate sampler map entries.\n");
595 return;
597 map->size = 4;
598 map->entries = entries;
600 else if (map->count == map->size)
602 size_t new_size = map->size * 2;
604 if (sizeof(*entries) * new_size <= sizeof(*entries) * map->size
605 || !(entries = HeapReAlloc(GetProcessHeap(), 0, entries, sizeof(*entries) * new_size)))
607 ERR("Failed to resize sampler map entries.\n");
608 return;
610 map->size = new_size;
611 map->entries = entries;
614 entry = &entries[map->count++];
615 entry->resource_idx = resource_idx;
616 entry->sampler_idx = sampler_idx;
617 entry->bind_idx = bind_idx;
620 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
622 switch (instr)
624 case WINED3DSIH_M4x4:
625 case WINED3DSIH_M3x4:
626 return param == 1 ? 3 : 0;
628 case WINED3DSIH_M4x3:
629 case WINED3DSIH_M3x3:
630 return param == 1 ? 2 : 0;
632 case WINED3DSIH_M3x2:
633 return param == 1 ? 1 : 0;
635 default:
636 return 0;
640 /* Note that this does not count the loop register as an address register. */
641 static HRESULT shader_get_registers_used(struct wined3d_shader *shader, const struct wined3d_shader_frontend *fe,
642 struct wined3d_shader_reg_maps *reg_maps, struct wined3d_shader_signature_element *input_signature,
643 struct wined3d_shader_signature_element *output_signature, const DWORD *byte_code, DWORD constf_size)
645 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
646 void *fe_data = shader->frontend_data;
647 struct wined3d_shader_version shader_version;
648 const DWORD *ptr = byte_code;
650 memset(reg_maps, 0, sizeof(*reg_maps));
651 reg_maps->min_rel_offset = ~0U;
653 fe->shader_read_header(fe_data, &ptr, &shader_version);
654 reg_maps->shader_version = shader_version;
656 shader_set_limits(shader);
658 reg_maps->constf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
659 sizeof(*reg_maps->constf) * ((min(shader->limits->constant_float, constf_size) + 31) / 32));
660 if (!reg_maps->constf)
662 ERR("Failed to allocate constant map memory.\n");
663 return E_OUTOFMEMORY;
666 while (!fe->shader_is_end(fe_data, &ptr))
668 struct wined3d_shader_instruction ins;
670 /* Fetch opcode. */
671 fe->shader_read_instruction(fe_data, &ptr, &ins);
673 /* Unhandled opcode, and its parameters. */
674 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
676 TRACE("Skipping unrecognized instruction.\n");
677 continue;
680 /* Handle declarations. */
681 if (ins.handler_idx == WINED3DSIH_DCL)
683 struct wined3d_shader_semantic *semantic = &ins.declaration.semantic;
684 unsigned int reg_idx = semantic->reg.reg.idx[0].offset;
686 switch (semantic->reg.reg.type)
688 /* Mark input registers used. */
689 case WINED3DSPR_INPUT:
690 if (reg_idx >= MAX_REG_INPUT)
692 ERR("Invalid input register index %u.\n", reg_idx);
693 break;
695 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL && shader_version.major == 3
696 && semantic->usage == WINED3D_DECL_USAGE_POSITION && !semantic->usage_idx)
697 return WINED3DERR_INVALIDCALL;
698 reg_maps->input_registers |= 1 << reg_idx;
699 shader_signature_from_semantic(&input_signature[reg_idx], semantic);
700 break;
702 /* Vertex shader: mark 3.0 output registers used, save token. */
703 case WINED3DSPR_OUTPUT:
704 if (reg_idx >= MAX_REG_OUTPUT)
706 ERR("Invalid output register index %u.\n", reg_idx);
707 break;
709 reg_maps->output_registers |= 1 << reg_idx;
710 shader_signature_from_semantic(&output_signature[reg_idx], semantic);
711 if (semantic->usage == WINED3D_DECL_USAGE_FOG)
712 reg_maps->fog = 1;
713 break;
715 case WINED3DSPR_SAMPLER:
716 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
717 case WINED3DSPR_RESOURCE:
718 if (reg_idx >= ARRAY_SIZE(reg_maps->resource_info))
720 ERR("Invalid resource index %u.\n", reg_idx);
721 break;
723 reg_maps->resource_info[reg_idx].type = semantic->resource_type;
724 reg_maps->resource_info[reg_idx].data_type = semantic->resource_data_type;
725 break;
727 default:
728 TRACE("Not recording DCL register type %#x.\n", semantic->reg.reg.type);
729 break;
732 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
734 struct wined3d_shader_register *reg = &ins.declaration.src.reg;
735 if (reg->idx[0].offset >= WINED3D_MAX_CBS)
736 ERR("Invalid CB index %u.\n", reg->idx[0].offset);
737 else
738 reg_maps->cb_sizes[reg->idx[0].offset] = reg->idx[1].offset;
740 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE)
742 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
743 shader->u.gs.input_type = ins.declaration.primitive_type;
744 else
745 FIXME("Invalid instruction %#x for shader type %#x.\n",
746 ins.handler_idx, shader_version.type);
748 else if (ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
750 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
751 shader->u.gs.output_type = ins.declaration.primitive_type;
752 else
753 FIXME("Invalid instruction %#x for shader type %#x.\n",
754 ins.handler_idx, shader_version.type);
756 else if (ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
758 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
759 shader->u.gs.vertices_out = ins.declaration.count;
760 else
761 FIXME("Invalid instruction %#x for shader type %#x.\n",
762 ins.handler_idx, shader_version.type);
764 else if (ins.handler_idx == WINED3DSIH_DEF)
766 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
767 float *value;
768 if (!lconst) return E_OUTOFMEMORY;
770 lconst->idx = ins.dst[0].reg.idx[0].offset;
771 memcpy(lconst->value, ins.src[0].reg.immconst_data, 4 * sizeof(DWORD));
772 value = (float *)lconst->value;
774 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
775 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
777 if (value[0] < -1.0f) value[0] = -1.0f;
778 else if (value[0] > 1.0f) value[0] = 1.0f;
779 if (value[1] < -1.0f) value[1] = -1.0f;
780 else if (value[1] > 1.0f) value[1] = 1.0f;
781 if (value[2] < -1.0f) value[2] = -1.0f;
782 else if (value[2] > 1.0f) value[2] = 1.0f;
783 if (value[3] < -1.0f) value[3] = -1.0f;
784 else if (value[3] > 1.0f) value[3] = 1.0f;
787 list_add_head(&shader->constantsF, &lconst->entry);
789 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
790 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
792 shader->lconst_inf_or_nan = TRUE;
795 else if (ins.handler_idx == WINED3DSIH_DEFI)
797 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
798 if (!lconst) return E_OUTOFMEMORY;
800 lconst->idx = ins.dst[0].reg.idx[0].offset;
801 memcpy(lconst->value, ins.src[0].reg.immconst_data, 4 * sizeof(DWORD));
803 list_add_head(&shader->constantsI, &lconst->entry);
804 reg_maps->local_int_consts |= (1 << lconst->idx);
806 else if (ins.handler_idx == WINED3DSIH_DEFB)
808 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
809 if (!lconst) return E_OUTOFMEMORY;
811 lconst->idx = ins.dst[0].reg.idx[0].offset;
812 memcpy(lconst->value, ins.src[0].reg.immconst_data, sizeof(DWORD));
814 list_add_head(&shader->constantsB, &lconst->entry);
815 reg_maps->local_bool_consts |= (1 << lconst->idx);
817 /* For subroutine prototypes. */
818 else if (ins.handler_idx == WINED3DSIH_LABEL)
820 reg_maps->labels |= 1 << ins.src[0].reg.idx[0].offset;
822 /* Set texture, address, temporary registers. */
823 else
825 BOOL color0_mov = FALSE;
826 unsigned int i;
828 /* This will loop over all the registers and try to
829 * make a bitmask of the ones we're interested in.
831 * Relative addressing tokens are ignored, but that's
832 * okay, since we'll catch any address registers when
833 * they are initialized (required by spec). */
834 for (i = 0; i < ins.dst_count; ++i)
836 if (!shader_record_register_usage(shader, reg_maps, &ins.dst[i].reg,
837 shader_version.type, constf_size))
838 return WINED3DERR_INVALIDCALL;
840 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
841 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
842 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
843 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX && shader_version.major < 3)
845 UINT idx = ins.dst[i].reg.idx[0].offset;
847 switch (ins.dst[i].reg.type)
849 case WINED3DSPR_RASTOUT:
850 switch (idx)
852 case 0: /* oPos */
853 reg_maps->output_registers |= 1 << 10;
854 shader_signature_from_usage(&output_signature[10],
855 WINED3D_DECL_USAGE_POSITION, 0, 10, WINED3DSP_WRITEMASK_ALL);
856 break;
858 case 1: /* oFog */
859 reg_maps->output_registers |= 1 << 11;
860 shader_signature_from_usage(&output_signature[11],
861 WINED3D_DECL_USAGE_FOG, 0, 11, WINED3DSP_WRITEMASK_0);
862 break;
864 case 2: /* oPts */
865 reg_maps->output_registers |= 1 << 11;
866 shader_signature_from_usage(&output_signature[11],
867 WINED3D_DECL_USAGE_PSIZE, 0, 11, WINED3DSP_WRITEMASK_1);
868 break;
870 break;
872 case WINED3DSPR_ATTROUT:
873 if (idx < 2)
875 idx += 8;
876 if (reg_maps->output_registers & (1 << idx))
878 output_signature[idx].mask |= ins.dst[i].write_mask;
880 else
882 reg_maps->output_registers |= 1 << idx;
883 shader_signature_from_usage(&output_signature[idx],
884 WINED3D_DECL_USAGE_COLOR, idx - 8, idx, ins.dst[i].write_mask);
887 break;
889 case WINED3DSPR_TEXCRDOUT:
891 reg_maps->texcoord_mask[idx] |= ins.dst[i].write_mask;
892 if (reg_maps->output_registers & (1 << idx))
894 output_signature[idx].mask |= ins.dst[i].write_mask;
896 else
898 reg_maps->output_registers |= 1 << idx;
899 shader_signature_from_usage(&output_signature[idx],
900 WINED3D_DECL_USAGE_TEXCOORD, idx, idx, ins.dst[i].write_mask);
902 break;
904 default:
905 break;
909 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
911 if (ins.dst[i].reg.type == WINED3DSPR_COLOROUT && !ins.dst[i].reg.idx[0].offset)
913 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
914 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
915 * the mov and perform the sRGB write correction from the source register.
917 * However, if the mov is only partial, we can't do this, and if the write
918 * comes from an instruction other than MOV it is hard to do as well. If
919 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
920 shader->u.ps.color0_mov = FALSE;
921 if (ins.handler_idx == WINED3DSIH_MOV
922 && ins.dst[i].write_mask == WINED3DSP_WRITEMASK_ALL)
924 /* Used later when the source register is read. */
925 color0_mov = TRUE;
928 /* Also drop the MOV marker if the source register is overwritten prior to the shader
929 * end
931 else if (ins.dst[i].reg.type == WINED3DSPR_TEMP
932 && ins.dst[i].reg.idx[0].offset == shader->u.ps.color0_reg)
934 shader->u.ps.color0_mov = FALSE;
938 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
939 if (shader_version.major == 1
940 && (ins.handler_idx == WINED3DSIH_TEX
941 || ins.handler_idx == WINED3DSIH_TEXBEM
942 || ins.handler_idx == WINED3DSIH_TEXBEML
943 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
944 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
945 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
946 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
947 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
948 || ins.handler_idx == WINED3DSIH_TEXREG2AR
949 || ins.handler_idx == WINED3DSIH_TEXREG2GB
950 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
952 unsigned int reg_idx = ins.dst[i].reg.idx[0].offset;
954 TRACE("Setting fake 2D resource for 1.x pixelshader.\n");
955 reg_maps->resource_info[reg_idx].type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
956 reg_maps->resource_info[reg_idx].data_type = WINED3D_DATA_FLOAT;
957 shader_record_sample(reg_maps, reg_idx, reg_idx, reg_idx);
959 /* texbem is only valid with < 1.4 pixel shaders */
960 if (ins.handler_idx == WINED3DSIH_TEXBEM
961 || ins.handler_idx == WINED3DSIH_TEXBEML)
963 reg_maps->bumpmat |= 1 << reg_idx;
964 if (ins.handler_idx == WINED3DSIH_TEXBEML)
966 reg_maps->luminanceparams |= 1 << reg_idx;
970 else if (ins.handler_idx == WINED3DSIH_BEM)
972 reg_maps->bumpmat |= 1 << ins.dst[i].reg.idx[0].offset;
976 if (ins.handler_idx == WINED3DSIH_NRM) reg_maps->usesnrm = 1;
977 else if (ins.handler_idx == WINED3DSIH_DSY) reg_maps->usesdsy = 1;
978 else if (ins.handler_idx == WINED3DSIH_DSX) reg_maps->usesdsx = 1;
979 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
980 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
981 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
982 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
983 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
984 else if (ins.handler_idx == WINED3DSIH_POW) reg_maps->usespow = 1;
985 else if (ins.handler_idx == WINED3DSIH_LOOP
986 || ins.handler_idx == WINED3DSIH_REP)
988 ++cur_loop_depth;
989 if (cur_loop_depth > max_loop_depth)
990 max_loop_depth = cur_loop_depth;
992 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
993 || ins.handler_idx == WINED3DSIH_ENDREP)
995 --cur_loop_depth;
997 else if (ins.handler_idx == WINED3DSIH_SAMPLE
998 || ins.handler_idx == WINED3DSIH_SAMPLE_GRAD
999 || ins.handler_idx == WINED3DSIH_SAMPLE_LOD)
1001 shader_record_sample(reg_maps, ins.src[1].reg.idx[0].offset,
1002 ins.src[2].reg.idx[0].offset, reg_maps->sampler_map.count);
1005 if (ins.predicate)
1006 if (!shader_record_register_usage(shader, reg_maps, &ins.predicate->reg,
1007 shader_version.type, constf_size))
1008 return WINED3DERR_INVALIDCALL;
1010 for (i = 0; i < ins.src_count; ++i)
1012 unsigned int count = get_instr_extra_regcount(ins.handler_idx, i);
1013 struct wined3d_shader_register reg = ins.src[i].reg;
1015 if (!shader_record_register_usage(shader, reg_maps, &ins.src[i].reg,
1016 shader_version.type, constf_size))
1017 return WINED3DERR_INVALIDCALL;
1018 while (count)
1020 ++reg.idx[0].offset;
1021 if (!shader_record_register_usage(shader, reg_maps, &reg,
1022 shader_version.type, constf_size))
1023 return WINED3DERR_INVALIDCALL;
1024 --count;
1027 if (color0_mov)
1029 if (ins.src[i].reg.type == WINED3DSPR_TEMP
1030 && ins.src[i].swizzle == WINED3DSP_NOSWIZZLE)
1032 shader->u.ps.color0_mov = TRUE;
1033 shader->u.ps.color0_reg = ins.src[i].reg.idx[0].offset;
1039 reg_maps->loop_depth = max_loop_depth;
1041 /* PS before 2.0 don't have explicit color outputs. Instead the value of
1042 * R0 is written to the render target. */
1043 if (shader_version.major < 2 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1044 reg_maps->rt_mask |= (1 << 0);
1046 shader->functionLength = ((const char *)ptr - (const char *)byte_code);
1048 return WINED3D_OK;
1051 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
1053 DWORD map = 1 << max;
1054 map |= map - 1;
1055 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
1057 return wined3d_log2i(map);
1060 static void shader_dump_decl_usage(const struct wined3d_shader_semantic *semantic,
1061 const struct wined3d_shader_version *shader_version)
1063 TRACE("dcl");
1065 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
1067 switch (semantic->resource_type)
1069 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1070 TRACE("_2d");
1071 break;
1073 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1074 TRACE("_volume");
1075 break;
1077 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1078 TRACE("_cube");
1079 break;
1081 default:
1082 TRACE("_unknown_ttype(0x%08x)", semantic->resource_type);
1083 break;
1086 else if (semantic->reg.reg.type == WINED3DSPR_RESOURCE)
1088 TRACE("_resource_");
1089 switch (semantic->resource_type)
1091 case WINED3D_SHADER_RESOURCE_BUFFER:
1092 TRACE("buffer");
1093 break;
1095 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1096 TRACE("texture1d");
1097 break;
1099 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1100 TRACE("texture2d");
1101 break;
1103 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
1104 TRACE("texture2dms");
1105 break;
1107 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1108 TRACE("texture3d");
1109 break;
1111 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1112 TRACE("texturecube");
1113 break;
1115 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
1116 TRACE("texture1darray");
1117 break;
1119 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
1120 TRACE("texture2darray");
1121 break;
1123 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
1124 TRACE("texture2dmsarray");
1125 break;
1127 default:
1128 TRACE("unknown");
1129 break;
1131 switch (semantic->resource_data_type)
1133 case WINED3D_DATA_FLOAT:
1134 TRACE(" (float)");
1135 break;
1137 case WINED3D_DATA_INT:
1138 TRACE(" (int)");
1139 break;
1141 case WINED3D_DATA_UINT:
1142 TRACE(" (uint)");
1143 break;
1145 case WINED3D_DATA_UNORM:
1146 TRACE(" (unorm)");
1147 break;
1149 case WINED3D_DATA_SNORM:
1150 TRACE(" (snorm)");
1151 break;
1153 default:
1154 TRACE(" (unknown)");
1155 break;
1158 else
1160 /* Pixel shaders 3.0 don't have usage semantics. */
1161 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL) return;
1162 else TRACE("_");
1164 switch (semantic->usage)
1166 case WINED3D_DECL_USAGE_POSITION:
1167 TRACE("position%u", semantic->usage_idx);
1168 break;
1170 case WINED3D_DECL_USAGE_BLEND_INDICES:
1171 TRACE("blend");
1172 break;
1174 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
1175 TRACE("weight");
1176 break;
1178 case WINED3D_DECL_USAGE_NORMAL:
1179 TRACE("normal%u", semantic->usage_idx);
1180 break;
1182 case WINED3D_DECL_USAGE_PSIZE:
1183 TRACE("psize");
1184 break;
1186 case WINED3D_DECL_USAGE_COLOR:
1187 if (!semantic->usage_idx) TRACE("color");
1188 else TRACE("specular%u", (semantic->usage_idx - 1));
1189 break;
1191 case WINED3D_DECL_USAGE_TEXCOORD:
1192 TRACE("texture%u", semantic->usage_idx);
1193 break;
1195 case WINED3D_DECL_USAGE_TANGENT:
1196 TRACE("tangent");
1197 break;
1199 case WINED3D_DECL_USAGE_BINORMAL:
1200 TRACE("binormal");
1201 break;
1203 case WINED3D_DECL_USAGE_TESS_FACTOR:
1204 TRACE("tessfactor");
1205 break;
1207 case WINED3D_DECL_USAGE_POSITIONT:
1208 TRACE("positionT%u", semantic->usage_idx);
1209 break;
1211 case WINED3D_DECL_USAGE_FOG:
1212 TRACE("fog");
1213 break;
1215 case WINED3D_DECL_USAGE_DEPTH:
1216 TRACE("depth");
1217 break;
1219 case WINED3D_DECL_USAGE_SAMPLE:
1220 TRACE("sample");
1221 break;
1223 default:
1224 FIXME("unknown_semantics(0x%08x)", semantic->usage);
1229 static void shader_dump_register(const struct wined3d_shader_register *reg,
1230 const struct wined3d_shader_version *shader_version)
1232 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
1233 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
1234 UINT offset = reg->idx[0].offset;
1236 switch (reg->type)
1238 case WINED3DSPR_TEMP:
1239 TRACE("r");
1240 break;
1242 case WINED3DSPR_INPUT:
1243 TRACE("v");
1244 break;
1246 case WINED3DSPR_CONST:
1247 case WINED3DSPR_CONST2:
1248 case WINED3DSPR_CONST3:
1249 case WINED3DSPR_CONST4:
1250 TRACE("c");
1251 offset = shader_get_float_offset(reg->type, offset);
1252 break;
1254 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
1255 TRACE("%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
1256 break;
1258 case WINED3DSPR_RASTOUT:
1259 TRACE("%s", rastout_reg_names[offset]);
1260 break;
1262 case WINED3DSPR_COLOROUT:
1263 TRACE("oC");
1264 break;
1266 case WINED3DSPR_DEPTHOUT:
1267 TRACE("oDepth");
1268 break;
1270 case WINED3DSPR_ATTROUT:
1271 TRACE("oD");
1272 break;
1274 case WINED3DSPR_TEXCRDOUT:
1275 /* Vertex shaders >= 3.0 use general purpose output registers
1276 * (WINED3DSPR_OUTPUT), which can include an address token. */
1277 if (shader_version->major >= 3) TRACE("o");
1278 else TRACE("oT");
1279 break;
1281 case WINED3DSPR_CONSTINT:
1282 TRACE("i");
1283 break;
1285 case WINED3DSPR_CONSTBOOL:
1286 TRACE("b");
1287 break;
1289 case WINED3DSPR_LABEL:
1290 TRACE("l");
1291 break;
1293 case WINED3DSPR_LOOP:
1294 TRACE("aL");
1295 break;
1297 case WINED3DSPR_SAMPLER:
1298 TRACE("s");
1299 break;
1301 case WINED3DSPR_MISCTYPE:
1302 if (offset > 1)
1303 FIXME("Unhandled misctype register %u.\n", offset);
1304 else
1305 TRACE("%s", misctype_reg_names[offset]);
1306 break;
1308 case WINED3DSPR_PREDICATE:
1309 TRACE("p");
1310 break;
1312 case WINED3DSPR_IMMCONST:
1313 TRACE("l");
1314 break;
1316 case WINED3DSPR_CONSTBUFFER:
1317 TRACE("cb");
1318 break;
1320 case WINED3DSPR_PRIMID:
1321 TRACE("primID");
1322 break;
1324 case WINED3DSPR_NULL:
1325 TRACE("null");
1326 break;
1328 case WINED3DSPR_RESOURCE:
1329 TRACE("t");
1330 break;
1332 default:
1333 TRACE("unhandled_rtype(%#x)", reg->type);
1334 break;
1337 if (reg->type == WINED3DSPR_IMMCONST)
1339 TRACE("(");
1340 switch (reg->immconst_type)
1342 case WINED3D_IMMCONST_SCALAR:
1343 switch (reg->data_type)
1345 case WINED3D_DATA_FLOAT:
1346 TRACE("%.8e", *(const float *)reg->immconst_data);
1347 break;
1348 case WINED3D_DATA_INT:
1349 TRACE("%d", reg->immconst_data[0]);
1350 break;
1351 case WINED3D_DATA_RESOURCE:
1352 case WINED3D_DATA_SAMPLER:
1353 case WINED3D_DATA_UINT:
1354 TRACE("%u", reg->immconst_data[0]);
1355 break;
1356 default:
1357 TRACE("<unhandled data type %#x>", reg->data_type);
1358 break;
1360 break;
1362 case WINED3D_IMMCONST_VEC4:
1363 switch (reg->data_type)
1365 case WINED3D_DATA_FLOAT:
1366 TRACE("%.8e, %.8e, %.8e, %.8e",
1367 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
1368 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
1369 break;
1370 case WINED3D_DATA_INT:
1371 TRACE("%d, %d, %d, %d",
1372 reg->immconst_data[0], reg->immconst_data[1],
1373 reg->immconst_data[2], reg->immconst_data[3]);
1374 break;
1375 case WINED3D_DATA_RESOURCE:
1376 case WINED3D_DATA_SAMPLER:
1377 case WINED3D_DATA_UINT:
1378 TRACE("%u, %u, %u, %u",
1379 reg->immconst_data[0], reg->immconst_data[1],
1380 reg->immconst_data[2], reg->immconst_data[3]);
1381 break;
1382 default:
1383 TRACE("<unhandled data type %#x>", reg->data_type);
1384 break;
1386 break;
1388 default:
1389 TRACE("<unhandled immconst_type %#x>", reg->immconst_type);
1390 break;
1392 TRACE(")");
1394 else if (reg->type != WINED3DSPR_RASTOUT
1395 && reg->type != WINED3DSPR_MISCTYPE
1396 && reg->type != WINED3DSPR_NULL)
1398 if (offset != ~0U)
1400 TRACE("[");
1401 if (reg->idx[0].rel_addr)
1403 shader_dump_src_param(reg->idx[0].rel_addr, shader_version);
1404 TRACE(" + ");
1406 TRACE("%u]", offset);
1408 if (reg->idx[1].offset != ~0U)
1410 TRACE("[");
1411 if (reg->idx[1].rel_addr)
1413 shader_dump_src_param(reg->idx[1].rel_addr, shader_version);
1414 TRACE(" + ");
1416 TRACE("%u]", reg->idx[1].offset);
1422 void shader_dump_dst_param(const struct wined3d_shader_dst_param *param,
1423 const struct wined3d_shader_version *shader_version)
1425 DWORD write_mask = param->write_mask;
1427 shader_dump_register(&param->reg, shader_version);
1429 if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
1431 static const char write_mask_chars[] = "xyzw";
1433 TRACE(".");
1434 if (write_mask & WINED3DSP_WRITEMASK_0) TRACE("%c", write_mask_chars[0]);
1435 if (write_mask & WINED3DSP_WRITEMASK_1) TRACE("%c", write_mask_chars[1]);
1436 if (write_mask & WINED3DSP_WRITEMASK_2) TRACE("%c", write_mask_chars[2]);
1437 if (write_mask & WINED3DSP_WRITEMASK_3) TRACE("%c", write_mask_chars[3]);
1441 void shader_dump_src_param(const struct wined3d_shader_src_param *param,
1442 const struct wined3d_shader_version *shader_version)
1444 enum wined3d_shader_src_modifier src_modifier = param->modifiers;
1445 DWORD swizzle = param->swizzle;
1447 if (src_modifier == WINED3DSPSM_NEG
1448 || src_modifier == WINED3DSPSM_BIASNEG
1449 || src_modifier == WINED3DSPSM_SIGNNEG
1450 || src_modifier == WINED3DSPSM_X2NEG
1451 || src_modifier == WINED3DSPSM_ABSNEG)
1452 TRACE("-");
1453 else if (src_modifier == WINED3DSPSM_COMP)
1454 TRACE("1-");
1455 else if (src_modifier == WINED3DSPSM_NOT)
1456 TRACE("!");
1458 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
1459 TRACE("abs(");
1461 shader_dump_register(&param->reg, shader_version);
1463 if (src_modifier)
1465 switch (src_modifier)
1467 case WINED3DSPSM_NONE: break;
1468 case WINED3DSPSM_NEG: break;
1469 case WINED3DSPSM_NOT: break;
1470 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
1471 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
1472 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
1473 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
1474 case WINED3DSPSM_COMP: break;
1475 case WINED3DSPSM_X2: TRACE("_x2"); break;
1476 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
1477 case WINED3DSPSM_DZ: TRACE("_dz"); break;
1478 case WINED3DSPSM_DW: TRACE("_dw"); break;
1479 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
1480 case WINED3DSPSM_ABS: TRACE(")"); break;
1481 default: TRACE("_unknown_modifier(%#x)", src_modifier);
1485 if (swizzle != WINED3DSP_NOSWIZZLE)
1487 static const char swizzle_chars[] = "xyzw";
1488 DWORD swizzle_x = swizzle & 0x03;
1489 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1490 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1491 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1493 if (swizzle_x == swizzle_y
1494 && swizzle_x == swizzle_z
1495 && swizzle_x == swizzle_w)
1497 TRACE(".%c", swizzle_chars[swizzle_x]);
1499 else
1501 TRACE(".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
1502 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
1507 /* Shared code in order to generate the bulk of the shader string.
1508 * NOTE: A description of how to parse tokens can be found on MSDN. */
1509 void shader_generate_main(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
1510 const struct wined3d_shader_reg_maps *reg_maps, const DWORD *byte_code, void *backend_ctx)
1512 struct wined3d_device *device = shader->device;
1513 const struct wined3d_shader_frontend *fe = shader->frontend;
1514 void *fe_data = shader->frontend_data;
1515 struct wined3d_shader_version shader_version;
1516 struct wined3d_shader_loop_state loop_state;
1517 struct wined3d_shader_instruction ins;
1518 struct wined3d_shader_tex_mx tex_mx;
1519 struct wined3d_shader_context ctx;
1520 const DWORD *ptr = byte_code;
1522 /* Initialize current parsing state. */
1523 tex_mx.current_row = 0;
1524 loop_state.current_depth = 0;
1525 loop_state.current_reg = 0;
1527 ctx.shader = shader;
1528 ctx.gl_info = &device->adapter->gl_info;
1529 ctx.reg_maps = reg_maps;
1530 ctx.buffer = buffer;
1531 ctx.tex_mx = &tex_mx;
1532 ctx.loop_state = &loop_state;
1533 ctx.backend_data = backend_ctx;
1534 ins.ctx = &ctx;
1536 fe->shader_read_header(fe_data, &ptr, &shader_version);
1538 while (!fe->shader_is_end(fe_data, &ptr))
1540 /* Read opcode. */
1541 fe->shader_read_instruction(fe_data, &ptr, &ins);
1543 /* Unknown opcode and its parameters. */
1544 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1546 TRACE("Skipping unrecognized instruction.\n");
1547 continue;
1550 if (ins.predicate)
1551 FIXME("Predicates not implemented.\n");
1553 /* Call appropriate function for output target */
1554 device->shader_backend->shader_handle_instruction(&ins);
1558 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param *dst)
1560 DWORD mmask = dst->modifiers;
1562 switch (dst->shift)
1564 case 0: break;
1565 case 13: TRACE("_d8"); break;
1566 case 14: TRACE("_d4"); break;
1567 case 15: TRACE("_d2"); break;
1568 case 1: TRACE("_x2"); break;
1569 case 2: TRACE("_x4"); break;
1570 case 3: TRACE("_x8"); break;
1571 default: TRACE("_unhandled_shift(%d)", dst->shift); break;
1574 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
1575 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
1576 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
1578 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
1579 if (mmask) FIXME("_unrecognized_modifier(%#x)", mmask);
1582 static void shader_dump_primitive_type(enum wined3d_primitive_type primitive_type)
1584 switch (primitive_type)
1586 case WINED3D_PT_UNDEFINED:
1587 TRACE("undefined");
1588 break;
1589 case WINED3D_PT_POINTLIST:
1590 TRACE("pointlist");
1591 break;
1592 case WINED3D_PT_LINELIST:
1593 TRACE("linelist");
1594 break;
1595 case WINED3D_PT_LINESTRIP:
1596 TRACE("linestrip");
1597 break;
1598 case WINED3D_PT_TRIANGLELIST:
1599 TRACE("trianglelist");
1600 break;
1601 case WINED3D_PT_TRIANGLESTRIP:
1602 TRACE("trianglestrip");
1603 break;
1604 case WINED3D_PT_TRIANGLEFAN:
1605 TRACE("trianglefan");
1606 break;
1607 case WINED3D_PT_LINELIST_ADJ:
1608 TRACE("linelist_adj");
1609 break;
1610 case WINED3D_PT_LINESTRIP_ADJ:
1611 TRACE("linestrip_adj");
1612 break;
1613 case WINED3D_PT_TRIANGLELIST_ADJ:
1614 TRACE("trianglelist_adj");
1615 break;
1616 case WINED3D_PT_TRIANGLESTRIP_ADJ:
1617 TRACE("trianglestrip_adj");
1618 break;
1619 default:
1620 TRACE("<unrecognized_primitive_type %#x>", primitive_type);
1621 break;
1625 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *byte_code)
1627 struct wined3d_shader_version shader_version;
1628 const DWORD *ptr = byte_code;
1629 const char *type_prefix;
1630 DWORD i;
1632 TRACE("Parsing %p.\n", byte_code);
1634 fe->shader_read_header(fe_data, &ptr, &shader_version);
1636 switch (shader_version.type)
1638 case WINED3D_SHADER_TYPE_VERTEX:
1639 type_prefix = "vs";
1640 break;
1642 case WINED3D_SHADER_TYPE_GEOMETRY:
1643 type_prefix = "gs";
1644 break;
1646 case WINED3D_SHADER_TYPE_PIXEL:
1647 type_prefix = "ps";
1648 break;
1650 default:
1651 FIXME("Unhandled shader type %#x.\n", shader_version.type);
1652 type_prefix = "unknown";
1653 break;
1656 TRACE("%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
1658 while (!fe->shader_is_end(fe_data, &ptr))
1660 struct wined3d_shader_instruction ins;
1662 fe->shader_read_instruction(fe_data, &ptr, &ins);
1663 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1665 TRACE("Skipping unrecognized instruction.\n");
1666 continue;
1669 if (ins.handler_idx == WINED3DSIH_DCL)
1671 shader_dump_decl_usage(&ins.declaration.semantic, &shader_version);
1672 shader_dump_ins_modifiers(&ins.declaration.semantic.reg);
1673 TRACE(" ");
1674 shader_dump_dst_param(&ins.declaration.semantic.reg, &shader_version);
1676 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
1678 TRACE("%s ", shader_opcode_names[ins.handler_idx]);
1679 shader_dump_src_param(&ins.declaration.src, &shader_version);
1680 TRACE(", %s", ins.flags & WINED3DSI_INDEXED_DYNAMIC ? "dynamicIndexed" : "immediateIndexed");
1682 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE
1683 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
1685 TRACE("%s ", shader_opcode_names[ins.handler_idx]);
1686 shader_dump_primitive_type(ins.declaration.primitive_type);
1688 else if (ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
1690 TRACE("%s %u", shader_opcode_names[ins.handler_idx], ins.declaration.count);
1692 else if (ins.handler_idx == WINED3DSIH_DEF)
1694 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(ins.dst[0].reg.type,
1695 ins.dst[0].reg.idx[0].offset),
1696 *(const float *)&ins.src[0].reg.immconst_data[0],
1697 *(const float *)&ins.src[0].reg.immconst_data[1],
1698 *(const float *)&ins.src[0].reg.immconst_data[2],
1699 *(const float *)&ins.src[0].reg.immconst_data[3]);
1701 else if (ins.handler_idx == WINED3DSIH_DEFI)
1703 TRACE("defi i%u = %d, %d, %d, %d", ins.dst[0].reg.idx[0].offset,
1704 ins.src[0].reg.immconst_data[0],
1705 ins.src[0].reg.immconst_data[1],
1706 ins.src[0].reg.immconst_data[2],
1707 ins.src[0].reg.immconst_data[3]);
1709 else if (ins.handler_idx == WINED3DSIH_DEFB)
1711 TRACE("defb b%u = %s", ins.dst[0].reg.idx[0].offset, ins.src[0].reg.immconst_data[0] ? "true" : "false");
1713 else
1715 if (ins.predicate)
1717 TRACE("(");
1718 shader_dump_src_param(ins.predicate, &shader_version);
1719 TRACE(") ");
1722 /* PixWin marks instructions with the coissue flag with a '+' */
1723 if (ins.coissue) TRACE("+");
1725 TRACE("%s", shader_opcode_names[ins.handler_idx]);
1727 if (ins.handler_idx == WINED3DSIH_IFC
1728 || ins.handler_idx == WINED3DSIH_BREAKC)
1730 switch (ins.flags)
1732 case WINED3D_SHADER_REL_OP_GT: TRACE("_gt"); break;
1733 case WINED3D_SHADER_REL_OP_EQ: TRACE("_eq"); break;
1734 case WINED3D_SHADER_REL_OP_GE: TRACE("_ge"); break;
1735 case WINED3D_SHADER_REL_OP_LT: TRACE("_lt"); break;
1736 case WINED3D_SHADER_REL_OP_NE: TRACE("_ne"); break;
1737 case WINED3D_SHADER_REL_OP_LE: TRACE("_le"); break;
1738 default: TRACE("_(%u)", ins.flags);
1741 else if (ins.handler_idx == WINED3DSIH_TEX
1742 && shader_version.major >= 2
1743 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
1745 TRACE("p");
1748 for (i = 0; i < ins.dst_count; ++i)
1750 shader_dump_ins_modifiers(&ins.dst[i]);
1751 TRACE(!i ? " " : ", ");
1752 shader_dump_dst_param(&ins.dst[i], &shader_version);
1755 /* Other source tokens */
1756 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
1758 TRACE(!i ? " " : ", ");
1759 shader_dump_src_param(&ins.src[i - ins.dst_count], &shader_version);
1762 TRACE("\n");
1766 static void shader_cleanup(struct wined3d_shader *shader)
1768 HeapFree(GetProcessHeap(), 0, shader->signature_strings);
1769 shader->device->shader_backend->shader_destroy(shader);
1770 HeapFree(GetProcessHeap(), 0, shader->reg_maps.constf);
1771 HeapFree(GetProcessHeap(), 0, shader->function);
1772 shader_delete_constant_list(&shader->constantsF);
1773 shader_delete_constant_list(&shader->constantsB);
1774 shader_delete_constant_list(&shader->constantsI);
1775 list_remove(&shader->shader_list_entry);
1777 if (shader->frontend && shader->frontend_data)
1778 shader->frontend->shader_free(shader->frontend_data);
1781 struct shader_none_priv
1783 const struct wined3d_vertex_pipe_ops *vertex_pipe;
1784 const struct fragment_pipeline *fragment_pipe;
1785 BOOL ffp_proj_control;
1788 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
1789 static void shader_none_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
1790 enum tex_types tex_type, const SIZE *ds_mask_size) {}
1791 static void shader_none_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info) {}
1792 static void shader_none_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count) {}
1793 static void shader_none_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count) {}
1794 static void shader_none_load_constants(void *shader_priv, struct wined3d_context *context,
1795 const struct wined3d_state *state) {}
1796 static void shader_none_destroy(struct wined3d_shader *shader) {}
1797 static void shader_none_free_context_data(struct wined3d_context *context) {}
1799 /* Context activation is done by the caller. */
1800 static void shader_none_select(void *shader_priv, struct wined3d_context *context,
1801 const struct wined3d_state *state)
1803 const struct wined3d_gl_info *gl_info = context->gl_info;
1804 struct shader_none_priv *priv = shader_priv;
1806 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
1807 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
1810 /* Context activation is done by the caller. */
1811 static void shader_none_disable(void *shader_priv, struct wined3d_context *context)
1813 struct shader_none_priv *priv = shader_priv;
1814 const struct wined3d_gl_info *gl_info = context->gl_info;
1816 priv->vertex_pipe->vp_enable(gl_info, FALSE);
1817 priv->fragment_pipe->enable_extension(gl_info, FALSE);
1819 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
1820 | (1 << WINED3D_SHADER_TYPE_VERTEX)
1821 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
1824 static HRESULT shader_none_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
1825 const struct fragment_pipeline *fragment_pipe)
1827 struct fragment_caps fragment_caps;
1828 void *vertex_priv, *fragment_priv;
1829 struct shader_none_priv *priv;
1831 if (!(priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv))))
1832 return E_OUTOFMEMORY;
1834 if (!(vertex_priv = vertex_pipe->vp_alloc(&none_shader_backend, priv)))
1836 ERR("Failed to initialize vertex pipe.\n");
1837 HeapFree(GetProcessHeap(), 0, priv);
1838 return E_FAIL;
1841 if (!(fragment_priv = fragment_pipe->alloc_private(&none_shader_backend, priv)))
1843 ERR("Failed to initialize fragment pipe.\n");
1844 vertex_pipe->vp_free(device);
1845 HeapFree(GetProcessHeap(), 0, priv);
1846 return E_FAIL;
1849 priv->vertex_pipe = vertex_pipe;
1850 priv->fragment_pipe = fragment_pipe;
1851 fragment_pipe->get_caps(&device->adapter->gl_info, &fragment_caps);
1852 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
1854 device->vertex_priv = vertex_priv;
1855 device->fragment_priv = fragment_priv;
1856 device->shader_priv = priv;
1858 return WINED3D_OK;
1861 static void shader_none_free(struct wined3d_device *device)
1863 struct shader_none_priv *priv = device->shader_priv;
1865 priv->fragment_pipe->free_private(device);
1866 priv->vertex_pipe->vp_free(device);
1867 HeapFree(GetProcessHeap(), 0, priv);
1870 static BOOL shader_none_allocate_context_data(struct wined3d_context *context)
1872 return TRUE;
1875 static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
1877 /* Set the shader caps to 0 for the none shader backend */
1878 caps->vs_version = 0;
1879 caps->gs_version = 0;
1880 caps->ps_version = 0;
1881 caps->vs_uniform_count = 0;
1882 caps->ps_uniform_count = 0;
1883 caps->ps_1x_max_value = 0.0f;
1884 caps->wined3d_caps = 0;
1887 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1889 /* We "support" every possible fixup, since we don't support any shader
1890 * model, and will never have to actually sample a texture. */
1891 return TRUE;
1894 static BOOL shader_none_has_ffp_proj_control(void *shader_priv)
1896 struct shader_none_priv *priv = shader_priv;
1898 return priv->ffp_proj_control;
1901 const struct wined3d_shader_backend_ops none_shader_backend =
1903 shader_none_handle_instruction,
1904 shader_none_select,
1905 shader_none_disable,
1906 shader_none_select_depth_blt,
1907 shader_none_deselect_depth_blt,
1908 shader_none_update_float_vertex_constants,
1909 shader_none_update_float_pixel_constants,
1910 shader_none_load_constants,
1911 shader_none_destroy,
1912 shader_none_alloc,
1913 shader_none_free,
1914 shader_none_allocate_context_data,
1915 shader_none_free_context_data,
1916 shader_none_get_caps,
1917 shader_none_color_fixup_supported,
1918 shader_none_has_ffp_proj_control,
1921 static HRESULT shader_set_function(struct wined3d_shader *shader, const DWORD *byte_code,
1922 const struct wined3d_shader_signature *output_signature, DWORD float_const_count,
1923 enum wined3d_shader_type type, unsigned int max_version)
1925 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1926 const struct wined3d_shader_frontend *fe;
1927 HRESULT hr;
1928 unsigned int backend_version;
1929 const struct wined3d_d3d_info *d3d_info = &shader->device->adapter->d3d_info;
1931 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1932 shader, byte_code, output_signature, float_const_count);
1934 fe = shader_select_frontend(*byte_code);
1935 if (!fe)
1937 FIXME("Unable to find frontend for shader.\n");
1938 return WINED3DERR_INVALIDCALL;
1940 shader->frontend = fe;
1941 shader->frontend_data = fe->shader_init(byte_code, output_signature);
1942 if (!shader->frontend_data)
1944 FIXME("Failed to initialize frontend.\n");
1945 return WINED3DERR_INVALIDCALL;
1948 /* First pass: trace shader. */
1949 if (TRACE_ON(d3d_shader))
1950 shader_trace_init(fe, shader->frontend_data, byte_code);
1952 /* Initialize immediate constant lists. */
1953 list_init(&shader->constantsF);
1954 list_init(&shader->constantsB);
1955 list_init(&shader->constantsI);
1956 shader->lconst_inf_or_nan = FALSE;
1958 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1959 hr = shader_get_registers_used(shader, fe,
1960 reg_maps, shader->input_signature, shader->output_signature,
1961 byte_code, float_const_count);
1962 if (FAILED(hr)) return hr;
1964 if (reg_maps->shader_version.type != type)
1966 WARN("Wrong shader type %d.\n", reg_maps->shader_version.type);
1967 return WINED3DERR_INVALIDCALL;
1969 if (reg_maps->shader_version.major > max_version)
1971 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps->shader_version.major);
1972 return WINED3DERR_INVALIDCALL;
1974 switch (type)
1976 case WINED3D_SHADER_TYPE_VERTEX:
1977 backend_version = d3d_info->limits.vs_version;
1978 break;
1979 case WINED3D_SHADER_TYPE_GEOMETRY:
1980 backend_version = d3d_info->limits.gs_version;
1981 break;
1982 case WINED3D_SHADER_TYPE_PIXEL:
1983 backend_version = d3d_info->limits.ps_version;
1984 break;
1985 default:
1986 FIXME("No backend version-checking for this shader type\n");
1987 backend_version = 0;
1989 if (reg_maps->shader_version.major > backend_version)
1991 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
1992 reg_maps->shader_version.major, reg_maps->shader_version.minor);
1993 return WINED3DERR_INVALIDCALL;
1996 shader->function = HeapAlloc(GetProcessHeap(), 0, shader->functionLength);
1997 if (!shader->function)
1998 return E_OUTOFMEMORY;
1999 memcpy(shader->function, byte_code, shader->functionLength);
2001 return WINED3D_OK;
2004 ULONG CDECL wined3d_shader_incref(struct wined3d_shader *shader)
2006 ULONG refcount = InterlockedIncrement(&shader->ref);
2008 TRACE("%p increasing refcount to %u.\n", shader, refcount);
2010 return refcount;
2013 ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
2015 ULONG refcount = InterlockedDecrement(&shader->ref);
2017 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
2019 if (!refcount)
2021 shader_cleanup(shader);
2022 shader->parent_ops->wined3d_object_destroyed(shader->parent);
2023 HeapFree(GetProcessHeap(), 0, shader);
2026 return refcount;
2029 void * CDECL wined3d_shader_get_parent(const struct wined3d_shader *shader)
2031 TRACE("shader %p.\n", shader);
2033 return shader->parent;
2036 HRESULT CDECL wined3d_shader_get_byte_code(const struct wined3d_shader *shader,
2037 void *byte_code, UINT *byte_code_size)
2039 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader, byte_code, byte_code_size);
2041 if (!byte_code)
2043 *byte_code_size = shader->functionLength;
2044 return WINED3D_OK;
2047 if (*byte_code_size < shader->functionLength)
2049 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
2050 * than the required size we should write the required size and
2051 * return D3DERR_MOREDATA. That's not actually true. */
2052 return WINED3DERR_INVALIDCALL;
2055 memcpy(byte_code, shader->function, shader->functionLength);
2057 return WINED3D_OK;
2060 /* Set local constants for d3d8 shaders. */
2061 HRESULT CDECL wined3d_shader_set_local_constants_float(struct wined3d_shader *shader,
2062 UINT start_idx, const float *src_data, UINT count)
2064 UINT end_idx = start_idx + count;
2065 UINT i;
2067 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader, start_idx, src_data, count);
2069 if (end_idx > shader->limits->constant_float)
2071 WARN("end_idx %u > float constants limit %u.\n",
2072 end_idx, shader->limits->constant_float);
2073 end_idx = shader->limits->constant_float;
2076 for (i = start_idx; i < end_idx; ++i)
2078 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
2079 float *value;
2080 if (!lconst)
2081 return E_OUTOFMEMORY;
2083 lconst->idx = i;
2084 value = (float *)lconst->value;
2085 memcpy(value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
2086 list_add_head(&shader->constantsF, &lconst->entry);
2088 if (isinf(value[0]) || isnan(value[0]) || isinf(value[1]) || isnan(value[1])
2089 || isinf(value[2]) || isnan(value[2]) || isinf(value[3]) || isnan(value[3]))
2091 shader->lconst_inf_or_nan = TRUE;
2095 return WINED3D_OK;
2098 void find_vs_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
2099 WORD swizzle_map, struct vs_compile_args *args)
2101 args->fog_src = state->render_states[WINED3D_RS_FOGTABLEMODE]
2102 == WINED3D_FOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
2103 args->clip_enabled = state->render_states[WINED3D_RS_CLIPPING]
2104 && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
2105 args->swizzle_map = swizzle_map;
2108 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
2110 if (usage_idx1 != usage_idx2)
2111 return FALSE;
2112 if (usage1 == usage2)
2113 return TRUE;
2114 if (usage1 == WINED3D_DECL_USAGE_POSITION && usage2 == WINED3D_DECL_USAGE_POSITIONT)
2115 return TRUE;
2116 if (usage2 == WINED3D_DECL_USAGE_POSITION && usage1 == WINED3D_DECL_USAGE_POSITIONT)
2117 return TRUE;
2119 return FALSE;
2122 BOOL vshader_get_input(const struct wined3d_shader *shader,
2123 BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
2125 WORD map = shader->reg_maps.input_registers;
2126 unsigned int i;
2128 for (i = 0; map; map >>= 1, ++i)
2130 if (!(map & 1)) continue;
2132 if (match_usage(shader->u.vs.attributes[i].usage,
2133 shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
2135 *regnum = i;
2136 return TRUE;
2139 return FALSE;
2142 static HRESULT vertexshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2143 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
2145 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2146 unsigned int i;
2147 HRESULT hr;
2148 WORD map;
2149 const DWORD vs_uniform_count = device->adapter->d3d_info.limits.vs_uniform_count;
2151 if (!desc->byte_code)
2152 return WINED3DERR_INVALIDCALL;
2154 shader_init(shader, device, parent, parent_ops);
2155 if (FAILED(hr = shader_set_function(shader, desc->byte_code, desc->output_signature,
2156 vs_uniform_count, WINED3D_SHADER_TYPE_VERTEX, desc->max_version)))
2158 WARN("Failed to set function, hr %#x.\n", hr);
2159 shader_cleanup(shader);
2160 return hr;
2163 map = reg_maps->input_registers;
2164 for (i = 0; map; map >>= 1, ++i)
2166 if (!(map & 1) || !shader->input_signature[i].semantic_name)
2167 continue;
2169 shader->u.vs.attributes[i].usage =
2170 shader_usage_from_semantic_name(shader->input_signature[i].semantic_name);
2171 shader->u.vs.attributes[i].usage_idx = shader->input_signature[i].semantic_idx;
2174 if (desc->output_signature)
2176 struct wined3d_shader_signature_element *e;
2177 SIZE_T total, len;
2178 char *ptr;
2180 total = 0;
2181 for (i = 0; i < desc->output_signature->element_count; ++i)
2183 e = &desc->output_signature->elements[i];
2184 len = strlen(e->semantic_name);
2185 if (len >= ~(SIZE_T)0 - total)
2187 shader_cleanup(shader);
2188 return E_OUTOFMEMORY;
2191 total += len + 1;
2194 if (!(shader->signature_strings = HeapAlloc(GetProcessHeap(), 0, total)))
2196 shader_cleanup(shader);
2197 return E_OUTOFMEMORY;
2199 ptr = shader->signature_strings;
2201 for (i = 0; i < desc->output_signature->element_count; ++i)
2203 e = &desc->output_signature->elements[i];
2204 reg_maps->output_registers |= 1 << e->register_idx;
2205 shader->output_signature[e->register_idx] = *e;
2207 len = strlen(e->semantic_name);
2208 memcpy(ptr, e->semantic_name, len + 1);
2209 shader->output_signature[e->register_idx].semantic_name = ptr;
2210 ptr += len + 1;
2214 shader->load_local_constsF = (reg_maps->usesrelconstF && !list_empty(&shader->constantsF)) ||
2215 shader->lconst_inf_or_nan;
2217 return WINED3D_OK;
2220 static HRESULT geometryshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2221 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
2223 HRESULT hr;
2225 shader_init(shader, device, parent, parent_ops);
2226 if (FAILED(hr = shader_set_function(shader, desc->byte_code, desc->output_signature,
2227 0, WINED3D_SHADER_TYPE_GEOMETRY, desc->max_version)))
2229 WARN("Failed to set function, hr %#x.\n", hr);
2230 shader_cleanup(shader);
2231 return hr;
2234 shader->load_local_constsF = shader->lconst_inf_or_nan;
2236 return WINED3D_OK;
2239 void find_ps_compile_args(const struct wined3d_state *state, const struct wined3d_shader *shader,
2240 BOOL position_transformed, struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
2242 const struct wined3d_texture *texture;
2243 UINT i;
2245 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
2246 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && state->render_states[WINED3D_RS_SRGBWRITEENABLE])
2248 const struct wined3d_format *rt_format = state->fb->render_targets[0]->format;
2249 if (rt_format->flags & WINED3DFMT_FLAG_SRGB_WRITE)
2251 static unsigned int warned = 0;
2253 args->srgb_correction = 1;
2254 if (state->render_states[WINED3D_RS_ALPHABLENDENABLE] && !warned++)
2255 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
2256 "support, expect rendering artifacts.\n");
2260 if (shader->reg_maps.shader_version.major == 1
2261 && shader->reg_maps.shader_version.minor <= 3)
2263 for (i = 0; i < shader->limits->sampler; ++i)
2265 DWORD flags = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
2267 if (flags & WINED3D_TTFF_PROJECTED)
2269 DWORD tex_transform = flags & ~WINED3D_TTFF_PROJECTED;
2271 if (!state->shader[WINED3D_SHADER_TYPE_VERTEX])
2273 enum wined3d_shader_resource_type resource_type = shader->reg_maps.resource_info[i].type;
2274 unsigned int j;
2275 unsigned int index = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
2276 DWORD max_valid = WINED3D_TTFF_COUNT4;
2278 for (j = 0; j < state->vertex_declaration->element_count; ++j)
2280 struct wined3d_vertex_declaration_element *element =
2281 &state->vertex_declaration->elements[j];
2283 if (element->usage == WINED3D_DECL_USAGE_TEXCOORD
2284 && element->usage_idx == index)
2286 max_valid = element->format->component_count;
2287 break;
2290 if (!tex_transform || tex_transform > max_valid)
2292 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2293 tex_transform, max_valid);
2294 tex_transform = max_valid;
2296 if ((resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1D && tex_transform > WINED3D_TTFF_COUNT1)
2297 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D
2298 && tex_transform > WINED3D_TTFF_COUNT2)
2299 || (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_3D
2300 && tex_transform > WINED3D_TTFF_COUNT3))
2301 tex_transform |= WINED3D_PSARGS_PROJECTED;
2302 else
2304 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2305 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2306 i, tex_transform, resource_type);
2309 else
2310 tex_transform = WINED3D_TTFF_COUNT4 | WINED3D_PSARGS_PROJECTED;
2312 args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
2316 if (shader->reg_maps.shader_version.major == 1
2317 && shader->reg_maps.shader_version.minor <= 4)
2319 for (i = 0; i < shader->limits->sampler; ++i)
2321 const struct wined3d_texture *texture = state->textures[i];
2323 if (!shader->reg_maps.resource_info[i].type)
2324 continue;
2326 /* Treat unbound textures as 2D. The dummy texture will provide
2327 * the proper sample value. The tex_types bitmap defaults to
2328 * 2D because of the memset. */
2329 if (!texture)
2330 continue;
2332 switch (texture->target)
2334 /* RECT textures are distinguished from 2D textures via np2_fixup */
2335 case GL_TEXTURE_RECTANGLE_ARB:
2336 case GL_TEXTURE_2D:
2337 break;
2339 case GL_TEXTURE_3D:
2340 args->tex_types |= WINED3D_SHADER_TEX_3D << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
2341 break;
2343 case GL_TEXTURE_CUBE_MAP_ARB:
2344 args->tex_types |= WINED3D_SHADER_TEX_CUBE << i * WINED3D_PSARGS_TEXTYPE_SHIFT;
2345 break;
2350 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2352 if (!shader->reg_maps.resource_info[i].type)
2353 continue;
2355 texture = state->textures[i];
2356 if (!texture)
2358 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
2359 continue;
2361 args->color_fixup[i] = texture->resource.format->color_fixup;
2363 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
2364 args->shadow |= 1 << i;
2366 /* Flag samplers that need NP2 texcoord fixup. */
2367 if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
2368 args->np2_fixup |= (1 << i);
2370 if (shader->reg_maps.shader_version.major >= 3)
2372 if (position_transformed)
2373 args->vp_mode = pretransformed;
2374 else if (use_vs(state))
2375 args->vp_mode = vertexshader;
2376 else
2377 args->vp_mode = fixedfunction;
2378 args->fog = WINED3D_FFP_PS_FOG_OFF;
2380 else
2382 args->vp_mode = vertexshader;
2383 if (state->render_states[WINED3D_RS_FOGENABLE])
2385 switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
2387 case WINED3D_FOG_NONE:
2388 if (position_transformed || use_vs(state))
2390 args->fog = WINED3D_FFP_PS_FOG_LINEAR;
2391 break;
2394 switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
2396 case WINED3D_FOG_NONE: /* Fall through. */
2397 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
2398 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
2399 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
2401 break;
2403 case WINED3D_FOG_LINEAR: args->fog = WINED3D_FFP_PS_FOG_LINEAR; break;
2404 case WINED3D_FOG_EXP: args->fog = WINED3D_FFP_PS_FOG_EXP; break;
2405 case WINED3D_FOG_EXP2: args->fog = WINED3D_FFP_PS_FOG_EXP2; break;
2408 else
2410 args->fog = WINED3D_FFP_PS_FOG_OFF;
2415 static HRESULT pixelshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2416 const struct wined3d_shader_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
2418 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2419 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
2420 HRESULT hr;
2421 const DWORD ps_uniform_count = device->adapter->d3d_info.limits.ps_uniform_count;
2423 if (!desc->byte_code)
2424 return WINED3DERR_INVALIDCALL;
2426 shader_init(shader, device, parent, parent_ops);
2427 if (FAILED(hr = shader_set_function(shader, desc->byte_code, desc->output_signature,
2428 ps_uniform_count, WINED3D_SHADER_TYPE_PIXEL, desc->max_version)))
2430 WARN("Failed to set function, hr %#x.\n", hr);
2431 shader_cleanup(shader);
2432 return hr;
2435 for (i = 0; i < MAX_REG_INPUT; ++i)
2437 if (shader->u.ps.input_reg_used[i])
2439 ++num_regs_used;
2440 highest_reg_used = i;
2444 /* Don't do any register mapping magic if it is not needed, or if we can't
2445 * achieve anything anyway */
2446 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
2447 || num_regs_used > (gl_info->limits.glsl_varyings / 4))
2449 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
2451 /* This happens with relative addressing. The input mapper function
2452 * warns about this if the higher registers are declared too, so
2453 * don't write a FIXME here */
2454 WARN("More varying registers used than supported\n");
2457 for (i = 0; i < MAX_REG_INPUT; ++i)
2459 shader->u.ps.input_reg_map[i] = i;
2462 shader->u.ps.declared_in_count = highest_reg_used + 1;
2464 else
2466 shader->u.ps.declared_in_count = 0;
2467 for (i = 0; i < MAX_REG_INPUT; ++i)
2469 if (shader->u.ps.input_reg_used[i])
2470 shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
2471 else shader->u.ps.input_reg_map[i] = ~0U;
2475 shader->load_local_constsF = shader->lconst_inf_or_nan;
2477 if (desc->input_signature)
2479 struct wined3d_shader_signature_element *e;
2480 SIZE_T total, len;
2481 char *ptr;
2483 total = 0;
2484 for (i = 0; i < desc->input_signature->element_count; ++i)
2486 e = &desc->input_signature->elements[i];
2487 len = strlen(e->semantic_name);
2488 if (len >= ~(SIZE_T)0 - total)
2490 shader_cleanup(shader);
2491 return E_OUTOFMEMORY;
2494 total += len + 1;
2497 if (!(shader->signature_strings = HeapAlloc(GetProcessHeap(), 0, total)))
2499 shader_cleanup(shader);
2500 return E_OUTOFMEMORY;
2502 ptr = shader->signature_strings;
2504 for (i = 0; i < desc->input_signature->element_count; ++i)
2506 e = &desc->input_signature->elements[i];
2507 shader->reg_maps.input_registers |= 1 << e->register_idx;
2508 shader->input_signature[e->register_idx] = *e;
2510 len = strlen(e->semantic_name);
2511 memcpy(ptr, e->semantic_name, len + 1);
2512 shader->input_signature[e->register_idx].semantic_name = ptr;
2513 ptr += len + 1;
2515 if (!strcmp(e->semantic_name, "SV_POSITION"))
2516 shader->reg_maps.vpos = 1;
2520 return WINED3D_OK;
2523 void pixelshader_update_resource_types(struct wined3d_shader *shader, WORD tex_types)
2525 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2526 struct wined3d_shader_resource_info *resource_info = reg_maps->resource_info;
2527 unsigned int i;
2529 if (reg_maps->shader_version.major != 1) return;
2531 for (i = 0; i < shader->limits->sampler; ++i)
2533 /* We don't sample from this sampler. */
2534 if (!resource_info[i].type)
2535 continue;
2537 switch ((tex_types >> i * WINED3D_PSARGS_TEXTYPE_SHIFT) & WINED3D_PSARGS_TEXTYPE_MASK)
2539 case WINED3D_SHADER_TEX_2D:
2540 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2541 break;
2543 case WINED3D_SHADER_TEX_3D:
2544 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_3D;
2545 break;
2547 case WINED3D_SHADER_TEX_CUBE:
2548 resource_info[i].type = WINED3D_SHADER_RESOURCE_TEXTURE_CUBE;
2549 break;
2554 HRESULT CDECL wined3d_shader_create_gs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
2555 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
2557 struct wined3d_shader *object;
2558 HRESULT hr;
2560 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
2561 device, desc, parent, parent_ops, shader);
2563 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2564 if (!object)
2565 return E_OUTOFMEMORY;
2567 if (FAILED(hr = geometryshader_init(object, device, desc, parent, parent_ops)))
2569 WARN("Failed to initialize geometry shader, hr %#x.\n", hr);
2570 HeapFree(GetProcessHeap(), 0, object);
2571 return hr;
2574 TRACE("Created geometry shader %p.\n", object);
2575 *shader = object;
2577 return WINED3D_OK;
2580 HRESULT CDECL wined3d_shader_create_ps(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
2581 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
2583 struct wined3d_shader *object;
2584 HRESULT hr;
2586 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
2587 device, desc, parent, parent_ops, shader);
2589 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2590 if (!object)
2591 return E_OUTOFMEMORY;
2593 if (FAILED(hr = pixelshader_init(object, device, desc, parent, parent_ops)))
2595 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2596 HeapFree(GetProcessHeap(), 0, object);
2597 return hr;
2600 TRACE("Created pixel shader %p.\n", object);
2601 *shader = object;
2603 return WINED3D_OK;
2606 HRESULT CDECL wined3d_shader_create_vs(struct wined3d_device *device, const struct wined3d_shader_desc *desc,
2607 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader)
2609 struct wined3d_shader *object;
2610 HRESULT hr;
2612 TRACE("device %p, desc %p, parent %p, parent_ops %p, shader %p.\n",
2613 device, desc, parent, parent_ops, shader);
2615 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2616 if (!object)
2617 return E_OUTOFMEMORY;
2619 if (FAILED(hr = vertexshader_init(object, device, desc, parent, parent_ops)))
2621 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2622 HeapFree(GetProcessHeap(), 0, object);
2623 return hr;
2626 TRACE("Created vertex shader %p.\n", object);
2627 *shader = object;
2629 return WINED3D_OK;