Motion vectors and reprojection need last ’s camera and object positions. Maintaining separate shader functions for current and previous state risks differences between them.
latticeThin beams and rising embers, smoothed over time with TAA.
The lattice stores its turn angle in history.create(root, d.f32, { usage: 'uniform' }).
TAA’s runs the vertex code again with previously to read last frame’s angle.
Switch to a plain uniform to make both runs read the current angle. The motion view then shows
only camera motion. TAA samples last frame’s colour at the wrong positions, and the beams dim as
they turn. Raise the speed to see the effect.
history.previously(fn) compiles fn a second time. In that copy, filled with history.temporal(current, previous) read previous. history.create stores last frame’s buffer state for . The kit’s cameras store their own previous views, and a table or a set of instances created with history: true keeps its own . Passes use previously to compute last frame’s vertex for outputs such as motion.
Creates buffers for the current and previous state. write queues values until record
records copies into the frame encoder. This preserves the previous state, because CPU writes
reach the queue before submitted commands. A uniform history names its usage in the options,
as { usage: 'uniform' }.
@param ― schema the state, such as d.arrayOf(Transform, 256)
Fills an accessor with current, or previous in code compiled with previously. Storage
binds read-only in render pipelines and read-write in compute pipelines. This fill also works
with mutable accessors that kernels write through.
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
Copies the current state to previous, then applies pending writes. Call once per frame
before passes and kernels that read or update the state. A write without a following record
never becomes current, and a frame that does not submit leaves it for the next. A write after
record leaves the recorded frame as it was and waits for a record in a later submit.
record(
frame:frames.Frame
frame);
const
constpass:TgpuComputePass
pass=
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.
history.epoch is a , 'current' or 'previous', and previously resolves its function with 'previous'. Call what it returns from any 'use gpu' function, in any stage.
history.temporal(current, previous) fills an accessor with any values it accepts. You can use two uniforms, two buffers, or a current constant and a previous uniform.
Code that never calls a previously function binds current only.
history.create(root, schema, options) owns three buffers, for current state, previous state and the next write.
The default storage usage binds reading read-only in render pipelines and read-write in compute pipelines, so it can fill a mutable accessor for that update the state. usage: 'uniform' suits small state written by the CPU.
Write through write, not straight into current. A queue write lands before everything the frame submits, so it would be copied aside as last frame’s too.
Call record once a frame, before what reads or moves the state. A write shows from the first record after it in a frame that submits, so a frame that throws leaves the write for the next.
The same moving dot, written through a history and straight into current
write
Try Choose write on click, then write once. The history shows the move for one frame, until the next record copies the new position aside. Written straight into current, the position reaches previous in the same frame, so the move never shows.
Store state that moves , such as the lattice’s turn angle, in a history. Read it through a vertex-code accessor filled with reading by the drawing. Outputs that compute last frame’s vertex then include the object’s motion:
import{
import history
history}from'@typegpu/kit';
// How far the triangle has turned, now and last frame
Creates buffers for the current and previous state. write queues values until record
records copies into the frame encoder. This preserves the previous state, because CPU writes
reach the queue before submitted commands. A uniform history names its usage in the options,
as { usage: 'uniform' }.
@param ― schema the state, such as d.arrayOf(Transform, 256)
@returns ― a history you destroy
create(
constroot:TgpuRoot
root,
import d
d.
constf32:d.F32
export f32
A schema that represents a 32-bit float value. (equivalent to f32 in WGSL)
Can also be called to cast a value to an f32.
@exampleconst value = f32(); // 0
@exampleconst value = f32(1.23); // 1.23
@exampleconst value = f32(true); // 1
f32,{
usage:"uniform"
Selects the buffer usage. Use uniform for small CPU-written state, such as a clock, or
storage for arrays that kernels can also update. Default storage.
A schema that represents a 32-bit float value. (equivalent to f32 in WGSL)
Can also be called to cast a value to an f32.
@exampleconst value = f32(); // 0
@exampleconst value = f32(1.23); // 1.23
@exampleconst value = f32(true); // 1
f32(
vertexIndex: number
vertexIndex)*2.1;
const
constworld:d.v3f
world=
import d
d.
constvec3f:d.Vec3f
(x:number,y:number,z:number)=>d.v3f(+5 overloads)
vec3f(
import std
std.
functioncos(value:number):number(+1overload)
export cos
cos(
constangle:number
angle),
import std
std.
functionsin(value:number):number(+1overload)
export sin
sin(
constangle:number
angle),0);
return
constCorner:d.WgslStruct
(props?:{
position:d.v4f;
}|undefined)=>{
position:d.v4f;
}
Corner({
position:d.v4f
position:
import cameras
cameras.
consttoClip:(point:d.v3f)=>d.v4f
Returns a world point in clip space, as a vertex shader's position. It reads cameras.current;
code that projects with a view of its own multiplies by that view's viewProjection.
Builds a pipeline for drawing that writes every described output and returns its draw
methods. If the drawing cannot supply the fields an output reads, type checking fails with
that target's name and those fields. Use without to leave it out or instead to replace
it.
Computes data at a pixel from the interpolated vertex. position is the pixel position.
Required for descriptions that write colour or outputs, and for depthOf and sampleMask.
Fills an accessor with current, or previous in code compiled with previously. Storage
binds read-only in render pipelines and read-write in compute pipelines. This fill also works
with mutable accessors that kernels write through.
Records a draw into the pass, on which the description has set the pipeline it built. That
pipeline binds indices if supplied. drawIndexed throws without an index buffer. A group
the drawing sets with pass.setBindGroup stays for later drawings in the pass that read its
layout, so give such a layout to one drawing.
Queues a copy of value for the next record to apply.
write(
constframe:Frame
frame.
Frame.time: number
Reports seconds since the first submitted frame.
time);
constturned:history.History<d.F32,"uniform">
turned.
History<F32,"uniform">.record(frame: Frame): void
Copies the current state to previous, then applies pending writes. Call once per frame
before passes and kernels that read or update the state. A write without a following record
never becomes current, and a frame that does not submit leaves it for the next. A write after
record leaves the recorded frame as it was and waits for a record in a later submit.
record(
constframe:Frame
frame);
Filled with a plain uniform, last frame’s code reads this frame’s turn, and the motion output sees only the camera move.
record copies the whole state every frame, and again in a frame with a write.
A write that jumps the state, such as a teleport, reads as motion for a frame.
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 →
outputAn output is a value every drawing writes at each pixel beside its colour, such as motion or an id.Outputs every drawing writes →
accessorAn accessor is a typed value that shader code reads and that each pipeline can fill with a different resource.Modules connect through TypeGPU →
rowA row is one object's values in a buffer, such as one enemy, one particle or one light.Objects are rows in buffers →
slotA slot is a row's place in its buffers, which a new row can take once the first is removed.Row ids →
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 →
drawingA drawing is what a renderer hands to a pass description: what each vertex carries to its pixels, what a pixel looks like and the draw call.Every renderer is a drawing →