2019-10-13 08:02:07 +02:00
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Memory
|
|
|
|
{
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// GPU mapped memory accessor.
|
|
|
|
/// </summary>
|
2020-01-01 01:08:02 +01:00
|
|
|
public class MemoryAccessor
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
private GpuContext _context;
|
|
|
|
|
2019-12-31 04:22:58 +01: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 08:02:07 +02:00
|
|
|
public MemoryAccessor(GpuContext context)
|
|
|
|
{
|
|
|
|
_context = context;
|
|
|
|
}
|
|
|
|
|
2020-01-01 01:08:02 +01: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>
|
|
|
|
public byte[] ReadBytes(ulong gpuVa, ulong size)
|
|
|
|
{
|
2020-01-13 00:27:50 +01:00
|
|
|
return GetSpan(gpuVa, size).ToArray();
|
2020-01-01 01:08:02 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
2020-01-13 00:27:50 +01:00
|
|
|
/// Gets a read-only span of data from GPU mapped memory.
|
2019-12-31 04:22:58 +01: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>
|
|
|
|
/// <param name="maxSize">Maximum size of the data</param>
|
2020-01-13 00:27:50 +01:00
|
|
|
/// <returns>The span of the data at the specified memory location</returns>
|
|
|
|
public ReadOnlySpan<byte> GetSpan(ulong gpuVa, ulong maxSize)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
ulong processVa = _context.MemoryManager.Translate(gpuVa);
|
|
|
|
|
2020-02-03 20:11:22 +01:00
|
|
|
ulong size = _context.MemoryManager.GetSubSize(gpuVa, maxSize);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-01-13 00:27:50 +01:00
|
|
|
return _context.PhysicalMemory.GetSpan(processVa, size);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Reads a structure from GPU mapped memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">Type of the structure</typeparam>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <param name="gpuVa">GPU virtual address where the structure is located</param>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <returns>The structure at the specified memory location</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
public T Read<T>(ulong gpuVa) where T : struct
|
|
|
|
{
|
|
|
|
ulong processVa = _context.MemoryManager.Translate(gpuVa);
|
|
|
|
|
|
|
|
ulong size = (uint)Marshal.SizeOf<T>();
|
|
|
|
|
2020-01-13 00:27:50 +01:00
|
|
|
return MemoryMarshal.Cast<byte, T>(_context.PhysicalMemory.GetSpan(processVa, size))[0];
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Reads a 32-bits signed integer from GPU mapped memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="gpuVa">GPU virtual address where the value is located</param>
|
|
|
|
/// <returns>The value at the specified memory location</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
public int ReadInt32(ulong gpuVa)
|
|
|
|
{
|
|
|
|
ulong processVa = _context.MemoryManager.Translate(gpuVa);
|
|
|
|
|
2020-01-13 00:27:50 +01:00
|
|
|
return BitConverter.ToInt32(_context.PhysicalMemory.GetSpan(processVa, 4));
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2020-01-01 01:08:02 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Reads a 64-bits unsigned integer from GPU mapped memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="gpuVa">GPU virtual address where the value is located</param>
|
|
|
|
/// <returns>The value at the specified memory location</returns>
|
|
|
|
public ulong ReadUInt64(ulong gpuVa)
|
|
|
|
{
|
|
|
|
ulong processVa = _context.MemoryManager.Translate(gpuVa);
|
|
|
|
|
2020-01-13 00:27:50 +01:00
|
|
|
return BitConverter.ToUInt64(_context.PhysicalMemory.GetSpan(processVa, 8));
|
2020-01-01 01:08:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Reads a 8-bits unsigned integer from GPU mapped memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="gpuVa">GPU virtual address where the value is located</param>
|
|
|
|
/// <param name="value">The value to be written</param>
|
|
|
|
public void WriteByte(ulong gpuVa, byte value)
|
|
|
|
{
|
|
|
|
ulong processVa = _context.MemoryManager.Translate(gpuVa);
|
|
|
|
|
|
|
|
_context.PhysicalMemory.Write(processVa, MemoryMarshal.CreateSpan(ref value, 1));
|
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01: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>
|
2019-10-13 08:02:07 +02:00
|
|
|
public void Write(ulong gpuVa, int value)
|
|
|
|
{
|
|
|
|
ulong processVa = _context.MemoryManager.Translate(gpuVa);
|
|
|
|
|
|
|
|
_context.PhysicalMemory.Write(processVa, BitConverter.GetBytes(value));
|
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01: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>
|
2019-10-13 08:02:07 +02:00
|
|
|
public void Write(ulong gpuVa, Span<byte> data)
|
|
|
|
{
|
|
|
|
ulong processVa = _context.MemoryManager.Translate(gpuVa);
|
|
|
|
|
|
|
|
_context.PhysicalMemory.Write(processVa, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|