Const Declaration
A const declaration gives a permanent name to a compile-time immutable value. These constants are evaluated entirely on the CPU by the compiler when your shader module is created, resulting in zero runtime overhead.
Scoping
A const can be declared in two locations:
- Module Scope: Declared at the top level of your shader (outside any function). It is visible to all functions in the shader module.
- Function Scope: Declared inside a function block. It is visible only within that block (and nested sub-blocks) after its point of declaration.
Syntax and Initialization
A const declaration must be initialized immediately, and can specify an optional type. If the type is omitted, the compiler automatically infers it from the initializer expression.
Explicit Type Syntax:
Inferred Type Syntax:
Examples
Constraints
To ensure compile-time evaluation, const declarations are subject to the following rules:
- Immutable: Constant values cannot be reassigned or modified. Any attempt to write to a
constcauses a compilation error. - Initializer Expression: The initializer must be a valid constant-expression. It can only be composed of literal values, other compile-time constants, and
@constbuilt-in function calls. It cannot depend onletvariables,overrideconstants, function parameters, or mutablevarvariables.
const BASE_SPEED = 2.0; // ✔️ VALID: literal initializer
const MAX_SPEED = BASE_SPEED * 1.5; // ✔️ VALID: constant-expression initializer
fn process(runtime_multiplier: f32) {
// ❌ COMPILE ERROR: runtime_multiplier is a dynamic parameter, not a constant
const dynamic_speed = BASE_SPEED * runtime_multiplier;
}
Deep-Dive: Constant Evaluation Stage
For details on how the compiler processes constants, including CPU-side constant folding, @const built-in function execution, and compile-time assertions, see the Constant Stage reference.