From: Mikko Rasa Date: Wed, 17 Nov 2021 12:49:56 +0000 (+0200) Subject: Add validation for mapping buffers X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=6c5c2bcbd4025cba36f48f6f326466351f4f2971;p=libs%2Fgl.git Add validation for mapping buffers On Vulkan static buffers will be stored on non-host-visible memory and can't be mapped. Already-mapped buffers also can't be mapped. --- diff --git a/source/backends/opengl/buffer_backend.cpp b/source/backends/opengl/buffer_backend.cpp index ab78b1b5..e0f5525a 100644 --- a/source/backends/opengl/buffer_backend.cpp +++ b/source/backends/opengl/buffer_backend.cpp @@ -91,7 +91,6 @@ void *OpenGLBuffer::map() bool OpenGLBuffer::unmap() { - // TODO check if it's mapped if(ARB_direct_state_access) return glUnmapNamedBuffer(id); else if(OES_mapbuffer) diff --git a/source/backends/opengl/buffer_backend.h b/source/backends/opengl/buffer_backend.h index 1e5106a5..6595ec2a 100644 --- a/source/backends/opengl/buffer_backend.h +++ b/source/backends/opengl/buffer_backend.h @@ -24,6 +24,7 @@ protected: void allocate(); void sub_data(size_t, size_t, const void *); + bool can_map() const { return true; } void *map(); bool unmap(); diff --git a/source/core/buffer.cpp b/source/core/buffer.cpp index 15a37397..4a36937e 100644 --- a/source/core/buffer.cpp +++ b/source/core/buffer.cpp @@ -46,5 +46,23 @@ void Buffer::require_size(size_t req_sz) const throw buffer_too_small(format("buffer has %d bytes; %d required", size, req_sz)); } +void *Buffer::map() +{ + if(!can_map() || mapped) + throw invalid_operation("Buffer::map"); + void *result = BufferBackend::map(); + mapped = true; + return result; +} + +bool Buffer::unmap() +{ + if(!can_map() || !mapped) + throw invalid_operation("Buffer::map"); + bool result = BufferBackend::unmap(); + mapped = false; + return result; +} + } // namespace GL } // namespace Msp diff --git a/source/core/buffer.h b/source/core/buffer.h index a12b3373..17678fd4 100644 --- a/source/core/buffer.h +++ b/source/core/buffer.h @@ -62,8 +62,8 @@ public: void require_size(std::size_t) const; - using BufferBackend::map; - using BufferBackend::unmap; + void *map(); + bool unmap(); using BufferBackend::set_debug_name; };