What Built-in Matrix Types Exist in Modern GLSL?
Modern OpenGL Shading Language (GLSL) provides a comprehensive set of built-in matrix types designed for geometric transformations, lighting calculations, and projection mathematics. These types cover both square and non-square dimensions with support for single-precision and double-precision floating-point formats. In GLSL, matrix types are defined using column-major ordering, meaning data is accessed and structured as a collection of column vectors.
Matrix Naming Conventions
Matrix types in GLSL use the syntax matNxM or
dmatNxM, where N specifies the number of
columns and M specifies the number of rows. This notation
is the inverse of standard mathematical matrix conventions (which
typically specify rows first). When a matrix is square (\(N = M\)), shorthand aliases like
matN and dmatN are standard.
Single-Precision Matrix
Types (float)
Single-precision 32-bit floating-point matrices are the most commonly used types in standard graphics pipelines.
mat2(ormat2x2): 2 columns \(\times\) 2 rowsmat2x3: 2 columns \(\times\) 3 rowsmat2x4: 2 columns \(\times\) 4 rowsmat3x2: 3 columns \(\times\) 2 rowsmat3(ormat3x3): 3 columns \(\times\) 3 rows (commonly used for normal transformations and 2D affine operations)mat3x4: 3 columns \(\times\) 4 rowsmat4x2: 4 columns \(\times\) 2 rowsmat4x3: 4 columns \(\times\) 3 rows (often used for skeletal animation and skinning)mat4(ormat4x4): 4 columns \(\times\) 4 rows (the standard type for 3D Model-View-Projection pipelines)
Double-Precision Matrix
Types (double)
Introduced in GLSL 4.00 for precision-sensitive applications such as
large-scale planetary rendering or scientific simulation, 64-bit
double-precision types use the dmat prefix:
dmat2(ordmat2x2): 2 columns \(\times\) 2 rowsdmat2x3: 2 columns \(\times\) 3 rowsdmat2x4: 2 columns \(\times\) 4 rowsdmat3x2: 3 columns \(\times\) 2 rowsdmat3(ordmat3x3): 3 columns \(\times\) 3 rowsdmat3x4: 3 columns \(\times\) 4 rowsdmat4x2: 4 columns \(\times\) 2 rowsdmat4x3: 4 columns \(\times\) 3 rowsdmat4(ordmat4x4): 4 columns \(\times\) 4 rows
Core Features and Operations
GLSL treats matrices as first-class types with built-in hardware optimization:
- Column Indexing: Indexing a matrix once yields a
column vector (e.g.,
myMat[0]returns the first column as avec4), while double-indexing accesses individual scalar elements viamyMat[col][row]. - Arithmetic Operations: Standard algebraic operators
(
+,-,*) perform linear algebra transformations directly. Multiplying a matrix by a vector (mat4 * vec4) transforms the vector, while multiplying two matrices performs standard matrix multiplication. - Built-in Functions: Modern GLSL provides built-in
intrinsic functions including
transpose(m),determinant(m), andinverse(m)for square matrices.