using Ryujinx.Graphics.Gpu.Memory;
using Ryujinx.Memory.Tracking;
using System;
using System.Runtime.InteropServices;
namespace Ryujinx.Graphics.Gpu.Image
{
///
/// Represents a pool of GPU resources, such as samplers or textures.
///
/// Type of the GPU resource
/// Type of the descriptor
abstract class Pool : IDisposable where T2 : unmanaged
{
protected const int DescriptorSize = 0x20;
protected GpuContext Context;
protected PhysicalMemory PhysicalMemory;
protected int SequenceNumber;
protected int ModifiedSequenceNumber;
protected T1[] Items;
protected T2[] DescriptorCache;
///
/// The maximum ID value of resources on the pool (inclusive).
///
///
/// The maximum amount of resources on the pool is equal to this value plus one.
///
public int MaximumId { get; }
///
/// The address of the pool in guest memory.
///
public ulong Address { get; }
///
/// The size of the pool in bytes.
///
public ulong Size { get; }
private readonly MultiRegionHandle _memoryTracking;
private readonly Action _modifiedDelegate;
private int _modifiedSequenceOffset;
private bool _modified;
///
/// Creates a new instance of the GPU resource pool.
///
/// GPU context that the pool belongs to
/// Physical memory where the resource descriptors are mapped
/// Address of the pool in physical memory
/// Maximum index of an item on the pool (inclusive)
public Pool(GpuContext context, PhysicalMemory physicalMemory, ulong address, int maximumId)
{
Context = context;
PhysicalMemory = physicalMemory;
MaximumId = maximumId;
int count = maximumId + 1;
ulong size = (ulong)(uint)count * DescriptorSize;
Items = new T1[count];
DescriptorCache = new T2[count];
Address = address;
Size = size;
_memoryTracking = physicalMemory.BeginGranularTracking(address, size, ResourceKind.Pool, RegionFlags.None);
_memoryTracking.RegisterPreciseAction(address, size, PreciseAction);
_modifiedDelegate = RegionModified;
}
///
/// Gets the descriptor for a given ID.
///
/// ID of the descriptor. This is effectively a zero-based index
/// The descriptor
public T2 GetDescriptor(int id)
{
return PhysicalMemory.Read(Address + (ulong)id * DescriptorSize);
}
///
/// Gets a reference to the descriptor for a given ID.
///
/// ID of the descriptor. This is effectively a zero-based index
/// A reference to the descriptor
public ref readonly T2 GetDescriptorRef(int id)
{
return ref GetDescriptorRefAddress(Address + (ulong)id * DescriptorSize);
}
///
/// Gets a reference to the descriptor for a given address.
///
/// Address of the descriptor
/// A reference to the descriptor
public ref readonly T2 GetDescriptorRefAddress(ulong address)
{
return ref MemoryMarshal.Cast(PhysicalMemory.GetSpan(address, DescriptorSize))[0];
}
///
/// Gets the GPU resource with the given ID.
///
/// ID of the resource. This is effectively a zero-based index
/// The GPU resource with the given ID
public abstract T1 Get(int id);
///
/// Checks if a given ID is valid and inside the range of the pool.
///
/// ID of the descriptor. This is effectively a zero-based index
/// True if the specified ID is valid, false otherwise
public bool IsValidId(int id)
{
return (uint)id <= MaximumId;
}
///
/// Synchronizes host memory with guest memory.
/// This causes invalidation of pool entries,
/// if a modification of entries by the CPU is detected.
///
public void SynchronizeMemory()
{
_modified = false;
_memoryTracking.QueryModified(_modifiedDelegate);
if (_modified)
{
UpdateModifiedSequence();
}
}
///
/// Indicate that a region of the pool was modified, and must be loaded from memory.
///
/// Start address of the modified region
/// Size of the modified region
private void RegionModified(ulong mAddress, ulong mSize)
{
_modified = true;
if (mAddress < Address)
{
mAddress = Address;
}
ulong maxSize = Address + Size - mAddress;
if (mSize > maxSize)
{
mSize = maxSize;
}
InvalidateRangeImpl(mAddress, mSize);
}
///
/// Updates the modified sequence number using the current sequence number and offset,
/// indicating that it has been modified.
///
protected void UpdateModifiedSequence()
{
ModifiedSequenceNumber = SequenceNumber + _modifiedSequenceOffset;
}
///
/// An action to be performed when a precise memory access occurs to this resource.
/// Makes sure that the dirty flags are checked.
///
/// Address of the memory action
/// Size in bytes
/// True if the access was a write, false otherwise
private bool PreciseAction(ulong address, ulong size, bool write)
{
if (write && Context.SequenceNumber == SequenceNumber)
{
if (ModifiedSequenceNumber == SequenceNumber + _modifiedSequenceOffset)
{
// The modified sequence number is offset when PreciseActions occur so that
// users checking it will see an increment and know the pool has changed since
// their last look, even though the main SequenceNumber has not been changed.
_modifiedSequenceOffset++;
}
// Force the pool to be checked again the next time it is used.
SequenceNumber--;
}
return false;
}
protected abstract void InvalidateRangeImpl(ulong address, ulong size);
protected abstract void Delete(T1 item);
///
/// Performs the disposal of all resources stored on the pool.
/// It's an error to try using the pool after disposal.
///
public virtual void Dispose()
{
if (Items != null)
{
for (int index = 0; index < Items.Length; index++)
{
Delete(Items[index]);
}
Items = null;
}
_memoryTracking.Dispose();
}
}
}