2
0
Panagiotis Christopoulos Charitos 4 жил өмнө
parent
commit
6e26eedb64

+ 3 - 0
AnKi/Renderer/IndirectDiffuse.cpp

@@ -10,6 +10,7 @@
 #include <AnKi/Renderer/DownscaleBlur.h>
 #include <AnKi/Renderer/MotionVectors.h>
 #include <AnKi/Renderer/GlobalIllumination.h>
+#include <AnKi/Renderer/Ssao.h>
 #include <AnKi/Core/ConfigSet.h>
 #include <AnKi/Shaders/Include/IndirectDiffuseTypes.h>
 
@@ -135,6 +136,7 @@ void IndirectDiffuse::populateRenderGraph(RenderingContext& ctx)
 		rpass.newDependency(RenderPassDependency(m_r->getMotionVectors().getRejectionFactorRt(), readUsage));
 		rpass.newDependency(RenderPassDependency(m_runCtx.m_mainRtHandles[READ], readUsage));
 		rpass.newDependency(RenderPassDependency(m_runCtx.m_momentsAndHistoryLengthHandles[READ], readUsage));
+		rpass.newDependency(RenderPassDependency(m_r->getSsao().getRt(), readUsage));
 
 		rpass.setWork([this, &ctx](RenderPassWorkContext& rgraphCtx) {
 			CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
@@ -160,6 +162,7 @@ void IndirectDiffuse::populateRenderGraph(RenderingContext& ctx)
 			rgraphCtx.bindColorTexture(0, 11, m_r->getMotionVectors().getMotionVectorsRt());
 			rgraphCtx.bindColorTexture(0, 12, m_r->getMotionVectors().getRejectionFactorRt());
 			rgraphCtx.bindColorTexture(0, 13, m_runCtx.m_momentsAndHistoryLengthHandles[READ]);
+			rgraphCtx.bindColorTexture(0, 14, m_r->getSsao().getRt());
 
 			// Bind uniforms
 			IndirectDiffuseUniforms unis;

+ 6 - 2
AnKi/Shaders/IndirectDiffuse.ankiprog

@@ -36,6 +36,7 @@ layout(set = 0, binding = 10) uniform texture2D u_historyTex;
 layout(set = 0, binding = 11) uniform texture2D u_motionVectorsTex;
 layout(set = 0, binding = 12) uniform texture2D u_motionVectorsRejectionTex;
 layout(set = 0, binding = 13) uniform texture2D u_prevMomentsAndHistoryLengthTex;
+layout(set = 0, binding = 14) uniform texture2D u_ssaoTex;
 
 layout(push_constant, std430) uniform b_pc
 {
@@ -182,6 +183,9 @@ void main()
 		outColor *= newLum / lum;
 	}
 
+	// Apply SSAO
+	outColor *= textureLod(u_ssaoTex, u_trilinearClampSampler, uv, 0.0).x;
+
 	// Compute history length
 	const Vec2 historyUv = uv + textureLod(u_motionVectorsTex, u_trilinearClampSampler, uv, 0.0).xy;
 	const F32 historyRejectionFactor = textureLod(u_motionVectorsRejectionTex, u_trilinearClampSampler, uv, 0.0).x;
@@ -191,7 +195,7 @@ void main()
 	if(historyRejectionFactor >= 0.5)
 	{
 		// Rejection factor too high, reset the temporal history
-		historyLength = 1.0;
+		historyLength = 0.0;
 	}
 	else
 	{
@@ -204,7 +208,7 @@ void main()
 		// Compute blend fractor. Use nearest sampler because it's an integer texture
 		const F32 lowestBlendFactor = 0.1;
 		const F32 stableFrames = 4.0;
-		const F32 lerp = min(1.0, historyLength / stableFrames);
+		const F32 lerp = min(1.0, (historyLength + 1.0) / stableFrames);
 		const F32 blendFactor = mix(1.0, lowestBlendFactor, lerp);
 
 		// Blend with history

+ 27 - 21
AnKi/Shaders/IndirectDiffuseDenoise.ankiprog

@@ -36,21 +36,23 @@ Vec3 unproject(Vec2 ndc, F32 depth)
 
 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;
 	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);
 }
 
@@ -75,31 +77,35 @@ void main()
 	const Vec3 normalCenter = readNormalFromGBuffer(u_gbuffer2Tex, u_linearAnyClampSampler, uv);
 
 	// Decide the amount of blurring
-	const F32 varianceCenter = computeSpatialVariance(uv);
 	const F32 historyLength = textureLod(u_momentsAndHistoryLengthTex, u_linearAnyClampSampler, uv, 0.0).z;
 
+	const F32 stableFrameCount = 4.0;
 	F32 sampleCount;
-	if(historyLength < 2.0)
+	if(historyLength < stableFrameCount)
 	{
 		// Worst case
 		sampleCount = u_unis.m_maxSampleCount;
 	}
-	else if(historyLength > 4.0 && varianceCenter < 0.0001)
-	{
-		// Best case
-		sampleCount = u_unis.m_minSampleCount;
-	}
 	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 = floor(sampleCount);