wined3d: Get rid of the token field from struct wined3d_shader_src_param.
[wine/hacks.git] / dlls / wined3d / baseshader.c
blobceb2473fd655dbb4db66909d21e22184b6ab1a1a
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
10 * Copyright 2009 Henri Verbeet for CodeWeavers
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.
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.
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
27 #include "config.h"
28 #include <string.h>
29 #include <stdio.h>
30 #include "wined3d_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
33 WINE_DECLARE_DEBUG_CHANNEL(d3d);
35 static void shader_dump_param(const DWORD param, const DWORD addr_token, int input, DWORD shader_version);
37 static inline BOOL shader_is_version_token(DWORD token) {
38 return shader_is_pshader_version(token) ||
39 shader_is_vshader_version(token);
42 void shader_buffer_init(struct SHADER_BUFFER *buffer)
44 buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
45 buffer->buffer[0] = '\0';
46 buffer->bsize = 0;
47 buffer->lineNo = 0;
48 buffer->newline = TRUE;
51 void shader_buffer_free(struct SHADER_BUFFER *buffer)
53 HeapFree(GetProcessHeap(), 0, buffer->buffer);
56 int shader_vaddline(SHADER_BUFFER* buffer, const char *format, va_list args)
58 char* base = buffer->buffer + buffer->bsize;
59 int rc;
61 rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
63 if (rc < 0 || /* C89 */
64 rc > SHADER_PGMSIZE - 1 - buffer->bsize) { /* C99 */
66 ERR("The buffer allocated for the shader program string "
67 "is too small at %d bytes.\n", SHADER_PGMSIZE);
68 buffer->bsize = SHADER_PGMSIZE - 1;
69 return -1;
72 if (buffer->newline) {
73 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
74 buffer->newline = FALSE;
75 } else {
76 TRACE("%s", base);
79 buffer->bsize += rc;
80 if (buffer->buffer[buffer->bsize-1] == '\n') {
81 buffer->lineNo++;
82 buffer->newline = TRUE;
84 return 0;
87 int shader_addline(SHADER_BUFFER* buffer, const char *format, ...)
89 int ret;
90 va_list args;
92 va_start(args, format);
93 ret = shader_vaddline(buffer, format, args);
94 va_end(args);
96 return ret;
99 void shader_init(struct IWineD3DBaseShaderClass *shader,
100 IWineD3DDevice *device, const SHADER_OPCODE *instruction_table)
102 shader->ref = 1;
103 shader->device = device;
104 shader->shader_ins = instruction_table;
105 list_init(&shader->linked_programs);
108 const SHADER_OPCODE *shader_get_opcode(const SHADER_OPCODE *opcode_table, DWORD shader_version, DWORD code)
110 DWORD i = 0;
112 /** TODO: use dichotomic search */
113 while (opcode_table[i].name)
115 if ((code & WINED3DSI_OPCODE_MASK) == opcode_table[i].opcode
116 && shader_version >= opcode_table[i].min_version
117 && (!opcode_table[i].max_version || shader_version <= opcode_table[i].max_version))
119 return &opcode_table[i];
121 ++i;
124 FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n",
125 code, code, code & WINED3DSI_OPCODE_MASK, shader_version);
127 return NULL;
130 /* Read a parameter opcode from the input stream,
131 * and possibly a relative addressing token.
132 * Return the number of tokens read */
133 static int shader_get_param(const DWORD *pToken, DWORD shader_version, DWORD *param, DWORD *addr_token)
135 UINT count = 1;
137 *param = *pToken;
139 /* PS >= 3.0 have relative addressing (with token)
140 * VS >= 2.0 have relative addressing (with token)
141 * VS >= 1.0 < 2.0 have relative addressing (without token)
142 * The version check below should work in general */
143 if (*pToken & WINED3DSHADER_ADDRMODE_RELATIVE)
145 if (WINED3DSHADER_VERSION_MAJOR(shader_version) < 2)
147 *addr_token = (1 << 31)
148 | ((WINED3DSPR_ADDR << WINED3DSP_REGTYPE_SHIFT2) & WINED3DSP_REGTYPE_MASK2)
149 | ((WINED3DSPR_ADDR << WINED3DSP_REGTYPE_SHIFT) & WINED3DSP_REGTYPE_MASK)
150 | WINED3DSP_NOSWIZZLE;
152 else
154 *addr_token = *(pToken + 1);
155 ++count;
159 return count;
162 /* Return the number of parameters to skip for an opcode */
163 static inline int shader_skip_opcode(const SHADER_OPCODE *curOpcode, DWORD opcode_token, DWORD shader_version)
165 /* Shaders >= 2.0 may contain address tokens, but fortunately they
166 * have a useful length mask - use it here. Shaders 1.0 contain no such tokens */
167 return (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 2)
168 ? ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT) : curOpcode->num_params;
171 /* Read the parameters of an unrecognized opcode from the input stream
172 * Return the number of tokens read.
174 * Note: This function assumes source or destination token format.
175 * It will not work with specially-formatted tokens like DEF or DCL,
176 * but hopefully those would be recognized */
177 static int shader_skip_unrecognized(const DWORD *pToken, DWORD shader_version)
179 int tokens_read = 0;
180 int i = 0;
182 /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
183 while (*pToken & 0x80000000) {
185 DWORD param, addr_token = 0;
186 tokens_read += shader_get_param(pToken, shader_version, &param, &addr_token);
187 pToken += tokens_read;
189 FIXME("Unrecognized opcode param: token=0x%08x "
190 "addr_token=0x%08x name=", param, addr_token);
191 shader_dump_param(param, addr_token, i, shader_version);
192 FIXME("\n");
193 ++i;
195 return tokens_read;
198 /* Convert floating point offset relative
199 * to a register file to an absolute offset for float constants */
200 static unsigned int shader_get_float_offset(const DWORD reg)
202 unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
203 int regtype = shader_get_regtype(reg);
205 switch (regtype) {
206 case WINED3DSPR_CONST: return regnum;
207 case WINED3DSPR_CONST2: return 2048 + regnum;
208 case WINED3DSPR_CONST3: return 4096 + regnum;
209 case WINED3DSPR_CONST4: return 6144 + regnum;
210 default:
211 FIXME("Unsupported register type: %d\n", regtype);
212 return regnum;
216 static void shader_delete_constant_list(struct list* clist) {
218 struct list *ptr;
219 struct local_constant* constant;
221 ptr = list_head(clist);
222 while (ptr) {
223 constant = LIST_ENTRY(ptr, struct local_constant, entry);
224 ptr = list_next(clist, ptr);
225 HeapFree(GetProcessHeap(), 0, constant);
227 list_init(clist);
230 static void shader_parse_dst_param(DWORD param, const struct wined3d_shader_src_param *rel_addr,
231 struct wined3d_shader_dst_param *dst)
233 dst->register_type = ((param & WINED3DSP_REGTYPE_MASK) >> WINED3DSP_REGTYPE_SHIFT)
234 | ((param & WINED3DSP_REGTYPE_MASK2) >> WINED3DSP_REGTYPE_SHIFT2);
235 dst->register_idx = param & WINED3DSP_REGNUM_MASK;
236 dst->write_mask = param & WINED3DSP_WRITEMASK_ALL;
237 dst->modifiers = param & WINED3DSP_DSTMOD_MASK;
238 dst->shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
239 dst->rel_addr = rel_addr;
242 static void shader_parse_src_param(DWORD param, const struct wined3d_shader_src_param *rel_addr,
243 struct wined3d_shader_src_param *src)
245 src->register_type = ((param & WINED3DSP_REGTYPE_MASK) >> WINED3DSP_REGTYPE_SHIFT)
246 | ((param & WINED3DSP_REGTYPE_MASK2) >> WINED3DSP_REGTYPE_SHIFT2);
247 src->register_idx = param & WINED3DSP_REGNUM_MASK;
248 src->swizzle = param & WINED3DSP_SWIZZLE_MASK;
249 src->modifiers = param & WINED3DSP_SRCMOD_MASK;
250 src->rel_addr = rel_addr;
253 /* Note that this does not count the loop register
254 * as an address register. */
256 HRESULT shader_get_registers_used(IWineD3DBaseShader *iface, struct shader_reg_maps *reg_maps,
257 struct wined3d_shader_semantic *semantics_in, struct wined3d_shader_semantic *semantics_out,
258 const DWORD *byte_code)
260 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
261 const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;
262 DWORD shader_version;
263 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
264 const DWORD* pToken = byte_code;
265 char pshader;
267 /* There are some minor differences between pixel and vertex shaders */
269 memset(reg_maps->bumpmat, 0, sizeof(reg_maps->bumpmat));
270 memset(reg_maps->luminanceparams, 0, sizeof(reg_maps->luminanceparams));
272 /* get_registers_used is called on every compile on some 1.x shaders, which can result
273 * in stacking up a collection of local constants. Delete the old constants if existing
275 shader_delete_constant_list(&This->baseShader.constantsF);
276 shader_delete_constant_list(&This->baseShader.constantsB);
277 shader_delete_constant_list(&This->baseShader.constantsI);
279 /* The version token is supposed to be the first token */
280 if (!shader_is_version_token(*pToken))
282 FIXME("First token is not a version token, invalid shader.\n");
283 return WINED3DERR_INVALIDCALL;
285 reg_maps->shader_version = shader_version = *pToken++;
286 pshader = shader_is_pshader_version(shader_version);
288 while (WINED3DVS_END() != *pToken) {
289 CONST SHADER_OPCODE* curOpcode;
290 DWORD opcode_token;
292 /* Skip comments */
293 if (shader_is_comment(*pToken))
295 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
296 ++pToken;
297 pToken += comment_len;
298 continue;
301 /* Fetch opcode */
302 opcode_token = *pToken++;
303 curOpcode = shader_get_opcode(shader_ins, shader_version, opcode_token);
305 /* Unhandled opcode, and its parameters */
306 if (NULL == curOpcode) {
307 while (*pToken & 0x80000000)
308 ++pToken;
310 /* Handle declarations */
311 } else if (WINED3DSIO_DCL == curOpcode->opcode) {
313 DWORD usage = *pToken++;
314 DWORD param = *pToken++;
315 DWORD regtype = shader_get_regtype(param);
316 unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
318 /* Vshader: mark attributes used
319 Pshader: mark 3.0 input registers used, save token */
320 if (WINED3DSPR_INPUT == regtype) {
322 if (!pshader)
323 reg_maps->attributes[regnum] = 1;
324 else
325 reg_maps->packed_input[regnum] = 1;
327 semantics_in[regnum].usage = (usage & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
328 semantics_in[regnum].usage_idx =
329 (usage & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
330 shader_parse_dst_param(param, NULL, &semantics_in[regnum].reg);
332 /* Vshader: mark 3.0 output registers used, save token */
333 } else if (WINED3DSPR_OUTPUT == regtype) {
334 reg_maps->packed_output[regnum] = 1;
335 semantics_out[regnum].usage = (usage & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
336 semantics_out[regnum].usage_idx =
337 (usage & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
338 shader_parse_dst_param(param, NULL, &semantics_out[regnum].reg);
340 if (usage & (WINED3DDECLUSAGE_FOG << WINED3DSP_DCL_USAGE_SHIFT))
341 reg_maps->fog = 1;
343 /* Save sampler usage token */
344 } else if (WINED3DSPR_SAMPLER == regtype)
345 reg_maps->samplers[regnum] = usage;
347 } else if (WINED3DSIO_DEF == curOpcode->opcode) {
349 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
350 if (!lconst) return E_OUTOFMEMORY;
351 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
352 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
354 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
355 if (WINED3DSHADER_VERSION_MAJOR(shader_version) == 1 && pshader)
357 float *value = (float *) lconst->value;
358 if(value[0] < -1.0) value[0] = -1.0;
359 else if(value[0] > 1.0) value[0] = 1.0;
360 if(value[1] < -1.0) value[1] = -1.0;
361 else if(value[1] > 1.0) value[1] = 1.0;
362 if(value[2] < -1.0) value[2] = -1.0;
363 else if(value[2] > 1.0) value[2] = 1.0;
364 if(value[3] < -1.0) value[3] = -1.0;
365 else if(value[3] > 1.0) value[3] = 1.0;
368 list_add_head(&This->baseShader.constantsF, &lconst->entry);
369 pToken += curOpcode->num_params;
371 } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
373 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
374 if (!lconst) return E_OUTOFMEMORY;
375 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
376 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
377 list_add_head(&This->baseShader.constantsI, &lconst->entry);
378 pToken += curOpcode->num_params;
380 } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
382 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
383 if (!lconst) return E_OUTOFMEMORY;
384 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
385 memcpy(lconst->value, pToken + 1, 1 * sizeof(DWORD));
386 list_add_head(&This->baseShader.constantsB, &lconst->entry);
387 pToken += curOpcode->num_params;
389 /* If there's a loop in the shader */
390 } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
391 WINED3DSIO_REP == curOpcode->opcode) {
392 cur_loop_depth++;
393 if(cur_loop_depth > max_loop_depth)
394 max_loop_depth = cur_loop_depth;
395 pToken += curOpcode->num_params;
397 /* Rep and Loop always use an integer constant for the control parameters */
398 This->baseShader.uses_int_consts = TRUE;
399 } else if (WINED3DSIO_ENDLOOP == curOpcode->opcode ||
400 WINED3DSIO_ENDREP == curOpcode->opcode) {
401 cur_loop_depth--;
403 /* For subroutine prototypes */
404 } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
406 DWORD snum = *pToken & WINED3DSP_REGNUM_MASK;
407 reg_maps->labels[snum] = 1;
408 pToken += curOpcode->num_params;
410 /* Set texture, address, temporary registers */
411 } else {
412 int i, limit;
414 /* Declare 1.X samplers implicitly, based on the destination reg. number */
415 if (WINED3DSHADER_VERSION_MAJOR(shader_version) == 1
416 && pshader /* Filter different instructions with the same enum values in VS */
417 && (WINED3DSIO_TEX == curOpcode->opcode
418 || WINED3DSIO_TEXBEM == curOpcode->opcode
419 || WINED3DSIO_TEXBEML == curOpcode->opcode
420 || WINED3DSIO_TEXDP3TEX == curOpcode->opcode
421 || WINED3DSIO_TEXM3x2TEX == curOpcode->opcode
422 || WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode
423 || WINED3DSIO_TEXM3x3TEX == curOpcode->opcode
424 || WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode
425 || WINED3DSIO_TEXREG2AR == curOpcode->opcode
426 || WINED3DSIO_TEXREG2GB == curOpcode->opcode
427 || WINED3DSIO_TEXREG2RGB == curOpcode->opcode))
429 /* Fake sampler usage, only set reserved bit and ttype */
430 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
432 TRACE("Setting fake 2D sampler for 1.x pixelshader\n");
433 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
435 /* texbem is only valid with < 1.4 pixel shaders */
436 if(WINED3DSIO_TEXBEM == curOpcode->opcode ||
437 WINED3DSIO_TEXBEML == curOpcode->opcode) {
438 reg_maps->bumpmat[sampler_code] = TRUE;
439 if(WINED3DSIO_TEXBEML == curOpcode->opcode) {
440 reg_maps->luminanceparams[sampler_code] = TRUE;
444 if(WINED3DSIO_NRM == curOpcode->opcode) {
445 reg_maps->usesnrm = 1;
446 } else if(WINED3DSIO_BEM == curOpcode->opcode && pshader) {
447 DWORD regnum = *pToken & WINED3DSP_REGNUM_MASK;
448 reg_maps->bumpmat[regnum] = TRUE;
449 } else if(WINED3DSIO_DSY == curOpcode->opcode) {
450 reg_maps->usesdsy = 1;
453 /* This will loop over all the registers and try to
454 * make a bitmask of the ones we're interested in.
456 * Relative addressing tokens are ignored, but that's
457 * okay, since we'll catch any address registers when
458 * they are initialized (required by spec) */
460 limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
461 curOpcode->num_params + 1: curOpcode->num_params;
463 for (i = 0; i < limit; ++i) {
465 DWORD param, addr_token, reg, regtype;
466 pToken += shader_get_param(pToken, shader_version, &param, &addr_token);
468 regtype = shader_get_regtype(param);
469 reg = param & WINED3DSP_REGNUM_MASK;
471 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
473 if (pshader)
474 reg_maps->texcoord[reg] = 1;
475 else
476 reg_maps->address[reg] = 1;
479 else if (WINED3DSPR_TEMP == regtype)
480 reg_maps->temporary[reg] = 1;
482 else if (WINED3DSPR_INPUT == regtype) {
483 if( !pshader)
484 reg_maps->attributes[reg] = 1;
485 else {
486 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
487 /* If relative addressing is used, we must assume that all registers
488 * are used. Even if it is a construct like v3[aL], we can't assume
489 * that v0, v1 and v2 aren't read because aL can be negative
491 unsigned int i;
492 for(i = 0; i < MAX_REG_INPUT; i++) {
493 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[i] = TRUE;
495 } else {
496 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[reg] = TRUE;
501 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
502 reg_maps->fog = 1;
504 else if (WINED3DSPR_MISCTYPE == regtype && reg == 0 && pshader)
505 reg_maps->vpos = 1;
507 else if(WINED3DSPR_CONST == regtype) {
508 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
509 if(!pshader) {
510 if(reg <= ((IWineD3DVertexShaderImpl *) This)->min_rel_offset) {
511 ((IWineD3DVertexShaderImpl *) This)->min_rel_offset = reg;
512 } else if(reg >= ((IWineD3DVertexShaderImpl *) This)->max_rel_offset) {
513 ((IWineD3DVertexShaderImpl *) This)->max_rel_offset = reg;
516 reg_maps->usesrelconstF = TRUE;
519 else if(WINED3DSPR_CONSTINT == regtype) {
520 This->baseShader.uses_int_consts = TRUE;
522 else if(WINED3DSPR_CONSTBOOL == regtype) {
523 This->baseShader.uses_bool_consts = TRUE;
526 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and is used
527 * in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel shaders because TECRDOUT
528 * isn't used in them, but future register types might cause issues
530 else if (WINED3DSPR_TEXCRDOUT == regtype && i == 0 /* Only look at writes */
531 && !pshader && WINED3DSHADER_VERSION_MAJOR(shader_version) < 3)
533 reg_maps->texcoord_mask[reg] |= shader_get_writemask(param);
538 ++pToken;
539 reg_maps->loop_depth = max_loop_depth;
541 This->baseShader.functionLength = ((char *)pToken - (char *)byte_code);
543 return WINED3D_OK;
546 static void shader_dump_decl_usage(DWORD decl, DWORD param, DWORD shader_version)
548 DWORD regtype = shader_get_regtype(param);
550 TRACE("dcl");
552 if (regtype == WINED3DSPR_SAMPLER) {
553 DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
555 switch (ttype) {
556 case WINED3DSTT_2D: TRACE("_2d"); break;
557 case WINED3DSTT_CUBE: TRACE("_cube"); break;
558 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
559 default: TRACE("_unknown_ttype(0x%08x)", ttype);
562 } else {
564 DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
565 DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
567 /* Pixel shaders 3.0 don't have usage semantics */
568 if (shader_is_pshader_version(shader_version) && shader_version < WINED3DPS_VERSION(3,0))
569 return;
570 else
571 TRACE("_");
573 switch(usage) {
574 case WINED3DDECLUSAGE_POSITION:
575 TRACE("position%d", idx);
576 break;
577 case WINED3DDECLUSAGE_BLENDINDICES:
578 TRACE("blend");
579 break;
580 case WINED3DDECLUSAGE_BLENDWEIGHT:
581 TRACE("weight");
582 break;
583 case WINED3DDECLUSAGE_NORMAL:
584 TRACE("normal%d", idx);
585 break;
586 case WINED3DDECLUSAGE_PSIZE:
587 TRACE("psize");
588 break;
589 case WINED3DDECLUSAGE_COLOR:
590 if(idx == 0) {
591 TRACE("color");
592 } else {
593 TRACE("specular%d", (idx - 1));
595 break;
596 case WINED3DDECLUSAGE_TEXCOORD:
597 TRACE("texture%d", idx);
598 break;
599 case WINED3DDECLUSAGE_TANGENT:
600 TRACE("tangent");
601 break;
602 case WINED3DDECLUSAGE_BINORMAL:
603 TRACE("binormal");
604 break;
605 case WINED3DDECLUSAGE_TESSFACTOR:
606 TRACE("tessfactor");
607 break;
608 case WINED3DDECLUSAGE_POSITIONT:
609 TRACE("positionT%d", idx);
610 break;
611 case WINED3DDECLUSAGE_FOG:
612 TRACE("fog");
613 break;
614 case WINED3DDECLUSAGE_DEPTH:
615 TRACE("depth");
616 break;
617 case WINED3DDECLUSAGE_SAMPLE:
618 TRACE("sample");
619 break;
620 default:
621 FIXME("unknown_semantics(0x%08x)", usage);
626 static void shader_dump_arr_entry(const DWORD param, const DWORD addr_token,
627 unsigned int reg, int input, DWORD shader_version)
629 char relative =
630 ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
632 if (relative) {
633 TRACE("[");
634 if (addr_token)
635 shader_dump_param(addr_token, 0, input, shader_version);
636 else
637 TRACE("a0.x");
638 TRACE(" + ");
640 TRACE("%u", reg);
641 if (relative)
642 TRACE("]");
645 static void shader_dump_param(const DWORD param, const DWORD addr_token, int input, DWORD shader_version)
647 static const char * const rastout_reg_names[] = { "oPos", "oFog", "oPts" };
648 static const char * const misctype_reg_names[] = { "vPos", "vFace"};
649 const char *swizzle_reg_chars = "xyzw";
651 DWORD reg = param & WINED3DSP_REGNUM_MASK;
652 DWORD regtype = shader_get_regtype(param);
653 DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
655 /* There are some minor differences between pixel and vertex shaders */
656 char pshader = shader_is_pshader_version(shader_version);
658 if (input) {
659 if ( (modifier == WINED3DSPSM_NEG) ||
660 (modifier == WINED3DSPSM_BIASNEG) ||
661 (modifier == WINED3DSPSM_SIGNNEG) ||
662 (modifier == WINED3DSPSM_X2NEG) ||
663 (modifier == WINED3DSPSM_ABSNEG) )
664 TRACE("-");
665 else if (modifier == WINED3DSPSM_COMP)
666 TRACE("1-");
667 else if (modifier == WINED3DSPSM_NOT)
668 TRACE("!");
670 if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG)
671 TRACE("abs(");
674 switch (regtype) {
675 case WINED3DSPR_TEMP:
676 TRACE("r%u", reg);
677 break;
678 case WINED3DSPR_INPUT:
679 TRACE("v");
680 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
681 break;
682 case WINED3DSPR_CONST:
683 case WINED3DSPR_CONST2:
684 case WINED3DSPR_CONST3:
685 case WINED3DSPR_CONST4:
686 TRACE("c");
687 shader_dump_arr_entry(param, addr_token, shader_get_float_offset(param), input, shader_version);
688 break;
689 case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
690 TRACE("%c%u", (pshader? 't':'a'), reg);
691 break;
692 case WINED3DSPR_RASTOUT:
693 TRACE("%s", rastout_reg_names[reg]);
694 break;
695 case WINED3DSPR_COLOROUT:
696 TRACE("oC%u", reg);
697 break;
698 case WINED3DSPR_DEPTHOUT:
699 TRACE("oDepth");
700 break;
701 case WINED3DSPR_ATTROUT:
702 TRACE("oD%u", reg);
703 break;
704 case WINED3DSPR_TEXCRDOUT:
706 /* Vertex shaders >= 3.0 use general purpose output registers
707 * (WINED3DSPR_OUTPUT), which can include an address token */
709 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3) {
710 TRACE("o");
711 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
713 else
714 TRACE("oT%u", reg);
715 break;
716 case WINED3DSPR_CONSTINT:
717 TRACE("i");
718 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
719 break;
720 case WINED3DSPR_CONSTBOOL:
721 TRACE("b");
722 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
723 break;
724 case WINED3DSPR_LABEL:
725 TRACE("l%u", reg);
726 break;
727 case WINED3DSPR_LOOP:
728 TRACE("aL");
729 break;
730 case WINED3DSPR_SAMPLER:
731 TRACE("s%u", reg);
732 break;
733 case WINED3DSPR_MISCTYPE:
734 if (reg > 1) {
735 FIXME("Unhandled misctype register %d\n", reg);
736 } else {
737 TRACE("%s", misctype_reg_names[reg]);
739 break;
740 case WINED3DSPR_PREDICATE:
741 TRACE("p%u", reg);
742 break;
743 default:
744 TRACE("unhandled_rtype(%#x)", regtype);
745 break;
748 if (!input) {
749 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
751 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
752 TRACE(".");
753 if (param & WINED3DSP_WRITEMASK_0) TRACE("%c", swizzle_reg_chars[0]);
754 if (param & WINED3DSP_WRITEMASK_1) TRACE("%c", swizzle_reg_chars[1]);
755 if (param & WINED3DSP_WRITEMASK_2) TRACE("%c", swizzle_reg_chars[2]);
756 if (param & WINED3DSP_WRITEMASK_3) TRACE("%c", swizzle_reg_chars[3]);
759 } else {
760 /** operand input */
761 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
762 DWORD swizzle_x = swizzle & 0x03;
763 DWORD swizzle_y = (swizzle >> 2) & 0x03;
764 DWORD swizzle_z = (swizzle >> 4) & 0x03;
765 DWORD swizzle_w = (swizzle >> 6) & 0x03;
767 if (0 != modifier) {
768 switch (modifier) {
769 case WINED3DSPSM_NONE: break;
770 case WINED3DSPSM_NEG: break;
771 case WINED3DSPSM_NOT: break;
772 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
773 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
774 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
775 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
776 case WINED3DSPSM_COMP: break;
777 case WINED3DSPSM_X2: TRACE("_x2"); break;
778 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
779 case WINED3DSPSM_DZ: TRACE("_dz"); break;
780 case WINED3DSPSM_DW: TRACE("_dw"); break;
781 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
782 case WINED3DSPSM_ABS: TRACE(")"); break;
783 default:
784 TRACE("_unknown_modifier(%#x)", modifier >> WINED3DSP_SRCMOD_SHIFT);
789 * swizzle bits fields:
790 * RRGGBBAA
792 if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) != swizzle) {
793 if (swizzle_x == swizzle_y &&
794 swizzle_x == swizzle_z &&
795 swizzle_x == swizzle_w) {
796 TRACE(".%c", swizzle_reg_chars[swizzle_x]);
797 } else {
798 TRACE(".%c%c%c%c",
799 swizzle_reg_chars[swizzle_x],
800 swizzle_reg_chars[swizzle_y],
801 swizzle_reg_chars[swizzle_z],
802 swizzle_reg_chars[swizzle_w]);
808 /* Shared code in order to generate the bulk of the shader string.
809 * NOTE: A description of how to parse tokens can be found on msdn */
810 void shader_generate_main(IWineD3DBaseShader *iface, SHADER_BUFFER* buffer,
811 const shader_reg_maps* reg_maps, CONST DWORD* pFunction)
813 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
814 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device; /* To access shader backend callbacks */
815 const SHADER_OPCODE *opcode_table = This->baseShader.shader_ins;
816 const SHADER_HANDLER *handler_table = device->shader_backend->shader_instruction_handler_table;
817 DWORD shader_version = reg_maps->shader_version;
818 struct wined3d_shader_src_param src_rel_addr[4];
819 struct wined3d_shader_src_param src_param[4];
820 struct wined3d_shader_src_param dst_rel_addr;
821 struct wined3d_shader_dst_param dst_param;
822 struct wined3d_shader_instruction ins;
823 struct wined3d_shader_context ctx;
824 const DWORD *pToken = pFunction;
825 const SHADER_OPCODE *curOpcode;
826 SHADER_HANDLER hw_fct;
827 DWORD i;
829 /* Initialize current parsing state */
830 ctx.shader = iface;
831 ctx.reg_maps = reg_maps;
832 ctx.buffer = buffer;
834 ins.ctx = &ctx;
835 ins.dst = &dst_param;
836 ins.src = src_param;
837 This->baseShader.parse_state.current_row = 0;
839 while (WINED3DPS_END() != *pToken)
841 DWORD opcode_token;
843 /* Skip version token */
844 if (shader_is_version_token(*pToken))
846 ++pToken;
847 continue;
850 /* Skip comment tokens */
851 if (shader_is_comment(*pToken))
853 pToken += (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
854 ++pToken;
855 continue;
858 /* Read opcode */
859 opcode_token = *pToken++;
860 curOpcode = shader_get_opcode(opcode_table, shader_version, opcode_token);
862 /* Unknown opcode and its parameters */
863 if (!curOpcode)
865 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
866 pToken += shader_skip_unrecognized(pToken, shader_version);
867 continue;
870 /* Nothing to do */
871 if (WINED3DSIO_DCL == curOpcode->opcode
872 || WINED3DSIO_NOP == curOpcode->opcode
873 || WINED3DSIO_DEF == curOpcode->opcode
874 || WINED3DSIO_DEFI == curOpcode->opcode
875 || WINED3DSIO_DEFB == curOpcode->opcode
876 || WINED3DSIO_PHASE == curOpcode->opcode
877 || WINED3DSIO_RET == curOpcode->opcode)
879 pToken += shader_skip_opcode(curOpcode, opcode_token, shader_version);
880 continue;
883 /* Select handler */
884 hw_fct = handler_table[curOpcode->handler_idx];
886 /* Unhandled opcode */
887 if (!hw_fct)
889 FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
890 pToken += shader_skip_opcode(curOpcode, opcode_token, shader_version);
891 continue;
894 ins.handler_idx = curOpcode->handler_idx;
895 ins.flags = opcode_token & WINED3D_OPCODESPECIFICCONTROL_MASK;
896 ins.coissue = opcode_token & WINED3DSI_COISSUE;
898 /* Destination token */
899 ins.dst_count = curOpcode->dst_token ? 1 : 0;
900 if (ins.dst_count)
902 DWORD param, addr_param;
903 pToken += shader_get_param(pToken, shader_version, &param, &addr_param);
905 if (param & WINED3DSHADER_ADDRMODE_RELATIVE)
907 shader_parse_src_param(addr_param, NULL, &dst_rel_addr);
908 shader_parse_dst_param(param, &dst_rel_addr, &dst_param);
910 else
912 shader_parse_dst_param(param, NULL, &dst_param);
916 /* Predication token */
917 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) ins.predicate = *pToken++;
919 /* Other source tokens */
920 ins.src_count = curOpcode->num_params - curOpcode->dst_token;
921 for (i = 0; i < ins.src_count; ++i)
923 DWORD param, addr_param;
924 pToken += shader_get_param(pToken, shader_version, &param, &addr_param);
925 if (param & WINED3DSHADER_ADDRMODE_RELATIVE)
927 shader_parse_src_param(addr_param, NULL, &src_rel_addr[i]);
928 shader_parse_src_param(param, &src_rel_addr[i], &src_param[i]);
930 else
932 shader_parse_src_param(param, NULL, &src_param[i]);
936 /* Call appropriate function for output target */
937 hw_fct(&ins);
939 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
940 /* FIXME: This should be internal to the shader backend.
941 * Also, right now this is the only reason "shader_mode" exists. */
942 if (This->baseShader.shader_mode == SHADER_GLSL) shader_glsl_add_instruction_modifiers(&ins);
946 static void shader_dump_ins_modifiers(const DWORD output)
948 DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
949 DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
951 switch (shift) {
952 case 0: break;
953 case 13: TRACE("_d8"); break;
954 case 14: TRACE("_d4"); break;
955 case 15: TRACE("_d2"); break;
956 case 1: TRACE("_x2"); break;
957 case 2: TRACE("_x4"); break;
958 case 3: TRACE("_x8"); break;
959 default: TRACE("_unhandled_shift(%d)", shift); break;
962 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
963 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
964 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
966 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
967 if (mmask)
968 FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
971 void shader_trace_init(const DWORD *pFunction, const SHADER_OPCODE *opcode_table)
973 const DWORD* pToken = pFunction;
974 const SHADER_OPCODE* curOpcode = NULL;
975 DWORD shader_version;
976 DWORD opcode_token;
977 DWORD i;
979 TRACE("Parsing %p\n", pFunction);
981 /* The version token is supposed to be the first token */
982 if (!shader_is_version_token(*pToken))
984 FIXME("First token is not a version token, invalid shader.\n");
985 return;
987 shader_version = *pToken++;
988 TRACE("%s_%u_%u\n", shader_is_pshader_version(shader_version) ? "ps": "vs",
989 WINED3DSHADER_VERSION_MAJOR(shader_version), WINED3DSHADER_VERSION_MINOR(shader_version));
991 while (WINED3DVS_END() != *pToken)
993 if (shader_is_comment(*pToken)) /* comment */
995 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
996 ++pToken;
997 TRACE("//%s\n", (const char*)pToken);
998 pToken += comment_len;
999 continue;
1001 opcode_token = *pToken++;
1002 curOpcode = shader_get_opcode(opcode_table, shader_version, opcode_token);
1004 if (!curOpcode)
1006 int tokens_read;
1007 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
1008 tokens_read = shader_skip_unrecognized(pToken, shader_version);
1009 pToken += tokens_read;
1011 else
1013 if (curOpcode->opcode == WINED3DSIO_DCL)
1015 DWORD usage = *pToken;
1016 DWORD param = *(pToken + 1);
1018 shader_dump_decl_usage(usage, param, shader_version);
1019 shader_dump_ins_modifiers(param);
1020 TRACE(" ");
1021 shader_dump_param(param, 0, 0, shader_version);
1022 pToken += 2;
1024 else if (curOpcode->opcode == WINED3DSIO_DEF)
1026 unsigned int offset = shader_get_float_offset(*pToken);
1028 TRACE("def c%u = %f, %f, %f, %f", offset,
1029 *(const float *)(pToken + 1),
1030 *(const float *)(pToken + 2),
1031 *(const float *)(pToken + 3),
1032 *(const float *)(pToken + 4));
1033 pToken += 5;
1035 else if (curOpcode->opcode == WINED3DSIO_DEFI)
1037 TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
1038 *(pToken + 1),
1039 *(pToken + 2),
1040 *(pToken + 3),
1041 *(pToken + 4));
1042 pToken += 5;
1044 else if (curOpcode->opcode == WINED3DSIO_DEFB)
1046 TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
1047 *(pToken + 1)? "true": "false");
1048 pToken += 2;
1050 else
1052 DWORD param, addr_token = 0;
1053 int tokens_read;
1055 /* Print out predication source token first - it follows
1056 * the destination token. */
1057 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
1059 TRACE("(");
1060 shader_dump_param(*(pToken + 2), 0, 1, shader_version);
1061 TRACE(") ");
1063 if (opcode_token & WINED3DSI_COISSUE)
1065 /* PixWin marks instructions with the coissue flag with a '+' */
1066 TRACE("+");
1069 TRACE("%s", curOpcode->name);
1071 if (curOpcode->opcode == WINED3DSIO_IFC
1072 || curOpcode->opcode == WINED3DSIO_BREAKC)
1074 DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1076 switch (op)
1078 case COMPARISON_GT: TRACE("_gt"); break;
1079 case COMPARISON_EQ: TRACE("_eq"); break;
1080 case COMPARISON_GE: TRACE("_ge"); break;
1081 case COMPARISON_LT: TRACE("_lt"); break;
1082 case COMPARISON_NE: TRACE("_ne"); break;
1083 case COMPARISON_LE: TRACE("_le"); break;
1084 default: TRACE("_(%u)", op);
1087 else if (curOpcode->opcode == WINED3DSIO_TEX
1088 && shader_version >= WINED3DPS_VERSION(2,0)
1089 && (opcode_token & WINED3DSI_TEXLD_PROJECT))
1091 TRACE("p");
1094 /* Destination token */
1095 if (curOpcode->dst_token)
1097 tokens_read = shader_get_param(pToken, shader_version, &param, &addr_token);
1098 pToken += tokens_read;
1100 shader_dump_ins_modifiers(param);
1101 TRACE(" ");
1102 shader_dump_param(param, addr_token, 0, shader_version);
1105 /* Predication token - already printed out, just skip it */
1106 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
1108 pToken++;
1111 /* Other source tokens */
1112 for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i)
1114 tokens_read = shader_get_param(pToken, shader_version, &param, &addr_token);
1115 pToken += tokens_read;
1117 TRACE((i == 0)? " " : ", ");
1118 shader_dump_param(param, addr_token, 1, shader_version);
1121 TRACE("\n");
1126 void shader_cleanup(IWineD3DBaseShader *iface)
1128 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1130 ((IWineD3DDeviceImpl *)This->baseShader.device)->shader_backend->shader_destroy(iface);
1131 HeapFree(GetProcessHeap(), 0, This->baseShader.function);
1132 shader_delete_constant_list(&This->baseShader.constantsF);
1133 shader_delete_constant_list(&This->baseShader.constantsB);
1134 shader_delete_constant_list(&This->baseShader.constantsI);
1135 list_remove(&This->baseShader.shader_list_entry);
1138 static const SHADER_HANDLER shader_none_instruction_handler_table[WINED3DSIH_TABLE_SIZE] = {0};
1139 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
1140 static void shader_none_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {}
1141 static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1142 static void shader_none_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1143 static void shader_none_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1144 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1145 static void shader_none_load_np2fixup_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1146 static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1147 static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1148 static void shader_none_free(IWineD3DDevice *iface) {}
1149 static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1150 static GLuint shader_none_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer, const struct ps_compile_args *args) {
1151 FIXME("NONE shader backend asked to generate a pixel shader\n");
1152 return 0;
1154 static GLuint shader_none_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer, const struct vs_compile_args *args) {
1155 FIXME("NONE shader backend asked to generate a vertex shader\n");
1156 return 0;
1159 #define GLINFO_LOCATION (*gl_info)
1160 static void shader_none_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
1162 /* Set the shader caps to 0 for the none shader backend */
1163 pCaps->VertexShaderVersion = 0;
1164 pCaps->PixelShaderVersion = 0;
1165 pCaps->PixelShader1xMaxValue = 0.0;
1167 #undef GLINFO_LOCATION
1168 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1170 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1172 TRACE("Checking support for fixup:\n");
1173 dump_color_fixup_desc(fixup);
1176 /* Faked to make some apps happy. */
1177 if (!is_yuv_fixup(fixup))
1179 TRACE("[OK]\n");
1180 return TRUE;
1183 TRACE("[FAILED]\n");
1184 return FALSE;
1187 const shader_backend_t none_shader_backend = {
1188 shader_none_instruction_handler_table,
1189 shader_none_select,
1190 shader_none_select_depth_blt,
1191 shader_none_deselect_depth_blt,
1192 shader_none_update_float_vertex_constants,
1193 shader_none_update_float_pixel_constants,
1194 shader_none_load_constants,
1195 shader_none_load_np2fixup_constants,
1196 shader_none_destroy,
1197 shader_none_alloc,
1198 shader_none_free,
1199 shader_none_dirty_const,
1200 shader_none_generate_pshader,
1201 shader_none_generate_vshader,
1202 shader_none_get_caps,
1203 shader_none_color_fixup_supported,