Skip to content

Performance Speed Up (~5x): Implement adaptive step raytracing & stabilize N-body physics - #45

Open
Quercius wants to merge 2 commits into
kavan010:mainfrom
Quercius:perf/geodesic-compute-opt
Open

Performance Speed Up (~5x): Implement adaptive step raytracing & stabilize N-body physics #45
Quercius wants to merge 2 commits into
kavan010:mainfrom
Quercius:perf/geodesic-compute-opt

Conversation

@Quercius

@Quercius Quercius commented Jul 2, 2026

Copy link
Copy Markdown

Overview

This PR significantly improves the overall performance of the simulation. By replacing the fixed-step integration in the compute shader with an adaptive approach and fixing the $O(N^2)$ gravity hot loop on the CPU, the rendering time has been cut by ~79%, yielding a ~4.7x speedup.

Key Changes

1. GPU Compute Shader (geodesic.comp)

  • Adaptive Step Size: Replaced the brute-force, fixed D_LAMBDA step with a dynamically scaled step (current_dL). Rays far from the black hole take massive leaps, clamping to high-precision microscopic values (MIN_STEP) only when approaching extreme curvature.
  • Smart Early Exit: Reduced ESCAPE_R to a realistic bound (1e12) and implemented an early exit for escaping rays (ray.r > ESCAPE_R && ray.dr > 0.0). This instantly kills rays heading into deep space, massively reducing Warp/Thread divergence.

2. CPU Physics Engine (black_hole.cpp)

  • Fixed N-body Integration: The previous logic updated object positions during the nested distance calculation, leading to compounding mathematical errors. Forces are now properly accumulated into a totalAccel vector before applying velocities/positions.
  • Delta Time (dt): Physics updates are now scaled by delta time, making the simulation framerate-independent.
  • Hot Loop Cleanup: Removed expensive heap allocations (std::vector) and blocking I/O (std::cout) from the gravity hot loop, replacing them with stack-allocated glm::dvec3.

Performance & Benchmarks

Based on a 60-second weighted average sampling:

  • Before: ~335 ms/frame (~3 FPS)
  • After: ~71 ms/frame (~14 FPS)

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.

1 participant