Inside a function, combine & with a let-declaration
to create a short name for something in a larger structure, as in the example.
You may have to use parentheses around the dereference, because
member access and array indexing bind more tightly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
structParticle{pos: vec3f}var<private>particles: array<Particle,10>;fnf(i: i32){letp=&particles[i];// This works.
(*p).pos=vec3f();// This left hand side is an error, because it means
// *(p.pos), and you can't do member access on a pointer.
*p.pos=vec3f();}
struct Particle {
momentum: vec3f,
pos: vec3f,
velocity: vec3f,
}
@group(0) @binding(0)
var<storage,read_write> particles: array<Particle>;
fn sequentially_update_momenta() {
let particles_ptr = &particles; // Here is a pointer
for (var i: u32; i < arrayLength(particles_ptr); i++) {
// Use & to get a pointer as a short name for something inside a big variable.
let a_particle = &particles[i];
// We could have spelled out the pointer type in full.
// let a_particle: ptr<storage,Particle,read_write> = &particles[i];
// Now use 'a_particle' as a short name.
let its_pos = (*a_particle).pos; // Parentheses are required.
let its_velocity = (*a_particle).velocity;
(*a_particle).momentum = its_pos * its_velocity;
}
}