Debug.cc 3.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/

#include <ACGL/ACGL.hh>
#include <ACGL/OpenGL/Debug.hh>
#include <ACGL/OpenGL/Tools.hh>
#include <ACGL/OpenGL/glloaders/extensions.hh>

using namespace std;
using namespace ACGL::Utils;

15 16
#include <sstream>

17 18 19 20 21 22 23 24 25 26
namespace ACGL{
namespace OpenGL{

const char *debugSourceName( GLenum _source )
{
    if (_source == GL_DEBUG_SOURCE_API)             return "API";
    if (_source == GL_DEBUG_SOURCE_WINDOW_SYSTEM)   return "Window System";
    if (_source == GL_DEBUG_SOURCE_SHADER_COMPILER) return "Shader Compiler";
    if (_source == GL_DEBUG_SOURCE_THIRD_PARTY)     return "Third Party";
    if (_source == GL_DEBUG_SOURCE_APPLICATION)     return "Application";
27 28
    if (_source == GL_DEBUG_SOURCE_OTHER)           return "Unknown";
    return "Unknown";
29 30 31 32 33 34 35
}

const char *debugTypeName( GLenum _type )
{
    if (_type == GL_DEBUG_TYPE_ERROR)               return "Error";
    if (_type == GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR) return "Deprecated Behavior";
    if (_type == GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR)  return "Undefined Behavior";
36 37
    if (_type == GL_DEBUG_TYPE_PORTABILITY)         return "Portability Issue";
    if (_type == GL_DEBUG_TYPE_PERFORMANCE)         return "Performance Issue";
38
    if (_type == GL_DEBUG_TYPE_MARKER)              return "Marker";
39 40
    if (_type == GL_DEBUG_TYPE_OTHER)               return "Issue";
    return "Issue";
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
}

const char *debugSeverityName( GLenum _type )
{
    if (_type == GL_DEBUG_SEVERITY_LOW)          return "low";
    if (_type == GL_DEBUG_SEVERITY_MEDIUM)       return "medium";
    if (_type == GL_DEBUG_SEVERITY_HIGH)         return "high";
    if (_type == GL_DEBUG_SEVERITY_NOTIFICATION) return "notification";
    return "unknown";
}

void ACGLRegisterDefaultDebugCallback()
{
    if (getOpenGLVersionNumber() >= 43 || ACGL_KHR_debug()) {
        GLint v;
        glGetIntegerv( GL_CONTEXT_FLAGS, &v );
        if ((v & GL_CONTEXT_FLAG_DEBUG_BIT) != 0) {
Robert Menzel's avatar
Robert Menzel committed
58
            debug() << "context was created with KHR_debug flag, register callback" << endl;
59
        } else {
60
            debug() << "context was created WITHOUT KHR_debug flag - registering a debug callback is possible but might not result in getting called even if errors occur!" << endl;
61 62
        }

63
        glDebugMessageCallback( ACGL_KHR_default_debug_callback, NULL );
64 65 66 67
    }
}

//! place a brakepoint in here to find the source of a problem!
68
void APIENTRY ACGL_KHR_default_debug_callback( GLenum _source, GLenum _type, GLuint _id, GLenum _severity, GLsizei _length, const GLchar *_message, const void *_userParam)
69
{
70 71 72 73
    ostream& stream = (_type == GL_DEBUG_TYPE_ERROR) ? static_cast<ostream&>(error()) : static_cast<ostream&>(debug());

    stream << debugSourceName(_source) << " " << debugTypeName(_type) << ": " << _message << " (id: " << _id << ", severity: " << debugSeverityName(_severity) << ")" << endl;

74 75 76 77 78 79
    // delete all errors to not create another error log for the same problem:
    while ( glGetError() != GL_NO_ERROR ) {}
}

} // OpenGL
} // ACGL