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.
This commit is contained in:
MaxLastBreath 2024-09-18 02:37:39 +03:00
parent f5389089a8
commit 9e2a35246c
4 changed files with 22 additions and 20 deletions

View File

@ -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<Texture> _textures;
private ulong _totalSize;
@ -58,30 +60,24 @@ namespace Ryujinx.Graphics.Gpu.Image
private readonly Dictionary<TextureDescriptor, ShortTextureCacheEntry> _shortCacheLookup;
private readonly GpuContext _context;
/// <summary>
/// Gets MaxTextureCapacity Dynamically
/// </summary>
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;
}
/// <summary>
/// Creates a new instance of the automatic deletion cache.
/// </summary>
public AutoDeleteCache(GpuContext context)
public AutoDeleteCache()
{
_context = context;
_textures = new LinkedList<Texture>();
_shortCacheBuilder = new HashSet<ShortTextureCacheEntry>();
@ -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();
}

View File

@ -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);
}
/// <summary>

View File

@ -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();
}
/// <summary>

View File

@ -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)