Intrinsics1 min read

transpose

Transposes a matrix (swaps rows and columns).

Reading Time
1 min
Word Count
133
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 float2x2, float3x3, float4x4, or rectangular variants.

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
float3x3 normalMatrix = float3x3(modelMatrix);
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
float3x3 normalMatrix = transpose(inverse(float3x3(modelMatrix)));
float3 worldNormal = normalize(normalMatrix * objectNormal);

View Matrix Inversion

bwsl
// For orthonormal matrices, inverse = transpose
float3x3 rotationOnly = float3x3(viewMatrix);
float3x3 inverseRotation = transpose(rotationOnly);

TBN Matrix

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

Switching Row/Column Major

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

Matrix Multiplication Order

bwsl
// Change multiplication order: transpose(A * B) = transpose(B) * transpose(A)
float3x3 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