counters keeps a counter for each kind of event, such as a hit, a kill or a pickup. It carries counts and no details: add to a counter, and the CPU reads how much it grew a or two later. That is enough to update a score, play a sound or restart a level, none of which a kernel can do. The details of each event take a queue of your own.
Each copy of the counter reaches the CPU when its map lands
hz
Try Drag map takes past six frames. The bars stack into all three staging buffers, captures are skipped, and the next copy still brings every stomp to take() once. clear drops the copies still on their way.
Keeps a counter for each named kind of event, and no details of one. Kernels add hits, kills,
pickups or other events, and the CPU receives the increases a frame or two later. Requires at
least one name, with no duplicates. Throws if names are invalid, hz is not finite and above 0, or
staging is not an integer above 0.
@injects ― member add, which adds to its own counters in any pipeline
@binds ― 1 storage: add, the counters
@owns ― a buffer, a readback: the counters; the readback copies them at hz
@records ― capture after the passes that add; clear before them, whenever the counting starts
again; in frame mode, capture clears after copying.
@param ― names one counter each
@returns ― a set of counters you destroy
create(
constroot:TgpuRoot
root,['stomps','hurts'],{
Options.hz?: number |undefined
Reads counters for take and latest this many times per second on average. Default 30.
Returns the increase in each counter since the last take, or the captured counts in frame
mode. Returns each read once, then undefined until the next read arrives. Reuses one object
and updates it in place.
take();// what each gained since the last take, or undefined
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.
Copies counters when a read is due, then clears them in frame mode. Record after the passes
that add.
capture(
frame:frames.Frame
frame);// after the passes that add
});
Addingadd.stomps(amount) adds atomically, from any of any kernel. The amount can vary, such as a kill’s reward of 1, 4 or 1000 gold.
Capturingcapture copies counters at hz, 30 times per second by default. It uses a readback with staging buffers, 3 by default, and skips while all are mapping. The platformer reads at 60 Hz, so the hero bounces a frame or two after a stomp.
Readingtake() returns each counter’s increase since the last take, once per completed read, and undefined between reads. latest holds totals since the last clear. Each reuses an object and updates it in place. Copy a result to keep it.
Late reads Counters only increase, so late or repeated reads lose no events. The next difference includes them. Differences use u32 arithmetic and remain correct across a wrap at 2^32.
Use { per: 'frame' } to count objects in a frame, such as the stampede’s skinned horses and cards. capture copies when a read is due and clears counters every frame. latest and take() then return one frame’s counts. Call capture every frame, after the passes that count.
clear(frame) sets every counter to 0 in the frame and drops the reads made before it, so the next gains count from the clear. Record it before the passes that add, as at a restart:
Resets all counters to 0, recorded into target, a frame or an encoder. Record before the
passes that add.
clear(
frame:frames.Frame
frame);
let restarting:boolean
restarting=false;
// ... the passes that add, then tally.capture(frame)
});
Read latest to display totals such as enemies spawned, slain or through the gate. The platformer keeps its counters across restarts and ignores damage for 1.5 s, longer than a read takes to complete.
Counters omit event details such as location. Handle immediate effects on the GPU, such as squashing a critter or emitting dust. Let the CPU react to the count.
Per-item thresholds need separate counters. The fish tank uses its own counter array to count bites per pellet and decide on the GPU when each is eaten.
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 →
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 →
threadA thread is one run of a kernel, and the GPU starts many at once, each with its own number.Slime mould →
captureA capture copies a buffer at one point in the frame, so the CPU can read it after the submit.Placing the capture →