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/playground/imgui/vk/imgui_shaders_vk.h"
25 #include "impeller/renderer/vk/compute_shaders_vk.h"
26 #include "impeller/scene/shaders/vk/scene_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>(impeller_imgui_shaders_vk_data,
44 impeller_imgui_shaders_vk_length),
45 std::make_shared<fml::NonOwnedMapping>(impeller_scene_shaders_vk_data,
46 impeller_scene_shaders_vk_length),
47 std::make_shared<fml::NonOwnedMapping>(
48 impeller_compute_shaders_vk_data, impeller_compute_shaders_vk_length),
52 vk::UniqueInstance PlaygroundImplVK::global_instance_;
54 void PlaygroundImplVK::DestroyWindowHandle(WindowHandle handle) {
58 ::glfwDestroyWindow(
reinterpret_cast<GLFWwindow*
>(handle));
62 :
PlaygroundImpl(switches), handle_(nullptr, &DestroyWindowHandle) {
65 InitGlobalVulkanInstance();
67 ::glfwDefaultWindowHints();
68 ::glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
69 ::glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
71 auto window = ::glfwCreateWindow(1, 1,
"Test",
nullptr,
nullptr);
79 ::glfwGetWindowSize(window, &width, &height);
80 size_ =
ISize{width, height};
82 handle_.reset(window);
86 reinterpret_cast<PFN_vkGetInstanceProcAddr
>(
87 &::glfwGetInstanceProcAddress);
93 if (!context_vk || !context_vk->IsValid()) {
94 VALIDATION_LOG <<
"Could not create Vulkan context in the playground.";
98 VkSurfaceKHR vk_surface;
99 auto res = vk::Result{::glfwCreateWindowSurface(
100 context_vk->GetInstance(),
105 if (res != vk::Result::eSuccess) {
107 << vk::to_string(res);
111 vk::UniqueSurfaceKHR surface{vk_surface, context_vk->GetInstance()};
112 auto context = context_vk->CreateSurfaceContext();
113 if (!context->SetWindowSurface(std::move(surface), size_)) {
118 context_ = std::move(context);
124 std::shared_ptr<Context> PlaygroundImplVK::GetContext()
const {
130 return handle_.get();
134 std::unique_ptr<Surface> PlaygroundImplVK::AcquireSurfaceFrame(
135 std::shared_ptr<Context> context) {
136 SurfaceContextVK* surface_context_vk =
137 reinterpret_cast<SurfaceContextVK*
>(context_.get());
141 ::glfwGetFramebufferSize(
reinterpret_cast<GLFWwindow*
>(handle_.get()), &width,
143 size_ =
ISize{width, height};
144 surface_context_vk->UpdateSurfaceSize(
ISize{width, height});
146 return surface_context_vk->AcquireNextSurface();
157 void PlaygroundImplVK::InitGlobalVulkanInstance() {
158 if (global_instance_) {
162 VULKAN_HPP_DEFAULT_DISPATCHER.init(::glfwGetInstanceProcAddress);
164 vk::ApplicationInfo application_info;
165 application_info.setApplicationVersion(VK_API_VERSION_1_0);
166 application_info.setApiVersion(VK_API_VERSION_1_1);
167 application_info.setEngineVersion(VK_API_VERSION_1_0);
168 application_info.setPEngineName(
"PlaygroundImplVK");
169 application_info.setPApplicationName(
"PlaygroundImplVK");
171 auto caps = std::shared_ptr<CapabilitiesVK>(
172 new CapabilitiesVK(
true));
173 FML_DCHECK(caps->IsValid());
175 std::optional<std::vector<std::string>> enabled_layers =
176 caps->GetEnabledLayers();
177 std::optional<std::vector<std::string>> enabled_extensions =
178 caps->GetEnabledInstanceExtensions();
179 FML_DCHECK(enabled_layers.has_value() && enabled_extensions.has_value());
181 std::vector<const char*> enabled_layers_c;
182 std::vector<const char*> enabled_extensions_c;
184 if (enabled_layers.has_value()) {
185 for (
const auto& layer : enabled_layers.value()) {
186 enabled_layers_c.push_back(layer.c_str());
190 if (enabled_extensions.has_value()) {
191 for (
const auto& ext : enabled_extensions.value()) {
192 enabled_extensions_c.push_back(ext.c_str());
196 vk::InstanceCreateFlags instance_flags = {};
197 instance_flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
198 vk::InstanceCreateInfo instance_info;
199 instance_info.setPEnabledLayerNames(enabled_layers_c);
200 instance_info.setPEnabledExtensionNames(enabled_extensions_c);
201 instance_info.setPApplicationInfo(&application_info);
202 instance_info.setFlags(instance_flags);
203 auto instance_result = vk::createInstanceUnique(instance_info);
204 FML_CHECK(instance_result.result == vk::Result::eSuccess)
205 <<
"Unable to initialize global Vulkan instance";
206 global_instance_ = std::move(instance_result.value);
210 const std::shared_ptr<Capabilities>& capabilities) {
212 fml::StatusCode::kUnimplemented,
213 "PlaygroundImplVK doesn't support setting the capabilities.");
217 if (::glfwVulkanSupported()) {
221 FML_LOG(ERROR) <<
"Attempting to initialize a Vulkan playground on macOS "
222 "where Vulkan cannot be found. It can be installed via "
223 "MoltenVK and make sure to install it globally so "
224 "dlopen can find it.";
225 #else // TARGET_OS_MAC
226 FML_LOG(ERROR) <<
"Attempting to initialize a Vulkan playground on a system "
227 "that does not support Vulkan.";
228 #endif // TARGET_OS_MAC