Search code examples
c++visual-studiodebuggingopenglopengl-3

MSVC __debugbreak() when used with openGL error callback produces no callstack


I'm using openGL's more recent glDebugMessageCallback convention to help with my openGL error handling. What I want to be able to do is have a way to see what function causes the openGL error. I figured the only way with the callback method would be to insert a breakpoint into my callback function so that when an error is generated within visual studio I can go back and check the call stack to see exactly what function caused the issue:

void GLAPIENTRY MyOpenGLErrorCallbackFunc(GLenum source, GLenum debugErrorType, GLuint errorID, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
{
    switch(debugErrorType)
    {
        case GL_DEBUG_TYPE_ERROR:
        {
            BGZ_CONSOLE("GL Type error: %s\nGL error id: %i\n", message, errorID);
#if _MSC_VER
            __debugbreak();
#endif
        }break;

        case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
        {
            BGZ_CONSOLE("GL deprecated gl function usage error: %s\nGL error id: %i\n", message, errorID);
#if _MSC_VER
            __debugbreak();
#endif
        }break;

        case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
        {
            BGZ_CONSOLE("GL undefined behavior error: %s\nGL error id: %i\n", message, errorID);

#if _MSC_VER
            __debugbreak();
#endif
        }break;
    };
};

However, when I try and test this by passing an invalid enum to one of the openGL functions, while the program does break, all my call stack shows is:

myProgram.exe!MyOpenGLErrorCallbackFunc(GLenum source, GLenum debugErrorType, GLuint errorID, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
[External code]

so there is no execution tree of my code to look through. Is there a way to get this to work?


Solution

  • You have to enable synchronous debug output:

    glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
    

    If the Debug Output is produced asynchronously, then the debug callback function may be called from a thread other than that in which the commands are execute. See Logging and glEnable.