// Created on: 2014-09-11 // Created by: Danila ULYANOV // Copyright (c) 2013-2014 OPEN CASCADE SAS // // This file is part of Open CASCADE Technology software library. // // This library is free software; you can redistribute it and/or modify it under // the terms of the GNU Lesser General Public License version 2.1 as published // by the Free Software Foundation, with special exception defined in the file // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT // distribution for complete text of the license and disclaimer of any warranty. // // Alternatively, this file may be used under the terms of Open CASCADE // commercial license or contractual agreement. #ifndef _BVH_LinearBuilder_Header #define _BVH_LinearBuilder_Header #include "BVH_RadixSorter.hxx" #include "NCollection_DynamicArray.hxx" #include "Standard_Assert.hxx" //! Performs fast BVH construction using LBVH building approach. //! Algorithm uses spatial Morton codes to reduce the BVH construction //! problem to a sorting problem (radix sort -- O(N) complexity). This //! Linear Bounding Volume Hierarchy (LBVH) builder produces BVH trees //! of lower quality compared to SAH-based BVH builders but it is over //! an order of magnitude faster (up to 3M triangles per second). //! //! For more details see: //! C. Lauterbach, M. Garland, S. Sengupta, D. Luebke, and D. Manocha. //! Fast BVH construction on GPUs. Eurographics, 2009. template class BVH_LinearBuilder : public BVH_Builder { public: typedef typename BVH::VectorType::Type BVH_VecNt; public: //! Creates binned LBVH builder. BVH_LinearBuilder(const int theLeafNodeSize = BVH_Constants_LeafNodeSizeDefault, const int theMaxTreeDepth = BVH_Constants_MaxTreeDepth); //! Releases resources of LBVH builder. ~BVH_LinearBuilder() override; //! Builds BVH. void Build(BVH_Set* theSet, BVH_Tree* theBVH, const BVH_Box& theBox) const override; protected: typedef NCollection_Array1::iterator LinkIterator; protected: //! Emits hierarchy from sorted Morton codes. int emitHierachy(BVH_Tree* theBVH, const NCollection_Array1& theEncodedLinks, const int theBit, const int theShift, const int theStart, const int theFinal) const; //! Returns index of the first element which does not compare less than the given one. int lowerBound(const NCollection_Array1& theEncodedLinks, int theStart, int theFinal, int theDigit) const; }; //================================================================================================= template BVH_LinearBuilder::BVH_LinearBuilder(const int theLeafNodeSize, const int theMaxTreeDepth) : BVH_Builder(theLeafNodeSize, theMaxTreeDepth) { } //================================================================================================= template BVH_LinearBuilder::~BVH_LinearBuilder() = default; // ======================================================================= // function : lowerBound // purpose : Returns index of first element greater than the given one // ======================================================================= template int BVH_LinearBuilder::lowerBound(const NCollection_Array1& theEncodedLinks, int theStart, int theFinal, int theDigit) const { int aNbPrims = theFinal - theStart; unsigned int aBit = 1U << theDigit; while (aNbPrims > 0) { const int aStep = aNbPrims / 2; if (theEncodedLinks.Value(theStart + aStep).first & aBit) { aNbPrims = aStep; } else { theStart += aStep + 1; aNbPrims -= aStep + 1; } } return theStart; } // ======================================================================= // function : emitHierachy // purpose : Emits hierarchy from sorted Morton codes // ======================================================================= template int BVH_LinearBuilder::emitHierachy( BVH_Tree* theBVH, const NCollection_Array1& theEncodedLinks, const int theDigit, const int theShift, const int theStart, const int theFinal) const { if (theFinal - theStart > BVH_Builder::myLeafNodeSize) { const int aPosition = theDigit < 0 ? (theStart + theFinal) / 2 : lowerBound(theEncodedLinks, theStart, theFinal, theDigit); if (aPosition == theStart || aPosition == theFinal) { return emitHierachy(theBVH, theEncodedLinks, theDigit - 1, theShift, theStart, theFinal); } // Build inner node const int aNode = theBVH->AddInnerNode(0, 0); const int aRghNode = theShift + aPosition - theStart; const int aLftChild = emitHierachy(theBVH, theEncodedLinks, theDigit - 1, theShift, theStart, aPosition); const int aRghChild = emitHierachy(theBVH, theEncodedLinks, theDigit - 1, aRghNode, aPosition, theFinal); theBVH->NodeInfoBuffer()[aNode].y() = aLftChild; theBVH->NodeInfoBuffer()[aNode].z() = aRghChild; return aNode; } else { // Build leaf node return theBVH->AddLeafNode(theShift, theShift + theFinal - theStart - 1); } } namespace BVH { //! Calculates bounding boxes (AABBs) for the given BVH tree. template int UpdateBounds(BVH_Set* theSet, BVH_Tree* theTree, const int theNode = 0) { const BVH_Vec4i aData = theTree->NodeInfoBuffer()[theNode]; if (aData.x() == 0) { const int aLftChild = theTree->NodeInfoBuffer()[theNode].y(); const int aRghChild = theTree->NodeInfoBuffer()[theNode].z(); const int aLftDepth = UpdateBounds(theSet, theTree, aLftChild); const int aRghDepth = UpdateBounds(theSet, theTree, aRghChild); typename BVH_Box::BVH_VecNt aLftMinPoint = theTree->MinPointBuffer()[aLftChild]; typename BVH_Box::BVH_VecNt aLftMaxPoint = theTree->MaxPointBuffer()[aLftChild]; typename BVH_Box::BVH_VecNt aRghMinPoint = theTree->MinPointBuffer()[aRghChild]; typename BVH_Box::BVH_VecNt aRghMaxPoint = theTree->MaxPointBuffer()[aRghChild]; BVH::BoxMinMax::CwiseMin(aLftMinPoint, aRghMinPoint); BVH::BoxMinMax::CwiseMax(aLftMaxPoint, aRghMaxPoint); theTree->MinPointBuffer()[theNode] = aLftMinPoint; theTree->MaxPointBuffer()[theNode] = aLftMaxPoint; return (std::max)(aLftDepth, aRghDepth) + 1; } else { typename BVH_Box::BVH_VecNt& aMinPoint = theTree->MinPointBuffer()[theNode]; typename BVH_Box::BVH_VecNt& aMaxPoint = theTree->MaxPointBuffer()[theNode]; for (int aPrimIdx = aData.y(); aPrimIdx <= aData.z(); ++aPrimIdx) { const BVH_Box aBox = theSet->Box(aPrimIdx); if (aPrimIdx == aData.y()) { aMinPoint = aBox.CornerMin(); aMaxPoint = aBox.CornerMax(); } else { BVH::BoxMinMax::CwiseMin(aMinPoint, aBox.CornerMin()); BVH::BoxMinMax::CwiseMax(aMaxPoint, aBox.CornerMax()); } } } return 0; } template struct BoundData { BVH_Set* mySet; //!< Set of geometric objects BVH_Tree* myBVH; //!< BVH tree built over the set int myNode; //!< BVH node to update bounding box int myLevel; //!< Level of the processed BVH node int* myHeight; //!< Height of the processed BVH node }; //! Task for parallel bounds updating. template class UpdateBoundTask { public: UpdateBoundTask(const bool isParallel) : myIsParallel(isParallel) { } //! Executes the task. void operator()(const BoundData& theData) const { if (theData.myBVH->IsOuter(theData.myNode) || theData.myLevel > 2) { *theData.myHeight = BVH::UpdateBounds(theData.mySet, theData.myBVH, theData.myNode); } else { int aLftHeight = 0; int aRghHeight = 0; const int aLftChild = theData.myBVH->NodeInfoBuffer()[theData.myNode].y(); const int aRghChild = theData.myBVH->NodeInfoBuffer()[theData.myNode].z(); NCollection_DynamicArray> aList(2); if (!theData.myBVH->IsOuter(aLftChild)) { BoundData aBoundData = {theData.mySet, theData.myBVH, aLftChild, theData.myLevel + 1, &aLftHeight}; aList.Append(aBoundData); } else { aLftHeight = BVH::UpdateBounds(theData.mySet, theData.myBVH, aLftChild); } if (!theData.myBVH->IsOuter(aRghChild)) { BoundData aBoundData = {theData.mySet, theData.myBVH, aRghChild, theData.myLevel + 1, &aRghHeight}; aList.Append(aBoundData); } else { aRghHeight = BVH::UpdateBounds(theData.mySet, theData.myBVH, aRghChild); } if (aList.Size() > 0) { OSD_Parallel::ForEach(aList.begin(), aList.end(), UpdateBoundTask(myIsParallel), !myIsParallel); } typename BVH_Box::BVH_VecNt aLftMinPoint = theData.myBVH->MinPointBuffer()[aLftChild]; typename BVH_Box::BVH_VecNt aLftMaxPoint = theData.myBVH->MaxPointBuffer()[aLftChild]; typename BVH_Box::BVH_VecNt aRghMinPoint = theData.myBVH->MinPointBuffer()[aRghChild]; typename BVH_Box::BVH_VecNt aRghMaxPoint = theData.myBVH->MaxPointBuffer()[aRghChild]; BVH::BoxMinMax::CwiseMin(aLftMinPoint, aRghMinPoint); BVH::BoxMinMax::CwiseMax(aLftMaxPoint, aRghMaxPoint); theData.myBVH->MinPointBuffer()[theData.myNode] = aLftMinPoint; theData.myBVH->MaxPointBuffer()[theData.myNode] = aLftMaxPoint; *theData.myHeight = (std::max)(aLftHeight, aRghHeight) + 1; } } private: bool myIsParallel; }; } // namespace BVH //================================================================================================= template void BVH_LinearBuilder::Build(BVH_Set* theSet, BVH_Tree* theBVH, const BVH_Box& theBox) const { Standard_STATIC_ASSERT(N == 2 || N == 3 || N == 4); const int aSetSize = theSet->Size(); if (theBVH == nullptr || aSetSize == 0) { return; } theBVH->Clear(); // Step 0 -- Initialize parameter of virtual grid BVH_RadixSorter aRadixSorter(theBox); aRadixSorter.SetParallel(this->IsParallel()); // Step 1 - Perform radix sorting of primitive set aRadixSorter.Perform(theSet); // Step 2 -- Emitting BVH hierarchy from sorted Morton codes emitHierachy(theBVH, aRadixSorter.EncodedLinks(), 29, 0, 0, theSet->Size()); // Step 3 -- Compute bounding boxes of BVH nodes theBVH->MinPointBuffer().Resize(theBVH->NodeInfoBuffer().Size()); theBVH->MaxPointBuffer().Resize(theBVH->NodeInfoBuffer().Size()); int aHeight = 0; BVH::BoundData aBoundData = {theSet, theBVH, 0, 0, &aHeight}; BVH::UpdateBoundTask aBoundTask(this->IsParallel()); aBoundTask(aBoundData); BVH_Builder::updateDepth(theBVH, aHeight); } #endif // _BVH_LinearBuilder_Header