From a576f94e9549c4527040342455f3bb5b9f7fc9ce Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 18 Feb 2023 13:08:12 +0100 Subject: [PATCH] A bit of reorganization in how shaders and buffers are created and destroyed. Also using Unity's system info to decide on inverted depth or not. --- Assets/Scripts/Fsr2.cs | 6 ++- Assets/Scripts/Fsr2Callbacks.cs | 5 +++ Assets/Scripts/Fsr2Context.cs | 71 +++++++++++++++++++++------------ 3 files changed, 55 insertions(+), 27 deletions(-) diff --git a/Assets/Scripts/Fsr2.cs b/Assets/Scripts/Fsr2.cs index d10e047..062b2f5 100644 --- a/Assets/Scripts/Fsr2.cs +++ b/Assets/Scripts/Fsr2.cs @@ -1,5 +1,6 @@ using System; using UnityEngine; +using UnityEngine.Rendering; namespace FidelityFX { @@ -63,7 +64,10 @@ namespace FidelityFX /// public static Fsr2Context CreateContext(InitializationFlags flags = 0) { - // flags |= InitializationFlags.EnableDepthInverted; // Depends on the runtime platform + if (SystemInfo.usesReversedZBuffer) + flags |= InitializationFlags.EnableDepthInverted; + else + flags &= ~InitializationFlags.EnableDepthInverted; var contextDescription = new ContextDescription { diff --git a/Assets/Scripts/Fsr2Callbacks.cs b/Assets/Scripts/Fsr2Callbacks.cs index 0238bee..83845d3 100644 --- a/Assets/Scripts/Fsr2Callbacks.cs +++ b/Assets/Scripts/Fsr2Callbacks.cs @@ -14,6 +14,11 @@ namespace FidelityFX return Resources.Load(name); } + public virtual void UnloadComputeShader(ComputeShader shader) + { + Resources.UnloadAsset(shader); + } + public virtual void ApplyMipmapBias(float biasOffset) { foreach (var texture in Resources.FindObjectsOfTypeAll()) diff --git a/Assets/Scripts/Fsr2Context.cs b/Assets/Scripts/Fsr2Context.cs index 8625692..8f7578d 100644 --- a/Assets/Scripts/Fsr2Context.cs +++ b/Assets/Scripts/Fsr2Context.cs @@ -32,9 +32,9 @@ namespace FidelityFX { _contextDescription = contextDescription; - _fsr2ConstantsBuffer = new ComputeBuffer(1, Marshal.SizeOf(), ComputeBufferType.Constant); - _spdConstantsBuffer = new ComputeBuffer(1, Marshal.SizeOf(), ComputeBufferType.Constant); - _rcasConstantsBuffer = new ComputeBuffer(1, Marshal.SizeOf(), ComputeBufferType.Constant); + _fsr2ConstantsBuffer = CreateConstantBuffer(); + _spdConstantsBuffer = CreateConstantBuffer(); + _rcasConstantsBuffer = CreateConstantBuffer(); // Set defaults _fsr2ConstantsArray[0].displaySize = _contextDescription.DisplaySize; @@ -71,12 +71,6 @@ namespace FidelityFX LoadComputeShader("FSR2/ffx_fsr2_tcr_autogen_pass", ref _tcrAutogenShader); } - private void LoadComputeShader(string name, ref ComputeShader shaderRef) - { - if (shaderRef == null) - shaderRef = _contextDescription.Callbacks.LoadComputeShader(name); - } - public void Dispatch(Fsr2.DispatchDescription dispatchDescription) { _fsr2ConstantsArray[0].preExposure = dispatchDescription.PreExposure; @@ -110,25 +104,21 @@ namespace FidelityFX public void Destroy() { - if (_rcasConstantsBuffer != null) - { - _rcasConstantsBuffer.Release(); - _rcasConstantsBuffer = null; - } + DestroyConstantBuffer(ref _rcasConstantsBuffer); + DestroyConstantBuffer(ref _spdConstantsBuffer); + DestroyConstantBuffer(ref _fsr2ConstantsBuffer); - if (_spdConstantsBuffer != null) - { - _spdConstantsBuffer.Release(); - _spdConstantsBuffer = null; - } - - if (_fsr2ConstantsBuffer != null) - { - _fsr2ConstantsBuffer.Release(); - _fsr2ConstantsBuffer = null; - } + DestroyComputeShader(ref _tcrAutogenShader); + DestroyComputeShader(ref _generateReactiveShader); + DestroyComputeShader(ref _accumulateShader); + DestroyComputeShader(ref _lockShader); + DestroyComputeShader(ref _reconstructPreviousDepthShader); + DestroyComputeShader(ref _depthClipShader); + DestroyComputeShader(ref _prepareInputColorShader); + DestroyComputeShader(ref _rcasShader); + DestroyComputeShader(ref _computeLuminancePyramidShader); } - + [Serializable, StructLayout(LayoutKind.Sequential)] private struct Fsr2Constants { @@ -203,5 +193,34 @@ namespace FidelityFX new(1064229695u, 997604214u), new(1065353216u, 1006648320), }; + + private static ComputeBuffer CreateConstantBuffer() where TConstants: struct + { + return new ComputeBuffer(1, Marshal.SizeOf(), ComputeBufferType.Constant); + } + + private void LoadComputeShader(string name, ref ComputeShader shaderRef) + { + if (shaderRef == null) + shaderRef = _contextDescription.Callbacks.LoadComputeShader(name); + } + + private static void DestroyConstantBuffer(ref ComputeBuffer bufferRef) + { + if (bufferRef == null) + return; + + bufferRef.Release(); + bufferRef = null; + } + + private void DestroyComputeShader(ref ComputeShader shaderRef) + { + if (shaderRef == null) + return; + + _contextDescription.Callbacks.UnloadComputeShader(shaderRef); + shaderRef = null; + } } }