Search code examples
clinuxgccglfwvulkan

vulkan C program exit with error -6: VK_ERROR_LAYER_NOT_PRESENT


I have arch linux installed vulkan-devel,vulkan-intel,glfw and everything works out just fine(vkcube,vkcubepp,vulkaninfo,glfw sample).

I search layers using vulkaninfo | grep layer, and I got the recommended layer:VK_LAYER_KHRONOS_validation (Khronos Validation Layer) Vulkan version 1.2.172, layer version 1:, which is supported by my PC.

I tried to use C programming language, typing my own code, and my gcc compiler had no error generated, but the program return segfault, then I checked with valgrind and got the error code -6, which is VK_ERROR_LAYER_NOT_PRESENT.

Here is my code(with simple glfw window):

#include"stdio.h"
#include"stdlib.h"
#include"string.h"

#include"vulkan/vulkan.h"
#include"GLFW/glfw3.h"

int main(){
    //glfw init
    glfwInit();
    GLFWwindow *window;

    //glfw give window hint
    glfwWindowHint(GLFW_CLIENT_API,GLFW_NO_API);

    //set result
    VkResult vkres;

    //instance creation part
    //step1:        create vulkan application info
    VkApplicationInfo vkappinfo;
    vkappinfo.sType=VK_STRUCTURE_TYPE_APPLICATION_INFO;
    vkappinfo.pNext=NULL;
    vkappinfo.pApplicationName="template application";
    vkappinfo.applicationVersion=VK_MAKE_VERSION(1,0,0);
    vkappinfo.pEngineName="vulkan engine";
    vkappinfo.engineVersion=VK_MAKE_VERSION(1,0,0);
    vkappinfo.apiVersion=VK_API_VERSION_1_2;

    //step2:        create vulkan instance info
    VkInstanceCreateInfo vkinstcreinfo;
    vkinstcreinfo.sType=VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    vkinstcreinfo.pNext=NULL;
    vkinstcreinfo.flags=0;
    vkinstcreinfo.pApplicationInfo=&vkappinfo;

    //add layers
    char *layerlist=(char *)malloc(50*200*sizeof(char));
    char **layernames=(char **)malloc(200*sizeof(char *));
    for(size_t i=0;i<200;i++){
        layernames[i]=&layerlist[50*i];
    }
    strcpy(layernames[0],"VK_LAYER_KHRONOS_validation");
    const char * const *layername=(const char * const *)layernames;

    vkinstcreinfo.enabledLayerCount=1;
    vkinstcreinfo.ppEnabledLayerNames=layername;

    //add extensions
    char *extlist=(char *)malloc(50*0*sizeof(char));
    char **extnames=(char **)malloc(0*sizeof(char *));
    for(size_t i=0;i<0;i++){
        extnames[i]=&extlist[50*i];
    }
    const char * const *extname=(const char * const *)extnames;

    vkinstcreinfo.enabledExtensionCount=0;
    vkinstcreinfo.ppEnabledLayerNames=extname;

    //step3:        create vulkan instnace!
    VkInstance instance;
    vkres=vkCreateInstance(&vkinstcreinfo,NULL,&instance);
    if(vkres<0){
        printf("%s\n",layername[0]);
        printf("%d\n",vkres);
        printf("failed to create instance.\n");
        exit(EXIT_SUCCESS);
    }

    //glfw create window
    window=glfwCreateWindow(800,600,"vulkan template",NULL,NULL);
    while(!glfwWindowShouldClose(window)){
        glfwPollEvents();
    }

    //glfw destroy window
    glfwDestroyWindow(window);

    //free extension list
    free(extnames);
    free(extlist);

    //free layer list
    free(layernames);
    free(layerlist);

    //goodbye instance
    vkDestroyInstance(instance,NULL);

    //glfw terminate
    glfwTerminate();
    return 0;
}

Is there anyting wrong with my layer part?


Solution

  • Oh! I found the bad code!

        //add extensions
        char *extlist=(char *)malloc(50*0*sizeof(char));
        char **extnames=(char **)malloc(0*sizeof(char *));
        for(size_t i=0;i<0;i++){
            extnames[i]=&extlist[50*i];
        }
        const char * const *extname=(const char * const *)extnames;
    
        vkinstcreinfo.enabledExtensionCount=0;
        vkinstcreinfo.ppEnabledLayerNames=extname;//here!
    

    this line should be:vkinstcreinfo.ppEnabledExtensionNames=extname;

    Now it works fine. HAHA. How embarassing!