Intrinsics1 min read

barrier

Workgroup execution barrier that also makes shared-memory writes visible.

Reading Time
1 min
Word Count
139
Sections
4
Try It Live

Test barrier in a live shader

Open the playground, start from a visual preset, and wire barrier into the fragment stage to see how it behaves with real values.

Open Playground

Live Demo Pending

This intrinsic does not yet have a self-contained interactive preview. Compute-only and external-resource intrinsics need a different demo path than the current fragment and 3D showcases.

barrier blocks each invocation until every invocation in the workgroup has reached it. It also orders shared memory: writes made before the barrier are visible to every invocation in the workgroup after it.

Signature

bwsl
barrier :: () -> void {...}

barrier is only valid in compute stages. Calling it in a vertex or fragment stage is a compile error.

Example

bwsl
compute "Blur" [64, 1, 1] {
shared float[64] tile;
uint i = input.local_index;
// Every invocation loads one value...
tile[i] = resources.input[input.global_id.x];
// ...and waits until the whole tile is loaded before reading neighbors.
barrier();
float left = tile[(i + 63u) % 64u];
float right = tile[(i + 1u) % 64u];
resources.output[input.global_id.x] = (left + tile[i] + right) / 3.0;
}

Uniform control flow

Every invocation in the workgroup must reach the same barrier() calls in the same order. Placing a barrier inside a branch or loop that only some invocations take is undefined behavior and can hang the GPU.

Compiled Output

TargetOutput
SPIR-VOpControlBarrier (workgroup scope, workgroup memory)
GLSLbarrier()
HLSLGroupMemoryBarrierWithGroupSync()
Metalthreadgroup_barrier(mem_flags::mem_threadgroup)

See Also