Intrinsics1 min read

transpose

Transposes a matrix (swaps rows and columns).

Reading Time
1 min
Word Count
131
Sections
12
Try It Live

Test transpose in a live shader

Open the playground, start from a visual preset, and wire transpose into the fragment stage to see how it behaves with real values.

Open Playground

Live Demo

The transpose function returns the transpose of a matrix, swapping its rows and columns.

Signature

bwsl
transpose :: (matNxM m) -> matMxN {...}

Where matNxM can be mat2, mat3, or mat4.

Parameters

ParameterTypeDescription
mmatNxMThe matrix to transpose

Return Value

Returns the transposed matrix where result[i][j] = m[j][i].

Example

bwsl
pipeline NormalTransform {
fragment {
// Transform normal by inverse transpose of model matrix
// For non-uniform scaling, normals need special handling
mat3 normalMatrix = mat3(tangent, bitangent, normal);
normalMatrix = transpose(inverse(normalMatrix));
float3 worldNormal = normalize(normalMatrix * input.normal);
output.color = float4(worldNormal * 0.5 + 0.5, 1.0);
}
}

Common Use Cases

Normal Transformation

bwsl
// Correct normal matrix for non-uniform scaling
mat3 normalMatrix = transpose(inverse(mat3(tangent, bitangent, normal)));
float3 worldNormal = normalize(normalMatrix * objectNormal);

View Matrix Inversion

bwsl
// For orthonormal matrices, inverse = transpose
mat3 rotationOnly = mat3(right, up, forward);
mat3 inverseRotation = transpose(rotationOnly);

TBN Matrix

bwsl
// Convert from tangent to world space (and back)
mat3 TBN = mat3(tangent, bitangent, normal);
mat3 inverseTBN = transpose(TBN); // Works if orthonormal

Switching Row/Column Major

bwsl
// Convert between conventions
mat4 columnMajor = transpose(rowMajorMatrix);

Matrix Multiplication Order

bwsl
// Change multiplication order: transpose(A * B) = transpose(B) * transpose(A)
mat3 combined = transpose(transpose(B) * transpose(A));

Orthonormal Matrices

For orthonormal matrices (rotation matrices), the transpose equals the inverse. This is much cheaper to compute than a full matrix inverse.

Rectangular Matrices

Transposing a 3x4 matrix gives a 4x3 matrix. The dimensions swap.

Compiled Output

When compiled to GLSL:

glsl
transpose(m)

When compiled to HLSL:

hlsl
transpose(m)

When compiled to SPIR-V:

Uses OpTranspose instruction.

See Also