Skip to content

Instantly share code, notes, and snippets.

@liam-middlebrook
Last active September 4, 2024 04:21
Show Gist options
  • Save liam-middlebrook/c52b069e4be2d87a6d2f to your computer and use it in GitHub Desktop.
Save liam-middlebrook/c52b069e4be2d87a6d2f 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 *msg, const void *data)
{
char* _source;
char* _type;
char* _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 = "UNKNOWN";
break;
default:
_source = "UNKNOWN";
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 = "UNKNOWN";
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 = "UNKNOWN";
break;
}
printf("%d: %s of %s severity, raised from %s: %s\n",
id, _type, _severity, _source, msg);
}
// =============== 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);
@YF-Tung
Copy link

YF-Tung commented Aug 22, 2018

This is really helpful! Though I have to use GL_DEBUG_OUTPUT since GL_DEBUG_OUTPUT_SYNCHRONOUS seems not working (i don't know why).

@kaesaecracker
Copy link

Was really helpful for me too, thanks a lot!

GL_DEBUG_OUTPUT_SYNCHRONOUS did not work for me too, @liam-middlebrook maybe change that?

@Plasmoxy
Copy link

Plasmoxy commented May 7, 2020

I'm able to get some stack trace in Visual Studio by enabling both.

glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(GLDebugMessageCallback, nullptr);

After placing breakpoint and breaking, the stack trace gives me line number of a statement right after the GL error-causing statement.
With GL_DEBUG_OUTPUT only, breakpoints in visual studio don't work (as stated).

I was able to get it working with C++, GLEW and GLFW3, here's my version: https://gist.github.com/Plasmoxy/aec637b85e306f671339dcfd509efc82

@Asplund86
Copy link

THANK YOU!

@ontron66
Copy link

ontron66 commented May 8, 2022

Hello! Beginner programmer here... I don't understand how this part of the code works ?

char* _source;
char* _type;
char* _severity;

Shouldn't these be allocated, why are they just pointers ?

@HuskyNator
Copy link

I believe the "API" etc parts are already allocated, meaning the pointer just references those literals.
(actually not entirely sure in what part of memory (data segment or text segment?))

@XavilPergis
Copy link

Hello! Beginner programmer here... I don't understand how this part of the code works ?

char* _source;
char* _type;
char* _severity;

Shouldn't these be allocated, why are they just pointers ?

pointers are just pointers, they just refer to somewhere in memory. it doesn't matter if that memory was dynamically-allocated, or if it was just a variable living on the stack, or if it was built into the binary itself. in the case of string literals, they're guaranteed to be valid for the entire lifetime of the program, usually implemented as references into the read-only data section of the binary.

// this does not dynamically-allocate the string
char *myString1 = "hello!";

// this is equivalent to the code above
char *myString2; // note, the contents of this variable are initially undefined (could be a pointer that points anywhere)
myString2 = "hello!"; // ok, now it points to the contents of this string literal

// both of the branches assign a statically-allocated string to myString
char *myString3;
if (foo) { myString3 = "successfully foo'd!!"; }
else { myString3 = "aw, the foo failed... ;w;"; }

// and there is nothing to "clean up" at the end here! yay!

@Challanger524
Copy link

Challanger524 commented Feb 8, 2024

Forked and reformatted with more sane linting (83 lines vs 139) and variable naming, +const char *source_,...: https://gist.github.com/Challanger524/cdf90cf11809749363fb638646225773
Big thanks to original author @liam-middlebrook!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment