Skip to content

Commit 87dd760

Browse files
SSR: a few minor improvements
1 parent 860fe49 commit 87dd760

File tree

5 files changed

+61
-70
lines changed

5 files changed

+61
-70
lines changed

PostProcess/ScreenSpaceReflection/src/ScreenSpaceReflection.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ namespace HLSL
4545
} // namespace HLSL
4646

4747
static DILIGENT_CONSTEXPR DepthStencilStateDesc DSS_StencilWrite{
48-
False, // DepthEnable
49-
False, // DepthWriteEnable
50-
COMPARISON_FUNC_LESS, // DepthFunc
51-
True, // StencilEnable
52-
0xFF, // StencilReadMask
53-
0xFF, // StencilWriteMask
48+
False, // DepthEnable
49+
False, // DepthWriteEnable
50+
COMPARISON_FUNC_ALWAYS, // DepthFunc
51+
True, // StencilEnable
52+
0xFF, // StencilReadMask
53+
0xFF, // StencilWriteMask
5454
{
5555
STENCIL_OP_KEEP, // StencilFailOp
5656
STENCIL_OP_KEEP, // StencilDepthFailOp
@@ -60,12 +60,12 @@ static DILIGENT_CONSTEXPR DepthStencilStateDesc DSS_StencilWrite{
6060
};
6161

6262
static DILIGENT_CONSTEXPR DepthStencilStateDesc DSS_StencilReadComparisonEqual{
63-
False, // DepthEnable
64-
False, // DepthWriteEnable
65-
COMPARISON_FUNC_LESS, // DepthFunc
66-
True, // StencilEnable
67-
0xFF, // StencilReadMask
68-
0xFF, // StencilWriteMask
63+
False, // DepthEnable
64+
False, // DepthWriteEnable
65+
COMPARISON_FUNC_ALWAYS, // DepthFunc
66+
True, // StencilEnable
67+
0xFF, // StencilReadMask
68+
0xFF, // StencilWriteMask
6969
{
7070
STENCIL_OP_KEEP, // StencilFailOp
7171
STENCIL_OP_KEEP, // StencilDepthFailOp

Shaders/PostProcess/ScreenSpaceReflection/private/SSR_ComputeBilateralCleanup.fx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ float4 ComputeBilateralCleanupPS(in FullScreenTriangleVSOutput VSOut) : SV_Targe
8080
{
8181
float SampledLinearDepth = DepthToCameraZ(SampledDepth, g_Camera.mProj);
8282
float WeightS = exp(-0.5 * dot(float2(x, y), float2(x, y)) / (Sigma * Sigma));
83-
float WeightZ = exp(-abs(LinearDepth - SampledLinearDepth) / (SSR_BILATERAL_SIGMA_DEPTH * abs(dot(float2(x, y), GradDepth)+1.e-6)));
83+
float WeightZ = exp(-abs(LinearDepth - SampledLinearDepth) / (SSR_BILATERAL_SIGMA_DEPTH * (abs(dot(float2(x, y), GradDepth)) + 1e-6)));
8484
float WeightN = pow(max(0.0, dot(NormalWS, SampledNormalWS)), SSR_BILATERAL_SIGMA_NORMAL);
8585
float Weight = WeightS * WeightN * WeightZ;
8686

Shaders/PostProcess/ScreenSpaceReflection/private/SSR_ComputeDownsampledStencilMask.fx

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,50 @@ cbuffer cbScreenSpaceReflectionAttribs
1010
Texture2D<float> g_TextureRoughness;
1111
Texture2D<float> g_TextureDepth;
1212

13-
float SampleRoughness(uint2 Location, uint2 Offset, uint2 Dimension)
13+
void UpdateClosestDepthAndMaxRoughness(int2 Location, int2 Dimension, inout float MinDepth, inout float MaxRoughness)
1414
{
15-
uint2 Position = Location + Offset;
16-
if (Position.x >= Dimension.x || Position.y >= Dimension.y)
17-
{
18-
return g_TextureRoughness.Load(int3(Location, 0));
19-
}
20-
else
21-
{
22-
return g_TextureRoughness.Load(int3(Position, 0));
23-
}
24-
}
15+
Location = ClampScreenCoord(Location, Dimension);
2516

26-
float SampleDepth(uint2 Location, uint2 Offset, uint2 Dimension)
27-
{
28-
uint2 Position = Location + Offset;
29-
if (Position.x >= Dimension.x || Position.y >= Dimension.y)
30-
{
31-
return g_TextureDepth.Load(int3(Location, 0));
32-
}
33-
else
34-
{
35-
return g_TextureDepth.Load(int3(Position, 0));
36-
}
17+
float Depth = g_TextureDepth.Load(int3(Location, 0));
18+
float Roughness = g_TextureRoughness.Load(int3(Location, 0));
19+
20+
MinDepth = ClosestDepth(MinDepth, Depth);
21+
MaxRoughness = max(MaxRoughness, Roughness);
3722
}
3823

3924
void ComputeDownsampledStencilMaskPS(in FullScreenTriangleVSOutput VSOut)
4025
{
41-
uint2 RemappedPosition = uint2(2.0 * floor(VSOut.f4PixelPos.xy));
26+
int2 RemappedPosition = int2(2.0 * floor(VSOut.f4PixelPos.xy));
4227

43-
uint2 TextureDimension;
28+
int2 TextureDimension;
4429
g_TextureDepth.GetDimensions(TextureDimension.x, TextureDimension.y);
4530

4631
float MinDepth = DepthFarPlane;
4732
float MaxRoughness = 0.0f;
33+
34+
UpdateClosestDepthAndMaxRoughness(RemappedPosition + int2(0, 0), TextureDimension, MinDepth, MaxRoughness);
35+
UpdateClosestDepthAndMaxRoughness(RemappedPosition + int2(1, 0), TextureDimension, MinDepth, MaxRoughness);
36+
UpdateClosestDepthAndMaxRoughness(RemappedPosition + int2(0, 1), TextureDimension, MinDepth, MaxRoughness);
37+
UpdateClosestDepthAndMaxRoughness(RemappedPosition + int2(1, 1), TextureDimension, MinDepth, MaxRoughness);
4838

49-
for (uint SampleIdx = 0u; SampleIdx < 4u; ++SampleIdx)
50-
{
51-
uint2 Offset = uint2(SampleIdx & 0x01u, SampleIdx >> 1u);
52-
MinDepth = ClosestDepth(MinDepth, SampleDepth(RemappedPosition, Offset, TextureDimension));
53-
MaxRoughness = max(MaxRoughness, SampleRoughness(RemappedPosition, Offset, TextureDimension));
54-
}
55-
56-
bool IsWidthOdd = (TextureDimension.x & 1u) != 0u;
57-
bool IsHeightOdd = (TextureDimension.y & 1u) != 0u;
39+
bool IsWidthOdd = (TextureDimension.x & 1) != 0;
40+
bool IsHeightOdd = (TextureDimension.y & 1) != 0;
5841

5942
if (IsWidthOdd)
6043
{
61-
for (uint SampleIdx = 0u; SampleIdx < 2u; ++SampleIdx)
62-
{
63-
uint2 Offset = uint2(2u, SampleIdx);
64-
MinDepth = ClosestDepth(MinDepth, SampleDepth(RemappedPosition, Offset, TextureDimension));
65-
MaxRoughness = max(MaxRoughness, SampleRoughness(RemappedPosition, Offset, TextureDimension));
66-
}
44+
UpdateClosestDepthAndMaxRoughness(RemappedPosition + int2(2, 0), TextureDimension, MinDepth, MaxRoughness);
45+
UpdateClosestDepthAndMaxRoughness(RemappedPosition + int2(2, 1), TextureDimension, MinDepth, MaxRoughness);
6746
}
6847

6948
if (IsHeightOdd)
7049
{
71-
for (uint SampleIdx = 0u; SampleIdx < 2u; ++SampleIdx)
72-
{
73-
uint2 Offset = uint2(SampleIdx, 2);
74-
MinDepth = ClosestDepth(MinDepth, SampleDepth(RemappedPosition, Offset, TextureDimension));
75-
MaxRoughness = max(MaxRoughness, SampleRoughness(RemappedPosition, Offset, TextureDimension));
76-
}
50+
UpdateClosestDepthAndMaxRoughness(RemappedPosition + int2(0, 2), TextureDimension, MinDepth, MaxRoughness);
51+
UpdateClosestDepthAndMaxRoughness(RemappedPosition + int2(1, 2), TextureDimension, MinDepth, MaxRoughness);
7752
}
7853

7954
if (IsWidthOdd && IsHeightOdd)
8055
{
81-
MinDepth = ClosestDepth(MinDepth, SampleDepth(RemappedPosition, uint2(2, 2), TextureDimension));
82-
MaxRoughness = max(MaxRoughness, SampleRoughness(RemappedPosition, uint2(2, 2), TextureDimension));
56+
UpdateClosestDepthAndMaxRoughness(RemappedPosition + int2(2, 2), TextureDimension, MinDepth, MaxRoughness);
8357
}
8458

8559
if (!IsReflectionSample(MaxRoughness, MinDepth, g_SSRAttribs.RoughnessThreshold))

Shaders/PostProcess/ScreenSpaceReflection/private/SSR_ComputeIntersection.fx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,15 @@ float3 SampleRadiance(int2 PixelCoord)
7575
return g_TextureRadiance.Load(int3(PixelCoord, 0));
7676
}
7777

78-
void InitialAdvanceRay(float3 Origin, float3 Direction, float3 InvDirection, float2 CurrentMipResolution, float2 InvCurrentMipResolution, float2 FloorOffset, float2 UVOffset, out float3 Position, out float CurrentT)
78+
void InitialAdvanceRay(float3 Origin,
79+
float3 Direction,
80+
float3 InvDirection,
81+
float2 CurrentMipResolution,
82+
float2 InvCurrentMipResolution,
83+
float2 FloorOffset,
84+
float2 UVOffset,
85+
out float3 Position,
86+
out float CurrentT)
7987
{
8088
float2 CurrentMipPosition = CurrentMipResolution * Origin.xy;
8189

@@ -89,12 +97,21 @@ void InitialAdvanceRay(float3 Origin, float3 Direction, float3 InvDirection, flo
8997
Position = Origin + CurrentT * Direction;
9098
}
9199

92-
bool AdvanceRay(float3 Origin, float3 Direction, float3 InvDirection, float2 CurrentMipPosition, float2 InvCurrentMipResolution, float2 FloorOffset, float2 UVOffset, float SurfaceZ, inout float3 Position, inout float CurrentT)
100+
bool AdvanceRay(float3 Origin,
101+
float3 Direction,
102+
float3 InvDirection,
103+
float2 CurrentMipPosition,
104+
float2 InvCurrentMipResolution,
105+
float2 FloorOffset,
106+
float2 UVOffset,
107+
float SurfaceDepth,
108+
inout float3 Position,
109+
inout float CurrentT)
93110
{
94111
// Create boundary planes
95112
float2 XYPlane = floor(CurrentMipPosition) + FloorOffset;
96113
XYPlane = XYPlane * InvCurrentMipResolution + UVOffset;
97-
float3 BoundaryPlanes = float3(XYPlane, SurfaceZ);
114+
float3 BoundaryPlanes = float3(XYPlane, SurfaceDepth);
98115

99116
// Intersect ray with the half box that is pointing away from the ray origin.
100117
// o + d * t = p' => t = (p' - o) / d
@@ -112,10 +129,10 @@ bool AdvanceRay(float3 Origin, float3 Direction, float3 InvDirection, float2 Cur
112129

113130
#if SSR_OPTION_INVERTED_DEPTH
114131
// Larger z means closer to the camera.
115-
bool AboveSurface = SurfaceZ < Position.z;
132+
bool AboveSurface = SurfaceDepth < Position.z;
116133
#else
117134
// Smaller z means closer to the camera.
118-
bool AboveSurface = SurfaceZ > Position.z;
135+
bool AboveSurface = SurfaceDepth > Position.z;
119136
#endif
120137

121138
// Decide whether we are able to advance the ray until we hit the xy boundaries or if we had to clamp it at the surface.
@@ -165,8 +182,8 @@ float3 HierarchicalRaymarch(float3 Origin, float3 Direction, float2 ScreenSize,
165182
while (Idx < MaxTraversalIntersections && CurrentMip >= MostDetailedMip)
166183
{
167184
float2 CurrentMipPosition = CurrentMipResolution * Position.xy;
168-
float SurfaceZ = SampleDepthHierarchy(int2(CurrentMipPosition), CurrentMip);
169-
bool SkippedTile = AdvanceRay(Origin, Direction, InvDirection, CurrentMipPosition, InvCurrentMipResolution, FloorOffset, UVOffset, SurfaceZ, Position, CurrentT);
185+
float SurfaceDepth = SampleDepthHierarchy(int2(CurrentMipPosition), CurrentMip);
186+
bool SkippedTile = AdvanceRay(Origin, Direction, InvDirection, CurrentMipPosition, InvCurrentMipResolution, FloorOffset, UVOffset, SurfaceDepth, Position, CurrentT);
170187

171188
// Don't increase mip further than this because we did not generate it
172189
bool NextMipIsOutOfRange = SkippedTile && (CurrentMip >= SSR_DEPTH_HIERARCHY_MAX_MIP);

Shaders/PostProcess/ScreenSpaceReflection/private/SSR_ComputeTemporalAccumulation.fx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ float2 ComputeReflectionHitPosition(int2 PixelCoord, float Depth)
118118
// TODO: Use normals to compute disocclusion
119119
float ComputeDisocclusion(float CurrDepth, float PrevDepth)
120120
{
121-
float LinearDepthCurr = DepthToCameraZ(CurrDepth, g_CurrCamera.mProj);
122-
float LinearDepthPrev = DepthToCameraZ(PrevDepth, g_PrevCamera.mProj);
123-
return exp(-abs(LinearDepthPrev - LinearDepthCurr) / LinearDepthCurr);
121+
float LinearDepthCurr = abs(DepthToCameraZ(CurrDepth, g_CurrCamera.mProj));
122+
float LinearDepthPrev = abs(DepthToCameraZ(PrevDepth, g_PrevCamera.mProj));
123+
return exp(-abs(LinearDepthPrev - LinearDepthCurr) / max(max(LinearDepthCurr, LinearDepthPrev), 1e-6));
124124
}
125125

126126
// Welford's online algorithm:

0 commit comments

Comments
 (0)