Hacker News new | ask | show | jobs
by riggsdk 89 days ago
I do this in a more crude fashion (like the article mentions) on the GPU in an old personal project.

I just run a low-res (256x256) pre-pass and store the distances to a floating point texture. I then use that pre-pass texture as the starting point (minus some delta) when drawing full-screen. This makes it really nice and performant, even for complex SDF shapes.

I think a common misconception in GPU programming is that branching is slow. It is only really slow when neighboring fragments diverges on those logic branches.

The quick pre-pass step gets close enough to the SDF surface that more fragments are in lockstep with each other and terminates at the same time, eliminating the expensive re-runs the GPU driver has to do. More experimentation is needed on my end. I do this in the browser with WebGL so accurate profiling is sometimes difficult.

I experimented with different resolutions and number of pre-pass steps but found it was sufficient on most GPU's with a single prepass run (subject to change the more I test).