#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 { template static GLuint vertexAttribFormatFunc(GLuint vao, GLuint index, GLuint offset) { glVertexArrayAttribFormat(vao, index, size, type, GL_FALSE, offset); return 1; } }; struct gl_traits_longfloat : public gl_traits_base { template static GLuint vertexAttribFormatFunc(GLuint vao, GLuint index, GLuint offset) { glVertexArrayAttribLFormat(vao, index, size, type, offset); return 1; } }; struct gl_traits_integer : public gl_traits_base { template static GLuint vertexAttribFormatFunc(GLuint vao, GLuint index, GLuint offset) { glVertexArrayAttribIFormat(vao, index, size, type, offset); 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}; static constexpr auto glTextureParameterFunc {&glTextureParameterf}; static constexpr auto glTextureParametervFunc {&glTextureParameterfv}; static constexpr auto vertexArrayAttribFormat {&vertexAttribFormatFunc}; }; template<> struct gl_traits : public gl_traits_longfloat { static constexpr GLenum type {GL_DOUBLE}; static constexpr auto vertexArrayAttribFormat {&vertexAttribFormatFunc}; }; template<> struct gl_traits : public gl_traits_integer { static constexpr GLenum type {GL_BYTE}; static constexpr auto vertexArrayAttribFormat {&vertexAttribFormatFunc}; }; template<> struct gl_traits : public gl_traits_integer { static constexpr GLenum type {GL_SHORT}; static constexpr auto vertexArrayAttribFormat {&vertexAttribFormatFunc}; }; 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}; static constexpr auto glTextureParameterFunc {&glTextureParameteri}; static constexpr auto glTextureParametervFunc {&glTextureParameteriv}; static constexpr auto vertexArrayAttribFormat {&vertexAttribFormatFunc}; }; template<> struct gl_traits : public gl_traits_integer { static constexpr GLenum type {GL_UNSIGNED_BYTE}; static constexpr auto vertexArrayAttribFormat {&vertexAttribFormatFunc}; }; template<> struct gl_traits : public gl_traits_integer { static constexpr GLenum type {GL_UNSIGNED_SHORT}; static constexpr auto vertexArrayAttribFormat {&vertexAttribFormatFunc}; }; 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}; static constexpr auto vertexArrayAttribFormat {&vertexAttribFormatFunc}; }; template struct gl_traits> : public gl_traits { static constexpr GLint size {S * gl_traits::size}; static constexpr auto vertexArrayAttribFormat {[](GLuint vao, GLuint index, GLuint offset) { if constexpr (std::is_pod_v) { return gl_traits::template vertexAttribFormatFunc::type, S>(vao, index, offset); } else { GLuint used = 0; for (GLuint e = 0; e < S; e++) { used += gl_traits::template vertexAttribFormatFunc::type, 1>( vao, index + e, offset + (e * sizeof(T))); } return used; } }}; }; template struct gl_traits> : public gl_traits { static constexpr GLint size {L}; static constexpr auto vertexArrayAttribFormat {[](GLuint vao, GLuint index, GLuint offset) { return gl_traits::template vertexAttribFormatFunc::type, L>(vao, index, offset); }}; }; template struct gl_traits> : public gl_traits { static constexpr GLint size {C * R}; static constexpr auto vertexArrayAttribFormat {[](GLuint vao, GLuint index, GLuint offset) { GLuint used = 0; for (GLuint row = 0; row < R; row++) { used += gl_traits::template vertexAttribFormatFunc::type, C>( vao, index + row, offset + (C * row * static_cast(sizeof(T)))); } return used; }}; }; 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 concept has_glTextureParameter = requires { gl_traits::glTextureParameterFunc; }; template concept has_glTextureParameterv = requires { gl_traits::glTextureParametervFunc; }; 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())); }