From 6504adeea66c65aebea1178bfa6348ac20bd4b0c Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sun, 11 Jun 2023 15:01:30 +0200 Subject: [PATCH 1/2] Create temporary resources at actual render size, reducing VRAM usage when dynamic resolution is in effect. --- .../PostProcessing/Runtime/FSR2/Fsr2Pipeline.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/FSR2/Fsr2Pipeline.cs b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/FSR2/Fsr2Pipeline.cs index 0ef1a4b..7a3bd75 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/FSR2/Fsr2Pipeline.cs +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/FSR2/Fsr2Pipeline.cs @@ -61,25 +61,25 @@ namespace FidelityFX public static void RegisterResources(CommandBuffer commandBuffer, Fsr2.ContextDescription contextDescription, Fsr2.DispatchDescription dispatchParams) { Vector2Int displaySize = contextDescription.DisplaySize; - Vector2Int maxRenderSize = contextDescription.MaxRenderSize; + Vector2Int renderSize = dispatchParams.RenderSize; // 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 // FSR2_ReconstructedPrevNearestDepth: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R32_UINT, FFX_RESOURCE_FLAGS_ALIASABLE - commandBuffer.GetTemporaryRT(Fsr2ShaderIDs.UavReconstructedPrevNearestDepth, maxRenderSize.x, maxRenderSize.y, 0, default, GraphicsFormat.R32_UInt, 1, true); + commandBuffer.GetTemporaryRT(Fsr2ShaderIDs.UavReconstructedPrevNearestDepth, renderSize.x, renderSize.y, 0, default, GraphicsFormat.R32_UInt, 1, true); // FSR2_DilatedDepth: FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R32_FLOAT, FFX_RESOURCE_FLAGS_ALIASABLE - commandBuffer.GetTemporaryRT(Fsr2ShaderIDs.UavDilatedDepth, maxRenderSize.x, maxRenderSize.y, 0, default, GraphicsFormat.R32_SFloat, 1, true); + commandBuffer.GetTemporaryRT(Fsr2ShaderIDs.UavDilatedDepth, renderSize.x, renderSize.y, 0, default, GraphicsFormat.R32_SFloat, 1, true); // FSR2_LockInputLuma: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R16_FLOAT, FFX_RESOURCE_FLAGS_ALIASABLE - commandBuffer.GetTemporaryRT(Fsr2ShaderIDs.UavLockInputLuma, maxRenderSize.x, maxRenderSize.y, 0, default, GraphicsFormat.R16_SFloat, 1, true); + commandBuffer.GetTemporaryRT(Fsr2ShaderIDs.UavLockInputLuma, renderSize.x, renderSize.y, 0, default, GraphicsFormat.R16_SFloat, 1, true); // FSR2_DilatedReactiveMasks: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R8G8_UNORM, FFX_RESOURCE_FLAGS_ALIASABLE - commandBuffer.GetTemporaryRT(Fsr2ShaderIDs.UavDilatedReactiveMasks, maxRenderSize.x, maxRenderSize.y, 0, default, GraphicsFormat.R8G8_UNorm, 1, true); + commandBuffer.GetTemporaryRT(Fsr2ShaderIDs.UavDilatedReactiveMasks, renderSize.x, renderSize.y, 0, default, GraphicsFormat.R8G8_UNorm, 1, true); // FSR2_PreparedInputColor: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT, FFX_RESOURCE_FLAGS_ALIASABLE - commandBuffer.GetTemporaryRT(Fsr2ShaderIDs.UavPreparedInputColor, maxRenderSize.x, maxRenderSize.y, 0, default, GraphicsFormat.R16G16B16A16_SFloat, 1, true); + commandBuffer.GetTemporaryRT(Fsr2ShaderIDs.UavPreparedInputColor, renderSize.x, renderSize.y, 0, default, GraphicsFormat.R16G16B16A16_SFloat, 1, true); // FSR2_NewLocks: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R8_UNORM, FFX_RESOURCE_FLAGS_ALIASABLE commandBuffer.GetTemporaryRT(Fsr2ShaderIDs.UavNewLocks, displaySize.x, displaySize.y, 0, default, GraphicsFormat.R8_UNorm, 1, true); From 70c7ea0e511dbf9a16ea3741470a0e810a566b29 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sun, 11 Jun 2023 17:37:19 +0200 Subject: [PATCH 2/2] Restored compatibility with Unity versions older than 2021.2, which didn't have the LocalKeyword struct yet. --- .../PostProcessing/Runtime/FSR2/Fsr2Pipeline.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/FSR2/Fsr2Pipeline.cs b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/FSR2/Fsr2Pipeline.cs index 7a3bd75..6e6b19f 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/FSR2/Fsr2Pipeline.cs +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/FSR2/Fsr2Pipeline.cs @@ -271,24 +271,37 @@ namespace FidelityFX internal class Fsr2AccumulatePipeline : Fsr2Pipeline { + private const string SharpeningKeyword = "FFX_FSR2_OPTION_APPLY_SHARPENING"; + // 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; +#if UNITY_2021_2_OR_NEWER private readonly LocalKeyword _sharpeningKeyword; +#endif 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"); +#if UNITY_2021_2_OR_NEWER + _sharpeningKeyword = new LocalKeyword(ComputeShader, SharpeningKeyword); +#endif } public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY) { +#if UNITY_2021_2_OR_NEWER if (dispatchParams.EnableSharpening) commandBuffer.EnableKeyword(ComputeShader, _sharpeningKeyword); else commandBuffer.DisableKeyword(ComputeShader, _sharpeningKeyword); +#else + if (dispatchParams.EnableSharpening) + commandBuffer.EnableShaderKeyword(SharpeningKeyword); + else + commandBuffer.DisableShaderKeyword(SharpeningKeyword); +#endif if ((ContextDescription.Flags & Fsr2.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvDilatedMotionVectors, Resources.DilatedMotionVectors[frameIndex]);