4faf47af7bb5d2d8a800ecc9997ff798bbaafb19
[blender.git] / intern / cycles / bvh / bvh4.cpp
1 /*
2  * Adapted from code copyright 2009-2010 NVIDIA Corporation
3  * Modifications Copyright 2011, Blender Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "bvh/bvh4.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
28 /* Can we avoid this somehow or make more generic?
29  *
30  * Perhaps we can merge nodes in actual tree and make our
31  * life easier all over the place.
32  */
33 static bool node_qbvh_is_unaligned(const BVHNode *node)
34 {
35         const BVHNode *node0 = node->get_child(0),
36                       *node1 = node->get_child(1);
37         bool has_unaligned = false;
38         if(node0->is_leaf()) {
39                 has_unaligned |= node0->is_unaligned;
40         }
41         else {
42                 has_unaligned |= node0->get_child(0)->is_unaligned;
43                 has_unaligned |= node0->get_child(1)->is_unaligned;
44         }
45         if(node1->is_leaf()) {
46                 has_unaligned |= node1->is_unaligned;
47         }
48         else {
49                 has_unaligned |= node1->get_child(0)->is_unaligned;
50                 has_unaligned |= node1->get_child(1)->is_unaligned;
51         }
52         return has_unaligned;
53 }
54
55 BVH4::BVH4(const BVHParams& params_, const vector<Object*>& objects_)
56 : BVH(params_, objects_)
57 {
58         params.bvh_layout = BVH_LAYOUT_BVH4;
59 }
60
61 void BVH4::pack_leaf(const BVHStackEntry& e, const LeafNode *leaf)
62 {
63         float4 data[BVH_QNODE_LEAF_SIZE];
64         memset(data, 0, sizeof(data));
65         if(leaf->num_triangles() == 1 && pack.prim_index[leaf->lo] == -1) {
66                 /* object */
67                 data[0].x = __int_as_float(~(leaf->lo));
68                 data[0].y = __int_as_float(0);
69         }
70         else {
71                 /* triangle */
72                 data[0].x = __int_as_float(leaf->lo);
73                 data[0].y = __int_as_float(leaf->hi);
74         }
75         data[0].z = __uint_as_float(leaf->visibility);
76         if(leaf->num_triangles() != 0) {
77                 data[0].w = __uint_as_float(pack.prim_type[leaf->lo]);
78         }
79
80         memcpy(&pack.leaf_nodes[e.idx], data, sizeof(float4)*BVH_QNODE_LEAF_SIZE);
81 }
82
83 void BVH4::pack_inner(const BVHStackEntry& e,
84                       const BVHStackEntry *en,
85                       int num)
86 {
87         bool has_unaligned = false;
88         /* Check whether we have to create unaligned node or all nodes are aligned
89          * and we can cut some corner here.
90          */
91         if(params.use_unaligned_nodes) {
92                 for(int i = 0; i < num; i++) {
93                         if(en[i].node->is_unaligned) {
94                                 has_unaligned = true;
95                                 break;
96                         }
97                 }
98         }
99         if(has_unaligned) {
100                 /* There's no unaligned children, pack into AABB node. */
101                 pack_unaligned_inner(e, en, num);
102         }
103         else {
104                 /* Create unaligned node with orientation transform for each of the
105                  * children.
106                  */
107                 pack_aligned_inner(e, en, num);
108         }
109 }
110
111 void BVH4::pack_aligned_inner(const BVHStackEntry& e,
112                               const BVHStackEntry *en,
113                               int num)
114 {
115         BoundBox bounds[4];
116         int child[4];
117         for(int i = 0; i < num; ++i) {
118                 bounds[i] = en[i].node->bounds;
119                 child[i] = en[i].encodeIdx();
120         }
121         pack_aligned_node(e.idx,
122                           bounds,
123                           child,
124                           e.node->visibility,
125                           e.node->time_from,
126                           e.node->time_to,
127                           num);
128 }
129
130 void BVH4::pack_aligned_node(int idx,
131                              const BoundBox *bounds,
132                              const int *child,
133                              const uint visibility,
134                              const float time_from,
135                              const float time_to,
136                              const int num)
137 {
138         float4 data[BVH_QNODE_SIZE];
139         memset(data, 0, sizeof(data));
140
141         data[0].x = __uint_as_float(visibility & ~PATH_RAY_NODE_UNALIGNED);
142         data[0].y = time_from;
143         data[0].z = time_to;
144
145         for(int i = 0; i < num; i++) {
146                 float3 bb_min = bounds[i].min;
147                 float3 bb_max = bounds[i].max;
148
149                 data[1][i] = bb_min.x;
150                 data[2][i] = bb_max.x;
151                 data[3][i] = bb_min.y;
152                 data[4][i] = bb_max.y;
153                 data[5][i] = bb_min.z;
154                 data[6][i] = bb_max.z;
155
156                 data[7][i] = __int_as_float(child[i]);
157         }
158
159         for(int i = num; i < 4; i++) {
160                 /* We store BB which would never be recorded as intersection
161                  * so kernel might safely assume there are always 4 child nodes.
162                  */
163                 data[1][i] = FLT_MAX;
164                 data[2][i] = -FLT_MAX;
165
166                 data[3][i] = FLT_MAX;
167                 data[4][i] = -FLT_MAX;
168
169                 data[5][i] = FLT_MAX;
170                 data[6][i] = -FLT_MAX;
171
172                 data[7][i] = __int_as_float(0);
173         }
174
175         memcpy(&pack.nodes[idx], data, sizeof(float4)*BVH_QNODE_SIZE);
176 }
177
178 void BVH4::pack_unaligned_inner(const BVHStackEntry& e,
179                                 const BVHStackEntry *en,
180                                 int num)
181 {
182         Transform aligned_space[4];
183         BoundBox bounds[4];
184         int child[4];
185         for(int i = 0; i < num; ++i) {
186                 aligned_space[i] = en[i].node->get_aligned_space();
187                 bounds[i] = en[i].node->bounds;
188                 child[i] = en[i].encodeIdx();
189         }
190         pack_unaligned_node(e.idx,
191                             aligned_space,
192                             bounds,
193                             child,
194                             e.node->visibility,
195                             e.node->time_from,
196                             e.node->time_to,
197                             num);
198 }
199
200 void BVH4::pack_unaligned_node(int idx,
201                                const Transform *aligned_space,
202                                const BoundBox *bounds,
203                                const int *child,
204                                const uint visibility,
205                                const float time_from,
206                                const float time_to,
207                                const int num)
208 {
209         float4 data[BVH_UNALIGNED_QNODE_SIZE];
210         memset(data, 0, sizeof(data));
211
212         data[0].x = __uint_as_float(visibility | PATH_RAY_NODE_UNALIGNED);
213         data[0].y = time_from;
214         data[0].z = time_to;
215
216         for(int i = 0; i < num; i++) {
217                 Transform space = BVHUnaligned::compute_node_transform(
218                         bounds[i],
219                         aligned_space[i]);
220
221                 data[1][i] = space.x.x;
222                 data[2][i] = space.x.y;
223                 data[3][i] = space.x.z;
224
225                 data[4][i] = space.y.x;
226                 data[5][i] = space.y.y;
227                 data[6][i] = space.y.z;
228
229                 data[7][i] = space.z.x;
230                 data[8][i] = space.z.y;
231                 data[9][i] = space.z.z;
232
233                 data[10][i] = space.x.w;
234                 data[11][i] = space.y.w;
235                 data[12][i] = space.z.w;
236
237                 data[13][i] = __int_as_float(child[i]);
238         }
239
240         for(int i = num; i < 4; i++) {
241                 /* We store BB which would never be recorded as intersection
242                  * so kernel might safely assume there are always 4 child nodes.
243                  */
244
245                 data[1][i] = NAN;
246                 data[2][i] = NAN;
247                 data[3][i] = NAN;
248
249                 data[4][i] = NAN;
250                 data[5][i] = NAN;
251                 data[6][i] = NAN;
252
253                 data[7][i] = NAN;
254                 data[8][i] = NAN;
255                 data[9][i] = NAN;
256
257                 data[10][i] = NAN;
258                 data[11][i] = NAN;
259                 data[12][i] = NAN;
260
261                 data[13][i] = __int_as_float(0);
262         }
263
264         memcpy(&pack.nodes[idx], data, sizeof(float4)*BVH_UNALIGNED_QNODE_SIZE);
265 }
266
267 /* Quad SIMD Nodes */
268
269 void BVH4::pack_nodes(const BVHNode *root)
270 {
271         /* Calculate size of the arrays required. */
272         const size_t num_nodes = root->getSubtreeSize(BVH_STAT_QNODE_COUNT);
273         const size_t num_leaf_nodes = root->getSubtreeSize(BVH_STAT_LEAF_COUNT);
274         assert(num_leaf_nodes <= num_nodes);
275         const size_t num_inner_nodes = num_nodes - num_leaf_nodes;
276         size_t node_size;
277         if(params.use_unaligned_nodes) {
278                 const size_t num_unaligned_nodes =
279                         root->getSubtreeSize(BVH_STAT_UNALIGNED_INNER_QNODE_COUNT);
280                 node_size = (num_unaligned_nodes * BVH_UNALIGNED_QNODE_SIZE) +
281                             (num_inner_nodes - num_unaligned_nodes) * BVH_QNODE_SIZE;
282         }
283         else {
284                 node_size = num_inner_nodes * BVH_QNODE_SIZE;
285         }
286         /* Resize arrays. */
287         pack.nodes.clear();
288         pack.leaf_nodes.clear();
289         /* For top level BVH, first merge existing BVH's so we know the offsets. */
290         if(params.top_level) {
291                 pack_instances(node_size, num_leaf_nodes*BVH_QNODE_LEAF_SIZE);
292         }
293         else {
294                 pack.nodes.resize(node_size);
295                 pack.leaf_nodes.resize(num_leaf_nodes*BVH_QNODE_LEAF_SIZE);
296         }
297
298         int nextNodeIdx = 0, nextLeafNodeIdx = 0;
299
300         vector<BVHStackEntry> stack;
301         stack.reserve(BVHParams::MAX_DEPTH*2);
302         if(root->is_leaf()) {
303                 stack.push_back(BVHStackEntry(root, nextLeafNodeIdx++));
304         }
305         else {
306                 stack.push_back(BVHStackEntry(root, nextNodeIdx));
307                 nextNodeIdx += node_qbvh_is_unaligned(root)
308                                        ? BVH_UNALIGNED_QNODE_SIZE
309                                        : BVH_QNODE_SIZE;
310         }
311
312         while(stack.size()) {
313                 BVHStackEntry e = stack.back();
314                 stack.pop_back();
315
316                 if(e.node->is_leaf()) {
317                         /* leaf node */
318                         const LeafNode *leaf = reinterpret_cast<const LeafNode*>(e.node);
319                         pack_leaf(e, leaf);
320                 }
321                 else {
322                         /* Inner node. */
323                         const BVHNode *node = e.node;
324                         const BVHNode *node0 = node->get_child(0);
325                         const BVHNode *node1 = node->get_child(1);
326                         /* Collect nodes. */
327                         const BVHNode *nodes[4];
328                         int numnodes = 0;
329                         if(node0->is_leaf()) {
330                                 nodes[numnodes++] = node0;
331                         }
332                         else {
333                                 nodes[numnodes++] = node0->get_child(0);
334                                 nodes[numnodes++] = node0->get_child(1);
335                         }
336                         if(node1->is_leaf()) {
337                                 nodes[numnodes++] = node1;
338                         }
339                         else {
340                                 nodes[numnodes++] = node1->get_child(0);
341                                 nodes[numnodes++] = node1->get_child(1);
342                         }
343                         /* Push entries on the stack. */
344                         for(int i = 0; i < numnodes; ++i) {
345                                 int idx;
346                                 if(nodes[i]->is_leaf()) {
347                                         idx = nextLeafNodeIdx++;
348                                 }
349                                 else {
350                                         idx = nextNodeIdx;
351                                         nextNodeIdx += node_qbvh_is_unaligned(nodes[i])
352                                                                ? BVH_UNALIGNED_QNODE_SIZE
353                                                                : BVH_QNODE_SIZE;
354                                 }
355                                 stack.push_back(BVHStackEntry(nodes[i], idx));
356                         }
357                         /* Set node. */
358                         pack_inner(e, &stack[stack.size()-numnodes], numnodes);
359                 }
360         }
361         assert(node_size == nextNodeIdx);
362         /* Root index to start traversal at, to handle case of single leaf node. */
363         pack.root_index = (root->is_leaf())? -1: 0;
364 }
365
366 void BVH4::refit_nodes()
367 {
368         assert(!params.top_level);
369
370         BoundBox bbox = BoundBox::empty;
371         uint visibility = 0;
372         refit_node(0, (pack.root_index == -1)? true: false, bbox, visibility);
373 }
374
375 void BVH4::refit_node(int idx, bool leaf, BoundBox& bbox, uint& visibility)
376 {
377         if(leaf) {
378                 /* Refit leaf node. */
379                 int4 *data = &pack.leaf_nodes[idx];
380                 int4 c = data[0];
381
382                 BVH::refit_primitives(c.x, c.y, bbox, visibility);
383
384                 /* TODO(sergey): This is actually a copy of pack_leaf(),
385                  * but this chunk of code only knows actual data and has
386                  * no idea about BVHNode.
387                  *
388                  * Would be nice to de-duplicate code, but trying to make
389                  * making code more general ends up in much nastier code
390                  * in my opinion so far.
391                  *
392                  * Same applies to the inner nodes case below.
393                  */
394                 float4 leaf_data[BVH_QNODE_LEAF_SIZE];
395                 leaf_data[0].x = __int_as_float(c.x);
396                 leaf_data[0].y = __int_as_float(c.y);
397                 leaf_data[0].z = __uint_as_float(visibility);
398                 leaf_data[0].w = __uint_as_float(c.w);
399                 memcpy(&pack.leaf_nodes[idx], leaf_data, sizeof(float4)*BVH_QNODE_LEAF_SIZE);
400         }
401         else {
402                 int4 *data = &pack.nodes[idx];
403                 bool is_unaligned = (data[0].x & PATH_RAY_NODE_UNALIGNED) != 0;
404                 int4 c;
405                 if(is_unaligned) {
406                         c = data[13];
407                 }
408                 else {
409                         c = data[7];
410                 }
411                 /* Refit inner node, set bbox from children. */
412                 BoundBox child_bbox[4] = {BoundBox::empty,
413                                           BoundBox::empty,
414                                           BoundBox::empty,
415                                           BoundBox::empty};
416                 uint child_visibility[4] = {0};
417                 int num_nodes = 0;
418
419                 for(int i = 0; i < 4; ++i) {
420                         if(c[i] != 0) {
421                                 refit_node((c[i] < 0)? -c[i]-1: c[i], (c[i] < 0),
422                                            child_bbox[i], child_visibility[i]);
423                                 ++num_nodes;
424                                 bbox.grow(child_bbox[i]);
425                                 visibility |= child_visibility[i];
426                         }
427                 }
428
429                 if(is_unaligned) {
430                         Transform aligned_space[4] = {transform_identity(),
431                                                       transform_identity(),
432                                                       transform_identity(),
433                                                       transform_identity()};
434                         pack_unaligned_node(idx,
435                                             aligned_space,
436                                             child_bbox,
437                                             &c[0],
438                                             visibility,
439                                             0.0f,
440                                             1.0f,
441                                             4);
442                 }
443                 else {
444                         pack_aligned_node(idx,
445                                           child_bbox,
446                                           &c[0],
447                                           visibility,
448                                           0.0f,
449                                           1.0f,
450                                           4);
451                 }
452         }
453 }
454
455 CCL_NAMESPACE_END