VariantFamily
On this page

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 only readonly Variant<A>[]
keysread only readonly string[]

Every key, in variants order: the id set a pipeline cache keys its programs by.

Instance methods

get (key: string): Variant<A> | undefined

Look one variant up by key; undefined when 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' }): string

One 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 corresponding emit() 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' }): EmitFragment

The 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.

emitGuarded returns a whole stage, preamble included. A ladder pasted into a host program through an #include cannot carry a second #version, so here the preamble comes back as data, exactly as emitGlslFragment returns it, and [...preamble, '', source].join('\n') reproduces emitGuarded’s output byte for byte.

declares and requires are 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 as emitGuarded does.

See also

Source

src/core/variant-family.ts, line 79, at commit 26de7be8

Edit this page Report a problem