d3dcompiler: Parse variable declarations.
[wine] / dlls / d3dcompiler_43 / utils.c
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
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdio.h>
28
29 #include "d3dcompiler_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
32
33 #define WINE_D3DCOMPILER_TO_STR(x) case x: return #x
34
35 const char *debug_d3dcompiler_shader_variable_class(D3D_SHADER_VARIABLE_CLASS c)
36 {
37     switch (c)
38     {
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";
50     }
51 }
52
53 const char *debug_d3dcompiler_shader_variable_type(D3D_SHADER_VARIABLE_TYPE t)
54 {
55     switch (t)
56     {
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";
106     }
107 }
108
109 const char *debug_d3dcompiler_d3d_blob_part(D3D_BLOB_PART part)
110 {
111     switch(part)
112     {
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";
128     }
129 }
130
131 const char *debug_print_srcmod(DWORD mod)
132 {
133     switch (mod)
134     {
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";
151     }
152 }
153
154 #undef WINE_D3DCOMPILER_TO_STR
155
156 const char *debug_print_dstmod(DWORD mod)
157 {
158     switch (mod)
159     {
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";
178     }
179 }
180
181 const char *debug_print_shift(DWORD shift)
182 {
183     static const char * const shiftstrings[] =
184     {
185         "",
186         "_x2",
187         "_x4",
188         "_x8",
189         "_x16",
190         "_x32",
191         "",
192         "",
193         "",
194         "",
195         "",
196         "",
197         "_d16",
198         "_d8",
199         "_d4",
200         "_d2",
201     };
202     return shiftstrings[shift];
203 }
204
205 static const char *get_regname(const struct shader_reg *reg)
206 {
207     switch (reg->type)
208     {
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)
221             {
222                 case BWRITERSRO_POSITION:   return "oPos";
223                 case BWRITERSRO_FOG:        return "oFog";
224                 case BWRITERSRO_POINT_SIZE: return "oPts";
225                 default: return "Unexpected RASTOUT";
226             }
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)
247             {
248                 case 0: return "vPos";
249                 case 1: return "vFace";
250                 default: return "unexpected misctype";
251             }
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);
258     }
259 }
260
261 static const char *debug_print_writemask(DWORD mask)
262 {
263     char ret[6];
264     unsigned char pos = 1;
265
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;
273
274     return wine_dbg_sprintf("%s", ret);
275 }
276
277 static const char *debug_print_swizzle(DWORD arg)
278 {
279     char ret[6];
280     unsigned int i;
281     DWORD swizzle[4];
282
283     switch (arg)
284     {
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";
295     }
296
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;
301
302     ret[0] = '.';
303     for (i = 0; i < 4; ++i)
304     {
305         switch (swizzle[i])
306         {
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;
311         }
312     }
313     ret[5] = '\0';
314
315     return wine_dbg_sprintf("%s", ret);
316 }
317
318 static const char *debug_print_relarg(const struct shader_reg *reg)
319 {
320     const char *short_swizzle;
321     if (!reg->rel_reg) return "";
322
323     short_swizzle = debug_print_swizzle(reg->rel_reg->u.swizzle);
324
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";
331 }
332
333 const char *debug_print_dstreg(const struct shader_reg *reg)
334 {
335     return wine_dbg_sprintf("%s%s%s", get_regname(reg),
336             debug_print_relarg(reg),
337             debug_print_writemask(reg->u.writemask));
338 }
339
340 const char *debug_print_srcreg(const struct shader_reg *reg)
341 {
342     switch (reg->srcmod)
343     {
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));
400     }
401     return "Unknown modifier";
402 }
403
404 const char *debug_print_comp(DWORD comp)
405 {
406     switch (comp)
407     {
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";
416     }
417 }
418
419 const char *debug_print_opcode(DWORD opcode)
420 {
421     switch (opcode)
422     {
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";
505
506         case BWRITERSIO_TEXLDP:       return "texldp";
507         case BWRITERSIO_TEXLDB:       return "texldb";
508
509         default:                      return "unknown";
510     }
511 }
512
513 void skip_dword_unknown(const char **ptr, unsigned int count)
514 {
515     unsigned int i;
516     DWORD d;
517
518     FIXME("Skipping %u unknown DWORDs:\n", count);
519     for (i = 0; i < count; ++i)
520     {
521         read_dword(ptr, &d);
522         FIXME("\t0x%08x\n", d);
523     }
524 }
525
526 static void write_dword_unknown(char **ptr, DWORD d)
527 {
528     FIXME("Writing unknown DWORD 0x%08x\n", d);
529     write_dword(ptr, d);
530 }
531
532 HRESULT dxbc_add_section(struct dxbc *dxbc, DWORD tag, const char *data, DWORD data_size)
533 {
534     TRACE("dxbc %p, tag %s, size %#x.\n", dxbc, debugstr_an((const char *)&tag, 4), data_size);
535
536     if (dxbc->count >= dxbc->size)
537     {
538         struct dxbc_section *new_sections;
539         DWORD new_size = dxbc->size << 1;
540
541         new_sections = HeapReAlloc(GetProcessHeap(), 0, dxbc->sections, new_size * sizeof(*dxbc->sections));
542         if (!new_sections)
543         {
544             ERR("Failed to allocate dxbc section memory\n");
545             return E_OUTOFMEMORY;
546         }
547
548         dxbc->sections = new_sections;
549         dxbc->size = new_size;
550     }
551
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;
556
557     return S_OK;
558 }
559
560 HRESULT dxbc_init(struct dxbc *dxbc, UINT size)
561 {
562     TRACE("dxbc %p, size %u.\n", dxbc, size);
563
564     /* use a good starting value for the size if none specified */
565     if (!size) size = 2;
566
567     dxbc->sections = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*dxbc->sections));
568     if (!dxbc->sections)
569     {
570         ERR("Failed to allocate dxbc section memory\n");
571         return E_OUTOFMEMORY;
572     }
573
574     dxbc->size = size;
575     dxbc->count = 0;
576
577     return S_OK;
578 }
579
580 HRESULT dxbc_parse(const char *data, SIZE_T data_size, struct dxbc *dxbc)
581 {
582     const char *ptr = data;
583     HRESULT hr;
584     unsigned int i;
585     DWORD tag, total_size, chunk_count;
586
587     if (!data)
588     {
589         WARN("No data supplied.\n");
590         return E_FAIL;
591     }
592
593     read_dword(&ptr, &tag);
594     TRACE("tag: %s.\n", debugstr_an((const char *)&tag, 4));
595
596     if (tag != TAG_DXBC)
597     {
598         WARN("Wrong tag.\n");
599         return E_FAIL;
600     }
601
602     /* checksum? */
603     skip_dword_unknown(&ptr, 4);
604
605     skip_dword_unknown(&ptr, 1);
606
607     read_dword(&ptr, &total_size);
608     TRACE("total size: %#x\n", total_size);
609
610     if (data_size != total_size)
611     {
612         WARN("Wrong size supplied.\n");
613         return D3DERR_INVALIDCALL;
614     }
615
616     read_dword(&ptr, &chunk_count);
617     TRACE("chunk count: %#x\n", chunk_count);
618
619     hr = dxbc_init(dxbc, chunk_count);
620     if (FAILED(hr))
621     {
622         WARN("Failed to init dxbc\n");
623         return hr;
624     }
625
626     for (i = 0; i < chunk_count; ++i)
627     {
628         DWORD chunk_tag, chunk_size;
629         const char *chunk_ptr;
630         DWORD chunk_offset;
631
632         read_dword(&ptr, &chunk_offset);
633         TRACE("chunk %u at offset %#x\n", i, chunk_offset);
634
635         chunk_ptr = data + chunk_offset;
636
637         read_dword(&chunk_ptr, &chunk_tag);
638         read_dword(&chunk_ptr, &chunk_size);
639
640         hr = dxbc_add_section(dxbc, chunk_tag, chunk_ptr, chunk_size);
641         if (FAILED(hr))
642         {
643             WARN("Failed to add section to dxbc\n");
644             return hr;
645         }
646     }
647
648     return hr;
649 }
650
651 void dxbc_destroy(struct dxbc *dxbc)
652 {
653     TRACE("dxbc %p.\n", dxbc);
654
655     HeapFree(GetProcessHeap(), 0, dxbc->sections);
656 }
657
658 HRESULT dxbc_write_blob(struct dxbc *dxbc, ID3DBlob **blob)
659 {
660     DWORD size = 32, offset = size + 4 * dxbc->count;
661     ID3DBlob *object;
662     HRESULT hr;
663     char *ptr;
664     unsigned int i;
665
666     TRACE("dxbc %p, blob %p.\n", dxbc, blob);
667
668     for (i = 0; i < dxbc->count; ++i)
669     {
670         size += 12 + dxbc->sections[i].data_size;
671     }
672
673     hr = D3DCreateBlob(size, &object);
674     if (FAILED(hr))
675     {
676         WARN("Failed to create blob\n");
677         return hr;
678     }
679
680     ptr = ID3D10Blob_GetBufferPointer(object);
681
682     write_dword(&ptr, TAG_DXBC);
683
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);
689
690     /* seems to be always 1 */
691     write_dword_unknown(&ptr, 1);
692
693     /* DXBC size */
694     write_dword(&ptr, size);
695
696     /* chunk count */
697     write_dword(&ptr, dxbc->count);
698
699     /* write the chunk offsets */
700     for (i = 0; i < dxbc->count; ++i)
701     {
702         write_dword(&ptr, offset);
703         offset += 8 + dxbc->sections[i].data_size;
704     }
705
706     /* write the chunks */
707     for (i = 0; i < dxbc->count; ++i)
708     {
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;
713     }
714
715     TRACE("Created ID3DBlob %p\n", object);
716
717     *blob = object;
718
719     return S_OK;
720 }
721
722 void compilation_message(struct compilation_messages *msg, const char *fmt, va_list args)
723 {
724     char* buffer;
725     int rc, size;
726
727     if (msg->capacity == 0)
728     {
729         msg->string = d3dcompiler_alloc(MESSAGEBUFFER_INITIAL_SIZE);
730         if (msg->string == NULL)
731         {
732             ERR("Error allocating memory for parser messages\n");
733             return;
734         }
735         msg->capacity = MESSAGEBUFFER_INITIAL_SIZE;
736     }
737
738     while (1)
739     {
740         rc = vsnprintf(msg->string + msg->size,
741                 msg->capacity - msg->size, fmt, args);
742
743         if (rc < 0 || rc >= msg->capacity - msg->size)
744         {
745             size = msg->capacity * 2;
746             buffer = d3dcompiler_realloc(msg->string, size);
747             if (buffer == NULL)
748             {
749                 ERR("Error reallocating memory for parser messages\n");
750                 return;
751             }
752             msg->string = buffer;
753             msg->capacity = size;
754         }
755         else
756         {
757             TRACE("%s", msg->string + msg->size);
758             msg->size += rc;
759             return;
760         }
761     }
762 }
763
764 BOOL add_declaration(struct hlsl_scope *scope, struct hlsl_ir_var *decl, BOOL local_var)
765 {
766     struct hlsl_ir_var *var;
767
768     LIST_FOR_EACH_ENTRY(var, &scope->vars, struct hlsl_ir_var, scope_entry)
769     {
770         if (!strcmp(decl->name, var->name))
771             return FALSE;
772     }
773     if (local_var)
774     {
775         LIST_FOR_EACH_ENTRY(var, &scope->upper->vars, struct hlsl_ir_var, scope_entry)
776         {
777             if (!strcmp(decl->name, var->name))
778                 return FALSE;
779         }
780     }
781
782     list_add_tail(&scope->vars, &decl->scope_entry);
783     return TRUE;
784 }
785
786 struct hlsl_ir_var *get_variable(struct hlsl_scope *scope, const char *name)
787 {
788     struct hlsl_ir_var *var;
789
790     LIST_FOR_EACH_ENTRY(var, &scope->vars, struct hlsl_ir_var, scope_entry)
791     {
792         if (!strcmp(name, var->name))
793             return var;
794     }
795     if (!scope->upper)
796         return NULL;
797     return get_variable(scope->upper, name);
798 }
799
800 void free_declaration(struct hlsl_ir_var *decl)
801 {
802     d3dcompiler_free((void *)decl->name);
803     d3dcompiler_free((void *)decl->semantic);
804     d3dcompiler_free(decl);
805 }
806
807 struct hlsl_type *new_hlsl_type(const char *name, enum hlsl_base_type base_type, unsigned dimx, unsigned dimy)
808 {
809     struct hlsl_type *type;
810
811     type = d3dcompiler_alloc(sizeof(*type));
812     if (!type)
813     {
814         ERR("Out of memory\n");
815         return NULL;
816     }
817     type->name = name;
818     type->base_type = base_type;
819     type->dimx = dimx;
820     type->dimy = dimy;
821
822     list_add_tail(&hlsl_ctx.types, &type->entry);
823
824     return type;
825 }
826
827 struct hlsl_type *new_array_type(struct hlsl_type *basic_type, unsigned int array_size)
828 {
829     FIXME("stub.\n");
830     return NULL;
831 }
832
833 struct hlsl_type *get_type(struct hlsl_scope *scope, const char *name, BOOL recursive)
834 {
835     FIXME("stub.\n");
836     return NULL;
837 }
838
839 BOOL find_function(const char *name)
840 {
841     FIXME("stub.\n");
842     return FALSE;
843 }
844
845 void push_scope(struct hlsl_parse_ctx *ctx)
846 {
847     struct hlsl_scope *new_scope = d3dcompiler_alloc(sizeof(*new_scope));
848
849     if (!new_scope)
850     {
851         ERR("Out of memory!\n");
852         return;
853     }
854     list_init(&new_scope->vars);
855     list_init(&new_scope->types);
856     new_scope->upper = ctx->cur_scope;
857     ctx->cur_scope = new_scope;
858     list_add_tail(&ctx->scopes, &new_scope->entry);
859 }
860
861 BOOL pop_scope(struct hlsl_parse_ctx *ctx)
862 {
863     struct hlsl_scope *prev_scope = ctx->cur_scope->upper;
864     if (!prev_scope)
865         return FALSE;
866
867     ctx->cur_scope = prev_scope;
868     return TRUE;
869 }
870
871 const char *debug_base_type(const struct hlsl_type *type)
872 {
873     const char *name = "(unknown)";
874
875     switch (type->base_type)
876     {
877         case HLSL_TYPE_FLOAT:        name = "float";         break;
878         case HLSL_TYPE_HALF:         name = "half";          break;
879         case HLSL_TYPE_INT:          name = "int";           break;
880         case HLSL_TYPE_UINT:         name = "uint";          break;
881         case HLSL_TYPE_BOOL:         name = "bool";          break;
882         case HLSL_TYPE_STRUCT:       name = "struct";        break;
883         case HLSL_TYPE_ARRAY:        name = "array";         break;
884         default:
885             FIXME("Unhandled case %u\n", type->base_type);
886     }
887     return name;
888 }
889
890 const char *debug_hlsl_type(const struct hlsl_type *type)
891 {
892     const char *name;
893
894     if (type->name)
895         return debugstr_a(type->name);
896
897     if (type->base_type == HLSL_TYPE_STRUCT)
898         name = "<anonymous struct>";
899     else
900         name = debug_base_type(type);
901     if (type->dimx == 1 && type->dimy == 1)
902         return wine_dbg_sprintf("%s", name);
903     if (type->dimy == 1)
904         return wine_dbg_sprintf("vector<%s, %u>", name, type->dimx);
905     return wine_dbg_sprintf("matrix<%s, %u, %u>", name, type->dimx, type->dimy);
906 }
907
908 const char *debug_node_type(enum hlsl_ir_node_type type)
909 {
910     const char *names[] =
911     {
912         "HLSL_IR_VAR",
913     };
914
915     if (type > sizeof(names) / sizeof(names[0]))
916     {
917         return "Unexpected node type";
918     }
919     return names[type];
920 }
921
922 void free_hlsl_type(struct hlsl_type *type)
923 {
924     struct hlsl_struct_field *field, *next_field;
925
926     d3dcompiler_free((void *)type->name);
927     if (type->base_type == HLSL_TYPE_STRUCT)
928     {
929         LIST_FOR_EACH_ENTRY_SAFE(field, next_field, type->e.elements, struct hlsl_struct_field, entry)
930         {
931             d3dcompiler_free((void *)field->name);
932             d3dcompiler_free((void *)field->semantic);
933             d3dcompiler_free(field);
934         }
935     }
936     d3dcompiler_free(type);
937 }