]> git.tdb.fi Git - libs/gl.git/commitdiff
Add validation for mapping buffers
authorMikko Rasa <tdb@tdb.fi>
Wed, 17 Nov 2021 12:49:56 +0000 (14:49 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 17 Nov 2021 13:07:00 +0000 (15:07 +0200)
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.

source/backends/opengl/buffer_backend.cpp
source/backends/opengl/buffer_backend.h
source/core/buffer.cpp
source/core/buffer.h

index ab78b1b5c3bd0b3b7fa63cb90e14d19ffc9de2f6..e0f5525a9a2b211f8b2f5367ee78379aa8415146 100644 (file)
@@ -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)
index 1e5106a50095b838488fd3c2a95980b5aba8b302..6595ec2a65e0c63c79a2b09ca9271cf4486340be 100644 (file)
@@ -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();
 
index 15a37397bcb18bdf094b5e5a5cef1a7d31b38c31..4a36937e7b745d1dd32f8c1e9ce19df0766037d7 100644 (file)
@@ -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
index a12b3373e601664bddf75d60bb930d56bbe26833..17678fd4b4008f68a7dd2f3745916fd237526a10 100644 (file)
@@ -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;
 };