how to properly load a Vulkan function?

149 Views Asked by At

Here is a little code snippet that I have. The problem is that I want to load the "createDebugUtilsMessengerEXT" function. But I get a linking error:

LNK2001: unresolved external symbol "class vk::DispatchLoaderDynamic vk::defaultDispatchLoaderDynamic".

What exactly am I doing wrong?

Also, I made this code based on this example

#include <vulkan.hpp>
#define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1

#ifdef VALIDATION_LAYERS
    VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
                                                 VkDebugUtilsMessageSeverityFlagsEXT messageType,
                                                 const VkDebugUtilsMessengerCallbackDataEXT* pCallBackData,
                                                 void* pUserData)
    {
        std::cerr << "Validation: " << pCallBackData->pMessage << std::endl;
        return VK_FALSE;
    }
#endif

vk::Instance createInstance()
{
    ...
#ifdef VALIDATION_LAYERS
        debugMessengerCreateInfo_ = vk::DebugUtilsMessengerCreateInfoEXT({},
                                                                         vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning | vk::DebugUtilsMessageSeverityFlagBitsEXT::eError,
                                                                         vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation | vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance,
                                                                         debugCallback);
#endif

        vk::Instance instance = vk::createInstance(instanceCreateInfo);

#ifdef VALIDATION_LAYERS
        VULKAN_HPP_DEFAULT_DISPATCHER.init(instance);
        debugMessenger_ = instance.createDebugUtilsMessengerEXT(debugMessengerCreateInfo_);
    #endif
1

There are 1 best solutions below

0
Botje On

You need to #define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC before you load Vulkan.hpp, and use the macro VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE after. From vulkan.hpp:

To use the vk::DispatchLoaderDynamic as the default dispatcher (means: you don't need to explicitly add it to every function call), you need to #define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1, and have the macro VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE exactly once in your source code to provide storage for that default dispatcher. Then you can use it by the macro VULKAN_HPP_DEFAULT_DISPATCHER

So the start of your program should look like:

#define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1
#include <vulkan.hpp>
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE