DescriptorSetManager.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using Silk.NET.Vulkan;
  2. using System;
  3. using System.Diagnostics;
  4. namespace Ryujinx.Graphics.Vulkan
  5. {
  6. class DescriptorSetManager : IDisposable
  7. {
  8. private const uint DescriptorPoolMultiplier = 16;
  9. public class DescriptorPoolHolder : IDisposable
  10. {
  11. public Vk Api { get; }
  12. public Device Device { get; }
  13. private readonly DescriptorPool _pool;
  14. private readonly uint _capacity;
  15. private int _totalSets;
  16. private int _setsInUse;
  17. private bool _done;
  18. public unsafe DescriptorPoolHolder(Vk api, Device device)
  19. {
  20. Api = api;
  21. Device = device;
  22. var poolSizes = new DescriptorPoolSize[]
  23. {
  24. new DescriptorPoolSize(DescriptorType.UniformBuffer, (1 + Constants.MaxUniformBufferBindings) * DescriptorPoolMultiplier),
  25. new DescriptorPoolSize(DescriptorType.StorageBuffer, Constants.MaxStorageBufferBindings * DescriptorPoolMultiplier),
  26. new DescriptorPoolSize(DescriptorType.CombinedImageSampler, Constants.MaxTextureBindings * DescriptorPoolMultiplier),
  27. new DescriptorPoolSize(DescriptorType.StorageImage, Constants.MaxImageBindings * DescriptorPoolMultiplier),
  28. new DescriptorPoolSize(DescriptorType.UniformTexelBuffer, Constants.MaxTextureBindings * DescriptorPoolMultiplier),
  29. new DescriptorPoolSize(DescriptorType.StorageTexelBuffer, Constants.MaxImageBindings * DescriptorPoolMultiplier)
  30. };
  31. uint maxSets = (uint)poolSizes.Length * DescriptorPoolMultiplier;
  32. _capacity = maxSets;
  33. fixed (DescriptorPoolSize* pPoolsSize = poolSizes)
  34. {
  35. var descriptorPoolCreateInfo = new DescriptorPoolCreateInfo()
  36. {
  37. SType = StructureType.DescriptorPoolCreateInfo,
  38. MaxSets = maxSets,
  39. PoolSizeCount = (uint)poolSizes.Length,
  40. PPoolSizes = pPoolsSize
  41. };
  42. Api.CreateDescriptorPool(device, descriptorPoolCreateInfo, null, out _pool).ThrowOnError();
  43. }
  44. }
  45. public unsafe DescriptorSetCollection AllocateDescriptorSets(ReadOnlySpan<DescriptorSetLayout> layouts)
  46. {
  47. TryAllocateDescriptorSets(layouts, isTry: false, out var dsc);
  48. return dsc;
  49. }
  50. public bool TryAllocateDescriptorSets(ReadOnlySpan<DescriptorSetLayout> layouts, out DescriptorSetCollection dsc)
  51. {
  52. return TryAllocateDescriptorSets(layouts, isTry: true, out dsc);
  53. }
  54. private unsafe bool TryAllocateDescriptorSets(ReadOnlySpan<DescriptorSetLayout> layouts, bool isTry, out DescriptorSetCollection dsc)
  55. {
  56. Debug.Assert(!_done);
  57. DescriptorSet[] descriptorSets = new DescriptorSet[layouts.Length];
  58. fixed (DescriptorSet* pDescriptorSets = descriptorSets)
  59. {
  60. fixed (DescriptorSetLayout* pLayouts = layouts)
  61. {
  62. var descriptorSetAllocateInfo = new DescriptorSetAllocateInfo()
  63. {
  64. SType = StructureType.DescriptorSetAllocateInfo,
  65. DescriptorPool = _pool,
  66. DescriptorSetCount = (uint)layouts.Length,
  67. PSetLayouts = pLayouts
  68. };
  69. var result = Api.AllocateDescriptorSets(Device, &descriptorSetAllocateInfo, pDescriptorSets);
  70. if (isTry && result == Result.ErrorOutOfPoolMemory)
  71. {
  72. _totalSets = (int)_capacity;
  73. _done = true;
  74. DestroyIfDone();
  75. dsc = default;
  76. return false;
  77. }
  78. result.ThrowOnError();
  79. }
  80. }
  81. _totalSets += layouts.Length;
  82. _setsInUse += layouts.Length;
  83. dsc = new DescriptorSetCollection(this, descriptorSets);
  84. return true;
  85. }
  86. public void FreeDescriptorSets(DescriptorSetCollection dsc)
  87. {
  88. _setsInUse -= dsc.SetsCount;
  89. Debug.Assert(_setsInUse >= 0);
  90. DestroyIfDone();
  91. }
  92. public bool CanFit(int count)
  93. {
  94. if (_totalSets + count <= _capacity)
  95. {
  96. return true;
  97. }
  98. _done = true;
  99. DestroyIfDone();
  100. return false;
  101. }
  102. private unsafe void DestroyIfDone()
  103. {
  104. if (_done && _setsInUse == 0)
  105. {
  106. Api.DestroyDescriptorPool(Device, _pool, null);
  107. }
  108. }
  109. protected virtual void Dispose(bool disposing)
  110. {
  111. if (disposing)
  112. {
  113. unsafe
  114. {
  115. Api.DestroyDescriptorPool(Device, _pool, null);
  116. }
  117. }
  118. }
  119. public void Dispose()
  120. {
  121. Dispose(true);
  122. }
  123. }
  124. private readonly Device _device;
  125. private DescriptorPoolHolder _currentPool;
  126. public DescriptorSetManager(Device device)
  127. {
  128. _device = device;
  129. }
  130. public Auto<DescriptorSetCollection> AllocateDescriptorSet(Vk api, DescriptorSetLayout layout)
  131. {
  132. Span<DescriptorSetLayout> layouts = stackalloc DescriptorSetLayout[1];
  133. layouts[0] = layout;
  134. return AllocateDescriptorSets(api, layouts);
  135. }
  136. public Auto<DescriptorSetCollection> AllocateDescriptorSets(Vk api, ReadOnlySpan<DescriptorSetLayout> layouts)
  137. {
  138. // If we fail the first time, just create a new pool and try again.
  139. if (!GetPool(api, layouts.Length).TryAllocateDescriptorSets(layouts, out var dsc))
  140. {
  141. dsc = GetPool(api, layouts.Length).AllocateDescriptorSets(layouts);
  142. }
  143. return new Auto<DescriptorSetCollection>(dsc);
  144. }
  145. private DescriptorPoolHolder GetPool(Vk api, int requiredCount)
  146. {
  147. if (_currentPool == null || !_currentPool.CanFit(requiredCount))
  148. {
  149. _currentPool = new DescriptorPoolHolder(api, _device);
  150. }
  151. return _currentPool;
  152. }
  153. protected virtual void Dispose(bool disposing)
  154. {
  155. if (disposing)
  156. {
  157. unsafe
  158. {
  159. _currentPool?.Dispose();
  160. }
  161. }
  162. }
  163. public void Dispose()
  164. {
  165. Dispose(true);
  166. }
  167. }
  168. }