Matrix Types

矩阵类型

Another very common type of variable that you will find yourself

using in HLSL shaders is matrices, which are 2D arrays of data.

Like scalars and vectors, matrices may be composed of any of the

basic data types: bool, int, half, float, or double. Matrices may be

of any size, but you will typically find shader writers using matrices

with up to four rows and columns. Recall that the example

vertex shader shown at the beginning of the chapter declared two

4×4 float matrices at global scope:

另一种你可以在你的HLSL着色器中发现的经常使用的变量类型

是矩阵,也就是2维的数据数组。像标量和向量一样,矩阵也许

可以由其他任何基本的数组类型组成:boolinthalffloat或者

double。矩阵也许可以是任何大小,但是你通常会发现着色器代码

作者使用最多44列。回想本章开始的例程中展示的顶点着色器

中在全局域中声明的两个4×4浮点数矩阵:

 

float4x4 view_proj_matrix;

float4x4 texture_matrix0;

Naturally, other dimensions of matrices can be used. For example,

we could declare a floating-point matrix with three rows and four

columns in a variety of ways:

 

自然地,其他维度的矩阵也可以使用。例如,我们可以用两种方式声明一个

三行四列的浮点数矩阵:

 

float3x4 mat0;

matrix<float, 3, 4> mat1;

 

Like vectors, the individual elements of matrices can be accessed

using array or structure/swizzle syntax. For example, the following

array indexing syntax can be used to access the top-left

element of the matrix view_proj_matrix:

 

类似于向量,矩阵的独立分量可以通过数组或者结构、swizzle语法

来存取。例如,下列数组索引语法可以被用来存取矩阵view_proj_matrix

左上角的元素。

 

float fValue = view_proj_matrix[0][0];

 

There is also a structure syntax defined for access to and swizzling

of matrix elements. For zero-based row-column position, you

can use any of the following:

 

还有一种被定义的结构语法用来存取和swizzling矩阵元素。对于

基于0起始的行列位置,可以使用下列任意一种方法:

 

_m00, _m01, _m02, _m03

_m10, _m11, _m12, _m13

_m20, _m21, _m22, _m23

_m30, _m31, _m32, _m33

 

For one-based row-column position, you can use any of the

following:

 

对于1起始的行列位置,可以使用下列任意一种方法:

 

_11, _12, _13, _14

_21, _22, _23, _24

_31, _32, _33, _34

_41, _42, _43, _44

 

Matrices can also be accessed using array notation. For example:

 

矩阵也可以通过数组下标进行存取。例如:

 

float2x2 fMat = {3.0f, 5.0f, // row 1

2.0f, 1.0f}; // row 2

float value0 = fMat[0]; // value0 is 3.0f

float value1 = fMat._m00; // value1 is 3.0f

float value2 = fMat._12 // value2 is 5.0f

float value3 = fMat[1][1] // value3 is 1.0f

float2 vec0 = fMat._21_22; // vec0 is {2.0f, 1.0f}

float2 vec1 = fMat[1]; // vec1 is {2.0f, 1.0f}