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