svn merge ^/trunk/blender -r48749:48754
[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(
382                    /* XXX, brecht. check this is needed!. it could be a bug elsewhere
383                     * /mango/pro/scenes/04_2e/04_2e.blend r2158. on Ian's system 192.168.3.27  - campbell */
384                    (bvh->pack.is_leaf.size() != 0) &&
385
386                    /* previously only checked this */
387                    bvh->pack.is_leaf[0])
388                 {
389                         pack.object_node[object_offset++] = -noffset-1;
390                 }
391                 else {
392                         pack.object_node[object_offset++] = noffset;
393                 }
394
395                 mesh_map[mesh] = pack.object_node[object_offset-1];
396
397                 /* merge primitive and object indexes */
398                 if(bvh->pack.prim_index.size()) {
399                         size_t bvh_prim_index_size = bvh->pack.prim_index.size();
400                         int *bvh_prim_index = &bvh->pack.prim_index[0];
401                         uint *bvh_prim_visibility = &bvh->pack.prim_visibility[0];
402
403                         for(size_t i = 0; i < bvh_prim_index_size; i++) {
404                                 pack_prim_index[pack_prim_index_offset] = bvh_prim_index[i] + mesh_tri_offset;
405                                 pack_prim_visibility[pack_prim_index_offset] = bvh_prim_visibility[i];
406                                 pack_prim_object[pack_prim_index_offset] = 0;  // unused for instances
407                                 pack_prim_index_offset++;
408                         }
409                 }
410
411                 /* merge triangle intersection data */
412                 if(bvh->pack.tri_woop.size()) {
413                         memcpy(pack_tri_woop+pack_tri_woop_offset, &bvh->pack.tri_woop[0],
414                                 bvh->pack.tri_woop.size()*sizeof(float4));
415                         pack_tri_woop_offset += bvh->pack.tri_woop.size();
416                 }
417
418                 /* merge nodes */
419                 if( bvh->pack.nodes.size()) {
420                         size_t nsize_bbox = (use_qbvh)? nsize-2: nsize-1;
421                         int4 *bvh_nodes = &bvh->pack.nodes[0];
422                         size_t bvh_nodes_size = bvh->pack.nodes.size(); 
423                         int *bvh_is_leaf = &bvh->pack.is_leaf[0];
424
425                         for(size_t i = 0, j = 0; i < bvh_nodes_size; i+=nsize, j++) {
426                                 memcpy(pack_nodes + pack_nodes_offset, bvh_nodes + i, nsize_bbox*sizeof(int4));
427
428                                 /* modify offsets into arrays */
429                                 int4 data = bvh_nodes[i + nsize_bbox];
430
431                                 if(bvh_is_leaf[j]) {
432                                         data.x += tri_offset;
433                                         data.y += tri_offset;
434                                 }
435                                 else {
436                                         data.x += (data.x < 0)? -noffset: noffset;
437                                         data.y += (data.y < 0)? -noffset: noffset;
438
439                                         if(use_qbvh) {
440                                                 data.z += (data.z < 0)? -noffset: noffset;
441                                                 data.w += (data.w < 0)? -noffset: noffset;
442                                         }
443                                 }
444
445                                 pack_nodes[pack_nodes_offset + nsize_bbox] = data;
446
447                                 if(use_qbvh)
448                                         pack_nodes[pack_nodes_offset + nsize_bbox+1] = bvh_nodes[i + nsize_bbox+1];
449
450                                 pack_nodes_offset += nsize;
451                         }
452                 }
453
454                 nodes_offset += bvh->pack.nodes.size();
455                 tri_offset += bvh->pack.prim_index.size();
456         }
457 }
458
459 /* Regular BVH */
460
461 RegularBVH::RegularBVH(const BVHParams& params_, const vector<Object*>& objects_)
462 : BVH(params_, objects_)
463 {
464 }
465
466 void RegularBVH::pack_leaf(const BVHStackEntry& e, const LeafNode *leaf)
467 {
468         if(leaf->num_triangles() == 1 && pack.prim_index[leaf->m_lo] == -1)
469                 /* object */
470                 pack_node(e.idx, leaf->m_bounds, leaf->m_bounds, ~(leaf->m_lo), 0, leaf->m_visibility, leaf->m_visibility);
471         else
472                 /* triangle */
473                 pack_node(e.idx, leaf->m_bounds, leaf->m_bounds, leaf->m_lo, leaf->m_hi, leaf->m_visibility, leaf->m_visibility);
474 }
475
476 void RegularBVH::pack_inner(const BVHStackEntry& e, const BVHStackEntry& e0, const BVHStackEntry& e1)
477 {
478         pack_node(e.idx, e0.node->m_bounds, e1.node->m_bounds, e0.encodeIdx(), e1.encodeIdx(), e0.node->m_visibility, e1.node->m_visibility);
479 }
480
481 void RegularBVH::pack_node(int idx, const BoundBox& b0, const BoundBox& b1, int c0, int c1, uint visibility0, uint visibility1)
482 {
483         int4 data[BVH_NODE_SIZE] =
484         {
485                 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)),
486                 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)),
487                 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)),
488                 make_int4(c0, c1, visibility0, visibility1)
489         };
490
491         memcpy(&pack.nodes[idx * BVH_NODE_SIZE], data, sizeof(int4)*BVH_NODE_SIZE);
492 }
493
494 void RegularBVH::pack_nodes(const array<int>& prims, const BVHNode *root)
495 {
496         size_t node_size = root->getSubtreeSize(BVH_STAT_NODE_COUNT);
497
498         /* resize arrays */
499         pack.nodes.clear();
500         pack.is_leaf.clear();
501         pack.is_leaf.resize(node_size);
502
503         /* for top level BVH, first merge existing BVH's so we know the offsets */
504         if(params.top_level)
505                 pack_instances(node_size*BVH_NODE_SIZE);
506         else
507                 pack.nodes.resize(node_size*BVH_NODE_SIZE);
508
509         int nextNodeIdx = 0;
510
511         vector<BVHStackEntry> stack;
512         stack.push_back(BVHStackEntry(root, nextNodeIdx++));
513
514         while(stack.size()) {
515                 BVHStackEntry e = stack.back();
516                 stack.pop_back();
517
518                 pack.is_leaf[e.idx] = e.node->is_leaf();
519
520                 if(e.node->is_leaf()) {
521                         /* leaf node */
522                         const LeafNode* leaf = reinterpret_cast<const LeafNode*>(e.node);
523                         pack_leaf(e, leaf);
524                 }
525                 else {
526                         /* innner node */
527                         stack.push_back(BVHStackEntry(e.node->get_child(0), nextNodeIdx++));
528                         stack.push_back(BVHStackEntry(e.node->get_child(1), nextNodeIdx++));
529
530                         pack_inner(e, stack[stack.size()-2], stack[stack.size()-1]);
531                 }
532         }
533
534         /* root index to start traversal at, to handle case of single leaf node */
535         pack.root_index = (pack.is_leaf[0])? -1: 0;
536 }
537
538 void RegularBVH::refit_nodes()
539 {
540         assert(!params.top_level);
541
542         BoundBox bbox = BoundBox::empty;
543         uint visibility = 0;
544         refit_node(0, (pack.is_leaf[0])? true: false, bbox, visibility);
545 }
546
547 void RegularBVH::refit_node(int idx, bool leaf, BoundBox& bbox, uint& visibility)
548 {
549         int4 *data = &pack.nodes[idx*4];
550
551         int c0 = data[3].x;
552         int c1 = data[3].y;
553
554         if(leaf) {
555                 /* refit leaf node */
556                 for(int tri = c0; tri < c1; tri++) {
557                         int tidx = pack.prim_index[tri];
558                         int tob = pack.prim_object[tri];
559                         Object *ob = objects[tob];
560
561                         if(tidx == -1) {
562                                 /* object instance */
563                                 bbox.grow(ob->bounds);
564                         }
565                         else {
566                                 /* triangles */
567                                 const Mesh *mesh = ob->mesh;
568                                 int tri_offset = (params.top_level)? mesh->tri_offset: 0;
569                                 const int *vidx = mesh->triangles[tidx - tri_offset].v;
570                                 const float3 *vpos = &mesh->verts[0];
571
572                                 bbox.grow(vpos[vidx[0]]);
573                                 bbox.grow(vpos[vidx[1]]);
574                                 bbox.grow(vpos[vidx[2]]);
575                         }
576
577                         visibility |= ob->visibility;
578                 }
579
580                 pack_node(idx, bbox, bbox, c0, c1, visibility, visibility);
581         }
582         else {
583                 /* refit inner node, set bbox from children */
584                 BoundBox bbox0 = BoundBox::empty, bbox1 = BoundBox::empty;
585                 uint visibility0 = 0, visibility1 = 0;
586
587                 refit_node((c0 < 0)? -c0-1: c0, (c0 < 0), bbox0, visibility0);
588                 refit_node((c1 < 0)? -c1-1: c1, (c1 < 0), bbox1, visibility1);
589
590                 pack_node(idx, bbox0, bbox1, c0, c1, visibility0, visibility1);
591
592                 bbox.grow(bbox0);
593                 bbox.grow(bbox1);
594                 visibility = visibility0|visibility1;
595         }
596 }
597
598 /* QBVH */
599
600 QBVH::QBVH(const BVHParams& params_, const vector<Object*>& objects_)
601 : BVH(params_, objects_)
602 {
603         params.use_qbvh = true;
604
605         /* todo: use visibility */
606 }
607
608 void QBVH::pack_leaf(const BVHStackEntry& e, const LeafNode *leaf)
609 {
610         float4 data[BVH_QNODE_SIZE];
611
612         memset(data, 0, sizeof(data));
613
614         if(leaf->num_triangles() == 1 && pack.prim_index[leaf->m_lo] == -1) {
615                 /* object */
616                 data[6].x = __int_as_float(~(leaf->m_lo));
617                 data[6].y = __int_as_float(0);
618         }
619         else {
620                 /* triangle */
621                 data[6].x = __int_as_float(leaf->m_lo);
622                 data[6].y = __int_as_float(leaf->m_hi);
623         }
624
625         memcpy(&pack.nodes[e.idx * BVH_QNODE_SIZE], data, sizeof(float4)*BVH_QNODE_SIZE);
626 }
627
628 void QBVH::pack_inner(const BVHStackEntry& e, const BVHStackEntry *en, int num)
629 {
630         float4 data[BVH_QNODE_SIZE];
631
632         for(int i = 0; i < num; i++) {
633                 float3 bb_min = en[i].node->m_bounds.min;
634                 float3 bb_max = en[i].node->m_bounds.max;
635
636                 data[0][i] = bb_min.x;
637                 data[1][i] = bb_max.x;
638                 data[2][i] = bb_min.y;
639                 data[3][i] = bb_max.y;
640                 data[4][i] = bb_min.z;
641                 data[5][i] = bb_max.z;
642
643                 data[6][i] = __int_as_float(en[i].encodeIdx());
644                 data[7][i] = 0.0f;
645         }
646
647         for(int i = num; i < 4; i++) {
648                 data[0][i] = 0.0f;
649                 data[1][i] = 0.0f;
650                 data[2][i] = 0.0f;
651
652                 data[3][i] = 0.0f;
653                 data[4][i] = 0.0f;
654                 data[5][i] = 0.0f;
655
656                 data[6][i] = __int_as_float(0);
657                 data[7][i] = 0.0f;
658         }
659
660         memcpy(&pack.nodes[e.idx * BVH_QNODE_SIZE], data, sizeof(float4)*BVH_QNODE_SIZE);
661 }
662
663 /* Quad SIMD Nodes */
664
665 void QBVH::pack_nodes(const array<int>& prims, const BVHNode *root)
666 {
667         size_t node_size = root->getSubtreeSize(BVH_STAT_NODE_COUNT);
668
669         /* resize arrays */
670         pack.nodes.clear();
671         pack.is_leaf.clear();
672         pack.is_leaf.resize(node_size);
673
674         /* for top level BVH, first merge existing BVH's so we know the offsets */
675         if(params.top_level)
676                 pack_instances(node_size*BVH_QNODE_SIZE);
677         else
678                 pack.nodes.resize(node_size*BVH_QNODE_SIZE);
679
680         int nextNodeIdx = 0;
681
682         vector<BVHStackEntry> stack;
683         stack.push_back(BVHStackEntry(root, nextNodeIdx++));
684
685         while(stack.size()) {
686                 BVHStackEntry e = stack.back();
687                 stack.pop_back();
688
689                 pack.is_leaf[e.idx] = e.node->is_leaf();
690
691                 if(e.node->is_leaf()) {
692                         /* leaf node */
693                         const LeafNode* leaf = reinterpret_cast<const LeafNode*>(e.node);
694                         pack_leaf(e, leaf);
695                 }
696                 else {
697                         /* inner node */
698                         const BVHNode *node = e.node;
699                         const BVHNode *node0 = node->get_child(0);
700                         const BVHNode *node1 = node->get_child(1);
701
702                         /* collect nodes */
703                         const BVHNode *nodes[4];
704                         int numnodes = 0;
705
706                         if(node0->is_leaf()) {
707                                 nodes[numnodes++] = node0;
708                         }
709                         else {
710                                 nodes[numnodes++] = node0->get_child(0);
711                                 nodes[numnodes++] = node0->get_child(1);
712                         }
713
714                         if(node1->is_leaf()) {
715                                 nodes[numnodes++] = node1;
716                         }
717                         else {
718                                 nodes[numnodes++] = node1->get_child(0);
719                                 nodes[numnodes++] = node1->get_child(1);
720                         }
721
722                         /* push entries on the stack */
723                         for(int i = 0; i < numnodes; i++)
724                                 stack.push_back(BVHStackEntry(nodes[i], nextNodeIdx++));
725
726                         /* set node */
727                         pack_inner(e, &stack[stack.size()-numnodes], numnodes);
728                 }
729         }
730
731         /* root index to start traversal at, to handle case of single leaf node */
732         pack.root_index = (pack.is_leaf[0])? -1: 0;
733 }
734
735 void QBVH::refit_nodes()
736 {
737         assert(0); /* todo */
738 }
739
740 CCL_NAMESPACE_END
741