PhysicalMemory.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. using Ryujinx.Cpu;
  2. using Ryujinx.Cpu.Tracking;
  3. using Ryujinx.Graphics.Gpu.Image;
  4. using Ryujinx.Graphics.Gpu.Shader;
  5. using Ryujinx.Memory;
  6. using Ryujinx.Memory.Range;
  7. using Ryujinx.Memory.Tracking;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Threading;
  11. namespace Ryujinx.Graphics.Gpu.Memory
  12. {
  13. /// <summary>
  14. /// Represents physical memory, accessible from the GPU.
  15. /// This is actually working CPU virtual addresses, of memory mapped on the application process.
  16. /// </summary>
  17. class PhysicalMemory : IDisposable
  18. {
  19. private readonly GpuContext _context;
  20. private IVirtualMemoryManagerTracked _cpuMemory;
  21. private int _referenceCount;
  22. /// <summary>
  23. /// Indicates whenever the memory manager supports 4KB pages.
  24. /// </summary>
  25. public bool Supports4KBPages => _cpuMemory.Supports4KBPages;
  26. /// <summary>
  27. /// In-memory shader cache.
  28. /// </summary>
  29. public ShaderCache ShaderCache { get; }
  30. /// <summary>
  31. /// GPU buffer manager.
  32. /// </summary>
  33. public BufferCache BufferCache { get; }
  34. /// <summary>
  35. /// GPU texture manager.
  36. /// </summary>
  37. public TextureCache TextureCache { get; }
  38. /// <summary>
  39. /// Creates a new instance of the physical memory.
  40. /// </summary>
  41. /// <param name="context">GPU context that the physical memory belongs to</param>
  42. /// <param name="cpuMemory">CPU memory manager of the application process</param>
  43. public PhysicalMemory(GpuContext context, IVirtualMemoryManagerTracked cpuMemory)
  44. {
  45. _context = context;
  46. _cpuMemory = cpuMemory;
  47. ShaderCache = new ShaderCache(context);
  48. BufferCache = new BufferCache(context, this);
  49. TextureCache = new TextureCache(context, this);
  50. if (cpuMemory is IRefCounted rc)
  51. {
  52. rc.IncrementReferenceCount();
  53. }
  54. _referenceCount = 1;
  55. }
  56. /// <summary>
  57. /// Increments the memory reference count.
  58. /// </summary>
  59. public void IncrementReferenceCount()
  60. {
  61. Interlocked.Increment(ref _referenceCount);
  62. }
  63. /// <summary>
  64. /// Decrements the memory reference count.
  65. /// </summary>
  66. public void DecrementReferenceCount()
  67. {
  68. if (Interlocked.Decrement(ref _referenceCount) == 0 && _cpuMemory is IRefCounted rc)
  69. {
  70. rc.DecrementReferenceCount();
  71. }
  72. }
  73. /// <summary>
  74. /// Gets a span of data from the application process.
  75. /// </summary>
  76. /// <param name="address">Start address of the range</param>
  77. /// <param name="size">Size in bytes to be range</param>
  78. /// <param name="tracked">True if read tracking is triggered on the span</param>
  79. /// <returns>A read only span of the data at the specified memory location</returns>
  80. public ReadOnlySpan<byte> GetSpan(ulong address, int size, bool tracked = false)
  81. {
  82. return _cpuMemory.GetSpan(address, size, tracked);
  83. }
  84. /// <summary>
  85. /// Gets a span of data from the application process.
  86. /// </summary>
  87. /// <param name="range">Ranges of physical memory where the data is located</param>
  88. /// <param name="tracked">True if read tracking is triggered on the span</param>
  89. /// <returns>A read only span of the data at the specified memory location</returns>
  90. public ReadOnlySpan<byte> GetSpan(MultiRange range, bool tracked = false)
  91. {
  92. if (range.Count == 1)
  93. {
  94. var singleRange = range.GetSubRange(0);
  95. if (singleRange.Address != MemoryManager.PteUnmapped)
  96. {
  97. return _cpuMemory.GetSpan(singleRange.Address, (int)singleRange.Size, tracked);
  98. }
  99. }
  100. Span<byte> data = new byte[range.GetSize()];
  101. int offset = 0;
  102. for (int i = 0; i < range.Count; i++)
  103. {
  104. var currentRange = range.GetSubRange(i);
  105. int size = (int)currentRange.Size;
  106. if (currentRange.Address != MemoryManager.PteUnmapped)
  107. {
  108. _cpuMemory.GetSpan(currentRange.Address, size, tracked).CopyTo(data.Slice(offset, size));
  109. }
  110. offset += size;
  111. }
  112. return data;
  113. }
  114. /// <summary>
  115. /// Gets a writable region from the application process.
  116. /// </summary>
  117. /// <param name="address">Start address of the range</param>
  118. /// <param name="size">Size in bytes to be range</param>
  119. /// <param name="tracked">True if write tracking is triggered on the span</param>
  120. /// <returns>A writable region with the data at the specified memory location</returns>
  121. public WritableRegion GetWritableRegion(ulong address, int size, bool tracked = false)
  122. {
  123. return _cpuMemory.GetWritableRegion(address, size, tracked);
  124. }
  125. /// <summary>
  126. /// Gets a writable region from GPU mapped memory.
  127. /// </summary>
  128. /// <param name="range">Range</param>
  129. /// <param name="tracked">True if write tracking is triggered on the span</param>
  130. /// <returns>A writable region with the data at the specified memory location</returns>
  131. public WritableRegion GetWritableRegion(MultiRange range, bool tracked = false)
  132. {
  133. if (range.Count == 1)
  134. {
  135. MemoryRange subrange = range.GetSubRange(0);
  136. return GetWritableRegion(subrange.Address, (int)subrange.Size, tracked);
  137. }
  138. else
  139. {
  140. Memory<byte> memory = new byte[range.GetSize()];
  141. int offset = 0;
  142. for (int i = 0; i < range.Count; i++)
  143. {
  144. var currentRange = range.GetSubRange(i);
  145. int size = (int)currentRange.Size;
  146. if (currentRange.Address != MemoryManager.PteUnmapped)
  147. {
  148. GetSpan(currentRange.Address, size).CopyTo(memory.Span.Slice(offset, size));
  149. }
  150. offset += size;
  151. }
  152. return new WritableRegion(new MultiRangeWritableBlock(range, this), 0, memory, tracked);
  153. }
  154. }
  155. /// <summary>
  156. /// Reads data from the application process.
  157. /// </summary>
  158. /// <typeparam name="T">Type of the structure</typeparam>
  159. /// <param name="address">Address to read from</param>
  160. /// <returns>The data at the specified memory location</returns>
  161. public T Read<T>(ulong address) where T : unmanaged
  162. {
  163. return _cpuMemory.Read<T>(address);
  164. }
  165. /// <summary>
  166. /// Reads data from the application process, with write tracking.
  167. /// </summary>
  168. /// <typeparam name="T">Type of the structure</typeparam>
  169. /// <param name="address">Address to read from</param>
  170. /// <returns>The data at the specified memory location</returns>
  171. public T ReadTracked<T>(ulong address) where T : unmanaged
  172. {
  173. return _cpuMemory.ReadTracked<T>(address);
  174. }
  175. /// <summary>
  176. /// Writes data to the application process, triggering a precise memory tracking event.
  177. /// </summary>
  178. /// <param name="address">Address to write into</param>
  179. /// <param name="data">Data to be written</param>
  180. public void WriteTrackedResource(ulong address, ReadOnlySpan<byte> data)
  181. {
  182. _cpuMemory.SignalMemoryTracking(address, (ulong)data.Length, true, precise: true);
  183. _cpuMemory.WriteUntracked(address, data);
  184. }
  185. /// <summary>
  186. /// Writes data to the application process.
  187. /// </summary>
  188. /// <param name="address">Address to write into</param>
  189. /// <param name="data">Data to be written</param>
  190. public void Write(ulong address, ReadOnlySpan<byte> data)
  191. {
  192. _cpuMemory.Write(address, data);
  193. }
  194. /// <summary>
  195. /// Writes data to the application process.
  196. /// </summary>
  197. /// <param name="range">Ranges of physical memory where the data is located</param>
  198. /// <param name="data">Data to be written</param>
  199. public void Write(MultiRange range, ReadOnlySpan<byte> data)
  200. {
  201. WriteImpl(range, data, _cpuMemory.Write);
  202. }
  203. /// <summary>
  204. /// Writes data to the application process, without any tracking.
  205. /// </summary>
  206. /// <param name="address">Address to write into</param>
  207. /// <param name="data">Data to be written</param>
  208. public void WriteUntracked(ulong address, ReadOnlySpan<byte> data)
  209. {
  210. _cpuMemory.WriteUntracked(address, data);
  211. }
  212. /// <summary>
  213. /// Writes data to the application process, without any tracking.
  214. /// </summary>
  215. /// <param name="range">Ranges of physical memory where the data is located</param>
  216. /// <param name="data">Data to be written</param>
  217. public void WriteUntracked(MultiRange range, ReadOnlySpan<byte> data)
  218. {
  219. WriteImpl(range, data, _cpuMemory.WriteUntracked);
  220. }
  221. /// <summary>
  222. /// Writes data to the application process, returning false if the data was not changed.
  223. /// This triggers read memory tracking, as a redundancy check would be useless if the data is not up to date.
  224. /// </summary>
  225. /// <remarks>The memory manager can return that memory has changed when it hasn't to avoid expensive data copies.</remarks>
  226. /// <param name="address">Address to write into</param>
  227. /// <param name="data">Data to be written</param>
  228. /// <returns>True if the data was changed, false otherwise</returns>
  229. public bool WriteWithRedundancyCheck(ulong address, ReadOnlySpan<byte> data)
  230. {
  231. return _cpuMemory.WriteWithRedundancyCheck(address, data);
  232. }
  233. private delegate void WriteCallback(ulong address, ReadOnlySpan<byte> data);
  234. /// <summary>
  235. /// Writes data to the application process, using the supplied callback method.
  236. /// </summary>
  237. /// <param name="range">Ranges of physical memory where the data is located</param>
  238. /// <param name="data">Data to be written</param>
  239. /// <param name="writeCallback">Callback method that will perform the write</param>
  240. private static void WriteImpl(MultiRange range, ReadOnlySpan<byte> data, WriteCallback writeCallback)
  241. {
  242. if (range.Count == 1)
  243. {
  244. var singleRange = range.GetSubRange(0);
  245. if (singleRange.Address != MemoryManager.PteUnmapped)
  246. {
  247. writeCallback(singleRange.Address, data);
  248. }
  249. }
  250. else
  251. {
  252. int offset = 0;
  253. for (int i = 0; i < range.Count; i++)
  254. {
  255. var currentRange = range.GetSubRange(i);
  256. int size = (int)currentRange.Size;
  257. if (currentRange.Address != MemoryManager.PteUnmapped)
  258. {
  259. writeCallback(currentRange.Address, data.Slice(offset, size));
  260. }
  261. offset += size;
  262. }
  263. }
  264. }
  265. /// <summary>
  266. /// Obtains a memory tracking handle for the given virtual region. This should be disposed when finished with.
  267. /// </summary>
  268. /// <param name="address">CPU virtual address of the region</param>
  269. /// <param name="size">Size of the region</param>
  270. /// <returns>The memory tracking handle</returns>
  271. public CpuRegionHandle BeginTracking(ulong address, ulong size)
  272. {
  273. return _cpuMemory.BeginTracking(address, size);
  274. }
  275. /// <summary>
  276. /// Obtains a memory tracking handle for the given virtual region. This should be disposed when finished with.
  277. /// </summary>
  278. /// <param name="range">Ranges of physical memory where the data is located</param>
  279. /// <returns>The memory tracking handle</returns>
  280. public GpuRegionHandle BeginTracking(MultiRange range)
  281. {
  282. var cpuRegionHandles = new CpuRegionHandle[range.Count];
  283. int count = 0;
  284. for (int i = 0; i < range.Count; i++)
  285. {
  286. var currentRange = range.GetSubRange(i);
  287. if (currentRange.Address != MemoryManager.PteUnmapped)
  288. {
  289. cpuRegionHandles[count++] = _cpuMemory.BeginTracking(currentRange.Address, currentRange.Size);
  290. }
  291. }
  292. if (count != range.Count)
  293. {
  294. Array.Resize(ref cpuRegionHandles, count);
  295. }
  296. return new GpuRegionHandle(cpuRegionHandles);
  297. }
  298. /// <summary>
  299. /// Obtains a memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
  300. /// </summary>
  301. /// <param name="address">CPU virtual address of the region</param>
  302. /// <param name="size">Size of the region</param>
  303. /// <param name="handles">Handles to inherit state from or reuse</param>
  304. /// <param name="granularity">Desired granularity of write tracking</param>
  305. /// <returns>The memory tracking handle</returns>
  306. public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles = null, ulong granularity = 4096)
  307. {
  308. return _cpuMemory.BeginGranularTracking(address, size, handles, granularity);
  309. }
  310. /// <summary>
  311. /// Obtains a smart memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
  312. /// </summary>
  313. /// <param name="address">CPU virtual address of the region</param>
  314. /// <param name="size">Size of the region</param>
  315. /// <param name="granularity">Desired granularity of write tracking</param>
  316. /// <returns>The memory tracking handle</returns>
  317. public CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity = 4096)
  318. {
  319. return _cpuMemory.BeginSmartGranularTracking(address, size, granularity);
  320. }
  321. /// <summary>
  322. /// Checks if a given memory page is mapped.
  323. /// </summary>
  324. /// <param name="address">CPU virtual address of the page</param>
  325. /// <returns>True if mapped, false otherwise</returns>
  326. public bool IsMapped(ulong address)
  327. {
  328. return _cpuMemory.IsMapped(address);
  329. }
  330. /// <summary>
  331. /// Release our reference to the CPU memory manager.
  332. /// </summary>
  333. public void Dispose()
  334. {
  335. _context.DeferredActions.Enqueue(Destroy);
  336. }
  337. /// <summary>
  338. /// Performs disposal of the host GPU caches with resources mapped on this physical memory.
  339. /// This must only be called from the render thread.
  340. /// </summary>
  341. private void Destroy()
  342. {
  343. ShaderCache.Dispose();
  344. BufferCache.Dispose();
  345. TextureCache.Dispose();
  346. DecrementReferenceCount();
  347. }
  348. }
  349. }