BufferCache.cs 14 KB

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