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