#pragma once #include #include #include #include #include #include template struct gl_traits; struct gl_traits_base { static constexpr GLint size {1}; }; struct gl_traits_float : public gl_traits_base { static constexpr auto vertexAttribFunc { [](GLuint index, GLint size, GLenum type, GLsizei stride, const void * pointer) -> GLuint { glVertexAttribPointer(index, size, type, GL_FALSE, stride, pointer); return 1; }}; }; struct gl_traits_longfloat : public gl_traits_base { static constexpr auto vertexAttribFunc { [](GLuint index, GLint size, GLenum type, GLsizei stride, const void * pointer) -> GLuint { glVertexAttribLPointer(index, size, type, stride, pointer); return 1; }}; }; struct gl_traits_integer : public gl_traits_base { static constexpr auto vertexAttribFunc { [](GLuint index, GLint size, GLenum type, GLsizei stride, const void * pointer) -> GLuint { glVertexAttribIPointer(index, size, type, stride, pointer); return 1; }}; }; template<> struct gl_traits : 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 : public gl_traits_longfloat { static constexpr GLenum type {GL_DOUBLE}; }; template<> struct gl_traits : public gl_traits_integer { static constexpr GLenum type {GL_BYTE}; }; template<> struct gl_traits : public gl_traits_integer { static constexpr GLenum type {GL_SHORT}; }; template<> struct gl_traits : 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 : public gl_traits_integer { static constexpr GLenum type {GL_UNSIGNED_BYTE}; }; template<> struct gl_traits : public gl_traits_integer { static constexpr GLenum type {GL_UNSIGNED_SHORT}; }; template<> struct gl_traits : public gl_traits_integer { static constexpr GLenum type {GL_UNSIGNED_INT}; static constexpr auto glUniformFunc {&glUniform1ui}; static constexpr std::array glUniformvFunc { &glUniform1uiv, &glUniform2uiv, &glUniform3uiv, &glUniform4uiv}; }; template struct gl_traits> : public gl_traits { static constexpr GLint size {S * gl_traits::size}; }; template struct gl_traits> : public gl_traits { static constexpr GLint size {L}; }; template struct gl_traits> : public gl_traits { static constexpr GLint size {C * R}; static constexpr auto vertexAttribFunc { [](GLuint index, GLint, GLenum type, GLsizei stride, const void * pointer) -> GLuint { const auto base = static_cast(pointer); for (GLuint r = 0; r < R; r++) { glVertexAttribPointer(index + r, C, type, GL_FALSE, stride, base + (r * C)); } return R; }}; }; template concept has_glUniform1 = requires { gl_traits::glUniformFunc; }; template concept has_glUniformNv = requires { gl_traits::glUniformvFunc; }; template concept has_glUniformMatrixNv = requires { gl_traits::glUniformmFunc; }; template void glUniform(GLint location, T v) { (*gl_traits::glUniformFunc)(location, v); } template void glUniform(GLint location, const glm::vec & v) { (*gl_traits::glUniformvFunc[L - 1])(location, 1, glm::value_ptr(v)); } template void glUniform(GLint location, std::span> v) { (*gl_traits::glUniformvFunc[L - 1])(location, static_cast(v.size()), glm::value_ptr(v.front())); } template void glUniform(GLint location, std::span v) { (*gl_traits::glUniformvFunc.front())(location, static_cast(v.size()), v.data()); } template void glUniform(GLint location, const glm::mat & v) { (*gl_traits::glUniformmFunc[L - 2])(location, 1, GL_FALSE, glm::value_ptr(v)); } template void glUniform(GLint location, std::span> v) { (*gl_traits::glUniformmFunc[L - 2])( location, static_cast(v.size()), GL_FALSE, glm::value_ptr(v.front())); }