aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.OpenGL/ResourcePool.cs
blob: bba5c5cb729a6cee4b366f289c19c22d934310df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.OpenGL.Image;
using System;
using System.Collections.Generic;

namespace Ryujinx.Graphics.OpenGL
{
    class DisposedTexture
    {
        public TextureCreateInfo Info;
        public TextureView View;
        public float ScaleFactor;
        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;

        private readonly object _lock = new();
        private readonly Dictionary<TextureCreateInfo, List<DisposedTexture>> _textures = new();

        /// <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)
            {
                if (!_textures.TryGetValue(view.Info, out List<DisposedTexture> list))
                {
                    list = new List<DisposedTexture>();
                    _textures.Add(view.Info, list);
                }

                list.Add(new DisposedTexture()
                {
                    Info = view.Info,
                    View = view,
                    ScaleFactor = view.ScaleFactor,
                    RemainingFrames = DisposedLiveFrames
                });
            }
        }

        /// <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>
        /// <param name="scaleFactor">The scale factor for the desired texture</param>
        /// <returns>A TextureView with the description specified, or null if one was not found.</returns>
        public TextureView GetTextureOrNull(TextureCreateInfo info, float scaleFactor)
        {
            lock (_lock)
            {
                if (!_textures.TryGetValue(info, out List<DisposedTexture> list))
                {
                    return null;
                }

                foreach (DisposedTexture texture in list)
                {
                    if (scaleFactor == texture.ScaleFactor)
                    {
                        list.Remove(texture);
                        return texture.View;
                    }
                }

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