FramebufferImpl.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include <AnKi/Gr/Vulkan/FramebufferImpl.h>
  6. #include <AnKi/Gr/Framebuffer.h>
  7. #include <AnKi/Gr/Vulkan/GrManagerImpl.h>
  8. #include <AnKi/Gr/Vulkan/TextureImpl.h>
  9. namespace anki {
  10. FramebufferImpl::~FramebufferImpl()
  11. {
  12. if(m_fb)
  13. {
  14. vkDestroyFramebuffer(getDevice(), m_fb, nullptr);
  15. }
  16. for(auto it : m_rpasses)
  17. {
  18. VkRenderPass rpass = it;
  19. ANKI_ASSERT(rpass);
  20. vkDestroyRenderPass(getDevice(), rpass, nullptr);
  21. }
  22. m_rpasses.destroy(getAllocator());
  23. if(m_compatibleRpass)
  24. {
  25. vkDestroyRenderPass(getDevice(), m_compatibleRpass, nullptr);
  26. }
  27. }
  28. Error FramebufferImpl::init(const FramebufferInitInfo& init)
  29. {
  30. ANKI_ASSERT(init.isValid());
  31. // Init common
  32. for(U32 i = 0; i < init.m_colorAttachmentCount; ++i)
  33. {
  34. m_colorAttachmentMask.set(i);
  35. m_colorAttCount = U8(i + 1);
  36. }
  37. if(init.m_depthStencilAttachment.m_textureView)
  38. {
  39. m_aspect = init.m_depthStencilAttachment.m_textureView->getSubresource().m_depthStencilAspect;
  40. }
  41. initClearValues(init);
  42. // Create a renderpass.
  43. initRpassCreateInfo(init);
  44. ANKI_VK_CHECK(vkCreateRenderPass(getDevice(), &m_rpassCi, nullptr, &m_compatibleRpass));
  45. getGrManagerImpl().trySetVulkanHandleName(init.getName(), VK_DEBUG_REPORT_OBJECT_TYPE_RENDER_PASS_EXT,
  46. m_compatibleRpass);
  47. // Create the FB
  48. ANKI_CHECK(initFbs(init));
  49. return Error::NONE;
  50. }
  51. void FramebufferImpl::initClearValues(const FramebufferInitInfo& init)
  52. {
  53. for(U i = 0; i < m_colorAttCount; ++i)
  54. {
  55. if(init.m_colorAttachments[i].m_loadOperation == AttachmentLoadOperation::CLEAR)
  56. {
  57. F32* col = &m_clearVals[i].color.float32[0];
  58. col[0] = init.m_colorAttachments[i].m_clearValue.m_colorf[0];
  59. col[1] = init.m_colorAttachments[i].m_clearValue.m_colorf[1];
  60. col[2] = init.m_colorAttachments[i].m_clearValue.m_colorf[2];
  61. col[3] = init.m_colorAttachments[i].m_clearValue.m_colorf[3];
  62. }
  63. else
  64. {
  65. m_clearVals[i] = {};
  66. }
  67. }
  68. if(hasDepthStencil())
  69. {
  70. if(init.m_depthStencilAttachment.m_loadOperation == AttachmentLoadOperation::CLEAR
  71. || init.m_depthStencilAttachment.m_stencilLoadOperation == AttachmentLoadOperation::CLEAR)
  72. {
  73. m_clearVals[m_colorAttCount].depthStencil.depth =
  74. init.m_depthStencilAttachment.m_clearValue.m_depthStencil.m_depth;
  75. m_clearVals[m_colorAttCount].depthStencil.stencil =
  76. init.m_depthStencilAttachment.m_clearValue.m_depthStencil.m_stencil;
  77. }
  78. else
  79. {
  80. m_clearVals[m_colorAttCount] = {};
  81. }
  82. }
  83. }
  84. Error FramebufferImpl::initFbs(const FramebufferInitInfo& init)
  85. {
  86. VkFramebufferCreateInfo ci = {};
  87. ci.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
  88. ci.renderPass = m_compatibleRpass;
  89. ci.attachmentCount = init.m_colorAttachmentCount + ((hasDepthStencil()) ? 1 : 0);
  90. ci.layers = 1;
  91. Array<VkImageView, MAX_COLOR_ATTACHMENTS + 1> imgViews;
  92. U count = 0;
  93. for(U i = 0; i < init.m_colorAttachmentCount; ++i)
  94. {
  95. const FramebufferAttachmentInfo& att = init.m_colorAttachments[i];
  96. const TextureViewImpl& view = static_cast<const TextureViewImpl&>(*att.m_textureView);
  97. const TextureImpl& tex = view.getTextureImpl();
  98. ANKI_ASSERT(tex.isSubresourceGoodForFramebufferAttachment(view.getSubresource()));
  99. imgViews[count++] = view.getHandle();
  100. if(m_width == 0)
  101. {
  102. m_width = tex.getWidth() >> view.getSubresource().m_firstMipmap;
  103. m_height = tex.getHeight() >> view.getSubresource().m_firstMipmap;
  104. }
  105. m_refs[i] = att.m_textureView;
  106. if(!!(tex.getTextureUsage() & TextureUsageBit::PRESENT))
  107. {
  108. m_presentableTex = true;
  109. }
  110. }
  111. if(hasDepthStencil())
  112. {
  113. const FramebufferAttachmentInfo& att = init.m_depthStencilAttachment;
  114. const TextureViewImpl& view = static_cast<const TextureViewImpl&>(*att.m_textureView);
  115. const TextureImpl& tex = view.getTextureImpl();
  116. ANKI_ASSERT(tex.isSubresourceGoodForFramebufferAttachment(view.getSubresource()));
  117. imgViews[count++] = view.getHandle();
  118. if(m_width == 0)
  119. {
  120. m_width = tex.getWidth() >> view.getSubresource().m_firstMipmap;
  121. m_height = tex.getHeight() >> view.getSubresource().m_firstMipmap;
  122. }
  123. m_refs[MAX_COLOR_ATTACHMENTS] = att.m_textureView;
  124. }
  125. ci.width = m_width;
  126. ci.height = m_height;
  127. ci.pAttachments = &imgViews[0];
  128. ANKI_ASSERT(count == ci.attachmentCount);
  129. ANKI_VK_CHECK(vkCreateFramebuffer(getDevice(), &ci, nullptr, &m_fb));
  130. getGrManagerImpl().trySetVulkanHandleName(init.getName(), VK_DEBUG_REPORT_OBJECT_TYPE_FRAMEBUFFER_EXT, m_fb);
  131. return Error::NONE;
  132. }
  133. void FramebufferImpl::setupAttachmentDescriptor(const FramebufferAttachmentInfo& att, VkAttachmentDescription& desc,
  134. VkImageLayout layout) const
  135. {
  136. desc = {};
  137. desc.format = convertFormat(static_cast<const TextureViewImpl&>(*att.m_textureView).getTextureImpl().getFormat());
  138. desc.samples = VK_SAMPLE_COUNT_1_BIT;
  139. desc.loadOp = convertLoadOp(att.m_loadOperation);
  140. desc.storeOp = convertStoreOp(att.m_storeOperation);
  141. desc.stencilLoadOp = convertLoadOp(att.m_stencilLoadOperation);
  142. desc.stencilStoreOp = convertStoreOp(att.m_stencilStoreOperation);
  143. desc.initialLayout = layout;
  144. desc.finalLayout = layout;
  145. }
  146. void FramebufferImpl::initRpassCreateInfo(const FramebufferInitInfo& init)
  147. {
  148. // Setup attachments and references
  149. for(U32 i = 0; i < init.m_colorAttachmentCount; ++i)
  150. {
  151. setupAttachmentDescriptor(init.m_colorAttachments[i], m_attachmentDescriptions[i],
  152. VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
  153. m_references[i].attachment = i;
  154. m_references[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  155. }
  156. if(hasDepthStencil())
  157. {
  158. setupAttachmentDescriptor(init.m_depthStencilAttachment, m_attachmentDescriptions[init.m_colorAttachmentCount],
  159. VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
  160. VkAttachmentReference& dsReference = m_references[init.m_colorAttachmentCount];
  161. dsReference.attachment = init.m_colorAttachmentCount;
  162. dsReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  163. }
  164. // Setup the render pass
  165. m_rpassCi.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
  166. m_rpassCi.pAttachments = &m_attachmentDescriptions[0];
  167. m_rpassCi.attachmentCount = init.m_colorAttachmentCount + ((hasDepthStencil()) ? 1 : 0);
  168. // Subpass
  169. m_subpassDescr.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
  170. m_subpassDescr.colorAttachmentCount = init.m_colorAttachmentCount;
  171. m_subpassDescr.pColorAttachments = (init.m_colorAttachmentCount) ? &m_references[0] : nullptr;
  172. m_subpassDescr.pDepthStencilAttachment = (hasDepthStencil()) ? &m_references[init.m_colorAttachmentCount] : nullptr;
  173. m_rpassCi.subpassCount = 1;
  174. m_rpassCi.pSubpasses = &m_subpassDescr;
  175. }
  176. VkRenderPass FramebufferImpl::getRenderPassHandle(const Array<VkImageLayout, MAX_COLOR_ATTACHMENTS>& colorLayouts,
  177. VkImageLayout dsLayout)
  178. {
  179. VkRenderPass out = {};
  180. // Create hash
  181. Array<VkImageLayout, MAX_COLOR_ATTACHMENTS + 1> allLayouts;
  182. U allLayoutCount = 0;
  183. for(U i = 0; i < m_colorAttCount; ++i)
  184. {
  185. ANKI_ASSERT(colorLayouts[i] != VK_IMAGE_LAYOUT_UNDEFINED);
  186. allLayouts[allLayoutCount++] = colorLayouts[i];
  187. }
  188. if(hasDepthStencil())
  189. {
  190. ANKI_ASSERT(dsLayout != VK_IMAGE_LAYOUT_UNDEFINED);
  191. allLayouts[allLayoutCount++] = dsLayout;
  192. }
  193. U64 hash = computeHash(&allLayouts[0], allLayoutCount * sizeof(allLayouts[0]));
  194. // Get or create
  195. LockGuard<Mutex> lock(m_rpassesMtx);
  196. auto it = m_rpasses.find(hash);
  197. if(it != m_rpasses.getEnd())
  198. {
  199. out = *it;
  200. }
  201. else
  202. {
  203. // Create
  204. VkRenderPassCreateInfo ci = m_rpassCi;
  205. Array<VkAttachmentDescription, MAX_COLOR_ATTACHMENTS + 1> attachmentDescriptions = m_attachmentDescriptions;
  206. Array<VkAttachmentReference, MAX_COLOR_ATTACHMENTS + 1> references = m_references;
  207. VkSubpassDescription subpassDescr = m_subpassDescr;
  208. // Fix pointers
  209. subpassDescr.pColorAttachments = &references[0];
  210. ci.pAttachments = &attachmentDescriptions[0];
  211. ci.pSubpasses = &subpassDescr;
  212. for(U i = 0; i < subpassDescr.colorAttachmentCount; ++i)
  213. {
  214. const VkImageLayout lay = colorLayouts[i];
  215. ANKI_ASSERT(lay != VK_IMAGE_LAYOUT_UNDEFINED);
  216. attachmentDescriptions[i].initialLayout = lay;
  217. attachmentDescriptions[i].finalLayout = lay;
  218. references[i].layout = lay;
  219. }
  220. if(hasDepthStencil())
  221. {
  222. const U i = subpassDescr.colorAttachmentCount;
  223. const VkImageLayout lay = dsLayout;
  224. ANKI_ASSERT(lay != VK_IMAGE_LAYOUT_UNDEFINED);
  225. attachmentDescriptions[i].initialLayout = lay;
  226. attachmentDescriptions[i].finalLayout = lay;
  227. references[subpassDescr.colorAttachmentCount].layout = lay;
  228. subpassDescr.pDepthStencilAttachment = &references[subpassDescr.colorAttachmentCount];
  229. }
  230. ANKI_VK_CHECKF(vkCreateRenderPass(getDevice(), &ci, nullptr, &out));
  231. getGrManagerImpl().trySetVulkanHandleName(getName(), VK_DEBUG_REPORT_OBJECT_TYPE_RENDER_PASS_EXT, out);
  232. m_rpasses.emplace(getAllocator(), hash, out);
  233. }
  234. ANKI_ASSERT(out);
  235. return out;
  236. }
  237. } // end namespace anki