Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RenderGraphCompilationCache.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Collections.Generic;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.RenderGraphModule;
  4. using UnityEngine.Rendering.RenderGraphModule.NativeRenderPassCompiler;
  5. class RenderGraphCompilationCache
  6. {
  7. struct HashEntry<T>
  8. {
  9. public int hash;
  10. public int lastFrameUsed;
  11. public T compiledGraph;
  12. }
  13. DynamicArray<HashEntry<RenderGraph.CompiledGraph>> m_HashEntries = new();
  14. DynamicArray<HashEntry<CompilerContextData>> m_NativeHashEntries = new();
  15. Stack<RenderGraph.CompiledGraph> m_CompiledGraphPool = new();
  16. Stack<CompilerContextData> m_NativeCompiledGraphPool = new();
  17. static int HashEntryComparer<T>(HashEntry<T> a, HashEntry<T> b)
  18. {
  19. if (a.lastFrameUsed < b.lastFrameUsed)
  20. return -1;
  21. else if (a.lastFrameUsed > b.lastFrameUsed)
  22. return 1;
  23. else
  24. return 0;
  25. }
  26. static DynamicArray<HashEntry<RenderGraph.CompiledGraph>>.SortComparer s_EntryComparer = HashEntryComparer<RenderGraph.CompiledGraph>;
  27. static DynamicArray<HashEntry<CompilerContextData>>.SortComparer s_NativeEntryComparer = HashEntryComparer<CompilerContextData>;
  28. const int k_CachedGraphCount = 20;
  29. public RenderGraphCompilationCache()
  30. {
  31. for (int i = 0; i < k_CachedGraphCount; ++i)
  32. {
  33. m_CompiledGraphPool.Push(new RenderGraph.CompiledGraph());
  34. m_NativeCompiledGraphPool.Push(new CompilerContextData(NativePassCompiler.k_EstimatedPassCount));
  35. }
  36. }
  37. // Avoid GC in lambda.
  38. static int s_Hash;
  39. bool GetCompilationCache<T>(int hash, int frameIndex, out T outGraph, DynamicArray<HashEntry<T>> hashEntries, Stack<T> pool, DynamicArray<HashEntry<T>>.SortComparer comparer)
  40. where T : RenderGraph.ICompiledGraph
  41. {
  42. s_Hash = hash;
  43. int index = hashEntries.FindIndex(0, hashEntries.size, (value) => value.hash == s_Hash);
  44. if (index != -1)
  45. {
  46. ref var entry = ref hashEntries[index];
  47. outGraph = entry.compiledGraph;
  48. entry.lastFrameUsed = frameIndex;
  49. return true;
  50. }
  51. else
  52. {
  53. if (pool.Count != 0)
  54. {
  55. var newEntry = new HashEntry<T>()
  56. {
  57. hash = hash,
  58. lastFrameUsed = frameIndex,
  59. compiledGraph = pool.Pop()
  60. };
  61. hashEntries.Add(newEntry);
  62. outGraph = newEntry.compiledGraph;
  63. return false;
  64. }
  65. else
  66. {
  67. // Reuse the oldest one.
  68. hashEntries.QuickSort(comparer);
  69. ref var oldestEntry = ref hashEntries[0];
  70. oldestEntry.hash = hash;
  71. oldestEntry.lastFrameUsed = frameIndex;
  72. oldestEntry.compiledGraph.Clear();
  73. outGraph = oldestEntry.compiledGraph;
  74. return false;
  75. }
  76. }
  77. }
  78. public bool GetCompilationCache(int hash, int frameIndex, out RenderGraph.CompiledGraph outGraph)
  79. {
  80. return GetCompilationCache(hash, frameIndex, out outGraph, m_HashEntries, m_CompiledGraphPool, s_EntryComparer);
  81. }
  82. public bool GetCompilationCache(int hash, int frameIndex, out CompilerContextData outGraph)
  83. {
  84. return GetCompilationCache(hash, frameIndex, out outGraph, m_NativeHashEntries, m_NativeCompiledGraphPool, s_NativeEntryComparer);
  85. }
  86. public void Clear()
  87. {
  88. for (int i = 0; i < m_HashEntries.size; ++i)
  89. m_CompiledGraphPool.Push(m_HashEntries[i].compiledGraph);
  90. m_HashEntries.Clear();
  91. for (int i = 0; i < m_NativeHashEntries.size; ++i)
  92. m_NativeCompiledGraphPool.Push(m_NativeHashEntries[i].compiledGraph);
  93. m_NativeHashEntries.Clear();
  94. }
  95. }