Task 4 of 5
Threads don't have to line up in a row. Give output two numbers —
output: [8, 8] — and gpu.js launches an 8×8 grid of 64
threads. Each one now has two coordinates: this.thread.x is its column and
this.thread.y is its row, and the result comes back as an array of rows you
read as result[y][x].
To prove both coordinates are live, paint a classic: a checkerboard. A cell is
“black” or “white” depending on whether x + y is even
or odd — which is just (x + y) % 2.
(x + y) % 2 — an alternating pattern of 0s and 1s.output to a grid: [8, 8]this.thread.x and this.thread.yTwo things: output gets a second number
([width, height]), and this.thread.y starts meaning
something. Nothing else about the kernel changes.
return (this.thread.x + this.thread.y) % 2;
Neighbours differ by one in x or y, so the parity flips
checkerboard-style.
dim3-shaped blocks, WebGPU dispatches workgroups across x/y/z, and Metal's
grids are up to three-dimensional. One thread per pixel — the idea Data In, Data
Out runs with —
starts exactly here.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.