MemoryAccessor.cs 4.5 KB

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