c++vulkandepth-bufferdepth-testing

Graphics to graphics depth dependency, depth buffer is being corrupted


I am trying to do the following: Take render pass 1, render to attachment set S.

Take render pass 2, render to attachment set S again.

There is a one to one explanation on how to do this in the official Khronos github:

Under Graphics to Graphics dependency. However, trying to implement the same code as in the example isn't working.

This is what I see:

enter image description here

This is what I should see:

Graphics to graphics depth dependency, depth buffer is being corrupted.

What I think is currently happening is that the depth buffer is not being synchronized properly and so depth testing isn't working correctly. I create both of my render passes like this:

    // Create a render pass from the information of a render target
vk::UniqueRenderPass CreateRenderPass(HardwareInterface& hi,
    const std::vector<vk::AttachmentDescription>& attachment_descriptions)
{
    auto instance    = hi.GetInstance();
    auto phys_device = hi.GetPhysicalDevice();
    auto device      = hi.GetDevice();

    // Create a reference for each image target the renderpass will render to
    std::vector<vk::AttachmentReference> attachment_references(
        attachment_descriptions.size());
    for(uint i = 0; i < attachment_descriptions.size() - 1; i++)
    {
        vk::AttachmentReference color_attachment_ref(
            i, vk::ImageLayout::eColorAttachmentOptimal);
        attachment_references[i] = color_attachment_ref;
    }
    // Last attachment is depth
    vk::AttachmentReference depth_attachment_ref(
        attachment_descriptions.size() - 1,
        vk::ImageLayout::eDepthStencilAttachmentOptimal);
    attachment_references[attachment_references.size() - 1] = depth_attachment_ref;

    const uint subpass_num = 1;
    vector<vk::SubpassDescription> subpasses(subpass_num);
    for(auto& sub_pass: subpasses)
    {
        // No input attachment (3rd and 4th parameters)
        sub_pass                         = vk::SubpassDescription();
        sub_pass.pipelineBindPoint       = vk::PipelineBindPoint::eGraphics;
        sub_pass.inputAttachmentCount    = 0;
        sub_pass.pInputAttachments       = nullptr;
        sub_pass.colorAttachmentCount    = attachment_references.size() - 1;
        sub_pass.pColorAttachments       = attachment_references.data();
        sub_pass.pResolveAttachments     = nullptr;
        sub_pass.pDepthStencilAttachment = &attachment_references.back();
    }
    vector<vk::SubpassDependency> dependencies(subpass_num);
    uint current_dependency = 0;
    for(auto& dependency: dependencies)
    {
        // Set the dependency of this subpass
        uint32_t prev = current_dependency == 0 ? (uint32_t)VK_SUBPASS_EXTERNAL
                                                : current_dependency - 1;
        uint32_t next = current_dependency;
        current_dependency++;

        // Execution and memory dependencies between subpasses
        dependency               = vk::SubpassDependency();
        dependency.srcSubpass    = prev;
        dependency.dstSubpass    = next;
        dependency.srcStageMask  =
            vk::PipelineStageFlagBits::eColorAttachmentOutput
            | vk::PipelineStageFlagBits::eEarlyFragmentTests
            | vk::PipelineStageFlagBits::eLateFragmentTests;
        dependency.dstStageMask  =
            vk::PipelineStageFlagBits::eColorAttachmentOutput
            | vk::PipelineStageFlagBits::eEarlyFragmentTests
            | vk::PipelineStageFlagBits::eLateFragmentTests;
        dependency.srcAccessMask =
            vk::AccessFlagBits::eDepthStencilAttachmentWrite |
            vk::AccessFlagBits::eDepthStencilAttachmentRead;
        dependency.dstAccessMask =
            vk::AccessFlagBits::eColorAttachmentRead |
            vk::AccessFlagBits::eColorAttachmentWrite |
            vk::AccessFlagBits::eDepthStencilAttachmentRead |
            vk::AccessFlagBits::eDepthStencilAttachmentWrite;
    }

    vk::RenderPassCreateInfo render_pass_info(
        {},
        attachment_descriptions.size(),
        attachment_descriptions.data(),
        subpasses.size(),
        subpasses.data(),
        dependencies.size(),
        dependencies.data());

    auto [result, render_pass] = device.createRenderPassUnique(render_pass_info);
    if(result != vk::Result::eSuccess)
        Log::RecordLogError("Failed to create render pass!");

    return move(render_pass);
}

I have read and reread the example and I have tried to make my code as identical to it as possible but alas, the issue still happens. The way I created the working image is by having both renders occur under the same render pass, but I don't want to do it that way.


Solution

  • The problem was being caused because when i created the attachment descriptors, I set the storeOp and loadOp enums of the renderpass to eDontCare so the depth buffer was UB for me.

    As suggested in the comments by @solidpixel. The solution is to make the first eStore and the second 'eLoad'.