GLSL Learning

GLSL Built-in Functions Reference

The built-in functions you reach for most when writing shaders. Each page explains what the function does, when to use it and where people usually go wrong, with a live example you can edit.

Common functions

Trigonometry and exponential

radians()float radians(float degrees)Converts an angle from degrees to radians, the unit that sin, cos and the other trigonometric functions expect.degrees()float degrees(float radians)Converts an angle from radians to degrees. Handy when you want to reason about an angle like 45 or 90.sin()float sin(float angle)Returns the sine of an angle in radians, a smooth wave that repeats every 2 * PI and stays between -1.0 and 1.0.cos()float cos(float angle)Returns the cosine of an angle in radians: the same wave as sin, shifted a quarter turn ahead.tan()float tan(float angle)Returns the tangent of an angle in radians, sin / cos. It shoots off to infinity near 90 degrees.atan()float atan(float y, float x)Returns the angle in radians whose tangent is y / x. The two-argument form atan(y, x) gives the full angle of a point around the origin, from -PI to PI.acos()float acos(float x)Returns the angle in radians whose cosine is x. acos(dot(a, b)) gives the angle between two unit vectors.pow()float pow(float x, float y)Returns x raised to the power y. On values between 0.0 and 1.0 it bends a straight ramp into a curve.sqrt()float sqrt(float x)Returns the square root of x. For values between 0.0 and 1.0 it lifts the low end, rising fast and then flattening.inversesqrt()float inversesqrt(float x)Returns 1.0 / sqrt(x). Common in normalization and in light falloff that fades with distance.exp()float exp(float x)Returns e (about 2.718) raised to the power x. exp(-k * d) is the standard smooth falloff for glows and fog.exp2()float exp2(float x)Returns 2 raised to the power x. Doubling and halving per step, as in noise octaves and exposure stops.log()float log(float x)Returns the natural logarithm of x, the inverse of exp. It turns multiplication into addition, which makes infinite zooms possible.log2()float log2(float x)Returns the base-2 logarithm of x: how many times you double 1.0 to reach x. The inverse of exp2.

Geometric functions

Textures and derivatives

Want to learn step by step?

This reference is for quick lookups. If you are new to GLSL, start the learning path from lesson one: one concept per lesson, each with an exercise.

Start learning