BufferCache.cs 18 KB

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