Support mapping gfx::buffer (write-only)

This commit is contained in:
Nikita Lisitsa 2020-12-10 21:56:13 +03:00
parent 77a32e444a
commit 28b4bad3a7

View file

@ -3,6 +3,7 @@
#include <psemek/gfx/gl.hpp>
#include <vector>
#include <memory>
namespace psemek::gfx
{
@ -41,8 +42,24 @@ namespace psemek::gfx
load(data.data(), data.size(), usage);
}
template <typename T>
std::shared_ptr<T> map()
{
if (auto p = mapped_.lock())
return std::static_pointer_cast<T>(p);
bind();
std::shared_ptr<T> p(reinterpret_cast<T *>(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<void> mapped_;
explicit buffer(std::nullptr_t);
};