From: Mikko Rasa Date: Wed, 11 Aug 2021 21:09:01 +0000 (+0300) Subject: Remove the notion of default binding point from Buffer X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f1244e29afd2a36aafc2373d485457b4cb0411ff;p=libs%2Fgl.git 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. --- 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