d3dcompiler: Unwrap swizzles from the left-hand side of an assignment.
[wine.git] / dlls / d3dcompiler_43 / utils.c
blob9c388008081681f66d37c5345c65564f50f2c8c0
1 /*
2 * Copyright 2008 Stefan Dösinger
3 * Copyright 2009 Matteo Bruni
4 * Copyright 2008-2009 Henri Verbeet for CodeWeavers
5 * Copyright 2010 Rico Schüller
6 * Copyright 2012 Matteo Bruni for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <stdio.h>
26 #include "d3dcompiler_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
30 #define WINE_D3DCOMPILER_TO_STR(x) case x: return #x
32 const char *debug_d3dcompiler_shader_variable_class(D3D_SHADER_VARIABLE_CLASS c)
34 switch (c)
36 WINE_D3DCOMPILER_TO_STR(D3D_SVC_SCALAR);
37 WINE_D3DCOMPILER_TO_STR(D3D_SVC_VECTOR);
38 WINE_D3DCOMPILER_TO_STR(D3D_SVC_MATRIX_ROWS);
39 WINE_D3DCOMPILER_TO_STR(D3D_SVC_MATRIX_COLUMNS);
40 WINE_D3DCOMPILER_TO_STR(D3D_SVC_OBJECT);
41 WINE_D3DCOMPILER_TO_STR(D3D_SVC_STRUCT);
42 WINE_D3DCOMPILER_TO_STR(D3D_SVC_INTERFACE_CLASS);
43 WINE_D3DCOMPILER_TO_STR(D3D_SVC_INTERFACE_POINTER);
44 default:
45 FIXME("Unrecognized D3D_SHADER_VARIABLE_CLASS %#x.\n", c);
46 return "unrecognized";
50 const char *debug_d3dcompiler_shader_variable_type(D3D_SHADER_VARIABLE_TYPE t)
52 switch (t)
54 WINE_D3DCOMPILER_TO_STR(D3D_SVT_VOID);
55 WINE_D3DCOMPILER_TO_STR(D3D_SVT_BOOL);
56 WINE_D3DCOMPILER_TO_STR(D3D_SVT_INT);
57 WINE_D3DCOMPILER_TO_STR(D3D_SVT_FLOAT);
58 WINE_D3DCOMPILER_TO_STR(D3D_SVT_STRING);
59 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE);
60 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE1D);
61 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE2D);
62 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE3D);
63 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURECUBE);
64 WINE_D3DCOMPILER_TO_STR(D3D_SVT_SAMPLER);
65 WINE_D3DCOMPILER_TO_STR(D3D_SVT_PIXELSHADER);
66 WINE_D3DCOMPILER_TO_STR(D3D_SVT_VERTEXSHADER);
67 WINE_D3DCOMPILER_TO_STR(D3D_SVT_UINT);
68 WINE_D3DCOMPILER_TO_STR(D3D_SVT_UINT8);
69 WINE_D3DCOMPILER_TO_STR(D3D_SVT_GEOMETRYSHADER);
70 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RASTERIZER);
71 WINE_D3DCOMPILER_TO_STR(D3D_SVT_DEPTHSTENCIL);
72 WINE_D3DCOMPILER_TO_STR(D3D_SVT_BLEND);
73 WINE_D3DCOMPILER_TO_STR(D3D_SVT_BUFFER);
74 WINE_D3DCOMPILER_TO_STR(D3D_SVT_CBUFFER);
75 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TBUFFER);
76 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE1DARRAY);
77 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE2DARRAY);
78 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RENDERTARGETVIEW);
79 WINE_D3DCOMPILER_TO_STR(D3D_SVT_DEPTHSTENCILVIEW);
80 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE2DMS);
81 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE2DMSARRAY);
82 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURECUBEARRAY);
83 WINE_D3DCOMPILER_TO_STR(D3D_SVT_HULLSHADER);
84 WINE_D3DCOMPILER_TO_STR(D3D_SVT_DOMAINSHADER);
85 WINE_D3DCOMPILER_TO_STR(D3D_SVT_INTERFACE_POINTER);
86 WINE_D3DCOMPILER_TO_STR(D3D_SVT_COMPUTESHADER);
87 WINE_D3DCOMPILER_TO_STR(D3D_SVT_DOUBLE);
88 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE1D);
89 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE1DARRAY);
90 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE2D);
91 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE2DARRAY);
92 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE3D);
93 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWBUFFER);
94 WINE_D3DCOMPILER_TO_STR(D3D_SVT_BYTEADDRESS_BUFFER);
95 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWBYTEADDRESS_BUFFER);
96 WINE_D3DCOMPILER_TO_STR(D3D_SVT_STRUCTURED_BUFFER);
97 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWSTRUCTURED_BUFFER);
98 WINE_D3DCOMPILER_TO_STR(D3D_SVT_APPEND_STRUCTURED_BUFFER);
99 WINE_D3DCOMPILER_TO_STR(D3D_SVT_CONSUME_STRUCTURED_BUFFER);
100 default:
101 FIXME("Unrecognized D3D_SHADER_VARIABLE_TYPE %#x.\n", t);
102 return "unrecognized";
106 const char *debug_d3dcompiler_d3d_blob_part(D3D_BLOB_PART part)
108 switch(part)
110 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_INPUT_SIGNATURE_BLOB);
111 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_OUTPUT_SIGNATURE_BLOB);
112 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB);
113 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB);
114 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_ALL_SIGNATURE_BLOB);
115 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_DEBUG_INFO);
116 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_LEGACY_SHADER);
117 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_XNA_PREPASS_SHADER);
118 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_XNA_SHADER);
119 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_TEST_ALTERNATE_SHADER);
120 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_TEST_COMPILE_DETAILS);
121 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_TEST_COMPILE_PERF);
122 default:
123 FIXME("Unrecognized D3D_BLOB_PART %#x\n", part);
124 return "unrecognized";
128 const char *debug_print_srcmod(DWORD mod)
130 switch (mod)
132 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_NEG);
133 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_BIAS);
134 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_BIASNEG);
135 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_SIGN);
136 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_SIGNNEG);
137 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_COMP);
138 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_X2);
139 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_X2NEG);
140 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_DZ);
141 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_DW);
142 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_ABS);
143 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_ABSNEG);
144 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_NOT);
145 default:
146 FIXME("Unrecognized source modifier %#x.\n", mod);
147 return "unrecognized_src_mod";
151 #undef WINE_D3DCOMPILER_TO_STR
153 const char *debug_print_dstmod(DWORD mod)
155 switch (mod)
157 case 0:
158 return "";
159 case BWRITERSPDM_SATURATE:
160 return "_sat";
161 case BWRITERSPDM_PARTIALPRECISION:
162 return "_pp";
163 case BWRITERSPDM_MSAMPCENTROID:
164 return "_centroid";
165 case BWRITERSPDM_SATURATE | BWRITERSPDM_PARTIALPRECISION:
166 return "_sat_pp";
167 case BWRITERSPDM_SATURATE | BWRITERSPDM_MSAMPCENTROID:
168 return "_sat_centroid";
169 case BWRITERSPDM_PARTIALPRECISION | BWRITERSPDM_MSAMPCENTROID:
170 return "_pp_centroid";
171 case BWRITERSPDM_SATURATE | BWRITERSPDM_PARTIALPRECISION | BWRITERSPDM_MSAMPCENTROID:
172 return "_sat_pp_centroid";
173 default:
174 return "Unexpected modifier\n";
178 const char *debug_print_shift(DWORD shift)
180 static const char * const shiftstrings[] =
183 "_x2",
184 "_x4",
185 "_x8",
186 "_x16",
187 "_x32",
194 "_d16",
195 "_d8",
196 "_d4",
197 "_d2",
199 return shiftstrings[shift];
202 static const char *get_regname(const struct shader_reg *reg)
204 switch (reg->type)
206 case BWRITERSPR_TEMP:
207 return wine_dbg_sprintf("r%u", reg->regnum);
208 case BWRITERSPR_INPUT:
209 return wine_dbg_sprintf("v%u", reg->regnum);
210 case BWRITERSPR_CONST:
211 return wine_dbg_sprintf("c%u", reg->regnum);
212 case BWRITERSPR_ADDR:
213 return wine_dbg_sprintf("a%u", reg->regnum);
214 case BWRITERSPR_TEXTURE:
215 return wine_dbg_sprintf("t%u", reg->regnum);
216 case BWRITERSPR_RASTOUT:
217 switch (reg->regnum)
219 case BWRITERSRO_POSITION: return "oPos";
220 case BWRITERSRO_FOG: return "oFog";
221 case BWRITERSRO_POINT_SIZE: return "oPts";
222 default: return "Unexpected RASTOUT";
224 case BWRITERSPR_ATTROUT:
225 return wine_dbg_sprintf("oD%u", reg->regnum);
226 case BWRITERSPR_TEXCRDOUT:
227 return wine_dbg_sprintf("oT%u", reg->regnum);
228 case BWRITERSPR_OUTPUT:
229 return wine_dbg_sprintf("o%u", reg->regnum);
230 case BWRITERSPR_CONSTINT:
231 return wine_dbg_sprintf("i%u", reg->regnum);
232 case BWRITERSPR_COLOROUT:
233 return wine_dbg_sprintf("oC%u", reg->regnum);
234 case BWRITERSPR_DEPTHOUT:
235 return "oDepth";
236 case BWRITERSPR_SAMPLER:
237 return wine_dbg_sprintf("s%u", reg->regnum);
238 case BWRITERSPR_CONSTBOOL:
239 return wine_dbg_sprintf("b%u", reg->regnum);
240 case BWRITERSPR_LOOP:
241 return "aL";
242 case BWRITERSPR_MISCTYPE:
243 switch (reg->regnum)
245 case 0: return "vPos";
246 case 1: return "vFace";
247 default: return "unexpected misctype";
249 case BWRITERSPR_LABEL:
250 return wine_dbg_sprintf("l%u", reg->regnum);
251 case BWRITERSPR_PREDICATE:
252 return wine_dbg_sprintf("p%u", reg->regnum);
253 default:
254 return wine_dbg_sprintf("unknown regname %#x", reg->type);
258 static const char *debug_print_writemask(DWORD mask)
260 char ret[6];
261 unsigned char pos = 1;
263 if(mask == BWRITERSP_WRITEMASK_ALL) return "";
264 ret[0] = '.';
265 if(mask & BWRITERSP_WRITEMASK_0) ret[pos++] = 'x';
266 if(mask & BWRITERSP_WRITEMASK_1) ret[pos++] = 'y';
267 if(mask & BWRITERSP_WRITEMASK_2) ret[pos++] = 'z';
268 if(mask & BWRITERSP_WRITEMASK_3) ret[pos++] = 'w';
269 ret[pos] = 0;
271 return wine_dbg_sprintf("%s", ret);
274 static const char *debug_print_swizzle(DWORD arg)
276 char ret[6];
277 unsigned int i;
278 DWORD swizzle[4];
280 switch (arg)
282 case BWRITERVS_NOSWIZZLE:
283 return "";
284 case BWRITERVS_SWIZZLE_X:
285 return ".x";
286 case BWRITERVS_SWIZZLE_Y:
287 return ".y";
288 case BWRITERVS_SWIZZLE_Z:
289 return ".z";
290 case BWRITERVS_SWIZZLE_W:
291 return ".w";
294 swizzle[0] = arg & 3;
295 swizzle[1] = (arg >> 2) & 3;
296 swizzle[2] = (arg >> 4) & 3;
297 swizzle[3] = (arg >> 6) & 3;
299 ret[0] = '.';
300 for (i = 0; i < 4; ++i)
302 switch (swizzle[i])
304 case 0: ret[1 + i] = 'x'; break;
305 case 1: ret[1 + i] = 'y'; break;
306 case 2: ret[1 + i] = 'z'; break;
307 case 3: ret[1 + i] = 'w'; break;
310 ret[5] = '\0';
312 return wine_dbg_sprintf("%s", ret);
315 static const char *debug_print_relarg(const struct shader_reg *reg)
317 const char *short_swizzle;
318 if (!reg->rel_reg) return "";
320 short_swizzle = debug_print_swizzle(reg->rel_reg->u.swizzle);
322 if (reg->rel_reg->type == BWRITERSPR_ADDR)
323 return wine_dbg_sprintf("[a%u%s]", reg->rel_reg->regnum, short_swizzle);
324 else if(reg->rel_reg->type == BWRITERSPR_LOOP && reg->rel_reg->regnum == 0)
325 return wine_dbg_sprintf("[aL%s]", short_swizzle);
326 else
327 return "Unexpected relative addressing argument";
330 const char *debug_print_dstreg(const struct shader_reg *reg)
332 return wine_dbg_sprintf("%s%s%s", get_regname(reg),
333 debug_print_relarg(reg),
334 debug_print_writemask(reg->u.writemask));
337 const char *debug_print_srcreg(const struct shader_reg *reg)
339 switch (reg->srcmod)
341 case BWRITERSPSM_NONE:
342 return wine_dbg_sprintf("%s%s%s", get_regname(reg),
343 debug_print_relarg(reg),
344 debug_print_swizzle(reg->u.swizzle));
345 case BWRITERSPSM_NEG:
346 return wine_dbg_sprintf("-%s%s%s", get_regname(reg),
347 debug_print_relarg(reg),
348 debug_print_swizzle(reg->u.swizzle));
349 case BWRITERSPSM_BIAS:
350 return wine_dbg_sprintf("%s%s_bias%s", get_regname(reg),
351 debug_print_relarg(reg),
352 debug_print_swizzle(reg->u.swizzle));
353 case BWRITERSPSM_BIASNEG:
354 return wine_dbg_sprintf("-%s%s_bias%s", get_regname(reg),
355 debug_print_relarg(reg),
356 debug_print_swizzle(reg->u.swizzle));
357 case BWRITERSPSM_SIGN:
358 return wine_dbg_sprintf("%s%s_bx2%s", get_regname(reg),
359 debug_print_relarg(reg),
360 debug_print_swizzle(reg->u.swizzle));
361 case BWRITERSPSM_SIGNNEG:
362 return wine_dbg_sprintf("-%s%s_bx2%s", get_regname(reg),
363 debug_print_relarg(reg),
364 debug_print_swizzle(reg->u.swizzle));
365 case BWRITERSPSM_COMP:
366 return wine_dbg_sprintf("1 - %s%s%s", get_regname(reg),
367 debug_print_relarg(reg),
368 debug_print_swizzle(reg->u.swizzle));
369 case BWRITERSPSM_X2:
370 return wine_dbg_sprintf("%s%s_x2%s", get_regname(reg),
371 debug_print_relarg(reg),
372 debug_print_swizzle(reg->u.swizzle));
373 case BWRITERSPSM_X2NEG:
374 return wine_dbg_sprintf("-%s%s_x2%s", get_regname(reg),
375 debug_print_relarg(reg),
376 debug_print_swizzle(reg->u.swizzle));
377 case BWRITERSPSM_DZ:
378 return wine_dbg_sprintf("%s%s_dz%s", get_regname(reg),
379 debug_print_relarg(reg),
380 debug_print_swizzle(reg->u.swizzle));
381 case BWRITERSPSM_DW:
382 return wine_dbg_sprintf("%s%s_dw%s", get_regname(reg),
383 debug_print_relarg(reg),
384 debug_print_swizzle(reg->u.swizzle));
385 case BWRITERSPSM_ABS:
386 return wine_dbg_sprintf("%s%s_abs%s", get_regname(reg),
387 debug_print_relarg(reg),
388 debug_print_swizzle(reg->u.swizzle));
389 case BWRITERSPSM_ABSNEG:
390 return wine_dbg_sprintf("-%s%s_abs%s", get_regname(reg),
391 debug_print_relarg(reg),
392 debug_print_swizzle(reg->u.swizzle));
393 case BWRITERSPSM_NOT:
394 return wine_dbg_sprintf("!%s%s%s", get_regname(reg),
395 debug_print_relarg(reg),
396 debug_print_swizzle(reg->u.swizzle));
398 return "Unknown modifier";
401 const char *debug_print_comp(DWORD comp)
403 switch (comp)
405 case BWRITER_COMPARISON_NONE: return "";
406 case BWRITER_COMPARISON_GT: return "_gt";
407 case BWRITER_COMPARISON_EQ: return "_eq";
408 case BWRITER_COMPARISON_GE: return "_ge";
409 case BWRITER_COMPARISON_LT: return "_lt";
410 case BWRITER_COMPARISON_NE: return "_ne";
411 case BWRITER_COMPARISON_LE: return "_le";
412 default: return "_unknown";
416 const char *debug_print_opcode(DWORD opcode)
418 switch (opcode)
420 case BWRITERSIO_NOP: return "nop";
421 case BWRITERSIO_MOV: return "mov";
422 case BWRITERSIO_ADD: return "add";
423 case BWRITERSIO_SUB: return "sub";
424 case BWRITERSIO_MAD: return "mad";
425 case BWRITERSIO_MUL: return "mul";
426 case BWRITERSIO_RCP: return "rcp";
427 case BWRITERSIO_RSQ: return "rsq";
428 case BWRITERSIO_DP3: return "dp3";
429 case BWRITERSIO_DP4: return "dp4";
430 case BWRITERSIO_MIN: return "min";
431 case BWRITERSIO_MAX: return "max";
432 case BWRITERSIO_SLT: return "slt";
433 case BWRITERSIO_SGE: return "sge";
434 case BWRITERSIO_EXP: return "exp";
435 case BWRITERSIO_LOG: return "log";
436 case BWRITERSIO_LIT: return "lit";
437 case BWRITERSIO_DST: return "dst";
438 case BWRITERSIO_LRP: return "lrp";
439 case BWRITERSIO_FRC: return "frc";
440 case BWRITERSIO_M4x4: return "m4x4";
441 case BWRITERSIO_M4x3: return "m4x3";
442 case BWRITERSIO_M3x4: return "m3x4";
443 case BWRITERSIO_M3x3: return "m3x3";
444 case BWRITERSIO_M3x2: return "m3x2";
445 case BWRITERSIO_CALL: return "call";
446 case BWRITERSIO_CALLNZ: return "callnz";
447 case BWRITERSIO_LOOP: return "loop";
448 case BWRITERSIO_RET: return "ret";
449 case BWRITERSIO_ENDLOOP: return "endloop";
450 case BWRITERSIO_LABEL: return "label";
451 case BWRITERSIO_DCL: return "dcl";
452 case BWRITERSIO_POW: return "pow";
453 case BWRITERSIO_CRS: return "crs";
454 case BWRITERSIO_SGN: return "sgn";
455 case BWRITERSIO_ABS: return "abs";
456 case BWRITERSIO_NRM: return "nrm";
457 case BWRITERSIO_SINCOS: return "sincos";
458 case BWRITERSIO_REP: return "rep";
459 case BWRITERSIO_ENDREP: return "endrep";
460 case BWRITERSIO_IF: return "if";
461 case BWRITERSIO_IFC: return "ifc";
462 case BWRITERSIO_ELSE: return "else";
463 case BWRITERSIO_ENDIF: return "endif";
464 case BWRITERSIO_BREAK: return "break";
465 case BWRITERSIO_BREAKC: return "breakc";
466 case BWRITERSIO_MOVA: return "mova";
467 case BWRITERSIO_DEFB: return "defb";
468 case BWRITERSIO_DEFI: return "defi";
469 case BWRITERSIO_TEXCOORD: return "texcoord";
470 case BWRITERSIO_TEXKILL: return "texkill";
471 case BWRITERSIO_TEX: return "tex";
472 case BWRITERSIO_TEXBEM: return "texbem";
473 case BWRITERSIO_TEXBEML: return "texbeml";
474 case BWRITERSIO_TEXREG2AR: return "texreg2ar";
475 case BWRITERSIO_TEXREG2GB: return "texreg2gb";
476 case BWRITERSIO_TEXM3x2PAD: return "texm3x2pad";
477 case BWRITERSIO_TEXM3x2TEX: return "texm3x2tex";
478 case BWRITERSIO_TEXM3x3PAD: return "texm3x3pad";
479 case BWRITERSIO_TEXM3x3TEX: return "texm3x3tex";
480 case BWRITERSIO_TEXM3x3SPEC: return "texm3x3vspec";
481 case BWRITERSIO_TEXM3x3VSPEC: return "texm3x3vspec";
482 case BWRITERSIO_EXPP: return "expp";
483 case BWRITERSIO_LOGP: return "logp";
484 case BWRITERSIO_CND: return "cnd";
485 case BWRITERSIO_DEF: return "def";
486 case BWRITERSIO_TEXREG2RGB: return "texreg2rgb";
487 case BWRITERSIO_TEXDP3TEX: return "texdp3tex";
488 case BWRITERSIO_TEXM3x2DEPTH: return "texm3x2depth";
489 case BWRITERSIO_TEXDP3: return "texdp3";
490 case BWRITERSIO_TEXM3x3: return "texm3x3";
491 case BWRITERSIO_TEXDEPTH: return "texdepth";
492 case BWRITERSIO_CMP: return "cmp";
493 case BWRITERSIO_BEM: return "bem";
494 case BWRITERSIO_DP2ADD: return "dp2add";
495 case BWRITERSIO_DSX: return "dsx";
496 case BWRITERSIO_DSY: return "dsy";
497 case BWRITERSIO_TEXLDD: return "texldd";
498 case BWRITERSIO_SETP: return "setp";
499 case BWRITERSIO_TEXLDL: return "texldl";
500 case BWRITERSIO_BREAKP: return "breakp";
501 case BWRITERSIO_PHASE: return "phase";
503 case BWRITERSIO_TEXLDP: return "texldp";
504 case BWRITERSIO_TEXLDB: return "texldb";
506 default: return "unknown";
510 void skip_dword_unknown(const char **ptr, unsigned int count)
512 unsigned int i;
513 DWORD d;
515 FIXME("Skipping %u unknown DWORDs:\n", count);
516 for (i = 0; i < count; ++i)
518 read_dword(ptr, &d);
519 FIXME("\t0x%08x\n", d);
523 static void write_dword_unknown(char **ptr, DWORD d)
525 FIXME("Writing unknown DWORD 0x%08x\n", d);
526 write_dword(ptr, d);
529 HRESULT dxbc_add_section(struct dxbc *dxbc, DWORD tag, const char *data, DWORD data_size)
531 TRACE("dxbc %p, tag %s, size %#x.\n", dxbc, debugstr_an((const char *)&tag, 4), data_size);
533 if (dxbc->count >= dxbc->size)
535 struct dxbc_section *new_sections;
536 DWORD new_size = dxbc->size << 1;
538 new_sections = HeapReAlloc(GetProcessHeap(), 0, dxbc->sections, new_size * sizeof(*dxbc->sections));
539 if (!new_sections)
541 ERR("Failed to allocate dxbc section memory\n");
542 return E_OUTOFMEMORY;
545 dxbc->sections = new_sections;
546 dxbc->size = new_size;
549 dxbc->sections[dxbc->count].tag = tag;
550 dxbc->sections[dxbc->count].data_size = data_size;
551 dxbc->sections[dxbc->count].data = data;
552 ++dxbc->count;
554 return S_OK;
557 HRESULT dxbc_init(struct dxbc *dxbc, unsigned int size)
559 TRACE("dxbc %p, size %u.\n", dxbc, size);
561 /* use a good starting value for the size if none specified */
562 if (!size) size = 2;
564 dxbc->sections = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*dxbc->sections));
565 if (!dxbc->sections)
567 ERR("Failed to allocate dxbc section memory\n");
568 return E_OUTOFMEMORY;
571 dxbc->size = size;
572 dxbc->count = 0;
574 return S_OK;
577 HRESULT dxbc_parse(const char *data, SIZE_T data_size, struct dxbc *dxbc)
579 const char *ptr = data;
580 HRESULT hr;
581 unsigned int i;
582 DWORD tag, total_size, chunk_count;
584 if (!data)
586 WARN("No data supplied.\n");
587 return E_FAIL;
590 read_dword(&ptr, &tag);
591 TRACE("tag: %s.\n", debugstr_an((const char *)&tag, 4));
593 if (tag != TAG_DXBC)
595 WARN("Wrong tag.\n");
596 return E_FAIL;
599 /* checksum? */
600 skip_dword_unknown(&ptr, 4);
602 skip_dword_unknown(&ptr, 1);
604 read_dword(&ptr, &total_size);
605 TRACE("total size: %#x\n", total_size);
607 if (data_size != total_size)
609 WARN("Wrong size supplied.\n");
610 return D3DERR_INVALIDCALL;
613 read_dword(&ptr, &chunk_count);
614 TRACE("chunk count: %#x\n", chunk_count);
616 hr = dxbc_init(dxbc, chunk_count);
617 if (FAILED(hr))
619 WARN("Failed to init dxbc\n");
620 return hr;
623 for (i = 0; i < chunk_count; ++i)
625 DWORD chunk_tag, chunk_size;
626 const char *chunk_ptr;
627 DWORD chunk_offset;
629 read_dword(&ptr, &chunk_offset);
630 TRACE("chunk %u at offset %#x\n", i, chunk_offset);
632 chunk_ptr = data + chunk_offset;
634 read_dword(&chunk_ptr, &chunk_tag);
635 read_dword(&chunk_ptr, &chunk_size);
637 hr = dxbc_add_section(dxbc, chunk_tag, chunk_ptr, chunk_size);
638 if (FAILED(hr))
640 WARN("Failed to add section to dxbc\n");
641 return hr;
645 return hr;
648 void dxbc_destroy(struct dxbc *dxbc)
650 TRACE("dxbc %p.\n", dxbc);
652 HeapFree(GetProcessHeap(), 0, dxbc->sections);
655 HRESULT dxbc_write_blob(struct dxbc *dxbc, ID3DBlob **blob)
657 DWORD size = 32, offset = size + 4 * dxbc->count;
658 ID3DBlob *object;
659 HRESULT hr;
660 char *ptr;
661 unsigned int i;
663 TRACE("dxbc %p, blob %p.\n", dxbc, blob);
665 for (i = 0; i < dxbc->count; ++i)
667 size += 12 + dxbc->sections[i].data_size;
670 hr = D3DCreateBlob(size, &object);
671 if (FAILED(hr))
673 WARN("Failed to create blob\n");
674 return hr;
677 ptr = ID3D10Blob_GetBufferPointer(object);
679 write_dword(&ptr, TAG_DXBC);
681 /* signature(?) */
682 write_dword_unknown(&ptr, 0);
683 write_dword_unknown(&ptr, 0);
684 write_dword_unknown(&ptr, 0);
685 write_dword_unknown(&ptr, 0);
687 /* seems to be always 1 */
688 write_dword_unknown(&ptr, 1);
690 /* DXBC size */
691 write_dword(&ptr, size);
693 /* chunk count */
694 write_dword(&ptr, dxbc->count);
696 /* write the chunk offsets */
697 for (i = 0; i < dxbc->count; ++i)
699 write_dword(&ptr, offset);
700 offset += 8 + dxbc->sections[i].data_size;
703 /* write the chunks */
704 for (i = 0; i < dxbc->count; ++i)
706 write_dword(&ptr, dxbc->sections[i].tag);
707 write_dword(&ptr, dxbc->sections[i].data_size);
708 memcpy(ptr, dxbc->sections[i].data, dxbc->sections[i].data_size);
709 ptr += dxbc->sections[i].data_size;
712 TRACE("Created ID3DBlob %p\n", object);
714 *blob = object;
716 return S_OK;
719 void compilation_message(struct compilation_messages *msg, const char *fmt, __ms_va_list args)
721 char* buffer;
722 int rc, size;
724 if (msg->capacity == 0)
726 msg->string = d3dcompiler_alloc(MESSAGEBUFFER_INITIAL_SIZE);
727 if (msg->string == NULL)
729 ERR("Error allocating memory for parser messages\n");
730 return;
732 msg->capacity = MESSAGEBUFFER_INITIAL_SIZE;
735 while (1)
737 rc = vsnprintf(msg->string + msg->size,
738 msg->capacity - msg->size, fmt, args);
740 if (rc < 0 || rc >= msg->capacity - msg->size)
742 size = msg->capacity * 2;
743 buffer = d3dcompiler_realloc(msg->string, size);
744 if (buffer == NULL)
746 ERR("Error reallocating memory for parser messages\n");
747 return;
749 msg->string = buffer;
750 msg->capacity = size;
752 else
754 TRACE("%s", msg->string + msg->size);
755 msg->size += rc;
756 return;
761 #if D3D_COMPILER_VERSION
762 BOOL add_declaration(struct hlsl_scope *scope, struct hlsl_ir_var *decl, BOOL local_var)
764 struct hlsl_ir_var *var;
766 LIST_FOR_EACH_ENTRY(var, &scope->vars, struct hlsl_ir_var, scope_entry)
768 if (!strcmp(decl->name, var->name))
769 return FALSE;
771 if (local_var && scope->upper->upper == hlsl_ctx.globals)
773 /* Check whether the variable redefines a function parameter. */
774 LIST_FOR_EACH_ENTRY(var, &scope->upper->vars, struct hlsl_ir_var, scope_entry)
776 if (!strcmp(decl->name, var->name))
777 return FALSE;
781 list_add_tail(&scope->vars, &decl->scope_entry);
782 return TRUE;
785 struct hlsl_ir_var *get_variable(struct hlsl_scope *scope, const char *name)
787 struct hlsl_ir_var *var;
789 LIST_FOR_EACH_ENTRY(var, &scope->vars, struct hlsl_ir_var, scope_entry)
791 if (!strcmp(name, var->name))
792 return var;
794 if (!scope->upper)
795 return NULL;
796 return get_variable(scope->upper, name);
799 void free_declaration(struct hlsl_ir_var *decl)
801 d3dcompiler_free((void *)decl->name);
802 d3dcompiler_free((void *)decl->semantic);
803 d3dcompiler_free((void *)decl->reg_reservation);
804 d3dcompiler_free(decl);
807 struct hlsl_type *new_hlsl_type(const char *name, enum hlsl_type_class type_class,
808 enum hlsl_base_type base_type, unsigned dimx, unsigned dimy)
810 struct hlsl_type *type;
812 type = d3dcompiler_alloc(sizeof(*type));
813 if (!type)
815 ERR("Out of memory\n");
816 return NULL;
818 type->name = name;
819 type->type = type_class;
820 type->base_type = base_type;
821 type->dimx = dimx;
822 type->dimy = dimy;
824 list_add_tail(&hlsl_ctx.types, &type->entry);
826 return type;
829 struct hlsl_type *new_array_type(struct hlsl_type *basic_type, unsigned int array_size)
831 struct hlsl_type *type = new_hlsl_type(NULL, HLSL_CLASS_ARRAY, HLSL_TYPE_FLOAT, 1, 1);
833 if (!type)
834 return NULL;
836 type->modifiers = basic_type->modifiers;
837 type->e.array.elements_count = array_size;
838 type->e.array.type = basic_type;
839 return type;
842 struct hlsl_type *get_type(struct hlsl_scope *scope, const char *name, BOOL recursive)
844 struct wine_rb_entry *entry = wine_rb_get(&scope->types, name);
845 if (entry)
846 return WINE_RB_ENTRY_VALUE(entry, struct hlsl_type, scope_entry);
848 if (recursive && scope->upper)
849 return get_type(scope->upper, name, recursive);
850 return NULL;
853 BOOL find_function(const char *name)
855 return wine_rb_get(&hlsl_ctx.functions, name) != NULL;
858 unsigned int components_count_type(struct hlsl_type *type)
860 unsigned int count = 0;
861 struct hlsl_struct_field *field;
863 if (type->type <= HLSL_CLASS_LAST_NUMERIC)
865 return type->dimx * type->dimy;
867 if (type->type == HLSL_CLASS_ARRAY)
869 return components_count_type(type->e.array.type) * type->e.array.elements_count;
871 if (type->type != HLSL_CLASS_STRUCT)
873 ERR("Unexpected data type %s.\n", debug_hlsl_type(type));
874 return 0;
877 LIST_FOR_EACH_ENTRY(field, type->e.elements, struct hlsl_struct_field, entry)
879 count += components_count_type(field->type);
881 return count;
884 BOOL compare_hlsl_types(const struct hlsl_type *t1, const struct hlsl_type *t2)
886 if (t1 == t2)
887 return TRUE;
889 if (t1->type != t2->type)
890 return FALSE;
891 if (t1->base_type != t2->base_type)
892 return FALSE;
893 if (t1->base_type == HLSL_TYPE_SAMPLER && t1->sampler_dim != t2->sampler_dim)
894 return FALSE;
895 if ((t1->modifiers & HLSL_MODIFIERS_COMPARISON_MASK)
896 != (t2->modifiers & HLSL_MODIFIERS_COMPARISON_MASK))
897 return FALSE;
898 if (t1->dimx != t2->dimx)
899 return FALSE;
900 if (t1->dimy != t2->dimy)
901 return FALSE;
902 if (t1->type == HLSL_CLASS_STRUCT)
904 struct list *t1cur, *t2cur;
905 struct hlsl_struct_field *t1field, *t2field;
907 t1cur = list_head(t1->e.elements);
908 t2cur = list_head(t2->e.elements);
909 while (t1cur && t2cur)
911 t1field = LIST_ENTRY(t1cur, struct hlsl_struct_field, entry);
912 t2field = LIST_ENTRY(t2cur, struct hlsl_struct_field, entry);
913 if (!compare_hlsl_types(t1field->type, t2field->type))
914 return FALSE;
915 if (strcmp(t1field->name, t2field->name))
916 return FALSE;
917 t1cur = list_next(t1->e.elements, t1cur);
918 t2cur = list_next(t2->e.elements, t2cur);
920 if (t1cur != t2cur)
921 return FALSE;
923 if (t1->type == HLSL_CLASS_ARRAY)
924 return t1->e.array.elements_count == t2->e.array.elements_count
925 && compare_hlsl_types(t1->e.array.type, t2->e.array.type);
927 return TRUE;
930 struct hlsl_type *clone_hlsl_type(struct hlsl_type *old)
932 struct hlsl_type *type;
933 struct hlsl_struct_field *old_field, *field;
935 type = d3dcompiler_alloc(sizeof(*type));
936 if (!type)
938 ERR("Out of memory\n");
939 return NULL;
941 if (old->name)
943 type->name = d3dcompiler_strdup(old->name);
944 if (!type->name)
946 d3dcompiler_free(type);
947 return NULL;
950 type->type = old->type;
951 type->base_type = old->base_type;
952 type->dimx = old->dimx;
953 type->dimy = old->dimy;
954 type->modifiers = old->modifiers;
955 type->sampler_dim = old->sampler_dim;
956 switch (old->type)
958 case HLSL_CLASS_ARRAY:
959 type->e.array.type = old->e.array.type;
960 type->e.array.elements_count = old->e.array.elements_count;
961 break;
962 case HLSL_CLASS_STRUCT:
963 type->e.elements = d3dcompiler_alloc(sizeof(*type->e.elements));
964 if (!type->e.elements)
966 d3dcompiler_free((void *)type->name);
967 d3dcompiler_free(type);
968 return NULL;
970 list_init(type->e.elements);
971 LIST_FOR_EACH_ENTRY(old_field, old->e.elements, struct hlsl_struct_field, entry)
973 field = d3dcompiler_alloc(sizeof(*field));
974 if (!field)
976 LIST_FOR_EACH_ENTRY_SAFE(field, old_field, type->e.elements, struct hlsl_struct_field, entry)
978 d3dcompiler_free((void *)field->semantic);
979 d3dcompiler_free((void *)field->name);
980 d3dcompiler_free(field);
982 d3dcompiler_free(type->e.elements);
983 d3dcompiler_free((void *)type->name);
984 d3dcompiler_free(type);
985 return NULL;
987 field->type = clone_hlsl_type(old_field->type);
988 field->name = d3dcompiler_strdup(old_field->name);
989 if (old_field->semantic)
990 field->semantic = d3dcompiler_strdup(old_field->semantic);
991 field->modifiers = old_field->modifiers;
992 list_add_tail(type->e.elements, &field->entry);
994 break;
995 default:
996 break;
999 list_add_tail(&hlsl_ctx.types, &type->entry);
1000 return type;
1003 static BOOL convertible_data_type(struct hlsl_type *type)
1005 return type->type != HLSL_CLASS_OBJECT;
1008 BOOL compatible_data_types(struct hlsl_type *t1, struct hlsl_type *t2)
1010 if (!convertible_data_type(t1) || !convertible_data_type(t2))
1011 return FALSE;
1013 if (t1->type <= HLSL_CLASS_LAST_NUMERIC)
1015 /* Scalar vars can be cast to pretty much everything */
1016 if (t1->dimx == 1 && t1->dimy == 1)
1017 return TRUE;
1019 if (t1->type == HLSL_CLASS_VECTOR && t2->type == HLSL_CLASS_VECTOR)
1020 return t1->dimx >= t2->dimx;
1023 /* The other way around is true too i.e. whatever to scalar */
1024 if (t2->type <= HLSL_CLASS_LAST_NUMERIC && t2->dimx == 1 && t2->dimy == 1)
1025 return TRUE;
1027 if (t1->type == HLSL_CLASS_ARRAY)
1029 if (compare_hlsl_types(t1->e.array.type, t2))
1030 /* e.g. float4[3] to float4 is allowed */
1031 return TRUE;
1033 if (t2->type == HLSL_CLASS_ARRAY || t2->type == HLSL_CLASS_STRUCT)
1034 return components_count_type(t1) >= components_count_type(t2);
1035 else
1036 return components_count_type(t1) == components_count_type(t2);
1039 if (t1->type == HLSL_CLASS_STRUCT)
1040 return components_count_type(t1) >= components_count_type(t2);
1042 if (t2->type == HLSL_CLASS_ARRAY || t2->type == HLSL_CLASS_STRUCT)
1043 return components_count_type(t1) == components_count_type(t2);
1045 if (t1->type == HLSL_CLASS_MATRIX || t2->type == HLSL_CLASS_MATRIX)
1047 if (t1->type == HLSL_CLASS_MATRIX && t2->type == HLSL_CLASS_MATRIX && t1->dimx >= t2->dimx && t1->dimy >= t2->dimy)
1048 return TRUE;
1050 /* Matrix-vector conversion is apparently allowed if they have the same components count */
1051 if ((t1->type == HLSL_CLASS_VECTOR || t2->type == HLSL_CLASS_VECTOR)
1052 && components_count_type(t1) == components_count_type(t2))
1053 return TRUE;
1054 return FALSE;
1057 if (components_count_type(t1) >= components_count_type(t2))
1058 return TRUE;
1059 return FALSE;
1062 static BOOL implicit_compatible_data_types(struct hlsl_type *t1, struct hlsl_type *t2)
1064 if (!convertible_data_type(t1) || !convertible_data_type(t2))
1065 return FALSE;
1067 if (t1->type <= HLSL_CLASS_LAST_NUMERIC)
1069 /* Scalar vars can be converted to any other numeric data type */
1070 if (t1->dimx == 1 && t1->dimy == 1 && t2->type <= HLSL_CLASS_LAST_NUMERIC)
1071 return TRUE;
1072 /* The other way around is true too */
1073 if (t2->dimx == 1 && t2->dimy == 1 && t2->type <= HLSL_CLASS_LAST_NUMERIC)
1074 return TRUE;
1077 if (t1->type == HLSL_CLASS_ARRAY && t2->type == HLSL_CLASS_ARRAY)
1079 return components_count_type(t1) == components_count_type(t2);
1082 if ((t1->type == HLSL_CLASS_ARRAY && t2->type <= HLSL_CLASS_LAST_NUMERIC)
1083 || (t1->type <= HLSL_CLASS_LAST_NUMERIC && t2->type == HLSL_CLASS_ARRAY))
1085 /* e.g. float4[3] to float4 is allowed */
1086 if (t1->type == HLSL_CLASS_ARRAY && compare_hlsl_types(t1->e.array.type, t2))
1087 return TRUE;
1088 if (components_count_type(t1) == components_count_type(t2))
1089 return TRUE;
1090 return FALSE;
1093 if (t1->type <= HLSL_CLASS_VECTOR && t2->type <= HLSL_CLASS_VECTOR)
1095 if (t1->dimx >= t2->dimx)
1096 return TRUE;
1097 return FALSE;
1100 if (t1->type == HLSL_CLASS_MATRIX || t2->type == HLSL_CLASS_MATRIX)
1102 if (t1->type == HLSL_CLASS_MATRIX && t2->type == HLSL_CLASS_MATRIX
1103 && t1->dimx >= t2->dimx && t1->dimy >= t2->dimy)
1104 return TRUE;
1106 /* Matrix-vector conversion is apparently allowed if they have the same components count */
1107 if ((t1->type == HLSL_CLASS_VECTOR || t2->type == HLSL_CLASS_VECTOR)
1108 && components_count_type(t1) == components_count_type(t2))
1109 return TRUE;
1110 return FALSE;
1113 if (t1->type == HLSL_CLASS_STRUCT && t2->type == HLSL_CLASS_STRUCT)
1114 return compare_hlsl_types(t1, t2);
1116 return FALSE;
1119 static BOOL expr_compatible_data_types(struct hlsl_type *t1, struct hlsl_type *t2)
1121 if (t1->base_type > HLSL_TYPE_LAST_SCALAR || t2->base_type > HLSL_TYPE_LAST_SCALAR)
1122 return FALSE;
1124 /* Scalar vars can be converted to pretty much everything */
1125 if ((t1->dimx == 1 && t1->dimy == 1) || (t2->dimx == 1 && t2->dimy == 1))
1126 return TRUE;
1128 if (t1->type == HLSL_CLASS_VECTOR && t2->type == HLSL_CLASS_VECTOR)
1129 return TRUE;
1131 if (t1->type == HLSL_CLASS_MATRIX || t2->type == HLSL_CLASS_MATRIX)
1133 /* Matrix-vector conversion is apparently allowed if either they have the same components
1134 count or the matrix is nx1 or 1xn */
1135 if (t1->type == HLSL_CLASS_VECTOR || t2->type == HLSL_CLASS_VECTOR)
1137 if (components_count_type(t1) == components_count_type(t2))
1138 return TRUE;
1140 return (t1->type == HLSL_CLASS_MATRIX && (t1->dimx == 1 || t1->dimy == 1))
1141 || (t2->type == HLSL_CLASS_MATRIX && (t2->dimx == 1 || t2->dimy == 1));
1144 /* Both matrices */
1145 if ((t1->dimx >= t2->dimx && t1->dimy >= t2->dimy)
1146 || (t1->dimx <= t2->dimx && t1->dimy <= t2->dimy))
1147 return TRUE;
1150 return FALSE;
1153 static enum hlsl_base_type expr_common_base_type(enum hlsl_base_type t1, enum hlsl_base_type t2)
1155 static const enum hlsl_base_type types[] =
1157 HLSL_TYPE_BOOL,
1158 HLSL_TYPE_INT,
1159 HLSL_TYPE_UINT,
1160 HLSL_TYPE_HALF,
1161 HLSL_TYPE_FLOAT,
1162 HLSL_TYPE_DOUBLE,
1164 int t1_idx = -1, t2_idx = -1, i;
1166 for (i = 0; i < ARRAY_SIZE(types); ++i)
1168 /* Always convert away from HLSL_TYPE_HALF */
1169 if (t1 == types[i])
1170 t1_idx = t1 == HLSL_TYPE_HALF ? i + 1 : i;
1171 if (t2 == types[i])
1172 t2_idx = t2 == HLSL_TYPE_HALF ? i + 1 : i;
1174 if (t1_idx != -1 && t2_idx != -1)
1175 break;
1177 if (t1_idx == -1 || t2_idx == -1)
1179 FIXME("Unexpected base type.\n");
1180 return HLSL_TYPE_FLOAT;
1182 return t1_idx >= t2_idx ? t1 : t2;
1185 static struct hlsl_type *expr_common_type(struct hlsl_type *t1, struct hlsl_type *t2,
1186 struct source_location *loc)
1188 enum hlsl_type_class type;
1189 enum hlsl_base_type base;
1190 unsigned int dimx, dimy;
1192 if (t1->type > HLSL_CLASS_LAST_NUMERIC || t2->type > HLSL_CLASS_LAST_NUMERIC)
1194 hlsl_report_message(*loc, HLSL_LEVEL_ERROR, "non scalar/vector/matrix data type in expression");
1195 return NULL;
1198 if (compare_hlsl_types(t1, t2))
1199 return t1;
1201 if (!expr_compatible_data_types(t1, t2))
1203 hlsl_report_message(*loc, HLSL_LEVEL_ERROR, "expression data types are incompatible");
1204 return NULL;
1207 if (t1->base_type == t2->base_type)
1208 base = t1->base_type;
1209 else
1210 base = expr_common_base_type(t1->base_type, t2->base_type);
1212 if (t1->dimx == 1 && t1->dimy == 1)
1214 type = t2->type;
1215 dimx = t2->dimx;
1216 dimy = t2->dimy;
1218 else if (t2->dimx == 1 && t2->dimy == 1)
1220 type = t1->type;
1221 dimx = t1->dimx;
1222 dimy = t1->dimy;
1224 else if (t1->type == HLSL_CLASS_MATRIX && t2->type == HLSL_CLASS_MATRIX)
1226 type = HLSL_CLASS_MATRIX;
1227 dimx = min(t1->dimx, t2->dimx);
1228 dimy = min(t1->dimy, t2->dimy);
1230 else
1232 /* Two vectors or a vector and a matrix (matrix must be 1xn or nx1) */
1233 unsigned int max_dim_1, max_dim_2;
1235 max_dim_1 = max(t1->dimx, t1->dimy);
1236 max_dim_2 = max(t2->dimx, t2->dimy);
1237 if (t1->dimx * t1->dimy == t2->dimx * t2->dimy)
1239 type = HLSL_CLASS_VECTOR;
1240 dimx = max(t1->dimx, t2->dimx);
1241 dimy = 1;
1243 else if (max_dim_1 <= max_dim_2)
1245 type = t1->type;
1246 if (type == HLSL_CLASS_VECTOR)
1248 dimx = max_dim_1;
1249 dimy = 1;
1251 else
1253 dimx = t1->dimx;
1254 dimy = t1->dimy;
1257 else
1259 type = t2->type;
1260 if (type == HLSL_CLASS_VECTOR)
1262 dimx = max_dim_2;
1263 dimy = 1;
1265 else
1267 dimx = t2->dimx;
1268 dimy = t2->dimy;
1273 return new_hlsl_type(NULL, type, base, dimx, dimy);
1276 struct hlsl_ir_node *implicit_conversion(struct hlsl_ir_node *node, struct hlsl_type *dst_type,
1277 struct source_location *loc)
1279 struct hlsl_type *src_type = node->data_type;
1280 struct hlsl_ir_expr *cast;
1282 if (compare_hlsl_types(src_type, dst_type))
1283 return node;
1285 if (!implicit_compatible_data_types(src_type, dst_type))
1287 hlsl_report_message(*loc, HLSL_LEVEL_ERROR, "can't implicitly convert %s to %s",
1288 debug_hlsl_type(src_type), debug_hlsl_type(dst_type));
1289 return NULL;
1292 if (dst_type->dimx * dst_type->dimy < src_type->dimx * src_type->dimy)
1293 hlsl_report_message(*loc, HLSL_LEVEL_WARNING, "implicit truncation of vector type");
1295 TRACE("Implicit conversion from %s to %s.\n", debug_hlsl_type(src_type), debug_hlsl_type(dst_type));
1297 if (!(cast = new_cast(node, dst_type, loc)))
1298 return NULL;
1299 list_add_after(&node->entry, &cast->node.entry);
1300 return &cast->node;
1303 struct hlsl_ir_expr *new_expr(enum hlsl_ir_expr_op op, struct hlsl_ir_node **operands,
1304 struct source_location *loc)
1306 struct hlsl_ir_expr *expr = d3dcompiler_alloc(sizeof(*expr));
1307 struct hlsl_type *type;
1308 unsigned int i;
1310 if (!expr)
1312 ERR("Out of memory\n");
1313 return NULL;
1315 expr->node.type = HLSL_IR_EXPR;
1316 expr->node.loc = *loc;
1317 type = operands[0]->data_type;
1318 for (i = 1; i <= 2; ++i)
1320 if (!operands[i])
1321 break;
1322 type = expr_common_type(type, operands[i]->data_type, loc);
1323 if (!type)
1325 d3dcompiler_free(expr);
1326 return NULL;
1329 for (i = 0; i <= 2; ++i)
1331 struct hlsl_ir_expr *cast;
1333 if (!operands[i])
1334 break;
1335 if (compare_hlsl_types(operands[i]->data_type, type))
1336 continue;
1337 TRACE("Implicitly converting %s into %s in an expression\n", debug_hlsl_type(operands[i]->data_type), debug_hlsl_type(type));
1338 if (operands[i]->data_type->dimx * operands[i]->data_type->dimy != 1
1339 && operands[i]->data_type->dimx * operands[i]->data_type->dimy != type->dimx * type->dimy)
1341 hlsl_report_message(operands[i]->loc, HLSL_LEVEL_WARNING, "implicit truncation of vector/matrix type");
1344 if (!(cast = new_cast(operands[i], type, &operands[i]->loc)))
1346 d3dcompiler_free(expr);
1347 return NULL;
1349 list_add_after(&operands[i]->entry, &cast->node.entry);
1350 operands[i] = &cast->node;
1352 expr->node.data_type = type;
1353 expr->op = op;
1354 expr->operands[0] = operands[0];
1355 expr->operands[1] = operands[1];
1356 expr->operands[2] = operands[2];
1358 return expr;
1361 struct hlsl_ir_expr *new_cast(struct hlsl_ir_node *node, struct hlsl_type *type,
1362 struct source_location *loc)
1364 struct hlsl_ir_node *cast;
1366 cast = new_unary_expr(HLSL_IR_UNOP_CAST, node, *loc);
1367 if (cast)
1368 cast->data_type = type;
1369 return expr_from_node(cast);
1372 struct hlsl_ir_deref *new_var_deref(struct hlsl_ir_var *var)
1374 struct hlsl_ir_deref *deref = d3dcompiler_alloc(sizeof(*deref));
1376 if (!deref)
1378 ERR("Out of memory.\n");
1379 return NULL;
1381 deref->node.type = HLSL_IR_DEREF;
1382 deref->node.data_type = var->data_type;
1383 deref->type = HLSL_IR_DEREF_VAR;
1384 deref->v.var = var;
1385 return deref;
1388 struct hlsl_ir_deref *new_record_deref(struct hlsl_ir_node *record, struct hlsl_struct_field *field)
1390 struct hlsl_ir_deref *deref = d3dcompiler_alloc(sizeof(*deref));
1392 if (!deref)
1394 ERR("Out of memory.\n");
1395 return NULL;
1397 deref->node.type = HLSL_IR_DEREF;
1398 deref->node.data_type = field->type;
1399 deref->type = HLSL_IR_DEREF_RECORD;
1400 deref->v.record.record = record;
1401 deref->v.record.field = field;
1402 return deref;
1405 static enum hlsl_ir_expr_op op_from_assignment(enum parse_assign_op op)
1407 static const enum hlsl_ir_expr_op ops[] =
1410 HLSL_IR_BINOP_ADD,
1411 HLSL_IR_BINOP_SUB,
1412 HLSL_IR_BINOP_MUL,
1413 HLSL_IR_BINOP_DIV,
1414 HLSL_IR_BINOP_MOD,
1415 HLSL_IR_BINOP_LSHIFT,
1416 HLSL_IR_BINOP_RSHIFT,
1417 HLSL_IR_BINOP_BIT_AND,
1418 HLSL_IR_BINOP_BIT_OR,
1419 HLSL_IR_BINOP_BIT_XOR,
1422 return ops[op];
1425 static unsigned int invert_swizzle(unsigned int *swizzle, unsigned int writemask)
1427 unsigned int i, j, bit = 0, inverted = 0, components, new_writemask = 0, new_swizzle = 0;
1429 /* Apply the writemask to the swizzle to get a new writemask and swizzle. */
1430 for (i = 0; i < 4; ++i)
1432 if (writemask & (1 << i))
1434 unsigned int s = (*swizzle >> (i * 2)) & 3;
1435 new_swizzle |= s << (bit++ * 2);
1436 if (new_writemask & (1 << s))
1437 return 0;
1438 new_writemask |= 1 << s;
1441 components = bit;
1443 /* Invert the swizzle. */
1444 bit = 0;
1445 for (i = 0; i < 4; ++i)
1447 for (j = 0; j < components; ++j)
1449 unsigned int s = (new_swizzle >> (j * 2)) & 3;
1450 if (s == i)
1451 inverted |= j << (bit++ * 2);
1455 *swizzle = inverted;
1456 return new_writemask;
1459 struct hlsl_ir_node *make_assignment(struct hlsl_ir_node *lhs, enum parse_assign_op assign_op,
1460 struct hlsl_ir_node *rhs)
1462 struct hlsl_ir_assignment *assign = d3dcompiler_alloc(sizeof(*assign));
1463 DWORD writemask = (1 << lhs->data_type->dimx) - 1;
1464 struct hlsl_type *type;
1466 if (!assign)
1468 ERR("Out of memory\n");
1469 return NULL;
1472 while (lhs->type != HLSL_IR_DEREF)
1474 struct hlsl_ir_node *lhs_inner;
1476 if (lhs->type == HLSL_IR_EXPR && expr_from_node(lhs)->op == HLSL_IR_UNOP_CAST)
1478 FIXME("Cast on the lhs.\n");
1479 d3dcompiler_free(assign);
1480 return NULL;
1482 else if (lhs->type == HLSL_IR_SWIZZLE)
1484 struct hlsl_ir_swizzle *swizzle = swizzle_from_node(lhs);
1486 if (lhs->data_type->type == HLSL_CLASS_MATRIX)
1487 FIXME("Assignments with writemasks and matrices on lhs are not supported yet.\n");
1489 lhs_inner = swizzle->val;
1490 list_remove(&lhs->entry);
1492 list_add_after(&rhs->entry, &lhs->entry);
1493 swizzle->val = rhs;
1494 if (!(writemask = invert_swizzle(&swizzle->swizzle, writemask)))
1496 hlsl_report_message(lhs->loc, HLSL_LEVEL_ERROR, "invalid writemask");
1497 d3dcompiler_free(assign);
1498 return NULL;
1500 rhs = &swizzle->node;
1502 else
1504 hlsl_report_message(lhs->loc, HLSL_LEVEL_ERROR, "invalid lvalue");
1505 d3dcompiler_free(assign);
1506 return NULL;
1509 lhs = lhs_inner;
1512 TRACE("Creating proper assignment expression.\n");
1513 if (writemask == BWRITERSP_WRITEMASK_ALL)
1514 type = lhs->data_type;
1515 else
1517 unsigned int dimx = 0;
1518 DWORD bitmask;
1519 enum hlsl_type_class type_class;
1521 if (lhs->data_type->type > HLSL_CLASS_LAST_NUMERIC)
1523 hlsl_report_message(lhs->loc, HLSL_LEVEL_ERROR,
1524 "writemask on a non scalar/vector/matrix type");
1525 d3dcompiler_free(assign);
1526 return NULL;
1528 bitmask = writemask & ((1 << lhs->data_type->dimx) - 1);
1529 while (bitmask)
1531 if (bitmask & 1)
1532 dimx++;
1533 bitmask >>= 1;
1535 if (lhs->data_type->type == HLSL_CLASS_MATRIX)
1536 FIXME("Assignments with writemasks and matrices on lhs are not supported yet.\n");
1537 if (dimx == 1)
1538 type_class = HLSL_CLASS_SCALAR;
1539 else
1540 type_class = lhs->data_type->type;
1541 type = new_hlsl_type(NULL, type_class, lhs->data_type->base_type, dimx, 1);
1543 assign->node.type = HLSL_IR_ASSIGNMENT;
1544 assign->node.loc = lhs->loc;
1545 assign->node.data_type = type;
1546 assign->writemask = writemask;
1548 rhs = implicit_conversion(rhs, type, &rhs->loc);
1550 assign->lhs = lhs;
1551 if (assign_op != ASSIGN_OP_ASSIGN)
1553 enum hlsl_ir_expr_op op = op_from_assignment(assign_op);
1554 struct hlsl_ir_node *expr;
1556 if (deref_from_node(lhs)->type != HLSL_IR_DEREF_VAR)
1558 FIXME("LHS expression not supported in compound assignments yet.\n");
1559 assign->rhs = rhs;
1561 else
1563 TRACE("Adding an expression for the compound assignment.\n");
1564 expr = new_binary_expr(op, lhs, rhs, lhs->loc);
1565 list_add_after(&rhs->entry, &expr->entry);
1566 assign->rhs = expr;
1569 else
1570 assign->rhs = rhs;
1572 return &assign->node;
1575 static int compare_hlsl_types_rb(const void *key, const struct wine_rb_entry *entry)
1577 const char *name = key;
1578 const struct hlsl_type *type = WINE_RB_ENTRY_VALUE(entry, const struct hlsl_type, scope_entry);
1580 if (name == type->name)
1581 return 0;
1583 if (!name || !type->name)
1585 ERR("hlsl_type without a name in a scope?\n");
1586 return -1;
1588 return strcmp(name, type->name);
1591 void push_scope(struct hlsl_parse_ctx *ctx)
1593 struct hlsl_scope *new_scope = d3dcompiler_alloc(sizeof(*new_scope));
1595 if (!new_scope)
1597 ERR("Out of memory!\n");
1598 return;
1600 TRACE("Pushing a new scope\n");
1601 list_init(&new_scope->vars);
1602 wine_rb_init(&new_scope->types, compare_hlsl_types_rb);
1603 new_scope->upper = ctx->cur_scope;
1604 ctx->cur_scope = new_scope;
1605 list_add_tail(&ctx->scopes, &new_scope->entry);
1608 BOOL pop_scope(struct hlsl_parse_ctx *ctx)
1610 struct hlsl_scope *prev_scope = ctx->cur_scope->upper;
1611 if (!prev_scope)
1612 return FALSE;
1614 TRACE("Popping current scope\n");
1615 ctx->cur_scope = prev_scope;
1616 return TRUE;
1619 struct hlsl_ir_function_decl *new_func_decl(struct hlsl_type *return_type, struct list *parameters)
1621 struct hlsl_ir_function_decl *decl;
1623 decl = d3dcompiler_alloc(sizeof(*decl));
1624 if (!decl)
1626 ERR("Out of memory.\n");
1627 return NULL;
1629 decl->return_type = return_type;
1630 decl->parameters = parameters;
1632 return decl;
1635 static int compare_param_hlsl_types(const struct hlsl_type *t1, const struct hlsl_type *t2)
1637 if (t1->type != t2->type)
1639 if (!((t1->type == HLSL_CLASS_SCALAR && t2->type == HLSL_CLASS_VECTOR)
1640 || (t1->type == HLSL_CLASS_VECTOR && t2->type == HLSL_CLASS_SCALAR)))
1641 return t1->type - t2->type;
1643 if (t1->base_type != t2->base_type)
1644 return t1->base_type - t2->base_type;
1645 if (t1->base_type == HLSL_TYPE_SAMPLER && t1->sampler_dim != t2->sampler_dim)
1646 return t1->sampler_dim - t2->sampler_dim;
1647 if (t1->dimx != t2->dimx)
1648 return t1->dimx - t2->dimx;
1649 if (t1->dimy != t2->dimy)
1650 return t1->dimx - t2->dimx;
1651 if (t1->type == HLSL_CLASS_STRUCT)
1653 struct list *t1cur, *t2cur;
1654 struct hlsl_struct_field *t1field, *t2field;
1655 int r;
1657 t1cur = list_head(t1->e.elements);
1658 t2cur = list_head(t2->e.elements);
1659 while (t1cur && t2cur)
1661 t1field = LIST_ENTRY(t1cur, struct hlsl_struct_field, entry);
1662 t2field = LIST_ENTRY(t2cur, struct hlsl_struct_field, entry);
1663 if ((r = compare_param_hlsl_types(t1field->type, t2field->type)))
1664 return r;
1665 if ((r = strcmp(t1field->name, t2field->name)))
1666 return r;
1667 t1cur = list_next(t1->e.elements, t1cur);
1668 t2cur = list_next(t2->e.elements, t2cur);
1670 if (t1cur != t2cur)
1671 return t1cur ? 1 : -1;
1672 return 0;
1674 if (t1->type == HLSL_CLASS_ARRAY)
1676 if (t1->e.array.elements_count != t2->e.array.elements_count)
1677 return t1->e.array.elements_count - t2->e.array.elements_count;
1678 return compare_param_hlsl_types(t1->e.array.type, t2->e.array.type);
1681 return 0;
1684 static int compare_function_decl_rb(const void *key, const struct wine_rb_entry *entry)
1686 const struct list *params = key;
1687 const struct hlsl_ir_function_decl *decl = WINE_RB_ENTRY_VALUE(entry, const struct hlsl_ir_function_decl, entry);
1688 int params_count = params ? list_count(params) : 0;
1689 int decl_params_count = decl->parameters ? list_count(decl->parameters) : 0;
1690 int r;
1691 struct list *p1cur, *p2cur;
1693 if (params_count != decl_params_count)
1694 return params_count - decl_params_count;
1696 p1cur = params ? list_head(params) : NULL;
1697 p2cur = decl->parameters ? list_head(decl->parameters) : NULL;
1698 while (p1cur && p2cur)
1700 struct hlsl_ir_var *p1, *p2;
1701 p1 = LIST_ENTRY(p1cur, struct hlsl_ir_var, param_entry);
1702 p2 = LIST_ENTRY(p2cur, struct hlsl_ir_var, param_entry);
1703 if ((r = compare_param_hlsl_types(p1->data_type, p2->data_type)))
1704 return r;
1705 p1cur = list_next(params, p1cur);
1706 p2cur = list_next(decl->parameters, p2cur);
1708 return 0;
1711 static int compare_function_rb(const void *key, const struct wine_rb_entry *entry)
1713 const char *name = key;
1714 const struct hlsl_ir_function *func = WINE_RB_ENTRY_VALUE(entry, const struct hlsl_ir_function,entry);
1716 return strcmp(name, func->name);
1719 void init_functions_tree(struct wine_rb_tree *funcs)
1721 wine_rb_init(&hlsl_ctx.functions, compare_function_rb);
1724 const char *debug_base_type(const struct hlsl_type *type)
1726 const char *name = "(unknown)";
1728 switch (type->base_type)
1730 case HLSL_TYPE_FLOAT: name = "float"; break;
1731 case HLSL_TYPE_HALF: name = "half"; break;
1732 case HLSL_TYPE_DOUBLE: name = "double"; break;
1733 case HLSL_TYPE_INT: name = "int"; break;
1734 case HLSL_TYPE_UINT: name = "uint"; break;
1735 case HLSL_TYPE_BOOL: name = "bool"; break;
1736 case HLSL_TYPE_SAMPLER:
1737 switch (type->sampler_dim)
1739 case HLSL_SAMPLER_DIM_GENERIC: name = "sampler"; break;
1740 case HLSL_SAMPLER_DIM_1D: name = "sampler1D"; break;
1741 case HLSL_SAMPLER_DIM_2D: name = "sampler2D"; break;
1742 case HLSL_SAMPLER_DIM_3D: name = "sampler3D"; break;
1743 case HLSL_SAMPLER_DIM_CUBE: name = "samplerCUBE"; break;
1745 break;
1746 default:
1747 FIXME("Unhandled case %u\n", type->base_type);
1749 return name;
1752 const char *debug_hlsl_type(const struct hlsl_type *type)
1754 const char *name;
1756 if (type->name)
1757 return debugstr_a(type->name);
1759 if (type->type == HLSL_CLASS_STRUCT)
1760 return "<anonymous struct>";
1762 if (type->type == HLSL_CLASS_ARRAY)
1764 name = debug_base_type(type->e.array.type);
1765 return wine_dbg_sprintf("%s[%u]", name, type->e.array.elements_count);
1768 name = debug_base_type(type);
1770 if (type->type == HLSL_CLASS_SCALAR)
1771 return wine_dbg_sprintf("%s", name);
1772 if (type->type == HLSL_CLASS_VECTOR)
1773 return wine_dbg_sprintf("%s%u", name, type->dimx);
1774 if (type->type == HLSL_CLASS_MATRIX)
1775 return wine_dbg_sprintf("%s%ux%u", name, type->dimx, type->dimy);
1776 return "unexpected_type";
1779 const char *debug_modifiers(DWORD modifiers)
1781 char string[110];
1783 string[0] = 0;
1784 if (modifiers & HLSL_STORAGE_EXTERN)
1785 strcat(string, " extern"); /* 7 */
1786 if (modifiers & HLSL_STORAGE_NOINTERPOLATION)
1787 strcat(string, " nointerpolation"); /* 16 */
1788 if (modifiers & HLSL_MODIFIER_PRECISE)
1789 strcat(string, " precise"); /* 8 */
1790 if (modifiers & HLSL_STORAGE_SHARED)
1791 strcat(string, " shared"); /* 7 */
1792 if (modifiers & HLSL_STORAGE_GROUPSHARED)
1793 strcat(string, " groupshared"); /* 12 */
1794 if (modifiers & HLSL_STORAGE_STATIC)
1795 strcat(string, " static"); /* 7 */
1796 if (modifiers & HLSL_STORAGE_UNIFORM)
1797 strcat(string, " uniform"); /* 8 */
1798 if (modifiers & HLSL_STORAGE_VOLATILE)
1799 strcat(string, " volatile"); /* 9 */
1800 if (modifiers & HLSL_MODIFIER_CONST)
1801 strcat(string, " const"); /* 6 */
1802 if (modifiers & HLSL_MODIFIER_ROW_MAJOR)
1803 strcat(string, " row_major"); /* 10 */
1804 if (modifiers & HLSL_MODIFIER_COLUMN_MAJOR)
1805 strcat(string, " column_major"); /* 13 */
1806 if ((modifiers & (HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT)) == (HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT))
1807 strcat(string, " inout"); /* 6 */
1808 else if (modifiers & HLSL_MODIFIER_IN)
1809 strcat(string, " in"); /* 3 */
1810 else if (modifiers & HLSL_MODIFIER_OUT)
1811 strcat(string, " out"); /* 4 */
1813 return wine_dbg_sprintf("%s", string[0] ? string + 1 : "");
1816 const char *debug_node_type(enum hlsl_ir_node_type type)
1818 static const char * const names[] =
1820 "HLSL_IR_ASSIGNMENT",
1821 "HLSL_IR_CONSTANT",
1822 "HLSL_IR_CONSTRUCTOR",
1823 "HLSL_IR_DEREF",
1824 "HLSL_IR_EXPR",
1825 "HLSL_IR_IF",
1826 "HLSL_IR_LOOP",
1827 "HLSL_IR_JUMP",
1828 "HLSL_IR_SWIZZLE",
1831 if (type >= ARRAY_SIZE(names))
1832 return "Unexpected node type";
1833 return names[type];
1836 static void debug_dump_instr(const struct hlsl_ir_node *instr);
1838 static void debug_dump_instr_list(const struct list *list)
1840 struct hlsl_ir_node *instr;
1842 LIST_FOR_EACH_ENTRY(instr, list, struct hlsl_ir_node, entry)
1844 debug_dump_instr(instr);
1845 wine_dbg_printf("\n");
1849 static void debug_dump_src(const struct hlsl_ir_node *node)
1851 wine_dbg_printf("%p", node);
1854 static void debug_dump_ir_var(const struct hlsl_ir_var *var)
1856 if (var->modifiers)
1857 wine_dbg_printf("%s ", debug_modifiers(var->modifiers));
1858 wine_dbg_printf("%s %s", debug_hlsl_type(var->data_type), var->name);
1859 if (var->semantic)
1860 wine_dbg_printf(" : %s", debugstr_a(var->semantic));
1863 static void debug_dump_ir_deref(const struct hlsl_ir_deref *deref)
1865 switch (deref->type)
1867 case HLSL_IR_DEREF_VAR:
1868 wine_dbg_printf("deref(");
1869 debug_dump_ir_var(deref->v.var);
1870 wine_dbg_printf(")");
1871 break;
1872 case HLSL_IR_DEREF_ARRAY:
1873 debug_dump_src(deref->v.array.array);
1874 wine_dbg_printf("[");
1875 debug_dump_src(deref->v.array.index);
1876 wine_dbg_printf("]");
1877 break;
1878 case HLSL_IR_DEREF_RECORD:
1879 debug_dump_src(deref->v.record.record);
1880 wine_dbg_printf(".%s", debugstr_a(deref->v.record.field->name));
1881 break;
1885 static void debug_dump_ir_constant(const struct hlsl_ir_constant *constant)
1887 struct hlsl_type *type = constant->node.data_type;
1888 unsigned int x, y;
1890 if (type->dimy != 1)
1891 wine_dbg_printf("{");
1892 for (y = 0; y < type->dimy; ++y)
1894 if (type->dimx != 1)
1895 wine_dbg_printf("{");
1896 for (x = 0; x < type->dimx; ++x)
1898 switch (type->base_type)
1900 case HLSL_TYPE_FLOAT:
1901 wine_dbg_printf("%g ", (double)constant->v.value.f[y * type->dimx + x]);
1902 break;
1903 case HLSL_TYPE_DOUBLE:
1904 wine_dbg_printf("%g ", constant->v.value.d[y * type->dimx + x]);
1905 break;
1906 case HLSL_TYPE_INT:
1907 wine_dbg_printf("%d ", constant->v.value.i[y * type->dimx + x]);
1908 break;
1909 case HLSL_TYPE_UINT:
1910 wine_dbg_printf("%u ", constant->v.value.u[y * type->dimx + x]);
1911 break;
1912 case HLSL_TYPE_BOOL:
1913 wine_dbg_printf("%s ", constant->v.value.b[y * type->dimx + x] == FALSE ? "false" : "true");
1914 break;
1915 default:
1916 wine_dbg_printf("Constants of type %s not supported\n", debug_base_type(type));
1919 if (type->dimx != 1)
1920 wine_dbg_printf("}");
1922 if (type->dimy != 1)
1923 wine_dbg_printf("}");
1926 static const char *debug_expr_op(const struct hlsl_ir_expr *expr)
1928 static const char * const op_names[] =
1930 "~",
1931 "!",
1932 "-",
1933 "abs",
1934 "sign",
1935 "rcp",
1936 "rsq",
1937 "sqrt",
1938 "nrm",
1939 "exp2",
1940 "log2",
1942 "cast",
1944 "fract",
1946 "sin",
1947 "cos",
1948 "sin_reduced",
1949 "cos_reduced",
1951 "dsx",
1952 "dsy",
1954 "sat",
1956 "pre++",
1957 "pre--",
1958 "post++",
1959 "post--",
1961 "+",
1962 "-",
1963 "*",
1964 "/",
1966 "%",
1968 "<",
1969 ">",
1970 "<=",
1971 ">=",
1972 "==",
1973 "!=",
1975 "&&",
1976 "||",
1978 "<<",
1979 ">>",
1980 "&",
1981 "|",
1982 "^",
1984 "dot",
1985 "crs",
1986 "min",
1987 "max",
1989 "pow",
1991 "lerp",
1993 ",",
1996 if (expr->op == HLSL_IR_UNOP_CAST)
1997 return debug_hlsl_type(expr->node.data_type);
1999 return op_names[expr->op];
2002 /* Dumps the expression in a prefix "operator (operands)" form */
2003 static void debug_dump_ir_expr(const struct hlsl_ir_expr *expr)
2005 unsigned int i;
2007 wine_dbg_printf("%s (", debug_expr_op(expr));
2008 for (i = 0; i < 3 && expr->operands[i]; ++i)
2010 debug_dump_src(expr->operands[i]);
2011 wine_dbg_printf(" ");
2013 wine_dbg_printf(")");
2016 static void debug_dump_ir_constructor(const struct hlsl_ir_constructor *constructor)
2018 unsigned int i;
2020 wine_dbg_printf("%s (", debug_hlsl_type(constructor->node.data_type));
2021 for (i = 0; i < constructor->args_count; ++i)
2023 debug_dump_src(constructor->args[i]);
2024 wine_dbg_printf(" ");
2026 wine_dbg_printf(")");
2029 static const char *debug_writemask(DWORD writemask)
2031 static const char components[] = {'x', 'y', 'z', 'w'};
2032 char string[5];
2033 unsigned int i = 0, pos = 0;
2035 assert(!(writemask & ~BWRITERSP_WRITEMASK_ALL));
2037 while (writemask)
2039 if (writemask & 1)
2040 string[pos++] = components[i];
2041 writemask >>= 1;
2042 i++;
2044 string[pos] = '\0';
2045 return wine_dbg_sprintf(".%s", string);
2048 static void debug_dump_ir_assignment(const struct hlsl_ir_assignment *assign)
2050 wine_dbg_printf("= (");
2051 debug_dump_src(assign->lhs);
2052 if (assign->writemask != BWRITERSP_WRITEMASK_ALL)
2053 wine_dbg_printf("%s", debug_writemask(assign->writemask));
2054 wine_dbg_printf(" ");
2055 debug_dump_src(assign->rhs);
2056 wine_dbg_printf(")");
2059 static void debug_dump_ir_swizzle(const struct hlsl_ir_swizzle *swizzle)
2061 unsigned int i;
2063 debug_dump_src(swizzle->val);
2064 wine_dbg_printf(".");
2065 if (swizzle->val->data_type->dimy > 1)
2067 for (i = 0; i < swizzle->node.data_type->dimx; ++i)
2068 wine_dbg_printf("_m%u%u", (swizzle->swizzle >> i * 8) & 0xf, (swizzle->swizzle >> (i * 8 + 4)) & 0xf);
2070 else
2072 static const char c[] = {'x', 'y', 'z', 'w'};
2074 for (i = 0; i < swizzle->node.data_type->dimx; ++i)
2075 wine_dbg_printf("%c", c[(swizzle->swizzle >> i * 2) & 0x3]);
2079 static void debug_dump_ir_jump(const struct hlsl_ir_jump *jump)
2081 switch (jump->type)
2083 case HLSL_IR_JUMP_BREAK:
2084 wine_dbg_printf("break");
2085 break;
2086 case HLSL_IR_JUMP_CONTINUE:
2087 wine_dbg_printf("continue");
2088 break;
2089 case HLSL_IR_JUMP_DISCARD:
2090 wine_dbg_printf("discard");
2091 break;
2092 case HLSL_IR_JUMP_RETURN:
2093 wine_dbg_printf("return ");
2094 if (jump->return_value)
2095 debug_dump_src(jump->return_value);
2096 wine_dbg_printf(";");
2097 break;
2101 static void debug_dump_ir_if(const struct hlsl_ir_if *if_node)
2103 wine_dbg_printf("if (");
2104 debug_dump_src(if_node->condition);
2105 wine_dbg_printf(")\n{\n");
2106 debug_dump_instr_list(if_node->then_instrs);
2107 wine_dbg_printf("}\n");
2108 if (if_node->else_instrs)
2110 wine_dbg_printf("else\n{\n");
2111 debug_dump_instr_list(if_node->else_instrs);
2112 wine_dbg_printf("}\n");
2116 static void debug_dump_ir_loop(const struct hlsl_ir_loop *loop)
2118 wine_dbg_printf("for (;;)\n{\n");
2119 debug_dump_instr_list(loop->body);
2120 wine_dbg_printf("}\n");
2123 static void debug_dump_instr(const struct hlsl_ir_node *instr)
2125 wine_dbg_printf("%p: ", instr);
2126 switch (instr->type)
2128 case HLSL_IR_EXPR:
2129 debug_dump_ir_expr(expr_from_node(instr));
2130 break;
2131 case HLSL_IR_DEREF:
2132 debug_dump_ir_deref(deref_from_node(instr));
2133 break;
2134 case HLSL_IR_CONSTANT:
2135 debug_dump_ir_constant(constant_from_node(instr));
2136 break;
2137 case HLSL_IR_ASSIGNMENT:
2138 debug_dump_ir_assignment(assignment_from_node(instr));
2139 break;
2140 case HLSL_IR_SWIZZLE:
2141 debug_dump_ir_swizzle(swizzle_from_node(instr));
2142 break;
2143 case HLSL_IR_CONSTRUCTOR:
2144 debug_dump_ir_constructor(constructor_from_node(instr));
2145 break;
2146 case HLSL_IR_JUMP:
2147 debug_dump_ir_jump(jump_from_node(instr));
2148 break;
2149 case HLSL_IR_IF:
2150 debug_dump_ir_if(if_from_node(instr));
2151 break;
2152 case HLSL_IR_LOOP:
2153 debug_dump_ir_loop(loop_from_node(instr));
2154 break;
2155 default:
2156 wine_dbg_printf("<No dump function for %s>", debug_node_type(instr->type));
2160 void debug_dump_ir_function_decl(const struct hlsl_ir_function_decl *func)
2162 struct hlsl_ir_var *param;
2164 TRACE("Dumping function %s.\n", debugstr_a(func->func->name));
2165 TRACE("Function parameters:\n");
2166 LIST_FOR_EACH_ENTRY(param, func->parameters, struct hlsl_ir_var, param_entry)
2168 debug_dump_ir_var(param);
2169 wine_dbg_printf("\n");
2171 if (func->semantic)
2172 TRACE("Function semantic: %s\n", debugstr_a(func->semantic));
2173 if (func->body)
2175 debug_dump_instr_list(func->body);
2179 void free_hlsl_type(struct hlsl_type *type)
2181 struct hlsl_struct_field *field, *next_field;
2183 d3dcompiler_free((void *)type->name);
2184 if (type->type == HLSL_CLASS_STRUCT)
2186 LIST_FOR_EACH_ENTRY_SAFE(field, next_field, type->e.elements, struct hlsl_struct_field, entry)
2188 d3dcompiler_free((void *)field->name);
2189 d3dcompiler_free((void *)field->semantic);
2190 d3dcompiler_free(field);
2193 d3dcompiler_free(type);
2196 void free_instr_list(struct list *list)
2198 struct hlsl_ir_node *node, *next_node;
2200 if (!list)
2201 return;
2202 LIST_FOR_EACH_ENTRY_SAFE(node, next_node, list, struct hlsl_ir_node, entry)
2203 free_instr(node);
2204 d3dcompiler_free(list);
2207 static void free_ir_constant(struct hlsl_ir_constant *constant)
2209 struct hlsl_type *type = constant->node.data_type;
2210 unsigned int i;
2211 struct hlsl_ir_constant *field, *next_field;
2213 switch (type->type)
2215 case HLSL_CLASS_ARRAY:
2216 for (i = 0; i < type->e.array.elements_count; ++i)
2217 free_ir_constant(&constant->v.array_elements[i]);
2218 d3dcompiler_free(constant->v.array_elements);
2219 break;
2220 case HLSL_CLASS_STRUCT:
2221 LIST_FOR_EACH_ENTRY_SAFE(field, next_field, constant->v.struct_elements, struct hlsl_ir_constant, node.entry)
2222 free_ir_constant(field);
2223 break;
2224 default:
2225 break;
2227 d3dcompiler_free(constant);
2230 static void free_ir_deref(struct hlsl_ir_deref *deref)
2232 d3dcompiler_free(deref);
2235 static void free_ir_swizzle(struct hlsl_ir_swizzle *swizzle)
2237 d3dcompiler_free(swizzle);
2240 static void free_ir_constructor(struct hlsl_ir_constructor *constructor)
2242 d3dcompiler_free(constructor);
2245 static void free_ir_expr(struct hlsl_ir_expr *expr)
2247 d3dcompiler_free(expr);
2250 static void free_ir_assignment(struct hlsl_ir_assignment *assignment)
2252 d3dcompiler_free(assignment);
2255 static void free_ir_if(struct hlsl_ir_if *if_node)
2257 free_instr_list(if_node->then_instrs);
2258 free_instr_list(if_node->else_instrs);
2259 d3dcompiler_free(if_node);
2262 static void free_ir_loop(struct hlsl_ir_loop *loop)
2264 free_instr_list(loop->body);
2265 d3dcompiler_free(loop);
2268 static void free_ir_jump(struct hlsl_ir_jump *jump)
2270 d3dcompiler_free(jump);
2273 void free_instr(struct hlsl_ir_node *node)
2275 switch (node->type)
2277 case HLSL_IR_CONSTANT:
2278 free_ir_constant(constant_from_node(node));
2279 break;
2280 case HLSL_IR_DEREF:
2281 free_ir_deref(deref_from_node(node));
2282 break;
2283 case HLSL_IR_SWIZZLE:
2284 free_ir_swizzle(swizzle_from_node(node));
2285 break;
2286 case HLSL_IR_CONSTRUCTOR:
2287 free_ir_constructor(constructor_from_node(node));
2288 break;
2289 case HLSL_IR_EXPR:
2290 free_ir_expr(expr_from_node(node));
2291 break;
2292 case HLSL_IR_ASSIGNMENT:
2293 free_ir_assignment(assignment_from_node(node));
2294 break;
2295 case HLSL_IR_IF:
2296 free_ir_if(if_from_node(node));
2297 break;
2298 case HLSL_IR_LOOP:
2299 free_ir_loop(loop_from_node(node));
2300 break;
2301 case HLSL_IR_JUMP:
2302 free_ir_jump(jump_from_node(node));
2303 break;
2304 default:
2305 FIXME("Unsupported node type %s\n", debug_node_type(node->type));
2309 static void free_function_decl(struct hlsl_ir_function_decl *decl)
2311 d3dcompiler_free((void *)decl->semantic);
2312 d3dcompiler_free(decl->parameters);
2313 free_instr_list(decl->body);
2314 d3dcompiler_free(decl);
2317 static void free_function_decl_rb(struct wine_rb_entry *entry, void *context)
2319 free_function_decl(WINE_RB_ENTRY_VALUE(entry, struct hlsl_ir_function_decl, entry));
2322 static void free_function(struct hlsl_ir_function *func)
2324 wine_rb_destroy(&func->overloads, free_function_decl_rb, NULL);
2325 d3dcompiler_free((void *)func->name);
2326 d3dcompiler_free(func);
2329 void free_function_rb(struct wine_rb_entry *entry, void *context)
2331 free_function(WINE_RB_ENTRY_VALUE(entry, struct hlsl_ir_function, entry));
2334 void add_function_decl(struct wine_rb_tree *funcs, char *name, struct hlsl_ir_function_decl *decl, BOOL intrinsic)
2336 struct hlsl_ir_function *func;
2337 struct wine_rb_entry *func_entry, *old_entry;
2339 func_entry = wine_rb_get(funcs, name);
2340 if (func_entry)
2342 func = WINE_RB_ENTRY_VALUE(func_entry, struct hlsl_ir_function, entry);
2343 if (intrinsic != func->intrinsic)
2345 if (intrinsic)
2347 ERR("Redeclaring a user defined function as an intrinsic.\n");
2348 return;
2350 TRACE("Function %s redeclared as a user defined function.\n", debugstr_a(name));
2351 func->intrinsic = intrinsic;
2352 wine_rb_destroy(&func->overloads, free_function_decl_rb, NULL);
2353 wine_rb_init(&func->overloads, compare_function_decl_rb);
2355 decl->func = func;
2356 if ((old_entry = wine_rb_get(&func->overloads, decl->parameters)))
2358 struct hlsl_ir_function_decl *old_decl =
2359 WINE_RB_ENTRY_VALUE(old_entry, struct hlsl_ir_function_decl, entry);
2361 if (!decl->body)
2363 free_function_decl(decl);
2364 d3dcompiler_free(name);
2365 return;
2367 wine_rb_remove(&func->overloads, old_entry);
2368 free_function_decl(old_decl);
2370 wine_rb_put(&func->overloads, decl->parameters, &decl->entry);
2371 d3dcompiler_free(name);
2372 return;
2374 func = d3dcompiler_alloc(sizeof(*func));
2375 func->name = name;
2376 wine_rb_init(&func->overloads, compare_function_decl_rb);
2377 decl->func = func;
2378 wine_rb_put(&func->overloads, decl->parameters, &decl->entry);
2379 func->intrinsic = intrinsic;
2380 wine_rb_put(funcs, func->name, &func->entry);
2382 #endif