Flutter Engine
vulkan::VulkanApplication Class Reference

#include <vulkan_application.h>

Public Member Functions

 VulkanApplication (VulkanProcTable &vk, const std::string &application_name, std::vector< std::string > enabled_extensions, uint32_t application_version=VK_MAKE_VERSION(1, 0, 0), uint32_t api_version=VK_MAKE_VERSION(1, 0, 0), bool enable_validation_layers=false)
 
 ~VulkanApplication ()
 
bool IsValid () const
 
uint32_t GetAPIVersion () const
 
const VulkanHandle< VkInstance > & GetInstance () const
 
void ReleaseInstanceOwnership ()
 
std::unique_ptr< VulkanDeviceAcquireFirstCompatibleLogicalDevice () const
 

Detailed Description

Applications using Vulkan acquire a VulkanApplication that attempts to create a VkInstance (with debug reporting optionally enabled).

Definition at line 26 of file vulkan_application.h.

Constructor & Destructor Documentation

◆ VulkanApplication()

vulkan::VulkanApplication::VulkanApplication ( VulkanProcTable vk,
const std::string &  application_name,
std::vector< std::string >  enabled_extensions,
uint32_t  application_version = VK_MAKE_VERSION(1, 0, 0),
uint32_t  api_version = VK_MAKE_VERSION(1, 0, 0),
bool  enable_validation_layers = false 
)

Definition at line 16 of file vulkan_application.cc.

References vulkan::VulkanDebugReport::DebugExtensionName(), FML_DLOG, vulkan::InstanceLayersToEnable(), vulkan::VulkanProcTable::SetupInstanceProcAddresses(), VK_CALL_LOG_ERROR, and ~VulkanApplication().

23  : vk(p_vk),
24  api_version_(api_version),
25  valid_(false),
26  enable_validation_layers_(enable_validation_layers) {
27  // Check if we want to enable debugging.
28  std::vector<VkExtensionProperties> supported_extensions =
29  GetSupportedInstanceExtensions(vk);
30  bool enable_instance_debugging =
31  enable_validation_layers_ &&
32  ExtensionSupported(supported_extensions,
34 
35  // Configure extensions.
36 
37  if (enable_instance_debugging) {
38  enabled_extensions.emplace_back(VulkanDebugReport::DebugExtensionName());
39  }
40 #if OS_FUCHSIA
41  if (ExtensionSupported(supported_extensions,
42  VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME)) {
43  // VK_KHR_get_physical_device_properties2 is a dependency of the memory
44  // capabilities extension, so the validation layers require that it be
45  // enabled.
46  enabled_extensions.emplace_back(
47  VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
48  enabled_extensions.emplace_back(
49  VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME);
50  }
51 #endif
52 
53  const char* extensions[enabled_extensions.size()];
54 
55  for (size_t i = 0; i < enabled_extensions.size(); i++) {
56  extensions[i] = enabled_extensions[i].c_str();
57  }
58 
59  // Configure layers.
60 
61  const std::vector<std::string> enabled_layers =
62  InstanceLayersToEnable(vk, enable_validation_layers_);
63 
64  const char* layers[enabled_layers.size()];
65 
66  for (size_t i = 0; i < enabled_layers.size(); i++) {
67  layers[i] = enabled_layers[i].c_str();
68  }
69 
70  // Configure init structs.
71 
72  const VkApplicationInfo info = {
73  .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
74  .pNext = nullptr,
75  .pApplicationName = application_name.c_str(),
76  .applicationVersion = application_version,
77  .pEngineName = "FlutterEngine",
78  .engineVersion = VK_MAKE_VERSION(1, 0, 0),
79  .apiVersion = api_version_,
80  };
81 
82  const VkInstanceCreateInfo create_info = {
83  .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
84  .pNext = nullptr,
85  .flags = 0,
86  .pApplicationInfo = &info,
87  .enabledLayerCount = static_cast<uint32_t>(enabled_layers.size()),
88  .ppEnabledLayerNames = layers,
89  .enabledExtensionCount = static_cast<uint32_t>(enabled_extensions.size()),
90  .ppEnabledExtensionNames = extensions,
91  };
92 
93  // Perform initialization.
94 
95  VkInstance instance = VK_NULL_HANDLE;
96 
97  if (VK_CALL_LOG_ERROR(vk.CreateInstance(&create_info, nullptr, &instance)) !=
98  VK_SUCCESS) {
99  FML_DLOG(INFO) << "Could not create application instance.";
100  return;
101  }
102 
103  // Now that we have an instance, setup instance proc table entries.
104  if (!vk.SetupInstanceProcAddresses(instance)) {
105  FML_DLOG(INFO) << "Could not setup instance proc addresses.";
106  return;
107  }
108 
109  instance_ = {instance, [this](VkInstance i) {
110  FML_DLOG(INFO) << "Destroying Vulkan instance";
111  vk.DestroyInstance(i, nullptr);
112  }};
113 
114  if (enable_instance_debugging) {
115  auto debug_report = std::make_unique<VulkanDebugReport>(vk, instance_);
116  if (!debug_report->IsValid()) {
117  FML_DLOG(INFO) << "Vulkan debugging was enabled but could not be setup "
118  "for this instance.";
119  } else {
120  debug_report_ = std::move(debug_report);
121  FML_DLOG(INFO) << "Debug reporting is enabled.";
122  }
123  }
124 
125  valid_ = true;
126 }
bool SetupInstanceProcAddresses(const VulkanHandle< VkInstance > &instance)
std::vector< std::string > InstanceLayersToEnable(const VulkanProcTable &vk, bool enable_validation_layers)
#define VK_CALL_LOG_ERROR(expression)
static std::string DebugExtensionName()
#define FML_DLOG(severity)
Definition: logging.h:85

◆ ~VulkanApplication()

vulkan::VulkanApplication::~VulkanApplication ( )
default

Referenced by VulkanApplication().

Member Function Documentation

◆ AcquireFirstCompatibleLogicalDevice()

std::unique_ptr< VulkanDevice > vulkan::VulkanApplication::AcquireFirstCompatibleLogicalDevice ( ) const

Definition at line 178 of file vulkan_application.cc.

References FML_DLOG, and VK_CALL_LOG_ERROR.

178  {
179  for (auto device_handle : GetPhysicalDevices()) {
180  auto logical_device = std::make_unique<VulkanDevice>(
181  vk, device_handle, enable_validation_layers_);
182  if (logical_device->IsValid()) {
183  return logical_device;
184  }
185  }
186  FML_DLOG(INFO) << "Could not acquire compatible logical device.";
187  return nullptr;
188 }
#define FML_DLOG(severity)
Definition: logging.h:85

◆ GetAPIVersion()

uint32_t vulkan::VulkanApplication::GetAPIVersion ( ) const

Definition at line 134 of file vulkan_application.cc.

134  {
135  return api_version_;
136 }

◆ GetInstance()

const VulkanHandle< VkInstance > & vulkan::VulkanApplication::GetInstance ( ) const

Definition at line 138 of file vulkan_application.cc.

Referenced by vulkan::VulkanSurface::VulkanSurface().

138  {
139  return instance_;
140 }

◆ IsValid()

bool vulkan::VulkanApplication::IsValid ( ) const

Definition at line 130 of file vulkan_application.cc.

Referenced by ReleaseInstanceOwnership().

130  {
131  return valid_;
132 }

◆ ReleaseInstanceOwnership()

void vulkan::VulkanApplication::ReleaseInstanceOwnership ( )

Definition at line 142 of file vulkan_application.cc.

References FML_DLOG, IsValid(), vulkan::VulkanHandle< T >::ReleaseOwnership(), and VK_CALL_LOG_ERROR.

142  {
143  instance_.ReleaseOwnership();
144 }

The documentation for this class was generated from the following files: