2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2005 Oliver Stieber
6 * Copyright 2006 Ivan Gyurdiev
7 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
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_EXP */ "exp",
68 /* WINED3DSIH_EXPP */ "expp",
69 /* WINED3DSIH_FRC */ "frc",
70 /* WINED3DSIH_FTOI */ "ftoi",
71 /* WINED3DSIH_IADD */ "iadd",
72 /* WINED3DSIH_IEQ */ "ieq",
73 /* WINED3DSIH_IF */ "if",
74 /* WINED3DSIH_IFC */ "ifc",
75 /* WINED3DSIH_IGE */ "ige",
76 /* WINED3DSIH_IMUL */ "imul",
77 /* WINED3DSIH_ITOF */ "itof",
78 /* WINED3DSIH_LABEL */ "label",
79 /* WINED3DSIH_LD */ "ld",
80 /* WINED3DSIH_LIT */ "lit",
81 /* WINED3DSIH_LOG */ "log",
82 /* WINED3DSIH_LOGP */ "logp",
83 /* WINED3DSIH_LOOP */ "loop",
84 /* WINED3DSIH_LRP */ "lrp",
85 /* WINED3DSIH_LT */ "lt",
86 /* WINED3DSIH_M3x2 */ "m3x2",
87 /* WINED3DSIH_M3x3 */ "m3x3",
88 /* WINED3DSIH_M3x4 */ "m3x4",
89 /* WINED3DSIH_M4x3 */ "m4x3",
90 /* WINED3DSIH_M4x4 */ "m4x4",
91 /* WINED3DSIH_MAD */ "mad",
92 /* WINED3DSIH_MAX */ "max",
93 /* WINED3DSIH_MIN */ "min",
94 /* WINED3DSIH_MOV */ "mov",
95 /* WINED3DSIH_MOVA */ "mova",
96 /* WINED3DSIH_MOVC */ "movc",
97 /* WINED3DSIH_MUL */ "mul",
98 /* WINED3DSIH_NOP */ "nop",
99 /* WINED3DSIH_NRM */ "nrm",
100 /* WINED3DSIH_PHASE */ "phase",
101 /* WINED3DSIH_POW */ "pow",
102 /* WINED3DSIH_RCP */ "rcp",
103 /* WINED3DSIH_REP */ "rep",
104 /* WINED3DSIH_RET */ "ret",
105 /* WINED3DSIH_RSQ */ "rsq",
106 /* WINED3DSIH_SAMPLE */ "sample",
107 /* WINED3DSIH_SAMPLE_GRAD */ "sample_d",
108 /* WINED3DSIH_SAMPLE_LOD */ "sample_l",
109 /* WINED3DSIH_SETP */ "setp",
110 /* WINED3DSIH_SGE */ "sge",
111 /* WINED3DSIH_SGN */ "sgn",
112 /* WINED3DSIH_SINCOS */ "sincos",
113 /* WINED3DSIH_SLT */ "slt",
114 /* WINED3DSIH_SQRT */ "sqrt",
115 /* WINED3DSIH_SUB */ "sub",
116 /* WINED3DSIH_TEX */ "texld",
117 /* WINED3DSIH_TEXBEM */ "texbem",
118 /* WINED3DSIH_TEXBEML */ "texbeml",
119 /* WINED3DSIH_TEXCOORD */ "texcrd",
120 /* WINED3DSIH_TEXDEPTH */ "texdepth",
121 /* WINED3DSIH_TEXDP3 */ "texdp3",
122 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
123 /* WINED3DSIH_TEXKILL */ "texkill",
124 /* WINED3DSIH_TEXLDD */ "texldd",
125 /* WINED3DSIH_TEXLDL */ "texldl",
126 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
127 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
128 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
129 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
130 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
131 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
132 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
133 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
134 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
135 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
136 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
137 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
138 /* WINED3DSIH_UTOF */ "utof",
141 static const char * const semantic_names[] =
143 /* WINED3DDECLUSAGE_POSITION */ "SV_POSITION",
144 /* WINED3DDECLUSAGE_BLENDWEIGHT */ "BLENDWEIGHT",
145 /* WINED3DDECLUSAGE_BLENDINDICES */ "BLENDINDICES",
146 /* WINED3DDECLUSAGE_NORMAL */ "NORMAL",
147 /* WINED3DDECLUSAGE_PSIZE */ "PSIZE",
148 /* WINED3DDECLUSAGE_TEXCOORD */ "TEXCOORD",
149 /* WINED3DDECLUSAGE_TANGENT */ "TANGENT",
150 /* WINED3DDECLUSAGE_BINORMAL */ "BINORMAL",
151 /* WINED3DDECLUSAGE_TESSFACTOR */ "TESSFACTOR",
152 /* WINED3DDECLUSAGE_POSITIONT */ "POSITIONT",
153 /* WINED3DDECLUSAGE_COLOR */ "COLOR",
154 /* WINED3DDECLUSAGE_FOG */ "FOG",
155 /* WINED3DDECLUSAGE_DEPTH */ "DEPTH",
156 /* WINED3DDECLUSAGE_SAMPLE */ "SAMPLE",
159 static const char *shader_semantic_name_from_usage(WINED3DDECLUSAGE usage)
161 if (usage >= sizeof(semantic_names) / sizeof(*semantic_names))
163 FIXME("Unrecognized usage %#x.\n", usage);
164 return "UNRECOGNIZED";
167 return semantic_names[usage];
170 static WINED3DDECLUSAGE shader_usage_from_semantic_name(const char *name)
174 for (i = 0; i < sizeof(semantic_names) / sizeof(*semantic_names); ++i)
176 if (!strcmp(name, semantic_names[i])) return i;
182 BOOL shader_match_semantic(const char *semantic_name, WINED3DDECLUSAGE usage)
184 return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
187 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
188 const struct wined3d_shader_semantic *s)
190 e->semantic_name = shader_semantic_name_from_usage(s->usage);
191 e->semantic_idx = s->usage_idx;
192 e->sysval_semantic = 0;
193 e->component_type = 0;
194 e->register_idx = s->reg.reg.idx;
195 e->mask = s->reg.write_mask;
198 static void shader_signature_from_usage(struct wined3d_shader_signature_element *e,
199 WINED3DDECLUSAGE usage, UINT usage_idx, UINT reg_idx, DWORD write_mask)
201 e->semantic_name = shader_semantic_name_from_usage(usage);
202 e->semantic_idx = usage_idx;
203 e->sysval_semantic = 0;
204 e->component_type = 0;
205 e->register_idx = reg_idx;
206 e->mask = write_mask;
209 static const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token)
211 switch (version_token >> 16)
215 return &sm1_shader_frontend;
220 return &sm4_shader_frontend;
223 FIXME("Unrecognised version token %#x\n", version_token);
228 void shader_buffer_clear(struct wined3d_shader_buffer *buffer)
230 buffer->buffer[0] = '\0';
233 buffer->newline = TRUE;
236 BOOL shader_buffer_init(struct wined3d_shader_buffer *buffer)
238 buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
241 ERR("Failed to allocate shader buffer memory.\n");
245 shader_buffer_clear(buffer);
249 void shader_buffer_free(struct wined3d_shader_buffer *buffer)
251 HeapFree(GetProcessHeap(), 0, buffer->buffer);
254 int shader_vaddline(struct wined3d_shader_buffer *buffer, const char *format, va_list args)
256 char *base = buffer->buffer + buffer->bsize;
259 rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
261 if (rc < 0 /* C89 */ || (unsigned int)rc > SHADER_PGMSIZE - 1 - buffer->bsize /* C99 */)
263 ERR("The buffer allocated for the shader program string "
264 "is too small at %d bytes.\n", SHADER_PGMSIZE);
265 buffer->bsize = SHADER_PGMSIZE - 1;
271 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
272 buffer->newline = FALSE;
280 if (buffer->buffer[buffer->bsize-1] == '\n')
283 buffer->newline = TRUE;
289 int shader_addline(struct wined3d_shader_buffer *buffer, const char *format, ...)
294 va_start(args, format);
295 ret = shader_vaddline(buffer, format, args);
301 static void shader_init(struct IWineD3DBaseShaderClass *shader, IWineD3DDeviceImpl *device,
302 void *parent, const struct wined3d_parent_ops *parent_ops)
305 shader->device = device;
306 shader->parent = parent;
307 shader->parent_ops = parent_ops;
308 list_init(&shader->linked_programs);
309 list_add_head(&device->shaders, &shader->shader_list_entry);
312 /* Convert floating point offset relative to a register file to an absolute
313 * offset for float constants. */
314 static unsigned int shader_get_float_offset(WINED3DSHADER_PARAM_REGISTER_TYPE register_type, UINT register_idx)
316 switch (register_type)
318 case WINED3DSPR_CONST: return register_idx;
319 case WINED3DSPR_CONST2: return 2048 + register_idx;
320 case WINED3DSPR_CONST3: return 4096 + register_idx;
321 case WINED3DSPR_CONST4: return 6144 + register_idx;
323 FIXME("Unsupported register type: %u.\n", register_type);
328 static void shader_delete_constant_list(struct list *clist)
330 struct local_constant *constant;
333 ptr = list_head(clist);
336 constant = LIST_ENTRY(ptr, struct local_constant, entry);
337 ptr = list_next(clist, ptr);
338 HeapFree(GetProcessHeap(), 0, constant);
343 static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
348 bitmap[idx] |= (1 << shift);
351 static void shader_record_register_usage(IWineD3DBaseShaderImpl *shader, struct wined3d_shader_reg_maps *reg_maps,
352 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type)
356 case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
357 if (shader_type == WINED3D_SHADER_TYPE_PIXEL) reg_maps->texcoord |= 1 << reg->idx;
358 else reg_maps->address |= 1 << reg->idx;
361 case WINED3DSPR_TEMP:
362 reg_maps->temporary |= 1 << reg->idx;
365 case WINED3DSPR_INPUT:
366 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
370 /* If relative addressing is used, we must assume that all registers
371 * are used. Even if it is a construct like v3[aL], we can't assume
372 * that v0, v1 and v2 aren't read because aL can be negative */
374 for (i = 0; i < MAX_REG_INPUT; ++i)
376 shader->u.ps.input_reg_used[i] = TRUE;
381 shader->u.ps.input_reg_used[reg->idx] = TRUE;
384 else reg_maps->input_registers |= 1 << reg->idx;
387 case WINED3DSPR_RASTOUT:
388 if (reg->idx == 1) reg_maps->fog = 1;
391 case WINED3DSPR_MISCTYPE:
392 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
394 if (!reg->idx) reg_maps->vpos = 1;
395 else if (reg->idx == 1) reg_maps->usesfacing = 1;
399 case WINED3DSPR_CONST:
402 if (reg->idx < reg_maps->min_rel_offset) reg_maps->min_rel_offset = reg->idx;
403 if (reg->idx > reg_maps->max_rel_offset) reg_maps->max_rel_offset = reg->idx;
404 reg_maps->usesrelconstF = TRUE;
408 set_bitmap_bit(reg_maps->constf, reg->idx);
412 case WINED3DSPR_CONSTINT:
413 reg_maps->integer_constants |= (1 << reg->idx);
416 case WINED3DSPR_CONSTBOOL:
417 reg_maps->boolean_constants |= (1 << reg->idx);
420 case WINED3DSPR_COLOROUT:
421 reg_maps->highest_render_target = max(reg_maps->highest_render_target, reg->idx);
425 TRACE("Not recording register of type %#x and idx %u\n", reg->type, reg->idx);
430 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
434 case WINED3DSIH_M4x4:
435 case WINED3DSIH_M3x4:
436 return param == 1 ? 3 : 0;
438 case WINED3DSIH_M4x3:
439 case WINED3DSIH_M3x3:
440 return param == 1 ? 2 : 0;
442 case WINED3DSIH_M3x2:
443 return param == 1 ? 1 : 0;
450 /* Note that this does not count the loop register as an address register. */
451 static HRESULT shader_get_registers_used(IWineD3DBaseShaderImpl *shader, const struct wined3d_shader_frontend *fe,
452 struct wined3d_shader_reg_maps *reg_maps, struct wined3d_shader_signature_element *input_signature,
453 struct wined3d_shader_signature_element *output_signature, const DWORD *byte_code, DWORD constf_size)
455 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
456 void *fe_data = shader->baseShader.frontend_data;
457 struct wined3d_shader_version shader_version;
458 const DWORD *ptr = byte_code;
460 memset(reg_maps, 0, sizeof(*reg_maps));
461 reg_maps->min_rel_offset = ~0U;
463 fe->shader_read_header(fe_data, &ptr, &shader_version);
464 reg_maps->shader_version = shader_version;
466 reg_maps->constf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
467 sizeof(*reg_maps->constf) * ((constf_size + 31) / 32));
468 if (!reg_maps->constf)
470 ERR("Failed to allocate constant map memory.\n");
471 return E_OUTOFMEMORY;
474 while (!fe->shader_is_end(fe_data, &ptr))
476 struct wined3d_shader_instruction ins;
482 fe->shader_read_comment(&ptr, &comment, &comment_size);
483 if (comment) continue;
486 fe->shader_read_opcode(fe_data, &ptr, &ins, ¶m_size);
488 /* Unhandled opcode, and its parameters. */
489 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
491 TRACE("Skipping unrecognized instruction.\n");
496 /* Handle declarations. */
497 if (ins.handler_idx == WINED3DSIH_DCL)
499 struct wined3d_shader_semantic semantic;
501 fe->shader_read_semantic(&ptr, &semantic);
503 switch (semantic.reg.reg.type)
505 /* Mark input registers used. */
506 case WINED3DSPR_INPUT:
507 reg_maps->input_registers |= 1 << semantic.reg.reg.idx;
508 shader_signature_from_semantic(&input_signature[semantic.reg.reg.idx], &semantic);
511 /* Vertex shader: mark 3.0 output registers used, save token. */
512 case WINED3DSPR_OUTPUT:
513 reg_maps->output_registers |= 1 << semantic.reg.reg.idx;
514 shader_signature_from_semantic(&output_signature[semantic.reg.reg.idx], &semantic);
515 if (semantic.usage == WINED3DDECLUSAGE_FOG) reg_maps->fog = 1;
518 /* Save sampler usage token. */
519 case WINED3DSPR_SAMPLER:
520 reg_maps->sampler_type[semantic.reg.reg.idx] = semantic.sampler_type;
524 TRACE("Not recording DCL register type %#x.\n", semantic.reg.reg.type);
528 else if (ins.handler_idx == WINED3DSIH_DEF)
530 struct wined3d_shader_src_param rel_addr;
531 struct wined3d_shader_dst_param dst;
533 local_constant *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
534 if (!lconst) return E_OUTOFMEMORY;
536 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
537 lconst->idx = dst.reg.idx;
539 memcpy(lconst->value, ptr, 4 * sizeof(DWORD));
542 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
543 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
545 float *value = (float *)lconst->value;
546 if (value[0] < -1.0f) value[0] = -1.0f;
547 else if (value[0] > 1.0f) value[0] = 1.0f;
548 if (value[1] < -1.0f) value[1] = -1.0f;
549 else if (value[1] > 1.0f) value[1] = 1.0f;
550 if (value[2] < -1.0f) value[2] = -1.0f;
551 else if (value[2] > 1.0f) value[2] = 1.0f;
552 if (value[3] < -1.0f) value[3] = -1.0f;
553 else if (value[3] > 1.0f) value[3] = 1.0f;
556 list_add_head(&shader->baseShader.constantsF, &lconst->entry);
558 else if (ins.handler_idx == WINED3DSIH_DEFI)
560 struct wined3d_shader_src_param rel_addr;
561 struct wined3d_shader_dst_param dst;
563 local_constant *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
564 if (!lconst) return E_OUTOFMEMORY;
566 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
567 lconst->idx = dst.reg.idx;
569 memcpy(lconst->value, ptr, 4 * sizeof(DWORD));
572 list_add_head(&shader->baseShader.constantsI, &lconst->entry);
573 reg_maps->local_int_consts |= (1 << dst.reg.idx);
575 else if (ins.handler_idx == WINED3DSIH_DEFB)
577 struct wined3d_shader_src_param rel_addr;
578 struct wined3d_shader_dst_param dst;
580 local_constant *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
581 if (!lconst) return E_OUTOFMEMORY;
583 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
584 lconst->idx = dst.reg.idx;
586 memcpy(lconst->value, ptr, sizeof(DWORD));
589 list_add_head(&shader->baseShader.constantsB, &lconst->entry);
590 reg_maps->local_bool_consts |= (1 << dst.reg.idx);
592 /* If there's a loop in the shader. */
593 else if (ins.handler_idx == WINED3DSIH_LOOP
594 || ins.handler_idx == WINED3DSIH_REP)
596 struct wined3d_shader_src_param src, rel_addr;
598 fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
600 /* Rep and Loop always use an integer constant for the control parameters. */
601 if (ins.handler_idx == WINED3DSIH_REP)
603 reg_maps->integer_constants |= 1 << src.reg.idx;
607 fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
608 reg_maps->integer_constants |= 1 << src.reg.idx;
612 if (cur_loop_depth > max_loop_depth) max_loop_depth = cur_loop_depth;
614 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
615 || ins.handler_idx == WINED3DSIH_ENDREP)
619 /* For subroutine prototypes. */
620 else if (ins.handler_idx == WINED3DSIH_LABEL)
622 struct wined3d_shader_src_param src, rel_addr;
624 fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
625 reg_maps->labels |= 1 << src.reg.idx;
627 /* Set texture, address, temporary registers. */
630 BOOL color0_mov = FALSE;
633 /* This will loop over all the registers and try to
634 * make a bitmask of the ones we're interested in.
636 * Relative addressing tokens are ignored, but that's
637 * okay, since we'll catch any address registers when
638 * they are initialized (required by spec). */
639 for (i = 0; i < ins.dst_count; ++i)
641 struct wined3d_shader_src_param dst_rel_addr;
642 struct wined3d_shader_dst_param dst_param;
644 fe->shader_read_dst_param(fe_data, &ptr, &dst_param, &dst_rel_addr);
646 shader_record_register_usage(shader, reg_maps, &dst_param.reg, shader_version.type);
648 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
649 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
650 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
651 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX && shader_version.major < 3)
653 UINT idx = dst_param.reg.idx;
655 switch (dst_param.reg.type)
657 case WINED3DSPR_RASTOUT:
661 reg_maps->output_registers |= 1 << 10;
662 shader_signature_from_usage(&output_signature[10],
663 WINED3DDECLUSAGE_POSITION, 0, 10, WINED3DSP_WRITEMASK_ALL);
667 reg_maps->output_registers |= 1 << 11;
668 shader_signature_from_usage(&output_signature[11],
669 WINED3DDECLUSAGE_FOG, 0, 11, WINED3DSP_WRITEMASK_0);
673 reg_maps->output_registers |= 1 << 11;
674 shader_signature_from_usage(&output_signature[11],
675 WINED3DDECLUSAGE_PSIZE, 0, 11, WINED3DSP_WRITEMASK_1);
680 case WINED3DSPR_ATTROUT:
684 if (reg_maps->output_registers & (1 << idx))
686 output_signature[idx].mask |= dst_param.write_mask;
690 reg_maps->output_registers |= 1 << idx;
691 shader_signature_from_usage(&output_signature[idx],
692 WINED3DDECLUSAGE_COLOR, idx - 8, idx, dst_param.write_mask);
697 case WINED3DSPR_TEXCRDOUT:
699 reg_maps->texcoord_mask[idx] |= dst_param.write_mask;
700 if (reg_maps->output_registers & (1 << idx))
702 output_signature[idx].mask |= dst_param.write_mask;
706 reg_maps->output_registers |= 1 << idx;
707 shader_signature_from_usage(&output_signature[idx],
708 WINED3DDECLUSAGE_TEXCOORD, idx, idx, dst_param.write_mask);
717 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
719 if (dst_param.reg.type == WINED3DSPR_COLOROUT && !dst_param.reg.idx)
721 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
722 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
723 * the mov and perform the sRGB write correction from the source register.
725 * However, if the mov is only partial, we can't do this, and if the write
726 * comes from an instruction other than MOV it is hard to do as well. If
727 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
728 shader->u.ps.color0_mov = FALSE;
729 if (ins.handler_idx == WINED3DSIH_MOV
730 && dst_param.write_mask == WINED3DSP_WRITEMASK_ALL)
732 /* Used later when the source register is read. */
736 /* Also drop the MOV marker if the source register is overwritten prior to the shader
739 else if (dst_param.reg.type == WINED3DSPR_TEMP
740 && dst_param.reg.idx == shader->u.ps.color0_reg)
742 shader->u.ps.color0_mov = FALSE;
746 /* Declare 1.x samplers implicitly, based on the destination reg. number. */
747 if (shader_version.major == 1
748 && (ins.handler_idx == WINED3DSIH_TEX
749 || ins.handler_idx == WINED3DSIH_TEXBEM
750 || ins.handler_idx == WINED3DSIH_TEXBEML
751 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
752 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
753 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
754 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
755 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
756 || ins.handler_idx == WINED3DSIH_TEXREG2AR
757 || ins.handler_idx == WINED3DSIH_TEXREG2GB
758 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
760 /* Fake sampler usage, only set reserved bit and type. */
761 DWORD sampler_code = dst_param.reg.idx;
763 TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
764 reg_maps->sampler_type[sampler_code] = WINED3DSTT_2D;
766 /* texbem is only valid with < 1.4 pixel shaders */
767 if (ins.handler_idx == WINED3DSIH_TEXBEM
768 || ins.handler_idx == WINED3DSIH_TEXBEML)
770 reg_maps->bumpmat |= 1 << dst_param.reg.idx;
771 if (ins.handler_idx == WINED3DSIH_TEXBEML)
773 reg_maps->luminanceparams |= 1 << dst_param.reg.idx;
777 else if (ins.handler_idx == WINED3DSIH_BEM)
779 reg_maps->bumpmat |= 1 << dst_param.reg.idx;
783 if (ins.handler_idx == WINED3DSIH_NRM) reg_maps->usesnrm = 1;
784 else if (ins.handler_idx == WINED3DSIH_DSY) reg_maps->usesdsy = 1;
785 else if (ins.handler_idx == WINED3DSIH_DSX) reg_maps->usesdsx = 1;
786 else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
787 else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
788 else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
789 else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
790 else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
791 else if (ins.handler_idx == WINED3DSIH_RCP) reg_maps->usesrcp = 1;
793 limit = ins.src_count + (ins.predicate ? 1 : 0);
794 for (i = 0; i < limit; ++i)
796 struct wined3d_shader_src_param src_param, src_rel_addr;
799 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
800 count = get_instr_extra_regcount(ins.handler_idx, i);
802 shader_record_register_usage(shader, reg_maps, &src_param.reg, shader_version.type);
806 shader_record_register_usage(shader, reg_maps, &src_param.reg, shader_version.type);
812 if (src_param.reg.type == WINED3DSPR_TEMP
813 && src_param.swizzle == WINED3DSP_NOSWIZZLE)
815 shader->u.ps.color0_mov = TRUE;
816 shader->u.ps.color0_reg = src_param.reg.idx;
822 reg_maps->loop_depth = max_loop_depth;
824 shader->baseShader.functionLength = ((const char *)ptr - (const char *)byte_code);
829 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
831 DWORD map = 1 << max;
833 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
835 return wined3d_log2i(map);
838 static void shader_dump_decl_usage(const struct wined3d_shader_semantic *semantic,
839 const struct wined3d_shader_version *shader_version)
843 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
845 switch (semantic->sampler_type)
847 case WINED3DSTT_2D: TRACE("_2d"); break;
848 case WINED3DSTT_CUBE: TRACE("_cube"); break;
849 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
850 default: TRACE("_unknown_ttype(0x%08x)", semantic->sampler_type);
855 /* Pixel shaders 3.0 don't have usage semantics. */
856 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL) return;
859 switch (semantic->usage)
861 case WINED3DDECLUSAGE_POSITION:
862 TRACE("position%u", semantic->usage_idx);
865 case WINED3DDECLUSAGE_BLENDINDICES:
869 case WINED3DDECLUSAGE_BLENDWEIGHT:
873 case WINED3DDECLUSAGE_NORMAL:
874 TRACE("normal%u", semantic->usage_idx);
877 case WINED3DDECLUSAGE_PSIZE:
881 case WINED3DDECLUSAGE_COLOR:
882 if (!semantic->usage_idx) TRACE("color");
883 else TRACE("specular%u", (semantic->usage_idx - 1));
886 case WINED3DDECLUSAGE_TEXCOORD:
887 TRACE("texture%u", semantic->usage_idx);
890 case WINED3DDECLUSAGE_TANGENT:
894 case WINED3DDECLUSAGE_BINORMAL:
898 case WINED3DDECLUSAGE_TESSFACTOR:
902 case WINED3DDECLUSAGE_POSITIONT:
903 TRACE("positionT%u", semantic->usage_idx);
906 case WINED3DDECLUSAGE_FOG:
910 case WINED3DDECLUSAGE_DEPTH:
914 case WINED3DDECLUSAGE_SAMPLE:
919 FIXME("unknown_semantics(0x%08x)", semantic->usage);
924 static void shader_dump_register(const struct wined3d_shader_register *reg,
925 const struct wined3d_shader_version *shader_version)
927 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
928 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
929 UINT offset = reg->idx;
933 case WINED3DSPR_TEMP:
937 case WINED3DSPR_INPUT:
941 case WINED3DSPR_CONST:
942 case WINED3DSPR_CONST2:
943 case WINED3DSPR_CONST3:
944 case WINED3DSPR_CONST4:
946 offset = shader_get_float_offset(reg->type, reg->idx);
949 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
950 TRACE("%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
953 case WINED3DSPR_RASTOUT:
954 TRACE("%s", rastout_reg_names[reg->idx]);
957 case WINED3DSPR_COLOROUT:
961 case WINED3DSPR_DEPTHOUT:
965 case WINED3DSPR_ATTROUT:
969 case WINED3DSPR_TEXCRDOUT:
970 /* Vertex shaders >= 3.0 use general purpose output registers
971 * (WINED3DSPR_OUTPUT), which can include an address token. */
972 if (shader_version->major >= 3) TRACE("o");
976 case WINED3DSPR_CONSTINT:
980 case WINED3DSPR_CONSTBOOL:
984 case WINED3DSPR_LABEL:
988 case WINED3DSPR_LOOP:
992 case WINED3DSPR_SAMPLER:
996 case WINED3DSPR_MISCTYPE:
997 if (reg->idx > 1) FIXME("Unhandled misctype register %u.\n", reg->idx);
998 else TRACE("%s", misctype_reg_names[reg->idx]);
1001 case WINED3DSPR_PREDICATE:
1005 case WINED3DSPR_IMMCONST:
1009 case WINED3DSPR_CONSTBUFFER:
1013 case WINED3DSPR_NULL:
1017 case WINED3DSPR_RESOURCE:
1022 TRACE("unhandled_rtype(%#x)", reg->type);
1026 if (reg->type == WINED3DSPR_IMMCONST)
1029 switch (reg->immconst_type)
1031 case WINED3D_IMMCONST_SCALAR:
1032 TRACE("%.8e", *(const float *)reg->immconst_data);
1035 case WINED3D_IMMCONST_VEC4:
1036 TRACE("%.8e, %.8e, %.8e, %.8e",
1037 *(const float *)®->immconst_data[0], *(const float *)®->immconst_data[1],
1038 *(const float *)®->immconst_data[2], *(const float *)®->immconst_data[3]);
1042 TRACE("<unhandled immconst_type %#x>", reg->immconst_type);
1047 else if (reg->type != WINED3DSPR_RASTOUT
1048 && reg->type != WINED3DSPR_MISCTYPE
1049 && reg->type != WINED3DSPR_NULL)
1051 if (reg->array_idx != ~0U)
1053 TRACE("%u[%u", offset, reg->array_idx);
1057 shader_dump_src_param(reg->rel_addr, shader_version);
1066 shader_dump_src_param(reg->rel_addr, shader_version);
1069 TRACE("%u", offset);
1070 if (reg->rel_addr) TRACE("]");
1075 void shader_dump_dst_param(const struct wined3d_shader_dst_param *param,
1076 const struct wined3d_shader_version *shader_version)
1078 DWORD write_mask = param->write_mask;
1080 shader_dump_register(¶m->reg, shader_version);
1082 if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
1084 static const char *write_mask_chars = "xyzw";
1087 if (write_mask & WINED3DSP_WRITEMASK_0) TRACE("%c", write_mask_chars[0]);
1088 if (write_mask & WINED3DSP_WRITEMASK_1) TRACE("%c", write_mask_chars[1]);
1089 if (write_mask & WINED3DSP_WRITEMASK_2) TRACE("%c", write_mask_chars[2]);
1090 if (write_mask & WINED3DSP_WRITEMASK_3) TRACE("%c", write_mask_chars[3]);
1094 void shader_dump_src_param(const struct wined3d_shader_src_param *param,
1095 const struct wined3d_shader_version *shader_version)
1097 DWORD src_modifier = param->modifiers;
1098 DWORD swizzle = param->swizzle;
1100 if (src_modifier == WINED3DSPSM_NEG
1101 || src_modifier == WINED3DSPSM_BIASNEG
1102 || src_modifier == WINED3DSPSM_SIGNNEG
1103 || src_modifier == WINED3DSPSM_X2NEG
1104 || src_modifier == WINED3DSPSM_ABSNEG)
1106 else if (src_modifier == WINED3DSPSM_COMP)
1108 else if (src_modifier == WINED3DSPSM_NOT)
1111 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
1114 shader_dump_register(¶m->reg, shader_version);
1118 switch (src_modifier)
1120 case WINED3DSPSM_NONE: break;
1121 case WINED3DSPSM_NEG: break;
1122 case WINED3DSPSM_NOT: break;
1123 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
1124 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
1125 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
1126 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
1127 case WINED3DSPSM_COMP: break;
1128 case WINED3DSPSM_X2: TRACE("_x2"); break;
1129 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
1130 case WINED3DSPSM_DZ: TRACE("_dz"); break;
1131 case WINED3DSPSM_DW: TRACE("_dw"); break;
1132 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
1133 case WINED3DSPSM_ABS: TRACE(")"); break;
1134 default: TRACE("_unknown_modifier(%#x)", src_modifier);
1138 if (swizzle != WINED3DSP_NOSWIZZLE)
1140 static const char *swizzle_chars = "xyzw";
1141 DWORD swizzle_x = swizzle & 0x03;
1142 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1143 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1144 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1146 if (swizzle_x == swizzle_y
1147 && swizzle_x == swizzle_z
1148 && swizzle_x == swizzle_w)
1150 TRACE(".%c", swizzle_chars[swizzle_x]);
1154 TRACE(".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
1155 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
1160 /* Shared code in order to generate the bulk of the shader string.
1161 * NOTE: A description of how to parse tokens can be found on MSDN. */
1162 void shader_generate_main(IWineD3DBaseShaderImpl *shader, struct wined3d_shader_buffer *buffer,
1163 const struct wined3d_shader_reg_maps *reg_maps, const DWORD *byte_code, void *backend_ctx)
1165 IWineD3DDeviceImpl *device = shader->baseShader.device;
1166 const struct wined3d_shader_frontend *fe = shader->baseShader.frontend;
1167 void *fe_data = shader->baseShader.frontend_data;
1168 struct wined3d_shader_src_param dst_rel_addr[2];
1169 struct wined3d_shader_src_param src_rel_addr[4];
1170 struct wined3d_shader_dst_param dst_param[2];
1171 struct wined3d_shader_src_param src_param[4];
1172 struct wined3d_shader_version shader_version;
1173 struct wined3d_shader_loop_state loop_state;
1174 struct wined3d_shader_instruction ins;
1175 struct wined3d_shader_tex_mx tex_mx;
1176 struct wined3d_shader_context ctx;
1177 const DWORD *ptr = byte_code;
1180 /* Initialize current parsing state. */
1181 tex_mx.current_row = 0;
1182 loop_state.current_depth = 0;
1183 loop_state.current_reg = 0;
1185 ctx.shader = shader;
1186 ctx.gl_info = &device->adapter->gl_info;
1187 ctx.reg_maps = reg_maps;
1188 ctx.buffer = buffer;
1189 ctx.tex_mx = &tex_mx;
1190 ctx.loop_state = &loop_state;
1191 ctx.backend_data = backend_ctx;
1194 ins.dst = dst_param;
1195 ins.src = src_param;
1197 fe->shader_read_header(fe_data, &ptr, &shader_version);
1199 while (!fe->shader_is_end(fe_data, &ptr))
1201 const char *comment;
1205 /* Skip comment tokens. */
1206 fe->shader_read_comment(&ptr, &comment, &comment_size);
1207 if (comment) continue;
1210 fe->shader_read_opcode(fe_data, &ptr, &ins, ¶m_size);
1212 /* Unknown opcode and its parameters. */
1213 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1215 TRACE("Skipping unrecognized instruction.\n");
1220 /* Nothing to do. */
1221 if (ins.handler_idx == WINED3DSIH_DCL
1222 || ins.handler_idx == WINED3DSIH_NOP
1223 || ins.handler_idx == WINED3DSIH_DEF
1224 || ins.handler_idx == WINED3DSIH_DEFI
1225 || ins.handler_idx == WINED3DSIH_DEFB
1226 || ins.handler_idx == WINED3DSIH_PHASE)
1232 /* Destination tokens */
1233 for (i = 0; i < ins.dst_count; ++i)
1235 fe->shader_read_dst_param(fe_data, &ptr, &dst_param[i], &dst_rel_addr[i]);
1238 /* Predication token */
1241 FIXME("Predicates not implemented.\n");
1242 ins.predicate = *ptr++;
1245 /* Other source tokens */
1246 for (i = 0; i < ins.src_count; ++i)
1248 fe->shader_read_src_param(fe_data, &ptr, &src_param[i], &src_rel_addr[i]);
1251 /* Call appropriate function for output target */
1252 device->shader_backend->shader_handle_instruction(&ins);
1256 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param *dst)
1258 DWORD mmask = dst->modifiers;
1263 case 13: TRACE("_d8"); break;
1264 case 14: TRACE("_d4"); break;
1265 case 15: TRACE("_d2"); break;
1266 case 1: TRACE("_x2"); break;
1267 case 2: TRACE("_x4"); break;
1268 case 3: TRACE("_x8"); break;
1269 default: TRACE("_unhandled_shift(%d)", dst->shift); break;
1272 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
1273 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
1274 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
1276 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
1277 if (mmask) FIXME("_unrecognized_modifier(%#x)", mmask);
1280 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *byte_code)
1282 struct wined3d_shader_version shader_version;
1283 const DWORD *ptr = byte_code;
1284 const char *type_prefix;
1287 TRACE("Parsing %p.\n", byte_code);
1289 fe->shader_read_header(fe_data, &ptr, &shader_version);
1291 switch (shader_version.type)
1293 case WINED3D_SHADER_TYPE_VERTEX:
1297 case WINED3D_SHADER_TYPE_GEOMETRY:
1301 case WINED3D_SHADER_TYPE_PIXEL:
1306 FIXME("Unhandled shader type %#x.\n", shader_version.type);
1307 type_prefix = "unknown";
1311 TRACE("%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
1313 while (!fe->shader_is_end(fe_data, &ptr))
1315 struct wined3d_shader_instruction ins;
1316 const char *comment;
1321 fe->shader_read_comment(&ptr, &comment, &comment_size);
1324 if (comment_size > 4 && *(const DWORD *)comment == WINEMAKEFOURCC('T', 'E', 'X', 'T'))
1326 const char *end = comment + comment_size;
1327 const char *ptr = comment + 4;
1328 const char *line = ptr;
1335 UINT len = ptr - line;
1336 if (len && *(ptr - 1) == '\r') --len;
1337 TRACE("// %s\n", debugstr_an(line, len));
1342 if (line != ptr) TRACE("// %s\n", debugstr_an(line, ptr - line));
1344 else TRACE("// %s\n", debugstr_an(comment, comment_size));
1348 fe->shader_read_opcode(fe_data, &ptr, &ins, ¶m_size);
1349 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1351 TRACE("Skipping unrecognized instruction.\n");
1356 if (ins.handler_idx == WINED3DSIH_DCL)
1358 struct wined3d_shader_semantic semantic;
1360 fe->shader_read_semantic(&ptr, &semantic);
1362 shader_dump_decl_usage(&semantic, &shader_version);
1363 shader_dump_ins_modifiers(&semantic.reg);
1365 shader_dump_dst_param(&semantic.reg, &shader_version);
1367 else if (ins.handler_idx == WINED3DSIH_DEF)
1369 struct wined3d_shader_src_param rel_addr;
1370 struct wined3d_shader_dst_param dst;
1372 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1374 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(dst.reg.type, dst.reg.idx),
1375 *(const float *)(ptr),
1376 *(const float *)(ptr + 1),
1377 *(const float *)(ptr + 2),
1378 *(const float *)(ptr + 3));
1381 else if (ins.handler_idx == WINED3DSIH_DEFI)
1383 struct wined3d_shader_src_param rel_addr;
1384 struct wined3d_shader_dst_param dst;
1386 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1388 TRACE("defi i%u = %d, %d, %d, %d", dst.reg.idx,
1395 else if (ins.handler_idx == WINED3DSIH_DEFB)
1397 struct wined3d_shader_src_param rel_addr;
1398 struct wined3d_shader_dst_param dst;
1400 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1402 TRACE("defb b%u = %s", dst.reg.idx, *ptr ? "true" : "false");
1407 struct wined3d_shader_src_param dst_rel_addr[2];
1408 struct wined3d_shader_src_param src_rel_addr;
1409 struct wined3d_shader_dst_param dst_param[2];
1410 struct wined3d_shader_src_param src_param;
1412 for (i = 0; i < ins.dst_count; ++i)
1414 fe->shader_read_dst_param(fe_data, &ptr, &dst_param[i], &dst_rel_addr[i]);
1417 /* Print out predication source token first - it follows
1418 * the destination token. */
1421 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
1423 shader_dump_src_param(&src_param, &shader_version);
1427 /* PixWin marks instructions with the coissue flag with a '+' */
1428 if (ins.coissue) TRACE("+");
1430 TRACE("%s", shader_opcode_names[ins.handler_idx]);
1432 if (ins.handler_idx == WINED3DSIH_IFC
1433 || ins.handler_idx == WINED3DSIH_BREAKC)
1437 case COMPARISON_GT: TRACE("_gt"); break;
1438 case COMPARISON_EQ: TRACE("_eq"); break;
1439 case COMPARISON_GE: TRACE("_ge"); break;
1440 case COMPARISON_LT: TRACE("_lt"); break;
1441 case COMPARISON_NE: TRACE("_ne"); break;
1442 case COMPARISON_LE: TRACE("_le"); break;
1443 default: TRACE("_(%u)", ins.flags);
1446 else if (ins.handler_idx == WINED3DSIH_TEX
1447 && shader_version.major >= 2
1448 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
1453 /* We already read the destination tokens, print them. */
1454 for (i = 0; i < ins.dst_count; ++i)
1456 shader_dump_ins_modifiers(&dst_param[i]);
1457 TRACE(!i ? " " : ", ");
1458 shader_dump_dst_param(&dst_param[i], &shader_version);
1461 /* Other source tokens */
1462 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
1464 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
1465 TRACE(!i ? " " : ", ");
1466 shader_dump_src_param(&src_param, &shader_version);
1473 static void shader_cleanup(IWineD3DBaseShaderImpl *shader)
1475 shader->baseShader.device->shader_backend->shader_destroy(shader);
1476 HeapFree(GetProcessHeap(), 0, shader->baseShader.reg_maps.constf);
1477 HeapFree(GetProcessHeap(), 0, shader->baseShader.function);
1478 shader_delete_constant_list(&shader->baseShader.constantsF);
1479 shader_delete_constant_list(&shader->baseShader.constantsB);
1480 shader_delete_constant_list(&shader->baseShader.constantsI);
1481 list_remove(&shader->baseShader.shader_list_entry);
1483 if (shader->baseShader.frontend && shader->baseShader.frontend_data)
1485 shader->baseShader.frontend->shader_free(shader->baseShader.frontend_data);
1489 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
1490 static void shader_none_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS) {}
1491 static void shader_none_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
1492 enum tex_types tex_type, const SIZE *ds_mask_size) {}
1493 static void shader_none_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info) {}
1494 static void shader_none_update_float_vertex_constants(IWineD3DDeviceImpl *device, UINT start, UINT count) {}
1495 static void shader_none_update_float_pixel_constants(IWineD3DDeviceImpl *device, UINT start, UINT count) {}
1496 static void shader_none_load_constants(const struct wined3d_context *context, char usePS, char useVS) {}
1497 static void shader_none_load_np2fixup_constants(void *shader_priv,
1498 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state) {}
1499 static void shader_none_destroy(IWineD3DBaseShaderImpl *shader) {}
1500 static HRESULT shader_none_alloc(IWineD3DDeviceImpl *device) {return WINED3D_OK;}
1501 static void shader_none_free(IWineD3DDeviceImpl *device) {}
1502 static BOOL shader_none_dirty_const(void) {return FALSE;}
1504 static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
1506 /* Set the shader caps to 0 for the none shader backend */
1507 caps->VertexShaderVersion = 0;
1508 caps->MaxVertexShaderConst = 0;
1509 caps->PixelShaderVersion = 0;
1510 caps->PixelShader1xMaxValue = 0.0f;
1511 caps->MaxPixelShaderConst = 0;
1512 caps->VSClipping = FALSE;
1515 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1517 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1519 TRACE("Checking support for fixup:\n");
1520 dump_color_fixup_desc(fixup);
1523 /* Faked to make some apps happy. */
1524 if (!is_complex_fixup(fixup))
1530 TRACE("[FAILED]\n");
1534 const shader_backend_t none_shader_backend = {
1535 shader_none_handle_instruction,
1537 shader_none_select_depth_blt,
1538 shader_none_deselect_depth_blt,
1539 shader_none_update_float_vertex_constants,
1540 shader_none_update_float_pixel_constants,
1541 shader_none_load_constants,
1542 shader_none_load_np2fixup_constants,
1543 shader_none_destroy,
1546 shader_none_dirty_const,
1547 shader_none_get_caps,
1548 shader_none_color_fixup_supported,
1551 static HRESULT shader_get_function(IWineD3DBaseShaderImpl *shader, void *data, UINT *data_size)
1555 *data_size = shader->baseShader.functionLength;
1559 if (*data_size < shader->baseShader.functionLength)
1561 /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
1562 * than the required size we should write the required size and
1563 * return D3DERR_MOREDATA. That's not actually true. */
1564 return WINED3DERR_INVALIDCALL;
1567 memcpy(data, shader->baseShader.function, shader->baseShader.functionLength);
1572 /* Set local constants for d3d8 shaders. */
1573 static HRESULT shader_set_local_constants_float(IWineD3DBaseShaderImpl *shader,
1574 UINT start_idx, const float *src_data, UINT count)
1576 UINT end_idx = start_idx + count;
1579 if (end_idx > shader->baseShader.limits.constant_float)
1581 WARN("end_idx %u > float constants limit %u.\n",
1582 end_idx, shader->baseShader.limits.constant_float);
1583 end_idx = shader->baseShader.limits.constant_float;
1586 for (i = start_idx; i < end_idx; ++i)
1588 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
1589 if (!lconst) return E_OUTOFMEMORY;
1592 memcpy(lconst->value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
1593 list_add_head(&shader->baseShader.constantsF, &lconst->entry);
1599 static HRESULT shader_set_function(IWineD3DBaseShaderImpl *shader, const DWORD *byte_code,
1600 const struct wined3d_shader_signature *output_signature, DWORD float_const_count)
1602 struct wined3d_shader_reg_maps *reg_maps = &shader->baseShader.reg_maps;
1603 const struct wined3d_shader_frontend *fe;
1606 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1607 shader, byte_code, output_signature, float_const_count);
1609 fe = shader_select_frontend(*byte_code);
1612 FIXME("Unable to find frontend for shader.\n");
1613 return WINED3DERR_INVALIDCALL;
1615 shader->baseShader.frontend = fe;
1616 shader->baseShader.frontend_data = fe->shader_init(byte_code, output_signature);
1617 if (!shader->baseShader.frontend_data)
1619 FIXME("Failed to initialize frontend.\n");
1620 return WINED3DERR_INVALIDCALL;
1623 /* First pass: trace shader. */
1624 if (TRACE_ON(d3d_shader)) shader_trace_init(fe, shader->baseShader.frontend_data, byte_code);
1626 /* Initialize immediate constant lists. */
1627 list_init(&shader->baseShader.constantsF);
1628 list_init(&shader->baseShader.constantsB);
1629 list_init(&shader->baseShader.constantsI);
1631 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1632 hr = shader_get_registers_used(shader, fe,
1633 reg_maps, shader->baseShader.input_signature, shader->baseShader.output_signature,
1634 byte_code, float_const_count);
1635 if (FAILED(hr)) return hr;
1637 shader->baseShader.function = HeapAlloc(GetProcessHeap(), 0, shader->baseShader.functionLength);
1638 if (!shader->baseShader.function) return E_OUTOFMEMORY;
1639 memcpy(shader->baseShader.function, byte_code, shader->baseShader.functionLength);
1644 static HRESULT STDMETHODCALLTYPE vertexshader_QueryInterface(IWineD3DBaseShader *iface, REFIID riid, void **object)
1646 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1648 if (IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1649 || IsEqualGUID(riid, &IID_IWineD3DBase)
1650 || IsEqualGUID(riid, &IID_IUnknown))
1652 IUnknown_AddRef(iface);
1657 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1660 return E_NOINTERFACE;
1663 static ULONG STDMETHODCALLTYPE vertexshader_AddRef(IWineD3DBaseShader *iface)
1665 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
1666 ULONG refcount = InterlockedIncrement(&shader->baseShader.ref);
1668 TRACE("%p increasing refcount to %u.\n", shader, refcount);
1673 /* Do not call while under the GL lock. */
1674 static ULONG STDMETHODCALLTYPE vertexshader_Release(IWineD3DBaseShader *iface)
1676 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
1677 ULONG refcount = InterlockedDecrement(&shader->baseShader.ref);
1679 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1683 shader_cleanup(shader);
1684 shader->baseShader.parent_ops->wined3d_object_destroyed(shader->baseShader.parent);
1685 HeapFree(GetProcessHeap(), 0, shader);
1691 static void * STDMETHODCALLTYPE vertexshader_GetParent(IWineD3DBaseShader *iface)
1693 TRACE("iface %p.\n", iface);
1695 return ((IWineD3DBaseShaderImpl *)iface)->baseShader.parent;
1698 static HRESULT STDMETHODCALLTYPE vertexshader_GetFunction(IWineD3DBaseShader *iface, void *data, UINT *data_size)
1700 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
1702 return shader_get_function((IWineD3DBaseShaderImpl *)iface, data, data_size);
1705 static HRESULT STDMETHODCALLTYPE vertexshader_SetLocalConstantsF(IWineD3DBaseShader *iface,
1706 UINT start_idx, const float *src_data, UINT count)
1708 TRACE("iface %p, start_idx %u, src_data %p, count %u.\n", iface, start_idx, src_data, count);
1710 return shader_set_local_constants_float((IWineD3DBaseShaderImpl *)iface,
1711 start_idx, src_data, count);
1714 static const IWineD3DBaseShaderVtbl IWineD3DVertexShader_Vtbl =
1716 /* IUnknown methods */
1717 vertexshader_QueryInterface,
1718 vertexshader_AddRef,
1719 vertexshader_Release,
1720 /* IWineD3DBase methods */
1721 vertexshader_GetParent,
1722 /* IWineD3DBaseShader methods */
1723 vertexshader_GetFunction,
1724 vertexshader_SetLocalConstantsF,
1727 void find_vs_compile_args(const struct wined3d_state *state,
1728 IWineD3DBaseShaderImpl *shader, struct vs_compile_args *args)
1730 args->fog_src = state->render_states[WINED3DRS_FOGTABLEMODE]
1731 == WINED3DFOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
1732 args->clip_enabled = state->render_states[WINED3DRS_CLIPPING]
1733 && state->render_states[WINED3DRS_CLIPPLANEENABLE];
1734 args->swizzle_map = shader->baseShader.device->strided_streams.swizzle_map;
1737 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
1739 if (usage_idx1 != usage_idx2) return FALSE;
1740 if (usage1 == usage2) return TRUE;
1741 if (usage1 == WINED3DDECLUSAGE_POSITION && usage2 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
1742 if (usage2 == WINED3DDECLUSAGE_POSITION && usage1 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
1747 BOOL vshader_get_input(struct IWineD3DBaseShaderImpl *shader,
1748 BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
1750 WORD map = shader->baseShader.reg_maps.input_registers;
1753 for (i = 0; map; map >>= 1, ++i)
1755 if (!(map & 1)) continue;
1757 if (match_usage(shader->u.vs.attributes[i].usage,
1758 shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
1767 static void vertexshader_set_limits(IWineD3DBaseShaderImpl *shader)
1769 DWORD shader_version = WINED3D_SHADER_VERSION(shader->baseShader.reg_maps.shader_version.major,
1770 shader->baseShader.reg_maps.shader_version.minor);
1771 IWineD3DDeviceImpl *device = shader->baseShader.device;
1773 shader->baseShader.limits.texcoord = 0;
1774 shader->baseShader.limits.attributes = 16;
1775 shader->baseShader.limits.packed_input = 0;
1777 switch (shader_version)
1779 case WINED3D_SHADER_VERSION(1, 0):
1780 case WINED3D_SHADER_VERSION(1, 1):
1781 shader->baseShader.limits.temporary = 12;
1782 shader->baseShader.limits.constant_bool = 0;
1783 shader->baseShader.limits.constant_int = 0;
1784 shader->baseShader.limits.address = 1;
1785 shader->baseShader.limits.packed_output = 12;
1786 shader->baseShader.limits.sampler = 0;
1787 shader->baseShader.limits.label = 0;
1788 /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1789 * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1791 shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1794 case WINED3D_SHADER_VERSION(2, 0):
1795 case WINED3D_SHADER_VERSION(2, 1):
1796 shader->baseShader.limits.temporary = 12;
1797 shader->baseShader.limits.constant_bool = 16;
1798 shader->baseShader.limits.constant_int = 16;
1799 shader->baseShader.limits.address = 1;
1800 shader->baseShader.limits.packed_output = 12;
1801 shader->baseShader.limits.sampler = 0;
1802 shader->baseShader.limits.label = 16;
1803 shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1806 case WINED3D_SHADER_VERSION(4, 0):
1807 FIXME("Using 3.0 limits for 4.0 shader.\n");
1810 case WINED3D_SHADER_VERSION(3, 0):
1811 shader->baseShader.limits.temporary = 32;
1812 shader->baseShader.limits.constant_bool = 32;
1813 shader->baseShader.limits.constant_int = 32;
1814 shader->baseShader.limits.address = 1;
1815 shader->baseShader.limits.packed_output = 12;
1816 shader->baseShader.limits.sampler = 4;
1817 shader->baseShader.limits.label = 16; /* FIXME: 2048 */
1818 /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1819 * even though they are capable of supporting much more (GL
1820 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1821 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1822 * shaders to 256. */
1823 shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1827 shader->baseShader.limits.temporary = 12;
1828 shader->baseShader.limits.constant_bool = 16;
1829 shader->baseShader.limits.constant_int = 16;
1830 shader->baseShader.limits.address = 1;
1831 shader->baseShader.limits.packed_output = 12;
1832 shader->baseShader.limits.sampler = 0;
1833 shader->baseShader.limits.label = 16;
1834 shader->baseShader.limits.constant_float = min(256, device->d3d_vshader_constantF);
1835 FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1836 shader->baseShader.reg_maps.shader_version.major,
1837 shader->baseShader.reg_maps.shader_version.minor);
1841 HRESULT vertexshader_init(IWineD3DBaseShaderImpl *shader, IWineD3DDeviceImpl *device,
1842 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1843 void *parent, const struct wined3d_parent_ops *parent_ops)
1845 struct wined3d_shader_reg_maps *reg_maps = &shader->baseShader.reg_maps;
1850 if (!byte_code) return WINED3DERR_INVALIDCALL;
1852 shader->lpVtbl = &IWineD3DVertexShader_Vtbl;
1853 shader_init(&shader->baseShader, device, parent, parent_ops);
1855 hr = shader_set_function(shader, byte_code, output_signature, device->d3d_vshader_constantF);
1858 WARN("Failed to set function, hr %#x.\n", hr);
1859 shader_cleanup(shader);
1863 map = reg_maps->input_registers;
1864 for (i = 0; map; map >>= 1, ++i)
1866 if (!(map & 1) || !shader->baseShader.input_signature[i].semantic_name) continue;
1868 shader->u.vs.attributes[i].usage =
1869 shader_usage_from_semantic_name(shader->baseShader.input_signature[i].semantic_name);
1870 shader->u.vs.attributes[i].usage_idx = shader->baseShader.input_signature[i].semantic_idx;
1873 if (output_signature)
1875 for (i = 0; i < output_signature->element_count; ++i)
1877 struct wined3d_shader_signature_element *e = &output_signature->elements[i];
1878 reg_maps->output_registers |= 1 << e->register_idx;
1879 shader->baseShader.output_signature[e->register_idx] = *e;
1883 vertexshader_set_limits(shader);
1885 shader->baseShader.load_local_constsF = reg_maps->usesrelconstF
1886 && !list_empty(&shader->baseShader.constantsF);
1891 static HRESULT STDMETHODCALLTYPE geometryshader_QueryInterface(IWineD3DBaseShader *iface,
1892 REFIID riid, void **object)
1894 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1896 if (IsEqualGUID(riid, &IID_IWineD3DBaseShader)
1897 || IsEqualGUID(riid, &IID_IWineD3DBase)
1898 || IsEqualGUID(riid, &IID_IUnknown))
1900 IUnknown_AddRef(iface);
1905 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1908 return E_NOINTERFACE;
1911 static ULONG STDMETHODCALLTYPE geometryshader_AddRef(IWineD3DBaseShader *iface)
1913 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
1914 ULONG refcount = InterlockedIncrement(&shader->baseShader.ref);
1916 TRACE("%p increasing refcount to %u.\n", shader, refcount);
1921 /* Do not call while under the GL lock. */
1922 static ULONG STDMETHODCALLTYPE geometryshader_Release(IWineD3DBaseShader *iface)
1924 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
1925 ULONG refcount = InterlockedDecrement(&shader->baseShader.ref);
1927 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1931 shader_cleanup(shader);
1932 shader->baseShader.parent_ops->wined3d_object_destroyed(shader->baseShader.parent);
1933 HeapFree(GetProcessHeap(), 0, shader);
1939 static void * STDMETHODCALLTYPE geometryshader_GetParent(IWineD3DBaseShader *iface)
1941 TRACE("iface %p.\n", iface);
1943 return ((IWineD3DBaseShaderImpl *)iface)->baseShader.parent;
1946 static HRESULT STDMETHODCALLTYPE geometryshader_GetFunction(IWineD3DBaseShader *iface, void *data, UINT *data_size)
1948 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
1950 return shader_get_function((IWineD3DBaseShaderImpl *)iface, data, data_size);
1953 static HRESULT STDMETHODCALLTYPE geometryshader_SetLocalConstantsF(IWineD3DBaseShader *iface,
1954 UINT start_idx, const float *src_data, UINT count)
1956 TRACE("iface %p, start_idx %u, src_data %p, count %u.\n", iface, start_idx, src_data, count);
1958 return shader_set_local_constants_float((IWineD3DBaseShaderImpl *)iface,
1959 start_idx, src_data, count);
1962 static const IWineD3DBaseShaderVtbl wined3d_geometryshader_vtbl =
1964 /* IUnknown methods */
1965 geometryshader_QueryInterface,
1966 geometryshader_AddRef,
1967 geometryshader_Release,
1968 /* IWineD3DBase methods */
1969 geometryshader_GetParent,
1970 /* IWineD3DBaseShader methods */
1971 geometryshader_GetFunction,
1972 geometryshader_SetLocalConstantsF,
1975 HRESULT geometryshader_init(IWineD3DBaseShaderImpl *shader, IWineD3DDeviceImpl *device,
1976 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1977 void *parent, const struct wined3d_parent_ops *parent_ops)
1981 shader->lpVtbl = &wined3d_geometryshader_vtbl;
1982 shader_init(&shader->baseShader, device, parent, parent_ops);
1984 hr = shader_set_function(shader, byte_code, output_signature, 0);
1987 WARN("Failed to set function, hr %#x.\n", hr);
1988 shader_cleanup(shader);
1992 shader->baseShader.load_local_constsF = FALSE;
1997 static HRESULT STDMETHODCALLTYPE pixelshader_QueryInterface(IWineD3DBaseShader *iface, REFIID riid, void **object)
1999 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
2001 if (IsEqualGUID(riid, &IID_IWineD3DBaseShader)
2002 || IsEqualGUID(riid, &IID_IWineD3DBase)
2003 || IsEqualGUID(riid, &IID_IUnknown))
2005 IUnknown_AddRef(iface);
2010 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
2013 return E_NOINTERFACE;
2016 static ULONG STDMETHODCALLTYPE pixelshader_AddRef(IWineD3DBaseShader *iface)
2018 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
2019 ULONG refcount = InterlockedIncrement(&shader->baseShader.ref);
2021 TRACE("%p increasing refcount to %u.\n", shader, refcount);
2026 /* Do not call while under the GL lock. */
2027 static ULONG STDMETHODCALLTYPE pixelshader_Release(IWineD3DBaseShader *iface)
2029 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)iface;
2030 ULONG refcount = InterlockedDecrement(&shader->baseShader.ref);
2032 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
2036 shader_cleanup(shader);
2037 shader->baseShader.parent_ops->wined3d_object_destroyed(shader->baseShader.parent);
2038 HeapFree(GetProcessHeap(), 0, shader);
2044 static void * STDMETHODCALLTYPE pixelshader_GetParent(IWineD3DBaseShader *iface)
2046 TRACE("iface %p.\n", iface);
2048 return ((IWineD3DBaseShaderImpl *)iface)->baseShader.parent;
2051 static HRESULT STDMETHODCALLTYPE pixelshader_GetFunction(IWineD3DBaseShader *iface, void *data, UINT *data_size)
2053 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
2055 return shader_get_function((IWineD3DBaseShaderImpl *)iface, data, data_size);
2058 static HRESULT STDMETHODCALLTYPE pixelshader_SetLocalConstantsF(IWineD3DBaseShader *iface,
2059 UINT start_idx, const float *src_data, UINT count)
2061 TRACE("iface %p, start_idx %u, src_data %p, count %u.\n", iface, start_idx, src_data, count);
2063 return shader_set_local_constants_float((IWineD3DBaseShaderImpl *)iface,
2064 start_idx, src_data, count);
2067 static const IWineD3DBaseShaderVtbl IWineD3DPixelShader_Vtbl =
2069 /* IUnknown methods */
2070 pixelshader_QueryInterface,
2072 pixelshader_Release,
2073 /* IWineD3DBase methods */
2074 pixelshader_GetParent,
2075 /* IWineD3DBaseShader methods */
2076 pixelshader_GetFunction,
2077 pixelshader_SetLocalConstantsF,
2080 void find_ps_compile_args(const struct wined3d_state *state,
2081 IWineD3DBaseShaderImpl *shader, struct ps_compile_args *args)
2083 IWineD3DDeviceImpl *device = shader->baseShader.device;
2084 const struct wined3d_texture *texture;
2087 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
2088 if (state->render_states[WINED3DRS_SRGBWRITEENABLE])
2090 IWineD3DSurfaceImpl *rt = device->render_targets[0];
2091 if (rt->resource.format->flags & WINED3DFMT_FLAG_SRGB_WRITE) args->srgb_correction = 1;
2094 if (shader->baseShader.reg_maps.shader_version.major == 1
2095 && shader->baseShader.reg_maps.shader_version.minor <= 3)
2097 for (i = 0; i < 4; ++i)
2099 DWORD flags = state->texture_states[i][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2100 DWORD tex_transform = flags & ~WINED3DTTFF_PROJECTED;
2101 if (flags & WINED3DTTFF_PROJECTED)
2102 tex_transform |= WINED3D_PSARGS_PROJECTED;
2103 args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
2107 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2109 if (!shader->baseShader.reg_maps.sampler_type[i]) continue;
2110 texture = state->textures[i];
2113 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
2116 args->color_fixup[i] = texture->resource.format->color_fixup;
2118 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
2119 args->shadow |= 1 << i;
2121 /* Flag samplers that need NP2 texcoord fixup. */
2122 if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
2123 args->np2_fixup |= (1 << i);
2125 if (shader->baseShader.reg_maps.shader_version.major >= 3)
2127 if (device->strided_streams.position_transformed)
2129 args->vp_mode = pretransformed;
2131 else if (use_vs(state))
2133 args->vp_mode = vertexshader;
2137 args->vp_mode = fixedfunction;
2139 args->fog = FOG_OFF;
2143 args->vp_mode = vertexshader;
2144 if (state->render_states[WINED3DRS_FOGENABLE])
2146 switch (state->render_states[WINED3DRS_FOGTABLEMODE])
2148 case WINED3DFOG_NONE:
2149 if (device->strided_streams.position_transformed || use_vs(state))
2151 args->fog = FOG_LINEAR;
2155 switch (state->render_states[WINED3DRS_FOGVERTEXMODE])
2157 case WINED3DFOG_NONE: /* Fall through. */
2158 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
2159 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
2160 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
2164 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
2165 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
2166 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
2171 args->fog = FOG_OFF;
2176 static void pixelshader_set_limits(IWineD3DBaseShaderImpl *shader)
2178 DWORD shader_version = WINED3D_SHADER_VERSION(shader->baseShader.reg_maps.shader_version.major,
2179 shader->baseShader.reg_maps.shader_version.minor);
2181 shader->baseShader.limits.attributes = 0;
2182 shader->baseShader.limits.address = 0;
2183 shader->baseShader.limits.packed_output = 0;
2185 switch (shader_version)
2187 case WINED3D_SHADER_VERSION(1, 0):
2188 case WINED3D_SHADER_VERSION(1, 1):
2189 case WINED3D_SHADER_VERSION(1, 2):
2190 case WINED3D_SHADER_VERSION(1, 3):
2191 shader->baseShader.limits.temporary = 2;
2192 shader->baseShader.limits.constant_float = 8;
2193 shader->baseShader.limits.constant_int = 0;
2194 shader->baseShader.limits.constant_bool = 0;
2195 shader->baseShader.limits.texcoord = 4;
2196 shader->baseShader.limits.sampler = 4;
2197 shader->baseShader.limits.packed_input = 0;
2198 shader->baseShader.limits.label = 0;
2201 case WINED3D_SHADER_VERSION(1, 4):
2202 shader->baseShader.limits.temporary = 6;
2203 shader->baseShader.limits.constant_float = 8;
2204 shader->baseShader.limits.constant_int = 0;
2205 shader->baseShader.limits.constant_bool = 0;
2206 shader->baseShader.limits.texcoord = 6;
2207 shader->baseShader.limits.sampler = 6;
2208 shader->baseShader.limits.packed_input = 0;
2209 shader->baseShader.limits.label = 0;
2212 /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2213 case WINED3D_SHADER_VERSION(2, 0):
2214 shader->baseShader.limits.temporary = 32;
2215 shader->baseShader.limits.constant_float = 32;
2216 shader->baseShader.limits.constant_int = 16;
2217 shader->baseShader.limits.constant_bool = 16;
2218 shader->baseShader.limits.texcoord = 8;
2219 shader->baseShader.limits.sampler = 16;
2220 shader->baseShader.limits.packed_input = 0;
2223 case WINED3D_SHADER_VERSION(2, 1):
2224 shader->baseShader.limits.temporary = 32;
2225 shader->baseShader.limits.constant_float = 32;
2226 shader->baseShader.limits.constant_int = 16;
2227 shader->baseShader.limits.constant_bool = 16;
2228 shader->baseShader.limits.texcoord = 8;
2229 shader->baseShader.limits.sampler = 16;
2230 shader->baseShader.limits.packed_input = 0;
2231 shader->baseShader.limits.label = 16;
2234 case WINED3D_SHADER_VERSION(4, 0):
2235 FIXME("Using 3.0 limits for 4.0 shader.\n");
2238 case WINED3D_SHADER_VERSION(3, 0):
2239 shader->baseShader.limits.temporary = 32;
2240 shader->baseShader.limits.constant_float = 224;
2241 shader->baseShader.limits.constant_int = 16;
2242 shader->baseShader.limits.constant_bool = 16;
2243 shader->baseShader.limits.texcoord = 0;
2244 shader->baseShader.limits.sampler = 16;
2245 shader->baseShader.limits.packed_input = 12;
2246 shader->baseShader.limits.label = 16; /* FIXME: 2048 */
2250 shader->baseShader.limits.temporary = 32;
2251 shader->baseShader.limits.constant_float = 32;
2252 shader->baseShader.limits.constant_int = 16;
2253 shader->baseShader.limits.constant_bool = 16;
2254 shader->baseShader.limits.texcoord = 8;
2255 shader->baseShader.limits.sampler = 16;
2256 shader->baseShader.limits.packed_input = 0;
2257 shader->baseShader.limits.label = 0;
2258 FIXME("Unrecognized pixel shader version %u.%u\n",
2259 shader->baseShader.reg_maps.shader_version.major,
2260 shader->baseShader.reg_maps.shader_version.minor);
2264 HRESULT pixelshader_init(IWineD3DBaseShaderImpl *shader, IWineD3DDeviceImpl *device,
2265 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
2266 void *parent, const struct wined3d_parent_ops *parent_ops)
2268 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2269 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
2272 if (!byte_code) return WINED3DERR_INVALIDCALL;
2274 shader->lpVtbl = &IWineD3DPixelShader_Vtbl;
2275 shader_init(&shader->baseShader, device, parent, parent_ops);
2277 hr = shader_set_function(shader, byte_code, output_signature, device->d3d_pshader_constantF);
2280 WARN("Failed to set function, hr %#x.\n", hr);
2281 shader_cleanup(shader);
2285 pixelshader_set_limits(shader);
2287 for (i = 0; i < MAX_REG_INPUT; ++i)
2289 if (shader->u.ps.input_reg_used[i])
2292 highest_reg_used = i;
2296 /* Don't do any register mapping magic if it is not needed, or if we can't
2297 * achieve anything anyway */
2298 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
2299 || num_regs_used > (gl_info->limits.glsl_varyings / 4))
2301 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
2303 /* This happens with relative addressing. The input mapper function
2304 * warns about this if the higher registers are declared too, so
2305 * don't write a FIXME here */
2306 WARN("More varying registers used than supported\n");
2309 for (i = 0; i < MAX_REG_INPUT; ++i)
2311 shader->u.ps.input_reg_map[i] = i;
2314 shader->u.ps.declared_in_count = highest_reg_used + 1;
2318 shader->u.ps.declared_in_count = 0;
2319 for (i = 0; i < MAX_REG_INPUT; ++i)
2321 if (shader->u.ps.input_reg_used[i])
2322 shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
2323 else shader->u.ps.input_reg_map[i] = ~0U;
2327 shader->baseShader.load_local_constsF = FALSE;
2332 void pixelshader_update_samplers(struct wined3d_shader_reg_maps *reg_maps, struct wined3d_texture * const *textures)
2334 WINED3DSAMPLER_TEXTURE_TYPE *sampler_type = reg_maps->sampler_type;
2337 if (reg_maps->shader_version.major != 1) return;
2339 for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
2341 /* We don't sample from this sampler. */
2342 if (!sampler_type[i]) continue;
2346 WARN("No texture bound to sampler %u, using 2D.\n", i);
2347 sampler_type[i] = WINED3DSTT_2D;
2351 switch (textures[i]->target)
2353 case GL_TEXTURE_RECTANGLE_ARB:
2355 /* We have to select between texture rectangles and 2D
2356 * textures later because 2.0 and 3.0 shaders only have
2357 * WINED3DSTT_2D as well. */
2358 sampler_type[i] = WINED3DSTT_2D;
2362 sampler_type[i] = WINED3DSTT_VOLUME;
2365 case GL_TEXTURE_CUBE_MAP_ARB:
2366 sampler_type[i] = WINED3DSTT_CUBE;
2370 FIXME("Unrecognized texture type %#x, using 2D.\n", textures[i]->target);
2371 sampler_type[i] = WINED3DSTT_2D;