MemoryAccessor.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. 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 data from GPU mapped memory.
  21. /// This reads as much data as possible, up to the specified maximum size.
  22. /// </summary>
  23. /// <param name="gpuVa">GPU virtual address where the data is located</param>
  24. /// <param name="maxSize">Maximum size of the data</param>
  25. /// <returns>The data at the specified memory location</returns>
  26. public Span<byte> Read(ulong gpuVa, ulong maxSize)
  27. {
  28. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  29. ulong size = Math.Min(_context.MemoryManager.GetSubSize(gpuVa), maxSize);
  30. return _context.PhysicalMemory.Read(processVa, size);
  31. }
  32. /// <summary>
  33. /// Reads a structure from GPU mapped memory.
  34. /// </summary>
  35. /// <typeparam name="T">Type of the structure</typeparam>
  36. /// <param name="gpuVa">GPU virtual address where the strcture is located</param>
  37. /// <returns>The structure at the specified memory location</returns>
  38. public T Read<T>(ulong gpuVa) where T : struct
  39. {
  40. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  41. ulong size = (uint)Marshal.SizeOf<T>();
  42. return MemoryMarshal.Cast<byte, T>(_context.PhysicalMemory.Read(processVa, size))[0];
  43. }
  44. /// <summary>
  45. /// Reads a 32-bits signed integer from GPU mapped memory.
  46. /// </summary>
  47. /// <param name="gpuVa">GPU virtual address where the value is located</param>
  48. /// <returns>The value at the specified memory location</returns>
  49. public int ReadInt32(ulong gpuVa)
  50. {
  51. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  52. return BitConverter.ToInt32(_context.PhysicalMemory.Read(processVa, 4));
  53. }
  54. /// <summary>
  55. /// Writes a 32-bits signed integer to GPU mapped memory.
  56. /// </summary>
  57. /// <param name="gpuVa">GPU virtual address to write the value into</param>
  58. /// <param name="value">The value to be written</param>
  59. public void Write(ulong gpuVa, int value)
  60. {
  61. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  62. _context.PhysicalMemory.Write(processVa, BitConverter.GetBytes(value));
  63. }
  64. /// <summary>
  65. /// Writes data to GPU mapped memory.
  66. /// </summary>
  67. /// <param name="gpuVa">GPU virtual address to write the data into</param>
  68. /// <param name="data">The data to be written</param>
  69. public void Write(ulong gpuVa, Span<byte> data)
  70. {
  71. ulong processVa = _context.MemoryManager.Translate(gpuVa);
  72. _context.PhysicalMemory.Write(processVa, data);
  73. }
  74. }
  75. }