Skip to content

Commit

Permalink
started to add return flag to the set uniform methods to make checkin…
Browse files Browse the repository at this point in the history
…g easier. Also added tests for array uniform types as last errors didn't spot this when using the light demo and edit shaders
  • Loading branch information
jmacey committed May 22, 2023
1 parent 2aafcc8 commit 596f7a8
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 63 deletions.
22 changes: 11 additions & 11 deletions include/ngl/ShaderLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,18 @@ class NGL_DLLEXPORT ShaderLib
/// @param[in] _paramName the name of the Uniform to set
/// @param[in] _v0 the float value of the parameter to set
//----------------------------------------------------------------------------------------------------------------------
static void setUniform(std::string_view _paramName, Real _v0) noexcept;
static void getUniform(std::string_view _paramName, Real &o_v0) noexcept;
static bool setUniform(std::string_view _paramName, Real _v0) noexcept;
static bool getUniform(std::string_view _paramName, Real &o_v0) noexcept;
//----------------------------------------------------------------------------------------------------------------------
/// @brief overloaded method to set shader Uniforms the shader
/// must be the currently active shader of else this will fail
/// @param[in] _paramName the name of the Uniform to set
/// @param[in] _v0 the float value of the parameter to set
/// @param[in] _v1 the float value of the parameter to set
//----------------------------------------------------------------------------------------------------------------------
static void setUniform(std::string_view _paramName, Real _v0, Real _v1) noexcept;
static void getUniform(std::string_view _paramName, Real &o_v0, Real &o_v1) noexcept;
static void getUniform(std::string_view _paramName, Vec2 &o_v3) noexcept;
static bool setUniform(std::string_view _paramName, Real _v0, Real _v1) noexcept;
static bool getUniform(std::string_view _paramName, Real &o_v0, Real &o_v1) noexcept;
static bool getUniform(std::string_view _paramName, Vec2 &o_v3) noexcept;
//----------------------------------------------------------------------------------------------------------------------
/// @brief overloaded method to set shader Uniforms the shader
/// must be the currently active shader of else this will fail
Expand All @@ -263,9 +263,9 @@ class NGL_DLLEXPORT ShaderLib
/// @param[in] _v1 the float value of the parameter to set
/// @param[in] _v2 the float value of the parameter to set
//----------------------------------------------------------------------------------------------------------------------
static void setUniform(std::string_view _paramName, Real _v0, Real _v1, Real _v2) noexcept;
static void getUniform(std::string_view _paramName, Real &o_v0, Real &o_v1, Real &o_v2) noexcept;
static void getUniform(std::string_view _paramName, Vec3 &o_v) noexcept;
static bool setUniform(std::string_view _paramName, Real _v0, Real _v1, Real _v2) noexcept;
static bool getUniform(std::string_view _paramName, Real &o_v0, Real &o_v1, Real &o_v2) noexcept;
static bool getUniform(std::string_view _paramName, Vec3 &o_v) noexcept;
//----------------------------------------------------------------------------------------------------------------------
/// @brief overloaded method to set shader Uniforms the shader
/// must be the currently active shader of else this will fail
Expand All @@ -275,9 +275,9 @@ class NGL_DLLEXPORT ShaderLib
/// @param[in] _v2 the float value of the parameter to set
/// @param[in] _v3 the float value of the parameter to set
//----------------------------------------------------------------------------------------------------------------------
static void setUniform(std::string_view _paramName, Real _v0, Real _v1, Real _v2, Real _v3) noexcept;
static void getUniform(std::string_view _paramName, Real &o_v0, Real &o_v1, Real &o_v2, Real &o_v3) noexcept;
static void getUniform(std::string_view _paramName, Vec4 &o_v) noexcept;
static bool setUniform(std::string_view _paramName, Real _v0, Real _v1, Real _v2, Real _v3) noexcept;
static bool getUniform(std::string_view _paramName, Real &o_v0, Real &o_v1, Real &o_v2, Real &o_v3) noexcept;
static bool getUniform(std::string_view _paramName, Vec4 &o_v) noexcept;
//----------------------------------------------------------------------------------------------------------------------
/// @brief overloaded method to set shader Uniforms the shader
/// must be the currently active shader of else this will fail
Expand Down
16 changes: 8 additions & 8 deletions include/ngl/ShaderProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,17 @@ class NGL_DLLEXPORT ShaderProgram
/// @param _varname - name of the uniform variable
/// @param _v0 - new value for the variable
//----------------------------------------------------------------------------------------------------------------------
void setRegisteredUniform1f(std::string_view _varname, float _v0) const noexcept;
void getRegisteredUniform1f(std::string_view _varname, float &o_v0) const noexcept;
bool setRegisteredUniform1f(std::string_view _varname, float _v0) const noexcept;
bool getRegisteredUniform1f(std::string_view _varname, float &o_v0) const noexcept;

//----------------------------------------------------------------------------------------------------------------------
/// @brief sets the registered uniform to a single float
/// @param _varname - name of the uniform variable
/// @param _v0 - new value for the variable
/// @param _v1 - new value for the variable
//----------------------------------------------------------------------------------------------------------------------
void setRegisteredUniform2f(std::string_view _varname, float _v0, float _v1) const noexcept;
void getRegisteredUniform2f(std::string_view _varname, float &o_v0, float &o_v1) const noexcept;
bool setRegisteredUniform2f(std::string_view _varname, float _v0, float _v1) const noexcept;
bool getRegisteredUniform2f(std::string_view _varname, float &o_v0, float &o_v1) const noexcept;

//----------------------------------------------------------------------------------------------------------------------
/// @brief sets the registered uniform to a single float
Expand All @@ -144,8 +144,8 @@ class NGL_DLLEXPORT ShaderProgram
/// @param _v1 - new value for the variable
/// @param _v2 - new value for the variable
//----------------------------------------------------------------------------------------------------------------------
void setRegisteredUniform3f(std::string_view _varname, float _v0, float _v1, float _v2) const noexcept;
void getRegisteredUniform3f(std::string_view _varname, float &_v0, float &_v1, float &_v2) const noexcept;
bool setRegisteredUniform3f(std::string_view _varname, float _v0, float _v1, float _v2) const noexcept;
bool getRegisteredUniform3f(std::string_view _varname, float &_v0, float &_v1, float &_v2) const noexcept;
//----------------------------------------------------------------------------------------------------------------------
/// @brief sets the registered uniform to a single float
/// @param _varname - name of the uniform variable
Expand All @@ -154,8 +154,8 @@ class NGL_DLLEXPORT ShaderProgram
/// @param _v2 - new value for the variable
/// @param _v3 - new value for the variable
/////----------------------------------------------------------------------------------------------------------------------
void setRegisteredUniform4f(std::string_view _varname, float _v0, float _v1, float _v2, float _v3) const noexcept;
void getRegisteredUniform4f(std::string_view _varname, float &_v0, float &_v1, float &_v2, float &o_v3) const noexcept;
bool setRegisteredUniform4f(std::string_view _varname, float _v0, float _v1, float _v2, float _v3) const noexcept;
bool getRegisteredUniform4f(std::string_view _varname, float &_v0, float &_v1, float &_v2, float &o_v3) const noexcept;
//----------------------------------------------------------------------------------------------------------------------
/// @brief sets the registered uniform to a single int
/// @param _varname - name of the uniform variable
Expand Down
59 changes: 34 additions & 25 deletions src/ShaderLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -818,65 +818,74 @@ void ShaderLib::printProperties() noexcept
}
}

void ShaderLib::setUniform(std::string_view _paramName, Real _v0) noexcept
bool ShaderLib::setUniform(std::string_view _paramName, Real _v0) noexcept
{
m_shaderPrograms[m_currentShader]->setRegisteredUniform1f(_paramName.data(), _v0);
return m_shaderPrograms[m_currentShader]->setRegisteredUniform1f(_paramName.data(), _v0);
}

void ShaderLib::getUniform(std::string_view _paramName, Real &o_v0) noexcept
bool ShaderLib::getUniform(std::string_view _paramName, Real &o_v0) noexcept
{
m_shaderPrograms[m_currentShader]->getRegisteredUniform1f(_paramName.data(), o_v0);
return m_shaderPrograms[m_currentShader]->getRegisteredUniform1f(_paramName.data(), o_v0);
}

void ShaderLib::getUniform(std::string_view _paramName, Real &o_v0, Real &o_v1) noexcept
bool ShaderLib::getUniform(std::string_view _paramName, Real &o_v0, Real &o_v1) noexcept
{
m_shaderPrograms[m_currentShader]->getRegisteredUniform2f(_paramName.data(), o_v0, o_v1);
return m_shaderPrograms[m_currentShader]->getRegisteredUniform2f(_paramName.data(), o_v0, o_v1);
}

void ShaderLib::getUniform(std::string_view _paramName, ngl::Vec2 &o_v) noexcept
bool ShaderLib::getUniform(std::string_view _paramName, ngl::Vec2 &o_v) noexcept
{
float x, y;
m_shaderPrograms[m_currentShader]->getRegisteredUniform2f(_paramName.data(), x, y);
float x=0.0f;
float y=0.0f;
auto ret= m_shaderPrograms[m_currentShader]->getRegisteredUniform2f(_paramName.data(), x, y);
o_v.set(x, y);
return ret;
}

void ShaderLib::getUniform(std::string_view _paramName, Real &o_v0, Real &o_v1, Real &o_v2) noexcept
bool ShaderLib::getUniform(std::string_view _paramName, Real &o_v0, Real &o_v1, Real &o_v2) noexcept
{
m_shaderPrograms[m_currentShader]->getRegisteredUniform3f(_paramName.data(), o_v0, o_v1, o_v2);
return m_shaderPrograms[m_currentShader]->getRegisteredUniform3f(_paramName.data(), o_v0, o_v1, o_v2);
}

void ShaderLib::getUniform(std::string_view _paramName, ngl::Vec3 &o_v) noexcept
bool ShaderLib::getUniform(std::string_view _paramName, ngl::Vec3 &o_v) noexcept
{
float x, y, z;
m_shaderPrograms[m_currentShader]->getRegisteredUniform3f(_paramName.data(), x, y, z);
float x=0.0f;
float y=0.0f;
float z=0.0f;
auto ret=m_shaderPrograms[m_currentShader]->getRegisteredUniform3f(_paramName.data(), x, y, z);
o_v.set(x, y, z);
return ret;
}

void ShaderLib::getUniform(std::string_view _paramName, Real &o_v0, Real &o_v1, Real &o_v2, Real &o_v3) noexcept
bool ShaderLib::getUniform(std::string_view _paramName, Real &o_v0, Real &o_v1, Real &o_v2, Real &o_v3) noexcept
{
m_shaderPrograms[m_currentShader]->getRegisteredUniform4f(_paramName.data(), o_v0, o_v1, o_v2, o_v3);
return m_shaderPrograms[m_currentShader]->getRegisteredUniform4f(_paramName.data(), o_v0, o_v1, o_v2, o_v3);
}

void ShaderLib::getUniform(std::string_view _paramName, ngl::Vec4 &o_v) noexcept
bool ShaderLib::getUniform(std::string_view _paramName, ngl::Vec4 &o_v) noexcept
{
float x, y, z, w;
m_shaderPrograms[m_currentShader]->getRegisteredUniform4f(_paramName.data(), x, y, z, w);
float x=0.0f;
float y=0.0f;
float z=0.0f;
float w=0.0f;
auto ret=m_shaderPrograms[m_currentShader]->getRegisteredUniform4f(_paramName.data(), x, y, z, w);
o_v.set(x, y, z, w);
return ret;
}

void ShaderLib::setUniform(std::string_view _paramName, Real _v0, Real _v1) noexcept
bool ShaderLib::setUniform(std::string_view _paramName, Real _v0, Real _v1) noexcept
{
m_shaderPrograms[m_currentShader]->setRegisteredUniform2f(_paramName.data(), _v0, _v1);
return m_shaderPrograms[m_currentShader]->setRegisteredUniform2f(_paramName.data(), _v0, _v1);
}

void ShaderLib::setUniform(std::string_view _paramName, Real _v0, Real _v1, Real _v2) noexcept
bool ShaderLib::setUniform(std::string_view _paramName, Real _v0, Real _v1, Real _v2) noexcept
{
m_shaderPrograms[m_currentShader]->setRegisteredUniform3f(_paramName.data(), _v0, _v1, _v2);
return m_shaderPrograms[m_currentShader]->setRegisteredUniform3f(_paramName.data(), _v0, _v1, _v2);
}

void ShaderLib::setUniform(std::string_view _paramName, Real _v0, Real _v1, Real _v2, Real _v3) noexcept
bool ShaderLib::setUniform(std::string_view _paramName, Real _v0, Real _v1, Real _v2, Real _v3) noexcept
{
m_shaderPrograms[m_currentShader]->setRegisteredUniform4f(_paramName.data(), _v0, _v1, _v2, _v3);
return m_shaderPrograms[m_currentShader]->setRegisteredUniform4f(_paramName.data(), _v0, _v1, _v2, _v3);
}

void ShaderLib::setUniform(std::string_view _paramName, GLint _v0) noexcept
Expand Down
45 changes: 37 additions & 8 deletions src/ShaderProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,50 +249,56 @@ void ShaderProgram::printActiveAttributes() const noexcept
}

//----------------------------------------------------------------------------------------------------------------------
void ShaderProgram::setRegisteredUniform1f(std::string_view _varname, float _v0) const noexcept
bool ShaderProgram::setRegisteredUniform1f(std::string_view _varname, float _v0) const noexcept
{
auto uniform = m_registeredUniforms.find(_varname.data());
// make sure we have a valid shader
if(uniform != m_registeredUniforms.end())
{
glUniform1f(uniform->second.loc, _v0);
return true;
}
else
{
ngl::NGLMessage::addWarning(fmt::format("Uniform {0} Not found in Shader {1}", _varname, m_programName));
return false;
}
}

void ShaderProgram::getRegisteredUniform1f(std::string_view _varname, float &o_v0) const noexcept
bool ShaderProgram::getRegisteredUniform1f(std::string_view _varname, float &o_v0) const noexcept
{
auto uniform = m_registeredUniforms.find(_varname.data());
// make sure we have a valid shader
if(uniform != m_registeredUniforms.end())
{
glGetUniformfv(m_programID, uniform->second.loc, &o_v0);
return true;
}
else
{
ngl::NGLMessage::addWarning(fmt::format("Uniform {0} Not found in Shader {1}", _varname, m_programName));
return false;
}
}

//----------------------------------------------------------------------------------------------------------------------
void ShaderProgram::setRegisteredUniform2f(std::string_view _varname, float _v0, float _v1) const noexcept
bool ShaderProgram::setRegisteredUniform2f(std::string_view _varname, float _v0, float _v1) const noexcept
{
auto uniform = m_registeredUniforms.find(_varname.data());
// make sure we have a valid shader
if(uniform != m_registeredUniforms.end())
{
glUniform2f(uniform->second.loc, _v0, _v1);
return true;
}
else
{
ngl::NGLMessage::addWarning(fmt::format("Uniform {0} Not found in Shader {1}", _varname, m_programName));
return false;
}
}

void ShaderProgram::getRegisteredUniform2f(std::string_view _varname, float &o_v0, float &o_v1) const noexcept
bool ShaderProgram::getRegisteredUniform2f(std::string_view _varname, float &o_v0, float &o_v1) const noexcept
{
auto uniform = m_registeredUniforms.find(_varname.data());
float data[2];
Expand All @@ -302,25 +308,33 @@ void ShaderProgram::getRegisteredUniform2f(std::string_view _varname, float &o_v
glGetUniformfv(m_programID, uniform->second.loc, &data[0]);
o_v0 = data[0];
o_v1 = data[1];
return true;
}
else
{
ngl::NGLMessage::addWarning(fmt::format("Uniform {0} Not found in Shader {1}", _varname, m_programName));
return false;
}
}

//----------------------------------------------------------------------------------------------------------------------
void ShaderProgram::setRegisteredUniform3f(std::string_view _varname, float _v0, float _v1, float _v2) const noexcept
bool ShaderProgram::setRegisteredUniform3f(std::string_view _varname, float _v0, float _v1, float _v2) const noexcept
{
auto uniform = m_registeredUniforms.find(_varname.data());
// make sure we have a valid shader
if(uniform != m_registeredUniforms.end())
{
glUniform3f(uniform->second.loc, _v0, _v1, _v2);
return true;
}
else
{
ngl::NGLMessage::addWarning(fmt::format("Uniform {0} Not found in Shader {1}", _varname, m_programName));
return false;
}
}

void ShaderProgram::getRegisteredUniform3f(std::string_view _varname, float &o_v0, float &o_v1, float &o_v2) const noexcept
bool ShaderProgram::getRegisteredUniform3f(std::string_view _varname, float &o_v0, float &o_v1, float &o_v2) const noexcept
{
auto uniform = m_registeredUniforms.find(_varname.data());
float data[3];
Expand All @@ -331,25 +345,33 @@ void ShaderProgram::getRegisteredUniform3f(std::string_view _varname, float &o_v
o_v0 = data[0];
o_v1 = data[1];
o_v2 = data[2];
return true;
}
else
{
ngl::NGLMessage::addWarning(fmt::format("Uniform {0} Not found in Shader {1}", _varname, m_programName));
return false;
}
}

//----------------------------------------------------------------------------------------------------------------------
void ShaderProgram::setRegisteredUniform4f(std::string_view _varname, float _v0, float _v1, float _v2, float _v3) const noexcept
bool ShaderProgram::setRegisteredUniform4f(std::string_view _varname, float _v0, float _v1, float _v2, float _v3) const noexcept
{
auto uniform = m_registeredUniforms.find(_varname.data());
// make sure we have a valid shader
if(uniform != m_registeredUniforms.end())
{
glUniform4f(uniform->second.loc, _v0, _v1, _v2, _v3);
return true;
}
else
{
ngl::NGLMessage::addWarning(fmt::format("Uniform {0} Not found in Shader {1}", _varname, m_programName));
return false;
}
}

void ShaderProgram::getRegisteredUniform4f(std::string_view _varname, float &o_v0, float &o_v1, float &o_v2, float &o_v3) const noexcept
bool ShaderProgram::getRegisteredUniform4f(std::string_view _varname, float &o_v0, float &o_v1, float &o_v2, float &o_v3) const noexcept
{
auto uniform = m_registeredUniforms.find(_varname.data());
float data[4];
Expand All @@ -361,7 +383,14 @@ void ShaderProgram::getRegisteredUniform4f(std::string_view _varname, float &o_v
o_v1 = data[1];
o_v2 = data[2];
o_v3 = data[3];
return true;
}
else
{
ngl::NGLMessage::addWarning(fmt::format("Uniform {0} Not found in Shader {1}", _varname, m_programName));
return false;
}

}

//----------------------------------------------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 596f7a8

Please sign in to comment.