From 3d917fcbadac599d9acacb4624c62c169312582f Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Thu, 25 Oct 2018 00:07:01 +0100 Subject: [PATCH 1/6] Add basic OpenGL debug callback --- apps/openmw/engine.cpp | 4 +++ components/CMakeLists.txt | 2 +- components/misc/gldebug.cpp | 60 +++++++++++++++++++++++++++++++++++++ components/misc/gldebug.hpp | 16 ++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 components/misc/gldebug.cpp create mode 100644 components/misc/gldebug.hpp diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 2a4145c98..015ff635c 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -365,6 +366,7 @@ void OMW::Engine::createWindow(Settings::Manager& settings) checkSDLError(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8)); checkSDLError(SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0)); checkSDLError(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24)); + checkSDLError(SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG)); if (antialiasing > 0) { @@ -425,6 +427,8 @@ void OMW::Engine::createWindow(Settings::Manager& settings) camera->setGraphicsContext(graphicsWindow); camera->setViewport(0, 0, width, height); + mViewer->setRealizeOperation(new EnableGLDebugOperation()); + mViewer->realize(); mViewer->getEventQueue()->getCurrentEventState()->setWindowRectangle(0, 0, width, height); diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index d26a92d44..ec0e511ca 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -86,7 +86,7 @@ add_component_dir (esmterrain ) add_component_dir (misc - constants utf8stream stringops resourcehelpers rng messageformatparser weakcache + constants utf8stream stringops resourcehelpers rng messageformatparser weakcache gldebug ) add_component_dir (debug diff --git a/components/misc/gldebug.cpp b/components/misc/gldebug.cpp new file mode 100644 index 000000000..71d75fa73 --- /dev/null +++ b/components/misc/gldebug.cpp @@ -0,0 +1,60 @@ +#include "gldebug.hpp" + +#include + +// OpenGL constants not provided by OSG: +#define GL_DEBUG_OUTPUT 0x92E0 +#define GL_DEBUG_SEVERITY_MEDIUM 0x9147 +#define GL_DEBUG_TYPE_ERROR 0x824C + +void debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam) +{ + Log(Debug::Error) << message; +} + +void enableGLDebugExtension(unsigned int contextID) +{ + typedef void (GL_APIENTRY *DEBUGPROC)(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam); + typedef void (GL_APIENTRY *GLDebugMessageControlFunction)(GLenum source, GLenum type, GLenum severity, GLsizei count, const GLuint *ids, GLboolean enabled); + typedef void (GL_APIENTRY *GLDebugMessageCallbackFunction)(DEBUGPROC, const void* userParam); + + GLDebugMessageControlFunction glDebugMessageControl = nullptr; + GLDebugMessageCallbackFunction glDebugMessageCallback = nullptr; + + if (osg::isGLExtensionSupported(contextID, "GL_KHR_debug")) + { + osg::setGLExtensionFuncPtr(glDebugMessageCallback, "glDebugMessageCallback"); + osg::setGLExtensionFuncPtr(glDebugMessageControl, "glDebugMessageControl"); + } + else if (osg::isGLExtensionSupported(contextID, "GL_ARB_debug_output")) + { + osg::setGLExtensionFuncPtr(glDebugMessageCallback, "glDebugMessageCallbackARB"); + osg::setGLExtensionFuncPtr(glDebugMessageControl, "glDebugMessageControlARB"); + } + else if (osg::isGLExtensionSupported(contextID, "GL_AMD_debug_output")) + { + osg::setGLExtensionFuncPtr(glDebugMessageCallback, "glDebugMessageCallbackAMD"); + osg::setGLExtensionFuncPtr(glDebugMessageControl, "glDebugMessageControlAMD"); + } + + if (glDebugMessageCallback && glDebugMessageControl) + { + glEnable(GL_DEBUG_OUTPUT); + glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR, GL_DEBUG_SEVERITY_MEDIUM, 0, nullptr, true); + glDebugMessageCallback(debugCallback, nullptr); + + Log(Debug::Info) << "OpenGL debug callback attached."; + } +} + +EnableGLDebugOperation::EnableGLDebugOperation() : osg::GraphicsOperation("EnableGLDebugOperation", false) +{ +} + +void EnableGLDebugOperation::operator()(osg::GraphicsContext* graphicsContext) +{ + OpenThreads::ScopedLock lock(mMutex); + + unsigned int contextID = graphicsContext->getState()->getContextID(); + enableGLDebugExtension(contextID); +} diff --git a/components/misc/gldebug.hpp b/components/misc/gldebug.hpp new file mode 100644 index 000000000..2401a53a4 --- /dev/null +++ b/components/misc/gldebug.hpp @@ -0,0 +1,16 @@ +#ifndef OPENMW_COMPONENTS_MISC_GLDEBUG_H +#define OPENMW_COMPONENTS_MISC_GLDEBUG_H + +#include + +class EnableGLDebugOperation : public osg::GraphicsOperation +{ +public: + EnableGLDebugOperation(); + + virtual void operator()(osg::GraphicsContext* graphicsContext); + +private: + OpenThreads::Mutex mMutex; +}; +#endif From aaa3eedf990d4988c328c107d8d33c4c21483959 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Fri, 26 Oct 2018 15:09:08 +0100 Subject: [PATCH 2/6] Move gldebug from components/misc to components/debug --- components/{misc => debug}/gldebug.cpp | 0 components/{misc => debug}/gldebug.hpp | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename components/{misc => debug}/gldebug.cpp (100%) rename components/{misc => debug}/gldebug.hpp (100%) diff --git a/components/misc/gldebug.cpp b/components/debug/gldebug.cpp similarity index 100% rename from components/misc/gldebug.cpp rename to components/debug/gldebug.cpp diff --git a/components/misc/gldebug.hpp b/components/debug/gldebug.hpp similarity index 100% rename from components/misc/gldebug.hpp rename to components/debug/gldebug.hpp From d42c97685284df35a34d419d258ae15fc02ae96b Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Fri, 26 Oct 2018 15:11:50 +0100 Subject: [PATCH 3/6] Add detailed OpenGL debug messages --- components/debug/gldebug.cpp | 63 +++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/components/debug/gldebug.cpp b/components/debug/gldebug.cpp index 71d75fa73..52a1bb5a6 100644 --- a/components/debug/gldebug.cpp +++ b/components/debug/gldebug.cpp @@ -3,13 +3,66 @@ #include // OpenGL constants not provided by OSG: -#define GL_DEBUG_OUTPUT 0x92E0 -#define GL_DEBUG_SEVERITY_MEDIUM 0x9147 -#define GL_DEBUG_TYPE_ERROR 0x824C +#include void debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam) { - Log(Debug::Error) << message; + std::string srcStr; + switch (source) + { + case GL_DEBUG_SOURCE_API: + srcStr = "API"; + break; + case GL_DEBUG_SOURCE_WINDOW_SYSTEM: + srcStr = "WINDOW_SYSTEM"; + break; + case GL_DEBUG_SOURCE_SHADER_COMPILER: + srcStr = "SHADER_COMPILER"; + break; + case GL_DEBUG_SOURCE_THIRD_PARTY: + srcStr = "THIRD_PARTY"; + break; + case GL_DEBUG_SOURCE_APPLICATION: + srcStr = "APPLICATION"; + break; + case GL_DEBUG_SOURCE_OTHER: + srcStr = "OTHER"; + break; + default: + srcStr = "UNDEFINED"; + break; + } + + std::string typeStr; + + Debug::Level logSeverity = Debug::Warning; + switch (type) + { + case GL_DEBUG_TYPE_ERROR: + typeStr = "ERROR"; + logSeverity = Debug::Error; + break; + case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: + typeStr = "DEPRECATED_BEHAVIOR"; + break; + case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: + typeStr = "UNDEFINED_BEHAVIOR"; + break; + case GL_DEBUG_TYPE_PORTABILITY: + typeStr = "PORTABILITY"; + break; + case GL_DEBUG_TYPE_PERFORMANCE: + typeStr = "PERFORMANCE"; + break; + case GL_DEBUG_TYPE_OTHER: + typeStr = "OTHER"; + break; + default: + typeStr = "UNDEFINED"; + break; + } + + Log(logSeverity) << "OpenGL " << typeStr << " [" << srcStr << "]: " << message; } void enableGLDebugExtension(unsigned int contextID) @@ -45,6 +98,8 @@ void enableGLDebugExtension(unsigned int contextID) Log(Debug::Info) << "OpenGL debug callback attached."; } + else + Log(Debug::Error) << "Unable to attach OpenGL debug callback."; } EnableGLDebugOperation::EnableGLDebugOperation() : osg::GraphicsOperation("EnableGLDebugOperation", false) From ac18983f37733d0d93d2e6f64d8211c41678222f Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Fri, 26 Oct 2018 15:18:38 +0100 Subject: [PATCH 4/6] Finish gldebug location move --- apps/openmw/engine.cpp | 4 ++-- components/CMakeLists.txt | 4 ++-- components/debug/gldebug.hpp | 21 ++++++++++++--------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 015ff635c..96fac86bc 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -11,9 +11,9 @@ #include #include +#include #include -#include #include #include @@ -427,7 +427,7 @@ void OMW::Engine::createWindow(Settings::Manager& settings) camera->setGraphicsContext(graphicsWindow); camera->setViewport(0, 0, width, height); - mViewer->setRealizeOperation(new EnableGLDebugOperation()); + mViewer->setRealizeOperation(new Debug::EnableGLDebugOperation()); mViewer->realize(); diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index ec0e511ca..d57a61f46 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -86,11 +86,11 @@ add_component_dir (esmterrain ) add_component_dir (misc - constants utf8stream stringops resourcehelpers rng messageformatparser weakcache gldebug + constants utf8stream stringops resourcehelpers rng messageformatparser weakcache ) add_component_dir (debug - debugging debuglog + debugging debuglog gldebug ) IF(NOT WIN32 AND NOT APPLE) diff --git a/components/debug/gldebug.hpp b/components/debug/gldebug.hpp index 2401a53a4..77d6c82a8 100644 --- a/components/debug/gldebug.hpp +++ b/components/debug/gldebug.hpp @@ -1,16 +1,19 @@ -#ifndef OPENMW_COMPONENTS_MISC_GLDEBUG_H -#define OPENMW_COMPONENTS_MISC_GLDEBUG_H +#ifndef OPENMW_COMPONENTS_DEBUG_GLDEBUG_H +#define OPENMW_COMPONENTS_DEBUG_GLDEBUG_H #include -class EnableGLDebugOperation : public osg::GraphicsOperation +namespace Debug { -public: - EnableGLDebugOperation(); + class EnableGLDebugOperation : public osg::GraphicsOperation + { + public: + EnableGLDebugOperation(); - virtual void operator()(osg::GraphicsContext* graphicsContext); + virtual void operator()(osg::GraphicsContext* graphicsContext); -private: - OpenThreads::Mutex mMutex; -}; + private: + OpenThreads::Mutex mMutex; + }; +} #endif From e147f6fed9d9a2545c2aa2aa935ba0a08f4e7373 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Fri, 26 Oct 2018 15:19:31 +0100 Subject: [PATCH 5/6] Finsih gldebug move completely this time --- components/debug/gldebug.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/debug/gldebug.cpp b/components/debug/gldebug.cpp index 52a1bb5a6..2c5b38845 100644 --- a/components/debug/gldebug.cpp +++ b/components/debug/gldebug.cpp @@ -102,11 +102,11 @@ void enableGLDebugExtension(unsigned int contextID) Log(Debug::Error) << "Unable to attach OpenGL debug callback."; } -EnableGLDebugOperation::EnableGLDebugOperation() : osg::GraphicsOperation("EnableGLDebugOperation", false) +Debug::EnableGLDebugOperation::EnableGLDebugOperation() : osg::GraphicsOperation("EnableGLDebugOperation", false) { } -void EnableGLDebugOperation::operator()(osg::GraphicsContext* graphicsContext) +void Debug::EnableGLDebugOperation::operator()(osg::GraphicsContext* graphicsContext) { OpenThreads::ScopedLock lock(mMutex); From 6de1deeb2d0555480f2fbf56fe56ccc86ca8bc53 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Fri, 26 Oct 2018 15:25:12 +0100 Subject: [PATCH 6/6] Include gldebug attribution and licence --- components/debug/gldebug.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/components/debug/gldebug.cpp b/components/debug/gldebug.cpp index 2c5b38845..76e7a4bb9 100644 --- a/components/debug/gldebug.cpp +++ b/components/debug/gldebug.cpp @@ -1,3 +1,34 @@ +// This file is based heavily on code from https://github.com/ThermalPixel/osgdemos/blob/master/osgdebug/EnableGLDebugOperation.cpp +// The original licence is included below: +/* +Copyright (c) 2014, Andreas Klein +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation +and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The views and conclusions contained in the software and documentation are those +of the authors and should not be interpreted as representing official policies, +either expressed or implied, of the FreeBSD Project. +*/ + #include "gldebug.hpp" #include