Intrinsics1 min read

determinant

Computes the determinant of a matrix.

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

Test determinant in a live shader

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

Open Playground

Live Demo

The determinant function computes the determinant of a square matrix, a scalar value that encodes important properties like whether the matrix is invertible.

Signature

bwsl
determinant :: (matNxN m) -> float {...}

Where matNxN can be float2x2, float3x3, or float4x4.

Parameters

ParameterTypeDescription
mmatNxNA square matrix

Return Value

Returns the determinant of the matrix.

Example

bwsl
pipeline TransformCheck {
fragment {
// Check if transform preserves handedness
float3x3 rotation = float3x3(modelMatrix);
float det = determinant(rotation);
// Positive determinant = right-handed (normal)
// Negative determinant = left-handed (mirrored)
float handedness = sign(det);
// Adjust normal direction for mirrored objects
float3 normal = input.normal * handedness;
output.color = float4(normal * 0.5 + 0.5, 1.0);
}
}

Common Use Cases

Checking Invertibility

bwsl
// A matrix is invertible if determinant != 0
float det = determinant(matrix);
if (abs(det) > 0.0001) {
// Safe to invert
float3x3 inv = inverse(matrix);
}

Handedness Detection

bwsl
// Check if transform flips orientation
float det = determinant(float3x3(transform));
bool isMirrored = det < 0.0;

Scale Factor

bwsl
// Determinant gives volume scaling factor
float volumeScale = abs(determinant(scaleMatrix));
// For 3x3: det = scaleX * scaleY * scaleZ

Jacobian for Texture Mapping

bwsl
// Calculate texture distortion factor
float2x2 jacobian = float2x2(ddx(uv), ddy(uv));
float distortion = abs(determinant(jacobian));

Triangle Area

bwsl
// 2D triangle area using determinant
float2x2 m = float2x2(v1 - v0, v2 - v0);
float signedArea = determinant(m) * 0.5;
float area = abs(signedArea);

Geometric Meaning

  • For 2x2: Area scaling factor (signed)
  • For 3x3: Volume scaling factor (signed)
  • Sign indicates whether orientation is preserved

Performance

Computing the determinant is relatively expensive for 4x4 matrices. Cache the result if you need it multiple times.

Compiled Output

When compiled to GLSL:

glsl
determinant(m)

When compiled to HLSL:

hlsl
determinant(m)

See Also