7 #include "flutter/fml/paths.h"
10 #define GLFW_INCLUDE_VULKAN
11 #include <GLFW/glfw3.h>
13 #include "flutter/fml/logging.h"
14 #include "flutter/fml/mapping.h"
15 #include "impeller/entity/vk/entity_shaders_vk.h"
16 #include "impeller/entity/vk/framebuffer_blend_shaders_vk.h"
17 #include "impeller/entity/vk/modern_shaders_vk.h"
18 #include "impeller/fixtures/vk/fixtures_shaders_vk.h"
19 #include "impeller/fixtures/vk/modern_fixtures_shaders_vk.h"
20 #include "impeller/playground/imgui/vk/imgui_shaders_vk.h"
26 #include "impeller/renderer/vk/compute_shaders_vk.h"
30 static std::vector<std::shared_ptr<fml::Mapping>>
33 std::make_shared<fml::NonOwnedMapping>(impeller_entity_shaders_vk_data,
34 impeller_entity_shaders_vk_length),
35 std::make_shared<fml::NonOwnedMapping>(impeller_modern_shaders_vk_data,
36 impeller_modern_shaders_vk_length),
37 std::make_shared<fml::NonOwnedMapping>(
38 impeller_framebuffer_blend_shaders_vk_data,
39 impeller_framebuffer_blend_shaders_vk_length),
40 std::make_shared<fml::NonOwnedMapping>(
41 impeller_fixtures_shaders_vk_data,
42 impeller_fixtures_shaders_vk_length),
43 std::make_shared<fml::NonOwnedMapping>(
44 impeller_modern_fixtures_shaders_vk_data,
45 impeller_modern_fixtures_shaders_vk_length),
46 std::make_shared<fml::NonOwnedMapping>(impeller_imgui_shaders_vk_data,
47 impeller_imgui_shaders_vk_length),
48 std::make_shared<fml::NonOwnedMapping>(
49 impeller_compute_shaders_vk_data, impeller_compute_shaders_vk_length),
57 VkInstance PlaygroundImplVK::global_instance_ = VK_NULL_HANDLE;
59 void PlaygroundImplVK::DestroyWindowHandle(WindowHandle handle) {
63 ::glfwDestroyWindow(
reinterpret_cast<GLFWwindow*
>(handle));
67 :
PlaygroundImpl(switches), handle_(nullptr, &DestroyWindowHandle) {
70 InitGlobalVulkanInstance();
72 ::glfwDefaultWindowHints();
73 ::glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
74 ::glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
76 auto window = ::glfwCreateWindow(1, 1,
"Test",
nullptr,
nullptr);
84 ::glfwGetWindowSize(window, &width, &height);
85 size_ =
ISize{width, height};
87 handle_.reset(window);
91 reinterpret_cast<PFN_vkGetInstanceProcAddr
>(
92 &::glfwGetInstanceProcAddress);
101 if (!context_vk || !context_vk->IsValid()) {
102 VALIDATION_LOG <<
"Could not create Vulkan context in the playground.";
106 VkSurfaceKHR vk_surface;
107 auto res = vk::Result{::glfwCreateWindowSurface(
108 context_vk->GetInstance(),
113 if (res != vk::Result::eSuccess) {
115 << vk::to_string(res);
119 vk::UniqueSurfaceKHR surface{vk_surface, context_vk->GetInstance()};
120 auto context = context_vk->CreateSurfaceContext();
121 if (!context->SetWindowSurface(std::move(surface), size_)) {
126 context_ = std::move(context);
132 std::shared_ptr<Context> PlaygroundImplVK::GetContext()
const {
138 return handle_.get();
142 std::unique_ptr<Surface> PlaygroundImplVK::AcquireSurfaceFrame(
143 std::shared_ptr<Context> context) {
144 SurfaceContextVK* surface_context_vk =
145 reinterpret_cast<SurfaceContextVK*
>(context_.get());
149 ::glfwGetFramebufferSize(
reinterpret_cast<GLFWwindow*
>(handle_.get()), &width,
151 size_ =
ISize{width, height};
152 surface_context_vk->UpdateSurfaceSize(
ISize{width, height});
154 return surface_context_vk->AcquireNextSurface();
165 void PlaygroundImplVK::InitGlobalVulkanInstance() {
166 if (global_instance_) {
170 VULKAN_HPP_DEFAULT_DISPATCHER.init(::glfwGetInstanceProcAddress);
172 vk::ApplicationInfo application_info;
173 application_info.setApplicationVersion(VK_API_VERSION_1_0);
174 application_info.setApiVersion(VK_API_VERSION_1_1);
175 application_info.setEngineVersion(VK_API_VERSION_1_0);
176 application_info.setPEngineName(
"PlaygroundImplVK");
177 application_info.setPApplicationName(
"PlaygroundImplVK");
179 auto caps = std::shared_ptr<CapabilitiesVK>(
180 new CapabilitiesVK(
true));
181 FML_DCHECK(caps->IsValid());
183 std::optional<std::vector<std::string>> enabled_layers =
184 caps->GetEnabledLayers();
185 std::optional<std::vector<std::string>> enabled_extensions =
186 caps->GetEnabledInstanceExtensions();
187 FML_DCHECK(enabled_layers.has_value() && enabled_extensions.has_value());
189 std::vector<const char*> enabled_layers_c;
190 std::vector<const char*> enabled_extensions_c;
192 if (enabled_layers.has_value()) {
193 for (
const auto& layer : enabled_layers.value()) {
194 enabled_layers_c.push_back(layer.c_str());
198 if (enabled_extensions.has_value()) {
199 for (
const auto& ext : enabled_extensions.value()) {
200 enabled_extensions_c.push_back(ext.c_str());
204 vk::InstanceCreateFlags instance_flags = {};
205 instance_flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
206 vk::InstanceCreateInfo instance_info;
207 instance_info.setPEnabledLayerNames(enabled_layers_c);
208 instance_info.setPEnabledExtensionNames(enabled_extensions_c);
209 instance_info.setPApplicationInfo(&application_info);
210 instance_info.setFlags(instance_flags);
211 auto instance_result = vk::createInstanceUnique(instance_info);
212 FML_CHECK(instance_result.result == vk::Result::eSuccess)
213 <<
"Unable to initialize global Vulkan instance";
214 global_instance_ = instance_result.value.release();
218 const std::shared_ptr<Capabilities>& capabilities) {
220 fml::StatusCode::kUnimplemented,
221 "PlaygroundImplVK doesn't support setting the capabilities.");
225 if (::glfwVulkanSupported()) {
229 FML_LOG(ERROR) <<
"Attempting to initialize a Vulkan playground on macOS "
230 "where Vulkan cannot be found. It can be installed via "
231 "MoltenVK and make sure to install it globally so "
232 "dlopen can find it.";
234 FML_LOG(ERROR) <<
"Attempting to initialize a Vulkan playground on a system "
235 "that does not support Vulkan.";
242 PlaygroundImplVK::CreateVKProcAddressResolver()
const {
243 return [](
void* instance,
const char* proc_name) ->
void* {
244 return reinterpret_cast<void*
>(::glfwGetInstanceProcAddress(
245 reinterpret_cast<VkInstance
>(instance), proc_name));
static std::shared_ptr< ContextVK > Create(Settings settings)
std::function< void *(void *instance, const char *proc_name)> VKProcAddressResolver
const PlaygroundSwitches switches_
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities) override
static bool IsVulkanDriverPresent()
PlaygroundImplVK(PlaygroundSwitches switches)
static std::vector< std::shared_ptr< fml::Mapping > > ShaderLibraryMappingsForPlayground()
std::vector< std::shared_ptr< fml::Mapping > > shader_libraries_data
PFN_vkGetInstanceProcAddr proc_address_callback
bool fatal_missing_validations
If validations are requested but cannot be enabled, log a fatal error.
fml::UniqueFD cache_directory
bool enable_vulkan_validation