1 #include <msp/gl/extensions/arb_vertex_array_object.h>
2 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
3 #include <msp/gl/extensions/arb_vertex_shader.h>
4 #include <msp/gl/extensions/khr_debug.h>
9 #include "resourcemanager.h"
10 #include "vertexsetup.h"
17 Mesh::Mesh(ResourceManager *rm)
22 Mesh::Mesh(const VertexFormat &f, ResourceManager *rm)
28 void Mesh::init(ResourceManager *rm)
33 disallow_rendering = false;
34 face_winding = NON_MANIFOLD;
48 void Mesh::storage(const VertexFormat &fmt)
50 if(!vertices.get_format().empty())
51 throw invalid_operation("Mesh::storage");
53 vertices.set_format(fmt);
54 vtx_setup.set_format(fmt);
63 void Mesh::check_buffers(unsigned mask)
65 if(mask&VERTEX_BUFFER)
67 unsigned req_size = vertices.get_required_buffer_size();
68 if(!vbuf || (vbuf->get_size()>0 && vbuf->get_size()<req_size))
72 vertices.use_buffer(vbuf);
73 vtx_setup.set_vertex_array(vertices);
74 dirty |= VERTEX_BUFFER;
77 if(!debug_name.empty())
78 vbuf->set_debug_name(debug_name+" [VBO]");
85 unsigned req_size = (batches.empty() ? 0 : batches.front().get_required_buffer_size());
86 if(!ibuf || (ibuf->get_size()>0 && ibuf->get_size()<req_size))
91 batches.front().change_buffer(ibuf);
92 dirty |= INDEX_BUFFER;
95 if(!debug_name.empty())
96 vbuf->set_debug_name(debug_name+" [IBO]");
101 vtx_setup.set_index_buffer(*ibuf, batches.front().get_index_type());
105 unsigned Mesh::get_n_vertices() const
107 return vertices.size();
110 char *Mesh::modify_vertex(unsigned i)
112 if(vertices.get_format().empty())
113 throw invalid_operation("Mesh::modify_vertex");
114 return vertices.modify(i);
117 void Mesh::add_batch(const Batch &b)
121 batches.push_back(b);
123 batches.back().use_buffer(ibuf);
125 else if(batches.back().can_append(b.get_type()))
126 batches.back().append(b);
129 bool reallocate = (batches.size()==batches.capacity());
132 for(auto i=batches.end(); i!=batches.begin(); )
133 (--i)->use_buffer(0);
136 Batch *prev = &batches.back();
137 batches.push_back(b);
141 for(Batch &a: batches)
143 a.use_buffer(ibuf, prev);
148 batches.back().use_buffer(ibuf, prev);
151 DataType existing_type = batches.front().get_index_type();
152 DataType added_type = batches.back().get_index_type();
153 if(existing_type!=added_type)
155 if(get_type_size(existing_type)>get_type_size(added_type))
156 batches.back().set_index_type(existing_type);
159 for(Batch &a: batches)
160 a.set_index_type(added_type);
164 check_buffers(INDEX_BUFFER);
167 void Mesh::set_winding(FaceWinding w)
172 void Mesh::draw(Renderer &renderer) const
174 draw(renderer, 0, 0);
177 void Mesh::draw_instanced(Renderer &renderer, const VertexSetup &vs, unsigned count) const
179 if(vs.get_vertex_array()!=&vertices)
180 throw invalid_argument("Mesh::draw_instanced");
182 draw(renderer, &vs, count);
185 void Mesh::draw(Renderer &renderer, const VertexSetup *vs, unsigned count) const
189 manager->resource_used(*this);
190 if(disallow_rendering)
200 renderer.set_vertex_setup(vs ? vs : &vtx_setup);
201 renderer.set_front_face(face_winding);
205 for(const Batch &b: batches)
210 for(const Batch &b: batches)
211 renderer.draw_instanced(b, count);
215 void Mesh::resize_buffers() const
217 if(dirty&VERTEX_BUFFER)
218 vbuf->storage(vertices.get_required_buffer_size());
219 if((dirty&INDEX_BUFFER) && !batches.empty())
220 ibuf->storage(batches.front().get_required_buffer_size());
224 Resource::AsyncLoader *Mesh::load(IO::Seekable &io, const Resources *)
226 return new AsyncLoader(*this, io);
229 UInt64 Mesh::get_data_size() const
233 size += vbuf->get_size();
235 size += ibuf->get_size();
242 vertices.use_buffer(0);
251 void Mesh::set_debug_name(const string &name)
256 vbuf->set_debug_name(name+" [VBO]");
258 ibuf->set_debug_name(name+" [IBO]");
259 vtx_setup.set_debug_name(name+" [VAO]");
266 Mesh::Loader::Loader(Mesh &m, bool g):
267 DataFile::ObjectLoader<Mesh>(m),
270 add("batch", &Loader::batch);
271 add("storage", &Loader::storage);
272 add("vertices", &Loader::vertices);
273 add("vertices", &Loader::vertices_with_format);
274 add("winding", &Mesh::face_winding);
277 void Mesh::Loader::storage(const vector<VertexAttribute> &attrs)
280 throw invalid_argument("No vertex attributes");
283 for(VertexAttribute a: attrs)
288 void Mesh::Loader::vertices()
290 load_sub(obj.vertices);
292 obj.check_buffers(VERTEX_BUFFER);
295 void Mesh::Loader::vertices_with_format(const vector<VertexAttribute> &a)
301 void Mesh::Loader::batch(PrimitiveType p)
309 Mesh::AsyncLoader::AsyncLoader(Mesh &m, IO::Seekable &i):
316 mesh.disallow_rendering = true;
317 mesh.check_buffers(VERTEX_BUFFER|INDEX_BUFFER);
320 Mesh::AsyncLoader::~AsyncLoader()
322 mesh.disallow_rendering = false;
323 delete vertex_updater;
324 delete index_updater;
327 bool Mesh::AsyncLoader::needs_sync() const
332 bool Mesh::AsyncLoader::process()
336 // TODO use correct filename
337 DataFile::Parser parser(io, "async");
338 Loader loader(mesh, false);
343 mesh.resize_buffers();
344 vertex_updater = mesh.vertices.refresh_async();
345 if(!mesh.batches.empty())
346 index_updater = mesh.batches.front().refresh_async();
351 vertex_updater->upload_data();
353 index_updater->upload_data();
357 delete vertex_updater;
359 delete index_updater;
364 if(phase==1 && !mesh.vbuf && !mesh.ibuf)