From 52015015343d77f4283f38ee186a29316635e6e7 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 27 Oct 2018 16:04:28 +0200 Subject: [PATCH] - added lock/unlock methods to the buffer implementations. These are not interchangeable with Map/Unmap! Map/Unmap is for mapping a buffer for updating on old hardware, Lock/Unlock are for manually copying some initialization data directly into a static buffer. --- src/gl/system/glsys_vertexbuffer.cpp | 55 +++++++++++++++++++++++++++- src/gl/system/glsys_vertexbuffer.h | 4 ++ src/hwrenderer/data/vertexbuffer.h | 4 ++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/gl/system/glsys_vertexbuffer.cpp b/src/gl/system/glsys_vertexbuffer.cpp index 19cc349e0..91d5205f6 100644 --- a/src/gl/system/glsys_vertexbuffer.cpp +++ b/src/gl/system/glsys_vertexbuffer.cpp @@ -60,7 +60,7 @@ void GLVertexBuffer::SetData(size_t size, void *data, bool staticdata) } else { - mPersistent = screen->BuffersArePersistent(); + mPersistent = screen->BuffersArePersistent() && !staticdata; if (mPersistent) { glBindBuffer(GL_ARRAY_BUFFER, vbo_id); @@ -70,7 +70,7 @@ void GLVertexBuffer::SetData(size_t size, void *data, bool staticdata) else { glBindBuffer(GL_ARRAY_BUFFER, vbo_id); - glBufferData(GL_ARRAY_BUFFER, size, NULL, GL_STREAM_DRAW); + glBufferData(GL_ARRAY_BUFFER, size, NULL, staticdata ? GL_STATIC_DRAW : GL_STREAM_DRAW); map = nullptr; } nomap = false; @@ -143,6 +143,31 @@ void GLVertexBuffer::Bind(int *offsets) } } +//=========================================================================== +// +// +// +//=========================================================================== + +void *GLVertexBuffer::Lock(unsigned int size) +{ + SetData(size, nullptr, true); + return glMapBufferRange(GL_ARRAY_BUFFER, 0, size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_UNSYNCHRONIZED_BIT); +} + +//=========================================================================== +// +// +// +//=========================================================================== + +void GLVertexBuffer::Unlock() +{ + glBindBuffer(GL_ARRAY_BUFFER, vbo_id); + glUnmapBuffer(GL_ARRAY_BUFFER); + gl_RenderState.ResetVertexBuffer(); +} + //========================================================================== // // Index buffer implementation @@ -180,3 +205,29 @@ void GLIndexBuffer::Bind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_id); } + +//=========================================================================== +// +// +// +//=========================================================================== + +void *GLIndexBuffer::Lock(unsigned int size) +{ + SetData(size, nullptr, true); + return glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, 0, size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_UNSYNCHRONIZED_BIT); +} + +//=========================================================================== +// +// +// +//=========================================================================== + +void GLIndexBuffer::Unlock() +{ + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_id); + glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); + gl_RenderState.ResetVertexBuffer(); +} + diff --git a/src/gl/system/glsys_vertexbuffer.h b/src/gl/system/glsys_vertexbuffer.h index d90722ecf..c3c836142 100644 --- a/src/gl/system/glsys_vertexbuffer.h +++ b/src/gl/system/glsys_vertexbuffer.h @@ -27,6 +27,8 @@ public: void Bind(int *offsets); void Map() override; void Unmap() override; + void *Lock(unsigned int size) override; + void Unlock() override; }; class GLIndexBuffer : public IIndexBuffer @@ -38,5 +40,7 @@ public: ~GLIndexBuffer(); void SetData(size_t size, void *data, bool staticdata) override; void Bind(); + void *Lock(unsigned int size) override; + void Unlock() override; }; diff --git a/src/hwrenderer/data/vertexbuffer.h b/src/hwrenderer/data/vertexbuffer.h index 104455a14..d16c3cdbc 100644 --- a/src/hwrenderer/data/vertexbuffer.h +++ b/src/hwrenderer/data/vertexbuffer.h @@ -43,6 +43,8 @@ public: virtual ~IVertexBuffer() {} virtual void SetData(size_t size, void *data, bool staticdata = true) = 0; virtual void SetFormat(int numBindingPoints, int numAttributes, size_t stride, const FVertexBufferAttribute *attrs) = 0; + virtual void *Lock(unsigned int size) = 0; + virtual void Unlock() = 0; virtual void Map() {} // Only needed by old OpenGL but this needs to be in the interface. virtual void Unmap() {} void *Memory() { assert(map); return map; } @@ -59,5 +61,7 @@ protected: public: virtual ~IIndexBuffer() {} virtual void SetData(size_t size, void *data, bool staticdata = true) = 0; + virtual void *Lock(unsigned int size) = 0; + virtual void Unlock() = 0; };