Skip to content

Commit 42b87ae

Browse files
authored
Move debug messenger/callback from vkb::core::Instance to VulkanSample; cleanup of vkb::core::Instance (#1487)
* Move debug messenger/callback from vkb::core::Instance to VulkanSample; cleanup of vkb::core::Instance * Fix create_surface on direct_window * Resolve merge conflicts.
1 parent ee135c6 commit 42b87ae

19 files changed

Lines changed: 205 additions & 237 deletions

File tree

framework/core/hpp_debug.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,6 @@ inline VKAPI_ATTR vk::Bool32 VKAPI_CALL debug_utils_messenger_callback(vk::Debug
166166
return false;
167167
}
168168

169-
inline vk::DebugUtilsMessengerCreateInfoEXT getDefaultDebugUtilsMessengerCreateInfoEXT()
170-
{
171-
return vk::DebugUtilsMessengerCreateInfoEXT{.messageSeverity = vk::DebugUtilsMessageSeverityFlagBitsEXT::eError | vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning,
172-
.messageType = vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation | vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance,
173-
.pfnUserCallback = debug_utils_messenger_callback};
174-
}
175-
176169
inline VKAPI_ATTR vk::Bool32 VKAPI_CALL debug_callback(vk::DebugReportFlagsEXT flags,
177170
vk::DebugReportObjectTypeEXT /*type*/,
178171
uint64_t /*object*/,
@@ -200,13 +193,6 @@ inline VKAPI_ATTR vk::Bool32 VKAPI_CALL debug_callback(vk::DebugReportFlagsEXT f
200193
}
201194
return false;
202195
}
203-
204-
inline vk::DebugReportCallbackCreateInfoEXT getDefaultDebugReportCallbackCreateInfoEXT()
205-
{
206-
return vk::DebugReportCallbackCreateInfoEXT{.flags = vk::DebugReportFlagBitsEXT::eError | vk::DebugReportFlagBitsEXT::eWarning | vk::DebugReportFlagBitsEXT::ePerformanceWarning,
207-
.pfnCallback = debug_callback};
208-
}
209-
210196
#endif
211197
} // namespace core
212198
} // namespace vkb

framework/core/instance.h

Lines changed: 44 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,45 @@
1919
#pragma once
2020

2121
#include "common/helpers.h"
22-
#include "common/vk_common.h"
23-
#include "core/hpp_debug.h"
24-
#include <ranges>
25-
#include <unordered_set>
2622
#include <vulkan/vulkan.hpp>
2723

28-
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
29-
# define USE_VALIDATION_LAYERS
30-
#endif
31-
32-
#if defined(USE_VALIDATION_LAYERS) && \
33-
(defined(VKB_VALIDATION_LAYERS_GPU_ASSISTED) || defined(VKB_VALIDATION_LAYERS_BEST_PRACTICES) || defined(VKB_VALIDATION_LAYERS_SYNCHRONIZATION))
34-
# define USE_VALIDATION_LAYER_FEATURES
35-
#endif
36-
3724
namespace vkb
3825
{
3926
namespace core
4027
{
28+
namespace
29+
{
4130
template <vkb::BindingType bindingType>
42-
class PhysicalDevice;
43-
using PhysicalDeviceC = PhysicalDevice<vkb::BindingType::C>;
44-
using PhysicalDeviceCpp = PhysicalDevice<vkb::BindingType::Cpp>;
31+
typename std::conditional<bindingType == vkb::BindingType::Cpp, vk::InstanceCreateFlags, VkInstanceCreateFlags>::type get_default_create_flags(std::vector<std::string> const &)
32+
{
33+
if constexpr (bindingType == vkb::BindingType::Cpp)
34+
{
35+
return vk::InstanceCreateFlags{};
36+
}
37+
else
38+
{
39+
return 0;
40+
}
41+
}
42+
43+
void const *get_default_pNext(std::vector<std::string> const &, std::vector<std::string> const &)
44+
{
45+
return nullptr;
46+
}
47+
} // namespace
4548

4649
/**
4750
* @brief A wrapper class for InstanceType
4851
*
49-
* This class is responsible for initializing the dispatcher, enumerating over all available extensions and validation layers
50-
* enabling them if they exist, setting up debug messaging and querying all the physical devices existing on the machine.
52+
* This class is responsible for checking the API version, checking for required and optional extensions and layers, creating the Vulkan
53+
* instance and initializing the default dispatcher.
5154
*/
5255
template <vkb::BindingType bindingType>
5356
class Instance
5457
{
5558
public:
5659
using InstanceCreateFlagsType = typename std::conditional<bindingType == vkb::BindingType::Cpp, vk::InstanceCreateFlags, VkInstanceCreateFlags>::type;
5760
using InstanceType = typename std::conditional<bindingType == vkb::BindingType::Cpp, vk::Instance, VkInstance>::type;
58-
using LayerSettingType = typename std::conditional<bindingType == vkb::BindingType::Cpp, vk::LayerSettingEXT, VkLayerSettingEXT>::type;
59-
using SurfaceType = typename std::conditional<bindingType == vkb::BindingType::Cpp, vk::SurfaceKHR, VkSurfaceKHR>::type;
6061

6162
public:
6263
/**
@@ -70,32 +71,15 @@ class Instance
7071
* @throws runtime_error if a required layer or extension is not available
7172
*/
7273
Instance(
73-
std::string const &application_name,
74-
uint32_t api_version = VK_API_VERSION_1_1,
75-
std::unordered_map<std::string, vkb::RequestMode> const &requested_layers = {},
76-
std::unordered_map<std::string, vkb::RequestMode> const &requested_extensions = {},
77-
std::function<void *(std::vector<std::string> const &, std::vector<std::string> const &)> const &get_pNext =
78-
[](std::vector<std::string> const &, std::vector<std::string> const &) { return nullptr; },
79-
std::function<InstanceCreateFlagsType(std::vector<std::string> const &)> const &get_create_flags =
80-
[](std::vector<std::string> const &) {
81-
if constexpr (bindingType == vkb::BindingType::Cpp)
82-
{
83-
return vk::InstanceCreateFlags{};
84-
}
85-
else
86-
{
87-
return 0;
88-
}
89-
});
74+
std::string const &application_name,
75+
uint32_t api_version = VK_API_VERSION_1_1,
76+
std::unordered_map<std::string, vkb::RequestMode> const &requested_layers = {},
77+
std::unordered_map<std::string, vkb::RequestMode> const &requested_extensions = {},
78+
std::function<void const *(std::vector<std::string> const &, std::vector<std::string> const &)> const &get_pNext = get_default_pNext,
79+
std::function<InstanceCreateFlagsType(std::vector<std::string> const &)> const &get_create_flags = get_default_create_flags);
9080

91-
/**
92-
* @brief Queries the GPUs of a InstanceType that is already created
93-
* @param instance A valid InstanceType
94-
* @param externally_enabled_extensions List of extensions that have been enabled, used for following checks e.g. during device creation
95-
* @param needsToInitializeDispatcher If the sample uses the C-bindings and some "non-standard" initialization, the dispatcher needs to be initialized
96-
*/
97-
Instance(vk::Instance instance, const std::vector<const char *> &externally_enabled_extensions = {}, bool needsToInitializeDispatcher = false);
98-
Instance(VkInstance instance, const std::vector<const char *> &externally_enabled_extensions = {});
81+
Instance(vk::Instance instance, std::vector<char const *> const &externally_enabled_extensions = {}, bool needsToInitializeDispatcher = false);
82+
Instance(VkInstance instance, std::vector<char const *> const &externally_enabled_extensions = {});
9983

10084
Instance(Instance const &) = delete;
10185
Instance(Instance &&) = delete;
@@ -105,37 +89,24 @@ class Instance
10589
Instance &operator=(Instance const &) = delete;
10690
Instance &operator=(Instance &&) = delete;
10791

108-
const std::vector<std::string> &get_extensions();
92+
std::vector<std::string> const &get_enabled_extensions();
10993

11094
InstanceType get_handle() const;
11195

11296
/**
11397
* @brief Checks if the given extension is enabled in the InstanceType
11498
* @param extension An extension to check
11599
*/
116-
bool is_enabled(char const *extension) const;
100+
bool is_extension_enabled(char const *extension) const;
117101

118102
private:
119103
std::vector<std::string> enabled_extensions; // The enabled extensions
120104
vk::Instance handle; // The Vulkan instance
121-
122-
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
123-
vk::DebugReportCallbackEXT debug_report_callback; // The debug report callback
124-
vk::DebugUtilsMessengerEXT debug_utils_messenger; // Debug utils messenger callback for VK_EXT_Debug_Utils
125-
#endif
126105
};
127106

128107
using InstanceC = Instance<vkb::BindingType::C>;
129108
using InstanceCpp = Instance<vkb::BindingType::Cpp>;
130-
} // namespace core
131-
} // namespace vkb
132-
133-
#include "core/physical_device.h"
134109

135-
namespace vkb
136-
{
137-
namespace core
138-
{
139110
namespace
140111
{
141112
inline bool enable_extension(std::string const &requested_extension,
@@ -184,39 +155,15 @@ inline bool
184155

185156
return is_available;
186157
}
187-
188-
inline bool validate_layers(std::vector<char const *> const &required, std::vector<vk::LayerProperties> const &available)
189-
{
190-
for (auto layer : required)
191-
{
192-
bool found = false;
193-
for (auto &available_layer : available)
194-
{
195-
if (strcmp(available_layer.layerName, layer) == 0)
196-
{
197-
found = true;
198-
break;
199-
}
200-
}
201-
202-
if (!found)
203-
{
204-
LOGE("Validation Layer {} not found", layer);
205-
return false;
206-
}
207-
}
208-
209-
return true;
210-
}
211158
} // namespace
212159

213160
template <vkb::BindingType bindingType>
214-
inline Instance<bindingType>::Instance(std::string const &application_name,
215-
uint32_t api_version,
216-
std::unordered_map<std::string, vkb::RequestMode> const &requested_layers,
217-
std::unordered_map<std::string, vkb::RequestMode> const &requested_extensions,
218-
std::function<void *(std::vector<std::string> const &, std::vector<std::string> const &)> const &get_pNext,
219-
std::function<InstanceCreateFlagsType(std::vector<std::string> const &)> const &get_create_flags)
161+
inline Instance<bindingType>::Instance(std::string const &application_name,
162+
uint32_t api_version,
163+
std::unordered_map<std::string, vkb::RequestMode> const &requested_layers,
164+
std::unordered_map<std::string, vkb::RequestMode> const &requested_extensions,
165+
std::function<void const *(std::vector<std::string> const &, std::vector<std::string> const &)> const &get_pNext,
166+
std::function<InstanceCreateFlagsType(std::vector<std::string> const &)> const &get_create_flags)
220167
{
221168
// check API version
222169
LOGI("Requesting Vulkan API version {}.{}", VK_VERSION_MAJOR(api_version), VK_VERSION_MINOR(api_version));
@@ -266,7 +213,7 @@ inline Instance<bindingType>::Instance(std::string const
266213

267214
if (contains(enabled_layers, "VK_LAYER_KHRONOS_validation"))
268215
{
269-
const std::string validation_layer_name = "VK_LAYER_KHRONOS_validation";
216+
std::string const validation_layer_name = "VK_LAYER_KHRONOS_validation";
270217
std::vector<vk::ExtensionProperties> available_layer_instance_extensions = vk::enumerateInstanceExtensionProperties(validation_layer_name);
271218
available_extensions.insert(available_extensions.end(),
272219
available_layer_instance_extensions.begin(),
@@ -288,7 +235,7 @@ inline Instance<bindingType>::Instance(std::string const
288235
}
289236
}
290237
}
291-
std::vector<const char *> enabled_extensions_cstr;
238+
std::vector<char const *> enabled_extensions_cstr;
292239
for (auto &extension : enabled_extensions)
293240
{
294241
enabled_extensions_cstr.push_back(extension.c_str());
@@ -312,21 +259,10 @@ inline Instance<bindingType>::Instance(std::string const
312259

313260
// Need to load volk for all the not-yet Vulkan-Hpp calls
314261
volkLoadInstance(handle);
315-
316-
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
317-
if (contains(enabled_extensions, VK_EXT_DEBUG_UTILS_EXTENSION_NAME))
318-
{
319-
debug_utils_messenger = handle.createDebugUtilsMessengerEXT(vkb::core::getDefaultDebugUtilsMessengerCreateInfoEXT());
320-
}
321-
else if (contains(enabled_extensions, VK_EXT_DEBUG_REPORT_EXTENSION_NAME))
322-
{
323-
debug_report_callback = handle.createDebugReportCallbackEXT(vkb::core::getDefaultDebugReportCallbackCreateInfoEXT());
324-
}
325-
#endif
326262
}
327263

328264
template <vkb::BindingType bindingType>
329-
inline Instance<bindingType>::Instance(vk::Instance instance, const std::vector<const char *> &externally_enabled_extensions, bool needsToInitializeDispatcher) :
265+
inline Instance<bindingType>::Instance(vk::Instance instance, std::vector<char const *> const &externally_enabled_extensions, bool needsToInitializeDispatcher) :
330266
handle{instance}
331267
{
332268
if (needsToInitializeDispatcher)
@@ -350,38 +286,19 @@ inline Instance<bindingType>::Instance(vk::Instance instance, const std::vector<
350286
}
351287

352288
template <vkb::BindingType bindingType>
353-
inline Instance<bindingType>::Instance(VkInstance instance, const std::vector<const char *> &externally_enabled_extensions) :
289+
inline Instance<bindingType>::Instance(VkInstance instance, std::vector<char const *> const &externally_enabled_extensions) :
354290
Instance<bindingType>(static_cast<vk::Instance>(instance), externally_enabled_extensions, true)
355291
{}
356292

357293
template <vkb::BindingType bindingType>
358294
inline Instance<bindingType>::~Instance()
359295
{
360-
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
361-
if (debug_utils_messenger)
362-
{
363-
handle.destroyDebugUtilsMessengerEXT(debug_utils_messenger);
364-
}
365-
if (debug_report_callback)
366-
{
367-
handle.destroyDebugReportCallbackEXT(debug_report_callback);
368-
}
369-
#endif
370-
371296
if (handle)
372297
{
373298
handle.destroy();
374299
}
375300
}
376301

377-
#if defined(USE_VALIDATION_LAYERS)
378-
# undef USE_VALIDATION_LAYERS
379-
#endif
380-
381-
#if defined(USE_VALIDATION_LAYER_FEATURES)
382-
# undef USE_VALIDATION_LAYER_FEATURES
383-
#endif
384-
385302
template <vkb::BindingType bindingType>
386303
inline typename Instance<bindingType>::InstanceType Instance<bindingType>::get_handle() const
387304
{
@@ -393,18 +310,16 @@ inline typename Instance<bindingType>::InstanceType Instance<bindingType>::get_h
393310
{
394311
return static_cast<VkInstance>(handle);
395312
}
396-
return handle;
397313
}
398314

399315
template <vkb::BindingType bindingType>
400-
inline bool Instance<bindingType>::is_enabled(char const *extension) const
316+
inline bool Instance<bindingType>::is_extension_enabled(char const *extension) const
401317
{
402-
return std::ranges::find_if(enabled_extensions, [extension](std::string const &enabled_extension) { return enabled_extension == extension; }) !=
403-
enabled_extensions.end();
318+
return std::ranges::any_of(enabled_extensions, [extension](std::string const &enabled_extension) { return enabled_extension == extension; });
404319
}
405320

406321
template <vkb::BindingType bindingType>
407-
inline std::vector<std::string> const &Instance<bindingType>::get_extensions()
322+
inline std::vector<std::string> const &Instance<bindingType>::get_enabled_extensions()
408323
{
409324
return enabled_extensions;
410325
}

framework/core/physical_device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ template <typename FeatureType>
229229
inline FeatureType &PhysicalDevice<bindingType>::add_extension_features_impl()
230230
{
231231
// We cannot request extension features if the physical device properties 2 instance extension isn't enabled
232-
if (!instance.is_enabled(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME))
232+
if (!instance.is_extension_enabled(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME))
233233
{
234234
throw std::runtime_error("Couldn't request feature from device as " + std::string(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME) +
235235
" isn't enabled!");
@@ -320,7 +320,7 @@ template <typename FeatureType>
320320
inline FeatureType PhysicalDevice<bindingType>::get_extension_features_impl()
321321
{
322322
// We cannot request extension features if the physical device properties 2 instance extension isn't enabled
323-
if (!instance.is_enabled(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME))
323+
if (!instance.is_extension_enabled(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME))
324324
{
325325
throw std::runtime_error("Couldn't request feature from device as " + std::string(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME) +
326326
" isn't enabled!");

framework/core/swapchain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2019-2025, Arm Limited and Contributors
1+
/* Copyright (c) 2019-2026, Arm Limited and Contributors
22
*
33
* SPDX-License-Identifier: Apache-2.0
44
*
@@ -602,7 +602,7 @@ std::vector<Swapchain::SurfaceFormatCompression> Swapchain::query_supported_fixe
602602

603603
if (device.is_extension_enabled(VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME))
604604
{
605-
if (device.get_gpu().get_instance().is_enabled(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME))
605+
if (device.get_gpu().get_instance().is_extension_enabled(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME))
606606
{
607607
VkPhysicalDeviceSurfaceInfo2KHR surface_info{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SURFACE_INFO_2_KHR};
608608
surface_info.surface = surface;

framework/platform/unix/direct_window.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ static std::vector<T> get_props(F func, Targs... args)
261261

262262
VkSurfaceKHR DirectWindow::create_surface(vkb::core::InstanceC &instance)
263263
{
264-
return create_surface(instance.get_handle(), get_props<VkPhysicalDevice>(vkEnumeratePhysicalDevices, instance.get_handle()).front());
264+
return create_surface(instance.get_handle(), VK_NULL_HANDLE);
265265
}
266266

267267
// Local structure for holding display candidate information
@@ -366,11 +366,16 @@ static std::vector<Candidate> find_display_candidates(VkPhysicalDevice phys_dev,
366366

367367
VkSurfaceKHR DirectWindow::create_surface(VkInstance instance, VkPhysicalDevice phys_dev)
368368
{
369-
if (instance == VK_NULL_HANDLE || phys_dev == VK_NULL_HANDLE)
369+
if (instance == VK_NULL_HANDLE)
370370
{
371371
return VK_NULL_HANDLE;
372372
}
373373

374+
if (phys_dev == VK_NULL_HANDLE)
375+
{
376+
phys_dev = get_props<VkPhysicalDevice>(vkEnumeratePhysicalDevices, instance).front();
377+
}
378+
374379
// Find possible display config candidates
375380
std::vector<Candidate> candidates = find_display_candidates(phys_dev, properties.mode);
376381
if (candidates.empty())

0 commit comments

Comments
 (0)