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