vulkan C 程序退出并出现错误 -6:VK_ERROR_LAYER_NOT_PRESENT

vulkan C program exit with error -6: VK_ERROR_LAYER_NOT_PRESENT

我安装了 arch linux vulkan-devel、vulkan-intel、glfw,一切正常(vkcube、vkcubepp、vulkaninfo、glfw 样本)。

我使用vulkaninfo | grep layer搜索图层,得到推荐的图层:VK_LAYER_KHRONOS_validation (Khronos Validation Layer) Vulkan version 1.2.172, layer version 1:,我的电脑支持

我尝试用C语言,自己敲代码,gcc编译器没有报错,但是程序returnsegfault,然后用valgrind查了一下,得到错误码-6,这是 VK_ERROR_LAYER_NOT_PRESENT.

这是我的代码(使用简单的 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;
}

我的图层部分有问题吗?

哦!我发现了错误代码!

    //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!

这一行应该是:vkinstcreinfo.ppEnabledExtensionNames=extname;

现在可以正常使用了。哈哈。好尴尬!