This is a reference to WGSL syntax for users coming from GLSL. It is not meant to be complete, but to cover some common usages.
WGSL is still evolving, so some things may change before the final version. This reference is based on the WGSL draft spec.
WGSL | GLSL | Note |
---|---|---|
bool | bool | true or false |
i32 | int | 32-bit signed integer |
u32 | uint | 32-bit unsigned integer |
f32 | float | Single-precision float |
Not available | double | Double-precision float |
WGSL | GLSL | Note |
---|---|---|
vecN<bool> | bvecN | Vector of N ∈ {2, 3, 4} bools. |
vecN<i32> | ivecN | Vector of N ∈ {2, 3, 4} signed integers. |
vecN<u32> | uvecN | Vector of N ∈ {2, 3, 4} unsigned integers. |
vecN<f32> | vecN | Vector of N ∈ {2, 3, 4} floats. |
Not available | dvecN | Vector of N ∈ {2, 3, 4} double-precision vectors. |
matNxM<f32> | matNxM, matN | Matrix of NxM ∈ {2, 3, 4} columns and rows (or NxN). |
WGSL | GLSL | Note |
---|---|---|
var m: i32 = 4; | int m = 4; | var creates a local variable. |
let m: i32 = 4; | Not available? | let creates an immutable binding. |
WGSL | GLSL | Note |
---|---|---|
123 | 123 (when used in int context) | |
123u, 123u32 | 123 (when used in uint context) | WGSL does not infer int type from context; you need to be explicit. |
WGSL needs explicit fallthrough
to not break
at the end of a switch block. my_var
must be a u32
(I think?).
WGSL | GLSL |
---|---|
switch (my_var) { case 0: { fallthrough; } case 1: { foo = 1; } case 2: { foo = 4; } } |
switch (my_var) { case 0: case 1: foo = 1; break; case 2: foo = 4; } |
WGSL | GLSL |
---|---|
// Structure of vertex shader output struct VertexOutput { [[builtin(position)]] position: vec4<f32>; [[location(0)]] baz; }; // Vertex shader function [[stage(vertex)]] fn vs_main( [[location(0)]] foo: vec2<f32>, [[location(1)]] bar: vec4<f32>, ) -> VertexOutput { var out: VertexOutput; if (foo.x > foo.y) { discard; } out.baz = vec4<f32>(0.0, 1.0, 0.0, 1.0); out.position = bar; return out; } |
// Inputs from vertex buffer. layout(location=0) in vec2 foo; layout(location=1) in vec4 bar; // Output to frag shader. layout(location=0) out vec4 baz; void main() { if (foo.x > foo.y) { discard; } baz = vec4(0.0, 1.0, 0.0, 1.0); gl_Position = bar; } |