FramebufferParams.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 bool IsValidColorAttachment(int bindIndex)
  112. {
  113. return (uint)bindIndex < Constants.MaxRenderTargets && (_validColorAttachments & (1u << bindIndex)) != 0;
  114. }
  115. private static bool IsValidTextureView(ITexture texture)
  116. {
  117. return texture is TextureView view && view.Valid;
  118. }
  119. public ClearRect GetClearRect(Rectangle<int> scissor, int layer, int layerCount)
  120. {
  121. int x = scissor.X;
  122. int y = scissor.Y;
  123. int width = Math.Min((int)Width - scissor.X, scissor.Width);
  124. int height = Math.Min((int)Height - scissor.Y, scissor.Height);
  125. return new ClearRect(new Rect2D(new Offset2D(x, y), new Extent2D((uint)width, (uint)height)), (uint)layer, (uint)layerCount);
  126. }
  127. public unsafe Auto<DisposableFramebuffer> Create(Vk api, CommandBufferScoped cbs, Auto<DisposableRenderPass> renderPass)
  128. {
  129. ImageView* attachments = stackalloc ImageView[_attachments.Length];
  130. for (int i = 0; i < _attachments.Length; i++)
  131. {
  132. attachments[i] = _attachments[i].Get(cbs).Value;
  133. }
  134. var framebufferCreateInfo = new FramebufferCreateInfo()
  135. {
  136. SType = StructureType.FramebufferCreateInfo,
  137. RenderPass = renderPass.Get(cbs).Value,
  138. AttachmentCount = (uint)_attachments.Length,
  139. PAttachments = attachments,
  140. Width = Width,
  141. Height = Height,
  142. Layers = Layers
  143. };
  144. api.CreateFramebuffer(_device, framebufferCreateInfo, null, out var framebuffer).ThrowOnError();
  145. return new Auto<DisposableFramebuffer>(new DisposableFramebuffer(api, _device, framebuffer), null, _attachments);
  146. }
  147. public void UpdateModifications()
  148. {
  149. if (_colors != null)
  150. {
  151. for (int index = 0; index < _colors.Length; index++)
  152. {
  153. _colors[index].Storage.SetModification(
  154. AccessFlags.ColorAttachmentWriteBit,
  155. PipelineStageFlags.ColorAttachmentOutputBit);
  156. }
  157. }
  158. _depthStencil?.Storage.SetModification(
  159. AccessFlags.DepthStencilAttachmentWriteBit,
  160. PipelineStageFlags.ColorAttachmentOutputBit);
  161. }
  162. }
  163. }