Repository navigation
feat(render3d): add directional cascaded shadow maps - #733
Merged
Merged
Conversation
…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
force-pushed
the
feat/render3d-csm-257
branch
from
November 24, 2026 18:54
f210c15 to
11a0aa2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #257
Summary
Adds directional cascaded shadow maps (CSM) to
packages/game/render3dfor the first directional light of aLightRig, 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).ComputeCSMSplits(practical split scheme,SplitLambdadefault 0.75) andFitCascades/FitCascadesFromCamera, which cut each cascade along the projection's own near and far planes (orthographic cameras withNear <= 0included), pad toward the light byCasterPadding, and snap to whole texels withTexelSnap. Fitting and uniform encoding allocate nothing.CascadedShadowMap): 1-4 cascades as tiles of oneFormatDepth32Floattexture (1x1, 2x1, 2x2).Updaterefuses frames fitted with another cascade count, resolution or filter than the atlas; the first cascade pass afterUpdateclears the atlas, so cascades can be drawn in any order.DrawCastertakes one slot per distinct model per frame, reused across cascades, and returns false whenMaxCastersis exhausted.CSMGroupisShadowGroup(3).CSMWGSL/CSMWGSLForGroupdeclaresample_csm_shadow(world_pos, world_normal)forshade_pbr_shadowed;LitPBRCSMWGSL,NewLitPBRCSMPipelineandLitPBRPipelineOptions.CSMLayoutshade the rig exactly likeLitPBRWGSLwith directional light 0 attenuated by the cascades. Filtering reusesShadowFilter;DebugCascadestints by cascade.EvaluateCSMShadow(over an atlas fromReadCSMDepthAtlas/ReadCSMDepthAtlasAsync),ShadePointShadowedandCSMFrame.DebugTint.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.gocovers splits, texel-snapping stability, cascade selection and blending, validation, zero-allocation fitting,Updaterefusing 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,Retaineddevice loss andClose.TestCSMLargeSceneReferenceRenderrenders six pillars across four cascades throughLitPBRCSMWGSLon the full andNoGPUComputeprofiles, compares withtestdata/golden/csm_large_scene.png, and checks GPU pixels against the CPU reference within 4/255.Verification
gofmt -l(empty)go tool nsgo test ./render3dandgo tool nsgo check -target linux,web ./render3dgo tool nsgo test .inpackages/gamego run ./tools/changelog checkbun run verify-notesScreenshots
None (no UI change; the golden test render is updated).