Optimize texture cache code

This commit is contained in:
KeatonTheBot
2026-03-24 15:21:33 -05:00
parent d7f90f9f1f
commit d207d43768
@@ -53,11 +53,6 @@ namespace Ryujinx.Graphics.Gpu.Image
private ulong MaxTextureSizeCapacity = 2 * GiB;
private const ulong MinTextureSizeCapacity = 512 * MiB;
private const ulong DefaultTextureSizeCapacity = 1 * GiB;
private const ulong TextureSizeCapacity4GiB = 2 * GiB;
private const ulong TextureSizeCapacity6GiB = 4 * GiB;
private const ulong TextureSizeCapacity8GiB = 6 * GiB;
private const ulong TextureSizeCapacity10GiB = 10 * GiB;
private const ulong TextureSizeCapacity12GiB = 12 * GiB;
private const float MemoryScaleFactor = 0.50f;
private ulong _maxCacheMemoryUsage = DefaultTextureSizeCapacity;
@@ -82,25 +77,18 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <param name="cpuMemorySize">The amount of physical CPU Memory available on the device.</param>
public void Initialize(GpuContext context, ulong cpuMemorySize)
{
var cpuMemorySizeGiB = cpuMemorySize / GiB;
var MaximumGpuMemoryGiB = context.Capabilities.MaximumGpuMemory / GiB;
ulong cpuMemorySizeGiB = cpuMemorySize / GiB;
ulong MaximumGpuMemoryGiB = context.Capabilities.MaximumGpuMemory / GiB;
ulong TextureSizeCapacity = cpuMemorySize - (2 * GiB);
if (context.Capabilities.MaximumGpuMemory == 0)
{
MaxTextureSizeCapacity = DefaultTextureSizeCapacity;
}
else
MaxTextureSizeCapacity = cpuMemorySizeGiB switch
{
< 6 when MaximumGpuMemoryGiB < 6 => DefaultTextureSizeCapacity,
< 6 => TextureSizeCapacity4GiB,
6 => TextureSizeCapacity6GiB,
8 => TextureSizeCapacity8GiB,
10 => TextureSizeCapacity10GiB,
_ => TextureSizeCapacity12GiB
};
MaxTextureSizeCapacity =
context.Capabilities.MaximumGpuMemory == 0 || cpuMemorySizeGiB < 6 && MaximumGpuMemoryGiB < 6
? DefaultTextureSizeCapacity
: cpuMemorySizeGiB < 10
? TextureSizeCapacity
: cpuMemorySize;
var cacheMemory = (ulong)(context.Capabilities.MaximumGpuMemory * MemoryScaleFactor);
ulong cacheMemory = (ulong)(context.Capabilities.MaximumGpuMemory * MemoryScaleFactor);
_maxCacheMemoryUsage = Math.Clamp(cacheMemory, MinTextureSizeCapacity, MaxTextureSizeCapacity);
@@ -177,10 +165,10 @@ namespace Ryujinx.Graphics.Gpu.Image
/// </summary>
private void RemoveLeastUsedTexture()
{
if (_textures.First != null)
{
Texture oldestTexture = _textures.First.Value;
Texture oldestTexture = _textures.First?.Value;
if (oldestTexture != null)
{
_totalSize -= oldestTexture.Size;
if (!oldestTexture.CheckModified(false))
@@ -206,11 +194,11 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <param name="texture">The texture to be removed from the cache</param>
/// <param name="flush">True to remove the texture if it was on the cache</param>
/// <returns>True if the texture was found and removed, false otherwise</returns>
public bool Remove(Texture texture, bool flush)
public void Remove(Texture texture, bool flush)
{
if (texture.CacheNode == null)
{
return false;
return;
}
// Remove our reference to this texture.
@@ -225,7 +213,7 @@ namespace Ryujinx.Graphics.Gpu.Image
texture.CacheNode = null;
return texture.DecrementReferenceCount();
texture.DecrementReferenceCount();
}
/// <summary>
@@ -241,10 +229,8 @@ namespace Ryujinx.Graphics.Gpu.Image
{
return entry.Texture;
}
else
{
_shortCacheLookup.Remove(descriptor);
}
_shortCacheLookup.Remove(descriptor);
}
return null;