Skip to content

Optimize LBVH Construction using Bottom-up Build#230

Merged
zfergus merged 4 commits intomainfrom
apetrei-lbvh
May 1, 2026
Merged

Optimize LBVH Construction using Bottom-up Build#230
zfergus merged 4 commits intomainfrom
apetrei-lbvh

Conversation

@zfergus
Copy link
Copy Markdown
Member

@zfergus zfergus commented May 1, 2026

Description

  • Replace top-down split search with a single bottom-up pass (Apetrei 2014) to build the hierarchy and bounding boxes simultaneously
  • Update ConstructionInfo to track range endpoints instead of parent indices
  • Add Catch2 benchmarks for LBVH::build performance testing

Fixes # (issue)
lbvh_build_benchmark

The single-pass bottom-up construction of Apetrei [2014] is up to 10% faster than the two-pass construction of Karras [2012]. Benchmarked on Apple M3 Pro with 11 cores.

Type of change

  • Enhancement (non-breaking change which improves existing functionality)

- Replace top-down split search with a single bottom-up pass
  (Apetrei 2014) to build the hierarchy and bounding boxes
  simultaneously
- Update `ConstructionInfo` to track range endpoints instead of parent
  indices
- Add Catch2 benchmarks for `LBVH::build` performance testing
@zfergus zfergus changed the title Optimize LBVH construction using bottom-up build Optimize LBVH Construction using Bottom-up Build May 1, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.84%. Comparing base (e400bb0) to head (abaf7f6).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #230      +/-   ##
==========================================
- Coverage   95.84%   95.84%   -0.01%     
==========================================
  Files         160      160              
  Lines       16611    16589      -22     
  Branches      923      918       -5     
==========================================
- Hits        15921    15899      -22     
  Misses        690      690              
Flag Coverage Δ
unittests 95.84% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zfergus zfergus marked this pull request as ready for review May 1, 2026 15:26
Copilot AI review requested due to automatic review settings May 1, 2026 15:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR optimizes LBVH construction by switching from the prior top-down split search / two-pass approach to a single-pass bottom-up build (Apetrei 2014), while also adding benchmarks to measure LBVH::build performance.

Changes:

  • Replaced hierarchy construction with a bottom-up, single-pass algorithm that builds internal nodes and bounding boxes simultaneously.
  • Updated ConstructionInfo to track subtree key-range endpoints (left/right) instead of parent indices.
  • Added Catch2 benchmark coverage for LBVH::build across several meshes/scenes.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
tests/src/tests/broad_phase/test_lbvh.cpp Adds Catch2 benchmarks to measure LBVH::build performance on multiple scenes.
src/ipc/broad_phase/lbvh.hpp Updates construction metadata to store subtree range endpoints used by bottom-up build.
src/ipc/broad_phase/lbvh.cpp Implements the Apetrei 2014 bottom-up build, including root swapping to preserve traversal assumptions.
Comments suppressed due to low confidence (1)

src/ipc/broad_phase/lbvh.cpp:269

  • std::atomic::operator++ defaults to memory_order_seq_cst, which is typically more expensive than needed for this arrival gate in a performance-critical build. Consider switching to fetch_add(1, std::memory_order_acq_rel) (or equivalent) so (1) the memory ordering is explicit for correctness of publishing the child pointer/range/AABB data, and (2) you avoid the stronger global ordering constraints of seq_cst.
                if (info.visitation_count++ == 0) {
                    // this is the first thread that arrived at this
                    // node -> finished
                    break;
                }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/ipc/broad_phase/lbvh.cpp Outdated
Comment thread src/ipc/broad_phase/lbvh.cpp
Comment thread src/ipc/broad_phase/lbvh.cpp Outdated
Comment thread src/ipc/broad_phase/lbvh.cpp
Comment thread src/ipc/broad_phase/lbvh.cpp
Comment thread src/ipc/broad_phase/lbvh.cpp Outdated
- Change `root_idx` to `std::atomic<int>` to prevent data races during
  parallel initialization
- Safely assign the root node using `compare_exchange_strong`
- Clarify `code_i` parameter in `delta` function documentation
@zfergus zfergus merged commit d37adba into main May 1, 2026
21 checks passed
@zfergus zfergus deleted the apetrei-lbvh branch May 1, 2026 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants