2016-07-19 17:35:25 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
|
2020-12-12 17:29:11 +00:00
|
|
|
* Copyright (C) 2015-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-07-19 17:35:25 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2023-04-19 11:51:26 +00:00
|
|
|
* @file bvh_pbrt.cpp
|
2016-07-19 17:35:25 +00:00
|
|
|
* @brief This BVH implementation is based on the source code implementation
|
|
|
|
* from the book "Physically Based Rendering" (v2 and v3)
|
|
|
|
*
|
2020-12-12 17:29:11 +00:00
|
|
|
* Adaptions performed for KiCad:
|
2016-07-19 17:35:25 +00:00
|
|
|
* - Types and class types adapted to KiCad project
|
|
|
|
* - Convert some source to build in the C++ specification of KiCad
|
|
|
|
* - Code style to match KiCad
|
|
|
|
* - Asserts converted
|
|
|
|
* - Use compare functions/structures for std::partition and std::nth_element
|
|
|
|
*
|
2020-12-12 17:29:11 +00:00
|
|
|
* The original source code has the following license:
|
2016-07-19 17:35:25 +00:00
|
|
|
*
|
|
|
|
* "pbrt source code is Copyright(c) 1998-2015
|
|
|
|
* Matt Pharr, Greg Humphreys, and Wenzel Jakob.
|
|
|
|
*
|
|
|
|
* This file is part of pbrt.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
|
|
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-01-03 22:23:00 +00:00
|
|
|
#include "bvh_pbrt.h"
|
2016-07-19 17:35:25 +00:00
|
|
|
#include "../../../3d_fastmath.h"
|
2020-04-24 23:44:09 +00:00
|
|
|
#include <macros.h>
|
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
#include <boost/range/algorithm/nth_element.hpp>
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <boost/range/algorithm/partition.hpp>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <vector>
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
#include <stack>
|
|
|
|
#include <wx/debug.h>
|
|
|
|
|
|
|
|
#ifdef PRINT_STATISTICS_3D_VIEWER
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// BVHAccel Local Declarations
|
|
|
|
struct BVHPrimitiveInfo
|
|
|
|
{
|
|
|
|
BVHPrimitiveInfo()
|
|
|
|
{
|
|
|
|
primitiveNumber = 0;
|
|
|
|
bounds.Reset();
|
|
|
|
centroid = SFVEC3F( 0.0f );
|
|
|
|
}
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BVHPrimitiveInfo( int aPrimitiveNumber, const BBOX_3D& aBounds ) :
|
2016-07-19 17:35:25 +00:00
|
|
|
primitiveNumber( aPrimitiveNumber ),
|
|
|
|
bounds( aBounds ),
|
|
|
|
centroid( .5f * aBounds.Min() + .5f * aBounds.Max() ) {}
|
|
|
|
|
|
|
|
int primitiveNumber;
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D bounds;
|
2016-07-19 17:35:25 +00:00
|
|
|
SFVEC3F centroid;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct BVHBuildNode
|
|
|
|
{
|
|
|
|
// BVHBuildNode Public Methods
|
2021-01-02 21:05:29 +00:00
|
|
|
void InitLeaf( int first, int n, const BBOX_3D& b)
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
firstPrimOffset = first;
|
|
|
|
nPrimitives = n;
|
|
|
|
bounds = b;
|
2021-01-02 21:05:29 +00:00
|
|
|
children[0] = children[1] = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void InitInterior( int axis, BVHBuildNode* c0, BVHBuildNode* c1 )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
children[0] = c0;
|
|
|
|
children[1] = c1;
|
|
|
|
bounds.Set( c0->bounds );
|
|
|
|
bounds.Union( c1->bounds );
|
|
|
|
splitAxis = axis;
|
|
|
|
nPrimitives = 0;
|
|
|
|
}
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D bounds;
|
|
|
|
BVHBuildNode* children[2];
|
2016-07-19 17:35:25 +00:00
|
|
|
int splitAxis, firstPrimOffset, nPrimitives;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct MortonPrimitive
|
|
|
|
{
|
|
|
|
int primitiveIndex;
|
|
|
|
uint32_t mortonCode;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct LBVHTreelet
|
|
|
|
{
|
|
|
|
int startIndex, numPrimitives;
|
|
|
|
BVHBuildNode *buildNodes;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// BVHAccel Utility Functions
|
|
|
|
inline uint32_t LeftShift3( uint32_t x )
|
|
|
|
{
|
|
|
|
wxASSERT( x <= (1 << 10) );
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
if( x == ( 1 << 10 ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
--x;
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
x = ( x | ( x << 16 ) ) & 0b00000011000000000000000011111111;
|
2016-07-19 17:35:25 +00:00
|
|
|
// x = ---- --98 ---- ---- ---- ---- 7654 3210
|
2021-01-07 19:33:43 +00:00
|
|
|
x = ( x | ( x << 8 ) ) & 0b00000011000000001111000000001111;
|
2016-07-19 17:35:25 +00:00
|
|
|
// x = ---- --98 ---- ---- 7654 ---- ---- 3210
|
2021-01-07 19:33:43 +00:00
|
|
|
x = ( x | ( x << 4 ) ) & 0b00000011000011000011000011000011;
|
2016-07-19 17:35:25 +00:00
|
|
|
// x = ---- --98 ---- 76-- --54 ---- 32-- --10
|
2021-01-07 19:33:43 +00:00
|
|
|
x = ( x | ( x << 2 ) ) & 0b00001001001001001001001001001001;
|
2016-07-19 17:35:25 +00:00
|
|
|
// x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
inline uint32_t EncodeMorton3( const SFVEC3F& v )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( v.x >= 0 && v.x <= ( 1 << 10 ) );
|
|
|
|
wxASSERT( v.y >= 0 && v.y <= ( 1 << 10 ) );
|
|
|
|
wxASSERT( v.z >= 0 && v.z <= ( 1 << 10 ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
return ( LeftShift3( v.z ) << 2 ) | ( LeftShift3( v.y ) << 1 ) | LeftShift3( v.x );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
static void RadixSort( std::vector<MortonPrimitive>* v )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
std::vector<MortonPrimitive> tempVector( v->size() );
|
|
|
|
|
|
|
|
const int bitsPerPass = 6;
|
|
|
|
const int nBits = 30;
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
wxASSERT( ( nBits % bitsPerPass ) == 0 );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
const int nPasses = nBits / bitsPerPass;
|
|
|
|
|
|
|
|
for( int pass = 0; pass < nPasses; ++pass )
|
|
|
|
{
|
|
|
|
// Perform one pass of radix sort, sorting _bitsPerPass_ bits
|
|
|
|
const int lowBit = pass * bitsPerPass;
|
|
|
|
|
|
|
|
// Set in and out vector pointers for radix sort pass
|
2021-01-02 21:05:29 +00:00
|
|
|
std::vector<MortonPrimitive>& in = ( pass & 1 ) ? tempVector : *v;
|
|
|
|
std::vector<MortonPrimitive>& out = ( pass & 1 ) ? *v : tempVector;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Count number of zero bits in array for current radix sort bit
|
|
|
|
const int nBuckets = 1 << bitsPerPass;
|
|
|
|
int bucketCount[nBuckets] = {0};
|
2021-01-07 19:33:43 +00:00
|
|
|
const int bitMask = ( 1 << bitsPerPass ) - 1;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
for( uint32_t i = 0; i < in.size(); ++i )
|
|
|
|
{
|
|
|
|
const MortonPrimitive &mp = in[i];
|
2021-01-02 21:05:29 +00:00
|
|
|
int bucket = ( mp.mortonCode >> lowBit ) & bitMask;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( ( bucket >= 0 ) && ( bucket < nBuckets ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
++bucketCount[bucket];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute starting index in output array for each bucket
|
|
|
|
int startIndex[nBuckets];
|
|
|
|
startIndex[0] = 0;
|
|
|
|
|
|
|
|
for( int i = 1; i < nBuckets; ++i )
|
|
|
|
startIndex[i] = startIndex[i - 1] + bucketCount[i - 1];
|
|
|
|
|
|
|
|
// Store sorted values in output array
|
|
|
|
for( uint32_t i = 0; i < in.size(); ++i )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const MortonPrimitive& mp = in[i];
|
2016-07-19 17:35:25 +00:00
|
|
|
int bucket = (mp.mortonCode >> lowBit) & bitMask;
|
|
|
|
out[startIndex[bucket]++] = mp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy final result from _tempVector_, if needed
|
|
|
|
if( nPasses & 1 )
|
|
|
|
std::swap( *v, tempVector );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BVH_PBRT::BVH_PBRT( const CONTAINER_3D_BASE& aObjectContainer, int aMaxPrimsInNode,
|
2021-01-07 19:33:43 +00:00
|
|
|
SPLITMETHOD aSplitMethod ) :
|
2016-07-19 17:35:25 +00:00
|
|
|
m_maxPrimsInNode( std::min( 255, aMaxPrimsInNode ) ),
|
|
|
|
m_splitMethod( aSplitMethod )
|
|
|
|
{
|
|
|
|
if( aObjectContainer.GetList().empty() )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
m_nodes = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the indexes of ray packet for partition traversal
|
|
|
|
for( unsigned int i = 0; i < RAYPACKET_RAYS_PER_PACKET; ++i )
|
|
|
|
{
|
|
|
|
m_I[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the objects list to vector of objects
|
|
|
|
aObjectContainer.ConvertTo( m_primitives );
|
|
|
|
|
|
|
|
wxASSERT( aObjectContainer.GetList().size() == m_primitives.size() );
|
|
|
|
|
|
|
|
// Initialize _primitiveInfo_ array for primitives
|
|
|
|
std::vector<BVHPrimitiveInfo> primitiveInfo( m_primitives.size() );
|
|
|
|
|
|
|
|
for( size_t i = 0; i < m_primitives.size(); ++i )
|
|
|
|
{
|
|
|
|
wxASSERT( m_primitives[i]->GetBBox().IsInitialized() );
|
|
|
|
|
|
|
|
primitiveInfo[i] = BVHPrimitiveInfo( i, m_primitives[i]->GetBBox() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build BVH tree for primitives using _primitiveInfo_
|
|
|
|
int totalNodes = 0;
|
|
|
|
|
|
|
|
CONST_VECTOR_OBJECT orderedPrims;
|
|
|
|
orderedPrims.clear();
|
|
|
|
orderedPrims.reserve( m_primitives.size() );
|
|
|
|
|
|
|
|
BVHBuildNode *root;
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
if( m_splitMethod == SPLITMETHOD::HLBVH )
|
2021-01-02 21:05:29 +00:00
|
|
|
root = HLBVHBuild( primitiveInfo, &totalNodes, orderedPrims );
|
2016-07-19 17:35:25 +00:00
|
|
|
else
|
2021-01-02 21:05:29 +00:00
|
|
|
root = recursiveBuild( primitiveInfo, 0, m_primitives.size(), &totalNodes, orderedPrims );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
wxASSERT( m_primitives.size() == orderedPrims.size() );
|
|
|
|
|
|
|
|
m_primitives.swap( orderedPrims );
|
|
|
|
|
|
|
|
// Compute representation of depth-first traversal of BVH tree
|
2021-01-02 21:05:29 +00:00
|
|
|
m_nodes = static_cast<LinearBVHNode*>( malloc( sizeof( LinearBVHNode ) * totalNodes ) );
|
2021-01-07 19:33:43 +00:00
|
|
|
m_nodesToFree.push_back( m_nodes );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
for( int i = 0; i < totalNodes; ++i )
|
|
|
|
{
|
|
|
|
m_nodes[i].bounds.Reset();
|
|
|
|
m_nodes[i].primitivesOffset = 0;
|
|
|
|
m_nodes[i].nPrimitives = 0;
|
|
|
|
m_nodes[i].axis = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
flattenBVHTree( root, &offset );
|
|
|
|
|
|
|
|
wxASSERT( offset == (unsigned int)totalNodes );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BVH_PBRT::~BVH_PBRT()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
if( !m_nodesToFree.empty() )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
for( std::list<void *>::iterator ii = m_nodesToFree.begin(); ii != m_nodesToFree.end();
|
2016-07-19 17:35:25 +00:00
|
|
|
++ii )
|
|
|
|
{
|
2017-02-09 20:23:40 +00:00
|
|
|
free( *ii );
|
2021-01-02 21:05:29 +00:00
|
|
|
*ii = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
m_nodesToFree.clear();
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ComparePoints
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
explicit ComparePoints( int d ) { dim = d; }
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
int dim;
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool operator()( const BVHPrimitiveInfo& a, const BVHPrimitiveInfo& b ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
return a.centroid[dim] < b.centroid[dim];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CompareToMid
|
|
|
|
{
|
|
|
|
explicit CompareToMid( int d, float m ) { dim = d; mid = m; }
|
|
|
|
|
|
|
|
int dim;
|
|
|
|
float mid;
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool operator()( const BVHPrimitiveInfo& a ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
return a.centroid[dim] < mid;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CompareToBucket
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
CompareToBucket( int split, int num, int d, const BBOX_3D& b )
|
|
|
|
: centroidBounds( b )
|
2020-12-12 17:29:11 +00:00
|
|
|
{
|
|
|
|
splitBucket = split;
|
2021-01-02 21:05:29 +00:00
|
|
|
nBuckets = num;
|
|
|
|
dim = d;
|
2020-12-12 17:29:11 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool operator()( const BVHPrimitiveInfo& p ) const;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
int splitBucket, nBuckets, dim;
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const BBOX_3D& centroidBounds;
|
2016-07-19 17:35:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool CompareToBucket::operator()( const BVHPrimitiveInfo& p ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
const float centroid = p.centroid[dim];
|
|
|
|
|
|
|
|
int b = nBuckets *
|
|
|
|
// Computes the offset (0.0 - 1.0) for one axis
|
|
|
|
( ( centroid - centroidBounds.Min()[dim] ) /
|
|
|
|
( centroidBounds.Max()[dim] - centroidBounds.Min()[dim] ) );
|
|
|
|
|
|
|
|
if( b == nBuckets )
|
|
|
|
b = nBuckets - 1;
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( ( b >= 0 ) && ( b < nBuckets ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
return b <= splitBucket;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct HLBVH_SAH_Evaluator
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
HLBVH_SAH_Evaluator( int split, int num, int d, const BBOX_3D& b )
|
2016-07-19 17:35:25 +00:00
|
|
|
: centroidBounds(b)
|
2020-12-12 17:29:11 +00:00
|
|
|
{
|
|
|
|
minCostSplitBucket = split;
|
|
|
|
nBuckets = num; dim = d;
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool operator()( const BVHBuildNode* node ) const;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
int minCostSplitBucket, nBuckets, dim;
|
2021-01-02 21:05:29 +00:00
|
|
|
const BBOX_3D& centroidBounds;
|
2016-07-19 17:35:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool HLBVH_SAH_Evaluator::operator()( const BVHBuildNode* node ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
const float centroid = node->bounds.GetCenter( dim );
|
|
|
|
|
|
|
|
int b = nBuckets *
|
|
|
|
// Computes the offset (0.0 - 1.0) for one axis
|
|
|
|
( ( centroid - centroidBounds.Min()[dim] ) /
|
|
|
|
( centroidBounds.Max()[dim] - centroidBounds.Min()[dim] ) );
|
|
|
|
|
|
|
|
if( b == nBuckets )
|
|
|
|
b = nBuckets - 1;
|
|
|
|
|
|
|
|
wxASSERT( b >= 0 && b < nBuckets );
|
|
|
|
|
|
|
|
return b <= minCostSplitBucket;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct BucketInfo
|
|
|
|
{
|
|
|
|
int count;
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D bounds;
|
2016-07-19 17:35:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BVHBuildNode *BVH_PBRT::recursiveBuild ( std::vector<BVHPrimitiveInfo>& primitiveInfo,
|
|
|
|
int start, int end, int* totalNodes,
|
|
|
|
CONST_VECTOR_OBJECT& orderedPrims )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( totalNodes != nullptr );
|
2016-07-19 17:35:25 +00:00
|
|
|
wxASSERT( start >= 0 );
|
|
|
|
wxASSERT( end >= 0 );
|
|
|
|
wxASSERT( start != end );
|
|
|
|
wxASSERT( start < end );
|
|
|
|
wxASSERT( start <= (int)primitiveInfo.size() );
|
|
|
|
wxASSERT( end <= (int)primitiveInfo.size() );
|
|
|
|
|
|
|
|
(*totalNodes)++;
|
|
|
|
|
2024-02-05 11:13:45 +00:00
|
|
|
// !TODO: implement a memory arena
|
2017-02-09 20:23:40 +00:00
|
|
|
BVHBuildNode *node = static_cast<BVHBuildNode *>( malloc( sizeof( BVHBuildNode ) ) );
|
2021-01-07 19:33:43 +00:00
|
|
|
m_nodesToFree.push_back( node );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
node->bounds.Reset();
|
|
|
|
node->firstPrimOffset = 0;
|
|
|
|
node->nPrimitives = 0;
|
|
|
|
node->splitAxis = 0;
|
2021-01-02 21:05:29 +00:00
|
|
|
node->children[0] = nullptr;
|
|
|
|
node->children[1] = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Compute bounds of all primitives in BVH node
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D bounds;
|
2016-07-19 17:35:25 +00:00
|
|
|
bounds.Reset();
|
|
|
|
|
|
|
|
for( int i = start; i < end; ++i )
|
|
|
|
bounds.Union( primitiveInfo[i].bounds );
|
|
|
|
|
|
|
|
int nPrimitives = end - start;
|
|
|
|
|
|
|
|
if( nPrimitives == 1 )
|
|
|
|
{
|
|
|
|
// Create leaf _BVHBuildNode_
|
|
|
|
int firstPrimOffset = orderedPrims.size();
|
|
|
|
|
|
|
|
for( int i = start; i < end; ++i )
|
|
|
|
{
|
|
|
|
int primitiveNr = primitiveInfo[i].primitiveNumber;
|
|
|
|
wxASSERT( primitiveNr < (int)m_primitives.size() );
|
|
|
|
orderedPrims.push_back( m_primitives[ primitiveNr ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
node->InitLeaf( firstPrimOffset, nPrimitives, bounds );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Compute bound of primitive centroids, choose split dimension _dim_
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D centroidBounds;
|
2016-07-19 17:35:25 +00:00
|
|
|
centroidBounds.Reset();
|
|
|
|
|
|
|
|
for( int i = start; i < end; ++i )
|
|
|
|
centroidBounds.Union( primitiveInfo[i].centroid );
|
|
|
|
|
|
|
|
const int dim = centroidBounds.MaxDimension();
|
|
|
|
|
|
|
|
// Partition primitives into two sets and build children
|
|
|
|
int mid = (start + end) / 2;
|
|
|
|
|
|
|
|
if( fabs( centroidBounds.Max()[dim] -
|
|
|
|
centroidBounds.Min()[dim] ) < (FLT_EPSILON + FLT_EPSILON) )
|
|
|
|
{
|
|
|
|
// Create leaf _BVHBuildNode_
|
|
|
|
const int firstPrimOffset = orderedPrims.size();
|
|
|
|
|
|
|
|
for( int i = start; i < end; ++i )
|
|
|
|
{
|
|
|
|
int primitiveNr = primitiveInfo[i].primitiveNumber;
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
wxASSERT( ( primitiveNr >= 0 ) && ( primitiveNr < (int) m_primitives.size() ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const OBJECT_3D* obj = static_cast<const OBJECT_3D*>( m_primitives[ primitiveNr ] );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( obj != nullptr );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
orderedPrims.push_back( obj );
|
|
|
|
}
|
|
|
|
|
|
|
|
node->InitLeaf( firstPrimOffset, nPrimitives, bounds );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Partition primitives based on _splitMethod_
|
|
|
|
switch( m_splitMethod )
|
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
case SPLITMETHOD::MIDDLE:
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// Partition primitives through node's midpoint
|
|
|
|
float pmid = centroidBounds.GetCenter( dim );
|
|
|
|
|
|
|
|
BVHPrimitiveInfo *midPtr = std::partition( &primitiveInfo[start],
|
|
|
|
&primitiveInfo[end - 1] + 1,
|
|
|
|
CompareToMid( dim, pmid ) );
|
|
|
|
mid = midPtr - &primitiveInfo[0];
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
wxASSERT( ( mid >= start ) && ( mid <= end ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
if( ( mid != start ) && ( mid != end ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-11 19:16:32 +00:00
|
|
|
// Intentionally fall through to SPLITMETHOD::EQUAL_COUNTS since prims
|
|
|
|
// with large overlapping bounding boxes may fail to partition
|
2020-04-24 23:44:09 +00:00
|
|
|
KI_FALLTHROUGH;
|
2020-01-11 19:16:32 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case SPLITMETHOD::EQUALCOUNTS:
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// Partition primitives into equally-sized subsets
|
2021-01-07 19:33:43 +00:00
|
|
|
mid = ( start + end ) / 2;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
std::nth_element( &primitiveInfo[start], &primitiveInfo[mid],
|
|
|
|
&primitiveInfo[end - 1] + 1, ComparePoints( dim ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case SPLITMETHOD::SAH:
|
2016-07-19 17:35:25 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
// Partition primitives using approximate SAH
|
|
|
|
if( nPrimitives <= 2 )
|
|
|
|
{
|
|
|
|
// Partition primitives into equally-sized subsets
|
2021-01-07 19:33:43 +00:00
|
|
|
mid = ( start + end ) / 2;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
std::nth_element( &primitiveInfo[start], &primitiveInfo[mid],
|
|
|
|
&primitiveInfo[end - 1] + 1, ComparePoints( dim ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Allocate _BucketInfo_ for SAH partition buckets
|
|
|
|
const int nBuckets = 12;
|
|
|
|
|
|
|
|
BucketInfo buckets[nBuckets];
|
|
|
|
|
|
|
|
for( int i = 0; i < nBuckets; ++i )
|
|
|
|
{
|
|
|
|
buckets[i].count = 0;
|
|
|
|
buckets[i].bounds.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize _BucketInfo_ for SAH partition buckets
|
|
|
|
for( int i = start; i < end; ++i )
|
|
|
|
{
|
|
|
|
int b = nBuckets *
|
|
|
|
centroidBounds.Offset( primitiveInfo[i].centroid )[dim];
|
|
|
|
|
|
|
|
if( b == nBuckets )
|
|
|
|
b = nBuckets - 1;
|
|
|
|
|
|
|
|
wxASSERT( b >= 0 && b < nBuckets );
|
|
|
|
|
|
|
|
buckets[b].count++;
|
|
|
|
buckets[b].bounds.Union( primitiveInfo[i].bounds );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute costs for splitting after each bucket
|
|
|
|
float cost[nBuckets - 1];
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
for( int i = 0; i < ( nBuckets - 1 ); ++i )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D b0, b1;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
b0.Reset();
|
|
|
|
b1.Reset();
|
|
|
|
|
|
|
|
int count0 = 0;
|
|
|
|
int count1 = 0;
|
|
|
|
|
|
|
|
for( int j = 0; j <= i; ++j )
|
|
|
|
{
|
|
|
|
if( buckets[j].count )
|
|
|
|
{
|
|
|
|
count0 += buckets[j].count;
|
|
|
|
b0.Union( buckets[j].bounds );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( int j = i + 1; j < nBuckets; ++j )
|
|
|
|
{
|
|
|
|
if( buckets[j].count )
|
|
|
|
{
|
|
|
|
count1 += buckets[j].count;
|
|
|
|
b1.Union( buckets[j].bounds );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
cost[i] = 1.0f + ( count0 * b0.SurfaceArea() +
|
|
|
|
count1 * b1.SurfaceArea() ) / bounds.SurfaceArea();
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find bucket to split at that minimizes SAH metric
|
|
|
|
float minCost = cost[0];
|
|
|
|
int minCostSplitBucket = 0;
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
for( int i = 1; i < ( nBuckets - 1 ); ++i )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
if( cost[i] < minCost )
|
|
|
|
{
|
|
|
|
minCost = cost[i];
|
|
|
|
minCostSplitBucket = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
// Either create leaf or split primitives at selected SAH bucket
|
|
|
|
if( ( nPrimitives > m_maxPrimsInNode ) || ( minCost < (float) nPrimitives ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
BVHPrimitiveInfo *pmid =
|
2021-01-02 21:05:29 +00:00
|
|
|
std::partition( &primitiveInfo[start], &primitiveInfo[end - 1] + 1,
|
|
|
|
CompareToBucket( minCostSplitBucket, nBuckets,
|
|
|
|
dim, centroidBounds ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
mid = pmid - &primitiveInfo[0];
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
wxASSERT( ( mid >= start ) && ( mid <= end ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Create leaf _BVHBuildNode_
|
|
|
|
const int firstPrimOffset = orderedPrims.size();
|
|
|
|
|
|
|
|
for( int i = start; i < end; ++i )
|
|
|
|
{
|
|
|
|
const int primitiveNr = primitiveInfo[i].primitiveNumber;
|
|
|
|
|
|
|
|
wxASSERT( primitiveNr < (int)m_primitives.size() );
|
|
|
|
|
|
|
|
orderedPrims.push_back( m_primitives[ primitiveNr ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
node->InitLeaf( firstPrimOffset, nPrimitives, bounds );
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
node->InitInterior( dim, recursiveBuild( primitiveInfo, start, mid, totalNodes,
|
|
|
|
orderedPrims ),
|
|
|
|
recursiveBuild( primitiveInfo, mid, end, totalNodes,
|
2016-07-19 17:35:25 +00:00
|
|
|
orderedPrims) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BVHBuildNode *BVH_PBRT::HLBVHBuild( const std::vector<BVHPrimitiveInfo>& primitiveInfo,
|
|
|
|
int* totalNodes, CONST_VECTOR_OBJECT& orderedPrims )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// Compute bounding box of all primitive centroids
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D bounds;
|
2016-07-19 17:35:25 +00:00
|
|
|
bounds.Reset();
|
|
|
|
|
|
|
|
for( unsigned int i = 0; i < primitiveInfo.size(); ++i )
|
|
|
|
bounds.Union( primitiveInfo[i].centroid );
|
|
|
|
|
|
|
|
// Compute Morton indices of primitives
|
|
|
|
std::vector<MortonPrimitive> mortonPrims( primitiveInfo.size() );
|
|
|
|
|
|
|
|
for( int i = 0; i < (int)primitiveInfo.size(); ++i )
|
|
|
|
{
|
|
|
|
// Initialize _mortonPrims[i]_ for _i_th primitive
|
|
|
|
const int mortonBits = 10;
|
|
|
|
const int mortonScale = 1 << mortonBits;
|
|
|
|
|
|
|
|
wxASSERT( primitiveInfo[i].primitiveNumber < (int)primitiveInfo.size() );
|
|
|
|
|
|
|
|
mortonPrims[i].primitiveIndex = primitiveInfo[i].primitiveNumber;
|
|
|
|
|
|
|
|
const SFVEC3F centroidOffset = bounds.Offset( primitiveInfo[i].centroid );
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
wxASSERT( ( centroidOffset.x >= 0.0f ) && ( centroidOffset.x <= 1.0f ) );
|
|
|
|
wxASSERT( ( centroidOffset.y >= 0.0f ) && ( centroidOffset.y <= 1.0f ) );
|
|
|
|
wxASSERT( ( centroidOffset.z >= 0.0f ) && ( centroidOffset.z <= 1.0f ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
mortonPrims[i].mortonCode = EncodeMorton3( centroidOffset * SFVEC3F( (float)mortonScale ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Radix sort primitive Morton indices
|
|
|
|
RadixSort( &mortonPrims );
|
|
|
|
|
|
|
|
// Create LBVH treelets at bottom of BVH
|
|
|
|
|
|
|
|
// Find intervals of primitives for each treelet
|
|
|
|
std::vector<LBVHTreelet> treeletsToBuild;
|
|
|
|
|
|
|
|
for( int start = 0, end = 1; end <= (int)mortonPrims.size(); ++end )
|
|
|
|
{
|
|
|
|
const uint32_t mask = 0b00111111111111000000000000000000;
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
if( ( end == (int) mortonPrims.size() )
|
|
|
|
|| ( ( mortonPrims[start].mortonCode & mask )
|
|
|
|
!= ( mortonPrims[end].mortonCode & mask ) ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// Add entry to _treeletsToBuild_ for this treelet
|
|
|
|
const int numPrimitives = end - start;
|
|
|
|
const int maxBVHNodes = 2 * numPrimitives;
|
|
|
|
|
|
|
|
// !TODO: implement a memory arena
|
2017-02-09 20:23:40 +00:00
|
|
|
BVHBuildNode *nodes = static_cast<BVHBuildNode *>( malloc( maxBVHNodes *
|
2020-12-12 17:29:11 +00:00
|
|
|
sizeof( BVHBuildNode ) ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
m_nodesToFree.push_back( nodes );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
for( int i = 0; i < maxBVHNodes; ++i )
|
|
|
|
{
|
|
|
|
nodes[i].bounds.Reset();
|
|
|
|
nodes[i].firstPrimOffset = 0;
|
|
|
|
nodes[i].nPrimitives = 0;
|
|
|
|
nodes[i].splitAxis = 0;
|
2021-01-02 21:05:29 +00:00
|
|
|
nodes[i].children[0] = nullptr;
|
|
|
|
nodes[i].children[1] = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LBVHTreelet tmpTreelet;
|
|
|
|
|
|
|
|
tmpTreelet.startIndex = start;
|
|
|
|
tmpTreelet.numPrimitives = numPrimitives;
|
|
|
|
tmpTreelet.buildNodes = nodes;
|
|
|
|
|
|
|
|
treeletsToBuild.push_back( tmpTreelet );
|
|
|
|
|
|
|
|
start = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create LBVHs for treelets in parallel
|
|
|
|
int atomicTotal = 0;
|
|
|
|
int orderedPrimsOffset = 0;
|
|
|
|
|
|
|
|
orderedPrims.resize( m_primitives.size() );
|
|
|
|
|
|
|
|
for( int index = 0; index < (int)treeletsToBuild.size(); ++index )
|
|
|
|
{
|
|
|
|
// Generate _index_th LBVH treelet
|
|
|
|
int nodesCreated = 0;
|
|
|
|
const int firstBit = 29 - 12;
|
|
|
|
|
|
|
|
LBVHTreelet &tr = treeletsToBuild[index];
|
|
|
|
|
|
|
|
wxASSERT( tr.startIndex < (int)mortonPrims.size() );
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
tr.buildNodes = emitLBVH( tr.buildNodes, primitiveInfo, &mortonPrims[tr.startIndex],
|
|
|
|
tr.numPrimitives, &nodesCreated, orderedPrims,
|
|
|
|
&orderedPrimsOffset, firstBit );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
atomicTotal += nodesCreated;
|
|
|
|
}
|
|
|
|
|
|
|
|
*totalNodes = atomicTotal;
|
|
|
|
|
|
|
|
// Initialize _finishedTreelets_ with treelet root node pointers
|
|
|
|
std::vector<BVHBuildNode *> finishedTreelets;
|
|
|
|
finishedTreelets.reserve( treeletsToBuild.size() );
|
|
|
|
|
|
|
|
for( int index = 0; index < (int)treeletsToBuild.size(); ++index )
|
|
|
|
finishedTreelets.push_back( treeletsToBuild[index].buildNodes );
|
|
|
|
|
|
|
|
// Create and return SAH BVH from LBVH treelets
|
2021-01-02 21:05:29 +00:00
|
|
|
return buildUpperSAH( finishedTreelets, 0, finishedTreelets.size(), totalNodes );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BVHBuildNode *BVH_PBRT::emitLBVH( BVHBuildNode* &buildNodes,
|
|
|
|
const std::vector<BVHPrimitiveInfo>& primitiveInfo,
|
|
|
|
MortonPrimitive* mortonPrims, int nPrimitives, int* totalNodes,
|
|
|
|
CONST_VECTOR_OBJECT& orderedPrims, int *orderedPrimsOffset,
|
|
|
|
int bit )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
wxASSERT( nPrimitives > 0 );
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( totalNodes != nullptr );
|
|
|
|
wxASSERT( orderedPrimsOffset != nullptr );
|
2016-07-19 17:35:25 +00:00
|
|
|
wxASSERT( nPrimitives > 0 );
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( mortonPrims != nullptr );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
if( ( bit == -1 ) || ( nPrimitives < m_maxPrimsInNode ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// Create and return leaf node of LBVH treelet
|
|
|
|
(*totalNodes)++;
|
|
|
|
|
|
|
|
BVHBuildNode *node = buildNodes++;
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D bounds;
|
2016-07-19 17:35:25 +00:00
|
|
|
bounds.Reset();
|
|
|
|
|
|
|
|
int firstPrimOffset = *orderedPrimsOffset;
|
|
|
|
*orderedPrimsOffset += nPrimitives;
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
wxASSERT( ( firstPrimOffset + ( nPrimitives - 1 ) ) < (int) orderedPrims.size() );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
for( int i = 0; i < nPrimitives; ++i )
|
|
|
|
{
|
|
|
|
const int primitiveIndex = mortonPrims[i].primitiveIndex;
|
|
|
|
|
|
|
|
wxASSERT( primitiveIndex < (int)m_primitives.size() );
|
|
|
|
|
|
|
|
orderedPrims[firstPrimOffset + i] = m_primitives[primitiveIndex];
|
|
|
|
bounds.Union( primitiveInfo[primitiveIndex].bounds );
|
|
|
|
}
|
|
|
|
|
|
|
|
node->InitLeaf( firstPrimOffset, nPrimitives, bounds );
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int mask = 1 << bit;
|
|
|
|
|
|
|
|
// Advance to next subtree level if there's no LBVH split for this bit
|
2020-12-12 17:29:11 +00:00
|
|
|
if( ( mortonPrims[0].mortonCode & mask ) ==
|
|
|
|
( mortonPrims[nPrimitives - 1].mortonCode & mask ) )
|
|
|
|
return emitLBVH( buildNodes, primitiveInfo, mortonPrims, nPrimitives, totalNodes,
|
2021-01-02 21:05:29 +00:00
|
|
|
orderedPrims, orderedPrimsOffset, bit - 1 );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Find LBVH split point for this dimension
|
|
|
|
int searchStart = 0;
|
|
|
|
int searchEnd = nPrimitives - 1;
|
|
|
|
|
|
|
|
while( searchStart + 1 != searchEnd )
|
|
|
|
{
|
|
|
|
wxASSERT(searchStart != searchEnd);
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
const int mid = ( searchStart + searchEnd ) / 2;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
if( ( mortonPrims[searchStart].mortonCode & mask ) ==
|
|
|
|
( mortonPrims[mid].mortonCode & mask ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
searchStart = mid;
|
|
|
|
else
|
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
wxASSERT( ( mortonPrims[mid].mortonCode & mask ) ==
|
|
|
|
( mortonPrims[searchEnd].mortonCode & mask ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
searchEnd = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const int splitOffset = searchEnd;
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
wxASSERT( splitOffset <= ( nPrimitives - 1 ) );
|
|
|
|
wxASSERT( ( mortonPrims[splitOffset - 1].mortonCode & mask )
|
|
|
|
!= ( mortonPrims[splitOffset].mortonCode & mask ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Create and return interior LBVH node
|
|
|
|
(*totalNodes)++;
|
|
|
|
|
|
|
|
BVHBuildNode *node = buildNodes++;
|
|
|
|
BVHBuildNode *lbvh[2];
|
|
|
|
|
|
|
|
lbvh[0] = emitLBVH( buildNodes, primitiveInfo, mortonPrims, splitOffset,
|
2020-12-12 17:29:11 +00:00
|
|
|
totalNodes, orderedPrims, orderedPrimsOffset, bit - 1 );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
lbvh[1] = emitLBVH( buildNodes, primitiveInfo, &mortonPrims[splitOffset],
|
2020-12-12 17:29:11 +00:00
|
|
|
nPrimitives - splitOffset, totalNodes, orderedPrims,
|
|
|
|
orderedPrimsOffset, bit - 1 );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
const int axis = bit % 3;
|
|
|
|
|
|
|
|
node->InitInterior( axis, lbvh[0], lbvh[1] );
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BVHBuildNode *BVH_PBRT::buildUpperSAH( std::vector<BVHBuildNode*>& treeletRoots, int start,
|
|
|
|
int end, int* totalNodes )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( totalNodes != nullptr );
|
2016-07-19 17:35:25 +00:00
|
|
|
wxASSERT( start < end );
|
|
|
|
wxASSERT( end <= (int)treeletRoots.size() );
|
|
|
|
|
|
|
|
int nNodes = end - start;
|
|
|
|
|
|
|
|
if( nNodes == 1 )
|
|
|
|
return treeletRoots[start];
|
|
|
|
|
|
|
|
(*totalNodes)++;
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BVHBuildNode* node = static_cast<BVHBuildNode*>( malloc( sizeof( BVHBuildNode ) ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
m_nodesToFree.push_back( node );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
node->bounds.Reset();
|
|
|
|
node->firstPrimOffset = 0;
|
|
|
|
node->nPrimitives = 0;
|
|
|
|
node->splitAxis = 0;
|
2021-01-02 21:05:29 +00:00
|
|
|
node->children[0] = nullptr;
|
|
|
|
node->children[1] = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Compute bounds of all nodes under this HLBVH node
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D bounds;
|
2016-07-19 17:35:25 +00:00
|
|
|
bounds.Reset();
|
|
|
|
|
|
|
|
for( int i = start; i < end; ++i )
|
|
|
|
bounds.Union( treeletRoots[i]->bounds );
|
|
|
|
|
|
|
|
// Compute bound of HLBVH node centroids, choose split dimension _dim_
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D centroidBounds;
|
2016-07-19 17:35:25 +00:00
|
|
|
centroidBounds.Reset();
|
|
|
|
|
|
|
|
for( int i = start; i < end; ++i )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
SFVEC3F centroid = ( treeletRoots[i]->bounds.Min() + treeletRoots[i]->bounds.Max() ) * 0.5f;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
centroidBounds.Union( centroid );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const int dim = centroidBounds.MaxDimension();
|
|
|
|
|
|
|
|
// FIXME: if this hits, what do we need to do?
|
|
|
|
// Make sure the SAH split below does something... ?
|
|
|
|
wxASSERT( centroidBounds.Max()[dim] != centroidBounds.Min()[dim] );
|
|
|
|
|
|
|
|
// Allocate _BucketInfo_ for SAH partition buckets
|
|
|
|
const int nBuckets = 12;
|
|
|
|
|
|
|
|
BucketInfo buckets[nBuckets];
|
|
|
|
|
|
|
|
for( int i = 0; i < nBuckets; ++i )
|
|
|
|
{
|
|
|
|
buckets[i].count = 0;
|
|
|
|
buckets[i].bounds.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize _BucketInfo_ for HLBVH SAH partition buckets
|
|
|
|
for( int i = start; i < end; ++i )
|
|
|
|
{
|
|
|
|
const float centroid = ( treeletRoots[i]->bounds.Min()[dim] +
|
2021-01-02 21:05:29 +00:00
|
|
|
treeletRoots[i]->bounds.Max()[dim] ) * 0.5f;
|
|
|
|
int b = nBuckets * ( ( centroid - centroidBounds.Min()[dim] )
|
|
|
|
/ ( centroidBounds.Max()[dim] - centroidBounds.Min()[dim] ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
if( b == nBuckets )
|
|
|
|
b = nBuckets - 1;
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
wxASSERT( ( b >= 0 ) && ( b < nBuckets ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
buckets[b].count++;
|
|
|
|
buckets[b].bounds.Union( treeletRoots[i]->bounds );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute costs for splitting after each bucket
|
|
|
|
float cost[nBuckets - 1];
|
|
|
|
|
|
|
|
for( int i = 0; i < nBuckets - 1; ++i )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D b0, b1;
|
2016-07-19 17:35:25 +00:00
|
|
|
b0.Reset();
|
|
|
|
b1.Reset();
|
|
|
|
|
|
|
|
int count0 = 0, count1 = 0;
|
|
|
|
|
|
|
|
for( int j = 0; j <= i; ++j )
|
|
|
|
{
|
|
|
|
if( buckets[j].count )
|
|
|
|
{
|
|
|
|
count0 += buckets[j].count;
|
|
|
|
b0.Union( buckets[j].bounds );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( int j = i + 1; j < nBuckets; ++j )
|
|
|
|
{
|
|
|
|
if( buckets[j].count )
|
|
|
|
{
|
|
|
|
count1 += buckets[j].count;
|
|
|
|
b1.Union( buckets[j].bounds );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
cost[i] = .125f + ( count0 * b0.SurfaceArea() + count1 * b1.SurfaceArea() ) /
|
2016-07-19 17:35:25 +00:00
|
|
|
bounds.SurfaceArea();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find bucket to split at that minimizes SAH metric
|
|
|
|
float minCost = cost[0];
|
|
|
|
int minCostSplitBucket = 0;
|
|
|
|
|
|
|
|
for( int i = 1; i < nBuckets - 1; ++i )
|
|
|
|
{
|
|
|
|
if( cost[i] < minCost )
|
|
|
|
{
|
|
|
|
minCost = cost[i];
|
|
|
|
minCostSplitBucket = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split nodes and create interior HLBVH SAH node
|
2021-01-02 21:05:29 +00:00
|
|
|
BVHBuildNode **pmid = std::partition( &treeletRoots[start], &treeletRoots[end - 1] + 1,
|
|
|
|
HLBVH_SAH_Evaluator( minCostSplitBucket, nBuckets,
|
|
|
|
dim, centroidBounds ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
const int mid = pmid - &treeletRoots[0];
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( ( mid > start ) && ( mid < end ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
node->InitInterior( dim,
|
|
|
|
buildUpperSAH( treeletRoots, start, mid, totalNodes ),
|
|
|
|
buildUpperSAH( treeletRoots, mid, end, totalNodes ) );
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
int BVH_PBRT::flattenBVHTree( BVHBuildNode* node, uint32_t* offset )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
LinearBVHNode *linearNode = &m_nodes[*offset];
|
|
|
|
|
|
|
|
linearNode->bounds = node->bounds;
|
|
|
|
|
|
|
|
int myOffset = (*offset)++;
|
|
|
|
|
|
|
|
if( node->nPrimitives > 0 )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( ( !node->children[0] ) && ( !node->children[1] ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
wxASSERT( node->nPrimitives < 65536 );
|
|
|
|
|
|
|
|
linearNode->primitivesOffset = node->firstPrimOffset;
|
|
|
|
linearNode->nPrimitives = node->nPrimitives;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
// Create interior flattened BVH node
|
2016-07-19 17:35:25 +00:00
|
|
|
linearNode->axis = node->splitAxis;
|
|
|
|
linearNode->nPrimitives = 0;
|
|
|
|
flattenBVHTree( node->children[0], offset );
|
|
|
|
linearNode->secondChildOffset = flattenBVHTree( node->children[1], offset );
|
|
|
|
}
|
|
|
|
|
|
|
|
return myOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_TODOS 64
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool BVH_PBRT::Intersect( const RAY& aRay, HITINFO& aHitInfo ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
if( !m_nodes )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool hit = false;
|
|
|
|
|
|
|
|
// Follow ray through BVH nodes to find primitive intersections
|
|
|
|
int todoOffset = 0, nodeNum = 0;
|
|
|
|
int todo[MAX_TODOS];
|
|
|
|
|
|
|
|
while( true )
|
|
|
|
{
|
|
|
|
const LinearBVHNode *node = &m_nodes[nodeNum];
|
|
|
|
|
|
|
|
wxASSERT( todoOffset < MAX_TODOS );
|
|
|
|
|
|
|
|
// Check ray against BVH node
|
|
|
|
float hitBox = 0.0f;
|
|
|
|
|
|
|
|
const bool hitted = node->bounds.Intersect( aRay, &hitBox );
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
if( hitted && ( hitBox < aHitInfo.m_tHit ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
if( node->nPrimitives > 0 )
|
|
|
|
{
|
|
|
|
// Intersect ray with primitives in leaf BVH node
|
|
|
|
for( int i = 0; i < node->nPrimitives; ++i )
|
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
if( m_primitives[node->primitivesOffset + i]->Intersect( aRay, aHitInfo ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
aHitInfo.m_acc_node_info = nodeNum;
|
|
|
|
hit = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Put far BVH node on _todo_ stack, advance to near node
|
|
|
|
if( aRay.m_dirIsNeg[node->axis] )
|
|
|
|
{
|
|
|
|
todo[todoOffset++] = nodeNum + 1;
|
|
|
|
nodeNum = node->secondChildOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
todo[todoOffset++] = node->secondChildOffset;
|
|
|
|
nodeNum = nodeNum + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( todoOffset == 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
nodeNum = todo[--todoOffset];
|
|
|
|
}
|
|
|
|
|
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
|
|
|
/// @todo This may be optimized
|
2021-01-02 21:05:29 +00:00
|
|
|
bool BVH_PBRT::Intersect( const RAY& aRay, HITINFO& aHitInfo, unsigned int aAccNodeInfo ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
if( !m_nodes )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool hit = false;
|
|
|
|
|
|
|
|
// Follow ray through BVH nodes to find primitive intersections
|
|
|
|
int todoOffset = 0, nodeNum = aAccNodeInfo;
|
|
|
|
int todo[MAX_TODOS];
|
|
|
|
|
|
|
|
while( true )
|
|
|
|
{
|
|
|
|
const LinearBVHNode *node = &m_nodes[nodeNum];
|
|
|
|
|
|
|
|
wxASSERT( todoOffset < MAX_TODOS );
|
|
|
|
|
|
|
|
// Check ray against BVH node
|
|
|
|
float hitBox = 0.0f;
|
|
|
|
|
|
|
|
const bool hitted = node->bounds.Intersect( aRay, &hitBox );
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
if( hitted && ( hitBox < aHitInfo.m_tHit ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
if( node->nPrimitives > 0 )
|
|
|
|
{
|
|
|
|
// Intersect ray with primitives in leaf BVH node
|
|
|
|
for( int i = 0; i < node->nPrimitives; ++i )
|
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
if( m_primitives[node->primitivesOffset + i]->Intersect( aRay, aHitInfo ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
//aHitInfo.m_acc_node_info = nodeNum;
|
|
|
|
hit = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Put far BVH node on _todo_ stack, advance to near node
|
|
|
|
if( aRay.m_dirIsNeg[node->axis] )
|
|
|
|
{
|
|
|
|
todo[todoOffset++] = nodeNum + 1;
|
|
|
|
nodeNum = node->secondChildOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
todo[todoOffset++] = node->secondChildOffset;
|
|
|
|
nodeNum = nodeNum + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( todoOffset == 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
nodeNum = todo[--todoOffset];
|
|
|
|
}
|
|
|
|
|
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool BVH_PBRT::IntersectP( const RAY& aRay, float aMaxDistance ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
if( !m_nodes )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Follow ray through BVH nodes to find primitive intersections
|
|
|
|
int todoOffset = 0, nodeNum = 0;
|
|
|
|
int todo[MAX_TODOS];
|
|
|
|
|
|
|
|
while( true )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const LinearBVHNode* node = &m_nodes[nodeNum];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
wxASSERT( todoOffset < MAX_TODOS );
|
|
|
|
|
|
|
|
// Check ray against BVH node
|
|
|
|
float hitBox = 0.0f;
|
|
|
|
|
|
|
|
const bool hitted = node->bounds.Intersect( aRay, &hitBox );
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
if( hitted && ( hitBox < aMaxDistance ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
if( node->nPrimitives > 0 )
|
|
|
|
{
|
|
|
|
// Intersect ray with primitives in leaf BVH node
|
|
|
|
for( int i = 0; i < node->nPrimitives; ++i )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const OBJECT_3D* obj = m_primitives[node->primitivesOffset + i];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
if( obj->GetMaterial()->GetCastShadows()
|
|
|
|
&& obj->IntersectP( aRay, aMaxDistance ) )
|
|
|
|
return true;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Put far BVH node on _todo_ stack, advance to near node
|
|
|
|
if( aRay.m_dirIsNeg[node->axis] )
|
|
|
|
{
|
|
|
|
todo[todoOffset++] = nodeNum + 1;
|
|
|
|
nodeNum = node->secondChildOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
todo[todoOffset++] = node->secondChildOffset;
|
|
|
|
nodeNum = nodeNum + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( todoOffset == 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
nodeNum = todo[--todoOffset];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|