PhysicalMemory.cs 13 KB

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