I developed a full CPU-based relativistic ray tracer capable of rendering how light behaves around a Schwarzschild black hole. The system traces each pixel by numerically integrating the exact light paths (geodesics) predicted by general relativity, rather than approximating the effect with simple gravitational forces.
Multithreaded Tiled Renderer
To achieve good performance on the CPU, the image is divided into 32×32 pixel tiles.
A thread pool handles rendering, and each worker thread uses a shared atomic counter to fetch the next available tile dynamically. This guarantees:
- perfect load balancing
- no thread idling
- very low synchronization overhead
- consistent performance even when some tiles require far more computation than others (e.g., rays near the black hole)
This approach keeps all CPU cores fully utilized throughout the frame.
Geodesic-Based Ray Tracing
Instead of straight rays, each pixel generates a ray whose path is governed by spacetime curvature. For a Schwarzschild black hole, this curvature is described using spherical-like coordinates around the black hole.
For every ray:
- A local 2D plane is constructed based on the camera position and ray direction.
- The ray is expressed in curved-space coordinates
- The ray’s trajectory through spacetime is integrated using a Runge–Kutta 4 solver (RK4).
- At each RK4 step, the current coordinates are mapped back to 3D to determine the ray direction and sample the background environment.
Adaptive Step Sizes and Fast Early Exits
To optimize performance:
- Adaptive step sizes allow large RK4 steps when the ray is far from the black hole, and smaller steps near the photon sphere where curvature is extreme.
- Rays that clearly escape are detected early and immediately sample the background sky.
- Rays that fall below the Schwarzschild radius are terminated and contribute to the black hole shadow.
This significantly reduces the number of RK4 iterations while preserving accuracy where it matters most.
Physical Visual Result
The final output shows:
- Achieved 11fps at 1400×800 resolution
- the black hole shadow, larger than the horizon due to gravitational lensing
- the photon ring, where light nearly orbits the black hole
- the distorted background, such as stars or nebulae bent around the gravitational field
All of this arises naturally from the geodesic integration—no shaders or fake effects.