You need to enable JavaScript to run this app. Vulkan调用vkCreateGraphicsPipelines触发0xC0000005访问违例崩溃
优惠活动
大模型
产品
解决方案
定价
更多

Vulkan调用vkCreateGraphicsPipelines触发0xC0000005访问违例崩溃

Vulkan粒子模拟项目vkCreateGraphicsPipelines调用崩溃问题

异常表现

  • 启动Vulkan验证层后未输出任何合规性错误提示
  • 异常触发于ParticleSimulation.exe进程内地址0x79AFD88F,所属模块为VkLayer_khronos_validation.dll
  • 错误码为0xC0000005,触发原因为读取0xCCCCCCCC地址处内存时发生访问违例
  • 已完成排查操作:
    • 检查程序运行流程中的未初始化变量
    • 将所有Vulkan结构体的pNext成员设为nullptr、flags成员设为0,问题仍未解决
    • 调试过程中观察到的相关结构体初始化状态可参考对应截图

核心实现代码

PrepareGraphicsPipeline() 完整实现

void ParticleSystem::PrepareGraphicsPipeline()
{
    auto vertShaderModule = loadShader("Shaders/shader.vert.spv", m_Device);
    auto fragShaderModule = loadShader("Shaders/shader.frag.spv", m_Device);

    std::array<VkPipelineShaderStageCreateInfo,2> shaderStages;
    shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
    shaderStages[0].pNext = nullptr;
    shaderStages[0].flags = 0;
    shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
    shaderStages[0].pName = "main";
    shaderStages[0].module = vertShaderModule;
    shaderStages[0].pSpecializationInfo = nullptr;

    shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
    shaderStages[1].pNext = nullptr;
    shaderStages[1].flags = 0;
    shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
    shaderStages[1].pName = "main";
    shaderStages[1].module = fragShaderModule;
    shaderStages[1].pSpecializationInfo = nullptr;

    // 顶点输入绑定描述
    vertices.bindingDescriptions.resize(1);
    vertices.bindingDescriptions[0].binding = 0;
    vertices.bindingDescriptions[0].stride = sizeof(Particle);
    vertices.bindingDescriptions[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;

    // 顶点属性描述(对应顶点缓冲绑定索引)
    vertices.attributeDescriptions.resize(3);

    // 位置:Location = 0
    vertices.attributeDescriptions[0].binding = 0;
    vertices.attributeDescriptions[0].location = 0;
    vertices.attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
    vertices.attributeDescriptions[0].offset = 0;

    // 颜色:Location = 1
    vertices.attributeDescriptions[1].binding = 0;
    vertices.attributeDescriptions[1].location = 1;
    vertices.attributeDescriptions[1].format = VK_FORMAT_R32G32B32A32_SFLOAT;
    vertices.attributeDescriptions[1].offset = 2 * sizeof(float);

    // 速度:Location = 2
    vertices.attributeDescriptions[2].binding = 0;
    vertices.attributeDescriptions[2].location = 2;
    vertices.attributeDescriptions[2].format = VK_FORMAT_R32G32B32_SFLOAT;
    vertices.attributeDescriptions[2].offset = 6 * sizeof(float);

    // 顶点输入状态配置
    vertices.inputState.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
    vertices.inputState.pNext = nullptr;
    vertices.inputState.flags = 0;
    vertices.inputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertices.bindingDescriptions.size());
    vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
    vertices.inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertices.attributeDescriptions.size());
    vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();

    VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCreateInfo;
    inputAssemblyStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
    inputAssemblyStateCreateInfo.pNext = nullptr;
    inputAssemblyStateCreateInfo.flags = 0;
    inputAssemblyStateCreateInfo.topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
    inputAssemblyStateCreateInfo.primitiveRestartEnable = VK_FALSE;

    std::vector<VkViewport> viewPort;
    viewPort.resize(1);
    viewPort[0].x = 0.0f;
    viewPort[0].y = 0.0f;
    // 与交换链图像尺寸保持一致
    viewPort[0].width = (float)m_SwapChainExent.width;
    viewPort[0].height = (float)m_SwapChainExent.height;
    viewPort[0].minDepth = 0.0f;
    viewPort[0].maxDepth = 1.0f;

    std::vector<VkRect2D> scissor;
    scissor.resize(viewPort.size());
    scissor[0].offset = { 0,0 };
    scissor[0].extent = m_SwapChainExent;

    VkPipelineViewportStateCreateInfo viewPortState;
    viewPortState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
    viewPortState.pNext = nullptr;
    viewPortState.flags = 0;
    viewPortState.viewportCount = viewPort.size();
    viewPortState.pViewports = viewPort.data();
    viewPortState.scissorCount = scissor.size();
    viewPortState.pScissors = scissor.data();

    VkPipelineRasterizationStateCreateInfo rasterizationState;
    rasterizationState.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
    rasterizationState.pNext = nullptr;
    rasterizationState.flags = 0;
    rasterizationState.depthClampEnable = VK_FALSE;
    rasterizationState.rasterizerDiscardEnable = VK_FALSE;
    rasterizationState.polygonMode = VK_POLYGON_MODE_FILL;
    rasterizationState.lineWidth = 1.0f;
    rasterizationState.cullMode = VK_CULL_MODE_NONE;
    rasterizationState.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
    rasterizationState.depthBiasEnable = VK_FALSE;

    VkPipelineMultisampleStateCreateInfo multisampling;
    multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
    multisampling.pNext = nullptr;
    multisampling.flags = 0;
    multisampling.sampleShadingEnable = VK_FALSE;
    multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;

    VkPipelineColorBlendAttachmentState blendAttachmentState;
    blendAttachmentState.colorWriteMask = 0xF;
    blendAttachmentState.blendEnable = VK_TRUE;
    blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
    blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
    blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE;
    blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
    blendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
    blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_DST_ALPHA;

    VkPipelineColorBlendStateCreateInfo colorBlending;
    colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
    colorBlending.flags = 0;
    colorBlending.pNext = nullptr;
    colorBlending.logicOpEnable = VK_FALSE;
    colorBlending.logicOp = VK_LOGIC_OP_COPY;
    colorBlending.attachmentCount = 1;
    colorBlending.pAttachments = &blendAttachmentState;
    colorBlending.blendConstants[0] = 0.0f;
    colorBlending.blendConstants[1] = 0.0f;
    colorBlending.blendConstants[2] = 0.0f;
    colorBlending.blendConstants[3] = 0.0f;

    VkPipelineLayoutCreateInfo pipelineLayoutInfo;
    pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
    pipelineLayoutInfo.pNext = nullptr;
    pipelineLayoutInfo.flags = 0;
    pipelineLayoutInfo.setLayoutCount = 0;
    pipelineLayoutInfo.pSetLayouts = nullptr;
    pipelineLayoutInfo.pushConstantRangeCount = 0;
    pipelineLayoutInfo.pPushConstantRanges = nullptr;

    if (vkCreatePipelineLayout(m_Device, &pipelineLayoutInfo, nullptr, &graphics.pipelineLayout) != VK_SUCCESS) {
        throw std::runtime_error("创建图形管线布局失败!");
    }

    VkGraphicsPipelineCreateInfo pipelineCreateInfo;
    pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
    pipelineCreateInfo.pNext = nullptr;
    pipelineCreateInfo.flags = 0;
    pipelineCreateInfo.pVertexInputState = &vertices.inputState;
    pipelineCreateInfo.pInputAssemblyState = &inputAssemblyStateCreateInfo;
    pipelineCreateInfo.pRasterizationState = &rasterizationState;
    pipelineCreateInfo.pColorBlendState = &colorBlending;
    pipelineCreateInfo.pMultisampleState = &multisampling;
    pipelineCreateInfo.pViewportState = &viewPortState;
    pipelineCreateInfo.pDynamicState = nullptr;
    pipelineCreateInfo.pDepthStencilState = nullptr;
    pipelineCreateInfo.pTessellationState = nullptr;
    pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
    pipelineCreateInfo.pStages = shaderStages.data();
    pipelineCreateInfo.layout = graphics.pipelineLayout;
    pipelineCreateInfo.subpass = 0;
    pipelineCreateInfo.renderPass = m_RenderPass;
    pipelineCreateInfo.basePipelineIndex = -1;
    pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;

异常触发位置代码

if ((vkCreateGraphicsPipelines(m_Device, VK_NULL_HANDLE, 1, &pipelineCreateInfo, nullptr, &graphics.pipeline)) != VK_SUCCESS) {
    throw std::runtime_error("创建图形管线失败!");
}

vkDestroyShaderModule(m_Device, fragShaderModule, nullptr);
vkDestroyShaderModule(m_Device, vertShaderModule, nullptr);

内容的提问来源于stack exchange,提问作者RITIK GUPTA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.15 23:54:02