PipelineLayoutCacheEntry.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using Ryujinx.Graphics.GAL;
  2. using Silk.NET.Vulkan;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. namespace Ryujinx.Graphics.Vulkan
  7. {
  8. class PipelineLayoutCacheEntry
  9. {
  10. // Those were adjusted based on current descriptor usage and the descriptor counts usually used on pipeline layouts.
  11. // It might be a good idea to tweak them again if those change, or maybe find a way to calculate an optimal value dynamically.
  12. private const uint DefaultUniformBufferPoolCapacity = 19 * DescriptorSetManager.MaxSets;
  13. private const uint DefaultStorageBufferPoolCapacity = 16 * DescriptorSetManager.MaxSets;
  14. private const uint DefaultTexturePoolCapacity = 128 * DescriptorSetManager.MaxSets;
  15. private const uint DefaultImagePoolCapacity = 8 * DescriptorSetManager.MaxSets;
  16. private const int MaxPoolSizesPerSet = 2;
  17. private readonly VulkanRenderer _gd;
  18. private readonly Device _device;
  19. public DescriptorSetLayout[] DescriptorSetLayouts { get; }
  20. public PipelineLayout PipelineLayout { get; }
  21. private readonly int[] _consumedDescriptorsPerSet;
  22. private readonly List<Auto<DescriptorSetCollection>>[][] _dsCache;
  23. private List<Auto<DescriptorSetCollection>>[] _currentDsCache;
  24. private readonly int[] _dsCacheCursor;
  25. private int _dsLastCbIndex;
  26. private int _dsLastSubmissionCount;
  27. private readonly Dictionary<long, DescriptorSetTemplate> _pdTemplates;
  28. private readonly ResourceDescriptorCollection _pdDescriptors;
  29. private long _lastPdUsage;
  30. private DescriptorSetTemplate _lastPdTemplate;
  31. private PipelineLayoutCacheEntry(VulkanRenderer gd, Device device, int setsCount)
  32. {
  33. _gd = gd;
  34. _device = device;
  35. _dsCache = new List<Auto<DescriptorSetCollection>>[CommandBufferPool.MaxCommandBuffers][];
  36. for (int i = 0; i < CommandBufferPool.MaxCommandBuffers; i++)
  37. {
  38. _dsCache[i] = new List<Auto<DescriptorSetCollection>>[setsCount];
  39. for (int j = 0; j < _dsCache[i].Length; j++)
  40. {
  41. _dsCache[i][j] = new List<Auto<DescriptorSetCollection>>();
  42. }
  43. }
  44. _dsCacheCursor = new int[setsCount];
  45. }
  46. public PipelineLayoutCacheEntry(
  47. VulkanRenderer gd,
  48. Device device,
  49. ReadOnlyCollection<ResourceDescriptorCollection> setDescriptors,
  50. bool usePushDescriptors) : this(gd, device, setDescriptors.Count)
  51. {
  52. (DescriptorSetLayouts, PipelineLayout) = PipelineLayoutFactory.Create(gd, device, setDescriptors, usePushDescriptors);
  53. _consumedDescriptorsPerSet = new int[setDescriptors.Count];
  54. for (int setIndex = 0; setIndex < setDescriptors.Count; setIndex++)
  55. {
  56. int count = 0;
  57. foreach (var descriptor in setDescriptors[setIndex].Descriptors)
  58. {
  59. count += descriptor.Count;
  60. }
  61. _consumedDescriptorsPerSet[setIndex] = count;
  62. }
  63. if (usePushDescriptors)
  64. {
  65. _pdDescriptors = setDescriptors[0];
  66. _pdTemplates = new();
  67. }
  68. }
  69. public void UpdateCommandBufferIndex(int commandBufferIndex)
  70. {
  71. int submissionCount = _gd.CommandBufferPool.GetSubmissionCount(commandBufferIndex);
  72. if (_dsLastCbIndex != commandBufferIndex || _dsLastSubmissionCount != submissionCount)
  73. {
  74. _dsLastCbIndex = commandBufferIndex;
  75. _dsLastSubmissionCount = submissionCount;
  76. Array.Clear(_dsCacheCursor);
  77. }
  78. _currentDsCache = _dsCache[commandBufferIndex];
  79. }
  80. public Auto<DescriptorSetCollection> GetNewDescriptorSetCollection(int setIndex, out bool isNew)
  81. {
  82. var list = _currentDsCache[setIndex];
  83. int index = _dsCacheCursor[setIndex]++;
  84. if (index == list.Count)
  85. {
  86. Span<DescriptorPoolSize> poolSizes = stackalloc DescriptorPoolSize[MaxPoolSizesPerSet];
  87. poolSizes = GetDescriptorPoolSizes(poolSizes, setIndex);
  88. int consumedDescriptors = _consumedDescriptorsPerSet[setIndex];
  89. var dsc = _gd.DescriptorSetManager.AllocateDescriptorSet(
  90. _gd.Api,
  91. DescriptorSetLayouts[setIndex],
  92. poolSizes,
  93. setIndex,
  94. consumedDescriptors,
  95. false);
  96. list.Add(dsc);
  97. isNew = true;
  98. return dsc;
  99. }
  100. isNew = false;
  101. return list[index];
  102. }
  103. private static Span<DescriptorPoolSize> GetDescriptorPoolSizes(Span<DescriptorPoolSize> output, int setIndex)
  104. {
  105. int count = 1;
  106. switch (setIndex)
  107. {
  108. case PipelineBase.UniformSetIndex:
  109. output[0] = new(DescriptorType.UniformBuffer, DefaultUniformBufferPoolCapacity);
  110. break;
  111. case PipelineBase.StorageSetIndex:
  112. output[0] = new(DescriptorType.StorageBuffer, DefaultStorageBufferPoolCapacity);
  113. break;
  114. case PipelineBase.TextureSetIndex:
  115. output[0] = new(DescriptorType.CombinedImageSampler, DefaultTexturePoolCapacity);
  116. output[1] = new(DescriptorType.UniformTexelBuffer, DefaultTexturePoolCapacity);
  117. count = 2;
  118. break;
  119. case PipelineBase.ImageSetIndex:
  120. output[0] = new(DescriptorType.StorageImage, DefaultImagePoolCapacity);
  121. output[1] = new(DescriptorType.StorageTexelBuffer, DefaultImagePoolCapacity);
  122. count = 2;
  123. break;
  124. }
  125. return output[..count];
  126. }
  127. public DescriptorSetTemplate GetPushDescriptorTemplate(PipelineBindPoint pbp, long updateMask)
  128. {
  129. if (_lastPdUsage == updateMask && _lastPdTemplate != null)
  130. {
  131. // Most likely result is that it asks to update the same buffers.
  132. return _lastPdTemplate;
  133. }
  134. if (!_pdTemplates.TryGetValue(updateMask, out DescriptorSetTemplate template))
  135. {
  136. template = new DescriptorSetTemplate(_gd, _device, _pdDescriptors, updateMask, this, pbp, 0);
  137. _pdTemplates.Add(updateMask, template);
  138. }
  139. _lastPdUsage = updateMask;
  140. _lastPdTemplate = template;
  141. return template;
  142. }
  143. protected virtual unsafe void Dispose(bool disposing)
  144. {
  145. if (disposing)
  146. {
  147. if (_pdTemplates != null)
  148. {
  149. foreach (DescriptorSetTemplate template in _pdTemplates.Values)
  150. {
  151. template.Dispose();
  152. }
  153. }
  154. for (int i = 0; i < _dsCache.Length; i++)
  155. {
  156. for (int j = 0; j < _dsCache[i].Length; j++)
  157. {
  158. for (int k = 0; k < _dsCache[i][j].Count; k++)
  159. {
  160. _dsCache[i][j][k].Dispose();
  161. }
  162. _dsCache[i][j].Clear();
  163. }
  164. }
  165. _gd.Api.DestroyPipelineLayout(_device, PipelineLayout, null);
  166. for (int i = 0; i < DescriptorSetLayouts.Length; i++)
  167. {
  168. _gd.Api.DestroyDescriptorSetLayout(_device, DescriptorSetLayouts[i], null);
  169. }
  170. }
  171. }
  172. public void Dispose()
  173. {
  174. Dispose(true);
  175. }
  176. }
  177. }