From 8b99547d099c96bd8cc6037e2026db123b2523c1 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 21 Dec 2013 13:30:02 +0200 Subject: [PATCH] Present Mesh's index buffer as current while the Mesh is bound This simplifies the internal update operations. --- source/batch.cpp | 15 +-------------- source/buffer.cpp | 19 ++++++++++++++++--- source/buffer.h | 2 +- source/mesh.cpp | 22 +++++++++------------- source/mesh.h | 1 + 5 files changed, 28 insertions(+), 31 deletions(-) diff --git a/source/batch.cpp b/source/batch.cpp index bca33484..7f9adff7 100644 --- a/source/batch.cpp +++ b/source/batch.cpp @@ -248,25 +248,12 @@ void Batch::draw() const if(Buffer *ibuf = get_buffer()) { - bool have_vao = Mesh::current(); - const Buffer *old_ibuf = 0; - if(!have_vao) - { - old_ibuf = Buffer::current(ELEMENT_ARRAY_BUFFER); - ibuf->bind_to(ELEMENT_ARRAY_BUFFER); - } + BindRestore _bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER); if(dirty) update_buffer(); glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast(get_offset())); - if(!have_vao) - { - if(old_ibuf) - old_ibuf->bind_to(ELEMENT_ARRAY_BUFFER); - else - Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); - } } else glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]); diff --git a/source/buffer.cpp b/source/buffer.cpp index 61670cf9..9fa03c12 100644 --- a/source/buffer.cpp +++ b/source/buffer.cpp @@ -65,13 +65,26 @@ void Buffer::bind_to(BufferType t) const { if(t!=type) require_buffer_type(t); - // Don't change the binding in a mesh's vertex array object - if(t==ELEMENT_ARRAY_BUFFER && Mesh::current()) - throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)"); + if(t==ELEMENT_ARRAY_BUFFER) + if(const Mesh *m = Mesh::current()) + { + // Don't change the binding in a mesh's vertex array object + if(this==m->get_index_buffer()) + return; + throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)"); + } if(set_current(t, this)) glBindBuffer(t, id); } +const Buffer *Buffer::current(BufferType t) +{ + if(t==ELEMENT_ARRAY_BUFFER) + if(const Mesh *m = Mesh::current()) + return m->get_index_buffer(); + return binding(t); +} + void Buffer::unbind_from(BufferType type) { if(type==ELEMENT_ARRAY_BUFFER && Mesh::current()) diff --git a/source/buffer.h b/source/buffer.h index bfbfd148..a7d4c2ba 100644 --- a/source/buffer.h +++ b/source/buffer.h @@ -89,7 +89,7 @@ public: /** Unbinds the buffer from its default slot. */ void unbind() const { unbind_from(type); } - static const Buffer *current(BufferType t) { return binding(t); } + static const Buffer *current(BufferType); static void unbind_from(BufferType); private: static const Buffer *&binding(BufferType); diff --git a/source/mesh.cpp b/source/mesh.cpp index c9819ae9..413986f4 100644 --- a/source/mesh.cpp +++ b/source/mesh.cpp @@ -80,16 +80,18 @@ void Mesh::refresh() const { if(dirty&1) { - unbind(); for(list::const_iterator i=batches.begin(); i!=batches.end(); ++i) i->refresh(); } if(dirty&2) { - glBindVertexArray(vao_id); Bind bind_vbuf(vbuf, ARRAY_BUFFER); + bool bind_here = !current(); + if(bind_here) + glBindVertexArray(vao_id); + const VertexFormat &fmt = vertices.get_format(); unsigned stride = get_stride(fmt)*sizeof(float); float *ptr = 0; @@ -107,7 +109,9 @@ void Mesh::refresh() const ptr += sz; } glBindBuffer(ELEMENT_ARRAY_BUFFER, ibuf->get_id()); - glBindVertexArray(0); + + if(bind_here) + glBindVertexArray(0); } dirty = 0; @@ -184,16 +188,8 @@ void Mesh::bind() const called. Thus unbind won't try to call a null function either. */ if(!vao_id) unbind(); - else - { - if(Buffer::current(ELEMENT_ARRAY_BUFFER)) - { - unbind(); - Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); - } - if(set_current(this)) - glBindVertexArray(vao_id); - } + else if(set_current(this)) + glBindVertexArray(vao_id); } void Mesh::unbind() diff --git a/source/mesh.h b/source/mesh.h index 2aba273e..ba21f256 100644 --- a/source/mesh.h +++ b/source/mesh.h @@ -55,6 +55,7 @@ private: public: const VertexArray &get_vertices() const { return vertices; } + const Buffer *get_index_buffer() const { return ibuf; } unsigned get_n_vertices() const; float *modify_vertex(unsigned); -- 2.45.2