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