BufferCache.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. private readonly RangeList<Buffer> _buffers;
  21. private Buffer[] _bufferOverlaps;
  22. private readonly Dictionary<ulong, BufferCacheEntry> _dirtyCache;
  23. private readonly Dictionary<ulong, BufferCacheEntry> _modifiedCache;
  24. private bool _pruneCaches;
  25. public event Action NotifyBuffersModified;
  26. /// <summary>
  27. /// Creates a new instance of the buffer manager.
  28. /// </summary>
  29. /// <param name="context">The GPU context that the buffer manager belongs to</param>
  30. /// <param name="physicalMemory">Physical memory where the cached buffers are mapped</param>
  31. public BufferCache(GpuContext context, PhysicalMemory physicalMemory)
  32. {
  33. _context = context;
  34. _physicalMemory = physicalMemory;
  35. _buffers = new RangeList<Buffer>();
  36. _bufferOverlaps = new Buffer[OverlapsBufferInitialCapacity];
  37. _dirtyCache = new Dictionary<ulong, BufferCacheEntry>();
  38. // There are a lot more entries on the modified cache, so it is separate from the one for ForceDirty.
  39. _modifiedCache = new Dictionary<ulong, BufferCacheEntry>();
  40. }
  41. /// <summary>
  42. /// Handles removal of buffers written to a memory region being unmapped.
  43. /// </summary>
  44. /// <param name="sender">Sender object</param>
  45. /// <param name="e">Event arguments</param>
  46. public void MemoryUnmappedHandler(object sender, UnmapEventArgs e)
  47. {
  48. Buffer[] overlaps = new Buffer[10];
  49. int overlapCount;
  50. ulong address = ((MemoryManager)sender).Translate(e.Address);
  51. ulong size = e.Size;
  52. lock (_buffers)
  53. {
  54. overlapCount = _buffers.FindOverlaps(address, size, ref overlaps);
  55. }
  56. for (int i = 0; i < overlapCount; i++)
  57. {
  58. overlaps[i].Unmapped(address, size);
  59. }
  60. }
  61. /// <summary>
  62. /// Performs address translation of the GPU virtual address, and creates a
  63. /// new buffer, if needed, for the specified range.
  64. /// </summary>
  65. /// <param name="memoryManager">GPU memory manager where the buffer is mapped</param>
  66. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  67. /// <param name="size">Size in bytes of the buffer</param>
  68. /// <returns>CPU virtual address of the buffer, after address translation</returns>
  69. public ulong TranslateAndCreateBuffer(MemoryManager memoryManager, ulong gpuVa, ulong size)
  70. {
  71. if (gpuVa == 0)
  72. {
  73. return 0;
  74. }
  75. ulong address = memoryManager.Translate(gpuVa);
  76. if (address == MemoryManager.PteUnmapped)
  77. {
  78. return 0;
  79. }
  80. CreateBuffer(address, size);
  81. return address;
  82. }
  83. /// <summary>
  84. /// Creates a new buffer for the specified range, if it does not yet exist.
  85. /// This can be used to ensure the existance of a buffer.
  86. /// </summary>
  87. /// <param name="address">Address of the buffer in memory</param>
  88. /// <param name="size">Size of the buffer in bytes</param>
  89. public void CreateBuffer(ulong address, ulong size)
  90. {
  91. ulong endAddress = address + size;
  92. ulong alignedAddress = address & ~BufferAlignmentMask;
  93. ulong alignedEndAddress = (endAddress + BufferAlignmentMask) & ~BufferAlignmentMask;
  94. // The buffer must have the size of at least one page.
  95. if (alignedEndAddress == alignedAddress)
  96. {
  97. alignedEndAddress += BufferAlignmentSize;
  98. }
  99. CreateBufferAligned(alignedAddress, alignedEndAddress - alignedAddress);
  100. }
  101. /// <summary>
  102. /// Performs address translation of the GPU virtual address, and attempts to force
  103. /// the buffer in the region as dirty.
  104. /// The buffer lookup for this function is cached in a dictionary for quick access, which
  105. /// accelerates common UBO updates.
  106. /// </summary>
  107. /// <param name="memoryManager">GPU memory manager where the buffer is mapped</param>
  108. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  109. /// <param name="size">Size in bytes of the buffer</param>
  110. public void ForceDirty(MemoryManager memoryManager, ulong gpuVa, ulong size)
  111. {
  112. if (_pruneCaches)
  113. {
  114. Prune();
  115. }
  116. if (!_dirtyCache.TryGetValue(gpuVa, out BufferCacheEntry result) ||
  117. result.EndGpuAddress < gpuVa + size ||
  118. result.UnmappedSequence != result.Buffer.UnmappedSequence)
  119. {
  120. ulong address = TranslateAndCreateBuffer(memoryManager, gpuVa, size);
  121. result = new BufferCacheEntry(address, gpuVa, GetBuffer(address, size));
  122. _dirtyCache[gpuVa] = result;
  123. }
  124. result.Buffer.ForceDirty(result.Address, size);
  125. }
  126. /// <summary>
  127. /// Checks if the given buffer range has been GPU modifed.
  128. /// </summary>
  129. /// <param name="memoryManager">GPU memory manager where the buffer is mapped</param>
  130. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  131. /// <param name="size">Size in bytes of the buffer</param>
  132. /// <returns>True if modified, false otherwise</returns>
  133. public bool CheckModified(MemoryManager memoryManager, ulong gpuVa, ulong size, out ulong outAddr)
  134. {
  135. if (_pruneCaches)
  136. {
  137. Prune();
  138. }
  139. // Align the address to avoid creating too many entries on the quick lookup dictionary.
  140. ulong mask = BufferAlignmentMask;
  141. ulong alignedGpuVa = gpuVa & (~mask);
  142. ulong alignedEndGpuVa = (gpuVa + size + mask) & (~mask);
  143. size = alignedEndGpuVa - alignedGpuVa;
  144. if (!_modifiedCache.TryGetValue(alignedGpuVa, out BufferCacheEntry result) ||
  145. result.EndGpuAddress < alignedEndGpuVa ||
  146. result.UnmappedSequence != result.Buffer.UnmappedSequence)
  147. {
  148. ulong address = TranslateAndCreateBuffer(memoryManager, alignedGpuVa, size);
  149. result = new BufferCacheEntry(address, alignedGpuVa, GetBuffer(address, size));
  150. _modifiedCache[alignedGpuVa] = result;
  151. }
  152. outAddr = result.Address | (gpuVa & mask);
  153. return result.Buffer.IsModified(result.Address, size);
  154. }
  155. /// <summary>
  156. /// Creates a new buffer for the specified range, if needed.
  157. /// If a buffer where this range can be fully contained already exists,
  158. /// then the creation of a new buffer is not necessary.
  159. /// </summary>
  160. /// <param name="address">Address of the buffer in guest memory</param>
  161. /// <param name="size">Size in bytes of the buffer</param>
  162. private void CreateBufferAligned(ulong address, ulong size)
  163. {
  164. int overlapsCount;
  165. lock (_buffers)
  166. {
  167. overlapsCount = _buffers.FindOverlapsNonOverlapping(address, size, ref _bufferOverlaps);
  168. }
  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.DisposeData();
  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. lock (_buffers)
  339. {
  340. buffer = _buffers.FindFirstOverlap(address, size);
  341. }
  342. buffer.SynchronizeMemory(address, size);
  343. if (write)
  344. {
  345. buffer.SignalModified(address, size);
  346. }
  347. }
  348. else
  349. {
  350. lock (_buffers)
  351. {
  352. buffer = _buffers.FindFirstOverlap(address, 1);
  353. }
  354. }
  355. return buffer;
  356. }
  357. /// <summary>
  358. /// Performs guest to host memory synchronization of a given memory range.
  359. /// </summary>
  360. /// <param name="address">Start address of the memory range</param>
  361. /// <param name="size">Size in bytes of the memory range</param>
  362. public void SynchronizeBufferRange(ulong address, ulong size)
  363. {
  364. if (size != 0)
  365. {
  366. Buffer buffer;
  367. lock (_buffers)
  368. {
  369. buffer = _buffers.FindFirstOverlap(address, size);
  370. }
  371. buffer.SynchronizeMemory(address, size);
  372. }
  373. }
  374. /// <summary>
  375. /// Prune any invalid entries from a quick access dictionary.
  376. /// </summary>
  377. /// <param name="dictionary">Dictionary to prune</param>
  378. /// <param name="toDelete">List used to track entries to delete</param>
  379. private void Prune(Dictionary<ulong, BufferCacheEntry> dictionary, ref List<ulong> toDelete)
  380. {
  381. foreach (var entry in dictionary)
  382. {
  383. if (entry.Value.UnmappedSequence != entry.Value.Buffer.UnmappedSequence)
  384. {
  385. (toDelete ??= new()).Add(entry.Key);
  386. }
  387. }
  388. if (toDelete != null)
  389. {
  390. foreach (ulong entry in toDelete)
  391. {
  392. dictionary.Remove(entry);
  393. }
  394. }
  395. }
  396. /// <summary>
  397. /// Prune any invalid entries from the quick access dictionaries.
  398. /// </summary>
  399. private void Prune()
  400. {
  401. List<ulong> toDelete = null;
  402. Prune(_dirtyCache, ref toDelete);
  403. toDelete?.Clear();
  404. Prune(_modifiedCache, ref toDelete);
  405. _pruneCaches = false;
  406. }
  407. /// <summary>
  408. /// Queues a prune of invalid entries the next time a dictionary cache is accessed.
  409. /// </summary>
  410. public void QueuePrune()
  411. {
  412. _pruneCaches = true;
  413. }
  414. /// <summary>
  415. /// Disposes all buffers in the cache.
  416. /// It's an error to use the buffer manager after disposal.
  417. /// </summary>
  418. public void Dispose()
  419. {
  420. lock (_buffers)
  421. {
  422. foreach (Buffer buffer in _buffers)
  423. {
  424. buffer.Dispose();
  425. }
  426. }
  427. }
  428. }
  429. }