DEV Community

Mark Nefedov
Mark Nefedov

Posted on

Using Vulkan without linking to it. (volk alternative for vulkan.hpp)

Instead of using the full fledged Vulkan SDK, we only can use the Vulkan-headers package and load actual Vulkan implementation from the driver.

To do this, we need to enable vulkan.hpp dynamic dispatcher by default by setting VULKAN_HPP_DISPATCH_LOADER_DYNAMIC to 1, adding VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE to the source file and updating VULKAN_HPP_DEFAULT_DISPATCHER as we load in more Vulkan features.

#define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1
#include "vulkan/vulkan.hpp"

// Vulkan defined macro storage for dispatch loader
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE

int main()
{
    vk::DynamicLoader dl;
    PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = dl.getProcAddress<PFN_vkGetInstanceProcAddr>("vkGetInstanceProcAddr");
    VULKAN_HPP_DEFAULT_DISPATCHER.init(vkGetInstanceProcAddr);
    vk::Instance instance = vk::createInstance({}, nullptr);
    // initialize function pointers for instance
    VULKAN_HPP_DEFAULT_DISPATCHER.init(instance);
    // create a dispatcher, based on additional vkDevice/vkGetDeviceProcAddr
    std::vector<vk::PhysicalDevice> physicalDevices = instance.enumeratePhysicalDevices();  
    vk::Device device = physicalDevices[0].createDevice({}, nullptr);
    // function pointer specialization for device
    VULKAN_HPP_DEFAULT_DISPATCHER.init(device);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)