FramebufferParams.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using Ryujinx.Graphics.GAL;
  2. using Silk.NET.Vulkan;
  3. using System;
  4. using System.Linq;
  5. using VkFormat = Silk.NET.Vulkan.Format;
  6. namespace Ryujinx.Graphics.Vulkan
  7. {
  8. class FramebufferParams
  9. {
  10. private readonly Device _device;
  11. private readonly Auto<DisposableImageView>[] _attachments;
  12. private readonly TextureView[] _colors;
  13. private readonly TextureView _depthStencil;
  14. private uint _validColorAttachments;
  15. public uint Width { get; }
  16. public uint Height { get; }
  17. public uint Layers { get; }
  18. public uint[] AttachmentSamples { get; }
  19. public VkFormat[] AttachmentFormats { get; }
  20. public int[] AttachmentIndices { get; }
  21. public int AttachmentsCount { get; }
  22. public int MaxColorAttachmentIndex { get; }
  23. public bool HasDepthStencil { get; }
  24. public int ColorAttachmentsCount => AttachmentsCount - (HasDepthStencil ? 1 : 0);
  25. public FramebufferParams(
  26. Device device,
  27. Auto<DisposableImageView> view,
  28. uint width,
  29. uint height,
  30. uint samples,
  31. bool isDepthStencil,
  32. VkFormat format)
  33. {
  34. _device = device;
  35. _attachments = new[] { view };
  36. _validColorAttachments = 1u;
  37. Width = width;
  38. Height = height;
  39. Layers = 1;
  40. AttachmentSamples = new[] { samples };
  41. AttachmentFormats = new[] { format };
  42. AttachmentIndices = new[] { 0 };
  43. AttachmentsCount = 1;
  44. HasDepthStencil = isDepthStencil;
  45. }
  46. public FramebufferParams(Device device, ITexture[] colors, ITexture depthStencil)
  47. {
  48. _device = device;
  49. int colorsCount = colors.Count(IsValidTextureView);
  50. int count = colorsCount + (IsValidTextureView(depthStencil) ? 1 : 0);
  51. _attachments = new Auto<DisposableImageView>[count];
  52. _colors = new TextureView[colorsCount];
  53. AttachmentSamples = new uint[count];
  54. AttachmentFormats = new VkFormat[count];
  55. AttachmentIndices = new int[count];
  56. MaxColorAttachmentIndex = colors.Length - 1;
  57. uint width = uint.MaxValue;
  58. uint height = uint.MaxValue;
  59. uint layers = uint.MaxValue;
  60. int index = 0;
  61. int bindIndex = 0;
  62. foreach (ITexture color in colors)
  63. {
  64. if (IsValidTextureView(color))
  65. {
  66. var texture = (TextureView)color;
  67. _attachments[index] = texture.GetImageViewForAttachment();
  68. _colors[index] = texture;
  69. _validColorAttachments |= 1u << bindIndex;
  70. AttachmentSamples[index] = (uint)texture.Info.Samples;
  71. AttachmentFormats[index] = texture.VkFormat;
  72. AttachmentIndices[index] = bindIndex;
  73. width = Math.Min(width, (uint)texture.Width);
  74. height = Math.Min(height, (uint)texture.Height);
  75. layers = Math.Min(layers, (uint)texture.Layers);
  76. if (++index >= colorsCount)
  77. {
  78. break;
  79. }
  80. }
  81. bindIndex++;
  82. }
  83. if (depthStencil is TextureView dsTexture && dsTexture.Valid)
  84. {
  85. _attachments[count - 1] = dsTexture.GetImageViewForAttachment();
  86. _depthStencil = dsTexture;
  87. AttachmentSamples[count - 1] = (uint)dsTexture.Info.Samples;
  88. AttachmentFormats[count - 1] = dsTexture.VkFormat;
  89. width = Math.Min(width, (uint)dsTexture.Width);
  90. height = Math.Min(height, (uint)dsTexture.Height);
  91. layers = Math.Min(layers, (uint)dsTexture.Layers);
  92. HasDepthStencil = true;
  93. }
  94. if (count == 0)
  95. {
  96. width = height = layers = 1;
  97. }
  98. Width = width;
  99. Height = height;
  100. Layers = layers;
  101. AttachmentsCount = count;
  102. }
  103. public Auto<DisposableImageView> GetAttachment(int index)
  104. {
  105. if ((uint)index >= _attachments.Length)
  106. {
  107. return null;
  108. }
  109. return _attachments[index];
  110. }
  111. public ComponentType GetAttachmentComponentType(int index)
  112. {
  113. if (_colors != null && (uint)index < _colors.Length)
  114. {
  115. var format = _colors[index].Info.Format;
  116. if (format.IsSint())
  117. {
  118. return ComponentType.SignedInteger;
  119. }
  120. else if (format.IsUint())
  121. {
  122. return ComponentType.UnsignedInteger;
  123. }
  124. }
  125. return ComponentType.Float;
  126. }
  127. public bool IsValidColorAttachment(int bindIndex)
  128. {
  129. return (uint)bindIndex < Constants.MaxRenderTargets && (_validColorAttachments & (1u << bindIndex)) != 0;
  130. }
  131. private static bool IsValidTextureView(ITexture texture)
  132. {
  133. return texture is TextureView view && view.Valid;
  134. }
  135. public ClearRect GetClearRect(Rectangle<int> scissor, int layer, int layerCount)
  136. {
  137. int x = scissor.X;
  138. int y = scissor.Y;
  139. int width = Math.Min((int)Width - scissor.X, scissor.Width);
  140. int height = Math.Min((int)Height - scissor.Y, scissor.Height);
  141. return new ClearRect(new Rect2D(new Offset2D(x, y), new Extent2D((uint)width, (uint)height)), (uint)layer, (uint)layerCount);
  142. }
  143. public unsafe Auto<DisposableFramebuffer> Create(Vk api, CommandBufferScoped cbs, Auto<DisposableRenderPass> renderPass)
  144. {
  145. ImageView* attachments = stackalloc ImageView[_attachments.Length];
  146. for (int i = 0; i < _attachments.Length; i++)
  147. {
  148. attachments[i] = _attachments[i].Get(cbs).Value;
  149. }
  150. var framebufferCreateInfo = new FramebufferCreateInfo()
  151. {
  152. SType = StructureType.FramebufferCreateInfo,
  153. RenderPass = renderPass.Get(cbs).Value,
  154. AttachmentCount = (uint)_attachments.Length,
  155. PAttachments = attachments,
  156. Width = Width,
  157. Height = Height,
  158. Layers = Layers
  159. };
  160. api.CreateFramebuffer(_device, framebufferCreateInfo, null, out var framebuffer).ThrowOnError();
  161. return new Auto<DisposableFramebuffer>(new DisposableFramebuffer(api, _device, framebuffer), null, _attachments);
  162. }
  163. public void UpdateModifications()
  164. {
  165. if (_colors != null)
  166. {
  167. for (int index = 0; index < _colors.Length; index++)
  168. {
  169. _colors[index].Storage.SetModification(
  170. AccessFlags.ColorAttachmentWriteBit,
  171. PipelineStageFlags.ColorAttachmentOutputBit);
  172. }
  173. }
  174. _depthStencil?.Storage.SetModification(
  175. AccessFlags.DepthStencilAttachmentWriteBit,
  176. PipelineStageFlags.ColorAttachmentOutputBit);
  177. }
  178. }
  179. }