diff --git a/Runtime/Common/FfxResourcesBase.cs b/Runtime/Common/FfxResourcesBase.cs new file mode 100644 index 0000000..aac9e5c --- /dev/null +++ b/Runtime/Common/FfxResourcesBase.cs @@ -0,0 +1,80 @@ +using System; +using System.Runtime.InteropServices; +using UnityEngine; +using UnityEngine.Experimental.Rendering; + +namespace FidelityFX +{ + internal abstract class FfxResourcesBase + { + protected static ComputeBuffer CreateBuffer(string name, int count) + { + return new ComputeBuffer(count, Marshal.SizeOf()); + } + + protected static RenderTexture CreateResource(string name, in Vector2Int size, GraphicsFormat format) + { + var rt = new RenderTexture(size.x, size.y, 0, format) { name = name, enableRandomWrite = true }; + rt.Create(); + return rt; + } + + protected static RenderTexture CreateResourceMips(string name, in Vector2Int size, GraphicsFormat format) + { + int mipCount = 1 + Mathf.FloorToInt(Mathf.Log(Math.Max(size.x, size.y), 2.0f)); + var rt = new RenderTexture(size.x, size.y, 0, format, mipCount) { name = name, enableRandomWrite = true, useMipMap = true, autoGenerateMips = false }; + rt.Create(); + return rt; + } + + protected static void CreateDoubleBufferedResource(RenderTexture[] resource, string name, in Vector2Int size, GraphicsFormat format, int numElements = 2) + { + numElements = Math.Min(resource.Length, numElements); + for (int i = 0; i < numElements; ++i) + { + resource[i] = new RenderTexture(size.x, size.y, 0, format) { name = name + (i + 1), enableRandomWrite = true }; + resource[i].Create(); + } + } + + protected static void DestroyResource(ref Texture2D resource) + { + if (resource == null) + return; + +#if UNITY_EDITOR + if (Application.isPlaying && !UnityEditor.EditorApplication.isPaused) + UnityEngine.Object.Destroy(resource); + else + UnityEngine.Object.DestroyImmediate(resource); +#else + UnityEngine.Object.Destroy(resource); +#endif + resource = null; + } + + protected static void DestroyResource(ref RenderTexture resource) + { + if (resource == null) + return; + + resource.Release(); + resource = null; + } + + protected static void DestroyResource(ref ComputeBuffer resource) + { + if (resource == null) + return; + + resource.Release(); + resource = null; + } + + protected static void DestroyResource(RenderTexture[] resource) + { + for (int i = 0; i < resource.Length; ++i) + DestroyResource(ref resource[i]); + } + } +} diff --git a/Runtime/Common/FfxResourcesBase.cs.meta b/Runtime/Common/FfxResourcesBase.cs.meta new file mode 100644 index 0000000..7398dad --- /dev/null +++ b/Runtime/Common/FfxResourcesBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2a46b97ac72c45545a80a896462e1112 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/FSR2/Fsr2Resources.cs b/Runtime/FSR2/Fsr2Resources.cs index 0314a45..4252c85 100644 --- a/Runtime/FSR2/Fsr2Resources.cs +++ b/Runtime/FSR2/Fsr2Resources.cs @@ -29,7 +29,7 @@ namespace FidelityFX.FSR2 /// Helper class for bundling and managing persistent resources required by the FSR2 process. /// This includes lookup tables, default fallback resources and double-buffered resources that get swapped between frames. /// - internal class Fsr2Resources + internal class Fsr2Resources: FfxResourcesBase { public Texture2D DefaultExposure; public Texture2D DefaultReactive; @@ -169,15 +169,6 @@ namespace FidelityFX.FSR2 commandBuffer.ReleaseTemporaryRT(Fsr2ShaderIDs.UavNewLocks); } - private static void CreateDoubleBufferedResource(RenderTexture[] resource, string name, Vector2Int size, GraphicsFormat format) - { - for (int i = 0; i < 2; ++i) - { - resource[i] = new RenderTexture(size.x, size.y, 0, format) { name = name + (i + 1), enableRandomWrite = true }; - resource[i].Create(); - } - } - public void Destroy() { DestroyTcrAutogenResources(); @@ -201,37 +192,6 @@ namespace FidelityFX.FSR2 DestroyResource(ref AutoComposition); DestroyResource(ref AutoReactive); } - - private static void DestroyResource(ref Texture2D resource) - { - if (resource == null) - return; - -#if UNITY_EDITOR - if (Application.isPlaying && !UnityEditor.EditorApplication.isPaused) - UnityEngine.Object.Destroy(resource); - else - UnityEngine.Object.DestroyImmediate(resource); -#else - UnityEngine.Object.Destroy(resource); -#endif - resource = null; - } - - private static void DestroyResource(ref RenderTexture resource) - { - if (resource == null) - return; - - resource.Release(); - resource = null; - } - - private static void DestroyResource(RenderTexture[] resource) - { - for (int i = 0; i < resource.Length; ++i) - DestroyResource(ref resource[i]); - } private const int MaximumBiasTextureWidth = 16; private const int MaximumBiasTextureHeight = 16; diff --git a/Runtime/FSR3/Fsr3UpscalerResources.cs b/Runtime/FSR3/Fsr3UpscalerResources.cs index d591215..150fa89 100644 --- a/Runtime/FSR3/Fsr3UpscalerResources.cs +++ b/Runtime/FSR3/Fsr3UpscalerResources.cs @@ -29,7 +29,7 @@ namespace FidelityFX.FSR3 /// Helper class for bundling and managing persistent resources required by the FSR3 Upscaler process. /// This includes lookup tables, default fallback resources and double-buffered resources that get swapped between frames. /// - internal class Fsr3UpscalerResources + internal class Fsr3UpscalerResources: FfxResourcesBase { public Texture2D LanczosLut; public Texture2D DefaultExposure; @@ -173,15 +173,6 @@ namespace FidelityFX.FSR3 commandBuffer.ReleaseTemporaryRT(Fsr3ShaderIDs.UavIntermediate); } - private static void CreateDoubleBufferedResource(RenderTexture[] resource, string name, Vector2Int size, GraphicsFormat format) - { - for (int i = 0; i < 2; ++i) - { - resource[i] = new RenderTexture(size.x, size.y, 0, format) { name = name + (i + 1), enableRandomWrite = true }; - resource[i].Create(); - } - } - public void Destroy() { DestroyTcrAutogenResources(); @@ -210,36 +201,5 @@ namespace FidelityFX.FSR3 DestroyResource(ref AutoComposition); DestroyResource(ref AutoReactive); } - - private static void DestroyResource(ref Texture2D resource) - { - if (resource == null) - return; - -#if UNITY_EDITOR - if (Application.isPlaying && !UnityEditor.EditorApplication.isPaused) - UnityEngine.Object.Destroy(resource); - else - UnityEngine.Object.DestroyImmediate(resource); -#else - UnityEngine.Object.Destroy(resource); -#endif - resource = null; - } - - private static void DestroyResource(ref RenderTexture resource) - { - if (resource == null) - return; - - resource.Release(); - resource = null; - } - - private static void DestroyResource(RenderTexture[] resource) - { - for (int i = 0; i < resource.Length; ++i) - DestroyResource(ref resource[i]); - } } }