Skip to content

Commit

Permalink
Double precision traversal complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbikker committed Dec 2, 2024
1 parent d54e5c5 commit 3094235
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
29 changes: 28 additions & 1 deletion tiny_bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,14 @@ static inline bvhvec3 cross( const bvhvec3& a, const bvhvec3& b )
{
return bvhvec3( a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x );
}
static inline bvhdbl3 cross( const bvhdbl3& a, const bvhdbl3& b )
{
return bvhdbl3( a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x );
}
static inline float dot( const bvhvec2& a, const bvhvec2& b ) { return a.x * b.x + a.y * b.y; }
static inline float dot( const bvhvec3& a, const bvhvec3& b ) { return a.x * b.x + a.y * b.y + a.z * b.z; }
static inline float dot( const bvhvec4& a, const bvhvec4& b ) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; }
static inline double dot( const bvhdbl3& a, const bvhdbl3& b ) { return a.x * b.x + a.y * b.y + a.z * b.z; }

// Vector math: common operations.
static float length( const bvhvec3& a ) { return sqrtf( a.x * a.x + a.y * a.y + a.z * a.z ); }
Expand Down Expand Up @@ -2548,7 +2553,29 @@ int BVH::IntersectEx_WaldDouble( RayEx& ray ) const
steps++;
if (node->isLeaf())
{
// for (unsigned i = 0; i < node->triCount; i++) IntersectTri( ray, triIdx[node->leftFirst + i] );
for (unsigned i = 0; i < node->triCount; i++)
{
const unsigned long long int idx = triIdxEx[node->leftFirst + i];
const unsigned long long int vertIdx = idx * 3;
const bvhdbl3 edge1 = vertsEx[vertIdx + 1] - vertsEx[vertIdx];
const bvhdbl3 edge2 = vertsEx[vertIdx + 2] - vertsEx[vertIdx];
const bvhdbl3 h = cross( ray.D, edge2 );
const double a = dot( edge1, h );
if (fabs( a ) < 0.0000001) continue; // ray parallel to triangle
const double f = 1 / a;
const bvhdbl3 s = ray.O - bvhdbl3( vertsEx[vertIdx] );
const double u = f * dot( s, h );
if (u < 0 || u > 1) continue;
const bvhdbl3 q = cross( s, edge1 );
const double v = f * dot( ray.D, q );
if (v < 0 || u + v > 1) continue;
const double t = f * dot( edge2, q );
if (t > 0 && t < ray.t)
{
// register a hit: ray is shortened to t
ray.t = t, ray.u = u, ray.v = v, ray.primIdx = idx;
}
}
if (stackPtr == 0) break; else node = stack[--stackPtr];
continue;
}
Expand Down
17 changes: 15 additions & 2 deletions tiny_bvh_speedtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ void ValidateTraceResult( Ray* batch, float* ref, unsigned N, unsigned line )
}
}

void ValidateTraceResultEx( RayEx* batch, float* ref, unsigned N, unsigned line )
{
float refSum = 0;
double batchSum = 0;
for (unsigned i = 0; i < N; i += 4)
refSum += ref[i] == 1e30f ? 100 : ref[i],
batchSum += batch[i].t == 1e300 ? 100 : batch[i].t;
if (fabs( refSum - (float)batchSum ) / refSum < 0.0001f) return;
fprintf( stderr, "Validation failed on line %i.\n", line );
exit( 1 );
}

int main()
{
int minor = TINY_BVH_VERSION_MINOR;
Expand Down Expand Up @@ -512,6 +524,7 @@ int main()
// double-precision Rays/BVH
printf( "- WALD_DOUBLE - primary: " );
traceTime = TestPrimaryRaysEx( BVH::WALD_DOUBLE, doubleBatch, Nsmall, 3 );
ValidateTraceResultEx( doubleBatch, refDist, Nsmall, __LINE__ );
printf( "%4.2fM rays in %5.1fms (%7.2fMRays/s)\n", (float)Nsmall * 1e-6f, traceTime * 1000, (float)Nsmall / traceTime * 1e-6f );

#endif
Expand Down Expand Up @@ -809,11 +822,11 @@ int main()

#endif

#if defined EMBREE_TRAVERSE && defined EMBREE_BUILD

// report threaded CPU performance
printf( "BVH traversal speed - EMBREE reference\n" );

#if defined EMBREE_TRAVERSE && defined EMBREE_BUILD

// trace all rays three times to estimate average performance
// - coherent, Embree, single-threaded
printf( "- Default BVH - primary: " );
Expand Down

0 comments on commit 3094235

Please sign in to comment.