diff --git a/libs/gfx/include/psemek/gfx/buffer.hpp b/libs/gfx/include/psemek/gfx/buffer.hpp index eb25949e..3ad7b7f8 100644 --- a/libs/gfx/include/psemek/gfx/buffer.hpp +++ b/libs/gfx/include/psemek/gfx/buffer.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace psemek::gfx { @@ -41,8 +42,24 @@ namespace psemek::gfx load(data.data(), data.size(), usage); } + template + std::shared_ptr map() + { + if (auto p = mapped_.lock()) + return std::static_pointer_cast(p); + + bind(); + std::shared_ptr p(reinterpret_cast(gl::MapBuffer(gl::ARRAY_BUFFER, gl::WRITE_ONLY)), [this](T *){ + bind(); + gl::UnmapBuffer(gl::ARRAY_BUFFER); + }); + mapped_ = p; + return p; + } + private: GLuint id_; + std::weak_ptr mapped_; explicit buffer(std::nullptr_t); };