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
abs()
float abs(float x)Returns the absolute value of x: negative numbers become positive, positive numbers stay the same.sign()float sign(float x)Returns -1.0 if x is negative, 0.0 if x is zero, and 1.0 if x is positive.floor()float floor(float x)Rounds x down to the nearest whole number that is less than or equal to x.ceil()float ceil(float x)Rounds x up to the nearest whole number that is greater than or equal to x.fract()float fract(float x)Returns the fractional part of x, computed as x - floor(x), always in the range 0.0 to 1.0.mod()float mod(float x, float y)Returns x modulo y, computed as x - y * floor(x / y): the remainder after dividing x by y.min()float min(float x, float y)Returns the smaller of x and y. With vectors, each component is compared separately.max()float max(float x, float y)Returns the larger of x and y. With vectors, each component is compared separately.clamp()float clamp(float x, float minVal, float maxVal)Limits x to the range between minVal and maxVal, computed as min(max(x, minVal), maxVal).mix()float mix(float x, float y, float a)Linearly interpolates between x and y: returns x when a is 0.0, y when a is 1.0, and a blend in between.step()float step(float edge, float x)Returns 0.0 if x is less than edge, otherwise 1.0. A hard on/off switch at a threshold.smoothstep()float smoothstep(float edge0, float edge1, float x)Returns 0.0 below edge0, 1.0 above edge1, and a smooth S-shaped curve in between.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
length()
float length(float x)Returns the length of a vector, its distance from the origin. length(p) is the starting point for drawing circles.distance()float distance(float p0, float p1)Returns the distance between two points, the same as length(p0 - p1).dot()float dot(vec2 x, vec2 y)Returns the dot product of two vectors. For unit vectors it measures how much they point the same way: 1.0 same direction, 0.0 perpendicular, -1.0 opposite.normalize()vec2 normalize(vec2 x)Returns a vector pointing in the same direction as x but with length 1.0. Keeps the direction, drops the size.cross()vec3 cross(vec3 x, vec3 y)Returns the cross product of two 3D vectors: a new vector perpendicular to both. Used to build normals and coordinate frames.reflect()vec2 reflect(vec2 I, vec2 N)Returns the direction a ray bounces to after hitting a surface: reflect(I, N) = I - 2.0 * dot(N, I) * N.refract()vec2 refract(vec2 I, vec2 N, float eta)Returns the direction a ray takes after passing into a different material, like light bending as it enters glass or water.faceforward()vec3 faceforward(vec3 N, vec3 I, vec3 Nref)Returns N or -N, whichever points against the incoming direction I. Makes normals face the viewer for two-sided lighting.Textures and derivatives
texture2D()
vec4 texture2D(sampler2D sampler, vec2 coord)Reads the color of an image (texture) at a given 0..1 coordinate. Changing the coordinate before reading is how you distort, scroll or tile images.fwidth()float fwidth(float p)Returns how much a value changes between neighboring pixels. Using it as the smoothstep width gives edges exactly one pixel soft at any zoom level.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