2019-10-13 02:02:07 -04:00
|
|
|
using System;
|
2020-05-05 21:02:28 -04:00
|
|
|
using System.Runtime.CompilerServices;
|
2019-10-13 02:02:07 -04:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Memory
|
|
|
|
{
|
2019-12-30 22:22:58 -05:00
|
|
|
/// <summary>
|
|
|
|
/// GPU mapped memory accessor.
|
|
|
|
/// </summary>
|
2019-12-31 19:08:02 -05:00
|
|
|
public class MemoryAccessor
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
private GpuContext _context;
|
|
|
|
|
2019-12-30 22:22:58 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the GPU memory accessor.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">GPU context that the memory accessor belongs to</param>
|
2019-10-13 02:02:07 -04:00
|
|
|
public MemoryAccessor(GpuContext context)
|
|
|
|
{
|
|
|
|
_context = context;
|
|
|
|
}
|
|
|
|
|
2019-12-31 19:08:02 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Reads a byte array from GPU mapped memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="gpuVa">GPU virtual address where the data is located</param>
|
|
|
|
/// <param name="size">Size of the data in bytes</param>
|
|
|
|
/// <returns>Byte array with the data</returns>
|
2020-05-05 21:02:28 -04:00
|
|
|
public byte[] ReadBytes(ulong gpuVa, int size)
|
2019-12-31 19:08:02 -05:00
|
|
|
{
|
2020-01-12 18:27:50 -05:00
|
|
|
return GetSpan(gpuVa, size).ToArray();
|
2019-12-31 19:08:02 -05:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:22:58 -05:00
|
|
|
/// <summary>
|
2020-01-12 18:27:50 -05:00
|
|
|
/// Gets a read-only span of data from GPU mapped memory.
|
2019-12-30 22:22:58 -05:00
|
|
|
/// This reads as much data as possible, up to the specified maximum size.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="gpuVa">GPU virtual address where the data is located</param>
|
2020-05-05 21:02:28 -04:00
|
|
|
/// <param name="size">Size of the data</param>
|
2020-01-12 18:27:50 -05:00
|
|
|
/// <returns>The span of the data at the specified memory location</returns>
|
2020-05-05 21:02:28 -04:00
|
|
|
public ReadOnlySpan<byte> GetSpan(ulong gpuVa, int size)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
ulong processVa = _context.MemoryManager.Translate(gpuVa);
|
|
|
|
|
2020-01-12 18:27:50 -05:00
|
|
|
return _context.PhysicalMemory.GetSpan(processVa, size);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:22:58 -05:00
|
|
|
/// <summary>
|
2020-05-27 10:07:10 -04:00
|
|
|
/// Reads data from GPU mapped memory.
|
2019-12-30 22:22:58 -05:00
|
|
|
/// </summary>
|
2020-05-27 10:07:10 -04:00
|
|
|
/// <typeparam name="T">Type of the data</typeparam>
|
|
|
|
/// <param name="gpuVa">GPU virtual address where the data is located</param>
|
|
|
|
/// <returns>The data at the specified memory location</returns>
|
2020-05-05 21:02:28 -04:00
|
|
|
public T Read<T>(ulong gpuVa) where T : unmanaged
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
ulong processVa = _context.MemoryManager.Translate(gpuVa);
|
|
|
|
|
2020-05-05 21:02:28 -04:00
|
|
|
return MemoryMarshal.Cast<byte, T>(_context.PhysicalMemory.GetSpan(processVa, Unsafe.SizeOf<T>()))[0];
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:22:58 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Writes a 32-bits signed integer to GPU mapped memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="gpuVa">GPU virtual address to write the value into</param>
|
|
|
|
/// <param name="value">The value to be written</param>
|
2020-07-14 23:01:10 -04:00
|
|
|
public void Write<T>(ulong gpuVa, T value) where T : unmanaged
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
ulong processVa = _context.MemoryManager.Translate(gpuVa);
|
|
|
|
|
2020-07-14 23:01:10 -04:00
|
|
|
_context.PhysicalMemory.Write(processVa, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:22:58 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Writes data to GPU mapped memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="gpuVa">GPU virtual address to write the data into</param>
|
|
|
|
/// <param name="data">The data to be written</param>
|
2020-05-05 21:02:28 -04:00
|
|
|
public void Write(ulong gpuVa, ReadOnlySpan<byte> data)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
ulong processVa = _context.MemoryManager.Translate(gpuVa);
|
|
|
|
|
|
|
|
_context.PhysicalMemory.Write(processVa, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|