Files
webgpufundamentals/webgpu/lessons/webgpu-wgsl-function-reference.md
2024-02-10 21:45:19 -08:00

4.2 KiB
Raw Permalink Blame History

Title: WGSL Function Reference Description: How to use Buffers TOC: WGSL Function Reference

{{{include "webgpu/lessons/webgpu-wgsl-function-reference.inc.html"}}}

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.

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>