WebGPU port of smallpt: Global Illumination in 99 lines of C++ by Kevin Beason.
Original source code was found here and ported to the web using TypeScript and WebGPU. Actually, this version is loosely based on two modifications of the original algorithm. The first one is called "explicit.cpp" which can be found here and, as its description specifies, is a
Huge speedup, especially for small lights. Adds explicit light sampling with 23 additional lines of code and a small function signature change. Produces this image in 10 seconds on a Intel Core i7 920 quad-core CPU using 16 samples per pixel.
The second one is called "forward.cpp" and it's a
Revision of
radiance()function that removes all recursion and uses only a simple loop and no path branching. That is, the ray tree is always one ray wide.
Moreover, other than running on the GPU instead of the CPU like the original C++ implementation, this port also introduces some features on top of the original algorithm, those beeing:
-
UWAL library for a more straight forward context initialization and to easily manage WebGPU resources.
-
OffscreenCanvas(es)1 to avoid blocking the main thread while the simulation is running.
-
oidn-web denoising library to produce a clearer output image after only a few samples.
git clone https://github.com/UstymUkhman/uwal-webgpu-smallpt.git
cd uwal-webgpu-smallptbun install
bun run devbun run build
bun run previewFootnotes
-
At the moment, 2 canvas elements are used: one with the WebGPU context to run the path tracer (compute shader) and output the result (render pass); and another one with a 2D context to output the result of the image denoiser. I know that a better implementation would be to use only one canvas with a WebGPU context and run a denoiser on top of that, leveraging input and output buffers to avoid the cost of syncing between CPU and GPU as mentioned here, but that would require to setup an additional normal and albedo buffers since
hdrandauxoptions are required in the WebGPU pipeline. This is considered to be a nice future improvement. ↩