Open code, practice, and notes →
Download source with English comments · for reading
WGSL · 04 Const, Let, Var, and Override
Series contents · Technology map
Stage: Types and Shader Interfaces · Editable example · Storage ID: wgsl-step-04
Goals and learning outcomes
- Choose declarations that match value lifetimes.
- Distinguish pipeline constants from runtime inputs.
Prerequisites
Continue using the output and records from Unit 3. Before experimenting with Const, Let, Var, and Override, preserve the previous inputs and reference outputs.
Key concepts and explanations
Mutability
const and let express values that are not reassigned, but differ in evaluation time. Use var for state that changes during function execution.
Pipeline Constants
override defines a value that can be supplied when creating a pipeline. A resource such as a uniform is more suitable than override for inputs that change every frame.
Practice setup
First check the execution scope guidance on the example screen. The following describes the environment needed to extend the assignment in a separate project. This document-guided exercise requires your own implementation in a separate localhost or HTTPS project. Check the secure context, navigator.gpu, and adapter request result, and provide an unsupported-environment message. Retain device.lost handling and resource cleanup.
The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.
Practice tasks
- In constants.wgsl, distinguish a const coefficient, a let intermediate value, and a var accumulator.
- Set override SCALE to 1 and 2 through pipeline constants.
- Inspect a let reassignment error and move per-frame color input into a uniform.
Variables to observe
| Variable | Baseline | Change | Expected change / hypothesis |
|---|---|---|---|
| SCALE | 1 | Create a pipeline with 2 | The pipeline's result is expected to double, with a lifetime different from a uniform update. |
Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.
Completion criteria
- Tabulate when the four declarations are evaluated and can change.
- Record the results for two pipeline constants and the let reassignment error.
Self-check
- Why does changing override require a different pipeline configuration?
- Which declaration suits a value that stays unchanged during a function call?
Official references
Linked example and execution scope
Example source · Runtime environments and limits
Runs actual WebGPU/WGSL. Requires HTTPS/localhost and a supported adapter; unsupported environments display a reason, and CPU results are not presented as GPU results.
Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.
Next learning
Previous: Creating and Multiplying Vectors and Matrices · Next: Function and Structure Contracts