2023-12-04 13:17:13 +00:00
|
|
|
using Ryujinx.Graphics.GAL;
|
2020-09-10 19:44:04 +00:00
|
|
|
using Ryujinx.Graphics.OpenGL.Image;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.OpenGL
|
|
|
|
{
|
|
|
|
class DisposedTexture
|
|
|
|
{
|
|
|
|
public TextureCreateInfo Info;
|
|
|
|
public TextureView View;
|
|
|
|
public int RemainingFrames;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A structure for pooling resources that can be reused without recreation, such as textures.
|
|
|
|
/// </summary>
|
|
|
|
class ResourcePool : IDisposable
|
|
|
|
{
|
|
|
|
private const int DisposedLiveFrames = 2;
|
|
|
|
|
2023-06-15 00:34:55 +00:00
|
|
|
private readonly object _lock = new();
|
2023-06-28 16:10:55 +00:00
|
|
|
private readonly Dictionary<TextureCreateInfo, List<DisposedTexture>> _textures = new();
|
2020-09-10 19:44:04 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Add a texture that is not being used anymore to the resource pool to be used later.
|
|
|
|
/// Both the texture's view and storage should be completely unused.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="view">The texture's view</param>
|
|
|
|
public void AddTexture(TextureView view)
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
2023-06-28 16:10:55 +00:00
|
|
|
if (!_textures.TryGetValue(view.Info, out List<DisposedTexture> list))
|
2020-09-10 19:44:04 +00:00
|
|
|
{
|
|
|
|
list = new List<DisposedTexture>();
|
|
|
|
_textures.Add(view.Info, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Add(new DisposedTexture()
|
|
|
|
{
|
|
|
|
Info = view.Info,
|
|
|
|
View = view,
|
2023-07-24 16:35:04 +00:00
|
|
|
RemainingFrames = DisposedLiveFrames,
|
2020-09-10 19:44:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Attempt to obtain a texture from the resource cache with the desired parameters.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="info">The creation info for the desired texture</param>
|
|
|
|
/// <returns>A TextureView with the description specified, or null if one was not found.</returns>
|
2023-07-11 17:07:41 +00:00
|
|
|
public TextureView GetTextureOrNull(TextureCreateInfo info)
|
2020-09-10 19:44:04 +00:00
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
2023-06-28 16:10:55 +00:00
|
|
|
if (!_textures.TryGetValue(info, out List<DisposedTexture> list))
|
2020-09-10 19:44:04 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (DisposedTexture texture in list)
|
|
|
|
{
|
2023-07-11 17:07:41 +00:00
|
|
|
list.Remove(texture);
|
|
|
|
return texture.View;
|
2020-09-10 19:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Update the pool, removing any resources that have expired.
|
|
|
|
/// </summary>
|
|
|
|
public void Tick()
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
foreach (List<DisposedTexture> list in _textures.Values)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < list.Count; i++)
|
|
|
|
{
|
|
|
|
DisposedTexture tex = list[i];
|
|
|
|
|
|
|
|
if (--tex.RemainingFrames < 0)
|
|
|
|
{
|
|
|
|
tex.View.Dispose();
|
|
|
|
list.RemoveAt(i--);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Disposes the resource pool.
|
|
|
|
/// </summary>
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
foreach (List<DisposedTexture> list in _textures.Values)
|
|
|
|
{
|
|
|
|
foreach (DisposedTexture texture in list)
|
|
|
|
{
|
|
|
|
texture.View.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_textures.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|