From f0e27a23a5a4c834cc846e415c9cce0597e8d6c5 Mon Sep 17 00:00:00 2001 From: riperiperi Date: Tue, 17 Jan 2023 03:39:46 +0000 Subject: Add short duration texture cache (#3754) * Add short duration texture cache This texture cache takes textures that lose their last pool reference and keeps them alive until the next frame, or until an incompatible overlap removes it. This is done since under certain circumstances, a texture's reference can be wiped from a pool despite it still being in use - though typically the reference will return when rendering the next frame. While this may slightly increase texture memory usage when quickly going through a bunch of temporary textures, it's still bounded due to the overlap removal rule. This greatly increases performance in Hyrule Warriors: Age of Calamity. It may positively affect some UE4 games which dip framerate severely under certain circumstances. * Small optimization * Don't forget this. * Add short cache dictionary * Address feedback * Address some feedback --- Ryujinx.Graphics.Gpu/Image/AutoDeleteCache.cs | 111 ++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) (limited to 'Ryujinx.Graphics.Gpu/Image/AutoDeleteCache.cs') diff --git a/Ryujinx.Graphics.Gpu/Image/AutoDeleteCache.cs b/Ryujinx.Graphics.Gpu/Image/AutoDeleteCache.cs index 4a1615f0..379eb715 100644 --- a/Ryujinx.Graphics.Gpu/Image/AutoDeleteCache.cs +++ b/Ryujinx.Graphics.Gpu/Image/AutoDeleteCache.cs @@ -4,6 +4,28 @@ using System.Collections.Generic; namespace Ryujinx.Graphics.Gpu.Image { + /// + /// An entry on the short duration texture cache. + /// + class ShortTextureCacheEntry + { + public readonly TextureDescriptor Descriptor; + public readonly int InvalidatedSequence; + public readonly Texture Texture; + + /// + /// Create a new entry on the short duration texture cache. + /// + /// Last descriptor that referenced the texture + /// The texture + public ShortTextureCacheEntry(TextureDescriptor descriptor, Texture texture) + { + Descriptor = descriptor; + InvalidatedSequence = texture.InvalidatedSequence; + Texture = texture; + } + } + /// /// A texture cache that automatically removes older textures that are not used for some time. /// The cache works with a rotated list with a fixed size. When new textures are added, the @@ -16,6 +38,11 @@ namespace Ryujinx.Graphics.Gpu.Image private readonly LinkedList _textures; private readonly ConcurrentQueue _deferredRemovals; + private HashSet _shortCacheBuilder; + private HashSet _shortCache; + + private Dictionary _shortCacheLookup; + /// /// Creates a new instance of the automatic deletion cache. /// @@ -23,6 +50,11 @@ namespace Ryujinx.Graphics.Gpu.Image { _textures = new LinkedList(); _deferredRemovals = new ConcurrentQueue(); + + _shortCacheBuilder = new HashSet(); + _shortCache = new HashSet(); + + _shortCacheLookup = new Dictionary(); } /// @@ -130,6 +162,85 @@ namespace Ryujinx.Graphics.Gpu.Image _deferredRemovals.Enqueue(texture); } + /// + /// Attempt to find a texture on the short duration cache. + /// + /// The texture descriptor + /// The texture if found, null otherwise + public Texture FindShortCache(in TextureDescriptor descriptor) + { + if (_shortCacheLookup.Count > 0 && _shortCacheLookup.TryGetValue(descriptor, out var entry)) + { + if (entry.InvalidatedSequence == entry.Texture.InvalidatedSequence) + { + return entry.Texture; + } + else + { + _shortCacheLookup.Remove(descriptor); + } + } + + return null; + } + + /// + /// Removes a texture from the short duration cache. + /// + /// Texture to remove from the short cache + public void RemoveShortCache(Texture texture) + { + bool removed = _shortCache.Remove(texture.ShortCacheEntry); + removed |= _shortCacheBuilder.Remove(texture.ShortCacheEntry); + + if (removed) + { + texture.DecrementReferenceCount(); + + _shortCacheLookup.Remove(texture.ShortCacheEntry.Descriptor); + texture.ShortCacheEntry = null; + } + } + + /// + /// Adds a texture to the short duration cache. + /// It starts in the builder set, and it is moved into the deletion set on next process. + /// + /// Texture to add to the short cache + /// Last used texture descriptor + public void AddShortCache(Texture texture, ref TextureDescriptor descriptor) + { + var entry = new ShortTextureCacheEntry(descriptor, texture); + + _shortCacheBuilder.Add(entry); + _shortCacheLookup.Add(entry.Descriptor, entry); + + texture.ShortCacheEntry = entry; + + texture.IncrementReferenceCount(); + } + + /// + /// Delete textures from the short duration cache. + /// Moves the builder set to be deleted on next process. + /// + public void ProcessShortCache() + { + HashSet toRemove = _shortCache; + + foreach (var entry in toRemove) + { + entry.Texture.DecrementReferenceCount(); + + _shortCacheLookup.Remove(entry.Descriptor); + entry.Texture.ShortCacheEntry = null; + } + + toRemove.Clear(); + _shortCache = _shortCacheBuilder; + _shortCacheBuilder = toRemove; + } + public IEnumerator GetEnumerator() { return _textures.GetEnumerator(); -- cgit v1.2.3