Skip to content

feat(render3d): add directional cascaded shadow maps - #733

Merged
houko merged 2 commits into
mainfrom
feat/render3d-csm-257
Nov 24, 2026
Merged

houko merged 2 commits into
mainfrom
feat/render3d-csm-257

Conversation

@houko

@houko houko commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #257

Summary

Adds directional cascaded shadow maps (CSM) to packages/game/render3d for the first directional light of a LightRig, so shadows stay sharp near the camera and still reach the horizon on every NSGo backend (WebGL 2 / OpenGL ES 3.0 and the software renderer included).

  • Splits and fitting: ComputeCSMSplits (practical split scheme, SplitLambda default 0.75) and FitCascades / FitCascadesFromCamera, which cut each cascade along the projection's own near and far planes (orthographic cameras with Near <= 0 included), pad toward the light by CasterPadding, and snap to whole texels with TexelSnap. Fitting and uniform encoding allocate nothing.
  • One 2D depth atlas (CascadedShadowMap): 1-4 cascades as tiles of one FormatDepth32Float texture (1x1, 2x1, 2x2). Update refuses frames fitted with another cascade count, resolution or filter than the atlas; the first cascade pass after Update clears the atlas, so cascades can be drawn in any order. DrawCaster takes one slot per distinct model per frame, reused across cascades, and returns false when MaxCasters is exhausted.
  • Lit PBR integration: CSMGroup is ShadowGroup (3). CSMWGSL / CSMWGSLForGroup declare sample_csm_shadow(world_pos, world_normal) for shade_pbr_shadowed; LitPBRCSMWGSL, NewLitPBRCSMPipeline and LitPBRPipelineOptions.CSMLayout shade the rig exactly like LitPBRWGSL with directional light 0 attenuated by the cascades. Filtering reuses ShadowFilter; DebugCascades tints by cascade.
  • CPU reference: EvaluateCSMShadow (over an atlas from ReadCSMDepthAtlas / ReadCSMDepthAtlasAsync), ShadePointShadowed and CSMFrame.DebugTint.
  • Docs in docs/en|zh/render3d.md (next to the other shadow sections), changelog.d/render3d-csm-257.md, and .agents/notes/implemented/feature/2026-11-24-render3d-csm.md.

Tests

packages/game/render3d/csm_test.go covers splits, texel-snapping stability, cascade selection and blending, validation, zero-allocation fitting, Update refusing mismatched frames, caster slot reuse and overflow, an orthographic camera with a negative near plane, reverse cascade order, hard vs 3x3 PCF against the CPU reference, Retained device loss and Close. TestCSMLargeSceneReferenceRender renders six pillars across four cascades through LitPBRCSMWGSL on the full and NoGPUCompute profiles, compares with testdata/golden/csm_large_scene.png, and checks GPU pixels against the CPU reference within 4/255.

Verification

  • gofmt -l (empty)
  • go tool nsgo test ./render3d and go tool nsgo check -target linux,web ./render3d
  • go tool nsgo test . in packages/game
  • go run ./tools/changelog check
  • bun run verify-notes

Screenshots

None (no UI change; the golden test render is updated).

@github-actions github-actions Bot added size/XL enhancement New feature or request documentation Improvements or additions to documentation pkg/render3d area/agents conflict Conflicts with the target branch; resolve before merging and removed enhancement New feature or request size/XL labels Nov 24, 2026
…h cascaded shadow maps

Rebuild the cascaded shadow map on the lit PBR path that main now has, and fix the review findings.

- CSMGroup is ShadowGroup (3), CSMWGSL declares sample_csm_shadow for shade_pbr_shadowed, CSMWGSLForGroup moves the bindings, and LitPBRCSMWGSL / NewLitPBRCSMPipeline / LitPBRPipelineOptions.CSMLayout replace the separate Lambert shader (CSMLitWGSL, CSMLitMaterial, EvaluateCSM, NewCSMLitPipeline) that collided with LightGroup and PBRDrawGroup. Filtering reuses ShadowFilter instead of CSMFilterMode, and CSMFrame.DebugTint is the CPU reference of the debug tint.
- Update refuses a frame fitted with another cascade count, resolution or filter than the atlas, and CascadeCount reports the uploaded frame's count.
- DrawCaster reuses one slot per distinct model across cascades and returns false when the slots run out instead of dropping the shadow silently.
- FitCascades parameterises the frustum corner rays by the projection's own near and far planes, so orthographic cameras with Near <= 0 fit correctly, and FitCascadesFromCamera uses the planes ProjMatrix uses.
- Only the first cascade pass after Update clears the atlas, so cascades can be drawn in any order.
- Comments are no longer hard-wrapped; the docs section moves next to the other shadow sections in both languages.
@houko
houko force-pushed the feat/render3d-csm-257 branch from f210c15 to 11a0aa2 Compare November 24, 2026 18:54
@github-actions github-actions Bot added enhancement New feature or request size/XL and removed conflict Conflicts with the target branch; resolve before merging labels Nov 24, 2026
@houko
houko merged commit 5dedb66 into main Nov 24, 2026
19 of 33 checks passed
@houko
houko deleted the feat/render3d-csm-257 branch November 24, 2026 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agents documentation Improvements or additions to documentation enhancement New feature or request pkg/render3d size/XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3D light: cascaded shadow maps

1 participant