Skip to content

feat(render3d): add clustered forward shading for many local lights - #732

Merged
houko merged 2 commits into
mainfrom
feat/render3d-clustered-forward-382
Nov 24, 2026
Merged

houko merged 2 commits into
mainfrom
feat/render3d-clustered-forward-382

Conversation

@houko

@houko houko commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds clustered forward shading to packages/game/render3d so a scene can light hundreds of local point and spot lights, built on the existing LightRig and 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

  • ClusteredLightSystem takes a GPULightBuffer's place at LightGroup. Binding 0 is the same LightRig uniform LightWGSL reads (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) matches GPULightBuffer.Bind.
  • shade_pbr_clustered(world_pos, normal, view_dir, base_color, metallic, roughness, frag_pos) returns shade_pbr plus every light in the fragment's cluster, each through pbr_brdf, point_light_attenuation and spot_cone_attenuation, so clustered and rig lights shade identically. ClusteredLitWGSL / ClusteredFallbackWGSL are LitPBRWGSL with it (sRGB output), built by NewClusteredLitPBRPipeline.
  • ClusterLightFromPoint / ClusterLightFromSpot convert PointLight / SpotLight; for Range == 0 they derive the distance where the radiance falls to ClusterRangeCutoff. Update returns an error for an invalid light or rig instead of dropping it.
  • ClusterGrid bins 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 evaluates log/log2/exp/exp2 to zero.
  • SelectFallbackLights frustum-culls before ranking and the fallback path allocates nothing.
  • Validate bounds MaxLights, MaxLightsPerCluster and index slots, and a storage buffer above the device's binding limit is an error.
  • CPU references: ShadeClusteredPoint (brute force) and ShadeClusteredGridPoint (cluster lookup, as the GPU does).

Performance

BenchmarkClusterAssign256Lights measures 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 ./render3d and go tool nsgo check -target linux,web ./render3d pass (350 tests)
  • go tool nsgo test . in packages/game passes (3397 passed, 3 skipped)
  • go run ./tools/changelog check passes
  • Tests compare GPU pixels with the CPU reference on the full and nsgotest.NoGPUCompute profiles (including a 256-light scene with a rig), and cover truncation, converters, invalid input, fallback allocations, and retained device loss.

@github-actions github-actions Bot added size/XL documentation Improvements or additions to documentation pkg/render3d area/agents enhancement New feature or request conflict Conflicts with the target branch; resolve before merging labels Nov 24, 2026
…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
houko force-pushed the feat/render3d-clustered-forward-382 branch from d929289 to 6d02dab Compare November 24, 2026 18:39
@houko
houko merged commit 2fbfede into main Nov 24, 2026
9 of 33 checks passed
@houko
houko deleted the feat/render3d-clustered-forward-382 branch November 24, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agents conflict Conflicts with the target branch; resolve before merging 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.

1 participant