CacheByRange.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 SetBuffer(Auto<DisposableBuffer> buffer)
  24. {
  25. _buffer = buffer;
  26. }
  27. public void Dispose()
  28. {
  29. _gd.PipelineInternal.DirtyIndexBuffer(_buffer);
  30. }
  31. }
  32. struct AlignedVertexBufferCacheKey : ICacheKey
  33. {
  34. private readonly int _stride;
  35. private readonly int _alignment;
  36. // Used to notify the pipeline that bindings have invalidated on dispose.
  37. private readonly VulkanRenderer _gd;
  38. private Auto<DisposableBuffer> _buffer;
  39. public AlignedVertexBufferCacheKey(VulkanRenderer gd, int stride, int alignment)
  40. {
  41. _gd = gd;
  42. _stride = stride;
  43. _alignment = alignment;
  44. _buffer = null;
  45. }
  46. public bool KeyEqual(ICacheKey other)
  47. {
  48. return other is AlignedVertexBufferCacheKey entry &&
  49. entry._stride == _stride &&
  50. entry._alignment == _alignment;
  51. }
  52. public void SetBuffer(Auto<DisposableBuffer> buffer)
  53. {
  54. _buffer = buffer;
  55. }
  56. public void Dispose()
  57. {
  58. _gd.PipelineInternal.DirtyVertexBuffer(_buffer);
  59. }
  60. }
  61. struct TopologyConversionCacheKey : ICacheKey
  62. {
  63. private IndexBufferPattern _pattern;
  64. private int _indexSize;
  65. // Used to notify the pipeline that bindings have invalidated on dispose.
  66. private readonly VulkanRenderer _gd;
  67. private Auto<DisposableBuffer> _buffer;
  68. public TopologyConversionCacheKey(VulkanRenderer gd, IndexBufferPattern pattern, int indexSize)
  69. {
  70. _gd = gd;
  71. _pattern = pattern;
  72. _indexSize = indexSize;
  73. _buffer = null;
  74. }
  75. public bool KeyEqual(ICacheKey other)
  76. {
  77. return other is TopologyConversionCacheKey entry &&
  78. entry._pattern == _pattern &&
  79. entry._indexSize == _indexSize;
  80. }
  81. public void SetBuffer(Auto<DisposableBuffer> buffer)
  82. {
  83. _buffer = buffer;
  84. }
  85. public void Dispose()
  86. {
  87. _gd.PipelineInternal.DirtyIndexBuffer(_buffer);
  88. }
  89. }
  90. struct CacheByRange<T> where T : IDisposable
  91. {
  92. private struct Entry
  93. {
  94. public ICacheKey Key;
  95. public T Value;
  96. public Entry(ICacheKey key, T value)
  97. {
  98. Key = key;
  99. Value = value;
  100. }
  101. }
  102. private Dictionary<ulong, List<Entry>> _ranges;
  103. public void Add(int offset, int size, ICacheKey key, T value)
  104. {
  105. List<Entry> entries = GetEntries(offset, size);
  106. entries.Add(new Entry(key, value));
  107. }
  108. public bool TryGetValue(int offset, int size, ICacheKey key, out T value)
  109. {
  110. List<Entry> entries = GetEntries(offset, size);
  111. foreach (Entry entry in entries)
  112. {
  113. if (entry.Key.KeyEqual(key))
  114. {
  115. value = entry.Value;
  116. return true;
  117. }
  118. }
  119. value = default;
  120. return false;
  121. }
  122. public void Clear()
  123. {
  124. if (_ranges != null)
  125. {
  126. foreach (List<Entry> entries in _ranges.Values)
  127. {
  128. foreach (Entry entry in entries)
  129. {
  130. entry.Key.Dispose();
  131. entry.Value.Dispose();
  132. }
  133. }
  134. _ranges.Clear();
  135. _ranges = null;
  136. }
  137. }
  138. public void ClearRange(int offset, int size)
  139. {
  140. if (_ranges != null && _ranges.Count > 0)
  141. {
  142. int end = offset + size;
  143. List<ulong> toRemove = null;
  144. foreach (KeyValuePair<ulong, List<Entry>> range in _ranges)
  145. {
  146. (int rOffset, int rSize) = UnpackRange(range.Key);
  147. int rEnd = rOffset + rSize;
  148. if (rEnd > offset && rOffset < end)
  149. {
  150. List<Entry> entries = range.Value;
  151. foreach (Entry entry in entries)
  152. {
  153. entry.Key.Dispose();
  154. entry.Value.Dispose();
  155. }
  156. (toRemove ??= new List<ulong>()).Add(range.Key);
  157. }
  158. }
  159. if (toRemove != null)
  160. {
  161. foreach (ulong range in toRemove)
  162. {
  163. _ranges.Remove(range);
  164. }
  165. }
  166. }
  167. }
  168. private List<Entry> GetEntries(int offset, int size)
  169. {
  170. if (_ranges == null)
  171. {
  172. _ranges = new Dictionary<ulong, List<Entry>>();
  173. }
  174. ulong key = PackRange(offset, size);
  175. List<Entry> value;
  176. if (!_ranges.TryGetValue(key, out value))
  177. {
  178. value = new List<Entry>();
  179. _ranges.Add(key, value);
  180. }
  181. return value;
  182. }
  183. private static ulong PackRange(int offset, int size)
  184. {
  185. return (uint)offset | ((ulong)size << 32);
  186. }
  187. private static (int offset, int size) UnpackRange(ulong range)
  188. {
  189. return ((int)range, (int)(range >> 32));
  190. }
  191. public void Dispose()
  192. {
  193. Clear();
  194. }
  195. }
  196. }