using System; using System.Collections.Generic; namespace UnityEngine.Experimental.Rendering.RenderGraphModule { /// /// Helper class provided in the RenderGraphContext to all Render Passes. /// It allows you to do temporary allocations of various objects during a Render Pass. /// public sealed class RenderGraphObjectPool { abstract class SharedObjectPoolBase { protected static List s_AllocatedPools = new List(); protected abstract void Clear(); public static void ClearAll() { foreach (var pool in s_AllocatedPools) pool.Clear(); } } class SharedObjectPool : SharedObjectPoolBase where T : new() { Stack m_Pool = new Stack(); public T Get() { var result = m_Pool.Count == 0 ? new T() : m_Pool.Pop(); return result; } public void Release(T value) { m_Pool.Push(value); } static SharedObjectPool AllocatePool() { var pool = new SharedObjectPool(); s_AllocatedPools.Add(pool); return pool; } override protected void Clear() { m_Pool.Clear(); } static readonly Lazy> s_Instance = new Lazy>(AllocatePool); public static SharedObjectPool sharedPool => s_Instance.Value; } Dictionary<(Type, int), Stack> m_ArrayPool = new Dictionary<(Type, int), Stack>(); List<(object, (Type, int))> m_AllocatedArrays = new List<(object, (Type, int))>(); List m_AllocatedMaterialPropertyBlocks = new List(); internal RenderGraphObjectPool() { } /// /// Allocate a temporary typed array of a specific size. /// Unity releases the array at the end of the Render Pass. /// /// Type of the array to be allocated. /// Number of element in the array. /// A new array of type T with size number of elements. public T[] GetTempArray(int size) { if (!m_ArrayPool.TryGetValue((typeof(T), size), out var stack)) { stack = new Stack(); m_ArrayPool.Add((typeof(T), size), stack); } var result = stack.Count > 0 ? (T[])stack.Pop() : new T[size]; m_AllocatedArrays.Add((result, (typeof(T), size))); return result; } /// /// Allocate a temporary MaterialPropertyBlock for the Render Pass. /// /// A new clean MaterialPropertyBlock. public MaterialPropertyBlock GetTempMaterialPropertyBlock() { var result = SharedObjectPool.sharedPool.Get(); result.Clear(); m_AllocatedMaterialPropertyBlocks.Add(result); return result; } internal void ReleaseAllTempAlloc() { foreach (var arrayDesc in m_AllocatedArrays) { bool result = m_ArrayPool.TryGetValue(arrayDesc.Item2, out var stack); Debug.Assert(result, "Correct stack type should always be allocated."); stack.Push(arrayDesc.Item1); } m_AllocatedArrays.Clear(); foreach (var mpb in m_AllocatedMaterialPropertyBlocks) { SharedObjectPool.sharedPool.Release(mpb); } m_AllocatedMaterialPropertyBlocks.Clear(); } // Regular pooling API. Only internal use for now internal T Get() where T : new() { var pool = SharedObjectPool.sharedPool; return pool.Get(); } internal void Release(T value) where T : new() { var pool = SharedObjectPool.sharedPool; pool.Release(value); } internal void Cleanup() { m_AllocatedArrays.Clear(); m_AllocatedMaterialPropertyBlocks.Clear(); m_ArrayPool.Clear(); SharedObjectPoolBase.ClearAll(); } } }