Skip to content

Commit

Permalink
Textures : Increase the amount of VRAM Cache available for Textures b…
Browse files Browse the repository at this point in the history
…ased on selected DRAM. (#36)
  • Loading branch information
MaxLastBreath authored Nov 1, 2024
1 parent fb4ab5e commit 9305d17
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/Ryujinx.Graphics.Gpu/GpuContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,17 @@ public GpuChannel CreateChannel()
/// Creates a new GPU memory manager.
/// </summary>
/// <param name="pid">ID of the process that owns the memory manager</param>
/// <param name="cpuMemorySize">The amount of physical CPU Memory Avaiable on the device.</param>
/// <returns>The memory manager</returns>
/// <exception cref="ArgumentException">Thrown when <paramref name="pid"/> is invalid</exception>
public MemoryManager CreateMemoryManager(ulong pid)
public MemoryManager CreateMemoryManager(ulong pid, ulong cpuMemorySize)
{
if (!PhysicalMemoryRegistry.TryGetValue(pid, out var physicalMemory))
{
throw new ArgumentException("The PID is invalid or the process was not registered", nameof(pid));
}

return new MemoryManager(physicalMemory);
return new MemoryManager(physicalMemory, cpuMemorySize);
}

/// <summary>
Expand Down
43 changes: 35 additions & 8 deletions src/Ryujinx.Graphics.Gpu/Image/AutoDeleteCache.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Ryujinx.Common.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -47,11 +48,17 @@ class AutoDeleteCache : IEnumerable<Texture>
{
private const int MinCountForDeletion = 32;
private const int MaxCapacity = 2048;
private const ulong GiB = 1024 * 1024 * 1024;
private ulong MaxTextureSizeCapacity = 4UL * GiB;
private const ulong MinTextureSizeCapacity = 512 * 1024 * 1024;
private const ulong MaxTextureSizeCapacity = 4UL * 1024 * 1024 * 1024;
private const ulong DefaultTextureSizeCapacity = 1UL * 1024 * 1024 * 1024;
private const ulong DefaultTextureSizeCapacity = 1 * GiB;
private const ulong TextureSizeCapacity6GiB = 4 * GiB;
private const ulong TextureSizeCapacity8GiB = 6 * GiB;
private const ulong TextureSizeCapacity12GiB = 12 * GiB;


private const float MemoryScaleFactor = 0.50f;
private ulong _maxCacheMemoryUsage = 0;
private ulong _maxCacheMemoryUsage = DefaultTextureSizeCapacity;

private readonly LinkedList<Texture> _textures;
private ulong _totalSize;
Expand All @@ -66,18 +73,38 @@ class AutoDeleteCache : IEnumerable<Texture>
/// </summary>
/// <remarks>
/// If the backend GPU has 0 memory capacity, the cache size defaults to `DefaultTextureSizeCapacity`.
///
/// Reads the current Device total CPU Memory, to determine the maximum amount of Vram available. Capped to 50% of Current GPU Memory.
/// </remarks>
/// <param name="context">The GPU context that the cache belongs to</param>
public void Initialize(GpuContext context)
/// <param name="cpuMemorySize">The amount of physical CPU Memory Avaiable on the device.</param>
public void Initialize(GpuContext context, ulong cpuMemorySize)
{
var cacheMemory = (ulong)(context.Capabilities.MaximumGpuMemory * MemoryScaleFactor);

_maxCacheMemoryUsage = Math.Clamp(cacheMemory, MinTextureSizeCapacity, MaxTextureSizeCapacity);
var cpuMemorySizeGiB = cpuMemorySize / GiB;

if (context.Capabilities.MaximumGpuMemory == 0)
if (cpuMemorySizeGiB < 6 || context.Capabilities.MaximumGpuMemory == 0)
{
_maxCacheMemoryUsage = DefaultTextureSizeCapacity;
return;
}
else if (cpuMemorySizeGiB == 6)
{
MaxTextureSizeCapacity = TextureSizeCapacity6GiB;
}
else if (cpuMemorySizeGiB == 8)
{
MaxTextureSizeCapacity = TextureSizeCapacity8GiB;
}
else
{
MaxTextureSizeCapacity = TextureSizeCapacity12GiB;
}

var cacheMemory = (ulong)(context.Capabilities.MaximumGpuMemory * MemoryScaleFactor);

_maxCacheMemoryUsage = Math.Clamp(cacheMemory, MinTextureSizeCapacity, MaxTextureSizeCapacity);

Logger.Info?.Print(LogClass.Gpu, $"AutoDelete Cache Allocated VRAM : {_maxCacheMemoryUsage / GiB} GiB");
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Ryujinx.Graphics.Gpu/Image/TextureCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ public TextureCache(GpuContext context, PhysicalMemory physicalMemory)
/// <summary>
/// Initializes the cache, setting the maximum texture capacity for the specified GPU context.
/// </summary>
public void Initialize()
/// <param name="cpuMemorySize">The amount of physical CPU Memory Avaiable on the device.</param>
public void Initialize(ulong cpuMemorySize)
{
_cache.Initialize(_context);
_cache.Initialize(_context, cpuMemorySize);
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public class MemoryManager : IWritableBlock
/// Creates a new instance of the GPU memory manager.
/// </summary>
/// <param name="physicalMemory">Physical memory that this memory manager will map into</param>
internal MemoryManager(PhysicalMemory physicalMemory)
/// <param name="cpuMemorySize">The amount of physical CPU Memory Avaiable on the device.</param>
internal MemoryManager(PhysicalMemory physicalMemory, ulong cpuMemorySize)
{
Physical = physicalMemory;
VirtualRangeCache = new VirtualRangeCache(this);
Expand All @@ -65,7 +66,7 @@ internal MemoryManager(PhysicalMemory physicalMemory)
MemoryUnmapped += Physical.BufferCache.MemoryUnmappedHandler;
MemoryUnmapped += VirtualRangeCache.MemoryUnmappedHandler;
MemoryUnmapped += CounterCache.MemoryUnmappedHandler;
Physical.TextureCache.Initialize();
Physical.TextureCache.Initialize(cpuMemorySize);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public VmRegion(ulong start, ulong limit)

public NvHostAsGpuDeviceFile(ServiceCtx context, IVirtualMemoryManager memory, ulong owner) : base(context, owner)
{
_asContext = new AddressSpaceContext(context.Device.Gpu.CreateMemoryManager(owner));
_asContext = new AddressSpaceContext(context.Device.Gpu.CreateMemoryManager(owner, context.Device.Memory.Size));
_memoryAllocator = new NvMemoryAllocator();
}

Expand Down

0 comments on commit 9305d17

Please sign in to comment.