home/ atoms/ wgsl-storage-read-write-access

WGSL storage buffers are read-only by default with var<storage>; read-write access requires var<storage, read_write> and is only available in compute shaders

In WGSL, storage buffer access mode is declared in the variable type: var<storage> is read-only (can be used in vertex, fragment, and compute shaders), while var<storage, read_write> allows both reading and writing and is only available in compute shaders. There is no write-only storage mode in WebGPU. This distinction maps to the bind group layout entry types: 'read-only-storage' for read-only storage and 'storage' for read-write. The output buffer in a ping-pong compute simulation must use read_write; the input buffer can use read-only for safety.

Examples

@group(0) @binding(1) var<storage> cellStateIn: array<u32>;          // read-only
@group(0) @binding(2) var<storage, read_write> cellStateOut: array<u32>; // read-write

Bind group layout: { type: 'read-only-storage' } vs { type: 'storage' }

Assessment

Can a vertex shader use var<storage, read_write>? What bind group layout entry type corresponds to each WGSL storage access mode?

“first storage buffer is declared with `var<storage>`, which makes it read-only,”
corpus · your-first-webgpu-app-google-codelab · chunk 20