using Ryujinx.Cpu;
using Ryujinx.Cpu.Tracking;
using Ryujinx.Graphics.Gpu.Image;
using Ryujinx.Graphics.Gpu.Shader;
using Ryujinx.Memory;
using Ryujinx.Memory.Range;
using Ryujinx.Memory.Tracking;
using System;
using System.Collections.Generic;
using System.Threading;
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 : IDisposable
{
private readonly GpuContext _context;
private IVirtualMemoryManagerTracked _cpuMemory;
private int _referenceCount;
///
/// In-memory shader cache.
///
public ShaderCache ShaderCache { get; }
///
/// GPU buffer manager.
///
public BufferCache BufferCache { get; }
///
/// GPU texture manager.
///
public TextureCache TextureCache { get; }
///
/// Creates a new instance of the physical memory.
///
/// GPU context that the physical memory belongs to
/// CPU memory manager of the application process
public PhysicalMemory(GpuContext context, IVirtualMemoryManagerTracked cpuMemory)
{
_context = context;
_cpuMemory = cpuMemory;
ShaderCache = new ShaderCache(context);
BufferCache = new BufferCache(context, this);
TextureCache = new TextureCache(context, this);
if (cpuMemory is IRefCounted rc)
{
rc.IncrementReferenceCount();
}
_referenceCount = 1;
}
///
/// Increments the memory reference count.
///
public void IncrementReferenceCount()
{
Interlocked.Increment(ref _referenceCount);
}
///
/// Decrements the memory reference count.
///
public void DecrementReferenceCount()
{
if (Interlocked.Decrement(ref _referenceCount) == 0 && _cpuMemory is IRefCounted rc)
{
rc.DecrementReferenceCount();
}
}
///
/// 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 span of data from the application process.
///
/// Ranges of physical memory where the data is located
/// True if read tracking is triggered on the span
/// A read only span of the data at the specified memory location
public ReadOnlySpan GetSpan(MultiRange range, bool tracked = false)
{
if (range.Count == 1)
{
var singleRange = range.GetSubRange(0);
if (singleRange.Address != MemoryManager.PteUnmapped)
{
return _cpuMemory.GetSpan(singleRange.Address, (int)singleRange.Size, tracked);
}
}
Span data = new byte[range.GetSize()];
int offset = 0;
for (int i = 0; i < range.Count; i++)
{
var currentRange = range.GetSubRange(i);
int size = (int)currentRange.Size;
if (currentRange.Address != MemoryManager.PteUnmapped)
{
_cpuMemory.GetSpan(currentRange.Address, size, tracked).CopyTo(data.Slice(offset, size));
}
offset += size;
}
return data;
}
///
/// Gets a writable region from the application process.
///
/// Start address of the range
/// Size in bytes to be range
/// True if write tracking is triggered on the span
/// A writable region with the data at the specified memory location
public WritableRegion GetWritableRegion(ulong address, int size, bool tracked = false)
{
return _cpuMemory.GetWritableRegion(address, size, tracked);
}
///
/// Gets a writable region from GPU mapped memory.
///
/// Range
/// True if write tracking is triggered on the span
/// A writable region with the data at the specified memory location
public WritableRegion GetWritableRegion(MultiRange range, bool tracked = false)
{
if (range.Count == 1)
{
MemoryRange subrange = range.GetSubRange(0);
return GetWritableRegion(subrange.Address, (int)subrange.Size, tracked);
}
else
{
Memory memory = new byte[range.GetSize()];
int offset = 0;
for (int i = 0; i < range.Count; i++)
{
var currentRange = range.GetSubRange(i);
int size = (int)currentRange.Size;
if (currentRange.Address != MemoryManager.PteUnmapped)
{
GetSpan(currentRange.Address, size).CopyTo(memory.Span.Slice(offset, size));
}
offset += size;
}
return new WritableRegion(new MultiRangeWritableBlock(range, this), 0, memory, tracked);
}
}
///
/// 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 _cpuMemory.Read(address);
}
///
/// Reads data from the application process, with write tracking.
///
/// Type of the structure
/// Address to read from
/// The data at the specified memory location
public T ReadTracked(ulong address) where T : unmanaged
{
return _cpuMemory.ReadTracked(address);
}
///
/// Writes data to the application process, triggering a precise memory tracking event.
///
/// Address to write into
/// Data to be written
public void WriteTrackedResource(ulong address, ReadOnlySpan data)
{
_cpuMemory.SignalMemoryTracking(address, (ulong)data.Length, true, precise: true);
_cpuMemory.WriteUntracked(address, data);
}
///
/// 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.
///
/// Ranges of physical memory where the data is located
/// Data to be written
public void Write(MultiRange range, ReadOnlySpan data)
{
WriteImpl(range, data, _cpuMemory.Write);
}
///
/// 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);
}
///
/// Writes data to the application process, without any tracking.
///
/// Ranges of physical memory where the data is located
/// Data to be written
public void WriteUntracked(MultiRange range, ReadOnlySpan data)
{
WriteImpl(range, data, _cpuMemory.WriteUntracked);
}
private delegate void WriteCallback(ulong address, ReadOnlySpan data);
///
/// Writes data to the application process, using the supplied callback method.
///
/// Ranges of physical memory where the data is located
/// Data to be written
/// Callback method that will perform the write
private static void WriteImpl(MultiRange range, ReadOnlySpan data, WriteCallback writeCallback)
{
if (range.Count == 1)
{
var singleRange = range.GetSubRange(0);
if (singleRange.Address != MemoryManager.PteUnmapped)
{
writeCallback(singleRange.Address, data);
}
}
else
{
int offset = 0;
for (int i = 0; i < range.Count; i++)
{
var currentRange = range.GetSubRange(i);
int size = (int)currentRange.Size;
if (currentRange.Address != MemoryManager.PteUnmapped)
{
writeCallback(currentRange.Address, data.Slice(offset, size));
}
offset += size;
}
}
}
///
/// 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. This should be disposed when finished with.
///
/// Ranges of physical memory where the data is located
/// The memory tracking handle
public GpuRegionHandle BeginTracking(MultiRange range)
{
var cpuRegionHandles = new CpuRegionHandle[range.Count];
int count = 0;
for (int i = 0; i < range.Count; i++)
{
var currentRange = range.GetSubRange(i);
if (currentRange.Address != MemoryManager.PteUnmapped)
{
cpuRegionHandles[count++] = _cpuMemory.BeginTracking(currentRange.Address, currentRange.Size);
}
}
if (count != range.Count)
{
Array.Resize(ref cpuRegionHandles, count);
}
return new GpuRegionHandle(cpuRegionHandles);
}
///
/// 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
/// Handles to inherit state from or reuse
/// Desired granularity of write tracking
/// The memory tracking handle
public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable handles = null, ulong granularity = 4096)
{
return _cpuMemory.BeginGranularTracking(address, size, handles, 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);
}
///
/// Checks if the page at a given address is mapped on CPU memory.
///
/// CPU virtual address of the page to check
/// True if mapped, false otherwise
public bool IsMapped(ulong address)
{
return _cpuMemory.IsMapped(address);
}
///
/// Release our reference to the CPU memory manager.
///
public void Dispose()
{
_context.DeferredActions.Enqueue(Destroy);
}
///
/// Performs disposal of the host GPU caches with resources mapped on this physical memory.
/// This must only be called from the render thread.
///
private void Destroy()
{
ShaderCache.Dispose();
BufferCache.Dispose();
TextureCache.Dispose();
DecrementReferenceCount();
}
}
}