如何正确设置 Vk Debug Utils Messenger tEXT?

How do properly set up VkDebugUtilsMessengerEXT?

我已经尝试使用多种方法让它工作,无论我得到什么:

/usr/local/include/vulkan/vulkan.hpp:2453: undefined reference to `vkCreateDebugUtilsMessengerEXT'

/usr/local/include/vulkan/vulkan.hpp:2548: undefined reference to `vkDestroyDebugUtilsMessengerEXT'

这是我目前拥有的(希望这些代码足以诊断问题)

// destroy and create funcs
PFN_vkCreateDebugUtilsMessengerEXT pfnVkCreateDebugUtilsMessengerEXT;
PFN_vkDestroyDebugUtilsMessengerEXT pfnVkDestroyDebugUtilsMessengerEXT;

VKAPI_ATTR VkResult VKAPI_CALL vkCreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugUtilsMessengerEXT* pMessenger)
{
  return pfnVkCreateDebugUtilsMessengerEXT(instance, pCreateInfo, pAllocator, pMessenger);
}

VKAPI_ATTR void VKAPI_CALL vkDestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT messenger, VkAllocationCallbacks const * pAllocator)
{
  return pfnVkDestroyDebugUtilsMessengerEXT(instance, messenger, pAllocator);
}
// instance creation stuff here ...
createUniqueInstance(...);
//


// init and setup debug utils messenger
    pfnVkCreateDebugUtilsMessengerEXT = reinterpret_cast<PFN_vkCreateDebugUtilsMessengerEXT>(
        instance->getProcAddr("vkCreateDebugUtilsMessengerEXT"));
    if (!pfnVkCreateDebugUtilsMessengerEXT) {
        exit_(1, "GetInstanceProcAddr: Unable to find pfnVkCreateDebugUtilsMessengerEXT function.");
    }

    pfnVkDestroyDebugUtilsMessengerEXT = reinterpret_cast<PFN_vkDestroyDebugUtilsMessengerEXT>(
        instance->getProcAddr("vkDestroyDebugUtilsMessengerEXT"));
    if (!pfnVkDestroyDebugUtilsMessengerEXT) {
        exit_(1, "GetInstanceProcAddr: Unable to find pfnVkDestroyDebugUtilsMessengerEXT function.");
    }


    vk::DebugUtilsMessageSeverityFlagsEXT severityFlags(
        vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning |
        vk::DebugUtilsMessageSeverityFlagBitsEXT::eError);
    vk::DebugUtilsMessageTypeFlagsEXT messageTypeFlags(
        vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral |
        vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance |
        vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation);
    debugUtilsMessenger = instance->createDebugUtilsMessengerEXTUnique(vk::DebugUtilsMessengerCreateInfoEXT(
        {}, severityFlags, messageTypeFlags, &vk::su::debugUtilsMessengerCallback));

// 注意 vk::su::debugUtilsMessengerCallback 来自这里: https://github.com/KhronosGroup/Vulkan-Hpp/blob/master/samples/utils/utils.hpp

'createUniqueInstance' 函数也或多或少地派生自上面文件中的 createInstance 函数。不过,应该 没有任何修改会影响 debug utils messenger 内容。

我做错了什么,总是收到链接器错误。

这是由 vulkan.hpp C++ header 引起的。它假定所有函数都存在,而扩展函数(不是核心的一部分)并非如此。因此,您需要将动态调度程序传递给 Messenger 回调创建函数。

我自己没有使用 vulkan.hpp,但在你的情况下应该是这样的:

debugUtilsMessenger = instance->createDebugUtilsMessengerEXTUnique(vk::DebugUtilsMessengerCreateInfoEXT(
    {}, severityFlags, messageTypeFlags, &vk::su::debugUtilsMessengerCallback), nullptr, vk::DispatchLoaderDynamic{ *instance });

注意作为最后一个参数传递的 vk::DispatchLoaderDynamic

可以找到有关此的详细信息here