Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

storage!

Declares a storage buffer binding, either read-only or read-write.

Syntax

#![allow(unused)]
fn main() {
// read-only
storage!(group(N), binding(M), NAME: Type);

// read-write
storage!(group(N), binding(M), read_write, NAME: Type);
}

What It Generates

Read-only:

@group(N) @binding(M) var<storage, read> NAME: Type;

Read-write:

@group(N) @binding(M) var<storage, read_write> NAME: Type;

Rust:

#![allow(unused)]
fn main() {
pub static NAME: Storage<Type>;
}

Access

  • get!(NAME) reads the buffer.
  • get_mut!(NAME) writes to the buffer (only valid for read_write).

Example: Compute Shader

#![allow(unused)]
fn main() {
#[wgsl]
pub mod prefix {
    use wgsl_rs::std::*;

    #[derive(Wgsl)]
    pub struct Data {
        pub value: f32,
    }

    storage!(group(0), binding(0), read_write, INPUT: Data);
    storage!(group(0), binding(1), read_write, OUTPUT: Data);

    #[compute]
    #[workgroup_size(64)]
    pub fn cs_main() {
        let mut src = get_mut!(INPUT);
        let mut dst = get_mut!(OUTPUT);
        dst.value = src.value * 2.0;
    }
}
}

Notes

  • Use arrays in Type (e.g. array<f32, N>) for large buffers.
  • read_write storage requires the binding to be created with the read_write access flag on the host side.
  • The slab_copy! helper operates on storage! bindings (see Binding Macros).