int h = test.a; // h = 3
The component selection syntax allows multiple components to be selected.
float4 c;
c.xyzw = float4(1.0f, 2.0f, 3.0f, 4.0f);
c.z = 1.0f;
c.xy = float2(3.0f, 4.0f);
c.xyz = float3(3.0f, 4.0f, 5.0f);
The component selection syntax also allows components to be permuted or replicated.
float4 pos = float4(1.0f, 2.0f, 3.0f, 4.0f);
float4 swiz = pos.wzyx; // swiz = (4.0f, 3.0f, 2.0f, 1.0f)
float4 dup = pos.xxyy; // dup = (1.0f, 1.0f, 2.0f, 2.0f)
The component group notation can occur on the left hand side of an expression. To form the
lvalue, swizzling may be applied. The resulting lvalue may be either the scalar or vector type,
depending on number of components specified. Each component must be a supported scalar or
vector type. The resulting lvalue of vector type must not contain duplicate components.
float4 pos = float4(1.0f, 2.0f, 3.0f, 4.0f);
// pos = (5.0, 2.0, 3.0, 6.0)
pos.xw = float2(5.0f, 6.0f);
// pos = (8.0, 2.0, 3.0, 7.0)
pos.wx = float2(7.0f, 8.0f);
// pos = (3.0, 5.0, 9.0, 7.0)
pos.xyz = float3(3.0f, 5.0f, 9.0f);
The following methods of vector component access are not permitted and result in a compile-
time error:
• Accessing components beyond those declared for the vector type is an error. 2-
component vector data types can only access
.xy
or
.rg
elements. 3-component vector
data types can only access
.xyz
or
.rgb
elements. For instance:
float2 pos;
pos.x = 1.0f; //
is legal; so is y
pos.z = 1.0f; //
is illegal; so is w
2017-9-12 | Copyright © 2017 Apple Inc. All Rights Reserved.
Page of
15
174