From 47c2560476ca8ab7bc6dca9837946976f34dfebe Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Mon, 5 Jun 2023 15:57:02 +0200 Subject: [PATCH] Consolidated accumulate and accumulate+sharpen pipelines into a single pipeline, with the sharpening option being changed into a local keyword that can be enabled or disabled on-the-fly. This removes the need for an additional copy of the accumulate shader, which saves a considerable amount of memory. --- Assets/Scripts/Core/Fsr2Context.cs | 10 ++----- Assets/Scripts/Core/Fsr2Pipeline.cs | 42 ++++++----------------------- 2 files changed, 10 insertions(+), 42 deletions(-) diff --git a/Assets/Scripts/Core/Fsr2Context.cs b/Assets/Scripts/Core/Fsr2Context.cs index 8a6ea50..c588acb 100644 --- a/Assets/Scripts/Core/Fsr2Context.cs +++ b/Assets/Scripts/Core/Fsr2Context.cs @@ -43,7 +43,6 @@ namespace FidelityFX private Fsr2Pipeline _reconstructPreviousDepthPipeline; private Fsr2Pipeline _lockPipeline; private Fsr2Pipeline _accumulatePipeline; - private Fsr2Pipeline _accumulateSharpenPipeline; private Fsr2Pipeline _rcasPipeline; private Fsr2Pipeline _computeLuminancePyramidPipeline; private Fsr2Pipeline _generateReactivePipeline; @@ -103,7 +102,6 @@ namespace FidelityFX _depthClipPipeline = new Fsr2DepthClipPipeline(_contextDescription, _resources, _fsr2ConstantsBuffer); _lockPipeline = new Fsr2LockPipeline(_contextDescription, _resources, _fsr2ConstantsBuffer); _accumulatePipeline = new Fsr2AccumulatePipeline(_contextDescription, _resources, _fsr2ConstantsBuffer); - _accumulateSharpenPipeline = new Fsr2AccumulateSharpenPipeline(_contextDescription, _resources, _fsr2ConstantsBuffer); _rcasPipeline = new Fsr2RcasPipeline(_contextDescription, _resources, _fsr2ConstantsBuffer, _rcasConstantsBuffer); _generateReactivePipeline = new Fsr2GenerateReactivePipeline(_contextDescription, _resources, _generateReactiveConstantsBuffer); _tcrAutogeneratePipeline = new Fsr2TcrAutogeneratePipeline(_contextDescription, _resources, _fsr2ConstantsBuffer, _tcrAutogenerateConstantsBuffer); @@ -115,7 +113,6 @@ namespace FidelityFX DestroyPipeline(ref _generateReactivePipeline); DestroyPipeline(ref _computeLuminancePyramidPipeline); DestroyPipeline(ref _rcasPipeline); - DestroyPipeline(ref _accumulateSharpenPipeline); DestroyPipeline(ref _accumulatePipeline); DestroyPipeline(ref _lockPipeline); DestroyPipeline(ref _reconstructPreviousDepthPipeline); @@ -241,13 +238,10 @@ namespace FidelityFX // Create locks _lockPipeline.ScheduleDispatch(commandBuffer, dispatchParams, frameIndex, dispatchSrcX, dispatchSrcY); - bool sharpenEnabled = dispatchParams.EnableSharpening; - // Accumulate - var accumulatePipeline = sharpenEnabled ? _accumulateSharpenPipeline : _accumulatePipeline; - accumulatePipeline.ScheduleDispatch(commandBuffer, dispatchParams, frameIndex, dispatchDstX, dispatchDstY); + _accumulatePipeline.ScheduleDispatch(commandBuffer, dispatchParams, frameIndex, dispatchDstX, dispatchDstY); - if (sharpenEnabled) + if (dispatchParams.EnableSharpening) { // Compute the constants SetupRcasConstants(dispatchParams); diff --git a/Assets/Scripts/Core/Fsr2Pipeline.cs b/Assets/Scripts/Core/Fsr2Pipeline.cs index 7db59f4..a89fe59 100644 --- a/Assets/Scripts/Core/Fsr2Pipeline.cs +++ b/Assets/Scripts/Core/Fsr2Pipeline.cs @@ -277,14 +277,22 @@ namespace FidelityFX // Workaround: Disable FP16 path for the accumulate pass on NVIDIA due to reduced occupancy and high VRAM throughput. protected override bool AllowFP16 => SystemInfo.graphicsDeviceVendorID != 0x10DE; + private readonly LocalKeyword _sharpeningKeyword; + public Fsr2AccumulatePipeline(Fsr2.ContextDescription contextDescription, Fsr2Resources resources, ComputeBuffer constants) : base(contextDescription, resources, constants) { LoadComputeShader("FSR2/ffx_fsr2_accumulate_pass"); + _sharpeningKeyword = new LocalKeyword(ComputeShader, "FFX_FSR2_OPTION_APPLY_SHARPENING"); } public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY) { + if (dispatchParams.EnableSharpening) + commandBuffer.EnableKeyword(ComputeShader, _sharpeningKeyword); + else + commandBuffer.DisableKeyword(ComputeShader, _sharpeningKeyword); + if ((ContextDescription.Flags & Fsr2.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvDilatedMotionVectors, Resources.DilatedMotionVectors[frameIndex]); else if (dispatchParams.MotionVectors.HasValue) @@ -316,40 +324,6 @@ namespace FidelityFX } } - internal class Fsr2AccumulateSharpenPipeline : Fsr2AccumulatePipeline - { - private readonly ComputeShader _shaderCopy; - - public Fsr2AccumulateSharpenPipeline(Fsr2.ContextDescription contextDescription, Fsr2Resources resources, ComputeBuffer constants) - : base(contextDescription, resources, constants) - { - // Simply loading the accumulate_pass compute shader will give us the same instance as the non-sharpen pipeline - // So we have to clone the shader instance and set the extra keyword on the new copy - _shaderCopy = UnityEngine.Object.Instantiate(ComputeShader); - foreach (var keyword in ComputeShader.shaderKeywords) - { - _shaderCopy.EnableKeyword(keyword); - } - _shaderCopy.EnableKeyword("FFX_FSR2_OPTION_APPLY_SHARPENING"); - } - - public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY) - { - // Temporarily swap around the shaders so that the dispatch will bind and execute the correct one - ComputeShader tmp = ComputeShader; - ComputeShader = _shaderCopy; - base.ScheduleDispatch(commandBuffer, dispatchParams, frameIndex, dispatchX, dispatchY); - ComputeShader = tmp; - } - - public override void Dispose() - { - // Since we instantiated this copy, we have to destroy it instead of unloading the shader resource - UnityEngine.Object.Destroy(_shaderCopy); - base.Dispose(); - } - } - internal class Fsr2RcasPipeline : Fsr2Pipeline { private readonly ComputeBuffer _rcasConstants;