Skip to main content

Module csm

Module csm 

Source
Expand description

Cascaded shadow map cascade computation. Produces a ShadowUniforms carrying one orthographic light-view-projection matrix per cascade plus the view-space far depth for each cascade (the fragment shader uses these to select which cascade slice to sample).

Algorithm (per-frame, called from each backend’s draw loop):

  1. Split the camera’s [near, shadow_distance] depth range into N cascade sub-ranges using the practical PSSM blend (lambda * logarithmic + (1 - lambda) * linear).
  2. For each cascade, compute the 8 frustum corners at its near/far depths, then bound the corners with a sphere. The sphere bound makes the orthographic light frustum rotation-invariant, eliminating shimmer when the camera rotates.
  3. Snap the sphere centre, in world space along the light’s right/up axes, to a per-cascade texel grid so the grid stays anchored in the world and individual texels don’t crawl as the camera translates.
  4. Build a RH look_at from outside the sphere along the light direction and an ortho projection that exactly encloses the sphere.

The math is shared across all three backends: Metal, Vulkan, and DirectX all use RH view matrices with [0, 1] depth in their orthographic projections, so the same VPs are valid for every backend’s shadow sampling.

Structs§

ShadowUniformInputs
Camera, light, and shadow-configuration inputs to compute_shadow_uniforms.

Functions§

compute_shadow_uniforms
Compute cascade VPs + split depths from camera + light parameters.
empty_shadow_uniforms
Fallback uniforms used when no shadow pass is active: identity VPs and a single split at +inf so the fragment shader always picks cascade 0 with a 1x1 fallback texture (returns “fully lit”).