Shadows
EntropyPortal implements a Virtual Shadow Map (VSM) system that supports high-resolution shadows for multiple light types.
Virtual Shadow Maps (VSM)
Section titled “Virtual Shadow Maps (VSM)”Traditional shadow mapping requires allocating a texture for every shadow-casting light, which consumes immense VRAM. VSM solves this by allocating a single, large Shadow Atlas (e.g., 8192x8192) and virtualizing the pages.
Key Concepts
Section titled “Key Concepts”- Shadow Atlas: A massive texture array where all active shadow pages reside.
- Page Table: A structure mapping
(LightID, Face, lod)to a physical page in the atlas. - Sparse Allocation: Only pages that are visible on screen are allocated and updated.
Light Types
Section titled “Light Types”Directional Lights (Cascaded Shadow Maps)
Section titled “Directional Lights (Cascaded Shadow Maps)”- Uses Cascaded Shadow Maps (CSM) to handle large outdoor scenes.
- Typically 4 cascades per directional light.
- Splits are calculated logarithmically (
cascadeLambda) to balance near-field resolution and far-field coverage.
Point Lights (Omnidirectional)
Section titled “Point Lights (Omnidirectional)”- Rendered as 6-face cubemaps into the atlas.
- Used for local light sources.
Spot Lights (Perspective)
Section titled “Spot Lights (Perspective)”- Rendered as a single perspective projection.
- Most efficient shadow type.
Implementation Details
Section titled “Implementation Details”The VirtualShadowMapService manages the atlas and page tables.
graph TD
Start([Frame Start]) --> Collect[Collect Lights]
Collect --> Layout["Atlas Layout<br/>(Shelf Packing)"]
Layout --> Matrices[Compute Matrices]
Matrices --> Render[Render Shadow Passes]
Render --> Upload["Upload Metadata<br/>(Page Tables)"]
Upload --> End([Frame End])
- Culling: Determine which lights cast shadows on visible geometry.
- Allocation: Allocate pages in the atlas for required shadow faces.
- Rendering: Render depth for all allocated pages in a single pass (or batched passes).
- Sampling: In the shader, the
ShadowAtlasis sampled using manual PCF or hardware comparison samplers.