2020-07-11 20:07:01 -07:00
|
|
|
using Ryujinx.Cpu;
|
2019-12-25 15:28:17 -08:00
|
|
|
using System;
|
2020-05-03 15:54:50 -07:00
|
|
|
using System.Runtime.CompilerServices;
|
2020-05-27 07:07:10 -07:00
|
|
|
using System.Runtime.InteropServices;
|
2019-12-25 15:28:17 -08:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Memory
|
|
|
|
{
|
2019-12-30 19:22:58 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Represents physical memory, accessible from the GPU.
|
2019-12-30 20:46:57 -08:00
|
|
|
/// This is actually working CPU virtual addresses, of memory mapped on the application process.
|
2019-12-30 19:22:58 -08:00
|
|
|
/// </summary>
|
2019-12-25 15:28:17 -08:00
|
|
|
class PhysicalMemory
|
|
|
|
{
|
2020-05-03 15:54:50 -07:00
|
|
|
public const int PageSize = Cpu.MemoryManager.PageSize;
|
|
|
|
|
|
|
|
private readonly Cpu.MemoryManager _cpuMemory;
|
2019-12-25 15:28:17 -08:00
|
|
|
|
2019-12-30 19:22:58 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the physical memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="cpuMemory">CPU memory manager of the application process</param>
|
2020-05-03 15:54:50 -07:00
|
|
|
public PhysicalMemory(Cpu.MemoryManager cpuMemory)
|
2019-12-25 15:28:17 -08:00
|
|
|
{
|
|
|
|
_cpuMemory = cpuMemory;
|
|
|
|
}
|
|
|
|
|
2019-12-30 19:22:58 -08:00
|
|
|
/// <summary>
|
2020-01-12 15:27:50 -08:00
|
|
|
/// Gets a span of data from the application process.
|
2019-12-30 19:22:58 -08:00
|
|
|
/// </summary>
|
2020-01-12 15:27:50 -08:00
|
|
|
/// <param name="address">Start address of the range</param>
|
|
|
|
/// <param name="size">Size in bytes to be range</param>
|
|
|
|
/// <returns>A read only span of the data at the specified memory location</returns>
|
2020-05-05 18:02:28 -07:00
|
|
|
public ReadOnlySpan<byte> GetSpan(ulong address, int size)
|
2019-12-25 15:28:17 -08:00
|
|
|
{
|
2020-05-05 18:02:28 -07:00
|
|
|
return _cpuMemory.GetSpan(address, size);
|
2019-12-25 15:28:17 -08:00
|
|
|
}
|
|
|
|
|
2020-07-11 20:07:01 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a writable region from the application process.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="address">Start address of the range</param>
|
|
|
|
/// <param name="size">Size in bytes to be range</param>
|
|
|
|
/// <returns>A writable region with the data at the specified memory location</returns>
|
|
|
|
public WritableRegion GetWritableRegion(ulong address, int size)
|
|
|
|
{
|
|
|
|
return _cpuMemory.GetWritableRegion(address, size);
|
|
|
|
}
|
|
|
|
|
2020-05-27 07:07:10 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Reads data from the application process.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">Type of the structure</typeparam>
|
|
|
|
/// <param name="gpuVa">Address to read from</param>
|
|
|
|
/// <returns>The data at the specified memory location</returns>
|
|
|
|
public T Read<T>(ulong address) where T : unmanaged
|
|
|
|
{
|
|
|
|
return MemoryMarshal.Cast<byte, T>(GetSpan(address, Unsafe.SizeOf<T>()))[0];
|
|
|
|
}
|
|
|
|
|
2019-12-30 19:22:58 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Writes data to the application process.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="address">Address to write into</param>
|
|
|
|
/// <param name="data">Data to be written</param>
|
2020-01-12 15:27:50 -08:00
|
|
|
public void Write(ulong address, ReadOnlySpan<byte> data)
|
2019-12-25 15:28:17 -08:00
|
|
|
{
|
2020-05-03 15:54:50 -07:00
|
|
|
_cpuMemory.Write(address, data);
|
2019-12-25 15:28:17 -08:00
|
|
|
}
|
|
|
|
|
2019-12-30 19:22:58 -08:00
|
|
|
/// <summary>
|
2020-05-03 15:54:50 -07:00
|
|
|
/// Checks if a specified virtual memory region has been modified by the CPU since the last call.
|
2019-12-30 19:22:58 -08:00
|
|
|
/// </summary>
|
2020-05-03 15:54:50 -07:00
|
|
|
/// <param name="address">CPU virtual address of the region</param>
|
|
|
|
/// <param name="size">Size of the region</param>
|
|
|
|
/// <param name="name">Resource name</param>
|
|
|
|
/// <param name="modifiedRanges">Optional array where the modified ranges should be written</param>
|
|
|
|
/// <returns>The number of modified ranges</returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public int QueryModified(ulong address, ulong size, ResourceName name, (ulong, ulong)[] modifiedRanges = null)
|
2019-12-25 15:28:17 -08:00
|
|
|
{
|
2020-05-03 15:54:50 -07:00
|
|
|
return _cpuMemory.QueryModified(address, size, (int)name, modifiedRanges);
|
2019-12-25 15:28:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|