From 47c0dd2b46fce3a5535ee457549ebd5bcea91c61 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 6 Mar 2026 13:48:04 +0000 Subject: Add vertexAttribFormatFunc to gl_traits for DSA vertex configuration --- lib/gl_traits.h | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'lib/gl_traits.h') diff --git a/lib/gl_traits.h b/lib/gl_traits.h index b3c6909..97183f0 100644 --- a/lib/gl_traits.h +++ b/lib/gl_traits.h @@ -19,6 +19,14 @@ struct gl_traits_float : public gl_traits_base { glVertexAttribPointer(index, size, type, GL_FALSE, stride, pointer); return 1; }}; + + 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 { @@ -27,6 +35,14 @@ struct gl_traits_longfloat : public gl_traits_base { glVertexAttribLPointer(index, size, type, stride, pointer); return 1; }}; + + 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 { @@ -35,6 +51,14 @@ struct gl_traits_integer : public gl_traits_base { glVertexAttribIPointer(index, size, type, stride, pointer); return 1; }}; + + 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 { @@ -44,18 +68,22 @@ template<> struct gl_traits : public gl_traits_float { static constexpr std::array glUniformmFunc {&glUniformMatrix2fv, &glUniformMatrix3fv, &glUniformMatrix4fv}; static constexpr auto glTexParameterFunc {&glTexParameterf}; static constexpr auto glTexParameterfFunc {&glTexParameterfv}; + 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 { @@ -64,14 +92,17 @@ template<> struct gl_traits : public gl_traits_integer { static constexpr std::array glUniformvFunc {&glUniform1iv, &glUniform2iv, &glUniform3iv, &glUniform4iv}; static constexpr auto glTexParameterFunc {&glTexParameteri}; static constexpr auto glTexParameterfFunc {&glTexParameteriv}; + 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 { @@ -79,6 +110,7 @@ template<> struct gl_traits : public gl_traits_integer { 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 { @@ -91,10 +123,26 @@ template struct gl_traits> : public } return S; }}; + 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 @@ -108,6 +156,14 @@ struct gl_traits> : public gl_traits { } return 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 -- cgit v1.3 From 1db3aead80d9209175aa5547363ad34b881a3660 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 6 Mar 2026 14:26:59 +0000 Subject: Remove VertexArrayObject and supporting non-DSA gl_traits helpers --- gfx/gl/vertexArrayObject.h | 125 --------------------------------------------- lib/gl_traits.h | 34 ------------ 2 files changed, 159 deletions(-) delete mode 100644 gfx/gl/vertexArrayObject.h (limited to 'lib/gl_traits.h') diff --git a/gfx/gl/vertexArrayObject.h b/gfx/gl/vertexArrayObject.h deleted file mode 100644 index d008897..0000000 --- a/gfx/gl/vertexArrayObject.h +++ /dev/null @@ -1,125 +0,0 @@ -#pragma once - -#include "collections.h" -#include "gl_traits.h" -#include "special_members.h" -#include - -class VertexArrayObject { -public: - template [[nodiscard]] VertexArrayObject(const T & arrayObject) - { - glBindVertexArray(arrayObject); - } - - ~VertexArrayObject() - { - glBindVertexArray(0); - } - - NO_MOVE(VertexArrayObject); - NO_COPY(VertexArrayObject); - - template struct MP { - constexpr MP(m T::* p) : P {p} { } - - constexpr - operator void *() const - { - return &(static_cast(nullptr)->*P); - } - - m T::* P; - using value_type = m; - }; - - template MP(m T::*) -> MP; - - template - VertexArrayObject & - addAttribs(const GLuint arrayBuffer, const SequentialCollection auto & vertices, const GLuint divisor = 0) - { - addAttribs(arrayBuffer, divisor); - data(vertices, arrayBuffer, GL_ARRAY_BUFFER); - return *this; - } - - template - VertexArrayObject & - addAttribs(const GLuint arrayBuffer, const GLuint divisor = 0) - { - configure_attribs(arrayBuffer, divisor); - return *this; - } - - // Customisation point - template VertexArrayObject & addAttribsFor(const GLuint arrayBuffer, const GLuint divisor = 0); - - template - VertexArrayObject & - addIndices(const GLuint arrayBuffer, const Indices & indices) - { - data(indices, arrayBuffer, GL_ELEMENT_ARRAY_BUFFER); - return *this; - } - - VertexArrayObject & - addIndices(const GLuint arrayBuffer) - { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, arrayBuffer); - return *this; - } - - VertexArrayObject & - data(const GLuint arrayBuffer, GLenum target) - { - glBindBuffer(target, arrayBuffer); - return *this; - } - - template - static void - data(const Data & data, const GLuint arrayBuffer, GLenum target) - { - using Value = typename Data::value_type; - glBindBuffer(target, arrayBuffer); - glBufferData(target, static_cast(sizeof(Value) * data.size()), data.data(), GL_STATIC_DRAW); - } - -private: - template - static auto - set_pointer(const GLuint vertexArrayId, const void * ptr, const GLuint divisor) - { - using traits = gl_traits; - const auto usedAttribs - = traits::vertexAttribFunc(vertexArrayId, traits::size, traits::type, sizeof(VertexT), ptr); - for (GLuint i {}; i < usedAttribs; i++) { - glEnableVertexAttribArray(vertexArrayId + i); - glVertexAttribDivisor(vertexArrayId + i, divisor); - } - return usedAttribs; - } - - template - static auto - set_pointer(const GLuint vertexArrayId, const GLuint divisor) - { - return set_pointer(vertexArrayId, attrib, divisor); - } - - template - void - configure_attribs(const GLuint arrayBuffer, const GLuint divisor) - { - glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer); - if constexpr (sizeof...(attribs) == 0) { - vertexArrayId += set_pointer(vertexArrayId, nullptr, divisor); - } - else { - ((vertexArrayId += set_pointer(vertexArrayId, divisor)), ...); - } - } - - GLuint vertexArrayId {}; -}; diff --git a/lib/gl_traits.h b/lib/gl_traits.h index 97183f0..35ae9e8 100644 --- a/lib/gl_traits.h +++ b/lib/gl_traits.h @@ -14,12 +14,6 @@ struct gl_traits_base { }; 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; - }}; - template static GLuint vertexAttribFormatFunc(GLuint vao, GLuint index, GLuint offset) @@ -30,12 +24,6 @@ struct gl_traits_float : public gl_traits_base { }; 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; - }}; - template static GLuint vertexAttribFormatFunc(GLuint vao, GLuint index, GLuint offset) @@ -46,12 +34,6 @@ struct gl_traits_longfloat : public gl_traits_base { }; 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 static GLuint vertexAttribFormatFunc(GLuint vao, GLuint index, GLuint offset) @@ -115,14 +97,6 @@ template<> struct gl_traits : public gl_traits_integer { template struct gl_traits> : public gl_traits { static constexpr GLint size {S * gl_traits::size}; - static constexpr auto vertexAttribFunc { - [](GLuint index, GLint, GLenum type, GLsizei stride, const void * pointer) -> GLuint { - const auto base = static_cast(pointer); - for (GLuint e = 0; e < S; e++) { - glVertexAttribPointer(index + e, gl_traits::size, type, GL_FALSE, stride, base + e); - } - return S; - }}; 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); @@ -148,14 +122,6 @@ template struct gl_traits 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; - }}; static constexpr auto vertexArrayAttribFormat {[](GLuint vao, GLuint index, GLuint offset) { GLuint used = 0; for (GLuint row = 0; row < R; row++) { -- cgit v1.3