BufferModifiedRangeList.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using Ryujinx.Common.Pools;
  2. using Ryujinx.Memory.Range;
  3. using System;
  4. using System.Linq;
  5. namespace Ryujinx.Graphics.Gpu.Memory
  6. {
  7. /// <summary>
  8. /// A range within a buffer that has been modified by the GPU.
  9. /// </summary>
  10. class BufferModifiedRange : IRange
  11. {
  12. /// <summary>
  13. /// Start address of the range in guest memory.
  14. /// </summary>
  15. public ulong Address { get; }
  16. /// <summary>
  17. /// Size of the range in bytes.
  18. /// </summary>
  19. public ulong Size { get; }
  20. /// <summary>
  21. /// End address of the range in guest memory.
  22. /// </summary>
  23. public ulong EndAddress => Address + Size;
  24. /// <summary>
  25. /// The GPU sync number at the time of the last modification.
  26. /// </summary>
  27. public ulong SyncNumber { get; internal set; }
  28. /// <summary>
  29. /// Creates a new instance of a modified range.
  30. /// </summary>
  31. /// <param name="address">Start address of the range</param>
  32. /// <param name="size">Size of the range in bytes</param>
  33. /// <param name="syncNumber">The GPU sync number at the time of creation</param>
  34. public BufferModifiedRange(ulong address, ulong size, ulong syncNumber)
  35. {
  36. Address = address;
  37. Size = size;
  38. SyncNumber = syncNumber;
  39. }
  40. /// <summary>
  41. /// Checks if a given range overlaps with the modified range.
  42. /// </summary>
  43. /// <param name="address">Start address of the range</param>
  44. /// <param name="size">Size in bytes of the range</param>
  45. /// <returns>True if the range overlaps, false otherwise</returns>
  46. public bool OverlapsWith(ulong address, ulong size)
  47. {
  48. return Address < address + size && address < EndAddress;
  49. }
  50. }
  51. /// <summary>
  52. /// A structure used to track GPU modified ranges within a buffer.
  53. /// </summary>
  54. class BufferModifiedRangeList : RangeList<BufferModifiedRange>
  55. {
  56. private const int BackingInitialSize = 8;
  57. private GpuContext _context;
  58. private object _lock = new object();
  59. /// <summary>
  60. /// Creates a new instance of a modified range list.
  61. /// </summary>
  62. /// <param name="context">GPU context that the buffer range list belongs to</param>
  63. public BufferModifiedRangeList(GpuContext context) : base(BackingInitialSize)
  64. {
  65. _context = context;
  66. }
  67. /// <summary>
  68. /// Given an input range, calls the given action with sub-ranges which exclude any of the modified regions.
  69. /// </summary>
  70. /// <param name="address">Start address of the query range</param>
  71. /// <param name="size">Size of the query range in bytes</param>
  72. /// <param name="action">Action to perform for each remaining sub-range of the input range</param>
  73. public void ExcludeModifiedRegions(ulong address, ulong size, Action<ulong, ulong> action)
  74. {
  75. lock (_lock)
  76. {
  77. // Slices a given region using the modified regions in the list. Calls the action for the new slices.
  78. ref var overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
  79. int count = FindOverlapsNonOverlapping(address, size, ref overlaps);
  80. for (int i = 0; i < count; i++)
  81. {
  82. BufferModifiedRange overlap = overlaps[i];
  83. if (overlap.Address > address)
  84. {
  85. // The start of the remaining region is uncovered by this overlap. Call the action for it.
  86. action(address, overlap.Address - address);
  87. }
  88. // Remaining region is after this overlap.
  89. size -= overlap.EndAddress - address;
  90. address = overlap.EndAddress;
  91. }
  92. if ((long)size > 0)
  93. {
  94. // If there is any region left after removing the overlaps, signal it.
  95. action(address, size);
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// Signal that a region of the buffer has been modified, and add the new region to the range list.
  101. /// Any overlapping ranges will be (partially) removed.
  102. /// </summary>
  103. /// <param name="address">Start address of the modified region</param>
  104. /// <param name="size">Size of the modified region in bytes</param>
  105. public void SignalModified(ulong address, ulong size)
  106. {
  107. // Must lock, as this can affect flushes from the background thread.
  108. lock (_lock)
  109. {
  110. // We may overlap with some existing modified regions. They must be cut into by the new entry.
  111. ref var overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
  112. int count = FindOverlapsNonOverlapping(address, size, ref overlaps);
  113. ulong endAddress = address + size;
  114. ulong syncNumber = _context.SyncNumber;
  115. for (int i = 0; i < count; i++)
  116. {
  117. // The overlaps must be removed or split.
  118. BufferModifiedRange overlap = overlaps[i];
  119. if (overlap.Address == address && overlap.Size == size)
  120. {
  121. // Region already exists. Just update the existing sync number.
  122. overlap.SyncNumber = syncNumber;
  123. return;
  124. }
  125. Remove(overlap);
  126. if (overlap.Address < address && overlap.EndAddress > address)
  127. {
  128. // A split item must be created behind this overlap.
  129. Add(new BufferModifiedRange(overlap.Address, address - overlap.Address, overlap.SyncNumber));
  130. }
  131. if (overlap.Address < endAddress && overlap.EndAddress > endAddress)
  132. {
  133. // A split item must be created after this overlap.
  134. Add(new BufferModifiedRange(endAddress, overlap.EndAddress - endAddress, overlap.SyncNumber));
  135. }
  136. }
  137. Add(new BufferModifiedRange(address, size, syncNumber));
  138. }
  139. }
  140. /// <summary>
  141. /// Gets modified ranges within the specified region, and then fires the given action for each range individually.
  142. /// </summary>
  143. /// <param name="address">Start address to query</param>
  144. /// <param name="size">Size to query</param>
  145. /// <param name="rangeAction">The action to call for each modified range</param>
  146. public void GetRanges(ulong address, ulong size, Action<ulong, ulong> rangeAction)
  147. {
  148. int count = 0;
  149. ref var overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
  150. // Range list must be consistent for this operation.
  151. lock (_lock)
  152. {
  153. count = FindOverlapsNonOverlapping(address, size, ref overlaps);
  154. }
  155. for (int i = 0; i < count; i++)
  156. {
  157. BufferModifiedRange overlap = overlaps[i];
  158. rangeAction(overlap.Address, overlap.Size);
  159. }
  160. }
  161. /// <summary>
  162. /// Queries if a range exists within the specified region.
  163. /// </summary>
  164. /// <param name="address">Start address to query</param>
  165. /// <param name="size">Size to query</param>
  166. /// <returns>True if a range exists in the specified region, false otherwise</returns>
  167. public bool HasRange(ulong address, ulong size)
  168. {
  169. // Range list must be consistent for this operation.
  170. lock (_lock)
  171. {
  172. return FindOverlapsNonOverlapping(address, size, ref ThreadStaticArray<BufferModifiedRange>.Get()) > 0;
  173. }
  174. }
  175. /// <summary>
  176. /// Gets modified ranges within the specified region, waits on ones from a previous sync number,
  177. /// and then fires the given action for each range individually.
  178. /// </summary>
  179. /// <remarks>
  180. /// This function assumes it is called from the background thread.
  181. /// Modifications from the current sync number are ignored because the guest should not expect them to be available yet.
  182. /// They will remain reserved, so that any data sync prioritizes the data in the GPU.
  183. /// </remarks>
  184. /// <param name="address">Start address to query</param>
  185. /// <param name="size">Size to query</param>
  186. /// <param name="rangeAction">The action to call for each modified range</param>
  187. public void WaitForAndGetRanges(ulong address, ulong size, Action<ulong, ulong> rangeAction)
  188. {
  189. ulong endAddress = address + size;
  190. ulong currentSync = _context.SyncNumber;
  191. int rangeCount = 0;
  192. ref var overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
  193. // Range list must be consistent for this operation
  194. lock (_lock)
  195. {
  196. rangeCount = FindOverlapsNonOverlapping(address, size, ref overlaps);
  197. }
  198. if (rangeCount == 0)
  199. {
  200. return;
  201. }
  202. // First, determine which syncpoint to wait on.
  203. // This is the latest syncpoint that is not equal to the current sync.
  204. long highestDiff = long.MinValue;
  205. for (int i = 0; i < rangeCount; i++)
  206. {
  207. BufferModifiedRange overlap = overlaps[i];
  208. long diff = (long)(overlap.SyncNumber - currentSync);
  209. if (diff < 0 && diff > highestDiff)
  210. {
  211. highestDiff = diff;
  212. }
  213. }
  214. if (highestDiff == long.MinValue)
  215. {
  216. return;
  217. }
  218. // Wait for the syncpoint.
  219. _context.Renderer.WaitSync(currentSync + (ulong)highestDiff);
  220. // Flush and remove all regions with the older syncpoint.
  221. lock (_lock)
  222. {
  223. for (int i = 0; i < rangeCount; i++)
  224. {
  225. BufferModifiedRange overlap = overlaps[i];
  226. long diff = (long)(overlap.SyncNumber - currentSync);
  227. if (diff <= highestDiff)
  228. {
  229. ulong clampAddress = Math.Max(address, overlap.Address);
  230. ulong clampEnd = Math.Min(endAddress, overlap.EndAddress);
  231. ClearPart(overlap, clampAddress, clampEnd);
  232. rangeAction(clampAddress, clampEnd - clampAddress);
  233. }
  234. }
  235. }
  236. }
  237. /// <summary>
  238. /// Inherit ranges from another modified range list.
  239. /// </summary>
  240. /// <param name="ranges">The range list to inherit from</param>
  241. /// <param name="rangeAction">The action to call for each modified range</param>
  242. public void InheritRanges(BufferModifiedRangeList ranges, Action<ulong, ulong> rangeAction)
  243. {
  244. BufferModifiedRange[] inheritRanges;
  245. lock (ranges._lock)
  246. {
  247. inheritRanges = ranges.ToArray();
  248. }
  249. lock (_lock)
  250. {
  251. foreach (BufferModifiedRange range in inheritRanges)
  252. {
  253. Add(range);
  254. }
  255. }
  256. ulong currentSync = _context.SyncNumber;
  257. foreach (BufferModifiedRange range in inheritRanges)
  258. {
  259. if (range.SyncNumber != currentSync)
  260. {
  261. rangeAction(range.Address, range.Size);
  262. }
  263. }
  264. }
  265. /// <summary>
  266. /// Calls the given action for modified ranges that aren't from the current sync number.
  267. /// </summary>
  268. /// <param name="rangeAction">The action to call for each modified range</param>
  269. public void ReregisterRanges(Action<ulong, ulong> rangeAction)
  270. {
  271. ref var ranges = ref ThreadStaticArray<BufferModifiedRange>.Get();
  272. // Range list must be consistent for this operation.
  273. lock (_lock)
  274. {
  275. if (ranges.Length < Count)
  276. {
  277. Array.Resize(ref ranges, Count);
  278. }
  279. int i = 0;
  280. foreach (BufferModifiedRange range in this)
  281. {
  282. ranges[i++] = range;
  283. }
  284. }
  285. ulong currentSync = _context.SyncNumber;
  286. for (int i = 0; i < Count; i++)
  287. {
  288. BufferModifiedRange range = ranges[i];
  289. if (range.SyncNumber != currentSync)
  290. {
  291. rangeAction(range.Address, range.Size);
  292. }
  293. }
  294. }
  295. private void ClearPart(BufferModifiedRange overlap, ulong address, ulong endAddress)
  296. {
  297. Remove(overlap);
  298. // If the overlap extends outside of the clear range, make sure those parts still exist.
  299. if (overlap.Address < address)
  300. {
  301. Add(new BufferModifiedRange(overlap.Address, address - overlap.Address, overlap.SyncNumber));
  302. }
  303. if (overlap.EndAddress > endAddress)
  304. {
  305. Add(new BufferModifiedRange(endAddress, overlap.EndAddress - endAddress, overlap.SyncNumber));
  306. }
  307. }
  308. /// <summary>
  309. /// Clear modified ranges within the specified area.
  310. /// </summary>
  311. /// <param name="address">Start address to clear</param>
  312. /// <param name="size">Size to clear</param>
  313. public void Clear(ulong address, ulong size)
  314. {
  315. lock (_lock)
  316. {
  317. // This function can be called from any thread, so it cannot use the arrays for background or foreground.
  318. BufferModifiedRange[] toClear = new BufferModifiedRange[1];
  319. int rangeCount = FindOverlapsNonOverlapping(address, size, ref toClear);
  320. ulong endAddress = address + size;
  321. for (int i = 0; i < rangeCount; i++)
  322. {
  323. BufferModifiedRange overlap = toClear[i];
  324. ClearPart(overlap, address, endAddress);
  325. }
  326. }
  327. }
  328. /// <summary>
  329. /// Clear all modified ranges.
  330. /// </summary>
  331. public void Clear()
  332. {
  333. lock (_lock)
  334. {
  335. Count = 0;
  336. }
  337. }
  338. }
  339. }