using Ryujinx.Common;
using Ryujinx.Graphics.Gpu.Memory;
using System;
namespace Ryujinx.Graphics.Gpu.Image
{
///
/// Represents a pool of GPU resources, such as samplers or textures.
///
/// Type of the GPU resource
abstract class Pool : IDisposable
{
protected const int DescriptorSize = 0x20;
protected GpuContext Context;
protected T[] Items;
///
/// 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 (ulong, ulong)[] _modifiedRanges;
public Pool(GpuContext context, ulong address, int maximumId)
{
Context = context;
MaximumId = maximumId;
int count = maximumId + 1;
ulong size = (ulong)(uint)count * DescriptorSize;;
Items = new T[count];
Address = address;
Size = size;
ulong endAddress = BitUtils.AlignUp(Address + Size, PhysicalMemory.PageSize);
ulong pagesCount = (endAddress - BitUtils.AlignDown(Address, PhysicalMemory.PageSize)) / PhysicalMemory.PageSize;
_modifiedRanges = new (ulong, ulong)[pagesCount];
}
///
/// 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 T Get(int id);
///
/// 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()
{
int count = Context.PhysicalMemory.QueryModified(Address, Size, ResourceName.TexturePool, _modifiedRanges);
for (int index = 0; index < count; index++)
{
(ulong mAddress, ulong mSize) = _modifiedRanges[index];
if (mAddress < Address)
{
mAddress = Address;
}
ulong maxSize = Address + Size - mAddress;
if (mSize > maxSize)
{
mSize = maxSize;
}
InvalidateRangeImpl(mAddress, mSize);
}
}
private void InvalidateRangeInternal(ulong offset, int size)
{
InvalidateRangeImpl(Address + offset, (ulong)size);
}
///
/// Invalidates a range of memory of the GPU resource pool.
/// Entries that falls inside the speicified range will be invalidated,
/// causing all the data to be reloaded from guest memory.
///
/// The start address of the range to invalidate
/// The size of the range to invalidate
public void InvalidateRange(ulong address, ulong size)
{
ulong endAddress = address + size;
ulong texturePoolEndAddress = Address + Size;
// If the range being invalidated is not overlapping the texture pool range,
// then we don't have anything to do, exit early.
if (address >= texturePoolEndAddress || endAddress <= Address)
{
return;
}
if (address < Address)
{
address = Address;
}
if (endAddress > texturePoolEndAddress)
{
endAddress = texturePoolEndAddress;
}
size = endAddress - address;
InvalidateRangeImpl(address, size);
}
protected abstract void InvalidateRangeImpl(ulong address, ulong size);
protected abstract void Delete(T item);
///
/// Performs the disposal of all resources stored on the pool.
/// It's an error to try using the pool after disposal.
///
public void Dispose()
{
if (Items != null)
{
for (int index = 0; index < Items.Length; index++)
{
Delete(Items[index]);
}
Items = null;
}
}
}
}