CUDA Path Tracer
Monte Carlo path tracing on the GPU, with a BVH and OptiX denoising
CUDA, GLSL, C++ · Sep – Oct 2025
- 3–160× frame rate from the BVH in complex scenes
- +24–67% from stream compaction
- +6–24% from Russian roulette
A physically based Monte Carlo path tracer in CUDA. It renders globally illuminated images of custom scenes: glTF models with their own materials and textures, under HDR environment maps. It grew from the CIS 5650 base code into everything below.
Materials
- Diffuse: Lambertian, sampled from a cosine-weighted hemisphere, so the BRDF and the PDF cancel and a bounce is just a multiply by the albedo.
- Mirror and glass: perfect specular reflection, and refraction by Snell's law, with Schlick's Fresnel choosing between reflecting and refracting, and total internal reflection.
- PBR: a Cook–Torrance BRDF in the metallic-roughness workflow, with the GGX (Trowbridge–Reitz) distribution, Smith masking and shadowing, Schlick Fresnel, transparency, and energy conserved between the diffuse and specular lobes.
- Subsurface scattering for translucent materials like jade, wax and skin: scattering radius and color per channel, anisotropy, and attenuation along diffusion profiles.


Light and sampling
- Multiple importance sampling of three strategies, the area lights, the BRDF and the environment map, weighted by the power heuristic, in both the diffuse and the PBR shaders.
- HDR environment maps for image-based lighting, importance-sampled from precomputed CDFs.
- Anti-aliasing by jittering: each pixel is split into a grid, 2×2 by default, the iterations cycle through its cells, and each ray is jittered within its cell.
- NVIDIA's OptiX 9.0 denoiser, guided by albedo and normal buffers, with its blend and interval adjustable live: a clean preview in seconds instead of minutes.


Geometry
- glTF 2.0 through tinygltf, both
.gltfand.glb: meshes, transforms, and metallic-roughness materials with their base color, normal and metallic-roughness textures. - A bounding volume hierarchy, built on the CPU by the surface area heuristic into a GPU-friendly layout, and traversed iteratively with a stack.


Keeping the GPU busy
Each bounce runs as kernels over every live path: intersection, then shading. Three optimizations work on that loop. All the timings are on an RTX 5080 with an i9-13900H.
- Stream compaction. Paths that miss everything or run out of bounces are gathered, then removed with
thrust::remove_if, so each later bounce launches only for the paths still alive. - Russian roulette from bounce 3: a path survives with a probability set by its throughput, kept within bounds, and survivors are divided by it, so the estimate stays unbiased.
- Material sorting.
thrust::sort_by_keygroups paths by material before shading, so the threads of a warp run the same code.
| Trace depth | With compaction | Without | Time saved |
|---|---|---|---|
| 4 | 26 ms | 29 ms | 10% |
| 8 | 32 ms | 42 ms | 24% |
| 12 | 34 ms | 57 ms | 40% |
| 16 | 34 ms | 68 ms | 50% |
| 24 | 37 ms | 94 ms | 61% |
| 32 | 39 ms | 118 ms | 67% |
| Trace depth | No roulette | From half depth | From a quarter |
|---|---|---|---|
| 8 | 38 ms | 32 ms | 30 ms |
| 12 | 42 ms | 36 ms | 32 ms |
| 16 | 45 ms | 40 ms | 35 ms |
| 24 | 48 ms | 45 ms | 39 ms |
| 32 | 50 ms | 48 ms | 44 ms |
| Scene | Triangles | With BVH | Without | Speedup |
|---|---|---|---|---|
| Duck | 4K | 17 ms | 70 ms | 4.1× |
| Spartan armor | 42K | 30 ms | 765 ms | 25.5× |
| Dragon | 134K | 42 ms | 2,846 ms | 67.8× |
| Dodge Challenger | 196K | 27 ms | 3,023 ms | 112× |
| Porsche | 241K | 25 ms | 3,604 ms | 144× |
| Chess set | 1.5M | 270 ms | 43,343 ms | 160× |
| Scene | Materials | Sorted | Unsorted |
|---|---|---|---|
| Duck | 3 | 17 ms | 15 ms |
| Dragon | 6 | 42 ms | 41 ms |
| Spartan armor | 13 | 29 ms | 28 ms |
| Porsche | 16 | 26 ms | 22 ms |
| Dodge Challenger | 23 | 27 ms | 23 ms |
| Chess set | 18 | 267 ms | 278 ms |
MATERIAL_SORTING.Tools
An ImGui panel shows live statistics (frame rate, rays per second, iterations, active paths and per-kernel timings), orbits, pans and zooms the camera, loads another scene from its JSON file without a restart, drives the denoiser, and saves the image as a PNG.