smallpt is a bare minimum path tracer written under 100 lines of C++, featuring diffuse, and specular reflection, and refraction. Using the detailed explanation slides by David Cline, I experimented porting it to GLSL on Shadertoy.
This proved to be an interesting experiment that brought a few lessons.
- Path tracing is fun, easy to implement, and good looking.
- It is also slow to converge, so trying to get your rays toward the light source is a big win.
- GLSL support in WebGL is still nowhere near robust: valid code will or will not work depending on the platform, the browser, and whether the OpenGL layer is native or not. The statements “break” and “continue” in particular seem often to break everything.
You can see the shader and tweak it here. By default it uses 6 samples per pixel, and 3 bounces, which allows it to run smoothly on average hardware. I found 40 samples per pixel and 5 bounces to give nice results while maintaining interactive framerate.
Update: since GLSL Sandbox has a feature, reading from the previous frame buffer, that Shadertoy is missing at the moment, I thought it’d be interesting try it to have the image converging over time. A little hacking later, a minute or so worth of rendering got me this kind of result: Given the effort, I am really pleased by the result.