Skip to content

Commit 5b33a3f

Browse files
SSR: fixed temporal accumulation to work consistently with reverse depth
1 parent ed852b9 commit 5b33a3f

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

Shaders/PostProcess/ScreenSpaceReflection/private/SSR_ComputeTemporalAccumulation.fx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,18 @@ float2 ComputeReflectionHitPosition(int2 PixelCoord, float Depth)
116116
}
117117

118118
// TODO: Use normals to compute disocclusion
119-
float ComputeDisocclusion(float CurrDepth, float PrevDepth)
119+
float ComputeDisocclusionZ(float CurrCameraZ, float PrevCameraZ)
120120
{
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));
121+
CurrCameraZ = abs(CurrCameraZ);
122+
PrevCameraZ = abs(PrevCameraZ);
123+
return exp(-abs(CurrCameraZ - PrevCameraZ) / max(max(CurrCameraZ, PrevCameraZ), 1e-6));
124+
}
125+
126+
float ComputeDisocclusionD(float CurrDepth, float PrevDepth)
127+
{
128+
float CurrCameraZ = DepthToCameraZ(CurrDepth, g_CurrCamera.mProj);
129+
float PrevCameraZ = DepthToCameraZ(PrevDepth, g_PrevCamera.mProj);
130+
return ComputeDisocclusionZ(CurrCameraZ, PrevCameraZ);
124131
}
125132

126133
// Welford's online algorithm:
@@ -154,7 +161,7 @@ ProjectionDesc ComputeReprojection(float2 PrevPos, float CurrDepth)
154161
{
155162
ProjectionDesc Desc;
156163
Desc.PrevCoord = PrevPos;
157-
Desc.IsSuccess = ComputeDisocclusion(CurrDepth, SamplePrevDepth(int2(PrevPos))) > SSR_DISOCCLUSION_THRESHOLD;
164+
Desc.IsSuccess = ComputeDisocclusionD(CurrDepth, SamplePrevDepth(int2(PrevPos))) > SSR_DISOCCLUSION_THRESHOLD;
158165
Desc.Color = SamplePrevRadianceLinear(Desc.PrevCoord);
159166

160167
if (!Desc.IsSuccess)
@@ -167,7 +174,7 @@ ProjectionDesc ComputeReprojection(float2 PrevPos, float CurrDepth)
167174
{
168175
float2 Location = PrevPos + float2(x, y);
169176
float PrevDepth = SamplePrevDepthLinear(Location);
170-
float Weight = ComputeDisocclusion(CurrDepth, PrevDepth);
177+
float Weight = ComputeDisocclusionD(CurrDepth, PrevDepth);
171178
if (Weight > Disocclusion)
172179
{
173180
Disocclusion = Weight;
@@ -197,29 +204,29 @@ ProjectionDesc ComputeReprojection(float2 PrevPos, float CurrDepth)
197204
{
198205
int2 Location = PrevPosi + int2(SampleIdx & 0x01, SampleIdx >> 1);
199206
float PrevDepth = SamplePrevDepth(Location);
200-
bool IsValidSample = ComputeDisocclusion(CurrDepth, PrevDepth) > (SSR_DISOCCLUSION_THRESHOLD / 2.0);
207+
bool IsValidSample = ComputeDisocclusionD(CurrDepth, PrevDepth) > (SSR_DISOCCLUSION_THRESHOLD / 2.0);
201208
Weight[SampleIdx] *= float(IsValidSample);
202209
}
203210
}
204211

205-
float WeightSum = 0.0;
206-
float DepthSum = 0.0;
212+
float WeightSum = 0.0;
213+
float WeightedCamZ = 0.0;
207214
float4 ColorSum = float4(0.0, 0.0, 0.0, 0.0);
208215

209216
{
210217
for (int SampleIdx = 0; SampleIdx < 4; ++SampleIdx)
211218
{
212219
int2 Location = PrevPosi + int2(SampleIdx & 0x01, SampleIdx >> 1);
213-
ColorSum += Weight[SampleIdx] * SamplePrevRadiance(Location);
214-
DepthSum += Weight[SampleIdx] * SamplePrevDepth(Location);
215-
WeightSum += Weight[SampleIdx];
220+
ColorSum += Weight[SampleIdx] * SamplePrevRadiance(Location);
221+
WeightedCamZ += Weight[SampleIdx] * DepthToCameraZ(SamplePrevDepth(Location), g_PrevCamera.mProj);
222+
WeightSum += Weight[SampleIdx];
216223
}
217224
}
218225

219-
DepthSum /= max(WeightSum, 1.0e-6f);
220-
ColorSum /= max(WeightSum, 1.0e-6f);
226+
WeightedCamZ /= max(WeightSum, 1e-6);
227+
ColorSum /= max(WeightSum, 1e-6);
221228

222-
Desc.IsSuccess = ComputeDisocclusion(CurrDepth, DepthSum) > SSR_DISOCCLUSION_THRESHOLD;
229+
Desc.IsSuccess = ComputeDisocclusionZ(DepthToCameraZ(CurrDepth, g_CurrCamera.mProj), WeightedCamZ) > SSR_DISOCCLUSION_THRESHOLD;
223230
Desc.Color = ColorSum;
224231
}
225232

0 commit comments

Comments
 (0)