Debian Codegraph @

mojoshader_opengl.c

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 #ifdef _WIN32
11 #define WIN32_LEAN_AND_MEAN 1
12 #include <windows.h> // GL headers need this for WINGDIAPI definition.
13 #endif
14
15 #if (defined(__APPLE__) && defined(__MACH__))
16 #include "TargetConditionals.h"
17 #if !TARGET_OS_IPHONE && !TARGET_OS_TV
18 #define PLATFORM_MACOSX 1
19 #endif /* !TARGET_OS_IPHONE && !TARGET_OS_TV */
20 #endif /* (defined(__APPLE__) && defined(__MACH__)) */
21
22 #if PLATFORM_MACOSX
23 #include <Carbon/Carbon.h>
24 #endif
25
26 #define __MOJOSHADER_INTERNAL__ 1
27 #include "mojoshader_internal.h"
28
29 #define GL_GLEXT_LEGACY 1
30 #include "GL/gl.h"
31 #include "GL/glext.h"
32
33 #if SUPPORT_PROFILE_GLSPIRV
34 #include "spirv/spirv.h"
35 #endif
36
37 #ifndef GL_HALF_FLOAT_NV
38 #define GL_HALF_FLOAT_NV 0x140B
39 #endif
40
41 #ifndef GL_HALF_FLOAT_ARB
42 #define GL_HALF_FLOAT_ARB 0x140B
43 #endif
44
45 #ifndef GL_HALF_FLOAT_OES
46 #define GL_HALF_FLOAT_OES 0x8D61
47 #endif
48
49 // this happens to be the same value for ARB1 and GLSL.
50 #ifndef GL_PROGRAM_POINT_SIZE
51 #define GL_PROGRAM_POINT_SIZE 0x8642
52 #endif
53
54 // FIXME: ARB_gl_spirv in glext.h? -flibit
55 #ifndef GL_ARB_gl_spirv
56 #define GL_ARB_gl_spirv 1
57 #define GL_SHADER_BINARY_FORMAT_SPIR_V_ARB 0x9551
58 typedef void (APIENTRYP PFNGLSPECIALIZESHADERARBPROC) (
59 GLuint shader,
60 const GLchar* pEntryPoint,
61 GLuint numSpecializationConstants,
62 const GLuint* pConstantIndex,
63 const GLuint* pConstantValue
64 );
65 #endif
66
67 struct MOJOSHADER_glShader
68 {
69 const MOJOSHADER_parseData *parseData;
70 GLuint handle;
71 uint32 refcount;
72 };
73
74 typedef struct
75 {
76 MOJOSHADER_shaderType shader_type;
77 const MOJOSHADER_uniform *uniform;
78 GLint location;
79 } UniformMap;
80
81 typedef struct
82 {
83 const MOJOSHADER_attribute *attribute;
84 GLint location;
85 } AttributeMap;
86
87 struct MOJOSHADER_glProgram
88 {
89 MOJOSHADER_glShader *vertex;
90 MOJOSHADER_glShader *fragment;
91 GLuint handle;
92 uint32 generation;
93 uint32 uniform_count;
94 uint32 texbem_count;
95 UniformMap *uniforms;
96 uint32 attribute_count;
97 AttributeMap *attributes;
98 size_t vs_uniforms_float4_count;
99 GLfloat *vs_uniforms_float4;
100 size_t vs_uniforms_int4_count;
101 GLint *vs_uniforms_int4;
102 size_t vs_uniforms_bool_count;
103 GLint *vs_uniforms_bool;
104 size_t ps_uniforms_float4_count;
105 GLfloat *ps_uniforms_float4;
106 size_t ps_uniforms_int4_count;
107 GLint *ps_uniforms_int4;
108 size_t ps_uniforms_bool_count;
109 GLint *ps_uniforms_bool;
110
111 uint32 refcount;
112
113 int uses_pointsize;
114
115 // According to MSDN...
116 //
117 // n is an optional integer between 0 and the number of resources supported.
118 // For example, POSITION0, TEXCOOR1, etc.
119 //
120 // The input registers consist of 16 four-component floating-point vectors,
121 // designated as v0 through v15.
122 GLint vertex_attrib_loc[MOJOSHADER_USAGE_TOTAL][16];
123
124 // GLSL uses these...location of uniform arrays.
125 GLint vs_float4_loc;
126 GLint vs_int4_loc;
127 GLint vs_bool_loc;
128 GLint ps_float4_loc;
129 GLint ps_int4_loc;
130 GLint ps_bool_loc;
131
132 // Numerous fixes for coordinate system mismatches
133 GLint ps_vpos_flip_loc;
134 int current_vpos_flip[2];
135 #ifdef MOJOSHADER_FLIP_RENDERTARGET
136 GLint vs_flip_loc;
137 int current_flip;
138 #endif
139 };
140
141 #ifndef WINGDIAPI
142 #define WINGDIAPI
143 #endif
144
145 // Entry points in base OpenGL that lack function pointer prototypes...
146 typedef WINGDIAPI void (APIENTRYP PFNGLGETINTEGERVPROC) (GLenum pname, GLint *params);
147 typedef WINGDIAPI const GLubyte * (APIENTRYP PFNGLGETSTRINGPROC) (GLenum name);
148 typedef WINGDIAPI GLenum (APIENTRYP PFNGLGETERRORPROC) (void);
149 typedef WINGDIAPI void (APIENTRYP PFNGLENABLEPROC) (GLenum cap);
150 typedef WINGDIAPI void (APIENTRYP PFNGLDISABLEPROC) (GLenum cap);
151
152 // Max entries for each register file type...
153 #define MAX_REG_FILE_F 8192
154 #define MAX_REG_FILE_I 2047
155 #define MAX_REG_FILE_B 2047
156 #define MAX_TEXBEMS 3 // ps_1_1 allows 4 texture stages, texbem can't use t0.
157
158 struct MOJOSHADER_glContext
159 {
160 // Allocators...
161 MOJOSHADER_malloc malloc_fn;
162 MOJOSHADER_free free_fn;
163 void *malloc_data;
164
165 // The constant register files...
166 // !!! FIXME: Man, it kills me how much memory this takes...
167 // !!! FIXME: ... make this dynamically allocated on demand.
168 GLfloat vs_reg_file_f[MAX_REG_FILE_F * 4];
169 GLint vs_reg_file_i[MAX_REG_FILE_I * 4];
170 uint8 vs_reg_file_b[MAX_REG_FILE_B];
171 GLfloat ps_reg_file_f[MAX_REG_FILE_F * 4];
172 GLint ps_reg_file_i[MAX_REG_FILE_I * 4];
173 uint8 ps_reg_file_b[MAX_REG_FILE_B];
174 GLuint sampler_reg_file[16];
175 GLfloat texbem_state[MAX_TEXBEMS * 6];
176
177 // This increments every time we change the register files.
178 uint32 generation;
179
180 // This keeps track of implicitly linked programs.
181 HashTable *linker_cache;
182
183 // This tells us which vertex attribute arrays we have enabled.
184 GLint max_attrs;
185 uint8 want_attr[32];
186 uint8 have_attr[32];
187
188 // This shadows vertex attribute and divisor states.
189 GLuint attr_divisor[32];
190
191 // rarely used, so we don't touch when we don't have to.
192 int pointsize_enabled;
193
194 // GL stuff...
195 int opengl_major;
196 int opengl_minor;
197 int glsl_major;
198 int glsl_minor;
199 MOJOSHADER_glProgram *bound_program;
200 char profile[16];
201
202 #ifdef MOJOSHADER_XNA4_VERTEX_TEXTURES
203 // Vertex texture sampler offset...
204 int vertex_sampler_offset;
205 #endif
206
207 // Extensions...
208 int have_core_opengl;
209 int have_opengl_2; // different entry points than ARB extensions.
210 int have_opengl_3; // different extension query.
211 int have_opengl_es; // different extension requirements
212 int have_opengl_es3; // different shader synxtax and attributes
213 int have_GL_ARB_vertex_program;
214 int have_GL_ARB_fragment_program;
215 int have_GL_NV_vertex_program2_option;
216 int have_GL_NV_fragment_program2;
217 int have_GL_NV_vertex_program3;
218 int have_GL_NV_gpu_program4;
219 int have_GL_ARB_shader_objects;
220 int have_GL_ARB_vertex_shader;
221 int have_GL_ARB_fragment_shader;
222 int have_GL_ARB_shading_language_100;
223 int have_GL_NV_half_float;
224 int have_GL_ARB_half_float_vertex;
225 int have_GL_OES_vertex_half_float;
226 int have_GL_ARB_instanced_arrays;
227 int have_GL_ARB_ES2_compatibility;
228 int have_GL_ARB_gl_spirv;
229
230 // Entry points...
231 PFNGLGETSTRINGPROC glGetString;
232 PFNGLGETSTRINGIPROC glGetStringi;
233 PFNGLGETERRORPROC glGetError;
234 PFNGLGETINTEGERVPROC glGetIntegerv;
235 PFNGLENABLEPROC glEnable;
236 PFNGLDISABLEPROC glDisable;
237 PFNGLDELETESHADERPROC glDeleteShader;
238 PFNGLDELETEPROGRAMPROC glDeleteProgram;
239 PFNGLATTACHSHADERPROC glAttachShader;
240 PFNGLCOMPILESHADERPROC glCompileShader;
241 PFNGLCREATESHADERPROC glCreateShader;
242 PFNGLCREATEPROGRAMPROC glCreateProgram;
243 PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray;
244 PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
245 PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation;
246 PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
247 PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
248 PFNGLGETSHADERIVPROC glGetShaderiv;
249 PFNGLGETPROGRAMIVPROC glGetProgramiv;
250 PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
251 PFNGLLINKPROGRAMPROC glLinkProgram;
252 PFNGLSHADERSOURCEPROC glShaderSource;
253 PFNGLUNIFORM1IPROC glUniform1i;
254 PFNGLUNIFORM1IVPROC glUniform1iv;
255 PFNGLUNIFORM2FPROC glUniform2f;
256 #ifdef MOJOSHADER_FLIP_RENDERTARGET
257 PFNGLUNIFORM1FPROC glUniform1f;
258 #endif
259 PFNGLUNIFORM4FVPROC glUniform4fv;
260 PFNGLUNIFORM4IVPROC glUniform4iv;
261 PFNGLUSEPROGRAMPROC glUseProgram;
262 PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;
263 PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
264 PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
265 PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
266 PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
267 PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
268 PFNGLDISABLEVERTEXATTRIBARRAYARBPROC glDisableVertexAttribArrayARB;
269 PFNGLENABLEVERTEXATTRIBARRAYARBPROC glEnableVertexAttribArrayARB;
270 PFNGLGETATTRIBLOCATIONARBPROC glGetAttribLocationARB;
271 PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
272 PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
273 PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
274 PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
275 PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
276 PFNGLUNIFORM1IARBPROC glUniform1iARB;
277 PFNGLUNIFORM1IVARBPROC glUniform1ivARB;
278 PFNGLUNIFORM4FVARBPROC glUniform4fvARB;
279 PFNGLUNIFORM4IVARBPROC glUniform4ivARB;
280 PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
281 PFNGLVERTEXATTRIBPOINTERARBPROC glVertexAttribPointerARB;
282 PFNGLGETPROGRAMIVARBPROC glGetProgramivARB;
283 PFNGLPROGRAMLOCALPARAMETER4FVARBPROC glProgramLocalParameter4fvARB;
284 PFNGLPROGRAMLOCALPARAMETERI4IVNVPROC glProgramLocalParameterI4ivNV;
285 PFNGLDELETEPROGRAMSARBPROC glDeleteProgramsARB;
286 PFNGLGENPROGRAMSARBPROC glGenProgramsARB;
287 PFNGLBINDPROGRAMARBPROC glBindProgramARB;
288 PFNGLPROGRAMSTRINGARBPROC glProgramStringARB;
289 PFNGLVERTEXATTRIBDIVISORARBPROC glVertexAttribDivisorARB;
290 PFNGLSHADERBINARYPROC glShaderBinary;
291 PFNGLSPECIALIZESHADERARBPROC glSpecializeShaderARB;
292
293 // interface for profile-specific things.
294 int (*profileMaxUniforms)(MOJOSHADER_shaderType shader_type);
295 int (*profileCompileShader)(const MOJOSHADER_parseData *pd, GLuint *s);
296 void (*profileDeleteShader)(const GLuint shader);
297 void (*profileDeleteProgram)(const GLuint program);
298 GLint (*profileGetAttribLocation)(MOJOSHADER_glProgram *program, int idx);
299 GLint (*profileGetUniformLocation)(MOJOSHADER_glProgram *program, MOJOSHADER_glShader *shader, int idx);
300 GLint (*profileGetSamplerLocation)(MOJOSHADER_glProgram *, MOJOSHADER_glShader *, int);
301 GLuint (*profileLinkProgram)(MOJOSHADER_glShader *, MOJOSHADER_glShader *);
302 void (*profileFinalInitProgram)(MOJOSHADER_glProgram *program);
303 void (*profileUseProgram)(MOJOSHADER_glProgram *program);
304 void (*profilePushConstantArray)(MOJOSHADER_glProgram *, const MOJOSHADER_uniform *, const GLfloat *);
305 void (*profilePushUniforms)(void);
306 void (*profilePushSampler)(GLint loc, GLuint sampler);
307 int (*profileMustPushConstantArrays)(void);
308 int (*profileMustPushSamplers)(void);
309 void (*profileToggleProgramPointSize)(int enable);
310 };
311
312 #ifdef MOJOSHADER_NO_THREAD_LOCAL
313 #define MOJOSHADER_THREADLOCAL
314 #elif defined(_WIN32)
315 #define MOJOSHADER_THREADLOCAL __declspec(thread)
316 #elif defined(__GNUC__) || defined(__clang__)
317 #define MOJOSHADER_THREADLOCAL __thread
318 #else
319 #error Please define your platform.
320 #endif
321
322 static MOJOSHADER_THREADLOCAL MOJOSHADER_glContext *ctx = NULL;
323
324 // Error state...
325 static char error_buffer[1024] = { '\0' };
326
327 static void set_error(const char *str)
328 {
329 snprintf(error_buffer, sizeof (error_buffer), "%s", str);
330 } // set_error
331
332 #if PLATFORM_MACOSX
333 static inline int macosx_version_atleast(int x, int y, int z)
334 {
335 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
336 return 1;
337 #else
338 static int checked = 0;
339 static int combined = 0;
340
341 if (!checked)
342 {
343 SInt32 ver = 0;
344 SInt32 major = 0;
345 SInt32 minor = 0;
346 SInt32 patch = 0;
347 int convert = 0;
348
349 if (Gestalt(gestaltSystemVersion, &ver) != noErr)
350 {
351 ver = 0x1000; // oh well.
352 convert = 1; // split (ver) into (major),(minor),(patch).
353 }
354 else if (ver < 0x1030)
355 {
356 convert = 1; // split (ver) into (major),(minor),(patch).
357 }
358 else
359 {
360 // presumably this won't fail. But if it does, we'll just use the
361 // original version value. This might cut the value--10.12.11 will
362 // come out to 10.9.9, for example--but it's better than nothing.
363 if (Gestalt(gestaltSystemVersionMajor, &major) != noErr)
364 convert = 1;
365 else if (Gestalt(gestaltSystemVersionMinor, &minor) != noErr)
366 convert = 1;
367 else if (Gestalt(gestaltSystemVersionBugFix, &patch) != noErr)
368 convert = 1;
369 } // else
370
371 if (convert)
372 {
373 major = ((ver & 0xFF00) >> 8);
374 major = (((major / 16) * 10) + (major % 16));
375 minor = ((ver & 0xF0) >> 4);
376 patch = (ver & 0xF);
377 } // if
378
379 combined = (major << 16) | (minor << 8) | patch;
380 checked = 1;
381 } // if
382
383 return (combined >= ((x << 16) | (y << 8) | z));
384 #endif
385 } // macosx_version_atleast
386 #endif
387
388 static inline void out_of_memory(void)
389 {
390 set_error("out of memory");
391 } // out_of_memory
392
393 static inline void *Malloc(const size_t len)
394 {
395 void *retval = ctx->malloc_fn((int) len, ctx->malloc_data);
396 if (retval == NULL)
397 out_of_memory();
398 return retval;
399 } // Malloc
400
401 static inline void Free(void *ptr)
402 {
403 if (ptr != NULL)
404 ctx->free_fn(ptr, ctx->malloc_data);
405 } // Free
406
407
408 static inline void toggle_gl_state(GLenum state, int val)
409 {
410 if (val)
411 ctx->glEnable(state);
412 else
413 ctx->glDisable(state);
414 } // toggle_gl_state
415
416
417 // profile-specific implementations...
418
419 #if SUPPORT_PROFILE_GLSL || SUPPORT_PROFILE_GLSPIRV
420 static inline GLenum glsl_shader_type(const MOJOSHADER_shaderType t)
421 {
422 // these enums match between core 2.0 and the ARB extensions.
423 if (t == MOJOSHADER_TYPE_VERTEX)
424 return GL_VERTEX_SHADER;
425 else if (t == MOJOSHADER_TYPE_PIXEL)
426 return GL_FRAGMENT_SHADER;
427
428 // !!! FIXME: geometry shaders?
429 assert(0 && "Unknown GLSL shader type!");
430 return GL_NONE;
431 } // glsl_shader_type
432
433
434 static int impl_GLSL_MustPushConstantArrays(void) { return 1; }
435 static int impl_GLSL_MustPushSamplers(void) { return 1; }
436
437 static int impl_GLSL_MaxUniforms(MOJOSHADER_shaderType shader_type)
438 {
439 // these enums match between core 2.0 and the ARB extensions.
440 GLenum pname = GL_NONE;
441 GLint val = 0;
442 if (shader_type == MOJOSHADER_TYPE_VERTEX)
443 pname = GL_MAX_VERTEX_UNIFORM_COMPONENTS;
444 else if (shader_type == MOJOSHADER_TYPE_PIXEL)
445 pname = GL_MAX_FRAGMENT_UNIFORM_COMPONENTS;
446 else
447 return -1;
448
449 ctx->glGetIntegerv(pname, &val);
450 return (int) val;
451 } // impl_GLSL_MaxUniforms
452
453 #if SUPPORT_PROFILE_GLSPIRV
454 static const SpirvPatchTable* spv_getPatchTable(MOJOSHADER_glShader *shader)
455 {
456 const MOJOSHADER_parseData *pd = shader->parseData;
457 size_t table_offset = pd->output_len - sizeof(SpirvPatchTable);
458 return (const SpirvPatchTable *) (pd->output + table_offset);
459 } // spv_getPatchTable
460
461 static int spv_CompileShader(const MOJOSHADER_parseData *pd, int32 base_location, GLuint *s)
462 {
463 GLint ok = 0;
464
465 GLsizei data_len = pd->output_len - sizeof(SpirvPatchTable);
466 const GLvoid* data = pd->output;
467 uint32 *patched_data = NULL;
468 if (base_location)
469 {
470 size_t i, max;
471
472 patched_data = (uint32 *) Malloc(data_len);
473 memcpy(patched_data, data, data_len);
474 const SpirvPatchTable *table = (const SpirvPatchTable *) &pd->output[data_len];
475 if (table->vpflip.offset) patched_data[table->vpflip.offset] += base_location;
476 if (table->array_vec4.offset) patched_data[table->array_vec4.offset] += base_location;
477 if (table->array_ivec4.offset) patched_data[table->array_ivec4.offset] += base_location;
478 if (table->array_bool.offset) patched_data[table->array_bool.offset] += base_location;
479
480 for (i = 0, max = STATICARRAYLEN(table->samplers); i < max; i++)
481 {
482 SpirvPatchEntry entry = table->samplers[i];
483 if (entry.offset)
484 patched_data[entry.offset] += base_location;
485 } // for
486
487 data = patched_data;
488 } // if
489
490 const GLuint shader = ctx->glCreateShader(glsl_shader_type(pd->shader_type));
491 ctx->glShaderBinary(1, &shader, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, data, data_len);
492 ctx->glSpecializeShaderARB(shader, pd->mainfn, 0, NULL, NULL); // FIXME: Spec Constants? -flibit
493 ctx->glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
494
495 if (patched_data)
496 Free(patched_data);
497
498 if (!ok)
499 {
500 GLsizei len = 0;
501 ctx->glGetShaderInfoLog(shader, sizeof(error_buffer), &len,
502 (GLchar *) error_buffer);
503 ctx->glDeleteShader(shader);
504 *s = 0;
505 return 0;
506 } // if
507
508 *s = shader;
509
510 return 1;
511 } // spv_CompileShader
512
513 static int impl_SPIRV_CompileShader(const MOJOSHADER_parseData *pd, GLuint *s)
514 {
515 // Compilation postponed until linking, but generate dummy shader id so hash table lookups work.
516 *s = ctx->glCreateShader(glsl_shader_type(pd->shader_type));
517 return 1;
518 } // impl_SPIRV_CompileShader
519
520 static GLuint impl_SPIRV_LinkProgram(MOJOSHADER_glShader *vshader,
521 MOJOSHADER_glShader *pshader)
522 {
523 GLint ok = 0;
524 GLuint vs_handle = 0;
525 int32 base_location = 0;
526
527 // Shader compilation postponed until linking due to locations being global
528 // in program. To avoid overlap between VS and PS, we need to know about
529 // other shader stages to assign final uniform/attrib locations before
530 // compilation.
531
532 MOJOSHADER_spirv_link_attributes(vshader->parseData, pshader->parseData, 1);
533
534 if (vshader)
535 {
536 if (!spv_CompileShader(vshader->parseData, base_location, &vs_handle))
537 return 0;
538
539 const SpirvPatchTable* patch_table = spv_getPatchTable(vshader);
540 base_location += patch_table->location_count;
541 } // if
542
543 GLuint ps_handle = 0;
544 if (pshader)
545 {
546 if (!spv_CompileShader(pshader->parseData, base_location, &ps_handle))
547 return 0;
548 } // if
549
550 if (ctx->have_opengl_2)
551 {
552 const GLuint program = ctx->glCreateProgram();
553 if (vs_handle)
554 {
555 ctx->glAttachShader(program, vs_handle);
556 ctx->glDeleteShader(vs_handle);
557 } // if
558 if (ps_handle)
559 {
560 ctx->glAttachShader(program, ps_handle);
561 ctx->glDeleteShader(ps_handle);
562 } // if
563 ctx->glLinkProgram(program);
564 ctx->glGetProgramiv(program, GL_LINK_STATUS, &ok);
565 if (!ok)
566 {
567 GLsizei len = 0;
568 ctx->glGetProgramInfoLog(program, sizeof (error_buffer),
569 &len, (GLchar *) error_buffer);
570 ctx->glDeleteProgram(program);
571 return 0;
572 } // if
573
574 return program;
575 } // if
576 else
577 {
578 const GLhandleARB program = ctx->glCreateProgramObjectARB();
579 assert(sizeof(program) == sizeof(GLuint)); // not always true on OS X!
580 if (vs_handle)
581 {
582 ctx->glAttachObjectARB(program, (GLhandleARB) vs_handle);
583 ctx->glDeleteObjectARB((GLhandleARB) vs_handle);
584 } // if
585 if (ps_handle)
586 {
587 ctx->glAttachObjectARB(program, (GLhandleARB) ps_handle);
588 ctx->glDeleteObjectARB((GLhandleARB) ps_handle);
589 } // if
590 ctx->glLinkProgramARB(program);
591 ctx->glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &ok);
592 if (!ok)
593 {
594 GLsizei len = 0;
595 ctx->glGetInfoLogARB(program, sizeof (error_buffer),
596 &len, (GLcharARB *) error_buffer);
597 ctx->glDeleteObjectARB(program);
598 return 0;
599 } // if
600
601 return (GLuint) program;
602 } // else
603 } // impl_SPIRV_LinkProgram
604
605 static void impl_SPIRV_DeleteShader(const GLuint shader)
606 {
607 ctx->glDeleteShader(shader);
608 } // impl_SPIRV_DeleteShader
609
610 static void impl_SPIRV_DeleteProgram(const GLuint program)
611 {
612 if (ctx->have_opengl_2)
613 ctx->glDeleteProgram(program);
614 else
615 ctx->glDeleteObjectARB((GLhandleARB) program);
616 } // impl_SPIRV_DeleteProgram
617
618 static GLint impl_SPIRV_GetAttribLocation(MOJOSHADER_glProgram *program, int idx)
619 {
620 return idx;
621 } // impl_SPIRV_GetAttribLocation
622
623 static GLint impl_SPIRV_GetUniformLocation(MOJOSHADER_glProgram *program, MOJOSHADER_glShader *shader, int idx)
624 {
625 return 0; // no-op, we push this as one big-ass array now.
626 } // impl_SPIRV_GetUniformLocation
627
628 static GLint impl_SPIRV_GetSamplerLocation(MOJOSHADER_glProgram *program, MOJOSHADER_glShader *shader, int idx)
629 {
630 const SpirvPatchTable *table = spv_getPatchTable(shader);
631 GLint location = table->samplers[idx].location;
632 if (location == -1)
633 return location;
634
635 assert(location >= 0);
636 if (shader->parseData->shader_type == MOJOSHADER_TYPE_PIXEL)
637 location += spv_getPatchTable(program->vertex)->location_count;
638
639 return location;
640 } // impl_SPIRV_GetSamplerLocation
641
642 static void impl_SPIRV_FinalInitProgram(MOJOSHADER_glProgram *program)
643 {
644 const SpirvPatchTable *vs_table = spv_getPatchTable(program->vertex);
645 const SpirvPatchTable *ps_table = spv_getPatchTable(program->fragment);
646 program->vs_float4_loc = vs_table->array_vec4.location;
647 program->vs_int4_loc = vs_table->array_ivec4.location;
648 program->vs_bool_loc = vs_table->array_bool.location;
649 program->ps_float4_loc = ps_table->array_vec4.location;
650 program->ps_int4_loc = ps_table->array_ivec4.location;
651 program->ps_bool_loc = ps_table->array_bool.location;
652 program->ps_vpos_flip_loc = ps_table->vpflip.location;
653 #ifdef MOJOSHADER_FLIP_RENDERTARGET
654 program->vs_flip_loc = vs_table->vpflip.location;
655 #endif
656
657 int32 ps_base_location = vs_table->location_count;
658 if (ps_base_location)
659 {
660 if (program->ps_float4_loc != -1) program->ps_float4_loc += ps_base_location;
661 if (program->ps_int4_loc != -1) program->ps_int4_loc += ps_base_location;
662 if (program->ps_bool_loc != -1) program->ps_bool_loc += ps_base_location;
663 if (program->ps_vpos_flip_loc != -1) program->ps_vpos_flip_loc += ps_base_location;
664 } // if
665 } // impl_SPIRV_FinalInitProgram
666 #endif // SUPPORT_PROFILE_GLSPIRV
667
668 #if SUPPORT_PROFILE_GLSL
669 static int impl_GLSL_CompileShader(const MOJOSHADER_parseData *pd, GLuint *s)
670 {
671 GLint ok = 0;
672 const GLint codelen = (GLint) pd->output_len;
673 const GLenum shader_type = glsl_shader_type(pd->shader_type);
674
675 if (ctx->have_opengl_2)
676 {
677 const GLuint shader = ctx->glCreateShader(shader_type);
678 ctx->glShaderSource(shader, 1, (const GLchar**) &pd->output, &codelen);
679 ctx->glCompileShader(shader);
680 ctx->glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
681 if (!ok)
682 {
683 GLsizei len = 0;
684 ctx->glGetShaderInfoLog(shader, sizeof (error_buffer), &len,
685 (GLchar *) error_buffer);
686 ctx->glDeleteShader(shader);
687 *s = 0;
688 return 0;
689 } // if
690
691 *s = shader;
692 } // if
693 else
694 {
695 const GLhandleARB shader = ctx->glCreateShaderObjectARB(shader_type);
696 assert(sizeof (shader) == sizeof (*s)); // not always true on OS X!
697 ctx->glShaderSourceARB(shader, 1,
698 (const GLcharARB **) &pd->output, &codelen);
699 ctx->glCompileShaderARB(shader);
700 ctx->glGetObjectParameterivARB(shader,GL_OBJECT_COMPILE_STATUS_ARB,&ok);
701 if (!ok)
702 {
703 GLsizei len = 0;
704 ctx->glGetInfoLogARB(shader, sizeof (error_buffer), &len,
705 (GLcharARB *) error_buffer);
706 ctx->glDeleteObjectARB(shader);
707 *s = 0;
708 return 0;
709 } // if
710
711 *s = (GLuint) shader;
712 } // else
713
714 return 1;
715 } // impl_GLSL_CompileShader
716 #endif // SUPPORT_PROFILE_GLSL
717
718
719 static void impl_GLSL_DeleteShader(const GLuint shader)
720 {
721 if (ctx->have_opengl_2)
722 ctx->glDeleteShader(shader);
723 else
724 ctx->glDeleteObjectARB((GLhandleARB) shader);
725 } // impl_GLSL_DeleteShader
726
727
728 static void impl_GLSL_DeleteProgram(const GLuint program)
729 {
730 if (ctx->have_opengl_2)
731 ctx->glDeleteProgram(program);
732 else
733 ctx->glDeleteObjectARB((GLhandleARB) program);
734 } // impl_GLSL_DeleteProgram
735
736
737 static GLint impl_GLSL_GetUniformLocation(MOJOSHADER_glProgram *program,
738 MOJOSHADER_glShader *shader, int idx)
739 {
740 return 0; // no-op, we push this as one big-ass array now.
741 } // impl_GLSL_GetUniformLocation
742
743
744 static inline GLint glsl_uniform_loc(MOJOSHADER_glProgram *program,
745 const char *name)
746 {
747 return ctx->have_opengl_2 ?
748 ctx->glGetUniformLocation(program->handle, name) :
749 ctx->glGetUniformLocationARB((GLhandleARB) program->handle, name);
750 } // glsl_uniform_loc
751
752
753 static GLint impl_GLSL_GetSamplerLocation(MOJOSHADER_glProgram *program,
754 MOJOSHADER_glShader *shader, int idx)
755 {
756 return glsl_uniform_loc(program, shader->parseData->samplers[idx].name);
757 } // impl_GLSL_GetSamplerLocation
758
759
760 static GLint impl_GLSL_GetAttribLocation(MOJOSHADER_glProgram *program, int idx)
761 {
762 const MOJOSHADER_parseData *pd = program->vertex->parseData;
763 const MOJOSHADER_attribute *a = pd->attributes;
764
765 if (ctx->have_opengl_2)
766 {
767 return ctx->glGetAttribLocation(program->handle,
768 (const GLchar *) a[idx].name);
769 } // if
770
771 return ctx->glGetAttribLocationARB((GLhandleARB) program->handle,
772 (const GLcharARB *) a[idx].name);
773 } // impl_GLSL_GetAttribLocation
774
775
776 static GLuint impl_GLSL_LinkProgram(MOJOSHADER_glShader *vshader,
777 MOJOSHADER_glShader *pshader)
778 {
779 GLint ok = 0;
780
781 if (ctx->have_opengl_2)
782 {
783 const GLuint program = ctx->glCreateProgram();
784
785 if (vshader != NULL) ctx->glAttachShader(program, vshader->handle);
786 if (pshader != NULL) ctx->glAttachShader(program, pshader->handle);
787
788 ctx->glLinkProgram(program);
789
790 ctx->glGetProgramiv(program, GL_LINK_STATUS, &ok);
791 if (!ok)
792 {
793 GLsizei len = 0;
794 ctx->glGetProgramInfoLog(program, sizeof (error_buffer),
795 &len, (GLchar *) error_buffer);
796 ctx->glDeleteProgram(program);
797 return 0;
798 } // if
799
800 return program;
801 } // if
802 else
803 {
804 const GLhandleARB program = ctx->glCreateProgramObjectARB();
805 assert(sizeof(program) == sizeof(GLuint)); // not always true on OS X!
806
807 if (vshader != NULL)
808 ctx->glAttachObjectARB(program, (GLhandleARB) vshader->handle);
809
810 if (pshader != NULL)
811 ctx->glAttachObjectARB(program, (GLhandleARB) pshader->handle);
812
813 ctx->glLinkProgramARB(program);
814
815 ctx->glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &ok);
816 if (!ok)
817 {
818 GLsizei len = 0;
819 ctx->glGetInfoLogARB(program, sizeof (error_buffer),
820 &len, (GLcharARB *) error_buffer);
821 ctx->glDeleteObjectARB(program);
822 return 0;
823 } // if
824
825 return (GLuint) program;
826 } // else
827 } // impl_GLSL_LinkProgram
828
829 static void impl_GLSL_FinalInitProgram(MOJOSHADER_glProgram *program)
830 {
831 program->vs_float4_loc = glsl_uniform_loc(program, "vs_uniforms_vec4");
832 program->vs_int4_loc = glsl_uniform_loc(program, "vs_uniforms_ivec4");
833 program->vs_bool_loc = glsl_uniform_loc(program, "vs_uniforms_bool");
834 program->ps_float4_loc = glsl_uniform_loc(program, "ps_uniforms_vec4");
835 program->ps_int4_loc = glsl_uniform_loc(program, "ps_uniforms_ivec4");
836 program->ps_bool_loc = glsl_uniform_loc(program, "ps_uniforms_bool");
837 program->ps_vpos_flip_loc = glsl_uniform_loc(program, "vposFlip");
838 #ifdef MOJOSHADER_FLIP_RENDERTARGET
839 program->vs_flip_loc = glsl_uniform_loc(program, "vpFlip");
840 #endif
841 } // impl_GLSL_FinalInitProgram
842
843
844 static void impl_GLSL_UseProgram(MOJOSHADER_glProgram *program)
845 {
846 if (ctx->have_opengl_2)
847 ctx->glUseProgram(program ? program->handle : 0);
848 else
849 ctx->glUseProgramObjectARB((GLhandleARB) (program ? program->handle : 0));
850 } // impl_GLSL_UseProgram
851
852
853 static void impl_GLSL_PushConstantArray(MOJOSHADER_glProgram *program,
854 const MOJOSHADER_uniform *u,
855 const GLfloat *f)
856 {
857 const GLint loc = glsl_uniform_loc(program, u->name);
858 if (loc >= 0) // not optimized out?
859 ctx->glUniform4fv(loc, u->array_count, f);
860 } // impl_GLSL_PushConstantArray
861
862
863 static void impl_GLSL_PushUniforms(void)
864 {
865 const MOJOSHADER_glProgram *program = ctx->bound_program;
866
867 assert(program->uniform_count > 0); // don't call with nothing to do!
868
869 if (program->vs_float4_loc != -1)
870 {
871 ctx->glUniform4fv(program->vs_float4_loc,
872 program->vs_uniforms_float4_count,
873 program->vs_uniforms_float4);
874 } // if
875
876 if (program->vs_int4_loc != -1)
877 {
878 ctx->glUniform4iv(program->vs_int4_loc,
879 program->vs_uniforms_int4_count,
880 program->vs_uniforms_int4);
881 } // if
882
883 if (program->vs_bool_loc != -1)
884 {
885 ctx->glUniform1iv(program->vs_bool_loc,
886 program->vs_uniforms_bool_count,
887 program->vs_uniforms_bool);
888 } // if
889
890 if (program->ps_float4_loc != -1)
891 {
892 ctx->glUniform4fv(program->ps_float4_loc,
893 program->ps_uniforms_float4_count,
894 program->ps_uniforms_float4);
895 } // if
896
897 if (program->ps_int4_loc != -1)
898 {
899 ctx->glUniform4iv(program->ps_int4_loc,
900 program->ps_uniforms_int4_count,
901 program->ps_uniforms_int4);
902 } // if
903
904 if (program->ps_bool_loc != -1)
905 {
906 ctx->glUniform1iv(program->ps_bool_loc,
907 program->ps_uniforms_bool_count,
908 program->ps_uniforms_bool);
909 } // if
910 } // impl_GLSL_PushUniforms
911
912
913 static void impl_GLSL_PushSampler(GLint loc, GLuint sampler)
914 {
915 ctx->glUniform1i(loc, sampler);
916 } // impl_GLSL_PushSampler
917
918 #endif // SUPPORT_PROFILE_GLSL || SUPPORT_PROFILE_GLSPIRV
919
920
921 #if SUPPORT_PROFILE_ARB1
922 static inline GLenum arb1_shader_type(const MOJOSHADER_shaderType t)
923 {
924 if (t == MOJOSHADER_TYPE_VERTEX)
925 return GL_VERTEX_PROGRAM_ARB;
926 else if (t == MOJOSHADER_TYPE_PIXEL)
927 return GL_FRAGMENT_PROGRAM_ARB;
928
929 // !!! FIXME: geometry shaders?
930 return GL_NONE;
931 } // arb1_shader_type
932
933 static int impl_ARB1_MustPushConstantArrays(void) { return 0; }
934 static int impl_ARB1_MustPushSamplers(void) { return 0; }
935
936 static int impl_ARB1_MaxUniforms(MOJOSHADER_shaderType shader_type)
937 {
938 GLint retval = 0;
939 const GLenum program_type = arb1_shader_type(shader_type);
940 if (program_type == GL_NONE)
941 return -1;
942
943 ctx->glGetProgramivARB(program_type, GL_MAX_PROGRAM_PARAMETERS_ARB, &retval);
944 return (int) retval; // !!! FIXME: times four?
945 } // impl_ARB1_MaxUniforms
946
947
948 static int impl_ARB1_CompileShader(const MOJOSHADER_parseData *pd, GLuint *s)
949 {
950 GLint shaderlen = (GLint) pd->output_len;
951 const GLenum shader_type = arb1_shader_type(pd->shader_type);
952 GLuint shader = 0;
953 ctx->glGenProgramsARB(1, &shader);
954
955 ctx->glGetError(); // flush any existing error state.
956 ctx->glBindProgramARB(shader_type, shader);
957 ctx->glProgramStringARB(shader_type, GL_PROGRAM_FORMAT_ASCII_ARB,
958 shaderlen, pd->output);
959
960 if (ctx->glGetError() == GL_INVALID_OPERATION)
961 {
962 GLint pos = 0;
963 ctx->glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
964 const GLubyte *errstr = ctx->glGetString(GL_PROGRAM_ERROR_STRING_ARB);
965 snprintf(error_buffer, sizeof (error_buffer),
966 "ARB1 compile error at position %d: %s",
967 (int) pos, (const char *) errstr);
968 ctx->glBindProgramARB(shader_type, 0);
969 ctx->glDeleteProgramsARB(1, &shader);
970 *s = 0;
971 return 0;
972 } // if
973
974 *s = shader;
975 return 1;
976 } // impl_ARB1_CompileShader
977
978
979 static void impl_ARB1_DeleteShader(const GLuint _shader)
980 {
981 GLuint shader = _shader; // const removal.
982 ctx->glDeleteProgramsARB(1, &shader);
983 } // impl_ARB1_DeleteShader
984
985
986 static void impl_ARB1_DeleteProgram(const GLuint program)
987 {
988 // no-op. ARB1 doesn't have real linked programs.
989 } // impl_ARB1_DeleteProgram
990
991 static GLint impl_ARB1_GetUniformLocation(MOJOSHADER_glProgram *program,
992 MOJOSHADER_glShader *shader, int idx)
993 {
994 return 0; // no-op, we push this as one big-ass array now.
995 } // impl_ARB1_GetUniformLocation
996
997 static GLint impl_ARB1_GetSamplerLocation(MOJOSHADER_glProgram *program,
998 MOJOSHADER_glShader *shader, int idx)
999 {
1000 return shader->parseData->samplers[idx].index;
1001 } // impl_ARB1_GetSamplerLocation
1002
1003
1004 static GLint impl_ARB1_GetAttribLocation(MOJOSHADER_glProgram *program, int idx)
1005 {
1006 return idx; // map to vertex arrays in the same order as the parseData.
1007 } // impl_ARB1_GetAttribLocation
1008
1009
1010 static GLuint impl_ARB1_LinkProgram(MOJOSHADER_glShader *vshader,
1011 MOJOSHADER_glShader *pshader)
1012 {
1013 // there is no formal linking in ARB1...just return a unique value.
1014 static GLuint retval = 1;
1015 return retval++;
1016 } // impl_ARB1_LinkProgram
1017
1018
1019 static void impl_ARB1_FinalInitProgram(MOJOSHADER_glProgram *program)
1020 {
1021 // no-op.
1022 } // impl_ARB1_FinalInitProgram
1023
1024
1025 static void impl_ARB1_UseProgram(MOJOSHADER_glProgram *program)
1026 {
1027 GLuint vhandle = 0;
1028 GLuint phandle = 0;
1029 if (program != NULL)
1030 {
1031 if (program->vertex != NULL)
1032 vhandle = program->vertex->handle;
1033 if (program->fragment != NULL)
1034 phandle = program->fragment->handle;
1035 } // if
1036
1037 toggle_gl_state(GL_VERTEX_PROGRAM_ARB, vhandle != 0);
1038 toggle_gl_state(GL_FRAGMENT_PROGRAM_ARB, phandle != 0);
1039
1040 ctx->glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vhandle);
1041 ctx->glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, phandle);
1042 } // impl_ARB1_UseProgram
1043
1044
1045 static void impl_ARB1_PushConstantArray(MOJOSHADER_glProgram *program,
1046 const MOJOSHADER_uniform *u,
1047 const GLfloat *f)
1048 {
1049 // no-op. Constant arrays are defined in source code for arb1.
1050 } // impl_ARB1_PushConstantArray
1051
1052
1053 static void impl_ARB1_PushUniforms(void)
1054 {
1055 // vertex shader uniforms come first in program->uniforms array.
1056 MOJOSHADER_shaderType shader_type = MOJOSHADER_TYPE_VERTEX;
1057 GLenum arb_shader_type = arb1_shader_type(shader_type);
1058 const MOJOSHADER_glProgram *program = ctx->bound_program;
1059 const uint32 count = program->uniform_count;
1060 const GLfloat *srcf = program->vs_uniforms_float4;
1061 const GLint *srci = program->vs_uniforms_int4;
1062 const GLint *srcb = program->vs_uniforms_bool;
1063 GLint loc = 0;
1064 GLint texbem_loc = 0;
1065 uint32 i;
1066
1067 assert(count > 0); // shouldn't call this with nothing to do!
1068
1069 for (i = 0; i < count; i++)
1070 {
1071 UniformMap *map = &program->uniforms[i];
1072 const MOJOSHADER_shaderType uniform_shader_type = map->shader_type;
1073 const MOJOSHADER_uniform *u = map->uniform;
1074 const MOJOSHADER_uniformType type = u->type;
1075 const int size = u->array_count ? u->array_count : 1;
1076
1077 assert(!u->constant);
1078
1079 // Did we switch from vertex to pixel (to geometry, etc)?
1080 if (shader_type != uniform_shader_type)
1081 {
1082 if (shader_type == MOJOSHADER_TYPE_PIXEL)
1083 texbem_loc = loc;
1084
1085 // we start with vertex, move to pixel, then to geometry, etc.
1086 // The array should always be sorted as such.
1087 if (uniform_shader_type == MOJOSHADER_TYPE_PIXEL)
1088 {
1089 assert(shader_type == MOJOSHADER_TYPE_VERTEX);
1090 srcf = program->ps_uniforms_float4;
1091 srci = program->ps_uniforms_int4;
1092 srcb = program->ps_uniforms_bool;
1093 loc = 0;
1094 } // if
1095 else
1096 {
1097 // These should be ordered vertex, then pixel, then geometry.
1098 assert(0 && "Unexpected shader type");
1099 } // else
1100
1101 shader_type = uniform_shader_type;
1102 arb_shader_type = arb1_shader_type(uniform_shader_type);
1103 } // if
1104
1105 if (type == MOJOSHADER_UNIFORM_FLOAT)
1106 {
1107 int i;
1108 for (i = 0; i < size; i++, srcf += 4, loc++)
1109 ctx->glProgramLocalParameter4fvARB(arb_shader_type, loc, srcf);
1110 } // if
1111 else if (type == MOJOSHADER_UNIFORM_INT)
1112 {
1113 int i;
1114 if (ctx->have_GL_NV_gpu_program4)
1115 {
1116 // GL_NV_gpu_program4 has integer uniform loading support.
1117 for (i = 0; i < size; i++, srci += 4, loc++)
1118 ctx->glProgramLocalParameterI4ivNV(arb_shader_type, loc, srci);
1119 } // if
1120 else
1121 {
1122 for (i = 0; i < size; i++, srci += 4, loc++)
1123 {
1124 const GLfloat fv[4] = {
1125 (GLfloat) srci[0], (GLfloat) srci[1],
1126 (GLfloat) srci[2], (GLfloat) srci[3]
1127 };
1128 ctx->glProgramLocalParameter4fvARB(arb_shader_type, loc, fv);
1129 } // for
1130 } // else
1131 } // else if
1132 else if (type == MOJOSHADER_UNIFORM_BOOL)
1133 {
1134 int i;
1135 if (ctx->have_GL_NV_gpu_program4)
1136 {
1137 // GL_NV_gpu_program4 has integer uniform loading support.
1138 for (i = 0; i < size; i++, srcb++, loc++)
1139 {
1140 const GLint ib = (GLint) ((*srcb) ? 1 : 0);
1141 const GLint iv[4] = { ib, ib, ib, ib };
1142 ctx->glProgramLocalParameterI4ivNV(arb_shader_type, loc, iv);
1143 } // for
1144 } // if
1145 else
1146 {
1147 for (i = 0; i < size; i++, srcb++, loc++)
1148 {
1149 const GLfloat fb = (GLfloat) ((*srcb) ? 1.0f : 0.0f);
1150 const GLfloat fv[4] = { fb, fb, fb, fb };
1151 ctx->glProgramLocalParameter4fvARB(arb_shader_type, loc, fv);
1152 } // for
1153 } // else
1154 } // else if
1155 } // for
1156
1157 if (program->texbem_count)
1158 {
1159 const GLenum target = GL_FRAGMENT_PROGRAM_ARB;
1160 GLfloat *srcf = program->ps_uniforms_float4;
1161 srcf += (program->ps_uniforms_float4_count * 4) -
1162 (program->texbem_count * 8);
1163 loc = texbem_loc;
1164 for (i = 0; i < program->texbem_count; i++, srcf += 8)
1165 {
1166 ctx->glProgramLocalParameter4fvARB(target, loc++, srcf);
1167 ctx->glProgramLocalParameter4fvARB(target, loc++, srcf + 4);
1168 } // for
1169 } // if
1170 } // impl_ARB1_PushUniforms
1171
1172 static void impl_ARB1_PushSampler(GLint loc, GLuint sampler)
1173 {
1174 // no-op in this profile...arb1 uses the texture units as-is.
1175 assert(loc == (GLint) sampler);
1176 } // impl_ARB1_PushSampler
1177
1178 #endif // SUPPORT_PROFILE_ARB1
1179
1180 #if SUPPORT_PROFILE_GLSL || SUPPORT_PROFILE_ARB1
1181
1182 static void impl_REAL_ToggleProgramPointSize(int enable)
1183 {
1184 toggle_gl_state(GL_PROGRAM_POINT_SIZE, enable);
1185 } // impl_REAL_ToggleProgramPointSize
1186
1187 static void impl_NOOP_ToggleProgramPointSize(int enable)
1188 {
1189 // No-op, this profile's GL context forces this to always be on
1190 } // impl_NOOP_ToggleProgramPointSize
1191
1192 #endif // SUPPORT_PROFILE_GLSL || SUPPORT_PROFILE_ARB1
1193
1194
1195 const char *MOJOSHADER_glGetError(void)
1196 {
1197 return error_buffer;
1198 } // MOJOSHADER_glGetError
1199
1200
1201 static void *loadsym(MOJOSHADER_glGetProcAddress lookup, void *d,
1202 const char *fn, int *ext)
1203 {
1204 void *retval = NULL;
1205 if (lookup != NULL)
1206 retval = lookup(fn, d);
1207
1208 if (retval == NULL)
1209 *ext = 0;
1210
1211 return retval;
1212 } // loadsym
1213
1214 static void lookup_entry_points(MOJOSHADER_glGetProcAddress lookup, void *d)
1215 {
1216 #define DO_LOOKUP(ext, typ, fn) { \
1217 ctx->fn = (typ) loadsym(lookup, d, #fn, &ctx->have_##ext); \
1218 }
1219
1220 DO_LOOKUP(core_opengl, PFNGLGETSTRINGPROC, glGetString);
1221 DO_LOOKUP(core_opengl, PFNGLGETERRORPROC, glGetError);
1222 DO_LOOKUP(core_opengl, PFNGLGETINTEGERVPROC, glGetIntegerv);
1223 DO_LOOKUP(core_opengl, PFNGLENABLEPROC, glEnable);
1224 DO_LOOKUP(core_opengl, PFNGLDISABLEPROC, glDisable);
1225 DO_LOOKUP(opengl_3, PFNGLGETSTRINGIPROC, glGetStringi);
1226 DO_LOOKUP(opengl_2, PFNGLDELETESHADERPROC, glDeleteShader);
1227 DO_LOOKUP(opengl_2, PFNGLDELETEPROGRAMPROC, glDeleteProgram);
1228 DO_LOOKUP(opengl_2, PFNGLATTACHSHADERPROC, glAttachShader);
1229 DO_LOOKUP(opengl_2, PFNGLCOMPILESHADERPROC, glCompileShader);
1230 DO_LOOKUP(opengl_2, PFNGLCREATESHADERPROC, glCreateShader);
1231 DO_LOOKUP(opengl_2, PFNGLCREATEPROGRAMPROC, glCreateProgram);
1232 DO_LOOKUP(opengl_2, PFNGLDISABLEVERTEXATTRIBARRAYPROC, glDisableVertexAttribArray);
1233 DO_LOOKUP(opengl_2, PFNGLENABLEVERTEXATTRIBARRAYPROC, glEnableVertexAttribArray);
1234 DO_LOOKUP(opengl_2, PFNGLGETATTRIBLOCATIONPROC, glGetAttribLocation);
1235 DO_LOOKUP(opengl_2, PFNGLGETPROGRAMINFOLOGPROC, glGetProgramInfoLog);
1236 DO_LOOKUP(opengl_2, PFNGLGETSHADERINFOLOGPROC, glGetShaderInfoLog);
1237 DO_LOOKUP(opengl_2, PFNGLGETSHADERIVPROC, glGetShaderiv);
1238 DO_LOOKUP(opengl_2, PFNGLGETPROGRAMIVPROC, glGetProgramiv);
1239 DO_LOOKUP(opengl_2, PFNGLGETUNIFORMLOCATIONPROC, glGetUniformLocation);
1240 DO_LOOKUP(opengl_2, PFNGLLINKPROGRAMPROC, glLinkProgram);
1241 DO_LOOKUP(opengl_2, PFNGLSHADERSOURCEPROC, glShaderSource);
1242 DO_LOOKUP(opengl_2, PFNGLUNIFORM1IPROC, glUniform1i);
1243 DO_LOOKUP(opengl_2, PFNGLUNIFORM1IVPROC, glUniform1iv);
1244 DO_LOOKUP(opengl_2, PFNGLUNIFORM2FPROC, glUniform2f);
1245 #ifdef MOJOSHADER_FLIP_RENDERTARGET
1246 DO_LOOKUP(opengl_2, PFNGLUNIFORM1FPROC, glUniform1f);
1247 #endif
1248 DO_LOOKUP(opengl_2, PFNGLUNIFORM4FVPROC, glUniform4fv);
1249 DO_LOOKUP(opengl_2, PFNGLUNIFORM4IVPROC, glUniform4iv);
1250 DO_LOOKUP(opengl_2, PFNGLUSEPROGRAMPROC, glUseProgram);
1251 DO_LOOKUP(opengl_2, PFNGLVERTEXATTRIBPOINTERPROC, glVertexAttribPointer);
1252 DO_LOOKUP(GL_ARB_shader_objects, PFNGLDELETEOBJECTARBPROC, glDeleteObjectARB);
1253 DO_LOOKUP(GL_ARB_shader_objects, PFNGLATTACHOBJECTARBPROC, glAttachObjectARB);
1254 DO_LOOKUP(GL_ARB_shader_objects, PFNGLCOMPILESHADERARBPROC, glCompileShaderARB);
1255 DO_LOOKUP(GL_ARB_shader_objects, PFNGLCREATEPROGRAMOBJECTARBPROC, glCreateProgramObjectARB);
1256 DO_LOOKUP(GL_ARB_shader_objects, PFNGLCREATESHADEROBJECTARBPROC, glCreateShaderObjectARB);
1257 DO_LOOKUP(GL_ARB_shader_objects, PFNGLGETINFOLOGARBPROC, glGetInfoLogARB);
1258 DO_LOOKUP(GL_ARB_shader_objects, PFNGLGETOBJECTPARAMETERIVARBPROC, glGetObjectParameterivARB);
1259 DO_LOOKUP(GL_ARB_shader_objects, PFNGLGETUNIFORMLOCATIONARBPROC, glGetUniformLocationARB);
1260 DO_LOOKUP(GL_ARB_shader_objects, PFNGLLINKPROGRAMARBPROC, glLinkProgramARB);
1261 DO_LOOKUP(GL_ARB_shader_objects, PFNGLSHADERSOURCEARBPROC, glShaderSourceARB);
1262 DO_LOOKUP(GL_ARB_shader_objects, PFNGLUNIFORM1IARBPROC, glUniform1iARB);
1263 DO_LOOKUP(GL_ARB_shader_objects, PFNGLUNIFORM1IVARBPROC, glUniform1ivARB);
1264 DO_LOOKUP(GL_ARB_shader_objects, PFNGLUNIFORM4FVARBPROC, glUniform4fvARB);
1265 DO_LOOKUP(GL_ARB_shader_objects, PFNGLUNIFORM4IVARBPROC, glUniform4ivARB);
1266 DO_LOOKUP(GL_ARB_shader_objects, PFNGLUSEPROGRAMOBJECTARBPROC, glUseProgramObjectARB);
1267 DO_LOOKUP(GL_ARB_vertex_shader, PFNGLDISABLEVERTEXATTRIBARRAYARBPROC, glDisableVertexAttribArrayARB);
1268 DO_LOOKUP(GL_ARB_vertex_shader, PFNGLENABLEVERTEXATTRIBARRAYARBPROC, glEnableVertexAttribArrayARB);
1269 DO_LOOKUP(GL_ARB_vertex_shader, PFNGLGETATTRIBLOCATIONARBPROC, glGetAttribLocationARB);
1270 DO_LOOKUP(GL_ARB_vertex_shader, PFNGLVERTEXATTRIBPOINTERARBPROC, glVertexAttribPointerARB);
1271 DO_LOOKUP(GL_ARB_vertex_program, PFNGLVERTEXATTRIBPOINTERARBPROC, glVertexAttribPointerARB);
1272 DO_LOOKUP(GL_ARB_vertex_program, PFNGLGETPROGRAMIVARBPROC, glGetProgramivARB);
1273 DO_LOOKUP(GL_ARB_vertex_program, PFNGLPROGRAMLOCALPARAMETER4FVARBPROC, glProgramLocalParameter4fvARB);
1274 DO_LOOKUP(GL_ARB_vertex_program, PFNGLDELETEPROGRAMSARBPROC, glDeleteProgramsARB);
1275 DO_LOOKUP(GL_ARB_vertex_program, PFNGLGENPROGRAMSARBPROC, glGenProgramsARB);
1276 DO_LOOKUP(GL_ARB_vertex_program, PFNGLBINDPROGRAMARBPROC, glBindProgramARB);
1277 DO_LOOKUP(GL_ARB_vertex_program, PFNGLPROGRAMSTRINGARBPROC, glProgramStringARB);
1278 DO_LOOKUP(GL_NV_gpu_program4, PFNGLPROGRAMLOCALPARAMETERI4IVNVPROC, glProgramLocalParameterI4ivNV);
1279 DO_LOOKUP(GL_ARB_instanced_arrays, PFNGLVERTEXATTRIBDIVISORARBPROC, glVertexAttribDivisorARB);
1280 DO_LOOKUP(GL_ARB_ES2_compatibility, PFNGLSHADERBINARYPROC, glShaderBinary);
1281 DO_LOOKUP(GL_ARB_gl_spirv, PFNGLSPECIALIZESHADERARBPROC, glSpecializeShaderARB);
1282
1283 #undef DO_LOOKUP
1284 } // lookup_entry_points
1285
1286 static inline int opengl_version_atleast(const int major, const int minor)
1287 {
1288 return ( ((ctx->opengl_major << 16) | (ctx->opengl_minor & 0xFFFF)) >=
1289 ((major << 16) | (minor & 0xFFFF)) );
1290 } // opengl_version_atleast
1291
1292 static int verify_extension(const char *ext, int have, StringCache *exts,
1293 int major, int minor)
1294 {
1295 if (have == 0)
1296 return 0; // don't bother checking, we're missing an entry point.
1297
1298 else if (!ctx->have_core_opengl)
1299 return 0; // don't bother checking, we're missing basic functionality.
1300
1301 // See if it's in the spec for this GL implementation's version.
1302 if ((major > 0) && (opengl_version_atleast(major, minor)))
1303 return 1;
1304
1305 // Not available in the GL version, check the extension list.
1306 return stringcache_iscached(exts, ext);
1307 } // verify_extension
1308
1309
1310 static void parse_opengl_version_str(const char *verstr, int *maj, int *min)
1311 {
1312 if (verstr == NULL)
1313 *maj = *min = 0;
1314 else
1315 sscanf(verstr, "%d.%d", maj, min);
1316 } // parse_opengl_version_str
1317
1318
1319 #if SUPPORT_PROFILE_GLSL
1320 static inline int glsl_version_atleast(const int major, const int minor)
1321 {
1322 return ( ((ctx->glsl_major << 16) | (ctx->glsl_minor & 0xFFFF)) >=
1323 ((major << 16) | (minor & 0xFFFF)) );
1324 } // glsl_version_atleast
1325 #endif
1326
1327 static void detect_glsl_version(void)
1328 {
1329 ctx->glsl_major = ctx->glsl_minor = 0;
1330
1331 #if SUPPORT_PROFILE_GLSL
1332 if (!ctx->have_core_opengl)
1333 return; // everything's busted, give up.
1334
1335 #if PLATFORM_MACOSX
1336 // If running on Mac OS X <= 10.4, don't ever use GLSL, even if
1337 // the system claims it is available.
1338 if (!macosx_version_atleast(10, 5, 0))
1339 return;
1340 #endif
1341
1342 if ( ctx->have_opengl_2 ||
1343 ( ctx->have_GL_ARB_shader_objects &&
1344 ctx->have_GL_ARB_vertex_shader &&
1345 ctx->have_GL_ARB_fragment_shader &&
1346 ctx->have_GL_ARB_shading_language_100 ) )
1347 {
1348 // the GL2.0 and ARB enum is the same value.
1349 const GLenum enumval = GL_SHADING_LANGUAGE_VERSION;
1350 ctx->glGetError(); // flush any existing error state.
1351 const char *str = (const char *) ctx->glGetString(enumval);
1352 if (ctx->glGetError() == GL_INVALID_ENUM)
1353 str = NULL;
1354 if (strstr(str, "OpenGL ES GLSL "))
1355 str += 15;
1356 if (strstr(str, "ES "))
1357 str += 3;
1358 parse_opengl_version_str(str, &ctx->glsl_major, &ctx->glsl_minor);
1359 } // if
1360 #endif
1361 } // detect_glsl_version
1362
1363
1364 static int iswhitespace(const char ch)
1365 {
1366 switch (ch)
1367 {
1368 case ' ': case '\t': case '\r': case '\n': return 1;
1369 default: return 0;
1370 } // switch
1371 } // iswhitespace
1372
1373
1374 static void load_extensions(MOJOSHADER_glGetProcAddress lookup, void *d)
1375 {
1376 StringCache *exts = stringcache_create(ctx->malloc_fn, ctx->free_fn, ctx->malloc_data);
1377 if (!exts)
1378 {
1379 out_of_memory();
1380 return;
1381 } // if
1382
1383 ctx->have_core_opengl = 1;
1384 ctx->have_opengl_2 = 1;
1385 ctx->have_opengl_3 = 1;
1386 ctx->have_GL_ARB_vertex_program = 1;
1387 ctx->have_GL_ARB_fragment_program = 1;
1388 ctx->have_GL_NV_vertex_program2_option = 1;
1389 ctx->have_GL_NV_fragment_program2 = 1;
1390 ctx->have_GL_NV_vertex_program3 = 1;
1391 ctx->have_GL_NV_gpu_program4 = 1;
1392 ctx->have_GL_ARB_shader_objects = 1;
1393 ctx->have_GL_ARB_vertex_shader = 1;
1394 ctx->have_GL_ARB_fragment_shader = 1;
1395 ctx->have_GL_ARB_shading_language_100 = 1;
1396 ctx->have_GL_NV_half_float = 1;
1397 ctx->have_GL_ARB_half_float_vertex = 1;
1398 ctx->have_GL_OES_vertex_half_float = 1;
1399 ctx->have_GL_ARB_instanced_arrays = 1;
1400 ctx->have_GL_ARB_ES2_compatibility = 1;
1401 ctx->have_GL_ARB_gl_spirv = 1;
1402
1403 lookup_entry_points(lookup, d);
1404
1405 if (!ctx->have_core_opengl)
1406 set_error("missing basic OpenGL entry points");
1407 else
1408 {
1409 const char *str = (const char *) ctx->glGetString(GL_VERSION);
1410 if (strstr(str, "OpenGL ES "))
1411 {
1412 ctx->have_opengl_es = 1;
1413 str += 10;
1414 }
1415 parse_opengl_version_str(str, &ctx->opengl_major, &ctx->opengl_minor);
1416
1417 if (opengl_version_atleast(3, 0) && ctx->have_opengl_es)
1418 {
1419 ctx->have_opengl_es3 = 1;
1420 }
1421
1422 if ((ctx->have_opengl_3) && (opengl_version_atleast(3, 0)))
1423 {
1424 GLint i;
1425 GLint num_exts = 0;
1426 ctx->glGetIntegerv(GL_NUM_EXTENSIONS, &num_exts);
1427 for (i = 0; i < num_exts; i++)
1428 {
1429 if (!stringcache(exts, (const char *) ctx->glGetStringi(GL_EXTENSIONS, i)))
1430 out_of_memory();
1431 } // for
1432 } // if
1433 else
1434 {
1435 const char *str = (const char *) ctx->glGetString(GL_EXTENSIONS);
1436 const char *ext;
1437 ctx->have_opengl_3 = 0;
1438
1439 while (*str && iswhitespace(*str))
1440 str++;
1441 ext = str;
1442
1443 while (1)
1444 {
1445 const char ch = *str;
1446 if (ch && (!iswhitespace(ch)))
1447 {
1448 str++;
1449 continue;
1450 } // else if
1451
1452 if (str != ext)
1453 {
1454 if (!stringcache_len(exts, ext, (unsigned int) (str - ext)))
1455 {
1456 out_of_memory();
1457 break;
1458 } // if
1459 } // if
1460
1461 if (ch == '\0')
1462 break;
1463
1464 str++;
1465 while (*str && iswhitespace(*str))
1466 str++;
1467 ext = str;
1468 } // while
1469 } // else
1470 } // else
1471
1472 if ((ctx->have_opengl_2) && (!opengl_version_atleast(2, 0)))
1473 {
1474 ctx->have_opengl_2 = 0; // Not GL2! must have the ARB extensions!
1475
1476 // Force compatible ARB function pointers in...this keeps the code
1477 // cleaner when they are identical, so we don't have to if/else
1478 // every function call, but we definitely have the right entry
1479 // point. Be careful what you add here.
1480 // These may be NULL, btw.
1481 ctx->glUniform1i = ctx->glUniform1iARB;
1482 ctx->glUniform4fv = ctx->glUniform4fvARB;
1483 ctx->glUniform4iv = ctx->glUniform4ivARB;
1484 ctx->glDisableVertexAttribArray = ctx->glDisableVertexAttribArrayARB;
1485 ctx->glEnableVertexAttribArray = ctx->glEnableVertexAttribArrayARB;
1486 ctx->glVertexAttribPointer = ctx->glVertexAttribPointerARB;
1487 } // if
1488
1489 #define VERIFY_EXT(ext, major, minor) \
1490 ctx->have_##ext = verify_extension(#ext, ctx->have_##ext, exts, major, minor)
1491
1492 VERIFY_EXT(GL_ARB_vertex_program, -1, -1);
1493 VERIFY_EXT(GL_ARB_fragment_program, -1, -1);
1494 VERIFY_EXT(GL_ARB_shader_objects, -1, -1);
1495 VERIFY_EXT(GL_ARB_vertex_shader, -1, -1);
1496 VERIFY_EXT(GL_ARB_fragment_shader, -1, -1);
1497 VERIFY_EXT(GL_ARB_shading_language_100, -1, -1);
1498 VERIFY_EXT(GL_NV_vertex_program2_option, -1, -1);
1499 VERIFY_EXT(GL_NV_fragment_program2, -1, -1);
1500 VERIFY_EXT(GL_NV_vertex_program3, -1, -1);
1501 VERIFY_EXT(GL_NV_half_float, -1, -1);
1502 VERIFY_EXT(GL_ARB_half_float_vertex, 3, 0);
1503 VERIFY_EXT(GL_OES_vertex_half_float, -1, -1);
1504 VERIFY_EXT(GL_ARB_instanced_arrays, 3, 3);
1505 VERIFY_EXT(GL_ARB_ES2_compatibility, 4, 1);
1506 VERIFY_EXT(GL_ARB_gl_spirv, -1, -1);
1507
1508 #undef VERIFY_EXT
1509
1510 stringcache_destroy(exts);
1511
1512 detect_glsl_version();
1513 } // load_extensions
1514
1515
1516 static int valid_profile(const char *profile)
1517 {
1518 if (!ctx->have_core_opengl)
1519 return 0;
1520
1521 #define MUST_HAVE(p, x) \
1522 if (!ctx->have_##x) { set_error(#p " profile needs " #x); return 0; }
1523
1524 // we might actually _have_ maj.min, but forcibly disabled GLSL elsewhere.
1525 #define MUST_HAVE_GLSL(p, maj, min) \
1526 if (!glsl_version_atleast(maj, min)) { \
1527 set_error(#p " profile needs missing GLSL support"); return 0; \
1528 }
1529
1530 if (profile == NULL)
1531 {
1532 set_error("NULL profile");
1533 return 0;
1534 } // if
1535
1536 #if SUPPORT_PROFILE_ARB1
1537 else if (strcmp(profile, MOJOSHADER_PROFILE_ARB1) == 0)
1538 {
1539 MUST_HAVE(MOJOSHADER_PROFILE_ARB1, GL_ARB_vertex_program);
1540 MUST_HAVE(MOJOSHADER_PROFILE_ARB1, GL_ARB_fragment_program);
1541 } // else if
1542 #endif
1543
1544 #if SUPPORT_PROFILE_ARB1_NV
1545 else if (strcmp(profile, MOJOSHADER_PROFILE_NV2) == 0)
1546 {
1547 MUST_HAVE(MOJOSHADER_PROFILE_NV2, GL_ARB_vertex_program);
1548 MUST_HAVE(MOJOSHADER_PROFILE_NV2, GL_ARB_fragment_program);
1549 MUST_HAVE(MOJOSHADER_PROFILE_NV2, GL_NV_vertex_program2_option);
1550 MUST_HAVE(MOJOSHADER_PROFILE_NV2, GL_NV_fragment_program2);
1551 } // else if
1552
1553 else if (strcmp(profile, MOJOSHADER_PROFILE_NV3) == 0)
1554 {
1555 MUST_HAVE(MOJOSHADER_PROFILE_NV3, GL_ARB_vertex_program);
1556 MUST_HAVE(MOJOSHADER_PROFILE_NV3, GL_ARB_fragment_program);
1557 MUST_HAVE(MOJOSHADER_PROFILE_NV3, GL_NV_vertex_program3);
1558 MUST_HAVE(MOJOSHADER_PROFILE_NV3, GL_NV_fragment_program2);
1559 } // else if
1560
1561 else if (strcmp(profile, MOJOSHADER_PROFILE_NV4) == 0)
1562 {
1563 MUST_HAVE(MOJOSHADER_PROFILE_NV4, GL_NV_gpu_program4);
1564 } // else if
1565 #endif
1566
1567 #if SUPPORT_PROFILE_GLSPIRV
1568 else if (strcmp(profile, MOJOSHADER_PROFILE_GLSPIRV) == 0)
1569 {
1570 MUST_HAVE(MOJOSHADER_PROFILE_GLSPIRV, GL_ARB_ES2_compatibility);
1571 MUST_HAVE(MOJOSHADER_PROFILE_GLSPIRV, GL_ARB_gl_spirv);
1572 } // else if
1573 #endif
1574
1575 #if SUPPORT_PROFILE_GLSLES
1576 else if (strcmp(profile, MOJOSHADER_PROFILE_GLSLES3) == 0)
1577 {
1578 MUST_HAVE_GLSL(MOJOSHADER_PROFILE_GLSLES3, 3, 00);
1579 } // else if
1580 #endif
1581
1582 #if SUPPORT_PROFILE_GLSLES
1583 else if (strcmp(profile, MOJOSHADER_PROFILE_GLSLES) == 0)
1584 {
1585 MUST_HAVE_GLSL(MOJOSHADER_PROFILE_GLSLES, 1, 00);
1586 } // else if
1587 #endif
1588
1589 #if SUPPORT_PROFILE_GLSL120
1590 else if (strcmp(profile, MOJOSHADER_PROFILE_GLSL120) == 0)
1591 {
1592 MUST_HAVE_GLSL(MOJOSHADER_PROFILE_GLSL120, 1, 20);
1593 } // else if
1594 #endif
1595
1596 #if SUPPORT_PROFILE_GLSL
1597 else if (strcmp(profile, MOJOSHADER_PROFILE_GLSL) == 0)
1598 {
1599 MUST_HAVE_GLSL(MOJOSHADER_PROFILE_GLSL, 1, 10);
1600 } // else if
1601 #endif
1602
1603 else
1604 {
1605 set_error("unknown or unsupported profile");
1606 return 0;
1607 } // else
1608
1609 #undef MUST_HAVE
1610
1611 return 1;
1612 } // valid_profile
1613
1614
1615 static const char *profile_priorities[] = {
1616 #if SUPPORT_PROFILE_GLSPIRV
1617 MOJOSHADER_PROFILE_GLSPIRV,
1618 #endif
1619 #if SUPPORT_PROFILE_GLSL120
1620 MOJOSHADER_PROFILE_GLSL120,
1621 #endif
1622 #if SUPPORT_PROFILE_GLSL
1623 MOJOSHADER_PROFILE_GLSL,
1624 #endif
1625 #if SUPPORT_PROFILE_ARB1_NV
1626 MOJOSHADER_PROFILE_NV4,
1627 MOJOSHADER_PROFILE_NV3,
1628 MOJOSHADER_PROFILE_NV2,
1629 #endif
1630 #if SUPPORT_PROFILE_ARB1
1631 MOJOSHADER_PROFILE_ARB1,
1632 #endif
1633 };
1634
1635 int MOJOSHADER_glAvailableProfiles(MOJOSHADER_glGetProcAddress lookup,
1636 void *lookup_d,
1637 const char **profs, const int size,
1638 MOJOSHADER_malloc m, MOJOSHADER_free f,
1639 void *malloc_d)
1640 {
1641 int retval = 0;
1642 MOJOSHADER_glContext _ctx;
1643 MOJOSHADER_glContext *current_ctx = ctx;
1644
1645 if (m == NULL) m = MOJOSHADER_internal_malloc;
1646 if (f == NULL) f = MOJOSHADER_internal_free;
1647
1648 ctx = &_ctx;
1649 memset(ctx, '\0', sizeof (MOJOSHADER_glContext));
1650 ctx->malloc_fn = m;
1651 ctx->free_fn = f;
1652 ctx->malloc_data = malloc_d;
1653
1654 load_extensions(lookup, lookup_d);
1655
1656 #if SUPPORT_PROFILE_GLSLES3
1657 if (ctx->have_opengl_es3)
1658 {
1659 profs[0] = MOJOSHADER_PROFILE_GLSLES3;
1660 return 1;
1661 } // if
1662 #endif
1663
1664 #if SUPPORT_PROFILE_GLSLES
1665 if (ctx->have_opengl_es)
1666 {
1667 profs[0] = MOJOSHADER_PROFILE_GLSLES;
1668 return 1;
1669 } // if
1670 #endif
1671
1672 if (ctx->have_core_opengl)
1673 {
1674 size_t i;
1675 for (i = 0; i < STATICARRAYLEN(profile_priorities); i++)
1676 {
1677 const char *profile = profile_priorities[i];
1678 if (valid_profile(profile))
1679 {
1680 if (retval < size)
1681 profs[retval] = profile;
1682 retval++;
1683 } // if
1684 } // for
1685 } // if
1686
1687 ctx = current_ctx;
1688 return retval;
1689 } // MOJOSHADER_glAvailableProfiles
1690
1691
1692 const char *MOJOSHADER_glBestProfile(MOJOSHADER_glGetProcAddress gpa,
1693 void *lookup_d,
1694 MOJOSHADER_malloc m, MOJOSHADER_free f,
1695 void *malloc_d)
1696 {
1697 const char *prof[STATICARRAYLEN(profile_priorities)];
1698 const int avail = MOJOSHADER_glAvailableProfiles(gpa, lookup_d, prof,
1699 STATICARRAYLEN(prof),
1700 m, f, malloc_d);
1701 if (avail <= 0)
1702 {
1703 set_error("no profiles available");
1704 return NULL;
1705 } // if
1706
1707 return prof[0]; // profiles are sorted "best" to "worst."
1708 } // MOJOSHADER_glBestProfile
1709
1710
1711 MOJOSHADER_glContext *MOJOSHADER_glCreateContext(const char *profile,
1712 MOJOSHADER_glGetProcAddress lookup,
1713 void *lookup_d,
1714 MOJOSHADER_malloc m, MOJOSHADER_free f,
1715 void *malloc_d)
1716 {
1717 MOJOSHADER_glContext *retval = NULL;
1718 MOJOSHADER_glContext *current_ctx = ctx;
1719
1720 ctx = NULL;
1721
1722 if (m == NULL) m = MOJOSHADER_internal_malloc;
1723 if (f == NULL) f = MOJOSHADER_internal_free;
1724
1725 ctx = (MOJOSHADER_glContext *) m(sizeof (MOJOSHADER_glContext), malloc_d);
1726 if (ctx == NULL)
1727 {
1728 out_of_memory();
1729 goto init_fail;
1730 } // if
1731
1732 memset(ctx, '\0', sizeof (MOJOSHADER_glContext));
1733 ctx->malloc_fn = m;
1734 ctx->free_fn = f;
1735 ctx->malloc_data = malloc_d;
1736 snprintf(ctx->profile, sizeof (ctx->profile), "%s", profile);
1737
1738 load_extensions(lookup, lookup_d);
1739 if (!valid_profile(profile))
1740 goto init_fail;
1741
1742 #ifdef MOJOSHADER_XNA4_VERTEX_TEXTURES
1743 GLint maxTextures;
1744 GLint maxVertexTextures;
1745 ctx->glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextures);
1746 if (maxTextures > 20)
1747 maxTextures = 20;
1748 if (maxTextures > 16)
1749 maxVertexTextures = maxTextures - 16;
1750 else
1751 maxVertexTextures = 0;
1752 ctx->vertex_sampler_offset = maxTextures - maxVertexTextures;
1753 #endif
1754
1755 MOJOSHADER_glBindProgram(NULL);
1756
1757 // !!! FIXME: generalize this part.
1758 if (profile == NULL) {}
1759
1760 // We don't check SUPPORT_PROFILE_GLSPIRV here, since valid_profile() does.
1761 #if SUPPORT_PROFILE_GLSPIRV
1762 else if (strcmp(profile, MOJOSHADER_PROFILE_GLSPIRV) == 0)
1763 {
1764 ctx->profileMaxUniforms = impl_GLSL_MaxUniforms;
1765 ctx->profileCompileShader = impl_SPIRV_CompileShader;
1766 ctx->profileDeleteShader = impl_SPIRV_DeleteShader;
1767 ctx->profileDeleteProgram = impl_SPIRV_DeleteProgram;
1768 ctx->profileGetAttribLocation = impl_SPIRV_GetAttribLocation;
1769 ctx->profileGetUniformLocation = impl_SPIRV_GetUniformLocation;
1770 ctx->profileGetSamplerLocation = impl_SPIRV_GetSamplerLocation;
1771 ctx->profileLinkProgram = impl_SPIRV_LinkProgram;
1772 ctx->profileFinalInitProgram = impl_SPIRV_FinalInitProgram;
1773 ctx->profileUseProgram = impl_GLSL_UseProgram;
1774 ctx->profilePushConstantArray = impl_GLSL_PushConstantArray;
1775 ctx->profilePushUniforms = impl_GLSL_PushUniforms;
1776 ctx->profilePushSampler = impl_GLSL_PushSampler;
1777 ctx->profileMustPushConstantArrays = impl_GLSL_MustPushConstantArrays;
1778 ctx->profileMustPushSamplers = impl_GLSL_MustPushSamplers;
1779 ctx->profileToggleProgramPointSize = impl_REAL_ToggleProgramPointSize;
1780 } // if
1781 #endif
1782
1783 // We don't check SUPPORT_PROFILE_GLSL120/ES here, since valid_profile() does.
1784 #if SUPPORT_PROFILE_GLSL
1785 else if ( (strcmp(profile, MOJOSHADER_PROFILE_GLSL) == 0) ||
1786 (strcmp(profile, MOJOSHADER_PROFILE_GLSL120) == 0) ||
1787 (strcmp(profile, MOJOSHADER_PROFILE_GLSLES) == 0) ||
1788 (strcmp(profile, MOJOSHADER_PROFILE_GLSLES3) == 0) )
1789 {
1790 ctx->profileMaxUniforms = impl_GLSL_MaxUniforms;
1791 ctx->profileCompileShader = impl_GLSL_CompileShader;
1792 ctx->profileDeleteShader = impl_GLSL_DeleteShader;
1793 ctx->profileDeleteProgram = impl_GLSL_DeleteProgram;
1794 ctx->profileGetAttribLocation = impl_GLSL_GetAttribLocation;
1795 ctx->profileGetUniformLocation = impl_GLSL_GetUniformLocation;
1796 ctx->profileGetSamplerLocation = impl_GLSL_GetSamplerLocation;
1797 ctx->profileLinkProgram = impl_GLSL_LinkProgram;
1798 ctx->profileFinalInitProgram = impl_GLSL_FinalInitProgram;
1799 ctx->profileUseProgram = impl_GLSL_UseProgram;
1800 ctx->profilePushConstantArray = impl_GLSL_PushConstantArray;
1801 ctx->profilePushUniforms = impl_GLSL_PushUniforms;
1802 ctx->profilePushSampler = impl_GLSL_PushSampler;
1803 ctx->profileMustPushConstantArrays = impl_GLSL_MustPushConstantArrays;
1804 ctx->profileMustPushSamplers = impl_GLSL_MustPushSamplers;
1805 if (strcmp(profile, MOJOSHADER_PROFILE_GLSLES) == 0 || strcmp(profile, MOJOSHADER_PROFILE_GLSLES3) == 0)
1806 ctx->profileToggleProgramPointSize = impl_NOOP_ToggleProgramPointSize;
1807 else
1808 ctx->profileToggleProgramPointSize = impl_REAL_ToggleProgramPointSize;
1809 } // if
1810 #endif
1811
1812 // We don't check SUPPORT_PROFILE_ARB1_NV here, since valid_profile() does.
1813 #if SUPPORT_PROFILE_ARB1
1814 else if ( (strcmp(profile, MOJOSHADER_PROFILE_ARB1) == 0) ||
1815 (strcmp(profile, MOJOSHADER_PROFILE_NV2) == 0) ||
1816 (strcmp(profile, MOJOSHADER_PROFILE_NV3) == 0) ||
1817 (strcmp(profile, MOJOSHADER_PROFILE_NV4) == 0) )
1818 {
1819 ctx->profileMaxUniforms = impl_ARB1_MaxUniforms;
1820 ctx->profileCompileShader = impl_ARB1_CompileShader;
1821 ctx->profileDeleteShader = impl_ARB1_DeleteShader;
1822 ctx->profileDeleteProgram = impl_ARB1_DeleteProgram;
1823 ctx->profileGetAttribLocation = impl_ARB1_GetAttribLocation;
1824 ctx->profileGetUniformLocation = impl_ARB1_GetUniformLocation;
1825 ctx->profileGetSamplerLocation = impl_ARB1_GetSamplerLocation;
1826 ctx->profileLinkProgram = impl_ARB1_LinkProgram;
1827 ctx->profileFinalInitProgram = impl_ARB1_FinalInitProgram;
1828 ctx->profileUseProgram = impl_ARB1_UseProgram;
1829 ctx->profilePushConstantArray = impl_ARB1_PushConstantArray;
1830 ctx->profilePushUniforms = impl_ARB1_PushUniforms;
1831 ctx->profilePushSampler = impl_ARB1_PushSampler;
1832 ctx->profileMustPushConstantArrays = impl_ARB1_MustPushConstantArrays;
1833 ctx->profileMustPushSamplers = impl_ARB1_MustPushSamplers;
1834 ctx->profileToggleProgramPointSize = impl_REAL_ToggleProgramPointSize;
1835 } // if
1836 #endif
1837
1838 assert(ctx->profileMaxUniforms != NULL);
1839 assert(ctx->profileCompileShader != NULL);
1840 assert(ctx->profileDeleteShader != NULL);
1841 assert(ctx->profileDeleteProgram != NULL);
1842 assert(ctx->profileMaxUniforms != NULL);
1843 assert(ctx->profileGetAttribLocation != NULL);
1844 assert(ctx->profileGetUniformLocation != NULL);
1845 assert(ctx->profileGetSamplerLocation != NULL);
1846 assert(ctx->profileLinkProgram != NULL);
1847 assert(ctx->profileFinalInitProgram != NULL);
1848 assert(ctx->profileUseProgram != NULL);
1849 assert(ctx->profilePushConstantArray != NULL);
1850 assert(ctx->profilePushUniforms != NULL);
1851 assert(ctx->profilePushSampler != NULL);
1852 assert(ctx->profileMustPushConstantArrays != NULL);
1853 assert(ctx->profileMustPushSamplers != NULL);
1854 assert(ctx->profileToggleProgramPointSize != NULL);
1855
1856 retval = ctx;
1857 ctx = current_ctx;
1858 return retval;
1859
1860 init_fail:
1861 if (ctx != NULL)
1862 f(ctx, malloc_d);
1863 ctx = current_ctx;
1864 return NULL;
1865 } // MOJOSHADER_glCreateContext
1866
1867
1868 void MOJOSHADER_glMakeContextCurrent(MOJOSHADER_glContext *_ctx)
1869 {
1870 ctx = _ctx;
1871 } // MOJOSHADER_glMakeContextCurrent
1872
1873
1874 int MOJOSHADER_glMaxUniforms(MOJOSHADER_shaderType shader_type)
1875 {
1876 return ctx->profileMaxUniforms(shader_type);
1877 } // MOJOSHADER_glMaxUniforms
1878
1879
1880 MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
1881 const unsigned int bufsize,
1882 const MOJOSHADER_swizzle *swiz,
1883 const unsigned int swizcount,
1884 const MOJOSHADER_samplerMap *smap,
1885 const unsigned int smapcount)
1886 {
1887 MOJOSHADER_glShader *retval = NULL;
1888 GLuint shader = 0;
1889
1890 // This doesn't need a mainfn, since there's no GL lang that does.
1891 const MOJOSHADER_parseData *pd = MOJOSHADER_parse(ctx->profile, NULL,
1892 tokenbuf, bufsize,
1893 swiz, swizcount,
1894 smap, smapcount,
1895 ctx->malloc_fn,
1896 ctx->free_fn,
1897 ctx->malloc_data);
1898 if (pd->error_count > 0)
1899 {
1900 // !!! FIXME: put multiple errors in the buffer? Don't use
1901 // !!! FIXME: MOJOSHADER_glGetError() for this?
1902 set_error(pd->errors[0].error);
1903 goto compile_shader_fail;
1904 } // if
1905
1906 retval = (MOJOSHADER_glShader *) Malloc(sizeof (MOJOSHADER_glShader));
1907 if (retval == NULL)
1908 goto compile_shader_fail;
1909
1910 if (!ctx->profileCompileShader(pd, &shader))
1911 goto compile_shader_fail;
1912
1913 retval->parseData = pd;
1914 retval->handle = shader;
1915 retval->refcount = 1;
1916 return retval;
1917
1918 compile_shader_fail:
1919 MOJOSHADER_freeParseData(pd);
1920 Free(retval);
1921 if (shader != 0)
1922 ctx->profileDeleteShader(shader);
1923 return NULL;
1924 } // MOJOSHADER_glCompileShader
1925
1926
1927 void MOJOSHADER_glShaderAddRef(MOJOSHADER_glShader *shader)
1928 {
1929 if (shader != NULL)
1930 shader->refcount++;
1931 } // MOJOSHADER_glShaderAddRef
1932
1933
1934 const MOJOSHADER_parseData *MOJOSHADER_glGetShaderParseData(
1935 MOJOSHADER_glShader *shader)
1936 {
1937 return (shader != NULL) ? shader->parseData : NULL;
1938 } // MOJOSHADER_glGetShaderParseData
1939
1940
1941 static void shader_unref(MOJOSHADER_glShader *shader)
1942 {
1943 if (shader != NULL)
1944 {
1945 const uint32 refcount = shader->refcount;
1946 if (refcount > 1)
1947 shader->refcount--;
1948 else
1949 {
1950 ctx->profileDeleteShader(shader->handle);
1951 MOJOSHADER_freeParseData(shader->parseData);
1952 Free(shader);
1953 } // else
1954 } // if
1955 } // shader_unref
1956
1957
1958 static void program_unref(MOJOSHADER_glProgram *program)
1959 {
1960 if (program != NULL)
1961 {
1962 const uint32 refcount = program->refcount;
1963 if (refcount > 1)
1964 program->refcount--;
1965 else
1966 {
1967 ctx->profileDeleteProgram(program->handle);
1968 shader_unref(program->vertex);
1969 shader_unref(program->fragment);
1970 Free(program->vs_uniforms_float4);
1971 Free(program->vs_uniforms_int4);
1972 Free(program->vs_uniforms_bool);
1973 Free(program->ps_uniforms_float4);
1974 Free(program->ps_uniforms_int4);
1975 Free(program->ps_uniforms_bool);
1976 Free(program->uniforms);
1977 Free(program->attributes);
1978 Free(program);
1979 } // else
1980 } // if
1981 } // program_unref
1982
1983
1984 static void fill_constant_array(GLfloat *f, const int base, const int size,
1985 const MOJOSHADER_parseData *pd)
1986 {
1987 int i;
1988 int filled = 0;
1989 for (i = 0; i < pd->constant_count; i++)
1990 {
1991 const MOJOSHADER_constant *c = &pd->constants[i];
1992 if (c->type != MOJOSHADER_UNIFORM_FLOAT)
1993 continue;
1994 else if (c->index < base)
1995 continue;
1996 else if (c->index >= (base+size))
1997 continue;
1998 memcpy(&f[(c->index-base) * 4], &c->value.f, sizeof (c->value.f));
1999 filled++;
2000 } // for
2001
2002 assert(filled == size);
2003 } // fill_constant_array
2004
2005
2006 static int lookup_uniforms(MOJOSHADER_glProgram *program,
2007 MOJOSHADER_glShader *shader, int *bound)
2008 {
2009 const MOJOSHADER_parseData *pd = shader->parseData;
2010 const MOJOSHADER_shaderType shader_type = pd->shader_type;
2011 uint32 float4_count = 0;
2012 uint32 int4_count = 0;
2013 uint32 bool_count = 0;
2014 int i;
2015
2016 for (i = 0; i < pd->uniform_count; i++)
2017 {
2018 const MOJOSHADER_uniform *u = &pd->uniforms[i];
2019
2020 if (u->constant)
2021 {
2022 // only do constants once, at link time. These aren't changed ever.
2023 if (ctx->profileMustPushConstantArrays())
2024 {
2025 const int base = u->index;
2026 const int size = u->array_count;
2027 GLfloat *f = (GLfloat *) alloca(sizeof (GLfloat) * (size * 4));
2028 fill_constant_array(f, base, size, pd);
2029 if (!(*bound))
2030 {
2031 ctx->profileUseProgram(program);
2032 *bound = 1;
2033 } // if
2034 ctx->profilePushConstantArray(program, u, f);
2035 } // if
2036 } // if
2037
2038 else
2039 {
2040 const GLint loc = ctx->profileGetUniformLocation(program, shader, i);
2041 if (loc != -1) // -1 means it was optimized out, or failure.
2042 {
2043 const int regcount = u->array_count;
2044 UniformMap *map = &program->uniforms[program->uniform_count];
2045 map->shader_type = shader_type;
2046 map->uniform = u;
2047 map->location = (GLuint) loc;
2048 program->uniform_count++;
2049
2050 if (u->type == MOJOSHADER_UNIFORM_FLOAT)
2051 float4_count += regcount ? regcount : 1;
2052 else if (u->type == MOJOSHADER_UNIFORM_INT)
2053 int4_count += regcount ? regcount : 1;
2054 else if (u->type == MOJOSHADER_UNIFORM_BOOL)
2055 bool_count += regcount ? regcount : 1;
2056 else
2057 assert(0 && "Unexpected register type");
2058 } // if
2059 } // else
2060 } // for
2061
2062 if (shader_type == MOJOSHADER_TYPE_PIXEL)
2063 {
2064 for (i = 0; i < pd->sampler_count; i++)
2065 {
2066 if (pd->samplers[i].texbem)
2067 {
2068 float4_count += 2;
2069 program->texbem_count++;
2070 } // if
2071 } // for
2072 } // if
2073
2074 #define MAKE_ARRAY(typ, gltyp, siz, count) \
2075 if (count) { \
2076 const size_t buflen = sizeof (gltyp) * siz * count; \
2077 gltyp *ptr = (gltyp *) Malloc(buflen); \
2078 if (ptr == NULL) { \
2079 return 0; \
2080 } else if (shader_type == MOJOSHADER_TYPE_VERTEX) { \
2081 program->vs_uniforms_##typ = ptr; \
2082 program->vs_uniforms_##typ##_count = count; \
2083 } else if (shader_type == MOJOSHADER_TYPE_PIXEL) { \
2084 program->ps_uniforms_##typ = ptr; \
2085 program->ps_uniforms_##typ##_count = count; \
2086 } else { \
2087 assert(0 && "unsupported shader type"); \
2088 } \
2089 memset(ptr, '\0', buflen); \
2090 }
2091
2092 MAKE_ARRAY(float4, GLfloat, 4, float4_count);
2093 MAKE_ARRAY(int4, GLint, 4, int4_count);
2094 MAKE_ARRAY(bool, GLint, 1, bool_count);
2095
2096 #undef MAKE_ARRAY
2097
2098 return 1;
2099 } // lookup_uniforms
2100
2101
2102 static void lookup_samplers(MOJOSHADER_glProgram *program,
2103 MOJOSHADER_glShader *shader, int *bound)
2104 {
2105 const MOJOSHADER_parseData *pd = shader->parseData;
2106 const MOJOSHADER_sampler *s = pd->samplers;
2107 int i;
2108
2109 if ((pd->sampler_count == 0) || (!ctx->profileMustPushSamplers()))
2110 return; // nothing to do here, so don't bother binding, etc.
2111
2112 // Link up the Samplers. These never change after link time, since they
2113 // are meant to be constant texture unit ids and not textures.
2114
2115 if (!(*bound))
2116 {
2117 ctx->profileUseProgram(program);
2118 *bound = 1;
2119 } // if
2120
2121 for (i = 0; i < pd->sampler_count; i++)
2122 {
2123 const GLint loc = ctx->profileGetSamplerLocation(program, shader, i);
2124 if (loc >= 0) // maybe the Sampler was optimized out?
2125 {
2126 #ifdef MOJOSHADER_XNA4_VERTEX_TEXTURES
2127 if (pd->shader_type == MOJOSHADER_TYPE_VERTEX)
2128 ctx->profilePushSampler(loc, s[i].index + ctx->vertex_sampler_offset);
2129 else
2130 #endif
2131 ctx->profilePushSampler(loc, s[i].index);
2132 } // if
2133 } // for
2134 } // lookup_samplers
2135
2136
2137 // Right now, this just decides if we have to toggle pointsize support later.
2138 static void lookup_outputs(MOJOSHADER_glProgram *program,
2139 MOJOSHADER_glShader *shader)
2140 {
2141 if (shader == NULL)
2142 return;
2143
2144 const MOJOSHADER_parseData *pd = shader->parseData;
2145 int i;
2146
2147 for (i = 0; i < pd->output_count; i++)
2148 {
2149 if (pd->outputs[i].usage == MOJOSHADER_USAGE_POINTSIZE)
2150 {
2151 program->uses_pointsize = 1;
2152 break;
2153 } // if
2154 } // for
2155 } // lookup_outputs
2156
2157
2158 static int lookup_attributes(MOJOSHADER_glProgram *program)
2159 {
2160 int i;
2161 const MOJOSHADER_parseData *pd = program->vertex->parseData;
2162 const MOJOSHADER_attribute *a = pd->attributes;
2163
2164 for (i = 0; i < pd->attribute_count; i++)
2165 {
2166 const GLint loc = ctx->profileGetAttribLocation(program, i);
2167 if (loc >= 0) // maybe the Attribute was optimized out?
2168 {
2169 AttributeMap *map = &program->attributes[program->attribute_count];
2170 map->attribute = &a[i];
2171 map->location = loc;
2172 program->vertex_attrib_loc[map->attribute->usage][map->attribute->index] = loc;
2173 program->attribute_count++;
2174
2175 if (((size_t)loc) > STATICARRAYLEN(ctx->want_attr))
2176 {
2177 assert(0 && "Static array is too small."); // laziness fail.
2178 return 0;
2179 } // if
2180 } // if
2181 } // for
2182
2183 return 1;
2184 } // lookup_attributes
2185
2186
2187 // !!! FIXME: misnamed
2188 // build a list of indexes that need to be overwritten with constant values
2189 // when pushing a uniform array to the GL.
2190 static int build_constants_lists(MOJOSHADER_glProgram *program)
2191 {
2192 int i;
2193 const int count = program->uniform_count;
2194 for (i = 0; i < count; i++)
2195 {
2196 UniformMap *map = &program->uniforms[i];
2197 const MOJOSHADER_uniform *u = map->uniform;
2198 const int size = u->array_count;
2199
2200 assert(!u->constant);
2201
2202 if (size == 0)
2203 continue; // nothing to see here.
2204
2205 // only use arrays for 'c' registers.
2206 assert(u->type == MOJOSHADER_UNIFORM_FLOAT);
2207
2208 // !!! FIXME: deal with this.
2209 } // for
2210
2211 return 1;
2212 } // build_constants_lists
2213
2214
2215 MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
2216 MOJOSHADER_glShader *pshader)
2217 {
2218 int bound = 0;
2219
2220 if ((vshader == NULL) && (pshader == NULL))
2221 return NULL;
2222
2223 int numregs = 0;
2224 MOJOSHADER_glProgram *retval = NULL;
2225 const GLuint program = ctx->profileLinkProgram(vshader, pshader);
2226 if (program == 0)
2227 goto link_program_fail;
2228
2229 retval = (MOJOSHADER_glProgram *) Malloc(sizeof (MOJOSHADER_glProgram));
2230 if (retval == NULL)
2231 goto link_program_fail;
2232 memset(retval, '\0', sizeof (MOJOSHADER_glProgram));
2233 memset(retval->vertex_attrib_loc, 0xFF, sizeof(retval->vertex_attrib_loc));
2234
2235 numregs = 0;
2236 if (vshader != NULL) numregs += vshader->parseData->uniform_count;
2237 if (pshader != NULL) numregs += pshader->parseData->uniform_count;
2238 if (numregs > 0)
2239 {
2240 const size_t len = sizeof (UniformMap) * numregs;
2241 retval->uniforms = (UniformMap *) Malloc(len);
2242 if (retval->uniforms == NULL)
2243 goto link_program_fail;
2244 memset(retval->uniforms, '\0', len);
2245 } // if
2246
2247 retval->handle = program;
2248 retval->vertex = vshader;
2249 retval->fragment = pshader;
2250 retval->generation = ctx->generation - 1;
2251 retval->refcount = 1;
2252
2253 if (vshader != NULL)
2254 {
2255 if (vshader->parseData->attribute_count > 0)
2256 {
2257 const int count = vshader->parseData->attribute_count;
2258 const size_t len = sizeof (AttributeMap) * count;
2259 retval->attributes = (AttributeMap *) Malloc(len);
2260 if (retval->attributes == NULL)
2261 goto link_program_fail;
2262
2263 memset(retval->attributes, '\0', len);
2264 if (!lookup_attributes(retval))
2265 goto link_program_fail;
2266 } // if
2267
2268 if (!lookup_uniforms(retval, vshader, &bound))
2269 goto link_program_fail;
2270 lookup_samplers(retval, vshader, &bound);
2271 lookup_outputs(retval, vshader);
2272 vshader->refcount++;
2273 } // if
2274
2275 if (pshader != NULL)
2276 {
2277 if (!lookup_uniforms(retval, pshader, &bound))
2278 goto link_program_fail;
2279 lookup_samplers(retval, pshader, &bound);
2280 lookup_outputs(retval, pshader);
2281 pshader->refcount++;
2282 } // if
2283
2284 if (!build_constants_lists(retval))
2285 goto link_program_fail;
2286
2287 if (bound) // reset the old binding.
2288 ctx->profileUseProgram(ctx->bound_program);
2289
2290 ctx->profileFinalInitProgram(retval);
2291
2292 return retval;
2293
2294 link_program_fail:
2295 if (retval != NULL)
2296 {
2297 Free(retval->vs_uniforms_float4);
2298 Free(retval->vs_uniforms_int4);
2299 Free(retval->vs_uniforms_bool);
2300 Free(retval->ps_uniforms_float4);
2301 Free(retval->ps_uniforms_int4);
2302 Free(retval->ps_uniforms_bool);
2303 Free(retval->uniforms);
2304 Free(retval->attributes);
2305 Free(retval);
2306 } // if
2307
2308 if (program != 0)
2309 ctx->profileDeleteProgram(program);
2310
2311 if (bound)
2312 ctx->profileUseProgram(ctx->bound_program);
2313
2314 return NULL;
2315 } // MOJOSHADER_glLinkProgram
2316
2317
2318 static void update_enabled_arrays(void)
2319 {
2320 int highest_enabled = 0;
2321 int i;
2322
2323 // Enable/disable vertex arrays to match our needs.
2324 // this happens to work in both ARB1 and GLSL, but if something alien
2325 // shows up, we'll have to split these into profile*() functions.
2326 for (i = 0; i < ctx->max_attrs; i++)
2327 {
2328 const int want = (const int) ctx->want_attr[i];
2329 const int have = (const int) ctx->have_attr[i];
2330 if (want != have)
2331 {
2332 if (want)
2333 ctx->glEnableVertexAttribArray(i);
2334 else
2335 ctx->glDisableVertexAttribArray(i);
2336 ctx->have_attr[i] = want;
2337 } // if
2338
2339 if (want)
2340 highest_enabled = i + 1;
2341 } // for
2342
2343 ctx->max_attrs = highest_enabled; // trim unneeded iterations next time.
2344 } // update_enabled_arrays
2345
2346
2347 void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program)
2348 {
2349 if (program == ctx->bound_program)
2350 return; // nothing to do.
2351
2352 if (program != NULL)
2353 program->refcount++;
2354
2355 memset(ctx->want_attr, '\0', sizeof (ctx->want_attr[0]) * ctx->max_attrs);
2356
2357 // If no program bound, disable all arrays, in case we're switching to
2358 // fixed function pipeline. Otherwise, we try to minimize state changes
2359 // by toggling just the changed set of needed arrays in ProgramReady().
2360 if (program == NULL)
2361 update_enabled_arrays();
2362
2363 ctx->profileUseProgram(program);
2364 program_unref(ctx->bound_program);
2365 ctx->bound_program = program;
2366 } // MOJOSHADER_glBindProgram
2367
2368
2369 typedef struct
2370 {
2371 MOJOSHADER_glShader *vertex;
2372 MOJOSHADER_glShader *fragment;
2373 } BoundShaders;
2374
2375 static uint32 hash_shaders(const void *sym, void *data)
2376 {
2377 (void) data;
2378 const BoundShaders *s = (const BoundShaders *) sym;
2379 const uint32 v = (s->vertex) ? (uint32) s->vertex->handle : 0;
2380 const uint32 f = (s->fragment) ? (uint32) s->fragment->handle : 0;
2381 return ((v & 0xFFFF) << 16) | (f & 0xFFFF);
2382 } // hash_shaders
2383
2384 static int match_shaders(const void *_a, const void *_b, void *data)
2385 {
2386 (void) data;
2387 const BoundShaders *a = (const BoundShaders *) _a;
2388 const BoundShaders *b = (const BoundShaders *) _b;
2389
2390 const GLuint av = (a->vertex) ? a->vertex->handle : 0;
2391 const GLuint bv = (b->vertex) ? b->vertex->handle : 0;
2392 if (av != bv)
2393 return 0;
2394
2395 const GLuint af = (a->fragment) ? a->fragment->handle : 0;
2396 const GLuint bf = (b->fragment) ? b->fragment->handle : 0;
2397 if (af != bf)
2398 return 0;
2399
2400 return 1;
2401 } // match_shaders
2402
2403 static void nuke_shaders(const void *_ctx, const void *key, const void *value, void *data)
2404 {
2405 (void) _ctx;
2406 (void) data;
2407 Free((void *) key); // this was a BoundShaders struct.
2408 MOJOSHADER_glDeleteProgram((MOJOSHADER_glProgram *) value);
2409 } // nuke_shaders
2410
2411 void MOJOSHADER_glBindShaders(MOJOSHADER_glShader *v, MOJOSHADER_glShader *p)
2412 {
2413 if ((v == NULL) && (p == NULL))
2414 {
2415 MOJOSHADER_glBindProgram(NULL);
2416 return;
2417 } // if
2418
2419 // !!! FIXME: eventually support GL_EXT_separate_shader_objects.
2420 if (ctx->linker_cache == NULL)
2421 {
2422 ctx->linker_cache = hash_create(NULL, hash_shaders, match_shaders,
2423 nuke_shaders, 0, ctx->malloc_fn,
2424 ctx->free_fn, ctx->malloc_data);
2425
2426 if (ctx->linker_cache == NULL)
2427 {
2428 out_of_memory();
2429 return;
2430 } // if
2431 } // if
2432
2433 MOJOSHADER_glProgram *program = NULL;
2434 BoundShaders shaders;
2435 shaders.vertex = v;
2436 shaders.fragment = p;
2437
2438 const void *val = NULL;
2439 if (hash_find(ctx->linker_cache, &shaders, &val))
2440 program = (MOJOSHADER_glProgram *) val;
2441 else
2442 {
2443 program = MOJOSHADER_glLinkProgram(v, p);
2444 if (program == NULL)
2445 return;
2446
2447 BoundShaders *item = (BoundShaders *) Malloc(sizeof (BoundShaders));
2448 if (item == NULL)
2449 {
2450 MOJOSHADER_glDeleteProgram(program);
2451 return;
2452 } // if
2453
2454 memcpy(item, &shaders, sizeof (BoundShaders));
2455 if (hash_insert(ctx->linker_cache, item, program) != 1)
2456 {
2457 Free(item);
2458 MOJOSHADER_glDeleteProgram(program);
2459 out_of_memory();
2460 return;
2461 } // if
2462 } // else
2463
2464 assert(program != NULL);
2465 MOJOSHADER_glBindProgram(program);
2466 } // MOJOSHADER_glBindShaders
2467
2468
2469 void MOJOSHADER_glGetBoundShaders(MOJOSHADER_glShader **v,
2470 MOJOSHADER_glShader **p)
2471 {
2472 if (v != NULL)
2473 {
2474 if (ctx->bound_program != NULL)
2475 *v = ctx->bound_program->vertex;
2476 else
2477 *v = NULL;
2478 } // if
2479 if (p != NULL)
2480 {
2481 if (ctx->bound_program != NULL)
2482 *p = ctx->bound_program->fragment;
2483 else
2484 *p = NULL;
2485 } // if
2486 } // MOJOSHADER_glGetBoundShaders
2487
2488
2489 static inline uint minuint(const uint a, const uint b)
2490 {
2491 return ((a < b) ? a : b);
2492 } // minuint
2493
2494
2495 void MOJOSHADER_glSetVertexShaderUniformF(unsigned int idx, const float *data,
2496 unsigned int vec4n)
2497 {
2498 const uint maxregs = STATICARRAYLEN(ctx->vs_reg_file_f) / 4;
2499 if (idx < maxregs)
2500 {
2501 assert(sizeof (GLfloat) == sizeof (float));
2502 const uint cpy = (minuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
2503 memcpy(ctx->vs_reg_file_f + (idx * 4), data, cpy);
2504 ctx->generation++;
2505 } // if
2506 } // MOJOSHADER_glSetVertexShaderUniformF
2507
2508
2509 void MOJOSHADER_glGetVertexShaderUniformF(unsigned int idx, float *data,
2510 unsigned int vec4n)
2511 {
2512 const uint maxregs = STATICARRAYLEN(ctx->vs_reg_file_f) / 4;
2513 if (idx < maxregs)
2514 {
2515 assert(sizeof (GLfloat) == sizeof (float));
2516 const uint cpy = (minuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
2517 memcpy(data, ctx->vs_reg_file_f + (idx * 4), cpy);
2518 } // if
2519 } // MOJOSHADER_glGetVertexShaderUniformF
2520
2521
2522 void MOJOSHADER_glSetVertexShaderUniformI(unsigned int idx, const int *data,
2523 unsigned int ivec4n)
2524 {
2525 const uint maxregs = STATICARRAYLEN(ctx->vs_reg_file_i) / 4;
2526 if (idx < maxregs)
2527 {
2528 assert(sizeof (GLint) == sizeof (int));
2529 const uint cpy = (minuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
2530 memcpy(ctx->vs_reg_file_i + (idx * 4), data, cpy);
2531 ctx->generation++;
2532 } // if
2533 } // MOJOSHADER_glSetVertexShaderUniformI
2534
2535
2536 void MOJOSHADER_glGetVertexShaderUniformI(unsigned int idx, int *data,
2537 unsigned int ivec4n)
2538 {
2539 const uint maxregs = STATICARRAYLEN(ctx->vs_reg_file_i) / 4;
2540 if (idx < maxregs)
2541 {
2542 assert(sizeof (GLint) == sizeof (int));
2543 const uint cpy = (minuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
2544 memcpy(data, ctx->vs_reg_file_i + (idx * 4), cpy);
2545 } // if
2546 } // MOJOSHADER_glGetVertexShaderUniformI
2547
2548
2549 void MOJOSHADER_glSetVertexShaderUniformB(unsigned int idx, const int *data,
2550 unsigned int bcount)
2551 {
2552 const uint maxregs = STATICARRAYLEN(ctx->vs_reg_file_b) / 4;
2553 if (idx < maxregs)
2554 {
2555 uint8 *wptr = ctx->vs_reg_file_b + idx;
2556 uint8 *endptr = wptr + minuint(maxregs - idx, bcount);
2557 while (wptr != endptr)
2558 *(wptr++) = *(data++) ? 1 : 0;
2559 ctx->generation++;
2560 } // if
2561 } // MOJOSHADER_glSetVertexShaderUniformB
2562
2563
2564 void MOJOSHADER_glGetVertexShaderUniformB(unsigned int idx, int *data,
2565 unsigned int bcount)
2566 {
2567 const uint maxregs = STATICARRAYLEN(ctx->vs_reg_file_b) / 4;
2568 if (idx < maxregs)
2569 {
2570 uint8 *rptr = ctx->vs_reg_file_b + idx;
2571 uint8 *endptr = rptr + minuint(maxregs - idx, bcount);
2572 while (rptr != endptr)
2573 *(data++) = (int) *(rptr++);
2574 } // if
2575 } // MOJOSHADER_glGetVertexShaderUniformB
2576
2577
2578 void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
2579 unsigned int vec4n)
2580 {
2581 const uint maxregs = STATICARRAYLEN(ctx->ps_reg_file_f) / 4;
2582 if (idx < maxregs)
2583 {
2584 assert(sizeof (GLfloat) == sizeof (float));
2585 const uint cpy = (minuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
2586 memcpy(ctx->ps_reg_file_f + (idx * 4), data, cpy);
2587 ctx->generation++;
2588 } // if
2589 } // MOJOSHADER_glSetPixelShaderUniformF
2590
2591
2592 void MOJOSHADER_glGetPixelShaderUniformF(unsigned int idx, float *data,
2593 unsigned int vec4n)
2594 {
2595 const uint maxregs = STATICARRAYLEN(ctx->ps_reg_file_f) / 4;
2596 if (idx < maxregs)
2597 {
2598 assert(sizeof (GLfloat) == sizeof (float));
2599 const uint cpy = (minuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
2600 memcpy(data, ctx->ps_reg_file_f + (idx * 4), cpy);
2601 } // if
2602 } // MOJOSHADER_glGetPixelShaderUniformF
2603
2604
2605 void MOJOSHADER_glSetPixelShaderUniformI(unsigned int idx, const int *data,
2606 unsigned int ivec4n)
2607 {
2608 const uint maxregs = STATICARRAYLEN(ctx->ps_reg_file_i) / 4;
2609 if (idx < maxregs)
2610 {
2611 assert(sizeof (GLint) == sizeof (int));
2612 const uint cpy = (minuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
2613 memcpy(ctx->ps_reg_file_i + (idx * 4), data, cpy);
2614 ctx->generation++;
2615 } // if
2616 } // MOJOSHADER_glSetPixelShaderUniformI
2617
2618
2619 void MOJOSHADER_glGetPixelShaderUniformI(unsigned int idx, int *data,
2620 unsigned int ivec4n)
2621 {
2622 const uint maxregs = STATICARRAYLEN(ctx->ps_reg_file_i) / 4;
2623 if (idx < maxregs)
2624 {
2625 assert(sizeof (GLint) == sizeof (int));
2626 const uint cpy = (minuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
2627 memcpy(data, ctx->ps_reg_file_i + (idx * 4), cpy);
2628 } // if
2629 } // MOJOSHADER_glGetPixelShaderUniformI
2630
2631
2632 void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
2633 unsigned int bcount)
2634 {
2635 const uint maxregs = STATICARRAYLEN(ctx->ps_reg_file_b) / 4;
2636 if (idx < maxregs)
2637 {
2638 uint8 *wptr = ctx->ps_reg_file_b + idx;
2639 uint8 *endptr = wptr + minuint(maxregs - idx, bcount);
2640 while (wptr != endptr)
2641 *(wptr++) = *(data++) ? 1 : 0;
2642 ctx->generation++;
2643 } // if
2644 } // MOJOSHADER_glSetPixelShaderUniformB
2645
2646
2647 void MOJOSHADER_glGetPixelShaderUniformB(unsigned int idx, int *data,
2648 unsigned int bcount)
2649 {
2650 const uint maxregs = STATICARRAYLEN(ctx->ps_reg_file_b) / 4;
2651 if (idx < maxregs)
2652 {
2653 uint8 *rptr = ctx->ps_reg_file_b + idx;
2654 uint8 *endptr = rptr + minuint(maxregs - idx, bcount);
2655 while (rptr != endptr)
2656 *(data++) = (int) *(rptr++);
2657 } // if
2658 } // MOJOSHADER_glGetPixelShaderUniformB
2659
2660
2661 void MOJOSHADER_glMapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb,
2662 float **psf, int **psi, unsigned char **psb)
2663 {
2664 *vsf = ctx->vs_reg_file_f;
2665 *vsi = ctx->vs_reg_file_i;
2666 *vsb = ctx->vs_reg_file_b;
2667 *psf = ctx->ps_reg_file_f;
2668 *psi = ctx->ps_reg_file_i;
2669 *psb = ctx->ps_reg_file_b;
2670 } // MOJOSHADER_glMapUniformBufferMemory
2671
2672
2673 void MOJOSHADER_glUnmapUniformBufferMemory()
2674 {
2675 ctx->generation++;
2676 } // MOJOSHADER_glUnmapUniformBufferMemory
2677
2678
2679 static inline GLenum opengl_attr_type(const MOJOSHADER_attributeType type)
2680 {
2681 switch (type)
2682 {
2683 case MOJOSHADER_ATTRIBUTE_UNKNOWN: return GL_NONE; // oh well.
2684 case MOJOSHADER_ATTRIBUTE_BYTE: return GL_BYTE;
2685 case MOJOSHADER_ATTRIBUTE_UBYTE: return GL_UNSIGNED_BYTE;
2686 case MOJOSHADER_ATTRIBUTE_SHORT: return GL_SHORT;
2687 case MOJOSHADER_ATTRIBUTE_USHORT: return GL_UNSIGNED_SHORT;
2688 case MOJOSHADER_ATTRIBUTE_INT: return GL_INT;
2689 case MOJOSHADER_ATTRIBUTE_UINT: return GL_UNSIGNED_INT;
2690 case MOJOSHADER_ATTRIBUTE_FLOAT: return GL_FLOAT;
2691 case MOJOSHADER_ATTRIBUTE_DOUBLE: return GL_DOUBLE;
2692
2693 case MOJOSHADER_ATTRIBUTE_HALF_FLOAT:
2694 if (ctx->have_GL_NV_half_float)
2695 return GL_HALF_FLOAT_NV;
2696 else if (ctx->have_GL_ARB_half_float_vertex)
2697 return GL_HALF_FLOAT_ARB;
2698 else if (ctx->have_GL_OES_vertex_half_float)
2699 return GL_HALF_FLOAT_OES;
2700 break;
2701 } // switch
2702
2703 return GL_NONE; // oh well. Raises a GL error later.
2704 } // opengl_attr_type
2705
2706
2707 int MOJOSHADER_glGetVertexAttribLocation(MOJOSHADER_usage usage, int index)
2708 {
2709 if ((ctx->bound_program == NULL) || (ctx->bound_program->vertex == NULL))
2710 return -1;
2711
2712 return ctx->bound_program->vertex_attrib_loc[usage][index];
2713 } // MOJOSHADER_glGetVertexAttribLocation
2714
2715
2716 // !!! FIXME: shouldn't (index) be unsigned?
2717 void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
2718 int index, unsigned int size,
2719 MOJOSHADER_attributeType type,
2720 int normalized, unsigned int stride,
2721 const void *ptr)
2722 {
2723 if ((ctx->bound_program == NULL) || (ctx->bound_program->vertex == NULL))
2724 return;
2725
2726 const GLenum gl_type = opengl_attr_type(type);
2727 const GLboolean norm = (normalized) ? GL_TRUE : GL_FALSE;
2728 const GLint gl_index = ctx->bound_program->vertex_attrib_loc[usage][index];
2729
2730 if (gl_index == -1)
2731 return; // Nothing to do, this shader doesn't use this stream.
2732
2733 // this happens to work in both ARB1 and GLSL, but if something alien
2734 // shows up, we'll have to split these into profile*() functions.
2735 ctx->glVertexAttribPointer(gl_index, size, gl_type, norm, stride, ptr);
2736
2737 // flag this array as in use, so we can enable it later.
2738 ctx->want_attr[gl_index] = 1;
2739 if (ctx->max_attrs < (gl_index + 1))
2740 ctx->max_attrs = gl_index + 1;
2741 } // MOJOSHADER_glSetVertexAttribute
2742
2743
2744 // !!! FIXME: shouldn't (index) be unsigned?
2745 void MOJOSHADER_glSetVertexAttribDivisor(MOJOSHADER_usage usage,
2746 int index, unsigned int divisor)
2747 {
2748 assert(ctx->have_GL_ARB_instanced_arrays);
2749
2750 if ((ctx->bound_program == NULL) || (ctx->bound_program->vertex == NULL))
2751 return;
2752
2753 const GLint gl_index = ctx->bound_program->vertex_attrib_loc[usage][index];
2754
2755 if (gl_index == -1)
2756 return; // Nothing to do, this shader doesn't use this stream.
2757
2758 if (divisor != ctx->attr_divisor[gl_index])
2759 {
2760 ctx->glVertexAttribDivisorARB(gl_index, divisor);
2761 ctx->attr_divisor[gl_index] = divisor;
2762 } // if
2763 } // MOJOSHADER_glSetVertexAttribDivisor
2764
2765
2766 void MOJOSHADER_glSetLegacyBumpMapEnv(unsigned int sampler, float mat00,
2767 float mat01, float mat10, float mat11,
2768 float lscale, float loffset)
2769 {
2770 if ((sampler == 0) || (sampler > (MAX_TEXBEMS+1)))
2771 return;
2772
2773 GLfloat *dstf = ctx->texbem_state + (6 * (sampler-1));
2774 *(dstf++) = (GLfloat) mat00;
2775 *(dstf++) = (GLfloat) mat01;
2776 *(dstf++) = (GLfloat) mat10;
2777 *(dstf++) = (GLfloat) mat11;
2778 *(dstf++) = (GLfloat) lscale;
2779 *(dstf++) = (GLfloat) loffset;
2780 ctx->generation++;
2781 } // MOJOSHADER_glSetLegacyBumpMapEnv
2782
2783
2784 void MOJOSHADER_glProgramReady(void)
2785 {
2786 MOJOSHADER_glProgram *program = ctx->bound_program;
2787
2788 if (program == NULL)
2789 return; // nothing to do.
2790
2791 // Toggle vertex attribute arrays on/off, based on our needs.
2792 update_enabled_arrays();
2793
2794 if (program->uses_pointsize != ctx->pointsize_enabled)
2795 {
2796 ctx->profileToggleProgramPointSize(program->uses_pointsize);
2797 ctx->pointsize_enabled = program->uses_pointsize;
2798 } // if
2799
2800 // push Uniforms to the program from our register files...
2801 if ( ((program->uniform_count) || (program->texbem_count)) &&
2802 (program->generation != ctx->generation))
2803 {
2804 // vertex shader uniforms come first in program->uniforms array.
2805 const uint32 count = program->uniform_count;
2806 const GLfloat *srcf = ctx->vs_reg_file_f;
2807 const GLint *srci = ctx->vs_reg_file_i;
2808 const uint8 *srcb = ctx->vs_reg_file_b;
2809 MOJOSHADER_shaderType shader_type = MOJOSHADER_TYPE_VERTEX;
2810 GLfloat *dstf = program->vs_uniforms_float4;
2811 GLint *dsti = program->vs_uniforms_int4;
2812 GLint *dstb = program->vs_uniforms_bool;
2813 uint8 uniforms_changed = 0;
2814 uint32 i;
2815
2816 for (i = 0; i < count; i++)
2817 {
2818 UniformMap *map = &program->uniforms[i];
2819 const MOJOSHADER_shaderType uniform_shader_type = map->shader_type;
2820 const MOJOSHADER_uniform *u = map->uniform;
2821 const MOJOSHADER_uniformType type = u->type;
2822 const int index = u->index;
2823 const int size = u->array_count ? u->array_count : 1;
2824
2825 assert(!u->constant);
2826
2827 // Did we switch from vertex to pixel (to geometry, etc)?
2828 if (shader_type != uniform_shader_type)
2829 {
2830 // we start with vertex, move to pixel, then to geometry, etc.
2831 // The array should always be sorted as such.
2832 if (uniform_shader_type == MOJOSHADER_TYPE_PIXEL)
2833 {
2834 assert(shader_type == MOJOSHADER_TYPE_VERTEX);
2835 srcf = ctx->ps_reg_file_f;
2836 srci = ctx->ps_reg_file_i;
2837 srcb = ctx->ps_reg_file_b;
2838 dstf = program->ps_uniforms_float4;
2839 dsti = program->ps_uniforms_int4;
2840 dstb = program->ps_uniforms_bool;
2841 } // if
2842 else
2843 {
2844 // Should be ordered vertex, then pixel, then geometry.
2845 assert(0 && "Unexpected shader type");
2846 } // else
2847
2848 shader_type = uniform_shader_type;
2849 } // if
2850
2851 if (type == MOJOSHADER_UNIFORM_FLOAT)
2852 {
2853 const size_t count = 4 * size;
2854 const GLfloat *f = &srcf[index * 4];
2855 if (memcmp(dstf, f, sizeof (GLfloat) * count) != 0)
2856 {
2857 memcpy(dstf, f, sizeof (GLfloat) * count);
2858 uniforms_changed = 1;
2859 }
2860 dstf += count;
2861 } // if
2862 else if (type == MOJOSHADER_UNIFORM_INT)
2863 {
2864 const size_t count = 4 * size;
2865 const GLint *i = &srci[index * 4];
2866 if (memcmp(dsti, i, sizeof (GLint) * count) != 0)
2867 {
2868 memcpy(dsti, i, sizeof (GLint) * count);
2869 uniforms_changed = 1;
2870 } // if
2871 dsti += count;
2872 } // else if
2873 else if (type == MOJOSHADER_UNIFORM_BOOL)
2874 {
2875 const size_t count = size;
2876 const uint8 *b = &srcb[index];
2877 size_t i;
2878 for (i = 0; i < count; i++)
2879 if (dstb[i] != b[i])
2880 {
2881 dstb[i] = (GLint) b[i];
2882 uniforms_changed = 1;
2883 } // if
2884 dstb += count;
2885 } // else if
2886
2887 // !!! FIXME: set constants that overlap the array.
2888 } // for
2889
2890 assert((!program->texbem_count) || (program->fragment));
2891 if ((program->texbem_count) && (program->fragment))
2892 {
2893 const MOJOSHADER_parseData *pd = program->fragment->parseData;
2894 const int samp_count = pd->sampler_count;
2895 const MOJOSHADER_sampler *samps = pd->samplers;
2896 GLfloat *dstf = program->ps_uniforms_float4;
2897 int texbem_count = 0;
2898
2899 dstf += (program->ps_uniforms_float4_count * 4) -
2900 (program->texbem_count * 8);
2901
2902 assert(program->texbem_count <= MAX_TEXBEMS);
2903 for (i = 0; i < samp_count; i++)
2904 {
2905 if (samps[i].texbem)
2906 {
2907 assert(samps[i].index > 0);
2908 assert(samps[i].index <= MAX_TEXBEMS);
2909 memcpy(dstf, &ctx->texbem_state[6 * (samps[i].index-1)],
2910 sizeof (GLfloat) * 6);
2911 dstf[6] = 0.0f;
2912 dstf[7] = 0.0f;
2913 dstf += 8;
2914 texbem_count++;
2915 } // if
2916 } // for
2917
2918 assert(texbem_count == program->texbem_count);
2919 } // if
2920
2921 program->generation = ctx->generation;
2922
2923 if (uniforms_changed)
2924 ctx->profilePushUniforms();
2925 } // if
2926 } // MOJOSHADER_glProgramReady
2927
2928
2929 void MOJOSHADER_glProgramViewportInfo(int viewportW, int viewportH,
2930 int backbufferW, int backbufferH,
2931 int renderTargetBound)
2932 {
2933 int vposFlip[2];
2934
2935 /* The uniform is only going to exist if VPOS is used! */
2936 if (ctx->bound_program->ps_vpos_flip_loc != -1)
2937 {
2938 if (renderTargetBound)
2939 {
2940 vposFlip[0] = 1;
2941 vposFlip[1] = 0;
2942 } // if
2943 else
2944 {
2945 vposFlip[0] = -1;
2946 vposFlip[1] = backbufferH;
2947 } // else
2948 if ( (ctx->bound_program->current_vpos_flip[0] != vposFlip[0]) ||
2949 (ctx->bound_program->current_vpos_flip[1] != vposFlip[1]) )
2950 {
2951 ctx->glUniform2f(
2952 ctx->bound_program->ps_vpos_flip_loc,
2953 (float) vposFlip[0],
2954 (float) vposFlip[1]
2955 );
2956 ctx->bound_program->current_vpos_flip[0] = vposFlip[0];
2957 ctx->bound_program->current_vpos_flip[1] = vposFlip[1];
2958 } // if
2959 } // if
2960
2961 #ifdef MOJOSHADER_FLIP_RENDERTARGET
2962 if (ctx->bound_program->vs_flip_loc != -1)
2963 {
2964 /* Some compilers require that vpFlip be a float value, rather than int.
2965 * However, there's no real reason for it to be a float in the API, so we
2966 * do a cast in here. That's not so bad, right...?
2967 * -flibit
2968 */
2969 const int flip = renderTargetBound ? -1 : 1;
2970 if (flip != ctx->bound_program->current_flip)
2971 {
2972 ctx->glUniform1f(ctx->bound_program->vs_flip_loc, (float) flip);
2973 ctx->bound_program->current_flip = flip;
2974 } // if
2975 } // if
2976 #endif
2977 } // MOJOSHADER_glViewportInfo
2978
2979
2980 void MOJOSHADER_glDeleteProgram(MOJOSHADER_glProgram *program)
2981 {
2982 program_unref(program);
2983 } // MOJOSHADER_glDeleteProgram
2984
2985
2986 void MOJOSHADER_glDeleteShader(MOJOSHADER_glShader *shader)
2987 {
2988 // See if this was bound as an unlinked program anywhere...
2989 if (ctx->linker_cache)
2990 {
2991 const void *key = NULL;
2992 void *iter = NULL;
2993 int morekeys = hash_iter_keys(ctx->linker_cache, &key, &iter);
2994 while (morekeys)
2995 {
2996 const BoundShaders *shaders = (const BoundShaders *) key;
2997 // Do this here so we don't confuse the iteration by removing...
2998 morekeys = hash_iter_keys(ctx->linker_cache, &key, &iter);
2999 if ((shaders->vertex == shader) || (shaders->fragment == shader))
3000 {
3001 // Deletes the linked program, which will unref the shader.
3002 hash_remove(ctx->linker_cache, shaders, ctx);
3003 } // if
3004 } // while
3005 } // if
3006
3007 shader_unref(shader);
3008 } // MOJOSHADER_glDeleteShader
3009
3010
3011 void MOJOSHADER_glDestroyContext(MOJOSHADER_glContext *_ctx)
3012 {
3013 MOJOSHADER_glContext *current_ctx = ctx;
3014 ctx = _ctx;
3015 MOJOSHADER_glBindProgram(NULL);
3016 if (ctx->linker_cache)
3017 hash_destroy(ctx->linker_cache, ctx);
3018 lookup_entry_points(NULL, NULL); // !!! FIXME: is there a value to this?
3019 Free(ctx);
3020 ctx = ((current_ctx == _ctx) ? NULL : current_ctx);
3021 } // MOJOSHADER_glDestroyContext
3022
3023 // end of mojoshader_opengl.c ...
3024