Graphics Reference
In-Depth Information
6.3.3 Matrices
Matrix variables are declared and used in a manner similar to vector variables. Matrix dec-
larations specify a base primitive type, in addition to the number of rows and columns. The
number of rows and columns are listed to 4 each, making for a maximum of 16 individual
components. Component access can be done with two-dimensional array syntax, where the
first index specifies the row and the second specifies the column. Additionally, when only
a single array index is specified, it returns the corresponding row of the matrix as a vector
type. Matrices also support their own syntax for member access, which differs from the
format used for vectors. These formats are shown in Listing 6.7.
float4x4 worldMatrix = float4x4( float4( 1.0f, 0.0f, 0.0f, 0.0f ),
float4( 0.0f, 1.0f, 0.0f, 0.0f ),
float4( 0.0f, 0.0f, 1.0f, 0.0f ),
float4( 0.0f, 0.0f, 0.0f, 1.0f ) );
float matVal0 = worldMatrix._m00; // Value from first row, first column
float matVall = worldMatrix._12; // Value from first row, second column
float matVal2 = worldMatrix[0][1]; // Value from first row, second column
float2 matVal3 = worldMatrix._11_12; // Swizzles
Listing 6.7. Matrix component access syntax.
As with vectors, operators used with matrix types are performed per-component.
Consequently the multiplication operator should not be used to perform matrix multiplica-
tions and transformations. Instead the mul intrinsic function is provided for matrix/matrix
and matrix/vector multiplications. See the "Intrinsic Functions" section in this chapter or
the SDK documentation for more details.
Typically, shader programs initialize matrices in a row-major format and handle all
vector/matrix transformations accordingly. However, in many cases the compiler will op-
timize the assembly such that the matrices contain column-major data, because that format
allows for a more efficient expression in assembly using four dot products. In addition, the
compiler defaults to treating all matrices declared in constant buffers as if they contained
column-major data, even when row-major transformation is performed with the mul intrin-
sic. Consequently, matrices may need to be transposed by the host application before being
set into the constant buffer, or the matrix would have to be transposed in the shader pro-
gram. This behavior can be changed by passing D3D10_SHADER_PACK_MATRIX_ROW_MAJOR
to shader compilation functions, or by declaring matrices with the row_major modifier.
6.3.4 Structures
HLSL allows for custom structure declarations, with rules very similar to C++. Structures
can contain an arbitrary number of members with scalar, vector, or matrix primitive types.
Search WWH ::




Custom Search