c++ - vkCreatePipeline 失败和验证层段错误

标签 c++ vulkan

我正在尝试使用 Vulkan 在 HTC Vive 上绘制一些东西。

我启用了验证层,程序在 vkCreateGraphicsPipeline 内给我段错误。段错误发生在 VkLayer_core_validation.dll 中。如果这还不够奇怪,发生段错误的函数是 vkEmurateInstanceExtensions。因此,我在没有验证层的情况下进行了测试,然后 vkCreateGraphicsPipeline 失败并返回 VK_ERROR_VALIDATION_FAILED_EXT

我现在已经多次阅读我所关注的教程的管道部分,但我没有发现任何错误。我还尝试了旧版本的 Vulkan SDK,但唯一的区别是该段错误发生在 vkCreateGraphicsPipeline 内的 vkGetInstanceProcAddr 中。

static int loadShader(VrDevice *device,VkShaderModule *module,char *filename){
    // load the shader
         .
         .
         .
    // Create the VkShaderModule
    VkShaderModuleCreateInfo shadermodule;
    shadermodule.sType=VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
    shadermodule.flags=0;
    shadermodule.pNext=NULL;
    shadermodule.codeSize=buffersize;
    shadermodule.pCode=(const uint32_t *)buffer;
    VkResult result=vkCreateShaderModule(device->logicaldevice,&shadermodule,NULL,module);

    // Check for vulkan error and free allocated memory before exiting
    delete[] buffer;
    if(result==VK_SUCCESS) return 1;
    else return 0;
}

int renderingInit(VrDevice *device,char *appname){
     .
     .
     .

    VkApplicationInfo appinfo;
    appinfo.sType=VK_STRUCTURE_TYPE_APPLICATION_INFO;
    appinfo.pNext=NULL;
    appinfo.apiVersion=VK_MAKE_VERSION(1,0,0);
    appinfo.pApplicationName=appname;
    appinfo.applicationVersion=1;
    appinfo.pEngineName=appname;
    appinfo.engineVersion=1;

    VkInstanceCreateInfo instanceinfo;
    instanceinfo.sType=VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    instanceinfo.pNext=NULL;
    instanceinfo.flags=0;
    instanceinfo.pApplicationInfo=&appinfo;
    instanceinfo.enabledExtensionCount=numextension;
    instanceinfo.ppEnabledExtensionNames=extensions;
    instanceinfo.enabledLayerCount=layercount;
    instanceinfo.ppEnabledLayerNames=debuglayers;

    if(vkCreateInstance(&instanceinfo,NULL,&device->instance)!=VK_SUCCESS) return 0;
     .
     .
     .

    // Graphics card related matters
    uint32_t devicecount=1;
    result=vkEnumeratePhysicalDevices(device->instance,&devicecount,&device->physicaldevice);
    if(result==VK_SUCCESS || result==VK_INCOMPLETE){       
        vkGetPhysicalDeviceProperties(device->physicaldevice,&device->physicaldeviceprop);
        vkGetPhysicalDeviceMemoryProperties(device->physicaldevice,&device->physicaldevicememprop);
        vkGetPhysicalDeviceFeatures(device->physicaldevice,&device->physicaldevicefeatures);
    }
    else return 0

    uint32_t queuecount;
    vkGetPhysicalDeviceQueueFamilyProperties(device->physicaldevice,&queuecount,NULL);
    if(queuecount>0){
        VkQueueFamilyProperties *queues=new VkQueueFamilyProperties[queuecount];
        vkGetPhysicalDeviceQueueFamilyProperties(device->physicaldevice,&queuecount,queues);
        uint32_t queue;
        for(queue=0;queue<queuecount;queue++){
            if(queues[queue].queueFlags&VK_QUEUE_GRAPHICS_BIT) break;
        }

        delete[] queues;

        device->queuefamily=queue;

    }
    else return 0;
     .
     .
     .                  
    // Make logical device
    VkDeviceQueueCreateInfo queueinfo;
    queueinfo.sType=VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
    queueinfo.pNext=NULL;
    queueinfo.flags=0;
    queueinfo.queueCount=1;
    queueinfo.queueFamilyIndex=device->queuefamily;
    float priority=1.0f;
    queueinfo.pQueuePriorities=&priority;

    VkDeviceCreateInfo createinfo;
    createinfo.sType=VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
    createinfo.pNext=NULL;
    createinfo.flags=0;
    createinfo.pQueueCreateInfos=&queueinfo;
    createinfo.queueCreateInfoCount=1;
    createinfo.ppEnabledExtensionNames=extensions;
    createinfo.enabledExtensionCount=numextensions;
    createinfo.ppEnabledLayerNames=NULL;
    createinfo.enabledLayerCount=0;
    createinfo.pEnabledFeatures=&device->physicaldevicefeatures;

    if(vkCreateDevice(device->physicaldevice,&createinfo,NULL,&device->logicaldevice)!=VK_SUCCESS) return 0;
    vkGetDeviceQueue(device->logicaldevice,device->queuefamily,0,&device->queue);

    // Create the frame image for the Vive

     .
     .
     .      

    // Create renderpass
    VkAttachmentDescription colorattachment;
    colorattachment.format = VK_FORMAT_R8G8B8A8_SRGB;
    colorattachment.samples = VK_SAMPLE_COUNT_1_BIT;
    colorattachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
    colorattachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
    colorattachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
    colorattachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
    colorattachment.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
    colorattachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;

    VkAttachmentReference attachreferences;
    attachreferences.attachment=0;
    attachreferences.layout=VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;

    VkSubpassDescription subpass;
    subpass.flags=0;
    subpass.pipelineBindPoint=VK_PIPELINE_BIND_POINT_GRAPHICS;
    subpass.inputAttachmentCount=0;
    subpass.pInputAttachments=NULL;
    subpass.colorAttachmentCount=1;
    subpass.pColorAttachments=&attachreferences;
    subpass.pResolveAttachments=0;
    subpass.pDepthStencilAttachment=0;
    subpass.preserveAttachmentCount=0;
    subpass.pPreserveAttachments=0;

    VkRenderPassCreateInfo renderpassinfo;
    renderpassinfo.sType=VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
    renderpassinfo.pNext=NULL;
    renderpassinfo.flags=0;
    renderpassinfo.attachmentCount=1;
    renderpassinfo.pAttachments=&colorattachment;
    renderpassinfo.subpassCount=1;
    renderpassinfo.pSubpasses=&subpass;
    renderpassinfo.dependencyCount=0;
    renderpassinfo.pDependencies=NULL;

    if(vkCreateRenderPass(device->logicaldevice,&renderpassinfo,NULL,&device->renderpass)!=VK_SUCCESS) return 0;

    //** Load shaders and handle pipeline creation **//

    // Pipeline layout
    VkPipelineLayoutCreateInfo createinfo;
    createinfo.sType=VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
    createinfo.pNext=NULL;
    createinfo.flags=0;
    createinfo.setLayoutCount=0;
    createinfo.pSetLayouts=NULL;
    createinfo.pushConstantRangeCount=0;
    createinfo.pPushConstantRanges=NULL;
    if(vkCreatePipelineLayout(device->logicaldevice,&createinfo,NULL,&device->pipelinelayout)!=VK_SUCCESS) return 0;


    // Shader modules.
    VkPipelineShaderStageCreateInfo shaderstages[2];
    if(loadShader(device,&shaderstages[0].module,VERTEX_SHADER_NAME)==0 && loadShader(device,&shaderstages[1].module,FRAGMENT_SHADER_NAME)==0) return 0;

    shaderstages[0].sType=VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
    shaderstages[0].pNext=NULL;
    shaderstages[0].flags=0;
    shaderstages[0].stage=VK_SHADER_STAGE_VERTEX_BIT;
    shaderstages[0].pSpecializationInfo=NULL;
    shaderstages[0].pName="main";
    shaderstages[1].sType=VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
    shaderstages[1].pNext=NULL;
    shaderstages[1].flags=0;
    shaderstages[1].stage=VK_SHADER_STAGE_FRAGMENT_BIT;
    shaderstages[1].pSpecializationInfo=NULL;
    shaderstages[1].pName="main";

    // Descripte the vertex input to pipeline.
    VkPipelineVertexInputStateCreateInfo vertexinfo;
    vertexinfo.sType=VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
    vertexinfo.pNext=NULL;
    vertexinfo.flags=0;
    vertexinfo.pVertexAttributeDescriptions=NULL;
    vertexinfo.vertexAttributeDescriptionCount=0;
    vertexinfo.pVertexBindingDescriptions=NULL;
    vertexinfo.vertexBindingDescriptionCount=0;

    VkPipelineInputAssemblyStateCreateInfo inputassembly;
    inputassembly.sType=VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
    inputassembly.pNext=NULL;
    inputassembly.flags=0;
    inputassembly.topology=VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
    inputassembly.primitiveRestartEnable=VK_FALSE;

    // Viewport decide what reqion of framebuffer is used.
    VkViewport viewport = {0.0f,0.0f,(float)device->renderwidth,(float)device->renderheight,0.0f,1.0f};
    // Scissors decide how much pippeline covers the window (how much info goes to rasterizing).
    VkRect2D scissor = {0,0,device->renderwidth,device->renderheight};

    VkPipelineViewportStateCreateInfo viewportstate;
    viewportstate.sType=VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
    viewportstate.pNext=NULL;
    viewportstate.flags=0;
    viewportstate.pScissors=&scissor;
    viewportstate.scissorCount=1;
    viewportstate.pViewports=&viewport;
    viewportstate.viewportCount=1;

    // Rasterization infomration
    VkPipelineRasterizationStateCreateInfo rasterization;
    rasterization.sType=VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
    rasterization.pNext=NULL;
    rasterization.flags=0;
    rasterization.depthClampEnable=VK_FALSE;
    rasterization.rasterizerDiscardEnable=VK_FALSE;
    rasterization.polygonMode=VK_POLYGON_MODE_FILL;
    rasterization.cullMode=VK_CULL_MODE_BACK_BIT;
    rasterization.frontFace=VK_FRONT_FACE_CLOCKWISE;
    rasterization.depthBiasEnable=VK_FALSE;
    rasterization.depthBiasConstantFactor=0.0f;
    rasterization.depthBiasClamp=0.0f;
    rasterization.lineWidth=1.0f;

    // Multisampling
    VkPipelineMultisampleStateCreateInfo multisampling;
    multisampling.sType=VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
    multisampling.pNext=NULL;
    multisampling.flags=0;
    multisampling.rasterizationSamples=VK_SAMPLE_COUNT_1_BIT;
    multisampling.sampleShadingEnable=VK_FALSE;
    multisampling.minSampleShading=0.0f;
    multisampling.pSampleMask=NULL;
    multisampling.alphaToCoverageEnable=VK_FALSE;
    multisampling.alphaToOneEnable=VK_FALSE;

    // Color blending
    VkPipelineColorBlendAttachmentState colorblendattachment;
    colorblendattachment.colorWriteMask=VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
    colorblendattachment.blendEnable=VK_FALSE;
    colorblendattachment.srcAlphaBlendFactor=VK_BLEND_FACTOR_ONE;
    colorblendattachment.dstAlphaBlendFactor=VK_BLEND_FACTOR_ZERO;
    colorblendattachment.colorBlendOp=VK_BLEND_OP_ADD;
    colorblendattachment.srcColorBlendFactor=VK_BLEND_FACTOR_ONE;
    colorblendattachment.dstColorBlendFactor=VK_BLEND_FACTOR_ZERO;
    colorblendattachment.alphaBlendOp=VK_BLEND_OP_ADD;
    VkPipelineColorBlendStateCreateInfo colorblend;
    colorblend.sType=VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
    colorblend.pNext=NULL;
    colorblend.flags=0;
    colorblend.logicOpEnable=VK_FALSE;
    colorblend.logicOp=VK_LOGIC_OP_COPY;
    colorblend.attachmentCount=1;
    colorblend.pAttachments=&colorblendattachment;
    colorblend.blendConstants[0]=0;
    colorblend.blendConstants[1]=0;
    colorblend.blendConstants[2]=0;
    colorblend.blendConstants[3]=0;

    // If tuo want to change viewport, line width, blend constants you have to change it in this data type.
    VkPipelineDynamicStateCreateInfo dynamicstateinfo;
    dynamicstateinfo.sType=VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
    dynamicstateinfo.pNext=NULL;
    dynamicstateinfo.flags=0;
    dynamicstateinfo.dynamicStateCount=0;
    dynamicstateinfo.pDynamicStates=NULL;

    VkGraphicsPipelineCreateInfo pipelineinfo;
    pipelineinfo.sType=VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
    pipelineinfo.pNext=NULL;
    pipelineinfo.flags=0;
    pipelineinfo.stageCount=2;
    pipelineinfo.pStages=shaderstages;
    pipelineinfo.pVertexInputState=&vertexinfo;
    pipelineinfo.pInputAssemblyState=&inputassembly;
    pipelineinfo.pViewportState=&viewportstate;
    pipelineinfo.pRasterizationState=&rasterization;
    pipelineinfo.pMultisampleState=&multisampling;
    pipelineinfo.pDepthStencilState=NULL;
    pipelineinfo.pColorBlendState=&colorblend;
    pipelineinfo.pDynamicState=&dynamicstateinfo;
    pipelineinfo.layout=device->pipelinelayout;
    pipelineinfo.renderPass=device->renderpass;
    pipelineinfo.subpass=0;
    pipelineinfo.basePipelineHandle=VK_NULL_HANDLE;
    pipelineinfo.basePipelineIndex=0;

    // ** TODO: SEGMENT FAULT WHILE DEBUG LAYER IS ON! ** //
    if((result=vkCreateGraphicsPipelines(device->logicaldevice,VK_NULL_HANDLE,1,&pipelineinfo,NULL,&device->pipeline))!=VK_SUCCESS) return 0;

    // Destroy shaders after pipeline creation
    vkDestroyShaderModule(device->logicaldevice,shaderstages[0].module,NULL);
    vkDestroyShaderModule(device->logicaldevice,shaderstages[1].module,NULL);
}

编辑 1:将调试层更改为验证层。

附加信息: Vulkan SDK 版本 1.0.54.0

最佳答案

最可能的解释是您未分配指针并包含无效的非 NULL 指针值。尽管看起来您已尝试填写代码中的每个结构成员字段,但仍有可能会被忽略。在运行代码以设置成员之前尝试清除整个结构可能是值得的。

希望您使用“调试层”意味着启用标准验证层。标准验证层是一个元层,它启用多个与验证相关的层,包括“参数检查”和“核心验证”。

参数检查层如果在应该是有效指针的地方发现了NULL指针,往往会报错。这就是为什么我建议清除你的结构。这可以使该层产生有意义的消息。

core_validation 层假定参数检查“已通过”,因此在取消引用之前不会总是测试 NULL 指针。而且它无法避免取消引用错误的非 NULL 指针。目的是参数检查层将成功报告有用的错误消息,即使应用程序可能会在稍后的核心验证中崩溃。然后,程序员应解决参数检查报告的错误,这将允许核心验证完成其工作。

所以,最重要的是,我建议在填充之前将所有结构清零。确保您使用的是标准验证元层,其中包括参数检查。然后观察验证错误消息。

如果所有这些都失败了,那么您可能希望在验证层中捕获段错误,以获取导致错误的数据结构的线索。看起来图层代码和符号文件之间可能存在符号不匹配。可能值得尝试解决该问题或构建您自己的图层,以便您拥有准确的符号。

关于c++ - vkCreatePipeline 失败和验证层段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45460516/

相关文章:

c++ - getline() 函数在 while 循环中不起作用

c++ - 是否有 C++ 等同于 PHP 的 var_dump 和死?

android - 我们可以在 Android 平台上将 Vulkan 与 Java Activity 一起使用吗

android - 我是否必须创建一个 native Activity 来获取窗口,或者我可以从 Android 设备上的窗口管理器为 vulkan 应用程序提供一个?

opengl - Vulkan 和 X11 Pixmap

c++ - 如何停止命令缓冲区之间的清除?

C++ 在嵌入式系统中需要汇编

c++ - 计算PI最多42个小数位

C++ 代码与模板方法调用混淆

c++ - Vulkan 计算着色器只读取部分统一缓冲区