瀏覽代碼

Some refactoring

Panagiotis Christopoulos Charitos 4 年之前
父節點
當前提交
4becf3bbf2

+ 51 - 61
AnKi/Shaders/BilateralFilter.glsl

@@ -3,87 +3,77 @@
 // Code licensed under the BSD License.
 // http://www.anki3d.org/LICENSE
 
+// Contains a bounch of edge stopping functions plus some other things
+
 #pragma once
 
 #include <AnKi/Shaders/Common.glsl>
 
-struct BilateralSample
-{
-	F32 m_depth;
-	Vec3 m_position;
-	Vec3 m_normal;
-	F32 m_roughness;
-};
-
-struct BilateralConfig
+// https://cs.dartmouth.edu/~wjarosz/publications/mara17towards.html
+F32 calculateBilateralWeightDepth(F32 center, F32 tap, F32 phi)
 {
-	F32 m_depthWeight;
-	F32 m_normalWeight;
-	F32 m_planeWeight;
-	F32 m_roughnessWeight;
-};
+	const F32 diff = abs(tap - center);
+#if 0
+	return max(0.0, 1.0 - diff * phi);
+#else
+	return sqrt(1.0 / (EPSILON + diff)) * phi;
+#endif
+}
 
-void initConfig(out BilateralConfig cfg)
+// From the SVGF sample code. Depth is linear
+F32 calculateBilateralWeightDepth2(F32 center, F32 tap, F32 phi)
 {
-	cfg.m_depthWeight = 0.0;
-	cfg.m_normalWeight = 0.0;
-	cfg.m_planeWeight = 0.0;
-	cfg.m_roughnessWeight = 0.0;
+	return (phi == 0.0) ? 0.0 : abs(depthCenter - depthTap) / phi;
 }
 
 // https://cs.dartmouth.edu/~wjarosz/publications/mara17towards.html
-F32 calculateBilateralWeight(BilateralSample center, BilateralSample tap, BilateralConfig config)
+F32 calculateBilateralWeightNormal(Vec3 center, Vec3 tap, F32 phi)
 {
-	F32 depthWeight = 1.0;
-	F32 normalWeight = 1.0;
-	F32 planeWeight = 1.0;
-	F32 glossyWeight = 1.0;
-
-	if(config.m_depthWeight > 0.0)
-	{
-#if 0
-		depthWeight = max(0.0, 1.0 - abs(tap.m_depth - center.m_depth) * config.m_depthWeight);
-#else
-		const F32 diff = abs(tap.m_depth - center.m_depth);
-		depthWeight = sqrt(1.0 / (EPSILON + diff)) * config.m_depthWeight;
-#endif
-	}
+	F32 normalCloseness = dot(tap, center);
+	normalCloseness = normalCloseness * normalCloseness;
+	normalCloseness = normalCloseness * normalCloseness;
 
-	if(config.m_normalWeight > 0.0)
-	{
-		F32 normalCloseness = dot(tap.m_normal, center.m_normal);
-		normalCloseness = normalCloseness * normalCloseness;
-		normalCloseness = normalCloseness * normalCloseness;
+	const F32 normalError = (1.0 - normalCloseness);
+	return max((1.0 - normalError * phi), 0.0);
+}
 
-		const F32 normalError = (1.0 - normalCloseness);
-		normalWeight = max((1.0 - normalError * config.m_normalWeight), 0.0);
-	}
+F32 calculateBilateralWeightNormalCos(Vec3 ref, Vec3 tap, F32 phi)
+{
+	return pow(saturate(dot(ref, tap)), phi);
+}
 
-	if(config.m_planeWeight > 0.0)
-	{
-		const F32 lowDistanceThreshold2 = 0.001;
+// https://cs.dartmouth.edu/~wjarosz/publications/mara17towards.html
+F32 calculateBilateralWeightPlane(Vec3 positionCenter, Vec3 centerNormal, Vec3 positionTap, Vec3 normalTap, F32 phi)
+{
+	const F32 lowDistanceThreshold2 = 0.001;
 
-		// Change in position in camera space
-		const Vec3 dq = center.m_position - tap.m_position;
+	// Change in position in camera space
+	const Vec3 dq = positionCenter - positionTap;
 
-		// How far away is this point from the original sample in camera space? (Max value is unbounded)
-		const F32 distance2 = dot(dq, dq);
+	// How far away is this point from the original sample in camera space? (Max value is unbounded)
+	const F32 distance2 = dot(dq, dq);
 
-		// How far off the expected plane (on the perpendicular) is this point? Max value is unbounded.
-		const F32 planeError = max(abs(dot(dq, tap.m_normal)), abs(dot(dq, center.m_normal)));
+	// How far off the expected plane (on the perpendicular) is this point? Max value is unbounded.
+	const F32 planeError = max(abs(dot(dq, normalTap)), abs(dot(dq, centerNormal)));
 
-		planeWeight = (distance2 < lowDistanceThreshold2)
-						  ? 1.0
-						  : pow(max(0.0, 1.0 - 2.0 * config.m_planeWeight * planeError / sqrt(distance2)), 2.0);
-	}
+	return (distance2 < lowDistanceThreshold2) ? 1.0
+											   : pow(max(0.0, 1.0 - 2.0 * phi * planeError / sqrt(distance2)), 2.0);
+}
 
-	if(config.m_roughnessWeight > 0.0)
-	{
-		const F32 gDiff = abs(tap.m_roughness - center.m_roughness) * 10.0;
-		glossyWeight = max(0.0, 1.0 - (gDiff * config.m_roughnessWeight));
-	}
+// https://cs.dartmouth.edu/~wjarosz/publications/mara17towards.html
+F32 calculateBilateralWeightRoughness(F32 roughnessCenter, F32 roughnessTap, F32 phi)
+{
+	const F32 gDiff = abs(roughnessCenter - roughnessTap) * 10.0;
+	return max(0.0, 1.0 - (gDiff * phi));
+}
 
-	return depthWeight * normalWeight * planeWeight * glossyWeight;
+// From SVGF sample code.
+F32 calculateBilateralWeightLinearDepthAndLuminance(F32 depthCenter, F32 luminanceCenter, F32 depthTap,
+													F32 luminanceTap, F32 phiDepth, F32 phiLuminance)
+{
+	const F32 wZ = calculateBilateralWeightDepth(depthCenter, depthTap, phiDepth);
+	const F32 wL = abs(luminanceCenter - luminanceTap) / phiLuminance;
+	return exp(0.0 - max(wL, 0.0) - max(wZ, 0.0));
 }
 
 struct SpatialBilateralContext

+ 8 - 17
AnKi/Shaders/RtShadowsDenoise.ankiprog

@@ -51,10 +51,9 @@ void main()
 	unpackRtShadows(textureLod(u_inTex, u_nearestAnyClampSampler, uv, 0.0), shadowFactors);
 	F32 weight = 1.0;
 
-	BilateralSample ref;
-	ref.m_depth = textureLod(u_depthTex, u_nearestAnyClampSampler, uv, 0.0).r;
-	ref.m_position = unproject(UV_TO_NDC(uv), ref.m_depth);
-	ref.m_normal = readNormalFromGBuffer(u_gbuffer2Tex, u_nearestAnyClampSampler, uv);
+	const F32 depthCenter = textureLod(u_depthTex, u_nearestAnyClampSampler, uv, 0.0).r;
+	const Vec3 positionCenter = unproject(UV_TO_NDC(uv), depthCenter);
+	const Vec3 normalCenter = readNormalFromGBuffer(u_gbuffer2Tex, u_nearestAnyClampSampler, uv);
 
 	// Sample
 	SpatialBilateralContext ctx =
@@ -65,19 +64,11 @@ void main()
 		const IVec2 unormalizedUvs = clamp(IVec2(spatialBilateralIterate(ctx, i)), IVec2(0), IVec2(OUT_IMAGE_SIZE - 1));
 		const Vec2 sampleUv = Vec2(unormalizedUvs) / Vec2(OUT_IMAGE_SIZE);
 
-		BilateralSample crnt;
-		crnt.m_depth = texelFetch(u_depthTex, unormalizedUvs, 0).r;
-		crnt.m_position = unproject(UV_TO_NDC(sampleUv), crnt.m_depth);
-		crnt.m_normal = unpackNormalFromGBuffer(texelFetch(u_gbuffer2Tex, unormalizedUvs, 0));
-
-		BilateralConfig config;
-		initConfig(config);
-		const Vec3 weights = normalize(Vec3(0.0, 1.0, 1.0));
-		config.m_depthWeight = weights.x;
-		config.m_normalWeight = weights.y;
-		config.m_planeWeight = weights.z;
-		config.m_roughnessWeight = 0.0;
-		const F32 w = calculateBilateralWeight(crnt, ref, config);
+		const F32 depthTap = texelFetch(u_depthTex, unormalizedUvs, 0).r;
+		const Vec3 positionTap = unproject(UV_TO_NDC(sampleUv), depthTap);
+		const Vec3 normalTap = unpackNormalFromGBuffer(texelFetch(u_gbuffer2Tex, unormalizedUvs, 0));
+
+		const F32 w = calculateBilateralWeightPlane(positionCenter, normalCenter, positionTap, normalTap, 1.0);
 		weight += w;
 
 		F32 localShadowFactors[MAX_RT_SHADOW_LAYERS];

+ 8 - 15
AnKi/Shaders/RtShadowsSvgfAtrous.ankiprog

@@ -74,10 +74,9 @@ void main()
 	}
 
 	// Set the reference sample
-	BilateralSample ref;
-	ref.m_depth = depth;
-	ref.m_position = unproject(uv, ref.m_depth);
-	ref.m_normal = readNormalFromGBuffer(u_gbuffer2Tex, u_linearAnyClampSampler, uv);
+	const F32 depthCenter = depth;
+	const Vec3 positionCenter = unproject(uv, depthCenter);
+	const Vec3 normalCenter = readNormalFromGBuffer(u_gbuffer2Tex, u_linearAnyClampSampler, uv);
 
 	// Read center luma
 	F32 shadowLayers[MAX_RT_SHADOW_LAYERS];
@@ -109,19 +108,13 @@ void main()
 			const F32 wl = min(1.0, exp(-abs(luma - refLuma) / (sigmaL * sqrt(variance + 0.001) + EPSILON)));
 
 			// Set the current sample
-			BilateralSample crnt;
-			crnt.m_depth = textureLod(u_depthTex, u_linearAnyClampSampler, sampleUv, 0.0).r;
-			crnt.m_position = unproject(sampleUv, crnt.m_depth);
-			crnt.m_normal = unpackNormalFromGBuffer(textureLod(u_gbuffer2Tex, u_linearAnyClampSampler, sampleUv, 0.0));
+			const F32 depthTap = textureLod(u_depthTex, u_linearAnyClampSampler, sampleUv, 0.0).r;
+			const Vec3 positionTap = unproject(sampleUv, depthTap);
+			const Vec3 normalTap =
+				unpackNormalFromGBuffer(textureLod(u_gbuffer2Tex, u_linearAnyClampSampler, sampleUv, 0.0));
 
 			// Do bilateral
-			BilateralConfig config;
-			initConfig(config);
-			const Vec3 weights = normalize(Vec3(0.0, 0.0, 1.0));
-			config.m_depthWeight = weights.x;
-			config.m_normalWeight = weights.y;
-			config.m_planeWeight = weights.z;
-			F32 w = calculateBilateralWeight(crnt, ref, config);
+			F32 w = calculateBilateralWeightPlane(positionCenter, normalCenter, positionTap, normalTap, 1.0);
 
 			// Include more weights
 			w *= wl;

+ 7 - 15
AnKi/Shaders/RtShadowsSvgfVariance.ankiprog

@@ -69,10 +69,9 @@ void main()
 		const Vec2 texelSize = 1.0 / Vec2(FB_SIZE);
 
 		// Set the reference sample
-		BilateralSample ref;
-		ref.m_depth = depth;
-		ref.m_position = unproject(uv, ref.m_depth);
-		ref.m_normal = readNormalFromGBuffer(u_gbuffer2Tex, u_linearAnyClampSampler, uv);
+		const F32 depthCenter = depth;
+		const Vec3 positionCenter = unproject(uv, depthCenter);
+		const Vec3 normalCenter = readNormalFromGBuffer(u_gbuffer2Tex, u_linearAnyClampSampler, uv);
 
 		// Init the sums
 		Vec2 sumMoments = Vec2(0.0);
@@ -88,20 +87,13 @@ void main()
 				const Vec2 sampleUv = uv + Vec2(offsetx, offsety) * texelSize;
 
 				// Set the current sample
-				BilateralSample crnt;
-				crnt.m_depth = textureLod(u_depthTex, u_linearAnyClampSampler, sampleUv, 0.0).r;
-				crnt.m_position = unproject(sampleUv, crnt.m_depth);
-				crnt.m_normal =
+				const F32 depthTap = textureLod(u_depthTex, u_linearAnyClampSampler, sampleUv, 0.0).r;
+				const Vec3 positionTap = unproject(sampleUv, depthTap);
+				const Vec3 normalTap =
 					unpackNormalFromGBuffer(textureLod(u_gbuffer2Tex, u_linearAnyClampSampler, sampleUv, 0.0));
 
 				// Do bilateral
-				BilateralConfig config;
-				initConfig(config);
-				const Vec3 weights = normalize(Vec3(0.0, 0.0, 1.0));
-				config.m_depthWeight = weights.x;
-				config.m_normalWeight = weights.y;
-				config.m_planeWeight = weights.z;
-				const F32 w = calculateBilateralWeight(crnt, ref, config);
+				const F32 w = calculateBilateralWeightPlane(positionCenter, normalCenter, positionTap, normalTap, 1.0);
 
 				// Sum
 				const Vec2 moments = textureLod(u_momentsTex, u_linearAnyClampSampler, sampleUv, 0.0).xy;

+ 11 - 19
AnKi/Shaders/SsgiDenoise.ankiprog

@@ -49,23 +49,16 @@ Vec3 readNormal(Vec2 uv)
 	return readNormalFromGBuffer(u_gbuffer2Tex, u_linearAnyClampSampler, uv);
 }
 
-void sampleTex(Vec2 colorUv, Vec2 fullUv, BilateralSample ref, inout Vec3 col, inout F32 weight)
+void sampleTex(Vec2 colorUv, Vec2 fullUv, Vec3 positionCenter, Vec3 normalCenter, inout Vec3 col, inout F32 weight)
 {
 	const Vec3 color = textureLod(u_inTex, u_linearAnyClampSampler, colorUv, 0.0).rgb;
 
-	BilateralSample crnt;
-	crnt.m_depth = readDepth(fullUv);
-	crnt.m_position = unproject(UV_TO_NDC(fullUv), crnt.m_depth);
-	crnt.m_normal = readNormal(fullUv);
+	const F32 depthTap = readDepth(fullUv);
+	const Vec3 positionTap = unproject(UV_TO_NDC(fullUv), depthTap);
+	const Vec3 normalTap = readNormal(fullUv);
 
-	BilateralConfig config;
-	initConfig(config);
-	const Vec3 weights = normalize(Vec3(0.0, 1.0, 1.0));
-	config.m_depthWeight = weights.x;
-	config.m_normalWeight = weights.y;
-	config.m_planeWeight = weights.z;
-
-	const F32 w = calculateBilateralWeight(crnt, ref, config);
+	F32 w = calculateBilateralWeightPlane(positionCenter, normalCenter, positionTap, normalTap, 0.5);
+	w *= calculateBilateralWeightNormal(normalCenter, normalTap, 0.5);
 	col += color * w;
 	weight += w;
 }
@@ -98,10 +91,9 @@ void main()
 	Vec3 color = textureLod(u_inTex, u_linearAnyClampSampler, inUv, 0.0).rgb;
 	F32 weight = 1.0;
 
-	BilateralSample ref;
-	ref.m_depth = readDepth(depthUv);
-	ref.m_position = unproject(UV_TO_NDC(depthUv), ref.m_depth);
-	ref.m_normal = readNormal(depthUv);
+	const F32 depthCenter = readDepth(depthUv);
+	const Vec3 positionCenter = unproject(UV_TO_NDC(depthUv), depthCenter);
+	const Vec3 normalCenter = readNormal(depthUv);
 
 #if ORIENTATION == 1
 #	define X_OR_Y x
@@ -116,8 +108,8 @@ void main()
 
 	ANKI_UNROLL for(U32 i = 0u; i < (SAMPLE_COUNT - 1u) / 2u; ++i)
 	{
-		sampleTex(inUv + inUvOffset, depthUv + depthUvOffset, ref, color, weight);
-		sampleTex(inUv - inUvOffset, depthUv - depthUvOffset, ref, color, weight);
+		sampleTex(inUv + inUvOffset, depthUv + depthUvOffset, positionCenter, normalCenter, color, weight);
+		sampleTex(inUv - inUvOffset, depthUv - depthUvOffset, positionCenter, normalCenter, color, weight);
 
 		inUvOffset.X_OR_Y += IN_TEXEL_SIZE.X_OR_Y;
 		depthUvOffset.X_OR_Y += 2.0 * DEPTH_TEXEL_SIZE.X_OR_Y;