c++unity-game-enginevulkan

Why vkCreateInstance returns VK_ERROR_INITIALIZATION_FAILED although VULKAN is supported?


My PC supports DirectX11 & Vulkan. I run my unity game with vulkan without problems.

I need to check Vulkan support in a separate C++ project. It is necessary to know about possibility to run my game with vulkan command line arguments.

Here's the code:

bool checkVulkanSupport(VkResult* res) {
    // Initialize Vulkan instance
    VkInstance instance;
    VkInstanceCreateInfo createInfo{};
    createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;

    // Add validation layers
    const char* validationLayers[] = { "VK_LAYER_KHRONOS_validation" };
    createInfo.enabledLayerCount = 1;
    createInfo.ppEnabledLayerNames = validationLayers;

    *res = vkCreateInstance(&createInfo, nullptr, &instance);// << VK_ERROR_INITIALIZATION_FAILED
    // Create a Vulkan instance
    if (*res != VK_SUCCESS)
        return false; // Vulkan instance creation failed

    uint32_t deviceCount = 0;
    vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);

    if (deviceCount == 0) {
        vkDestroyInstance(instance, nullptr);
        return false; // No physical devices found
    }

    std::vector<VkPhysicalDevice> devices(deviceCount);
    vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());

    bool supportsVulkan = false;
    for (const auto& device : devices) {
        VkPhysicalDeviceProperties deviceProperties;
        vkGetPhysicalDeviceProperties(device, &deviceProperties);

        // Check if the Vulkan version is at least 1.0.0
        if (deviceProperties.apiVersion >= VK_API_VERSION_1_0) {
            supportsVulkan = true; // Found a Vulkan-supporting device
            break; // No need to check further
        }
    }

    vkDestroyInstance(instance, nullptr); // Clean up Vulkan instance
    return supportsVulkan;
}

I don't know why, but vkCreateInstance returns VK_ERROR_INITIALIZATION_FAILED.

This part of code causes the problem:

// Initialize Vulkan instance
VkInstance instance;
VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    
// Add validation layers
const char* validationLayers[] = { "VK_LAYER_KHRONOS_validation" };
createInfo.enabledLayerCount = 1;
createInfo.ppEnabledLayerNames = validationLayers;
    
*res = vkCreateInstance(&createInfo, nullptr, &instance);// << VK_ERROR_INITIALIZATION_FAILED

Why vkCreateInstance returns VK_ERROR_INITIALIZATION_FAILED?


Solution

  • If the only goal is to “check Vulkan support”, then try running vulkaninfo which will give you a lot of information. Nvidia includes this program in their driver installation, I believe.

    If you still need to write a program, your code is acting like it can’t find the validation layer, which is a bit puzzling because you obviously have the Vulkan header files, which are usually obtained from the Vulkan SDK and also contains the validation layer. If you somehow obtained the headers in some other way, then you need to install the SDK to get the validation layer.