2020-07-12 05:07:01 +02:00
|
|
|
using Ryujinx.Cpu;
|
2019-12-26 00:28:17 +01:00
|
|
|
using System;
|
2020-05-04 00:54:50 +02:00
|
|
|
using System.Runtime.CompilerServices;
|
2020-05-27 16:07:10 +02:00
|
|
|
using System.Runtime.InteropServices;
|
2019-12-26 00:28:17 +01:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Memory
|
|
|
|
{
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Represents physical memory, accessible from the GPU.
|
2019-12-31 05:46:57 +01:00
|
|
|
/// This is actually working CPU virtual addresses, of memory mapped on the application process.
|
2019-12-31 04:22:58 +01:00
|
|
|
/// </summary>
|
2019-12-26 00:28:17 +01:00
|
|
|
class PhysicalMemory
|
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
public const int PageSize = Cpu.MemoryManager.PageSize;
|
|
|
|
|
|
|
|
private readonly Cpu.MemoryManager _cpuMemory;
|
2019-12-26 00:28:17 +01:00
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the physical memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="cpuMemory">CPU memory manager of the application process</param>
|
2020-05-04 00:54:50 +02:00
|
|
|
public PhysicalMemory(Cpu.MemoryManager cpuMemory)
|
2019-12-26 00:28:17 +01:00
|
|
|
{
|
|
|
|
_cpuMemory = cpuMemory;
|
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
2020-01-13 00:27:50 +01:00
|
|
|
/// Gets a span of data from the application process.
|
2019-12-31 04:22:58 +01:00
|
|
|
/// </summary>
|
2020-01-13 00:27:50 +01: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-06 03:02:28 +02:00
|
|
|
public ReadOnlySpan<byte> GetSpan(ulong address, int size)
|
2019-12-26 00:28:17 +01:00
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
return _cpuMemory.GetSpan(address, size);
|
2019-12-26 00:28:17 +01:00
|
|
|
}
|
|
|
|
|
2020-07-12 05:07:01 +02: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 16:07:10 +02: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-31 04:22:58 +01: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-13 00:27:50 +01:00
|
|
|
public void Write(ulong address, ReadOnlySpan<byte> data)
|
2019-12-26 00:28:17 +01:00
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
_cpuMemory.Write(address, data);
|
2019-12-26 00:28:17 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
2020-05-04 00:54:50 +02:00
|
|
|
/// Checks if a specified virtual memory region has been modified by the CPU since the last call.
|
2019-12-31 04:22:58 +01:00
|
|
|
/// </summary>
|
2020-05-04 00:54:50 +02: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-26 00:28:17 +01:00
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
return _cpuMemory.QueryModified(address, size, (int)name, modifiedRanges);
|
2019-12-26 00:28:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|