From 28939078e2fe03d91feb9fa646fab976d7f8af9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 22 Jan 2014 23:55:18 +0100 Subject: [PATCH] Decide about used extensions on only one place. Having the same branch cascade twice for debug output and for actual feature selection is not good for maintenance. Put the extensions into some list and then make it unique instead. --- src/Magnum/Implementation/DebugState.cpp | 17 ++++++--- src/Magnum/Implementation/DebugState.h | 3 +- src/Magnum/Implementation/State.cpp | 47 ++++++++++++------------ src/Magnum/Implementation/State.h | 16 ++++---- 4 files changed, 46 insertions(+), 37 deletions(-) diff --git a/src/Magnum/Implementation/DebugState.cpp b/src/Magnum/Implementation/DebugState.cpp index 1e514ac54..f030a8689 100644 --- a/src/Magnum/Implementation/DebugState.cpp +++ b/src/Magnum/Implementation/DebugState.cpp @@ -31,8 +31,10 @@ namespace Magnum { namespace Implementation { -DebugState::DebugState(Context& context): maxLabelLength(0), maxLoggedMessages(0), maxMessageLength(0), messageCallback(nullptr) { +DebugState::DebugState(Context& context, std::vector& extensions): maxLabelLength(0), maxLoggedMessages(0), maxMessageLength(0), messageCallback(nullptr) { if(context.isExtensionSupported()) { + extensions.push_back(Extensions::GL::KHR::debug::string()); + getLabelImplementation = &AbstractObject::getLabelImplementationKhr; labelImplementation = &AbstractObject::labelImplementationKhr; messageInsertImplementation = &DebugMessage::insertImplementationKhr; @@ -40,6 +42,8 @@ DebugState::DebugState(Context& context): maxLabelLength(0), maxLoggedMessages(0 } else { if(context.isExtensionSupported()) { + extensions.push_back(Extensions::GL::EXT::debug_label::string()); + getLabelImplementation = &AbstractObject::getLabelImplementationExt; labelImplementation = &AbstractObject::labelImplementationExt; } else { @@ -47,14 +51,17 @@ DebugState::DebugState(Context& context): maxLabelLength(0), maxLoggedMessages(0 labelImplementation = &AbstractObject::labelImplementationNoOp; } - if(context.isExtensionSupported()) + if(context.isExtensionSupported()) { + extensions.push_back(Extensions::GL::EXT::debug_marker::string()); + messageInsertImplementation = &DebugMessage::insertImplementationExt; #ifndef MAGNUM_TARGET_GLES - else if(context.isExtensionSupported()) + } else if(context.isExtensionSupported()) { + extensions.push_back(Extensions::GL::GREMEDY::string_marker::string()); + messageInsertImplementation = &DebugMessage::insertImplementationGremedy; #endif - else - messageInsertImplementation = &DebugMessage::insertImplementationNoOp; + } else messageInsertImplementation = &DebugMessage::insertImplementationNoOp; messageCallbackImplementation = &DebugMessage::callbackImplementationNoOp; } diff --git a/src/Magnum/Implementation/DebugState.h b/src/Magnum/Implementation/DebugState.h index 95955e1c2..a435e1b20 100644 --- a/src/Magnum/Implementation/DebugState.h +++ b/src/Magnum/Implementation/DebugState.h @@ -26,13 +26,14 @@ */ #include +#include #include "Magnum/DebugMessage.h" namespace Magnum { namespace Implementation { struct DebugState { - DebugState(Context& context); + explicit DebugState(Context& context, std::vector& extensions); std::string(*getLabelImplementation)(GLenum, GLuint); void(*labelImplementation)(GLenum, GLuint, const std::string&); diff --git a/src/Magnum/Implementation/State.cpp b/src/Magnum/Implementation/State.cpp index 015773280..62ee567f1 100644 --- a/src/Magnum/Implementation/State.cpp +++ b/src/Magnum/Implementation/State.cpp @@ -25,6 +25,8 @@ #include "State.h" +#include + #include "Magnum/Context.h" #include "Magnum/Extensions.h" @@ -39,32 +41,31 @@ namespace Magnum { namespace Implementation { -State::State(Context& context): - buffer(new BufferState), - debug(new DebugState(context)), - framebuffer(new FramebufferState), - mesh(new MeshState), - renderer(new RendererState), - shader(new ShaderState), - shaderProgram(new ShaderProgramState), - texture(new TextureState) -{ +State::State(Context& context) { + /* List of extensions used in current context. Guesstimate count to avoid + unnecessary reallocations. */ + std::vector extensions; + #ifndef MAGNUM_TARGET_GLES + extensions.reserve(32); + #else + extensions.reserve(8); + #endif - Debug() << "Using optional features:"; + buffer = new BufferState; + debug = new DebugState(context, extensions); + framebuffer = new FramebufferState; + mesh = new MeshState; + renderer = new RendererState; + shader = new ShaderState; + shaderProgram = new ShaderProgramState; + texture = new TextureState; - if(context.isExtensionSupported()) - Debug() << " " << Extensions::GL::KHR::debug::string(); - else { - if(context.isExtensionSupported()) - Debug() << " " << Extensions::GL::EXT::debug_label::string(); + /* Sort the features and remove duplicates */ + std::sort(extensions.begin(), extensions.end()); + extensions.erase(std::unique(extensions.begin(), extensions.end()), extensions.end()); - if(context.isExtensionSupported()) - Debug() << " " << Extensions::GL::EXT::debug_marker::string(); - #ifndef MAGNUM_TARGET_GLES - else if(context.isExtensionSupported()) - Debug() << " " << Extensions::GL::GREMEDY::string_marker::string(); - #endif - } + Debug() << "Using optional features:"; + for(const auto& ext: extensions) Debug() << " " << ext; } State::~State() { diff --git a/src/Magnum/Implementation/State.h b/src/Magnum/Implementation/State.h index 61f34a546..207594cd2 100644 --- a/src/Magnum/Implementation/State.h +++ b/src/Magnum/Implementation/State.h @@ -44,14 +44,14 @@ struct State { ~State(); - BufferState* const buffer; - DebugState* const debug; - FramebufferState* const framebuffer; - MeshState* const mesh; - RendererState* const renderer; - ShaderState* const shader; - ShaderProgramState* const shaderProgram; - TextureState* const texture; + BufferState* buffer; + DebugState* debug; + FramebufferState* framebuffer; + MeshState* mesh; + RendererState* renderer; + ShaderState* shader; + ShaderProgramState* shaderProgram; + TextureState* texture; }; }}