Skip to content

Commit

Permalink
[Data/BoundingVolumeHierarchy] Querying a leaf checks the triangle first
Browse files Browse the repository at this point in the history
- It allows avoiding doing useless operations further, since we know it can't hit anything

- If the node is a leaf, the triangle is directly tested instead of first testing the AABB
  - Performance-wise, it presumably is either the same or marginally more efficient, as there's supposedly a high probability of hitting the triangle when reaching a leaf
  - The ray-AABB check can fill the hit structure with irrelevant data

- These changes allow for a speedup of ~10%
  • Loading branch information
Razakhel committed Sep 14, 2024
1 parent c242a21 commit 0895474
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/RaZ/Data/BoundingVolumeHierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ Entity* BoundingVolumeHierarchyNode::query(const Ray& ray, RayHit* hit) const {
// The following call can produce way too many zones, *drastically* increasing the profiling time & memory consumption
//ZoneScopedN("BoundingVolumeHierarchyNode::query");

if (!ray.intersects(m_boundingBox, hit))
if (isLeaf()) {
if (ray.intersects(m_triangleInfo.triangle, hit))
return m_triangleInfo.entity;

return nullptr;
}

if (isLeaf() && ray.intersects(m_triangleInfo.triangle, hit))
return m_triangleInfo.entity;
if (!ray.intersects(m_boundingBox, hit))
return nullptr;

RayHit leftHit;
RayHit rightHit;
Expand Down

0 comments on commit 0895474

Please sign in to comment.