overrideConst
On this page

overrideConst()

Function in Authoring

Declare a pipeline specialization constant: one value that is fixed when a pipeline is created, later than module build and earlier than the draw.

import { overrideConst } from 'typeshade'

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

Syntax

function overrideConst<T extends ShaderType>(
name: string,
type: T,
defaultValue: number | boolean,
): OverrideHandle<KeyOf<T>>

Parameters

name string

the override’s name, as the host spells it in constants or in a define.

type T extends ShaderType

the scalar shader type of the value.

defaultValue number | boolean

the value used when the host pins nothing.

Return value

OverrideHandle<KeyOf<T>>

an OverrideHandle: .node reads the value, .decl goes into module({ overrides }).

Exceptions

SD0014

SD0014 when type is not a scalar.

Description

WGSL emits a module-scope override name: type = default;, and the host pins it through pipeline constants, createRenderPipeline({ constants: { name } }). GLSL ES 3.00 has no driver-side equivalent, so a specialized variant is a re-emit: pass emitGlslModule(m, stage, { overrideValues }) and the backend writes a generated #define NAME <value> placed after the #version line, which is where GLSL will accept it. An override the caller does not name keeps its default. The values a host passes on either target come from reflect(m).overrides, which reports each name, type and default.

The read stays opaque to the optimizer, so a branch guarded by q.node survives every DSL pass and the driver eliminates it per variant. That is the ubershader mechanism: one module, one emit, N specialized pipelines.

WGSL allows scalar overrides only, so type must be bool, i32, u32 or f32.

Examples

Example

import { overrideConst, module, reflect, u32T } from 'typeshade'
const quality = overrideConst('QUALITY', u32T, 1)
const m = module({ overrides: [quality.decl], funcs: [fs] })
reflect(m).overrides // [{ name: 'QUALITY', type: u32T, default: 1 }]

In the guide

See also

Source

src/core/ir/builder.ts, line 1501, at commit 26de7be8

Edit this page Report a problem