From 9e2a35246c48dafa7edf2eb321ad3b8db7ea488a Mon Sep 17 00:00:00 2001 From: MaxLastBreath Date: Wed, 18 Sep 2024 02:37:39 +0300 Subject: [PATCH] Refractor - Added Initialize() function to TextureCache and AutoDeleteCache - Removed GetMaxTextureCapacity() function and instead added _maxCacheMemoryUsage - Added private const MaxTextureSizeCapacity to AutoDelete Cache - Added TextureCache.Initialize() to MemoryManager in order to fetch MaxGpuMemory at the right time. - Moved and Changed Logger.Info for Gpu Memory to Logger.Notice and Moved it to PrintGpuInformation function. - Opted to use a ternary operator for the Initialize function, I think it looks cleaner than bunch of if statements. --- .../Image/AutoDeleteCache.cs | 30 ++++++++----------- .../Image/TextureCache.cs | 7 ++++- .../Memory/MemoryManager.cs | 2 ++ src/Ryujinx.Graphics.Vulkan/VulkanRenderer.cs | 3 +- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/Ryujinx.Graphics.Gpu/Image/AutoDeleteCache.cs b/src/Ryujinx.Graphics.Gpu/Image/AutoDeleteCache.cs index e9477e469b..bcff9aa0be 100644 --- a/src/Ryujinx.Graphics.Gpu/Image/AutoDeleteCache.cs +++ b/src/Ryujinx.Graphics.Gpu/Image/AutoDeleteCache.cs @@ -1,4 +1,4 @@ -using Ryujinx.Graphics.GAL; +using Ryujinx.Common.Logging; using System.Collections; using System.Collections.Generic; @@ -47,8 +47,10 @@ namespace Ryujinx.Graphics.Gpu.Image { private const int MinCountForDeletion = 32; private const int MaxCapacity = 2048; - private const ulong DefaultTextureSizeCapacity = 1024 * 1024 * 1024; + private const ulong DefaultTextureSizeCapacity = 1UL * 1024 * 1024 * 1024; + private const ulong MaxTextureSizeCapacity = 4UL * 1024 * 1024 * 1024; private const float MemoryScaleFactor = 0.50f; + private ulong _maxCacheMemoryUsage = 0; private readonly LinkedList _textures; private ulong _totalSize; @@ -58,30 +60,24 @@ namespace Ryujinx.Graphics.Gpu.Image private readonly Dictionary _shortCacheLookup; - private readonly GpuContext _context; - /// /// Gets MaxTextureCapacity Dynamically /// - private ulong GetMaxTextureCapacity() + public void Initialize(GpuContext context) { - ulong maxMemory = _context.Capabilities.MaximumGpuMemory; + var maxMemory = (ulong)(context.Capabilities.MaximumGpuMemory * MemoryScaleFactor); - if (maxMemory > 0) - { - return (ulong)(maxMemory * MemoryScaleFactor); - } - - return DefaultTextureSizeCapacity; + _maxCacheMemoryUsage = maxMemory == 0 ? + DefaultTextureSizeCapacity : + maxMemory > MaxTextureSizeCapacity ? + MaxTextureSizeCapacity : maxMemory; } /// /// Creates a new instance of the automatic deletion cache. /// - public AutoDeleteCache(GpuContext context) + public AutoDeleteCache() { - _context = context; - _textures = new LinkedList(); _shortCacheBuilder = new HashSet(); @@ -106,7 +102,7 @@ namespace Ryujinx.Graphics.Gpu.Image texture.CacheNode = _textures.AddLast(texture); if (_textures.Count > MaxCapacity || - (_totalSize > GetMaxTextureCapacity() && _textures.Count >= MinCountForDeletion)) + (_totalSize > _maxCacheMemoryUsage && _textures.Count >= MinCountForDeletion)) { RemoveLeastUsedTexture(); } @@ -131,7 +127,7 @@ namespace Ryujinx.Graphics.Gpu.Image _textures.AddLast(texture.CacheNode); } - if (_totalSize > GetMaxTextureCapacity() && _textures.Count >= MinCountForDeletion) + if (_totalSize > _maxCacheMemoryUsage && _textures.Count >= MinCountForDeletion) { RemoveLeastUsedTexture(); } diff --git a/src/Ryujinx.Graphics.Gpu/Image/TextureCache.cs b/src/Ryujinx.Graphics.Gpu/Image/TextureCache.cs index 5352d188a1..2fd8a5cd8b 100644 --- a/src/Ryujinx.Graphics.Gpu/Image/TextureCache.cs +++ b/src/Ryujinx.Graphics.Gpu/Image/TextureCache.cs @@ -65,7 +65,12 @@ namespace Ryujinx.Graphics.Gpu.Image _textureOverlaps = new Texture[OverlapsBufferInitialCapacity]; _overlapInfo = new OverlapInfo[OverlapsBufferInitialCapacity]; - _cache = new AutoDeleteCache(_context); + _cache = new AutoDeleteCache(); + } + + public void Initialize() + { + _cache.Initialize(_context); } /// diff --git a/src/Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs b/src/Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs index 59a940a4f9..d1065431d1 100644 --- a/src/Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs +++ b/src/Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs @@ -1,4 +1,5 @@ using Ryujinx.Common.Memory; +using Ryujinx.Graphics.Gpu.Image; using Ryujinx.Memory; using Ryujinx.Memory.Range; using System; @@ -64,6 +65,7 @@ namespace Ryujinx.Graphics.Gpu.Memory MemoryUnmapped += Physical.BufferCache.MemoryUnmappedHandler; MemoryUnmapped += VirtualRangeCache.MemoryUnmappedHandler; MemoryUnmapped += CounterCache.MemoryUnmappedHandler; + Physical.TextureCache.Initialize(); } /// diff --git a/src/Ryujinx.Graphics.Vulkan/VulkanRenderer.cs b/src/Ryujinx.Graphics.Vulkan/VulkanRenderer.cs index 6d0b860f94..57207bed00 100644 --- a/src/Ryujinx.Graphics.Vulkan/VulkanRenderer.cs +++ b/src/Ryujinx.Graphics.Vulkan/VulkanRenderer.cs @@ -800,8 +800,6 @@ namespace Ryujinx.Graphics.Vulkan } } - Logger.Info?.Print(LogClass.Gpu, $"GPU Memory: {totalMemory / (1024 * 1024)} MB"); - return totalMemory; } @@ -886,6 +884,7 @@ namespace Ryujinx.Graphics.Vulkan private void PrintGpuInformation() { Logger.Notice.Print(LogClass.Gpu, $"{GpuVendor} {GpuRenderer} ({GpuVersion})"); + Logger.Notice.Print(LogClass.Gpu, $"GPU Memory: {GetTotalGPUMemory() / (1024 * 1024)} MiB"); } public void Initialize(GraphicsDebugLevel logLevel)