5

I'm currently learning vulkan with golang, and I've run into an issue creating the vulkan instance with validation layers. I'm trying to use VK_LAYER_KHRONOS_validation, and it shows up as an available layer when I call EnumerateInstanceLayerProperties, but when I pass the layer name into my InstanceCreateInfo.PpEnabledLayerNames, and call CreateInstance, I get ErrorLayerNotPresent

I've tried changing how I pass the layer []string to the InstanceCreateInfo struct, and I've tried testing to see if using a different layer works, but I keep coming to the same error.

Here is my main function

func main() {
    logger := initLogger()

    logger.Println("initializing glfw window...")
    window, err := initWindow()
    if err != nil {
        fatalError(
            "unable to initialize glfw window",
            err,
            logger,
        )
    }

    vk.SetGetInstanceProcAddr(glfw.GetVulkanGetInstanceProcAddress())
    vk.Init()

    logger.Println("initializing vulkan instance")
    instance, err := initVulkan(window, logger)
    if err != nil {
        fatalError(
            "unable to initialize vulkan instance",
            err,
            logger,
        )
    }

    mainLoop(window)

    cleanup(instance)
}

My vulkan initialization code

const enableValidationLayers = true

func getValidationLayers() []string {
    return []string{
        "VK_LAYER_KHRONOS_validation",
    }
}

func initVulkan(window *glfw.Window, logger *log.Logger) (vk.Instance, error) {
    if enableValidationLayers && !checkValidationLayerSupport(logger) {
        return nil, errors.New("validation layer requested but not supported")
    }

    var appInfo = vk.ApplicationInfo{
        SType:              vk.StructureTypeApplicationInfo,
        PNext:              nil,
        PApplicationName:   "Hello Triangle",
        ApplicationVersion: vk.MakeVersion(1, 0, 0),
        PEngineName:        "No Engine",
        EngineVersion:      vk.MakeVersion(1, 0, 0),
        ApiVersion:         vk.ApiVersion10,
    }

    glfwExtensions := window.GetRequiredInstanceExtensions()

    var createInfo = vk.InstanceCreateInfo{
        SType:                   vk.StructureTypeInstanceCreateInfo,
        PNext:                   nil,
        Flags:                   0,
        PApplicationInfo:        &appInfo,
        EnabledLayerCount:       0,
        PpEnabledLayerNames:     nil,
        EnabledExtensionCount:   uint32(len(glfwExtensions)),
        PpEnabledExtensionNames: glfwExtensions,
    }

    layers := getValidationLayers()
    if enableValidationLayers {
        createInfo.EnabledLayerCount = uint32(len(layers))
        createInfo.PpEnabledLayerNames = make([]string, createInfo.EnabledLayerCount)
        createInfo.PpEnabledLayerNames = layers
    } else {
        createInfo.EnabledLayerCount = 0
    }

    var instance = new(vk.Instance)
    if result := vk.CreateInstance(
        &createInfo,
        nil,
        instance,
    ); result != vk.Success {
        switch result {
        case vk.ErrorLayerNotPresent:
            return nil, errors.New("unable to create vk instance: layer not present")
        default:
            return nil, fmt.Errorf("unable to create vk instance: %v", result)
        }
    }

    return *instance, nil
}

func checkValidationLayerSupport(logger *log.Logger) bool {
    var layerCount uint32
    vk.EnumerateInstanceLayerProperties(&layerCount, nil)

    var availableLayers = make([]vk.LayerProperties, layerCount)
    vk.EnumerateInstanceLayerProperties(&layerCount, availableLayers)
    logger.Println("available layers:")
    for i := 0; i < int(layerCount); i++ {
        availableLayers[i].Deref()
        logger.Printf("\t%v\n", readLayerName(availableLayers[i].LayerName))
    }

    for _, layerName := range getValidationLayers() {
        var layerFound bool = false

        for i := 0; i < int(layerCount); i++ {
            availableLayerName := readLayerName(availableLayers[i].LayerName)
            if layerName == availableLayerName {
                layerFound = true
                break
            }
        }

        if !layerFound {
            return false
        }
    }

    return true
}

And a log showing the available layers

vulkan: main.go:48: initializing glfw window...
vulkan: main.go:61: initializing vulkan instance
vulkan: vulkan.go:81: available layers:
vulkan: vulkan.go:84:   VK_LAYER_NV_optimus
vulkan: vulkan.go:84:   VK_LAYER_MESA_device_select
vulkan: vulkan.go:84:   VK_LAYER_VALVE_steam_fossilize_32
vulkan: vulkan.go:84:   VK_LAYER_VALVE_steam_overlay_32
vulkan: vulkan.go:84:   VK_LAYER_VALVE_steam_fossilize_64
vulkan: vulkan.go:84:   VK_LAYER_VALVE_steam_overlay_64
vulkan: vulkan.go:84:   VK_LAYER_LUNARG_gfxreconstruct
vulkan: vulkan.go:84:   VK_LAYER_MESA_overlay
vulkan: vulkan.go:84:   VK_LAYER_LUNARG_screenshot
vulkan: vulkan.go:84:   VK_LAYER_KHRONOS_synchronization2
vulkan: vulkan.go:84:   VK_LAYER_LUNARG_device_simulation
vulkan: vulkan.go:84:   VK_LAYER_KHRONOS_validation
vulkan: vulkan.go:84:   VK_LAYER_LUNARG_monitor
vulkan: vulkan.go:84:   VK_LAYER_LUNARG_api_dump
vulkan: main.go:77: unable to initialize vulkan instance: 
    unable to create vk instance: layer not present

1

1 Answer 1

0

Hope this is not related to missing VK_LAYER_PATH env var. Layers Overview

Try the short code from below C++ snippet:


std::vector<char*> enabledInstanceExtensions;

enabledInstanceLayers.push_back("VK_LAYER_KHRONOS_validation");

pCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;

pCreateInfo.enabledLayerCount = static_cast<uint32_t>(enabledInstanceLayers.size());

pCreateInfo.ppEnabledLayerNames = &enabledInstanceLayers[0];

vkCreateInstance(&pCreateInfo, pAllocator, &pInstance)

Please fill the other members of the pCreateInfo and try if the instance creation works or not. Sorry I don't know golang.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.