FramebufferImpl.h 3.4 KB

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