Skip to content

Skip spot light shadow maps that don't overlap the view frustum - #1788

Closed
sunabouzu1991 wants to merge 1 commit into
vsg-dev:masterfrom
sunabouzu1991:empty-spot-shadow-bounds
Closed

sunabouzu1991 wants to merge 1 commit into
vsg-dev:masterfrom
sunabouzu1991:empty-spot-shadow-bounds

Conversation

@sunabouzu1991

Copy link
Copy Markdown
Contributor

Description

For a spot light with shadows, ViewDependentState::traverse() fits the shadow map to the part of the view frustum (up to maxShadowDistance) that is inside the light's frustum. When they don't overlap the bounds end up with min > max, which isn't checked. If the bounds are empty the scale in tweakedOrthographic is 2 / -inf, so the shadow map is rendered with a degenerate projection:

  • the frustum planes are all the same or NaN, so next to nothing is culled
  • State::lodDistance() is 1.414 for every sphere that passes, so every PagedLOD requests its high resolution child

I came across this with a paged 16km x 16km terrain, a small spot light and the camera 2.7km away from it. The DatabasePager went on loading tiles for as long as I left it running (12314 active PagedLOD after 70 seconds, and around 80 without the light).

This PR doesn't render the shadow map in this case. The light data keeps its layout, the matrix maps all positions to outside the shadow map.

How Has This Been Tested?

I added a --paged test case to vsgspotshadow: https://github.com/sunabouzu1991/vsgExamples/tree/spot-shadow-paged. It has 1024 PagedLOD tiles and counts how many of them are read in high resolution, with a spot light 500m from the camera and maxShadowDistance of 60m.

master (502d1cc) this PR
vsgspotshadow --paged 1024 of 1024 10 of 1024
vsgspotshadow --paged --no-shadows 10 of 1024 10 of 1024

With the original vsgspotshadow scene (maxShadowDistance of 60m) the light data is the same before and after. Adding two more shadowed spot lights that are out of range in front of the visible one doesn't change the image. No validation errors.

Linux, GTX 1650, hard shadows.

🤖 Generated with Claude Code

The bounds were empty in this case, which gave the shadow map a degenerate projection
with a frustum that culls nothing and makes every PagedLOD request its high resolution child.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@robertosfield

Copy link
Copy Markdown
Collaborator

I have done a first pass review and this doesn't look like an efficient way to resolve the problem at all.

Far better is to cull the light source before it ever gets processed by ViewDependentState. I will review the RecordTraversal::apply(SpotLight& ) and apply(PointLight&) code with a view to adding culling against the view frustum and maxShadowDistance.

@robertosfield

Copy link
Copy Markdown
Collaborator

I have merged your changes to vsgspotshadow and try this out as a test case.

@sunabouzu1991

Copy link
Copy Markdown
Contributor Author

Thanks, culling the light before ViewDependentState makes sense, I'm happy to close this in favour of that. One thing to watch: a spot light beyond maxShadowDistance can still light visible surfaces, so I think that distance should only drop its shadow map, while the light itself is culled against the view frustum. The clipped bounds can also be finite but inverted (min > max), not only the initial empty box. I can test your change with my scene when it's ready.

@robertosfield

Copy link
Copy Markdown
Collaborator

I think we'll need to 2 cull tests - one potentially in the RecordTraversal that culls the light against the frustum to see if that light should be entered into the lightData passed to GPU for lighting, and another within ViewDependentState against the shadow extents to see if that light might affect the shadows.

Potentially both could be done within the ViewDependentState. I will ponder on this today.

@robertosfield

Copy link
Copy Markdown
Collaborator

To help with testing I have merged this PR as branch:

https://github.com/vsg-dev/VulkanSceneGraph/tree/sunabouzu1991-empty-spot-shadow-bounds

@robertosfield

Copy link
Copy Markdown
Collaborator

I have invested the issue that this PR attempts to solve and found that the problem goes far earlier in the sequence of operations that are down for both PointLight and SpotLight. So I'll close this PR as it's replaced by the more complete fix in #1790.

The fix I went for was to changed RecordTraversal to cull PointLight and SpotLight based on the radius of the influence that their illumination will cover, and to change ViewDepenedentState::traverse(..) so that shadow map culling is done and if any shadow maps are culled the number of shadow maps is adjusted.

This approach reduces the number of lights that are occupying the lightData passed to the GPU and the number of shadow map slots that are used. This will help performance and visual quality in complex scenes.

@sunabouzu1991
sunabouzu1991 deleted the empty-spot-shadow-bounds branch December 3, 2026 04:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants