Skip to content

Commit

Permalink
triIdx array renamed to (the more generic) 'primIdx'.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbikker committed Jan 23, 2025
1 parent 5c173ee commit e20185b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 25 deletions.
2 changes: 1 addition & 1 deletion tiny_bvh_anim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
34 changes: 13 additions & 21 deletions tiny_bvh_custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion tiny_bvh_gpu2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion tiny_bvh_speedtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tmpl8/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit e20185b

Please sign in to comment.