ResourcePool.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.OpenGL.Image;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Graphics.OpenGL
  6. {
  7. class DisposedTexture
  8. {
  9. public TextureCreateInfo Info;
  10. public TextureView View;
  11. public float ScaleFactor;
  12. public int RemainingFrames;
  13. }
  14. /// <summary>
  15. /// A structure for pooling resources that can be reused without recreation, such as textures.
  16. /// </summary>
  17. class ResourcePool : IDisposable
  18. {
  19. private const int DisposedLiveFrames = 2;
  20. private readonly object _lock = new object();
  21. private readonly Dictionary<TextureCreateInfo, List<DisposedTexture>> _textures = new Dictionary<TextureCreateInfo, List<DisposedTexture>>();
  22. /// <summary>
  23. /// Add a texture that is not being used anymore to the resource pool to be used later.
  24. /// Both the texture's view and storage should be completely unused.
  25. /// </summary>
  26. /// <param name="view">The texture's view</param>
  27. public void AddTexture(TextureView view)
  28. {
  29. lock (_lock)
  30. {
  31. List<DisposedTexture> list;
  32. if (!_textures.TryGetValue(view.Info, out list))
  33. {
  34. list = new List<DisposedTexture>();
  35. _textures.Add(view.Info, list);
  36. }
  37. list.Add(new DisposedTexture()
  38. {
  39. Info = view.Info,
  40. View = view,
  41. ScaleFactor = view.ScaleFactor,
  42. RemainingFrames = DisposedLiveFrames
  43. });
  44. }
  45. }
  46. /// <summary>
  47. /// Attempt to obtain a texture from the resource cache with the desired parameters.
  48. /// </summary>
  49. /// <param name="info">The creation info for the desired texture</param>
  50. /// <param name="scaleFactor">The scale factor for the desired texture</param>
  51. /// <returns>A TextureView with the description specified, or null if one was not found.</returns>
  52. public TextureView GetTextureOrNull(TextureCreateInfo info, float scaleFactor)
  53. {
  54. lock (_lock)
  55. {
  56. List<DisposedTexture> list;
  57. if (!_textures.TryGetValue(info, out list))
  58. {
  59. return null;
  60. }
  61. foreach (DisposedTexture texture in list)
  62. {
  63. if (scaleFactor == texture.ScaleFactor)
  64. {
  65. list.Remove(texture);
  66. return texture.View;
  67. }
  68. }
  69. return null;
  70. }
  71. }
  72. /// <summary>
  73. /// Update the pool, removing any resources that have expired.
  74. /// </summary>
  75. public void Tick()
  76. {
  77. lock (_lock)
  78. {
  79. foreach (List<DisposedTexture> list in _textures.Values)
  80. {
  81. for (int i = 0; i < list.Count; i++)
  82. {
  83. DisposedTexture tex = list[i];
  84. if (--tex.RemainingFrames < 0)
  85. {
  86. tex.View.Dispose();
  87. list.RemoveAt(i--);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. /// <summary>
  94. /// Disposes the resource pool.
  95. /// </summary>
  96. public void Dispose()
  97. {
  98. lock (_lock)
  99. {
  100. foreach (List<DisposedTexture> list in _textures.Values)
  101. {
  102. foreach (DisposedTexture texture in list)
  103. {
  104. texture.View.Dispose();
  105. }
  106. }
  107. _textures.Clear();
  108. }
  109. }
  110. }
  111. }