style cleanup: block comments
[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 "mesh.h"
19 #include "object.h"
20 #include "scene.h"
21
22 #include "bvh.h"
23 #include "bvh_build.h"
24 #include "bvh_node.h"
25 #include "bvh_params.h"
26
27 #include "util_cache.h"
28 #include "util_debug.h"
29 #include "util_foreach.h"
30 #include "util_map.h"
31 #include "util_progress.h"
32 #include "util_types.h"
33
34 CCL_NAMESPACE_BEGIN
35
36 /* Pack Utility */
37
38 struct BVHStackEntry
39 {
40         const BVHNode *node;
41         int idx;
42
43         BVHStackEntry(const BVHNode* n = 0, int i = 0)
44         : node(n), idx(i)
45         {
46         }
47
48         int encodeIdx() const
49         {
50                 return (node->is_leaf())? ~idx: idx;
51         }
52 };
53
54 /* BVH */
55
56 BVH::BVH(const BVHParams& params_, const vector<Object*>& objects_)
57 : params(params_), objects(objects_)
58 {
59 }
60
61 BVH *BVH::create(const BVHParams& params, const vector<Object*>& objects)
62 {
63         if(params.use_qbvh)
64                 return new QBVH(params, objects);
65         else
66                 return new RegularBVH(params, objects);
67 }
68
69 /* Cache */
70
71 bool BVH::cache_read(CacheData& key)
72 {
73         key.add(&params, sizeof(params));
74
75         foreach(Object *ob, objects) {
76                 key.add(ob->mesh->verts);
77                 key.add(ob->mesh->triangles);
78                 key.add(&ob->bounds, sizeof(ob->bounds));
79                 key.add(&ob->visibility, sizeof(ob->visibility));
80                 key.add(&ob->mesh->transform_applied, sizeof(bool));
81         }
82
83         CacheData value;
84
85         if(Cache::global.lookup(key, value)) {
86                 cache_filename = key.get_filename();
87
88                 value.read(pack.root_index);
89                 value.read(pack.SAH);
90
91                 value.read(pack.nodes);
92                 value.read(pack.object_node);
93                 value.read(pack.tri_woop);
94                 value.read(pack.prim_visibility);
95                 value.read(pack.prim_index);
96                 value.read(pack.prim_object);
97                 value.read(pack.is_leaf);
98
99                 return true;
100         }
101
102         return false;
103 }
104
105 void BVH::cache_write(CacheData& key)
106 {
107         CacheData value;
108
109         value.add(pack.root_index);
110         value.add(pack.SAH);
111
112         value.add(pack.nodes);
113         value.add(pack.object_node);
114         value.add(pack.tri_woop);
115         value.add(pack.prim_visibility);
116         value.add(pack.prim_index);
117         value.add(pack.prim_object);
118         value.add(pack.is_leaf);
119
120         Cache::global.insert(key, value);
121
122         cache_filename = key.get_filename();
123 }
124
125 void BVH::clear_cache_except()
126 {
127         set<string> except;
128
129         if(!cache_filename.empty())
130                 except.insert(cache_filename);
131
132         foreach(Object *ob, objects) {
133                 Mesh *mesh = ob->mesh;
134                 BVH *bvh = mesh->bvh;
135
136                 if(bvh && !bvh->cache_filename.empty())
137                         except.insert(bvh->cache_filename);
138         }
139
140         Cache::global.clear_except("bvh", except);
141 }
142
143 /* Building */
144
145 void BVH::build(Progress& progress)
146 {
147         progress.set_substatus("Building BVH");
148
149         /* cache read */
150         CacheData key("bvh");
151
152         if(params.use_cache) {
153                 progress.set_substatus("Looking in BVH cache");
154
155                 if(cache_read(key))
156                         return;
157         }
158
159         /* build nodes */
160         vector<int> prim_index;
161         vector<int> prim_object;
162
163         BVHBuild bvh_build(objects, prim_index, prim_object, params, progress);
164         BVHNode *root = bvh_build.run();
165
166         if(progress.get_cancel()) {
167                 if(root) root->deleteSubtree();
168                 return;
169         }
170
171         /* todo: get rid of this copy */
172         pack.prim_index = prim_index;
173         pack.prim_object = prim_object;
174
175         /* compute SAH */
176         if(!params.top_level)
177                 pack.SAH = root->computeSubtreeSAHCost(params);
178
179         if(progress.get_cancel()) {
180                 root->deleteSubtree();
181                 return;
182         }
183
184         /* pack triangles */
185         progress.set_substatus("Packing BVH triangles");
186         pack_triangles();
187
188         if(progress.get_cancel()) {
189                 root->deleteSubtree();
190                 return;
191         }
192
193         /* pack nodes */
194         progress.set_substatus("Packing BVH nodes");
195         array<int> tmp_prim_object = pack.prim_object;
196         pack_nodes(tmp_prim_object, root);
197         
198         /* free build nodes */
199         root->deleteSubtree();
200
201         if(progress.get_cancel()) return;
202
203         /* cache write */
204         if(params.use_cache) {
205                 progress.set_substatus("Writing BVH cache");
206                 cache_write(key);
207
208                 /* clear other bvh files from cache */
209                 if(params.top_level)
210                         clear_cache_except();
211         }
212 }
213
214 /* Refitting */
215
216 void BVH::refit(Progress& progress)
217 {
218         progress.set_substatus("Packing BVH triangles");
219         pack_triangles();
220
221         if(progress.get_cancel()) return;
222
223         progress.set_substatus("Refitting BVH nodes");
224         refit_nodes();
225 }
226
227 /* Triangles */
228
229 void BVH::pack_triangle(int idx, float4 woop[3])
230 {
231         /* create Woop triangle */
232         int tob = pack.prim_object[idx];
233         const Mesh *mesh = objects[tob]->mesh;
234         int tidx = pack.prim_index[idx];
235         const int *vidx = mesh->triangles[tidx].v;
236         const float3* vpos = &mesh->verts[0];
237         float3 v0 = vpos[vidx[0]];
238         float3 v1 = vpos[vidx[1]];
239         float3 v2 = vpos[vidx[2]];
240
241         float3 r0 = v0 - v2;
242         float3 r1 = v1 - v2;
243         float3 r2 = cross(r0, r1);
244
245         if(dot(r0, r0) == 0.0f || dot(r1, r1) == 0.0f || dot(r2, r2) == 0.0f) {
246                 /* degenerate */
247                 woop[0] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
248                 woop[1] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
249                 woop[2] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
250         }
251         else {
252                 Transform t = make_transform(
253                         r0.x, r1.x, r2.x, v2.x,
254                         r0.y, r1.y, r2.y, v2.y,
255                         r0.z, r1.z, r2.z, v2.z,
256                         0.0f, 0.0f, 0.0f, 1.0f);
257
258                 t = transform_inverse(t);
259
260                 woop[0] = make_float4(t.z.x, t.z.y, t.z.z, -t.z.w);
261                 woop[1] = make_float4(t.x.x, t.x.y, t.x.z, t.x.w);
262                 woop[2] = make_float4(t.y.x, t.y.y, t.y.z, t.y.w);
263         }
264 }
265
266 void BVH::pack_triangles()
267 {
268         int nsize = TRI_NODE_SIZE;
269         size_t tidx_size = pack.prim_index.size();
270
271         pack.tri_woop.clear();
272         pack.tri_woop.resize(tidx_size * nsize);
273         pack.prim_visibility.clear();
274         pack.prim_visibility.resize(tidx_size);
275
276         for(unsigned int i = 0; i < tidx_size; i++) {
277                 if(pack.prim_index[i] != -1) {
278                         float4 woop[3];
279
280                         pack_triangle(i, woop);
281                         memcpy(&pack.tri_woop[i * nsize], woop, sizeof(float4)*3);
282
283                         int tob = pack.prim_object[i];
284                         Object *ob = objects[tob];
285                         pack.prim_visibility[i] = ob->visibility;
286                 }
287         }
288 }
289
290 /* Pack Instances */
291
292 void BVH::pack_instances(size_t nodes_size)
293 {
294         /* The BVH's for instances are built separately, but for traversal all
295          * BVH's are stored in global arrays. This function merges them into the
296          * top level BVH, adjusting indexes and offsets where appropriate. */
297         bool use_qbvh = params.use_qbvh;
298         size_t nsize = (use_qbvh)? BVH_QNODE_SIZE: BVH_NODE_SIZE;
299
300         /* adjust primitive index to point to the triangle in the global array, for
301          * meshes with transform applied and already in the top level BVH */
302         for(size_t i = 0; i < pack.prim_index.size(); i++)
303                 if(pack.prim_index[i] != -1)
304                         pack.prim_index[i] += objects[pack.prim_object[i]]->mesh->tri_offset;
305
306         /* track offsets of instanced BVH data in global array */
307         size_t tri_offset = pack.prim_index.size();
308         size_t nodes_offset = nodes_size;
309
310         /* clear array that gives the node indexes for instanced objects */
311         pack.object_node.clear();
312
313         /* reserve */
314         size_t prim_index_size = pack.prim_index.size();
315         size_t tri_woop_size = pack.tri_woop.size();
316
317         size_t pack_prim_index_offset = prim_index_size;
318         size_t pack_tri_woop_offset = tri_woop_size;
319         size_t pack_nodes_offset = nodes_size;
320         size_t object_offset = 0;
321
322         map<Mesh*, int> mesh_map;
323
324         foreach(Object *ob, objects) {
325                 Mesh *mesh = ob->mesh;
326                 BVH *bvh = mesh->bvh;
327
328                 if(!mesh->transform_applied) {
329                         if(mesh_map.find(mesh) == mesh_map.end()) {
330                                 prim_index_size += bvh->pack.prim_index.size();
331                                 tri_woop_size += bvh->pack.tri_woop.size();
332                                 nodes_size += bvh->pack.nodes.size()*nsize;
333
334                                 mesh_map[mesh] = 1;
335                         }
336                 }
337         }
338
339         mesh_map.clear();
340
341         pack.prim_index.resize(prim_index_size);
342         pack.prim_object.resize(prim_index_size);
343         pack.prim_visibility.resize(prim_index_size);
344         pack.tri_woop.resize(tri_woop_size);
345         pack.nodes.resize(nodes_size);
346         pack.object_node.resize(objects.size());
347
348         int *pack_prim_index = (pack.prim_index.size())? &pack.prim_index[0]: NULL;
349         int *pack_prim_object = (pack.prim_object.size())? &pack.prim_object[0]: NULL;
350         uint *pack_prim_visibility = (pack.prim_visibility.size())? &pack.prim_visibility[0]: NULL;
351         float4 *pack_tri_woop = (pack.tri_woop.size())? &pack.tri_woop[0]: NULL;
352         int4 *pack_nodes = (pack.nodes.size())? &pack.nodes[0]: NULL;
353
354         /* merge */
355         foreach(Object *ob, objects) {
356                 Mesh *mesh = ob->mesh;
357
358                 /* if mesh transform is applied, that means it's already in the top
359                  * level BVH, and we don't need to merge it in */
360                 if(mesh->transform_applied) {
361                         pack.object_node[object_offset++] = 0;
362                         continue;
363                 }
364
365                 /* if mesh already added once, don't add it again, but used set
366                  * node offset for this object */
367                 map<Mesh*, int>::iterator it = mesh_map.find(mesh);
368
369                 if(mesh_map.find(mesh) != mesh_map.end()) {
370                         int noffset = it->second;
371                         pack.object_node[object_offset++] = noffset;
372                         continue;
373                 }
374
375                 BVH *bvh = mesh->bvh;
376
377                 int noffset = nodes_offset/nsize;
378                 int mesh_tri_offset = mesh->tri_offset;
379
380                 /* fill in node indexes for instances */
381                 if(bvh->pack.is_leaf[0])
382                         pack.object_node[object_offset++] = -noffset-1;
383                 else
384                         pack.object_node[object_offset++] = noffset;
385
386                 mesh_map[mesh] = pack.object_node[object_offset-1];
387
388                 /* merge primitive and object indexes */
389                 if(bvh->pack.prim_index.size()) {
390                         size_t bvh_prim_index_size = bvh->pack.prim_index.size();
391                         int *bvh_prim_index = &bvh->pack.prim_index[0];
392                         uint *bvh_prim_visibility = &bvh->pack.prim_visibility[0];
393
394                         for(size_t i = 0; i < bvh_prim_index_size; i++) {
395                                 pack_prim_index[pack_prim_index_offset] = bvh_prim_index[i] + mesh_tri_offset;
396                                 pack_prim_visibility[pack_prim_index_offset] = bvh_prim_visibility[i];
397                                 pack_prim_object[pack_prim_index_offset] = 0;  // unused for instances
398                                 pack_prim_index_offset++;
399                         }
400                 }
401
402                 /* merge triangle intersection data */
403                 if(bvh->pack.tri_woop.size()) {
404                         memcpy(pack_tri_woop+pack_tri_woop_offset, &bvh->pack.tri_woop[0],
405                                 bvh->pack.tri_woop.size()*sizeof(float4));
406                         pack_tri_woop_offset += bvh->pack.tri_woop.size();
407                 }
408
409                 /* merge nodes */
410                 if( bvh->pack.nodes.size()) {
411                         size_t nsize_bbox = (use_qbvh)? nsize-2: nsize-1;
412                         int4 *bvh_nodes = &bvh->pack.nodes[0];
413                         size_t bvh_nodes_size = bvh->pack.nodes.size(); 
414                         int *bvh_is_leaf = &bvh->pack.is_leaf[0];
415
416                         for(size_t i = 0, j = 0; i < bvh_nodes_size; i+=nsize, j++) {
417                                 memcpy(pack_nodes + pack_nodes_offset, bvh_nodes + i, nsize_bbox*sizeof(int4));
418
419                                 /* modify offsets into arrays */
420                                 int4 data = bvh_nodes[i + nsize_bbox];
421
422                                 if(bvh_is_leaf[j]) {
423                                         data.x += tri_offset;
424                                         data.y += tri_offset;
425                                 }
426                                 else {
427                                         data.x += (data.x < 0)? -noffset: noffset;
428                                         data.y += (data.y < 0)? -noffset: noffset;
429
430                                         if(use_qbvh) {
431                                                 data.z += (data.z < 0)? -noffset: noffset;
432                                                 data.w += (data.w < 0)? -noffset: noffset;
433                                         }
434                                 }
435
436                                 pack_nodes[pack_nodes_offset + nsize_bbox] = data;
437
438                                 if(use_qbvh)
439                                         pack_nodes[pack_nodes_offset + nsize_bbox+1] = bvh_nodes[i + nsize_bbox+1];
440
441                                 pack_nodes_offset += nsize;
442                         }
443                 }
444
445                 nodes_offset += bvh->pack.nodes.size();
446                 tri_offset += bvh->pack.prim_index.size();
447         }
448 }
449
450 /* Regular BVH */
451
452 RegularBVH::RegularBVH(const BVHParams& params_, const vector<Object*>& objects_)
453 : BVH(params_, objects_)
454 {
455 }
456
457 void RegularBVH::pack_leaf(const BVHStackEntry& e, const LeafNode *leaf)
458 {
459         if(leaf->num_triangles() == 1 && pack.prim_index[leaf->m_lo] == -1)
460                 /* object */
461                 pack_node(e.idx, leaf->m_bounds, leaf->m_bounds, ~(leaf->m_lo), 0, leaf->m_visibility, leaf->m_visibility);
462         else
463                 /* triangle */
464                 pack_node(e.idx, leaf->m_bounds, leaf->m_bounds, leaf->m_lo, leaf->m_hi, leaf->m_visibility, leaf->m_visibility);
465 }
466
467 void RegularBVH::pack_inner(const BVHStackEntry& e, const BVHStackEntry& e0, const BVHStackEntry& e1)
468 {
469         pack_node(e.idx, e0.node->m_bounds, e1.node->m_bounds, e0.encodeIdx(), e1.encodeIdx(), e0.node->m_visibility, e1.node->m_visibility);
470 }
471
472 void RegularBVH::pack_node(int idx, const BoundBox& b0, const BoundBox& b1, int c0, int c1, uint visibility0, uint visibility1)
473 {
474         int4 data[BVH_NODE_SIZE] =
475         {
476                 make_int4(__float_as_int(b0.min.x), __float_as_int(b0.max.x), __float_as_int(b0.min.y), __float_as_int(b0.max.y)),
477                 make_int4(__float_as_int(b1.min.x), __float_as_int(b1.max.x), __float_as_int(b1.min.y), __float_as_int(b1.max.y)),
478                 make_int4(__float_as_int(b0.min.z), __float_as_int(b0.max.z), __float_as_int(b1.min.z), __float_as_int(b1.max.z)),
479                 make_int4(c0, c1, visibility0, visibility1)
480         };
481
482         memcpy(&pack.nodes[idx * BVH_NODE_SIZE], data, sizeof(int4)*BVH_NODE_SIZE);
483 }
484
485 void RegularBVH::pack_nodes(const array<int>& prims, const BVHNode *root)
486 {
487         size_t node_size = root->getSubtreeSize(BVH_STAT_NODE_COUNT);
488
489         /* resize arrays */
490         pack.nodes.clear();
491         pack.is_leaf.clear();
492         pack.is_leaf.resize(node_size);
493
494         /* for top level BVH, first merge existing BVH's so we know the offsets */
495         if(params.top_level)
496                 pack_instances(node_size*BVH_NODE_SIZE);
497         else
498                 pack.nodes.resize(node_size*BVH_NODE_SIZE);
499
500         int nextNodeIdx = 0;
501
502         vector<BVHStackEntry> stack;
503         stack.push_back(BVHStackEntry(root, nextNodeIdx++));
504
505         while(stack.size()) {
506                 BVHStackEntry e = stack.back();
507                 stack.pop_back();
508
509                 pack.is_leaf[e.idx] = e.node->is_leaf();
510
511                 if(e.node->is_leaf()) {
512                         /* leaf node */
513                         const LeafNode* leaf = reinterpret_cast<const LeafNode*>(e.node);
514                         pack_leaf(e, leaf);
515                 }
516                 else {
517                         /* innner node */
518                         stack.push_back(BVHStackEntry(e.node->get_child(0), nextNodeIdx++));
519                         stack.push_back(BVHStackEntry(e.node->get_child(1), nextNodeIdx++));
520
521                         pack_inner(e, stack[stack.size()-2], stack[stack.size()-1]);
522                 }
523         }
524
525         /* root index to start traversal at, to handle case of single leaf node */
526         pack.root_index = (pack.is_leaf[0])? -1: 0;
527 }
528
529 void RegularBVH::refit_nodes()
530 {
531         assert(!params.top_level);
532
533         BoundBox bbox = BoundBox::empty;
534         uint visibility = 0;
535         refit_node(0, (pack.is_leaf[0])? true: false, bbox, visibility);
536 }
537
538 void RegularBVH::refit_node(int idx, bool leaf, BoundBox& bbox, uint& visibility)
539 {
540         int4 *data = &pack.nodes[idx*4];
541
542         int c0 = data[3].x;
543         int c1 = data[3].y;
544
545         if(leaf) {
546                 /* refit leaf node */
547                 for(int tri = c0; tri < c1; tri++) {
548                         int tidx = pack.prim_index[tri];
549                         int tob = pack.prim_object[tri];
550                         Object *ob = objects[tob];
551
552                         if(tidx == -1) {
553                                 /* object instance */
554                                 bbox.grow(ob->bounds);
555                         }
556                         else {
557                                 /* triangles */
558                                 const Mesh *mesh = ob->mesh;
559                                 int tri_offset = (params.top_level)? mesh->tri_offset: 0;
560                                 const int *vidx = mesh->triangles[tidx - tri_offset].v;
561                                 const float3 *vpos = &mesh->verts[0];
562
563                                 bbox.grow(vpos[vidx[0]]);
564                                 bbox.grow(vpos[vidx[1]]);
565                                 bbox.grow(vpos[vidx[2]]);
566                         }
567
568                         visibility |= ob->visibility;
569                 }
570
571                 pack_node(idx, bbox, bbox, c0, c1, visibility, visibility);
572         }
573         else {
574                 /* refit inner node, set bbox from children */
575                 BoundBox bbox0 = BoundBox::empty, bbox1 = BoundBox::empty;
576                 uint visibility0 = 0, visibility1 = 0;
577
578                 refit_node((c0 < 0)? -c0-1: c0, (c0 < 0), bbox0, visibility0);
579                 refit_node((c1 < 0)? -c1-1: c1, (c1 < 0), bbox1, visibility1);
580
581                 pack_node(idx, bbox0, bbox1, c0, c1, visibility0, visibility1);
582
583                 bbox.grow(bbox0);
584                 bbox.grow(bbox1);
585                 visibility = visibility0|visibility1;
586         }
587 }
588
589 /* QBVH */
590
591 QBVH::QBVH(const BVHParams& params_, const vector<Object*>& objects_)
592 : BVH(params_, objects_)
593 {
594         params.use_qbvh = true;
595
596         /* todo: use visibility */
597 }
598
599 void QBVH::pack_leaf(const BVHStackEntry& e, const LeafNode *leaf)
600 {
601         float4 data[BVH_QNODE_SIZE];
602
603         memset(data, 0, sizeof(data));
604
605         if(leaf->num_triangles() == 1 && pack.prim_index[leaf->m_lo] == -1) {
606                 /* object */
607                 data[6].x = __int_as_float(~(leaf->m_lo));
608                 data[6].y = __int_as_float(0);
609         }
610         else {
611                 /* triangle */
612                 data[6].x = __int_as_float(leaf->m_lo);
613                 data[6].y = __int_as_float(leaf->m_hi);
614         }
615
616         memcpy(&pack.nodes[e.idx * BVH_QNODE_SIZE], data, sizeof(float4)*BVH_QNODE_SIZE);
617 }
618
619 void QBVH::pack_inner(const BVHStackEntry& e, const BVHStackEntry *en, int num)
620 {
621         float4 data[BVH_QNODE_SIZE];
622
623         for(int i = 0; i < num; i++) {
624                 float3 bb_min = en[i].node->m_bounds.min;
625                 float3 bb_max = en[i].node->m_bounds.max;
626
627                 data[0][i] = bb_min.x;
628                 data[1][i] = bb_max.x;
629                 data[2][i] = bb_min.y;
630                 data[3][i] = bb_max.y;
631                 data[4][i] = bb_min.z;
632                 data[5][i] = bb_max.z;
633
634                 data[6][i] = __int_as_float(en[i].encodeIdx());
635                 data[7][i] = 0.0f;
636         }
637
638         for(int i = num; i < 4; i++) {
639                 data[0][i] = 0.0f;
640                 data[1][i] = 0.0f;
641                 data[2][i] = 0.0f;
642
643                 data[3][i] = 0.0f;
644                 data[4][i] = 0.0f;
645                 data[5][i] = 0.0f;
646
647                 data[6][i] = __int_as_float(0);
648                 data[7][i] = 0.0f;
649         }
650
651         memcpy(&pack.nodes[e.idx * BVH_QNODE_SIZE], data, sizeof(float4)*BVH_QNODE_SIZE);
652 }
653
654 /* Quad SIMD Nodes */
655
656 void QBVH::pack_nodes(const array<int>& prims, const BVHNode *root)
657 {
658         size_t node_size = root->getSubtreeSize(BVH_STAT_NODE_COUNT);
659
660         /* resize arrays */
661         pack.nodes.clear();
662         pack.is_leaf.clear();
663         pack.is_leaf.resize(node_size);
664
665         /* for top level BVH, first merge existing BVH's so we know the offsets */
666         if(params.top_level)
667                 pack_instances(node_size*BVH_QNODE_SIZE);
668         else
669                 pack.nodes.resize(node_size*BVH_QNODE_SIZE);
670
671         int nextNodeIdx = 0;
672
673         vector<BVHStackEntry> stack;
674         stack.push_back(BVHStackEntry(root, nextNodeIdx++));
675
676         while(stack.size()) {
677                 BVHStackEntry e = stack.back();
678                 stack.pop_back();
679
680                 pack.is_leaf[e.idx] = e.node->is_leaf();
681
682                 if(e.node->is_leaf()) {
683                         /* leaf node */
684                         const LeafNode* leaf = reinterpret_cast<const LeafNode*>(e.node);
685                         pack_leaf(e, leaf);
686                 }
687                 else {
688                         /* inner node */
689                         const BVHNode *node = e.node;
690                         const BVHNode *node0 = node->get_child(0);
691                         const BVHNode *node1 = node->get_child(1);
692
693                         /* collect nodes */
694                         const BVHNode *nodes[4];
695                         int numnodes = 0;
696
697                         if(node0->is_leaf()) {
698                                 nodes[numnodes++] = node0;
699                         }
700                         else {
701                                 nodes[numnodes++] = node0->get_child(0);
702                                 nodes[numnodes++] = node0->get_child(1);
703                         }
704
705                         if(node1->is_leaf()) {
706                                 nodes[numnodes++] = node1;
707                         }
708                         else {
709                                 nodes[numnodes++] = node1->get_child(0);
710                                 nodes[numnodes++] = node1->get_child(1);
711                         }
712
713                         /* push entries on the stack */
714                         for(int i = 0; i < numnodes; i++)
715                                 stack.push_back(BVHStackEntry(nodes[i], nextNodeIdx++));
716
717                         /* set node */
718                         pack_inner(e, &stack[stack.size()-numnodes], numnodes);
719                 }
720         }
721
722         /* root index to start traversal at, to handle case of single leaf node */
723         pack.root_index = (pack.is_leaf[0])? -1: 0;
724 }
725
726 void QBVH::refit_nodes()
727 {
728         assert(0); /* todo */
729 }
730
731 CCL_NAMESPACE_END
732