Vulkan, glfw and macOS: vkCreateInstance returns VK_ERROR_INITIALIZATION_FAILED

1.1k views Asked by At

I'm going through Vulkan tutorial and stuck on creating an instance I'm using macOS 10.15.6 (19G2021) on my MacBook Pro 15 (2019) All environment variables as VK_ICD_FILENAMES and VK_LAYER_PATH are set correctly as per tutorial.

glfwVulkanSupported() returns True glfwGetRequiredInstanceExtensions(&glfwExtensionCount) returns NULL

Here is my main.cpp:

#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>

#include <iostream>
#include <stdexcept>
#include <cstdlib>

#include <stdio.h>

const uint32_t WIDTH = 800;
const uint32_t HEIGHT = 600;

class HelloTriangleApplication {
public:
    void run() {
        initWindow();
        initVulkan();
        mainLoop();
        cleanup();
    }

private:
    void initWindow() {
        glfwInit();
        
        glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
        glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
        window_ = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
    }
    
    // initiate vulkan objects
    void initVulkan() {
        if (glfwVulkanSupported()) {
            std::cout << "Vulkan Supported" << std::endl;
            createInstance();
        }
    }

    void createInstance() {
        VkApplicationInfo appInfo{};
        appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
        appInfo.pApplicationName = "Hello Triangle";
        appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
        appInfo.pEngineName = "No Engine";
        appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
        appInfo.apiVersion = VK_API_VERSION_1_0;
        
        VkInstanceCreateInfo createInfo{};
        createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
        createInfo.pApplicationInfo = &appInfo;

        uint32_t glfwExtensionCount = 0;
        const char** glfwExtensions;
        
        glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
        printf("glfwExtensions %d\n", glfwExtensionCount);
        
        createInfo.enabledExtensionCount = glfwExtensionCount;
        createInfo.ppEnabledExtensionNames = glfwExtensions;

        createInfo.enabledLayerCount = 0;
        
        VkResult result = vkCreateInstance(&createInfo, nullptr, &instance_);
        if (result != VK_SUCCESS) {
            printf("failed to create instance with error_code %d", result);
            throw std::runtime_error("failed to create instance!");
        } else {
            std::cout << "Instance for application " << appInfo.pApplicationName << " created successfully";
        }
    }
    
    void mainLoop() {
        while (!glfwWindowShouldClose(window_)) {
            glfwPollEvents();
        }
    }

    void cleanup() {
        glfwDestroyWindow(window_);
        
        glfwTerminate();
    }
    
    GLFWwindow* window_;
    VkInstance instance_;
};

int main() {
    HelloTriangleApplication app;

    try {
        app.run();
    } catch (const std::exception& e) {
        std::cerr << e.what() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
2

There are 2 answers

0
Daria Zenkova On

I run ./install_vulkan.py previously, so environment variables were already set. My guess is that trying to override them in XCode to a local sdk path confused the dependencies. So I've just gone down the path where I have environment variables set by the script and the library copied to /usr/local/lib (Alternate methods using system paths) https://vulkan.lunarg.com/doc/sdk/1.2.148.1/mac/getting_started.html

0
TylerRajotte On

I was stumbling into this myself and I found removing any relative file paths like ~/{stuff} and replacing them with the actual path /user/{user}/{stuff} fixed my problem after bashing my head in for half a day and considering completely restarting on linux. This was happening in my environment variables under my schema in xcode