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