From b90528eaa26f0505818b8923df4c6a27256d30ce Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Tue, 26 Jul 2022 10:56:04 -0400 Subject: [PATCH] [Impeller] [vulkan] Wire-up creating pipeline (flutter/engine#34890) --- .../backend/vulkan/pipeline_library_vk.cc | 155 +++++++++++------- .../backend/vulkan/pipeline_library_vk.h | 8 +- .../renderer/backend/vulkan/pipeline_vk.cc | 30 +++- .../renderer/backend/vulkan/pipeline_vk.h | 30 +++- 4 files changed, 155 insertions(+), 68 deletions(-) diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.cc index 6b9b47e2e4..b207723825 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.cc @@ -4,10 +4,13 @@ #include "impeller/renderer/backend/vulkan/pipeline_library_vk.h" +#include + #include "flutter/fml/trace_event.h" #include "impeller/base/promise.h" #include "impeller/base/validation.h" #include "impeller/renderer/backend/vulkan/formats_vk.h" +#include "impeller/renderer/backend/vulkan/pipeline_vk.h" #include "impeller/renderer/backend/vulkan/shader_function_vk.h" namespace impeller { @@ -73,8 +76,10 @@ PipelineFuture PipelineLibraryVK::GetRenderPipeline( "could be created."; return; } - promise->set_value( - PipelineLibraryVK::Cast(thiz.get())->CreatePipeline(descriptor)); + auto pipeline_create_info = + PipelineLibraryVK::Cast(thiz.get())->CreatePipeline(descriptor); + promise->set_value(std::make_shared( + weak_this, descriptor, std::move(pipeline_create_info))); }); return future; @@ -104,22 +109,88 @@ static vk::AttachmentDescription CreatePlaceholderAttachmentDescription( return desc; } -std::shared_ptr PipelineLibraryVK::CreatePipeline( - const PipelineDescriptor& desc) const { +//---------------------------------------------------------------------------- +/// Render Pass +/// We are NOT going to use the same render pass with the framebuffer (later) +/// and the graphics pipeline (here). Instead, we are going to ensure that the +/// sub-passes are compatible. To see the compatibility rules, see the Vulkan +/// spec: +/// https://www.khronos.org/registry/vulkan/specs/1.3-extensions/html/chap8.html#renderpass-compatibility +/// TODO(106378): Add a format specifier to the ColorAttachmentDescriptor, +/// StencilAttachmentDescriptor, and, DepthAttachmentDescriptor. +/// Right now, these are placeholders. +/// +std::optional PipelineLibraryVK::CreateRenderPass( + const PipelineDescriptor& desc) { + std::vector render_pass_attachments; + const auto sample_count = desc.GetSampleCount(); + // Set the color attachment. + render_pass_attachments.push_back(CreatePlaceholderAttachmentDescription( + vk::Format::eR8G8B8A8Unorm, sample_count)); + + std::vector color_attachment_references; + std::vector resolve_attachment_references; + std::optional depth_stencil_attachment_reference; + + // TODO (kaushikiska): consider changing the image layout to + // eColorAttachmentOptimal. + color_attachment_references.push_back(vk::AttachmentReference( + render_pass_attachments.size() - 1u, vk::ImageLayout::eGeneral)); + + // Set the resolve attachment if MSAA is enabled. + if (sample_count != SampleCount::kCount1) { + render_pass_attachments.push_back(CreatePlaceholderAttachmentDescription( + vk::Format::eR8G8B8A8Unorm, SampleCount::kCount1)); + resolve_attachment_references.push_back(vk::AttachmentReference( + render_pass_attachments.size() - 1u, vk::ImageLayout::eGeneral)); + } + + if (desc.HasStencilAttachmentDescriptors()) { + render_pass_attachments.push_back(CreatePlaceholderAttachmentDescription( + vk::Format::eS8Uint, sample_count)); + depth_stencil_attachment_reference = vk::AttachmentReference( + render_pass_attachments.size() - 1u, vk::ImageLayout::eGeneral); + } + + vk::SubpassDescription subpass_info; + subpass_info.setPipelineBindPoint(vk::PipelineBindPoint::eGraphics); + subpass_info.setColorAttachments(color_attachment_references); + subpass_info.setResolveAttachments(resolve_attachment_references); + if (depth_stencil_attachment_reference.has_value()) { + subpass_info.setPDepthStencilAttachment( + &depth_stencil_attachment_reference.value()); + } + + vk::RenderPassCreateInfo render_pass_info; + render_pass_info.setSubpasses(subpass_info); + render_pass_info.setAttachments(render_pass_attachments); + auto render_pass = device_.createRenderPassUnique(render_pass_info); + if (render_pass.result != vk::Result::eSuccess) { + VALIDATION_LOG << "Could not create render pass for pipeline " + << desc.GetLabel() << ": " + << vk::to_string(render_pass.result); + return std::nullopt; + } + + return std::move(render_pass.value); +} + +std::unique_ptr PipelineLibraryVK::CreatePipeline( + const PipelineDescriptor& desc) { TRACE_EVENT0("flutter", __FUNCTION__); vk::GraphicsPipelineCreateInfo pipeline_info; //---------------------------------------------------------------------------- /// Dynamic States /// - vk::PipelineDynamicStateCreateInfo dynamic_state_info; + vk::PipelineDynamicStateCreateInfo dynamic_create_state_info; std::vector dynamic_states = { vk::DynamicState::eViewport, vk::DynamicState::eScissor, vk::DynamicState::eStencilReference, }; - dynamic_state_info.setDynamicStates(dynamic_states); - pipeline_info.setPDynamicState(&dynamic_state_info); + dynamic_create_state_info.setDynamicStates(dynamic_states); + pipeline_info.setPDynamicState(&dynamic_create_state_info); //---------------------------------------------------------------------------- /// Viewport State @@ -160,7 +231,12 @@ std::shared_ptr PipelineLibraryVK::CreatePipeline( rasterization_state.setFrontFace(vk::FrontFace::eClockwise); rasterization_state.setCullMode(vk::CullModeFlagBits::eNone); rasterization_state.setPolygonMode(vk::PolygonMode::eFill); - rasterization_state.setLineWidth(1.0f); + // requires GPU extensions to change. + { + rasterization_state.setLineWidth(1.0f); + rasterization_state.setDepthClampEnable(false); + } + rasterization_state.setRasterizerDiscardEnable(false); pipeline_info.setPRasterizationState(&rasterization_state); //---------------------------------------------------------------------------- @@ -195,59 +271,14 @@ std::shared_ptr PipelineLibraryVK::CreatePipeline( blend_state.setAttachments(attachment_blend_state); pipeline_info.setPColorBlendState(&blend_state); - //---------------------------------------------------------------------------- - /// Render Pass - /// We are NOT going to use the same render pass with the framebuffer (later) - /// and the graphics pipeline (here). Instead, we are going to ensure that the - /// sub-passes are compatible. To see the compatibility rules, see the Vulkan - /// spec: - /// https://www.khronos.org/registry/vulkan/specs/1.3-extensions/html/chap8.html#renderpass-compatibility - /// TODO(106378): Add a format specifier to the ColorAttachmentDescriptor, - /// StencilAttachmentDescriptor, and, DepthAttachmentDescriptor. - /// Right now, these are placeholders. - /// - std::vector color_attachment_references; - std::vector resolve_attachment_references; - std::optional depth_stencil_attachment_reference; - std::vector render_pass_attachments; - const auto sample_count = desc.GetSampleCount(); - // Set the color attachment. - render_pass_attachments.push_back(CreatePlaceholderAttachmentDescription( - vk::Format::eR8G8B8A8Unorm, sample_count)); - color_attachment_references.push_back(vk::AttachmentReference( - render_pass_attachments.size() - 1u, vk::ImageLayout::eGeneral)); - // Set the resolve attachment if MSAA is enabled. - if (sample_count != SampleCount::kCount1) { - render_pass_attachments.push_back(CreatePlaceholderAttachmentDescription( - vk::Format::eR8G8B8A8Unorm, SampleCount::kCount1)); - resolve_attachment_references.push_back(vk::AttachmentReference( - render_pass_attachments.size() - 1u, vk::ImageLayout::eGeneral)); - } - if (desc.HasStencilAttachmentDescriptors()) { - render_pass_attachments.push_back(CreatePlaceholderAttachmentDescription( - vk::Format::eS8Uint, sample_count)); - depth_stencil_attachment_reference = vk::AttachmentReference( - render_pass_attachments.size() - 1u, vk::ImageLayout::eGeneral); - } - vk::SubpassDescription subpass_info; - subpass_info.setPipelineBindPoint(vk::PipelineBindPoint::eGraphics); - subpass_info.setColorAttachments(color_attachment_references); - subpass_info.setResolveAttachments(resolve_attachment_references); - if (depth_stencil_attachment_reference.has_value()) { - subpass_info.setPDepthStencilAttachment( - &depth_stencil_attachment_reference.value()); - } - vk::RenderPassCreateInfo render_pass_info; - render_pass_info.setSubpasses(subpass_info); - render_pass_info.setAttachments(render_pass_attachments); - auto render_pass = device_.createRenderPassUnique(render_pass_info); - if (render_pass.result != vk::Result::eSuccess) { - VALIDATION_LOG << "Could not create render pass for pipeline " - << desc.GetLabel() << ": " - << vk::to_string(render_pass.result); + auto render_pass = CreateRenderPass(desc); + if (render_pass.has_value()) { + pipeline_info.setBasePipelineHandle(VK_NULL_HANDLE); + pipeline_info.setSubpass(0); + pipeline_info.setRenderPass((*render_pass).get()); + } else { return nullptr; } - pipeline_info.setRenderPass(render_pass.value.get()); //---------------------------------------------------------------------------- /// Pipeline Layout a.k.a the descriptor sets and uniforms. @@ -267,10 +298,8 @@ std::shared_ptr PipelineLibraryVK::CreatePipeline( pipeline_info.setLayout(pipeline_layout.value.get()); // TODO(WIP) - // pipeline_info.setPVertexInputState(&vertex_input_state); // pipeline_info.setPDepthStencilState(&depth_stencil_state_); - // pipeline_info.setLayout(pipeline_layout); // See the note in the header about why this is a reader lock. ReaderLock lock(cache_mutex_); @@ -280,7 +309,9 @@ std::shared_ptr PipelineLibraryVK::CreatePipeline( VALIDATION_LOG << "Could not create graphics pipeline: " << desc.GetLabel(); return nullptr; } - FML_UNREACHABLE(); + + return std::make_unique(std::move(pipeline.value), + std::move(render_pass.value())); } } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.h index c8c5a6fbc2..9ec08fb865 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.h @@ -12,6 +12,7 @@ #include "impeller/renderer/backend/vulkan/pipeline_vk.h" #include "impeller/renderer/backend/vulkan/vk.h" #include "impeller/renderer/pipeline_library.h" +#include "vulkan/vulkan_handles.hpp" namespace impeller { @@ -52,8 +53,11 @@ class PipelineLibraryVK final // |PipelineLibrary| PipelineFuture GetRenderPipeline(PipelineDescriptor descriptor) override; - std::shared_ptr CreatePipeline( - const PipelineDescriptor& desc) const; + std::unique_ptr CreatePipeline( + const PipelineDescriptor& desc); + + std::optional CreateRenderPass( + const PipelineDescriptor& desc); FML_DISALLOW_COPY_AND_ASSIGN(PipelineLibraryVK); }; 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 623a589509..8b76786067 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.cc @@ -6,6 +6,34 @@ namespace impeller { -// +PipelineCreateInfoVK::PipelineCreateInfoVK(vk::UniquePipeline pipeline, + vk::UniqueRenderPass render_pass) + : pipeline_(std::move(pipeline)), render_pass_(std::move(render_pass)) { + is_valid_ = pipeline_ && render_pass_; +} + +bool PipelineCreateInfoVK::IsValid() const { + return is_valid_; +} + +vk::UniquePipeline PipelineCreateInfoVK::GetPipeline() { + return std::move(pipeline_); +} + +vk::UniqueRenderPass PipelineCreateInfoVK::GetRenderPass() { + return std::move(render_pass_); +} + +PipelineVK::PipelineVK(std::weak_ptr library, + PipelineDescriptor desc, + std::unique_ptr create_info) + : Pipeline(std::move(library), std::move(desc)), + pipeline_info_(std::move(create_info)) {} + +PipelineVK::~PipelineVK() = default; + +bool PipelineVK::IsValid() const { + return pipeline_info_->IsValid(); +} } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.h index 34ec0d78c6..3a0b9e127c 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.h @@ -4,26 +4,50 @@ #pragma once +#include + #include "flutter/fml/macros.h" #include "impeller/base/backend_cast.h" +#include "impeller/renderer/backend/vulkan/vk.h" #include "impeller/renderer/pipeline.h" namespace impeller { +class PipelineCreateInfoVK { + public: + PipelineCreateInfoVK(vk::UniquePipeline pipeline, + vk::UniqueRenderPass render_pass); + + bool IsValid() const; + + vk::UniquePipeline GetPipeline(); + + vk::UniqueRenderPass GetRenderPass(); + + private: + bool is_valid_ = false; + vk::UniquePipeline pipeline_; + vk::UniqueRenderPass render_pass_; +}; + class PipelineVK final : public Pipeline, public BackendCast { public: + PipelineVK(std::weak_ptr library, + PipelineDescriptor desc, + std::unique_ptr create_info); + // |Pipeline| ~PipelineVK() override; private: - friend class PipelineLibraryMTL; - - PipelineVK(std::weak_ptr library, PipelineDescriptor desc); + friend class PipelineLibraryVK; // |Pipeline| bool IsValid() const override; + std::unique_ptr pipeline_info_; + FML_DISALLOW_COPY_AND_ASSIGN(PipelineVK); };