Skip to content

Commit 54e1597

Browse files
SSAO: reworked temporal accumulation shader
1 parent 6065b29 commit 54e1597

File tree

1 file changed

+38
-47
lines changed

1 file changed

+38
-47
lines changed

Shaders/PostProcess/ScreenSpaceAmbientOcclusion/private/SSAO_ComputeTemporalAccumulation.fx

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,9 @@ float2 LoadMotion(int2 PixelCoord)
7373
return g_TextureMotion.Load(int3(PixelCoord, 0)) * F3NDC_XYZ_TO_UVD_SCALE.xy;
7474
}
7575

76-
bool IsDepthSimilar(float CurrDepth, float PrevDepth)
76+
float IsCameraZSimilar(float CurrCamZ, float PrevCamZ)
7777
{
78-
float LinearDepthCurr = DepthToCameraZ(CurrDepth, g_PrevCamera.mProj);
79-
float LinearDepthPrev = DepthToCameraZ(PrevDepth, g_PrevCamera.mProj);
80-
return abs(1.0 - LinearDepthCurr / LinearDepthPrev) < SSAO_DISOCCLUSION_DEPTH_THRESHOLD;
81-
}
82-
83-
bool IsInsideScreenMinusOne(int2 PixelCoord, int2 Dimension)
84-
{
85-
return PixelCoord.x > 0 &&
86-
PixelCoord.y > 0 &&
87-
PixelCoord.x < (Dimension.x - 1) &&
88-
PixelCoord.y < (Dimension.y - 1);
78+
return abs(1.0 - CurrCamZ / PrevCamZ) < SSAO_DISOCCLUSION_DEPTH_THRESHOLD ? 1.0 : 0.0;
8979
}
9080

9181
PixelStatistic ComputePixelStatistic(int2 PixelCoord)
@@ -114,45 +104,46 @@ PixelStatistic ComputePixelStatistic(int2 PixelCoord)
114104

115105
ProjectionDesc ComputeReprojection(float2 PrevPos, float CurrDepth)
116106
{
117-
ProjectionDesc Desc;
118-
119-
int2 PrevPosi = int2(PrevPos - 0.5);
120-
float x = frac(PrevPos.x + 0.5);
121-
float y = frac(PrevPos.y + 0.5);
107+
float CurrCamZ = DepthToCameraZ(CurrDepth, g_CurrCamera.mProj);
122108

123-
float Weight[4];
124-
Weight[0] = (1.0 - x) * (1.0 - y);
125-
Weight[1] = x * (1.0 - y);
126-
Weight[2] = (1.0 - x) * y;
127-
Weight[3] = x * y;
128-
129-
{
130-
for (int SampleIdx = 0; SampleIdx < 4; ++SampleIdx)
131-
{
132-
int2 Location = PrevPosi + int2(SampleIdx & 0x01, SampleIdx >> 1);
133-
float PrevDepth = LoadPrevDepth(Location);
134-
Weight[SampleIdx] *= float(IsDepthSimilar(CurrDepth, PrevDepth));
135-
Weight[SampleIdx] *= float(IsInsideScreenMinusOne(Location, int2(g_CurrCamera.f4ViewportSize.xy)));
136-
}
137-
}
138-
139-
float WeightSum = 0.0;
140-
float OcclusionSum = 0.0;
141-
float HistorySum = 0.0;
109+
int4 FetchCoords;
110+
float4 Weights;
111+
GetBilinearSamplingInfoUC(PrevPos, int2(g_CurrCamera.f4ViewportSize.xy), FetchCoords, Weights);
112+
113+
float PrevCamZ00 = DepthToCameraZ(LoadPrevDepth(FetchCoords.xy), g_PrevCamera.mProj);
114+
float PrevCamZ10 = DepthToCameraZ(LoadPrevDepth(FetchCoords.zy), g_PrevCamera.mProj);
115+
float PrevCamZ01 = DepthToCameraZ(LoadPrevDepth(FetchCoords.xw), g_PrevCamera.mProj);
116+
float PrevCamZ11 = DepthToCameraZ(LoadPrevDepth(FetchCoords.zw), g_PrevCamera.mProj);
117+
118+
Weights.x *= IsCameraZSimilar(CurrCamZ, PrevCamZ00);
119+
Weights.y *= IsCameraZSimilar(CurrCamZ, PrevCamZ10);
120+
Weights.z *= IsCameraZSimilar(CurrCamZ, PrevCamZ01);
121+
Weights.w *= IsCameraZSimilar(CurrCamZ, PrevCamZ11);
142122

123+
float TotalWeight = dot(Weights, float4(1.0, 1.0, 1.0, 1.0));
124+
125+
ProjectionDesc Desc;
126+
Desc.Occlusion = 1.0;
127+
Desc.History = 1.0;
128+
Desc.IsSuccess = TotalWeight > 0.01 && !g_SSAOAttribs.ResetAccumulation;
129+
if (Desc.IsSuccess)
143130
{
144-
for (int SampleIdx = 0; SampleIdx < 4; ++SampleIdx)
145-
{
146-
int2 Location = PrevPosi + int2(SampleIdx & 0x01, SampleIdx >> 1);
147-
OcclusionSum += Weight[SampleIdx] * LoadPrevOcclusion(Location);
148-
HistorySum += Weight[SampleIdx] * min(16.0, LoadHistory(Location) + 1.0);;
149-
WeightSum += Weight[SampleIdx];
150-
}
131+
float4 PrevOcclusion = float4(
132+
LoadPrevOcclusion(FetchCoords.xy),
133+
LoadPrevOcclusion(FetchCoords.zy),
134+
LoadPrevOcclusion(FetchCoords.xw),
135+
LoadPrevOcclusion(FetchCoords.zw)
136+
);
137+
float4 History = float4(
138+
LoadHistory(FetchCoords.xy),
139+
LoadHistory(FetchCoords.zy),
140+
LoadHistory(FetchCoords.xw),
141+
LoadHistory(FetchCoords.zw)
142+
);
143+
History = min(float4(16.0, 16.0, 16.0, 16.0), History + float4(1.0, 1.0, 1.0, 1.0));
144+
Desc.Occlusion = dot(PrevOcclusion, Weights) / TotalWeight;
145+
Desc.History = dot(History, Weights) / TotalWeight;
151146
}
152-
153-
Desc.IsSuccess = WeightSum > 0.0 && !g_SSAOAttribs.ResetAccumulation;
154-
Desc.Occlusion = Desc.IsSuccess ? OcclusionSum / WeightSum : 1.0;
155-
Desc.History = Desc.IsSuccess ? HistorySum / WeightSum : 1.0;
156147

157148
return Desc;
158149
}

0 commit comments

Comments
 (0)