Skip to content

sdf_shader.cssl — autodiff walkthrough

Status: stub

Full SDF shader walkthrough is planned for Phase 1 completion. This stub covers the key concepts.

The SDF shader example shows two Sigil-specific capabilities that have no equivalent in HLSL/GLSL:

  1. The operator — take the spatial derivative of an SDF at a point. Used to compute the surface normal without a second SDF evaluation.
  2. Effect isolation — the SDF evaluation function is pure (!pure); it can be safely differentiated by the compiler.

In a classical SDF renderer, you compute the surface normal with:

// Classic approach: six extra SDF evaluations
let eps = 0.001;
let normal = Vec3::normalize(Vec3::new(
sdf(p + Vec3::X * eps) - sdf(p - Vec3::X * eps),
sdf(p + Vec3::Y * eps) - sdf(p - Vec3::Y * eps),
sdf(p + Vec3::Z * eps) - sdf(p - Vec3::Z * eps),
));

With Sigil’s autodiff:

// Autodiff: one function, one derivative, exact result
let grad_sdf = ∂sdf; // compiler generates the gradient function
let normal = Vec3::normalize(grad_sdf(p)); // exact gradient, no eps noise

The ∂sdf expression is a compile-time transformation. The compiler analyzes the body of sdf, applies automatic differentiation, and generates a grad_sdf function that computes the exact gradient. No numerical approximation, no epsilon tuning.

This only works because sdf is !pure. The autodiff transformation requires that the function has no side effects that would be duplicated during the differentiation pass.

Coming in Phase 1 completion. See §C.1 Autodiff for the autodiff system documentation.