Why Are gpu.js Thread Coordinates Zero-Indexed?

In gpu.js, thread coordinates (this.thread.x, this.thread.y, this.thread.z) use zero-based indexing rather than one-based indexing to maintain native consistency with JavaScript, mirror standard GPU shading languages, and optimize memory addressing mathematics. By starting at zero, the library eliminates off-by-one translation overhead, simplifies multidimensional array flattening, and conforms to the universal conventions of high-performance parallel computing.

JavaScript Ecosystem Alignment

gpu.js is built for JavaScript and TypeScript developers. Because arrays, typed buffers, and loops in JavaScript are natively zero-indexed, using zero-indexed thread coordinates provides seamless parity with standard host code. When a kernel writes to an output array or reads from an input array, a zero-indexed coordinate directly corresponds to the source element index without requiring developers to constantly subtract 1.

Simplified Memory Stride Math

Under the hood, GPU memory buffers are contiguous, one-dimensional blocks of data. Flattening multidimensional coordinates into a linear memory address is computationally cheaper and simpler with zero-based math:

Because graphics hardware runs kernel logic across thousands of concurrent threads, eliminating the subtraction operations for every coordinate lookup reduces unnecessary instruction cycles and optimizes kernel performance.

Consistency with WebGL and GLSL

gpu.js operates by compiling JavaScript kernel functions into WebGL shaders (specifically GLSL). Graphics hardware and shader environments universally employ zero-based indices for vertex IDs, instance IDs, texture coordinates, and pixel positions. Mapping thread executions from zero aligns directly with the underlying graphics hardware pipeline, ensuring that gpu.js does not introduce artificial translation layers during code generation.

Industry-Wide GPU Computing Standards

Parallel computing frameworks such as CUDA (threadIdx, blockIdx), OpenCL (get_global_id()), and compute shaders in WebGPU start indexing work items from zero. By adopting this standard, gpu.js ensures that algorithms ported between native compute platforms and the browser behave identically, avoiding confusion and design friction across parallel programming paradigms.