mirror of https://github.com/mosra/magnum.git
Browse Source
The class is rather heavy (strings, STL vector) and it'll stay heavier than strictly needed even after the planned STL cleanup -- shader users should not bear the overhead of Array, StringView etc. that it needs in order to compile the shader sources. I might eventually come to a different conclusion (maybe separating GL::Shader population and usage like doing in Vulkan with CreateInfos), but right now this commit has the best available solution -- converting the instance to a lightweight class containing just ID and type, and then converting that back to a GL::Shader upon checking compilation/link status. While at it, also removed the not-strictly-needed Optional usage from the header. It wouldn't work with forward-declared GL::Shader anyway.pull/589/head
18 changed files with 396 additions and 34 deletions
@ -0,0 +1,171 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||
2020, 2021, 2022 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 "Magnum/GL/OpenGLTester.h" |
||||
#include "Magnum/GL/Shader.h" |
||||
#include "Magnum/GL/Version.h" |
||||
#include "Magnum/Shaders/glShaderWrapper.h" |
||||
|
||||
namespace Magnum { namespace Shaders { namespace Test { namespace { |
||||
|
||||
struct GLShaderWrapperGLTest: GL::OpenGLTester { |
||||
explicit GLShaderWrapperGLTest(); |
||||
|
||||
void construct(); |
||||
void constructMove(); |
||||
|
||||
void convert(); |
||||
void convertRvalue(); |
||||
}; |
||||
|
||||
GLShaderWrapperGLTest::GLShaderWrapperGLTest() { |
||||
addTests({&GLShaderWrapperGLTest::construct, |
||||
&GLShaderWrapperGLTest::constructMove, |
||||
|
||||
&GLShaderWrapperGLTest::convert, |
||||
&GLShaderWrapperGLTest::convertRvalue}); |
||||
} |
||||
|
||||
void GLShaderWrapperGLTest::construct() { |
||||
{ |
||||
GL::Shader glShader{ |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
GL::Version::GL300, |
||||
#else |
||||
GL::Version::GLES300, |
||||
#endif |
||||
GL::Shader::Type::Fragment}; |
||||
|
||||
GLuint id = glShader.id(); |
||||
MAGNUM_VERIFY_NO_GL_ERROR(); |
||||
CORRADE_VERIFY(id > 0); |
||||
|
||||
Implementation::GLShaderWrapper shader{std::move(glShader)}; |
||||
MAGNUM_VERIFY_NO_GL_ERROR(); |
||||
CORRADE_COMPARE(shader.id, id); |
||||
CORRADE_COMPARE(shader.type, GL_FRAGMENT_SHADER); |
||||
CORRADE_VERIFY(!glShader.id()); |
||||
} |
||||
|
||||
MAGNUM_VERIFY_NO_GL_ERROR(); |
||||
} |
||||
|
||||
void GLShaderWrapperGLTest::constructMove() { |
||||
GL::Shader glShaderA{ |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
GL::Version::GL300, |
||||
#else |
||||
GL::Version::GLES300, |
||||
#endif |
||||
GL::Shader::Type::Fragment}; |
||||
Implementation::GLShaderWrapper a{std::move(glShaderA)}; |
||||
|
||||
GLuint id = a.id; |
||||
MAGNUM_VERIFY_NO_GL_ERROR(); |
||||
CORRADE_VERIFY(id > 0); |
||||
|
||||
Implementation::GLShaderWrapper b{std::move(a)}; |
||||
CORRADE_VERIFY(!a.id); |
||||
CORRADE_COMPARE(b.id, id); |
||||
CORRADE_COMPARE(b.type, GL_FRAGMENT_SHADER); |
||||
|
||||
GL::Shader glShaderB{ |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
GL::Version::GL210, |
||||
#else |
||||
GL::Version::GLES200, |
||||
#endif |
||||
GL::Shader::Type::Vertex}; |
||||
Implementation::GLShaderWrapper c{std::move(glShaderB)}; |
||||
|
||||
GLuint cId = c.id; |
||||
c = std::move(b); |
||||
MAGNUM_VERIFY_NO_GL_ERROR(); |
||||
CORRADE_VERIFY(cId > 0); |
||||
CORRADE_COMPARE(b.id, cId); |
||||
CORRADE_COMPARE(c.id, id); |
||||
CORRADE_COMPARE(c.type, GL_FRAGMENT_SHADER); |
||||
|
||||
CORRADE_VERIFY(std::is_nothrow_move_constructible<Implementation::GLShaderWrapper>::value); |
||||
CORRADE_VERIFY(std::is_nothrow_move_assignable<Implementation::GLShaderWrapper>::value); |
||||
} |
||||
|
||||
void GLShaderWrapperGLTest::convert() { |
||||
{ |
||||
GL::Shader glShader{ |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
GL::Version::GL300, |
||||
#else |
||||
GL::Version::GLES300, |
||||
#endif |
||||
GL::Shader::Type::Fragment}; |
||||
Implementation::GLShaderWrapper shader{std::move(glShader)}; |
||||
|
||||
GLuint id = shader.id; |
||||
MAGNUM_VERIFY_NO_GL_ERROR(); |
||||
CORRADE_VERIFY(id > 0); |
||||
|
||||
GL::Shader glShader2 = shader; |
||||
MAGNUM_VERIFY_NO_GL_ERROR(); |
||||
|
||||
MAGNUM_VERIFY_NO_GL_ERROR(); |
||||
CORRADE_COMPARE(glShader2.id(), id); |
||||
CORRADE_COMPARE(glShader2.type(), GL::Shader::Type::Fragment); |
||||
CORRADE_VERIFY(shader.id); |
||||
} |
||||
|
||||
MAGNUM_VERIFY_NO_GL_ERROR(); |
||||
} |
||||
|
||||
void GLShaderWrapperGLTest::convertRvalue() { |
||||
{ |
||||
GL::Shader glShader{ |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
GL::Version::GL300, |
||||
#else |
||||
GL::Version::GLES300, |
||||
#endif |
||||
GL::Shader::Type::Fragment}; |
||||
Implementation::GLShaderWrapper shader{std::move(glShader)}; |
||||
|
||||
GLuint id = shader.id; |
||||
MAGNUM_VERIFY_NO_GL_ERROR(); |
||||
CORRADE_VERIFY(id > 0); |
||||
|
||||
GL::Shader glShader2 = std::move(shader); |
||||
MAGNUM_VERIFY_NO_GL_ERROR(); |
||||
|
||||
MAGNUM_VERIFY_NO_GL_ERROR(); |
||||
CORRADE_COMPARE(glShader2.id(), id); |
||||
CORRADE_COMPARE(glShader2.type(), GL::Shader::Type::Fragment); |
||||
CORRADE_VERIFY(!shader.id); |
||||
} |
||||
|
||||
MAGNUM_VERIFY_NO_GL_ERROR(); |
||||
} |
||||
|
||||
}}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Shaders::Test::GLShaderWrapperGLTest) |
||||
@ -0,0 +1,58 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||
2020, 2021, 2022 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 <Corrade/TestSuite/Tester.h> |
||||
|
||||
#include "Magnum/Magnum.h" |
||||
#include "Magnum/Shaders/glShaderWrapper.h" |
||||
|
||||
namespace Magnum { namespace Shaders { namespace Test { namespace { |
||||
|
||||
struct GLShaderWrapperTest: TestSuite::Tester { |
||||
explicit GLShaderWrapperTest(); |
||||
|
||||
void constructNoCreate(); |
||||
void constructCopy(); |
||||
}; |
||||
|
||||
GLShaderWrapperTest::GLShaderWrapperTest() { |
||||
addTests({&GLShaderWrapperTest::constructNoCreate, |
||||
&GLShaderWrapperTest::constructCopy}); |
||||
} |
||||
|
||||
void GLShaderWrapperTest::constructNoCreate() { |
||||
Implementation::GLShaderWrapper shader{NoCreate}; |
||||
CORRADE_COMPARE(shader.type, 0); |
||||
CORRADE_COMPARE(shader.id, 0); |
||||
} |
||||
|
||||
void GLShaderWrapperTest::constructCopy() { |
||||
CORRADE_VERIFY(!std::is_copy_constructible<Implementation::GLShaderWrapper>{}); |
||||
CORRADE_VERIFY(!std::is_copy_assignable<Implementation::GLShaderWrapper>{}); |
||||
} |
||||
|
||||
}}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Shaders::Test::GLShaderWrapperTest) |
||||
@ -0,0 +1,61 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||
2020, 2021, 2022 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 "glShaderWrapper.h" |
||||
|
||||
#include "Magnum/GL/Shader.h" |
||||
|
||||
namespace Magnum { namespace Shaders { namespace Implementation { |
||||
|
||||
GLShaderWrapper::GLShaderWrapper(GL::Shader&& shader) noexcept: type{GLenum(shader.type())}, id{shader.release()} {} |
||||
|
||||
GLShaderWrapper::GLShaderWrapper(GLShaderWrapper&& other) noexcept: type{other.type}, id{other.id} { |
||||
other.id = 0; |
||||
} |
||||
|
||||
GLShaderWrapper& GLShaderWrapper::operator=(GLShaderWrapper&& other) noexcept { |
||||
using std::swap; |
||||
swap(other.type, type); |
||||
swap(other.id, id); |
||||
return *this; |
||||
} |
||||
|
||||
GLShaderWrapper::~GLShaderWrapper() { |
||||
/* Convert itself to a temporary GL::Shader, triggering deletion in its
|
||||
destructor */ |
||||
if(id) GL::Shader{std::move(*this)}; |
||||
} |
||||
|
||||
GLShaderWrapper::operator GL::Shader() & noexcept { |
||||
return GL::Shader::wrap(GL::Shader::Type(type), id); |
||||
} |
||||
|
||||
GLShaderWrapper::operator GL::Shader() && noexcept { |
||||
GL::Shader out = GL::Shader::wrap(GL::Shader::Type(type), id, GL::ObjectFlag::DeleteOnDestruction); |
||||
id = 0; |
||||
return out; |
||||
} |
||||
|
||||
}}} |
||||
@ -0,0 +1,62 @@
|
||||
#ifndef Magnum_Shaders_glShaderWrapper_h |
||||
#define Magnum_Shaders_glShaderWrapper_h |
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||
2020, 2021, 2022 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 "Magnum/Tags.h" |
||||
#include "Magnum/GL/GL.h" |
||||
#include "Magnum/Shaders/visibility.h" |
||||
|
||||
namespace Magnum { namespace Shaders { namespace Implementation { |
||||
|
||||
/* A lightweight alternative to GL::Shader that holds just the type and ID,
|
||||
used by CompileState instances of all shaders. There it's used just to |
||||
retrieve error messages in case of a compilation failures, so it doesn't |
||||
make sense to pull in stuff needed to store shader sources such as strings |
||||
and vectors/arrays. |
||||
|
||||
Might get revisited once GL::Shader gets the deSTLification treatment, but |
||||
even then this class seems significantly lighter weight. */ |
||||
struct MAGNUM_SHADERS_EXPORT GLShaderWrapper { |
||||
/*implicit*/ GLShaderWrapper(NoCreateT) noexcept: type{}, id{} {} |
||||
/*implicit*/ GLShaderWrapper(GL::Shader&& shader) noexcept; |
||||
|
||||
GLShaderWrapper(const GLShaderWrapper&) = delete; |
||||
GLShaderWrapper(GLShaderWrapper&& other) noexcept; |
||||
|
||||
GLShaderWrapper& operator=(const GLShaderWrapper&) = delete; |
||||
GLShaderWrapper& operator=(GLShaderWrapper&& other) noexcept; |
||||
|
||||
~GLShaderWrapper(); |
||||
/*implicit*/ operator GL::Shader() & noexcept; |
||||
/*implicit*/ operator GL::Shader() && noexcept; |
||||
|
||||
GLenum type; |
||||
GLuint id; |
||||
}; |
||||
|
||||
}}} |
||||
|
||||
#endif |
||||
Loading…
Reference in new issue