subgroupIndex
In a compute shader, retrieves the unique index of the Subgroup to which the current Invocation belongs within its Workgroup. This is a read-only built-in variable provided by the GPU.
Core Advantages
Unlocks high-performance, subgroup-level parallel operations (like voting, shuffling, reduction) that leverage specialized hardware instructions, making them significantly faster than manual implementations using shared memory, which is key for high-performance computing.
Common Uses
Parallel Reduction: Each subgroup computes a partial result, using its index to safely write the result to a unique location in shared memory.
Optimizing Shared Memory Access: Used as an offset to stagger memory access from different subgroups, avoiding bank conflicts.
Task Specialization: Assigning different tasks to different subgroups based on their index, enabling functional differentiation.
Data Partitioning: Acts as a high-level index to partition data or tasks within a workgroup, simplifying logic.
How to adjust
Since `subgroupIndex` is a read-only built-in variable, it cannot be adjusted directly. However, you can alter shader behavior based on its value. For example, use a conditional branch like `if ( subgroupIndex.equal( 0 ) ) { ... }` to make the first subgroup in a workgroup execute completely different code logic, thus achieving task specialization.
Code Examples
1// Get the index of the subgroup to which the current invocation belongs.
2const sIndex = TSL.subgroupIndex;
3
4// Write the subgroup index to an output buffer for debugging or further processing.
5outputBuffer.element( globalInvocationIndex.x ).assign( sIndex );