[Impeller] [vulkan] Setup surface producer, and wireup swapchain sync objects (flutter/engine#35322)

This commit is contained in:
Kaushik Iska
2022-08-10 16:25:10 -07:00
committed by GitHub
parent 30f729309c
commit fe037aac1d
14 changed files with 403 additions and 26 deletions

View File

@@ -799,6 +799,8 @@ FILE: ../../../flutter/impeller/renderer/backend/vulkan/shader_function_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/shader_function_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/shader_library_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/shader_library_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/surface_producer_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/surface_producer_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/surface_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/surface_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain_details_vk.cc

View File

@@ -85,11 +85,9 @@ PlaygroundImplVK::PlaygroundImplVK()
void PlaygroundImplVK::SetupSwapchain() {
ContextVK* context_vk = reinterpret_cast<ContextVK*>(context_.get());
auto window = reinterpret_cast<GLFWwindow*>(handle_.get());
::glfwCreateWindowSurface(context_vk->GetInstance(), window, nullptr,
&surface_);
swapchain_ = context_vk->CreateSwapchain(surface_);
context_vk->SetupSwapchain(surface_);
}
PlaygroundImplVK::~PlaygroundImplVK() = default;

View File

@@ -27,9 +27,8 @@ class PlaygroundImplVK final : public PlaygroundImpl {
using UniqueHandle = std::unique_ptr<void, decltype(&DestroyWindowHandle)>;
UniqueHandle handle_;
// surface and swapchain
// surface
VkSurfaceKHR surface_;
std::unique_ptr<SwapchainVK> swapchain_;
// |PlaygroundImpl|
std::shared_ptr<Context> GetContext() const override;

View File

@@ -36,6 +36,8 @@ impeller_component("vulkan") {
"shader_function_vk.h",
"shader_library_vk.cc",
"shader_library_vk.h",
"surface_producer_vk.cc",
"surface_producer_vk.h",
"surface_vk.cc",
"surface_vk.h",
"swapchain_details_vk.cc",

View File

@@ -14,8 +14,10 @@
#include "impeller/base/validation.h"
#include "impeller/renderer/backend/vulkan/allocator_vk.h"
#include "impeller/renderer/backend/vulkan/capabilities_vk.h"
#include "impeller/renderer/backend/vulkan/surface_producer_vk.h"
#include "impeller/renderer/backend/vulkan/swapchain_details_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include "vulkan/vulkan.hpp"
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
@@ -137,6 +139,23 @@ static std::optional<QueueVK> PickQueue(const vk::PhysicalDevice& device,
return std::nullopt;
}
static std::optional<QueueVK> PickPresentQueue(const vk::PhysicalDevice& device,
vk::SurfaceKHR surface) {
const auto families = device.getQueueFamilyProperties();
for (size_t i = 0u; i < families.size(); i++) {
auto res = device.getSurfaceSupportKHR(i, surface);
if (res.result != vk::Result::eSuccess) {
continue;
}
vk::Bool32 present_supported = res.value;
if (present_supported) {
return QueueVK{.family = i, .index = 0};
}
}
VALIDATION_LOG << "No present queue found.";
return std::nullopt;
}
std::shared_ptr<ContextVK> ContextVK::Create(
PFN_vkGetInstanceProcAddr proc_address_callback,
const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
@@ -436,14 +455,27 @@ vk::Instance ContextVK::GetInstance() const {
return *instance_;
}
std::unique_ptr<impeller::SwapchainVK> ContextVK::CreateSwapchain(
vk::SurfaceKHR surface) const {
void ContextVK::SetupSwapchain(vk::SurfaceKHR surface) {
auto present_queue_out = PickPresentQueue(physical_device_, surface);
if (!present_queue_out.has_value()) {
return;
}
present_queue_ =
device_->getQueue(present_queue_out->family, present_queue_out->index);
auto swapchain_details =
SwapchainDetailsVK::Create(physical_device_, surface);
if (!swapchain_details) {
return nullptr;
return;
}
return SwapchainVK::Create(*device_, surface, *swapchain_details);
swapchain_ = SwapchainVK::Create(*device_, surface, *swapchain_details);
surface_producer_ = SurfaceProducerVK::Create({
.device = *device_,
.graphics_queue = graphics_queue_,
.present_queue = present_queue_,
.swapchain = swapchain_.get(),
});
}
} // namespace impeller

View File

@@ -14,6 +14,7 @@
#include "impeller/renderer/backend/vulkan/pipeline_library_vk.h"
#include "impeller/renderer/backend/vulkan/sampler_library_vk.h"
#include "impeller/renderer/backend/vulkan/shader_library_vk.h"
#include "impeller/renderer/backend/vulkan/surface_producer_vk.h"
#include "impeller/renderer/backend/vulkan/swapchain_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include "impeller/renderer/context.h"
@@ -58,8 +59,7 @@ class ContextVK final : public Context, public BackendCast<ContextVK, Context> {
vk::Instance GetInstance() const;
std::unique_ptr<impeller::SwapchainVK> CreateSwapchain(
vk::SurfaceKHR surface) const;
void SetupSwapchain(vk::SurfaceKHR surface);
private:
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner_;
@@ -74,7 +74,10 @@ class ContextVK final : public Context, public BackendCast<ContextVK, Context> {
vk::Queue graphics_queue_;
vk::Queue compute_queue_;
vk::Queue transfer_queue_;
vk::Queue present_queue_;
std::unique_ptr<SwapchainVK> swapchain_;
std::unique_ptr<CommandPoolVK> graphics_command_pool_;
std::unique_ptr<SurfaceProducerVK> surface_producer_;
bool is_valid_ = false;
ContextVK(

View File

@@ -151,6 +151,34 @@ constexpr vk::Format ToVKImageFormat(PixelFormat format) {
}
}
constexpr PixelFormat ToPixelFormat(vk::Format format) {
switch (format) {
case vk::Format::eUndefined:
return PixelFormat::kUnknown;
case vk::Format::eA8B8G8R8UnormPack32:
return PixelFormat::kA8UNormInt;
case vk::Format::eR8G8B8A8Unorm:
return PixelFormat::kR8G8B8A8UNormInt;
case vk::Format::eR8G8B8A8Srgb:
return PixelFormat::kR8G8B8A8UNormIntSRGB;
case vk::Format::eB8G8R8A8Unorm:
return PixelFormat::kB8G8R8A8UNormInt;
case vk::Format::eB8G8R8A8Srgb:
return PixelFormat::kB8G8R8A8UNormIntSRGB;
case vk::Format::eS8Uint:
return PixelFormat::kS8UInt;
default:
return PixelFormat::kUnknown;
}
}
constexpr vk::SampleCountFlagBits ToVKSampleCount(SampleCount sample_count) {
switch (sample_count) {
case SampleCount::kCount1:

View File

@@ -0,0 +1,161 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "impeller/renderer/backend/vulkan/surface_producer_vk.h"
#include <array>
#include "fml/logging.h"
#include "impeller/renderer/backend/vulkan/surface_vk.h"
namespace impeller {
std::unique_ptr<SurfaceProducerVK> SurfaceProducerVK::Create(
const SurfaceProducerCreateInfoVK& create_info) {
auto surface_producer = std::make_unique<SurfaceProducerVK>(create_info);
if (!surface_producer->SetupSyncObjects()) {
FML_LOG(ERROR) << "Failed to setup sync objects.";
return nullptr;
}
return surface_producer;
}
SurfaceProducerVK::SurfaceProducerVK(
const SurfaceProducerCreateInfoVK& create_info)
: create_info_(create_info) {}
SurfaceProducerVK::~SurfaceProducerVK() = default;
std::unique_ptr<Surface> SurfaceProducerVK::AcquireSurface(
vk::CommandBuffer command_buffer) {
auto fence_wait_res = create_info_.device.waitForFences({*in_flight_fence_},
VK_TRUE, UINT64_MAX);
if (fence_wait_res != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to wait for fence: "
<< vk::to_string(fence_wait_res);
return nullptr;
}
auto fence_reset_res = create_info_.device.resetFences({*in_flight_fence_});
if (fence_reset_res != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to reset fence: "
<< vk::to_string(fence_reset_res);
return nullptr;
}
uint32_t image_index;
auto acuire_image_res = create_info_.device.acquireNextImageKHR(
create_info_.swapchain->GetSwapchain(), UINT64_MAX,
*image_available_semaphore_, {}, &image_index);
if (acuire_image_res != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to acquire next image: "
<< vk::to_string(acuire_image_res);
return nullptr;
}
SurfaceVK::SwapCallback swap_callback = [this, image_index,
command_buffer]() {
if (!Submit(command_buffer)) {
return false;
}
return Present(image_index);
};
return SurfaceVK::WrapSwapchainImage(
create_info_.swapchain->GetSwapchainImage(image_index),
std::move(swap_callback));
}
bool SurfaceProducerVK::SetupSyncObjects() {
vk::SemaphoreCreateInfo semaphore_create_info;
{
auto image_avail_res =
create_info_.device.createSemaphoreUnique(semaphore_create_info);
if (image_avail_res.result != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to create image available semaphore: "
<< vk::to_string(image_avail_res.result);
return false;
}
image_available_semaphore_ = std::move(image_avail_res.value);
}
{
auto render_finished_res =
create_info_.device.createSemaphoreUnique(semaphore_create_info);
if (render_finished_res.result != vk::Result::eSuccess) {
FML_LOG(ERROR) << "Failed to create render finished semaphore: "
<< vk::to_string(render_finished_res.result);
return false;
}
render_finished_semaphore_ = std::move(render_finished_res.value);
}
vk::FenceCreateInfo fence_create_info;
fence_create_info.flags = vk::FenceCreateFlagBits::eSignaled;
{
auto fence_res = create_info_.device.createFenceUnique(fence_create_info);
if (fence_res.result != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to create fence: "
<< vk::to_string(fence_res.result);
return false;
}
in_flight_fence_ = std::move(fence_res.value);
}
return true;
}
bool SurfaceProducerVK::Submit(vk::CommandBuffer buffer) {
vk::SubmitInfo submit_info;
std::array<vk::PipelineStageFlags, 1> wait_stages = {
vk::PipelineStageFlagBits::eColorAttachmentOutput};
submit_info.setWaitDstStageMask(wait_stages);
std::array<vk::Semaphore, 1> wait_semaphores = {*image_available_semaphore_};
submit_info.setWaitSemaphores(wait_semaphores);
std::array<vk::Semaphore, 1> signal_semaphores = {
*render_finished_semaphore_};
submit_info.setSignalSemaphores(signal_semaphores);
std::array<vk::CommandBuffer, 1> command_buffers = {buffer};
submit_info.setCommandBuffers(command_buffers);
auto graphics_submit_res =
create_info_.graphics_queue.submit({submit_info}, *in_flight_fence_);
if (graphics_submit_res != vk::Result::eSuccess) {
FML_LOG(ERROR) << "Failed to submit graphics queue: "
<< vk::to_string(graphics_submit_res);
return false;
}
return true;
}
bool SurfaceProducerVK::Present(uint32_t image_index) {
vk::PresentInfoKHR present_info;
std::array<vk::Semaphore, 1> signal_semaphores = {
*render_finished_semaphore_};
present_info.setWaitSemaphores(signal_semaphores);
std::array<vk::SwapchainKHR, 1> swapchains = {
create_info_.swapchain->GetSwapchain()};
present_info.setSwapchains(swapchains);
std::array<uint32_t, 1> image_indices = {image_index};
present_info.setImageIndices(image_indices);
auto present_res = create_info_.present_queue.presentKHR(present_info);
if (present_res != vk::Result::eSuccess) {
FML_LOG(ERROR) << "Failed to present: " << vk::to_string(present_res);
return false;
}
return true;
}
} // namespace impeller

View File

@@ -0,0 +1,51 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#pragma once
#include <memory>
#include "flutter/fml/macros.h"
#include "impeller/renderer/backend/vulkan/swapchain_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include "impeller/renderer/surface.h"
namespace impeller {
struct SurfaceProducerCreateInfoVK {
vk::Device device;
vk::Queue graphics_queue;
vk::Queue present_queue;
SwapchainVK* swapchain;
};
class SurfaceProducerVK {
public:
static std::unique_ptr<SurfaceProducerVK> Create(
const SurfaceProducerCreateInfoVK& create_info);
explicit SurfaceProducerVK(const SurfaceProducerCreateInfoVK& create_info);
~SurfaceProducerVK();
std::unique_ptr<Surface> AcquireSurface(vk::CommandBuffer command_buffer);
private:
bool SetupSyncObjects();
bool Submit(vk::CommandBuffer buffer);
bool Present(uint32_t image_index);
const SurfaceProducerCreateInfoVK create_info_;
// sync objects
vk::UniqueSemaphore image_available_semaphore_;
vk::UniqueSemaphore render_finished_semaphore_;
vk::UniqueFence in_flight_fence_;
FML_DISALLOW_COPY_AND_ASSIGN(SurfaceProducerVK);
};
} // namespace impeller

View File

@@ -5,7 +5,62 @@
#include "impeller/renderer/backend/vulkan/surface_vk.h"
namespace impeller {
std::unique_ptr<SurfaceVK> SurfaceVK::WrapSwapchainImage(
SwapchainImageVK* swapchain_image,
SwapCallback swap_callback) {
if (!swapchain_image) {
return nullptr;
}
//
TextureDescriptor color0_tex;
color0_tex.type = TextureType::kTexture2D;
color0_tex.format = swapchain_image->GetPixelFormat();
color0_tex.size = swapchain_image->GetSize();
color0_tex.usage = static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
color0_tex.sample_count = SampleCount::kCount1;
ColorAttachment color0;
// TODO (kaushikiska): this needs to be fixed.
// color0.texture = std::make_shared<TextureGLES>(
// gl_context.GetReactor(), std::move(color0_tex),
// TextureGLES::IsWrapped::kWrapped);
color0.clear_color = Color::DarkSlateGray();
color0.load_action = LoadAction::kClear;
color0.store_action = StoreAction::kStore;
TextureDescriptor stencil0_tex;
stencil0_tex.type = TextureType::kTexture2D;
stencil0_tex.format = swapchain_image->GetPixelFormat();
stencil0_tex.size = swapchain_image->GetSize();
stencil0_tex.usage =
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
stencil0_tex.sample_count = SampleCount::kCount1;
StencilAttachment stencil0;
stencil0.clear_stencil = 0;
// TODO (kaushikiska): this needs to be fixed.
// stencil0.texture = std::make_shared<TextureGLES>(
// gl_context.GetReactor(), std::move(stencil0_tex),
// TextureGLES::IsWrapped::kWrapped);
stencil0.load_action = LoadAction::kClear;
stencil0.store_action = StoreAction::kDontCare;
RenderTarget render_target_desc;
render_target_desc.SetColorAttachment(color0, 0u);
return std::unique_ptr<SurfaceVK>(new SurfaceVK(std::move(render_target_desc),
swapchain_image,
std::move(swap_callback)));
}
SurfaceVK::SurfaceVK(RenderTarget target,
SwapchainImageVK* swapchain_image,
SwapCallback swap_callback) {}
SurfaceVK::~SurfaceVK() = default;
bool SurfaceVK::Present() const {
return swap_callback_ ? swap_callback_() : false;
}
} // namespace impeller

View File

@@ -4,19 +4,33 @@
#pragma once
#include <memory>
#include "flutter/fml/macros.h"
#include "impeller/renderer/context.h"
#include "impeller/renderer/backend/vulkan/context_vk.h"
#include "impeller/renderer/backend/vulkan/swapchain_vk.h"
#include "impeller/renderer/surface.h"
namespace impeller {
class SurfaceVK final : public Surface {
public:
using SwapCallback = std::function<bool(void)>;
static std::unique_ptr<SurfaceVK> WrapSwapchainImage(
SwapchainImageVK* swapchain_image,
SwapCallback swap_callback);
SurfaceVK(RenderTarget target,
SwapchainImageVK* swapchain_image,
SwapCallback swap_callback);
// |Surface|
~SurfaceVK() override;
private:
SurfaceVK(RenderTarget target);
const SwapchainImageVK* swapchain_image_;
SwapCallback swap_callback_;
// |Surface|
bool Present() const override;

View File

@@ -6,6 +6,7 @@
#include "fml/logging.h"
#include "impeller/base/validation.h"
#include "impeller/renderer/backend/vulkan/formats_vk.h"
namespace impeller {
@@ -47,9 +48,9 @@ std::unique_ptr<SwapchainVK> SwapchainVK::Create(vk::Device device,
return nullptr;
}
auto swapchain = std::make_unique<SwapchainVK>(std::move(swapchain_res.value),
surface_format.format, extent);
if (!swapchain->CreateSwapchainImages(device)) {
auto swapchain = std::make_unique<SwapchainVK>(
device, std::move(swapchain_res.value), surface_format.format, extent);
if (!swapchain->CreateSwapchainImages()) {
VALIDATION_LOG << "Failed to create swapchain images.";
return nullptr;
}
@@ -57,9 +58,9 @@ std::unique_ptr<SwapchainVK> SwapchainVK::Create(vk::Device device,
return swapchain;
}
bool SwapchainVK::CreateSwapchainImages(vk::Device device) {
bool SwapchainVK::CreateSwapchainImages() {
FML_DCHECK(swapchain_images_.empty()) << "Swapchain images already created";
auto res = device.getSwapchainImagesKHR(*swapchain_);
auto res = device_.getSwapchainImagesKHR(*swapchain_);
if (res.result != vk::Result::eSuccess) {
FML_CHECK(false) << "Failed to get swapchain images: "
<< vk::to_string(res.result);
@@ -86,7 +87,7 @@ bool SwapchainVK::CreateSwapchainImages(vk::Device device) {
create_info.subresourceRange.baseArrayLayer = 0;
create_info.subresourceRange.layerCount = 1;
auto img_view_res = device.createImageViewUnique(create_info);
auto img_view_res = device_.createImageViewUnique(create_info);
if (img_view_res.result != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to create image view: "
<< vk::to_string(img_view_res.result);
@@ -100,10 +101,12 @@ bool SwapchainVK::CreateSwapchainImages(vk::Device device) {
return true;
}
SwapchainVK::SwapchainVK(vk::UniqueSwapchainKHR swapchain,
SwapchainVK::SwapchainVK(vk::Device device,
vk::UniqueSwapchainKHR swapchain,
vk::Format image_format,
vk::Extent2D extent)
: swapchain_(std::move(swapchain)),
: device_(device),
swapchain_(std::move(swapchain)),
image_format_(image_format),
extent_(extent) {}
@@ -118,6 +121,23 @@ SwapchainImageVK::SwapchainImageVK(vk::Image image,
image_format_(image_format),
extent_(extent) {}
vk::SwapchainKHR SwapchainVK::GetSwapchain() const {
return *swapchain_;
}
SwapchainImageVK* SwapchainVK::GetSwapchainImage(uint32_t image_index) const {
FML_DCHECK(image_index < swapchain_images_.size());
return swapchain_images_[image_index].get();
}
PixelFormat SwapchainImageVK::GetPixelFormat() const {
return ToPixelFormat(image_format_);
}
ISize SwapchainImageVK::GetSize() const {
return ISize(extent_.width, extent_.height);
}
SwapchainImageVK::~SwapchainImageVK() = default;
} // namespace impeller

View File

@@ -7,7 +7,9 @@
#include <vector>
#include "flutter/fml/macros.h"
#include "impeller/geometry/size.h"
#include "impeller/renderer/backend/vulkan/swapchain_details_vk.h"
#include "impeller/renderer/surface.h"
namespace impeller {
@@ -20,6 +22,10 @@ class SwapchainImageVK {
~SwapchainImageVK();
PixelFormat GetPixelFormat() const;
ISize GetSize() const;
private:
vk::Image image_;
vk::UniqueImageView image_view_;
@@ -35,15 +41,21 @@ class SwapchainVK {
vk::SurfaceKHR surface,
SwapchainDetailsVK& details);
SwapchainVK(vk::UniqueSwapchainKHR swapchain,
SwapchainVK(vk::Device device,
vk::UniqueSwapchainKHR swapchain,
vk::Format image_format,
vk::Extent2D extent);
~SwapchainVK();
private:
bool CreateSwapchainImages(vk::Device device);
vk::SwapchainKHR GetSwapchain() const;
SwapchainImageVK* GetSwapchainImage(uint32_t image_index) const;
private:
bool CreateSwapchainImages();
vk::Device device_;
vk::UniqueSwapchainKHR swapchain_;
vk::Format image_format_;
vk::Extent2D extent_;

View File

@@ -18,7 +18,7 @@ class Surface {
public:
Surface();
Surface(RenderTarget target_desc);
explicit Surface(RenderTarget target_desc);
virtual ~Surface();