BufferMirrorRangeList.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Graphics.Vulkan
  4. {
  5. /// <summary>
  6. /// A structure tracking pending upload ranges for buffers.
  7. /// Where a range is present, pending data exists that can either be used to build mirrors
  8. /// or upload directly to the buffer.
  9. /// </summary>
  10. struct BufferMirrorRangeList
  11. {
  12. internal readonly struct Range
  13. {
  14. public int Offset { get; }
  15. public int Size { get; }
  16. public int End => Offset + Size;
  17. public Range(int offset, int size)
  18. {
  19. Offset = offset;
  20. Size = size;
  21. }
  22. public bool OverlapsWith(int offset, int size)
  23. {
  24. return Offset < offset + size && offset < Offset + Size;
  25. }
  26. }
  27. private List<Range> _ranges;
  28. public readonly IEnumerable<Range> All()
  29. {
  30. return _ranges;
  31. }
  32. public readonly bool Remove(int offset, int size)
  33. {
  34. List<Range> list = _ranges;
  35. bool removedAny = false;
  36. if (list != null)
  37. {
  38. int overlapIndex = BinarySearch(list, offset, size);
  39. if (overlapIndex >= 0)
  40. {
  41. // Overlaps with a range. Search back to find the first one it doesn't overlap with.
  42. while (overlapIndex > 0 && list[overlapIndex - 1].OverlapsWith(offset, size))
  43. {
  44. overlapIndex--;
  45. }
  46. int endOffset = offset + size;
  47. int startIndex = overlapIndex;
  48. Range currentOverlap = list[overlapIndex];
  49. // Orphan the start of the overlap.
  50. if (currentOverlap.Offset < offset)
  51. {
  52. list[overlapIndex] = new Range(currentOverlap.Offset, offset - currentOverlap.Offset);
  53. currentOverlap = new Range(offset, currentOverlap.End - offset);
  54. list.Insert(++overlapIndex, currentOverlap);
  55. startIndex++;
  56. removedAny = true;
  57. }
  58. // Remove any middle overlaps.
  59. while (currentOverlap.Offset < endOffset)
  60. {
  61. if (currentOverlap.End > endOffset)
  62. {
  63. // Update the end overlap instead of removing it, if it spans beyond the removed range.
  64. list[overlapIndex] = new Range(endOffset, currentOverlap.End - endOffset);
  65. removedAny = true;
  66. break;
  67. }
  68. if (++overlapIndex >= list.Count)
  69. {
  70. break;
  71. }
  72. currentOverlap = list[overlapIndex];
  73. }
  74. int count = overlapIndex - startIndex;
  75. list.RemoveRange(startIndex, count);
  76. removedAny |= count > 0;
  77. }
  78. }
  79. return removedAny;
  80. }
  81. public void Add(int offset, int size)
  82. {
  83. List<Range> list = _ranges;
  84. if (list != null)
  85. {
  86. int overlapIndex = BinarySearch(list, offset, size);
  87. if (overlapIndex >= 0)
  88. {
  89. while (overlapIndex > 0 && list[overlapIndex - 1].OverlapsWith(offset, size))
  90. {
  91. overlapIndex--;
  92. }
  93. int endOffset = offset + size;
  94. int startIndex = overlapIndex;
  95. while (overlapIndex < list.Count && list[overlapIndex].OverlapsWith(offset, size))
  96. {
  97. Range currentOverlap = list[overlapIndex];
  98. int currentOverlapEndOffset = currentOverlap.Offset + currentOverlap.Size;
  99. if (offset > currentOverlap.Offset)
  100. {
  101. offset = currentOverlap.Offset;
  102. }
  103. if (endOffset < currentOverlapEndOffset)
  104. {
  105. endOffset = currentOverlapEndOffset;
  106. }
  107. overlapIndex++;
  108. size = endOffset - offset;
  109. }
  110. int count = overlapIndex - startIndex;
  111. list.RemoveRange(startIndex, count);
  112. overlapIndex = startIndex;
  113. }
  114. else
  115. {
  116. overlapIndex = ~overlapIndex;
  117. }
  118. list.Insert(overlapIndex, new Range(offset, size));
  119. }
  120. else
  121. {
  122. _ranges = [new(offset, size)];
  123. }
  124. }
  125. public readonly bool OverlapsWith(int offset, int size)
  126. {
  127. List<Range> list = _ranges;
  128. if (list == null)
  129. {
  130. return false;
  131. }
  132. return BinarySearch(list, offset, size) >= 0;
  133. }
  134. public readonly List<Range> FindOverlaps(int offset, int size)
  135. {
  136. List<Range> list = _ranges;
  137. if (list == null)
  138. {
  139. return null;
  140. }
  141. List<Range> result = null;
  142. int index = BinarySearch(list, offset, size);
  143. if (index >= 0)
  144. {
  145. while (index > 0 && list[index - 1].OverlapsWith(offset, size))
  146. {
  147. index--;
  148. }
  149. do
  150. {
  151. (result ??= []).Add(list[index++]);
  152. }
  153. while (index < list.Count && list[index].OverlapsWith(offset, size));
  154. }
  155. return result;
  156. }
  157. private static int BinarySearch(List<Range> list, int offset, int size)
  158. {
  159. int left = 0;
  160. int right = list.Count - 1;
  161. while (left <= right)
  162. {
  163. int range = right - left;
  164. int middle = left + (range >> 1);
  165. Range item = list[middle];
  166. if (item.OverlapsWith(offset, size))
  167. {
  168. return middle;
  169. }
  170. if (offset < item.Offset)
  171. {
  172. right = middle - 1;
  173. }
  174. else
  175. {
  176. left = middle + 1;
  177. }
  178. }
  179. return ~left;
  180. }
  181. public readonly void FillData(Span<byte> baseData, Span<byte> modData, int offset, Span<byte> result)
  182. {
  183. int size = baseData.Length;
  184. int endOffset = offset + size;
  185. List<Range> list = _ranges;
  186. if (list == null)
  187. {
  188. baseData.CopyTo(result);
  189. }
  190. int srcOffset = offset;
  191. int dstOffset = 0;
  192. bool activeRange = false;
  193. for (int i = 0; i < list.Count; i++)
  194. {
  195. Range range = list[i];
  196. int rangeEnd = range.Offset + range.Size;
  197. if (activeRange)
  198. {
  199. if (range.Offset >= endOffset)
  200. {
  201. break;
  202. }
  203. }
  204. else
  205. {
  206. if (rangeEnd <= offset)
  207. {
  208. continue;
  209. }
  210. activeRange = true;
  211. }
  212. int baseSize = range.Offset - srcOffset;
  213. if (baseSize > 0)
  214. {
  215. baseData.Slice(dstOffset, baseSize).CopyTo(result.Slice(dstOffset, baseSize));
  216. srcOffset += baseSize;
  217. dstOffset += baseSize;
  218. }
  219. int modSize = Math.Min(rangeEnd - srcOffset, endOffset - srcOffset);
  220. if (modSize != 0)
  221. {
  222. modData.Slice(dstOffset, modSize).CopyTo(result.Slice(dstOffset, modSize));
  223. srcOffset += modSize;
  224. dstOffset += modSize;
  225. }
  226. }
  227. int baseSizeEnd = endOffset - srcOffset;
  228. if (baseSizeEnd > 0)
  229. {
  230. baseData.Slice(dstOffset, baseSizeEnd).CopyTo(result.Slice(dstOffset, baseSizeEnd));
  231. }
  232. }
  233. public readonly int Count()
  234. {
  235. return _ranges?.Count ?? 0;
  236. }
  237. public void Clear()
  238. {
  239. _ranges = null;
  240. }
  241. }
  242. }