Somewhat better caching system

This commit is contained in:
Gabriel A 2024-03-25 11:54:18 -03:00
parent bec99d00b1
commit c8a19501ee

View file

@ -15,18 +15,18 @@ namespace Ryujinx.Graphics.Gpu.Image
class TextureBindingsArrayCache class TextureBindingsArrayCache
{ {
/// <summary> /// <summary>
/// Maximum amount of entries on the cache, before entries are re-used. /// Minimum timestamp delta until texture array can be removed from the cache.
/// </summary> /// </summary>
private const int CacheCapacity = 1024; private const int MinDeltaForRemoval = 20000;
private readonly GpuContext _context; private readonly GpuContext _context;
private readonly GpuChannel _channel; private readonly GpuChannel _channel;
private readonly bool _isCompute; private readonly bool _isCompute;
/// <summary> /// <summary>
/// Array cache entry. /// Array cache entry key.
/// </summary> /// </summary>
private struct CacheEntry private readonly struct CacheEntryKey : IEquatable<CacheEntryKey>
{ {
/// <summary> /// <summary>
/// Whether the entry is for an image. /// Whether the entry is for an image.
@ -48,6 +48,99 @@ namespace Ryujinx.Graphics.Gpu.Image
/// </summary> /// </summary>
public readonly int ArrayLength; public readonly int ArrayLength;
private readonly TexturePool _texturePool;
private readonly SamplerPool _samplerPool;
private readonly BufferBounds _textureBufferBounds;
/// <summary>
/// Creates a new array cache entry.
/// </summary>
/// <param name="isImage">Whether the entry is for an image</param>
/// <param name="bindingInfo">Binding information for the array</param>
/// <param name="texturePool">Texture pool where the array textures are located</param>
/// <param name="samplerPool">Sampler pool where the array samplers are located</param>
/// <param name="textureBufferBounds">Constant buffer bounds with the texture handles</param>
public CacheEntryKey(
bool isImage,
TextureBindingInfo bindingInfo,
TexturePool texturePool,
SamplerPool samplerPool,
ref BufferBounds textureBufferBounds)
{
IsImage = isImage;
Target = bindingInfo.Target;
HandleIndex = bindingInfo.Handle;
ArrayLength = bindingInfo.ArrayLength;
_texturePool = texturePool;
_samplerPool = samplerPool;
_textureBufferBounds = textureBufferBounds;
}
/// <summary>
/// Checks if the texture and sampler pools matches the cached pools.
/// </summary>
/// <param name="texturePool">Texture pool instance</param>
/// <param name="samplerPool">Sampler pool instance</param>
/// <returns>True if the pools match, false otherwise</returns>
private bool MatchesPools(TexturePool texturePool, SamplerPool samplerPool)
{
return _texturePool == texturePool && _samplerPool == samplerPool;
}
/// <summary>
/// Checks if the cached constant buffer address and size matches.
/// </summary>
/// <param name="textureBufferBounds">New buffer address and size</param>
/// <returns>True if the address and size matches, false otherwise</returns>
private bool MatchesBufferBounds(BufferBounds textureBufferBounds)
{
return _textureBufferBounds.Equals(textureBufferBounds);
}
public bool Equals(CacheEntryKey other)
{
return IsImage == other.IsImage &&
Target == other.Target &&
HandleIndex == other.HandleIndex &&
ArrayLength == other.ArrayLength &&
MatchesPools(other._texturePool, other._samplerPool) &&
MatchesBufferBounds(other._textureBufferBounds);
}
public override bool Equals(object obj)
{
return obj is CacheEntryKey other && Equals(other);
}
public override int GetHashCode()
{
return _textureBufferBounds.Range.GetHashCode();
}
}
/// <summary>
/// Array cache entry.
/// </summary>
private class CacheEntry
{
/// <summary>
/// Key for this entry on the cache.
/// </summary>
public readonly CacheEntryKey Key;
/// <summary>
/// Linked list node used on the texture bindings array cache.
/// </summary>
public LinkedListNode<CacheEntry> CacheNode;
/// <summary>
/// Timestamp set on the last use of the array by the cache.
/// </summary>
public int CacheTimestamp;
/// <summary> /// <summary>
/// All cached textures, along with their invalidated sequence number as value. /// All cached textures, along with their invalidated sequence number as value.
/// </summary> /// </summary>
@ -64,12 +157,12 @@ namespace Ryujinx.Graphics.Gpu.Image
public readonly Dictionary<int, Sampler> SamplerIds; public readonly Dictionary<int, Sampler> SamplerIds;
/// <summary> /// <summary>
/// Backend texture array if <see cref="IsImage"/> is false, otherwise null. /// Backend texture array if the entry is for a texture, otherwise null.
/// </summary> /// </summary>
public readonly ITextureArray TextureArray; public readonly ITextureArray TextureArray;
/// <summary> /// <summary>
/// Backend image array if <see cref="IsImage"/> is true, otherwise null. /// Backend image array if the entry is for an image, otherwise null.
/// </summary> /// </summary>
public readonly IImageArray ImageArray; public readonly IImageArray ImageArray;
@ -79,8 +172,6 @@ namespace Ryujinx.Graphics.Gpu.Image
private int _texturePoolSequence; private int _texturePoolSequence;
private int _samplerPoolSequence; private int _samplerPoolSequence;
private readonly BufferBounds _textureBufferBounds;
private int[] _cachedTextureBuffer; private int[] _cachedTextureBuffer;
private int[] _cachedSamplerBuffer; private int[] _cachedSamplerBuffer;
@ -89,47 +180,28 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <summary> /// <summary>
/// Creates a new array cache entry. /// Creates a new array cache entry.
/// </summary> /// </summary>
/// <param name="isImage">Whether the entry is for an image</param> /// <param name="key">Key for this entry on the cache</param>
/// <param name="bindingInfo">Binding information for the array</param>
/// <param name="texturePool">Texture pool where the array textures are located</param> /// <param name="texturePool">Texture pool where the array textures are located</param>
/// <param name="samplerPool">Sampler pool where the array samplers are located</param> /// <param name="samplerPool">Sampler pool where the array samplers are located</param>
/// <param name="textureBufferBounds">Constant buffer bounds with the texture handles</param> private CacheEntry(ref CacheEntryKey key, TexturePool texturePool, SamplerPool samplerPool)
private CacheEntry(
bool isImage,
TextureBindingInfo bindingInfo,
TexturePool texturePool,
SamplerPool samplerPool,
ref BufferBounds textureBufferBounds)
{ {
IsImage = isImage; Key = key;
Target = bindingInfo.Target;
HandleIndex = bindingInfo.Handle;
ArrayLength = bindingInfo.ArrayLength;
Textures = new Dictionary<Texture, int>(); Textures = new Dictionary<Texture, int>();
TextureIds = new Dictionary<int, Texture>(); TextureIds = new Dictionary<int, Texture>();
SamplerIds = new Dictionary<int, Sampler>(); SamplerIds = new Dictionary<int, Sampler>();
_texturePool = texturePool; _texturePool = texturePool;
_samplerPool = samplerPool; _samplerPool = samplerPool;
_textureBufferBounds = textureBufferBounds;
} }
/// <summary> /// <summary>
/// Creates a new array cache entry. /// Creates a new array cache entry.
/// </summary> /// </summary>
/// <param name="isImage">Whether the entry is for an image</param> /// <param name="key">Key for this entry on the cache</param>
/// <param name="array">Backend texture array</param> /// <param name="array">Backend texture array</param>
/// <param name="texturePool">Texture pool where the array textures are located</param> /// <param name="texturePool">Texture pool where the array textures are located</param>
/// <param name="samplerPool">Sampler pool where the array samplers are located</param> /// <param name="samplerPool">Sampler pool where the array samplers are located</param>
/// <param name="textureBufferBounds">Constant buffer bounds with the texture handles</param> public CacheEntry(ref CacheEntryKey key, ITextureArray array, TexturePool texturePool, SamplerPool samplerPool) : this(ref key, texturePool, samplerPool)
public CacheEntry(
TextureBindingInfo bindingInfo,
ITextureArray array,
TexturePool texturePool,
SamplerPool samplerPool,
ref BufferBounds textureBufferBounds) : this(isImage: false, bindingInfo, texturePool, samplerPool, ref textureBufferBounds)
{ {
TextureArray = array; TextureArray = array;
} }
@ -137,17 +209,11 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <summary> /// <summary>
/// Creates a new array cache entry. /// Creates a new array cache entry.
/// </summary> /// </summary>
/// <param name="isImage">Whether the entry is for an image</param> /// <param name="key">Key for this entry on the cache</param>
/// <param name="array">Backend image array</param> /// <param name="array">Backend image array</param>
/// <param name="texturePool">Texture pool where the array textures are located</param> /// <param name="texturePool">Texture pool where the array textures are located</param>
/// <param name="samplerPool">Sampler pool where the array samplers are located</param> /// <param name="samplerPool">Sampler pool where the array samplers are located</param>
/// <param name="textureBufferBounds">Constant buffer bounds with the texture handles</param> public CacheEntry(ref CacheEntryKey key, IImageArray array, TexturePool texturePool, SamplerPool samplerPool) : this(ref key, texturePool, samplerPool)
public CacheEntry(
TextureBindingInfo bindingInfo,
IImageArray array,
TexturePool texturePool,
SamplerPool samplerPool,
ref BufferBounds textureBufferBounds) : this(isImage: true, bindingInfo, texturePool, samplerPool, ref textureBufferBounds)
{ {
ImageArray = array; ImageArray = array;
} }
@ -155,7 +221,7 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <summary> /// <summary>
/// Clears all cached texture instances. /// Clears all cached texture instances.
/// </summary> /// </summary>
public readonly void Reset() public void Reset()
{ {
Textures.Clear(); Textures.Clear();
TextureIds.Clear(); TextureIds.Clear();
@ -190,7 +256,7 @@ namespace Ryujinx.Graphics.Gpu.Image
/// Checks if any texture has been deleted since the last call to this method. /// Checks if any texture has been deleted since the last call to this method.
/// </summary> /// </summary>
/// <returns>True if one or more textures have been deleted, false otherwise</returns> /// <returns>True if one or more textures have been deleted, false otherwise</returns>
public readonly bool ValidateTextures() public bool ValidateTextures()
{ {
foreach ((Texture texture, int invalidatedSequence) in Textures) foreach ((Texture texture, int invalidatedSequence) in Textures)
{ {
@ -270,27 +336,6 @@ namespace Ryujinx.Graphics.Gpu.Image
return false; return false;
} }
/// <summary>
/// Checks if the texture and sampler pools matches the cached pools.
/// </summary>
/// <param name="texturePool">Texture pool instance</param>
/// <param name="samplerPool">Sampler pool instance</param>
/// <returns>True if the pools match, false otherwise</returns>
public readonly bool MatchesPools(TexturePool texturePool, SamplerPool samplerPool)
{
return _texturePool == texturePool && _samplerPool == samplerPool;
}
/// <summary>
/// Checks if the cached constant buffer address and size matches.
/// </summary>
/// <param name="textureBufferBounds">New buffer address and size</param>
/// <returns>True if the address and size matches, false otherwise</returns>
public readonly bool MatchesBufferBounds(ref BufferBounds textureBufferBounds)
{
return _textureBufferBounds.Equals(ref textureBufferBounds);
}
/// <summary> /// <summary>
/// Checks if the buffer data matches the cached data. /// Checks if the buffer data matches the cached data.
/// </summary> /// </summary>
@ -299,7 +344,7 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <param name="separateSamplerBuffer">Whether <paramref name="cachedTextureBuffer"/> and <paramref name="cachedSamplerBuffer"/> comes from different buffers</param> /// <param name="separateSamplerBuffer">Whether <paramref name="cachedTextureBuffer"/> and <paramref name="cachedSamplerBuffer"/> comes from different buffers</param>
/// <param name="samplerWordOffset">Word offset of the sampler constant buffer handle that is used</param> /// <param name="samplerWordOffset">Word offset of the sampler constant buffer handle that is used</param>
/// <returns>True if the data matches, false otherwise</returns> /// <returns>True if the data matches, false otherwise</returns>
public readonly bool MatchesBufferData( public bool MatchesBufferData(
ReadOnlySpan<int> cachedTextureBuffer, ReadOnlySpan<int> cachedTextureBuffer,
ReadOnlySpan<int> cachedSamplerBuffer, ReadOnlySpan<int> cachedSamplerBuffer,
bool separateSamplerBuffer, bool separateSamplerBuffer,
@ -334,7 +379,10 @@ namespace Ryujinx.Graphics.Gpu.Image
} }
} }
private CacheEntry[] _cache; private readonly Dictionary<CacheEntryKey, CacheEntry> _cache;
private readonly LinkedList<CacheEntry> _lruCache;
private int _currentTimestamp;
/// <summary> /// <summary>
/// Creates a new instance of the texture bindings array cache. /// Creates a new instance of the texture bindings array cache.
@ -347,7 +395,8 @@ namespace Ryujinx.Graphics.Gpu.Image
_context = context; _context = context;
_channel = channel; _channel = channel;
_isCompute = isCompute; _isCompute = isCompute;
_cache = Array.Empty<CacheEntry>(); _cache = new Dictionary<CacheEntryKey, CacheEntry>();
_lruCache = new LinkedList<CacheEntry>();
} }
/// <summary> /// <summary>
@ -429,7 +478,7 @@ namespace Ryujinx.Graphics.Gpu.Image
(_, int samplerWordOffset, _) = TextureHandle.UnpackOffsets(bindingInfo.Handle); (_, int samplerWordOffset, _) = TextureHandle.UnpackOffsets(bindingInfo.Handle);
ref CacheEntry entry = ref GetOrAddEntry( CacheEntry entry = GetOrAddEntry(
texturePool, texturePool,
samplerPool, samplerPool,
bindingInfo, bindingInfo,
@ -575,8 +624,8 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <param name="isImage">Whether the array is a image or texture array</param> /// <param name="isImage">Whether the array is a image or texture array</param>
/// <param name="textureBufferBounds">Constant buffer bounds with the texture handles</param> /// <param name="textureBufferBounds">Constant buffer bounds with the texture handles</param>
/// <param name="isNew">Whether a new entry was created, or an existing one was returned</param> /// <param name="isNew">Whether a new entry was created, or an existing one was returned</param>
/// <returns>Cache entry reference</returns> /// <returns>Cache entry</returns>
private ref CacheEntry GetOrAddEntry( private CacheEntry GetOrAddEntry(
TexturePool texturePool, TexturePool texturePool,
SamplerPool samplerPool, SamplerPool samplerPool,
TextureBindingInfo bindingInfo, TextureBindingInfo bindingInfo,
@ -584,67 +633,60 @@ namespace Ryujinx.Graphics.Gpu.Image
ref BufferBounds textureBufferBounds, ref BufferBounds textureBufferBounds,
out bool isNew) out bool isNew)
{ {
int matchIndex = -1; CacheEntryKey key = new CacheEntryKey(
isImage,
bindingInfo,
texturePool,
samplerPool,
ref textureBufferBounds);
isNew = !_cache.TryGetValue(key, out CacheEntry entry);
if (isNew)
{
int arrayLength = bindingInfo.ArrayLength; int arrayLength = bindingInfo.ArrayLength;
for (int index = 0; index < _cache.Length; index++)
{
ref CacheEntry entry = ref _cache[index];
if (entry.IsImage == isImage &&
entry.Target == bindingInfo.Target &&
entry.HandleIndex == bindingInfo.Handle &&
entry.ArrayLength == arrayLength &&
entry.MatchesPools(texturePool, samplerPool) &&
entry.MatchesBufferBounds(ref textureBufferBounds))
{
matchIndex = index;
break;
}
}
if (matchIndex < 0)
{
if (_cache.Length < CacheCapacity)
{
matchIndex = _cache.Length;
Array.Resize(ref _cache, matchIndex + 1);
}
else
{
matchIndex = CacheCapacity - 1;
}
if (isImage) if (isImage)
{ {
IImageArray array = _context.Renderer.CreateImageArray(arrayLength, bindingInfo.Target == Target.TextureBuffer); IImageArray array = _context.Renderer.CreateImageArray(arrayLength, bindingInfo.Target == Target.TextureBuffer);
_cache[matchIndex] = new CacheEntry(bindingInfo, array, texturePool, samplerPool, ref textureBufferBounds); _cache.Add(key, entry = new CacheEntry(ref key, array, texturePool, samplerPool));
} }
else else
{ {
ITextureArray array = _context.Renderer.CreateTextureArray(arrayLength, bindingInfo.Target == Target.TextureBuffer); ITextureArray array = _context.Renderer.CreateTextureArray(arrayLength, bindingInfo.Target == Target.TextureBuffer);
_cache[matchIndex] = new CacheEntry(bindingInfo, array, texturePool, samplerPool, ref textureBufferBounds); _cache.Add(key, entry = new CacheEntry(ref key, array, texturePool, samplerPool));
}
} }
isNew = true; if (entry.CacheNode != null)
}
else
{ {
isNew = false; _lruCache.Remove(entry.CacheNode);
} }
if (matchIndex > 0) entry.CacheNode = _lruCache.AddLast(entry);
entry.CacheTimestamp = ++_currentTimestamp;
RemoveLeastUsedEntries();
return entry;
}
/// <summary>
/// Remove entries from the cache that have not been used for some time.
/// </summary>
private void RemoveLeastUsedEntries()
{ {
CacheEntry temp = _cache[matchIndex]; LinkedListNode<CacheEntry> nextNode = _lruCache.First;
Array.Copy(_cache, 0, _cache, 1, matchIndex); while (nextNode != null && _currentTimestamp - nextNode.Value.CacheTimestamp >= MinDeltaForRemoval)
{
_cache[0] = temp; LinkedListNode<CacheEntry> toRemove = nextNode;
nextNode = nextNode.Next;
_cache.Remove(toRemove.Value.Key);
_lruCache.Remove(toRemove);
} }
return ref _cache[0];
} }
/// <summary> /// <summary>
@ -653,9 +695,9 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <param name="binding">Binding number to clear</param> /// <param name="binding">Binding number to clear</param>
private void ClearBindingsIfEqual(int binding) private void ClearBindingsIfEqual(int binding)
{ {
for (int index = 0; index < _cache.Length; index++) foreach (CacheEntry entry in _cache.Values)
{ {
_cache[index].ClearBindingIfEqual(binding); entry.ClearBindingIfEqual(binding);
} }
} }
} }