numWorkgroups
In a compute shader, provides the global task scale dispatched from the CPU, i.e., the total number of workgroups in the x, y, and z dimensions of the entire compute grid.
Core Advantages
Its core advantage is decoupling the shader algorithm from the specific data size. Developers don't need to hardcode the task size in the shader; the same algorithm can flexibly handle datasets of different sizes simply by passing different workgroup counts to the compute() call on the CPU side.
Common Uses
In image processing, used to determine the boundaries of the total pixel processing range.
In particle system simulations, used to define the total number of parallel computation batches, thus understanding the scope of the entire simulation task.
In parallel data algorithms (like sorting or reduction), used to define the size of the entire problem space, ensuring each thread is aware of the total scale of the data structure.
How to adjust
This node is read-only within the shader and cannot be adjusted directly. Its value is determined by the arguments passed to the `compute(x, y, z)` function on the CPU side. For example, changing the call from `compute(shader, 16, 16)` to `compute(shader, 32, 16)` will change the value of `numWorkgroups.x` from 16 to 32 inside the shader, effectively doubling the computation range on the X-axis.
Code Examples
1// Calculate the total number of invocations in the grid
2const totalInvocations = numWorkgroups.mul(workgroupSize);
3
4// Calculate the normalized coordinate (like UV) for the current thread
5const uv = globalInvocationID.xy.div(totalInvocations.xy.sub(1));