ptr!
Declares a WGSL pointer parameter. Used in function signatures where the function needs to read or write a variable in a specific address space.
Syntax
#![allow(unused)] fn main() { fn name(p: ptr!(address_space, Type)) { ... } }
address_space is one of function, private, or workgroup.
What It Generates
Rust:
#![allow(unused)] fn main() { fn name(p: &mut Type) { ... } }
WGSL:
fn name(p: ptr<address_space, Type>) { ... }
Dereference
Read or write through the pointer with *p:
#![allow(unused)] fn main() { pub fn increment(p: ptr!(function, f32)) { *p += 1.0; } }
fn increment(p: ptr<function, f32>) {
*p += 1.0;
}
Example: Swap
#![allow(unused)] fn main() { #[wgsl] pub mod utils { use wgsl_rs::std::*; pub fn swap(a: ptr!(function, f32), b: ptr!(function, f32)) { let t = *a; *a = *b; *b = t; } pub fn sort_pair(mut x: f32, mut y: f32) -> Vec2f { if x > y { swap(&mut x, &mut y); } vec2f(x, y) } } }
Notes
- Use
&mutat the call site in Rust; the macro translates this to a WGSL pointer of the declared address space. functionis the most common address space for local variables. Useworkgroupfor pointers toworkgroup!variables andprivatefor module-private variables.