From ef395e1f3cb2cdbeda3ca73c30892886de592d1f Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sun, 27 Apr 2008 05:01:18 -0400 Subject: [PATCH] Added MOJOSHADER_glGetError() function, filled in error state. --HG-- branch : trunk --- mojoshader.h | 24 ++++++++++++++++++++++++ mojoshader_opengl.c | 33 +++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/mojoshader.h b/mojoshader.h index 5465eafc..add6843a 100644 --- a/mojoshader.h +++ b/mojoshader.h @@ -372,6 +372,30 @@ int MOJOSHADER_glInit(const char *profile, 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 diff --git a/mojoshader_opengl.c b/mojoshader_opengl.c index d2057235..b5e00b2c 100644 --- a/mojoshader_opengl.c +++ b/mojoshader_opengl.c @@ -65,6 +65,14 @@ static uint8 ps_register_file_b[2047]; // 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 @@ static inline void Free(void *ptr) } // 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 @@ MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf, 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 @@ MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf, 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 @@ MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader, 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 @@ void MOJOSHADER_glDeinit(void) malloc_fn = NULL; free_fn = NULL; malloc_data = NULL; + error_buffer[0] = '\0'; // !!! FIXME: NULL entry points. } // MOJOSHADER_glDeinit