using UnityEngine; using UnityEngine.Experimental.Rendering; using UnityEngine.Rendering; namespace FidelityFX { internal class CacaoResources { public RenderTexture LoadCounter; public RenderTexture DeinterleavedDepths; public RenderTexture DeinterleavedNormals; public RenderTexture SsaoBufferPing; public RenderTexture SsaoBufferPong; public RenderTexture ImportanceMap; public RenderTexture ImportanceMapPong; public RenderTexture DownsampledSsaoBuffer; public bool CreateLoadCounter() { LoadCounter = new RenderTexture(1, 1, 0, GraphicsFormat.R32_UInt) { name = "FFX_CACAO_LOAD_COUNTER", enableRandomWrite = true }; if (!LoadCounter.Create()) { Debug.LogError("Failed to create load counter resource"); return false; } return true; } public void DestroyLoadCounter() { DestroyResource(ref LoadCounter); } public bool CreateResources(in Cacao.BufferSizeInfo bsi) { CreateArrayResource(out DeinterleavedDepths, "FFX_CACAO_DEINTERLEAVED_DEPTHS", bsi.DeinterleavedDepthBufferWidth, bsi.DeinterleavedDepthBufferHeight, GraphicsFormat.R16_SFloat, 4,4); CreateArrayResource(out DeinterleavedNormals, "FFX_CACAO_DEINTERLEAVED_NORMALS", bsi.SsaoBufferWidth, bsi.SsaoBufferHeight, GraphicsFormat.R8G8B8A8_SNorm, 4); // TODO: does SNorm work? CreateArrayResource(out SsaoBufferPing, "FFX_CACAO_SSAO_BUFFER_PING", bsi.SsaoBufferWidth, bsi.SsaoBufferHeight, GraphicsFormat.R8G8_UNorm, 4); CreateArrayResource(out SsaoBufferPong, "FFX_CACAO_SSAO_BUFFER_PONG", bsi.SsaoBufferWidth, bsi.SsaoBufferHeight, GraphicsFormat.R8G8_UNorm, 4); CreateResource(out ImportanceMap, "FFX_CACAO_IMPORTANCE_MAP", bsi.ImportanceMapWidth, bsi.ImportanceMapHeight, GraphicsFormat.R8_UNorm); CreateResource(out ImportanceMapPong, "FFX_CACAO_IMPORTANCE_MAP_PONG", bsi.ImportanceMapWidth, bsi.ImportanceMapHeight, GraphicsFormat.R8_UNorm); CreateResource(out DownsampledSsaoBuffer, "FFX_CACAO_DOWNSAMPLED_SSAO_BUFFER", bsi.DownsampledSsaoBufferWidth, bsi.DownsampledSsaoBufferHeight, GraphicsFormat.R8_UNorm); return true; } public void DestroyResources() { DestroyResource(ref DownsampledSsaoBuffer); DestroyResource(ref ImportanceMapPong); DestroyResource(ref ImportanceMap); DestroyResource(ref SsaoBufferPong); DestroyResource(ref SsaoBufferPing); DestroyResource(ref DeinterleavedNormals); DestroyResource(ref DeinterleavedDepths); } private static bool CreateResource(out RenderTexture resource, string name, uint width, uint height, GraphicsFormat format) { resource = new RenderTexture((int)width, (int)height, 0, format) { name = name, enableRandomWrite = true }; return resource.Create(); } private static bool CreateArrayResource(out RenderTexture resource, string name, uint width, uint height, GraphicsFormat format, int arraySize, int mipCount = 1) { var desc = new RenderTextureDescriptor { width = (int)width, height = (int)height, depthBufferBits = 0, msaaSamples = 1, graphicsFormat = format, useMipMap = mipCount > 1, mipCount = mipCount, autoGenerateMips = false, enableRandomWrite = true, dimension = TextureDimension.Tex2DArray, volumeDepth = arraySize, }; resource = new RenderTexture(desc); return resource.Create(); } private static void DestroyResource(ref RenderTexture resource) { if (resource == null) return; resource.Release(); resource = null; } } }