From 47e7de47c85ffbc3841ab9c0c702345c161c9203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 17 May 2021 12:29:07 +0200 Subject: [PATCH] Shaders: remove the AbstractVector base. 167 additions, 341 deletions. I think that speaks for itself about benefits of this base class. --- doc/changelog.dox | 28 ++-- src/Magnum/Shaders/AbstractVector.h | 70 --------- src/Magnum/Shaders/AbstractVectorGL.cpp | 43 ------ src/Magnum/Shaders/AbstractVectorGL.h | 134 ------------------ src/Magnum/Shaders/CMakeLists.txt | 3 - src/Magnum/Shaders/DistanceFieldVector.frag | 1 - src/Magnum/Shaders/DistanceFieldVectorGL.cpp | 47 +++--- src/Magnum/Shaders/DistanceFieldVectorGL.h | 70 ++++++--- src/Magnum/Shaders/Shaders.h | 9 -- src/Magnum/Shaders/Vector.frag | 1 - .../{AbstractVector.vert => Vector.vert} | 0 src/Magnum/Shaders/VectorGL.cpp | 38 +++-- src/Magnum/Shaders/VectorGL.h | 60 ++++++-- src/Magnum/Shaders/resources-gl.conf | 6 +- src/Magnum/Text/Renderer.cpp | 13 +- src/Magnum/Text/Renderer.h | 7 +- 16 files changed, 178 insertions(+), 352 deletions(-) delete mode 100644 src/Magnum/Shaders/AbstractVector.h delete mode 100644 src/Magnum/Shaders/AbstractVectorGL.cpp delete mode 100644 src/Magnum/Shaders/AbstractVectorGL.h rename src/Magnum/Shaders/{AbstractVector.vert => Vector.vert} (100%) diff --git a/doc/changelog.dox b/doc/changelog.dox index f2ae999e5..11b5cf82a 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -462,19 +462,17 @@ See also: @ref DebugTools::FrameProfilerGL. The new name plays better with IDE autocompletion and makes the GL-specific class appear next to the API-independent base in alphabetically sorted lists. -- @cpp Shaders::AbstractVector @ce, @cpp Shaders::DistanceFieldVector @ce, - @cpp Shaders::Flat @ce, @cpp Shaders::Generic @ce, - @cpp Shaders::MeshVisualizer2D @ce, @cpp Shaders::MeshVisualizer3D @ce, - @cpp Shaders::Phong @ce, @cpp Shaders::Vector @ce, - @cpp Shaders::VertexColor @ce and related 2D/3D typedefs are deprecated in - favor of @ref Shaders::AbstractVectorGL, - @ref Shaders::DistanceFieldVectorGL, @ref Shaders::FlatGL, - @ref Shaders::GenericGL, @ref Shaders::MeshVisualizerGL2D, - @ref Shaders::MeshVisualizerGL3D, @ref Shaders::PhongGL, - @ref Shaders::VectorGL, @ref Shaders::VertexColorGL and correspondingly - renamed typedefs to make room for Vulkan shaders and functionality shared - between OpenGL and Vulkan implementation such as uniform buffer layout - definitions +- @cpp Shaders::DistanceFieldVector @ce, @cpp Shaders::Flat @ce, + @cpp Shaders::Generic @ce, @cpp Shaders::MeshVisualizer2D @ce, + @cpp Shaders::MeshVisualizer3D @ce, @cpp Shaders::Phong @ce, + @cpp Shaders::Vector @ce, @cpp Shaders::VertexColor @ce and related 2D/3D + typedefs are deprecated in favor of @ref Shaders::DistanceFieldVectorGL, + @ref Shaders::FlatGL, @ref Shaders::GenericGL, + @ref Shaders::MeshVisualizerGL2D, @ref Shaders::MeshVisualizerGL3D, + @ref Shaders::PhongGL, @ref Shaders::VectorGL, @ref Shaders::VertexColorGL + and correspondingly renamed typedefs to make room for Vulkan shaders and + functionality shared between OpenGL and Vulkan implementation such as + uniform buffer layout definitions - @ref Shaders::PhongGL::setLightPositions() and @ref Shaders::PhongGL::setLightPosition() taking three-component vectors are deprecated in favor of variants taking four-component vectors, where the @@ -658,6 +656,10 @@ See also: isn't available anymore. For backwards compatibility, light positions supplied through three-component vectors are now represented as directional lights, which is close, but not exactly the same as before. +- The @cpp Shaders::AbstractVector @ce base class for @ref Shaders::VectorGL + and @ref Shaders::DistanceFieldVectorGL is removed, as its benefits were + rather questionable --- on the contrary, it made subclass implementation + more verbose and less clear - Mutable access to @ref Trade::PhongMaterialData color and texture information, deprecated in 2020.06, is now removed, as it's impossible to implement through the redesigned @ref Trade::MaterialData APIs. However diff --git a/src/Magnum/Shaders/AbstractVector.h b/src/Magnum/Shaders/AbstractVector.h deleted file mode 100644 index 192371879..000000000 --- a/src/Magnum/Shaders/AbstractVector.h +++ /dev/null @@ -1,70 +0,0 @@ -#ifndef Magnum_Shaders_AbstractVector_h -#define Magnum_Shaders_AbstractVector_h -/* - This file is part of Magnum. - - Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, - 2020, 2021 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. -*/ - -#ifdef MAGNUM_BUILD_DEPRECATED -/** @file - * @brief Typedef @ref Magnum::Shaders::AbstractVector, alias @ref Magnum::Shaders::AbstractVector2D, @ref Magnum::Shaders::AbstractVector3D - * @m_deprecated_since_latest Use @ref Magnum/Shaders/AbstractVectorGL.h, the - * @ref Magnum::Shaders::AbstractVectorGL "AbstractVectorGL" class and - * related typedefs instead. - */ -#endif - -#include "Magnum/configure.h" - -#ifdef MAGNUM_BUILD_DEPRECATED -#include - -#include "Magnum/Shaders/AbstractVectorGL.h" - -CORRADE_DEPRECATED_FILE("use Magnum/Shaders/AbstractVectorGL.h, the AbstractVectorGL class and related typedefs instead") - -namespace Magnum { namespace Shaders { - -/** @brief @copybrief AbstractVectorGL - * @m_deprecated_since_latest Use @ref AbstractVectorGL instead. - */ -#ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */ -template using AbstractVector CORRADE_DEPRECATED_ALIAS("use AbstractVectorGL instead") = AbstractVectorGL; -#endif - -/** @brief @copybrief AbstractVectorGL2D - * @m_deprecated_since_latest Use @ref AbstractVectorGL2D instead. - */ -typedef CORRADE_DEPRECATED("use AbstractVectorGL2D instead") AbstractVectorGL2D AbstractVector2D; - -/** @brief @copybrief AbstractVectorGL3D - * @m_deprecated_since_latest Use @ref AbstractVectorGL3D instead. - */ -typedef CORRADE_DEPRECATED("use AbstractVectorGL3D instead") AbstractVectorGL3D AbstractVector3D; - -}} -#else -#error use Magnum/Shaders/AbstractVectorGL.h, the AbstractVectorGL class and related typedefs instead -#endif - -#endif diff --git a/src/Magnum/Shaders/AbstractVectorGL.cpp b/src/Magnum/Shaders/AbstractVectorGL.cpp deleted file mode 100644 index e02251094..000000000 --- a/src/Magnum/Shaders/AbstractVectorGL.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - This file is part of Magnum. - - Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, - 2020, 2021 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 "AbstractVectorGL.h" - -#include "Magnum/GL/Texture.h" -#include "Magnum/Shaders/visibility.h" - -namespace Magnum { namespace Shaders { - -template AbstractVectorGL& AbstractVectorGL::bindVectorTexture(GL::Texture2D& texture) { - texture.bind(VectorTextureUnit); - return *this; -} - -#ifndef DOXYGEN_GENERATING_OUTPUT -template class MAGNUM_SHADERS_EXPORT AbstractVectorGL<2>; -template class MAGNUM_SHADERS_EXPORT AbstractVectorGL<3>; -#endif - -}} diff --git a/src/Magnum/Shaders/AbstractVectorGL.h b/src/Magnum/Shaders/AbstractVectorGL.h deleted file mode 100644 index 95933640d..000000000 --- a/src/Magnum/Shaders/AbstractVectorGL.h +++ /dev/null @@ -1,134 +0,0 @@ -#ifndef Magnum_Shaders_AbstractVectorGL_h -#define Magnum_Shaders_AbstractVectorGL_h -/* - This file is part of Magnum. - - Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, - 2020, 2021 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. -*/ - -/** @file - * @brief Class @ref Magnum::Shaders::AbstractVectorGL, typedef @ref Magnum::Shaders::AbstractVectorGL2D, @ref Magnum::Shaders::AbstractVectorGL3D - * @m_since_latest - */ - -#include "Magnum/GL/AbstractShaderProgram.h" -#include "Magnum/Shaders/GenericGL.h" - -namespace Magnum { namespace Shaders { - -/** -@brief Base for vector OpenGL shaders -@m_since_latest - -See @ref DistanceFieldVectorGL and @ref VectorGL for more information. -@see @ref shaders, @ref AbstractVectorGL2D, @ref AbstractVectorGL3D -*/ -template class AbstractVectorGL: public GL::AbstractShaderProgram { - public: - /** - * @brief Vertex position - * - * @ref shaders-generic "Generic attribute", - * @ref Magnum::Vector2 "Vector2" in 2D, @ref Magnum::Vector3 "Vector3" - * in 3D. - */ - typedef typename GenericGL::Position Position; - - /** - * @brief 2D texture coordinates - * - * @ref shaders-generic "Generic attribute", - * @ref Magnum::Vector2 "Vector2". - */ - typedef typename GenericGL::TextureCoordinates TextureCoordinates; - - enum: UnsignedInt { - /** - * Color shader output. @ref shaders-generic "Generic output", - * present always. Expects three- or four-component floating-point - * or normalized buffer attachment. - */ - ColorOutput = GenericGL::ColorOutput - }; - - /** @brief Copying is not allowed */ - AbstractVectorGL(const AbstractVectorGL&) = delete; - - /** @brief Move constructor */ - AbstractVectorGL(AbstractVectorGL&&) noexcept = default; - - /** @brief Copying is not allowed */ - AbstractVectorGL& operator=(const AbstractVectorGL&) = delete; - - /** @brief Move assignment */ - AbstractVectorGL& operator=(AbstractVectorGL&&) noexcept = default; - - /** @{ - * @name Texture binding - */ - - /** - * @brief Bind vector texture - * @return Reference to self (for method chaining) - * - * @see @ref DistanceFieldVectorGL::Flag::TextureTransformation, - * @ref VectorGL::Flag::TextureTransformation, - * @ref DistanceFieldVectorGL::setTextureMatrix(), - * @ref VectorGL::setTextureMatrix() - */ - AbstractVectorGL& bindVectorTexture(GL::Texture2D& texture); - - /** - * @} - */ - - #ifndef DOXYGEN_GENERATING_OUTPUT - protected: - #else - private: - #endif - /* Those textures are quite specific (and likely reused multiple times - per frame for e.g. text rendering, so put them in a specific slot. - Older iOS (and iOS WebGL) has only 8 texture units, so can't go - above that. Unit 7 is used by TextureTools::DistanceField. */ - enum: Int { VectorTextureUnit = 6 }; - - explicit AbstractVectorGL(NoCreateT) noexcept: GL::AbstractShaderProgram{NoCreate} {} - explicit AbstractVectorGL() = default; - ~AbstractVectorGL() = default; -}; - -/** -@brief Base for two-dimensional vector OpenGL shaders -@m_since_latest -*/ -typedef AbstractVectorGL<2> AbstractVectorGL2D; - -/** -@brief Base for three-dimensional vector OpenGL shader -@m_since_latest -*/ -typedef AbstractVectorGL<3> AbstractVectorGL3D; - -}} - -#endif diff --git a/src/Magnum/Shaders/CMakeLists.txt b/src/Magnum/Shaders/CMakeLists.txt index b5b55947b..e48a91475 100644 --- a/src/Magnum/Shaders/CMakeLists.txt +++ b/src/Magnum/Shaders/CMakeLists.txt @@ -31,7 +31,6 @@ corrade_add_resource(MagnumShaders_RESOURCES_GL resources-gl.conf) set_target_properties(MagnumShaders_RESOURCES_GL-dependencies PROPERTIES FOLDER "Magnum/Shaders") set(MagnumShaders_SRCS - AbstractVectorGL.cpp VertexColorGL.cpp ${MagnumShaders_RESOURCES_GL}) @@ -45,7 +44,6 @@ set(MagnumShaders_GracefulAssert_SRCS set(MagnumShaders_HEADERS DistanceFieldVectorGL.h - AbstractVectorGL.h FlatGL.h GenericGL.h MeshVisualizerGL.h @@ -59,7 +57,6 @@ set(MagnumShaders_HEADERS if(MAGNUM_BUILD_DEPRECATED) list(APPEND MagnumShaders_HEADERS DistanceFieldVector.h - AbstractVector.h Flat.h Generic.h MeshVisualizer.h diff --git a/src/Magnum/Shaders/DistanceFieldVector.frag b/src/Magnum/Shaders/DistanceFieldVector.frag index 51a79e78e..2f45fb27e 100644 --- a/src/Magnum/Shaders/DistanceFieldVector.frag +++ b/src/Magnum/Shaders/DistanceFieldVector.frag @@ -66,7 +66,6 @@ uniform lowp float smoothness /* Textures */ #ifdef EXPLICIT_BINDING -/* See AbstractVector.h for details about the ID */ layout(binding = 6) #endif uniform lowp sampler2D vectorTexture; diff --git a/src/Magnum/Shaders/DistanceFieldVectorGL.cpp b/src/Magnum/Shaders/DistanceFieldVectorGL.cpp index 1ce614bc3..022195230 100644 --- a/src/Magnum/Shaders/DistanceFieldVectorGL.cpp +++ b/src/Magnum/Shaders/DistanceFieldVectorGL.cpp @@ -32,6 +32,7 @@ #include "Magnum/GL/Context.h" #include "Magnum/GL/Extensions.h" #include "Magnum/GL/Shader.h" +#include "Magnum/GL/Texture.h" #include "Magnum/Math/Color.h" #include "Magnum/Math/Matrix3.h" #include "Magnum/Math/Matrix4.h" @@ -40,6 +41,10 @@ namespace Magnum { namespace Shaders { +namespace { + enum: Int { TextureUnit = 6 }; +} + template DistanceFieldVectorGL::DistanceFieldVectorGL(const Flags flags): _flags{flags} { #ifdef MAGNUM_BUILD_STATIC /* Import resources on static build, if not already */ @@ -62,13 +67,13 @@ template DistanceFieldVectorGL::DistanceFiel vert.addSource(flags & Flag::TextureTransformation ? "#define TEXTURE_TRANSFORMATION\n" : "") .addSource(dimensions == 2 ? "#define TWO_DIMENSIONS\n" : "#define THREE_DIMENSIONS\n") .addSource(rs.get("generic.glsl")) - .addSource(rs.get("AbstractVector.vert")); + .addSource(rs.get("Vector.vert")); frag.addSource(rs.get("generic.glsl")) .addSource(rs.get("DistanceFieldVector.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(GL::Shader::compile({vert, frag})); - GL::AbstractShaderProgram::attachShaders({vert, frag}); + attachShaders({vert, frag}); /* ES3 has this done in the shader directly */ #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES2) @@ -76,32 +81,31 @@ template DistanceFieldVectorGL::DistanceFiel if(!context.isExtensionSupported(version)) #endif { - GL::AbstractShaderProgram::bindAttributeLocation(AbstractVectorGL::Position::Location, "position"); - GL::AbstractShaderProgram::bindAttributeLocation(AbstractVectorGL::TextureCoordinates::Location, "textureCoordinates"); + bindAttributeLocation(Position::Location, "position"); + bindAttributeLocation(TextureCoordinates::Location, "textureCoordinates"); } #endif - CORRADE_INTERNAL_ASSERT_OUTPUT(GL::AbstractShaderProgram::link()); + CORRADE_INTERNAL_ASSERT_OUTPUT(link()); #ifndef MAGNUM_TARGET_GLES if(!context.isExtensionSupported(version)) #endif { - _transformationProjectionMatrixUniform = GL::AbstractShaderProgram::uniformLocation("transformationProjectionMatrix"); + _transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); if(flags & Flag::TextureTransformation) - _textureMatrixUniform = GL::AbstractShaderProgram::uniformLocation("textureMatrix"); - _colorUniform = GL::AbstractShaderProgram::uniformLocation("color"); - _outlineColorUniform = GL::AbstractShaderProgram::uniformLocation("outlineColor"); - _outlineRangeUniform = GL::AbstractShaderProgram::uniformLocation("outlineRange"); - _smoothnessUniform = GL::AbstractShaderProgram::uniformLocation("smoothness"); + _textureMatrixUniform = uniformLocation("textureMatrix"); + _colorUniform = uniformLocation("color"); + _outlineColorUniform = uniformLocation("outlineColor"); + _outlineRangeUniform = uniformLocation("outlineRange"); + _smoothnessUniform = uniformLocation("smoothness"); } #ifndef MAGNUM_TARGET_GLES if(!context.isExtensionSupported(version)) #endif { - GL::AbstractShaderProgram::setUniform(GL::AbstractShaderProgram::uniformLocation("vectorTexture"), - AbstractVectorGL::VectorTextureUnit); + setUniform(uniformLocation("vectorTexture"), TextureUnit); } /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ @@ -116,34 +120,39 @@ template DistanceFieldVectorGL::DistanceFiel } template DistanceFieldVectorGL& DistanceFieldVectorGL::setTransformationProjectionMatrix(const MatrixTypeFor& matrix) { - GL::AbstractShaderProgram::setUniform(_transformationProjectionMatrixUniform, matrix); + setUniform(_transformationProjectionMatrixUniform, matrix); return *this; } template DistanceFieldVectorGL& DistanceFieldVectorGL::setTextureMatrix(const Matrix3& matrix) { CORRADE_ASSERT(_flags & Flag::TextureTransformation, "Shaders::DistanceFieldVectorGL::setTextureMatrix(): the shader was not created with texture transformation enabled", *this); - GL::AbstractShaderProgram::setUniform(_textureMatrixUniform, matrix); + setUniform(_textureMatrixUniform, matrix); return *this; } template DistanceFieldVectorGL& DistanceFieldVectorGL::setColor(const Color4& color) { - GL::AbstractShaderProgram::setUniform(_colorUniform, color); + setUniform(_colorUniform, color); return *this; } template DistanceFieldVectorGL& DistanceFieldVectorGL::setOutlineColor(const Color4& color) { - GL::AbstractShaderProgram::setUniform(_outlineColorUniform, color); + setUniform(_outlineColorUniform, color); return *this; } template DistanceFieldVectorGL& DistanceFieldVectorGL::setOutlineRange(Float start, Float end) { - GL::AbstractShaderProgram::setUniform(_outlineRangeUniform, Vector2(start, end)); + setUniform(_outlineRangeUniform, Vector2(start, end)); return *this; } template DistanceFieldVectorGL& DistanceFieldVectorGL::setSmoothness(Float value) { - GL::AbstractShaderProgram::setUniform(_smoothnessUniform, value); + setUniform(_smoothnessUniform, value); + return *this; +} + +template DistanceFieldVectorGL& DistanceFieldVectorGL::bindVectorTexture(GL::Texture2D& texture) { + texture.bind(TextureUnit); return *this; } diff --git a/src/Magnum/Shaders/DistanceFieldVectorGL.h b/src/Magnum/Shaders/DistanceFieldVectorGL.h index f4752b357..c039940fd 100644 --- a/src/Magnum/Shaders/DistanceFieldVectorGL.h +++ b/src/Magnum/Shaders/DistanceFieldVectorGL.h @@ -31,7 +31,8 @@ */ #include "Magnum/DimensionTraits.h" -#include "Magnum/Shaders/AbstractVectorGL.h" +#include "Magnum/GL/AbstractShaderProgram.h" +#include "Magnum/Shaders/GenericGL.h" #include "Magnum/Shaders/visibility.h" namespace Magnum { namespace Shaders { @@ -48,9 +49,10 @@ namespace Implementation { @m_since_latest Renders vector graphics in a form of signed distance field. See -@ref TextureTools::DistanceField for more information. Note that the final -rendered outlook will greatly depend on radius of input distance field and -value passed to @ref setSmoothness(). You need to provide @ref Position and +@ref TextureTools::DistanceField for more information and @ref VectorGL for a +simpler variant of this shader. Note that the final rendered outlook will +greatly depend on radius of input distance field and value passed to +@ref setSmoothness(). You need to provide @ref Position and @ref TextureCoordinates attributes in your triangle mesh and call at least @ref bindVectorTexture(). By default, the shader renders the distance field texture with a white color in an identity transformation, use @@ -79,8 +81,34 @@ Common rendering setup: large zoom levels, make it optional as it might have negative performance impact */ -template class MAGNUM_SHADERS_EXPORT DistanceFieldVectorGL: public AbstractVectorGL { +template class MAGNUM_SHADERS_EXPORT DistanceFieldVectorGL: public GL::AbstractShaderProgram { public: + /** + * @brief Vertex position + * + * @ref shaders-generic "Generic attribute", + * @ref Magnum::Vector2 "Vector2" in 2D, @ref Magnum::Vector3 "Vector3" + * in 3D. + */ + typedef typename GenericGL::Position Position; + + /** + * @brief 2D texture coordinates + * + * @ref shaders-generic "Generic attribute", + * @ref Magnum::Vector2 "Vector2". + */ + typedef typename GenericGL::TextureCoordinates TextureCoordinates; + + enum: UnsignedInt { + /** + * Color shader output. @ref shaders-generic "Generic output", + * present always. Expects three- or four-component floating-point + * or normalized buffer attachment. + */ + ColorOutput = GenericGL::ColorOutput + }; + #ifdef DOXYGEN_GENERATING_OUTPUT /** * @brief Flag @@ -129,12 +157,7 @@ template class MAGNUM_SHADERS_EXPORT DistanceFieldVector * However note that this is a low-level and a potentially dangerous * API, see the documentation of @ref NoCreate for alternatives. */ - explicit DistanceFieldVectorGL(NoCreateT) noexcept - /** @todoc remove workaround when doxygen is sane */ - #ifndef DOXYGEN_GENERATING_OUTPUT - : AbstractVectorGL{NoCreate} - #endif - {} + explicit DistanceFieldVectorGL(NoCreateT) noexcept: GL::AbstractShaderProgram{NoCreate} {} /** @brief Copying is not allowed */ DistanceFieldVectorGL(const DistanceFieldVectorGL&) = delete; @@ -226,13 +249,24 @@ template class MAGNUM_SHADERS_EXPORT DistanceFieldVector * @} */ - #ifndef DOXYGEN_GENERATING_OUTPUT - /* Overloads to remove WTF-factor from method chaining order */ - DistanceFieldVectorGL& bindVectorTexture(GL::Texture2D& texture) { - AbstractVectorGL::bindVectorTexture(texture); - return *this; - } - #endif + /** @{ + * @name Texture binding + */ + + /** + * @brief Bind vector texture + * @return Reference to self (for method chaining) + * + * @see @ref DistanceFieldVectorGL::Flag::TextureTransformation, + * @ref VectorGL::Flag::TextureTransformation, + * @ref DistanceFieldVectorGL::setTextureMatrix(), + * @ref VectorGL::setTextureMatrix() + */ + DistanceFieldVectorGL& bindVectorTexture(GL::Texture2D& texture); + + /** + * @} + */ private: /* Prevent accidentally calling irrelevant functions */ diff --git a/src/Magnum/Shaders/Shaders.h b/src/Magnum/Shaders/Shaders.h index 5e353add8..2e2e31392 100644 --- a/src/Magnum/Shaders/Shaders.h +++ b/src/Magnum/Shaders/Shaders.h @@ -38,15 +38,6 @@ namespace Magnum { namespace Shaders { #ifndef DOXYGEN_GENERATING_OUTPUT -template class AbstractVectorGL; -typedef AbstractVectorGL<2> AbstractVectorGL2D; -typedef AbstractVectorGL<3> AbstractVectorGL3D; -#ifdef MAGNUM_BUILD_DEPRECATED -template using AbstractVector CORRADE_DEPRECATED_ALIAS("use AbstractVectorGL instead") = AbstractVectorGL; -typedef CORRADE_DEPRECATED("use AbstractVectorGL2D instead") AbstractVectorGL2D AbstractVector2D; -typedef CORRADE_DEPRECATED("use AbstractVectorGL3D instead") AbstractVectorGL3D AbstractVector3D; -#endif - template class DistanceFieldVectorGL; typedef DistanceFieldVectorGL<2> DistanceFieldVectorGL2D; typedef DistanceFieldVectorGL<3> DistanceFieldVectorGL3D; diff --git a/src/Magnum/Shaders/Vector.frag b/src/Magnum/Shaders/Vector.frag index ec5da58c5..058c38b09 100644 --- a/src/Magnum/Shaders/Vector.frag +++ b/src/Magnum/Shaders/Vector.frag @@ -46,7 +46,6 @@ uniform lowp vec4 color ; #ifdef EXPLICIT_BINDING -/* See AbstractVector.h for details about the ID */ layout(binding = 6) #endif uniform lowp sampler2D vectorTexture; diff --git a/src/Magnum/Shaders/AbstractVector.vert b/src/Magnum/Shaders/Vector.vert similarity index 100% rename from src/Magnum/Shaders/AbstractVector.vert rename to src/Magnum/Shaders/Vector.vert diff --git a/src/Magnum/Shaders/VectorGL.cpp b/src/Magnum/Shaders/VectorGL.cpp index db401d3f4..214f04ab6 100644 --- a/src/Magnum/Shaders/VectorGL.cpp +++ b/src/Magnum/Shaders/VectorGL.cpp @@ -32,6 +32,7 @@ #include "Magnum/GL/Context.h" #include "Magnum/GL/Extensions.h" #include "Magnum/GL/Shader.h" +#include "Magnum/GL/Texture.h" #include "Magnum/Math/Color.h" #include "Magnum/Math/Matrix3.h" #include "Magnum/Math/Matrix4.h" @@ -40,6 +41,10 @@ namespace Magnum { namespace Shaders { +namespace { + enum: Int { TextureUnit = 6 }; +} + template VectorGL::VectorGL(const Flags flags): _flags{flags} { #ifdef MAGNUM_BUILD_STATIC /* Import resources on static build, if not already */ @@ -62,13 +67,13 @@ template VectorGL::VectorGL(const Flags flag vert.addSource(flags & Flag::TextureTransformation ? "#define TEXTURE_TRANSFORMATION\n" : "") .addSource(dimensions == 2 ? "#define TWO_DIMENSIONS\n" : "#define THREE_DIMENSIONS\n") .addSource(rs.get("generic.glsl")) - .addSource(rs.get("AbstractVector.vert")); + .addSource(rs.get("Vector.vert")); frag.addSource(rs.get("generic.glsl")) .addSource(rs.get("Vector.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(GL::Shader::compile({vert, frag})); - GL::AbstractShaderProgram::attachShaders({vert, frag}); + attachShaders({vert, frag}); /* ES3 has this done in the shader directly */ #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES2) @@ -76,29 +81,29 @@ template VectorGL::VectorGL(const Flags flag if(!context.isExtensionSupported(version)) #endif { - GL::AbstractShaderProgram::bindAttributeLocation(AbstractVectorGL::Position::Location, "position"); - GL::AbstractShaderProgram::bindAttributeLocation(AbstractVectorGL::TextureCoordinates::Location, "textureCoordinates"); + bindAttributeLocation(Position::Location, "position"); + bindAttributeLocation(TextureCoordinates::Location, "textureCoordinates"); } #endif - CORRADE_INTERNAL_ASSERT_OUTPUT(GL::AbstractShaderProgram::link()); + CORRADE_INTERNAL_ASSERT_OUTPUT(link()); #ifndef MAGNUM_TARGET_GLES if(!context.isExtensionSupported(version)) #endif { - _transformationProjectionMatrixUniform = GL::AbstractShaderProgram::uniformLocation("transformationProjectionMatrix"); + _transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); if(flags & Flag::TextureTransformation) - _textureMatrixUniform = GL::AbstractShaderProgram::uniformLocation("textureMatrix"); - _backgroundColorUniform = GL::AbstractShaderProgram::uniformLocation("backgroundColor"); - _colorUniform = GL::AbstractShaderProgram::uniformLocation("color"); + _textureMatrixUniform = uniformLocation("textureMatrix"); + _backgroundColorUniform = uniformLocation("backgroundColor"); + _colorUniform = uniformLocation("color"); } #ifndef MAGNUM_TARGET_GLES if(!context.isExtensionSupported(version)) #endif { - GL::AbstractShaderProgram::setUniform(GL::AbstractShaderProgram::uniformLocation("vectorTexture"), AbstractVectorGL::VectorTextureUnit); + setUniform(uniformLocation("vectorTexture"), TextureUnit); } /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ @@ -111,24 +116,29 @@ template VectorGL::VectorGL(const Flags flag } template VectorGL& VectorGL::setTransformationProjectionMatrix(const MatrixTypeFor& matrix) { - GL::AbstractShaderProgram::setUniform(_transformationProjectionMatrixUniform, matrix); + setUniform(_transformationProjectionMatrixUniform, matrix); return *this; } template VectorGL& VectorGL::setTextureMatrix(const Matrix3& matrix) { CORRADE_ASSERT(_flags & Flag::TextureTransformation, "Shaders::VectorGL::setTextureMatrix(): the shader was not created with texture transformation enabled", *this); - GL::AbstractShaderProgram::setUniform(_textureMatrixUniform, matrix); + setUniform(_textureMatrixUniform, matrix); return *this; } template VectorGL& VectorGL::setBackgroundColor(const Color4& color) { - GL::AbstractShaderProgram::setUniform(_backgroundColorUniform, color); + setUniform(_backgroundColorUniform, color); return *this; } template VectorGL& VectorGL::setColor(const Color4& color) { - GL::AbstractShaderProgram::setUniform(_colorUniform, color); + setUniform(_colorUniform, color); + return *this; +} + +template VectorGL& VectorGL::bindVectorTexture(GL::Texture2D& texture) { + texture.bind(TextureUnit); return *this; } diff --git a/src/Magnum/Shaders/VectorGL.h b/src/Magnum/Shaders/VectorGL.h index d788f18d3..dd5694fc4 100644 --- a/src/Magnum/Shaders/VectorGL.h +++ b/src/Magnum/Shaders/VectorGL.h @@ -31,7 +31,8 @@ */ #include "Magnum/DimensionTraits.h" -#include "Magnum/Shaders/AbstractVectorGL.h" +#include "Magnum/GL/AbstractShaderProgram.h" +#include "Magnum/Shaders/GenericGL.h" #include "Magnum/Shaders/visibility.h" namespace Magnum { namespace Shaders { @@ -75,8 +76,34 @@ Common rendering setup: @see @ref shaders, @ref VectorGL2D, @ref VectorGL3D */ -template class MAGNUM_SHADERS_EXPORT VectorGL: public AbstractVectorGL { +template class MAGNUM_SHADERS_EXPORT VectorGL: public GL::AbstractShaderProgram { public: + /** + * @brief Vertex position + * + * @ref shaders-generic "Generic attribute", + * @ref Magnum::Vector2 "Vector2" in 2D, @ref Magnum::Vector3 "Vector3" + * in 3D. + */ + typedef typename GenericGL::Position Position; + + /** + * @brief 2D texture coordinates + * + * @ref shaders-generic "Generic attribute", + * @ref Magnum::Vector2 "Vector2". + */ + typedef typename GenericGL::TextureCoordinates TextureCoordinates; + + enum: UnsignedInt { + /** + * Color shader output. @ref shaders-generic "Generic output", + * present always. Expects three- or four-component floating-point + * or normalized buffer attachment. + */ + ColorOutput = GenericGL::ColorOutput + }; + #ifdef DOXYGEN_GENERATING_OUTPUT /** * @brief Flag @@ -125,12 +152,7 @@ template class MAGNUM_SHADERS_EXPORT VectorGL: public Ab * However note that this is a low-level and a potentially dangerous * API, see the documentation of @ref NoCreate for alternatives. */ - explicit VectorGL(NoCreateT) noexcept - /** @todoc remove workaround when doxygen is sane */ - #ifndef DOXYGEN_GENERATING_OUTPUT - : AbstractVectorGL{NoCreate} - #endif - {} + explicit VectorGL(NoCreateT) noexcept: GL::AbstractShaderProgram{NoCreate} {} /** @brief Copying is not allowed */ VectorGL(const VectorGL&) = delete; @@ -195,13 +217,21 @@ template class MAGNUM_SHADERS_EXPORT VectorGL: public Ab * @} */ - #ifndef DOXYGEN_GENERATING_OUTPUT - /* Overloads to remove WTF-factor from method chaining order */ - VectorGL& bindVectorTexture(GL::Texture2D& texture) { - AbstractVectorGL::bindVectorTexture(texture); - return *this; - } - #endif + /** @{ + * @name Texture binding + */ + + /** + * @brief Bind vector texture + * @return Reference to self (for method chaining) + * + * @see @ref Flag::TextureTransformation, @ref setTextureMatrix()s + */ + VectorGL& bindVectorTexture(GL::Texture2D& texture); + + /** + * @} + */ private: /* Prevent accidentally calling irrelevant functions */ diff --git a/src/Magnum/Shaders/resources-gl.conf b/src/Magnum/Shaders/resources-gl.conf index f545425c9..57a1c346e 100644 --- a/src/Magnum/Shaders/resources-gl.conf +++ b/src/Magnum/Shaders/resources-gl.conf @@ -1,8 +1,5 @@ group=MagnumShadersGL -[file] -filename=AbstractVector.vert - [file] filename=Flat.vert @@ -30,6 +27,9 @@ filename=Phong.vert [file] filename=Phong.frag +[file] +filename=Vector.vert + [file] filename=Vector.frag diff --git a/src/Magnum/Text/Renderer.cpp b/src/Magnum/Text/Renderer.cpp index 088561041..e6dfd0762 100644 --- a/src/Magnum/Text/Renderer.cpp +++ b/src/Magnum/Text/Renderer.cpp @@ -33,7 +33,7 @@ #include "Magnum/GL/Extensions.h" #include "Magnum/GL/Mesh.h" #include "Magnum/Math/Functions.h" -#include "Magnum/Shaders/AbstractVectorGL.h" +#include "Magnum/Shaders/GenericGL.h" #include "Magnum/Text/AbstractFont.h" #include "Magnum/Text/GlyphCache.h" @@ -255,9 +255,9 @@ template std::tuple Renderer(r); mesh.addVertexBuffer(vertexBuffer, 0, - typename Shaders::AbstractVectorGL::Position( - Shaders::AbstractVectorGL::Position::Components::Two), - typename Shaders::AbstractVectorGL::TextureCoordinates()); + typename Shaders::GenericGL::Position( + Shaders::GenericGL::Position::Components::Two), + typename Shaders::GenericGL::TextureCoordinates()); return r; } @@ -320,8 +320,9 @@ AbstractRenderer::~AbstractRenderer() = default; template Renderer::Renderer(AbstractFont& font, const GlyphCache& cache, const Float size, const Alignment alignment): AbstractRenderer(font, cache, size, alignment) { /* Finalize mesh configuration */ _mesh.addVertexBuffer(_vertexBuffer, 0, - typename Shaders::AbstractVectorGL::Position(Shaders::AbstractVectorGL::Position::Components::Two), - typename Shaders::AbstractVectorGL::TextureCoordinates()); + typename Shaders::GenericGL::Position( + Shaders::GenericGL::Position::Components::Two), + typename Shaders::GenericGL::TextureCoordinates()); } void AbstractRenderer::reserve(const uint32_t glyphCount, const GL::BufferUsage vertexBufferUsage, const GL::BufferUsage indexBufferUsage) { diff --git a/src/Magnum/Text/Renderer.h b/src/Magnum/Text/Renderer.h index eb9edfdf9..8640ea982 100644 --- a/src/Magnum/Text/Renderer.h +++ b/src/Magnum/Text/Renderer.h @@ -202,7 +202,7 @@ asynchronous buffer updates. There is no similar extension in WebGL, thus plain (and slow) buffer updates are used there. @see @ref Renderer2D, @ref Renderer3D, @ref AbstractFont, - @ref Shaders::AbstractVectorGL + @ref Shaders::VectorGL, @ref Shaders::DistanceFieldVectorGL */ template class MAGNUM_TEXT_EXPORT Renderer: public AbstractRenderer { public: @@ -217,8 +217,9 @@ template class MAGNUM_TEXT_EXPORT Renderer: public Abstr * @param usage Usage of vertex and index buffer * @param alignment Text alignment * - * Returns mesh prepared for use with @ref Shaders::AbstractVectorGL - * subclasses and rectangle spanning the rendered text. + * Returns mesh prepared for use with @ref Shaders::VectorGL or + * @ref Shaders::DistanceFieldVectorGL and rectangle spanning the + * rendered text. */ static std::tuple render(AbstractFont& font, const GlyphCache& cache, Float size, const std::string& text, GL::Buffer& vertexBuffer, GL::Buffer& indexBuffer, GL::BufferUsage usage, Alignment alignment = Alignment::LineLeft);