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
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
31 #include "wined3d_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
34 WINE_DECLARE_DEBUG_CHANNEL(d3d);
36 static const char * const shader_opcode_names[] =
38 /* WINED3DSIH_ABS */ "abs",
39 /* WINED3DSIH_ADD */ "add",
40 /* WINED3DSIH_AND */ "and",
41 /* WINED3DSIH_BEM */ "bem",
42 /* WINED3DSIH_BREAK */ "break",
43 /* WINED3DSIH_BREAKC */ "breakc",
44 /* WINED3DSIH_BREAKP */ "breakp",
45 /* WINED3DSIH_CALL */ "call",
46 /* WINED3DSIH_CALLNZ */ "callnz",
47 /* WINED3DSIH_CMP */ "cmp",
48 /* WINED3DSIH_CND */ "cnd",
49 /* WINED3DSIH_CRS */ "crs",
50 /* WINED3DSIH_CUT */ "cut",
51 /* WINED3DSIH_DCL */ "dcl",
52 /* WINED3DSIH_DEF */ "def",
53 /* WINED3DSIH_DEFB */ "defb",
54 /* WINED3DSIH_DEFI */ "defi",
55 /* WINED3DSIH_DIV */ "div",
56 /* WINED3DSIH_DP2ADD */ "dp2add",
57 /* WINED3DSIH_DP3 */ "dp3",
58 /* WINED3DSIH_DP4 */ "dp4",
59 /* WINED3DSIH_DST */ "dst",
60 /* WINED3DSIH_DSX */ "dsx",
61 /* WINED3DSIH_DSY */ "dsy",
62 /* WINED3DSIH_ELSE */ "else",
63 /* WINED3DSIH_EMIT */ "emit",
64 /* WINED3DSIH_ENDIF */ "endif",
65 /* WINED3DSIH_ENDLOOP */ "endloop",
66 /* WINED3DSIH_ENDREP */ "endrep",
67 /* WINED3DSIH_EQ */ "eq",
68 /* WINED3DSIH_EXP */ "exp",
69 /* WINED3DSIH_EXPP */ "expp",
70 /* WINED3DSIH_FRC */ "frc",
71 /* WINED3DSIH_FTOI */ "ftoi",
72 /* WINED3DSIH_GE */ "ge",
73 /* WINED3DSIH_IADD */ "iadd",
74 /* WINED3DSIH_IEQ */ "ieq",
75 /* WINED3DSIH_IF */ "if",
76 /* WINED3DSIH_IFC */ "ifc",
77 /* WINED3DSIH_IGE */ "ige",
78 /* WINED3DSIH_IMUL */ "imul",
79 /* WINED3DSIH_ITOF */ "itof",
80 /* WINED3DSIH_LABEL */ "label",
81 /* WINED3DSIH_LD */ "ld",
82 /* WINED3DSIH_LIT */ "lit",
83 /* WINED3DSIH_LOG */ "log",
84 /* WINED3DSIH_LOGP */ "logp",
85 /* WINED3DSIH_LOOP */ "loop",
86 /* WINED3DSIH_LRP */ "lrp",
87 /* WINED3DSIH_LT */ "lt",
88 /* WINED3DSIH_M3x2 */ "m3x2",
89 /* WINED3DSIH_M3x3 */ "m3x3",
90 /* WINED3DSIH_M3x4 */ "m3x4",
91 /* WINED3DSIH_M4x3 */ "m4x3",
92 /* WINED3DSIH_M4x4 */ "m4x4",
93 /* WINED3DSIH_MAD */ "mad",
94 /* WINED3DSIH_MAX */ "max",
95 /* WINED3DSIH_MIN */ "min",
96 /* WINED3DSIH_MOV */ "mov",
97 /* WINED3DSIH_MOVA */ "mova",
98 /* WINED3DSIH_MOVC */ "movc",
99 /* WINED3DSIH_MUL */ "mul",
100 /* WINED3DSIH_NOP */ "nop",
101 /* WINED3DSIH_NRM */ "nrm",
102 /* WINED3DSIH_PHASE */ "phase",
103 /* WINED3DSIH_POW */ "pow",
104 /* WINED3DSIH_RCP */ "rcp",
105 /* WINED3DSIH_REP */ "rep",
106 /* WINED3DSIH_RET */ "ret",
107 /* WINED3DSIH_ROUND_NI */ "round_ni",
108 /* WINED3DSIH_RSQ */ "rsq",
109 /* WINED3DSIH_SAMPLE */ "sample",
110 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
111 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
112 /* WINED3DSIH_SETP */ "setp",
113 /* WINED3DSIH_SGE */ "sge",
114 /* WINED3DSIH_SGN */ "sgn",
115 /* WINED3DSIH_SINCOS */ "sincos",
116 /* WINED3DSIH_SLT */ "slt",
117 /* WINED3DSIH_SQRT */ "sqrt",
118 /* WINED3DSIH_SUB */ "sub",
119 /* WINED3DSIH_TEX */ "texld",
120 /* WINED3DSIH_TEXBEM */ "texbem",
121 /* WINED3DSIH_TEXBEML */ "texbeml",
122 /* WINED3DSIH_TEXCOORD */ "texcrd",
123 /* WINED3DSIH_TEXDEPTH */ "texdepth",
124 /* WINED3DSIH_TEXDP3 */ "texdp3",
125 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
126 /* WINED3DSIH_TEXKILL */ "texkill",
127 /* WINED3DSIH_TEXLDD */ "texldd",
128 /* WINED3DSIH_TEXLDL */ "texldl",
129 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
130 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
131 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
132 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
133 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
134 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
135 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
136 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
137 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
138 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
139 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
140 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
141 /* WINED3DSIH_UDIV */ "udiv",
142 /* WINED3DSIH_USHR */ "ushr",
143 /* WINED3DSIH_UTOF */ "utof",
144 /* WINED3DSIH_XOR */ "xor",
147 static const char * const semantic_names[] =
149 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
150 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
151 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
152 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
153 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
154 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
155 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
156 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
157 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
158 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
159 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
160 /* WINED3D_DECL_USAGE_FOG */ "FOG",
161 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
162 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
165 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage)
167 if (usage >= sizeof(semantic_names) / sizeof(*semantic_names))
169 FIXME("Unrecognized usage %#x.\n", usage);
170 return "UNRECOGNIZED";
173 return semantic_names[usage];
176 static enum wined3d_decl_usage shader_usage_from_semantic_name(const char *name)
180 for (i = 0; i < sizeof(semantic_names) / sizeof(*semantic_names); ++i)
182 if (!strcmp(name, semantic_names[i])) return i;
188 BOOL shader_match_semantic(const char *semantic_name, enum wined3d_decl_usage usage)
190 return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
193 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
194 const struct wined3d_shader_semantic *s)
196 e->semantic_name = shader_semantic_name_from_usage(s->usage);
197 e->semantic_idx = s->usage_idx;
198 e->sysval_semantic = 0;
199 e->component_type = 0;
200 e->register_idx = s->reg.reg.idx;
201 e->mask = s->reg.write_mask;
204 static void shader_signature_from_usage(struct wined3d_shader_signature_element *e,
205 enum wined3d_decl_usage usage, UINT usage_idx, UINT reg_idx, DWORD write_mask)
207 e->semantic_name = shader_semantic_name_from_usage(usage);
208 e->semantic_idx = usage_idx;
209 e->sysval_semantic = 0;
210 e->component_type = 0;
211 e->register_idx = reg_idx;
212 e->mask = write_mask;
215 static const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token)
217 switch (version_token >> 16)
221 return &sm1_shader_frontend;
226 return &sm4_shader_frontend;
229 FIXME("Unrecognised version token %#x\n", version_token);
234 void shader_buffer_clear(struct wined3d_shader_buffer *buffer)
236 buffer->buffer[0] = '\0';
239 buffer->newline = TRUE;
242 BOOL shader_buffer_init(struct wined3d_shader_buffer *buffer)
244 buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
247 ERR("Failed to allocate shader buffer memory.\n");
251 shader_buffer_clear(buffer);
255 void shader_buffer_free(struct wined3d_shader_buffer *buffer)
257 HeapFree(GetProcessHeap(), 0, buffer->buffer);
260 int shader_vaddline(struct wined3d_shader_buffer *buffer, const char *format, va_list args)
262 char *base = buffer->buffer + buffer->bsize;
265 rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
267 if (rc < 0 /* C89 */ || (unsigned int)rc > SHADER_PGMSIZE - 1 - buffer->bsize /* C99 */)
269 ERR("The buffer allocated for the shader program string "
270 "is too small at %d bytes.\n", SHADER_PGMSIZE);
271 buffer->bsize = SHADER_PGMSIZE - 1;
277 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
278 buffer->newline = FALSE;
286 if (buffer->buffer[buffer->bsize-1] == '\n')
289 buffer->newline = TRUE;
295 int shader_addline(struct wined3d_shader_buffer *buffer, const char *format, ...)
300 va_start(args, format);
301 ret = shader_vaddline(buffer, format, args);
307 static void shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
308 void *parent, const struct wined3d_parent_ops *parent_ops)
311 shader->device = device;
312 shader->parent = parent;
313 shader->parent_ops = parent_ops;
314 list_init(&shader->linked_programs);
315 list_add_head(&device->shaders, &shader->shader_list_entry);
318 /* Convert floating point offset relative to a register file to an absolute
319 * offset for float constants. */
320 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type, UINT register_idx)
322 switch (register_type)
324 case WINED3DSPR_CONST: return register_idx;
325 case WINED3DSPR_CONST2: return 2048 + register_idx;
326 case WINED3DSPR_CONST3: return 4096 + register_idx;
327 case WINED3DSPR_CONST4: return 6144 + register_idx;
329 FIXME("Unsupported register type: %u.\n", register_type);
334 static void shader_delete_constant_list(struct list *clist)
336 struct wined3d_shader_lconst *constant;
339 ptr = list_head(clist);
342 constant = LIST_ENTRY(ptr, struct wined3d_shader_lconst, entry);
343 ptr = list_next(clist, ptr);
344 HeapFree(GetProcessHeap(), 0, constant);
349 static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
354 bitmap[idx] |= (1 << shift);
357 static void shader_record_register_usage(struct wined3d_shader *shader, struct wined3d_shader_reg_maps *reg_maps,
358 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type)
362 case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
363 if (shader_type == WINED3D_SHADER_TYPE_PIXEL) reg_maps->texcoord |= 1 << reg->idx;
364 else reg_maps->address |= 1 << reg->idx;
367 case WINED3DSPR_TEMP:
368 reg_maps->temporary |= 1 << reg->idx;
371 case WINED3DSPR_INPUT:
372 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
376 /* If relative addressing is used, we must assume that all registers
377 * are used. Even if it is a construct like v3[aL], we can't assume
378 * that v0, v1 and v2 aren't read because aL can be negative */
380 for (i = 0; i < MAX_REG_INPUT; ++i)
382 shader->u.ps.input_reg_used[i] = TRUE;
387 shader->u.ps.input_reg_used[reg->idx] = TRUE;
390 else reg_maps->input_registers |= 1 << reg->idx;
393 case WINED3DSPR_RASTOUT:
394 if (reg->idx == 1) reg_maps->fog = 1;
397 case WINED3DSPR_MISCTYPE:
398 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
400 if (!reg->idx) reg_maps->vpos = 1;
401 else if (reg->idx == 1) reg_maps->usesfacing = 1;
405 case WINED3DSPR_CONST:
408 if (reg->idx < reg_maps->min_rel_offset) reg_maps->min_rel_offset = reg->idx;
409 if (reg->idx > reg_maps->max_rel_offset) reg_maps->max_rel_offset = reg->idx;
410 reg_maps->usesrelconstF = TRUE;
414 set_bitmap_bit(reg_maps->constf, reg->idx);
418 case WINED3DSPR_CONSTINT:
419 reg_maps->integer_constants |= (1 << reg->idx);
422 case WINED3DSPR_CONSTBOOL:
423 reg_maps->boolean_constants |= (1 << reg->idx);
426 case WINED3DSPR_COLOROUT:
427 reg_maps->rt_mask |= (1 << reg->idx);
431 TRACE("Not recording register of type %#x and idx %u\n", reg->type, reg->idx);
436 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
440 case WINED3DSIH_M4x4:
441 case WINED3DSIH_M3x4:
442 return param == 1 ? 3 : 0;
444 case WINED3DSIH_M4x3:
445 case WINED3DSIH_M3x3:
446 return param == 1 ? 2 : 0;
448 case WINED3DSIH_M3x2:
449 return param == 1 ? 1 : 0;
456 /* Note that this does not count the loop register as an address register. */
457 static HRESULT shader_get_registers_used(struct wined3d_shader *shader, const struct wined3d_shader_frontend *fe,
458 struct wined3d_shader_reg_maps *reg_maps, struct wined3d_shader_signature_element *input_signature,
459 struct wined3d_shader_signature_element *output_signature, const DWORD *byte_code, DWORD constf_size)
461 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
462 void *fe_data = shader->frontend_data;
463 struct wined3d_shader_version shader_version;
464 const DWORD *ptr = byte_code;
466 memset(reg_maps, 0, sizeof(*reg_maps));
467 reg_maps->min_rel_offset = ~0U;
469 fe->shader_read_header(fe_data, &ptr, &shader_version);
470 reg_maps->shader_version = shader_version;
472 reg_maps->constf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
473 sizeof(*reg_maps->constf) * ((constf_size + 31) / 32));
474 if (!reg_maps->constf)
476 ERR("Failed to allocate constant map memory.\n");
477 return E_OUTOFMEMORY;
480 while (!fe->shader_is_end(fe_data, &ptr))
482 struct wined3d_shader_instruction ins;
488 fe->shader_read_comment(&ptr, &comment, &comment_size);
489 if (comment) continue;
492 fe->shader_read_opcode(fe_data, &ptr, &ins, ¶m_size);
494 /* Unhandled opcode, and its parameters. */
495 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
497 TRACE("Skipping unrecognized instruction.\n");
502 /* Handle declarations. */
503 if (ins.handler_idx == WINED3DSIH_DCL)
505 struct wined3d_shader_semantic semantic;
507 fe->shader_read_semantic(&ptr, &semantic);
509 switch (semantic.reg.reg.type)
511 /* Mark input registers used. */
512 case WINED3DSPR_INPUT:
513 reg_maps->input_registers |= 1 << semantic.reg.reg.idx;
514 shader_signature_from_semantic(&input_signature[semantic.reg.reg.idx], &semantic);
517 /* Vertex shader: mark 3.0 output registers used, save token. */
518 case WINED3DSPR_OUTPUT:
519 reg_maps->output_registers |= 1 << semantic.reg.reg.idx;
520 shader_signature_from_semantic(&output_signature[semantic.reg.reg.idx], &semantic);
521 if (semantic.usage == WINED3D_DECL_USAGE_FOG)
525 /* Save sampler usage token. */
526 case WINED3DSPR_SAMPLER:
527 reg_maps->sampler_type[semantic.reg.reg.idx] = semantic.sampler_type;
531 TRACE("Not recording DCL register type %#x.\n", semantic.reg.reg.type);
535 else if (ins.handler_idx == WINED3DSIH_DEF)
537 struct wined3d_shader_src_param rel_addr;
538 struct wined3d_shader_dst_param dst;
540 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
541 if (!lconst) return E_OUTOFMEMORY;
543 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
544 lconst->idx = dst.reg.idx;
546 memcpy(lconst->value, ptr, 4 * sizeof(DWORD));
549 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
550 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
552 float *value = (float *)lconst->value;
553 if (value[0] < -1.0f) value[0] = -1.0f;
554 else if (value[0] > 1.0f) value[0] = 1.0f;
555 if (value[1] < -1.0f) value[1] = -1.0f;
556 else if (value[1] > 1.0f) value[1] = 1.0f;
557 if (value[2] < -1.0f) value[2] = -1.0f;
558 else if (value[2] > 1.0f) value[2] = 1.0f;
559 if (value[3] < -1.0f) value[3] = -1.0f;
560 else if (value[3] > 1.0f) value[3] = 1.0f;
563 list_add_head(&shader->constantsF, &lconst->entry);
565 else if (ins.handler_idx == WINED3DSIH_DEFI)
567 struct wined3d_shader_src_param rel_addr;
568 struct wined3d_shader_dst_param dst;
570 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
571 if (!lconst) return E_OUTOFMEMORY;
573 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
574 lconst->idx = dst.reg.idx;
576 memcpy(lconst->value, ptr, 4 * sizeof(DWORD));
579 list_add_head(&shader->constantsI, &lconst->entry);
580 reg_maps->local_int_consts |= (1 << dst.reg.idx);
582 else if (ins.handler_idx == WINED3DSIH_DEFB)
584 struct wined3d_shader_src_param rel_addr;
585 struct wined3d_shader_dst_param dst;
587 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
588 if (!lconst) return E_OUTOFMEMORY;
590 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
591 lconst->idx = dst.reg.idx;
593 memcpy(lconst->value, ptr, sizeof(DWORD));
596 list_add_head(&shader->constantsB, &lconst->entry);
597 reg_maps->local_bool_consts |= (1 << dst.reg.idx);
599 /* For subroutine prototypes. */
600 else if (ins.handler_idx == WINED3DSIH_LABEL)
602 struct wined3d_shader_src_param src, rel_addr;
604 fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
605 reg_maps->labels |= 1 << src.reg.idx;
607 /* Set texture, address, temporary registers. */
610 BOOL color0_mov = FALSE;
611 unsigned int i, limit;
613 /* This will loop over all the registers and try to
614 * make a bitmask of the ones we're interested in.
616 * Relative addressing tokens are ignored, but that's
617 * okay, since we'll catch any address registers when
618 * they are initialized (required by spec). */
619 for (i = 0; i < ins.dst_count; ++i)
621 struct wined3d_shader_src_param dst_rel_addr;
622 struct wined3d_shader_dst_param dst_param;
624 fe->shader_read_dst_param(fe_data, &ptr, &dst_param, &dst_rel_addr);
626 shader_record_register_usage(shader, reg_maps, &dst_param.reg, shader_version.type);
628 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
629 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
630 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
631 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX && shader_version.major < 3)
633 UINT idx = dst_param.reg.idx;
635 switch (dst_param.reg.type)
637 case WINED3DSPR_RASTOUT:
641 reg_maps->output_registers |= 1 << 10;
642 shader_signature_from_usage(&output_signature[10],
643 WINED3D_DECL_USAGE_POSITION, 0, 10, WINED3DSP_WRITEMASK_ALL);
647 reg_maps->output_registers |= 1 << 11;
648 shader_signature_from_usage(&output_signature[11],
649 WINED3D_DECL_USAGE_FOG, 0, 11, WINED3DSP_WRITEMASK_0);
653 reg_maps->output_registers |= 1 << 11;
654 shader_signature_from_usage(&output_signature[11],
655 WINED3D_DECL_USAGE_PSIZE, 0, 11, WINED3DSP_WRITEMASK_1);
660 case WINED3DSPR_ATTROUT:
664 if (reg_maps->output_registers & (1 << idx))
666 output_signature[idx].mask |= dst_param.write_mask;
670 reg_maps->output_registers |= 1 << idx;
671 shader_signature_from_usage(&output_signature[idx],
672 WINED3D_DECL_USAGE_COLOR, idx - 8, idx, dst_param.write_mask);
677 case WINED3DSPR_TEXCRDOUT:
679 reg_maps->texcoord_mask[idx] |= dst_param.write_mask;
680 if (reg_maps->output_registers & (1 << idx))
682 output_signature[idx].mask |= dst_param.write_mask;
686 reg_maps->output_registers |= 1 << idx;
687 shader_signature_from_usage(&output_signature[idx],
688 WINED3D_DECL_USAGE_TEXCOORD, idx, idx, dst_param.write_mask);
697 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
699 if (dst_param.reg.type == WINED3DSPR_COLOROUT && !dst_param.reg.idx)
701 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
702 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
703 * the mov and perform the sRGB write correction from the source register.
705 * However, if the mov is only partial, we can't do this, and if the write
706 * comes from an instruction other than MOV it is hard to do as well. If
707 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
708 shader->u.ps.color0_mov = FALSE;
709 if (ins.handler_idx == WINED3DSIH_MOV
710 && dst_param.write_mask == WINED3DSP_WRITEMASK_ALL)
712 /* Used later when the source register is read. */
716 /* Also drop the MOV marker if the source register is overwritten prior to the shader
719 else if (dst_param.reg.type == WINED3DSPR_TEMP
720 && dst_param.reg.idx == shader->u.ps.color0_reg)
722 shader->u.ps.color0_mov = FALSE;
726 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
727 if (shader_version.major == 1
728 && (ins.handler_idx == WINED3DSIH_TEX
729 || ins.handler_idx == WINED3DSIH_TEXBEM
730 || ins.handler_idx == WINED3DSIH_TEXBEML
731 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
732 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
733 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
734 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
735 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
736 || ins.handler_idx == WINED3DSIH_TEXREG2AR
737 || ins.handler_idx == WINED3DSIH_TEXREG2GB
738 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
740 /* Fake sampler usage, only set reserved bit and type. */
741 DWORD sampler_code = dst_param.reg.idx;
743 TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
744 reg_maps->sampler_type[sampler_code] = WINED3DSTT_2D;
746 /* texbem is only valid with < 1.4 pixel shaders */
747 if (ins.handler_idx == WINED3DSIH_TEXBEM
748 || ins.handler_idx == WINED3DSIH_TEXBEML)
750 reg_maps->bumpmat |= 1 << dst_param.reg.idx;
751 if (ins.handler_idx == WINED3DSIH_TEXBEML)
753 reg_maps->luminanceparams |= 1 << dst_param.reg.idx;
757 else if (ins.handler_idx == WINED3DSIH_BEM)
759 reg_maps->bumpmat |= 1 << dst_param.reg.idx;
763 if (ins.handler_idx == WINED3DSIH_NRM) reg_maps->usesnrm = 1;
764 else if (ins.handler_idx == WINED3DSIH_DSY) reg_maps->usesdsy = 1;
765 else if (ins.handler_idx == WINED3DSIH_DSX) reg_maps->usesdsx = 1;
766 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
767 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
768 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
769 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
770 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
771 else if (ins.handler_idx == WINED3DSIH_POW) reg_maps->usespow = 1;
772 else if (ins.handler_idx == WINED3DSIH_LOOP
773 || ins.handler_idx == WINED3DSIH_REP)
776 if (cur_loop_depth > max_loop_depth)
777 max_loop_depth = cur_loop_depth;
779 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
780 || ins.handler_idx == WINED3DSIH_ENDREP)
783 limit = ins.src_count + (ins.predicate ? 1 : 0);
784 for (i = 0; i < limit; ++i)
786 struct wined3d_shader_src_param src_param, src_rel_addr;
789 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
790 count = get_instr_extra_regcount(ins.handler_idx, i);
792 shader_record_register_usage(shader, reg_maps, &src_param.reg, shader_version.type);
796 shader_record_register_usage(shader, reg_maps, &src_param.reg, shader_version.type);
802 if (src_param.reg.type == WINED3DSPR_TEMP
803 && src_param.swizzle == WINED3DSP_NOSWIZZLE)
805 shader->u.ps.color0_mov = TRUE;
806 shader->u.ps.color0_reg = src_param.reg.idx;
812 reg_maps->loop_depth = max_loop_depth;
814 /* PS before 2.0 don't have explicit color outputs. Instead the value of
815 * R0 is written to the render target. */
816 if (shader_version.major < 2 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
817 reg_maps->rt_mask |= (1 << 0);
819 shader->functionLength = ((const char *)ptr - (const char *)byte_code);
824 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
826 DWORD map = 1 << max;
828 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
830 return wined3d_log2i(map);
833 static void shader_dump_decl_usage(const struct wined3d_shader_semantic *semantic,
834 const struct wined3d_shader_version *shader_version)
838 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
840 switch (semantic->sampler_type)
842 case WINED3DSTT_2D: TRACE("_2d"); break;
843 case WINED3DSTT_CUBE: TRACE("_cube"); break;
844 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
845 default: TRACE("_unknown_ttype(0x%08x)", semantic->sampler_type);
850 /* Pixel shaders 3.0 don't have usage semantics. */
851 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL) return;
854 switch (semantic->usage)
856 case WINED3D_DECL_USAGE_POSITION:
857 TRACE("position%u", semantic->usage_idx);
860 case WINED3D_DECL_USAGE_BLEND_INDICES:
864 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
868 case WINED3D_DECL_USAGE_NORMAL:
869 TRACE("normal%u", semantic->usage_idx);
872 case WINED3D_DECL_USAGE_PSIZE:
876 case WINED3D_DECL_USAGE_COLOR:
877 if (!semantic->usage_idx) TRACE("color");
878 else TRACE("specular%u", (semantic->usage_idx - 1));
881 case WINED3D_DECL_USAGE_TEXCOORD:
882 TRACE("texture%u", semantic->usage_idx);
885 case WINED3D_DECL_USAGE_TANGENT:
889 case WINED3D_DECL_USAGE_BINORMAL:
893 case WINED3D_DECL_USAGE_TESS_FACTOR:
897 case WINED3D_DECL_USAGE_POSITIONT:
898 TRACE("positionT%u", semantic->usage_idx);
901 case WINED3D_DECL_USAGE_FOG:
905 case WINED3D_DECL_USAGE_DEPTH:
909 case WINED3D_DECL_USAGE_SAMPLE:
914 FIXME("unknown_semantics(0x%08x)", semantic->usage);
919 static void shader_dump_register(const struct wined3d_shader_register *reg,
920 const struct wined3d_shader_version *shader_version)
922 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
923 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
924 UINT offset = reg->idx;
928 case WINED3DSPR_TEMP:
932 case WINED3DSPR_INPUT:
936 case WINED3DSPR_CONST:
937 case WINED3DSPR_CONST2:
938 case WINED3DSPR_CONST3:
939 case WINED3DSPR_CONST4:
941 offset = shader_get_float_offset(reg->type, reg->idx);
944 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
945 TRACE("%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
948 case WINED3DSPR_RASTOUT:
949 TRACE("%s", rastout_reg_names[reg->idx]);
952 case WINED3DSPR_COLOROUT:
956 case WINED3DSPR_DEPTHOUT:
960 case WINED3DSPR_ATTROUT:
964 case WINED3DSPR_TEXCRDOUT:
965 /* Vertex shaders >= 3.0 use general purpose output registers
966 * (WINED3DSPR_OUTPUT), which can include an address token. */
967 if (shader_version->major >= 3) TRACE("o");
971 case WINED3DSPR_CONSTINT:
975 case WINED3DSPR_CONSTBOOL:
979 case WINED3DSPR_LABEL:
983 case WINED3DSPR_LOOP:
987 case WINED3DSPR_SAMPLER:
991 case WINED3DSPR_MISCTYPE:
992 if (reg->idx > 1) FIXME("Unhandled misctype register %u.\n", reg->idx);
993 else TRACE("%s", misctype_reg_names[reg->idx]);
996 case WINED3DSPR_PREDICATE:
1000 case WINED3DSPR_IMMCONST:
1004 case WINED3DSPR_CONSTBUFFER:
1008 case WINED3DSPR_NULL:
1012 case WINED3DSPR_RESOURCE:
1017 TRACE("unhandled_rtype(%#x)", reg->type);
1021 if (reg->type == WINED3DSPR_IMMCONST)
1024 switch (reg->immconst_type)
1026 case WINED3D_IMMCONST_SCALAR:
1027 TRACE("%.8e", *(const float *)reg->immconst_data);
1030 case WINED3D_IMMCONST_VEC4:
1031 TRACE("%.8e, %.8e, %.8e, %.8e",
1032 *(const float *)®->immconst_data[0], *(const float *)®->immconst_data[1],
1033 *(const float *)®->immconst_data[2], *(const float *)®->immconst_data[3]);
1037 TRACE("<unhandled immconst_type %#x>", reg->immconst_type);
1042 else if (reg->type != WINED3DSPR_RASTOUT
1043 && reg->type != WINED3DSPR_MISCTYPE
1044 && reg->type != WINED3DSPR_NULL)
1046 if (reg->array_idx != ~0U)
1048 TRACE("%u[%u", offset, reg->array_idx);
1052 shader_dump_src_param(reg->rel_addr, shader_version);
1061 shader_dump_src_param(reg->rel_addr, shader_version);
1064 TRACE("%u", offset);
1065 if (reg->rel_addr) TRACE("]");
1070 void shader_dump_dst_param(const struct wined3d_shader_dst_param *param,
1071 const struct wined3d_shader_version *shader_version)
1073 DWORD write_mask = param->write_mask;
1075 shader_dump_register(¶m->reg, shader_version);
1077 if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
1079 static const char *write_mask_chars = "xyzw";
1082 if (write_mask & WINED3DSP_WRITEMASK_0) TRACE("%c", write_mask_chars[0]);
1083 if (write_mask & WINED3DSP_WRITEMASK_1) TRACE("%c", write_mask_chars[1]);
1084 if (write_mask & WINED3DSP_WRITEMASK_2) TRACE("%c", write_mask_chars[2]);
1085 if (write_mask & WINED3DSP_WRITEMASK_3) TRACE("%c", write_mask_chars[3]);
1089 void shader_dump_src_param(const struct wined3d_shader_src_param *param,
1090 const struct wined3d_shader_version *shader_version)
1092 enum wined3d_shader_src_modifier src_modifier = param->modifiers;
1093 DWORD swizzle = param->swizzle;
1095 if (src_modifier == WINED3DSPSM_NEG
1096 || src_modifier == WINED3DSPSM_BIASNEG
1097 || src_modifier == WINED3DSPSM_SIGNNEG
1098 || src_modifier == WINED3DSPSM_X2NEG
1099 || src_modifier == WINED3DSPSM_ABSNEG)
1101 else if (src_modifier == WINED3DSPSM_COMP)
1103 else if (src_modifier == WINED3DSPSM_NOT)
1106 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
1109 shader_dump_register(¶m->reg, shader_version);
1113 switch (src_modifier)
1115 case WINED3DSPSM_NONE: break;
1116 case WINED3DSPSM_NEG: break;
1117 case WINED3DSPSM_NOT: break;
1118 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
1119 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
1120 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
1121 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
1122 case WINED3DSPSM_COMP: break;
1123 case WINED3DSPSM_X2: TRACE("_x2"); break;
1124 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
1125 case WINED3DSPSM_DZ: TRACE("_dz"); break;
1126 case WINED3DSPSM_DW: TRACE("_dw"); break;
1127 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
1128 case WINED3DSPSM_ABS: TRACE(")"); break;
1129 default: TRACE("_unknown_modifier(%#x)", src_modifier);
1133 if (swizzle != WINED3DSP_NOSWIZZLE)
1135 static const char *swizzle_chars = "xyzw";
1136 DWORD swizzle_x = swizzle & 0x03;
1137 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1138 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1139 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1141 if (swizzle_x == swizzle_y
1142 && swizzle_x == swizzle_z
1143 && swizzle_x == swizzle_w)
1145 TRACE(".%c", swizzle_chars[swizzle_x]);
1149 TRACE(".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
1150 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
1155 /* Shared code in order to generate the bulk of the shader string.
1156 * NOTE: A description of how to parse tokens can be found on MSDN. */
1157 void shader_generate_main(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
1158 const struct wined3d_shader_reg_maps *reg_maps, const DWORD *byte_code, void *backend_ctx)
1160 struct wined3d_device *device = shader->device;
1161 const struct wined3d_shader_frontend *fe = shader->frontend;
1162 void *fe_data = shader->frontend_data;
1163 struct wined3d_shader_src_param dst_rel_addr[2];
1164 struct wined3d_shader_src_param src_rel_addr[4];
1165 struct wined3d_shader_dst_param dst_param[2];
1166 struct wined3d_shader_src_param src_param[4];
1167 struct wined3d_shader_version shader_version;
1168 struct wined3d_shader_loop_state loop_state;
1169 struct wined3d_shader_instruction ins;
1170 struct wined3d_shader_tex_mx tex_mx;
1171 struct wined3d_shader_context ctx;
1172 const DWORD *ptr = byte_code;
1175 /* Initialize current parsing state. */
1176 tex_mx.current_row = 0;
1177 loop_state.current_depth = 0;
1178 loop_state.current_reg = 0;
1180 ctx.shader = shader;
1181 ctx.gl_info = &device->adapter->gl_info;
1182 ctx.reg_maps = reg_maps;
1183 ctx.buffer = buffer;
1184 ctx.tex_mx = &tex_mx;
1185 ctx.loop_state = &loop_state;
1186 ctx.backend_data = backend_ctx;
1189 ins.dst = dst_param;
1190 ins.src = src_param;
1192 fe->shader_read_header(fe_data, &ptr, &shader_version);
1194 while (!fe->shader_is_end(fe_data, &ptr))
1196 const char *comment;
1200 /* Skip comment tokens. */
1201 fe->shader_read_comment(&ptr, &comment, &comment_size);
1202 if (comment) continue;
1205 fe->shader_read_opcode(fe_data, &ptr, &ins, ¶m_size);
1207 /* Unknown opcode and its parameters. */
1208 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1210 TRACE("Skipping unrecognized instruction.\n");
1215 /* Nothing to do. */
1216 if (ins.handler_idx == WINED3DSIH_DCL
1217 || ins.handler_idx == WINED3DSIH_NOP
1218 || ins.handler_idx == WINED3DSIH_DEF
1219 || ins.handler_idx == WINED3DSIH_DEFI
1220 || ins.handler_idx == WINED3DSIH_DEFB
1221 || ins.handler_idx == WINED3DSIH_PHASE)
1227 /* Destination tokens */
1228 for (i = 0; i < ins.dst_count; ++i)
1230 fe->shader_read_dst_param(fe_data, &ptr, &dst_param[i], &dst_rel_addr[i]);
1233 /* Predication token */
1236 FIXME("Predicates not implemented.\n");
1237 ins.predicate = *ptr++;
1240 /* Other source tokens */
1241 for (i = 0; i < ins.src_count; ++i)
1243 fe->shader_read_src_param(fe_data, &ptr, &src_param[i], &src_rel_addr[i]);
1246 /* Call appropriate function for output target */
1247 device->shader_backend->shader_handle_instruction(&ins);
1251 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param *dst)
1253 DWORD mmask = dst->modifiers;
1258 case 13: TRACE("_d8"); break;
1259 case 14: TRACE("_d4"); break;
1260 case 15: TRACE("_d2"); break;
1261 case 1: TRACE("_x2"); break;
1262 case 2: TRACE("_x4"); break;
1263 case 3: TRACE("_x8"); break;
1264 default: TRACE("_unhandled_shift(%d)", dst->shift); break;
1267 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
1268 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
1269 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
1271 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
1272 if (mmask) FIXME("_unrecognized_modifier(%#x)", mmask);
1275 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *byte_code)
1277 struct wined3d_shader_version shader_version;
1278 const DWORD *ptr = byte_code;
1279 const char *type_prefix;
1282 TRACE("Parsing %p.\n", byte_code);
1284 fe->shader_read_header(fe_data, &ptr, &shader_version);
1286 switch (shader_version.type)
1288 case WINED3D_SHADER_TYPE_VERTEX:
1292 case WINED3D_SHADER_TYPE_GEOMETRY:
1296 case WINED3D_SHADER_TYPE_PIXEL:
1301 FIXME("Unhandled shader type %#x.\n", shader_version.type);
1302 type_prefix = "unknown";
1306 TRACE("%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
1308 while (!fe->shader_is_end(fe_data, &ptr))
1310 struct wined3d_shader_instruction ins;
1311 const char *comment;
1316 fe->shader_read_comment(&ptr, &comment, &comment_size);
1319 if (comment_size > 4 && *(const DWORD *)comment == WINEMAKEFOURCC('T', 'E', 'X', 'T'))
1321 const char *end = comment + comment_size;
1322 const char *ptr = comment + 4;
1323 const char *line = ptr;
1330 UINT len = ptr - line;
1331 if (len && *(ptr - 1) == '\r') --len;
1332 TRACE("// %s\n", debugstr_an(line, len));
1337 if (line != ptr) TRACE("// %s\n", debugstr_an(line, ptr - line));
1339 else TRACE("// %s\n", debugstr_an(comment, comment_size));
1343 fe->shader_read_opcode(fe_data, &ptr, &ins, ¶m_size);
1344 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1346 TRACE("Skipping unrecognized instruction.\n");
1351 if (ins.handler_idx == WINED3DSIH_DCL)
1353 struct wined3d_shader_semantic semantic;
1355 fe->shader_read_semantic(&ptr, &semantic);
1357 shader_dump_decl_usage(&semantic, &shader_version);
1358 shader_dump_ins_modifiers(&semantic.reg);
1360 shader_dump_dst_param(&semantic.reg, &shader_version);
1362 else if (ins.handler_idx == WINED3DSIH_DEF)
1364 struct wined3d_shader_src_param rel_addr;
1365 struct wined3d_shader_dst_param dst;
1367 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1369 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(dst.reg.type, dst.reg.idx),
1370 *(const float *)(ptr),
1371 *(const float *)(ptr + 1),
1372 *(const float *)(ptr + 2),
1373 *(const float *)(ptr + 3));
1376 else if (ins.handler_idx == WINED3DSIH_DEFI)
1378 struct wined3d_shader_src_param rel_addr;
1379 struct wined3d_shader_dst_param dst;
1381 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1383 TRACE("defi i%u = %d, %d, %d, %d", dst.reg.idx,
1390 else if (ins.handler_idx == WINED3DSIH_DEFB)
1392 struct wined3d_shader_src_param rel_addr;
1393 struct wined3d_shader_dst_param dst;
1395 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1397 TRACE("defb b%u = %s", dst.reg.idx, *ptr ? "true" : "false");
1402 struct wined3d_shader_src_param dst_rel_addr[2];
1403 struct wined3d_shader_src_param src_rel_addr;
1404 struct wined3d_shader_dst_param dst_param[2];
1405 struct wined3d_shader_src_param src_param;
1407 for (i = 0; i < ins.dst_count; ++i)
1409 fe->shader_read_dst_param(fe_data, &ptr, &dst_param[i], &dst_rel_addr[i]);
1412 /* Print out predication source token first - it follows
1413 * the destination token. */
1416 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
1418 shader_dump_src_param(&src_param, &shader_version);
1422 /* PixWin marks instructions with the coissue flag with a '+' */
1423 if (ins.coissue) TRACE("+");
1425 TRACE("%s", shader_opcode_names[ins.handler_idx]);
1427 if (ins.handler_idx == WINED3DSIH_IFC
1428 || ins.handler_idx == WINED3DSIH_BREAKC)
1432 case WINED3D_SHADER_REL_OP_GT: TRACE("_gt"); break;
1433 case WINED3D_SHADER_REL_OP_EQ: TRACE("_eq"); break;
1434 case WINED3D_SHADER_REL_OP_GE: TRACE("_ge"); break;
1435 case WINED3D_SHADER_REL_OP_LT: TRACE("_lt"); break;
1436 case WINED3D_SHADER_REL_OP_NE: TRACE("_ne"); break;
1437 case WINED3D_SHADER_REL_OP_LE: TRACE("_le"); break;
1438 default: TRACE("_(%u)", ins.flags);
1441 else if (ins.handler_idx == WINED3DSIH_TEX
1442 && shader_version.major >= 2
1443 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
1448 /* We already read the destination tokens, print them. */
1449 for (i = 0; i < ins.dst_count; ++i)
1451 shader_dump_ins_modifiers(&dst_param[i]);
1452 TRACE(!i ? " " : ", ");
1453 shader_dump_dst_param(&dst_param[i], &shader_version);
1456 /* Other source tokens */
1457 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
1459 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
1460 TRACE(!i ? " " : ", ");
1461 shader_dump_src_param(&src_param, &shader_version);
1468 static void shader_cleanup(struct wined3d_shader *shader)
1470 shader->device->shader_backend->shader_destroy(shader);
1471 HeapFree(GetProcessHeap(), 0, shader->reg_maps.constf);
1472 HeapFree(GetProcessHeap(), 0, shader->function);
1473 shader_delete_constant_list(&shader->constantsF);
1474 shader_delete_constant_list(&shader->constantsB);
1475 shader_delete_constant_list(&shader->constantsI);
1476 list_remove(&shader->shader_list_entry);
1478 if (shader->frontend && shader->frontend_data)
1479 shader->frontend->shader_free(shader->frontend_data);
1482 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
1483 static void shader_none_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS) {}
1484 static void shader_none_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
1485 enum tex_types tex_type, const SIZE *ds_mask_size) {}
1486 static void shader_none_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info) {}
1487 static void shader_none_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count) {}
1488 static void shader_none_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count) {}
1489 static void shader_none_load_constants(const struct wined3d_context *context, char usePS, char useVS) {}
1490 static void shader_none_load_np2fixup_constants(void *shader_priv,
1491 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state) {}
1492 static void shader_none_destroy(struct wined3d_shader *shader) {}
1493 static HRESULT shader_none_alloc(struct wined3d_device *device) {return WINED3D_OK;}
1494 static void shader_none_free(struct wined3d_device *device) {}
1495 static void shader_none_context_destroyed(void *shader_priv, const struct wined3d_context *context) {}
1497 static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
1499 /* Set the shader caps to 0 for the none shader backend */
1500 caps->VertexShaderVersion = 0;
1501 caps->MaxVertexShaderConst = 0;
1502 caps->PixelShaderVersion = 0;
1503 caps->PixelShader1xMaxValue = 0.0f;
1504 caps->MaxPixelShaderConst = 0;
1505 caps->VSClipping = FALSE;
1508 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1510 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1512 TRACE("Checking support for fixup:\n");
1513 dump_color_fixup_desc(fixup);
1516 /* Faked to make some apps happy. */
1517 if (!is_complex_fixup(fixup))
1523 TRACE("[FAILED]\n");
1527 const struct wined3d_shader_backend_ops none_shader_backend =
1529 shader_none_handle_instruction,
1531 shader_none_select_depth_blt,
1532 shader_none_deselect_depth_blt,
1533 shader_none_update_float_vertex_constants,
1534 shader_none_update_float_pixel_constants,
1535 shader_none_load_constants,
1536 shader_none_load_np2fixup_constants,
1537 shader_none_destroy,
1540 shader_none_context_destroyed,
1541 shader_none_get_caps,
1542 shader_none_color_fixup_supported,
1545 static HRESULT shader_set_function(struct wined3d_shader *shader, const DWORD *byte_code,
1546 const struct wined3d_shader_signature *output_signature, DWORD float_const_count,
1547 enum wined3d_shader_type type, unsigned int max_version)
1549 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1550 const struct wined3d_shader_frontend *fe;
1552 unsigned int backend_version;
1554 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1555 shader, byte_code, output_signature, float_const_count);
1557 fe = shader_select_frontend(*byte_code);
1560 FIXME("Unable to find frontend for shader.\n");
1561 return WINED3DERR_INVALIDCALL;
1563 shader->frontend = fe;
1564 shader->frontend_data = fe->shader_init(byte_code, output_signature);
1565 if (!shader->frontend_data)
1567 FIXME("Failed to initialize frontend.\n");
1568 return WINED3DERR_INVALIDCALL;
1571 /* First pass: trace shader. */
1572 if (TRACE_ON(d3d_shader))
1573 shader_trace_init(fe, shader->frontend_data, byte_code);
1575 /* Initialize immediate constant lists. */
1576 list_init(&shader->constantsF);
1577 list_init(&shader->constantsB);
1578 list_init(&shader->constantsI);
1580 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1581 hr = shader_get_registers_used(shader, fe,
1582 reg_maps, shader->input_signature, shader->output_signature,
1583 byte_code, float_const_count);
1584 if (FAILED(hr)) return hr;
1586 if (reg_maps->shader_version.type != type)
1588 WARN("Wrong shader type %d.\n", reg_maps->shader_version.type);
1589 return WINED3DERR_INVALIDCALL;
1591 if (reg_maps->shader_version.major > max_version)
1593 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps->shader_version.major);
1594 return WINED3DERR_INVALIDCALL;
1598 case WINED3D_SHADER_TYPE_VERTEX:
1599 backend_version = shader->device->vshader_version;
1601 case WINED3D_SHADER_TYPE_PIXEL:
1602 backend_version = shader->device->pshader_version;
1605 FIXME("No backend version-checking for this shader type\n");
1606 backend_version = 0;
1608 if (reg_maps->shader_version.major > backend_version)
1610 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
1611 reg_maps->shader_version.major, reg_maps->shader_version.minor);
1612 return WINED3DERR_INVALIDCALL;
1615 shader->function = HeapAlloc(GetProcessHeap(), 0, shader->functionLength);
1616 if (!shader->function)
1617 return E_OUTOFMEMORY;
1618 memcpy(shader->function, byte_code, shader->functionLength);
1623 ULONG CDECL wined3d_shader_incref(struct wined3d_shader *shader)
1625 ULONG refcount = InterlockedIncrement(&shader->ref);
1627 TRACE("%p increasing refcount to %u.\n", shader, refcount);
1632 /* Do not call while under the GL lock. */
1633 ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
1635 ULONG refcount = InterlockedDecrement(&shader->ref);
1637 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1641 shader_cleanup(shader);
1642 shader->parent_ops->wined3d_object_destroyed(shader->parent);
1643 HeapFree(GetProcessHeap(), 0, shader);
1649 void * CDECL wined3d_shader_get_parent(const struct wined3d_shader *shader)
1651 TRACE("shader %p.\n", shader);
1653 return shader->parent;
1656 HRESULT CDECL wined3d_shader_get_byte_code(const struct wined3d_shader *shader,
1657 void *byte_code, UINT *byte_code_size)
1659 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader, byte_code, byte_code_size);
1663 *byte_code_size = shader->functionLength;
1667 if (*byte_code_size < shader->functionLength)
1669 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
1670 * than the required size we should write the required size and
1671 * return D3DERR_MOREDATA. That's not actually true. */
1672 return WINED3DERR_INVALIDCALL;
1675 memcpy(byte_code, shader->function, shader->functionLength);
1680 /* Set local constants for d3d8 shaders. */
1681 HRESULT CDECL wined3d_shader_set_local_constants_float(struct wined3d_shader *shader,
1682 UINT start_idx, const float *src_data, UINT count)
1684 UINT end_idx = start_idx + count;
1687 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader, start_idx, src_data, count);
1689 if (end_idx > shader->limits.constant_float)
1691 WARN("end_idx %u > float constants limit %u.\n",
1692 end_idx, shader->limits.constant_float);
1693 end_idx = shader->limits.constant_float;
1696 for (i = start_idx; i < end_idx; ++i)
1698 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1700 return E_OUTOFMEMORY;
1703 memcpy(lconst->value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
1704 list_add_head(&shader->constantsF, &lconst->entry);
1710 void find_vs_compile_args(const struct wined3d_state *state,
1711 const struct wined3d_shader *shader, struct vs_compile_args *args)
1713 args->fog_src = state->render_states[WINED3D_RS_FOGTABLEMODE]
1714 == WINED3D_FOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
1715 args->clip_enabled = state->render_states[WINED3D_RS_CLIPPING]
1716 && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
1717 args->swizzle_map = shader->device->strided_streams.swizzle_map;
1720 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
1722 if (usage_idx1 != usage_idx2)
1724 if (usage1 == usage2)
1726 if (usage1 == WINED3D_DECL_USAGE_POSITION && usage2 == WINED3D_DECL_USAGE_POSITIONT)
1728 if (usage2 == WINED3D_DECL_USAGE_POSITION && usage1 == WINED3D_DECL_USAGE_POSITIONT)
1734 BOOL vshader_get_input(const struct wined3d_shader *shader,
1735 BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
1737 WORD map = shader->reg_maps.input_registers;
1740 for (i = 0; map; map >>= 1, ++i)
1742 if (!(map & 1)) continue;
1744 if (match_usage(shader->u.vs.attributes[i].usage,
1745 shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
1754 static void vertexshader_set_limits(struct wined3d_shader *shader)
1756 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
1757 shader->reg_maps.shader_version.minor);
1758 struct wined3d_device *device = shader->device;
1760 shader->limits.texcoord = 0;
1761 shader->limits.attributes = 16;
1762 shader->limits.packed_input = 0;
1764 switch (shader_version)
1766 case WINED3D_SHADER_VERSION(1, 0):
1767 case WINED3D_SHADER_VERSION(1, 1):
1768 shader->limits.temporary = 12;
1769 shader->limits.constant_bool = 0;
1770 shader->limits.constant_int = 0;
1771 shader->limits.address = 1;
1772 shader->limits.packed_output = 12;
1773 shader->limits.sampler = 0;
1774 shader->limits.label = 0;
1775 /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1776 * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1778 shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1781 case WINED3D_SHADER_VERSION(2, 0):
1782 case WINED3D_SHADER_VERSION(2, 1):
1783 shader->limits.temporary = 12;
1784 shader->limits.constant_bool = 16;
1785 shader->limits.constant_int = 16;
1786 shader->limits.address = 1;
1787 shader->limits.packed_output = 12;
1788 shader->limits.sampler = 0;
1789 shader->limits.label = 16;
1790 shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1793 case WINED3D_SHADER_VERSION(4, 0):
1794 FIXME("Using 3.0 limits for 4.0 shader.\n");
1797 case WINED3D_SHADER_VERSION(3, 0):
1798 shader->limits.temporary = 32;
1799 shader->limits.constant_bool = 32;
1800 shader->limits.constant_int = 32;
1801 shader->limits.address = 1;
1802 shader->limits.packed_output = 12;
1803 shader->limits.sampler = 4;
1804 shader->limits.label = 16; /* FIXME: 2048 */
1805 /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1806 * even though they are capable of supporting much more (GL
1807 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1808 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1809 * shaders to 256. */
1810 shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1814 shader->limits.temporary = 12;
1815 shader->limits.constant_bool = 16;
1816 shader->limits.constant_int = 16;
1817 shader->limits.address = 1;
1818 shader->limits.packed_output = 12;
1819 shader->limits.sampler = 0;
1820 shader->limits.label = 16;
1821 shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1822 FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1823 shader->reg_maps.shader_version.major,
1824 shader->reg_maps.shader_version.minor);
1828 static HRESULT vertexshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
1829 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1830 void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
1832 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1837 if (!byte_code) return WINED3DERR_INVALIDCALL;
1839 shader_init(shader, device, parent, parent_ops);
1840 hr = shader_set_function(shader, byte_code, output_signature, device->d3d_vshader_constantF,
1841 WINED3D_SHADER_TYPE_VERTEX, max_version);
1844 WARN("Failed to set function, hr %#x.\n", hr);
1845 shader_cleanup(shader);
1849 map = reg_maps->input_registers;
1850 for (i = 0; map; map >>= 1, ++i)
1852 if (!(map & 1) || !shader->input_signature[i].semantic_name)
1855 shader->u.vs.attributes[i].usage =
1856 shader_usage_from_semantic_name(shader->input_signature[i].semantic_name);
1857 shader->u.vs.attributes[i].usage_idx = shader->input_signature[i].semantic_idx;
1860 if (output_signature)
1862 for (i = 0; i < output_signature->element_count; ++i)
1864 struct wined3d_shader_signature_element *e = &output_signature->elements[i];
1865 reg_maps->output_registers |= 1 << e->register_idx;
1866 shader->output_signature[e->register_idx] = *e;
1870 vertexshader_set_limits(shader);
1872 shader->load_local_constsF = reg_maps->usesrelconstF
1873 && !list_empty(&shader->constantsF);
1878 static HRESULT geometryshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
1879 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1880 void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
1884 shader_init(shader, device, parent, parent_ops);
1885 hr = shader_set_function(shader, byte_code, output_signature, 0,
1886 WINED3D_SHADER_TYPE_GEOMETRY, max_version);
1889 WARN("Failed to set function, hr %#x.\n", hr);
1890 shader_cleanup(shader);
1894 shader->load_local_constsF = FALSE;
1899 void find_ps_compile_args(const struct wined3d_state *state,
1900 const struct wined3d_shader *shader, struct ps_compile_args *args)
1902 struct wined3d_device *device = shader->device;
1903 const struct wined3d_texture *texture;
1906 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
1907 if (state->render_states[WINED3D_RS_SRGBWRITEENABLE])
1909 const struct wined3d_surface *rt = state->fb->render_targets[0];
1910 if (rt->resource.format->flags & WINED3DFMT_FLAG_SRGB_WRITE) args->srgb_correction = 1;
1913 if (shader->reg_maps.shader_version.major == 1
1914 && shader->reg_maps.shader_version.minor <= 3)
1916 for (i = 0; i < 4; ++i)
1918 DWORD flags = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
1920 if (flags & WINED3D_TTFF_PROJECTED)
1922 enum wined3d_sampler_texture_type sampler_type = shader->reg_maps.sampler_type[i];
1923 DWORD tex_transform = flags & ~WINED3D_TTFF_PROJECTED;
1924 DWORD max_valid = WINED3D_TTFF_COUNT4;
1926 if (!state->vertex_shader)
1929 unsigned int index = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
1930 for (j = 0; j < state->vertex_declaration->element_count; ++j)
1932 struct wined3d_vertex_declaration_element *element =
1933 &state->vertex_declaration->elements[j];
1935 if (element->usage == WINED3D_DECL_USAGE_TEXCOORD
1936 && element->usage_idx == index)
1938 max_valid = element->format->component_count;
1944 if (!tex_transform || tex_transform > max_valid)
1946 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
1947 tex_transform, max_valid);
1948 tex_transform = max_valid;
1951 if ((sampler_type == WINED3DSTT_1D && tex_transform > WINED3D_TTFF_COUNT1)
1952 || (sampler_type == WINED3DSTT_2D && tex_transform > WINED3D_TTFF_COUNT2)
1953 || (sampler_type == WINED3DSTT_VOLUME && tex_transform > WINED3D_TTFF_COUNT3))
1954 tex_transform |= WINED3D_PSARGS_PROJECTED;
1956 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
1958 args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
1963 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
1965 if (!shader->reg_maps.sampler_type[i])
1968 texture = state->textures[i];
1971 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
1974 args->color_fixup[i] = texture->resource.format->color_fixup;
1976 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
1977 args->shadow |= 1 << i;
1979 /* Flag samplers that need NP2 texcoord fixup. */
1980 if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
1981 args->np2_fixup |= (1 << i);
1983 if (shader->reg_maps.shader_version.major >= 3)
1985 if (device->strided_streams.position_transformed)
1987 args->vp_mode = pretransformed;
1989 else if (use_vs(state))
1991 args->vp_mode = vertexshader;
1995 args->vp_mode = fixedfunction;
1997 args->fog = FOG_OFF;
2001 args->vp_mode = vertexshader;
2002 if (state->render_states[WINED3D_RS_FOGENABLE])
2004 switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
2006 case WINED3D_FOG_NONE:
2007 if (device->strided_streams.position_transformed || use_vs(state))
2009 args->fog = FOG_LINEAR;
2013 switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
2015 case WINED3D_FOG_NONE: /* Fall through. */
2016 case WINED3D_FOG_LINEAR: args->fog = FOG_LINEAR; break;
2017 case WINED3D_FOG_EXP: args->fog = FOG_EXP; break;
2018 case WINED3D_FOG_EXP2: args->fog = FOG_EXP2; break;
2022 case WINED3D_FOG_LINEAR: args->fog = FOG_LINEAR; break;
2023 case WINED3D_FOG_EXP: args->fog = FOG_EXP; break;
2024 case WINED3D_FOG_EXP2: args->fog = FOG_EXP2; break;
2029 args->fog = FOG_OFF;
2034 static void pixelshader_set_limits(struct wined3d_shader *shader)
2036 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
2037 shader->reg_maps.shader_version.minor);
2039 shader->limits.attributes = 0;
2040 shader->limits.address = 0;
2041 shader->limits.packed_output = 0;
2043 switch (shader_version)
2045 case WINED3D_SHADER_VERSION(1, 0):
2046 case WINED3D_SHADER_VERSION(1, 1):
2047 case WINED3D_SHADER_VERSION(1, 2):
2048 case WINED3D_SHADER_VERSION(1, 3):
2049 shader->limits.temporary = 2;
2050 shader->limits.constant_float = 8;
2051 shader->limits.constant_int = 0;
2052 shader->limits.constant_bool = 0;
2053 shader->limits.texcoord = 4;
2054 shader->limits.sampler = 4;
2055 shader->limits.packed_input = 0;
2056 shader->limits.label = 0;
2059 case WINED3D_SHADER_VERSION(1, 4):
2060 shader->limits.temporary = 6;
2061 shader->limits.constant_float = 8;
2062 shader->limits.constant_int = 0;
2063 shader->limits.constant_bool = 0;
2064 shader->limits.texcoord = 6;
2065 shader->limits.sampler = 6;
2066 shader->limits.packed_input = 0;
2067 shader->limits.label = 0;
2070 /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2071 case WINED3D_SHADER_VERSION(2, 0):
2072 shader->limits.temporary = 32;
2073 shader->limits.constant_float = 32;
2074 shader->limits.constant_int = 16;
2075 shader->limits.constant_bool = 16;
2076 shader->limits.texcoord = 8;
2077 shader->limits.sampler = 16;
2078 shader->limits.packed_input = 0;
2081 case WINED3D_SHADER_VERSION(2, 1):
2082 shader->limits.temporary = 32;
2083 shader->limits.constant_float = 32;
2084 shader->limits.constant_int = 16;
2085 shader->limits.constant_bool = 16;
2086 shader->limits.texcoord = 8;
2087 shader->limits.sampler = 16;
2088 shader->limits.packed_input = 0;
2089 shader->limits.label = 16;
2092 case WINED3D_SHADER_VERSION(4, 0):
2093 FIXME("Using 3.0 limits for 4.0 shader.\n");
2096 case WINED3D_SHADER_VERSION(3, 0):
2097 shader->limits.temporary = 32;
2098 shader->limits.constant_float = 224;
2099 shader->limits.constant_int = 16;
2100 shader->limits.constant_bool = 16;
2101 shader->limits.texcoord = 0;
2102 shader->limits.sampler = 16;
2103 shader->limits.packed_input = 12;
2104 shader->limits.label = 16; /* FIXME: 2048 */
2108 shader->limits.temporary = 32;
2109 shader->limits.constant_float = 32;
2110 shader->limits.constant_int = 16;
2111 shader->limits.constant_bool = 16;
2112 shader->limits.texcoord = 8;
2113 shader->limits.sampler = 16;
2114 shader->limits.packed_input = 0;
2115 shader->limits.label = 0;
2116 FIXME("Unrecognized pixel shader version %u.%u\n",
2117 shader->reg_maps.shader_version.major,
2118 shader->reg_maps.shader_version.minor);
2122 static HRESULT pixelshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2123 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
2124 void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
2126 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2127 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
2130 if (!byte_code) return WINED3DERR_INVALIDCALL;
2132 shader_init(shader, device, parent, parent_ops);
2133 hr = shader_set_function(shader, byte_code, output_signature, device->d3d_pshader_constantF,
2134 WINED3D_SHADER_TYPE_PIXEL, max_version);
2137 WARN("Failed to set function, hr %#x.\n", hr);
2138 shader_cleanup(shader);
2142 pixelshader_set_limits(shader);
2144 for (i = 0; i < MAX_REG_INPUT; ++i)
2146 if (shader->u.ps.input_reg_used[i])
2149 highest_reg_used = i;
2153 /* Don't do any register mapping magic if it is not needed, or if we can't
2154 * achieve anything anyway */
2155 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
2156 || num_regs_used > (gl_info->limits.glsl_varyings / 4))
2158 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
2160 /* This happens with relative addressing. The input mapper function
2161 * warns about this if the higher registers are declared too, so
2162 * don't write a FIXME here */
2163 WARN("More varying registers used than supported\n");
2166 for (i = 0; i < MAX_REG_INPUT; ++i)
2168 shader->u.ps.input_reg_map[i] = i;
2171 shader->u.ps.declared_in_count = highest_reg_used + 1;
2175 shader->u.ps.declared_in_count = 0;
2176 for (i = 0; i < MAX_REG_INPUT; ++i)
2178 if (shader->u.ps.input_reg_used[i])
2179 shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
2180 else shader->u.ps.input_reg_map[i] = ~0U;
2184 shader->load_local_constsF = FALSE;
2189 void pixelshader_update_samplers(struct wined3d_shader_reg_maps *reg_maps, struct wined3d_texture * const *textures)
2191 enum wined3d_sampler_texture_type *sampler_type = reg_maps->sampler_type;
2194 if (reg_maps->shader_version.major != 1) return;
2196 for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
2198 /* We don't sample from this sampler. */
2199 if (!sampler_type[i]) continue;
2203 WARN("No texture bound to sampler %u, using 2D.\n", i);
2204 sampler_type[i] = WINED3DSTT_2D;
2208 switch (textures[i]->target)
2210 case GL_TEXTURE_RECTANGLE_ARB:
2212 /* We have to select between texture rectangles and 2D
2213 * textures later because 2.0 and 3.0 shaders only have
2214 * WINED3DSTT_2D as well. */
2215 sampler_type[i] = WINED3DSTT_2D;
2219 sampler_type[i] = WINED3DSTT_VOLUME;
2222 case GL_TEXTURE_CUBE_MAP_ARB:
2223 sampler_type[i] = WINED3DSTT_CUBE;
2227 FIXME("Unrecognized texture type %#x, using 2D.\n", textures[i]->target);
2228 sampler_type[i] = WINED3DSTT_2D;
2233 HRESULT CDECL wined3d_shader_create_gs(struct wined3d_device *device, const DWORD *byte_code,
2234 const struct wined3d_shader_signature *output_signature, void *parent,
2235 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2237 struct wined3d_shader *object;
2240 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2241 device, byte_code, output_signature, parent, parent_ops, shader);
2243 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2246 ERR("Failed to allocate shader memory.\n");
2247 return E_OUTOFMEMORY;
2250 hr = geometryshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2253 WARN("Failed to initialize geometry shader, hr %#x.\n", hr);
2254 HeapFree(GetProcessHeap(), 0, object);
2258 TRACE("Created geometry shader %p.\n", object);
2264 HRESULT CDECL wined3d_shader_create_ps(struct wined3d_device *device, const DWORD *byte_code,
2265 const struct wined3d_shader_signature *output_signature, void *parent,
2266 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2268 struct wined3d_shader *object;
2271 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2272 device, byte_code, output_signature, parent, parent_ops, shader);
2274 if (device->ps_selected_mode == SHADER_NONE)
2275 return WINED3DERR_INVALIDCALL;
2277 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2280 ERR("Failed to allocate shader memory.\n");
2281 return E_OUTOFMEMORY;
2284 hr = pixelshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2287 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2288 HeapFree(GetProcessHeap(), 0, object);
2292 TRACE("Created pixel shader %p.\n", object);
2298 HRESULT CDECL wined3d_shader_create_vs(struct wined3d_device *device, const DWORD *byte_code,
2299 const struct wined3d_shader_signature *output_signature, void *parent,
2300 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2302 struct wined3d_shader *object;
2305 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2306 device, byte_code, output_signature, parent, parent_ops, shader);
2308 if (device->vs_selected_mode == SHADER_NONE)
2309 return WINED3DERR_INVALIDCALL;
2311 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2314 ERR("Failed to allocate shader memory.\n");
2315 return E_OUTOFMEMORY;
2318 hr = vertexshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2321 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2322 HeapFree(GetProcessHeap(), 0, object);
2326 TRACE("Created vertex shader %p.\n", object);