Search code examples
wgsl

How do vec attributes/accessors work in wgsl


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


Solution

  • 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.