From f1244e29afd2a36aafc2373d485457b4cb0411ff Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 12 Aug 2021 00:09:01 +0300 Subject: [PATCH] Remove the notion of default binding point from Buffer 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 | 31 ++++++++++++++----------------- source/core/buffer.h | 14 ++------------ source/core/mesh.cpp | 4 ++-- source/core/texture2d.cpp | 1 - source/render/instancearray.cpp | 4 ++-- source/render/programdata.cpp | 9 ++------- 6 files changed, 22 insertions(+), 41 deletions(-) diff --git a/source/core/buffer.cpp b/source/core/buffer.cpp index 013a136a..4750846a 100644 --- a/source/core/buffer.cpp +++ b/source/core/buffer.cpp @@ -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 diff --git a/source/core/buffer.h b/source/core/buffer.h index ad308429..83e75927 100644 --- a/source/core/buffer.h +++ b/source/core/buffer.h @@ -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: diff --git a/source/core/mesh.cpp b/source/core/mesh.cpp index ea14d590..492f416d 100644 --- a/source/core/mesh.cpp +++ b/source/core/mesh.cpp @@ -68,7 +68,7 @@ void Mesh::check_buffers(unsigned mask) if(!vbuf || (vbuf->get_size()>0 && vbuf->get_size()get_size()>0 && ibuf->get_size()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()use_buffer(instance_buffer); } } diff --git a/source/render/programdata.cpp b/source/render/programdata.cpp index 4133b9ac..b21c395c 100644 --- a/source/render/programdata.cpp +++ b/source/render/programdata.cpp @@ -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 -- 2.43.0