Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Challanger524/cdf90cf11809749363fb638646225773 to your computer and use it in GitHub Desktop.
Save Challanger524/cdf90cf11809749363fb638646225773 to your computer and use it in GitHub Desktop.
GL Debug Output Message Callback Guide
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or distribute
// this software, either in source code form or as a compiled binary, for any
// purpose, commercial or non-commercial, and by any means.
//
// In jurisdictions that recognize copyright laws, the author or authors of this
// software dedicate any and all copyright interest in the software to the
// public domain. We make this dedication for the benefit of the public at large
// and to the detriment of our heirs and successors. We intend this dedication
// to be an overt act of relinquishment in perpetuity of all present and future
// rights to this software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>
// REQUIREMENTS: OpenGL version with the KHR_debug extension available.
// Callback function for printing debug statements
void APIENTRY GLDebugMessageCallback(GLenum source, GLenum type, GLuint id,
GLenum severity, GLsizei length,
const GLchar *message, const void *param)
{
const char *source_, *type_, *severity_;
switch (source)
{
case GL_DEBUG_SOURCE_API: source_ = "API"; break;
case GL_DEBUG_SOURCE_WINDOW_SYSTEM: source_ = "WINDOW_SYSTEM"; break;
case GL_DEBUG_SOURCE_SHADER_COMPILER: source_ = "SHADER_COMPILER"; break;
case GL_DEBUG_SOURCE_THIRD_PARTY: source_ = "THIRD_PARTY"; break;
case GL_DEBUG_SOURCE_APPLICATION: source_ = "APPLICATION"; break;
case GL_DEBUG_SOURCE_OTHER: source_ = "OTHER"; break;
default: source_ = "<SOURCE>"; break;
}
switch (type)
{
case GL_DEBUG_TYPE_ERROR: type_ = "ERROR"; break;
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: type_ = "DEPRECATED_BEHAVIOR"; break;
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: type_ = "UDEFINED_BEHAVIOR"; break;
case GL_DEBUG_TYPE_PORTABILITY: type_ = "PORTABILITY"; break;
case GL_DEBUG_TYPE_PERFORMANCE: type_ = "PERFORMANCE"; break;
case GL_DEBUG_TYPE_OTHER: type_ = "OTHER"; break;
case GL_DEBUG_TYPE_MARKER: type_ = "MARKER"; break;
default: type_ = "<TYPE>"; break;
}
switch (severity)
{
case GL_DEBUG_SEVERITY_HIGH: severity_ = "HIGH"; break;
case GL_DEBUG_SEVERITY_MEDIUM: severity_ = "MEDIUM"; break;
case GL_DEBUG_SEVERITY_LOW: severity_ = "LOW"; break;
case GL_DEBUG_SEVERITY_NOTIFICATION: severity_ = "NOTIFICATION"; break;
default: severity_ = "<SEVERITY>"; break;
}
printf("%d: GL %s %s (%s): %s\n",
id, severity_, type_, source_, message);
}
// =============== INIT DEBUG OUTPUT ================
// The following function calls should be made directly after OpenGL
// initialization.
// Enable the debugging layer of OpenGL
//
// GL_DEBUG_OUTPUT - Faster version but not useful for breakpoints
// GL_DEBUG_OUTPUT_SYNCHRONUS - Callback is in sync with errors, so a breakpoint
// can be placed on the callback in order to get a stacktrace for the GL error.
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
// Set the function that will be triggered by the callback, the second parameter
// is the data parameter of the callback, it can be useful for different
// contexts but isn't necessary for our simple use case.
glDebugMessageCallback(GLDebugMessageCallback, NULL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment