d3dcompiler: Distinguish between scalars, vectors and matrices.
[wine] / dlls / d3dcompiler_43 / d3dcompiler_private.h
1 /*
2  * Copyright 2008 Stefan Dösinger
3  * Copyright 2009 Matteo Bruni
4  * Copyright 2010 Rico Schüller
5  * Copyright 2012 Matteo Bruni for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #ifndef __WINE_D3DCOMPILER_PRIVATE_H
23 #define __WINE_D3DCOMPILER_PRIVATE_H
24
25 #include "wine/debug.h"
26 #include "wine/list.h"
27 #include "wine/rbtree.h"
28
29 #define COBJMACROS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "objbase.h"
33
34 #include "d3dcompiler.h"
35
36 /*
37  * This doesn't belong here, but for some functions it is possible to return that value,
38  * see http://msdn.microsoft.com/en-us/library/bb205278%28v=VS.85%29.aspx
39  * The original definition is in D3DX10core.h.
40  */
41 #define D3DERR_INVALIDCALL 0x8876086c
42
43 /* TRACE helper functions */
44 const char *debug_d3dcompiler_d3d_blob_part(D3D_BLOB_PART part) DECLSPEC_HIDDEN;
45 const char *debug_d3dcompiler_shader_variable_class(D3D_SHADER_VARIABLE_CLASS c) DECLSPEC_HIDDEN;
46 const char *debug_d3dcompiler_shader_variable_type(D3D_SHADER_VARIABLE_TYPE t) DECLSPEC_HIDDEN;
47
48 enum shader_type
49 {
50     ST_VERTEX,
51     ST_PIXEL,
52 };
53
54 typedef enum BWRITER_COMPARISON_TYPE {
55     BWRITER_COMPARISON_NONE,
56     BWRITER_COMPARISON_GT,
57     BWRITER_COMPARISON_EQ,
58     BWRITER_COMPARISON_GE,
59     BWRITER_COMPARISON_LT,
60     BWRITER_COMPARISON_NE,
61     BWRITER_COMPARISON_LE
62 } BWRITER_COMPARISON_TYPE;
63
64 struct constant {
65     DWORD                   regnum;
66     union {
67         float               f;
68         INT                 i;
69         BOOL                b;
70         DWORD               d;
71     }                       value[4];
72 };
73
74 struct shader_reg {
75     DWORD                   type;
76     DWORD                   regnum;
77     struct shader_reg       *rel_reg;
78     DWORD                   srcmod;
79     union {
80         DWORD               swizzle;
81         DWORD               writemask;
82     } u;
83 };
84
85 struct instruction {
86     DWORD                   opcode;
87     DWORD                   dstmod;
88     DWORD                   shift;
89     BWRITER_COMPARISON_TYPE comptype;
90     BOOL                    has_dst;
91     struct shader_reg       dst;
92     struct shader_reg       *src;
93     unsigned int            num_srcs; /* For freeing the rel_regs */
94     BOOL                    has_predicate;
95     struct shader_reg       predicate;
96     BOOL                    coissue;
97 };
98
99 struct declaration {
100     DWORD                   usage, usage_idx;
101     DWORD                   regnum;
102     DWORD                   mod;
103     DWORD                   writemask;
104     BOOL                    builtin;
105 };
106
107 struct samplerdecl {
108     DWORD                   type;
109     DWORD                   regnum;
110     DWORD                   mod;
111 };
112
113 #define INSTRARRAY_INITIAL_SIZE 8
114 struct bwriter_shader {
115     enum shader_type        type;
116
117     /* Shader version selected */
118     DWORD                   version;
119
120     /* Local constants. Every constant that is not defined below is loaded from
121      * the global constant set at shader runtime
122      */
123     struct constant         **constF;
124     struct constant         **constI;
125     struct constant         **constB;
126     unsigned int            num_cf, num_ci, num_cb;
127
128     /* Declared input and output varyings */
129     struct declaration      *inputs, *outputs;
130     unsigned int            num_inputs, num_outputs;
131     struct samplerdecl      *samplers;
132     unsigned int            num_samplers;
133
134     /* Are special pixel shader 3.0 registers declared? */
135     BOOL                    vPos, vFace;
136
137     /* Array of shader instructions - The shader code itself */
138     struct instruction      **instr;
139     unsigned int            num_instrs, instr_alloc_size;
140 };
141
142 static inline void *d3dcompiler_alloc(SIZE_T size)
143 {
144     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
145 }
146
147 static inline void *d3dcompiler_realloc(void *ptr, SIZE_T size)
148 {
149     return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
150 }
151
152 static inline BOOL d3dcompiler_free(void *ptr)
153 {
154     return HeapFree(GetProcessHeap(), 0, ptr);
155 }
156
157 static inline char *d3dcompiler_strdup(const char *string)
158 {
159     char *copy;
160     SIZE_T len;
161
162     if (!string)
163         return NULL;
164
165     len = strlen(string);
166     copy = d3dcompiler_alloc(len + 1);
167     if (copy)
168         memcpy(copy, string, len + 1);
169     return copy;
170 }
171
172 struct asm_parser;
173
174 /* This structure is only used in asmshader.y, but since the .l file accesses the semantic types
175  * too it has to know it as well
176  */
177 struct rel_reg {
178     BOOL            has_rel_reg;
179     DWORD           type;
180     DWORD           additional_offset;
181     DWORD           rel_regnum;
182     DWORD           swizzle;
183 };
184
185 #define MAX_SRC_REGS 4
186
187 struct src_regs {
188     struct shader_reg reg[MAX_SRC_REGS];
189     unsigned int      count;
190 };
191
192 struct asmparser_backend {
193     void (*constF)(struct asm_parser *This, DWORD reg, float x, float y, float z, float w);
194     void (*constI)(struct asm_parser *This, DWORD reg, INT x, INT y, INT z, INT w);
195     void (*constB)(struct asm_parser *This, DWORD reg, BOOL x);
196
197     void (*dstreg)(struct asm_parser *This, struct instruction *instr,
198                    const struct shader_reg *dst);
199     void (*srcreg)(struct asm_parser *This, struct instruction *instr, int num,
200                    const struct shader_reg *src);
201
202     void (*predicate)(struct asm_parser *This,
203                       const struct shader_reg *predicate);
204     void (*coissue)(struct asm_parser *This);
205
206     void (*dcl_output)(struct asm_parser *This, DWORD usage, DWORD num,
207                        const struct shader_reg *reg);
208     void (*dcl_input)(struct asm_parser *This, DWORD usage, DWORD num,
209                       DWORD mod, const struct shader_reg *reg);
210     void (*dcl_sampler)(struct asm_parser *This, DWORD samptype, DWORD mod,
211                         DWORD regnum, unsigned int line_no);
212
213     void (*end)(struct asm_parser *This);
214
215     void (*instr)(struct asm_parser *This, DWORD opcode, DWORD mod, DWORD shift,
216                   BWRITER_COMPARISON_TYPE comp, const struct shader_reg *dst,
217                   const struct src_regs *srcs, int expectednsrcs);
218 };
219
220 struct instruction *alloc_instr(unsigned int srcs) DECLSPEC_HIDDEN;
221 BOOL add_instruction(struct bwriter_shader *shader, struct instruction *instr) DECLSPEC_HIDDEN;
222 BOOL add_constF(struct bwriter_shader *shader, DWORD reg, float x, float y, float z, float w) DECLSPEC_HIDDEN;
223 BOOL add_constI(struct bwriter_shader *shader, DWORD reg, INT x, INT y, INT z, INT w) DECLSPEC_HIDDEN;
224 BOOL add_constB(struct bwriter_shader *shader, DWORD reg, BOOL x) DECLSPEC_HIDDEN;
225 BOOL record_declaration(struct bwriter_shader *shader, DWORD usage, DWORD usage_idx,
226         DWORD mod, BOOL output, DWORD regnum, DWORD writemask, BOOL builtin) DECLSPEC_HIDDEN;
227 BOOL record_sampler(struct bwriter_shader *shader, DWORD samptype, DWORD mod, DWORD regnum) DECLSPEC_HIDDEN;
228
229 #define MESSAGEBUFFER_INITIAL_SIZE 256
230
231 enum parse_status
232 {
233     PARSE_SUCCESS = 0,
234     PARSE_WARN = 1,
235     PARSE_ERR = 2
236 };
237
238 struct compilation_messages
239 {
240     char *string;
241     unsigned int size;
242     unsigned int capacity;
243 };
244
245 struct asm_parser
246 {
247     /* The function table of the parser implementation */
248     const struct asmparser_backend *funcs;
249
250     /* Private data follows */
251     struct bwriter_shader *shader;
252     unsigned int m3x3pad_count;
253
254     enum parse_status status;
255     struct compilation_messages messages;
256     unsigned int line_no;
257 };
258
259 extern struct asm_parser asm_ctx DECLSPEC_HIDDEN;
260
261 void create_vs10_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
262 void create_vs11_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
263 void create_vs20_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
264 void create_vs2x_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
265 void create_vs30_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
266 void create_ps10_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
267 void create_ps11_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
268 void create_ps12_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
269 void create_ps13_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
270 void create_ps14_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
271 void create_ps20_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
272 void create_ps2x_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
273 void create_ps30_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
274
275 struct bwriter_shader *parse_asm_shader(char **messages) DECLSPEC_HIDDEN;
276
277 #ifdef __GNUC__
278 #define PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
279 #else
280 #define PRINTF_ATTR(fmt,args)
281 #endif
282
283 void compilation_message(struct compilation_messages *msg, const char *fmt, va_list args) DECLSPEC_HIDDEN;
284 void asmparser_message(struct asm_parser *ctx, const char *fmt, ...) PRINTF_ATTR(2,3) DECLSPEC_HIDDEN;
285 static inline void set_parse_status(enum parse_status *current, enum parse_status update)
286 {
287     if (update == PARSE_ERR)
288         *current = PARSE_ERR;
289     else if (update == PARSE_WARN && *current == PARSE_SUCCESS)
290         *current = PARSE_WARN;
291 }
292
293 /* A reasonable value as initial size */
294 #define BYTECODEBUFFER_INITIAL_SIZE 32
295 struct bytecode_buffer {
296     DWORD *data;
297     DWORD size;
298     DWORD alloc_size;
299     /* For tracking rare out of memory situations without passing
300      * return values around everywhere
301      */
302     HRESULT state;
303 };
304
305 struct bc_writer; /* Predeclaration for use in vtable parameters */
306
307 typedef void (*instr_writer)(struct bc_writer *This,
308                              const struct instruction *instr,
309                              struct bytecode_buffer *buffer);
310
311 struct bytecode_backend {
312     void (*header)(struct bc_writer *This, const struct bwriter_shader *shader,
313                    struct bytecode_buffer *buffer);
314     void (*end)(struct bc_writer *This, const struct bwriter_shader *shader,
315                 struct bytecode_buffer *buffer);
316     void (*srcreg)(struct bc_writer *This, const struct shader_reg *reg,
317                    struct bytecode_buffer *buffer);
318     void (*dstreg)(struct bc_writer *This, const struct shader_reg *reg,
319                    struct bytecode_buffer *buffer, DWORD shift, DWORD mod);
320     void (*opcode)(struct bc_writer *This, const struct instruction *instr,
321                    DWORD token, struct bytecode_buffer *buffer);
322
323     const struct instr_handler_table {
324         DWORD opcode;
325         instr_writer func;
326     } *instructions;
327 };
328
329 /* Bytecode writing stuff */
330 struct bc_writer {
331     const struct bytecode_backend *funcs;
332
333     /* Avoid result checking */
334     HRESULT                       state;
335
336     DWORD                         version;
337
338     /* Vertex shader varying mapping */
339     DWORD                         oPos_regnum;
340     DWORD                         oD_regnum[2];
341     DWORD                         oT_regnum[8];
342     DWORD                         oFog_regnum;
343     DWORD                         oFog_mask;
344     DWORD                         oPts_regnum;
345     DWORD                         oPts_mask;
346
347     /* Pixel shader specific members */
348     DWORD                         t_regnum[8];
349     DWORD                         v_regnum[2];
350 };
351
352 /* Debug utility routines */
353 const char *debug_print_srcmod(DWORD mod) DECLSPEC_HIDDEN;
354 const char *debug_print_dstmod(DWORD mod) DECLSPEC_HIDDEN;
355 const char *debug_print_shift(DWORD shift) DECLSPEC_HIDDEN;
356 const char *debug_print_dstreg(const struct shader_reg *reg) DECLSPEC_HIDDEN;
357 const char *debug_print_srcreg(const struct shader_reg *reg) DECLSPEC_HIDDEN;
358 const char *debug_print_comp(DWORD comp) DECLSPEC_HIDDEN;
359 const char *debug_print_opcode(DWORD opcode) DECLSPEC_HIDDEN;
360
361 /* Used to signal an incorrect swizzle/writemask */
362 #define SWIZZLE_ERR ~0U
363
364 /*
365   Enumerations and defines used in the bytecode writer
366   intermediate representation
367 */
368 typedef enum _BWRITERSHADER_INSTRUCTION_OPCODE_TYPE {
369     BWRITERSIO_NOP,
370     BWRITERSIO_MOV,
371     BWRITERSIO_ADD,
372     BWRITERSIO_SUB,
373     BWRITERSIO_MAD,
374     BWRITERSIO_MUL,
375     BWRITERSIO_RCP,
376     BWRITERSIO_RSQ,
377     BWRITERSIO_DP3,
378     BWRITERSIO_DP4,
379     BWRITERSIO_MIN,
380     BWRITERSIO_MAX,
381     BWRITERSIO_SLT,
382     BWRITERSIO_SGE,
383     BWRITERSIO_EXP,
384     BWRITERSIO_LOG,
385     BWRITERSIO_LIT,
386     BWRITERSIO_DST,
387     BWRITERSIO_LRP,
388     BWRITERSIO_FRC,
389     BWRITERSIO_M4x4,
390     BWRITERSIO_M4x3,
391     BWRITERSIO_M3x4,
392     BWRITERSIO_M3x3,
393     BWRITERSIO_M3x2,
394     BWRITERSIO_CALL,
395     BWRITERSIO_CALLNZ,
396     BWRITERSIO_LOOP,
397     BWRITERSIO_RET,
398     BWRITERSIO_ENDLOOP,
399     BWRITERSIO_LABEL,
400     BWRITERSIO_DCL,
401     BWRITERSIO_POW,
402     BWRITERSIO_CRS,
403     BWRITERSIO_SGN,
404     BWRITERSIO_ABS,
405     BWRITERSIO_NRM,
406     BWRITERSIO_SINCOS,
407     BWRITERSIO_REP,
408     BWRITERSIO_ENDREP,
409     BWRITERSIO_IF,
410     BWRITERSIO_IFC,
411     BWRITERSIO_ELSE,
412     BWRITERSIO_ENDIF,
413     BWRITERSIO_BREAK,
414     BWRITERSIO_BREAKC,
415     BWRITERSIO_MOVA,
416     BWRITERSIO_DEFB,
417     BWRITERSIO_DEFI,
418
419     BWRITERSIO_TEXCOORD,
420     BWRITERSIO_TEXKILL,
421     BWRITERSIO_TEX,
422     BWRITERSIO_TEXBEM,
423     BWRITERSIO_TEXBEML,
424     BWRITERSIO_TEXREG2AR,
425     BWRITERSIO_TEXREG2GB,
426     BWRITERSIO_TEXM3x2PAD,
427     BWRITERSIO_TEXM3x2TEX,
428     BWRITERSIO_TEXM3x3PAD,
429     BWRITERSIO_TEXM3x3TEX,
430     BWRITERSIO_TEXM3x3SPEC,
431     BWRITERSIO_TEXM3x3VSPEC,
432     BWRITERSIO_EXPP,
433     BWRITERSIO_LOGP,
434     BWRITERSIO_CND,
435     BWRITERSIO_DEF,
436     BWRITERSIO_TEXREG2RGB,
437     BWRITERSIO_TEXDP3TEX,
438     BWRITERSIO_TEXM3x2DEPTH,
439     BWRITERSIO_TEXDP3,
440     BWRITERSIO_TEXM3x3,
441     BWRITERSIO_TEXDEPTH,
442     BWRITERSIO_CMP,
443     BWRITERSIO_BEM,
444     BWRITERSIO_DP2ADD,
445     BWRITERSIO_DSX,
446     BWRITERSIO_DSY,
447     BWRITERSIO_TEXLDD,
448     BWRITERSIO_SETP,
449     BWRITERSIO_TEXLDL,
450     BWRITERSIO_BREAKP,
451     BWRITERSIO_TEXLDP,
452     BWRITERSIO_TEXLDB,
453
454     BWRITERSIO_PHASE,
455     BWRITERSIO_COMMENT,
456     BWRITERSIO_END,
457 } BWRITERSHADER_INSTRUCTION_OPCODE_TYPE;
458
459 typedef enum _BWRITERSHADER_PARAM_REGISTER_TYPE {
460     BWRITERSPR_TEMP,
461     BWRITERSPR_INPUT,
462     BWRITERSPR_CONST,
463     BWRITERSPR_ADDR,
464     BWRITERSPR_TEXTURE,
465     BWRITERSPR_RASTOUT,
466     BWRITERSPR_ATTROUT,
467     BWRITERSPR_TEXCRDOUT,
468     BWRITERSPR_OUTPUT,
469     BWRITERSPR_CONSTINT,
470     BWRITERSPR_COLOROUT,
471     BWRITERSPR_DEPTHOUT,
472     BWRITERSPR_SAMPLER,
473     BWRITERSPR_CONSTBOOL,
474     BWRITERSPR_LOOP,
475     BWRITERSPR_MISCTYPE,
476     BWRITERSPR_LABEL,
477     BWRITERSPR_PREDICATE
478 } BWRITERSHADER_PARAM_REGISTER_TYPE;
479
480 typedef enum _BWRITERVS_RASTOUT_OFFSETS
481 {
482     BWRITERSRO_POSITION,
483     BWRITERSRO_FOG,
484     BWRITERSRO_POINT_SIZE
485 } BWRITERVS_RASTOUT_OFFSETS;
486
487 #define BWRITERSP_WRITEMASK_0   0x1 /* .x r */
488 #define BWRITERSP_WRITEMASK_1   0x2 /* .y g */
489 #define BWRITERSP_WRITEMASK_2   0x4 /* .z b */
490 #define BWRITERSP_WRITEMASK_3   0x8 /* .w a */
491 #define BWRITERSP_WRITEMASK_ALL 0xf /* all */
492
493 typedef enum _BWRITERSHADER_PARAM_DSTMOD_TYPE {
494     BWRITERSPDM_NONE = 0,
495     BWRITERSPDM_SATURATE = 1,
496     BWRITERSPDM_PARTIALPRECISION = 2,
497     BWRITERSPDM_MSAMPCENTROID = 4,
498 } BWRITERSHADER_PARAM_DSTMOD_TYPE;
499
500 typedef enum _BWRITERSAMPLER_TEXTURE_TYPE {
501     BWRITERSTT_UNKNOWN = 0,
502     BWRITERSTT_1D = 1,
503     BWRITERSTT_2D = 2,
504     BWRITERSTT_CUBE = 3,
505     BWRITERSTT_VOLUME = 4,
506 } BWRITERSAMPLER_TEXTURE_TYPE;
507
508 #define BWRITERSI_TEXLD_PROJECT 1
509 #define BWRITERSI_TEXLD_BIAS    2
510
511 typedef enum _BWRITERSHADER_PARAM_SRCMOD_TYPE {
512     BWRITERSPSM_NONE = 0,
513     BWRITERSPSM_NEG,
514     BWRITERSPSM_BIAS,
515     BWRITERSPSM_BIASNEG,
516     BWRITERSPSM_SIGN,
517     BWRITERSPSM_SIGNNEG,
518     BWRITERSPSM_COMP,
519     BWRITERSPSM_X2,
520     BWRITERSPSM_X2NEG,
521     BWRITERSPSM_DZ,
522     BWRITERSPSM_DW,
523     BWRITERSPSM_ABS,
524     BWRITERSPSM_ABSNEG,
525     BWRITERSPSM_NOT,
526 } BWRITERSHADER_PARAM_SRCMOD_TYPE;
527
528 #define BWRITER_SM1_VS  0xfffe
529 #define BWRITER_SM1_PS  0xffff
530
531 #define BWRITERPS_VERSION(major, minor) ((BWRITER_SM1_PS << 16) | ((major) << 8) | (minor))
532 #define BWRITERVS_VERSION(major, minor) ((BWRITER_SM1_VS << 16) | ((major) << 8) | (minor))
533
534 #define BWRITERVS_SWIZZLE_SHIFT      16
535 #define BWRITERVS_SWIZZLE_MASK       (0xFF << BWRITERVS_SWIZZLE_SHIFT)
536
537 #define BWRITERVS_X_X       (0 << BWRITERVS_SWIZZLE_SHIFT)
538 #define BWRITERVS_X_Y       (1 << BWRITERVS_SWIZZLE_SHIFT)
539 #define BWRITERVS_X_Z       (2 << BWRITERVS_SWIZZLE_SHIFT)
540 #define BWRITERVS_X_W       (3 << BWRITERVS_SWIZZLE_SHIFT)
541
542 #define BWRITERVS_Y_X       (0 << (BWRITERVS_SWIZZLE_SHIFT + 2))
543 #define BWRITERVS_Y_Y       (1 << (BWRITERVS_SWIZZLE_SHIFT + 2))
544 #define BWRITERVS_Y_Z       (2 << (BWRITERVS_SWIZZLE_SHIFT + 2))
545 #define BWRITERVS_Y_W       (3 << (BWRITERVS_SWIZZLE_SHIFT + 2))
546
547 #define BWRITERVS_Z_X       (0 << (BWRITERVS_SWIZZLE_SHIFT + 4))
548 #define BWRITERVS_Z_Y       (1 << (BWRITERVS_SWIZZLE_SHIFT + 4))
549 #define BWRITERVS_Z_Z       (2 << (BWRITERVS_SWIZZLE_SHIFT + 4))
550 #define BWRITERVS_Z_W       (3 << (BWRITERVS_SWIZZLE_SHIFT + 4))
551
552 #define BWRITERVS_W_X       (0 << (BWRITERVS_SWIZZLE_SHIFT + 6))
553 #define BWRITERVS_W_Y       (1 << (BWRITERVS_SWIZZLE_SHIFT + 6))
554 #define BWRITERVS_W_Z       (2 << (BWRITERVS_SWIZZLE_SHIFT + 6))
555 #define BWRITERVS_W_W       (3 << (BWRITERVS_SWIZZLE_SHIFT + 6))
556
557 #define BWRITERVS_NOSWIZZLE (BWRITERVS_X_X | BWRITERVS_Y_Y | BWRITERVS_Z_Z | BWRITERVS_W_W)
558
559 #define BWRITERVS_SWIZZLE_X (BWRITERVS_X_X | BWRITERVS_Y_X | BWRITERVS_Z_X | BWRITERVS_W_X)
560 #define BWRITERVS_SWIZZLE_Y (BWRITERVS_X_Y | BWRITERVS_Y_Y | BWRITERVS_Z_Y | BWRITERVS_W_Y)
561 #define BWRITERVS_SWIZZLE_Z (BWRITERVS_X_Z | BWRITERVS_Y_Z | BWRITERVS_Z_Z | BWRITERVS_W_Z)
562 #define BWRITERVS_SWIZZLE_W (BWRITERVS_X_W | BWRITERVS_Y_W | BWRITERVS_Z_W | BWRITERVS_W_W)
563
564 typedef enum _BWRITERDECLUSAGE {
565     BWRITERDECLUSAGE_POSITION,
566     BWRITERDECLUSAGE_BLENDWEIGHT,
567     BWRITERDECLUSAGE_BLENDINDICES,
568     BWRITERDECLUSAGE_NORMAL,
569     BWRITERDECLUSAGE_PSIZE,
570     BWRITERDECLUSAGE_TEXCOORD,
571     BWRITERDECLUSAGE_TANGENT,
572     BWRITERDECLUSAGE_BINORMAL,
573     BWRITERDECLUSAGE_TESSFACTOR,
574     BWRITERDECLUSAGE_POSITIONT,
575     BWRITERDECLUSAGE_COLOR,
576     BWRITERDECLUSAGE_FOG,
577     BWRITERDECLUSAGE_DEPTH,
578     BWRITERDECLUSAGE_SAMPLE
579 } BWRITERDECLUSAGE;
580
581 /* ps 1.x texture registers mappings */
582 #define T0_REG          2
583 #define T1_REG          3
584 #define T2_REG          4
585 #define T3_REG          5
586
587 struct bwriter_shader *SlAssembleShader(const char *text, char **messages) DECLSPEC_HIDDEN;
588 HRESULT SlWriteBytecode(const struct bwriter_shader *shader, int dxversion, DWORD **result, DWORD *size) DECLSPEC_HIDDEN;
589 void SlDeleteShader(struct bwriter_shader *shader) DECLSPEC_HIDDEN;
590
591 /* The general IR structure is inspired by Mesa GLSL hir, even though the code
592  * ends up being quite different in practice. Anyway, here comes the relevant
593  * licensing information.
594  *
595  * Copyright © 2010 Intel Corporation
596  *
597  * Permission is hereby granted, free of charge, to any person obtaining a
598  * copy of this software and associated documentation files (the "Software"),
599  * to deal in the Software without restriction, including without limitation
600  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
601  * and/or sell copies of the Software, and to permit persons to whom the
602  * Software is furnished to do so, subject to the following conditions:
603  *
604  * The above copyright notice and this permission notice (including the next
605  * paragraph) shall be included in all copies or substantial portions of the
606  * Software.
607  *
608  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
609  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
610  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
611  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
612  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
613  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
614  * DEALINGS IN THE SOFTWARE.
615  */
616
617 enum hlsl_type_class
618 {
619     HLSL_CLASS_SCALAR,
620     HLSL_CLASS_VECTOR,
621     HLSL_CLASS_MATRIX,
622     HLSL_CLASS_LAST_NUMERIC = HLSL_CLASS_MATRIX,
623     HLSL_CLASS_STRUCT,
624     HLSL_CLASS_ARRAY,
625     HLSL_CLASS_OBJECT,
626 };
627
628 enum hlsl_base_type
629 {
630     HLSL_TYPE_FLOAT,
631     HLSL_TYPE_HALF,
632     HLSL_TYPE_DOUBLE,
633     HLSL_TYPE_INT,
634     HLSL_TYPE_UINT,
635     HLSL_TYPE_BOOL,
636     HLSL_TYPE_LAST_SCALAR = HLSL_TYPE_BOOL,
637     HLSL_TYPE_SAMPLER,
638     HLSL_TYPE_TEXTURE,
639     HLSL_TYPE_PIXELSHADER,
640     HLSL_TYPE_VERTEXSHADER,
641     HLSL_TYPE_STRING,
642     HLSL_TYPE_VOID,
643 };
644
645 enum hlsl_matrix_majority
646 {
647     HLSL_COLUMN_MAJOR,
648     HLSL_ROW_MAJOR
649 };
650
651 struct hlsl_type
652 {
653     struct list entry;
654     struct list scope_entry;
655     enum hlsl_type_class type;
656     enum hlsl_base_type base_type;
657     const char *name;
658     unsigned int modifiers;
659     unsigned int dimx;
660     unsigned int dimy;
661     union
662     {
663         struct list *elements;
664         struct
665         {
666             struct hlsl_type *type;
667             unsigned int elements_count;
668         } array;
669     } e;
670 };
671
672 struct hlsl_struct_field
673 {
674     struct list entry;
675     struct hlsl_type *type;
676     const char *name;
677     const char *semantic;
678     DWORD modifiers;
679 };
680
681 enum hlsl_ir_node_type
682 {
683     HLSL_IR_VAR = 0,
684 };
685
686 struct hlsl_ir_node
687 {
688     struct list entry;
689     enum hlsl_ir_node_type type;
690     struct hlsl_type *data_type;
691
692     char *source_file;
693     unsigned int line;
694     unsigned int column;
695 };
696
697 struct hlsl_ir_var
698 {
699     struct hlsl_ir_node node;
700     const char *name;
701     const char *semantic;
702     unsigned int modifiers;
703     struct list scope_entry;
704
705     struct hlsl_var_allocation *allocation;
706 };
707
708 struct hlsl_scope
709 {
710     struct list entry;
711     struct list vars;
712     struct list types;
713     struct hlsl_scope *upper;
714 };
715
716 /* Structures used only during parsing */
717 struct parse_variable_def
718 {
719     struct list entry;
720     char *name;
721     unsigned int array_size;
722     char *semantic;
723     struct list *initializer;
724 };
725
726 struct hlsl_parse_ctx
727 {
728     char *source_file;
729     unsigned int line_no;
730     enum parse_status status;
731     struct compilation_messages messages;
732
733     struct hlsl_scope *cur_scope;
734     struct hlsl_scope *globals;
735     struct list scopes;
736
737     struct list types;
738     struct list functions;
739
740     enum hlsl_matrix_majority matrix_majority;
741 };
742
743 extern struct hlsl_parse_ctx hlsl_ctx DECLSPEC_HIDDEN;
744
745 void hlsl_message(const char *fmt, ...) PRINTF_ATTR(1,2) DECLSPEC_HIDDEN;
746
747 BOOL add_declaration(struct hlsl_scope *scope, struct hlsl_ir_var *decl, BOOL local_var) DECLSPEC_HIDDEN;
748 struct hlsl_ir_var *get_variable(struct hlsl_scope *scope, const char *name) DECLSPEC_HIDDEN;
749 void free_declaration(struct hlsl_ir_var *decl) DECLSPEC_HIDDEN;
750 struct hlsl_type *new_hlsl_type(const char *name, enum hlsl_type_class type_class,
751         enum hlsl_base_type base_type, unsigned dimx, unsigned dimy) DECLSPEC_HIDDEN;
752 struct hlsl_type *new_array_type(struct hlsl_type *basic_type, unsigned int array_size) DECLSPEC_HIDDEN;
753 struct hlsl_type *get_type(struct hlsl_scope *scope, const char *name, BOOL recursive) DECLSPEC_HIDDEN;
754 BOOL find_function(const char *name) DECLSPEC_HIDDEN;
755 unsigned int components_count_type(struct hlsl_type *type) DECLSPEC_HIDDEN;
756 struct hlsl_ir_deref *new_var_deref(struct hlsl_ir_var *var) DECLSPEC_HIDDEN;
757 void push_scope(struct hlsl_parse_ctx *ctx) DECLSPEC_HIDDEN;
758 BOOL pop_scope(struct hlsl_parse_ctx *ctx) DECLSPEC_HIDDEN;
759 struct bwriter_shader *parse_hlsl_shader(const char *text, enum shader_type type, DWORD version,
760         const char *entrypoint, char **messages) DECLSPEC_HIDDEN;
761
762 const char *debug_base_type(const struct hlsl_type *type) DECLSPEC_HIDDEN;
763 const char *debug_hlsl_type(const struct hlsl_type *type) DECLSPEC_HIDDEN;
764 const char *debug_modifiers(DWORD modifiers) DECLSPEC_HIDDEN;
765 void free_hlsl_type(struct hlsl_type *type) DECLSPEC_HIDDEN;
766
767
768 #define MAKE_TAG(ch0, ch1, ch2, ch3) \
769     ((DWORD)(ch0) | ((DWORD)(ch1) << 8) | \
770     ((DWORD)(ch2) << 16) | ((DWORD)(ch3) << 24 ))
771 #define TAG_Aon9 MAKE_TAG('A', 'o', 'n', '9')
772 #define TAG_DXBC MAKE_TAG('D', 'X', 'B', 'C')
773 #define TAG_ISGN MAKE_TAG('I', 'S', 'G', 'N')
774 #define TAG_OSGN MAKE_TAG('O', 'S', 'G', 'N')
775 #define TAG_OSG5 MAKE_TAG('O', 'S', 'G', '5')
776 #define TAG_PCSG MAKE_TAG('P', 'C', 'S', 'G')
777 #define TAG_RDEF MAKE_TAG('R', 'D', 'E', 'F')
778 #define TAG_SDBG MAKE_TAG('S', 'D', 'B', 'G')
779 #define TAG_SHDR MAKE_TAG('S', 'H', 'D', 'R')
780 #define TAG_SHEX MAKE_TAG('S', 'H', 'E', 'X')
781 #define TAG_STAT MAKE_TAG('S', 'T', 'A', 'T')
782 #define TAG_XNAP MAKE_TAG('X', 'N', 'A', 'P')
783 #define TAG_XNAS MAKE_TAG('X', 'N', 'A', 'S')
784
785 struct dxbc_section
786 {
787     DWORD tag;
788     const char *data;
789     DWORD data_size;
790 };
791
792 struct dxbc
793 {
794     UINT size;
795     UINT count;
796     struct dxbc_section *sections;
797 };
798
799 HRESULT dxbc_write_blob(struct dxbc *dxbc, ID3DBlob **blob) DECLSPEC_HIDDEN;
800 void dxbc_destroy(struct dxbc *dxbc) DECLSPEC_HIDDEN;
801 HRESULT dxbc_parse(const char *data, SIZE_T data_size, struct dxbc *dxbc) DECLSPEC_HIDDEN;
802 HRESULT dxbc_add_section(struct dxbc *dxbc, DWORD tag, const char *data, DWORD data_size) DECLSPEC_HIDDEN;
803 HRESULT dxbc_init(struct dxbc *dxbc, DWORD count) DECLSPEC_HIDDEN;
804
805 static inline void read_dword(const char **ptr, DWORD *d)
806 {
807     memcpy(d, *ptr, sizeof(*d));
808     *ptr += sizeof(*d);
809 }
810
811 static inline void write_dword(char **ptr, DWORD d)
812 {
813     memcpy(*ptr, &d, sizeof(d));
814     *ptr += sizeof(d);
815 }
816
817 void skip_dword_unknown(const char **ptr, unsigned int count) DECLSPEC_HIDDEN;
818
819 #endif /* __WINE_D3DCOMPILER_PRIVATE_H */