BufferModifiedRangeList.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 GpuContext _context;
  57. private object _lock = new object();
  58. /// <summary>
  59. /// Creates a new instance of a modified range list.
  60. /// </summary>
  61. /// <param name="context">GPU context that the buffer range list belongs to</param>
  62. public BufferModifiedRangeList(GpuContext context)
  63. {
  64. _context = context;
  65. }
  66. /// <summary>
  67. /// Given an input range, calls the given action with sub-ranges which exclude any of the modified regions.
  68. /// </summary>
  69. /// <param name="address">Start address of the query range</param>
  70. /// <param name="size">Size of the query range in bytes</param>
  71. /// <param name="action">Action to perform for each remaining sub-range of the input range</param>
  72. public void ExcludeModifiedRegions(ulong address, ulong size, Action<ulong, ulong> action)
  73. {
  74. lock (_lock)
  75. {
  76. // Slices a given region using the modified regions in the list. Calls the action for the new slices.
  77. ref var overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
  78. int count = FindOverlapsNonOverlapping(address, size, ref overlaps);
  79. for (int i = 0; i < count; i++)
  80. {
  81. BufferModifiedRange overlap = overlaps[i];
  82. if (overlap.Address > address)
  83. {
  84. // The start of the remaining region is uncovered by this overlap. Call the action for it.
  85. action(address, overlap.Address - address);
  86. }
  87. // Remaining region is after this overlap.
  88. size -= overlap.EndAddress - address;
  89. address = overlap.EndAddress;
  90. }
  91. if ((long)size > 0)
  92. {
  93. // If there is any region left after removing the overlaps, signal it.
  94. action(address, size);
  95. }
  96. }
  97. }
  98. /// <summary>
  99. /// Signal that a region of the buffer has been modified, and add the new region to the range list.
  100. /// Any overlapping ranges will be (partially) removed.
  101. /// </summary>
  102. /// <param name="address">Start address of the modified region</param>
  103. /// <param name="size">Size of the modified region in bytes</param>
  104. public void SignalModified(ulong address, ulong size)
  105. {
  106. // Must lock, as this can affect flushes from the background thread.
  107. lock (_lock)
  108. {
  109. // We may overlap with some existing modified regions. They must be cut into by the new entry.
  110. ref var overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
  111. int count = FindOverlapsNonOverlapping(address, size, ref overlaps);
  112. ulong endAddress = address + size;
  113. ulong syncNumber = _context.SyncNumber;
  114. for (int i = 0; i < count; i++)
  115. {
  116. // The overlaps must be removed or split.
  117. BufferModifiedRange overlap = overlaps[i];
  118. if (overlap.Address == address && overlap.Size == size)
  119. {
  120. // Region already exists. Just update the existing sync number.
  121. overlap.SyncNumber = syncNumber;
  122. return;
  123. }
  124. Remove(overlap);
  125. if (overlap.Address < address && overlap.EndAddress > address)
  126. {
  127. // A split item must be created behind this overlap.
  128. Add(new BufferModifiedRange(overlap.Address, address - overlap.Address, overlap.SyncNumber));
  129. }
  130. if (overlap.Address < endAddress && overlap.EndAddress > endAddress)
  131. {
  132. // A split item must be created after this overlap.
  133. Add(new BufferModifiedRange(endAddress, overlap.EndAddress - endAddress, overlap.SyncNumber));
  134. }
  135. }
  136. Add(new BufferModifiedRange(address, size, syncNumber));
  137. }
  138. }
  139. /// <summary>
  140. /// Gets modified ranges within the specified region, and then fires the given action for each range individually.
  141. /// </summary>
  142. /// <param name="address">Start address to query</param>
  143. /// <param name="size">Size to query</param>
  144. /// <param name="rangeAction">The action to call for each modified range</param>
  145. public void GetRanges(ulong address, ulong size, Action<ulong, ulong> rangeAction)
  146. {
  147. int count = 0;
  148. ref var overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
  149. // Range list must be consistent for this operation.
  150. lock (_lock)
  151. {
  152. count = FindOverlapsNonOverlapping(address, size, ref overlaps);
  153. }
  154. for (int i = 0; i < count; i++)
  155. {
  156. BufferModifiedRange overlap = overlaps[i];
  157. rangeAction(overlap.Address, overlap.Size);
  158. }
  159. }
  160. /// <summary>
  161. /// Queries if a range exists within the specified region.
  162. /// </summary>
  163. /// <param name="address">Start address to query</param>
  164. /// <param name="size">Size to query</param>
  165. /// <returns>True if a range exists in the specified region, false otherwise</returns>
  166. public bool HasRange(ulong address, ulong size)
  167. {
  168. // Range list must be consistent for this operation.
  169. lock (_lock)
  170. {
  171. return FindOverlapsNonOverlapping(address, size, ref ThreadStaticArray<BufferModifiedRange>.Get()) > 0;
  172. }
  173. }
  174. /// <summary>
  175. /// Gets modified ranges within the specified region, waits on ones from a previous sync number,
  176. /// and then fires the given action for each range individually.
  177. /// </summary>
  178. /// <remarks>
  179. /// This function assumes it is called from the background thread.
  180. /// Modifications from the current sync number are ignored because the guest should not expect them to be available yet.
  181. /// They will remain reserved, so that any data sync prioritizes the data in the GPU.
  182. /// </remarks>
  183. /// <param name="address">Start address to query</param>
  184. /// <param name="size">Size to query</param>
  185. /// <param name="rangeAction">The action to call for each modified range</param>
  186. public void WaitForAndGetRanges(ulong address, ulong size, Action<ulong, ulong> rangeAction)
  187. {
  188. ulong endAddress = address + size;
  189. ulong currentSync = _context.SyncNumber;
  190. int rangeCount = 0;
  191. ref var overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
  192. // Range list must be consistent for this operation
  193. lock (_lock)
  194. {
  195. rangeCount = FindOverlapsNonOverlapping(address, size, ref overlaps);
  196. }
  197. if (rangeCount == 0)
  198. {
  199. return;
  200. }
  201. // First, determine which syncpoint to wait on.
  202. // This is the latest syncpoint that is not equal to the current sync.
  203. long highestDiff = long.MinValue;
  204. for (int i = 0; i < rangeCount; i++)
  205. {
  206. BufferModifiedRange overlap = overlaps[i];
  207. long diff = (long)(overlap.SyncNumber - currentSync);
  208. if (diff < 0 && diff > highestDiff)
  209. {
  210. highestDiff = diff;
  211. }
  212. }
  213. if (highestDiff == long.MinValue)
  214. {
  215. return;
  216. }
  217. // Wait for the syncpoint.
  218. _context.Renderer.WaitSync(currentSync + (ulong)highestDiff);
  219. // Flush and remove all regions with the older syncpoint.
  220. lock (_lock)
  221. {
  222. for (int i = 0; i < rangeCount; i++)
  223. {
  224. BufferModifiedRange overlap = overlaps[i];
  225. long diff = (long)(overlap.SyncNumber - currentSync);
  226. if (diff <= highestDiff)
  227. {
  228. ulong clampAddress = Math.Max(address, overlap.Address);
  229. ulong clampEnd = Math.Min(endAddress, overlap.EndAddress);
  230. ClearPart(overlap, clampAddress, clampEnd);
  231. rangeAction(clampAddress, clampEnd - clampAddress);
  232. }
  233. }
  234. }
  235. }
  236. /// <summary>
  237. /// Inherit ranges from another modified range list.
  238. /// </summary>
  239. /// <param name="ranges">The range list to inherit from</param>
  240. /// <param name="rangeAction">The action to call for each modified range</param>
  241. public void InheritRanges(BufferModifiedRangeList ranges, Action<ulong, ulong> rangeAction)
  242. {
  243. BufferModifiedRange[] inheritRanges;
  244. lock (ranges._lock)
  245. {
  246. inheritRanges = ranges.ToArray();
  247. }
  248. lock (_lock)
  249. {
  250. foreach (BufferModifiedRange range in inheritRanges)
  251. {
  252. Add(range);
  253. }
  254. }
  255. ulong currentSync = _context.SyncNumber;
  256. foreach (BufferModifiedRange range in inheritRanges)
  257. {
  258. if (range.SyncNumber != currentSync)
  259. {
  260. rangeAction(range.Address, range.Size);
  261. }
  262. }
  263. }
  264. private void ClearPart(BufferModifiedRange overlap, ulong address, ulong endAddress)
  265. {
  266. Remove(overlap);
  267. // If the overlap extends outside of the clear range, make sure those parts still exist.
  268. if (overlap.Address < address)
  269. {
  270. Add(new BufferModifiedRange(overlap.Address, address - overlap.Address, overlap.SyncNumber));
  271. }
  272. if (overlap.EndAddress > endAddress)
  273. {
  274. Add(new BufferModifiedRange(endAddress, overlap.EndAddress - endAddress, overlap.SyncNumber));
  275. }
  276. }
  277. /// <summary>
  278. /// Clear modified ranges within the specified area.
  279. /// </summary>
  280. /// <param name="address">Start address to clear</param>
  281. /// <param name="size">Size to clear</param>
  282. public void Clear(ulong address, ulong size)
  283. {
  284. lock (_lock)
  285. {
  286. // This function can be called from any thread, so it cannot use the arrays for background or foreground.
  287. BufferModifiedRange[] toClear = new BufferModifiedRange[1];
  288. int rangeCount = FindOverlapsNonOverlapping(address, size, ref toClear);
  289. ulong endAddress = address + size;
  290. for (int i = 0; i < rangeCount; i++)
  291. {
  292. BufferModifiedRange overlap = toClear[i];
  293. ClearPart(overlap, address, endAddress);
  294. }
  295. }
  296. }
  297. /// <summary>
  298. /// Clear all modified ranges.
  299. /// </summary>
  300. public void Clear()
  301. {
  302. lock (_lock)
  303. {
  304. Items.Clear();
  305. }
  306. }
  307. }
  308. }