FramebufferImpl.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Gr/Framebuffer.h>
  7. #include <AnKi/Gr/Vulkan/VulkanObject.h>
  8. #include <AnKi/Gr/Vulkan/SwapchainFactory.h>
  9. #include <AnKi/Util/HashMap.h>
  10. #include <AnKi/Util/BitSet.h>
  11. namespace anki
  12. {
  13. // Forward
  14. class FramebufferAttachmentInfo;
  15. /// @addtogroup vulkan
  16. /// @{
  17. /// Framebuffer implementation.
  18. class FramebufferImpl final : public Framebuffer, public VulkanObject<Framebuffer, FramebufferImpl>
  19. {
  20. public:
  21. FramebufferImpl(GrManager* manager, CString name)
  22. : Framebuffer(manager, name)
  23. {
  24. }
  25. ~FramebufferImpl();
  26. ANKI_USE_RESULT Error init(const FramebufferInitInfo& init);
  27. /// Good for pipeline creation.
  28. VkRenderPass getCompatibleRenderPass() const
  29. {
  30. ANKI_ASSERT(m_compatibleRpass);
  31. return m_compatibleRpass;
  32. }
  33. /// Use it for binding. It's thread-safe
  34. VkRenderPass getRenderPassHandle(const Array<VkImageLayout, MAX_COLOR_ATTACHMENTS>& colorLayouts,
  35. VkImageLayout dsLayout);
  36. VkFramebuffer getFramebufferHandle() const
  37. {
  38. ANKI_ASSERT(m_fb);
  39. return m_fb;
  40. }
  41. void getAttachmentInfo(BitSet<MAX_COLOR_ATTACHMENTS, U8>& colorAttachments, Bool& depth, Bool& stencil) const
  42. {
  43. colorAttachments = m_colorAttachmentMask;
  44. depth = !!(m_aspect & DepthStencilAspectBit::DEPTH);
  45. stencil = !!(m_aspect & DepthStencilAspectBit::STENCIL);
  46. }
  47. U32 getColorAttachmentCount() const
  48. {
  49. return m_colorAttCount;
  50. }
  51. Bool hasDepthStencil() const
  52. {
  53. return !!m_aspect;
  54. }
  55. U32 getAttachmentCount() const
  56. {
  57. return m_colorAttCount + (hasDepthStencil() ? 1 : 0);
  58. }
  59. const TextureViewPtr& getColorAttachment(U att) const
  60. {
  61. ANKI_ASSERT(m_refs[att].get());
  62. return m_refs[att];
  63. }
  64. const TextureViewPtr& getDepthStencilAttachment() const
  65. {
  66. ANKI_ASSERT(m_refs[MAX_COLOR_ATTACHMENTS].get());
  67. return m_refs[MAX_COLOR_ATTACHMENTS];
  68. }
  69. const VkClearValue* getClearValues() const
  70. {
  71. return &m_clearVals[0];
  72. }
  73. void getAttachmentsSize(U32& width, U32& height) const
  74. {
  75. ANKI_ASSERT(m_width != 0 && m_height != 0);
  76. width = m_width;
  77. height = m_height;
  78. }
  79. Bool hasPresentableTexture() const
  80. {
  81. return m_presentableTex;
  82. }
  83. private:
  84. BitSet<MAX_COLOR_ATTACHMENTS, U8> m_colorAttachmentMask = {false};
  85. DepthStencilAspectBit m_aspect = DepthStencilAspectBit::NONE;
  86. U8 m_colorAttCount = 0;
  87. Array<VkClearValue, MAX_COLOR_ATTACHMENTS + 1> m_clearVals;
  88. U32 m_width = 0;
  89. U32 m_height = 0;
  90. Bool m_presentableTex = false;
  91. Array<TextureViewPtr, MAX_COLOR_ATTACHMENTS + 1> m_refs; ///< @note The pos of every attachment is fixed.
  92. // RenderPass create info
  93. VkRenderPassCreateInfo m_rpassCi = {};
  94. Array<VkAttachmentDescription, MAX_COLOR_ATTACHMENTS + 1> m_attachmentDescriptions = {};
  95. Array<VkAttachmentReference, MAX_COLOR_ATTACHMENTS + 1> m_references = {};
  96. VkSubpassDescription m_subpassDescr = {};
  97. // VK objects
  98. VkRenderPass m_compatibleRpass = {}; ///< Compatible renderpass.
  99. HashMap<U64, VkRenderPass> m_rpasses;
  100. Mutex m_rpassesMtx;
  101. VkFramebuffer m_fb = VK_NULL_HANDLE;
  102. // Methods
  103. ANKI_USE_RESULT Error initFbs(const FramebufferInitInfo& init);
  104. void initRpassCreateInfo(const FramebufferInitInfo& init);
  105. void initClearValues(const FramebufferInitInfo& init);
  106. void setupAttachmentDescriptor(const FramebufferAttachmentInfo& att, VkAttachmentDescription& desc,
  107. VkImageLayout layout) const;
  108. };
  109. /// @}
  110. } // end namespace anki