A compact path-tracing playground built on Walnut and Vulkan.
-
Application Layer (
RayTracing/src/WalnutApp.cpp):- Hosts an ImGui-driven UI with panels for Settings, Scene, and the Viewport.
- Owns
Renderer,Camera, andSceneinstances. - 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::ImageRGBA 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.
- CPU path tracer that renders into a
-
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
SpheresandMaterialsarrays. MaterialexposesAlbedo,Roughness,Metallic, and emissive color/power; emission contributes additively.
- Minimal scene graph with
-
Rays (
RayTracing/src/Ray.h):- Simple ray structure with
OriginandDirection.
- Simple ray structure with
-
Runtime/UI:
- ImGui panels expose renderer settings (accumulation toggle, reset), sphere transforms/material selection, and material parameter controls.
- Viewport panel displays the current
Walnut::Imagevia descriptor set.
- Camera caches per-pixel primary ray directions using inverse projection and view matrices.
- Renderer walks each pixel, spawns a ray from camera position along the cached direction.
TraceRayfinds the closest sphere by solving the quadratic analytically; returns hit/miss payload.- On hit, the integrator accumulates emission and multiplies
contributionby material albedo. - The new ray origin is offset by the surface normal; the next direction is cosine-like by normal + random unit vector.
- Accumulated color is averaged across frames for denoising-like convergence when accumulation is enabled.
- 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_eachto 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.
- 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).
Officially supports Windows 10/11 and Visual Studio 2022. Requires the Vulkan SDK.
- Install the Vulkan SDK:
https://vulkan.lunarg.com/ - Clone recursively:
git clone --recursive https://github.com/TheCherno/RayTracing - Run
scripts/Setup.bat(generates project files via Premake). - Open
RayTracing.slnin Visual Studio 2022. - Build and run. Prefer
ReleaseorDistfor real-time performance;Debugis slow.
- 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
- 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.
MIT license, following the upstream project.