constDecl
On this page

constDecl()

Function in Layout

Declare a module-level scalar constant together with its typed reference in one call.

import { constDecl } from 'typeshade'

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

Syntax

function constDecl<T extends ShaderType>(
name: string,
type: T,
values: { readonly wgsl: number; readonly cpu: number },
): ConstHandle<T>

Parameters

name string

the emitted constant name.

type T extends ShaderType

the constant’s scalar type.

values { readonly wgsl: number; readonly cpu: number }

wgsl, the literal WGSL emits, and cpu, the value the CPU oracle uses.

Return value

ConstHandle<T>

the ConstHandle, .decl and .node.

Description

WGSL emits values.wgsl as the literal, in the same spelling a hand-written ConstDecl uses (for example PI = 3.14159265). The CPU oracle, the same module run in double precision on the CPU, evaluates values.cpu at full JS double precision, so a WGSL literal you deliberately shorten does not lower the precision of the parity check.

Returns a ConstHandle. Its .decl goes into module({ consts }) or, more usually, into module({ uses }), which collects the .decl of any handle passed to it. Its .node is the typed reference to use at call sites, so a renamed or misspelled constant is a tsc error everywhere it is used. For a constant of vector, matrix or array type (a vec4<f32> colour, an array<vec4<f32>, N> palette) use constExpr, which takes one constant-foldable literal node in place of the WGSL/CPU value pair.

Examples

Example

const TAU = constDecl('TAU', f32T, { wgsl: 6.2831853, cpu: Math.PI * 2 })
const g = fn('g', { t: f32T }, ({ t }) => t.mul(TAU.node))
const m = module({ consts: [TAU.decl], funcs: [g] })

In the guide

See also

Source

src/core/sot.ts, line 76, at commit 26de7be8

Edit this page Report a problem