localId
In a compute shader, provides a unique, 3D integer coordinate starting from (0,0,0) for each invocation (thread) within its workgroup, solving the core problem of 'who am I?' in parallel computing.
Core Advantages
As the cornerstone of data parallelism, it allows hundreds or thousands of threads to distinguish themselves and be assigned to process different parts of a large dataset (like textures or buffers), which is key to enabling General-Purpose GPU (GPGPU) computing.
Common Uses
As a basis for calculating the global coordinate (globalId) in procedural texture generation.
As an array index for accessing and updating specific elements in particle systems or large buffers.
In image processing (e.g., convolution), as a local offset for collaboratively accessing neighboring data in high-speed shared memory.
As a 3D local coordinate for reading or modifying data within a voxel grid.
How to adjust
localId is a read-only built-in variable provided by the GPU; its value cannot be adjusted directly. However, its effect can be understood by visualizing its value: normalize localId by dividing it by the workgroup size and output the result as a color. This will create a color gradient from black to bright within each workgroup, clearly demonstrating the local coordinate system.
Code Examples
1const normalizedId = localId.xy.div( workgroupSize );
2const color = vec3( normalizedId.x, normalizedId.y, 0 ); // Visualize local coordinates as color