VK_ERROR_LAYER_NOT_PRESENT error

Hello. I recently started learning the vulkan API at vulkan-tutorial. I roughly rewrote it before the article about Validation Layers. This code compiles without problems and I even see the GLFW box, but then I get the -6(VK_ERROR_LAYER_NOT_PRESENT) error code. On the site itself, a cycle was described that checks for the presence of support for layers, it calmly passes the test and displays the layer I need (I added output for debugging). What could be the problem?

P.S. I do not know C++ well, if I made a mistake or stupidity, please do not judge strictly :slight_smile:

#include <iostream>
#include <vector>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <cstring>

//#ifdef NDEBUG
//    const bool enableValidationLayers = true;
//#else
//    const bool enableValidationLayers = false;
//#endif
const bool enableValidationLayers = true;
const uint32_t WIDTH = 800;
const uint32_t HEIGHT = 600;

void closeApp(GLFWwindow *window, VkInstance instance) {
    vkDestroyInstance(instance, nullptr);
    glfwDestroyWindow(window);
    glfwTerminate();
}

VkInstanceCreateInfo createVkInfo(struct VkInstanceCreateInfo createInfo, std::vector<const char*> validationLayers) {
    VkApplicationInfo appInfo{};
    uint32_t glfwExtensionCount = 0;
    const char** glfwExtensions;
    glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
    createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    createInfo.pApplicationInfo = nullptr;
    createInfo.enabledExtensionCount = glfwExtensionCount;
    createInfo.ppEnabledExtensionNames = glfwExtensions;
    if (enableValidationLayers) {
        createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
        createInfo.ppEnabledLayerNames = validationLayers.data();
    } else {
        createInfo.enabledLayerCount = 0;
    }
    return createInfo;
}

bool checkValidationSupport(std::vector<const char*> validationLayers) {
    uint32_t layerCount;
    vkEnumerateInstanceLayerProperties(&layerCount, nullptr);

    std::vector<VkLayerProperties> availableLayers(layerCount);
    vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
    for (const char* layername : validationLayers){
        bool layerFound = false;
        for (const VkLayerProperties& layerProperties : availableLayers){
            std::cout << layerProperties.layerName << std::endl;
            if (strcmp(layername, layerProperties.layerName) == 0){
                layerFound = true;
                break;
            }
        }
        if (!layerFound){
            return false;
        }
    }
    return true;
}

int main() {
    const std::vector<const char*> validationLayers = {
            "VK_LAYER_KHRONOS_validation"
    };
    if (enableValidationLayers && !checkValidationSupport(validationLayers)) {
        std::cout << "Error in validation layers initialization" << std::endl;
    } else {
        std::cout << "Validation Layers enabled" << std::endl;
    }
    GLFWwindow* window;
    VkInstance instance;
    VkInstanceCreateInfo createInfo{};

    glfwInit();
    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
    window = glfwCreateWindow(WIDTH, HEIGHT, "Engine", nullptr, nullptr);

    createInfo = createVkInfo(createInfo, validationLayers);

    VkResult result = vkCreateInstance(&createInfo, nullptr, &instance);
    if (result != VK_SUCCESS){
        std::cout << result;
        return result;
    }

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

    closeApp(window, instance);
    return 0;
}

The code above only enables the validation layers, and if that fails it’s probably because you haven’t installed the validation layers e.g. by installing LunarG’s Vulkan SDK.