Skip to content
Niffoxic

CPU Raytracer with AI

One scene, four renderers: my C++ path tracer, its DXR port to the GPU, a game-style rasterizer and an AI pass, measured against a 16,384-sample reference.

Type
Renderer · C++23 · DXR
Role
Solo: CPU and DXR path tracers, the D3D12 raster viewport, the AI pass, the measurement tools and the docks shot
Stack
C++23 · DirectX 12 · DXR 1.1 · HLSL · Intel OIDN · Python · SDXL + ControlNet · Dear ImGui
Links
Source on GitHub ↗
CPU Raytracer with AI
The night docks: path traced with DXR at 512 samples per pixel and denoised with Intel OIDN, 15.2 s per 1080p frame on the GPU. This shot has no AI pass.

My DXR port matches the CPU path tracer within 0.16% at 64× the speed, and 64 samples plus Intel's denoiser score like about 1,600.

faster with my DXR port than on the CPU at equal samples (106 s to 1.6 s), within 0.16% in brightness
64×
is what 64 spp scores like after Intel's denoiser: the work of 25× the samples
~1,600 spp
per path-traced 1080p docks frame (2.74M triangles, 512 spp, motion blur), against 4.1 ms rasterized
15.2 s
less flicker in the AI pass, by reprojecting the last frame with the renderer's own depth instead of optical flow
45%

Overview

Clean path-traced images need thousands of samples per pixel, which is slow on a CPU. I wanted to know how close a fast, noisy render plus a denoiser and an AI pass can get to a high-sample reference, how that compares with a GPU path tracer and a game-style rasterizer on the same shot, and what the AI changes along the way. I wrote the CPU path tracer first, in April 2026. In November 2026 I turned it into a small rendering lab: set up a shot in a real-time DirectX 12 viewport, record a camera path, and render that exact shot four ways, frame for frame. All four renderers read the same scene file and camera, so their frames line up pixel for pixel and can be scored against a 16,384-sample reference with PSNR, SSIM and FLIP. The night docks shot at the top is the same pipeline at production scale: 2.74 million triangles lit by moonlight, fire and fog.

The full demo, with captions and sound: the docks rasterized, then path traced with DXR and denoised, the two side by side, and how a frame is built.

What I built

  • CPU path tracer Unidirectional path tracing with next-event estimation and multiple importance sampling (power heuristic), Russian roulette from depth 3 and firefly clamps, plus direct-lighting, instant-radiosity, photon-mapping and light-tracing modes. Lambert, Oren-Nayar, mirror, rough conductor (GGX, complex IOR), glass, rough glass, plastic and layered materials; area lights sampled by power, and environment maps.
  • Fast on the CPU A binned-SAH BVH and 64×64 tiles across all 32 threads of a Ryzen 9 9950X, with progressive accumulation: about 76 million rays per second.
  • DirectX 12 platform My own D3D12 layer, replacing the D3D11 framework the project started on: a flip-model swapchain with 3 frames in flight, descriptor heaps, upload and readback, GPU timestamp queries, ImGui and DXC shader builds. One upload of the scene feeds both GPU renderers.
  • DXR path tracer An HLSL port of the CPU integrator and every material, using DXR 1.1 inline ray queries in a compute shader. The same pixel filter and deterministic per-frame seeds let its frames be checked against the CPU's.
  • Raster viewport A real-time preview on the CPU camera's exact pixel grid in under 1 ms: a 2048² shadow map with 5×5 PCF, emitters grouped into point lights, blended glass and the same tonemap as the path tracers.
  • Camera paths and sequence export Keyframe or live-record a fly-through (Catmull-Rom on a fixed frame grid, scrubbed in a timeline panel), then render it with any backend. Every frame writes beauty, HDR, denoised, albedo, normal, depth and camera data with timings; runs resume and every frame is reproducible.
  • AI pass Stable Diffusion XL img2img at strength 0.4, starting from the denoised frame with a fixed seed and prompt, with ControlNet Union fed the renderer's own depth and normals so the geometry stays locked. The previous AI frame is warped into the current view with the renderer's exact depth and camera, then blended where the surface was visible in both: temporal stability without optical flow.
  • Production shot The night docks: 2.74 million triangles exported from Unreal Engine 5.8 as glTF, a skinned walk baked per frame, a procedural moonlit sky, blackbody fire, and smoke and mist volumes path traced with delta and ratio tracking. Motion blur comes from 4 sub-frames across a 180° shutter, denoised with Intel OIDN using albedo and normal guides.
  • Measurement tools Python tools that score every frame against the reference (PSNR, SSIM, FLIP), measure flicker with the camera motion removed, and assemble the comparison videos.

Results

9 figures
Path traced · 15.2 sRasterized · 4.1 msRasterized · 4.1 msPath traced · 15.2 s
FIG. 1The same docks frame, rasterized in 4.1 ms and path traced in 15.2 s: 512 samples per pixel with motion blur, denoised with Intel OIDN. Drag the divider to compare.
One camera path, four renderers, frame for frame: the GPU rasterizer (0.4 ms a frame), my DXR path tracer at 1,024 spp (1.6 s), my CPU path tracer at 64 spp (6.8 s) and the CPU frames after denoising and the AI pass.
FIG. 2One camera path, four renderers, frame for frame: the GPU rasterizer (0.4 ms a frame), my DXR path tracer at 1,024 spp (1.6 s), my CPU path tracer at 64 spp (6.8 s) and the CPU frames after denoising and the AI pass.
RendererCost per framePSNR dBSSIMFLIPFlicker
GPU raster (D3D12)0.4 ms GPU22.40.9250.2480.0016
DXR path tracer, 1,024 spp1.6 s GPU42.60.9570.0200.0080
CPU path tracer, 64 spp6.8 s30.80.6270.0540.0269
CPU 64 spp + OIDN+0.15 s44.40.9920.0250.0036
CPU 64 spp + OIDN + AI+13.6 s GPU30.90.9600.1180.0043
Same AI pass, no reprojection30.30.9540.1200.0078
FIG. 3181 frames at 1280×720, scored on the tonemapped image against a 16,384-spp reference. PSNR and SSIM: higher is better. FLIP and flicker: lower is better. The AI pass scores below OIDN on purpose: it re-imagines materials and light.
Quality per sample on one frame: the CPU and DXR path tracers land on the same curve, and 64 spp plus OIDN scores like about 1,600 spp without denoising.
FIG. 4Quality per sample on one frame: the CPU and DXR path tracers land on the same curve, and 64 spp plus OIDN scores like about 1,600 spp without denoising.
DXR from 1 to 4,096 samples per pixel, then the CPU's 64-spp frame denoised with OIDN, then the AI pass.
FIG. 5DXR from 1 to 4,096 samples per pixel, then the CPU's 64-spp frame denoised with OIDN, then the AI pass.
AI passDenoisedDenoisedAI pass
FIG. 6What the AI pass changes, in a 2× crop of the same frame: the glass sphere loses its refracted checkerboard. The depth and normal guides keep the geometry; the surfaces are re-imagined.
Fire and lamps light the fog through every bounce. The smoke and mist are path-traced volumes with their own shadows.
FIG. 7Fire and lamps light the fog through every bounce. The smoke and mist are path-traced volumes with their own shadows.
Moonlight from a procedural sky, a lamp and a brazier. No bloom, grain or colour grade: only exposure and a filmic tonemap, the same for both renderers.
FIG. 8Moonlight from a procedural sky, a lamp and a brazier. No bloom, grain or colour grade: only exposure and a filmic tonemap, the same for both renderers.
The pier from above: animated waves, wet planks and drifting mist. The motion blur comes from 4 sub-frames across a 180° shutter.
FIG. 9The pier from above: animated waves, wet planks and drifting mist. The motion blur comes from 4 sub-frames across a 180° shutter.

Hard parts

  • Feeding the previous AI frame back in as the diffusion input made invented detail compound from frame to frame: lamps appeared on the walls and the sphere grew a pattern. Diffusion now always starts from the clean render, and the smoothing happens afterwards by reprojection, which took flicker from 0.0078 to 0.0043.
  • Proving the GPU port is right, not just fast: at 1,024 spp on the same frame, the DXR image's mean brightness is within 0.16% of the CPU's, and quality per sample matches along the whole ladder (30.8 vs 30.5 dB at 64 spp, 42.2 vs 42.5 dB at 1,024).
  • Measuring a moving shot fairly: flicker is the mean change between frames after the camera motion is removed with the renderer's own depth and cameras, so it measures the renderer, not the camera move.

Limits

What it doesn't do yet
  • The AI pass invents and changes detail; it isn't a faithful denoiser. The glass sphere loses its refraction detail, and at strength 0.55 it turned the glass to stone and a reflection into an apple. Its lower scores measure how far it re-imagines the frame.
  • Only the raster preview runs in real time. The CPU frames (about 7 s each), the AI pass (about 14 s) and the high-sample DXR frames (1.6 s at 1,024 spp, 15.2 s for a docks frame) are offline renders.
  • The raster lighting is approximate on purpose: emitters as point lights, one shadow map, no mirror reflections and an ambient fill tuned against the reference.
  • The DXR port maps the layered material to its base layer. Windows and DirectX 12 only; the DXR path needs a DXR 1.1 GPU.

Credits. 3D assets: a docks environment and the Dark Knight character from Fab. Denoiser: Intel Open Image Denoise. AI pass: Stable Diffusion XL base 1.0 and ControlNet Union SDXL, run locally in ComfyUI.

I can build something like this for your project.

Related services: Renderers and game engine tools, Rendering R&D and optimisation.

Contact for a freelance project
Harsh Dubey

Harsh Dubey

Graphics and game programmer. MSc Games Engineering (Distinction), University of Warwick.

About me