wined3d: Store a struct wined3d_shader_dst_param in struct wined3d_shader_semantic.
[wine/hacks.git] / dlls / wined3d / baseshader.c
bloba1d165158e34755547ee48536f0b09d5f7a796dd
1 /*
2 * shaders implementation
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
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "config.h"
27 #include <string.h>
28 #include <stdio.h>
29 #include "wined3d_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
32 WINE_DECLARE_DEBUG_CHANNEL(d3d);
34 static void shader_dump_param(const DWORD param, const DWORD addr_token, int input, DWORD shader_version);
36 static inline BOOL shader_is_version_token(DWORD token) {
37 return shader_is_pshader_version(token) ||
38 shader_is_vshader_version(token);
41 void shader_buffer_init(struct SHADER_BUFFER *buffer)
43 buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
44 buffer->buffer[0] = '\0';
45 buffer->bsize = 0;
46 buffer->lineNo = 0;
47 buffer->newline = TRUE;
50 void shader_buffer_free(struct SHADER_BUFFER *buffer)
52 HeapFree(GetProcessHeap(), 0, buffer->buffer);
55 int shader_vaddline(SHADER_BUFFER* buffer, const char *format, va_list args)
57 char* base = buffer->buffer + buffer->bsize;
58 int rc;
60 rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
62 if (rc < 0 || /* C89 */
63 rc > SHADER_PGMSIZE - 1 - buffer->bsize) { /* C99 */
65 ERR("The buffer allocated for the shader program string "
66 "is too small at %d bytes.\n", SHADER_PGMSIZE);
67 buffer->bsize = SHADER_PGMSIZE - 1;
68 return -1;
71 if (buffer->newline) {
72 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
73 buffer->newline = FALSE;
74 } else {
75 TRACE("%s", base);
78 buffer->bsize += rc;
79 if (buffer->buffer[buffer->bsize-1] == '\n') {
80 buffer->lineNo++;
81 buffer->newline = TRUE;
83 return 0;
86 int shader_addline(SHADER_BUFFER* buffer, const char *format, ...)
88 int ret;
89 va_list args;
91 va_start(args, format);
92 ret = shader_vaddline(buffer, format, args);
93 va_end(args);
95 return ret;
98 void shader_init(struct IWineD3DBaseShaderClass *shader,
99 IWineD3DDevice *device, const SHADER_OPCODE *instruction_table)
101 shader->ref = 1;
102 shader->device = device;
103 shader->shader_ins = instruction_table;
104 list_init(&shader->linked_programs);
107 const SHADER_OPCODE *shader_get_opcode(const SHADER_OPCODE *opcode_table, DWORD shader_version, DWORD code)
109 DWORD i = 0;
111 /** TODO: use dichotomic search */
112 while (opcode_table[i].name)
114 if ((code & WINED3DSI_OPCODE_MASK) == opcode_table[i].opcode
115 && shader_version >= opcode_table[i].min_version
116 && (!opcode_table[i].max_version || shader_version <= opcode_table[i].max_version))
118 return &opcode_table[i];
120 ++i;
123 FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n",
124 code, code, code & WINED3DSI_OPCODE_MASK, shader_version);
126 return NULL;
129 /* Read a parameter opcode from the input stream,
130 * and possibly a relative addressing token.
131 * Return the number of tokens read */
132 static int shader_get_param(const DWORD *pToken, DWORD shader_version, DWORD *param, DWORD *addr_token)
134 /* PS >= 3.0 have relative addressing (with token)
135 * VS >= 2.0 have relative addressing (with token)
136 * VS >= 1.0 < 2.0 have relative addressing (without token)
137 * The version check below should work in general */
139 char rel_token = WINED3DSHADER_VERSION_MAJOR(shader_version) >= 2 &&
140 ((*pToken & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
142 *param = *pToken;
143 *addr_token = rel_token? *(pToken + 1): 0;
144 return rel_token? 2:1;
147 /* Return the number of parameters to skip for an opcode */
148 static inline int shader_skip_opcode(const SHADER_OPCODE *curOpcode, DWORD opcode_token, DWORD shader_version)
150 /* Shaders >= 2.0 may contain address tokens, but fortunately they
151 * have a useful length mask - use it here. Shaders 1.0 contain no such tokens */
152 return (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 2)
153 ? ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT) : curOpcode->num_params;
156 /* Read the parameters of an unrecognized opcode from the input stream
157 * Return the number of tokens read.
159 * Note: This function assumes source or destination token format.
160 * It will not work with specially-formatted tokens like DEF or DCL,
161 * but hopefully those would be recognized */
162 static int shader_skip_unrecognized(const DWORD *pToken, DWORD shader_version)
164 int tokens_read = 0;
165 int i = 0;
167 /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
168 while (*pToken & 0x80000000) {
170 DWORD param, addr_token;
171 tokens_read += shader_get_param(pToken, shader_version, &param, &addr_token);
172 pToken += tokens_read;
174 FIXME("Unrecognized opcode param: token=0x%08x "
175 "addr_token=0x%08x name=", param, addr_token);
176 shader_dump_param(param, addr_token, i, shader_version);
177 FIXME("\n");
178 ++i;
180 return tokens_read;
183 /* Convert floating point offset relative
184 * to a register file to an absolute offset for float constants */
185 static unsigned int shader_get_float_offset(const DWORD reg)
187 unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
188 int regtype = shader_get_regtype(reg);
190 switch (regtype) {
191 case WINED3DSPR_CONST: return regnum;
192 case WINED3DSPR_CONST2: return 2048 + regnum;
193 case WINED3DSPR_CONST3: return 4096 + regnum;
194 case WINED3DSPR_CONST4: return 6144 + regnum;
195 default:
196 FIXME("Unsupported register type: %d\n", regtype);
197 return regnum;
201 static void shader_delete_constant_list(struct list* clist) {
203 struct list *ptr;
204 struct local_constant* constant;
206 ptr = list_head(clist);
207 while (ptr) {
208 constant = LIST_ENTRY(ptr, struct local_constant, entry);
209 ptr = list_next(clist, ptr);
210 HeapFree(GetProcessHeap(), 0, constant);
212 list_init(clist);
215 static void shader_parse_dst_param(DWORD param, DWORD addr_param, struct wined3d_shader_dst_param *dst)
217 dst->register_type = ((param & WINED3DSP_REGTYPE_MASK) >> WINED3DSP_REGTYPE_SHIFT)
218 | ((param & WINED3DSP_REGTYPE_MASK2) >> WINED3DSP_REGTYPE_SHIFT2);
219 dst->register_idx = param & WINED3DSP_REGNUM_MASK;
220 dst->write_mask = param & WINED3DSP_WRITEMASK_ALL;
221 dst->modifiers = param & WINED3DSP_DSTMOD_MASK;
222 dst->token = param;
223 dst->addr_token = addr_param;
226 /* Note that this does not count the loop register
227 * as an address register. */
229 HRESULT shader_get_registers_used(IWineD3DBaseShader *iface, struct shader_reg_maps *reg_maps,
230 struct wined3d_shader_semantic *semantics_in, struct wined3d_shader_semantic *semantics_out,
231 const DWORD *byte_code)
233 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
234 const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;
235 DWORD shader_version;
236 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
237 const DWORD* pToken = byte_code;
238 char pshader;
240 /* There are some minor differences between pixel and vertex shaders */
242 memset(reg_maps->bumpmat, 0, sizeof(reg_maps->bumpmat));
243 memset(reg_maps->luminanceparams, 0, sizeof(reg_maps->luminanceparams));
245 /* get_registers_used is called on every compile on some 1.x shaders, which can result
246 * in stacking up a collection of local constants. Delete the old constants if existing
248 shader_delete_constant_list(&This->baseShader.constantsF);
249 shader_delete_constant_list(&This->baseShader.constantsB);
250 shader_delete_constant_list(&This->baseShader.constantsI);
252 /* The version token is supposed to be the first token */
253 if (!shader_is_version_token(*pToken))
255 FIXME("First token is not a version token, invalid shader.\n");
256 return WINED3DERR_INVALIDCALL;
258 reg_maps->shader_version = shader_version = *pToken++;
259 pshader = shader_is_pshader_version(shader_version);
261 while (WINED3DVS_END() != *pToken) {
262 CONST SHADER_OPCODE* curOpcode;
263 DWORD opcode_token;
265 /* Skip comments */
266 if (shader_is_comment(*pToken))
268 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
269 ++pToken;
270 pToken += comment_len;
271 continue;
274 /* Fetch opcode */
275 opcode_token = *pToken++;
276 curOpcode = shader_get_opcode(shader_ins, shader_version, opcode_token);
278 /* Unhandled opcode, and its parameters */
279 if (NULL == curOpcode) {
280 while (*pToken & 0x80000000)
281 ++pToken;
283 /* Handle declarations */
284 } else if (WINED3DSIO_DCL == curOpcode->opcode) {
286 DWORD usage = *pToken++;
287 DWORD param = *pToken++;
288 DWORD regtype = shader_get_regtype(param);
289 unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
291 /* Vshader: mark attributes used
292 Pshader: mark 3.0 input registers used, save token */
293 if (WINED3DSPR_INPUT == regtype) {
295 if (!pshader)
296 reg_maps->attributes[regnum] = 1;
297 else
298 reg_maps->packed_input[regnum] = 1;
300 semantics_in[regnum].usage = usage;
301 shader_parse_dst_param(param, 0, &semantics_in[regnum].reg);
303 /* Vshader: mark 3.0 output registers used, save token */
304 } else if (WINED3DSPR_OUTPUT == regtype) {
305 reg_maps->packed_output[regnum] = 1;
306 semantics_out[regnum].usage = usage;
307 shader_parse_dst_param(param, 0, &semantics_out[regnum].reg);
309 if (usage & (WINED3DDECLUSAGE_FOG << WINED3DSP_DCL_USAGE_SHIFT))
310 reg_maps->fog = 1;
312 /* Save sampler usage token */
313 } else if (WINED3DSPR_SAMPLER == regtype)
314 reg_maps->samplers[regnum] = usage;
316 } else if (WINED3DSIO_DEF == curOpcode->opcode) {
318 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
319 if (!lconst) return E_OUTOFMEMORY;
320 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
321 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
323 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
324 if (WINED3DSHADER_VERSION_MAJOR(shader_version) == 1 && pshader)
326 float *value = (float *) lconst->value;
327 if(value[0] < -1.0) value[0] = -1.0;
328 else if(value[0] > 1.0) value[0] = 1.0;
329 if(value[1] < -1.0) value[1] = -1.0;
330 else if(value[1] > 1.0) value[1] = 1.0;
331 if(value[2] < -1.0) value[2] = -1.0;
332 else if(value[2] > 1.0) value[2] = 1.0;
333 if(value[3] < -1.0) value[3] = -1.0;
334 else if(value[3] > 1.0) value[3] = 1.0;
337 list_add_head(&This->baseShader.constantsF, &lconst->entry);
338 pToken += curOpcode->num_params;
340 } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
342 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
343 if (!lconst) return E_OUTOFMEMORY;
344 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
345 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
346 list_add_head(&This->baseShader.constantsI, &lconst->entry);
347 pToken += curOpcode->num_params;
349 } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
351 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
352 if (!lconst) return E_OUTOFMEMORY;
353 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
354 memcpy(lconst->value, pToken + 1, 1 * sizeof(DWORD));
355 list_add_head(&This->baseShader.constantsB, &lconst->entry);
356 pToken += curOpcode->num_params;
358 /* If there's a loop in the shader */
359 } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
360 WINED3DSIO_REP == curOpcode->opcode) {
361 cur_loop_depth++;
362 if(cur_loop_depth > max_loop_depth)
363 max_loop_depth = cur_loop_depth;
364 pToken += curOpcode->num_params;
366 /* Rep and Loop always use an integer constant for the control parameters */
367 This->baseShader.uses_int_consts = TRUE;
368 } else if (WINED3DSIO_ENDLOOP == curOpcode->opcode ||
369 WINED3DSIO_ENDREP == curOpcode->opcode) {
370 cur_loop_depth--;
372 /* For subroutine prototypes */
373 } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
375 DWORD snum = *pToken & WINED3DSP_REGNUM_MASK;
376 reg_maps->labels[snum] = 1;
377 pToken += curOpcode->num_params;
379 /* Set texture, address, temporary registers */
380 } else {
381 int i, limit;
383 /* Declare 1.X samplers implicitly, based on the destination reg. number */
384 if (WINED3DSHADER_VERSION_MAJOR(shader_version) == 1
385 && pshader /* Filter different instructions with the same enum values in VS */
386 && (WINED3DSIO_TEX == curOpcode->opcode
387 || WINED3DSIO_TEXBEM == curOpcode->opcode
388 || WINED3DSIO_TEXBEML == curOpcode->opcode
389 || WINED3DSIO_TEXDP3TEX == curOpcode->opcode
390 || WINED3DSIO_TEXM3x2TEX == curOpcode->opcode
391 || WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode
392 || WINED3DSIO_TEXM3x3TEX == curOpcode->opcode
393 || WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode
394 || WINED3DSIO_TEXREG2AR == curOpcode->opcode
395 || WINED3DSIO_TEXREG2GB == curOpcode->opcode
396 || WINED3DSIO_TEXREG2RGB == curOpcode->opcode))
398 /* Fake sampler usage, only set reserved bit and ttype */
399 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
401 TRACE("Setting fake 2D sampler for 1.x pixelshader\n");
402 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
404 /* texbem is only valid with < 1.4 pixel shaders */
405 if(WINED3DSIO_TEXBEM == curOpcode->opcode ||
406 WINED3DSIO_TEXBEML == curOpcode->opcode) {
407 reg_maps->bumpmat[sampler_code] = TRUE;
408 if(WINED3DSIO_TEXBEML == curOpcode->opcode) {
409 reg_maps->luminanceparams[sampler_code] = TRUE;
413 if(WINED3DSIO_NRM == curOpcode->opcode) {
414 reg_maps->usesnrm = 1;
415 } else if(WINED3DSIO_BEM == curOpcode->opcode && pshader) {
416 DWORD regnum = *pToken & WINED3DSP_REGNUM_MASK;
417 reg_maps->bumpmat[regnum] = TRUE;
418 } else if(WINED3DSIO_DSY == curOpcode->opcode) {
419 reg_maps->usesdsy = 1;
422 /* This will loop over all the registers and try to
423 * make a bitmask of the ones we're interested in.
425 * Relative addressing tokens are ignored, but that's
426 * okay, since we'll catch any address registers when
427 * they are initialized (required by spec) */
429 limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
430 curOpcode->num_params + 1: curOpcode->num_params;
432 for (i = 0; i < limit; ++i) {
434 DWORD param, addr_token, reg, regtype;
435 pToken += shader_get_param(pToken, shader_version, &param, &addr_token);
437 regtype = shader_get_regtype(param);
438 reg = param & WINED3DSP_REGNUM_MASK;
440 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
442 if (pshader)
443 reg_maps->texcoord[reg] = 1;
444 else
445 reg_maps->address[reg] = 1;
448 else if (WINED3DSPR_TEMP == regtype)
449 reg_maps->temporary[reg] = 1;
451 else if (WINED3DSPR_INPUT == regtype) {
452 if( !pshader)
453 reg_maps->attributes[reg] = 1;
454 else {
455 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
456 /* If relative addressing is used, we must assume that all registers
457 * are used. Even if it is a construct like v3[aL], we can't assume
458 * that v0, v1 and v2 aren't read because aL can be negative
460 unsigned int i;
461 for(i = 0; i < MAX_REG_INPUT; i++) {
462 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[i] = TRUE;
464 } else {
465 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[reg] = TRUE;
470 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
471 reg_maps->fog = 1;
473 else if (WINED3DSPR_MISCTYPE == regtype && reg == 0 && pshader)
474 reg_maps->vpos = 1;
476 else if(WINED3DSPR_CONST == regtype) {
477 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
478 if(!pshader) {
479 if(reg <= ((IWineD3DVertexShaderImpl *) This)->min_rel_offset) {
480 ((IWineD3DVertexShaderImpl *) This)->min_rel_offset = reg;
481 } else if(reg >= ((IWineD3DVertexShaderImpl *) This)->max_rel_offset) {
482 ((IWineD3DVertexShaderImpl *) This)->max_rel_offset = reg;
485 reg_maps->usesrelconstF = TRUE;
488 else if(WINED3DSPR_CONSTINT == regtype) {
489 This->baseShader.uses_int_consts = TRUE;
491 else if(WINED3DSPR_CONSTBOOL == regtype) {
492 This->baseShader.uses_bool_consts = TRUE;
495 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and is used
496 * in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel shaders because TECRDOUT
497 * isn't used in them, but future register types might cause issues
499 else if (WINED3DSPR_TEXCRDOUT == regtype && i == 0 /* Only look at writes */
500 && !pshader && WINED3DSHADER_VERSION_MAJOR(shader_version) < 3)
502 reg_maps->texcoord_mask[reg] |= shader_get_writemask(param);
507 ++pToken;
508 reg_maps->loop_depth = max_loop_depth;
510 This->baseShader.functionLength = ((char *)pToken - (char *)byte_code);
512 return WINED3D_OK;
515 static void shader_dump_decl_usage(DWORD decl, DWORD param, DWORD shader_version)
517 DWORD regtype = shader_get_regtype(param);
519 TRACE("dcl");
521 if (regtype == WINED3DSPR_SAMPLER) {
522 DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
524 switch (ttype) {
525 case WINED3DSTT_2D: TRACE("_2d"); break;
526 case WINED3DSTT_CUBE: TRACE("_cube"); break;
527 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
528 default: TRACE("_unknown_ttype(0x%08x)", ttype);
531 } else {
533 DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
534 DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
536 /* Pixel shaders 3.0 don't have usage semantics */
537 if (shader_is_pshader_version(shader_version) && shader_version < WINED3DPS_VERSION(3,0))
538 return;
539 else
540 TRACE("_");
542 switch(usage) {
543 case WINED3DDECLUSAGE_POSITION:
544 TRACE("position%d", idx);
545 break;
546 case WINED3DDECLUSAGE_BLENDINDICES:
547 TRACE("blend");
548 break;
549 case WINED3DDECLUSAGE_BLENDWEIGHT:
550 TRACE("weight");
551 break;
552 case WINED3DDECLUSAGE_NORMAL:
553 TRACE("normal%d", idx);
554 break;
555 case WINED3DDECLUSAGE_PSIZE:
556 TRACE("psize");
557 break;
558 case WINED3DDECLUSAGE_COLOR:
559 if(idx == 0) {
560 TRACE("color");
561 } else {
562 TRACE("specular%d", (idx - 1));
564 break;
565 case WINED3DDECLUSAGE_TEXCOORD:
566 TRACE("texture%d", idx);
567 break;
568 case WINED3DDECLUSAGE_TANGENT:
569 TRACE("tangent");
570 break;
571 case WINED3DDECLUSAGE_BINORMAL:
572 TRACE("binormal");
573 break;
574 case WINED3DDECLUSAGE_TESSFACTOR:
575 TRACE("tessfactor");
576 break;
577 case WINED3DDECLUSAGE_POSITIONT:
578 TRACE("positionT%d", idx);
579 break;
580 case WINED3DDECLUSAGE_FOG:
581 TRACE("fog");
582 break;
583 case WINED3DDECLUSAGE_DEPTH:
584 TRACE("depth");
585 break;
586 case WINED3DDECLUSAGE_SAMPLE:
587 TRACE("sample");
588 break;
589 default:
590 FIXME("unknown_semantics(0x%08x)", usage);
595 static void shader_dump_arr_entry(const DWORD param, const DWORD addr_token,
596 unsigned int reg, int input, DWORD shader_version)
598 char relative =
599 ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
601 if (relative) {
602 TRACE("[");
603 if (addr_token)
604 shader_dump_param(addr_token, 0, input, shader_version);
605 else
606 TRACE("a0.x");
607 TRACE(" + ");
609 TRACE("%u", reg);
610 if (relative)
611 TRACE("]");
614 static void shader_dump_param(const DWORD param, const DWORD addr_token, int input, DWORD shader_version)
616 static const char * const rastout_reg_names[] = { "oPos", "oFog", "oPts" };
617 static const char * const misctype_reg_names[] = { "vPos", "vFace"};
618 const char *swizzle_reg_chars = "xyzw";
620 DWORD reg = param & WINED3DSP_REGNUM_MASK;
621 DWORD regtype = shader_get_regtype(param);
622 DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
624 /* There are some minor differences between pixel and vertex shaders */
625 char pshader = shader_is_pshader_version(shader_version);
627 if (input) {
628 if ( (modifier == WINED3DSPSM_NEG) ||
629 (modifier == WINED3DSPSM_BIASNEG) ||
630 (modifier == WINED3DSPSM_SIGNNEG) ||
631 (modifier == WINED3DSPSM_X2NEG) ||
632 (modifier == WINED3DSPSM_ABSNEG) )
633 TRACE("-");
634 else if (modifier == WINED3DSPSM_COMP)
635 TRACE("1-");
636 else if (modifier == WINED3DSPSM_NOT)
637 TRACE("!");
639 if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG)
640 TRACE("abs(");
643 switch (regtype) {
644 case WINED3DSPR_TEMP:
645 TRACE("r%u", reg);
646 break;
647 case WINED3DSPR_INPUT:
648 TRACE("v");
649 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
650 break;
651 case WINED3DSPR_CONST:
652 case WINED3DSPR_CONST2:
653 case WINED3DSPR_CONST3:
654 case WINED3DSPR_CONST4:
655 TRACE("c");
656 shader_dump_arr_entry(param, addr_token, shader_get_float_offset(param), input, shader_version);
657 break;
658 case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
659 TRACE("%c%u", (pshader? 't':'a'), reg);
660 break;
661 case WINED3DSPR_RASTOUT:
662 TRACE("%s", rastout_reg_names[reg]);
663 break;
664 case WINED3DSPR_COLOROUT:
665 TRACE("oC%u", reg);
666 break;
667 case WINED3DSPR_DEPTHOUT:
668 TRACE("oDepth");
669 break;
670 case WINED3DSPR_ATTROUT:
671 TRACE("oD%u", reg);
672 break;
673 case WINED3DSPR_TEXCRDOUT:
675 /* Vertex shaders >= 3.0 use general purpose output registers
676 * (WINED3DSPR_OUTPUT), which can include an address token */
678 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3) {
679 TRACE("o");
680 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
682 else
683 TRACE("oT%u", reg);
684 break;
685 case WINED3DSPR_CONSTINT:
686 TRACE("i");
687 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
688 break;
689 case WINED3DSPR_CONSTBOOL:
690 TRACE("b");
691 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
692 break;
693 case WINED3DSPR_LABEL:
694 TRACE("l%u", reg);
695 break;
696 case WINED3DSPR_LOOP:
697 TRACE("aL");
698 break;
699 case WINED3DSPR_SAMPLER:
700 TRACE("s%u", reg);
701 break;
702 case WINED3DSPR_MISCTYPE:
703 if (reg > 1) {
704 FIXME("Unhandled misctype register %d\n", reg);
705 } else {
706 TRACE("%s", misctype_reg_names[reg]);
708 break;
709 case WINED3DSPR_PREDICATE:
710 TRACE("p%u", reg);
711 break;
712 default:
713 TRACE("unhandled_rtype(%#x)", regtype);
714 break;
717 if (!input) {
718 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
720 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
721 TRACE(".");
722 if (param & WINED3DSP_WRITEMASK_0) TRACE("%c", swizzle_reg_chars[0]);
723 if (param & WINED3DSP_WRITEMASK_1) TRACE("%c", swizzle_reg_chars[1]);
724 if (param & WINED3DSP_WRITEMASK_2) TRACE("%c", swizzle_reg_chars[2]);
725 if (param & WINED3DSP_WRITEMASK_3) TRACE("%c", swizzle_reg_chars[3]);
728 } else {
729 /** operand input */
730 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
731 DWORD swizzle_x = swizzle & 0x03;
732 DWORD swizzle_y = (swizzle >> 2) & 0x03;
733 DWORD swizzle_z = (swizzle >> 4) & 0x03;
734 DWORD swizzle_w = (swizzle >> 6) & 0x03;
736 if (0 != modifier) {
737 switch (modifier) {
738 case WINED3DSPSM_NONE: break;
739 case WINED3DSPSM_NEG: break;
740 case WINED3DSPSM_NOT: break;
741 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
742 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
743 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
744 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
745 case WINED3DSPSM_COMP: break;
746 case WINED3DSPSM_X2: TRACE("_x2"); break;
747 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
748 case WINED3DSPSM_DZ: TRACE("_dz"); break;
749 case WINED3DSPSM_DW: TRACE("_dw"); break;
750 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
751 case WINED3DSPSM_ABS: TRACE(")"); break;
752 default:
753 TRACE("_unknown_modifier(%#x)", modifier >> WINED3DSP_SRCMOD_SHIFT);
758 * swizzle bits fields:
759 * RRGGBBAA
761 if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) != swizzle) {
762 if (swizzle_x == swizzle_y &&
763 swizzle_x == swizzle_z &&
764 swizzle_x == swizzle_w) {
765 TRACE(".%c", swizzle_reg_chars[swizzle_x]);
766 } else {
767 TRACE(".%c%c%c%c",
768 swizzle_reg_chars[swizzle_x],
769 swizzle_reg_chars[swizzle_y],
770 swizzle_reg_chars[swizzle_z],
771 swizzle_reg_chars[swizzle_w]);
777 /* Shared code in order to generate the bulk of the shader string.
778 * NOTE: A description of how to parse tokens can be found on msdn */
779 void shader_generate_main(IWineD3DBaseShader *iface, SHADER_BUFFER* buffer,
780 const shader_reg_maps* reg_maps, CONST DWORD* pFunction)
782 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
783 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device; /* To access shader backend callbacks */
784 const SHADER_OPCODE *opcode_table = This->baseShader.shader_ins;
785 const SHADER_HANDLER *handler_table = device->shader_backend->shader_instruction_handler_table;
786 DWORD shader_version = reg_maps->shader_version;
787 struct wined3d_shader_dst_param dst_param;
788 struct wined3d_shader_instruction ins;
789 const DWORD *pToken = pFunction;
790 const SHADER_OPCODE *curOpcode;
791 SHADER_HANDLER hw_fct;
792 DWORD i;
794 /* Initialize current parsing state */
795 ins.shader = iface;
796 ins.buffer = buffer;
797 ins.reg_maps = reg_maps;
798 ins.dst = &dst_param;
799 This->baseShader.parse_state.current_row = 0;
801 while (WINED3DPS_END() != *pToken)
803 DWORD opcode_token;
805 /* Skip version token */
806 if (shader_is_version_token(*pToken))
808 ++pToken;
809 continue;
812 /* Skip comment tokens */
813 if (shader_is_comment(*pToken))
815 pToken += (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
816 ++pToken;
817 continue;
820 /* Read opcode */
821 opcode_token = *pToken++;
822 curOpcode = shader_get_opcode(opcode_table, shader_version, opcode_token);
824 /* Unknown opcode and its parameters */
825 if (!curOpcode)
827 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
828 pToken += shader_skip_unrecognized(pToken, shader_version);
829 continue;
832 /* Nothing to do */
833 if (WINED3DSIO_DCL == curOpcode->opcode
834 || WINED3DSIO_NOP == curOpcode->opcode
835 || WINED3DSIO_DEF == curOpcode->opcode
836 || WINED3DSIO_DEFI == curOpcode->opcode
837 || WINED3DSIO_DEFB == curOpcode->opcode
838 || WINED3DSIO_PHASE == curOpcode->opcode
839 || WINED3DSIO_RET == curOpcode->opcode)
841 pToken += shader_skip_opcode(curOpcode, opcode_token, shader_version);
842 continue;
845 /* Select handler */
846 hw_fct = handler_table[curOpcode->handler_idx];
848 /* Unhandled opcode */
849 if (!hw_fct)
851 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
852 pToken += shader_skip_opcode(curOpcode, opcode_token, shader_version);
853 continue;
856 ins.handler_idx = curOpcode->handler_idx;
857 ins.flags = opcode_token & WINED3D_OPCODESPECIFICCONTROL_MASK;
858 ins.coissue = opcode_token & WINED3DSI_COISSUE;
860 /* Destination token */
861 ins.dst_count = curOpcode->dst_token ? 1 : 0;
862 if (ins.dst_count)
864 DWORD param, addr_param = 0;
865 pToken += shader_get_param(pToken, shader_version, &param, &addr_param);
866 shader_parse_dst_param(param, addr_param, &dst_param);
869 /* Predication token */
870 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) ins.predicate = *pToken++;
872 /* Other source tokens */
873 ins.src_count = curOpcode->num_params - curOpcode->dst_token;
874 for (i = 0; i < ins.src_count; ++i)
876 DWORD param, addr_token = 0;
877 pToken += shader_get_param(pToken, shader_version, &param, &addr_token);
878 ins.src[i] = param;
879 ins.src_addr[i] = addr_token;
882 /* Call appropriate function for output target */
883 hw_fct(&ins);
885 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
886 /* FIXME: This should be internal to the shader backend.
887 * Also, right now this is the only reason "shader_mode" exists. */
888 if (This->baseShader.shader_mode == SHADER_GLSL) shader_glsl_add_instruction_modifiers(&ins);
892 static void shader_dump_ins_modifiers(const DWORD output)
894 DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
895 DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
897 switch (shift) {
898 case 0: break;
899 case 13: TRACE("_d8"); break;
900 case 14: TRACE("_d4"); break;
901 case 15: TRACE("_d2"); break;
902 case 1: TRACE("_x2"); break;
903 case 2: TRACE("_x4"); break;
904 case 3: TRACE("_x8"); break;
905 default: TRACE("_unhandled_shift(%d)", shift); break;
908 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
909 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
910 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
912 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
913 if (mmask)
914 FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
917 void shader_trace_init(const DWORD *pFunction, const SHADER_OPCODE *opcode_table)
919 const DWORD* pToken = pFunction;
920 const SHADER_OPCODE* curOpcode = NULL;
921 DWORD shader_version;
922 DWORD opcode_token;
923 DWORD i;
925 TRACE("Parsing %p\n", pFunction);
927 /* The version token is supposed to be the first token */
928 if (!shader_is_version_token(*pToken))
930 FIXME("First token is not a version token, invalid shader.\n");
931 return;
933 shader_version = *pToken++;
934 TRACE("%s_%u_%u\n", shader_is_pshader_version(shader_version) ? "ps": "vs",
935 WINED3DSHADER_VERSION_MAJOR(shader_version), WINED3DSHADER_VERSION_MINOR(shader_version));
937 while (WINED3DVS_END() != *pToken)
939 if (shader_is_comment(*pToken)) /* comment */
941 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
942 ++pToken;
943 TRACE("//%s\n", (const char*)pToken);
944 pToken += comment_len;
945 continue;
947 opcode_token = *pToken++;
948 curOpcode = shader_get_opcode(opcode_table, shader_version, opcode_token);
950 if (!curOpcode)
952 int tokens_read;
953 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
954 tokens_read = shader_skip_unrecognized(pToken, shader_version);
955 pToken += tokens_read;
957 else
959 if (curOpcode->opcode == WINED3DSIO_DCL)
961 DWORD usage = *pToken;
962 DWORD param = *(pToken + 1);
964 shader_dump_decl_usage(usage, param, shader_version);
965 shader_dump_ins_modifiers(param);
966 TRACE(" ");
967 shader_dump_param(param, 0, 0, shader_version);
968 pToken += 2;
970 else if (curOpcode->opcode == WINED3DSIO_DEF)
972 unsigned int offset = shader_get_float_offset(*pToken);
974 TRACE("def c%u = %f, %f, %f, %f", offset,
975 *(const float *)(pToken + 1),
976 *(const float *)(pToken + 2),
977 *(const float *)(pToken + 3),
978 *(const float *)(pToken + 4));
979 pToken += 5;
981 else if (curOpcode->opcode == WINED3DSIO_DEFI)
983 TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
984 *(pToken + 1),
985 *(pToken + 2),
986 *(pToken + 3),
987 *(pToken + 4));
988 pToken += 5;
990 else if (curOpcode->opcode == WINED3DSIO_DEFB)
992 TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
993 *(pToken + 1)? "true": "false");
994 pToken += 2;
996 else
998 DWORD param, addr_token;
999 int tokens_read;
1001 /* Print out predication source token first - it follows
1002 * the destination token. */
1003 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
1005 TRACE("(");
1006 shader_dump_param(*(pToken + 2), 0, 1, shader_version);
1007 TRACE(") ");
1009 if (opcode_token & WINED3DSI_COISSUE)
1011 /* PixWin marks instructions with the coissue flag with a '+' */
1012 TRACE("+");
1015 TRACE("%s", curOpcode->name);
1017 if (curOpcode->opcode == WINED3DSIO_IFC
1018 || curOpcode->opcode == WINED3DSIO_BREAKC)
1020 DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1022 switch (op)
1024 case COMPARISON_GT: TRACE("_gt"); break;
1025 case COMPARISON_EQ: TRACE("_eq"); break;
1026 case COMPARISON_GE: TRACE("_ge"); break;
1027 case COMPARISON_LT: TRACE("_lt"); break;
1028 case COMPARISON_NE: TRACE("_ne"); break;
1029 case COMPARISON_LE: TRACE("_le"); break;
1030 default: TRACE("_(%u)", op);
1033 else if (curOpcode->opcode == WINED3DSIO_TEX
1034 && shader_version >= WINED3DPS_VERSION(2,0)
1035 && (opcode_token & WINED3DSI_TEXLD_PROJECT))
1037 TRACE("p");
1040 /* Destination token */
1041 if (curOpcode->dst_token)
1043 tokens_read = shader_get_param(pToken, shader_version, &param, &addr_token);
1044 pToken += tokens_read;
1046 shader_dump_ins_modifiers(param);
1047 TRACE(" ");
1048 shader_dump_param(param, addr_token, 0, shader_version);
1051 /* Predication token - already printed out, just skip it */
1052 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
1054 pToken++;
1057 /* Other source tokens */
1058 for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i)
1060 tokens_read = shader_get_param(pToken, shader_version, &param, &addr_token);
1061 pToken += tokens_read;
1063 TRACE((i == 0)? " " : ", ");
1064 shader_dump_param(param, addr_token, 1, shader_version);
1067 TRACE("\n");
1072 void shader_cleanup(IWineD3DBaseShader *iface)
1074 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1076 ((IWineD3DDeviceImpl *)This->baseShader.device)->shader_backend->shader_destroy(iface);
1077 HeapFree(GetProcessHeap(), 0, This->baseShader.function);
1078 shader_delete_constant_list(&This->baseShader.constantsF);
1079 shader_delete_constant_list(&This->baseShader.constantsB);
1080 shader_delete_constant_list(&This->baseShader.constantsI);
1081 list_remove(&This->baseShader.shader_list_entry);
1084 static const SHADER_HANDLER shader_none_instruction_handler_table[WINED3DSIH_TABLE_SIZE] = {0};
1085 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
1086 static void shader_none_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {}
1087 static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1088 static void shader_none_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1089 static void shader_none_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1090 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1091 static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1092 static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1093 static void shader_none_free(IWineD3DDevice *iface) {}
1094 static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1095 static GLuint shader_none_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer, const struct ps_compile_args *args) {
1096 FIXME("NONE shader backend asked to generate a pixel shader\n");
1097 return 0;
1099 static GLuint shader_none_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer, const struct vs_compile_args *args) {
1100 FIXME("NONE shader backend asked to generate a vertex shader\n");
1101 return 0;
1104 #define GLINFO_LOCATION (*gl_info)
1105 static void shader_none_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
1107 /* Set the shader caps to 0 for the none shader backend */
1108 pCaps->VertexShaderVersion = 0;
1109 pCaps->PixelShaderVersion = 0;
1110 pCaps->PixelShader1xMaxValue = 0.0;
1112 #undef GLINFO_LOCATION
1113 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1115 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1117 TRACE("Checking support for fixup:\n");
1118 dump_color_fixup_desc(fixup);
1121 /* Faked to make some apps happy. */
1122 if (!is_yuv_fixup(fixup))
1124 TRACE("[OK]\n");
1125 return TRUE;
1128 TRACE("[FAILED]\n");
1129 return FALSE;
1132 const shader_backend_t none_shader_backend = {
1133 shader_none_instruction_handler_table,
1134 shader_none_select,
1135 shader_none_select_depth_blt,
1136 shader_none_deselect_depth_blt,
1137 shader_none_update_float_vertex_constants,
1138 shader_none_update_float_pixel_constants,
1139 shader_none_load_constants,
1140 shader_none_destroy,
1141 shader_none_alloc,
1142 shader_none_free,
1143 shader_none_dirty_const,
1144 shader_none_generate_pshader,
1145 shader_none_generate_vshader,
1146 shader_none_get_caps,
1147 shader_none_color_fixup_supported,