Skip to main content

Utilities

warning

Automatic WGSL docs generation publishable on this website is currently waiting on wgpu#6364. In the meantime, refer to the WGSL sources in the wgebra repository.

WGebra provides a few utilities that are generally useful in mathematical code but are missing from the WebGPU standard.

Component reductions

Calculating the minimum or maximum element of a vec2, vec3, vec4, mat2x2, mat3x3, mat4x4 is rather verbose with vanilla WGSL code. The WgMinMax composable shader exposes functions for computing it in your shader:

#[derive(Shader)]
#[shader(derive(WgMinMax), src = "your_shader.wgsl")]
struct YourShader;

// Automatically generates a test to check with `cargo test` if `your_shader.wgsl` compiles.
wgcore::test_shader_compilation!(YourShader);

Stable tangents

In some platforms (especially Metal/MacOS) provide numerically unstable implementations of some trigonometric functions, potentially leading to NaNs for some edge-cases. The WgTrig module exposes implementations of tanh and atan2 that will be more numerically stable across platforms.

#[derive(Shader)]
#[shader(derive(WgTrig), src = "your_shader.wgsl")]
struct YourShader;

// Automatically generates a test to check with `cargo test` if `your_shader.wgsl` compiles.
wgcore::test_shader_compilation!(YourShader);