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
constfield:{
initAsync():Promise<void>;
}
field=
functioncreateField():{
initAsync():Promise<void>;
}
createField();
const
constfinal:{
initAsync():Promise<void>;
}
final=
functioncreateFinal():{
initAsync():Promise<void>;
}
createFinal();
await
var Promise:PromiseConstructor
Represents the completion of an asynchronous operation
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
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)=>(
constbar:HTMLProgressElement
bar.
HTMLProgressElement.value: number
The value property of the HTMLProgressElement interface represents the current progress of the element.
The listcompile([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.
ProgressonProgress(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.
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
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([
constfield:{
initAsync():Promise<unknown>;
}
field]);
constcounted: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:
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 →
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.
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.