Skip to content

ModuleCore / FrameConcept: How the kit works

Frames

A frame loop that hands you one command encoder each frame and submits it once, with the time and callbacks that run after the submit.

import
import { frames } from '@typegpu/kit'
call
frames.create(root, record) → Framesalso encoderOf · dtOf · animate
facts
submitsevery frame, on its ownlifecycledestroy()
On this page

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.
import {
import frames
frames
} from '@typegpu/kit';
const
const loop: frames.Frames
loop
=
import frames
frames
.
function create(root: TgpuRoot, record: Recorder): frames.Frames

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
(
const root: 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.

dt
,
index: number

Counts frames submitted before this one.

index
}) => {
// record passes into `encoder`; don't submit it
});
import frames
frames
.
function animate(loop: Pick<frames.Frames, "run"> & Partial<Pick<frames.Frames, "seconds">>, options?: frames.AnimateOptions): frames.Animation
export animate

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
(
const loop: frames.Frames
loop
); // calls loop.run(seconds) at every animation frame
  • The clock time 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.
  • Animating frames.animate(loop) calls run at every animation frame. A test or a benchmark calls run itself.
  • 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.
  • Stopping destroy() 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 time frame.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:

import frames
frames
.
function animate(loop: Pick<frames.Frames, "run"> & Partial<Pick<frames.Frames, "seconds">>, options?: frames.AnimateOptions): frames.Animation
export animate

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
(
const loop: 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.

MDN Reference

IntersectionObserver
(([
entry: IntersectionObserverEntry
entry
]) =>
entry: IntersectionObserverEntry
entry
?.
IntersectionObserverEntry.isIntersecting: boolean

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.

MDN Reference

isIntersecting
?
const surface: web.Surface
surface
.
Surface.resume(): void

Resumes the frames after pause.

resume
() :
const surface: web.Surface
surface
.
Surface.pause(): void

Pauses the surface for a canvas outside the viewport or a pause menu. Hidden tabs need no call, because browsers stop their animation frames.

pause
(),
).
IntersectionObserver.observe(target: Element): void

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.

MDN Reference

observe
(
const canvas: HTMLCanvasElement
canvas
);

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
function createProbe(root: TgpuRoot): {
record(frame: Frame): void;
}
createProbe
(
root: TgpuRoot
root
:
(alias) interface TgpuRoot
import TgpuRoot
TgpuRoot
/* , ... */) {
const
const map: () => void
map
= () => {
// staging.mapAsync(GPUMapMode.READ).then(...)
};
return {
function record(frame: Frame): void
record
(
frame: Frame
frame
:
(alias) interface Frame
import Frame

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
(
const map: () => 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
function frame(frame: Frame): void
frame
(
frame: Frame
frame
:
(alias) interface Frame
import Frame

Represents one frame being recorded. Each run creates a separate object, so modules can compare frames by identity.

Frame
) {
for (const
const phase: number
phase
of
const phases: number[]
phases
) {
const params: TgpuUniform<d.WgslStruct<{
phase: d.F32;
}>>
params
.
TgpuBufferBindingBase<WgslStruct<{ phase: F32; }>>.write(data: d.InferInput<d.WgslStruct<{
phase: d.F32;
}>>, options?: BufferWriteOptions): void
write
({
phase: number
phase
});
const
const pass: TgpuComputePass
pass
=
frame: Frame
frame
.
Frame.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
.
TgpuCommandEncoder.beginComputePass(descriptor?: TgpuComputePassDescriptor): TgpuComputePass

Begins recording a compute pass

beginComputePass
();
const pass: TgpuComputePass
pass
.
TgpuComputePass.setPipeline(pipeline: TgpuComputePipeline): void

Sets the current

TgpuComputePipeline

for subsequent dispatches

setPipeline
(
const step: TgpuComputePipeline
step
);
const pass: TgpuComputePass
pass
.
TgpuComputePass.dispatchWorkgroups(x: number, y?: number, z?: number): void
dispatchWorkgroups
(64);
const pass: TgpuComputePass
pass
.
TgpuComputePass.end(): void

Completes the recording of this compute pass

end
();
frame: Frame
frame
.
Frame.submit(): void

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:

const
const STEP: number
STEP
= 1 / 60;
const
const STEPS: 600
STEPS
= 600;
const
const loop: frames.Frames
loop
=
import frames
frames
.
function create(root: TgpuRoot, record: Recorder): frames.Frames

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
(
const root: TgpuRoot
root
, (
frame: frames.Frame
frame
) =>
const scene: {
frame(frame: Frame): void;
}
scene
.
function frame(frame: Frame): void
frame
(
frame: frames.Frame
frame
));
const
const start: number
start
=
var performance: Performance
performance
.
Performance.now(): DOMHighResTimeStamp

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

MDN Reference

now
();
for (let
let i: number
i
= 0;
let i: number
i
<
const STEPS: 600
STEPS
;
let i: number
i
++) {
const loop: frames.Frames
loop
.
Frames.run(seconds: number): boolean

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
*
const STEP: number
STEP
); // one step, one submit
}
await
const root: 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.

MDN Reference

The primary

GPUQueue

for this device.

queue
.
GPUQueue.onSubmittedWorkDone(): Promise<undefined> (+1 overload)

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

GPUBuffer#mapAsync

calls made prior to that call, on

GPUBuffer

s last used exclusively on that queue.

onSubmittedWorkDone
();
const
const msPerStep: number
msPerStep
= (
var performance: Performance
performance
.
Performance.now(): DOMHighResTimeStamp

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

MDN Reference

now
() -
const start: number
start
) /
const STEPS: 600
STEPS
;
  • 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 →

Reference

kit.frames

moduleHow to read this card

Callsborrows, never destroystakesreturns

frames.create(root, record) → Frames

root
the root
record
your frame, recorded into each frame's encoder
Frames
a loop you run with each frame's time and destroy
returns

Frames

3 members
run(seconds)

Records and submits one frame with a fresh Frame and encoder.

seconds

number | undefined

Reports the seconds of the last recorded frame, or undefined before the first.

destroy()

Stops future submissions and keeps borrowed resources alive.

frames.encoderOf(target)

target
a frame or an encoder

frames.dtOf(target, dt?)

target
a frame or an encoder
dt?
the method's dt option

frames.animate(loop, options?) → Animation

loop
the loop to run on each unpaused frame. Continues from its seconds if it ran before
Animation
an animation you destroy to stop the frames
takes

AnimateOptions

4 fields
surface?

{ paused: boolean }

Stops frames and elapsed time while paused is true.Default none.

maxDt?

number

Limits the time added per frame, in seconds, after a stall or when a tab becomes visible.Default 0.1.

schedule?

Schedule

Supplies animation frames.Default requestAnimationFrame.

onError?

(error: unknown) => void

Receives a frame error after the animation stops.Default none.

returns

Animation

1 member
destroy()

Stops requesting frames, even when called from a frame.

In a frame

submits

every frame, on its own

Once a frame after your recorder returns, and again at each frame.submit().

Holds

lifecycle

destroy()[Symbol.dispose]

Exports

types
Schedule Frame

used byevery example page