From 13d9a8f6f50d4cfb19c1a3379c0992382e60125d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 6 Mar 2017 13:40:31 +0100 Subject: [PATCH] NoInit constructor for AbstractShaderProgram. Also made it possible to call AbstractShaderProgram destructor without an GL context being active. --- src/Magnum/AbstractShaderProgram.cpp | 6 +- src/Magnum/AbstractShaderProgram.h | 14 +++++ src/Magnum/Test/AbstractShaderProgramTest.cpp | 56 +++++++++++++++++++ src/Magnum/Test/CMakeLists.txt | 1 + 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/Magnum/Test/AbstractShaderProgramTest.cpp diff --git a/src/Magnum/AbstractShaderProgram.cpp b/src/Magnum/AbstractShaderProgram.cpp index ce19cb1a6..4fda1ecfd 100644 --- a/src/Magnum/AbstractShaderProgram.cpp +++ b/src/Magnum/AbstractShaderProgram.cpp @@ -277,16 +277,20 @@ AbstractShaderProgram::AbstractShaderProgram(): _id(glCreateProgram()) { CORRADE_INTERNAL_ASSERT(_id != Implementation::State::DisengagedBinding); } +AbstractShaderProgram::AbstractShaderProgram(NoCreateT) noexcept: _id{0} {} + AbstractShaderProgram::AbstractShaderProgram(AbstractShaderProgram&& other) noexcept: _id(other._id) { other._id = 0; } AbstractShaderProgram::~AbstractShaderProgram() { + if(!_id) return; + /* Remove current usage from the state */ GLuint& current = Context::current().state().shaderProgram->current; if(current == _id) current = 0; - if(_id) glDeleteProgram(_id); + glDeleteProgram(_id); } AbstractShaderProgram& AbstractShaderProgram::operator=(AbstractShaderProgram&& other) noexcept { diff --git a/src/Magnum/AbstractShaderProgram.h b/src/Magnum/AbstractShaderProgram.h index 9c5aed167..36c27d115 100644 --- a/src/Magnum/AbstractShaderProgram.h +++ b/src/Magnum/AbstractShaderProgram.h @@ -35,6 +35,7 @@ #include "Magnum/AbstractObject.h" #include "Magnum/Attribute.h" +#include "Magnum/Tags.h" #if defined(CORRADE_TARGET_WINDOWS) && !defined(MAGNUM_TARGET_GLES2) #include @@ -695,6 +696,19 @@ class MAGNUM_EXPORT AbstractShaderProgram: public AbstractObject { */ explicit AbstractShaderProgram(); + /** + * @brief Construct without creating the underlying OpenGL object + * + * The constructed instance is equivalent to moved-from state. Useful + * in cases where you will overwrite the instance later anyway. Move + * another object over it to make it useful. + * + * This function can be safely used for constructing (and later + * destructing) objects even without any OpenGL context being active. + * @see @ref AbstractShaderProgram() + */ + explicit AbstractShaderProgram(NoCreateT) noexcept; + /** @brief Copying is not allowed */ AbstractShaderProgram(const AbstractShaderProgram&) = delete; diff --git a/src/Magnum/Test/AbstractShaderProgramTest.cpp b/src/Magnum/Test/AbstractShaderProgramTest.cpp new file mode 100644 index 000000000..cfac6c5e3 --- /dev/null +++ b/src/Magnum/Test/AbstractShaderProgramTest.cpp @@ -0,0 +1,56 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016 + Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include + +#include "Magnum/AbstractShaderProgram.h" + +namespace Magnum { namespace Test { + +struct AbstractShaderProgramTest: TestSuite::Tester { + explicit AbstractShaderProgramTest(); + + void constructNoCreate(); +}; + +AbstractShaderProgramTest::AbstractShaderProgramTest() { + addTests({&AbstractShaderProgramTest::constructNoCreate}); +} + +void AbstractShaderProgramTest::constructNoCreate() { + { + struct: AbstractShaderProgram { + using AbstractShaderProgram::AbstractShaderProgram; + } shader{NoCreate}; + + CORRADE_COMPARE(shader.id(), 0); + } + + CORRADE_VERIFY(true); +} + +}} + +CORRADE_TEST_MAIN(Magnum::Test::AbstractShaderProgramTest) diff --git a/src/Magnum/Test/CMakeLists.txt b/src/Magnum/Test/CMakeLists.txt index 85fd4698a..ce67e5efb 100644 --- a/src/Magnum/Test/CMakeLists.txt +++ b/src/Magnum/Test/CMakeLists.txt @@ -25,6 +25,7 @@ corrade_add_test(ArrayTest ArrayTest.cpp LIBRARIES Magnum) corrade_add_test(AttributeTest AttributeTest.cpp LIBRARIES Magnum) +corrade_add_test(AbstractShaderProgramTest AbstractShaderProgramTest.cpp LIBRARIES Magnum) corrade_add_test(BufferTest BufferTest.cpp LIBRARIES Magnum) corrade_add_test(FormatTest FormatTest.cpp LIBRARIES Magnum) corrade_add_test(ContextTest ContextTest.cpp LIBRARIES Magnum)