Skip to content

Commit 177edeb

Browse files
authored
Fix usage of vkCreateDebugUtilsMessengerEXT/vk::Instance::createDebugUtilsMessengerEXT. (#1526)
* Use StructureChainBuilder to create the debug callbacks. * Remove unneeded code. * Fix usage of vkCreateDebugUtilsMessengerEXT/vk::Instance::createDebugUtilsMessengerEXT.
1 parent 4eeaf24 commit 177edeb

5 files changed

Lines changed: 91 additions & 150 deletions

File tree

‎framework/core/instance.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ inline Instance<bindingType>::Instance(std::string const
247247
.enabledExtensionCount = static_cast<uint32_t>(enabled_extensions_cstr.size()),
248248
.ppEnabledExtensionNames = enabled_extensions_cstr.data()};
249249

250-
vkb::StructureChainBuilder<vkb::BindingType::Cpp, vk::InstanceCreateInfo> scb;
251-
scb.set_anchor_struct(create_info);
250+
vkb::StructureChainBuilderCpp<vk::InstanceCreateInfo> scb(create_info);
252251
if constexpr (bindingType == vkb::BindingType::Cpp)
253252
{
254253
extend_instance_create_info(scb);

‎framework/structure_chain_builder.h‎

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ namespace vkb
2525
template <vkb::BindingType bindingType, typename AnchorStructType>
2626
class StructureChainBuilder
2727
{
28+
static_assert((offsetof(AnchorStructType, sType) == 0) && (offsetof(AnchorStructType, pNext) == sizeof(void *)));
29+
2830
public:
29-
StructureChainBuilder();
31+
StructureChainBuilder(AnchorStructType const &anchor_struct);
3032

3133
template <typename T>
3234
T &add_chain_data(T const &data_to_add = {}); // Adds data to the structure chain builder that is not part of the structure chain itself, but is used by the
@@ -38,14 +40,11 @@ class StructureChainBuilder
3840
template <typename StructType>
3941
StructType const *get_struct(size_t skip = 0) const;
4042

41-
void set_anchor_struct(AnchorStructType const &anchor_struct);
42-
4343
private:
4444
template <typename StructType>
4545
StructType &add_struct_impl(StructType const &struct_to_add);
4646
template <typename StructType>
4747
StructType const *get_struct_impl(size_t skip) const;
48-
void set_anchor_struct_impl(AnchorStructType const &anchor_struct);
4948

5049
private:
5150
std::vector<std::unique_ptr<std::any>> structure_chain;
@@ -59,18 +58,17 @@ template <typename AnchorStructType>
5958
using StructureChainBuilderCpp = StructureChainBuilder<vkb::BindingType::Cpp, AnchorStructType>;
6059

6160
template <vkb::BindingType bindingType, typename AnchorStructType>
62-
template <typename T>
63-
T &StructureChainBuilder<bindingType, AnchorStructType>::add_chain_data(T const &data_to_add)
61+
inline StructureChainBuilder<bindingType, AnchorStructType>::StructureChainBuilder(AnchorStructType const &anchor_struct)
6462
{
65-
chain_data.push_back(std::make_unique<std::any>(std::make_any<T>(data_to_add)));
66-
return *std::any_cast<T>(chain_data.back().get());
63+
structure_chain.push_back(std::make_unique<std::any>(std::make_any<AnchorStructType>(anchor_struct)));
6764
}
6865

6966
template <vkb::BindingType bindingType, typename AnchorStructType>
70-
inline StructureChainBuilder<bindingType, AnchorStructType>::StructureChainBuilder()
67+
template <typename T>
68+
inline T &StructureChainBuilder<bindingType, AnchorStructType>::add_chain_data(T const &data_to_add)
7169
{
72-
static_assert((offsetof(AnchorStructType, sType) == 0) && (offsetof(AnchorStructType, pNext) == sizeof(void *)));
73-
structure_chain.push_back(std::make_unique<std::any>(std::make_any<AnchorStructType>()));
70+
chain_data.push_back(std::make_unique<std::any>(std::make_any<T>(data_to_add)));
71+
return *std::any_cast<T>(chain_data.back().get());
7472
}
7573

7674
template <vkb::BindingType bindingType, typename AnchorStructType>
@@ -115,7 +113,7 @@ inline StructType const *StructureChainBuilder<bindingType, AnchorStructType>::g
115113
}
116114
else
117115
{
118-
return reinterpret_cast<StructType const *>(get_struct_impl<vk::CppType<StructType>::Type>(skip));
116+
return reinterpret_cast<StructType const *>(get_struct_impl<typename vk::CppType<StructType>::Type>(skip));
119117
}
120118
}
121119

@@ -137,26 +135,4 @@ inline StructType const *StructureChainBuilder<bindingType, AnchorStructType>::g
137135
return (it != structure_chain.end()) ? std::any_cast<StructType>(it->get()) : nullptr;
138136
}
139137

140-
template <vkb::BindingType bindingType, typename AnchorStructType>
141-
inline void StructureChainBuilder<bindingType, AnchorStructType>::set_anchor_struct(AnchorStructType const &anchor_struct)
142-
{
143-
if constexpr (bindingType == vkb::BindingType::Cpp)
144-
{
145-
set_anchor_struct_impl(anchor_struct);
146-
}
147-
else
148-
{
149-
return reinterpret_cast<StructureChainBuilder<vkb::BindingType::Cpp, typename vk::CppType<AnchorStructType>::Type> *>(this)->add_anchor_struct(
150-
reinterpret_cast<typename vk::CppType<AnchorStructType>::Type const &>(anchor_struct));
151-
}
152-
}
153-
154-
template <vkb::BindingType bindingType, typename AnchorStructType>
155-
inline void StructureChainBuilder<bindingType, AnchorStructType>::set_anchor_struct_impl(AnchorStructType const &anchor_struct)
156-
{
157-
void const *pNext = std::any_cast<AnchorStructType>(structure_chain.front().get())->pNext;
158-
*std::any_cast<AnchorStructType>(structure_chain.front().get()) = anchor_struct;
159-
std::any_cast<AnchorStructType>(structure_chain.front().get())->pNext = pNext;
160-
}
161-
162138
} // namespace vkb

‎framework/vulkan_sample.h‎

Lines changed: 36 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,9 @@ class VulkanSample : public vkb::Application
197197
*/
198198
virtual void draw_renderpass(vkb::core::CommandBuffer<bindingType> &command_buffer, vkb::rendering::RenderTarget<bindingType> &render_target);
199199

200-
virtual void extend_instance_create_info(vkb::StructureChainBuilder<bindingType, InstanceCreateInfoType> &create_info) const;
201-
virtual uint32_t get_api_version() const;
202-
virtual DebugReportCallbackCreateInfoType const *get_debug_report_callback_create_info() const;
203-
virtual DebugUtilsMessengerCreateInfoType const *get_debug_utils_messenger_create_info() const;
204-
virtual InstanceCreateFlagsType get_instance_create_flags(std::vector<std::string> const &enabled_extensions) const;
200+
virtual void extend_instance_create_info(vkb::StructureChainBuilder<bindingType, InstanceCreateInfoType> &scb) const;
201+
virtual uint32_t get_api_version() const;
202+
virtual InstanceCreateFlagsType get_instance_create_flags(std::vector<std::string> const &enabled_extensions) const;
205203

206204
/**
207205
* @brief Override this to customise the creation of the swapchain and render_context
@@ -331,6 +329,11 @@ class VulkanSample : public vkb::Application
331329
void request_layer_settings_impl(std::vector<vk::LayerSettingEXT> &requested_layer_settings, vkb::StructureChainBuilderCpp<vk::InstanceCreateInfo> &scb) const;
332330
static void set_viewport_and_scissor_impl(vkb::core::CommandBufferCpp const &command_buffer, vk::Extent2D const &extent);
333331

332+
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
333+
vk::DebugReportCallbackCreateInfoEXT const &get_debug_report_callback_create_info() const;
334+
vk::DebugUtilsMessengerCreateInfoEXT const &get_debug_utils_messenger_create_info() const;
335+
#endif
336+
334337
/// <summary>
335338
/// PRIVATE MEMBERS
336339
/// </summary>
@@ -758,16 +761,11 @@ inline void VulkanSample<bindingType>::extend_instance_create_info_impl(vkb::Str
758761
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
759762
if (contains(create_info->enabledExtensionCount, create_info->ppEnabledExtensionNames, VK_EXT_DEBUG_UTILS_EXTENSION_NAME))
760763
{
761-
vk::DebugUtilsMessengerCreateInfoEXT debug_utils_messenger_create_info{.messageSeverity = vk::DebugUtilsMessageSeverityFlagBitsEXT::eError | vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning,
762-
.messageType = vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation | vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance,
763-
.pfnUserCallback = vkb::core::debug_utils_messenger_callback};
764-
scb.add_struct(debug_utils_messenger_create_info);
764+
scb.add_struct(get_debug_utils_messenger_create_info());
765765
}
766766
else if (contains(create_info->enabledExtensionCount, create_info->ppEnabledExtensionNames, VK_EXT_DEBUG_REPORT_EXTENSION_NAME))
767767
{
768-
vk::DebugReportCallbackCreateInfoEXT debug_report_callback_create_info{.flags = vk::DebugReportFlagBitsEXT::eError | vk::DebugReportFlagBitsEXT::eWarning | vk::DebugReportFlagBitsEXT::ePerformanceWarning,
769-
.pfnCallback = vkb::core::debug_callback};
770-
scb.add_struct(debug_report_callback_create_info);
768+
scb.add_struct(get_debug_report_callback_create_info());
771769
}
772770
#endif
773771

@@ -819,45 +817,6 @@ inline uint32_t VulkanSample<bindingType>::get_api_version() const
819817
return VK_API_VERSION_1_1;
820818
}
821819

822-
template <vkb::BindingType bindingType>
823-
inline typename VulkanSample<bindingType>::DebugReportCallbackCreateInfoType const *VulkanSample<bindingType>::get_debug_report_callback_create_info() const
824-
{
825-
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
826-
static vk::DebugReportCallbackCreateInfoEXT debug_report_callback_create_info{.flags = vk::DebugReportFlagBitsEXT::eError | vk::DebugReportFlagBitsEXT::eWarning | vk::DebugReportFlagBitsEXT::ePerformanceWarning,
827-
.pfnCallback = vkb::core::debug_callback};
828-
if constexpr (bindingType == vkb::BindingType::Cpp)
829-
{
830-
return &debug_report_callback_create_info;
831-
}
832-
else
833-
{
834-
return reinterpret_cast<VkDebugReportCallbackCreateInfoEXT *>(&debug_report_callback_create_info);
835-
}
836-
#else
837-
return nullptr;
838-
#endif
839-
}
840-
841-
template <vkb::BindingType bindingType>
842-
inline typename VulkanSample<bindingType>::DebugUtilsMessengerCreateInfoType const *VulkanSample<bindingType>::get_debug_utils_messenger_create_info() const
843-
{
844-
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
845-
static vk::DebugUtilsMessengerCreateInfoEXT debug_utils_messenger_create_info{.messageSeverity = vk::DebugUtilsMessageSeverityFlagBitsEXT::eError | vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning,
846-
.messageType = vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation | vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance,
847-
.pfnUserCallback = vkb::core::debug_utils_messenger_callback};
848-
if constexpr (bindingType == vkb::BindingType::Cpp)
849-
{
850-
return &debug_utils_messenger_create_info;
851-
}
852-
else
853-
{
854-
return reinterpret_cast<VkDebugUtilsMessengerCreateInfoEXT *>(&debug_utils_messenger_create_info);
855-
}
856-
#else
857-
return nullptr;
858-
#endif
859-
}
860-
861820
template <vkb::BindingType bindingType>
862821
inline typename VulkanSample<bindingType>::InstanceCreateFlagsType VulkanSample<bindingType>::get_instance_create_flags(std::vector<std::string> const &enabled_extensions) const
863822
{
@@ -911,6 +870,27 @@ inline vkb::core::Device<bindingType> &VulkanSample<bindingType>::get_device()
911870
}
912871
}
913872

873+
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
874+
template <vkb::BindingType bindingType>
875+
inline vk::DebugReportCallbackCreateInfoEXT const &VulkanSample<bindingType>::get_debug_report_callback_create_info() const
876+
{
877+
static vk::DebugReportCallbackCreateInfoEXT debug_report_callback_create_info =
878+
{.flags = vk::DebugReportFlagBitsEXT::eError | vk::DebugReportFlagBitsEXT::eWarning | vk::DebugReportFlagBitsEXT::ePerformanceWarning,
879+
.pfnCallback = vkb::core::debug_callback};
880+
return debug_report_callback_create_info;
881+
}
882+
883+
template <vkb::BindingType bindingType>
884+
inline vk::DebugUtilsMessengerCreateInfoEXT const &VulkanSample<bindingType>::get_debug_utils_messenger_create_info() const
885+
{
886+
static vk::DebugUtilsMessengerCreateInfoEXT debug_utils_messenger_create_info =
887+
{.messageSeverity = vk::DebugUtilsMessageSeverityFlagBitsEXT::eError | vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning,
888+
.messageType = vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation | vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance,
889+
.pfnUserCallback = vkb::core::debug_utils_messenger_callback};
890+
return debug_utils_messenger_create_info;
891+
}
892+
#endif
893+
914894
template <vkb::BindingType bindingType>
915895
inline vkb::Gui<bindingType> &VulkanSample<bindingType>::get_gui()
916896
{
@@ -1207,40 +1187,17 @@ inline bool VulkanSample<bindingType>::prepare(const ApplicationOptions &options
12071187
instance.reset(reinterpret_cast<vkb::core::InstanceCpp *>(create_instance().release()));
12081188
}
12091189

1210-
// initialize debug utils or report callback based on enabled extensions, if any
1190+
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
1191+
// initialize debug utils or report callback based on enabled extensions
12111192
if (instance->is_extension_enabled(VK_EXT_DEBUG_UTILS_EXTENSION_NAME))
12121193
{
1213-
#ifdef VKB_VULKAN_DEBUG
1214-
debug_utils = std::make_unique<vkb::core::HPPDebugUtilsExtDebugUtils>();
1215-
#endif
1216-
auto const *debug_utils_messenger_create_info = get_debug_utils_messenger_create_info();
1217-
if (debug_utils_messenger_create_info)
1218-
{
1219-
if constexpr (bindingType == BindingType::Cpp)
1220-
{
1221-
debug_utils_messenger = instance->get_handle().createDebugUtilsMessengerEXT(*debug_utils_messenger_create_info);
1222-
}
1223-
else
1224-
{
1225-
debug_utils_messenger = instance->get_handle().createDebugUtilsMessengerEXT(*reinterpret_cast<vk::DebugUtilsMessengerCreateInfoEXT const *>(debug_utils_messenger_create_info));
1226-
}
1227-
}
1194+
debug_utils_messenger = instance->get_handle().createDebugUtilsMessengerEXT(get_debug_utils_messenger_create_info());
12281195
}
12291196
else if (instance->is_extension_enabled(VK_EXT_DEBUG_REPORT_EXTENSION_NAME))
12301197
{
1231-
auto const *debug_report_callback_create_info = get_debug_report_callback_create_info();
1232-
if (debug_report_callback_create_info)
1233-
{
1234-
if constexpr (bindingType == BindingType::Cpp)
1235-
{
1236-
debug_report_callback = instance->get_handle().createDebugReportCallbackEXT(*debug_report_callback_create_info);
1237-
}
1238-
else
1239-
{
1240-
debug_report_callback = instance->get_handle().createDebugReportCallbackEXT(*reinterpret_cast<vk::DebugReportCallbackCreateInfoEXT const *>(debug_report_callback_create_info));
1241-
}
1242-
}
1198+
debug_report_callback = instance->get_handle().createDebugReportCallbackEXT(get_debug_report_callback_create_info());
12431199
}
1200+
#endif
12441201

12451202
// Getting a valid vulkan surface from the platform
12461203
surface = static_cast<vk::SurfaceKHR>(window->create_surface(reinterpret_cast<vkb::core::InstanceC &>(*instance)));

‎samples/extensions/shader_debugprintf/shader_debugprintf.cpp‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ ShaderDebugPrintf::~ShaderDebugPrintf()
5959
vkDestroyDescriptorSetLayout(get_device().get_handle(), descriptor_set_layout, nullptr);
6060

6161
vkDestroySampler(get_device().get_handle(), textures.skysphere.sampler, nullptr);
62+
63+
vkDestroyDebugUtilsMessengerEXT(get_instance().get_handle(), debug_utils_messenger, nullptr);
6264
}
6365
}
6466

@@ -445,6 +447,12 @@ bool ShaderDebugPrintf::prepare(const vkb::ApplicationOptions &options)
445447
return false;
446448
}
447449

450+
// Register a sample specific debug utils callback in addition to the one registered by the base class
451+
if (get_instance().is_extension_enabled(VK_EXT_DEBUG_UTILS_EXTENSION_NAME))
452+
{
453+
vkCreateDebugUtilsMessengerEXT(get_instance().get_handle(), &get_debug_utils_messenger_create_info(), nullptr, &debug_utils_messenger);
454+
}
455+
448456
camera.type = vkb::CameraType::LookAt;
449457
camera.set_position(glm::vec3(0.0f, 0.0f, -6.0f));
450458
camera.set_rotation(glm::vec3(0.0f, 180.0f, 0.0f));
@@ -463,27 +471,24 @@ bool ShaderDebugPrintf::prepare(const vkb::ApplicationOptions &options)
463471
return true;
464472
}
465473

466-
void ShaderDebugPrintf::extend_instance_create_info(vkb::StructureChainBuilderC<VkInstanceCreateInfo> &scb) const
474+
VkDebugUtilsMessengerCreateInfoEXT const &ShaderDebugPrintf::get_debug_utils_messenger_create_info() const
467475
{
468-
ApiVulkanSample::extend_instance_create_info(scb);
469-
470-
// Register a sample specific debug utils callback in addition to the one registered by the base class
471-
VkDebugUtilsMessengerCreateInfoEXT debug_utils_messenger_create_info{.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
472-
.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT,
473-
.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT,
474-
.pfnUserCallback = debug_utils_message_callback};
475-
scb.add_struct(debug_utils_messenger_create_info);
476+
static VkDebugUtilsMessengerCreateInfoEXT debug_utils_messenger_create_info =
477+
{.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
478+
.pNext = nullptr,
479+
.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT,
480+
.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT,
481+
.pfnUserCallback = debug_utils_message_callback,
482+
.pUserData = nullptr};
483+
return debug_utils_messenger_create_info;
476484
}
477485

478-
VkDebugUtilsMessengerCreateInfoEXT const *ShaderDebugPrintf::get_debug_utils_messenger_create_info() const
486+
void ShaderDebugPrintf::extend_instance_create_info(vkb::StructureChainBuilderC<VkInstanceCreateInfo> &scb) const
479487
{
488+
ApiVulkanSample::extend_instance_create_info(scb);
489+
480490
// Register a sample specific debug utils callback in addition to the one registered by the base class
481-
static VkDebugUtilsMessengerCreateInfoEXT local_debug_utils_messenger_create_info{.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
482-
.pNext = ApiVulkanSample::get_debug_utils_messenger_create_info(),
483-
.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT,
484-
.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT,
485-
.pfnUserCallback = debug_utils_message_callback};
486-
return &local_debug_utils_messenger_create_info;
491+
scb.add_struct(get_debug_utils_messenger_create_info());
487492
}
488493

489494
void ShaderDebugPrintf::render(float delta_time)

0 commit comments

Comments
 (0)