CacheByRange.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Graphics.Vulkan
  4. {
  5. interface ICacheKey : IDisposable
  6. {
  7. bool KeyEqual(ICacheKey other);
  8. }
  9. struct I8ToI16CacheKey : ICacheKey
  10. {
  11. // Used to notify the pipeline that bindings have invalidated on dispose.
  12. private readonly VulkanRenderer _gd;
  13. private Auto<DisposableBuffer> _buffer;
  14. public I8ToI16CacheKey(VulkanRenderer gd)
  15. {
  16. _gd = gd;
  17. _buffer = null;
  18. }
  19. public bool KeyEqual(ICacheKey other)
  20. {
  21. return other is I8ToI16CacheKey;
  22. }
  23. public void Dispose()
  24. {
  25. _gd.PipelineInternal.DirtyIndexBuffer(_buffer);
  26. }
  27. }
  28. struct AlignedVertexBufferCacheKey : ICacheKey
  29. {
  30. private readonly int _stride;
  31. private readonly int _alignment;
  32. // Used to notify the pipeline that bindings have invalidated on dispose.
  33. private readonly VulkanRenderer _gd;
  34. private Auto<DisposableBuffer> _buffer;
  35. public AlignedVertexBufferCacheKey(VulkanRenderer gd, int stride, int alignment)
  36. {
  37. _gd = gd;
  38. _stride = stride;
  39. _alignment = alignment;
  40. _buffer = null;
  41. }
  42. public bool KeyEqual(ICacheKey other)
  43. {
  44. return other is AlignedVertexBufferCacheKey entry &&
  45. entry._stride == _stride &&
  46. entry._alignment == _alignment;
  47. }
  48. public void SetBuffer(Auto<DisposableBuffer> buffer)
  49. {
  50. _buffer = buffer;
  51. }
  52. public void Dispose()
  53. {
  54. _gd.PipelineInternal.DirtyVertexBuffer(_buffer);
  55. }
  56. }
  57. struct TopologyConversionCacheKey : ICacheKey
  58. {
  59. private IndexBufferPattern _pattern;
  60. private int _indexSize;
  61. // Used to notify the pipeline that bindings have invalidated on dispose.
  62. private readonly VulkanRenderer _gd;
  63. private Auto<DisposableBuffer> _buffer;
  64. public TopologyConversionCacheKey(VulkanRenderer gd, IndexBufferPattern pattern, int indexSize)
  65. {
  66. _gd = gd;
  67. _pattern = pattern;
  68. _indexSize = indexSize;
  69. _buffer = null;
  70. }
  71. public bool KeyEqual(ICacheKey other)
  72. {
  73. return other is TopologyConversionCacheKey entry &&
  74. entry._pattern == _pattern &&
  75. entry._indexSize == _indexSize;
  76. }
  77. public void SetBuffer(Auto<DisposableBuffer> buffer)
  78. {
  79. _buffer = buffer;
  80. }
  81. public void Dispose()
  82. {
  83. _gd.PipelineInternal.DirtyIndexBuffer(_buffer);
  84. }
  85. }
  86. struct CacheByRange<T> where T : IDisposable
  87. {
  88. private struct Entry
  89. {
  90. public ICacheKey Key;
  91. public T Value;
  92. public Entry(ICacheKey key, T value)
  93. {
  94. Key = key;
  95. Value = value;
  96. }
  97. }
  98. private Dictionary<ulong, List<Entry>> _ranges;
  99. public void Add(int offset, int size, ICacheKey key, T value)
  100. {
  101. List<Entry> entries = GetEntries(offset, size);
  102. entries.Add(new Entry(key, value));
  103. }
  104. public bool TryGetValue(int offset, int size, ICacheKey key, out T value)
  105. {
  106. List<Entry> entries = GetEntries(offset, size);
  107. foreach (Entry entry in entries)
  108. {
  109. if (entry.Key.KeyEqual(key))
  110. {
  111. value = entry.Value;
  112. return true;
  113. }
  114. }
  115. value = default;
  116. return false;
  117. }
  118. public void Clear()
  119. {
  120. if (_ranges != null)
  121. {
  122. foreach (List<Entry> entries in _ranges.Values)
  123. {
  124. foreach (Entry entry in entries)
  125. {
  126. entry.Key.Dispose();
  127. entry.Value.Dispose();
  128. }
  129. }
  130. _ranges.Clear();
  131. _ranges = null;
  132. }
  133. }
  134. private List<Entry> GetEntries(int offset, int size)
  135. {
  136. if (_ranges == null)
  137. {
  138. _ranges = new Dictionary<ulong, List<Entry>>();
  139. }
  140. ulong key = PackRange(offset, size);
  141. List<Entry> value;
  142. if (!_ranges.TryGetValue(key, out value))
  143. {
  144. value = new List<Entry>();
  145. _ranges.Add(key, value);
  146. }
  147. return value;
  148. }
  149. private static ulong PackRange(int offset, int size)
  150. {
  151. return (uint)offset | ((ulong)size << 32);
  152. }
  153. public void Dispose()
  154. {
  155. Clear();
  156. }
  157. }
  158. }