v144 · Subgroup Ballot Visualizer
Subgroup Ballot Visualizer
Visualize how subgroupBallot() and subgroupElect() work across a subgroup of invocations. Click cells to toggle which invocations vote — the bitmask and elected invocation update live. The subgroup_id built-in identifies which subgroup each invocation belongs to.
subgroupBallot(vote) result
WGSL
// subgroup_id and subgroupBallot are part of the "subgroups" extension
// require("subgroups") enables both subgroup_id and the ballot operations
enable subgroups;
@compute @workgroup_size(64)
fn main(
@builtin(global_invocation_id) gid: vec3u,
@builtin(subgroup_id) sg_id: u32, // Chrome 144: which subgroup
@builtin(subgroup_invocation_id) sg_inv: u32, // position within subgroup
@builtin(subgroup_size) sg_size: u32,
) {
// Each invocation casts a vote based on some condition
let vote = (gid.x % 3u) != 0u;
// subgroupBallot returns a vec4u bitmask: bit N is set if invocation N voted
let ballot: vec4<u32> = subgroupBallot(vote);
// subgroupElect returns true for the lowest-numbered active invocation
let is_elected = subgroupElect();
// Use sg_id to coordinate work across subgroups without ID collisions
if (is_elected) {
results[sg_id] = ballot.x; // store one result per subgroup
}
}