BufferCache.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Memory.Range;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. namespace Ryujinx.Graphics.Gpu.Memory
  7. {
  8. /// <summary>
  9. /// Buffer cache.
  10. /// </summary>
  11. class BufferCache : IDisposable
  12. {
  13. private const int OverlapsBufferInitialCapacity = 10;
  14. private const int OverlapsBufferMaxCapacity = 10000;
  15. private const ulong BufferAlignmentSize = 0x1000;
  16. private const ulong BufferAlignmentMask = BufferAlignmentSize - 1;
  17. private const ulong MaxDynamicGrowthSize = 0x100000;
  18. private readonly GpuContext _context;
  19. private readonly PhysicalMemory _physicalMemory;
  20. /// <remarks>
  21. /// Only modified from the GPU thread. Must lock for add/remove.
  22. /// Must lock for any access from other threads.
  23. /// </remarks>
  24. private readonly RangeList<Buffer> _buffers;
  25. private Buffer[] _bufferOverlaps;
  26. private readonly Dictionary<ulong, BufferCacheEntry> _dirtyCache;
  27. private readonly Dictionary<ulong, BufferCacheEntry> _modifiedCache;
  28. private bool _pruneCaches;
  29. public event Action NotifyBuffersModified;
  30. /// <summary>
  31. /// Creates a new instance of the buffer manager.
  32. /// </summary>
  33. /// <param name="context">The GPU context that the buffer manager belongs to</param>
  34. /// <param name="physicalMemory">Physical memory where the cached buffers are mapped</param>
  35. public BufferCache(GpuContext context, PhysicalMemory physicalMemory)
  36. {
  37. _context = context;
  38. _physicalMemory = physicalMemory;
  39. _buffers = new RangeList<Buffer>();
  40. _bufferOverlaps = new Buffer[OverlapsBufferInitialCapacity];
  41. _dirtyCache = new Dictionary<ulong, BufferCacheEntry>();
  42. // There are a lot more entries on the modified cache, so it is separate from the one for ForceDirty.
  43. _modifiedCache = new Dictionary<ulong, BufferCacheEntry>();
  44. }
  45. /// <summary>
  46. /// Handles removal of buffers written to a memory region being unmapped.
  47. /// </summary>
  48. /// <param name="sender">Sender object</param>
  49. /// <param name="e">Event arguments</param>
  50. public void MemoryUnmappedHandler(object sender, UnmapEventArgs e)
  51. {
  52. Buffer[] overlaps = new Buffer[10];
  53. int overlapCount;
  54. ulong address = ((MemoryManager)sender).Translate(e.Address);
  55. ulong size = e.Size;
  56. lock (_buffers)
  57. {
  58. overlapCount = _buffers.FindOverlaps(address, size, ref overlaps);
  59. }
  60. for (int i = 0; i < overlapCount; i++)
  61. {
  62. overlaps[i].Unmapped(address, size);
  63. }
  64. }
  65. /// <summary>
  66. /// Performs address translation of the GPU virtual address, and creates a
  67. /// new buffer, if needed, for the specified range.
  68. /// </summary>
  69. /// <param name="memoryManager">GPU memory manager where the buffer is mapped</param>
  70. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  71. /// <param name="size">Size in bytes of the buffer</param>
  72. /// <returns>CPU virtual address of the buffer, after address translation</returns>
  73. public ulong TranslateAndCreateBuffer(MemoryManager memoryManager, ulong gpuVa, ulong size)
  74. {
  75. if (gpuVa == 0)
  76. {
  77. return 0;
  78. }
  79. ulong address = memoryManager.Translate(gpuVa);
  80. if (address == MemoryManager.PteUnmapped)
  81. {
  82. return 0;
  83. }
  84. CreateBuffer(address, size);
  85. return address;
  86. }
  87. /// <summary>
  88. /// Creates a new buffer for the specified range, if it does not yet exist.
  89. /// This can be used to ensure the existance of a buffer.
  90. /// </summary>
  91. /// <param name="address">Address of the buffer in memory</param>
  92. /// <param name="size">Size of the buffer in bytes</param>
  93. public void CreateBuffer(ulong address, ulong size)
  94. {
  95. ulong endAddress = address + size;
  96. ulong alignedAddress = address & ~BufferAlignmentMask;
  97. ulong alignedEndAddress = (endAddress + BufferAlignmentMask) & ~BufferAlignmentMask;
  98. // The buffer must have the size of at least one page.
  99. if (alignedEndAddress == alignedAddress)
  100. {
  101. alignedEndAddress += BufferAlignmentSize;
  102. }
  103. CreateBufferAligned(alignedAddress, alignedEndAddress - alignedAddress);
  104. }
  105. /// <summary>
  106. /// Performs address translation of the GPU virtual address, and attempts to force
  107. /// the buffer in the region as dirty.
  108. /// The buffer lookup for this function is cached in a dictionary for quick access, which
  109. /// accelerates common UBO updates.
  110. /// </summary>
  111. /// <param name="memoryManager">GPU memory manager where the buffer is mapped</param>
  112. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  113. /// <param name="size">Size in bytes of the buffer</param>
  114. public void ForceDirty(MemoryManager memoryManager, ulong gpuVa, ulong size)
  115. {
  116. if (_pruneCaches)
  117. {
  118. Prune();
  119. }
  120. if (!_dirtyCache.TryGetValue(gpuVa, out BufferCacheEntry result) ||
  121. result.EndGpuAddress < gpuVa + size ||
  122. result.UnmappedSequence != result.Buffer.UnmappedSequence)
  123. {
  124. ulong address = TranslateAndCreateBuffer(memoryManager, gpuVa, size);
  125. result = new BufferCacheEntry(address, gpuVa, GetBuffer(address, size));
  126. _dirtyCache[gpuVa] = result;
  127. }
  128. result.Buffer.ForceDirty(result.Address, size);
  129. }
  130. /// <summary>
  131. /// Checks if the given buffer range has been GPU modifed.
  132. /// </summary>
  133. /// <param name="memoryManager">GPU memory manager where the buffer is mapped</param>
  134. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  135. /// <param name="size">Size in bytes of the buffer</param>
  136. /// <returns>True if modified, false otherwise</returns>
  137. public bool CheckModified(MemoryManager memoryManager, ulong gpuVa, ulong size, out ulong outAddr)
  138. {
  139. if (_pruneCaches)
  140. {
  141. Prune();
  142. }
  143. // Align the address to avoid creating too many entries on the quick lookup dictionary.
  144. ulong mask = BufferAlignmentMask;
  145. ulong alignedGpuVa = gpuVa & (~mask);
  146. ulong alignedEndGpuVa = (gpuVa + size + mask) & (~mask);
  147. size = alignedEndGpuVa - alignedGpuVa;
  148. if (!_modifiedCache.TryGetValue(alignedGpuVa, out BufferCacheEntry result) ||
  149. result.EndGpuAddress < alignedEndGpuVa ||
  150. result.UnmappedSequence != result.Buffer.UnmappedSequence)
  151. {
  152. ulong address = TranslateAndCreateBuffer(memoryManager, alignedGpuVa, size);
  153. result = new BufferCacheEntry(address, alignedGpuVa, GetBuffer(address, size));
  154. _modifiedCache[alignedGpuVa] = result;
  155. }
  156. outAddr = result.Address | (gpuVa & mask);
  157. return result.Buffer.IsModified(result.Address, size);
  158. }
  159. /// <summary>
  160. /// Creates a new buffer for the specified range, if needed.
  161. /// If a buffer where this range can be fully contained already exists,
  162. /// then the creation of a new buffer is not necessary.
  163. /// </summary>
  164. /// <param name="address">Address of the buffer in guest memory</param>
  165. /// <param name="size">Size in bytes of the buffer</param>
  166. private void CreateBufferAligned(ulong address, ulong size)
  167. {
  168. int overlapsCount = _buffers.FindOverlapsNonOverlapping(address, size, ref _bufferOverlaps);
  169. if (overlapsCount != 0)
  170. {
  171. // The buffer already exists. We can just return the existing buffer
  172. // if the buffer we need is fully contained inside the overlapping buffer.
  173. // Otherwise, we must delete the overlapping buffers and create a bigger buffer
  174. // that fits all the data we need. We also need to copy the contents from the
  175. // old buffer(s) to the new buffer.
  176. ulong endAddress = address + size;
  177. if (_bufferOverlaps[0].Address > address || _bufferOverlaps[0].EndAddress < endAddress)
  178. {
  179. // Check if the following conditions are met:
  180. // - We have a single overlap.
  181. // - The overlap starts at or before the requested range. That is, the overlap happens at the end.
  182. // - The size delta between the new, merged buffer and the old one is of at most 2 pages.
  183. // In this case, we attempt to extend the buffer further than the requested range,
  184. // this can potentially avoid future resizes if the application keeps using overlapping
  185. // sequential memory.
  186. // Allowing for 2 pages (rather than just one) is necessary to catch cases where the
  187. // range crosses a page, and after alignment, ends having a size of 2 pages.
  188. if (overlapsCount == 1 &&
  189. address >= _bufferOverlaps[0].Address &&
  190. endAddress - _bufferOverlaps[0].EndAddress <= BufferAlignmentSize * 2)
  191. {
  192. // Try to grow the buffer by 1.5x of its current size.
  193. // This improves performance in the cases where the buffer is resized often by small amounts.
  194. ulong existingSize = _bufferOverlaps[0].Size;
  195. ulong growthSize = (existingSize + Math.Min(existingSize >> 1, MaxDynamicGrowthSize)) & ~BufferAlignmentMask;
  196. size = Math.Max(size, growthSize);
  197. endAddress = address + size;
  198. overlapsCount = _buffers.FindOverlapsNonOverlapping(address, size, ref _bufferOverlaps);
  199. }
  200. for (int index = 0; index < overlapsCount; index++)
  201. {
  202. Buffer buffer = _bufferOverlaps[index];
  203. address = Math.Min(address, buffer.Address);
  204. endAddress = Math.Max(endAddress, buffer.EndAddress);
  205. lock (_buffers)
  206. {
  207. _buffers.Remove(buffer);
  208. }
  209. }
  210. ulong newSize = endAddress - address;
  211. Buffer newBuffer = new Buffer(_context, _physicalMemory, address, newSize, _bufferOverlaps.Take(overlapsCount));
  212. lock (_buffers)
  213. {
  214. _buffers.Add(newBuffer);
  215. }
  216. for (int index = 0; index < overlapsCount; index++)
  217. {
  218. Buffer buffer = _bufferOverlaps[index];
  219. int dstOffset = (int)(buffer.Address - newBuffer.Address);
  220. buffer.CopyTo(newBuffer, dstOffset);
  221. newBuffer.InheritModifiedRanges(buffer);
  222. buffer.DecrementReferenceCount();
  223. }
  224. newBuffer.SynchronizeMemory(address, newSize);
  225. // Existing buffers were modified, we need to rebind everything.
  226. NotifyBuffersModified?.Invoke();
  227. }
  228. }
  229. else
  230. {
  231. // No overlap, just create a new buffer.
  232. Buffer buffer = new Buffer(_context, _physicalMemory, address, size);
  233. lock (_buffers)
  234. {
  235. _buffers.Add(buffer);
  236. }
  237. }
  238. ShrinkOverlapsBufferIfNeeded();
  239. }
  240. /// <summary>
  241. /// Resizes the temporary buffer used for range list intersection results, if it has grown too much.
  242. /// </summary>
  243. private void ShrinkOverlapsBufferIfNeeded()
  244. {
  245. if (_bufferOverlaps.Length > OverlapsBufferMaxCapacity)
  246. {
  247. Array.Resize(ref _bufferOverlaps, OverlapsBufferMaxCapacity);
  248. }
  249. }
  250. /// <summary>
  251. /// Copy a buffer data from a given address to another.
  252. /// </summary>
  253. /// <remarks>
  254. /// This does a GPU side copy.
  255. /// </remarks>
  256. /// <param name="memoryManager">GPU memory manager where the buffer is mapped</param>
  257. /// <param name="srcVa">GPU virtual address of the copy source</param>
  258. /// <param name="dstVa">GPU virtual address of the copy destination</param>
  259. /// <param name="size">Size in bytes of the copy</param>
  260. public void CopyBuffer(MemoryManager memoryManager, ulong srcVa, ulong dstVa, ulong size)
  261. {
  262. ulong srcAddress = TranslateAndCreateBuffer(memoryManager, srcVa, size);
  263. ulong dstAddress = TranslateAndCreateBuffer(memoryManager, dstVa, size);
  264. Buffer srcBuffer = GetBuffer(srcAddress, size);
  265. Buffer dstBuffer = GetBuffer(dstAddress, size);
  266. int srcOffset = (int)(srcAddress - srcBuffer.Address);
  267. int dstOffset = (int)(dstAddress - dstBuffer.Address);
  268. _context.Renderer.Pipeline.CopyBuffer(
  269. srcBuffer.Handle,
  270. dstBuffer.Handle,
  271. srcOffset,
  272. dstOffset,
  273. (int)size);
  274. if (srcBuffer.IsModified(srcAddress, size))
  275. {
  276. dstBuffer.SignalModified(dstAddress, size);
  277. }
  278. else
  279. {
  280. // Optimization: If the data being copied is already in memory, then copy it directly instead of flushing from GPU.
  281. dstBuffer.ClearModified(dstAddress, size);
  282. memoryManager.Physical.WriteUntracked(dstAddress, memoryManager.Physical.GetSpan(srcAddress, (int)size));
  283. }
  284. }
  285. /// <summary>
  286. /// Clears a buffer at a given address with the specified value.
  287. /// </summary>
  288. /// <remarks>
  289. /// Both the address and size must be aligned to 4 bytes.
  290. /// </remarks>
  291. /// <param name="memoryManager">GPU memory manager where the buffer is mapped</param>
  292. /// <param name="gpuVa">GPU virtual address of the region to clear</param>
  293. /// <param name="size">Number of bytes to clear</param>
  294. /// <param name="value">Value to be written into the buffer</param>
  295. public void ClearBuffer(MemoryManager memoryManager, ulong gpuVa, ulong size, uint value)
  296. {
  297. ulong address = TranslateAndCreateBuffer(memoryManager, gpuVa, size);
  298. Buffer buffer = GetBuffer(address, size);
  299. int offset = (int)(address - buffer.Address);
  300. _context.Renderer.Pipeline.ClearBuffer(buffer.Handle, offset, (int)size, value);
  301. buffer.SignalModified(address, size);
  302. }
  303. /// <summary>
  304. /// Gets a buffer sub-range starting at a given memory address.
  305. /// </summary>
  306. /// <param name="address">Start address of the memory range</param>
  307. /// <param name="size">Size in bytes of the memory range</param>
  308. /// <param name="write">Whether the buffer will be written to by this use</param>
  309. /// <returns>The buffer sub-range starting at the given memory address</returns>
  310. public BufferRange GetBufferRangeTillEnd(ulong address, ulong size, bool write = false)
  311. {
  312. return GetBuffer(address, size, write).GetRange(address);
  313. }
  314. /// <summary>
  315. /// Gets a buffer sub-range for a given memory range.
  316. /// </summary>
  317. /// <param name="address">Start address of the memory range</param>
  318. /// <param name="size">Size in bytes of the memory range</param>
  319. /// <param name="write">Whether the buffer will be written to by this use</param>
  320. /// <returns>The buffer sub-range for the given range</returns>
  321. public BufferRange GetBufferRange(ulong address, ulong size, bool write = false)
  322. {
  323. return GetBuffer(address, size, write).GetRange(address, size);
  324. }
  325. /// <summary>
  326. /// Gets a buffer for a given memory range.
  327. /// A buffer overlapping with the specified range is assumed to already exist on the cache.
  328. /// </summary>
  329. /// <param name="address">Start address of the memory range</param>
  330. /// <param name="size">Size in bytes of the memory range</param>
  331. /// <param name="write">Whether the buffer will be written to by this use</param>
  332. /// <returns>The buffer where the range is fully contained</returns>
  333. private Buffer GetBuffer(ulong address, ulong size, bool write = false)
  334. {
  335. Buffer buffer;
  336. if (size != 0)
  337. {
  338. buffer = _buffers.FindFirstOverlap(address, size);
  339. buffer.SynchronizeMemory(address, size);
  340. if (write)
  341. {
  342. buffer.SignalModified(address, size);
  343. }
  344. }
  345. else
  346. {
  347. buffer = _buffers.FindFirstOverlap(address, 1);
  348. }
  349. return buffer;
  350. }
  351. /// <summary>
  352. /// Performs guest to host memory synchronization of a given memory range.
  353. /// </summary>
  354. /// <param name="address">Start address of the memory range</param>
  355. /// <param name="size">Size in bytes of the memory range</param>
  356. public void SynchronizeBufferRange(ulong address, ulong size)
  357. {
  358. if (size != 0)
  359. {
  360. Buffer buffer = _buffers.FindFirstOverlap(address, size);
  361. buffer.SynchronizeMemory(address, size);
  362. }
  363. }
  364. /// <summary>
  365. /// Prune any invalid entries from a quick access dictionary.
  366. /// </summary>
  367. /// <param name="dictionary">Dictionary to prune</param>
  368. /// <param name="toDelete">List used to track entries to delete</param>
  369. private void Prune(Dictionary<ulong, BufferCacheEntry> dictionary, ref List<ulong> toDelete)
  370. {
  371. foreach (var entry in dictionary)
  372. {
  373. if (entry.Value.UnmappedSequence != entry.Value.Buffer.UnmappedSequence)
  374. {
  375. (toDelete ??= new()).Add(entry.Key);
  376. }
  377. }
  378. if (toDelete != null)
  379. {
  380. foreach (ulong entry in toDelete)
  381. {
  382. dictionary.Remove(entry);
  383. }
  384. }
  385. }
  386. /// <summary>
  387. /// Prune any invalid entries from the quick access dictionaries.
  388. /// </summary>
  389. private void Prune()
  390. {
  391. List<ulong> toDelete = null;
  392. Prune(_dirtyCache, ref toDelete);
  393. toDelete?.Clear();
  394. Prune(_modifiedCache, ref toDelete);
  395. _pruneCaches = false;
  396. }
  397. /// <summary>
  398. /// Queues a prune of invalid entries the next time a dictionary cache is accessed.
  399. /// </summary>
  400. public void QueuePrune()
  401. {
  402. _pruneCaches = true;
  403. }
  404. /// <summary>
  405. /// Disposes all buffers in the cache.
  406. /// It's an error to use the buffer manager after disposal.
  407. /// </summary>
  408. public void Dispose()
  409. {
  410. lock (_buffers)
  411. {
  412. foreach (Buffer buffer in _buffers)
  413. {
  414. buffer.Dispose();
  415. }
  416. }
  417. }
  418. }
  419. }