Skip to content

Multithreaded CPU path tracer in C++ (Walnut/Vulkan) with progressive accumulation, interactive ImGui scene editing, and ~31 ms per-frame on the reference scene.

License

Notifications You must be signed in to change notification settings

Bugitoy/Cherno-Raytracing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ray Tracing

A compact path-tracing playground built on Walnut and Vulkan.

Architecture Overview

  • Application Layer (RayTracing/src/WalnutApp.cpp):

    • Hosts an ImGui-driven UI with panels for Settings, Scene, and the Viewport.
    • Owns Renderer, Camera, and Scene instances.
    • Maintains a dynamic render loop; measures per-frame time and displays "Last render: X.XXX ms".
  • Renderer (RayTracing/src/Renderer.h, RayTracing/src/Renderer.cpp):

    • CPU path tracer that renders into a Walnut::Image RGBA buffer.
    • Supports progressive accumulation across frames (Settings.Accumulate).
    • Implements per-pixel ray generation and a simple integrator with up to 5 bounces.
    • Performs closest-hit intersection against an array of spheres; returns miss when no hit.
    • Multithreaded per-row and per-column loops using C++17 parallel algorithms (<execution>), converting HDR color to 8-bit RGBA.
  • Camera (RayTracing/src/Camera.h, RayTracing/src/Camera.cpp):

    • Perspective camera with FOV/near/far controls, inverse matrices cached.
    • Generates and caches world-space primary ray directions per pixel when the viewport changes.
    • First-person controls (W/A/S/D, Q/E, mouse look with RMB), with movement and rotation speed tuning.
  • Scene & Materials (RayTracing/src/Scene.h):

    • Minimal scene graph with Spheres and Materials arrays.
    • Material exposes Albedo, Roughness, Metallic, and emissive color/power; emission contributes additively.
  • Rays (RayTracing/src/Ray.h):

    • Simple ray structure with Origin and Direction.
  • Runtime/UI:

    • ImGui panels expose renderer settings (accumulation toggle, reset), sphere transforms/material selection, and material parameter controls.
    • Viewport panel displays the current Walnut::Image via descriptor set.

Rendering Pipeline (High Level)

  1. Camera caches per-pixel primary ray directions using inverse projection and view matrices.
  2. Renderer walks each pixel, spawns a ray from camera position along the cached direction.
  3. TraceRay finds the closest sphere by solving the quadratic analytically; returns hit/miss payload.
  4. On hit, the integrator accumulates emission and multiplies contribution by material albedo.
  5. The new ray origin is offset by the surface normal; the next direction is cosine-like by normal + random unit vector.
  6. Accumulated color is averaged across frames for denoising-like convergence when accumulation is enabled.

Features

  • Progressive Accumulation: Temporal averaging for smoother results when the camera/scene is static.
  • Stochastic Bounces: Up to 5 bounces with random hemisphere sampling for indirect light.
  • Analytic Sphere Intersections: Fast, robust closest-hit with quadratic discriminant test.
  • Multithreaded CPU Path: Uses parallel std::for_each to saturate CPU cores on per-pixel work.
  • Interactive Editing: Live ImGui editing of sphere transforms and material properties.
  • Viewport-Driven Resolution: Rendering and ray cache resize to match the UI viewport dimensions.

Performance Notes

  • The renderer is CPU-bound and multithreaded. Performance scales with core count and clock speed.
  • Progressive accumulation means the first frame is often the slowest; subsequent frames reuse and average results.
  • In the reference setup from the series, the last render reported by the on-screen timer is around 31 ms per frame (dependent on resolution, scene complexity, and hardware).

Build and Run

Officially supports Windows 10/11 and Visual Studio 2022. Requires the Vulkan SDK.

  1. Install the Vulkan SDK: https://vulkan.lunarg.com/
  2. Clone recursively: git clone --recursive https://github.com/TheCherno/RayTracing
  3. Run scripts/Setup.bat (generates project files via Premake).
  4. Open RayTracing.sln in Visual Studio 2022.
  5. Build and run. Prefer Release or Dist for real-time performance; Debug is slow.

Controls

  • Right Mouse Button: enter look mode (locks cursor)
  • Mouse Move: look around
  • W/A/S/D: forward/left/back/right
  • Q/E: down/up
  • Settings Panel: toggle accumulation, force re-render, reset accumulation

Extensibility Ideas

  • Add triangle meshes and a BVH for scalable geometry.
  • Implement MIS and proper cosine-weighted hemisphere sampling.
  • Add materials (dielectric/metal) and importance sampling for BRDFs.
  • Introduce sky/IBL lighting and direct light sampling.
  • Port to GPU compute for large performance gains.

License

MIT license, following the upstream project.

About

Multithreaded CPU path tracer in C++ (Walnut/Vulkan) with progressive accumulation, interactive ImGui scene editing, and ~31 ms per-frame on the reference scene.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages