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
;
78 FIXME("Unhandled BWRITERSPSM token %u\n", bwriter_srcmod
);
83 DWORD
d3d9_dstmod(DWORD bwriter_mod
) {
86 if(bwriter_mod
& BWRITERSPDM_SATURATE
) ret
|= D3DSPDM_SATURATE
;
87 if(bwriter_mod
& BWRITERSPDM_PARTIALPRECISION
) ret
|= D3DSPDM_PARTIALPRECISION
;
88 if(bwriter_mod
& BWRITERSPDM_MSAMPCENTROID
) ret
|= D3DSPDM_MSAMPCENTROID
;
93 DWORD
d3d9_register(DWORD bwriter_register
) {
94 if(bwriter_register
== BWRITERSPR_TEMP
) return D3DSPR_TEMP
;
95 if(bwriter_register
== BWRITERSPR_INPUT
) return D3DSPR_INPUT
;
96 if(bwriter_register
== BWRITERSPR_CONST
) return D3DSPR_CONST
;
97 if(bwriter_register
== BWRITERSPR_ADDR
) return D3DSPR_ADDR
;
98 if(bwriter_register
== BWRITERSPR_TEXTURE
) return D3DSPR_TEXTURE
;
99 if(bwriter_register
== BWRITERSPR_RASTOUT
) return D3DSPR_RASTOUT
;
100 if(bwriter_register
== BWRITERSPR_ATTROUT
) return D3DSPR_ATTROUT
;
101 if(bwriter_register
== BWRITERSPR_TEXCRDOUT
) return D3DSPR_TEXCRDOUT
;
102 if(bwriter_register
== BWRITERSPR_OUTPUT
) return D3DSPR_OUTPUT
;
103 if(bwriter_register
== BWRITERSPR_CONSTINT
) return D3DSPR_CONSTINT
;
104 if(bwriter_register
== BWRITERSPR_COLOROUT
) return D3DSPR_COLOROUT
;
105 if(bwriter_register
== BWRITERSPR_DEPTHOUT
) return D3DSPR_DEPTHOUT
;
106 if(bwriter_register
== BWRITERSPR_SAMPLER
) return D3DSPR_SAMPLER
;
107 if(bwriter_register
== BWRITERSPR_CONSTBOOL
) return D3DSPR_CONSTBOOL
;
108 if(bwriter_register
== BWRITERSPR_LOOP
) return D3DSPR_LOOP
;
109 if(bwriter_register
== BWRITERSPR_MISCTYPE
) return D3DSPR_MISCTYPE
;
110 if(bwriter_register
== BWRITERSPR_LABEL
) return D3DSPR_LABEL
;
111 if(bwriter_register
== BWRITERSPR_PREDICATE
) return D3DSPR_PREDICATE
;
113 FIXME("Unexpected BWRITERSPR %u\n", bwriter_register
);
117 DWORD
d3d9_opcode(DWORD bwriter_opcode
) {
118 switch(bwriter_opcode
) {
119 case BWRITERSIO_NOP
: return D3DSIO_NOP
;
120 case BWRITERSIO_MOV
: return D3DSIO_MOV
;
121 case BWRITERSIO_ADD
: return D3DSIO_ADD
;
122 case BWRITERSIO_SUB
: return D3DSIO_SUB
;
123 case BWRITERSIO_MAD
: return D3DSIO_MAD
;
124 case BWRITERSIO_MUL
: return D3DSIO_MUL
;
125 case BWRITERSIO_RCP
: return D3DSIO_RCP
;
126 case BWRITERSIO_RSQ
: return D3DSIO_RSQ
;
127 case BWRITERSIO_DP3
: return D3DSIO_DP3
;
128 case BWRITERSIO_DP4
: return D3DSIO_DP4
;
129 case BWRITERSIO_MIN
: return D3DSIO_MIN
;
130 case BWRITERSIO_MAX
: return D3DSIO_MAX
;
131 case BWRITERSIO_SLT
: return D3DSIO_SLT
;
132 case BWRITERSIO_SGE
: return D3DSIO_SGE
;
133 case BWRITERSIO_EXP
: return D3DSIO_EXP
;
134 case BWRITERSIO_LOG
: return D3DSIO_LOG
;
135 case BWRITERSIO_LIT
: return D3DSIO_LIT
;
136 case BWRITERSIO_DST
: return D3DSIO_DST
;
137 case BWRITERSIO_LRP
: return D3DSIO_LRP
;
138 case BWRITERSIO_FRC
: return D3DSIO_FRC
;
139 case BWRITERSIO_M4x4
: return D3DSIO_M4x4
;
140 case BWRITERSIO_M4x3
: return D3DSIO_M4x3
;
141 case BWRITERSIO_M3x4
: return D3DSIO_M3x4
;
142 case BWRITERSIO_M3x3
: return D3DSIO_M3x3
;
143 case BWRITERSIO_M3x2
: return D3DSIO_M3x2
;
144 case BWRITERSIO_POW
: return D3DSIO_POW
;
145 case BWRITERSIO_CRS
: return D3DSIO_CRS
;
146 case BWRITERSIO_SGN
: return D3DSIO_SGN
;
147 case BWRITERSIO_ABS
: return D3DSIO_ABS
;
148 case BWRITERSIO_NRM
: return D3DSIO_NRM
;
149 case BWRITERSIO_SINCOS
: return D3DSIO_SINCOS
;
150 case BWRITERSIO_MOVA
: return D3DSIO_MOVA
;
151 case BWRITERSIO_EXPP
: return D3DSIO_EXPP
;
152 case BWRITERSIO_LOGP
: return D3DSIO_LOGP
;
153 case BWRITERSIO_TEXLDL
: return D3DSIO_TEXLDL
;
155 case BWRITERSIO_COMMENT
: return D3DSIO_COMMENT
;
156 case BWRITERSIO_END
: return D3DSIO_END
;
159 FIXME("Unhandled BWRITERSIO token %u\n", bwriter_opcode
);
164 /* Debug print functions */
165 const char *debug_print_srcmod(DWORD mod
) {
167 case BWRITERSPSM_NEG
: return "D3DSPSM_NEG";
168 case BWRITERSPSM_ABS
: return "D3DSPSM_ABS";
169 case BWRITERSPSM_ABSNEG
: return "D3DSPSM_ABSNEG";
170 default: return "Unknown source modifier\n";
174 const char *debug_print_dstmod(DWORD mod
) {
179 case BWRITERSPDM_SATURATE
:
181 case BWRITERSPDM_PARTIALPRECISION
:
183 case BWRITERSPDM_MSAMPCENTROID
:
186 case BWRITERSPDM_SATURATE
| BWRITERSPDM_PARTIALPRECISION
:
188 case BWRITERSPDM_SATURATE
| BWRITERSPDM_MSAMPCENTROID
:
189 return "_sat_centroid";
190 case BWRITERSPDM_PARTIALPRECISION
| BWRITERSPDM_MSAMPCENTROID
:
191 return "_pp_centroid";
193 case BWRITERSPDM_SATURATE
| BWRITERSPDM_PARTIALPRECISION
| BWRITERSPDM_MSAMPCENTROID
:
194 return "_sat_pp_centroid";
197 return "Unexpected modifier\n";
201 static const char *get_regname(const struct shader_reg
*reg
, shader_type st
) {
203 case BWRITERSPR_TEMP
:
204 return wine_dbg_sprintf("r%u", reg
->regnum
);
205 case BWRITERSPR_INPUT
:
206 return wine_dbg_sprintf("v%u", reg
->regnum
);
207 case BWRITERSPR_CONST
:
208 return wine_dbg_sprintf("c%u", reg
->regnum
);
209 /* case BWRITERSPR_ADDR: */
210 case BWRITERSPR_TEXTURE
:
211 if(st
== ST_VERTEX
) {
212 return wine_dbg_sprintf("a%u", reg
->regnum
);
214 return wine_dbg_sprintf("t%u", reg
->regnum
);
216 case BWRITERSPR_RASTOUT
:
217 switch(reg
->regnum
) {
218 case BWRITERSRO_POSITION
: return "oPos";
219 case BWRITERSRO_FOG
: return "oFog";
220 case BWRITERSRO_POINT_SIZE
: return "oPts";
221 default: return "Unexpected RASTOUT";
223 case BWRITERSPR_ATTROUT
:
224 return wine_dbg_sprintf("oD%u", reg
->regnum
);
225 /* case BWRITERSPR_TEXCRDOUT: */
226 case BWRITERSPR_OUTPUT
:
227 return wine_dbg_sprintf("o[T]%u", reg
->regnum
);
228 case BWRITERSPR_CONSTINT
:
229 return wine_dbg_sprintf("i%u", reg
->regnum
);
230 case BWRITERSPR_COLOROUT
:
231 return wine_dbg_sprintf("oC%u", reg
->regnum
);
232 case BWRITERSPR_DEPTHOUT
:
234 case BWRITERSPR_SAMPLER
:
235 return wine_dbg_sprintf("s%u", reg
->regnum
);
236 case BWRITERSPR_CONSTBOOL
:
237 return wine_dbg_sprintf("b%u", reg
->regnum
);
238 case BWRITERSPR_LOOP
:
240 case BWRITERSPR_MISCTYPE
:
241 switch(reg
->regnum
) {
242 case 0: return "vPos";
243 case 1: return "vFace";
244 case 2: return "unexpected misctype";
246 case BWRITERSPR_LABEL
:
247 return wine_dbg_sprintf("l%u", reg
->regnum
);
248 case BWRITERSPR_PREDICATE
:
249 return wine_dbg_sprintf("p%u", reg
->regnum
);
250 default: return "unknown regname";
254 const char *debug_print_writemask(DWORD mask
) {
256 unsigned char pos
= 1;
258 if(mask
== BWRITERSP_WRITEMASK_ALL
) return "";
260 if(mask
& BWRITERSP_WRITEMASK_0
) ret
[pos
++] = 'x';
261 if(mask
& BWRITERSP_WRITEMASK_1
) ret
[pos
++] = 'y';
262 if(mask
& BWRITERSP_WRITEMASK_2
) ret
[pos
++] = 'z';
263 if(mask
& BWRITERSP_WRITEMASK_3
) ret
[pos
++] = 'w';
265 return wine_dbg_sprintf("%s", ret
);
268 const char *debug_print_relarg(const struct shader_reg
*reg
) {
269 const char *short_swizzle
;
270 if(!reg
->rel_reg
) return "";
272 short_swizzle
= debug_print_swizzle(reg
->rel_reg
->swizzle
);
274 if(reg
->rel_reg
->type
== BWRITERSPR_ADDR
) {
275 return wine_dbg_sprintf("[a%u%s]", reg
->rel_reg
->regnum
, short_swizzle
);
276 } else if(reg
->rel_reg
->type
== BWRITERSPR_LOOP
&& reg
->rel_reg
->regnum
== 0) {
277 return wine_dbg_sprintf("[aL%s]", short_swizzle
);
279 return "Unexpected relative addressing argument";
283 const char *debug_print_dstreg(const struct shader_reg
*reg
, shader_type st
) {
284 return wine_dbg_sprintf("%s%s%s", get_regname(reg
, st
),
285 debug_print_relarg(reg
),
286 debug_print_writemask(reg
->writemask
));
289 const char *debug_print_swizzle(DWORD arg
) {
295 case BWRITERVS_NOSWIZZLE
:
297 case BWRITERVS_SWIZZLE_X
:
299 case BWRITERVS_SWIZZLE_Y
:
301 case BWRITERVS_SWIZZLE_Z
:
303 case BWRITERVS_SWIZZLE_W
:
307 swizzle
[0] = (arg
>> (BWRITERVS_SWIZZLE_SHIFT
+ 0)) & 0x03;
308 swizzle
[1] = (arg
>> (BWRITERVS_SWIZZLE_SHIFT
+ 2)) & 0x03;
309 swizzle
[2] = (arg
>> (BWRITERVS_SWIZZLE_SHIFT
+ 4)) & 0x03;
310 swizzle
[3] = (arg
>> (BWRITERVS_SWIZZLE_SHIFT
+ 6)) & 0x03;
313 for(i
= 0; i
< 4; i
++) {
315 case 0: ret
[1 + i
] = 'x'; break;
316 case 1: ret
[1 + i
] = 'y'; break;
317 case 2: ret
[1 + i
] = 'z'; break;
318 case 3: ret
[1 + i
] = 'w'; break;
322 return wine_dbg_sprintf("%s", ret
);
325 const char *debug_print_srcreg(const struct shader_reg
*reg
, shader_type st
) {
326 switch(reg
->srcmod
) {
327 case BWRITERSPSM_NONE
:
328 return wine_dbg_sprintf("%s%s%s", get_regname(reg
, st
),
329 debug_print_relarg(reg
),
330 debug_print_swizzle(reg
->swizzle
));
331 case BWRITERSPSM_NEG
:
332 return wine_dbg_sprintf("-%s%s%s", get_regname(reg
, st
),
333 debug_print_relarg(reg
),
334 debug_print_swizzle(reg
->swizzle
));
335 case BWRITERSPSM_ABS
:
336 return wine_dbg_sprintf("%s%s_abs%s", get_regname(reg
, st
),
337 debug_print_relarg(reg
),
338 debug_print_swizzle(reg
->swizzle
));
339 case BWRITERSPSM_ABSNEG
:
340 return wine_dbg_sprintf("-%s%s_abs%s", get_regname(reg
, st
),
341 debug_print_relarg(reg
),
342 debug_print_swizzle(reg
->swizzle
));
344 return "Unknown modifier";
347 const char *debug_print_opcode(DWORD opcode
) {
349 case BWRITERSIO_NOP
: return "nop";
350 case BWRITERSIO_MOV
: return "mov";
351 case BWRITERSIO_ADD
: return "add";
352 case BWRITERSIO_SUB
: return "sub";
353 case BWRITERSIO_MAD
: return "mad";
354 case BWRITERSIO_MUL
: return "mul";
355 case BWRITERSIO_RCP
: return "rcp";
356 case BWRITERSIO_RSQ
: return "rsq";
357 case BWRITERSIO_DP3
: return "dp3";
358 case BWRITERSIO_DP4
: return "dp4";
359 case BWRITERSIO_MIN
: return "min";
360 case BWRITERSIO_MAX
: return "max";
361 case BWRITERSIO_SLT
: return "slt";
362 case BWRITERSIO_SGE
: return "sge";
363 case BWRITERSIO_EXP
: return "exp";
364 case BWRITERSIO_LOG
: return "log";
365 case BWRITERSIO_LIT
: return "lit";
366 case BWRITERSIO_DST
: return "dst";
367 case BWRITERSIO_LRP
: return "lrp";
368 case BWRITERSIO_FRC
: return "frc";
369 case BWRITERSIO_M4x4
: return "m4x4";
370 case BWRITERSIO_M4x3
: return "m4x3";
371 case BWRITERSIO_M3x4
: return "m3x4";
372 case BWRITERSIO_M3x3
: return "m3x3";
373 case BWRITERSIO_M3x2
: return "m3x2";
374 case BWRITERSIO_POW
: return "pow";
375 case BWRITERSIO_CRS
: return "crs";
376 case BWRITERSIO_SGN
: return "sgn";
377 case BWRITERSIO_ABS
: return "abs";
378 case BWRITERSIO_NRM
: return "nrm";
379 case BWRITERSIO_SINCOS
: return "sincos";
380 case BWRITERSIO_MOVA
: return "mova";
381 case BWRITERSIO_EXPP
: return "expp";
382 case BWRITERSIO_LOGP
: return "logp";
383 case BWRITERSIO_TEXLDL
: return "texldl";
385 default: return "unknown";