#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()
{
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);
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);
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)
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
#include <msp/datafile/objectloader.h>
#include "batch.h"
+#include "resource.h"
#include "vertexarray.h"
#include "windingtest.h"
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;
};
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;
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();
void bind() const;
static void unbind();
+
+ virtual Resource::AsyncLoader *load(IO::Seekable &);
+ virtual UInt64 get_data_size() const;
+ virtual void unload();
};
} // namespace GL
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");
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);