- Merge from trunk up to r45543
[reactos.git] / dll / directx / wine / wined3d / baseshader.c
1 /*
2 * shaders implementation
3 *
4 * Copyright 2002-2003 Jason Edmeades
5 * Copyright 2002-2003 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2006 Ivan Gyurdiev
9 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
10 * Copyright 2009 Henri Verbeet for CodeWeavers
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27 #include "config.h"
28 #include <string.h>
29 #include <stdio.h>
30 #include "wined3d_private.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
33 WINE_DECLARE_DEBUG_CHANNEL(d3d);
34
35 static const char *shader_opcode_names[] =
36 {
37 /* WINED3DSIH_ABS */ "abs",
38 /* WINED3DSIH_ADD */ "add",
39 /* WINED3DSIH_BEM */ "bem",
40 /* WINED3DSIH_BREAK */ "break",
41 /* WINED3DSIH_BREAKC */ "breakc",
42 /* WINED3DSIH_BREAKP */ "breakp",
43 /* WINED3DSIH_CALL */ "call",
44 /* WINED3DSIH_CALLNZ */ "callnz",
45 /* WINED3DSIH_CMP */ "cmp",
46 /* WINED3DSIH_CND */ "cnd",
47 /* WINED3DSIH_CRS */ "crs",
48 /* WINED3DSIH_DCL */ "dcl",
49 /* WINED3DSIH_DEF */ "def",
50 /* WINED3DSIH_DEFB */ "defb",
51 /* WINED3DSIH_DEFI */ "defi",
52 /* WINED3DSIH_DP2ADD */ "dp2add",
53 /* WINED3DSIH_DP3 */ "dp3",
54 /* WINED3DSIH_DP4 */ "dp4",
55 /* WINED3DSIH_DST */ "dst",
56 /* WINED3DSIH_DSX */ "dsx",
57 /* WINED3DSIH_DSY */ "dsy",
58 /* WINED3DSIH_ELSE */ "else",
59 /* WINED3DSIH_ENDIF */ "endif",
60 /* WINED3DSIH_ENDLOOP */ "endloop",
61 /* WINED3DSIH_ENDREP */ "endrep",
62 /* WINED3DSIH_EXP */ "exp",
63 /* WINED3DSIH_EXPP */ "expp",
64 /* WINED3DSIH_FRC */ "frc",
65 /* WINED3DSIH_IF */ "if",
66 /* WINED3DSIH_IFC */ "ifc",
67 /* WINED3DSIH_LABEL */ "label",
68 /* WINED3DSIH_LIT */ "lit",
69 /* WINED3DSIH_LOG */ "log",
70 /* WINED3DSIH_LOGP */ "logp",
71 /* WINED3DSIH_LOOP */ "loop",
72 /* WINED3DSIH_LRP */ "lrp",
73 /* WINED3DSIH_M3x2 */ "m3x2",
74 /* WINED3DSIH_M3x3 */ "m3x3",
75 /* WINED3DSIH_M3x4 */ "m3x4",
76 /* WINED3DSIH_M4x3 */ "m4x3",
77 /* WINED3DSIH_M4x4 */ "m4x4",
78 /* WINED3DSIH_MAD */ "mad",
79 /* WINED3DSIH_MAX */ "max",
80 /* WINED3DSIH_MIN */ "min",
81 /* WINED3DSIH_MOV */ "mov",
82 /* WINED3DSIH_MOVA */ "mova",
83 /* WINED3DSIH_MUL */ "mul",
84 /* WINED3DSIH_NOP */ "nop",
85 /* WINED3DSIH_NRM */ "nrm",
86 /* WINED3DSIH_PHASE */ "phase",
87 /* WINED3DSIH_POW */ "pow",
88 /* WINED3DSIH_RCP */ "rcp",
89 /* WINED3DSIH_REP */ "rep",
90 /* WINED3DSIH_RET */ "ret",
91 /* WINED3DSIH_RSQ */ "rsq",
92 /* WINED3DSIH_SETP */ "setp",
93 /* WINED3DSIH_SGE */ "sge",
94 /* WINED3DSIH_SGN */ "sgn",
95 /* WINED3DSIH_SINCOS */ "sincos",
96 /* WINED3DSIH_SLT */ "slt",
97 /* WINED3DSIH_SUB */ "sub",
98 /* WINED3DSIH_TEX */ "texld",
99 /* WINED3DSIH_TEXBEM */ "texbem",
100 /* WINED3DSIH_TEXBEML */ "texbeml",
101 /* WINED3DSIH_TEXCOORD */ "texcrd",
102 /* WINED3DSIH_TEXDEPTH */ "texdepth",
103 /* WINED3DSIH_TEXDP3 */ "texdp3",
104 /* WINED3DSIH_TEXDP3TEX */ "texdp3tex",
105 /* WINED3DSIH_TEXKILL */ "texkill",
106 /* WINED3DSIH_TEXLDD */ "texldd",
107 /* WINED3DSIH_TEXLDL */ "texldl",
108 /* WINED3DSIH_TEXM3x2DEPTH */ "texm3x2depth",
109 /* WINED3DSIH_TEXM3x2PAD */ "texm3x2pad",
110 /* WINED3DSIH_TEXM3x2TEX */ "texm3x2tex",
111 /* WINED3DSIH_TEXM3x3 */ "texm3x3",
112 /* WINED3DSIH_TEXM3x3DIFF */ "texm3x3diff",
113 /* WINED3DSIH_TEXM3x3PAD */ "texm3x3pad",
114 /* WINED3DSIH_TEXM3x3SPEC */ "texm3x3spec",
115 /* WINED3DSIH_TEXM3x3TEX */ "texm3x3tex",
116 /* WINED3DSIH_TEXM3x3VSPEC */ "texm3x3vspec",
117 /* WINED3DSIH_TEXREG2AR */ "texreg2ar",
118 /* WINED3DSIH_TEXREG2GB */ "texreg2gb",
119 /* WINED3DSIH_TEXREG2RGB */ "texreg2rgb",
120 };
121
122 const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token)
123 {
124 switch (version_token >> 16)
125 {
126 case WINED3D_SM1_VS:
127 case WINED3D_SM1_PS:
128 return &sm1_shader_frontend;
129
130 case WINED3D_SM4_PS:
131 case WINED3D_SM4_VS:
132 case WINED3D_SM4_GS:
133 return &sm4_shader_frontend;
134
135 default:
136 FIXME("Unrecognised version token %#x\n", version_token);
137 return NULL;
138 }
139 }
140
141 void shader_buffer_clear(struct wined3d_shader_buffer *buffer)
142 {
143 buffer->buffer[0] = '\0';
144 buffer->bsize = 0;
145 buffer->lineNo = 0;
146 buffer->newline = TRUE;
147 }
148
149 BOOL shader_buffer_init(struct wined3d_shader_buffer *buffer)
150 {
151 buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
152 if (!buffer->buffer)
153 {
154 ERR("Failed to allocate shader buffer memory.\n");
155 return FALSE;
156 }
157
158 shader_buffer_clear(buffer);
159 return TRUE;
160 }
161
162 void shader_buffer_free(struct wined3d_shader_buffer *buffer)
163 {
164 HeapFree(GetProcessHeap(), 0, buffer->buffer);
165 }
166
167 int shader_vaddline(struct wined3d_shader_buffer *buffer, const char *format, va_list args)
168 {
169 char* base = buffer->buffer + buffer->bsize;
170 int rc;
171
172 rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
173
174 if (rc < 0 /* C89 */ || (unsigned int)rc > SHADER_PGMSIZE - 1 - buffer->bsize /* C99 */)
175 {
176 ERR("The buffer allocated for the shader program string "
177 "is too small at %d bytes.\n", SHADER_PGMSIZE);
178 buffer->bsize = SHADER_PGMSIZE - 1;
179 return -1;
180 }
181
182 if (buffer->newline) {
183 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
184 buffer->newline = FALSE;
185 } else {
186 TRACE("%s", base);
187 }
188
189 buffer->bsize += rc;
190 if (buffer->buffer[buffer->bsize-1] == '\n') {
191 buffer->lineNo++;
192 buffer->newline = TRUE;
193 }
194 return 0;
195 }
196
197 int shader_addline(struct wined3d_shader_buffer *buffer, const char *format, ...)
198 {
199 int ret;
200 va_list args;
201
202 va_start(args, format);
203 ret = shader_vaddline(buffer, format, args);
204 va_end(args);
205
206 return ret;
207 }
208
209 void shader_init(struct IWineD3DBaseShaderClass *shader, IWineD3DDeviceImpl *device,
210 IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
211 {
212 shader->ref = 1;
213 shader->device = (IWineD3DDevice *)device;
214 shader->parent = parent;
215 shader->parent_ops = parent_ops;
216 list_init(&shader->linked_programs);
217 list_add_head(&device->shaders, &shader->shader_list_entry);
218 }
219
220 /* Convert floating point offset relative
221 * to a register file to an absolute offset for float constants */
222 static unsigned int shader_get_float_offset(WINED3DSHADER_PARAM_REGISTER_TYPE register_type, UINT register_idx)
223 {
224 switch (register_type)
225 {
226 case WINED3DSPR_CONST: return register_idx;
227 case WINED3DSPR_CONST2: return 2048 + register_idx;
228 case WINED3DSPR_CONST3: return 4096 + register_idx;
229 case WINED3DSPR_CONST4: return 6144 + register_idx;
230 default:
231 FIXME("Unsupported register type: %d\n", register_type);
232 return register_idx;
233 }
234 }
235
236 static void shader_delete_constant_list(struct list* clist) {
237
238 struct list *ptr;
239 struct local_constant* constant;
240
241 ptr = list_head(clist);
242 while (ptr) {
243 constant = LIST_ENTRY(ptr, struct local_constant, entry);
244 ptr = list_next(clist, ptr);
245 HeapFree(GetProcessHeap(), 0, constant);
246 }
247 list_init(clist);
248 }
249
250 static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
251 {
252 DWORD idx, shift;
253 idx = bit >> 5;
254 shift = bit & 0x1f;
255 bitmap[idx] |= (1 << shift);
256 }
257
258 static void shader_record_register_usage(IWineD3DBaseShaderImpl *This, struct shader_reg_maps *reg_maps,
259 const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type)
260 {
261 switch (reg->type)
262 {
263 case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
264 if (shader_type == WINED3D_SHADER_TYPE_PIXEL) reg_maps->texcoord |= 1 << reg->idx;
265 else reg_maps->address |= 1 << reg->idx;
266 break;
267
268 case WINED3DSPR_TEMP:
269 reg_maps->temporary |= 1 << reg->idx;
270 break;
271
272 case WINED3DSPR_INPUT:
273 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
274 {
275 if (reg->rel_addr)
276 {
277 /* If relative addressing is used, we must assume that all registers
278 * are used. Even if it is a construct like v3[aL], we can't assume
279 * that v0, v1 and v2 aren't read because aL can be negative */
280 unsigned int i;
281 for (i = 0; i < MAX_REG_INPUT; ++i)
282 {
283 ((IWineD3DPixelShaderImpl *)This)->input_reg_used[i] = TRUE;
284 }
285 }
286 else
287 {
288 ((IWineD3DPixelShaderImpl *)This)->input_reg_used[reg->idx] = TRUE;
289 }
290 }
291 else reg_maps->input_registers |= 1 << reg->idx;
292 break;
293
294 case WINED3DSPR_RASTOUT:
295 if (reg->idx == 1) reg_maps->fog = 1;
296 break;
297
298 case WINED3DSPR_MISCTYPE:
299 if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
300 {
301 if (reg->idx == 0) reg_maps->vpos = 1;
302 else if (reg->idx == 1) reg_maps->usesfacing = 1;
303 }
304 break;
305
306 case WINED3DSPR_CONST:
307 if (reg->rel_addr)
308 {
309 if (shader_type != WINED3D_SHADER_TYPE_PIXEL)
310 {
311 if (reg->idx < ((IWineD3DVertexShaderImpl *)This)->min_rel_offset)
312 {
313 ((IWineD3DVertexShaderImpl *)This)->min_rel_offset = reg->idx;
314 }
315 if (reg->idx > ((IWineD3DVertexShaderImpl *)This)->max_rel_offset)
316 {
317 ((IWineD3DVertexShaderImpl *)This)->max_rel_offset = reg->idx;
318 }
319 }
320 reg_maps->usesrelconstF = TRUE;
321 }
322 else
323 {
324 set_bitmap_bit(reg_maps->constf, reg->idx);
325 }
326 break;
327
328 case WINED3DSPR_CONSTINT:
329 reg_maps->integer_constants |= (1 << reg->idx);
330 break;
331
332 case WINED3DSPR_CONSTBOOL:
333 reg_maps->boolean_constants |= (1 << reg->idx);
334 break;
335
336 case WINED3DSPR_COLOROUT:
337 reg_maps->highest_render_target = max(reg_maps->highest_render_target, reg->idx);
338 break;
339
340 default:
341 TRACE("Not recording register of type %#x and idx %u\n", reg->type, reg->idx);
342 break;
343 }
344 }
345
346 static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
347 {
348 switch(instr)
349 {
350 case WINED3DSIH_M4x4:
351 case WINED3DSIH_M3x4:
352 return param == 1 ? 3 : 0;
353
354 case WINED3DSIH_M4x3:
355 case WINED3DSIH_M3x3:
356 return param == 1 ? 2 : 0;
357
358 case WINED3DSIH_M3x2:
359 return param == 1 ? 1 : 0;
360
361 default:
362 return 0;
363 }
364 }
365
366 static const char *semantic_names[] =
367 {
368 /* WINED3DDECLUSAGE_POSITION */ "SV_POSITION",
369 /* WINED3DDECLUSAGE_BLENDWEIGHT */ "BLENDWEIGHT",
370 /* WINED3DDECLUSAGE_BLENDINDICES */ "BLENDINDICES",
371 /* WINED3DDECLUSAGE_NORMAL */ "NORMAL",
372 /* WINED3DDECLUSAGE_PSIZE */ "PSIZE",
373 /* WINED3DDECLUSAGE_TEXCOORD */ "TEXCOORD",
374 /* WINED3DDECLUSAGE_TANGENT */ "TANGENT",
375 /* WINED3DDECLUSAGE_BINORMAL */ "BINORMAL",
376 /* WINED3DDECLUSAGE_TESSFACTOR */ "TESSFACTOR",
377 /* WINED3DDECLUSAGE_POSITIONT */ "POSITIONT",
378 /* WINED3DDECLUSAGE_COLOR */ "COLOR",
379 /* WINED3DDECLUSAGE_FOG */ "FOG",
380 /* WINED3DDECLUSAGE_DEPTH */ "DEPTH",
381 /* WINED3DDECLUSAGE_SAMPLE */ "SAMPLE",
382 };
383
384 static const char *shader_semantic_name_from_usage(WINED3DDECLUSAGE usage)
385 {
386 if (usage >= sizeof(semantic_names) / sizeof(*semantic_names))
387 {
388 FIXME("Unrecognized usage %#x\n", usage);
389 return "UNRECOGNIZED";
390 }
391
392 return semantic_names[usage];
393 }
394
395 WINED3DDECLUSAGE shader_usage_from_semantic_name(const char *name)
396 {
397 unsigned int i;
398
399 for (i = 0; i < sizeof(semantic_names) / sizeof(*semantic_names); ++i)
400 {
401 if (!strcmp(name, semantic_names[i])) return i;
402 }
403
404 return ~0U;
405 }
406
407 BOOL shader_match_semantic(const char *semantic_name, WINED3DDECLUSAGE usage)
408 {
409 return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
410 }
411
412 static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
413 const struct wined3d_shader_semantic *s)
414 {
415 e->semantic_name = shader_semantic_name_from_usage(s->usage);
416 e->semantic_idx = s->usage_idx;
417 e->sysval_semantic = 0;
418 e->component_type = 0;
419 e->register_idx = s->reg.reg.idx;
420 e->mask = s->reg.write_mask;
421 }
422
423 /* Note that this does not count the loop register
424 * as an address register. */
425
426 HRESULT shader_get_registers_used(IWineD3DBaseShader *iface, const struct wined3d_shader_frontend *fe,
427 struct shader_reg_maps *reg_maps, struct wined3d_shader_signature_element *input_signature,
428 struct wined3d_shader_signature_element *output_signature, const DWORD *byte_code, DWORD constf_size)
429 {
430 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
431 void *fe_data = This->baseShader.frontend_data;
432 struct wined3d_shader_version shader_version;
433 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
434 const DWORD* pToken = byte_code;
435
436 /* There are some minor differences between pixel and vertex shaders */
437
438 memset(reg_maps, 0, sizeof(*reg_maps));
439
440 /* get_registers_used is called on every compile on some 1.x shaders, which can result
441 * in stacking up a collection of local constants. Delete the old constants if existing
442 */
443 shader_delete_constant_list(&This->baseShader.constantsF);
444 shader_delete_constant_list(&This->baseShader.constantsB);
445 shader_delete_constant_list(&This->baseShader.constantsI);
446
447 fe->shader_read_header(fe_data, &pToken, &shader_version);
448 reg_maps->shader_version = shader_version;
449
450 reg_maps->constf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
451 sizeof(*reg_maps->constf) * ((constf_size + 31) / 32));
452 if(!reg_maps->constf) {
453 ERR("Out of memory\n");
454 return E_OUTOFMEMORY;
455 }
456
457 while (!fe->shader_is_end(fe_data, &pToken))
458 {
459 struct wined3d_shader_instruction ins;
460 const char *comment;
461 UINT param_size;
462
463 /* Skip comments */
464 fe->shader_read_comment(&pToken, &comment);
465 if (comment) continue;
466
467 /* Fetch opcode */
468 fe->shader_read_opcode(fe_data, &pToken, &ins, &param_size);
469
470 /* Unhandled opcode, and its parameters */
471 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
472 {
473 TRACE("Skipping unrecognized instruction.\n");
474 pToken += param_size;
475 continue;
476 }
477
478 /* Handle declarations */
479 if (ins.handler_idx == WINED3DSIH_DCL)
480 {
481 struct wined3d_shader_semantic semantic;
482
483 fe->shader_read_semantic(&pToken, &semantic);
484
485 switch (semantic.reg.reg.type)
486 {
487 /* Mark input registers used. */
488 case WINED3DSPR_INPUT:
489 reg_maps->input_registers |= 1 << semantic.reg.reg.idx;
490 shader_signature_from_semantic(&input_signature[semantic.reg.reg.idx], &semantic);
491 break;
492
493 /* Vshader: mark 3.0 output registers used, save token */
494 case WINED3DSPR_OUTPUT:
495 reg_maps->output_registers |= 1 << semantic.reg.reg.idx;
496 shader_signature_from_semantic(&output_signature[semantic.reg.reg.idx], &semantic);
497 if (semantic.usage == WINED3DDECLUSAGE_FOG) reg_maps->fog = 1;
498 break;
499
500 /* Save sampler usage token */
501 case WINED3DSPR_SAMPLER:
502 reg_maps->sampler_type[semantic.reg.reg.idx] = semantic.sampler_type;
503 break;
504
505 default:
506 TRACE("Not recording DCL register type %#x.\n", semantic.reg.reg.type);
507 break;
508 }
509 }
510 else if (ins.handler_idx == WINED3DSIH_DEF)
511 {
512 struct wined3d_shader_dst_param dst;
513 struct wined3d_shader_src_param rel_addr;
514
515 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
516 if (!lconst) return E_OUTOFMEMORY;
517
518 fe->shader_read_dst_param(fe_data, &pToken, &dst, &rel_addr);
519 lconst->idx = dst.reg.idx;
520
521 memcpy(lconst->value, pToken, 4 * sizeof(DWORD));
522 pToken += 4;
523
524 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
525 if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
526 {
527 float *value = (float *) lconst->value;
528 if (value[0] < -1.0f) value[0] = -1.0f;
529 else if (value[0] > 1.0f) value[0] = 1.0f;
530 if (value[1] < -1.0f) value[1] = -1.0f;
531 else if (value[1] > 1.0f) value[1] = 1.0f;
532 if (value[2] < -1.0f) value[2] = -1.0f;
533 else if (value[2] > 1.0f) value[2] = 1.0f;
534 if (value[3] < -1.0f) value[3] = -1.0f;
535 else if (value[3] > 1.0f) value[3] = 1.0f;
536 }
537
538 list_add_head(&This->baseShader.constantsF, &lconst->entry);
539 }
540 else if (ins.handler_idx == WINED3DSIH_DEFI)
541 {
542 struct wined3d_shader_dst_param dst;
543 struct wined3d_shader_src_param rel_addr;
544
545 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
546 if (!lconst) return E_OUTOFMEMORY;
547
548 fe->shader_read_dst_param(fe_data, &pToken, &dst, &rel_addr);
549 lconst->idx = dst.reg.idx;
550
551 memcpy(lconst->value, pToken, 4 * sizeof(DWORD));
552 pToken += 4;
553
554 list_add_head(&This->baseShader.constantsI, &lconst->entry);
555 reg_maps->local_int_consts |= (1 << dst.reg.idx);
556 }
557 else if (ins.handler_idx == WINED3DSIH_DEFB)
558 {
559 struct wined3d_shader_dst_param dst;
560 struct wined3d_shader_src_param rel_addr;
561
562 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
563 if (!lconst) return E_OUTOFMEMORY;
564
565 fe->shader_read_dst_param(fe_data, &pToken, &dst, &rel_addr);
566 lconst->idx = dst.reg.idx;
567
568 memcpy(lconst->value, pToken, sizeof(DWORD));
569 ++pToken;
570
571 list_add_head(&This->baseShader.constantsB, &lconst->entry);
572 reg_maps->local_bool_consts |= (1 << dst.reg.idx);
573 }
574 /* If there's a loop in the shader */
575 else if (ins.handler_idx == WINED3DSIH_LOOP
576 || ins.handler_idx == WINED3DSIH_REP)
577 {
578 struct wined3d_shader_src_param src, rel_addr;
579
580 fe->shader_read_src_param(fe_data, &pToken, &src, &rel_addr);
581
582 /* Rep and Loop always use an integer constant for the control parameters */
583 if (ins.handler_idx == WINED3DSIH_REP)
584 {
585 reg_maps->integer_constants |= 1 << src.reg.idx;
586 }
587 else
588 {
589 fe->shader_read_src_param(fe_data, &pToken, &src, &rel_addr);
590 reg_maps->integer_constants |= 1 << src.reg.idx;
591 }
592
593 cur_loop_depth++;
594 if(cur_loop_depth > max_loop_depth)
595 max_loop_depth = cur_loop_depth;
596 }
597 else if (ins.handler_idx == WINED3DSIH_ENDLOOP
598 || ins.handler_idx == WINED3DSIH_ENDREP)
599 {
600 cur_loop_depth--;
601 }
602 /* For subroutine prototypes */
603 else if (ins.handler_idx == WINED3DSIH_LABEL)
604 {
605 struct wined3d_shader_src_param src, rel_addr;
606
607 fe->shader_read_src_param(fe_data, &pToken, &src, &rel_addr);
608 reg_maps->labels |= 1 << src.reg.idx;
609 }
610 /* Set texture, address, temporary registers */
611 else
612 {
613 int i, limit;
614 BOOL color0_mov = FALSE;
615
616 /* This will loop over all the registers and try to
617 * make a bitmask of the ones we're interested in.
618 *
619 * Relative addressing tokens are ignored, but that's
620 * okay, since we'll catch any address registers when
621 * they are initialized (required by spec) */
622
623 if (ins.dst_count)
624 {
625 struct wined3d_shader_dst_param dst_param;
626 struct wined3d_shader_src_param dst_rel_addr;
627
628 fe->shader_read_dst_param(fe_data, &pToken, &dst_param, &dst_rel_addr);
629
630 shader_record_register_usage(This, reg_maps, &dst_param.reg, shader_version.type);
631
632 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
633 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
634 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
635 if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX && shader_version.major < 3
636 && dst_param.reg.type == WINED3DSPR_TEXCRDOUT)
637 {
638 reg_maps->texcoord_mask[dst_param.reg.idx] |= dst_param.write_mask;
639 }
640
641 if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
642 {
643 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *)This;
644
645 if(dst_param.reg.type == WINED3DSPR_COLOROUT && dst_param.reg.idx == 0)
646 {
647 /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
648 * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
649 * the mov and perform the sRGB write correction from the source register.
650 *
651 * However, if the mov is only partial, we can't do this, and if the write
652 * comes from an instruction other than MOV it is hard to do as well. If
653 * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
654
655 ps->color0_mov = FALSE;
656 if (ins.handler_idx == WINED3DSIH_MOV)
657 {
658 /* Used later when the source register is read. */
659 color0_mov = TRUE;
660 }
661 }
662 /* Also drop the MOV marker if the source register is overwritten prior to the shader
663 * end
664 */
665 else if(dst_param.reg.type == WINED3DSPR_TEMP && dst_param.reg.idx == ps->color0_reg)
666 {
667 ps->color0_mov = FALSE;
668 }
669 }
670
671 /* Declare 1.X samplers implicitly, based on the destination reg. number */
672 if (shader_version.major == 1
673 && (ins.handler_idx == WINED3DSIH_TEX
674 || ins.handler_idx == WINED3DSIH_TEXBEM
675 || ins.handler_idx == WINED3DSIH_TEXBEML
676 || ins.handler_idx == WINED3DSIH_TEXDP3TEX
677 || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
678 || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
679 || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
680 || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
681 || ins.handler_idx == WINED3DSIH_TEXREG2AR
682 || ins.handler_idx == WINED3DSIH_TEXREG2GB
683 || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
684 {
685 /* Fake sampler usage, only set reserved bit and ttype */
686 DWORD sampler_code = dst_param.reg.idx;
687
688 TRACE("Setting fake 2D sampler for 1.x pixelshader\n");
689 reg_maps->sampler_type[sampler_code] = WINED3DSTT_2D;
690
691 /* texbem is only valid with < 1.4 pixel shaders */
692 if (ins.handler_idx == WINED3DSIH_TEXBEM
693 || ins.handler_idx == WINED3DSIH_TEXBEML)
694 {
695 reg_maps->bumpmat |= 1 << dst_param.reg.idx;
696 if (ins.handler_idx == WINED3DSIH_TEXBEML)
697 {
698 reg_maps->luminanceparams |= 1 << dst_param.reg.idx;
699 }
700 }
701 }
702 else if (ins.handler_idx == WINED3DSIH_BEM)
703 {
704 reg_maps->bumpmat |= 1 << dst_param.reg.idx;
705 }
706 }
707
708 if (ins.handler_idx == WINED3DSIH_NRM)
709 {
710 reg_maps->usesnrm = 1;
711 }
712 else if (ins.handler_idx == WINED3DSIH_DSY)
713 {
714 reg_maps->usesdsy = 1;
715 }
716 else if (ins.handler_idx == WINED3DSIH_DSX)
717 {
718 reg_maps->usesdsx = 1;
719 }
720 else if(ins.handler_idx == WINED3DSIH_TEXLDD)
721 {
722 reg_maps->usestexldd = 1;
723 }
724 else if(ins.handler_idx == WINED3DSIH_TEXLDL)
725 {
726 reg_maps->usestexldl = 1;
727 }
728 else if(ins.handler_idx == WINED3DSIH_MOVA)
729 {
730 reg_maps->usesmova = 1;
731 }
732 else if(ins.handler_idx == WINED3DSIH_IFC)
733 {
734 reg_maps->usesifc = 1;
735 }
736 else if(ins.handler_idx == WINED3DSIH_CALL)
737 {
738 reg_maps->usescall = 1;
739 }
740
741 limit = ins.src_count + (ins.predicate ? 1 : 0);
742 for (i = 0; i < limit; ++i)
743 {
744 struct wined3d_shader_src_param src_param, src_rel_addr;
745 unsigned int count;
746
747 fe->shader_read_src_param(fe_data, &pToken, &src_param, &src_rel_addr);
748 count = get_instr_extra_regcount(ins.handler_idx, i);
749
750 shader_record_register_usage(This, reg_maps, &src_param.reg, shader_version.type);
751 while (count)
752 {
753 ++src_param.reg.idx;
754 shader_record_register_usage(This, reg_maps, &src_param.reg, shader_version.type);
755 --count;
756 }
757
758 if(color0_mov)
759 {
760 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) This;
761 if(src_param.reg.type == WINED3DSPR_TEMP &&
762 src_param.swizzle == WINED3DSP_NOSWIZZLE)
763 {
764 ps->color0_mov = TRUE;
765 ps->color0_reg = src_param.reg.idx;
766 }
767 }
768 }
769 }
770 }
771 reg_maps->loop_depth = max_loop_depth;
772
773 This->baseShader.functionLength = ((const char *)pToken - (const char *)byte_code);
774
775 return WINED3D_OK;
776 }
777
778 unsigned int shader_find_free_input_register(const struct shader_reg_maps *reg_maps, unsigned int max)
779 {
780 DWORD map = 1 << max;
781 map |= map - 1;
782 map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;
783
784 return wined3d_log2i(map);
785 }
786
787 static void shader_dump_decl_usage(const struct wined3d_shader_semantic *semantic,
788 const struct wined3d_shader_version *shader_version)
789 {
790 TRACE("dcl");
791
792 if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
793 {
794 switch (semantic->sampler_type)
795 {
796 case WINED3DSTT_2D: TRACE("_2d"); break;
797 case WINED3DSTT_CUBE: TRACE("_cube"); break;
798 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
799 default: TRACE("_unknown_ttype(0x%08x)", semantic->sampler_type);
800 }
801 }
802 else
803 {
804 /* Pixel shaders 3.0 don't have usage semantics */
805 if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL)
806 return;
807 else
808 TRACE("_");
809
810 switch (semantic->usage)
811 {
812 case WINED3DDECLUSAGE_POSITION:
813 TRACE("position%d", semantic->usage_idx);
814 break;
815 case WINED3DDECLUSAGE_BLENDINDICES:
816 TRACE("blend");
817 break;
818 case WINED3DDECLUSAGE_BLENDWEIGHT:
819 TRACE("weight");
820 break;
821 case WINED3DDECLUSAGE_NORMAL:
822 TRACE("normal%d", semantic->usage_idx);
823 break;
824 case WINED3DDECLUSAGE_PSIZE:
825 TRACE("psize");
826 break;
827 case WINED3DDECLUSAGE_COLOR:
828 if (semantic->usage_idx == 0) TRACE("color");
829 else TRACE("specular%d", (semantic->usage_idx - 1));
830 break;
831 case WINED3DDECLUSAGE_TEXCOORD:
832 TRACE("texture%d", semantic->usage_idx);
833 break;
834 case WINED3DDECLUSAGE_TANGENT:
835 TRACE("tangent");
836 break;
837 case WINED3DDECLUSAGE_BINORMAL:
838 TRACE("binormal");
839 break;
840 case WINED3DDECLUSAGE_TESSFACTOR:
841 TRACE("tessfactor");
842 break;
843 case WINED3DDECLUSAGE_POSITIONT:
844 TRACE("positionT%d", semantic->usage_idx);
845 break;
846 case WINED3DDECLUSAGE_FOG:
847 TRACE("fog");
848 break;
849 case WINED3DDECLUSAGE_DEPTH:
850 TRACE("depth");
851 break;
852 case WINED3DDECLUSAGE_SAMPLE:
853 TRACE("sample");
854 break;
855 default:
856 FIXME("unknown_semantics(0x%08x)", semantic->usage);
857 }
858 }
859 }
860
861 static void shader_dump_register(const struct wined3d_shader_register *reg,
862 const struct wined3d_shader_version *shader_version)
863 {
864 static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
865 static const char * const misctype_reg_names[] = {"vPos", "vFace"};
866 UINT offset = reg->idx;
867
868 switch (reg->type)
869 {
870 case WINED3DSPR_TEMP:
871 TRACE("r");
872 break;
873
874 case WINED3DSPR_INPUT:
875 TRACE("v");
876 break;
877
878 case WINED3DSPR_CONST:
879 case WINED3DSPR_CONST2:
880 case WINED3DSPR_CONST3:
881 case WINED3DSPR_CONST4:
882 TRACE("c");
883 offset = shader_get_float_offset(reg->type, reg->idx);
884 break;
885
886 case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
887 TRACE("%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
888 break;
889
890 case WINED3DSPR_RASTOUT:
891 TRACE("%s", rastout_reg_names[reg->idx]);
892 break;
893
894 case WINED3DSPR_COLOROUT:
895 TRACE("oC");
896 break;
897
898 case WINED3DSPR_DEPTHOUT:
899 TRACE("oDepth");
900 break;
901
902 case WINED3DSPR_ATTROUT:
903 TRACE("oD");
904 break;
905
906 case WINED3DSPR_TEXCRDOUT:
907 /* Vertex shaders >= 3.0 use general purpose output registers
908 * (WINED3DSPR_OUTPUT), which can include an address token */
909 if (shader_version->major >= 3) TRACE("o");
910 else TRACE("oT");
911 break;
912
913 case WINED3DSPR_CONSTINT:
914 TRACE("i");
915 break;
916
917 case WINED3DSPR_CONSTBOOL:
918 TRACE("b");
919 break;
920
921 case WINED3DSPR_LABEL:
922 TRACE("l");
923 break;
924
925 case WINED3DSPR_LOOP:
926 TRACE("aL");
927 break;
928
929 case WINED3DSPR_SAMPLER:
930 TRACE("s");
931 break;
932
933 case WINED3DSPR_MISCTYPE:
934 if (reg->idx > 1) FIXME("Unhandled misctype register %d\n", reg->idx);
935 else TRACE("%s", misctype_reg_names[reg->idx]);
936 break;
937
938 case WINED3DSPR_PREDICATE:
939 TRACE("p");
940 break;
941
942 case WINED3DSPR_IMMCONST:
943 TRACE("l");
944 break;
945
946 case WINED3DSPR_CONSTBUFFER:
947 TRACE("cb");
948 break;
949
950 default:
951 TRACE("unhandled_rtype(%#x)", reg->type);
952 break;
953 }
954
955 if (reg->type == WINED3DSPR_IMMCONST)
956 {
957 TRACE("(");
958 switch (reg->immconst_type)
959 {
960 case WINED3D_IMMCONST_FLOAT:
961 TRACE("%.8e", *(const float *)reg->immconst_data);
962 break;
963
964 case WINED3D_IMMCONST_FLOAT4:
965 TRACE("%.8e, %.8e, %.8e, %.8e",
966 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
967 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
968 break;
969
970 default:
971 TRACE("<unhandled immconst_type %#x>", reg->immconst_type);
972 break;
973 }
974 TRACE(")");
975 }
976 else if (reg->type != WINED3DSPR_RASTOUT && reg->type != WINED3DSPR_MISCTYPE)
977 {
978 if (reg->array_idx != ~0U)
979 {
980 TRACE("%u[%u", offset, reg->array_idx);
981 if (reg->rel_addr)
982 {
983 TRACE(" + ");
984 shader_dump_src_param(reg->rel_addr, shader_version);
985 }
986 TRACE("]");
987 }
988 else
989 {
990 if (reg->rel_addr)
991 {
992 TRACE("[");
993 shader_dump_src_param(reg->rel_addr, shader_version);
994 TRACE(" + ");
995 }
996 TRACE("%u", offset);
997 if (reg->rel_addr) TRACE("]");
998 }
999 }
1000 }
1001
1002 void shader_dump_dst_param(const struct wined3d_shader_dst_param *param,
1003 const struct wined3d_shader_version *shader_version)
1004 {
1005 DWORD write_mask = param->write_mask;
1006
1007 shader_dump_register(&param->reg, shader_version);
1008
1009 if (write_mask != WINED3DSP_WRITEMASK_ALL)
1010 {
1011 static const char *write_mask_chars = "xyzw";
1012
1013 TRACE(".");
1014 if (write_mask & WINED3DSP_WRITEMASK_0) TRACE("%c", write_mask_chars[0]);
1015 if (write_mask & WINED3DSP_WRITEMASK_1) TRACE("%c", write_mask_chars[1]);
1016 if (write_mask & WINED3DSP_WRITEMASK_2) TRACE("%c", write_mask_chars[2]);
1017 if (write_mask & WINED3DSP_WRITEMASK_3) TRACE("%c", write_mask_chars[3]);
1018 }
1019 }
1020
1021 void shader_dump_src_param(const struct wined3d_shader_src_param *param,
1022 const struct wined3d_shader_version *shader_version)
1023 {
1024 DWORD src_modifier = param->modifiers;
1025 DWORD swizzle = param->swizzle;
1026
1027 if (src_modifier == WINED3DSPSM_NEG
1028 || src_modifier == WINED3DSPSM_BIASNEG
1029 || src_modifier == WINED3DSPSM_SIGNNEG
1030 || src_modifier == WINED3DSPSM_X2NEG
1031 || src_modifier == WINED3DSPSM_ABSNEG)
1032 TRACE("-");
1033 else if (src_modifier == WINED3DSPSM_COMP)
1034 TRACE("1-");
1035 else if (src_modifier == WINED3DSPSM_NOT)
1036 TRACE("!");
1037
1038 if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
1039 TRACE("abs(");
1040
1041 shader_dump_register(&param->reg, shader_version);
1042
1043 if (src_modifier)
1044 {
1045 switch (src_modifier)
1046 {
1047 case WINED3DSPSM_NONE: break;
1048 case WINED3DSPSM_NEG: break;
1049 case WINED3DSPSM_NOT: break;
1050 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
1051 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
1052 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
1053 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
1054 case WINED3DSPSM_COMP: break;
1055 case WINED3DSPSM_X2: TRACE("_x2"); break;
1056 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
1057 case WINED3DSPSM_DZ: TRACE("_dz"); break;
1058 case WINED3DSPSM_DW: TRACE("_dw"); break;
1059 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
1060 case WINED3DSPSM_ABS: TRACE(")"); break;
1061 default:
1062 TRACE("_unknown_modifier(%#x)", src_modifier);
1063 }
1064 }
1065
1066 if (swizzle != WINED3DSP_NOSWIZZLE)
1067 {
1068 static const char *swizzle_chars = "xyzw";
1069 DWORD swizzle_x = swizzle & 0x03;
1070 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1071 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1072 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1073
1074 if (swizzle_x == swizzle_y
1075 && swizzle_x == swizzle_z
1076 && swizzle_x == swizzle_w)
1077 {
1078 TRACE(".%c", swizzle_chars[swizzle_x]);
1079 }
1080 else
1081 {
1082 TRACE(".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
1083 swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
1084 }
1085 }
1086 }
1087
1088 /* Shared code in order to generate the bulk of the shader string.
1089 * NOTE: A description of how to parse tokens can be found on msdn */
1090 void shader_generate_main(IWineD3DBaseShader *iface, struct wined3d_shader_buffer *buffer,
1091 const shader_reg_maps *reg_maps, const DWORD *pFunction, void *backend_ctx)
1092 {
1093 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
1094 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device; /* To access shader backend callbacks */
1095 const struct wined3d_shader_frontend *fe = This->baseShader.frontend;
1096 void *fe_data = This->baseShader.frontend_data;
1097 struct wined3d_shader_src_param src_rel_addr[4];
1098 struct wined3d_shader_src_param src_param[4];
1099 struct wined3d_shader_version shader_version;
1100 struct wined3d_shader_src_param dst_rel_addr;
1101 struct wined3d_shader_dst_param dst_param;
1102 struct wined3d_shader_instruction ins;
1103 struct wined3d_shader_context ctx;
1104 const DWORD *pToken = pFunction;
1105 DWORD i;
1106
1107 /* Initialize current parsing state */
1108 ctx.shader = iface;
1109 ctx.reg_maps = reg_maps;
1110 ctx.buffer = buffer;
1111 ctx.backend_data = backend_ctx;
1112
1113 ins.ctx = &ctx;
1114 ins.dst = &dst_param;
1115 ins.src = src_param;
1116 This->baseShader.parse_state.current_row = 0;
1117
1118 fe->shader_read_header(fe_data, &pToken, &shader_version);
1119
1120 while (!fe->shader_is_end(fe_data, &pToken))
1121 {
1122 const char *comment;
1123 UINT param_size;
1124
1125 /* Skip comment tokens */
1126 fe->shader_read_comment(&pToken, &comment);
1127 if (comment) continue;
1128
1129 /* Read opcode */
1130 fe->shader_read_opcode(fe_data, &pToken, &ins, &param_size);
1131
1132 /* Unknown opcode and its parameters */
1133 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1134 {
1135 TRACE("Skipping unrecognized instruction.\n");
1136 pToken += param_size;
1137 continue;
1138 }
1139
1140 /* Nothing to do */
1141 if (ins.handler_idx == WINED3DSIH_DCL
1142 || ins.handler_idx == WINED3DSIH_NOP
1143 || ins.handler_idx == WINED3DSIH_DEF
1144 || ins.handler_idx == WINED3DSIH_DEFI
1145 || ins.handler_idx == WINED3DSIH_DEFB
1146 || ins.handler_idx == WINED3DSIH_PHASE)
1147 {
1148 pToken += param_size;
1149 continue;
1150 }
1151
1152 /* Destination token */
1153 if (ins.dst_count) fe->shader_read_dst_param(fe_data, &pToken, &dst_param, &dst_rel_addr);
1154
1155 /* Predication token */
1156 if (ins.predicate) ins.predicate = *pToken++;
1157
1158 /* Other source tokens */
1159 for (i = 0; i < ins.src_count; ++i)
1160 {
1161 fe->shader_read_src_param(fe_data, &pToken, &src_param[i], &src_rel_addr[i]);
1162 }
1163
1164 /* Call appropriate function for output target */
1165 device->shader_backend->shader_handle_instruction(&ins);
1166 }
1167 }
1168
1169 static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param *dst)
1170 {
1171 DWORD mmask = dst->modifiers;
1172
1173 switch (dst->shift)
1174 {
1175 case 0: break;
1176 case 13: TRACE("_d8"); break;
1177 case 14: TRACE("_d4"); break;
1178 case 15: TRACE("_d2"); break;
1179 case 1: TRACE("_x2"); break;
1180 case 2: TRACE("_x4"); break;
1181 case 3: TRACE("_x8"); break;
1182 default: TRACE("_unhandled_shift(%d)", dst->shift); break;
1183 }
1184
1185 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
1186 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
1187 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
1188
1189 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
1190 if (mmask)
1191 FIXME("_unrecognized_modifier(%#x)", mmask);
1192 }
1193
1194 void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *pFunction)
1195 {
1196 struct wined3d_shader_version shader_version;
1197 const DWORD* pToken = pFunction;
1198 const char *type_prefix;
1199 DWORD i;
1200
1201 TRACE("Parsing %p\n", pFunction);
1202
1203 fe->shader_read_header(fe_data, &pToken, &shader_version);
1204
1205 switch (shader_version.type)
1206 {
1207 case WINED3D_SHADER_TYPE_VERTEX:
1208 type_prefix = "vs";
1209 break;
1210
1211 case WINED3D_SHADER_TYPE_GEOMETRY:
1212 type_prefix = "gs";
1213 break;
1214
1215 case WINED3D_SHADER_TYPE_PIXEL:
1216 type_prefix = "ps";
1217 break;
1218
1219 default:
1220 FIXME("Unhandled shader type %#x.\n", shader_version.type);
1221 type_prefix = "unknown";
1222 break;
1223 }
1224
1225 TRACE("%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);
1226
1227 while (!fe->shader_is_end(fe_data, &pToken))
1228 {
1229 struct wined3d_shader_instruction ins;
1230 const char *comment;
1231 UINT param_size;
1232
1233 /* comment */
1234 fe->shader_read_comment(&pToken, &comment);
1235 if (comment)
1236 {
1237 TRACE("//%s\n", comment);
1238 continue;
1239 }
1240
1241 fe->shader_read_opcode(fe_data, &pToken, &ins, &param_size);
1242 if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
1243 {
1244 TRACE("Skipping unrecognized instruction.\n");
1245 pToken += param_size;
1246 continue;
1247 }
1248
1249 if (ins.handler_idx == WINED3DSIH_DCL)
1250 {
1251 struct wined3d_shader_semantic semantic;
1252
1253 fe->shader_read_semantic(&pToken, &semantic);
1254
1255 shader_dump_decl_usage(&semantic, &shader_version);
1256 shader_dump_ins_modifiers(&semantic.reg);
1257 TRACE(" ");
1258 shader_dump_dst_param(&semantic.reg, &shader_version);
1259 }
1260 else if (ins.handler_idx == WINED3DSIH_DEF)
1261 {
1262 struct wined3d_shader_dst_param dst;
1263 struct wined3d_shader_src_param rel_addr;
1264
1265 fe->shader_read_dst_param(fe_data, &pToken, &dst, &rel_addr);
1266
1267 TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(dst.reg.type, dst.reg.idx),
1268 *(const float *)(pToken),
1269 *(const float *)(pToken + 1),
1270 *(const float *)(pToken + 2),
1271 *(const float *)(pToken + 3));
1272 pToken += 4;
1273 }
1274 else if (ins.handler_idx == WINED3DSIH_DEFI)
1275 {
1276 struct wined3d_shader_dst_param dst;
1277 struct wined3d_shader_src_param rel_addr;
1278
1279 fe->shader_read_dst_param(fe_data, &pToken, &dst, &rel_addr);
1280
1281 TRACE("defi i%u = %d, %d, %d, %d", dst.reg.idx,
1282 *(pToken),
1283 *(pToken + 1),
1284 *(pToken + 2),
1285 *(pToken + 3));
1286 pToken += 4;
1287 }
1288 else if (ins.handler_idx == WINED3DSIH_DEFB)
1289 {
1290 struct wined3d_shader_dst_param dst;
1291 struct wined3d_shader_src_param rel_addr;
1292
1293 fe->shader_read_dst_param(fe_data, &pToken, &dst, &rel_addr);
1294
1295 TRACE("defb b%u = %s", dst.reg.idx, *pToken ? "true" : "false");
1296 ++pToken;
1297 }
1298 else
1299 {
1300 struct wined3d_shader_src_param dst_rel_addr, src_rel_addr;
1301 struct wined3d_shader_dst_param dst_param;
1302 struct wined3d_shader_src_param src_param;
1303
1304 if (ins.dst_count)
1305 {
1306 fe->shader_read_dst_param(fe_data, &pToken, &dst_param, &dst_rel_addr);
1307 }
1308
1309 /* Print out predication source token first - it follows
1310 * the destination token. */
1311 if (ins.predicate)
1312 {
1313 fe->shader_read_src_param(fe_data, &pToken, &src_param, &src_rel_addr);
1314 TRACE("(");
1315 shader_dump_src_param(&src_param, &shader_version);
1316 TRACE(") ");
1317 }
1318
1319 /* PixWin marks instructions with the coissue flag with a '+' */
1320 if (ins.coissue) TRACE("+");
1321
1322 TRACE("%s", shader_opcode_names[ins.handler_idx]);
1323
1324 if (ins.handler_idx == WINED3DSIH_IFC
1325 || ins.handler_idx == WINED3DSIH_BREAKC)
1326 {
1327 switch (ins.flags)
1328 {
1329 case COMPARISON_GT: TRACE("_gt"); break;
1330 case COMPARISON_EQ: TRACE("_eq"); break;
1331 case COMPARISON_GE: TRACE("_ge"); break;
1332 case COMPARISON_LT: TRACE("_lt"); break;
1333 case COMPARISON_NE: TRACE("_ne"); break;
1334 case COMPARISON_LE: TRACE("_le"); break;
1335 default: TRACE("_(%u)", ins.flags);
1336 }
1337 }
1338 else if (ins.handler_idx == WINED3DSIH_TEX
1339 && shader_version.major >= 2
1340 && (ins.flags & WINED3DSI_TEXLD_PROJECT))
1341 {
1342 TRACE("p");
1343 }
1344
1345 /* We already read the destination token, print it. */
1346 if (ins.dst_count)
1347 {
1348 shader_dump_ins_modifiers(&dst_param);
1349 TRACE(" ");
1350 shader_dump_dst_param(&dst_param, &shader_version);
1351 }
1352
1353 /* Other source tokens */
1354 for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
1355 {
1356 fe->shader_read_src_param(fe_data, &pToken, &src_param, &src_rel_addr);
1357 TRACE(!i ? " " : ", ");
1358 shader_dump_src_param(&src_param, &shader_version);
1359 }
1360 }
1361 TRACE("\n");
1362 }
1363 }
1364
1365 void shader_cleanup(IWineD3DBaseShader *iface)
1366 {
1367 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1368
1369 ((IWineD3DDeviceImpl *)This->baseShader.device)->shader_backend->shader_destroy(iface);
1370 HeapFree(GetProcessHeap(), 0, This->baseShader.reg_maps.constf);
1371 HeapFree(GetProcessHeap(), 0, This->baseShader.function);
1372 shader_delete_constant_list(&This->baseShader.constantsF);
1373 shader_delete_constant_list(&This->baseShader.constantsB);
1374 shader_delete_constant_list(&This->baseShader.constantsI);
1375 list_remove(&This->baseShader.shader_list_entry);
1376
1377 if (This->baseShader.frontend && This->baseShader.frontend_data)
1378 {
1379 This->baseShader.frontend->shader_free(This->baseShader.frontend_data);
1380 }
1381 }
1382
1383 static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
1384 static void shader_none_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS) {}
1385 static void shader_none_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {}
1386 static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1387 static void shader_none_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1388 static void shader_none_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1389 static void shader_none_load_constants(const struct wined3d_context *context, char usePS, char useVS) {}
1390 static void shader_none_load_np2fixup_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1391 static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1392 static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1393 static void shader_none_free(IWineD3DDevice *iface) {}
1394 static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1395
1396 static void shader_none_get_caps(WINED3DDEVTYPE devtype,
1397 const struct wined3d_gl_info *gl_info, struct shader_caps *pCaps)
1398 {
1399 /* Set the shader caps to 0 for the none shader backend */
1400 pCaps->VertexShaderVersion = 0;
1401 pCaps->PixelShaderVersion = 0;
1402 pCaps->PixelShader1xMaxValue = 0.0f;
1403 }
1404
1405 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1406 {
1407 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1408 {
1409 TRACE("Checking support for fixup:\n");
1410 dump_color_fixup_desc(fixup);
1411 }
1412
1413 /* Faked to make some apps happy. */
1414 if (!is_yuv_fixup(fixup))
1415 {
1416 TRACE("[OK]\n");
1417 return TRUE;
1418 }
1419
1420 TRACE("[FAILED]\n");
1421 return FALSE;
1422 }
1423
1424 const shader_backend_t none_shader_backend = {
1425 shader_none_handle_instruction,
1426 shader_none_select,
1427 shader_none_select_depth_blt,
1428 shader_none_deselect_depth_blt,
1429 shader_none_update_float_vertex_constants,
1430 shader_none_update_float_pixel_constants,
1431 shader_none_load_constants,
1432 shader_none_load_np2fixup_constants,
1433 shader_none_destroy,
1434 shader_none_alloc,
1435 shader_none_free,
1436 shader_none_dirty_const,
1437 shader_none_get_caps,
1438 shader_none_color_fixup_supported,
1439 };