首页 > 解决方案 > 获取 xC0000005: 调用 vkCreateGraphicsPipelines() 函数时访问冲突读取位置 0xCCCCCCCC 错误

问题描述

在验证层中没有错误

在 ParticleSimulation.exe 中的 0x79AFD88F (VkLayer_khronos_validation.dll) 处引发异常:0xC0000005:访问冲突读取位置 0xCCCCCCCC。

我尝试在运行时找到未初始化的内容

这是在运行代码时初始化的值

我尝试将所有 pNext 设置为 nullptr 并将标志设置为 0

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;

    //VERTEX INPUT BINDING DESCRIPTION
    vertices.bindingDescriptions.resize(1);
    vertices.bindingDescriptions[0].binding = 0;
    vertices.bindingDescriptions[0].stride = sizeof(Particle);
    vertices.bindingDescriptions[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;

    //VERTEX ATTRIBUTE DESCRIPTION from the given binding
    //(index at which vertex buffer is bind i.e from which binding to retrieve these attributes)
    vertices.attributeDescriptions.resize(3);

    // Position : 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;

    // Color : 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);

    // Velocity : 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);

    // VERTEX INPUT STATE assigned to vertex buffer
    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;
    //kept Same as SwapChain ImageExtent
    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("failed to create GRAPHICS pipeline layout!");
    }

    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("failed to create graphics pipeline!");
    }

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

标签: 3dvulkanparticle-system

解决方案


我们必须在 Vulkan 中声明 createInfos 类型的变量时添加“{}” 来初始化它们,这就是我错过的。

例如-

VkGraphicsPipelineCreateInfo pipelineCreateInfo{};

这可能会消除由于未初始化而在程序中发生的所有访问冲突错误。我花了几个小时才弄清楚这一点。

对于解释,请参阅 Kro0ze 的这个答案


推荐阅读