PhysicalMemory.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /// Checks if a specified virtual memory region has been modified by the CPU since the last call.
  64. /// </summary>
  65. /// <param name="address">CPU virtual address of the region</param>
  66. /// <param name="size">Size of the region</param>
  67. /// <param name="name">Resource name</param>
  68. /// <param name="modifiedRanges">Optional array where the modified ranges should be written</param>
  69. /// <returns>The number of modified ranges</returns>
  70. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  71. public int QueryModified(ulong address, ulong size, ResourceName name, (ulong, ulong)[] modifiedRanges = null)
  72. {
  73. return _cpuMemory.QueryModified(address, size, (int)name, modifiedRanges);
  74. }
  75. }
  76. }