//基本数据类型.
bool a= true;
char b = 5;
int d = 15;
//向量
bool2 A=[1,0];
float4 pos = float4(1.0,2.0,3.0,4.0);
float x = pos[0];
float y = pos[1];
float4 vb;
for(int i = 0;i<4;i++)
{
vb[i] = pos[i] * 2.0f;
}
//1.支持通过向量字母来获取对应元素
int4 test = int4(0,1,2,3);
int a = test.x;
int b = test.y;
int c = test.z;
int d = test.w;
//rgba;
int e = test.r;
int f = test.g;
int g = test.b;
int h = test.a;
float4 c;
c.xyzw = float4(1.0,2.0,3.0,4.0);
c.z = 1.0f;
c.xy = float2(3.0,4.0);
c.xyz = float3(3.0,4.0,5.0);
//分量允许多个分量乱序/重复出现
float4 pos = float4(1.0,2.0,3.0,4.0);
float4 swiz = pos.wxyz;
float4 dup = pos.xxyy;
pos.xw = float2(5.0,6.0);
pos.wx = float2(7.0,8.0);
//不允许超过其维度去访问变量. xy
float2 pos;
pos.x = 1.0f;//合法
pos.z = 1.0f;//不合法
//作为一个左值,同一个分量出现大于1次都是错误
pos.xx = float2(3.0,4.0);
//不合法,使用混合限定符
pos.xy = float4(1.0,2.0,3.0,4.0);
float4 pos4 = float4(1.0,2.0,3.0,4.0);
pos.x = 1.0;
pos.y = 2.0;
//非法,rgba和xyzw 混合使用
pos.xg = float2(2.0,3.0);
//非法,原因同上
float3 coord = pos4.ryz;
float4 pos5 = float4(1.0,2.0,3.0,4.0);
//非法,使用指针指向向量的分量不能允许
my_func(&pos5.xy);
//矩阵.
float4x4 m;
//指的将第二排向量(x,y,z,w)赋值都为2.0f
m[1] = float4(2.0f);
m[0][0] = 1.0f;
//向量所有的构造方式:
//float4类型向量的所有可能构造方式
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,float2 b,float c);
float4(float a,float b,float2 c);
float4(float3 a,float b);
float4(float a,float3 b);
float4(float4 x);
//float3类型向量的所有可能的构造的方式
float3(float x);
float3(float x,float y,float z);
float3(float a,float2 b);
float3(float2 a,float b);
float3(float3 x);
//float2类型向量的所有可能的构造方式
float2(float x);
float2(float x,float y);
float2(float2 x);
//多个向量构造器的使用
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);
float2 a1 = float2(1.0,1.0);
float2 a2 = float2(2.0,2.0);
float2x2 a = float2x2(a1,a2);
//不可以!
//矩阵不支持从多个标量构造.
float2x2(float a00,float a01,float a02,float a00);
//矩阵不支持标量和向量混合构造
float2x3(float2 a,float b,float c,float d);
//定义一块缓存!
//Metal 所谓缓存就是一个块用指针指向区域.
//客户端代码中,使用指针指向的一块地址区域(内存)
//在Metal 程序中,你使用指针指向一块地址区域(显存)
//2个修饰符: device (设备空间),constant(设备空间只读)
//作用: 当做函数的参数来传递.
//const device
//constant
device float4 *device_buffer;
//结构体
struct my_user_data{
float4 a;
float2 c;
int b;
};
constant my_user_data *user_data;
//纹理.
//纹理数据类型就是一个句柄. 它指向一个一维/二维/三维纹理数据.
enum class access {sample,read,write};
texture1d<T,access a = access::sample>
texture1d_array<T,access a = access::sample>
texture2d<T,access a = access::sample>
texture2d_array<T,access a = access::sample>
texture3d<T,access a = access::sample>
texturecube<T,access a = access::sample>
texture2d_ms<T,access a = access::read>
//带有深度格式的纹理必须被声明为下面纹理数据类型中的一个
enum class depth_forma {depth_float};
depth2d<T,access a = depth_format::depth_float>
depth2d_array<T,access a = access::sample,depth_format d = depth_format::depth_float>
depthcube<T,access a = access::sample,depth_format d = depth_format::depth_float>
depth2d_ms<T,access a = access::read,depth_format d = depth_format::depth_float>
//T.表示类型.int,short....
void foo (texture2d<float> imgA[[texture(0)]],
texture2d<float,access::read> imgB[[texture(1)]],
texture2d<float,access::write> imgC[[texture(2)]])
{
//...
}
网友评论