BackgroundResources.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Silk.NET.Vulkan;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace Ryujinx.Graphics.Vulkan
  6. {
  7. class BackgroundResource : IDisposable
  8. {
  9. private readonly VulkanRenderer _gd;
  10. private Device _device;
  11. private CommandBufferPool _pool;
  12. private PersistentFlushBuffer _flushBuffer;
  13. public BackgroundResource(VulkanRenderer gd, Device device)
  14. {
  15. _gd = gd;
  16. _device = device;
  17. }
  18. public CommandBufferPool GetPool()
  19. {
  20. if (_pool == null)
  21. {
  22. bool useBackground = _gd.BackgroundQueue.Handle != 0 && _gd.Vendor != Vendor.Amd;
  23. Queue queue = useBackground ? _gd.BackgroundQueue : _gd.Queue;
  24. Lock queueLock = useBackground ? _gd.BackgroundQueueLock : _gd.QueueLock;
  25. lock (queueLock)
  26. {
  27. _pool = new CommandBufferPool(
  28. _gd.Api,
  29. _device,
  30. queue,
  31. queueLock,
  32. _gd.QueueFamilyIndex,
  33. _gd.IsQualcommProprietary,
  34. isLight: true);
  35. }
  36. }
  37. return _pool;
  38. }
  39. public PersistentFlushBuffer GetFlushBuffer()
  40. {
  41. _flushBuffer ??= new PersistentFlushBuffer(_gd);
  42. return _flushBuffer;
  43. }
  44. public void Dispose()
  45. {
  46. _pool?.Dispose();
  47. _flushBuffer?.Dispose();
  48. }
  49. }
  50. class BackgroundResources : IDisposable
  51. {
  52. private readonly VulkanRenderer _gd;
  53. private Device _device;
  54. private readonly Dictionary<Thread, BackgroundResource> _resources;
  55. public BackgroundResources(VulkanRenderer gd, Device device)
  56. {
  57. _gd = gd;
  58. _device = device;
  59. _resources = new Dictionary<Thread, BackgroundResource>();
  60. }
  61. private void Cleanup()
  62. {
  63. lock (_resources)
  64. {
  65. foreach (KeyValuePair<Thread, BackgroundResource> tuple in _resources)
  66. {
  67. if (!tuple.Key.IsAlive)
  68. {
  69. tuple.Value.Dispose();
  70. _resources.Remove(tuple.Key);
  71. }
  72. }
  73. }
  74. }
  75. public BackgroundResource Get()
  76. {
  77. Thread thread = Thread.CurrentThread;
  78. lock (_resources)
  79. {
  80. if (!_resources.TryGetValue(thread, out BackgroundResource resource))
  81. {
  82. Cleanup();
  83. resource = new BackgroundResource(_gd, _device);
  84. _resources[thread] = resource;
  85. }
  86. return resource;
  87. }
  88. }
  89. public void Dispose()
  90. {
  91. lock (_resources)
  92. {
  93. foreach (var resource in _resources.Values)
  94. {
  95. resource.Dispose();
  96. }
  97. }
  98. }
  99. }
  100. }