record into an encoder you hand them. The loop creates one encoder each , runs your with it and submits it once, so a GPU capture shows the whole frame.
What each line of a frame records, and what it sends at once
frames.create(root, (frame) => { const compute = frame.encoder.beginComputePass(); sparks.spawn(compute); field.step(compute); sparks.step(compute); sparks.compact(compute, sim.ahead); compute.end(); probe.capture(frame); const pass = scene.begin(frame); field.draw(pass); sparks.draw(pass); pass.end();});// after the submit, probe maps its copy
Encoder
GPU
After the submit
Try Throw on any line. The writes already sent stay, and the encoder's passes never reach the GPU.
Records a frame into one encoder and submits once, with a clock. Borrows the root. The
synchronous recorder borrows each frame's encoder.
@submits ― always once a frame after your recorder returns, and again at each frame.submit()
@param ― record your frame, recorded into each frame's encoder
@returns ― a loop you run with each frame's time and destroy
create(
constroot:TgpuRoot
root,({
encoder: TgpuCommandEncoder
Records commands for submission after the recorder returns. submit() replaces this encoder.
Read it at each call instead of keeping it, and let the frame submit it.
encoder,
time: number
Reports seconds since the first submitted frame.
time,
dt: number
Reports seconds since the previous submitted frame. The first frame has a dt of zero.
Runs the loop on each scheduled frame. Time advances by at most maxDt per frame, never steps
back, and stops while the surface is paused. Destroying the animation keeps the loop alive.
Another animation on the same loop continues from its recorded time. A frame error, or a run
that returns false because the loop was destroyed, stops the animation.
@param ― loop the loop to run on each unpaused frame. Continues from its seconds if it ran before
@returns ― an animation you destroy to stop the frames
animate(
constloop:frames.Frames
loop);// calls loop.run(seconds) at every animation frame
The clocktime is seconds since the first submitted frame. dt is seconds since the previous frame and is 0 on the first. index counts frames submitted before this one. loop.seconds holds the time of the last submitted frame, so a frame that throws leaves it unchanged.
Scheduling The loop schedules nothing. run(seconds) records one frame and submits it. It throws a RangeError if seconds isn’t finite or is smaller than loop.seconds.
Synchronous The recorder is synchronous. Returning a promise throws a TypeError, and so does calling run from inside it.
Errors If the recorder throws or calls destroy(), the frame submits no further commands and the clock doesn’t advance. Queue writes and commands already sent with frame.submit() still run. After destroy(), run returns false.
frames.animate(loop, options) runs the loop at every animation frame, on the web and React Native.
Stalls Each frame advances time by at most maxDt, which defaults to 0.1 s. A frame after a stall, or after a hidden tab comes back, adds no more than that.
Pausing With a surface, animation and time stop while it is paused. The first frame after resume() has a dt of 0. On React Native, pause it from AppState. On the web, pause it for a canvas scrolled out of view or a pause menu; browsers already stop a hidden tab’s frames.
Stoppingdestroy() on the animation returned by animate requests no more frames. Call it from inside a frame to make that frame the last. The loop stays yours. If you destroy the loop, the animation stops too.
Errors An error ends animation and reaches onError once. Without onError, the animation frame callback throws it.
Wall timeframe.time is the loop’s own time, so it drifts from performance.now() after the first pause or stall. Read performance.now() for wall time.
A canvas that draws only while on screen pauses its surface from an IntersectionObserver:
Runs the loop on each scheduled frame. Time advances by at most maxDt per frame, never steps
back, and stops while the surface is paused. Destroying the animation keeps the loop alive.
Another animation on the same loop continues from its recorded time. A frame error, or a run
that returns false because the loop was destroyed, stops the animation.
@param ― loop the loop to run on each unpaused frame. Continues from its seconds if it ran before
@returns ― an animation you destroy to stop the frames
animate(
constloop:frames.Frames
loop,{
AnimateOptions.surface?:{
readonly paused: boolean;
}|undefined
Stops frames and elapsed time while paused is true. The first frame after resume() has a
dt of 0. On React Native, pass the canvas surface and pause it from AppState. On the web,
pause it from an IntersectionObserver or a pause menu. Without a surface, frames run until
the animation is destroyed. Default none.
surface});
new
var IntersectionObserver:new(callback:IntersectionObserverCallback,options?:IntersectionObserverInit)=>IntersectionObserver
The IntersectionObserver interface of the Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport. The ancestor element or viewport is referred to as the root.
The isIntersecting read-only property of the IntersectionObserverEntry interface is a Boolean value which is true if the target element intersects with the intersection observer's root.
The observe() method of the IntersectionObserver interface adds an element to the set of target elements being watched by the IntersectionObserver. One observer has one set of thresholds and one root, but can watch multiple target elements for visibility changes in keeping with those.
A records its copy in the frame but can only map it once the copy is submitted. A module that has to wait for the submit registers a callback while it records:
function
functioncreateProbe(root:TgpuRoot):{
record(frame:Frame):void;
}
createProbe(
root: TgpuRoot
root:
(alias)interfaceTgpuRoot
importTgpuRoot
TgpuRoot/* , ... */){
const
constmap:()=>void
map=()=>{
// staging.mapAsync(GPUMapMode.READ).then(...)
};
return{
functionrecord(frame:Frame):void
record(
frame: Frame
frame:
(alias)interfaceFrame
importFrame
Represents one frame being recorded. Each run creates a separate object, so modules can
compare frames by identity.
Frame){
// copy into the staging buffer on frame.encoder
frame: Frame
frame.
Frame.afterSubmit(callback: ()=>void): void
Calls callback after the frame's last submit, in registration order. Drops it if the frame
does not finish. Throws unless this frame is recording, so a retained frame cannot add
callbacks to a later one. Create the callback outside the frame to avoid allocating.
afterSubmit(
constmap:()=>void
map);
},
};
}
Callbacks run right after the frame’s last submit, in registration order, and only for frames that finish.
Every callback runs even if one throws. The frame still counts. run throws the callback error afterwards, or an AggregateError if several failed.
afterSubmit throws unless its own frame is recording. Each run makes a new Frame, so a module that acts once per frame can tell frames apart by that object.
Create the callback once, as above, so recording allocates nothing.
frame.submit() submits the commands recorded so far. The next access to frame.encoder creates another encoder, and the loop submits the remaining commands when your recorder returns. Before you submit early, try per-step values. Each early submit adds a queue.submit and splits the frame in a GPU capture.
function
functionframe(frame:Frame):void
frame(
frame: Frame
frame:
(alias)interfaceFrame
importFrame
Represents one frame being recorded. Each run creates a separate object, so modules can
compare frames by identity.
Records commands for submission after the recorder returns. submit() replaces this encoder.
Read it at each call instead of keeping it, and let the frame submit it.
Submits the recorded commands and replaces encoder for further recording. Each call adds one
queue.submit. End every pass first, because an open pass invalidates the command buffer.
Only the program's recorder should call this. Throws unless this frame is recording. Does
nothing after the loop is destroyed.
submit();// queued ahead of the next write
}
// the draws go into a fresh frame.encoder, which the loop submits
}
End every pass first. A submit with a pass open is a WebGPU validation error, and it voids everything recorded since the last submit. A recording root throws there instead.
Only the recorder you pass to frames.create calls submit(). A module never submits. It reads frame.encoder at each call and never keeps it, since a kept encoder may already have been submitted.
A that takes a frame or an encoder reads the encoder with frames.encoderOf(target) at each call.
A frame ending with submit() sends no empty command buffer. It creates another encoder only if frame.encoder is read again.
Kit modules mark their recorded copies as sent when their encoder submits. Copies sent by an early frame.submit() still count if the frame throws later. A Frame you build yourself marks them through afterSubmit, after its last submit.
Submit once per frame unless an early submit is required. Use one when:
Per-step values A step reads a value you can’t supply another way, such as a library that reads its own createUniform.
Library submits A library call submits its own work during a frame. Examples include a pipeline drawn without an encoder and a ’s add without an encoder. That work runs before your frame’s pending commands. Submit first to preserve order, or pass the encoder where supported.
Risky commands A command may be invalid. WebGPU invalidates its whole command buffer, while commands submitted before it still run.
An early start The GPU could start sooner. Submitting the simulation before recording draws lets the GPU work while the CPU records the rest.
A benchmark or GPU capture needs the same steps on every run, regardless of display rate. Leave out frames.animate and web.host, make the loop yourself, and call run once per step:
The performance.now() method returns a high resolution timestamp in milliseconds. It represents the time elapsed since Performance.timeOrigin (the time when navigation has started in window contexts, or the time when the worker is run in Worker and ServiceWorker contexts).
Records and submits one frame with a fresh Frame and encoder. Requires finite, nondecreasing
seconds. Throws for an invalid time, a nested frame or an asynchronous recorder. Returns
false if the loop is destroyed before or during recording. Runs all after-submit callbacks. If
any throw, the frame still counts and run rethrows their errors.
run(
let i:number
i*
constSTEP:number
STEP);// one step, one submit
}
await
constroot:TgpuRoot
root.
TgpuRoot.device: GPUDevice
The GPU device associated with this root.
device.
GPUDevice.queue: GPUQueue
The queue read-only property of the GPUDevice interface returns the primary GPUQueue for the device.
Returns a Promise that resolves once this queue finishes processing all the work submitted
up to this moment.
Resolution of this Promise implies the completion of
The performance.now() method returns a high resolution timestamp in milliseconds. It represents the time elapsed since Performance.timeOrigin (the time when navigation has started in window contexts, or the time when the worker is run in Worker and ServiceWorker contexts).
run takes a time in seconds. With i * STEP, the first frame has a dt of zero. Each later frame advances by one step.
Each run submits, so a uniform written for one step can’t change what the step before it reads. No count on the GPU is needed here.
onSubmittedWorkDone() resolves once the GPU has finished everything submitted. Wait for it before stopping a timer or reading results.
A fixed step of the same length runs exactly one step per frame. A of a fixed size draws the same pixels on every screen, and offscreenSurface from @typegpu/kit/testing gives the scene a surface without a canvas.
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 →
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 →
recorderThe recorder is the function you give the frame loop, which records one frame into the encoder it is handed.Words →
readbackA readback copies a buffer during a frame and hands it to the CPU a frame or two later, without making the frame wait.Results back →
recording methodA recording method records a module's work into the pass, the encoder or the frame you hand it, and never submits.Recording methods →
kernelA kernel is a compute function that the GPU runs many times at once, each run on its own thread.A frame in plain TypeGPU →
mesh poolA mesh pool stores every mesh's vertices in shared buffers, so one pipeline can draw any mesh.Mesh pool →
surfaceA surface is a canvas set up for your root, which keeps its drawing buffer the size the page shows it at.Surfaces →