Browse Source

Convert DownscaleBlur to HLSL

Panagiotis Christopoulos Charitos 3 years ago
parent
commit
c10ecd7387

+ 1 - 1
AnKi/Renderer/DownscaleBlur.cpp

@@ -189,7 +189,7 @@ void DownscaleBlur::run(U32 passIdx, RenderPassWorkContext& rgraphCtx)
 		sampleSubresource.m_firstMipmap = passIdx;
 		sampleSubresource.m_firstMipmap = passIdx;
 		rgraphCtx.bindImage(0, 3, m_runCtx.m_rt, sampleSubresource);
 		rgraphCtx.bindImage(0, 3, m_runCtx.m_rt, sampleSubresource);
 
 
-		dispatchPPCompute(cmdb, m_workgroupSize[0], m_workgroupSize[1], vpWidth, vpHeight);
+		dispatchPPCompute(cmdb, 8, 8, vpWidth, vpHeight);
 	}
 	}
 	else
 	else
 	{
 	{

+ 0 - 2
AnKi/Renderer/DownscaleBlur.h

@@ -52,8 +52,6 @@ public:
 	}
 	}
 
 
 private:
 private:
-	const Array<U32, 2> m_workgroupSize = {16, 16};
-
 	U32 m_passCount = 0; ///< It's also the mip count of the m_rtTex.
 	U32 m_passCount = 0; ///< It's also the mip count of the m_rtTex.
 
 
 	TexturePtr m_rtTex;
 	TexturePtr m_rtTex;

+ 0 - 63
AnKi/Shaders/DownscaleBlur.glsl

@@ -1,63 +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 once
-
-#include <AnKi/Shaders/TonemappingFunctions.glsl>
-#include <AnKi/Shaders/Functions.glsl>
-
-layout(set = 0, binding = 0) uniform sampler u_linearAnyClampSampler;
-layout(set = 0, binding = 1) uniform ANKI_RP texture2D u_tex;
-
-const U32 kTonemappingBinding = 2u;
-#include <AnKi/Shaders/TonemappingResources.glsl>
-
-layout(push_constant, row_major, std140) uniform b_pc
-{
-	UVec2 u_fbSize;
-	U32 u_revertTonemapping;
-	U32 u_padding;
-};
-
-#if defined(ANKI_COMPUTE_SHADER)
-const UVec2 kWorkgroupSize = UVec2(16, 16);
-layout(local_size_x = kWorkgroupSize.x, local_size_y = kWorkgroupSize.y, local_size_z = 1) in;
-
-Vec2 in_uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(u_fbSize);
-layout(set = 0, binding = 3) writeonly uniform ANKI_RP image2D out_img;
-ANKI_RP Vec3 out_color;
-#else
-layout(location = 0) in Vec2 in_uv;
-layout(location = 0) out ANKI_RP Vec3 out_color;
-#endif
-
-void main()
-{
-#if defined(ANKI_COMPUTE_SHADER)
-	if(gl_GlobalInvocationID.x >= u_fbSize.x || gl_GlobalInvocationID.y >= u_fbSize.y)
-	{
-		// Skip pixels outside the viewport
-		return;
-	}
-#endif
-
-	const ANKI_RP F32 weight = 1.0 / 5.0;
-	out_color = textureLod(u_tex, u_linearAnyClampSampler, in_uv, 0.0).rgb * weight;
-	out_color += textureLodOffset(sampler2D(u_tex, u_linearAnyClampSampler), in_uv, 0.0, IVec2(+1, +1)).rgb * weight;
-	out_color += textureLodOffset(sampler2D(u_tex, u_linearAnyClampSampler), in_uv, 0.0, IVec2(-1, -1)).rgb * weight;
-	out_color += textureLodOffset(sampler2D(u_tex, u_linearAnyClampSampler), in_uv, 0.0, IVec2(+1, -1)).rgb * weight;
-	out_color += textureLodOffset(sampler2D(u_tex, u_linearAnyClampSampler), in_uv, 0.0, IVec2(-1, +1)).rgb * weight;
-
-	if(u_revertTonemapping != 0u)
-	{
-		out_color = saturate(out_color);
-		out_color = sRgbToLinear(out_color);
-		out_color = invertTonemap(out_color, readExposureAndAverageLuminance().x);
-	}
-
-#if defined(ANKI_COMPUTE_SHADER)
-	imageStore(out_img, IVec2(gl_GlobalInvocationID.xy), Vec4(out_color, 0.0));
-#endif
-}

+ 65 - 0
AnKi/Shaders/DownscaleBlur.hlsl

@@ -0,0 +1,65 @@
+// Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#pragma once
+
+#include <AnKi/Shaders/TonemappingFunctions.hlsl>
+#include <AnKi/Shaders/Functions.hlsl>
+
+[[vk::binding(0)]] SamplerState g_linearAnyClampSampler;
+[[vk::binding(1)]] Texture2D<RVec3> g_tex;
+
+constexpr U32 kTonemappingBinding = 2u;
+#include <AnKi/Shaders/TonemappingResources.hlsl>
+
+struct Uniforms
+{
+	UVec2 m_fbSize;
+	U32 m_revertTonemapping;
+	U32 m_padding;
+};
+[[vk::push_constant]] ConstantBuffer<Uniforms> g_uniforms;
+
+#if defined(ANKI_COMPUTE_SHADER)
+[[vk::binding(3)]] RWTexture2D<RVec3> g_outUav;
+#endif
+
+#if defined(ANKI_COMPUTE_SHADER)
+ANKI_NUMTHREADS(8, 8, 1) void main(UVec4 svDispatchThreadId : SV_DISPATCHTHREADID)
+#else
+RVec3 main([[vk::location(0)]] Vec2 uv : TEXCOORD): SV_TARGET0
+#endif
+{
+#if defined(ANKI_COMPUTE_SHADER)
+	if(svDispatchThreadId.x >= g_uniforms.m_fbSize.x || svDispatchThreadId.y >= g_uniforms.m_fbSize.y)
+	{
+		// Skip pixels outside the viewport
+		return;
+	}
+
+	const Vec2 uv = (Vec2(svDispatchThreadId.xy) + 0.5) / Vec2(g_uniforms.m_fbSize);
+#endif
+
+	RVec3 output;
+	const RF32 weight = 1.0 / 5.0;
+	output = g_tex.SampleLevel(g_linearAnyClampSampler, uv, 0.0) * weight;
+	output += g_tex.SampleLevel(g_linearAnyClampSampler, uv, 0.0, IVec2(+1, +1)) * weight;
+	output += g_tex.SampleLevel(g_linearAnyClampSampler, uv, 0.0, IVec2(-1, -1)) * weight;
+	output += g_tex.SampleLevel(g_linearAnyClampSampler, uv, 0.0, IVec2(+1, -1)) * weight;
+	output += g_tex.SampleLevel(g_linearAnyClampSampler, uv, 0.0, IVec2(-1, +1)) * weight;
+
+	if(g_uniforms.m_revertTonemapping != 0u)
+	{
+		output = saturate(output);
+		output = sRgbToLinear(output);
+		output = invertTonemap(output, readExposureAndAverageLuminance().x);
+	}
+
+#if defined(ANKI_COMPUTE_SHADER)
+	g_outUav[svDispatchThreadId.xy] = output;
+#else
+	return output;
+#endif
+}

+ 3 - 1
AnKi/Shaders/DownscaleBlurCompute.ankiprog

@@ -3,6 +3,8 @@
 // Code licensed under the BSD License.
 // Code licensed under the BSD License.
 // http://www.anki3d.org/LICENSE
 // http://www.anki3d.org/LICENSE
 
 
+#pragma anki hlsl
+
 #pragma anki start comp
 #pragma anki start comp
-#include <AnKi/Shaders/DownscaleBlur.glsl>
+#include <AnKi/Shaders/DownscaleBlur.hlsl>
 #pragma anki end
 #pragma anki end

+ 4 - 2
AnKi/Shaders/DownscaleBlurRaster.ankiprog

@@ -3,10 +3,12 @@
 // Code licensed under the BSD License.
 // Code licensed under the BSD License.
 // http://www.anki3d.org/LICENSE
 // http://www.anki3d.org/LICENSE
 
 
+#pragma anki hlsl
+
 #pragma anki start vert
 #pragma anki start vert
-#include <AnKi/Shaders/QuadVert.glsl>
+#include <AnKi/Shaders/QuadVert.hlsl>
 #pragma anki end
 #pragma anki end
 
 
 #pragma anki start frag
 #pragma anki start frag
-#include <AnKi/Shaders/DownscaleBlur.glsl>
+#include <AnKi/Shaders/DownscaleBlur.hlsl>
 #pragma anki end
 #pragma anki end