A CUDA implementation of the Ray Tracing in One Weekend Book Series with per-pixel RNG, thin-lens depth of field, metal/dielectric materials, procedural textures,
quads, instancing, light sources, Cornell box, texture mapping, and the randomized final scene. The code mirrors the books’ progression, adapted for GPU kernels and
CUDA memory/launch patterns.
References
• Peter Shirley, Ray Tracing in One Weekend Book Series — https://raytracing.github.io/
• NVIDIA Developer Blog, “Accelerated Ray Tracing in One Weekend in CUDA” — https://developer.nvidia.com/blog/accelerated-ray-tracing-cuda/
-
Antialiasing via stochastic supersampling (per-pixel jitter using cuRAND)
-
Gamma correction with configurable values
- Metal with fuzz parameter for rough/microfacet reflections
- Lambertian (diffuse) with random cosine-ish scattering
- Dielectric (glass): refraction, total internal reflection, and Schlick reflectance
- Thin-lens defocus blur (depth of field) (aperture + focus distance)
- Proper camera basis (
u/v/w) with configurable FOV and aspect ratio
- Checkered planes and spheres
- Perlin noise textures with turbulence & marble patterns
- Shutter interval sampling: each ray carries a randomized time in
[time0, time1] - Animated primitives: object positions are interpolated across the shutter window
- Produces natural blur trails when geometry moves during exposure
- Emissive materials for area lights
- Example: Cornell box ceiling quad + glowing sphere
- General quad primitive
- Used for walls, floors, ceilings, and light sources
- Translate / rotate geometry without duplicating vertex data
- Used to place rotated blocks in the Cornell Box
- Spherical coordinate texture mapping
- Example: Earth texture on a sphere
- Windows with Visual Studio 2022 (MSVC toolset)
- CUDA Toolkit 12+ (13.x tested)
- CMake 3.24+
- NVIDIA GPU with supported compute capability (current flags target Ada 8.9; adjust for your GPU)
If you target a different architecture, set
CMAKE_CUDA_ARCHITECTURES(or-gencodein NVCC flags) appropriately inCMakeLists.txt.
A helper script automates clean, configure, build, and run. It will prompt for an output name and append .ppm:
setup.batrmdir /s /q build
cmake -S . -B build
cmake --build build --config Debug
Build\\bin\\Debug
RayTracer.exe > output.ppm-
The renderer writes a PPM (P3) image to
stdout. -
Run after building (Debug example):
Build\\bin\\Debug rayTracer.exe > output.ppm -
Or use the helper script (prompts for a filename and appends
.ppm):setup.bat
-
View/convert the PPM:
- ImageMagick:
magick convert output.ppm output.png - GIMP/Photoshop: open
output.ppmdirectly
- ImageMagick:
Scene parameters (resolution
nx/ny, samplesns, camera, aperture, focus distance, and scene generator) live insrc/main.cu.
- Bounded depth instead of recursion: the book’s recursive
color()is turned into a loop (default max depth = 50) to avoid device stack overflows. - Per-pixel RNG: Each thread has a
curandState. We copy the state to a local variable, sample multiple times, then write it back. - Unified memory for the framebuffer (
cudaMallocManaged) to simplify host readout (stdout→ PPM). - Device-side scene build: A small kernel constructs the world and camera once, then the main render kernel traces rays.
- Thin-lens DOF:
random_in_unit_disksamples the aperture;lower_left_corner,horizontal, andverticalare scaled by the focus distance;lens_radius = aperture/2.
- Samples per pixel (spp): higher
ns→ cleaner images (time ∝ spp). - Resolution: increase
nx/nyfor detail. - Max depth: 50 is a good default; raising it gives diminishing returns.
- Aperture: small (
0.1) = subtle blur; large (2.0) = strong DOF (needs more spp). - Build config: use Release for speed; set
CMAKE_CUDA_ARCHITECTURESto match your GPU.
- Peter Shirley et al. for the Ray Tracing in One Weekend series.
- NVIDIA Developer Blog for the CUDA adaptation guidance.











