MemoryAccessor.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /// Writes a 32-bits signed integer to GPU mapped memory.
  55. /// </summary>
  56. /// <param name="gpuVa">GPU virtual address to write the value into</param>
  57. /// <param name="value">The value to be written</param>
  58. public void Write<T>(ulong gpuVa, T value) where T : unmanaged
  59. {
  60. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  61. _context.PhysicalMemory.Write(processVa, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
  62. }
  63. /// <summary>
  64. /// Writes data to GPU mapped memory.
  65. /// </summary>
  66. /// <param name="gpuVa">GPU virtual address to write the data into</param>
  67. /// <param name="data">The data to be written</param>
  68. public void Write(ulong gpuVa, ReadOnlySpan<byte> data)
  69. {
  70. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  71. _context.PhysicalMemory.Write(processVa, data);
  72. }
  73. }
  74. }