wave_broadcast
Broadcasts a value from one lane to all lanes in the wave.
Test wave_broadcast in a live shader
Open the playground, start from a visual preset, and wire wave_broadcast into the fragment stage to see how it behaves with real values.
Open PlaygroundLive Demo Pending
The wave_broadcast function reads a value from a specific lane and returns it to all lanes in the wave.
Signature
wave_broadcast :: (T x, int lane) -> T {...}
Where T can be float, float2, float3, float4, int, or uint.
Parameters
| Parameter | Type | Description |
|---|---|---|
x | T | Value to broadcast |
lane | int | Lane index to broadcast from (must be uniform) |
Return Value
Returns the value of x from the specified lane to all lanes.
Example
pipeline SharedData {
pass "Main" {
compute "Main" [64, 1, 1] {
uint localIdx = input.local_index;
// Each lane computes a value
float value = computeValue(localIdx);
// Lane 0 has special "leader" data
float leaderValue = wave_broadcast(value, 0);
// All lanes now have lane 0's value
float normalized = value / leaderValue;
float _ = normalized;
}
}
}Common Use Cases
Leader Lane Pattern
// First lane reads shared data, broadcasts to others
float sharedValue = (input.local_index == 0u) ? loadSharedData() : 0.0;
float broadcast = wave_broadcast(sharedValue, 0);Matrix Row Distribution
// Each lane computes one element, broadcast to build row
float element = computeElement(input.local_index);
float row[4];
for (int i = 0; i < 4; i++) {
row[i] = wave_broadcast(element, i);
}Reference Value
// Use specific lane's value as reference
float myValue = computeValue(input.local_index);
float referenceValue = wave_broadcast(myValue, referenceLaneId);
float diff = myValue - referenceValue;Pivot Selection
// Broadcast pivot for sorting/partitioning
float pivotValue = wave_broadcast(values[input.local_index], pivotLane);
bool lessThanPivot = values[input.local_index] < pivotValue;Coordinate Sharing
// Share computed position from one lane
float3 position = computePosition(input.local_index);
float3 referencePos = wave_broadcast(position, 0);
float dist = distance(position, referencePos);Uniform Lane Index
The lane parameter must be uniform across all lanes (same value in all lanes). Non-uniform lane indices result in undefined behavior.
Dynamic Lane Selection
For dynamic lane selection based on conditions, use wave_read_first instead, which reads from the first active lane.
Compiled Output
When compiled to GLSL:
// Requires GL_KHR_shader_subgroup_ballot
subgroupBroadcast(x, lane)
When compiled to HLSL:
WaveReadLaneAt(x, lane)
When compiled to SPIR-V:
Uses OpGroupNonUniformBroadcast instruction.
See Also
- wave_read_first - Read from first active lane
- wave_all - Check if all lanes are true