Skip to content

Commit

Permalink
Refit over indexed geometry, speedtest for refit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbikker committed Jan 7, 2025
1 parent cc4483c commit dd5e4f1
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 10 deletions.
77 changes: 71 additions & 6 deletions tiny_bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ template <int M> class MBVH : public BVHBase
void BuildHQ( const bvhvec4slice& vertices );
void BuildHQ( const bvhvec4* vertices, const uint32_t* indices, const uint32_t primCount );
void BuildHQ( const bvhvec4slice& vertices, const uint32_t* indices, const uint32_t primCount );
void Refit( const uint32_t nodeIdx = 0 );
void ConvertFrom( const BVH& original );
void SplitBVHLeaf( const uint32_t nodeIdx, const uint32_t maxPrims );
// BVH data
Expand Down Expand Up @@ -1836,19 +1837,40 @@ void BVH::Refit( const uint32_t nodeIdx )
FATAL_ERROR_IF( !refittable, "BVH::Refit( .. ), refitting an SBVH." );
FATAL_ERROR_IF( bvhNode == 0, "BVH::Refit( .. ), bvhNode == 0." );
FATAL_ERROR_IF( may_have_holes, "BVH::Refit( .. ), bvh may have holes." );
FATAL_ERROR_IF( bvh_over_indices, "BVH::Refit( .. ), bvh used indexed tris." );
for (int32_t i = usedNodes - 1; i >= 0; i--)
{
BVHNode& node = bvhNode[i];
if (node.isLeaf()) // leaf: adjust to current triangle vertex positions
{
bvhvec4 aabbMin( BVH_FAR ), aabbMax( -BVH_FAR );
for (uint32_t first = node.leftFirst, j = 0; j < node.triCount; j++)
if (vertIdx)
{
for (uint32_t first = node.leftFirst, j = 0; j < node.triCount; j++)
{
const uint32_t vidx = triIdx[first + j] * 3;
const uint32_t i0 = vertIdx[vidx], i1 = vertIdx[vidx + 1], i2 = vertIdx[vidx + 2];
const bvhvec4 v0 = verts[i0], v1 = verts[i1], v2 = verts[i2];
const bvhvec4 t1 = tinybvh_min( v0, aabbMin );
const bvhvec4 t2 = tinybvh_max( v0, aabbMax );
const bvhvec4 t3 = tinybvh_min( v1, v2 );
const bvhvec4 t4 = tinybvh_max( v1, v2 );
aabbMin = tinybvh_min( t1, t3 );
aabbMax = tinybvh_max( t2, t4 );
}
}
else
{
const uint32_t vertIdx = triIdx[first + j] * 3;
aabbMin = tinybvh_min( aabbMin, verts[vertIdx] ), aabbMax = tinybvh_max( aabbMax, verts[vertIdx] );
aabbMin = tinybvh_min( aabbMin, verts[vertIdx + 1] ), aabbMax = tinybvh_max( aabbMax, verts[vertIdx + 1] );
aabbMin = tinybvh_min( aabbMin, verts[vertIdx + 2] ), aabbMax = tinybvh_max( aabbMax, verts[vertIdx + 2] );
for (uint32_t first = node.leftFirst, j = 0; j < node.triCount; j++)
{
const uint32_t vidx = triIdx[first + j] * 3;
const bvhvec4 v0 = verts[vidx], v1 = verts[vidx + 1], v2 = verts[vidx + 2];
const bvhvec4 t1 = tinybvh_min( v0, aabbMin );
const bvhvec4 t2 = tinybvh_max( v0, aabbMax );
const bvhvec4 t3 = tinybvh_min( v1, v2 );
const bvhvec4 t4 = tinybvh_max( v1, v2 );
aabbMin = tinybvh_min( t1, t3 );
aabbMax = tinybvh_max( t2, t4 );
}
}
node.aabbMin = aabbMin, node.aabbMax = aabbMax;
continue;
Expand Down Expand Up @@ -2712,6 +2734,49 @@ template<int M> void MBVH<M>::BuildHQ( const bvhvec4slice& vertices, const uint3
bvh_over_indices = true;
}

template<int M> void MBVH<M>::Refit( const uint32_t nodeIdx )
{
MBVHNode& node = mbvhNode[nodeIdx];
if (node.isLeaf())
{
bvhvec3 aabbMin( BVH_FAR ), aabbMax( -BVH_FAR );
if (bvh.vertIdx)
{
for (uint32_t first = node.firstTri, j = 0; j < node.triCount; j++)
{
const uint32_t vidx = bvh.triIdx[first + j] * 3;
const uint32_t i0 = bvh.vertIdx[vidx], i1 = bvh.vertIdx[vidx + 1], i2 = bvh.vertIdx[vidx + 2];
const bvhvec3 v0 = bvh.verts[i0], v1 = bvh.verts[i1], v2 = bvh.verts[i2];
aabbMin = tinybvh_min( aabbMin, tinybvh_min( tinybvh_min( v0, v1 ), v2 ) );
aabbMax = tinybvh_max( aabbMax, tinybvh_max( tinybvh_max( v0, v1 ), v2 ) );
}
}
else
{
for (uint32_t first = node.firstTri, j = 0; j < node.triCount; j++)
{
const uint32_t vidx = bvh.triIdx[first + j] * 3;
const bvhvec3 v0 = bvh.verts[vidx], v1 = bvh.verts[vidx + 1], v2 = bvh.verts[vidx + 2];
aabbMin = tinybvh_min( aabbMin, tinybvh_min( tinybvh_min( v0, v1 ), v2 ) );
aabbMax = tinybvh_max( aabbMax, tinybvh_max( tinybvh_max( v0, v1 ), v2 ) );
}
}
node.aabbMin = aabbMin, node.aabbMax = aabbMax;
}
else
{
for( unsigned i = 0; i < node.childCount; i++ ) Refit( node.child[i] );
MBVHNode& firstChild = mbvhNode[node.child[0]];
bvhvec3 bmin = firstChild.aabbMin, bmax = firstChild.aabbMax;
for( unsigned i = 1; i < node.childCount; i++ )
{
MBVHNode& child = mbvhNode[node.child[i]];
bmin = tinybvh_min( bmin, child.aabbMin );
bmax = tinybvh_max( bmax, child.aabbMax );
}
}
}

template<int M> void MBVH<M>::ConvertFrom( const BVH& original )
{
// get a copy of the original bvh
Expand Down
71 changes: 67 additions & 4 deletions tiny_bvh_speedtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@

// tests to perform
// #define BUILD_MIDPOINT
#define BUILD_REFERENCE
#define BUILD_DOUBLE
// #define BUILD_REFERENCE
// #define BUILD_DOUBLE
#define BUILD_AVX
#define BUILD_NEON
#define BUILD_SBVH
// #define BUILD_SBVH
#define REFIT_BVH2
#define REFIT_MBVH4
#define REFIT_MBVH8
// #define BUILD_AVX_SBVH
#define TRAVERSE_2WAY_ST
#define TRAVERSE_ALT2WAY_ST
Expand Down Expand Up @@ -58,7 +61,7 @@ using namespace tinybvh;
bvhvec4* triangles = 0;
#include <fstream>
int verts = 0;
float traceTime, buildTime, * refDist = 0, * refDistFull = 0;
float traceTime, buildTime, refitTime, * refDist = 0, * refDistFull = 0;
unsigned refOccluded[3] = {}, * refOccl[3] = {};
unsigned Nfull, Nsmall;
Ray* fullBatch[3], * smallBatch[3];
Expand Down Expand Up @@ -514,6 +517,66 @@ int main()
printf( "%7.2fms for %7i triangles ", buildTime * 1000.0f, verts / 3 );
printf( "- %6i nodes, SAH=%.2f, rayCost=%.2f\n", bvh->usedNodes, bvh->SAHCost(), avgCost );

#endif

// measure single-core bvh construction time - warming caches
printf( "BVH refitting speed\n" );

#ifdef REFIT_BVH2

// measure single-core bvh refit time
printf( "- BVH2 refitting: " );
{
BVH tmpBVH;
tmpBVH.Build( triangles, verts / 3 );
for (int pass = 0; pass < 10; pass++)
{
if (pass == 1) t.reset();
tmpBVH.Refit();
}
refitTime = t.elapsed() / 9.0f;
}
printf( "%7.2fms for %7i triangles ", refitTime * 1000.0f, verts / 3 );
printf( "- SAH=%.2f\n", bvh->SAHCost() );

#endif

#ifdef REFIT_MBVH4

// measure single-core mbvh refit time
printf( "- BVH4 refitting: " );
{
MBVH<4> tmpBVH4;
tmpBVH4.Build( triangles, verts / 3 );
for (int pass = 0; pass < 10; pass++)
{
if (pass == 1) t.reset();
tmpBVH4.Refit();
}
refitTime = t.elapsed() / 9.0f;
}
printf( "%7.2fms for %7i triangles ", refitTime * 1000.0f, verts / 3 );
printf( "- SAH=%.2f\n", bvh->SAHCost() );

#endif

#ifdef REFIT_MBVH8

// measure single-core mbvh refit time
printf( "- BVH8 refitting: " );
{
MBVH<8> tmpBVH8;
tmpBVH8.Build( triangles, verts / 3 );
for (int pass = 0; pass < 10; pass++)
{
if (pass == 1) t.reset();
tmpBVH8.Refit();
}
refitTime = t.elapsed() / 9.0f;
}
printf( "%7.2fms for %7i triangles ", refitTime * 1000.0f, verts / 3 );
printf( "- SAH=%.2f\n", bvh->SAHCost() );

#endif

#if defined BUILD_AVX_SBVH && defined BVH_USEAVX
Expand Down

0 comments on commit dd5e4f1

Please sign in to comment.