]> git.tdb.fi Git - libs/gl.git/commitdiff
Enable asynchronous loading of Meshes
authorMikko Rasa <tdb@tdb.fi>
Tue, 23 Sep 2014 21:08:21 +0000 (00:08 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 23 Sep 2014 21:09:59 +0000 (00:09 +0300)
This currently causes object bounding spheres to be incorrect when using
managed meshes.  The situation will be rectified shortly.

source/mesh.cpp
source/mesh.h
source/resources.cpp
source/resources.h

index 150822ce85e1771a3826332cb19c12922c15a044..055345faed1f17586de0c9c93d204f4c7956e985 100644 (file)
@@ -1,34 +1,41 @@
 #include <msp/gl/extensions/arb_vertex_array_object.h>
 #include <msp/gl/extensions/arb_vertex_shader.h>
+#include <msp/gl/extensions/nv_primitive_restart.h>
 #include "buffer.h"
 #include "error.h"
 #include "mesh.h"
 #include "renderer.h"
+#include "resourcemanager.h"
 
 using namespace std;
 
 namespace Msp {
 namespace GL {
 
-Mesh::Mesh():
-       vertices(VERTEX3),
-       vbuf(0),
-       ibuf(0),
-       vao_id(0),
-       defer_buffers(true),
-       dirty(true),
-       winding(0)
-{ }
-
-Mesh::Mesh(const VertexFormat &f):
-       vertices(f),
-       vbuf(0),
-       ibuf(0),
-       vao_id(0),
-       defer_buffers(true),
-       dirty(true),
-       winding(0)
-{ }
+Mesh::Mesh(ResourceManager *rm):
+       vertices(VERTEX3)
+{
+       init(rm);
+}
+
+Mesh::Mesh(const VertexFormat &f, ResourceManager *rm):
+       vertices(f)
+{
+       init(rm);
+}
+
+void Mesh::init(ResourceManager *rm)
+{
+       vbuf = 0;
+       ibuf = 0;
+       vao_id = 0;
+       defer_buffers = true;
+       dirty = true;
+       winding = 0;
+
+       if(rm)
+               set_manager(rm);
+}
 
 Mesh::~Mesh()
 {
@@ -136,6 +143,13 @@ void Mesh::draw() const
        if(cur && cur!=this)
                throw invalid_operation("Mesh::draw");
 
+       if(manager)
+       {
+               manager->resource_used(*this);
+               if(disallow_rendering)
+                       return;
+       }
+
        if(!current())
                vertices.apply();
        Bind bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
@@ -147,6 +161,13 @@ void Mesh::draw() const
 
 void Mesh::draw(Renderer &renderer) const
 {
+       if(manager)
+       {
+               manager->resource_used(*this);
+               if(disallow_rendering)
+                       return;
+       }
+
        renderer.set_mesh(this);
        renderer.set_element_buffer(ibuf);
        renderer.set_winding_test(winding);
@@ -179,6 +200,33 @@ void Mesh::unbind()
                glBindVertexArray(0);
 }
 
+Resource::AsyncLoader *Mesh::load(IO::Seekable &io)
+{
+       return new AsyncLoader(*this, io);
+}
+
+UInt64 Mesh::get_data_size() const
+{
+       UInt64 size = 0;
+       if(vbuf)
+               size += vbuf->get_size();
+       if(ibuf)
+               size += ibuf->get_size();
+       return size;
+}
+
+void Mesh::unload()
+{
+       vertices.clear();
+       vertices.use_buffer(0);
+       batches.clear();
+       delete vbuf;
+       delete ibuf;
+       defer_buffers = (vbuf || ibuf);
+       vbuf = 0;
+       ibuf = 0;
+}
+
 
 Mesh::Loader::Loader(Mesh &m):
        DataFile::ObjectLoader<Mesh>(m)
@@ -216,5 +264,65 @@ void Mesh::Loader::winding(FaceWinding w)
                obj.winding = &WindingTest::counterclockwise();
 }
 
+
+Mesh::AsyncLoader::AsyncLoader(Mesh &m, IO::Seekable &i):
+       mesh(m),
+       io(i),
+       vertex_updater(0),
+       index_updater(0),
+       phase(0)
+{
+       // Make sure the extension is initialized in the rendering thread.
+       (bool)NV_primitive_restart;
+
+       mesh.disallow_rendering = true;
+       if(mesh.defer_buffers)
+               mesh.create_buffers();
+}
+
+Mesh::AsyncLoader::~AsyncLoader()
+{
+       mesh.disallow_rendering = false;
+}
+
+bool Mesh::AsyncLoader::needs_sync() const
+{
+       return phase%2;
+}
+
+bool Mesh::AsyncLoader::process()
+{
+       if(phase==0)
+       {
+               // TODO use correct filename
+               DataFile::Parser parser(io, "async");
+               Loader loader(mesh);
+               loader.load(parser);
+       }
+       else if(phase==1)
+       {
+               vertex_updater = mesh.vertices.refresh_async();
+               if(!mesh.batches.empty())
+                       index_updater = mesh.batches.front().refresh_async();
+       }
+       else if(phase==2)
+       {
+               if(vertex_updater)
+                       vertex_updater->upload_data();
+               if(index_updater)
+                       index_updater->upload_data();
+       }
+       else if(phase==3)
+       {
+               delete vertex_updater;
+               delete index_updater;
+       }
+
+       ++phase;
+       if(phase==1 && !mesh.vbuf && !mesh.ibuf)
+               phase += 3;
+       return phase>3;
+}
+
 } // namespace GL
 } // namespace Msp
index 730b724c9e81a9eee6072cac1d4762b503dbd6fc..14cb072dda26891dafb4fcf402e4055358f1a48b 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <msp/datafile/objectloader.h>
 #include "batch.h"
+#include "resource.h"
 #include "vertexarray.h"
 #include "windingtest.h"
 
@@ -17,7 +18,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>
+class Mesh: public Bindable<Mesh>, public Resource
 {
        friend class MeshBuilder;
 
@@ -33,6 +34,23 @@ public:
        };
 
 private:
+       class AsyncLoader: public Resource::AsyncLoader
+       {
+       private:
+               Mesh &mesh;
+               IO::Seekable &io;
+               Bufferable::AsyncUpdater *vertex_updater;
+               Bufferable::AsyncUpdater *index_updater;
+               unsigned phase;
+
+       public:
+               AsyncLoader(Mesh &, IO::Seekable &);
+               ~AsyncLoader();
+
+               virtual bool needs_sync() const;
+               virtual bool process();
+       };
+
        VertexArray vertices;
        std::list<Batch> batches;
        Buffer *vbuf;
@@ -40,11 +58,15 @@ private:
        unsigned vao_id;
        bool defer_buffers;
        mutable bool dirty;
+       bool disallow_rendering;
        const WindingTest *winding;
 
 public:
-       Mesh();
-       Mesh(const VertexFormat &f);
+       Mesh(ResourceManager * = 0);
+       Mesh(const VertexFormat &, ResourceManager * = 0);
+private:
+       void init(ResourceManager *);
+public:
        ~Mesh();
 
        void clear();
@@ -70,6 +92,10 @@ public:
        void bind() const;
 
        static void unbind();
+
+       virtual Resource::AsyncLoader *load(IO::Seekable &);
+       virtual UInt64 get_data_size() const;
+       virtual void unload();
 };
 
 } // namespace GL
index 79c5e85fbe47764e0cca300f15c8acb3ff350450..2fd6950370df6580fe08cdf54139bef9b6d50316 100644 (file)
@@ -29,7 +29,7 @@ Resources::Resources():
        add_type<Font>().keyword("font");
        add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
        add_type<Material>().suffix(".mat").keyword("material");
-       add_type<Mesh>().keyword("mesh");
+       add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
        add_type<Object>().keyword("object");
        add_type<Pose>().keyword("pose");
        add_type<Program>().keyword("shader");
@@ -53,6 +53,21 @@ void Resources::set_resource_manager(ResourceManager *m)
        resource_manager = m;
 }
 
+Mesh *Resources::create_mesh(const string &name)
+{
+       if(!resource_manager)
+               return 0;
+
+       if(RefPtr<IO::Seekable> io = open_from_sources(name))
+       {
+               RefPtr<GL::Mesh> mesh = new GL::Mesh(resource_manager);
+               resource_manager->set_resource_location(*mesh, *this, name);
+               return mesh.release();
+       }
+
+       return 0;
+}
+
 Texture2D *Resources::create_texture2d(const string &name)
 {
        string ext = FS::extpart(name);
index e2503298c5ce59fe0ba04570700970abeb9d881a..e939f7cef8b9b9238fb3c2c28350c9e391832d04 100644 (file)
@@ -7,6 +7,7 @@
 namespace Msp {
 namespace GL {
 
+class Mesh;
 class ResourceManager;
 class Texture2D;
 
@@ -35,6 +36,7 @@ public:
        void set_resource_manager(ResourceManager *);
 
 protected:
+       Mesh *create_mesh(const std::string &);
        Texture2D *create_texture2d(const std::string &);
 };