ImageArray.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 ImageArray : IImageArray
  8. {
  9. private readonly VulkanRenderer _gd;
  10. private record struct TextureRef
  11. {
  12. public TextureStorage Storage;
  13. public TextureView View;
  14. public GAL.Format ImageFormat;
  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 bool Bound;
  25. public ImageArray(VulkanRenderer gd, int size, bool isBuffer)
  26. {
  27. _gd = gd;
  28. if (isBuffer)
  29. {
  30. _bufferTextureRefs = new TextureBuffer[size];
  31. _bufferTextures = new BufferView[size];
  32. }
  33. else
  34. {
  35. _textureRefs = new TextureRef[size];
  36. _textures = new DescriptorImageInfo[size];
  37. }
  38. _storages = null;
  39. _cachedCommandBufferIndex = -1;
  40. _cachedSubmissionCount = 0;
  41. _isBuffer = isBuffer;
  42. }
  43. public void SetFormats(int index, GAL.Format[] imageFormats)
  44. {
  45. for (int i = 0; i < imageFormats.Length; i++)
  46. {
  47. _textureRefs[index + i].ImageFormat = imageFormats[i];
  48. }
  49. SetDirty();
  50. }
  51. public void SetImages(int index, ITexture[] images)
  52. {
  53. for (int i = 0; i < images.Length; i++)
  54. {
  55. ITexture image = images[i];
  56. if (image is TextureBuffer textureBuffer)
  57. {
  58. _bufferTextureRefs[index + i] = textureBuffer;
  59. }
  60. else if (image is TextureView view)
  61. {
  62. _textureRefs[index + i].Storage = view.Storage;
  63. _textureRefs[index + i].View = view;
  64. }
  65. else if (!_isBuffer)
  66. {
  67. _textureRefs[index + i].Storage = null;
  68. _textureRefs[index + i].View = default;
  69. }
  70. else
  71. {
  72. _bufferTextureRefs[index + i] = null;
  73. }
  74. }
  75. SetDirty();
  76. }
  77. private void SetDirty()
  78. {
  79. _cachedCommandBufferIndex = -1;
  80. _storages = null;
  81. _gd.PipelineInternal.ForceImageDirty();
  82. }
  83. public void QueueWriteToReadBarriers(CommandBufferScoped cbs, PipelineStageFlags stageFlags)
  84. {
  85. HashSet<TextureStorage> storages = _storages;
  86. if (storages == null)
  87. {
  88. storages = new HashSet<TextureStorage>();
  89. for (int index = 0; index < _textureRefs.Length; index++)
  90. {
  91. if (_textureRefs[index].Storage != null)
  92. {
  93. storages.Add(_textureRefs[index].Storage);
  94. }
  95. }
  96. _storages = storages;
  97. }
  98. foreach (TextureStorage storage in storages)
  99. {
  100. storage.QueueWriteToReadBarrier(cbs, AccessFlags.ShaderReadBit, stageFlags);
  101. }
  102. }
  103. public ReadOnlySpan<DescriptorImageInfo> GetImageInfos(VulkanRenderer gd, CommandBufferScoped cbs, TextureView dummyTexture)
  104. {
  105. int submissionCount = gd.CommandBufferPool.GetSubmissionCount(cbs.CommandBufferIndex);
  106. Span<DescriptorImageInfo> textures = _textures;
  107. if (cbs.CommandBufferIndex == _cachedCommandBufferIndex && submissionCount == _cachedSubmissionCount)
  108. {
  109. return textures;
  110. }
  111. _cachedCommandBufferIndex = cbs.CommandBufferIndex;
  112. _cachedSubmissionCount = submissionCount;
  113. for (int i = 0; i < textures.Length; i++)
  114. {
  115. ref var texture = ref textures[i];
  116. ref var refs = ref _textureRefs[i];
  117. if (i > 0 && _textureRefs[i - 1].View == refs.View && _textureRefs[i - 1].ImageFormat == refs.ImageFormat)
  118. {
  119. texture = textures[i - 1];
  120. continue;
  121. }
  122. texture.ImageLayout = ImageLayout.General;
  123. texture.ImageView = refs.View?.GetView(refs.ImageFormat).GetIdentityImageView().Get(cbs).Value ?? default;
  124. if (texture.ImageView.Handle == 0)
  125. {
  126. texture.ImageView = dummyTexture.GetImageView().Get(cbs).Value;
  127. }
  128. }
  129. return textures;
  130. }
  131. public ReadOnlySpan<BufferView> GetBufferViews(CommandBufferScoped cbs)
  132. {
  133. Span<BufferView> bufferTextures = _bufferTextures;
  134. for (int i = 0; i < bufferTextures.Length; i++)
  135. {
  136. bufferTextures[i] = _bufferTextureRefs[i]?.GetBufferView(cbs, _textureRefs[i].ImageFormat, true) ?? default;
  137. }
  138. return bufferTextures;
  139. }
  140. }
  141. }