Table of Contents

webgpufundamentals.org

Fix, Fork, Contribute

WGSL Function Reference

Bit Reinterpretation Built-in Functions

FunctionParameter TypesDescription
 fn bitcast<T>(e : T) -> T 
T is a concrete numeric scalar or concrete numeric vectorIdentity transform.
Component-wise when T is a vector.
The result is e.
 fn bitcast<T>(e : S) -> T 
S is i32, u32, or f32 T is not S and is i32, u32, or f32Reinterpretation of bits as T.
The result is the reintepretation of bits in e as a T value.
 fn bitcast<vecN<T>>(e : vecN<S>) -> vecN<T> 
S is i32, u32, or f32 T is not S and is i32, u32, or f32Component-wise reinterpretation of bits as T.
The result is the reintepretation of bits in e as a vecN<T> value.
 fn bitcast<u32>(e : AbstractInt) -> u32 fn bitcast<vecN<u32>>(e : vecN<AbstractInt>) -> vecN<u32> 
The identity operation if e can be represented as u32, otherwise it produces a shader-creation error. That is, produces the same result as u32(e).

Component-wise when e is a vector.

 fn bitcast<T>(e : vec2<f16>) -> T 
T is i32, u32, or f32Component-wise reinterpretation of bits as T.
The result is the reintepretation of the 32 bits in e as a T value, following the internal layout rules.
 fn bitcast<vec2<T>>(e : vec4<f16>) -> vec2<T> 
T is i32, u32, or f32Component-wise reinterpretation of bits as T.
The result is the reintepretation of the 64 bits in e as a T value, following the internal layout rules.
 fn bitcast<vec2<f16>>(e : T) -> vec2<f16> 
T is i32, u32, or f32Component-wise reinterpretation of bits as f16.
The result is the reintepretation of the 32 bits in e as an f16 value, following the internal layout rules.
 fn bitcast<vec4<f16>>(e : vec2<T>) -> vec4<f16> 
T is i32, u32, or f32Component-wise reinterpretation of bits as vec2<f16>.
The result is the reintepretation of the 64 bits in e as an f16 value, following the internal layout rules.

Logical Built-in Functions

FunctionParameter TypesDescription
 fn all(e: vecN<bool>) -> bool 
Returns true if each component of e is true.
 fn all(e: bool) -> bool 
Returns e.
 fn any(e: vecN<bool>) -> bool 
Returns true if any component of e is true.
 fn any(e: bool) -> bool 
Returns e.
 fn select(f: T,
           t: T,
           cond: bool) -> T 
T is scalar or vectorReturns t when cond is true, and f otherwise.
 fn select(f: vecN<T>,
           t: vecN<T>,
           cond: vecN<bool>) -> vecN<T> 
T is scalarComponent-wise selection. Result component i is evaluated as select(f[i], t[i], cond[i]).

Array Built-in Functions

FunctionParameter TypesDescription
 fn arrayLength(p: ptr<storage, array<E>, AM>) -> u32 
E is an element type for a runtime-sized array, access mode AM is read or read_write Returns NRuntime, the number of elements in the runtime-sized array.

See § 13.3.4 Buffer Binding Determines Runtime-Sized Array Element Count

Numeric Built-in Functions

FunctionParameter TypesDescription
 fn abs(e: T ) -> T 
S is AbstractInt, AbstractFloat, i32, u32, f32, or f16 T is S, or vecN<S> The absolute value of e. Component-wise when T is a vector.

If e is a floating-point type, then the result is e with a positive sign bit. If e is an unsigned integer scalar type, then the result is e. If e is a signed integer scalar type and evaluates to the largest negative value, then the result is e.

 fn acos(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns the principal value, in radians, of the inverse cosine (cos-1) of e.
That is, approximates x with 0 ≤ x ≤ π, such that cos(x) = e.

Component-wise when T is a vector.

Interval [−1, 1]
 fn acosh(x: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns the inverse hyperbolic cosine (cosh-1) of x, as a hyperbolic angle.
That is, approximates a with 0 ≤ a ≤ +∞, such that cosh(a) = x.

Component-wise when T is a vector.

Interval [1, +∞]
 fn asin(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns the principal value, in radians, of the inverse sine (sin-1) of e.
That is, approximates x with -π/2 ≤ x ≤ π/2, such that sin(x) = e.

Component-wise when T is a vector.

Interval [−1, 1]
 fn asinh(y: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns the inverse hyperbolic sine (sinh-1) of y, as a hyperbolic angle.
That is, approximates a such that sinh(y) = a.

Component-wise when T is a vector.

 fn atan(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns the principal value, in radians, of the inverse tangent (tan-1) of e.
That is, approximates x with − π/2 ≤ x ≤ π/2, such that tan(x) = e.

Component-wise when T is a vector.

 fn atanh(t: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns the inverse hyperbolic tangent (tanh-1) of t, as a hyperbolic angle.
That is, approximates a such that tanh(a) = t.

Component-wise when T is a vector.

Interval [−1, 1]
 fn atan2(y: T,
          x: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns an angle, in radians, in the interval [-π, π] whose tangent is y÷x.

The quadrant selected by the result depends on the signs of y and x. For example, the function may be implemented as:

  • atan(y/x) when x > 0

  • atan(y/x) + π when (x < 0) and (y > 0)

  • atan(y/x) - π when (x < 0) and (y < 0)

Note: The error in the result is unbounded:
  • When abs(x) is very small, e.g. subnormal for its type,

  • At the origin (x,y) = (0,0), or

  • When y is subnormal or infinite.

Component-wise when T is a vector.

 fn ceil(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns the ceiling of e. Component-wise when T is a vector.
 fn clamp(e: T,
          low: T,
          high: T) -> T 
S is AbstractInt, AbstractFloat, i32, u32, f32, or f16 T is S, or vecN<S> Restricts the value of e within a range.

If T is an integer type, then the result is min(max(e, low), high).

If T is a floating-point type, then the result is either min(max(e, low), high), or the median of the three values e, low, high.

Component-wise when T is a vector.

If low is greater than high, then:

  • It is a shader-creation error if low and high are const-expressions.

  • It is a pipeline-creation error if low and high are override-expressions.

 fn cos(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns the cosine of e, where e is in radians. Component-wise when T is a vector. Interval (−∞, +∞)
 fn cosh(a: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns the hyperbolic cosine of a, where a is a hyperbolic angle. Approximates the pure mathematical function (ea + e−a)÷2, but not necessarily computed that way.

Component-wise when T is a vector

 fn countLeadingZeros(e: T) -> T 
T is i32, u32, vecN<i32>, or vecN<u32>The number of consecutive 0 bits starting from the most significant bit of e, when T is a scalar type.
Component-wise when T is a vector.
Also known as "clz" in some languages.
 fn countOneBits(e: T) -> T 
T is i32, u32, vecN<i32>, or vecN<u32>The number of 1 bits in the representation of e.
Also known as "population count".
Component-wise when T is a vector.
 fn countTrailingZeros(e: T) -> T 
T is i32, u32, vecN<i32>, or vecN<u32>The number of consecutive 0 bits starting from the least significant bit of e, when T is a scalar type.
Component-wise when T is a vector.
Also known as "ctz" in some languages.
 fn cross(a: vec3<T>,
          b: vec3<T>) -> vec3<T> 
T is AbstractFloat, f32, or f16Returns the cross product of e1 and e2. Implied from linear terms given by a possible implementation:
  • a[1] × b[2] − a[2] × b[1]

  • a[2] × b[0] − a[0] × b[2]

  • a[0] × b[1] − a[1] × b[0]

 fn degrees(e1: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Converts radians to degrees, approximating e1 × 180 ÷ π. Component-wise when T is a vector
 fn determinant(e: matCxC<T>) -> T 
T is AbstractFloat, f32, or f16Returns the determinant of e. Implied from linear terms in a standard mathematical definition of the determinant.
 fn distance(e1: T,
             e2: T) -> S 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns the distance between e1 and e2 (e.g. length(e1 - e2)).

The domain is all vectors (e1,e2) where the subtraction e1e2 is valid. That is, the set of all vectors except where e1[i] and e2[i] are the same infinite value, for some component i.

 fn dot(e1: vecN<T>,
        e2: vecN<T>) -> T 
T is AbstractInt, AbstractFloat, i32, u32, f32, or f16Returns the dot product of e1 and e2. Implied from linear terms of the sum over terms e1[i] × e2[i].
 fn dot4U8Packed(e1: u32,
                 e2: u32) -> u32 
e1 and e2 are interpreted as vectors with four 8-bit unsigned integer components. Return the unsigned integer dot product of these two vectors.
 fn dot4I8Packed(e1: u32,
                 e2: u32) -> i32 
e1 and e2 are interpreted as vectors with four 8-bit signed integer components. Return the signed integer dot product of these two vectors. Each component is sign-extended to i32 before performing the multiply, and then the add operations are done in WGSL i32 (the addition cannot overflow since the result is mathematically guaranteed to be in the range from -65024 to 65536, which lies within the range of numbers that can be represented by i32).
 fn exp(e1: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns the natural exponentiation of e1 (e.g. ee1). Component-wise when T is a vector.
 fn exp2(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns 2 raised to the power e (e.g. 2e). Component-wise when T is a vector.
 fn extractBits(e: T,
                offset: u32,
                count: u32) -> T 
T is i32 or vecN<i32> Reads bits from an integer, with sign extension.

When T is a scalar type, then:

  • w is the bit width of T
  • o = min(offset, w)
  • c = min(count, w - o)
  • The result is 0 if c is 0.
  • Otherwise, 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.
Component-wise when T is a vector.

If count + offset is greater than w, then:

  • It is a shader-creation error if count and offset are const-expressions.

  • It is a pipeline-creation error if count and offset are override-expressions.

 fn extractBits(e: T,
                offset: u32,
                count: u32) -> T 
T is u32 or vecN<u32> Reads bits from an integer, without sign extension.

When T is a scalar type, then:

  • w is the bit width of T
  • o = min(offset, w)
  • c = min(count, w - o)
  • The result is 0 if c is 0.
  • Otherwise, bits 0..c - 1 of the result are copied from bits o..o + c - 1 of e. Other bits of the result are 0.
Component-wise when T is a vector.

If count + offset is greater than w, then:

  • It is a shader-creation error if count and offset are const-expressions.

  • It is a pipeline-creation error if count and offset are override-expressions.

 fn faceForward(e1: T,
                e2: T,
                e3: T) -> T 
T is vecN<AbstractFloat>, vecN<f32>, or vecN<f16>Returns e1 if dot(e2, e3) is negative, and -e1 otherwise. The domain restrictions arise from the dot(e2,e3) operation: they are implied from linear terms of the sum over terms e2[i] × e3[i].
 fn firstLeadingBit(e: T) -> T 
T is i32 or vecN<i32> For scalar T, the result is:
  • -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.

Component-wise when T is a vector.

 fn firstLeadingBit(e: T) -> T 
T is u32 or vecN<u32> For scalar T, the result is:
  • T(-1) if e is zero.
  • Otherwise the position of the most significant 1 bit in e.
Component-wise when T is a vector.
 fn firstTrailingBit(e: T) -> T 
T is i32, u32, vecN<i32>, or vecN<u32> For scalar T, the result is:
  • T(-1) if e is zero.
  • Otherwise the position of the least significant 1 bit in e.
Component-wise when T is a vector.
 fn floor(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns the floor of e. Component-wise when T is a vector.
 fn fma(e1: T,
        e2: T,
        e3: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns e1 * e2 + e3. Component-wise when T is a vector.

Note: The name fma is short for "fused multiply add".

Note: The IEEE-754 fusedMultiplyAdd operation computes the intermediate results as if with unbounded range and precision, and only the final result is rounded to a value in the destination type. However, the § 15.7.4 Floating Point Accuracy rule for fma allows an implementation which performs an ordinary multiply to the target type followed by an ordinary addition. In this case the intermediate result values may overflow or lose accuracy, and the overall operation is not "fused" at all.

Implied from linear terms of the expressions e2 × e2 + e3.
 fn fract(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns the fractional part of e, computed as e - floor(e).
Component-wise when T is a vector.
 fn frexp(e: T) -> __frexp_result_f32 
T is f32 Splits e into a fraction and an exponent.
  • When e is zero, the fraction is zero.

  • When e is non-zero and normal, e = fraction * 2exponent, where the fraction is in the range [0.5, 1.0) or (-1.0, -0.5].

  • Otherwise, e is subnormal, NaN, or infinite. The result fraction and exponent are indeterminate values.

Returns the __frexp_result_f32 built-in structure, defined as follows:

struct __frexp_result_f32 {
  fract : f32, // fraction part
  exp : i32    // exponent part
}

Note: A mnemonic for the name frexp is "fraction and exponent".

 fn frexp(e: T) -> __frexp_result_f16 
T is f16 Splits e into a fraction and an exponent.
  • When e is zero, the fraction is zero.

  • When e is non-zero and normal, e = fraction * 2exponent, where the fraction is in the range [0.5, 1.0) or (-1.0, -0.5].

  • Otherwise, e is subnormal, NaN, or infinite. The result fraction and exponent are indeterminate values.

Returns the __frexp_result_f16 built-in structure, defined as if as follows:

struct __frexp_result_f16 {
  fract : f16, // fraction part
  exp : i32    // exponent part
}

Note: A mnemonic for the name frexp is "fraction and exponent".

 fn frexp(e: T) -> __frexp_result_abstract 
T is AbstractFloat Splits e into a fraction and an exponent.
  • When e is zero, the fraction is zero.

  • When e is non-zero and normal, e = fraction * 2exponent, where the fraction is in the range [0.5, 1.0) or (-1.0, -0.5].

  • When e is subnormal, the fraction and exponent are have unbounded error. The fraction may be any AbstractFloat value, and the exponent may be any AbstractInt value.

Note: AbstractFloat expressions resulting in infinity or NaN cause a shader-creation error.

Returns the __frexp_result_abstract built-in structure, defined as follows:

struct __frexp_result_abstract {
  fract : AbstractFloat, // fraction part
  exp : AbstractInt      // exponent part
}

Note: A mnemonic for the name frexp is "fraction and exponent".

 fn frexp(e: T) -> __frexp_result_vecN_f32 
T is vecN<f32> Splits components ei of e into a fraction and an exponent.
  • When ei is zero, the fraction is zero.

  • When ei is non-zero and normal, ei = fraction * 2exponent, where the fraction is in the range [0.5, 1.0) or (-1.0, -0.5].

  • Otherwise, ei is NaN or infinite. The result fraction and exponent are indeterminate values.

Returns the __frexp_result_vecN_f32 built-in structure, defined as follows:

struct __frexp_result_vecN_f32 {
  fract : vecN, // fraction part
  exp : vecN    // exponent part
}

Note: A mnemonic for the name frexp is "fraction and exponent".

 fn frexp(e: T) -> __frexp_result_vecN_f16 
T is vecN<f16> Splits components ei of e into a fraction and an exponent.
  • When ei is zero, the fraction is zero.

  • When ei is non-zero and normal, ei = fraction * 2exponent, where the fraction is in the range [0.5, 1.0) or (-1.0, -0.5].

  • Otherwise, ei is NaN or infinite. The result fraction and exponent are indeterminate values.

Returns the __frexp_result_vecN_f16 built-in structure, defined as if as follows:

struct __frexp_result_vecN_f16 {
  fract : vecN, // fraction part
  exp : vecN    // exponent part
}

Note: A mnemonic for the name frexp is "fraction and exponent".

 fn frexp(e: T) -> __frexp_result_vecN_abstract 
T is vecN<AbstractFloat> Splits components ei of e into a fraction and an exponent.
  • When ei is zero, the fraction is zero.

  • When ei is non-zero and normal, ei = fraction * 2exponent, where the fraction is in the range [0.5, 1.0) or (-1.0, -0.5].

  • When ei is subnormal, the fraction and exponent are have unbounded error. The fraction may be any AbstractFloat value, and the exponent may be any AbstractInt value.

Note: AbstractFloat expressions resulting in infinity or NaN cause a shader-creation error.

Returns the __frexp_result_vecN_abstract built-in structure, defined as follows:

struct __frexp_result_vecN_abstract {
  fract : vecN, // fraction part
  exp : vecN      // exponent part
}

Note: A mnemonic for the name frexp is "fraction and exponent".

 fn insertBits(e: T,
               newbits: T,
               offset: u32,
               count: u32) -> T 
T is i32, u32, vecN<i32>, or vecN<u32> Sets bits in an integer.

When T is a scalar type, then:

  • w is the bit width of T
  • o = min(offset, w)
  • c = min(count, w - o)
  • 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.
Component-wise when T is a vector.

If count + offset is greater than w, then:

  • It is a shader-creation error if count and offset are const-expressions.

  • It is a pipeline-creation error if count and offset are override-expressions.

 fn inverseSqrt(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns the reciprocal of sqrt(e). Component-wise when T is a vector. Interval [0, +∞]
 fn ldexp(e1: T,
          e2: I) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S> I is AbstractInt, i32, vecN<AbstractInt>, or vecN<i32> I is a vector if and only if T is a vector T can only be abstract if I is also abstract and vice versa Note: If either parameter is concrete then the other parameter will undergo automatic conversion to a concrete type (if applicable) and the result will be a concrete type. Returns e1 * 2e2, except:
  • The result may be zero if e2 + bias ≤ 0.

  • If e2 > bias + 1

    • It is a shader-creation error if e2 is a const-expression.

    • It is a pipeline-creation error if e2 is an override-expression.

    • Otherwise the result is an indeterminate value for T.

Here, bias is the exponent bias of the floating point format:

  • 15 for f16

  • 127 for f32

  • 1023 for AbstractFloat, when AbstractFloat is IEEE-754 binary64

If x is zero or a finite normal value for its type, then:

x = ldexp(frexp(x).fract, frexp(x).exp)

Component-wise when T is a vector.

Note: A mnemonic for the name ldexp is "load exponent". The name may have been taken from the corresponding instruction in the floating point unit of the PDP-11.

 fn length(e: T) -> S 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns the length of e.
Evaluates to the absolute value of e if T is scalar.
Evaluates to sqrt(e[0]2 + e[1]2 + ...) if T is a vector type.

Note: The scalar case may be evaluated as sqrt(e * e), which may unnecessarily overflow or lose accuracy.

 fn log(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns the natural logarithm of e. Component-wise when T is a vector. Interval [0, +∞]
 fn log2(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns the base-2 logarithm of e. Component-wise when T is a vector. Interval [0, +∞]
 fn max(e1: T,
        e2: T) -> T 
S is AbstractInt, AbstractFloat, i32, u32, f32, or f16 T is S, or vecN<S> Returns e2 if e1 is less than e2, and e1 otherwise. Component-wise when T is a vector.

If e1 and e2 are floating-point values, then:

  • If both e1 and e2 are subnormal, then the result may be either value.

 fn min(e1: T,
        e2: T) -> T 
S is AbstractInt, AbstractFloat, i32, u32, f32, or f16 T is S, or vecN<S> Returns e2 if e2 is less than e1, and e1 otherwise. Component-wise when T is a vector.

If e1 and e2 are floating-point values, then:

  • If both e1 and e2 are subnormal, then the result may be either value.

 fn mix(e1: T,
        e2: T,
        e3: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns the linear blend of e1 and e2 (e.g. e1 * (T(1) - e3) + e2 * e3). Component-wise when T is a vector. Implied from linear terms of the expressions: e1[i] × (1 − e3[i]) + e2[i] × e3[i]. e2[i] × e2[i] + e3[i].
 fn mix(e1: T2,
        e2: T2,
        e3: T) -> T2 
T is AbstractFloat, f32, or f16 T2 is vecN<T>Returns the component-wise linear blend of e1 and e2, using scalar blending factor e3 for each component.
Same as mix(e1, e2, T2(e3)). Implied from linear terms of the expressions: e1[i] × (1 − e3) + e2[i] × e3.
 fn modf(e: T) -> __modf_result_f32 
T is f32 Splits e into fractional and whole number parts.

The whole part is trunc(e), and the fractional part is e - trunc(e).

Returns the __modf_result_f32 built-in structure, defined as follows:

struct __modf_result_f32 {
  fract : f32, // fractional part
  whole : f32  // whole part
}
 fn modf(e: T) -> __modf_result_f16 
T is f16 Splits e into fractional and whole number parts.

The whole part is trunc(e), and the fractional part is e - trunc(e).

Returns the __modf_result_f16 built-in structure, defined as if as follows:

struct __modf_result_f16 {
  fract : f16, // fractional part
  whole : f16  // whole part
}
 fn modf(e: T) -> __modf_result_abstract 
T is AbstractFloat Splits e into fractional and whole number parts.

The whole part is trunc(e), and the fractional part is e - trunc(e).

Returns the __modf_result_abstract built-in structure, defined as follows:

struct __modf_result_abstract {
  fract : AbstractFloat, // fractional part
  whole : AbstractFloat  // whole part
}
 fn modf(e: T) -> __modf_result_vecN_f32 
T is vecN<f32> Splits the components of e into fractional and whole number parts.

The i’th component of the whole and fractional parts equal the whole and fractional parts of modf(e[i]).

Returns the __modf_result_vecN_f32 built-in structure, defined as follows:

struct __modf_result_vecN_f32 {
  fract : vecN, // fractional part
  whole : vecN  // whole part
}
 fn modf(e: T) -> __modf_result_vecN_f16 
T is vecN<f16> Splits the components of e into fractional and whole number parts.

The i’th component of the whole and fractional parts equal the whole and fractional parts of modf(e[i]).

Returns the __modf_result_vecN_f16 built-in structure, defined as if as follows:

struct __modf_result_vecN_f16 {
  fract : vecN, // fractional part
  whole : vecN  // whole part
}
 fn modf(e: T) -> __modf_result_vecN_abstract 
T is vecN<AbstractFloat> Splits the components of e into fractional and whole number parts.

The i’th component of the whole and fractional parts equal the whole and fractional parts of modf(e[i]).

Returns the __modf_result_vecN_abstract built-in structure, defined as follows:

struct __modf_result_vecN_abstract {
  fract : vecN, // fractional part
  whole : vecN  // whole part
}
 fn normalize(e: vecN<T> ) -> vecN<T> 
T is AbstractFloat, f32, or f16 Returns a unit vector in the same direction as e.

The domain is all vectors except the zero vector.

 fn pow(e1: T,
        e2: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns e1 raised to the power e2. Component-wise when T is a vector. The set of all pairs of extended reals (x,y) except:
  • x < 0.

  • x is 1 and y is infinite.

  • x is infinite and y is 0.

This rule arises from the fact the result may be computed as exp2(y * log2(x)).

 fn quantizeToF16(e: T) -> T 
T is f32 or vecN<f32> 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.

If e is outside the finite range of binary16, then:

  • It is a shader-creation error if e is a const-expression.

  • It is a pipeline-creation error if e is an override-expression.

  • Otherwise the result is an indeterminate value for T.

The intermediate binary16 value may be flushed to zero, i.e. the final result may be zero if the intermediate binary16 value is subnormal.

See § 15.7.6 Floating Point Conversion.

Component-wise when T is a vector.

 fn radians(e1: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Converts degrees to radians, approximating e1 × π ÷ 180. Component-wise when T is a vector
 fn reflect(e1: T,
            e2: T) -> T 
T is vecN<AbstractFloat>, vecN<f32>, or vecN<f16>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 
T is vecN<I> I is AbstractFloat, f32, or f16For 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. The incident vector e1 and the normal e2 should be normalized for desired results according to Snell’s Law; otherwise, the results may not conform to expected physical behavior.
 fn reverseBits(e: T) -> T 
T is i32, u32, vecN<i32>, or vecN<u32>Reverses the bits in e: The bit at position k of the result equals the bit at position 31 -k of e.
Component-wise when T is a vector.
 fn round(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Result is 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.
Component-wise when T is a vector.
 fn saturate(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns clamp(e, 0.0, 1.0). Component-wise when T is a vector.
 fn sign(e: T) -> T 
S is AbstractInt, AbstractFloat, i32, f32, or f16 T is S, or vecN<S> Result is:
  • 1 when e > 0
  • 0 when e = 0
  • -1 when e < 0

Component-wise when T is a vector.

 fn sin(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns the sine of e, where e is in radians. Component-wise when T is a vector. Interval (−∞, +∞)
 fn sinh(a: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns the hyperbolic sine of a, where a is a hyperbolic angle. Approximates the pure mathematical function (eae−a)÷2, but not necessarily computed that way.

Component-wise when T is a vector.

 fn smoothstep(edge0: T,
               edge1: T,
               x: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns the smooth Hermite interpolation between 0 and 1. Component-wise when T is a vector.

For scalar T, the result is t * t * (3.0 - 2.0 * t),
where t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0).

Qualitatively:

  • When edge0 < edge1, the function is 0 for x below edge0, then smoothly rises until x reaches edge1, and remains at 1 afterward.

  • When edge0 > edge1, the function is 1 for x below edge1, then smoothly descends until x reaches edge0, and remains at 0 afterward.

If edge0 = edge1:

  • It is a shader-creation error if edge0 and edge1 are const-expressions.

  • It is a pipeline-creation error if edge0 and edge1 are override-expressions.

  • Otherwise, the result is an indeterminate value for T. In this case the computation performs a floating point division by zero, and the Finite Math Assumption applies.

 fn sqrt(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns the square root of e. Component-wise when T is a vector. Interval [0, +∞]
 fn step(edge: T,
         x: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns 1.0 if edgex, and 0.0 otherwise. Component-wise when T is a vector.
 fn tan(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns the tangent of e, where e is in radians. Component-wise when T is a vector. Interval (−∞, +∞)
 fn tanh(a: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S> Returns the hyperbolic tangent of a, where a is a hyperbolic angle. Approximates the pure mathematical function (eae−a) ÷ (ea + e−a) but not necessarily computed that way.

Component-wise when T is a vector.

 fn transpose(e: matRxC<T>) -> matCxR<T> 
T is AbstractFloat, f32, or f16Returns the transpose of e.
 fn trunc(e: T) -> T 
S is AbstractFloat, f32, or f16 T is S or vecN<S>Returns truncate(e), the nearest whole number whose absolute value is less than or equal to the absolute value of e. Component-wise when T is a vector.

Derivative Built-in Functions

FunctionParameter TypesDescription
 fn dpdx(e: T) -> T 
T is f32 or vecN<f32> Partial derivative of e with respect to window x coordinates. The result is the same as either dpdxFine(e) or dpdxCoarse(e).

Returns an indeterminate value if called in non-uniform control flow.

 fn dpdxCoarse(e: T) -> T 
T is f32 or vecN<f32> Returns the partial derivative of e with respect to window x coordinates using local differences. This may result in fewer unique positions than dpdxFine(e).

Returns an indeterminate value if called in non-uniform control flow.

 fn dpdxFine(e: T) -> T 
T is f32 or vecN<f32> Returns the partial derivative of e with respect to window x coordinates.

Returns an indeterminate value if called in non-uniform control flow.

 fn dpdy(e: T) -> T 
T is f32 or vecN<f32> Partial derivative of e with respect to window y coordinates. The result is the same as either dpdyFine(e) or dpdyCoarse(e).

Returns an indeterminate value if called in non-uniform control flow.

 fn dpdyCoarse(e: T) -> T 
T is f32 or vecN<f32> Returns the partial derivative of e with respect to window y coordinates using local differences. This may result in fewer unique positions than dpdyFine(e).

Returns an indeterminate value if called in non-uniform control flow.

 fn dpdyFine(e: T) -> T 
T is f32 or vecN<f32> Returns the partial derivative of e with respect to window y coordinates.

Returns an indeterminate value if called in non-uniform control flow.

 fn fwidth(e: T) -> T 
T is f32 or vecN<f32> Returns abs(dpdx(e)) + abs(dpdy(e)).

Returns an indeterminate value if called in non-uniform control flow.

 fn fwidthCoarse(e: T) -> T 
T is f32 or vecN<f32> Returns abs(dpdxCoarse(e)) + abs(dpdyCoarse(e)).

Returns an indeterminate value if called in non-uniform control flow.

 fn fwidthFine(e: T) -> T 
T is f32 or vecN<f32> Returns abs(dpdxFine(e)) + abs(dpdyFine(e)).

Returns an indeterminate value if called in non-uniform control flow.

Texture Built-in Functions

FunctionParameter TypesDescription
textureDimensions

Returns the dimensions of a texture, or texture’s mip level in texels.

Parameters:

t The sampled, multisampled, depth, storage, or external texture.
level The mip level, with level 0 containing a full size version of the texture.
If omitted, the dimensions of level 0 are returned.

Returns:

The coordinate dimensions of the texture.

That is, the result provides the integer bounds on the coordinates of the logical texel address, excluding the mip level count, array size, and sample count.

For textures based on cubes, the results are the dimensions of each face of the cube. Cube faces are square, so the x and y components of the result are equal.

If level is outside the range [0, textureNumLevels(t)) then an indeterminate value for the return type may be returned.

 fn textureDimensions(t: T) -> u32 
ST is i32, u32, or f32 F is a texel format A is an access mode T is texture_1d<ST> or texture_storage_1d<F,A>
 fn textureDimensions(t: T,
                      level: L) -> u32 
ST is i32, u32, or f32 T is texture_1d<ST> L is i32, or u32
 fn textureDimensions(t: T) -> vec2<u32> 
ST is i32, u32, or f32 F is a texel format A is an access mode T is texture_2d<ST>, texture_2d_array<ST>, texture_cube<ST>, texture_cube_array<ST>, texture_multisampled_2d<ST>, texture_depth_2d, texture_depth_2d_array, texture_depth_cube, texture_depth_cube_array, texture_depth_multisampled_2d, texture_storage_2d<F,A>, texture_storage_2d_array<F,A>, or texture_external
 fn textureDimensions(t: T,
                      level: L) -> vec2<u32> 
ST is i32, u32, or f32 T is texture_2d<ST>, texture_2d_array<ST>, texture_cube<ST>, texture_cube_array<ST>, texture_depth_2d, texture_depth_2d_array, texture_depth_cube, or texture_depth_cube_array L is i32, or u32
 fn textureDimensions(t: T) -> vec3<u32> 
ST is i32, u32, or f32 F is a texel format A is an access mode T is texture_3d<ST> or texture_storage_3d<F,A>
 fn textureDimensions(t: T,
                      level: L) -> vec3<u32> 
ST is i32, u32, or f32 T is texture_3d<ST> L is i32, or u32
textureGather

A texture gather operation reads from a 2D, 2D array, cube, or cube array texture, computing a four-component vector as follows:

  • Find the four texels that would be used in a sampling operation with linear filtering, from mip level 0:

    • Use the specified coordinate, array index (when present), and offset (when present).

    • The texels are adjacent, forming a square, when considering their texture space coordinates (u,v).

    • Selected texels at the texture edge, cube face edge, or cube corners are handled as in ordinary texture sampling.

  • For each texel, read one channel and convert it into a scalar value.

    • For non-depth textures, a zero-based component parameter specifies the channel to use.

      • If the texture format supports the specified channel, i.e. has more than component channels:

        • Yield scalar value v[component] when the texel value is v.

      • Otherwise:

        • Yield 0.0 when component is 1 or 2.

        • Yield 1.0 when component is 3 (the alpha channel).

    • For depth textures, yield the texel value. (Depth textures only have one channel.)

  • Yield the four-component vector, arranging scalars produced by the previous step into components according to the relative coordinates of the texels, as follows:

    • Result component Relative texel coordinate
      x (umin,vmax)
      y (umax,vmax)
      z (umax,vmin)
      w (umin,vmin)

The four texels form the sampled area as described in the WebGPU sampler descriptor.

Parameters:

component Only applies to non-depth textures.
The index of the channel to read from the selected texels.
When provided, the component expression must be a const-expression (e.g. 1).
Its value must be at least 0 and at most 3. Values outside of this range will result in a shader-creation error.
t The sampled or depth texture to read from.
s The sampler type.
coords The texture coordinates.
array_index The 0-based texture array index.
This value will be clamped to the range [0, textureNumLayers(t) - 1].
offset The optional texel offset applied to the unnormalized texture coordinate before sampling the texture. This offset is applied before applying any texture wrapping modes.
The offset expression must be a const-expression (e.g. vec2<i32>(1, 2)).
Each offset component must be at least -8 and at most 7. Values outside of this range will result in a shader-creation error.

Returns:

A four component vector with components extracted from the specified channel from the selected texels, as described above.

 fn textureGather(component: C,
                  t: texture_2d<ST>,
                  s: sampler,
                  coords: vec2<f32>) -> vec4<ST> 
C is i32, or u32 ST is i32, u32, or f32
 fn textureGather(component: C,
                  t: texture_2d<ST>,
                  s: sampler,
                  coords: vec2<f32>,
                  offset: vec2<i32>) -> vec4<ST> 
C is i32, or u32 ST is i32, u32, or f32
 fn textureGather(component: C,
                  t: texture_2d_array<ST>,
                  s: sampler,
                  coords: vec2<f32>,
                  array_index: A) -> vec4<ST> 
C is i32, or u32 A is i32, or u32 ST is i32, u32, or f32
 fn textureGather(component: C,
                  t: texture_2d_array<ST>,
                  s: sampler,
                  coords: vec2<f32>,
                  array_index: A,
                  offset: vec2<i32>) -> vec4<ST> 
C is i32, or u32 A is i32, or u32 ST is i32, u32, or f32
 fn textureGather(component: C,
                  t: texture_cube<ST>,
                  s: sampler,
                  coords: vec3<f32>) -> vec4<ST> 
C is i32, or u32 ST is i32, u32, or f32
 fn textureGather(component: C,
                  t: texture_cube_array<ST>,
                  s: sampler,
                  coords: vec3<f32>,
                  array_index: A) -> vec4<ST> 
C is i32, or u32 A is i32, or u32 ST is i32, u32, or f32
 fn textureGather(t: texture_depth_2d,
                  s: sampler,
                  coords: vec2<f32>) -> vec4<f32> 
 fn textureGather(t: texture_depth_2d,
                  s: sampler,
                  coords: vec2<f32>,
                  offset: vec2<i32>) -> vec4<f32> 
 fn textureGather(t: texture_depth_cube,
                  s: sampler,
                  coords: vec3<f32>) -> vec4<f32> 
 fn textureGather(t: texture_depth_2d_array,
                  s: sampler,
                  coords: vec2<f32>,
                  array_index: A) -> vec4<f32> 
A is i32, or u32
 fn textureGather(t: texture_depth_2d_array,
                  s: sampler,
                  coords: vec2<f32>,
                  array_index: A,
                  offset: vec2<i32>) -> vec4<f32> 
A is i32, or u32
 fn textureGather(t: texture_depth_cube_array,
                  s: sampler,
                  coords: vec3<f32>,
                  array_index: A) -> vec4<f32> 
A is i32, or u32
textureGatherCompare

A texture gather compare operation performs a depth comparison on four texels in a depth texture and collects the results into a single vector, as follows:

  • Find the four texels that would be used in a depth sampling operation with linear filtering, from mip level 0:

    • Use the specified coordinate, array index (when present), and offset (when present).

    • The texels are adjacent, forming a square, when considering their texture space coordinates (u,v).

    • Selected texels at the texture edge, cube face edge, or cube corners are handled as in ordinary texture sampling.

  • For each texel, perform a comparison against the depth reference value, yielding a 0.0 or 1.0 value, as controlled by the comparison sampler parameters.

  • Yield the four-component vector where the components are the comparison results with the texels with relative texel coordinates as follows:

    • Result component Relative texel coordinate
      x (umin,vmax)
      y (umax,vmax)
      z (umax,vmin)
      w (umin,vmin)

Parameters:

t The depth texture to read from.
s The sampler comparison.
coords The texture coordinates.
array_index The 0-based texture array index.
This value will be clamped to the range [0, textureNumLayers(t) - 1].
depth_ref The reference value to compare the sampled depth value against.
offset The optional texel offset applied to the unnormalized texture coordinate before sampling the texture. This offset is applied before applying any texture wrapping modes.
The offset expression must be a const-expression (e.g. vec2<i32>(1, 2)).
Each offset component must be at least -8 and at most 7. Values outside of this range will result in a shader-creation error.

Returns:

A four component vector with comparison result for the selected texels, as described above.

 fn textureGatherCompare(t: texture_depth_2d,
                         s: sampler_comparison,
                         coords: vec2<f32>,
                         depth_ref: f32) -> vec4<f32> 
 fn textureGatherCompare(t: texture_depth_2d,
                         s: sampler_comparison,
                         coords: vec2<f32>,
                         depth_ref: f32,
                         offset: vec2<i32>) -> vec4<f32> 
 fn textureGatherCompare(t: texture_depth_2d_array,
                         s: sampler_comparison,
                         coords: vec2<f32>,
                         array_index: A,
                         depth_ref: f32) -> vec4<f32> 
A is i32, or u32
 fn textureGatherCompare(t: texture_depth_2d_array,
                         s: sampler_comparison,
                         coords: vec2<f32>,
                         array_index: A,
                         depth_ref: f32,
                         offset: vec2<i32>) -> vec4<f32> 
A is i32, or u32
 fn textureGatherCompare(t: texture_depth_cube,
                         s: sampler_comparison,
                         coords: vec3<f32>,
                         depth_ref: f32) -> vec4<f32> 
 fn textureGatherCompare(t: texture_depth_cube_array,
                         s: sampler_comparison,
                         coords: vec3<f32>,
                         array_index: A,
                         depth_ref: f32) -> vec4<f32> 
A is i32, or u32
textureLoad

Reads a single texel from a texture without sampling or filtering.

Parameters:

t The sampled, multisampled, depth, storage, or external texture
coords The 0-based texel coordinate.
array_index The 0-based texture array index.
level The mip level, with level 0 containing a full size version of the texture.
sample_index The 0-based sample index of the multisampled texture.

Returns:

The unfiltered texel data.

The logical texel address is invalid if:

  • any element of coords is outside the range [0, textureDimensions(t, level)) for the corresponding element, or

  • array_index is outside the range [0, textureNumLayers(t)), or

  • level is outside the range [0, textureNumLevels(t)), or

  • sample_index is outside the range [0, textureNumSamples(s))

If the logical texel address is invalid, the built-in function returns one of:

  • The data for some texel within bounds of the texture.

  • For non-depth textures, either:

    • The vector (0,0,0,0) or (0,0,0,1) of the appropriate type, with the texture view swizzle applied, or

    • The vector (0,0,0,0) or (0,0,0,1) of the appropriate type, without the texture view swizzle applied.

  • 0.0 for depth textures.

 fn textureLoad(t: texture_1d<ST>,
                coords: C,
                level: L) -> vec4<ST> 
C is i32, or u32 L is i32, or u32 ST is i32, u32, or f32
 fn textureLoad(t: texture_2d<ST>,
                coords: vec2<C>,
                level: L) -> vec4<ST> 
C is i32, or u32 L is i32, or u32 ST is i32, u32, or f32
 fn textureLoad(t: texture_2d_array<ST>,
                coords: vec2<C>,
                array_index: A,
                level: L) -> vec4<ST> 
C is i32, or u32 A is i32, or u32 L is i32, or u32 ST is i32, u32, or f32
 fn textureLoad(t: texture_3d<ST>,
                coords: vec3<C>,
                level: L) -> vec4<ST> 
C is i32, or u32 L is i32, or u32 ST is i32, u32, or f32
 fn textureLoad(t: texture_multisampled_2d<ST>,
                coords: vec2<C>,
                sample_index: S)-> vec4<ST> 
C is i32, or u32 S is i32, or u32 ST is i32, u32, or f32
 fn textureLoad(t: texture_depth_2d,
                coords: vec2<C>,
                level: L) -> f32 
C is i32, or u32 L is i32, or u32
 fn textureLoad(t: texture_depth_2d_array,
                coords: vec2<C>,
                array_index: A,
                level: L) -> f32 
C is i32, or u32 A is i32, or u32 L is i32, or u32
 fn textureLoad(t: texture_depth_multisampled_2d,
                coords: vec2<C>,
                sample_index: S)-> f32 
C is i32, or u32 S is i32, or u32
 fn textureLoad(t: texture_external,
                coords: vec2<C>) -> vec4<f32> 
C is i32, or u32
 fn textureLoad(t : texture_storage_1d<F, AM>,
                coords : C) -> vec4<CF> 
C is i32, or u32 AM is read or read_write CF depends on the storage texel format F. See the texel format table for the mapping of texel format to channel format.
 fn textureLoad(t : texture_storage_2d<F, AM>,
                coords : vec2<C>) -> vec4<CF> 
C is i32, or u32 AM is read or read_write CF depends on the storage texel format F. See the texel format table for the mapping of texel format to channel format.
 fn textureLoad(t : texture_storage_2d_array<F, AM>,
                coords : vec2<C>,
                array_index : A) -> vec4<CF> 
C is i32, or u32 AM is read or read_write A is i32 or u32 CF depends on the storage texel format F. See the texel format table for the mapping of texel format to channel format.
 fn textureLoad(t : texture_storage_3d<F, AM>,
                coords : vec3<C>) -> vec4<CF> 
C is i32, or u32 AM is read or read_write CF depends on the storage texel format F. See the texel format table for the mapping of texel format to channel format.
textureNumLayers

Returns the number of layers (elements) of an arrayed texture.

Parameters:

t The sampled, depth, or storage texture array texture.

Returns:

If the texture is based on cubes, returns the number of cubes in the cube arrayed texture.

Otherwise returns the number of layers (homogeneous grids of texels) in the arrayed texture.

 fn textureNumLayers(t: T) -> u32 
F is a texel format A is an access mode ST is i32, u32, or f32 T is texture_2d_array<ST>, texture_cube_array<ST>, texture_depth_2d_array, texture_depth_cube_array, or texture_storage_2d_array<F,A>
textureNumLevels

Returns the number of mip levels of a texture.

Parameters:

t The sampled or depth texture.

Returns:

The mip level count for the texture.

 fn textureNumLevels(t: T) -> u32 
ST is i32, u32, or f32 T is texture_1d<ST>, texture_2d<ST>, texture_2d_array<ST>, texture_3d<ST>, texture_cube<ST>, texture_cube_array<ST>, texture_depth_2d, texture_depth_2d_array, texture_depth_cube, or texture_depth_cube_array
textureNumSamples

Returns the number samples per texel in a multisampled texture.

Parameters:

t The multisampled texture.

Returns:

The sample count for the multisampled texture.

 fn textureNumSamples(t: T) -> u32 
ST is i32, u32, or f32 T is texture_multisampled_2d<ST> or texture_depth_multisampled_2d
textureSample

Samples a texture.

Must only be used in a fragment shader stage.

If uniformity analysis cannot prove a call to this function is in uniform control flow, then a derivative_uniformity diagnostic is triggered.

Parameters:

t The sampled or depth texture to sample.
s The sampler type.
coords The texture coordinates used for sampling.
array_index The 0-based texture array index to sample.
This value will be clamped to the range [0, textureNumLayers(t) - 1].
offset The optional texel offset applied to the unnormalized texture coordinate before sampling the texture. This offset is applied before applying any texture wrapping modes.
The offset expression must be a const-expression (e.g. vec2<i32>(1, 2)).
Each offset component must be at least -8 and at most 7. Values outside of this range will result in a shader-creation error.

Returns:

The sampled value.

An indeterminate value results if called in non-uniform control flow.

 fn textureSample(t: texture_1d<f32>,
                  s: sampler,
                  coords: f32) -> vec4<f32> 
 fn textureSample(t: texture_2d<f32>,
                  s: sampler,
                  coords: vec2<f32>) -> vec4<f32> 
 fn textureSample(t: texture_2d<f32>,
                  s: sampler,
                  coords: vec2<f32>,
                  offset: vec2<i32>) -> vec4<f32> 
 fn textureSample(t: texture_2d_array<f32>,
                  s: sampler,
                  coords: vec2<f32>,
                  array_index: A) -> vec4<f32> 
A is i32, or u32
 fn textureSample(t: texture_2d_array<f32>,
                  s: sampler,
                  coords: vec2<f32>,
                  array_index: A,
                  offset: vec2<i32>) -> vec4<f32> 
A is i32, or u32
 fn textureSample(t: T,
                  s: sampler,
                  coords: vec3<f32>) -> vec4<f32> 
T is texture_3d<f32>, or texture_cube<f32>
 fn textureSample(t: texture_3d<f32>,
                  s: sampler,
                  coords: vec3<f32>,
                  offset: vec3<i32>) -> vec4<f32> 
 fn textureSample(t: texture_cube_array<f32>,
                  s: sampler,
                  coords: vec3<f32>,
                  array_index: A) -> vec4<f32> 
A is i32, or u32
 fn textureSample(t: texture_depth_2d,
                  s: sampler,
                  coords: vec2<f32>) -> f32 
 fn textureSample(t: texture_depth_2d,
                  s: sampler,
                  coords: vec2<f32>,
                  offset: vec2<i32>) -> f32 
 fn textureSample(t: texture_depth_2d_array,
                  s: sampler,
                  coords: vec2<f32>,
                  array_index: A) -> f32 
A is i32, or u32
 fn textureSample(t: texture_depth_2d_array,
                  s: sampler,
                  coords: vec2<f32>,
                  array_index: A,
                  offset: vec2<i32>) -> f32 
A is i32, or u32
 fn textureSample(t: texture_depth_cube,
                  s: sampler,
                  coords: vec3<f32>) -> f32 
 fn textureSample(t: texture_depth_cube_array,
                  s: sampler,
                  coords: vec3<f32>,
                  array_index: A) -> f32 
A is i32, or u32
textureSampleBias

Samples a texture with a bias to the mip level.

Must only be used in a fragment shader stage.

If uniformity analysis cannot prove a call to this function is in uniform control flow, then a derivative_uniformity diagnostic is triggered.

Parameters:

t The sampled texture to sample.
s The sampler type.
coords The texture coordinates used for sampling.
array_index The 0-based texture array index to sample.
This value will be clamped to the range [0, textureNumLayers(t) - 1].
bias The bias to apply to the mip level before sampling.
This value will be clamped in the range [-16.0, 15.99].
offset The optional texel offset applied to the unnormalized texture coordinate before sampling the texture. This offset is applied before applying any texture wrapping modes.
The offset expression must be a const-expression (e.g. vec2<i32>(1, 2)).
Each offset component must be at least -8 and at most 7. Values outside of this range will result in a shader-creation error.

Returns:

The sampled value.

 fn textureSampleBias(t: texture_2d<f32>,
                      s: sampler,
                      coords: vec2<f32>,
                      bias: f32) -> vec4<f32> 
 fn textureSampleBias(t: texture_2d<f32>,
                      s: sampler,
                      coords: vec2<f32>,
                      bias: f32,
                      offset: vec2<i32>) -> vec4<f32> 
 fn textureSampleBias(t: texture_2d_array<f32>,
                      s: sampler,
                      coords: vec2<f32>,
                      array_index: A,
                      bias: f32) -> vec4<f32> 
A is i32, or u32
 fn textureSampleBias(t: texture_2d_array<f32>,
                      s: sampler,
                      coords: vec2<f32>,
                      array_index: A,
                      bias: f32,
                      offset: vec2<i32>) -> vec4<f32> 
A is i32, or u32
 fn textureSampleBias(t: T,
                      s: sampler,
                      coords: vec3<f32>,
                      bias: f32) -> vec4<f32> 
T is texture_3d<f32>, or texture_cube<f32>
 fn textureSampleBias(t: texture_3d<f32>,
                      s: sampler,
                      coords: vec3<f32>,
                      bias: f32,
                      offset: vec3<i32>) -> vec4<f32> 
 fn textureSampleBias(t: texture_cube_array<f32>,
                      s: sampler,
                      coords: vec3<f32>,
                      array_index: A,
                      bias: f32) -> vec4<f32> 
A is i32, or u32
textureSampleCompare

Samples a depth texture and compares the sampled depth values against a reference value.

Must only be used in a fragment shader stage.

If uniformity analysis cannot prove a call to this function is in uniform control flow, then a derivative_uniformity diagnostic is triggered.

Parameters:

t The depth texture to sample.
s The sampler_comparison type.
coords The texture coordinates used for sampling.
array_index The 0-based texture array index to sample.
This value will be clamped to the range [0, textureNumLayers(t) - 1].
depth_ref The reference value to compare the sampled depth value against.
offset The optional texel offset applied to the unnormalized texture coordinate before sampling the texture. This offset is applied before applying any texture wrapping modes.
The offset expression must be a const-expression (e.g. vec2<i32>(1, 2)).
Each offset component must be at least -8 and at most 7. Values outside of this range will result in a shader-creation error.

Returns:

A value in the range [0.0..1.0].

Each sampled texel is compared against the reference value using the comparison operator defined by the sampler_comparison, resulting in either a 0 or 1 value for each texel.

If the sampler uses bilinear filtering then the returned value is the filtered average of these values, otherwise the comparison result of a single texel is returned.

An indeterminate value results if called in non-uniform control flow.

 fn textureSampleCompare(t: texture_depth_2d,
                         s: sampler_comparison,
                         coords: vec2<f32>,
                         depth_ref: f32) -> f32 
 fn textureSampleCompare(t: texture_depth_2d,
                         s: sampler_comparison,
                         coords: vec2<f32>,
                         depth_ref: f32,
                         offset: vec2<i32>) -> f32 
 fn textureSampleCompare(t: texture_depth_2d_array,
                         s: sampler_comparison,
                         coords: vec2<f32>,
                         array_index: A,
                         depth_ref: f32) -> f32 
A is i32, or u32
 fn textureSampleCompare(t: texture_depth_2d_array,
                         s: sampler_comparison,
                         coords: vec2<f32>,
                         array_index: A,
                         depth_ref: f32,
                         offset: vec2<i32>) -> f32 
A is i32, or u32
 fn textureSampleCompare(t: texture_depth_cube,
                         s: sampler_comparison,
                         coords: vec3<f32>,
                         depth_ref: f32) -> f32 
 fn textureSampleCompare(t: texture_depth_cube_array,
                         s: sampler_comparison,
                         coords: vec3<f32>,
                         array_index: A,
                         depth_ref: f32) -> f32 
A is i32, or u32
textureSampleCompareLevel

Samples a depth texture and compares the sampled depth values against a reference value.

Parameters:

t The depth texture to sample.
s The sampler_comparison type.
coords The texture coordinates used for sampling.
array_index The 0-based texture array index to sample.
This value will be clamped to the range [0, textureNumLayers(t) - 1].
depth_ref The reference value to compare the sampled depth value against.
offset The optional texel offset applied to the unnormalized texture coordinate before sampling the texture. This offset is applied before applying any texture wrapping modes.
The offset expression must be a const-expression (e.g. vec2<i32>(1, 2)).
Each offset component must be at least -8 and at most 7. Values outside of this range will result in a shader-creation error.

Returns:

A value in the range [0.0..1.0].

The textureSampleCompareLevel function is the same as textureSampleCompare, except that:

  • textureSampleCompareLevel always samples texels from mip level 0.

    • The function does not compute derivatives.

    • There is no requirement for textureSampleCompareLevel to be invoked in uniform control flow.

  • textureSampleCompareLevel may be invoked in any shader stage.

 fn textureSampleCompareLevel(t: texture_depth_2d,
                              s: sampler_comparison,
                              coords: vec2<f32>,
                              depth_ref: f32) -> f32 
 fn textureSampleCompareLevel(t: texture_depth_2d,
                              s: sampler_comparison,
                              coords: vec2<f32>,
                              depth_ref: f32,
                              offset: vec2<i32>) -> f32 
 fn textureSampleCompareLevel(t: texture_depth_2d_array,
                              s: sampler_comparison,
                              coords: vec2<f32>,
                              array_index: A,
                              depth_ref: f32) -> f32 
A is i32, or u32
 fn textureSampleCompareLevel(t: texture_depth_2d_array,
                              s: sampler_comparison,
                              coords: vec2<f32>,
                              array_index: A,
                              depth_ref: f32,
                              offset: vec2<i32>) -> f32 
A is i32, or u32
 fn textureSampleCompareLevel(t: texture_depth_cube,
                              s: sampler_comparison,
                              coords: vec3<f32>,
                              depth_ref: f32) -> f32 
 fn textureSampleCompareLevel(t: texture_depth_cube_array,
                              s: sampler_comparison,
                              coords: vec3<f32>,
                              array_index: A,
                              depth_ref: f32) -> f32 
A is i32, or u32
textureSampleGrad

Samples a texture using explicit gradients.

Parameters:

t The sampled texture to sample.
s The sampler.
coords The texture coordinates used for sampling.
array_index The 0-based texture array index to sample.
This value will be clamped to the range [0, textureNumLayers(t) - 1].
ddx The x direction derivative vector used to compute the sampling locations.
ddy The y direction derivative vector used to compute the sampling locations.
offset The optional texel offset applied to the unnormalized texture coordinate before sampling the texture. This offset is applied before applying any texture wrapping modes.
The offset expression must be a const-expression (e.g. vec2<i32>(1, 2)).
Each offset component must be at least -8 and at most 7. Values outside of this range will result in a shader-creation error.

Returns:

The sampled value.

 fn textureSampleGrad(t: texture_2d<f32>,
                      s: sampler,
                      coords: vec2<f32>,
                      ddx: vec2<f32>,
                      ddy: vec2<f32>) -> vec4<f32> 
 fn textureSampleGrad(t: texture_2d<f32>,
                      s: sampler,
                      coords: vec2<f32>,
                      ddx: vec2<f32>,
                      ddy: vec2<f32>,
                      offset: vec2<i32>) -> vec4<f32> 
 fn textureSampleGrad(t: texture_2d_array<f32>,
                      s: sampler,
                      coords: vec2<f32>,
                      array_index: A,
                      ddx: vec2<f32>,
                      ddy: vec2<f32>) -> vec4<f32> 
A is i32, or u32
 fn textureSampleGrad(t: texture_2d_array<f32>,
                      s: sampler,
                      coords: vec2<f32>,
                      array_index: A,
                      ddx: vec2<f32>,
                      ddy: vec2<f32>,
                      offset: vec2<i32>) -> vec4<f32> 
A is i32, or u32
 fn textureSampleGrad(t: T,
                      s: sampler,
                      coords: vec3<f32>,
                      ddx: vec3<f32>,
                      ddy: vec3<f32>) -> vec4<f32> 
T is texture_3d<f32>, or texture_cube<f32>
 fn textureSampleGrad(t: texture_3d<f32>,
                      s: sampler,
                      coords: vec3<f32>,
                      ddx: vec3<f32>,
                      ddy: vec3<f32>,
                      offset: vec3<i32>) -> vec4<f32> 
 fn textureSampleGrad(t: texture_cube_array<f32>,
                      s: sampler,
                      coords: vec3<f32>,
                      array_index: A,
                      ddx: vec3<f32>,
                      ddy: vec3<f32>) -> vec4<f32> 
A is i32, or u32
textureSampleLevel

Samples a texture using an explicit mip level.

Parameters:

t The sampled or depth texture to sample.
s The sampler type.
coords The texture coordinates used for sampling.
array_index The 0-based texture array index to sample.
This value will be clamped to the range [0, textureNumLayers(t) - 1].
level The mip level, with level 0 containing a full size version of the texture. For the functions where level is a f32, fractional values may interpolate between two levels if the format is filterable according to the Texture Format Capabilities.
offset The optional texel offset applied to the unnormalized texture coordinate before sampling the texture. This offset is applied before applying any texture wrapping modes.
The offset expression must be a const-expression (e.g. vec2<i32>(1, 2)).
Each offset component must be at least -8 and at most 7. Values outside of this range will result in a shader-creation error.

Returns:

The sampled value.

 fn textureSampleLevel(t: texture_1d<f32>,
                       s: sampler,
                       coords: f32,
                       level: f32) -> vec4<f32> 
 fn textureSampleLevel(t: texture_2d<f32>,
                       s: sampler,
                       coords: vec2<f32>,
                       level: f32) -> vec4<f32> 
 fn textureSampleLevel(t: texture_2d<f32>,
                       s: sampler,
                       coords: vec2<f32>,
                       level: f32,
                       offset: vec2<i32>) -> vec4<f32> 
 fn textureSampleLevel(t: texture_2d_array<f32>,
                       s: sampler,
                       coords: vec2<f32>,
                       array_index: A,
                       level: f32) -> vec4<f32> 
A is i32, or u32
 fn textureSampleLevel(t: texture_2d_array<f32>,
                       s: sampler,
                       coords: vec2<f32>,
                       array_index: A,
                       level: f32,
                       offset: vec2<i32>) -> vec4<f32> 
A is i32, or u32
 fn textureSampleLevel(t: T,
                       s: sampler,
                       coords: vec3<f32>,
                       level: f32) -> vec4<f32> 
T is texture_3d<f32>, or texture_cube<f32>
 fn textureSampleLevel(t: texture_3d<f32>,
                       s: sampler,
                       coords: vec3<f32>,
                       level: f32,
                       offset: vec3<i32>) -> vec4<f32> 
 fn textureSampleLevel(t: texture_cube_array<f32>,
                       s: sampler,
                       coords: vec3<f32>,
                       array_index: A,
                       level: f32) -> vec4<f32> 
A is i32, or u32
 fn textureSampleLevel(t: texture_depth_2d,
                       s: sampler,
                       coords: vec2<f32>,
                       level: L) -> f32 
L is i32, or u32
 fn textureSampleLevel(t: texture_depth_2d,
                       s: sampler,
                       coords: vec2<f32>,
                       level: L,
                       offset: vec2<i32>) -> f32 
L is i32, or u32
 fn textureSampleLevel(t: texture_depth_2d_array,
                       s: sampler,
                       coords: vec2<f32>,
                       array_index: A,
                       level: L) -> f32 
A is i32, or u32 L is i32, or u32
 fn textureSampleLevel(t: texture_depth_2d_array,
                       s: sampler,
                       coords: vec2<f32>,
                       array_index: A,
                       level: L,
                       offset: vec2<i32>) -> f32 
A is i32, or u32 L is i32, or u32
 fn textureSampleLevel(t: texture_depth_cube,
                       s: sampler,
                       coords: vec3<f32>,
                       level: L) -> f32 
L is i32, or u32
 fn textureSampleLevel(t: texture_depth_cube_array,
                       s: sampler,
                       coords: vec3<f32>,
                       array_index: A,
                       level: L) -> f32 
A is i32, or u32 L is i32, or u32
textureSampleBaseClampToEdge

Samples a texture view at its base level, with texture coordinates clamped to the edge as described below.

Parameters:

t The sampled or external texture to sample.
s The sampler type.
coords The texture coordinates used for sampling.

Before sampling, the given coordinates will be clamped to the rectangle

[ half_texel, 1 - half_texel ]

where

half_texel = vec2(0.5) / vec2<f32>(textureDimensions(t))

Note: The half-texel adjustment ensures that, independent of the sampler’s addressing and filter modes, wrapping will not occur. That is, when sampling near an edge, the sampled texels will be at or adjacent to that edge, and not selected from the opposite edge.

Returns:

The sampled value.

 fn textureSampleBaseClampToEdge(t: T,
                                 s: sampler,
                                 coords: vec2<f32>) -> vec4<f32> 
T is texture_2d<f32> or texture_external
textureStore

Writes a single texel to a texture.

Parameters:

t The write-only storage texture or read-write storage texture
coords The 0-based texel coordinate.
array_index The 0-based texture array index.
value The new texel value. value is converted using the inverse channel transfer function.

Note:

The logical texel address is invalid if:

  • any element of coords is outside the range [0, textureDimensions(t)) for the corresponding element, or

  • array_index is outside the range of [0, textureNumLayers(t))

If the logical texel address is invalid, the built-in function will not be executed.

 fn textureStore(t: texture_storage_1d<F,AM>,
                 coords: C,
                 value: vec4<CF>) 
F is a texel format C is i32, or u32 AM is write or read_write CF depends on the storage texel format F. See the texel format table for the mapping of texel format to channel format.
 fn textureStore(t: texture_storage_2d<F,AM>,
                 coords: vec2<C>,
                 value: vec4<CF>) 
F is a texel format C is i32, or u32 AM is write or read_write CF depends on the storage texel format F. See the texel format table for the mapping of texel format to channel format.
 fn textureStore(t: texture_storage_2d_array<F,AM>,
                 coords: vec2<C>,
                 array_index: A,
                 value: vec4<CF>) 
F is a texel format C is i32, or u32 AM is write or read_write A is i32, or u32 CF depends on the storage texel format F. See the texel format table for the mapping of texel format to channel format.
 fn textureStore(t: texture_storage_3d<F,AM>,
                 coords: vec3<C>,
                 value: vec4<CF>) 
F is a texel format C is i32, or u32 AM is write or read_write CF depends on the storage texel format F. See the texel format table for the mapping of texel format to channel format.

Atomic Built-in Functions

FunctionParameter TypesDescription
fn atomicLoad(atomic_ptr: ptr<AS, atomic<T>, read_write>) -> T 

Returns the atomically loaded the value pointed to by atomic_ptr. It does not modify the object.

fn atomicStore(atomic_ptr: ptr<AS, atomic<T>, read_write>,
               v: T) 

Atomically stores the value v in the atomic object pointed to by atomic_ptr.

fn atomicAdd(atomic_ptr: ptr<AS, atomic<T>, read_write>,
             v: T) -> T 

Atomically performs an addition operation on the atomic object pointed to by atomic_ptr with the value v, and returns the original value stored in the atomic object before the operation.

fn atomicSub(atomic_ptr: ptr<AS, atomic<T>, read_write>,
             v: T) -> T 

Atomically performs a subtraction operation on the atomic object pointed to by atomic_ptr with the value v, and returns the original value stored in the atomic object before the operation.

fn atomicMax(atomic_ptr: ptr<AS, atomic<T>, read_write>,
             v: T) -> T 

Atomically performs a maximum operation on the atomic object pointed to by atomic_ptr with the value v, and returns the original value stored in the atomic object before the operation.

fn atomicMin(atomic_ptr: ptr<AS, atomic<T>, read_write>,
             v: T) -> T 

Atomically performs a minimum operation on the atomic object pointed to by atomic_ptr with the value v, and returns the original value stored in the atomic object before the operation.

fn atomicAnd(atomic_ptr: ptr<AS, atomic<T>, read_write>,
             v: T) -> T 

Atomically performs a bitwise AND operation on the atomic object pointed to by atomic_ptr with the value v, and returns the original value stored in the atomic object before the operation.

fn atomicOr(atomic_ptr: ptr<AS, atomic<T>, read_write>,
            v: T) -> T 

Atomically performs a bitwise OR operation on the atomic object pointed to by atomic_ptr with the value v, and returns the original value stored in the atomic object before the operation.

fn atomicXor(atomic_ptr: ptr<AS, atomic<T>, read_write>,
             v: T) -> T 

Atomically performs a bitwise XOR operation on the atomic object pointed to by atomic_ptr with the value v, and returns the original value stored in the atomic object before the operation.

fn atomicExchange(atomic_ptr: ptr<AS, atomic<T>, read_write>,
                  v: T) -> T 

Atomically stores the value v in the atomic object pointed to by atomic_ptr and returns the original value stored in the atomic object before the operation.

fn atomicCompareExchangeWeak( atomic_ptr: ptr<AS, atomic<T>, read_write>,
                             cmp: T,
                             v: T) -> __atomic_compare_exchange_result<T> struct __atomic_compare_exchange_result<T> { old_value : T, // old value stored in the atomic exchanged : bool // true if the exchange was done } 

Note: A value cannot be explicitly declared with the type __atomic_compare_exchange_result, but a value may infer the type.

Performs the following steps atomically:

Returns a two member structure, where the first member, old_value, is the original value of the atomic object before the operation and the second member, exchanged, is whether or not the comparison succeeded.

Note: The equality comparison may spuriously fail on some implementations. That is, the second component of the result vector may be false even if the first component of the result vector equals cmp.

Data Packing Built-in Functions

FunctionParameter TypesDescription
 fn pack4x8snorm(e: vec4<f32>) -> u32 
Converts four normalized floating point values to 8-bit signed integers, and then combines them into one u32 value.

Component e[i] of the input is converted to an 8-bit twos complement integer value ⌊ 0.5 + 127 × min(1, max(-1, e[i])) ⌋ which is then placed in bits 8 × i through 8 × i + 7 of the result.

 fn pack4x8unorm(e: vec4<f32>) -> u32 
Converts four normalized floating point values to 8-bit unsigned integers, and then combines them into one u32 value.

Component e[i] of the input is converted to an 8-bit unsigned integer value ⌊ 0.5 + 255 × min(1, max(0, e[i])) ⌋ which is then placed in bits 8 × i through 8 × i + 7 of the result.

 fn pack4xI8(e: vec4<i32>) -> u32 
Pack the lower 8 bits of each component of e into a u32 value and drop all the unused bits.

Component e[i] of the input is mapped to bits 8 × i through 8 × i + 7 of the result.

 fn pack4xU8(e: vec4<u32>) -> u32 
Pack the lower 8 bits of each component of e into a u32 value and drop all the unused bits.

Component e[i] of the input is mapped to bits 8 × i through 8 × i + 7 of the result.

 fn pack4xI8Clamp(e: vec4<i32>) -> u32 
Clamp each component of e in the range [-128, 127] and then pack the lower 8 bits of each component into a u32 value.

Component e[i] of the input is mapped to bits 8 × i through 8 × i + 7 of the result.

 fn pack4xU8Clamp(e: vec4<u32>) -> u32 
Clamp each component of e in the range of [0, 255] and then pack the lower 8 bits of each component into a u32 value.

Component e[i] of the input is mapped to bits 8 × i through 8 × i + 7 of the result.

 fn pack2x16snorm(e: vec2<f32>) -> u32 
Converts two normalized floating point values to 16-bit signed integers, and then combines them into one u32 value.
Component e[i] of the input is converted to a 16-bit twos complement integer value ⌊ 0.5 + 32767 × min(1, max(-1, e[i])) ⌋ which is then placed in bits 16 × i through 16 × i + 15 of the result.
 fn pack2x16unorm(e: vec2<f32>) -> u32 
Converts two normalized floating point values to 16-bit unsigned integers, and then combines them into one u32 value.
Component e[i] of the input is converted to a 16-bit unsigned integer value ⌊ 0.5 + 65535 × min(1, max(0, e[i])) ⌋ which is then placed in bits 16 × i through 16 × i + 15 of the result.
 fn pack2x16float(e: vec2<f32>) -> u32 
Converts two floating point values to half-precision floating point numbers, and then combines them into one u32 value.
Component e[i] of the input is converted to a IEEE-754 binary16 value, which is then placed in bits 16 × i through 16 × i + 15 of the result. See § 15.7.6 Floating Point Conversion.

If either e[0] or e[1] is outside the finite range of binary16 then:

  • It is a shader-creation error if e is a const-expression.

  • It is a pipeline-creation error if e is an override-expression.

  • Otherwise the result is an indeterminate value for u32.

Data Unpacking Built-in Functions

FunctionParameter TypesDescription
 fn unpack4x8snorm(e: u32) -> vec4<f32> 
Decomposes a 32-bit value into four 8-bit chunks, then reinterprets each chunk as a signed normalized floating point value.
Component i of the result is max(v ÷ 127, -1), where v is the interpretation of bits 8×i through 8×i + 7 of e as a twos-complement signed integer.
 fn unpack4x8unorm(e: u32) -> vec4<f32> 
Decomposes a 32-bit value into four 8-bit chunks, then reinterprets each chunk as an unsigned normalized floating point value.
Component i of the result is v ÷ 255, where v is the interpretation of bits 8×i through 8×i + 7 of e as an unsigned integer.
 fn unpack4xI8(e: u32) -> vec4<i32> 
e is interpreted as a vector with four 8-bit signed integer components. Unpack e into a vec4<i32> with sign extension.
 fn unpack4xU8(e: u32) -> vec4<u32> 
e is interpreted as a vector with four 8-bit unsigned integer components. Unpack e into a vec4<u32> with zero extension.
 fn unpack2x16snorm(e: u32) -> vec2<f32> 
Decomposes a 32-bit value into two 16-bit chunks, then reinterprets each chunk as a signed normalized floating point value.
Component i of the result is max(v ÷ 32767, -1), where v is the interpretation of bits 16×i through 16×i + 15 of e as a twos-complement signed integer.
 fn unpack2x16unorm(e: u32) -> vec2<f32> 
Decomposes a 32-bit value into two 16-bit chunks, then reinterprets each chunk as an unsigned normalized floating point value.
Component i of the result is v ÷ 65535, where v is the interpretation of bits 16×i through 16×i + 15 of e as an unsigned integer.
 fn unpack2x16float(e: u32) -> vec2<f32> 
Decomposes a 32-bit value into two 16-bit chunks, and reinterpets each chunk as a floating point value.
Component i of the result is the f32 representation of v, where v is the interpretation of bits 16×i through 16×i + 15 of e as an IEEE-754 binary16 value. See § 15.7.6 Floating Point Conversion.

Synchronization Built-in Functions

FunctionParameter TypesDescription
 fn storageBarrier() 
Executes a control barrier synchronization function that affects memory and atomic operations in the storage address space.
 fn textureBarrier() 
Executes a control barrier synchronization function that affects memory operations in the handle address space.
 fn workgroupBarrier() 
Executes a control barrier synchronization function that affects memory and atomic operations in the workgroup address space.
 fn workgroupUniformLoad(p : ptr<workgroup, T>) -> T 
T is a concrete constructible type. Returns the value pointed to by p to all invocations in the workgroup. The return value is uniform. p must be a uniform value.

Executes a control barrier synchronization function that affects memory and atomic operations in the workgroup address space.

 fn workgroupUniformLoad(p : ptr<workgroup, atomic<T>, read_write>) -> T 
Atomically loads the value pointed to by p and returns it to all invocations in the workgroup. The return value is uniform. p must be a uniform value.

Executes a control barrier synchronization function that affects memory and atomic operations in the workgroup address space.

Subgroup Built-in Functions

FunctionParameter TypesDescription
 fn subgroupAdd(e : T) -> T 
Reduction operation.

Returns the sum of e among all active invocations in the subgroup.

 fn subgroupExclusiveAdd(e : T) -> T 
Exclusive prefix scan operation.

Returns the sum of e among all active invocations in the subgroup whose subgroup invocation IDs are less than the current invocation’s id.

The value returned for the invocation with the lowest id among active invocations is T(0).

 fn subgroupInclusiveAdd(e : T) -> T 
Inclusive prefix scan operation.

Returns the sum of e among all active invocations in the subgroup whose subgroup invocation IDs are less than or equal to the current invocation’s id.

Note: equivalent to subgroupExclusiveAdd(x) + x.

 fn subgroupAll(e : bool) -> bool 
Returns true if e is true for all active invocations in the subgroup.
 fn subgroupAnd(e : T) -> T 
Reduction operation.

Returns the bitwise and (&) of e among all active invocations in the subgroup.

 fn subgroupAny(e : bool) -> bool 
Returns true if e is true for any active invocations in the subgroup.
 fn subgroupBallot(pred : bool) -> vec4<u32> 
Returns a bitmask of the active invocations in the subgroup for whom pred is true.

The x component of the return value contains invocations 0 through 31.
The y component of the return value contains invocations 32 through 63.
The z component of the return value contains invocations 64 through 95.
The w component of the return value contains invocations 96 through 127.

Within each component, the IDs are in ascending order by bit position (e.g. ID 32 is at bit position 0 in the y component).

 fn subgroupBroadcast(e : T,
                      id : I) -> T 
Returns the value of e from the invocation whose subgroup invocation ID matches id in the subgroup to all active invocations in the subgroup.

id must be a const-expression in the range [0, 128).

It is a dynamic error if id does not select an active invocation.

Note: If a non-constant version of id is required, use subgroupShuffle instead.

 fn subgroupBroadcastFirst(e : T) -> T 
Returns the value of e from the invocation that has the lowest subgroup invocation ID among active invocations in the subgroup to all active invocations in the subgroup.
 fn subgroupElect() -> bool 
Returns true if the current invocation has the lowest subgroup invocation ID among active invocations in the subgroup.
 fn subgroupMax(e : T) -> T 
Reduction operation.

Returns the maximum value of e among all active invocations in the subgroup.

 fn subgroupMin(e : T) -> T 
Reduction operation.

Returns the minimum value of e among all active invocations in the subgroup.

 fn subgroupMul(e : T) -> T 
Reduction operation.

Returns the product of e among all active invocations in the subgroup.

 fn subgroupExclusiveMul(e : T) -> T 
Exclusive prefix scan operation.

Returns the product of e among all active invocations in the subgroup whose subgroup invocation IDs are less than the current invocation’s id.

The value returned for the invocation with the lowest id among active invocations is T(1).

 fn subgroupInclusiveMul(e : T) -> T 
Inclusive prefix scan operation.

Returns the product of e among all active invocations in the subgroup whose subgroup invocation IDs are less than or equal to the current invocation’s id.

Note: equivalent to subgroupExclusiveMul(x) * x.

 fn subgroupOr(e : T) -> T 
Reduction operation.

Returns the bitwise or (|) of e among all active invocations in the subgroup.

 fn subgroupShuffle(e : T,
                    id : I) -> T 
Returns e from the invocation whose subgroup invocation ID matches id.

If id is outside the range [0, 128), then:

  • It is a shader-creation error if id is a const-expression.

  • It is a pipeline-creation error if id is an override-expression.

An indeterminate value is returned if id does not select an active invocation.

 fn subgroupShuffleDown(e : T,
                        delta : u32) -> T 
Returns e from the invocation whose subgroup invocation ID matches subgroup_invocation_id + delta for the current invocation.

If delta is greater than 127, then:

  • It is a shader-creation error if delta is a const-expression.

  • It is a pipeline-creation error if delta is an override-expression.

A subgroup_uniformity diagnostic is triggered if delta is not a uniform value. An indeterminate value is returned if subgroup_invocation_id + delta does not select an active invocation or if delta is a not a uniform value within the subgroup.

 fn subgroupShuffleUp(e : T,
                      delta : u32) -> T 
Returns e from the invocation whose subgroup invocation ID matches subgroup_invocation_id - delta for the current invocation.

If delta is greater than 127, then:

  • It is a shader-creation error if delta is a const-expression.

  • It is a pipeline-creation error if delta is an override-expression.

A subgroup_uniformity diagnostic is triggered if delta is not a uniform value. An indeterminate value is returned if subgroup_invocation_id - delta does not select an active invocation or if delta is not a uniform value within the subgroup.

 fn subgroupShuffleXor(e : T,
                       mask : u32) -> T 
Returns e from the invocation whose subgroup invocation ID matches subgroup_invocation_id ^ mask for the current invocation.

If mask is greater than 127, then:

  • It is a shader-creation error if mask is a const-expression.

  • It is a pipeline-creation error if mask is an override-expression.

A subgroup_uniformity diagnostic is triggered if mask is not a uniform value. An indeterminate value is returned if mask does not select an active invocation or if mask is not a uniform value within the subgroup.

 fn subgroupXor(e : T) -> T 
Reduction operation.

Returns the bitwise xor (^) of e among all active invocations in the subgroup.

Quad Operations

FunctionParameter TypesDescription
 fn quadBroadcast(e : T,
                  id : I) -> T 
Returns the value of e from the invocation whose quad invocation ID matches id in the quad to all active invocations in the quad.

id must be a const-expression in the range [0, 4).

An indeterminate value is returned if id does not select an active invocation.

Note: Unlike subgroupBroadcast, there is currently no non-constant alternative.

 fn quadSwapDiagonal(e : T) -> T 
Returns the value of e from the invocation in the quad with the opposite coordinates. That is:
  • IDs 0 and 3 swap.

  • IDs 1 and 2 swap.

 fn quadSwapX(e : T) -> T 
Returns the value of e from invocation in the quad sharing the same X dimension. That is:
  • IDs 0 and 1 swap.

  • IDs 2 and 3 swap.

 fn quadSwapY(e : T) -> T 
Returns the value of e from invocation in the quad sharing the same Y dimension. That is:
  • IDs 0 and 2 swap.

  • IDs 1 and 3 swap.

Be aware of undefined behavior in WGSL

Several functions in WGSL are undefined for certain values. Trying to raise a negative number to a power with pow is one example since the result would be an imaginary number. We went over another example above with smoothstep.

You need to try to be aware of these or else your shaders will get different results on different machines.

Here's a list of undefined some behaviors. Note T means float, vec2f, vec3f, or vec4f.

fn asin(x: T) -> T

Arc sine. Returns an angle whose sine is x. The range of values returned by this function is [−π/2, π/2] Results are undefined if ∣x∣ > 1.

fn acos(x: T) -> T

Arc cosine. Returns an angle whose cosine is x. The range of values returned by this function is [0, π]. Results are undefined if ∣x∣ > 1.

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

Arc tangent. Returns an angle whose tangent is y/x. The signs of x and y are used to determine what quadrant the angle is in. The range of values returned by this function is [−π,π]. Results are undefined if x and y are both 0.

fn acosh(x: T) -> T

Arc hyperbolic cosine; returns the non-negative inverse of cosh. Results are undefined if x < 1.

fn atanh(x: T) -> T

Arc hyperbolic tangent; returns the inverse of tanh. Results are undefined if ∣x∣≥1.

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

Returns x raised to the y power, i.e., xy. Results are undefined if x < 0. Results are undefined if x = 0 and y <= 0.

fn log(x: T) -> T

Returns the natural log of x. Results are undefined if x < 0.

fn log2(x: T) -> T

Returns the base-2 logarithm of x. Results are undefined if x < 0.

fn log(x: T) -> T

Returns the natural logarithm of x, i.e., returns the value y which satisfies the equation x = ey. Results are undefined if x <= 0.

fn log2(x: T) -> T

Returns the base 2 logarithm of x, i.e., returns the value y which satisfies the equation x=2y. Results are undefined if x <= 0.

fn sqrt(T: x) -> T

Returns √x . Results are undefined if x < 0.

fn inverseSqrt(x: T) -> T

Returns 1/√x. Results are undefined if x <= 0.

fn clamp(x: T, minVal: T, maxVal: T) -> T

Returns min(max(x, minVal), maxVal). Results are undefined if minVal > maxVal

fn smoothstep(edge0: T, edge1: T, x: T) -> T

Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and performs smooth Hermite interpolation between 0 and 1 when edge0 < x < edge1. Results are undefined if edge0 >= edge1.

Questions? Ask on stackoverflow.
Suggestion? Request? Issue? Bug?
comments powered by Disqus