mojoshader_internal.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. #ifndef _INCLUDE_MOJOSHADER_INTERNAL_H_
  2. #define _INCLUDE_MOJOSHADER_INTERNAL_H_
  3. #ifndef __MOJOSHADER_INTERNAL__
  4. #error Do not include this header from your applications.
  5. #endif
  6. // Shader bytecode format is described at MSDN:
  7. // http://msdn.microsoft.com/en-us/library/ff569705.aspx
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <stdarg.h>
  12. #include <assert.h>
  13. #include "mojoshader.h"
  14. #define DEBUG_LEXER 0
  15. #define DEBUG_PREPROCESSOR 0
  16. #define DEBUG_ASSEMBLER_PARSER 0
  17. #define DEBUG_COMPILER_PARSER 0
  18. #define DEBUG_TOKENIZER \
  19. (DEBUG_PREPROCESSOR || DEBUG_ASSEMBLER_PARSER || DEBUG_LEXER)
  20. #if (defined(__APPLE__) && defined(__MACH__))
  21. #define PLATFORM_MACOSX 1
  22. #endif
  23. // This is the highest shader version we currently support.
  24. #define MAX_SHADER_MAJOR 3
  25. #define MAX_SHADER_MINOR 255 // vs_3_sw
  26. // If SUPPORT_PROFILE_* isn't defined, we assume an implicit desire to support.
  27. // You get all the profiles unless you go out of your way to disable them.
  28. #ifndef SUPPORT_PROFILE_D3D
  29. #define SUPPORT_PROFILE_D3D 1
  30. #endif
  31. #ifndef SUPPORT_PROFILE_BYTECODE
  32. #define SUPPORT_PROFILE_BYTECODE 1
  33. #endif
  34. #ifndef SUPPORT_PROFILE_GLSL
  35. #define SUPPORT_PROFILE_GLSL 1
  36. #endif
  37. #ifndef SUPPORT_PROFILE_GLSL120
  38. #define SUPPORT_PROFILE_GLSL120 1
  39. #endif
  40. #ifndef SUPPORT_PROFILE_ARB1
  41. #define SUPPORT_PROFILE_ARB1 1
  42. #endif
  43. #ifndef SUPPORT_PROFILE_ARB1_NV
  44. #define SUPPORT_PROFILE_ARB1_NV 1
  45. #endif
  46. #if SUPPORT_PROFILE_ARB1_NV && !SUPPORT_PROFILE_ARB1
  47. #error nv profiles require arb1 profile. Fix your build.
  48. #endif
  49. #if SUPPORT_PROFILE_GLSL120 && !SUPPORT_PROFILE_GLSL
  50. #error glsl120 profile requires glsl profile. Fix your build.
  51. #endif
  52. // Other stuff you can disable...
  53. // This removes the preshader parsing and execution code. You can save some
  54. // bytes if you have normal shaders and not Effect files.
  55. #ifndef SUPPORT_PRESHADERS
  56. #define SUPPORT_PRESHADERS 1
  57. #endif
  58. #if SUPPORT_PRESHADERS
  59. void MOJOSHADER_runPreshader(const MOJOSHADER_preshader*, const float*, float*);
  60. #else
  61. #define MOJOSHADER_runPreshader(a, b)
  62. #endif
  63. // Get basic wankery out of the way here...
  64. #ifdef _WINDOWS
  65. #define ENDLINE_STR "\r\n"
  66. #else
  67. #define ENDLINE_STR "\n"
  68. #endif
  69. typedef unsigned int uint; // this is a printf() helper. don't use for code.
  70. #ifdef _MSC_VER
  71. #include <malloc.h>
  72. #define va_copy(a, b) a = b
  73. #define snprintf _snprintf // !!! FIXME: not a safe replacement!
  74. #define vsnprintf _vsnprintf // !!! FIXME: not a safe replacement!
  75. #define strcasecmp stricmp
  76. #define strncasecmp strnicmp
  77. typedef unsigned __int8 uint8;
  78. typedef unsigned __int16 uint16;
  79. typedef unsigned __int32 uint32;
  80. typedef unsigned __int64 uint64;
  81. typedef __int32 int32;
  82. typedef __int64 int64;
  83. #ifdef _WIN64
  84. typedef __int64 ssize_t;
  85. #elif defined _WIN32
  86. typedef __int32 ssize_t;
  87. #else
  88. #error Please define your platform.
  89. #endif
  90. // Warning Level 4 considered harmful. :)
  91. #pragma warning(disable: 4100) // "unreferenced formal parameter"
  92. #pragma warning(disable: 4389) // "signed/unsigned mismatch"
  93. #else
  94. #include <stdint.h>
  95. typedef uint8_t uint8;
  96. typedef uint16_t uint16;
  97. typedef uint32_t uint32;
  98. typedef int32_t int32;
  99. typedef int64_t int64;
  100. typedef uint64_t uint64;
  101. #endif
  102. #ifdef sun
  103. #include <alloca.h>
  104. #endif
  105. #ifdef __GNUC__
  106. #define ISPRINTF(x,y) __attribute__((format (printf, x, y)))
  107. #else
  108. #define ISPRINTF(x,y)
  109. #endif
  110. #define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
  111. // Byteswap magic...
  112. #if ((defined __GNUC__) && (defined __POWERPC__))
  113. static inline uint32 SWAP32(uint32 x)
  114. {
  115. __asm__ __volatile__("lwbrx %0,0,%1" : "=r" (x) : "r" (&x));
  116. return x;
  117. } // SWAP32
  118. static inline uint16 SWAP16(uint16 x)
  119. {
  120. __asm__ __volatile__("lhbrx %0,0,%1" : "=r" (x) : "r" (&x));
  121. return x;
  122. } // SWAP16
  123. #elif defined(__POWERPC__)
  124. static inline uint32 SWAP32(uint32 x)
  125. {
  126. return ( (((x) >> 24) & 0x000000FF) | (((x) >> 8) & 0x0000FF00) |
  127. (((x) << 8) & 0x00FF0000) | (((x) << 24) & 0xFF000000) );
  128. } // SWAP32
  129. static inline uint16 SWAP16(uint16 x)
  130. {
  131. return ( (((x) >> 8) & 0x00FF) | (((x) << 8) & 0xFF00) );
  132. } // SWAP16
  133. #else
  134. # define SWAP16(x) (x)
  135. # define SWAP32(x) (x)
  136. #endif
  137. #define SWAPDBL(x) (x) // !!! FIXME
  138. static inline int Min(const int a, const int b)
  139. {
  140. return ((a < b) ? a : b);
  141. } // Min
  142. // Hashtables...
  143. typedef struct HashTable HashTable;
  144. typedef uint32 (*HashTable_HashFn)(const void *key, void *data);
  145. typedef int (*HashTable_KeyMatchFn)(const void *a, const void *b, void *data);
  146. typedef void (*HashTable_NukeFn)(const void *key, const void *value, void *data);
  147. HashTable *hash_create(void *data, const HashTable_HashFn hashfn,
  148. const HashTable_KeyMatchFn keymatchfn,
  149. const HashTable_NukeFn nukefn,
  150. const int stackable,
  151. MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
  152. void hash_destroy(HashTable *table);
  153. int hash_insert(HashTable *table, const void *key, const void *value);
  154. int hash_remove(HashTable *table, const void *key);
  155. int hash_find(const HashTable *table, const void *key, const void **_value);
  156. int hash_iter(const HashTable *table, const void *key, const void **_value, void **iter);
  157. int hash_iter_keys(const HashTable *table, const void **_key, void **iter);
  158. uint32 hash_hash_string(const void *sym, void *unused);
  159. int hash_keymatch_string(const void *a, const void *b, void *unused);
  160. // String -> String map ...
  161. typedef HashTable StringMap;
  162. StringMap *stringmap_create(const int copy, MOJOSHADER_malloc m,
  163. MOJOSHADER_free f, void *d);
  164. void stringmap_destroy(StringMap *smap);
  165. int stringmap_insert(StringMap *smap, const char *key, const char *value);
  166. int stringmap_remove(StringMap *smap, const char *key);
  167. int stringmap_find(const StringMap *smap, const char *key, const char **_val);
  168. // String caching...
  169. typedef struct StringCache StringCache;
  170. StringCache *stringcache_create(MOJOSHADER_malloc m,MOJOSHADER_free f,void *d);
  171. const char *stringcache(StringCache *cache, const char *str);
  172. const char *stringcache_len(StringCache *cache, const char *str,
  173. const unsigned int len);
  174. const char *stringcache_fmt(StringCache *cache, const char *fmt, ...);
  175. void stringcache_destroy(StringCache *cache);
  176. // Error lists...
  177. typedef struct ErrorList ErrorList;
  178. ErrorList *errorlist_create(MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
  179. int errorlist_add(ErrorList *list, const char *fname,
  180. const int errpos, const char *str);
  181. int errorlist_add_fmt(ErrorList *list, const char *fname,
  182. const int errpos, const char *fmt, ...) ISPRINTF(4,5);
  183. int errorlist_add_va(ErrorList *list, const char *_fname,
  184. const int errpos, const char *fmt, va_list va);
  185. int errorlist_count(ErrorList *list);
  186. MOJOSHADER_error *errorlist_flatten(ErrorList *list); // resets the list!
  187. void errorlist_destroy(ErrorList *list);
  188. // Dynamic buffers...
  189. typedef struct Buffer Buffer;
  190. Buffer *buffer_create(size_t blksz,MOJOSHADER_malloc m,MOJOSHADER_free f,void *d);
  191. char *buffer_reserve(Buffer *buffer, const size_t len);
  192. int buffer_append(Buffer *buffer, const void *_data, size_t len);
  193. int buffer_append_fmt(Buffer *buffer, const char *fmt, ...) ISPRINTF(2,3);
  194. int buffer_append_va(Buffer *buffer, const char *fmt, va_list va);
  195. size_t buffer_size(Buffer *buffer);
  196. void buffer_empty(Buffer *buffer);
  197. char *buffer_flatten(Buffer *buffer);
  198. char *buffer_merge(Buffer **buffers, const size_t n, size_t *_len);
  199. void buffer_destroy(Buffer *buffer);
  200. ssize_t buffer_find(Buffer *buffer, const size_t start,
  201. const void *data, const size_t len);
  202. // This is the ID for a D3DXSHADER_CONSTANTTABLE in the bytecode comments.
  203. #define CTAB_ID 0x42415443 // 0x42415443 == 'CTAB'
  204. #define CTAB_SIZE 28 // sizeof (D3DXSHADER_CONSTANTTABLE).
  205. #define CINFO_SIZE 20 // sizeof (D3DXSHADER_CONSTANTINFO).
  206. #define CTYPEINFO_SIZE 16 // sizeof (D3DXSHADER_TYPEINFO).
  207. #define CMEMBERINFO_SIZE 8 // sizeof (D3DXSHADER_STRUCTMEMBERINFO)
  208. // Preshader magic values...
  209. #define PRES_ID 0x53455250 // 0x53455250 == 'PRES'
  210. #define PRSI_ID 0x49535250 // 0x49535250 == 'PRSI'
  211. #define CLIT_ID 0x54494C43 // 0x54494C43 == 'CLIT'
  212. #define FXLC_ID 0x434C5846 // 0x434C5846 == 'FXLC'
  213. // we need to reference these by explicit value occasionally...
  214. #define OPCODE_RET 28
  215. #define OPCODE_IF 40
  216. #define OPCODE_IFC 41
  217. #define OPCODE_BREAK 44
  218. #define OPCODE_BREAKC 45
  219. #define OPCODE_TEXLD 66
  220. #define OPCODE_SETP 94
  221. // TEXLD becomes a different instruction with these instruction controls.
  222. #define CONTROL_TEXLD 0
  223. #define CONTROL_TEXLDP 1
  224. #define CONTROL_TEXLDB 2
  225. // #define this to force app to supply an allocator, so there's no reference
  226. // to the C runtime's malloc() and free()...
  227. #if MOJOSHADER_FORCE_ALLOCATOR
  228. #define MOJOSHADER_internal_malloc NULL
  229. #define MOJOSHADER_internal_free NULL
  230. #else
  231. void *MOJOSHADER_internal_malloc(int bytes, void *d);
  232. void MOJOSHADER_internal_free(void *ptr, void *d);
  233. #endif
  234. #if MOJOSHADER_FORCE_INCLUDE_CALLBACKS
  235. #define MOJOSHADER_internal_include_open NULL
  236. #define MOJOSHADER_internal_include_close NULL
  237. #else
  238. int MOJOSHADER_internal_include_open(MOJOSHADER_includeType inctype,
  239. const char *fname, const char *parent,
  240. const char **outdata,
  241. unsigned int *outbytes,
  242. MOJOSHADER_malloc m, MOJOSHADER_free f,
  243. void *d);
  244. void MOJOSHADER_internal_include_close(const char *data, MOJOSHADER_malloc m,
  245. MOJOSHADER_free f, void *d);
  246. #endif
  247. // result modifiers.
  248. // !!! FIXME: why isn't this an enum?
  249. #define MOD_SATURATE 0x01
  250. #define MOD_PP 0x02
  251. #define MOD_CENTROID 0x04
  252. typedef enum
  253. {
  254. REG_TYPE_TEMP = 0,
  255. REG_TYPE_INPUT = 1,
  256. REG_TYPE_CONST = 2,
  257. REG_TYPE_ADDRESS = 3,
  258. REG_TYPE_TEXTURE = 3, // ALSO 3!
  259. REG_TYPE_RASTOUT = 4,
  260. REG_TYPE_ATTROUT = 5,
  261. REG_TYPE_TEXCRDOUT = 6,
  262. REG_TYPE_OUTPUT = 6, // ALSO 6!
  263. REG_TYPE_CONSTINT = 7,
  264. REG_TYPE_COLOROUT = 8,
  265. REG_TYPE_DEPTHOUT = 9,
  266. REG_TYPE_SAMPLER = 10,
  267. REG_TYPE_CONST2 = 11,
  268. REG_TYPE_CONST3 = 12,
  269. REG_TYPE_CONST4 = 13,
  270. REG_TYPE_CONSTBOOL = 14,
  271. REG_TYPE_LOOP = 15,
  272. REG_TYPE_TEMPFLOAT16 = 16,
  273. REG_TYPE_MISCTYPE = 17,
  274. REG_TYPE_LABEL = 18,
  275. REG_TYPE_PREDICATE = 19,
  276. REG_TYPE_MAX = 19
  277. } RegisterType;
  278. typedef enum
  279. {
  280. TEXTURE_TYPE_2D = 2,
  281. TEXTURE_TYPE_CUBE = 3,
  282. TEXTURE_TYPE_VOLUME = 4,
  283. } TextureType;
  284. typedef enum
  285. {
  286. RASTOUT_TYPE_POSITION = 0,
  287. RASTOUT_TYPE_FOG = 1,
  288. RASTOUT_TYPE_POINT_SIZE = 2,
  289. RASTOUT_TYPE_MAX = 2
  290. } RastOutType;
  291. typedef enum
  292. {
  293. MISCTYPE_TYPE_POSITION = 0,
  294. MISCTYPE_TYPE_FACE = 1,
  295. MISCTYPE_TYPE_MAX = 1
  296. } MiscTypeType;
  297. // source modifiers.
  298. typedef enum
  299. {
  300. SRCMOD_NONE,
  301. SRCMOD_NEGATE,
  302. SRCMOD_BIAS,
  303. SRCMOD_BIASNEGATE,
  304. SRCMOD_SIGN,
  305. SRCMOD_SIGNNEGATE,
  306. SRCMOD_COMPLEMENT,
  307. SRCMOD_X2,
  308. SRCMOD_X2NEGATE,
  309. SRCMOD_DZ,
  310. SRCMOD_DW,
  311. SRCMOD_ABS,
  312. SRCMOD_ABSNEGATE,
  313. SRCMOD_NOT,
  314. SRCMOD_TOTAL
  315. } SourceMod;
  316. typedef struct
  317. {
  318. const uint32 *token; // this is the unmolested token in the stream.
  319. int regnum;
  320. int relative;
  321. int writemask; // xyzw or rgba (all four, not split out).
  322. int writemask0; // x or red
  323. int writemask1; // y or green
  324. int writemask2; // z or blue
  325. int writemask3; // w or alpha
  326. int orig_writemask; // writemask before mojoshader tweaks it.
  327. int result_mod;
  328. int result_shift;
  329. RegisterType regtype;
  330. } DestArgInfo;
  331. // NOTE: This will NOT know a dcl_psize or dcl_fog output register should be
  332. // scalar! This function doesn't have access to that information.
  333. static inline int scalar_register(const MOJOSHADER_shaderType shader_type,
  334. const RegisterType regtype, const int regnum)
  335. {
  336. switch (regtype)
  337. {
  338. case REG_TYPE_RASTOUT:
  339. if (((const RastOutType) regnum) == RASTOUT_TYPE_FOG)
  340. return 1;
  341. else if (((const RastOutType) regnum) == RASTOUT_TYPE_POINT_SIZE)
  342. return 1;
  343. return 0;
  344. case REG_TYPE_DEPTHOUT:
  345. case REG_TYPE_CONSTBOOL:
  346. case REG_TYPE_LOOP:
  347. return 1;
  348. case REG_TYPE_MISCTYPE:
  349. if ( ((const MiscTypeType) regnum) == MISCTYPE_TYPE_FACE )
  350. return 1;
  351. return 0;
  352. case REG_TYPE_PREDICATE:
  353. return (shader_type == MOJOSHADER_TYPE_PIXEL) ? 1 : 0;
  354. default: break;
  355. } // switch
  356. return 0;
  357. } // scalar_register
  358. extern MOJOSHADER_error MOJOSHADER_out_of_mem_error;
  359. extern MOJOSHADER_parseData MOJOSHADER_out_of_mem_data;
  360. // preprocessor stuff.
  361. typedef enum
  362. {
  363. TOKEN_UNKNOWN = 256, // start past ASCII character values.
  364. // These are all C-like constructs. Tokens < 256 may be single
  365. // chars (like '+' or whatever). These are just multi-char sequences
  366. // (like "+=" or whatever).
  367. TOKEN_IDENTIFIER,
  368. TOKEN_INT_LITERAL,
  369. TOKEN_FLOAT_LITERAL,
  370. TOKEN_STRING_LITERAL,
  371. TOKEN_RSHIFTASSIGN,
  372. TOKEN_LSHIFTASSIGN,
  373. TOKEN_ADDASSIGN,
  374. TOKEN_SUBASSIGN,
  375. TOKEN_MULTASSIGN,
  376. TOKEN_DIVASSIGN,
  377. TOKEN_MODASSIGN,
  378. TOKEN_XORASSIGN,
  379. TOKEN_ANDASSIGN,
  380. TOKEN_ORASSIGN,
  381. TOKEN_INCREMENT,
  382. TOKEN_DECREMENT,
  383. TOKEN_RSHIFT,
  384. TOKEN_LSHIFT,
  385. TOKEN_ANDAND,
  386. TOKEN_OROR,
  387. TOKEN_LEQ,
  388. TOKEN_GEQ,
  389. TOKEN_EQL,
  390. TOKEN_NEQ,
  391. TOKEN_HASH,
  392. TOKEN_HASHHASH,
  393. // This is returned at the end of input...no more to process.
  394. TOKEN_EOI,
  395. // This is returned for char sequences we think are bogus. You'll have
  396. // to judge for yourself. In most cases, you'll probably just fail with
  397. // bogus syntax without explicitly checking for this token.
  398. TOKEN_BAD_CHARS,
  399. // This is returned if there's an error condition (the error is returned
  400. // as a NULL-terminated string from preprocessor_nexttoken(), instead
  401. // of actual token data). You can continue getting tokens after this
  402. // is reported. It happens for things like missing #includes, etc.
  403. TOKEN_PREPROCESSING_ERROR,
  404. // These are all caught by the preprocessor. Caller won't ever see them,
  405. // except TOKEN_PP_PRAGMA.
  406. // They control the preprocessor (#includes new files, etc).
  407. TOKEN_PP_INCLUDE,
  408. TOKEN_PP_LINE,
  409. TOKEN_PP_DEFINE,
  410. TOKEN_PP_UNDEF,
  411. TOKEN_PP_IF,
  412. TOKEN_PP_IFDEF,
  413. TOKEN_PP_IFNDEF,
  414. TOKEN_PP_ELSE,
  415. TOKEN_PP_ELIF,
  416. TOKEN_PP_ENDIF,
  417. TOKEN_PP_ERROR, // caught, becomes TOKEN_PREPROCESSING_ERROR
  418. TOKEN_PP_PRAGMA,
  419. TOKEN_INCOMPLETE_COMMENT, // caught, becomes TOKEN_PREPROCESSING_ERROR
  420. TOKEN_PP_UNARY_MINUS, // used internally, never returned.
  421. TOKEN_PP_UNARY_PLUS, // used internally, never returned.
  422. } Token;
  423. // This is opaque.
  424. struct Preprocessor;
  425. typedef struct Preprocessor Preprocessor;
  426. typedef struct Conditional
  427. {
  428. Token type;
  429. int linenum;
  430. int skipping;
  431. int chosen;
  432. struct Conditional *next;
  433. } Conditional;
  434. typedef struct Define
  435. {
  436. const char *identifier;
  437. const char *definition;
  438. const char *original;
  439. const char **parameters;
  440. int paramcount;
  441. struct Define *next;
  442. } Define;
  443. typedef struct IncludeState
  444. {
  445. const char *filename;
  446. const char *source_base;
  447. const char *source;
  448. const char *token;
  449. unsigned int tokenlen;
  450. Token tokenval;
  451. int pushedback;
  452. const unsigned char *lexer_marker;
  453. int report_whitespace;
  454. int asm_comments;
  455. unsigned int orig_length;
  456. unsigned int bytes_left;
  457. unsigned int line;
  458. Conditional *conditional_stack;
  459. MOJOSHADER_includeClose close_callback;
  460. struct IncludeState *next;
  461. } IncludeState;
  462. Token preprocessor_lexer(IncludeState *s);
  463. // This will only fail if the allocator fails, so it doesn't return any
  464. // error code...NULL on failure.
  465. Preprocessor *preprocessor_start(const char *fname, const char *source,
  466. unsigned int sourcelen,
  467. MOJOSHADER_includeOpen open_callback,
  468. MOJOSHADER_includeClose close_callback,
  469. const MOJOSHADER_preprocessorDefine *defines,
  470. unsigned int define_count, int asm_comments,
  471. MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
  472. void preprocessor_end(Preprocessor *pp);
  473. int preprocessor_outofmemory(Preprocessor *pp);
  474. const char *preprocessor_nexttoken(Preprocessor *_ctx,
  475. unsigned int *_len, Token *_token);
  476. const char *preprocessor_sourcepos(Preprocessor *pp, unsigned int *pos);
  477. void MOJOSHADER_print_debug_token(const char *subsystem, const char *token,
  478. const unsigned int tokenlen,
  479. const Token tokenval);
  480. #endif // _INCLUDE_MOJOSHADER_INTERNAL_H_
  481. #if MOJOSHADER_DO_INSTRUCTION_TABLE
  482. // These have to be in the right order! Arrays are indexed by the value
  483. // of the instruction token.
  484. // INSTRUCTION_STATE means this opcode has to update the state machine
  485. // (we're entering an ELSE block, etc). INSTRUCTION means there's no
  486. // state, just go straight to the emitters.
  487. // !!! FIXME: Some of these MOJOSHADER_TYPE_ANYs need to have their scope
  488. // !!! FIXME: reduced to just PIXEL or VERTEX.
  489. INSTRUCTION(NOP, "NOP", 1, NULL, MOJOSHADER_TYPE_ANY)
  490. INSTRUCTION(MOV, "MOV", 1, DS, MOJOSHADER_TYPE_ANY)
  491. INSTRUCTION(ADD, "ADD", 1, DSS, MOJOSHADER_TYPE_ANY)
  492. INSTRUCTION(SUB, "SUB", 1, DSS, MOJOSHADER_TYPE_ANY)
  493. INSTRUCTION(MAD, "MAD", 1, DSSS, MOJOSHADER_TYPE_ANY)
  494. INSTRUCTION(MUL, "MUL", 1, DSS, MOJOSHADER_TYPE_ANY)
  495. INSTRUCTION_STATE(RCP, "RCP", 1, DS, MOJOSHADER_TYPE_ANY)
  496. INSTRUCTION(RSQ, "RSQ", 1, DS, MOJOSHADER_TYPE_ANY)
  497. INSTRUCTION(DP3, "DP3", 1, DSS, MOJOSHADER_TYPE_ANY)
  498. INSTRUCTION_STATE(DP4, "DP4", 1, DSS, MOJOSHADER_TYPE_ANY)
  499. INSTRUCTION(MIN, "MIN", 1, DSS, MOJOSHADER_TYPE_ANY)
  500. INSTRUCTION(MAX, "MAX", 1, DSS, MOJOSHADER_TYPE_ANY)
  501. INSTRUCTION(SLT, "SLT", 1, DSS, MOJOSHADER_TYPE_ANY)
  502. INSTRUCTION(SGE, "SGE", 1, DSS, MOJOSHADER_TYPE_ANY)
  503. INSTRUCTION(EXP, "EXP", 1, DS, MOJOSHADER_TYPE_ANY)
  504. INSTRUCTION_STATE(LOG, "LOG", 1, DS, MOJOSHADER_TYPE_ANY)
  505. INSTRUCTION(LIT, "LIT", 3, DS, MOJOSHADER_TYPE_ANY)
  506. INSTRUCTION(DST, "DST", 1, DSS, MOJOSHADER_TYPE_VERTEX)
  507. INSTRUCTION(LRP, "LRP", 2, DSSS, MOJOSHADER_TYPE_ANY)
  508. INSTRUCTION_STATE(FRC, "FRC", 1, DS, MOJOSHADER_TYPE_ANY)
  509. INSTRUCTION_STATE(M4X4, "M4X4", 4, DSS, MOJOSHADER_TYPE_ANY)
  510. INSTRUCTION_STATE(M4X3, "M4X3", 3, DSS, MOJOSHADER_TYPE_ANY)
  511. INSTRUCTION_STATE(M3X4, "M3X4", 4, DSS, MOJOSHADER_TYPE_ANY)
  512. INSTRUCTION_STATE(M3X3, "M3X3", 3, DSS, MOJOSHADER_TYPE_ANY)
  513. INSTRUCTION_STATE(M3X2, "M3X2", 2, DSS, MOJOSHADER_TYPE_ANY)
  514. INSTRUCTION_STATE(CALL, "CALL", 2, S, MOJOSHADER_TYPE_ANY)
  515. INSTRUCTION_STATE(CALLNZ, "CALLNZ", 3, SS, MOJOSHADER_TYPE_ANY)
  516. INSTRUCTION_STATE(LOOP, "LOOP", 3, SS, MOJOSHADER_TYPE_ANY)
  517. INSTRUCTION_STATE(RET, "RET", 1, NULL, MOJOSHADER_TYPE_ANY)
  518. INSTRUCTION_STATE(ENDLOOP, "ENDLOOP", 2, NULL, MOJOSHADER_TYPE_ANY)
  519. INSTRUCTION_STATE(LABEL, "LABEL", 0, S, MOJOSHADER_TYPE_ANY)
  520. INSTRUCTION_STATE(DCL, "DCL", 0, DCL, MOJOSHADER_TYPE_ANY)
  521. INSTRUCTION_STATE(POW, "POW", 3, DSS, MOJOSHADER_TYPE_ANY)
  522. INSTRUCTION(CRS, "CRS", 2, DSS, MOJOSHADER_TYPE_ANY)
  523. INSTRUCTION(SGN, "SGN", 3, DSSS, MOJOSHADER_TYPE_ANY)
  524. INSTRUCTION(ABS, "ABS", 1, DS, MOJOSHADER_TYPE_ANY)
  525. INSTRUCTION(NRM, "NRM", 3, DS, MOJOSHADER_TYPE_ANY)
  526. INSTRUCTION_STATE(SINCOS, "SINCOS", 8, SINCOS, MOJOSHADER_TYPE_ANY)
  527. INSTRUCTION_STATE(REP, "REP", 3, S, MOJOSHADER_TYPE_ANY)
  528. INSTRUCTION_STATE(ENDREP, "ENDREP", 2, NULL, MOJOSHADER_TYPE_ANY)
  529. INSTRUCTION_STATE(IF, "IF", 3, S, MOJOSHADER_TYPE_ANY)
  530. INSTRUCTION_STATE(IFC, "IF", 3, SS, MOJOSHADER_TYPE_ANY)
  531. INSTRUCTION(ELSE, "ELSE", 1, NULL, MOJOSHADER_TYPE_ANY) // !!! FIXME: state!
  532. INSTRUCTION(ENDIF, "ENDIF", 1, NULL, MOJOSHADER_TYPE_ANY) // !!! FIXME: state!
  533. INSTRUCTION_STATE(BREAK, "BREAK", 1, NULL, MOJOSHADER_TYPE_ANY)
  534. INSTRUCTION_STATE(BREAKC, "BREAK", 3, SS, MOJOSHADER_TYPE_ANY)
  535. INSTRUCTION_STATE(MOVA, "MOVA", 1, DS, MOJOSHADER_TYPE_VERTEX)
  536. INSTRUCTION_STATE(DEFB, "DEFB", 0, DEFB, MOJOSHADER_TYPE_ANY)
  537. INSTRUCTION_STATE(DEFI, "DEFI", 0, DEFI, MOJOSHADER_TYPE_ANY)
  538. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  539. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  540. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  541. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  542. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  543. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  544. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  545. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  546. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  547. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  548. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  549. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  550. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  551. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  552. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  553. INSTRUCTION_STATE(TEXCRD, "TEXCRD", 1, TEXCRD, MOJOSHADER_TYPE_PIXEL)
  554. INSTRUCTION_STATE(TEXKILL, "TEXKILL", 2, D, MOJOSHADER_TYPE_PIXEL)
  555. INSTRUCTION_STATE(TEXLD, "TEXLD", 1, TEXLD, MOJOSHADER_TYPE_PIXEL)
  556. INSTRUCTION_STATE(TEXBEM, "TEXBEM", 1, DS, MOJOSHADER_TYPE_PIXEL)
  557. INSTRUCTION_STATE(TEXBEML, "TEXBEML", 2, DS, MOJOSHADER_TYPE_PIXEL)
  558. INSTRUCTION(TEXREG2AR, "TEXREG2AR", 1, DS, MOJOSHADER_TYPE_PIXEL)
  559. INSTRUCTION(TEXREG2GB, "TEXREG2GB", 1, DS, MOJOSHADER_TYPE_PIXEL)
  560. INSTRUCTION_STATE(TEXM3X2PAD, "TEXM3X2PAD", 1, DS, MOJOSHADER_TYPE_PIXEL)
  561. INSTRUCTION_STATE(TEXM3X2TEX, "TEXM3X2TEX", 1, DS, MOJOSHADER_TYPE_PIXEL)
  562. INSTRUCTION_STATE(TEXM3X3PAD, "TEXM3X3PAD", 1, DS, MOJOSHADER_TYPE_PIXEL)
  563. INSTRUCTION_STATE(TEXM3X3TEX, "TEXM3X3TEX", 1, DS, MOJOSHADER_TYPE_PIXEL)
  564. INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
  565. INSTRUCTION_STATE(TEXM3X3SPEC, "TEXM3X3SPEC", 1, DSS, MOJOSHADER_TYPE_PIXEL)
  566. INSTRUCTION_STATE(TEXM3X3VSPEC, "TEXM3X3VSPEC", 1, DS, MOJOSHADER_TYPE_PIXEL)
  567. INSTRUCTION(EXPP, "EXPP", 1, DS, MOJOSHADER_TYPE_ANY)
  568. INSTRUCTION_STATE(LOGP, "LOGP", 1, DS, MOJOSHADER_TYPE_ANY)
  569. INSTRUCTION_STATE(CND, "CND", 1, DSSS, MOJOSHADER_TYPE_PIXEL)
  570. INSTRUCTION_STATE(DEF, "DEF", 0, DEF, MOJOSHADER_TYPE_ANY)
  571. INSTRUCTION(TEXREG2RGB, "TEXREG2RGB", 1, DS, MOJOSHADER_TYPE_PIXEL)
  572. INSTRUCTION(TEXDP3TEX, "TEXDP3TEX", 1, DS, MOJOSHADER_TYPE_PIXEL)
  573. INSTRUCTION(TEXM3X2DEPTH, "TEXM3X2DEPTH", 1, DS, MOJOSHADER_TYPE_PIXEL)
  574. INSTRUCTION(TEXDP3, "TEXDP3", 1, DS, MOJOSHADER_TYPE_PIXEL)
  575. INSTRUCTION_STATE(TEXM3X3, "TEXM3X3", 1, DS, MOJOSHADER_TYPE_PIXEL)
  576. INSTRUCTION(TEXDEPTH, "TEXDEPTH", 1, D, MOJOSHADER_TYPE_PIXEL)
  577. INSTRUCTION_STATE(CMP, "CMP", 1, DSSS, MOJOSHADER_TYPE_PIXEL)
  578. INSTRUCTION(BEM, "BEM", 2, DSS, MOJOSHADER_TYPE_PIXEL)
  579. INSTRUCTION_STATE(DP2ADD, "DP2ADD", 2, DSSS, MOJOSHADER_TYPE_PIXEL)
  580. INSTRUCTION(DSX, "DSX", 2, DS, MOJOSHADER_TYPE_PIXEL)
  581. INSTRUCTION(DSY, "DSY", 2, DS, MOJOSHADER_TYPE_PIXEL)
  582. INSTRUCTION(TEXLDD, "TEXLDD", 3, DSSSS, MOJOSHADER_TYPE_PIXEL)
  583. INSTRUCTION_STATE(SETP, "SETP", 1, DSS, MOJOSHADER_TYPE_ANY)
  584. INSTRUCTION_STATE(TEXLDL, "TEXLDL", 2, DSS, MOJOSHADER_TYPE_ANY)
  585. INSTRUCTION_STATE(BREAKP, "BREAKP", 3, S, MOJOSHADER_TYPE_ANY)
  586. #endif
  587. // end of mojoshader_internal.h ...