Vulkan macOS Instance failed to load

945 views Asked by At

I'm following the vulkan tutorial to create an instance on macOS. I've diverged slightly in that I'm using Clion and thus CMake, rather than XCode. When I try to run the program, I cannot create a Vulkan instance and the process exits with an error.

I've checked and Vulkan is supported by my GLFW library (3.3, installed with homebrew --HEAD flag).

My code:

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

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

const int WIDTH = 800;
const int HEIGHT = 600;

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

private:
    GLFWwindow* window;
    VkInstance instance;

    void initWindow() {
        glfwInit();

        glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
        glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

        window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
    }

    void initVulkan() {
        if(glfwVulkanSupported()) {
            std::cout << "Vulkan supported";
            createInstance();
        }
    }

    void mainLoop() {
        while (!glfwWindowShouldClose(window)) {
            glfwPollEvents();
        }
    }

    void cleanup() {
        vkDestroyInstance(instance, nullptr);

        glfwDestroyWindow(window);

        glfwTerminate();
    }

    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);

        createInfo.enabledExtensionCount = glfwExtensionCount;
        createInfo.ppEnabledExtensionNames = glfwExtensions;

        createInfo.enabledLayerCount = 0;

        if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
            throw std::runtime_error("failed to create 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;
}

My CMakeLists.txt file:

cmake_minimum_required(VERSION 3.14)
project(vulkantutorial)

set(CMAKE_CXX_STANDARD 17)

add_executable(vulkantutorial main.cpp)

find_package(Vulkan REQUIRED)
target_include_directories(${PROJECT_NAME} PUBLIC ${Vulkan_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} Vulkan::Vulkan)

find_package(PkgConfig REQUIRED)
pkg_search_module(GLM REQUIRED glm)
include_directories(${GLM_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${GLM_LIBRARY_DIRS})

find_package(glfw3 3.3 REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${GLFW_LIBRARIES})

In my CLion settings, I've defined the following environment variables:

  • DYLD_LIBRARY_PATH
  • VK_LAYER_PATH
  • VK_ICD_FILENAMES
  • VULKAN_SDK

All but VULKAN_SDK were required to get vulkaninfo:

==========
VULKANINFO
==========

Vulkan Instance Version: 1.1.114



Instance Extensions:
====================
Instance Extensions     count = 8
        VK_EXT_debug_report                 : extension revision  9
        VK_EXT_debug_utils                  : extension revision  1
        VK_EXT_swapchain_colorspace         : extension revision  4
        VK_KHR_device_group_creation        : extension revision  1
        VK_KHR_get_physical_device_properties2: extension revision  1
        VK_KHR_get_surface_capabilities2    : extension revision  1
        VK_KHR_surface                      : extension revision 25
        VK_MVK_macos_surface                : extension revision  2
Layers: count = 0
=======
Presentable Surfaces:
=====================
GPU id       : 0 (Intel(R) Iris(TM) Plus Graphics 655)
Surface type : VK_MVK_macos_surface
Formats:                count = 32
        B8G8R8A8_UNORM
...

Can anyone shed some light and help?

1

There are 1 answers

0
junglie85 On

It seems that although I set the environment variables in CLion Preferences -> Build, Execution, Deployment -> CMake -> Environment, these are not passed through to the run configuration.

This is interesting, because they seem to be partially registered - I made a typo in DYLD_LIBRARY_PATH and got dyld: warning, unknown environment variable: DYLD_LIBRARY for my troubles.

Setting the environment variables explicitly in the run configuration solved the problem (and also highlighted an error in my layer path). Although, would I be correct in thinking these should be passed down through CMake?