From 56da810b50aa6265f345ebc61087ee21a8a62fb3 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Thu, 23 Feb 2023 16:41:07 +0100 Subject: [PATCH] Implemented lock pass --- .../FSR2/ffx_fsr2_depth_clip_pass.compute | 2 +- .../Resources/FSR2/ffx_fsr2_lock_pass.compute | 3 +++ Assets/Scripts/Fsr2Context.cs | 14 +++++++++--- Assets/Scripts/Fsr2Pipeline.cs | 22 +++++++++++++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Assets/Resources/FSR2/ffx_fsr2_depth_clip_pass.compute b/Assets/Resources/FSR2/ffx_fsr2_depth_clip_pass.compute index fdf09c6..3668ef2 100644 --- a/Assets/Resources/FSR2/ffx_fsr2_depth_clip_pass.compute +++ b/Assets/Resources/FSR2/ffx_fsr2_depth_clip_pass.compute @@ -11,7 +11,7 @@ #define FFX_GPU // Compiling for GPU #define FFX_HLSL // Compile for plain HLSL -// Monkey patch SRV names to match the UAV names from the shaders that output them +// Monkey-patch SRV names to match the UAV names from the shaders that output them #define r_reconstructed_previous_nearest_depth rw_reconstructed_previous_nearest_depth #define r_dilatedDepth rw_dilatedDepth diff --git a/Assets/Resources/FSR2/ffx_fsr2_lock_pass.compute b/Assets/Resources/FSR2/ffx_fsr2_lock_pass.compute index c48343d..d25087e 100644 --- a/Assets/Resources/FSR2/ffx_fsr2_lock_pass.compute +++ b/Assets/Resources/FSR2/ffx_fsr2_lock_pass.compute @@ -11,4 +11,7 @@ #define FFX_GPU // Compiling for GPU #define FFX_HLSL // Compile for plain HLSL +// Monkey-patch SRV names to match the UAV names from the shaders that output them +#define r_lock_input_luma rw_lock_input_luma + #include "shaders/ffx_fsr2_lock_pass.hlsl" diff --git a/Assets/Scripts/Fsr2Context.cs b/Assets/Scripts/Fsr2Context.cs index c01be74..771052d 100644 --- a/Assets/Scripts/Fsr2Context.cs +++ b/Assets/Scripts/Fsr2Context.cs @@ -93,10 +93,12 @@ namespace FidelityFX _lanczosLutResource.SetPixelData(lanczos2Weights, 0); _lanczosLutResource.Apply(); + // Resource FSR2_DefaultExposure: FFX_RESOURCE_USAGE_READ_ONLY, FFX_SURFACE_FORMAT_R32G32_FLOAT, FFX_RESOURCE_FLAGS_NONE _defaultExposureResource = new Texture2D(1, 1, GraphicsFormat.R32G32_SFloat, TextureCreationFlags.None) { name = "FSR2_DefaultExposure" }; _defaultExposureResource.SetPixel(0, 0, Color.black); _defaultExposureResource.Apply(); + // Resource FSR2_DefaultReactiviyMask: FFX_RESOURCE_USAGE_READ_ONLY, FFX_SURFACE_FORMAT_R8_UNORM, FFX_RESOURCE_FLAGS_NONE _defaultReactiveResource = new Texture2D(1, 1, GraphicsFormat.R8_UNorm, TextureCreationFlags.None) { name = "FSR2_DefaultReactivityMask" }; _defaultReactiveResource.SetPixel(0, 0, Color.black); _defaultReactiveResource.Apply(); @@ -132,6 +134,7 @@ namespace FidelityFX _computeLuminancePyramidPipeline = new Fsr2ComputeLuminancePyramidPipeline(_contextDescription, _fsr2ConstantsBuffer, _spdConstantsBuffer, _autoExposureResource); _reconstructPreviousDepthPipeline = new Fsr2ReconstructPreviousDepthPipeline(_contextDescription, _fsr2ConstantsBuffer, _dilatedMotionVectorResources); _depthClipPipeline = new Fsr2DepthClipPipeline(_contextDescription, _fsr2ConstantsBuffer, _dilatedMotionVectorResources); + _lockPipeline = new Fsr2LockPipeline(_contextDescription, _fsr2ConstantsBuffer); _accumulatePipeline = new Fsr2AccumulatePipeline(_contextDescription, _fsr2ConstantsBuffer); _accumulateSharpenPipeline = new Fsr2AccumulateSharpenPipeline(_contextDescription, _fsr2ConstantsBuffer); _rcasPipeline = new Fsr2RcasPipeline(_contextDescription, _fsr2ConstantsBuffer, _rcasConstantsBuffer); @@ -149,8 +152,7 @@ namespace FidelityFX DestroyPipeline(ref _reconstructPreviousDepthPipeline); DestroyPipeline(ref _depthClipPipeline); - DestroyResource(ref _dilatedMotionVectorResources[1]); - DestroyResource(ref _dilatedMotionVectorResources[0]); + DestroyResources(_dilatedMotionVectorResources); DestroyResource(ref _autoExposureResource); DestroyResource(ref _defaultReactiveResource); DestroyResource(ref _defaultExposureResource); @@ -223,7 +225,7 @@ namespace FidelityFX _depthClipPipeline.ScheduleDispatch(_commandBuffer, dispatchParams, frameIndex, dispatchSrcX, dispatchSrcY); // Create locks - // _commandBuffer.DispatchCompute(_lockShader, _lockKernel, dispatchSrcX, dispatchSrcY, 1); + _lockPipeline.ScheduleDispatch(_commandBuffer, dispatchParams, frameIndex, dispatchSrcX, dispatchSrcY); bool sharpenEnabled = dispatchParams.EnableSharpening; @@ -463,6 +465,12 @@ namespace FidelityFX resource = null; } + private static void DestroyResources(RenderTexture[] resources) + { + for (int i = 0; i < resources.Length; ++i) + DestroyResource(ref resources[i]); + } + private static void DestroyPipeline(ref Fsr2Pipeline pipeline) { if (pipeline == null) diff --git a/Assets/Scripts/Fsr2Pipeline.cs b/Assets/Scripts/Fsr2Pipeline.cs index 6e73f17..21a89b7 100644 --- a/Assets/Scripts/Fsr2Pipeline.cs +++ b/Assets/Scripts/Fsr2Pipeline.cs @@ -41,6 +41,7 @@ namespace FidelityFX protected static readonly int UavLockInputLuma = Shader.PropertyToID("rw_lock_input_luma"); protected static readonly int UavDilatedReactiveMasks = Shader.PropertyToID("rw_dilated_reactive_masks"); protected static readonly int UavPreparedInputColor = Shader.PropertyToID("rw_prepared_input_color"); + protected static readonly int UavNewLocks = Shader.PropertyToID("rw_new_locks"); // Constant buffer bindings protected static readonly int CbFsr2 = Shader.PropertyToID("cbFSR2"); @@ -69,6 +70,8 @@ namespace FidelityFX // Set up shared aliasable resources, i.e. temporary render textures // These do not need to persist between frames, but they do need to be available between passes + // TODO: we could potentially gather *all* resource binding here, by using CommandBuffer.SetGlobalTexture for everything + // Resource FSR2_SpdAtomicCounter: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R32_UINT, FFX_RESOURCE_FLAGS_ALIASABLE commandBuffer.GetTemporaryRT(UavSpdAtomicCount, 1, 1, 0, FilterMode.Point, GraphicsFormat.R32_UInt, 1, true); @@ -92,6 +95,9 @@ namespace FidelityFX // FSR2_PreparedInputColor: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT, FFX_RESOURCE_FLAGS_ALIASABLE commandBuffer.GetTemporaryRT(UavPreparedInputColor, maxRenderSize.x, maxRenderSize.y, 0, FilterMode.Point, GraphicsFormat.R16G16B16A16_SFloat, 1, true); + + // FSR2_NewLocks: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R8_UNORM, FFX_RESOURCE_FLAGS_ALIASABLE + commandBuffer.GetTemporaryRT(UavNewLocks, displaySize.x, displaySize.y, 0, FilterMode.Point, GraphicsFormat.R8_UNorm, 1, true); } public static void UnregisterResources(CommandBuffer commandBuffer) @@ -232,6 +238,22 @@ namespace FidelityFX } } + internal class Fsr2LockPipeline : Fsr2Pipeline + { + public Fsr2LockPipeline(Fsr2.ContextDescription contextDescription, ComputeBuffer constants) + : base(contextDescription, constants) + { + LoadComputeShader("FSR2/ffx_fsr2_lock_pass"); + } + + public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY) + { + commandBuffer.SetComputeConstantBufferParam(ComputeShader, CbFsr2, Constants, 0, Marshal.SizeOf()); + + commandBuffer.DispatchCompute(ComputeShader, KernelIndex, dispatchX, dispatchY, 1); + } + } + internal class Fsr2AccumulatePipeline : Fsr2Pipeline { public Fsr2AccumulatePipeline(Fsr2.ContextDescription contextDescription, ComputeBuffer constants)