PhysicalMemory.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Ryujinx.Cpu;
  2. using Ryujinx.Cpu.Tracking;
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. namespace Ryujinx.Graphics.Gpu.Memory
  7. {
  8. /// <summary>
  9. /// Represents physical memory, accessible from the GPU.
  10. /// This is actually working CPU virtual addresses, of memory mapped on the application process.
  11. /// </summary>
  12. class PhysicalMemory
  13. {
  14. public const int PageSize = Cpu.MemoryManager.PageSize;
  15. private readonly Cpu.MemoryManager _cpuMemory;
  16. /// <summary>
  17. /// Creates a new instance of the physical memory.
  18. /// </summary>
  19. /// <param name="cpuMemory">CPU memory manager of the application process</param>
  20. public PhysicalMemory(Cpu.MemoryManager cpuMemory)
  21. {
  22. _cpuMemory = cpuMemory;
  23. }
  24. /// <summary>
  25. /// Gets a span of data from the application process.
  26. /// </summary>
  27. /// <param name="address">Start address of the range</param>
  28. /// <param name="size">Size in bytes to be range</param>
  29. /// <param name="tracked">True if read tracking is triggered on the span</param>
  30. /// <returns>A read only span of the data at the specified memory location</returns>
  31. public ReadOnlySpan<byte> GetSpan(ulong address, int size, bool tracked = false)
  32. {
  33. return _cpuMemory.GetSpan(address, size, tracked);
  34. }
  35. /// <summary>
  36. /// Gets a writable region from the application process.
  37. /// </summary>
  38. /// <param name="address">Start address of the range</param>
  39. /// <param name="size">Size in bytes to be range</param>
  40. /// <returns>A writable region with the data at the specified memory location</returns>
  41. public WritableRegion GetWritableRegion(ulong address, int size)
  42. {
  43. return _cpuMemory.GetWritableRegion(address, size);
  44. }
  45. /// <summary>
  46. /// Reads data from the application process.
  47. /// </summary>
  48. /// <typeparam name="T">Type of the structure</typeparam>
  49. /// <param name="gpuVa">Address to read from</param>
  50. /// <returns>The data at the specified memory location</returns>
  51. public T Read<T>(ulong address) where T : unmanaged
  52. {
  53. return MemoryMarshal.Cast<byte, T>(GetSpan(address, Unsafe.SizeOf<T>()))[0];
  54. }
  55. /// <summary>
  56. /// Writes data to the application process.
  57. /// </summary>
  58. /// <param name="address">Address to write into</param>
  59. /// <param name="data">Data to be written</param>
  60. public void Write(ulong address, ReadOnlySpan<byte> data)
  61. {
  62. _cpuMemory.Write(address, data);
  63. }
  64. /// <summary>
  65. /// Writes data to the application process, without any tracking.
  66. /// </summary>
  67. /// <param name="address">Address to write into</param>
  68. /// <param name="data">Data to be written</param>
  69. public void WriteUntracked(ulong address, ReadOnlySpan<byte> data)
  70. {
  71. _cpuMemory.WriteUntracked(address, data);
  72. }
  73. /// <summary>
  74. /// Obtains a memory tracking handle for the given virtual region. This should be disposed when finished with.
  75. /// </summary>
  76. /// <param name="address">CPU virtual address of the region</param>
  77. /// <param name="size">Size of the region</param>
  78. /// <returns>The memory tracking handle</returns>
  79. public CpuRegionHandle BeginTracking(ulong address, ulong size)
  80. {
  81. return _cpuMemory.BeginTracking(address, size);
  82. }
  83. /// <summary>
  84. /// Obtains a memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
  85. /// </summary>
  86. /// <param name="address">CPU virtual address of the region</param>
  87. /// <param name="size">Size of the region</param>
  88. /// <param name="granularity">Desired granularity of write tracking</param>
  89. /// <returns>The memory tracking handle</returns>
  90. public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, ulong granularity = 4096)
  91. {
  92. return _cpuMemory.BeginGranularTracking(address, size, granularity);
  93. }
  94. /// <summary>
  95. /// Obtains a smart memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
  96. /// </summary>
  97. /// <param name="address">CPU virtual address of the region</param>
  98. /// <param name="size">Size of the region</param>
  99. /// <param name="granularity">Desired granularity of write tracking</param>
  100. /// <returns>The memory tracking handle</returns>
  101. public CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity = 4096)
  102. {
  103. return _cpuMemory.BeginSmartGranularTracking(address, size, granularity);
  104. }
  105. }
  106. }