Browse Source

Shaders: group uniform setters and texture binding functions together.

Otherwise it would get way too messy with the introduction of uniform
buffers.
pull/518/head
Vladimír Vondruš 5 years ago
parent
commit
f70bbef473
  1. 8
      src/Magnum/Shaders/AbstractVectorGL.h
  2. 8
      src/Magnum/Shaders/DistanceFieldVectorGL.h
  3. 14
      src/Magnum/Shaders/FlatGL.cpp
  4. 38
      src/Magnum/Shaders/FlatGL.h
  5. 88
      src/Magnum/Shaders/MeshVisualizerGL.h
  6. 70
      src/Magnum/Shaders/PhongGL.cpp
  7. 148
      src/Magnum/Shaders/PhongGL.h
  8. 8
      src/Magnum/Shaders/VectorGL.h
  9. 8
      src/Magnum/Shaders/VertexColorGL.h

8
src/Magnum/Shaders/AbstractVectorGL.h

@ -82,6 +82,10 @@ template<UnsignedInt dimensions> class AbstractVectorGL: public GL::AbstractShad
/** @brief Move assignment */
AbstractVectorGL<dimensions>& operator=(AbstractVectorGL<dimensions>&&) noexcept = default;
/** @{
* @name Texture binding
*/
/**
* @brief Bind vector texture
* @return Reference to self (for method chaining)
@ -93,6 +97,10 @@ template<UnsignedInt dimensions> class AbstractVectorGL: public GL::AbstractShad
*/
AbstractVectorGL<dimensions>& bindVectorTexture(GL::Texture2D& texture);
/**
* @}
*/
#ifndef DOXYGEN_GENERATING_OUTPUT
protected:
#else

8
src/Magnum/Shaders/DistanceFieldVectorGL.h

@ -154,6 +154,10 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT DistanceFieldVector
*/
Flags flags() const { return _flags; }
/** @{
* @name Uniform setters
*/
/**
* @brief Set transformation and projection matrix
* @return Reference to self (for method chaining)
@ -218,6 +222,10 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT DistanceFieldVector
*/
DistanceFieldVectorGL<dimensions>& setSmoothness(Float value);
/**
* @}
*/
#ifndef DOXYGEN_GENERATING_OUTPUT
/* Overloads to remove WTF-factor from method chaining order */
DistanceFieldVectorGL<dimensions>& bindVectorTexture(GL::Texture2D& texture) {

14
src/Magnum/Shaders/FlatGL.cpp

@ -168,13 +168,6 @@ template<UnsignedInt dimensions> FlatGL<dimensions>& FlatGL<dimensions>::setColo
return *this;
}
template<UnsignedInt dimensions> FlatGL<dimensions>& FlatGL<dimensions>::bindTexture(GL::Texture2D& texture) {
CORRADE_ASSERT(_flags & Flag::Textured,
"Shaders::FlatGL::bindTexture(): the shader was not created with texturing enabled", *this);
texture.bind(TextureUnit);
return *this;
}
template<UnsignedInt dimensions> FlatGL<dimensions>& FlatGL<dimensions>::setAlphaMask(Float mask) {
CORRADE_ASSERT(_flags & Flag::AlphaMask,
"Shaders::FlatGL::setAlphaMask(): the shader was not created with alpha mask enabled", *this);
@ -191,6 +184,13 @@ template<UnsignedInt dimensions> FlatGL<dimensions>& FlatGL<dimensions>::setObje
}
#endif
template<UnsignedInt dimensions> FlatGL<dimensions>& FlatGL<dimensions>::bindTexture(GL::Texture2D& texture) {
CORRADE_ASSERT(_flags & Flag::Textured,
"Shaders::FlatGL::bindTexture(): the shader was not created with texturing enabled", *this);
texture.bind(TextureUnit);
return *this;
}
template class MAGNUM_SHADERS_EXPORT FlatGL<2>;
template class MAGNUM_SHADERS_EXPORT FlatGL<3>;

38
src/Magnum/Shaders/FlatGL.h

@ -416,6 +416,10 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT FlatGL: public GL::
/** @brief Flags */
Flags flags() const { return _flags; }
/** @{
* @name Uniform setters
*/
/**
* @brief Set transformation and projection matrix
* @return Reference to self (for method chaining)
@ -446,17 +450,6 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT FlatGL: public GL::
*/
FlatGL<dimensions>& setColor(const Magnum::Color4& color);
/**
* @brief Bind a color texture
* @return Reference to self (for method chaining)
*
* Expects that the shader was created with @ref Flag::Textured
* enabled.
* @see @ref setColor(), @ref Flag::TextureTransformation,
* @ref setTextureMatrix()
*/
FlatGL<dimensions>& bindTexture(GL::Texture2D& texture);
/**
* @brief Set alpha mask value
* @return Reference to self (for method chaining)
@ -489,6 +482,29 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT FlatGL: public GL::
FlatGL<dimensions>& setObjectId(UnsignedInt id);
#endif
/**
* @}
*/
/** @{
* @name Texture binding
*/
/**
* @brief Bind a color texture
* @return Reference to self (for method chaining)
*
* Expects that the shader was created with @ref Flag::Textured
* enabled.
* @see @ref setColor(), @ref Flag::TextureTransformation,
* @ref setTextureMatrix()
*/
FlatGL<dimensions>& bindTexture(GL::Texture2D& texture);
/**
* @}
*/
private:
/* Prevent accidentally calling irrelevant functions */
#ifndef MAGNUM_TARGET_GLES

88
src/Magnum/Shaders/MeshVisualizerGL.h

@ -249,6 +249,10 @@ class MAGNUM_SHADERS_EXPORT MeshVisualizerGL2D: public Implementation::MeshVisua
return Flag(UnsignedShort(Implementation::MeshVisualizerGLBase::_flags));
}
/** @{
* @name Uniform setters
*/
/**
* @brief Set transformation and projection matrix
* @return Reference to self (for method chaining)
@ -309,11 +313,6 @@ class MAGNUM_SHADERS_EXPORT MeshVisualizerGL2D: public Implementation::MeshVisua
MeshVisualizerGL2D& setColorMapTransformation(Float offset, Float scale) {
return static_cast<MeshVisualizerGL2D&>(Implementation::MeshVisualizerGLBase::setColorMapTransformation(offset, scale));
}
/** @copydoc MeshVisualizerGL3D::bindColorMapTexture() */
MeshVisualizerGL2D& bindColorMapTexture(GL::Texture2D& texture) {
return static_cast<MeshVisualizerGL2D&>(Implementation::MeshVisualizerGLBase::bindColorMapTexture(texture));
}
#endif
/**
@ -326,6 +325,25 @@ class MAGNUM_SHADERS_EXPORT MeshVisualizerGL2D: public Implementation::MeshVisua
*/
MeshVisualizerGL2D& setSmoothness(Float smoothness);
/**
* @}
*/
/** @{
* @name Texture binding
*/
#ifndef MAGNUM_TARGET_GLES2
/** @copydoc MeshVisualizerGL3D::bindColorMapTexture() */
MeshVisualizerGL2D& bindColorMapTexture(GL::Texture2D& texture) {
return static_cast<MeshVisualizerGL2D&>(Implementation::MeshVisualizerGLBase::bindColorMapTexture(texture));
}
#endif
/**
* @}
*/
private:
Int _transformationProjectionMatrixUniform{0};
};
@ -801,6 +819,10 @@ class MAGNUM_SHADERS_EXPORT MeshVisualizerGL3D: public Implementation::MeshVisua
return Flag(UnsignedShort(Implementation::MeshVisualizerGLBase::_flags));
}
/** @{
* @name Uniform setters
*/
#ifdef MAGNUM_BUILD_DEPRECATED
/**
* @brief Set transformation and projection matrix
@ -933,27 +955,6 @@ class MAGNUM_SHADERS_EXPORT MeshVisualizerGL3D: public Implementation::MeshVisua
MeshVisualizerGL3D& setColorMapTransformation(Float offset, Float scale) {
return static_cast<MeshVisualizerGL3D&>(Implementation::MeshVisualizerGLBase::setColorMapTransformation(offset, scale));
}
/**
* @brief Bind a color map texture
* @return Reference to self (for method chaining)
* @m_since{2020,06}
*
* See also @ref setColorMapTransformation(). Expects that either
* @ref Flag::InstancedObjectId or @ref Flag::PrimitiveId /
* @ref Flag::PrimitiveIdFromVertexId is enabled.
* @requires_gles30 Object ID visualization requires integer attributes
* while primitive ID visualization requires the `gl_VertexID` /
* `gl_PrimitiveID` builtins, neither of which is available in
* OpenGL ES 2.0.
* @requires_webgl20 Object ID visualization requires integer
* attributes while primitive ID visualization requires at least
* the `gl_VertexID` builtin, neither of which is available in
* WebGL 1.
*/
MeshVisualizerGL3D& bindColorMapTexture(GL::Texture2D& texture) {
return static_cast<MeshVisualizerGL3D&>(Implementation::MeshVisualizerGLBase::bindColorMapTexture(texture));
}
#endif
#if !defined(MAGNUM_TARGET_GLES2) && !defined(MAGNUM_TARGET_WEBGL)
@ -1008,6 +1009,41 @@ class MAGNUM_SHADERS_EXPORT MeshVisualizerGL3D: public Implementation::MeshVisua
*/
MeshVisualizerGL3D& setSmoothness(Float smoothness);
/**
* @}
*/
/** @{
* @name Texture binding
*/
#ifndef MAGNUM_TARGET_GLES2
/**
* @brief Bind a color map texture
* @return Reference to self (for method chaining)
* @m_since{2020,06}
*
* See also @ref setColorMapTransformation(). Expects that either
* @ref Flag::InstancedObjectId or @ref Flag::PrimitiveId /
* @ref Flag::PrimitiveIdFromVertexId is enabled.
* @requires_gles30 Object ID visualization requires integer attributes
* while primitive ID visualization requires the `gl_VertexID` /
* `gl_PrimitiveID` builtins, neither of which is available in
* OpenGL ES 2.0.
* @requires_webgl20 Object ID visualization requires integer
* attributes while primitive ID visualization requires at least
* the `gl_VertexID` builtin, neither of which is available in
* WebGL 1.
*/
MeshVisualizerGL3D& bindColorMapTexture(GL::Texture2D& texture) {
return static_cast<MeshVisualizerGL3D&>(Implementation::MeshVisualizerGLBase::bindColorMapTexture(texture));
}
#endif
/**
* @}
*/
private:
Int _transformationMatrixUniform{0},
_projectionMatrixUniform{7};

70
src/Magnum/Shaders/PhongGL.cpp

@ -280,51 +280,16 @@ PhongGL& PhongGL::setAmbientColor(const Magnum::Color4& color) {
return *this;
}
PhongGL& PhongGL::bindAmbientTexture(GL::Texture2D& texture) {
CORRADE_ASSERT(_flags & Flag::AmbientTexture,
"Shaders::PhongGL::bindAmbientTexture(): the shader was not created with ambient texture enabled", *this);
texture.bind(AmbientTextureUnit);
return *this;
}
PhongGL& PhongGL::setDiffuseColor(const Magnum::Color4& color) {
if(_lightCount) setUniform(_diffuseColorUniform, color);
return *this;
}
PhongGL& PhongGL::bindDiffuseTexture(GL::Texture2D& texture) {
CORRADE_ASSERT(_flags & Flag::DiffuseTexture,
"Shaders::PhongGL::bindDiffuseTexture(): the shader was not created with diffuse texture enabled", *this);
if(_lightCount) texture.bind(DiffuseTextureUnit);
return *this;
}
PhongGL& PhongGL::setSpecularColor(const Magnum::Color4& color) {
if(_lightCount) setUniform(_specularColorUniform, color);
return *this;
}
PhongGL& PhongGL::bindSpecularTexture(GL::Texture2D& texture) {
CORRADE_ASSERT(_flags & Flag::SpecularTexture,
"Shaders::PhongGL::bindSpecularTexture(): the shader was not created with specular texture enabled", *this);
if(_lightCount) texture.bind(SpecularTextureUnit);
return *this;
}
PhongGL& PhongGL::bindNormalTexture(GL::Texture2D& texture) {
CORRADE_ASSERT(_flags & Flag::NormalTexture,
"Shaders::PhongGL::bindNormalTexture(): the shader was not created with normal texture enabled", *this);
if(_lightCount) texture.bind(NormalTextureUnit);
return *this;
}
PhongGL& PhongGL::bindTextures(GL::Texture2D* ambient, GL::Texture2D* diffuse, GL::Texture2D* specular, GL::Texture2D* normal) {
CORRADE_ASSERT(_flags & (Flag::AmbientTexture|Flag::DiffuseTexture|Flag::SpecularTexture|Flag::NormalTexture),
"Shaders::PhongGL::bindTextures(): the shader was not created with any textures enabled", *this);
GL::AbstractTexture::bind(AmbientTextureUnit, {ambient, diffuse, specular, normal});
return *this;
}
PhongGL& PhongGL::setShininess(Float shininess) {
if(_lightCount) setUniform(_shininessUniform, shininess);
return *this;
@ -503,6 +468,41 @@ PhongGL& PhongGL::setLightRange(const UnsignedInt id, const Float range) {
return *this;
}
PhongGL& PhongGL::bindAmbientTexture(GL::Texture2D& texture) {
CORRADE_ASSERT(_flags & Flag::AmbientTexture,
"Shaders::PhongGL::bindAmbientTexture(): the shader was not created with ambient texture enabled", *this);
texture.bind(AmbientTextureUnit);
return *this;
}
PhongGL& PhongGL::bindDiffuseTexture(GL::Texture2D& texture) {
CORRADE_ASSERT(_flags & Flag::DiffuseTexture,
"Shaders::PhongGL::bindDiffuseTexture(): the shader was not created with diffuse texture enabled", *this);
if(_lightCount) texture.bind(DiffuseTextureUnit);
return *this;
}
PhongGL& PhongGL::bindSpecularTexture(GL::Texture2D& texture) {
CORRADE_ASSERT(_flags & Flag::SpecularTexture,
"Shaders::PhongGL::bindSpecularTexture(): the shader was not created with specular texture enabled", *this);
if(_lightCount) texture.bind(SpecularTextureUnit);
return *this;
}
PhongGL& PhongGL::bindNormalTexture(GL::Texture2D& texture) {
CORRADE_ASSERT(_flags & Flag::NormalTexture,
"Shaders::PhongGL::bindNormalTexture(): the shader was not created with normal texture enabled", *this);
if(_lightCount) texture.bind(NormalTextureUnit);
return *this;
}
PhongGL& PhongGL::bindTextures(GL::Texture2D* ambient, GL::Texture2D* diffuse, GL::Texture2D* specular, GL::Texture2D* normal) {
CORRADE_ASSERT(_flags & (Flag::AmbientTexture|Flag::DiffuseTexture|Flag::SpecularTexture|Flag::NormalTexture),
"Shaders::PhongGL::bindTextures(): the shader was not created with any textures enabled", *this);
GL::AbstractTexture::bind(AmbientTextureUnit, {ambient, diffuse, specular, normal});
return *this;
}
Debug& operator<<(Debug& debug, const PhongGL::Flag value) {
debug << "Shaders::PhongGL::Flag" << Debug::nospace;

148
src/Magnum/Shaders/PhongGL.h

@ -604,6 +604,10 @@ class MAGNUM_SHADERS_EXPORT PhongGL: public GL::AbstractShaderProgram {
/** @brief Light count */
UnsignedInt lightCount() const { return _lightCount; }
/** @{
* @name Uniform setters
*/
/**
* @brief Set ambient color
* @return Reference to self (for method chaining)
@ -615,17 +619,6 @@ class MAGNUM_SHADERS_EXPORT PhongGL: public GL::AbstractShaderProgram {
*/
PhongGL& setAmbientColor(const Magnum::Color4& color);
/**
* @brief Bind an ambient texture
* @return Reference to self (for method chaining)
*
* Expects that the shader was created with @ref Flag::AmbientTexture
* enabled.
* @see @ref bindTextures(), @ref setAmbientColor(),
* @ref Shaders-PhongGL-lights-ambient
*/
PhongGL& bindAmbientTexture(GL::Texture2D& texture);
/**
* @brief Set diffuse color
* @return Reference to self (for method chaining)
@ -637,17 +630,6 @@ class MAGNUM_SHADERS_EXPORT PhongGL: public GL::AbstractShaderProgram {
*/
PhongGL& setDiffuseColor(const Magnum::Color4& color);
/**
* @brief Bind a diffuse texture
* @return Reference to self (for method chaining)
*
* Expects that the shader was created with @ref Flag::DiffuseTexture
* enabled. If @ref lightCount() is zero, this function is a no-op, as
* diffuse color doesn't contribute to the output in that case.
* @see @ref bindTextures(), @ref setDiffuseColor()
*/
PhongGL& bindDiffuseTexture(GL::Texture2D& texture);
/**
* @brief Set normal texture scale
* @return Reference to self (for method chaining)
@ -666,20 +648,6 @@ class MAGNUM_SHADERS_EXPORT PhongGL: public GL::AbstractShaderProgram {
*/
PhongGL& setNormalTextureScale(Float scale);
/**
* @brief Bind a normal texture
* @return Reference to self (for method chaining)
* @m_since{2019,10}
*
* Expects that the shader was created with @ref Flag::NormalTexture
* enabled and the @ref Tangent attribute was supplied. If
* @ref lightCount() is zero, this function is a no-op, as normals
* don't contribute to the output in that case.
* @see @ref Shaders-PhongGL-normal-mapping,
* @ref bindTextures(), @ref setNormalTextureScale()
*/
PhongGL& bindNormalTexture(GL::Texture2D& texture);
/**
* @brief Set specular color
* @return Reference to self (for method chaining)
@ -694,36 +662,6 @@ class MAGNUM_SHADERS_EXPORT PhongGL: public GL::AbstractShaderProgram {
*/
PhongGL& setSpecularColor(const Magnum::Color4& color);
/**
* @brief Bind a specular texture
* @return Reference to self (for method chaining)
*
* Expects that the shader was created with @ref Flag::SpecularTexture
* enabled. If @ref lightCount() is zero, this function is a no-op, as
* specular color doesn't contribute to the output in that case.
* @see @ref bindTextures(), @ref setSpecularColor()
*/
PhongGL& bindSpecularTexture(GL::Texture2D& texture);
/**
* @brief Bind textures
* @return Reference to self (for method chaining)
*
* A particular texture has effect only if particular texture flag from
* @ref PhongGL::Flag "Flag" is set, you can use @cpp nullptr @ce for
* the rest. Expects that the shader was created with at least one of
* @ref Flag::AmbientTexture, @ref Flag::DiffuseTexture,
* @ref Flag::SpecularTexture or @ref Flag::NormalTexture enabled. More
* efficient than setting each texture separately.
* @see @ref bindAmbientTexture(), @ref bindDiffuseTexture(),
* @ref bindSpecularTexture(), @ref bindNormalTexture()
*/
PhongGL& bindTextures(GL::Texture2D* ambient, GL::Texture2D* diffuse, GL::Texture2D* specular, GL::Texture2D* normal
#ifdef MAGNUM_BUILD_DEPRECATED
= nullptr
#endif
);
/**
* @brief Set shininess
* @return Reference to self (for method chaining)
@ -1009,6 +947,84 @@ class MAGNUM_SHADERS_EXPORT PhongGL: public GL::AbstractShaderProgram {
*/
PhongGL& setLightRange(UnsignedInt id, Float range);
/**
* @}
*/
/** @{
* @name Texture binding
*/
/**
* @brief Bind a diffuse texture
* @return Reference to self (for method chaining)
*
* Expects that the shader was created with @ref Flag::DiffuseTexture
* enabled. If @ref lightCount() is zero, this function is a no-op, as
* diffuse color doesn't contribute to the output in that case.
* @see @ref bindTextures(), @ref setDiffuseColor()
*/
PhongGL& bindDiffuseTexture(GL::Texture2D& texture);
/**
* @brief Bind an ambient texture
* @return Reference to self (for method chaining)
*
* Expects that the shader was created with @ref Flag::AmbientTexture
* enabled.
* @see @ref bindTextures(), @ref setAmbientColor(),
* @ref Shaders-PhongGL-lights-ambient
*/
PhongGL& bindAmbientTexture(GL::Texture2D& texture);
/**
* @brief Bind a normal texture
* @return Reference to self (for method chaining)
* @m_since{2019,10}
*
* Expects that the shader was created with @ref Flag::NormalTexture
* enabled and the @ref Tangent attribute was supplied. If
* @ref lightCount() is zero, this function is a no-op, as normals
* don't contribute to the output in that case.
* @see @ref Shaders-PhongGL-normal-mapping,
* @ref bindTextures(), @ref setNormalTextureScale()
*/
PhongGL& bindNormalTexture(GL::Texture2D& texture);
/**
* @brief Bind a specular texture
* @return Reference to self (for method chaining)
*
* Expects that the shader was created with @ref Flag::SpecularTexture
* enabled. If @ref lightCount() is zero, this function is a no-op, as
* specular color doesn't contribute to the output in that case.
* @see @ref bindTextures(), @ref setSpecularColor()
*/
PhongGL& bindSpecularTexture(GL::Texture2D& texture);
/**
* @brief Bind textures
* @return Reference to self (for method chaining)
*
* A particular texture has effect only if particular texture flag from
* @ref PhongGL::Flag "Flag" is set, you can use @cpp nullptr @ce for
* the rest. Expects that the shader was created with at least one of
* @ref Flag::AmbientTexture, @ref Flag::DiffuseTexture,
* @ref Flag::SpecularTexture or @ref Flag::NormalTexture enabled. More
* efficient than setting each texture separately.
* @see @ref bindAmbientTexture(), @ref bindDiffuseTexture(),
* @ref bindSpecularTexture(), @ref bindNormalTexture()
*/
PhongGL& bindTextures(GL::Texture2D* ambient, GL::Texture2D* diffuse, GL::Texture2D* specular, GL::Texture2D* normal
#ifdef MAGNUM_BUILD_DEPRECATED
= nullptr
#endif
);
/**
* @}
*/
private:
/* Prevent accidentally calling irrelevant functions */
#ifndef MAGNUM_TARGET_GLES

8
src/Magnum/Shaders/VectorGL.h

@ -150,6 +150,10 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT VectorGL: public Ab
*/
Flags flags() const { return _flags; }
/** @{
* @name Uniform setters
*/
/**
* @brief Set transformation and projection matrix
* @return Reference to self (for method chaining)
@ -187,6 +191,10 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT VectorGL: public Ab
*/
VectorGL<dimensions>& setColor(const Color4& color);
/**
* @}
*/
#ifndef DOXYGEN_GENERATING_OUTPUT
/* Overloads to remove WTF-factor from method chaining order */
VectorGL<dimensions>& bindVectorTexture(GL::Texture2D& texture) {

8
src/Magnum/Shaders/VertexColorGL.h

@ -135,6 +135,10 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT VertexColorGL: publ
/** @brief Move assignment */
VertexColorGL<dimensions>& operator=(VertexColorGL<dimensions>&&) noexcept = default;
/** @{
* @name Uniform setters
*/
/**
* @brief Set transformation and projection matrix
* @return Reference to self (for method chaining)
@ -143,6 +147,10 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT VertexColorGL: publ
*/
VertexColorGL<dimensions>& setTransformationProjectionMatrix(const MatrixTypeFor<dimensions, Float>& matrix);
/**
* @}
*/
private:
/* Prevent accidentally calling irrelevant functions */
#ifndef MAGNUM_TARGET_GLES

Loading…
Cancel
Save