wined3d: Get rid of the WINED3DSHADER_PARAM_SRCMOD_TYPE typedef.
[wine/multimedia.git] / dlls / wined3d / shader.c
blob447de8f8ca5c2659c256fa9393c5807bfe4230b8
1 /*
2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2005 Oliver Stieber
6 * Copyright 2006 Ivan Gyurdiev
7 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
27 #include <math.h>
28 #include <stdio.h>
29 #include <string.h>
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)
172 unsigned int i;
174 for (i = 0; i < sizeof(semantic_names) / sizeof(*semantic_names); ++i)
176 if (!strcmp(name, semantic_names[i])) return i;
179 return ~0U;
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)
213 case WINED3D_SM1_VS:
214 case WINED3D_SM1_PS:
215 return &sm1_shader_frontend;
217 case WINED3D_SM4_PS:
218 case WINED3D_SM4_VS:
219 case WINED3D_SM4_GS:
220 return &sm4_shader_frontend;
222 default:
223 FIXME("Unrecognised version token %#x\n", version_token);
224 return NULL;
228 void shader_buffer_clear(struct wined3d_shader_buffer *buffer)
230 buffer->buffer[0] = '\0';
231 buffer->bsize = 0;
232 buffer->lineNo = 0;
233 buffer->newline = TRUE;
236 BOOL shader_buffer_init(struct wined3d_shader_buffer *buffer)
238 buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
239 if (!buffer->buffer)
241 ERR("Failed to allocate shader buffer memory.\n");
242 return FALSE;
245 shader_buffer_clear(buffer);
246 return TRUE;
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;
257 int rc;
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;
266 return -1;
269 if (buffer->newline)
271 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
272 buffer->newline = FALSE;
274 else
276 TRACE("%s", base);
279 buffer->bsize += rc;
280 if (buffer->buffer[buffer->bsize-1] == '\n')
282 ++buffer->lineNo;
283 buffer->newline = TRUE;
286 return 0;
289 int shader_addline(struct wined3d_shader_buffer *buffer, const char *format, ...)
291 va_list args;
292 int ret;
294 va_start(args, format);
295 ret = shader_vaddline(buffer, format, args);
296 va_end(args);
298 return ret;
301 static void shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
302 void *parent, const struct wined3d_parent_ops *parent_ops)
304 shader->ref = 1;
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(enum wined3d_shader_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;
322 default:
323 FIXME("Unsupported register type: %u.\n", register_type);
324 return register_idx;
328 static void shader_delete_constant_list(struct list *clist)
330 struct wined3d_shader_lconst *constant;
331 struct list *ptr;
333 ptr = list_head(clist);
334 while (ptr)
336 constant = LIST_ENTRY(ptr, struct wined3d_shader_lconst, entry);
337 ptr = list_next(clist, ptr);
338 HeapFree(GetProcessHeap(), 0, constant);
340 list_init(clist);
343 static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
345 DWORD idx, shift;
346 idx = bit >> 5;
347 shift = bit & 0x1f;
348 bitmap[idx] |= (1 << shift);
351 static void shader_record_register_usage(struct wined3d_shader *shader, struct wined3d_shader_reg_maps *reg_maps,
352 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type)
354 switch (reg->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;
359 break;
361 case WINED3DSPR_TEMP:
362 reg_maps->temporary |= 1 << reg->idx;
363 break;
365 case WINED3DSPR_INPUT:
366 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
368 if (reg->rel_addr)
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 */
373 unsigned int i;
374 for (i = 0; i < MAX_REG_INPUT; ++i)
376 shader->u.ps.input_reg_used[i] = TRUE;
379 else
381 shader->u.ps.input_reg_used[reg->idx] = TRUE;
384 else reg_maps->input_registers |= 1 << reg->idx;
385 break;
387 case WINED3DSPR_RASTOUT:
388 if (reg->idx == 1) reg_maps->fog = 1;
389 break;
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;
397 break;
399 case WINED3DSPR_CONST:
400 if (reg->rel_addr)
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;
406 else
408 set_bitmap_bit(reg_maps->constf, reg->idx);
410 break;
412 case WINED3DSPR_CONSTINT:
413 reg_maps->integer_constants |= (1 << reg->idx);
414 break;
416 case WINED3DSPR_CONSTBOOL:
417 reg_maps->boolean_constants |= (1 << reg->idx);
418 break;
420 case WINED3DSPR_COLOROUT:
421 reg_maps->rt_mask |= (1 << reg->idx);
422 break;
424 default:
425 TRACE("Not recording register of type %#x and idx %u\n", reg->type, reg->idx);
426 break;
430 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
432 switch (instr)
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;
445 default:
446 return 0;
450 /* Note that this does not count the loop register as an address register. */
451 static HRESULT shader_get_registers_used(struct wined3d_shader *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->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;
477 const char *comment;
478 UINT comment_size;
479 UINT param_size;
481 /* Skip comments. */
482 fe->shader_read_comment(&ptr, &comment, &comment_size);
483 if (comment) continue;
485 /* Fetch opcode. */
486 fe->shader_read_opcode(fe_data, &ptr, &ins, &param_size);
488 /* Unhandled opcode, and its parameters. */
489 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
491 TRACE("Skipping unrecognized instruction.\n");
492 ptr += param_size;
493 continue;
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);
509 break;
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;
516 break;
518 /* Save sampler usage token. */
519 case WINED3DSPR_SAMPLER:
520 reg_maps->sampler_type[semantic.reg.reg.idx] = semantic.sampler_type;
521 break;
523 default:
524 TRACE("Not recording DCL register type %#x.\n", semantic.reg.reg.type);
525 break;
528 else if (ins.handler_idx == WINED3DSIH_DEF)
530 struct wined3d_shader_src_param rel_addr;
531 struct wined3d_shader_dst_param dst;
533 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
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));
540 ptr += 4;
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->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 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
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));
570 ptr += 4;
572 list_add_head(&shader->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 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
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));
587 ++ptr;
589 list_add_head(&shader->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;
605 else
607 fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
608 reg_maps->integer_constants |= 1 << src.reg.idx;
611 cur_loop_depth++;
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)
617 cur_loop_depth--;
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. */
628 else
630 BOOL color0_mov = FALSE;
631 unsigned int i, limit;
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:
658 switch (idx)
660 case 0: /* oPos */
661 reg_maps->output_registers |= 1 << 10;
662 shader_signature_from_usage(&output_signature[10],
663 WINED3DDECLUSAGE_POSITION, 0, 10, WINED3DSP_WRITEMASK_ALL);
664 break;
666 case 1: /* oFog */
667 reg_maps->output_registers |= 1 << 11;
668 shader_signature_from_usage(&output_signature[11],
669 WINED3DDECLUSAGE_FOG, 0, 11, WINED3DSP_WRITEMASK_0);
670 break;
672 case 2: /* oPts */
673 reg_maps->output_registers |= 1 << 11;
674 shader_signature_from_usage(&output_signature[11],
675 WINED3DDECLUSAGE_PSIZE, 0, 11, WINED3DSP_WRITEMASK_1);
676 break;
678 break;
680 case WINED3DSPR_ATTROUT:
681 if (idx < 2)
683 idx += 8;
684 if (reg_maps->output_registers & (1 << idx))
686 output_signature[idx].mask |= dst_param.write_mask;
688 else
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);
695 break;
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;
704 else
706 reg_maps->output_registers |= 1 << idx;
707 shader_signature_from_usage(&output_signature[idx],
708 WINED3DDECLUSAGE_TEXCOORD, idx, idx, dst_param.write_mask);
710 break;
712 default:
713 break;
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. */
733 color0_mov = TRUE;
736 /* Also drop the MOV marker if the source register is overwritten prior to the shader
737 * end
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_POW) reg_maps->usespow = 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;
797 unsigned int count;
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);
803 while (count)
805 ++src_param.reg.idx;
806 shader_record_register_usage(shader, reg_maps, &src_param.reg, shader_version.type);
807 --count;
810 if (color0_mov)
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 /* PS before 2.0 don't have explicit color outputs. Instead the value of
825 * R0 is written to the render target. */
826 if (shader_version.major < 2 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
827 reg_maps->rt_mask |= (1 << 0);
829 shader->functionLength = ((const char *)ptr - (const char *)byte_code);
831 return WINED3D_OK;
834 unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
836 DWORD map = 1 << max;
837 map |= map - 1;
838 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
840 return wined3d_log2i(map);
843 static void shader_dump_decl_usage(const struct wined3d_shader_semantic *semantic,
844 const struct wined3d_shader_version *shader_version)
846 TRACE("dcl");
848 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
850 switch (semantic->sampler_type)
852 case WINED3DSTT_2D: TRACE("_2d"); break;
853 case WINED3DSTT_CUBE: TRACE("_cube"); break;
854 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
855 default: TRACE("_unknown_ttype(0x%08x)", semantic->sampler_type);
858 else
860 /* Pixel shaders 3.0 don't have usage semantics. */
861 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL) return;
862 else TRACE("_");
864 switch (semantic->usage)
866 case WINED3DDECLUSAGE_POSITION:
867 TRACE("position%u", semantic->usage_idx);
868 break;
870 case WINED3DDECLUSAGE_BLENDINDICES:
871 TRACE("blend");
872 break;
874 case WINED3DDECLUSAGE_BLENDWEIGHT:
875 TRACE("weight");
876 break;
878 case WINED3DDECLUSAGE_NORMAL:
879 TRACE("normal%u", semantic->usage_idx);
880 break;
882 case WINED3DDECLUSAGE_PSIZE:
883 TRACE("psize");
884 break;
886 case WINED3DDECLUSAGE_COLOR:
887 if (!semantic->usage_idx) TRACE("color");
888 else TRACE("specular%u", (semantic->usage_idx - 1));
889 break;
891 case WINED3DDECLUSAGE_TEXCOORD:
892 TRACE("texture%u", semantic->usage_idx);
893 break;
895 case WINED3DDECLUSAGE_TANGENT:
896 TRACE("tangent");
897 break;
899 case WINED3DDECLUSAGE_BINORMAL:
900 TRACE("binormal");
901 break;
903 case WINED3DDECLUSAGE_TESSFACTOR:
904 TRACE("tessfactor");
905 break;
907 case WINED3DDECLUSAGE_POSITIONT:
908 TRACE("positionT%u", semantic->usage_idx);
909 break;
911 case WINED3DDECLUSAGE_FOG:
912 TRACE("fog");
913 break;
915 case WINED3DDECLUSAGE_DEPTH:
916 TRACE("depth");
917 break;
919 case WINED3DDECLUSAGE_SAMPLE:
920 TRACE("sample");
921 break;
923 default:
924 FIXME("unknown_semantics(0x%08x)", semantic->usage);
929 static void shader_dump_register(const struct wined3d_shader_register *reg,
930 const struct wined3d_shader_version *shader_version)
932 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
933 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
934 UINT offset = reg->idx;
936 switch (reg->type)
938 case WINED3DSPR_TEMP:
939 TRACE("r");
940 break;
942 case WINED3DSPR_INPUT:
943 TRACE("v");
944 break;
946 case WINED3DSPR_CONST:
947 case WINED3DSPR_CONST2:
948 case WINED3DSPR_CONST3:
949 case WINED3DSPR_CONST4:
950 TRACE("c");
951 offset = shader_get_float_offset(reg->type, reg->idx);
952 break;
954 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
955 TRACE("%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
956 break;
958 case WINED3DSPR_RASTOUT:
959 TRACE("%s", rastout_reg_names[reg->idx]);
960 break;
962 case WINED3DSPR_COLOROUT:
963 TRACE("oC");
964 break;
966 case WINED3DSPR_DEPTHOUT:
967 TRACE("oDepth");
968 break;
970 case WINED3DSPR_ATTROUT:
971 TRACE("oD");
972 break;
974 case WINED3DSPR_TEXCRDOUT:
975 /* Vertex shaders >= 3.0 use general purpose output registers
976 * (WINED3DSPR_OUTPUT), which can include an address token. */
977 if (shader_version->major >= 3) TRACE("o");
978 else TRACE("oT");
979 break;
981 case WINED3DSPR_CONSTINT:
982 TRACE("i");
983 break;
985 case WINED3DSPR_CONSTBOOL:
986 TRACE("b");
987 break;
989 case WINED3DSPR_LABEL:
990 TRACE("l");
991 break;
993 case WINED3DSPR_LOOP:
994 TRACE("aL");
995 break;
997 case WINED3DSPR_SAMPLER:
998 TRACE("s");
999 break;
1001 case WINED3DSPR_MISCTYPE:
1002 if (reg->idx > 1) FIXME("Unhandled misctype register %u.\n", reg->idx);
1003 else TRACE("%s", misctype_reg_names[reg->idx]);
1004 break;
1006 case WINED3DSPR_PREDICATE:
1007 TRACE("p");
1008 break;
1010 case WINED3DSPR_IMMCONST:
1011 TRACE("l");
1012 break;
1014 case WINED3DSPR_CONSTBUFFER:
1015 TRACE("cb");
1016 break;
1018 case WINED3DSPR_NULL:
1019 TRACE("null");
1020 break;
1022 case WINED3DSPR_RESOURCE:
1023 TRACE("t");
1024 break;
1026 default:
1027 TRACE("unhandled_rtype(%#x)", reg->type);
1028 break;
1031 if (reg->type == WINED3DSPR_IMMCONST)
1033 TRACE("(");
1034 switch (reg->immconst_type)
1036 case WINED3D_IMMCONST_SCALAR:
1037 TRACE("%.8e", *(const float *)reg->immconst_data);
1038 break;
1040 case WINED3D_IMMCONST_VEC4:
1041 TRACE("%.8e, %.8e, %.8e, %.8e",
1042 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
1043 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
1044 break;
1046 default:
1047 TRACE("<unhandled immconst_type %#x>", reg->immconst_type);
1048 break;
1050 TRACE(")");
1052 else if (reg->type != WINED3DSPR_RASTOUT
1053 && reg->type != WINED3DSPR_MISCTYPE
1054 && reg->type != WINED3DSPR_NULL)
1056 if (reg->array_idx != ~0U)
1058 TRACE("%u[%u", offset, reg->array_idx);
1059 if (reg->rel_addr)
1061 TRACE(" + ");
1062 shader_dump_src_param(reg->rel_addr, shader_version);
1064 TRACE("]");
1066 else
1068 if (reg->rel_addr)
1070 TRACE("[");
1071 shader_dump_src_param(reg->rel_addr, shader_version);
1072 TRACE(" + ");
1074 TRACE("%u", offset);
1075 if (reg->rel_addr) TRACE("]");
1080 void shader_dump_dst_param(const struct wined3d_shader_dst_param *param,
1081 const struct wined3d_shader_version *shader_version)
1083 DWORD write_mask = param->write_mask;
1085 shader_dump_register(&param->reg, shader_version);
1087 if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
1089 static const char *write_mask_chars = "xyzw";
1091 TRACE(".");
1092 if (write_mask & WINED3DSP_WRITEMASK_0) TRACE("%c", write_mask_chars[0]);
1093 if (write_mask & WINED3DSP_WRITEMASK_1) TRACE("%c", write_mask_chars[1]);
1094 if (write_mask & WINED3DSP_WRITEMASK_2) TRACE("%c", write_mask_chars[2]);
1095 if (write_mask & WINED3DSP_WRITEMASK_3) TRACE("%c", write_mask_chars[3]);
1099 void shader_dump_src_param(const struct wined3d_shader_src_param *param,
1100 const struct wined3d_shader_version *shader_version)
1102 enum wined3d_shader_src_modifier src_modifier = param->modifiers;
1103 DWORD swizzle = param->swizzle;
1105 if (src_modifier == WINED3DSPSM_NEG
1106 || src_modifier == WINED3DSPSM_BIASNEG
1107 || src_modifier == WINED3DSPSM_SIGNNEG
1108 || src_modifier == WINED3DSPSM_X2NEG
1109 || src_modifier == WINED3DSPSM_ABSNEG)
1110 TRACE("-");
1111 else if (src_modifier == WINED3DSPSM_COMP)
1112 TRACE("1-");
1113 else if (src_modifier == WINED3DSPSM_NOT)
1114 TRACE("!");
1116 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
1117 TRACE("abs(");
1119 shader_dump_register(&param->reg, shader_version);
1121 if (src_modifier)
1123 switch (src_modifier)
1125 case WINED3DSPSM_NONE: break;
1126 case WINED3DSPSM_NEG: break;
1127 case WINED3DSPSM_NOT: break;
1128 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
1129 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
1130 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
1131 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
1132 case WINED3DSPSM_COMP: break;
1133 case WINED3DSPSM_X2: TRACE("_x2"); break;
1134 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
1135 case WINED3DSPSM_DZ: TRACE("_dz"); break;
1136 case WINED3DSPSM_DW: TRACE("_dw"); break;
1137 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
1138 case WINED3DSPSM_ABS: TRACE(")"); break;
1139 default: TRACE("_unknown_modifier(%#x)", src_modifier);
1143 if (swizzle != WINED3DSP_NOSWIZZLE)
1145 static const char *swizzle_chars = "xyzw";
1146 DWORD swizzle_x = swizzle & 0x03;
1147 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1148 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1149 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1151 if (swizzle_x == swizzle_y
1152 && swizzle_x == swizzle_z
1153 && swizzle_x == swizzle_w)
1155 TRACE(".%c", swizzle_chars[swizzle_x]);
1157 else
1159 TRACE(".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
1160 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
1165 /* Shared code in order to generate the bulk of the shader string.
1166 * NOTE: A description of how to parse tokens can be found on MSDN. */
1167 void shader_generate_main(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
1168 const struct wined3d_shader_reg_maps *reg_maps, const DWORD *byte_code, void *backend_ctx)
1170 struct wined3d_device *device = shader->device;
1171 const struct wined3d_shader_frontend *fe = shader->frontend;
1172 void *fe_data = shader->frontend_data;
1173 struct wined3d_shader_src_param dst_rel_addr[2];
1174 struct wined3d_shader_src_param src_rel_addr[4];
1175 struct wined3d_shader_dst_param dst_param[2];
1176 struct wined3d_shader_src_param src_param[4];
1177 struct wined3d_shader_version shader_version;
1178 struct wined3d_shader_loop_state loop_state;
1179 struct wined3d_shader_instruction ins;
1180 struct wined3d_shader_tex_mx tex_mx;
1181 struct wined3d_shader_context ctx;
1182 const DWORD *ptr = byte_code;
1183 DWORD i;
1185 /* Initialize current parsing state. */
1186 tex_mx.current_row = 0;
1187 loop_state.current_depth = 0;
1188 loop_state.current_reg = 0;
1190 ctx.shader = shader;
1191 ctx.gl_info = &device->adapter->gl_info;
1192 ctx.reg_maps = reg_maps;
1193 ctx.buffer = buffer;
1194 ctx.tex_mx = &tex_mx;
1195 ctx.loop_state = &loop_state;
1196 ctx.backend_data = backend_ctx;
1198 ins.ctx = &ctx;
1199 ins.dst = dst_param;
1200 ins.src = src_param;
1202 fe->shader_read_header(fe_data, &ptr, &shader_version);
1204 while (!fe->shader_is_end(fe_data, &ptr))
1206 const char *comment;
1207 UINT comment_size;
1208 UINT param_size;
1210 /* Skip comment tokens. */
1211 fe->shader_read_comment(&ptr, &comment, &comment_size);
1212 if (comment) continue;
1214 /* Read opcode. */
1215 fe->shader_read_opcode(fe_data, &ptr, &ins, &param_size);
1217 /* Unknown opcode and its parameters. */
1218 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1220 TRACE("Skipping unrecognized instruction.\n");
1221 ptr += param_size;
1222 continue;
1225 /* Nothing to do. */
1226 if (ins.handler_idx == WINED3DSIH_DCL
1227 || ins.handler_idx == WINED3DSIH_NOP
1228 || ins.handler_idx == WINED3DSIH_DEF
1229 || ins.handler_idx == WINED3DSIH_DEFI
1230 || ins.handler_idx == WINED3DSIH_DEFB
1231 || ins.handler_idx == WINED3DSIH_PHASE)
1233 ptr += param_size;
1234 continue;
1237 /* Destination tokens */
1238 for (i = 0; i < ins.dst_count; ++i)
1240 fe->shader_read_dst_param(fe_data, &ptr, &dst_param[i], &dst_rel_addr[i]);
1243 /* Predication token */
1244 if (ins.predicate)
1246 FIXME("Predicates not implemented.\n");
1247 ins.predicate = *ptr++;
1250 /* Other source tokens */
1251 for (i = 0; i < ins.src_count; ++i)
1253 fe->shader_read_src_param(fe_data, &ptr, &src_param[i], &src_rel_addr[i]);
1256 /* Call appropriate function for output target */
1257 device->shader_backend->shader_handle_instruction(&ins);
1261 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param *dst)
1263 DWORD mmask = dst->modifiers;
1265 switch (dst->shift)
1267 case 0: break;
1268 case 13: TRACE("_d8"); break;
1269 case 14: TRACE("_d4"); break;
1270 case 15: TRACE("_d2"); break;
1271 case 1: TRACE("_x2"); break;
1272 case 2: TRACE("_x4"); break;
1273 case 3: TRACE("_x8"); break;
1274 default: TRACE("_unhandled_shift(%d)", dst->shift); break;
1277 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
1278 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
1279 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
1281 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
1282 if (mmask) FIXME("_unrecognized_modifier(%#x)", mmask);
1285 static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *byte_code)
1287 struct wined3d_shader_version shader_version;
1288 const DWORD *ptr = byte_code;
1289 const char *type_prefix;
1290 DWORD i;
1292 TRACE("Parsing %p.\n", byte_code);
1294 fe->shader_read_header(fe_data, &ptr, &shader_version);
1296 switch (shader_version.type)
1298 case WINED3D_SHADER_TYPE_VERTEX:
1299 type_prefix = "vs";
1300 break;
1302 case WINED3D_SHADER_TYPE_GEOMETRY:
1303 type_prefix = "gs";
1304 break;
1306 case WINED3D_SHADER_TYPE_PIXEL:
1307 type_prefix = "ps";
1308 break;
1310 default:
1311 FIXME("Unhandled shader type %#x.\n", shader_version.type);
1312 type_prefix = "unknown";
1313 break;
1316 TRACE("%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
1318 while (!fe->shader_is_end(fe_data, &ptr))
1320 struct wined3d_shader_instruction ins;
1321 const char *comment;
1322 UINT comment_size;
1323 UINT param_size;
1325 /* comment */
1326 fe->shader_read_comment(&ptr, &comment, &comment_size);
1327 if (comment)
1329 if (comment_size > 4 && *(const DWORD *)comment == WINEMAKEFOURCC('T', 'E', 'X', 'T'))
1331 const char *end = comment + comment_size;
1332 const char *ptr = comment + 4;
1333 const char *line = ptr;
1335 TRACE("// TEXT\n");
1336 while (ptr != end)
1338 if (*ptr == '\n')
1340 UINT len = ptr - line;
1341 if (len && *(ptr - 1) == '\r') --len;
1342 TRACE("// %s\n", debugstr_an(line, len));
1343 line = ++ptr;
1345 else ++ptr;
1347 if (line != ptr) TRACE("// %s\n", debugstr_an(line, ptr - line));
1349 else TRACE("// %s\n", debugstr_an(comment, comment_size));
1350 continue;
1353 fe->shader_read_opcode(fe_data, &ptr, &ins, &param_size);
1354 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1356 TRACE("Skipping unrecognized instruction.\n");
1357 ptr += param_size;
1358 continue;
1361 if (ins.handler_idx == WINED3DSIH_DCL)
1363 struct wined3d_shader_semantic semantic;
1365 fe->shader_read_semantic(&ptr, &semantic);
1367 shader_dump_decl_usage(&semantic, &shader_version);
1368 shader_dump_ins_modifiers(&semantic.reg);
1369 TRACE(" ");
1370 shader_dump_dst_param(&semantic.reg, &shader_version);
1372 else if (ins.handler_idx == WINED3DSIH_DEF)
1374 struct wined3d_shader_src_param rel_addr;
1375 struct wined3d_shader_dst_param dst;
1377 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1379 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(dst.reg.type, dst.reg.idx),
1380 *(const float *)(ptr),
1381 *(const float *)(ptr + 1),
1382 *(const float *)(ptr + 2),
1383 *(const float *)(ptr + 3));
1384 ptr += 4;
1386 else if (ins.handler_idx == WINED3DSIH_DEFI)
1388 struct wined3d_shader_src_param rel_addr;
1389 struct wined3d_shader_dst_param dst;
1391 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1393 TRACE("defi i%u = %d, %d, %d, %d", dst.reg.idx,
1394 *(ptr),
1395 *(ptr + 1),
1396 *(ptr + 2),
1397 *(ptr + 3));
1398 ptr += 4;
1400 else if (ins.handler_idx == WINED3DSIH_DEFB)
1402 struct wined3d_shader_src_param rel_addr;
1403 struct wined3d_shader_dst_param dst;
1405 fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
1407 TRACE("defb b%u = %s", dst.reg.idx, *ptr ? "true" : "false");
1408 ++ptr;
1410 else
1412 struct wined3d_shader_src_param dst_rel_addr[2];
1413 struct wined3d_shader_src_param src_rel_addr;
1414 struct wined3d_shader_dst_param dst_param[2];
1415 struct wined3d_shader_src_param src_param;
1417 for (i = 0; i < ins.dst_count; ++i)
1419 fe->shader_read_dst_param(fe_data, &ptr, &dst_param[i], &dst_rel_addr[i]);
1422 /* Print out predication source token first - it follows
1423 * the destination token. */
1424 if (ins.predicate)
1426 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
1427 TRACE("(");
1428 shader_dump_src_param(&src_param, &shader_version);
1429 TRACE(") ");
1432 /* PixWin marks instructions with the coissue flag with a '+' */
1433 if (ins.coissue) TRACE("+");
1435 TRACE("%s", shader_opcode_names[ins.handler_idx]);
1437 if (ins.handler_idx == WINED3DSIH_IFC
1438 || ins.handler_idx == WINED3DSIH_BREAKC)
1440 switch (ins.flags)
1442 case WINED3D_SHADER_REL_OP_GT: TRACE("_gt"); break;
1443 case WINED3D_SHADER_REL_OP_EQ: TRACE("_eq"); break;
1444 case WINED3D_SHADER_REL_OP_GE: TRACE("_ge"); break;
1445 case WINED3D_SHADER_REL_OP_LT: TRACE("_lt"); break;
1446 case WINED3D_SHADER_REL_OP_NE: TRACE("_ne"); break;
1447 case WINED3D_SHADER_REL_OP_LE: TRACE("_le"); break;
1448 default: TRACE("_(%u)", ins.flags);
1451 else if (ins.handler_idx == WINED3DSIH_TEX
1452 && shader_version.major >= 2
1453 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
1455 TRACE("p");
1458 /* We already read the destination tokens, print them. */
1459 for (i = 0; i < ins.dst_count; ++i)
1461 shader_dump_ins_modifiers(&dst_param[i]);
1462 TRACE(!i ? " " : ", ");
1463 shader_dump_dst_param(&dst_param[i], &shader_version);
1466 /* Other source tokens */
1467 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
1469 fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
1470 TRACE(!i ? " " : ", ");
1471 shader_dump_src_param(&src_param, &shader_version);
1474 TRACE("\n");
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 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
1493 static void shader_none_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS) {}
1494 static void shader_none_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
1495 enum tex_types tex_type, const SIZE *ds_mask_size) {}
1496 static void shader_none_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info) {}
1497 static void shader_none_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count) {}
1498 static void shader_none_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count) {}
1499 static void shader_none_load_constants(const struct wined3d_context *context, char usePS, char useVS) {}
1500 static void shader_none_load_np2fixup_constants(void *shader_priv,
1501 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state) {}
1502 static void shader_none_destroy(struct wined3d_shader *shader) {}
1503 static HRESULT shader_none_alloc(struct wined3d_device *device) {return WINED3D_OK;}
1504 static void shader_none_free(struct wined3d_device *device) {}
1505 static void shader_none_context_destroyed(void *shader_priv, const struct wined3d_context *context) {}
1507 static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
1509 /* Set the shader caps to 0 for the none shader backend */
1510 caps->VertexShaderVersion = 0;
1511 caps->MaxVertexShaderConst = 0;
1512 caps->PixelShaderVersion = 0;
1513 caps->PixelShader1xMaxValue = 0.0f;
1514 caps->MaxPixelShaderConst = 0;
1515 caps->VSClipping = FALSE;
1518 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1520 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1522 TRACE("Checking support for fixup:\n");
1523 dump_color_fixup_desc(fixup);
1526 /* Faked to make some apps happy. */
1527 if (!is_complex_fixup(fixup))
1529 TRACE("[OK]\n");
1530 return TRUE;
1533 TRACE("[FAILED]\n");
1534 return FALSE;
1537 const struct wined3d_shader_backend_ops none_shader_backend =
1539 shader_none_handle_instruction,
1540 shader_none_select,
1541 shader_none_select_depth_blt,
1542 shader_none_deselect_depth_blt,
1543 shader_none_update_float_vertex_constants,
1544 shader_none_update_float_pixel_constants,
1545 shader_none_load_constants,
1546 shader_none_load_np2fixup_constants,
1547 shader_none_destroy,
1548 shader_none_alloc,
1549 shader_none_free,
1550 shader_none_context_destroyed,
1551 shader_none_get_caps,
1552 shader_none_color_fixup_supported,
1555 static HRESULT shader_set_function(struct wined3d_shader *shader, const DWORD *byte_code,
1556 const struct wined3d_shader_signature *output_signature, DWORD float_const_count,
1557 enum wined3d_shader_type type, unsigned int max_version)
1559 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1560 const struct wined3d_shader_frontend *fe;
1561 HRESULT hr;
1562 unsigned int backend_version;
1564 TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
1565 shader, byte_code, output_signature, float_const_count);
1567 fe = shader_select_frontend(*byte_code);
1568 if (!fe)
1570 FIXME("Unable to find frontend for shader.\n");
1571 return WINED3DERR_INVALIDCALL;
1573 shader->frontend = fe;
1574 shader->frontend_data = fe->shader_init(byte_code, output_signature);
1575 if (!shader->frontend_data)
1577 FIXME("Failed to initialize frontend.\n");
1578 return WINED3DERR_INVALIDCALL;
1581 /* First pass: trace shader. */
1582 if (TRACE_ON(d3d_shader))
1583 shader_trace_init(fe, shader->frontend_data, byte_code);
1585 /* Initialize immediate constant lists. */
1586 list_init(&shader->constantsF);
1587 list_init(&shader->constantsB);
1588 list_init(&shader->constantsI);
1590 /* Second pass: figure out which registers are used, what the semantics are, etc. */
1591 hr = shader_get_registers_used(shader, fe,
1592 reg_maps, shader->input_signature, shader->output_signature,
1593 byte_code, float_const_count);
1594 if (FAILED(hr)) return hr;
1596 if (reg_maps->shader_version.type != type)
1598 WARN("Wrong shader type %d.\n", reg_maps->shader_version.type);
1599 return WINED3DERR_INVALIDCALL;
1601 if (reg_maps->shader_version.major > max_version)
1603 WARN("Shader version %d not supported by this D3D API version.\n", reg_maps->shader_version.major);
1604 return WINED3DERR_INVALIDCALL;
1606 switch (type)
1608 case WINED3D_SHADER_TYPE_VERTEX:
1609 backend_version = shader->device->vshader_version;
1610 break;
1611 case WINED3D_SHADER_TYPE_PIXEL:
1612 backend_version = shader->device->pshader_version;
1613 break;
1614 default:
1615 FIXME("No backend version-checking for this shader type\n");
1616 backend_version = 0;
1618 if (reg_maps->shader_version.major > backend_version)
1620 WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
1621 reg_maps->shader_version.major, reg_maps->shader_version.minor);
1622 return WINED3DERR_INVALIDCALL;
1625 shader->function = HeapAlloc(GetProcessHeap(), 0, shader->functionLength);
1626 if (!shader->function)
1627 return E_OUTOFMEMORY;
1628 memcpy(shader->function, byte_code, shader->functionLength);
1630 return WINED3D_OK;
1633 ULONG CDECL wined3d_shader_incref(struct wined3d_shader *shader)
1635 ULONG refcount = InterlockedIncrement(&shader->ref);
1637 TRACE("%p increasing refcount to %u.\n", shader, refcount);
1639 return refcount;
1642 /* Do not call while under the GL lock. */
1643 ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
1645 ULONG refcount = InterlockedDecrement(&shader->ref);
1647 TRACE("%p decreasing refcount to %u.\n", shader, refcount);
1649 if (!refcount)
1651 shader_cleanup(shader);
1652 shader->parent_ops->wined3d_object_destroyed(shader->parent);
1653 HeapFree(GetProcessHeap(), 0, shader);
1656 return refcount;
1659 void * CDECL wined3d_shader_get_parent(const struct wined3d_shader *shader)
1661 TRACE("shader %p.\n", shader);
1663 return shader->parent;
1666 HRESULT CDECL wined3d_shader_get_byte_code(const struct wined3d_shader *shader,
1667 void *byte_code, UINT *byte_code_size)
1669 TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader, byte_code, byte_code_size);
1671 if (!byte_code)
1673 *byte_code_size = shader->functionLength;
1674 return WINED3D_OK;
1677 if (*byte_code_size < shader->functionLength)
1679 /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
1680 * than the required size we should write the required size and
1681 * return D3DERR_MOREDATA. That's not actually true. */
1682 return WINED3DERR_INVALIDCALL;
1685 memcpy(byte_code, shader->function, shader->functionLength);
1687 return WINED3D_OK;
1690 /* Set local constants for d3d8 shaders. */
1691 HRESULT CDECL wined3d_shader_set_local_constants_float(struct wined3d_shader *shader,
1692 UINT start_idx, const float *src_data, UINT count)
1694 UINT end_idx = start_idx + count;
1695 UINT i;
1697 TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader, start_idx, src_data, count);
1699 if (end_idx > shader->limits.constant_float)
1701 WARN("end_idx %u > float constants limit %u.\n",
1702 end_idx, shader->limits.constant_float);
1703 end_idx = shader->limits.constant_float;
1706 for (i = start_idx; i < end_idx; ++i)
1708 struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1709 if (!lconst)
1710 return E_OUTOFMEMORY;
1712 lconst->idx = i;
1713 memcpy(lconst->value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
1714 list_add_head(&shader->constantsF, &lconst->entry);
1717 return WINED3D_OK;
1720 void find_vs_compile_args(const struct wined3d_state *state,
1721 const struct wined3d_shader *shader, struct vs_compile_args *args)
1723 args->fog_src = state->render_states[WINED3DRS_FOGTABLEMODE]
1724 == WINED3DFOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
1725 args->clip_enabled = state->render_states[WINED3DRS_CLIPPING]
1726 && state->render_states[WINED3DRS_CLIPPLANEENABLE];
1727 args->swizzle_map = shader->device->strided_streams.swizzle_map;
1730 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
1732 if (usage_idx1 != usage_idx2) return FALSE;
1733 if (usage1 == usage2) return TRUE;
1734 if (usage1 == WINED3DDECLUSAGE_POSITION && usage2 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
1735 if (usage2 == WINED3DDECLUSAGE_POSITION && usage1 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
1737 return FALSE;
1740 BOOL vshader_get_input(const struct wined3d_shader *shader,
1741 BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
1743 WORD map = shader->reg_maps.input_registers;
1744 unsigned int i;
1746 for (i = 0; map; map >>= 1, ++i)
1748 if (!(map & 1)) continue;
1750 if (match_usage(shader->u.vs.attributes[i].usage,
1751 shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
1753 *regnum = i;
1754 return TRUE;
1757 return FALSE;
1760 static void vertexshader_set_limits(struct wined3d_shader *shader)
1762 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
1763 shader->reg_maps.shader_version.minor);
1764 struct wined3d_device *device = shader->device;
1766 shader->limits.texcoord = 0;
1767 shader->limits.attributes = 16;
1768 shader->limits.packed_input = 0;
1770 switch (shader_version)
1772 case WINED3D_SHADER_VERSION(1, 0):
1773 case WINED3D_SHADER_VERSION(1, 1):
1774 shader->limits.temporary = 12;
1775 shader->limits.constant_bool = 0;
1776 shader->limits.constant_int = 0;
1777 shader->limits.address = 1;
1778 shader->limits.packed_output = 12;
1779 shader->limits.sampler = 0;
1780 shader->limits.label = 0;
1781 /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
1782 * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
1783 * constants? */
1784 shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1785 break;
1787 case WINED3D_SHADER_VERSION(2, 0):
1788 case WINED3D_SHADER_VERSION(2, 1):
1789 shader->limits.temporary = 12;
1790 shader->limits.constant_bool = 16;
1791 shader->limits.constant_int = 16;
1792 shader->limits.address = 1;
1793 shader->limits.packed_output = 12;
1794 shader->limits.sampler = 0;
1795 shader->limits.label = 16;
1796 shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1797 break;
1799 case WINED3D_SHADER_VERSION(4, 0):
1800 FIXME("Using 3.0 limits for 4.0 shader.\n");
1801 /* Fall through. */
1803 case WINED3D_SHADER_VERSION(3, 0):
1804 shader->limits.temporary = 32;
1805 shader->limits.constant_bool = 32;
1806 shader->limits.constant_int = 32;
1807 shader->limits.address = 1;
1808 shader->limits.packed_output = 12;
1809 shader->limits.sampler = 4;
1810 shader->limits.label = 16; /* FIXME: 2048 */
1811 /* DX10 cards on Windows advertise a d3d9 constant limit of 256
1812 * even though they are capable of supporting much more (GL
1813 * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
1814 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
1815 * shaders to 256. */
1816 shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1817 break;
1819 default:
1820 shader->limits.temporary = 12;
1821 shader->limits.constant_bool = 16;
1822 shader->limits.constant_int = 16;
1823 shader->limits.address = 1;
1824 shader->limits.packed_output = 12;
1825 shader->limits.sampler = 0;
1826 shader->limits.label = 16;
1827 shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1828 FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1829 shader->reg_maps.shader_version.major,
1830 shader->reg_maps.shader_version.minor);
1834 static HRESULT vertexshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
1835 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1836 void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
1838 struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1839 unsigned int i;
1840 HRESULT hr;
1841 WORD map;
1843 if (!byte_code) return WINED3DERR_INVALIDCALL;
1845 shader_init(shader, device, parent, parent_ops);
1846 hr = shader_set_function(shader, byte_code, output_signature, device->d3d_vshader_constantF,
1847 WINED3D_SHADER_TYPE_VERTEX, max_version);
1848 if (FAILED(hr))
1850 WARN("Failed to set function, hr %#x.\n", hr);
1851 shader_cleanup(shader);
1852 return hr;
1855 map = reg_maps->input_registers;
1856 for (i = 0; map; map >>= 1, ++i)
1858 if (!(map & 1) || !shader->input_signature[i].semantic_name)
1859 continue;
1861 shader->u.vs.attributes[i].usage =
1862 shader_usage_from_semantic_name(shader->input_signature[i].semantic_name);
1863 shader->u.vs.attributes[i].usage_idx = shader->input_signature[i].semantic_idx;
1866 if (output_signature)
1868 for (i = 0; i < output_signature->element_count; ++i)
1870 struct wined3d_shader_signature_element *e = &output_signature->elements[i];
1871 reg_maps->output_registers |= 1 << e->register_idx;
1872 shader->output_signature[e->register_idx] = *e;
1876 vertexshader_set_limits(shader);
1878 shader->load_local_constsF = reg_maps->usesrelconstF
1879 && !list_empty(&shader->constantsF);
1881 return WINED3D_OK;
1884 static HRESULT geometryshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
1885 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1886 void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
1888 HRESULT hr;
1890 shader_init(shader, device, parent, parent_ops);
1891 hr = shader_set_function(shader, byte_code, output_signature, 0,
1892 WINED3D_SHADER_TYPE_GEOMETRY, max_version);
1893 if (FAILED(hr))
1895 WARN("Failed to set function, hr %#x.\n", hr);
1896 shader_cleanup(shader);
1897 return hr;
1900 shader->load_local_constsF = FALSE;
1902 return WINED3D_OK;
1905 void find_ps_compile_args(const struct wined3d_state *state,
1906 const struct wined3d_shader *shader, struct ps_compile_args *args)
1908 struct wined3d_device *device = shader->device;
1909 const struct wined3d_texture *texture;
1910 UINT i;
1912 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
1913 if (state->render_states[WINED3DRS_SRGBWRITEENABLE])
1915 const struct wined3d_surface *rt = state->fb->render_targets[0];
1916 if (rt->resource.format->flags & WINED3DFMT_FLAG_SRGB_WRITE) args->srgb_correction = 1;
1919 if (shader->reg_maps.shader_version.major == 1
1920 && shader->reg_maps.shader_version.minor <= 3)
1922 for (i = 0; i < 4; ++i)
1924 DWORD flags = state->texture_states[i][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1925 DWORD tex_transform = flags & ~WINED3DTTFF_PROJECTED;
1926 if (flags & WINED3DTTFF_PROJECTED)
1927 tex_transform |= WINED3D_PSARGS_PROJECTED;
1928 args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
1932 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
1934 if (!shader->reg_maps.sampler_type[i])
1935 continue;
1937 texture = state->textures[i];
1938 if (!texture)
1940 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
1941 continue;
1943 args->color_fixup[i] = texture->resource.format->color_fixup;
1945 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
1946 args->shadow |= 1 << i;
1948 /* Flag samplers that need NP2 texcoord fixup. */
1949 if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
1950 args->np2_fixup |= (1 << i);
1952 if (shader->reg_maps.shader_version.major >= 3)
1954 if (device->strided_streams.position_transformed)
1956 args->vp_mode = pretransformed;
1958 else if (use_vs(state))
1960 args->vp_mode = vertexshader;
1962 else
1964 args->vp_mode = fixedfunction;
1966 args->fog = FOG_OFF;
1968 else
1970 args->vp_mode = vertexshader;
1971 if (state->render_states[WINED3DRS_FOGENABLE])
1973 switch (state->render_states[WINED3DRS_FOGTABLEMODE])
1975 case WINED3DFOG_NONE:
1976 if (device->strided_streams.position_transformed || use_vs(state))
1978 args->fog = FOG_LINEAR;
1979 break;
1982 switch (state->render_states[WINED3DRS_FOGVERTEXMODE])
1984 case WINED3DFOG_NONE: /* Fall through. */
1985 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
1986 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
1987 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
1989 break;
1991 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
1992 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
1993 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
1996 else
1998 args->fog = FOG_OFF;
2003 static void pixelshader_set_limits(struct wined3d_shader *shader)
2005 DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
2006 shader->reg_maps.shader_version.minor);
2008 shader->limits.attributes = 0;
2009 shader->limits.address = 0;
2010 shader->limits.packed_output = 0;
2012 switch (shader_version)
2014 case WINED3D_SHADER_VERSION(1, 0):
2015 case WINED3D_SHADER_VERSION(1, 1):
2016 case WINED3D_SHADER_VERSION(1, 2):
2017 case WINED3D_SHADER_VERSION(1, 3):
2018 shader->limits.temporary = 2;
2019 shader->limits.constant_float = 8;
2020 shader->limits.constant_int = 0;
2021 shader->limits.constant_bool = 0;
2022 shader->limits.texcoord = 4;
2023 shader->limits.sampler = 4;
2024 shader->limits.packed_input = 0;
2025 shader->limits.label = 0;
2026 break;
2028 case WINED3D_SHADER_VERSION(1, 4):
2029 shader->limits.temporary = 6;
2030 shader->limits.constant_float = 8;
2031 shader->limits.constant_int = 0;
2032 shader->limits.constant_bool = 0;
2033 shader->limits.texcoord = 6;
2034 shader->limits.sampler = 6;
2035 shader->limits.packed_input = 0;
2036 shader->limits.label = 0;
2037 break;
2039 /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
2040 case WINED3D_SHADER_VERSION(2, 0):
2041 shader->limits.temporary = 32;
2042 shader->limits.constant_float = 32;
2043 shader->limits.constant_int = 16;
2044 shader->limits.constant_bool = 16;
2045 shader->limits.texcoord = 8;
2046 shader->limits.sampler = 16;
2047 shader->limits.packed_input = 0;
2048 break;
2050 case WINED3D_SHADER_VERSION(2, 1):
2051 shader->limits.temporary = 32;
2052 shader->limits.constant_float = 32;
2053 shader->limits.constant_int = 16;
2054 shader->limits.constant_bool = 16;
2055 shader->limits.texcoord = 8;
2056 shader->limits.sampler = 16;
2057 shader->limits.packed_input = 0;
2058 shader->limits.label = 16;
2059 break;
2061 case WINED3D_SHADER_VERSION(4, 0):
2062 FIXME("Using 3.0 limits for 4.0 shader.\n");
2063 /* Fall through. */
2065 case WINED3D_SHADER_VERSION(3, 0):
2066 shader->limits.temporary = 32;
2067 shader->limits.constant_float = 224;
2068 shader->limits.constant_int = 16;
2069 shader->limits.constant_bool = 16;
2070 shader->limits.texcoord = 0;
2071 shader->limits.sampler = 16;
2072 shader->limits.packed_input = 12;
2073 shader->limits.label = 16; /* FIXME: 2048 */
2074 break;
2076 default:
2077 shader->limits.temporary = 32;
2078 shader->limits.constant_float = 32;
2079 shader->limits.constant_int = 16;
2080 shader->limits.constant_bool = 16;
2081 shader->limits.texcoord = 8;
2082 shader->limits.sampler = 16;
2083 shader->limits.packed_input = 0;
2084 shader->limits.label = 0;
2085 FIXME("Unrecognized pixel shader version %u.%u\n",
2086 shader->reg_maps.shader_version.major,
2087 shader->reg_maps.shader_version.minor);
2091 static HRESULT pixelshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2092 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
2093 void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
2095 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2096 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
2097 HRESULT hr;
2099 if (!byte_code) return WINED3DERR_INVALIDCALL;
2101 shader_init(shader, device, parent, parent_ops);
2102 hr = shader_set_function(shader, byte_code, output_signature, device->d3d_pshader_constantF,
2103 WINED3D_SHADER_TYPE_PIXEL, max_version);
2104 if (FAILED(hr))
2106 WARN("Failed to set function, hr %#x.\n", hr);
2107 shader_cleanup(shader);
2108 return hr;
2111 pixelshader_set_limits(shader);
2113 for (i = 0; i < MAX_REG_INPUT; ++i)
2115 if (shader->u.ps.input_reg_used[i])
2117 ++num_regs_used;
2118 highest_reg_used = i;
2122 /* Don't do any register mapping magic if it is not needed, or if we can't
2123 * achieve anything anyway */
2124 if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
2125 || num_regs_used > (gl_info->limits.glsl_varyings / 4))
2127 if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
2129 /* This happens with relative addressing. The input mapper function
2130 * warns about this if the higher registers are declared too, so
2131 * don't write a FIXME here */
2132 WARN("More varying registers used than supported\n");
2135 for (i = 0; i < MAX_REG_INPUT; ++i)
2137 shader->u.ps.input_reg_map[i] = i;
2140 shader->u.ps.declared_in_count = highest_reg_used + 1;
2142 else
2144 shader->u.ps.declared_in_count = 0;
2145 for (i = 0; i < MAX_REG_INPUT; ++i)
2147 if (shader->u.ps.input_reg_used[i])
2148 shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
2149 else shader->u.ps.input_reg_map[i] = ~0U;
2153 shader->load_local_constsF = FALSE;
2155 return WINED3D_OK;
2158 void pixelshader_update_samplers(struct wined3d_shader_reg_maps *reg_maps, struct wined3d_texture * const *textures)
2160 enum wined3d_sampler_texture_type *sampler_type = reg_maps->sampler_type;
2161 unsigned int i;
2163 if (reg_maps->shader_version.major != 1) return;
2165 for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
2167 /* We don't sample from this sampler. */
2168 if (!sampler_type[i]) continue;
2170 if (!textures[i])
2172 WARN("No texture bound to sampler %u, using 2D.\n", i);
2173 sampler_type[i] = WINED3DSTT_2D;
2174 continue;
2177 switch (textures[i]->target)
2179 case GL_TEXTURE_RECTANGLE_ARB:
2180 case GL_TEXTURE_2D:
2181 /* We have to select between texture rectangles and 2D
2182 * textures later because 2.0 and 3.0 shaders only have
2183 * WINED3DSTT_2D as well. */
2184 sampler_type[i] = WINED3DSTT_2D;
2185 break;
2187 case GL_TEXTURE_3D:
2188 sampler_type[i] = WINED3DSTT_VOLUME;
2189 break;
2191 case GL_TEXTURE_CUBE_MAP_ARB:
2192 sampler_type[i] = WINED3DSTT_CUBE;
2193 break;
2195 default:
2196 FIXME("Unrecognized texture type %#x, using 2D.\n", textures[i]->target);
2197 sampler_type[i] = WINED3DSTT_2D;
2202 HRESULT CDECL wined3d_shader_create_gs(struct wined3d_device *device, const DWORD *byte_code,
2203 const struct wined3d_shader_signature *output_signature, void *parent,
2204 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2206 struct wined3d_shader *object;
2207 HRESULT hr;
2209 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2210 device, byte_code, output_signature, parent, parent_ops, shader);
2212 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2213 if (!object)
2215 ERR("Failed to allocate shader memory.\n");
2216 return E_OUTOFMEMORY;
2219 hr = geometryshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2220 if (FAILED(hr))
2222 WARN("Failed to initialize geometry shader, hr %#x.\n", hr);
2223 HeapFree(GetProcessHeap(), 0, object);
2224 return hr;
2227 TRACE("Created geometry shader %p.\n", object);
2228 *shader = object;
2230 return WINED3D_OK;
2233 HRESULT CDECL wined3d_shader_create_ps(struct wined3d_device *device, const DWORD *byte_code,
2234 const struct wined3d_shader_signature *output_signature, void *parent,
2235 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2237 struct wined3d_shader *object;
2238 HRESULT hr;
2240 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2241 device, byte_code, output_signature, parent, parent_ops, shader);
2243 if (device->ps_selected_mode == SHADER_NONE)
2244 return WINED3DERR_INVALIDCALL;
2246 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2247 if (!object)
2249 ERR("Failed to allocate shader memory.\n");
2250 return E_OUTOFMEMORY;
2253 hr = pixelshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2254 if (FAILED(hr))
2256 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2257 HeapFree(GetProcessHeap(), 0, object);
2258 return hr;
2261 TRACE("Created pixel shader %p.\n", object);
2262 *shader = object;
2264 return WINED3D_OK;
2267 HRESULT CDECL wined3d_shader_create_vs(struct wined3d_device *device, const DWORD *byte_code,
2268 const struct wined3d_shader_signature *output_signature, void *parent,
2269 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2271 struct wined3d_shader *object;
2272 HRESULT hr;
2274 TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
2275 device, byte_code, output_signature, parent, parent_ops, shader);
2277 if (device->vs_selected_mode == SHADER_NONE)
2278 return WINED3DERR_INVALIDCALL;
2280 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2281 if (!object)
2283 ERR("Failed to allocate shader memory.\n");
2284 return E_OUTOFMEMORY;
2287 hr = vertexshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2288 if (FAILED(hr))
2290 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2291 HeapFree(GetProcessHeap(), 0, object);
2292 return hr;
2295 TRACE("Created vertex shader %p.\n", object);
2296 *shader = object;
2298 return WINED3D_OK;