PhysicalMemory.cs 14 KB

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