ConstDecl
On this page

ConstDecl

Interface in IR

A ModuleDecl.consts entry: a module-scope constant.

import type { ConstDecl } from 'typeshade'

The signature, the description and the examples come from the compiler's own source at commit 26de7be8.

Syntax

interface ConstDecl {
readonly name: string;
readonly type: ShaderType;
readonly wgslValue: number;
readonly cpuValue: number;
readonly valueExpr?: Expr;
}

Description

For a scalar, write a plain object literal with one value per precision, for example { name: 'PI', type: f32T, wgslValue: 3.14159265, cpuValue: Math.PI }: the WGSL and GLSL backends emit wgslValue, and the CPU backend, which runs the module in double precision, uses cpuValue. For a vector, array or struct constant use constExpr, which fills valueExpr instead. When both forms are present, valueExpr wins.

Instance properties

nameread only string
typeread only ShaderType
wgslValueread only number

Scalar value emitted by the WGSL/GLSL backends (the truncated shader constant). Used when valueExpr is absent; ignored otherwise.

cpuValueread only number

Scalar value used by the CPU backend, at full double precision. Used when valueExpr is absent; ignored otherwise.

valueExproptionalread only Expr

Optional constant value as an IR literal expression, for example a vec4<f32>(…) colour, an array<vec4<f32>, N>(…) palette, or a struct literal. When present it replaces wgslValue and cpuValue on every backend: WGSL and GLSL emit it and the CPU backend evaluates it. It must be a constant-foldable literal expression (lit, construct, unop or binop over those, or a constref to an earlier constant); it may not read a binding, a parameter, or a runtime input. Ordinary f32 scalars keep using the wgslValue/cpuValue pair.

See also

Source

src/core/ir/nodes.ts, line 377, at commit 26de7be8

Edit this page Report a problem