Declarations
A declaration names a constant, a variable or a layout. Which of the three it is decides whether anything is emitted for it at all.
-
Scalar
constA module constant. A scalar folds to one value at its declaration. module-const
TypeScript const GAIN: f32 = 1.5 * 2.Emitted WGSL const GAIN: f32 = 3.0; -
Vector
constA vector or array constant carries its value as an expression each backend evaluates. palette-const
TypeScript const TINT: vec3 = vec3(0.2, 0.4, 0.9)Emitted WGSL const TINT: vec3<f32> = vec3<f32>(0.2, 0.4, 0.9); -
Local
letA mutable local. The annotation carries the type, and WGSL gives it a zero. bitfield-bands
TypeScript export function band(x: f32): f32 {let w: f32w = x * 2.return w}Emitted WGSL fn band(x: f32) -> f32 {var w: f32;w = (x * 2.0);return w;} -
Module
letA module variable, one per invocation. private-state
TypeScript let seed: u32 = 7Emitted WGSL var<private> seed: u32 = 7u; -
Refusedvarletis the per-invocation variable andconstthe module constant.TypeScript var k = 1.Compiler diagnostic
TS8014 Top-level var is not allowed. Use `let` for a per-invocation variable or `const` for a module constant.
-
enumOne module constant per member, named
Enum_Memberand typedi32.TypeScript enum Mode {Flat,Shaded,Wire,}Emitted WGSL const Mode_Flat: i32 = 0;const Mode_Shaded: i32 = 1;const Mode_Wire: i32 = 2; -
const enumThe same constants. A
const enumis no different here.TypeScript const enum Flag {None = 0,Both = 3,}Emitted WGSL const Flag_None: i32 = 0;const Flag_Both: i32 = 3; -
typealiasAnother name for its target, resolved wherever a type may stand.
TypeScript type Meters = f32Emitted WGSL fn deeper(d: f32) -> f32 {return (d * 2.0);} -
interfaceA struct, the same one a class of those fields would give.
TypeScript interface Camera {view: mat4pos: vec3}Emitted WGSL struct Camera {view: mat4x4<f32>,pos: vec3<f32>,} -
Class as a struct
A struct, whose fields lay out the memory the host fills. ray-class
TypeScript class Ray {origin: vec3dir: vec3}Emitted WGSL struct Ray {origin: vec3<f32>,dir: vec3<f32>,} -
namespaceThe members flatten to
Ns_member, and the namespace itself emits nothing.TypeScript namespace Palette {export const WARM: vec3 = vec3(0.9, 0.6, 0.3)export function tint(x: f32): vec3 {return WARM * x}}Emitted WGSL const Palette_WARM: vec3<f32> = vec3<f32>(0.9, 0.6, 0.3);fn Palette_tint(x: f32) -> vec3<f32> {return (Palette_WARM * x);}