Path: blob/master/thirdparty/embree/kernels/bvh/bvh_rotate.cpp
9906 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#include "bvh_rotate.h"45namespace embree6{7namespace isa8{9/*! Computes half surface area of box. */10__forceinline float halfArea3f(const BBox<vfloat4>& box) {11const vfloat4 d = box.size();12const vfloat4 a = d*shuffle<1,2,0,3>(d);13return a[0]+a[1]+a[2];14}1516size_t BVHNRotate<4>::rotate(NodeRef parentRef, size_t depth)17{18/*! nothing to rotate if we reached a leaf node. */19if (parentRef.isBarrier()) return 0;20if (parentRef.isLeaf()) return 0;21AABBNode* parent = parentRef.getAABBNode();2223/*! rotate all children first */24vint4 cdepth;25for (size_t c=0; c<4; c++)26cdepth[c] = (int)rotate(parent->child(c),depth+1);2728/* compute current areas of all children */29vfloat4 sizeX = parent->upper_x-parent->lower_x;30vfloat4 sizeY = parent->upper_y-parent->lower_y;31vfloat4 sizeZ = parent->upper_z-parent->lower_z;32vfloat4 childArea = madd(sizeX,(sizeY + sizeZ),sizeY*sizeZ);3334/*! get node bounds */35BBox<vfloat4> child1_0,child1_1,child1_2,child1_3;36parent->bounds(child1_0,child1_1,child1_2,child1_3);3738/*! Find best rotation. We pick a first child (child1) and a sub-child39(child2child) of a different second child (child2), and swap child140and child2child. We perform the best such swap. */41float bestArea = 0;42size_t bestChild1 = -1, bestChild2 = -1, bestChild2Child = -1;43for (size_t c2=0; c2<4; c2++)44{45/*! ignore leaf nodes as we cannot descent into them */46if (parent->child(c2).isBarrier()) continue;47if (parent->child(c2).isLeaf()) continue;48AABBNode* child2 = parent->child(c2).getAABBNode();4950/*! transpose child bounds */51BBox<vfloat4> child2c0,child2c1,child2c2,child2c3;52child2->bounds(child2c0,child2c1,child2c2,child2c3);5354/*! put child1_0 at each child2 position */55float cost00 = halfArea3f(merge(child1_0,child2c1,child2c2,child2c3));56float cost01 = halfArea3f(merge(child2c0,child1_0,child2c2,child2c3));57float cost02 = halfArea3f(merge(child2c0,child2c1,child1_0,child2c3));58float cost03 = halfArea3f(merge(child2c0,child2c1,child2c2,child1_0));59vfloat4 cost0 = vfloat4(cost00,cost01,cost02,cost03);60vfloat4 min0 = vreduce_min(cost0);61int pos0 = (int)bsf(movemask(min0 == cost0));6263/*! put child1_1 at each child2 position */64float cost10 = halfArea3f(merge(child1_1,child2c1,child2c2,child2c3));65float cost11 = halfArea3f(merge(child2c0,child1_1,child2c2,child2c3));66float cost12 = halfArea3f(merge(child2c0,child2c1,child1_1,child2c3));67float cost13 = halfArea3f(merge(child2c0,child2c1,child2c2,child1_1));68vfloat4 cost1 = vfloat4(cost10,cost11,cost12,cost13);69vfloat4 min1 = vreduce_min(cost1);70int pos1 = (int)bsf(movemask(min1 == cost1));7172/*! put child1_2 at each child2 position */73float cost20 = halfArea3f(merge(child1_2,child2c1,child2c2,child2c3));74float cost21 = halfArea3f(merge(child2c0,child1_2,child2c2,child2c3));75float cost22 = halfArea3f(merge(child2c0,child2c1,child1_2,child2c3));76float cost23 = halfArea3f(merge(child2c0,child2c1,child2c2,child1_2));77vfloat4 cost2 = vfloat4(cost20,cost21,cost22,cost23);78vfloat4 min2 = vreduce_min(cost2);79int pos2 = (int)bsf(movemask(min2 == cost2));8081/*! put child1_3 at each child2 position */82float cost30 = halfArea3f(merge(child1_3,child2c1,child2c2,child2c3));83float cost31 = halfArea3f(merge(child2c0,child1_3,child2c2,child2c3));84float cost32 = halfArea3f(merge(child2c0,child2c1,child1_3,child2c3));85float cost33 = halfArea3f(merge(child2c0,child2c1,child2c2,child1_3));86vfloat4 cost3 = vfloat4(cost30,cost31,cost32,cost33);87vfloat4 min3 = vreduce_min(cost3);88int pos3 = (int)bsf(movemask(min3 == cost3));8990/*! find best other child */91vfloat4 area0123 = vfloat4(extract<0>(min0),extract<0>(min1),extract<0>(min2),extract<0>(min3)) - vfloat4(childArea[c2]);92int pos[4] = { pos0,pos1,pos2,pos3 };93const size_t mbd = BVH4::maxBuildDepth;94vbool4 valid = vint4(int(depth+1))+cdepth <= vint4(mbd); // only select swaps that fulfill depth constraints95valid &= vint4(int(c2)) != vint4(step);96if (none(valid)) continue;97size_t c1 = select_min(valid,area0123);98float area = area0123[c1];99if (c1 == c2) continue; // can happen if bounds are NANs100101/*! accept a swap when it reduces cost and is not swapping a node with itself */102if (area < bestArea) {103bestArea = area;104bestChild1 = c1;105bestChild2 = c2;106bestChild2Child = pos[c1];107}108}109110/*! if we did not find a swap that improves the SAH then do nothing */111if (bestChild1 == size_t(-1)) return 1+reduce_max(cdepth);112113/*! perform the best found tree rotation */114AABBNode* child2 = parent->child(bestChild2).getAABBNode();115AABBNode::swap(parent,bestChild1,child2,bestChild2Child);116parent->setBounds(bestChild2,child2->bounds());117AABBNode::compact(parent);118AABBNode::compact(child2);119120/*! This returned depth is conservative as the child that was121* pulled up in the tree could have been on the critical path. */122cdepth[bestChild1]++; // bestChild1 was pushed down one level123return 1+reduce_max(cdepth);124}125}126}127128129