home/ atoms/ gpu-color-channel-clamping

GPU fragment shader color output channels are clamped to [0,1]; values exceeding 1 saturate and produce incorrect gradients

Color values returned from a fragment shader are clamped to the range [0, 1] per channel. If you pass raw cell coordinates (which range from 0 to grid size) directly as color components, all cells beyond the first row/column saturate at maximum brightness and appear identical. To produce a gradient across the whole grid, normalize by dividing by the grid size: cell / grid maps coordinates to [0, 1]. This clamping behavior is fundamental to RGBA color output and affects any attempt to use large numbers or negative values as colors.

Examples

// Wrong: clamps to 1 for all cells except the first row/column
return vec4f(cell, 0, 1);
// Correct: normalizes to [0,1] range across the full grid
return vec4f(cell / grid, 0, 1);

Assessment

A 32×32 grid passes cell coordinates (ranging 0–31) directly as color channels. What do cells (1,0) and (16,0) look like? How does dividing by grid fix the gradient?

“any values outside of that range are clamped to it. Your cell values, on the oth”
corpus · your-first-webgpu-app-google-codelab · chunk 14