float3 pos;
pos.z = 1.0f; // is legal
pos.w = 1.0f; // is illegal
• Accessing the same component twice on the left-hand side is ambiguous; for instance,
// illegal - 'x' used twice
pos.xx = float2(3.0f, 4.0f);
// illegal - mismatch between float2 and float4
pos.xy = float4(1.0f, 2.0f, 3.0f, 4.0f);
• The
.rgba
and
.xyzw
attributes cannot be intermixed in a single access; for instance,
float4 pos = float4(1.0f, 2.0f, 3.0f, 4.0f);
pos.x = 1.0f;
// OK
pos.g = 2.0f;
// OK
pos.xg = float2(3.0f, 4.0f); // illegal - mixed attributes used
float3 coord = pos.ryz; // illegal - mixed attributes used
• A pointer or reference to a vector with swizzles; for instance
float4 pos = float4(1.0f, 2.0f, 3.0f, 4.0f);
my_func(&pos.xy); // illegal
The
sizeof
operator on a vector type returns the size of the vector, which is given as the
number of components * size of each component. For example,
sizeof(float4)
returns 16
and
sizeof(half4)
returns 8.
2.2.2
Vector Constructors
Constructors can be used to create vectors from a set of scalars or vectors. When a vector is
initialized, its parameter signature determines how it is constructed. For instance, if the vector is
initialized with only a single scalar parameter, all components of the constructed vector are set
to that scalar value.
If a vector is constructed from multiple scalars, one or more vectors, or a mixture of these, the
vector's components are constructed in order from the components of the arguments. The
arguments are consumed from left to right. Each argument has all its components consumed, in
order, before any components from the next argument are consumed.
This is a complete list of constructors that are available for
float4
:
2017-9-12 | Copyright © 2017 Apple Inc. All Rights Reserved.
Page of
16
174
float4(float x);
float4(float x, float y, float z, float w);
float4(float2 a, float2 b);
float4(float2 a, float b, float c);
float4(float a, float b, float2 c);
float4(float a, float2 b, float c);
float4(float3 a, float b);
float4(float a, float3 b);
float4(float4 x);
This is a complete list of constructors that are available for
float3
:
float3(float x);
float3(float x, float y, float z);
float3(float a, float2 b);
float3(float2 a, float b);
float3(float3 x);
This is a complete list of constructors that are available for
float2
:
float2(float x);
float2(float x, float y);
float2(float2 x);
The following examples illustrate uses of the constructors:
float x = 1.0f, y = 2.0f, z = 3.0f, w = 4.0f;
float4 a = float4(0.0f);
float4 b = float4(x, y, z, w);
float2 c = float2(5.0f, 6.0f);
float2 a = float2(x, y);
float2 b = float2(z, w);
float4 x = float4(a.xy, b.xy);
Under-initializing a vector constructor is a compile-time error.
2017-9-12 | Copyright © 2017 Apple Inc. All Rights Reserved.
Page of
17
174
2.2.3
Packed Vector Types
The vector data types described in section 2.2 are aligned to the size of the vector. Developers
can also require their vector data to be tightly packed; for example, a vertex struct that may
contain position, normal, tangent vectors and texture coordinates tightly packed and passed as
a buffer to a vertex function.
The packed vector type names supported are:
packed_charn
,
packed_shortn
,
packed_intn
,
packed_ucharn
,
packed_ushortn
,
packed_uintn
,
packed_halfn
and
packed_floatn
n
is 2, 3, or 4 representing a 2-, 3- or 4- component vector type, respectively. (The
packed_booln
vector type names are reserved.)
Table 4 lists the size and alignment of the packed vector data types.
Table 4 Size and Alignment of Packed Vector Data Types
Type
Size (in bytes)
Alignment (in bytes)
packed_char2,
packed_uchar2
2
1
packed_char3,
packed_uchar3
3
1
packed_char4,
packed_uchar4
4
1
packed_short2,
packed_ushort2
4
2
packed_short3,
packed_ushort3
6
2
packed_short4,
packed_ushort4
8
2
packed_int2,
packed_uint2
8
4
packed_int3,
packed_uint3
12
4
packed_int4,
packed_uint4
16
4
packed_half2
4
2
packed_half3
6
2
packed_half4
8
2
2017-9-12 | Copyright © 2017 Apple Inc. All Rights Reserved.
Page of
18
174
Packed vector data types are typically used as a data storage format. Loads and stores from a
packed vector data type to an aligned vector data type and vice-versa, copy constructor and
assignment operator are supported. The arithmetic, logical and relational operators are also
supported for packed vector data types.
Examples:
device float4 *buffer;
device packed_float4 *packed_buffer;
int i;
packed_float4 f ( buffer[i] );
pack_buffer[i] = buffer[i];
// operator to convert from packed_float4 to float4.
buffer[i] = float4( packed_buffer[i] );
Components of a packed vector data type can be accessed with an array index. However,
components of a packed vector data type cannot be accessed with the
.xyzw
or
.rgba
selection syntax.
Example:
packed_float4 f;
f[0] = 1.0f; // OK
f.x = 1.0f; // Illegal - compilation error
2.3 Matrix Data Types
Metal supports a subset of the matrix data types implemented by the system math library.
The matrix type names supported are:
halfnxm
and
floatnxm
packed_float2
8
4
packed_float3
12
4
packed_float4
16
4
Type
Size (in bytes)
Alignment (in bytes)
2017-9-12 | Copyright © 2017 Apple Inc. All Rights Reserved.
Page of
19
174
Dostları ilə paylaş: |