Debian Codegraph @

mojoshader.h

raw symbols SCIP
1 /**
2 * MojoShader; generate shader programs from bytecode of compiled
3 * Direct3D shaders.
4 *
5 * Please see the file LICENSE.txt in the source's root directory.
6 *
7 * This file written by Ryan C. Gordon.
8 */
9
10 #ifndef _INCL_MOJOSHADER_H_
11 #define _INCL_MOJOSHADER_H_
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 /* You can define this if you aren't generating mojoshader_version.h */
18 #ifndef MOJOSHADER_NO_VERSION_INCLUDE
19 #include "mojoshader_version.h"
20 #endif
21
22 #ifndef MOJOSHADER_VERSION
23 #define MOJOSHADER_VERSION -1
24 #endif
25
26 #ifndef MOJOSHADER_CHANGESET
27 #define MOJOSHADER_CHANGESET "???"
28 #endif
29
30 #ifndef DECLSPEC
31 #ifdef _WIN32
32 #define DECLSPEC __declspec(dllexport)
33 #else
34 #define DECLSPEC
35 #endif
36 #endif
37
38 #ifndef MOJOSHADERCALL
39 #ifdef _WIN32
40 #define MOJOSHADERCALL __cdecl
41 #else
42 #define MOJOSHADERCALL
43 #endif
44 #endif
45
46 /* -Wpedantic nameless union/struct silencing */
47 #ifndef MOJOSHADERNAMELESS
48 #ifdef __GNUC__
49 #define MOJOSHADERNAMELESS __extension__
50 #else
51 #define MOJOSHADERNAMELESS
52 #endif /* __GNUC__ */
53 #endif /* MOJOSHADERNAMELESS */
54
55 /*
56 * For determining the version of MojoShader you are using:
57 * const int compiled_against = MOJOSHADER_VERSION;
58 * const int linked_against = MOJOSHADER_version();
59 *
60 * The version is a single integer that increments, not a major/minor value.
61 *
62 * Please note that since moving to git, this function always returns -1;
63 * when hosted in Mercurial, this number meant something, but even
64 * there it wasn't reliable. With git, this number no longer exists at all.
65 */
66 DECLSPEC int MOJOSHADER_version(void);
67
68 /*
69 * For determining the revision control changeset of MojoShader you are using:
70 * const char *compiled_against = MOJOSHADER_CHANGESET;
71 * const char *linked_against = MOJOSHADER_changeset();
72 *
73 * The version is an arbitrary, null-terminated ASCII string. It is probably
74 * a hash that represents a revision control changeset, and can't be
75 * compared to any other string to determine chronology.
76 *
77 * Do not attempt to free this string; it's statically allocated.
78 */
79 DECLSPEC const char *MOJOSHADER_changeset(void);
80
81 /*
82 * These allocators work just like the C runtime's malloc() and free()
83 * (in fact, they probably use malloc() and free() internally if you don't
84 * specify your own allocator, but don't rely on that behaviour).
85 * (data) is the pointer you supplied when specifying these allocator
86 * callbacks, in case you need instance-specific data...it is passed through
87 * to your allocator unmolested, and can be NULL if you like.
88 */
89 typedef void *(MOJOSHADERCALL *MOJOSHADER_malloc)(int bytes, void *data);
90 typedef void (MOJOSHADERCALL *MOJOSHADER_free)(void *ptr, void *data);
91
92
93 /*
94 * These are enum values, but they also can be used in bitmasks, so we can
95 * test if an opcode is acceptable: if (op->shader_types & ourtype) {} ...
96 */
97 typedef enum
98 {
99 MOJOSHADER_TYPE_UNKNOWN = 0,
100 MOJOSHADER_TYPE_PIXEL = (1 << 0),
101 MOJOSHADER_TYPE_VERTEX = (1 << 1),
102 MOJOSHADER_TYPE_GEOMETRY = (1 << 2), /* (not supported yet.) */
103 MOJOSHADER_TYPE_ANY = 0x7FFFFFFF /* used for bitmasks */
104 } MOJOSHADER_shaderType;
105
106 /*
107 * Data types for vertex attribute streams.
108 */
109 typedef enum
110 {
111 MOJOSHADER_ATTRIBUTE_UNKNOWN = -1, /* housekeeping; not returned. */
112 MOJOSHADER_ATTRIBUTE_BYTE,
113 MOJOSHADER_ATTRIBUTE_UBYTE,
114 MOJOSHADER_ATTRIBUTE_SHORT,
115 MOJOSHADER_ATTRIBUTE_USHORT,
116 MOJOSHADER_ATTRIBUTE_INT,
117 MOJOSHADER_ATTRIBUTE_UINT,
118 MOJOSHADER_ATTRIBUTE_FLOAT,
119 MOJOSHADER_ATTRIBUTE_DOUBLE,
120 MOJOSHADER_ATTRIBUTE_HALF_FLOAT /* MAYBE available in your OpenGL! */
121 } MOJOSHADER_attributeType;
122
123 /*
124 * Data types for uniforms. See MOJOSHADER_uniform for more information.
125 */
126 typedef enum
127 {
128 MOJOSHADER_UNIFORM_UNKNOWN = -1, /* housekeeping value; never returned. */
129 MOJOSHADER_UNIFORM_FLOAT,
130 MOJOSHADER_UNIFORM_INT,
131 MOJOSHADER_UNIFORM_BOOL
132 } MOJOSHADER_uniformType;
133
134 /*
135 * These are the uniforms to be set for a shader. "Uniforms" are what Direct3D
136 * calls "Constants" ... IDirect3DDevice::SetVertexShaderConstantF() would
137 * need this data, for example. These integers are register indexes. So if
138 * index==6 and type==MOJOSHADER_UNIFORM_FLOAT, that means we'd expect a
139 * 4-float vector to be specified for what would be register "c6" in D3D
140 * assembly language, before drawing with the shader.
141 * (array_count) means this is an array of uniforms...this happens in some
142 * profiles when we see a relative address ("c0[a0.x]", not the usual "c0").
143 * In those cases, the shader was built to set some range of constant
144 * registers as an array. You should set this array with (array_count)
145 * elements from the constant register file, starting at (index) instead of
146 * just a single uniform. To be extra difficult, you'll need to fill in the
147 * correct values from the MOJOSHADER_constant data into the appropriate
148 * parts of the array, overriding the constant register file. Fun!
149 * (constant) says whether this is a constant array; these need to be loaded
150 * once at creation time, from the constant list and not ever updated from
151 * the constant register file. This is a workaround for limitations in some
152 * profiles.
153 * (name) is a profile-specific variable name; it may be NULL if it isn't
154 * applicable to the requested profile.
155 */
156 typedef struct MOJOSHADER_uniform
157 {
158 MOJOSHADER_uniformType type;
159 int index;
160 int array_count;
161 int constant;
162 const char *name;
163 } MOJOSHADER_uniform;
164
165 /*
166 * These are the constants defined in a shader. These are data values
167 * hardcoded in a shader (with the DEF, DEFI, DEFB instructions), which
168 * override your Uniforms. This data is largely for informational purposes,
169 * since they are compiled in and can't be changed, like Uniforms can be.
170 * These integers are register indexes. So if index==6 and
171 * type==MOJOSHADER_UNIFORM_FLOAT, that means we'd expect a 4-float vector
172 * to be specified for what would be register "c6" in D3D assembly language,
173 * before drawing with the shader.
174 * (value) is the value of the constant, unioned by type.
175 */
176 typedef struct MOJOSHADER_constant
177 {
178 MOJOSHADER_uniformType type;
179 int index;
180 union
181 {
182 float f[4]; /* if type==MOJOSHADER_UNIFORM_FLOAT */
183 int i[4]; /* if type==MOJOSHADER_UNIFORM_INT */
184 int b; /* if type==MOJOSHADER_UNIFORM_BOOL */
185 } value;
186 } MOJOSHADER_constant;
187
188 /*
189 * Data types for samplers. See MOJOSHADER_sampler for more information.
190 */
191 typedef enum
192 {
193 MOJOSHADER_SAMPLER_UNKNOWN = -1, /* housekeeping value; never returned. */
194 MOJOSHADER_SAMPLER_2D,
195 MOJOSHADER_SAMPLER_CUBE,
196 MOJOSHADER_SAMPLER_VOLUME
197 } MOJOSHADER_samplerType;
198
199 /*
200 * These are the samplers to be set for a shader. ...
201 * IDirect3DDevice::SetTexture() would need this data, for example.
202 * These integers are the sampler "stage". So if index==6 and
203 * type==MOJOSHADER_SAMPLER_2D, that means we'd expect a regular 2D texture
204 * to be specified for what would be register "s6" in D3D assembly language,
205 * before drawing with the shader.
206 * (name) is a profile-specific variable name; it may be NULL if it isn't
207 * applicable to the requested profile.
208 * (texbem) will be non-zero if a TEXBEM opcode references this sampler. This
209 * is only used in legacy shaders (ps_1_1 through ps_1_3), but it needs some
210 * special support to work, as we have to load a magic uniform behind the
211 * scenes to support it. Most code can ignore this field in general, and no
212 * one has to touch it unless they really know what they're doing.
213 */
214 typedef struct MOJOSHADER_sampler
215 {
216 MOJOSHADER_samplerType type;
217 int index;
218 const char *name;
219 int texbem;
220 } MOJOSHADER_sampler;
221
222
223 /*
224 * This struct is used if you have to force a sampler to a specific type.
225 * Generally, you can ignore this, but if you have, say, a ps_1_1
226 * shader, you might need to specify what the samplers are meant to be
227 * to get correct results, as Shader Model 1 samples textures according
228 * to what is bound to a sampler at the moment instead of what the shader
229 * is hardcoded to expect.
230 */
231 typedef struct MOJOSHADER_samplerMap
232 {
233 int index;
234 MOJOSHADER_samplerType type;
235 } MOJOSHADER_samplerMap;
236
237 /*
238 * Data types for attributes. See MOJOSHADER_attribute for more information.
239 */
240 typedef enum
241 {
242 MOJOSHADER_USAGE_UNKNOWN = -1, /* housekeeping value; never returned. */
243 MOJOSHADER_USAGE_POSITION, /* 0-15 for Vertex, 1-15 for Pixel */
244 MOJOSHADER_USAGE_BLENDWEIGHT, /* 0-15 */
245 MOJOSHADER_USAGE_BLENDINDICES, /* 0-15 */
246 MOJOSHADER_USAGE_NORMAL, /* 0-15 */
247 MOJOSHADER_USAGE_POINTSIZE, /* 0-15 */
248 MOJOSHADER_USAGE_TEXCOORD, /* 0-15 */
249 MOJOSHADER_USAGE_TANGENT, /* 0-15 */
250 MOJOSHADER_USAGE_BINORMAL, /* 0-15 */
251 MOJOSHADER_USAGE_TESSFACTOR, /* 0 only */
252 MOJOSHADER_USAGE_POSITIONT, /* 0-15 for Vertex, 1-15 for Pixel */
253 MOJOSHADER_USAGE_COLOR, /* 0-15 but depends on MRT support */
254 MOJOSHADER_USAGE_FOG, /* 0-15 */
255 MOJOSHADER_USAGE_DEPTH, /* 0-15 */
256 MOJOSHADER_USAGE_SAMPLE,
257 MOJOSHADER_USAGE_TOTAL /* housekeeping value; never returned. */
258 } MOJOSHADER_usage;
259
260 /*
261 * These are the attributes to be set for a shader. "Attributes" are what
262 * Direct3D calls "Vertex Declarations Usages" ...
263 * IDirect3DDevice::CreateVertexDeclaration() would need this data, for
264 * example. Each attribute is associated with an array of data that uses one
265 * element per-vertex. So if usage==MOJOSHADER_USAGE_COLOR and index==1, that
266 * means we'd expect a secondary color array to be bound to this shader
267 * before drawing.
268 * (name) is a profile-specific variable name; it may be NULL if it isn't
269 * applicable to the requested profile.
270 */
271 typedef struct MOJOSHADER_attribute
272 {
273 MOJOSHADER_usage usage;
274 int index;
275 const char *name;
276 } MOJOSHADER_attribute;
277
278 /*
279 * Use this if you want to specify newly-parsed code to swizzle incoming
280 * data. This can be useful if you know, at parse time, that a shader
281 * will be processing data on COLOR0 that should be RGBA, but you'll
282 * be passing it a vertex array full of ARGB instead.
283 */
284 typedef struct MOJOSHADER_swizzle
285 {
286 MOJOSHADER_usage usage;
287 unsigned int index;
288 unsigned char swizzles[4]; /* {0,1,2,3} == .xyzw, {2,2,2,2} == .zzzz */
289 } MOJOSHADER_swizzle;
290
291
292 /*
293 * MOJOSHADER_symbol data.
294 *
295 * These are used to expose high-level information in shader bytecode.
296 * They associate HLSL variables with registers. This data is used for both
297 * debugging and optimization.
298 */
299
300 typedef enum
301 {
302 MOJOSHADER_SYMREGSET_BOOL=0,
303 MOJOSHADER_SYMREGSET_INT4,
304 MOJOSHADER_SYMREGSET_FLOAT4,
305 MOJOSHADER_SYMREGSET_SAMPLER,
306 MOJOSHADER_SYMREGSET_TOTAL /* housekeeping value; never returned. */
307 } MOJOSHADER_symbolRegisterSet;
308
309 typedef enum
310 {
311 MOJOSHADER_SYMCLASS_SCALAR=0,
312 MOJOSHADER_SYMCLASS_VECTOR,
313 MOJOSHADER_SYMCLASS_MATRIX_ROWS,
314 MOJOSHADER_SYMCLASS_MATRIX_COLUMNS,
315 MOJOSHADER_SYMCLASS_OBJECT,
316 MOJOSHADER_SYMCLASS_STRUCT,
317 MOJOSHADER_SYMCLASS_TOTAL /* housekeeping value; never returned. */
318 } MOJOSHADER_symbolClass;
319
320 typedef enum
321 {
322 MOJOSHADER_SYMTYPE_VOID=0,
323 MOJOSHADER_SYMTYPE_BOOL,
324 MOJOSHADER_SYMTYPE_INT,
325 MOJOSHADER_SYMTYPE_FLOAT,
326 MOJOSHADER_SYMTYPE_STRING,
327 MOJOSHADER_SYMTYPE_TEXTURE,
328 MOJOSHADER_SYMTYPE_TEXTURE1D,
329 MOJOSHADER_SYMTYPE_TEXTURE2D,
330 MOJOSHADER_SYMTYPE_TEXTURE3D,
331 MOJOSHADER_SYMTYPE_TEXTURECUBE,
332 MOJOSHADER_SYMTYPE_SAMPLER,
333 MOJOSHADER_SYMTYPE_SAMPLER1D,
334 MOJOSHADER_SYMTYPE_SAMPLER2D,
335 MOJOSHADER_SYMTYPE_SAMPLER3D,
336 MOJOSHADER_SYMTYPE_SAMPLERCUBE,
337 MOJOSHADER_SYMTYPE_PIXELSHADER,
338 MOJOSHADER_SYMTYPE_VERTEXSHADER,
339 MOJOSHADER_SYMTYPE_PIXELFRAGMENT,
340 MOJOSHADER_SYMTYPE_VERTEXFRAGMENT,
341 MOJOSHADER_SYMTYPE_UNSUPPORTED,
342 MOJOSHADER_SYMTYPE_TOTAL /* housekeeping value; never returned. */
343 } MOJOSHADER_symbolType;
344
345 typedef struct MOJOSHADER_symbolStructMember MOJOSHADER_symbolStructMember;
346
347 typedef struct MOJOSHADER_symbolTypeInfo
348 {
349 MOJOSHADER_symbolClass parameter_class;
350 MOJOSHADER_symbolType parameter_type;
351 unsigned int rows;
352 unsigned int columns;
353 unsigned int elements;
354 unsigned int member_count;
355 MOJOSHADER_symbolStructMember *members;
356 } MOJOSHADER_symbolTypeInfo;
357
358 struct MOJOSHADER_symbolStructMember
359 {
360 const char *name;
361 MOJOSHADER_symbolTypeInfo info;
362 };
363
364 typedef struct MOJOSHADER_symbol
365 {
366 const char *name;
367 MOJOSHADER_symbolRegisterSet register_set;
368 unsigned int register_index;
369 unsigned int register_count;
370 MOJOSHADER_symbolTypeInfo info;
371 } MOJOSHADER_symbol;
372
373
374 /*
375 * These are used with MOJOSHADER_error as special case positions.
376 */
377 #define MOJOSHADER_POSITION_NONE (-3)
378 #define MOJOSHADER_POSITION_BEFORE (-2)
379 #define MOJOSHADER_POSITION_AFTER (-1)
380
381 typedef struct MOJOSHADER_error
382 {
383 /*
384 * Human-readable error, if there is one. Will be NULL if there was no
385 * error. The string will be UTF-8 encoded, and English only. Most of
386 * these shouldn't be shown to the end-user anyhow.
387 */
388 const char *error;
389
390 /*
391 * Filename where error happened. This can be NULL if the information
392 * isn't available.
393 */
394 const char *filename;
395
396 /*
397 * Position of error, if there is one. Will be MOJOSHADER_POSITION_NONE if
398 * there was no error, MOJOSHADER_POSITION_BEFORE if there was an error
399 * before processing started, and MOJOSHADER_POSITION_AFTER if there was
400 * an error during final processing. If >= 0, MOJOSHADER_parse() sets
401 * this to the byte offset (starting at zero) into the bytecode you
402 * supplied, and MOJOSHADER_assemble(), MOJOSHADER_parseAst(), and
403 * MOJOSHADER_compile() sets this to a a line number in the source code
404 * you supplied (starting at one).
405 */
406 int error_position;
407 } MOJOSHADER_error;
408
409
410 /* !!! FIXME: document me. */
411 typedef enum MOJOSHADER_preshaderOpcode
412 {
413 MOJOSHADER_PRESHADEROP_NOP,
414 MOJOSHADER_PRESHADEROP_MOV,
415 MOJOSHADER_PRESHADEROP_NEG,
416 MOJOSHADER_PRESHADEROP_RCP,
417 MOJOSHADER_PRESHADEROP_FRC,
418 MOJOSHADER_PRESHADEROP_EXP,
419 MOJOSHADER_PRESHADEROP_LOG,
420 MOJOSHADER_PRESHADEROP_RSQ,
421 MOJOSHADER_PRESHADEROP_SIN,
422 MOJOSHADER_PRESHADEROP_COS,
423 MOJOSHADER_PRESHADEROP_ASIN,
424 MOJOSHADER_PRESHADEROP_ACOS,
425 MOJOSHADER_PRESHADEROP_ATAN,
426 MOJOSHADER_PRESHADEROP_MIN,
427 MOJOSHADER_PRESHADEROP_MAX,
428 MOJOSHADER_PRESHADEROP_LT,
429 MOJOSHADER_PRESHADEROP_GE,
430 MOJOSHADER_PRESHADEROP_ADD,
431 MOJOSHADER_PRESHADEROP_MUL,
432 MOJOSHADER_PRESHADEROP_ATAN2,
433 MOJOSHADER_PRESHADEROP_DIV,
434 MOJOSHADER_PRESHADEROP_CMP,
435 MOJOSHADER_PRESHADEROP_MOVC,
436 MOJOSHADER_PRESHADEROP_DOT,
437 MOJOSHADER_PRESHADEROP_NOISE,
438 MOJOSHADER_PRESHADEROP_SCALAR_OPS,
439 MOJOSHADER_PRESHADEROP_MIN_SCALAR = MOJOSHADER_PRESHADEROP_SCALAR_OPS,
440 MOJOSHADER_PRESHADEROP_MAX_SCALAR,
441 MOJOSHADER_PRESHADEROP_LT_SCALAR,
442 MOJOSHADER_PRESHADEROP_GE_SCALAR,
443 MOJOSHADER_PRESHADEROP_ADD_SCALAR,
444 MOJOSHADER_PRESHADEROP_MUL_SCALAR,
445 MOJOSHADER_PRESHADEROP_ATAN2_SCALAR,
446 MOJOSHADER_PRESHADEROP_DIV_SCALAR,
447 MOJOSHADER_PRESHADEROP_DOT_SCALAR,
448 MOJOSHADER_PRESHADEROP_NOISE_SCALAR
449 } MOJOSHADER_preshaderOpcode;
450
451 typedef enum MOJOSHADER_preshaderOperandType
452 {
453 MOJOSHADER_PRESHADEROPERAND_INPUT,
454 MOJOSHADER_PRESHADEROPERAND_OUTPUT,
455 MOJOSHADER_PRESHADEROPERAND_LITERAL,
456 MOJOSHADER_PRESHADEROPERAND_TEMP
457 } MOJOSHADER_preshaderOperandType;
458
459 typedef struct MOJOSHADER_preshaderOperand
460 {
461 MOJOSHADER_preshaderOperandType type;
462 unsigned int index;
463 unsigned int array_register_count;
464 unsigned int *array_registers;
465 } MOJOSHADER_preshaderOperand;
466
467 typedef struct MOJOSHADER_preshaderInstruction
468 {
469 MOJOSHADER_preshaderOpcode opcode;
470 unsigned int element_count;
471 unsigned int operand_count;
472 MOJOSHADER_preshaderOperand operands[4];
473 } MOJOSHADER_preshaderInstruction;
474
475 typedef struct MOJOSHADER_preshader
476 {
477 unsigned int literal_count;
478 double *literals;
479 unsigned int temp_count; /* scalar, not vector! */
480 unsigned int symbol_count;
481 MOJOSHADER_symbol *symbols;
482 unsigned int instruction_count;
483 MOJOSHADER_preshaderInstruction *instructions;
484 unsigned int register_count;
485 float *registers;
486 MOJOSHADER_malloc malloc;
487 MOJOSHADER_free free;
488 void *malloc_data;
489 } MOJOSHADER_preshader;
490
491 /*
492 * Structure used to return data from parsing of a shader...
493 */
494 /* !!! FIXME: most of these ints should be unsigned. */
495 typedef struct MOJOSHADER_parseData
496 {
497 /*
498 * The number of elements pointed to by (errors).
499 */
500 int error_count;
501
502 /*
503 * (error_count) elements of data that specify errors that were generated
504 * by parsing this shader.
505 * This can be NULL if there were no errors or if (error_count) is zero.
506 */
507 MOJOSHADER_error *errors;
508
509 /*
510 * The name of the profile used to parse the shader. Will be NULL on error.
511 */
512 const char *profile;
513
514 /*
515 * Bytes of output from parsing. Most profiles produce a string of source
516 * code, but profiles that do binary output may not be text at all.
517 * Will be NULL on error.
518 */
519 const char *output;
520
521 /*
522 * Byte count for output, not counting any null terminator. Most profiles
523 * produce an ASCII string of source code (which will be null-terminated
524 * even though that null char isn't included in output_len), but profiles
525 * that do binary output may not be text at all. Will be 0 on error.
526 */
527 int output_len;
528
529 /*
530 * Count of Direct3D instruction slots used. This is meaningless in terms
531 * of the actual output, as the profile will probably grow or reduce
532 * the count (or for high-level languages, not have that information at
533 * all). Also, as with Microsoft's own assembler, this value is just a
534 * rough estimate, as unpredicable real-world factors make the actual
535 * value vary at least a little from this count. Still, it can give you
536 * a rough idea of the size of your shader. Will be zero on error.
537 */
538 int instruction_count;
539
540 /*
541 * The type of shader we parsed. Will be MOJOSHADER_TYPE_UNKNOWN on error.
542 */
543 MOJOSHADER_shaderType shader_type;
544
545 /*
546 * The shader's major version. If this was a "vs_3_0", this would be 3.
547 */
548 int major_ver;
549
550 /*
551 * The shader's minor version. If this was a "ps_1_4", this would be 4.
552 * Two notes: for "vs_2_x", this is 1, and for "vs_3_sw", this is 255.
553 */
554 int minor_ver;
555
556 /*
557 * This is the main function name of the shader. This will be the
558 * caller-supplied string even if a given profile ignores it (GLSL,
559 * for example, always uses "main" in the shader output out of necessity,
560 * and Direct3D assembly has no concept of a "main function", etc).
561 * Otherwise, it'll be a default name chosen by the profile ("main") or
562 * whatnot.
563 */
564 const char *mainfn;
565
566 /*
567 * The number of elements pointed to by (uniforms).
568 */
569 int uniform_count;
570
571 /*
572 * (uniform_count) elements of data that specify Uniforms to be set for
573 * this shader. See discussion on MOJOSHADER_uniform for details.
574 * This can be NULL on error or if (uniform_count) is zero.
575 */
576 MOJOSHADER_uniform *uniforms;
577
578 /*
579 * The number of elements pointed to by (constants).
580 */
581 int constant_count;
582
583 /*
584 * (constant_count) elements of data that specify constants used in
585 * this shader. See discussion on MOJOSHADER_constant for details.
586 * This can be NULL on error or if (constant_count) is zero.
587 * This is largely informational: constants are hardcoded into a shader.
588 * The constants that you can set like parameters are in the "uniforms"
589 * list.
590 */
591 MOJOSHADER_constant *constants;
592
593 /*
594 * The number of elements pointed to by (samplers).
595 */
596 int sampler_count;
597
598 /*
599 * (sampler_count) elements of data that specify Samplers to be set for
600 * this shader. See discussion on MOJOSHADER_sampler for details.
601 * This can be NULL on error or if (sampler_count) is zero.
602 */
603 MOJOSHADER_sampler *samplers;
604
605 /* !!! FIXME: this should probably be "input" and not "attribute" */
606 /*
607 * The number of elements pointed to by (attributes).
608 */
609 int attribute_count;
610
611 /* !!! FIXME: this should probably be "input" and not "attribute" */
612 /*
613 * (attribute_count) elements of data that specify Attributes to be set
614 * for this shader. See discussion on MOJOSHADER_attribute for details.
615 * This can be NULL on error or if (attribute_count) is zero.
616 */
617 MOJOSHADER_attribute *attributes;
618
619 /*
620 * The number of elements pointed to by (outputs).
621 */
622 int output_count;
623
624 /*
625 * (output_count) elements of data that specify outputs this shader
626 * writes to. See discussion on MOJOSHADER_attribute for details.
627 * This can be NULL on error or if (output_count) is zero.
628 */
629 MOJOSHADER_attribute *outputs;
630
631 /*
632 * The number of elements pointed to by (swizzles).
633 */
634 int swizzle_count;
635
636 /* !!! FIXME: this should probably be "input" and not "attribute" */
637 /*
638 * (swizzle_count) elements of data that specify swizzles the shader will
639 * apply to incoming attributes. This is a copy of what was passed to
640 * MOJOSHADER_parseData().
641 * This can be NULL on error or if (swizzle_count) is zero.
642 */
643 MOJOSHADER_swizzle *swizzles;
644
645 /*
646 * The number of elements pointed to by (symbols).
647 */
648 int symbol_count;
649
650 /*
651 * (symbol_count) elements of data that specify high-level symbol data
652 * for the shader. This will be parsed from the CTAB section
653 * in bytecode, and will be a copy of what you provide to
654 * MOJOSHADER_assemble(). This data is optional.
655 * This can be NULL on error or if (symbol_count) is zero.
656 */
657 MOJOSHADER_symbol *symbols;
658
659 /*
660 * !!! FIXME: document me.
661 * This can be NULL on error or if no preshader was available.
662 */
663 MOJOSHADER_preshader *preshader;
664
665 /*
666 * This is the malloc implementation you passed to MOJOSHADER_parse().
667 */
668 MOJOSHADER_malloc malloc;
669
670 /*
671 * This is the free implementation you passed to MOJOSHADER_parse().
672 */
673 MOJOSHADER_free free;
674
675 /*
676 * This is the pointer you passed as opaque data for your allocator.
677 */
678 void *malloc_data;
679 } MOJOSHADER_parseData;
680
681
682 /*
683 * Profile string for Direct3D assembly language output.
684 */
685 #define MOJOSHADER_PROFILE_D3D "d3d"
686
687 /*
688 * Profile string for passthrough of the original bytecode, unchanged.
689 */
690 #define MOJOSHADER_PROFILE_BYTECODE "bytecode"
691
692 /*
693 * Profile string for HLSL Shader Model 4 output.
694 */
695 #define MOJOSHADER_PROFILE_HLSL "hlsl"
696
697 /*
698 * Profile string for GLSL: OpenGL high-level shader language output.
699 */
700 #define MOJOSHADER_PROFILE_GLSL "glsl"
701
702 /*
703 * Profile string for GLSL 1.20: minor improvements to base GLSL spec.
704 */
705 #define MOJOSHADER_PROFILE_GLSL120 "glsl120"
706
707 /*
708 * Profile string for GLSL ES: minor changes to GLSL output for ES compliance.
709 */
710 #define MOJOSHADER_PROFILE_GLSLES "glsles"
711
712 /*
713 * Profile string for GLSL ES: changes to GLSL output for ES 3.x compliance.
714 */
715 #define MOJOSHADER_PROFILE_GLSLES3 "glsles3"
716
717 /*
718 * Profile string for OpenGL ARB 1.0 shaders: GL_ARB_(vertex|fragment)_program.
719 */
720 #define MOJOSHADER_PROFILE_ARB1 "arb1"
721
722 /*
723 * Profile string for OpenGL ARB 1.0 shaders with Nvidia 2.0 extensions:
724 * GL_NV_vertex_program2_option and GL_NV_fragment_program2
725 */
726 #define MOJOSHADER_PROFILE_NV2 "nv2"
727
728 /*
729 * Profile string for OpenGL ARB 1.0 shaders with Nvidia 3.0 extensions:
730 * GL_NV_vertex_program3 and GL_NV_fragment_program2
731 */
732 #define MOJOSHADER_PROFILE_NV3 "nv3"
733
734 /*
735 * Profile string for OpenGL ARB 1.0 shaders with Nvidia 4.0 extensions:
736 * GL_NV_gpu_program4
737 */
738 #define MOJOSHADER_PROFILE_NV4 "nv4"
739
740 /*
741 * Profile string for Metal: Apple's lowlevel API's high-level shader language.
742 */
743 #define MOJOSHADER_PROFILE_METAL "metal"
744
745 /*
746 * Profile string for SPIR-V binary output
747 */
748 #define MOJOSHADER_PROFILE_SPIRV "spirv"
749
750 /*
751 * Profile string for ARB_gl_spirv-friendly SPIR-V binary output
752 */
753 #define MOJOSHADER_PROFILE_GLSPIRV "glspirv"
754
755 /*
756 * Determine the highest supported Shader Model for a profile.
757 */
758 DECLSPEC int MOJOSHADER_maxShaderModel(const char *profile);
759
760
761 /*
762 * Parse a compiled Direct3D shader's bytecode.
763 *
764 * This is your primary entry point into MojoShader. You need to pass it
765 * a compiled D3D shader and tell it which "profile" you want to use to
766 * convert it into useful data.
767 *
768 * The available profiles are the set of MOJOSHADER_PROFILE_* defines.
769 * Note that MojoShader may be built without support for all listed
770 * profiles (in which case using one here will return with an error).
771 *
772 * As parsing requires some memory to be allocated, you may provide a custom
773 * allocator to this function, which will be used to allocate/free memory.
774 * They function just like malloc() and free(). We do not use realloc().
775 * If you don't care, pass NULL in for the allocator functions. If your
776 * allocator needs instance-specific data, you may supply it with the
777 * (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
778 *
779 * This function returns a MOJOSHADER_parseData.
780 *
781 * This function will never return NULL, even if the system is completely
782 * out of memory upon entry (in which case, this function returns a static
783 * MOJOSHADER_parseData object, which is still safe to pass to
784 * MOJOSHADER_freeParseData()).
785 *
786 * You can tell the generated program to swizzle certain inputs. If you know
787 * that COLOR0 should be RGBA but you're passing in ARGB, you can specify
788 * a swizzle of { MOJOSHADER_USAGE_COLOR, 0, {1,2,3,0} } to (swiz). If the
789 * input register in the code would produce reg.ywzx, that swizzle would
790 * change it to reg.wzxy ... (swiz) can be NULL.
791 *
792 * You can force the shader to expect samplers of certain types. Generally
793 * you don't need this, as Shader Model 2 and later always specify what they
794 * expect samplers to be (2D, cubemap, etc). Shader Model 1, however, just
795 * uses whatever is bound to a given sampler at draw time, but this doesn't
796 * work in OpenGL, etc. In these cases, MojoShader will default to
797 * 2D texture sampling (or cubemap sampling, in cases where it makes sense,
798 * like the TEXM3X3TEX opcode), which works 75% of the time, but if you
799 * really needed something else, you'll need to specify it here. This can
800 * also be used, at your own risk, to override DCL opcodes in shaders: if
801 * the shader explicit says 2D, but you want Cubemap, for example, you can
802 * use this to override. If you aren't sure about any of this stuff, you can
803 * (and should) almost certainly ignore it: (smap) can be NULL.
804 *
805 * (bufsize) is the size in bytes of (tokenbuf). If (bufsize) is zero,
806 * MojoShader will attempt to figure out the size of the buffer, but you
807 * risk a buffer overflow if you have corrupt data, etc. Supply the value
808 * if you can.
809 *
810 * You should pass a name for your shader's main function in here, via the
811 * (mainfn) param. Some profiles need this name to be unique. Passing a NULL
812 * here will pick a reasonable default, and most profiles will ignore it
813 * anyhow. As the name of the shader's main function, etc, so make it a
814 * simple name that would match C's identifier rules. Keep it simple!
815 *
816 * This function is thread safe, so long as (m) and (f) are too, and that
817 * (tokenbuf) remains intact for the duration of the call. This allows you
818 * to parse several shaders on separate CPU cores at the same time.
819 */
820 DECLSPEC const MOJOSHADER_parseData *MOJOSHADER_parse(const char *profile,
821 const char *mainfn,
822 const unsigned char *tokenbuf,
823 const unsigned int bufsize,
824 const MOJOSHADER_swizzle *swiz,
825 const unsigned int swizcount,
826 const MOJOSHADER_samplerMap *smap,
827 const unsigned int smapcount,
828 MOJOSHADER_malloc m,
829 MOJOSHADER_free f,
830 void *d);
831
832
833 /*
834 * Call this to dispose of parsing results when you are done with them.
835 * This will call the MOJOSHADER_free function you provided to
836 * MOJOSHADER_parse multiple times, if you provided one.
837 * Passing a NULL here is a safe no-op.
838 *
839 * This function is thread safe, so long as any allocator you passed into
840 * MOJOSHADER_parse() is, too.
841 */
842 DECLSPEC void MOJOSHADER_freeParseData(const MOJOSHADER_parseData *data);
843
844
845 /*
846 * You almost certainly don't need this function, unless you absolutely know
847 * why you need it without hesitation. This is useful if you're doing
848 * extremely low-level shader work or building specialized tools.
849 *
850 * Parse a preshader structure. This expects a buffer of bytes that represents
851 * the preshader data starting with its magic number token and ending at
852 * the end of the comment tokens that contain this preshader. Note that it
853 * does _not_ start at the beginning of the comment tokens.
854 *
855 * On success, this will return a MOJOSHADER_preshader. This can be
856 * deallocated later by calling MOJOSHADER_freePreshader(). On failure,
857 * this will return NULL. Unlike other MojoShader APIs, this assumes you
858 * either have a complete and valid buffer of preshader tokens or you have
859 * incomplete/corrupted data, so there is no explicit error reporting. Please
860 * note that if the system runs out of memory, this function will also return
861 * NULL without distinction.
862 *
863 * This function is thread safe, so long as any allocator you passed into
864 * MOJOSHADER_parsePreshader() is, too.
865 */
866 DECLSPEC const MOJOSHADER_preshader *MOJOSHADER_parsePreshader(const unsigned char *buf,
867 const unsigned int len,
868 MOJOSHADER_malloc m,
869 MOJOSHADER_free f,
870 void *d);
871
872
873 /*
874 * You almost certainly don't need this function, unless you absolutely know
875 * why you need it without hesitation. This is useful if you're doing
876 * extremely low-level shader work or building specialized tools.
877 *
878 * Call this to dispose of preshader parsing results when you are done with
879 * them. This will call the MOJOSHADER_free function you provided to
880 * MOJOSHADER_parsePreshader() multiple times, if you provided one.
881 * Passing a NULL here is a safe no-op.
882 *
883 * You only need to call this function for results from a call to
884 * MOJOSHADER_parsePreshader(). Other MojoShader structures with a preshader
885 * field, such as MOJOSHADER_parseData(), should not use this function, as
886 * the preshader will be deallocated with everything else in
887 * MOJOSHADER_freeParseData(), etc.
888 *
889 * This function is thread safe, so long as any allocator you passed into
890 * MOJOSHADER_parsePreshader() is, too.
891 */
892 DECLSPEC void MOJOSHADER_freePreshader(const MOJOSHADER_preshader *preshader);
893
894
895 /* SPIR-V interface... */
896
897 typedef enum
898 {
899 MOJOSHADER_VERTEXELEMENTFORMAT_SINGLE,
900 MOJOSHADER_VERTEXELEMENTFORMAT_VECTOR2,
901 MOJOSHADER_VERTEXELEMENTFORMAT_VECTOR3,
902 MOJOSHADER_VERTEXELEMENTFORMAT_VECTOR4,
903 MOJOSHADER_VERTEXELEMENTFORMAT_COLOR,
904 MOJOSHADER_VERTEXELEMENTFORMAT_BYTE4,
905 MOJOSHADER_VERTEXELEMENTFORMAT_SHORT2,
906 MOJOSHADER_VERTEXELEMENTFORMAT_SHORT4,
907 MOJOSHADER_VERTEXELEMENTFORMAT_NORMALIZEDSHORT2,
908 MOJOSHADER_VERTEXELEMENTFORMAT_NORMALIZEDSHORT4,
909 MOJOSHADER_VERTEXELEMENTFORMAT_HALFVECTOR2,
910 MOJOSHADER_VERTEXELEMENTFORMAT_HALFVECTOR4
911 } MOJOSHADER_vertexElementFormat;
912
913 typedef struct MOJOSHADER_vertexAttribute
914 {
915 MOJOSHADER_usage usage;
916 MOJOSHADER_vertexElementFormat vertexElementFormat;
917 int usageIndex;
918 } MOJOSHADER_vertexAttribute;
919
920 /*
921 * You almost certainly don't need this function, unless you absolutely know
922 * why you need it without hesitation. This is useful if you're doing
923 * extremely low-level shader work or building specialized tools.
924 *
925 * Call this to patch SPIR-V output returned from MOJOSHADER_parse to correctly
926 * interpret vertex input and link vertex shader output to pixel shader input.
927 *
928 * On success, this returns the size of the shaders' internal "patch table",
929 * which should be subtracted from the parseData's output_len when passing the
930 * final SPIR-V to your shader compiler.
931 */
932 DECLSPEC int MOJOSHADER_linkSPIRVShaders(const MOJOSHADER_parseData *vertex_spirv,
933 const MOJOSHADER_parseData *pixel_spirv,
934 const MOJOSHADER_vertexAttribute *vertexAttributes,
935 const int vertexAttributeCount);
936
937 /* Preprocessor interface... */
938
939 /*
940 * Structure used to pass predefined macros. Maps to D3DXMACRO.
941 * You can have macro arguments: set identifier to "a(b, c)" or whatever.
942 */
943 typedef struct MOJOSHADER_preprocessorDefine
944 {
945 const char *identifier;
946 const char *definition;
947 } MOJOSHADER_preprocessorDefine;
948
949 /*
950 * Used with the MOJOSHADER_includeOpen callback. Maps to D3DXINCLUDE_TYPE.
951 */
952 typedef enum
953 {
954 MOJOSHADER_INCLUDETYPE_LOCAL, /* local header: #include "blah.h" */
955 MOJOSHADER_INCLUDETYPE_SYSTEM /* system header: #include <blah.h> */
956 } MOJOSHADER_includeType;
957
958
959 /*
960 * Structure used to return data from preprocessing of a shader...
961 */
962 /* !!! FIXME: most of these ints should be unsigned. */
963 typedef struct MOJOSHADER_preprocessData
964 {
965 /*
966 * The number of elements pointed to by (errors).
967 */
968 int error_count;
969
970 /*
971 * (error_count) elements of data that specify errors that were generated
972 * by parsing this shader.
973 * This can be NULL if there were no errors or if (error_count) is zero.
974 */
975 MOJOSHADER_error *errors;
976
977 /*
978 * Bytes of output from preprocessing. This is a UTF-8 string. We
979 * guarantee it to be NULL-terminated. Will be NULL on error.
980 */
981 const char *output;
982
983 /*
984 * Byte count for output, not counting any null terminator.
985 * Will be 0 on error.
986 */
987 int output_len;
988
989 /*
990 * This is the malloc implementation you passed to MOJOSHADER_parse().
991 */
992 MOJOSHADER_malloc malloc;
993
994 /*
995 * This is the free implementation you passed to MOJOSHADER_parse().
996 */
997 MOJOSHADER_free free;
998
999 /*
1000 * This is the pointer you passed as opaque data for your allocator.
1001 */
1002 void *malloc_data;
1003 } MOJOSHADER_preprocessData;
1004
1005
1006 /*
1007 * This callback allows an app to handle #include statements for the
1008 * preprocessor. When the preprocessor sees an #include, it will call this
1009 * function to obtain the contents of the requested file. This is optional;
1010 * the preprocessor will open files directly if no callback is supplied, but
1011 * this allows an app to retrieve data from something other than the
1012 * traditional filesystem (for example, headers packed in a .zip file or
1013 * headers generated on-the-fly).
1014 *
1015 * This function maps to ID3DXInclude::Open()
1016 *
1017 * (inctype) specifies the type of header we wish to include.
1018 * (fname) specifies the name of the file specified on the #include line.
1019 * (parent) is a string of the entire source file containing the include, in
1020 * its original, not-yet-preprocessed state. Note that this is just the
1021 * contents of the specific file, not all source code that the preprocessor
1022 * has seen through other includes, etc.
1023 * (outdata) will be set by the callback to a pointer to the included file's
1024 * contents. The callback is responsible for allocating this however they
1025 * see fit (we provide allocator functions, but you may ignore them). This
1026 * pointer must remain valid until the includeClose callback runs. This
1027 * string does not need to be NULL-terminated.
1028 * (outbytes) will be set by the callback to the number of bytes pointed to
1029 * by (outdata).
1030 * (m),(f), and (d) are the allocator details that the application passed to
1031 * MojoShader. If these were NULL, MojoShader may have replaced them with its
1032 * own internal allocators.
1033 *
1034 * The callback returns zero on error, non-zero on success.
1035 *
1036 * If you supply an includeOpen callback, you must supply includeClose, too.
1037 */
1038 typedef int (MOJOSHADERCALL *MOJOSHADER_includeOpen)(MOJOSHADER_includeType inctype,
1039 const char *fname, const char *parent,
1040 const char **outdata, unsigned int *outbytes,
1041 MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
1042
1043 /*
1044 * This callback allows an app to clean up the results of a previous
1045 * includeOpen callback.
1046 *
1047 * This function maps to ID3DXInclude::Close()
1048 *
1049 * (data) is the data that was returned from a previous call to includeOpen.
1050 * It is now safe to deallocate this data.
1051 * (m),(f), and (d) are the same allocator details that were passed to your
1052 * includeOpen callback.
1053 *
1054 * If you supply an includeClose callback, you must supply includeOpen, too.
1055 */
1056 typedef void (MOJOSHADERCALL *MOJOSHADER_includeClose)(const char *data,
1057 MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
1058
1059
1060 /*
1061 * This function is optional. Even if you are dealing with shader source
1062 * code, you don't need to explicitly use the preprocessor, as the compiler
1063 * and assembler will use it behind the scenes. In fact, you probably never
1064 * need this function unless you are debugging a custom tool (or debugging
1065 * MojoShader itself).
1066 *
1067 * Preprocessing roughly follows the syntax of an ANSI C preprocessor, as
1068 * Microsoft's Direct3D assembler and HLSL compiler use this syntax. Please
1069 * note that we try to match the output you'd get from Direct3D's
1070 * preprocessor, which has some quirks if you're expecting output that matches
1071 * a generic C preprocessor.
1072 *
1073 * This function maps to D3DXPreprocessShader().
1074 *
1075 * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
1076 * actually access this file, as we obtain our data from (source). This
1077 * string is copied when we need to report errors while processing (source),
1078 * as opposed to errors in a file referenced via the #include directive in
1079 * (source). If this is NULL, then errors will report the filename as NULL,
1080 * too.
1081 *
1082 * (source) is an string of UTF-8 text to preprocess. It does not need to be
1083 * NULL-terminated.
1084 *
1085 * (sourcelen) is the length of the string pointed to by (source), in bytes.
1086 *
1087 * (defines) points to (define_count) preprocessor definitions, and can be
1088 * NULL. These are treated by the preprocessor as if the source code started
1089 * with one #define for each entry you pass in here.
1090 *
1091 * (include_open) and (include_close) let the app control the preprocessor's
1092 * behaviour for #include statements. Both are optional and can be NULL, but
1093 * both must be specified if either is specified.
1094 *
1095 * This will return a MOJOSHADER_preprocessorData. You should pass this
1096 * return value to MOJOSHADER_freePreprocessData() when you are done with
1097 * it.
1098 *
1099 * This function will never return NULL, even if the system is completely
1100 * out of memory upon entry (in which case, this function returns a static
1101 * MOJOSHADER_preprocessData object, which is still safe to pass to
1102 * MOJOSHADER_freePreprocessData()).
1103 *
1104 * As preprocessing requires some memory to be allocated, you may provide a
1105 * custom allocator to this function, which will be used to allocate/free
1106 * memory. They function just like malloc() and free(). We do not use
1107 * realloc(). If you don't care, pass NULL in for the allocator functions.
1108 * If your allocator needs instance-specific data, you may supply it with the
1109 * (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
1110 *
1111 * This function is thread safe, so long as the various callback functions
1112 * are, too, and that the parameters remains intact for the duration of the
1113 * call. This allows you to preprocess several shaders on separate CPU cores
1114 * at the same time.
1115 */
1116 DECLSPEC const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *filename,
1117 const char *source, unsigned int sourcelen,
1118 const MOJOSHADER_preprocessorDefine *defines,
1119 unsigned int define_count,
1120 MOJOSHADER_includeOpen include_open,
1121 MOJOSHADER_includeClose include_close,
1122 MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
1123
1124
1125 /*
1126 * Call this to dispose of preprocessing results when you are done with them.
1127 * This will call the MOJOSHADER_free function you provided to
1128 * MOJOSHADER_preprocess() multiple times, if you provided one.
1129 * Passing a NULL here is a safe no-op.
1130 *
1131 * This function is thread safe, so long as any allocator you passed into
1132 * MOJOSHADER_preprocess() is, too.
1133 */
1134 DECLSPEC void MOJOSHADER_freePreprocessData(const MOJOSHADER_preprocessData *data);
1135
1136
1137 /* Assembler interface... */
1138
1139 /*
1140 * This function is optional. Use this to convert Direct3D shader assembly
1141 * language into bytecode, which can be handled by MOJOSHADER_parse().
1142 *
1143 * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
1144 * actually access this file, as we obtain our data from (source). This
1145 * string is copied when we need to report errors while processing (source),
1146 * as opposed to errors in a file referenced via the #include directive in
1147 * (source). If this is NULL, then errors will report the filename as NULL,
1148 * too.
1149 *
1150 * (source) is an UTF-8 string of valid Direct3D shader assembly source code.
1151 * It does not need to be NULL-terminated.
1152 *
1153 * (sourcelen) is the length of the string pointed to by (source), in bytes.
1154 *
1155 * (comments) points to (comment_count) NULL-terminated UTF-8 strings, and
1156 * can be NULL. These strings are inserted as comments in the bytecode.
1157 *
1158 * (symbols) points to (symbol_count) symbol structs, and can be NULL. These
1159 * become a CTAB field in the bytecode. This is optional, but
1160 * MOJOSHADER_parse() needs CTAB data for all arrays used in a program, or
1161 * relative addressing will not be permitted, so you'll want to at least
1162 * provide symbol information for those. The symbol data is 100% trusted
1163 * at this time; it will not be checked to see if it matches what was
1164 * assembled in any way whatsoever.
1165 *
1166 * (defines) points to (define_count) preprocessor definitions, and can be
1167 * NULL. These are treated by the preprocessor as if the source code started
1168 * with one #define for each entry you pass in here.
1169 *
1170 * (include_open) and (include_close) let the app control the preprocessor's
1171 * behaviour for #include statements. Both are optional and can be NULL, but
1172 * both must be specified if either is specified.
1173 *
1174 * This will return a MOJOSHADER_parseData, like MOJOSHADER_parse() would,
1175 * except the profile will be MOJOSHADER_PROFILE_BYTECODE and the output
1176 * will be the assembled bytecode instead of some other language. This output
1177 * can be pushed back through MOJOSHADER_parseData() with a different profile.
1178 *
1179 * This function will never return NULL, even if the system is completely
1180 * out of memory upon entry (in which case, this function returns a static
1181 * MOJOSHADER_parseData object, which is still safe to pass to
1182 * MOJOSHADER_freeParseData()).
1183 *
1184 * As assembling requires some memory to be allocated, you may provide a
1185 * custom allocator to this function, which will be used to allocate/free
1186 * memory. They function just like malloc() and free(). We do not use
1187 * realloc(). If you don't care, pass NULL in for the allocator functions.
1188 * If your allocator needs instance-specific data, you may supply it with the
1189 * (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
1190 *
1191 * This function is thread safe, so long as the various callback functions
1192 * are, too, and that the parameters remains intact for the duration of the
1193 * call. This allows you to assemble several shaders on separate CPU cores
1194 * at the same time.
1195 */
1196 DECLSPEC const MOJOSHADER_parseData *MOJOSHADER_assemble(const char *filename,
1197 const char *source, unsigned int sourcelen,
1198 const char **comments, unsigned int comment_count,
1199 const MOJOSHADER_symbol *symbols,
1200 unsigned int symbol_count,
1201 const MOJOSHADER_preprocessorDefine *defines,
1202 unsigned int define_count,
1203 MOJOSHADER_includeOpen include_open,
1204 MOJOSHADER_includeClose include_close,
1205 MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
1206
1207
1208 /* High level shading language support... */
1209
1210 /*
1211 * Source profile strings for HLSL: Direct3D High Level Shading Language.
1212 */
1213 #define MOJOSHADER_SRC_PROFILE_HLSL_VS_1_1 "hlsl_vs_1_1"
1214 #define MOJOSHADER_SRC_PROFILE_HLSL_VS_2_0 "hlsl_vs_2_0"
1215 #define MOJOSHADER_SRC_PROFILE_HLSL_VS_3_0 "hlsl_vs_3_0"
1216 #define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_1 "hlsl_ps_1_1"
1217 #define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_2 "hlsl_ps_1_2"
1218 #define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_3 "hlsl_ps_1_3"
1219 #define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_4 "hlsl_ps_1_4"
1220 #define MOJOSHADER_SRC_PROFILE_HLSL_PS_2_0 "hlsl_ps_2_0"
1221 #define MOJOSHADER_SRC_PROFILE_HLSL_PS_3_0 "hlsl_ps_3_0"
1222
1223
1224 /* Abstract Syntax Tree interface... */
1225
1226 /*
1227 * ATTENTION: This adds a lot of stuff to the API, but almost everyone can
1228 * ignore this section. Seriously, go ahead and skip over anything that has
1229 * "AST" in it, unless you know why you'd want to use it.
1230 *
1231 * ALSO: This API is still evolving! We make no promises at this time to keep
1232 * source or binary compatibility for the AST pieces.
1233 *
1234 * Important notes:
1235 * - ASTs are the result of parsing the source code: a program that fails to
1236 * compile will often parse successfully. Undeclared variables,
1237 * type incompatibilities, etc, aren't detected at this point.
1238 * - Vector swizzles (the ".xyzw" part of "MyVec4.xyzw") will look like
1239 * structure dereferences. We don't realize these are actually swizzles
1240 * until semantic analysis.
1241 * - MOJOSHADER_astDataType info is not reliable when returned from
1242 * MOJOSHADER_parseAst()! Most of the datatype info will be missing or have
1243 * inaccurate data types. We sort these out during semantic analysis, which
1244 * happens after the AST parsing is complete. A few are filled in, or can
1245 * be deduced fairly trivially by processing several pieces into one.
1246 * It's enough that you can reproduce the original source code, more or
1247 * less, from the AST.
1248 */
1249
1250 /* High-level datatypes for AST nodes. */
1251 typedef enum MOJOSHADER_astDataTypeType
1252 {
1253 MOJOSHADER_AST_DATATYPE_NONE,
1254 MOJOSHADER_AST_DATATYPE_BOOL,
1255 MOJOSHADER_AST_DATATYPE_INT,
1256 MOJOSHADER_AST_DATATYPE_UINT,
1257 MOJOSHADER_AST_DATATYPE_FLOAT,
1258 MOJOSHADER_AST_DATATYPE_FLOAT_SNORM,
1259 MOJOSHADER_AST_DATATYPE_FLOAT_UNORM,
1260 MOJOSHADER_AST_DATATYPE_HALF,
1261 MOJOSHADER_AST_DATATYPE_DOUBLE,
1262 MOJOSHADER_AST_DATATYPE_STRING,
1263 MOJOSHADER_AST_DATATYPE_SAMPLER_1D,
1264 MOJOSHADER_AST_DATATYPE_SAMPLER_2D,
1265 MOJOSHADER_AST_DATATYPE_SAMPLER_3D,
1266 MOJOSHADER_AST_DATATYPE_SAMPLER_CUBE,
1267 MOJOSHADER_AST_DATATYPE_SAMPLER_STATE,
1268 MOJOSHADER_AST_DATATYPE_SAMPLER_COMPARISON_STATE,
1269 MOJOSHADER_AST_DATATYPE_STRUCT,
1270 MOJOSHADER_AST_DATATYPE_ARRAY,
1271 MOJOSHADER_AST_DATATYPE_VECTOR,
1272 MOJOSHADER_AST_DATATYPE_MATRIX,
1273 MOJOSHADER_AST_DATATYPE_BUFFER,
1274 MOJOSHADER_AST_DATATYPE_FUNCTION,
1275 MOJOSHADER_AST_DATATYPE_USER
1276 } MOJOSHADER_astDataTypeType;
1277 #define MOJOSHADER_AST_DATATYPE_CONST (1 << 31)
1278
1279 typedef union MOJOSHADER_astDataType MOJOSHADER_astDataType;
1280
1281 /* This is just part of DataTypeStruct, never appears outside of it. */
1282 typedef struct MOJOSHADER_astDataTypeStructMember
1283 {
1284 const MOJOSHADER_astDataType *datatype;
1285 const char *identifier;
1286 } MOJOSHADER_astDataTypeStructMember;
1287
1288 typedef struct MOJOSHADER_astDataTypeStruct
1289 {
1290 MOJOSHADER_astDataTypeType type;
1291 const MOJOSHADER_astDataTypeStructMember *members;
1292 int member_count;
1293 } MOJOSHADER_astDataTypeStruct;
1294
1295 typedef struct MOJOSHADER_astDataTypeArray
1296 {
1297 MOJOSHADER_astDataTypeType type;
1298 const MOJOSHADER_astDataType *base;
1299 int elements;
1300 } MOJOSHADER_astDataTypeArray;
1301
1302 typedef MOJOSHADER_astDataTypeArray MOJOSHADER_astDataTypeVector;
1303
1304 typedef struct MOJOSHADER_astDataTypeMatrix
1305 {
1306 MOJOSHADER_astDataTypeType type;
1307 const MOJOSHADER_astDataType *base;
1308 int rows;
1309 int columns;
1310 } MOJOSHADER_astDataTypeMatrix;
1311
1312 typedef struct MOJOSHADER_astDataTypeBuffer
1313 {
1314 MOJOSHADER_astDataTypeType type;
1315 const MOJOSHADER_astDataType *base;
1316 } MOJOSHADER_astDataTypeBuffer;
1317
1318 typedef struct MOJOSHADER_astDataTypeFunction
1319 {
1320 MOJOSHADER_astDataTypeType type;
1321 const MOJOSHADER_astDataType *retval;
1322 const MOJOSHADER_astDataType **params;
1323 int num_params;
1324 int intrinsic; /* non-zero for built-in functions */
1325 } MOJOSHADER_astDataTypeFunction;
1326
1327 typedef struct MOJOSHADER_astDataTypeUser
1328 {
1329 MOJOSHADER_astDataTypeType type;
1330 const MOJOSHADER_astDataType *details;
1331 const char *name;
1332 } MOJOSHADER_astDataTypeUser;
1333
1334 union MOJOSHADER_astDataType
1335 {
1336 MOJOSHADER_astDataTypeType type;
1337 MOJOSHADER_astDataTypeArray array;
1338 MOJOSHADER_astDataTypeStruct structure;
1339 MOJOSHADER_astDataTypeVector vector;
1340 MOJOSHADER_astDataTypeMatrix matrix;
1341 MOJOSHADER_astDataTypeBuffer buffer;
1342 MOJOSHADER_astDataTypeUser user;
1343 MOJOSHADER_astDataTypeFunction function;
1344 };
1345
1346 /* Structures that make up the parse tree... */
1347
1348 typedef enum MOJOSHADER_astNodeType
1349 {
1350 MOJOSHADER_AST_OP_START_RANGE, /* expression operators. */
1351
1352 MOJOSHADER_AST_OP_START_RANGE_UNARY, /* unary operators. */
1353 MOJOSHADER_AST_OP_PREINCREMENT,
1354 MOJOSHADER_AST_OP_PREDECREMENT,
1355 MOJOSHADER_AST_OP_NEGATE,
1356 MOJOSHADER_AST_OP_COMPLEMENT,
1357 MOJOSHADER_AST_OP_NOT,
1358 MOJOSHADER_AST_OP_POSTINCREMENT,
1359 MOJOSHADER_AST_OP_POSTDECREMENT,
1360 MOJOSHADER_AST_OP_CAST,
1361 MOJOSHADER_AST_OP_END_RANGE_UNARY,
1362
1363 MOJOSHADER_AST_OP_START_RANGE_BINARY, /* binary operators. */
1364 MOJOSHADER_AST_OP_COMMA,
1365 MOJOSHADER_AST_OP_MULTIPLY,
1366 MOJOSHADER_AST_OP_DIVIDE,
1367 MOJOSHADER_AST_OP_MODULO,
1368 MOJOSHADER_AST_OP_ADD,
1369 MOJOSHADER_AST_OP_SUBTRACT,
1370 MOJOSHADER_AST_OP_LSHIFT,
1371 MOJOSHADER_AST_OP_RSHIFT,
1372 MOJOSHADER_AST_OP_LESSTHAN,
1373 MOJOSHADER_AST_OP_GREATERTHAN,
1374 MOJOSHADER_AST_OP_LESSTHANOREQUAL,
1375 MOJOSHADER_AST_OP_GREATERTHANOREQUAL,
1376 MOJOSHADER_AST_OP_EQUAL,
1377 MOJOSHADER_AST_OP_NOTEQUAL,
1378 MOJOSHADER_AST_OP_BINARYAND,
1379 MOJOSHADER_AST_OP_BINARYXOR,
1380 MOJOSHADER_AST_OP_BINARYOR,
1381 MOJOSHADER_AST_OP_LOGICALAND,
1382 MOJOSHADER_AST_OP_LOGICALOR,
1383 MOJOSHADER_AST_OP_ASSIGN,
1384 MOJOSHADER_AST_OP_MULASSIGN,
1385 MOJOSHADER_AST_OP_DIVASSIGN,
1386 MOJOSHADER_AST_OP_MODASSIGN,
1387 MOJOSHADER_AST_OP_ADDASSIGN,
1388 MOJOSHADER_AST_OP_SUBASSIGN,
1389 MOJOSHADER_AST_OP_LSHIFTASSIGN,
1390 MOJOSHADER_AST_OP_RSHIFTASSIGN,
1391 MOJOSHADER_AST_OP_ANDASSIGN,
1392 MOJOSHADER_AST_OP_XORASSIGN,
1393 MOJOSHADER_AST_OP_ORASSIGN,
1394 MOJOSHADER_AST_OP_DEREF_ARRAY,
1395 MOJOSHADER_AST_OP_END_RANGE_BINARY,
1396
1397 MOJOSHADER_AST_OP_START_RANGE_TERNARY, /* ternary operators. */
1398 MOJOSHADER_AST_OP_CONDITIONAL,
1399 MOJOSHADER_AST_OP_END_RANGE_TERNARY,
1400
1401 MOJOSHADER_AST_OP_START_RANGE_DATA, /* expression operands. */
1402 MOJOSHADER_AST_OP_IDENTIFIER,
1403 MOJOSHADER_AST_OP_INT_LITERAL,
1404 MOJOSHADER_AST_OP_FLOAT_LITERAL,
1405 MOJOSHADER_AST_OP_STRING_LITERAL,
1406 MOJOSHADER_AST_OP_BOOLEAN_LITERAL,
1407 MOJOSHADER_AST_OP_END_RANGE_DATA,
1408
1409 MOJOSHADER_AST_OP_START_RANGE_MISC, /* other expression things. */
1410 MOJOSHADER_AST_OP_DEREF_STRUCT,
1411 MOJOSHADER_AST_OP_CALLFUNC,
1412 MOJOSHADER_AST_OP_CONSTRUCTOR,
1413 MOJOSHADER_AST_OP_END_RANGE_MISC,
1414 MOJOSHADER_AST_OP_END_RANGE,
1415
1416 MOJOSHADER_AST_COMPUNIT_START_RANGE, /* things in global scope. */
1417 MOJOSHADER_AST_COMPUNIT_FUNCTION,
1418 MOJOSHADER_AST_COMPUNIT_TYPEDEF,
1419 MOJOSHADER_AST_COMPUNIT_STRUCT,
1420 MOJOSHADER_AST_COMPUNIT_VARIABLE,
1421 MOJOSHADER_AST_COMPUNIT_END_RANGE,
1422
1423 MOJOSHADER_AST_STATEMENT_START_RANGE, /* statements in function scope. */
1424 MOJOSHADER_AST_STATEMENT_EMPTY,
1425 MOJOSHADER_AST_STATEMENT_BREAK,
1426 MOJOSHADER_AST_STATEMENT_CONTINUE,
1427 MOJOSHADER_AST_STATEMENT_DISCARD,
1428 MOJOSHADER_AST_STATEMENT_BLOCK,
1429 MOJOSHADER_AST_STATEMENT_EXPRESSION,
1430 MOJOSHADER_AST_STATEMENT_IF,
1431 MOJOSHADER_AST_STATEMENT_SWITCH,
1432 MOJOSHADER_AST_STATEMENT_FOR,
1433 MOJOSHADER_AST_STATEMENT_DO,
1434 MOJOSHADER_AST_STATEMENT_WHILE,
1435 MOJOSHADER_AST_STATEMENT_RETURN,
1436 MOJOSHADER_AST_STATEMENT_TYPEDEF,
1437 MOJOSHADER_AST_STATEMENT_STRUCT,
1438 MOJOSHADER_AST_STATEMENT_VARDECL,
1439 MOJOSHADER_AST_STATEMENT_END_RANGE,
1440
1441 MOJOSHADER_AST_MISC_START_RANGE, /* misc. syntactic glue. */
1442 MOJOSHADER_AST_FUNCTION_PARAMS,
1443 MOJOSHADER_AST_FUNCTION_SIGNATURE,
1444 MOJOSHADER_AST_SCALAR_OR_ARRAY,
1445 MOJOSHADER_AST_TYPEDEF,
1446 MOJOSHADER_AST_PACK_OFFSET,
1447 MOJOSHADER_AST_VARIABLE_LOWLEVEL,
1448 MOJOSHADER_AST_ANNOTATION,
1449 MOJOSHADER_AST_VARIABLE_DECLARATION,
1450 MOJOSHADER_AST_STRUCT_DECLARATION,
1451 MOJOSHADER_AST_STRUCT_MEMBER,
1452 MOJOSHADER_AST_SWITCH_CASE,
1453 MOJOSHADER_AST_ARGUMENTS,
1454 MOJOSHADER_AST_MISC_END_RANGE,
1455
1456 MOJOSHADER_AST_END_RANGE
1457 } MOJOSHADER_astNodeType;
1458
1459 typedef struct MOJOSHADER_astNodeInfo
1460 {
1461 MOJOSHADER_astNodeType type;
1462 const char *filename;
1463 unsigned int line;
1464 } MOJOSHADER_astNodeInfo;
1465
1466 typedef enum MOJOSHADER_astVariableAttributes
1467 {
1468 MOJOSHADER_AST_VARATTR_EXTERN = (1 << 0),
1469 MOJOSHADER_AST_VARATTR_NOINTERPOLATION = (1 << 1),
1470 MOJOSHADER_AST_VARATTR_SHARED = (1 << 2),
1471 MOJOSHADER_AST_VARATTR_STATIC = (1 << 3),
1472 MOJOSHADER_AST_VARATTR_UNIFORM = (1 << 4),
1473 MOJOSHADER_AST_VARATTR_VOLATILE = (1 << 5),
1474 MOJOSHADER_AST_VARATTR_CONST = (1 << 6),
1475 MOJOSHADER_AST_VARATTR_ROWMAJOR = (1 << 7),
1476 MOJOSHADER_AST_VARATTR_COLUMNMAJOR = (1 << 8)
1477 } MOJOSHADER_astVariableAttributes;
1478
1479 typedef enum MOJOSHADER_astIfAttributes
1480 {
1481 MOJOSHADER_AST_IFATTR_NONE,
1482 MOJOSHADER_AST_IFATTR_BRANCH,
1483 MOJOSHADER_AST_IFATTR_FLATTEN,
1484 MOJOSHADER_AST_IFATTR_IFALL,
1485 MOJOSHADER_AST_IFATTR_IFANY,
1486 MOJOSHADER_AST_IFATTR_PREDICATE,
1487 MOJOSHADER_AST_IFATTR_PREDICATEBLOCK
1488 } MOJOSHADER_astIfAttributes;
1489
1490 typedef enum MOJOSHADER_astSwitchAttributes
1491 {
1492 MOJOSHADER_AST_SWITCHATTR_NONE,
1493 MOJOSHADER_AST_SWITCHATTR_FLATTEN,
1494 MOJOSHADER_AST_SWITCHATTR_BRANCH,
1495 MOJOSHADER_AST_SWITCHATTR_FORCECASE,
1496 MOJOSHADER_AST_SWITCHATTR_CALL
1497 } MOJOSHADER_astSwitchAttributes;
1498
1499 /* You can cast any AST node pointer to this. */
1500 typedef struct MOJOSHADER_astGeneric
1501 {
1502 MOJOSHADER_astNodeInfo ast;
1503 } MOJOSHADER_astGeneric;
1504
1505 typedef struct MOJOSHADER_astExpression
1506 {
1507 MOJOSHADER_astNodeInfo ast;
1508 const MOJOSHADER_astDataType *datatype;
1509 } MOJOSHADER_astExpression;
1510
1511 typedef struct MOJOSHADER_astArguments
1512 {
1513 MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_ARGUMENTS */
1514 MOJOSHADER_astExpression *argument;
1515 struct MOJOSHADER_astArguments *next;
1516 } MOJOSHADER_astArguments;
1517
1518 typedef struct MOJOSHADER_astExpressionUnary
1519 {
1520 MOJOSHADER_astNodeInfo ast;
1521 const MOJOSHADER_astDataType *datatype;
1522 MOJOSHADER_astExpression *operand;
1523 } MOJOSHADER_astExpressionUnary;
1524
1525 typedef struct MOJOSHADER_astExpressionBinary
1526 {
1527 MOJOSHADER_astNodeInfo ast;
1528 const MOJOSHADER_astDataType *datatype;
1529 MOJOSHADER_astExpression *left;
1530 MOJOSHADER_astExpression *right;
1531 } MOJOSHADER_astExpressionBinary;
1532
1533 typedef struct MOJOSHADER_astExpressionTernary
1534 {
1535 MOJOSHADER_astNodeInfo ast;
1536 const MOJOSHADER_astDataType *datatype;
1537 MOJOSHADER_astExpression *left;
1538 MOJOSHADER_astExpression *center;
1539 MOJOSHADER_astExpression *right;
1540 } MOJOSHADER_astExpressionTernary;
1541
1542 /* Identifier indexes aren't available until semantic analysis phase completes.
1543 * It provides a unique id for this identifier's variable.
1544 * It will be negative for global scope, positive for function scope
1545 * (global values are globally unique, function values are only
1546 * unique within the scope of the given function). There's a different
1547 * set of indices if this identifier is a function (positive for
1548 * user-defined functions, negative for intrinsics).
1549 * May be zero for various reasons (unknown identifier, etc).
1550 */
1551 typedef struct MOJOSHADER_astExpressionIdentifier
1552 {
1553 MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_IDENTIFIER */
1554 const MOJOSHADER_astDataType *datatype;
1555 const char *identifier;
1556 int index;
1557 } MOJOSHADER_astExpressionIdentifier;
1558
1559 typedef struct MOJOSHADER_astExpressionIntLiteral
1560 {
1561 MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_INT_LITERAL */
1562 const MOJOSHADER_astDataType *datatype; /* always AST_DATATYPE_INT */
1563 int value;
1564 } MOJOSHADER_astExpressionIntLiteral;
1565
1566 typedef struct MOJOSHADER_astExpressionFloatLiteral
1567 {
1568 MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_FLOAT_LITERAL */
1569 const MOJOSHADER_astDataType *datatype; /* always AST_DATATYPE_FLOAT */
1570 double value;
1571 } MOJOSHADER_astExpressionFloatLiteral;
1572
1573 typedef struct MOJOSHADER_astExpressionStringLiteral
1574 {
1575 MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_STRING_LITERAL */
1576 const MOJOSHADER_astDataType *datatype; /* always AST_DATATYPE_STRING */
1577 const char *string;
1578 } MOJOSHADER_astExpressionStringLiteral;
1579
1580 typedef struct MOJOSHADER_astExpressionBooleanLiteral
1581 {
1582 MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_BOOLEAN_LITERAL */
1583 const MOJOSHADER_astDataType *datatype; /* always AST_DATATYPE_BOOL */
1584 int value; /* Always 1 or 0. */
1585 } MOJOSHADER_astExpressionBooleanLiteral;
1586
1587 typedef struct MOJOSHADER_astExpressionConstructor
1588 {
1589 MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_CONSTRUCTOR */
1590 const MOJOSHADER_astDataType *datatype;
1591 MOJOSHADER_astArguments *args;
1592 } MOJOSHADER_astExpressionConstructor;
1593
1594 typedef struct MOJOSHADER_astExpressionDerefStruct
1595 {
1596 MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_DEREF_STRUCT */
1597 const MOJOSHADER_astDataType *datatype;
1598 /* !!! FIXME:
1599 * "identifier" is misnamed; this might not be an identifier at all:
1600 * x = FunctionThatReturnsAStruct().SomeMember;
1601 */
1602 MOJOSHADER_astExpression *identifier;
1603 const char *member;
1604 int isswizzle; /* Always 1 or 0. Never set by parseAst()! */
1605 int member_index; /* Never set by parseAst()! */
1606 } MOJOSHADER_astExpressionDerefStruct;
1607
1608 typedef struct MOJOSHADER_astExpressionCallFunction
1609 {
1610 MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_CALLFUNC */
1611 const MOJOSHADER_astDataType *datatype;
1612 MOJOSHADER_astExpressionIdentifier *identifier;
1613 MOJOSHADER_astArguments *args;
1614 } MOJOSHADER_astExpressionCallFunction;
1615
1616 typedef struct MOJOSHADER_astExpressionCast
1617 {
1618 MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_CAST */
1619 const MOJOSHADER_astDataType *datatype;
1620 MOJOSHADER_astExpression *operand;
1621 } MOJOSHADER_astExpressionCast;
1622
1623 typedef struct MOJOSHADER_astCompilationUnit
1624 {
1625 MOJOSHADER_astNodeInfo ast;
1626 struct MOJOSHADER_astCompilationUnit *next;
1627 } MOJOSHADER_astCompilationUnit;
1628
1629 typedef enum MOJOSHADER_astFunctionStorageClass
1630 {
1631 MOJOSHADER_AST_FNSTORECLS_NONE,
1632 MOJOSHADER_AST_FNSTORECLS_INLINE
1633 } MOJOSHADER_astFunctionStorageClass;
1634
1635 typedef enum MOJOSHADER_astInputModifier
1636 {
1637 MOJOSHADER_AST_INPUTMOD_NONE,
1638 MOJOSHADER_AST_INPUTMOD_IN,
1639 MOJOSHADER_AST_INPUTMOD_OUT,
1640 MOJOSHADER_AST_INPUTMOD_INOUT,
1641 MOJOSHADER_AST_INPUTMOD_UNIFORM
1642 } MOJOSHADER_astInputModifier;
1643
1644 typedef enum MOJOSHADER_astInterpolationModifier
1645 {
1646 MOJOSHADER_AST_INTERPMOD_NONE,
1647 MOJOSHADER_AST_INTERPMOD_LINEAR,
1648 MOJOSHADER_AST_INTERPMOD_CENTROID,
1649 MOJOSHADER_AST_INTERPMOD_NOINTERPOLATION,
1650 MOJOSHADER_AST_INTERPMOD_NOPERSPECTIVE,
1651 MOJOSHADER_AST_INTERPMOD_SAMPLE
1652 } MOJOSHADER_astInterpolationModifier;
1653
1654 typedef struct MOJOSHADER_astFunctionParameters
1655 {
1656 MOJOSHADER_astNodeInfo ast;
1657 const MOJOSHADER_astDataType *datatype;
1658 MOJOSHADER_astInputModifier input_modifier;
1659 const char *identifier;
1660 const char *semantic;
1661 MOJOSHADER_astInterpolationModifier interpolation_modifier;
1662 MOJOSHADER_astExpression *initializer;
1663 struct MOJOSHADER_astFunctionParameters *next;
1664 } MOJOSHADER_astFunctionParameters;
1665
1666 typedef struct MOJOSHADER_astFunctionSignature
1667 {
1668 MOJOSHADER_astNodeInfo ast;
1669 const MOJOSHADER_astDataType *datatype;
1670 const char *identifier;
1671 MOJOSHADER_astFunctionParameters *params;
1672 MOJOSHADER_astFunctionStorageClass storage_class;
1673 const char *semantic;
1674 } MOJOSHADER_astFunctionSignature;
1675
1676 typedef struct MOJOSHADER_astScalarOrArray
1677 {
1678 MOJOSHADER_astNodeInfo ast;
1679 const char *identifier;
1680 int isarray; /* boolean: 1 or 0 */
1681 MOJOSHADER_astExpression *dimension;
1682 } MOJOSHADER_astScalarOrArray;
1683
1684 typedef struct MOJOSHADER_astAnnotations
1685 {
1686 MOJOSHADER_astNodeInfo ast;
1687 const MOJOSHADER_astDataType *datatype;
1688 MOJOSHADER_astExpression *initializer;
1689 struct MOJOSHADER_astAnnotations *next;
1690 } MOJOSHADER_astAnnotations;
1691
1692 typedef struct MOJOSHADER_astPackOffset
1693 {
1694 MOJOSHADER_astNodeInfo ast;
1695 const char *ident1; /* !!! FIXME: rename this. */
1696 const char *ident2;
1697 } MOJOSHADER_astPackOffset;
1698
1699 typedef struct MOJOSHADER_astVariableLowLevel
1700 {
1701 MOJOSHADER_astNodeInfo ast;
1702 MOJOSHADER_astPackOffset *packoffset;
1703 const char *register_name;
1704 } MOJOSHADER_astVariableLowLevel;
1705
1706 typedef struct MOJOSHADER_astStructMembers
1707 {
1708 MOJOSHADER_astNodeInfo ast;
1709 const MOJOSHADER_astDataType *datatype;
1710 const char *semantic;
1711 MOJOSHADER_astScalarOrArray *details;
1712 MOJOSHADER_astInterpolationModifier interpolation_mod;
1713 struct MOJOSHADER_astStructMembers *next;
1714 } MOJOSHADER_astStructMembers;
1715
1716 typedef struct MOJOSHADER_astStructDeclaration
1717 {
1718 MOJOSHADER_astNodeInfo ast;
1719 const MOJOSHADER_astDataType *datatype;
1720 const char *name;
1721 MOJOSHADER_astStructMembers *members;
1722 } MOJOSHADER_astStructDeclaration;
1723
1724 typedef struct MOJOSHADER_astVariableDeclaration
1725 {
1726 MOJOSHADER_astNodeInfo ast;
1727 int attributes;
1728 const MOJOSHADER_astDataType *datatype;
1729 MOJOSHADER_astStructDeclaration *anonymous_datatype;
1730 MOJOSHADER_astScalarOrArray *details;
1731 const char *semantic;
1732 MOJOSHADER_astAnnotations *annotations;
1733 MOJOSHADER_astExpression *initializer;
1734 MOJOSHADER_astVariableLowLevel *lowlevel;
1735 struct MOJOSHADER_astVariableDeclaration *next;
1736 } MOJOSHADER_astVariableDeclaration;
1737
1738 typedef struct MOJOSHADER_astStatement
1739 {
1740 MOJOSHADER_astNodeInfo ast;
1741 struct MOJOSHADER_astStatement *next;
1742 } MOJOSHADER_astStatement;
1743
1744 typedef MOJOSHADER_astStatement MOJOSHADER_astEmptyStatement;
1745 typedef MOJOSHADER_astStatement MOJOSHADER_astBreakStatement;
1746 typedef MOJOSHADER_astStatement MOJOSHADER_astContinueStatement;
1747 typedef MOJOSHADER_astStatement MOJOSHADER_astDiscardStatement;
1748
1749 /* something enclosed in "{}" braces. */
1750 typedef struct MOJOSHADER_astBlockStatement
1751 {
1752 MOJOSHADER_astNodeInfo ast;
1753 MOJOSHADER_astStatement *next;
1754 MOJOSHADER_astStatement *statements; /* list of child statements. */
1755 } MOJOSHADER_astBlockStatement;
1756
1757 typedef struct MOJOSHADER_astReturnStatement
1758 {
1759 MOJOSHADER_astNodeInfo ast;
1760 MOJOSHADER_astStatement *next;
1761 MOJOSHADER_astExpression *expr;
1762 } MOJOSHADER_astReturnStatement;
1763
1764 typedef struct MOJOSHADER_astExpressionStatement
1765 {
1766 MOJOSHADER_astNodeInfo ast;
1767 MOJOSHADER_astStatement *next;
1768 MOJOSHADER_astExpression *expr;
1769 } MOJOSHADER_astExpressionStatement;
1770
1771 typedef struct MOJOSHADER_astIfStatement
1772 {
1773 MOJOSHADER_astNodeInfo ast;
1774 MOJOSHADER_astStatement *next;
1775 int attributes;
1776 MOJOSHADER_astExpression *expr;
1777 MOJOSHADER_astStatement *statement;
1778 MOJOSHADER_astStatement *else_statement;
1779 } MOJOSHADER_astIfStatement;
1780
1781 typedef struct MOJOSHADER_astSwitchCases
1782 {
1783 MOJOSHADER_astNodeInfo ast;
1784 MOJOSHADER_astExpression *expr;
1785 MOJOSHADER_astStatement *statement;
1786 struct MOJOSHADER_astSwitchCases *next;
1787 } MOJOSHADER_astSwitchCases;
1788
1789 typedef struct MOJOSHADER_astSwitchStatement
1790 {
1791 MOJOSHADER_astNodeInfo ast;
1792 MOJOSHADER_astStatement *next;
1793 int attributes;
1794 MOJOSHADER_astExpression *expr;
1795 MOJOSHADER_astSwitchCases *cases;
1796 } MOJOSHADER_astSwitchStatement;
1797
1798 typedef struct MOJOSHADER_astWhileStatement
1799 {
1800 MOJOSHADER_astNodeInfo ast;
1801 MOJOSHADER_astStatement *next;
1802 int unroll; /* # times to unroll, 0 to loop, < 0 == compiler's choice. */
1803 MOJOSHADER_astExpression *expr;
1804 MOJOSHADER_astStatement *statement;
1805 } MOJOSHADER_astWhileStatement;
1806
1807 typedef MOJOSHADER_astWhileStatement MOJOSHADER_astDoStatement;
1808
1809 typedef struct MOJOSHADER_astForStatement
1810 {
1811 MOJOSHADER_astNodeInfo ast;
1812 MOJOSHADER_astStatement *next;
1813 int unroll; /* # times to unroll, 0 to loop, < 0 == compiler's choice. */
1814 MOJOSHADER_astVariableDeclaration *var_decl; /* either this ... */
1815 MOJOSHADER_astExpression *initializer; /* ... or this will used. */
1816 MOJOSHADER_astExpression *looptest;
1817 MOJOSHADER_astExpression *counter;
1818 MOJOSHADER_astStatement *statement;
1819 } MOJOSHADER_astForStatement;
1820
1821 typedef struct MOJOSHADER_astTypedef
1822 {
1823 MOJOSHADER_astNodeInfo ast;
1824 const MOJOSHADER_astDataType *datatype;
1825 int isconst; /* boolean: 1 or 0 */
1826 MOJOSHADER_astScalarOrArray *details;
1827 } MOJOSHADER_astTypedef;
1828
1829 typedef struct MOJOSHADER_astTypedefStatement
1830 {
1831 MOJOSHADER_astNodeInfo ast;
1832 MOJOSHADER_astStatement *next;
1833 MOJOSHADER_astTypedef *type_info;
1834 } MOJOSHADER_astTypedefStatement;
1835
1836 typedef struct MOJOSHADER_astVarDeclStatement
1837 {
1838 MOJOSHADER_astNodeInfo ast;
1839 MOJOSHADER_astStatement *next;
1840 MOJOSHADER_astVariableDeclaration *declaration;
1841 } MOJOSHADER_astVarDeclStatement;
1842
1843 typedef struct MOJOSHADER_astStructStatement
1844 {
1845 MOJOSHADER_astNodeInfo ast;
1846 MOJOSHADER_astStatement *next;
1847 MOJOSHADER_astStructDeclaration *struct_info;
1848 } MOJOSHADER_astStructStatement;
1849
1850 typedef struct MOJOSHADER_astCompilationUnitFunction
1851 {
1852 MOJOSHADER_astNodeInfo ast;
1853 MOJOSHADER_astCompilationUnit *next;
1854 MOJOSHADER_astFunctionSignature *declaration;
1855 MOJOSHADER_astStatement *definition;
1856 int index; /* unique id. Will be 0 until semantic analysis runs. */
1857 } MOJOSHADER_astCompilationUnitFunction;
1858
1859 typedef struct MOJOSHADER_astCompilationUnitTypedef
1860 {
1861 MOJOSHADER_astNodeInfo ast;
1862 MOJOSHADER_astCompilationUnit *next;
1863 MOJOSHADER_astTypedef *type_info;
1864 } MOJOSHADER_astCompilationUnitTypedef;
1865
1866 typedef struct MOJOSHADER_astCompilationUnitStruct
1867 {
1868 MOJOSHADER_astNodeInfo ast;
1869 MOJOSHADER_astCompilationUnit *next;
1870 MOJOSHADER_astStructDeclaration *struct_info;
1871 } MOJOSHADER_astCompilationUnitStruct;
1872
1873 typedef struct MOJOSHADER_astCompilationUnitVariable
1874 {
1875 MOJOSHADER_astNodeInfo ast;
1876 MOJOSHADER_astCompilationUnit *next;
1877 MOJOSHADER_astVariableDeclaration *declaration;
1878 } MOJOSHADER_astCompilationUnitVariable;
1879
1880
1881 /* this is way cleaner than all the nasty typecasting. */
1882 typedef union MOJOSHADER_astNode
1883 {
1884 MOJOSHADER_astNodeInfo ast;
1885 MOJOSHADER_astGeneric generic;
1886 MOJOSHADER_astExpression expression;
1887 MOJOSHADER_astArguments arguments;
1888 MOJOSHADER_astExpressionUnary unary;
1889 MOJOSHADER_astExpressionBinary binary;
1890 MOJOSHADER_astExpressionTernary ternary;
1891 MOJOSHADER_astExpressionIdentifier identifier;
1892 MOJOSHADER_astExpressionIntLiteral intliteral;
1893 MOJOSHADER_astExpressionFloatLiteral floatliteral;
1894 MOJOSHADER_astExpressionStringLiteral stringliteral;
1895 MOJOSHADER_astExpressionBooleanLiteral boolliteral;
1896 MOJOSHADER_astExpressionConstructor constructor;
1897 MOJOSHADER_astExpressionDerefStruct derefstruct;
1898 MOJOSHADER_astExpressionCallFunction callfunc;
1899 MOJOSHADER_astExpressionCast cast;
1900 MOJOSHADER_astCompilationUnit compunit;
1901 MOJOSHADER_astFunctionParameters params;
1902 MOJOSHADER_astFunctionSignature funcsig;
1903 MOJOSHADER_astScalarOrArray soa;
1904 MOJOSHADER_astAnnotations annotations;
1905 MOJOSHADER_astPackOffset packoffset;
1906 MOJOSHADER_astVariableLowLevel varlowlevel;
1907 MOJOSHADER_astStructMembers structmembers;
1908 MOJOSHADER_astStructDeclaration structdecl;
1909 MOJOSHADER_astVariableDeclaration vardecl;
1910 MOJOSHADER_astStatement stmt;
1911 MOJOSHADER_astEmptyStatement emptystmt;
1912 MOJOSHADER_astBreakStatement breakstmt;
1913 MOJOSHADER_astContinueStatement contstmt;
1914 MOJOSHADER_astDiscardStatement discardstmt;
1915 MOJOSHADER_astBlockStatement blockstmt;
1916 MOJOSHADER_astReturnStatement returnstmt;
1917 MOJOSHADER_astExpressionStatement exprstmt;
1918 MOJOSHADER_astIfStatement ifstmt;
1919 MOJOSHADER_astSwitchCases cases;
1920 MOJOSHADER_astSwitchStatement switchstmt;
1921 MOJOSHADER_astWhileStatement whilestmt;
1922 MOJOSHADER_astDoStatement dostmt;
1923 MOJOSHADER_astForStatement forstmt;
1924 MOJOSHADER_astTypedef typdef;
1925 MOJOSHADER_astTypedefStatement typedefstmt;
1926 MOJOSHADER_astVarDeclStatement vardeclstmt;
1927 MOJOSHADER_astStructStatement structstmt;
1928 MOJOSHADER_astCompilationUnitFunction funcunit;
1929 MOJOSHADER_astCompilationUnitTypedef typedefunit;
1930 MOJOSHADER_astCompilationUnitStruct structunit;
1931 MOJOSHADER_astCompilationUnitVariable varunit;
1932 } MOJOSHADER_astNode;
1933
1934
1935 /*
1936 * Structure used to return data from parsing of a shader into an AST...
1937 */
1938 /* !!! FIXME: most of these ints should be unsigned. */
1939 typedef struct MOJOSHADER_astData
1940 {
1941 /*
1942 * The number of elements pointed to by (errors).
1943 */
1944 int error_count;
1945
1946 /*
1947 * (error_count) elements of data that specify errors that were generated
1948 * by parsing this shader.
1949 * This can be NULL if there were no errors or if (error_count) is zero.
1950 * Note that this will only produce errors for syntax problems. Most of
1951 * the things we expect a compiler to produce errors for--incompatible
1952 * types, unknown identifiers, etc--are not checked at all during
1953 * initial generation of the syntax tree...bogus programs that would
1954 * fail to compile will pass here without error, if they are syntactically
1955 * correct!
1956 */
1957 MOJOSHADER_error *errors;
1958
1959 /*
1960 * The name of the source profile used to parse the shader. Will be NULL
1961 * on error.
1962 */
1963 const char *source_profile;
1964
1965 /*
1966 * The actual syntax tree. You are responsible for walking it yourself.
1967 * CompilationUnits are always the top of the tree (functions, typedefs,
1968 * global variables, etc). Will be NULL on error.
1969 */
1970 const MOJOSHADER_astNode *ast;
1971
1972 /*
1973 * This is the malloc implementation you passed to MOJOSHADER_parse().
1974 */
1975 MOJOSHADER_malloc malloc;
1976
1977 /*
1978 * This is the free implementation you passed to MOJOSHADER_parse().
1979 */
1980 MOJOSHADER_free free;
1981
1982 /*
1983 * This is the pointer you passed as opaque data for your allocator.
1984 */
1985 void *malloc_data;
1986
1987 /*
1988 * This is internal data, and not for the application to touch.
1989 */
1990 void *opaque;
1991 } MOJOSHADER_astData;
1992
1993
1994 /*
1995 * You almost certainly don't need this function, unless you absolutely know
1996 * why you need it without hesitation. This is almost certainly only good for
1997 * building code analysis tools on top of.
1998 *
1999 * This is intended to parse HLSL source code, turning it into an abstract
2000 * syntax tree.
2001 *
2002 * (srcprofile) specifies the source language of the shader. You can specify
2003 * a shader model with this, too. See MOJOSHADER_SRC_PROFILE_* constants.
2004 *
2005 * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
2006 * actually access this file, as we obtain our data from (source). This
2007 * string is copied when we need to report errors while processing (source),
2008 * as opposed to errors in a file referenced via the #include directive in
2009 * (source). If this is NULL, then errors will report the filename as NULL,
2010 * too.
2011 *
2012 * (source) is an UTF-8 string of valid high-level shader source code.
2013 * It does not need to be NULL-terminated.
2014 *
2015 * (sourcelen) is the length of the string pointed to by (source), in bytes.
2016 *
2017 * (defines) points to (define_count) preprocessor definitions, and can be
2018 * NULL. These are treated by the preprocessor as if the source code started
2019 * with one #define for each entry you pass in here.
2020 *
2021 * (include_open) and (include_close) let the app control the preprocessor's
2022 * behaviour for #include statements. Both are optional and can be NULL, but
2023 * both must be specified if either is specified.
2024 *
2025 * This will return a MOJOSHADER_astData. The data supplied here gives the
2026 * application a tree-like structure they can walk to see the layout of
2027 * a given program. When you are done with this data, pass it to
2028 * MOJOSHADER_freeCompileData() to deallocate resources.
2029 *
2030 * This function will never return NULL, even if the system is completely
2031 * out of memory upon entry (in which case, this function returns a static
2032 * MOJOSHADER_astData object, which is still safe to pass to
2033 * MOJOSHADER_freeAstData()).
2034 *
2035 * As parsing requires some memory to be allocated, you may provide a
2036 * custom allocator to this function, which will be used to allocate/free
2037 * memory. They function just like malloc() and free(). We do not use
2038 * realloc(). If you don't care, pass NULL in for the allocator functions.
2039 * If your allocator needs instance-specific data, you may supply it with the
2040 * (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
2041 *
2042 * This function is thread safe, so long as the various callback functions
2043 * are, too, and that the parameters remains intact for the duration of the
2044 * call. This allows you to parse several shaders on separate CPU cores
2045 * at the same time.
2046 */
2047 DECLSPEC const MOJOSHADER_astData *MOJOSHADER_parseAst(const char *srcprofile,
2048 const char *filename, const char *source,
2049 unsigned int sourcelen,
2050 const MOJOSHADER_preprocessorDefine *defs,
2051 unsigned int define_count,
2052 MOJOSHADER_includeOpen include_open,
2053 MOJOSHADER_includeClose include_close,
2054 MOJOSHADER_malloc m, MOJOSHADER_free f,
2055 void *d);
2056
2057
2058 /* !!! FIXME: expose semantic analysis to the public API? */
2059
2060
2061 /*
2062 * Call this to dispose of AST parsing results when you are done with them.
2063 * This will call the MOJOSHADER_free function you provided to
2064 * MOJOSHADER_parseAst() multiple times, if you provided one.
2065 * Passing a NULL here is a safe no-op.
2066 *
2067 * This function is thread safe, so long as any allocator you passed into
2068 * MOJOSHADER_parseAst() is, too.
2069 */
2070 DECLSPEC void MOJOSHADER_freeAstData(const MOJOSHADER_astData *data);
2071
2072
2073 /* Intermediate Representation interface... */
2074 /* !!! FIXME: there is currently no way to access the IR via the public API. */
2075 typedef enum MOJOSHADER_irNodeType
2076 {
2077 MOJOSHADER_IR_START_RANGE_EXPR,
2078 MOJOSHADER_IR_CONSTANT,
2079 MOJOSHADER_IR_TEMP,
2080 MOJOSHADER_IR_BINOP,
2081 MOJOSHADER_IR_MEMORY,
2082 MOJOSHADER_IR_CALL,
2083 MOJOSHADER_IR_ESEQ,
2084 MOJOSHADER_IR_ARRAY,
2085 MOJOSHADER_IR_CONVERT,
2086 MOJOSHADER_IR_SWIZZLE,
2087 MOJOSHADER_IR_CONSTRUCT,
2088 MOJOSHADER_IR_END_RANGE_EXPR,
2089
2090 MOJOSHADER_IR_START_RANGE_STMT,
2091 MOJOSHADER_IR_MOVE,
2092 MOJOSHADER_IR_EXPR_STMT,
2093 MOJOSHADER_IR_JUMP,
2094 MOJOSHADER_IR_CJUMP,
2095 MOJOSHADER_IR_SEQ,
2096 MOJOSHADER_IR_LABEL,
2097 MOJOSHADER_IR_DISCARD,
2098 MOJOSHADER_IR_END_RANGE_STMT,
2099
2100 MOJOSHADER_IR_START_RANGE_MISC,
2101 MOJOSHADER_IR_EXPRLIST,
2102 MOJOSHADER_IR_END_RANGE_MISC,
2103
2104 MOJOSHADER_IR_END_RANGE
2105 } MOJOSHADER_irNodeType;
2106
2107 typedef struct MOJOSHADER_irNodeInfo
2108 {
2109 MOJOSHADER_irNodeType type;
2110 const char *filename;
2111 unsigned int line;
2112 } MOJOSHADER_irNodeInfo;
2113
2114 typedef struct MOJOSHADER_irExprList MOJOSHADER_irExprList;
2115
2116 /*
2117 * IR nodes are categorized into Expressions, Statements, and Everything Else.
2118 * You can cast any of them to MOJOSHADER_irGeneric, but this split is
2119 * useful for slightly better type-checking (you can't cleanly assign
2120 * something that doesn't return a value to something that wants one, etc).
2121 * These broader categories are just unions of the simpler types, so the
2122 * real definitions are below all the things they contain (but these
2123 * predeclarations are because the simpler types refer to the broader
2124 * categories).
2125 */
2126 typedef union MOJOSHADER_irExpression MOJOSHADER_irExpression; /* returns a value. */
2127 typedef union MOJOSHADER_irStatement MOJOSHADER_irStatement; /* no returned value. */
2128 typedef union MOJOSHADER_irMisc MOJOSHADER_irMisc; /* Everything Else. */
2129 typedef union MOJOSHADER_irNode MOJOSHADER_irNode; /* Generic uber-wrapper. */
2130
2131 /* You can cast any IR node pointer to this. */
2132 typedef struct MOJOSHADER_irGeneric
2133 {
2134 MOJOSHADER_irNodeInfo ir;
2135 } MOJOSHADER_irGeneric;
2136
2137
2138 /* These are used for MOJOSHADER_irBinOp */
2139 typedef enum MOJOSHADER_irBinOpType
2140 {
2141 MOJOSHADER_IR_BINOP_ADD,
2142 MOJOSHADER_IR_BINOP_SUBTRACT,
2143 MOJOSHADER_IR_BINOP_MULTIPLY,
2144 MOJOSHADER_IR_BINOP_DIVIDE,
2145 MOJOSHADER_IR_BINOP_MODULO,
2146 MOJOSHADER_IR_BINOP_AND,
2147 MOJOSHADER_IR_BINOP_OR,
2148 MOJOSHADER_IR_BINOP_XOR,
2149 MOJOSHADER_IR_BINOP_LSHIFT,
2150 MOJOSHADER_IR_BINOP_RSHIFT,
2151 MOJOSHADER_IR_BINOP_UNKNOWN
2152 } MOJOSHADER_irBinOpType;
2153
2154 typedef enum MOJOSHADER_irConditionType
2155 {
2156 MOJOSHADER_IR_COND_EQL,
2157 MOJOSHADER_IR_COND_NEQ,
2158 MOJOSHADER_IR_COND_LT,
2159 MOJOSHADER_IR_COND_GT,
2160 MOJOSHADER_IR_COND_LEQ,
2161 MOJOSHADER_IR_COND_GEQ,
2162 MOJOSHADER_IR_COND_UNKNOWN
2163 } MOJOSHADER_irConditionType;
2164
2165
2166 /* MOJOSHADER_irExpression types... */
2167
2168 typedef struct MOJOSHADER_irExprInfo
2169 {
2170 MOJOSHADER_irNodeInfo ir;
2171 MOJOSHADER_astDataTypeType type;
2172 int elements;
2173 } MOJOSHADER_irExprInfo;
2174
2175 typedef struct MOJOSHADER_irConstant /* Constant value */
2176 {
2177 MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_CONSTANT */
2178 union
2179 {
2180 int ival[16];
2181 float fval[16];
2182 } value;
2183 } MOJOSHADER_irConstant;
2184
2185 typedef struct MOJOSHADER_irTemp /* temp value (not necessarily a register). */
2186 {
2187 MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_TEMP */
2188 int index;
2189 } MOJOSHADER_irTemp;
2190
2191 typedef struct MOJOSHADER_irBinOp /* binary operator (+, -, etc) */
2192 {
2193 MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_BINOP */
2194 MOJOSHADER_irBinOpType op;
2195 MOJOSHADER_irExpression *left;
2196 MOJOSHADER_irExpression *right;
2197 } MOJOSHADER_irBinOp;
2198
2199 typedef struct MOJOSHADER_irMemory
2200 {
2201 MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_MEMORY */
2202 int index; /* not final addresses, just a unique identifier. */
2203 } MOJOSHADER_irMemory;
2204
2205 typedef struct MOJOSHADER_irCall
2206 {
2207 MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_CALL */
2208 int index;
2209 MOJOSHADER_irExprList *args;
2210 } MOJOSHADER_irCall;
2211
2212 typedef struct MOJOSHADER_irESeq /* statement with result */
2213 {
2214 MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_ESEQ */
2215 MOJOSHADER_irStatement *stmt; /* execute this for side-effects, then... */
2216 MOJOSHADER_irExpression *expr; /* ...use this for the result. */
2217 } MOJOSHADER_irESeq;
2218
2219 typedef struct MOJOSHADER_irArray /* Array dereference. */
2220 {
2221 MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_ARRAY */
2222 MOJOSHADER_irExpression *array;
2223 MOJOSHADER_irExpression *element;
2224 } MOJOSHADER_irArray;
2225
2226 typedef struct MOJOSHADER_irConvert /* casting between datatypes */
2227 {
2228 MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_CONVERT */
2229 MOJOSHADER_irExpression *expr;
2230 } MOJOSHADER_irConvert;
2231
2232 typedef struct MOJOSHADER_irSwizzle /* vector swizzle */
2233 {
2234 MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_SWIZZLE */
2235 MOJOSHADER_irExpression *expr;
2236 char channels[4];
2237 } MOJOSHADER_irSwizzle;
2238
2239 typedef struct MOJOSHADER_irConstruct /* vector construct from discrete items */
2240 {
2241 MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_CONTSTRUCT */
2242 MOJOSHADER_irExprList *args;
2243 } MOJOSHADER_irConstruct;
2244
2245 /* Wrap the whole category in a union for type "safety." */
2246 union MOJOSHADER_irExpression
2247 {
2248 MOJOSHADER_irNodeInfo ir;
2249 MOJOSHADER_irExprInfo info;
2250 MOJOSHADER_irConstant constant;
2251 MOJOSHADER_irTemp temp;
2252 MOJOSHADER_irBinOp binop;
2253 MOJOSHADER_irMemory memory;
2254 MOJOSHADER_irCall call;
2255 MOJOSHADER_irESeq eseq;
2256 MOJOSHADER_irArray array;
2257 MOJOSHADER_irConvert convert;
2258 MOJOSHADER_irSwizzle swizzle;
2259 MOJOSHADER_irConstruct construct;
2260 };
2261
2262 /* MOJOSHADER_irStatement types. */
2263
2264 typedef struct MOJOSHADER_irMove /* load/store. */
2265 {
2266 MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_MOVE */
2267 MOJOSHADER_irExpression *dst; /* must result in a temp or mem! */
2268 MOJOSHADER_irExpression *src;
2269 int writemask; /* for write-masking vector channels. */
2270 } MOJOSHADER_irMove;
2271
2272 typedef struct MOJOSHADER_irExprStmt /* evaluate expression, throw it away. */
2273 {
2274 MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_EXPR_STMT */
2275 MOJOSHADER_irExpression *expr;
2276 } MOJOSHADER_irExprStmt;
2277
2278 typedef struct MOJOSHADER_irJump /* unconditional jump */
2279 {
2280 MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_JUMP */
2281 int label;
2282 /* !!! FIXME: possible label list, for further optimization passes. */
2283 } MOJOSHADER_irJump;
2284
2285 typedef struct MOJOSHADER_irCJump /* conditional jump */
2286 {
2287 MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_CJUMP */
2288 MOJOSHADER_irConditionType cond;
2289 MOJOSHADER_irExpression *left; /* if (left cond right) */
2290 MOJOSHADER_irExpression *right;
2291 int iftrue; /* label id for true case. */
2292 int iffalse; /* label id for false case. */
2293 } MOJOSHADER_irCJump;
2294
2295 typedef struct MOJOSHADER_irSeq /* statement without side effects */
2296 {
2297 MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_SEQ */
2298 MOJOSHADER_irStatement *first;
2299 MOJOSHADER_irStatement *next;
2300 } MOJOSHADER_irSeq;
2301
2302 typedef struct MOJOSHADER_irLabel /* like a label in assembly language. */
2303 {
2304 MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_LABEL */
2305 int index;
2306 } MOJOSHADER_irLabel;
2307
2308 typedef MOJOSHADER_irGeneric MOJOSHADER_irDiscard; /* discard statement. */
2309
2310
2311 /* Wrap the whole category in a union for type "safety." */
2312 union MOJOSHADER_irStatement
2313 {
2314 MOJOSHADER_irNodeInfo ir;
2315 MOJOSHADER_irGeneric generic;
2316 MOJOSHADER_irMove move;
2317 MOJOSHADER_irExprStmt expr;
2318 MOJOSHADER_irJump jump;
2319 MOJOSHADER_irCJump cjump;
2320 MOJOSHADER_irSeq seq;
2321 MOJOSHADER_irLabel label;
2322 MOJOSHADER_irDiscard discard;
2323 };
2324
2325 /* MOJOSHADER_irMisc types. */
2326
2327 struct MOJOSHADER_irExprList
2328 {
2329 MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_EXPRLIST */
2330 MOJOSHADER_irExpression *expr;
2331 MOJOSHADER_irExprList *next;
2332 };
2333
2334 /* Wrap the whole category in a union for type "safety." */
2335 union MOJOSHADER_irMisc
2336 {
2337 MOJOSHADER_irNodeInfo ir;
2338 MOJOSHADER_irGeneric generic;
2339 MOJOSHADER_irExprList exprlist;
2340 };
2341
2342 /* This is a catchall for all your needs. :) */
2343 union MOJOSHADER_irNode
2344 {
2345 MOJOSHADER_irNodeInfo ir;
2346 MOJOSHADER_irGeneric generic;
2347 MOJOSHADER_irExpression expr;
2348 MOJOSHADER_irStatement stmt;
2349 MOJOSHADER_irMisc misc;
2350 };
2351
2352
2353 /* Compiler interface... */
2354
2355 /*
2356 * Structure used to return data from parsing of a shader...
2357 */
2358 /* !!! FIXME: most of these ints should be unsigned. */
2359 typedef struct MOJOSHADER_compileData
2360 {
2361 /*
2362 * The number of elements pointed to by (errors).
2363 */
2364 int error_count;
2365
2366 /*
2367 * (error_count) elements of data that specify errors that were generated
2368 * by compiling this shader.
2369 * This can be NULL if there were no errors or if (error_count) is zero.
2370 */
2371 MOJOSHADER_error *errors;
2372
2373 /*
2374 * The number of elements pointed to by (warnings).
2375 */
2376 int warning_count;
2377
2378 /*
2379 * (warning_count) elements of data that specify errors that were
2380 * generated by compiling this shader.
2381 * This can be NULL if there were no errors or if (warning_count) is zero.
2382 */
2383 MOJOSHADER_error *warnings;
2384
2385 /*
2386 * The name of the source profile used to compile the shader. Will be NULL
2387 * on error.
2388 */
2389 const char *source_profile;
2390
2391 /*
2392 * Bytes of output from compiling. This will be a null-terminated ASCII
2393 * string of D3D assembly source code.
2394 */
2395 const char *output;
2396
2397 /*
2398 * Byte count for output, not counting any null terminator.
2399 * Will be 0 on error.
2400 */
2401 int output_len;
2402
2403 /*
2404 * The number of elements pointed to by (symbols).
2405 */
2406 int symbol_count;
2407
2408 /*
2409 * (symbol_count) elements of data that specify high-level symbol data
2410 * for the shader. This can be used by MOJOSHADER_assemble() to
2411 * generate a CTAB section in bytecode, which is needed by
2412 * MOJOSHADER_parseData() to handle some shaders. This can be NULL on
2413 * error or if (symbol_count) is zero.
2414 */
2415 MOJOSHADER_symbol *symbols;
2416
2417 /*
2418 * This is the malloc implementation you passed to MOJOSHADER_parse().
2419 */
2420 MOJOSHADER_malloc malloc;
2421
2422 /*
2423 * This is the free implementation you passed to MOJOSHADER_parse().
2424 */
2425 MOJOSHADER_free free;
2426
2427 /*
2428 * This is the pointer you passed as opaque data for your allocator.
2429 */
2430 void *malloc_data;
2431 } MOJOSHADER_compileData;
2432
2433
2434 /*
2435 * This function is optional. Use this to compile high-level shader programs.
2436 *
2437 * This is intended to turn HLSL source code into D3D assembly code, which
2438 * can then be passed to MOJOSHADER_assemble() to convert it to D3D bytecode
2439 * (which can then be used with MOJOSHADER_parseData() to support other
2440 * shading targets).
2441 *
2442 * (srcprofile) specifies the source language of the shader. You can specify
2443 * a shader model with this, too. See MOJOSHADER_SRC_PROFILE_* constants.
2444 *
2445 * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
2446 * actually access this file, as we obtain our data from (source). This
2447 * string is copied when we need to report errors while processing (source),
2448 * as opposed to errors in a file referenced via the #include directive in
2449 * (source). If this is NULL, then errors will report the filename as NULL,
2450 * too.
2451 *
2452 * (source) is an UTF-8 string of valid high-level shader source code.
2453 * It does not need to be NULL-terminated.
2454 *
2455 * (sourcelen) is the length of the string pointed to by (source), in bytes.
2456 *
2457 * (defines) points to (define_count) preprocessor definitions, and can be
2458 * NULL. These are treated by the preprocessor as if the source code started
2459 * with one #define for each entry you pass in here.
2460 *
2461 * (include_open) and (include_close) let the app control the preprocessor's
2462 * behaviour for #include statements. Both are optional and can be NULL, but
2463 * both must be specified if either is specified.
2464 *
2465 * This will return a MOJOSHADER_compileData. The data supplied here is
2466 * sufficient to supply to MOJOSHADER_assemble() for further processing.
2467 * When you are done with this data, pass it to MOJOSHADER_freeCompileData()
2468 * to deallocate resources.
2469 *
2470 * This function will never return NULL, even if the system is completely
2471 * out of memory upon entry (in which case, this function returns a static
2472 * MOJOSHADER_compileData object, which is still safe to pass to
2473 * MOJOSHADER_freeCompileData()).
2474 *
2475 * As compiling requires some memory to be allocated, you may provide a
2476 * custom allocator to this function, which will be used to allocate/free
2477 * memory. They function just like malloc() and free(). We do not use
2478 * realloc(). If you don't care, pass NULL in for the allocator functions.
2479 * If your allocator needs instance-specific data, you may supply it with the
2480 * (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
2481 *
2482 * This function is thread safe, so long as the various callback functions
2483 * are, too, and that the parameters remains intact for the duration of the
2484 * call. This allows you to compile several shaders on separate CPU cores
2485 * at the same time.
2486 */
2487 DECLSPEC const MOJOSHADER_compileData *MOJOSHADER_compile(const char *srcprofile,
2488 const char *filename, const char *source,
2489 unsigned int sourcelen,
2490 const MOJOSHADER_preprocessorDefine *defs,
2491 unsigned int define_count,
2492 MOJOSHADER_includeOpen include_open,
2493 MOJOSHADER_includeClose include_close,
2494 MOJOSHADER_malloc m, MOJOSHADER_free f,
2495 void *d);
2496
2497
2498 /*
2499 * Call this to dispose of compile results when you are done with them.
2500 * This will call the MOJOSHADER_free function you provided to
2501 * MOJOSHADER_compile() multiple times, if you provided one.
2502 * Passing a NULL here is a safe no-op.
2503 *
2504 * This function is thread safe, so long as any allocator you passed into
2505 * MOJOSHADER_compile() is, too.
2506 */
2507 DECLSPEC void MOJOSHADER_freeCompileData(const MOJOSHADER_compileData *data);
2508
2509
2510 /* OpenGL interface... */
2511
2512 /*
2513 * Signature for function lookup callbacks. MojoShader will call a function
2514 * you provide to get OpenGL entry points (both standard functions and
2515 * extensions). Through this, MojoShader never links directly to OpenGL,
2516 * but relies on you to provide the implementation. This means you can
2517 * swap in different drivers, or hook functions (log every GL call MojoShader
2518 * makes, etc).
2519 *
2520 * (fnname) is the function name we want the address for ("glBegin" or
2521 * whatever. (data) is a void pointer you provide, if this callback needs
2522 * extra information. If you don't need it, you may specify NULL.
2523 *
2524 * Return the entry point on success, NULL if it couldn't be found.
2525 * Note that this could ask for standard entry points like glEnable(), or
2526 * extensions like glProgramLocalParameterI4ivNV(), so you might need
2527 * to check two places to find the desired entry point, depending on your
2528 * platform (Windows might need to look in OpenGL32.dll and use WGL, etc).
2529 */
2530 typedef void *(MOJOSHADERCALL *MOJOSHADER_glGetProcAddress)(const char *fnname, void *data);
2531
2532
2533 /*
2534 * "Contexts" map to OpenGL contexts...you need one per window, or whatever,
2535 * and need to inform MojoShader when you make a new one current.
2536 *
2537 * "Shaders" refer to individual vertex or pixel programs, and are created
2538 * by "compiling" Direct3D shader bytecode. A vertex and pixel shader are
2539 * "linked" into a "Program" before you can use them to render.
2540 *
2541 * To the calling application, these are all opaque handles.
2542 */
2543 typedef struct MOJOSHADER_glContext MOJOSHADER_glContext;
2544 typedef struct MOJOSHADER_glShader MOJOSHADER_glShader;
2545 typedef struct MOJOSHADER_glProgram MOJOSHADER_glProgram;
2546
2547
2548 /*
2549 * Get a list of available profiles. This will fill in the array (profs)
2550 * with up to (size) pointers of profiles that the current system can handle;
2551 * that is, the profiles are built into MojoShader and the OpenGL extensions
2552 * required for them exist at runtime. This function returns the number of
2553 * available profiles, which may be more, less, or equal to (size).
2554 *
2555 * If there are more than (size) profiles, the (profs) buffer will not
2556 * overflow. You can check the return value for the total number of
2557 * available profiles, allocate more space, and try again if necessary.
2558 * Calling this function with (size) == 0 is legal.
2559 *
2560 * You can only call this AFTER you have successfully built your GL context
2561 * and made it current. This function will lookup the GL functions it needs
2562 * through the callback you supply, via (lookup) and (lookup_d). The lookup
2563 * function is neither stored nor used by MojoShader after this function
2564 * returns, nor are the functions it might look up.
2565 *
2566 * As MojoShader requires some memory to be allocated, you may provide a
2567 * custom allocator to this function, which will be used to allocate/free
2568 * memory. They function just like malloc() and free(). We do not use
2569 * realloc(). If you don't care, pass NULL in for the allocator functions.
2570 * If your allocator needs instance-specific data, you may supply it with the
2571 * (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
2572 * functions.
2573 *
2574 * You should not free any strings returned from this function; they are
2575 * pointers to internal, probably static, memory.
2576 *
2577 * This call is NOT thread safe! As most OpenGL implementations are not thread
2578 * safe, you should probably only call this from the same thread that created
2579 * the GL context.
2580 */
2581 DECLSPEC int MOJOSHADER_glAvailableProfiles(MOJOSHADER_glGetProcAddress lookup,
2582 void *lookup_d,
2583 const char **profs, const int size,
2584 MOJOSHADER_malloc m, MOJOSHADER_free f,
2585 void *malloc_d);
2586
2587
2588 /*
2589 * Determine the best profile to use for the current system.
2590 *
2591 * You can only call this AFTER you have successfully built your GL context
2592 * and made it current. This function will lookup the GL functions it needs
2593 * through the callback you supply via (lookup) and (lookup_d). The lookup
2594 * function is neither stored nor used by MojoShader after this function
2595 * returns, nor are the functions it might look up.
2596 *
2597 * Returns the name of the "best" profile on success, NULL if none of the
2598 * available profiles will work on this system. "Best" is a relative term,
2599 * but it generally means the best trade off between feature set and
2600 * performance. The selection algorithm may be arbitrary and complex.
2601 *
2602 * As MojoShader requires some memory to be allocated, you may provide a
2603 * custom allocator to this function, which will be used to allocate/free
2604 * memory. They function just like malloc() and free(). We do not use
2605 * realloc(). If you don't care, pass NULL in for the allocator functions.
2606 * If your allocator needs instance-specific data, you may supply it with the
2607 * (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
2608 * functions.
2609 *
2610 * The returned value is an internal static string, and should not be free()'d
2611 * by the caller. If you get a NULL, calling MOJOSHADER_glGetError() might
2612 * shed some light on why.
2613 *
2614 * This call is NOT thread safe! As most OpenGL implementations are not thread
2615 * safe, you should probably only call this from the same thread that created
2616 * the GL context.
2617 */
2618 DECLSPEC const char *MOJOSHADER_glBestProfile(MOJOSHADER_glGetProcAddress lookup,
2619 void *lookup_d,
2620 MOJOSHADER_malloc m, MOJOSHADER_free f,
2621 void *malloc_d);
2622
2623 /*
2624 * Prepare MojoShader to manage OpenGL shaders.
2625 *
2626 * You do not need to call this if all you want is MOJOSHADER_parse().
2627 *
2628 * You must call this once AFTER you have successfully built your GL context
2629 * and made it current. This function will lookup the GL functions it needs
2630 * through the callback you supply via (lookup) and (lookup_d), after which
2631 * it may call them at any time up until you call
2632 * MOJOSHADER_glDestroyContext(). The lookup function is neither stored nor
2633 * used by MojoShader after this function returns.
2634 *
2635 * (profile) is an OpenGL-specific MojoShader profile, which decides how
2636 * Direct3D bytecode shaders get turned into OpenGL programs, and how they
2637 * are fed to the GL.
2638 *
2639 * (lookup) is a callback that is used to load GL entry points. This callback
2640 * has to look up base GL functions and extension entry points. The pointer
2641 * you supply in (lookup_d) is passed as-is to the callback.
2642 *
2643 * As MojoShader requires some memory to be allocated, you may provide a
2644 * custom allocator to this function, which will be used to allocate/free
2645 * memory. They function just like malloc() and free(). We do not use
2646 * realloc(). If you don't care, pass NULL in for the allocator functions.
2647 * If your allocator needs instance-specific data, you may supply it with the
2648 * (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
2649 * functions.
2650 *
2651 * Returns a new context on success, NULL on error. If you get a new context,
2652 * you need to make it current before using it with
2653 * MOJOSHADER_glMakeContextCurrent().
2654 *
2655 * This call is NOT thread safe! It must return success before you may call
2656 * any other MOJOSHADER_gl* function. Also, as most OpenGL implementations
2657 * are not thread safe, you should probably only call this from the same
2658 * thread that created the GL context.
2659 */
2660 DECLSPEC MOJOSHADER_glContext *MOJOSHADER_glCreateContext(const char *profile,
2661 MOJOSHADER_glGetProcAddress lookup,
2662 void *lookup_d,
2663 MOJOSHADER_malloc m, MOJOSHADER_free f,
2664 void *malloc_d);
2665
2666 /*
2667 * You must call this before using the context that you got from
2668 * MOJOSHADER_glCreateContext(), and must use it when you switch to a new GL
2669 * context.
2670 *
2671 * You can only have one MOJOSHADER_glContext per actual GL context, or
2672 * undefined behaviour will result.
2673 *
2674 * It is legal to call this with a NULL pointer to make no context current,
2675 * but you need a valid context to be current to use most of MojoShader.
2676 *
2677 * In current times, this allows one context to be current _per thread_,
2678 * as the internal state inside MojoShader is marked thread local. Each
2679 * new thread should call this function before using the context, even if
2680 * other threads have previously set it current. You _also_ have to set
2681 * the OpenGL context itself current for each thread (and have an OpenGL
2682 * implementation that allows that in the first place).
2683 */
2684 DECLSPEC void MOJOSHADER_glMakeContextCurrent(MOJOSHADER_glContext *ctx);
2685
2686 /*
2687 * Get any error state we might have picked up. MojoShader will NOT call
2688 * glGetError() internally, but there are other errors we can pick up,
2689 * such as failed shader compilation, etc.
2690 *
2691 * Returns a human-readable string. This string is for debugging purposes, and
2692 * not guaranteed to be localized, coherent, or user-friendly in any way.
2693 * It's for programmers!
2694 *
2695 * The latest error may remain between calls. New errors replace any existing
2696 * error. Don't check this string for a sign that an error happened, check
2697 * return codes instead and use this for explanation when debugging.
2698 *
2699 * Do not free the returned string: it's a pointer to a static internal
2700 * buffer. Do not keep the pointer around, either, as it's likely to become
2701 * invalid as soon as you call into MojoShader again.
2702 *
2703 * This call is NOT thread safe! As most OpenGL implementations are not thread
2704 * safe, you should probably only call this from the same thread that created
2705 * the GL context.
2706 *
2707 * This call does NOT require a valid MOJOSHADER_glContext to have been made
2708 * current. The error buffer is shared between contexts, so you can get
2709 * error results from a failed MOJOSHADER_glCreateContext().
2710 */
2711 DECLSPEC const char *MOJOSHADER_glGetError(void);
2712
2713 /*
2714 * Get the maximum uniforms a shader can support for the current GL context,
2715 * MojoShader profile, and shader type. You can use this to make decisions
2716 * about what shaders you want to use (for example, a less complicated
2717 * shader may be swapped in for lower-end systems).
2718 *
2719 * Returns the number, or -1 on error.
2720 *
2721 * This call is NOT thread safe! As most OpenGL implementations are not thread
2722 * safe, you should probably only call this from the same thread that created
2723 * the GL context.
2724 *
2725 * This call requires a valid MOJOSHADER_glContext to have been made current,
2726 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
2727 */
2728 DECLSPEC int MOJOSHADER_glMaxUniforms(MOJOSHADER_shaderType shader_type);
2729
2730 /*
2731 * Compile a buffer of Direct3D shader bytecode into an OpenGL shader.
2732 * You still need to link the shader before you may render with it.
2733 *
2734 * (tokenbuf) is a buffer of Direct3D shader bytecode.
2735 * (bufsize) is the size, in bytes, of the bytecode buffer.
2736 * (swiz), (swizcount), (smap), and (smapcount) are passed to
2737 * MOJOSHADER_parse() unmolested.
2738 *
2739 * Returns NULL on error, or a shader handle on success.
2740 *
2741 * This call is NOT thread safe! As most OpenGL implementations are not thread
2742 * safe, you should probably only call this from the same thread that created
2743 * the GL context.
2744 *
2745 * This call requires a valid MOJOSHADER_glContext to have been made current,
2746 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
2747 *
2748 * Compiled shaders from this function may not be shared between contexts.
2749 */
2750 DECLSPEC MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
2751 const unsigned int bufsize,
2752 const MOJOSHADER_swizzle *swiz,
2753 const unsigned int swizcount,
2754 const MOJOSHADER_samplerMap *smap,
2755 const unsigned int smapcount);
2756
2757 /*
2758 * Increments a shader's internal refcount. To decrement the refcount, call
2759 * MOJOSHADER_glDeleteShader().
2760 *
2761 * This call is NOT thread safe! As most OpenGL implementations are not thread
2762 * safe, you should probably only call this from the same thread that created
2763 * the GL context.
2764 *
2765 * This call requires a valid MOJOSHADER_glContext to have been made current,
2766 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
2767 */
2768 DECLSPEC void MOJOSHADER_glShaderAddRef(MOJOSHADER_glShader *shader);
2769
2770 /*
2771 * Get the MOJOSHADER_parseData structure that was produced from the
2772 * call to MOJOSHADER_glCompileShader().
2773 *
2774 * This data is read-only, and you should NOT attempt to free it. This
2775 * pointer remains valid until the shader is deleted.
2776 */
2777 DECLSPEC const MOJOSHADER_parseData *MOJOSHADER_glGetShaderParseData(
2778 MOJOSHADER_glShader *shader);
2779 /*
2780 * Link a vertex and pixel shader into an OpenGL program.
2781 * (vshader) or (pshader) can be NULL, to specify that the GL should use the
2782 * fixed-function pipeline instead of the programmable pipeline for that
2783 * portion of the work. You can reuse shaders in various combinations across
2784 * multiple programs, by relinking different pairs.
2785 *
2786 * It is illegal to give a vertex shader for (pshader) or a pixel shader
2787 * for (vshader).
2788 *
2789 * Once you have successfully linked a program, you may render with it.
2790 *
2791 * Returns NULL on error, or a program handle on success.
2792 *
2793 * This call is NOT thread safe! As most OpenGL implementations are not thread
2794 * safe, you should probably only call this from the same thread that created
2795 * the GL context.
2796 *
2797 * This call requires a valid MOJOSHADER_glContext to have been made current,
2798 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
2799 *
2800 * Linked programs from this function may not be shared between contexts.
2801 */
2802 DECLSPEC MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
2803 MOJOSHADER_glShader *pshader);
2804
2805 /*
2806 * This binds the program (using, for example, glUseProgramObjectARB()), and
2807 * disables all the client-side arrays so we can reset them with new values
2808 * if appropriate.
2809 *
2810 * Call with NULL to disable the programmable pipeline and all enabled
2811 * client-side arrays.
2812 *
2813 * After binding a program, you should update any uniforms you care about
2814 * with MOJOSHADER_glSetVertexShaderUniformF() (etc), set any vertex arrays
2815 * you want to use with MOJOSHADER_glSetVertexAttribute(), and finally call
2816 * MOJOSHADER_glProgramReady() to commit everything to the GL. Then you may
2817 * begin drawing through standard GL entry points.
2818 *
2819 * This call is NOT thread safe! As most OpenGL implementations are not thread
2820 * safe, you should probably only call this from the same thread that created
2821 * the GL context.
2822 *
2823 * This call requires a valid MOJOSHADER_glContext to have been made current,
2824 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
2825 */
2826 DECLSPEC void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program);
2827
2828 /*
2829 * This binds individual shaders as if you had linked them with
2830 * MOJOSHADER_glLinkProgram(), and used MOJOSHADER_glBindProgram() on the
2831 * linked result.
2832 *
2833 * MojoShader will handle linking behind the scenes, and keep a cache of
2834 * programs linked here. Programs are removed from this cache when one of the
2835 * invidual shaders in it is deleted, otherwise they remain cached so future
2836 * calls to this function don't need to relink a previously-used shader
2837 * grouping.
2838 *
2839 * This function is for convenience, as the API is closer to how Direct3D
2840 * works, and retrofitting linking into your app can be difficult;
2841 * frequently, you just end up building your own cache, anyhow.
2842 *
2843 * Calling with all shaders set to NULL is equivalent to calling
2844 * MOJOSHADER_glBindProgram(NULL).
2845 *
2846 * This call is NOT thread safe! As most OpenGL implementations are not thread
2847 * safe, you should probably only call this from the same thread that created
2848 * the GL context.
2849 *
2850 * This call requires a valid MOJOSHADER_glContext to have been made current,
2851 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
2852 */
2853 DECLSPEC void MOJOSHADER_glBindShaders(MOJOSHADER_glShader *vshader,
2854 MOJOSHADER_glShader *pshader);
2855
2856 /*
2857 * This queries for the shaders currently bound to the active context.
2858 *
2859 * This function is only for convenience, specifically for compatibility with
2860 * the effects API.
2861 *
2862 * This call is NOT thread safe! As most OpenGL implementations are not thread
2863 * safe, you should probably only call this from the same thread that created
2864 * the GL context.
2865 *
2866 * This call requires a valid MOJOSHADER_glContext to have been made current,
2867 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
2868 */
2869 DECLSPEC void MOJOSHADER_glGetBoundShaders(MOJOSHADER_glShader **vshader,
2870 MOJOSHADER_glShader **pshader);
2871
2872 /*
2873 * Set a floating-point uniform value (what Direct3D calls a "constant").
2874 *
2875 * There is a single array of 4-float "registers" shared by all vertex shaders.
2876 * This is the "c" register file in Direct3D (c0, c1, c2, etc...)
2877 * MojoShader will take care of synchronizing this internal array with the
2878 * appropriate variables in the GL shaders.
2879 *
2880 * (idx) is the index into the internal array: 0 is the first four floats,
2881 * 1 is the next four, etc.
2882 * (data) is a pointer to (vec4count*4) floats.
2883 *
2884 * This call is NOT thread safe! As most OpenGL implementations are not thread
2885 * safe, you should probably only call this from the same thread that created
2886 * the GL context.
2887 *
2888 * This call requires a valid MOJOSHADER_glContext to have been made current,
2889 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
2890 *
2891 * Uniforms are not shared between contexts.
2892 */
2893 DECLSPEC void MOJOSHADER_glSetVertexShaderUniformF(unsigned int idx, const float *data,
2894 unsigned int vec4count);
2895
2896 /*
2897 * Retrieve a floating-point uniform value (what Direct3D calls a "constant").
2898 *
2899 * There is a single array of 4-float "registers" shared by all vertex shaders.
2900 * This is the "c" register file in Direct3D (c0, c1, c2, etc...)
2901 * MojoShader will take care of synchronizing this internal array with the
2902 * appropriate variables in the GL shaders.
2903 *
2904 * (idx) is the index into the internal array: 0 is the first four floats,
2905 * 1 is the next four, etc.
2906 * (data) is a pointer to space for (vec4count*4) floats.
2907 * (data) will be filled will current values in the register file. Results
2908 * are undefined if you request data past the end of the register file or
2909 * previously uninitialized registers.
2910 *
2911 * This is a "fast" call; we're just reading from internal memory. We do not
2912 * query the GPU or the GL for this information.
2913 *
2914 * This call is NOT thread safe! As most OpenGL implementations are not thread
2915 * safe, you should probably only call this from the same thread that created
2916 * the GL context.
2917 *
2918 * This call requires a valid MOJOSHADER_glContext to have been made current,
2919 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
2920 *
2921 * Uniforms are not shared between contexts.
2922 */
2923 DECLSPEC void MOJOSHADER_glGetVertexShaderUniformF(unsigned int idx, float *data,
2924 unsigned int vec4count);
2925
2926
2927 /*
2928 * Set an integer uniform value (what Direct3D calls a "constant").
2929 *
2930 * There is a single array of 4-int "registers" shared by all vertex shaders.
2931 * This is the "i" register file in Direct3D (i0, i1, i2, etc...)
2932 * MojoShader will take care of synchronizing this internal array with the
2933 * appropriate variables in the GL shaders.
2934 *
2935 * (idx) is the index into the internal array: 0 is the first four ints,
2936 * 1 is the next four, etc.
2937 * (data) is a pointer to (ivec4count*4) ints.
2938 *
2939 * This call is NOT thread safe! As most OpenGL implementations are not thread
2940 * safe, you should probably only call this from the same thread that created
2941 * the GL context.
2942 *
2943 * This call requires a valid MOJOSHADER_glContext to have been made current,
2944 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
2945 *
2946 * Uniforms are not shared between contexts.
2947 */
2948 DECLSPEC void MOJOSHADER_glSetVertexShaderUniformI(unsigned int idx, const int *data,
2949 unsigned int ivec4count);
2950
2951 /*
2952 * Retrieve an integer uniform value (what Direct3D calls a "constant").
2953 *
2954 * There is a single array of 4-int "registers" shared by all vertex shaders.
2955 * This is the "i" register file in Direct3D (i0, i1, i2, etc...)
2956 * MojoShader will take care of synchronizing this internal array with the
2957 * appropriate variables in the GL shaders.
2958 *
2959 * (idx) is the index into the internal array: 0 is the first four ints,
2960 * 1 is the next four, etc.
2961 * (data) is a pointer to space for (ivec4count*4) ints.
2962 * (data) will be filled will current values in the register file. Results
2963 * are undefined if you request data past the end of the register file or
2964 * previously uninitialized registers.
2965 *
2966 * This is a "fast" call; we're just reading from internal memory. We do not
2967 * query the GPU or the GL for this information.
2968 *
2969 * This call is NOT thread safe! As most OpenGL implementations are not thread
2970 * safe, you should probably only call this from the same thread that created
2971 * the GL context.
2972 *
2973 * This call requires a valid MOJOSHADER_glContext to have been made current,
2974 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
2975 *
2976 * Uniforms are not shared between contexts.
2977 */
2978 DECLSPEC void MOJOSHADER_glGetVertexShaderUniformI(unsigned int idx, int *data,
2979 unsigned int ivec4count);
2980
2981 /*
2982 * Set a boolean uniform value (what Direct3D calls a "constant").
2983 *
2984 * There is a single array of "registers" shared by all vertex shaders.
2985 * This is the "b" register file in Direct3D (b0, b1, b2, etc...)
2986 * MojoShader will take care of synchronizing this internal array with the
2987 * appropriate variables in the GL shaders.
2988 *
2989 * Unlike the float and int counterparts, booleans are single values, not
2990 * four-element vectors...so idx==1 is the second boolean in the internal
2991 * array, not the fifth.
2992 *
2993 * Non-zero values are considered "true" and zero is considered "false".
2994 *
2995 * (idx) is the index into the internal array.
2996 * (data) is a pointer to (bcount) ints.
2997 *
2998 * This call is NOT thread safe! As most OpenGL implementations are not thread
2999 * safe, you should probably only call this from the same thread that created
3000 * the GL context.
3001 *
3002 * This call requires a valid MOJOSHADER_glContext to have been made current,
3003 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3004 *
3005 * Uniforms are not shared between contexts.
3006 */
3007 DECLSPEC void MOJOSHADER_glSetVertexShaderUniformB(unsigned int idx, const int *data,
3008 unsigned int bcount);
3009
3010 /*
3011 * Retrieve a boolean uniform value (what Direct3D calls a "constant").
3012 *
3013 * There is a single array of "registers" shared by all vertex shaders.
3014 * This is the "b" register file in Direct3D (b0, b1, b2, etc...)
3015 * MojoShader will take care of synchronizing this internal array with the
3016 * appropriate variables in the GL shaders.
3017 *
3018 * Unlike the float and int counterparts, booleans are single values, not
3019 * four-element vectors...so idx==1 is the second boolean in the internal
3020 * array, not the fifth.
3021 *
3022 * Non-zero values are considered "true" and zero is considered "false".
3023 * This function will always return true values as 1, regardless of what
3024 * non-zero integer you originally used to set the registers.
3025 *
3026 * (idx) is the index into the internal array.
3027 * (data) is a pointer to space for (bcount) ints.
3028 * (data) will be filled will current values in the register file. Results
3029 * are undefined if you request data past the end of the register file or
3030 * previously uninitialized registers.
3031 *
3032 * This is a "fast" call; we're just reading from internal memory. We do not
3033 * query the GPU or the GL for this information.
3034 *
3035 * This call is NOT thread safe! As most OpenGL implementations are not thread
3036 * safe, you should probably only call this from the same thread that created
3037 * the GL context.
3038 *
3039 * This call requires a valid MOJOSHADER_glContext to have been made current,
3040 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3041 *
3042 * Uniforms are not shared between contexts.
3043 */
3044 DECLSPEC void MOJOSHADER_glGetVertexShaderUniformB(unsigned int idx, int *data,
3045 unsigned int bcount);
3046
3047 /*
3048 * The equivalent of MOJOSHADER_glSetVertexShaderUniformF() for pixel
3049 * shaders. Other than using a different internal array that is specific
3050 * to pixel shaders, this functions just like its vertex array equivalent.
3051 *
3052 * This call is NOT thread safe! As most OpenGL implementations are not thread
3053 * safe, you should probably only call this from the same thread that created
3054 * the GL context.
3055 *
3056 * This call requires a valid MOJOSHADER_glContext to have been made current,
3057 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3058 *
3059 * Uniforms are not shared between contexts.
3060 */
3061 DECLSPEC void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
3062 unsigned int vec4count);
3063
3064
3065 /*
3066 * The equivalent of MOJOSHADER_glGetVertexShaderUniformF() for pixel
3067 * shaders. Other than using a different internal array that is specific
3068 * to pixel shaders, this functions just like its vertex array equivalent.
3069 *
3070 * This call is NOT thread safe! As most OpenGL implementations are not thread
3071 * safe, you should probably only call this from the same thread that created
3072 * the GL context.
3073 *
3074 * This call requires a valid MOJOSHADER_glContext to have been made current,
3075 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3076 *
3077 * Uniforms are not shared between contexts.
3078 */
3079 DECLSPEC void MOJOSHADER_glGetPixelShaderUniformF(unsigned int idx, float *data,
3080 unsigned int vec4count);
3081
3082
3083 /*
3084 * The equivalent of MOJOSHADER_glSetVertexShaderUniformI() for pixel
3085 * shaders. Other than using a different internal array that is specific
3086 * to pixel shaders, this functions just like its vertex array equivalent.
3087 *
3088 * This call is NOT thread safe! As most OpenGL implementations are not thread
3089 * safe, you should probably only call this from the same thread that created
3090 * the GL context.
3091 *
3092 * This call requires a valid MOJOSHADER_glContext to have been made current,
3093 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3094 *
3095 * Uniforms are not shared between contexts.
3096 */
3097 DECLSPEC void MOJOSHADER_glSetPixelShaderUniformI(unsigned int idx, const int *data,
3098 unsigned int ivec4count);
3099
3100
3101 /*
3102 * The equivalent of MOJOSHADER_glGetVertexShaderUniformI() for pixel
3103 * shaders. Other than using a different internal array that is specific
3104 * to pixel shaders, this functions just like its vertex array equivalent.
3105 *
3106 * This call is NOT thread safe! As most OpenGL implementations are not thread
3107 * safe, you should probably only call this from the same thread that created
3108 * the GL context.
3109 *
3110 * This call requires a valid MOJOSHADER_glContext to have been made current,
3111 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3112 *
3113 * Uniforms are not shared between contexts.
3114 */
3115 DECLSPEC void MOJOSHADER_glGetPixelShaderUniformI(unsigned int idx, int *data,
3116 unsigned int ivec4count);
3117
3118 /*
3119 * The equivalent of MOJOSHADER_glSetVertexShaderUniformB() for pixel
3120 * shaders. Other than using a different internal array that is specific
3121 * to pixel shaders, this functions just like its vertex array equivalent.
3122 *
3123 * This call is NOT thread safe! As most OpenGL implementations are not thread
3124 * safe, you should probably only call this from the same thread that created
3125 * the GL context.
3126 *
3127 * This call requires a valid MOJOSHADER_glContext to have been made current,
3128 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3129 *
3130 * Uniforms are not shared between contexts.
3131 */
3132 DECLSPEC void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
3133 unsigned int bcount);
3134
3135 /*
3136 * The equivalent of MOJOSHADER_glGetVertexShaderUniformB() for pixel
3137 * shaders. Other than using a different internal array that is specific
3138 * to pixel shaders, this functions just like its vertex array equivalent.
3139 *
3140 * This call is NOT thread safe! As most OpenGL implementations are not thread
3141 * safe, you should probably only call this from the same thread that created
3142 * the GL context.
3143 *
3144 * This call requires a valid MOJOSHADER_glContext to have been made current,
3145 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3146 *
3147 * Uniforms are not shared between contexts.
3148 */
3149 DECLSPEC void MOJOSHADER_glGetPixelShaderUniformB(unsigned int idx, int *data,
3150 unsigned int bcount);
3151
3152 /*
3153 * Fills register pointers with pointers that are directly used to push uniform
3154 * data to the GL shader context.
3155 *
3156 * This function is really just for the effects API, you should NOT be using
3157 * this unless you know every single line of MojoShader from memory.
3158 *
3159 * This call is NOT thread safe! As most OpenGL implementations are not thread
3160 * safe, you should probably only call this from the same thread that created
3161 * the GL context.
3162 *
3163 * This call requires a valid MOJOSHADER_glContext to have been made current,
3164 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3165 */
3166 DECLSPEC void MOJOSHADER_glMapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb,
3167 float **psf, int **psi, unsigned char **psb);
3168
3169 /*
3170 * Tells the context that you are done with the memory mapped by
3171 * MOJOSHADER_glMapUniformBufferMemory().
3172 *
3173 * This call is NOT thread safe! As most OpenGL implementations are not thread
3174 * safe, you should probably only call this from the same thread that created
3175 * the GL context.
3176 *
3177 * This call requires a valid MOJOSHADER_glContext to have been made current,
3178 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3179 */
3180 DECLSPEC void MOJOSHADER_glUnmapUniformBufferMemory();
3181
3182 /*
3183 * Set up the vector for the TEXBEM opcode. Most apps can ignore this API.
3184 *
3185 * Shader Model 1.1 through 1.3 had an instruction for "fake bump mapping"
3186 * called TEXBEM. To use it, you had to set some sampler states,
3187 * D3DTSS_BUMPENVMATxx, which would be referenced by the opcode.
3188 *
3189 * This functionality was removed from Shader Model 1.4 and later, because
3190 * it was special-purpose and limited. The functionality could be built on
3191 * more general opcodes, and the sampler state could be supplied in a more
3192 * general uniform.
3193 *
3194 * However, to support this opcode, we supply a way to specify that sampler
3195 * state, and the OpenGL glue code does the right thing to pass that
3196 * information to the shader.
3197 *
3198 * This call maps to IDirect3DDevice::SetTextureStageState() with the
3199 * D3DTSS_BUMPENVMAT00, D3DTSS_BUMPENVMAT01, D3DTSS_BUMPENVMAT10,
3200 * D3DTSS_BUMPENVMAT11, D3DTSS_BUMPENVLSCALE, and D3DTSS_BUMPENVLOFFSET
3201 * targets. This is only useful for Shader Model < 1.4 pixel shaders, if
3202 * they use the TEXBEM or TEXBEML opcode. If you aren't sure, you don't need
3203 * this function.
3204 *
3205 * Like the rest of your uniforms, you must call MOJOSHADER_glProgramReady()
3206 * between setting new values and drawing with them.
3207 *
3208 * This call is NOT thread safe! As most OpenGL implementations are not thread
3209 * safe, you should probably only call this from the same thread that created
3210 * the GL context.
3211 *
3212 * This call requires a valid MOJOSHADER_glContext to have been made current,
3213 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3214 *
3215 * These values are not shared between contexts.
3216 */
3217 DECLSPEC void MOJOSHADER_glSetLegacyBumpMapEnv(unsigned int sampler, float mat00,
3218 float mat01, float mat10, float mat11,
3219 float lscale, float loffset);
3220
3221 /*
3222 * Return the location of a vertex attribute for the currently-bound program.
3223 *
3224 * (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
3225 * be MOJOSHADER_USAGE_COLOR and 1.
3226 *
3227 * The return value is the index of the attribute to be sent to
3228 * glVertexAttribPointer, or -1 if the stream is not used.
3229 *
3230 * This call requires a valid MOJOSHADER_glContext to have been made current,
3231 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3232 */
3233 DECLSPEC int MOJOSHADER_glGetVertexAttribLocation(MOJOSHADER_usage usage, int index);
3234
3235 /*
3236 * Connect a client-side array to the currently-bound program.
3237 *
3238 * (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
3239 * be MOJOSHADER_USAGE_COLOR and 1.
3240 *
3241 * The caller should bind VBOs before this call and treat (ptr) as an offset,
3242 * if appropriate.
3243 *
3244 * MojoShader will figure out where to plug this stream into the
3245 * currently-bound program, and enable the appropriate client-side array.
3246 *
3247 * (size), (type), (normalized), (stride), and (ptr) correspond to
3248 * glVertexAttribPointer()'s parameters (in most cases, these get passed
3249 * unmolested to that very entry point during this function).
3250 *
3251 * This call is NOT thread safe! As most OpenGL implementations are not thread
3252 * safe, you should probably only call this from the same thread that created
3253 * the GL context.
3254 *
3255 * This call requires a valid MOJOSHADER_glContext to have been made current,
3256 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3257 *
3258 * Vertex attributes are not shared between contexts.
3259 */
3260 /* !!! FIXME: this should probably be "input" and not "attribute" */
3261 /* !!! FIXME: or maybe "vertex array" or something. */
3262 DECLSPEC void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
3263 int index, unsigned int size,
3264 MOJOSHADER_attributeType type,
3265 int normalized, unsigned int stride,
3266 const void *ptr);
3267
3268 /*
3269 * Modify the rate at which this vertex attribute advances during instanced
3270 * rendering.
3271 *
3272 * This should be called alongside glSetVertexAttribute, as this does not flag
3273 * the vertex array as being in use. This just calls glVertexAttribDivisorARB.
3274 *
3275 * This call is NOT thread safe! As most OpenGL implementations are not thread
3276 * safe, you should probably only call this from the same thread that created
3277 * the GL context.
3278 *
3279 * This call requires a valid MOJOSHADER_glContext to have been made current,
3280 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3281 *
3282 * Vertex attributes are not shared between contexts.
3283 */
3284 DECLSPEC void MOJOSHADER_glSetVertexAttribDivisor(MOJOSHADER_usage usage,
3285 int index, unsigned int divisor);
3286
3287 /*
3288 * Inform MojoShader that it should commit any pending state to the GL. This
3289 * must be called after you bind a program and update any inputs, right
3290 * before you start drawing, so any outstanding changes made to the shared
3291 * constants array (etc) can propagate to the shader during this call.
3292 *
3293 * This call is NOT thread safe! As most OpenGL implementations are not thread
3294 * safe, you should probably only call this from the same thread that created
3295 * the GL context.
3296 *
3297 * This call requires a valid MOJOSHADER_glContext to have been made current,
3298 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3299 */
3300 DECLSPEC void MOJOSHADER_glProgramReady(void);
3301
3302 /*
3303 * Provide information about the current viewport to the prepared shader
3304 * program.
3305 *
3306 * There are numerous components of OpenGL and Direct3D where the coordinate
3307 * systems do not match, and so the vertex/pixel shaders have to be modified to
3308 * compensate for these mismatches (for example, gl_FragCoord requires some
3309 * additional math on the Y coordinate to match vPos when rendering to the
3310 * backbuffer). Call this after MOJOSHADER_glProgramReady to apply all of the
3311 * relevant coordinate fixups at once.
3312 *
3313 * This call is NOT thread safe! As most OpenGL implementations are not thread
3314 * safe, you should probably only call this from the same thread that created
3315 * the GL context.
3316 *
3317 * This call requires a valid MOJOSHADER_glContext to have been made current,
3318 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3319 */
3320 DECLSPEC void MOJOSHADER_glProgramViewportInfo(int viewportW, int viewportH,
3321 int backbufferW, int backbufferH,
3322 int renderTargetBound);
3323
3324 /*
3325 * Free the resources of a linked program. This will delete the GL object
3326 * and free memory.
3327 *
3328 * If the program is currently bound by MOJOSHADER_glBindProgram(), it will
3329 * be deleted as soon as it becomes unbound.
3330 *
3331 * This call is NOT thread safe! As most OpenGL implementations are not thread
3332 * safe, you should probably only call this from the same thread that created
3333 * the GL context.
3334 *
3335 * This call requires a valid MOJOSHADER_glContext to have been made current,
3336 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3337 */
3338 DECLSPEC void MOJOSHADER_glDeleteProgram(MOJOSHADER_glProgram *program);
3339
3340 /*
3341 * Free the resources of a compiled shader. This will delete the GL object
3342 * and free memory.
3343 *
3344 * If the shader is currently referenced by a linked program (or is currently
3345 * bound with MOJOSHADER_glBindShaders()), it will be deleted as soon as all
3346 * referencing programs are deleted and it is no longer bound, too.
3347 *
3348 * This call is NOT thread safe! As most OpenGL implementations are not thread
3349 * safe, you should probably only call this from the same thread that created
3350 * the GL context.
3351 *
3352 * This call requires a valid MOJOSHADER_glContext to have been made current,
3353 * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
3354 */
3355 DECLSPEC void MOJOSHADER_glDeleteShader(MOJOSHADER_glShader *shader);
3356
3357 /*
3358 * Deinitialize MojoShader's OpenGL shader management.
3359 *
3360 * You must call this once, while your GL context (not MojoShader context) is
3361 * still current, if you previously had a successful call to
3362 * MOJOSHADER_glCreateContext(). This should be the last MOJOSHADER_gl*
3363 * function you call until you've prepared a context again.
3364 *
3365 * This will clean up resources previously allocated, and may call into the GL.
3366 *
3367 * This will not clean up shaders and programs you created! Please call
3368 * MOJOSHADER_glDeleteShader() and MOJOSHADER_glDeleteProgram() to clean
3369 * those up before calling this function!
3370 *
3371 * This function destroys the MOJOSHADER_glContext you pass it. If it's the
3372 * current context, then no context will be current upon return.
3373 *
3374 * This call is NOT thread safe! There must not be any other MOJOSHADER_gl*
3375 * functions running when this is called. Also, as most OpenGL implementations
3376 * are not thread safe, you should probably only call this from the same
3377 * thread that created the GL context.
3378 */
3379 DECLSPEC void MOJOSHADER_glDestroyContext(MOJOSHADER_glContext *ctx);
3380
3381
3382 /* D3D11 interface... */
3383
3384 typedef struct MOJOSHADER_d3d11Context MOJOSHADER_d3d11Context;
3385 typedef struct MOJOSHADER_d3d11Shader MOJOSHADER_d3d11Shader;
3386
3387 /*
3388 * Prepare MojoShader to manage Direct3D 11 shaders.
3389 *
3390 * You do not need to call this if all you want is MOJOSHADER_parse().
3391 *
3392 * You must call this once AFTER you have successfully built your D3D11 context.
3393 *
3394 * As MojoShader requires some memory to be allocated, you may provide a
3395 * custom allocator to this function, which will be used to allocate/free
3396 * memory. They function just like malloc() and free(). We do not use
3397 * realloc(). If you don't care, pass NULL in for the allocator functions.
3398 * If your allocator needs instance-specific data, you may supply it with the
3399 * (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
3400 * functions.
3401 *
3402 * This call is only as thread safe as your D3D11 context! If you call your
3403 * context from multiple threads, you must protect this call with whatever
3404 * thread synchronization technique you have for your other D3D calls.
3405 */
3406 DECLSPEC MOJOSHADER_d3d11Context *MOJOSHADER_d3d11CreateContext(void *device,
3407 void *deviceContext,
3408 MOJOSHADER_malloc m,
3409 MOJOSHADER_free f,
3410 void *malloc_d);
3411
3412 /*
3413 * Get any error state we might have picked up, such as failed shader
3414 * compilation.
3415 *
3416 * Returns a human-readable string. This string is for debugging purposes, and
3417 * not guaranteed to be localized, coherent, or user-friendly in any way.
3418 * It's for programmers!
3419 *
3420 * The latest error may remain between calls. New errors replace any existing
3421 * error. Don't check this string for a sign that an error happened, check
3422 * return codes instead and use this for explanation when debugging.
3423 *
3424 * Do not free the returned string: it's a pointer to a static internal
3425 * buffer. Do not keep the pointer around, either, as it's likely to become
3426 * invalid as soon as you call into MojoShader again.
3427 */
3428 DECLSPEC const char *MOJOSHADER_d3d11GetError(MOJOSHADER_d3d11Context *context);
3429
3430 /*
3431 * Compile a buffer of Direct3D 9 shader bytecode into a Direct3D 11 shader.
3432 * You still need to link the shader before you may render with it.
3433 *
3434 * (mainfn) is the name of the shader's main function.
3435 * (tokenbuf) is a buffer of Direct3D shader bytecode.
3436 * (bufsize) is the size, in bytes, of the bytecode buffer.
3437 * (swiz), (swizcount), (smap), and (smapcount) are passed to
3438 * MOJOSHADER_parse() unmolested.
3439 *
3440 * Returns NULL on error, or a shader handle on success.
3441 *
3442 * This call is only as thread safe as your D3D11 context! If you call your
3443 * context from multiple threads, you must protect this call with whatever
3444 * thread synchronization technique you have for your other D3D calls.
3445 */
3446 DECLSPEC MOJOSHADER_d3d11Shader *MOJOSHADER_d3d11CompileShader(MOJOSHADER_d3d11Context *context,
3447 const char *mainfn,
3448 const unsigned char *tokenbuf,
3449 const unsigned int bufsize,
3450 const MOJOSHADER_swizzle *swiz,
3451 const unsigned int swizcount,
3452 const MOJOSHADER_samplerMap *smap,
3453 const unsigned int smapcount);
3454
3455 /*
3456 * Increments a shader's internal refcount. To decrement the refcount, call
3457 * MOJOSHADER_glDeleteShader().
3458 *
3459 * This call is only as thread safe as your D3D11 context! If you call your
3460 * context from multiple threads, you must protect this call with whatever
3461 * thread synchronization technique you have for your other D3D calls.
3462 */
3463 DECLSPEC void MOJOSHADER_d3d11ShaderAddRef(MOJOSHADER_d3d11Shader *shader);
3464
3465 /*
3466 * Get the MOJOSHADER_parseData structure that was produced from the
3467 * call to MOJOSHADER_d3d11CompileShader().
3468 *
3469 * This data is read-only, and you should NOT attempt to free it. This
3470 * pointer remains valid until the shader is deleted.
3471 */
3472 DECLSPEC const MOJOSHADER_parseData *MOJOSHADER_d3d11GetShaderParseData(
3473 MOJOSHADER_d3d11Shader *shader);
3474
3475 /*
3476 * This binds individual shaders together, to be linked into a single working
3477 * program once MOJOSHADER_d3d11ProgramReady is called.
3478 *
3479 * This call is only as thread safe as your D3D11 context! If you call your
3480 * context from multiple threads, you must protect this call with whatever
3481 * thread synchronization technique you have for your other D3D calls.
3482 */
3483 DECLSPEC void MOJOSHADER_d3d11BindShaders(MOJOSHADER_d3d11Context *context,
3484 MOJOSHADER_d3d11Shader *vshader,
3485 MOJOSHADER_d3d11Shader *pshader);
3486
3487 /*
3488 * This queries for the shaders currently bound to the active context.
3489 *
3490 * This function is only for convenience, specifically for compatibility with
3491 * the effects API.
3492 *
3493 * This call is only as thread safe as your D3D11 context! If you call your
3494 * context from multiple threads, you must protect this call with whatever
3495 * thread synchronization technique you have for your other D3D calls.
3496 */
3497 DECLSPEC void MOJOSHADER_d3d11GetBoundShaders(MOJOSHADER_d3d11Context *context,
3498 MOJOSHADER_d3d11Shader **vshader,
3499 MOJOSHADER_d3d11Shader **pshader);
3500
3501 /*
3502 * Fills register pointers with pointers that are directly used to push uniform
3503 * data to the D3D11 shader context.
3504 *
3505 * This function is really just for the effects API, you should NOT be using
3506 * this unless you know every single line of MojoShader from memory.
3507 *
3508 * This call is only as thread safe as your D3D11 context! If you call your
3509 * context from multiple threads, you must protect this call with whatever
3510 * thread synchronization technique you have for your other D3D calls.
3511 */
3512 DECLSPEC void MOJOSHADER_d3d11MapUniformBufferMemory(MOJOSHADER_d3d11Context *context,
3513 float **vsf, int **vsi, unsigned char **vsb,
3514 float **psf, int **psi, unsigned char **psb);
3515
3516 /*
3517 * Tells the context that you are done with the memory mapped by
3518 * MOJOSHADER_d3d11MapUniformBufferMemory().
3519 *
3520 * This call is only as thread safe as your D3D11 context! If you call your
3521 * context from multiple threads, you must protect this call with whatever
3522 * thread synchronization technique you have for your other D3D calls.
3523 */
3524 DECLSPEC void MOJOSHADER_d3d11UnmapUniformBufferMemory(MOJOSHADER_d3d11Context *context);
3525
3526 /*
3527 * Return the location of a vertex attribute for the given vertex shader.
3528 *
3529 * (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
3530 * be MOJOSHADER_USAGE_COLOR and 1.
3531 *
3532 * The return value is the index of the attribute to be used when building the
3533 * input layout object.
3534 */
3535 DECLSPEC int MOJOSHADER_d3d11GetVertexAttribLocation(MOJOSHADER_d3d11Shader *vert,
3536 MOJOSHADER_usage usage,
3537 int index);
3538
3539 /*
3540 * Using the given input layout, compiles the vertex shader with input
3541 * parameters that will be compatible with the incoming vertex data.
3542 *
3543 * (inputLayoutHash) is an application-defined value to differentiate unique
3544 * vertex declarations that will be passed to the vertex shader.
3545 * (elements) is an array of D3D11_INPUT_ELEMENT_DESCs, with (elementCount)
3546 * entries. (bytecode) and (bytecodeLength) will be filled with the final
3547 * compiled D3D11 vertex shader.
3548 *
3549 * Returns 0 on success, nonzero on error.
3550 *
3551 * This call is only as thread safe as your D3D11 context! If you call your
3552 * context from multiple threads, you must protect this call with whatever
3553 * thread synchronization technique you have for your other D3D calls.
3554 */
3555 DECLSPEC int MOJOSHADER_d3d11CompileVertexShader(MOJOSHADER_d3d11Context *ctx,
3556 unsigned long long inputLayoutHash,
3557 void *elements, int elementCount,
3558 void **bytecode, int *bytecodeLength);
3559
3560 /*
3561 * Inform MojoShader that it should commit any pending state and prepare the
3562 * final shader program object, linking the input/output parameter data to
3563 * be compatible with the more-strict Shader Model 4 rule set. This must be
3564 * called after you bind shaders and update any inputs, right before you start
3565 * drawing, so any outstanding changes made to the shared constants array (etc)
3566 * can propagate to the shader during this call.
3567 *
3568 * Returns 0 on success, nonzero on error.
3569 *
3570 * This call is only as thread safe as your D3D11 context! If you call your
3571 * context from multiple threads, you must protect this call with whatever
3572 * thread synchronization technique you have for your other D3D calls.
3573 */
3574 DECLSPEC int MOJOSHADER_d3d11ProgramReady(MOJOSHADER_d3d11Context *context,
3575 unsigned long long inputLayoutHash);
3576
3577 /*
3578 * Free the resources of a compiled shader. This will delete the shader object
3579 * and free memory.
3580 *
3581 * This call is only as thread safe as your D3D11 context! If you call your
3582 * context from multiple threads, you must protect this call with whatever
3583 * thread synchronization technique you have for your other D3D calls.
3584 */
3585 DECLSPEC void MOJOSHADER_d3d11DeleteShader(MOJOSHADER_d3d11Context *context,
3586 MOJOSHADER_d3d11Shader *shader);
3587
3588 /*
3589 * Deinitialize MojoShader's D3D11 shader management.
3590 *
3591 * This will clean up resources previously allocated for the active context.
3592 *
3593 * This will NOT clean up shaders you created! Please destroy all shaders
3594 * before calling this function.
3595 */
3596 DECLSPEC void MOJOSHADER_d3d11DestroyContext(MOJOSHADER_d3d11Context *context);
3597
3598
3599 /* SDL_GPU interface... */
3600
3601 typedef struct MOJOSHADER_sdlContext MOJOSHADER_sdlContext;
3602 typedef struct MOJOSHADER_sdlShaderData MOJOSHADER_sdlShaderData;
3603 typedef struct MOJOSHADER_sdlProgram MOJOSHADER_sdlProgram;
3604
3605 #ifndef SDL_GPU_H
3606 typedef struct SDL_GPUDevice SDL_GPUDevice;
3607 typedef struct SDL_GPUShader SDL_GPUShader;
3608 typedef struct SDL_GPUCommandBuffer SDL_GPUCommandBuffer;
3609 #endif /* SDL_GPU_H */
3610
3611 /*
3612 * Call this function to get the 'formatFlags' parameter for
3613 * SDL_CreateGPUDevice.
3614 *
3615 * Returns the SDL_GPUShaderFormatFlagBits for creating the SDL_GPUDevice.
3616 */
3617 DECLSPEC unsigned int MOJOSHADER_sdlGetShaderFormats(void);
3618
3619 /*
3620 * Prepares a context to manage SDL_gpu shaders.
3621 *
3622 * You do not need to call this if all you want is MOJOSHADER_parse().
3623 *
3624 * (device) refers to the SDL_GPUDevice.
3625 *
3626 * You can only have one MOJOSHADER_sdlContext per actual SDL_gpu context, or
3627 * undefined behaviour will result.
3628 *
3629 * As MojoShader requires some memory to be allocated, you may provide a
3630 * custom allocator to this function, which will be used to allocate/free
3631 * memory. They function just like malloc() and free(). We do not use
3632 * realloc(). If you don't care, pass NULL in for the allocator functions.
3633 * If your allocator needs instance-specific data, you may supply it with the
3634 * (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
3635 * functions.
3636 *
3637 * Returns a new context on success, NULL on error.
3638 */
3639 DECLSPEC MOJOSHADER_sdlContext *MOJOSHADER_sdlCreateContext(SDL_GPUDevice *device,
3640 MOJOSHADER_malloc m,
3641 MOJOSHADER_free f,
3642 void *malloc_d);
3643
3644 /*
3645 * Get any error state we might have picked up.
3646 *
3647 * Returns a human-readable string. This string is for debugging purposes, and
3648 * not guaranteed to be localized, coherent, or user-friendly in any way.
3649 * It's for programmers!
3650 *
3651 * The latest error may remain between calls. New errors replace any existing
3652 * error. Don't check this string for a sign that an error happened, check
3653 * return codes instead and use this for explanation when debugging.
3654 *
3655 * Do not free the returned string: it's a pointer to a static internal
3656 * buffer. Do not keep the pointer around, either, as it's likely to become
3657 * invalid as soon as you call into MojoShader again.
3658 *
3659 * This call does NOT require a valid MOJOSHADER_sdlContext to have been made
3660 * current. The error buffer is shared between contexts, so you can get
3661 * error results from a failed MOJOSHADER_sdlCreateContext().
3662 */
3663 DECLSPEC const char *MOJOSHADER_sdlGetError(MOJOSHADER_sdlContext *ctx);
3664
3665 /*
3666 * Deinitialize MojoShader's SDL_gpu shader management.
3667 *
3668 * You must call this once, while your SDL_GPUDevice is still valid. This should
3669 * be the last MOJOSHADER_sdl* function you call until you've prepared a context
3670 * again.
3671 *
3672 * This will clean up resources previously allocated, and may call into SDL_gpu.
3673 *
3674 * This will not clean up shaders and programs you created! Please call
3675 * MOJOSHADER_sdlDeleteShader() and MOJOSHADER_sdlDeleteProgram() to clean
3676 * those up before calling this function!
3677 *
3678 * This function destroys the MOJOSHADER_sdlContext you pass it.
3679 */
3680 DECLSPEC void MOJOSHADER_sdlDestroyContext(MOJOSHADER_sdlContext *ctx);
3681
3682 /*
3683 * Compile a buffer of Direct3D shader bytecode into an SDL_gpu shader module.
3684 *
3685 * (tokenbuf) is a buffer of Direct3D shader bytecode.
3686 * (bufsize) is the size, in bytes, of the bytecode buffer.
3687 * (swiz), (swizcount), (smap), and (smapcount) are passed to
3688 * MOJOSHADER_parse() unmolested.
3689 *
3690 * Returns NULL on error, or a shader handle on success.
3691 *
3692 * Compiled shaders from this function may not be shared between contexts.
3693 */
3694 DECLSPEC MOJOSHADER_sdlShaderData *MOJOSHADER_sdlCompileShader(MOJOSHADER_sdlContext *ctx,
3695 const char *mainfn,
3696 const unsigned char *tokenbuf,
3697 const unsigned int bufsize,
3698 const MOJOSHADER_swizzle *swiz,
3699 const unsigned int swizcount,
3700 const MOJOSHADER_samplerMap *smap,
3701 const unsigned int smapcount);
3702
3703 /*
3704 * Increments a shader's internal refcount.
3705 *
3706 * To decrement the refcount, call MOJOSHADER_sdlDeleteShader().
3707 */
3708 DECLSPEC void MOJOSHADER_sdlShaderAddRef(MOJOSHADER_sdlShaderData *shader);
3709
3710 /*
3711 * Decrements a shader's internal refcount, and deletes if the refcount is zero.
3712 *
3713 * To increment the refcount, call MOJOSHADER_sdlShaderAddRef().
3714 */
3715 DECLSPEC void MOJOSHADER_sdlDeleteShader(MOJOSHADER_sdlContext *ctx,
3716 MOJOSHADER_sdlShaderData *shader);
3717
3718 /*
3719 * Get the MOJOSHADER_parseData structure that was produced from the
3720 * call to MOJOSHADER_sdlCompileShader().
3721 *
3722 * This data is read-only, and you should NOT attempt to free it. This
3723 * pointer remains valid until the shader is deleted.
3724 */
3725 DECLSPEC const MOJOSHADER_parseData *MOJOSHADER_sdlGetShaderParseData(
3726 MOJOSHADER_sdlShaderData *shader);
3727
3728 /*
3729 * Link bound vertex and pixel shader into a working SDL_gpu shader program.
3730 * (vshader) or (pshader) can NOT be NULL, unlike OpenGL.
3731 *
3732 * You can reuse shaders in various combinations across
3733 * multiple programs, by relinking different pairs.
3734 *
3735 * Requires vertex element data for patches.
3736 *
3737 * It is illegal to give a vertex shader for (pshader) or a pixel shader
3738 * for (vshader).
3739 *
3740 * Once you have successfully linked a program, you may render with it.
3741 *
3742 * Returns NULL on error, or a program handle on success.
3743 */
3744 DECLSPEC MOJOSHADER_sdlProgram *MOJOSHADER_sdlLinkProgram(MOJOSHADER_sdlContext *context,
3745 MOJOSHADER_vertexAttribute *vertexAttributes,
3746 int vertexAttributeCount);
3747
3748 /*
3749 * This binds the program to the active context, and does nothing particularly
3750 * special until you start working with uniform buffers or shader modules.
3751 *
3752 * After binding a program, you should update any uniforms you care about
3753 * with MOJOSHADER_sdlMapUniformBufferMemory() (etc), set any vertex arrays
3754 * using MOJOSHADER_sdlGetVertexAttribLocation(), and finally call
3755 * MOJOSHADER_sdlGetShaders() to get the final modules. Then you may
3756 * begin building your pipeline state objects.
3757 */
3758 DECLSPEC void MOJOSHADER_sdlBindProgram(MOJOSHADER_sdlContext *context,
3759 MOJOSHADER_sdlProgram *program);
3760
3761 /*
3762 * Free the resources of a linked program. This will delete the shader modules
3763 * and free memory.
3764 *
3765 * If the program is currently bound by MOJOSHADER_sdlBindProgram(), it will
3766 * be deleted as soon as it becomes unbound.
3767 */
3768 DECLSPEC void MOJOSHADER_sdlDeleteProgram(MOJOSHADER_sdlContext *context,
3769 MOJOSHADER_sdlProgram *program);
3770
3771 /*
3772 * This "binds" individual shaders, which effectively means the context
3773 * will store these shaders for later retrieval. No actual binding or
3774 * pipeline creation is performed.
3775 *
3776 * This function is only for convenience, specifically for compatibility
3777 * with the effects API.
3778 */
3779 DECLSPEC void MOJOSHADER_sdlBindShaders(MOJOSHADER_sdlContext *ctx,
3780 MOJOSHADER_sdlShaderData *vshader,
3781 MOJOSHADER_sdlShaderData *pshader);
3782
3783 /*
3784 * This queries for the shaders currently bound to the active context.
3785 *
3786 * This function is only for convenience, specifically for compatibility
3787 * with the effects API.
3788 */
3789 DECLSPEC void MOJOSHADER_sdlGetBoundShaderData(MOJOSHADER_sdlContext *ctx,
3790 MOJOSHADER_sdlShaderData **vshader,
3791 MOJOSHADER_sdlShaderData **pshader);
3792
3793 /*
3794 * Fills register pointers with pointers that are directly used to push uniform
3795 * data to the SDL3 shader context.
3796 *
3797 * This function is really just for the effects API, you should NOT be using
3798 * this unless you know every single line of MojoShader from memory.
3799 */
3800 DECLSPEC void MOJOSHADER_sdlMapUniformBufferMemory(MOJOSHADER_sdlContext *ctx,
3801 float **vsf, int **vsi, unsigned char **vsb,
3802 float **psf, int **psi, unsigned char **psb);
3803
3804 /*
3805 * Tells the context that you are done with the memory mapped by
3806 * MOJOSHADER_sdlMapUniformBufferMemory().
3807 */
3808 DECLSPEC void MOJOSHADER_sdlUnmapUniformBufferMemory(MOJOSHADER_sdlContext *ctx);
3809
3810 /*
3811 * Returns the minimum required size of the uniform buffer for this shader.
3812 * You will need this to fill out the SDL_GPUGraphicsPipelineCreateInfo struct.
3813 */
3814 DECLSPEC int MOJOSHADER_sdlGetUniformBufferSize(MOJOSHADER_sdlShaderData *shader);
3815
3816 /*
3817 * Pushes the uniform buffer updates for the currently bound program.
3818 *
3819 * This function will record calls to SDL_GPUPush*ShaderUniforms into the
3820 * passed command buffer.
3821 */
3822 DECLSPEC void MOJOSHADER_sdlUpdateUniformBuffers(MOJOSHADER_sdlContext *ctx,
3823 SDL_GPUCommandBuffer *cb);
3824
3825 /*
3826 * Return the location of a vertex attribute for the given shader.
3827 *
3828 * (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
3829 * be MOJOSHADER_USAGE_COLOR and 1.
3830 *
3831 * The return value is the index of the attribute to be used to create
3832 * an SDL_GPUVertexAttribute, or -1 if the stream is not used.
3833 */
3834 DECLSPEC int MOJOSHADER_sdlGetVertexAttribLocation(MOJOSHADER_sdlShaderData *vert,
3835 MOJOSHADER_usage usage,
3836 int index);
3837
3838 /*
3839 * Get the SDL_GPUShaderModules from the currently bound shader program.
3840 */
3841 DECLSPEC void MOJOSHADER_sdlGetShaders(MOJOSHADER_sdlContext *ctx,
3842 SDL_GPUShader **vshader,
3843 SDL_GPUShader **pshader);
3844
3845 /*
3846 * Gets the number of sampler slots needed by a given shader module.
3847 */
3848 DECLSPEC unsigned int MOJOSHADER_sdlGetSamplerSlots(MOJOSHADER_sdlShaderData *shader);
3849
3850
3851 /* Effects interface... */
3852 #include "mojoshader_effects.h"
3853
3854
3855 #ifdef __cplusplus
3856 }
3857 #endif
3858
3859 #endif /* include-once blocker. */
3860
3861 /* end of mojoshader.h ... */
3862