/
mojoshader_opengl.c
601 lines (490 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <assert.h>
#include "mojoshader.h"
// Get basic wankery out of the way here...
12
13
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
14
15
16
17
18
19
20
21
typedef unsigned int uint; // this is a printf() helper. don't use for code.
typedef uint8_t uint8;
typedef uint32_t uint32;
typedef int32_t int32;
struct MOJOSHADER_glShader
{
const MOJOSHADER_parseData *parseData;
22
GLhandleARB handle;
23
24
25
uint32 refcount;
};
26
27
typedef struct
{
28
MOJOSHADER_shaderType shader_type;
29
MOJOSHADER_uniform *uniform;
30
GLuint location;
31
32
} UniformMap;
33
34
35
typedef struct
{
MOJOSHADER_attribute *attribute;
36
GLuint location;
37
38
} AttributeMap;
39
40
41
42
struct MOJOSHADER_glProgram
{
const MOJOSHADER_glShader *vertex;
const MOJOSHADER_glShader *fragment;
43
GLhandleARB handle;
44
45
uint32 uniform_count;
UniformMap uniforms;
46
47
uint32 attribute_count;
AttributeMap attributes;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
uint32 refcount;
};
// Allocators...
static MOJOSHADER_malloc malloc_fn = NULL;
static MOJOSHADER_free free_fn = NULL;
static void *malloc_data = NULL;
// The constant register files...
// Man, it kills me how much memory this takes...
static float vs_register_file_f[8192 * 4];
static int vs_register_file_i[2047 * 4];
static uint8 vs_register_file_b[2047];
static float ps_register_file_f[8192 * 4];
static int ps_register_file_i[2047 * 4];
static uint8 ps_register_file_b[2047];
// GL stuff...
static MOJOSHADER_glProgram *bound_program = NULL;
68
69
70
71
72
73
74
75
// Error state...
static char error_buffer[1024] = { '\0' };
static void set_error(const char *str)
{
snprintf(error_buffer, sizeof (error_buffer), "%s", str);
} // set_error
76
77
78
79
80
81
82
83
84
85
86
87
88
// #define this to force app to supply an allocator, so there's no reference
// to the C runtime's malloc() and free()...
#if MOJOSHADER_FORCE_ALLOCATOR
#define internal_malloc NULL
#define internal_free NULL
#else
static void *internal_malloc(int bytes, void *d) { return malloc(bytes); }
static void internal_free(void *ptr, void *d) { free(ptr); }
#endif
static inline void *Malloc(const size_t len)
{
89
90
91
92
void *retval = malloc_fn(len, malloc_data);
if (retval == NULL)
set_error("out of memory");
return retval;
93
94
95
96
} // Malloc
static inline void Free(void *ptr)
{
97
98
if (ptr != NULL)
free_fn(ptr, malloc_data);
99
100
101
} // Free
102
103
104
105
106
107
const char *MOJOSHADER_glGetError(void)
{
return error_buffer;
} // MOJOSHADER_glGetError
108
109
110
111
int MOJOSHADER_glInit(const char *_profile,
void *(*lookup)(const char *fnname),
MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
{
112
113
error_buffer[0] = '\0';
114
115
116
if (strcmp(_profile, MOJOSHADER_PROFILE_GLSL) != 0)
profile = MOJOSHADER_PROFILE_GLSL;
else
117
118
{
set_error("unknown profile");
119
return 0;
120
} // else
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// !!! FIXME: lookup glGetString(), check extensions.
malloc_fn = (m == NULL) ? internal_malloc : m;
free_fn = (f == NULL) ? internal_free : f;
malloc_data = d;
// !!! FIXME: lookup other entry points.
memset(vs_register_file_f, '\0', sizeof (vs_register_file_f));
memset(vs_register_file_i, '\0', sizeof (vs_register_file_i));
memset(vs_register_file_b, '\0', sizeof (vs_register_file_b));
memset(ps_register_file_f, '\0', sizeof (ps_register_file_f));
memset(ps_register_file_i, '\0', sizeof (ps_register_file_i));
memset(ps_register_file_b, '\0', sizeof (ps_register_file_b));
MOJOSHADER_glBindProgram(NULL);
return 1;
} // MOJOSHADER_glInit
MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
const unsigned int bufsize)
{
MOJOSHADER_glShader *retval = NULL;
147
GLhandleARB shader = 0;
148
149
150
151
const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, tokenbuf,
bufsize, malloc_fn,
free_fn, malloc_data);
if (pd->error != NULL)
152
153
{
set_error(pd->error);
154
goto compile_shader_fail;
155
} // if
156
157
158
retval = (MOJOSHADER_glShader *) Malloc(sizeof (MOJOSHADER_glShader));
if (retval == NULL)
159
160
goto compile_shader_fail;
161
162
163
GLint ok = 0;
const GLenum shader_type = (pd->shader_type == MOJOSHADER_TYPE_PIXEL) ? GL_FRAGMENT_SHADER_ARB : GL_VERTEX_SHADER_ARB;
GLint shaderlen = (GLint) pd->output_len;
164
shader = pglCreateShaderObjectARB(shader_type);
165
166
167
168
169
170
171
172
pglShaderSourceARB(shader, 1, (const GLcharARB **) &pd->output, &shaderlen);
pglCompileShaderARB(shader);
pglGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &ok);
if (!ok)
{
GLsizei len = 0;
173
174
pglGetInfoLogARB(shader, sizeof (error_buffer), &len,
(GLcharARB *) error_buffer);
175
goto compile_shader_fail;
176
177
178
179
180
181
} // if
retval->parseData = pd;
retval->handle = shader;
retval->refcount = 1;
return retval;
182
183
184
185
186
187
188
compile_shader_fail:
MOJOSHADER_freeParseData(pd);
Free(retval);
if (shader != 0)
pglDeleteObjectARB(shader);
return NULL;
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
} // MOJOSHADER_glCompileShader
static void shader_unref(MOJOSHADER_glShader *shader)
{
if (shader != NULL)
{
const uint32 refcount = program->refcount;
if (refcount > 1)
program->refcount--;
else
{
pglDeleteObjectARB(shader->handle);
MOJOSHADER_freeParseData(shader->parseData);
Free(shader);
} // else
} // if
} // shader_unref
static void program_unref(MOJOSHADER_glProgram *program)
{
if (program != NULL)
{
const uint32 refcount = program->refcount;
if (refcount > 1)
program->refcount--;
else
{
pglDeleteObjectARB(program->handle);
shader_unref(program->vertex);
shader_unref(program->fragment);
221
Free(program->attributes);
222
Free(program->uniforms);
223
224
225
226
227
228
Free(program);
} // else
} // if
} // program_unref
229
230
231
232
233
234
static void lookup_uniforms(MOJOSHADER_glProgram *program,
MOJOSHADER_glShader *shader)
{
int i;
const MOJOSHADER_parseData *pd = shader->parseData;
const MOJOSHADER_uniforms *u = pd->uniforms;
235
const MOJOSHADER_shaderType shader_type = pd->shader_type;
236
237
238
239
240
241
242
for (i = 0; i < pd->uniform_count; i++)
{
const GLint loc = pglGetUniformLocationARB(program->handle, u[i].name);
if (loc != -1) // maybe the Uniform was optimized out?
{
UniformMap *map = &program->uniforms[program->uniform_count];
243
map->shader_type = shader_type;
244
map->uniform = &u[i];
245
map->location = (GLuint) loc;
246
247
248
249
250
251
program->uniform_count++;
} // if
} // for
} // lookup_uniforms
252
253
254
255
256
257
258
259
260
261
262
263
264
static void lookup_attributes(MOJOSHADER_glProgram *program)
{
int i;
const MOJOSHADER_parseData *pd = program->vertex->parseData;
const MOJOSHADER_attributes *a = pd->attributes;
for (i = 0; i < pd->attribute_count; i++)
{
const GLint loc = pglGetAttribLocationARB(program->handle, a->name);
if (loc != -1) // maybe the Attribute was optimized out?
{
AttributeMap *map = &program->attributes[program->attribute_count];
map->attribute = &a[i];
265
map->location = (GLuint) loc;
266
267
268
269
270
271
program->attribute_count++;
} // if
} // for
} // lookup_attributes
272
273
274
MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
MOJOSHADER_glShader *pshader)
{
275
276
277
if ((vshader == NULL) && (pshader == NULL))
return NULL;
278
MOJOSHADER_glProgram *retval = NULL;
279
const GLhandleARB program = pglCreateProgramObjectARB();
280
281
282
if (vshader != NULL) pglAttachObjectARB(program, vshader->handle);
if (pshader != NULL) pglAttachObjectARB(program, pshader->handle);
283
284
285
286
287
288
289
290
pglLinkProgramARB(program);
GLint ok = 0;
pglGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &ok);
if (!ok)
{
GLsizei len = 0;
291
292
pglGetInfoLogARB(shader, sizeof (error_buffer), &len,
(GLcharARB *) error_buffer);
293
294
goto link_program_fail;
} // if
295
296
297
retval = (MOJOSHADER_glProgram *) Malloc(sizeof (MOJOSHADER_glProgram));
if (retval == NULL)
298
299
goto link_program_fail;
memset(retval, '\0', sizeof (MOJOSHADER_glProgram));
300
301
302
303
int numregs = 0;
if (vshader != NULL) numregs += vshader->parseData->uniform_count;
if (pshader != NULL) numregs += pshader->parseData->uniform_count;
304
305
306
retval->uniforms = (UniformMap *) Malloc(sizeof (UniformMap) * numregs);
if (retval->uniforms == NULL)
goto link_program_fail;
307
memset(retval->uniforms, '\0', sizeof (UniformMap) * numregs);
308
309
310
311
312
retval->handle = program;
retval->vertex = vshader;
retval->fragment = pshader;
retval->refcount = 1;
313
314
if (vshader != NULL)
315
{
316
317
318
319
320
321
retval->attributes = (AttributeMap *) Malloc(sizeof (AttributeMap) *
vshader->parseData->attribute_count);
if (retval->attributes == NULL)
goto link_program_fail;
lookup_attributes(retval);
322
lookup_uniforms(retval, vshader);
323
vshader->refcount++;
324
325
} // if
326
if (pshader != NULL)
327
328
{
lookup_uniforms(retval, pshader);
329
pshader->refcount++;
330
} // if
331
332
return retval;
333
334
335
336
link_program_fail:
if (retval != NULL)
{
337
338
Free(retval->uniforms);
Free(retval->attributes);
339
340
341
Free(retval);
} // if
342
pglDeleteObjectARB(program);
343
return NULL;
344
345
346
347
348
} // MOJOSHADER_glLinkProgram
void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program)
{
349
GLhandleARB handle = 0;
350
351
352
353
354
355
356
357
358
359
360
361
362
363
if (program != NULL)
{
handle = program->handle;
program->refcount++;
} // if
// !!! FIXME: unbind client-side arrays.
pglUseProgramObjectARB(handle);
program_unref(bound_program);
bound_program = program;
} // MOJOSHADER_glBindProgram
364
365
366
367
368
369
static inline uint maxuint(const uint a, const uint b)
{
return ((a > b) ? a : b);
} // maxuint
370
void MOJOSHADER_glSetVertexShaderUniformF(unsigned int idx, const float *data,
371
unsigned int vec4n)
372
{
373
374
375
376
377
378
const uint maxregs = STATICARRAYLEN(vs_register_file_f) / 4;
if (idx < maxregs)
{
const uint cpy = maxuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
memcpy(vs_register_file_f + (idx * 4), data, cpy);
} // if
379
380
381
382
} // MOJOSHADER_glSetVertexShaderUniformF
void MOJOSHADER_glSetVertexShaderUniformI(unsigned int idx, const int *data,
383
unsigned int ivec4n)
384
{
385
386
387
388
389
390
const uint maxregs = STATICARRAYLEN(vs_register_file_i) / 4;
if (idx < maxregs)
{
const uint cpy = maxuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
memcpy(vs_register_file_i + (idx * 4), data, cpy);
} // if
391
392
393
394
395
396
} // MOJOSHADER_glSetVertexShaderUniformI
void MOJOSHADER_glSetVertexShaderUniformB(unsigned int idx, const int *data,
unsigned int bcount)
{
397
398
399
400
401
402
403
404
const uint maxregs = STATICARRAYLEN(vs_register_file_f) / 4;
if (idx < maxregs)
{
uint8 *wptr = vs_register_file_b + idx;
uint8 *endptr = wptr + maxuint(maxregs - idx, bcount);
while (wptr != endptr)
*(wptr++) = *(data++) ? 1 : 0;
} // if
405
406
407
408
} // MOJOSHADER_glSetVertexShaderUniformB
void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
409
unsigned int vec4n)
410
{
411
412
413
414
415
416
const uint maxregs = STATICARRAYLEN(ps_register_file_f) / 4;
if (idx < maxregs)
{
const uint cpy = maxuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
memcpy(ps_register_file_f + (idx * 4), data, cpy);
} // if
417
418
419
420
} // MOJOSHADER_glSetPixelShaderUniformF
void MOJOSHADER_glSetPixelShaderUniformI(unsigned int idx, const int *data,
421
unsigned int ivec4n)
422
{
423
424
425
426
427
428
const uint maxregs = STATICARRAYLEN(ps_register_file_i) / 4;
if (idx < maxregs)
{
const uint cpy = maxuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
memcpy(ps_register_file_i + (idx * 4), data, cpy);
} // if
429
430
431
432
} // MOJOSHADER_glSetPixelShaderUniformI
void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
433
unsigned int bcount)
434
{
435
436
437
438
439
440
441
442
const uint maxregs = STATICARRAYLEN(ps_register_file_f) / 4;
if (idx < maxregs)
{
uint8 *wptr = ps_register_file_b + idx;
uint8 *endptr = wptr + maxuint(maxregs - idx, bcount);
while (wptr != endptr)
*(wptr++) = *(data++) ? 1 : 0;
} // if
443
444
445
} // MOJOSHADER_glSetPixelShaderUniformB
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
static inline GLenum opengl_posattr_type(const MOJOSHADER_attributeType type)
{
// we don't ever use the glVertexPointer() stream, so we just need to
// make the data types reasonably match, so the GL doesn't overrun
// the buffer when dereferencing it.
switch (type)
{
case MOJOSHADER_ATTRIBUTE_BYTE: return GL_NONE; // oh well.
case MOJOSHADER_ATTRIBUTE_UBYTE: return GL_NONE; // oh well.
case MOJOSHADER_ATTRIBUTE_SHORT: return GL_SHORT;
case MOJOSHADER_ATTRIBUTE_USHORT: return GL_SHORT;
case MOJOSHADER_ATTRIBUTE_INT: return GL_INT;
case MOJOSHADER_ATTRIBUTE_UINT: return GL_INT;
case MOJOSHADER_ATTRIBUTE_FLOAT: return GL_FLOAT;
case MOJOSHADER_ATTRIBUTE_DOUBLE: return GL_DOUBLE;
} // switch
return GL_NONE; // oh well. Raises a GL error later.
} // opengl_posattr_type
static inline GLenum opengl_attr_type(const MOJOSHADER_attributeType type)
{
switch (type)
{
case MOJOSHADER_ATTRIBUTE_BYTE: return GL_BYTE;
case MOJOSHADER_ATTRIBUTE_UBYTE: return GL_UNSIGNED_BYTE;
case MOJOSHADER_ATTRIBUTE_SHORT: return GL_SHORT;
case MOJOSHADER_ATTRIBUTE_USHORT: return GL_USHORT;
case MOJOSHADER_ATTRIBUTE_INT: return GL_INT;
case MOJOSHADER_ATTRIBUTE_UINT: return GL_UNSIGNED_INT;
case MOJOSHADER_ATTRIBUTE_FLOAT: return GL_FLOAT;
case MOJOSHADER_ATTRIBUTE_DOUBLE: return GL_DOUBLE;
} // switch
return GL_NONE; // oh well. Raises a GL error later.
} // opengl_attr_type
486
487
488
489
490
491
void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
int index, unsigned int size,
MOJOSHADER_attributeType type,
int normalized, unsigned int stride,
const void *ptr)
{
492
if ((bound_program == NULL) || (bound_program->vertex == NULL))
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
return;
// Since glVertexPointer() lacks the flexibility that we can get from
// glVertexAttribPointer(), we set POSITION0 to a generic vertex
// attribute, and the shaders we generate know to look there instead of
// GLSL's gl_Position global variable. But to keep the GL handy, we
// also set the best possible equivalent with glVertexPointer(). Messy.
if ((usage == MOJOSHADER_USAGE_POSITION) && (index == 0))
{
// !!! FIXME: fails if size==1.
pglVertexPointer(size, opengl_posattr_type(type), stride, ptr);
} // if
int i;
GLuint gl_index = 0;
for (i = 0; i < bound_program->attribute_count; i++)
{
const AttributeMap *map = &bound_program->attributes[i];
const MOJOSHADER_attribute *a = map->attribute;
// !!! FIXME: is this array guaranteed to be sorted by usage?
// !!! FIXME: if so, we can break out of the loop if a->usage > usage.
if ((a->usage == usage) && (a->index == index))
{
gl_index = map->location;
break;
} // if
} // for
if (gl_index != 0)
{
const GLenum gl_type = opengl_attr_type(type);
const GLboolean gl_norm = (normalized) ? GL_TRUE : GL_FALSE;
pglVertexAttribPointer(gl_index, size, gl_type, gl_norm, stride, ptr);
} // if
530
531
532
533
534
} // MOJOSHADER_glSetVertexAttribute
void MOJOSHADER_glProgramReady(void)
{
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
int i;
if (bound_program == NULL)
return; // nothing to do.
// !!! FIXME: don't push Uniforms if we know they haven't changed.
// push Uniforms to the program from our register files...
for (i = 0; i < bound_program->uniform_count; i++)
{
const UniformMap *map = &bound_program->uniforms[i];
const MOJOSHADER_uniform *u = map->uniform;
const MOJOSHADER_uniformType type = u->type;
const MOJOSHADER_shaderType shader_type = map->shader_type;
const int index = u->index;
const GLint location = map->location;
if (shader_type == MOJOSHADER_TYPE_VERTEX)
{
if (type == MOJOSHADER_UNIFORM_FLOAT)
pglUniform4fvARB(location, 1, &vs_register_file_f[index * 4]);
else if (type == MOJOSHADER_UNIFORM_INT)
pglUniform4ivARB(location, 1, &vs_register_file_i[index * 4]);
else if (type == MOJOSHADER_UNIFORM_BOOL)
pglUniform1iARB(location, vs_register_file_b[index]);
} // if
else if (shader_type == MOJOSHADER_TYPE_PIXEL)
{
if (type == MOJOSHADER_UNIFORM_FLOAT)
pglUniform4fvARB(location, 1, &ps_register_file_f[index * 4]);
else if (type == MOJOSHADER_UNIFORM_INT)
pglUniform4ivARB(location, 1, &ps_register_file_i[index * 4]);
else if (type == MOJOSHADER_UNIFORM_BOOL)
pglUniform1iARB(location, ps_register_file_b[index]);
} // else if
} // for
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
} // MOJOSHADER_glProgramReady
void MOJOSHADER_glDeleteProgram(const MOJOSHADER_glProgram *program)
{
program_unref(shader);
} // MOJOSHADER_glDeleteProgram
void MOJOSHADER_glDeleteShader(const MOJOSHADER_glShader *shader)
{
shader_unref(shader);
} // MOJOSHADER_glDeleteShader
void MOJOSHADER_glDeinit(void)
{
MOJOSHADER_glBindProgram(NULL);
profile = NULL;
malloc_fn = NULL;
free_fn = NULL;
malloc_data = NULL;
595
error_buffer[0] = '\0';
596
597
598
599
600
// !!! FIXME: NULL entry points.
} // MOJOSHADER_glDeinit
// end of mojoshader_opengl.c ...