]> git.tdb.fi Git - libs/gl.git/commitdiff
Remove bind mechanics from Mesh and VertexArray
authorMikko Rasa <tdb@tdb.fi>
Tue, 29 Oct 2019 00:25:16 +0000 (02:25 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 29 Oct 2019 22:03:21 +0000 (00:03 +0200)
It's now handled through VertexSetup.

source/mesh.cpp
source/mesh.h
source/renderer.cpp
source/renderer.h
source/vertexarray.cpp
source/vertexarray.h
source/vertexsetup.cpp

index 4b247e8d4f759aa6f68287590207076ff0dbf5cc..c6543f01cee322bbaa465ea492b2cd15e800bb66 100644 (file)
@@ -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<Batch>::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);
index 2de418ae077bff1a9f43621d9051057fd81893e0..b4ba0c45151534fab16a0023b05c00cce636d5da 100644 (file)
@@ -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<Mesh>, 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;
index 302d943727a2812e66bd6b85f748f398117bd4d9..62e5ced05aa90c254ba4149546091295615f541a 100644 (file)
@@ -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),
index 4899b4f330d8bb173898ff73dfc061a4d9471688..dd732bd8d491150375c929ea065c225b654f6ff0 100644 (file)
@@ -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);
index 9947ac1108a59ef5b5a9a72d9caaa7987ba11782..e3aef7a0eea16cd38302d068535edaa255c56028 100644 (file)
@@ -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<float *>(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<Array> *arrays, const vector<Array> *old_arrays, const float *base, unsigned stride_bytes)
-{
-       unsigned n_arrays = arrays ? arrays->size() : 0;
-       if(old_arrays)
-               n_arrays = max<unsigned>(n_arrays, old_arrays->size());
-       for(unsigned i=0; i<n_arrays; ++i)
-       {
-               const Array *arr = ((arrays && i<arrays->size() && (*arrays)[i].component) ? &(*arrays)[i] : 0);
-               const Array *old_arr = ((old_arrays && i<old_arrays->size() && (*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)
index efbf8ce27cb2f4ef25c74c5b14c49ce1b2c2dd02..95a6ec2f3aa526ac11165b4f24e77e5d30d67b10 100644 (file)
@@ -5,7 +5,6 @@
 #include <vector>
 #include <msp/core/refptr.h>
 #include <msp/datafile/loader.h>
-#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<VertexArray>, 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<float> data;
        unsigned stride;
-       std::vector<Array> 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<float> &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<Array> *, const std::vector<Array> *, const float *, unsigned);
-public:
-       static void unbind();
 };
 
 } // namespace GL
index 53f825697b34b16444e783cfb32262361d10fc4f..2a3b2f1a7d920aa5a5a1200dd4df646ca1d195c6 100644 (file)
@@ -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)
                {