Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Operators

wgsl-rs transpiles Rust operators to their WGSL equivalents. Most have a 1:1 mapping.

Arithmetic

RustWGSLDescription
a + ba + baddition
a - ba - bsubtraction
a * ba * bmultiplication
a / ba / bdivision
a % ba % bremainder

Comparison

RustWGSL
a == ba == b
a != ba != b
a < ba < b
a <= ba <= b
a > ba > b
a >= ba >= b

Logical

RustWGSL
a && ba && b
a || ba || b
!a!a

Bitwise

RustWGSL
a & ba & b
a | ba | b
a ^ ba ^ b
a << na << n
a >> na >> n

Compound Assignment

+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= all transpile directly:

#![allow(unused)]
fn main() {
pub fn bump(x: ptr!(function, f32)) {
    *x += 1.0;
    *x *= 2.0;
}
}

Mutable references in function signatures must use the ptr! macro — bare &mut T parameters are not supported in #[wgsl] modules.

select

select(false_val, true_val, condition) maps to the WGSL select builtin. Argument order matches WGSL:

#![allow(unused)]
fn main() {
pub fn abs_or(x: f32, sign: bool) -> f32 {
    select(-x, x, sign)
}
}
fn abs_or(x: f32, sign: bool) -> f32 {
  return select(-x, x, sign);
}

Unary

RustWGSLDescription
-a-anegation
!a!alogical/bitwise not
*p*pdereference (for ptr!)

as Casts

as casts are transpiled when meaningful in WGSL. The common case is as usize for array indexing — this is stripped in WGSL, which uses the index directly:

#![allow(unused)]
fn main() {
pub fn at(arr: Vec4f, i: u32) -> f32 {
    arr[i as usize]
}
}
fn at(arr: vec4<f32>, i: u32) -> f32 {
  return arr[i];
}