Skip to content

Commit

Permalink
working shader lib with templates, need some tidy ups and comments, a…
Browse files Browse the repository at this point in the history
…ll tests are passing.
  • Loading branch information
jmacey committed Jun 2, 2023
1 parent b49085d commit 3d4d464
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 366 deletions.
105 changes: 4 additions & 101 deletions include/ngl/ShaderLib.inl
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,12 @@ bool ShaderLib::getUniform(std::string_view _paramName, Ts &&o_arg) noexcept
{
return m_shaderPrograms[m_currentShader]->getRegisteredUniform(_paramName.data(), o_arg);
}
else if constexpr (std::is_same<Ts,ngl::Vec2 &>::value ) //|| std::is_same<Ts,ngl::Vec3 &>::value || std::is_same<Ts,ngl::Vec4 &>::value)
else if constexpr (is_ngl_vec<Ts>::value)
{
std::array<float,2> data;
std::vector<float> data(o_arg.m_openGL.size());
auto ret = m_shaderPrograms[m_currentShader]->getRegisteredUniform(_paramName.data(), data);
o_arg.m_openGL = data;

return ret;
}
else if constexpr (std::is_same<Ts,ngl::Vec3 &>::value ) //|| std::is_same<Ts,ngl::Vec3 &>::value || std::is_same<Ts,ngl::Vec4 &>::value)
{
std::array<float,3> data;
auto ret = m_shaderPrograms[m_currentShader]->getRegisteredUniform(_paramName.data(), data);
o_arg.m_openGL = data;

return ret;
}
else if constexpr (std::is_same<Ts,ngl::Vec4 &>::value ) //|| std::is_same<Ts,ngl::Vec3 &>::value || std::is_same<Ts,ngl::Vec4 &>::value)
{
std::array<float,4 > data;
auto ret = m_shaderPrograms[m_currentShader]->getRegisteredUniform(_paramName.data(), data);
o_arg.m_openGL = data;

//o_arg.m_openGL = data;
std::copy_n(std::begin(data), data.size(), std::begin(o_arg.m_openGL));
return ret;
}

Expand All @@ -56,92 +40,11 @@ bool ShaderLib::getUniform(std::string_view _paramName, Ts &&o_arg) noexcept
template <typename... Ts>
bool ShaderLib::getUniform(std::string_view _paramName, Ts &&...o_args) noexcept
{
//auto values = std::forward_as_tuple(std::forward<Ts>(o_args)...);
std::array<std::common_type_t<Ts...>, sizeof...(Ts)> data;
auto ret = m_shaderPrograms[m_currentShader]->getRegisteredUniform(_paramName.data(), data);
std::size_t i = 0;
// fold expression to copy data to o_args
((o_args = data[i++]), ...);
// for(auto &a : data)
// {
// std::cout<<"data "<<a<<"\n";
// }
// print_args(o_args...);
return ret;
}

/*
template<typename ... Ts>
bool ShaderLib::setUniform(std::string_view _varname ,Ts &&...args) noexcept
{
// uses variadic templates to forward a variable number of arguments to a function.
// The function std::forward_as_tuple takes the arguments and returns a tuple of references to the arguments.
// The code then extracts the first argument from the tuple using std::get<0>(values) and assigns it to a variable v.
// Finally, the code uses a static assertion to check that all the arguments passed to the function are of the same type Ts.
// If the assertion fails, it will print an error message.
// extract the arugments and get first type
auto values = std::forward_as_tuple(args...);
auto v=std::get<0>(values);
// now ensure we have all the same type (this is a compile time check)
static_assert(std::conjunction_v<std::is_same<decltype(v), Ts>...> ,
"setUniform must have values of all the same types");
static_assert(sizeof...(args) <= 4, "setUniform can only handle upto 4 values");
// now we can just expand based on the size of args
// we only have 4 types so it's simple.
if constexpr (sizeof...(args) == 2)
{
auto values =std::forward_as_tuple(args...);
m_shaderPrograms[m_currentShader]->setRegisteredUniform(_varname,static_cast<decltype(v)>(std::get<0>(values)),
static_cast<decltype(v)>(std::get<1>(values)));
return true;
}
else if constexpr (sizeof...(args) == 3)
{
auto values =std::forward_as_tuple(args...);
m_shaderPrograms[m_currentShader]->setRegisteredUniform(_varname,static_cast<decltype(v)>(std::get<0>(values)),
static_cast<decltype(v)>(std::get<1>(values)),
static_cast<decltype(v)>(std::get<2>(values)));
return true;
}
else if constexpr (sizeof...(args) == 4)
{
auto values =std::forward_as_tuple(args...);
m_shaderPrograms[m_currentShader]->setRegisteredUniform(_varname,static_cast<decltype(v)>(std::get<0>(values)),
static_cast<decltype(v)>(std::get<1>(values)),
static_cast<decltype(v)>(std::get<2>(values)),
static_cast<decltype(v)>(std::get<3>(values)));
return true;
}
return false;
}
template< typename Ts>
bool ShaderLib::getUniform(std::string_view _paramName, Ts &&o_arg) noexcept
{
return m_shaderPrograms[m_currentShader]->getRegisteredUniform(_paramName.data(), o_arg);
}
template< typename ... Ts>
bool ShaderLib::getUniform(std::string_view _paramName, Ts &&...o_args) noexcept
{
auto values = std::forward_as_tuple(o_args...);
auto v=std::get<0>(values);
std::array<decltype(v),sizeof...(o_args)> data;
auto ret=m_shaderPrograms[m_currentShader]->getRegisteredUniform(_paramName.data(),data);
size_t i=0;
std::apply([data,&i](auto&&... args) {((args=data[i++] ), ...);}, values);
// for(size_t i=0; i<sizeof...(o_args); ++i)
// {
// std::get<i>(values)=data[i];
// }
return ret;
}
*/
6 changes: 5 additions & 1 deletion include/ngl/ShaderProgram.inl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ bool ShaderProgram::setRegisteredUniform(std::string_view _varname ,Ts &&arg ) c
glUniformMatrix4fv(uniform->second.loc, 1, GL_FALSE, &arg.m_openGL[0]);
return true;
} // end of mat4
#ifdef USE_GLM
else if constexpr(std::is_same<Ts,glm::mat2&>::value)
{
glUniformMatrix2fv(uniform->second.loc, 1, GL_FALSE, &arg[0][0]);
Expand All @@ -48,6 +49,7 @@ bool ShaderProgram::setRegisteredUniform(std::string_view _varname ,Ts &&arg ) c
glUniformMatrix4fv(uniform->second.loc, 1, GL_FALSE, &arg[0][0]);
return true;
} // end of glmmat2
#endif
else if constexpr(std::is_same<Ts,Vec2&>::value)
{
glUniform2f(uniform->second.loc,arg.m_x,arg.m_y);
Expand All @@ -63,6 +65,7 @@ bool ShaderProgram::setRegisteredUniform(std::string_view _varname ,Ts &&arg ) c
glUniform4f(uniform->second.loc,arg.m_x,arg.m_y,arg.m_z,arg.m_w);
return true;
}
#ifdef USE_GLM
else if constexpr(std::is_same<Ts,glm::vec2&>::value)
{
glUniform2f(uniform->second.loc,arg.x,arg.y);
Expand All @@ -78,6 +81,7 @@ bool ShaderProgram::setRegisteredUniform(std::string_view _varname ,Ts &&arg ) c
glUniform4f(uniform->second.loc,arg.x,arg.y,arg.z,arg.w);
return true;
}
#endif
// handle arrays as matrix
else if constexpr(is_std_array<std::decay_t<Ts>>::value ||
is_std_vector<std::decay_t<Ts>>::value ||
Expand Down Expand Up @@ -205,7 +209,7 @@ bool ShaderProgram::getRegisteredUniform(std::string_view _varname ,Ts &o_arg )
glGetUniformiv(m_programID, uniform->second.loc, &o_arg);
return true;
} // end of float
else if constexpr(is_std_array<Ts>::value)
else if constexpr(is_std_array<Ts>::value || is_std_vector<Ts>::value)
{
if constexpr (std::is_same<array_value_type<Ts>,float>::value )
{
Expand Down
15 changes: 15 additions & 0 deletions include/ngl/TemplateHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ template<typename T, std::size_t N>
struct is_std_array<std::array<T,N>> : std::true_type {};

// traits for matrix types
// see if forwarding this helps
template<typename T>
struct is_ngl_matrix : std::false_type {};
template<>
Expand All @@ -23,14 +24,28 @@ template<>
struct is_ngl_matrix<ngl::Mat3> : std::true_type {};
template<>
struct is_ngl_matrix<ngl::Mat4> : std::true_type {};
template<>
struct is_ngl_matrix<ngl::Mat2 &> : std::true_type {};
template<>
struct is_ngl_matrix<ngl::Mat3 &> : std::true_type {};
template<>
struct is_ngl_matrix<ngl::Mat4 &> : std::true_type {};

template<typename T>
// vec traits
struct is_ngl_vec : std::false_type {};
template<>
struct is_ngl_vec<ngl::Vec2> : std::true_type {};
template<>
struct is_ngl_vec<ngl::Vec3> : std::true_type {};
template<>
struct is_ngl_vec<ngl::Vec4> : std::true_type {};
template<>
struct is_ngl_vec<ngl::Vec2 &> : std::true_type {};
template<>
struct is_ngl_vec<ngl::Vec3 &> : std::true_type {};
template<>
struct is_ngl_vec<ngl::Vec4 &> : std::true_type {};

// get the value type of the array
template <typename T>
Expand Down
Loading

0 comments on commit 3d4d464

Please sign in to comment.