WGSL and GLSL
On this page

WGSL vs GLSL ES 3.00

WGSL and GLSL are shader languages. In browser applications, WebGPU uses WGSL, while WebGL uses GLSL ES. Shader source describes a calculation within the API execution model.

When it helps

Use the language accepted by the chosen API. When porting a shader, compare types and expressions together with entry-point inputs, outputs, and resource bindings.

Worked example

Both local expressions interpolate between the same scalar endpoints with the same weight. The result agrees even though the variable declarations use different syntax.

WGSL
let value: f32 = mix(0.0, 1.0, 0.25);
GLSL ES
float value = mix(0.0, 1.0, 0.25);

Result: value = 0.25

Practical limits

Porting also requires checking buffer layouts, coordinate conventions, interpolation, and precision. GLSL ES default precision depends on the shader stage; WGSL follows its own type and accuracy rules.

One "use typeshade" file reaches WebGPU as WGSL and WebGL2 as GLSL ES 3.00. The compiler spells 190 builtins, and 44 of them are written the same way in both languages. The blocks on this page are compiled while the page is built, from the file above them, by the compiler pinned at af54ddad5.

The source

A triangle, with a vertex entry that places it and a fragment entry that colours it. The two structs give each stage its output shape.

hello.shade.ts
"use typeshade";
interface Clip {
pos: builtin<"position">;
}
interface Color {
color: location<0, vec4>;
}
@vertex
export function vs(i: builtin<"vertex_index">): Clip {
const x = i === 1 ? 0.8 : i === 2 ? 0. : -0.8;
const y = i === 2 ? 0.8 : -0.8;
return { pos: vec4(x, y, 0., 1.) };
}
@fragment
export function fs(): Color {
return { color: vec4(1., 0., 0., 1.) };
}

WGSL

WGSL is the language a WebGPU device accepts, and one module holds every stage. The structs survive as structs, each entry point keeps its stage attribute, and the builtin input stays an attribute on the parameter that receives it.

The emitted WGSL module
struct Clip {
@builtin(position) pos: vec4<f32>,
}
struct Color {
@location(0) color: vec4<f32>,
}
@vertex
fn vs(@builtin(vertex_index) i: u32) -> Clip {
let _cse0 = (i == 2u);
let x = select(select(-0.8, 0.0, _cse0), 0.8, (i == 1u));
let y = select(-0.8, 0.8, _cse0);
return Clip(vec4<f32>(x, y, 0.0, 1.0));
}
@fragment
fn fs() -> Color {
return Color(vec4<f32>(1.0, 0.0, 0.0, 1.0));
}

GLSL ES 3.00

A WebGL2 program is linked from one vertex shader and one fragment shader, so the compiler emits a separate program for each stage. Each one opens with its version line and its default precision, the vertex index arrives under the name the language reserves for it, and the fragment output becomes a declared out variable.

The emitted GLSL ES 3.00 vertex shader
#version 300 es
precision highp float;
precision highp int;
void main() {
uint i = uint(gl_VertexID);
bool _cse0 = (i == 2u);
float x = mix(mix(-0.8, 0.0, _cse0), 0.8, (i == 1u));
float y = mix(-0.8, 0.8, _cse0);
gl_Position = vec4(x, y, 0.0, 1.0);
}
The emitted GLSL ES 3.00 fragment shader
#version 300 es
precision highp float;
precision highp int;
layout(location = 0) out vec4 color;
void main() {
color = vec4(1.0, 0.0, 0.0, 1.0);
}

Where the targets differ

Each difference is named by the page of the guide that handles it. Where the two disagree, WGSL defines what a construct means, and GLSL ES 3.00 is a target the compiler writes to (Rule 1.2no test checks this yet).

What differsWGSLGLSL ES 3.00
Compute stagesA compute entry emits a compute shader carrying its workgroup size.WebGL2 has no native compute stage. TypeShade runs supported compute entries through generated GLSL ES 3.00 passes.
PrecisionA type carries its own width, so the program declares nothing.The program opens with a default precision for floating-point and integer values.
Builtin inputsA builtin stays an attribute on the parameter that receives it.A builtin becomes the name the language reserves for it, and the parameter goes away.
Stage outputsA stage returns a struct whose fields carry their locations.A stage writes to declared out variables, and the position goes to the reserved one.
ExtensionsA GPU feature is turned on by a declaration at the top of the module.A GPU feature is turned on by a preprocessor line, and the host asks the context for the matching extension.

Names that differ

Each row is one construct. The GLSL ES 3.00 and WGSL cells are the text the compiler at af54ddad5 writes for the TypeShade cell. A spelling the compiler changes reaches this table at the next pin.

Types

GLSL ES 3.00WGSLTypeShade
float
f32
f32
int
i32
i32
uint
u32
u32
bool
bool
bool
vec3
vec3<f32>
vec3
ivec3
vec3<i32>
vec3i
uvec3
vec3<u32>
vec3u
bvec3
vec3<bool>
vec3b
mat4
mat4x4<f32>
mat4
mat2x3
mat2x3<f32>
mat2x3
float[4]
array<f32, 4>
array<f32, 4>
sampler2D
texture_2d<f32>
texture_2d<f32>
samplerCube
texture_cube<f32>
texture_cube<f32>

standalone sampler — fused into the combined sampler2D

sampler
sampler

Builtin variables

GLSL ES 3.00WGSLTypeShade
gl_Position
@builtin(position)
builtin<"position">
gl_FragCoord
@builtin(position)
builtin<"position">
uint(gl_VertexID)
@builtin(vertex_index)
builtin<"vertex_index">
uint(gl_InstanceID)
@builtin(instance_index)
builtin<"instance_index">
gl_FrontFacing
@builtin(front_facing)
builtin<"front_facing">
gl_FragDepth
@builtin(frag_depth)
builtin<"frag_depth">

Functions

GLSL ES 3.00WGSLTypeShade
atan(a, b)
atan2(a, b)
inversesqrt(a)
inverseSqrt(a)
faceforward(a, b, c)
faceForward(a, b, c)
dFdx(a)
dpdx(a)
dFdy(a)
dpdy(a)
clamp(a, 0.0, 1.0)
saturate(a)
((a) * (b) + (c))
fma(a, b, c)
(c ? b : a)
select(a, b, c)
texture(a, c)
textureSample(a, b, c)
textureLod(a, c, d)
textureSampleLevel(a, b, c, d)
texelFetch(a, b, int(c))
textureLoad(a, b, c)
uvec2(textureSize(a, 0))
textureDimensions(a)
packHalf2x16(a)
pack2x16float(a)
float(a)
f32(a)
lessThan(uv, vec2(0.5, 0.25))
(uv < vec2<f32>(0.5, 0.25))
uv < vec2(0.5, 0.25)

Same name, other meaning

GLSL ES 3.00WGSLTypeShade
mod(uv.x, 0.5)
(uv.x - 0.5 * floor(uv.x / 0.5))
mod(uv.x, 0.5)
(uv.x - 0.5 * trunc(uv.x / 0.5))
(uv.x % 0.5)
uv.x % 0.5
roundEven(uv.x)
round(uv.x)
round(uv.x)

A few names are in both languages and mean different things. GLSL's mod rounds the quotient down, and WGSL's % truncates it toward zero. WGSL's round sends a half to the even integer and GLSL's may send it either way, so the compiler writes roundEven. TypeShade has mod and %, and the compiler writes each target the text that keeps the meaning.

GLSL can assign to several components at once, as in c.xy = uv, and so can TypeShade (change 0106): the compiler reads the old components the operation needs into a let, then writes each destination component in turn, so WGSL needs no swizzle_assignment extension. A swizzle that names a component twice, c.xx = uv, is refused with TS8018.

Converting a shader

compile() reads one kind of source, a "use typeshade" file, which is TypeScript. The compiler has no GLSL or WGSL front end, so a shader in either language is written once more in TypeScript. The compiler then writes the WGSL and the GLSL ES 3.00 from that one file.

  • From GLSL: Migrating a GLSL shader covers what a whole shader carries, such as uniform blocks, #ifdef, #include and #extension. GLSL ES 3.00 in TypeShade has the types, uniforms, builtin variables and functions.
  • From WGSL: WGSL in TypeShade gives each WGSL construct its TypeShade spelling.
  • Only in WGSL: a file that uses such a construct still compiles. A scalar uniform, a "read_write" storage binding in a draw and { interpolate: "linear" } are examples. compile() then keeps the WGSL, leaves glsl undefined and says why in a TS8015 warning.

Further reading

Learn what precision highp float; means, compare stage defaults, and inspect browser capabilities in the GLSL precision guide.

Next: Examples, where every example in the compiler’s registry names the targets it emits.

Edit this page Report a problem