From c8a9422d1c26b2f4c35367c5d141ee05df11d35a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 30 Apr 2020 23:16:13 +0200 Subject: [PATCH] DebugTools: make GLFrameProfiler::Values storeable in Configuration. I want to use these in command-line arguments. --- src/Magnum/DebugTools/FrameProfiler.cpp | 76 +++++++++++++++---- src/Magnum/DebugTools/FrameProfiler.h | 50 ++++++++++++ .../DebugTools/Test/FrameProfilerTest.cpp | 39 ++++++++++ 3 files changed, 152 insertions(+), 13 deletions(-) diff --git a/src/Magnum/DebugTools/FrameProfiler.cpp b/src/Magnum/DebugTools/FrameProfiler.cpp index 48077e881..d6a04e171 100644 --- a/src/Magnum/DebugTools/FrameProfiler.cpp +++ b/src/Magnum/DebugTools/FrameProfiler.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include "Magnum/Math/Functions.h" #ifdef MAGNUM_TARGET_GL @@ -629,22 +630,24 @@ Double GLFrameProfiler::primitiveClipRatioMean() const { } #endif +namespace { + +constexpr const char* GLFrameProfilerValueNames[] { + "FrameTime", + "CpuDuration", + "GpuDuration", + "VertexFetchRatio", + "PrimitiveClipRatio" +}; + +} + Debug& operator<<(Debug& debug, const GLFrameProfiler::Value value) { debug << "DebugTools::GLFrameProfiler::Value" << Debug::nospace; - switch(value) { - /* LCOV_EXCL_START */ - #define _c(v) case GLFrameProfiler::Value::v: return debug << "::" #v; - _c(FrameTime) - _c(CpuDuration) - _c(GpuDuration) - #ifndef MAGNUM_TARGET_GLES - _c(VertexFetchRatio) - _c(PrimitiveClipRatio) - #endif - #undef _c - /* LCOV_EXCL_STOP */ - } + const UnsignedInt bit = Math::log2(UnsignedShort(value)); + if(1 << bit == UnsignedShort(value)) + return debug << "::" << Debug::nospace << GLFrameProfilerValueNames[bit]; return debug << "(" << Debug::nospace << reinterpret_cast(UnsignedShort(value)) << Debug::nospace << ")"; } @@ -663,3 +666,50 @@ Debug& operator<<(Debug& debug, const GLFrameProfiler::Values value) { #endif }} + +namespace Corrade { namespace Utility { + +using namespace Magnum; + +std::string ConfigurationValue::toString(const DebugTools::GLFrameProfiler::Value value, ConfigurationValueFlags) { + const UnsignedInt bit = Math::log2(UnsignedShort(value)); + if(1 << bit == UnsignedShort(value)) + return DebugTools::GLFrameProfilerValueNames[bit]; + return ""; +} + +DebugTools::GLFrameProfiler::Value ConfigurationValue::fromString(const std::string& value, ConfigurationValueFlags) { + for(std::size_t i = 0; i != Containers::arraySize(DebugTools::GLFrameProfilerValueNames); ++i) + if(DebugTools::GLFrameProfilerValueNames[i] == value) + return DebugTools::GLFrameProfiler::Value(1 << i); + + return DebugTools::GLFrameProfiler::Value{}; +} + +std::string ConfigurationValue::toString(const DebugTools::GLFrameProfiler::Values value, ConfigurationValueFlags) { + std::string out; + + for(std::size_t i = 0; i != Containers::arraySize(DebugTools::GLFrameProfilerValueNames); ++i) { + const auto bit = DebugTools::GLFrameProfiler::Value(1 << i); + if(value & bit) { + if(!out.empty()) out += ' '; + out += DebugTools::GLFrameProfilerValueNames[i]; + } + } + + return out; +} + +DebugTools::GLFrameProfiler::Values ConfigurationValue::fromString(const std::string& value, ConfigurationValueFlags) { + const std::vector bits = Utility::String::splitWithoutEmptyParts(value); + + DebugTools::GLFrameProfiler::Values values; + for(const std::string& bit: bits) + for(std::size_t i = 0; i != Containers::arraySize(DebugTools::GLFrameProfilerValueNames); ++i) + if(DebugTools::GLFrameProfilerValueNames[i] == bit) + values |= DebugTools::GLFrameProfiler::Value(1 << i); + + return values; +} + +}} diff --git a/src/Magnum/DebugTools/FrameProfiler.h b/src/Magnum/DebugTools/FrameProfiler.h index c5fc55e71..684f1816b 100644 --- a/src/Magnum/DebugTools/FrameProfiler.h +++ b/src/Magnum/DebugTools/FrameProfiler.h @@ -701,4 +701,54 @@ MAGNUM_DEBUGTOOLS_EXPORT Debug& operator<<(Debug& debug, GLFrameProfiler::Values }} +namespace Corrade { namespace Utility { + +/** +@configurationvalue{Magnum::DebugTools::GLFrameProfiler::Value} +@m_since_latest +*/ +template<> struct MAGNUM_DEBUGTOOLS_EXPORT ConfigurationValue { + ConfigurationValue() = delete; + + /** + * @brief Writes enum value as a string + * + * If the value is invalid, returns an empty string. + */ + static std::string toString(Magnum::DebugTools::GLFrameProfiler::Value value, ConfigurationValueFlags); + + /** + * @brief Reads enum value as a string + * + * If the string is invalid, returns a zero (invalid) value. + */ + static Magnum::DebugTools::GLFrameProfiler::Value fromString(const std::string& stringValue, ConfigurationValueFlags); +}; + +/** +@configurationvalue{Magnum::DebugTools::GLFrameProfiler::Values} +@m_since_latest +*/ +template<> struct MAGNUM_DEBUGTOOLS_EXPORT ConfigurationValue { + ConfigurationValue() = delete; + + /** + * @brief Writes enum set value as a string + * + * Writes the enum set as a sequence of flag names separated by spaces. If + * the value is invalid, returns an empty string. + */ + static std::string toString(Magnum::DebugTools::GLFrameProfiler::Values value, ConfigurationValueFlags); + + /** + * @brief Reads enum set value as a string + * + * Assumes the string is a sequence of flag names separated by spaces. If + * the value is invalid, returns an empty set. + */ + static Magnum::DebugTools::GLFrameProfiler::Values fromString(const std::string& stringValue, ConfigurationValueFlags); +}; + +}} + #endif diff --git a/src/Magnum/DebugTools/Test/FrameProfilerTest.cpp b/src/Magnum/DebugTools/Test/FrameProfilerTest.cpp index 23e66e127..b8eb11bfe 100644 --- a/src/Magnum/DebugTools/Test/FrameProfilerTest.cpp +++ b/src/Magnum/DebugTools/Test/FrameProfilerTest.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include "Magnum/DebugTools/FrameProfiler.h" @@ -67,6 +68,9 @@ struct FrameProfilerTest: TestSuite::Tester { #ifdef MAGNUM_TARGET_GL void debugGLValue(); void debugGLValues(); + + void configurationGLValue(); + void configurationGLValues(); #endif }; @@ -141,6 +145,9 @@ FrameProfilerTest::FrameProfilerTest() { #ifdef MAGNUM_TARGET_GL &FrameProfilerTest::debugGLValue, &FrameProfilerTest::debugGLValues, + + &FrameProfilerTest::configurationGLValue, + &FrameProfilerTest::configurationGLValues #endif }); } @@ -1051,6 +1058,38 @@ void FrameProfilerTest::debugGLValues() { Debug{&out} << (GLFrameProfiler::Value::CpuDuration|GLFrameProfiler::Value::FrameTime) << GLFrameProfiler::Values{}; CORRADE_COMPARE(out.str(), "DebugTools::GLFrameProfiler::Value::FrameTime|DebugTools::GLFrameProfiler::Value::CpuDuration DebugTools::GLFrameProfiler::Values{}\n"); } + +void FrameProfilerTest::configurationGLValue() { + Utility::ConfigurationGroup c; + + c.setValue("value", GLFrameProfiler::Value::GpuDuration); + CORRADE_COMPARE(c.value("value"), "GpuDuration"); + CORRADE_COMPARE(c.value("value"), GLFrameProfiler::Value::GpuDuration); + + c.setValue("zero", GLFrameProfiler::Value{}); + CORRADE_COMPARE(c.value("zero"), ""); + CORRADE_COMPARE(c.value("zero"), GLFrameProfiler::Value{}); + + c.setValue("invalid", GLFrameProfiler::Value(0xdead)); + CORRADE_COMPARE(c.value("invalid"), ""); + CORRADE_COMPARE(c.value("invalid"), GLFrameProfiler::Value{}); +} + +void FrameProfilerTest::configurationGLValues() { + Utility::ConfigurationGroup c; + + c.setValue("value", GLFrameProfiler::Value::FrameTime|GLFrameProfiler::Value::CpuDuration|GLFrameProfiler::Value::GpuDuration); + CORRADE_COMPARE(c.value("value"), "FrameTime CpuDuration GpuDuration"); + CORRADE_COMPARE(c.value("value"), GLFrameProfiler::Value::FrameTime|GLFrameProfiler::Value::CpuDuration|GLFrameProfiler::Value::GpuDuration); + + c.setValue("empty", GLFrameProfiler::Values{}); + CORRADE_COMPARE(c.value("empty"), ""); + CORRADE_COMPARE(c.value("empty"), GLFrameProfiler::Values{}); + + c.setValue("invalid", GLFrameProfiler::Value::CpuDuration|GLFrameProfiler::Value::GpuDuration|GLFrameProfiler::Value(0xff00)); + CORRADE_COMPARE(c.value("invalid"), "CpuDuration GpuDuration"); + CORRADE_COMPARE(c.value("invalid"), GLFrameProfiler::Value::CpuDuration|GLFrameProfiler::Value::GpuDuration); +} #endif }}}}