5 #include "resourcemanager.h"
12 Mesh::Mesh(const VertexFormat &f)
17 Mesh::Mesh(Mesh &&other):
18 Resource(move(other)),
19 vertices(move(other.vertices)),
20 batches(move(other.batches)),
23 vtx_setup(move(other.vtx_setup)),
25 disallow_rendering(other.disallow_rendering),
26 face_winding(other.face_winding),
27 debug_name(move(other.debug_name))
41 void Mesh::storage(const VertexFormat &fmt)
43 if(!vertices.get_format().empty())
44 throw invalid_operation("Mesh::storage");
46 vertices.set_format(fmt);
47 vtx_setup.set_format(fmt);
49 vtx_setup.set_vertex_array(vertices);
58 void Mesh::check_buffers(unsigned mask)
60 if(mask&VERTEX_BUFFER)
62 unsigned req_size = vertices.get_required_buffer_size();
63 if(!vbuf || (vbuf->get_size()>0 && vbuf->get_size()<req_size))
67 vertices.use_buffer(vbuf);
68 if(!vertices.get_format().empty())
69 vtx_setup.set_vertex_array(vertices);
70 dirty |= VERTEX_BUFFER;
73 if(!debug_name.empty())
74 vbuf->set_debug_name(debug_name+" [VBO]");
81 unsigned req_size = (batches.empty() ? 0 : batches.front().get_required_buffer_size());
82 if(!ibuf || (ibuf->get_size()>0 && ibuf->get_size()<req_size))
87 batches.front().change_buffer(ibuf);
88 dirty |= INDEX_BUFFER;
91 if(!debug_name.empty())
92 vbuf->set_debug_name(debug_name+" [IBO]");
97 vtx_setup.set_index_buffer(*ibuf, batches.front().get_index_type());
101 size_t Mesh::get_n_vertices() const
103 return vertices.size();
106 char *Mesh::modify_vertex(size_t i)
108 if(vertices.get_format().empty())
109 throw invalid_operation("Mesh::modify_vertex");
110 return vertices.modify(i);
113 void Mesh::add_batch(Batch &&b)
117 batches.emplace_back(move(b));
119 batches.back().use_buffer(ibuf);
121 else if(batches.back().can_append(b.get_type()))
122 batches.back().append(b);
125 bool reallocate = (batches.size()==batches.capacity());
128 for(auto i=batches.end(); i!=batches.begin(); )
129 (--i)->use_buffer(0);
132 Batch *prev = &batches.back();
133 batches.emplace_back(move(b));
137 for(Batch &a: batches)
139 a.use_buffer(ibuf, prev);
144 batches.back().use_buffer(ibuf, prev);
147 DataType existing_type = batches.front().get_index_type();
148 DataType added_type = batches.back().get_index_type();
149 if(existing_type!=added_type)
151 if(get_type_size(existing_type)>get_type_size(added_type))
152 batches.back().set_index_type(existing_type);
155 for(Batch &a: batches)
156 a.set_index_type(added_type);
160 check_buffers(INDEX_BUFFER);
163 void Mesh::set_winding(FaceWinding w)
168 void Mesh::draw(Renderer &renderer) const
170 draw(renderer, 0, 0);
173 void Mesh::draw_instanced(Renderer &renderer, const VertexSetup &vs, unsigned count) const
175 if(vs.get_vertex_array()!=&vertices)
176 throw invalid_argument("Mesh::draw_instanced");
178 draw(renderer, &vs, count);
181 void Mesh::draw(Renderer &renderer, const VertexSetup *vs, unsigned count) const
185 manager->resource_used(*this);
186 if(disallow_rendering)
196 renderer.set_vertex_setup(vs ? vs : &vtx_setup);
197 renderer.set_front_face(face_winding);
201 for(const Batch &b: batches)
206 for(const Batch &b: batches)
207 renderer.draw_instanced(b, count);
211 void Mesh::resize_buffers() const
213 if(dirty&VERTEX_BUFFER)
214 vbuf->storage(vertices.get_required_buffer_size(), STATIC);
215 if((dirty&INDEX_BUFFER) && !batches.empty())
216 ibuf->storage(batches.front().get_required_buffer_size(), STATIC);
220 Resource::AsyncLoader *Mesh::load(IO::Seekable &io, const Resources *)
222 return new AsyncLoader(*this, io);
225 uint64_t Mesh::get_data_size() const
229 size += vbuf->get_size();
231 size += ibuf->get_size();
238 vertices.use_buffer(0);
247 void Mesh::set_debug_name(const string &name)
252 vbuf->set_debug_name(name+" [VBO]");
254 ibuf->set_debug_name(name+" [IBO]");
255 vtx_setup.set_debug_name(name+" [VAO]");
262 Mesh::Loader::Loader(Mesh &m):
263 DataFile::ObjectLoader<Mesh>(m)
265 add("batch", &Loader::batch);
266 add("storage", &Loader::storage);
267 add("vertices", &Loader::vertices);
268 add("vertices", &Loader::vertices_with_format);
269 add("winding", &Mesh::face_winding);
272 void Mesh::Loader::storage(const vector<VertexAttribute> &attrs)
275 throw invalid_argument("No vertex attributes");
278 for(VertexAttribute a: attrs)
283 void Mesh::Loader::vertices()
285 load_sub(obj.vertices);
287 obj.check_buffers(VERTEX_BUFFER);
290 void Mesh::Loader::vertices_with_format(const vector<VertexAttribute> &a)
296 void Mesh::Loader::batch(PrimitiveType p)
300 obj.add_batch(move(btc));
304 Mesh::AsyncLoader::AsyncLoader(Mesh &m, IO::Seekable &i):
308 mesh.disallow_rendering = true;
309 mesh.check_buffers(VERTEX_BUFFER|INDEX_BUFFER);
312 Mesh::AsyncLoader::~AsyncLoader()
314 mesh.disallow_rendering = false;
315 delete vertex_updater;
316 delete index_updater;
319 bool Mesh::AsyncLoader::needs_sync() const
324 bool Mesh::AsyncLoader::process()
328 // TODO use correct filename
329 DataFile::Parser parser(io, "async");
331 loader.allow_gl_calls = false;
336 mesh.resize_buffers();
337 vertex_updater = mesh.vertices.refresh_async();
338 if(!mesh.batches.empty())
339 index_updater = mesh.batches.front().refresh_async();
344 vertex_updater->upload_data();
346 index_updater->upload_data();
350 delete vertex_updater;
352 delete index_updater;
357 if(phase==1 && !mesh.vbuf && !mesh.ibuf)