riched20: Implement IsEqual() for ranges.
[wine.git] / dlls / d3dcompiler_43 / utils.c
blob83f815f05852327d4f3597510e3a4107e7b2d603
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 "config.h"
25 #include "wine/port.h"
27 #include <stdio.h>
29 #include "d3dcompiler_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
33 #define WINE_D3DCOMPILER_TO_STR(x) case x: return #x
35 const char *debug_d3dcompiler_shader_variable_class(D3D_SHADER_VARIABLE_CLASS c)
37 switch (c)
39 WINE_D3DCOMPILER_TO_STR(D3D_SVC_SCALAR);
40 WINE_D3DCOMPILER_TO_STR(D3D_SVC_VECTOR);
41 WINE_D3DCOMPILER_TO_STR(D3D_SVC_MATRIX_ROWS);
42 WINE_D3DCOMPILER_TO_STR(D3D_SVC_MATRIX_COLUMNS);
43 WINE_D3DCOMPILER_TO_STR(D3D_SVC_OBJECT);
44 WINE_D3DCOMPILER_TO_STR(D3D_SVC_STRUCT);
45 WINE_D3DCOMPILER_TO_STR(D3D_SVC_INTERFACE_CLASS);
46 WINE_D3DCOMPILER_TO_STR(D3D_SVC_INTERFACE_POINTER);
47 default:
48 FIXME("Unrecognized D3D_SHADER_VARIABLE_CLASS %#x.\n", c);
49 return "unrecognized";
53 const char *debug_d3dcompiler_shader_variable_type(D3D_SHADER_VARIABLE_TYPE t)
55 switch (t)
57 WINE_D3DCOMPILER_TO_STR(D3D_SVT_VOID);
58 WINE_D3DCOMPILER_TO_STR(D3D_SVT_BOOL);
59 WINE_D3DCOMPILER_TO_STR(D3D_SVT_INT);
60 WINE_D3DCOMPILER_TO_STR(D3D_SVT_FLOAT);
61 WINE_D3DCOMPILER_TO_STR(D3D_SVT_STRING);
62 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE);
63 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE1D);
64 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE2D);
65 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE3D);
66 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURECUBE);
67 WINE_D3DCOMPILER_TO_STR(D3D_SVT_SAMPLER);
68 WINE_D3DCOMPILER_TO_STR(D3D_SVT_PIXELSHADER);
69 WINE_D3DCOMPILER_TO_STR(D3D_SVT_VERTEXSHADER);
70 WINE_D3DCOMPILER_TO_STR(D3D_SVT_UINT);
71 WINE_D3DCOMPILER_TO_STR(D3D_SVT_UINT8);
72 WINE_D3DCOMPILER_TO_STR(D3D_SVT_GEOMETRYSHADER);
73 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RASTERIZER);
74 WINE_D3DCOMPILER_TO_STR(D3D_SVT_DEPTHSTENCIL);
75 WINE_D3DCOMPILER_TO_STR(D3D_SVT_BLEND);
76 WINE_D3DCOMPILER_TO_STR(D3D_SVT_BUFFER);
77 WINE_D3DCOMPILER_TO_STR(D3D_SVT_CBUFFER);
78 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TBUFFER);
79 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE1DARRAY);
80 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE2DARRAY);
81 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RENDERTARGETVIEW);
82 WINE_D3DCOMPILER_TO_STR(D3D_SVT_DEPTHSTENCILVIEW);
83 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE2DMS);
84 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE2DMSARRAY);
85 WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURECUBEARRAY);
86 WINE_D3DCOMPILER_TO_STR(D3D_SVT_HULLSHADER);
87 WINE_D3DCOMPILER_TO_STR(D3D_SVT_DOMAINSHADER);
88 WINE_D3DCOMPILER_TO_STR(D3D_SVT_INTERFACE_POINTER);
89 WINE_D3DCOMPILER_TO_STR(D3D_SVT_COMPUTESHADER);
90 WINE_D3DCOMPILER_TO_STR(D3D_SVT_DOUBLE);
91 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE1D);
92 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE1DARRAY);
93 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE2D);
94 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE2DARRAY);
95 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE3D);
96 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWBUFFER);
97 WINE_D3DCOMPILER_TO_STR(D3D_SVT_BYTEADDRESS_BUFFER);
98 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWBYTEADDRESS_BUFFER);
99 WINE_D3DCOMPILER_TO_STR(D3D_SVT_STRUCTURED_BUFFER);
100 WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWSTRUCTURED_BUFFER);
101 WINE_D3DCOMPILER_TO_STR(D3D_SVT_APPEND_STRUCTURED_BUFFER);
102 WINE_D3DCOMPILER_TO_STR(D3D_SVT_CONSUME_STRUCTURED_BUFFER);
103 default:
104 FIXME("Unrecognized D3D_SHADER_VARIABLE_TYPE %#x.\n", t);
105 return "unrecognized";
109 const char *debug_d3dcompiler_d3d_blob_part(D3D_BLOB_PART part)
111 switch(part)
113 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_INPUT_SIGNATURE_BLOB);
114 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_OUTPUT_SIGNATURE_BLOB);
115 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB);
116 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB);
117 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_ALL_SIGNATURE_BLOB);
118 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_DEBUG_INFO);
119 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_LEGACY_SHADER);
120 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_XNA_PREPASS_SHADER);
121 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_XNA_SHADER);
122 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_TEST_ALTERNATE_SHADER);
123 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_TEST_COMPILE_DETAILS);
124 WINE_D3DCOMPILER_TO_STR(D3D_BLOB_TEST_COMPILE_PERF);
125 default:
126 FIXME("Unrecognized D3D_BLOB_PART %#x\n", part);
127 return "unrecognized";
131 const char *debug_print_srcmod(DWORD mod)
133 switch (mod)
135 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_NEG);
136 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_BIAS);
137 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_BIASNEG);
138 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_SIGN);
139 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_SIGNNEG);
140 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_COMP);
141 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_X2);
142 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_X2NEG);
143 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_DZ);
144 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_DW);
145 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_ABS);
146 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_ABSNEG);
147 WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_NOT);
148 default:
149 FIXME("Unrecognized source modifier %#x.\n", mod);
150 return "unrecognized_src_mod";
154 #undef WINE_D3DCOMPILER_TO_STR
156 const char *debug_print_dstmod(DWORD mod)
158 switch (mod)
160 case 0:
161 return "";
162 case BWRITERSPDM_SATURATE:
163 return "_sat";
164 case BWRITERSPDM_PARTIALPRECISION:
165 return "_pp";
166 case BWRITERSPDM_MSAMPCENTROID:
167 return "_centroid";
168 case BWRITERSPDM_SATURATE | BWRITERSPDM_PARTIALPRECISION:
169 return "_sat_pp";
170 case BWRITERSPDM_SATURATE | BWRITERSPDM_MSAMPCENTROID:
171 return "_sat_centroid";
172 case BWRITERSPDM_PARTIALPRECISION | BWRITERSPDM_MSAMPCENTROID:
173 return "_pp_centroid";
174 case BWRITERSPDM_SATURATE | BWRITERSPDM_PARTIALPRECISION | BWRITERSPDM_MSAMPCENTROID:
175 return "_sat_pp_centroid";
176 default:
177 return "Unexpected modifier\n";
181 const char *debug_print_shift(DWORD shift)
183 static const char * const shiftstrings[] =
186 "_x2",
187 "_x4",
188 "_x8",
189 "_x16",
190 "_x32",
197 "_d16",
198 "_d8",
199 "_d4",
200 "_d2",
202 return shiftstrings[shift];
205 static const char *get_regname(const struct shader_reg *reg)
207 switch (reg->type)
209 case BWRITERSPR_TEMP:
210 return wine_dbg_sprintf("r%u", reg->regnum);
211 case BWRITERSPR_INPUT:
212 return wine_dbg_sprintf("v%u", reg->regnum);
213 case BWRITERSPR_CONST:
214 return wine_dbg_sprintf("c%u", reg->regnum);
215 case BWRITERSPR_ADDR:
216 return wine_dbg_sprintf("a%u", reg->regnum);
217 case BWRITERSPR_TEXTURE:
218 return wine_dbg_sprintf("t%u", reg->regnum);
219 case BWRITERSPR_RASTOUT:
220 switch (reg->regnum)
222 case BWRITERSRO_POSITION: return "oPos";
223 case BWRITERSRO_FOG: return "oFog";
224 case BWRITERSRO_POINT_SIZE: return "oPts";
225 default: return "Unexpected RASTOUT";
227 case BWRITERSPR_ATTROUT:
228 return wine_dbg_sprintf("oD%u", reg->regnum);
229 case BWRITERSPR_TEXCRDOUT:
230 return wine_dbg_sprintf("oT%u", reg->regnum);
231 case BWRITERSPR_OUTPUT:
232 return wine_dbg_sprintf("o%u", reg->regnum);
233 case BWRITERSPR_CONSTINT:
234 return wine_dbg_sprintf("i%u", reg->regnum);
235 case BWRITERSPR_COLOROUT:
236 return wine_dbg_sprintf("oC%u", reg->regnum);
237 case BWRITERSPR_DEPTHOUT:
238 return "oDepth";
239 case BWRITERSPR_SAMPLER:
240 return wine_dbg_sprintf("s%u", reg->regnum);
241 case BWRITERSPR_CONSTBOOL:
242 return wine_dbg_sprintf("b%u", reg->regnum);
243 case BWRITERSPR_LOOP:
244 return "aL";
245 case BWRITERSPR_MISCTYPE:
246 switch (reg->regnum)
248 case 0: return "vPos";
249 case 1: return "vFace";
250 default: return "unexpected misctype";
252 case BWRITERSPR_LABEL:
253 return wine_dbg_sprintf("l%u", reg->regnum);
254 case BWRITERSPR_PREDICATE:
255 return wine_dbg_sprintf("p%u", reg->regnum);
256 default:
257 return wine_dbg_sprintf("unknown regname %#x", reg->type);
261 static const char *debug_print_writemask(DWORD mask)
263 char ret[6];
264 unsigned char pos = 1;
266 if(mask == BWRITERSP_WRITEMASK_ALL) return "";
267 ret[0] = '.';
268 if(mask & BWRITERSP_WRITEMASK_0) ret[pos++] = 'x';
269 if(mask & BWRITERSP_WRITEMASK_1) ret[pos++] = 'y';
270 if(mask & BWRITERSP_WRITEMASK_2) ret[pos++] = 'z';
271 if(mask & BWRITERSP_WRITEMASK_3) ret[pos++] = 'w';
272 ret[pos] = 0;
274 return wine_dbg_sprintf("%s", ret);
277 static const char *debug_print_swizzle(DWORD arg)
279 char ret[6];
280 unsigned int i;
281 DWORD swizzle[4];
283 switch (arg)
285 case BWRITERVS_NOSWIZZLE:
286 return "";
287 case BWRITERVS_SWIZZLE_X:
288 return ".x";
289 case BWRITERVS_SWIZZLE_Y:
290 return ".y";
291 case BWRITERVS_SWIZZLE_Z:
292 return ".z";
293 case BWRITERVS_SWIZZLE_W:
294 return ".w";
297 swizzle[0] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 0)) & 0x03;
298 swizzle[1] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 2)) & 0x03;
299 swizzle[2] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 4)) & 0x03;
300 swizzle[3] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 6)) & 0x03;
302 ret[0] = '.';
303 for (i = 0; i < 4; ++i)
305 switch (swizzle[i])
307 case 0: ret[1 + i] = 'x'; break;
308 case 1: ret[1 + i] = 'y'; break;
309 case 2: ret[1 + i] = 'z'; break;
310 case 3: ret[1 + i] = 'w'; break;
313 ret[5] = '\0';
315 return wine_dbg_sprintf("%s", ret);
318 static const char *debug_print_relarg(const struct shader_reg *reg)
320 const char *short_swizzle;
321 if (!reg->rel_reg) return "";
323 short_swizzle = debug_print_swizzle(reg->rel_reg->u.swizzle);
325 if (reg->rel_reg->type == BWRITERSPR_ADDR)
326 return wine_dbg_sprintf("[a%u%s]", reg->rel_reg->regnum, short_swizzle);
327 else if(reg->rel_reg->type == BWRITERSPR_LOOP && reg->rel_reg->regnum == 0)
328 return wine_dbg_sprintf("[aL%s]", short_swizzle);
329 else
330 return "Unexpected relative addressing argument";
333 const char *debug_print_dstreg(const struct shader_reg *reg)
335 return wine_dbg_sprintf("%s%s%s", get_regname(reg),
336 debug_print_relarg(reg),
337 debug_print_writemask(reg->u.writemask));
340 const char *debug_print_srcreg(const struct shader_reg *reg)
342 switch (reg->srcmod)
344 case BWRITERSPSM_NONE:
345 return wine_dbg_sprintf("%s%s%s", get_regname(reg),
346 debug_print_relarg(reg),
347 debug_print_swizzle(reg->u.swizzle));
348 case BWRITERSPSM_NEG:
349 return wine_dbg_sprintf("-%s%s%s", get_regname(reg),
350 debug_print_relarg(reg),
351 debug_print_swizzle(reg->u.swizzle));
352 case BWRITERSPSM_BIAS:
353 return wine_dbg_sprintf("%s%s_bias%s", get_regname(reg),
354 debug_print_relarg(reg),
355 debug_print_swizzle(reg->u.swizzle));
356 case BWRITERSPSM_BIASNEG:
357 return wine_dbg_sprintf("-%s%s_bias%s", get_regname(reg),
358 debug_print_relarg(reg),
359 debug_print_swizzle(reg->u.swizzle));
360 case BWRITERSPSM_SIGN:
361 return wine_dbg_sprintf("%s%s_bx2%s", get_regname(reg),
362 debug_print_relarg(reg),
363 debug_print_swizzle(reg->u.swizzle));
364 case BWRITERSPSM_SIGNNEG:
365 return wine_dbg_sprintf("-%s%s_bx2%s", get_regname(reg),
366 debug_print_relarg(reg),
367 debug_print_swizzle(reg->u.swizzle));
368 case BWRITERSPSM_COMP:
369 return wine_dbg_sprintf("1 - %s%s%s", get_regname(reg),
370 debug_print_relarg(reg),
371 debug_print_swizzle(reg->u.swizzle));
372 case BWRITERSPSM_X2:
373 return wine_dbg_sprintf("%s%s_x2%s", get_regname(reg),
374 debug_print_relarg(reg),
375 debug_print_swizzle(reg->u.swizzle));
376 case BWRITERSPSM_X2NEG:
377 return wine_dbg_sprintf("-%s%s_x2%s", get_regname(reg),
378 debug_print_relarg(reg),
379 debug_print_swizzle(reg->u.swizzle));
380 case BWRITERSPSM_DZ:
381 return wine_dbg_sprintf("%s%s_dz%s", get_regname(reg),
382 debug_print_relarg(reg),
383 debug_print_swizzle(reg->u.swizzle));
384 case BWRITERSPSM_DW:
385 return wine_dbg_sprintf("%s%s_dw%s", get_regname(reg),
386 debug_print_relarg(reg),
387 debug_print_swizzle(reg->u.swizzle));
388 case BWRITERSPSM_ABS:
389 return wine_dbg_sprintf("%s%s_abs%s", get_regname(reg),
390 debug_print_relarg(reg),
391 debug_print_swizzle(reg->u.swizzle));
392 case BWRITERSPSM_ABSNEG:
393 return wine_dbg_sprintf("-%s%s_abs%s", get_regname(reg),
394 debug_print_relarg(reg),
395 debug_print_swizzle(reg->u.swizzle));
396 case BWRITERSPSM_NOT:
397 return wine_dbg_sprintf("!%s%s%s", get_regname(reg),
398 debug_print_relarg(reg),
399 debug_print_swizzle(reg->u.swizzle));
401 return "Unknown modifier";
404 const char *debug_print_comp(DWORD comp)
406 switch (comp)
408 case BWRITER_COMPARISON_NONE: return "";
409 case BWRITER_COMPARISON_GT: return "_gt";
410 case BWRITER_COMPARISON_EQ: return "_eq";
411 case BWRITER_COMPARISON_GE: return "_ge";
412 case BWRITER_COMPARISON_LT: return "_lt";
413 case BWRITER_COMPARISON_NE: return "_ne";
414 case BWRITER_COMPARISON_LE: return "_le";
415 default: return "_unknown";
419 const char *debug_print_opcode(DWORD opcode)
421 switch (opcode)
423 case BWRITERSIO_NOP: return "nop";
424 case BWRITERSIO_MOV: return "mov";
425 case BWRITERSIO_ADD: return "add";
426 case BWRITERSIO_SUB: return "sub";
427 case BWRITERSIO_MAD: return "mad";
428 case BWRITERSIO_MUL: return "mul";
429 case BWRITERSIO_RCP: return "rcp";
430 case BWRITERSIO_RSQ: return "rsq";
431 case BWRITERSIO_DP3: return "dp3";
432 case BWRITERSIO_DP4: return "dp4";
433 case BWRITERSIO_MIN: return "min";
434 case BWRITERSIO_MAX: return "max";
435 case BWRITERSIO_SLT: return "slt";
436 case BWRITERSIO_SGE: return "sge";
437 case BWRITERSIO_EXP: return "exp";
438 case BWRITERSIO_LOG: return "log";
439 case BWRITERSIO_LIT: return "lit";
440 case BWRITERSIO_DST: return "dst";
441 case BWRITERSIO_LRP: return "lrp";
442 case BWRITERSIO_FRC: return "frc";
443 case BWRITERSIO_M4x4: return "m4x4";
444 case BWRITERSIO_M4x3: return "m4x3";
445 case BWRITERSIO_M3x4: return "m3x4";
446 case BWRITERSIO_M3x3: return "m3x3";
447 case BWRITERSIO_M3x2: return "m3x2";
448 case BWRITERSIO_CALL: return "call";
449 case BWRITERSIO_CALLNZ: return "callnz";
450 case BWRITERSIO_LOOP: return "loop";
451 case BWRITERSIO_RET: return "ret";
452 case BWRITERSIO_ENDLOOP: return "endloop";
453 case BWRITERSIO_LABEL: return "label";
454 case BWRITERSIO_DCL: return "dcl";
455 case BWRITERSIO_POW: return "pow";
456 case BWRITERSIO_CRS: return "crs";
457 case BWRITERSIO_SGN: return "sgn";
458 case BWRITERSIO_ABS: return "abs";
459 case BWRITERSIO_NRM: return "nrm";
460 case BWRITERSIO_SINCOS: return "sincos";
461 case BWRITERSIO_REP: return "rep";
462 case BWRITERSIO_ENDREP: return "endrep";
463 case BWRITERSIO_IF: return "if";
464 case BWRITERSIO_IFC: return "ifc";
465 case BWRITERSIO_ELSE: return "else";
466 case BWRITERSIO_ENDIF: return "endif";
467 case BWRITERSIO_BREAK: return "break";
468 case BWRITERSIO_BREAKC: return "breakc";
469 case BWRITERSIO_MOVA: return "mova";
470 case BWRITERSIO_DEFB: return "defb";
471 case BWRITERSIO_DEFI: return "defi";
472 case BWRITERSIO_TEXCOORD: return "texcoord";
473 case BWRITERSIO_TEXKILL: return "texkill";
474 case BWRITERSIO_TEX: return "tex";
475 case BWRITERSIO_TEXBEM: return "texbem";
476 case BWRITERSIO_TEXBEML: return "texbeml";
477 case BWRITERSIO_TEXREG2AR: return "texreg2ar";
478 case BWRITERSIO_TEXREG2GB: return "texreg2gb";
479 case BWRITERSIO_TEXM3x2PAD: return "texm3x2pad";
480 case BWRITERSIO_TEXM3x2TEX: return "texm3x2tex";
481 case BWRITERSIO_TEXM3x3PAD: return "texm3x3pad";
482 case BWRITERSIO_TEXM3x3TEX: return "texm3x3tex";
483 case BWRITERSIO_TEXM3x3SPEC: return "texm3x3vspec";
484 case BWRITERSIO_TEXM3x3VSPEC: return "texm3x3vspec";
485 case BWRITERSIO_EXPP: return "expp";
486 case BWRITERSIO_LOGP: return "logp";
487 case BWRITERSIO_CND: return "cnd";
488 case BWRITERSIO_DEF: return "def";
489 case BWRITERSIO_TEXREG2RGB: return "texreg2rgb";
490 case BWRITERSIO_TEXDP3TEX: return "texdp3tex";
491 case BWRITERSIO_TEXM3x2DEPTH: return "texm3x2depth";
492 case BWRITERSIO_TEXDP3: return "texdp3";
493 case BWRITERSIO_TEXM3x3: return "texm3x3";
494 case BWRITERSIO_TEXDEPTH: return "texdepth";
495 case BWRITERSIO_CMP: return "cmp";
496 case BWRITERSIO_BEM: return "bem";
497 case BWRITERSIO_DP2ADD: return "dp2add";
498 case BWRITERSIO_DSX: return "dsx";
499 case BWRITERSIO_DSY: return "dsy";
500 case BWRITERSIO_TEXLDD: return "texldd";
501 case BWRITERSIO_SETP: return "setp";
502 case BWRITERSIO_TEXLDL: return "texldl";
503 case BWRITERSIO_BREAKP: return "breakp";
504 case BWRITERSIO_PHASE: return "phase";
506 case BWRITERSIO_TEXLDP: return "texldp";
507 case BWRITERSIO_TEXLDB: return "texldb";
509 default: return "unknown";
513 void skip_dword_unknown(const char **ptr, unsigned int count)
515 unsigned int i;
516 DWORD d;
518 FIXME("Skipping %u unknown DWORDs:\n", count);
519 for (i = 0; i < count; ++i)
521 read_dword(ptr, &d);
522 FIXME("\t0x%08x\n", d);
526 static void write_dword_unknown(char **ptr, DWORD d)
528 FIXME("Writing unknown DWORD 0x%08x\n", d);
529 write_dword(ptr, d);
532 HRESULT dxbc_add_section(struct dxbc *dxbc, DWORD tag, const char *data, DWORD data_size)
534 TRACE("dxbc %p, tag %s, size %#x.\n", dxbc, debugstr_an((const char *)&tag, 4), data_size);
536 if (dxbc->count >= dxbc->size)
538 struct dxbc_section *new_sections;
539 DWORD new_size = dxbc->size << 1;
541 new_sections = HeapReAlloc(GetProcessHeap(), 0, dxbc->sections, new_size * sizeof(*dxbc->sections));
542 if (!new_sections)
544 ERR("Failed to allocate dxbc section memory\n");
545 return E_OUTOFMEMORY;
548 dxbc->sections = new_sections;
549 dxbc->size = new_size;
552 dxbc->sections[dxbc->count].tag = tag;
553 dxbc->sections[dxbc->count].data_size = data_size;
554 dxbc->sections[dxbc->count].data = data;
555 ++dxbc->count;
557 return S_OK;
560 HRESULT dxbc_init(struct dxbc *dxbc, UINT size)
562 TRACE("dxbc %p, size %u.\n", dxbc, size);
564 /* use a good starting value for the size if none specified */
565 if (!size) size = 2;
567 dxbc->sections = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*dxbc->sections));
568 if (!dxbc->sections)
570 ERR("Failed to allocate dxbc section memory\n");
571 return E_OUTOFMEMORY;
574 dxbc->size = size;
575 dxbc->count = 0;
577 return S_OK;
580 HRESULT dxbc_parse(const char *data, SIZE_T data_size, struct dxbc *dxbc)
582 const char *ptr = data;
583 HRESULT hr;
584 unsigned int i;
585 DWORD tag, total_size, chunk_count;
587 if (!data)
589 WARN("No data supplied.\n");
590 return E_FAIL;
593 read_dword(&ptr, &tag);
594 TRACE("tag: %s.\n", debugstr_an((const char *)&tag, 4));
596 if (tag != TAG_DXBC)
598 WARN("Wrong tag.\n");
599 return E_FAIL;
602 /* checksum? */
603 skip_dword_unknown(&ptr, 4);
605 skip_dword_unknown(&ptr, 1);
607 read_dword(&ptr, &total_size);
608 TRACE("total size: %#x\n", total_size);
610 if (data_size != total_size)
612 WARN("Wrong size supplied.\n");
613 return D3DERR_INVALIDCALL;
616 read_dword(&ptr, &chunk_count);
617 TRACE("chunk count: %#x\n", chunk_count);
619 hr = dxbc_init(dxbc, chunk_count);
620 if (FAILED(hr))
622 WARN("Failed to init dxbc\n");
623 return hr;
626 for (i = 0; i < chunk_count; ++i)
628 DWORD chunk_tag, chunk_size;
629 const char *chunk_ptr;
630 DWORD chunk_offset;
632 read_dword(&ptr, &chunk_offset);
633 TRACE("chunk %u at offset %#x\n", i, chunk_offset);
635 chunk_ptr = data + chunk_offset;
637 read_dword(&chunk_ptr, &chunk_tag);
638 read_dword(&chunk_ptr, &chunk_size);
640 hr = dxbc_add_section(dxbc, chunk_tag, chunk_ptr, chunk_size);
641 if (FAILED(hr))
643 WARN("Failed to add section to dxbc\n");
644 return hr;
648 return hr;
651 void dxbc_destroy(struct dxbc *dxbc)
653 TRACE("dxbc %p.\n", dxbc);
655 HeapFree(GetProcessHeap(), 0, dxbc->sections);
658 HRESULT dxbc_write_blob(struct dxbc *dxbc, ID3DBlob **blob)
660 DWORD size = 32, offset = size + 4 * dxbc->count;
661 ID3DBlob *object;
662 HRESULT hr;
663 char *ptr;
664 unsigned int i;
666 TRACE("dxbc %p, blob %p.\n", dxbc, blob);
668 for (i = 0; i < dxbc->count; ++i)
670 size += 12 + dxbc->sections[i].data_size;
673 hr = D3DCreateBlob(size, &object);
674 if (FAILED(hr))
676 WARN("Failed to create blob\n");
677 return hr;
680 ptr = ID3D10Blob_GetBufferPointer(object);
682 write_dword(&ptr, TAG_DXBC);
684 /* signature(?) */
685 write_dword_unknown(&ptr, 0);
686 write_dword_unknown(&ptr, 0);
687 write_dword_unknown(&ptr, 0);
688 write_dword_unknown(&ptr, 0);
690 /* seems to be always 1 */
691 write_dword_unknown(&ptr, 1);
693 /* DXBC size */
694 write_dword(&ptr, size);
696 /* chunk count */
697 write_dword(&ptr, dxbc->count);
699 /* write the chunk offsets */
700 for (i = 0; i < dxbc->count; ++i)
702 write_dword(&ptr, offset);
703 offset += 8 + dxbc->sections[i].data_size;
706 /* write the chunks */
707 for (i = 0; i < dxbc->count; ++i)
709 write_dword(&ptr, dxbc->sections[i].tag);
710 write_dword(&ptr, dxbc->sections[i].data_size);
711 memcpy(ptr, dxbc->sections[i].data, dxbc->sections[i].data_size);
712 ptr += dxbc->sections[i].data_size;
715 TRACE("Created ID3DBlob %p\n", object);
717 *blob = object;
719 return S_OK;
722 void compilation_message(struct compilation_messages *msg, const char *fmt, va_list args)
724 char* buffer;
725 int rc, size;
727 if (msg->capacity == 0)
729 msg->string = d3dcompiler_alloc(MESSAGEBUFFER_INITIAL_SIZE);
730 if (msg->string == NULL)
732 ERR("Error allocating memory for parser messages\n");
733 return;
735 msg->capacity = MESSAGEBUFFER_INITIAL_SIZE;
738 while (1)
740 rc = vsnprintf(msg->string + msg->size,
741 msg->capacity - msg->size, fmt, args);
743 if (rc < 0 || rc >= msg->capacity - msg->size)
745 size = msg->capacity * 2;
746 buffer = d3dcompiler_realloc(msg->string, size);
747 if (buffer == NULL)
749 ERR("Error reallocating memory for parser messages\n");
750 return;
752 msg->string = buffer;
753 msg->capacity = size;
755 else
757 TRACE("%s", msg->string + msg->size);
758 msg->size += rc;
759 return;
764 BOOL add_declaration(struct hlsl_scope *scope, struct hlsl_ir_var *decl, BOOL local_var)
766 struct hlsl_ir_var *var;
768 LIST_FOR_EACH_ENTRY(var, &scope->vars, struct hlsl_ir_var, scope_entry)
770 if (!strcmp(decl->name, var->name))
771 return FALSE;
773 if (local_var && scope->upper->upper == hlsl_ctx.globals)
775 /* Check whether the variable redefines a function parameter. */
776 LIST_FOR_EACH_ENTRY(var, &scope->upper->vars, struct hlsl_ir_var, scope_entry)
778 if (!strcmp(decl->name, var->name))
779 return FALSE;
783 list_add_tail(&scope->vars, &decl->scope_entry);
784 return TRUE;
787 struct hlsl_ir_var *get_variable(struct hlsl_scope *scope, const char *name)
789 struct hlsl_ir_var *var;
791 LIST_FOR_EACH_ENTRY(var, &scope->vars, struct hlsl_ir_var, scope_entry)
793 if (!strcmp(name, var->name))
794 return var;
796 if (!scope->upper)
797 return NULL;
798 return get_variable(scope->upper, name);
801 void free_declaration(struct hlsl_ir_var *decl)
803 d3dcompiler_free((void *)decl->name);
804 d3dcompiler_free((void *)decl->semantic);
805 d3dcompiler_free(decl);
808 struct hlsl_type *new_hlsl_type(const char *name, enum hlsl_type_class type_class,
809 enum hlsl_base_type base_type, unsigned dimx, unsigned dimy)
811 struct hlsl_type *type;
813 type = d3dcompiler_alloc(sizeof(*type));
814 if (!type)
816 ERR("Out of memory\n");
817 return NULL;
819 type->name = name;
820 type->type = type_class;
821 type->base_type = base_type;
822 type->dimx = dimx;
823 type->dimy = dimy;
825 list_add_tail(&hlsl_ctx.types, &type->entry);
827 return type;
830 struct hlsl_type *new_array_type(struct hlsl_type *basic_type, unsigned int array_size)
832 struct hlsl_type *type = new_hlsl_type(NULL, HLSL_CLASS_ARRAY, HLSL_TYPE_FLOAT, 1, 1);
834 if (!type)
835 return NULL;
837 type->modifiers = basic_type->modifiers;
838 type->e.array.elements_count = array_size;
839 type->e.array.type = basic_type;
840 return type;
843 struct hlsl_type *get_type(struct hlsl_scope *scope, const char *name, BOOL recursive)
845 struct wine_rb_entry *entry = wine_rb_get(&scope->types, name);
846 if (entry)
847 return WINE_RB_ENTRY_VALUE(entry, struct hlsl_type, scope_entry);
849 if (recursive && scope->upper)
850 return get_type(scope->upper, name, recursive);
851 return NULL;
854 BOOL find_function(const char *name)
856 return wine_rb_get(&hlsl_ctx.functions, name) != NULL;
859 unsigned int components_count_type(struct hlsl_type *type)
861 unsigned int count = 0;
862 struct hlsl_struct_field *field;
864 if (type->type <= HLSL_CLASS_LAST_NUMERIC)
866 return type->dimx * type->dimy;
868 if (type->type == HLSL_CLASS_ARRAY)
870 return components_count_type(type->e.array.type) * type->e.array.elements_count;
872 if (type->type != HLSL_CLASS_STRUCT)
874 ERR("Unexpected data type %s.\n", debug_hlsl_type(type));
875 return 0;
878 LIST_FOR_EACH_ENTRY(field, type->e.elements, struct hlsl_struct_field, entry)
880 count += components_count_type(field->type);
882 return count;
885 BOOL compare_hlsl_types(const struct hlsl_type *t1, const struct hlsl_type *t2)
887 if (t1 == t2)
888 return TRUE;
890 if (t1->type != t2->type)
891 return FALSE;
892 if (t1->base_type != t2->base_type)
893 return FALSE;
894 if (t1->base_type == HLSL_TYPE_SAMPLER && t1->sampler_dim != t2->sampler_dim)
895 return FALSE;
896 if ((t1->modifiers & HLSL_MODIFIERS_COMPARISON_MASK)
897 != (t2->modifiers & HLSL_MODIFIERS_COMPARISON_MASK))
898 return FALSE;
899 if (t1->dimx != t2->dimx)
900 return FALSE;
901 if (t1->dimy != t2->dimy)
902 return FALSE;
903 if (t1->type == HLSL_CLASS_STRUCT)
905 struct list *t1cur, *t2cur;
906 struct hlsl_struct_field *t1field, *t2field;
908 t1cur = list_head(t1->e.elements);
909 t2cur = list_head(t2->e.elements);
910 while (t1cur && t2cur)
912 t1field = LIST_ENTRY(t1cur, struct hlsl_struct_field, entry);
913 t2field = LIST_ENTRY(t2cur, struct hlsl_struct_field, entry);
914 if (!compare_hlsl_types(t1field->type, t2field->type))
915 return FALSE;
916 if (strcmp(t1field->name, t2field->name))
917 return FALSE;
918 t1cur = list_next(t1->e.elements, t1cur);
919 t2cur = list_next(t2->e.elements, t2cur);
921 if (t1cur != t2cur)
922 return FALSE;
924 if (t1->type == HLSL_CLASS_ARRAY)
925 return t1->e.array.elements_count == t2->e.array.elements_count
926 && compare_hlsl_types(t1->e.array.type, t2->e.array.type);
928 return TRUE;
931 struct hlsl_type *clone_hlsl_type(struct hlsl_type *old)
933 struct hlsl_type *type;
934 struct hlsl_struct_field *old_field, *field;
936 type = d3dcompiler_alloc(sizeof(*type));
937 if (!type)
939 ERR("Out of memory\n");
940 return NULL;
942 if (old->name)
944 type->name = d3dcompiler_strdup(old->name);
945 if (!type->name)
947 d3dcompiler_free(type);
948 return NULL;
951 type->type = old->type;
952 type->base_type = old->base_type;
953 type->dimx = old->dimx;
954 type->dimy = old->dimy;
955 type->modifiers = old->modifiers;
956 type->sampler_dim = old->sampler_dim;
957 switch (old->type)
959 case HLSL_CLASS_ARRAY:
960 type->e.array.type = old->e.array.type;
961 type->e.array.elements_count = old->e.array.elements_count;
962 break;
963 case HLSL_CLASS_STRUCT:
964 type->e.elements = d3dcompiler_alloc(sizeof(*type->e.elements));
965 if (!type->e.elements)
967 d3dcompiler_free((void *)type->name);
968 d3dcompiler_free(type);
969 return NULL;
971 list_init(type->e.elements);
972 LIST_FOR_EACH_ENTRY(old_field, old->e.elements, struct hlsl_struct_field, entry)
974 field = d3dcompiler_alloc(sizeof(*field));
975 if (!field)
977 LIST_FOR_EACH_ENTRY_SAFE(field, old_field, type->e.elements, struct hlsl_struct_field, entry)
979 d3dcompiler_free((void *)field->semantic);
980 d3dcompiler_free((void *)field->name);
981 d3dcompiler_free(field);
983 d3dcompiler_free(type->e.elements);
984 d3dcompiler_free((void *)type->name);
985 d3dcompiler_free(type);
986 return NULL;
988 field->type = clone_hlsl_type(old_field->type);
989 field->name = d3dcompiler_strdup(old_field->name);
990 if (old_field->semantic)
991 field->semantic = d3dcompiler_strdup(old_field->semantic);
992 field->modifiers = old_field->modifiers;
993 list_add_tail(type->e.elements, &field->entry);
995 break;
996 default:
997 break;
1000 list_add_tail(&hlsl_ctx.types, &type->entry);
1001 return type;
1004 static BOOL convertible_data_type(struct hlsl_type *type)
1006 return type->type != HLSL_CLASS_OBJECT;
1009 BOOL compatible_data_types(struct hlsl_type *t1, struct hlsl_type *t2)
1011 if (!convertible_data_type(t1) || !convertible_data_type(t2))
1012 return FALSE;
1014 if (t1->type <= HLSL_CLASS_LAST_NUMERIC)
1016 /* Scalar vars can be cast to pretty much everything */
1017 if (t1->dimx == 1 && t1->dimy == 1)
1018 return TRUE;
1020 if (t1->type == HLSL_CLASS_VECTOR && t2->type == HLSL_CLASS_VECTOR)
1021 return t1->dimx >= t2->dimx;
1024 /* The other way around is true too i.e. whatever to scalar */
1025 if (t2->type <= HLSL_CLASS_LAST_NUMERIC && t2->dimx == 1 && t2->dimy == 1)
1026 return TRUE;
1028 if (t1->type == HLSL_CLASS_ARRAY)
1030 if (compare_hlsl_types(t1->e.array.type, t2))
1031 /* e.g. float4[3] to float4 is allowed */
1032 return TRUE;
1034 if (t2->type == HLSL_CLASS_ARRAY || t2->type == HLSL_CLASS_STRUCT)
1035 return components_count_type(t1) >= components_count_type(t2);
1036 else
1037 return components_count_type(t1) == components_count_type(t2);
1040 if (t1->type == HLSL_CLASS_STRUCT)
1041 return components_count_type(t1) >= components_count_type(t2);
1043 if (t2->type == HLSL_CLASS_ARRAY || t2->type == HLSL_CLASS_STRUCT)
1044 return components_count_type(t1) == components_count_type(t2);
1046 if (t1->type == HLSL_CLASS_MATRIX || t2->type == HLSL_CLASS_MATRIX)
1048 if (t1->type == HLSL_CLASS_MATRIX && t2->type == HLSL_CLASS_MATRIX && t1->dimx >= t2->dimx && t1->dimy >= t2->dimy)
1049 return TRUE;
1051 /* Matrix-vector conversion is apparently allowed if they have the same components count */
1052 if ((t1->type == HLSL_CLASS_VECTOR || t2->type == HLSL_CLASS_VECTOR)
1053 && components_count_type(t1) == components_count_type(t2))
1054 return TRUE;
1055 return FALSE;
1058 if (components_count_type(t1) >= components_count_type(t2))
1059 return TRUE;
1060 return FALSE;
1063 static BOOL implicit_compatible_data_types(struct hlsl_type *t1, struct hlsl_type *t2)
1065 if (!convertible_data_type(t1) || !convertible_data_type(t2))
1066 return FALSE;
1068 if (t1->type <= HLSL_CLASS_LAST_NUMERIC)
1070 /* Scalar vars can be converted to any other numeric data type */
1071 if (t1->dimx == 1 && t1->dimy == 1 && t2->type <= HLSL_CLASS_LAST_NUMERIC)
1072 return TRUE;
1073 /* The other way around is true too */
1074 if (t2->dimx == 1 && t2->dimy == 1 && t2->type <= HLSL_CLASS_LAST_NUMERIC)
1075 return TRUE;
1078 if (t1->type == HLSL_CLASS_ARRAY && t2->type == HLSL_CLASS_ARRAY)
1080 return components_count_type(t1) == components_count_type(t2);
1083 if ((t1->type == HLSL_CLASS_ARRAY && t2->type <= HLSL_CLASS_LAST_NUMERIC)
1084 || (t1->type <= HLSL_CLASS_LAST_NUMERIC && t2->type == HLSL_CLASS_ARRAY))
1086 /* e.g. float4[3] to float4 is allowed */
1087 if (t1->type == HLSL_CLASS_ARRAY && compare_hlsl_types(t1->e.array.type, t2))
1088 return TRUE;
1089 if (components_count_type(t1) == components_count_type(t2))
1090 return TRUE;
1091 return FALSE;
1094 if (t1->type <= HLSL_CLASS_VECTOR && t2->type <= HLSL_CLASS_VECTOR)
1096 if (t1->dimx >= t2->dimx)
1097 return TRUE;
1098 return FALSE;
1101 if (t1->type == HLSL_CLASS_MATRIX || t2->type == HLSL_CLASS_MATRIX)
1103 if (t1->type == HLSL_CLASS_MATRIX && t2->type == HLSL_CLASS_MATRIX
1104 && t1->dimx >= t2->dimx && t1->dimy >= t2->dimy)
1105 return TRUE;
1107 /* Matrix-vector conversion is apparently allowed if they have the same components count */
1108 if ((t1->type == HLSL_CLASS_VECTOR || t2->type == HLSL_CLASS_VECTOR)
1109 && components_count_type(t1) == components_count_type(t2))
1110 return TRUE;
1111 return FALSE;
1114 if (t1->type == HLSL_CLASS_STRUCT && t2->type == HLSL_CLASS_STRUCT)
1115 return compare_hlsl_types(t1, t2);
1117 return FALSE;
1120 static BOOL expr_compatible_data_types(struct hlsl_type *t1, struct hlsl_type *t2)
1122 if (t1->base_type > HLSL_TYPE_LAST_SCALAR || t2->base_type > HLSL_TYPE_LAST_SCALAR)
1123 return FALSE;
1125 /* Scalar vars can be converted to pretty much everything */
1126 if ((t1->dimx == 1 && t1->dimy == 1) || (t2->dimx == 1 && t2->dimy == 1))
1127 return TRUE;
1129 if (t1->type == HLSL_CLASS_VECTOR && t2->type == HLSL_CLASS_VECTOR)
1130 return TRUE;
1132 if (t1->type == HLSL_CLASS_MATRIX || t2->type == HLSL_CLASS_MATRIX)
1134 /* Matrix-vector conversion is apparently allowed if either they have the same components
1135 count or the matrix is nx1 or 1xn */
1136 if (t1->type == HLSL_CLASS_VECTOR || t2->type == HLSL_CLASS_VECTOR)
1138 if (components_count_type(t1) == components_count_type(t2))
1139 return TRUE;
1141 return (t1->type == HLSL_CLASS_MATRIX && (t1->dimx == 1 || t1->dimy == 1))
1142 || (t2->type == HLSL_CLASS_MATRIX && (t2->dimx == 1 || t2->dimy == 1));
1145 /* Both matrices */
1146 if ((t1->dimx >= t2->dimx && t1->dimy >= t2->dimy)
1147 || (t1->dimx <= t2->dimx && t1->dimy <= t2->dimy))
1148 return TRUE;
1151 return FALSE;
1154 static enum hlsl_base_type expr_common_base_type(enum hlsl_base_type t1, enum hlsl_base_type t2)
1156 enum hlsl_base_type types[] =
1158 HLSL_TYPE_BOOL,
1159 HLSL_TYPE_INT,
1160 HLSL_TYPE_UINT,
1161 HLSL_TYPE_HALF,
1162 HLSL_TYPE_FLOAT,
1163 HLSL_TYPE_DOUBLE,
1165 int t1_idx = -1, t2_idx = -1, i;
1167 for (i = 0; i < sizeof(types) / sizeof(types[0]); ++i)
1169 /* Always convert away from HLSL_TYPE_HALF */
1170 if (t1 == types[i])
1171 t1_idx = t1 == HLSL_TYPE_HALF ? i + 1 : i;
1172 if (t2 == types[i])
1173 t2_idx = t2 == HLSL_TYPE_HALF ? i + 1 : i;
1175 if (t1_idx != -1 && t2_idx != -1)
1176 break;
1178 if (t1_idx == -1 || t2_idx == -1)
1180 FIXME("Unexpected base type.\n");
1181 return HLSL_TYPE_FLOAT;
1183 return t1_idx >= t2_idx ? t1 : t2;
1186 static struct hlsl_type *expr_common_type(struct hlsl_type *t1, struct hlsl_type *t2,
1187 struct source_location *loc)
1189 enum hlsl_type_class type;
1190 enum hlsl_base_type base;
1191 unsigned int dimx, dimy;
1193 if (t1->type > HLSL_CLASS_LAST_NUMERIC || t2->type > HLSL_CLASS_LAST_NUMERIC)
1195 hlsl_report_message(loc->file, loc->line, loc->col, HLSL_LEVEL_ERROR,
1196 "non scalar/vector/matrix data type in expression");
1197 return NULL;
1200 if (compare_hlsl_types(t1, t2))
1201 return t1;
1203 if (!expr_compatible_data_types(t1, t2))
1205 hlsl_report_message(loc->file, loc->line, loc->col, HLSL_LEVEL_ERROR,
1206 "expression data types are incompatible");
1207 return NULL;
1210 if (t1->base_type == t2->base_type)
1211 base = t1->base_type;
1212 else
1213 base = expr_common_base_type(t1->base_type, t2->base_type);
1215 if (t1->dimx == 1 && t1->dimy == 1)
1217 type = t2->type;
1218 dimx = t2->dimx;
1219 dimy = t2->dimy;
1221 else if (t2->dimx == 1 && t2->dimy == 1)
1223 type = t1->type;
1224 dimx = t1->dimx;
1225 dimy = t1->dimy;
1227 else if (t1->type == HLSL_CLASS_MATRIX && t2->type == HLSL_CLASS_MATRIX)
1229 type = HLSL_CLASS_MATRIX;
1230 dimx = min(t1->dimx, t2->dimx);
1231 dimy = min(t1->dimy, t2->dimy);
1233 else
1235 /* Two vectors or a vector and a matrix (matrix must be 1xn or nx1) */
1236 unsigned int max_dim_1, max_dim_2;
1238 max_dim_1 = max(t1->dimx, t1->dimy);
1239 max_dim_2 = max(t2->dimx, t2->dimy);
1240 if (t1->dimx * t1->dimy == t2->dimx * t2->dimy)
1242 type = HLSL_CLASS_VECTOR;
1243 dimx = max(t1->dimx, t2->dimx);
1244 dimy = 1;
1246 else if (max_dim_1 <= max_dim_2)
1248 type = t1->type;
1249 if (type == HLSL_CLASS_VECTOR)
1251 dimx = max_dim_1;
1252 dimy = 1;
1254 else
1256 dimx = t1->dimx;
1257 dimy = t1->dimy;
1260 else
1262 type = t2->type;
1263 if (type == HLSL_CLASS_VECTOR)
1265 dimx = max_dim_2;
1266 dimy = 1;
1268 else
1270 dimx = t2->dimx;
1271 dimy = t2->dimy;
1276 return new_hlsl_type(NULL, type, base, dimx, dimy);
1279 static struct hlsl_ir_node *implicit_conversion(struct hlsl_ir_node *node, struct hlsl_type *type,
1280 struct source_location *loc)
1282 struct hlsl_ir_expr *cast;
1283 struct hlsl_ir_node *operands[3];
1285 if (compare_hlsl_types(node->data_type, type))
1286 return node;
1287 TRACE("Implicit conversion of expression to %s\n", debug_hlsl_type(type));
1288 operands[0] = node;
1289 operands[1] = operands[2] = NULL;
1290 cast = new_expr(HLSL_IR_UNOP_CAST, operands, loc);
1291 if (!cast)
1292 return NULL;
1293 cast->node.data_type = type;
1294 return &cast->node;
1297 struct hlsl_ir_expr *new_expr(enum hlsl_ir_expr_op op, struct hlsl_ir_node **operands,
1298 struct source_location *loc)
1300 struct hlsl_ir_expr *expr = d3dcompiler_alloc(sizeof(*expr));
1301 struct hlsl_type *type;
1302 unsigned int i;
1304 if (!expr)
1306 ERR("Out of memory\n");
1307 return NULL;
1309 expr->node.type = HLSL_IR_EXPR;
1310 expr->node.loc = *loc;
1311 type = operands[0]->data_type;
1312 for (i = 1; i <= 2; ++i)
1314 if (!operands[i])
1315 break;
1316 type = expr_common_type(type, operands[i]->data_type, loc);
1317 if (!type)
1319 d3dcompiler_free(expr);
1320 return NULL;
1323 for (i = 0; i <= 2; ++i)
1325 if (!operands[i])
1326 break;
1327 if (compare_hlsl_types(operands[i]->data_type, type))
1328 continue;
1329 TRACE("Implicitly converting %s into %s in an expression\n", debug_hlsl_type(operands[i]->data_type), debug_hlsl_type(type));
1330 if (operands[i]->data_type->dimx * operands[i]->data_type->dimy != 1
1331 && operands[i]->data_type->dimx * operands[i]->data_type->dimy != type->dimx * type->dimy)
1333 hlsl_report_message(operands[i]->loc.file,
1334 operands[i]->loc.line, operands[i]->loc.col, HLSL_LEVEL_WARNING,
1335 "implicit truncation of vector/matrix type");
1337 operands[i] = implicit_conversion(operands[i], type, &operands[i]->loc);
1338 if (!operands[i])
1340 ERR("Impossible to convert expression operand %u to %s\n", i + 1, debug_hlsl_type(type));
1341 d3dcompiler_free(expr);
1342 return NULL;
1345 expr->node.data_type = type;
1346 expr->op = op;
1347 expr->operands[0] = operands[0];
1348 expr->operands[1] = operands[1];
1349 expr->operands[2] = operands[2];
1351 return expr;
1354 struct hlsl_ir_expr *new_cast(struct hlsl_ir_node *node, struct hlsl_type *type,
1355 struct source_location *loc)
1357 struct hlsl_ir_expr *cast;
1358 struct hlsl_ir_node *operands[3];
1360 operands[0] = node;
1361 operands[1] = operands[2] = NULL;
1362 cast = new_expr(HLSL_IR_UNOP_CAST, operands, loc);
1363 if (cast)
1364 cast->node.data_type = type;
1365 return cast;
1368 struct hlsl_ir_expr *hlsl_mul(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1369 struct source_location *loc)
1371 struct hlsl_ir_expr *expr;
1372 struct hlsl_ir_node *ops[3];
1374 ops[0] = op1;
1375 ops[1] = op2;
1376 ops[2] = NULL;
1377 expr = new_expr(HLSL_IR_BINOP_MUL, ops, loc);
1378 return expr;
1381 struct hlsl_ir_expr *hlsl_div(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1382 struct source_location *loc)
1384 struct hlsl_ir_expr *expr;
1385 struct hlsl_ir_node *ops[3];
1387 ops[0] = op1;
1388 ops[1] = op2;
1389 ops[2] = NULL;
1390 expr = new_expr(HLSL_IR_BINOP_DIV, ops, loc);
1391 return expr;
1394 struct hlsl_ir_expr *hlsl_mod(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1395 struct source_location *loc)
1397 struct hlsl_ir_expr *expr;
1398 struct hlsl_ir_node *ops[3];
1400 ops[0] = op1;
1401 ops[1] = op2;
1402 ops[2] = NULL;
1403 expr = new_expr(HLSL_IR_BINOP_MOD, ops, loc);
1404 return expr;
1407 struct hlsl_ir_expr *hlsl_add(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1408 struct source_location *loc)
1410 struct hlsl_ir_expr *expr;
1411 struct hlsl_ir_node *ops[3];
1413 ops[0] = op1;
1414 ops[1] = op2;
1415 ops[2] = NULL;
1416 expr = new_expr(HLSL_IR_BINOP_ADD, ops, loc);
1417 return expr;
1420 struct hlsl_ir_expr *hlsl_sub(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1421 struct source_location *loc)
1423 struct hlsl_ir_expr *expr;
1424 struct hlsl_ir_node *ops[3];
1426 ops[0] = op1;
1427 ops[1] = op2;
1428 ops[2] = NULL;
1429 expr = new_expr(HLSL_IR_BINOP_SUB, ops, loc);
1430 return expr;
1433 struct hlsl_ir_expr *hlsl_lt(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1434 struct source_location *loc)
1436 struct hlsl_ir_expr *expr;
1437 struct hlsl_ir_node *ops[3];
1439 ops[0] = op1;
1440 ops[1] = op2;
1441 ops[2] = NULL;
1442 expr = new_expr(HLSL_IR_BINOP_LESS, ops, loc);
1443 return expr;
1446 struct hlsl_ir_expr *hlsl_gt(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1447 struct source_location *loc)
1449 struct hlsl_ir_expr *expr;
1450 struct hlsl_ir_node *ops[3];
1452 ops[0] = op1;
1453 ops[1] = op2;
1454 ops[2] = NULL;
1455 expr = new_expr(HLSL_IR_BINOP_GREATER, ops, loc);
1456 return expr;
1459 struct hlsl_ir_expr *hlsl_le(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1460 struct source_location *loc)
1462 struct hlsl_ir_expr *expr;
1463 struct hlsl_ir_node *ops[3];
1465 ops[0] = op1;
1466 ops[1] = op2;
1467 ops[2] = NULL;
1468 expr = new_expr(HLSL_IR_BINOP_LEQUAL, ops, loc);
1469 return expr;
1472 struct hlsl_ir_expr *hlsl_ge(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1473 struct source_location *loc)
1475 struct hlsl_ir_expr *expr;
1476 struct hlsl_ir_node *ops[3];
1478 ops[0] = op1;
1479 ops[1] = op2;
1480 ops[2] = NULL;
1481 expr = new_expr(HLSL_IR_BINOP_GEQUAL, ops, loc);
1482 return expr;
1485 struct hlsl_ir_expr *hlsl_eq(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1486 struct source_location *loc)
1488 struct hlsl_ir_expr *expr;
1489 struct hlsl_ir_node *ops[3];
1491 ops[0] = op1;
1492 ops[1] = op2;
1493 ops[2] = NULL;
1494 expr = new_expr(HLSL_IR_BINOP_EQUAL, ops, loc);
1495 return expr;
1498 struct hlsl_ir_expr *hlsl_ne(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1499 struct source_location *loc)
1501 struct hlsl_ir_expr *expr;
1502 struct hlsl_ir_node *ops[3];
1504 ops[0] = op1;
1505 ops[1] = op2;
1506 ops[2] = NULL;
1507 expr = new_expr(HLSL_IR_BINOP_NEQUAL, ops, loc);
1508 return expr;
1511 struct hlsl_ir_deref *new_var_deref(struct hlsl_ir_var *var)
1513 struct hlsl_ir_deref *deref = d3dcompiler_alloc(sizeof(*deref));
1515 if (!deref)
1517 ERR("Out of memory.\n");
1518 return NULL;
1520 deref->node.type = HLSL_IR_DEREF;
1521 deref->node.data_type = var->node.data_type;
1522 deref->type = HLSL_IR_DEREF_VAR;
1523 deref->v.var = var;
1524 return deref;
1527 struct hlsl_ir_deref *new_record_deref(struct hlsl_ir_node *record, struct hlsl_struct_field *field)
1529 struct hlsl_ir_deref *deref = d3dcompiler_alloc(sizeof(*deref));
1531 if (!deref)
1533 ERR("Out of memory.\n");
1534 return NULL;
1536 deref->node.type = HLSL_IR_DEREF;
1537 deref->node.data_type = field->type;
1538 deref->type = HLSL_IR_DEREF_RECORD;
1539 if (record->type == HLSL_IR_VAR)
1540 deref->v.record.record = &new_var_deref(var_from_node(record))->node;
1541 else
1542 deref->v.record.record = record;
1543 deref->v.record.field = field;
1544 return deref;
1547 static enum hlsl_ir_expr_op op_from_assignment(enum parse_assign_op op)
1549 static const enum hlsl_ir_expr_op ops[] =
1552 HLSL_IR_BINOP_ADD,
1553 HLSL_IR_BINOP_SUB,
1554 HLSL_IR_BINOP_MUL,
1555 HLSL_IR_BINOP_DIV,
1556 HLSL_IR_BINOP_MOD,
1557 HLSL_IR_BINOP_LSHIFT,
1558 HLSL_IR_BINOP_RSHIFT,
1559 HLSL_IR_BINOP_BIT_AND,
1560 HLSL_IR_BINOP_BIT_OR,
1561 HLSL_IR_BINOP_BIT_XOR,
1564 return ops[op];
1567 struct hlsl_ir_node *make_assignment(struct hlsl_ir_node *left, enum parse_assign_op assign_op,
1568 DWORD writemask, struct hlsl_ir_node *right)
1570 struct hlsl_ir_expr *expr;
1571 struct hlsl_ir_assignment *assign = d3dcompiler_alloc(sizeof(*assign));
1572 struct hlsl_type *type;
1573 struct hlsl_ir_node *lhs, *rhs;
1575 if (!assign)
1577 ERR("Out of memory\n");
1578 return NULL;
1581 TRACE("Creating proper assignment expression.\n");
1582 rhs = right;
1583 if (writemask == BWRITERSP_WRITEMASK_ALL)
1584 type = left->data_type;
1585 else
1587 FIXME("Assignments with writemasks not supported yet.\n");
1588 type = NULL;
1590 assign->node.type = HLSL_IR_ASSIGNMENT;
1591 assign->node.loc = left->loc;
1592 assign->node.data_type = type;
1593 assign->writemask = writemask;
1594 FIXME("Check for casts in the lhs.\n");
1596 lhs = left;
1597 if (lhs->type == HLSL_IR_VAR)
1599 struct hlsl_ir_deref *lhs_deref = new_var_deref(var_from_node(lhs));
1600 lhs = &lhs_deref->node;
1602 /* FIXME: check for invalid writemasks on the lhs. */
1604 if (!compare_hlsl_types(type, rhs->data_type))
1606 struct hlsl_ir_node *converted_rhs;
1608 if (!implicit_compatible_data_types(rhs->data_type, type))
1610 hlsl_report_message(rhs->loc.file, rhs->loc.line, rhs->loc.col, HLSL_LEVEL_ERROR,
1611 "can't implicitly convert %s to %s",
1612 debug_hlsl_type(rhs->data_type), debug_hlsl_type(type));
1613 free_instr(lhs);
1614 free_instr(rhs);
1615 d3dcompiler_free(assign);
1616 return NULL;
1618 if (lhs->data_type->dimx * lhs->data_type->dimy < rhs->data_type->dimx * rhs->data_type->dimy)
1619 hlsl_report_message(rhs->loc.file, rhs->loc.line, rhs->loc.col, HLSL_LEVEL_WARNING,
1620 "implicit truncation of vector type");
1622 converted_rhs = implicit_conversion(rhs, type, &rhs->loc);
1623 if (!converted_rhs)
1625 ERR("Couldn't implicitly convert expression to %s.\n", debug_hlsl_type(type));
1626 free_instr(lhs);
1627 free_instr(rhs);
1628 d3dcompiler_free(assign);
1629 return NULL;
1631 rhs = converted_rhs;
1634 assign->lhs = lhs;
1635 if (assign_op != ASSIGN_OP_ASSIGN)
1637 struct hlsl_ir_node *operands[3];
1638 enum hlsl_ir_expr_op op = op_from_assignment(assign_op);
1640 if (lhs->type != HLSL_IR_DEREF || deref_from_node(lhs)->type != HLSL_IR_DEREF_VAR)
1642 FIXME("LHS expression not supported in compound assignments yet.\n");
1643 assign->rhs = rhs;
1645 else
1647 struct hlsl_ir_deref *lhs_deref = deref_from_node(lhs), *new_deref;
1649 TRACE("Adding an expression for the compound assignment.\n");
1650 new_deref = new_var_deref(lhs_deref->v.var);
1651 operands[0] = &new_deref->node;
1652 operands[1] = rhs;
1653 operands[2] = NULL;
1654 expr = new_expr(op, operands, &left->loc);
1655 assign->rhs = &expr->node;
1658 else
1659 assign->rhs = rhs;
1661 return &assign->node;
1664 static int compare_hlsl_types_rb(const void *key, const struct wine_rb_entry *entry)
1666 const char *name = key;
1667 const struct hlsl_type *type = WINE_RB_ENTRY_VALUE(entry, const struct hlsl_type, scope_entry);
1669 if (name == type->name)
1670 return 0;
1672 if (!name || !type->name)
1674 ERR("hlsl_type without a name in a scope?\n");
1675 return -1;
1677 return strcmp(name, type->name);
1680 static inline void *d3dcompiler_alloc_rb(size_t size)
1682 return d3dcompiler_alloc(size);
1685 static inline void *d3dcompiler_realloc_rb(void *ptr, size_t size)
1687 return d3dcompiler_realloc(ptr, size);
1690 static inline void d3dcompiler_free_rb(void *ptr)
1692 d3dcompiler_free(ptr);
1694 static const struct wine_rb_functions hlsl_type_rb_funcs =
1696 d3dcompiler_alloc_rb,
1697 d3dcompiler_realloc_rb,
1698 d3dcompiler_free_rb,
1699 compare_hlsl_types_rb,
1702 void push_scope(struct hlsl_parse_ctx *ctx)
1704 struct hlsl_scope *new_scope = d3dcompiler_alloc(sizeof(*new_scope));
1706 if (!new_scope)
1708 ERR("Out of memory!\n");
1709 return;
1711 TRACE("Pushing a new scope\n");
1712 list_init(&new_scope->vars);
1713 if (wine_rb_init(&new_scope->types, &hlsl_type_rb_funcs) == -1)
1715 ERR("Failed to initialize types rbtree.\n");
1716 d3dcompiler_free(new_scope);
1717 return;
1719 new_scope->upper = ctx->cur_scope;
1720 ctx->cur_scope = new_scope;
1721 list_add_tail(&ctx->scopes, &new_scope->entry);
1724 BOOL pop_scope(struct hlsl_parse_ctx *ctx)
1726 struct hlsl_scope *prev_scope = ctx->cur_scope->upper;
1727 if (!prev_scope)
1728 return FALSE;
1730 TRACE("Popping current scope\n");
1731 ctx->cur_scope = prev_scope;
1732 return TRUE;
1735 struct hlsl_ir_function_decl *new_func_decl(struct hlsl_type *return_type, struct list *parameters)
1737 struct hlsl_ir_function_decl *decl;
1739 decl = d3dcompiler_alloc(sizeof(*decl));
1740 if (!decl)
1742 ERR("Out of memory.\n");
1743 return NULL;
1745 decl->node.type = HLSL_IR_FUNCTION_DECL;
1746 decl->node.data_type = return_type;
1747 decl->parameters = parameters;
1749 return decl;
1752 static int compare_param_hlsl_types(const struct hlsl_type *t1, const struct hlsl_type *t2)
1754 if (t1->type != t2->type)
1756 if (!((t1->type == HLSL_CLASS_SCALAR && t2->type == HLSL_CLASS_VECTOR)
1757 || (t1->type == HLSL_CLASS_VECTOR && t2->type == HLSL_CLASS_SCALAR)))
1758 return t1->type - t2->type;
1760 if (t1->base_type != t2->base_type)
1761 return t1->base_type - t2->base_type;
1762 if (t1->base_type == HLSL_TYPE_SAMPLER && t1->sampler_dim != t2->sampler_dim)
1763 return t1->sampler_dim - t2->sampler_dim;
1764 if (t1->dimx != t2->dimx)
1765 return t1->dimx - t2->dimx;
1766 if (t1->dimy != t2->dimy)
1767 return t1->dimx - t2->dimx;
1768 if (t1->type == HLSL_CLASS_STRUCT)
1770 struct list *t1cur, *t2cur;
1771 struct hlsl_struct_field *t1field, *t2field;
1772 int r;
1774 t1cur = list_head(t1->e.elements);
1775 t2cur = list_head(t2->e.elements);
1776 while (t1cur && t2cur)
1778 t1field = LIST_ENTRY(t1cur, struct hlsl_struct_field, entry);
1779 t2field = LIST_ENTRY(t2cur, struct hlsl_struct_field, entry);
1780 if ((r = compare_param_hlsl_types(t1field->type, t2field->type)))
1781 return r;
1782 if ((r = strcmp(t1field->name, t2field->name)))
1783 return r;
1784 t1cur = list_next(t1->e.elements, t1cur);
1785 t2cur = list_next(t2->e.elements, t2cur);
1787 if (t1cur != t2cur)
1788 return t1cur ? 1 : -1;
1789 return 0;
1791 if (t1->type == HLSL_CLASS_ARRAY)
1793 if (t1->e.array.elements_count != t2->e.array.elements_count)
1794 return t1->e.array.elements_count - t2->e.array.elements_count;
1795 return compare_param_hlsl_types(t1->e.array.type, t2->e.array.type);
1798 return 0;
1801 static int compare_function_decl_rb(const void *key, const struct wine_rb_entry *entry)
1803 const struct list *params = key;
1804 const struct hlsl_ir_function_decl *decl = WINE_RB_ENTRY_VALUE(entry, const struct hlsl_ir_function_decl, entry);
1805 int params_count = params ? list_count(params) : 0;
1806 int decl_params_count = decl->parameters ? list_count(decl->parameters) : 0;
1807 int r;
1808 struct list *p1cur, *p2cur;
1810 if (params_count != decl_params_count)
1811 return params_count - decl_params_count;
1813 p1cur = params ? list_head(params) : NULL;
1814 p2cur = decl->parameters ? list_head(decl->parameters) : NULL;
1815 while (p1cur && p2cur)
1817 struct hlsl_ir_var *p1, *p2;
1818 p1 = LIST_ENTRY(p1cur, struct hlsl_ir_var, node.entry);
1819 p2 = LIST_ENTRY(p2cur, struct hlsl_ir_var, node.entry);
1820 if ((r = compare_param_hlsl_types(p1->node.data_type, p2->node.data_type)))
1821 return r;
1822 p1cur = list_next(params, p1cur);
1823 p2cur = list_next(decl->parameters, p2cur);
1825 return 0;
1828 static const struct wine_rb_functions hlsl_ir_function_decl_rb_funcs =
1830 d3dcompiler_alloc_rb,
1831 d3dcompiler_realloc_rb,
1832 d3dcompiler_free_rb,
1833 compare_function_decl_rb,
1836 static int compare_function_rb(const void *key, const struct wine_rb_entry *entry)
1838 const char *name = key;
1839 const struct hlsl_ir_function *func = WINE_RB_ENTRY_VALUE(entry, const struct hlsl_ir_function,entry);
1841 return strcmp(name, func->name);
1844 static const struct wine_rb_functions function_rb_funcs =
1846 d3dcompiler_alloc_rb,
1847 d3dcompiler_realloc_rb,
1848 d3dcompiler_free_rb,
1849 compare_function_rb,
1852 void init_functions_tree(struct wine_rb_tree *funcs)
1854 if (wine_rb_init(&hlsl_ctx.functions, &function_rb_funcs) == -1)
1855 ERR("Failed to initialize functions rbtree.\n");
1858 static const char *debug_base_type(const struct hlsl_type *type)
1860 const char *name = "(unknown)";
1862 switch (type->base_type)
1864 case HLSL_TYPE_FLOAT: name = "float"; break;
1865 case HLSL_TYPE_HALF: name = "half"; break;
1866 case HLSL_TYPE_DOUBLE: name = "double"; break;
1867 case HLSL_TYPE_INT: name = "int"; break;
1868 case HLSL_TYPE_UINT: name = "uint"; break;
1869 case HLSL_TYPE_BOOL: name = "bool"; break;
1870 case HLSL_TYPE_SAMPLER:
1871 switch (type->sampler_dim)
1873 case HLSL_SAMPLER_DIM_GENERIC: name = "sampler"; break;
1874 case HLSL_SAMPLER_DIM_1D: name = "sampler1D"; break;
1875 case HLSL_SAMPLER_DIM_2D: name = "sampler2D"; break;
1876 case HLSL_SAMPLER_DIM_3D: name = "sampler3D"; break;
1877 case HLSL_SAMPLER_DIM_CUBE: name = "samplerCUBE"; break;
1879 break;
1880 default:
1881 FIXME("Unhandled case %u\n", type->base_type);
1883 return name;
1886 const char *debug_hlsl_type(const struct hlsl_type *type)
1888 const char *name;
1890 if (type->name)
1891 return debugstr_a(type->name);
1893 if (type->type == HLSL_CLASS_STRUCT)
1894 return "<anonymous struct>";
1896 if (type->type == HLSL_CLASS_ARRAY)
1898 name = debug_base_type(type->e.array.type);
1899 return wine_dbg_sprintf("%s[%u]", name, type->e.array.elements_count);
1902 name = debug_base_type(type);
1904 if (type->type == HLSL_CLASS_SCALAR)
1905 return wine_dbg_sprintf("%s", name);
1906 if (type->type == HLSL_CLASS_VECTOR)
1907 return wine_dbg_sprintf("%s%u", name, type->dimx);
1908 if (type->type == HLSL_CLASS_MATRIX)
1909 return wine_dbg_sprintf("%s%ux%u", name, type->dimx, type->dimy);
1910 return "unexpected_type";
1913 const char *debug_modifiers(DWORD modifiers)
1915 char string[110];
1917 string[0] = 0;
1918 if (modifiers & HLSL_STORAGE_EXTERN)
1919 strcat(string, " extern"); /* 7 */
1920 if (modifiers & HLSL_STORAGE_NOINTERPOLATION)
1921 strcat(string, " nointerpolation"); /* 16 */
1922 if (modifiers & HLSL_MODIFIER_PRECISE)
1923 strcat(string, " precise"); /* 8 */
1924 if (modifiers & HLSL_STORAGE_SHARED)
1925 strcat(string, " shared"); /* 7 */
1926 if (modifiers & HLSL_STORAGE_GROUPSHARED)
1927 strcat(string, " groupshared"); /* 12 */
1928 if (modifiers & HLSL_STORAGE_STATIC)
1929 strcat(string, " static"); /* 7 */
1930 if (modifiers & HLSL_STORAGE_UNIFORM)
1931 strcat(string, " uniform"); /* 8 */
1932 if (modifiers & HLSL_STORAGE_VOLATILE)
1933 strcat(string, " volatile"); /* 9 */
1934 if (modifiers & HLSL_MODIFIER_CONST)
1935 strcat(string, " const"); /* 6 */
1936 if (modifiers & HLSL_MODIFIER_ROW_MAJOR)
1937 strcat(string, " row_major"); /* 10 */
1938 if (modifiers & HLSL_MODIFIER_COLUMN_MAJOR)
1939 strcat(string, " column_major"); /* 13 */
1940 if ((modifiers & (HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT)) == (HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT))
1941 strcat(string, " inout"); /* 6 */
1942 else if (modifiers & HLSL_MODIFIER_IN)
1943 strcat(string, " in"); /* 3 */
1944 else if (modifiers & HLSL_MODIFIER_OUT)
1945 strcat(string, " out"); /* 4 */
1947 return wine_dbg_sprintf("%s", string[0] ? string + 1 : "");
1950 static const char *debug_node_type(enum hlsl_ir_node_type type)
1952 static const char * const names[] =
1954 "HLSL_IR_VAR",
1955 "HLSL_IR_ASSIGNMENT",
1956 "HLSL_IR_CONSTANT",
1957 "HLSL_IR_CONSTRUCTOR",
1958 "HLSL_IR_DEREF",
1959 "HLSL_IR_EXPR",
1960 "HLSL_IR_FUNCTION_DECL",
1961 "HLSL_IR_IF",
1962 "HLSL_IR_JUMP",
1963 "HLSL_IR_SWIZZLE",
1966 if (type >= sizeof(names) / sizeof(names[0]))
1967 return "Unexpected node type";
1968 return names[type];
1971 static void debug_dump_instr(const struct hlsl_ir_node *instr);
1973 static void debug_dump_instr_list(const struct list *list)
1975 struct hlsl_ir_node *instr;
1977 LIST_FOR_EACH_ENTRY(instr, list, struct hlsl_ir_node, entry)
1979 debug_dump_instr(instr);
1980 TRACE("\n");
1984 static void debug_dump_ir_var(const struct hlsl_ir_var *var)
1986 if (var->modifiers)
1987 TRACE("%s ", debug_modifiers(var->modifiers));
1988 TRACE("%s %s", debug_hlsl_type(var->node.data_type), var->name);
1989 if (var->semantic)
1990 TRACE(" : %s", debugstr_a(var->semantic));
1993 static void debug_dump_ir_deref(const struct hlsl_ir_deref *deref)
1995 switch (deref->type)
1997 case HLSL_IR_DEREF_VAR:
1998 TRACE("deref(");
1999 debug_dump_ir_var(deref->v.var);
2000 TRACE(")");
2001 break;
2002 case HLSL_IR_DEREF_ARRAY:
2003 debug_dump_instr(deref->v.array.array);
2004 TRACE("[");
2005 debug_dump_instr(deref->v.array.index);
2006 TRACE("]");
2007 break;
2008 case HLSL_IR_DEREF_RECORD:
2009 debug_dump_instr(deref->v.record.record);
2010 TRACE(".%s", debugstr_a(deref->v.record.field->name));
2011 break;
2015 static void debug_dump_ir_constant(const struct hlsl_ir_constant *constant)
2017 struct hlsl_type *type = constant->node.data_type;
2018 unsigned int x, y;
2020 if (type->dimy != 1)
2021 TRACE("{");
2022 for (y = 0; y < type->dimy; ++y)
2024 if (type->dimx != 1)
2025 TRACE("{");
2026 for (x = 0; x < type->dimx; ++x)
2028 switch (type->base_type)
2030 case HLSL_TYPE_FLOAT:
2031 TRACE("%g ", (double)constant->v.value.f[y * type->dimx + x]);
2032 break;
2033 case HLSL_TYPE_DOUBLE:
2034 TRACE("%g ", constant->v.value.d[y * type->dimx + x]);
2035 break;
2036 case HLSL_TYPE_INT:
2037 TRACE("%d ", constant->v.value.i[y * type->dimx + x]);
2038 break;
2039 case HLSL_TYPE_UINT:
2040 TRACE("%u ", constant->v.value.u[y * type->dimx + x]);
2041 break;
2042 case HLSL_TYPE_BOOL:
2043 TRACE("%s ", constant->v.value.b[y * type->dimx + x] == FALSE ? "false" : "true");
2044 break;
2045 default:
2046 TRACE("Constants of type %s not supported\n", debug_base_type(type));
2049 if (type->dimx != 1)
2050 TRACE("}");
2052 if (type->dimy != 1)
2053 TRACE("}");
2056 static const char *debug_expr_op(const struct hlsl_ir_expr *expr)
2058 static const char * const op_names[] =
2060 "~",
2061 "!",
2062 "-",
2063 "abs",
2064 "sign",
2065 "rcp",
2066 "rsq",
2067 "sqrt",
2068 "nrm",
2069 "exp2",
2070 "log2",
2072 "cast",
2074 "fract",
2076 "sin",
2077 "cos",
2078 "sin_reduced",
2079 "cos_reduced",
2081 "dsx",
2082 "dsy",
2084 "sat",
2086 "pre++",
2087 "pre--",
2088 "post++",
2089 "post--",
2091 "+",
2092 "-",
2093 "*",
2094 "/",
2096 "%",
2098 "<",
2099 ">",
2100 "<=",
2101 ">=",
2102 "==",
2103 "!=",
2105 "&&",
2106 "||",
2108 "<<",
2109 ">>",
2110 "&",
2111 "|",
2112 "^",
2114 "dot",
2115 "crs",
2116 "min",
2117 "max",
2119 "pow",
2121 "lerp",
2123 ",",
2126 if (expr->op == HLSL_IR_UNOP_CAST)
2127 return debug_hlsl_type(expr->node.data_type);
2129 return op_names[expr->op];
2132 /* Dumps the expression in a prefix "operator (operands)" form */
2133 static void debug_dump_ir_expr(const struct hlsl_ir_expr *expr)
2135 unsigned int i;
2137 TRACE("%s (", debug_expr_op(expr));
2138 for (i = 0; i < 3 && expr->operands[i]; ++i)
2140 debug_dump_instr(expr->operands[i]);
2141 TRACE(" ");
2143 TRACE(")");
2146 static void debug_dump_ir_constructor(const struct hlsl_ir_constructor *constructor)
2148 struct hlsl_ir_node *arg;
2150 TRACE("%s (", debug_hlsl_type(constructor->node.data_type));
2151 LIST_FOR_EACH_ENTRY(arg, constructor->arguments, struct hlsl_ir_node, entry)
2153 debug_dump_instr(arg);
2154 TRACE(" ");
2156 TRACE(")");
2159 static const char *debug_writemask(DWORD writemask)
2161 char string[5], components[] = {'x', 'y', 'z', 'w'};
2162 unsigned int i = 0, pos = 0;
2164 while (writemask)
2166 if (writemask & 1)
2167 string[pos++] = components[i];
2168 writemask >>= 1;
2169 i++;
2171 string[pos] = '\0';
2172 return wine_dbg_sprintf(".%s", string);
2175 static void debug_dump_ir_assignment(const struct hlsl_ir_assignment *assign)
2177 TRACE("= (");
2178 debug_dump_instr(assign->lhs);
2179 if (assign->writemask != BWRITERSP_WRITEMASK_ALL)
2180 TRACE("%s", debug_writemask(assign->writemask));
2181 TRACE(" ");
2182 debug_dump_instr(assign->rhs);
2183 TRACE(")");
2186 static void debug_dump_ir_swizzle(const struct hlsl_ir_swizzle *swizzle)
2188 unsigned int i;
2190 debug_dump_instr(swizzle->val);
2191 TRACE(".");
2192 if (swizzle->val->data_type->dimy > 1)
2194 for (i = 0; i < swizzle->node.data_type->dimx; ++i)
2195 TRACE("_m%u%u", (swizzle->swizzle >> i * 8) & 0xf, (swizzle->swizzle >> (i * 8 + 4)) & 0xf);
2197 else
2199 char c[] = {'x', 'y', 'z', 'w'};
2201 for (i = 0; i < swizzle->node.data_type->dimx; ++i)
2202 TRACE("%c", c[(swizzle->swizzle >> i * 2) & 0x3]);
2206 static void debug_dump_ir_jump(const struct hlsl_ir_jump *jump)
2208 switch (jump->type)
2210 case HLSL_IR_JUMP_BREAK:
2211 TRACE("break");
2212 break;
2213 case HLSL_IR_JUMP_CONTINUE:
2214 TRACE("continue");
2215 break;
2216 case HLSL_IR_JUMP_DISCARD:
2217 TRACE("discard");
2218 break;
2219 case HLSL_IR_JUMP_RETURN:
2220 TRACE("return ");
2221 if (jump->return_value)
2222 debug_dump_instr(jump->return_value);
2223 TRACE(";");
2224 break;
2228 static void debug_dump_ir_if(const struct hlsl_ir_if *if_node)
2230 TRACE("if (");
2231 debug_dump_instr(if_node->condition);
2232 TRACE(")\n{\n");
2233 debug_dump_instr_list(if_node->then_instrs);
2234 TRACE("}\n");
2235 if (if_node->else_instrs)
2237 TRACE("else\n{\n");
2238 debug_dump_instr_list(if_node->else_instrs);
2239 TRACE("}\n");
2243 static void debug_dump_instr(const struct hlsl_ir_node *instr)
2245 switch (instr->type)
2247 case HLSL_IR_EXPR:
2248 debug_dump_ir_expr(expr_from_node(instr));
2249 break;
2250 case HLSL_IR_DEREF:
2251 debug_dump_ir_deref(deref_from_node(instr));
2252 break;
2253 case HLSL_IR_CONSTANT:
2254 debug_dump_ir_constant(constant_from_node(instr));
2255 break;
2256 case HLSL_IR_ASSIGNMENT:
2257 debug_dump_ir_assignment(assignment_from_node(instr));
2258 break;
2259 case HLSL_IR_SWIZZLE:
2260 debug_dump_ir_swizzle(swizzle_from_node(instr));
2261 break;
2262 case HLSL_IR_CONSTRUCTOR:
2263 debug_dump_ir_constructor(constructor_from_node(instr));
2264 break;
2265 case HLSL_IR_JUMP:
2266 debug_dump_ir_jump(jump_from_node(instr));
2267 break;
2268 case HLSL_IR_IF:
2269 debug_dump_ir_if(if_from_node(instr));
2270 break;
2271 default:
2272 TRACE("<No dump function for %s>", debug_node_type(instr->type));
2276 void debug_dump_ir_function_decl(const struct hlsl_ir_function_decl *func)
2278 struct hlsl_ir_var *param;
2280 TRACE("Dumping function %s.\n", debugstr_a(func->func->name));
2281 TRACE("Function parameters:\n");
2282 LIST_FOR_EACH_ENTRY(param, func->parameters, struct hlsl_ir_var, node.entry)
2284 debug_dump_ir_var(param);
2285 TRACE("\n");
2287 if (func->semantic)
2288 TRACE("Function semantic: %s\n", debugstr_a(func->semantic));
2289 if (func->body)
2291 debug_dump_instr_list(func->body);
2295 void free_hlsl_type(struct hlsl_type *type)
2297 struct hlsl_struct_field *field, *next_field;
2299 d3dcompiler_free((void *)type->name);
2300 if (type->type == HLSL_CLASS_STRUCT)
2302 LIST_FOR_EACH_ENTRY_SAFE(field, next_field, type->e.elements, struct hlsl_struct_field, entry)
2304 d3dcompiler_free((void *)field->name);
2305 d3dcompiler_free((void *)field->semantic);
2306 d3dcompiler_free(field);
2309 d3dcompiler_free(type);
2312 void free_instr_list(struct list *list)
2314 struct hlsl_ir_node *node, *next_node;
2316 if (!list)
2317 return;
2318 LIST_FOR_EACH_ENTRY_SAFE(node, next_node, list, struct hlsl_ir_node, entry)
2319 free_instr(node);
2320 d3dcompiler_free(list);
2323 static void free_ir_constant(struct hlsl_ir_constant *constant)
2325 struct hlsl_type *type = constant->node.data_type;
2326 unsigned int i;
2327 struct hlsl_ir_constant *field, *next_field;
2329 switch (type->type)
2331 case HLSL_CLASS_ARRAY:
2332 for (i = 0; i < type->e.array.elements_count; ++i)
2333 free_ir_constant(&constant->v.array_elements[i]);
2334 d3dcompiler_free(constant->v.array_elements);
2335 break;
2336 case HLSL_CLASS_STRUCT:
2337 LIST_FOR_EACH_ENTRY_SAFE(field, next_field, constant->v.struct_elements, struct hlsl_ir_constant, node.entry)
2338 free_ir_constant(field);
2339 break;
2340 default:
2341 break;
2343 d3dcompiler_free(constant);
2346 static void free_ir_deref(struct hlsl_ir_deref *deref)
2348 switch (deref->type)
2350 case HLSL_IR_DEREF_VAR:
2351 /* Variables are shared among nodes in the tree. */
2352 break;
2353 case HLSL_IR_DEREF_ARRAY:
2354 free_instr(deref->v.array.array);
2355 free_instr(deref->v.array.index);
2356 break;
2357 case HLSL_IR_DEREF_RECORD:
2358 free_instr(deref->v.record.record);
2359 break;
2361 d3dcompiler_free(deref);
2364 static void free_ir_swizzle(struct hlsl_ir_swizzle *swizzle)
2366 free_instr(swizzle->val);
2367 d3dcompiler_free(swizzle);
2370 static void free_ir_constructor(struct hlsl_ir_constructor *constructor)
2372 free_instr_list(constructor->arguments);
2373 d3dcompiler_free(constructor);
2376 static void free_ir_expr(struct hlsl_ir_expr *expr)
2378 unsigned int i;
2380 for (i = 0; i < 3; ++i)
2382 if (!expr->operands[i])
2383 break;
2384 free_instr(expr->operands[i]);
2386 free_instr_list(expr->subexpressions);
2387 d3dcompiler_free(expr);
2390 static void free_ir_assignment(struct hlsl_ir_assignment *assignment)
2392 free_instr(assignment->lhs);
2393 free_instr(assignment->rhs);
2394 d3dcompiler_free(assignment);
2397 static void free_ir_if(struct hlsl_ir_if *if_node)
2399 free_instr(if_node->condition);
2400 free_instr_list(if_node->then_instrs);
2401 free_instr_list(if_node->else_instrs);
2402 d3dcompiler_free(if_node);
2405 static void free_ir_jump(struct hlsl_ir_jump *jump)
2407 if (jump->type == HLSL_IR_JUMP_RETURN)
2408 free_instr(jump->return_value);
2409 d3dcompiler_free(jump);
2412 void free_instr(struct hlsl_ir_node *node)
2414 switch (node->type)
2416 case HLSL_IR_VAR:
2417 /* These are freed later on from the scopes. */
2418 break;
2419 case HLSL_IR_CONSTANT:
2420 free_ir_constant(constant_from_node(node));
2421 break;
2422 case HLSL_IR_DEREF:
2423 free_ir_deref(deref_from_node(node));
2424 break;
2425 case HLSL_IR_SWIZZLE:
2426 free_ir_swizzle(swizzle_from_node(node));
2427 break;
2428 case HLSL_IR_CONSTRUCTOR:
2429 free_ir_constructor(constructor_from_node(node));
2430 break;
2431 case HLSL_IR_EXPR:
2432 free_ir_expr(expr_from_node(node));
2433 break;
2434 case HLSL_IR_ASSIGNMENT:
2435 free_ir_assignment(assignment_from_node(node));
2436 break;
2437 case HLSL_IR_IF:
2438 free_ir_if(if_from_node(node));
2439 break;
2440 case HLSL_IR_JUMP:
2441 free_ir_jump(jump_from_node(node));
2442 break;
2443 default:
2444 FIXME("Unsupported node type %s\n", debug_node_type(node->type));
2448 static void free_function_decl(struct hlsl_ir_function_decl *decl)
2450 d3dcompiler_free((void *)decl->semantic);
2451 d3dcompiler_free(decl->parameters);
2452 free_instr_list(decl->body);
2453 d3dcompiler_free(decl);
2456 static void free_function_decl_rb(struct wine_rb_entry *entry, void *context)
2458 free_function_decl(WINE_RB_ENTRY_VALUE(entry, struct hlsl_ir_function_decl, entry));
2461 static void free_function(struct hlsl_ir_function *func)
2463 wine_rb_destroy(&func->overloads, free_function_decl_rb, NULL);
2464 d3dcompiler_free((void *)func->name);
2465 d3dcompiler_free(func);
2468 void free_function_rb(struct wine_rb_entry *entry, void *context)
2470 free_function(WINE_RB_ENTRY_VALUE(entry, struct hlsl_ir_function, entry));
2473 void add_function_decl(struct wine_rb_tree *funcs, char *name, struct hlsl_ir_function_decl *decl, BOOL intrinsic)
2475 struct hlsl_ir_function *func;
2476 struct wine_rb_entry *func_entry, *old_entry;
2478 func_entry = wine_rb_get(funcs, name);
2479 if (func_entry)
2481 func = WINE_RB_ENTRY_VALUE(func_entry, struct hlsl_ir_function, entry);
2482 decl->func = func;
2483 if ((old_entry = wine_rb_get(&func->overloads, decl->parameters)))
2485 struct hlsl_ir_function_decl *old_decl =
2486 WINE_RB_ENTRY_VALUE(old_entry, struct hlsl_ir_function_decl, entry);
2488 if (!decl->body)
2490 free_function_decl(decl);
2491 d3dcompiler_free(name);
2492 return;
2494 wine_rb_remove(&func->overloads, decl->parameters);
2495 free_function_decl(old_decl);
2497 wine_rb_put(&func->overloads, decl->parameters, &decl->entry);
2498 d3dcompiler_free(name);
2499 return;
2501 func = d3dcompiler_alloc(sizeof(*func));
2502 func->name = name;
2503 if (wine_rb_init(&func->overloads, &hlsl_ir_function_decl_rb_funcs) == -1)
2505 ERR("Failed to initialize function rbtree.\n");
2506 d3dcompiler_free(name);
2507 d3dcompiler_free(func);
2508 return;
2510 decl->func = func;
2511 wine_rb_put(&func->overloads, decl->parameters, &decl->entry);
2512 func->intrinsic = intrinsic;
2513 wine_rb_put(funcs, func->name, &func->entry);