PhysicalMemory.cs 4.9 KB

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