A game may keep enemies, pickups, lights or fish in a GPU buffer. If the CPU and write the same fields, a stale CPU copy can overwrite a kernel’s update, such as the start of a death animation.
A table separates CPU-owned host fields from GPU-owned device fields in different buffers. Uploading host rows preserves the device fields that kernels wrote.
The same flush in one struct and in a table
Try Hit rows 1 and 6 while a kernel moves every row. The flush uploads the rows between them.
import{
import frames
frames,
import table
table}from'@typegpu/kit';
const
constEnemy:table.TableDef<"Enemy",{
species:d.U32;
tint:d.Vec4f;
radius:d.F32;
},{
position:d.Vec3f;
velocity:d.Vec3f;
health:d.F32;
}>
Enemy=
import table
table.
functiondefine<"Enemy",{
species:d.U32;
tint:d.Vec4f;
radius:d.F32;
},{
position:d.Vec3f;
velocity:d.Vec3f;
health:d.F32;
}>(name:"Enemy",parts:{
host?:{
species:d.U32;
tint:d.Vec4f;
radius:d.F32;
}|undefined;
device?:{
position:d.Vec3f;
velocity:d.Vec3f;
health:d.F32;
}|undefined;
}):table.TableDef<"Enemy",{
species:d.U32;
tint:d.Vec4f;
radius:d.F32;
},{
position:d.Vec3f;
velocity:d.Vec3f;
health:d.F32;
}>
Declares CPU-written host fields and GPU-written device fields using the records accepted by
d.struct. Throws if no fields are declared, a field
appears in both parts, the CPU writer cannot write a field type such as f16, or a field uses a
reserved member name such as alive.
name labels structs, buffers, WGSL functions and errors. It is not a lookup key. The
definition itself brands row ids, as RowId<typeof Enemy>.
@param ― name a label for ids and WGSL
@param ― parts the host fields the CPU writes and the device fields the GPU writes
@returns ― a definition whose getters shaders read and that table.create allocates
define('Enemy',{
host?:{
species:d.U32;
tint: d.Vec4f;
radius: d.F32;
}|undefined
host:{
species:d.U32
species:
import d
d.
constu32:d.U32
export u32
A schema that represents an unsigned 32-bit integer value. (equivalent to u32 in WGSL)
Can also be called to cast a value to an u32 in accordance with WGSL casting rules.
@exampleconst value = u32(); // 0
@exampleconst value = u32(7); // 7
@exampleconst value = u32(3.14); // 3
@exampleconst value = u32(-1); // 4294967295
@exampleconst value = u32(-3.1); // 0
u32,
tint:d.Vec4f
tint:
import d
d.
constvec4f:d.Vec4f
vec4f,
radius:d.F32
radius:
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},// written by the CPU
device?:{
position:d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}|undefined
device:{
position:d.Vec3f
position:
import d
d.
constvec3f:d.Vec3f
vec3f,
velocity:d.Vec3f
velocity:
import d
d.
constvec3f:d.Vec3f
vec3f,
health:d.F32
health:
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},// by kernels
});
const
constenemies:table.Table<table.TableDef<"Enemy",{
species:d.U32;
tint:d.Vec4f;
radius:d.F32;
},{
position:d.Vec3f;
velocity:d.Vec3f;
health:d.F32;
}>>
enemies=
import table
table.
functioncreate<table.TableDef<"Enemy",{
species:d.U32;
tint:d.Vec4f;
radius:d.F32;
},{
position:d.Vec3f;
velocity:d.Vec3f;
health:d.F32;
}>>(root:TgpuRoot,def:table.TableDef<"Enemy",{
species:d.U32;
tint:d.Vec4f;
radius:d.F32;
},{
position:d.Vec3f;
velocity:d.Vec3f;
health:d.F32;
}>,options:table.Options<table.TableDef<"Enemy",{
species:d.U32;
tint:d.Vec4f;
radius:d.F32;
},{
position:d.Vec3f;
velocity:d.Vec3f;
health:d.F32;
}>>):table.Table<table.TableDef<"Enemy",{
species:d.U32;
tint:d.Vec4f;
radius:d.F32;
},{
position:d.Vec3f;
velocity:d.Vec3f;
health:d.F32;
}>>(+1overload)
Allocates GPU buffers and a CPU copy of host rows. Throws unless capacity is an integer in [1,
2^20].
@param ― def the definition its shaders were written against, from table.define
@returns ― a table you destroy
create(
constroot:TgpuRoot
root,
constEnemy:table.TableDef<"Enemy",{
species:d.U32;
tint:d.Vec4f;
radius:d.F32;
},{
position:d.Vec3f;
velocity:d.Vec3f;
health:d.F32;
}>
Enemy,{
Options<T extends AnyTableDef>.capacity: number
Sets the table capacity for its lifetime. Must be an integer in [1, 2^20], with slots from 0
to capacity - 1. A row id uses enough bits for the slot and the rest for its generation. The
generation has 29 bits at 8 rows and 12 bits at 2^20 rows.
Adds a row in the lowest free slot. Uses defaults for omitted fields. Throws when the table is
full, or when a value fails to write, which leaves the slot free.
Uploads changes since the last flush with queue.writeBuffer. Writes reach the queue before
the next submit. Call once per frame before passes that read the table. A HistoryTable takes
the frame or its encoder and throws without one.
Definitionstable.define takes no root. Its name labels WGSL names such as EnemyHost and Enemy_position, as well as buffers and errors. Nothing looks up a by name.
Parts Either part may be omitted. A host-only table holds data, such as Blockfall’s blocks. A field in both parts is a type error, and define throws for it. define also throws for f16 and bool fields.
Capacity The capacity is fixed, at most 2^20, and add throws when the table is full. Fields start at defaults, or zero, and add() with no values allocates nothing. count is the number of live . clear() removes them all.
Types The types take the : table.Table<typeof Enemy>, table.Options, table.Fields.
Buffersenemies.buffers holds the host, device and live buffers and the index uniform, which stores the span and the id mask. Use them for a readback or a of your own.
Row schemastable.rows(Enemy, 'device', capacity) gives a part’s array schema, for a buffer or history of your own. It throws for a capacity a table can’t have, or a part with no fields.
add returns a row id, a u32 that holds the slot and its generation. Its type is RowId<typeof Enemy>. A plain number or another definition’s id is a type error.
Stale idsremove(id) bumps the slot’s generation. A field handle given the old id then throws, so check with has(id) first. idAt(slot) turns a slot read back from the GPU into an id.
Other tables The types accept an id from another table of Enemy. The starting generation varies by table to help catch ids from another table of the same definition. A table decodes an id using its own capacity. If the decoded slot is out of range, empty or has a different generation, has returns false and remove, write and field handles throw.
Zero ids Generations start above 0, so a zeroed u32 is never a live id.
The capacity determines how many bits the slot needs. The generation uses the remaining bits. Once a slot’s generation wraps, an old id is valid again. A bullet spawned and freed every takes the same slot every frame, so at 120 fps it wraps after:
A vector field’s write and get also take any array of numbers, such as a Float32Array from a math library. write reads its first components.
Matrices, arrays and nested structs have only write. A matrix may be a Float32Array in its padded layout.
Device fields have no get. A device write sets an absolute value before the frame’s GPU work, for a teleport or a revive. Apply relative changes, such as damage, with a command.
Try Click cells, add or remove rows, then flush. Outlines mark changed fields, and the highlight marks uploaded cells.
Nothing reaches the GPU until flush(). Call it once per frame, before submit. Without history, flush() needs no argument and can go anywhere in the frame, since queue.writeBuffer lands before every later submit. A uploads:
written host rows in runs, bridging gaps of fewer than 16 unwritten rows from the CPU copy;
each run of written device fields in a row, with the padding between them, so a kernel’s field between two written ones keeps its value;
new rows’ device values, whole;
runs that meet across rows, such as adjacent new rows, as one upload;
For its , runs its vertex code a second time, compiled with history.previously. To give moving rows a motion of their own, create the table with history: true. The second run then reads last frame’s rows through the table’s getters:
Allocates GPU buffers and a CPU copy of host rows. Throws unless capacity is an integer in [1,
2^20].
@param ― def the definition its shaders were written against, from table.define
@returns ― a table you destroy
create(
constroot:TgpuRoot
root,
constEnemy:table.TableDef<"Enemy",{
species:d.U32;
tint:d.Vec4f;
radius:d.F32;
},{
position:d.Vec3f;
velocity:d.Vec3f;
health:d.F32;
}>
Enemy,{
Options<T extends AnyTableDef>.capacity: number
Sets the table capacity for its lifetime. Must be an integer in [1, 2^20], with slots from 0
to capacity - 1. A row id uses enough bits for the slot and the rest for its generation. The
generation has 29 bits at 8 rows and 12 bits at 2^20 rows.
capacity:4096,
history:true
Keeps last frame's rows, which code compiled with history.previously reads, such as a
drawing's motion. create then returns a HistoryTable, whose flush takes the frame or its
encoder: it copies the rows before the frame's changes, so a row added this frame starts where
it is. Default false.
Uploads changes since the last flush and records into target, a frame or an encoder, the
copies that keep last frame's rows. Call once per frame before passes that read the table. A
frame that throws leaves its copies to the next flush; an encoder can't, so a dropped one
loses them.
flush(
frame:frames.Frame
frame);// copies last frame's rows aside, then lands the uploads
// ... kernels that move them, passes that draw them
});
Flushing The table is then a HistoryTable, whose flush takes the frame or an encoder. Record the flush before the passes that read the table. Called with no argument, even through the plain Table type, it throws.
Unfinished frames A frame that throws leaves its uploads and new rows to the next flush. Given a bare encoder, flush treats its recorded copies as sent. Dropping that encoder loses its copies of the row uploads and history.
Second flush A second flush in one frame lands what changed since the first and leaves last frame’s rows as they were. If both flushes upload the same fields before the submit, commands recorded between them read the second upload, as without history. Call frame.submit() between the flushes to let those commands read the first.
New rows A row added this frame starts last frame where it is, so it doesn’t streak in from the origin.
Current rows Kernels and render pipelines read and write the current rows, as without history. Only code compiled with previously reads last frame’s.
One buffer Last frame’s rows follow the current ones in the same buffer, from slot capacity, so reading them binds no more buffers. Read rows through the getters, such as enemy.position(i), since enemy.device.$[i] always reads the current rows.
Copies Each flush copies the device rows across the span. It copies host rows only in the frame after an upload changed them.
Teleports A write that jumps a row, such as a teleport, reads as motion for a frame.
Slots never move. An id stays valid until its row is removed, and other buffers can refer to rows by slot. The span is the highest live slot plus one. add takes the lowest free slot to keep the span short. Kernels skip dead rows but still read memory across the span. A span with half its rows dead at random costs about twice as much as dense rows.
A table's slots beside a last-freed-first list
Try Play matching additions and removals in the table and a last-freed-first list. Kernels visit the full span, so a larger live fraction means less wasted work.
A dead instance costs as much vertex work as a live one, so don’t draw over the span. Live lists list each group’s live slots on the GPU every frame. They draw them indirectly at the cost of dense rows.
group rows by a field, such as species: d.u32. Groups share one capacity in any mix; rows with different fields belong in different tables.
To light and draw meshes, use a set of instances. A set is a table whose add takes a mesh, and its draws the live rows of each mesh.
A table’s gpu read its own rows in any pipeline: a getter per field, row(i), alive(i), span(), slotOf(id), and the host and device arrays. A pipeline that reads them needs :
Provides the definition's shader members bound to this table, such as reds.gpu.position(i)
and reds.gpu.device.$[i]. A shader reads several tables of one definition through their
gpu members, with nothing piped.
gpu;
// A helper that takes a whole row: the dead sink into the floor
The definition has the same members, as for code written over any table of Enemy, such as an instance kind’s . The pipeline fills them with one table’s inject(), as in root.pipe(enemies.inject()).
Without it, the pipeline fails at first use or in initAsync with an error that names the and the inject() it needs, such as The pipeline is missing a fill for `Enemy_host`. ….
Prefer getters to row(i) in hot code. row(i) copies every field, and some compilers keep loads the shader doesn’t use.
host, device, row, alive, span and slotOf are taken; a field can’t use them.
Each table’s members read its own buffers, so one kernel reads and writes two tables of one definition, such as enemies that defect to the other side:
const
constreds:table.Table<table.TableDef<"Enemy",{
species:d.U32;
},{
position:d.Vec3f;
health:d.F32;
}>>
reds=
import table
table.
functioncreate<table.TableDef<"Enemy",{
species:d.U32;
},{
position:d.Vec3f;
health:d.F32;
}>>(root:TgpuRoot,def:table.TableDef<"Enemy",{
species:d.U32;
},{
position:d.Vec3f;
health:d.F32;
}>,options:table.Options<table.TableDef<"Enemy",{
species:d.U32;
},{
position:d.Vec3f;
health:d.F32;
}>>):table.Table<table.TableDef<"Enemy",{
species:d.U32;
},{
position:d.Vec3f;
health:d.F32;
}>>(+1overload)
Allocates GPU buffers and a CPU copy of host rows. Throws unless capacity is an integer in [1,
2^20].
@param ― def the definition its shaders were written against, from table.define
@returns ― a table you destroy
create(
constroot:TgpuRoot
root,
constEnemy:table.TableDef<"Enemy",{
species:d.U32;
},{
position:d.Vec3f;
health:d.F32;
}>
Enemy,{
Options<T extends AnyTableDef>.capacity: number
Sets the table capacity for its lifetime. Must be an integer in [1, 2^20], with slots from 0
to capacity - 1. A row id uses enough bits for the slot and the rest for its generation. The
generation has 29 bits at 8 rows and 12 bits at 2^20 rows.
capacity:1024});
const
constblues:table.Table<table.TableDef<"Enemy",{
species:d.U32;
},{
position:d.Vec3f;
health:d.F32;
}>>
blues=
import table
table.
functioncreate<table.TableDef<"Enemy",{
species:d.U32;
},{
position:d.Vec3f;
health:d.F32;
}>>(root:TgpuRoot,def:table.TableDef<"Enemy",{
species:d.U32;
},{
position:d.Vec3f;
health:d.F32;
}>,options:table.Options<table.TableDef<"Enemy",{
species:d.U32;
},{
position:d.Vec3f;
health:d.F32;
}>>):table.Table<table.TableDef<"Enemy",{
species:d.U32;
},{
position:d.Vec3f;
health:d.F32;
}>>(+1overload)
Allocates GPU buffers and a CPU copy of host rows. Throws unless capacity is an integer in [1,
2^20].
@param ― def the definition its shaders were written against, from table.define
@returns ― a table you destroy
create(
constroot:TgpuRoot
root,
constEnemy:table.TableDef<"Enemy",{
species:d.U32;
},{
position:d.Vec3f;
health:d.F32;
}>
Enemy,{
Options<T extends AnyTableDef>.capacity: number
Sets the table capacity for its lifetime. Must be an integer in [1, 2^20], with slots from 0
to capacity - 1. A row id uses enough bits for the slot and the rest for its generation. The
generation has 29 bits at 8 rows and 12 bits at 2^20 rows.
Provides the definition's shader members bound to this table, such as reds.gpu.position(i)
and reds.gpu.device.$[i]. A shader reads several tables of one definition through their
gpu members, with nothing piped.
Provides the definition's shader members bound to this table, such as reds.gpu.position(i)
and reds.gpu.device.$[i]. A shader reads several tables of one definition through their
gpu members, with nothing piped.
gpu;
const
constclaimed:TgpuMutable<d.Atomic<d.U32>>
claimed=
constroot:TgpuRoot
root.
TgpuRoot.createMutable<d.Atomic<d.U32>>(typeSchema: d.Atomic<d.U32>, initial?: number |((buffer:TgpuBuffer<NoInfer<d.Atomic<d.U32>>>)=>void)|undefined): TgpuMutable<d.Atomic<d.U32>>(+1 overload)
Allocates memory on the GPU, allows passing data between host and shader.
Can be mutated in-place on the GPU. For a general-purpose buffer,
use
TgpuRoot.createBuffer
.
@param ― typeSchema The type of data that this buffer will hold.
@param ― initial Either initial value of the buffer, or an initializer to execute on the mapped buffer. (optional)
createMutable(
import d
d.
atomic<d.U32>(data: d.U32): d.Atomic<d.U32>
export atomic
Marks a concrete integer scalar type schema (u32 or i32) as a WGSL atomic.
Provides the definition's shader members bound to this table, such as reds.gpu.position(i)
and reds.gpu.device.$[i]. A shader reads several tables of one definition through their
gpu members, with nothing piped.
Device rows bind read-write in compute pipelines and read-only in render pipelines. WebGPU forbids read-write storage in vertex stages. Writing device rows in a render pipeline fails with readonly buffers cannot be mutated. Host rows are read-only everywhere.
Every row takes its left neighbour's value, one thread at a time
read
Try Shuffle the order. In place, a red row read a neighbour that an earlier thread had already written. From last step's copy, every order leaves the same rows.
A boid kernel reads neighbours while other move them. Some neighbours may have moved this step and others may not. First copy the fields neighbours need in a separate . Have the steering kernel read only that copy:
Provides the definition's shader members bound to this table, such as reds.gpu.position(i)
and reds.gpu.device.$[i]. A shader reads several tables of one definition through their
gpu members, with nothing piped.
gpu;
/** A fish as its neighbours see it. */
const
constSeen:d.WgslStruct<{
position:d.Vec3f;
velocity:d.Vec3f;
}>
A fish as its neighbours see it.
Seen=
import d
d.
struct<{
position:d.Vec3f;
velocity:d.Vec3f;
}>(props:{
position:d.Vec3f;
velocity:d.Vec3f;
}):d.WgslStruct<{
position:d.Vec3f;
velocity:d.Vec3f;
}>
export struct
Creates a struct schema that can be used to construct GPU buffers.
Ensures proper alignment and padding of properties (as opposed to a d.unstruct schema).
The order of members matches the passed in properties object.
A that reads rows takes getters in its options, such as the table’s gpu members. The module receives no table handle, and its defines shaders that call those getters:
interface
interfaceTrailsOptions
TrailsOptions{
readonly
TrailsOptions.capacity: number
capacity:number;
readonly
TrailsOptions.alive: (i:number)=> boolean
alive:(
i: number
i:number)=>boolean;
readonly
TrailsOptions.position: table.Getter<d.Vec3f>
position:
import table
table.
interfaceGetter<T>
Reads a field in a shader. Its type carries the field schema, so a u32 getter cannot pass for
an f32 getter.
Getter<
import d
d.
export Vec3f
Type of the d.vec3f object/function: vector data type schema/constructor
Vec3f>;
readonly
TrailsOptions.width: table.Getter<d.F32>
width:
import table
table.
interfaceGetter<T>
Reads a field in a shader. Its type carries the field schema, so a u32 getter cannot pass for
an f32 getter.
Getter<
import d
d.
export F32
32-bit float schema representing a single WGSL f32 value.
Provides the definition's shader members bound to this table, such as reds.gpu.position(i)
and reds.gpu.device.$[i]. A shader reads several tables of one definition through their
gpu members, with nothing piped.
Error ts(2322) ― Type 'Getter<U32>' is not assignable to type 'Getter<F32>'.
Types of property '[schemaOf]' are incompatible.
Type 'U32 | undefined' is not assignable to type 'F32 | undefined'.
Type 'U32' is not assignable to type 'F32'.
Types of property 'type' are incompatible.
Type '"u32"' is not assignable to type '"f32"'.
table.Getter keeps a field’s schema in its type, also through pipe. A u32 field then can’t pass for an f32, though both are numbers in TypeScript.
You can pass any shader function. (i) => enemy.alive(i) && enemy.species(i) === BOSS selects some rows.
enemies.live() returns the table’s capacity, its gpu.alive and a span function. Spread them into options for the neighbour grid, live lists, picker or state machines: liveLists.create(root, { ...enemies.live(), draws }). Each dispatches only up to the span.
The CPU doesn’t know a device field’s value. To apply a relative change such as “take 5 damage”, send a command to the kernel that owns the field. A host-only table holds the commands:
const
constHits:table.TableDef<"Hits",{
victim:d.U32;
amount:d.F32;
},{}>
Hits=
import table
table.
functiondefine<"Hits",{
victim:d.U32;
amount:d.F32;
},{}>(name:"Hits",parts:{
host?:{
victim:d.U32;
amount:d.F32;
}|undefined;
device?:{}|undefined;
}):table.TableDef<"Hits",{
victim:d.U32;
amount:d.F32;
},{}>
Declares CPU-written host fields and GPU-written device fields using the records accepted by
d.struct. Throws if no fields are declared, a field
appears in both parts, the CPU writer cannot write a field type such as f16, or a field uses a
reserved member name such as alive.
name labels structs, buffers, WGSL functions and errors. It is not a lookup key. The
definition itself brands row ids, as RowId<typeof Enemy>.
@param ― name a label for ids and WGSL
@param ― parts the host fields the CPU writes and the device fields the GPU writes
@returns ― a definition whose getters shaders read and that table.create allocates
define('Hits',{
host?:{
victim:d.U32;
amount: d.F32;
}|undefined
host:{
victim:d.U32
victim:
import d
d.
constu32:d.U32
export u32
A schema that represents an unsigned 32-bit integer value. (equivalent to u32 in WGSL)
Can also be called to cast a value to an u32 in accordance with WGSL casting rules.
@exampleconst value = u32(); // 0
@exampleconst value = u32(7); // 7
@exampleconst value = u32(3.14); // 3
@exampleconst value = u32(-1); // 4294967295
@exampleconst value = u32(-3.1); // 0
u32,
amount:d.F32
amount:
import d
d.
constf32:d.F32
export f32
A schema that represents a 32-bit float value. (equivalent to f32 in WGSL)
Allocates GPU buffers and a CPU copy of host rows. Throws unless capacity is an integer in [1,
2^20].
@param ― def the definition its shaders were written against, from table.define
@returns ― a table you destroy
create(
constroot:TgpuRoot
root,
constHits:table.TableDef<"Hits",{
victim:d.U32;
amount:d.F32;
},{}>
Hits,{
Options<T extends AnyTableDef>.capacity: number
Sets the table capacity for its lifetime. Must be an integer in [1, 2^20], with slots from 0
to capacity - 1. A row id uses enough bits for the slot and the rest for its generation. The
generation has 29 bits at 8 rows and 12 bits at 2^20 rows.
Provides the definition's shader members bound to this table, such as reds.gpu.position(i)
and reds.gpu.device.$[i]. A shader reads several tables of one definition through their
gpu members, with nothing piped.
Provides the definition's shader members bound to this table, such as reds.gpu.position(i)
and reds.gpu.device.$[i]. A shader reads several tables of one definition through their
gpu members, with nothing piped.
Adds a row in the lowest free slot. Uses defaults for omitted fields. Throws when the table is
full, or when a value fails to write, which leaves the slot free.
A schema that represents an unsigned 32-bit integer value. (equivalent to u32 in WGSL)
Can also be called to cast a value to an u32 in accordance with WGSL casting rules.
@exampleconst value = u32(); // 0
@exampleconst value = u32(7); // 7
@exampleconst value = u32(3.14); // 3
@exampleconst value = u32(-1); // 4294967295
@exampleconst value = u32(-3.1); // 0
u32(0);
let h:number
h<
consthit:GpuMembers<table.TableDef<"Hits",{
victim:d.U32;
amount:d.F32;
},{}>>
hit.
span: TgpuGenericFn
()=> number
Returns the highest live slot plus one.
span();
let h:number
h++){
if(
consthit:GpuMembers<table.TableDef<"Hits",{
victim:d.U32;
amount:d.F32;
},{}>>
hit.
alive: TgpuGenericFn
(i:number)=> boolean
Tests whether slot i holds a row. Returns false beyond the span.
alive(
let h:number
h)&&
constenemy:GpuMembers<table.TableDef<"Enemy",{
species:d.U32;
},{
position:d.Vec3f;
health:d.F32;
}>>
enemy.
slotOf: TgpuGenericFn
(id:number)=> number
Extracts a slot from a row id, such as an id stored in a host field. Does not check the
generation.
slotOf(
consthit:GpuMembers<table.TableDef<"Hits",{
victim:d.U32;
amount:d.F32;
},{}>>
hit.
victim:table.Getter
(i:number)=> number
victim(
let h:number
h))===
consti:number
i)
constrow:{
position:d.v3f;
health:number;
}
row.
health: number
health-=
consthit:GpuMembers<table.TableDef<"Hits",{
victim:d.U32;
amount:d.F32;
},{}>>
hit.
amount:table.Getter
(i:number)=> number
amount(
let h:number
h);
}
});
Use one thread per row to scan the commands. A thread per command can lose an update when two commands hit the same row, and WGSL has no float atomics.
The kernel does rows × commands checks, which suits a few hundred commands per frame. For more, sort them by target or use fixed-point atomics.
Adding rows Rows can’t be added on the GPU; particles keep a ring of their own.
Layout No struct-of-arrays layout, and no packed fields such as rgba8 in a u32.
Storage bindings A table uses three of the eight storage WebGPU guarantees per stage, for host rows, device rows and live bits.
Id maskslotOf(id) reads the id mask from the uniform the table binds for its span, so a shader that reads ids binds it too.
Same names Two definitions with one name and the same fields share their ids’ type.
Hooks The instances renderer passes a whole row to each of an ’s hooks per vertex and per fragment. Prefer field getters in hot shader code to avoid unnecessary loads.
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 →
tableA table is a buffer of rows with named fields, some written by the CPU and the others by kernels.Rows →
instanceAn instance is one copy of a mesh with its own position, rotation, scale and material, kept as a row of a table.Instances →
rowA row is one object's values in a buffer, such as one enemy, one particle or one light.Objects are rows in buffers →
definitionA definition names a table's fields before any table exists, so shader code written over it works with every table made from it.Members and placeholders →
bind groupA bind group is a set of buffers, textures and samplers that a pipeline reads, and a pipeline has room for four.Words →
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 →
flushA flush uploads the rows the CPU changed since the last one.Rows →
outputAn output is a value every drawing writes at each pixel beside its colour, such as motion or an id.Outputs every drawing writes →
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 →
live listA live list is a list of the live rows of one group, made on the GPU each frame so that a draw covers only them.Only what is alive →
rendererA renderer draws one sort of thing, such as instances or particles, by handing a pass description a drawing.Every renderer is a drawing →
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 →
placeholderA placeholder is an accessor with no resource of its own, which each pipeline fills through the inject() of the module it reads.Members and placeholders →
hookA hook is a placeholder that each pipeline fills with its own choice, such as the camera it draws through.Members and placeholders →
accessorAn accessor is a typed value that shader code reads and that each pipeline can fill with a different resource.Modules connect through TypeGPU →
threadA thread is one run of a kernel, and the GPU starts many at once, each with its own number.Slime mould →
dispatchA dispatch runs a kernel once over a number of threads, inside a compute pass.Words →
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 →
factoryA factory is the function that creates a module, which allocates what the module needs and returns its resources, functions and recording methods.A module borrows →
bindingA binding is one buffer, texture or sampler in a bind group, which a shader reads by its place there.Words →
kindA kind adds fields to the rows of a set of instances and replaces functions in the shaders that draw them.Kinds →
flush() once a frame, before submit: it uploads the frame's changes with queue.writeBuffer, which land before every later submit. With history, flush(frame) also records copies that keep last frame's rows, before the passes that read the table.
The host and device rows, the live bits and the span; a part without fields has no buffer. With history, a part's buffer holds last frame's rows after its own, and 2 more per part hold the uploads and a copy on the way.