[impeller] [vulkan] Add support for swapchain creation and swapchain (flutter/engine#35199)

images

Also wires up playground surface creation and swapchain setup.
This commit is contained in:
Kaushik Iska
2022-08-08 16:53:28 -07:00
committed by GitHub
parent 0ac321bc81
commit 16de1bc82c
10 changed files with 421 additions and 2 deletions

View File

@@ -795,6 +795,10 @@ 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_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/surface_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain_details_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain_details_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/texture_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/texture_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/vertex_descriptor_vk.cc

View File

@@ -6,6 +6,7 @@
#include "impeller/renderer/backend/vulkan/vk.h"
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include "flutter/fml/logging.h"
@@ -34,13 +35,35 @@ ShaderLibraryMappingsForPlayground() {
};
}
void PlaygroundImplVK::DestroyWindowHandle(WindowHandle handle) {
if (!handle) {
return;
}
::glfwDestroyWindow(reinterpret_cast<GLFWwindow*>(handle));
}
PlaygroundImplVK::PlaygroundImplVK()
: concurrent_loop_(fml::ConcurrentMessageLoop::Create()) {
: concurrent_loop_(fml::ConcurrentMessageLoop::Create()),
handle_(nullptr, &DestroyWindowHandle) {
if (!::glfwVulkanSupported()) {
VALIDATION_LOG << "Attempted to initialize a Vulkan playground on a system "
"that does not support Vulkan.";
return;
}
::glfwDefaultWindowHints();
::glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
::glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
auto window =
::glfwCreateWindow(800, 600, "Test Vulkan Window", nullptr, nullptr);
if (!window) {
VALIDATION_LOG << "Unable to create glfw window";
return;
}
handle_.reset(window);
auto context = ContextVK::Create(reinterpret_cast<PFN_vkGetInstanceProcAddr>(
&::glfwGetInstanceProcAddress), //
ShaderLibraryMappingsForPlayground(), //
@@ -55,6 +78,18 @@ PlaygroundImplVK::PlaygroundImplVK()
}
context_ = std::move(context);
SetupSwapchain();
}
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_);
}
PlaygroundImplVK::~PlaygroundImplVK() = default;
@@ -66,7 +101,7 @@ std::shared_ptr<Context> PlaygroundImplVK::GetContext() const {
// |PlaygroundImpl|
PlaygroundImpl::WindowHandle PlaygroundImplVK::GetWindowHandle() const {
FML_UNREACHABLE();
return handle_.get();
}
// |PlaygroundImpl|

View File

@@ -7,6 +7,8 @@
#include "flutter/fml/concurrent_message_loop.h"
#include "flutter/fml/macros.h"
#include "impeller/playground/playground_impl.h"
#include "impeller/renderer/backend/vulkan/swapchain_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
namespace impeller {
@@ -20,6 +22,15 @@ class PlaygroundImplVK final : public PlaygroundImpl {
std::shared_ptr<fml::ConcurrentMessageLoop> concurrent_loop_;
std::shared_ptr<Context> context_;
// windows
static void DestroyWindowHandle(WindowHandle handle);
using UniqueHandle = std::unique_ptr<void, decltype(&DestroyWindowHandle)>;
UniqueHandle handle_;
// surface and swapchain
VkSurfaceKHR surface_;
std::unique_ptr<SwapchainVK> swapchain_;
// |PlaygroundImpl|
std::shared_ptr<Context> GetContext() const override;
@@ -30,6 +41,8 @@ class PlaygroundImplVK final : public PlaygroundImpl {
std::unique_ptr<Surface> AcquireSurfaceFrame(
std::shared_ptr<Context> context) override;
void SetupSwapchain();
FML_DISALLOW_COPY_AND_ASSIGN(PlaygroundImplVK);
};

View File

@@ -36,6 +36,10 @@ impeller_component("vulkan") {
"shader_library_vk.h",
"surface_vk.cc",
"surface_vk.h",
"swapchain_details_vk.cc",
"swapchain_details_vk.h",
"swapchain_vk.cc",
"swapchain_vk.h",
"texture_vk.cc",
"texture_vk.h",
"vertex_descriptor_vk.cc",

View File

@@ -14,6 +14,7 @@
#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/swapchain_details_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
@@ -27,6 +28,10 @@ static std::set<std::string> kRequiredDeviceExtensions = {
#endif
};
#if FML_OS_MACOSX
static const char* MVK_MACOS_SURFACE_EXT = "VK_MVK_macos_surface";
#endif
static bool HasRequiredQueues(const vk::PhysicalDevice& device) {
auto present_flags = vk::QueueFlags{};
for (const auto& queue : device.getQueueFamilyProperties()) {
@@ -205,6 +210,14 @@ ContextVK::ContextVK(
// is a requirement for opting into Molten VK on Mac.
enabled_extensions.push_back(
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
// Required for glfw macOS surfaces.
if (!capabilities->HasExtension(MVK_MACOS_SURFACE_EXT)) {
VALIDATION_LOG << "On Mac: Required extension " << MVK_MACOS_SURFACE_EXT
<< " absent.";
return;
}
enabled_extensions.push_back(MVK_MACOS_SURFACE_EXT);
#endif // FML_OS_MACOSX
//----------------------------------------------------------------------------
@@ -308,6 +321,8 @@ ContextVK::ContextVK(
auto compute_queue =
PickQueue(physical_device.value(), vk::QueueFlagBits::eCompute);
physical_device_ = physical_device.value();
if (!graphics_queue.has_value() || !transfer_queue.has_value() ||
!compute_queue.has_value()) {
VALIDATION_LOG << "Could not pick device queues.";
@@ -423,4 +438,18 @@ std::shared_ptr<CommandBuffer> ContextVK::CreateTransferCommandBuffer() const {
FML_UNREACHABLE();
}
vk::Instance ContextVK::GetInstance() const {
return *instance_;
}
std::unique_ptr<impeller::SwapchainVK> ContextVK::CreateSwapchain(
vk::SurfaceKHR surface) const {
auto swapchain_details =
SwapchainDetailsVK::Create(physical_device_, surface);
if (!swapchain_details) {
return nullptr;
}
return SwapchainVK::Create(*device_, surface, *swapchain_details);
}
} // namespace impeller

View File

@@ -13,6 +13,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/swapchain_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include "impeller/renderer/context.h"
@@ -54,10 +55,16 @@ class ContextVK final : public Context, public BackendCast<ContextVK, Context> {
return true;
}
vk::Instance GetInstance() const;
std::unique_ptr<impeller::SwapchainVK> CreateSwapchain(
vk::SurfaceKHR surface) const;
private:
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner_;
vk::UniqueInstance instance_;
vk::UniqueDebugUtilsMessengerEXT debug_messenger_;
vk::PhysicalDevice physical_device_;
vk::UniqueDevice device_;
std::shared_ptr<Allocator> allocator_;
std::shared_ptr<ShaderLibraryVK> shader_library_;

View File

@@ -0,0 +1,107 @@
// 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/swapchain_details_vk.h"
#include "impeller/base/validation.h"
namespace impeller {
std::unique_ptr<SwapchainDetailsVK> SwapchainDetailsVK::Create(
vk::PhysicalDevice physical_device,
vk::SurfaceKHR surface) {
FML_DCHECK(surface) << "surface provided as nullptr";
auto capabilities_res = physical_device.getSurfaceCapabilitiesKHR(surface);
if (capabilities_res.result != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to get surface capabilities: "
<< vk::to_string(capabilities_res.result);
return nullptr;
}
vk::SurfaceCapabilitiesKHR capabilities = capabilities_res.value;
auto surface_formats_res = physical_device.getSurfaceFormatsKHR(surface);
if (surface_formats_res.result != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to get surface formats: "
<< vk::to_string(surface_formats_res.result);
return nullptr;
}
std::vector<vk::SurfaceFormatKHR> surface_formats = surface_formats_res.value;
auto surface_present_modes_res =
physical_device.getSurfacePresentModesKHR(surface);
if (surface_present_modes_res.result != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to get surface present modes: "
<< vk::to_string(surface_present_modes_res.result);
return nullptr;
}
std::vector<vk::PresentModeKHR> surface_present_modes =
surface_present_modes_res.value;
return std::make_unique<SwapchainDetailsVK>(capabilities, surface_formats,
surface_present_modes);
}
vk::SurfaceFormatKHR SwapchainDetailsVK::PickSurfaceFormat() const {
for (const auto& format : surface_formats_) {
if ((format.format == vk::Format::eR8G8B8A8Unorm ||
format.format == vk::Format::eB8G8R8A8Unorm) &&
format.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear) {
return format;
}
}
VALIDATION_LOG << "Picking a sub-optimal surface format.";
return surface_formats_[0];
}
vk::PresentModeKHR SwapchainDetailsVK::PickPresentationMode() const {
for (const auto& mode : present_modes_) {
if (mode == vk::PresentModeKHR::eMailbox) {
return mode;
}
}
VALIDATION_LOG << "Picking a sub-optimal presentation mode.";
// Vulkan spec dictates that FIFO is always available.
return vk::PresentModeKHR::eFifo;
}
vk::Extent2D SwapchainDetailsVK::PickExtent() const {
if (surface_capabilities_.currentExtent.width !=
std::numeric_limits<uint32_t>::max()) {
return surface_capabilities_.currentExtent;
}
vk::Extent2D actual_extent = {
std::max(surface_capabilities_.minImageExtent.width,
std::min(surface_capabilities_.maxImageExtent.width,
surface_capabilities_.currentExtent.width)),
std::max(surface_capabilities_.minImageExtent.height,
std::min(surface_capabilities_.maxImageExtent.height,
surface_capabilities_.currentExtent.height))};
return actual_extent;
}
uint32_t SwapchainDetailsVK::GetImageCount() const {
uint32_t image_count = surface_capabilities_.minImageCount;
// for triple buffering
return image_count + 1;
}
vk::SurfaceTransformFlagBitsKHR SwapchainDetailsVK::GetTransform() const {
return surface_capabilities_.currentTransform;
}
SwapchainDetailsVK::SwapchainDetailsVK(
vk::SurfaceCapabilitiesKHR capabilities,
std::vector<vk::SurfaceFormatKHR> surface_formats,
std::vector<vk::PresentModeKHR> surface_present_modes)
: surface_capabilities_(capabilities),
surface_formats_(surface_formats),
present_modes_(surface_present_modes) {}
SwapchainDetailsVK::~SwapchainDetailsVK() = default;
} // namespace impeller

View File

@@ -0,0 +1,42 @@
// 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 "flutter/fml/macros.h"
#include "impeller/renderer/backend/vulkan/vk.h"
namespace impeller {
class SwapchainDetailsVK {
public:
static std::unique_ptr<SwapchainDetailsVK> Create(
vk::PhysicalDevice physical_device,
vk::SurfaceKHR surface);
SwapchainDetailsVK(vk::SurfaceCapabilitiesKHR capabilities,
std::vector<vk::SurfaceFormatKHR> surface_formats,
std::vector<vk::PresentModeKHR> surface_present_modes);
~SwapchainDetailsVK();
vk::SurfaceFormatKHR PickSurfaceFormat() const;
vk::PresentModeKHR PickPresentationMode() const;
vk::Extent2D PickExtent() const;
uint32_t GetImageCount() const;
vk::SurfaceTransformFlagBitsKHR GetTransform() const;
private:
vk::SurfaceCapabilitiesKHR surface_capabilities_;
std::vector<vk::SurfaceFormatKHR> surface_formats_;
std::vector<vk::PresentModeKHR> present_modes_;
FML_DISALLOW_COPY_AND_ASSIGN(SwapchainDetailsVK);
};
} // namespace impeller

View File

@@ -0,0 +1,123 @@
// 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/swapchain_vk.h"
#include "fml/logging.h"
#include "impeller/base/validation.h"
namespace impeller {
std::unique_ptr<SwapchainVK> SwapchainVK::Create(vk::Device device,
vk::SurfaceKHR surface,
SwapchainDetailsVK& details) {
vk::SurfaceFormatKHR surface_format = details.PickSurfaceFormat();
vk::PresentModeKHR present_mode = details.PickPresentationMode();
vk::Extent2D extent = details.PickExtent();
vk::SwapchainCreateInfoKHR create_info;
create_info.surface = surface;
create_info.imageFormat = surface_format.format;
create_info.imageColorSpace = surface_format.colorSpace;
create_info.presentMode = present_mode;
create_info.imageExtent = extent;
create_info.minImageCount = details.GetImageCount();
create_info.imageArrayLayers = 1;
create_info.imageUsage = vk::ImageUsageFlagBits::eColorAttachment;
create_info.preTransform = details.GetTransform();
create_info.compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque;
create_info.clipped = VK_TRUE;
create_info.imageSharingMode = vk::SharingMode::eExclusive;
{
// TODO (kaushikiska): This needs to change if graphics queue is not the
// same as present queue which is rare.
create_info.queueFamilyIndexCount = 0;
create_info.pQueueFamilyIndices = nullptr;
}
create_info.oldSwapchain = nullptr;
auto swapchain_res = device.createSwapchainKHRUnique(create_info);
if (swapchain_res.result != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to create swapchain: "
<< vk::to_string(swapchain_res.result);
return nullptr;
}
auto swapchain = std::make_unique<SwapchainVK>(std::move(swapchain_res.value),
surface_format.format, extent);
if (!swapchain->CreateSwapchainImages(device)) {
VALIDATION_LOG << "Failed to create swapchain images.";
return nullptr;
}
return swapchain;
}
bool SwapchainVK::CreateSwapchainImages(vk::Device device) {
FML_DCHECK(swapchain_images_.empty()) << "Swapchain images already created";
auto res = device.getSwapchainImagesKHR(*swapchain_);
if (res.result != vk::Result::eSuccess) {
FML_CHECK(false) << "Failed to get swapchain images: "
<< vk::to_string(res.result);
return false;
}
std::vector<vk::Image> images = res.value;
for (const auto& image : images) {
vk::ImageViewCreateInfo create_info;
create_info.image = image;
create_info.viewType = vk::ImageViewType::e2D;
create_info.format = image_format_;
create_info.components.r = vk::ComponentSwizzle::eIdentity;
create_info.components.g = vk::ComponentSwizzle::eIdentity;
create_info.components.b = vk::ComponentSwizzle::eIdentity;
create_info.components.a = vk::ComponentSwizzle::eIdentity;
// TODO (kaushikiska): This has to match up to the texture we will be
// rendering to (TextureVK).
create_info.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
create_info.subresourceRange.baseMipLevel = 0;
create_info.subresourceRange.levelCount = 1;
create_info.subresourceRange.baseArrayLayer = 0;
create_info.subresourceRange.layerCount = 1;
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);
return false;
}
swapchain_images_.push_back(std::make_unique<SwapchainImageVK>(
image, std::move(img_view_res.value), image_format_, extent_));
}
return true;
}
SwapchainVK::SwapchainVK(vk::UniqueSwapchainKHR swapchain,
vk::Format image_format,
vk::Extent2D extent)
: swapchain_(std::move(swapchain)),
image_format_(image_format),
extent_(extent) {}
SwapchainVK::~SwapchainVK() = default;
SwapchainImageVK::SwapchainImageVK(vk::Image image,
vk::UniqueImageView image_view,
vk::Format image_format,
vk::Extent2D extent)
: image_(image),
image_view_(std::move(image_view)),
image_format_(image_format),
extent_(extent) {}
SwapchainImageVK::~SwapchainImageVK() = default;
} // namespace impeller

View File

@@ -0,0 +1,55 @@
// 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 <vector>
#include "flutter/fml/macros.h"
#include "impeller/renderer/backend/vulkan/swapchain_details_vk.h"
namespace impeller {
class SwapchainImageVK {
public:
SwapchainImageVK(vk::Image image,
vk::UniqueImageView image_view,
vk::Format image_format,
vk::Extent2D extent);
~SwapchainImageVK();
private:
vk::Image image_;
vk::UniqueImageView image_view_;
vk::Format image_format_;
vk::Extent2D extent_;
FML_DISALLOW_COPY_AND_ASSIGN(SwapchainImageVK);
};
class SwapchainVK {
public:
static std::unique_ptr<SwapchainVK> Create(vk::Device device,
vk::SurfaceKHR surface,
SwapchainDetailsVK& details);
SwapchainVK(vk::UniqueSwapchainKHR swapchain,
vk::Format image_format,
vk::Extent2D extent);
~SwapchainVK();
private:
bool CreateSwapchainImages(vk::Device device);
vk::UniqueSwapchainKHR swapchain_;
vk::Format image_format_;
vk::Extent2D extent_;
std::vector<std::unique_ptr<SwapchainImageVK>> swapchain_images_;
FML_DISALLOW_COPY_AND_ASSIGN(SwapchainVK);
};
} // namespace impeller