From 97c434c66204abd886ba6617b11b63d830fe62d8 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Tue, 23 Apr 2024 11:05:46 -0700 Subject: [PATCH] [Impeller] Cleanup `PipelineVK::Create` (flutter/engine#52278) `PipelineVK::Create` was one giant function. Now its 3 smaller functions and one less giant but still big function. This is groundwork for trying to batch calls. I tried to split out the creation of the `vk::GraphicsPipelineCreateInfo` but it holds pointers to things on the stack so it's hard to do. test exempt: refactor, no new functionality ## 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 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 [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [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 --- engine/src/flutter/fml/status_or.h | 4 + .../renderer/backend/vulkan/pipeline_vk.cc | 217 +++++++++++------- 2 files changed, 133 insertions(+), 88 deletions(-) diff --git a/engine/src/flutter/fml/status_or.h b/engine/src/flutter/fml/status_or.h index 8c911f93bf..fc8c5dc1bb 100644 --- a/engine/src/flutter/fml/status_or.h +++ b/engine/src/flutter/fml/status_or.h @@ -35,6 +35,10 @@ class StatusOr { // NOLINTNEXTLINE(google-explicit-constructor) StatusOr(const T& value) : status_(), value_(value) {} + // These constructors are intended be compatible with absl::status_or. + // NOLINTNEXTLINE(google-explicit-constructor) + StatusOr(T&& value) : status_(), value_(std::move(value)) {} + // These constructors are intended be compatible with absl::status_or. // NOLINTNEXTLINE(google-explicit-constructor) StatusOr(const Status& status) : status_(status), value_() { diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.cc index 84374190a2..793a158a32 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.cc @@ -5,6 +5,7 @@ #include "impeller/renderer/backend/vulkan/pipeline_vk.h" #include "flutter/fml/make_copyable.h" +#include "flutter/fml/status_or.h" #include "flutter/fml/trace_event.h" #include "impeller/base/timing.h" #include "impeller/renderer/backend/vulkan/capabilities_vk.h" @@ -164,21 +165,82 @@ static vk::UniqueRenderPass CreateCompatRenderPassForPipeline( return pass; } -std::unique_ptr PipelineVK::Create( +namespace { +fml::StatusOr MakeDescriptorSetLayout( const PipelineDescriptor& desc, const std::shared_ptr& device_holder, - const std::weak_ptr& weak_library, - std::shared_ptr immutable_sampler) { - TRACE_EVENT0("flutter", "PipelineVK::Create"); + const std::shared_ptr& immutable_sampler) { + std::vector set_bindings; - auto library = weak_library.lock(); + vk::Sampler vk_immutable_sampler = + immutable_sampler ? immutable_sampler->GetSampler() + : static_cast(VK_NULL_HANDLE); - if (!device_holder || !library) { - return nullptr; + for (auto layout : desc.GetVertexDescriptor()->GetDescriptorSetLayouts()) { + vk::DescriptorSetLayoutBinding set_binding; + set_binding.binding = layout.binding; + set_binding.descriptorCount = 1u; + set_binding.descriptorType = ToVKDescriptorType(layout.descriptor_type); + set_binding.stageFlags = ToVkShaderStage(layout.shader_stage); + // TODO(143719): This specifies the immutable sampler for all sampled + // images. This is incorrect. In cases where the shader samples from the + // multiple images, there is currently no way to tell which sampler needs to + // be immutable and which one needs a binding set in the render pass. Expect + // errors if the shader has more than on sampled image. The sampling from + // the one that is expected to be non-immutable will be incorrect. + if (vk_immutable_sampler && + layout.descriptor_type == DescriptorType::kSampledImage) { + set_binding.setImmutableSamplers(vk_immutable_sampler); + } + set_bindings.push_back(set_binding); } - const auto& pso_cache = PipelineLibraryVK::Cast(*library).GetPSOCache(); + vk::DescriptorSetLayoutCreateInfo desc_set_layout_info; + desc_set_layout_info.setBindings(set_bindings); + auto [descs_result, descs_layout] = + device_holder->GetDevice().createDescriptorSetLayoutUnique( + desc_set_layout_info); + if (descs_result != vk::Result::eSuccess) { + VALIDATION_LOG << "unable to create uniform descriptors"; + return {fml::Status(fml::StatusCode::kUnknown, + "unable to create uniform descriptors")}; + } + + ContextVK::SetDebugName(device_holder->GetDevice(), descs_layout.get(), + "Descriptor Set Layout " + desc.GetLabel()); + + return fml::StatusOr(std::move(descs_layout)); +} + +fml::StatusOr MakePipelineLayout( + const PipelineDescriptor& desc, + const std::shared_ptr& device_holder, + const vk::DescriptorSetLayout& descs_layout) { + vk::PipelineLayoutCreateInfo pipeline_layout_info; + pipeline_layout_info.setSetLayouts(descs_layout); + auto pipeline_layout = device_holder->GetDevice().createPipelineLayoutUnique( + pipeline_layout_info); + if (pipeline_layout.result != vk::Result::eSuccess) { + VALIDATION_LOG << "Could not create pipeline layout for pipeline " + << desc.GetLabel() << ": " + << vk::to_string(pipeline_layout.result); + return {fml::Status(fml::StatusCode::kUnknown, + "Could not create pipeline layout for pipeline.")}; + } + + ContextVK::SetDebugName(device_holder->GetDevice(), *pipeline_layout.value, + "Pipeline Layout " + desc.GetLabel()); + + return std::move(pipeline_layout.value); +} + +fml::StatusOr MakePipeline( + const PipelineDescriptor& desc, + const std::shared_ptr& device_holder, + const std::shared_ptr& pso_cache, + const vk::PipelineLayout& pipeline_layout, + const vk::RenderPass& render_pass) { vk::StructureChain chain; @@ -192,6 +254,7 @@ std::unique_ptr PipelineVK::Create( } auto& pipeline_info = chain.get(); + pipeline_info.setLayout(pipeline_layout); //---------------------------------------------------------------------------- /// Dynamic States @@ -232,7 +295,8 @@ std::unique_ptr PipelineVK::Create( if (!stage.has_value()) { VALIDATION_LOG << "Unsupported shader type in pipeline: " << desc.GetLabel(); - return nullptr; + return {fml::Status(fml::StatusCode::kUnknown, + "Unsupported shader type in pipeline.")}; } std::vector& entries = @@ -303,22 +367,12 @@ std::unique_ptr PipelineVK::Create( blend_state.setAttachments(attachment_blend_state); pipeline_info.setPColorBlendState(&blend_state); - auto render_pass = - CreateCompatRenderPassForPipeline(device_holder->GetDevice(), // - desc // - ); - - if (!render_pass) { - VALIDATION_LOG << "Could not create render pass for pipeline."; - return nullptr; - } - // Convention wisdom says that the base acceleration pipelines are never used // by drivers for cache hits. Instead, the PSO cache is the preferred // mechanism. pipeline_info.setBasePipelineHandle(VK_NULL_HANDLE); pipeline_info.setSubpass(0u); - pipeline_info.setRenderPass(render_pass.get()); + pipeline_info.setRenderPass(render_pass); //---------------------------------------------------------------------------- /// Vertex Input Setup @@ -351,63 +405,6 @@ std::unique_ptr PipelineVK::Create( pipeline_info.setPVertexInputState(&vertex_input_state); - //---------------------------------------------------------------------------- - /// Pipeline Layout a.k.a the descriptor sets and uniforms. - /// - std::vector set_bindings; - - vk::Sampler vk_immutable_sampler = - immutable_sampler ? immutable_sampler->GetSampler() - : static_cast(VK_NULL_HANDLE); - - for (auto layout : desc.GetVertexDescriptor()->GetDescriptorSetLayouts()) { - vk::DescriptorSetLayoutBinding set_binding; - set_binding.binding = layout.binding; - set_binding.descriptorCount = 1u; - set_binding.descriptorType = ToVKDescriptorType(layout.descriptor_type); - set_binding.stageFlags = ToVkShaderStage(layout.shader_stage); - // TODO(143719): This specifies the immutable sampler for all sampled - // images. This is incorrect. In cases where the shader samples from the - // multiple images, there is currently no way to tell which sampler needs to - // be immutable and which one needs a binding set in the render pass. Expect - // errors if the shader has more than on sampled image. The sampling from - // the one that is expected to be non-immutable will be incorrect. - if (vk_immutable_sampler && - layout.descriptor_type == DescriptorType::kSampledImage) { - set_binding.setImmutableSamplers(vk_immutable_sampler); - } - set_bindings.push_back(set_binding); - } - - vk::DescriptorSetLayoutCreateInfo desc_set_layout_info; - desc_set_layout_info.setBindings(set_bindings); - - auto [descs_result, descs_layout] = - device_holder->GetDevice().createDescriptorSetLayoutUnique( - desc_set_layout_info); - if (descs_result != vk::Result::eSuccess) { - VALIDATION_LOG << "unable to create uniform descriptors"; - return nullptr; - } - - ContextVK::SetDebugName(device_holder->GetDevice(), descs_layout.get(), - "Descriptor Set Layout " + desc.GetLabel()); - - //---------------------------------------------------------------------------- - /// Create the pipeline layout. - /// - vk::PipelineLayoutCreateInfo pipeline_layout_info; - pipeline_layout_info.setSetLayouts(descs_layout.get()); - auto pipeline_layout = device_holder->GetDevice().createPipelineLayoutUnique( - pipeline_layout_info); - if (pipeline_layout.result != vk::Result::eSuccess) { - VALIDATION_LOG << "Could not create pipeline layout for pipeline " - << desc.GetLabel() << ": " - << vk::to_string(pipeline_layout.result); - return nullptr; - } - pipeline_info.setLayout(pipeline_layout.value.get()); - //---------------------------------------------------------------------------- /// Create the depth stencil state. /// @@ -434,27 +431,71 @@ std::unique_ptr PipelineVK::Create( auto pipeline = pso_cache->CreatePipeline(pipeline_info); if (!pipeline) { VALIDATION_LOG << "Could not create graphics pipeline: " << desc.GetLabel(); - return nullptr; + return {fml::Status(fml::StatusCode::kUnknown, + "Could not create graphics pipeline.")}; } if (supports_pipeline_creation_feedback) { ReportPipelineCreationFeedback(desc, feedback); } - ContextVK::SetDebugName(device_holder->GetDevice(), *pipeline_layout.value, - "Pipeline Layout " + desc.GetLabel()); ContextVK::SetDebugName(device_holder->GetDevice(), *pipeline, "Pipeline " + desc.GetLabel()); + return std::move(pipeline); +} +} // namespace + +std::unique_ptr PipelineVK::Create( + const PipelineDescriptor& desc, + const std::shared_ptr& device_holder, + const std::weak_ptr& weak_library, + std::shared_ptr immutable_sampler) { + TRACE_EVENT0("flutter", "PipelineVK::Create"); + + auto library = weak_library.lock(); + + if (!device_holder || !library) { + return nullptr; + } + + const auto& pso_cache = PipelineLibraryVK::Cast(*library).GetPSOCache(); + + fml::StatusOr descs_layout = + MakeDescriptorSetLayout(desc, device_holder, immutable_sampler); + if (!descs_layout.ok()) { + return nullptr; + } + + fml::StatusOr pipeline_layout = + MakePipelineLayout(desc, device_holder, descs_layout.value().get()); + if (!pipeline_layout.ok()) { + return nullptr; + } + + vk::UniqueRenderPass render_pass = + CreateCompatRenderPassForPipeline(device_holder->GetDevice(), desc); + if (!render_pass) { + VALIDATION_LOG << "Could not create render pass for pipeline."; + return nullptr; + } + + fml::StatusOr pipeline = + MakePipeline(desc, device_holder, pso_cache, + pipeline_layout.value().get(), render_pass.get()); + if (!pipeline.ok()) { + return nullptr; + } + auto pipeline_vk = std::unique_ptr(new PipelineVK( - device_holder, // - library, // - desc, // - std::move(pipeline), // - std::move(render_pass), // - std::move(pipeline_layout.value), // - std::move(descs_layout), // - std::move(immutable_sampler) // + device_holder, // + library, // + desc, // + std::move(pipeline.value()), // + std::move(render_pass), // + std::move(pipeline_layout.value()), // + std::move(descs_layout.value()), // + std::move(immutable_sampler) // )); if (!pipeline_vk->IsValid()) { VALIDATION_LOG << "Could not create a valid pipeline.";