PhysicalMemory.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Ryujinx.Cpu;
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Graphics.Gpu.Memory
  6. {
  7. /// <summary>
  8. /// Represents physical memory, accessible from the GPU.
  9. /// This is actually working CPU virtual addresses, of memory mapped on the application process.
  10. /// </summary>
  11. class PhysicalMemory
  12. {
  13. public const int PageSize = Cpu.MemoryManager.PageSize;
  14. private readonly Cpu.MemoryManager _cpuMemory;
  15. /// <summary>
  16. /// Creates a new instance of the physical memory.
  17. /// </summary>
  18. /// <param name="cpuMemory">CPU memory manager of the application process</param>
  19. public PhysicalMemory(Cpu.MemoryManager cpuMemory)
  20. {
  21. _cpuMemory = cpuMemory;
  22. }
  23. /// <summary>
  24. /// Gets a span of data from the application process.
  25. /// </summary>
  26. /// <param name="address">Start address of the range</param>
  27. /// <param name="size">Size in bytes to be range</param>
  28. /// <returns>A read only span of the data at the specified memory location</returns>
  29. public ReadOnlySpan<byte> GetSpan(ulong address, int size)
  30. {
  31. return _cpuMemory.GetSpan(address, size);
  32. }
  33. /// <summary>
  34. /// Gets a writable region from the application process.
  35. /// </summary>
  36. /// <param name="address">Start address of the range</param>
  37. /// <param name="size">Size in bytes to be range</param>
  38. /// <returns>A writable region with the data at the specified memory location</returns>
  39. public WritableRegion GetWritableRegion(ulong address, int size)
  40. {
  41. return _cpuMemory.GetWritableRegion(address, size);
  42. }
  43. /// <summary>
  44. /// Reads data from the application process.
  45. /// </summary>
  46. /// <typeparam name="T">Type of the structure</typeparam>
  47. /// <param name="gpuVa">Address to read from</param>
  48. /// <returns>The data at the specified memory location</returns>
  49. public T Read<T>(ulong address) where T : unmanaged
  50. {
  51. return MemoryMarshal.Cast<byte, T>(GetSpan(address, Unsafe.SizeOf<T>()))[0];
  52. }
  53. /// <summary>
  54. /// Writes data to the application process.
  55. /// </summary>
  56. /// <param name="address">Address to write into</param>
  57. /// <param name="data">Data to be written</param>
  58. public void Write(ulong address, ReadOnlySpan<byte> data)
  59. {
  60. _cpuMemory.Write(address, data);
  61. }
  62. /// <summary>
  63. /// Writes data to the application process, without any tracking.
  64. /// </summary>
  65. /// <param name="address">Address to write into</param>
  66. /// <param name="data">Data to be written</param>
  67. public void WriteUntracked(ulong address, ReadOnlySpan<byte> data)
  68. {
  69. _cpuMemory.WriteUntracked(address, data);
  70. }
  71. /// <summary>
  72. /// Checks if a specified virtual memory region has been modified by the CPU since the last call.
  73. /// </summary>
  74. /// <param name="address">CPU virtual address of the region</param>
  75. /// <param name="size">Size of the region</param>
  76. /// <param name="name">Resource name</param>
  77. /// <param name="modifiedRanges">Optional array where the modified ranges should be written</param>
  78. /// <returns>The number of modified ranges</returns>
  79. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  80. public int QueryModified(ulong address, ulong size, ResourceName name, (ulong, ulong)[] modifiedRanges = null)
  81. {
  82. return _cpuMemory.QueryModified(address, size, (int)name, modifiedRanges);
  83. }
  84. }
  85. }