Skip to main content

Module shadow

Module shadow 

Source
Expand description

Where each shadow map looks, and how far: the sun’s cascades, a spot light’s cone and a point light’s six faces.

Topic 45’s shadows (docs/notes/rendering.md), arithmetic half. This module owns the matrices and the tile budget; crate::forward owns the atlas, the depth-only pipeline and the per-tile cull that fills them. The split is where the testable part is: a cascade matrix is a pure function of a camera and a light direction, a spot’s is a pure function of the light, and the tile budget is a pure function of the light list — so every claim below is checked here without a device in the room.

§One atlas, TILES slots, and an allocator behind them

The maps live side by side in a single D32Float image rather than as layers of an array. That is a consequence of the render graph, not a preference: a crate::graph render pass attaches an image, and there is no way to attach layer i of one. Tiles need nothing the graph does not already have — the shadow pass sets a viewport over a tile and draws — so the tile count is a constant either side of the seam and not a feature request. mesh.slang’s shadow_atlas says the same from the sampling side.

A slot is one map: an index into the frame block’s matrices, and the number a light’s row carries. The split of them is topic 18’s 2026-08-13 decision: the sun’s cascades take the first CASCADES slots and the rest are handed out one per shadowed spot and POINT_FACES per shadowed point, with LIGHT_TILES of them. A light that gets no slots still lights and simply does not occlude, which is what makes the budget a quality knob rather than a correctness cliff — see Selection.

Where a slot’s map lives in the image is a separate question, and AtlasAllocator answers it. Topic 45’s atlas rung: the image is a quadtree over ATLAS_COLUMNS by ATLAS_ROWS root cells of TILE texels, a map takes a whole cell or a halving of one down to MIN_TILE, and the shader reads the rectangle it landed in — see Selection::atlas_rect and mesh.slang’s atlas_uv.

What asks for a halving is the priority rung, and it is coverage: how much of the frame’s height a light’s map covers on screen decides both which lights are worth a run of tiles and how large each of those tiles is. A light covering less than WHOLE_CELL_COVERAGE takes a quarter of a cell, and each halving of its coverage below that takes another — tile_level is the ladder and LEVEL_HOLD_RATIO the band that stops a light on a threshold halving and doubling its map every frame. A frame whose lights all clear the top of the ladder lays out exactly as the fixed grid did, cell for cell.

§Two budgets, because they buy different things

LIGHT_TILES is atlas space and LIGHT_SLOTS is cull space, and the two stopped being one number the moment a light could own six tiles:

  • A tile is TILE² of D32Float, and the atlas is allocated once for the renderer’s life. POINT_FACES of them is what a point light needs to be shadowable at all, since its faces have to fit in the region together, and LIGHT_TILES holds two such runs with two tiles over — so two cubes and two cones fit side by side.
  • A slot is one shadowed light, and what it costs is a DrawGen — roughly five megabytes, most of it per-instance LOD hysteresis state that is device-local and permanent. Topic 18’s fourth decision is that a point light gets one of these rather than one per face: the six faces’ union is the light’s sphere, which is what the cull tests against anyway, so one visible set feeds all six draws through six matrices into six tiles. A face draws what is behind it and the rasteriser discards it.

So the light region holds two point lights and two spots, or LIGHT_SLOTS spots, and a frame with more shadow-worthy lights than either budget covers shadows the most influential ones it can fit and lights the rest without occluding. A third point light is what no longer fits: three cubes are three times POINT_FACES tiles and the region is not that long.

§The 2026-08-26 re-tiling, which cost no memory

The region held POINT_FACES tiles and one over until 2026-08-26, so exactly one point light in any scene could occlude — and a rig with a light either side of a walkway, which is an ordinary rig, had the torch that lost the tie re-lighting the shadow its twin cast. Widening the grid by a column and a row while shrinking TILE to match leaves atlas_extent where it was to the texel, so the image, its D32Float format and its memory are unchanged and the whole cost is per-tile resolution. What is not free is LIGHT_SLOTS: a slot is a DrawGen, so the culls this budget added are device-local memory the atlas did not ask for.

§Which filter samples them, and the seam that shows two at once

r_shadow_filter is the one knob here that reaches into mesh.slang’s sampling rather than into this module’s arithmetic: topic 45’s ladder has three filter rungs and, until it existed, only the top one was in the tree. Filter is the set, filter is the near side of the comparison and shipped_filter is the far one.

The seam is per pixel and not per pass, which is what r_shadow_split buys and what makes it unlike crate::ssao::r_ssao_split: the occlusion gather is a full-screen pass, so its two sides are two recordings under two scissors and cost one target’s worth of fragments between them. The filter lives in a scene draw, where a second recording is every triangle again — so the two modes and the seam’s column ride in crcbl_shaders::mesh::FrameUniforms::shadow_filter and the fragment picks its own side. crate::split’s header carries both shapes.

pcss is the default and is what every golden was blessed at, and a frame nobody has touched either variable on writes a row of zeroes — one mode in both lanes and no column — which is the picture this engine drew before the row existed.

§Stability: a sphere around the eye, snapped to texels

The classic cascade shimmer is a shadow edge crawling along a static surface while the camera moves. It has two causes and this module removes both:

  • The cascade’s extent changing with the camera’s orientation. A box fitted to the split’s view frustum is tight but rotates with the camera, so every frame resamples the same geometry at a different scale. Cascades uses a sphere instead, and its radius is Cascades::far — a number that depends on the split alone. Rotating the camera cannot change it.
  • The cascade’s origin moving by a fraction of a texel. Fixed by quantising the light-space origin to whole texels, which is what snap_to_texel does and what the ortho box below is built around.

The cost of the sphere is resolution: a sphere of radius r centred on the eye covers everything within r, including the half of it behind the camera that will never be shaded. A frustum-fitted box would be roughly twice as dense for the same tile. That is the trade, taken deliberately: the tight fit is the version that has to branch on Projection — an orthographic camera has no field of view to build corners from — and a shadow pass that is correct on one projection and wrong on the other is worse than one that is uniformly coarser.

Structs§

Assignment
One shadowed light: which light it is, where its run of tiles starts, and how large each of them is.
AtlasAllocator
The atlas’s free space, as a quadtree over each of its TILES root cells.
Cadence
The two limits a frame’s redraws are spent under.
Cascades
The cascade matrices a frame shades and culls with.
Group
One group of the atlas that wants redrawing, and what redrawing it costs.
Selection
Which lights hold the atlas’s light tiles, and its memory of last frame.
Tile
One tile the allocator handed out, and the only thing that names it.
TileRect
A rectangle of the shadow atlas, in texels.

Enums§

Filter
The shadow filter a fragment runs — topic 45’s ladder, with every rung of it still in the tree.

Constants§

ATLAS_COLUMNS
Tiles across the atlas.
ATLAS_ROWS
Tiles down it.
CASCADES
How many cascades the sun’s shadow map is split into.
DISTANCE
How far from the eye the sun’s shadows reach, in world units.
GROUPS
Groups the shadow atlas is drawn in: one per cascade and one per light slot.
LIGHT_SLOTS
How many lights a frame can shadow at once.
LIGHT_TILES
How many tiles the atlas has for shadowed lights beside the cascades.
MAX_SPOT_HALF_ANGLE
The widest half-angle a spot can have and still be given a tile, in radians.
MIN_TILE
The smallest tile the allocator can hand out, in texels.
POINT_FACES
Tiles one point light’s shadow map is: the six faces of a cube.
TILE
The side of one root cell of the shadow atlas, in texels.
TILES
Tiles in the whole atlas: CASCADES for the sun and LIGHT_TILES for the lights that fit.
TILE_LEVELS
Sizes the allocator hands out: TILE and each halving below it, so the finest is MIN_TILE.

Statics§

r_shadow_bias
r_shadow_cadence
r_shadow_faces
r_shadow_filter
r_shadow_normal_offset
r_shadow_split

Functions§

atlas_extent
The shadow atlas’s extent in texels: an ATLAS_COLUMNS by ATLAS_ROWS grid of TILE-sided tiles.
face_axis
The direction point-light face face looks along.
filter
r_shadow_filter as the near side of the seam wants it.
light_tile
Which atlas tile light tile tile of the light region is.
point_frustum
The frustum a point light’s one cull runs against: the axis-aligned box around its sphere of influence.
point_matrix
A point light’s world → shadow-clip matrix for face face: a 90° perspective down that face’s axis, reversed-Z.
shipped_filter
The filter the engine ships, for the other side of the seam.
split_at
r_shadow_split as crate::split::halves wants it, or None for a frame comparing nothing.
spot_matrix
A spot’s world → shadow-clip matrix: a perspective projection down the cone, reversed-Z, covering the cone exactly.
tile_origin
Where root cell index starts in the atlas, in texels.
tile_span
How many light tiles light needs to be shadowed: POINT_FACES for a point light, one for a spot.