00 Prologue: how a GPU paints
Two nested loops in C, one pass on the GPU
The same shade() function: the CPU computes one cell at a time; the GPU calls it for every cell at once.
UV424
4120
Exercise
Drag the underlined numbers left and rightDrag numbers, or edit the code
for (int y = 0; y < ; y++) for (int x = 0; x < ; x++) image[y][x] = shade(x, y);// the i-th call
i =/ 144
void main() { // no loop here: // main runs once per pixel gl_FragColor = shade(vUv);}
Calledtimes at once
This cell(6, 6) → RGB (0.54, 0.54, 0.25)Key differencemain knows only its own position; it cannot see what its neighbours computedA real canvas has hundreds of thousands to millions of pixels; a GPU has thousands of small cores running at once.
JS side
gl.uniform1f(uTime, 0.00)In C you write two nested for loops; on the GPU you write only the loop body, and the GPU calls it for every pixel at once.
Try it
- Switch to “CPU · one by one” and watch the cells fill in
- Switch to “GPU · all at once”: every cell appears together
- Move the pointer and read one cell’s input and output