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

Scalars & Literals

wgsl-rs shares the four WGSL scalar types with Rust directly. The same names mean the same thing in both worlds.

TypeRustWGSLNotes
f3232-bit floatf32IEEE 754 single precision
i32signed inti3232-bit two's complement
u32unsigned intu3232-bit
boolbooleanbooltrue / false

Because shader code must type-check as ordinary Rust, scalar types are not aliases: they are the literal Rust primitive types. The transpiler maps them onto the matching WGSL keyword.

Literal Suffixes

Rust literal suffixes carry type information into the generated WGSL. Always suffix literals when the surrounding context does not pin the type (e.g. constants, function arguments to generic constructors).

#![allow(unused)]
fn main() {
const WIDTH: u32 = 1024u32;
const EPS: f32 = 1e-5f32;
let count: i32 = 0i32;
}

Unsuffixed integer literals are accepted by Rust and inferred from context, but explicit suffixes make the transpiler's job unambiguous and the generated WGSL easier to read.

as Casts

Rust's as cast operator transpiles to a WGSL conversion expression of the same form.

#![allow(unused)]
fn main() {
let i: i32 = 7;
let u: u32 = i as u32;     // -> u32(i)
let f: f32 = u as f32;     // -> f32(u)
let n: f32 = i as f32;     // -> f32(i)
}

Cross-kind conversions (i32 <-> u32 <-> f32) all generate the corresponding WGSL T(x) conversion. Booleans cannot be cast with as; use select or a manual comparison instead.