d3dx9: Add input dcl instruction support to the shader assembler.
[wine] / dlls / d3dx9_36 / d3dx9_36_private.h
1 /*
2  * Copyright (C) 2002 Raphael Junqueira
3  * Copyright (C) 2008 David Adam
4  * Copyright (C) 2008 Tony Wasserka
5  * Copyright (C) 2008 Stefan Dösinger
6  * Copyright (C) 2009 Matteo Bruni
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 #ifndef __WINE_D3DX9_36_PRIVATE_H
25 #define __WINE_D3DX9_36_PRIVATE_H
26
27 #include <stdarg.h>
28
29 #define COBJMACROS
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "d3dx9.h"
34
35 /* for internal use */
36 typedef enum _FormatType {
37     FORMAT_ARGB,   /* unsigned */
38     FORMAT_UNKNOWN
39 } FormatType;
40
41 typedef struct _PixelFormatDesc {
42     D3DFORMAT format;
43     BYTE bits[4];
44     BYTE shift[4];
45     UINT bytes_per_pixel;
46     FormatType type;
47 } PixelFormatDesc;
48
49 HRESULT map_view_of_file(LPCWSTR filename, LPVOID *buffer, DWORD *length);
50 HRESULT load_resource_into_memory(HMODULE module, HRSRC resinfo, LPVOID *buffer, DWORD *length);
51
52 const PixelFormatDesc *get_format_info(D3DFORMAT format);
53
54
55 extern const ID3DXBufferVtbl D3DXBuffer_Vtbl;
56
57 /* ID3DXBUFFER */
58 typedef struct ID3DXBufferImpl
59 {
60     /* IUnknown fields */
61     const ID3DXBufferVtbl *lpVtbl;
62     LONG           ref;
63
64     /* ID3DXBuffer fields */
65     DWORD         *buffer;
66     DWORD          bufferSize;
67 } ID3DXBufferImpl;
68
69
70 /* ID3DXFont */
71 typedef struct ID3DXFontImpl
72 {
73     /* IUnknown fields */
74     const ID3DXFontVtbl *lpVtbl;
75     LONG ref;
76
77     /* ID3DXFont fields */
78     IDirect3DDevice9 *device;
79     D3DXFONT_DESCW desc;
80
81     HDC hdc;
82     HFONT hfont;
83 } ID3DXFontImpl;
84
85 /* ID3DXMatrixStack */
86 typedef struct ID3DXMatrixStackImpl
87 {
88   /* IUnknown fields */
89   const ID3DXMatrixStackVtbl *lpVtbl;
90   LONG                   ref;
91
92   /* ID3DXMatrixStack fields */
93   unsigned int current;
94   unsigned int stack_size;
95   D3DXMATRIX *stack;
96 } ID3DXMatrixStackImpl;
97
98 /*ID3DXSprite */
99 typedef struct _SPRITE {
100     LPDIRECT3DTEXTURE9 texture;
101     UINT texw, texh;
102     RECT rect;
103     D3DXVECTOR3 center;
104     D3DXVECTOR3 pos;
105     D3DCOLOR color;
106 } SPRITE;
107
108 typedef struct ID3DXSpriteImpl
109 {
110     /* IUnknown fields */
111     const ID3DXSpriteVtbl *lpVtbl;
112     LONG ref;
113
114     /* ID3DXSprite fields */
115     IDirect3DDevice9 *device;
116     IDirect3DVertexDeclaration9 *vdecl;
117     IDirect3DStateBlock9 *stateblock;
118     D3DXMATRIX transform;
119     D3DXMATRIX view;
120     DWORD flags;
121     BOOL ready;
122
123     /* Store the relevant caps to prevent multiple GetDeviceCaps calls */
124     DWORD texfilter_caps;
125     DWORD maxanisotropy;
126     DWORD alphacmp_caps;
127
128     SPRITE *sprites;
129     int sprite_count;      /* number of sprites to be drawn */
130     int allocated_sprites; /* number of (pre-)allocated sprites */
131 } ID3DXSpriteImpl;
132
133 /* Shader assembler definitions */
134 typedef enum _shader_type {
135     ST_VERTEX,
136     ST_PIXEL,
137 } shader_type;
138
139 typedef enum BWRITER_COMPARISON_TYPE {
140     BWRITER_COMPARISON_NONE,
141     BWRITER_COMPARISON_GT,
142     BWRITER_COMPARISON_EQ,
143     BWRITER_COMPARISON_GE,
144     BWRITER_COMPARISON_LT,
145     BWRITER_COMPARISON_NE,
146     BWRITER_COMPARISON_LE
147 } BWRITER_COMPARISON_TYPE;
148
149 struct shader_reg {
150     DWORD                   type;
151     DWORD                   regnum;
152     struct shader_reg       *rel_reg;
153     DWORD                   srcmod;
154     union {
155         DWORD               swizzle;
156         DWORD               writemask;
157     };
158 };
159
160 struct instruction {
161     DWORD                   opcode;
162     DWORD                   dstmod;
163     DWORD                   shift;
164     BWRITER_COMPARISON_TYPE comptype;
165     BOOL                    has_dst;
166     struct shader_reg       dst;
167     struct shader_reg       *src;
168     unsigned int            num_srcs; /* For freeing the rel_regs */
169     BOOL                    has_predicate;
170     struct shader_reg       predicate;
171 };
172
173 struct declaration {
174     DWORD                   usage, usage_idx;
175     DWORD                   regnum;
176     DWORD                   writemask;
177 };
178
179 struct samplerdecl {
180     DWORD                   type;
181     DWORD                   regnum;
182     unsigned int            line_no; /* for error messages */
183 };
184
185 #define INSTRARRAY_INITIAL_SIZE 8
186 struct bwriter_shader {
187     shader_type             type;
188
189     /* Shader version selected */
190     DWORD                   version;
191
192     /* Local constants. Every constant that is not defined below is loaded from
193      * the global constant set at shader runtime
194      */
195     struct constant         **constF;
196     struct constant         **constI;
197     struct constant         **constB;
198     unsigned int            num_cf, num_ci, num_cb;
199
200     /* Declared input and output varyings */
201     struct declaration      *inputs, *outputs;
202     unsigned int            num_inputs, num_outputs;
203     struct samplerdecl      *samplers;
204     unsigned int            num_samplers;
205
206     /* Are special pixel shader 3.0 registers declared? */
207     BOOL                    vPos, vFace;
208
209     /* Array of shader instructions - The shader code itself */
210     struct instruction      **instr;
211     unsigned int            num_instrs, instr_alloc_size;
212 };
213
214 static inline LPVOID asm_alloc(SIZE_T size) {
215     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
216 }
217
218 static inline LPVOID asm_realloc(LPVOID ptr, SIZE_T size) {
219     return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
220 }
221
222 static inline BOOL asm_free(LPVOID ptr) {
223     return HeapFree(GetProcessHeap(), 0, ptr);
224 }
225
226 struct asm_parser;
227
228 /* This structure is only used in asmshader.y, but since the .l file accesses the semantic types
229  * too it has to know it as well
230  */
231 struct rel_reg {
232     BOOL            has_rel_reg;
233     DWORD           type;
234     DWORD           additional_offset;
235     DWORD           rel_regnum;
236     DWORD           swizzle;
237 };
238
239 #define MAX_SRC_REGS 4
240
241 struct src_regs {
242     struct shader_reg reg[MAX_SRC_REGS];
243     unsigned int      count;
244 };
245
246 struct asmparser_backend {
247     void (*dstreg)(struct asm_parser *This, struct instruction *instr,
248                    const struct shader_reg *dst);
249     void (*srcreg)(struct asm_parser *This, struct instruction *instr, int num,
250                    const struct shader_reg *src);
251
252     void (*predicate)(struct asm_parser *This,
253                       const struct shader_reg *predicate);
254     void (*coissue)(struct asm_parser *This);
255
256     void (*dcl_output)(struct asm_parser *This, DWORD usage, DWORD num,
257                        const struct shader_reg *reg);
258     void (*dcl_input)(struct asm_parser *This, DWORD usage, DWORD num,
259                       const struct shader_reg *reg);
260
261     void (*end)(struct asm_parser *This);
262
263     void (*instr)(struct asm_parser *This, DWORD opcode, DWORD mod, DWORD shift,
264                   BWRITER_COMPARISON_TYPE comp, const struct shader_reg *dst,
265                   const struct src_regs *srcs, int expectednsrcs);
266 };
267
268 struct instruction *alloc_instr(unsigned int srcs);
269 BOOL add_instruction(struct bwriter_shader *shader, struct instruction *instr);
270 BOOL record_declaration(struct bwriter_shader *shader, DWORD usage, DWORD usage_idx, BOOL output, DWORD regnum, DWORD writemask);
271
272 #define MESSAGEBUFFER_INITIAL_SIZE 256
273
274 struct asm_parser {
275     /* The function table of the parser implementation */
276     const struct asmparser_backend *funcs;
277
278     /* Private data follows */
279     struct bwriter_shader *shader;
280     unsigned int m3x3pad_count;
281
282     enum parse_status {
283         PARSE_SUCCESS = 0,
284         PARSE_WARN = 1,
285         PARSE_ERR = 2
286     } status;
287     char *messages;
288     unsigned int messagesize;
289     unsigned int messagecapacity;
290     unsigned int line_no;
291 };
292
293 extern struct asm_parser asm_ctx;
294
295 void create_vs30_parser(struct asm_parser *ret);
296
297 struct bwriter_shader *parse_asm_shader(char **messages);
298
299 #ifdef __GNUC__
300 #define PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
301 #else
302 #define PRINTF_ATTR(fmt,args)
303 #endif
304
305 void asmparser_message(struct asm_parser *ctx, const char *fmt, ...) PRINTF_ATTR(2,3);
306 void set_parse_status(struct asm_parser *ctx, enum parse_status status);
307
308 /* A reasonable value as initial size */
309 #define BYTECODEBUFFER_INITIAL_SIZE 32
310 struct bytecode_buffer {
311     DWORD *data;
312     DWORD size;
313     DWORD alloc_size;
314     /* For tracking rare out of memory situations without passing
315      * return values around everywhere
316      */
317     HRESULT state;
318 };
319
320 struct bc_writer; /* Predeclaration for use in vtable parameters */
321
322 typedef void (*instr_writer)(struct bc_writer *This,
323                              const struct instruction *instr,
324                              struct bytecode_buffer *buffer);
325
326 struct bytecode_backend {
327     void (*header)(struct bc_writer *This, const struct bwriter_shader *shader,
328                    struct bytecode_buffer *buffer);
329     void (*end)(struct bc_writer *This, const struct bwriter_shader *shader,
330                 struct bytecode_buffer *buffer);
331     void (*srcreg)(struct bc_writer *This, const struct shader_reg *reg,
332                    struct bytecode_buffer *buffer);
333     void (*dstreg)(struct bc_writer *This, const struct shader_reg *reg,
334                    struct bytecode_buffer *buffer, DWORD shift, DWORD mod);
335     void (*opcode)(struct bc_writer *This, const struct instruction *instr,
336                    DWORD token, struct bytecode_buffer *buffer);
337
338     const struct instr_handler_table {
339         DWORD opcode;
340         instr_writer func;
341     } *instructions;
342 };
343
344 /* Bytecode writing stuff */
345 struct bc_writer {
346     const struct bytecode_backend *funcs;
347
348     /* Avoid result checking */
349     HRESULT                       state;
350
351     DWORD                         version;
352 };
353
354 /* Debug utility routines */
355 const char *debug_print_dstmod(DWORD mod);
356 const char *debug_print_dstreg(const struct shader_reg *reg, shader_type st);
357 const char *debug_print_srcreg(const struct shader_reg *reg, shader_type st);
358 const char *debug_print_swizzle(DWORD swizzle);
359 const char *debug_print_writemask(DWORD mask);
360 const char *debug_print_comp(DWORD comp);
361 const char *debug_print_opcode(DWORD opcode);
362
363 /* Utilities for internal->d3d constant mapping */
364 DWORD d3d9_swizzle(DWORD bwriter_swizzle);
365 DWORD d3d9_writemask(DWORD bwriter_writemask);
366 DWORD d3d9_srcmod(DWORD bwriter_srcmod);
367 DWORD d3d9_dstmod(DWORD bwriter_mod);
368 DWORD d3d9_comparetype(DWORD bwriter_comparetype);
369 DWORD d3d9_register(DWORD bwriter_register);
370 DWORD d3d9_opcode(DWORD bwriter_opcode);
371
372 /* Used to signal an incorrect swizzle/writemask */
373 #define SWIZZLE_ERR ~0U
374
375 /*
376   Enumerations and defines used in the bytecode writer
377   intermediate representation
378 */
379 typedef enum _BWRITERSHADER_INSTRUCTION_OPCODE_TYPE {
380     BWRITERSIO_NOP,
381     BWRITERSIO_MOV,
382     BWRITERSIO_ADD,
383     BWRITERSIO_SUB,
384     BWRITERSIO_MAD,
385     BWRITERSIO_MUL,
386     BWRITERSIO_RCP,
387     BWRITERSIO_RSQ,
388     BWRITERSIO_DP3,
389     BWRITERSIO_DP4,
390     BWRITERSIO_MIN,
391     BWRITERSIO_MAX,
392     BWRITERSIO_SLT,
393     BWRITERSIO_SGE,
394     BWRITERSIO_EXP,
395     BWRITERSIO_LOG,
396     BWRITERSIO_LIT,
397     BWRITERSIO_DST,
398     BWRITERSIO_LRP,
399     BWRITERSIO_FRC,
400     BWRITERSIO_M4x4,
401     BWRITERSIO_M4x3,
402     BWRITERSIO_M3x4,
403     BWRITERSIO_M3x3,
404     BWRITERSIO_M3x2,
405     BWRITERSIO_CALL,
406     BWRITERSIO_CALLNZ,
407     BWRITERSIO_LOOP,
408     BWRITERSIO_RET,
409     BWRITERSIO_ENDLOOP,
410     BWRITERSIO_LABEL,
411     BWRITERSIO_DCL,
412     BWRITERSIO_POW,
413     BWRITERSIO_CRS,
414     BWRITERSIO_SGN,
415     BWRITERSIO_ABS,
416     BWRITERSIO_NRM,
417     BWRITERSIO_SINCOS,
418     BWRITERSIO_REP,
419     BWRITERSIO_ENDREP,
420     BWRITERSIO_IF,
421     BWRITERSIO_IFC,
422     BWRITERSIO_ELSE,
423     BWRITERSIO_ENDIF,
424     BWRITERSIO_BREAK,
425     BWRITERSIO_BREAKC,
426     BWRITERSIO_MOVA,
427
428     BWRITERSIO_EXPP,
429     BWRITERSIO_LOGP,
430     BWRITERSIO_SETP,
431     BWRITERSIO_TEXLDL,
432     BWRITERSIO_BREAKP,
433
434     BWRITERSIO_COMMENT,
435     BWRITERSIO_END,
436 } BWRITERSHADER_INSTRUCTION_OPCODE_TYPE;
437
438 typedef enum _BWRITERSHADER_PARAM_REGISTER_TYPE {
439     BWRITERSPR_TEMP,
440     BWRITERSPR_INPUT,
441     BWRITERSPR_CONST,
442     BWRITERSPR_ADDR,
443     BWRITERSPR_TEXTURE,
444     BWRITERSPR_RASTOUT,
445     BWRITERSPR_ATTROUT,
446     BWRITERSPR_TEXCRDOUT,
447     BWRITERSPR_OUTPUT,
448     BWRITERSPR_CONSTINT,
449     BWRITERSPR_COLOROUT,
450     BWRITERSPR_DEPTHOUT,
451     BWRITERSPR_SAMPLER,
452     BWRITERSPR_CONSTBOOL,
453     BWRITERSPR_LOOP,
454     BWRITERSPR_MISCTYPE,
455     BWRITERSPR_LABEL,
456     BWRITERSPR_PREDICATE
457 } BWRITERSHADER_PARAM_REGISTER_TYPE;
458
459 typedef enum _BWRITERVS_RASTOUT_OFFSETS
460 {
461     BWRITERSRO_POSITION,
462     BWRITERSRO_FOG,
463     BWRITERSRO_POINT_SIZE
464 } BWRITERVS_RASTOUT_OFFSETS;
465
466 #define BWRITERSP_WRITEMASK_0   0x1 /* .x r */
467 #define BWRITERSP_WRITEMASK_1   0x2 /* .y g */
468 #define BWRITERSP_WRITEMASK_2   0x4 /* .z b */
469 #define BWRITERSP_WRITEMASK_3   0x8 /* .w a */
470 #define BWRITERSP_WRITEMASK_ALL 0xf /* all */
471
472 typedef enum _BWRITERSHADER_PARAM_DSTMOD_TYPE {
473     BWRITERSPDM_NONE = 0,
474     BWRITERSPDM_SATURATE = 1,
475     BWRITERSPDM_PARTIALPRECISION = 2,
476     BWRITERSPDM_MSAMPCENTROID = 4,
477 } BWRITERSHADER_PARAM_DSTMOD_TYPE;
478
479 typedef enum _BWRITERSHADER_PARAM_SRCMOD_TYPE {
480     BWRITERSPSM_NONE = 0,
481     BWRITERSPSM_NEG,
482     BWRITERSPSM_ABS,
483     BWRITERSPSM_ABSNEG,
484     BWRITERSPSM_NOT,
485 } BWRITERSHADER_PARAM_SRCMOD_TYPE;
486
487 #define BWRITER_SM1_VS  0xfffe
488 #define BWRITER_SM1_PS  0xffff
489
490 #define BWRITERPS_VERSION(major, minor) ((BWRITER_SM1_PS << 16) | ((major) << 8) | (minor))
491 #define BWRITERVS_VERSION(major, minor) ((BWRITER_SM1_VS << 16) | ((major) << 8) | (minor))
492
493 #define BWRITERVS_SWIZZLE_SHIFT      16
494 #define BWRITERVS_SWIZZLE_MASK       (0xFF << BWRITERVS_SWIZZLE_SHIFT)
495
496 #define BWRITERVS_X_X       (0 << BWRITERVS_SWIZZLE_SHIFT)
497 #define BWRITERVS_X_Y       (1 << BWRITERVS_SWIZZLE_SHIFT)
498 #define BWRITERVS_X_Z       (2 << BWRITERVS_SWIZZLE_SHIFT)
499 #define BWRITERVS_X_W       (3 << BWRITERVS_SWIZZLE_SHIFT)
500
501 #define BWRITERVS_Y_X       (0 << (BWRITERVS_SWIZZLE_SHIFT + 2))
502 #define BWRITERVS_Y_Y       (1 << (BWRITERVS_SWIZZLE_SHIFT + 2))
503 #define BWRITERVS_Y_Z       (2 << (BWRITERVS_SWIZZLE_SHIFT + 2))
504 #define BWRITERVS_Y_W       (3 << (BWRITERVS_SWIZZLE_SHIFT + 2))
505
506 #define BWRITERVS_Z_X       (0 << (BWRITERVS_SWIZZLE_SHIFT + 4))
507 #define BWRITERVS_Z_Y       (1 << (BWRITERVS_SWIZZLE_SHIFT + 4))
508 #define BWRITERVS_Z_Z       (2 << (BWRITERVS_SWIZZLE_SHIFT + 4))
509 #define BWRITERVS_Z_W       (3 << (BWRITERVS_SWIZZLE_SHIFT + 4))
510
511 #define BWRITERVS_W_X       (0 << (BWRITERVS_SWIZZLE_SHIFT + 6))
512 #define BWRITERVS_W_Y       (1 << (BWRITERVS_SWIZZLE_SHIFT + 6))
513 #define BWRITERVS_W_Z       (2 << (BWRITERVS_SWIZZLE_SHIFT + 6))
514 #define BWRITERVS_W_W       (3 << (BWRITERVS_SWIZZLE_SHIFT + 6))
515
516 #define BWRITERVS_NOSWIZZLE (BWRITERVS_X_X | BWRITERVS_Y_Y | BWRITERVS_Z_Z | BWRITERVS_W_W)
517
518 #define BWRITERVS_SWIZZLE_X (BWRITERVS_X_X | BWRITERVS_Y_X | BWRITERVS_Z_X | BWRITERVS_W_X)
519 #define BWRITERVS_SWIZZLE_Y (BWRITERVS_X_Y | BWRITERVS_Y_Y | BWRITERVS_Z_Y | BWRITERVS_W_Y)
520 #define BWRITERVS_SWIZZLE_Z (BWRITERVS_X_Z | BWRITERVS_Y_Z | BWRITERVS_Z_Z | BWRITERVS_W_Z)
521 #define BWRITERVS_SWIZZLE_W (BWRITERVS_X_W | BWRITERVS_Y_W | BWRITERVS_Z_W | BWRITERVS_W_W)
522
523 typedef enum _BWRITERDECLUSAGE {
524     BWRITERDECLUSAGE_POSITION,
525     BWRITERDECLUSAGE_BLENDWEIGHT,
526     BWRITERDECLUSAGE_BLENDINDICES,
527     BWRITERDECLUSAGE_NORMAL,
528     BWRITERDECLUSAGE_PSIZE,
529     BWRITERDECLUSAGE_TEXCOORD,
530     BWRITERDECLUSAGE_TANGENT,
531     BWRITERDECLUSAGE_BINORMAL,
532     BWRITERDECLUSAGE_TESSFACTOR,
533     BWRITERDECLUSAGE_POSITIONT,
534     BWRITERDECLUSAGE_COLOR,
535     BWRITERDECLUSAGE_FOG,
536     BWRITERDECLUSAGE_DEPTH,
537     BWRITERDECLUSAGE_SAMPLE
538 } BWRITERDECLUSAGE;
539
540 struct bwriter_shader *SlAssembleShader(const char *text, char **messages);
541 DWORD SlWriteBytecode(const struct bwriter_shader *shader, int dxversion, DWORD **result);
542 void SlDeleteShader(struct bwriter_shader *shader);
543
544 #endif /* __WINE_D3DX9_36_PRIVATE_H */