ac0614e3659aa5ee1ac7cc3c1330cc30cb028a8b
[blender.git] / intern / cycles / bvh / bvh.cpp
1 /*
2  * Adapted from code copyright 2009-2010 NVIDIA Corporation
3  * Modifications Copyright 2011, Blender Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "bvh/bvh.h"
19
20 #include "render/mesh.h"
21 #include "render/object.h"
22
23 #include "bvh/bvh2.h"
24 #include "bvh/bvh4.h"
25 #include "bvh/bvh8.h"
26 #include "bvh/bvh_build.h"
27 #include "bvh/bvh_node.h"
28
29 #ifdef WITH_EMBREE
30 #include "bvh/bvh_embree.h"
31 #endif
32
33 #include "util/util_foreach.h"
34 #include "util/util_logging.h"
35 #include "util/util_progress.h"
36
37 CCL_NAMESPACE_BEGIN
38
39 /* BVH Parameters. */
40
41 const char *bvh_layout_name(BVHLayout layout)
42 {
43         switch(layout) {
44                 case BVH_LAYOUT_BVH2: return "BVH2";
45                 case BVH_LAYOUT_BVH4: return "BVH4";
46                 case BVH_LAYOUT_BVH8: return "BVH8";
47                 case BVH_LAYOUT_NONE: return "NONE";
48                 case BVH_LAYOUT_EMBREE: return "EMBREE";
49                 case BVH_LAYOUT_ALL:  return "ALL";
50         }
51         LOG(DFATAL) << "Unsupported BVH layout was passed.";
52         return "";
53 }
54
55 BVHLayout BVHParams::best_bvh_layout(BVHLayout requested_layout,
56                                      BVHLayoutMask supported_layouts)
57 {
58         const BVHLayoutMask requested_layout_mask = (BVHLayoutMask)requested_layout;
59         /* Check whether requested layout is supported, if so -- no need to do
60          * any extra computation.
61          */
62         if(supported_layouts & requested_layout_mask) {
63                 return requested_layout;
64         }
65         /* Some bit magic to get widest supported BVH layout. */
66         /* This is a mask of supported BVH layouts which are narrower than the
67          * requested one.
68          */
69         const BVHLayoutMask allowed_layouts_mask =
70                 (supported_layouts & (requested_layout_mask - 1));
71         /* We get widest from allowed ones and convert mask to actual layout. */
72         const BVHLayoutMask widest_allowed_layout_mask = __bsr(allowed_layouts_mask);
73         return (BVHLayout)(1 << widest_allowed_layout_mask);
74 }
75
76 /* Pack Utility */
77
78 BVHStackEntry::BVHStackEntry(const BVHNode *n, int i)
79     : node(n), idx(i)
80 {
81 }
82
83 int BVHStackEntry::encodeIdx() const
84 {
85         return (node->is_leaf())? ~idx: idx;
86 }
87
88 /* BVH */
89
90 BVH::BVH(const BVHParams& params_, const vector<Object*>& objects_)
91 : params(params_), objects(objects_)
92 {
93 }
94
95 BVH *BVH::create(const BVHParams& params, const vector<Object*>& objects)
96 {
97         switch(params.bvh_layout) {
98                 case BVH_LAYOUT_BVH2:
99                         return new BVH2(params, objects);
100                 case BVH_LAYOUT_BVH4:
101                         return new BVH4(params, objects);
102                 case BVH_LAYOUT_BVH8:
103                         return new BVH8(params, objects);
104                 case BVH_LAYOUT_EMBREE:
105 #ifdef WITH_EMBREE
106                         return new BVHEmbree(params, objects);
107 #endif
108                 case BVH_LAYOUT_NONE:
109                 case BVH_LAYOUT_ALL:
110                         break;
111         }
112         LOG(DFATAL) << "Requested unsupported BVH layout.";
113         return NULL;
114 }
115
116 /* Building */
117
118 void BVH::build(Progress& progress, Stats*)
119 {
120         progress.set_substatus("Building BVH");
121
122         /* build nodes */
123         BVHBuild bvh_build(objects,
124                            pack.prim_type,
125                            pack.prim_index,
126                            pack.prim_object,
127                            pack.prim_time,
128                            params,
129                            progress);
130         BVHNode *root = bvh_build.run();
131
132         if(progress.get_cancel()) {
133                 if(root) root->deleteSubtree();
134                 return;
135         }
136
137         /* pack triangles */
138         progress.set_substatus("Packing BVH triangles and strands");
139         pack_primitives();
140
141         if(progress.get_cancel()) {
142                 root->deleteSubtree();
143                 return;
144         }
145
146         /* pack nodes */
147         progress.set_substatus("Packing BVH nodes");
148         pack_nodes(root);
149
150         /* free build nodes */
151         root->deleteSubtree();
152 }
153
154 /* Refitting */
155
156 void BVH::refit(Progress& progress)
157 {
158         progress.set_substatus("Packing BVH primitives");
159         pack_primitives();
160
161         if(progress.get_cancel()) return;
162
163         progress.set_substatus("Refitting BVH nodes");
164         refit_nodes();
165 }
166
167 void BVH::refit_primitives(int start, int end, BoundBox& bbox, uint& visibility)
168 {
169         /* Refit range of primitives. */
170         for(int prim = start; prim < end; prim++) {
171                 int pidx = pack.prim_index[prim];
172                 int tob = pack.prim_object[prim];
173                 Object *ob = objects[tob];
174
175                 if(pidx == -1) {
176                         /* Object instance. */
177                         bbox.grow(ob->bounds);
178                 }
179                 else {
180                         /* Primitives. */
181                         const Mesh *mesh = ob->mesh;
182
183                         if(pack.prim_type[prim] & PRIMITIVE_ALL_CURVE) {
184                                 /* Curves. */
185                                 int str_offset = (params.top_level)? mesh->curve_offset: 0;
186                                 Mesh::Curve curve = mesh->get_curve(pidx - str_offset);
187                                 int k = PRIMITIVE_UNPACK_SEGMENT(pack.prim_type[prim]);
188
189                                 curve.bounds_grow(k, &mesh->curve_keys[0], &mesh->curve_radius[0], bbox);
190
191                                 visibility |= PATH_RAY_CURVE;
192
193                                 /* Motion curves. */
194                                 if(mesh->use_motion_blur) {
195                                         Attribute *attr = mesh->curve_attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
196
197                                         if(attr) {
198                                                 size_t mesh_size = mesh->curve_keys.size();
199                                                 size_t steps = mesh->motion_steps - 1;
200                                                 float3 *key_steps = attr->data_float3();
201
202                                                 for(size_t i = 0; i < steps; i++)
203                                                         curve.bounds_grow(k, key_steps + i*mesh_size, &mesh->curve_radius[0], bbox);
204                                         }
205                                 }
206                         }
207                         else {
208                                 /* Triangles. */
209                                 int tri_offset = (params.top_level)? mesh->tri_offset: 0;
210                                 Mesh::Triangle triangle = mesh->get_triangle(pidx - tri_offset);
211                                 const float3 *vpos = &mesh->verts[0];
212
213                                 triangle.bounds_grow(vpos, bbox);
214
215                                 /* Motion triangles. */
216                                 if(mesh->use_motion_blur) {
217                                         Attribute *attr = mesh->attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
218
219                                         if(attr) {
220                                                 size_t mesh_size = mesh->verts.size();
221                                                 size_t steps = mesh->motion_steps - 1;
222                                                 float3 *vert_steps = attr->data_float3();
223
224                                                 for(size_t i = 0; i < steps; i++)
225                                                         triangle.bounds_grow(vert_steps + i*mesh_size, bbox);
226                                         }
227                                 }
228                         }
229                 }
230                 visibility |= ob->visibility_for_tracing();
231
232         }
233 }
234
235 bool BVH::leaf_check(const BVHNode *node, BVH_TYPE bvh)
236 {
237         if(node->is_leaf()) {
238                 return node->is_unaligned;
239         }
240         else {
241                 return node_is_unaligned(node, bvh);
242         }
243 }
244
245 bool BVH::node_is_unaligned(const BVHNode *node, BVH_TYPE bvh)
246 {
247         const BVHNode *node0 = node->get_child(0);
248         const BVHNode *node1 = node->get_child(1);
249
250         switch(bvh) {
251                 case bvh2:
252                         return node0->is_unaligned || node1->is_unaligned;
253                         break;
254                 case bvh4:
255                         return leaf_check(node0, bvh2) || leaf_check(node1, bvh2);
256                         break;
257                 case bvh8:
258                         return leaf_check(node0, bvh4) || leaf_check(node1, bvh4);
259                         break;
260                 default:
261                         assert(0);
262                         return false;
263         }
264 }
265
266 /* Triangles */
267
268 void BVH::pack_triangle(int idx, float4 tri_verts[3])
269 {
270         int tob = pack.prim_object[idx];
271         assert(tob >= 0 && tob < objects.size());
272         const Mesh *mesh = objects[tob]->mesh;
273
274         int tidx = pack.prim_index[idx];
275         Mesh::Triangle t = mesh->get_triangle(tidx);
276         const float3 *vpos = &mesh->verts[0];
277         float3 v0 = vpos[t.v[0]];
278         float3 v1 = vpos[t.v[1]];
279         float3 v2 = vpos[t.v[2]];
280
281         tri_verts[0] = float3_to_float4(v0);
282         tri_verts[1] = float3_to_float4(v1);
283         tri_verts[2] = float3_to_float4(v2);
284 }
285
286 void BVH::pack_primitives()
287 {
288         const size_t tidx_size = pack.prim_index.size();
289         size_t num_prim_triangles = 0;
290         /* Count number of triangles primitives in BVH. */
291         for(unsigned int i = 0; i < tidx_size; i++) {
292                 if((pack.prim_index[i] != -1)) {
293                         if((pack.prim_type[i] & PRIMITIVE_ALL_TRIANGLE) != 0) {
294                                 ++num_prim_triangles;
295                         }
296                 }
297         }
298         /* Reserve size for arrays. */
299         pack.prim_tri_index.clear();
300         pack.prim_tri_index.resize(tidx_size);
301         pack.prim_tri_verts.clear();
302         pack.prim_tri_verts.resize(num_prim_triangles * 3);
303         pack.prim_visibility.clear();
304         pack.prim_visibility.resize(tidx_size);
305         /* Fill in all the arrays. */
306         size_t prim_triangle_index = 0;
307         for(unsigned int i = 0; i < tidx_size; i++) {
308                 if(pack.prim_index[i] != -1) {
309                         int tob = pack.prim_object[i];
310                         Object *ob = objects[tob];
311                         if((pack.prim_type[i] & PRIMITIVE_ALL_TRIANGLE) != 0) {
312                                 pack_triangle(i, (float4*)&pack.prim_tri_verts[3 * prim_triangle_index]);
313                                 pack.prim_tri_index[i] = 3 * prim_triangle_index;
314                                 ++prim_triangle_index;
315                         }
316                         else {
317                                 pack.prim_tri_index[i] = -1;
318                         }
319                         pack.prim_visibility[i] = ob->visibility_for_tracing();
320                         if(pack.prim_type[i] & PRIMITIVE_ALL_CURVE) {
321                                 pack.prim_visibility[i] |= PATH_RAY_CURVE;
322                         }
323                 }
324                 else {
325                         pack.prim_tri_index[i] = -1;
326                         pack.prim_visibility[i] = 0;
327                 }
328         }
329 }
330
331 /* Pack Instances */
332
333 void BVH::pack_instances(size_t nodes_size, size_t leaf_nodes_size)
334 {
335         /* The BVH's for instances are built separately, but for traversal all
336          * BVH's are stored in global arrays. This function merges them into the
337          * top level BVH, adjusting indexes and offsets where appropriate.
338          */
339         const bool use_qbvh = (params.bvh_layout == BVH_LAYOUT_BVH4);
340         const bool use_obvh = (params.bvh_layout == BVH_LAYOUT_BVH8);
341
342         /* Adjust primitive index to point to the triangle in the global array, for
343          * meshes with transform applied and already in the top level BVH.
344          */
345         for(size_t i = 0; i < pack.prim_index.size(); i++)
346                 if(pack.prim_index[i] != -1) {
347                         if(pack.prim_type[i] & PRIMITIVE_ALL_CURVE)
348                                 pack.prim_index[i] += objects[pack.prim_object[i]]->mesh->curve_offset;
349                         else
350                                 pack.prim_index[i] += objects[pack.prim_object[i]]->mesh->tri_offset;
351                 }
352
353         /* track offsets of instanced BVH data in global array */
354         size_t prim_offset = pack.prim_index.size();
355         size_t nodes_offset = nodes_size;
356         size_t nodes_leaf_offset = leaf_nodes_size;
357
358         /* clear array that gives the node indexes for instanced objects */
359         pack.object_node.clear();
360
361         /* reserve */
362         size_t prim_index_size = pack.prim_index.size();
363         size_t prim_tri_verts_size = pack.prim_tri_verts.size();
364
365         size_t pack_prim_index_offset = prim_index_size;
366         size_t pack_prim_tri_verts_offset = prim_tri_verts_size;
367         size_t pack_nodes_offset = nodes_size;
368         size_t pack_leaf_nodes_offset = leaf_nodes_size;
369         size_t object_offset = 0;
370
371         map<Mesh*, int> mesh_map;
372
373         foreach(Object *ob, objects) {
374                 Mesh *mesh = ob->mesh;
375                 BVH *bvh = mesh->bvh;
376
377                 if(mesh->need_build_bvh()) {
378                         if(mesh_map.find(mesh) == mesh_map.end()) {
379                                 prim_index_size += bvh->pack.prim_index.size();
380                                 prim_tri_verts_size += bvh->pack.prim_tri_verts.size();
381                                 nodes_size += bvh->pack.nodes.size();
382                                 leaf_nodes_size += bvh->pack.leaf_nodes.size();
383
384                                 mesh_map[mesh] = 1;
385                         }
386                 }
387         }
388
389         mesh_map.clear();
390
391         pack.prim_index.resize(prim_index_size);
392         pack.prim_type.resize(prim_index_size);
393         pack.prim_object.resize(prim_index_size);
394         pack.prim_visibility.resize(prim_index_size);
395         pack.prim_tri_verts.resize(prim_tri_verts_size);
396         pack.prim_tri_index.resize(prim_index_size);
397         pack.nodes.resize(nodes_size);
398         pack.leaf_nodes.resize(leaf_nodes_size);
399         pack.object_node.resize(objects.size());
400
401         if(params.num_motion_curve_steps > 0 || params.num_motion_triangle_steps > 0) {
402                 pack.prim_time.resize(prim_index_size);
403         }
404
405         int *pack_prim_index = (pack.prim_index.size())? &pack.prim_index[0]: NULL;
406         int *pack_prim_type = (pack.prim_type.size())? &pack.prim_type[0]: NULL;
407         int *pack_prim_object = (pack.prim_object.size())? &pack.prim_object[0]: NULL;
408         uint *pack_prim_visibility = (pack.prim_visibility.size())? &pack.prim_visibility[0]: NULL;
409         float4 *pack_prim_tri_verts = (pack.prim_tri_verts.size())? &pack.prim_tri_verts[0]: NULL;
410         uint *pack_prim_tri_index = (pack.prim_tri_index.size())? &pack.prim_tri_index[0]: NULL;
411         int4 *pack_nodes = (pack.nodes.size())? &pack.nodes[0]: NULL;
412         int4 *pack_leaf_nodes = (pack.leaf_nodes.size())? &pack.leaf_nodes[0]: NULL;
413         float2 *pack_prim_time = (pack.prim_time.size())? &pack.prim_time[0]: NULL;
414
415         /* merge */
416         foreach(Object *ob, objects) {
417                 Mesh *mesh = ob->mesh;
418
419                 /* We assume that if mesh doesn't need own BVH it was already included
420                  * into a top-level BVH and no packing here is needed.
421                  */
422                 if(!mesh->need_build_bvh()) {
423                         pack.object_node[object_offset++] = 0;
424                         continue;
425                 }
426
427                 /* if mesh already added once, don't add it again, but used set
428                  * node offset for this object */
429                 map<Mesh*, int>::iterator it = mesh_map.find(mesh);
430
431                 if(mesh_map.find(mesh) != mesh_map.end()) {
432                         int noffset = it->second;
433                         pack.object_node[object_offset++] = noffset;
434                         continue;
435                 }
436
437                 BVH *bvh = mesh->bvh;
438
439                 int noffset = nodes_offset;
440                 int noffset_leaf = nodes_leaf_offset;
441                 int mesh_tri_offset = mesh->tri_offset;
442                 int mesh_curve_offset = mesh->curve_offset;
443
444                 /* fill in node indexes for instances */
445                 if(bvh->pack.root_index == -1)
446                         pack.object_node[object_offset++] = -noffset_leaf-1;
447                 else
448                         pack.object_node[object_offset++] = noffset;
449
450                 mesh_map[mesh] = pack.object_node[object_offset-1];
451
452                 /* merge primitive, object and triangle indexes */
453                 if(bvh->pack.prim_index.size()) {
454                         size_t bvh_prim_index_size = bvh->pack.prim_index.size();
455                         int *bvh_prim_index = &bvh->pack.prim_index[0];
456                         int *bvh_prim_type = &bvh->pack.prim_type[0];
457                         uint *bvh_prim_visibility = &bvh->pack.prim_visibility[0];
458                         uint *bvh_prim_tri_index = &bvh->pack.prim_tri_index[0];
459                         float2 *bvh_prim_time = bvh->pack.prim_time.size()? &bvh->pack.prim_time[0]: NULL;
460
461                         for(size_t i = 0; i < bvh_prim_index_size; i++) {
462                                 if(bvh->pack.prim_type[i] & PRIMITIVE_ALL_CURVE) {
463                                         pack_prim_index[pack_prim_index_offset] = bvh_prim_index[i] + mesh_curve_offset;
464                                         pack_prim_tri_index[pack_prim_index_offset] = -1;
465                                 }
466                                 else {
467                                         pack_prim_index[pack_prim_index_offset] = bvh_prim_index[i] + mesh_tri_offset;
468                                         pack_prim_tri_index[pack_prim_index_offset] =
469                                                 bvh_prim_tri_index[i] + pack_prim_tri_verts_offset;
470                                 }
471
472                                 pack_prim_type[pack_prim_index_offset] = bvh_prim_type[i];
473                                 pack_prim_visibility[pack_prim_index_offset] = bvh_prim_visibility[i];
474                                 pack_prim_object[pack_prim_index_offset] = 0;  // unused for instances
475                                 if(bvh_prim_time != NULL) {
476                                         pack_prim_time[pack_prim_index_offset] = bvh_prim_time[i];
477                                 }
478                                 pack_prim_index_offset++;
479                         }
480                 }
481
482                 /* Merge triangle vertices data. */
483                 if(bvh->pack.prim_tri_verts.size()) {
484                         const size_t prim_tri_size = bvh->pack.prim_tri_verts.size();
485                         memcpy(pack_prim_tri_verts + pack_prim_tri_verts_offset,
486                                &bvh->pack.prim_tri_verts[0],
487                                prim_tri_size*sizeof(float4));
488                         pack_prim_tri_verts_offset += prim_tri_size;
489                 }
490
491                 /* merge nodes */
492                 if(bvh->pack.leaf_nodes.size()) {
493                         int4 *leaf_nodes_offset = &bvh->pack.leaf_nodes[0];
494                         size_t leaf_nodes_offset_size = bvh->pack.leaf_nodes.size();
495                         for(size_t i = 0, j = 0;
496                             i < leaf_nodes_offset_size;
497                             i += BVH_NODE_LEAF_SIZE, j++)
498                         {
499                                 int4 data = leaf_nodes_offset[i];
500                                 data.x += prim_offset;
501                                 data.y += prim_offset;
502                                 pack_leaf_nodes[pack_leaf_nodes_offset] = data;
503                                 for(int j = 1; j < BVH_NODE_LEAF_SIZE; ++j) {
504                                         pack_leaf_nodes[pack_leaf_nodes_offset + j] = leaf_nodes_offset[i + j];
505                                 }
506                                 pack_leaf_nodes_offset += BVH_NODE_LEAF_SIZE;
507                         }
508                 }
509
510                 if(bvh->pack.nodes.size()) {
511                         int4 *bvh_nodes = &bvh->pack.nodes[0];
512                         size_t bvh_nodes_size = bvh->pack.nodes.size();
513
514                         for(size_t i = 0, j = 0; i < bvh_nodes_size; j++) {
515                                 size_t nsize, nsize_bbox;
516                                 if(bvh_nodes[i].x & PATH_RAY_NODE_UNALIGNED) {
517                                         if(use_obvh) {
518                                                 nsize = BVH_UNALIGNED_ONODE_SIZE;
519                                                 nsize_bbox = BVH_UNALIGNED_ONODE_SIZE-1;
520                                         }
521                                         else {
522                                                 nsize = use_qbvh
523                                                         ? BVH_UNALIGNED_QNODE_SIZE
524                                                         : BVH_UNALIGNED_NODE_SIZE;
525                                                 nsize_bbox = (use_qbvh) ? BVH_UNALIGNED_QNODE_SIZE-1 : 0;
526                                         }
527                                 }
528                                 else {
529                                         if(use_obvh) {
530                                                 nsize = BVH_ONODE_SIZE;
531                                                 nsize_bbox = BVH_ONODE_SIZE-1;
532                                         }
533                                         else {
534                                                 nsize = (use_qbvh)? BVH_QNODE_SIZE: BVH_NODE_SIZE;
535                                                 nsize_bbox = (use_qbvh)? BVH_QNODE_SIZE-1 : 0;
536                                         }
537                                 }
538
539                                 memcpy(pack_nodes + pack_nodes_offset,
540                                        bvh_nodes + i,
541                                        nsize_bbox*sizeof(int4));
542
543                                 /* Modify offsets into arrays */
544                                 int4 data = bvh_nodes[i + nsize_bbox];
545                                 int4 data1 = bvh_nodes[i + nsize_bbox-1];
546                                 if(use_obvh) {
547                                         data.z += (data.z < 0) ? -noffset_leaf : noffset;
548                                         data.w += (data.w < 0) ? -noffset_leaf : noffset;
549                                         data.x += (data.x < 0) ? -noffset_leaf : noffset;
550                                         data.y += (data.y < 0) ? -noffset_leaf : noffset;
551                                         data1.z += (data1.z < 0) ? -noffset_leaf : noffset;
552                                         data1.w += (data1.w < 0) ? -noffset_leaf : noffset;
553                                         data1.x += (data1.x < 0) ? -noffset_leaf : noffset;
554                                         data1.y += (data1.y < 0) ? -noffset_leaf : noffset;
555                                 }
556                                 else {
557                                         data.z += (data.z < 0) ? -noffset_leaf : noffset;
558                                         data.w += (data.w < 0) ? -noffset_leaf : noffset;
559                                         if(use_qbvh) {
560                                                 data.x += (data.x < 0)? -noffset_leaf: noffset;
561                                                 data.y += (data.y < 0)? -noffset_leaf: noffset;
562                                         }
563                                 }
564                                 pack_nodes[pack_nodes_offset + nsize_bbox] = data;
565                                 if(use_obvh) {
566                                         pack_nodes[pack_nodes_offset + nsize_bbox - 1] = data1;
567                                 }
568
569                                 /* Usually this copies nothing, but we better
570                                  * be prepared for possible node size extension.
571                                  */
572                                 memcpy(&pack_nodes[pack_nodes_offset + nsize_bbox+1],
573                                        &bvh_nodes[i + nsize_bbox+1],
574                                        sizeof(int4) * (nsize - (nsize_bbox+1)));
575
576                                 pack_nodes_offset += nsize;
577                                 i += nsize;
578                         }
579                 }
580
581                 nodes_offset += bvh->pack.nodes.size();
582                 nodes_leaf_offset += bvh->pack.leaf_nodes.size();
583                 prim_offset += bvh->pack.prim_index.size();
584         }
585 }
586
587 CCL_NAMESPACE_END