home/ atoms/ webgpu-uniforms

A WebGPU uniform is a shader global held constant for every invocation of one draw call

A uniform is like a global variable for a shader: you set its value before executing the shader and every invocation in that draw sees the same value, then you can set a different value before the next draw. This contrasts with per-vertex data (from vertex buffers) which changes each invocation. Uniforms are how you feed a shader whole-frame parameters — time, resolution, a transform matrix, a colour — for audio-reactive or generative visuals. You supply them by creating a GPU buffer with UNIFORM usage, referencing it from a bind group, calling setBindGroup before drawing, and reading it in WGSL via a var binding.

Examples

Pass an audio level and elapsed time as a uniform struct each frame; the fragment shader reads uniforms.time and uniforms.level to drive colour and motion identically for every pixel of that frame, then the CPU updates the buffer next frame.

Assessment

Explain how a uniform differs from per-vertex data, and outline the four steps needed to get a per-frame ‘time’ value from JavaScript into a WGSL fragment shader.

“Uniforms are kind of like global variables for your shader. You can set their values before you execute the shader and they'll have those values for every iteration of the shader.”