DEV Community

Mark Nefedov
Mark Nefedov

Posted on

2 1

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

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay