]> git.tdb.fi Git - libs/gl.git/commitdiff
Remove the notion of default binding point from Buffer
authorMikko Rasa <tdb@tdb.fi>
Wed, 11 Aug 2021 21:09:01 +0000 (00:09 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 12 Aug 2021 19:32:18 +0000 (22:32 +0300)
It's more robust to always specify the binding point when using the
buffer.

ARRAY_BUFFER is used for buffer operations because it's always available
and is only used transiently by other code so there's no interference.

source/core/buffer.cpp
source/core/buffer.h
source/core/mesh.cpp
source/core/texture2d.cpp
source/render/instancearray.cpp
source/render/programdata.cpp

index 013a136a560a2a5866ee6bf7c343b0102cf9e246..4750846ab1dceb291a600043977f1c06f868f403 100644 (file)
@@ -18,12 +18,11 @@ namespace GL {
 const Buffer *Buffer::bound[5] = { 0, 0, 0, 0, 0 };
 BufferType buffer_types[] = { ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER, PIXEL_PACK_BUFFER, PIXEL_UNPACK_BUFFER, UNIFORM_BUFFER };
 
-Buffer::Buffer(BufferType t):
-       type(t),
+Buffer::Buffer():
        size(0),
        allocated(false)
 {
-       require_buffer_type(type);
+       static Require _req(ARB_vertex_buffer_object);
 
        if(ARB_direct_state_access)
                glCreateBuffers(1, &id);
@@ -76,8 +75,8 @@ void Buffer::allocate()
                        glNamedBufferStorage(id, size, 0, flags);
                else
                {
-                       BindRestore _bind(this, type);
-                       glBufferStorage(type, size, 0, flags);
+                       BindRestore _bind(this, ARRAY_BUFFER);
+                       glBufferStorage(ARRAY_BUFFER, size, 0, flags);
                }
 
                allocated = true;
@@ -102,8 +101,8 @@ void Buffer::data(const void *d)
                glNamedBufferData(id, size, d, STATIC_DRAW);
        else
        {
-               BindRestore _bind(this, type);
-               glBufferData(type, size, d, STATIC_DRAW);
+               BindRestore _bind(this, ARRAY_BUFFER);
+               glBufferData(ARRAY_BUFFER, size, d, STATIC_DRAW);
        }
 
        allocated = true;
@@ -130,8 +129,8 @@ void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
                glNamedBufferSubData(id, off, sz, d);
        else
        {
-               BindRestore _bind(this, type);
-               glBufferSubData(type, off, sz, d);
+               BindRestore _bind(this, ARRAY_BUFFER);
+               glBufferSubData(ARRAY_BUFFER, off, sz, d);
        }
 }
 
@@ -155,8 +154,8 @@ void *Buffer::map()
                return glMapNamedBufferRange(id, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
        else
        {
-               BindRestore _bind(this, type);
-               return glMapBufferRange(type, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
+               BindRestore _bind(this, ARRAY_BUFFER);
+               return glMapBufferRange(ARRAY_BUFFER, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
        }
 }
 
@@ -167,8 +166,8 @@ bool Buffer::unmap()
                return glUnmapNamedBuffer(id);
        else if(OES_mapbuffer)
        {
-               BindRestore _bind(this, type);
-               return glUnmapBuffer(type);
+               BindRestore _bind(this, ARRAY_BUFFER);
+               return glUnmapBuffer(ARRAY_BUFFER);
        }
        else
                return true;
@@ -176,8 +175,7 @@ bool Buffer::unmap()
 
 void Buffer::bind_to(BufferType t) const
 {
-       if(t!=type)
-               require_buffer_type(t);
+       require_buffer_type(t);
        if(t==ELEMENT_ARRAY_BUFFER)
                if(const VertexSetup *vs = VertexSetup::current())
                {
@@ -265,8 +263,7 @@ void BufferRange::data(const void *d)
 
 void BufferRange::bind_to(BufferType t, unsigned i)
 {
-       if(t!=buffer.type)
-               Buffer::require_buffer_type(t);
+       Buffer::require_buffer_type(t);
        if(set_current(t, i, this))
        {
                // The buffer gets bound as a side effect
index ad3084298c55e74e0cfb4296bab53a556c5cceae..83e75927ae31c3a44cd81420cea8f626a39c605e 100644 (file)
@@ -62,7 +62,6 @@ class Buffer
        friend class BufferRange;
 
 private:
-       BufferType type;
        unsigned id;
        unsigned size;
        bool allocated;
@@ -70,7 +69,7 @@ private:
        static const Buffer *bound[5];
 
 public:
-       Buffer(BufferType);
+       Buffer();
        ~Buffer();
 
 private:
@@ -80,9 +79,6 @@ public:
        /** Returns the OpenGL ID of the buffer.  For internal use only. */
        unsigned get_id() const { return id; }
 
-       /** Returns the default binding type for the buffer. */
-       BufferType get_type() const { return type; }
-
        /** Defines the storage size of the buffer.  Must be called before data can
        be uploaded.  Storage cannot be changed once set. */
        void storage(unsigned);
@@ -116,15 +112,9 @@ public:
        DEPRECATED void *map(BufferAccess) { return map(); }
        bool unmap();
 
-       /** Binds the buffer in its default slot. */
-       void bind() const { bind_to(type); }
-
-       /** Binds the buffer in an alternate slot. */
+       /** Binds the buffer. */
        void bind_to(BufferType) const;
 
-       /** Unbinds the buffer from its default slot. */
-       void unbind() const { unbind_from(type); }
-
        static const Buffer *current(BufferType);
        static void unbind_from(BufferType);
 private:
index ea14d590b3012815526247555a4d4a2c74d473d1..492f416d1adeccb28e2abf3febfbe874a679abeb 100644 (file)
@@ -68,7 +68,7 @@ void Mesh::check_buffers(unsigned mask)
                if(!vbuf || (vbuf->get_size()>0 && vbuf->get_size()<req_size))
                {
                        delete vbuf;
-                       vbuf = new Buffer(ARRAY_BUFFER);
+                       vbuf = new Buffer;
                        vertices.use_buffer(vbuf);
                        vtx_setup.set_vertex_array(vertices);
                        dirty |= VERTEX_BUFFER;
@@ -86,7 +86,7 @@ void Mesh::check_buffers(unsigned mask)
                if(!ibuf || (ibuf->get_size()>0 && ibuf->get_size()<req_size))
                {
                        delete ibuf;
-                       ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
+                       ibuf = new Buffer;
                        if(!batches.empty())
                                batches.front().change_buffer(ibuf);
                        vtx_setup.set_index_buffer(*ibuf);
index b3caf509ec3108b1d418c8298186d1e795d2cb03..c0a96a4e7e2d43e10e14b58f268d8952fe0ac90c 100644 (file)
@@ -254,7 +254,6 @@ void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h,
 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
        texture(t),
        io(i),
-       pixel_buffer(PIXEL_UNPACK_BUFFER),
        mapped_address(0),
        img_loader(Graphics::ImageLoader::open_io(io)),
        phase(0)
index 5854906fe9229de3a59e46f13f27e8f3521c2d20..11279f76262fe60882df087e91d50d84d43a173b 100644 (file)
@@ -48,7 +48,7 @@ InstanceArray::InstanceArray(const Object &o):
                const VertexFormat &fmt = instance_data->get_format();
                matrix_offset = fmt.offset(make_indexed_attribute(RAW_ATTRIB4, matrix_location));
 
-               instance_buffer = new Buffer(ARRAY_BUFFER);
+               instance_buffer = new Buffer;
                instance_data->use_buffer(instance_buffer);
 
                vtx_setup = new VertexSetup;
@@ -82,7 +82,7 @@ void InstanceArray::append(ObjectInstance *inst)
                        if(instance_buffer->get_size()>0 && instance_buffer->get_size()<req_size)
                        {
                                delete instance_buffer;
-                               instance_buffer = new Buffer(ARRAY_BUFFER);
+                               instance_buffer = new Buffer;
                                instance_data->use_buffer(instance_buffer);
                        }
                }
index 4133b9acaabb207fbd63d077ca9bc8da7bcca2c4..b21c395cf04c27f1084492294dbd2635993af5df 100644 (file)
@@ -566,7 +566,7 @@ void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program
                {
                        if(!buffer)
                        {
-                               buffer = new Buffer(UNIFORM_BUFFER);
+                               buffer = new Buffer();
 
 #ifdef DEBUG
                                if(!debug_name.empty())
@@ -657,11 +657,6 @@ void ProgramData::apply() const
 
                prog_begin->masks.dirty = 0;
 
-               /* If any blocks stored in the buffer were updated, bind the buffer here
-               to avoid state thrashing. */
-               if(buffered_blocks_updated && !ARB_direct_state_access)
-                       buffer->bind();
-
                if(last_buffer_block!=old_last_block)
                {
                        unsigned required_size = last_buffer_block->get_required_buffer_size();
@@ -670,7 +665,7 @@ void ProgramData::apply() const
                                if(buffer->get_size()>0)
                                {
                                        delete buffer;
-                                       buffer = new Buffer(UNIFORM_BUFFER);
+                                       buffer = new Buffer();
                                        last_buffer_block->change_buffer(buffer);
 
 #ifdef DEBUG