From 22f0f95981f17524587f5f2c5e3e91005240ddb7 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 29 Oct 2019 02:25:16 +0200 Subject: [PATCH] Remove bind mechanics from Mesh and VertexArray It's now handled through VertexSetup. --- source/mesh.cpp | 17 +----- source/mesh.h | 8 +-- source/renderer.cpp | 21 +------ source/renderer.h | 2 - source/vertexarray.cpp | 124 ----------------------------------------- source/vertexarray.h | 27 +-------- source/vertexsetup.cpp | 3 + 7 files changed, 9 insertions(+), 193 deletions(-) diff --git a/source/mesh.cpp b/source/mesh.cpp index 4b247e8d..c6543f01 100644 --- a/source/mesh.cpp +++ b/source/mesh.cpp @@ -123,7 +123,7 @@ void Mesh::draw(Renderer &renderer) const return; } - renderer.set_mesh(this); + renderer.set_vertex_setup(&vtx_setup); renderer.set_winding_test(winding); for(vector::const_iterator i=batches.begin(); i!=batches.end(); ++i) @@ -149,21 +149,6 @@ void Mesh::draw_instanced(Renderer &renderer, const VertexSetup &vs, unsigned co renderer.draw_instanced(*i, count); } -void Mesh::bind() const -{ - if(set_current(this)) - { - vtx_setup.bind(); - vertices.refresh(); - } -} - -void Mesh::unbind() -{ - if(set_current(0)) - VertexSetup::unbind(); -} - Resource::AsyncLoader *Mesh::load(IO::Seekable &io, const Resources *) { return new AsyncLoader(*this, io); diff --git a/source/mesh.h b/source/mesh.h index 2de418ae..b4ba0c45 100644 --- a/source/mesh.h +++ b/source/mesh.h @@ -19,7 +19,7 @@ Raw mesh data, consisting of a VertexArray and one or more Batches. Though a Mesh can draw itself, it's usually used as part of Renderables rather than on its own. */ -class Mesh: public Bindable, public Resource +class Mesh: public Resource { friend class MeshBuilder; @@ -89,12 +89,6 @@ public: void draw(Renderer &) const; void draw_instanced(Renderer &, const VertexSetup &, unsigned) const; - /** Binds the mesh for rendering. The vertex array is applied using generic - attributes only. Uses vertex array object if possible. */ - void bind() const; - - static void unbind(); - virtual int get_load_priority() const { return 1; } virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0); virtual UInt64 get_data_size() const; diff --git a/source/renderer.cpp b/source/renderer.cpp index 302d9437..62e5ced0 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -5,7 +5,6 @@ #include "error.h" #include "lighting.h" #include "material.h" -#include "mesh.h" #include "program.h" #include "programdata.h" #include "renderable.h" @@ -144,11 +143,6 @@ void Renderer::flush_shader_data() shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end()); } -void Renderer::set_mesh(const Mesh *m) -{ - state->mesh = m; -} - void Renderer::set_vertex_setup(const VertexSetup *vs) { state->vertex_setup = vs; @@ -235,7 +229,6 @@ void Renderer::end() shdata_stack.clear(); excluded.clear(); - Mesh::unbind(); Texturing::unbind(); Texture::unbind_from(0); Clipping::unbind(); @@ -336,17 +329,10 @@ void Renderer::apply_state() else Program::unbind(); - if(state->mesh) - state->mesh->bind(); + if(state->vertex_setup) + state->vertex_setup->bind(); else - { - Mesh::unbind(); - - if(state->vertex_setup) - state->vertex_setup->bind(); - else - VertexSetup::unbind(); - } + VertexSetup::unbind(); if(state->winding_test) { @@ -370,7 +356,6 @@ Renderer::State::State(): clipping(0), shprog(0), shdata_count(0), - mesh(0), vertex_setup(0), winding_test(0), reverse_winding(false), diff --git a/source/renderer.h b/source/renderer.h index 4899b4f3..dd732bd8 100644 --- a/source/renderer.h +++ b/source/renderer.h @@ -77,7 +77,6 @@ private: Matrix clipping_matrix; const Program *shprog; unsigned shdata_count; - const Mesh *mesh; const VertexSetup *vertex_setup; const WindingTest *winding_test; bool reverse_winding; @@ -147,7 +146,6 @@ public: void flush_shader_data(); - void set_mesh(const Mesh *); void set_vertex_setup(const VertexSetup *); void set_winding_test(const WindingTest *); void set_reverse_winding(bool); diff --git a/source/vertexarray.cpp b/source/vertexarray.cpp index 9947ac11..e3aef7a0 100644 --- a/source/vertexarray.cpp +++ b/source/vertexarray.cpp @@ -16,60 +16,11 @@ VertexArray::VertexArray(const VertexFormat &f) reset(f); } -VertexArray::~VertexArray() -{ - /* Unbind accesses the current VertexArray, so a call from ~Bindable would - try to access destroyed data. */ - if(current()==this) - unbind(); -} - void VertexArray::reset(const VertexFormat &f) { clear(); format = f; stride = get_stride(format); - - arrays.clear(); - - unsigned offs = 0; - for(const unsigned char *c=format.begin(); c!=format.end(); ++c) - { - unsigned slot = get_array_slot(*c); - if(slot>=arrays.size()) - arrays.resize(slot+1); - - Array &arr = arrays[slot]; - arr.component = *c; - arr.offset = offs; - - offs += get_component_size(*c); - } -} - -unsigned VertexArray::get_array_slot(unsigned char comp) -{ - unsigned t = get_component_type(comp); - if(t==get_component_type(VERTEX3)) - return 0; - else if(t==get_component_type(NORMAL3)) - return 1; - else if(t==get_component_type(COLOR4_FLOAT)) - return 2; - else if(comp>=TEXCOORD1 && comp<=TEXCOORD4+12) - { - t -= get_component_type(TEXCOORD1); - if(t>0) - static Require _req(ARB_multitexture); - return 3+t; - } - else - { - static Require _req(ARB_vertex_shader); - if(comp>=ATTRIB1) - t -= get_component_type(ATTRIB1); - return 7+t; - } } void VertexArray::clear() @@ -101,81 +52,6 @@ unsigned VertexArray::get_data_size() const return data.size()*sizeof(float); } -void VertexArray::apply() const -{ - if(format.empty()) - throw invalid_operation("VertexArray::apply"); - // Don't mess up the vertex array object of a mesh - if(Mesh::current()) - throw invalid_operation("VertexArray::apply"); - - static Require _req(ARB_vertex_shader); - - const VertexArray *old = current(); - /* If the array has been modified, apply it even if it was the last one to - be applied. This is necessary to get the data updated to vertex buffer, and - to resync things after a format change. Radeon drivers also have some - problems with modifying vertex arrays without re-setting the pointers. */ - if(!set_current(this) && !dirty) - return; - - const Buffer *vbuf = get_buffer(); - Bind _bind_vbuf(vbuf, ARRAY_BUFFER); - if(vbuf && dirty) - update_buffer(); - - const float *base = (vbuf ? reinterpret_cast(get_offset()) : &data[0]); - unsigned stride_bytes = stride*sizeof(float); - apply_arrays(&arrays, (old ? &old->arrays : 0), base, stride_bytes); -} - -void VertexArray::apply_arrays(const vector *arrays, const vector *old_arrays, const float *base, unsigned stride_bytes) -{ - unsigned n_arrays = arrays ? arrays->size() : 0; - if(old_arrays) - n_arrays = max(n_arrays, old_arrays->size()); - for(unsigned i=0; isize() && (*arrays)[i].component) ? &(*arrays)[i] : 0); - const Array *old_arr = ((old_arrays && isize() && (*old_arrays)[i].component) ? &(*old_arrays)[i] : 0); - if(!arr && !old_arr) - continue; - - unsigned char comp = (arr ? arr->component : old_arr->component); - unsigned sz = get_component_size(comp); - unsigned t = get_component_type(comp); - - if(t>=get_component_type(ATTRIB1)) - t -= get_component_type(ATTRIB1); - if(arr) - { - if(arr->component==COLOR4_UBYTE) - glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride_bytes, base+arr->offset); - else - glVertexAttribPointer(t, sz, GL_FLOAT, false, stride_bytes, base+arr->offset); - } - - // Only change enable state if needed - if(arr && !old_arr) - glEnableVertexAttribArray(t); - else if(old_arr && !arr) - glDisableVertexAttribArray(t); - } -} - -void VertexArray::unbind() -{ - const VertexArray *old = current(); - if(set_current(0)) - apply_arrays(0, &old->arrays, 0, 0); -} - - -VertexArray::Array::Array(): - component(0), - offset(0) -{ } - VertexArray::Loader::Loader(VertexArray &a): VertexArrayBuilder(a) diff --git a/source/vertexarray.h b/source/vertexarray.h index efbf8ce2..95a6ec2f 100644 --- a/source/vertexarray.h +++ b/source/vertexarray.h @@ -5,7 +5,6 @@ #include #include #include -#include "bindable.h" #include "bufferable.h" #include "datatype.h" #include "primitivetype.h" @@ -27,7 +26,7 @@ VertexFormat::offset. A higher-level interface for filling in vertex data is available in the VertexArrayBuilder class. */ -class VertexArray: public Bindable, public Bufferable +class VertexArray: public Bufferable { public: class Loader: public DataFile::Loader, public VertexArrayBuilder @@ -37,33 +36,20 @@ public: }; private: - struct Array - { - unsigned char component; - unsigned char offset; - - Array(); - }; - VertexFormat format; std::vector data; unsigned stride; - std::vector arrays; VertexArray(const VertexArray &); VertexArray &operator=(const VertexArray &); public: VertexArray(const VertexFormat &); - ~VertexArray(); /// Resets the VertexArray to a different format. All data is cleared. void reset(const VertexFormat &); const VertexFormat &get_format() const { return format; } -private: - static unsigned get_array_slot(unsigned char); -public: /// Clears all vertices from the array. void clear(); @@ -83,17 +69,6 @@ public: unsigned size() const { return data.size()/stride; } const std::vector &get_data() const { return data; } const float *operator[](unsigned i) const { return &data[0]+i*stride; } - - /// Equivalent to apply(). For compatibility with the Bindable interface. - void bind() const { apply(); } - - /// Applies component arrays to the GL. - void apply() const; - -private: - static void apply_arrays(const std::vector *, const std::vector *, const float *, unsigned); -public: - static void unbind(); }; } // namespace GL diff --git a/source/vertexsetup.cpp b/source/vertexsetup.cpp index 53f82569..2a3b2f1a 100644 --- a/source/vertexsetup.cpp +++ b/source/vertexsetup.cpp @@ -130,6 +130,9 @@ void VertexSetup::bind() const if(set_current(this)) { + vertex_array->refresh(); + if(inst_array) + inst_array->refresh(); glBindVertexArray(id); if(dirty) { -- 2.43.0