Vireo  0.0
Vireo 3D Rendering Hardware Interface
Memory barriers

A memory barrier is a GPU/GPU synchronization mechanism. They are use inside render passes : barriers enforce execution and memory ordering within or across command lists.

They prevent hazards like read-after-write, write-after-read, etc., by ensuring visibility and availability of resource modifications and prepare the resource to be in an optimized layout for the next task.

A barrier in recorded in a command list with vireo::CommandList::barrier with the resource, the source state and the destination state. For Vulkan the barrier methods will take care of the correct pipeline stages and layouts for you (for DirectX 12 it's the driver) :

// Wait for all the data of the graphic rendering pass to be written in the color attachment
// before copying the image
commandList->barrier(
colorBuffer,

In an application the barriers states must be perfect : with DirectX 12 you can't use an source state different from the current state of the resource. This can lead to a lot of barriers :

commandList->barrier(
depthPrepass.getDepthBuffer(frameIndex),
depthPrepass.isWithStencil() ?
auto colorBuffer = postProcessing.getColorBuffer(frameIndex);
if (colorBuffer == nullptr) {
colorBuffer = frame.colorBuffer;
}
commandList->barrier(
swapChain,
commandList->barrier(
colorBuffer,
commandList->copy(colorBuffer, swapChain);
commandList->barrier(
colorBuffer,
commandList->barrier(
swapChain,
commandList->end();