Переглянути джерело

Convert TemporalAA to HLSL

Panagiotis Christopoulos Charitos 2 роки тому
батько
коміт
33adac43a3

+ 6 - 8
AnKi/Renderer/TemporalAA.cpp

@@ -148,17 +148,15 @@ void TemporalAA::populateRenderGraph(RenderingContext& ctx)
 		cmdb->bindShaderProgram(m_grProg);
 
 		cmdb->bindSampler(0, 0, m_r->getSamplers().m_trilinearClamp);
-		rgraphCtx.bindTexture(0, 1, m_r->getGBuffer().getDepthRt(),
-							  TextureSubresourceInfo(DepthStencilAspectBit::kDepth));
-		rgraphCtx.bindColorTexture(0, 2, m_r->getLightShading().getRt());
-		rgraphCtx.bindColorTexture(0, 3, m_runCtx.m_historyRt);
-		rgraphCtx.bindColorTexture(0, 4, m_r->getMotionVectors().getMotionVectorsRt());
-		rgraphCtx.bindImage(0, 5, m_r->getTonemapping().getRt());
+		rgraphCtx.bindColorTexture(0, 1, m_r->getLightShading().getRt());
+		rgraphCtx.bindColorTexture(0, 2, m_runCtx.m_historyRt);
+		rgraphCtx.bindColorTexture(0, 3, m_r->getMotionVectors().getMotionVectorsRt());
+		rgraphCtx.bindImage(0, 4, m_r->getTonemapping().getRt());
 
 		if(getExternalSubsystems().m_config->getRPreferCompute())
 		{
-			rgraphCtx.bindImage(0, 6, m_runCtx.m_renderRt, TextureSubresourceInfo());
-			rgraphCtx.bindImage(0, 7, m_runCtx.m_tonemappedRt, TextureSubresourceInfo());
+			rgraphCtx.bindImage(0, 5, m_runCtx.m_renderRt, TextureSubresourceInfo());
+			rgraphCtx.bindImage(0, 6, m_runCtx.m_tonemappedRt, TextureSubresourceInfo());
 
 			dispatchPPCompute(cmdb, 8, 8, m_r->getInternalResolution().x(), m_r->getInternalResolution().y());
 		}

+ 0 - 120
AnKi/Shaders/TemporalAA.glsl

@@ -1,120 +0,0 @@
-// Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#pragma anki mutator VARIANCE_CLIPPING 0 1
-#pragma anki mutator YCBCR 0 1
-
-#include <AnKi/Shaders/Functions.glsl>
-#include <AnKi/Shaders/PackFunctions.glsl>
-#include <AnKi/Shaders/TonemappingFunctions.glsl>
-
-ANKI_SPECIALIZATION_CONSTANT_F32(kVarianceClippingGamma, 0u);
-ANKI_SPECIALIZATION_CONSTANT_F32(kBlendFactor, 1u);
-ANKI_SPECIALIZATION_CONSTANT_UVEC2(kFramebufferSize, 2u);
-
-layout(set = 0, binding = 0) uniform sampler u_linearAnyClampSampler;
-layout(set = 0, binding = 1) uniform texture2D u_depthRt;
-layout(set = 0, binding = 2) uniform ANKI_RP texture2D u_inputRt;
-layout(set = 0, binding = 3) uniform ANKI_RP texture2D u_historyRt;
-layout(set = 0, binding = 4) uniform texture2D u_motionVectorsTex;
-
-const U32 kTonemappingBinding = 5u;
-#include <AnKi/Shaders/TonemappingResources.glsl>
-
-#if defined(ANKI_COMPUTE_SHADER)
-layout(set = 0, binding = 6) writeonly uniform image2D u_outImg;
-layout(set = 0, binding = 7) writeonly uniform image2D u_tonemappedImg;
-
-const UVec2 kWorkgroupSize = UVec2(8, 8);
-layout(local_size_x = kWorkgroupSize.x, local_size_y = kWorkgroupSize.y, local_size_z = 1) in;
-#else
-layout(location = 0) in Vec2 in_uv;
-layout(location = 0) out Vec3 out_color;
-layout(location = 1) out Vec3 out_tonemappedColor;
-#endif
-
-#if YCBCR
-#	define sample(s, uv) rgbToYCbCr(textureLod(s, u_linearAnyClampSampler, uv, 0.0).rgb)
-#	define sampleOffset(s, uv, x, y) \
-		rgbToYCbCr(textureLodOffset(sampler2D(s, u_linearAnyClampSampler), uv, 0.0, IVec2(x, y)).rgb)
-#else
-#	define sample(s, uv) textureLod(s, u_linearAnyClampSampler, uv, 0.0).rgb
-#	define sampleOffset(s, uv, x, y) textureLodOffset(sampler2D(s, u_linearAnyClampSampler), uv, 0.0, IVec2(x, y)).rgb
-#endif
-
-void main()
-{
-#if defined(ANKI_COMPUTE_SHADER)
-	if(skipOutOfBoundsInvocations(kWorkgroupSize, kFramebufferSize))
-	{
-		return;
-	}
-
-	const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(kFramebufferSize);
-#else
-	const Vec2 uv = in_uv;
-#endif
-
-	const F32 depth = textureLod(u_depthRt, u_linearAnyClampSampler, uv, 0.0).r;
-
-	// Get prev uv coords
-	const Vec2 oldUv = uv + textureLod(u_motionVectorsTex, u_linearAnyClampSampler, uv, 0.0).rg;
-
-	// Read textures
-	Vec3 historyCol = sample(u_historyRt, oldUv);
-	const Vec3 crntCol = sample(u_inputRt, uv);
-
-	// Remove ghosting by clamping the history color to neighbour's AABB
-	const Vec3 near0 = sampleOffset(u_inputRt, uv, 1, 0);
-	const Vec3 near1 = sampleOffset(u_inputRt, uv, 0, 1);
-	const Vec3 near2 = sampleOffset(u_inputRt, uv, -1, 0);
-	const Vec3 near3 = sampleOffset(u_inputRt, uv, 0, -1);
-
-#if VARIANCE_CLIPPING
-	const Vec3 m1 = crntCol + near0 + near1 + near2 + near3;
-	const Vec3 m2 = crntCol * crntCol + near0 * near0 + near1 * near1 + near2 * near2 + near3 * near3;
-
-	const Vec3 mu = m1 / 5.0;
-	const Vec3 sigma = sqrt(m2 / 5.0 - mu * mu);
-
-	const Vec3 boxMin = mu - kVarianceClippingGamma * sigma;
-	const Vec3 boxMax = mu + kVarianceClippingGamma * sigma;
-#else
-	const Vec3 boxMin = min(crntCol, min(near0, min(near1, min(near2, near3))));
-	const Vec3 boxMax = max(crntCol, max(near0, max(near1, max(near2, near3))));
-#endif
-
-	historyCol = clamp(historyCol, boxMin, boxMax);
-
-	// Remove jitter (T. Lottes)
-#if YCBCR
-	const F32 lum0 = crntCol.r;
-	const F32 lum1 = historyCol.r;
-	const F32 maxLum = boxMax.r;
-#else
-	const F32 lum0 = computeLuminance(reinhardTonemap(crntCol));
-	const F32 lum1 = computeLuminance(reinhardTonemap(historyCol));
-	const F32 maxLum = 1.0;
-#endif
-
-	F32 diff = abs(lum0 - lum1) / max(lum0, max(lum1, maxLum + kEpsilonf));
-	diff = 1.0 - diff;
-	diff = diff * diff;
-	const F32 feedback = mix(0.0, kBlendFactor, diff);
-
-	// Write result
-	Vec3 outColor = mix(historyCol, crntCol, feedback);
-#if YCBCR
-	outColor = yCbCrToRgb(outColor);
-#endif
-	const Vec3 tonemapped = linearToSRgb(tonemap(outColor, readExposureAndAverageLuminance().x));
-#if defined(ANKI_COMPUTE_SHADER)
-	imageStore(u_outImg, IVec2(gl_GlobalInvocationID.xy), Vec4(outColor, 0.0));
-	imageStore(u_tonemappedImg, IVec2(gl_GlobalInvocationID.xy), Vec4(tonemapped, 0.0));
-#else
-	out_color = outColor;
-	out_tonemappedColor = tonemapped;
-#endif
-}

+ 121 - 0
AnKi/Shaders/TemporalAA.hlsl

@@ -0,0 +1,121 @@
+// Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#pragma anki hlsl
+
+#pragma anki mutator VARIANCE_CLIPPING 0 1
+#pragma anki mutator YCBCR 0 1
+
+#include <AnKi/Shaders/Functions.hlsl>
+#include <AnKi/Shaders/PackFunctions.hlsl>
+#include <AnKi/Shaders/TonemappingFunctions.hlsl>
+
+ANKI_SPECIALIZATION_CONSTANT_F32(kVarianceClippingGamma, 0u);
+ANKI_SPECIALIZATION_CONSTANT_F32(kBlendFactor, 1u);
+ANKI_SPECIALIZATION_CONSTANT_UVEC2(kFramebufferSize, 2u);
+
+[[vk::binding(0)]] SamplerState g_linearAnyClampSampler;
+[[vk::binding(1)]] Texture2D<RVec4> g_inputRt;
+[[vk::binding(2)]] Texture2D<RVec4> g_historyRt;
+[[vk::binding(3)]] Texture2D g_motionVectorsTex;
+
+constexpr U32 kTonemappingBinding = 4u;
+#include <AnKi/Shaders/TonemappingResources.hlsl>
+
+#if defined(ANKI_COMPUTE_SHADER)
+[[vk::binding(5)]] RWTexture2D<RVec4> g_outImg;
+[[vk::binding(6)]] RWTexture2D<RVec4> g_tonemappedImg;
+#else
+struct FragOut
+{
+	RVec3 m_color : SV_TARGET0;
+	RVec3 m_tonemappedColor : SV_TARGET1;
+};
+#endif
+
+#if YCBCR
+#	define sample(s, uv) rgbToYCbCr(s.SampleLevel(g_linearAnyClampSampler, uv, 0.0).rgb)
+#	define sampleOffset(s, uv, x, y) rgbToYCbCr(s.SampleLevel(g_linearAnyClampSampler, uv, 0.0, IVec2(x, y)).rgb)
+#else
+#	define sample(s, uv) s.SampleLevel(g_linearAnyClampSampler, uv, 0.0).rgb
+#	define sampleOffset(s, uv, x, y) s.SampleLevel(g_linearAnyClampSampler, uv, 0.0, IVec2(x, y)).rgb
+#endif
+
+#if defined(ANKI_COMPUTE_SHADER)
+[numthreads(8, 8, 1)] void main(UVec3 svDispatchThreadId : SV_DISPATCHTHREADID)
+#else
+FragOut main(Vec2 uv : TEXCOORD)
+#endif
+{
+#if defined(ANKI_COMPUTE_SHADER)
+	if(any(svDispatchThreadId.xy >= kFramebufferSize))
+	{
+		return;
+	}
+
+	const Vec2 uv = (Vec2(svDispatchThreadId.xy) + 0.5) / Vec2(kFramebufferSize);
+#endif
+
+	// Get prev uv coords
+	const Vec2 oldUv = uv + g_motionVectorsTex.SampleLevel(g_linearAnyClampSampler, uv, 0.0).rg;
+
+	// Read textures
+	Vec3 historyCol = sample(g_historyRt, oldUv);
+	const Vec3 crntCol = sample(g_inputRt, uv);
+
+	// Remove ghosting by clamping the history color to neighbour's AABB
+	const Vec3 near0 = sampleOffset(g_inputRt, uv, 1, 0);
+	const Vec3 near1 = sampleOffset(g_inputRt, uv, 0, 1);
+	const Vec3 near2 = sampleOffset(g_inputRt, uv, -1, 0);
+	const Vec3 near3 = sampleOffset(g_inputRt, uv, 0, -1);
+
+#if VARIANCE_CLIPPING
+	const Vec3 m1 = crntCol + near0 + near1 + near2 + near3;
+	const Vec3 m2 = crntCol * crntCol + near0 * near0 + near1 * near1 + near2 * near2 + near3 * near3;
+
+	const Vec3 mu = m1 / 5.0;
+	const Vec3 sigma = sqrt(m2 / 5.0 - mu * mu);
+
+	const Vec3 boxMin = mu - kVarianceClippingGamma * sigma;
+	const Vec3 boxMax = mu + kVarianceClippingGamma * sigma;
+#else
+	const Vec3 boxMin = min(crntCol, min(near0, min(near1, min(near2, near3))));
+	const Vec3 boxMax = max(crntCol, max(near0, max(near1, max(near2, near3))));
+#endif
+
+	historyCol = clamp(historyCol, boxMin, boxMax);
+
+	// Remove jitter (T. Lottes)
+#if YCBCR
+	const F32 lum0 = crntCol.r;
+	const F32 lum1 = historyCol.r;
+	const F32 maxLum = boxMax.r;
+#else
+	const F32 lum0 = computeLuminance(reinhardTonemap(crntCol));
+	const F32 lum1 = computeLuminance(reinhardTonemap(historyCol));
+	const F32 maxLum = 1.0;
+#endif
+
+	F32 diff = abs(lum0 - lum1) / max(lum0, max(lum1, maxLum + kEpsilonF32));
+	diff = 1.0 - diff;
+	diff = diff * diff;
+	const F32 feedback = lerp(0.0, kBlendFactor, diff);
+
+	// Write result
+	Vec3 outColor = lerp(historyCol, crntCol, feedback);
+#if YCBCR
+	outColor = yCbCrToRgb(outColor);
+#endif
+	const Vec3 tonemapped = linearToSRgb(tonemap(outColor, readExposureAndAverageLuminance().x));
+#if defined(ANKI_COMPUTE_SHADER)
+	g_outImg[svDispatchThreadId.xy] = RVec4(outColor, 0.0);
+	g_tonemappedImg[svDispatchThreadId.xy] = RVec4(tonemapped, 0.0);
+#else
+	FragOut output;
+	output.m_color = outColor;
+	output.m_tonemappedColor = tonemapped;
+	return output;
+#endif
+}

+ 2 - 2
AnKi/Shaders/TemporalAACompute.ankiprog

@@ -4,5 +4,5 @@
 // http://www.anki3d.org/LICENSE
 
 #pragma anki start comp
-#include <AnKi/Shaders/TemporalAA.glsl>
-#pragma anki end
+#include <AnKi/Shaders/TemporalAA.hlsl>
+#pragma anki end

+ 3 - 3
AnKi/Shaders/TemporalAARaster.ankiprog

@@ -4,9 +4,9 @@
 // http://www.anki3d.org/LICENSE
 
 #pragma anki start vert
-#include <AnKi/Shaders/QuadVert.glsl>
+#include <AnKi/Shaders/QuadVert.hlsl>
 #pragma anki end
 
 #pragma anki start frag
-#include <AnKi/Shaders/TemporalAA.glsl>
-#pragma anki end
+#include <AnKi/Shaders/TemporalAA.hlsl>
+#pragma anki end