vulkan barrier initial state confusion

Started by
1 comment, last by j_uk_dev 6 years, 11 months ago

hi!

vkCmdPipelineBarrier needs srcStageMask and srcAccessMask, what should be the initial value for these parameters right after a resource is created? In DirectX 12, you can specify initial state for resources, what about vulkan?

Advertisement

you can also provide an initial state (member of VkImageCreateInfo) but it has to be VK_IMAGE_LAYOUT_UNDEFINED or VK_IMAGE_LAYOUT_PREINITIALIZED.

The pipeline stage doesn't have anything to do with resource initial state. Pipeline barrier allows to change for example image layout but it's up to you to decide how and when you are going to use the resource. For example, a newly created image with bound memory must make sure that all the writes and reads already happened before you try to change the layout. The safest place ( but usually least optimal ) is going to be a top of the pipeline for both - source and destination stages, as you may want to use this resource for example as sampled image. But again it depends on when and how you use your resource in the pipeline. To understand barriers you may think of it as "all the access operation must complete before source stage(s) to perform access operations after destination stage(s)". If we continue the example of image layout transition, usually the source stage for the new image is top of the pipeline ( regardless if the old layout is preinitialized or undefined ). The source access usually stays empty for uninitialized layout ( the data is undefined, no read and writes will occur at this point ). If your image is preinitialized you are going probably to write the data, so host write access must be complete before layout transition will happen. Those layouts are useless anywhere in the pipeline, so we use top of the pipeline as the source stage. Now depends on what you want to do next. If for example, you want to turn your image into the color attachment, then your destination stage mask will include color attachment output bit and access mask will include color attachment read and write bits. However, if you want to sample image in the shader, you need to perform transition earlier ( for example your new layout is VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL ). The stage mask should include all stages that may try to sample your image ( so mostly all shading stages ). Destination access flag would include shader read bit as that's the only operation that may happen after layout change.
I'm not sure if it's clear enough. I used image resource as the example although mind that barriers are a lot more complex in terms of what and how they synchronise. The example above is a simplification of the concept to help you understand what stages and access masks are.

This topic is closed to new replies.

Advertisement