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/bvh2.h"
19 
20 #include "render/mesh.h"
21 #include "render/object.h"
22 
23 #include "bvh/bvh_node.h"
24 #include "bvh/bvh_unaligned.h"
25 
26 CCL_NAMESPACE_BEGIN
27 
BVH2(const BVHParams & params_,const vector<Geometry * > & geometry_,const vector<Object * > & objects_)28 BVH2::BVH2(const BVHParams &params_,
29            const vector<Geometry *> &geometry_,
30            const vector<Object *> &objects_)
31     : BVH(params_, geometry_, objects_)
32 {
33 }
34 
widen_children_nodes(const BVHNode * root)35 BVHNode *BVH2::widen_children_nodes(const BVHNode *root)
36 {
37   return const_cast<BVHNode *>(root);
38 }
39 
pack_leaf(const BVHStackEntry & e,const LeafNode * leaf)40 void BVH2::pack_leaf(const BVHStackEntry &e, const LeafNode *leaf)
41 {
42   assert(e.idx + BVH_NODE_LEAF_SIZE <= pack.leaf_nodes.size());
43   float4 data[BVH_NODE_LEAF_SIZE];
44   memset(data, 0, sizeof(data));
45   if (leaf->num_triangles() == 1 && pack.prim_index[leaf->lo] == -1) {
46     /* object */
47     data[0].x = __int_as_float(~(leaf->lo));
48     data[0].y = __int_as_float(0);
49   }
50   else {
51     /* triangle */
52     data[0].x = __int_as_float(leaf->lo);
53     data[0].y = __int_as_float(leaf->hi);
54   }
55   data[0].z = __uint_as_float(leaf->visibility);
56   if (leaf->num_triangles() != 0) {
57     data[0].w = __uint_as_float(pack.prim_type[leaf->lo]);
58   }
59 
60   memcpy(&pack.leaf_nodes[e.idx], data, sizeof(float4) * BVH_NODE_LEAF_SIZE);
61 }
62 
pack_inner(const BVHStackEntry & e,const BVHStackEntry & e0,const BVHStackEntry & e1)63 void BVH2::pack_inner(const BVHStackEntry &e, const BVHStackEntry &e0, const BVHStackEntry &e1)
64 {
65   if (e0.node->is_unaligned || e1.node->is_unaligned) {
66     pack_unaligned_inner(e, e0, e1);
67   }
68   else {
69     pack_aligned_inner(e, e0, e1);
70   }
71 }
72 
pack_aligned_inner(const BVHStackEntry & e,const BVHStackEntry & e0,const BVHStackEntry & e1)73 void BVH2::pack_aligned_inner(const BVHStackEntry &e,
74                               const BVHStackEntry &e0,
75                               const BVHStackEntry &e1)
76 {
77   pack_aligned_node(e.idx,
78                     e0.node->bounds,
79                     e1.node->bounds,
80                     e0.encodeIdx(),
81                     e1.encodeIdx(),
82                     e0.node->visibility,
83                     e1.node->visibility);
84 }
85 
pack_aligned_node(int idx,const BoundBox & b0,const BoundBox & b1,int c0,int c1,uint visibility0,uint visibility1)86 void BVH2::pack_aligned_node(int idx,
87                              const BoundBox &b0,
88                              const BoundBox &b1,
89                              int c0,
90                              int c1,
91                              uint visibility0,
92                              uint visibility1)
93 {
94   assert(idx + BVH_NODE_SIZE <= pack.nodes.size());
95   assert(c0 < 0 || c0 < pack.nodes.size());
96   assert(c1 < 0 || c1 < pack.nodes.size());
97 
98   int4 data[BVH_NODE_SIZE] = {
99       make_int4(
100           visibility0 & ~PATH_RAY_NODE_UNALIGNED, visibility1 & ~PATH_RAY_NODE_UNALIGNED, c0, c1),
101       make_int4(__float_as_int(b0.min.x),
102                 __float_as_int(b1.min.x),
103                 __float_as_int(b0.max.x),
104                 __float_as_int(b1.max.x)),
105       make_int4(__float_as_int(b0.min.y),
106                 __float_as_int(b1.min.y),
107                 __float_as_int(b0.max.y),
108                 __float_as_int(b1.max.y)),
109       make_int4(__float_as_int(b0.min.z),
110                 __float_as_int(b1.min.z),
111                 __float_as_int(b0.max.z),
112                 __float_as_int(b1.max.z)),
113   };
114 
115   memcpy(&pack.nodes[idx], data, sizeof(int4) * BVH_NODE_SIZE);
116 }
117 
pack_unaligned_inner(const BVHStackEntry & e,const BVHStackEntry & e0,const BVHStackEntry & e1)118 void BVH2::pack_unaligned_inner(const BVHStackEntry &e,
119                                 const BVHStackEntry &e0,
120                                 const BVHStackEntry &e1)
121 {
122   pack_unaligned_node(e.idx,
123                       e0.node->get_aligned_space(),
124                       e1.node->get_aligned_space(),
125                       e0.node->bounds,
126                       e1.node->bounds,
127                       e0.encodeIdx(),
128                       e1.encodeIdx(),
129                       e0.node->visibility,
130                       e1.node->visibility);
131 }
132 
pack_unaligned_node(int idx,const Transform & aligned_space0,const Transform & aligned_space1,const BoundBox & bounds0,const BoundBox & bounds1,int c0,int c1,uint visibility0,uint visibility1)133 void BVH2::pack_unaligned_node(int idx,
134                                const Transform &aligned_space0,
135                                const Transform &aligned_space1,
136                                const BoundBox &bounds0,
137                                const BoundBox &bounds1,
138                                int c0,
139                                int c1,
140                                uint visibility0,
141                                uint visibility1)
142 {
143   assert(idx + BVH_UNALIGNED_NODE_SIZE <= pack.nodes.size());
144   assert(c0 < 0 || c0 < pack.nodes.size());
145   assert(c1 < 0 || c1 < pack.nodes.size());
146 
147   float4 data[BVH_UNALIGNED_NODE_SIZE];
148   Transform space0 = BVHUnaligned::compute_node_transform(bounds0, aligned_space0);
149   Transform space1 = BVHUnaligned::compute_node_transform(bounds1, aligned_space1);
150   data[0] = make_float4(__int_as_float(visibility0 | PATH_RAY_NODE_UNALIGNED),
151                         __int_as_float(visibility1 | PATH_RAY_NODE_UNALIGNED),
152                         __int_as_float(c0),
153                         __int_as_float(c1));
154 
155   data[1] = space0.x;
156   data[2] = space0.y;
157   data[3] = space0.z;
158   data[4] = space1.x;
159   data[5] = space1.y;
160   data[6] = space1.z;
161 
162   memcpy(&pack.nodes[idx], data, sizeof(float4) * BVH_UNALIGNED_NODE_SIZE);
163 }
164 
pack_nodes(const BVHNode * root)165 void BVH2::pack_nodes(const BVHNode *root)
166 {
167   const size_t num_nodes = root->getSubtreeSize(BVH_STAT_NODE_COUNT);
168   const size_t num_leaf_nodes = root->getSubtreeSize(BVH_STAT_LEAF_COUNT);
169   assert(num_leaf_nodes <= num_nodes);
170   const size_t num_inner_nodes = num_nodes - num_leaf_nodes;
171   size_t node_size;
172   if (params.use_unaligned_nodes) {
173     const size_t num_unaligned_nodes = root->getSubtreeSize(BVH_STAT_UNALIGNED_INNER_COUNT);
174     node_size = (num_unaligned_nodes * BVH_UNALIGNED_NODE_SIZE) +
175                 (num_inner_nodes - num_unaligned_nodes) * BVH_NODE_SIZE;
176   }
177   else {
178     node_size = num_inner_nodes * BVH_NODE_SIZE;
179   }
180   /* Resize arrays */
181   pack.nodes.clear();
182   pack.leaf_nodes.clear();
183   /* For top level BVH, first merge existing BVH's so we know the offsets. */
184   if (params.top_level) {
185     pack_instances(node_size, num_leaf_nodes * BVH_NODE_LEAF_SIZE);
186   }
187   else {
188     pack.nodes.resize(node_size);
189     pack.leaf_nodes.resize(num_leaf_nodes * BVH_NODE_LEAF_SIZE);
190   }
191 
192   int nextNodeIdx = 0, nextLeafNodeIdx = 0;
193 
194   vector<BVHStackEntry> stack;
195   stack.reserve(BVHParams::MAX_DEPTH * 2);
196   if (root->is_leaf()) {
197     stack.push_back(BVHStackEntry(root, nextLeafNodeIdx++));
198   }
199   else {
200     stack.push_back(BVHStackEntry(root, nextNodeIdx));
201     nextNodeIdx += root->has_unaligned() ? BVH_UNALIGNED_NODE_SIZE : BVH_NODE_SIZE;
202   }
203 
204   while (stack.size()) {
205     BVHStackEntry e = stack.back();
206     stack.pop_back();
207 
208     if (e.node->is_leaf()) {
209       /* leaf node */
210       const LeafNode *leaf = reinterpret_cast<const LeafNode *>(e.node);
211       pack_leaf(e, leaf);
212     }
213     else {
214       /* inner node */
215       int idx[2];
216       for (int i = 0; i < 2; ++i) {
217         if (e.node->get_child(i)->is_leaf()) {
218           idx[i] = nextLeafNodeIdx++;
219         }
220         else {
221           idx[i] = nextNodeIdx;
222           nextNodeIdx += e.node->get_child(i)->has_unaligned() ? BVH_UNALIGNED_NODE_SIZE :
223                                                                  BVH_NODE_SIZE;
224         }
225       }
226 
227       stack.push_back(BVHStackEntry(e.node->get_child(0), idx[0]));
228       stack.push_back(BVHStackEntry(e.node->get_child(1), idx[1]));
229 
230       pack_inner(e, stack[stack.size() - 2], stack[stack.size() - 1]);
231     }
232   }
233   assert(node_size == nextNodeIdx);
234   /* root index to start traversal at, to handle case of single leaf node */
235   pack.root_index = (root->is_leaf()) ? -1 : 0;
236 }
237 
refit_nodes()238 void BVH2::refit_nodes()
239 {
240   assert(!params.top_level);
241 
242   BoundBox bbox = BoundBox::empty;
243   uint visibility = 0;
244   refit_node(0, (pack.root_index == -1) ? true : false, bbox, visibility);
245 }
246 
refit_node(int idx,bool leaf,BoundBox & bbox,uint & visibility)247 void BVH2::refit_node(int idx, bool leaf, BoundBox &bbox, uint &visibility)
248 {
249   if (leaf) {
250     /* refit leaf node */
251     assert(idx + BVH_NODE_LEAF_SIZE <= pack.leaf_nodes.size());
252     const int4 *data = &pack.leaf_nodes[idx];
253     const int c0 = data[0].x;
254     const int c1 = data[0].y;
255 
256     BVH::refit_primitives(c0, c1, bbox, visibility);
257 
258     /* TODO(sergey): De-duplicate with pack_leaf(). */
259     float4 leaf_data[BVH_NODE_LEAF_SIZE];
260     leaf_data[0].x = __int_as_float(c0);
261     leaf_data[0].y = __int_as_float(c1);
262     leaf_data[0].z = __uint_as_float(visibility);
263     leaf_data[0].w = __uint_as_float(data[0].w);
264     memcpy(&pack.leaf_nodes[idx], leaf_data, sizeof(float4) * BVH_NODE_LEAF_SIZE);
265   }
266   else {
267     assert(idx + BVH_NODE_SIZE <= pack.nodes.size());
268 
269     const int4 *data = &pack.nodes[idx];
270     const bool is_unaligned = (data[0].x & PATH_RAY_NODE_UNALIGNED) != 0;
271     const int c0 = data[0].z;
272     const int c1 = data[0].w;
273     /* refit inner node, set bbox from children */
274     BoundBox bbox0 = BoundBox::empty, bbox1 = BoundBox::empty;
275     uint visibility0 = 0, visibility1 = 0;
276 
277     refit_node((c0 < 0) ? -c0 - 1 : c0, (c0 < 0), bbox0, visibility0);
278     refit_node((c1 < 0) ? -c1 - 1 : c1, (c1 < 0), bbox1, visibility1);
279 
280     if (is_unaligned) {
281       Transform aligned_space = transform_identity();
282       pack_unaligned_node(
283           idx, aligned_space, aligned_space, bbox0, bbox1, c0, c1, visibility0, visibility1);
284     }
285     else {
286       pack_aligned_node(idx, bbox0, bbox1, c0, c1, visibility0, visibility1);
287     }
288 
289     bbox.grow(bbox0);
290     bbox.grow(bbox1);
291     visibility = visibility0 | visibility1;
292   }
293 }
294 
295 CCL_NAMESPACE_END
296