Functions
Every function is a function of the module. There are no function values, no environment to close over and no call stack.
Parameters and names
-
Shadowing a module value
A parameter or body local can use the name of a module value. Closures resolve the nearest declaration; duplicates in the same scope remain errors.
TypeScriptconst gain: f32 = 0.25;function scale(gain: f32): f32 { return gain * 2.; }export function answer(): f32 {const gain: f32 = 1.;return scale(gain);}Emitted WGSL fn scale(gain: f32) -> f32 {return (gain * 2.0);}fn answer() -> f32 {return scale(1.0);}Emitted GLSL ES 3.00 float scale(float gain) {return (gain * 2.0);}float answer() {return scale(1.0);} -
Parameter reassignment
A number, a
bool, a vector and a matrix are values, as in JavaScript. The function writes a local copy that starts as the input, so the caller’s variable keeps its value. To give a changed value back, return it.TypeScriptexport function brighter(value: f32): f32 {value += 0.25;return value;}Emitted WGSL fn brighter(value: f32) -> f32 {var value_1: f32 = value;value_1 += 0.25;return value_1;}Emitted GLSL ES 3.00 float brighter(float value) {float value_1 = value;value_1 += 0.25;return value_1;} -
Two results
A function that computes two values returns a list, and the caller takes the list apart with
[lo, hi] = swap(lo, hi). WGSL writes the list into a localarray<f32, 2>through a pointer, and GLSL ES 3.00 returns afloat[2]. reference-parametersTypeScriptfunction swap(a: f32, b: f32): [f32, f32] {return [b, a]}export function order(x: f32, y: f32): f32 {let lo = xlet hi = yif (lo > hi) {[lo, hi] = swap(lo, hi)}return hi - lo}Emitted WGSL fn order(x: f32, y: f32) -> f32 {var lo: f32 = x;var hi: f32 = y;if ((lo > hi)) {var _list: array<f32, 2>;swap_into(&_list, lo, hi);lo = _list[0u];hi = _list[1u];}return (hi - lo);}Emitted GLSL ES 3.00 float order(float x, float y) {float lo = x;float hi = y;if ((lo > hi)) {float[2] _list = swap(lo, hi);lo = _list[0u];hi = _list[1u];}return (hi - lo);} -
Object parameter
A struct, a class instance and an array are objects, as in JavaScript. A function that writes a field or an element of its parameter changes the caller's object, and the call passes the variable with no mark. The compiler reads which parameters a function writes: WGSL takes a pointer and GLSL ES 3.00 an
inoutparameter, the way a method that writesthisdoes. reference-parametersTypeScriptclass Ray {origin: vec2dir: vec2}function advance(r: Ray, t: f32): void {r.origin = r.origin + r.dir * t}export function moved(o: vec2, d: vec2, t: f32): vec2 {let r = new Ray()r.origin = or.dir = dadvance(r, t)return r.origin}Emitted WGSL fn advance(r: ptr<function, Ray>, t: f32) {(*r).origin = ((*r).origin + ((*r).dir * t));}fn moved(o: vec2<f32>, d: vec2<f32>, t: f32) -> vec2<f32> {var r: Ray;Ray_new_into(&r);r.origin = o;r.dir = d;advance(&r, t);return r.origin;}Emitted GLSL ES 3.00 void advance(inout Ray r, float t) {r.origin = (r.origin + (r.dir * t));}vec2 moved(vec2 o, vec2 d, float t) {Ray r = Ray_new();r.origin = o;r.dir = d;advance(r, t);return r.origin;} -
One object under two names
RefusedOne call may not hand one variable to two parameters when its function writes either. Inside the function the two names would be one object, which the compiler does not support yet (
TS8074). Pass two variables, or give the function one parameter for it.TypeScriptexport function twice(o: vec2): vec2 {let r = new Ray()r.origin = opull(r, r)return r.origin}Compiler diagnostic
TS8074 This call hands "r" to "pull" twice, and "pull" writes one of the two: one object under two names inside "pull", which is not supported yet (change 0142, stage 2). Pass two variables, or give "pull" one parameter for it.
Kinds of function
-
Top-level function
A module function of the same name, parameters and return type.
TypeScriptexport function lum(c: vec3): f32 {return dot(c, vec3(0.2126, 0.7152, 0.0722))}Emitted WGSL fn lum(c: vec3<f32>) -> f32 {return dot(c, vec3<f32>(0.2126, 0.7152, 0.0722));}Emitted GLSL ES 3.00 float lum(vec3 c) {return dot(c, vec3(0.2126, 0.7152, 0.0722));} -
Local function
A function of the module, named after the function that declares it.
TypeScriptexport function ramp(x: f32): f32 {const twice = (v: f32): f32 => v * 2.return twice(x)}Emitted WGSL fn ramp_twice(v: f32) -> f32 {return (v * 2.0);}fn ramp(x: f32) -> f32 {return ramp_twice(x);}Emitted GLSL ES 3.00 float ramp_twice(float v) {return (v * 2.0);}float ramp(float x) {return ramp_twice(x);} -
Closure over a name
The local function takes each name it reads from the function around it as a parameter, and a name it writes by reference.
TypeScriptexport function ramp(x: f32): f32 {const twice = (v: f32): f32 => v * xreturn twice(x)}Emitted WGSL fn ramp_twice(x: f32, v: f32) -> f32 {return (v * x);}fn ramp(x: f32) -> f32 {return ramp_twice(x, x);}Emitted GLSL ES 3.00 float ramp_twice(float x, float v) {return (v * x);}float ramp(float x) {return ramp_twice(x, x);} -
Default argument
Every parameter stays, and the argument left out is written at the call. default-args
TypeScriptfunction vignette(uv: vec2, strength: f32 = 0.8): f32 {return 1. - length(uv) * strength}Emitted WGSL fn vignette(uv: vec2<f32>, strength: f32) -> f32 {return (1.0 - (length(uv) * strength));}fn shade(uv: vec2<f32>) -> f32 {return vignette(uv, 0.8);}Emitted GLSL ES 3.00 float vignette(vec2 uv, float strength) {return (1.0 - (length(uv) * strength));}float shade(vec2 uv) {return vignette(uv, 0.8);} -
Optional value
Function results may be
T | undefined,T | nullorT | null | undefined(Rule 7.10). Inference preserves the absent states of returned values, including callbacks and local functions. A bare return or fall-through supplies null for a null-only result and undefined when admitted. Strict tests distinguish states;??evaluates its fallback only for an absent value. An un-narrowed read isTS8003. Nullable valuesTypeScriptfunction root(x: f32): f32 | undefined {if (x < 0.) {return undefined}return sqrt(x)}Emitted WGSL fn root_into(_dst: ptr<function, _opt_f32_>, x: f32) {if ((x < 0.0)) {(*_dst) = _opt_f32_(0.0, false);return;}(*_dst) = _opt_f32_(sqrt(x), true);}fn rootOr(x: f32) -> f32 {var _opt: _opt_f32_;root_into(&_opt, x);return select(1.0, _opt.value, _opt.present);}Emitted GLSL ES 3.00 _opt_f32_ root(float x) {if ((x < 0.0)) {return _opt_f32_(0.0, false);}return _opt_f32_(sqrt(x), true);}float rootOr(float x) {_opt_f32_ _opt = root(x);return mix(1.0, _opt.value, _opt.present);} -
Overload signature
The signatures are skipped and the implementation is lowered once.
TypeScriptexport function lum(c: vec3): f32export function lum(c: vec3): f32 {return dot(c, vec3(0.2126, 0.7152, 0.0722))}Emitted WGSL fn lum(c: vec3<f32>) -> f32 {return dot(c, vec3<f32>(0.2126, 0.7152, 0.0722));}Emitted GLSL ES 3.00 float lum(vec3 c) {return dot(c, vec3(0.2126, 0.7152, 0.0722));} -
Recursive call
RefusedWGSL has no call stack, and the check reads the call graph.
TypeScriptexport function fall(x: f32): f32 {return fall(x * 0.5)}Compiler diagnostic
TS8031 Recursive call: "fall" -> "fall". WGSL has no call stack, so a function must not take part in a call cycle.
Calls
-
Call as a statement
A statement of its own. A value-returning builtin standing alone computes nothing, so the optimizer drops it.
TypeScript@compute([64, 1, 1])export function main_k(gid: builtin<"global_invocation_id">): void {store(gid.x, 1.)}Emitted WGSL @compute @workgroup_size(64)fn main_k(@builtin(global_invocation_id) gid: vec3<u32>) {store(gid.x, 1.0);}The compiler emits no GLSL for this construct. The backend says:
backend 'glsl-es300' cannot emit this module — missing capabilities: storageBuffer, compute
-
Call with an effect
A builtin that returns a value and has an effect takes WGSL's phony assignment,
_ =, since Tint treats it as must-use. atomic-histogramTypeScript@compute([64, 1, 1])export function main_k(gid: builtin<"global_invocation_id">): void {atomicAdd(hits[gid.x], 1)}Emitted WGSL @compute @workgroup_size(64)fn main_k(@builtin(global_invocation_id) gid: vec3<u32>) {_ = atomicAdd(&hits[gid.x], 1u);}The compiler emits no GLSL for this construct. The backend says:
backend 'glsl-es300' cannot emit this module — missing capabilities: storageBuffer, compute
-
MathnamesMath.sinis the same builtin assin, andMath.PIfolds to its value.TypeScriptexport function wave(x: f32): f32 {return Math.sin(x * Math.PI)}Emitted WGSL fn wave(x: f32) -> f32 {return sin((x * 3.141592653589793));}Emitted GLSL ES 3.00 float wave(float x) {return sin((x * 3.141592653589793));}