2 * Original code Copyright 2017, Intel Corporation
3 * Modifications Copyright 2018, Blender Foundation.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Intel Corporation nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "render/mesh.h"
32 #include "render/object.h"
34 #include "bvh/bvh_node.h"
35 #include "bvh/bvh_unaligned.h"
39 BVH8::BVH8(const BVHParams& params_, const vector<Object*>& objects_)
40 : BVH(params_, objects_)
44 void BVH8::pack_leaf(const BVHStackEntry& e, const LeafNode *leaf)
46 float4 data[BVH_ONODE_LEAF_SIZE];
47 memset(data, 0, sizeof(data));
48 if(leaf->num_triangles() == 1 && pack.prim_index[leaf->lo] == -1) {
50 data[0].x = __int_as_float(~(leaf->lo));
51 data[0].y = __int_as_float(0);
55 data[0].x = __int_as_float(leaf->lo);
56 data[0].y = __int_as_float(leaf->hi);
58 data[0].z = __uint_as_float(leaf->visibility);
59 if(leaf->num_triangles() != 0) {
60 data[0].w = __uint_as_float(pack.prim_type[leaf->lo]);
63 memcpy(&pack.leaf_nodes[e.idx], data, sizeof(float4)*BVH_ONODE_LEAF_SIZE);
66 void BVH8::pack_inner(const BVHStackEntry& e,
67 const BVHStackEntry *en,
70 bool has_unaligned = false;
71 /* Check whether we have to create unaligned node or all nodes are aligned
72 * and we can cut some corner here.
74 if(params.use_unaligned_nodes) {
75 for(int i = 0; i < num; i++) {
76 if(en[i].node->is_unaligned) {
83 /* There's no unaligned children, pack into AABB node. */
84 pack_unaligned_inner(e, en, num);
87 /* Create unaligned node with orientation transform for each of the
90 pack_aligned_inner(e, en, num);
94 void BVH8::pack_aligned_inner(const BVHStackEntry& e,
95 const BVHStackEntry *en,
100 for(int i = 0; i < num; ++i) {
101 bounds[i] = en[i].node->bounds;
102 child[i] = en[i].encodeIdx();
104 pack_aligned_node(e.idx,
113 void BVH8::pack_aligned_node(int idx,
114 const BoundBox *bounds,
116 const uint visibility,
117 const float time_from,
122 memset(data, 0, sizeof(data));
124 data[0].a = __uint_as_float(visibility & ~PATH_RAY_NODE_UNALIGNED);
125 data[0].b = time_from;
127 for(int i = 0; i < num; i++) {
128 float3 bb_min = bounds[i].min;
129 float3 bb_max = bounds[i].max;
131 data[1][i] = bb_min.x;
132 data[2][i] = bb_max.x;
133 data[3][i] = bb_min.y;
134 data[4][i] = bb_max.y;
135 data[5][i] = bb_min.z;
136 data[6][i] = bb_max.z;
138 data[7][i] = __int_as_float(child[i]);
141 for(int i = num; i < 8; i++) {
142 /* We store BB which would never be recorded as intersection
143 * so kernel might safely assume there are always 4 child nodes.
145 data[1][i] = FLT_MAX;
146 data[2][i] = -FLT_MAX;
148 data[3][i] = FLT_MAX;
149 data[4][i] = -FLT_MAX;
151 data[5][i] = FLT_MAX;
152 data[6][i] = -FLT_MAX;
154 data[7][i] = __int_as_float(0);
156 memcpy(&pack.nodes[idx], data, sizeof(float4)*BVH_ONODE_SIZE);
159 void BVH8::pack_unaligned_inner(const BVHStackEntry& e,
160 const BVHStackEntry *en,
163 Transform aligned_space[8];
166 for(int i = 0; i < num; ++i) {
167 aligned_space[i] = en[i].node->get_aligned_space();
168 bounds[i] = en[i].node->bounds;
169 child[i] = en[i].encodeIdx();
171 pack_unaligned_node(e.idx,
181 void BVH8::pack_unaligned_node(int idx,
182 const Transform *aligned_space,
183 const BoundBox *bounds,
185 const uint visibility,
186 const float time_from,
190 float8 data[BVH_UNALIGNED_ONODE_SIZE];
191 memset(data, 0, sizeof(data));
192 data[0].a = __uint_as_float(visibility | PATH_RAY_NODE_UNALIGNED);
193 data[0].b = time_from;
196 for(int i = 0; i < num; i++) {
197 Transform space = BVHUnaligned::compute_node_transform(
201 data[1][i] = space.x.x;
202 data[2][i] = space.x.y;
203 data[3][i] = space.x.z;
205 data[4][i] = space.y.x;
206 data[5][i] = space.y.y;
207 data[6][i] = space.y.z;
209 data[7][i] = space.z.x;
210 data[8][i] = space.z.y;
211 data[9][i] = space.z.z;
213 data[10][i] = space.x.w;
214 data[11][i] = space.y.w;
215 data[12][i] = space.z.w;
217 data[13][i] = __int_as_float(child[i]);
220 for(int i = num; i < 8; i++) {
221 /* We store BB which would never be recorded as intersection
222 * so kernel might safely assume there are always 4 child nodes.
241 data[13][i] = __int_as_float(0);
244 memcpy(&pack.nodes[idx], data, sizeof(float4)*BVH_UNALIGNED_ONODE_SIZE);
247 /* Quad SIMD Nodes */
249 void BVH8::pack_nodes(const BVHNode *root)
251 /* Calculate size of the arrays required. */
252 const size_t num_nodes = root->getSubtreeSize(BVH_STAT_ONODE_COUNT);
253 const size_t num_leaf_nodes = root->getSubtreeSize(BVH_STAT_LEAF_COUNT);
254 assert(num_leaf_nodes <= num_nodes);
255 const size_t num_inner_nodes = num_nodes - num_leaf_nodes;
257 if(params.use_unaligned_nodes) {
258 const size_t num_unaligned_nodes =
259 root->getSubtreeSize(BVH_STAT_UNALIGNED_INNER_ONODE_COUNT);
260 node_size = (num_unaligned_nodes * BVH_UNALIGNED_ONODE_SIZE) +
261 (num_inner_nodes - num_unaligned_nodes) * BVH_ONODE_SIZE;
264 node_size = num_inner_nodes * BVH_ONODE_SIZE;
268 pack.leaf_nodes.clear();
269 /* For top level BVH, first merge existing BVH's so we know the offsets. */
270 if(params.top_level) {
271 pack_instances(node_size, num_leaf_nodes*BVH_ONODE_LEAF_SIZE);
274 pack.nodes.resize(node_size);
275 pack.leaf_nodes.resize(num_leaf_nodes*BVH_ONODE_LEAF_SIZE);
278 int nextNodeIdx = 0, nextLeafNodeIdx = 0;
280 vector<BVHStackEntry> stack;
281 stack.reserve(BVHParams::MAX_DEPTH*2);
282 if(root->is_leaf()) {
283 stack.push_back(BVHStackEntry(root, nextLeafNodeIdx++));
286 stack.push_back(BVHStackEntry(root, nextNodeIdx));
287 nextNodeIdx += node_is_unaligned(root, bvh8)
288 ? BVH_UNALIGNED_ONODE_SIZE
292 while(stack.size()) {
293 BVHStackEntry e = stack.back();
296 if(e.node->is_leaf()) {
298 const LeafNode *leaf = reinterpret_cast<const LeafNode*>(e.node);
303 const BVHNode *node = e.node;
304 const BVHNode *node0 = node->get_child(0);
305 const BVHNode *node1 = node->get_child(1);
307 const BVHNode *nodes[8];
309 if(node0->is_leaf()) {
310 nodes[numnodes++] = node0;
313 const BVHNode *node00 = node0->get_child(0),
314 *node01 = node0->get_child(1);
315 if(node00->is_leaf()) {
316 nodes[numnodes++] = node00;
319 nodes[numnodes++] = node00->get_child(0);
320 nodes[numnodes++] = node00->get_child(1);
322 if(node01->is_leaf()) {
323 nodes[numnodes++] = node01;
326 nodes[numnodes++] = node01->get_child(0);
327 nodes[numnodes++] = node01->get_child(1);
330 if(node1->is_leaf()) {
331 nodes[numnodes++] = node1;
334 const BVHNode *node10 = node1->get_child(0),
335 *node11 = node1->get_child(1);
336 if(node10->is_leaf()) {
337 nodes[numnodes++] = node10;
340 nodes[numnodes++] = node10->get_child(0);
341 nodes[numnodes++] = node10->get_child(1);
343 if(node11->is_leaf()) {
344 nodes[numnodes++] = node11;
347 nodes[numnodes++] = node11->get_child(0);
348 nodes[numnodes++] = node11->get_child(1);
351 /* Push entries on the stack. */
352 for(int i = 0; i < numnodes; ++i) {
354 if(nodes[i]->is_leaf()) {
355 idx = nextLeafNodeIdx++;
359 nextNodeIdx += node_is_unaligned(nodes[i], bvh8)
360 ? BVH_UNALIGNED_ONODE_SIZE
363 stack.push_back(BVHStackEntry(nodes[i], idx));
366 pack_inner(e, &stack[stack.size() - numnodes], numnodes);
369 assert(node_size == nextNodeIdx);
370 /* Root index to start traversal at, to handle case of single leaf node. */
371 pack.root_index = (root->is_leaf()) ? -1 : 0;
374 void BVH8::refit_nodes()
376 assert(!params.top_level);
378 BoundBox bbox = BoundBox::empty;
380 refit_node(0, (pack.root_index == -1)? true: false, bbox, visibility);
383 void BVH8::refit_node(int idx, bool leaf, BoundBox& bbox, uint& visibility)
386 int4 *data = &pack.leaf_nodes[idx];
388 /* Refit leaf node. */
389 for(int prim = c.x; prim < c.y; prim++) {
390 int pidx = pack.prim_index[prim];
391 int tob = pack.prim_object[prim];
392 Object *ob = objects[tob];
395 /* Object instance. */
396 bbox.grow(ob->bounds);
400 const Mesh *mesh = ob->mesh;
402 if(pack.prim_type[prim] & PRIMITIVE_ALL_CURVE) {
404 int str_offset = (params.top_level) ? mesh->curve_offset : 0;
405 Mesh::Curve curve = mesh->get_curve(pidx - str_offset);
406 int k = PRIMITIVE_UNPACK_SEGMENT(pack.prim_type[prim]);
408 curve.bounds_grow(k, &mesh->curve_keys[0], &mesh->curve_radius[0], bbox);
410 visibility |= PATH_RAY_CURVE;
413 if(mesh->use_motion_blur) {
414 Attribute *attr = mesh->curve_attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
417 size_t mesh_size = mesh->curve_keys.size();
418 size_t steps = mesh->motion_steps - 1;
419 float3 *key_steps = attr->data_float3();
421 for(size_t i = 0; i < steps; i++) {
422 curve.bounds_grow(k, key_steps + i*mesh_size, &mesh->curve_radius[0], bbox);
429 int tri_offset = (params.top_level) ? mesh->tri_offset : 0;
430 Mesh::Triangle triangle = mesh->get_triangle(pidx - tri_offset);
431 const float3 *vpos = &mesh->verts[0];
433 triangle.bounds_grow(vpos, bbox);
435 /* Motion triangles. */
436 if(mesh->use_motion_blur) {
437 Attribute *attr = mesh->attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
440 size_t mesh_size = mesh->verts.size();
441 size_t steps = mesh->motion_steps - 1;
442 float3 *vert_steps = attr->data_float3();
444 for(size_t i = 0; i < steps; i++) {
445 triangle.bounds_grow(vert_steps + i*mesh_size, bbox);
452 visibility |= ob->visibility;
455 float4 leaf_data[BVH_ONODE_LEAF_SIZE];
456 leaf_data[0].x = __int_as_float(c.x);
457 leaf_data[0].y = __int_as_float(c.y);
458 leaf_data[0].z = __uint_as_float(visibility);
459 leaf_data[0].w = __uint_as_float(c.w);
460 memcpy(&pack.leaf_nodes[idx], leaf_data, sizeof(float4)*BVH_ONODE_LEAF_SIZE);
463 float8 *data = (float8*)&pack.nodes[idx];
464 bool is_unaligned = (__float_as_uint(data[0].a) & PATH_RAY_NODE_UNALIGNED) != 0;
465 /* Refit inner node, set bbox from children. */
466 BoundBox child_bbox[8] = { BoundBox::empty, BoundBox::empty,
467 BoundBox::empty, BoundBox::empty,
468 BoundBox::empty, BoundBox::empty,
469 BoundBox::empty, BoundBox::empty };
471 uint child_visibility[8] = { 0 };
474 for(int i = 0; i < 8; ++i) {
475 child[i] = __float_as_int(data[(is_unaligned) ? 13: 7][i]);
478 refit_node((child[i] < 0)? -child[i]-1: child[i], (child[i] < 0),
479 child_bbox[i], child_visibility[i]);
481 bbox.grow(child_bbox[i]);
482 visibility |= child_visibility[i];
487 Transform aligned_space[8] = { transform_identity(), transform_identity(),
488 transform_identity(), transform_identity(),
489 transform_identity(), transform_identity(),
490 transform_identity(), transform_identity()};
491 pack_unaligned_node(idx,
501 pack_aligned_node(idx,