| Flutter Impeller |

context_vk.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/toolkit/interop/backend/vulkan/context_vk.h"
6
7 #include "flutter/fml/paths.h"
8 #include "impeller/entity/vk/entity_shaders_vk.h"
9 #include "impeller/entity/vk/framebuffer_blend_shaders_vk.h"
10 #include "impeller/entity/vk/modern_shaders_vk.h"
11 #include "impeller/renderer/backend/vulkan/context_vk.h"
12 #include "impeller/renderer/vk/compute_shaders_vk.h"
13
14 namespace impeller::interop {
15
16 static std::vector<std::shared_ptr<fml::Mapping>>
17 CreateShaderLibraryMappings() {
18 return {
19 std::make_shared<fml::NonOwnedMapping>(impeller_entity_shaders_vk_data,
20 impeller_entity_shaders_vk_length),
21 std::make_shared<fml::NonOwnedMapping>(impeller_modern_shaders_vk_data,
22 impeller_modern_shaders_vk_length),
23 std::make_shared<fml::NonOwnedMapping>(
24 impeller_framebuffer_blend_shaders_vk_data,
25 impeller_framebuffer_blend_shaders_vk_length),
26 std::make_shared<fml::NonOwnedMapping>(
27 impeller_compute_shaders_vk_data, impeller_compute_shaders_vk_length),
28 };
29 }
30 // This bit is complicated by the fact that impeller::ContextVK::Settings takes
31 // a raw function pointer to the callback.
32 thread_local std::function<PFN_vkVoidFunction(VkInstance instance,
33 const char* proc_name)>
34 sContextVKProcAddressCallback;
35
36 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL ContextVKGetInstanceProcAddress(
37 VkInstance instance,
38 const char* proc_name) {
39 if (sContextVKProcAddressCallback) {
40 return sContextVKProcAddressCallback(instance, proc_name);
41 }
42 return nullptr;
43 }
44
45 ScopedObject<Context> ContextVK::Create(const Settings& settings) {
46 if (!settings.IsValid()) {
47 VALIDATION_LOG << "Invalid settings for Vulkan context creation.";
48 return {};
49 }
50 impeller::ContextVK::Settings impeller_settings;
51 impeller_settings.shader_libraries_data = CreateShaderLibraryMappings();
52 impeller_settings.cache_directory = fml::paths::GetCachesDirectory();
53 impeller_settings.enable_validation = true;
54 sContextVKProcAddressCallback = settings.instance_proc_address_callback;
55 impeller_settings.proc_address_callback = ContextVKGetInstanceProcAddress;
56 impeller_settings.flags = impeller::Flags{};
57 auto impeller_context =
58 impeller::ContextVK::Create(std::move(impeller_settings));
59 sContextVKProcAddressCallback = nullptr;
60 if (!impeller_context) {
61 VALIDATION_LOG << "Could not create Impeller context.";
62 return {};
63 }
64 return Create(std::move(impeller_context));
65 }
66
67 ScopedObject<Context> ContextVK::Create(
68 std::shared_ptr<impeller::Context> impeller_context) {
69 // Can't call Create because of private constructor. Adopt the raw pointer
70 // instead.
71 auto context = Adopt<Context>(new ContextVK(std::move(impeller_context)));
72 if (!context->IsValid()) {
73 VALIDATION_LOG << " Could not create valid context.";
74 return {};
75 }
76 return context;
77 }
78
79 ContextVK::ContextVK(std::shared_ptr<impeller::Context> context)
80 : Context(std::move(context)) {}
81
82 ContextVK::~ContextVK() = default;
83
84 ContextVK::Settings::Settings(const ImpellerContextVulkanSettings& settings)
85 : enable_validation(settings.enable_vulkan_validation) {
86 instance_proc_address_callback =
87 [&settings](VkInstance instance,
88 const char* proc_name) -> PFN_vkVoidFunction {
89 if (settings.proc_address_callback) {
90 return reinterpret_cast<PFN_vkVoidFunction>(
91 settings.proc_address_callback(instance, proc_name,
92 settings.user_data));
93 }
94 return nullptr;
95 };
96 }
97
98 bool ContextVK::GetInfo(ImpellerContextVulkanInfo& info) const {
99 if (!IsValid()) {
100 return false;
101 }
102 const auto& context = impeller::ContextVK::Cast(*GetContext());
103 // NOLINTBEGIN(google-readability-casting)
104 info.vk_instance = reinterpret_cast<void*>(VkInstance(context.GetInstance()));
105 info.vk_physical_device =
106 reinterpret_cast<void*>(VkPhysicalDevice(context.GetPhysicalDevice()));
107 info.vk_logical_device =
108 reinterpret_cast<void*>(VkDevice(context.GetDevice()));
109 // NOLINTEND(google-readability-casting)
110 info.graphics_queue_family_index =
111 context.GetGraphicsQueue()->GetIndex().family;
112 info.graphics_queue_index = context.GetGraphicsQueue()->GetIndex().index;
113 return true;
114 }
115
116 bool ContextVK::Settings::IsValid() const {
117 return !!instance_proc_address_callback;
118 }
119
120 } // namespace impeller::interop
impeller::BackendCast< ContextVK, Context >::Cast
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13
impeller::ContextVK::Create
static std::shared_ptr< ContextVK > Create(Settings settings)
Definition: context_vk.cc:105
impeller::interop::Context
Definition: context.h:16
impeller::interop::Context::GetContext
std::shared_ptr< impeller::Context > GetContext() const
Definition: context.cc:20
impeller::interop::Context::IsValid
bool IsValid() const
Definition: context.cc:16
impeller::interop::ContextVK::ContextVK
ContextVK(const ContextVK &)=delete
impeller::interop::ContextVK::GetInfo
bool GetInfo(ImpellerContextVulkanInfo &info) const
Definition: context_vk.cc:98
impeller::interop::ContextVK::~ContextVK
~ContextVK() override
impeller::interop::ContextVK::Create
static ScopedObject< Context > Create(const Settings &settings)
Definition: context_vk.cc:45
impeller::interop::ScopedObject
Definition: object.h:67
impeller::interop
Definition: context_gles.cc:12
impeller::interop::sContextVKProcAddressCallback
thread_local std::function< PFN_vkVoidFunction(VkInstance instance, const char *proc_name)> sContextVKProcAddressCallback
Definition: context_vk.cc:34
impeller::interop::ContextVKGetInstanceProcAddress
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL ContextVKGetInstanceProcAddress(VkInstance instance, const char *proc_name)
Definition: context_vk.cc:36
impeller::interop::CreateShaderLibraryMappings
static std::vector< std::shared_ptr< fml::Mapping > > CreateShaderLibraryMappings()
Definition: context_mtl.mm:17
std
Definition: comparable.h:95
context_vk.h
ImpellerContextVulkanInfo
Definition: impeller.h:614
ImpellerContextVulkanInfo::graphics_queue_index
uint32_t graphics_queue_index
Definition: impeller.h:619
ImpellerContextVulkanInfo::graphics_queue_family_index
uint32_t graphics_queue_family_index
Definition: impeller.h:618
ImpellerContextVulkanInfo::vk_instance
void *IMPELLER_NULLABLE vk_instance
Definition: impeller.h:615
ImpellerContextVulkanInfo::vk_logical_device
void *IMPELLER_NULLABLE vk_logical_device
Definition: impeller.h:617
ImpellerContextVulkanInfo::vk_physical_device
void *IMPELLER_NULLABLE vk_physical_device
Definition: impeller.h:616
ImpellerContextVulkanSettings
Definition: impeller.h:608
ImpellerContextVulkanSettings::proc_address_callback
ImpellerVulkanProcAddressCallback IMPELLER_NONNULL proc_address_callback
Definition: impeller.h:610
ImpellerContextVulkanSettings::user_data
void *IMPELLER_NULLABLE user_data
Definition: impeller.h:609
impeller::ContextVK::Settings
Definition: context_vk.h:79
impeller::ContextVK::Settings::flags
Flags flags
Definition: context_vk.h:88
impeller::ContextVK::Settings::shader_libraries_data
std::vector< std::shared_ptr< fml::Mapping > > shader_libraries_data
Definition: context_vk.h:81
impeller::ContextVK::Settings::proc_address_callback
PFN_vkGetInstanceProcAddr proc_address_callback
Definition: context_vk.h:80
impeller::ContextVK::Settings::enable_validation
bool enable_validation
Definition: context_vk.h:83
impeller::ContextVK::Settings::cache_directory
fml::UniqueFD cache_directory
Definition: context_vk.h:82
impeller::Flags
Definition: flags.h:9
impeller::interop::ContextVK::Settings
Definition: context_vk.h:16
impeller::interop::ContextVK::Settings::Settings
Settings(const ImpellerContextVulkanSettings &settings)
Definition: context_vk.cc:84
impeller::interop::ContextVK::Settings::instance_proc_address_callback
std::function< PFN_vkVoidFunction(VkInstance instance, const char *proc_name)> instance_proc_address_callback
Definition: context_vk.h:19
impeller::interop::ContextVK::Settings::IsValid
bool IsValid() const
Definition: context_vk.cc:116
context_vk.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91