Metal 着色语言编程指南 五

(.) 操作符访问矢量成员 

    Metal 也支持(.) 操作符来访问矢量成员, (.) 操作符后面跟随字符来表示坐标或者颜色: <Vector_data_type>.xyzw 或者 <Vector_data_type>.rgba。

   示例代码如下

int4 test = int4(0, 1, 2, 3);
int a = test.x;  //  a = 0
int b = test.y;  //  b = 1
int c = test.z;  //  c = 2
int d = test.w;  //  d = 3
int e = test.r;  //  e = 0
int f = test.g;  //  f = 1
int g = test.b;  //  g = 2
int h = test.a;  //  h = 3

  (.) 操作符也可以同时选择多个成员:

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);

应用(.)操作符来复制或者交换矢量成员:

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)


应用(.)操作符给矢量成员复制,  当矢量成员成为左值(lvalue)的时候, 用来标示其成员索引的字母(xyzw或者rgba)是不可重复的:

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);




你可能感兴趣的:(ios,metal)