WebGPU Shading Language


WebGPU Shading Language 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, 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.
  • Portability across diverse GPU backends via an abstract resource model shared with WebGPU.
  • Readability and explicitness.
  • Alignment with modern GPU features while retaining a familiar C/Rust-like syntax.

    Language overview

Types and values

Core scalar types include,,, and. Vectors and matrices are available for floating-point element types. Optional 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 , , or . Storage classes 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, , , ,, and.

Resources

WGSL exposes buffers, textures, and samplers. 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


/* Transforms positions by an MVP matrix and passes through color. */
struct VertexInput ;
struct VertexOutput ;
@group @binding
var mvp : mat4x4f;
@vertex
fn main -> VertexOutput

Fragment shaders

Fragment shaders run per-fragment and compute color outputs written to color attachments.

Example


/* Writes an interpolated color with opaque alpha. */
@fragment
fn main -> @location vec4f

If half-precision is desired, the code must be prefaced with a enable f16; statement.

Compute shaders

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

Example


/* Doubles elements from an input buffer into an output buffer. */
struct Params ;
@group @binding
var in_data : array;
@group @binding
var out_data : array;
@group @binding
var params : Params;
@compute @workgroup_size
fn main

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.

    Other shading languages

  • GLSL, shading language for OpenGL
  • HLSL, Microsoft's shading language for Direct3D
  • Metal Shading Language, Apple's shading language for Metal
  • Cg, NVIDIA's C-based shading language
  • Open Shading Language, offline rendering shading language