Built-in Functions

Most built-ins are component-wise and accept float or vecN (genType).

Most built-ins are component-wise and accept float or vecN (genType).

12.1 Angle & trigonometry

radians, degrees, sin, cos, tan, asin, acos, atan(y, x), atan(y_over_x), sinh, cosh, tanh, asinh, acosh, atanh.

12.2 Exponential

pow(x, y), exp, log, exp2, log2, sqrt, inversesqrt. ⚠️ pow(x, y) is undefined for x < 0 and for x = 0, y <= 0.

12.3 Common functions

abs, sign, floor, ceil, trunc, round, roundEven, fract x − y·floor(x/y) (differs from C fmod for negatives) modf(x, out i) fractional + integer parts min, max, clamp(x, lo, hi) mix(a, b, t) linear interpolation a·(1−t) + b·t (also mix(a, b, bvec) select) step(edge, x) 0.0 if x < edge else 1.0 smoothstep(e0, e1, x) smooth Hermite 0→1 between edges isnan, isinf NaN/Inf checks

floatBitsToInt, intBitsToFloat, floatBitsToUint, uintBitsToFloat fma(a, b, c) fused multiply-add (4.00+) frexp, ldexp

12.4 Geometric

length(v), distance(a, b) Euclidean length / distance dot(a, b), cross(a, b) dot/cross product (cross: vec3 only) faceforward(N, I, Nref) flip N to face the viewer reflect(I, N) reflection direction (N must be normalized) refract(I, N, eta) refraction direction (Snell's law; returns 0 for total internal reflection)

12.5 Matrix & vector relational

matrixCompMult, outerProduct, transpose, determinant, inverse; lessThan, lessThanEqual, greaterThan, greaterThanEqual, equal, notEqual, any, all, not.

12.6 Integer & bit functions (GLSL 4.00+, ES 3.10+)

bitfieldExtract, bitfieldInsert, bitfieldReverse, bitCount, findLSB, findMSB, uaddCarry, usubBorrow, umulExtended, imulExtended.

12.7 Packing

packUnorm2x16, packSnorm4x8, packHalf2x16, unpackHalf2x16, unpackUnorm4x8, packDouble2x32 — compress data in G-buffers and SSBOs.

12.8 Texture functions (§16)

texture, textureLod, textureOffset, texelFetch, textureGrad, textureProj, textureSize, textureQueryLod, textureQueryLevels, textureGather, textureSamples.

12.9 Fragment derivatives

dFdx(p), dFdy(p) — rate of change across neighboring pixels in the 2×2 quad; fwidth(p) = abs(dFdx(p)) + abs(dFdy(p)); dFdxFine/Coarse, dFdyFine/Coarse (4.50). Used for anti-aliasing, mip selection, flat normals:

vec3 flatNormal = normalize(cross(dFdx(vWorldPos), dFdy(vWorldPos)));
float aa = fwidth(d);                         // anti-aliased edge width
float alpha = 1.0 - smoothstep(-aa, aa, d);

12.10 Other built-ins

Noise functions noise1..4 exist in the spec but are unimplemented (return 0) — write your own (§19). Image functions (imageLoad, imageStore, imageAtomicAdd), atomic functions (atomicAdd, atomicCompSwap), barriers (barrier, memoryBarrier*), geometry shader EmitVertex/EndPrimitive, interpolation functions interpolateAtCentroid/Sample/Offset.

🧪 Part I Exercises

On Shadertoy, draw a horizontal gradient from red to blue, then a radial gradient centered on the mouse (iMouse).

Render a triangle in OpenGL or WebGL with per-vertex colors; then make it flat-shaded with the flat qualifier.

Write a fullscreen-triangle vertex shader using only gl_VertexID.

Practice swizzles: given vec4 v = vec4(1,2,3,4), compute (4,3,2,1), (1,1,4,4), and swap xy with zw in place.

Write a function rotate2D(float angle) returning a mat2; use it to rotate UV coordinates over time.

Write a shader that uses step, smoothstep, and mix to draw three vertical bands with soft edges.

Port a GLSL ES 1.00 shader (using attribute, varying, gl_FragColor, texture2D) to #version 300 es.

Use #define and #if to toggle between two color schemes at compile time.

Display gl_FrontFacing as color (red front, blue back) on a double-sided quad.

Visualize fwidth(uv * 10.0) and explain what it shows.