BackgroundResources.cs 3.0 KB

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