style cleanup: block comments
[blender.git] / intern / cycles / render / mesh.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "bvh.h"
20 #include "bvh_build.h"
21
22 #include "device.h"
23 #include "shader.h"
24 #include "light.h"
25 #include "mesh.h"
26 #include "object.h"
27 #include "scene.h"
28
29 #include "osl_globals.h"
30
31 #include "util_cache.h"
32 #include "util_foreach.h"
33 #include "util_progress.h"
34 #include "util_set.h"
35
36 CCL_NAMESPACE_BEGIN
37
38 /* Mesh */
39
40 Mesh::Mesh()
41 {
42         need_update = true;
43         transform_applied = false;
44         transform_negative_scaled = false;
45         displacement_method = DISPLACE_BUMP;
46         bounds = BoundBox::empty;
47
48         bvh = NULL;
49
50         tri_offset = 0;
51         vert_offset = 0;
52
53         attributes.mesh = this;
54 }
55
56 Mesh::~Mesh()
57 {
58         delete bvh;
59 }
60
61 void Mesh::reserve(int numverts, int numtris)
62 {
63         /* reserve space to add verts and triangles later */
64         verts.resize(numverts);
65         triangles.resize(numtris);
66         shader.resize(numtris);
67         smooth.resize(numtris);
68         attributes.reserve(numverts, numtris);
69 }
70
71 void Mesh::clear()
72 {
73         /* clear all verts and triangles */
74         verts.clear();
75         triangles.clear();
76         shader.clear();
77         smooth.clear();
78
79         attributes.clear();
80         used_shaders.clear();
81
82         transform_applied = false;
83         transform_negative_scaled = false;
84 }
85
86 void Mesh::add_triangle(int v0, int v1, int v2, int shader_, bool smooth_)
87 {
88         Triangle t;
89         t.v[0] = v0;
90         t.v[1] = v1;
91         t.v[2] = v2;
92
93         triangles.push_back(t);
94         shader.push_back(shader_);
95         smooth.push_back(smooth_);
96 }
97
98 void Mesh::compute_bounds()
99 {
100         BoundBox bnds = BoundBox::empty;
101         size_t verts_size = verts.size();
102
103         for(size_t i = 0; i < verts_size; i++)
104                 bnds.grow(verts[i]);
105
106         /* happens mostly on empty meshes */
107         if(!bnds.valid())
108                 bnds.grow(make_float3(0.0f, 0.0f, 0.0f));
109
110         bounds = bnds;
111 }
112
113 void Mesh::add_face_normals()
114 {
115         /* don't compute if already there */
116         if(attributes.find(ATTR_STD_FACE_NORMAL))
117                 return;
118
119         /* get attributes */
120         Attribute *attr_fN = attributes.add(ATTR_STD_FACE_NORMAL);
121         float3 *fN = attr_fN->data_float3();
122
123         /* compute face normals */
124         size_t triangles_size = triangles.size();
125         bool flip = transform_negative_scaled;
126
127         if(triangles_size) {
128                 float3 *verts_ptr = &verts[0];
129                 Triangle *triangles_ptr = &triangles[0];
130
131                 for(size_t i = 0; i < triangles_size; i++) {
132                         Triangle t = triangles_ptr[i];
133                         float3 v0 = verts_ptr[t.v[0]];
134                         float3 v1 = verts_ptr[t.v[1]];
135                         float3 v2 = verts_ptr[t.v[2]];
136
137                         fN[i] = normalize(cross(v1 - v0, v2 - v0));
138
139                         if(flip)
140                                 fN[i] = -fN[i];
141                 }
142         }
143 }
144
145 void Mesh::add_vertex_normals()
146 {
147         /* don't compute if already there */
148         if(attributes.find(ATTR_STD_VERTEX_NORMAL))
149                 return;
150
151         /* get attributes */
152         Attribute *attr_fN = attributes.find(ATTR_STD_FACE_NORMAL);
153         Attribute *attr_vN = attributes.add(ATTR_STD_VERTEX_NORMAL);
154
155         float3 *fN = attr_fN->data_float3();
156         float3 *vN = attr_vN->data_float3();
157
158         /* compute vertex normals */
159         memset(vN, 0, verts.size()*sizeof(float3));
160
161         size_t verts_size = verts.size();
162         size_t triangles_size = triangles.size();
163         bool flip = transform_negative_scaled;
164
165         if(triangles_size) {
166                 Triangle *triangles_ptr = &triangles[0];
167
168                 for(size_t i = 0; i < triangles_size; i++)
169                         for(size_t j = 0; j < 3; j++)
170                                 vN[triangles_ptr[i].v[j]] += fN[i];
171         }
172
173         for(size_t i = 0; i < verts_size; i++) {
174                 vN[i] = normalize(vN[i]);
175                 if(flip)
176                         vN[i] = -vN[i];
177         }
178 }
179
180 void Mesh::pack_normals(Scene *scene, float4 *normal, float4 *vnormal)
181 {
182         Attribute *attr_fN = attributes.find(ATTR_STD_FACE_NORMAL);
183         Attribute *attr_vN = attributes.find(ATTR_STD_VERTEX_NORMAL);
184
185         float3 *fN = attr_fN->data_float3();
186         float3 *vN = attr_vN->data_float3();
187         int shader_id = 0;
188         uint last_shader = -1;
189         bool last_smooth = false;
190
191         size_t triangles_size = triangles.size();
192         uint *shader_ptr = (shader.size())? &shader[0]: NULL;
193
194         for(size_t i = 0; i < triangles_size; i++) {
195                 normal[i].x = fN[i].x;
196                 normal[i].y = fN[i].y;
197                 normal[i].z = fN[i].z;
198
199                 /* stuff shader id in here too */
200                 if(shader_ptr[i] != last_shader || last_smooth != smooth[i]) {
201                         last_shader = shader_ptr[i];
202                         last_smooth = smooth[i];
203                         shader_id = scene->shader_manager->get_shader_id(last_shader, this, last_smooth);
204                 }
205
206                 normal[i].w = __int_as_float(shader_id);
207         }
208
209         size_t verts_size = verts.size();
210
211         for(size_t i = 0; i < verts_size; i++)
212                 vnormal[i] = make_float4(vN[i].x, vN[i].y, vN[i].z, 0.0f);
213 }
214
215 void Mesh::pack_verts(float4 *tri_verts, float4 *tri_vindex, size_t vert_offset)
216 {
217         size_t verts_size = verts.size();
218
219         if(verts_size) {
220                 float3 *verts_ptr = &verts[0];
221
222                 for(size_t i = 0; i < verts_size; i++) {
223                         float3 p = verts_ptr[i];
224                         tri_verts[i] = make_float4(p.x, p.y, p.z, 0.0f);
225                 }
226         }
227
228         size_t triangles_size = triangles.size();
229
230         if(triangles_size) {
231                 Triangle *triangles_ptr = &triangles[0];
232
233                 for(size_t i = 0; i < triangles_size; i++) {
234                         Triangle t = triangles_ptr[i];
235
236                         tri_vindex[i] = make_float4(
237                                 __int_as_float(t.v[0] + vert_offset),
238                                 __int_as_float(t.v[1] + vert_offset),
239                                 __int_as_float(t.v[2] + vert_offset),
240                                 0);
241                 }
242         }
243 }
244
245 void Mesh::compute_bvh(SceneParams *params, Progress *progress, int n, int total)
246 {
247         if(progress->get_cancel())
248                 return;
249
250         compute_bounds();
251
252         if(!transform_applied) {
253                 string msg = "Updating Mesh BVH ";
254                 if(name == "")
255                         msg += string_printf("%u/%u", (uint)(n+1), (uint)total);
256                 else
257                         msg += string_printf("%s %u/%u", name.c_str(), (uint)(n+1), (uint)total);
258
259                 Object object;
260                 object.mesh = this;
261
262                 vector<Object*> objects;
263                 objects.push_back(&object);
264
265                 if(bvh && !need_update_rebuild) {
266                         progress->set_status(msg, "Refitting BVH");
267                         bvh->objects = objects;
268                         bvh->refit(*progress);
269                 }
270                 else {
271                         progress->set_status(msg, "Building BVH");
272
273                         BVHParams bparams;
274                         bparams.use_cache = params->use_bvh_cache;
275                         bparams.use_spatial_split = params->use_bvh_spatial_split;
276                         bparams.use_qbvh = params->use_qbvh;
277
278                         delete bvh;
279                         bvh = BVH::create(bparams, objects);
280                         bvh->build(*progress);
281                 }
282         }
283
284         need_update = false;
285         need_update_rebuild = false;
286 }
287
288 void Mesh::tag_update(Scene *scene, bool rebuild)
289 {
290         need_update = true;
291
292         if(rebuild) {
293                 need_update_rebuild = true;
294                 scene->light_manager->need_update = true;
295         }
296         else {
297                 foreach(uint sindex, used_shaders)
298                         if(scene->shaders[sindex]->has_surface_emission)
299                                 scene->light_manager->need_update = true;
300         }
301
302         scene->mesh_manager->need_update = true;
303         scene->object_manager->need_update = true;
304 }
305
306 /* Mesh Manager */
307
308 MeshManager::MeshManager()
309 {
310         bvh = NULL;
311         need_update = true;
312 }
313
314 MeshManager::~MeshManager()
315 {
316         delete bvh;
317 }
318
319 void MeshManager::update_osl_attributes(Device *device, Scene *scene, vector<AttributeRequestSet>& mesh_attributes)
320 {
321 #ifdef WITH_OSL
322         /* for OSL, a hash map is used to lookup the attribute by name. */
323         OSLGlobals *og = (OSLGlobals*)device->osl_memory();
324
325         og->object_name_map.clear();
326         og->attribute_map.clear();
327
328         og->attribute_map.resize(scene->objects.size());
329
330         for(size_t i = 0; i < scene->objects.size(); i++) {
331                 /* set object name to object index map */
332                 Object *object = scene->objects[i];
333                 og->object_name_map[object->name] = i;
334
335                 /* set object attributes */
336                 foreach(ParamValue& attr, object->attributes) {
337                         OSLGlobals::Attribute osl_attr;
338
339                         osl_attr.type = attr.type();
340                         osl_attr.elem = ATTR_ELEMENT_VALUE;
341                         osl_attr.value = attr;
342
343                         og->attribute_map[i][attr.name()] = osl_attr;
344                 }
345
346                 /* find mesh attributes */
347                 size_t j;
348
349                 for(j = 0; j < scene->meshes.size(); j++)
350                         if(scene->meshes[j] == object->mesh)
351                                 break;
352
353                 AttributeRequestSet& attributes = mesh_attributes[j];
354
355                 /* set object attributes */
356                 foreach(AttributeRequest& req, attributes.requests) {
357                         OSLGlobals::Attribute osl_attr;
358
359                         osl_attr.elem = req.element;
360                         osl_attr.offset = req.offset;
361
362                         if(req.type == TypeDesc::TypeFloat)
363                                 osl_attr.type = TypeDesc::TypeFloat;
364                         else
365                                 osl_attr.type = TypeDesc::TypeColor;
366
367                         if(req.std != ATTR_STD_NONE) {
368                                 /* if standard attribute, add lookup by std:: name convention */
369                                 ustring stdname = ustring(string("std::") + Attribute::standard_name(req.std).c_str());
370                                 og->attribute_map[i][stdname] = osl_attr;
371                         }
372                         else if(req.name != ustring()) {
373                                 /* add lookup by mesh attribute name */
374                                 og->attribute_map[i][req.name] = osl_attr;
375                         }
376                 }
377         }
378 #endif
379 }
380
381 void MeshManager::update_svm_attributes(Device *device, DeviceScene *dscene, Scene *scene, vector<AttributeRequestSet>& mesh_attributes)
382 {
383         /* for SVM, the attributes_map table is used to lookup the offset of an
384          * attribute, based on a unique shader attribute id. */
385
386         /* compute array stride */
387         int attr_map_stride = 0;
388
389         for(size_t i = 0; i < scene->meshes.size(); i++)
390                 attr_map_stride = max(attr_map_stride, mesh_attributes[i].size()+1);
391
392         if(attr_map_stride == 0)
393                 return;
394         
395         /* create attribute map */
396         uint4 *attr_map = dscene->attributes_map.resize(attr_map_stride*scene->objects.size());
397         memset(attr_map, 0, dscene->attributes_map.size()*sizeof(uint));
398
399         for(size_t i = 0; i < scene->objects.size(); i++) {
400                 Object *object = scene->objects[i];
401
402                 /* find mesh attributes */
403                 size_t j;
404
405                 for(j = 0; j < scene->meshes.size(); j++)
406                         if(scene->meshes[j] == object->mesh)
407                                 break;
408
409                 AttributeRequestSet& attributes = mesh_attributes[j];
410
411                 /* set object attributes */
412                 int index = i*attr_map_stride;
413
414                 foreach(AttributeRequest& req, attributes.requests) {
415                         uint id;
416
417                         if(req.std == ATTR_STD_NONE)
418                                 id = scene->shader_manager->get_attribute_id(req.name);
419                         else
420                                 id = scene->shader_manager->get_attribute_id(req.std);
421
422                         attr_map[index].x = id;
423                         attr_map[index].y = req.element;
424                         attr_map[index].z = as_uint(req.offset);
425
426                         if(req.type == TypeDesc::TypeFloat)
427                                 attr_map[index].w = NODE_ATTR_FLOAT;
428                         else
429                                 attr_map[index].w = NODE_ATTR_FLOAT3;
430
431                         index++;
432                 }
433
434                 /* terminator */
435                 attr_map[index].x = ATTR_STD_NONE;
436                 attr_map[index].y = 0;
437                 attr_map[index].z = 0;
438                 attr_map[index].w = 0;
439         }
440
441         /* copy to device */
442         dscene->data.bvh.attributes_map_stride = attr_map_stride;
443         device->tex_alloc("__attributes_map", dscene->attributes_map);
444 }
445
446 void MeshManager::device_update_attributes(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
447 {
448         progress.set_status("Updating Mesh", "Computing attributes");
449
450         /* gather per mesh requested attributes. as meshes may have multiple
451          * shaders assigned, this merges the requested attributes that have
452          * been set per shader by the shader manager */
453         vector<AttributeRequestSet> mesh_attributes(scene->meshes.size());
454
455         for(size_t i = 0; i < scene->meshes.size(); i++) {
456                 Mesh *mesh = scene->meshes[i];
457
458                 scene->need_global_attributes(mesh_attributes[i]);
459
460                 foreach(uint sindex, mesh->used_shaders) {
461                         Shader *shader = scene->shaders[sindex];
462                         mesh_attributes[i].add(shader->attributes);
463                 }
464         }
465
466         /* mesh attribute are stored in a single array per data type. here we fill
467          * those arrays, and set the offset and element type to create attribute
468          * maps next */
469         vector<float> attr_float;
470         vector<float4> attr_float3;
471
472         for(size_t i = 0; i < scene->meshes.size(); i++) {
473                 Mesh *mesh = scene->meshes[i];
474                 AttributeRequestSet& attributes = mesh_attributes[i];
475
476                 /* todo: we now store std and name attributes from requests even if
477                  * they actually refer to the same mesh attributes, optimize */
478                 foreach(AttributeRequest& req, attributes.requests) {
479                         Attribute *mattr = mesh->attributes.find(req);
480
481                         /* todo: get rid of this exception */
482                         if(!mattr && req.std == ATTR_STD_GENERATED) {
483                                 mattr = mesh->attributes.add(ATTR_STD_GENERATED);
484                                 if(mesh->verts.size())
485                                         memcpy(mattr->data_float3(), &mesh->verts[0], sizeof(float3)*mesh->verts.size());
486                         }
487
488                         /* attribute not found */
489                         if(!mattr) {
490                                 req.element = ATTR_ELEMENT_NONE;
491                                 req.offset = 0;
492                                 continue;
493                         }
494
495                         /* we abuse AttributeRequest to pass on info like element and
496                          * offset, it doesn't really make sense but is convenient */
497
498                         /* store element and type */
499                         if(mattr->element == Attribute::VERTEX)
500                                 req.element = ATTR_ELEMENT_VERTEX;
501                         else if(mattr->element == Attribute::FACE)
502                                 req.element = ATTR_ELEMENT_FACE;
503                         else if(mattr->element == Attribute::CORNER)
504                                 req.element = ATTR_ELEMENT_CORNER;
505
506                         req.type = mattr->type;
507
508                         /* store attribute data in arrays */
509                         size_t size = mattr->element_size(mesh->verts.size(), mesh->triangles.size());
510
511                         if(mattr->type == TypeDesc::TypeFloat) {
512                                 float *data = mattr->data_float();
513                                 req.offset = attr_float.size();
514
515                                 attr_float.resize(attr_float.size() + size);
516
517                                 for(size_t k = 0; k < size; k++)
518                                         attr_float[req.offset+k] = data[k];
519                         }
520                         else {
521                                 float3 *data = mattr->data_float3();
522                                 req.offset = attr_float3.size();
523
524                                 attr_float3.resize(attr_float3.size() + size);
525
526                                 for(size_t k = 0; k < size; k++)
527                                         attr_float3[req.offset+k] = float3_to_float4(data[k]);
528                         }
529
530                         /* mesh vertex/triangle index is global, not per object, so we sneak
531                          * a correction for that in here */
532                         if(req.element == ATTR_ELEMENT_VERTEX)
533                                 req.offset -= mesh->vert_offset;
534                         else if(mattr->element == Attribute::FACE)
535                                 req.offset -= mesh->tri_offset;
536                         else if(mattr->element == Attribute::CORNER)
537                                 req.offset -= 3*mesh->tri_offset;
538
539                         if(progress.get_cancel()) return;
540                 }
541         }
542
543         /* create attribute lookup maps */
544         if(scene->params.shadingsystem == SceneParams::OSL)
545                 update_osl_attributes(device, scene, mesh_attributes);
546         else
547                 update_svm_attributes(device, dscene, scene, mesh_attributes);
548
549         if(progress.get_cancel()) return;
550
551         /* copy to device */
552         progress.set_status("Updating Mesh", "Copying Attributes to device");
553
554         if(attr_float.size()) {
555                 dscene->attributes_float.copy(&attr_float[0], attr_float.size());
556                 device->tex_alloc("__attributes_float", dscene->attributes_float);
557         }
558         if(attr_float3.size()) {
559                 dscene->attributes_float3.copy(&attr_float3[0], attr_float3.size());
560                 device->tex_alloc("__attributes_float3", dscene->attributes_float3);
561         }
562 }
563
564 void MeshManager::device_update_mesh(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
565 {
566         /* count and update offsets */
567         size_t vert_size = 0;
568         size_t tri_size = 0;
569
570         foreach(Mesh *mesh, scene->meshes) {
571                 mesh->vert_offset = vert_size;
572                 mesh->tri_offset = tri_size;
573
574                 vert_size += mesh->verts.size();
575                 tri_size += mesh->triangles.size();
576         }
577
578         if(tri_size == 0)
579                 return;
580
581         /* normals */
582         progress.set_status("Updating Mesh", "Computing normals");
583
584         float4 *normal = dscene->tri_normal.resize(tri_size);
585         float4 *vnormal = dscene->tri_vnormal.resize(vert_size);
586         float4 *tri_verts = dscene->tri_verts.resize(vert_size);
587         float4 *tri_vindex = dscene->tri_vindex.resize(tri_size);
588
589         foreach(Mesh *mesh, scene->meshes) {
590                 mesh->pack_normals(scene, &normal[mesh->tri_offset], &vnormal[mesh->vert_offset]);
591                 mesh->pack_verts(&tri_verts[mesh->vert_offset], &tri_vindex[mesh->tri_offset], mesh->vert_offset);
592
593                 if(progress.get_cancel()) return;
594         }
595
596         /* vertex coordinates */
597         progress.set_status("Updating Mesh", "Copying Mesh to device");
598
599         device->tex_alloc("__tri_normal", dscene->tri_normal);
600         device->tex_alloc("__tri_vnormal", dscene->tri_vnormal);
601         device->tex_alloc("__tri_verts", dscene->tri_verts);
602         device->tex_alloc("__tri_vindex", dscene->tri_vindex);
603 }
604
605 void MeshManager::device_update_bvh(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
606 {
607         /* bvh build */
608         progress.set_status("Updating Scene BVH", "Building");
609
610         BVHParams bparams;
611         bparams.top_level = true;
612         bparams.use_qbvh = scene->params.use_qbvh;
613         bparams.use_spatial_split = scene->params.use_bvh_spatial_split;
614         bparams.use_cache = scene->params.use_bvh_cache;
615
616         delete bvh;
617         bvh = BVH::create(bparams, scene->objects);
618         bvh->build(progress);
619
620         if(progress.get_cancel()) return;
621
622         /* copy to device */
623         progress.set_status("Updating Scene BVH", "Copying BVH to device");
624
625         PackedBVH& pack = bvh->pack;
626
627         if(pack.nodes.size()) {
628                 dscene->bvh_nodes.reference((float4*)&pack.nodes[0], pack.nodes.size());
629                 device->tex_alloc("__bvh_nodes", dscene->bvh_nodes);
630         }
631         if(pack.object_node.size()) {
632                 dscene->object_node.reference((uint*)&pack.object_node[0], pack.object_node.size());
633                 device->tex_alloc("__object_node", dscene->object_node);
634         }
635         if(pack.tri_woop.size()) {
636                 dscene->tri_woop.reference(&pack.tri_woop[0], pack.tri_woop.size());
637                 device->tex_alloc("__tri_woop", dscene->tri_woop);
638         }
639         if(pack.prim_visibility.size()) {
640                 dscene->prim_visibility.reference((uint*)&pack.prim_visibility[0], pack.prim_visibility.size());
641                 device->tex_alloc("__prim_visibility", dscene->prim_visibility);
642         }
643         if(pack.prim_index.size()) {
644                 dscene->prim_index.reference((uint*)&pack.prim_index[0], pack.prim_index.size());
645                 device->tex_alloc("__prim_index", dscene->prim_index);
646         }
647         if(pack.prim_object.size()) {
648                 dscene->prim_object.reference((uint*)&pack.prim_object[0], pack.prim_object.size());
649                 device->tex_alloc("__prim_object", dscene->prim_object);
650         }
651
652         dscene->data.bvh.root = pack.root_index;
653 }
654
655 void MeshManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
656 {
657         if(!need_update)
658                 return;
659
660         /* update normals */
661         foreach(Mesh *mesh, scene->meshes) {
662                 foreach(uint shader, mesh->used_shaders)
663                         if(scene->shaders[shader]->need_update_attributes)
664                                 mesh->need_update = true;
665
666                 if(mesh->need_update) {
667                         mesh->add_face_normals();
668                         mesh->add_vertex_normals();
669
670                         if(progress.get_cancel()) return;
671                 }
672         }
673
674         /* device update */
675         device_free(device, dscene);
676
677         device_update_mesh(device, dscene, scene, progress);
678         if(progress.get_cancel()) return;
679
680         device_update_attributes(device, dscene, scene, progress);
681         if(progress.get_cancel()) return;
682
683         /* update displacement */
684         bool displacement_done = false;
685
686         foreach(Mesh *mesh, scene->meshes)
687                 if(mesh->need_update && displace(device, scene, mesh, progress))
688                         displacement_done = true;
689
690         /* todo: properly handle cancel halfway displacement */
691         if(progress.get_cancel()) return;
692
693         /* device re-update after displacement */
694         if(displacement_done) {
695                 device_free(device, dscene);
696
697                 device_update_mesh(device, dscene, scene, progress);
698                 if(progress.get_cancel()) return;
699
700                 device_update_attributes(device, dscene, scene, progress);
701                 if(progress.get_cancel()) return;
702         }
703
704         /* update bvh */
705         size_t i = 0, num_bvh = 0;
706
707         foreach(Mesh *mesh, scene->meshes)
708                 if(mesh->need_update && !mesh->transform_applied)
709                         num_bvh++;
710
711         TaskPool pool;
712
713         foreach(Mesh *mesh, scene->meshes) {
714                 if(mesh->need_update) {
715                         pool.push(function_bind(&Mesh::compute_bvh, mesh, &scene->params, &progress, i, num_bvh));
716                         i++;
717                 }
718         }
719
720         pool.wait_work();
721         
722         foreach(Shader *shader, scene->shaders)
723                 shader->need_update_attributes = false;
724
725         bool motion_blur = scene->need_motion() == Scene::MOTION_BLUR;
726
727         foreach(Object *object, scene->objects)
728                 object->compute_bounds(motion_blur);
729
730         if(progress.get_cancel()) return;
731
732         device_update_bvh(device, dscene, scene, progress);
733
734         need_update = false;
735 }
736
737 void MeshManager::device_free(Device *device, DeviceScene *dscene)
738 {
739         device->tex_free(dscene->bvh_nodes);
740         device->tex_free(dscene->object_node);
741         device->tex_free(dscene->tri_woop);
742         device->tex_free(dscene->prim_visibility);
743         device->tex_free(dscene->prim_index);
744         device->tex_free(dscene->prim_object);
745         device->tex_free(dscene->tri_normal);
746         device->tex_free(dscene->tri_vnormal);
747         device->tex_free(dscene->tri_vindex);
748         device->tex_free(dscene->tri_verts);
749         device->tex_free(dscene->attributes_map);
750         device->tex_free(dscene->attributes_float);
751         device->tex_free(dscene->attributes_float3);
752
753         dscene->bvh_nodes.clear();
754         dscene->object_node.clear();
755         dscene->tri_woop.clear();
756         dscene->prim_visibility.clear();
757         dscene->prim_index.clear();
758         dscene->prim_object.clear();
759         dscene->tri_normal.clear();
760         dscene->tri_vnormal.clear();
761         dscene->tri_vindex.clear();
762         dscene->tri_verts.clear();
763         dscene->attributes_map.clear();
764         dscene->attributes_float.clear();
765         dscene->attributes_float3.clear();
766 }
767
768 void MeshManager::tag_update(Scene *scene)
769 {
770         need_update = true;
771         scene->object_manager->need_update = true;
772 }
773
774 bool Mesh::need_attribute(Scene *scene, AttributeStandard std)
775 {
776         if(std == ATTR_STD_NONE)
777                 return false;
778         
779         if(scene->need_global_attribute(std))
780                 return true;
781
782         foreach(uint shader, used_shaders)
783                 if(scene->shaders[shader]->attributes.find(std))
784                         return true;
785         
786         return false;
787 }
788
789 bool Mesh::need_attribute(Scene *scene, ustring name)
790 {
791         if(name == ustring())
792                 return false;
793
794         foreach(uint shader, used_shaders)
795                 if(scene->shaders[shader]->attributes.find(name))
796                         return true;
797         
798         return false;
799 }
800
801 CCL_NAMESPACE_END
802