BufferCache.cs 15 KB

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