Skip to content

Latest commit

 

History

History
878 lines (805 loc) · 33.6 KB

mojoshader.h

File metadata and controls

878 lines (805 loc) · 33.6 KB
 
Feb 10, 2008
Feb 10, 2008
1
/**
Mar 22, 2008
Mar 22, 2008
2
3
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
Feb 10, 2008
Feb 10, 2008
4
5
6
7
8
9
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
Mar 22, 2008
Mar 22, 2008
10
11
#ifndef __INCL_MOJOSHADER_H_
#define __INCL_MOJOSHADER_H_
Feb 10, 2008
Feb 10, 2008
12
13
14
15
16
#ifdef __cplusplus
extern "C" {
#endif
Mar 16, 2008
Mar 16, 2008
17
/*
Mar 28, 2008
Mar 28, 2008
18
19
20
21
22
* For determining the version of MojoShader you are using:
* const int compiled_against = MOJOSHADER_VERSION;
* const int linked_against = MOJOSHADER_version();
*
* The version is a single integer that increments, not a major/minor value.
Mar 16, 2008
Mar 16, 2008
23
*/
Mar 22, 2008
Mar 22, 2008
24
25
#define MOJOSHADER_VERSION 1
int MOJOSHADER_version(void);
Mar 16, 2008
Mar 16, 2008
26
27
28
/*
* These allocators work just like the C runtime's malloc() and free()
Mar 28, 2008
Mar 28, 2008
29
30
* (in fact, they probably use malloc() and free() internally if you don't
* specify your own allocator, but don't rely on that behaviour).
Apr 4, 2008
Apr 4, 2008
31
32
33
* (data) is the pointer you supplied when specifying these allocator
* callbacks, in case you need instance-specific data...it is passed through
* to your allocator unmolested, and can be NULL if you like.
Mar 16, 2008
Mar 16, 2008
34
*/
Apr 4, 2008
Apr 4, 2008
35
36
typedef void *(*MOJOSHADER_malloc)(int bytes, void *data);
typedef void (*MOJOSHADER_free)(void *ptr, void *data);
Mar 14, 2008
Mar 14, 2008
37
Mar 28, 2008
Mar 28, 2008
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* These are enum values, but they also can be used in bitmasks, so we can
* test if an opcode is acceptable: if (op->shader_types & ourtype) {} ...
*/
typedef enum
{
MOJOSHADER_TYPE_UNKNOWN = 0,
MOJOSHADER_TYPE_PIXEL = (1 << 0),
MOJOSHADER_TYPE_VERTEX = (1 << 1),
MOJOSHADER_TYPE_GEOMETRY = (1 << 2), /* (not supported yet.) */
MOJOSHADER_TYPE_ANY = 0xFFFFFFFF /* used for bitmasks */
} MOJOSHADER_shaderType;
Apr 26, 2008
Apr 26, 2008
52
53
54
55
56
/*
* Data types for vertex attribute streams.
*/
typedef enum
{
Apr 28, 2008
Apr 28, 2008
57
MOJOSHADER_ATTRIBUTE_UNKNOWN = -1, /* housekeeping; not returned. */
Apr 26, 2008
Apr 26, 2008
58
59
60
61
62
63
64
MOJOSHADER_ATTRIBUTE_BYTE,
MOJOSHADER_ATTRIBUTE_UBYTE,
MOJOSHADER_ATTRIBUTE_SHORT,
MOJOSHADER_ATTRIBUTE_USHORT,
MOJOSHADER_ATTRIBUTE_INT,
MOJOSHADER_ATTRIBUTE_UINT,
MOJOSHADER_ATTRIBUTE_FLOAT,
Apr 28, 2008
Apr 28, 2008
65
66
MOJOSHADER_ATTRIBUTE_DOUBLE,
MOJOSHADER_ATTRIBUTE_HALF_FLOAT, /* MAYBE available in your OpenGL! */
Apr 26, 2008
Apr 26, 2008
67
68
} MOJOSHADER_attributeType;
Apr 4, 2008
Apr 4, 2008
69
70
71
/*
* Data types for uniforms. See MOJOSHADER_uniform for more information.
*/
Apr 4, 2008
Apr 4, 2008
72
73
typedef enum
{
Apr 28, 2008
Apr 28, 2008
74
MOJOSHADER_UNIFORM_UNKNOWN = -1, /* housekeeping value; never returned. */
Apr 19, 2008
Apr 19, 2008
75
76
77
MOJOSHADER_UNIFORM_FLOAT,
MOJOSHADER_UNIFORM_INT,
MOJOSHADER_UNIFORM_BOOL,
Apr 4, 2008
Apr 4, 2008
78
} MOJOSHADER_uniformType;
Apr 4, 2008
Apr 4, 2008
79
Apr 4, 2008
Apr 4, 2008
80
81
82
83
84
85
86
/*
* These are the uniforms to be set for a shader. "Uniforms" are what Direct3D
* calls "Constants" ... IDirect3DDevice::SetVertexShaderConstantF() would
* need this data, for example. These integers are register indexes. So if
* index==6 and type==MOJOSHADER_UNIFORM_FLOAT, that means we'd expect a
* 4-float vector to be specified for what would be register "c6" in D3D
* assembly language, before drawing with the shader.
May 5, 2008
May 5, 2008
87
88
89
90
91
92
93
94
* (array_count) means this is an array of uniforms...this happens in some
* profiles when we see a relative address ("c0[a0.x]", not the usual "c0").
* In those cases, the shader was built to set some range of constant
* registers as an array. You should set this array with (array_count)
* elements from the constant register file, starting at (index) instead of
* just a single uniform. To be extra difficult, you'll need to fill in the
* correct values from the MOJOSHADER_constant data into the appropriate
* parts of the array, overriding the constant register file. Fun!
Apr 25, 2008
Apr 25, 2008
95
96
* (name) is a profile-specific variable name; it may be NULL if it isn't
* applicable to the requested profile.
Apr 4, 2008
Apr 4, 2008
97
*/
Apr 4, 2008
Apr 4, 2008
98
99
typedef struct
{
Apr 4, 2008
Apr 4, 2008
100
MOJOSHADER_uniformType type;
Apr 5, 2008
Apr 5, 2008
101
int index;
May 5, 2008
May 5, 2008
102
int array_count;
Apr 25, 2008
Apr 25, 2008
103
const char *name;
Apr 4, 2008
Apr 4, 2008
104
105
} MOJOSHADER_uniform;
May 3, 2008
May 3, 2008
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* These are the constants defined in a shader. These are data values
* hardcoded in a shader (with the DEF, DEFI, DEFB instructions), which
* override your Uniforms. This data is largely for informational purposes,
* since they are compiled in and can't be changed, like Uniforms can be.
* These integers are register indexes. So if index==6 and
* type==MOJOSHADER_UNIFORM_FLOAT, that means we'd expect a 4-float vector
* to be specified for what would be register "c6" in D3D assembly language,
* before drawing with the shader.
* (value) is the value of the constant, unioned by type.
*/
typedef struct
{
MOJOSHADER_uniformType type;
int index;
union
{
float f[4]; /* if type==MOJOSHADER_UNIFORM_FLOAT */
int i[4]; /* if type==MOJOSHADER_UNIFORM_INT */
int b; /* if type==MOJOSHADER_UNIFORM_BOOL */
} value;
} MOJOSHADER_constant;
Apr 19, 2008
Apr 19, 2008
129
130
131
132
133
/*
* Data types for samplers. See MOJOSHADER_sampler for more information.
*/
typedef enum
{
Apr 28, 2008
Apr 28, 2008
134
MOJOSHADER_SAMPLER_UNKNOWN = -1, /* housekeeping value; never returned. */
Apr 19, 2008
Apr 19, 2008
135
136
137
138
139
140
141
142
143
144
145
146
MOJOSHADER_SAMPLER_2D,
MOJOSHADER_SAMPLER_CUBE,
MOJOSHADER_SAMPLER_VOLUME,
} MOJOSHADER_samplerType;
/*
* These are the samplers to be set for a shader. ...
* IDirect3DDevice::SetTexture() would need this data, for example.
* These integers are the sampler "stage". So if index==6 and
* type==MOJOSHADER_SAMPLER_2D, that means we'd expect a regular 2D texture
* to be specified for what would be register "s6" in D3D assembly language,
* before drawing with the shader.
Apr 25, 2008
Apr 25, 2008
147
148
* (name) is a profile-specific variable name; it may be NULL if it isn't
* applicable to the requested profile.
Apr 19, 2008
Apr 19, 2008
149
150
151
152
153
*/
typedef struct
{
MOJOSHADER_samplerType type;
int index;
Apr 25, 2008
Apr 25, 2008
154
const char *name;
Apr 19, 2008
Apr 19, 2008
155
156
} MOJOSHADER_sampler;
Apr 5, 2008
Apr 5, 2008
157
158
159
160
161
/*
* Data types for attributes. See MOJOSHADER_attribute for more information.
*/
typedef enum
{
Apr 28, 2008
Apr 28, 2008
162
MOJOSHADER_USAGE_UNKNOWN = -1, /* housekeeping value; never returned. */
Apr 19, 2008
Apr 19, 2008
163
164
165
166
167
168
169
170
171
172
173
174
175
176
MOJOSHADER_USAGE_POSITION,
MOJOSHADER_USAGE_BLENDWEIGHT,
MOJOSHADER_USAGE_BLENDINDICES,
MOJOSHADER_USAGE_NORMAL,
MOJOSHADER_USAGE_POINTSIZE,
MOJOSHADER_USAGE_TEXCOORD,
MOJOSHADER_USAGE_TANGENT,
MOJOSHADER_USAGE_BINORMAL,
MOJOSHADER_USAGE_TESSFACTOR,
MOJOSHADER_USAGE_POSITIONT,
MOJOSHADER_USAGE_COLOR,
MOJOSHADER_USAGE_FOG,
MOJOSHADER_USAGE_DEPTH,
MOJOSHADER_USAGE_SAMPLE,
Apr 28, 2008
Apr 28, 2008
177
MOJOSHADER_USAGE_TOTAL, /* housekeeping value; never returned. */
Apr 5, 2008
Apr 5, 2008
178
179
180
181
182
183
184
185
186
187
} MOJOSHADER_usage;
/*
* These are the attributes to be set for a shader. "Attributes" are what
* Direct3D calls "Vertex Declarations Usages" ...
* IDirect3DDevice::CreateVertexDeclaration() would need this data, for
* example. Each attribute is associated with an array of data that uses one
* element per-vertex. So if usage==MOJOSHADER_USAGE_COLOR and index==1, that
* means we'd expect a secondary color array to be bound to this shader
* before drawing.
Apr 25, 2008
Apr 25, 2008
188
189
* (name) is a profile-specific variable name; it may be NULL if it isn't
* applicable to the requested profile.
Apr 5, 2008
Apr 5, 2008
190
191
192
193
194
*/
typedef struct
{
MOJOSHADER_usage usage;
int index;
Apr 25, 2008
Apr 25, 2008
195
const char *name;
Apr 5, 2008
Apr 5, 2008
196
} MOJOSHADER_attribute;
Mar 28, 2008
Mar 28, 2008
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Structure used to return data from parsing of a shader...
*/
typedef struct
{
/*
* Human-readable error, if there is one. Will be NULL if there was no
* error. The string will be UTF-8 encoded, and English only. Most of
* these shouldn't be shown to the end-user anyhow.
*/
const char *error;
Apr 23, 2008
Apr 23, 2008
210
211
212
213
214
/*
* The name of the profile used to parse the shader. Will be NULL on error.
*/
const char *profile;
Mar 28, 2008
Mar 28, 2008
215
216
217
218
219
220
221
222
223
/*
* Bytes of output from parsing. Most profiles produce a string of source
* code, but profiles that do binary output may not be text at all.
* Will be NULL on error.
*/
const char *output;
/*
* Byte count for output, not counting any null terminator. Most profiles
Apr 6, 2008
Apr 6, 2008
224
225
226
* produce an ASCII string of source code (which will be null-terminated
* even though that null char isn't included in output_len), but profiles
* that do binary output may not be text at all. Will be 0 on error.
Mar 28, 2008
Mar 28, 2008
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
*/
int output_len;
/*
* Count of Direct3D instructions we parsed. This is meaningless in terms
* of the actual output, as the profile will probably grow or reduce
* the count (or for high-level languages, not have that information at
* all), but it can give you a rough idea of the size of your shader.
* Will be zero on error.
*/
int instruction_count;
/*
* The type of shader we parsed. Will be MOJOSHADER_TYPE_UNKNOWN on error.
*/
MOJOSHADER_shaderType shader_type;
/*
* The shader's major version. If this was a "vs_3_0", this would be 3.
*/
int major_ver;
/*
* The shader's minor version. If this was a "ps_1_4", this would be 4.
* Two notes: for "vs_2_x", this is 1, and for "vs_3_sw", this is 255.
*/
int minor_ver;
Apr 4, 2008
Apr 4, 2008
255
256
257
258
259
260
/*
* The number of elements pointed to by (uniforms).
*/
int uniform_count;
/*
Apr 4, 2008
Apr 4, 2008
261
262
* (uniform_count) elements of data that specify Uniforms to be set for
* this shader. See discussion on MOJOSHADER_uniform for details.
Apr 19, 2008
Apr 19, 2008
263
* This can be NULL on error or if (uniform_count) is zero.
Apr 4, 2008
Apr 4, 2008
264
265
266
*/
MOJOSHADER_uniform *uniforms;
May 3, 2008
May 3, 2008
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* The number of elements pointed to by (constants).
*/
int constant_count;
/*
* (constant_count) elements of data that specify constants used in
* this shader. See discussion on MOJOSHADER_constant for details.
* This can be NULL on error or if (constant_count) is zero.
* This is largely informational: constants are hardcoded into a shader.
* The constants that you can set like parameters are in the "uniforms"
* list.
*/
MOJOSHADER_constant *constants;
Apr 19, 2008
Apr 19, 2008
282
283
284
285
286
287
288
289
290
291
292
293
/*
* The number of elements pointed to by (samplers).
*/
int sampler_count;
/*
* (sampler_count) elements of data that specify Samplers to be set for
* this shader. See discussion on MOJOSHADER_sampler for details.
* This can be NULL on error or if (sampler_count) is zero.
*/
MOJOSHADER_sampler *samplers;
Apr 5, 2008
Apr 5, 2008
294
295
296
297
298
299
300
301
/*
* The number of elements pointed to by (attributes).
*/
int attribute_count;
/*
* (attribute_count) elements of data that specify Attributes to be set
* for this shader. See discussion on MOJOSHADER_attribute for details.
Apr 19, 2008
Apr 19, 2008
302
* This can be NULL on error or if (attribute_count) is zero.
Apr 5, 2008
Apr 5, 2008
303
304
305
*/
MOJOSHADER_attribute *attributes;
Mar 28, 2008
Mar 28, 2008
306
307
308
309
310
311
312
313
314
/*
* This is the malloc implementation you passed to MOJOSHADER_parse().
*/
MOJOSHADER_malloc malloc;
/*
* This is the free implementation you passed to MOJOSHADER_parse().
*/
MOJOSHADER_free free;
Apr 4, 2008
Apr 4, 2008
315
316
317
318
319
/*
* This is the pointer you passed as opaque data for your allocator.
*/
void *malloc_data;
Mar 28, 2008
Mar 28, 2008
320
321
322
323
324
325
326
327
} MOJOSHADER_parseData;
/*
* Profile string for Direct3D assembly language output.
*/
#define MOJOSHADER_PROFILE_D3D "d3d"
Apr 6, 2008
Apr 6, 2008
328
329
330
331
332
/*
* Profile string for passthrough of the original bytecode, unchanged.
*/
#define MOJOSHADER_PROFILE_PASSTHROUGH "passthrough"
Mar 28, 2008
Mar 28, 2008
333
334
335
336
337
/*
* Profile string for GLSL: OpenGL high-level shader language output.
*/
#define MOJOSHADER_PROFILE_GLSL "glsl"
Apr 4, 2008
Apr 4, 2008
338
Mar 28, 2008
Mar 28, 2008
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Parse a compiled Direct3D shader's bytecode.
*
* This is your primary entry point into MojoShader. You need to pass it
* a compiled D3D shader and tell it which "profile" you want to use to
* convert it into useful data.
*
* The available profiles are the set of MOJOSHADER_PROFILE_* defines.
* Note that MojoShader may be built without support for all listed
* profiles (in which case using one here will return with an error).
*
* As parsing requires some memory to be allocated, you may provide a custom
* allocator to this function, which will be used to allocate/free memory.
* They function just like malloc() and free(). We do not use realloc().
Apr 4, 2008
Apr 4, 2008
353
354
355
* If you don't care, pass NULL in for the allocator functions. If your
* allocator needs instance-specific data, you may supply it with the
* (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
Mar 28, 2008
Mar 28, 2008
356
*
Apr 4, 2008
Apr 4, 2008
357
* This function returns a MOJOSHADER_parseData.
Mar 28, 2008
Mar 28, 2008
358
359
360
361
362
363
*
* This function will never return NULL, even if the system is completely
* out of memory upon entry (in which case, this function returns a static
* MOJOSHADER_parseData object, which is still safe to pass to
* MOJOSHADER_freeParseData()).
*
Apr 23, 2008
Apr 23, 2008
364
* This function is thread safe, so long as (m) and (f) are too, and that
Mar 28, 2008
Mar 28, 2008
365
366
367
368
369
370
371
* (tokenbuf) remains intact for the duration of the call. This allows you
* to parse several shaders on separate CPU cores at the same time.
*/
const MOJOSHADER_parseData *MOJOSHADER_parse(const char *profile,
const unsigned char *tokenbuf,
const unsigned int bufsize,
MOJOSHADER_malloc m,
Apr 4, 2008
Apr 4, 2008
372
373
MOJOSHADER_free f,
void *d);
Mar 28, 2008
Mar 28, 2008
374
375
376
377
378
379
380
381
382
383
384
/*
* Call this to dispose of parsing results when you are done with them.
* This will call the MOJOSHADER_free function you provided to
* MOJOSHADER_parse multiple times, if you provided one.
* Passing a NULL here is a safe no-op.
*
* This function is thread safe, so long as any allocator you passed into
* MOJOSHADER_parse() is, too.
*/
void MOJOSHADER_freeParseData(const MOJOSHADER_parseData *data);
Feb 10, 2008
Feb 10, 2008
385
Apr 26, 2008
Apr 26, 2008
386
Apr 27, 2008
Apr 27, 2008
387
388
389
390
/* OpenGL interface... */
Apr 28, 2008
Apr 28, 2008
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* "Contexts" map to OpenGL contexts...you need one per window, or whatever,
* and need to inform MojoShader when you make a new one current.
*
* "Shaders" refer to individual vertex or pixel programs, and are created
* by "compiling" Direct3D shader bytecode. A vertex and pixel shader are
* "linked" into a "Program" before you can use them to render.
*
* To the calling application, these are all opaque handles.
*/
typedef struct MOJOSHADER_glContext MOJOSHADER_glContext;
typedef struct MOJOSHADER_glShader MOJOSHADER_glShader;
typedef struct MOJOSHADER_glProgram MOJOSHADER_glProgram;
Apr 30, 2008
Apr 30, 2008
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* Determine the best profile to use for the current system.
*
* You do not need to call this if all you want is MOJOSHADER_parse().
*
* You can only call this AFTER you have successfully built your GL context
* and made it current. This function will lookup the GL functions it needs
* through the callback you supply. The lookup function is neither stored nor
* used by MojoShader after this function returns, nor are the functions it
* might look up.
*
* Returns the name of the "best" profile on success, NULL if none of the
* available profiles will work on this system. "Best" is a relative term,
* but it generally means the best trade off between feature set and
* performance. The selection algorithm may be arbitrary and complex.
*
* The returned value is an internal static string, and should not be free()'d
* by the caller. If you get a NULL, calling MOJOSHADER_glGetError() might
* shed some light on why.
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*/
const char *MOJOSHADER_glBestProfile(void *(*lookup)(const char *fnname));
Apr 26, 2008
Apr 26, 2008
433
434
435
436
437
438
439
440
/*
* Prepare MojoShader to manage OpenGL shaders.
*
* You do not need to call this if all you want is MOJOSHADER_parse().
*
* You must call this once AFTER you have successfully built your GL context
* and made it current. This function will lookup the GL functions it needs
* through the callback you supply, after which it may call them at any time
Apr 28, 2008
Apr 28, 2008
441
442
* up until you call MOJOSHADER_glDestroyContext(). The lookup function is
* neither stored nor used by MojoShader after this function returns.
Apr 26, 2008
Apr 26, 2008
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
*
* (profile) is an OpenGL-specific MojoShader profile, which decides how
* Direct3D bytecode shaders get turned into OpenGL programs, and how they
* are fed to the GL.
*
* (lookup) is a callback that is used to load GL entry points. This callback
* has to look up base GL functions and extension entry points.
*
* As MojoShader requires some memory to be allocated, you may provide a
* custom allocator to this function, which will be used to allocate/free
* memory. They function just like malloc() and free(). We do not use
* realloc(). If you don't care, pass NULL in for the allocator functions.
* If your allocator needs instance-specific data, you may supply it with the
* (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
*
Apr 28, 2008
Apr 28, 2008
458
459
460
* Returns a new context on success, NULL on error. If you get a new context,
* you need to make it current before using it with
* MOJOSHADER_glMakeContextCurrent().
Apr 26, 2008
Apr 26, 2008
461
462
463
464
465
466
*
* This call is NOT thread safe! It must return success before you may call
* any other MOJOSHADER_gl* function. Also, as most OpenGL implementations
* are not thread safe, you should probably only call this from the same
* thread that created the GL context.
*/
Apr 28, 2008
Apr 28, 2008
467
MOJOSHADER_glContext *MOJOSHADER_glCreateContext(const char *profile,
Apr 28, 2008
Apr 28, 2008
468
469
470
471
472
473
void *(*lookup)(const char *fnname),
MOJOSHADER_malloc m, MOJOSHADER_free f,
void *d);
/*
* You must call this before using the context that you got from
Apr 28, 2008
Apr 28, 2008
474
475
* MOJOSHADER_glCreateContext(), and must use it when you switch to a new GL
* context.
Apr 28, 2008
Apr 28, 2008
476
477
478
479
480
481
482
483
*
* You can only have one MOJOSHADER_glContext per actual GL context, or
* undefined behaviour will result.
*
* It is legal to call this with a NULL pointer to make no context current,
* but you need a valid context to be current to use most of MojoShader.
*/
void MOJOSHADER_glMakeContextCurrent(MOJOSHADER_glContext *ctx);
Apr 26, 2008
Apr 26, 2008
484
Apr 27, 2008
Apr 27, 2008
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
* Get any error state we might have picked up. MojoShader will NOT call
* glGetError() internally, but there are other errors we can pick up,
* such as failed shader compilation, etc.
*
* Returns a human-readable string. This string is for debugging purposes, and
* not guaranteed to be localized, coherent, or user-friendly in any way.
* It's for programmers!
*
* The latest error may remain between calls. New errors replace any existing
* error. Don't check this string for a sign that an error happened, check
* return codes instead and use this for explanation when debugging.
*
* Do not free the returned string: it's a pointer to a static internal
* buffer. Do not keep the pointer around, either, as it's likely to become
* invalid as soon as you call into MojoShader again.
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 26, 2008
Apr 26, 2008
505
*
Apr 28, 2008
Apr 28, 2008
506
507
* This call does NOT require a valid MOJOSHADER_glContext to have been made
* current. The error buffer is shared between contexts, so you can get
Apr 28, 2008
Apr 28, 2008
508
* error results from a failed MOJOSHADER_glCreateContext().
Apr 26, 2008
Apr 26, 2008
509
*/
Apr 28, 2008
Apr 28, 2008
510
const char *MOJOSHADER_glGetError(void);
Apr 26, 2008
Apr 26, 2008
511
May 3, 2008
May 3, 2008
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* Get the maximum uniforms a shader can support for the current GL context,
* MojoShader profile, and shader type. You can use this to make decisions
* about what shaders you want to use (for example, a less complicated
* shader may be swapped in for lower-end systems).
*
* Returns the number, or -1 on error.
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*/
int MOJOSHADER_glMaxUniforms(MOJOSHADER_shaderType shader_type);
Apr 26, 2008
Apr 26, 2008
529
530
531
532
533
534
535
536
/*
* Compile a buffer of Direct3D shader bytecode into an OpenGL shader.
* You still need to link the shader before you may render with it.
*
* (tokenbuf) is a buffer of Direct3D shader bytecode.
* (bufsize) is the size, in bytes, of the bytecode buffer.
*
* Returns NULL on error, or a shader handle on success.
Apr 27, 2008
Apr 27, 2008
537
538
539
540
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 28, 2008
Apr 28, 2008
541
542
543
544
545
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*
* Compiled shaders from this function may not be shared between contexts.
Apr 26, 2008
Apr 26, 2008
546
*/
Apr 26, 2008
Apr 26, 2008
547
548
MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
const unsigned int bufsize);
Apr 26, 2008
Apr 26, 2008
549
Apr 29, 2008
Apr 29, 2008
550
551
552
553
554
555
556
557
558
559
/*
* Get the MOJOSHADER_parseData structure that was produced from the
* call to MOJOSHADER_glCompileShader().
*
* This data is read-only, and you should NOT attempt to free it. This
* pointer remains valid until the shader is deleted.
*/
const MOJOSHADER_parseData *MOJOSHADER_glGetShaderParseData(
MOJOSHADER_glShader *shader);
Apr 26, 2008
Apr 26, 2008
560
/*
Apr 26, 2008
Apr 26, 2008
561
562
563
564
565
* Link a vertex and pixel shader into an OpenGL program.
* (vshader) or (pshader) can be NULL, to specify that the GL should use the
* fixed-function pipeline instead of the programmable pipeline for that
* portion of the work. You can reuse shaders in various combinations across
* multiple programs, by relinking different pairs.
Apr 26, 2008
Apr 26, 2008
566
*
Apr 26, 2008
Apr 26, 2008
567
568
* It is illegal to give a vertex shader for (pshader) or a pixel shader
* for (vshader).
Apr 26, 2008
Apr 26, 2008
569
570
571
572
*
* Once you have successfully linked a program, you may render with it.
*
* Returns NULL on error, or a program handle on success.
Apr 27, 2008
Apr 27, 2008
573
574
575
576
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 28, 2008
Apr 28, 2008
577
578
579
580
581
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*
* Linked programs from this function may not be shared between contexts.
Apr 26, 2008
Apr 26, 2008
582
*/
Apr 26, 2008
Apr 26, 2008
583
584
MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
MOJOSHADER_glShader *pshader);
Apr 26, 2008
Apr 26, 2008
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
* This binds the program (using, for example, glUseProgramObjectARB()), and
* disables all the client-side arrays so we can reset them with new values
* if appropriate.
*
* Call with NULL to disable the programmable pipeline and all enabled
* client-side arrays.
*
* After binding a program, you should update any uniforms you care about
* with MOJOSHADER_glSetVertexShaderUniformF() (etc), set any vertex arrays
* you want to use with MOJOSHADER_glSetVertexAttribute(), and finally call
* MOJOSHADER_glProgramReady() to commit everything to the GL. Then you may
* begin drawing through standard GL entry points.
Apr 27, 2008
Apr 27, 2008
599
600
601
602
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 28, 2008
Apr 28, 2008
603
604
605
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
Apr 26, 2008
Apr 26, 2008
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
*/
void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program);
/*
* Set a floating-point uniform value (what Direct3D calls a "constant").
*
* There is a single array of 4-float "registers" shared by all vertex shaders.
* This is the "c" register file in Direct3D (c0, c1, c2, etc...)
* MojoShader will take care of synchronizing this internal array with the
* appropriate variables in the GL shaders.
*
* (idx) is the index into the internal array: 0 is the first four floats,
* 1 is the next four, etc.
* (data) is a pointer to (vec4count*4) floats.
Apr 27, 2008
Apr 27, 2008
621
622
623
624
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 28, 2008
Apr 28, 2008
625
626
627
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
Apr 28, 2008
Apr 28, 2008
628
629
*
* Uniforms are not shared between contexts.
Apr 26, 2008
Apr 26, 2008
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
*/
void MOJOSHADER_glSetVertexShaderUniformF(unsigned int idx, const float *data,
unsigned int vec4count);
/*
* Set an integer uniform value (what Direct3D calls a "constant").
*
* There is a single array of 4-int "registers" shared by all vertex shaders.
* This is the "i" register file in Direct3D (i0, i1, i2, etc...)
* MojoShader will take care of synchronizing this internal array with the
* appropriate variables in the GL shaders.
*
* (idx) is the index into the internal array: 0 is the first four ints,
* 1 is the next four, etc.
* (data) is a pointer to (ivec4count*4) ints.
Apr 27, 2008
Apr 27, 2008
645
646
647
648
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 28, 2008
Apr 28, 2008
649
650
651
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
Apr 28, 2008
Apr 28, 2008
652
653
*
* Uniforms are not shared between contexts.
Apr 26, 2008
Apr 26, 2008
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
*/
void MOJOSHADER_glSetVertexShaderUniformI(unsigned int idx, const int *data,
unsigned int ivec4count);
/*
* Set a boolean uniform value (what Direct3D calls a "constant").
*
* There is a single array of "registers" shared by all vertex shaders.
* This is the "b" register file in Direct3D (b0, b1, b2, etc...)
* MojoShader will take care of synchronizing this internal array with the
* appropriate variables in the GL shaders.
*
* Unlike the float and int counterparts, booleans are single values, not
* four-element vectors...so idx==1 is the second boolean in the internal
* array, not the fifth.
*
* Non-zero values are considered "true" and zero is considered "false".
*
* (idx) is the index into the internal array.
* (data) is a pointer to (bcount) ints.
Apr 27, 2008
Apr 27, 2008
674
675
676
677
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 28, 2008
Apr 28, 2008
678
679
680
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
Apr 28, 2008
Apr 28, 2008
681
682
*
* Uniforms are not shared between contexts.
Apr 26, 2008
Apr 26, 2008
683
684
685
686
*/
void MOJOSHADER_glSetVertexShaderUniformB(unsigned int idx, const int *data,
unsigned int bcount);
Apr 26, 2008
Apr 26, 2008
687
688
689
690
/*
* The equivalent of MOJOSHADER_glSetVertexShaderUniformF() for pixel
* shaders. Other than using a different internal array that is specific
* to pixel shaders, this functions just like its vertex array equivalent.
Apr 27, 2008
Apr 27, 2008
691
692
693
694
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 28, 2008
Apr 28, 2008
695
696
697
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
Apr 28, 2008
Apr 28, 2008
698
699
*
* Uniforms are not shared between contexts.
Apr 26, 2008
Apr 26, 2008
700
701
702
703
704
705
706
707
*/
void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
unsigned int vec4count);
/*
* The equivalent of MOJOSHADER_glSetVertexShaderUniformI() for pixel
* shaders. Other than using a different internal array that is specific
* to pixel shaders, this functions just like its vertex array equivalent.
Apr 27, 2008
Apr 27, 2008
708
709
710
711
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 28, 2008
Apr 28, 2008
712
713
714
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
Apr 28, 2008
Apr 28, 2008
715
716
*
* Uniforms are not shared between contexts.
Apr 26, 2008
Apr 26, 2008
717
718
719
720
721
722
723
724
*/
void MOJOSHADER_glSetPixelShaderUniformI(unsigned int idx, const int *data,
unsigned int ivec4count);
/*
* The equivalent of MOJOSHADER_glSetVertexShaderUniformB() for pixel
* shaders. Other than using a different internal array that is specific
* to pixel shaders, this functions just like its vertex array equivalent.
Apr 27, 2008
Apr 27, 2008
725
726
727
728
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 28, 2008
Apr 28, 2008
729
730
731
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
Apr 28, 2008
Apr 28, 2008
732
733
*
* Uniforms are not shared between contexts.
Apr 26, 2008
Apr 26, 2008
734
735
736
*/
void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
unsigned int bcount);
Apr 26, 2008
Apr 26, 2008
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/*
* Connect a client-side array to the currently-bound program.
*
* (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
* be MOJOSHADER_USAGE_COLOR and 1.
*
* The caller should bind VBOs before this call and treat (ptr) as an offset,
* if appropriate.
*
* MojoShader will figure out where to plug this stream into the
* currently-bound program, and enable the appropriate client-side array.
*
* (size), (type), (normalized), (stride), and (ptr) correspond to
* glVertexAttribPointer()'s parameters (in most cases, these get passed
* unmolested to that very entry point during this function).
Apr 27, 2008
Apr 27, 2008
753
754
755
756
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 28, 2008
Apr 28, 2008
757
758
759
760
761
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*
* Vertex attributes are not shared between contexts.
Apr 26, 2008
Apr 26, 2008
762
763
764
765
766
767
768
*/
void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
int index, unsigned int size,
MOJOSHADER_attributeType type,
int normalized, unsigned int stride,
const void *ptr);
May 6, 2008
May 6, 2008
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/*
* Set a sampler.
*
* There is a single array of samplers shared by all shaders.
* This is the "s" register file in Direct3D (s0, s1, s2, etc...), also
* known as the "sampler stages." MojoShader will take care of synchronizing
* this internal array with the appropriate variables in the GL shaders.
*
* Unlike the float and int counterparts, samplers are single values, not
* four-element vectors...so idx==1 is the second sampler in the internal
* array, not the fifth.
*
* (sampler) is the index into the internal array.
* (unit) is an OpenGL TEXTURE UNIT (not a texture name!). This is the
* unit's index, not the GLenum. So if you want GL_TEXTURE_0, use 0.
*
* Bind the texture unit, set state, call this to assign it to the internal
* array, where it will propagate to the shader during
* MOJOSHADER_glProgramReady().
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*
* Samplers are not shared between contexts.
*/
void MOJOSHADER_glSetSampler(unsigned int sampler, unsigned int unit);
Apr 26, 2008
Apr 26, 2008
800
801
802
803
804
/*
* Inform MojoShader that it should commit any pending state to the GL. This
* must be called after you bind a program and update any inputs, right
* before you start drawing, so any outstanding changes made to the shared
* constants array (etc) can propagate to the shader during this call.
Apr 27, 2008
Apr 27, 2008
805
806
807
808
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 28, 2008
Apr 28, 2008
809
810
811
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
Apr 26, 2008
Apr 26, 2008
812
813
814
815
816
817
818
*/
void MOJOSHADER_glProgramReady(void);
/*
* Free the resources of a linked program. This will delete the GL object
* and free memory.
*
Apr 26, 2008
Apr 26, 2008
819
820
* If the program is currently bound by MOJOSHADER_glBindProgram(), it will
* be deleted as soon as it becomes unbound.
Apr 27, 2008
Apr 27, 2008
821
822
823
824
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 28, 2008
Apr 28, 2008
825
826
827
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
Apr 26, 2008
Apr 26, 2008
828
*/
Apr 26, 2008
Apr 26, 2008
829
void MOJOSHADER_glDeleteProgram(MOJOSHADER_glProgram *program);
Apr 26, 2008
Apr 26, 2008
830
831
832
833
834
/*
* Free the resources of a compiled shader. This will delete the GL object
* and free memory.
*
Apr 26, 2008
Apr 26, 2008
835
836
* If the shader is currently referenced by a linked program, it will
* be deleted as soon as all referencing programs are deleted, too.
Apr 27, 2008
Apr 27, 2008
837
838
839
840
*
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
Apr 28, 2008
Apr 28, 2008
841
842
843
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
Apr 26, 2008
Apr 26, 2008
844
*/
Apr 26, 2008
Apr 26, 2008
845
void MOJOSHADER_glDeleteShader(MOJOSHADER_glShader *shader);
Apr 26, 2008
Apr 26, 2008
846
847
848
849
/*
* Deinitialize MojoShader's OpenGL shader management.
*
Apr 28, 2008
Apr 28, 2008
850
851
852
853
* You must call this once, while your GL context (not MojoShader context) is
* still current, if you previously had a successful call to
* MOJOSHADER_glCreateContext(). This should be the last MOJOSHADER_gl*
* function you call until you've prepared a context again.
Apr 26, 2008
Apr 26, 2008
854
855
856
857
858
859
860
*
* This will clean up resources previously allocated, and may call into the GL.
*
* This will not clean up shaders and programs you created! Please call
* MOJOSHADER_glDeleteShader() and MOJOSHADER_glDeleteProgram() to clean
* those up before calling this function!
*
Apr 28, 2008
Apr 28, 2008
861
862
863
* This function destroys the MOJOSHADER_glContext you pass it. If it's the
* current context, then no context will be current upon return.
*
Apr 26, 2008
Apr 26, 2008
864
865
866
867
868
* This call is NOT thread safe! There must not be any other MOJOSHADER_gl*
* functions running when this is called. Also, as most OpenGL implementations
* are not thread safe, you should probably only call this from the same
* thread that created the GL context.
*/
Apr 28, 2008
Apr 28, 2008
869
void MOJOSHADER_glDestroyContext(MOJOSHADER_glContext *ctx);
Apr 26, 2008
Apr 26, 2008
870
Feb 10, 2008
Feb 10, 2008
871
872
873
874
875
876
#ifdef __cplusplus
}
#endif
#endif /* include-once blocker. */
Mar 22, 2008
Mar 22, 2008
877
/* end of mojoshader.h ... */