1

I made a render pass using Vulkan which contains multiple subpasses. We can assume it has 2 subpasses, subpass A and B. In subpass A, I render to an image I want to use in subpass B as a combined image sampler (i. e. NOT as an input attachment). My question is how should I do the image transition from VK_IMAGE_LAYOUT_COLOR_ATTACHMENT to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL?

I think the subpass dependency mechanism is not good by itself, because I don't use the image in subpass B as an attachment (I guess it doesn't make sense to use it either as input, color, or preserve attachment.) I wanted to use a simple image barrier with a self-dependency, and I was surprised when the validation layer told me that if an image barrier is used inside a render pass, the new and old layout fields should be equal!

This image layout transition pattern seems kind of natural to me, but I haven't found any solution yet. Maybe I misunderstand the concept of subpasses and render passes and I should use different render passes for A and B? (Although in that case I would have to process my G-buffer in two render pass, and I wouldn't be able to use the "input attachment mechanism" of Vulkan.)

2 Answers 2

3

Render pass (and subpasses) are designed for a situation where subpass A writes the same (x, y, layer) pixel as the subpass B will later read. It is not intended for random access from the image.

As you noticed, all the kinds of attachment only access the images this way. Then there is pPreserveAttachments. If you do include you image there, you are not allowed to touch it; if you don't, then the image will become undefined. It is possibly better to think of attachments as something that is not stored anywhere (i.e. exists only in the cache memories). You would have to copy/store the attachment out to non-attachment image to make it usable this way.

To quote from the Vulkan specification:

Image subresources used as attachments must not be accessed in any other way for the duration of a render pass instance.

and your image is an attachment. You are using it as color attachment in subpass A (, so it has to be).

That being said, you probably need two separate render passes (or make use of the input attachment feature if possible for your kind of problem, as @AntoineMorrier suggests). Especially if no other of your images have optimizable dependency as described above.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. As I said, I cannot solve this problem with input attachments (although they are very useful and I use them a lot in my renderer), so I will redesign my passes a little bit.
0

When you are creating your renderpass and subpasses, you must use this structure :

typedef struct VkAttachmentReference {
    uint32_t         attachment;
    VkImageLayout    layout;
} VkAttachmentReference;

Globally, this structure say that at the beginning of your subpass, your image will be in the layout specified.

So, let's say you want to use the layout COLOR_ATTACHMENT in subpass 0, and INPUT_ATTACHMENT (prefer it than SHADER_READ) in subpass 1, you just have to specify the layout for each subpass description and you have to declare one subpass dependency between the subpass 0 and subpass 1.

1 Comment

If we want to read from an image that is a color/depth attachment in the previous subpass of the same render pass, we can do it only through an input attachment. So it's not that we can prefer INPUT_ATTACHMENT layout over other layouts. We must use it. But apart from that, I agree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.