4.2 KiB
Title: WGSL Function Reference Description: How to use Buffers TOC: WGSL Function Reference
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) -> TArc 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) -> TArc 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) -> TArc 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) -> TArc hyperbolic cosine; returns the non-negative inverse of cosh. Results are undefined if x < 1.
fn atanh(x: T) -> TArc hyperbolic tangent; returns the inverse of tanh. Results are undefined if ∣x∣≥1.
fn pow(x: T, y: T) -> TReturns 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) -> TReturns the natural log of x. Results are undefined if x < 0.
fn log2(x: T) -> TReturns the base-2 logarithm of x. Results are undefined if x < 0.
fn log(x: T) -> TReturns 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) -> TReturns 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) -> TReturns √x . Results are undefined if x < 0.
fn inverseSqrt(x: T) -> TReturns 1/√x. Results are undefined if x <= 0.
fn clamp(x: T, minVal: T, maxVal: T) -> TReturns min(max(x, minVal), maxVal). Results are undefined if minVal > maxVal
fn smoothstep(edge0: T, edge1: T, x: T) -> TReturns 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.
Copyright © 2023 World Wide Web Consortium. W3C® liability, trademark and permissive document license rules apply.
<script type="module" src="webgpu-wgsl-function-reference.js"></script>