home/ atoms/ webgpu-uniform-buffer

A uniform buffer holds per-draw-call constants visible to all shader invocations, unlike per-vertex attributes or writable storage buffers

A uniform is a buffer value that is constant across all invocations of a shader in a single draw call — every vertex and every fragment sees the same value. Uniforms are ideal for per-frame constants like grid size, elapsed time, or transform matrices. In WebGPU, stored in GPUBuffers with GPUBufferUsage.UNIFORM; in WGSL, declared with var<uniform> and @group/@binding attributes. Contrast: per-vertex attributes differ per vertex; storage buffers can be large and writable by compute shaders. Uniform buffers have size limits and cannot be written by compute shaders.

Examples

const uniformBuffer = device.createBuffer({ size: uniformArray.byteLength, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });
device.queue.writeBuffer(uniformBuffer, 0, uniformArray);

WGSL: @group(0) @binding(0) var<uniform> grid: vec2f;

Assessment

Name two limitations of uniform buffers that make storage buffers preferable for simulation state. What WGSL var type keyword is used for a uniform?

“uniform is a value from a buffer that is the same for every invocation. They're”
corpus · your-first-webgpu-app-google-codelab · chunk 9