|
@@ -36,21 +36,23 @@ Vec3 unproject(Vec2 ndc, F32 depth)
|
|
|
|
|
|
|
|
F32 computeSpatialVariance(Vec2 uv)
|
|
F32 computeSpatialVariance(Vec2 uv)
|
|
|
{
|
|
{
|
|
|
- const F32 kernel[2][2] = {{1.0 / 4.0, 1.0 / 8.0}, {1.0 / 8.0, 1.0 / 16.0}};
|
|
|
|
|
- const I32 radius = 1;
|
|
|
|
|
|
|
+ const F32 radius = 2.0;
|
|
|
const Vec2 texelSize = 1.0 / u_unis.m_viewportSizef;
|
|
const Vec2 texelSize = 1.0 / u_unis.m_viewportSizef;
|
|
|
Vec2 sumMoments = Vec2(0.0);
|
|
Vec2 sumMoments = Vec2(0.0);
|
|
|
|
|
|
|
|
- for(I32 yy = -radius; yy <= radius; yy++)
|
|
|
|
|
|
|
+ for(F32 i = -radius; i <= radius; i += 1.0)
|
|
|
{
|
|
{
|
|
|
- for(I32 xx = -radius; xx <= radius; xx++)
|
|
|
|
|
- {
|
|
|
|
|
- const Vec2 newUv = uv + Vec2(xx, yy) * texelSize;
|
|
|
|
|
- const F32 k = kernel[abs(xx)][abs(yy)];
|
|
|
|
|
- sumMoments += textureLod(u_momentsAndHistoryLengthTex, u_linearAnyClampSampler, newUv, 0.0).xy * k;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ Vec2 newUv = uv;
|
|
|
|
|
+#if BLUR_ORIENTATION == 0
|
|
|
|
|
+ newUv.x += i * texelSize.x;
|
|
|
|
|
+#else
|
|
|
|
|
+ newUv.y += i * texelSize.y;
|
|
|
|
|
+#endif
|
|
|
|
|
+ sumMoments += textureLod(u_momentsAndHistoryLengthTex, u_linearAnyClampSampler, newUv, 0.0).xy;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ sumMoments *= 1.0 / (radius * 2.0 + 1.0);
|
|
|
|
|
+
|
|
|
return abs(sumMoments.y - sumMoments.x * sumMoments.x);
|
|
return abs(sumMoments.y - sumMoments.x * sumMoments.x);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -75,31 +77,35 @@ void main()
|
|
|
const Vec3 normalCenter = readNormalFromGBuffer(u_gbuffer2Tex, u_linearAnyClampSampler, uv);
|
|
const Vec3 normalCenter = readNormalFromGBuffer(u_gbuffer2Tex, u_linearAnyClampSampler, uv);
|
|
|
|
|
|
|
|
// Decide the amount of blurring
|
|
// Decide the amount of blurring
|
|
|
- const F32 varianceCenter = computeSpatialVariance(uv);
|
|
|
|
|
const F32 historyLength = textureLod(u_momentsAndHistoryLengthTex, u_linearAnyClampSampler, uv, 0.0).z;
|
|
const F32 historyLength = textureLod(u_momentsAndHistoryLengthTex, u_linearAnyClampSampler, uv, 0.0).z;
|
|
|
|
|
|
|
|
|
|
+ const F32 stableFrameCount = 4.0;
|
|
|
F32 sampleCount;
|
|
F32 sampleCount;
|
|
|
- if(historyLength < 2.0)
|
|
|
|
|
|
|
+ if(historyLength < stableFrameCount)
|
|
|
{
|
|
{
|
|
|
// Worst case
|
|
// Worst case
|
|
|
sampleCount = u_unis.m_maxSampleCount;
|
|
sampleCount = u_unis.m_maxSampleCount;
|
|
|
}
|
|
}
|
|
|
- else if(historyLength > 4.0 && varianceCenter < 0.0001)
|
|
|
|
|
- {
|
|
|
|
|
- // Best case
|
|
|
|
|
- sampleCount = u_unis.m_minSampleCount;
|
|
|
|
|
- }
|
|
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- // Every other case
|
|
|
|
|
|
|
+ const F32 varianceCenter = computeSpatialVariance(uv);
|
|
|
|
|
+
|
|
|
|
|
+ if(historyLength > stableFrameCount && varianceCenter < 0.01)
|
|
|
|
|
+ {
|
|
|
|
|
+ // Best case
|
|
|
|
|
+ sampleCount = u_unis.m_minSampleCount;
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ // Every other case
|
|
|
|
|
|
|
|
- F32 blur = varianceCenter * 100.0;
|
|
|
|
|
- blur = min(1.0, blur);
|
|
|
|
|
|
|
+ F32 blur = varianceCenter * 10.0;
|
|
|
|
|
+ blur = min(1.0, blur);
|
|
|
|
|
|
|
|
- sampleCount = mix(u_unis.m_minSampleCount, u_unis.m_maxSampleCount, blur);
|
|
|
|
|
|
|
+ sampleCount = mix(u_unis.m_minSampleCount, u_unis.m_maxSampleCount, blur);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- sampleCount = 32.0;
|
|
|
|
|
sampleCount = sampleCount / 2.0;
|
|
sampleCount = sampleCount / 2.0;
|
|
|
sampleCount = floor(sampleCount);
|
|
sampleCount = floor(sampleCount);
|
|
|
|
|
|