diff options
| author | Dan Goodliffe <dan.goodliffe@octal.co.uk> | 2026-03-10 09:40:37 +0000 |
|---|---|---|
| committer | Dan Goodliffe <dan.goodliffe@octal.co.uk> | 2026-03-10 10:23:51 +0000 |
| commit | b73a3c677171a13bdd7b0044071601647bfbfe67 (patch) | |
| tree | 31f3ff8371ee964dfb6e68226d8f7f835c46293a /lib/glMappedBufferSpan.h | |
| parent | Switch frame and render buffer to DSA helpers (diff) | |
| download | ilt-main.tar.bz2 ilt-main.tar.xz ilt-main.zip | |
Diffstat (limited to 'lib/glMappedBufferSpan.h')
| -rw-r--r-- | lib/glMappedBufferSpan.h | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/glMappedBufferSpan.h b/lib/glMappedBufferSpan.h new file mode 100644 index 0000000..d30c911 --- /dev/null +++ b/lib/glMappedBufferSpan.h @@ -0,0 +1,51 @@ +#pragma once + +#include "special_members.h" +#include <cstddef> +#include <glad/gl.h> +#include <span> +#include <utility> + +template<typename T> class glMappedBufferSpan : public std::span<T> { +public: + glMappedBufferSpan(GLuint buffer, size_t count, GLenum access, bool reinit) : + std::span<T> {[&]() { + if (reinit) { + glNamedBufferData( + buffer, static_cast<GLsizeiptr>(sizeof(T) * count), nullptr, GL_DYNAMIC_DRAW); + } + return static_cast<T *>(glMapNamedBuffer(buffer, access)); + }(), + count}, + buffer {buffer} + { + } + + ~glMappedBufferSpan() + { + if (buffer) { + glUnmapNamedBuffer(buffer); + } + } + + glMappedBufferSpan(glMappedBufferSpan && other) noexcept : + std::span<T> {other}, buffer {std::exchange(other.buffer, 0)} + { + } + + glMappedBufferSpan & + operator=(glMappedBufferSpan && other) noexcept + { + std::span<T>::span = other; + if (buffer) { + glUnmapBuffer(buffer); + } + buffer = std::exchange(other.buffer, 0); + return *this; + } + + NO_COPY(glMappedBufferSpan); + +private: + GLuint buffer; +}; |
