msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / wined3d / baseshader.c
bloba270b0c49cbc6e3f17904d67cdf6e4f32f6203c5
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 #define GLNAME_REQUIRE_GLSL ((const char *)1)
36 static void shader_dump_param(const DWORD param, const DWORD addr_token, int input, DWORD shader_version);
38 static inline BOOL shader_is_version_token(DWORD token) {
39 return shader_is_pshader_version(token) ||
40 shader_is_vshader_version(token);
43 void shader_buffer_init(struct SHADER_BUFFER *buffer)
45 buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
46 buffer->buffer[0] = '\0';
47 buffer->bsize = 0;
48 buffer->lineNo = 0;
49 buffer->newline = TRUE;
52 void shader_buffer_free(struct SHADER_BUFFER *buffer)
54 HeapFree(GetProcessHeap(), 0, buffer->buffer);
57 int shader_addline(
58 SHADER_BUFFER* buffer,
59 const char *format, ...) {
61 char* base = buffer->buffer + buffer->bsize;
62 int rc;
64 va_list args;
65 va_start(args, format);
66 rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);
67 va_end(args);
69 if (rc < 0 || /* C89 */
70 rc > SHADER_PGMSIZE - 1 - buffer->bsize) { /* C99 */
72 ERR("The buffer allocated for the shader program string "
73 "is too small at %d bytes.\n", SHADER_PGMSIZE);
74 buffer->bsize = SHADER_PGMSIZE - 1;
75 return -1;
78 if (buffer->newline) {
79 TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
80 buffer->newline = FALSE;
81 } else {
82 TRACE("%s", base);
85 buffer->bsize += rc;
86 if (buffer->buffer[buffer->bsize-1] == '\n') {
87 buffer->lineNo++;
88 buffer->newline = TRUE;
90 return 0;
93 void shader_init(struct IWineD3DBaseShaderClass *shader,
94 IWineD3DDevice *device, const SHADER_OPCODE *instruction_table)
96 shader->ref = 1;
97 shader->device = device;
98 shader->shader_ins = instruction_table;
99 list_init(&shader->linked_programs);
102 const SHADER_OPCODE *shader_get_opcode(const SHADER_OPCODE *opcode_table, DWORD shader_version, DWORD code)
104 DWORD i = 0;
106 /** TODO: use dichotomic search */
107 while (opcode_table[i].name)
109 if ((code & WINED3DSI_OPCODE_MASK) == opcode_table[i].opcode
110 && shader_version >= opcode_table[i].min_version
111 && (!opcode_table[i].max_version || shader_version <= opcode_table[i].max_version))
113 return &opcode_table[i];
115 ++i;
118 FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n",
119 code, code, code & WINED3DSI_OPCODE_MASK, shader_version);
121 return NULL;
124 /* Read a parameter opcode from the input stream,
125 * and possibly a relative addressing token.
126 * Return the number of tokens read */
127 static int shader_get_param(const DWORD *pToken, DWORD shader_version, DWORD *param, DWORD *addr_token)
129 /* PS >= 3.0 have relative addressing (with token)
130 * VS >= 2.0 have relative addressing (with token)
131 * VS >= 1.0 < 2.0 have relative addressing (without token)
132 * The version check below should work in general */
134 char rel_token = WINED3DSHADER_VERSION_MAJOR(shader_version) >= 2 &&
135 ((*pToken & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
137 *param = *pToken;
138 *addr_token = rel_token? *(pToken + 1): 0;
139 return rel_token? 2:1;
142 /* Return the number of parameters to skip for an opcode */
143 static inline int shader_skip_opcode(const SHADER_OPCODE *curOpcode, DWORD opcode_token, DWORD shader_version)
145 /* Shaders >= 2.0 may contain address tokens, but fortunately they
146 * have a useful length mask - use it here. Shaders 1.0 contain no such tokens */
147 return (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 2)
148 ? ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT) : curOpcode->num_params;
151 /* Read the parameters of an unrecognized opcode from the input stream
152 * Return the number of tokens read.
154 * Note: This function assumes source or destination token format.
155 * It will not work with specially-formatted tokens like DEF or DCL,
156 * but hopefully those would be recognized */
157 static int shader_skip_unrecognized(const DWORD *pToken, DWORD shader_version)
159 int tokens_read = 0;
160 int i = 0;
162 /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
163 while (*pToken & 0x80000000) {
165 DWORD param, addr_token;
166 tokens_read += shader_get_param(pToken, shader_version, &param, &addr_token);
167 pToken += tokens_read;
169 FIXME("Unrecognized opcode param: token=0x%08x "
170 "addr_token=0x%08x name=", param, addr_token);
171 shader_dump_param(param, addr_token, i, shader_version);
172 FIXME("\n");
173 ++i;
175 return tokens_read;
178 /* Convert floating point offset relative
179 * to a register file to an absolute offset for float constants */
180 static unsigned int shader_get_float_offset(const DWORD reg)
182 unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
183 int regtype = shader_get_regtype(reg);
185 switch (regtype) {
186 case WINED3DSPR_CONST: return regnum;
187 case WINED3DSPR_CONST2: return 2048 + regnum;
188 case WINED3DSPR_CONST3: return 4096 + regnum;
189 case WINED3DSPR_CONST4: return 6144 + regnum;
190 default:
191 FIXME("Unsupported register type: %d\n", regtype);
192 return regnum;
196 static void shader_delete_constant_list(struct list* clist) {
198 struct list *ptr;
199 struct local_constant* constant;
201 ptr = list_head(clist);
202 while (ptr) {
203 constant = LIST_ENTRY(ptr, struct local_constant, entry);
204 ptr = list_next(clist, ptr);
205 HeapFree(GetProcessHeap(), 0, constant);
207 list_init(clist);
210 /* Note that this does not count the loop register
211 * as an address register. */
213 HRESULT shader_get_registers_used(IWineD3DBaseShader *iface, struct shader_reg_maps *reg_maps,
214 struct semantic *semantics_in, struct semantic *semantics_out, const DWORD *byte_code)
216 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
217 const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;
218 DWORD shader_version;
219 unsigned int cur_loop_depth = 0, max_loop_depth = 0;
220 const DWORD* pToken = byte_code;
221 char pshader;
223 /* There are some minor differences between pixel and vertex shaders */
225 memset(reg_maps->bumpmat, 0, sizeof(reg_maps->bumpmat));
226 memset(reg_maps->luminanceparams, 0, sizeof(reg_maps->luminanceparams));
228 /* get_registers_used is called on every compile on some 1.x shaders, which can result
229 * in stacking up a collection of local constants. Delete the old constants if existing
231 shader_delete_constant_list(&This->baseShader.constantsF);
232 shader_delete_constant_list(&This->baseShader.constantsB);
233 shader_delete_constant_list(&This->baseShader.constantsI);
235 /* The version token is supposed to be the first token */
236 if (!shader_is_version_token(*pToken))
238 FIXME("First token is not a version token, invalid shader.\n");
239 return WINED3DERR_INVALIDCALL;
241 reg_maps->shader_version = shader_version = *pToken++;
242 pshader = shader_is_pshader_version(shader_version);
244 while (WINED3DVS_END() != *pToken) {
245 CONST SHADER_OPCODE* curOpcode;
246 DWORD opcode_token;
248 /* Skip comments */
249 if (shader_is_comment(*pToken))
251 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
252 ++pToken;
253 pToken += comment_len;
254 continue;
257 /* Fetch opcode */
258 opcode_token = *pToken++;
259 curOpcode = shader_get_opcode(shader_ins, shader_version, opcode_token);
261 /* Unhandled opcode, and its parameters */
262 if (NULL == curOpcode) {
263 while (*pToken & 0x80000000)
264 ++pToken;
266 /* Handle declarations */
267 } else if (WINED3DSIO_DCL == curOpcode->opcode) {
269 DWORD usage = *pToken++;
270 DWORD param = *pToken++;
271 DWORD regtype = shader_get_regtype(param);
272 unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
274 /* Vshader: mark attributes used
275 Pshader: mark 3.0 input registers used, save token */
276 if (WINED3DSPR_INPUT == regtype) {
278 if (!pshader)
279 reg_maps->attributes[regnum] = 1;
280 else
281 reg_maps->packed_input[regnum] = 1;
283 semantics_in[regnum].usage = usage;
284 semantics_in[regnum].reg = param;
286 /* Vshader: mark 3.0 output registers used, save token */
287 } else if (WINED3DSPR_OUTPUT == regtype) {
288 reg_maps->packed_output[regnum] = 1;
289 semantics_out[regnum].usage = usage;
290 semantics_out[regnum].reg = param;
291 if (usage & (WINED3DDECLUSAGE_FOG << WINED3DSP_DCL_USAGE_SHIFT))
292 reg_maps->fog = 1;
294 /* Save sampler usage token */
295 } else if (WINED3DSPR_SAMPLER == regtype)
296 reg_maps->samplers[regnum] = usage;
298 } else if (WINED3DSIO_DEF == curOpcode->opcode) {
300 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
301 if (!lconst) return E_OUTOFMEMORY;
302 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
303 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
305 /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
306 if (WINED3DSHADER_VERSION_MAJOR(shader_version) == 1 && pshader)
308 float *value = (float *) lconst->value;
309 if(value[0] < -1.0) value[0] = -1.0;
310 else if(value[0] > 1.0) value[0] = 1.0;
311 if(value[1] < -1.0) value[1] = -1.0;
312 else if(value[1] > 1.0) value[1] = 1.0;
313 if(value[2] < -1.0) value[2] = -1.0;
314 else if(value[2] > 1.0) value[2] = 1.0;
315 if(value[3] < -1.0) value[3] = -1.0;
316 else if(value[3] > 1.0) value[3] = 1.0;
319 list_add_head(&This->baseShader.constantsF, &lconst->entry);
320 pToken += curOpcode->num_params;
322 } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
324 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
325 if (!lconst) return E_OUTOFMEMORY;
326 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
327 memcpy(lconst->value, pToken + 1, 4 * sizeof(DWORD));
328 list_add_head(&This->baseShader.constantsI, &lconst->entry);
329 pToken += curOpcode->num_params;
331 } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
333 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
334 if (!lconst) return E_OUTOFMEMORY;
335 lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
336 memcpy(lconst->value, pToken + 1, 1 * sizeof(DWORD));
337 list_add_head(&This->baseShader.constantsB, &lconst->entry);
338 pToken += curOpcode->num_params;
340 /* If there's a loop in the shader */
341 } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
342 WINED3DSIO_REP == curOpcode->opcode) {
343 cur_loop_depth++;
344 if(cur_loop_depth > max_loop_depth)
345 max_loop_depth = cur_loop_depth;
346 pToken += curOpcode->num_params;
348 /* Rep and Loop always use an integer constant for the control parameters */
349 This->baseShader.uses_int_consts = TRUE;
350 } else if (WINED3DSIO_ENDLOOP == curOpcode->opcode ||
351 WINED3DSIO_ENDREP == curOpcode->opcode) {
352 cur_loop_depth--;
354 /* For subroutine prototypes */
355 } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
357 DWORD snum = *pToken & WINED3DSP_REGNUM_MASK;
358 reg_maps->labels[snum] = 1;
359 pToken += curOpcode->num_params;
361 /* Set texture, address, temporary registers */
362 } else {
363 int i, limit;
365 /* Declare 1.X samplers implicitly, based on the destination reg. number */
366 if (WINED3DSHADER_VERSION_MAJOR(shader_version) == 1
367 && pshader /* Filter different instructions with the same enum values in VS */
368 && (WINED3DSIO_TEX == curOpcode->opcode
369 || WINED3DSIO_TEXBEM == curOpcode->opcode
370 || WINED3DSIO_TEXBEML == curOpcode->opcode
371 || WINED3DSIO_TEXDP3TEX == curOpcode->opcode
372 || WINED3DSIO_TEXM3x2TEX == curOpcode->opcode
373 || WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode
374 || WINED3DSIO_TEXM3x3TEX == curOpcode->opcode
375 || WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode
376 || WINED3DSIO_TEXREG2AR == curOpcode->opcode
377 || WINED3DSIO_TEXREG2GB == curOpcode->opcode
378 || WINED3DSIO_TEXREG2RGB == curOpcode->opcode))
380 /* Fake sampler usage, only set reserved bit and ttype */
381 DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
383 TRACE("Setting fake 2D sampler for 1.x pixelshader\n");
384 reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
386 /* texbem is only valid with < 1.4 pixel shaders */
387 if(WINED3DSIO_TEXBEM == curOpcode->opcode ||
388 WINED3DSIO_TEXBEML == curOpcode->opcode) {
389 reg_maps->bumpmat[sampler_code] = TRUE;
390 if(WINED3DSIO_TEXBEML == curOpcode->opcode) {
391 reg_maps->luminanceparams[sampler_code] = TRUE;
395 if(WINED3DSIO_NRM == curOpcode->opcode) {
396 reg_maps->usesnrm = 1;
397 } else if(WINED3DSIO_BEM == curOpcode->opcode && pshader) {
398 DWORD regnum = *pToken & WINED3DSP_REGNUM_MASK;
399 reg_maps->bumpmat[regnum] = TRUE;
400 } else if(WINED3DSIO_DSY == curOpcode->opcode) {
401 reg_maps->usesdsy = 1;
404 /* This will loop over all the registers and try to
405 * make a bitmask of the ones we're interested in.
407 * Relative addressing tokens are ignored, but that's
408 * okay, since we'll catch any address registers when
409 * they are initialized (required by spec) */
411 limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
412 curOpcode->num_params + 1: curOpcode->num_params;
414 for (i = 0; i < limit; ++i) {
416 DWORD param, addr_token, reg, regtype;
417 pToken += shader_get_param(pToken, shader_version, &param, &addr_token);
419 regtype = shader_get_regtype(param);
420 reg = param & WINED3DSP_REGNUM_MASK;
422 if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
424 if (pshader)
425 reg_maps->texcoord[reg] = 1;
426 else
427 reg_maps->address[reg] = 1;
430 else if (WINED3DSPR_TEMP == regtype)
431 reg_maps->temporary[reg] = 1;
433 else if (WINED3DSPR_INPUT == regtype) {
434 if( !pshader)
435 reg_maps->attributes[reg] = 1;
436 else {
437 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
438 /* If relative addressing is used, we must assume that all registers
439 * are used. Even if it is a construct like v3[aL], we can't assume
440 * that v0, v1 and v2 aren't read because aL can be negative
442 unsigned int i;
443 for(i = 0; i < MAX_REG_INPUT; i++) {
444 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[i] = TRUE;
446 } else {
447 ((IWineD3DPixelShaderImpl *) This)->input_reg_used[reg] = TRUE;
452 else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
453 reg_maps->fog = 1;
455 else if (WINED3DSPR_MISCTYPE == regtype && reg == 0 && pshader)
456 reg_maps->vpos = 1;
458 else if(WINED3DSPR_CONST == regtype) {
459 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
460 if(!pshader) {
461 if(reg <= ((IWineD3DVertexShaderImpl *) This)->min_rel_offset) {
462 ((IWineD3DVertexShaderImpl *) This)->min_rel_offset = reg;
463 } else if(reg >= ((IWineD3DVertexShaderImpl *) This)->max_rel_offset) {
464 ((IWineD3DVertexShaderImpl *) This)->max_rel_offset = reg;
467 reg_maps->usesrelconstF = TRUE;
470 else if(WINED3DSPR_CONSTINT == regtype) {
471 This->baseShader.uses_int_consts = TRUE;
473 else if(WINED3DSPR_CONSTBOOL == regtype) {
474 This->baseShader.uses_bool_consts = TRUE;
477 /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and is used
478 * in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel shaders because TECRDOUT
479 * isn't used in them, but future register types might cause issues
481 else if (WINED3DSPR_TEXCRDOUT == regtype && i == 0 /* Only look at writes */
482 && !pshader && WINED3DSHADER_VERSION_MAJOR(shader_version) < 3)
484 reg_maps->texcoord_mask[reg] |= shader_get_writemask(param);
489 ++pToken;
490 reg_maps->loop_depth = max_loop_depth;
492 This->baseShader.functionLength = ((char *)pToken - (char *)byte_code);
494 return WINED3D_OK;
497 static void shader_dump_decl_usage(DWORD decl, DWORD param, DWORD shader_version)
499 DWORD regtype = shader_get_regtype(param);
501 TRACE("dcl");
503 if (regtype == WINED3DSPR_SAMPLER) {
504 DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
506 switch (ttype) {
507 case WINED3DSTT_2D: TRACE("_2d"); break;
508 case WINED3DSTT_CUBE: TRACE("_cube"); break;
509 case WINED3DSTT_VOLUME: TRACE("_volume"); break;
510 default: TRACE("_unknown_ttype(0x%08x)", ttype);
513 } else {
515 DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
516 DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
518 /* Pixel shaders 3.0 don't have usage semantics */
519 if (shader_is_pshader_version(shader_version) && shader_version < WINED3DPS_VERSION(3,0))
520 return;
521 else
522 TRACE("_");
524 switch(usage) {
525 case WINED3DDECLUSAGE_POSITION:
526 TRACE("position%d", idx);
527 break;
528 case WINED3DDECLUSAGE_BLENDINDICES:
529 TRACE("blend");
530 break;
531 case WINED3DDECLUSAGE_BLENDWEIGHT:
532 TRACE("weight");
533 break;
534 case WINED3DDECLUSAGE_NORMAL:
535 TRACE("normal%d", idx);
536 break;
537 case WINED3DDECLUSAGE_PSIZE:
538 TRACE("psize");
539 break;
540 case WINED3DDECLUSAGE_COLOR:
541 if(idx == 0) {
542 TRACE("color");
543 } else {
544 TRACE("specular%d", (idx - 1));
546 break;
547 case WINED3DDECLUSAGE_TEXCOORD:
548 TRACE("texture%d", idx);
549 break;
550 case WINED3DDECLUSAGE_TANGENT:
551 TRACE("tangent");
552 break;
553 case WINED3DDECLUSAGE_BINORMAL:
554 TRACE("binormal");
555 break;
556 case WINED3DDECLUSAGE_TESSFACTOR:
557 TRACE("tessfactor");
558 break;
559 case WINED3DDECLUSAGE_POSITIONT:
560 TRACE("positionT%d", idx);
561 break;
562 case WINED3DDECLUSAGE_FOG:
563 TRACE("fog");
564 break;
565 case WINED3DDECLUSAGE_DEPTH:
566 TRACE("depth");
567 break;
568 case WINED3DDECLUSAGE_SAMPLE:
569 TRACE("sample");
570 break;
571 default:
572 FIXME("unknown_semantics(0x%08x)", usage);
577 static void shader_dump_arr_entry(const DWORD param, const DWORD addr_token,
578 unsigned int reg, int input, DWORD shader_version)
580 char relative =
581 ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
583 if (relative) {
584 TRACE("[");
585 if (addr_token)
586 shader_dump_param(addr_token, 0, input, shader_version);
587 else
588 TRACE("a0.x");
589 TRACE(" + ");
591 TRACE("%u", reg);
592 if (relative)
593 TRACE("]");
596 static void shader_dump_param(const DWORD param, const DWORD addr_token, int input, DWORD shader_version)
598 static const char * const rastout_reg_names[] = { "oPos", "oFog", "oPts" };
599 static const char * const misctype_reg_names[] = { "vPos", "vFace"};
600 const char *swizzle_reg_chars = "xyzw";
602 DWORD reg = param & WINED3DSP_REGNUM_MASK;
603 DWORD regtype = shader_get_regtype(param);
604 DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
606 /* There are some minor differences between pixel and vertex shaders */
607 char pshader = shader_is_pshader_version(shader_version);
609 if (input) {
610 if ( (modifier == WINED3DSPSM_NEG) ||
611 (modifier == WINED3DSPSM_BIASNEG) ||
612 (modifier == WINED3DSPSM_SIGNNEG) ||
613 (modifier == WINED3DSPSM_X2NEG) ||
614 (modifier == WINED3DSPSM_ABSNEG) )
615 TRACE("-");
616 else if (modifier == WINED3DSPSM_COMP)
617 TRACE("1-");
618 else if (modifier == WINED3DSPSM_NOT)
619 TRACE("!");
621 if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG)
622 TRACE("abs(");
625 switch (regtype) {
626 case WINED3DSPR_TEMP:
627 TRACE("r%u", reg);
628 break;
629 case WINED3DSPR_INPUT:
630 TRACE("v");
631 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
632 break;
633 case WINED3DSPR_CONST:
634 case WINED3DSPR_CONST2:
635 case WINED3DSPR_CONST3:
636 case WINED3DSPR_CONST4:
637 TRACE("c");
638 shader_dump_arr_entry(param, addr_token, shader_get_float_offset(param), input, shader_version);
639 break;
640 case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
641 TRACE("%c%u", (pshader? 't':'a'), reg);
642 break;
643 case WINED3DSPR_RASTOUT:
644 TRACE("%s", rastout_reg_names[reg]);
645 break;
646 case WINED3DSPR_COLOROUT:
647 TRACE("oC%u", reg);
648 break;
649 case WINED3DSPR_DEPTHOUT:
650 TRACE("oDepth");
651 break;
652 case WINED3DSPR_ATTROUT:
653 TRACE("oD%u", reg);
654 break;
655 case WINED3DSPR_TEXCRDOUT:
657 /* Vertex shaders >= 3.0 use general purpose output registers
658 * (WINED3DSPR_OUTPUT), which can include an address token */
660 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3) {
661 TRACE("o");
662 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
664 else
665 TRACE("oT%u", reg);
666 break;
667 case WINED3DSPR_CONSTINT:
668 TRACE("i");
669 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
670 break;
671 case WINED3DSPR_CONSTBOOL:
672 TRACE("b");
673 shader_dump_arr_entry(param, addr_token, reg, input, shader_version);
674 break;
675 case WINED3DSPR_LABEL:
676 TRACE("l%u", reg);
677 break;
678 case WINED3DSPR_LOOP:
679 TRACE("aL");
680 break;
681 case WINED3DSPR_SAMPLER:
682 TRACE("s%u", reg);
683 break;
684 case WINED3DSPR_MISCTYPE:
685 if (reg > 1) {
686 FIXME("Unhandled misctype register %d\n", reg);
687 } else {
688 TRACE("%s", misctype_reg_names[reg]);
690 break;
691 case WINED3DSPR_PREDICATE:
692 TRACE("p%u", reg);
693 break;
694 default:
695 TRACE("unhandled_rtype(%#x)", regtype);
696 break;
699 if (!input) {
700 /* operand output (for modifiers and shift, see dump_ins_modifiers) */
702 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
703 TRACE(".");
704 if (param & WINED3DSP_WRITEMASK_0) TRACE("%c", swizzle_reg_chars[0]);
705 if (param & WINED3DSP_WRITEMASK_1) TRACE("%c", swizzle_reg_chars[1]);
706 if (param & WINED3DSP_WRITEMASK_2) TRACE("%c", swizzle_reg_chars[2]);
707 if (param & WINED3DSP_WRITEMASK_3) TRACE("%c", swizzle_reg_chars[3]);
710 } else {
711 /** operand input */
712 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
713 DWORD swizzle_x = swizzle & 0x03;
714 DWORD swizzle_y = (swizzle >> 2) & 0x03;
715 DWORD swizzle_z = (swizzle >> 4) & 0x03;
716 DWORD swizzle_w = (swizzle >> 6) & 0x03;
718 if (0 != modifier) {
719 switch (modifier) {
720 case WINED3DSPSM_NONE: break;
721 case WINED3DSPSM_NEG: break;
722 case WINED3DSPSM_NOT: break;
723 case WINED3DSPSM_BIAS: TRACE("_bias"); break;
724 case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
725 case WINED3DSPSM_SIGN: TRACE("_bx2"); break;
726 case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
727 case WINED3DSPSM_COMP: break;
728 case WINED3DSPSM_X2: TRACE("_x2"); break;
729 case WINED3DSPSM_X2NEG: TRACE("_x2"); break;
730 case WINED3DSPSM_DZ: TRACE("_dz"); break;
731 case WINED3DSPSM_DW: TRACE("_dw"); break;
732 case WINED3DSPSM_ABSNEG: TRACE(")"); break;
733 case WINED3DSPSM_ABS: TRACE(")"); break;
734 default:
735 TRACE("_unknown_modifier(%#x)", modifier >> WINED3DSP_SRCMOD_SHIFT);
740 * swizzle bits fields:
741 * RRGGBBAA
743 if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) != swizzle) {
744 if (swizzle_x == swizzle_y &&
745 swizzle_x == swizzle_z &&
746 swizzle_x == swizzle_w) {
747 TRACE(".%c", swizzle_reg_chars[swizzle_x]);
748 } else {
749 TRACE(".%c%c%c%c",
750 swizzle_reg_chars[swizzle_x],
751 swizzle_reg_chars[swizzle_y],
752 swizzle_reg_chars[swizzle_z],
753 swizzle_reg_chars[swizzle_w]);
759 static void shader_color_correction(IWineD3DBaseShaderImpl *shader,
760 IWineD3DDeviceImpl *device, const struct SHADER_OPCODE_ARG *arg, DWORD shader_version)
762 IWineD3DBaseTextureImpl *texture;
763 struct color_fixup_desc fixup;
764 BOOL recorded = FALSE;
765 DWORD sampler_idx;
766 UINT i;
768 switch(arg->opcode->opcode)
770 case WINED3DSIO_TEX:
771 if (WINED3DSHADER_VERSION_MAJOR(shader_version) < 2) sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
772 else sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
773 break;
775 case WINED3DSIO_TEXLDL:
776 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
777 return;
779 case WINED3DSIO_TEXDP3TEX:
780 case WINED3DSIO_TEXM3x3TEX:
781 case WINED3DSIO_TEXM3x3SPEC:
782 case WINED3DSIO_TEXM3x3VSPEC:
783 case WINED3DSIO_TEXBEM:
784 case WINED3DSIO_TEXREG2AR:
785 case WINED3DSIO_TEXREG2GB:
786 case WINED3DSIO_TEXREG2RGB:
787 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
788 break;
790 default:
791 /* Not a texture sampling instruction, nothing to do */
792 return;
795 texture = (IWineD3DBaseTextureImpl *)device->stateBlock->textures[sampler_idx];
796 if (texture) fixup = texture->baseTexture.shader_color_fixup;
797 else fixup = COLOR_FIXUP_IDENTITY;
799 /* before doing anything, record the sampler with the format in the format conversion list,
800 * but check if it's not there already */
801 for (i = 0; i < shader->baseShader.num_sampled_samplers; ++i)
803 if (shader->baseShader.sampled_samplers[i] == sampler_idx)
805 recorded = TRUE;
806 break;
810 if (!recorded)
812 shader->baseShader.sampled_samplers[shader->baseShader.num_sampled_samplers] = sampler_idx;
813 ++shader->baseShader.num_sampled_samplers;
816 device->shader_backend->shader_color_correction(arg, fixup);
819 /* Shared code in order to generate the bulk of the shader string.
820 * NOTE: A description of how to parse tokens can be found on msdn */
821 void shader_generate_main(IWineD3DBaseShader *iface, SHADER_BUFFER* buffer,
822 const shader_reg_maps* reg_maps, CONST DWORD* pFunction)
824 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
825 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device; /* To access shader backend callbacks */
826 const SHADER_OPCODE *opcode_table = This->baseShader.shader_ins;
827 const SHADER_HANDLER *handler_table = device->shader_backend->shader_instruction_handler_table;
828 DWORD shader_version = reg_maps->shader_version;
829 const DWORD *pToken = pFunction;
830 const SHADER_OPCODE *curOpcode;
831 SHADER_HANDLER hw_fct;
832 DWORD i;
833 SHADER_OPCODE_ARG hw_arg;
835 /* Initialize current parsing state */
836 hw_arg.shader = iface;
837 hw_arg.buffer = buffer;
838 hw_arg.reg_maps = reg_maps;
839 This->baseShader.parse_state.current_row = 0;
841 while (WINED3DPS_END() != *pToken)
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 hw_arg.opcode_token = *pToken++;
860 curOpcode = shader_get_opcode(opcode_table, shader_version, hw_arg.opcode_token);
862 /* Unknown opcode and its parameters */
863 if (!curOpcode)
865 FIXME("Unrecognized opcode: token=0x%08x\n", hw_arg.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, hw_arg.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, hw_arg.opcode_token, shader_version);
891 continue;
894 hw_arg.opcode = curOpcode;
896 /* Destination token */
897 if (curOpcode->dst_token)
899 DWORD param, addr_token = 0;
900 pToken += shader_get_param(pToken, shader_version, &param, &addr_token);
901 hw_arg.dst = param;
902 hw_arg.dst_addr = addr_token;
905 /* Predication token */
906 if (hw_arg.opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) hw_arg.predicate = *pToken++;
908 /* Other source tokens */
909 for (i = 0; i < (curOpcode->num_params - curOpcode->dst_token); ++i)
911 DWORD param, addr_token = 0;
912 pToken += shader_get_param(pToken, shader_version, &param, &addr_token);
913 hw_arg.src[i] = param;
914 hw_arg.src_addr[i] = addr_token;
917 /* Call appropriate function for output target */
918 hw_fct(&hw_arg);
920 /* Add color correction if needed */
921 shader_color_correction(This, device, &hw_arg, shader_version);
923 /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
924 /* FIXME: This should be internal to the shader backend.
925 * Also, right now this is the only reason "shader_mode" exists. */
926 if (This->baseShader.shader_mode == SHADER_GLSL) shader_glsl_add_instruction_modifiers(&hw_arg);
930 static void shader_dump_ins_modifiers(const DWORD output)
932 DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
933 DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
935 switch (shift) {
936 case 0: break;
937 case 13: TRACE("_d8"); break;
938 case 14: TRACE("_d4"); break;
939 case 15: TRACE("_d2"); break;
940 case 1: TRACE("_x2"); break;
941 case 2: TRACE("_x4"); break;
942 case 3: TRACE("_x8"); break;
943 default: TRACE("_unhandled_shift(%d)", shift); break;
946 if (mmask & WINED3DSPDM_SATURATE) TRACE("_sat");
947 if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
948 if (mmask & WINED3DSPDM_MSAMPCENTROID) TRACE("_centroid");
950 mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
951 if (mmask)
952 FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
955 void shader_trace_init(const DWORD *pFunction, const SHADER_OPCODE *opcode_table)
957 const DWORD* pToken = pFunction;
958 const SHADER_OPCODE* curOpcode = NULL;
959 DWORD shader_version;
960 DWORD opcode_token;
961 DWORD i;
963 TRACE("Parsing %p\n", pFunction);
965 /* The version token is supposed to be the first token */
966 if (!shader_is_version_token(*pToken))
968 FIXME("First token is not a version token, invalid shader.\n");
969 return;
971 shader_version = *pToken++;
972 TRACE("%s_%u_%u\n", shader_is_pshader_version(shader_version) ? "ps": "vs",
973 WINED3DSHADER_VERSION_MAJOR(shader_version), WINED3DSHADER_VERSION_MINOR(shader_version));
975 while (WINED3DVS_END() != *pToken)
977 if (shader_is_comment(*pToken)) /* comment */
979 DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
980 ++pToken;
981 TRACE("//%s\n", (const char*)pToken);
982 pToken += comment_len;
983 continue;
985 opcode_token = *pToken++;
986 curOpcode = shader_get_opcode(opcode_table, shader_version, opcode_token);
988 if (!curOpcode)
990 int tokens_read;
991 FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
992 tokens_read = shader_skip_unrecognized(pToken, shader_version);
993 pToken += tokens_read;
995 else
997 if (curOpcode->opcode == WINED3DSIO_DCL)
999 DWORD usage = *pToken;
1000 DWORD param = *(pToken + 1);
1002 shader_dump_decl_usage(usage, param, shader_version);
1003 shader_dump_ins_modifiers(param);
1004 TRACE(" ");
1005 shader_dump_param(param, 0, 0, shader_version);
1006 pToken += 2;
1008 else if (curOpcode->opcode == WINED3DSIO_DEF)
1010 unsigned int offset = shader_get_float_offset(*pToken);
1012 TRACE("def c%u = %f, %f, %f, %f", offset,
1013 *(const float *)(pToken + 1),
1014 *(const float *)(pToken + 2),
1015 *(const float *)(pToken + 3),
1016 *(const float *)(pToken + 4));
1017 pToken += 5;
1019 else if (curOpcode->opcode == WINED3DSIO_DEFI)
1021 TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
1022 *(pToken + 1),
1023 *(pToken + 2),
1024 *(pToken + 3),
1025 *(pToken + 4));
1026 pToken += 5;
1028 else if (curOpcode->opcode == WINED3DSIO_DEFB)
1030 TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
1031 *(pToken + 1)? "true": "false");
1032 pToken += 2;
1034 else
1036 DWORD param, addr_token;
1037 int tokens_read;
1039 /* Print out predication source token first - it follows
1040 * the destination token. */
1041 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
1043 TRACE("(");
1044 shader_dump_param(*(pToken + 2), 0, 1, shader_version);
1045 TRACE(") ");
1047 if (opcode_token & WINED3DSI_COISSUE)
1049 /* PixWin marks instructions with the coissue flag with a '+' */
1050 TRACE("+");
1053 TRACE("%s", curOpcode->name);
1055 if (curOpcode->opcode == WINED3DSIO_IFC
1056 || curOpcode->opcode == WINED3DSIO_BREAKC)
1058 DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1060 switch (op)
1062 case COMPARISON_GT: TRACE("_gt"); break;
1063 case COMPARISON_EQ: TRACE("_eq"); break;
1064 case COMPARISON_GE: TRACE("_ge"); break;
1065 case COMPARISON_LT: TRACE("_lt"); break;
1066 case COMPARISON_NE: TRACE("_ne"); break;
1067 case COMPARISON_LE: TRACE("_le"); break;
1068 default: TRACE("_(%u)", op);
1071 else if (curOpcode->opcode == WINED3DSIO_TEX
1072 && shader_version >= WINED3DPS_VERSION(2,0)
1073 && (opcode_token & WINED3DSI_TEXLD_PROJECT))
1075 TRACE("p");
1078 /* Destination token */
1079 if (curOpcode->dst_token)
1081 tokens_read = shader_get_param(pToken, shader_version, &param, &addr_token);
1082 pToken += tokens_read;
1084 shader_dump_ins_modifiers(param);
1085 TRACE(" ");
1086 shader_dump_param(param, addr_token, 0, shader_version);
1089 /* Predication token - already printed out, just skip it */
1090 if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)
1092 pToken++;
1095 /* Other source tokens */
1096 for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i)
1098 tokens_read = shader_get_param(pToken, shader_version, &param, &addr_token);
1099 pToken += tokens_read;
1101 TRACE((i == 0)? " " : ", ");
1102 shader_dump_param(param, addr_token, 1, shader_version);
1105 TRACE("\n");
1110 void shader_cleanup(IWineD3DBaseShader *iface)
1112 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
1114 ((IWineD3DDeviceImpl *)This->baseShader.device)->shader_backend->shader_destroy(iface);
1115 HeapFree(GetProcessHeap(), 0, This->baseShader.function);
1116 shader_delete_constant_list(&This->baseShader.constantsF);
1117 shader_delete_constant_list(&This->baseShader.constantsB);
1118 shader_delete_constant_list(&This->baseShader.constantsI);
1119 list_remove(&This->baseShader.shader_list_entry);
1122 static const SHADER_HANDLER shader_none_instruction_handler_table[WINED3DSIH_TABLE_SIZE] = {0};
1123 static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
1124 static void shader_none_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {}
1125 static void shader_none_deselect_depth_blt(IWineD3DDevice *iface) {}
1126 static void shader_none_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1127 static void shader_none_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count) {}
1128 static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1129 static void shader_none_color_correction(const struct SHADER_OPCODE_ARG *arg, struct color_fixup_desc fixup) {}
1130 static void shader_none_destroy(IWineD3DBaseShader *iface) {}
1131 static HRESULT shader_none_alloc(IWineD3DDevice *iface) {return WINED3D_OK;}
1132 static void shader_none_free(IWineD3DDevice *iface) {}
1133 static BOOL shader_none_dirty_const(IWineD3DDevice *iface) {return FALSE;}
1134 static GLuint shader_none_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer, const struct ps_compile_args *args) {
1135 FIXME("NONE shader backend asked to generate a pixel shader\n");
1136 return 0;
1138 static void shader_none_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
1139 FIXME("NONE shader backend asked to generate a vertex shader\n");
1142 #define GLINFO_LOCATION (*gl_info)
1143 static void shader_none_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
1145 /* Set the shader caps to 0 for the none shader backend */
1146 pCaps->VertexShaderVersion = 0;
1147 pCaps->PixelShaderVersion = 0;
1148 pCaps->PixelShader1xMaxValue = 0.0;
1150 #undef GLINFO_LOCATION
1151 static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
1153 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1155 TRACE("Checking support for fixup:\n");
1156 dump_color_fixup_desc(fixup);
1159 /* Faked to make some apps happy. */
1160 if (!is_yuv_fixup(fixup))
1162 TRACE("[OK]\n");
1163 return TRUE;
1166 TRACE("[FAILED]\n");
1167 return FALSE;
1170 const shader_backend_t none_shader_backend = {
1171 shader_none_instruction_handler_table,
1172 shader_none_select,
1173 shader_none_select_depth_blt,
1174 shader_none_deselect_depth_blt,
1175 shader_none_update_float_vertex_constants,
1176 shader_none_update_float_pixel_constants,
1177 shader_none_load_constants,
1178 shader_none_color_correction,
1179 shader_none_destroy,
1180 shader_none_alloc,
1181 shader_none_free,
1182 shader_none_dirty_const,
1183 shader_none_generate_pshader,
1184 shader_none_generate_vshader,
1185 shader_none_get_caps,
1186 shader_none_color_fixup_supported,