Valentin Milea
Valentin Milea

Reputation: 3323

During which pipeline stage is blending performed?

The Vulkan spec states:

VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT specifies the stage of the pipeline after blending where the final color values are output from the pipeline.

This seems to imply some undefined stage between fragment-shader and color-attachment-output where blending takes place.

But let's say after writing to an image I want to use it as color attachment, and add a memory dependency with srcStageMask=VK_PIPELINE_STAGE_TRANSFER_BIT, srcAccessMask=VK_ACCESS_TRANSFER_WRITE_BIT, dstStageMask=VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, dstAccessMask=VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT. If blending took place before color-attachment-output stage, it could read data that's not yet visible.

So what does the spec actually mean in this case?

Upvotes: 2

Views: 480

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473447

It's important to remember several facts:

  1. You can only do blending within a render pass.
  2. An image used as an attachment during a render pass cannot be transferred to.

Given these facts, a render pass has to have begun between the transfer to the image and the attempt to blend with that image. And note that your blending operation is relying on the data in the image to be what it was when the render pass began. That means your loadOp for that attachment needs to load the image, not clear it.

And in order for the render pass begin to load the image... it must synchronize with prior modifications to that image. And the specification does spell out which stage actually performs the load operation and how all of those things work:

The load operation for each sample in an attachment happens-before any recorded command which accesses the sample in the first subpass where the attachment is used. Load operations for attachments with a depth/stencil format execute in the VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT pipeline stage. Load operations for attachments with a color format execute in the VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT pipeline stage.

So you don't need to synchronize the transfer with the blend operation; you need to synchronize the transfer with the render pass. And the stage for that is COLOR_ATTACHMENT_OUTPUT.

As to the deeper point of your question (what stage does blending), the answer is that Vulkan doesn't allow it to matter. Images being used as an attachment in a render pass can only be used in very limited ways. As previously stated, you can't just perform arbitrary transfer operations to them. You can't perform arbitrary write operations to them. You can only access their data as color/depth/stencil attachments and/or as input attachments.

Synchronization between blending in different rendering commands (in the same render pass) is handled automatically. And you can't write to an image via an input attachment (hence the name). So there's no special need to make blending products visible to other operations.

Basically, blending never needs an explicit stage because of the restrictions of the render pass model and the ordering guarantees of blending and other per-sample operations.

Upvotes: 2

Related Questions