my-server
← Wiki

WebGPU Shading Language

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.

History and background

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.

Design goals

WGSL's design emphasizes:

  • Safety and determinism suitable for web security constraints (extensive static validation and well-defined semantics).
  • Portability across diverse GPU backends via an abstract resource model shared with WebGPU.
  • Readability and explicitness (no preprocessor, minimal implicit conversions, explicit address spaces and bindings).
  • Alignment with modern GPU features (compute, storage buffers, textures, atomics) while retaining a familiar C/Rust-like syntax.

Language overview

Types and values

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 and 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 and control flow

Functions use explicit parameter and return types. Control flow includes , , , , and constructs, with /. Recursion is disallowed; entry-point call graphs must be acyclic.

Entry points and attributes

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 .

Resources

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.

Compilation and validation

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.

Shader stages

WGSL supports three pipeline stages: vertex, fragment, and compute.

Vertex shaders

Vertex shaders transform per-vertex inputs and produce values for rasterization, including a clip-space position written to the builtin.

Example

Fragment shaders

Fragment shaders run per-fragment and compute color (and optionally depth) outputs written to color attachments.

Example

If half-precision (, shorthand for ) is desired, the code must be prefaced with a statement.

Compute shaders

Compute shaders run in workgroups and are used for general-purpose GPU computations.

Example

Differences from GLSL and HLSL

Compared with legacy shading languages, WGSL:

  • Omits a preprocessor and requires explicit types and conversions.
  • Uses explicit address spaces and binding annotations aligned with WebGPU's model.
  • Enforces strict validation to avoid undefined behavior common in other shading languages.
  • Defines a portable, web-focused feature set; 16-bit types and other features are opt-in and may depend on device capabilities.

See also

Other shading languages

References

External links