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);
45 struct d3dcompiler_blob
47 const struct ID3D10BlobVtbl *vtbl;
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;
59 /* Shader assembler definitions */
60 typedef enum _shader_type {
65 typedef enum BWRITER_COMPARISON_TYPE {
66 BWRITER_COMPARISON_NONE,
67 BWRITER_COMPARISON_GT,
68 BWRITER_COMPARISON_EQ,
69 BWRITER_COMPARISON_GE,
70 BWRITER_COMPARISON_LT,
71 BWRITER_COMPARISON_NE,
73 } BWRITER_COMPARISON_TYPE;
88 struct shader_reg *rel_reg;
100 BWRITER_COMPARISON_TYPE comptype;
102 struct shader_reg dst;
103 struct shader_reg *src;
104 unsigned int num_srcs; /* For freeing the rel_regs */
106 struct shader_reg predicate;
111 DWORD usage, usage_idx;
124 #define INSTRARRAY_INITIAL_SIZE 8
125 struct bwriter_shader {
128 /* Shader version selected */
131 /* Local constants. Every constant that is not defined below is loaded from
132 * the global constant set at shader runtime
134 struct constant **constF;
135 struct constant **constI;
136 struct constant **constB;
137 unsigned int num_cf, num_ci, num_cb;
139 /* Declared input and output varyings */
140 struct declaration *inputs, *outputs;
141 unsigned int num_inputs, num_outputs;
142 struct samplerdecl *samplers;
143 unsigned int num_samplers;
145 /* Are special pixel shader 3.0 registers declared? */
148 /* Array of shader instructions - The shader code itself */
149 struct instruction **instr;
150 unsigned int num_instrs, instr_alloc_size;
153 static inline LPVOID asm_alloc(SIZE_T size) {
154 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
157 static inline LPVOID asm_realloc(LPVOID ptr, SIZE_T size) {
158 return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
161 static inline BOOL asm_free(LPVOID ptr) {
162 return HeapFree(GetProcessHeap(), 0, ptr);
167 /* This structure is only used in asmshader.y, but since the .l file accesses the semantic types
168 * too it has to know it as well
173 DWORD additional_offset;
178 #define MAX_SRC_REGS 4
181 struct shader_reg reg[MAX_SRC_REGS];
185 struct asmparser_backend {
186 void (*constF)(struct asm_parser *This, DWORD reg, float x, float y, float z, float w);
187 void (*constI)(struct asm_parser *This, DWORD reg, INT x, INT y, INT z, INT w);
188 void (*constB)(struct asm_parser *This, DWORD reg, BOOL x);
190 void (*dstreg)(struct asm_parser *This, struct instruction *instr,
191 const struct shader_reg *dst);
192 void (*srcreg)(struct asm_parser *This, struct instruction *instr, int num,
193 const struct shader_reg *src);
195 void (*predicate)(struct asm_parser *This,
196 const struct shader_reg *predicate);
197 void (*coissue)(struct asm_parser *This);
199 void (*dcl_output)(struct asm_parser *This, DWORD usage, DWORD num,
200 const struct shader_reg *reg);
201 void (*dcl_input)(struct asm_parser *This, DWORD usage, DWORD num,
202 DWORD mod, const struct shader_reg *reg);
203 void (*dcl_sampler)(struct asm_parser *This, DWORD samptype, DWORD mod,
204 DWORD regnum, unsigned int line_no);
206 void (*end)(struct asm_parser *This);
208 void (*instr)(struct asm_parser *This, DWORD opcode, DWORD mod, DWORD shift,
209 BWRITER_COMPARISON_TYPE comp, const struct shader_reg *dst,
210 const struct src_regs *srcs, int expectednsrcs);
213 struct instruction *alloc_instr(unsigned int srcs);
214 BOOL add_instruction(struct bwriter_shader *shader, struct instruction *instr);
215 BOOL add_constF(struct bwriter_shader *shader, DWORD reg, float x, float y, float z, float w);
216 BOOL add_constI(struct bwriter_shader *shader, DWORD reg, INT x, INT y, INT z, INT w);
217 BOOL add_constB(struct bwriter_shader *shader, DWORD reg, BOOL x);
218 BOOL record_declaration(struct bwriter_shader *shader, DWORD usage,
219 DWORD usage_idx, DWORD mod, BOOL output,
220 DWORD regnum, DWORD writemask, BOOL builtin);
221 BOOL record_sampler(struct bwriter_shader *shader, DWORD samptype,
222 DWORD mod, DWORD regnum);
224 #define MESSAGEBUFFER_INITIAL_SIZE 256
227 /* The function table of the parser implementation */
228 const struct asmparser_backend *funcs;
230 /* Private data follows */
231 struct bwriter_shader *shader;
232 unsigned int m3x3pad_count;
240 unsigned int messagesize;
241 unsigned int messagecapacity;
242 unsigned int line_no;
245 extern struct asm_parser asm_ctx;
247 void create_vs10_parser(struct asm_parser *ret);
248 void create_vs11_parser(struct asm_parser *ret);
249 void create_vs20_parser(struct asm_parser *ret);
250 void create_vs2x_parser(struct asm_parser *ret);
251 void create_vs30_parser(struct asm_parser *ret);
252 void create_ps10_parser(struct asm_parser *ret);
253 void create_ps11_parser(struct asm_parser *ret);
254 void create_ps12_parser(struct asm_parser *ret);
255 void create_ps13_parser(struct asm_parser *ret);
256 void create_ps14_parser(struct asm_parser *ret);
257 void create_ps20_parser(struct asm_parser *ret);
258 void create_ps2x_parser(struct asm_parser *ret);
259 void create_ps30_parser(struct asm_parser *ret);
261 struct bwriter_shader *parse_asm_shader(char **messages);
264 #define PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
266 #define PRINTF_ATTR(fmt,args)
269 void asmparser_message(struct asm_parser *ctx, const char *fmt, ...) PRINTF_ATTR(2,3);
270 void set_parse_status(struct asm_parser *ctx, enum parse_status status);
272 /* A reasonable value as initial size */
273 #define BYTECODEBUFFER_INITIAL_SIZE 32
274 struct bytecode_buffer {
278 /* For tracking rare out of memory situations without passing
279 * return values around everywhere
284 struct bc_writer; /* Predeclaration for use in vtable parameters */
286 typedef void (*instr_writer)(struct bc_writer *This,
287 const struct instruction *instr,
288 struct bytecode_buffer *buffer);
290 struct bytecode_backend {
291 void (*header)(struct bc_writer *This, const struct bwriter_shader *shader,
292 struct bytecode_buffer *buffer);
293 void (*end)(struct bc_writer *This, const struct bwriter_shader *shader,
294 struct bytecode_buffer *buffer);
295 void (*srcreg)(struct bc_writer *This, const struct shader_reg *reg,
296 struct bytecode_buffer *buffer);
297 void (*dstreg)(struct bc_writer *This, const struct shader_reg *reg,
298 struct bytecode_buffer *buffer, DWORD shift, DWORD mod);
299 void (*opcode)(struct bc_writer *This, const struct instruction *instr,
300 DWORD token, struct bytecode_buffer *buffer);
302 const struct instr_handler_table {
308 /* Bytecode writing stuff */
310 const struct bytecode_backend *funcs;
312 /* Avoid result checking */
317 /* Vertex shader varying mapping */
326 /* Pixel shader specific members */
331 /* Debug utility routines */
332 const char *debug_print_srcmod(DWORD mod);
333 const char *debug_print_dstmod(DWORD mod);
334 const char *debug_print_shift(DWORD shift);
335 const char *debug_print_dstreg(const struct shader_reg *reg);
336 const char *debug_print_srcreg(const struct shader_reg *reg);
337 const char *debug_print_comp(DWORD comp);
338 const char *debug_print_opcode(DWORD opcode);
340 /* Utilities for internal->d3d constant mapping */
341 DWORD d3d9_swizzle(DWORD bwriter_swizzle);
342 DWORD d3d9_writemask(DWORD bwriter_writemask);
343 DWORD d3d9_srcmod(DWORD bwriter_srcmod);
344 DWORD d3d9_dstmod(DWORD bwriter_mod);
345 DWORD d3d9_comparetype(DWORD bwriter_comparetype);
346 DWORD d3d9_sampler(DWORD bwriter_sampler);
347 DWORD d3d9_register(DWORD bwriter_register);
348 DWORD d3d9_opcode(DWORD bwriter_opcode);
350 /* Used to signal an incorrect swizzle/writemask */
351 #define SWIZZLE_ERR ~0U
354 Enumerations and defines used in the bytecode writer
355 intermediate representation
357 typedef enum _BWRITERSHADER_INSTRUCTION_OPCODE_TYPE {
413 BWRITERSIO_TEXREG2AR,
414 BWRITERSIO_TEXREG2GB,
415 BWRITERSIO_TEXM3x2PAD,
416 BWRITERSIO_TEXM3x2TEX,
417 BWRITERSIO_TEXM3x3PAD,
418 BWRITERSIO_TEXM3x3TEX,
419 BWRITERSIO_TEXM3x3SPEC,
420 BWRITERSIO_TEXM3x3VSPEC,
425 BWRITERSIO_TEXREG2RGB,
426 BWRITERSIO_TEXDP3TEX,
427 BWRITERSIO_TEXM3x2DEPTH,
446 } BWRITERSHADER_INSTRUCTION_OPCODE_TYPE;
448 typedef enum _BWRITERSHADER_PARAM_REGISTER_TYPE {
456 BWRITERSPR_TEXCRDOUT,
462 BWRITERSPR_CONSTBOOL,
467 } BWRITERSHADER_PARAM_REGISTER_TYPE;
469 typedef enum _BWRITERVS_RASTOUT_OFFSETS
473 BWRITERSRO_POINT_SIZE
474 } BWRITERVS_RASTOUT_OFFSETS;
476 #define BWRITERSP_WRITEMASK_0 0x1 /* .x r */
477 #define BWRITERSP_WRITEMASK_1 0x2 /* .y g */
478 #define BWRITERSP_WRITEMASK_2 0x4 /* .z b */
479 #define BWRITERSP_WRITEMASK_3 0x8 /* .w a */
480 #define BWRITERSP_WRITEMASK_ALL 0xf /* all */
482 typedef enum _BWRITERSHADER_PARAM_DSTMOD_TYPE {
483 BWRITERSPDM_NONE = 0,
484 BWRITERSPDM_SATURATE = 1,
485 BWRITERSPDM_PARTIALPRECISION = 2,
486 BWRITERSPDM_MSAMPCENTROID = 4,
487 } BWRITERSHADER_PARAM_DSTMOD_TYPE;
489 typedef enum _BWRITERSAMPLER_TEXTURE_TYPE {
490 BWRITERSTT_UNKNOWN = 0,
494 BWRITERSTT_VOLUME = 4,
495 } BWRITERSAMPLER_TEXTURE_TYPE;
497 #define BWRITERSI_TEXLD_PROJECT 1
498 #define BWRITERSI_TEXLD_BIAS 2
500 typedef enum _BWRITERSHADER_PARAM_SRCMOD_TYPE {
501 BWRITERSPSM_NONE = 0,
515 } BWRITERSHADER_PARAM_SRCMOD_TYPE;
517 #define BWRITER_SM1_VS 0xfffe
518 #define BWRITER_SM1_PS 0xffff
520 #define BWRITERPS_VERSION(major, minor) ((BWRITER_SM1_PS << 16) | ((major) << 8) | (minor))
521 #define BWRITERVS_VERSION(major, minor) ((BWRITER_SM1_VS << 16) | ((major) << 8) | (minor))
523 #define BWRITERVS_SWIZZLE_SHIFT 16
524 #define BWRITERVS_SWIZZLE_MASK (0xFF << BWRITERVS_SWIZZLE_SHIFT)
526 #define BWRITERVS_X_X (0 << BWRITERVS_SWIZZLE_SHIFT)
527 #define BWRITERVS_X_Y (1 << BWRITERVS_SWIZZLE_SHIFT)
528 #define BWRITERVS_X_Z (2 << BWRITERVS_SWIZZLE_SHIFT)
529 #define BWRITERVS_X_W (3 << BWRITERVS_SWIZZLE_SHIFT)
531 #define BWRITERVS_Y_X (0 << (BWRITERVS_SWIZZLE_SHIFT + 2))
532 #define BWRITERVS_Y_Y (1 << (BWRITERVS_SWIZZLE_SHIFT + 2))
533 #define BWRITERVS_Y_Z (2 << (BWRITERVS_SWIZZLE_SHIFT + 2))
534 #define BWRITERVS_Y_W (3 << (BWRITERVS_SWIZZLE_SHIFT + 2))
536 #define BWRITERVS_Z_X (0 << (BWRITERVS_SWIZZLE_SHIFT + 4))
537 #define BWRITERVS_Z_Y (1 << (BWRITERVS_SWIZZLE_SHIFT + 4))
538 #define BWRITERVS_Z_Z (2 << (BWRITERVS_SWIZZLE_SHIFT + 4))
539 #define BWRITERVS_Z_W (3 << (BWRITERVS_SWIZZLE_SHIFT + 4))
541 #define BWRITERVS_W_X (0 << (BWRITERVS_SWIZZLE_SHIFT + 6))
542 #define BWRITERVS_W_Y (1 << (BWRITERVS_SWIZZLE_SHIFT + 6))
543 #define BWRITERVS_W_Z (2 << (BWRITERVS_SWIZZLE_SHIFT + 6))
544 #define BWRITERVS_W_W (3 << (BWRITERVS_SWIZZLE_SHIFT + 6))
546 #define BWRITERVS_NOSWIZZLE (BWRITERVS_X_X | BWRITERVS_Y_Y | BWRITERVS_Z_Z | BWRITERVS_W_W)
548 #define BWRITERVS_SWIZZLE_X (BWRITERVS_X_X | BWRITERVS_Y_X | BWRITERVS_Z_X | BWRITERVS_W_X)
549 #define BWRITERVS_SWIZZLE_Y (BWRITERVS_X_Y | BWRITERVS_Y_Y | BWRITERVS_Z_Y | BWRITERVS_W_Y)
550 #define BWRITERVS_SWIZZLE_Z (BWRITERVS_X_Z | BWRITERVS_Y_Z | BWRITERVS_Z_Z | BWRITERVS_W_Z)
551 #define BWRITERVS_SWIZZLE_W (BWRITERVS_X_W | BWRITERVS_Y_W | BWRITERVS_Z_W | BWRITERVS_W_W)
553 typedef enum _BWRITERDECLUSAGE {
554 BWRITERDECLUSAGE_POSITION,
555 BWRITERDECLUSAGE_BLENDWEIGHT,
556 BWRITERDECLUSAGE_BLENDINDICES,
557 BWRITERDECLUSAGE_NORMAL,
558 BWRITERDECLUSAGE_PSIZE,
559 BWRITERDECLUSAGE_TEXCOORD,
560 BWRITERDECLUSAGE_TANGENT,
561 BWRITERDECLUSAGE_BINORMAL,
562 BWRITERDECLUSAGE_TESSFACTOR,
563 BWRITERDECLUSAGE_POSITIONT,
564 BWRITERDECLUSAGE_COLOR,
565 BWRITERDECLUSAGE_FOG,
566 BWRITERDECLUSAGE_DEPTH,
567 BWRITERDECLUSAGE_SAMPLE
570 /* ps 1.x texture registers mappings */
576 struct bwriter_shader *SlAssembleShader(const char *text, char **messages);
577 DWORD SlWriteBytecode(const struct bwriter_shader *shader, int dxversion, DWORD **result);
578 void SlDeleteShader(struct bwriter_shader *shader);
580 #define MAKE_TAG(ch0, ch1, ch2, ch3) \
581 ((DWORD)(ch0) | ((DWORD)(ch1) << 8) | \
582 ((DWORD)(ch2) << 16) | ((DWORD)(ch3) << 24 ))
583 #define TAG_DXBC MAKE_TAG('D', 'X', 'B', 'C')
584 #define TAG_ISGN MAKE_TAG('I', 'S', 'G', 'N')
597 struct dxbc_section *sections;
600 HRESULT dxbc_write_blob(struct dxbc *dxbc, ID3DBlob **blob) DECLSPEC_HIDDEN;
601 void dxbc_destroy(struct dxbc *dxbc) DECLSPEC_HIDDEN;
602 HRESULT dxbc_parse(const char *data, SIZE_T data_size, struct dxbc *dxbc) DECLSPEC_HIDDEN;
603 HRESULT dxbc_add_section(struct dxbc *dxbc, DWORD tag, const char *data, DWORD data_size) DECLSPEC_HIDDEN;
604 HRESULT dxbc_init(struct dxbc *dxbc, DWORD count) DECLSPEC_HIDDEN;
606 static inline void read_dword(const char **ptr, DWORD *d)
608 memcpy(d, *ptr, sizeof(*d));
612 static inline void write_dword(char **ptr, DWORD d)
614 memcpy(*ptr, &d, sizeof(d));
618 void skip_dword_unknown(const char **ptr, unsigned int count) DECLSPEC_HIDDEN;
619 void write_dword_unknown(char **ptr, DWORD d) DECLSPEC_HIDDEN;
621 #endif /* __WINE_D3DCOMPILER_PRIVATE_H */