user14265619
user14265619

Reputation:

How to assign components to a swizzled vector in WGSL?

I am doing some experiments with WebGPU, programming a shader in the WGSL language. I can't figure out how to assign a 3D sub-vector to a 4D vector using swizzled indexing. What I am trying to do is:

var color: vec4<f32>;
color.rgb = vec3(0.0, 0.0, 0.0); // --> error: "cannot assign to value expression of type 'vec3<f32>'"
color.a = 1.0;

But:

var color: vec4<f32>;
color.r = 0.0;
color.g = 0.0;
color.b = 0.0;
color.a = 1.0;

gives me the expected correct result.

What is the correct syntax to achieve this very same behavior in a single statement for the rgb components?

Thanks

Upvotes: 5

Views: 838

Answers (1)

Manatee Pink
Manatee Pink

Reputation: 253

I think the answer lies in https://github.com/gpuweb/gpuweb/discussions/3478.

For an assignment, the left-hand side must be a reference. Single-component accesses return a reference, so that's why color.r = 0 works because color.r is a reference.

color.rgb however returns a value, not a reference, that's why it says you can't assign to a value expression.

can't you do color = vec4(0,0,0,1)?

Upvotes: 4

Related Questions