| 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
37 ContextVKGetInstanceProcAddress(VkInstance instance, const char* proc_name) {
38 if (sContextVKProcAddressCallback) {
39 return sContextVKProcAddressCallback(instance, proc_name);
40 }
41 return nullptr;
42 }
43
44 ScopedObject<Context> ContextVK::Create(const Settings& settings) {
45 if (!settings.IsValid()) {
46 VALIDATION_LOG << "Invalid settings for Vulkan context creation.";
47 return {};
48 }
49 impeller::ContextVK::Settings impeller_settings;
50 impeller_settings.shader_libraries_data = CreateShaderLibraryMappings();
51 impeller_settings.cache_directory = fml::paths::GetCachesDirectory();
52 impeller_settings.enable_validation = true;
53 sContextVKProcAddressCallback = settings.instance_proc_address_callback;
54 impeller_settings.proc_address_callback = ContextVKGetInstanceProcAddress;
55 impeller_settings.flags = impeller::Flags{};
56 auto impeller_context =
57 impeller::ContextVK::Create(std::move(impeller_settings));
58 sContextVKProcAddressCallback = nullptr;
59 if (!impeller_context) {
60 VALIDATION_LOG << "Could not create Impeller context.";
61 return {};
62 }
63 return Create(std::move(impeller_context));
64 }
65
66 ScopedObject<Context> ContextVK::Create(
67 std::shared_ptr<impeller::Context> impeller_context) {
68 // Can't call Create because of private constructor. Adopt the raw pointer
69 // instead.
70 auto context = Adopt<Context>(new ContextVK(std::move(impeller_context)));
71 if (!context->IsValid()) {
72 VALIDATION_LOG << " Could not create valid context.";
73 return {};
74 }
75 return context;
76 }
77
78 ContextVK::ContextVK(std::shared_ptr<impeller::Context> context)
79 : Context(std::move(context)) {}
80
81 ContextVK::~ContextVK() = default;
82
83 ContextVK::Settings::Settings(const ImpellerContextVulkanSettings& settings)
84 : enable_validation(settings.enable_vulkan_validation) {
85 instance_proc_address_callback =
86 [&settings](VkInstance instance,
87 const char* proc_name) -> PFN_vkVoidFunction {
88 if (settings.proc_address_callback) {
89 return reinterpret_cast<PFN_vkVoidFunction>(
90 settings.proc_address_callback(instance, proc_name,
91 settings.user_data));
92 }
93 return nullptr;
94 };
95 }
96
97 bool ContextVK::GetInfo(ImpellerContextVulkanInfo& info) const {
98 if (!IsValid()) {
99 return false;
100 }
101 const auto& context = impeller::ContextVK::Cast(*GetContext());
102 // NOLINTBEGIN(google-readability-casting)
103 info.vk_instance = reinterpret_cast<void*>(VkInstance(context.GetInstance()));
104 info.vk_physical_device =
105 reinterpret_cast<void*>(VkPhysicalDevice(context.GetPhysicalDevice()));
106 info.vk_logical_device =
107 reinterpret_cast<void*>(VkDevice(context.GetDevice()));
108 // NOLINTEND(google-readability-casting)
109 info.graphics_queue_family_index =
110 context.GetGraphicsQueue()->GetIndex().family;
111 info.graphics_queue_index = context.GetGraphicsQueue()->GetIndex().index;
112 return true;
113 }
114
115 bool ContextVK::Settings::IsValid() const {
116 return !!instance_proc_address_callback;
117 }
118
119 } // 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:97
impeller::interop::ContextVK::~ContextVK
~ContextVK() override
impeller::interop::ContextVK::Create
static ScopedObject< Context > Create(const Settings &settings)
Definition: context_vk.cc:44
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:37
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:638
ImpellerContextVulkanInfo::graphics_queue_index
uint32_t graphics_queue_index
Definition: impeller.h:643
ImpellerContextVulkanInfo::graphics_queue_family_index
uint32_t graphics_queue_family_index
Definition: impeller.h:642
ImpellerContextVulkanInfo::vk_instance
void *IMPELLER_NULLABLE vk_instance
Definition: impeller.h:639
ImpellerContextVulkanInfo::vk_logical_device
void *IMPELLER_NULLABLE vk_logical_device
Definition: impeller.h:641
ImpellerContextVulkanInfo::vk_physical_device
void *IMPELLER_NULLABLE vk_physical_device
Definition: impeller.h:640
ImpellerContextVulkanSettings
Definition: impeller.h:632
ImpellerContextVulkanSettings::proc_address_callback
ImpellerVulkanProcAddressCallback IMPELLER_NONNULL proc_address_callback
Definition: impeller.h:634
ImpellerContextVulkanSettings::user_data
void *IMPELLER_NULLABLE user_data
Definition: impeller.h:633
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:83
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:115
context_vk.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91