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.
let value: f32 = mix(0.0, 1.0, 0.25);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.
"use typeshade";
interface Clip { pos: builtin<"position">;}
interface Color { color: location<0, vec4>;}
@vertexexport 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.) };}
@fragmentexport 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.
struct Clip { @builtin(position) pos: vec4<f32>,}
struct Color { @location(0) color: vec4<f32>,}
@vertexfn 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));}
@fragmentfn 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.
#version 300 esprecision 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);}#version 300 esprecision 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 differs | WGSL | GLSL ES 3.00 |
|---|---|---|
| Compute stages | A 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. |
| Precision | A type carries its own width, so the program declares nothing. | The program opens with a default precision for floating-point and integer values. |
| Builtin inputs | A 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 outputs | A stage returns a struct whose fields carry their locations. | A stage writes to declared out variables, and the position goes to the reserved one. |
| Extensions | A 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
floatf32f32inti32i32uintu32u32boolboolboolvec3vec3<f32>vec3ivec3vec3<i32>vec3iuvec3vec3<u32>vec3ubvec3vec3<bool>vec3bmat4mat4x4<f32>mat4mat2x3mat2x3<f32>mat2x3float[4]array<f32, 4>array<f32, 4>sampler2Dtexture_2d<f32>texture_2d<f32>samplerCubetexture_cube<f32>texture_cube<f32>standalone sampler — fused into the combined sampler2D
samplersamplerBuiltin variables
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
lessThan(uv, vec2(0.5, 0.25))(uv < vec2<f32>(0.5, 0.25))uv < vec2(0.5, 0.25)Same name, other meaning
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.5roundEven(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,#includeand#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, leavesglslundefined and says why in aTS8015warning.
Further reading
Learn what precision highp float; means, compare stage defaults, and inspect browser capabilities in the GLSL precision guide.
- WGSL specification defines the first of the two targets.
- GLSL ES 3.00 specification and the WebGL2 specification define the second.
- MDN WebGPU API and MDN WebGL2RenderingContext show how a host hands each of them to a driver.
- Functions in the language reference gives each builtin with the WGSL and GLSL ES 3.00 text the compiler writes for it, and Types does the same for every type.
- Builtin functions lists all 190 builtins on one page, with the text each backend writes. 64 of them have no GLSL ES 3.00 form of their own: the writer has none, or a pass rewrites the call before the writer runs.
Next: Examples, where every example in the compiler’s registry names the targets it emits.