From f3ee640033ec6367915b51a3beaf2330f39d75ac Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 18 Jun 2019 11:42:22 +0300 Subject: [PATCH] Move vertex array object management out of Mesh It's called VertexSetup because VertexArray was already in use. On the other hand I don't want to tie VAOs with the VertexArray class because instanced rendering will need two VertexArrays in a single VAO. --- source/buffer.cpp | 14 +++---- source/mesh.cpp | 50 +++++++---------------- source/mesh.h | 4 +- source/vertexsetup.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++ source/vertexsetup.h | 42 ++++++++++++++++++++ 5 files changed, 155 insertions(+), 45 deletions(-) create mode 100644 source/vertexsetup.cpp create mode 100644 source/vertexsetup.h diff --git a/source/buffer.cpp b/source/buffer.cpp index 2b18d7d7..4b485dda 100644 --- a/source/buffer.cpp +++ b/source/buffer.cpp @@ -3,8 +3,8 @@ #include #include "buffer.h" #include "error.h" -#include "mesh.h" #include "misc.h" +#include "vertexsetup.h" using namespace std; @@ -127,10 +127,10 @@ void Buffer::bind_to(BufferType t) const if(t!=type) require_buffer_type(t); if(t==ELEMENT_ARRAY_BUFFER) - if(const Mesh *m = Mesh::current()) + if(const VertexSetup *vs = VertexSetup::current()) { - // Don't change the binding in a mesh's vertex array object - if(this==m->get_index_buffer()) + // Don't change the binding in a vertex array object + if(this==vs->get_index_buffer()) return; throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)"); } @@ -141,14 +141,14 @@ void Buffer::bind_to(BufferType t) const const Buffer *Buffer::current(BufferType t) { if(t==ELEMENT_ARRAY_BUFFER) - if(const Mesh *m = Mesh::current()) - return m->get_index_buffer(); + if(const VertexSetup *vs = VertexSetup::current()) + return vs->get_index_buffer(); return binding(t); } void Buffer::unbind_from(BufferType type) { - if(type==ELEMENT_ARRAY_BUFFER && Mesh::current()) + if(type==ELEMENT_ARRAY_BUFFER && VertexSetup::current()) throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)"); if(set_current(type, 0)) glBindBuffer(type, 0); diff --git a/source/mesh.cpp b/source/mesh.cpp index b3b719c5..a463ba7f 100644 --- a/source/mesh.cpp +++ b/source/mesh.cpp @@ -7,6 +7,7 @@ #include "mesh.h" #include "renderer.h" #include "resourcemanager.h" +#include "vertexsetup.h" using namespace std; @@ -29,9 +30,8 @@ void Mesh::init(ResourceManager *rm) { vbuf = 0; ibuf = 0; - vao_id = 0; + vtx_setup = 0; defer_buffers = ARB_vertex_buffer_object; - dirty = true; disallow_rendering = false; winding = 0; @@ -42,10 +42,9 @@ void Mesh::init(ResourceManager *rm) Mesh::~Mesh() { set_manager(0); + delete vtx_setup; delete vbuf; delete ibuf; - if(vao_id) - glDeleteVertexArrays(1, &vao_id); } void Mesh::clear() @@ -80,33 +79,12 @@ void Mesh::create_buffers() if(!ibuf) ibuf = new Buffer(ELEMENT_ARRAY_BUFFER); - if(ARB_vertex_array_object && !vao_id) - glGenVertexArrays(1, &vao_id); -} - -void Mesh::setup_vao() const -{ - Bind bind_vbuf(vbuf, ARRAY_BUFFER); - - const VertexFormat &fmt = vertices.get_format(); - unsigned stride = get_stride(fmt)*sizeof(float); - float *ptr = 0; - for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c) + if(ARB_vertex_array_object && !vtx_setup) { - unsigned t = get_component_type(*c); - if(t>=get_component_type(ATTRIB1)) - t -= get_component_type(ATTRIB1); - unsigned sz = get_component_size(*c); - if(*c==COLOR4_UBYTE) - glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, ptr); - else - glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, ptr); - glEnableVertexAttribArray(t); - ptr += sz; + vtx_setup = new VertexSetup; + vtx_setup->set_vertex_array(vertices); + vtx_setup->set_index_buffer(*ibuf); } - glBindBuffer(ELEMENT_ARRAY_BUFFER, ibuf->get_id()); - - dirty = false; } unsigned Mesh::get_n_vertices() const @@ -199,26 +177,24 @@ void Mesh::draw(Renderer &renderer) const void Mesh::bind() const { - /* If VAOs are not supported, vao_id is zero and set_current won't get + /* If VAOs are not supported, vtx_setup is zero and set_current won't get called. Thus unbind won't try to call a null function either. */ - if(!vao_id) + if(!vtx_setup) { unbind(); vertices.apply(false); } else if(set_current(this)) { - glBindVertexArray(vao_id); + vtx_setup->bind(); vertices.refresh(); - if(dirty) - setup_vao(); } } void Mesh::unbind() { if(set_current(0)) - glBindVertexArray(0); + VertexSetup::unbind(); } Resource::AsyncLoader *Mesh::load(IO::Seekable &io, const Resources *) @@ -266,7 +242,9 @@ void Mesh::Loader::vertices(const vector &c) for(vector::const_iterator i=c.begin(); i!=c.end(); ++i) fmt = (fmt, *i); obj.vertices.reset(fmt); - obj.dirty = true; + if(obj.vtx_setup) + // Set it again to force the vertex setup to update + obj.vtx_setup->set_vertex_array(obj.vertices); load_sub(obj.vertices); } diff --git a/source/mesh.h b/source/mesh.h index 70370633..db0163e1 100644 --- a/source/mesh.h +++ b/source/mesh.h @@ -12,6 +12,7 @@ namespace GL { class Buffer; class Renderer; +class VertexSetup; /** Raw mesh data, consisting of a VertexArray and one or more Batches. Though a @@ -55,7 +56,7 @@ private: std::vector batches; Buffer *vbuf; Buffer *ibuf; - unsigned vao_id; + VertexSetup *vtx_setup; bool defer_buffers; mutable bool dirty; bool disallow_rendering; @@ -73,7 +74,6 @@ public: void use_buffers(bool); private: void create_buffers(); - void setup_vao() const; public: const VertexArray &get_vertices() const { return vertices; } diff --git a/source/vertexsetup.cpp b/source/vertexsetup.cpp new file mode 100644 index 00000000..f6a71f49 --- /dev/null +++ b/source/vertexsetup.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include "buffer.h" +#include "gl.h" +#include "vertexarray.h" +#include "vertexsetup.h" + +namespace Msp { +namespace GL { + +VertexSetup::VertexSetup(): + dirty(false), + array(0), + index_buffer(0) +{ + static Require req(ARB_vertex_array_object); + glGenVertexArrays(1, &id); +} + +VertexSetup::~VertexSetup() +{ + if(current()==this) + unbind(); + glDeleteVertexArrays(1, &id); +} + +void VertexSetup::set_vertex_array(const VertexArray &a) +{ + array = &a; + update(); +} + +void VertexSetup::set_index_buffer(const Buffer &ibuf) +{ + index_buffer = &ibuf; + update(); +} + +void VertexSetup::update() const +{ + if(current()!=this) + { + dirty = true; + return; + } + + Bind bind_vbuf(array->get_buffer(), ARRAY_BUFFER); + + const VertexFormat &fmt = array->get_format(); + unsigned stride = get_stride(fmt)*sizeof(float); + float *ptr = 0; + for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c) + { + unsigned t = get_component_type(*c); + if(t>=get_component_type(ATTRIB1)) + t -= get_component_type(ATTRIB1); + unsigned sz = get_component_size(*c); + if(*c==COLOR4_UBYTE) + glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, ptr); + else + glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, ptr); + glEnableVertexAttribArray(t); + ptr += sz; + } + + glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id()); +} + +void VertexSetup::bind() const +{ + if(set_current(this)) + { + glBindVertexArray(id); + if(dirty) + { + update(); + dirty = false; + } + } +} + +void VertexSetup::unbind() +{ + if(set_current(0)) + glBindVertexArray(0); +} + +} // namespace GL +} // namespace Msp diff --git a/source/vertexsetup.h b/source/vertexsetup.h new file mode 100644 index 00000000..466eed40 --- /dev/null +++ b/source/vertexsetup.h @@ -0,0 +1,42 @@ +#ifndef MSP_GL_VERTEXSETUP_H_ +#define MSP_GL_VERTEXSETUP_H_ + +#include "bindable.h" + +namespace Msp { +namespace GL { + +class VertexArray; + +/** +Combines a VertexArray with an index buffer. This wraps OpenGL's vertex array +objects. Intended for internal use. +*/ +class VertexSetup: public Bindable +{ +private: + unsigned id; + mutable bool dirty; + const VertexArray *array; + const Buffer *index_buffer; + +public: + VertexSetup(); + ~VertexSetup(); + + void set_vertex_array(const VertexArray &); + void set_index_buffer(const Buffer &); + const Buffer *get_index_buffer() const { return index_buffer; } + +private: + void update() const; + +public: + void bind() const; + static void unbind(); +}; + +} // namespace GL +} // namespace Msp + +#endif -- 2.43.0