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 Texture2D CreateLookup(string name, GraphicsFormat format, Color data) { var tex = new Texture2D(1, 1, format, TextureCreationFlags.None) { name = name }; tex.SetPixel(0, 0, data); tex.Apply(); return tex; } protected static Texture2D CreateLookup(string name, in Vector2Int size, GraphicsFormat format, T[] data) { var tex = new Texture2D(size.x, size.y, format, TextureCreationFlags.None) { name = name }; tex.SetPixelData(data, 0); tex.Apply(); return tex; } 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]); } } }