BufferModifiedRangeList.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using Ryujinx.Memory.Range;
  2. using System;
  3. using System.Linq;
  4. namespace Ryujinx.Graphics.Gpu.Memory
  5. {
  6. /// <summary>
  7. /// A range within a buffer that has been modified by the GPU.
  8. /// </summary>
  9. class BufferModifiedRange : IRange
  10. {
  11. /// <summary>
  12. /// Start address of the range in guest memory.
  13. /// </summary>
  14. public ulong Address { get; }
  15. /// <summary>
  16. /// Size of the range in bytes.
  17. /// </summary>
  18. public ulong Size { get; }
  19. /// <summary>
  20. /// End address of the range in guest memory.
  21. /// </summary>
  22. public ulong EndAddress => Address + Size;
  23. /// <summary>
  24. /// The GPU sync number at the time of the last modification.
  25. /// </summary>
  26. public ulong SyncNumber { get; internal set; }
  27. /// <summary>
  28. /// Creates a new instance of a modified range.
  29. /// </summary>
  30. /// <param name="address">Start address of the range</param>
  31. /// <param name="size">Size of the range in bytes</param>
  32. /// <param name="syncNumber">The GPU sync number at the time of creation</param>
  33. public BufferModifiedRange(ulong address, ulong size, ulong syncNumber)
  34. {
  35. Address = address;
  36. Size = size;
  37. SyncNumber = syncNumber;
  38. }
  39. /// <summary>
  40. /// Checks if a given range overlaps with the modified range.
  41. /// </summary>
  42. /// <param name="address">Start address of the range</param>
  43. /// <param name="size">Size in bytes of the range</param>
  44. /// <returns>True if the range overlaps, false otherwise</returns>
  45. public bool OverlapsWith(ulong address, ulong size)
  46. {
  47. return Address < address + size && address < EndAddress;
  48. }
  49. }
  50. /// <summary>
  51. /// A structure used to track GPU modified ranges within a buffer.
  52. /// </summary>
  53. class BufferModifiedRangeList : RangeList<BufferModifiedRange>
  54. {
  55. private GpuContext _context;
  56. private object _lock = new object();
  57. // The list can be accessed from both the GPU thread, and a background thread.
  58. private BufferModifiedRange[] _foregroundOverlaps = new BufferModifiedRange[1];
  59. private BufferModifiedRange[] _backgroundOverlaps = new BufferModifiedRange[1];
  60. /// <summary>
  61. /// Creates a new instance of a modified range list.
  62. /// </summary>
  63. /// <param name="context">GPU context that the buffer range list belongs to</param>
  64. public BufferModifiedRangeList(GpuContext context)
  65. {
  66. _context = context;
  67. }
  68. /// <summary>
  69. /// Given an input range, calls the given action with sub-ranges which exclude any of the modified regions.
  70. /// </summary>
  71. /// <param name="address">Start address of the query range</param>
  72. /// <param name="size">Size of the query range in bytes</param>
  73. /// <param name="action">Action to perform for each remaining sub-range of the input range</param>
  74. public void ExcludeModifiedRegions(ulong address, ulong size, Action<ulong, ulong> action)
  75. {
  76. lock (_lock)
  77. {
  78. // Slices a given region using the modified regions in the list. Calls the action for the new slices.
  79. int count = FindOverlapsNonOverlapping(address, size, ref _foregroundOverlaps);
  80. for (int i = 0; i < count; i++)
  81. {
  82. BufferModifiedRange overlap = _foregroundOverlaps[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. int count = FindOverlapsNonOverlapping(address, size, ref _foregroundOverlaps);
  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 = _foregroundOverlaps[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. // Range list must be consistent for this operation.
  149. lock (_lock)
  150. {
  151. count = FindOverlapsNonOverlapping(address, size, ref _foregroundOverlaps);
  152. }
  153. for (int i = 0; i < count; i++)
  154. {
  155. BufferModifiedRange overlap = _foregroundOverlaps[i];
  156. rangeAction(overlap.Address, overlap.Size);
  157. }
  158. }
  159. /// <summary>
  160. /// Queries if a range exists within the specified region.
  161. /// </summary>
  162. /// <param name="address">Start address to query</param>
  163. /// <param name="size">Size to query</param>
  164. /// <returns>True if a range exists in the specified region, false otherwise</returns>
  165. public bool HasRange(ulong address, ulong size)
  166. {
  167. // Range list must be consistent for this operation.
  168. lock (_lock)
  169. {
  170. return FindOverlapsNonOverlapping(address, size, ref _foregroundOverlaps) > 0;
  171. }
  172. }
  173. /// <summary>
  174. /// Gets modified ranges within the specified region, waits on ones from a previous sync number,
  175. /// and then fires the given action for each range individually.
  176. /// </summary>
  177. /// <remarks>
  178. /// This function assumes it is called from the background thread.
  179. /// Modifications from the current sync number are ignored because the guest should not expect them to be available yet.
  180. /// They will remain reserved, so that any data sync prioritizes the data in the GPU.
  181. /// </remarks>
  182. /// <param name="address">Start address to query</param>
  183. /// <param name="size">Size to query</param>
  184. /// <param name="rangeAction">The action to call for each modified range</param>
  185. public void WaitForAndGetRanges(ulong address, ulong size, Action<ulong, ulong> rangeAction)
  186. {
  187. ulong endAddress = address + size;
  188. ulong currentSync = _context.SyncNumber;
  189. int rangeCount = 0;
  190. // Range list must be consistent for this operation
  191. lock (_lock)
  192. {
  193. rangeCount = FindOverlapsNonOverlapping(address, size, ref _backgroundOverlaps);
  194. }
  195. if (rangeCount == 0)
  196. {
  197. return;
  198. }
  199. // First, determine which syncpoint to wait on.
  200. // This is the latest syncpoint that is not equal to the current sync.
  201. long highestDiff = long.MinValue;
  202. for (int i = 0; i < rangeCount; i++)
  203. {
  204. BufferModifiedRange overlap = _backgroundOverlaps[i];
  205. long diff = (long)(overlap.SyncNumber - currentSync);
  206. if (diff < 0 && diff > highestDiff)
  207. {
  208. highestDiff = diff;
  209. }
  210. }
  211. if (highestDiff == long.MinValue)
  212. {
  213. return;
  214. }
  215. // Wait for the syncpoint.
  216. _context.Renderer.WaitSync(currentSync + (ulong)highestDiff);
  217. // Flush and remove all regions with the older syncpoint.
  218. lock (_lock)
  219. {
  220. for (int i = 0; i < rangeCount; i++)
  221. {
  222. BufferModifiedRange overlap = _backgroundOverlaps[i];
  223. long diff = (long)(overlap.SyncNumber - currentSync);
  224. if (diff <= highestDiff)
  225. {
  226. ulong clampAddress = Math.Max(address, overlap.Address);
  227. ulong clampEnd = Math.Min(endAddress, overlap.EndAddress);
  228. ClearPart(overlap, clampAddress, clampEnd);
  229. rangeAction(clampAddress, clampEnd - clampAddress);
  230. }
  231. }
  232. }
  233. }
  234. /// <summary>
  235. /// Inherit ranges from another modified range list.
  236. /// </summary>
  237. /// <param name="ranges">The range list to inherit from</param>
  238. /// <param name="rangeAction">The action to call for each modified range</param>
  239. public void InheritRanges(BufferModifiedRangeList ranges, Action<ulong, ulong> rangeAction)
  240. {
  241. BufferModifiedRange[] inheritRanges;
  242. lock (ranges._lock)
  243. {
  244. inheritRanges = ranges.ToArray();
  245. }
  246. lock (_lock)
  247. {
  248. foreach (BufferModifiedRange range in inheritRanges)
  249. {
  250. Add(range);
  251. }
  252. }
  253. ulong currentSync = _context.SyncNumber;
  254. foreach (BufferModifiedRange range in inheritRanges)
  255. {
  256. if (range.SyncNumber != currentSync)
  257. {
  258. rangeAction(range.Address, range.Size);
  259. }
  260. }
  261. }
  262. private void ClearPart(BufferModifiedRange overlap, ulong address, ulong endAddress)
  263. {
  264. Remove(overlap);
  265. // If the overlap extends outside of the clear range, make sure those parts still exist.
  266. if (overlap.Address < address)
  267. {
  268. Add(new BufferModifiedRange(overlap.Address, address - overlap.Address, overlap.SyncNumber));
  269. }
  270. if (overlap.EndAddress > endAddress)
  271. {
  272. Add(new BufferModifiedRange(endAddress, overlap.EndAddress - endAddress, overlap.SyncNumber));
  273. }
  274. }
  275. /// <summary>
  276. /// Clear modified ranges within the specified area.
  277. /// </summary>
  278. /// <param name="address">Start address to clear</param>
  279. /// <param name="size">Size to clear</param>
  280. public void Clear(ulong address, ulong size)
  281. {
  282. lock (_lock)
  283. {
  284. // This function can be called from any thread, so it cannot use the arrays for background or foreground.
  285. BufferModifiedRange[] toClear = new BufferModifiedRange[1];
  286. int rangeCount = FindOverlapsNonOverlapping(address, size, ref toClear);
  287. ulong endAddress = address + size;
  288. for (int i = 0; i < rangeCount; i++)
  289. {
  290. BufferModifiedRange overlap = toClear[i];
  291. ClearPart(overlap, address, endAddress);
  292. }
  293. }
  294. }
  295. /// <summary>
  296. /// Clear all modified ranges.
  297. /// </summary>
  298. public void Clear()
  299. {
  300. lock (_lock)
  301. {
  302. Items.Clear();
  303. }
  304. }
  305. }
  306. }