WebGPU Shading Language (WGSL, internet media type: ) is a high-level shading language and the normative shader language for the WebGPU API on the web. WGSL's syntax is influenced by Rust and is designed with strong static validation, explicit resource binding, and portability in mind for secure execution in browsers. In web contexts, WebGPU implementations accept WGSL source and perform compilation to platform-specific intermediate forms (for example, to SPIRâÂÂV, DXIL, or MSL via the user agent), but such backends are not exposed to web content.
Graphics on the web historically used WebGL, with shaders written in GLSL ES. As applications demanded more modern GPU features and finer control over compute and graphics pipelines, the W3C's GPU for the Web Community Group and Working Group created WebGPU and its companion shading language, WGSL, to provide a secure, portable model suitable for the web platform. WGSL was developed to be human-readable, avoid undefined behavior common in legacy shading languages, and align closely with WebGPU's resource and validation model.
WGSL's design emphasizes:
Core scalar types include , , , and . Vectors (e.g., , , ) and matrices (up to 4ÃÂ4) are available for floating-point element types. Optional (half precision) may be enabled via a WebGPU feature; availability is implementation-dependent. Atomic types (, ) support limited atomic operations in qualified address spaces.
Variables are declared with (immutable), (mutable), or (compile-time constant). Storage classes (address spaces) include , , , , and with or access as applicable. WGSL defines explicit layout and alignment rules; attributes such as , , and control data layout for buffer interoperability.
Functions use explicit parameter and return types. Control flow includes , , , , and constructs, with /. Recursion is disallowed; entry-point call graphs must be acyclic.
Shaders define stage entry points with , , or . Attributes annotate bindings and interfaces, including , (resource binding), (user-defined I/O), (stage built-ins such as or ), , and .
WGSL exposes buffers (, ), textures (sampled, storage, and multisampled variants), and samplers (filtering/non-filtering/comparison). The binding model is explicit via descriptor sets called groups and bindings, matching WebGPU's pipeline layout model.
Browsers compile WGSL to platform-appropriate representations and native driver formats; the specific compilation pipeline is not observable by web content. WGSL source undergoes strict parsing and static validation, and WebGPU enforces robust resource access rules to avoid out-of-bounds memory hazards, contributing to predictable behavior across implementations.
WGSL supports three pipeline stages: vertex, fragment, and compute.
Vertex shaders transform per-vertex inputs and produce values for rasterization, including a clip-space position written to the builtin.
Fragment shaders run per-fragment and compute color (and optionally depth) outputs written to color attachments.
If half-precision (, shorthand for ) is desired, the code must be prefaced with a statement.
Compute shaders run in workgroups and are used for general-purpose GPU computations.
Compared with legacy shading languages, WGSL: