From e20185bf4d6d85e548086c19a8a59faa7d41f1d9 Mon Sep 17 00:00:00 2001 From: Jacco Bikker Date: Thu, 23 Jan 2025 09:10:40 +0100 Subject: [PATCH] triIdx array renamed to (the more generic) 'primIdx'. --- tiny_bvh_anim.cpp | 2 +- tiny_bvh_custom.cpp | 34 +++++++++++++--------------------- tiny_bvh_gpu2.cpp | 2 +- tiny_bvh_speedtest.cpp | 2 +- tmpl8/game.cpp | 2 +- 5 files changed, 17 insertions(+), 25 deletions(-) diff --git a/tiny_bvh_anim.cpp b/tiny_bvh_anim.cpp index 4179c4f..312f783 100644 --- a/tiny_bvh_anim.cpp +++ b/tiny_bvh_anim.cpp @@ -54,7 +54,7 @@ void Init() inst[b].transform[7] = (float)y * 0.2f - GRIDSIZE * 0.1f + 7; inst[b].transform[11] = (float)z * 0.2f - GRIDSIZE * 0.1f - 1; } - tlas.Build( inst, 1 + INSTCOUNT, bvhList, 2 ); + tlas.Build( inst, 1 + INSTCOUNT, bvhList, 2 ); // just move build to Tick if instance transforms are not static. } bool UpdateCamera( float delta_time_s, fenster& f ) diff --git a/tiny_bvh_custom.cpp b/tiny_bvh_custom.cpp index a5983d1..f66234f 100644 --- a/tiny_bvh_custom.cpp +++ b/tiny_bvh_custom.cpp @@ -26,7 +26,7 @@ static bvhvec3 eye( -15.24f, 21.5f, 2.54f ), p1, p2, p3; static bvhvec3 view = tinybvh_normalize( bvhvec3( 0.826f, -0.438f, -0.356f ) ); // callback for custom geometry: ray/sphere intersection -void sphereIntersect( tinybvh::Ray& ray, unsigned primID ) +void sphereIntersect( tinybvh::Ray& ray, const unsigned primID ) { bvhvec3 oc = ray.O - spheres[primID].pos; float b = tinybvh_dot( oc, ray.D ); @@ -39,7 +39,7 @@ void sphereIntersect( tinybvh::Ray& ray, unsigned primID ) if (hit) ray.hit.t = t, ray.hit.prim = primID; } -bool sphereIsOccluded( const tinybvh::Ray& ray, unsigned primID ) +bool sphereIsOccluded( const tinybvh::Ray& ray, const unsigned primID ) { bvhvec3 oc = ray.O - spheres[primID].pos; float b = tinybvh_dot( oc, ray.D ); @@ -51,6 +51,12 @@ bool sphereIsOccluded( const tinybvh::Ray& ray, unsigned primID ) return t < ray.hit.t && t > 0; } +void sphereAABB( const unsigned primID, bvhvec3& boundsMin, bvhvec3& boundsMax ) +{ + boundsMin = spheres[primID].pos - bvhvec3( spheres[primID].r ); + boundsMax = spheres[primID].pos + bvhvec3( spheres[primID].r ); +} + void Init() { // load raw vertex data for Crytek's Sponza @@ -66,25 +72,13 @@ void Init() spheres = new Sphere[verts / 3]; for (int i = 0; i < verts / 3; i++) { - bvhvec3 v0 = triangles[i * 3 + 0]; - bvhvec3 v1 = triangles[i * 3 + 1]; - bvhvec3 v2 = triangles[i * 3 + 2]; - spheres[i].r = tinybvh_min( 0.05f, tinybvh_min( tinybvh_length( v1 - v0 ), tinybvh_length( v2 - v0 ) ) ); + bvhvec3 v0 = triangles[i * 3], v1 = triangles[i * 3 + 1], v2 = triangles[i * 3 + 2]; + spheres[i].r = tinybvh_min( 0.35f, 0.25f * tinybvh_min( tinybvh_length( v1 - v0 ), tinybvh_length( v2 - v0 ) ) ); spheres[i].pos = (v0 + v1 + v2) * 0.33333f; } - // abuse the triangle array to hold sphere bounding boxes - for (int i = 0; i < verts / 3; i++) - { - bvhvec3 aabbMin = spheres[i].pos - bvhvec3( spheres[i].r ); - bvhvec3 aabbMax = spheres[i].pos + bvhvec3( spheres[i].r ); - triangles[i * 3 + 0] = aabbMin; - triangles[i * 3 + 1] = (aabbMax + aabbMin) * 0.5f; - triangles[i * 3 + 2] = aabbMax; // so, a degenerate tri: just a diagonal line. - } - // build the BVH over the aabbs - bvh.Build( triangles, verts / 3 ); + bvh.Build( &sphereAABB, verts / 3 ); // set custom intersection callbacks bvh.customIntersect = &sphereIntersect; @@ -138,10 +132,8 @@ void Tick( float delta_time_s, fenster& f, uint32_t* buf ) if (ray.hit.t < 10000) { int pixel_x = tx * 4 + x, pixel_y = ty * 4 + y, primIdx = ray.hit.prim; - bvhvec3 v0 = triangles[primIdx * 3]; - bvhvec3 v1 = triangles[primIdx * 3 + 1]; - bvhvec3 v2 = triangles[primIdx * 3 + 2]; - bvhvec3 N = tinybvh_normalize( tinybvh_cross( v1 - v0, v2 - v0 ) ); + bvhvec3 I = ray.O + ray.hit.t * ray.D; + bvhvec3 N = tinybvh_normalize( I - spheres[primIdx].pos ); int c = (int)(255.9f * fabs( tinybvh_dot( N, L ) )); buf[pixel_x + pixel_y * SCRWIDTH] = c + (c << 8) + (c << 16); } diff --git a/tiny_bvh_gpu2.cpp b/tiny_bvh_gpu2.cpp index 199a210..533c9f3 100644 --- a/tiny_bvh_gpu2.cpp +++ b/tiny_bvh_gpu2.cpp @@ -231,7 +231,7 @@ void Init() // create OpenCL buffers for BVH data tlasNodes = new Buffer( tlas.allocatedNodes /* could change! */ * sizeof( BVH_GPU::BVHNode ), tlas.bvhNode ); - tlasIndices = new Buffer( tlas.bvh.idxCount * sizeof( uint32_t ), tlas.bvh.triIdx ); + tlasIndices = new Buffer( tlas.bvh.idxCount * sizeof( uint32_t ), tlas.bvh.primIdx ); tlasNodes->CopyToDevice(); tlasIndices->CopyToDevice(); blasInstances = new Buffer( (DRAGONS + 1) * sizeof( BLASInstance ), instance ); diff --git a/tiny_bvh_speedtest.cpp b/tiny_bvh_speedtest.cpp index a2f5fe7..4ec8a97 100644 --- a/tiny_bvh_speedtest.cpp +++ b/tiny_bvh_speedtest.cpp @@ -821,7 +821,7 @@ int main() } // create OpenCL buffers for the BVH data calculated by tiny_bvh.h tinyocl::Buffer gpuNodes( bvh_gpu->usedNodes * sizeof( BVH_GPU::BVHNode ), bvh_gpu->bvhNode ); - tinyocl::Buffer idxData( bvh_gpu->idxCount * sizeof( unsigned ), bvh_gpu->bvh.triIdx ); + tinyocl::Buffer idxData( bvh_gpu->idxCount * sizeof( unsigned ), bvh_gpu->bvh.primIdx ); tinyocl::Buffer triData( bvh_gpu->triCount * 3 * sizeof( tinybvh::bvhvec4 ), triangles ); // synchronize the host-side data to the gpu side gpuNodes.CopyToDevice(); diff --git a/tmpl8/game.cpp b/tmpl8/game.cpp index bdc0309..7e28f70 100644 --- a/tmpl8/game.cpp +++ b/tmpl8/game.cpp @@ -77,7 +77,7 @@ void Game::Init() // create OpenCL buffers for BVH data tlasNodes = new Buffer( tlas.allocatedNodes /* could change! */ * sizeof( BVH_GPU::BVHNode ), tlas.bvhNode ); - tlasIndices = new Buffer( tlas.bvh.idxCount * sizeof( uint32_t ), tlas.bvh.triIdx ); + tlasIndices = new Buffer( tlas.bvh.idxCount * sizeof( uint32_t ), tlas.bvh.primIdx ); tlasNodes->CopyToDevice(); tlasIndices->CopyToDevice(); blasInstances = new Buffer( (DRAGONS + 1) * sizeof( BLASInstance ), instance );