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;
128 for(int i = 0; i < num; i++) {
129 float3 bb_min = bounds[i].min;
130 float3 bb_max = bounds[i].max;
132 data[1][i] = bb_min.x;
133 data[2][i] = bb_max.x;
134 data[3][i] = bb_min.y;
135 data[4][i] = bb_max.y;
136 data[5][i] = bb_min.z;
137 data[6][i] = bb_max.z;
139 data[7][i] = __int_as_float(child[i]);
142 for(int i = num; i < 8; i++) {
143 /* We store BB which would never be recorded as intersection
144 * so kernel might safely assume there are always 4 child nodes.
146 data[1][i] = FLT_MAX;
147 data[2][i] = -FLT_MAX;
149 data[3][i] = FLT_MAX;
150 data[4][i] = -FLT_MAX;
152 data[5][i] = FLT_MAX;
153 data[6][i] = -FLT_MAX;
155 data[7][i] = __int_as_float(0);
158 memcpy(&pack.nodes[idx], data, sizeof(float4)*BVH_ONODE_SIZE);
161 void BVH8::pack_unaligned_inner(const BVHStackEntry& e,
162 const BVHStackEntry *en,
165 Transform aligned_space[8];
168 for(int i = 0; i < num; ++i) {
169 aligned_space[i] = en[i].node->get_aligned_space();
170 bounds[i] = en[i].node->bounds;
171 child[i] = en[i].encodeIdx();
173 pack_unaligned_node(e.idx,
183 void BVH8::pack_unaligned_node(int idx,
184 const Transform *aligned_space,
185 const BoundBox *bounds,
187 const uint visibility,
188 const float time_from,
192 float8 data[BVH_UNALIGNED_ONODE_SIZE];
193 memset(data, 0, sizeof(data));
195 data[0].a = __uint_as_float(visibility | PATH_RAY_NODE_UNALIGNED);
196 data[0].b = time_from;
199 for(int i = 0; i < num; i++) {
200 Transform space = BVHUnaligned::compute_node_transform(
204 data[1][i] = space.x.x;
205 data[2][i] = space.x.y;
206 data[3][i] = space.x.z;
208 data[4][i] = space.y.x;
209 data[5][i] = space.y.y;
210 data[6][i] = space.y.z;
212 data[7][i] = space.z.x;
213 data[8][i] = space.z.y;
214 data[9][i] = space.z.z;
216 data[10][i] = space.x.w;
217 data[11][i] = space.y.w;
218 data[12][i] = space.z.w;
220 data[13][i] = __int_as_float(child[i]);
223 for(int i = num; i < 8; i++) {
224 /* We store BB which would never be recorded as intersection
225 * so kernel might safely assume there are always 4 child nodes.
244 data[13][i] = __int_as_float(0);
247 memcpy(&pack.nodes[idx], data, sizeof(float4)*BVH_UNALIGNED_ONODE_SIZE);
250 /* Quad SIMD Nodes */
252 void BVH8::pack_nodes(const BVHNode *root)
254 /* Calculate size of the arrays required. */
255 const size_t num_nodes = root->getSubtreeSize(BVH_STAT_ONODE_COUNT);
256 const size_t num_leaf_nodes = root->getSubtreeSize(BVH_STAT_LEAF_COUNT);
257 assert(num_leaf_nodes <= num_nodes);
258 const size_t num_inner_nodes = num_nodes - num_leaf_nodes;
260 if(params.use_unaligned_nodes) {
261 const size_t num_unaligned_nodes =
262 root->getSubtreeSize(BVH_STAT_UNALIGNED_INNER_ONODE_COUNT);
263 node_size = (num_unaligned_nodes * BVH_UNALIGNED_ONODE_SIZE) +
264 (num_inner_nodes - num_unaligned_nodes) * BVH_ONODE_SIZE;
267 node_size = num_inner_nodes * BVH_ONODE_SIZE;
271 pack.leaf_nodes.clear();
272 /* For top level BVH, first merge existing BVH's so we know the offsets. */
273 if(params.top_level) {
274 pack_instances(node_size, num_leaf_nodes*BVH_ONODE_LEAF_SIZE);
277 pack.nodes.resize(node_size);
278 pack.leaf_nodes.resize(num_leaf_nodes*BVH_ONODE_LEAF_SIZE);
281 int nextNodeIdx = 0, nextLeafNodeIdx = 0;
283 vector<BVHStackEntry> stack;
284 stack.reserve(BVHParams::MAX_DEPTH*2);
285 if(root->is_leaf()) {
286 stack.push_back(BVHStackEntry(root, nextLeafNodeIdx++));
289 stack.push_back(BVHStackEntry(root, nextNodeIdx));
290 nextNodeIdx += node_is_unaligned(root, bvh8)
291 ? BVH_UNALIGNED_ONODE_SIZE
295 while(stack.size()) {
296 BVHStackEntry e = stack.back();
299 if(e.node->is_leaf()) {
301 const LeafNode *leaf = reinterpret_cast<const LeafNode*>(e.node);
306 const BVHNode *node = e.node;
307 const BVHNode *node0 = node->get_child(0);
308 const BVHNode *node1 = node->get_child(1);
310 const BVHNode *nodes[8];
312 if(node0->is_leaf()) {
313 nodes[numnodes++] = node0;
316 const BVHNode *node00 = node0->get_child(0),
317 *node01 = node0->get_child(1);
318 if(node00->is_leaf()) {
319 nodes[numnodes++] = node00;
322 nodes[numnodes++] = node00->get_child(0);
323 nodes[numnodes++] = node00->get_child(1);
325 if(node01->is_leaf()) {
326 nodes[numnodes++] = node01;
329 nodes[numnodes++] = node01->get_child(0);
330 nodes[numnodes++] = node01->get_child(1);
333 if(node1->is_leaf()) {
334 nodes[numnodes++] = node1;
337 const BVHNode *node10 = node1->get_child(0),
338 *node11 = node1->get_child(1);
339 if(node10->is_leaf()) {
340 nodes[numnodes++] = node10;
343 nodes[numnodes++] = node10->get_child(0);
344 nodes[numnodes++] = node10->get_child(1);
346 if(node11->is_leaf()) {
347 nodes[numnodes++] = node11;
350 nodes[numnodes++] = node11->get_child(0);
351 nodes[numnodes++] = node11->get_child(1);
354 /* Push entries on the stack. */
355 for(int i = 0; i < numnodes; ++i) {
357 if(nodes[i]->is_leaf()) {
358 idx = nextLeafNodeIdx++;
362 nextNodeIdx += node_is_unaligned(nodes[i], bvh8)
363 ? BVH_UNALIGNED_ONODE_SIZE
366 stack.push_back(BVHStackEntry(nodes[i], idx));
369 pack_inner(e, &stack[stack.size() - numnodes], numnodes);
372 assert(node_size == nextNodeIdx);
373 /* Root index to start traversal at, to handle case of single leaf node. */
374 pack.root_index = (root->is_leaf()) ? -1 : 0;
377 void BVH8::refit_nodes()
379 assert(!params.top_level);
381 BoundBox bbox = BoundBox::empty;
383 refit_node(0, (pack.root_index == -1)? true: false, bbox, visibility);
386 void BVH8::refit_node(int idx, bool leaf, BoundBox& bbox, uint& visibility)
389 int4 *data = &pack.leaf_nodes[idx];
391 /* Refit leaf node. */
392 for(int prim = c.x; prim < c.y; prim++) {
393 int pidx = pack.prim_index[prim];
394 int tob = pack.prim_object[prim];
395 Object *ob = objects[tob];
398 /* Object instance. */
399 bbox.grow(ob->bounds);
403 const Mesh *mesh = ob->mesh;
405 if(pack.prim_type[prim] & PRIMITIVE_ALL_CURVE) {
407 int str_offset = (params.top_level) ? mesh->curve_offset : 0;
408 Mesh::Curve curve = mesh->get_curve(pidx - str_offset);
409 int k = PRIMITIVE_UNPACK_SEGMENT(pack.prim_type[prim]);
411 curve.bounds_grow(k, &mesh->curve_keys[0], &mesh->curve_radius[0], bbox);
413 visibility |= PATH_RAY_CURVE;
416 if(mesh->use_motion_blur) {
417 Attribute *attr = mesh->curve_attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
420 size_t mesh_size = mesh->curve_keys.size();
421 size_t steps = mesh->motion_steps - 1;
422 float3 *key_steps = attr->data_float3();
424 for(size_t i = 0; i < steps; i++) {
425 curve.bounds_grow(k, key_steps + i*mesh_size, &mesh->curve_radius[0], bbox);
432 int tri_offset = (params.top_level) ? mesh->tri_offset : 0;
433 Mesh::Triangle triangle = mesh->get_triangle(pidx - tri_offset);
434 const float3 *vpos = &mesh->verts[0];
436 triangle.bounds_grow(vpos, bbox);
438 /* Motion triangles. */
439 if(mesh->use_motion_blur) {
440 Attribute *attr = mesh->attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
443 size_t mesh_size = mesh->verts.size();
444 size_t steps = mesh->motion_steps - 1;
445 float3 *vert_steps = attr->data_float3();
447 for(size_t i = 0; i < steps; i++) {
448 triangle.bounds_grow(vert_steps + i*mesh_size, bbox);
455 visibility |= ob->visibility;
458 float4 leaf_data[BVH_ONODE_LEAF_SIZE];
459 leaf_data[0].x = __int_as_float(c.x);
460 leaf_data[0].y = __int_as_float(c.y);
461 leaf_data[0].z = __uint_as_float(visibility);
462 leaf_data[0].w = __uint_as_float(c.w);
463 memcpy(&pack.leaf_nodes[idx], leaf_data, sizeof(float4)*BVH_ONODE_LEAF_SIZE);
466 float8 *data = (float8*)&pack.nodes[idx];
467 bool is_unaligned = (__float_as_uint(data[0].a) & PATH_RAY_NODE_UNALIGNED) != 0;
468 /* Refit inner node, set bbox from children. */
469 BoundBox child_bbox[8] = { BoundBox::empty, BoundBox::empty,
470 BoundBox::empty, BoundBox::empty,
471 BoundBox::empty, BoundBox::empty,
472 BoundBox::empty, BoundBox::empty };
474 uint child_visibility[8] = { 0 };
477 for(int i = 0; i < 8; ++i) {
478 child[i] = __float_as_int(data[(is_unaligned) ? 13: 7][i]);
481 refit_node((child[i] < 0)? -child[i]-1: child[i], (child[i] < 0),
482 child_bbox[i], child_visibility[i]);
484 bbox.grow(child_bbox[i]);
485 visibility |= child_visibility[i];
490 Transform aligned_space[8] = { transform_identity(), transform_identity(),
491 transform_identity(), transform_identity(),
492 transform_identity(), transform_identity(),
493 transform_identity(), transform_identity()};
494 pack_unaligned_node(idx,
504 pack_aligned_node(idx,