Skip to content

ModuleCore / FrameConcept: How the kit works

Pipeline warm-up

Compile pipelines behind a loading screen, list each stage's bindings, and find the pipelines warm-up missed.

On this page
import
import { pipelines } from '@typegpu/kit'
call
pipelines.compile(from, options?)also bindingsOf

A pipeline compiles at first use. That waits while TypeGPU resolves WGSL and the browser compiles it. Compile pipelines before rendering to avoid that stall.

Ten pipelines warm up within a per-frame budget

Try Raise the budget to 32 ms. Frames then run past the display's 16.7 ms.

Use pipelines.compile to warm up before the frame loop. It yields to the page between batches so a loading screen can keep drawing:

import {
import pipelines
pipelines
} from '@typegpu/kit';
const
const field: {
initAsync(): Promise<void>;
}
field
=
function createField(): {
initAsync(): Promise<void>;
}
createField
();
const
const final: {
initAsync(): Promise<void>;
}
final
=
function createFinal(): {
initAsync(): Promise<void>;
}
createFinal
();
await
var Promise: PromiseConstructor

Represents the completion of an asynchronous operation

Promise
.
PromiseConstructor.all<[Promise<void>, Promise<void>]>(values: [Promise<void>, Promise<void>]): Promise<[void, void]> (+1 overload)

Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

@param ― values An array of Promises.

@returns ― A new Promise.

all
([
const castle: {
ready: Promise<void>;
}
castle
.
ready: Promise<void>
ready
, // the model's files
import pipelines
pipelines
.
function compile(from: readonly pipelines.Warmable[] | Owner, options?: pipelines.CompileOptions): Promise<void>

Starts compiling every item the list holds at the call, yielding to the event loop past the time budget so the page's other work, such as a loading animation, keeps running. Use before the frame loop starts. Waits for every item to finish, then rejects if any failed.

@param ― from a list of pipelines and modules that compile their own, or an owner that took them

@returns ― a promise that resolves once every item has compiled

compile
([
const field: {
initAsync(): Promise<void>;
}
field
,
const final: {
initAsync(): Promise<void>;
}
final
], {
CompileOptions.onProgress?: ((fraction: number) => void) | undefined

Receives the completed fraction whenever an item finishes compiling. Once it throws, it is not called again, and compile rejects with that error after every item finishes. Default none.

@cpu

onProgress
: (
fraction: number
fraction
) => (
const bar: HTMLProgressElement
bar
.
HTMLProgressElement.value: number

The value property of the HTMLProgressElement interface represents the current progress of the element.

MDN Reference

value
=
fraction: number
fraction
),
}),
]);
  • The list compile([field, final]) copies the list when you call it. It calls initAsync() on each item in that copy and resolves once all of them compile. An item is a TypeGPU pipeline, a , or a that compiles its own pipelines.
  • Budget Each start resolves WGSL and creates a shader module on the main thread. The browser compiles in the background. budget limits how long compile keeps starting compiles before it yields to the event loop. Each stretch starts at least one item. The default is 8 ms, about half a 60 Hz frame. A larger budget can make frames late without increasing compiler throughput.
  • Progress onProgress(fraction) runs as each item finishes; every item counts the same. If onProgress throws, compile stops calling it and waits for every item to finish. compile rejects with that error alone if all items compiled, or includes it with the compile errors in an AggregateError.
  • Failures The promise rejects after every item finishes. Multiple failures produce an AggregateError. A missing inject() fails with an error that names the fill, and a stage over its limit with an error that lists its bindings.
  • Before recording Wait for the promise before recording. TypeGPU throws if a pipeline’s initAsync is pending. If the frame loop starts earlier, return from its until then. A coroutine waits with yield* coroutines.until(compiled).

WebGPU guarantees each shader stage 8 , 12 uniform buffers, 16 textures, 16 and 4 storage textures. A binding counts in every stage its layout entry is visible to, whether or not that stage reads it. TypeGPU shows a resource in the automatic group only to the stages that read it. An entry of a tgpu.bindGroupLayout, such as a shared layout’s, is visible to every stage that can use it unless it sets visibility. A lit adds up the camera, the lighting, its shadows and every lighting ’s resources.

When a pipeline in compile fails while a stage is over its limit, the error lists that kind’s bindings by name, which TypeGPU takes from each resource’s label:

The pipeline 'figure' binds more than WebGPU guarantees per stage:
- vertex: 13 uniform buffers, over 12
- fragment: 13 uniform buffers, over 12
Its uniform buffers:
camera (group 0, binding 0; vertex, fragment)
paint (group 0, binding 7; vertex, fragment)
…

pipelines.bindingsOf(pipeline) reports the same bindings before and after compilation. Its bindings list each layout entry with its group, binding, name, kind and the stages that count it. Its over lists each stage and resource kind over the limit. It counts entries the shader never reads, such as the other parts of a shared layout. An external texture counts as 4 textures, a sampler and a uniform buffer. Pass { limits: root.device.limits } to compare with the device’s own limits.

To make room:

  • Fill a lighting hook on just the renderers that need it, with things.renderer().pipe((cfg) => cfg.with(shading.fog, fogged)). A hook filled in lighting.create applies wherever a pipeline uses that lighting and reads the hook.
  • Pack uniforms that change together into one struct.
  • Keep per-object values in a buffer the pipeline already binds, such as an ’s host fields.
import
import { census } from '@typegpu/kit/debug'
call
census.create(root, options?) → Census
facts
stabilityunstablelifecycledestroy()

A census from @typegpu/kit/debug finds what warm-up missed. Create it right after the root, in development only, and call warm() once warm-up is done:

import {
import census
census
} from '@typegpu/kit/debug';
const
const counted: census.Census
counted
=
import census
census
.
function create(root: TgpuRoot, options?: census.Options): census.Census

Wraps the device's creation methods to count shader modules, pipelines and the bindings in each pipeline's layout, until destroyed. Create it right after the root, before anything compiles, so it sees every layout.

@returns ― counts that grow as pipelines compile, until you destroy it

create
(
const root: TgpuRoot
root
); // before any compile
await
import pipelines
pipelines
.
function compile(from: readonly pipelines.Warmable[] | Owner, options?: pipelines.CompileOptions): Promise<void>

Starts compiling every item the list holds at the call, yielding to the event loop past the time budget so the page's other work, such as a loading animation, keeps running. Use before the frame loop starts. Waits for every item to finish, then rejects if any failed.

@param ― from a list of pipelines and modules that compile their own, or an owner that took them

@returns ― a promise that resolves once every item has compiled

compile
([
const field: {
initAsync(): Promise<unknown>;
}
field
]);
const counted: census.Census
counted
.
Census.warm(): void

Marks warm-up complete. Later synchronous compilations count as late.

warm
(); // a compile from now on is late
  • It wraps the device’s createShaderModule, createBindGroupLayout, createPipelineLayout and four pipeline methods. destroy(), or using, restores them.
  • late lists the pipelines created synchronously after warm(). It can reveal compiles that happen during a frame. Play through the game to catch an effect or a menu that compiles on first use.
  • Each entry of pipelines describes one pipeline. Its bindings list the layout entries, counted as bindingsOf counts them. groups counts its bind groups, including TypeGPU’s automatic group, and over lists the stages over a limit.
  • overBudget lists the pipelines with a stage over a limit or more bind groups than maxBindGroups. The limits default to the minimums WebGPU guarantees on every device.
  • modules and wgslBytes count the shader modules and their WGSL.

pipelines.blend.add adds light, for sparks, glow and bloom. pipelines.blend.over lays premultiplied colour over what is there, for a fragment that returns vec4f(rgb * a, a). pipelines.blend.straight lays straight colour, for one that returns vec4f(rgb, a). Give any of them to a pipeline’s target:

root.createRenderPipeline({ vertex, fragment, targets: { format, blend: pipelines.blend.add } });
  • The budget covers only compilation starts. Allow time for the rest of your frame. At 120 Hz a frame lasts about 8 ms, so use a smaller budget.
  • Progress counts items, so a module of six pipelines moves the bar as much as one pipeline. Pass a module’s pipelines one by one to count each.
  • Started compiles can’t be cancelled, so a typo in the first item is reported only after the slowest compile.
  • Nothing warms a pipeline the first frames don’t use, such as an effect that fires later. On WebKit it stalls the first time it draws.
  • Only pipelines compiled through compile, directly or inside a module, fail with the binding list. A pipeline that compiles at its first draw reports the device’s error.
frameA frame is one turn of the loop, and the object your function gets for it, which holds the encoder, the time and the time since the last frame.You own the frame →
pass descriptionA pass description says once which textures a pass draws into and what each holds, and builds the pipeline for every drawing added to it.Scenes and passes →
moduleA module is one piece of GPU work, made by a factory that borrows your root and returns resources, shader functions and methods that record into your frame.A module borrows →
bindingA binding is one buffer, texture or sampler in a bind group, which a shader reads by its place there.Words →
recorderThe recorder is the function you give the frame loop, which records one frame into the encoder it is handed.Words →
dispatchA dispatch runs a kernel once over a number of threads, inside a compute pass.Words →
storage bufferA storage buffer is GPU memory that shaders read by index and kernels can write, which is where the kit keeps rows.Words →
samplerA sampler tells the GPU how to read a texture between its texels and past its edges, such as smoothly or in blocks.Words →
rendererA renderer draws one sort of thing, such as instances or particles, by handing a pass description a drawing.Every renderer is a drawing →
hookA hook is a placeholder that each pipeline fills with its own choice, such as the camera it draws through.Members and placeholders →
bind groupA bind group is a set of buffers, textures and samplers that a pipeline reads, and a pipeline has room for four.Words →
kindA kind adds fields to the rows of a set of instances and replaces functions in the shaders that draw them.Kinds →

Reference

kit.pipelines

cpuHow to read this card

Compiles pipelines behind a loading screen, lists a pipeline's bindings per stage, and provides blend states.

Callstakesreturns

pipelines.compile(from, options?)

from
a list of pipelines and modules that compile their own, or an owner that took them
takes

CompileOptions

2 fields
onProgress?

(fraction: number) => void

Receives the completed fraction whenever an item finishes compiling.Default none.

budget?

number

Limits time spent starting compilations at a stretch, in milliseconds; starting an item resolves its WGSL on the calling thread.Default 8.

pipelines.bindingsOf(pipeline, options?) → Bindings

pipeline
a TypeGPU render or compute pipeline
Bindings
its bindings and the stages over their limits
takes

BindingsOptions

1 field
limits?

Partial<Record<(typeof limitNames)[Overflow['kind']], number>>

Sets the per-stage limits to compare with, such as root.device.limits.Default the minimums WebGPU guarantees: 8 storage buffers, 12 uniform buffers, 16 textures, 16 samplers and 4 storage textures.

returns

Bindings

3 members
label

string

Names the pipeline by its label.

bindings

Binding[]

Lists every entry of every bind group layout the pipeline binds, in group and binding order.

over

Overflow[]

Lists each stage and kind over its limit.

Exports

constants
blend
types
Warmable ShaderStage BindingKind Binding Overflow

used byAfterimageBenthosBlockfallCenserClassroomCloisterDomino runEmber mirrorFalling sandFireworksFish tankFlow fieldForestHex isleJelliesJelly worldJump floodKineticLatticeNight marketOceanPlatformerQuiver treesRaymarchRobot armSkeletonsSlime mouldSolar systemSPH sculptSpiderStampedeStar mapSurvey model

Counts pipelines, their bindings per stage, and late compiles.

Callsborrows, never destroysreturns

census.create(root, options?) → Census

root
the root
Census
counts that grow as pipelines compile, until you destroy it
takes

Options

1 field
limits?

BindingsOptions['limits'] & { maxBindGroups?: number }

Sets the limits to compare with, such as root.device.limits.Default the minimums WebGPU guarantees: 4 bind groups, 8 storage buffers, 12 uniform buffers, 16 textures, 16 samplers and 4 storage textures per stage.

returns

Census

7 members
pipelines

CensusEntry[]

Lists every pipeline created so far, in creation order.

modules

number

Counts the shader modules created so far.

wgslBytes

number

Sums the WGSL length of every shader module created so far, in characters.

late

CensusEntry[]

Lists pipelines created synchronously after warm().

overBudget

CensusEntry[]

Lists pipelines with a stage over its limits, or more bind groups than allowed.

warm()

Marks warm-up complete.

destroy()

Restores the device's methods.

Holds

lifecycle

destroy()[Symbol.dispose]

Exports

types
CensusEntry