TextureArray.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using Ryujinx.Graphics.GAL;
  2. using Silk.NET.Vulkan;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Graphics.Vulkan
  6. {
  7. class TextureArray : ResourceArray, ITextureArray
  8. {
  9. private readonly VulkanRenderer _gd;
  10. private struct TextureRef
  11. {
  12. public TextureStorage Storage;
  13. public Auto<DisposableImageView> View;
  14. public Auto<DisposableSampler> Sampler;
  15. }
  16. private readonly TextureRef[] _textureRefs;
  17. private readonly TextureBuffer[] _bufferTextureRefs;
  18. private readonly DescriptorImageInfo[] _textures;
  19. private readonly BufferView[] _bufferTextures;
  20. private HashSet<TextureStorage> _storages;
  21. private int _cachedCommandBufferIndex;
  22. private int _cachedSubmissionCount;
  23. private readonly bool _isBuffer;
  24. public TextureArray(VulkanRenderer gd, int size, bool isBuffer)
  25. {
  26. _gd = gd;
  27. if (isBuffer)
  28. {
  29. _bufferTextureRefs = new TextureBuffer[size];
  30. _bufferTextures = new BufferView[size];
  31. }
  32. else
  33. {
  34. _textureRefs = new TextureRef[size];
  35. _textures = new DescriptorImageInfo[size];
  36. }
  37. _storages = null;
  38. _cachedCommandBufferIndex = -1;
  39. _cachedSubmissionCount = 0;
  40. _isBuffer = isBuffer;
  41. }
  42. public void SetSamplers(int index, ISampler[] samplers)
  43. {
  44. for (int i = 0; i < samplers.Length; i++)
  45. {
  46. ISampler sampler = samplers[i];
  47. if (sampler is SamplerHolder samplerHolder)
  48. {
  49. _textureRefs[index + i].Sampler = samplerHolder.GetSampler();
  50. }
  51. else
  52. {
  53. _textureRefs[index + i].Sampler = default;
  54. }
  55. }
  56. SetDirty();
  57. }
  58. public void SetTextures(int index, ITexture[] textures)
  59. {
  60. for (int i = 0; i < textures.Length; i++)
  61. {
  62. ITexture texture = textures[i];
  63. if (texture is TextureBuffer textureBuffer)
  64. {
  65. _bufferTextureRefs[index + i] = textureBuffer;
  66. }
  67. else if (texture is TextureView view)
  68. {
  69. _textureRefs[index + i].Storage = view.Storage;
  70. _textureRefs[index + i].View = view.GetImageView();
  71. }
  72. else if (!_isBuffer)
  73. {
  74. _textureRefs[index + i].Storage = null;
  75. _textureRefs[index + i].View = default;
  76. }
  77. else
  78. {
  79. _bufferTextureRefs[index + i] = null;
  80. }
  81. }
  82. SetDirty();
  83. }
  84. private void SetDirty()
  85. {
  86. _cachedCommandBufferIndex = -1;
  87. _storages = null;
  88. SetDirty(_gd, isImage: false);
  89. }
  90. public void QueueWriteToReadBarriers(CommandBufferScoped cbs, PipelineStageFlags stageFlags)
  91. {
  92. HashSet<TextureStorage> storages = _storages;
  93. if (storages == null)
  94. {
  95. storages = new HashSet<TextureStorage>();
  96. for (int index = 0; index < _textureRefs.Length; index++)
  97. {
  98. if (_textureRefs[index].Storage != null)
  99. {
  100. storages.Add(_textureRefs[index].Storage);
  101. }
  102. }
  103. _storages = storages;
  104. }
  105. foreach (TextureStorage storage in storages)
  106. {
  107. storage.QueueWriteToReadBarrier(cbs, AccessFlags.ShaderReadBit, stageFlags);
  108. }
  109. }
  110. public ReadOnlySpan<DescriptorImageInfo> GetImageInfos(VulkanRenderer gd, CommandBufferScoped cbs, TextureView dummyTexture, SamplerHolder dummySampler)
  111. {
  112. int submissionCount = gd.CommandBufferPool.GetSubmissionCount(cbs.CommandBufferIndex);
  113. Span<DescriptorImageInfo> textures = _textures;
  114. if (cbs.CommandBufferIndex == _cachedCommandBufferIndex && submissionCount == _cachedSubmissionCount)
  115. {
  116. return textures;
  117. }
  118. _cachedCommandBufferIndex = cbs.CommandBufferIndex;
  119. _cachedSubmissionCount = submissionCount;
  120. for (int i = 0; i < textures.Length; i++)
  121. {
  122. ref var texture = ref textures[i];
  123. ref var refs = ref _textureRefs[i];
  124. if (i > 0 && _textureRefs[i - 1].View == refs.View && _textureRefs[i - 1].Sampler == refs.Sampler)
  125. {
  126. texture = textures[i - 1];
  127. continue;
  128. }
  129. texture.ImageLayout = ImageLayout.General;
  130. texture.ImageView = refs.View?.Get(cbs).Value ?? default;
  131. texture.Sampler = refs.Sampler?.Get(cbs).Value ?? default;
  132. if (texture.ImageView.Handle == 0)
  133. {
  134. texture.ImageView = dummyTexture.GetImageView().Get(cbs).Value;
  135. }
  136. if (texture.Sampler.Handle == 0)
  137. {
  138. texture.Sampler = dummySampler.GetSampler().Get(cbs).Value;
  139. }
  140. }
  141. return textures;
  142. }
  143. public ReadOnlySpan<BufferView> GetBufferViews(CommandBufferScoped cbs)
  144. {
  145. Span<BufferView> bufferTextures = _bufferTextures;
  146. for (int i = 0; i < bufferTextures.Length; i++)
  147. {
  148. bufferTextures[i] = _bufferTextureRefs[i]?.GetBufferView(cbs, false) ?? default;
  149. }
  150. return bufferTextures;
  151. }
  152. public DescriptorSet[] GetDescriptorSets(
  153. Device device,
  154. CommandBufferScoped cbs,
  155. DescriptorSetTemplateUpdater templateUpdater,
  156. ShaderCollection program,
  157. int setIndex,
  158. TextureView dummyTexture,
  159. SamplerHolder dummySampler)
  160. {
  161. if (TryGetCachedDescriptorSets(cbs, program, setIndex, out DescriptorSet[] sets))
  162. {
  163. // We still need to ensure the current command buffer holds a reference to all used textures.
  164. if (!_isBuffer)
  165. {
  166. GetImageInfos(_gd, cbs, dummyTexture, dummySampler);
  167. }
  168. else
  169. {
  170. GetBufferViews(cbs);
  171. }
  172. return sets;
  173. }
  174. DescriptorSetTemplate template = program.Templates[setIndex];
  175. DescriptorSetTemplateWriter tu = templateUpdater.Begin(template);
  176. if (!_isBuffer)
  177. {
  178. tu.Push(GetImageInfos(_gd, cbs, dummyTexture, dummySampler));
  179. }
  180. else
  181. {
  182. tu.Push(GetBufferViews(cbs));
  183. }
  184. templateUpdater.Commit(_gd, device, sets[0]);
  185. return sets;
  186. }
  187. }
  188. }