GLSL Learning

GLSL built-in function

inversesqrt()

Returns 1.0 / sqrt(x). Common in normalization and in light falloff that fades with distance.

Syntax

float inversesqrt(float x)
vec2 inversesqrt(vec2 x)
vec3 inversesqrt(vec3 x)

Parameters

x
A value greater than 0.

Return value

1.0 / sqrt(x), with the same type as x.

Live example

Edit the code and the preview updates as you type.

How it works

inversesqrt(x) is 1.0 / sqrt(x) in a single call: inversesqrt(4.0) is 0.5.

Combined with dot(v, v) (the squared length) it gives 1.0 / length(v). In the example that value is the brightness of each light: very bright right next to the light and fading slowly with distance, which produces soft glowing orbs that add up where they overlap.

Typical uses

  • Manual normalization: v * inversesqrt(dot(v, v)) is exactly normalize(v).
  • Glows and falloff: k * inversesqrt(dot(q, q)) for a 1 / distance glow.

Common mistakes

  • Passing 0 or a negative number. The result is undefined; add a small epsilon like dot(q, q) + 1e-4.
  • Using it purely for speed. Modern GPUs implement normalize and length efficiently; prefer whichever reads more clearly.

Related functions