]> git.tdb.fi Git - libs/gl.git/blob - source/core/mesh.cpp
Annotate uniforms with descriptor set layout qualifiers
[libs/gl.git] / source / core / mesh.cpp
1 #include "buffer.h"
2 #include "error.h"
3 #include "mesh.h"
4 #include "renderer.h"
5 #include "resourcemanager.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 Mesh::Mesh(const VertexFormat &f)
13 {
14         storage(f);
15 }
16
17 Mesh::Mesh(Mesh &&other):
18         Resource(move(other)),
19         vertices(move(other.vertices)),
20         batches(move(other.batches)),
21         vbuf(other.vbuf),
22         ibuf(other.ibuf),
23         vtx_setup(move(other.vtx_setup)),
24         dirty(other.dirty),
25         disallow_rendering(other.disallow_rendering),
26         face_winding(other.face_winding),
27         debug_name(move(other.debug_name))
28 {
29         other.vbuf = 0;
30         other.ibuf = 0;
31 }
32
33 Mesh::~Mesh()
34 {
35         set_manager(0);
36         batches.clear();
37         delete vbuf;
38         delete ibuf;
39 }
40
41 void Mesh::storage(const VertexFormat &fmt)
42 {
43         if(!vertices.get_format().empty())
44                 throw invalid_operation("Mesh::storage");
45
46         vertices.set_format(fmt);
47         vtx_setup.set_format(fmt);
48         if(vbuf)
49                 vtx_setup.set_vertex_array(vertices);
50 }
51
52 void Mesh::clear()
53 {
54         vertices.clear();
55         batches.clear();
56 }
57
58 void Mesh::check_buffers(unsigned mask)
59 {
60         if(mask&VERTEX_BUFFER)
61         {
62                 unsigned req_size = vertices.get_required_buffer_size();
63                 if(!vbuf || (vbuf->get_size()>0 && vbuf->get_size()<req_size))
64                 {
65                         delete vbuf;
66                         vbuf = new Buffer;
67                         vertices.use_buffer(vbuf);
68                         if(!vertices.get_format().empty())
69                                 vtx_setup.set_vertex_array(vertices);
70                         dirty |= VERTEX_BUFFER;
71
72 #ifdef DEBUG
73                         if(!debug_name.empty())
74                                 vbuf->set_debug_name(debug_name+" [VBO]");
75 #endif
76                 }
77         }
78
79         if(mask&INDEX_BUFFER)
80         {
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))
83                 {
84                         delete ibuf;
85                         ibuf = new Buffer;
86                         if(!batches.empty())
87                                 batches.front().change_buffer(ibuf);
88                         dirty |= INDEX_BUFFER;
89
90 #ifdef DEBUG
91                         if(!debug_name.empty())
92                                 vbuf->set_debug_name(debug_name+" [IBO]");
93 #endif
94                 }
95
96                 if(!batches.empty())
97                         vtx_setup.set_index_buffer(*ibuf, batches.front().get_index_type());
98         }
99 }
100
101 size_t Mesh::get_n_vertices() const
102 {
103         return vertices.size();
104 }
105
106 char *Mesh::modify_vertex(size_t i)
107 {
108         if(vertices.get_format().empty())
109                 throw invalid_operation("Mesh::modify_vertex");
110         return vertices.modify(i);
111 }
112
113 void Mesh::add_batch(Batch &&b)
114 {
115         if(batches.empty())
116         {
117                 batches.emplace_back(move(b));
118                 if(ibuf)
119                         batches.back().use_buffer(ibuf);
120         }
121         else if(batches.back().can_append(b.get_type()))
122                 batches.back().append(b);
123         else
124         {
125                 bool reallocate = (batches.size()==batches.capacity());
126                 if(reallocate)
127                 {
128                         for(auto i=batches.end(); i!=batches.begin(); )
129                                 (--i)->use_buffer(0);
130                 }
131
132                 Batch *prev = &batches.back();
133                 batches.emplace_back(move(b));
134                 if(reallocate)
135                 {
136                         prev = 0;
137                         for(Batch &a: batches)
138                         {
139                                 a.use_buffer(ibuf, prev);
140                                 prev = &a;
141                         }
142                 }
143                 else
144                         batches.back().use_buffer(ibuf, prev);
145         }
146
147         DataType existing_type = batches.front().get_index_type();
148         DataType added_type = batches.back().get_index_type();
149         if(existing_type!=added_type)
150         {
151                 if(get_type_size(existing_type)>get_type_size(added_type))
152                         batches.back().set_index_type(existing_type);
153                 else
154                 {
155                         for(Batch &a: batches)
156                                 a.set_index_type(added_type);
157                 }
158         }
159
160         check_buffers(INDEX_BUFFER);
161 }
162
163 void Mesh::set_winding(FaceWinding w)
164 {
165         face_winding = w;
166 }
167
168 void Mesh::draw(Renderer &renderer) const
169 {
170         draw(renderer, 0, 0);
171 }
172
173 void Mesh::draw_instanced(Renderer &renderer, const VertexSetup &vs, unsigned count) const
174 {
175         if(vs.get_vertex_array()!=&vertices)
176                 throw invalid_argument("Mesh::draw_instanced");
177
178         draw(renderer, &vs, count);
179 }
180
181 void Mesh::draw(Renderer &renderer, const VertexSetup *vs, unsigned count) const
182 {
183         if(manager)
184         {
185                 manager->resource_used(*this);
186                 if(disallow_rendering)
187                         return;
188         }
189
190         if(batches.empty())
191                 return;
192
193         if(dirty)
194                 resize_buffers();
195
196         renderer.set_vertex_setup(vs ? vs : &vtx_setup);
197         renderer.set_front_face(face_winding);
198
199         if(!count)
200         {
201                 for(const Batch &b: batches)
202                         renderer.draw(b);
203         }
204         else
205         {
206                 for(const Batch &b: batches)
207                         renderer.draw_instanced(b, count);
208         }
209 }
210
211 void Mesh::resize_buffers() const
212 {
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);
217         dirty = 0;
218 }
219
220 Resource::AsyncLoader *Mesh::load(IO::Seekable &io, const Resources *)
221 {
222         return new AsyncLoader(*this, io);
223 }
224
225 uint64_t Mesh::get_data_size() const
226 {
227         uint64_t size = 0;
228         if(vbuf)
229                 size += vbuf->get_size();
230         if(ibuf)
231                 size += ibuf->get_size();
232         return size;
233 }
234
235 void Mesh::unload()
236 {
237         vertices.clear();
238         vertices.use_buffer(0);
239         batches.clear();
240         vtx_setup.unload();
241         delete vbuf;
242         delete ibuf;
243         vbuf = 0;
244         ibuf = 0;
245 }
246
247 void Mesh::set_debug_name(const string &name)
248 {
249 #ifdef DEBUG
250         debug_name = name;
251         if(vbuf)
252                 vbuf->set_debug_name(name+" [VBO]");
253         if(ibuf)
254                 ibuf->set_debug_name(name+" [IBO]");
255         vtx_setup.set_debug_name(name+" [VAO]");
256 #else
257         (void)name;
258 #endif
259 }
260
261
262 Mesh::Loader::Loader(Mesh &m):
263         DataFile::ObjectLoader<Mesh>(m)
264 {
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);
270 }
271
272 void Mesh::Loader::storage(const vector<VertexAttribute> &attrs)
273 {
274         if(attrs.empty())
275                 throw invalid_argument("No vertex attributes");
276
277         VertexFormat fmt;
278         for(VertexAttribute a: attrs)
279                 fmt = (fmt, a);
280         obj.storage(fmt);
281 }
282
283 void Mesh::Loader::vertices()
284 {
285         load_sub(obj.vertices);
286         if(allow_gl_calls)
287                 obj.check_buffers(VERTEX_BUFFER);
288 }
289
290 void Mesh::Loader::vertices_with_format(const vector<VertexAttribute> &a)
291 {
292         storage(a);
293         vertices();
294 }
295
296 void Mesh::Loader::batch(PrimitiveType p)
297 {
298         Batch btc(p);
299         load_sub(btc);
300         obj.add_batch(move(btc));
301 }
302
303
304 Mesh::AsyncLoader::AsyncLoader(Mesh &m, IO::Seekable &i):
305         mesh(m),
306         io(i)
307 {
308         mesh.disallow_rendering = true;
309         mesh.check_buffers(VERTEX_BUFFER|INDEX_BUFFER);
310 }
311
312 Mesh::AsyncLoader::~AsyncLoader()
313 {
314         mesh.disallow_rendering = false;
315         delete vertex_updater;
316         delete index_updater;
317 }
318
319 bool Mesh::AsyncLoader::needs_sync() const
320 {
321         return phase%2;
322 }
323
324 bool Mesh::AsyncLoader::process()
325 {
326         if(phase==0)
327         {
328                 // TODO use correct filename
329                 DataFile::Parser parser(io, "async");
330                 Loader loader(mesh);
331                 loader.allow_gl_calls = false;
332                 loader.load(parser);
333         }
334         else if(phase==1)
335         {
336                 mesh.resize_buffers();
337                 vertex_updater = mesh.vertices.refresh_async();
338                 if(!mesh.batches.empty())
339                         index_updater = mesh.batches.front().refresh_async();
340         }
341         else if(phase==2)
342         {
343                 if(vertex_updater)
344                         vertex_updater->upload_data();
345                 if(index_updater)
346                         index_updater->upload_data();
347         }
348         else if(phase==3)
349         {
350                 delete vertex_updater;
351                 vertex_updater = 0;
352                 delete index_updater;
353                 index_updater = 0;
354         }
355
356         ++phase;
357         if(phase==1 && !mesh.vbuf && !mesh.ibuf)
358                 phase += 3;
359         return phase>3;
360 }
361
362 } // namespace GL
363 } // namespace Msp