From 017ca4691f84281c0a1ec4da36e7cf81e38dbbbc Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 2 Feb 2024 21:07:23 +0000 Subject: Allow specifying a target other than array buffer GL_ARRAY_BUFFER is the default. Also removes accidental use for OpenGL 4.x functions. --- lib/glContainer.h | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/glContainer.h b/lib/glContainer.h index 4960e3c..5cbb038 100644 --- a/lib/glContainer.h +++ b/lib/glContainer.h @@ -26,7 +26,7 @@ public: using const_reverse_iterator = const_span::reverse_iterator; static constexpr bool is_trivial_dest = std::is_trivially_destructible_v; - glContainer() + explicit glContainer(GLenum target = GL_ARRAY_BUFFER) : target_ {target} { allocBuffer(1); } @@ -41,7 +41,8 @@ public: clear(); } - template typename C> explicit glContainer(const C & src) + template typename C> + explicit glContainer(const C & src, GLenum target = GL_ARRAY_BUFFER) : target_ {target} { reserve(src.size()); std::copy(src.begin(), src.end(), std::back_inserter(*this)); @@ -155,9 +156,9 @@ public: mkspan()[pos] = value; } else { - glBindBuffer(GL_ARRAY_BUFFER, buffer_); - glBufferSubData(GL_ARRAY_BUFFER, static_cast(pos * sizeof(T)), sizeof(value), &value); - glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(target_, buffer_); + glBufferSubData(target_, static_cast(pos * sizeof(T)), sizeof(value), &value); + glBindBuffer(target_, 0); } } @@ -253,7 +254,9 @@ public: unmap() const { if (data_) { - glUnmapNamedBuffer(buffer_); + glBindBuffer(target_, buffer_); + glUnmapBuffer(target_); + glBindBuffer(target_, 0); data_ = {}; access_ = {}; } @@ -432,9 +435,9 @@ protected: if (newCapacity == 0) { return allocBuffer(1); } - glBindBuffer(GL_ARRAY_BUFFER, buffer_); - glBufferData(GL_ARRAY_BUFFER, static_cast(sizeof(T) * newCapacity), nullptr, GL_DYNAMIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(target_, buffer_); + glBufferData(target_, static_cast(sizeof(T) * newCapacity), nullptr, GL_DYNAMIC_DRAW); + glBindBuffer(target_, 0); capacity_ = newCapacity; data_ = {}; access_ = {}; @@ -463,7 +466,9 @@ protected: unmap(); } if (!data_) { - data_ = static_cast(glMapNamedBuffer(buffer_, access)); + glBindBuffer(target_, buffer_); + data_ = static_cast(glMapBuffer(target_, access)); + glBindBuffer(target_, 0); assert(data_); access_ = access; } @@ -484,6 +489,7 @@ protected: } glBuffer buffer_; + GLenum target_; std::size_t capacity_ {}; std::size_t size_ {}; mutable T * data_; -- cgit v1.2.3