perf(volumetric shadows): optimise data and fix cascades#2182
Conversation
📝 WalkthroughWalkthroughReplaces a GPU compute-shader per-geometry copy with a CPU-populated directional-shadow structured buffer and SRV (t98); removes the CopyShadowDataCS shader and per-geometry buffer, and updates shaders and call sites to consume DirectionalShadowLightData via FrameBuffer::GetShadowDepth. Changes
Sequence Diagram(s)sequenceDiagram
participant CPU as rgba(70,130,180,0.5) CPU / Deferred
participant GPU as rgba(34,139,34,0.5) GPU (StructuredBuffer t98)
participant Shader as rgba(218,165,32,0.5) Volumetric Shader
participant DepthUtil as rgba(147,112,219,0.5) FrameBuffer::GetShadowDepth
CPU->>CPU: Render shadow maps
CPU->>CPU: CopyShadowLightData()\nPopulate DirectionalShadowLightData
CPU->>GPU: Map/Memcpy/Unmap structured buffer\nBind SRV to t98
Shader->>GPU: Read DirectionalShadowLights[0]
Shader->>DepthUtil: GetShadowDepth(positionWS, eyeIndex)
DepthUtil-->>Shader: NDC depth for cascade checks
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
No actionable suggestions for changed features. |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/Deferred.cpp`:
- Around line 600-609: In SetShadowCascadeParameters, the loop iterates over
lightData.shadowmapDescriptors.size() but writes into dd.ShadowProj and
dd.InvShadowProj which only have two entries, causing potential out-of-bounds
writes; fix by clamping the iteration count to the fixed cascade array size
(e.g., compute count = std::min(lightData.shadowmapDescriptors.size(),
size_t(2)) or use the known constant for the cascade count) and iterate up to
that count when populating dd.ShadowProj[i] and dd.InvShadowProj[i].
In `@src/Features/VolumetricShadows.cpp`:
- Around line 273-274: The current binding logic always binds shadowCopySRV even
when there is no source shadowView this frame, causing stale VSM data; change
the logic around shadowCopySRV/shadowView so that if shadowView is null you bind
nullptr instead of shadowCopySRV. Concretely, in the block that computes srv
before calling context->PSSetShaderResources(18, 1, &srv), set srv to nullptr
when shadowView is null; otherwise choose shadowCopySRV if available, else
shadowView. Keep using the existing context->PSSetShaderResources call and
ensure no leftover SRV is rebound when there is no source this frame.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 35a677fa-0d5d-4094-9544-4b187916eb37
📒 Files selected for processing (9)
features/Volumetric Shadows/Shaders/VolumetricShadows/CopyShadowDataCS.hlslfeatures/Volumetric Shadows/Shaders/VolumetricShadows/VolumetricShadows.hlslipackage/Shaders/Common/FrameBuffer.hlslipackage/Shaders/Common/ShadowSampling.hlslisrc/Deferred.cppsrc/Deferred.hsrc/Features/VolumetricShadows.cppsrc/Features/VolumetricShadows.hsrc/State.cpp
💤 Files with no reviewable changes (1)
- features/Volumetric Shadows/Shaders/VolumetricShadows/CopyShadowDataCS.hlsl
|
✅ A pre-release build is available for this PR: |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/Deferred.cpp (1)
598-609: Make the clamp type-agnostic sostd::mindeduction can’t bite later.Small nit:
std::min(lightData.shadowmapDescriptors.size(), static_cast<uint32_t>(std::size(dd.ShadowProj)))relies onshadowmapDescriptors.size()returning exactlyuint32_t. If CLib ever changes tosize_t(or this template is instantiated with a container whosesize()issize_t), template argument deduction fails to compile. Prefer an explicit template argument or a common-type cast.Proposed refactor
- const auto count = std::min(lightData.shadowmapDescriptors.size(), static_cast<uint32_t>(std::size(dd.ShadowProj))); - for (uint32_t i = 0; i < count; i++) { + const size_t count = std::min<size_t>(lightData.shadowmapDescriptors.size(), std::size(dd.ShadowProj)); + for (size_t i = 0; i < count; i++) {🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/Deferred.cpp` around lines 598 - 609, The std::min call in Deferred::SetShadowCascadeParameters uses mixed integer types which can break template deduction; make it type-agnostic by using a fixed common type (e.g., std::size_t) for the comparison and loop: compute count as std::min<std::size_t>(lightData.shadowmapDescriptors.size(), std::size(dd.ShadowProj)) and change the loop index to std::size_t (and cast to uint32_t where needed for APIs expecting that type), so the comparison and iteration no longer depend on container-specific size() return types.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/Deferred.cpp`:
- Around line 598-609: The std::min call in Deferred::SetShadowCascadeParameters
uses mixed integer types which can break template deduction; make it
type-agnostic by using a fixed common type (e.g., std::size_t) for the
comparison and loop: compute count as
std::min<std::size_t>(lightData.shadowmapDescriptors.size(),
std::size(dd.ShadowProj)) and change the loop index to std::size_t (and cast to
uint32_t where needed for APIs expecting that type), so the comparison and
iteration no longer depend on container-specific size() return types.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 12f5627f-c6f9-45a6-ae51-310b030b3934
📒 Files selected for processing (1)
src/Deferred.cpp
Summary by CodeRabbit