5 #include "resourcemanager.h"
12 Mesh::Mesh(ResourceManager *rm):
16 disallow_rendering(false),
17 face_winding(NON_MANIFOLD)
23 Mesh::Mesh(const VertexFormat &f, ResourceManager *rm):
37 void Mesh::storage(const VertexFormat &fmt)
39 if(!vertices.get_format().empty())
40 throw invalid_operation("Mesh::storage");
42 vertices.set_format(fmt);
43 vtx_setup.set_format(fmt);
52 void Mesh::check_buffers(unsigned mask)
54 if(mask&VERTEX_BUFFER)
56 unsigned req_size = vertices.get_required_buffer_size();
57 if(!vbuf || (vbuf->get_size()>0 && vbuf->get_size()<req_size))
61 vertices.use_buffer(vbuf);
62 vtx_setup.set_vertex_array(vertices);
63 dirty |= VERTEX_BUFFER;
66 if(!debug_name.empty())
67 vbuf->set_debug_name(debug_name+" [VBO]");
74 unsigned req_size = (batches.empty() ? 0 : batches.front().get_required_buffer_size());
75 if(!ibuf || (ibuf->get_size()>0 && ibuf->get_size()<req_size))
80 batches.front().change_buffer(ibuf);
81 dirty |= INDEX_BUFFER;
84 if(!debug_name.empty())
85 vbuf->set_debug_name(debug_name+" [IBO]");
90 vtx_setup.set_index_buffer(*ibuf, batches.front().get_index_type());
94 unsigned Mesh::get_n_vertices() const
96 return vertices.size();
99 char *Mesh::modify_vertex(unsigned i)
101 if(vertices.get_format().empty())
102 throw invalid_operation("Mesh::modify_vertex");
103 return vertices.modify(i);
106 void Mesh::add_batch(const Batch &b)
110 batches.push_back(b);
112 batches.back().use_buffer(ibuf);
114 else if(batches.back().can_append(b.get_type()))
115 batches.back().append(b);
118 bool reallocate = (batches.size()==batches.capacity());
121 for(auto i=batches.end(); i!=batches.begin(); )
122 (--i)->use_buffer(0);
125 Batch *prev = &batches.back();
126 batches.push_back(b);
130 for(Batch &a: batches)
132 a.use_buffer(ibuf, prev);
137 batches.back().use_buffer(ibuf, prev);
140 DataType existing_type = batches.front().get_index_type();
141 DataType added_type = batches.back().get_index_type();
142 if(existing_type!=added_type)
144 if(get_type_size(existing_type)>get_type_size(added_type))
145 batches.back().set_index_type(existing_type);
148 for(Batch &a: batches)
149 a.set_index_type(added_type);
153 check_buffers(INDEX_BUFFER);
156 void Mesh::set_winding(FaceWinding w)
161 void Mesh::draw(Renderer &renderer) const
163 draw(renderer, 0, 0);
166 void Mesh::draw_instanced(Renderer &renderer, const VertexSetup &vs, unsigned count) const
168 if(vs.get_vertex_array()!=&vertices)
169 throw invalid_argument("Mesh::draw_instanced");
171 draw(renderer, &vs, count);
174 void Mesh::draw(Renderer &renderer, const VertexSetup *vs, unsigned count) const
178 manager->resource_used(*this);
179 if(disallow_rendering)
189 renderer.set_vertex_setup(vs ? vs : &vtx_setup);
190 renderer.set_front_face(face_winding);
194 for(const Batch &b: batches)
199 for(const Batch &b: batches)
200 renderer.draw_instanced(b, count);
204 void Mesh::resize_buffers() const
206 if(dirty&VERTEX_BUFFER)
207 vbuf->storage(vertices.get_required_buffer_size());
208 if((dirty&INDEX_BUFFER) && !batches.empty())
209 ibuf->storage(batches.front().get_required_buffer_size());
213 Resource::AsyncLoader *Mesh::load(IO::Seekable &io, const Resources *)
215 return new AsyncLoader(*this, io);
218 uint64_t Mesh::get_data_size() const
222 size += vbuf->get_size();
224 size += ibuf->get_size();
231 vertices.use_buffer(0);
240 void Mesh::set_debug_name(const string &name)
245 vbuf->set_debug_name(name+" [VBO]");
247 ibuf->set_debug_name(name+" [IBO]");
248 vtx_setup.set_debug_name(name+" [VAO]");
255 Mesh::Loader::Loader(Mesh &m, bool g):
256 DataFile::ObjectLoader<Mesh>(m),
259 add("batch", &Loader::batch);
260 add("storage", &Loader::storage);
261 add("vertices", &Loader::vertices);
262 add("vertices", &Loader::vertices_with_format);
263 add("winding", &Mesh::face_winding);
266 void Mesh::Loader::storage(const vector<VertexAttribute> &attrs)
269 throw invalid_argument("No vertex attributes");
272 for(VertexAttribute a: attrs)
277 void Mesh::Loader::vertices()
279 load_sub(obj.vertices);
281 obj.check_buffers(VERTEX_BUFFER);
284 void Mesh::Loader::vertices_with_format(const vector<VertexAttribute> &a)
290 void Mesh::Loader::batch(PrimitiveType p)
298 Mesh::AsyncLoader::AsyncLoader(Mesh &m, IO::Seekable &i):
305 mesh.disallow_rendering = true;
306 mesh.check_buffers(VERTEX_BUFFER|INDEX_BUFFER);
309 Mesh::AsyncLoader::~AsyncLoader()
311 mesh.disallow_rendering = false;
312 delete vertex_updater;
313 delete index_updater;
316 bool Mesh::AsyncLoader::needs_sync() const
321 bool Mesh::AsyncLoader::process()
325 // TODO use correct filename
326 DataFile::Parser parser(io, "async");
327 Loader loader(mesh, false);
332 mesh.resize_buffers();
333 vertex_updater = mesh.vertices.refresh_async();
334 if(!mesh.batches.empty())
335 index_updater = mesh.batches.front().refresh_async();
340 vertex_updater->upload_data();
342 index_updater->upload_data();
346 delete vertex_updater;
348 delete index_updater;
353 if(phase==1 && !mesh.vbuf && !mesh.ibuf)