Added MOJOSHADER_glGetError() function, filled in error state. trunk
authorRyan C. Gordon <icculus@icculus.org>
Sun, 27 Apr 2008 05:01:18 -0400
branchtrunk
changeset 219 61167fc96790
parent 218 d3fac270bca1
child 220 df5ea69833d5
Added MOJOSHADER_glGetError() function, filled in error state.
mojoshader.h
mojoshader_opengl.c
--- a/mojoshader.h	Sun Apr 27 04:59:30 2008 -0400
+++ b/mojoshader.h	Sun Apr 27 05:01:18 2008 -0400
@@ -372,6 +372,30 @@
                       void *(*lookup)(const char *fnname),
                       MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
 
+/*
+ * Get any error state we might have picked up. MojoShader will NOT call
+ *  glGetError() internally, but there are other errors we can pick up,
+ *  such as failed shader compilation, etc.
+ *
+ * Returns a human-readable string. This string is for debugging purposes, and
+ *  not guaranteed to be localized, coherent, or user-friendly in any way.
+ *  It's for programmers!
+ *
+ * The latest error may remain between calls. New errors replace any existing
+ *  error. Don't check this string for a sign that an error happened, check
+ *  return codes instead and use this for explanation when debugging.
+ *
+ * Do not free the returned string: it's a pointer to a static internal
+ *  buffer. Do not keep the pointer around, either, as it's likely to become
+ *  invalid as soon as you call into MojoShader again.
+ *
+ * This is safe to call even if MOJOSHADER_glInit() failed.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ */
+const char *MOJOSHADER_glGetError(void);
 
 /*
  * "Shaders" refer to individual vertex or pixel programs, and are created
--- a/mojoshader_opengl.c	Sun Apr 27 04:59:30 2008 -0400
+++ b/mojoshader_opengl.c	Sun Apr 27 05:01:18 2008 -0400
@@ -65,6 +65,14 @@
 // GL stuff...
 static MOJOSHADER_glProgram *bound_program = NULL;
 
+// Error state...
+static char error_buffer[1024] = { '\0' };
+
+static void set_error(const char *str)
+{
+    snprintf(error_buffer, sizeof (error_buffer), "%s", str);
+} // set_error
+
 
 // #define this to force app to supply an allocator, so there's no reference
 //  to the C runtime's malloc() and free()...
@@ -87,14 +95,25 @@
 } // Free
 
 
+const char *MOJOSHADER_glGetError(void)
+{
+    return error_buffer;
+} // MOJOSHADER_glGetError
+
+
 int MOJOSHADER_glInit(const char *_profile,
                       void *(*lookup)(const char *fnname),
                       MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
 {
+    error_buffer[0] = '\0';
+
     if (strcmp(_profile, MOJOSHADER_PROFILE_GLSL) != 0)
         profile = MOJOSHADER_PROFILE_GLSL;
     else
+    {
+        set_error("unknown profile");
         return 0;
+    } // else
 
     // !!! FIXME: lookup glGetString(), check extensions.
 
@@ -126,7 +145,10 @@
                                                       bufsize, malloc_fn,
                                                       free_fn, malloc_data);
     if (pd->error != NULL)
+    {
+        set_error(pd->error);
         goto compile_shader_fail;
+    } // if
 
     retval = (MOJOSHADER_glShader *) Malloc(sizeof (MOJOSHADER_glShader));
     if (retval == NULL)
@@ -143,10 +165,9 @@
 
     if (!ok)
     {
-        GLcharARB err[1024];
         GLsizei len = 0;
-        pglGetInfoLogARB(shader, sizeof (err), &len, err);
-        //printf("FAIL: %s glsl compile: %s\n", fname, err);
+        pglGetInfoLogARB(shader, sizeof (error_buffer), &len,
+                         (GLcharARB *) error_buffer);
         goto compile_shader_fail;
     } // if
 
@@ -262,10 +283,9 @@
     pglGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &ok);
     if (!ok)
     {
-        GLcharARB err[1024];
         GLsizei len = 0;
-        pglGetInfoLogARB(program, sizeof (err), &len, err);
-        //printf("FAIL: %s glsl link: %s\n", fname, err);
+        pglGetInfoLogARB(shader, sizeof (error_buffer), &len,
+                         (GLcharARB *) error_buffer);
         goto link_program_fail;
     } // if
 
@@ -568,6 +588,7 @@
     malloc_fn = NULL;
     free_fn = NULL;
     malloc_data = NULL;
+    error_buffer[0] = '\0';
 
     // !!! FIXME: NULL entry points.
 } // MOJOSHADER_glDeinit