CacheByRange.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. public I8ToI16CacheKey() { }
  12. public bool KeyEqual(ICacheKey other)
  13. {
  14. return other is I8ToI16CacheKey;
  15. }
  16. public void Dispose() { }
  17. }
  18. struct AlignedVertexBufferCacheKey : ICacheKey
  19. {
  20. private readonly int _stride;
  21. private readonly int _alignment;
  22. // Used to notify the pipeline that bindings have invalidated on dispose.
  23. private readonly VulkanRenderer _gd;
  24. private Auto<DisposableBuffer> _buffer;
  25. public AlignedVertexBufferCacheKey(VulkanRenderer gd, int stride, int alignment)
  26. {
  27. _gd = gd;
  28. _stride = stride;
  29. _alignment = alignment;
  30. _buffer = null;
  31. }
  32. public bool KeyEqual(ICacheKey other)
  33. {
  34. return other is AlignedVertexBufferCacheKey entry &&
  35. entry._stride == _stride &&
  36. entry._alignment == _alignment;
  37. }
  38. public void SetBuffer(Auto<DisposableBuffer> buffer)
  39. {
  40. _buffer = buffer;
  41. }
  42. public void Dispose()
  43. {
  44. _gd.PipelineInternal.DirtyVertexBuffer(_buffer);
  45. }
  46. }
  47. struct CacheByRange<T> where T : IDisposable
  48. {
  49. private struct Entry
  50. {
  51. public ICacheKey Key;
  52. public T Value;
  53. public Entry(ICacheKey key, T value)
  54. {
  55. Key = key;
  56. Value = value;
  57. }
  58. }
  59. private Dictionary<ulong, List<Entry>> _ranges;
  60. public void Add(int offset, int size, ICacheKey key, T value)
  61. {
  62. List<Entry> entries = GetEntries(offset, size);
  63. entries.Add(new Entry(key, value));
  64. }
  65. public bool TryGetValue(int offset, int size, ICacheKey key, out T value)
  66. {
  67. List<Entry> entries = GetEntries(offset, size);
  68. foreach (Entry entry in entries)
  69. {
  70. if (entry.Key.KeyEqual(key))
  71. {
  72. value = entry.Value;
  73. return true;
  74. }
  75. }
  76. value = default;
  77. return false;
  78. }
  79. public void Clear()
  80. {
  81. if (_ranges != null)
  82. {
  83. foreach (List<Entry> entries in _ranges.Values)
  84. {
  85. foreach (Entry entry in entries)
  86. {
  87. entry.Key.Dispose();
  88. entry.Value.Dispose();
  89. }
  90. }
  91. _ranges.Clear();
  92. _ranges = null;
  93. }
  94. }
  95. private List<Entry> GetEntries(int offset, int size)
  96. {
  97. if (_ranges == null)
  98. {
  99. _ranges = new Dictionary<ulong, List<Entry>>();
  100. }
  101. ulong key = PackRange(offset, size);
  102. List<Entry> value;
  103. if (!_ranges.TryGetValue(key, out value))
  104. {
  105. value = new List<Entry>();
  106. _ranges.Add(key, value);
  107. }
  108. return value;
  109. }
  110. private static ulong PackRange(int offset, int size)
  111. {
  112. return (uint)offset | ((ulong)size << 32);
  113. }
  114. public void Dispose()
  115. {
  116. Clear();
  117. }
  118. }
  119. }