Declarations

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 const

    A 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 const

    A 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 let

    A mutable local. The annotation carries the type, and WGSL gives it a zero. bitfield-bands

    TypeScript
    export function band(x: f32): f32 {
    let w: f32
    w = x * 2.
    return w
    }
    Emitted WGSL
    fn band(x: f32) -> f32 {
    var w: f32;
    w = (x * 2.0);
    return w;
    }
  • Module let

    A module variable, one per invocation. private-state

    TypeScript
    let seed: u32 = 7
    Emitted WGSL
    var<private> seed: u32 = 7u;
  • var

    Refused

    let is the per-invocation variable and const the 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.

  • enum

    One module constant per member, named Enum_Member and typed i32.

    TypeScript
    enum Mode {
    Flat,
    Shaded,
    Wire,
    }
    Emitted WGSL
    const Mode_Flat: i32 = 0;
    const Mode_Shaded: i32 = 1;
    const Mode_Wire: i32 = 2;
  • const enum

    The same constants. A const enum is no different here.

    TypeScript
    const enum Flag {
    None = 0,
    Both = 3,
    }
    Emitted WGSL
    const Flag_None: i32 = 0;
    const Flag_Both: i32 = 3;
  • type alias

    Another name for its target, resolved wherever a type may stand.

    TypeScript
    type Meters = f32
    Emitted WGSL
    fn deeper(d: f32) -> f32 {
    return (d * 2.0);
    }
  • interface

    A struct, the same one a class of those fields would give.

    TypeScript
    interface Camera {
    view: mat4
    pos: 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: vec3
    dir: vec3
    }
    Emitted WGSL
    struct Ray {
    origin: vec3<f32>,
    dir: vec3<f32>,
    }
  • namespace

    The 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);
    }

Edit this page Report a problem