2 * Direct3D shader library utility routines
4 * Copyright 2008 Stefan Dösinger
5 * Copyright 2009 Matteo Bruni
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/debug.h"
26 #include "d3dx9_36_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(asmshader);
30 /* bwriter -> d3d9 conversion functions */
31 DWORD d3d9_swizzle(DWORD bwriter_swizzle) {
32 /* Currently a NOP, but this allows changing the internal definitions
33 * without side effects
37 if((bwriter_swizzle & BWRITERVS_X_X) == BWRITERVS_X_X) ret |= D3DVS_X_X;
38 if((bwriter_swizzle & BWRITERVS_X_Y) == BWRITERVS_X_Y) ret |= D3DVS_X_Y;
39 if((bwriter_swizzle & BWRITERVS_X_Z) == BWRITERVS_X_Z) ret |= D3DVS_X_Z;
40 if((bwriter_swizzle & BWRITERVS_X_W) == BWRITERVS_X_W) ret |= D3DVS_X_W;
42 if((bwriter_swizzle & BWRITERVS_Y_X) == BWRITERVS_Y_X) ret |= D3DVS_Y_X;
43 if((bwriter_swizzle & BWRITERVS_Y_Y) == BWRITERVS_Y_Y) ret |= D3DVS_Y_Y;
44 if((bwriter_swizzle & BWRITERVS_Y_Z) == BWRITERVS_Y_Z) ret |= D3DVS_Y_Z;
45 if((bwriter_swizzle & BWRITERVS_Y_W) == BWRITERVS_Y_W) ret |= D3DVS_Y_W;
47 if((bwriter_swizzle & BWRITERVS_Z_X) == BWRITERVS_Z_X) ret |= D3DVS_Z_X;
48 if((bwriter_swizzle & BWRITERVS_Z_Y) == BWRITERVS_Z_Y) ret |= D3DVS_Z_Y;
49 if((bwriter_swizzle & BWRITERVS_Z_Z) == BWRITERVS_Z_Z) ret |= D3DVS_Z_Z;
50 if((bwriter_swizzle & BWRITERVS_Z_W) == BWRITERVS_Z_W) ret |= D3DVS_Z_W;
52 if((bwriter_swizzle & BWRITERVS_W_X) == BWRITERVS_W_X) ret |= D3DVS_W_X;
53 if((bwriter_swizzle & BWRITERVS_W_Y) == BWRITERVS_W_Y) ret |= D3DVS_W_Y;
54 if((bwriter_swizzle & BWRITERVS_W_Z) == BWRITERVS_W_Z) ret |= D3DVS_W_Z;
55 if((bwriter_swizzle & BWRITERVS_W_W) == BWRITERVS_W_W) ret |= D3DVS_W_W;
60 DWORD d3d9_writemask(DWORD bwriter_writemask) {
63 if(bwriter_writemask & BWRITERSP_WRITEMASK_0) ret |= D3DSP_WRITEMASK_0;
64 if(bwriter_writemask & BWRITERSP_WRITEMASK_1) ret |= D3DSP_WRITEMASK_1;
65 if(bwriter_writemask & BWRITERSP_WRITEMASK_2) ret |= D3DSP_WRITEMASK_2;
66 if(bwriter_writemask & BWRITERSP_WRITEMASK_3) ret |= D3DSP_WRITEMASK_3;
71 DWORD d3d9_srcmod(DWORD bwriter_srcmod) {
72 switch(bwriter_srcmod) {
73 case BWRITERSPSM_NONE: return D3DSPSM_NONE;
74 case BWRITERSPSM_NEG: return D3DSPSM_NEG;
75 case BWRITERSPSM_ABS: return D3DSPSM_ABS;
76 case BWRITERSPSM_ABSNEG: return D3DSPSM_ABSNEG;
77 case BWRITERSPSM_NOT: return D3DSPSM_NOT;
79 FIXME("Unhandled BWRITERSPSM token %u\n", bwriter_srcmod);
84 DWORD d3d9_dstmod(DWORD bwriter_mod) {
87 if(bwriter_mod & BWRITERSPDM_SATURATE) ret |= D3DSPDM_SATURATE;
88 if(bwriter_mod & BWRITERSPDM_PARTIALPRECISION) ret |= D3DSPDM_PARTIALPRECISION;
89 if(bwriter_mod & BWRITERSPDM_MSAMPCENTROID) ret |= D3DSPDM_MSAMPCENTROID;
94 DWORD d3d9_comparetype(DWORD asmshader_comparetype) {
95 switch(asmshader_comparetype) {
96 case BWRITER_COMPARISON_GT: return D3DSPC_GT;
97 case BWRITER_COMPARISON_EQ: return D3DSPC_EQ;
98 case BWRITER_COMPARISON_GE: return D3DSPC_GE;
99 case BWRITER_COMPARISON_LT: return D3DSPC_LT;
100 case BWRITER_COMPARISON_NE: return D3DSPC_NE;
101 case BWRITER_COMPARISON_LE: return D3DSPC_LE;
103 FIXME("Unexpected BWRITER_COMPARISON type %u\n", asmshader_comparetype);
108 DWORD d3d9_register(DWORD bwriter_register) {
109 if(bwriter_register == BWRITERSPR_TEMP) return D3DSPR_TEMP;
110 if(bwriter_register == BWRITERSPR_INPUT) return D3DSPR_INPUT;
111 if(bwriter_register == BWRITERSPR_CONST) return D3DSPR_CONST;
112 if(bwriter_register == BWRITERSPR_ADDR) return D3DSPR_ADDR;
113 if(bwriter_register == BWRITERSPR_TEXTURE) return D3DSPR_TEXTURE;
114 if(bwriter_register == BWRITERSPR_RASTOUT) return D3DSPR_RASTOUT;
115 if(bwriter_register == BWRITERSPR_ATTROUT) return D3DSPR_ATTROUT;
116 if(bwriter_register == BWRITERSPR_TEXCRDOUT) return D3DSPR_TEXCRDOUT;
117 if(bwriter_register == BWRITERSPR_OUTPUT) return D3DSPR_OUTPUT;
118 if(bwriter_register == BWRITERSPR_CONSTINT) return D3DSPR_CONSTINT;
119 if(bwriter_register == BWRITERSPR_COLOROUT) return D3DSPR_COLOROUT;
120 if(bwriter_register == BWRITERSPR_DEPTHOUT) return D3DSPR_DEPTHOUT;
121 if(bwriter_register == BWRITERSPR_SAMPLER) return D3DSPR_SAMPLER;
122 if(bwriter_register == BWRITERSPR_CONSTBOOL) return D3DSPR_CONSTBOOL;
123 if(bwriter_register == BWRITERSPR_LOOP) return D3DSPR_LOOP;
124 if(bwriter_register == BWRITERSPR_MISCTYPE) return D3DSPR_MISCTYPE;
125 if(bwriter_register == BWRITERSPR_LABEL) return D3DSPR_LABEL;
126 if(bwriter_register == BWRITERSPR_PREDICATE) return D3DSPR_PREDICATE;
128 FIXME("Unexpected BWRITERSPR %u\n", bwriter_register);
132 DWORD d3d9_opcode(DWORD bwriter_opcode) {
133 switch(bwriter_opcode) {
134 case BWRITERSIO_NOP: return D3DSIO_NOP;
135 case BWRITERSIO_MOV: return D3DSIO_MOV;
136 case BWRITERSIO_ADD: return D3DSIO_ADD;
137 case BWRITERSIO_SUB: return D3DSIO_SUB;
138 case BWRITERSIO_MAD: return D3DSIO_MAD;
139 case BWRITERSIO_MUL: return D3DSIO_MUL;
140 case BWRITERSIO_RCP: return D3DSIO_RCP;
141 case BWRITERSIO_RSQ: return D3DSIO_RSQ;
142 case BWRITERSIO_DP3: return D3DSIO_DP3;
143 case BWRITERSIO_DP4: return D3DSIO_DP4;
144 case BWRITERSIO_MIN: return D3DSIO_MIN;
145 case BWRITERSIO_MAX: return D3DSIO_MAX;
146 case BWRITERSIO_SLT: return D3DSIO_SLT;
147 case BWRITERSIO_SGE: return D3DSIO_SGE;
148 case BWRITERSIO_EXP: return D3DSIO_EXP;
149 case BWRITERSIO_LOG: return D3DSIO_LOG;
150 case BWRITERSIO_LIT: return D3DSIO_LIT;
151 case BWRITERSIO_DST: return D3DSIO_DST;
152 case BWRITERSIO_LRP: return D3DSIO_LRP;
153 case BWRITERSIO_FRC: return D3DSIO_FRC;
154 case BWRITERSIO_M4x4: return D3DSIO_M4x4;
155 case BWRITERSIO_M4x3: return D3DSIO_M4x3;
156 case BWRITERSIO_M3x4: return D3DSIO_M3x4;
157 case BWRITERSIO_M3x3: return D3DSIO_M3x3;
158 case BWRITERSIO_M3x2: return D3DSIO_M3x2;
159 case BWRITERSIO_CALL: return D3DSIO_CALL;
160 case BWRITERSIO_CALLNZ: return D3DSIO_CALLNZ;
161 case BWRITERSIO_LOOP: return D3DSIO_LOOP;
162 case BWRITERSIO_RET: return D3DSIO_RET;
163 case BWRITERSIO_ENDLOOP: return D3DSIO_ENDLOOP;
164 case BWRITERSIO_LABEL: return D3DSIO_LABEL;
165 case BWRITERSIO_DCL: return D3DSIO_DCL;
166 case BWRITERSIO_POW: return D3DSIO_POW;
167 case BWRITERSIO_CRS: return D3DSIO_CRS;
168 case BWRITERSIO_SGN: return D3DSIO_SGN;
169 case BWRITERSIO_ABS: return D3DSIO_ABS;
170 case BWRITERSIO_NRM: return D3DSIO_NRM;
171 case BWRITERSIO_SINCOS: return D3DSIO_SINCOS;
172 case BWRITERSIO_REP: return D3DSIO_REP;
173 case BWRITERSIO_ENDREP: return D3DSIO_ENDREP;
174 case BWRITERSIO_IF: return D3DSIO_IF;
175 case BWRITERSIO_IFC: return D3DSIO_IFC;
176 case BWRITERSIO_ELSE: return D3DSIO_ELSE;
177 case BWRITERSIO_ENDIF: return D3DSIO_ENDIF;
178 case BWRITERSIO_BREAK: return D3DSIO_BREAK;
179 case BWRITERSIO_BREAKC: return D3DSIO_BREAKC;
180 case BWRITERSIO_MOVA: return D3DSIO_MOVA;
181 case BWRITERSIO_EXPP: return D3DSIO_EXPP;
182 case BWRITERSIO_LOGP: return D3DSIO_LOGP;
183 case BWRITERSIO_SETP: return D3DSIO_SETP;
184 case BWRITERSIO_TEXLDL: return D3DSIO_TEXLDL;
185 case BWRITERSIO_BREAKP: return D3DSIO_BREAKP;
187 case BWRITERSIO_COMMENT: return D3DSIO_COMMENT;
188 case BWRITERSIO_END: return D3DSIO_END;
191 FIXME("Unhandled BWRITERSIO token %u\n", bwriter_opcode);
196 /* Debug print functions */
197 const char *debug_print_srcmod(DWORD mod) {
199 case BWRITERSPSM_NEG: return "D3DSPSM_NEG";
200 case BWRITERSPSM_ABS: return "D3DSPSM_ABS";
201 case BWRITERSPSM_ABSNEG: return "D3DSPSM_ABSNEG";
202 case BWRITERSPSM_NOT: return "D3DSPSM_NOT";
203 default: return "Unknown source modifier\n";
207 const char *debug_print_dstmod(DWORD mod) {
212 case BWRITERSPDM_SATURATE:
214 case BWRITERSPDM_PARTIALPRECISION:
216 case BWRITERSPDM_MSAMPCENTROID:
219 case BWRITERSPDM_SATURATE | BWRITERSPDM_PARTIALPRECISION:
221 case BWRITERSPDM_SATURATE | BWRITERSPDM_MSAMPCENTROID:
222 return "_sat_centroid";
223 case BWRITERSPDM_PARTIALPRECISION | BWRITERSPDM_MSAMPCENTROID:
224 return "_pp_centroid";
226 case BWRITERSPDM_SATURATE | BWRITERSPDM_PARTIALPRECISION | BWRITERSPDM_MSAMPCENTROID:
227 return "_sat_pp_centroid";
230 return "Unexpected modifier\n";
234 static const char *get_regname(const struct shader_reg *reg, shader_type st) {
236 case BWRITERSPR_TEMP:
237 return wine_dbg_sprintf("r%u", reg->regnum);
238 case BWRITERSPR_INPUT:
239 return wine_dbg_sprintf("v%u", reg->regnum);
240 case BWRITERSPR_CONST:
241 return wine_dbg_sprintf("c%u", reg->regnum);
242 /* case BWRITERSPR_ADDR: */
243 case BWRITERSPR_TEXTURE:
244 if(st == ST_VERTEX) {
245 return wine_dbg_sprintf("a%u", reg->regnum);
247 return wine_dbg_sprintf("t%u", reg->regnum);
249 case BWRITERSPR_RASTOUT:
250 switch(reg->regnum) {
251 case BWRITERSRO_POSITION: return "oPos";
252 case BWRITERSRO_FOG: return "oFog";
253 case BWRITERSRO_POINT_SIZE: return "oPts";
254 default: return "Unexpected RASTOUT";
256 case BWRITERSPR_ATTROUT:
257 return wine_dbg_sprintf("oD%u", reg->regnum);
258 /* case BWRITERSPR_TEXCRDOUT: */
259 case BWRITERSPR_OUTPUT:
260 return wine_dbg_sprintf("o[T]%u", reg->regnum);
261 case BWRITERSPR_CONSTINT:
262 return wine_dbg_sprintf("i%u", reg->regnum);
263 case BWRITERSPR_COLOROUT:
264 return wine_dbg_sprintf("oC%u", reg->regnum);
265 case BWRITERSPR_DEPTHOUT:
267 case BWRITERSPR_SAMPLER:
268 return wine_dbg_sprintf("s%u", reg->regnum);
269 case BWRITERSPR_CONSTBOOL:
270 return wine_dbg_sprintf("b%u", reg->regnum);
271 case BWRITERSPR_LOOP:
273 case BWRITERSPR_MISCTYPE:
274 switch(reg->regnum) {
275 case 0: return "vPos";
276 case 1: return "vFace";
277 case 2: return "unexpected misctype";
279 case BWRITERSPR_LABEL:
280 return wine_dbg_sprintf("l%u", reg->regnum);
281 case BWRITERSPR_PREDICATE:
282 return wine_dbg_sprintf("p%u", reg->regnum);
283 default: return "unknown regname";
287 const char *debug_print_writemask(DWORD mask) {
289 unsigned char pos = 1;
291 if(mask == BWRITERSP_WRITEMASK_ALL) return "";
293 if(mask & BWRITERSP_WRITEMASK_0) ret[pos++] = 'x';
294 if(mask & BWRITERSP_WRITEMASK_1) ret[pos++] = 'y';
295 if(mask & BWRITERSP_WRITEMASK_2) ret[pos++] = 'z';
296 if(mask & BWRITERSP_WRITEMASK_3) ret[pos++] = 'w';
298 return wine_dbg_sprintf("%s", ret);
301 const char *debug_print_relarg(const struct shader_reg *reg) {
302 const char *short_swizzle;
303 if(!reg->rel_reg) return "";
305 short_swizzle = debug_print_swizzle(reg->rel_reg->swizzle);
307 if(reg->rel_reg->type == BWRITERSPR_ADDR) {
308 return wine_dbg_sprintf("[a%u%s]", reg->rel_reg->regnum, short_swizzle);
309 } else if(reg->rel_reg->type == BWRITERSPR_LOOP && reg->rel_reg->regnum == 0) {
310 return wine_dbg_sprintf("[aL%s]", short_swizzle);
312 return "Unexpected relative addressing argument";
316 const char *debug_print_dstreg(const struct shader_reg *reg, shader_type st) {
317 return wine_dbg_sprintf("%s%s%s", get_regname(reg, st),
318 debug_print_relarg(reg),
319 debug_print_writemask(reg->writemask));
322 const char *debug_print_swizzle(DWORD arg) {
328 case BWRITERVS_NOSWIZZLE:
330 case BWRITERVS_SWIZZLE_X:
332 case BWRITERVS_SWIZZLE_Y:
334 case BWRITERVS_SWIZZLE_Z:
336 case BWRITERVS_SWIZZLE_W:
340 swizzle[0] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 0)) & 0x03;
341 swizzle[1] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 2)) & 0x03;
342 swizzle[2] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 4)) & 0x03;
343 swizzle[3] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 6)) & 0x03;
346 for(i = 0; i < 4; i++) {
348 case 0: ret[1 + i] = 'x'; break;
349 case 1: ret[1 + i] = 'y'; break;
350 case 2: ret[1 + i] = 'z'; break;
351 case 3: ret[1 + i] = 'w'; break;
355 return wine_dbg_sprintf("%s", ret);
358 const char *debug_print_srcreg(const struct shader_reg *reg, shader_type st) {
359 switch(reg->srcmod) {
360 case BWRITERSPSM_NONE:
361 return wine_dbg_sprintf("%s%s%s", get_regname(reg, st),
362 debug_print_relarg(reg),
363 debug_print_swizzle(reg->swizzle));
364 case BWRITERSPSM_NEG:
365 return wine_dbg_sprintf("-%s%s%s", get_regname(reg, st),
366 debug_print_relarg(reg),
367 debug_print_swizzle(reg->swizzle));
368 case BWRITERSPSM_ABS:
369 return wine_dbg_sprintf("%s%s_abs%s", get_regname(reg, st),
370 debug_print_relarg(reg),
371 debug_print_swizzle(reg->swizzle));
372 case BWRITERSPSM_ABSNEG:
373 return wine_dbg_sprintf("-%s%s_abs%s", get_regname(reg, st),
374 debug_print_relarg(reg),
375 debug_print_swizzle(reg->swizzle));
376 case BWRITERSPSM_NOT:
377 return wine_dbg_sprintf("!%s%s%s", get_regname(reg, st),
378 debug_print_relarg(reg),
379 debug_print_swizzle(reg->swizzle));
381 return "Unknown modifier";
384 const char *debug_print_comp(DWORD comp) {
386 case BWRITER_COMPARISON_NONE: return "";
387 case BWRITER_COMPARISON_GT: return "_gt";
388 case BWRITER_COMPARISON_EQ: return "_eq";
389 case BWRITER_COMPARISON_GE: return "_ge";
390 case BWRITER_COMPARISON_LT: return "_lt";
391 case BWRITER_COMPARISON_NE: return "_ne";
392 case BWRITER_COMPARISON_LE: return "_le";
393 default: return "_unknown";
397 const char *debug_print_opcode(DWORD opcode) {
399 case BWRITERSIO_NOP: return "nop";
400 case BWRITERSIO_MOV: return "mov";
401 case BWRITERSIO_ADD: return "add";
402 case BWRITERSIO_SUB: return "sub";
403 case BWRITERSIO_MAD: return "mad";
404 case BWRITERSIO_MUL: return "mul";
405 case BWRITERSIO_RCP: return "rcp";
406 case BWRITERSIO_RSQ: return "rsq";
407 case BWRITERSIO_DP3: return "dp3";
408 case BWRITERSIO_DP4: return "dp4";
409 case BWRITERSIO_MIN: return "min";
410 case BWRITERSIO_MAX: return "max";
411 case BWRITERSIO_SLT: return "slt";
412 case BWRITERSIO_SGE: return "sge";
413 case BWRITERSIO_EXP: return "exp";
414 case BWRITERSIO_LOG: return "log";
415 case BWRITERSIO_LIT: return "lit";
416 case BWRITERSIO_DST: return "dst";
417 case BWRITERSIO_LRP: return "lrp";
418 case BWRITERSIO_FRC: return "frc";
419 case BWRITERSIO_M4x4: return "m4x4";
420 case BWRITERSIO_M4x3: return "m4x3";
421 case BWRITERSIO_M3x4: return "m3x4";
422 case BWRITERSIO_M3x3: return "m3x3";
423 case BWRITERSIO_M3x2: return "m3x2";
424 case BWRITERSIO_CALL: return "call";
425 case BWRITERSIO_CALLNZ: return "callnz";
426 case BWRITERSIO_LOOP: return "loop";
427 case BWRITERSIO_RET: return "ret";
428 case BWRITERSIO_ENDLOOP: return "endloop";
429 case BWRITERSIO_LABEL: return "label";
430 case BWRITERSIO_DCL: return "dcl";
431 case BWRITERSIO_POW: return "pow";
432 case BWRITERSIO_CRS: return "crs";
433 case BWRITERSIO_SGN: return "sgn";
434 case BWRITERSIO_ABS: return "abs";
435 case BWRITERSIO_NRM: return "nrm";
436 case BWRITERSIO_SINCOS: return "sincos";
437 case BWRITERSIO_REP: return "rep";
438 case BWRITERSIO_ENDREP: return "endrep";
439 case BWRITERSIO_IF: return "if";
440 case BWRITERSIO_IFC: return "ifc";
441 case BWRITERSIO_ELSE: return "else";
442 case BWRITERSIO_ENDIF: return "endif";
443 case BWRITERSIO_BREAK: return "break";
444 case BWRITERSIO_BREAKC: return "breakc";
445 case BWRITERSIO_MOVA: return "mova";
446 case BWRITERSIO_EXPP: return "expp";
447 case BWRITERSIO_LOGP: return "logp";
448 case BWRITERSIO_SETP: return "setp";
449 case BWRITERSIO_TEXLDL: return "texldl";
450 case BWRITERSIO_BREAKP: return "breakp";
452 default: return "unknown";