紧致矢量类型 (Packed Vector Data Type)
如前文所述, 矢量类型是有其规范的对齐方式(基于性能优化的考量). 但是你也许会有需要你所定义的矢量是被紧密地包装着的, 比如, 你定义了一个顶点的数据类型, 其成员有坐标, 法线矢量, 正切矢量和矩阵坐标, 并且希望它们是被紧密地包在一起作为给Vertex 函数的输入参数.
Metal 着色语言支持下面的紧致矢量类型:
下表描述了所有紧致矢量类型的大小和对齐packed_charn, packed_shortn, packed_intn, packed_ucharn, packed_ushortn, packed_uintn, packed_halfn, 和 packed_floatn
-
紧致矢量类型通常用于存储和host 到 device 的数据传递. Metal 着色语言支持从紧致矢量到普通矢量的类型转换: 通过拷贝构造或者赋值运算, 反之亦然。 算数,逻辑和关系运算也适用于紧致矢量数据。Packed Vector Type
Alignment (in bytes)
sizeof (in bytes)
packed_char2
packed_uchar2
1
2
packed_char3
packed_uchar3
1
3
packed_char4
packed_uchar4
1
4
packed_short2
packed_ushort2
2
4
packed_short3
packed_ushort3
2
6
packed_short4
packed_ushort4
2
8
packed_int2
packed_uint2
4
8
packed_int3
packed_uint3
4
12
packed_int4
packed_uint4
4
16
packed_half2
2
4
packed_half3
2
6
packed_half4
2
8
packed_float2
4
8
packed_float3
4
12
packed_float4
4
16
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] );
- 紧致矢量数据的成员可以用索引的方式进行访问, 但是不支持 .xyzw 和 .rgba 访问方式, 这一点同普通的矢量数据不同。
packed_float4 f; f[0] = 1.0f; // OK f.x = 1.0f; // Illegal - compilation error