How do vec attributes/accessors work in wgsl

226 Views Asked by At

Hi this is a simple question.

I'm trying to read and understand a shader function which makes use of vec3<f32> variable types.

I don't understand what is that .zzzz key for:

  var myvar: vec3<f32> = vec3<f32>(1.3, 3.3, 3.3);
  myvar.zzzz; // ??
  myvar.xy; // ??

I can only understand myvar.x, myvar.y, myvar.z, but what happens when you combine or repeat those keys?

I can't yet find it in the official documentation unfortunately.

Thanks

2

There are 2 best solutions below

0
On BEST ANSWER

This is a swizzle, also called convenience letterings in the spec. The spec section 7.7.1 says:

The convenience letterings can be applied in any order, including duplicating letters as needed. The provided number of letters must be between 1 and 4

There are letterings for x, y, z, w and r, g, b, a. (Note, they can not be mixed.)

So zzzz means make a vec4 initialized with the z component in each spot. The xy means make a vec2 initialized with the x and y components.

0
On

So, after looking at this example, my best guess is that myvar.zzzz returns a vec4<f32> initialised with the z component of the myvar vector.

So if a_var = vec<f32>(1.0, 2.0, 3.0), then b_var = a_var.zzzz is equal to (3.0, 3.0, 3.0, 3.0)