Skip to content

Commit

Permalink
Added MOJOSHADER_glGetError() function, filled in error state.
Browse files Browse the repository at this point in the history
--HG--
branch : trunk
  • Loading branch information
icculus committed Apr 27, 2008
1 parent 6814537 commit ef395e1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
24 changes: 24 additions & 0 deletions mojoshader.h
Expand Up @@ -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
Expand Down
33 changes: 27 additions & 6 deletions mojoshader_opengl.c
Expand Up @@ -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()...
Expand All @@ -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.

Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ef395e1

Please sign in to comment.