Introduction
WGSL, the WebGPU Shading Language, is the shader language of WebGPU, the W3C’s graphics and compute API for web browsers. Browsers take WGSL source as text and translate it into whatever the system’s graphics API needs, such as HLSL, Metal or SPIR-V. It is also used outside the browser by native WebGPU libraries such as wgpu and Dawn.
Structure
A WGSL file is plain text with a syntax closer to Rust than to C. Functions are
declared with fn, variables with var, let or
const, and types come after names, as in x: f32. Attributes
start with @: @vertex, @fragment and
@compute mark entry points, @location and
@builtin mark inputs and outputs, and @group and
@binding place resources. One file can hold several entry points. The
lexical rules, directives, types, declarations, statements, attributes, built-in values
and memory layout are on the language page;
every built-in function is on built-in
functions, and every keyword, reserved word and token on
keywords and tokens.
@fragment
fn fs_main(@location(0) colour: vec3f) -> @location(0) vec4f {
return vec4f(colour, 1.0);
}
Not described
Known, not yet covered
- Reference: the layout constraints of each address space, the
const_assertgrammar, and the full overload lists of the texture built-in functions (summarised on built-ins), all in the WGSL specification. - Viewer: there is no viewer for WGSL yet.
Links
Software
Developers
Reference
- WebGPU Shading Language – the W3C specification