Control flow
A loop has to be one the GPU can finish, and a choice between two values has to be one the target has an operator for.
Conditions and loops
-
ifAn
if, as written.TypeScriptexport function capped(x: f32): f32 {if (x > 1.) {return 1.}return x}Emitted WGSL fn capped(x: f32) -> f32 {if ((x > 1.0)) {return 1.0;}return x;}Emitted GLSL ES 3.00 float capped(float x) {if ((x > 1.0)) {return 1.0;}return x;} -
Counted
forA counted loop: an integer variable, a constant step, and an exit that compares it to a bound the body does not write. block-scope
TypeScriptexport function ramp(x: f32): f32 {let acc: f32 = 0.for (let i = 0; i < 4; i += 1) {acc = acc + x * f32(i)}return acc}Emitted WGSL fn ramp(x: f32) -> f32 {let acc_1 = (x * 0.0);let acc_2 = (acc_1 + x);let acc_3 = (acc_2 + (x * 2.0));let acc_4 = (acc_3 + (x * 3.0));return acc_4;}Emitted GLSL ES 3.00 float ramp(float x) {float acc_1 = (x * 0.0);float acc_2 = (acc_1 + x);float acc_3 = (acc_2 + (x * 2.0));float acc_4 = (acc_3 + (x * 3.0));return acc_4;} -
forto a runtime boundThe start and the bound may be values the program learns at run time, and no trip count is too many.
TypeScriptexport function ramp(x: f32, n: i32): f32 {let acc: f32 = 0.for (let i = 0; i < n; i += 1) {acc = acc + x}return acc}Emitted WGSL fn ramp(x: f32, n: i32) -> f32 {var acc: f32 = 0.0;for (var i: i32 = 0; (i < n); i += 1) {acc = (acc + x);}return acc;}Emitted GLSL ES 3.00 float ramp(float x, int n) {float acc = 0.0;for (int i = 0; (i < n); i += 1) {acc = (acc + x);}return acc;} -
whileA loop with a counter the compiler adds. Nothing checks that the body moves toward the bound.
TypeScriptexport function halve(x: f32): f32 {let acc: f32 = xlet w: i32 = 0while (w < 4) {acc = acc * 0.5w = w + 1}return acc}Emitted WGSL fn halve(x: f32) -> f32 {var acc: f32 = x;var w: i32 = 0;for (var _w: i32 = 0; (w < 4); _w = (_w + 1)) {acc = (acc * 0.5);w = (w + 1);}return acc;}Emitted GLSL ES 3.00 float halve(float x) {float acc = x;int w = 0;for (int _w = 0; (w < 4); _w = (_w + 1)) {acc = (acc * 0.5);w = (w + 1);}return acc;} -
switchA
switch. Each case ends with abreak, which the lowering drops. A case whose end is reachable is refused (TS8017), because TypeScript would run on into the next case and WGSL runs only the one. bitfield-bandsTypeScriptexport function band(m: i32): f32 {let v: f32 = 0.switch (m) {case 0:v = 0.25breakdefault:v = 1.break}return v}Emitted WGSL fn band(m: i32) -> f32 {var v: f32 = 0.0;switch m {case 0: {v = 0.25;}default: {v = 1.0;}}return v;}Emitted GLSL ES 3.00 float band(int m) {float v = 0.0;switch (m) {case 0: {v = 0.25;break;}default: {v = 1.0;}}return v;}
Choosing a value
-
Ternary on a scalar
WGSL has no
?:operator, so the ternary becomesselect: the false value first, the condition last. WGSL evaluates both values, so an arm that calls a function of the file, reads a texture or holds eight or more operations is hoisted into a slot and anifinstead, and only the arm taken runs. GLSL ES 3.00 keeps the ternary for the cheap arm.TypeScriptexport function nearer(a: f32, b: f32): f32 {return a < b ? a : b}Emitted WGSL fn nearer(a: f32, b: f32) -> f32 {return select(b, a, (a < b));}Emitted GLSL ES 3.00 float nearer(float a, float b) {return mix(b, a, (a < b));} -
Ternary on a struct
Neither target has an operator for it, so the value is hoisted into a slot and an
if. pick-compositeTypeScriptexport function chosen(warm: Palette, cool: Palette, c: bool): vec3 {const shade: Palette = c ? warm : coolreturn shade.lo}Emitted WGSL fn chosen(warm: Palette, cool: Palette, c: bool) -> vec3<f32> {var _sel0: Palette;if (c) {_sel0 = warm;} else {_sel0 = cool;}var shade: Palette = _sel0;return shade.lo;}Emitted GLSL ES 3.00 vec3 chosen(Palette warm, Palette cool, bool c) {Palette _sel0;if (c) {_sel0 = warm;} else {_sel0 = cool;}Palette shade = _sel0;return shade.lo;}
Leaving early
-
breakA
break, which is how a counted loop leaves early. julia-twinTypeScriptexport function escape(x: f32): f32 {let acc: f32 = xfor (let i = 0; i < 8; i += 1) {acc = acc * 2.if (acc > 1.) {break}}return acc}Emitted WGSL fn escape(x: f32) -> f32 {var acc: f32 = x;for (var i: i32 = 0; (i < 8); i += 1) {acc = (acc * 2.0);if ((acc > 1.0)) {break;}}return acc;}Emitted GLSL ES 3.00 float escape(float x) {float acc = x;for (int i = 0; (i < 8); i += 1) {acc = (acc * 2.0);if ((acc > 1.0)) {break;}}return acc;} -
continueA
continue. In aswitchthat no loop encloses it is refused.TypeScriptexport function odds(x: f32): f32 {let acc: f32 = 0.for (let i = 0; i < 4; i += 1) {if (i === 2) {continue}acc = acc + x}return acc}Emitted WGSL fn odds(x: f32) -> f32 {var acc: f32 = 0.0;for (var i: i32 = 0; (i < 4); i += 1) {if ((i == 2)) {continue;}acc = (acc + x);}return acc;}Emitted GLSL ES 3.00 float odds(float x) {float acc = 0.0;for (int i = 0; (i < 4); i += 1) {if ((i == 2)) {continue;}acc = (acc + x);}return acc;} -
discardA
discard, in a fragment entry or in a function one calls. cutoutTypeScript@fragmentexport function fs(frag: builtin<"position">): vec4 {if (frag.x < 0.5) {discard}return vec4(1.)}Emitted WGSL @fragmentfn fs(@builtin(position) frag: vec4<f32>) -> @location(0) vec4<f32> {if ((frag.x < 0.5)) {discard;}return vec4<f32>(1.0);}Emitted GLSL ES 3.00 layout(location = 0) out vec4 _ret;void main() {vec4 frag = gl_FragCoord;if ((frag.x < 0.5)) {discard;}_ret = vec4(1.0);}