-
Notifications
You must be signed in to change notification settings - Fork 130
feat(vr): Screen Space Shadows VR optimization #2004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
vrnord
wants to merge
3
commits into
community-shaders:dev
Choose a base branch
from
vrnord:pr/vr-sss-optimization
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
76 changes: 74 additions & 2 deletions
76
features/Screen-Space Shadows/Shaders/ScreenSpaceShadows/ScreenSpaceShadows.hlsli
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,82 @@ | ||
| // Screen Space Shadows consumption helper. | ||
| // Non-VR: depth-weighted 4-sample Poisson blur for spatial denoising. | ||
| // VR: direct Load — the Poisson blur's per-pixel noise rotation is | ||
| // screen-position-dependent, causing shadows to shift on camera movement. | ||
| // Without TAA to average out the rotation noise, the instability hits | ||
| // the final output directly. Matches the stable v1.2 VR implementation. | ||
|
|
||
| #include "Common/Math.hlsli" | ||
|
|
||
| namespace ScreenSpaceShadows | ||
| { | ||
| Texture2D<unorm half> ScreenSpaceShadowsTexture : register(t45); | ||
|
|
||
| float4 GetBlurWeights(float4 depths, float centerDepth) | ||
| { | ||
| centerDepth += 1.0; | ||
| float depthSharpness = saturate((1024.0 * 1024.0) / (centerDepth * centerDepth)); | ||
| float4 depthDifference = (depths - centerDepth) * depthSharpness; | ||
| return exp2(-depthDifference * depthDifference); | ||
| } | ||
|
|
||
| float GetScreenSpaceShadow(float3 screenPosition, float2 uv, float noise, uint eyeIndex) | ||
| { | ||
| return ScreenSpaceShadowsTexture.Load(int3(int2(screenPosition.xy + 0.5f), 0)).x; | ||
| #if defined(VR) | ||
| // VR: direct sample, no spatial blur. The Poisson blur's per-pixel noise | ||
| // rotation is screen-position-dependent — camera movement changes the | ||
| // rotation angle for the same world surface, causing shadows to visually | ||
| // shift. Without TAA to average out the rotation noise, the per-frame | ||
| // instability hits the final output directly. Direct Load avoids this. | ||
| // Matches the stable v1.2 VR implementation. | ||
| return ScreenSpaceShadowsTexture.Load(int3(screenPosition.xy, 0)); | ||
| #else | ||
| // Flat: depth-weighted 4-sample Poisson blur for spatial denoising. | ||
| // Rotated per-pixel by screen-space noise to break structured patterns. | ||
| // TAA averages out the rotation noise across frames. | ||
| noise *= Math::TAU; | ||
|
|
||
| float2x2 rotationMatrix = float2x2(cos(noise), sin(noise), -sin(noise), cos(noise)); | ||
|
|
||
| float4 shadowSamples = 0; | ||
| float4 depthSamples = 0; | ||
|
|
||
| # if defined(DEFERRED) && !defined(DO_ALPHA_TEST) | ||
| depthSamples[0] = screenPosition.z; | ||
| # else | ||
| depthSamples[0] = SharedData::DepthTexture.Load(int3(screenPosition.xy, 0)).x; | ||
| # endif | ||
|
|
||
| shadowSamples[0] = ScreenSpaceShadowsTexture.Load(int3(screenPosition.xy, 0)); | ||
|
|
||
| static const float2 BlurOffsets[3] = { | ||
| float2(-0.6720635096678028f, 0.6601738628451107f), | ||
| float2(0.6110340335380645f, 0.5269905984201742f), | ||
| float2(0.20239029763403027f, -0.7841160574831084f), | ||
| }; | ||
|
|
||
| [unroll] for (uint i = 1; i < 4; i++) | ||
| { | ||
| float2 offset = mul(BlurOffsets[i - 1], rotationMatrix) * 0.0025; | ||
|
|
||
| float2 sampleUV = uv + offset; | ||
| sampleUV = saturate(sampleUV); | ||
|
|
||
| int3 sampleCoord = SharedData::ConvertUVToSampleCoord(sampleUV, eyeIndex); | ||
|
|
||
| depthSamples[i] = SharedData::DepthTexture.Load(sampleCoord).x; | ||
| shadowSamples[i] = ScreenSpaceShadowsTexture.Load(sampleCoord); | ||
| } | ||
|
|
||
| depthSamples = SharedData::GetScreenDepths(depthSamples); | ||
|
|
||
| float4 blurWeights = GetBlurWeights(depthSamples, depthSamples[0]); | ||
| float shadow = dot(shadowSamples, blurWeights); | ||
|
|
||
| float blurWeightsTotal = dot(blurWeights, 1.0); | ||
| [flatten] if (blurWeightsTotal > 0.0) | ||
| shadow = shadow / blurWeightsTotal; | ||
|
|
||
| return shadow; | ||
| #endif | ||
| } | ||
| } | ||
| } |
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
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.