#include <msp/gl/extensions/arb_vertex_array_object.h>
+#include <msp/gl/extensions/arb_vertex_buffer_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(0),
- winding(0)
-{ }
-
-Mesh::Mesh(const VertexFormat &f):
- vertices(f),
- vbuf(0),
- ibuf(0),
- vao_id(0),
- defer_buffers(true),
- dirty(2),
- 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 = ARB_vertex_buffer_object;
+ dirty = true;
+ disallow_rendering = false;
+ winding = 0;
+
+ if(rm)
+ set_manager(rm);
+}
Mesh::~Mesh()
{
+ set_manager(0);
delete vbuf;
delete ibuf;
if(vao_id)
glGenVertexArrays(1, &vao_id);
}
-void Mesh::refresh() const
+void Mesh::setup_vao() const
{
- vertices.refresh();
- if(dirty)
+ 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(dirty&1)
- {
- for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
- i->refresh();
- }
-
- if(dirty&2)
- {
- Bind bind_vbuf(vbuf, ARRAY_BUFFER);
-
- bool bind_here = !current();
- if(bind_here)
- glBindVertexArray(vao_id);
-
- 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)
- {
- 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, ibuf->get_id());
-
- if(bind_here)
- glBindVertexArray(0);
- }
-
- dirty = 0;
+ 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, ibuf->get_id());
+
+ dirty = false;
}
unsigned Mesh::get_n_vertices() const
if(defer_buffers)
create_buffers();
- dirty |= 1;
if(!batches.empty() && batches.back().can_append(b.get_type()))
batches.back().append(b);
else
if(cur && cur!=this)
throw invalid_operation("Mesh::draw");
- refresh();
+ if(manager)
+ {
+ manager->resource_used(*this);
+ if(disallow_rendering)
+ return;
+ }
if(!current())
vertices.apply();
- Bind bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
+ BindRestore bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
Bind bind_winding(winding);
for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
void Mesh::draw(Renderer &renderer) const
{
- refresh();
+ if(manager)
+ {
+ manager->resource_used(*this);
+ if(disallow_rendering)
+ return;
+ }
renderer.set_mesh(this);
- renderer.set_element_buffer(ibuf);
renderer.set_winding_test(winding);
for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
/* If VAOs are not supported, vao_id is zero and set_current won't get
called. Thus unbind won't try to call a null function either. */
if(!vao_id)
+ {
unbind();
+ vertices.apply(false);
+ }
else if(set_current(this))
+ {
glBindVertexArray(vao_id);
+ vertices.refresh();
+ if(dirty)
+ setup_vao();
+ }
}
void Mesh::unbind()
glBindVertexArray(0);
}
+Resource::AsyncLoader *Mesh::load(IO::Seekable &io, const Resources *)
+{
+ 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)
for(vector<VertexComponent>::const_iterator i=c.begin(); i!=c.end(); ++i)
fmt = (fmt, *i);
obj.vertices.reset(fmt);
- obj.dirty |= 2;
+ obj.dirty = true;
load_sub(obj.vertices);
}
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;
+ delete vertex_updater;
+ delete index_updater;
+}
+
+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;
+ vertex_updater = 0;
+ delete index_updater;
+ index_updater = 0;
+ }
+
+ ++phase;
+ if(phase==1 && !mesh.vbuf && !mesh.ibuf)
+ phase += 3;
+ return phase>3;
+}
+
} // namespace GL
} // namespace Msp