]> git.tdb.fi Git - libs/gl.git/commitdiff
Track allocation status of Buffer
authorMikko Rasa <tdb@tdb.fi>
Mon, 29 Mar 2021 22:34:08 +0000 (01:34 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 29 Mar 2021 22:34:08 +0000 (01:34 +0300)
If ABR_buffer_storage is not supported, calling storage() and then
sub_data() did not result in the buffer being allocated.

source/core/buffer.cpp
source/core/buffer.h

index 56705b8701b88a00b691bf25725d10042554679a..56810b6f4ff12aa41f9c37b7812f5d4496187e7a 100644 (file)
@@ -18,7 +18,8 @@ BufferType buffer_types[] = { ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER, PIXEL_PACK_BUF
 
 Buffer::Buffer(BufferType t):
        type(t),
-       size(0)
+       size(0),
+       allocated(false)
 {
        require_buffer_type(type);
 
@@ -53,6 +54,15 @@ void Buffer::storage(unsigned sz)
                throw invalid_argument("Buffer::storage");
 
        size = sz;
+}
+
+void Buffer::allocate()
+{
+       if(size==0)
+               throw invalid_operation("Buffer::allocate");
+       if(allocated)
+               return;
+
        if(ARB_buffer_storage)
        {
                static const int flags = GL_MAP_READ_BIT|GL_MAP_WRITE_BIT|GL_DYNAMIC_STORAGE_BIT;
@@ -63,7 +73,11 @@ void Buffer::storage(unsigned sz)
                        BindRestore _bind(this, type);
                        glBufferStorage(type, size, 0, flags);
                }
+
+               allocated = true;
        }
+       else
+               data(0);
 }
 
 void Buffer::set_usage(BufferUsage)
@@ -85,6 +99,8 @@ void Buffer::data(const void *d)
                BindRestore _bind(this, type);
                glBufferData(type, size, d, STATIC_DRAW);
        }
+
+       allocated = true;
 }
 
 void Buffer::data(unsigned sz, const void *d)
@@ -99,6 +115,11 @@ void Buffer::data(unsigned sz, const void *d)
 
 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
 {
+       if(size==0)
+               throw invalid_operation("Buffer::sub_data");
+
+       allocate();
+
        if(ARB_direct_state_access)
                glNamedBufferSubData(id, off, sz, d);
        else
index cc5e58741c141f1fd5910e1abda23ec68a650a39..47cab8cd5646e2c3842f641bd31dcb4c418aaf6f 100644 (file)
@@ -65,6 +65,7 @@ private:
        BufferType type;
        unsigned id;
        unsigned size;
+       bool allocated;
 
        static const Buffer *bound[5];
 
@@ -86,6 +87,10 @@ public:
        be uploaded.  Storage cannot be changed once set. */
        void storage(unsigned);
 
+       /** Allocates storage for the buffer.  The contents are initially undefined.
+       If storage has already been allocated, does nothing. */
+       void allocate();
+
        /** Sets the usage hint of the buffer.  It will take effect the next time
        the buffer's contents are defined. */
        DEPRECATED void set_usage(BufferUsage);