Ray-traced shadows for the poor

A while back I was toying around with doing ray-tracing using signed distance fields.
I did get quite far, so I thought I would share the direct shadowing use case: Demo Link

image

This is in rendered using Shade, and as a disclaimer - it’s not opensource.

There is no GI here either, just standard IBL

Shade can bake SDFs for individual meshes or the entire scene, and that’s how the SDF was created. To make the demo more tolerant of lower-end GPUs I uploaded a pre-computed SDF alongside.
It’s 128x128x128 voxels.

The format is f32, entire texture is stored as raw bytes and takes up 64 Mb, no surprise there. So if you wonder why it takes a while for shadows to show up - it’s because SDF is not downloaded yet :sweat_smile:

Each pixel does up to 64 ray marching steps through the SDF, typically ~7 steps is enough.

The shadows are accurately soft, meaning that you get larger penumbra further away from an occluder. I guess it would be more accurate to call this cone-traced shadows rather than “ray” traced, since we get occlusion value for each cone and the cone angle is proportional to the light source radius.

Took me a few years overall to get to this point. I played with SDF ray tracing in Shade for many months, and I poured over all and any SDF research I could get my hands on.


The tech would also not be possible without the ray tracing API that Shade offers. Key components being:

// queries ray tracing acceleration structures for nearest point in a sphere
fn query_nearest(position_ws: vec3f, max_distance: f32) -> SURFACE_POINT_3D

// produces -1 or 1, based on whether a point is inside of a mesh or outside. This is critical for the "Signed" part of SDF determination
fn query_volume_sign(position_ws: vec3f, max_distance: f32) -> f32

People write that SDF build for meshes takes minutes or hours, which is somewhat embarrassing for me to read, considering that with RTX api it’s almost free :blush:

Still, 128 voxel resolution might not seem like much, but 128*128*128 = 2,981,888 which is a lot of points to process.


PS
If you could share your FPS, resolution and the GPU model I would really appreciate it, helps me improve :person_bowing:

3 Likes

Hi, what you are doing is quite amazing. Thanks for sharing.

I have a MacBook M2 Max.
at 5100x1200 I get 18 fps
at 1800x1200 I get 33 fps
I hope it helps.

1 Like