Debug.cc 4.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/***********************************************************************
 * 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;

namespace ACGL{
namespace OpenGL{

#ifdef ACGL_EMULATE_KHR_DEBUG

GLDEBUGPROC currentDebugCallback = NULL;
const void *currentDebugUserData = NULL;

void acglDebugMessageInsert(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const char* buf)
{
    currentDebugCallback( source, type, id, severity, length, buf, currentDebugUserData );
}

void acglDebugMessageCallback(GLDEBUGPROC callback, const void* userParam)
{
    currentDebugCallback = callback;
    currentDebugUserData = userParam;
}

#else

void acglDebugMessageInsert(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const char* buf)
{
    glDebugMessageInsert( source,  type,  id,  severity,  length,  buf);
}

void acglDebugMessageCallback(GLDEBUGPROC callback, const void* userParam)
{
    glEnable( GL_DEBUG_OUTPUT_SYNCHRONOUS );
    glDebugMessageCallback( callback,  userParam);
}

#endif

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";
    if (_source == GL_DEBUG_SOURCE_OTHER)           return "Other";
    return "unknown";
}

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";
    if (_type == GL_DEBUG_TYPE_PORTABILITY)         return "Portability";
    if (_type == GL_DEBUG_TYPE_PERFORMANCE)         return "Performance";
    if (_type == GL_DEBUG_TYPE_OTHER)               return "Other";
    if (_type == GL_DEBUG_TYPE_MARKER)              return "Marker";
    return "unknown";
}

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()
{
#ifndef ACGL_EMULATE_KHR_DEBUG
    if (getOpenGLVersionNumber() >= 43 || ACGL_KHR_debug()) {
        GLint v;
        glGetIntegerv( GL_CONTEXT_FLAGS, &v );
        if ((v & GL_CONTEXT_FLAG_DEBUG_BIT) != 0) {
            debug() << "context was created with debug capabilities" << endl;
        } else {
            debug() << "context was created WITHOUT debug capabilities - registering a debug callback is possible but might not result in getting called even if errors occur!" << endl;
        }

        acglDebugMessageCallback( ACGL_KHR_default_debug_callback, NULL );
    }
#else
    // the emulation works with every GL version:
    debug() << "using emulated debug capabilities - might not catch everything!" << endl;
    acglDebugMessageCallback( ACGL_KHR_default_debug_callback, NULL );
#endif
}

//! place a brakepoint in here to find the source of a problem!
void ACGL_KHR_default_debug_callback( GLenum _source, GLenum _type, GLuint _id, GLenum _severity, GLsizei _length, const GLchar *_message, const void *_userParam)
{
    debug() << "<" << _id << "> " << debugSeverityName(_severity) << ": " << debugSourceName(_source) << " " << debugTypeName(_type) << " " << _message << endl;

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

} // OpenGL
} // ACGL