PhysicalMemory.cs 17 KB

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