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.
Constants and variables
-
Scalar
constA module constant. A scalar folds to one value at its declaration. module-const
TypeScriptconst GAIN: f32 = 1.5 * 2.Emitted WGSL const GAIN: f32 = 3.0;Emitted GLSL ES 3.00 const float GAIN = 3.0; -
Vector
constA vector or array constant carries its value as an expression each backend evaluates. palette-const
TypeScriptconst TINT: vec3 = vec3(0.2, 0.4, 0.9)Emitted WGSL const TINT: vec3<f32> = vec3<f32>(0.2, 0.4, 0.9);Emitted GLSL ES 3.00 const vec3 TINT = vec3(0.2, 0.4, 0.9); -
Local
letA mutable local. The annotation carries the type, and every target starts it at zero. bitfield-bands
TypeScriptexport function band(x: f32): f32 {let w: f32w = x * 2.return w}Emitted WGSL fn band(x: f32) -> f32 {let w = (x * 2.0);return w;}Emitted GLSL ES 3.00 float band(float x) {float w = (x * 2.0);return w;} -
Read before assignment
RefusedA read of a local on a path that has not assigned it is refused with
TS8075, by the rule TypeScript reports as TS2454. The editor showsTS8075in its place.TypeScriptexport function band(x: f32): f32 {let w: f32if (x > 0.) {w = x * 2.}return w}Compiler diagnostic
TS8075 "w" is read here before it is assigned on every path. Assign it before this read, or declare it with a value (Rule 7.6).
-
Module
letA module variable, one per invocation. private-state
TypeScriptlet seed: u32 = 7Emitted WGSL var<private> seed: u32 = 7u;Emitted GLSL ES 3.00 uint seed = 7u; -
Refusedvarletis the per-invocation variable andconstthe module constant.TypeScriptvar k = 1.Compiler diagnostic
TS8014 Top-level var is not allowed. Use `let` for a per-invocation variable or `const` for a module constant.
Types and namespaces
-
enumOne module constant per member, named
Enum_Memberand typedi32.TypeScriptenum Mode {Flat,Shaded,Wire,}Emitted WGSL const Mode_Flat: i32 = 0;const Mode_Shaded: i32 = 1;const Mode_Wire: i32 = 2;Emitted GLSL ES 3.00 const int Mode_Flat = 0;const int Mode_Shaded = 1;const int Mode_Wire = 2; -
const enumThe same constants. A
const enumis no different here.TypeScriptconst enum Flag {None = 0,Both = 3,}Emitted WGSL const Flag_None: i32 = 0;const Flag_Both: i32 = 3;Emitted GLSL ES 3.00 const int Flag_None = 0;const int Flag_Both = 3; -
typealiasAnother name for its target, resolved wherever a type may stand.
TypeScripttype Meters = f32Emitted WGSL fn deeper(d: f32) -> f32 {return (d * 2.0);}Emitted GLSL ES 3.00 float deeper(float d) {return (d * 2.0);} -
interfaceA struct, the same one a class of those fields would give.
TypeScriptinterface Camera {view: mat4pos: vec3}Emitted WGSL struct Camera {view: mat4x4<f32>,pos: vec3<f32>,}Emitted GLSL ES 3.00 struct Camera {mat4 view;vec3 pos;}; -
Class as a struct
A struct, whose fields lay out the memory the host fills. ray-class
TypeScriptclass Ray {origin: vec3dir: vec3}Emitted WGSL struct Ray {origin: vec3<f32>,dir: vec3<f32>,}Emitted GLSL ES 3.00 struct Ray {vec3 origin;vec3 dir;}; -
namespaceThe members flatten to
Ns_member, and the namespace itself emits nothing.TypeScriptnamespace 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);}Emitted GLSL ES 3.00 const vec3 Palette_WARM = vec3(0.9, 0.6, 0.3);vec3 Palette_tint(float x) {return (Palette_WARM * x);}