Debian Codegraph @

mojoshader_internal.h

raw symbols SCIP
1 #ifndef _INCLUDE_MOJOSHADER_INTERNAL_H_
2 #define _INCLUDE_MOJOSHADER_INTERNAL_H_
3
4 #ifndef __MOJOSHADER_INTERNAL__
5 #error Do not include this header from your applications.
6 #endif
7
8 // Shader bytecode format is described at MSDN:
9 // http://msdn.microsoft.com/en-us/library/ff569705.aspx
10
11 #ifdef MOJOSHADER_USE_SDL_STDLIB
12 #ifdef USE_SDL3 /* Private define, for now */
13 #include <SDL3/SDL_assert.h>
14 #include <SDL3/SDL_endian.h>
15 #include <SDL3/SDL_stdinc.h>
16 #include <SDL3/SDL_loadso.h>
17 #else
18 #include <SDL_assert.h>
19 #include <SDL_endian.h>
20 #include <SDL_stdinc.h>
21 #include <SDL_loadso.h>
22 #endif
23 #include <math.h> /* Needed for isinf/isnan :( */
24
25 /* FIXME: These includes are needed for alloca :( */
26 #include <stdlib.h>
27 #if defined(__linux__) || defined(__sun)
28 #include <alloca.h>
29 #endif
30 #ifdef _MSC_VER
31 #include <malloc.h>
32 #endif
33
34 /* stdint.h */
35 typedef Uint8 uint8;
36 typedef Uint16 uint16;
37 typedef Uint32 uint32;
38 typedef Sint32 int32;
39 typedef Sint64 int64;
40 typedef Uint64 uint64;
41
42 /* assert.h */
43 #define assert SDL_assert
44
45 /* stdlib.h */
46 #define malloc SDL_malloc
47 #define free SDL_free
48
49 /* stdio.h */
50 #define sscanf SDL_sscanf
51 #ifdef snprintf
52 #undef snprintf
53 #endif
54 #define snprintf SDL_snprintf
55 #ifdef vsnprintf
56 #undef vsnprintf
57 #endif
58 #define vsnprintf SDL_vsnprintf
59
60 /* math.h */
61 #define acos SDL_acos
62 #define asin SDL_asin
63 #define atan SDL_atan
64 #define atan2 SDL_atan2
65 #define cos SDL_cos
66 #define exp SDL_exp
67 #define floor SDL_floor
68 #define log SDL_log
69 #define sin SDL_sin
70 #define sqrt SDL_sqrt
71
72 /* string.h */
73 #ifdef memcmp
74 #undef memcmp
75 #endif
76 #define memcmp SDL_memcmp
77 #ifdef memcpy
78 #undef memcpy
79 #endif
80 #define memcpy SDL_memcpy
81 #ifdef memset
82 #undef memset
83 #endif
84 #define memset SDL_memset
85 #ifdef strchr
86 #undef strchr
87 #endif
88 #define strchr SDL_strchr
89 #ifdef strcmp
90 #undef strcmp
91 #endif
92 #define strcmp SDL_strcmp
93 #ifdef strlen
94 #undef strlen
95 #endif
96 #define strlen SDL_strlen
97 #ifdef strncmp
98 #undef strncmp
99 #endif
100 #define strncmp SDL_strncmp
101 #ifdef strstr
102 #undef strstr
103 #endif
104 #define strstr SDL_strstr
105 #ifdef strcat
106 #undef strcat
107 #endif
108 /* TODO: Move MojoShader away from strcat! This len is awful! */
109 #define strcat(dst, src) SDL_strlcat(dst, src, SDL_strlen(src) + 1)
110 #ifdef strcpy
111 #undef strcpy
112 #endif
113 /* TODO: Move MojoShader away from strcpy! This len is awful! */
114 #define strcpy(dst, src) SDL_strlcpy(dst, src, SDL_strlen(src) + 1)
115
116 /* ctype.h */
117 #ifdef isalnum
118 #undef isalnum
119 #endif
120 #define isalnum SDL_isalnum
121
122 /* endian.h */
123 #define MOJOSHADER_BIG_ENDIAN (SDL_BYTEORDER == SDL_BIG_ENDIAN)
124
125 /* dlfcn.h */
126 #define dlopen(a, b) SDL_LoadObject(a)
127 #define dlclose SDL_UnloadObject
128 #define dlsym SDL_LoadFunction
129 #else /* MOJOSHADER_USE_SDL_STDLIB */
130 #include <stdio.h>
131 #include <string.h>
132 #include <stdlib.h>
133 #include <stdarg.h>
134 #include <assert.h>
135
136 #ifdef __BYTE_ORDER__
137 #define MOJOSHADER_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
138 #elif defined(__linux__)
139 #include <endian.h>
140 #define MOJOSHADER_BIG_ENDIAN (__BYTE_ORDER == __BIG_ENDIAN)
141 #else
142 /* TODO: Detect little endian PowerPC? */
143 #if defined(__POWERPC__) || defined(__powerpc__)
144 #define MOJOSHADER_BIG_ENDIAN 1
145 #else
146 #define MOJOSHADER_BIG_ENDIAN 0
147 #endif
148 #endif
149 #endif /* MOJOSHADER_USE_SDL_STDLIB */
150
151 #include "mojoshader.h"
152
153 #define DEBUG_LEXER 0
154 #define DEBUG_PREPROCESSOR 0
155 #define DEBUG_ASSEMBLER_PARSER 0
156 #define DEBUG_COMPILER_PARSER 0
157 #define DEBUG_TOKENIZER \
158 (DEBUG_PREPROCESSOR || DEBUG_ASSEMBLER_PARSER || DEBUG_LEXER)
159
160 // This is the highest shader version we currently support.
161
162 #define MAX_SHADER_MAJOR 3
163 #define MAX_SHADER_MINOR 255 // vs_3_sw
164
165
166 // If SUPPORT_PROFILE_* isn't defined, we assume an implicit desire to support.
167 // You get all the profiles unless you go out of your way to disable them.
168
169 #ifndef SUPPORT_PROFILE_D3D
170 #define SUPPORT_PROFILE_D3D 1
171 #endif
172
173 #ifndef SUPPORT_PROFILE_BYTECODE
174 #define SUPPORT_PROFILE_BYTECODE 1
175 #endif
176
177 #ifndef SUPPORT_PROFILE_HLSL
178 #define SUPPORT_PROFILE_HLSL 1
179 #endif
180
181 #ifndef SUPPORT_PROFILE_GLSL
182 #define SUPPORT_PROFILE_GLSL 1
183 #endif
184
185 #ifndef SUPPORT_PROFILE_GLSL120
186 #define SUPPORT_PROFILE_GLSL120 1
187 #endif
188
189 #ifndef SUPPORT_PROFILE_GLSLES
190 #define SUPPORT_PROFILE_GLSLES 1
191 #endif
192
193 #ifndef SUPPORT_PROFILE_GLSLES3
194 #define SUPPORT_PROFILE_GLSLES3 1
195 #endif
196
197 #ifndef SUPPORT_PROFILE_ARB1
198 #define SUPPORT_PROFILE_ARB1 1
199 #endif
200
201 #ifndef SUPPORT_PROFILE_ARB1_NV
202 #define SUPPORT_PROFILE_ARB1_NV 1
203 #endif
204
205 #ifndef SUPPORT_PROFILE_METAL
206 #define SUPPORT_PROFILE_METAL 1
207 #endif
208
209 #ifndef SUPPORT_PROFILE_SPIRV
210 #define SUPPORT_PROFILE_SPIRV 1
211 #endif
212
213 #ifndef SUPPORT_PROFILE_GLSPIRV
214 #define SUPPORT_PROFILE_GLSPIRV 1
215 #endif
216
217 #if SUPPORT_PROFILE_ARB1_NV && !SUPPORT_PROFILE_ARB1
218 #error nv profiles require arb1 profile. Fix your build.
219 #endif
220
221 #if SUPPORT_PROFILE_GLSL120 && !SUPPORT_PROFILE_GLSL
222 #error glsl120 profile requires glsl profile. Fix your build.
223 #endif
224
225 #if SUPPORT_PROFILE_GLSLES && !SUPPORT_PROFILE_GLSL
226 #error glsles profile requires glsl profile. Fix your build.
227 #endif
228
229 #if SUPPORT_PROFILE_GLSLES3 && !SUPPORT_PROFILE_GLSLES
230 #error glsles3 profile requires glsles profile. Fix your build.
231 #endif
232
233
234 #if SUPPORT_PROFILE_GLSPIRV && !SUPPORT_PROFILE_SPIRV
235 #error glspirv profile requires spirv profile. Fix your build.
236 #endif
237
238 // Microsoft's preprocessor has some quirks. In some ways, it doesn't work
239 // like you'd expect a C preprocessor to function.
240 #ifndef MATCH_MICROSOFT_PREPROCESSOR
241 #define MATCH_MICROSOFT_PREPROCESSOR 1
242 #endif
243
244 // Other stuff you can disable...
245
246 #ifdef MOJOSHADER_EFFECT_SUPPORT
247 void MOJOSHADER_runPreshader(const MOJOSHADER_preshader*, float*);
248 #endif
249
250
251 // Get basic wankery out of the way here...
252
253 #ifdef _WINDOWS
254 #define ENDLINE_STR "\r\n"
255 #else
256 #define ENDLINE_STR "\n"
257 #endif
258
259 typedef unsigned int uint; // this is a printf() helper. don't use for code.
260
261 // Locale-independent float printing replacement for snprintf
262 size_t MOJOSHADER_printFloat(char *text, size_t maxlen, float arg);
263
264 #ifdef _MSC_VER
265 #include <float.h>
266 #define isnan _isnan // !!! FIXME: not a safe replacement!
267 #if _MSC_VER < 1900 // pre MSVC 2015
268 #define isinf(x) (!_finite(x)) // FIXME: not a safe replacement!
269 #endif
270 #define va_copy(a, b) a = b
271 #endif
272
273 #ifndef MOJOSHADER_USE_SDL_STDLIB
274 #ifdef _MSC_VER
275 #include <malloc.h>
276 #define snprintf _snprintf // !!! FIXME: not a safe replacement!
277 #define vsnprintf _vsnprintf // !!! FIXME: not a safe replacement!
278 #define strcasecmp stricmp
279 #define strncasecmp strnicmp
280 typedef unsigned __int8 uint8;
281 typedef unsigned __int16 uint16;
282 typedef unsigned __int32 uint32;
283 typedef unsigned __int64 uint64;
284 typedef __int32 int32;
285 typedef __int64 int64;
286 #ifdef _WIN64
287 typedef __int64 ssize_t;
288 #elif defined _WIN32
289 typedef __int32 ssize_t;
290 #else
291 #error Please define your platform.
292 #endif
293 // Warning Level 4 considered harmful. :)
294 #pragma warning(disable: 4100) // "unreferenced formal parameter"
295 #pragma warning(disable: 4389) // "signed/unsigned mismatch"
296 #else
297 #include <stdint.h>
298 typedef uint8_t uint8;
299 typedef uint16_t uint16;
300 typedef uint32_t uint32;
301 typedef int32_t int32;
302 typedef int64_t int64;
303 typedef uint64_t uint64;
304 #endif
305 #endif /* MOJOSHADER_USE_SDL_STDLIB */
306
307 #ifdef __GNUC__
308 #define ISPRINTF(x,y) __attribute__((format (printf, x, y)))
309 #else
310 #define ISPRINTF(x,y)
311 #endif
312
313 #define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
314
315
316 // Byteswap magic...
317
318 #if (defined(__GNUC__) && MOJOSHADER_BIG_ENDIAN && (defined(__POWERPC__) || defined(__powerpc__)))
319 static inline uint32 SWAP32(uint32 x)
320 {
321 __asm__ __volatile__("lwbrx %0,0,%1" : "=r" (x) : "r" (&x));
322 return x;
323 } // SWAP32
324 static inline uint16 SWAP16(uint16 x)
325 {
326 __asm__ __volatile__("lhbrx %0,0,%1" : "=r" (x) : "r" (&x));
327 return x;
328 } // SWAP16
329 #elif MOJOSHADER_BIG_ENDIAN
330 static inline uint32 SWAP32(uint32 x)
331 {
332 return ( (((x) >> 24) & 0x000000FF) | (((x) >> 8) & 0x0000FF00) |
333 (((x) << 8) & 0x00FF0000) | (((x) << 24) & 0xFF000000) );
334 } // SWAP32
335 static inline uint16 SWAP16(uint16 x)
336 {
337 return ( (((x) >> 8) & 0x00FF) | (((x) << 8) & 0xFF00) );
338 } // SWAP16
339 #else
340 # define SWAP16(x) (x)
341 # define SWAP32(x) (x)
342 #endif
343
344 #define SWAPDBL(x) (x) // !!! FIXME
345
346 static inline int Min(const int a, const int b)
347 {
348 return ((a < b) ? a : b);
349 } // Min
350
351
352 // Hashtables...
353
354 typedef struct HashTable HashTable;
355 typedef uint32 (*HashTable_HashFn)(const void *key, void *data);
356 typedef int (*HashTable_KeyMatchFn)(const void *a, const void *b, void *data);
357 typedef void (*HashTable_NukeFn)(const void *ctx, const void *key, const void *value, void *data);
358
359 HashTable *hash_create(void *data, const HashTable_HashFn hashfn,
360 const HashTable_KeyMatchFn keymatchfn,
361 const HashTable_NukeFn nukefn,
362 const int stackable,
363 MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
364 void hash_destroy(HashTable *table, const void *ctx);
365 int hash_insert(HashTable *table, const void *key, const void *value);
366 int hash_remove(HashTable *table, const void *key, const void *ctx);
367 int hash_find(const HashTable *table, const void *key, const void **_value);
368 int hash_iter(const HashTable *table, const void *key, const void **_value, void **iter);
369 int hash_iter_keys(const HashTable *table, const void **_key, void **iter);
370
371 uint32 hash_hash_string(const void *sym, void *unused);
372 int hash_keymatch_string(const void *a, const void *b, void *unused);
373
374
375 // String -> String map ...
376 typedef HashTable StringMap;
377 StringMap *stringmap_create(const int copy, MOJOSHADER_malloc m,
378 MOJOSHADER_free f, void *d);
379 void stringmap_destroy(StringMap *smap);
380 int stringmap_insert(StringMap *smap, const char *key, const char *value);
381 int stringmap_remove(StringMap *smap, const char *key);
382 int stringmap_find(const StringMap *smap, const char *key, const char **_val);
383
384
385 // String caching...
386
387 typedef struct StringCache StringCache;
388 StringCache *stringcache_create(MOJOSHADER_malloc m,MOJOSHADER_free f,void *d);
389 const char *stringcache(StringCache *cache, const char *str);
390 const char *stringcache_len(StringCache *cache, const char *str,
391 const unsigned int len);
392 const char *stringcache_fmt(StringCache *cache, const char *fmt, ...);
393 int stringcache_iscached(StringCache *cache, const char *str);
394 void stringcache_destroy(StringCache *cache);
395
396
397 // Error lists...
398
399 typedef struct ErrorList ErrorList;
400 ErrorList *errorlist_create(MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
401 int errorlist_add(ErrorList *list, const char *fname,
402 const int errpos, const char *str);
403 int errorlist_add_fmt(ErrorList *list, const char *fname,
404 const int errpos, const char *fmt, ...) ISPRINTF(4,5);
405 int errorlist_add_va(ErrorList *list, const char *_fname,
406 const int errpos, const char *fmt, va_list va);
407 int errorlist_count(ErrorList *list);
408 MOJOSHADER_error *errorlist_flatten(ErrorList *list); // resets the list!
409 void errorlist_destroy(ErrorList *list);
410
411
412
413 // Dynamic buffers...
414
415 typedef struct BufferBlock
416 {
417 uint8 *data;
418 size_t bytes;
419 struct BufferBlock *next;
420 } BufferBlock;
421 typedef struct Buffer
422 {
423 size_t total_bytes;
424 BufferBlock *head;
425 BufferBlock *tail;
426 size_t block_size;
427 MOJOSHADER_malloc m;
428 MOJOSHADER_free f;
429 void *d;
430 } Buffer;
431 Buffer *buffer_create(size_t blksz,MOJOSHADER_malloc m,MOJOSHADER_free f,void *d);
432 char *buffer_reserve(Buffer *buffer, const size_t len);
433 int buffer_append(Buffer *buffer, const void *_data, size_t len);
434 int buffer_append_fmt(Buffer *buffer, const char *fmt, ...) ISPRINTF(2,3);
435 int buffer_append_va(Buffer *buffer, const char *fmt, va_list va);
436 size_t buffer_size(Buffer *buffer);
437 void buffer_empty(Buffer *buffer);
438 char *buffer_flatten(Buffer *buffer);
439 char *buffer_merge(Buffer **buffers, const size_t n, size_t *_len);
440 void buffer_destroy(Buffer *buffer);
441 void buffer_patch(Buffer *buffer, const size_t start,
442 const void *data, const size_t len);
443
444
445
446 // This is the ID for a D3DXSHADER_CONSTANTTABLE in the bytecode comments.
447 #define CTAB_ID 0x42415443 // 0x42415443 == 'CTAB'
448 #define CTAB_SIZE 28 // sizeof (D3DXSHADER_CONSTANTTABLE).
449 #define CINFO_SIZE 20 // sizeof (D3DXSHADER_CONSTANTINFO).
450 #define CTYPEINFO_SIZE 16 // sizeof (D3DXSHADER_TYPEINFO).
451 #define CMEMBERINFO_SIZE 8 // sizeof (D3DXSHADER_STRUCTMEMBERINFO)
452
453 // Preshader magic values...
454 #define PRES_ID 0x53455250 // 0x53455250 == 'PRES'
455 #define PRSI_ID 0x49535250 // 0x49535250 == 'PRSI'
456 #define CLIT_ID 0x54494C43 // 0x54494C43 == 'CLIT'
457 #define FXLC_ID 0x434C5846 // 0x434C5846 == 'FXLC'
458
459 // we need to reference these by explicit value occasionally...
460 #define OPCODE_RCP 6
461 #define OPCODE_RSQ 7
462 #define OPCODE_RET 28
463 #define OPCODE_IF 40
464 #define OPCODE_IFC 41
465 #define OPCODE_BREAK 44
466 #define OPCODE_BREAKC 45
467 #define OPCODE_TEXLD 66
468 #define OPCODE_SETP 94
469
470 // TEXLD becomes a different instruction with these instruction controls.
471 #define CONTROL_TEXLD 0
472 #define CONTROL_TEXLDP 1
473 #define CONTROL_TEXLDB 2
474
475 // #define this to force app to supply an allocator, so there's no reference
476 // to the C runtime's malloc() and free()...
477 #if MOJOSHADER_FORCE_ALLOCATOR
478 #define MOJOSHADER_internal_malloc NULL
479 #define MOJOSHADER_internal_free NULL
480 #else
481 void * MOJOSHADERCALL MOJOSHADER_internal_malloc(int bytes, void *d);
482 void MOJOSHADERCALL MOJOSHADER_internal_free(void *ptr, void *d);
483 #endif
484
485 #if MOJOSHADER_FORCE_INCLUDE_CALLBACKS
486 #define MOJOSHADER_internal_include_open NULL
487 #define MOJOSHADER_internal_include_close NULL
488 #else
489 int MOJOSHADER_internal_include_open(MOJOSHADER_includeType inctype,
490 const char *fname, const char *parent,
491 const char **outdata,
492 unsigned int *outbytes,
493 MOJOSHADER_malloc m, MOJOSHADER_free f,
494 void *d);
495
496 void MOJOSHADER_internal_include_close(const char *data, MOJOSHADER_malloc m,
497 MOJOSHADER_free f, void *d);
498 #endif
499
500
501 // result modifiers.
502 // !!! FIXME: why isn't this an enum?
503 #define MOD_SATURATE 0x01
504 #define MOD_PP 0x02
505 #define MOD_CENTROID 0x04
506
507 typedef enum
508 {
509 REG_TYPE_TEMP = 0,
510 REG_TYPE_INPUT = 1,
511 REG_TYPE_CONST = 2,
512 REG_TYPE_ADDRESS = 3,
513 REG_TYPE_TEXTURE = 3, // ALSO 3!
514 REG_TYPE_RASTOUT = 4,
515 REG_TYPE_ATTROUT = 5,
516 REG_TYPE_TEXCRDOUT = 6,
517 REG_TYPE_OUTPUT = 6, // ALSO 6!
518 REG_TYPE_CONSTINT = 7,
519 REG_TYPE_COLOROUT = 8,
520 REG_TYPE_DEPTHOUT = 9,
521 REG_TYPE_SAMPLER = 10,
522 REG_TYPE_CONST2 = 11,
523 REG_TYPE_CONST3 = 12,
524 REG_TYPE_CONST4 = 13,
525 REG_TYPE_CONSTBOOL = 14,
526 REG_TYPE_LOOP = 15,
527 REG_TYPE_TEMPFLOAT16 = 16,
528 REG_TYPE_MISCTYPE = 17,
529 REG_TYPE_LABEL = 18,
530 REG_TYPE_PREDICATE = 19,
531 REG_TYPE_MAX = 19
532 } RegisterType;
533
534 typedef enum
535 {
536 TEXTURE_TYPE_2D = 2,
537 TEXTURE_TYPE_CUBE = 3,
538 TEXTURE_TYPE_VOLUME = 4,
539 } TextureType;
540
541 typedef enum
542 {
543 RASTOUT_TYPE_POSITION = 0,
544 RASTOUT_TYPE_FOG = 1,
545 RASTOUT_TYPE_POINT_SIZE = 2,
546 RASTOUT_TYPE_MAX = 2
547 } RastOutType;
548
549 typedef enum
550 {
551 MISCTYPE_TYPE_POSITION = 0,
552 MISCTYPE_TYPE_FACE = 1,
553 MISCTYPE_TYPE_MAX = 1
554 } MiscTypeType;
555
556 // source modifiers.
557 typedef enum
558 {
559 SRCMOD_NONE,
560 SRCMOD_NEGATE,
561 SRCMOD_BIAS,
562 SRCMOD_BIASNEGATE,
563 SRCMOD_SIGN,
564 SRCMOD_SIGNNEGATE,
565 SRCMOD_COMPLEMENT,
566 SRCMOD_X2,
567 SRCMOD_X2NEGATE,
568 SRCMOD_DZ,
569 SRCMOD_DW,
570 SRCMOD_ABS,
571 SRCMOD_ABSNEGATE,
572 SRCMOD_NOT,
573 SRCMOD_TOTAL
574 } SourceMod;
575
576
577 typedef struct
578 {
579 const uint32 *token; // this is the unmolested token in the stream.
580 int regnum;
581 int relative;
582 int writemask; // xyzw or rgba (all four, not split out).
583 int writemask0; // x or red
584 int writemask1; // y or green
585 int writemask2; // z or blue
586 int writemask3; // w or alpha
587 int orig_writemask; // writemask before mojoshader tweaks it.
588 int result_mod;
589 int result_shift;
590 RegisterType regtype;
591 } DestArgInfo;
592
593 // NOTE: This will NOT know a dcl_psize or dcl_fog output register should be
594 // scalar! This function doesn't have access to that information.
595 static inline int scalar_register(const MOJOSHADER_shaderType shader_type,
596 const RegisterType regtype, const int regnum)
597 {
598 switch (regtype)
599 {
600 case REG_TYPE_RASTOUT:
601 if (((const RastOutType) regnum) == RASTOUT_TYPE_FOG)
602 return 1;
603 else if (((const RastOutType) regnum) == RASTOUT_TYPE_POINT_SIZE)
604 return 1;
605 return 0;
606
607 case REG_TYPE_DEPTHOUT:
608 case REG_TYPE_CONSTBOOL:
609 case REG_TYPE_LOOP:
610 return 1;
611
612 case REG_TYPE_MISCTYPE:
613 if ( ((const MiscTypeType) regnum) == MISCTYPE_TYPE_FACE )
614 return 1;
615 return 0;
616
617 case REG_TYPE_PREDICATE:
618 return (shader_type == MOJOSHADER_TYPE_PIXEL) ? 1 : 0;
619
620 default: break;
621 } // switch
622
623 return 0;
624 } // scalar_register
625
626
627 extern MOJOSHADER_error MOJOSHADER_out_of_mem_error;
628 extern MOJOSHADER_parseData MOJOSHADER_out_of_mem_data;
629
630
631 // preprocessor stuff.
632
633 typedef enum
634 {
635 TOKEN_UNKNOWN = 256, // start past ASCII character values.
636
637 // These are all C-like constructs. Tokens < 256 may be single
638 // chars (like '+' or whatever). These are just multi-char sequences
639 // (like "+=" or whatever).
640 TOKEN_IDENTIFIER,
641 TOKEN_INT_LITERAL,
642 TOKEN_FLOAT_LITERAL,
643 TOKEN_STRING_LITERAL,
644 TOKEN_RSHIFTASSIGN,
645 TOKEN_LSHIFTASSIGN,
646 TOKEN_ADDASSIGN,
647 TOKEN_SUBASSIGN,
648 TOKEN_MULTASSIGN,
649 TOKEN_DIVASSIGN,
650 TOKEN_MODASSIGN,
651 TOKEN_XORASSIGN,
652 TOKEN_ANDASSIGN,
653 TOKEN_ORASSIGN,
654 TOKEN_INCREMENT,
655 TOKEN_DECREMENT,
656 TOKEN_RSHIFT,
657 TOKEN_LSHIFT,
658 TOKEN_ANDAND,
659 TOKEN_OROR,
660 TOKEN_LEQ,
661 TOKEN_GEQ,
662 TOKEN_EQL,
663 TOKEN_NEQ,
664 TOKEN_HASH,
665 TOKEN_HASHHASH,
666
667 // This is returned if the preprocessor isn't stripping comments. Note
668 // that in asm files, the ';' counts as a single-line comment, same as
669 // "//". Note that both eat newline tokens: all of the ones inside a
670 // multiline comment, and the ending newline on a single-line comment.
671 TOKEN_MULTI_COMMENT,
672 TOKEN_SINGLE_COMMENT,
673
674 // This is returned at the end of input...no more to process.
675 TOKEN_EOI,
676
677 // This is returned for char sequences we think are bogus. You'll have
678 // to judge for yourself. In most cases, you'll probably just fail with
679 // bogus syntax without explicitly checking for this token.
680 TOKEN_BAD_CHARS,
681
682 // This is returned if there's an error condition (the error is returned
683 // as a NULL-terminated string from preprocessor_nexttoken(), instead
684 // of actual token data). You can continue getting tokens after this
685 // is reported. It happens for things like missing #includes, etc.
686 TOKEN_PREPROCESSING_ERROR,
687
688 // These are all caught by the preprocessor. Caller won't ever see them,
689 // except TOKEN_PP_PRAGMA.
690 // They control the preprocessor (#includes new files, etc).
691 TOKEN_PP_INCLUDE,
692 TOKEN_PP_LINE,
693 TOKEN_PP_DEFINE,
694 TOKEN_PP_UNDEF,
695 TOKEN_PP_IF,
696 TOKEN_PP_IFDEF,
697 TOKEN_PP_IFNDEF,
698 TOKEN_PP_ELSE,
699 TOKEN_PP_ELIF,
700 TOKEN_PP_ENDIF,
701 TOKEN_PP_ERROR, // caught, becomes TOKEN_PREPROCESSING_ERROR
702 TOKEN_PP_PRAGMA,
703 TOKEN_INCOMPLETE_COMMENT, // caught, becomes TOKEN_PREPROCESSING_ERROR
704 TOKEN_PP_UNARY_MINUS, // used internally, never returned.
705 TOKEN_PP_UNARY_PLUS, // used internally, never returned.
706 } Token;
707
708
709 // This is opaque.
710 struct Preprocessor;
711 typedef struct Preprocessor Preprocessor;
712
713 typedef struct Conditional
714 {
715 Token type;
716 int linenum;
717 int skipping;
718 int chosen;
719 struct Conditional *next;
720 } Conditional;
721
722 typedef struct Define
723 {
724 const char *identifier;
725 const char *definition;
726 const char *original;
727 const char **parameters;
728 int paramcount;
729 struct Define *next;
730 } Define;
731
732 typedef struct IncludeState
733 {
734 const char *filename;
735 const char *source_base;
736 const char *source;
737 const char *token;
738 unsigned int tokenlen;
739 Token tokenval;
740 int pushedback;
741 const unsigned char *lexer_marker;
742 int report_whitespace;
743 int report_comments;
744 int asm_comments;
745 unsigned int orig_length;
746 unsigned int bytes_left;
747 unsigned int line;
748 Conditional *conditional_stack;
749 MOJOSHADER_includeClose close_callback;
750 struct IncludeState *next;
751 } IncludeState;
752
753 Token preprocessor_lexer(IncludeState *s);
754
755 // This will only fail if the allocator fails, so it doesn't return any
756 // error code...NULL on failure.
757 Preprocessor *preprocessor_start(const char *fname, const char *source,
758 unsigned int sourcelen,
759 MOJOSHADER_includeOpen open_callback,
760 MOJOSHADER_includeClose close_callback,
761 const MOJOSHADER_preprocessorDefine *defines,
762 unsigned int define_count, int asm_comments,
763 MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
764
765 void preprocessor_end(Preprocessor *pp);
766 int preprocessor_outofmemory(Preprocessor *pp);
767 const char *preprocessor_nexttoken(Preprocessor *_ctx,
768 unsigned int *_len, Token *_token);
769 const char *preprocessor_sourcepos(Preprocessor *pp, unsigned int *pos);
770
771
772 void MOJOSHADER_print_debug_token(const char *subsystem, const char *token,
773 const unsigned int tokenlen,
774 const Token tokenval);
775
776
777 #if SUPPORT_PROFILE_SPIRV
778 // Patching SPIR-V binaries before linking is needed to ensure locations do not
779 // overlap between shader stages. Unfortunately, OpDecorate takes Literal, so we
780 // can't use Result <id> from OpSpecConstant and leave this up to specialization
781 // mechanism.
782 // Patch table must be propagated from parsing to program linking, but since
783 // MOJOSHADER_parseData is public and I'd like to avoid changing ABI and exposing
784 // this, it is appended to MOJOSHADER_parseData::output using postflight buffer.
785 typedef struct SpirvPatchEntry
786 {
787 uint32 offset;
788 int32 location;
789 } SpirvPatchEntry;
790
791 typedef struct SpirvPatchTable
792 {
793 // Patches for uniforms
794 SpirvPatchEntry vpflip;
795 SpirvPatchEntry array_vec4;
796 SpirvPatchEntry array_ivec4;
797 SpirvPatchEntry array_bool;
798 SpirvPatchEntry samplers[16];
799 int32 location_count;
800
801 // TEXCOORD0 is patched to PointCoord if VS outputs PointSize.
802 // In `helpers`: [OpDecorate|id|Location|0xDEADBEEF] -> [OpDecorate|id|BuiltIn|PointCoord]
803 // Offset derived from attrib_offsets[TEXCOORD][0].
804 uint32 pointcoord_var_offset; // in `mainline_intro`, [OpVariable|tid|id|StorageClass], patch tid to pvec2i
805 uint32 pointcoord_load_offset; // in `mainline_top`, [OpLoad|tid|id|src_id], patch tid to vec2
806 uint32 tid_pvec2i;
807 uint32 tid_vec2;
808 uint32 tid_pvec4i;
809
810 /// Patches for TEXCOORD0 and vertex attribute types
811 uint32 tid_vec4;
812 uint32 tid_ivec4;
813 uint32 tid_uvec4;
814
815 // Patches for vertex attribute types
816 uint32 tid_vec4_p;
817 uint32 tid_ivec4_p;
818 uint32 tid_uvec4_p;
819 uint32 attrib_type_offsets[MOJOSHADER_USAGE_TOTAL][16];
820 struct
821 {
822 uint32 num_loads;
823 uint32 *load_types;
824 uint32 *load_opcodes;
825 } attrib_type_load_offsets[MOJOSHADER_USAGE_TOTAL][16];
826
827 // Patches for linking vertex output/pixel input
828 uint32 attrib_offsets[MOJOSHADER_USAGE_TOTAL][16];
829 uint32 output_offsets[16];
830 } SpirvPatchTable;
831
832 void MOJOSHADER_spirv_link_attributes(const MOJOSHADER_parseData *vertex,
833 const MOJOSHADER_parseData *pixel,
834 int is_glspirv);
835 #endif
836
837 #endif // _INCLUDE_MOJOSHADER_INTERNAL_H_
838
839
840 #if MOJOSHADER_DO_INSTRUCTION_TABLE
841 // These have to be in the right order! Arrays are indexed by the value
842 // of the instruction token.
843
844 // INSTRUCTION_STATE means this opcode has to update the state machine
845 // (we're entering an ELSE block, etc). INSTRUCTION means there's no
846 // state, just go straight to the emitters.
847
848 // !!! FIXME: Some of these MOJOSHADER_TYPE_ANYs need to have their scope
849 // !!! FIXME: reduced to just PIXEL or VERTEX.
850
851 INSTRUCTION(NOP, "NOP", 1, NULL, MOJOSHADER_TYPE_ANY, 0xF)
852 INSTRUCTION(MOV, "MOV", 1, DS, MOJOSHADER_TYPE_ANY, 0xF)
853 INSTRUCTION(ADD, "ADD", 1, DSS, MOJOSHADER_TYPE_ANY, 0xF)
854 INSTRUCTION(SUB, "SUB", 1, DSS, MOJOSHADER_TYPE_ANY, 0xF)
855 INSTRUCTION(MAD, "MAD", 1, DSSS, MOJOSHADER_TYPE_ANY, 0xF)
856 INSTRUCTION(MUL, "MUL", 1, DSS, MOJOSHADER_TYPE_ANY, 0xF)
857 INSTRUCTION_STATE(RCP, "RCP", 1, DS, MOJOSHADER_TYPE_ANY, 0xF)
858 INSTRUCTION_STATE(RSQ, "RSQ", 1, DS, MOJOSHADER_TYPE_ANY, 0xF)
859 INSTRUCTION(DP3, "DP3", 1, DSS, MOJOSHADER_TYPE_ANY, 0xF)
860 INSTRUCTION_STATE(DP4, "DP4", 1, DSS, MOJOSHADER_TYPE_ANY, 0xF)
861 INSTRUCTION(MIN, "MIN", 1, DSS, MOJOSHADER_TYPE_ANY, 0xF)
862 INSTRUCTION(MAX, "MAX", 1, DSS, MOJOSHADER_TYPE_ANY, 0xF)
863 INSTRUCTION(SLT, "SLT", 1, DSS, MOJOSHADER_TYPE_ANY, 0xF)
864 INSTRUCTION(SGE, "SGE", 1, DSS, MOJOSHADER_TYPE_ANY, 0xF)
865 INSTRUCTION(EXP, "EXP", 1, DS, MOJOSHADER_TYPE_ANY, 0xF)
866 INSTRUCTION_STATE(LOG, "LOG", 1, DS, MOJOSHADER_TYPE_ANY, 0xF)
867 INSTRUCTION(LIT, "LIT", 3, DS, MOJOSHADER_TYPE_ANY, 0xF)
868 INSTRUCTION(DST, "DST", 1, DSS, MOJOSHADER_TYPE_VERTEX, 0xF)
869 INSTRUCTION(LRP, "LRP", 2, DSSS, MOJOSHADER_TYPE_ANY, 0xF)
870 INSTRUCTION_STATE(FRC, "FRC", 1, DS, MOJOSHADER_TYPE_ANY, 0xF)
871 INSTRUCTION_STATE(M4X4, "M4X4", 4, DSS, MOJOSHADER_TYPE_ANY, 0xF)
872 INSTRUCTION_STATE(M4X3, "M4X3", 3, DSS, MOJOSHADER_TYPE_ANY, 0x7)
873 INSTRUCTION_STATE(M3X4, "M3X4", 4, DSS, MOJOSHADER_TYPE_ANY, 0xF)
874 INSTRUCTION_STATE(M3X3, "M3X3", 3, DSS, MOJOSHADER_TYPE_ANY, 0x7)
875 INSTRUCTION_STATE(M3X2, "M3X2", 2, DSS, MOJOSHADER_TYPE_ANY, 0x3)
876 INSTRUCTION_STATE(CALL, "CALL", 2, S, MOJOSHADER_TYPE_ANY, 0xF)
877 INSTRUCTION_STATE(CALLNZ, "CALLNZ", 3, SS, MOJOSHADER_TYPE_ANY, 0xF)
878 INSTRUCTION_STATE(LOOP, "LOOP", 3, SS, MOJOSHADER_TYPE_ANY, 0xF)
879 INSTRUCTION_STATE(RET, "RET", 1, NULL, MOJOSHADER_TYPE_ANY, 0xF)
880 INSTRUCTION_STATE(ENDLOOP, "ENDLOOP", 2, NULL, MOJOSHADER_TYPE_ANY, 0xF)
881 INSTRUCTION_STATE(LABEL, "LABEL", 0, S, MOJOSHADER_TYPE_ANY, 0xF)
882 INSTRUCTION_STATE(DCL, "DCL", 0, DCL, MOJOSHADER_TYPE_ANY, 0xF)
883 INSTRUCTION_STATE(POW, "POW", 3, DSS, MOJOSHADER_TYPE_ANY, 0xF)
884 INSTRUCTION(CRS, "CRS", 2, DSS, MOJOSHADER_TYPE_ANY, 0xF)
885 INSTRUCTION(SGN, "SGN", 3, DSSS, MOJOSHADER_TYPE_ANY, 0xF)
886 INSTRUCTION(ABS, "ABS", 1, DS, MOJOSHADER_TYPE_ANY, 0xF)
887 INSTRUCTION(NRM, "NRM", 3, DS, MOJOSHADER_TYPE_ANY, 0xF)
888 INSTRUCTION_STATE(SINCOS, "SINCOS", 8, SINCOS, MOJOSHADER_TYPE_ANY, 0xF)
889 INSTRUCTION_STATE(REP, "REP", 3, S, MOJOSHADER_TYPE_ANY, 0xF)
890 INSTRUCTION_STATE(ENDREP, "ENDREP", 2, NULL, MOJOSHADER_TYPE_ANY, 0xF)
891 INSTRUCTION_STATE(IF, "IF", 3, S, MOJOSHADER_TYPE_ANY, 0xF)
892 INSTRUCTION_STATE(IFC, "IF", 3, SS, MOJOSHADER_TYPE_ANY, 0xF)
893 INSTRUCTION(ELSE, "ELSE", 1, NULL, MOJOSHADER_TYPE_ANY, 0xF) // !!! FIXME: state!
894 INSTRUCTION(ENDIF, "ENDIF", 1, NULL, MOJOSHADER_TYPE_ANY, 0xF) // !!! FIXME: state!
895 INSTRUCTION_STATE(BREAK, "BREAK", 1, NULL, MOJOSHADER_TYPE_ANY, 0xF)
896 INSTRUCTION_STATE(BREAKC, "BREAK", 3, SS, MOJOSHADER_TYPE_ANY, 0xF)
897 INSTRUCTION_STATE(MOVA, "MOVA", 1, DS, MOJOSHADER_TYPE_VERTEX, 0xF)
898 INSTRUCTION_STATE(DEFB, "DEFB", 0, DEFB, MOJOSHADER_TYPE_ANY, 0xF)
899 INSTRUCTION_STATE(DEFI, "DEFI", 0, DEFI, MOJOSHADER_TYPE_ANY, 0xF)
900 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
901 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
902 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
903 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
904 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
905 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
906 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
907 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
908 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
909 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
910 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
911 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
912 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
913 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
914 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
915 INSTRUCTION_STATE(TEXCRD, "TEXCRD", 1, TEXCRD, MOJOSHADER_TYPE_PIXEL, 0xF)
916 INSTRUCTION_STATE(TEXKILL, "TEXKILL", 2, D, MOJOSHADER_TYPE_PIXEL, 0xF)
917 INSTRUCTION_STATE(TEXLD, "TEXLD", 1, TEXLD, MOJOSHADER_TYPE_PIXEL, 0xF)
918 INSTRUCTION_STATE(TEXBEM, "TEXBEM", 1, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
919 INSTRUCTION_STATE(TEXBEML, "TEXBEML", 2, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
920 INSTRUCTION(TEXREG2AR, "TEXREG2AR", 1, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
921 INSTRUCTION(TEXREG2GB, "TEXREG2GB", 1, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
922 INSTRUCTION_STATE(TEXM3X2PAD, "TEXM3X2PAD", 1, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
923 INSTRUCTION_STATE(TEXM3X2TEX, "TEXM3X2TEX", 1, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
924 INSTRUCTION_STATE(TEXM3X3PAD, "TEXM3X3PAD", 1, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
925 INSTRUCTION_STATE(TEXM3X3TEX, "TEXM3X3TEX", 1, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
926 INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN, 0xF)
927 INSTRUCTION_STATE(TEXM3X3SPEC, "TEXM3X3SPEC", 1, DSS, MOJOSHADER_TYPE_PIXEL, 0xF)
928 INSTRUCTION_STATE(TEXM3X3VSPEC, "TEXM3X3VSPEC", 1, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
929 INSTRUCTION(EXPP, "EXPP", 1, DS, MOJOSHADER_TYPE_ANY, 0xF)
930 INSTRUCTION_STATE(LOGP, "LOGP", 1, DS, MOJOSHADER_TYPE_ANY, 0xF)
931 INSTRUCTION_STATE(CND, "CND", 1, DSSS, MOJOSHADER_TYPE_PIXEL, 0xF)
932 INSTRUCTION_STATE(DEF, "DEF", 0, DEF, MOJOSHADER_TYPE_ANY, 0xF)
933 INSTRUCTION(TEXREG2RGB, "TEXREG2RGB", 1, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
934 INSTRUCTION(TEXDP3TEX, "TEXDP3TEX", 1, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
935 INSTRUCTION(TEXM3X2DEPTH, "TEXM3X2DEPTH", 1, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
936 INSTRUCTION(TEXDP3, "TEXDP3", 1, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
937 INSTRUCTION_STATE(TEXM3X3, "TEXM3X3", 1, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
938 INSTRUCTION(TEXDEPTH, "TEXDEPTH", 1, D, MOJOSHADER_TYPE_PIXEL, 0xF)
939 INSTRUCTION_STATE(CMP, "CMP", 1, DSSS, MOJOSHADER_TYPE_PIXEL, 0xF)
940 INSTRUCTION(BEM, "BEM", 2, DSS, MOJOSHADER_TYPE_PIXEL, 0xF)
941 INSTRUCTION_STATE(DP2ADD, "DP2ADD", 2, DSSS, MOJOSHADER_TYPE_PIXEL, 0xF)
942 INSTRUCTION(DSX, "DSX", 2, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
943 INSTRUCTION(DSY, "DSY", 2, DS, MOJOSHADER_TYPE_PIXEL, 0xF)
944 INSTRUCTION(TEXLDD, "TEXLDD", 3, DSSSS, MOJOSHADER_TYPE_PIXEL, 0xF)
945 INSTRUCTION_STATE(SETP, "SETP", 1, DSS, MOJOSHADER_TYPE_ANY, 0xF)
946 INSTRUCTION_STATE(TEXLDL, "TEXLDL", 2, DSS, MOJOSHADER_TYPE_ANY, 0xF)
947 INSTRUCTION_STATE(BREAKP, "BREAKP", 3, S, MOJOSHADER_TYPE_ANY, 0xF)
948
949 #undef MOJOSHADER_DO_INSTRUCTION_TABLE
950 #undef INSTRUCTION
951 #undef INSTRUCTION_STATE
952
953 #endif
954
955 // end of mojoshader_internal.h ...
956