Skip to content

ModuleCore / FrameConcept: How the kit works

Coroutines

Functions that pause at yield* and resume when your frame calls the scheduler.

import
import { coroutines } from '@typegpu/kit'
facts
stabilityunstablerecordsframelifecycledestroy()
On this page

A door may open half a second after its switch, a level may load while the game draws, or a pick may wait for the GPU. A loop needs flags and timers to track these tasks. A coroutine pauses at yield* and resumes when your frame calls the scheduler. An async function can resume between frames.

openDoor runs to its next wait, one frame at a time

Try Press the switch and step through openDoor. The marked line is paused, and white lines ran in the last frame.
const
const door: {
open: number;
}
door
= {
open: number
open
: 0 };
const
const scheduler: coroutines.Coroutines
scheduler
=
import coroutines
coroutines
.
function create(options?: coroutines.Options): coroutines.Coroutines

Creates a scheduler for coroutines that advance when you call run with a frame.

@returns ― a scheduler you start coroutines on and destroy

create
();
function*
function openDoor(): Generator<coroutines.Wait<unknown>, void, unknown>
openDoor
() {
yield*
import coroutines
coroutines
.
function seconds(duration: number): coroutines.Wait<frames.Frame>

Waits until the scheduler's time has advanced by duration seconds, frame time by default. Throws unless duration is finite and nonnegative.

@param ― duration seconds to wait

@returns ― a wait to yield*

seconds
(0.5);
while (
const door: {
open: number;
}
door
.
open: number
open
< 1) {
const
const frame: frames.Frame
frame
= yield*
import coroutines
coroutines
.
const nextFrame: coroutines.Wait<frames.Frame>

Waits until run in the next frame. Coroutines share this wait, and resuming through it allocates nothing.

nextFrame
;
const door: {
open: number;
}
door
.
open: number
open
=
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.min(...values: number[]): number

Returns the smaller of a set of supplied numeric expressions.

@param ― values Numeric expressions to be evaluated.

min
(
const door: {
open: number;
}
door
.
open: number
open
+
const frame: frames.Frame
frame
.
Frame.dt: number

Reports seconds since the previous submitted frame. The first frame has a dt of zero.

dt
* 2, 1);
}
}
function onSwitch(press: () => void): void
onSwitch
(() =>
const scheduler: coroutines.Coroutines
scheduler
.
Coroutines.start<void>(process: Generator<coroutines.Wait<unknown>, void, unknown>): coroutines.Coroutine<void>

Schedules a coroutine for the next run, or later in the current run if called from a coroutine. Yielding it starts it at once if it has not started. It runs on its own: cancelling a coroutine that waits on it only stops that wait. After destroy, returns a cancelled coroutine without running it.

start
(
function openDoor(): Generator<coroutines.Wait<unknown>, void, unknown>
openDoor
()));
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 scheduler: coroutines.Coroutines
scheduler
.
Coroutines.run(frame: frames.Frame): void

Resumes every coroutine whose wait is complete. Call while a frame is recording. Coroutine and wait errors go to the coroutine's yield* or to onError, rather than escaping run.

run
(
frame: frames.Frame
frame
); // resumes every coroutine whose wait is over
// ... your passes draw the door at door.open
});

Coroutines run only when you call scheduler.run(frame). Place the call where their work belongs in the frame.

Wait Resumes with When
coroutines.nextFrame the frame at the next frame’s run
coroutines.frames(n) the frame n frames later
coroutines.seconds(s) the frame once the scheduler’s time has advanced by s
coroutines.budget(ms) the frame in the same frame while it has run for less than ms since resuming, else next
a coroutine its return value when it finishes; its error is thrown here
coroutines.until(p) p’s value at the first run after promise p settles; a rejection is thrown here
  • A wait that could never resume, such as coroutines.seconds(NaN), throws when you make it.
  • Each frames(n), seconds(s) or budget(ms) call allocates a wait. Create it outside a hot loop. Resuming through a shared wait allocates nothing.
  • seconds counts frame.time unless you pass coroutines.create({ time }). Pass time: () => sim.time to count a fixed step’s time, so timed waits stop while the game is paused.
  • budget measures the wall clock, performance.now() unless you pass now.

A started coroutine is also a wait. yield* scheduler.child(pick(x, y)) starts pick as a child of the running coroutine and resumes with what pick returns, as await does. Here pick asks the picker for the candidate under a point and waits for its readback:

const
const scheduler: coroutines.Coroutines
scheduler
=
import coroutines
coroutines
.
function create(options?: coroutines.Options): coroutines.Coroutines

Creates a scheduler for coroutines that advance when you call run with a frame.

@returns ― a scheduler you start coroutines on and destroy

create
();
let
let point: [number, number] | undefined
point
: [number, number] | undefined;
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 scheduler: coroutines.Coroutines
scheduler
.
Coroutines.run(frame: frames.Frame): void

Resumes every coroutine whose wait is complete. Call while a frame is recording. Coroutine and wait errors go to the coroutine's yield* or to onError, rather than escaping run.

run
(
frame: frames.Frame
frame
); // before the passes, so a pick asked for here is recorded below
const
const compute: TgpuComputePass
compute
=
frame: frames.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
();
if (
let point: [number, number] | undefined
point
)
const hits: picker.Picker
hits
.
Picker.pick(pass: TgpuComputePass, x: number, y: number): void

Picks at x, y from 0 to 1 across the view, from the top-left corner, as view.rayAt and pointing take them. Uses the camera given to create. Records one pick per frame: a second call into the same pass replaces the point and records no second dispatch.

pick
(
const compute: TgpuComputePass
compute
, ...
let point: [number, number]
point
);
let point: [number, number] | undefined
point
=
var undefined
undefined
;
const compute: TgpuComputePass
compute
.
TgpuComputePass.end(): void

Completes the recording of this compute pass

end
();
const hits: picker.Picker
hits
.
Picker.result: Readback<number | null>

Reads the picked candidate index to the CPU, or null if nothing was found. Capture after the picker pass, then call take() or await next(). Without hz, each pick requests a copy for the next capture.

result
.
Readback<number | null>.capture(frame: frames.Frame): void

Records a copy when a next() is waiting, a request is pending or the capture rate is due. Maps the copy after submission. Call after the pass that writes the buffer. A second call in the same frame does nothing.

capture
(
frame: frames.Frame
frame
); // after the pass that picks
// ... your render passes
});
function*
function pick(x: number, y: number): Generator<coroutines.Wait<unknown>, number | null, unknown>
pick
(
x: number
x
: number,
y: number
y
: number) {
let point: [number, number] | undefined
point
= [
x: number
x
,
y: number
y
];
return yield*
import coroutines
coroutines
.
function until<number | null>(promise: PromiseLike<number | null>): coroutines.Wait<number | null>

Waits for a promise, such as a readback's next() or pipelines.compile. Resumes at the first run after it settles, with its value or its error.

@param ― promise the promise to wait for

@returns ― a wait to yield*

until
(
const hits: picker.Picker
hits
.
Picker.result: Readback<number | null>

Reads the picked candidate index to the CPU, or null if nothing was found. Capture after the picker pass, then call take() or await next(). Without hz, each pick requests a copy for the next capture.

result
.
Readback<number | null>.next(): Promise<number | null>

Returns a promise for the value from the next capture, and requests that capture. Promises asked for before one capture share its copy. Rejects if mapping fails or the reader is destroyed first. Waits indefinitely without a capture. In a coroutine, wait with yield* coroutines.until(probe.next()).

next
()); // a frame or two later
}
function*
function game(): Generator<coroutines.Wait<unknown>, void, unknown>
game
() {
const candidate = yield*
const scheduler: coroutines.Coroutines
scheduler
.
Coroutines.child<number | null>(process: Generator<coroutines.Wait<unknown>, number | null, unknown>): coroutines.Coroutine<number | null>

Starts a coroutine as start does, owned by the running coroutine: it ends with its owner, cancelled first when the owner is cancelled and cancelled when the owner returns or fails. Throws outside a running coroutine.

child
(
function pick(x: number, y: number): Generator<coroutines.Wait<unknown>, number | null, unknown>
pick
(10, 20));
const candidate: number | null
yield*
import coroutines
coroutines
.
function seconds(duration: number): coroutines.Wait<frames.Frame>

Waits until the scheduler's time has advanced by duration seconds, frame time by default. Throws unless duration is finite and nonnegative.

@param ― duration seconds to wait

@returns ― a wait to yield*

seconds
(0.5);
if (
const candidate: number | null
candidate
!== null)
function select(candidate: number): void
select
(
const candidate: number
candidate
);
}
const
const main: coroutines.Coroutine<void>
main
=
const scheduler: coroutines.Coroutines
scheduler
.
Coroutines.start<void>(process: Generator<coroutines.Wait<unknown>, void, unknown>): coroutines.Coroutine<void>

Schedules a coroutine for the next run, or later in the current run if called from a coroutine. Yielding it starts it at once if it has not started. It runs on its own: cancelling a coroutine that waits on it only stops that wait. After destroy, returns a cancelled coroutine without running it.

start
(
function game(): Generator<coroutines.Wait<unknown>, void, unknown>
game
());
const main: coroutines.Coroutine<void>
main
.
Coroutine<void>.cancel(): void

Cancels the coroutines it started with child, calls its wait's leave or stops waiting on the coroutine it joined, then runs its finally blocks and using disposals. Other coroutines waiting on it receive an error.

cancel
(); // runs finally blocks and disposes `using` resources
  • start and child return a coroutine with status, result, error and cancel(). Its status is running, done, failed or cancelled.
  • child works only inside a running coroutine. start starts one that runs on its own, from anywhere.
  • A coroutine started by a running one starts in the same frame, at once if its starter yields it, as game does.
  • One waiting on another resumes in the frame the other finishes.

Four coroutines run and wait across 36 frames

Try Cancel game while it waits on pick to see which finally blocks run and whether the read completes. Each block marks a running coroutine; lines show waits. Scroll the timeline sideways to see the later frames.

coroutines.until(promise) and coroutines.deferred(), which a callback resolves or rejects, cover most waits. coroutines.wait makes one from these :

Member Called
ready(frame, since) at every run until it returns true
value(frame) optionally, once it is ready; the coroutine resumes with what it returns, or else with the frame
begin(frame) optionally, in the frame a coroutine yields it, once per coroutine
leave() optionally, when a coroutine waiting on it is cancelled before it is ready

A coroutine cancelled from ready or value stops there: it gets no value call after leave, and no leave once ready returned true.

since holds the index of the frame where the coroutine yielded and the scheduler’s time then, which clock(frame) reads for a later frame. It also holds resumedAt, the start of its last resume, and now(), the wall clock. One wait can serve many coroutines, as nextFrame does, and resuming through it allocates nothing:

const
const flag: {
set: boolean;
}
flag
= {
set: boolean
set
: false };
const
const raised: NoInfer<{
ready: () => boolean;
} & coroutines.Wait<Frame>>
raised
=
import coroutines
coroutines
.
function wait<{
ready: () => boolean;
}>(parts: {
ready: () => boolean;
}): NoInfer<{
ready: () => boolean;
} & coroutines.Wait<Frame>>

Makes a new wait with the members of parts, which it leaves unchanged. The wait yields itself once, then returns the resume value, the frame without value. Many coroutines can share it, and resuming through it allocates nothing.

@param ― parts ready, and optionally value, begin and leave

@returns ― a wait to yield*

wait
({
ready: () => boolean
ready
: () =>
const flag: {
set: boolean;
}
flag
.
set: boolean
set
});

If a coroutine throws, the frame and other coroutines continue. run doesn’t throw.

  • A coroutine waiting on it gets the error at its yield*, as await does.
  • If no coroutine waits on it, onError reports the error. It defaults to reportError, which reports an uncaught error without throwing, or to console.error where reportError is missing.
  • A wait that throws, in begin, ready or value, throws at the coroutine’s yield*, and so does a bare yield of something that isn’t a wait.
  • A yield* of a coroutine that waits, directly or through others, on the one yielding it throws there too, since neither could finish. So does a coroutine yielding itself.
  • coroutine.cancel() closes the generator at once. It runs finally blocks and disposes using resources. Its wait gets a leave call, and coroutines waiting on it get an error.
  • scheduler.destroy() cancels everything. A coroutine started after that comes back cancelled.
  • A child from scheduler.child ends with its owner. Cancelling a coroutine first cancels its children, waited on or not, and one that returns or fails cancels those still running. In the figure, cancelling game runs pick’s finally, then game’s.
  • A coroutine from scheduler.start keeps running when one waiting on it is cancelled. Cancelling a coroutine that waits on a promise leaves the promise’s work running.
  • Order work inside a frame with call order.
  • Check a cadence beside a pass for work such as GI every fourth frame. A coroutine can’t reach a pass opened later.
  • Connect through their arguments. No module requires coroutines.
  • A stack trace from a resumed coroutine shows the scheduler’s run, not where the coroutine was started.
  • Copies in a coroutine read last frame’s results if run comes before the passes, or this frame’s results if it comes after them. Nothing warns if placing run earlier adds a frame of delay.
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 →
memberA member is a function or accessor on what a factory returns that reads that instance's own resources, so a pipeline that uses it needs no fill.Members and placeholders →
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 →

Reference

Callstakesreturns

coroutines.create(options?) → Coroutines

Coroutines
a scheduler you start coroutines on and destroy
takes

Options

3 fields
onError?

(error: unknown) => void

Receives errors from failed coroutines with no waiters, and from cleanup during cancellation.Default reportError where available, otherwise console.error.

now?

() => number

Supplies the wall clock in milliseconds for resume timestamps and budget.Default performance.now().

time?

(frame: Frame) => number

Supplies the clock in seconds that seconds waits count, such as a fixed step's time, so timed waits stop while a game is paused.Default frame.time.

returns

Coroutines

5 members
start(process)

Schedules a coroutine for the next run, or later in the current run if called from a coroutine.

child(process)

Starts a coroutine as start does, owned by the running coroutine: it ends with its owner, cancelled first when the owner is cancelled and cancelled when the owner returns or fails.

run(frame)

Resumes every coroutine whose wait is complete.

size

number

Counts the coroutines the scheduler holds.

destroy()

Cancels every coroutine.

coroutines.wait(parts)

parts
ready, and optionally value, begin and leave

coroutines.frames(count) → Wait

count
how many frames to wait
Wait
a wait to yield*

coroutines.seconds(duration) → Wait

duration
seconds to wait
Wait
a wait to yield*

and coroutines.budget, coroutines.deferred, coroutines.until

returns

Deferred

deferred · 3 members
status

'pending' | 'resolved' | 'rejected'

Starts as pending and records the first resolve or reject.

resolve(value)

Resumes waiting coroutines with value at the next run.

reject(error)

Throws error at waiting coroutines' yield* at the next run.

In a frame

records

Call run once per frame while recording to resume coroutines whose waits are complete.

Holds

lifecycle

destroy()[Symbol.dispose]

Exports

helpers
nextFrame
types
Suspension Wait Coroutine

used byBlockfallDomino runPlatformerStampede