BufferCache.cs 17 KB

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