diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-01-14 11:38:09 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-01-14 11:38:09 +0000 |
commit | 08128c14ec49a2b357e830c832ccd0a8e63c175d (patch) | |
tree | 6634dea4e5d8bac32bdcf1c2ea68000874c61a5a | |
parent | We only need 3x3 matrix for model rotation spec (diff) | |
download | ilt-08128c14ec49a2b357e830c832ccd0a8e63c175d.tar.bz2 ilt-08128c14ec49a2b357e830c832ccd0a8e63c175d.tar.xz ilt-08128c14ec49a2b357e830c832ccd0a8e63c175d.zip |
Add traits wrapper for setting uniforms
-rw-r--r-- | gfx/gl/sceneRenderer.cpp | 12 | ||||
-rw-r--r-- | gfx/gl/sceneShader.cpp | 12 | ||||
-rw-r--r-- | gfx/gl/shadowMapper.cpp | 13 | ||||
-rw-r--r-- | gfx/gl/uiShader.cpp | 3 | ||||
-rw-r--r-- | gfx/gl/uiShader.h | 3 | ||||
-rw-r--r-- | lib/gl_traits.h | 56 |
6 files changed, 79 insertions, 20 deletions
diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index c856279..4ae4297 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -143,11 +143,11 @@ SceneRenderer::DirectionalLightProgram::setDirectionalLight(const RGB & c, const const GlobalPosition3D & p, const std::span<const glm::mat4x4> lvp, const std::span<const TextureRelRegion> shadowMapRegions, std::size_t maps) const { - glUniform3fv(colourLoc, 1, glm::value_ptr(c)); + glUniform(colourLoc, c); const auto nd = glm::normalize(d); - glUniform3fv(directionLoc, 1, glm::value_ptr(nd)); - glUniform3iv(lightPointLoc, 1, glm::value_ptr(p)); - glUniform1ui(lightViewProjectionCountLoc, static_cast<GLuint>(maps)); - glUniformMatrix4fv(lightViewProjectionLoc, static_cast<GLsizei>(maps), GL_FALSE, glm::value_ptr(lvp.front())); - glUniform4fv(lightViewShadowMapRegionLoc, static_cast<GLsizei>(maps), glm::value_ptr(shadowMapRegions.front())); + glUniform(directionLoc, nd); + glUniform(lightPointLoc, p); + glUniform(lightViewProjectionCountLoc, static_cast<GLuint>(maps)); + glUniform(lightViewProjectionLoc, lvp); + glUniform(lightViewShadowMapRegionLoc, shadowMapRegions); } diff --git a/gfx/gl/sceneShader.cpp b/gfx/gl/sceneShader.cpp index dc77793..1b3b27c 100644 --- a/gfx/gl/sceneShader.cpp +++ b/gfx/gl/sceneShader.cpp @@ -47,8 +47,8 @@ void SceneShader::SceneProgram::setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const { glUseProgram(*this); - glUniformMatrix4fv(viewProjectionLoc, 1, GL_FALSE, glm::value_ptr(viewProjection)); - glUniform3iv(viewPointLoc, 1, glm::value_ptr(viewPoint)); + glUniform(viewProjectionLoc, viewProjection); + glUniform(viewPointLoc, viewPoint); } void @@ -56,7 +56,7 @@ SceneShader::SceneProgram::setViewPort(const ViewPort & viewPort) const { if (viewPortLoc >= 0) { glUseProgram(*this); - glUniform4iv(viewPortLoc, 1, glm::value_ptr(viewPort)); + glUniform(viewPortLoc, viewPort); } } @@ -68,8 +68,8 @@ SceneShader::BasicProgram::BasicProgram() : void SceneShader::BasicProgram::setModel(Location const & location) const { - glUniformMatrix3fv(modelLoc, 1, GL_FALSE, glm::value_ptr(location.getRotationTransform())); - glUniform3iv(modelPosLoc, 1, glm::value_ptr(location.pos)); + glUniform(modelLoc, location.getRotationTransform()); + glUniform(modelPosLoc, location.pos); } void @@ -85,5 +85,5 @@ void SceneShader::WaterProgram::use(float waveCycle) const { Program::use(); - glUniform1f(waveLoc, waveCycle); + glUniform(waveLoc, waveCycle); } diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index 5dc555a..ae374fe 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -4,6 +4,7 @@ #include "gfx/gl/shaders/vs-shadowDynamicPoint.h" #include "gfx/gl/shaders/vs-shadowDynamicPointInst.h" #include "gfx/gl/shaders/vs-shadowFixedPoint.h" +#include "gl_traits.h" #include "location.h" #include "maths.h" #include "sceneProvider.h" @@ -174,8 +175,8 @@ void ShadowMapper::FixedPoint::setViewProjection(const GlobalPosition3D viewPoint, const glm::mat4 & viewProjection) const { use(); - glUniformMatrix4fv(viewProjectionLoc, 1, GL_FALSE, glm::value_ptr(viewProjection)); - glUniform3iv(viewPointLoc, 1, glm::value_ptr(viewPoint)); + glUniform(viewProjectionLoc, viewProjection); + glUniform(viewPointLoc, viewPoint); } void @@ -194,8 +195,8 @@ void ShadowMapper::DynamicPoint::setViewProjection(const GlobalPosition3D viewPoint, const glm::mat4 & viewProjection) const { glUseProgram(*this); - glUniformMatrix4fv(viewProjectionLoc, 1, GL_FALSE, glm::value_ptr(viewProjection)); - glUniform3iv(viewPointLoc, 1, glm::value_ptr(viewPoint)); + glUniform(viewProjectionLoc, viewProjection); + glUniform(viewPointLoc, viewPoint); } void @@ -208,6 +209,6 @@ ShadowMapper::DynamicPoint::use(const Location & location) const void ShadowMapper::DynamicPoint::setModel(const Location & location) const { - glUniformMatrix3fv(modelLoc, 1, GL_FALSE, glm::value_ptr(location.getRotationTransform())); - glUniform3iv(modelPosLoc, 1, glm::value_ptr(location.pos)); + glUniform(modelLoc, location.getRotationTransform()); + glUniform(modelPosLoc, location.pos); } diff --git a/gfx/gl/uiShader.cpp b/gfx/gl/uiShader.cpp index dc4f4dc..cf53e2c 100644 --- a/gfx/gl/uiShader.cpp +++ b/gfx/gl/uiShader.cpp @@ -1,4 +1,5 @@ #include "uiShader.h" +#include "gl_traits.h" #include <gfx/gl/program.h> #include <gfx/gl/shader.h> #include <gfx/gl/shaders/fs-uiShader.h> @@ -26,5 +27,5 @@ void UIShader::TextProgram::use(const RGB & colour) const { Program::use(); - glUniform3fv(colorLoc, 1, glm::value_ptr(colour)); + glUniform(colorLoc, colour); } diff --git a/gfx/gl/uiShader.h b/gfx/gl/uiShader.h index 362e90c..99c5e17 100644 --- a/gfx/gl/uiShader.h +++ b/gfx/gl/uiShader.h @@ -1,6 +1,7 @@ #pragma once #include "config/types.h" +#include "gl_traits.h" #include "program.h" #include <cstddef> #include <glad/gl.h> @@ -21,7 +22,7 @@ private: { const RequiredUniformLocation uiProjectionLoc {*this, "uiProjection"}; glUseProgram(*this); - glUniformMatrix4fv(uiProjectionLoc, 1, GL_FALSE, glm::value_ptr(vp)); + glUniform(uiProjectionLoc, vp); } }; diff --git a/lib/gl_traits.h b/lib/gl_traits.h index 934b505..2d3d85d 100644 --- a/lib/gl_traits.h +++ b/lib/gl_traits.h @@ -4,6 +4,8 @@ #include <glad/gl.h> #include <glm/common.hpp> #include <glm/fwd.hpp> +#include <glm/gtc/type_ptr.hpp> +#include <span> template<typename T> struct gl_traits; @@ -37,6 +39,9 @@ struct gl_traits_integer : public gl_traits_base { template<> struct gl_traits<glm::f32> : public gl_traits_float { static constexpr GLenum type {GL_FLOAT}; + static constexpr auto glUniformFunc {&glUniform1f}; + static constexpr std::array glUniformvFunc {&glUniform1fv, &glUniform2fv, &glUniform3fv, &glUniform4fv}; + static constexpr std::array glUniformmFunc {&glUniformMatrix2fv, &glUniformMatrix3fv, &glUniformMatrix4fv}; }; template<> struct gl_traits<glm::f64> : public gl_traits_longfloat { @@ -53,6 +58,8 @@ template<> struct gl_traits<glm::int16> : public gl_traits_integer { template<> struct gl_traits<glm::int32> : public gl_traits_integer { static constexpr GLenum type {GL_INT}; + static constexpr auto glUniformFunc {&glUniform1i}; + static constexpr std::array glUniformvFunc {&glUniform1iv, &glUniform2iv, &glUniform3iv, &glUniform4iv}; }; template<> struct gl_traits<glm::uint8> : public gl_traits_integer { @@ -65,6 +72,9 @@ template<> struct gl_traits<glm::uint16> : public gl_traits_integer { template<> struct gl_traits<glm::uint32> : public gl_traits_integer { static constexpr GLenum type {GL_UNSIGNED_INT}; + static constexpr auto glUniformFunc {&glUniform1ui}; + static constexpr std::array<decltype(&glUniform1uiv), 5> glUniformvFunc { + &glUniform1uiv, &glUniform2uiv, &glUniform3uiv, &glUniform4uiv}; }; template<typename T, std::size_t S> struct gl_traits<std::array<T, S>> : public gl_traits<T> { @@ -87,3 +97,49 @@ struct gl_traits<glm::mat<C, R, T, Q>> : public gl_traits<T> { return R; }}; }; + +template<typename T> +void +glUniform(GLint location, T v) +{ + static_assert( + requires { gl_traits<T>::glUniformFunc; }, "Has glUnform1T"); + (*gl_traits<T>::glUniformFunc)(location, v); +} + +template<glm::length_t L, typename T, glm::qualifier Q> +void +glUniform(GLint location, const glm::vec<L, T, Q> & v) +{ + static_assert( + requires { gl_traits<T>::glUniformFunc; }, "Has glUnformNTv"); + (*gl_traits<T>::glUniformvFunc[L - 1])(location, 1, glm::value_ptr(v)); +} + +template<glm::length_t L, typename T, glm::qualifier Q> +void +glUniform(GLint location, std::span<const glm::vec<L, T, Q>> v) +{ + static_assert( + requires { gl_traits<T>::glUniformvFunc; }, "Has glUnformNTv"); + (*gl_traits<T>::glUniformvFunc[L - 1])(location, static_cast<GLsizei>(v.size()), glm::value_ptr(v.front())); +} + +template<glm::length_t L, typename T, glm::qualifier Q> +void +glUniform(GLint location, const glm::mat<L, L, T, Q> & v) +{ + static_assert( + requires { gl_traits<T>::glUniformmFunc; }, "Has glUnformMatrixNTv"); + (*gl_traits<T>::glUniformmFunc[L - 2])(location, 1, GL_FALSE, glm::value_ptr(v)); +} + +template<glm::length_t L, typename T, glm::qualifier Q> +void +glUniform(GLint location, std::span<const glm::mat<L, L, T, Q>> v) +{ + static_assert( + requires { gl_traits<T>::glUniformmFunc; }, "Has glUnformMatrixNTv"); + (*gl_traits<T>::glUniformmFunc[L - 2])( + location, static_cast<GLsizei>(v.size()), GL_FALSE, glm::value_ptr(v.front())); +} |