MemoryAccessor.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Ryujinx.Graphics.Gpu.Memory
  4. {
  5. /// <summary>
  6. /// GPU mapped memory accessor.
  7. /// </summary>
  8. public class MemoryAccessor
  9. {
  10. private GpuContext _context;
  11. /// <summary>
  12. /// Creates a new instance of the GPU memory accessor.
  13. /// </summary>
  14. /// <param name="context">GPU context that the memory accessor belongs to</param>
  15. public MemoryAccessor(GpuContext context)
  16. {
  17. _context = context;
  18. }
  19. /// <summary>
  20. /// Reads a byte array from GPU mapped memory.
  21. /// </summary>
  22. /// <param name="gpuVa">GPU virtual address where the data is located</param>
  23. /// <param name="size">Size of the data in bytes</param>
  24. /// <returns>Byte array with the data</returns>
  25. public byte[] ReadBytes(ulong gpuVa, ulong size)
  26. {
  27. return GetSpan(gpuVa, size).ToArray();
  28. }
  29. /// <summary>
  30. /// Gets a read-only span of data from GPU mapped memory.
  31. /// This reads as much data as possible, up to the specified maximum size.
  32. /// </summary>
  33. /// <param name="gpuVa">GPU virtual address where the data is located</param>
  34. /// <param name="maxSize">Maximum size of the data</param>
  35. /// <returns>The span of the data at the specified memory location</returns>
  36. public ReadOnlySpan<byte> GetSpan(ulong gpuVa, ulong maxSize)
  37. {
  38. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  39. ulong size = Math.Min(_context.MemoryManager.GetSubSize(gpuVa), maxSize);
  40. return _context.PhysicalMemory.GetSpan(processVa, size);
  41. }
  42. /// <summary>
  43. /// Reads a structure from GPU mapped memory.
  44. /// </summary>
  45. /// <typeparam name="T">Type of the structure</typeparam>
  46. /// <param name="gpuVa">GPU virtual address where the structure is located</param>
  47. /// <returns>The structure at the specified memory location</returns>
  48. public T Read<T>(ulong gpuVa) where T : struct
  49. {
  50. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  51. ulong size = (uint)Marshal.SizeOf<T>();
  52. return MemoryMarshal.Cast<byte, T>(_context.PhysicalMemory.GetSpan(processVa, size))[0];
  53. }
  54. /// <summary>
  55. /// Reads a 32-bits signed integer from GPU mapped memory.
  56. /// </summary>
  57. /// <param name="gpuVa">GPU virtual address where the value is located</param>
  58. /// <returns>The value at the specified memory location</returns>
  59. public int ReadInt32(ulong gpuVa)
  60. {
  61. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  62. return BitConverter.ToInt32(_context.PhysicalMemory.GetSpan(processVa, 4));
  63. }
  64. /// <summary>
  65. /// Reads a 64-bits unsigned integer from GPU mapped memory.
  66. /// </summary>
  67. /// <param name="gpuVa">GPU virtual address where the value is located</param>
  68. /// <returns>The value at the specified memory location</returns>
  69. public ulong ReadUInt64(ulong gpuVa)
  70. {
  71. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  72. return BitConverter.ToUInt64(_context.PhysicalMemory.GetSpan(processVa, 8));
  73. }
  74. /// <summary>
  75. /// Reads a 8-bits unsigned integer from GPU mapped memory.
  76. /// </summary>
  77. /// <param name="gpuVa">GPU virtual address where the value is located</param>
  78. /// <param name="value">The value to be written</param>
  79. public void WriteByte(ulong gpuVa, byte value)
  80. {
  81. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  82. _context.PhysicalMemory.Write(processVa, MemoryMarshal.CreateSpan(ref value, 1));
  83. }
  84. /// <summary>
  85. /// Writes a 32-bits signed integer to GPU mapped memory.
  86. /// </summary>
  87. /// <param name="gpuVa">GPU virtual address to write the value into</param>
  88. /// <param name="value">The value to be written</param>
  89. public void Write(ulong gpuVa, int value)
  90. {
  91. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  92. _context.PhysicalMemory.Write(processVa, BitConverter.GetBytes(value));
  93. }
  94. /// <summary>
  95. /// Writes data to GPU mapped memory.
  96. /// </summary>
  97. /// <param name="gpuVa">GPU virtual address to write the data into</param>
  98. /// <param name="data">The data to be written</param>
  99. public void Write(ulong gpuVa, Span<byte> data)
  100. {
  101. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  102. _context.PhysicalMemory.Write(processVa, data);
  103. }
  104. }
  105. }