vkCreatePipeline失败并且验证层段出现故障

时间:2017-08-02 12:10:20

标签: c++ vulkan

我正试图用Vulkan向HTC Vive画一些东西。

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

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

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

2 个答案:

答案 0 :(得分:1)

最可能的解释是您将指针保留为未分配且包含无效的非NULL指针值。虽然看起来你已经尝试填写代码中的每个struct成员字段,但是有可能被忽略了。在运行代码来设置成员之前,可能需要尝试清除整个结构。

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

如果参数检查层找到应该有有效指针的NULL指针,它通常会报告错误消息。这就是为什么我建议清除你的结构。这可以使该层产生有意义的消息。

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

所以,最重要的是,我建议在填写之前将所有结构清零。确保使用包含参数检查的标准验证元层。然后注意验证错误消息。

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

答案 1 :(得分:0)

图层中存在一个错误(可能在驱动程序中 - 它至少会使我的AMD崩溃)。 而不是pipelineinfo.pTessellationState的未初始化值(有效,但显然是崩溃),请尝试NULL

如果在调试模式下编译,您还有其他应该被图层捕获的错误:

  • 未初始化colorattachment.flags(可能应为0
  • dynamicstateinfo.dynamicStateCount 必须不是0(如果您要禁用动态状态,请完全使用pipelineinfo.pDynamicState=NULL
  • 由于&&短路而不会加载一个着色器模块(我认为您打算使用||代替)

请启用您的验证图层("VK_LAYER_LUNARG_standard_validation")+报告验证它们是否按预期工作。在编译器调试模式下编译和测试您的程序(它将通过填充未初始化的值来填充更多可检测的模式来帮助您)。对我来说,这三个人在撞车事故发生之前都是正确的报道。

我正处于修复第一个阶段的过程中,希望它能够在下一个阶段发布。