From e9ea159828ba3831fd547a7f42ec1d29a6f33c41 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Thu, 24 Aug 2023 20:36:05 -0700 Subject: [PATCH] [Impeller] Refactor: Create attachment descriptions without setting layouts (flutter/engine#45088) This refactor makes recycling render passes easier since it starts to split out setting the layout of textures with creating the render pass description. I didn't fully implement the split since it would technically be slower. Until we get caching of render passes working it would require us to calculate the attachment descriptions twice, once to make the render pass and once to set the texture layout. teases out some of the work from: https://github.com/flutter/engine/pull/44527 in the service of implementing: https://github.com/flutter/engine/pull/44861 issue: https://github.com/flutter/flutter/issues/133182 The final split will have this function for recycled render passes: ```c++ void SetTextureLayouts(const RenderTarget& render_target, const std::shared_ptr& command_buffer) { for (const auto& [bind_point, color] : render_target.GetColorAttachments()) { SetTextureLayout(color, CreateAttachmentDescription(color, &Attachment::texture), command_buffer, &Attachment::texture); if (color.resolve_texture) { SetTextureLayout( color, CreateAttachmentDescription(color, &Attachment::resolve_texture), command_buffer, &Attachment::resolve_texture); } } if (auto depth = render_target.GetDepthAttachment(); depth.has_value()) { SetTextureLayout( depth.value(), CreateAttachmentDescription(depth.value(), &Attachment::texture), command_buffer, &Attachment::texture); } if (auto stencil = render_target.GetStencilAttachment(); stencil.has_value()) { SetTextureLayout( stencil.value(), CreateAttachmentDescription(stencil.value(), &Attachment::texture), command_buffer, &Attachment::texture); } } ``` ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- .../renderer/backend/vulkan/render_pass_vk.cc | 59 ++++++++++++------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/render_pass_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/render_pass_vk.cc index 796048c553..6729a4ab09 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/render_pass_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/render_pass_vk.cc @@ -31,10 +31,8 @@ namespace impeller { static vk::AttachmentDescription CreateAttachmentDescription( const Attachment& attachment, - const std::shared_ptr& command_buffer, - bool resolve_texture = false) { - const auto& texture = - resolve_texture ? attachment.resolve_texture : attachment.texture; + const std::shared_ptr Attachment::*texture_ptr) { + const auto& texture = attachment.*texture_ptr; if (!texture) { return {}; } @@ -51,12 +49,36 @@ static vk::AttachmentDescription CreateAttachmentDescription( if (desc.storage_mode == StorageMode::kDeviceTransient) { store_action = StoreAction::kDontCare; - } else if (resolve_texture) { + } else if (texture_ptr == &Attachment::resolve_texture) { store_action = StoreAction::kStore; } if (current_layout != vk::ImageLayout::ePresentSrcKHR && current_layout != vk::ImageLayout::eUndefined) { + // Note: This should incur a barrier. + current_layout = vk::ImageLayout::eGeneral; + } + + return CreateAttachmentDescription(desc.format, // + desc.sample_count, // + load_action, // + store_action, // + current_layout // + ); +} + +static void SetTextureLayout( + const Attachment& attachment, + const vk::AttachmentDescription& attachment_desc, + const std::shared_ptr& command_buffer, + const std::shared_ptr Attachment::*texture_ptr) { + const auto& texture = attachment.*texture_ptr; + if (!texture) { + return; + } + const auto& texture_vk = TextureVK::Cast(*texture); + + if (attachment_desc.initialLayout == vk::ImageLayout::eGeneral) { BarrierVK barrier; barrier.new_layout = vk::ImageLayout::eGeneral; barrier.cmd_buffer = command_buffer->GetEncoder()->GetCommandBuffer(); @@ -68,22 +90,11 @@ static vk::AttachmentDescription CreateAttachmentDescription( vk::PipelineStageFlagBits::eTransfer; texture_vk.SetLayout(barrier); - current_layout = vk::ImageLayout::eGeneral; } - const auto attachment_desc = - CreateAttachmentDescription(desc.format, // - desc.sample_count, // - load_action, // - store_action, // - current_layout // - ); - // Instead of transitioning layouts manually using barriers, we are going to // make the subpass perform our transitions. texture_vk.SetLayoutWithoutEncoding(attachment_desc.finalLayout); - - return attachment_desc; } SharedHandleVK RenderPassVK::CreateVKRenderPass( @@ -113,12 +124,16 @@ SharedHandleVK RenderPassVK::CreateVKRenderPass( vk::AttachmentReference{static_cast(attachments.size()), vk::ImageLayout::eColorAttachmentOptimal}; attachments.emplace_back( - CreateAttachmentDescription(color, command_buffer)); + CreateAttachmentDescription(color, &Attachment::texture)); + SetTextureLayout(color, attachments.back(), command_buffer, + &Attachment::texture); if (color.resolve_texture) { resolve_refs[bind_point] = vk::AttachmentReference{ static_cast(attachments.size()), vk::ImageLayout::eGeneral}; attachments.emplace_back( - CreateAttachmentDescription(color, command_buffer, true)); + CreateAttachmentDescription(color, &Attachment::resolve_texture)); + SetTextureLayout(color, attachments.back(), command_buffer, + &Attachment::resolve_texture); } } @@ -127,7 +142,9 @@ SharedHandleVK RenderPassVK::CreateVKRenderPass( static_cast(attachments.size()), vk::ImageLayout::eDepthStencilAttachmentOptimal}; attachments.emplace_back( - CreateAttachmentDescription(depth.value(), command_buffer)); + CreateAttachmentDescription(depth.value(), &Attachment::texture)); + SetTextureLayout(depth.value(), attachments.back(), command_buffer, + &Attachment::texture); } if (auto stencil = render_target_.GetStencilAttachment(); @@ -136,7 +153,9 @@ SharedHandleVK RenderPassVK::CreateVKRenderPass( static_cast(attachments.size()), vk::ImageLayout::eDepthStencilAttachmentOptimal}; attachments.emplace_back( - CreateAttachmentDescription(stencil.value(), command_buffer)); + CreateAttachmentDescription(stencil.value(), &Attachment::texture)); + SetTextureLayout(stencil.value(), attachments.back(), command_buffer, + &Attachment::texture); } vk::SubpassDescription subpass_desc;