WebGPU Shading Language
WebGPU Shading Language (WGSL, internet media type: History and backgroundGraphics 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.[4][2] 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.[1] Design goalsWGSL’s design emphasizes:
Language overviewTypes and valuesCore scalar types include Variables and address spacesVariables are declared with Functions and control flowFunctions use explicit parameter and return types. Control flow includes Entry points and attributesShaders define stage entry points with ResourcesWGSL exposes buffers ( Compilation and validationBrowsers compile WGSL to platform-appropriate representations and native driver formats; the specific compilation pipeline is not observable by web content.[2] 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.[2][1] Shader stagesWGSL supports three pipeline stages: vertex, fragment, and compute.[1] Vertex shadersVertex shaders transform per-vertex inputs and produce values for rasterization, including a clip-space position written to the Example/* Transforms positions by an MVP matrix and passes through color. */
struct VertexInput {
@location(0) position : vec3f,
@location(1) color : vec3f,
};
struct VertexOutput {
@builtin(position) clip_position : vec4f,
@location(0) color : vec3f,
};
@group(0) @binding(0)
var<uniform> mvp : mat4x4f;
@vertex
fn main(v_in : VertexInput) -> VertexOutput {
var v_out : VertexOutput;
v_out.clip_position = mvp * vec4f(v_in.position, 1.0);
v_out.color = v_in.color;
return v_out;
}
Fragment shadersFragment shaders run per-fragment and compute color (and optionally depth) outputs written to color attachments.[1] Example/* Writes an interpolated color with opaque alpha. */
@fragment
fn main(@location(0) color : vec3f) -> @location(0) vec4f {
return vec4f(color, 1.0);
}
Compute shadersCompute shaders run in workgroups and are used for general-purpose GPU computations.[1] Example/* Doubles elements from an input buffer into an output buffer. */
struct Params {
element_count : u32,
};
@group(0) @binding(0)
var<storage, read> in_data : array<f32>;
@group(0) @binding(1)
var<storage, read_write> out_data : array<f32>;
@group(0) @binding(2)
var<uniform> params : Params;
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) gid : vec3<u32>) {
let idx : u32 = gid.x;
if (idx >= params.element_count) { return; }
out_data[idx] = in_data[idx] * 2.0;
}
Differences from GLSL and HLSLCompared with legacy shading languages, WGSL:
See alsoOther shading languages
References
External links
|