VariantFamily
Interface in Variants
A built family.
import type { VariantFamily } from 'typeshade'
The signature, the description and the examples come from the compiler's own source at commit 26de7be8.
Syntax
interface VariantFamily<A extends Record<string, readonly unknown[]>> { readonly variants: readonly Variant<A>[]; readonly keys: readonly string[]; get(key: string): Variant<A> | undefined; emit(target: 'wgsl', opts?: EmitOptions): ReadonlyMap<string, string>; emit( target: 'glsl-es300', opts?: GlslEmitOptions & { stage?: 'vertex' | 'fragment' }, ): ReadonlyMap<string, string>; emitGuarded( defines: GuardDefines<A>, opts?: GlslEmitOptions & { stage?: 'vertex' | 'fragment' }, ): string; emitGuardedFragment( defines: GuardDefines<A>, opts?: GlslEmitOptions & { stage?: 'vertex' | 'fragment' }, ): EmitFragment;}Instance properties
variantsread onlyreadonly Variant<A>[]keysread onlyreadonly string[]Every key, in
variantsorder: the id set a pipeline cache keys its programs by.
Instance methods
get(key: string): Variant<A> | undefinedLook one variant up by key;
undefinedwhen the key names no variant.emit(target: 'wgsl', opts?: EmitOptions): ReadonlyMap<string, string>One preprocessor-free source per key. The only shape WGSL can take, and the one a pipeline cache should prefer on GLSL too.
emit(target: 'glsl-es300', opts?: GlslEmitOptions & { stage?: 'vertex' | 'fragment' }): ReadonlyMap<string, string>emitGuarded(defines: GuardDefines<A>, opts?: GlslEmitOptions & { stage?: 'vertex' | 'fragment' }): stringOne GLSL source whose arms are selected by preprocessor defines the host owns.
Opt in to this when the host already sets the define per draw and cannot pick a source per variant; otherwise prefer
VariantFamily.emit. GLSL only, because the arms are selected by the GLSL preprocessor. Every arm is byte-identical to the correspondingemit()source, so a program selected through the ladder is the same program as the standalone variant.emitGuardedFragment(defines: GuardDefines<A>, opts?: GlslEmitOptions & { stage?: 'vertex' | 'fragment' }): EmitFragmentThe same generated ladder as
VariantFamily.emitGuarded, returned as a header-less fragment for a host that owns the program and composes these declarations into it.emitGuardedreturns a whole stage, preamble included. A ladder pasted into a host program through an#includecannot carry a second#version, so here the preamble comes back as data, exactly asemitGlslFragmentreturns it, and[...preamble, '', source].join('\n')reproducesemitGuarded’s output byte for byte.declaresandrequiresare the union across the arms, since the host’s preprocessor picks the arm and the composer cannot know which: every name any arm declares is one the host’s prelude must not collide with, and every symbol any arm needs is one the host must provide. Throws for a preamble mismatch exactly asemitGuardeddoes.
See also
Source
src/core/variant-family.ts, line 79, at commit 26de7be8