From 19a74fee4545365abb92b50864def04e83499c92 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 3 Aug 2024 16:42:01 +0200 Subject: [PATCH] Created an abstract base class for context objects, with a few common helper methods --- Runtime/Common/FfxContextBase.cs | 34 +++++++++++++++++++++++++++ Runtime/Common/FfxContextBase.cs.meta | 11 +++++++++ Runtime/FSR2/Fsr2Context.cs | 25 +------------------- Runtime/FSR3/Fsr3UpscalerContext.cs | 25 +------------------- 4 files changed, 47 insertions(+), 48 deletions(-) create mode 100644 Runtime/Common/FfxContextBase.cs create mode 100644 Runtime/Common/FfxContextBase.cs.meta diff --git a/Runtime/Common/FfxContextBase.cs b/Runtime/Common/FfxContextBase.cs new file mode 100644 index 0000000..127cf4f --- /dev/null +++ b/Runtime/Common/FfxContextBase.cs @@ -0,0 +1,34 @@ +using System; +using System.Runtime.InteropServices; +using UnityEngine; + +namespace FidelityFX +{ + public abstract class FfxContextBase + { + protected static ComputeBuffer CreateConstantBuffer() + where TBuf: struct + { + return new ComputeBuffer(1, Marshal.SizeOf(), ComputeBufferType.Constant); + } + + protected static void DestroyConstantBuffer(ref ComputeBuffer bufferRef) + { + if (bufferRef == null) + return; + + bufferRef.Release(); + bufferRef = null; + } + + protected static void DestroyPass(ref TPass pass) + where TPass: class, IDisposable + { + if (pass == null) + return; + + pass.Dispose(); + pass = null; + } + } +} diff --git a/Runtime/Common/FfxContextBase.cs.meta b/Runtime/Common/FfxContextBase.cs.meta new file mode 100644 index 0000000..0aceb0b --- /dev/null +++ b/Runtime/Common/FfxContextBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f4aeccbeb6e61434eb2e50b7190fda8d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/FSR2/Fsr2Context.cs b/Runtime/FSR2/Fsr2Context.cs index 9603e6b..8530c02 100644 --- a/Runtime/FSR2/Fsr2Context.cs +++ b/Runtime/FSR2/Fsr2Context.cs @@ -31,7 +31,7 @@ namespace FidelityFX.FSR2 /// Note that this class does not know anything about Unity render pipelines; all it knows is CommandBuffers and RenderTargetIdentifiers. /// This should make it suitable for integration with any of the available Unity render pipelines. /// - public class Fsr2Context + public class Fsr2Context: FfxContextBase { private const int MaxQueuedFrames = 16; @@ -591,28 +591,5 @@ namespace FidelityFX.FSR2 new Fsr2.RcasConstants(1064229695u, 997604214u), new Fsr2.RcasConstants(1065353216u, 1006648320), }; - - private static ComputeBuffer CreateConstantBuffer() where TConstants: struct - { - return new ComputeBuffer(1, Marshal.SizeOf(), ComputeBufferType.Constant); - } - - private static void DestroyConstantBuffer(ref ComputeBuffer bufferRef) - { - if (bufferRef == null) - return; - - bufferRef.Release(); - bufferRef = null; - } - - private static void DestroyPass(ref Fsr2Pass pass) - { - if (pass == null) - return; - - pass.Dispose(); - pass = null; - } } } diff --git a/Runtime/FSR3/Fsr3UpscalerContext.cs b/Runtime/FSR3/Fsr3UpscalerContext.cs index 18fbced..ccdab06 100644 --- a/Runtime/FSR3/Fsr3UpscalerContext.cs +++ b/Runtime/FSR3/Fsr3UpscalerContext.cs @@ -31,7 +31,7 @@ namespace FidelityFX.FSR3 /// Note that this class does not know anything about Unity render pipelines; all it knows is CommandBuffers and RenderTargetIdentifiers. /// This should make it suitable for integration with any of the available Unity render pipelines. /// - public class Fsr3UpscalerContext + public class Fsr3UpscalerContext: FfxContextBase { private const int MaxQueuedFrames = 16; @@ -630,28 +630,5 @@ namespace FidelityFX.FSR3 new Fsr3Upscaler.RcasConstants(1064229695u, 997604214u), new Fsr3Upscaler.RcasConstants(1065353216u, 1006648320), }; - - private static ComputeBuffer CreateConstantBuffer() where TConstants: struct - { - return new ComputeBuffer(1, Marshal.SizeOf(), ComputeBufferType.Constant); - } - - private static void DestroyConstantBuffer(ref ComputeBuffer bufferRef) - { - if (bufferRef == null) - return; - - bufferRef.Release(); - bufferRef = null; - } - - private static void DestroyPass(ref Fsr3UpscalerPass pass) - { - if (pass == null) - return; - - pass.Dispose(); - pass = null; - } } }