bvh_refit.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "../bvh/bvh.h"
  5. namespace embree
  6. {
  7. namespace isa
  8. {
  9. template<int N>
  10. class BVHNRefitter
  11. {
  12. public:
  13. /*! Type shortcuts */
  14. typedef BVHN<N> BVH;
  15. typedef typename BVH::AABBNode AABBNode;
  16. typedef typename BVH::NodeRef NodeRef;
  17. struct LeafBoundsInterface {
  18. virtual const BBox3fa leafBounds(NodeRef& ref) const = 0;
  19. };
  20. public:
  21. /*! Constructor. */
  22. BVHNRefitter (BVH* bvh, const LeafBoundsInterface& leafBounds);
  23. /*! refits the BVH */
  24. void refit();
  25. private:
  26. /* single-threaded subtree extraction based on BVH depth */
  27. void gather_subtree_refs(NodeRef& ref,
  28. size_t &subtrees,
  29. const size_t depth = 0);
  30. /* single-threaded top-level refit */
  31. BBox3fa refit_toplevel(NodeRef& ref,
  32. size_t &subtrees,
  33. const BBox3fa *const subTreeBounds,
  34. const size_t depth = 0);
  35. /* single-threaded subtree refit */
  36. BBox3fa recurse_bottom(NodeRef& ref);
  37. public:
  38. BVH* bvh; //!< BVH to refit
  39. const LeafBoundsInterface& leafBounds; //!< calculates bounds of leaves
  40. static const size_t MAX_SUB_TREE_EXTRACTION_DEPTH = (N==4) ? 4 : (N==8) ? 3 : 3;
  41. static const size_t MAX_NUM_SUB_TREES = (N==4) ? 256 : (N==8) ? 512 : N*N*N; // N ^ MAX_SUB_TREE_EXTRACTION_DEPTH
  42. size_t numSubTrees;
  43. NodeRef subTrees[MAX_NUM_SUB_TREES];
  44. };
  45. template<int N, typename Mesh, typename Primitive>
  46. class BVHNRefitT : public Builder, public BVHNRefitter<N>::LeafBoundsInterface
  47. {
  48. public:
  49. /*! Type shortcuts */
  50. typedef BVHN<N> BVH;
  51. typedef typename BVH::AABBNode AABBNode;
  52. typedef typename BVH::NodeRef NodeRef;
  53. public:
  54. BVHNRefitT (BVH* bvh, Builder* builder, Mesh* mesh, size_t mode);
  55. virtual void build();
  56. virtual void clear();
  57. virtual const BBox3fa leafBounds (NodeRef& ref) const
  58. {
  59. size_t num; char* prim = ref.leaf(num);
  60. if (unlikely(ref == BVH::emptyNode)) return empty;
  61. BBox3fa bounds = empty;
  62. for (size_t i=0; i<num; i++)
  63. bounds.extend(((Primitive*)prim)[i].update(mesh));
  64. return bounds;
  65. }
  66. private:
  67. BVH* bvh;
  68. std::unique_ptr<Builder> builder;
  69. std::unique_ptr<BVHNRefitter<N>> refitter;
  70. Mesh* mesh;
  71. unsigned int topologyVersion;
  72. };
  73. }
  74. }