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