Skip to content

Commit

Permalink
Fix upper bound for BVH8 conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbikker committed Nov 25, 2024
1 parent 9e6e996 commit 067f376
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
11 changes: 7 additions & 4 deletions tiny_bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ THE SOFTWARE.
#define BVHBINS 8

// SAH BVH building: Heuristic parameters
// CPU builds: C_INT = 1, C_TRAV = 0.01 seems optimal.
// CPU builds: C_INT = 1, C_TRAV = 1 seems optimal.
#define C_INT 1
#define C_TRAV 0.01f
#define C_TRAV 1

// include fast AVX BVH builder
#if defined(__x86_64__) || defined(_M_X64) || defined(__wasm_simd128__) || defined(__wasm_relaxed_simd__)
Expand Down Expand Up @@ -1421,7 +1421,10 @@ void BVH::Convert( const BVHLayout from, const BVHLayout to, const bool deleteOr
else if (from == WALD_32BYTE && to == BASIC_BVH8)
{
// allocate space
const unsigned spaceNeeded = usedBVHNodes;
// Note: The safe upper bound here is usedBVHNodes when converting an existing
// BVH2, but we need triCount * 2 to be safe in later conversions, e.g. to
// CWBVH, which may further split some leaf nodes.
const unsigned spaceNeeded = triCount * 2;
if (allocatedBVH8Nodes < spaceNeeded)
{
FATAL_ERROR_IF( bvhNode == 0, "BVH::Convert( WALD_32BYTE, BASIC_BVH8 ), bvhNode == 0." );
Expand Down Expand Up @@ -1486,7 +1489,7 @@ void BVH::Convert( const BVHLayout from, const BVHLayout to, const bool deleteOr
// Note: This can be far lower (specifically: usedBVH8Nodes) if we know that
// none of the BVH8 leafs has more than three primitives.
// Without this guarantee, the only safe upper limit is triCount * 2, since
// we will be splitting fat leafs to as we go.
// we will be splitting fat BVH8 leafs to as we go.
unsigned spaceNeeded = triCount * 2 * 5; // CWBVH nodes use 80 bytes each.
if (spaceNeeded > allocatedCWBVHBlocks)
{
Expand Down
4 changes: 2 additions & 2 deletions tiny_bvh_fenster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void Init()

// build a BVH over the scene
#if defined(BVH_USEAVX)
bvh.BuildHQ( triangles, verts / 3 );
bvh.BuildHQ( triangles, verts / 3 );
bvh.Convert( BVH::WALD_32BYTE, BVH::BASIC_BVH8 );
bvh.Convert( BVH::BASIC_BVH8, BVH::CWBVH );
#elif defined(BVH_USENEON)
Expand All @@ -133,7 +133,7 @@ void Tick( uint32_t* buf )
bvhvec3 right = normalize( cross( bvhvec3( 0, 1, 0 ), view ) );
bvhvec3 up = 0.8f * cross( view, right ), C = eye + 2 * view;
bvhvec3 p1 = C - right + up, p2 = C + right + up, p3 = C - right - up;
for( int i = 0; i < SCRWIDTH * SCRHEIGHT; i++ ) buf[i] = 0xff00ff; // purple
for (int i = 0; i < SCRWIDTH * SCRHEIGHT; i++) buf[i] = 0xff00ff; // purple

#ifdef _WIN32
// on windows, we get camera controls.
Expand Down
2 changes: 1 addition & 1 deletion tiny_bvh_speedtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#define BUILD_REFERENCE
#define BUILD_AVX
#define BUILD_NEON
#define BUILD_SBVH
// #define BUILD_SBVH
#define TRAVERSE_2WAY_ST
#define TRAVERSE_ALT2WAY_ST
#define TRAVERSE_SOA2WAY_ST
Expand Down

0 comments on commit 067f376

Please sign in to comment.