2019-10-26 19:50:52 +02:00
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
2019-10-13 08:02:07 +02:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Image
|
|
|
|
{
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Represents a pool of GPU resources, such as samplers or textures.
|
|
|
|
/// </summary>
|
2020-01-02 00:14:18 +01:00
|
|
|
/// <typeparam name="T">Type of the GPU resource</typeparam>
|
2019-10-13 08:02:07 +02:00
|
|
|
abstract class Pool<T> : IDisposable
|
|
|
|
{
|
|
|
|
protected const int DescriptorSize = 0x20;
|
|
|
|
|
|
|
|
protected GpuContext Context;
|
|
|
|
|
|
|
|
protected T[] Items;
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// The maximum ID value of resources on the pool (inclusive).
|
|
|
|
/// </summary>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <remarks>
|
|
|
|
/// The maximum amount of resources on the pool is equal to this value plus one.
|
|
|
|
/// </remarks>
|
2019-12-07 00:19:12 +01:00
|
|
|
public int MaximumId { get; }
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// The address of the pool in guest memory.
|
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
public ulong Address { get; }
|
2019-12-30 00:26:37 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The size of the pool in bytes.
|
|
|
|
/// </summary>
|
|
|
|
public ulong Size { get; }
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
public Pool(GpuContext context, ulong address, int maximumId)
|
|
|
|
{
|
2019-12-07 00:19:12 +01:00
|
|
|
Context = context;
|
|
|
|
MaximumId = maximumId;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
int count = maximumId + 1;
|
|
|
|
|
|
|
|
ulong size = (ulong)(uint)count * DescriptorSize;;
|
|
|
|
|
|
|
|
Items = new T[count];
|
|
|
|
|
|
|
|
Address = address;
|
|
|
|
Size = size;
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the GPU resource with the given ID.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">ID of the resource. This is effectively a zero-based index</param>
|
|
|
|
/// <returns>The GPU resource with the given ID</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
public abstract T Get(int id);
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Synchronizes host memory with guest memory.
|
2020-01-02 00:14:18 +01:00
|
|
|
/// This causes invalidation of pool entries,
|
2019-12-30 00:26:37 +01:00
|
|
|
/// if a modification of entries by the CPU is detected.
|
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
public void SynchronizeMemory()
|
|
|
|
{
|
2019-10-26 19:50:52 +02:00
|
|
|
(ulong, ulong)[] modifiedRanges = Context.PhysicalMemory.GetModifiedRanges(Address, Size, ResourceName.TexturePool);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
for (int index = 0; index < modifiedRanges.Length; 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// 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.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="address">The start address of the range to invalidate</param>
|
|
|
|
/// <param name="size">The size of the range to invalidate</param>
|
2019-10-13 08:02:07 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
size = endAddress - address;
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
InvalidateRangeImpl(address, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract void InvalidateRangeImpl(ulong address, ulong size);
|
|
|
|
|
|
|
|
protected abstract void Delete(T item);
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Performs the disposal of all resources stored on the pool.
|
|
|
|
/// It's an error to try using the pool after disposal.
|
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (Items != null)
|
|
|
|
{
|
|
|
|
for (int index = 0; index < Items.Length; index++)
|
|
|
|
{
|
|
|
|
Delete(Items[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Items = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|