CGVIS Utils
Loading...
Searching...
No Matches
GLDebug.h File Reference

Minimal OpenGL error utilities, exception type, and a debug GL() wrapper macro. More...

#include <iostream>
#include <exception>
Include dependency graph for GLDebug.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  GLException
 Exception used for OpenGL-related failures. More...
struct  Dimensions
 Simple pixel/viewport dimensions with an aspect ratio helper. More...

Functions

std::string errorString (GLenum glerr)
 Convert a GL error enum to a short string (e.g., "GL_INVALID_VALUE").
void checkAndThrow ()
 Throw a GLException if glGetError() reports an error.
void checkAndThrowShader (GLuint shader)
 Check a shader object's compile status and throw with its log on error.
void checkAndThrowProgram (GLuint program)
 Check a program object's link status and throw with its log on error.

Debug-only error reporting support

#define MAX_GL_ERROR_COUNT   10
 Maximum number of consecutive glGetError() iterations before bailing.
#define GL(stmt)
 Debug wrapper that flushes and reports GL errors around a statement.
void errorOut (const std::string &statement, const std::string &location, uint32_t line, const std::string &file, uint32_t errnum)
 Internal printer used by the GL() macro to emit formatted errors.

Detailed Description

Minimal OpenGL error utilities, exception type, and a debug GL() wrapper macro.

Declares a lightweight OpenGL error reporting facility:

Note
When NDEBUG is defined, GL(stmt) compiles down to a plain stmt without extra error checks.

Macro Definition Documentation

◆ GL

#define GL ( stmt)
Value:
do { \
GLenum glerr; \
uint32_t counter = 0; \
while((glerr = glGetError()) != GL_NO_ERROR) { \
errorOut(#stmt, "before", __LINE__, __FILE__, \
static_cast<uint32_t>(glerr)); \
counter++; \
if (counter > MAX_GL_ERROR_COUNT) break; \
} \
stmt; \
counter = 0; \
while((glerr = glGetError()) != GL_NO_ERROR) { \
errorOut(#stmt, "in", __LINE__, __FILE__, \
static_cast<uint32_t>(glerr)); \
counter++; \
if (counter > MAX_GL_ERROR_COUNT) break; \
} \
} while(0)
#define MAX_GL_ERROR_COUNT
Maximum number of consecutive glGetError() iterations before bailing.
Definition GLDebug.h:76

Debug wrapper that flushes and reports GL errors around a statement.

Usage:

GL(glBindTexture(GL_TEXTURE_2D, id));
#define GL(stmt)
Debug wrapper that flushes and reports GL errors around a statement.
Definition GLDebug.h:101

In debug builds, existing GL errors are drained and reported ("before"), the statement is executed, then new errors are drained and reported ("in"). In release builds (NDEBUG defined), this expands to the bare statement.

◆ MAX_GL_ERROR_COUNT

#define MAX_GL_ERROR_COUNT   10

Maximum number of consecutive glGetError() iterations before bailing.

Protects against pathological drivers/contexts where querying the error state itself may produce errors or never clears.

Function Documentation

◆ checkAndThrow()

void checkAndThrow ( )

Throw a GLException if glGetError() reports an error.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ checkAndThrowProgram()

void checkAndThrowProgram ( GLuint program)

Check a program object's link status and throw with its log on error.

Parameters
programProgram object name returned by glCreateProgram().

◆ checkAndThrowShader()

void checkAndThrowShader ( GLuint shader)

Check a shader object's compile status and throw with its log on error.

Parameters
shaderShader object name returned by glCreateShader().

◆ errorOut()

void errorOut ( const std::string & statement,
const std::string & location,
uint32_t line,
const std::string & file,
uint32_t errnum )

Internal printer used by the GL() macro to emit formatted errors.

Parameters
statementThe stringified GL statement being executed.
locationOne of "before" or "in" indicating when the error occurred.
lineSource line number.
fileSource file name.
errnumNumeric GL error code.
Here is the call graph for this function:

◆ errorString()

std::string errorString ( GLenum glerr)

Convert a GL error enum to a short string (e.g., "GL_INVALID_VALUE").

Parameters
glerrError code returned by glGetError().
Returns
Human-readable token for common error codes or "Unknown Error".
Here is the caller graph for this function: