Repository navigation
feat(render3d): add clustered forward shading for many local lights - #732
Merged
Merged
Conversation
…d PBR shading ClusteredLightSystem now owns the whole LightGroup bind group: the LightRig uniform LightWGSL reads stays at binding 0, and the cluster parameters or fallback lights and the storage buffers move to bindings 1 to 4, so clustered local lights combine with the rig's ambient, directional, point and spot lights in one shader. ClusteredLightGroup is gone and Bind(pass) matches GPULightBuffer.Bind. shade_pbr_clustered replaces the Lambert-only evaluate_clustered_lights: it adds each clustered light to shade_pbr through pbr_brdf, point_light_attenuation and spot_cone_attenuation, and ClusteredLitWGSL / ClusteredFallbackWGSL are LitPBRWGSL with it, writing linear_to_srgb. NewClusteredLitPBRPipeline builds them. On the CPU, EvaluateLightAttenuation reuses PointLightAttenuation and SpotConeAttenuation, and ShadeClusteredPoint / ShadeClusteredGridPoint replace EvaluateDirectLights / EvaluateClusteredLights. ClusterLightFromPoint and ClusterLightFromSpot convert the existing light types and derive a cutoff range for Range 0, and Update returns an error for an invalid light or rig instead of dropping it. SelectFallbackLights frustum-culls before ranking and sorts with slices.SortFunc so the fallback path allocates nothing; fallback stats count culled and truncated lights separately. Validate bounds MaxLights, MaxLightsPerCluster and the index slots, and a storage buffer above the device's binding limit is an error instead of a silent clamp. Tests cover truncation, the converters, invalid input and fallback allocations, and the wall-clock assertion moved out of the unit test. Docs, the changelog fragment and the design note describe the new design, link it from the LightRig section, and say to pass ScaledSize() under dynamic resolution.
houko
force-pushed
the
feat/render3d-clustered-forward-382
branch
from
November 24, 2026 18:39
d929289 to
6d02dab
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.
Summary
Adds clustered forward shading to
packages/game/render3dso a scene can light hundreds of local point and spot lights, built on the existingLightRigand metallic-roughness PBR path, with a uniform-buffer fallback on targets without storage buffers or compute shaders (WebGL 2 / OpenGL ES 3.0).Part of #382 (the native-GPU frame-time measurement below is still open, so this does not close it).
Design
ClusteredLightSystemtakes aGPULightBuffer's place atLightGroup. Binding 0 is the sameLightRiguniformLightWGSLreads (ambient, directional, and the rig's own point and spot lights); bindings 1 to 4 hold the cluster parameters and the light, cell and index storage buffers (or, in fallback mode, one uniform array of up to 16 lights).Bind(pass)matchesGPULightBuffer.Bind.shade_pbr_clustered(world_pos, normal, view_dir, base_color, metallic, roughness, frag_pos)returnsshade_pbrplus every light in the fragment's cluster, each throughpbr_brdf,point_light_attenuationandspot_cone_attenuation, so clustered and rig lights shade identically.ClusteredLitWGSL/ClusteredFallbackWGSLareLitPBRWGSLwith it (sRGB output), built byNewClusteredLitPBRPipeline.ClusterLightFromPoint/ClusterLightFromSpotconvertPointLight/SpotLight; forRange == 0they derive the distance where the radiance falls toClusterRangeCutoff.Updatereturns an error for an invalid light or rig instead of dropping it.ClusterGridbins lights on the CPU with no steady-state allocations (logarithmic depth slices, sphere and cone tests). The shader finds the depth slice with a binary search over a uniform split table, because NSGo v0.3.0's software GPU VM evaluateslog/log2/exp/exp2to zero.SelectFallbackLightsfrustum-culls before ranking and the fallback path allocates nothing.ValidateboundsMaxLights,MaxLightsPerClusterand index slots, and a storage buffer above the device's binding limit is an error.ShadeClusteredPoint(brute force) andShadeClusteredGridPoint(cluster lookup, as the GPU does).Performance
BenchmarkClusterAssign256Lightsmeasures CPU light assignment for 256 lights. GPU fragment cost of 256 lights at desktop resolution on a native backend has not been measured in this PR; CI only runs the software renderer, so that part of the #382 acceptance item is still open.Verification
go tool nsgo test ./render3dandgo tool nsgo check -target linux,web ./render3dpass (350 tests)go tool nsgo test .inpackages/gamepasses (3397 passed, 3 skipped)go run ./tools/changelog checkpassesnsgotest.NoGPUComputeprofiles (including a 256-light scene with a rig), and cover truncation, converters, invalid input, fallback allocations, and retained device loss.