What Does GLSL subgroupBallotFindLSB Return?

In GLSL, the subgroupBallotFindLSB function determines the lowest active invocation index recorded in a subgroup ballot bitmask. This article explains the exact return value of subgroupBallotFindLSB, details how it interprets the underlying 128-bit ballot vector, and addresses edge-case behavior when dealing with empty bitfields.

The Return Value

The subgroupBallotFindLSB function returns an unsigned integer (uint) representing the bit index of the least significant bit set to 1 within a ballot vector.

In GLSL, subgroup ballot operations represent invocation states as a 4-component unsigned integer vector (uvec4). Because each component is a 32-bit integer, the entire vector forms a continuous 128-bit bitfield:

  • value.x: Covers bits 0 to 31
  • value.y: Covers bits 32 to 63
  • value.z: Covers bits 64 to 95
  • value.w: Covers bits 96 to 127

subgroupBallotFindLSB scans this 128-bit sequence starting from bit 0 upward. The returned uint corresponds directly to the lowest invocation ID within the subgroup that has an active or true state in the provided ballot.

Return Value Range

  • Valid non-zero vector: An integer in the range [0, SubgroupSize - 1], with a theoretical maximum of 127 based on the 128-bit vector size.
  • Interpretation: If bit 0 is set (meaning invocation 0 is set), the function returns 0. If only bit 35 is set (bit 3 of value.y), it returns 35.

Behavior on Empty Ballots (Zero Vector)

According to the GLSL GL_KHR_shader_subgroup_ballot and Vulkan subgroup specifications, if all components of the input vector are zero (uvec4(0)), the result is undefined.

To avoid undefined behavior, applications should verify that the ballot is not empty using subgroupBallotBitCount(value) > 0 or by checking if any vector component is non-zero before calling subgroupBallotFindLSB.