From dfca605d63b5e267d5c8306bec8f558eb2a5af52 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Fri, 30 Jul 2021 15:37:03 +0200 Subject: [PATCH] Add the optional ability to print a stacktrace with an OpenGL error (closes #1700) --- include/openspace/engine/configuration.h | 1 + openspace.cfg | 1 + src/engine/configuration.cpp | 8 ++++++++ src/engine/openspaceengine.cpp | 10 ++++++++++ tests/test_configuration.cpp | 4 ++++ 5 files changed, 24 insertions(+) diff --git a/include/openspace/engine/configuration.h b/include/openspace/engine/configuration.h index daf0ac7ee4..f20b2d8900 100644 --- a/include/openspace/engine/configuration.h +++ b/include/openspace/engine/configuration.h @@ -108,6 +108,7 @@ struct Configuration { struct OpenGLDebugContext { bool isActive = false; + bool printStacktrace = false; bool isSynchronous = true; struct IdentifierFilter { std::string type; diff --git a/openspace.cfg b/openspace.cfg index b1afa57fd7..6828473069 100644 --- a/openspace.cfg +++ b/openspace.cfg @@ -238,6 +238,7 @@ ScreenSpaceRotation = { 0.0, 0.0, 0.0 } OpenGLDebugContext = { Activate = true, + PrintStacktrace = false, FilterIdentifier = { { Type = "Other", Source = "API", Identifier = 131185 }, -- API_ID_RECOMPILE_FRAGMENT_SHADER performance warning has been generated. Fragment shader recompiled due to state change diff --git a/src/engine/configuration.cpp b/src/engine/configuration.cpp index ad7b113f67..454ee1f6a3 100644 --- a/src/engine/configuration.cpp +++ b/src/engine/configuration.cpp @@ -223,6 +223,11 @@ namespace { // Determines whether the OpenGL context should be a debug context bool activate; + // If this is set to 'true', everytime an OpenGL error is logged, the full + // stacktrace leading to the error is printed as well, making debugging under + // production situations much easier + std::optional printStacktrace; + // Determines whether the OpenGL debug callbacks are performed synchronously. // If set to 'true' the callbacks are in the same thread as the context and in // the scope of the OpenGL function that triggered the message. The default @@ -478,6 +483,9 @@ void parseLuaState(Configuration& configuration) { if (p.openGLDebugContext.has_value()) { const Parameters::OpenGLDebugContext& l = *p.openGLDebugContext; c.openGLDebugContext.isActive = l.activate; + c.openGLDebugContext.printStacktrace = l.printStacktrace.value_or( + c.openGLDebugContext.printStacktrace + ); c.openGLDebugContext.isSynchronous = l.synchronous.value_or( c.openGLDebugContext.isSynchronous ); diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index b75e828183..576b6ad730 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -74,6 +74,7 @@ #include #include #include +#include #include #include #include @@ -560,6 +561,15 @@ void OpenSpaceEngine::initializeGL() { default: throw ghoul::MissingCaseException(); } + + if (global::configuration->openGLDebugContext.printStacktrace) { + std::string stackString = "Stacktrace\n"; + std::vector stack = ghoul::stackTrace(); + for (size_t i = 0; i < stack.size(); i++) { + stackString += fmt::format("{}: {}\n", i, stack[i]); + } + LDEBUGC(category, stackString); + } }; ghoul::opengl::debug::setDebugCallback(callback); } diff --git a/tests/test_configuration.cpp b/tests/test_configuration.cpp index c01cd9ab66..b3266665d5 100644 --- a/tests/test_configuration.cpp +++ b/tests/test_configuration.cpp @@ -388,6 +388,7 @@ TEST_CASE("Configuration: openGLDebugContext", "[configuration]") { constexpr const char Extra[] = R"(OpenGLDebugContext = { Activate = true })"; const Configuration c = loadConfiguration("openGLDebugContext1", Extra); CHECK(c.openGLDebugContext.isActive == true); + CHECK(c.openGLDebugContext.printStacktrace == false); CHECK( c.openGLDebugContext.isSynchronous == defaultConf.openGLDebugContext.isSynchronous @@ -451,6 +452,7 @@ OpenGLDebugContext = { Activate = true, Synchronous = true } constexpr const char Extra[] = R"( OpenGLDebugContext = { Activate = true, + PrintStacktrace = true, FilterIdentifier = { { Identifier = 1, Source = "API", Type = "Error" }, { Identifier = 2, Source = "Window System", Type = "Deprecated" }, @@ -467,6 +469,7 @@ OpenGLDebugContext = { )"; const Configuration c = loadConfiguration("openGLDebugContext3", Extra); CHECK(c.openGLDebugContext.isActive == true); + CHECK(c.openGLDebugContext.printStacktrace == true); CHECK( c.openGLDebugContext.isSynchronous == defaultConf.openGLDebugContext.isSynchronous @@ -515,6 +518,7 @@ OpenGLDebugContext = { Activate = true, FilterSeverity = { "High", "Medium" } } )"; const Configuration c = loadConfiguration("openGLDebugContext4", Extra); CHECK(c.openGLDebugContext.isActive == true); + CHECK(c.openGLDebugContext.printStacktrace == false); CHECK( c.openGLDebugContext.isSynchronous == defaultConf.openGLDebugContext.isSynchronous