FramebufferParams.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 => AttachmentIndices.Length > 0 ? AttachmentIndices[AttachmentIndices.Length - 1] : -1;
  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 = isDepthStencil ? 0u : 1u;
  37. Width = width;
  38. Height = height;
  39. Layers = 1;
  40. AttachmentSamples = new[] { samples };
  41. AttachmentFormats = new[] { format };
  42. AttachmentIndices = isDepthStencil ? Array.Empty<int>() : 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[colorsCount];
  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 ComponentType GetAttachmentComponentType(int index)
  111. {
  112. if (_colors != null && (uint)index < _colors.Length)
  113. {
  114. var format = _colors[index].Info.Format;
  115. if (format.IsSint())
  116. {
  117. return ComponentType.SignedInteger;
  118. }
  119. else if (format.IsUint())
  120. {
  121. return ComponentType.UnsignedInteger;
  122. }
  123. }
  124. return ComponentType.Float;
  125. }
  126. public bool IsValidColorAttachment(int bindIndex)
  127. {
  128. return (uint)bindIndex < Constants.MaxRenderTargets && (_validColorAttachments & (1u << bindIndex)) != 0;
  129. }
  130. private static bool IsValidTextureView(ITexture texture)
  131. {
  132. return texture is TextureView view && view.Valid;
  133. }
  134. public ClearRect GetClearRect(Rectangle<int> scissor, int layer, int layerCount)
  135. {
  136. int x = scissor.X;
  137. int y = scissor.Y;
  138. int width = Math.Min((int)Width - scissor.X, scissor.Width);
  139. int height = Math.Min((int)Height - scissor.Y, scissor.Height);
  140. return new ClearRect(new Rect2D(new Offset2D(x, y), new Extent2D((uint)width, (uint)height)), (uint)layer, (uint)layerCount);
  141. }
  142. public unsafe Auto<DisposableFramebuffer> Create(Vk api, CommandBufferScoped cbs, Auto<DisposableRenderPass> renderPass)
  143. {
  144. ImageView* attachments = stackalloc ImageView[_attachments.Length];
  145. for (int i = 0; i < _attachments.Length; i++)
  146. {
  147. attachments[i] = _attachments[i].Get(cbs).Value;
  148. }
  149. var framebufferCreateInfo = new FramebufferCreateInfo()
  150. {
  151. SType = StructureType.FramebufferCreateInfo,
  152. RenderPass = renderPass.Get(cbs).Value,
  153. AttachmentCount = (uint)_attachments.Length,
  154. PAttachments = attachments,
  155. Width = Width,
  156. Height = Height,
  157. Layers = Layers
  158. };
  159. api.CreateFramebuffer(_device, framebufferCreateInfo, null, out var framebuffer).ThrowOnError();
  160. return new Auto<DisposableFramebuffer>(new DisposableFramebuffer(api, _device, framebuffer), null, _attachments);
  161. }
  162. public void UpdateModifications()
  163. {
  164. if (_colors != null)
  165. {
  166. for (int index = 0; index < _colors.Length; index++)
  167. {
  168. _colors[index].Storage.SetModification(
  169. AccessFlags.ColorAttachmentWriteBit,
  170. PipelineStageFlags.ColorAttachmentOutputBit);
  171. }
  172. }
  173. _depthStencil?.Storage.SetModification(
  174. AccessFlags.DepthStencilAttachmentWriteBit,
  175. PipelineStageFlags.ColorAttachmentOutputBit);
  176. }
  177. public void InsertClearBarrier(CommandBufferScoped cbs, int index)
  178. {
  179. if (_colors != null)
  180. {
  181. int realIndex = Array.IndexOf(AttachmentIndices, index);
  182. if (realIndex != -1)
  183. {
  184. _colors[realIndex].Storage?.InsertReadToWriteBarrier(cbs, AccessFlags.ColorAttachmentWriteBit, PipelineStageFlags.ColorAttachmentOutputBit);
  185. }
  186. }
  187. }
  188. public void InsertClearBarrierDS(CommandBufferScoped cbs)
  189. {
  190. _depthStencil?.Storage?.InsertReadToWriteBarrier(cbs, AccessFlags.DepthStencilAttachmentWriteBit, PipelineStageFlags.EarlyFragmentTestsBit);
  191. }
  192. }
  193. }