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 
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"
12 #include "impeller/renderer/vk/compute_shaders_vk.h"
13 
14 namespace impeller::interop {
15 
16 static std::vector<std::shared_ptr<fml::Mapping>>
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)>
35 
36 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL ContextVKGetInstanceProcAddress(
37  VkInstance instance,
38  const char* proc_name) {
40  return sContextVKProcAddressCallback(instance, proc_name);
41  }
42  return nullptr;
43 }
44 
46  if (!settings.IsValid()) {
47  VALIDATION_LOG << "Invalid settings for Vulkan context creation.";
48  return {};
49  }
50  impeller::ContextVK::Settings impeller_settings;
52  impeller_settings.cache_directory = fml::paths::GetCachesDirectory();
53  impeller_settings.enable_validation = true;
56  impeller_settings.flags = impeller::Flags{};
57  auto impeller_context =
58  impeller::ContextVK::Create(std::move(impeller_settings));
60  if (!impeller_context) {
61  VALIDATION_LOG << "Could not create Impeller context.";
62  return {};
63  }
64  return Create(std::move(impeller_context));
65 }
66 
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 
85  : enable_validation(settings.enable_vulkan_validation) {
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 
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)
111  context.GetGraphicsQueue()->GetIndex().family;
112  info.graphics_queue_index = context.GetGraphicsQueue()->GetIndex().index;
113  return true;
114 }
115 
117  return !!instance_proc_address_callback;
118 }
119 
120 } // namespace impeller::interop
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13
static std::shared_ptr< ContextVK > Create(Settings settings)
Definition: context_vk.cc:105
std::shared_ptr< impeller::Context > GetContext() const
Definition: context.cc:20
bool IsValid() const
Definition: context.cc:16
ContextVK(const ContextVK &)=delete
bool GetInfo(ImpellerContextVulkanInfo &info) const
Definition: context_vk.cc:98
static ScopedObject< Context > Create(const Settings &settings)
Definition: context_vk.cc:45
thread_local std::function< PFN_vkVoidFunction(VkInstance instance, const char *proc_name)> sContextVKProcAddressCallback
Definition: context_vk.cc:34
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL ContextVKGetInstanceProcAddress(VkInstance instance, const char *proc_name)
Definition: context_vk.cc:36
static std::vector< std::shared_ptr< fml::Mapping > > CreateShaderLibraryMappings()
Definition: context_mtl.mm:17
Definition: comparable.h:95
uint32_t graphics_queue_index
Definition: impeller.h:643
uint32_t graphics_queue_family_index
Definition: impeller.h:642
void *IMPELLER_NULLABLE vk_instance
Definition: impeller.h:639
void *IMPELLER_NULLABLE vk_logical_device
Definition: impeller.h:641
void *IMPELLER_NULLABLE vk_physical_device
Definition: impeller.h:640
ImpellerVulkanProcAddressCallback IMPELLER_NONNULL proc_address_callback
Definition: impeller.h:634
void *IMPELLER_NULLABLE user_data
Definition: impeller.h:633
std::vector< std::shared_ptr< fml::Mapping > > shader_libraries_data
Definition: context_vk.h:81
PFN_vkGetInstanceProcAddr proc_address_callback
Definition: context_vk.h:80
fml::UniqueFD cache_directory
Definition: context_vk.h:82
Settings(const ImpellerContextVulkanSettings &settings)
Definition: context_vk.cc:84
std::function< PFN_vkVoidFunction(VkInstance instance, const char *proc_name)> instance_proc_address_callback
Definition: context_vk.h:19
#define VALIDATION_LOG
Definition: validation.h:91