Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Numeric Builtins

The numeric builtins mirror WGSL's numeric functions. Each is a free function exported by wgsl_rs::std::*. Many are defined per concrete type via a one-trait-per-builtin strategy: the transpiler resolves the right WGSL builtin based on argument types.

Trigonometric

FunctionWGSL EquivalentDescription
sin(x)sinSine, radians.
cos(x)cosCosine, radians.
tan(x)tanTangent, radians.
asin(x)asinArc sine, result in radians.
acos(x)acosArc cosine, result in radians.
atan(x)atanArc tangent, result in radians.
atan2(y, x)atan2Arc tangent of y / x, quadrant-aware.
sinh(x)sinhHyperbolic sine.
cosh(x)coshHyperbolic cosine.
tanh(x)tanhHyperbolic tangent.
asinh(x)asinhArc hyperbolic sine.
acosh(x)acoshArc hyperbolic cosine.
atanh(x)atanhArc hyperbolic tangent.
radians(x)radiansDegrees → radians.
degrees(x)degreesRadians → degrees.

Exponential, logarithmic, and root

FunctionWGSL EquivalentDescription
pow(x, y)powx raised to y.
exp(x)expe^x.
exp2(x)exp22^x.
log(x)logNatural logarithm.
log2(x)log2Base-2 logarithm.
sqrt(x)sqrtSquare root.
inverse_sqrt(x)inverseSqrt1 / sqrt(x).

Rounding and floating-point decomposition

FunctionWGSL EquivalentDescription
ceil(x)ceilRound toward +∞.
floor(x)floorRound toward −∞.
round(x)roundRound to nearest integer.
trunc(x)truncRound toward zero.
fract(x)fractFractional part: x - floor(x).
sign(x)signSign of x as −1, 0, or +1.
abs(x)absAbsolute value.
fma(a, b, c)fmaFused multiply-add: a*b + c with single rounding.
modf(x)modfSplit into fractional and whole parts (see below).
frexp(x)frexpSplit significand and exponent (see below).
ldexp(fract, exp)ldexpfract * 2^exp, inverse of frexp.

modf

modf(x) returns a struct with two fields:

#![allow(unused)]
fn main() {
let r = modf(-1.5);
let frac: f32 = r.fract; //  0.5
let whole: f32 = r.whole; // -1.0
}

frexp

frexp(x) returns a struct with:

  • .fract — significand in [0.5, 1.0)
  • .exp — exponent such that x = fract * 2^exp

Interpolation and clamping

FunctionWGSL EquivalentDescription
mix(a, b, t)mixLinear interpolation: a + (b - a) * t.
clamp(x, lo, hi)clampClamp x to [lo, hi].
min(x, y)minMinimum.
max(x, y)maxMaximum.
saturate(x)saturate (idiom)Clamp x to [0.0, 1.0].
step(edge, x)step0.0 if x < edge, else 1.0.

Geometric

FunctionWGSL EquivalentDescription
length(v)lengthEuclidean length.
distance(a, b)distancelength(a - b).
dot(a, b)dotDot product.
cross(a, b)cross3D cross product.
normalize(v)normalizeUnit-length vector: v / length(v).
reflect(i, n)reflectReflection of incident i about normal n.
refract(i, n, eta)refractRefraction per Snell's law.
face_forward(n, i, ng)faceForwardn flipped to face away from i relative to ng.

Per-type dispatch

Some builtins are implemented as traits with one method per concrete scalar type (the one-trait-per-builtin strategy). This keeps the CPU implementation type-correct and lets the transpiler emit the exact WGSL overload. You call them as ordinary free functions; the correct specialization is inferred from argument types.

Example

#![allow(unused)]
fn main() {
#[wgsl]
pub mod numeric_example {
    use wgsl_rs::std::*;

    pub fn to_srgb(linear: f32) -> f32 {
        if linear <= 0.0031308 {
            linear * 12.92
        } else {
            1.055 * pow(linear, 1.0 / 2.4) - 0.055
        }
    }

    pub fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
        let t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
        t * t * (3.0 - 2.0 * t)
    }
}
}

smoothstep shown above is a user-authored helper using clamp and pow. If a WGSL smoothStep builtin is supported by your target, prefer it.