Code cleanup to use array.data() rather than &array[0]
[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 "subd_split.h"
34 #include "subd_patch_table.h"
35
36 #include "util_foreach.h"
37 #include "util_logging.h"
38 #include "util_progress.h"
39 #include "util_set.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 /* SubdFace */
108
109 float3 Mesh::SubdFace::normal(const Mesh *mesh) const
110 {
111         float3 v0 = mesh->verts[mesh->subd_face_corners[start_corner+0]];
112         float3 v1 = mesh->verts[mesh->subd_face_corners[start_corner+1]];
113         float3 v2 = mesh->verts[mesh->subd_face_corners[start_corner+2]];
114
115         return safe_normalize(cross(v1 - v0, v2 - v0));
116 }
117
118 /* Mesh */
119
120 NODE_DEFINE(Mesh)
121 {
122         NodeType* type = NodeType::add("mesh", create);
123
124         SOCKET_UINT(motion_steps, "Motion Steps", 3);
125         SOCKET_BOOLEAN(use_motion_blur, "Use Motion Blur", false);
126
127         SOCKET_INT_ARRAY(triangles, "Triangles", array<int>());
128         SOCKET_POINT_ARRAY(verts, "Vertices", array<float3>());
129         SOCKET_INT_ARRAY(shader, "Shader", array<int>());
130         SOCKET_BOOLEAN_ARRAY(smooth, "Smooth", array<bool>());
131
132         SOCKET_POINT_ARRAY(curve_keys, "Curve Keys", array<float3>());
133         SOCKET_FLOAT_ARRAY(curve_radius, "Curve Radius", array<float>());
134         SOCKET_INT_ARRAY(curve_first_key, "Curve First Key", array<int>());
135         SOCKET_INT_ARRAY(curve_shader, "Curve Shader", array<int>());
136
137         return type;
138 }
139
140 Mesh::Mesh()
141 : Node(node_type)
142 {
143         need_update = true;
144         need_update_rebuild = false;
145         transform_applied = false;
146         transform_negative_scaled = false;
147         transform_normal = transform_identity();
148         bounds = BoundBox::empty;
149
150         bvh = NULL;
151
152         tri_offset = 0;
153         vert_offset = 0;
154
155         curve_offset = 0;
156         curvekey_offset = 0;
157
158         patch_offset = 0;
159         face_offset = 0;
160         corner_offset = 0;
161
162         num_subd_verts = 0;
163
164         attributes.triangle_mesh = this;
165         curve_attributes.curve_mesh = this;
166         subd_attributes.subd_mesh = this;
167
168         geometry_flags = GEOMETRY_NONE;
169
170         has_volume = false;
171         has_surface_bssrdf = false;
172
173         num_ngons = 0;
174
175         subdivision_type = SUBDIVISION_NONE;
176         subd_params = NULL;
177
178         patch_table = NULL;
179 }
180
181 Mesh::~Mesh()
182 {
183         delete bvh;
184         delete patch_table;
185         delete subd_params;
186 }
187
188 void Mesh::resize_mesh(int numverts, int numtris)
189 {
190         verts.resize(numverts);
191         triangles.resize(numtris * 3);
192         shader.resize(numtris);
193         smooth.resize(numtris);
194
195         if(subd_faces.size()) {
196                 triangle_patch.resize(numtris);
197                 vert_patch_uv.resize(numverts);
198         }
199
200         attributes.resize();
201 }
202
203 void Mesh::reserve_mesh(int numverts, int numtris)
204 {
205         /* reserve space to add verts and triangles later */
206         verts.reserve(numverts);
207         triangles.reserve(numtris * 3);
208         shader.reserve(numtris);
209         smooth.reserve(numtris);
210
211         if(subd_faces.size()) {
212                 triangle_patch.reserve(numtris);
213                 vert_patch_uv.reserve(numverts);
214         }
215
216         attributes.resize(true);
217 }
218
219 void Mesh::resize_curves(int numcurves, int numkeys)
220 {
221         curve_keys.resize(numkeys);
222         curve_radius.resize(numkeys);
223         curve_first_key.resize(numcurves);
224         curve_shader.resize(numcurves);
225
226         curve_attributes.resize();
227 }
228
229 void Mesh::reserve_curves(int numcurves, int numkeys)
230 {
231         curve_keys.reserve(numkeys);
232         curve_radius.reserve(numkeys);
233         curve_first_key.reserve(numcurves);
234         curve_shader.reserve(numcurves);
235
236         curve_attributes.resize(true);
237 }
238
239 void Mesh::resize_subd_faces(int numfaces, int num_ngons_, int numcorners)
240 {
241         subd_faces.resize(numfaces);
242         subd_face_corners.resize(numcorners);
243         num_ngons = num_ngons_;
244
245         subd_attributes.resize();
246 }
247
248 void Mesh::reserve_subd_faces(int numfaces, int num_ngons_, int numcorners)
249 {
250         subd_faces.reserve(numfaces);
251         subd_face_corners.reserve(numcorners);
252         num_ngons = num_ngons_;
253
254         subd_attributes.resize(true);
255 }
256
257 void Mesh::clear()
258 {
259         /* clear all verts and triangles */
260         verts.clear();
261         triangles.clear();
262         shader.clear();
263         smooth.clear();
264
265         triangle_patch.clear();
266         vert_patch_uv.clear();
267
268         curve_keys.clear();
269         curve_radius.clear();
270         curve_first_key.clear();
271         curve_shader.clear();
272
273         subd_faces.clear();
274         subd_face_corners.clear();
275
276         num_subd_verts = 0;
277
278         subd_creases.clear();
279
280         attributes.clear();
281         curve_attributes.clear();
282         subd_attributes.clear();
283         used_shaders.clear();
284
285         transform_applied = false;
286         transform_negative_scaled = false;
287         transform_normal = transform_identity();
288         geometry_flags = GEOMETRY_NONE;
289
290         delete patch_table;
291         patch_table = NULL;
292 }
293
294 int Mesh::split_vertex(int vertex)
295 {
296         /* copy vertex location and vertex attributes */
297         add_vertex_slow(verts[vertex]);
298
299         foreach(Attribute& attr, attributes.attributes) {
300                 if(attr.element == ATTR_ELEMENT_VERTEX) {
301                         array<char> tmp(attr.data_sizeof());
302                         memcpy(tmp.data(), attr.data() + tmp.size()*vertex, tmp.size());
303                         attr.add(tmp.data());
304                 }
305         }
306
307         foreach(Attribute& attr, subd_attributes.attributes) {
308                 if(attr.element == ATTR_ELEMENT_VERTEX) {
309                         array<char> tmp(attr.data_sizeof());
310                         memcpy(tmp.data(), attr.data() + tmp.size()*vertex, tmp.size());
311                         attr.add(tmp.data());
312                 }
313         }
314
315         return verts.size() - 1;
316 }
317
318 void Mesh::add_vertex(float3 P)
319 {
320         verts.push_back_reserved(P);
321
322         if(subd_faces.size()) {
323                 vert_patch_uv.push_back_reserved(make_float2(0.0f, 0.0f));
324         }
325 }
326
327 void Mesh::add_vertex_slow(float3 P)
328 {
329         verts.push_back_slow(P);
330
331         if(subd_faces.size()) {
332                 vert_patch_uv.push_back_slow(make_float2(0.0f, 0.0f));
333         }
334 }
335
336 void Mesh::add_triangle(int v0, int v1, int v2, int shader_, bool smooth_)
337 {
338         triangles.push_back_reserved(v0);
339         triangles.push_back_reserved(v1);
340         triangles.push_back_reserved(v2);
341         shader.push_back_reserved(shader_);
342         smooth.push_back_reserved(smooth_);
343
344         if(subd_faces.size()) {
345                 triangle_patch.push_back_reserved(-1);
346         }
347 }
348
349 void Mesh::add_curve_key(float3 co, float radius)
350 {
351         curve_keys.push_back_reserved(co);
352         curve_radius.push_back_reserved(radius);
353 }
354
355 void Mesh::add_curve(int first_key, int shader)
356 {
357         curve_first_key.push_back_reserved(first_key);
358         curve_shader.push_back_reserved(shader);
359 }
360
361 void Mesh::add_subd_face(int* corners, int num_corners, int shader_, bool smooth_)
362 {
363         int start_corner = subd_face_corners.size();
364
365         for(int i = 0; i < num_corners; i++) {
366                 subd_face_corners.push_back_reserved(corners[i]);
367         }
368
369         int ptex_offset = 0;
370
371         if(subd_faces.size()) {
372                 SubdFace& s = subd_faces[subd_faces.size()-1];
373                 ptex_offset = s.ptex_offset + s.num_ptex_faces();
374         }
375
376         SubdFace face = {start_corner, num_corners, shader_, smooth_, ptex_offset};
377         subd_faces.push_back_reserved(face);
378 }
379
380 void Mesh::compute_bounds()
381 {
382         BoundBox bnds = BoundBox::empty;
383         size_t verts_size = verts.size();
384         size_t curve_keys_size = curve_keys.size();
385
386         if(verts_size + curve_keys_size > 0) {
387                 for(size_t i = 0; i < verts_size; i++)
388                         bnds.grow(verts[i]);
389
390                 for(size_t i = 0; i < curve_keys_size; i++)
391                         bnds.grow(curve_keys[i], curve_radius[i]);
392
393                 Attribute *attr = attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
394                 if(use_motion_blur && attr) {
395                         size_t steps_size = verts.size() * (motion_steps - 1);
396                         float3 *vert_steps = attr->data_float3();
397         
398                         for(size_t i = 0; i < steps_size; i++)
399                                 bnds.grow(vert_steps[i]);
400                 }
401
402                 Attribute *curve_attr = curve_attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
403                 if(use_motion_blur && curve_attr) {
404                         size_t steps_size = curve_keys.size() * (motion_steps - 1);
405                         float3 *key_steps = curve_attr->data_float3();
406         
407                         for(size_t i = 0; i < steps_size; i++)
408                                 bnds.grow(key_steps[i]);
409                 }
410
411                 if(!bnds.valid()) {
412                         bnds = BoundBox::empty;
413
414                         /* skip nan or inf coordinates */
415                         for(size_t i = 0; i < verts_size; i++)
416                                 bnds.grow_safe(verts[i]);
417
418                         for(size_t i = 0; i < curve_keys_size; i++)
419                                 bnds.grow_safe(curve_keys[i], curve_radius[i]);
420                         
421                         if(use_motion_blur && attr) {
422                                 size_t steps_size = verts.size() * (motion_steps - 1);
423                                 float3 *vert_steps = attr->data_float3();
424                 
425                                 for(size_t i = 0; i < steps_size; i++)
426                                         bnds.grow_safe(vert_steps[i]);
427                         }
428
429                         if(use_motion_blur && curve_attr) {
430                                 size_t steps_size = curve_keys.size() * (motion_steps - 1);
431                                 float3 *key_steps = curve_attr->data_float3();
432                 
433                                 for(size_t i = 0; i < steps_size; i++)
434                                         bnds.grow_safe(key_steps[i]);
435                         }
436                 }
437         }
438
439         if(!bnds.valid()) {
440                 /* empty mesh */
441                 bnds.grow(make_float3(0.0f, 0.0f, 0.0f));
442         }
443
444         bounds = bnds;
445 }
446
447 static float3 compute_face_normal(const Mesh::Triangle& t, float3 *verts)
448 {
449         float3 v0 = verts[t.v[0]];
450         float3 v1 = verts[t.v[1]];
451         float3 v2 = verts[t.v[2]];
452
453         float3 norm = cross(v1 - v0, v2 - v0);
454         float normlen = len(norm);
455
456         if(normlen == 0.0f)
457                 return make_float3(1.0f, 0.0f, 0.0f);
458
459         return norm / normlen;
460 }
461
462 void Mesh::add_face_normals()
463 {
464         /* don't compute if already there */
465         if(attributes.find(ATTR_STD_FACE_NORMAL))
466                 return;
467         
468         /* get attributes */
469         Attribute *attr_fN = attributes.add(ATTR_STD_FACE_NORMAL);
470         float3 *fN = attr_fN->data_float3();
471
472         /* compute face normals */
473         size_t triangles_size = num_triangles();
474         bool flip = transform_negative_scaled;
475
476         if(triangles_size) {
477                 float3 *verts_ptr = verts.data();
478
479                 for(size_t i = 0; i < triangles_size; i++) {
480                         fN[i] = compute_face_normal(get_triangle(i), verts_ptr);
481
482                         if(flip)
483                                 fN[i] = -fN[i];
484                 }
485         }
486
487         /* expected to be in local space */
488         if(transform_applied) {
489                 Transform ntfm = transform_inverse(transform_normal);
490
491                 for(size_t i = 0; i < triangles_size; i++)
492                         fN[i] = normalize(transform_direction(&ntfm, fN[i]));
493         }
494 }
495
496 void Mesh::add_vertex_normals()
497 {
498         bool flip = transform_negative_scaled;
499         size_t verts_size = verts.size();
500         size_t triangles_size = num_triangles();
501
502         /* static vertex normals */
503         if(!attributes.find(ATTR_STD_VERTEX_NORMAL)) {
504                 /* get attributes */
505                 Attribute *attr_fN = attributes.find(ATTR_STD_FACE_NORMAL);
506                 Attribute *attr_vN = attributes.add(ATTR_STD_VERTEX_NORMAL);
507
508                 float3 *fN = attr_fN->data_float3();
509                 float3 *vN = attr_vN->data_float3();
510
511                 /* compute vertex normals */
512                 memset(vN, 0, verts.size()*sizeof(float3));
513
514                 if(triangles_size) {
515
516                         for(size_t i = 0; i < triangles_size; i++)
517                                 for(size_t j = 0; j < 3; j++)
518                                         vN[get_triangle(i).v[j]] += fN[i];
519                 }
520
521                 for(size_t i = 0; i < verts_size; i++) {
522                         vN[i] = normalize(vN[i]);
523                         if(flip)
524                                 vN[i] = -vN[i];
525                 }
526         }
527
528         /* motion vertex normals */
529         Attribute *attr_mP = attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
530         Attribute *attr_mN = attributes.find(ATTR_STD_MOTION_VERTEX_NORMAL);
531
532         if(has_motion_blur() && attr_mP && !attr_mN) {
533                 /* create attribute */
534                 attr_mN = attributes.add(ATTR_STD_MOTION_VERTEX_NORMAL);
535
536                 for(int step = 0; step < motion_steps - 1; step++) {
537                         float3 *mP = attr_mP->data_float3() + step*verts.size();
538                         float3 *mN = attr_mN->data_float3() + step*verts.size();
539
540                         /* compute */
541                         memset(mN, 0, verts.size()*sizeof(float3));
542
543                         if(triangles_size) {
544                                 for(size_t i = 0; i < triangles_size; i++) {
545                                         for(size_t j = 0; j < 3; j++) {
546                                                 float3 fN = compute_face_normal(get_triangle(i), mP);
547                                                 mN[get_triangle(i).v[j]] += fN;
548                                         }
549                                 }
550                         }
551
552                         for(size_t i = 0; i < verts_size; i++) {
553                                 mN[i] = normalize(mN[i]);
554                                 if(flip)
555                                         mN[i] = -mN[i];
556                         }
557                 }
558         }
559 }
560
561 void Mesh::pack_normals(Scene *scene, uint *tri_shader, float4 *vnormal)
562 {
563         Attribute *attr_vN = attributes.find(ATTR_STD_VERTEX_NORMAL);
564
565         float3 *vN = attr_vN->data_float3();
566         uint shader_id = 0;
567         uint last_shader = -1;
568         bool last_smooth = false;
569
570         size_t triangles_size = num_triangles();
571         int *shader_ptr = shader.data();
572
573         bool do_transform = transform_applied;
574         Transform ntfm = transform_normal;
575
576         /* save shader */
577         for(size_t i = 0; i < triangles_size; i++) {
578                 if(shader_ptr[i] != last_shader || last_smooth != smooth[i]) {
579                         last_shader = shader_ptr[i];
580                         last_smooth = smooth[i];
581                         Shader *shader = (last_shader < used_shaders.size()) ?
582                                 used_shaders[last_shader] : scene->default_surface;
583                         shader_id = scene->shader_manager->get_shader_id(shader, this, last_smooth);
584                 }
585
586                 tri_shader[i] = shader_id;
587         }
588
589         size_t verts_size = verts.size();
590
591         for(size_t i = 0; i < verts_size; i++) {
592                 float3 vNi = vN[i];
593
594                 if(do_transform)
595                         vNi = normalize(transform_direction(&ntfm, vNi));
596
597                 vnormal[i] = make_float4(vNi.x, vNi.y, vNi.z, 0.0f);
598         }
599 }
600
601 void Mesh::pack_verts(const vector<uint>& tri_prim_index,
602                       uint4 *tri_vindex,
603                       uint *tri_patch,
604                       float2 *tri_patch_uv,
605                       size_t vert_offset,
606                       size_t tri_offset)
607 {
608         size_t verts_size = verts.size();
609
610         if(verts_size && subd_faces.size()) {
611                 float2 *vert_patch_uv_ptr = vert_patch_uv.data();
612
613                 for(size_t i = 0; i < verts_size; i++) {
614                         tri_patch_uv[i] = vert_patch_uv_ptr[i];
615                 }
616         }
617
618         size_t triangles_size = num_triangles();
619
620         if(triangles_size) {
621                 for(size_t i = 0; i < triangles_size; i++) {
622                         Triangle t = get_triangle(i);
623                         tri_vindex[i] = make_uint4(t.v[0] + vert_offset,
624                                                    t.v[1] + vert_offset,
625                                                    t.v[2] + vert_offset,
626                                                    tri_prim_index[i + tri_offset]);
627
628                         tri_patch[i] = (!subd_faces.size()) ? -1 : (triangle_patch[i]*8 + patch_offset);
629                 }
630         }
631 }
632
633 void Mesh::pack_curves(Scene *scene, float4 *curve_key_co, float4 *curve_data, size_t curvekey_offset)
634 {
635         size_t curve_keys_size = curve_keys.size();
636
637         /* pack curve keys */
638         if(curve_keys_size) {
639                 float3 *keys_ptr = curve_keys.data();
640                 float *radius_ptr = curve_radius.data();
641
642                 for(size_t i = 0; i < curve_keys_size; i++)
643                         curve_key_co[i] = make_float4(keys_ptr[i].x, keys_ptr[i].y, keys_ptr[i].z, radius_ptr[i]);
644         }
645
646         /* pack curve segments */
647         size_t curve_num = num_curves();
648
649         if(curve_num) {
650                 for(size_t i = 0; i < curve_num; i++) {
651                         Curve curve = get_curve(i);
652                         int shader_id = curve_shader[i];
653                         Shader *shader = (shader_id < used_shaders.size()) ?
654                                 used_shaders[shader_id] : scene->default_surface;
655                         shader_id = scene->shader_manager->get_shader_id(shader, this, false);
656
657                         curve_data[i] = make_float4(
658                                 __int_as_float(curve.first_key + curvekey_offset),
659                                 __int_as_float(curve.num_keys),
660                                 __int_as_float(shader_id),
661                                 0.0f);
662                 }
663         }
664 }
665
666 void Mesh::pack_patches(uint *patch_data, uint vert_offset, uint face_offset, uint corner_offset)
667 {
668         size_t num_faces = subd_faces.size();
669         int ngons = 0;
670
671         if(num_faces) {
672                 for(size_t f = 0; f < num_faces; f++) {
673                         SubdFace face = subd_faces[f];
674
675                         if(face.is_quad()) {
676                                 int c[4];
677                                 memcpy(c, &subd_face_corners[face.start_corner], sizeof(int)*4);
678
679                                 *(patch_data++) = c[0] + vert_offset;
680                                 *(patch_data++) = c[1] + vert_offset;
681                                 *(patch_data++) = c[2] + vert_offset;
682                                 *(patch_data++) = c[3] + vert_offset;
683
684                                 *(patch_data++) = f+face_offset;
685                                 *(patch_data++) = face.num_corners;
686                                 *(patch_data++) = face.start_corner + corner_offset;
687                                 *(patch_data++) = 0;
688                         }
689                         else {
690                                 for(int i = 0; i < face.num_corners; i++) {
691                                         int c[4];
692                                         c[0] = subd_face_corners[face.start_corner + mod(i + 0, face.num_corners)];
693                                         c[1] = subd_face_corners[face.start_corner + mod(i + 1, face.num_corners)];
694                                         c[2] = verts.size() - num_subd_verts + ngons;
695                                         c[3] = subd_face_corners[face.start_corner + mod(i - 1, face.num_corners)];
696
697                                         *(patch_data++) = c[0] + vert_offset;
698                                         *(patch_data++) = c[1] + vert_offset;
699                                         *(patch_data++) = c[2] + vert_offset;
700                                         *(patch_data++) = c[3] + vert_offset;
701
702                                         *(patch_data++) = f+face_offset;
703                                         *(patch_data++) = face.num_corners | (i << 16);
704                                         *(patch_data++) = face.start_corner + corner_offset;
705                                         *(patch_data++) = subd_face_corners.size() + ngons + corner_offset;
706                                 }
707
708                                 ngons++;
709                         }
710                 }
711         }
712 }
713
714 void Mesh::compute_bvh(DeviceScene *dscene,
715                        SceneParams *params,
716                        Progress *progress,
717                        int n,
718                        int total)
719 {
720         if(progress->get_cancel())
721                 return;
722
723         compute_bounds();
724
725         if(need_build_bvh()) {
726                 string msg = "Updating Mesh BVH ";
727                 if(name == "")
728                         msg += string_printf("%u/%u", (uint)(n+1), (uint)total);
729                 else
730                         msg += string_printf("%s %u/%u", name.c_str(), (uint)(n+1), (uint)total);
731
732                 Object object;
733                 object.mesh = this;
734
735                 vector<Object*> objects;
736                 objects.push_back(&object);
737
738                 if(bvh && !need_update_rebuild) {
739                         progress->set_status(msg, "Refitting BVH");
740                         bvh->objects = objects;
741                         bvh->refit(*progress);
742                 }
743                 else {
744                         progress->set_status(msg, "Building BVH");
745
746                         BVHParams bparams;
747                         bparams.use_spatial_split = params->use_bvh_spatial_split;
748                         bparams.use_qbvh = params->use_qbvh;
749                         bparams.use_unaligned_nodes = dscene->data.bvh.have_curves &&
750                                                       params->use_bvh_unaligned_nodes;
751
752                         delete bvh;
753                         bvh = BVH::create(bparams, objects);
754                         MEM_GUARDED_CALL(progress, bvh->build, *progress);
755                 }
756         }
757
758         need_update = false;
759         need_update_rebuild = false;
760 }
761
762 void Mesh::tag_update(Scene *scene, bool rebuild)
763 {
764         need_update = true;
765
766         if(rebuild) {
767                 need_update_rebuild = true;
768                 scene->light_manager->need_update = true;
769         }
770         else {
771                 foreach(Shader *shader, used_shaders)
772                         if(shader->has_surface_emission)
773                                 scene->light_manager->need_update = true;
774         }
775
776         scene->mesh_manager->need_update = true;
777         scene->object_manager->need_update = true;
778 }
779
780 bool Mesh::has_motion_blur() const
781 {
782         return (use_motion_blur &&
783                 (attributes.find(ATTR_STD_MOTION_VERTEX_POSITION) ||
784                  curve_attributes.find(ATTR_STD_MOTION_VERTEX_POSITION)));
785 }
786
787 bool Mesh::has_true_displacement() const
788 {
789         foreach(Shader *shader, used_shaders) {
790                 if(shader->has_displacement && shader->displacement_method != DISPLACE_BUMP) {
791                         return true;
792                 }
793         }
794
795         return false;
796 }
797
798 bool Mesh::need_build_bvh() const
799 {
800         return !transform_applied || has_surface_bssrdf;
801 }
802
803 bool Mesh::is_instanced() const
804 {
805         /* Currently we treat subsurface objects as instanced.
806          *
807          * While it might be not very optimal for ray traversal, it avoids having
808          * duplicated BVH in the memory, saving quite some space.
809          */
810         return !transform_applied || has_surface_bssrdf;
811 }
812
813 /* Mesh Manager */
814
815 MeshManager::MeshManager()
816 {
817         bvh = NULL;
818         need_update = true;
819         need_flags_update = true;
820 }
821
822 MeshManager::~MeshManager()
823 {
824         delete bvh;
825 }
826
827 void MeshManager::update_osl_attributes(Device *device, Scene *scene, vector<AttributeRequestSet>& mesh_attributes)
828 {
829 #ifdef WITH_OSL
830         /* for OSL, a hash map is used to lookup the attribute by name. */
831         OSLGlobals *og = (OSLGlobals*)device->osl_memory();
832
833         og->object_name_map.clear();
834         og->attribute_map.clear();
835         og->object_names.clear();
836
837         og->attribute_map.resize(scene->objects.size()*ATTR_PRIM_TYPES);
838
839         for(size_t i = 0; i < scene->objects.size(); i++) {
840                 /* set object name to object index map */
841                 Object *object = scene->objects[i];
842                 og->object_name_map[object->name] = i;
843                 og->object_names.push_back(object->name);
844
845                 /* set object attributes */
846                 foreach(ParamValue& attr, object->attributes) {
847                         OSLGlobals::Attribute osl_attr;
848
849                         osl_attr.type = attr.type();
850                         osl_attr.desc.element = ATTR_ELEMENT_OBJECT;
851                         osl_attr.value = attr;
852                         osl_attr.desc.offset = 0;
853                         osl_attr.desc.flags = 0;
854
855                         og->attribute_map[i*ATTR_PRIM_TYPES + ATTR_PRIM_TRIANGLE][attr.name()] = osl_attr;
856                         og->attribute_map[i*ATTR_PRIM_TYPES + ATTR_PRIM_CURVE][attr.name()] = osl_attr;
857                         og->attribute_map[i*ATTR_PRIM_TYPES + ATTR_PRIM_SUBD][attr.name()] = osl_attr;
858                 }
859
860                 /* find mesh attributes */
861                 size_t j;
862
863                 for(j = 0; j < scene->meshes.size(); j++)
864                         if(scene->meshes[j] == object->mesh)
865                                 break;
866
867                 AttributeRequestSet& attributes = mesh_attributes[j];
868
869                 /* set object attributes */
870                 foreach(AttributeRequest& req, attributes.requests) {
871                         OSLGlobals::Attribute osl_attr;
872
873                         if(req.triangle_desc.element != ATTR_ELEMENT_NONE) {
874                                 osl_attr.desc = req.triangle_desc;
875
876                                 if(req.triangle_type == TypeDesc::TypeFloat)
877                                         osl_attr.type = TypeDesc::TypeFloat;
878                                 else if(req.triangle_type == TypeDesc::TypeMatrix)
879                                         osl_attr.type = TypeDesc::TypeMatrix;
880                                 else
881                                         osl_attr.type = TypeDesc::TypeColor;
882
883                                 if(req.std != ATTR_STD_NONE) {
884                                         /* if standard attribute, add lookup by geom: name convention */
885                                         ustring stdname(string("geom:") + string(Attribute::standard_name(req.std)));
886                                         og->attribute_map[i*ATTR_PRIM_TYPES + ATTR_PRIM_TRIANGLE][stdname] = osl_attr;
887                                 }
888                                 else if(req.name != ustring()) {
889                                         /* add lookup by mesh attribute name */
890                                         og->attribute_map[i*ATTR_PRIM_TYPES + ATTR_PRIM_TRIANGLE][req.name] = osl_attr;
891                                 }
892                         }
893
894                         if(req.curve_desc.element != ATTR_ELEMENT_NONE) {
895                                 osl_attr.desc = req.curve_desc;
896
897                                 if(req.curve_type == TypeDesc::TypeFloat)
898                                         osl_attr.type = TypeDesc::TypeFloat;
899                                 else if(req.curve_type == TypeDesc::TypeMatrix)
900                                         osl_attr.type = TypeDesc::TypeMatrix;
901                                 else
902                                         osl_attr.type = TypeDesc::TypeColor;
903
904                                 if(req.std != ATTR_STD_NONE) {
905                                         /* if standard attribute, add lookup by geom: name convention */
906                                         ustring stdname(string("geom:") + string(Attribute::standard_name(req.std)));
907                                         og->attribute_map[i*ATTR_PRIM_TYPES + ATTR_PRIM_CURVE][stdname] = osl_attr;
908                                 }
909                                 else if(req.name != ustring()) {
910                                         /* add lookup by mesh attribute name */
911                                         og->attribute_map[i*ATTR_PRIM_TYPES + ATTR_PRIM_CURVE][req.name] = osl_attr;
912                                 }
913                         }
914
915                         if(req.subd_desc.element != ATTR_ELEMENT_NONE) {
916                                 osl_attr.desc = req.subd_desc;
917
918                                 if(req.subd_type == TypeDesc::TypeFloat)
919                                         osl_attr.type = TypeDesc::TypeFloat;
920                                 else if(req.subd_type == TypeDesc::TypeMatrix)
921                                         osl_attr.type = TypeDesc::TypeMatrix;
922                                 else
923                                         osl_attr.type = TypeDesc::TypeColor;
924
925                                 if(req.std != ATTR_STD_NONE) {
926                                         /* if standard attribute, add lookup by geom: name convention */
927                                         ustring stdname(string("geom:") + string(Attribute::standard_name(req.std)));
928                                         og->attribute_map[i*ATTR_PRIM_TYPES + ATTR_PRIM_SUBD][stdname] = osl_attr;
929                                 }
930                                 else if(req.name != ustring()) {
931                                         /* add lookup by mesh attribute name */
932                                         og->attribute_map[i*ATTR_PRIM_TYPES + ATTR_PRIM_SUBD][req.name] = osl_attr;
933                                 }
934                         }
935                 }
936         }
937 #else
938         (void)device;
939         (void)scene;
940         (void)mesh_attributes;
941 #endif
942 }
943
944 void MeshManager::update_svm_attributes(Device *device, DeviceScene *dscene, Scene *scene, vector<AttributeRequestSet>& mesh_attributes)
945 {
946         /* for SVM, the attributes_map table is used to lookup the offset of an
947          * attribute, based on a unique shader attribute id. */
948
949         /* compute array stride */
950         int attr_map_stride = 0;
951
952         for(size_t i = 0; i < scene->meshes.size(); i++)
953                 attr_map_stride = max(attr_map_stride, (mesh_attributes[i].size() + 1)*ATTR_PRIM_TYPES);
954
955         if(attr_map_stride == 0)
956                 return;
957         
958         /* create attribute map */
959         uint4 *attr_map = dscene->attributes_map.resize(attr_map_stride*scene->objects.size());
960         memset(attr_map, 0, dscene->attributes_map.size()*sizeof(uint));
961
962         for(size_t i = 0; i < scene->objects.size(); i++) {
963                 Object *object = scene->objects[i];
964                 Mesh *mesh = object->mesh;
965
966                 /* find mesh attributes */
967                 size_t j;
968
969                 for(j = 0; j < scene->meshes.size(); j++)
970                         if(scene->meshes[j] == mesh)
971                                 break;
972
973                 AttributeRequestSet& attributes = mesh_attributes[j];
974
975                 /* set object attributes */
976                 int index = i*attr_map_stride;
977
978                 foreach(AttributeRequest& req, attributes.requests) {
979                         uint id;
980
981                         if(req.std == ATTR_STD_NONE)
982                                 id = scene->shader_manager->get_attribute_id(req.name);
983                         else
984                                 id = scene->shader_manager->get_attribute_id(req.std);
985
986                         if(mesh->num_triangles()) {
987                                 attr_map[index].x = id;
988                                 attr_map[index].y = req.triangle_desc.element;
989                                 attr_map[index].z = as_uint(req.triangle_desc.offset);
990
991                                 if(req.triangle_type == TypeDesc::TypeFloat)
992                                         attr_map[index].w = NODE_ATTR_FLOAT;
993                                 else if(req.triangle_type == TypeDesc::TypeMatrix)
994                                         attr_map[index].w = NODE_ATTR_MATRIX;
995                                 else
996                                         attr_map[index].w = NODE_ATTR_FLOAT3;
997
998                                 attr_map[index].w |= req.triangle_desc.flags << 8;
999                         }
1000
1001                         index++;
1002
1003                         if(mesh->num_curves()) {
1004                                 attr_map[index].x = id;
1005                                 attr_map[index].y = req.curve_desc.element;
1006                                 attr_map[index].z = as_uint(req.curve_desc.offset);
1007
1008                                 if(req.curve_type == TypeDesc::TypeFloat)
1009                                         attr_map[index].w = NODE_ATTR_FLOAT;
1010                                 else if(req.curve_type == TypeDesc::TypeMatrix)
1011                                         attr_map[index].w = NODE_ATTR_MATRIX;
1012                                 else
1013                                         attr_map[index].w = NODE_ATTR_FLOAT3;
1014
1015                                 attr_map[index].w |= req.curve_desc.flags << 8;
1016                         }
1017
1018                         index++;
1019
1020                         if(mesh->subd_faces.size()) {
1021                                 attr_map[index].x = id;
1022                                 attr_map[index].y = req.subd_desc.element;
1023                                 attr_map[index].z = as_uint(req.subd_desc.offset);
1024
1025                                 if(req.subd_type == TypeDesc::TypeFloat)
1026                                         attr_map[index].w = NODE_ATTR_FLOAT;
1027                                 else if(req.subd_type == TypeDesc::TypeMatrix)
1028                                         attr_map[index].w = NODE_ATTR_MATRIX;
1029                                 else
1030                                         attr_map[index].w = NODE_ATTR_FLOAT3;
1031
1032                                 attr_map[index].w |= req.subd_desc.flags << 8;
1033                         }
1034
1035                         index++;
1036                 }
1037
1038                 /* terminator */
1039                 for(int i = 0; i < ATTR_PRIM_TYPES; i++) {
1040                         attr_map[index].x = ATTR_STD_NONE;
1041                         attr_map[index].y = 0;
1042                         attr_map[index].z = 0;
1043                         attr_map[index].w = 0;
1044
1045                         index++;
1046                 }
1047         }
1048
1049         /* copy to device */
1050         dscene->data.bvh.attributes_map_stride = attr_map_stride;
1051         device->tex_alloc("__attributes_map", dscene->attributes_map);
1052 }
1053
1054 static void update_attribute_element_size(Mesh *mesh,
1055                                           Attribute *mattr,
1056                                           AttributePrimitive prim,
1057                                           size_t *attr_float_size,
1058                                           size_t *attr_float3_size,
1059                                           size_t *attr_uchar4_size)
1060 {
1061         if(mattr) {
1062                 size_t size = mattr->element_size(mesh, prim);
1063
1064                 if(mattr->element == ATTR_ELEMENT_VOXEL) {
1065                         /* pass */
1066                 }
1067                 else if(mattr->element == ATTR_ELEMENT_CORNER_BYTE) {
1068                         *attr_uchar4_size += size;
1069                 }
1070                 else if(mattr->type == TypeDesc::TypeFloat) {
1071                         *attr_float_size += size;
1072                 }
1073                 else if(mattr->type == TypeDesc::TypeMatrix) {
1074                         *attr_float3_size += size * 4;
1075                 }
1076                 else {
1077                         *attr_float3_size += size;
1078                 }
1079         }
1080 }
1081
1082 static void update_attribute_element_offset(Mesh *mesh,
1083                                             vector<float>& attr_float,
1084                                             size_t& attr_float_offset,
1085                                             vector<float4>& attr_float3,
1086                                             size_t& attr_float3_offset,
1087                                             vector<uchar4>& attr_uchar4,
1088                                             size_t& attr_uchar4_offset,
1089                                             Attribute *mattr,
1090                                             AttributePrimitive prim,
1091                                             TypeDesc& type,
1092                                             AttributeDescriptor& desc)
1093 {
1094         if(mattr) {
1095                 /* store element and type */
1096                 desc.element = mattr->element;
1097                 desc.flags = mattr->flags;
1098                 type = mattr->type;
1099
1100                 /* store attribute data in arrays */
1101                 size_t size = mattr->element_size(mesh, prim);
1102
1103                 AttributeElement& element = desc.element;
1104                 int& offset = desc.offset;
1105
1106                 if(mattr->element == ATTR_ELEMENT_VOXEL) {
1107                         /* store slot in offset value */
1108                         VoxelAttribute *voxel_data = mattr->data_voxel();
1109                         offset = voxel_data->slot;
1110                 }
1111                 else if(mattr->element == ATTR_ELEMENT_CORNER_BYTE) {
1112                         uchar4 *data = mattr->data_uchar4();
1113                         offset = attr_uchar4_offset;
1114
1115                         assert(attr_uchar4.capacity() >= offset + size);
1116                         for(size_t k = 0; k < size; k++) {
1117                                 attr_uchar4[offset+k] = data[k];
1118                         }
1119                         attr_uchar4_offset += size;
1120                 }
1121                 else if(mattr->type == TypeDesc::TypeFloat) {
1122                         float *data = mattr->data_float();
1123                         offset = attr_float_offset;
1124
1125                         assert(attr_float.capacity() >= offset + size);
1126                         for(size_t k = 0; k < size; k++) {
1127                                 attr_float[offset+k] = data[k];
1128                         }
1129                         attr_float_offset += size;
1130                 }
1131                 else if(mattr->type == TypeDesc::TypeMatrix) {
1132                         Transform *tfm = mattr->data_transform();
1133                         offset = attr_float3_offset;
1134
1135                         assert(attr_float3.capacity() >= offset + size * 4);
1136                         for(size_t k = 0; k < size*4; k++) {
1137                                 attr_float3[offset+k] = (&tfm->x)[k];
1138                         }
1139                         attr_float3_offset += size * 4;
1140                 }
1141                 else {
1142                         float4 *data = mattr->data_float4();
1143                         offset = attr_float3_offset;
1144
1145                         assert(attr_float3.capacity() >= offset + size);
1146                         for(size_t k = 0; k < size; k++) {
1147                                 attr_float3[offset+k] = data[k];
1148                         }
1149                         attr_float3_offset += size;
1150                 }
1151
1152                 /* mesh vertex/curve index is global, not per object, so we sneak
1153                  * a correction for that in here */
1154                 if(mesh->subdivision_type == Mesh::SUBDIVISION_CATMULL_CLARK && desc.flags & ATTR_SUBDIVIDED) {
1155                         /* indices for subdivided attributes are retrieved
1156                          * from patch table so no need for correction here*/
1157                 }
1158                 else if(element == ATTR_ELEMENT_VERTEX)
1159                         offset -= mesh->vert_offset;
1160                 else if(element == ATTR_ELEMENT_VERTEX_MOTION)
1161                         offset -= mesh->vert_offset;
1162                 else if(element == ATTR_ELEMENT_FACE) {
1163                         if(prim == ATTR_PRIM_TRIANGLE)
1164                                 offset -= mesh->tri_offset;
1165                         else
1166                                 offset -= mesh->face_offset;
1167                 }
1168                 else if(element == ATTR_ELEMENT_CORNER || element == ATTR_ELEMENT_CORNER_BYTE) {
1169                         if(prim == ATTR_PRIM_TRIANGLE)
1170                                 offset -= 3*mesh->tri_offset;
1171                         else
1172                                 offset -= mesh->corner_offset;
1173                 }
1174                 else if(element == ATTR_ELEMENT_CURVE)
1175                         offset -= mesh->curve_offset;
1176                 else if(element == ATTR_ELEMENT_CURVE_KEY)
1177                         offset -= mesh->curvekey_offset;
1178                 else if(element == ATTR_ELEMENT_CURVE_KEY_MOTION)
1179                         offset -= mesh->curvekey_offset;
1180         }
1181         else {
1182                 /* attribute not found */
1183                 desc.element = ATTR_ELEMENT_NONE;
1184                 desc.offset = 0;
1185         }
1186 }
1187
1188 void MeshManager::device_update_attributes(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
1189 {
1190         progress.set_status("Updating Mesh", "Computing attributes");
1191
1192         /* gather per mesh requested attributes. as meshes may have multiple
1193          * shaders assigned, this merges the requested attributes that have
1194          * been set per shader by the shader manager */
1195         vector<AttributeRequestSet> mesh_attributes(scene->meshes.size());
1196
1197         for(size_t i = 0; i < scene->meshes.size(); i++) {
1198                 Mesh *mesh = scene->meshes[i];
1199
1200                 scene->need_global_attributes(mesh_attributes[i]);
1201
1202                 foreach(Shader *shader, mesh->used_shaders) {
1203                         mesh_attributes[i].add(shader->attributes);
1204                 }
1205         }
1206
1207         /* mesh attribute are stored in a single array per data type. here we fill
1208          * those arrays, and set the offset and element type to create attribute
1209          * maps next */
1210
1211         /* Pre-allocate attributes to avoid arrays re-allocation which would
1212          * take 2x of overall attribute memory usage.
1213          */
1214         size_t attr_float_size = 0;
1215         size_t attr_float3_size = 0;
1216         size_t attr_uchar4_size = 0;
1217         for(size_t i = 0; i < scene->meshes.size(); i++) {
1218                 Mesh *mesh = scene->meshes[i];
1219                 AttributeRequestSet& attributes = mesh_attributes[i];
1220                 foreach(AttributeRequest& req, attributes.requests) {
1221                         Attribute *triangle_mattr = mesh->attributes.find(req);
1222                         Attribute *curve_mattr = mesh->curve_attributes.find(req);
1223                         Attribute *subd_mattr = mesh->subd_attributes.find(req);
1224
1225                         update_attribute_element_size(mesh,
1226                                                       triangle_mattr,
1227                                                       ATTR_PRIM_TRIANGLE,
1228                                                       &attr_float_size,
1229                                                       &attr_float3_size,
1230                                                       &attr_uchar4_size);
1231                         update_attribute_element_size(mesh,
1232                                                       curve_mattr,
1233                                                       ATTR_PRIM_CURVE,
1234                                                       &attr_float_size,
1235                                                       &attr_float3_size,
1236                                                       &attr_uchar4_size);
1237                         update_attribute_element_size(mesh,
1238                                                       subd_mattr,
1239                                                       ATTR_PRIM_SUBD,
1240                                                       &attr_float_size,
1241                                                       &attr_float3_size,
1242                                                       &attr_uchar4_size);
1243                 }
1244         }
1245
1246         vector<float> attr_float(attr_float_size);
1247         vector<float4> attr_float3(attr_float3_size);
1248         vector<uchar4> attr_uchar4(attr_uchar4_size);
1249
1250         size_t attr_float_offset = 0;
1251         size_t attr_float3_offset = 0;
1252         size_t attr_uchar4_offset = 0;
1253
1254         /* Fill in attributes. */
1255         for(size_t i = 0; i < scene->meshes.size(); i++) {
1256                 Mesh *mesh = scene->meshes[i];
1257                 AttributeRequestSet& attributes = mesh_attributes[i];
1258
1259                 /* todo: we now store std and name attributes from requests even if
1260                  * they actually refer to the same mesh attributes, optimize */
1261                 foreach(AttributeRequest& req, attributes.requests) {
1262                         Attribute *triangle_mattr = mesh->attributes.find(req);
1263                         Attribute *curve_mattr = mesh->curve_attributes.find(req);
1264                         Attribute *subd_mattr = mesh->subd_attributes.find(req);
1265
1266                         update_attribute_element_offset(mesh,
1267                                                         attr_float, attr_float_offset,
1268                                                         attr_float3, attr_float3_offset,
1269                                                         attr_uchar4, attr_uchar4_offset,
1270                                                         triangle_mattr,
1271                                                         ATTR_PRIM_TRIANGLE,
1272                                                         req.triangle_type,
1273                                                         req.triangle_desc);
1274
1275                         update_attribute_element_offset(mesh,
1276                                                         attr_float, attr_float_offset,
1277                                                         attr_float3, attr_float3_offset,
1278                                                         attr_uchar4, attr_uchar4_offset,
1279                                                         curve_mattr,
1280                                                         ATTR_PRIM_CURVE,
1281                                                         req.curve_type,
1282                                                         req.curve_desc);
1283
1284                         update_attribute_element_offset(mesh,
1285                                                         attr_float, attr_float_offset,
1286                                                         attr_float3, attr_float3_offset,
1287                                                         attr_uchar4, attr_uchar4_offset,
1288                                                         subd_mattr,
1289                                                         ATTR_PRIM_SUBD,
1290                                                         req.subd_type,
1291                                                         req.subd_desc);
1292
1293                         if(progress.get_cancel()) return;
1294                 }
1295         }
1296
1297         /* create attribute lookup maps */
1298         if(scene->shader_manager->use_osl())
1299                 update_osl_attributes(device, scene, mesh_attributes);
1300
1301         update_svm_attributes(device, dscene, scene, mesh_attributes);
1302
1303         if(progress.get_cancel()) return;
1304
1305         /* copy to device */
1306         progress.set_status("Updating Mesh", "Copying Attributes to device");
1307
1308         if(attr_float.size()) {
1309                 dscene->attributes_float.copy(&attr_float[0], attr_float.size());
1310                 device->tex_alloc("__attributes_float", dscene->attributes_float);
1311         }
1312         if(attr_float3.size()) {
1313                 dscene->attributes_float3.copy(&attr_float3[0], attr_float3.size());
1314                 device->tex_alloc("__attributes_float3", dscene->attributes_float3);
1315         }
1316         if(attr_uchar4.size()) {
1317                 dscene->attributes_uchar4.copy(&attr_uchar4[0], attr_uchar4.size());
1318                 device->tex_alloc("__attributes_uchar4", dscene->attributes_uchar4);
1319         }
1320 }
1321
1322 void MeshManager::mesh_calc_offset(Scene *scene)
1323 {
1324         size_t vert_size = 0;
1325         size_t tri_size = 0;
1326
1327         size_t curve_key_size = 0;
1328         size_t curve_size = 0;
1329
1330         size_t patch_size = 0;
1331         size_t face_size = 0;
1332         size_t corner_size = 0;
1333
1334         foreach(Mesh *mesh, scene->meshes) {
1335                 mesh->vert_offset = vert_size;
1336                 mesh->tri_offset = tri_size;
1337
1338                 mesh->curvekey_offset = curve_key_size;
1339                 mesh->curve_offset = curve_size;
1340
1341                 mesh->patch_offset = patch_size;
1342                 mesh->face_offset = face_size;
1343                 mesh->corner_offset = corner_size;
1344
1345                 vert_size += mesh->verts.size();
1346                 tri_size += mesh->num_triangles();
1347
1348                 curve_key_size += mesh->curve_keys.size();
1349                 curve_size += mesh->num_curves();
1350
1351                 if(mesh->subd_faces.size()) {
1352                         Mesh::SubdFace& last = mesh->subd_faces[mesh->subd_faces.size()-1];
1353                         patch_size += (last.ptex_offset + last.num_ptex_faces()) * 8;
1354
1355                         /* patch tables are stored in same array so include them in patch_size */
1356                         if(mesh->patch_table) {
1357                                 mesh->patch_table_offset = patch_size;
1358                                 patch_size += mesh->patch_table->total_size();
1359                         }
1360                 }
1361                 face_size += mesh->subd_faces.size();
1362                 corner_size += mesh->subd_face_corners.size();
1363         }
1364 }
1365
1366 void MeshManager::device_update_mesh(Device *device,
1367                                      DeviceScene *dscene,
1368                                      Scene *scene,
1369                                      bool for_displacement,
1370                                      Progress& progress)
1371 {
1372         /* Count. */
1373         size_t vert_size = 0;
1374         size_t tri_size = 0;
1375
1376         size_t curve_key_size = 0;
1377         size_t curve_size = 0;
1378
1379         size_t patch_size = 0;
1380
1381         foreach(Mesh *mesh, scene->meshes) {
1382                 vert_size += mesh->verts.size();
1383                 tri_size += mesh->num_triangles();
1384
1385                 curve_key_size += mesh->curve_keys.size();
1386                 curve_size += mesh->num_curves();
1387
1388                 if(mesh->subd_faces.size()) {
1389                         Mesh::SubdFace& last = mesh->subd_faces[mesh->subd_faces.size()-1];
1390                         patch_size += (last.ptex_offset + last.num_ptex_faces()) * 8;
1391
1392                         /* patch tables are stored in same array so include them in patch_size */
1393                         if(mesh->patch_table) {
1394                                 mesh->patch_table_offset = patch_size;
1395                                 patch_size += mesh->patch_table->total_size();
1396                         }
1397                 }
1398         }
1399
1400         /* Create mapping from triangle to primitive triangle array. */
1401         vector<uint> tri_prim_index(tri_size);
1402         if(for_displacement) {
1403                 /* For displacement kernels we do some trickery to make them believe
1404                  * we've got all required data ready. However, that data is different
1405                  * from final render kernels since we don't have BVH yet, so can't
1406                  * really use same semantic of arrays.
1407                  */
1408                 foreach(Mesh *mesh, scene->meshes) {
1409                         for(size_t i = 0; i < mesh->num_triangles(); ++i) {
1410                                 tri_prim_index[i + mesh->tri_offset] = 3 * (i + mesh->tri_offset);
1411                         }
1412                 }
1413         }
1414         else {
1415                 PackedBVH& pack = bvh->pack;
1416                 for(size_t i = 0; i < pack.prim_index.size(); ++i) {
1417                         if ((pack.prim_type[i] & PRIMITIVE_ALL_TRIANGLE) != 0) {
1418                                 tri_prim_index[pack.prim_index[i]] = pack.prim_tri_index[i];
1419                         }
1420                 }
1421         }
1422
1423         /* Fill in all the arrays. */
1424         if(tri_size != 0) {
1425                 /* normals */
1426                 progress.set_status("Updating Mesh", "Computing normals");
1427
1428                 uint *tri_shader = dscene->tri_shader.resize(tri_size);
1429                 float4 *vnormal = dscene->tri_vnormal.resize(vert_size);
1430                 uint4 *tri_vindex = dscene->tri_vindex.resize(tri_size);
1431                 uint *tri_patch = dscene->tri_patch.resize(tri_size);
1432                 float2 *tri_patch_uv = dscene->tri_patch_uv.resize(vert_size);
1433
1434                 foreach(Mesh *mesh, scene->meshes) {
1435                         mesh->pack_normals(scene,
1436                                            &tri_shader[mesh->tri_offset],
1437                                            &vnormal[mesh->vert_offset]);
1438                         mesh->pack_verts(tri_prim_index,
1439                                          &tri_vindex[mesh->tri_offset],
1440                                          &tri_patch[mesh->tri_offset],
1441                                          &tri_patch_uv[mesh->vert_offset],
1442                                          mesh->vert_offset,
1443                                          mesh->tri_offset);
1444                         if(progress.get_cancel()) return;
1445                 }
1446
1447                 /* vertex coordinates */
1448                 progress.set_status("Updating Mesh", "Copying Mesh to device");
1449
1450                 device->tex_alloc("__tri_shader", dscene->tri_shader);
1451                 device->tex_alloc("__tri_vnormal", dscene->tri_vnormal);
1452                 device->tex_alloc("__tri_vindex", dscene->tri_vindex);
1453                 device->tex_alloc("__tri_patch", dscene->tri_patch);
1454                 device->tex_alloc("__tri_patch_uv", dscene->tri_patch_uv);
1455         }
1456
1457         if(curve_size != 0) {
1458                 progress.set_status("Updating Mesh", "Copying Strands to device");
1459
1460                 float4 *curve_keys = dscene->curve_keys.resize(curve_key_size);
1461                 float4 *curves = dscene->curves.resize(curve_size);
1462
1463                 foreach(Mesh *mesh, scene->meshes) {
1464                         mesh->pack_curves(scene, &curve_keys[mesh->curvekey_offset], &curves[mesh->curve_offset], mesh->curvekey_offset);
1465                         if(progress.get_cancel()) return;
1466                 }
1467
1468                 device->tex_alloc("__curve_keys", dscene->curve_keys);
1469                 device->tex_alloc("__curves", dscene->curves);
1470         }
1471
1472         if(patch_size != 0) {
1473                 progress.set_status("Updating Mesh", "Copying Patches to device");
1474
1475                 uint *patch_data = dscene->patches.resize(patch_size);
1476
1477                 foreach(Mesh *mesh, scene->meshes) {
1478                         mesh->pack_patches(&patch_data[mesh->patch_offset], mesh->vert_offset, mesh->face_offset, mesh->corner_offset);
1479
1480                         if(mesh->patch_table) {
1481                                 mesh->patch_table->copy_adjusting_offsets(&patch_data[mesh->patch_table_offset], mesh->patch_table_offset);
1482                         }
1483
1484                         if(progress.get_cancel()) return;
1485                 }
1486
1487                 device->tex_alloc("__patches", dscene->patches);
1488         }
1489
1490         if(for_displacement) {
1491                 float4 *prim_tri_verts = dscene->prim_tri_verts.resize(tri_size * 3);
1492                 foreach(Mesh *mesh, scene->meshes) {
1493                         for(size_t i = 0; i < mesh->num_triangles(); ++i) {
1494                                 Mesh::Triangle t = mesh->get_triangle(i);
1495                                 size_t offset = 3 * (i + mesh->tri_offset);
1496                                 prim_tri_verts[offset + 0] = float3_to_float4(mesh->verts[t.v[0]]);
1497                                 prim_tri_verts[offset + 1] = float3_to_float4(mesh->verts[t.v[1]]);
1498                                 prim_tri_verts[offset + 2] = float3_to_float4(mesh->verts[t.v[2]]);
1499                         }
1500                 }
1501                 device->tex_alloc("__prim_tri_verts", dscene->prim_tri_verts);
1502         }
1503 }
1504
1505 void MeshManager::device_update_bvh(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
1506 {
1507         /* bvh build */
1508         progress.set_status("Updating Scene BVH", "Building");
1509
1510         VLOG(1) << (scene->params.use_qbvh ? "Using QBVH optimization structure"
1511                                            : "Using regular BVH optimization structure");
1512
1513         BVHParams bparams;
1514         bparams.top_level = true;
1515         bparams.use_qbvh = scene->params.use_qbvh;
1516         bparams.use_spatial_split = scene->params.use_bvh_spatial_split;
1517         bparams.use_unaligned_nodes = dscene->data.bvh.have_curves &&
1518                                       scene->params.use_bvh_unaligned_nodes;
1519
1520         delete bvh;
1521         bvh = BVH::create(bparams, scene->objects);
1522         bvh->build(progress);
1523
1524         if(progress.get_cancel()) return;
1525
1526         /* copy to device */
1527         progress.set_status("Updating Scene BVH", "Copying BVH to device");
1528
1529         PackedBVH& pack = bvh->pack;
1530
1531         if(pack.nodes.size()) {
1532                 dscene->bvh_nodes.reference((float4*)&pack.nodes[0], pack.nodes.size());
1533                 device->tex_alloc("__bvh_nodes", dscene->bvh_nodes);
1534         }
1535         if(pack.leaf_nodes.size()) {
1536                 dscene->bvh_leaf_nodes.reference((float4*)&pack.leaf_nodes[0], pack.leaf_nodes.size());
1537                 device->tex_alloc("__bvh_leaf_nodes", dscene->bvh_leaf_nodes);
1538         }
1539         if(pack.object_node.size()) {
1540                 dscene->object_node.reference((uint*)&pack.object_node[0], pack.object_node.size());
1541                 device->tex_alloc("__object_node", dscene->object_node);
1542         }
1543         if(pack.prim_tri_index.size()) {
1544                 dscene->prim_tri_index.reference((uint*)&pack.prim_tri_index[0], pack.prim_tri_index.size());
1545                 device->tex_alloc("__prim_tri_index", dscene->prim_tri_index);
1546         }
1547         if(pack.prim_tri_verts.size()) {
1548                 dscene->prim_tri_verts.reference((float4*)&pack.prim_tri_verts[0], pack.prim_tri_verts.size());
1549                 device->tex_alloc("__prim_tri_verts", dscene->prim_tri_verts);
1550         }
1551         if(pack.prim_type.size()) {
1552                 dscene->prim_type.reference((uint*)&pack.prim_type[0], pack.prim_type.size());
1553                 device->tex_alloc("__prim_type", dscene->prim_type);
1554         }
1555         if(pack.prim_visibility.size()) {
1556                 dscene->prim_visibility.reference((uint*)&pack.prim_visibility[0], pack.prim_visibility.size());
1557                 device->tex_alloc("__prim_visibility", dscene->prim_visibility);
1558         }
1559         if(pack.prim_index.size()) {
1560                 dscene->prim_index.reference((uint*)&pack.prim_index[0], pack.prim_index.size());
1561                 device->tex_alloc("__prim_index", dscene->prim_index);
1562         }
1563         if(pack.prim_object.size()) {
1564                 dscene->prim_object.reference((uint*)&pack.prim_object[0], pack.prim_object.size());
1565                 device->tex_alloc("__prim_object", dscene->prim_object);
1566         }
1567
1568         dscene->data.bvh.root = pack.root_index;
1569         dscene->data.bvh.use_qbvh = scene->params.use_qbvh;
1570 }
1571
1572 void MeshManager::device_update_flags(Device * /*device*/,
1573                                       DeviceScene * /*dscene*/,
1574                                       Scene * scene,
1575                                       Progress& /*progress*/)
1576 {
1577         if(!need_update && !need_flags_update) {
1578                 return;
1579         }
1580         /* update flags */
1581         foreach(Mesh *mesh, scene->meshes) {
1582                 mesh->has_volume = false;
1583                 foreach(const Shader *shader, mesh->used_shaders) {
1584                         if(shader->has_volume) {
1585                                 mesh->has_volume = true;
1586                         }
1587                         if(shader->has_surface_bssrdf) {
1588                                 mesh->has_surface_bssrdf = true;
1589                         }
1590                 }
1591         }
1592         need_flags_update = false;
1593 }
1594
1595 void MeshManager::device_update_displacement_images(Device *device,
1596                                                     DeviceScene *dscene,
1597                                                     Scene *scene,
1598                                                     Progress& progress)
1599 {
1600         progress.set_status("Updating Displacement Images");
1601         TaskPool pool;
1602         ImageManager *image_manager = scene->image_manager;
1603         set<int> bump_images;
1604         foreach(Mesh *mesh, scene->meshes) {
1605                 if(mesh->need_update) {
1606                         foreach(Shader *shader, mesh->used_shaders) {
1607                                 if(shader->graph_bump == NULL) {
1608                                         continue;
1609                                 }
1610                                 foreach(ShaderNode* node, shader->graph_bump->nodes) {
1611                                         if(node->special_type != SHADER_SPECIAL_TYPE_IMAGE_SLOT) {
1612                                                 continue;
1613                                         }
1614                                         if(device->info.pack_images) {
1615                                                 /* If device requires packed images we need to update all
1616                                                  * images now, even if they're not used for displacement.
1617                                                  */
1618                                                 image_manager->device_update(device,
1619                                                                              dscene,
1620                                                                              progress);
1621                                                 return;
1622                                         }
1623                                         ImageSlotTextureNode *image_node = static_cast<ImageSlotTextureNode*>(node);
1624                                         int slot = image_node->slot;
1625                                         if(slot != -1) {
1626                                                 bump_images.insert(slot);
1627                                         }
1628                                 }
1629                         }
1630                 }
1631         }
1632         foreach(int slot, bump_images) {
1633                 pool.push(function_bind(&ImageManager::device_update_slot,
1634                                         image_manager,
1635                                         device,
1636                                         dscene,
1637                                         slot,
1638                                         &progress));
1639         }
1640         pool.wait_work();
1641 }
1642
1643 void MeshManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
1644 {
1645         if(!need_update)
1646                 return;
1647
1648         VLOG(1) << "Total " << scene->meshes.size() << " meshes.";
1649
1650         /* Update normals. */
1651         foreach(Mesh *mesh, scene->meshes) {
1652                 foreach(Shader *shader, mesh->used_shaders) {
1653                         if(shader->need_update_attributes)
1654                                 mesh->need_update = true;
1655                 }
1656
1657                 if(mesh->need_update) {
1658                         mesh->add_face_normals();
1659                         mesh->add_vertex_normals();
1660
1661                         if(progress.get_cancel()) return;
1662                 }
1663         }
1664
1665         /* Tessellate meshes that are using subdivision */
1666         size_t total_tess_needed = 0;
1667         foreach(Mesh *mesh, scene->meshes) {
1668                 if(mesh->need_update &&
1669                    mesh->subdivision_type != Mesh::SUBDIVISION_NONE &&
1670                    mesh->num_subd_verts == 0 &&
1671                    mesh->subd_params)
1672                 {
1673                         total_tess_needed++;
1674                 }
1675         }
1676
1677         size_t i = 0;
1678         foreach(Mesh *mesh, scene->meshes) {
1679                 if(mesh->need_update &&
1680                    mesh->subdivision_type != Mesh::SUBDIVISION_NONE &&
1681                    mesh->num_subd_verts == 0 &&
1682                    mesh->subd_params)
1683                 {
1684                         string msg = "Tessellating ";
1685                         if(mesh->name == "")
1686                                 msg += string_printf("%u/%u", (uint)(i+1), (uint)total_tess_needed);
1687                         else
1688                                 msg += string_printf("%s %u/%u", mesh->name.c_str(), (uint)(i+1), (uint)total_tess_needed);
1689
1690                         progress.set_status("Updating Mesh", msg);
1691
1692                         DiagSplit dsplit(*mesh->subd_params);
1693                         mesh->tessellate(&dsplit);
1694
1695                         i++;
1696
1697                         if(progress.get_cancel()) return;
1698                 }
1699         }
1700
1701         /* Update images needed for true displacement. */
1702         bool true_displacement_used = false;
1703         bool old_need_object_flags_update = false;
1704         foreach(Mesh *mesh, scene->meshes) {
1705                 if(mesh->need_update &&
1706                    mesh->has_true_displacement())
1707                 {
1708                         true_displacement_used = true;
1709                         break;
1710                 }
1711         }
1712         if(true_displacement_used) {
1713                 VLOG(1) << "Updating images used for true displacement.";
1714                 device_update_displacement_images(device, dscene, scene, progress);
1715                 old_need_object_flags_update = scene->object_manager->need_flags_update;
1716                 scene->object_manager->device_update_flags(device,
1717                                                            dscene,
1718                                                            scene,
1719                                                            progress,
1720                                                            false);
1721         }
1722
1723         /* Device update. */
1724         device_free(device, dscene);
1725
1726         mesh_calc_offset(scene);
1727         if(true_displacement_used) {
1728                 device_update_mesh(device, dscene, scene, true, progress);
1729         }
1730         if(progress.get_cancel()) return;
1731
1732         /* after mesh data has been copied to device memory we need to update
1733          * offsets for patch tables as this can't be known before hand */
1734         scene->object_manager->device_update_patch_map_offsets(device, dscene, scene);
1735
1736         device_update_attributes(device, dscene, scene, progress);
1737         if(progress.get_cancel()) return;
1738
1739         /* Update displacement. */
1740         bool displacement_done = false;
1741         foreach(Mesh *mesh, scene->meshes) {
1742                 if(mesh->need_update &&
1743                    displace(device, dscene, scene, mesh, progress))
1744                 {
1745                         displacement_done = true;
1746                 }
1747         }
1748
1749         /* TODO: properly handle cancel halfway displacement */
1750         if(progress.get_cancel()) return;
1751
1752         /* Device re-update after displacement. */
1753         if(displacement_done) {
1754                 device_free(device, dscene);
1755
1756                 device_update_attributes(device, dscene, scene, progress);
1757                 if(progress.get_cancel()) return;
1758         }
1759
1760         /* Update bvh. */
1761         size_t num_bvh = 0;
1762         foreach(Mesh *mesh, scene->meshes) {
1763                 if(mesh->need_update && mesh->need_build_bvh()) {
1764                         num_bvh++;
1765                 }
1766         }
1767
1768         TaskPool pool;
1769
1770         i = 0;
1771         foreach(Mesh *mesh, scene->meshes) {
1772                 if(mesh->need_update) {
1773                         pool.push(function_bind(&Mesh::compute_bvh,
1774                                                 mesh,
1775                                                 dscene,
1776                                                 &scene->params,
1777                                                 &progress,
1778                                                 i,
1779                                                 num_bvh));
1780                         if(mesh->need_build_bvh()) {
1781                                 i++;
1782                         }
1783                 }
1784         }
1785
1786         TaskPool::Summary summary;
1787         pool.wait_work(&summary);
1788         VLOG(2) << "Objects BVH build pool statistics:\n"
1789                 << summary.full_report();
1790
1791         foreach(Shader *shader, scene->shaders) {
1792                 shader->need_update_attributes = false;
1793         }
1794
1795 #ifdef __OBJECT_MOTION__
1796         Scene::MotionType need_motion = scene->need_motion(device->info.advanced_shading);
1797         bool motion_blur = need_motion == Scene::MOTION_BLUR;
1798 #else
1799         bool motion_blur = false;
1800 #endif
1801
1802         /* Update objects. */
1803         vector<Object *> volume_objects;
1804         foreach(Object *object, scene->objects) {
1805                 object->compute_bounds(motion_blur);
1806         }
1807
1808         if(progress.get_cancel()) return;
1809
1810         device_update_bvh(device, dscene, scene, progress);
1811         if(progress.get_cancel()) return;
1812
1813         device_update_mesh(device, dscene, scene, false, progress);
1814         if(progress.get_cancel()) return;
1815
1816         need_update = false;
1817
1818         if(true_displacement_used) {
1819                 /* Re-tag flags for update, so they're re-evaluated
1820                  * for meshes with correct bounding boxes.
1821                  *
1822                  * This wouldn't cause wrong results, just true
1823                  * displacement might be less optimal ot calculate.
1824                  */
1825                 scene->object_manager->need_flags_update = old_need_object_flags_update;
1826         }
1827 }
1828
1829 void MeshManager::device_free(Device *device, DeviceScene *dscene)
1830 {
1831         device->tex_free(dscene->bvh_nodes);
1832         device->tex_free(dscene->bvh_leaf_nodes);
1833         device->tex_free(dscene->object_node);
1834         device->tex_free(dscene->prim_tri_verts);
1835         device->tex_free(dscene->prim_tri_index);
1836         device->tex_free(dscene->prim_type);
1837         device->tex_free(dscene->prim_visibility);
1838         device->tex_free(dscene->prim_index);
1839         device->tex_free(dscene->prim_object);
1840         device->tex_free(dscene->tri_shader);
1841         device->tex_free(dscene->tri_vnormal);
1842         device->tex_free(dscene->tri_vindex);
1843         device->tex_free(dscene->tri_patch);
1844         device->tex_free(dscene->tri_patch_uv);
1845         device->tex_free(dscene->curves);
1846         device->tex_free(dscene->curve_keys);
1847         device->tex_free(dscene->patches);
1848         device->tex_free(dscene->attributes_map);
1849         device->tex_free(dscene->attributes_float);
1850         device->tex_free(dscene->attributes_float3);
1851         device->tex_free(dscene->attributes_uchar4);
1852
1853         dscene->bvh_nodes.clear();
1854         dscene->object_node.clear();
1855         dscene->prim_tri_verts.clear();
1856         dscene->prim_tri_index.clear();
1857         dscene->prim_type.clear();
1858         dscene->prim_visibility.clear();
1859         dscene->prim_index.clear();
1860         dscene->prim_object.clear();
1861         dscene->tri_shader.clear();
1862         dscene->tri_vnormal.clear();
1863         dscene->tri_vindex.clear();
1864         dscene->tri_patch.clear();
1865         dscene->tri_patch_uv.clear();
1866         dscene->curves.clear();
1867         dscene->curve_keys.clear();
1868         dscene->patches.clear();
1869         dscene->attributes_map.clear();
1870         dscene->attributes_float.clear();
1871         dscene->attributes_float3.clear();
1872         dscene->attributes_uchar4.clear();
1873
1874 #ifdef WITH_OSL
1875         OSLGlobals *og = (OSLGlobals*)device->osl_memory();
1876
1877         if(og) {
1878                 og->object_name_map.clear();
1879                 og->attribute_map.clear();
1880                 og->object_names.clear();
1881         }
1882 #endif
1883 }
1884
1885 void MeshManager::tag_update(Scene *scene)
1886 {
1887         need_update = true;
1888         scene->object_manager->need_update = true;
1889 }
1890
1891 bool Mesh::need_attribute(Scene *scene, AttributeStandard std)
1892 {
1893         if(std == ATTR_STD_NONE)
1894                 return false;
1895         
1896         if(scene->need_global_attribute(std))
1897                 return true;
1898
1899         foreach(Shader *shader, used_shaders)
1900                 if(shader->attributes.find(std))
1901                         return true;
1902         
1903         return false;
1904 }
1905
1906 bool Mesh::need_attribute(Scene * /*scene*/, ustring name)
1907 {
1908         if(name == ustring())
1909                 return false;
1910
1911         foreach(Shader *shader, used_shaders)
1912                 if(shader->attributes.find(name))
1913                         return true;
1914         
1915         return false;
1916 }
1917
1918 CCL_NAMESPACE_END
1919