2 * Copyright 2008 Stefan Dösinger
3 * Copyright 2009 Matteo Bruni
4 * Copyright 2010 Rico Schüller
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifndef __WINE_D3DCOMPILER_PRIVATE_H
22 #define __WINE_D3DCOMPILER_PRIVATE_H
24 #include "wine/debug.h"
25 #include "wine/list.h"
32 #include "d3dcompiler.h"
35 * This doesn't belong here, but for some functions it is possible to return that value,
36 * see http://msdn.microsoft.com/en-us/library/bb205278%28v=VS.85%29.aspx
37 * The original definition is in D3DX10core.h.
39 #define D3DERR_INVALIDCALL 0x8876086c
41 /* TRACE helper functions */
42 const char *debug_d3dcompiler_d3d_blob_part(D3D_BLOB_PART part) DECLSPEC_HIDDEN;
45 struct d3dcompiler_blob
47 ID3DBlob ID3DBlob_iface;
54 HRESULT d3dcompiler_blob_init(struct d3dcompiler_blob *blob, SIZE_T data_size) DECLSPEC_HIDDEN;
57 HRESULT d3dcompiler_get_blob_part(const void *data, SIZE_T data_size, D3D_BLOB_PART part, UINT flags, ID3DBlob **blob) DECLSPEC_HIDDEN;
58 HRESULT d3dcompiler_strip_shader(const void *data, SIZE_T data_size, UINT flags, ID3DBlob **blob) DECLSPEC_HIDDEN;
60 struct d3dcompiler_shader_signature
62 D3D11_SIGNATURE_PARAMETER_DESC *elements;
67 /* ID3D11ShaderReflection */
68 struct d3dcompiler_shader_reflection
70 ID3D11ShaderReflection ID3D11ShaderReflection_iface;
73 UINT mov_instruction_count;
74 UINT conversion_instruction_count;
75 UINT instruction_count;
76 UINT emit_instruction_count;
77 D3D_PRIMITIVE_TOPOLOGY gs_output_topology;
78 UINT gs_max_output_vertex_count;
79 D3D_PRIMITIVE input_primitive;
80 UINT cut_instruction_count;
82 UINT static_flow_control_count;
83 UINT float_instruction_count;
84 UINT temp_register_count;
85 UINT int_instruction_count;
86 UINT uint_instruction_count;
87 UINT temp_array_count;
88 UINT array_instruction_count;
89 UINT texture_normal_instructions;
90 UINT texture_load_instructions;
91 UINT texture_comp_instructions;
92 UINT texture_bias_instructions;
93 UINT texture_gradient_instructions;
94 UINT dynamic_flow_control_count;
95 UINT c_control_points;
96 D3D_TESSELLATOR_OUTPUT_PRIMITIVE hs_output_primitive;
97 D3D_TESSELLATOR_PARTITIONING hs_prtitioning;
98 D3D_TESSELLATOR_DOMAIN tessellator_domain;
100 struct d3dcompiler_shader_signature *isgn;
101 struct d3dcompiler_shader_signature *osgn;
104 /* reflection handling */
105 HRESULT d3dcompiler_shader_reflection_init(struct d3dcompiler_shader_reflection *reflection, const void *data, SIZE_T data_size) DECLSPEC_HIDDEN;
107 /* Shader assembler definitions */
108 typedef enum _shader_type {
113 typedef enum BWRITER_COMPARISON_TYPE {
114 BWRITER_COMPARISON_NONE,
115 BWRITER_COMPARISON_GT,
116 BWRITER_COMPARISON_EQ,
117 BWRITER_COMPARISON_GE,
118 BWRITER_COMPARISON_LT,
119 BWRITER_COMPARISON_NE,
120 BWRITER_COMPARISON_LE
121 } BWRITER_COMPARISON_TYPE;
136 struct shader_reg *rel_reg;
148 BWRITER_COMPARISON_TYPE comptype;
150 struct shader_reg dst;
151 struct shader_reg *src;
152 unsigned int num_srcs; /* For freeing the rel_regs */
154 struct shader_reg predicate;
159 DWORD usage, usage_idx;
172 #define INSTRARRAY_INITIAL_SIZE 8
173 struct bwriter_shader {
176 /* Shader version selected */
179 /* Local constants. Every constant that is not defined below is loaded from
180 * the global constant set at shader runtime
182 struct constant **constF;
183 struct constant **constI;
184 struct constant **constB;
185 unsigned int num_cf, num_ci, num_cb;
187 /* Declared input and output varyings */
188 struct declaration *inputs, *outputs;
189 unsigned int num_inputs, num_outputs;
190 struct samplerdecl *samplers;
191 unsigned int num_samplers;
193 /* Are special pixel shader 3.0 registers declared? */
196 /* Array of shader instructions - The shader code itself */
197 struct instruction **instr;
198 unsigned int num_instrs, instr_alloc_size;
201 static inline LPVOID asm_alloc(SIZE_T size) {
202 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
205 static inline LPVOID asm_realloc(LPVOID ptr, SIZE_T size) {
206 return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
209 static inline BOOL asm_free(LPVOID ptr) {
210 return HeapFree(GetProcessHeap(), 0, ptr);
215 /* This structure is only used in asmshader.y, but since the .l file accesses the semantic types
216 * too it has to know it as well
221 DWORD additional_offset;
226 #define MAX_SRC_REGS 4
229 struct shader_reg reg[MAX_SRC_REGS];
233 struct asmparser_backend {
234 void (*constF)(struct asm_parser *This, DWORD reg, float x, float y, float z, float w);
235 void (*constI)(struct asm_parser *This, DWORD reg, INT x, INT y, INT z, INT w);
236 void (*constB)(struct asm_parser *This, DWORD reg, BOOL x);
238 void (*dstreg)(struct asm_parser *This, struct instruction *instr,
239 const struct shader_reg *dst);
240 void (*srcreg)(struct asm_parser *This, struct instruction *instr, int num,
241 const struct shader_reg *src);
243 void (*predicate)(struct asm_parser *This,
244 const struct shader_reg *predicate);
245 void (*coissue)(struct asm_parser *This);
247 void (*dcl_output)(struct asm_parser *This, DWORD usage, DWORD num,
248 const struct shader_reg *reg);
249 void (*dcl_input)(struct asm_parser *This, DWORD usage, DWORD num,
250 DWORD mod, const struct shader_reg *reg);
251 void (*dcl_sampler)(struct asm_parser *This, DWORD samptype, DWORD mod,
252 DWORD regnum, unsigned int line_no);
254 void (*end)(struct asm_parser *This);
256 void (*instr)(struct asm_parser *This, DWORD opcode, DWORD mod, DWORD shift,
257 BWRITER_COMPARISON_TYPE comp, const struct shader_reg *dst,
258 const struct src_regs *srcs, int expectednsrcs);
261 struct instruction *alloc_instr(unsigned int srcs) DECLSPEC_HIDDEN;
262 BOOL add_instruction(struct bwriter_shader *shader, struct instruction *instr) DECLSPEC_HIDDEN;
263 BOOL add_constF(struct bwriter_shader *shader, DWORD reg, float x, float y, float z, float w) DECLSPEC_HIDDEN;
264 BOOL add_constI(struct bwriter_shader *shader, DWORD reg, INT x, INT y, INT z, INT w) DECLSPEC_HIDDEN;
265 BOOL add_constB(struct bwriter_shader *shader, DWORD reg, BOOL x) DECLSPEC_HIDDEN;
266 BOOL record_declaration(struct bwriter_shader *shader, DWORD usage, DWORD usage_idx,
267 DWORD mod, BOOL output, DWORD regnum, DWORD writemask, BOOL builtin) DECLSPEC_HIDDEN;
268 BOOL record_sampler(struct bwriter_shader *shader, DWORD samptype, DWORD mod, DWORD regnum) DECLSPEC_HIDDEN;
270 #define MESSAGEBUFFER_INITIAL_SIZE 256
273 /* The function table of the parser implementation */
274 const struct asmparser_backend *funcs;
276 /* Private data follows */
277 struct bwriter_shader *shader;
278 unsigned int m3x3pad_count;
286 unsigned int messagesize;
287 unsigned int messagecapacity;
288 unsigned int line_no;
291 extern struct asm_parser asm_ctx DECLSPEC_HIDDEN;
293 void create_vs10_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
294 void create_vs11_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
295 void create_vs20_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
296 void create_vs2x_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
297 void create_vs30_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
298 void create_ps10_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
299 void create_ps11_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
300 void create_ps12_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
301 void create_ps13_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
302 void create_ps14_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
303 void create_ps20_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
304 void create_ps2x_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
305 void create_ps30_parser(struct asm_parser *ret) DECLSPEC_HIDDEN;
307 struct bwriter_shader *parse_asm_shader(char **messages) DECLSPEC_HIDDEN;
310 #define PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
312 #define PRINTF_ATTR(fmt,args)
315 void asmparser_message(struct asm_parser *ctx, const char *fmt, ...) PRINTF_ATTR(2,3) DECLSPEC_HIDDEN;
316 void set_parse_status(struct asm_parser *ctx, enum parse_status status) DECLSPEC_HIDDEN;
318 /* A reasonable value as initial size */
319 #define BYTECODEBUFFER_INITIAL_SIZE 32
320 struct bytecode_buffer {
324 /* For tracking rare out of memory situations without passing
325 * return values around everywhere
330 struct bc_writer; /* Predeclaration for use in vtable parameters */
332 typedef void (*instr_writer)(struct bc_writer *This,
333 const struct instruction *instr,
334 struct bytecode_buffer *buffer);
336 struct bytecode_backend {
337 void (*header)(struct bc_writer *This, const struct bwriter_shader *shader,
338 struct bytecode_buffer *buffer);
339 void (*end)(struct bc_writer *This, const struct bwriter_shader *shader,
340 struct bytecode_buffer *buffer);
341 void (*srcreg)(struct bc_writer *This, const struct shader_reg *reg,
342 struct bytecode_buffer *buffer);
343 void (*dstreg)(struct bc_writer *This, const struct shader_reg *reg,
344 struct bytecode_buffer *buffer, DWORD shift, DWORD mod);
345 void (*opcode)(struct bc_writer *This, const struct instruction *instr,
346 DWORD token, struct bytecode_buffer *buffer);
348 const struct instr_handler_table {
354 /* Bytecode writing stuff */
356 const struct bytecode_backend *funcs;
358 /* Avoid result checking */
363 /* Vertex shader varying mapping */
372 /* Pixel shader specific members */
377 /* Debug utility routines */
378 const char *debug_print_srcmod(DWORD mod) DECLSPEC_HIDDEN;
379 const char *debug_print_dstmod(DWORD mod) DECLSPEC_HIDDEN;
380 const char *debug_print_shift(DWORD shift) DECLSPEC_HIDDEN;
381 const char *debug_print_dstreg(const struct shader_reg *reg) DECLSPEC_HIDDEN;
382 const char *debug_print_srcreg(const struct shader_reg *reg) DECLSPEC_HIDDEN;
383 const char *debug_print_comp(DWORD comp) DECLSPEC_HIDDEN;
384 const char *debug_print_opcode(DWORD opcode) DECLSPEC_HIDDEN;
386 /* Used to signal an incorrect swizzle/writemask */
387 #define SWIZZLE_ERR ~0U
390 Enumerations and defines used in the bytecode writer
391 intermediate representation
393 typedef enum _BWRITERSHADER_INSTRUCTION_OPCODE_TYPE {
449 BWRITERSIO_TEXREG2AR,
450 BWRITERSIO_TEXREG2GB,
451 BWRITERSIO_TEXM3x2PAD,
452 BWRITERSIO_TEXM3x2TEX,
453 BWRITERSIO_TEXM3x3PAD,
454 BWRITERSIO_TEXM3x3TEX,
455 BWRITERSIO_TEXM3x3SPEC,
456 BWRITERSIO_TEXM3x3VSPEC,
461 BWRITERSIO_TEXREG2RGB,
462 BWRITERSIO_TEXDP3TEX,
463 BWRITERSIO_TEXM3x2DEPTH,
482 } BWRITERSHADER_INSTRUCTION_OPCODE_TYPE;
484 typedef enum _BWRITERSHADER_PARAM_REGISTER_TYPE {
492 BWRITERSPR_TEXCRDOUT,
498 BWRITERSPR_CONSTBOOL,
503 } BWRITERSHADER_PARAM_REGISTER_TYPE;
505 typedef enum _BWRITERVS_RASTOUT_OFFSETS
509 BWRITERSRO_POINT_SIZE
510 } BWRITERVS_RASTOUT_OFFSETS;
512 #define BWRITERSP_WRITEMASK_0 0x1 /* .x r */
513 #define BWRITERSP_WRITEMASK_1 0x2 /* .y g */
514 #define BWRITERSP_WRITEMASK_2 0x4 /* .z b */
515 #define BWRITERSP_WRITEMASK_3 0x8 /* .w a */
516 #define BWRITERSP_WRITEMASK_ALL 0xf /* all */
518 typedef enum _BWRITERSHADER_PARAM_DSTMOD_TYPE {
519 BWRITERSPDM_NONE = 0,
520 BWRITERSPDM_SATURATE = 1,
521 BWRITERSPDM_PARTIALPRECISION = 2,
522 BWRITERSPDM_MSAMPCENTROID = 4,
523 } BWRITERSHADER_PARAM_DSTMOD_TYPE;
525 typedef enum _BWRITERSAMPLER_TEXTURE_TYPE {
526 BWRITERSTT_UNKNOWN = 0,
530 BWRITERSTT_VOLUME = 4,
531 } BWRITERSAMPLER_TEXTURE_TYPE;
533 #define BWRITERSI_TEXLD_PROJECT 1
534 #define BWRITERSI_TEXLD_BIAS 2
536 typedef enum _BWRITERSHADER_PARAM_SRCMOD_TYPE {
537 BWRITERSPSM_NONE = 0,
551 } BWRITERSHADER_PARAM_SRCMOD_TYPE;
553 #define BWRITER_SM1_VS 0xfffe
554 #define BWRITER_SM1_PS 0xffff
556 #define BWRITERPS_VERSION(major, minor) ((BWRITER_SM1_PS << 16) | ((major) << 8) | (minor))
557 #define BWRITERVS_VERSION(major, minor) ((BWRITER_SM1_VS << 16) | ((major) << 8) | (minor))
559 #define BWRITERVS_SWIZZLE_SHIFT 16
560 #define BWRITERVS_SWIZZLE_MASK (0xFF << BWRITERVS_SWIZZLE_SHIFT)
562 #define BWRITERVS_X_X (0 << BWRITERVS_SWIZZLE_SHIFT)
563 #define BWRITERVS_X_Y (1 << BWRITERVS_SWIZZLE_SHIFT)
564 #define BWRITERVS_X_Z (2 << BWRITERVS_SWIZZLE_SHIFT)
565 #define BWRITERVS_X_W (3 << BWRITERVS_SWIZZLE_SHIFT)
567 #define BWRITERVS_Y_X (0 << (BWRITERVS_SWIZZLE_SHIFT + 2))
568 #define BWRITERVS_Y_Y (1 << (BWRITERVS_SWIZZLE_SHIFT + 2))
569 #define BWRITERVS_Y_Z (2 << (BWRITERVS_SWIZZLE_SHIFT + 2))
570 #define BWRITERVS_Y_W (3 << (BWRITERVS_SWIZZLE_SHIFT + 2))
572 #define BWRITERVS_Z_X (0 << (BWRITERVS_SWIZZLE_SHIFT + 4))
573 #define BWRITERVS_Z_Y (1 << (BWRITERVS_SWIZZLE_SHIFT + 4))
574 #define BWRITERVS_Z_Z (2 << (BWRITERVS_SWIZZLE_SHIFT + 4))
575 #define BWRITERVS_Z_W (3 << (BWRITERVS_SWIZZLE_SHIFT + 4))
577 #define BWRITERVS_W_X (0 << (BWRITERVS_SWIZZLE_SHIFT + 6))
578 #define BWRITERVS_W_Y (1 << (BWRITERVS_SWIZZLE_SHIFT + 6))
579 #define BWRITERVS_W_Z (2 << (BWRITERVS_SWIZZLE_SHIFT + 6))
580 #define BWRITERVS_W_W (3 << (BWRITERVS_SWIZZLE_SHIFT + 6))
582 #define BWRITERVS_NOSWIZZLE (BWRITERVS_X_X | BWRITERVS_Y_Y | BWRITERVS_Z_Z | BWRITERVS_W_W)
584 #define BWRITERVS_SWIZZLE_X (BWRITERVS_X_X | BWRITERVS_Y_X | BWRITERVS_Z_X | BWRITERVS_W_X)
585 #define BWRITERVS_SWIZZLE_Y (BWRITERVS_X_Y | BWRITERVS_Y_Y | BWRITERVS_Z_Y | BWRITERVS_W_Y)
586 #define BWRITERVS_SWIZZLE_Z (BWRITERVS_X_Z | BWRITERVS_Y_Z | BWRITERVS_Z_Z | BWRITERVS_W_Z)
587 #define BWRITERVS_SWIZZLE_W (BWRITERVS_X_W | BWRITERVS_Y_W | BWRITERVS_Z_W | BWRITERVS_W_W)
589 typedef enum _BWRITERDECLUSAGE {
590 BWRITERDECLUSAGE_POSITION,
591 BWRITERDECLUSAGE_BLENDWEIGHT,
592 BWRITERDECLUSAGE_BLENDINDICES,
593 BWRITERDECLUSAGE_NORMAL,
594 BWRITERDECLUSAGE_PSIZE,
595 BWRITERDECLUSAGE_TEXCOORD,
596 BWRITERDECLUSAGE_TANGENT,
597 BWRITERDECLUSAGE_BINORMAL,
598 BWRITERDECLUSAGE_TESSFACTOR,
599 BWRITERDECLUSAGE_POSITIONT,
600 BWRITERDECLUSAGE_COLOR,
601 BWRITERDECLUSAGE_FOG,
602 BWRITERDECLUSAGE_DEPTH,
603 BWRITERDECLUSAGE_SAMPLE
606 /* ps 1.x texture registers mappings */
612 struct bwriter_shader *SlAssembleShader(const char *text, char **messages) DECLSPEC_HIDDEN;
613 DWORD SlWriteBytecode(const struct bwriter_shader *shader, int dxversion, DWORD **result) DECLSPEC_HIDDEN;
614 void SlDeleteShader(struct bwriter_shader *shader) DECLSPEC_HIDDEN;
616 #define MAKE_TAG(ch0, ch1, ch2, ch3) \
617 ((DWORD)(ch0) | ((DWORD)(ch1) << 8) | \
618 ((DWORD)(ch2) << 16) | ((DWORD)(ch3) << 24 ))
619 #define TAG_Aon9 MAKE_TAG('A', 'o', 'n', '9')
620 #define TAG_DXBC MAKE_TAG('D', 'X', 'B', 'C')
621 #define TAG_ISGN MAKE_TAG('I', 'S', 'G', 'N')
622 #define TAG_OSGN MAKE_TAG('O', 'S', 'G', 'N')
623 #define TAG_OSG5 MAKE_TAG('O', 'S', 'G', '5')
624 #define TAG_PCSG MAKE_TAG('P', 'C', 'S', 'G')
625 #define TAG_RDEF MAKE_TAG('R', 'D', 'E', 'F')
626 #define TAG_SDBG MAKE_TAG('S', 'D', 'B', 'G')
627 #define TAG_STAT MAKE_TAG('S', 'T', 'A', 'T')
628 #define TAG_XNAP MAKE_TAG('X', 'N', 'A', 'P')
629 #define TAG_XNAS MAKE_TAG('X', 'N', 'A', 'S')
642 struct dxbc_section *sections;
645 HRESULT dxbc_write_blob(struct dxbc *dxbc, ID3DBlob **blob) DECLSPEC_HIDDEN;
646 void dxbc_destroy(struct dxbc *dxbc) DECLSPEC_HIDDEN;
647 HRESULT dxbc_parse(const char *data, SIZE_T data_size, struct dxbc *dxbc) DECLSPEC_HIDDEN;
648 HRESULT dxbc_add_section(struct dxbc *dxbc, DWORD tag, const char *data, DWORD data_size) DECLSPEC_HIDDEN;
649 HRESULT dxbc_init(struct dxbc *dxbc, DWORD count) DECLSPEC_HIDDEN;
651 static inline void read_dword(const char **ptr, DWORD *d)
653 memcpy(d, *ptr, sizeof(*d));
657 static inline void write_dword(char **ptr, DWORD d)
659 memcpy(*ptr, &d, sizeof(d));
663 void skip_dword_unknown(const char **ptr, unsigned int count) DECLSPEC_HIDDEN;
664 void write_dword_unknown(char **ptr, DWORD d) DECLSPEC_HIDDEN;
666 #endif /* __WINE_D3DCOMPILER_PRIVATE_H */