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
26 #include "wine/port.h"
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
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_DCL_CONSTANT_BUFFER */ "dcl_constantBuffer",
53 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ "dcl_inputPrimitive",
54 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ "dcl_outputTopology",
55 /* WINED3DSIH_DCL_VERTICES_OUT */ "dcl_maxOutputVertexCount",
56 /* WINED3DSIH_DEF */ "def",
57 /* WINED3DSIH_DEFB */ "defb",
58 /* WINED3DSIH_DEFI */ "defi",
59 /* WINED3DSIH_DIV */ "div",
60 /* WINED3DSIH_DP2ADD */ "dp2add",
61 /* WINED3DSIH_DP3 */ "dp3",
62 /* WINED3DSIH_DP4 */ "dp4",
63 /* WINED3DSIH_DST */ "dst",
64 /* WINED3DSIH_DSX */ "dsx",
65 /* WINED3DSIH_DSY */ "dsy",
66 /* WINED3DSIH_ELSE */ "else",
67 /* WINED3DSIH_EMIT */ "emit",
68 /* WINED3DSIH_ENDIF */ "endif",
69 /* WINED3DSIH_ENDLOOP */ "endloop",
70 /* WINED3DSIH_ENDREP */ "endrep",
71 /* WINED3DSIH_EQ */ "eq",
72 /* WINED3DSIH_EXP */ "exp",
73 /* WINED3DSIH_EXPP */ "expp",
74 /* WINED3DSIH_FRC */ "frc",
75 /* WINED3DSIH_FTOI */ "ftoi",
76 /* WINED3DSIH_GE */ "ge",
77 /* WINED3DSIH_IADD */ "iadd",
78 /* WINED3DSIH_IEQ */ "ieq",
79 /* WINED3DSIH_IF */ "if",
80 /* WINED3DSIH_IFC */ "ifc",
81 /* WINED3DSIH_IGE */ "ige",
82 /* WINED3DSIH_IMUL */ "imul",
83 /* WINED3DSIH_ITOF */ "itof",
84 /* WINED3DSIH_LABEL */ "label",
85 /* WINED3DSIH_LD */ "ld",
86 /* WINED3DSIH_LIT */ "lit",
87 /* WINED3DSIH_LOG */ "log",
88 /* WINED3DSIH_LOGP */ "logp",
89 /* WINED3DSIH_LOOP */ "loop",
90 /* WINED3DSIH_LRP */ "lrp",
91 /* WINED3DSIH_LT */ "lt",
92 /* WINED3DSIH_M3x2 */ "m3x2",
93 /* WINED3DSIH_M3x3 */ "m3x3",
94 /* WINED3DSIH_M3x4 */ "m3x4",
95 /* WINED3DSIH_M4x3 */ "m4x3",
96 /* WINED3DSIH_M4x4 */ "m4x4",
97 /* WINED3DSIH_MAD */ "mad",
98 /* WINED3DSIH_MAX */ "max",
99 /* WINED3DSIH_MIN */ "min",
100 /* WINED3DSIH_MOV */ "mov",
101 /* WINED3DSIH_MOVA */ "mova",
102 /* WINED3DSIH_MOVC */ "movc",
103 /* WINED3DSIH_MUL */ "mul",
104 /* WINED3DSIH_NOP */ "nop",
105 /* WINED3DSIH_NRM */ "nrm",
106 /* WINED3DSIH_PHASE */ "phase",
107 /* WINED3DSIH_POW */ "pow",
108 /* WINED3DSIH_RCP */ "rcp",
109 /* WINED3DSIH_REP */ "rep",
110 /* WINED3DSIH_RET */ "ret",
111 /* WINED3DSIH_ROUND_NI */ "round_ni",
112 /* WINED3DSIH_RSQ */ "rsq",
113 /* WINED3DSIH_SAMPLE */ "sample",
114 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
115 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
116 /* WINED3DSIH_SETP */ "setp",
117 /* WINED3DSIH_SGE */ "sge",
118 /* WINED3DSIH_SGN */ "sgn",
119 /* WINED3DSIH_SINCOS */ "sincos",
120 /* WINED3DSIH_SLT */ "slt",
121 /* WINED3DSIH_SQRT */ "sqrt",
122 /* WINED3DSIH_SUB */ "sub",
123 /* WINED3DSIH_TEX */ "texld",
124 /* WINED3DSIH_TEXBEM */ "texbem",
125 /* WINED3DSIH_TEXBEML */ "texbeml",
126 /* WINED3DSIH_TEXCOORD */ "texcrd",
127 /* WINED3DSIH_TEXDEPTH */ "texdepth",
128 /* WINED3DSIH_TEXDP3 */ "texdp3",
129 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
130 /* WINED3DSIH_TEXKILL */ "texkill",
131 /* WINED3DSIH_TEXLDD */ "texldd",
132 /* WINED3DSIH_TEXLDL */ "texldl",
133 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
134 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
135 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
136 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
137 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
138 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
139 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
140 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
141 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
142 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
143 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
144 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
145 /* WINED3DSIH_UDIV */ "udiv",
146 /* WINED3DSIH_USHR */ "ushr",
147 /* WINED3DSIH_UTOF */ "utof",
148 /* WINED3DSIH_XOR */ "xor",
151 static const char * const semantic_names[] =
153 /* WINED3D_DECL_USAGE_POSITION */ "SV_POSITION",
154 /* WINED3D_DECL_USAGE_BLEND_WEIGHT */ "BLENDWEIGHT",
155 /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
156 /* WINED3D_DECL_USAGE_NORMAL */ "NORMAL",
157 /* WINED3D_DECL_USAGE_PSIZE */ "PSIZE",
158 /* WINED3D_DECL_USAGE_TEXCOORD */ "TEXCOORD",
159 /* WINED3D_DECL_USAGE_TANGENT */ "TANGENT",
160 /* WINED3D_DECL_USAGE_BINORMAL */ "BINORMAL",
161 /* WINED3D_DECL_USAGE_TESS_FACTOR */ "TESSFACTOR",
162 /* WINED3D_DECL_USAGE_POSITIONT */ "POSITIONT",
163 /* WINED3D_DECL_USAGE_COLOR */ "COLOR",
164 /* WINED3D_DECL_USAGE_FOG */ "FOG",
165 /* WINED3D_DECL_USAGE_DEPTH */ "DEPTH",
166 /* WINED3D_DECL_USAGE_SAMPLE */ "SAMPLE",
169 static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage)
171 if (usage >= sizeof(semantic_names) / sizeof(*semantic_names))
173 FIXME("Unrecognized usage %#x.\n", usage);
174 return "UNRECOGNIZED";
177 return semantic_names[usage];
180 static enum wined3d_decl_usage shader_usage_from_semantic_name(const char *name)
184 for (i = 0; i < sizeof(semantic_names) / sizeof(*semantic_names); ++i)
186 if (!strcmp(name, semantic_names[i])) return i;
192 BOOL shader_match_semantic(const char *semantic_name, enum wined3d_decl_usage usage)
194 return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
197 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
198 const struct wined3d_shader_semantic *s)
200 e->semantic_name = shader_semantic_name_from_usage(s->usage);
201 e->semantic_idx = s->usage_idx;
202 e->sysval_semantic = 0;
203 e->component_type = 0;
204 e->register_idx = s->reg.reg.idx[0].offset;
205 e->mask = s->reg.write_mask;
208 static void shader_signature_from_usage(struct wined3d_shader_signature_element *e,
209 enum wined3d_decl_usage usage, UINT usage_idx, UINT reg_idx, DWORD write_mask)
211 e->semantic_name = shader_semantic_name_from_usage(usage);
212 e->semantic_idx = usage_idx;
213 e->sysval_semantic = 0;
214 e->component_type = 0;
215 e->register_idx = reg_idx;
216 e->mask = write_mask;
219 static const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token)
221 switch (version_token >> 16)
225 return &sm1_shader_frontend;
230 return &sm4_shader_frontend;
233 FIXME("Unrecognised version token %#x\n", version_token);
238 void shader_buffer_clear(struct wined3d_shader_buffer *buffer)
240 buffer->buffer[0] = '\0';
241 buffer->content_size = 0;
243 buffer->newline = TRUE;
246 BOOL shader_buffer_init(struct wined3d_shader_buffer *buffer)
248 buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
251 ERR("Failed to allocate shader buffer memory.\n");
254 buffer->buffer_size = SHADER_PGMSIZE;
256 shader_buffer_clear(buffer);
260 void shader_buffer_free(struct wined3d_shader_buffer *buffer)
262 HeapFree(GetProcessHeap(), 0, buffer->buffer);
265 int shader_vaddline(struct wined3d_shader_buffer *buffer, const char *format, va_list args)
267 char *base = buffer->buffer + buffer->content_size;
273 rc = vsnprintf(base, buffer->buffer_size - buffer->content_size, format, args);
274 if (rc < 0 /* C89 */ || (unsigned int)rc >= buffer->buffer_size - buffer->content_size /* C99 */)
276 new_buffer = HeapReAlloc(GetProcessHeap(), 0, buffer->buffer, buffer->buffer_size * 2);
279 ERR("The buffer allocated for the shader program string is too small at %d bytes.\n", buffer->buffer_size);
280 buffer->content_size = buffer->buffer_size - 1;
283 buffer->buffer = new_buffer;
284 buffer->buffer_size = buffer->buffer_size * 2;
285 base = buffer->buffer + buffer->content_size;
295 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->content_size, base);
296 buffer->newline = FALSE;
303 buffer->content_size += rc;
304 if (buffer->buffer[buffer->content_size-1] == '\n')
307 buffer->newline = TRUE;
313 int shader_addline(struct wined3d_shader_buffer *buffer, const char *format, ...)
318 va_start(args, format);
319 ret = shader_vaddline(buffer, format, args);
325 static void shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
326 void *parent, const struct wined3d_parent_ops *parent_ops)
329 shader->device = device;
330 shader->parent = parent;
331 shader->parent_ops = parent_ops;
332 list_init(&shader->linked_programs);
333 list_add_head(&device->shaders, &shader->shader_list_entry);
336 /* Convert floating point offset relative to a register file to an absolute
337 * offset for float constants. */
338 static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type, UINT register_idx)
340 switch (register_type)
342 case WINED3DSPR_CONST: return register_idx;
343 case WINED3DSPR_CONST2: return 2048 + register_idx;
344 case WINED3DSPR_CONST3: return 4096 + register_idx;
345 case WINED3DSPR_CONST4: return 6144 + register_idx;
347 FIXME("Unsupported register type: %u.\n", register_type);
352 static void shader_delete_constant_list(struct list *clist)
354 struct wined3d_shader_lconst *constant;
357 ptr = list_head(clist);
360 constant = LIST_ENTRY(ptr, struct wined3d_shader_lconst, entry);
361 ptr = list_next(clist, ptr);
362 HeapFree(GetProcessHeap(), 0, constant);
367 static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
372 bitmap[idx] |= (1 << shift);
375 static void shader_record_register_usage(struct wined3d_shader *shader, struct wined3d_shader_reg_maps *reg_maps,
376 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type)
380 case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
381 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
382 reg_maps->texcoord |= 1 << reg->idx[0].offset;
384 reg_maps->address |= 1 << reg->idx[0].offset;
387 case WINED3DSPR_TEMP:
388 reg_maps->temporary |= 1 << reg->idx[0].offset;
391 case WINED3DSPR_INPUT:
392 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
394 if (reg->idx[0].rel_addr)
396 /* If relative addressing is used, we must assume that all registers
397 * are used. Even if it is a construct like v3[aL], we can't assume
398 * that v0, v1 and v2 aren't read because aL can be negative */
400 for (i = 0; i < MAX_REG_INPUT; ++i)
402 shader->u.ps.input_reg_used[i] = TRUE;
407 shader->u.ps.input_reg_used[reg->idx[0].offset] = TRUE;
411 reg_maps->input_registers |= 1 << reg->idx[0].offset;
414 case WINED3DSPR_RASTOUT:
415 if (reg->idx[0].offset == 1)
419 case WINED3DSPR_MISCTYPE:
420 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
422 if (!reg->idx[0].offset)
424 else if (reg->idx[0].offset == 1)
425 reg_maps->usesfacing = 1;
429 case WINED3DSPR_CONST:
430 if (reg->idx[0].rel_addr)
432 if (reg->idx[0].offset < reg_maps->min_rel_offset)
433 reg_maps->min_rel_offset = reg->idx[0].offset;
434 if (reg->idx[0].offset > reg_maps->max_rel_offset)
435 reg_maps->max_rel_offset = reg->idx[0].offset;
436 reg_maps->usesrelconstF = TRUE;
440 set_bitmap_bit(reg_maps->constf, reg->idx[0].offset);
444 case WINED3DSPR_CONSTINT:
445 reg_maps->integer_constants |= (1 << reg->idx[0].offset);
448 case WINED3DSPR_CONSTBOOL:
449 reg_maps->boolean_constants |= (1 << reg->idx[0].offset);
452 case WINED3DSPR_COLOROUT:
453 reg_maps->rt_mask |= (1 << reg->idx[0].offset);
457 TRACE("Not recording register of type %#x and [%#x][%#x].\n",
458 reg->type, reg->idx[0].offset, reg->idx[1].offset);
463 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
467 case WINED3DSIH_M4x4:
468 case WINED3DSIH_M3x4:
469 return param == 1 ? 3 : 0;
471 case WINED3DSIH_M4x3:
472 case WINED3DSIH_M3x3:
473 return param == 1 ? 2 : 0;
475 case WINED3DSIH_M3x2:
476 return param == 1 ? 1 : 0;
483 /* Note that this does not count the loop register as an address register. */
484 static HRESULT shader_get_registers_used(struct wined3d_shader *shader, const struct wined3d_shader_frontend *fe,
485 struct wined3d_shader_reg_maps *reg_maps, struct wined3d_shader_signature_element *input_signature,
486 struct wined3d_shader_signature_element *output_signature, const DWORD *byte_code, DWORD constf_size)
488 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
489 void *fe_data = shader->frontend_data;
490 struct wined3d_shader_version shader_version;
491 const DWORD *ptr = byte_code;
493 memset(reg_maps, 0, sizeof(*reg_maps));
494 reg_maps->min_rel_offset = ~0U;
496 fe->shader_read_header(fe_data, &ptr, &shader_version);
497 reg_maps->shader_version = shader_version;
499 reg_maps->constf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
500 sizeof(*reg_maps->constf) * ((constf_size + 31) / 32));
501 if (!reg_maps->constf)
503 ERR("Failed to allocate constant map memory.\n");
504 return E_OUTOFMEMORY;
507 while (!fe->shader_is_end(fe_data, &ptr))
509 struct wined3d_shader_instruction ins;
512 fe->shader_read_instruction(fe_data, &ptr, &ins);
514 /* Unhandled opcode, and its parameters. */
515 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
517 TRACE("Skipping unrecognized instruction.\n");
521 /* Handle declarations. */
522 if (ins.handler_idx == WINED3DSIH_DCL)
524 struct wined3d_shader_semantic *semantic = &ins.declaration.semantic;
526 switch (semantic->reg.reg.type)
528 /* Mark input registers used. */
529 case WINED3DSPR_INPUT:
530 reg_maps->input_registers |= 1 << semantic->reg.reg.idx[0].offset;
531 shader_signature_from_semantic(&input_signature[semantic->reg.reg.idx[0].offset], semantic);
534 /* Vertex shader: mark 3.0 output registers used, save token. */
535 case WINED3DSPR_OUTPUT:
536 reg_maps->output_registers |= 1 << semantic->reg.reg.idx[0].offset;
537 shader_signature_from_semantic(&output_signature[semantic->reg.reg.idx[0].offset], semantic);
538 if (semantic->usage == WINED3D_DECL_USAGE_FOG)
542 /* Save sampler usage token. */
543 case WINED3DSPR_SAMPLER:
544 reg_maps->sampler_type[semantic->reg.reg.idx[0].offset] = semantic->sampler_type;
548 TRACE("Not recording DCL register type %#x.\n", semantic->reg.reg.type);
552 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
554 struct wined3d_shader_register *reg = &ins.declaration.src.reg;
555 if (reg->idx[0].offset >= WINED3D_MAX_CBS)
556 ERR("Invalid CB index %u.\n", reg->idx[0].offset);
558 reg_maps->cb_sizes[reg->idx[0].offset] = reg->idx[1].offset;
560 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE)
562 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
563 shader->u.gs.input_type = ins.declaration.primitive_type;
565 FIXME("Invalid instruction %#x for shader type %#x.\n",
566 ins.handler_idx, shader_version.type);
568 else if (ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
570 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
571 shader->u.gs.output_type = ins.declaration.primitive_type;
573 FIXME("Invalid instruction %#x for shader type %#x.\n",
574 ins.handler_idx, shader_version.type);
576 else if (ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
578 if (shader_version.type == WINED3D_SHADER_TYPE_GEOMETRY)
579 shader->u.gs.vertices_out = ins.declaration.count;
581 FIXME("Invalid instruction %#x for shader type %#x.\n",
582 ins.handler_idx, shader_version.type);
584 else if (ins.handler_idx == WINED3DSIH_DEF)
586 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
587 if (!lconst) return E_OUTOFMEMORY;
589 lconst->idx = ins.dst[0].reg.idx[0].offset;
590 memcpy(lconst->value, ins.src[0].reg.immconst_data, 4 * sizeof(DWORD));
592 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
593 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
595 float *value = (float *)lconst->value;
596 if (value[0] < -1.0f) value[0] = -1.0f;
597 else if (value[0] > 1.0f) value[0] = 1.0f;
598 if (value[1] < -1.0f) value[1] = -1.0f;
599 else if (value[1] > 1.0f) value[1] = 1.0f;
600 if (value[2] < -1.0f) value[2] = -1.0f;
601 else if (value[2] > 1.0f) value[2] = 1.0f;
602 if (value[3] < -1.0f) value[3] = -1.0f;
603 else if (value[3] > 1.0f) value[3] = 1.0f;
606 list_add_head(&shader->constantsF, &lconst->entry);
608 else if (ins.handler_idx == WINED3DSIH_DEFI)
610 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
611 if (!lconst) return E_OUTOFMEMORY;
613 lconst->idx = ins.dst[0].reg.idx[0].offset;
614 memcpy(lconst->value, ins.src[0].reg.immconst_data, 4 * sizeof(DWORD));
616 list_add_head(&shader->constantsI, &lconst->entry);
617 reg_maps->local_int_consts |= (1 << lconst->idx);
619 else if (ins.handler_idx == WINED3DSIH_DEFB)
621 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
622 if (!lconst) return E_OUTOFMEMORY;
624 lconst->idx = ins.dst[0].reg.idx[0].offset;
625 memcpy(lconst->value, ins.src[0].reg.immconst_data, sizeof(DWORD));
627 list_add_head(&shader->constantsB, &lconst->entry);
628 reg_maps->local_bool_consts |= (1 << lconst->idx);
630 /* For subroutine prototypes. */
631 else if (ins.handler_idx == WINED3DSIH_LABEL)
633 reg_maps->labels |= 1 << ins.src[0].reg.idx[0].offset;
635 /* Set texture, address, temporary registers. */
638 BOOL color0_mov = FALSE;
641 /* This will loop over all the registers and try to
642 * make a bitmask of the ones we're interested in.
644 * Relative addressing tokens are ignored, but that's
645 * okay, since we'll catch any address registers when
646 * they are initialized (required by spec). */
647 for (i = 0; i < ins.dst_count; ++i)
649 shader_record_register_usage(shader, reg_maps, &ins.dst[i].reg, shader_version.type);
651 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
652 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
653 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
654 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX && shader_version.major < 3)
656 UINT idx = ins.dst[i].reg.idx[0].offset;
658 switch (ins.dst[i].reg.type)
660 case WINED3DSPR_RASTOUT:
664 reg_maps->output_registers |= 1 << 10;
665 shader_signature_from_usage(&output_signature[10],
666 WINED3D_DECL_USAGE_POSITION, 0, 10, WINED3DSP_WRITEMASK_ALL);
670 reg_maps->output_registers |= 1 << 11;
671 shader_signature_from_usage(&output_signature[11],
672 WINED3D_DECL_USAGE_FOG, 0, 11, WINED3DSP_WRITEMASK_0);
676 reg_maps->output_registers |= 1 << 11;
677 shader_signature_from_usage(&output_signature[11],
678 WINED3D_DECL_USAGE_PSIZE, 0, 11, WINED3DSP_WRITEMASK_1);
683 case WINED3DSPR_ATTROUT:
687 if (reg_maps->output_registers & (1 << idx))
689 output_signature[idx].mask |= ins.dst[i].write_mask;
693 reg_maps->output_registers |= 1 << idx;
694 shader_signature_from_usage(&output_signature[idx],
695 WINED3D_DECL_USAGE_COLOR, idx - 8, idx, ins.dst[i].write_mask);
700 case WINED3DSPR_TEXCRDOUT:
702 reg_maps->texcoord_mask[idx] |= ins.dst[i].write_mask;
703 if (reg_maps->output_registers & (1 << idx))
705 output_signature[idx].mask |= ins.dst[i].write_mask;
709 reg_maps->output_registers |= 1 << idx;
710 shader_signature_from_usage(&output_signature[idx],
711 WINED3D_DECL_USAGE_TEXCOORD, idx, idx, ins.dst[i].write_mask);
720 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
722 if (ins.dst[i].reg.type == WINED3DSPR_COLOROUT && !ins.dst[i].reg.idx[0].offset)
724 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
725 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
726 * the mov and perform the sRGB write correction from the source register.
728 * However, if the mov is only partial, we can't do this, and if the write
729 * comes from an instruction other than MOV it is hard to do as well. If
730 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
731 shader->u.ps.color0_mov = FALSE;
732 if (ins.handler_idx == WINED3DSIH_MOV
733 && ins.dst[i].write_mask == WINED3DSP_WRITEMASK_ALL)
735 /* Used later when the source register is read. */
739 /* Also drop the MOV marker if the source register is overwritten prior to the shader
742 else if (ins.dst[i].reg.type == WINED3DSPR_TEMP
743 && ins.dst[i].reg.idx[0].offset == shader->u.ps.color0_reg)
745 shader->u.ps.color0_mov = FALSE;
749 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
750 if (shader_version.major == 1
751 && (ins.handler_idx == WINED3DSIH_TEX
752 || ins.handler_idx == WINED3DSIH_TEXBEM
753 || ins.handler_idx == WINED3DSIH_TEXBEML
754 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
755 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
756 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
757 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
758 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
759 || ins.handler_idx == WINED3DSIH_TEXREG2AR
760 || ins.handler_idx == WINED3DSIH_TEXREG2GB
761 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
763 /* Fake sampler usage, only set reserved bit and type. */
764 DWORD sampler_code = ins.dst[i].reg.idx[0].offset;
766 TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
767 reg_maps->sampler_type[sampler_code] = WINED3DSTT_2D;
769 /* texbem is only valid with < 1.4 pixel shaders */
770 if (ins.handler_idx == WINED3DSIH_TEXBEM
771 || ins.handler_idx == WINED3DSIH_TEXBEML)
773 reg_maps->bumpmat |= 1 << ins.dst[i].reg.idx[0].offset;
774 if (ins.handler_idx == WINED3DSIH_TEXBEML)
776 reg_maps->luminanceparams |= 1 << ins.dst[i].reg.idx[0].offset;
780 else if (ins.handler_idx == WINED3DSIH_BEM)
782 reg_maps->bumpmat |= 1 << ins.dst[i].reg.idx[0].offset;
786 if (ins.handler_idx == WINED3DSIH_NRM) reg_maps->usesnrm = 1;
787 else if (ins.handler_idx == WINED3DSIH_DSY) reg_maps->usesdsy = 1;
788 else if (ins.handler_idx == WINED3DSIH_DSX) reg_maps->usesdsx = 1;
789 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
790 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
791 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
792 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
793 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
794 else if (ins.handler_idx == WINED3DSIH_POW) reg_maps->usespow = 1;
795 else if (ins.handler_idx == WINED3DSIH_LOOP
796 || ins.handler_idx == WINED3DSIH_REP)
799 if (cur_loop_depth > max_loop_depth)
800 max_loop_depth = cur_loop_depth;
802 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
803 || ins.handler_idx == WINED3DSIH_ENDREP)
807 shader_record_register_usage(shader, reg_maps, &ins.predicate->reg, shader_version.type);
809 for (i = 0; i < ins.src_count; ++i)
811 unsigned int count = get_instr_extra_regcount(ins.handler_idx, i);
812 struct wined3d_shader_register reg = ins.src[i].reg;
814 shader_record_register_usage(shader, reg_maps, &ins.src[i].reg, shader_version.type);
818 shader_record_register_usage(shader, reg_maps, ®, shader_version.type);
824 if (ins.src[i].reg.type == WINED3DSPR_TEMP
825 && ins.src[i].swizzle == WINED3DSP_NOSWIZZLE)
827 shader->u.ps.color0_mov = TRUE;
828 shader->u.ps.color0_reg = ins.src[i].reg.idx[0].offset;
834 reg_maps->loop_depth = max_loop_depth;
836 /* PS before 2.0 don't have explicit color outputs. Instead the value of
837 * R0 is written to the render target. */
838 if (shader_version.major < 2 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
839 reg_maps->rt_mask |= (1 << 0);
841 shader->functionLength = ((const char *)ptr - (const char *)byte_code);
846 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
848 DWORD map = 1 << max;
850 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
852 return wined3d_log2i(map);
855 static void shader_dump_decl_usage(const struct wined3d_shader_semantic *semantic,
856 const struct wined3d_shader_version *shader_version)
860 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
862 switch (semantic->sampler_type)
864 case WINED3DSTT_2D: TRACE("_2d"); break;
865 case WINED3DSTT_CUBE: TRACE("_cube"); break;
866 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
867 default: TRACE("_unknown_ttype(0x%08x)", semantic->sampler_type);
872 /* Pixel shaders 3.0 don't have usage semantics. */
873 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL) return;
876 switch (semantic->usage)
878 case WINED3D_DECL_USAGE_POSITION:
879 TRACE("position%u", semantic->usage_idx);
882 case WINED3D_DECL_USAGE_BLEND_INDICES:
886 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
890 case WINED3D_DECL_USAGE_NORMAL:
891 TRACE("normal%u", semantic->usage_idx);
894 case WINED3D_DECL_USAGE_PSIZE:
898 case WINED3D_DECL_USAGE_COLOR:
899 if (!semantic->usage_idx) TRACE("color");
900 else TRACE("specular%u", (semantic->usage_idx - 1));
903 case WINED3D_DECL_USAGE_TEXCOORD:
904 TRACE("texture%u", semantic->usage_idx);
907 case WINED3D_DECL_USAGE_TANGENT:
911 case WINED3D_DECL_USAGE_BINORMAL:
915 case WINED3D_DECL_USAGE_TESS_FACTOR:
919 case WINED3D_DECL_USAGE_POSITIONT:
920 TRACE("positionT%u", semantic->usage_idx);
923 case WINED3D_DECL_USAGE_FOG:
927 case WINED3D_DECL_USAGE_DEPTH:
931 case WINED3D_DECL_USAGE_SAMPLE:
936 FIXME("unknown_semantics(0x%08x)", semantic->usage);
941 static void shader_dump_register(const struct wined3d_shader_register *reg,
942 const struct wined3d_shader_version *shader_version)
944 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
945 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
946 UINT offset = reg->idx[0].offset;
950 case WINED3DSPR_TEMP:
954 case WINED3DSPR_INPUT:
958 case WINED3DSPR_CONST:
959 case WINED3DSPR_CONST2:
960 case WINED3DSPR_CONST3:
961 case WINED3DSPR_CONST4:
963 offset = shader_get_float_offset(reg->type, offset);
966 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
967 TRACE("%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
970 case WINED3DSPR_RASTOUT:
971 TRACE("%s", rastout_reg_names[offset]);
974 case WINED3DSPR_COLOROUT:
978 case WINED3DSPR_DEPTHOUT:
982 case WINED3DSPR_ATTROUT:
986 case WINED3DSPR_TEXCRDOUT:
987 /* Vertex shaders >= 3.0 use general purpose output registers
988 * (WINED3DSPR_OUTPUT), which can include an address token. */
989 if (shader_version->major >= 3) TRACE("o");
993 case WINED3DSPR_CONSTINT:
997 case WINED3DSPR_CONSTBOOL:
1001 case WINED3DSPR_LABEL:
1005 case WINED3DSPR_LOOP:
1009 case WINED3DSPR_SAMPLER:
1013 case WINED3DSPR_MISCTYPE:
1015 FIXME("Unhandled misctype register %u.\n", offset);
1017 TRACE("%s", misctype_reg_names[offset]);
1020 case WINED3DSPR_PREDICATE:
1024 case WINED3DSPR_IMMCONST:
1028 case WINED3DSPR_CONSTBUFFER:
1032 case WINED3DSPR_PRIMID:
1036 case WINED3DSPR_NULL:
1040 case WINED3DSPR_RESOURCE:
1045 TRACE("unhandled_rtype(%#x)", reg->type);
1049 if (reg->type == WINED3DSPR_IMMCONST)
1052 switch (reg->immconst_type)
1054 case WINED3D_IMMCONST_SCALAR:
1055 switch (reg->data_type)
1057 case WINED3D_DATA_FLOAT:
1058 TRACE("%.8e", *(const float *)reg->immconst_data);
1060 case WINED3D_DATA_INT:
1061 TRACE("%d", reg->immconst_data[0]);
1063 case WINED3D_DATA_RESOURCE:
1064 case WINED3D_DATA_SAMPLER:
1065 case WINED3D_DATA_UINT:
1066 TRACE("%u", reg->immconst_data[0]);
1069 TRACE("<unhandled data type %#x>", reg->data_type);
1074 case WINED3D_IMMCONST_VEC4:
1075 switch (reg->data_type)
1077 case WINED3D_DATA_FLOAT:
1078 TRACE("%.8e, %.8e, %.8e, %.8e",
1079 *(const float *)®->immconst_data[0], *(const float *)®->immconst_data[1],
1080 *(const float *)®->immconst_data[2], *(const float *)®->immconst_data[3]);
1082 case WINED3D_DATA_INT:
1083 TRACE("%d, %d, %d, %d",
1084 reg->immconst_data[0], reg->immconst_data[1],
1085 reg->immconst_data[2], reg->immconst_data[3]);
1087 case WINED3D_DATA_RESOURCE:
1088 case WINED3D_DATA_SAMPLER:
1089 case WINED3D_DATA_UINT:
1090 TRACE("%u, %u, %u, %u",
1091 reg->immconst_data[0], reg->immconst_data[1],
1092 reg->immconst_data[2], reg->immconst_data[3]);
1095 TRACE("<unhandled data type %#x>", reg->data_type);
1101 TRACE("<unhandled immconst_type %#x>", reg->immconst_type);
1106 else if (reg->type != WINED3DSPR_RASTOUT
1107 && reg->type != WINED3DSPR_MISCTYPE
1108 && reg->type != WINED3DSPR_NULL)
1113 if (reg->idx[0].rel_addr)
1115 shader_dump_src_param(reg->idx[0].rel_addr, shader_version);
1118 TRACE("%u]", offset);
1120 if (reg->idx[1].offset != ~0U)
1123 if (reg->idx[1].rel_addr)
1125 shader_dump_src_param(reg->idx[1].rel_addr, shader_version);
1128 TRACE("%u]", reg->idx[1].offset);
1134 void shader_dump_dst_param(const struct wined3d_shader_dst_param *param,
1135 const struct wined3d_shader_version *shader_version)
1137 DWORD write_mask = param->write_mask;
1139 shader_dump_register(¶m->reg, shader_version);
1141 if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
1143 static const char *write_mask_chars = "xyzw";
1146 if (write_mask & WINED3DSP_WRITEMASK_0) TRACE("%c", write_mask_chars[0]);
1147 if (write_mask & WINED3DSP_WRITEMASK_1) TRACE("%c", write_mask_chars[1]);
1148 if (write_mask & WINED3DSP_WRITEMASK_2) TRACE("%c", write_mask_chars[2]);
1149 if (write_mask & WINED3DSP_WRITEMASK_3) TRACE("%c", write_mask_chars[3]);
1153 void shader_dump_src_param(const struct wined3d_shader_src_param *param,
1154 const struct wined3d_shader_version *shader_version)
1156 enum wined3d_shader_src_modifier src_modifier = param->modifiers;
1157 DWORD swizzle = param->swizzle;
1159 if (src_modifier == WINED3DSPSM_NEG
1160 || src_modifier == WINED3DSPSM_BIASNEG
1161 || src_modifier == WINED3DSPSM_SIGNNEG
1162 || src_modifier == WINED3DSPSM_X2NEG
1163 || src_modifier == WINED3DSPSM_ABSNEG)
1165 else if (src_modifier == WINED3DSPSM_COMP)
1167 else if (src_modifier == WINED3DSPSM_NOT)
1170 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
1173 shader_dump_register(¶m->reg, shader_version);
1177 switch (src_modifier)
1179 case WINED3DSPSM_NONE: break;
1180 case WINED3DSPSM_NEG: break;
1181 case WINED3DSPSM_NOT: break;
1182 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
1183 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
1184 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
1185 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
1186 case WINED3DSPSM_COMP: break;
1187 case WINED3DSPSM_X2: TRACE("_x2"); break;
1188 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
1189 case WINED3DSPSM_DZ: TRACE("_dz"); break;
1190 case WINED3DSPSM_DW: TRACE("_dw"); break;
1191 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
1192 case WINED3DSPSM_ABS: TRACE(")"); break;
1193 default: TRACE("_unknown_modifier(%#x)", src_modifier);
1197 if (swizzle != WINED3DSP_NOSWIZZLE)
1199 static const char *swizzle_chars = "xyzw";
1200 DWORD swizzle_x = swizzle & 0x03;
1201 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1202 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1203 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1205 if (swizzle_x == swizzle_y
1206 && swizzle_x == swizzle_z
1207 && swizzle_x == swizzle_w)
1209 TRACE(".%c", swizzle_chars[swizzle_x]);
1213 TRACE(".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
1214 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
1219 /* Shared code in order to generate the bulk of the shader string.
1220 * NOTE: A description of how to parse tokens can be found on MSDN. */
1221 void shader_generate_main(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
1222 const struct wined3d_shader_reg_maps *reg_maps, const DWORD *byte_code, void *backend_ctx)
1224 struct wined3d_device *device = shader->device;
1225 const struct wined3d_shader_frontend *fe = shader->frontend;
1226 void *fe_data = shader->frontend_data;
1227 struct wined3d_shader_version shader_version;
1228 struct wined3d_shader_loop_state loop_state;
1229 struct wined3d_shader_instruction ins;
1230 struct wined3d_shader_tex_mx tex_mx;
1231 struct wined3d_shader_context ctx;
1232 const DWORD *ptr = byte_code;
1234 /* Initialize current parsing state. */
1235 tex_mx.current_row = 0;
1236 loop_state.current_depth = 0;
1237 loop_state.current_reg = 0;
1239 ctx.shader = shader;
1240 ctx.gl_info = &device->adapter->gl_info;
1241 ctx.reg_maps = reg_maps;
1242 ctx.buffer = buffer;
1243 ctx.tex_mx = &tex_mx;
1244 ctx.loop_state = &loop_state;
1245 ctx.backend_data = backend_ctx;
1248 fe->shader_read_header(fe_data, &ptr, &shader_version);
1250 while (!fe->shader_is_end(fe_data, &ptr))
1253 fe->shader_read_instruction(fe_data, &ptr, &ins);
1255 /* Unknown opcode and its parameters. */
1256 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1258 TRACE("Skipping unrecognized instruction.\n");
1263 FIXME("Predicates not implemented.\n");
1265 /* Call appropriate function for output target */
1266 device->shader_backend->shader_handle_instruction(&ins);
1270 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param *dst)
1272 DWORD mmask = dst->modifiers;
1277 case 13: TRACE("_d8"); break;
1278 case 14: TRACE("_d4"); break;
1279 case 15: TRACE("_d2"); break;
1280 case 1: TRACE("_x2"); break;
1281 case 2: TRACE("_x4"); break;
1282 case 3: TRACE("_x8"); break;
1283 default: TRACE("_unhandled_shift(%d)", dst->shift); break;
1286 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
1287 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
1288 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
1290 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
1291 if (mmask) FIXME("_unrecognized_modifier(%#x)", mmask);
1294 static void shader_dump_primitive_type(enum wined3d_primitive_type primitive_type)
1296 switch (primitive_type)
1298 case WINED3D_PT_UNDEFINED:
1301 case WINED3D_PT_POINTLIST:
1304 case WINED3D_PT_LINELIST:
1307 case WINED3D_PT_LINESTRIP:
1310 case WINED3D_PT_TRIANGLELIST:
1311 TRACE("trianglelist");
1313 case WINED3D_PT_TRIANGLESTRIP:
1314 TRACE("trianglestrip");
1316 case WINED3D_PT_TRIANGLEFAN:
1317 TRACE("trianglefan");
1319 case WINED3D_PT_LINELIST_ADJ:
1320 TRACE("linelist_adj");
1322 case WINED3D_PT_LINESTRIP_ADJ:
1323 TRACE("linestrip_adj");
1325 case WINED3D_PT_TRIANGLELIST_ADJ:
1326 TRACE("trianglelist_adj");
1328 case WINED3D_PT_TRIANGLESTRIP_ADJ:
1329 TRACE("trianglestrip_adj");
1332 TRACE("<unrecognized_primitive_type %#x>", primitive_type);
1337 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *byte_code)
1339 struct wined3d_shader_version shader_version;
1340 const DWORD *ptr = byte_code;
1341 const char *type_prefix;
1344 TRACE("Parsing %p.\n", byte_code);
1346 fe->shader_read_header(fe_data, &ptr, &shader_version);
1348 switch (shader_version.type)
1350 case WINED3D_SHADER_TYPE_VERTEX:
1354 case WINED3D_SHADER_TYPE_GEOMETRY:
1358 case WINED3D_SHADER_TYPE_PIXEL:
1363 FIXME("Unhandled shader type %#x.\n", shader_version.type);
1364 type_prefix = "unknown";
1368 TRACE("%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
1370 while (!fe->shader_is_end(fe_data, &ptr))
1372 struct wined3d_shader_instruction ins;
1374 fe->shader_read_instruction(fe_data, &ptr, &ins);
1375 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1377 TRACE("Skipping unrecognized instruction.\n");
1381 if (ins.handler_idx == WINED3DSIH_DCL)
1383 shader_dump_decl_usage(&ins.declaration.semantic, &shader_version);
1384 shader_dump_ins_modifiers(&ins.declaration.semantic.reg);
1386 shader_dump_dst_param(&ins.declaration.semantic.reg, &shader_version);
1388 else if (ins.handler_idx == WINED3DSIH_DCL_CONSTANT_BUFFER)
1390 TRACE("%s ", shader_opcode_names[ins.handler_idx]);
1391 shader_dump_src_param(&ins.declaration.src, &shader_version);
1392 TRACE(", %s", ins.flags & WINED3DSI_INDEXED_DYNAMIC ? "dynamicIndexed" : "immediateIndexed");
1394 else if (ins.handler_idx == WINED3DSIH_DCL_INPUT_PRIMITIVE
1395 || ins.handler_idx == WINED3DSIH_DCL_OUTPUT_TOPOLOGY)
1397 TRACE("%s ", shader_opcode_names[ins.handler_idx]);
1398 shader_dump_primitive_type(ins.declaration.primitive_type);
1400 else if (ins.handler_idx == WINED3DSIH_DCL_VERTICES_OUT)
1402 TRACE("%s %u", shader_opcode_names[ins.handler_idx], ins.declaration.count);
1404 else if (ins.handler_idx == WINED3DSIH_DEF)
1406 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(ins.dst[0].reg.type,
1407 ins.dst[0].reg.idx[0].offset),
1408 *(const float *)&ins.src[0].reg.immconst_data[0],
1409 *(const float *)&ins.src[0].reg.immconst_data[1],
1410 *(const float *)&ins.src[0].reg.immconst_data[2],
1411 *(const float *)&ins.src[0].reg.immconst_data[3]);
1413 else if (ins.handler_idx == WINED3DSIH_DEFI)
1415 TRACE("defi i%u = %d, %d, %d, %d", ins.dst[0].reg.idx[0].offset,
1416 ins.src[0].reg.immconst_data[0],
1417 ins.src[0].reg.immconst_data[1],
1418 ins.src[0].reg.immconst_data[2],
1419 ins.src[0].reg.immconst_data[3]);
1421 else if (ins.handler_idx == WINED3DSIH_DEFB)
1423 TRACE("defb b%u = %s", ins.dst[0].reg.idx[0].offset, ins.src[0].reg.immconst_data[0] ? "true" : "false");
1430 shader_dump_src_param(ins.predicate, &shader_version);
1434 /* PixWin marks instructions with the coissue flag with a '+' */
1435 if (ins.coissue) TRACE("+");
1437 TRACE("%s", shader_opcode_names[ins.handler_idx]);
1439 if (ins.handler_idx == WINED3DSIH_IFC
1440 || ins.handler_idx == WINED3DSIH_BREAKC)
1444 case WINED3D_SHADER_REL_OP_GT: TRACE("_gt"); break;
1445 case WINED3D_SHADER_REL_OP_EQ: TRACE("_eq"); break;
1446 case WINED3D_SHADER_REL_OP_GE: TRACE("_ge"); break;
1447 case WINED3D_SHADER_REL_OP_LT: TRACE("_lt"); break;
1448 case WINED3D_SHADER_REL_OP_NE: TRACE("_ne"); break;
1449 case WINED3D_SHADER_REL_OP_LE: TRACE("_le"); break;
1450 default: TRACE("_(%u)", ins.flags);
1453 else if (ins.handler_idx == WINED3DSIH_TEX
1454 && shader_version.major >= 2
1455 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
1460 for (i = 0; i < ins.dst_count; ++i)
1462 shader_dump_ins_modifiers(&ins.dst[i]);
1463 TRACE(!i ? " " : ", ");
1464 shader_dump_dst_param(&ins.dst[i], &shader_version);
1467 /* Other source tokens */
1468 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
1470 TRACE(!i ? " " : ", ");
1471 shader_dump_src_param(&ins.src[i - ins.dst_count], &shader_version);
1478 static void shader_cleanup(struct wined3d_shader *shader)
1480 shader->device->shader_backend->shader_destroy(shader);
1481 HeapFree(GetProcessHeap(), 0, shader->reg_maps.constf);
1482 HeapFree(GetProcessHeap(), 0, shader->function);
1483 shader_delete_constant_list(&shader->constantsF);
1484 shader_delete_constant_list(&shader->constantsB);
1485 shader_delete_constant_list(&shader->constantsI);
1486 list_remove(&shader->shader_list_entry);
1488 if (shader->frontend && shader->frontend_data)
1489 shader->frontend->shader_free(shader->frontend_data);
1492 struct shader_none_priv
1494 const struct fragment_pipeline *fragment_pipe;
1495 BOOL ffp_proj_control;
1498 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
1499 static void shader_none_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
1500 enum tex_types tex_type, const SIZE *ds_mask_size) {}
1501 static void shader_none_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info) {}
1502 static void shader_none_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count) {}
1503 static void shader_none_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count) {}
1504 static void shader_none_load_constants(const struct wined3d_context *context, BOOL usePS, BOOL useVS) {}
1505 static void shader_none_load_np2fixup_constants(void *shader_priv,
1506 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state) {}
1507 static void shader_none_destroy(struct wined3d_shader *shader) {}
1508 static void shader_none_context_destroyed(void *shader_priv, const struct wined3d_context *context) {}
1510 static void shader_none_select(const struct wined3d_context *context, enum wined3d_shader_mode vertex_mode,
1511 enum wined3d_shader_mode fragment_mode)
1513 const struct wined3d_gl_info *gl_info = context->gl_info;
1514 struct wined3d_device *device = context->swapchain->device;
1515 struct shader_none_priv *priv = device->shader_priv;
1517 priv->fragment_pipe->enable_extension(gl_info, fragment_mode == WINED3D_SHADER_MODE_FFP);
1520 static HRESULT shader_none_alloc(struct wined3d_device *device, const struct fragment_pipeline *fragment_pipe)
1522 struct fragment_caps fragment_caps;
1523 struct shader_none_priv *priv;
1524 void *fragment_priv;
1526 if (!(priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv))))
1527 return E_OUTOFMEMORY;
1529 if (!(fragment_priv = fragment_pipe->alloc_private(&none_shader_backend, priv)))
1531 ERR("Failed to initialize fragment pipe.\n");
1532 HeapFree(GetProcessHeap(), 0, priv);
1536 fragment_pipe->get_caps(&device->adapter->gl_info, &fragment_caps);
1537 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
1538 device->fragment_priv = fragment_priv;
1539 priv->fragment_pipe = fragment_pipe;
1540 device->shader_priv = priv;
1545 static void shader_none_free(struct wined3d_device *device)
1547 struct shader_none_priv *priv = device->shader_priv;
1549 priv->fragment_pipe->free_private(device);
1550 HeapFree(GetProcessHeap(), 0, priv);
1553 static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
1555 /* Set the shader caps to 0 for the none shader backend */
1556 caps->vs_version = 0;
1557 caps->gs_version = 0;
1558 caps->ps_version = 0;
1559 caps->vs_uniform_count = 0;
1560 caps->ps_uniform_count = 0;
1561 caps->ps_1x_max_value = 0.0f;
1562 caps->wined3d_caps = 0;
1565 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1567 /* We "support" every possible fixup, since we don't support any shader
1568 * model, and will never have to actually sample a texture. */
1572 static BOOL shader_none_has_ffp_proj_control(void *shader_priv)
1574 struct shader_none_priv *priv = shader_priv;
1576 return priv->ffp_proj_control;
1579 const struct wined3d_shader_backend_ops none_shader_backend =
1581 shader_none_handle_instruction,
1583 shader_none_select_depth_blt,
1584 shader_none_deselect_depth_blt,
1585 shader_none_update_float_vertex_constants,
1586 shader_none_update_float_pixel_constants,
1587 shader_none_load_constants,
1588 shader_none_load_np2fixup_constants,
1589 shader_none_destroy,
1592 shader_none_context_destroyed,
1593 shader_none_get_caps,
1594 shader_none_color_fixup_supported,
1595 shader_none_has_ffp_proj_control,
1598 static HRESULT shader_set_function(struct wined3d_shader *shader, const DWORD *byte_code,
1599 const struct wined3d_shader_signature *output_signature, DWORD float_const_count,
1600 enum wined3d_shader_type type, unsigned int max_version)
1602 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1603 const struct wined3d_shader_frontend *fe;
1605 unsigned int backend_version;
1607 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1608 shader, byte_code, output_signature, float_const_count);
1610 fe = shader_select_frontend(*byte_code);
1613 FIXME("Unable to find frontend for shader.\n");
1614 return WINED3DERR_INVALIDCALL;
1616 shader->frontend = fe;
1617 shader->frontend_data = fe->shader_init(byte_code, output_signature);
1618 if (!shader->frontend_data)
1620 FIXME("Failed to initialize frontend.\n");
1621 return WINED3DERR_INVALIDCALL;
1624 /* First pass: trace shader. */
1625 if (TRACE_ON(d3d_shader))
1626 shader_trace_init(fe, shader->frontend_data, byte_code);
1628 /* Initialize immediate constant lists. */
1629 list_init(&shader->constantsF);
1630 list_init(&shader->constantsB);
1631 list_init(&shader->constantsI);
1633 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1634 hr = shader_get_registers_used(shader, fe,
1635 reg_maps, shader->input_signature, shader->output_signature,
1636 byte_code, float_const_count);
1637 if (FAILED(hr)) return hr;
1639 if (reg_maps->shader_version.type != type)
1641 WARN("Wrong shader type %d.\n", reg_maps->shader_version.type);
1642 return WINED3DERR_INVALIDCALL;
1644 if (reg_maps->shader_version.major > max_version)
1646 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps->shader_version.major);
1647 return WINED3DERR_INVALIDCALL;
1651 case WINED3D_SHADER_TYPE_VERTEX:
1652 backend_version = shader->device->vs_version;
1654 case WINED3D_SHADER_TYPE_GEOMETRY:
1655 backend_version = shader->device->gs_version;
1657 case WINED3D_SHADER_TYPE_PIXEL:
1658 backend_version = shader->device->ps_version;
1661 FIXME("No backend version-checking for this shader type\n");
1662 backend_version = 0;
1664 if (reg_maps->shader_version.major > backend_version)
1666 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
1667 reg_maps->shader_version.major, reg_maps->shader_version.minor);
1668 return WINED3DERR_INVALIDCALL;
1671 shader->function = HeapAlloc(GetProcessHeap(), 0, shader->functionLength);
1672 if (!shader->function)
1673 return E_OUTOFMEMORY;
1674 memcpy(shader->function, byte_code, shader->functionLength);
1679 ULONG CDECL wined3d_shader_incref(struct wined3d_shader *shader)
1681 ULONG refcount = InterlockedIncrement(&shader->ref);
1683 TRACE("%p increasing refcount to %u.\n", shader, refcount);
1688 /* Do not call while under the GL lock. */
1689 ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
1691 ULONG refcount = InterlockedDecrement(&shader->ref);
1693 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1697 shader_cleanup(shader);
1698 shader->parent_ops->wined3d_object_destroyed(shader->parent);
1699 HeapFree(GetProcessHeap(), 0, shader);
1705 void * CDECL wined3d_shader_get_parent(const struct wined3d_shader *shader)
1707 TRACE("shader %p.\n", shader);
1709 return shader->parent;
1712 HRESULT CDECL wined3d_shader_get_byte_code(const struct wined3d_shader *shader,
1713 void *byte_code, UINT *byte_code_size)
1715 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader, byte_code, byte_code_size);
1719 *byte_code_size = shader->functionLength;
1723 if (*byte_code_size < shader->functionLength)
1725 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
1726 * than the required size we should write the required size and
1727 * return D3DERR_MOREDATA. That's not actually true. */
1728 return WINED3DERR_INVALIDCALL;
1731 memcpy(byte_code, shader->function, shader->functionLength);
1736 /* Set local constants for d3d8 shaders. */
1737 HRESULT CDECL wined3d_shader_set_local_constants_float(struct wined3d_shader *shader,
1738 UINT start_idx, const float *src_data, UINT count)
1740 UINT end_idx = start_idx + count;
1743 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader, start_idx, src_data, count);
1745 if (end_idx > shader->limits.constant_float)
1747 WARN("end_idx %u > float constants limit %u.\n",
1748 end_idx, shader->limits.constant_float);
1749 end_idx = shader->limits.constant_float;
1752 for (i = start_idx; i < end_idx; ++i)
1754 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1756 return E_OUTOFMEMORY;
1759 memcpy(lconst->value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
1760 list_add_head(&shader->constantsF, &lconst->entry);
1766 void find_vs_compile_args(const struct wined3d_state *state,
1767 const struct wined3d_shader *shader, struct vs_compile_args *args)
1769 args->fog_src = state->render_states[WINED3D_RS_FOGTABLEMODE]
1770 == WINED3D_FOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
1771 args->clip_enabled = state->render_states[WINED3D_RS_CLIPPING]
1772 && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
1773 args->swizzle_map = shader->device->strided_streams.swizzle_map;
1776 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
1778 if (usage_idx1 != usage_idx2)
1780 if (usage1 == usage2)
1782 if (usage1 == WINED3D_DECL_USAGE_POSITION && usage2 == WINED3D_DECL_USAGE_POSITIONT)
1784 if (usage2 == WINED3D_DECL_USAGE_POSITION && usage1 == WINED3D_DECL_USAGE_POSITIONT)
1790 BOOL vshader_get_input(const struct wined3d_shader *shader,
1791 BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
1793 WORD map = shader->reg_maps.input_registers;
1796 for (i = 0; map; map >>= 1, ++i)
1798 if (!(map & 1)) continue;
1800 if (match_usage(shader->u.vs.attributes[i].usage,
1801 shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
1810 static void vertexshader_set_limits(struct wined3d_shader *shader)
1812 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
1813 shader->reg_maps.shader_version.minor);
1814 struct wined3d_device *device = shader->device;
1816 shader->limits.packed_input = 0;
1818 switch (shader_version)
1820 case WINED3D_SHADER_VERSION(1, 0):
1821 case WINED3D_SHADER_VERSION(1, 1):
1822 shader->limits.constant_bool = 0;
1823 shader->limits.constant_int = 0;
1824 shader->limits.packed_output = 12;
1825 shader->limits.sampler = 0;
1826 /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1827 * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1829 shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1832 case WINED3D_SHADER_VERSION(2, 0):
1833 case WINED3D_SHADER_VERSION(2, 1):
1834 shader->limits.constant_bool = 16;
1835 shader->limits.constant_int = 16;
1836 shader->limits.packed_output = 12;
1837 shader->limits.sampler = 0;
1838 shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1841 case WINED3D_SHADER_VERSION(3, 0):
1842 shader->limits.constant_bool = 16;
1843 shader->limits.constant_int = 16;
1844 shader->limits.packed_output = 12;
1845 shader->limits.sampler = 4;
1846 /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1847 * even though they are capable of supporting much more (GL
1848 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1849 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1850 * shaders to 256. */
1851 shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1854 case WINED3D_SHADER_VERSION(4, 0):
1855 shader->limits.sampler = 16; /* FIXME: 128 resources, 16 sampler states */
1856 shader->limits.constant_int = 0;
1857 shader->limits.constant_float = 0;
1858 shader->limits.constant_bool = 0;
1859 shader->limits.packed_output = 16;
1860 shader->limits.packed_input = 0;
1864 shader->limits.constant_bool = 16;
1865 shader->limits.constant_int = 16;
1866 shader->limits.packed_output = 12;
1867 shader->limits.sampler = 0;
1868 shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1869 FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1870 shader->reg_maps.shader_version.major,
1871 shader->reg_maps.shader_version.minor);
1875 static HRESULT vertexshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
1876 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1877 void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
1879 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1884 if (!byte_code) return WINED3DERR_INVALIDCALL;
1886 shader_init(shader, device, parent, parent_ops);
1887 hr = shader_set_function(shader, byte_code, output_signature, device->d3d_vshader_constantF,
1888 WINED3D_SHADER_TYPE_VERTEX, max_version);
1891 WARN("Failed to set function, hr %#x.\n", hr);
1892 shader_cleanup(shader);
1896 map = reg_maps->input_registers;
1897 for (i = 0; map; map >>= 1, ++i)
1899 if (!(map & 1) || !shader->input_signature[i].semantic_name)
1902 shader->u.vs.attributes[i].usage =
1903 shader_usage_from_semantic_name(shader->input_signature[i].semantic_name);
1904 shader->u.vs.attributes[i].usage_idx = shader->input_signature[i].semantic_idx;
1907 if (output_signature)
1909 for (i = 0; i < output_signature->element_count; ++i)
1911 struct wined3d_shader_signature_element *e = &output_signature->elements[i];
1912 reg_maps->output_registers |= 1 << e->register_idx;
1913 shader->output_signature[e->register_idx] = *e;
1917 vertexshader_set_limits(shader);
1919 shader->load_local_constsF = reg_maps->usesrelconstF
1920 && !list_empty(&shader->constantsF);
1925 static void geometryshader_set_limits(struct wined3d_shader *shader)
1927 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
1928 shader->reg_maps.shader_version.minor);
1930 switch (shader_version)
1932 case WINED3D_SHADER_VERSION(4, 0):
1933 shader->limits.sampler = 16; /* FIXME: 128 resources, 16 sampler states */
1934 shader->limits.constant_int = 0;
1935 shader->limits.constant_float = 0;
1936 shader->limits.constant_bool = 0;
1937 shader->limits.packed_output = 32;
1938 shader->limits.packed_input = 16;
1942 memset(&shader->limits, 0, sizeof(shader->limits));
1943 FIXME("Unhandled geometry shader version \"%u.%u\".\n",
1944 shader->reg_maps.shader_version.major,
1945 shader->reg_maps.shader_version.minor);
1949 static HRESULT geometryshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
1950 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1951 void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
1955 shader_init(shader, device, parent, parent_ops);
1956 hr = shader_set_function(shader, byte_code, output_signature, 0,
1957 WINED3D_SHADER_TYPE_GEOMETRY, max_version);
1960 WARN("Failed to set function, hr %#x.\n", hr);
1961 shader_cleanup(shader);
1965 geometryshader_set_limits(shader);
1967 shader->load_local_constsF = FALSE;
1972 void find_ps_compile_args(const struct wined3d_state *state,
1973 const struct wined3d_shader *shader, struct ps_compile_args *args)
1975 struct wined3d_device *device = shader->device;
1976 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1977 const struct wined3d_texture *texture;
1980 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
1981 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && state->render_states[WINED3D_RS_SRGBWRITEENABLE])
1983 const struct wined3d_surface *rt = state->fb->render_targets[0];
1984 if (rt->resource.format->flags & WINED3DFMT_FLAG_SRGB_WRITE)
1986 static unsigned int warned = 0;
1988 args->srgb_correction = 1;
1989 if (state->render_states[WINED3D_RS_ALPHABLENDENABLE] && !warned++)
1990 WARN("Blending into a sRGB render target with no GL_ARB_framebuffer_sRGB "
1991 "support, expect rendering artifacts.\n");
1995 if (shader->reg_maps.shader_version.major == 1
1996 && shader->reg_maps.shader_version.minor <= 3)
1998 for (i = 0; i < 4; ++i)
2000 DWORD flags = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
2002 if (flags & WINED3D_TTFF_PROJECTED)
2004 DWORD tex_transform = flags & ~WINED3D_TTFF_PROJECTED;
2006 if (!state->vertex_shader)
2009 unsigned int index = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
2010 DWORD max_valid = WINED3D_TTFF_COUNT4;
2011 enum wined3d_sampler_texture_type sampler_type = shader->reg_maps.sampler_type[i];
2013 for (j = 0; j < state->vertex_declaration->element_count; ++j)
2015 struct wined3d_vertex_declaration_element *element =
2016 &state->vertex_declaration->elements[j];
2018 if (element->usage == WINED3D_DECL_USAGE_TEXCOORD
2019 && element->usage_idx == index)
2021 max_valid = element->format->component_count;
2025 if (!tex_transform || tex_transform > max_valid)
2027 WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
2028 tex_transform, max_valid);
2029 tex_transform = max_valid;
2031 if ((sampler_type == WINED3DSTT_1D && tex_transform > WINED3D_TTFF_COUNT1)
2032 || (sampler_type == WINED3DSTT_2D && tex_transform > WINED3D_TTFF_COUNT2)
2033 || (sampler_type == WINED3DSTT_VOLUME && tex_transform > WINED3D_TTFF_COUNT3))
2034 tex_transform |= WINED3D_PSARGS_PROJECTED;
2037 WARN("Application requested projected texture with unsuitable texture coordinates.\n");
2038 WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
2039 i, tex_transform, sampler_type);
2043 tex_transform = WINED3D_TTFF_COUNT4 | WINED3D_PSARGS_PROJECTED;
2045 args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
2050 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2052 if (!shader->reg_maps.sampler_type[i])
2055 texture = state->textures[i];
2058 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
2061 args->color_fixup[i] = texture->resource.format->color_fixup;
2063 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
2064 args->shadow |= 1 << i;
2066 /* Flag samplers that need NP2 texcoord fixup. */
2067 if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
2068 args->np2_fixup |= (1 << i);
2070 if (shader->reg_maps.shader_version.major >= 3)
2072 if (device->strided_streams.position_transformed)
2074 args->vp_mode = pretransformed;
2076 else if (use_vs(state))
2078 args->vp_mode = vertexshader;
2082 args->vp_mode = fixedfunction;
2084 args->fog = FOG_OFF;
2088 args->vp_mode = vertexshader;
2089 if (state->render_states[WINED3D_RS_FOGENABLE])
2091 switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
2093 case WINED3D_FOG_NONE:
2094 if (device->strided_streams.position_transformed || use_vs(state))
2096 args->fog = FOG_LINEAR;
2100 switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
2102 case WINED3D_FOG_NONE: /* Fall through. */
2103 case WINED3D_FOG_LINEAR: args->fog = FOG_LINEAR; break;
2104 case WINED3D_FOG_EXP: args->fog = FOG_EXP; break;
2105 case WINED3D_FOG_EXP2: args->fog = FOG_EXP2; break;
2109 case WINED3D_FOG_LINEAR: args->fog = FOG_LINEAR; break;
2110 case WINED3D_FOG_EXP: args->fog = FOG_EXP; break;
2111 case WINED3D_FOG_EXP2: args->fog = FOG_EXP2; break;
2116 args->fog = FOG_OFF;
2121 static void pixelshader_set_limits(struct wined3d_shader *shader)
2123 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
2124 shader->reg_maps.shader_version.minor);
2126 shader->limits.packed_output = 0;
2128 switch (shader_version)
2130 case WINED3D_SHADER_VERSION(1, 0):
2131 case WINED3D_SHADER_VERSION(1, 1):
2132 case WINED3D_SHADER_VERSION(1, 2):
2133 case WINED3D_SHADER_VERSION(1, 3):
2134 shader->limits.constant_float = 8;
2135 shader->limits.constant_int = 0;
2136 shader->limits.constant_bool = 0;
2137 shader->limits.sampler = 4;
2138 shader->limits.packed_input = 0;
2141 case WINED3D_SHADER_VERSION(1, 4):
2142 shader->limits.constant_float = 8;
2143 shader->limits.constant_int = 0;
2144 shader->limits.constant_bool = 0;
2145 shader->limits.sampler = 6;
2146 shader->limits.packed_input = 0;
2149 /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2150 case WINED3D_SHADER_VERSION(2, 0):
2151 shader->limits.constant_float = 32;
2152 shader->limits.constant_int = 16;
2153 shader->limits.constant_bool = 16;
2154 shader->limits.sampler = 16;
2155 shader->limits.packed_input = 0;
2158 case WINED3D_SHADER_VERSION(2, 1):
2159 shader->limits.constant_float = 32;
2160 shader->limits.constant_int = 16;
2161 shader->limits.constant_bool = 16;
2162 shader->limits.sampler = 16;
2163 shader->limits.packed_input = 0;
2166 case WINED3D_SHADER_VERSION(3, 0):
2167 shader->limits.constant_float = 224;
2168 shader->limits.constant_int = 16;
2169 shader->limits.constant_bool = 16;
2170 shader->limits.sampler = 16;
2171 shader->limits.packed_input = 12;
2174 case WINED3D_SHADER_VERSION(4, 0):
2175 shader->limits.sampler = 16; /* FIXME: 128 resources, 16 sampler states */
2176 shader->limits.constant_int = 0;
2177 shader->limits.constant_float = 0;
2178 shader->limits.constant_bool = 0;
2179 shader->limits.packed_input = 32;
2183 shader->limits.constant_float = 32;
2184 shader->limits.constant_int = 16;
2185 shader->limits.constant_bool = 16;
2186 shader->limits.sampler = 16;
2187 shader->limits.packed_input = 0;
2188 FIXME("Unrecognized pixel shader version %u.%u\n",
2189 shader->reg_maps.shader_version.major,
2190 shader->reg_maps.shader_version.minor);
2194 static HRESULT pixelshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2195 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
2196 void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
2198 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2199 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
2202 if (!byte_code) return WINED3DERR_INVALIDCALL;
2204 shader_init(shader, device, parent, parent_ops);
2205 hr = shader_set_function(shader, byte_code, output_signature, device->d3d_pshader_constantF,
2206 WINED3D_SHADER_TYPE_PIXEL, max_version);
2209 WARN("Failed to set function, hr %#x.\n", hr);
2210 shader_cleanup(shader);
2214 pixelshader_set_limits(shader);
2216 for (i = 0; i < MAX_REG_INPUT; ++i)
2218 if (shader->u.ps.input_reg_used[i])
2221 highest_reg_used = i;
2225 /* Don't do any register mapping magic if it is not needed, or if we can't
2226 * achieve anything anyway */
2227 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
2228 || num_regs_used > (gl_info->limits.glsl_varyings / 4))
2230 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
2232 /* This happens with relative addressing. The input mapper function
2233 * warns about this if the higher registers are declared too, so
2234 * don't write a FIXME here */
2235 WARN("More varying registers used than supported\n");
2238 for (i = 0; i < MAX_REG_INPUT; ++i)
2240 shader->u.ps.input_reg_map[i] = i;
2243 shader->u.ps.declared_in_count = highest_reg_used + 1;
2247 shader->u.ps.declared_in_count = 0;
2248 for (i = 0; i < MAX_REG_INPUT; ++i)
2250 if (shader->u.ps.input_reg_used[i])
2251 shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
2252 else shader->u.ps.input_reg_map[i] = ~0U;
2256 shader->load_local_constsF = FALSE;
2261 void pixelshader_update_samplers(struct wined3d_shader_reg_maps *reg_maps, struct wined3d_texture * const *textures)
2263 enum wined3d_sampler_texture_type *sampler_type = reg_maps->sampler_type;
2266 if (reg_maps->shader_version.major != 1) return;
2268 for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
2270 /* We don't sample from this sampler. */
2271 if (!sampler_type[i]) continue;
2275 WARN("No texture bound to sampler %u, using 2D.\n", i);
2276 sampler_type[i] = WINED3DSTT_2D;
2280 switch (textures[i]->target)
2282 case GL_TEXTURE_RECTANGLE_ARB:
2284 /* We have to select between texture rectangles and 2D
2285 * textures later because 2.0 and 3.0 shaders only have
2286 * WINED3DSTT_2D as well. */
2287 sampler_type[i] = WINED3DSTT_2D;
2291 sampler_type[i] = WINED3DSTT_VOLUME;
2294 case GL_TEXTURE_CUBE_MAP_ARB:
2295 sampler_type[i] = WINED3DSTT_CUBE;
2299 FIXME("Unrecognized texture type %#x, using 2D.\n", textures[i]->target);
2300 sampler_type[i] = WINED3DSTT_2D;
2305 HRESULT CDECL wined3d_shader_create_gs(struct wined3d_device *device, const DWORD *byte_code,
2306 const struct wined3d_shader_signature *output_signature, void *parent,
2307 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2309 struct wined3d_shader *object;
2312 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2313 device, byte_code, output_signature, parent, parent_ops, shader);
2315 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2317 return E_OUTOFMEMORY;
2319 hr = geometryshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2322 WARN("Failed to initialize geometry shader, hr %#x.\n", hr);
2323 HeapFree(GetProcessHeap(), 0, object);
2327 TRACE("Created geometry shader %p.\n", object);
2333 HRESULT CDECL wined3d_shader_create_ps(struct wined3d_device *device, const DWORD *byte_code,
2334 const struct wined3d_shader_signature *output_signature, void *parent,
2335 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2337 struct wined3d_shader *object;
2340 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2341 device, byte_code, output_signature, parent, parent_ops, shader);
2343 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2345 return E_OUTOFMEMORY;
2347 hr = pixelshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2350 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2351 HeapFree(GetProcessHeap(), 0, object);
2355 TRACE("Created pixel shader %p.\n", object);
2361 HRESULT CDECL wined3d_shader_create_vs(struct wined3d_device *device, const DWORD *byte_code,
2362 const struct wined3d_shader_signature *output_signature, void *parent,
2363 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2365 struct wined3d_shader *object;
2368 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2369 device, byte_code, output_signature, parent, parent_ops, shader);
2371 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2373 return E_OUTOFMEMORY;
2375 hr = vertexshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2378 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2379 HeapFree(GetProcessHeap(), 0, object);
2383 TRACE("Created vertex shader %p.\n", object);