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.lessThan()bvec2 lessThan(vec2 x, vec2 y)Component-wise less-than comparison returning a boolean vector.lessThanEqual()bvec2 lessThanEqual(vec2 x, vec2 y)Component-wise less-than-or-equal comparison, including the boundary.greaterThan()bvec2 greaterThan(vec2 x, vec2 y)Component-wise greater-than comparison.greaterThanEqual()bvec2 greaterThanEqual(vec2 x, vec2 y)Component-wise greater-than-or-equal comparison.equal()bvec2 equal(vec2 x, vec2 y)Tests exact equality independently for each vector component.notEqual()bvec2 notEqual(vec2 x, vec2 y)Marks components that differ between two vectors.any()bool any(bvec2 x)Returns true when at least one component is true.all()bool all(bvec2 x)Returns true only when every component is true.not()bvec2 not(bvec2 x)Negates every component of a boolean vector.round()genType round(genType x)Rounds to the nearest integer with implementation-chosen half-way behavior.roundEven()genType roundEven(genType x)Rounds to nearest integer, breaking ties toward the even integer.trunc()genType trunc(genType x)Truncates toward zero.modf()genType modf(genType x, out genType integerPart)Returns the signed fractional part and writes the integer part.isnan()genBType isnan(genType x)Detects NaN component-wise.isinf()genBType isinf(genType x)Detects positive or negative infinity component-wise.floatBitsToInt()genIType floatBitsToInt(genType x)Reinterprets floating-point bits as a signed integer.floatBitsToUint()genUType floatBitsToUint(genType x)Reinterprets floating-point bits as an unsigned integer.intBitsToFloat()genType intBitsToFloat(genIType x)Reinterprets signed integer bits as floating-point.uintBitsToFloat()genType uintBitsToFloat(genUType x)Reinterprets unsigned integer bits as floating-point.packSnorm2x16()uint packSnorm2x16(vec2 v)Packs two signed normalized components into one uint.unpackSnorm2x16()vec2 unpackSnorm2x16(uint p)Unpacks one uint into two signed normalized components.packUnorm2x16()uint packUnorm2x16(vec2 v)Packs two unsigned normalized components into one uint.unpackUnorm2x16()vec2 unpackUnorm2x16(uint p)Unpacks one uint into two unsigned normalized components.packHalf2x16()uint packHalf2x16(vec2 v)Converts two floats to half precision and packs them.unpackHalf2x16()vec2 unpackHalf2x16(uint p)Unpacks two half-precision components to floats.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.asin()genType asin(genType x)Inverse sine; the input must be between minus one and one.sinh()genType sinh(genType x)Returns the hyperbolic sine.cosh()genType cosh(genType x)Returns the hyperbolic cosine, a catenary-shaped curve.tanh()genType tanh(genType x)Smoothly maps real inputs into the open interval minus one to one.asinh()genType asinh(genType x)Returns the inverse hyperbolic sine.acosh()genType acosh(genType x)Returns the inverse hyperbolic cosine for input at least one.atanh()genType atanh(genType x)Returns inverse hyperbolic tangent for absolute input below one.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.matrixCompMult()mat matrixCompMult(mat x, mat y)Multiplies matrices component by component rather than algebraically.outerProduct()mat outerProduct(vec column, vec row)Constructs a matrix from a column vector times a row vector.transpose()mat transpose(mat m)Swaps matrix rows and columns.determinant()float determinant(mat m)Returns a square matrix determinant, measuring signed scale.inverse()mat inverse(mat m)Returns the inverse of a nonsingular square matrix.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.dFdx()genType dFdx(genType p)Estimates the rate of change across screen-space fragment x.dFdy()genType dFdy(genType p)Estimates the rate of change across screen-space fragment y.texture2DProj()vec4 texture2DProj(sampler2D sampler, vec3 coord)Samples a 2D texture after dividing by the final coordinate.texture2DLod()vec4 texture2DLod(sampler2D sampler, vec2 coord, float lod)Selects an explicit 2D mip level in an ES 1.00 vertex shader.texture2DProjLod()vec4 texture2DProjLod(sampler2D sampler, vec3 coord, float lod)Combines projected coordinates with an explicit mip level in a vertex shader.textureCube()vec4 textureCube(samplerCube sampler, vec3 direction)Samples a cube map using a 3D direction.textureCubeLod()vec4 textureCubeLod(samplerCube sampler, vec3 direction, float lod)Samples a cube map at an explicit mip level in the vertex stage.texture()gvec4 texture(gsampler2D sampler, vec2 coord)Samples a texture using the sampler type to select the lookup.textureSize()ivec2 textureSize(gsampler2D sampler, int lod)Returns the dimensions of a selected texture mip level.texelFetch()gvec4 texelFetch(gsampler2D sampler, ivec2 coord, int lod)Reads an integer texel coordinate at a mip level without interpolation.texelFetchOffset()gvec4 texelFetchOffset(gsampler2D sampler, ivec2 coord, int lod, ivec2 offset)Fetches a texel with an additional constant integer offset.textureOffset()gvec4 textureOffset(gsampler2D sampler, vec2 coord, ivec2 offset)Performs a filtered texture lookup with a constant texel offset.textureLod()gvec4 textureLod(gsampler2D sampler, vec2 coord, float lod)Samples using an explicit floating-point mip level.textureLodOffset()gvec4 textureLodOffset(gsampler2D sampler, vec2 coord, float lod, ivec2 offset)Adds a constant texel offset to explicit-LOD sampling.textureGrad()gvec4 textureGrad(gsampler2D sampler, vec2 coord, vec2 dx, vec2 dy)Supplies explicit x/y coordinate gradients for texture filtering.textureGradOffset()gvec4 textureGradOffset(gsampler2D sampler, vec2 coord, vec2 dx, vec2 dy, ivec2 offset)Combines explicit texture gradients with a constant offset.textureProj()gvec4 textureProj(gsampler2D sampler, vec3 coord)Performs projected texture lookup in ES 3.00.textureProjOffset()gvec4 textureProjOffset(gsampler2D sampler, vec3 coord, ivec2 offset)Projected lookup with a constant texel offset.textureProjLod()gvec4 textureProjLod(gsampler2D sampler, vec3 coord, float lod)Projected sampling with explicit mip level.textureProjLodOffset()gvec4 textureProjLodOffset(gsampler2D sampler, vec3 coord, float lod, ivec2 offset)Projected lookup combining explicit LOD and constant offset.textureProjGrad()gvec4 textureProjGrad(gsampler2D sampler, vec3 coord, vec2 dx, vec2 dy)Projected lookup with explicit texture-coordinate gradients.textureProjGradOffset()gvec4 textureProjGradOffset(gsampler2D sampler, vec3 coord, vec2 dx, vec2 dy, ivec2 offset)Projected gradient lookup with a constant offset.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