Glfw and dynamically loaded vulkan functions


I am creating a vulkan instance and a glfw window and then trying to create a window surface but I’m getting an error. I’m running:

    MacOS 10.15.7
    Vulkan Instance Version: 1.2.182 (from vulkaninfo)
    glfw: stable 3.3.4 (installed from homebrew)

I am dynamically loading vulkan functions, an example would be:

    VulkanApp::OS::LibraryHandle VulkanLibrary = dlopen("libvulkan.1.2.182.dylib", RTLD_NOW);
    PFN_vkCreateDevice meta_vkCreateDevice = reinterpret_cast<PFN_vkCreateDevice>(dlsym(VulkanLibrary, "vkCreateDevice"));

and I then use the function e.g.

    auto gd_err = meta_vkCreateDevice(gpu, &graphics_device_create_info, VK_NULL_HANDLE, &graphicsDevice);
    if(VK_SUCCESS != gd_err){
        throw std::runtime_error("Graphics logical device creation failed");

Everything works fine until I try and create the window surface:

void Window::createSurface(){
    if (glfwCreateWindowSurface(renderer->getInstance(), window, nullptr, &surface) != VK_SUCCESS){
        VkResult result = glfwCreateWindowSurface(renderer->getInstance(), window, nullptr, &surface);
        std::cout << result << std::endl;
        throw std::runtime_error("failed to create window surface!");

I have enabled error logging in glfw, and see:

    GLFW error: Cocoa: Vulkan instance missing VK_EXT_metal_surface extension
    libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: failed to create window surface!

The instance has the “VK_EXT_metal_surface” extension enabled, along with “VK_KHR_surface” and “VK_EXT_debug_utils”.

This is a guess, but is the error being thrown because glfwCreateWindowSurface is calling vulkan functions, but not dynamically like I am doing and this is causing the errors? I have read the glfw web page about using Vulkan and it does mention using glfwGetInstanceProcAddress but it’s a little unclear to me how I should be using it. Do I have to do something similar to the vulkan function whereby I create a meta_glfwCreateWindowSurface() function calling “libglfw.3.3.dylib” directly?

I would really appreciate any suggestions, or any steps I can take to work out what the problem is.


Yes, it needs libvulkan.1.dylib accessible in path, so it can dynamically load vkGetInstanceProcAddr for its own internal usage. Though if it didn’t find it, then the glfwInit() would fail (so check your error codes).

Or so you think. Do not post prose. Prose does not show bugs, only code does.

Do not load vkCreateDevice this way. Use vkGet*ProcAddr instead. Only get vkGetInstanceProcAddr in the system specific way. You could also avoid getting your hands dirty there and just use glfwGetInstanceProcAddress to get vkGetInstanceProcAddr.

Also link against libvulkan.1.dylib or your app will stop working after each update.

GLFW error: Cocoa: Vulkan instance missing VK_EXT_metal_surface extension

Well use vulkaninfo to query which extensions you have. And use glfwGetRequiredInstanceExtensions to query which extensions you need.