textureBarrier
In a compute shader, resolves race conditions that can occur when writing to and then reading from the same texture. It creates a synchronization point, forcing all invocations to wait until all prior texture write operations are complete, ensuring subsequent reads access consistent and complete data.
Core Advantages
By ensuring data consistency for texture reads and writes, it enables the implementation of complex, multi-stage algorithms within a single dispatch, avoiding the overhead of multiple passes and significantly improving performance.
Common Uses
Iterative image processing (e.g., Gaussian blur)
GPU physics simulation (e.g., fluids, particles)
Parallel reduction algorithms
How to adjust
This node has no tunable parameters; its effect is entirely determined by its position in the code. It must be placed precisely after a sequence of write operations and before a sequence of read operations that depend on those writes. Omitting this node will lead to race conditions and unpredictable, incorrect results.
Code Examples
1// Stage 1: Read from texture A, process, and write to texture B
2const valueFromA = textureA.load( dispatchThreadID.xy );
3const processedValue = valueFromA.mul( 0.5 );
4textureB.store( dispatchThreadID.xy, processedValue );
5
6// -- Synchronization Point --
7// Insert a texture barrier to ensure all writes to textureB are complete
8textureBarrier();
9
10// Stage 2: Safely read the new data from texture B for calculation
11const valueFromB = textureB.load( dispatchThreadID.xy );