HLSL的基本语法
1 数据类型
1.1 标量类型
1. bool: True or false .Note that the HLSL provides the true and false keywordslike in C++.
2. int: 32-bit signedinteger.
3. half: 16-bit-floatingpoint number.
4. float: 32-bit-floatingpoint number.
5. double: 64-bit-floatingpoint number.
1.2 向量类型
1.2.1 向量种类
1. float2: 2D vector, wherethe components are of type float.
2. float3: 3D vector, wherethe components are of type float.
3. float4: 4D vector, wherethe components are of type float.
除了float之外,我们也可以用其它基本类型来定义向量,如bool2,int3.总之向量的表达类型是TypeN,其中N属于(2,4).
1.2.2向量的初始化
float3 v = {1.0f, 2.0f, 3.0f};
float2 w = float2(x, y);
float4 u = float4(w, 3.0f, 4.0f); // u= (w.x, w.y, 3.0f, 4.0f)
1.2.3向量的访问
用数组语法:
如:vec[i] = 2.0f;
用字母语法,xyzw,rgba。
如:
vec.x = vec.r = 1.0f;
vec.y = vec.g = 2.0f;
vec.z = vec.b = 3.0f;
vec.w = vec.a = 4.0f;
1.2.4 向量的替换调配(swizzles)
为了避免向量赋值时的复杂,HLSL采取以Swizzles的方法,如下:
float4 u = {1.0f, 2.0f, 3.0f, 4.0f};
float4 v = {0.0f, 0.0f, 5.0f, 6.0f};
v = u.wyyx; // v = {4.0f, 2.0f, 2.0f,1.0f}
v = u.wzyx; // v = {4.0f, 3.0f, 2.0f,1.0f}
v.xy = u; // v = {1.0f, 2.0f, 5.0f,6.0f}
1.3 矩阵类型
1.3.1矩阵的定义语法
Typem×n xxx;如:
1. float2x2: 2 × 2 matrix, where theentries are of type float.
2. half3x3: 3 × 3 matrix, where theentries are of type half.
3. int4x4: 4 × 4 matrix, where theentries are of type int.
4. bool3x4: 3 × 4 matrix, where theentries are of type bool.
1.3.2 矩阵的访问
用数组语法:
M[i] [j] = value;
用成员变量语法:
M._11 = M._12 = M._13 = M._14 = 0.0f;
M._21 = M._22 = M._23 = M._24 = 0.0f;
M._31 = M._32 = M._33 = M._34 = 0.0f;
M._41 = M._42 = M._43 = M._44 = 0.0f;
或者
M._m00 = M._m01 = M._m02 = M._m03 =0.0