computeKernel
Defines a compute shader kernel for performing general-purpose, massively parallel computations on the GPU (GPGPU), rather than for graphics rendering.
Core Advantages
It unlocks the GPU's massive parallel processing power for non-rendering tasks like physics simulations or image processing, achieving significant performance gains over CPU-based calculations.
Common Uses
Large-scale particle system simulation
Real-time image processing and filters
Procedural geometry generation and modification
Fluid and physics field simulation
How to adjust
Primarily by adjusting the `workgroupSize` parameter in its constructor. Changing it (e.g., from `[16]` to `[64]`) doesn't alter the result but significantly impacts performance by optimizing GPU thread scheduling. For 2D data, using a 2D size like `[8, 8]` can improve code clarity and cache efficiency.
Code Examples
1// Define logic: write the squared thread ID to the corresponding buffer element
2const computeLogic = assign(
3 element( myBufferNode, global.x ),
4 global.x.mul( global.x )
5);
6
7// Create the compute kernel with a specified workgroup size
8const myComputeKernel = computeKernel( computeLogic, [ 64 ] );