storage
Function
A storage declaration describes a host-bound buffer that shader invocations can index.
Compare examples
A read returns the selected element. A write changes only the selected location and leaves the other elements unchanged.
Model assumptionsdeclare const xs: storage<array<f32>, "read_write">;
Example 1host: xs = [1, 2, 3]; shader: xs[1]
- Result
2
Example 2host: xs = [1, 2, 3]; shader: xs[1] = 7
- Result
[1, 7, 3]
Example 3host: xs = [1, 2, 3]; shader: xs[0] = 0
- Result
[0, 2, 3]
The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.
When to use it
Use it for processing large arrays and writing compute results.
Inputs and limits
Read-write access must be explicit. The declaration allocates nothing; the host supplies the buffer. Shared writes need ownership, atomics, or suitable synchronization.
For shader input rules and precision, consult WGSL and GLSL ES 3.00.
Related functions
The signature, the sentence and any message are the compiler's own text at commit af54ddad5.
Declares a storage binding of type T; use declare const name: storage<T> for read-only or declare const name: storage<T, "read_write"> for read-write access.
Syntax
declare function storage<T, A extends StorageBufferAccess = 'read_write'>(slot?: number | { group?: number; binding?: number }, binding?: number): A extends 'read_write' ? T : ReadView<T>A number in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.
type f32 = number & { readonly [f32Tag]?: true }
A number you write as a type is inferred from its uses, and f32, i32 and u32 fix a width; the TypeScript constructs page carries that row with the compiler's emit.
A signature may name a type the ambient file declares for its own use. These are those declarations.
StorageBufferAccesstype StorageBufferAccess = 'read' | 'read_write'ReadViewtype ReadView<T> = T extends number | boolean ? T : T extends (...args: never[]) => unknown ? T : { readonly [K in keyof T]: K extends string | number ? ReadView<T[K]> : T[K] }
Parameters
-
slotoptionalnumber | { group?: number; binding?: number } -
bindingoptionalnumber
Return value
A extends 'read_write' ? T : ReadView<T>
Description
The access mode is part of the type, as it is in WGSL (var<storage, read_write>); the declaration is always declare const.
Examples
path/double-array/double-array.shade.tspath/image-blur/image-blur.shade.tspath/parallel-sum/parallel-sum.shade.tspath/histogram/histogram.shade.tsgallery/array-length/array-length.shade.tsgallery/atomic-histogram/atomic-histogram.shade.tsgallery/compute-reduction-twin/compute-reduction-twin.shade.tsgallery/compute-sync/compute-sync.shade.tsgallery/gpu-console/gpu-console.shade.tsgallery/particle-step/particle-step.shade.tsgallery/subgroup-reduce/subgroup-reduce.shade.tsgallery/workgroup-override/workgroup-override.shade.tsgallery/workgroup-reduce/workgroup-reduce.shade.tsgallery/workgroup-scratch/workgroup-scratch.shade.tsgallery/workgroup-tile-2d/workgroup-tile-2d.shade.ts