Skip to content

Latest commit

 

History

History
622 lines (508 loc) · 18.6 KB

mojoshader_opengl.c

File metadata and controls

622 lines (508 loc) · 18.6 KB
 
Apr 26, 2008
Apr 26, 2008
1
2
3
4
5
6
7
8
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <assert.h>
#include "mojoshader.h"
Apr 27, 2008
Apr 27, 2008
9
10
11
#define GL_GLEXT_LEGACY 1
#include "gl.h"
#include "glext.h"
Apr 26, 2008
Apr 26, 2008
12
13
14
// Get basic wankery out of the way here...
Apr 27, 2008
Apr 27, 2008
15
16
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
Apr 26, 2008
Apr 26, 2008
17
18
19
20
21
22
23
24
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;
Apr 27, 2008
Apr 27, 2008
25
GLhandleARB handle;
Apr 26, 2008
Apr 26, 2008
26
27
28
uint32 refcount;
};
Apr 27, 2008
Apr 27, 2008
29
30
typedef struct
{
Apr 27, 2008
Apr 27, 2008
31
MOJOSHADER_shaderType shader_type;
Apr 27, 2008
Apr 27, 2008
32
const MOJOSHADER_uniform *uniform;
Apr 27, 2008
Apr 27, 2008
33
GLuint location;
Apr 27, 2008
Apr 27, 2008
34
35
} UniformMap;
Apr 27, 2008
Apr 27, 2008
36
37
typedef struct
{
Apr 27, 2008
Apr 27, 2008
38
const MOJOSHADER_attribute *attribute;
Apr 27, 2008
Apr 27, 2008
39
GLuint location;
Apr 27, 2008
Apr 27, 2008
40
41
} AttributeMap;
Apr 26, 2008
Apr 26, 2008
42
43
struct MOJOSHADER_glProgram
{
Apr 27, 2008
Apr 27, 2008
44
45
MOJOSHADER_glShader *vertex;
MOJOSHADER_glShader *fragment;
Apr 27, 2008
Apr 27, 2008
46
GLhandleARB handle;
Apr 27, 2008
Apr 27, 2008
47
uint32 uniform_count;
Apr 27, 2008
Apr 27, 2008
48
UniformMap *uniforms;
Apr 27, 2008
Apr 27, 2008
49
uint32 attribute_count;
Apr 27, 2008
Apr 27, 2008
50
AttributeMap *attributes;
Apr 26, 2008
Apr 26, 2008
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
Apr 27, 2008
Apr 27, 2008
70
71
static const char *profile = NULL;
Apr 26, 2008
Apr 26, 2008
72
Apr 27, 2008
Apr 27, 2008
73
74
75
76
77
78
79
80
// 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
Apr 26, 2008
Apr 26, 2008
81
82
83
84
85
86
87
88
89
90
91
92
93
// #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)
{
Apr 27, 2008
Apr 27, 2008
94
95
96
97
void *retval = malloc_fn(len, malloc_data);
if (retval == NULL)
set_error("out of memory");
return retval;
Apr 26, 2008
Apr 26, 2008
98
99
100
101
} // Malloc
static inline void Free(void *ptr)
{
Apr 27, 2008
Apr 27, 2008
102
103
if (ptr != NULL)
free_fn(ptr, malloc_data);
Apr 26, 2008
Apr 26, 2008
104
105
106
} // Free
Apr 27, 2008
Apr 27, 2008
107
108
109
110
111
112
const char *MOJOSHADER_glGetError(void)
{
return error_buffer;
} // MOJOSHADER_glGetError
Apr 26, 2008
Apr 26, 2008
113
114
115
116
int MOJOSHADER_glInit(const char *_profile,
void *(*lookup)(const char *fnname),
MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
{
Apr 27, 2008
Apr 27, 2008
117
118
error_buffer[0] = '\0';
Apr 26, 2008
Apr 26, 2008
119
120
121
if (strcmp(_profile, MOJOSHADER_PROFILE_GLSL) != 0)
profile = MOJOSHADER_PROFILE_GLSL;
else
Apr 27, 2008
Apr 27, 2008
122
123
{
set_error("unknown profile");
Apr 26, 2008
Apr 26, 2008
124
return 0;
Apr 27, 2008
Apr 27, 2008
125
} // else
Apr 26, 2008
Apr 26, 2008
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// !!! 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;
Apr 27, 2008
Apr 27, 2008
152
GLhandleARB shader = 0;
Apr 26, 2008
Apr 26, 2008
153
154
155
156
const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, tokenbuf,
bufsize, malloc_fn,
free_fn, malloc_data);
if (pd->error != NULL)
Apr 27, 2008
Apr 27, 2008
157
158
{
set_error(pd->error);
Apr 27, 2008
Apr 27, 2008
159
goto compile_shader_fail;
Apr 27, 2008
Apr 27, 2008
160
} // if
Apr 26, 2008
Apr 26, 2008
161
162
163
retval = (MOJOSHADER_glShader *) Malloc(sizeof (MOJOSHADER_glShader));
if (retval == NULL)
Apr 27, 2008
Apr 27, 2008
164
165
goto compile_shader_fail;
Apr 26, 2008
Apr 26, 2008
166
167
168
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;
Apr 27, 2008
Apr 27, 2008
169
shader = pglCreateShaderObjectARB(shader_type);
Apr 26, 2008
Apr 26, 2008
170
171
172
173
174
175
176
177
pglShaderSourceARB(shader, 1, (const GLcharARB **) &pd->output, &shaderlen);
pglCompileShaderARB(shader);
pglGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &ok);
if (!ok)
{
GLsizei len = 0;
Apr 27, 2008
Apr 27, 2008
178
179
pglGetInfoLogARB(shader, sizeof (error_buffer), &len,
(GLcharARB *) error_buffer);
Apr 27, 2008
Apr 27, 2008
180
goto compile_shader_fail;
Apr 26, 2008
Apr 26, 2008
181
182
183
184
185
186
} // if
retval->parseData = pd;
retval->handle = shader;
retval->refcount = 1;
return retval;
Apr 27, 2008
Apr 27, 2008
187
188
189
190
191
192
193
compile_shader_fail:
MOJOSHADER_freeParseData(pd);
Free(retval);
if (shader != 0)
pglDeleteObjectARB(shader);
return NULL;
Apr 26, 2008
Apr 26, 2008
194
195
196
197
198
199
200
} // MOJOSHADER_glCompileShader
static void shader_unref(MOJOSHADER_glShader *shader)
{
if (shader != NULL)
{
Apr 27, 2008
Apr 27, 2008
201
const uint32 refcount = shader->refcount;
Apr 26, 2008
Apr 26, 2008
202
if (refcount > 1)
Apr 27, 2008
Apr 27, 2008
203
shader->refcount--;
Apr 26, 2008
Apr 26, 2008
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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);
Apr 27, 2008
Apr 27, 2008
226
Free(program->attributes);
Apr 27, 2008
Apr 27, 2008
227
Free(program->uniforms);
Apr 26, 2008
Apr 26, 2008
228
229
230
231
232
233
Free(program);
} // else
} // if
} // program_unref
Apr 27, 2008
Apr 27, 2008
234
235
236
237
238
static void lookup_uniforms(MOJOSHADER_glProgram *program,
MOJOSHADER_glShader *shader)
{
int i;
const MOJOSHADER_parseData *pd = shader->parseData;
Apr 27, 2008
Apr 27, 2008
239
const MOJOSHADER_uniform *u = pd->uniforms;
Apr 27, 2008
Apr 27, 2008
240
const MOJOSHADER_shaderType shader_type = pd->shader_type;
Apr 27, 2008
Apr 27, 2008
241
242
243
244
245
246
247
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];
Apr 27, 2008
Apr 27, 2008
248
map->shader_type = shader_type;
Apr 27, 2008
Apr 27, 2008
249
map->uniform = &u[i];
Apr 27, 2008
Apr 27, 2008
250
map->location = (GLuint) loc;
Apr 27, 2008
Apr 27, 2008
251
252
253
254
255
256
program->uniform_count++;
} // if
} // for
} // lookup_uniforms
Apr 27, 2008
Apr 27, 2008
257
258
259
260
static void lookup_attributes(MOJOSHADER_glProgram *program)
{
int i;
const MOJOSHADER_parseData *pd = program->vertex->parseData;
Apr 27, 2008
Apr 27, 2008
261
const MOJOSHADER_attribute *a = pd->attributes;
Apr 27, 2008
Apr 27, 2008
262
263
264
265
266
267
268
269
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];
Apr 27, 2008
Apr 27, 2008
270
map->location = (GLuint) loc;
Apr 27, 2008
Apr 27, 2008
271
272
273
274
275
276
program->attribute_count++;
} // if
} // for
} // lookup_attributes
Apr 26, 2008
Apr 26, 2008
277
278
279
MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
MOJOSHADER_glShader *pshader)
{
Apr 27, 2008
Apr 27, 2008
280
281
282
if ((vshader == NULL) && (pshader == NULL))
return NULL;
Apr 27, 2008
Apr 27, 2008
283
MOJOSHADER_glProgram *retval = NULL;
Apr 27, 2008
Apr 27, 2008
284
const GLhandleARB program = pglCreateProgramObjectARB();
Apr 26, 2008
Apr 26, 2008
285
Apr 27, 2008
Apr 27, 2008
286
287
if (vshader != NULL) pglAttachObjectARB(program, vshader->handle);
if (pshader != NULL) pglAttachObjectARB(program, pshader->handle);
Apr 27, 2008
Apr 27, 2008
288
Apr 27, 2008
Apr 27, 2008
289
290
291
292
293
294
295
pglLinkProgramARB(program);
GLint ok = 0;
pglGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &ok);
if (!ok)
{
GLsizei len = 0;
Apr 27, 2008
Apr 27, 2008
296
pglGetInfoLogARB(program, sizeof (error_buffer), &len,
Apr 27, 2008
Apr 27, 2008
297
(GLcharARB *) error_buffer);
Apr 27, 2008
Apr 27, 2008
298
299
goto link_program_fail;
} // if
Apr 26, 2008
Apr 26, 2008
300
Apr 27, 2008
Apr 27, 2008
301
302
retval = (MOJOSHADER_glProgram *) Malloc(sizeof (MOJOSHADER_glProgram));
if (retval == NULL)
Apr 27, 2008
Apr 27, 2008
303
304
goto link_program_fail;
memset(retval, '\0', sizeof (MOJOSHADER_glProgram));
Apr 27, 2008
Apr 27, 2008
305
306
307
308
int numregs = 0;
if (vshader != NULL) numregs += vshader->parseData->uniform_count;
if (pshader != NULL) numregs += pshader->parseData->uniform_count;
Apr 27, 2008
Apr 27, 2008
309
310
311
retval->uniforms = (UniformMap *) Malloc(sizeof (UniformMap) * numregs);
if (retval->uniforms == NULL)
goto link_program_fail;
Apr 27, 2008
Apr 27, 2008
312
memset(retval->uniforms, '\0', sizeof (UniformMap) * numregs);
Apr 27, 2008
Apr 27, 2008
313
314
315
316
317
retval->handle = program;
retval->vertex = vshader;
retval->fragment = pshader;
retval->refcount = 1;
Apr 26, 2008
Apr 26, 2008
318
319
if (vshader != NULL)
Apr 27, 2008
Apr 27, 2008
320
{
Apr 27, 2008
Apr 27, 2008
321
322
323
324
325
326
retval->attributes = (AttributeMap *) Malloc(sizeof (AttributeMap) *
vshader->parseData->attribute_count);
if (retval->attributes == NULL)
goto link_program_fail;
lookup_attributes(retval);
Apr 27, 2008
Apr 27, 2008
327
lookup_uniforms(retval, vshader);
Apr 26, 2008
Apr 26, 2008
328
vshader->refcount++;
Apr 27, 2008
Apr 27, 2008
329
330
} // if
Apr 26, 2008
Apr 26, 2008
331
if (pshader != NULL)
Apr 27, 2008
Apr 27, 2008
332
333
{
lookup_uniforms(retval, pshader);
Apr 26, 2008
Apr 26, 2008
334
pshader->refcount++;
Apr 27, 2008
Apr 27, 2008
335
} // if
Apr 27, 2008
Apr 27, 2008
336
337
return retval;
Apr 27, 2008
Apr 27, 2008
338
339
340
341
link_program_fail:
if (retval != NULL)
{
Apr 27, 2008
Apr 27, 2008
342
343
Free(retval->uniforms);
Free(retval->attributes);
Apr 27, 2008
Apr 27, 2008
344
345
346
Free(retval);
} // if
Apr 27, 2008
Apr 27, 2008
347
pglDeleteObjectARB(program);
Apr 27, 2008
Apr 27, 2008
348
return NULL;
Apr 26, 2008
Apr 26, 2008
349
350
351
352
353
} // MOJOSHADER_glLinkProgram
void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program)
{
Apr 27, 2008
Apr 27, 2008
354
GLhandleARB handle = 0;
Apr 27, 2008
Apr 27, 2008
355
int i;
Apr 27, 2008
Apr 27, 2008
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
if (program == bound_program)
return; // nothing to do.
// Disable any client-side arrays the current program could have used.
if (bound_program != NULL)
{
pglDisableClientState(GL_VERTEX_ARRAY);
for (i = 0; i < bound_program->attribute_count; i++)
{
const AttributeMap *map = &bound_program->attributes[i];
pglDisableVertexAttribArrayARB(map->location);
} // if
} // for
Apr 26, 2008
Apr 26, 2008
371
372
373
374
375
376
377
378
379
380
381
382
if (program != NULL)
{
handle = program->handle;
program->refcount++;
} // if
pglUseProgramObjectARB(handle);
program_unref(bound_program);
bound_program = program;
} // MOJOSHADER_glBindProgram
Apr 27, 2008
Apr 27, 2008
383
384
385
386
387
388
static inline uint maxuint(const uint a, const uint b)
{
return ((a > b) ? a : b);
} // maxuint
Apr 26, 2008
Apr 26, 2008
389
void MOJOSHADER_glSetVertexShaderUniformF(unsigned int idx, const float *data,
Apr 27, 2008
Apr 27, 2008
390
unsigned int vec4n)
Apr 26, 2008
Apr 26, 2008
391
{
Apr 27, 2008
Apr 27, 2008
392
393
394
const uint maxregs = STATICARRAYLEN(vs_register_file_f) / 4;
if (idx < maxregs)
{
Apr 27, 2008
Apr 27, 2008
395
const uint cpy = (maxuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
Apr 27, 2008
Apr 27, 2008
396
397
memcpy(vs_register_file_f + (idx * 4), data, cpy);
} // if
Apr 26, 2008
Apr 26, 2008
398
399
400
401
} // MOJOSHADER_glSetVertexShaderUniformF
void MOJOSHADER_glSetVertexShaderUniformI(unsigned int idx, const int *data,
Apr 27, 2008
Apr 27, 2008
402
unsigned int ivec4n)
Apr 26, 2008
Apr 26, 2008
403
{
Apr 27, 2008
Apr 27, 2008
404
405
406
const uint maxregs = STATICARRAYLEN(vs_register_file_i) / 4;
if (idx < maxregs)
{
Apr 27, 2008
Apr 27, 2008
407
const uint cpy = (maxuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
Apr 27, 2008
Apr 27, 2008
408
409
memcpy(vs_register_file_i + (idx * 4), data, cpy);
} // if
Apr 26, 2008
Apr 26, 2008
410
411
412
413
414
415
} // MOJOSHADER_glSetVertexShaderUniformI
void MOJOSHADER_glSetVertexShaderUniformB(unsigned int idx, const int *data,
unsigned int bcount)
{
Apr 27, 2008
Apr 27, 2008
416
417
418
419
420
421
422
423
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
Apr 26, 2008
Apr 26, 2008
424
425
426
427
} // MOJOSHADER_glSetVertexShaderUniformB
void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
Apr 27, 2008
Apr 27, 2008
428
unsigned int vec4n)
Apr 26, 2008
Apr 26, 2008
429
{
Apr 27, 2008
Apr 27, 2008
430
431
432
const uint maxregs = STATICARRAYLEN(ps_register_file_f) / 4;
if (idx < maxregs)
{
Apr 27, 2008
Apr 27, 2008
433
const uint cpy = (maxuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
Apr 27, 2008
Apr 27, 2008
434
435
memcpy(ps_register_file_f + (idx * 4), data, cpy);
} // if
Apr 26, 2008
Apr 26, 2008
436
437
438
439
} // MOJOSHADER_glSetPixelShaderUniformF
void MOJOSHADER_glSetPixelShaderUniformI(unsigned int idx, const int *data,
Apr 27, 2008
Apr 27, 2008
440
unsigned int ivec4n)
Apr 26, 2008
Apr 26, 2008
441
{
Apr 27, 2008
Apr 27, 2008
442
443
444
const uint maxregs = STATICARRAYLEN(ps_register_file_i) / 4;
if (idx < maxregs)
{
Apr 27, 2008
Apr 27, 2008
445
const uint cpy = (maxuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
Apr 27, 2008
Apr 27, 2008
446
447
memcpy(ps_register_file_i + (idx * 4), data, cpy);
} // if
Apr 26, 2008
Apr 26, 2008
448
449
450
451
} // MOJOSHADER_glSetPixelShaderUniformI
void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
Apr 27, 2008
Apr 27, 2008
452
unsigned int bcount)
Apr 26, 2008
Apr 26, 2008
453
{
Apr 27, 2008
Apr 27, 2008
454
455
456
457
458
459
460
461
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
Apr 26, 2008
Apr 26, 2008
462
463
464
} // MOJOSHADER_glSetPixelShaderUniformB
Apr 27, 2008
Apr 27, 2008
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
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;
Apr 27, 2008
Apr 27, 2008
494
case MOJOSHADER_ATTRIBUTE_USHORT: return GL_UNSIGNED_SHORT;
Apr 27, 2008
Apr 27, 2008
495
496
497
498
499
500
501
502
503
504
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
Apr 26, 2008
Apr 26, 2008
505
506
507
508
509
510
void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
int index, unsigned int size,
MOJOSHADER_attributeType type,
int normalized, unsigned int stride,
const void *ptr)
{
Apr 27, 2008
Apr 27, 2008
511
if ((bound_program == NULL) || (bound_program->vertex == NULL))
Apr 27, 2008
Apr 27, 2008
512
513
514
515
516
517
518
519
520
521
522
523
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);
Apr 27, 2008
Apr 27, 2008
524
pglEnableClientState(GL_VERTEX_ARRAY);
Apr 27, 2008
Apr 27, 2008
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
} // 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);
Apr 27, 2008
Apr 27, 2008
547
548
549
const GLboolean norm = (normalized) ? GL_TRUE : GL_FALSE;
pglVertexAttribPointerARB(gl_index, size, gl_type, norm, stride, ptr);
pglEnableVertexAttribArrayARB(gl_index);
Apr 27, 2008
Apr 27, 2008
550
} // if
Apr 26, 2008
Apr 26, 2008
551
552
553
554
555
} // MOJOSHADER_glSetVertexAttribute
void MOJOSHADER_glProgramReady(void)
{
Apr 27, 2008
Apr 27, 2008
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
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
Apr 26, 2008
Apr 26, 2008
593
594
595
} // MOJOSHADER_glProgramReady
Apr 27, 2008
Apr 27, 2008
596
void MOJOSHADER_glDeleteProgram(MOJOSHADER_glProgram *program)
Apr 26, 2008
Apr 26, 2008
597
{
Apr 27, 2008
Apr 27, 2008
598
program_unref(program);
Apr 26, 2008
Apr 26, 2008
599
600
601
} // MOJOSHADER_glDeleteProgram
Apr 27, 2008
Apr 27, 2008
602
void MOJOSHADER_glDeleteShader(MOJOSHADER_glShader *shader)
Apr 26, 2008
Apr 26, 2008
603
604
605
606
607
608
609
610
611
612
613
614
615
{
shader_unref(shader);
} // MOJOSHADER_glDeleteShader
void MOJOSHADER_glDeinit(void)
{
MOJOSHADER_glBindProgram(NULL);
profile = NULL;
malloc_fn = NULL;
free_fn = NULL;
malloc_data = NULL;
Apr 27, 2008
Apr 27, 2008
616
error_buffer[0] = '\0';
Apr 26, 2008
Apr 26, 2008
617
618
619
620
621
// !!! FIXME: NULL entry points.
} // MOJOSHADER_glDeinit
// end of mojoshader_opengl.c ...