Cycles: De-duplicate check for MIS shaders in meshes
[blender-staging.git] / intern / cycles / render / mesh.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "bvh.h"
18 #include "bvh_build.h"
19
20 #include "camera.h"
21 #include "curves.h"
22 #include "device.h"
23 #include "graph.h"
24 #include "shader.h"
25 #include "light.h"
26 #include "mesh.h"
27 #include "nodes.h"
28 #include "object.h"
29 #include "scene.h"
30
31 #include "osl_globals.h"
32
33 #include "util_foreach.h"
34 #include "util_logging.h"
35 #include "util_progress.h"
36 #include "util_set.h"
37
38 #include "subd_split.h"
39 #include "subd_patch.h"
40
41 CCL_NAMESPACE_BEGIN
42
43 /* Triangle */
44
45 void Mesh::Triangle::bounds_grow(const float3 *verts, BoundBox& bounds) const
46 {
47         bounds.grow(verts[v[0]]);
48         bounds.grow(verts[v[1]]);
49         bounds.grow(verts[v[2]]);
50 }
51
52 /* Curve */
53
54 void Mesh::Curve::bounds_grow(const int k, const float3 *curve_keys, const float *curve_radius, BoundBox& bounds) const
55 {
56         float3 P[4];
57
58         P[0] = curve_keys[max(first_key + k - 1,first_key)];
59         P[1] = curve_keys[first_key + k];
60         P[2] = curve_keys[first_key + k + 1];
61         P[3] = curve_keys[min(first_key + k + 2, first_key + num_keys - 1)];
62
63         float3 lower;
64         float3 upper;
65
66         curvebounds(&lower.x, &upper.x, P, 0);
67         curvebounds(&lower.y, &upper.y, P, 1);
68         curvebounds(&lower.z, &upper.z, P, 2);
69
70         float mr = max(curve_radius[first_key + k], curve_radius[first_key + k + 1]);
71
72         bounds.grow(lower, mr);
73         bounds.grow(upper, mr);
74 }
75
76 void Mesh::Curve::bounds_grow(const int k,
77                               const float3 *curve_keys,
78                               const float *curve_radius,
79                               const Transform& aligned_space,
80                               BoundBox& bounds) const
81 {
82         float3 P[4];
83
84         P[0] = curve_keys[max(first_key + k - 1,first_key)];
85         P[1] = curve_keys[first_key + k];
86         P[2] = curve_keys[first_key + k + 1];
87         P[3] = curve_keys[min(first_key + k + 2, first_key + num_keys - 1)];
88
89         P[0] = transform_point(&aligned_space, P[0]);
90         P[1] = transform_point(&aligned_space, P[1]);
91         P[2] = transform_point(&aligned_space, P[2]);
92         P[3] = transform_point(&aligned_space, P[3]);
93
94         float3 lower;
95         float3 upper;
96
97         curvebounds(&lower.x, &upper.x, P, 0);
98         curvebounds(&lower.y, &upper.y, P, 1);
99         curvebounds(&lower.z, &upper.z, P, 2);
100
101         float mr = max(curve_radius[first_key + k], curve_radius[first_key + k + 1]);
102
103         bounds.grow(lower, mr);
104         bounds.grow(upper, mr);
105 }
106
107 /* Mesh */
108
109 NODE_DEFINE(Mesh)
110 {
111         NodeType* type = NodeType::add("mesh", create);
112
113         static NodeEnum displacement_method_enum;
114         displacement_method_enum.insert("bump", DISPLACE_BUMP);
115         displacement_method_enum.insert("true", DISPLACE_TRUE);
116         displacement_method_enum.insert("both", DISPLACE_BOTH);
117         SOCKET_ENUM(displacement_method, "Displacement Method", displacement_method_enum, DISPLACE_BUMP);
118
119         SOCKET_UINT(motion_steps, "Motion Steps", 3);
120         SOCKET_BOOLEAN(use_motion_blur, "Use Motion Blur", false);
121
122         SOCKET_INT_ARRAY(triangles, "Triangles", array<int>());
123         SOCKET_POINT_ARRAY(verts, "Vertices", array<float3>());
124         SOCKET_INT_ARRAY(shader, "Shader", array<int>());
125         SOCKET_BOOLEAN_ARRAY(smooth, "Smooth", array<bool>());
126
127         SOCKET_POINT_ARRAY(curve_keys, "Curve Keys", array<float3>());
128         SOCKET_FLOAT_ARRAY(curve_radius, "Curve Radius", array<float>());
129         SOCKET_INT_ARRAY(curve_first_key, "Curve First Key", array<int>());
130         SOCKET_INT_ARRAY(curve_shader, "Curve Shader", array<int>());
131
132         return type;
133 }
134
135 Mesh::Mesh()
136 : Node(node_type)
137 {
138         need_update = true;
139         need_update_rebuild = false;
140         transform_applied = false;
141         transform_negative_scaled = false;
142         transform_normal = transform_identity();
143         bounds = BoundBox::empty;
144
145         bvh = NULL;
146
147         tri_offset = 0;
148         vert_offset = 0;
149
150         curve_offset = 0;
151         curvekey_offset = 0;
152
153         attributes.triangle_mesh = this;
154         curve_attributes.curve_mesh = this;
155
156         geometry_flags = GEOMETRY_NONE;
157
158         has_volume = false;
159         has_mis_emission = false;
160         has_surface_bssrdf = false;
161 }
162
163 Mesh::~Mesh()
164 {
165         delete bvh;
166 }
167
168 void Mesh::resize_mesh(int numverts, int numtris)
169 {
170         verts.resize(numverts);
171         triangles.resize(numtris * 3);
172         shader.resize(numtris);
173         smooth.resize(numtris);
174
175         forms_quad.resize(numtris);
176
177         attributes.resize();
178 }
179
180 void Mesh::reserve_mesh(int numverts, int numtris)
181 {
182         /* reserve space to add verts and triangles later */
183         verts.reserve(numverts);
184         triangles.reserve(numtris * 3);
185         shader.reserve(numtris);
186         smooth.reserve(numtris);
187
188         forms_quad.reserve(numtris);
189
190         attributes.resize(true);
191 }
192
193 void Mesh::resize_curves(int numcurves, int numkeys)
194 {
195         curve_keys.resize(numkeys);
196         curve_radius.resize(numkeys);
197         curve_first_key.resize(numcurves);
198         curve_shader.resize(numcurves);
199
200         curve_attributes.resize();
201 }
202
203 void Mesh::reserve_curves(int numcurves, int numkeys)
204 {
205         curve_keys.reserve(numkeys);
206         curve_radius.reserve(numkeys);
207         curve_first_key.reserve(numcurves);
208         curve_shader.reserve(numcurves);
209
210         curve_attributes.resize(true);
211 }
212
213 void Mesh::clear()
214 {
215         /* clear all verts and triangles */
216         verts.clear();
217         triangles.clear();
218         shader.clear();
219         smooth.clear();
220
221         forms_quad.clear();
222
223         curve_keys.clear();
224         curve_radius.clear();
225         curve_first_key.clear();
226         curve_shader.clear();
227
228         attributes.clear();
229         curve_attributes.clear();
230         used_shaders.clear();
231
232         transform_applied = false;
233         transform_negative_scaled = false;
234         transform_normal = transform_identity();
235         geometry_flags = GEOMETRY_NONE;
236 }
237
238 int Mesh::split_vertex(int vertex)
239 {
240         /* copy vertex location and vertex attributes */
241         add_vertex_slow(verts[vertex]);
242
243         foreach(Attribute& attr, attributes.attributes) {
244                 if(attr.element == ATTR_ELEMENT_VERTEX) {
245                         vector<char> tmp(attr.data_sizeof());
246                         memcpy(&tmp[0], attr.data() + tmp.size()*vertex, tmp.size());
247                         attr.add(&tmp[0]);
248                 }
249         }
250
251         return verts.size() - 1;
252 }
253
254 void Mesh::add_vertex(float3 P)
255 {
256         verts.push_back_reserved(P);
257 }
258
259 void Mesh::add_vertex_slow(float3 P)
260 {
261         verts.push_back_slow(P);
262 }
263
264 void Mesh::add_triangle(int v0, int v1, int v2, int shader_, bool smooth_, bool forms_quad_)
265 {
266         triangles.push_back_reserved(v0);
267         triangles.push_back_reserved(v1);
268         triangles.push_back_reserved(v2);
269         shader.push_back_reserved(shader_);
270         smooth.push_back_reserved(smooth_);
271         forms_quad.push_back_reserved(forms_quad_);
272 }
273
274 void Mesh::add_curve_key(float3 co, float radius)
275 {
276         curve_keys.push_back_reserved(co);
277         curve_radius.push_back_reserved(radius);
278 }
279
280 void Mesh::add_curve(int first_key, int shader)
281 {
282         curve_first_key.push_back_reserved(first_key);
283         curve_shader.push_back_reserved(shader);
284 }
285
286 void Mesh::compute_bounds()
287 {
288         BoundBox bnds = BoundBox::empty;
289         size_t verts_size = verts.size();
290         size_t curve_keys_size = curve_keys.size();
291
292         if(verts_size + curve_keys_size > 0) {
293                 for(size_t i = 0; i < verts_size; i++)
294                         bnds.grow(verts[i]);
295
296                 for(size_t i = 0; i < curve_keys_size; i++)
297                         bnds.grow(curve_keys[i], curve_radius[i]);
298
299                 Attribute *attr = attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
300                 if(use_motion_blur && attr) {
301                         size_t steps_size = verts.size() * (motion_steps - 1);
302                         float3 *vert_steps = attr->data_float3();
303         
304                         for(size_t i = 0; i < steps_size; i++)
305                                 bnds.grow(vert_steps[i]);
306                 }
307
308                 Attribute *curve_attr = curve_attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
309                 if(use_motion_blur && curve_attr) {
310                         size_t steps_size = curve_keys.size() * (motion_steps - 1);
311                         float3 *key_steps = curve_attr->data_float3();
312         
313                         for(size_t i = 0; i < steps_size; i++)
314                                 bnds.grow(key_steps[i]);
315                 }
316
317                 if(!bnds.valid()) {
318                         bnds = BoundBox::empty;
319
320                         /* skip nan or inf coordinates */
321                         for(size_t i = 0; i < verts_size; i++)
322                                 bnds.grow_safe(verts[i]);
323
324                         for(size_t i = 0; i < curve_keys_size; i++)
325                                 bnds.grow_safe(curve_keys[i], curve_radius[i]);
326                         
327                         if(use_motion_blur && attr) {
328                                 size_t steps_size = verts.size() * (motion_steps - 1);
329                                 float3 *vert_steps = attr->data_float3();
330                 
331                                 for(size_t i = 0; i < steps_size; i++)
332                                         bnds.grow_safe(vert_steps[i]);
333                         }
334
335                         if(use_motion_blur && curve_attr) {
336                                 size_t steps_size = curve_keys.size() * (motion_steps - 1);
337                                 float3 *key_steps = curve_attr->data_float3();
338                 
339                                 for(size_t i = 0; i < steps_size; i++)
340                                         bnds.grow_safe(key_steps[i]);
341                         }
342                 }
343         }
344
345         if(!bnds.valid()) {
346                 /* empty mesh */
347                 bnds.grow(make_float3(0.0f, 0.0f, 0.0f));
348         }
349
350         bounds = bnds;
351 }
352
353 static float3 compute_face_normal(const Mesh::Triangle& t, float3 *verts)
354 {
355         float3 v0 = verts[t.v[0]];
356         float3 v1 = verts[t.v[1]];
357         float3 v2 = verts[t.v[2]];
358
359         float3 norm = cross(v1 - v0, v2 - v0);
360         float normlen = len(norm);
361
362         if(normlen == 0.0f)
363                 return make_float3(1.0f, 0.0f, 0.0f);
364
365         return norm / normlen;
366 }
367
368 void Mesh::add_face_normals()
369 {
370         /* don't compute if already there */
371         if(attributes.find(ATTR_STD_FACE_NORMAL))
372                 return;
373         
374         /* get attributes */
375         Attribute *attr_fN = attributes.add(ATTR_STD_FACE_NORMAL);
376         float3 *fN = attr_fN->data_float3();
377
378         /* compute face normals */
379         size_t triangles_size = num_triangles();
380         bool flip = transform_negative_scaled;
381
382         if(triangles_size) {
383                 float3 *verts_ptr = &verts[0];
384
385                 for(size_t i = 0; i < triangles_size; i++) {
386                         fN[i] = compute_face_normal(get_triangle(i), verts_ptr);
387
388                         if(flip)
389                                 fN[i] = -fN[i];
390                 }
391         }
392
393         /* expected to be in local space */
394         if(transform_applied) {
395                 Transform ntfm = transform_inverse(transform_normal);
396
397                 for(size_t i = 0; i < triangles_size; i++)
398                         fN[i] = normalize(transform_direction(&ntfm, fN[i]));
399         }
400 }
401
402 void Mesh::add_vertex_normals()
403 {
404         bool flip = transform_negative_scaled;
405         size_t verts_size = verts.size();
406         size_t triangles_size = num_triangles();
407
408         /* static vertex normals */
409         if(!attributes.find(ATTR_STD_VERTEX_NORMAL)) {
410                 /* get attributes */
411                 Attribute *attr_fN = attributes.find(ATTR_STD_FACE_NORMAL);
412                 Attribute *attr_vN = attributes.add(ATTR_STD_VERTEX_NORMAL);
413
414                 float3 *fN = attr_fN->data_float3();
415                 float3 *vN = attr_vN->data_float3();
416
417                 /* compute vertex normals */
418                 memset(vN, 0, verts.size()*sizeof(float3));
419
420                 if(triangles_size) {
421
422                         for(size_t i = 0; i < triangles_size; i++)
423                                 for(size_t j = 0; j < 3; j++)
424                                         vN[get_triangle(i).v[j]] += fN[i];
425                 }
426
427                 for(size_t i = 0; i < verts_size; i++) {
428                         vN[i] = normalize(vN[i]);
429                         if(flip)
430                                 vN[i] = -vN[i];
431                 }
432         }
433
434         /* motion vertex normals */
435         Attribute *attr_mP = attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
436         Attribute *attr_mN = attributes.find(ATTR_STD_MOTION_VERTEX_NORMAL);
437
438         if(has_motion_blur() && attr_mP && !attr_mN) {
439                 /* create attribute */
440                 attr_mN = attributes.add(ATTR_STD_MOTION_VERTEX_NORMAL);
441
442                 for(int step = 0; step < motion_steps - 1; step++) {
443                         float3 *mP = attr_mP->data_float3() + step*verts.size();
444                         float3 *mN = attr_mN->data_float3() + step*verts.size();
445
446                         /* compute */
447                         memset(mN, 0, verts.size()*sizeof(float3));
448
449                         if(triangles_size) {
450                                 for(size_t i = 0; i < triangles_size; i++) {
451                                         for(size_t j = 0; j < 3; j++) {
452                                                 float3 fN = compute_face_normal(get_triangle(i), mP);
453                                                 mN[get_triangle(i).v[j]] += fN;
454                                         }
455                                 }
456                         }
457
458                         for(size_t i = 0; i < verts_size; i++) {
459                                 mN[i] = normalize(mN[i]);
460                                 if(flip)
461                                         mN[i] = -mN[i];
462                         }
463                 }
464         }
465 }
466
467 void Mesh::pack_normals(Scene *scene, uint *tri_shader, float4 *vnormal)
468 {
469         Attribute *attr_vN = attributes.find(ATTR_STD_VERTEX_NORMAL);
470
471         float3 *vN = attr_vN->data_float3();
472         uint shader_id = 0;
473         uint last_shader = -1;
474         bool last_smooth = false;
475
476         size_t triangles_size = num_triangles();
477         int *shader_ptr = (shader.size())? &shader[0]: NULL;
478
479         bool do_transform = transform_applied;
480         Transform ntfm = transform_normal;
481
482         /* save shader */
483         for(size_t i = 0; i < triangles_size; i++) {
484                 if(shader_ptr[i] != last_shader || last_smooth != smooth[i]) {
485                         last_shader = shader_ptr[i];
486                         last_smooth = smooth[i];
487                         Shader *shader = (last_shader < used_shaders.size()) ?
488                                 used_shaders[last_shader] : scene->default_surface;
489                         shader_id = scene->shader_manager->get_shader_id(shader, this, last_smooth);
490                 }
491
492                 tri_shader[i] = shader_id;
493         }
494
495         size_t verts_size = verts.size();
496
497         for(size_t i = 0; i < verts_size; i++) {
498                 float3 vNi = vN[i];
499
500                 if(do_transform)
501                         vNi = normalize(transform_direction(&ntfm, vNi));
502
503                 vnormal[i] = make_float4(vNi.x, vNi.y, vNi.z, 0.0f);
504         }
505 }
506
507 void Mesh::pack_verts(const vector<uint>& tri_prim_index,
508                       uint4 *tri_vindex,
509                       size_t vert_offset,
510                       size_t tri_offset)
511 {
512         const size_t triangles_size = num_triangles();
513         if(triangles_size) {
514                 for(size_t i = 0; i < triangles_size; i++) {
515                         Triangle t = get_triangle(i);
516                         tri_vindex[i] = make_uint4(t.v[0] + vert_offset,
517                                                    t.v[1] + vert_offset,
518                                                    t.v[2] + vert_offset,
519                                                    tri_prim_index[i + tri_offset]);
520                 }
521         }
522 }
523
524 void Mesh::pack_curves(Scene *scene, float4 *curve_key_co, float4 *curve_data, size_t curvekey_offset)
525 {
526         size_t curve_keys_size = curve_keys.size();
527
528         /* pack curve keys */
529         if(curve_keys_size) {
530                 float3 *keys_ptr = &curve_keys[0];
531                 float *radius_ptr = &curve_radius[0];
532
533                 for(size_t i = 0; i < curve_keys_size; i++)
534                         curve_key_co[i] = make_float4(keys_ptr[i].x, keys_ptr[i].y, keys_ptr[i].z, radius_ptr[i]);
535         }
536
537         /* pack curve segments */
538         size_t curve_num = num_curves();
539
540         if(curve_num) {
541                 for(size_t i = 0; i < curve_num; i++) {
542                         Curve curve = get_curve(i);
543                         int shader_id = curve_shader[i];
544                         Shader *shader = (shader_id < used_shaders.size()) ?
545                                 used_shaders[shader_id] : scene->default_surface;
546                         shader_id = scene->shader_manager->get_shader_id(shader, this, false);
547
548                         curve_data[i] = make_float4(
549                                 __int_as_float(curve.first_key + curvekey_offset),
550                                 __int_as_float(curve.num_keys),
551                                 __int_as_float(shader_id),
552                                 0.0f);
553                 }
554         }
555 }
556
557 void Mesh::compute_bvh(DeviceScene *dscene,
558                        SceneParams *params,
559                        Progress *progress,
560                        int n,
561                        int total)
562 {
563         if(progress->get_cancel())
564                 return;
565
566         compute_bounds();
567
568         if(need_build_bvh()) {
569                 string msg = "Updating Mesh BVH ";
570                 if(name == "")
571                         msg += string_printf("%u/%u", (uint)(n+1), (uint)total);
572                 else
573                         msg += string_printf("%s %u/%u", name.c_str(), (uint)(n+1), (uint)total);
574
575                 Object object;
576                 object.mesh = this;
577
578                 vector<Object*> objects;
579                 objects.push_back(&object);
580
581                 if(bvh && !need_update_rebuild) {
582                         progress->set_status(msg, "Refitting BVH");
583                         bvh->objects = objects;
584                         bvh->refit(*progress);
585                 }
586                 else {
587                         progress->set_status(msg, "Building BVH");
588
589                         BVHParams bparams;
590                         bparams.use_spatial_split = params->use_bvh_spatial_split;
591                         bparams.use_qbvh = params->use_qbvh;
592                         bparams.use_unaligned_nodes = dscene->data.bvh.have_curves &&
593                                                       params->use_bvh_unaligned_nodes;
594
595                         delete bvh;
596                         bvh = BVH::create(bparams, objects);
597                         MEM_GUARDED_CALL(progress, bvh->build, *progress);
598                 }
599         }
600
601         need_update = false;
602         need_update_rebuild = false;
603 }
604
605 void Mesh::tag_update(Scene *scene, bool rebuild)
606 {
607         need_update = true;
608
609         if(rebuild) {
610                 need_update_rebuild = true;
611                 scene->light_manager->need_update = true;
612         }
613         else {
614                 foreach(Shader *shader, used_shaders)
615                         if(shader->has_surface_emission)
616                                 scene->light_manager->need_update = true;
617         }
618
619         scene->mesh_manager->need_update = true;
620         scene->object_manager->need_update = true;
621 }
622
623 bool Mesh::has_motion_blur() const
624 {
625         return (use_motion_blur &&
626                 (attributes.find(ATTR_STD_MOTION_VERTEX_POSITION) ||
627                  curve_attributes.find(ATTR_STD_MOTION_VERTEX_POSITION)));
628 }
629
630 bool Mesh::need_build_bvh() const
631 {
632         return !transform_applied || has_surface_bssrdf;
633 }
634
635 bool Mesh::is_instanced() const
636 {
637         /* Currently we treat subsurface objects as instanced.
638          *
639          * While it might be not very optimal for ray traversal, it avoids having
640          * duplicated BVH in the memory, saving quite some space.
641          */
642         return !transform_applied || has_surface_bssrdf;
643 }
644
645 /* Mesh Manager */
646
647 MeshManager::MeshManager()
648 {
649         bvh = NULL;
650         need_update = true;
651         need_flags_update = true;
652 }
653
654 MeshManager::~MeshManager()
655 {
656         delete bvh;
657 }
658
659 void MeshManager::update_osl_attributes(Device *device, Scene *scene, vector<AttributeRequestSet>& mesh_attributes)
660 {
661 #ifdef WITH_OSL
662         /* for OSL, a hash map is used to lookup the attribute by name. */
663         OSLGlobals *og = (OSLGlobals*)device->osl_memory();
664
665         og->object_name_map.clear();
666         og->attribute_map.clear();
667         og->object_names.clear();
668
669         og->attribute_map.resize(scene->objects.size()*ATTR_PRIM_TYPES);
670
671         for(size_t i = 0; i < scene->objects.size(); i++) {
672                 /* set object name to object index map */
673                 Object *object = scene->objects[i];
674                 og->object_name_map[object->name] = i;
675                 og->object_names.push_back(object->name);
676
677                 /* set object attributes */
678                 foreach(ParamValue& attr, object->attributes) {
679                         OSLGlobals::Attribute osl_attr;
680
681                         osl_attr.type = attr.type();
682                         osl_attr.elem = ATTR_ELEMENT_OBJECT;
683                         osl_attr.value = attr;
684                         osl_attr.offset = 0;
685
686                         og->attribute_map[i*ATTR_PRIM_TYPES][attr.name()] = osl_attr;
687                         og->attribute_map[i*ATTR_PRIM_TYPES + ATTR_PRIM_CURVE][attr.name()] = osl_attr;
688                 }
689
690                 /* find mesh attributes */
691                 size_t j;
692
693                 for(j = 0; j < scene->meshes.size(); j++)
694                         if(scene->meshes[j] == object->mesh)
695                                 break;
696
697                 AttributeRequestSet& attributes = mesh_attributes[j];
698
699                 /* set object attributes */
700                 foreach(AttributeRequest& req, attributes.requests) {
701                         OSLGlobals::Attribute osl_attr;
702
703                         if(req.triangle_element != ATTR_ELEMENT_NONE) {
704                                 osl_attr.elem = req.triangle_element;
705                                 osl_attr.offset = req.triangle_offset;
706
707                                 if(req.triangle_type == TypeDesc::TypeFloat)
708                                         osl_attr.type = TypeDesc::TypeFloat;
709                                 else if(req.triangle_type == TypeDesc::TypeMatrix)
710                                         osl_attr.type = TypeDesc::TypeMatrix;
711                                 else
712                                         osl_attr.type = TypeDesc::TypeColor;
713
714                                 if(req.std != ATTR_STD_NONE) {
715                                         /* if standard attribute, add lookup by geom: name convention */
716                                         ustring stdname(string("geom:") + string(Attribute::standard_name(req.std)));
717                                         og->attribute_map[i*ATTR_PRIM_TYPES][stdname] = osl_attr;
718                                 }
719                                 else if(req.name != ustring()) {
720                                         /* add lookup by mesh attribute name */
721                                         og->attribute_map[i*ATTR_PRIM_TYPES][req.name] = osl_attr;
722                                 }
723                         }
724
725                         if(req.curve_element != ATTR_ELEMENT_NONE) {
726                                 osl_attr.elem = req.curve_element;
727                                 osl_attr.offset = req.curve_offset;
728
729                                 if(req.curve_type == TypeDesc::TypeFloat)
730                                         osl_attr.type = TypeDesc::TypeFloat;
731                                 else if(req.curve_type == TypeDesc::TypeMatrix)
732                                         osl_attr.type = TypeDesc::TypeMatrix;
733                                 else
734                                         osl_attr.type = TypeDesc::TypeColor;
735
736                                 if(req.std != ATTR_STD_NONE) {
737                                         /* if standard attribute, add lookup by geom: name convention */
738                                         ustring stdname(string("geom:") + string(Attribute::standard_name(req.std)));
739                                         og->attribute_map[i*ATTR_PRIM_TYPES + ATTR_PRIM_CURVE][stdname] = osl_attr;
740                                 }
741                                 else if(req.name != ustring()) {
742                                         /* add lookup by mesh attribute name */
743                                         og->attribute_map[i*ATTR_PRIM_TYPES + ATTR_PRIM_CURVE][req.name] = osl_attr;
744                                 }
745                         }
746                 }
747         }
748 #else
749         (void)device;
750         (void)scene;
751         (void)mesh_attributes;
752 #endif
753 }
754
755 void MeshManager::update_svm_attributes(Device *device, DeviceScene *dscene, Scene *scene, vector<AttributeRequestSet>& mesh_attributes)
756 {
757         /* for SVM, the attributes_map table is used to lookup the offset of an
758          * attribute, based on a unique shader attribute id. */
759
760         /* compute array stride */
761         int attr_map_stride = 0;
762
763         for(size_t i = 0; i < scene->meshes.size(); i++)
764                 attr_map_stride = max(attr_map_stride, (mesh_attributes[i].size() + 1)*ATTR_PRIM_TYPES);
765
766         if(attr_map_stride == 0)
767                 return;
768         
769         /* create attribute map */
770         uint4 *attr_map = dscene->attributes_map.resize(attr_map_stride*scene->objects.size());
771         memset(attr_map, 0, dscene->attributes_map.size()*sizeof(uint));
772
773         for(size_t i = 0; i < scene->objects.size(); i++) {
774                 Object *object = scene->objects[i];
775                 Mesh *mesh = object->mesh;
776
777                 /* find mesh attributes */
778                 size_t j;
779
780                 for(j = 0; j < scene->meshes.size(); j++)
781                         if(scene->meshes[j] == mesh)
782                                 break;
783
784                 AttributeRequestSet& attributes = mesh_attributes[j];
785
786                 /* set object attributes */
787                 int index = i*attr_map_stride;
788
789                 foreach(AttributeRequest& req, attributes.requests) {
790                         uint id;
791
792                         if(req.std == ATTR_STD_NONE)
793                                 id = scene->shader_manager->get_attribute_id(req.name);
794                         else
795                                 id = scene->shader_manager->get_attribute_id(req.std);
796
797                         if(mesh->num_triangles()) {
798                                 attr_map[index].x = id;
799                                 attr_map[index].y = req.triangle_element;
800                                 attr_map[index].z = as_uint(req.triangle_offset);
801
802                                 if(req.triangle_type == TypeDesc::TypeFloat)
803                                         attr_map[index].w = NODE_ATTR_FLOAT;
804                                 else if(req.triangle_type == TypeDesc::TypeMatrix)
805                                         attr_map[index].w = NODE_ATTR_MATRIX;
806                                 else
807                                         attr_map[index].w = NODE_ATTR_FLOAT3;
808                         }
809
810                         index++;
811
812                         if(mesh->num_curves()) {
813                                 attr_map[index].x = id;
814                                 attr_map[index].y = req.curve_element;
815                                 attr_map[index].z = as_uint(req.curve_offset);
816
817                                 if(req.curve_type == TypeDesc::TypeFloat)
818                                         attr_map[index].w = NODE_ATTR_FLOAT;
819                                 else if(req.curve_type == TypeDesc::TypeMatrix)
820                                         attr_map[index].w = NODE_ATTR_MATRIX;
821                                 else
822                                         attr_map[index].w = NODE_ATTR_FLOAT3;
823                         }
824
825                         index++;
826                 }
827
828                 /* terminator */
829                 attr_map[index].x = ATTR_STD_NONE;
830                 attr_map[index].y = 0;
831                 attr_map[index].z = 0;
832                 attr_map[index].w = 0;
833
834                 index++;
835
836                 attr_map[index].x = ATTR_STD_NONE;
837                 attr_map[index].y = 0;
838                 attr_map[index].z = 0;
839                 attr_map[index].w = 0;
840
841                 index++;
842         }
843
844         /* copy to device */
845         dscene->data.bvh.attributes_map_stride = attr_map_stride;
846         device->tex_alloc("__attributes_map", dscene->attributes_map);
847 }
848
849 static void update_attribute_element_size(Mesh *mesh,
850                                           Attribute *mattr,
851                                           size_t *attr_float_size,
852                                           size_t *attr_float3_size,
853                                           size_t *attr_uchar4_size)
854 {
855         if(mattr) {
856                 size_t size = mattr->element_size(
857                         mesh->verts.size(),
858                         mesh->num_triangles(),
859                         mesh->motion_steps,
860                         mesh->num_curves(),
861                         mesh->curve_keys.size());
862
863                 if(mattr->element == ATTR_ELEMENT_VOXEL) {
864                         /* pass */
865                 }
866                 else if(mattr->element == ATTR_ELEMENT_CORNER_BYTE) {
867                         *attr_uchar4_size += size;
868                 }
869                 else if(mattr->type == TypeDesc::TypeFloat) {
870                         *attr_float_size += size;
871                 }
872                 else if(mattr->type == TypeDesc::TypeMatrix) {
873                         *attr_float3_size += size * 4;
874                 }
875                 else {
876                         *attr_float3_size += size;
877                 }
878         }
879 }
880
881 static void update_attribute_element_offset(Mesh *mesh,
882                                             vector<float>& attr_float,
883                                             size_t& attr_float_offset,
884                                             vector<float4>& attr_float3,
885                                             size_t& attr_float3_offset,
886                                             vector<uchar4>& attr_uchar4,
887                                             size_t& attr_uchar4_offset,
888                                             Attribute *mattr,
889                                             TypeDesc& type,
890                                             int& offset,
891                                             AttributeElement& element)
892 {
893         if(mattr) {
894                 /* store element and type */
895                 element = mattr->element;
896                 type = mattr->type;
897
898                 /* store attribute data in arrays */
899                 size_t size = mattr->element_size(
900                         mesh->verts.size(),
901                         mesh->num_triangles(),
902                         mesh->motion_steps,
903                         mesh->num_curves(),
904                         mesh->curve_keys.size());
905
906                 if(mattr->element == ATTR_ELEMENT_VOXEL) {
907                         /* store slot in offset value */
908                         VoxelAttribute *voxel_data = mattr->data_voxel();
909                         offset = voxel_data->slot;
910                 }
911                 else if(mattr->element == ATTR_ELEMENT_CORNER_BYTE) {
912                         uchar4 *data = mattr->data_uchar4();
913                         offset = attr_uchar4_offset;
914
915                         assert(attr_uchar4.capacity() >= offset + size);
916                         for(size_t k = 0; k < size; k++) {
917                                 attr_uchar4[offset+k] = data[k];
918                         }
919                         attr_uchar4_offset += size;
920                 }
921                 else if(mattr->type == TypeDesc::TypeFloat) {
922                         float *data = mattr->data_float();
923                         offset = attr_float_offset;
924
925                         assert(attr_float.capacity() >= offset + size);
926                         for(size_t k = 0; k < size; k++) {
927                                 attr_float[offset+k] = data[k];
928                         }
929                         attr_float_offset += size;
930                 }
931                 else if(mattr->type == TypeDesc::TypeMatrix) {
932                         Transform *tfm = mattr->data_transform();
933                         offset = attr_float3_offset;
934
935                         assert(attr_float3.capacity() >= offset + size * 4);
936                         for(size_t k = 0; k < size*4; k++) {
937                                 attr_float3[offset+k] = (&tfm->x)[k];
938                         }
939                         attr_float3_offset += size * 4;
940                 }
941                 else {
942                         float4 *data = mattr->data_float4();
943                         offset = attr_float3_offset;
944
945                         assert(attr_float3.capacity() >= offset + size);
946                         for(size_t k = 0; k < size; k++) {
947                                 attr_float3[offset+k] = data[k];
948                         }
949                         attr_float3_offset += size;
950                 }
951
952                 /* mesh vertex/curve index is global, not per object, so we sneak
953                  * a correction for that in here */
954                 if(element == ATTR_ELEMENT_VERTEX)
955                         offset -= mesh->vert_offset;
956                 else if(element == ATTR_ELEMENT_VERTEX_MOTION)
957                         offset -= mesh->vert_offset;
958                 else if(element == ATTR_ELEMENT_FACE)
959                         offset -= mesh->tri_offset;
960                 else if(element == ATTR_ELEMENT_CORNER || element == ATTR_ELEMENT_CORNER_BYTE)
961                         offset -= 3*mesh->tri_offset;
962                 else if(element == ATTR_ELEMENT_CURVE)
963                         offset -= mesh->curve_offset;
964                 else if(element == ATTR_ELEMENT_CURVE_KEY)
965                         offset -= mesh->curvekey_offset;
966                 else if(element == ATTR_ELEMENT_CURVE_KEY_MOTION)
967                         offset -= mesh->curvekey_offset;
968         }
969         else {
970                 /* attribute not found */
971                 element = ATTR_ELEMENT_NONE;
972                 offset = 0;
973         }
974 }
975
976 void MeshManager::device_update_attributes(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
977 {
978         progress.set_status("Updating Mesh", "Computing attributes");
979
980         /* gather per mesh requested attributes. as meshes may have multiple
981          * shaders assigned, this merges the requested attributes that have
982          * been set per shader by the shader manager */
983         vector<AttributeRequestSet> mesh_attributes(scene->meshes.size());
984
985         for(size_t i = 0; i < scene->meshes.size(); i++) {
986                 Mesh *mesh = scene->meshes[i];
987
988                 scene->need_global_attributes(mesh_attributes[i]);
989
990                 foreach(Shader *shader, mesh->used_shaders) {
991                         mesh_attributes[i].add(shader->attributes);
992                 }
993         }
994
995         /* mesh attribute are stored in a single array per data type. here we fill
996          * those arrays, and set the offset and element type to create attribute
997          * maps next */
998
999         /* Pre-allocate attributes to avoid arrays re-allocation which would
1000          * take 2x of overall attribute memory usage.
1001          */
1002         size_t attr_float_size = 0;
1003         size_t attr_float3_size = 0;
1004         size_t attr_uchar4_size = 0;
1005         for(size_t i = 0; i < scene->meshes.size(); i++) {
1006                 Mesh *mesh = scene->meshes[i];
1007                 AttributeRequestSet& attributes = mesh_attributes[i];
1008                 foreach(AttributeRequest& req, attributes.requests) {
1009                         Attribute *triangle_mattr = mesh->attributes.find(req);
1010                         Attribute *curve_mattr = mesh->curve_attributes.find(req);
1011
1012                         /* todo: get rid of this exception, it's only here for giving some
1013                          * working texture coordinate for subdivision as we can't preserve
1014                          * any attributes yet */
1015                         if(!triangle_mattr && req.std == ATTR_STD_GENERATED) {
1016                                 triangle_mattr = mesh->attributes.add(ATTR_STD_GENERATED);
1017                                 if(mesh->verts.size())
1018                                         memcpy(triangle_mattr->data_float3(), &mesh->verts[0], sizeof(float3)*mesh->verts.size());
1019                         }
1020
1021                         update_attribute_element_size(mesh,
1022                                                       triangle_mattr,
1023                                                       &attr_float_size,
1024                                                       &attr_float3_size,
1025                                                       &attr_uchar4_size);
1026                         update_attribute_element_size(mesh,
1027                                                       curve_mattr,
1028                                                       &attr_float_size,
1029                                                       &attr_float3_size,
1030                                                       &attr_uchar4_size);
1031                 }
1032         }
1033
1034         vector<float> attr_float(attr_float_size);
1035         vector<float4> attr_float3(attr_float3_size);
1036         vector<uchar4> attr_uchar4(attr_uchar4_size);
1037
1038         size_t attr_float_offset = 0;
1039         size_t attr_float3_offset = 0;
1040         size_t attr_uchar4_offset = 0;
1041
1042         /* Fill in attributes. */
1043         for(size_t i = 0; i < scene->meshes.size(); i++) {
1044                 Mesh *mesh = scene->meshes[i];
1045                 AttributeRequestSet& attributes = mesh_attributes[i];
1046
1047                 /* todo: we now store std and name attributes from requests even if
1048                  * they actually refer to the same mesh attributes, optimize */
1049                 foreach(AttributeRequest& req, attributes.requests) {
1050                         Attribute *triangle_mattr = mesh->attributes.find(req);
1051                         Attribute *curve_mattr = mesh->curve_attributes.find(req);
1052
1053                         update_attribute_element_offset(mesh,
1054                                                         attr_float, attr_float_offset,
1055                                                         attr_float3, attr_float3_offset,
1056                                                         attr_uchar4, attr_uchar4_offset,
1057                                                         triangle_mattr,
1058                                                         req.triangle_type,
1059                                                         req.triangle_offset,
1060                                                         req.triangle_element);
1061
1062                         update_attribute_element_offset(mesh,
1063                                                         attr_float, attr_float_offset,
1064                                                         attr_float3, attr_float3_offset,
1065                                                         attr_uchar4, attr_uchar4_offset,
1066                                                         curve_mattr,
1067                                                         req.curve_type,
1068                                                         req.curve_offset,
1069                                                         req.curve_element);
1070
1071                         if(progress.get_cancel()) return;
1072                 }
1073         }
1074
1075         /* create attribute lookup maps */
1076         if(scene->shader_manager->use_osl())
1077                 update_osl_attributes(device, scene, mesh_attributes);
1078
1079         update_svm_attributes(device, dscene, scene, mesh_attributes);
1080
1081         if(progress.get_cancel()) return;
1082
1083         /* copy to device */
1084         progress.set_status("Updating Mesh", "Copying Attributes to device");
1085
1086         if(attr_float.size()) {
1087                 dscene->attributes_float.copy(&attr_float[0], attr_float.size());
1088                 device->tex_alloc("__attributes_float", dscene->attributes_float);
1089         }
1090         if(attr_float3.size()) {
1091                 dscene->attributes_float3.copy(&attr_float3[0], attr_float3.size());
1092                 device->tex_alloc("__attributes_float3", dscene->attributes_float3);
1093         }
1094         if(attr_uchar4.size()) {
1095                 dscene->attributes_uchar4.copy(&attr_uchar4[0], attr_uchar4.size());
1096                 device->tex_alloc("__attributes_uchar4", dscene->attributes_uchar4);
1097         }
1098 }
1099
1100 void MeshManager::mesh_calc_offset(Scene *scene)
1101 {
1102         size_t vert_size = 0;
1103         size_t tri_size = 0;
1104         size_t curve_key_size = 0;
1105         size_t curve_size = 0;
1106
1107         foreach(Mesh *mesh, scene->meshes) {
1108                 mesh->vert_offset = vert_size;
1109                 mesh->tri_offset = tri_size;
1110                 mesh->curvekey_offset = curve_key_size;
1111                 mesh->curve_offset = curve_size;
1112
1113                 vert_size += mesh->verts.size();
1114                 tri_size += mesh->num_triangles();
1115                 curve_key_size += mesh->curve_keys.size();
1116                 curve_size += mesh->num_curves();
1117         }
1118 }
1119
1120 void MeshManager::device_update_mesh(Device *device,
1121                                      DeviceScene *dscene,
1122                                      Scene *scene,
1123                                      bool for_displacement,
1124                                      Progress& progress)
1125 {
1126         /* Count. */
1127         size_t vert_size = 0;
1128         size_t tri_size = 0;
1129         size_t curve_key_size = 0;
1130         size_t curve_size = 0;
1131         foreach(Mesh *mesh, scene->meshes) {
1132                 vert_size += mesh->verts.size();
1133                 tri_size += mesh->num_triangles();
1134                 curve_key_size += mesh->curve_keys.size();
1135                 curve_size += mesh->num_curves();
1136         }
1137         /* Create mapping from triangle to primitive triangle array. */
1138         vector<uint> tri_prim_index(tri_size);
1139         if(for_displacement) {
1140                 /* For displacement kernels we do some trickery to make them believe
1141                  * we've got all required data ready. However, that data is different
1142                  * from final render kernels since we don't have BVH yet, so can't
1143                  * really use same semantic of arrays.
1144                  */
1145                 foreach(Mesh *mesh, scene->meshes) {
1146                         for(size_t i = 0; i < mesh->num_triangles(); ++i) {
1147                                 tri_prim_index[i + mesh->tri_offset] = 3 * (i + mesh->tri_offset);
1148                         }
1149                 }
1150         }
1151         else {
1152                 PackedBVH& pack = bvh->pack;
1153                 for(size_t i = 0; i < pack.prim_index.size(); ++i) {
1154                         if ((pack.prim_type[i] & PRIMITIVE_ALL_TRIANGLE) != 0) {
1155                                 tri_prim_index[pack.prim_index[i]] = pack.prim_tri_index[i];
1156                         }
1157                 }
1158         }
1159         /* Fill in all the arrays. */
1160         if(tri_size != 0) {
1161                 /* normals */
1162                 progress.set_status("Updating Mesh", "Computing normals");
1163
1164                 uint *tri_shader = dscene->tri_shader.resize(tri_size);
1165                 float4 *vnormal = dscene->tri_vnormal.resize(vert_size);
1166                 uint4 *tri_vindex = dscene->tri_vindex.resize(tri_size);
1167
1168                 foreach(Mesh *mesh, scene->meshes) {
1169                         mesh->pack_normals(scene,
1170                                            &tri_shader[mesh->tri_offset],
1171                                            &vnormal[mesh->vert_offset]);
1172                         mesh->pack_verts(tri_prim_index,
1173                                          &tri_vindex[mesh->tri_offset],
1174                                          mesh->vert_offset,
1175                                          mesh->tri_offset);
1176                         if(progress.get_cancel()) return;
1177                 }
1178
1179                 /* vertex coordinates */
1180                 progress.set_status("Updating Mesh", "Copying Mesh to device");
1181
1182                 device->tex_alloc("__tri_shader", dscene->tri_shader);
1183                 device->tex_alloc("__tri_vnormal", dscene->tri_vnormal);
1184                 device->tex_alloc("__tri_vindex", dscene->tri_vindex);
1185         }
1186         if(curve_size != 0) {
1187                 progress.set_status("Updating Mesh", "Copying Strands to device");
1188
1189                 float4 *curve_keys = dscene->curve_keys.resize(curve_key_size);
1190                 float4 *curves = dscene->curves.resize(curve_size);
1191
1192                 foreach(Mesh *mesh, scene->meshes) {
1193                         mesh->pack_curves(scene, &curve_keys[mesh->curvekey_offset], &curves[mesh->curve_offset], mesh->curvekey_offset);
1194                         if(progress.get_cancel()) return;
1195                 }
1196
1197                 device->tex_alloc("__curve_keys", dscene->curve_keys);
1198                 device->tex_alloc("__curves", dscene->curves);
1199         }
1200         if(for_displacement) {
1201                 float4 *prim_tri_verts = dscene->prim_tri_verts.resize(tri_size * 3);
1202                 foreach(Mesh *mesh, scene->meshes) {
1203                         for(size_t i = 0; i < mesh->num_triangles(); ++i) {
1204                                 Mesh::Triangle t = mesh->get_triangle(i);
1205                                 size_t offset = 3 * (i + mesh->tri_offset);
1206                                 prim_tri_verts[offset + 0] = float3_to_float4(mesh->verts[t.v[0]]);
1207                                 prim_tri_verts[offset + 1] = float3_to_float4(mesh->verts[t.v[1]]);
1208                                 prim_tri_verts[offset + 2] = float3_to_float4(mesh->verts[t.v[2]]);
1209                         }
1210                 }
1211                 device->tex_alloc("__prim_tri_verts", dscene->prim_tri_verts);
1212         }
1213 }
1214
1215 void MeshManager::device_update_bvh(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
1216 {
1217         /* bvh build */
1218         progress.set_status("Updating Scene BVH", "Building");
1219
1220         VLOG(1) << (scene->params.use_qbvh ? "Using QBVH optimization structure"
1221                                            : "Using regular BVH optimization structure");
1222
1223         BVHParams bparams;
1224         bparams.top_level = true;
1225         bparams.use_qbvh = scene->params.use_qbvh;
1226         bparams.use_spatial_split = scene->params.use_bvh_spatial_split;
1227         bparams.use_unaligned_nodes = dscene->data.bvh.have_curves &&
1228                                       scene->params.use_bvh_unaligned_nodes;
1229
1230         delete bvh;
1231         bvh = BVH::create(bparams, scene->objects);
1232         bvh->build(progress);
1233
1234         if(progress.get_cancel()) return;
1235
1236         /* copy to device */
1237         progress.set_status("Updating Scene BVH", "Copying BVH to device");
1238
1239         PackedBVH& pack = bvh->pack;
1240
1241         if(pack.nodes.size()) {
1242                 dscene->bvh_nodes.reference((float4*)&pack.nodes[0], pack.nodes.size());
1243                 device->tex_alloc("__bvh_nodes", dscene->bvh_nodes);
1244         }
1245         if(pack.leaf_nodes.size()) {
1246                 dscene->bvh_leaf_nodes.reference((float4*)&pack.leaf_nodes[0], pack.leaf_nodes.size());
1247                 device->tex_alloc("__bvh_leaf_nodes", dscene->bvh_leaf_nodes);
1248         }
1249         if(pack.object_node.size()) {
1250                 dscene->object_node.reference((uint*)&pack.object_node[0], pack.object_node.size());
1251                 device->tex_alloc("__object_node", dscene->object_node);
1252         }
1253         if(pack.prim_tri_index.size()) {
1254                 dscene->prim_tri_index.reference((uint*)&pack.prim_tri_index[0], pack.prim_tri_index.size());
1255                 device->tex_alloc("__prim_tri_index", dscene->prim_tri_index);
1256         }
1257         if(pack.prim_tri_verts.size()) {
1258                 dscene->prim_tri_verts.reference((float4*)&pack.prim_tri_verts[0], pack.prim_tri_verts.size());
1259                 device->tex_alloc("__prim_tri_verts", dscene->prim_tri_verts);
1260         }
1261         if(pack.prim_type.size()) {
1262                 dscene->prim_type.reference((uint*)&pack.prim_type[0], pack.prim_type.size());
1263                 device->tex_alloc("__prim_type", dscene->prim_type);
1264         }
1265         if(pack.prim_visibility.size()) {
1266                 dscene->prim_visibility.reference((uint*)&pack.prim_visibility[0], pack.prim_visibility.size());
1267                 device->tex_alloc("__prim_visibility", dscene->prim_visibility);
1268         }
1269         if(pack.prim_index.size()) {
1270                 dscene->prim_index.reference((uint*)&pack.prim_index[0], pack.prim_index.size());
1271                 device->tex_alloc("__prim_index", dscene->prim_index);
1272         }
1273         if(pack.prim_object.size()) {
1274                 dscene->prim_object.reference((uint*)&pack.prim_object[0], pack.prim_object.size());
1275                 device->tex_alloc("__prim_object", dscene->prim_object);
1276         }
1277
1278         dscene->data.bvh.root = pack.root_index;
1279         dscene->data.bvh.use_qbvh = scene->params.use_qbvh;
1280 }
1281
1282 void MeshManager::device_update_flags(Device * /*device*/,
1283                                       DeviceScene * /*dscene*/,
1284                                       Scene * scene,
1285                                       Progress& /*progress*/)
1286 {
1287         if(!need_update && !need_flags_update) {
1288                 return;
1289         }
1290         /* update flags */
1291         foreach(Mesh *mesh, scene->meshes) {
1292                 mesh->has_volume = false;
1293                 mesh->has_mis_emission = false;
1294                 foreach(const Shader *shader, mesh->used_shaders) {
1295                         if(shader->has_volume) {
1296                                 mesh->has_volume = true;
1297                         }
1298                         if(shader->use_mis && shader->has_surface_emission) {
1299                                 mesh->has_mis_emission = true;
1300                         }
1301                         if(shader->has_surface_bssrdf) {
1302                                 mesh->has_surface_bssrdf = true;
1303                         }
1304                 }
1305         }
1306         need_flags_update = false;
1307 }
1308
1309 void MeshManager::device_update_displacement_images(Device *device,
1310                                                     DeviceScene *dscene,
1311                                                     Scene *scene,
1312                                                     Progress& progress)
1313 {
1314         progress.set_status("Updating Displacement Images");
1315         TaskPool pool;
1316         ImageManager *image_manager = scene->image_manager;
1317         set<int> bump_images;
1318         foreach(Mesh *mesh, scene->meshes) {
1319                 if(mesh->need_update) {
1320                         foreach(Shader *shader, mesh->used_shaders) {
1321                                 if(shader->graph_bump == NULL) {
1322                                         continue;
1323                                 }
1324                                 foreach(ShaderNode* node, shader->graph_bump->nodes) {
1325                                         if(node->special_type != SHADER_SPECIAL_TYPE_IMAGE_SLOT) {
1326                                                 continue;
1327                                         }
1328                                         if(device->info.pack_images) {
1329                                                 /* If device requires packed images we need to update all
1330                                                  * images now, even if they're not used for displacement.
1331                                                  */
1332                                                 image_manager->device_update(device,
1333                                                                              dscene,
1334                                                                              progress);
1335                                                 return;
1336                                         }
1337                                         ImageSlotTextureNode *image_node = static_cast<ImageSlotTextureNode*>(node);
1338                                         int slot = image_node->slot;
1339                                         if(slot != -1) {
1340                                                 bump_images.insert(slot);
1341                                         }
1342                                 }
1343                         }
1344                 }
1345         }
1346         foreach(int slot, bump_images) {
1347                 pool.push(function_bind(&ImageManager::device_update_slot,
1348                                         image_manager,
1349                                         device,
1350                                         dscene,
1351                                         slot,
1352                                         &progress));
1353         }
1354         pool.wait_work();
1355 }
1356
1357 void MeshManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
1358 {
1359         if(!need_update)
1360                 return;
1361
1362         VLOG(1) << "Total " << scene->meshes.size() << " meshes.";
1363
1364         /* Update normals. */
1365         foreach(Mesh *mesh, scene->meshes) {
1366                 foreach(Shader *shader, mesh->used_shaders) {
1367                         if(shader->need_update_attributes)
1368                                 mesh->need_update = true;
1369                 }
1370
1371                 if(mesh->need_update) {
1372                         mesh->add_face_normals();
1373                         mesh->add_vertex_normals();
1374
1375                         if(progress.get_cancel()) return;
1376                 }
1377         }
1378
1379         /* Update images needed for true displacement. */
1380         bool true_displacement_used = false;
1381         bool old_need_object_flags_update = false;
1382         foreach(Mesh *mesh, scene->meshes) {
1383                 if(mesh->need_update &&
1384                    mesh->displacement_method != Mesh::DISPLACE_BUMP)
1385                 {
1386                         true_displacement_used = true;
1387                         break;
1388                 }
1389         }
1390         if(true_displacement_used) {
1391                 VLOG(1) << "Updating images used for true displacement.";
1392                 device_update_displacement_images(device, dscene, scene, progress);
1393                 old_need_object_flags_update = scene->object_manager->need_flags_update;
1394                 scene->object_manager->device_update_flags(device,
1395                                                            dscene,
1396                                                            scene,
1397                                                            progress,
1398                                                            false);
1399         }
1400
1401         /* Device update. */
1402         device_free(device, dscene);
1403
1404         mesh_calc_offset(scene);
1405         if(true_displacement_used) {
1406                 device_update_mesh(device, dscene, scene, true, progress);
1407         }
1408         if(progress.get_cancel()) return;
1409
1410         device_update_attributes(device, dscene, scene, progress);
1411         if(progress.get_cancel()) return;
1412
1413         /* Update displacement. */
1414         bool displacement_done = false;
1415         foreach(Mesh *mesh, scene->meshes) {
1416                 if(mesh->need_update &&
1417                    displace(device, dscene, scene, mesh, progress))
1418                 {
1419                         displacement_done = true;
1420                 }
1421         }
1422
1423         /* TODO: properly handle cancel halfway displacement */
1424         if(progress.get_cancel()) return;
1425
1426         /* Device re-update after displacement. */
1427         if(displacement_done) {
1428                 device_free(device, dscene);
1429
1430                 device_update_attributes(device, dscene, scene, progress);
1431                 if(progress.get_cancel()) return;
1432         }
1433
1434         /* Update bvh. */
1435         size_t i = 0, num_bvh = 0;
1436         foreach(Mesh *mesh, scene->meshes) {
1437                 if(mesh->need_update && mesh->need_build_bvh()) {
1438                         num_bvh++;
1439                 }
1440         }
1441         TaskPool pool;
1442         foreach(Mesh *mesh, scene->meshes) {
1443                 if(mesh->need_update) {
1444                         pool.push(function_bind(&Mesh::compute_bvh,
1445                                                 mesh,
1446                                                 dscene,
1447                                                 &scene->params,
1448                                                 &progress,
1449                                                 i,
1450                                                 num_bvh));
1451                         if(mesh->need_build_bvh()) {
1452                                 i++;
1453                         }
1454                 }
1455         }
1456         TaskPool::Summary summary;
1457         pool.wait_work(&summary);
1458         VLOG(2) << "Objects BVH build pool statistics:\n"
1459                 << summary.full_report();
1460
1461         foreach(Shader *shader, scene->shaders) {
1462                 shader->need_update_attributes = false;
1463         }
1464
1465 #ifdef __OBJECT_MOTION__
1466         Scene::MotionType need_motion = scene->need_motion(device->info.advanced_shading);
1467         bool motion_blur = need_motion == Scene::MOTION_BLUR;
1468 #else
1469         bool motion_blur = false;
1470 #endif
1471
1472         /* Update objects. */
1473         vector<Object *> volume_objects;
1474         foreach(Object *object, scene->objects) {
1475                 object->compute_bounds(motion_blur);
1476         }
1477
1478         if(progress.get_cancel()) return;
1479
1480         device_update_bvh(device, dscene, scene, progress);
1481         if(progress.get_cancel()) return;
1482
1483         device_update_mesh(device, dscene, scene, false, progress);
1484         if(progress.get_cancel()) return;
1485
1486         need_update = false;
1487
1488         if(true_displacement_used) {
1489                 /* Re-tag flags for update, so they're re-evaluated
1490                  * for meshes with correct bounding boxes.
1491                  *
1492                  * This wouldn't cause wrong results, just true
1493                  * displacement might be less optimal ot calculate.
1494                  */
1495                 scene->object_manager->need_flags_update = old_need_object_flags_update;
1496         }
1497 }
1498
1499 void MeshManager::device_free(Device *device, DeviceScene *dscene)
1500 {
1501         device->tex_free(dscene->bvh_nodes);
1502         device->tex_free(dscene->bvh_leaf_nodes);
1503         device->tex_free(dscene->object_node);
1504         device->tex_free(dscene->prim_tri_verts);
1505         device->tex_free(dscene->prim_tri_index);
1506         device->tex_free(dscene->prim_type);
1507         device->tex_free(dscene->prim_visibility);
1508         device->tex_free(dscene->prim_index);
1509         device->tex_free(dscene->prim_object);
1510         device->tex_free(dscene->tri_shader);
1511         device->tex_free(dscene->tri_vnormal);
1512         device->tex_free(dscene->tri_vindex);
1513         device->tex_free(dscene->curves);
1514         device->tex_free(dscene->curve_keys);
1515         device->tex_free(dscene->attributes_map);
1516         device->tex_free(dscene->attributes_float);
1517         device->tex_free(dscene->attributes_float3);
1518         device->tex_free(dscene->attributes_uchar4);
1519
1520         dscene->bvh_nodes.clear();
1521         dscene->object_node.clear();
1522         dscene->prim_tri_verts.clear();
1523         dscene->prim_tri_index.clear();
1524         dscene->prim_type.clear();
1525         dscene->prim_visibility.clear();
1526         dscene->prim_index.clear();
1527         dscene->prim_object.clear();
1528         dscene->tri_shader.clear();
1529         dscene->tri_vnormal.clear();
1530         dscene->tri_vindex.clear();
1531         dscene->curves.clear();
1532         dscene->curve_keys.clear();
1533         dscene->attributes_map.clear();
1534         dscene->attributes_float.clear();
1535         dscene->attributes_float3.clear();
1536         dscene->attributes_uchar4.clear();
1537
1538 #ifdef WITH_OSL
1539         OSLGlobals *og = (OSLGlobals*)device->osl_memory();
1540
1541         if(og) {
1542                 og->object_name_map.clear();
1543                 og->attribute_map.clear();
1544                 og->object_names.clear();
1545         }
1546 #endif
1547 }
1548
1549 void MeshManager::tag_update(Scene *scene)
1550 {
1551         need_update = true;
1552         scene->object_manager->need_update = true;
1553 }
1554
1555 bool Mesh::need_attribute(Scene *scene, AttributeStandard std)
1556 {
1557         if(std == ATTR_STD_NONE)
1558                 return false;
1559         
1560         if(scene->need_global_attribute(std))
1561                 return true;
1562
1563         foreach(Shader *shader, used_shaders)
1564                 if(shader->attributes.find(std))
1565                         return true;
1566         
1567         return false;
1568 }
1569
1570 bool Mesh::need_attribute(Scene * /*scene*/, ustring name)
1571 {
1572         if(name == ustring())
1573                 return false;
1574
1575         foreach(Shader *shader, used_shaders)
1576                 if(shader->attributes.find(name))
1577                         return true;
1578         
1579         return false;
1580 }
1581
1582 void Mesh::tessellate(DiagSplit *split)
1583 {
1584         int num_faces = num_triangles();
1585
1586         add_face_normals();
1587         add_vertex_normals();
1588
1589         Attribute *attr_fN = attributes.find(ATTR_STD_FACE_NORMAL);
1590         float3 *fN = attr_fN->data_float3();
1591
1592         Attribute *attr_vN = attributes.find(ATTR_STD_VERTEX_NORMAL);
1593         float3 *vN = attr_vN->data_float3();
1594
1595         for(int f = 0; f < num_faces; f++) {
1596                 if(!forms_quad[f]) {
1597                         /* triangle */
1598                         LinearTrianglePatch patch;
1599                         Triangle triangle = get_triangle(f);
1600                         float3 *hull = patch.hull;
1601                         float3 *normals = patch.normals;
1602
1603                         for(int i = 0; i < 3; i++) {
1604                                 hull[i] = verts[triangle.v[i]];
1605                         }
1606
1607                         if(smooth[f]) {
1608                                 for(int i = 0; i < 3; i++) {
1609                                         normals[i] = vN[triangle.v[i]];
1610                                 }
1611                         }
1612                         else {
1613                                 for(int i = 0; i < 3; i++) {
1614                                         normals[i] = fN[f];
1615                                 }
1616                         }
1617
1618                         split->split_triangle(&patch);
1619                 }
1620                 else {
1621                         /* quad */
1622                         LinearQuadPatch patch;
1623                         Triangle triangle0 = get_triangle(f);
1624                         Triangle triangle1 = get_triangle(f+1);
1625                         float3 *hull = patch.hull;
1626                         float3 *normals = patch.normals;
1627
1628                         hull[0] = verts[triangle0.v[0]];
1629                         hull[1] = verts[triangle0.v[1]];
1630                         hull[3] = verts[triangle0.v[2]];
1631                         hull[2] = verts[triangle1.v[2]];
1632
1633                         if(smooth[f]) {
1634                                 normals[0] = vN[triangle0.v[0]];
1635                                 normals[1] = vN[triangle0.v[1]];
1636                                 normals[3] = vN[triangle0.v[2]];
1637                                 normals[2] = vN[triangle1.v[2]];
1638                         }
1639                         else {
1640                                 for(int i = 0; i < 4; i++) {
1641                                         normals[i] = fN[f];
1642                                 }
1643                         }
1644
1645                         split->split_quad(&patch);
1646
1647                         // consume second triangle in quad
1648                         f++;
1649                 }
1650
1651         }
1652 }
1653
1654 CCL_NAMESPACE_END
1655