From 53383fbdf76e3b8f1510fcb26bdde24be4b06686 Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Mon, 17 Oct 2022 11:11:40 -0500 Subject: [PATCH] [Impeller] Add WSI support for Vulkan on Linux and Windows (flutter/engine#36762) There are additional instance extensions that need to be enabled for this. Having any one of them is sufficient on each of these platforms. There were also some `VALIDATION_LOG`s that would fail in the process of picking a valid physical device, this would incorrectly exit early when there were multiple devices and a latter device is valid. Also improves logging for playground when glfw fails to get a surface. --- engine/src/flutter/fml/string_conversion.cc | 12 +++++ engine/src/flutter/fml/string_conversion.h | 4 ++ .../backend/vulkan/playground_impl_vk.cc | 8 ++- .../renderer/backend/vulkan/context_vk.cc | 51 ++++++++++++++++--- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/engine/src/flutter/fml/string_conversion.cc b/engine/src/flutter/fml/string_conversion.cc index 7b718fef19..d55577c4e6 100644 --- a/engine/src/flutter/fml/string_conversion.cc +++ b/engine/src/flutter/fml/string_conversion.cc @@ -6,6 +6,7 @@ #include #include +#include #include #include "flutter/fml/build_config.h" @@ -22,6 +23,17 @@ namespace fml { using Utf16StringConverter = std::wstring_convert, char16_t>; +std::string Join(const std::vector& vec, const char* delim) { + std::stringstream res; + for (size_t i = 0; i < vec.size(); ++i) { + res << vec[i]; + if (i < vec.size() - 1) { + res << delim; + } + } + return res.str(); +} + std::string Utf16ToUtf8(const std::u16string_view string) { Utf16StringConverter converter; return converter.to_bytes(string.data()); diff --git a/engine/src/flutter/fml/string_conversion.h b/engine/src/flutter/fml/string_conversion.h index 5c2402980d..eb7d840c3c 100644 --- a/engine/src/flutter/fml/string_conversion.h +++ b/engine/src/flutter/fml/string_conversion.h @@ -6,9 +6,13 @@ #define FLUTTER_FML_STRING_CONVERSION_H_ #include +#include namespace fml { +// Returns a string joined by the given delimiter. +std::string Join(const std::vector& vec, const char* delimiter); + // Returns a UTF-8 encoded equivalent of a UTF-16 encoded input string. std::string Utf16ToUtf8(const std::u16string_view string); diff --git a/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.cc b/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.cc index 0088ccf1cd..334d35e1d6 100644 --- a/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.cc +++ b/engine/src/flutter/impeller/playground/backend/vulkan/playground_impl_vk.cc @@ -87,7 +87,13 @@ void PlaygroundImplVK::SetupSwapchain() { auto window = reinterpret_cast(handle_.get()); vk::Instance instance = context_vk->GetInstance(); VkSurfaceKHR surface_tmp; - ::glfwCreateWindowSurface(instance, window, nullptr, &surface_tmp); + auto res = vk::Result{ + ::glfwCreateWindowSurface(instance, window, nullptr, &surface_tmp)}; + if (res != vk::Result::eSuccess) { + VALIDATION_LOG << "Could not create surface for GLFW window: " + << vk::to_string(res); + return; + } vk::UniqueSurfaceKHR surface{surface_tmp, instance}; context_vk->SetupSwapchain(std::move(surface)); } diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc index 0a6dbbc385..3e071ebb20 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc @@ -8,8 +8,10 @@ #include #include #include +#include #include "flutter/fml/build_config.h" +#include "flutter/fml/string_conversion.h" #include "flutter/fml/trace_event.h" #include "impeller/base/validation.h" #include "impeller/base/work_queue_common.h" @@ -32,6 +34,20 @@ static std::set kRequiredDeviceExtensions = { #endif }; +std::vector kRequiredWSIInstanceExtensions = { +#if FML_OS_WIN + "VK_KHR_win32_surface", +#elif FML_OS_ANDROID + "VK_KHR_android_surface", +#elif FML_OS_LINUX + "VK_KHR_xcb_surface", + "VK_KHR_xlib_surface", + "VK_KHR_wayland_surface", +#elif FML_OS_MACOSX + "VK_EXT_metal_surface", +#endif +}; + #if FML_OS_MACOSX static const char* MVK_MACOS_SURFACE_EXT = "VK_MVK_macos_surface"; #endif @@ -50,17 +66,19 @@ static bool HasRequiredQueues(const vk::PhysicalDevice& device) { vk::QueueFlagBits::eTransfer)); } -static bool HasRequiredExtensions(const vk::PhysicalDevice& device) { +static std::vector HasRequiredExtensions( + const vk::PhysicalDevice& device) { std::set exts; + std::vector missing; for (const auto& ext : device.enumerateDeviceExtensionProperties().value) { exts.insert(ext.extensionName); } for (const auto& req_ext : kRequiredDeviceExtensions) { if (exts.count(req_ext) != 1u) { - return false; + missing.push_back(req_ext); } } - return true; + return missing; } static vk::PhysicalDeviceFeatures GetRequiredPhysicalDeviceFeatures() { @@ -80,15 +98,17 @@ static bool HasRequiredProperties(const vk::PhysicalDevice& device) { static bool IsPhysicalDeviceCompatible(const vk::PhysicalDevice& device) { if (!HasRequiredQueues(device)) { - VALIDATION_LOG << "Device doesn't have required queues."; + FML_LOG(ERROR) << "Device doesn't have required queues."; return false; } - if (!HasRequiredExtensions(device)) { - VALIDATION_LOG << "Device doesn't have required extensions."; + auto missing_exts = HasRequiredExtensions(device); + if (!missing_exts.empty()) { + FML_LOG(ERROR) << "Device doesn't have required extensions: " + << fml::Join(missing_exts, ", "); return false; } if (!HasRequiredProperties(device)) { - VALIDATION_LOG << "Device doesn't have required properties."; + FML_LOG(ERROR) << "Device doesn't have required properties."; return false; } return true; @@ -251,6 +271,23 @@ ContextVK::ContextVK( } enabled_extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); + //---------------------------------------------------------------------------- + /// Enable WSI Instance Extensions. Having any one of these is sufficient. + /// + bool has_wsi_extensions = false; + for (const auto& wsi_ext : kRequiredWSIInstanceExtensions) { + if (capabilities->HasExtension(wsi_ext)) { + enabled_extensions.push_back(wsi_ext.c_str()); + has_wsi_extensions = true; + } + } + if (!has_wsi_extensions) { + VALIDATION_LOG + << "Instance doesn't have any of the required WSI extensions: " + << fml::Join(kRequiredWSIInstanceExtensions, ", "); + return; + } + //---------------------------------------------------------------------------- /// Enable any and all validation as well as debug toggles. ///