RI::cmdBarrier¶
What it does¶
RI::cmdBarrier(cmd, before, after) ensures that every write made by the before stages in
earlier commands finishes and becomes visible to every access by the after stages in
later commands.
Each stage mask can combine multiple Stage values with |.
Use it between a command that writes memory and a later command that reads or writes the same memory—for example, between a copy and a dispatch that reads the copied data, between a dispatch and a copy of its output, between two dispatches where the second reads what the first wrote, or between a rendering section and a shader that samples its result. Barriers explains why barriers exist and why RI uses coarse stage-to-stage barriers.
Stage |
Covers |
|---|---|
eCopy |
copies, small writes (cmdWriteSmallData), and clears |
eCompute |
compute shaders |
eIndirect |
reading indirect arguments (cmdExecuteIndirect) |
eVertex |
vertex, hull, domain, geometry, task, and mesh shaders |
ePixel |
pixel shaders |
eRenderTarget |
color attachment reads and writes |
eDepthStencil |
depth and stencil tests and writes |
eAccelerationStructure |
acceleration structure builds and updates (requires DeviceLimits::rayTracing) |
eAll |
every stage |
A barrier names no resource and changes no image layout: textures stay in their resting layout between operations (Image layouts are RI's business).
Signature¶
From the header: Every write made by before stages becomes visible to every access of after stages; names no resource (P36).
// <RI/RI.hpp>, namespace RI
enum class Stage : uint32 {
eNone = 0,
eCopy = rvl::eBit(0),
eCompute = rvl::eBit(1),
eIndirect = rvl::eBit(2), // reading indirect arguments (P48)
eVertex = rvl::eBit(3), // every shader before rasterization: vertex, hull, domain, geometry, task, mesh
ePixel = rvl::eBit(4), // pixel shading
eRenderTarget = rvl::eBit(5), // color attachment reads and writes
eDepthStencil = rvl::eBit(6), // depth/stencil tests and writes
eAccelerationStructure = rvl::eBit(7), // acceleration structure builds (P49)
eAll = rvl::eBit(31),
};
From the header: Pipeline stages for coarse barriers (P36): a barrier from before to after makes every write of the before stages visible to every access of the after stages. Graphics stages come with graphics pipelines.
| Value | Meaning |
|---|---|
eNone |
|
eCopy |
|
eCompute |
|
eIndirect |
reading indirect arguments (P48) |
eVertex |
every shader before rasterization: vertex, hull, domain, geometry, task, mesh |
ePixel |
pixel shading |
eRenderTarget |
color attachment reads and writes |
eDepthStencil |
depth/stencil tests and writes |
eAccelerationStructure |
acceleration structure builds (P49) |
eAll |
Example¶
RI::cmdBarrier(cmd, RI::Stage::eCopy, RI::Stage::eCompute); // the copy's writes, then the shader's reads
RI::cmdBarrier(cmd, RI::Stage::eCompute, RI::Stage::eCopy); // the shader's writes, then the copy's reads
After a rendering section, before shaders sample what it wrote:
// The section's writes, then shaders that sample color and depth.
RI::cmdBarrier(cmd, RI::Stage::eRenderTarget | RI::Stage::eDepthStencil, RI::Stage::eCompute | RI::Stage::ePixel);
The same in native code¶
// A global memory barrier: every write of `srcStages` becomes visible to
// every access of `dstStages`. Buffers have no layouts, so this is all they need.
const auto barrier = [&](VkPipelineStageFlags2 srcStages, VkPipelineStageFlags2 dstStages,
VkAccessFlags2 dstAccess) {
const VkMemoryBarrier2 memoryBarrier{.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2,
.srcStageMask = srcStages,
.srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT,
.dstStageMask = dstStages,
.dstAccessMask = dstAccess};
const VkDependencyInfo dependency{.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
.memoryBarrierCount = 1,
.pMemoryBarriers = &memoryBarrier};
vkCmdPipelineBarrier2(cmd, &dependency);
};
barrier(VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT);
What to watch for: a global VkMemoryBarrier2 (synchronization2) needs no buffer or
image. Stage and access masks both matter: the stages order the work, the accesses make
the memory visible.
// A global barrier: no resource named. Buffers have no layouts, so this is all they need.
const auto barrier = [&](D3D12_BARRIER_SYNC syncBefore, D3D12_BARRIER_ACCESS accessBefore,
D3D12_BARRIER_SYNC syncAfter, D3D12_BARRIER_ACCESS accessAfter) {
D3D12_GLOBAL_BARRIER global{.SyncBefore = syncBefore,
.SyncAfter = syncAfter,
.AccessBefore = accessBefore,
.AccessAfter = accessAfter};
const D3D12_BARRIER_GROUP group{.Type = D3D12_BARRIER_TYPE_GLOBAL, .NumBarriers = 1, .pGlobalBarriers = &global};
cmd->Barrier(1, &group);
};
barrier(D3D12_BARRIER_SYNC_COPY, D3D12_BARRIER_ACCESS_COPY_DEST, D3D12_BARRIER_SYNC_COMPUTE_SHADING,
D3D12_BARRIER_ACCESS_SHADER_RESOURCE | D3D12_BARRIER_ACCESS_UNORDERED_ACCESS);
What to watch for: enhanced barriers (D3D12_GLOBAL_BARRIER) replace resource-state
transitions for buffers; the sync and access values must be compatible with each other.
Rules and errors¶
- Two reads never need a barrier between them.
- Do not call
cmdBarrierinside a rendering section: end the section withcmdEndRendering, place the barrier, and begin the next section. - Attachment stages (
Stage::eRenderTarget,Stage::eDepthStencil): on D3D12, global barriers cannot name attachment accesses, so these stages add nothing and a barrier with only attachment stages on either side records nothing; RI's layout switch back to rest atcmdEndRenderingalready makes attachment writes visible. Write the barrier anyway, because Vulkan uses it. Stage::eAccelerationStructurerequiresDeviceLimits::rayTracing; using it without ray tracing support reportsErrorCategory::eUnsupported.Stage::eAllwaits for and blocks every stage; it is correct, but slower than naming only the stages involved.- No barrier is needed before the CPU reads results from mapped memory:
cmdEnd(on Vulkan) and waiting for the submission'sTimelinePointmake GPU writes visible to the CPU. - A barrier also covers earlier submissions: when a command list reads what a previous
submission wrote, start it with a barrier (for example
cmdBarrier(cmd, Stage::eAll, Stage::eCompute)). Do not count on submission order alone: Vulkan does not wait between submissions unless told to.