FramebufferParams.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. bool isDepthStencil,
  31. VkFormat format)
  32. {
  33. _device = device;
  34. _attachments = new[] { view };
  35. _validColorAttachments = 1u;
  36. Width = width;
  37. Height = height;
  38. Layers = 1;
  39. AttachmentSamples = new[] { 1u };
  40. AttachmentFormats = new[] { format };
  41. AttachmentIndices = new[] { 0 };
  42. AttachmentsCount = 1;
  43. HasDepthStencil = isDepthStencil;
  44. }
  45. public FramebufferParams(Device device, ITexture[] colors, ITexture depthStencil)
  46. {
  47. _device = device;
  48. int colorsCount = colors.Count(IsValidTextureView);
  49. int count = colorsCount + (IsValidTextureView(depthStencil) ? 1 : 0);
  50. _attachments = new Auto<DisposableImageView>[count];
  51. _colors = new TextureView[colorsCount];
  52. AttachmentSamples = new uint[count];
  53. AttachmentFormats = new VkFormat[count];
  54. AttachmentIndices = new int[count];
  55. MaxColorAttachmentIndex = colors.Length - 1;
  56. uint width = uint.MaxValue;
  57. uint height = uint.MaxValue;
  58. uint layers = uint.MaxValue;
  59. int index = 0;
  60. int bindIndex = 0;
  61. foreach (ITexture color in colors)
  62. {
  63. if (IsValidTextureView(color))
  64. {
  65. var texture = (TextureView)color;
  66. _attachments[index] = texture.GetImageViewForAttachment();
  67. _colors[index] = texture;
  68. _validColorAttachments |= 1u << bindIndex;
  69. AttachmentSamples[index] = (uint)texture.Info.Samples;
  70. AttachmentFormats[index] = texture.VkFormat;
  71. AttachmentIndices[index] = bindIndex;
  72. width = Math.Min(width, (uint)texture.Width);
  73. height = Math.Min(height, (uint)texture.Height);
  74. layers = Math.Min(layers, (uint)texture.Layers);
  75. if (++index >= colorsCount)
  76. {
  77. break;
  78. }
  79. }
  80. bindIndex++;
  81. }
  82. if (depthStencil is TextureView dsTexture && dsTexture.Valid)
  83. {
  84. _attachments[count - 1] = dsTexture.GetImageViewForAttachment();
  85. _depthStencil = dsTexture;
  86. AttachmentSamples[count - 1] = (uint)dsTexture.Info.Samples;
  87. AttachmentFormats[count - 1] = dsTexture.VkFormat;
  88. width = Math.Min(width, (uint)dsTexture.Width);
  89. height = Math.Min(height, (uint)dsTexture.Height);
  90. layers = Math.Min(layers, (uint)dsTexture.Layers);
  91. HasDepthStencil = true;
  92. }
  93. if (count == 0)
  94. {
  95. width = height = layers = 1;
  96. }
  97. Width = width;
  98. Height = height;
  99. Layers = layers;
  100. AttachmentsCount = count;
  101. }
  102. public Auto<DisposableImageView> GetAttachment(int index)
  103. {
  104. if ((uint)index >= _attachments.Length)
  105. {
  106. return null;
  107. }
  108. return _attachments[index];
  109. }
  110. public bool IsValidColorAttachment(int bindIndex)
  111. {
  112. return (uint)bindIndex < Constants.MaxRenderTargets && (_validColorAttachments & (1u << bindIndex)) != 0;
  113. }
  114. private static bool IsValidTextureView(ITexture texture)
  115. {
  116. return texture is TextureView view && view.Valid;
  117. }
  118. public ClearRect GetClearRect(Rectangle<int> scissor, int layer, int layerCount)
  119. {
  120. int x = scissor.X;
  121. int y = scissor.Y;
  122. int width = Math.Min((int)Width - scissor.X, scissor.Width);
  123. int height = Math.Min((int)Height - scissor.Y, scissor.Height);
  124. return new ClearRect(new Rect2D(new Offset2D(x, y), new Extent2D((uint)width, (uint)height)), (uint)layer, (uint)layerCount);
  125. }
  126. public unsafe Auto<DisposableFramebuffer> Create(Vk api, CommandBufferScoped cbs, Auto<DisposableRenderPass> renderPass)
  127. {
  128. ImageView* attachments = stackalloc ImageView[_attachments.Length];
  129. for (int i = 0; i < _attachments.Length; i++)
  130. {
  131. attachments[i] = _attachments[i].Get(cbs).Value;
  132. }
  133. var framebufferCreateInfo = new FramebufferCreateInfo()
  134. {
  135. SType = StructureType.FramebufferCreateInfo,
  136. RenderPass = renderPass.Get(cbs).Value,
  137. AttachmentCount = (uint)_attachments.Length,
  138. PAttachments = attachments,
  139. Width = Width,
  140. Height = Height,
  141. Layers = Layers
  142. };
  143. api.CreateFramebuffer(_device, framebufferCreateInfo, null, out var framebuffer).ThrowOnError();
  144. return new Auto<DisposableFramebuffer>(new DisposableFramebuffer(api, _device, framebuffer), null, _attachments);
  145. }
  146. public void UpdateModifications()
  147. {
  148. if (_colors != null)
  149. {
  150. for (int index = 0; index < _colors.Length; index++)
  151. {
  152. _colors[index].Storage.SetModification(
  153. AccessFlags.ColorAttachmentWriteBit,
  154. PipelineStageFlags.ColorAttachmentOutputBit);
  155. }
  156. }
  157. _depthStencil?.Storage.SetModification(
  158. AccessFlags.DepthStencilAttachmentWriteBit,
  159. PipelineStageFlags.ColorAttachmentOutputBit);
  160. }
  161. }
  162. }