 | [TOC] Chapter 13: Shader Language |  |
 | Introduction |  |
The WebGPU Shader Language (WGSL) is the language used for writing shaders in WebGPU. WGSL is designed to be easy to read and write, with syntax and structure specifically suited to GPU programming. This chapter will introduce the key aspects of WGSL, including data types, control structures, operators, built-in functions, and compiling shader modules. By understanding WGSL fundamentals, you can harness WebGPU’s power for custom computations and visual effects.
WGSL is a high-level language created specifically for WebGPU. Unlike other shading languages like GLSL, HLSL, or SPIR-V, WGSL is streamlined to be JavaScript-friendly and more readable for web developers. WGSL offers strict type safety, simpler syntax, and fewer ambiguities, making it particularly well-suited for web applications that rely on the GPU.
WGSL is based on a structured and statically typed approach, meaning variables must be explicitly typed, and there’s no implicit casting. This enhances reliability and predictability, critical for GPU programs that execute on massive parallel threads. WGSL supports defining shader code for each stage (vertex, fragment, or compute), which can then be compiled and used in WebGPU pipelines.
 | WGSL Data Types and Variables |  |
WGSL offers several fundamental data types, including scalars, vectors, and matrices, which are essential for GPU programming due to their ability to represent positions, colors, normals, and transformations compactly.
Scalar Types
WGSL supports standard scalar types:
• `i32` and `u32`: Signed and unsigned 32-bit integers.
• `f32`: 32-bit floating-point numbers, commonly used for positions, colors, and other continuous data.
• `bool`: Boolean values (`true` or `false`), typically used in control structures.
Vector Types
Vectors are widely used in GPU programming to represent positions, colors, and directions. WGSL supports vectors of scalar types in lengths of 2, 3, or 4:
• `vec2`: A 2D vector of floats, for representing 2D coordinates or UV mappings.
• `vec3`: A 3D vector of floats, commonly used for positions and directions.
• `vec4`: A 4D vector, frequently used for color (RGBA) and homogeneous coordinates.
Example of using vectors in WGSL:
var pos: vec3<f32> = vec3<f32>(1.0, 2.0, 3.0); var color: vec4<f32> = vec4<f32>(1.0, 0.0, 0.0, 1.0); // RGBA red
Matrix Types
Matrices are vital for transformations in 3D space. WGSL supports `mat2x2`, `mat3x3`, and `mat4x4` of `f32`:
var transform: mat4x4<f32> = mat4x4<f32>( 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 );
Variables
Variables in WGSL are declared with the `var` keyword, and constants with the `let` keyword. All variables must be typed, which ensures clear, predictable behavior. For GPU programming, storage classes specify where data is stored and accessed:
• `@private`: Accessible only within the shader’s function.
• `@uniform`: Shared across all threads; typically used for constants like transformation matrices.
• `@storage`: Used for data shared across threads and modified during execution.
Example variable declarations:
@group(0) @binding(0) var<uniform> viewMatrix: mat4x4<f32>; @group(0) @binding(1) var<storage, read_write> particlePositions: array<vec4<f32>>;
 | WGSL Control Structures |  |
Control structures in WGSL allow for flow control in shader logic, including conditional statements and loops. This is important for managing complex conditions and repetitive calculations.
Conditional Statements
Conditional statements include `if` and `else`. They follow a structure similar to JavaScript, but each condition must evaluate to a `bool`.
if (color.a > 0.5) { // Execute if the alpha is greater than 0.5 } else { // Execute if the alpha is less than or equal to 0.5 }
Loops
WGSL supports `for` and `while` loops for repeating calculations. Loops are generally avoided on GPUs due to potential performance issues, but they are still useful for specific tasks.
|