2
0

bvh_rotate.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include "bvh_rotate.h"
  4. namespace embree
  5. {
  6. namespace isa
  7. {
  8. /*! Computes half surface area of box. */
  9. __forceinline float halfArea3f(const BBox<vfloat4>& box) {
  10. const vfloat4 d = box.size();
  11. const vfloat4 a = d*shuffle<1,2,0,3>(d);
  12. return a[0]+a[1]+a[2];
  13. }
  14. size_t BVHNRotate<4>::rotate(NodeRef parentRef, size_t depth)
  15. {
  16. /*! nothing to rotate if we reached a leaf node. */
  17. if (parentRef.isBarrier()) return 0;
  18. if (parentRef.isLeaf()) return 0;
  19. AABBNode* parent = parentRef.getAABBNode();
  20. /*! rotate all children first */
  21. vint4 cdepth;
  22. for (size_t c=0; c<4; c++)
  23. cdepth[c] = (int)rotate(parent->child(c),depth+1);
  24. /* compute current areas of all children */
  25. vfloat4 sizeX = parent->upper_x-parent->lower_x;
  26. vfloat4 sizeY = parent->upper_y-parent->lower_y;
  27. vfloat4 sizeZ = parent->upper_z-parent->lower_z;
  28. vfloat4 childArea = madd(sizeX,(sizeY + sizeZ),sizeY*sizeZ);
  29. /*! get node bounds */
  30. BBox<vfloat4> child1_0,child1_1,child1_2,child1_3;
  31. parent->bounds(child1_0,child1_1,child1_2,child1_3);
  32. /*! Find best rotation. We pick a first child (child1) and a sub-child
  33. (child2child) of a different second child (child2), and swap child1
  34. and child2child. We perform the best such swap. */
  35. float bestArea = 0;
  36. size_t bestChild1 = -1, bestChild2 = -1, bestChild2Child = -1;
  37. for (size_t c2=0; c2<4; c2++)
  38. {
  39. /*! ignore leaf nodes as we cannot descent into them */
  40. if (parent->child(c2).isBarrier()) continue;
  41. if (parent->child(c2).isLeaf()) continue;
  42. AABBNode* child2 = parent->child(c2).getAABBNode();
  43. /*! transpose child bounds */
  44. BBox<vfloat4> child2c0,child2c1,child2c2,child2c3;
  45. child2->bounds(child2c0,child2c1,child2c2,child2c3);
  46. /*! put child1_0 at each child2 position */
  47. float cost00 = halfArea3f(merge(child1_0,child2c1,child2c2,child2c3));
  48. float cost01 = halfArea3f(merge(child2c0,child1_0,child2c2,child2c3));
  49. float cost02 = halfArea3f(merge(child2c0,child2c1,child1_0,child2c3));
  50. float cost03 = halfArea3f(merge(child2c0,child2c1,child2c2,child1_0));
  51. vfloat4 cost0 = vfloat4(cost00,cost01,cost02,cost03);
  52. vfloat4 min0 = vreduce_min(cost0);
  53. int pos0 = (int)bsf(movemask(min0 == cost0));
  54. /*! put child1_1 at each child2 position */
  55. float cost10 = halfArea3f(merge(child1_1,child2c1,child2c2,child2c3));
  56. float cost11 = halfArea3f(merge(child2c0,child1_1,child2c2,child2c3));
  57. float cost12 = halfArea3f(merge(child2c0,child2c1,child1_1,child2c3));
  58. float cost13 = halfArea3f(merge(child2c0,child2c1,child2c2,child1_1));
  59. vfloat4 cost1 = vfloat4(cost10,cost11,cost12,cost13);
  60. vfloat4 min1 = vreduce_min(cost1);
  61. int pos1 = (int)bsf(movemask(min1 == cost1));
  62. /*! put child1_2 at each child2 position */
  63. float cost20 = halfArea3f(merge(child1_2,child2c1,child2c2,child2c3));
  64. float cost21 = halfArea3f(merge(child2c0,child1_2,child2c2,child2c3));
  65. float cost22 = halfArea3f(merge(child2c0,child2c1,child1_2,child2c3));
  66. float cost23 = halfArea3f(merge(child2c0,child2c1,child2c2,child1_2));
  67. vfloat4 cost2 = vfloat4(cost20,cost21,cost22,cost23);
  68. vfloat4 min2 = vreduce_min(cost2);
  69. int pos2 = (int)bsf(movemask(min2 == cost2));
  70. /*! put child1_3 at each child2 position */
  71. float cost30 = halfArea3f(merge(child1_3,child2c1,child2c2,child2c3));
  72. float cost31 = halfArea3f(merge(child2c0,child1_3,child2c2,child2c3));
  73. float cost32 = halfArea3f(merge(child2c0,child2c1,child1_3,child2c3));
  74. float cost33 = halfArea3f(merge(child2c0,child2c1,child2c2,child1_3));
  75. vfloat4 cost3 = vfloat4(cost30,cost31,cost32,cost33);
  76. vfloat4 min3 = vreduce_min(cost3);
  77. int pos3 = (int)bsf(movemask(min3 == cost3));
  78. /*! find best other child */
  79. vfloat4 area0123 = vfloat4(extract<0>(min0),extract<0>(min1),extract<0>(min2),extract<0>(min3)) - vfloat4(childArea[c2]);
  80. int pos[4] = { pos0,pos1,pos2,pos3 };
  81. const size_t mbd = BVH4::maxBuildDepth;
  82. vbool4 valid = vint4(int(depth+1))+cdepth <= vint4(mbd); // only select swaps that fulfill depth constraints
  83. valid &= vint4(int(c2)) != vint4(step);
  84. if (none(valid)) continue;
  85. size_t c1 = select_min(valid,area0123);
  86. float area = area0123[c1];
  87. if (c1 == c2) continue; // can happen if bounds are NANs
  88. /*! accept a swap when it reduces cost and is not swapping a node with itself */
  89. if (area < bestArea) {
  90. bestArea = area;
  91. bestChild1 = c1;
  92. bestChild2 = c2;
  93. bestChild2Child = pos[c1];
  94. }
  95. }
  96. /*! if we did not find a swap that improves the SAH then do nothing */
  97. if (bestChild1 == size_t(-1)) return 1+reduce_max(cdepth);
  98. /*! perform the best found tree rotation */
  99. AABBNode* child2 = parent->child(bestChild2).getAABBNode();
  100. AABBNode::swap(parent,bestChild1,child2,bestChild2Child);
  101. parent->setBounds(bestChild2,child2->bounds());
  102. AABBNode::compact(parent);
  103. AABBNode::compact(child2);
  104. /*! This returned depth is conservative as the child that was
  105. * pulled up in the tree could have been on the critical path. */
  106. cdepth[bestChild1]++; // bestChild1 was pushed down one level
  107. return 1+reduce_max(cdepth);
  108. }
  109. }
  110. }