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 (or mat2x2): 2 columns \(\times\) 2 rows
  • mat2x3: 2 columns \(\times\) 3 rows
  • mat2x4: 2 columns \(\times\) 4 rows
  • mat3x2: 3 columns \(\times\) 2 rows
  • mat3 (or mat3x3): 3 columns \(\times\) 3 rows (commonly used for normal transformations and 2D affine operations)
  • mat3x4: 3 columns \(\times\) 4 rows
  • mat4x2: 4 columns \(\times\) 2 rows
  • mat4x3: 4 columns \(\times\) 3 rows (often used for skeletal animation and skinning)
  • mat4 (or mat4x4): 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 (or dmat2x2): 2 columns \(\times\) 2 rows
  • dmat2x3: 2 columns \(\times\) 3 rows
  • dmat2x4: 2 columns \(\times\) 4 rows
  • dmat3x2: 3 columns \(\times\) 2 rows
  • dmat3 (or dmat3x3): 3 columns \(\times\) 3 rows
  • dmat3x4: 3 columns \(\times\) 4 rows
  • dmat4x2: 4 columns \(\times\) 2 rows
  • dmat4x3: 4 columns \(\times\) 3 rows
  • dmat4 (or dmat4x4): 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 a vec4), while double-indexing accesses individual scalar elements via myMat[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), and inverse(m) for square matrices.