WebGPU.rocks Logo

Numeric built-in functions

Trigonometric

For all functions in this section, if the argument is a vector, the result is calculated per component.

fn degrees(e: T) -> T

Converts radians to degrees, approximating e × 180 ÷ π.

fn radians(e: T) -> T

Converts degrees to radians, approximating e × π ÷ 180.

fn cos(e: T) -> T

Returns the cosine of e, where e is in radians. Component-wise when T is a vector.

fn cosh(e: T) -> T

Returns the hyperbolic cosine of e, where e is a hyperbolic angle in radians.

fn acos(e: T) -> T

Returns the principal value, in radians, of the inverse cosine of e.

fn acosh(e: T) -> T

Returns the inverse hyperbolic cosine of e, as a hyperbolic angle in radians.

fn sin(e: T) -> T

Returns the sine of e, where e is in radians. Component-wise when T is a vector.

fn sinh(e: T) -> T

Returns the hyperbolic sine of e, where e is a hyperbolic angle in radians.

fn asin(e: T) -> T

Returns the principal value, in radians, of the inverse sine of e.

fn asinh(e: T) -> T

Returns the inverse hyperbolic sine of e, as a hyperbolic angle in radians.

fn tan(e: T) -> T

Returns the tangent of e, where e is in radians.

fn tanh(e: T) -> T

Returns the hyperbolic tangent of e, where e is a hyperbolic angle in radians.

fn atan(e: T) -> T

Returns the principal value, in radians, of the inverse tangent of e.

fn atanh(e: T) -> T

Returns the inverse hyperbolic tangent of e, as a hyperbolic angle in radians.

fn atan2(y: T, x: T) -> T

Returns an angle, in radians, in the interval [-π, π] whose tangent is y÷x.

fn exp(e: T) -> T

Returns the natural exponentiation of e. Component-wise when T is a vector.

fn exp2(e: T) -> T

Returns 2 raised to the power e.

fn log(e: T) -> T

Returns the natural logarithm of e.

fn log2(e: T) -> T

Returns the base-2 logarithm of e.

fn pow(e1: T, e2: T) -> T

Returns e1 raised to the power e2.

fn sqrt(e: T) -> T

Returns the square root of e.

fn inverseSqrt(e: T) -> T

Returns the reciprocal of sqrt(e).

Linear Algebra

fn cross(e1: vec3<T>, e2: vec3<T>) -> vec3<T>

Returns the cross product of e1 and e2.

fn determinant(e: matCxC<T>) -> T

Returns the determinant of e.

fn distance(e1: T, e2: T) -> S

Returns the distance between e1 and e2, e.g. length(e1 - e2).

fn dot(e1: vecN<T>, e2: vecN<T>) -> T

Returns the dot product of e1 and e2.

fn faceForward(e1: vecN<T>, e2: vecN<T>, e3: vecN<T>) -> vecN<T>

Returns e1 if dot(e2, e3) is negative, and -e1 otherwise.

fn fma(e1: T, e2: T, e3: T) -> T

Fused multiply add, returns e1 * e2 + e3. Component-wise when T is a vector.

fn length(e: T) -> S

Returns the 2-norm of e. Evaluates to the absolute value of e if T is scalar.

fn normalize(e: vecN<T> ) -> vecN<T>

Returns a unit vector in the same direction as e.

fn reflect(e1: T, e2: T) -> T

For the incident vector e1 and surface orientation e2, returns the reflection direction e1 - 2 * dot(e2, e1) * e2.

fn refract(e1: T, e2: T, e3: I) -> T

For the incident vector e1 and surface normal e2, and the ratio of indices of refraction e3, let k = 1.0 - e3 * e3 * (1.0 - dot(e2, e1) * dot(e2, e1)). If k < 0.0, returns the refraction vector 0.0, otherwise return the refraction vector e3 * e1 - (e3 * dot(e2, e1) + sqrt(k)) * e2.

fn transpose(e: matRxC<T>) -> matCxR<T>

Returns the transpose of e.

Round, Limit, Mix, Compare

For all functions in this section, if the argument is a vector, the result is calculated per component.

fn abs(e: T ) -> T

The absolute value of e. Component-wise when T is a vector.

fn sign(e: T) -> T

Returns 1.0 when e > 0, 0.0 when e = 0, and -1.0 when e < 0.

fn fract(e: T) -> T

Returns the fractional part of e, computed as e - floor(e).

fn trunc(e: T) -> T

Returns the nearest whole number whose absolute value is less than or equal to e.

fn modf(e: T) -> __modf_result

Splits e into fractional and whole number parts. The fractional part is e % 1.0, and the whole part is e minus the whole part. Returns the __modf_result or __modf_result_f16 built-in structure.

fn ceil(e: T) -> T

Returns the ceiling of e. Component-wise when T is a vector.

fn floor(e: T) -> T

Returns the floor of e. Component-wise when T is a vector.

fn round(e: T) -> T

Returns the integer k nearest to e, as a floating point value. When e lies halfway between integers k and k + 1, the result is k when k is even, and k+1 when k is odd.

fn clamp(e: T, low: T, high: T) -> T

Restricts the value of e within a range. The result is min(max(e, low), high).

fn saturate(e: T) -> T

Returns clamp(e, 0.0, 1.0).

fn step(edge: T, x: T) -> T

Returns 1.0 if edge ≤ x, and 0.0 otherwise.

fn smoothstep(low: T, high: T, x: T) -> T

Returns the smooth Hermite interpolation between 0 and 1. For scalar t, the result is t * t * (3.0 - 2.0 * t), where t = clamp((x - low) / (high - low), 0.0, 1.0).

fn max(e1: T, e2: T) -> T

Returns e2 if e1 is less than e2, and e1 otherwise.

fn min(e1: T, e2: T) -> T

Returns e2 if e2 is less than e1, and e1 otherwise.

fn mix(e1: T, e2: T, e3: T) -> T

Returns the (component-wise) linear blend of e1 and e2, e.g. e1 * (1 - e3) + e2 * e3).

fn frexp(e: T) -> __frexp_result

Splits e into a fraction and an exponent so that e = fraction * 2^exponent. The fraction is 0.0 or its magnitude is in the range [0.5, 1.0). Returns the __frexp_result or __frexp_result_f16 built-in structure.

fn ldexp(fraction: T, exponent: I) -> T

Returns fraction * 2^exponent.

fn quantizeToF16(e: T) -> T

Quantizes a 32-bit floating point value e as if e were converted to a IEEE 754 binary16 value, and then converted back to a IEEE 754 binary32 value.

Bit

fn countLeadingZeros(e: T) -> T

The number of consecutive 0 bits starting from the most significant bit of e.

fn countOneBits(e: T) -> T

The number of 1 bits in the representation of e, also known as “population count”.

fn countTrailingZeros(e: T) -> T

The number of consecutive 0 bits starting from the least significant bit of e.

fn extractBits(e: T, offset: u32, count: u32) -> T

If T is signed, reads bits with sign extension. Bits 0...c-1 of the result are copied from bits o...o+c-1 of e. Other bits of the result are the same as bit c-1 of the result.

If T is unsigned, reads bits without sign extension. Bits 0...c-1 of the result are copied from bits o...o+c-1 of e. Other bits of the result are 0.

fn firstLeadingBit(e: T) -> T

if T is signed: returns -1 if e is 0 or -1, otherwise the position of the most significant bit in e that is different from e’s sign bit.

If T is unsigned: returns T(-1) if e is zero, otherwise the position of the most significant 1 bit in e.

fn firstTrailingBit(e: T) -> T

Returns T(-1) if e is zero, otherwise the position of the least significant 1 bit in e.

fn insertBits(e: T, newbits: T, offset: u32, count: u32) -> T

Sets bits in an integer. The result is e if c is 0. Otherwise, bits o...o+c-1 of the result are copied from bits 0...c-1 of newbits. Other bits of the result are copied from e.

fn reverseBits(e: T) -> T

Reverses the bits in e: The bit at position k of the result equals the bit at position 31 -k of e.