sdf_shader.cssl — autodiff walkthrough
Status: stub
Full SDF shader walkthrough is planned for Phase 1 completion. This stub covers the key concepts.
What this example demonstrates
Section titled “What this example demonstrates”The SDF shader example shows two Sigil-specific capabilities that have no equivalent in HLSL/GLSL:
- The
∂operator — take the spatial derivative of an SDF at a point. Used to compute the surface normal without a second SDF evaluation. - Effect isolation — the SDF evaluation function is pure (
!pure); it can be safely differentiated by the compiler.
The key insight
Section titled “The key insight”In a classical SDF renderer, you compute the surface normal with:
// Classic approach: six extra SDF evaluationslet 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 resultlet grad_sdf = ∂sdf; // compiler generates the gradient functionlet normal = Vec3::normalize(grad_sdf(p)); // exact gradient, no eps noiseThe ∂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.
Full example
Section titled “Full example”Coming in Phase 1 completion. See §C.1 Autodiff for the autodiff system documentation.