PipelineLayoutCacheEntry.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 PipelineLayoutCacheEntry(VulkanRenderer gd, Device device, int setsCount)
  28. {
  29. _gd = gd;
  30. _device = device;
  31. _dsCache = new List<Auto<DescriptorSetCollection>>[CommandBufferPool.MaxCommandBuffers][];
  32. for (int i = 0; i < CommandBufferPool.MaxCommandBuffers; i++)
  33. {
  34. _dsCache[i] = new List<Auto<DescriptorSetCollection>>[setsCount];
  35. for (int j = 0; j < _dsCache[i].Length; j++)
  36. {
  37. _dsCache[i][j] = new List<Auto<DescriptorSetCollection>>();
  38. }
  39. }
  40. _dsCacheCursor = new int[setsCount];
  41. }
  42. public PipelineLayoutCacheEntry(
  43. VulkanRenderer gd,
  44. Device device,
  45. ReadOnlyCollection<ResourceDescriptorCollection> setDescriptors,
  46. bool usePushDescriptors) : this(gd, device, setDescriptors.Count)
  47. {
  48. (DescriptorSetLayouts, PipelineLayout) = PipelineLayoutFactory.Create(gd, device, setDescriptors, usePushDescriptors);
  49. _consumedDescriptorsPerSet = new int[setDescriptors.Count];
  50. for (int setIndex = 0; setIndex < setDescriptors.Count; setIndex++)
  51. {
  52. int count = 0;
  53. foreach (var descriptor in setDescriptors[setIndex].Descriptors)
  54. {
  55. count += descriptor.Count;
  56. }
  57. _consumedDescriptorsPerSet[setIndex] = count;
  58. }
  59. }
  60. public void UpdateCommandBufferIndex(int commandBufferIndex)
  61. {
  62. int submissionCount = _gd.CommandBufferPool.GetSubmissionCount(commandBufferIndex);
  63. if (_dsLastCbIndex != commandBufferIndex || _dsLastSubmissionCount != submissionCount)
  64. {
  65. _dsLastCbIndex = commandBufferIndex;
  66. _dsLastSubmissionCount = submissionCount;
  67. Array.Clear(_dsCacheCursor);
  68. }
  69. _currentDsCache = _dsCache[commandBufferIndex];
  70. }
  71. public Auto<DescriptorSetCollection> GetNewDescriptorSetCollection(int setIndex, out bool isNew)
  72. {
  73. var list = _currentDsCache[setIndex];
  74. int index = _dsCacheCursor[setIndex]++;
  75. if (index == list.Count)
  76. {
  77. Span<DescriptorPoolSize> poolSizes = stackalloc DescriptorPoolSize[MaxPoolSizesPerSet];
  78. poolSizes = GetDescriptorPoolSizes(poolSizes, setIndex);
  79. int consumedDescriptors = _consumedDescriptorsPerSet[setIndex];
  80. var dsc = _gd.DescriptorSetManager.AllocateDescriptorSet(
  81. _gd.Api,
  82. DescriptorSetLayouts[setIndex],
  83. poolSizes,
  84. setIndex,
  85. consumedDescriptors,
  86. false);
  87. list.Add(dsc);
  88. isNew = true;
  89. return dsc;
  90. }
  91. isNew = false;
  92. return list[index];
  93. }
  94. private static Span<DescriptorPoolSize> GetDescriptorPoolSizes(Span<DescriptorPoolSize> output, int setIndex)
  95. {
  96. int count = 1;
  97. switch (setIndex)
  98. {
  99. case PipelineBase.UniformSetIndex:
  100. output[0] = new(DescriptorType.UniformBuffer, DefaultUniformBufferPoolCapacity);
  101. break;
  102. case PipelineBase.StorageSetIndex:
  103. output[0] = new(DescriptorType.StorageBuffer, DefaultStorageBufferPoolCapacity);
  104. break;
  105. case PipelineBase.TextureSetIndex:
  106. output[0] = new(DescriptorType.CombinedImageSampler, DefaultTexturePoolCapacity);
  107. output[1] = new(DescriptorType.UniformTexelBuffer, DefaultTexturePoolCapacity);
  108. count = 2;
  109. break;
  110. case PipelineBase.ImageSetIndex:
  111. output[0] = new(DescriptorType.StorageImage, DefaultImagePoolCapacity);
  112. output[1] = new(DescriptorType.StorageTexelBuffer, DefaultImagePoolCapacity);
  113. count = 2;
  114. break;
  115. }
  116. return output[..count];
  117. }
  118. protected virtual unsafe void Dispose(bool disposing)
  119. {
  120. if (disposing)
  121. {
  122. for (int i = 0; i < _dsCache.Length; i++)
  123. {
  124. for (int j = 0; j < _dsCache[i].Length; j++)
  125. {
  126. for (int k = 0; k < _dsCache[i][j].Count; k++)
  127. {
  128. _dsCache[i][j][k].Dispose();
  129. }
  130. _dsCache[i][j].Clear();
  131. }
  132. }
  133. _gd.Api.DestroyPipelineLayout(_device, PipelineLayout, null);
  134. for (int i = 0; i < DescriptorSetLayouts.Length; i++)
  135. {
  136. _gd.Api.DestroyDescriptorSetLayout(_device, DescriptorSetLayouts[i], null);
  137. }
  138. }
  139. }
  140. public void Dispose()
  141. {
  142. Dispose(true);
  143. }
  144. }
  145. }