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_sampler(DWORD bwriter_sampler) {
109 if(bwriter_sampler == BWRITERSTT_UNKNOWN) return D3DSTT_UNKNOWN;
110 if(bwriter_sampler == BWRITERSTT_1D) return D3DSTT_1D;
111 if(bwriter_sampler == BWRITERSTT_2D) return D3DSTT_2D;
112 if(bwriter_sampler == BWRITERSTT_CUBE) return D3DSTT_CUBE;
113 if(bwriter_sampler == BWRITERSTT_VOLUME) return D3DSTT_VOLUME;
114 FIXME("Unexpected BWRITERSAMPLER_TEXTURE_TYPE type %u\n", bwriter_sampler);
119 DWORD d3d9_register(DWORD bwriter_register) {
120 if(bwriter_register == BWRITERSPR_TEMP) return D3DSPR_TEMP;
121 if(bwriter_register == BWRITERSPR_INPUT) return D3DSPR_INPUT;
122 if(bwriter_register == BWRITERSPR_CONST) return D3DSPR_CONST;
123 if(bwriter_register == BWRITERSPR_ADDR) return D3DSPR_ADDR;
124 if(bwriter_register == BWRITERSPR_TEXTURE) return D3DSPR_TEXTURE;
125 if(bwriter_register == BWRITERSPR_RASTOUT) return D3DSPR_RASTOUT;
126 if(bwriter_register == BWRITERSPR_ATTROUT) return D3DSPR_ATTROUT;
127 if(bwriter_register == BWRITERSPR_TEXCRDOUT) return D3DSPR_TEXCRDOUT;
128 if(bwriter_register == BWRITERSPR_OUTPUT) return D3DSPR_OUTPUT;
129 if(bwriter_register == BWRITERSPR_CONSTINT) return D3DSPR_CONSTINT;
130 if(bwriter_register == BWRITERSPR_COLOROUT) return D3DSPR_COLOROUT;
131 if(bwriter_register == BWRITERSPR_DEPTHOUT) return D3DSPR_DEPTHOUT;
132 if(bwriter_register == BWRITERSPR_SAMPLER) return D3DSPR_SAMPLER;
133 if(bwriter_register == BWRITERSPR_CONSTBOOL) return D3DSPR_CONSTBOOL;
134 if(bwriter_register == BWRITERSPR_LOOP) return D3DSPR_LOOP;
135 if(bwriter_register == BWRITERSPR_MISCTYPE) return D3DSPR_MISCTYPE;
136 if(bwriter_register == BWRITERSPR_LABEL) return D3DSPR_LABEL;
137 if(bwriter_register == BWRITERSPR_PREDICATE) return D3DSPR_PREDICATE;
139 FIXME("Unexpected BWRITERSPR %u\n", bwriter_register);
143 DWORD d3d9_opcode(DWORD bwriter_opcode) {
144 switch(bwriter_opcode) {
145 case BWRITERSIO_NOP: return D3DSIO_NOP;
146 case BWRITERSIO_MOV: return D3DSIO_MOV;
147 case BWRITERSIO_ADD: return D3DSIO_ADD;
148 case BWRITERSIO_SUB: return D3DSIO_SUB;
149 case BWRITERSIO_MAD: return D3DSIO_MAD;
150 case BWRITERSIO_MUL: return D3DSIO_MUL;
151 case BWRITERSIO_RCP: return D3DSIO_RCP;
152 case BWRITERSIO_RSQ: return D3DSIO_RSQ;
153 case BWRITERSIO_DP3: return D3DSIO_DP3;
154 case BWRITERSIO_DP4: return D3DSIO_DP4;
155 case BWRITERSIO_MIN: return D3DSIO_MIN;
156 case BWRITERSIO_MAX: return D3DSIO_MAX;
157 case BWRITERSIO_SLT: return D3DSIO_SLT;
158 case BWRITERSIO_SGE: return D3DSIO_SGE;
159 case BWRITERSIO_EXP: return D3DSIO_EXP;
160 case BWRITERSIO_LOG: return D3DSIO_LOG;
161 case BWRITERSIO_LIT: return D3DSIO_LIT;
162 case BWRITERSIO_DST: return D3DSIO_DST;
163 case BWRITERSIO_LRP: return D3DSIO_LRP;
164 case BWRITERSIO_FRC: return D3DSIO_FRC;
165 case BWRITERSIO_M4x4: return D3DSIO_M4x4;
166 case BWRITERSIO_M4x3: return D3DSIO_M4x3;
167 case BWRITERSIO_M3x4: return D3DSIO_M3x4;
168 case BWRITERSIO_M3x3: return D3DSIO_M3x3;
169 case BWRITERSIO_M3x2: return D3DSIO_M3x2;
170 case BWRITERSIO_CALL: return D3DSIO_CALL;
171 case BWRITERSIO_CALLNZ: return D3DSIO_CALLNZ;
172 case BWRITERSIO_LOOP: return D3DSIO_LOOP;
173 case BWRITERSIO_RET: return D3DSIO_RET;
174 case BWRITERSIO_ENDLOOP: return D3DSIO_ENDLOOP;
175 case BWRITERSIO_LABEL: return D3DSIO_LABEL;
176 case BWRITERSIO_DCL: return D3DSIO_DCL;
177 case BWRITERSIO_POW: return D3DSIO_POW;
178 case BWRITERSIO_CRS: return D3DSIO_CRS;
179 case BWRITERSIO_SGN: return D3DSIO_SGN;
180 case BWRITERSIO_ABS: return D3DSIO_ABS;
181 case BWRITERSIO_NRM: return D3DSIO_NRM;
182 case BWRITERSIO_SINCOS: return D3DSIO_SINCOS;
183 case BWRITERSIO_REP: return D3DSIO_REP;
184 case BWRITERSIO_ENDREP: return D3DSIO_ENDREP;
185 case BWRITERSIO_IF: return D3DSIO_IF;
186 case BWRITERSIO_IFC: return D3DSIO_IFC;
187 case BWRITERSIO_ELSE: return D3DSIO_ELSE;
188 case BWRITERSIO_ENDIF: return D3DSIO_ENDIF;
189 case BWRITERSIO_BREAK: return D3DSIO_BREAK;
190 case BWRITERSIO_BREAKC: return D3DSIO_BREAKC;
191 case BWRITERSIO_MOVA: return D3DSIO_MOVA;
192 case BWRITERSIO_DEFI: return D3DSIO_DEFI;
193 case BWRITERSIO_EXPP: return D3DSIO_EXPP;
194 case BWRITERSIO_LOGP: return D3DSIO_LOGP;
195 case BWRITERSIO_DEF: return D3DSIO_DEF;
196 case BWRITERSIO_SETP: return D3DSIO_SETP;
197 case BWRITERSIO_TEXLDL: return D3DSIO_TEXLDL;
198 case BWRITERSIO_BREAKP: return D3DSIO_BREAKP;
200 case BWRITERSIO_COMMENT: return D3DSIO_COMMENT;
201 case BWRITERSIO_END: return D3DSIO_END;
204 FIXME("Unhandled BWRITERSIO token %u\n", bwriter_opcode);
209 /* Debug print functions */
210 const char *debug_print_srcmod(DWORD mod) {
212 case BWRITERSPSM_NEG: return "D3DSPSM_NEG";
213 case BWRITERSPSM_ABS: return "D3DSPSM_ABS";
214 case BWRITERSPSM_ABSNEG: return "D3DSPSM_ABSNEG";
215 case BWRITERSPSM_NOT: return "D3DSPSM_NOT";
216 default: return "Unknown source modifier\n";
220 const char *debug_print_dstmod(DWORD mod) {
225 case BWRITERSPDM_SATURATE:
227 case BWRITERSPDM_PARTIALPRECISION:
229 case BWRITERSPDM_MSAMPCENTROID:
232 case BWRITERSPDM_SATURATE | BWRITERSPDM_PARTIALPRECISION:
234 case BWRITERSPDM_SATURATE | BWRITERSPDM_MSAMPCENTROID:
235 return "_sat_centroid";
236 case BWRITERSPDM_PARTIALPRECISION | BWRITERSPDM_MSAMPCENTROID:
237 return "_pp_centroid";
239 case BWRITERSPDM_SATURATE | BWRITERSPDM_PARTIALPRECISION | BWRITERSPDM_MSAMPCENTROID:
240 return "_sat_pp_centroid";
243 return "Unexpected modifier\n";
247 static const char *get_regname(const struct shader_reg *reg, shader_type st) {
249 case BWRITERSPR_TEMP:
250 return wine_dbg_sprintf("r%u", reg->regnum);
251 case BWRITERSPR_INPUT:
252 return wine_dbg_sprintf("v%u", reg->regnum);
253 case BWRITERSPR_CONST:
254 return wine_dbg_sprintf("c%u", reg->regnum);
255 /* case BWRITERSPR_ADDR: */
256 case BWRITERSPR_TEXTURE:
257 if(st == ST_VERTEX) {
258 return wine_dbg_sprintf("a%u", reg->regnum);
260 return wine_dbg_sprintf("t%u", reg->regnum);
262 case BWRITERSPR_RASTOUT:
263 switch(reg->regnum) {
264 case BWRITERSRO_POSITION: return "oPos";
265 case BWRITERSRO_FOG: return "oFog";
266 case BWRITERSRO_POINT_SIZE: return "oPts";
267 default: return "Unexpected RASTOUT";
269 case BWRITERSPR_ATTROUT:
270 return wine_dbg_sprintf("oD%u", reg->regnum);
271 /* case BWRITERSPR_TEXCRDOUT: */
272 case BWRITERSPR_OUTPUT:
273 return wine_dbg_sprintf("o[T]%u", reg->regnum);
274 case BWRITERSPR_CONSTINT:
275 return wine_dbg_sprintf("i%u", reg->regnum);
276 case BWRITERSPR_COLOROUT:
277 return wine_dbg_sprintf("oC%u", reg->regnum);
278 case BWRITERSPR_DEPTHOUT:
280 case BWRITERSPR_SAMPLER:
281 return wine_dbg_sprintf("s%u", reg->regnum);
282 case BWRITERSPR_CONSTBOOL:
283 return wine_dbg_sprintf("b%u", reg->regnum);
284 case BWRITERSPR_LOOP:
286 case BWRITERSPR_MISCTYPE:
287 switch(reg->regnum) {
288 case 0: return "vPos";
289 case 1: return "vFace";
290 case 2: return "unexpected misctype";
292 case BWRITERSPR_LABEL:
293 return wine_dbg_sprintf("l%u", reg->regnum);
294 case BWRITERSPR_PREDICATE:
295 return wine_dbg_sprintf("p%u", reg->regnum);
296 default: return "unknown regname";
300 const char *debug_print_writemask(DWORD mask) {
302 unsigned char pos = 1;
304 if(mask == BWRITERSP_WRITEMASK_ALL) return "";
306 if(mask & BWRITERSP_WRITEMASK_0) ret[pos++] = 'x';
307 if(mask & BWRITERSP_WRITEMASK_1) ret[pos++] = 'y';
308 if(mask & BWRITERSP_WRITEMASK_2) ret[pos++] = 'z';
309 if(mask & BWRITERSP_WRITEMASK_3) ret[pos++] = 'w';
311 return wine_dbg_sprintf("%s", ret);
314 const char *debug_print_relarg(const struct shader_reg *reg) {
315 const char *short_swizzle;
316 if(!reg->rel_reg) return "";
318 short_swizzle = debug_print_swizzle(reg->rel_reg->swizzle);
320 if(reg->rel_reg->type == BWRITERSPR_ADDR) {
321 return wine_dbg_sprintf("[a%u%s]", reg->rel_reg->regnum, short_swizzle);
322 } else if(reg->rel_reg->type == BWRITERSPR_LOOP && reg->rel_reg->regnum == 0) {
323 return wine_dbg_sprintf("[aL%s]", short_swizzle);
325 return "Unexpected relative addressing argument";
329 const char *debug_print_dstreg(const struct shader_reg *reg, shader_type st) {
330 return wine_dbg_sprintf("%s%s%s", get_regname(reg, st),
331 debug_print_relarg(reg),
332 debug_print_writemask(reg->writemask));
335 const char *debug_print_swizzle(DWORD arg) {
341 case BWRITERVS_NOSWIZZLE:
343 case BWRITERVS_SWIZZLE_X:
345 case BWRITERVS_SWIZZLE_Y:
347 case BWRITERVS_SWIZZLE_Z:
349 case BWRITERVS_SWIZZLE_W:
353 swizzle[0] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 0)) & 0x03;
354 swizzle[1] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 2)) & 0x03;
355 swizzle[2] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 4)) & 0x03;
356 swizzle[3] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 6)) & 0x03;
359 for(i = 0; i < 4; i++) {
361 case 0: ret[1 + i] = 'x'; break;
362 case 1: ret[1 + i] = 'y'; break;
363 case 2: ret[1 + i] = 'z'; break;
364 case 3: ret[1 + i] = 'w'; break;
368 return wine_dbg_sprintf("%s", ret);
371 const char *debug_print_srcreg(const struct shader_reg *reg, shader_type st) {
372 switch(reg->srcmod) {
373 case BWRITERSPSM_NONE:
374 return wine_dbg_sprintf("%s%s%s", get_regname(reg, st),
375 debug_print_relarg(reg),
376 debug_print_swizzle(reg->swizzle));
377 case BWRITERSPSM_NEG:
378 return wine_dbg_sprintf("-%s%s%s", get_regname(reg, st),
379 debug_print_relarg(reg),
380 debug_print_swizzle(reg->swizzle));
381 case BWRITERSPSM_ABS:
382 return wine_dbg_sprintf("%s%s_abs%s", get_regname(reg, st),
383 debug_print_relarg(reg),
384 debug_print_swizzle(reg->swizzle));
385 case BWRITERSPSM_ABSNEG:
386 return wine_dbg_sprintf("-%s%s_abs%s", get_regname(reg, st),
387 debug_print_relarg(reg),
388 debug_print_swizzle(reg->swizzle));
389 case BWRITERSPSM_NOT:
390 return wine_dbg_sprintf("!%s%s%s", get_regname(reg, st),
391 debug_print_relarg(reg),
392 debug_print_swizzle(reg->swizzle));
394 return "Unknown modifier";
397 const char *debug_print_comp(DWORD comp) {
399 case BWRITER_COMPARISON_NONE: return "";
400 case BWRITER_COMPARISON_GT: return "_gt";
401 case BWRITER_COMPARISON_EQ: return "_eq";
402 case BWRITER_COMPARISON_GE: return "_ge";
403 case BWRITER_COMPARISON_LT: return "_lt";
404 case BWRITER_COMPARISON_NE: return "_ne";
405 case BWRITER_COMPARISON_LE: return "_le";
406 default: return "_unknown";
410 const char *debug_print_opcode(DWORD opcode) {
412 case BWRITERSIO_NOP: return "nop";
413 case BWRITERSIO_MOV: return "mov";
414 case BWRITERSIO_ADD: return "add";
415 case BWRITERSIO_SUB: return "sub";
416 case BWRITERSIO_MAD: return "mad";
417 case BWRITERSIO_MUL: return "mul";
418 case BWRITERSIO_RCP: return "rcp";
419 case BWRITERSIO_RSQ: return "rsq";
420 case BWRITERSIO_DP3: return "dp3";
421 case BWRITERSIO_DP4: return "dp4";
422 case BWRITERSIO_MIN: return "min";
423 case BWRITERSIO_MAX: return "max";
424 case BWRITERSIO_SLT: return "slt";
425 case BWRITERSIO_SGE: return "sge";
426 case BWRITERSIO_EXP: return "exp";
427 case BWRITERSIO_LOG: return "log";
428 case BWRITERSIO_LIT: return "lit";
429 case BWRITERSIO_DST: return "dst";
430 case BWRITERSIO_LRP: return "lrp";
431 case BWRITERSIO_FRC: return "frc";
432 case BWRITERSIO_M4x4: return "m4x4";
433 case BWRITERSIO_M4x3: return "m4x3";
434 case BWRITERSIO_M3x4: return "m3x4";
435 case BWRITERSIO_M3x3: return "m3x3";
436 case BWRITERSIO_M3x2: return "m3x2";
437 case BWRITERSIO_CALL: return "call";
438 case BWRITERSIO_CALLNZ: return "callnz";
439 case BWRITERSIO_LOOP: return "loop";
440 case BWRITERSIO_RET: return "ret";
441 case BWRITERSIO_ENDLOOP: return "endloop";
442 case BWRITERSIO_LABEL: return "label";
443 case BWRITERSIO_DCL: return "dcl";
444 case BWRITERSIO_POW: return "pow";
445 case BWRITERSIO_CRS: return "crs";
446 case BWRITERSIO_SGN: return "sgn";
447 case BWRITERSIO_ABS: return "abs";
448 case BWRITERSIO_NRM: return "nrm";
449 case BWRITERSIO_SINCOS: return "sincos";
450 case BWRITERSIO_REP: return "rep";
451 case BWRITERSIO_ENDREP: return "endrep";
452 case BWRITERSIO_IF: return "if";
453 case BWRITERSIO_IFC: return "ifc";
454 case BWRITERSIO_ELSE: return "else";
455 case BWRITERSIO_ENDIF: return "endif";
456 case BWRITERSIO_BREAK: return "break";
457 case BWRITERSIO_BREAKC: return "breakc";
458 case BWRITERSIO_MOVA: return "mova";
459 case BWRITERSIO_DEFI: return "defi";
460 case BWRITERSIO_EXPP: return "expp";
461 case BWRITERSIO_LOGP: return "logp";
462 case BWRITERSIO_DEF: return "def";
463 case BWRITERSIO_SETP: return "setp";
464 case BWRITERSIO_TEXLDL: return "texldl";
465 case BWRITERSIO_BREAKP: return "breakp";
467 default: return "unknown";