using Ryujinx.Cpu; using Ryujinx.Cpu.Tracking; using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Ryujinx.Graphics.Gpu.Memory { /// /// Represents physical memory, accessible from the GPU. /// This is actually working CPU virtual addresses, of memory mapped on the application process. /// class PhysicalMemory { public const int PageSize = Cpu.MemoryManager.PageSize; private readonly Cpu.MemoryManager _cpuMemory; /// /// Creates a new instance of the physical memory. /// /// CPU memory manager of the application process public PhysicalMemory(Cpu.MemoryManager cpuMemory) { _cpuMemory = cpuMemory; } /// /// Gets a span of data from the application process. /// /// Start address of the range /// Size in bytes to be range /// True if read tracking is triggered on the span /// A read only span of the data at the specified memory location public ReadOnlySpan GetSpan(ulong address, int size, bool tracked = false) { return _cpuMemory.GetSpan(address, size, tracked); } /// /// Gets a writable region from the application process. /// /// Start address of the range /// Size in bytes to be range /// A writable region with the data at the specified memory location public WritableRegion GetWritableRegion(ulong address, int size) { return _cpuMemory.GetWritableRegion(address, size); } /// /// Reads data from the application process. /// /// Type of the structure /// Address to read from /// The data at the specified memory location public T Read(ulong address) where T : unmanaged { return MemoryMarshal.Cast(GetSpan(address, Unsafe.SizeOf()))[0]; } /// /// Writes data to the application process. /// /// Address to write into /// Data to be written public void Write(ulong address, ReadOnlySpan data) { _cpuMemory.Write(address, data); } /// /// Writes data to the application process, without any tracking. /// /// Address to write into /// Data to be written public void WriteUntracked(ulong address, ReadOnlySpan data) { _cpuMemory.WriteUntracked(address, data); } /// /// Obtains a memory tracking handle for the given virtual region. This should be disposed when finished with. /// /// CPU virtual address of the region /// Size of the region /// The memory tracking handle public CpuRegionHandle BeginTracking(ulong address, ulong size) { return _cpuMemory.BeginTracking(address, size); } /// /// Obtains a memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with. /// /// CPU virtual address of the region /// Size of the region /// Desired granularity of write tracking /// The memory tracking handle public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, ulong granularity = 4096) { return _cpuMemory.BeginGranularTracking(address, size, granularity); } /// /// Obtains a smart memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with. /// /// CPU virtual address of the region /// Size of the region /// Desired granularity of write tracking /// The memory tracking handle public CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity = 4096) { return _cpuMemory.BeginSmartGranularTracking(address, size, granularity); } } }