Merging r46111 through r46136 from trunk into soc-2011-tomato
[blender-staging.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)
246 {
247         Object object;
248         object.mesh = this;
249
250         vector<Object*> objects;
251         objects.push_back(&object);
252
253         if(bvh && !need_update_rebuild) {
254                 progress.set_substatus("Refitting BVH");
255                 bvh->objects = objects;
256                 bvh->refit(progress);
257         }
258         else {
259                 progress.set_substatus("Building BVH");
260
261                 BVHParams bparams;
262                 bparams.use_cache = params->use_bvh_cache;
263                 bparams.use_spatial_split = params->use_bvh_spatial_split;
264                 bparams.use_qbvh = params->use_qbvh;
265
266                 delete bvh;
267                 bvh = BVH::create(bparams, objects);
268                 bvh->build(progress);
269         }
270 }
271
272 void Mesh::tag_update(Scene *scene, bool rebuild)
273 {
274         need_update = true;
275
276         if(rebuild) {
277                 need_update_rebuild = true;
278                 scene->light_manager->need_update = true;
279         }
280         else {
281                 foreach(uint sindex, used_shaders)
282                         if(scene->shaders[sindex]->has_surface_emission)
283                                 scene->light_manager->need_update = true;
284         }
285
286         scene->mesh_manager->need_update = true;
287         scene->object_manager->need_update = true;
288 }
289
290 /* Mesh Manager */
291
292 MeshManager::MeshManager()
293 {
294         bvh = NULL;
295         need_update = true;
296 }
297
298 MeshManager::~MeshManager()
299 {
300         delete bvh;
301 }
302
303 void MeshManager::update_osl_attributes(Device *device, Scene *scene, vector<AttributeRequestSet>& mesh_attributes)
304 {
305 #ifdef WITH_OSL
306         /* for OSL, a hash map is used to lookup the attribute by name. */
307         OSLGlobals *og = (OSLGlobals*)device->osl_memory();
308
309         og->object_name_map.clear();
310         og->attribute_map.clear();
311
312         og->attribute_map.resize(scene->objects.size());
313
314         for(size_t i = 0; i < scene->objects.size(); i++) {
315                 /* set object name to object index map */
316                 Object *object = scene->objects[i];
317                 og->object_name_map[object->name] = i;
318
319                 /* set object attributes */
320                 foreach(ParamValue& attr, object->attributes) {
321                         OSLGlobals::Attribute osl_attr;
322
323                         osl_attr.type = attr.type();
324                         osl_attr.elem = ATTR_ELEMENT_VALUE;
325                         osl_attr.value = attr;
326
327                         og->attribute_map[i][attr.name()] = osl_attr;
328                 }
329
330                 /* find mesh attributes */
331                 size_t j;
332
333                 for(j = 0; j < scene->meshes.size(); j++)
334                         if(scene->meshes[j] == object->mesh)
335                                 break;
336
337                 AttributeRequestSet& attributes = mesh_attributes[j];
338
339                 /* set object attributes */
340                 foreach(AttributeRequest& req, attributes.requests) {
341                         OSLGlobals::Attribute osl_attr;
342
343                         osl_attr.elem = req.element;
344                         osl_attr.offset = req.offset;
345
346                         if(req.type == TypeDesc::TypeFloat)
347                                 osl_attr.type = TypeDesc::TypeFloat;
348                         else
349                                 osl_attr.type = TypeDesc::TypeColor;
350
351                         if(req.std != ATTR_STD_NONE) {
352                                 /* if standard attribute, add lookup by std:: name convention */
353                                 ustring stdname = ustring(string("std::") + Attribute::standard_name(req.std).c_str());
354                                 og->attribute_map[i][stdname] = osl_attr;
355                         }
356                         else if(req.name != ustring()) {
357                                 /* add lookup by mesh attribute name */
358                                 og->attribute_map[i][req.name] = osl_attr;
359                         }
360                 }
361         }
362 #endif
363 }
364
365 void MeshManager::update_svm_attributes(Device *device, DeviceScene *dscene, Scene *scene, vector<AttributeRequestSet>& mesh_attributes)
366 {
367         /* for SVM, the attributes_map table is used to lookup the offset of an
368          * attribute, based on a unique shader attribute id. */
369
370         /* compute array stride */
371         int attr_map_stride = 0;
372
373         for(size_t i = 0; i < scene->meshes.size(); i++)
374                 attr_map_stride = max(attr_map_stride, mesh_attributes[i].size()+1);
375
376         if(attr_map_stride == 0)
377                 return;
378         
379         /* create attribute map */
380         uint4 *attr_map = dscene->attributes_map.resize(attr_map_stride*scene->objects.size());
381         memset(attr_map, 0, dscene->attributes_map.size()*sizeof(uint));
382
383         for(size_t i = 0; i < scene->objects.size(); i++) {
384                 Object *object = scene->objects[i];
385
386                 /* find mesh attributes */
387                 size_t j;
388
389                 for(j = 0; j < scene->meshes.size(); j++)
390                         if(scene->meshes[j] == object->mesh)
391                                 break;
392
393                 AttributeRequestSet& attributes = mesh_attributes[j];
394
395                 /* set object attributes */
396                 int index = i*attr_map_stride;
397
398                 foreach(AttributeRequest& req, attributes.requests) {
399                         uint id;
400
401                         if(req.std == ATTR_STD_NONE)
402                                 id = scene->shader_manager->get_attribute_id(req.name);
403                         else
404                                 id = scene->shader_manager->get_attribute_id(req.std);
405
406                         attr_map[index].x = id;
407                         attr_map[index].y = req.element;
408                         attr_map[index].z = req.offset;
409
410                         if(req.type == TypeDesc::TypeFloat)
411                                 attr_map[index].w = NODE_ATTR_FLOAT;
412                         else
413                                 attr_map[index].w = NODE_ATTR_FLOAT3;
414
415                         index++;
416                 }
417
418                 /* terminator */
419                 attr_map[index].x = ATTR_STD_NONE;
420                 attr_map[index].y = 0;
421                 attr_map[index].z = 0;
422                 attr_map[index].w = 0;
423         }
424
425         /* copy to device */
426         dscene->data.bvh.attributes_map_stride = attr_map_stride;
427         device->tex_alloc("__attributes_map", dscene->attributes_map);
428 }
429
430 void MeshManager::device_update_attributes(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
431 {
432         progress.set_status("Updating Mesh", "Computing attributes");
433
434         /* gather per mesh requested attributes. as meshes may have multiple
435          * shaders assigned, this merges the requested attributes that have
436          * been set per shader by the shader manager */
437         vector<AttributeRequestSet> mesh_attributes(scene->meshes.size());
438
439         for(size_t i = 0; i < scene->meshes.size(); i++) {
440                 Mesh *mesh = scene->meshes[i];
441
442                 scene->need_global_attributes(mesh_attributes[i]);
443
444                 foreach(uint sindex, mesh->used_shaders) {
445                         Shader *shader = scene->shaders[sindex];
446                         mesh_attributes[i].add(shader->attributes);
447                 }
448         }
449
450         /* mesh attribute are stored in a single array per data type. here we fill
451          * those arrays, and set the offset and element type to create attribute
452          * maps next */
453         vector<float> attr_float;
454         vector<float4> attr_float3;
455
456         for(size_t i = 0; i < scene->meshes.size(); i++) {
457                 Mesh *mesh = scene->meshes[i];
458                 AttributeRequestSet& attributes = mesh_attributes[i];
459
460                 /* todo: we now store std and name attributes from requests even if
461                    they actually refer to the same mesh attributes, optimize */
462                 foreach(AttributeRequest& req, attributes.requests) {
463                         Attribute *mattr = mesh->attributes.find(req);
464
465                         /* todo: get rid of this exception */
466                         if(!mattr && req.std == ATTR_STD_GENERATED) {
467                                 mattr = mesh->attributes.add(ATTR_STD_GENERATED);
468                                 if(mesh->verts.size())
469                                         memcpy(mattr->data_float3(), &mesh->verts[0], sizeof(float3)*mesh->verts.size());
470                         }
471
472                         /* attribute not found */
473                         if(!mattr) {
474                                 req.element = ATTR_ELEMENT_NONE;
475                                 req.offset = 0;
476                                 continue;
477                         }
478
479                         /* we abuse AttributeRequest to pass on info like element and
480                            offset, it doesn't really make sense but is convenient */
481
482                         /* store element and type */
483                         if(mattr->element == Attribute::VERTEX)
484                                 req.element = ATTR_ELEMENT_VERTEX;
485                         else if(mattr->element == Attribute::FACE)
486                                 req.element = ATTR_ELEMENT_FACE;
487                         else if(mattr->element == Attribute::CORNER)
488                                 req.element = ATTR_ELEMENT_CORNER;
489
490                         req.type = mattr->type;
491
492                         /* store attribute data in arrays */
493                         size_t size = mattr->element_size(mesh->verts.size(), mesh->triangles.size());
494
495                         if(mattr->type == TypeDesc::TypeFloat) {
496                                 float *data = mattr->data_float();
497                                 req.offset = attr_float.size();
498
499                                 attr_float.resize(attr_float.size() + size);
500
501                                 for(size_t k = 0; k < size; k++)
502                                         attr_float[req.offset+k] = data[k];
503                         }
504                         else {
505                                 float3 *data = mattr->data_float3();
506                                 req.offset = attr_float3.size();
507
508                                 attr_float3.resize(attr_float3.size() + size);
509
510                                 for(size_t k = 0; k < size; k++)
511                                         attr_float3[req.offset+k] = float3_to_float4(data[k]);
512                         }
513
514                         /* mesh vertex/triangle index is global, not per object, so we sneak
515                            a correction for that in here */
516                         if(req.element == ATTR_ELEMENT_VERTEX)
517                                 req.offset -= mesh->vert_offset;
518                         else if(mattr->element == Attribute::FACE)
519                                 req.offset -= mesh->tri_offset;
520                         else if(mattr->element == Attribute::CORNER)
521                                 req.offset -= 3*mesh->tri_offset;
522
523                         if(progress.get_cancel()) return;
524                 }
525         }
526
527         /* create attribute lookup maps */
528         if(scene->params.shadingsystem == SceneParams::OSL)
529                 update_osl_attributes(device, scene, mesh_attributes);
530         else
531                 update_svm_attributes(device, dscene, scene, mesh_attributes);
532
533         if(progress.get_cancel()) return;
534
535         /* copy to device */
536         progress.set_status("Updating Mesh", "Copying Attributes to device");
537
538         if(attr_float.size()) {
539                 dscene->attributes_float.copy(&attr_float[0], attr_float.size());
540                 device->tex_alloc("__attributes_float", dscene->attributes_float);
541         }
542         if(attr_float3.size()) {
543                 dscene->attributes_float3.copy(&attr_float3[0], attr_float3.size());
544                 device->tex_alloc("__attributes_float3", dscene->attributes_float3);
545         }
546 }
547
548 void MeshManager::device_update_mesh(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
549 {
550         /* count and update offsets */
551         size_t vert_size = 0;
552         size_t tri_size = 0;
553
554         foreach(Mesh *mesh, scene->meshes) {
555                 mesh->vert_offset = vert_size;
556                 mesh->tri_offset = tri_size;
557
558                 vert_size += mesh->verts.size();
559                 tri_size += mesh->triangles.size();
560         }
561
562         if(tri_size == 0)
563                 return;
564
565         /* normals */
566         progress.set_status("Updating Mesh", "Computing normals");
567
568         float4 *normal = dscene->tri_normal.resize(tri_size);
569         float4 *vnormal = dscene->tri_vnormal.resize(vert_size);
570         float4 *tri_verts = dscene->tri_verts.resize(vert_size);
571         float4 *tri_vindex = dscene->tri_vindex.resize(tri_size);
572
573         foreach(Mesh *mesh, scene->meshes) {
574                 mesh->pack_normals(scene, &normal[mesh->tri_offset], &vnormal[mesh->vert_offset]);
575                 mesh->pack_verts(&tri_verts[mesh->vert_offset], &tri_vindex[mesh->tri_offset], mesh->vert_offset);
576
577                 if(progress.get_cancel()) return;
578         }
579
580         /* vertex coordinates */
581         progress.set_status("Updating Mesh", "Copying Mesh to device");
582
583         device->tex_alloc("__tri_normal", dscene->tri_normal);
584         device->tex_alloc("__tri_vnormal", dscene->tri_vnormal);
585         device->tex_alloc("__tri_verts", dscene->tri_verts);
586         device->tex_alloc("__tri_vindex", dscene->tri_vindex);
587 }
588
589 void MeshManager::device_update_bvh(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
590 {
591         /* bvh build */
592         progress.set_status("Updating Scene BVH", "Building");
593
594         BVHParams bparams;
595         bparams.top_level = true;
596         bparams.use_qbvh = scene->params.use_qbvh;
597         bparams.use_spatial_split = scene->params.use_bvh_spatial_split;
598         bparams.use_cache = scene->params.use_bvh_cache;
599
600         delete bvh;
601         bvh = BVH::create(bparams, scene->objects);
602         bvh->build(progress);
603
604         if(progress.get_cancel()) return;
605
606         /* copy to device */
607         progress.set_status("Updating Scene BVH", "Copying BVH to device");
608
609         PackedBVH& pack = bvh->pack;
610
611         if(pack.nodes.size()) {
612                 dscene->bvh_nodes.reference((float4*)&pack.nodes[0], pack.nodes.size());
613                 device->tex_alloc("__bvh_nodes", dscene->bvh_nodes);
614         }
615         if(pack.object_node.size()) {
616                 dscene->object_node.reference((uint*)&pack.object_node[0], pack.object_node.size());
617                 device->tex_alloc("__object_node", dscene->object_node);
618         }
619         if(pack.tri_woop.size()) {
620                 dscene->tri_woop.reference(&pack.tri_woop[0], pack.tri_woop.size());
621                 device->tex_alloc("__tri_woop", dscene->tri_woop);
622         }
623         if(pack.prim_visibility.size()) {
624                 dscene->prim_visibility.reference((uint*)&pack.prim_visibility[0], pack.prim_visibility.size());
625                 device->tex_alloc("__prim_visibility", dscene->prim_visibility);
626         }
627         if(pack.prim_index.size()) {
628                 dscene->prim_index.reference((uint*)&pack.prim_index[0], pack.prim_index.size());
629                 device->tex_alloc("__prim_index", dscene->prim_index);
630         }
631         if(pack.prim_object.size()) {
632                 dscene->prim_object.reference((uint*)&pack.prim_object[0], pack.prim_object.size());
633                 device->tex_alloc("__prim_object", dscene->prim_object);
634         }
635
636         dscene->data.bvh.root = pack.root_index;
637 }
638
639 void MeshManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
640 {
641         if(!need_update)
642                 return;
643
644         /* update normals */
645         foreach(Mesh *mesh, scene->meshes) {
646                 foreach(uint shader, mesh->used_shaders)
647                         if(scene->shaders[shader]->need_update_attributes)
648                                 mesh->need_update = true;
649
650                 if(mesh->need_update) {
651                         mesh->add_face_normals();
652                         mesh->add_vertex_normals();
653
654                         if(progress.get_cancel()) return;
655                 }
656         }
657
658         /* device update */
659         device_free(device, dscene);
660
661         device_update_mesh(device, dscene, scene, progress);
662         if(progress.get_cancel()) return;
663
664         device_update_attributes(device, dscene, scene, progress);
665         if(progress.get_cancel()) return;
666
667         /* update displacement */
668         bool displacement_done = false;
669
670         foreach(Mesh *mesh, scene->meshes)
671                 if(mesh->need_update && displace(device, scene, mesh, progress))
672                         displacement_done = true;
673
674         /* todo: properly handle cancel halfway displacement */
675         if(progress.get_cancel()) return;
676
677         /* device re-update after displacement */
678         if(displacement_done) {
679                 device_free(device, dscene);
680
681                 device_update_mesh(device, dscene, scene, progress);
682                 if(progress.get_cancel()) return;
683
684                 device_update_attributes(device, dscene, scene, progress);
685                 if(progress.get_cancel()) return;
686         }
687
688         /* update bvh */
689         size_t i = 0, num_instance_bvh = 0;
690
691         foreach(Mesh *mesh, scene->meshes)
692                 if(mesh->need_update && !mesh->transform_applied)
693                         num_instance_bvh++;
694
695         foreach(Mesh *mesh, scene->meshes) {
696                 if(mesh->need_update) {
697                         mesh->compute_bounds();
698
699                         if(!mesh->transform_applied) {
700                                 string msg = "Updating Mesh BVH ";
701                                 if(mesh->name == "")
702                                         msg += string_printf("%u/%u", (uint)(i+1), (uint)num_instance_bvh);
703                                 else
704                                         msg += string_printf("%s %u/%u", mesh->name.c_str(), (uint)(i+1), (uint)num_instance_bvh);
705                                 progress.set_status(msg, "Building BVH");
706
707                                 mesh->compute_bvh(&scene->params, progress);
708
709                                 i++;
710                         }
711
712                         if(progress.get_cancel()) return;
713
714                         mesh->need_update = false;
715                         mesh->need_update_rebuild = false;
716                 }
717         }
718         
719         foreach(Shader *shader, scene->shaders)
720                 shader->need_update_attributes = false;
721
722         bool motion_blur = scene->need_motion() == Scene::MOTION_BLUR;
723
724         foreach(Object *object, scene->objects)
725                 object->compute_bounds(motion_blur);
726
727         if(progress.get_cancel()) return;
728
729         device_update_bvh(device, dscene, scene, progress);
730
731         need_update = false;
732 }
733
734 void MeshManager::device_free(Device *device, DeviceScene *dscene)
735 {
736         device->tex_free(dscene->bvh_nodes);
737         device->tex_free(dscene->object_node);
738         device->tex_free(dscene->tri_woop);
739         device->tex_free(dscene->prim_visibility);
740         device->tex_free(dscene->prim_index);
741         device->tex_free(dscene->prim_object);
742         device->tex_free(dscene->tri_normal);
743         device->tex_free(dscene->tri_vnormal);
744         device->tex_free(dscene->tri_vindex);
745         device->tex_free(dscene->tri_verts);
746         device->tex_free(dscene->attributes_map);
747         device->tex_free(dscene->attributes_float);
748         device->tex_free(dscene->attributes_float3);
749
750         dscene->bvh_nodes.clear();
751         dscene->object_node.clear();
752         dscene->tri_woop.clear();
753         dscene->prim_visibility.clear();
754         dscene->prim_index.clear();
755         dscene->prim_object.clear();
756         dscene->tri_normal.clear();
757         dscene->tri_vnormal.clear();
758         dscene->tri_vindex.clear();
759         dscene->tri_verts.clear();
760         dscene->attributes_map.clear();
761         dscene->attributes_float.clear();
762         dscene->attributes_float3.clear();
763 }
764
765 void MeshManager::tag_update(Scene *scene)
766 {
767         need_update = true;
768         scene->object_manager->need_update = true;
769 }
770
771 bool Mesh::need_attribute(Scene *scene, AttributeStandard std)
772 {
773         if(std == ATTR_STD_NONE)
774                 return false;
775         
776         if(scene->need_global_attribute(std))
777                 return true;
778
779         foreach(uint shader, used_shaders)
780                 if(scene->shaders[shader]->attributes.find(std))
781                         return true;
782         
783         return false;
784 }
785
786 bool Mesh::need_attribute(Scene *scene, ustring name)
787 {
788         if(name == ustring())
789                 return false;
790
791         foreach(uint shader, used_shaders)
792                 if(scene->shaders[shader]->attributes.find(name))
793                         return true;
794         
795         return false;
796 }
797
798 CCL_NAMESPACE_END
799