GLSL Learning

GLSL built-in function

pow()

Returns x raised to the power y. On values between 0.0 and 1.0 it bends a straight ramp into a curve.

Syntax

float pow(float x, float y)
vec2 pow(vec2 x, vec2 y)
vec3 pow(vec3 x, vec3 y)
vec4 pow(vec4 x, vec4 y)

Parameters

x
The base. Must be greater than or equal to 0.
y
The exponent. Above 1.0 pushes values down, below 1.0 pulls them up.

Return value

x to the power y, with the same type as x.

Live example

Edit the code and the preview updates as you type.

How it works

For inputs between 0 and 1, pow never changes the two ends: pow(0.0, k) is 0 and pow(1.0, k) is 1. It only bends the middle:

exponentpow(0.5, k)effect
4.00.0625stays low for longer, then shoots up
2.00.25gentle ease in
1.00.5straight line
0.50.707rises fast, then flattens

The example sweeps the exponent over time and draws the result in green against the straight line y = x in gray.

Typical uses

  • Specular highlights: pow(max(dot(r, v), 0.0), 32.0), where a higher exponent gives a smaller, sharper highlight.
  • Fresnel rim light: pow(1.0 - dot(n, v), 5.0) lights up the edges of a sphere.
  • Shaping gradients and easing: pow(t, 2.0) for a slow start.
  • Gamma correction: pow(color, vec3(1.0 / 2.2)).

Common mistakes

  • Passing a negative base. pow(-0.5, 2.0) is undefined in GLSL and often returns NaN (shown as black), even though the math answer is 0.25. Use x * x, or pow(abs(x), k).
  • pow(0.0, 0.0) or a zero base with a negative exponent, which are also undefined.
  • Using pow(x, 2.0) in hot code. x * x is clearer and cheaper.

Related functions

Tutorials that use pow()