Skip to content

ModuleCore / Tables and listsConcept: Data on the GPU

Tables

Per-object rows with separate buffers for CPU and GPU writes, so uploads preserve kernel updates.

import
import { table } from '@typegpu/kit'
call
table.create(root, def, { capacity, history, … }) → HistoryTablealso rows · define
facts
recordsencoderlifecycledestroy()injectsdefinition
On this page

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
const Enemy: 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
.
function define<"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
.
const u32: 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.

@example const value = u32(); // 0

@example const value = u32(7); // 7

@example const value = u32(3.14); // 3

@example const value = u32(-1); // 4294967295

@example const value = u32(-3.1); // 0

u32
,
tint: d.Vec4f
tint
:
import d
d
.
const vec4f: d.Vec4f
vec4f
,
radius: d.F32
radius
:
import d
d
.
const f32: 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.

@example const value = f32(); // 0

@example const value = f32(1.23); // 1.23

@example const 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
.
const vec3f: d.Vec3f
vec3f
,
velocity: d.Vec3f
velocity
:
import d
d
.
const vec3f: d.Vec3f
vec3f
,
health: d.F32
health
:
import d
d
.
const f32: 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.

@example const value = f32(); // 0

@example const value = f32(1.23); // 1.23

@example const value = f32(true); // 1

f32
}, // by kernels
});
const
const enemies: 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
.
function create<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;
}>> (+1 overload)

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
(
const root: TgpuRoot
root
,
const Enemy: 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,
Options<TableDef<"Enemy", { species: U32; tint: Vec4f; radius: F32; }, { position: Vec3f; velocity: Vec3f; health: F32; }>>.defaults?: table.Values<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>> | undefined

Sets values for fields omitted by add. Without defaults, every field starts at zero. Default none.

defaults
: {
tint?: ArrayLike<number> | d.InferInput<d.Vec4f> | undefined
tint
: [1, 1, 1, 1],
radius?: number | undefined
radius
: 0.5,
health?: number | undefined
health
: 10 },
});
const
const id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
id
=
const enemies: table.Table<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemies
.
Table<TableDef<"Enemy", { species: U32; tint: Vec4f; radius: F32; }, { position: Vec3f; velocity: Vec3f; health: F32; }>>.add(values?: table.Values<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>> | undefined): table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>

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.

add
({
species?: number | undefined
species
: 2,
position?: ArrayLike<number> | d.InferInput<d.Vec3f> | undefined
position
: [4, 0, 1] });
const enemies: table.Table<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemies
.
Table<TableDef<"Enemy", { species: U32; tint: Vec4f; radius: F32; }, { position: Vec3f; velocity: Vec3f; health: F32; }>>.fields: table.Fields<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>

Provides one handle per field. Keep a handle outside a loop instead of reading fields.x inside it.

fields
.
tint: table.HostField<d.Vec4f, table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
tint
.
function set(id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>, x: number, y: number, z: number, w: number): void
set
(
const id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
id
, 1, 0, 0, 1);
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 enemies: table.Table<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemies
.
Table<TableDef<"Enemy", { species: U32; tint: Vec4f; radius: F32; }, { position: Vec3f; velocity: Vec3f; health: F32; }>>.flush(target?: frames.Frame | TgpuCommandEncoder): void

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.

flush
(); // uploads what changed, before submit
// ... passes
});
const enemies: table.Table<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemies
.
Table<TableDef<"Enemy", { species: U32; tint: Vec4f; radius: F32; }, { position: Vec3f; velocity: Vec3f; health: F32; }>>.remove(id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>): void

Frees the row slot and makes its id stale.

remove
(
const id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
id
);
  • Definitions table.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.
  • Buffers enemies.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 schemas table.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 ids remove(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:

Capacity Generation bits Wraps after
8 29 52 days
300 23 19 hours
4096 20 2.4 hours
2^20 12 34 seconds

enemies.fields has a handle per field, which writes the CPU copy at a fixed offset:

const
const tint: table.HostField<d.Vec4f, table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
tint
=
const enemies: table.Table<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemies
.
Table<TableDef<"Enemy", { species: U32; tint: Vec4f; radius: F32; }, { position: Vec3f; velocity: Vec3f; health: F32; }>>.fields: table.Fields<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>

Provides one handle per field. Keep a handle outside a loop instead of reading fields.x inside it.

fields
.
tint: table.HostField<d.Vec4f, table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
tint
; // keep it outside the loop
for (const
const id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
id
of
const hit: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>[]
hit
)
const tint: table.HostField<d.Vec4f, table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
tint
.
function set(id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>, x: number, y: number, z: number, w: number): void
set
(
const id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
id
, 1, 0, 0, 1); // components: nothing allocated
const tint: table.HostField<d.Vec4f, table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
tint
.
FieldWrite<Vec4f, TableDef<"Enemy", { species: U32; tint: Vec4f; radius: F32; }, { position: Vec3f; velocity: Vec3f; health: F32; }>>.write(id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>, value: ArrayLike<number> | d.InferInput<d.Vec4f>): void

Writes the whole field. A vector takes any array of numbers and reads its first components. Reuse typed inputs to avoid allocating.

write
(
const id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
id
, [1, 0, 0, 1]); // allocates the tuple
const tint: table.HostField<d.Vec4f, table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
tint
.
get<d.v4f>(id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>, out: d.v4f): d.v4f

Copies the field into out, a vector or an array of numbers, and returns out.

get
(
const id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
id
,
import d
d
.
const vec4f: d.Vec4f
() => d.v4f (+9 overloads)
vec4f
()); // host fields read back from the CPU copy
const enemies: table.Table<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemies
.
Table<TableDef<"Enemy", { species: U32; tint: Vec4f; radius: F32; }, { position: Vec3f; velocity: Vec3f; health: F32; }>>.write(id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>, values: table.Values<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>): void

Writes values to several fields. Device field writes overwrite kernel results at flush.

write
(
const id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
id
, {
tint?: ArrayLike<number> | d.InferInput<d.Vec4f> | undefined
tint
: [1, 0, 0, 1],
health?: number | undefined
health
: 10 }); // several fields, both parts
  • 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.

A flush uploads only the ranges that changed

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;
  • the live bits, and the span when it changed.

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:

const
const enemies: table.HistoryTable<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
.
function create<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;
}>> & {
readonly history: true;
}): table.HistoryTable<...> (+1 overload)

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

history
: true });
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 enemies: table.HistoryTable<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemies
.
HistoryTable<TableDef<"Enemy", { species: U32; tint: Vec4f; radius: F32; }, { position: Vec3f; velocity: Vec3f; health: F32; }>>.flush(target: frames.Frame | TgpuCommandEncoder): void

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 :

const
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemy
=
const enemies: table.Table<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemies
.
Table<TableDef<"Enemy", { species: U32; tint: Vec4f; radius: F32; }, { position: Vec3f; velocity: Vec3f; health: F32; }>>.gpu: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>

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
const
const sinkOf: (row: RowOf<typeof Enemy>) => number
sinkOf
= (
row: {
species: number;
tint: d.v4f;
radius: number;
position: d.v3f;
velocity: d.v3f;
health: number;
}
row
:
type RowOf<T extends AnyTableDef> = { [K in keyof InferGPURecord<HostOf<T> & DeviceOf<T>>]: InferGPURecord<HostOf<T> & DeviceOf<T>>[K]; }

Gives the whole row type as read by shaders, for helpers that take a row value.

RowOf
<typeof
const Enemy: table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>
Enemy
>) => {
'use gpu';
return
import std
std
.
function select(f: number, t: number, cond: boolean): number (+2 overloads)
export select

Returns t if cond is true, and f otherwise. Component-wise if cond is a vector.

@example select(1, 2, false) // returns 1 select(1, 2, true) // returns 2 select(vec2i(1, 2), vec2i(3, 4), true) // returns vec2i(3, 4) select(vec2i(1, 2), vec2i(3, 4), vec2b(false, true)) // returns vec2i(1, 4)

select
(
import d
d
.
function f32(v?: number | boolean): number
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.

@example const value = f32(); // 0

@example const value = f32(1.23); // 1.23

@example const value = f32(true); // 1

f32
(0),
row: {
species: number;
tint: d.v4f;
radius: number;
position: d.v3f;
velocity: d.v3f;
health: number;
}
row
.
radius: number
radius
* 2,
row: {
species: number;
tint: d.v4f;
radius: number;
position: d.v3f;
velocity: d.v3f;
health: number;
}
row
.
health: number
health
<= 0);
};
const
const vertex: TgpuVertexFn<{}, {}>
vertex
=
const tgpu: {
const: typeof import("node_modules/typegpu/core/constant/tgpuConstant").constant;
fn: typeof import("node_modules/typegpu/core/function/tgpuFn").fn;
comptime: typeof import("node_modules/typegpu/core/function/comptime").comptime;
resolve: typeof import("node_modules/typegpu/core/resolve/tgpuResolve").resolve;
resolveWithContext: typeof import("node_modules/typegpu/core/resolve/tgpuResolve").resolveWithContext;
init: typeof import("node_modules/typegpu/core/root/init").init;
initFromDevice: typeof import("node_modules/typegpu/core/root/init").initFromDevice;
slot: typeof import("node_modules/typegpu/core/slot/slot").slot;
lazy: typeof import("node_modules/typegpu/core/slot/lazy").lazy;
... 10 more ...;
'~unstable': typeof import("node_modules/typegpu/tgpuUnstable");
}

@module ― typegpu

tgpu
.
vertexFn: <{
vertexIndex: d.BuiltinVertexIndex;
instanceIndex: d.BuiltinInstanceIndex;
}, {
position: d.BuiltinPosition;
}>(options: {
in: {
vertexIndex: d.BuiltinVertexIndex;
instanceIndex: d.BuiltinInstanceIndex;
};
out: {
position: d.BuiltinPosition;
};
}) => TgpuVertexFnShell<{
vertexIndex: d.BuiltinVertexIndex;
instanceIndex: d.BuiltinInstanceIndex;
}, {
position: d.BuiltinPosition;
}> (+1 overload)
vertexFn
({
in: {
vertexIndex: d.BuiltinVertexIndex;
instanceIndex: d.BuiltinInstanceIndex;
}
in
: {
vertexIndex: d.BuiltinVertexIndex
vertexIndex
:
import d
d
.
const builtin: {
readonly vertexIndex: d.BuiltinVertexIndex;
readonly instanceIndex: d.BuiltinInstanceIndex;
readonly clipDistances: d.BuiltinClipDistances;
readonly position: d.BuiltinPosition;
readonly frontFacing: d.BuiltinFrontFacing;
readonly fragDepth: d.BuiltinFragDepth;
readonly primitiveIndex: d.BuiltinPrimitiveIndex;
readonly sampleIndex: d.BuiltinSampleIndex;
... 11 more ...;
readonly numSubgroups: d.BuiltinNumSubgroups;
}
export builtin
builtin
.
vertexIndex: d.BuiltinVertexIndex
vertexIndex
,
instanceIndex: d.BuiltinInstanceIndex
instanceIndex
:
import d
d
.
const builtin: {
readonly vertexIndex: d.BuiltinVertexIndex;
readonly instanceIndex: d.BuiltinInstanceIndex;
readonly clipDistances: d.BuiltinClipDistances;
readonly position: d.BuiltinPosition;
readonly frontFacing: d.BuiltinFrontFacing;
readonly fragDepth: d.BuiltinFragDepth;
readonly primitiveIndex: d.BuiltinPrimitiveIndex;
readonly sampleIndex: d.BuiltinSampleIndex;
... 11 more ...;
readonly numSubgroups: d.BuiltinNumSubgroups;
}
export builtin
builtin
.
instanceIndex: d.BuiltinInstanceIndex
instanceIndex
},
out: {
position: d.BuiltinPosition;
}
out
: {
position: d.BuiltinPosition
position
:
import d
d
.
const builtin: {
readonly vertexIndex: d.BuiltinVertexIndex;
readonly instanceIndex: d.BuiltinInstanceIndex;
readonly clipDistances: d.BuiltinClipDistances;
readonly position: d.BuiltinPosition;
readonly frontFacing: d.BuiltinFrontFacing;
readonly fragDepth: d.BuiltinFragDepth;
readonly primitiveIndex: d.BuiltinPrimitiveIndex;
readonly sampleIndex: d.BuiltinSampleIndex;
... 11 more ...;
readonly numSubgroups: d.BuiltinNumSubgroups;
}
export builtin
builtin
.
position: d.BuiltinPosition
position
},
})(({
vertexIndex: number
vertexIndex
,
instanceIndex: number
instanceIndex
}) => {
'use gpu';
const
const slot: number
slot
=
const live: liveLists.LiveLists
live
.
LiveLists.slot: (group: number, i: number) => number

Returns the slot of live row i in group. Pass group as a captured constant.

slot
(
const SKELETON: 0
SKELETON
,
instanceIndex: number
instanceIndex
);
const
const center: d.v3f
center
=
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemy
.
position: table.Getter
(i: number) => d.v3f
position
(
const slot: number
slot
); // loads one field
const
const row: {
species: number;
tint: d.v4f;
radius: number;
position: d.v3f;
velocity: d.v3f;
health: number;
}
row
=
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemy
.
row: TgpuGenericFn
(i: number) => {
species: number;
tint: d.v4f;
radius: number;
position: d.v3f;
velocity: d.v3f;
health: number;
}

Returns a copy of the whole row, including every field, for helpers that take a row value.

row
(
const slot: number
slot
); // the whole row
const
const corner: d.v3f
corner
=
const cube: meshes.Geometry<d.WgslStruct<{
position: d.Vec3f;
normal: d.Vec3f;
uv: d.Vec2f;
}>>
cube
.
Geometry<WgslStruct<{ position: Vec3f; normal: Vec3f; uv: Vec2f; }>>.vertexAt(this: void, i: number): {
position: d.v3f;
normal: d.v3f;
uv: d.v2f;
}
vertexAt
(
vertexIndex: number
vertexIndex
).
position: d.v3f
position
*
const row: {
species: number;
tint: d.v4f;
radius: number;
position: d.v3f;
velocity: d.v3f;
health: number;
}
row
.
radius: number
radius
;
const
const world: d.v3f
world
=
const center: d.v3f
center
+
const corner: d.v3f
corner
-
import d
d
.
const vec3f: d.Vec3f
(x: number, y: number, z: number) => d.v3f (+5 overloads)
vec3f
(0,
const sinkOf: (row: RowOf<typeof Enemy>) => number
sinkOf
(
const row: {
species: number;
tint: d.v4f;
radius: number;
position: d.v3f;
velocity: d.v3f;
health: number;
}
row
), 0);
return {
position: d.v4f
position
:
const viewProjection: TgpuUniform<d.Mat4x4f>
viewProjection
.
TgpuUniform<Mat4x4f>.$: d.m4x4f
$
*
import d
d
.
const vec4f: d.Vec4f
(v0: AnyNumericVec3Instance, w: number) => d.v4f (+9 overloads)
vec4f
(
const world: d.v3f
world
, 1) };
});
  • 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
const reds: table.Table<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
reds
=
import table
table
.
function create<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;
}>> (+1 overload)

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
(
const root: TgpuRoot
root
,
const Enemy: 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
const blues: table.Table<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
blues
=
import table
table
.
function create<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;
}>> (+1 overload)

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
(
const root: TgpuRoot
root
,
const Enemy: 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
const red: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
red
=
const reds: table.Table<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
reds
.
Table<TableDef<"Enemy", { species: U32; }, { position: Vec3f; health: F32; }>>.gpu: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>

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
const blue: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
blue
=
const blues: table.Table<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
blues
.
Table<TableDef<"Enemy", { species: U32; }, { position: Vec3f; health: F32; }>>.gpu: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>

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
const claimed: TgpuMutable<d.Atomic<d.U32>>
claimed
=
const root: 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.

@example const atomicU32 = d.atomic(d.u32); const atomicI32 = d.atomic(d.i32);

@param ― data Underlying type schema.

atomic
(
import d
d
.
const u32: 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.

@example const value = u32(); // 0

@example const value = u32(7); // 7

@example const value = u32(3.14); // 3

@example const value = u32(-1); // 4294967295

@example const value = u32(-3.1); // 0

u32
));
const
const defect: TgpuComputeFn<{
gid: d.BuiltinGlobalInvocationId;
}>
defect
=
const tgpu: {
const: typeof import("node_modules/typegpu/core/constant/tgpuConstant").constant;
fn: typeof import("node_modules/typegpu/core/function/tgpuFn").fn;
comptime: typeof import("node_modules/typegpu/core/function/comptime").comptime;
resolve: typeof import("node_modules/typegpu/core/resolve/tgpuResolve").resolve;
resolveWithContext: typeof import("node_modules/typegpu/core/resolve/tgpuResolve").resolveWithContext;
init: typeof import("node_modules/typegpu/core/root/init").init;
initFromDevice: typeof import("node_modules/typegpu/core/root/init").initFromDevice;
slot: typeof import("node_modules/typegpu/core/slot/slot").slot;
lazy: typeof import("node_modules/typegpu/core/slot/lazy").lazy;
... 10 more ...;
'~unstable': typeof import("node_modules/typegpu/tgpuUnstable");
}

@module ― typegpu

tgpu
.
computeFn: <{
gid: d.BuiltinGlobalInvocationId;
}>(options: {
in: {
gid: d.BuiltinGlobalInvocationId;
};
workgroupSize: number[];
}) => TgpuComputeFnShell<{
gid: d.BuiltinGlobalInvocationId;
}> (+1 overload)
computeFn
({
workgroupSize: number[]
workgroupSize
: [64],
in: {
gid: d.BuiltinGlobalInvocationId;
}
in
: {
gid: d.BuiltinGlobalInvocationId
gid
:
import d
d
.
const builtin: {
readonly vertexIndex: d.BuiltinVertexIndex;
readonly instanceIndex: d.BuiltinInstanceIndex;
readonly clipDistances: d.BuiltinClipDistances;
readonly position: d.BuiltinPosition;
readonly frontFacing: d.BuiltinFrontFacing;
readonly fragDepth: d.BuiltinFragDepth;
readonly primitiveIndex: d.BuiltinPrimitiveIndex;
readonly sampleIndex: d.BuiltinSampleIndex;
... 11 more ...;
readonly numSubgroups: d.BuiltinNumSubgroups;
}
export builtin
builtin
.
globalInvocationId: d.BuiltinGlobalInvocationId
globalInvocationId
},
})(({
gid: d.v3u
gid
}) => {
'use gpu';
const
const i: number
i
=
gid: d.v3u
gid
.
v3u.x: number
x
;
if (!
const red: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
red
.
alive: TgpuGenericFn
(i: number) => boolean

Tests whether slot i holds a row. Returns false beyond the span.

alive
(
const i: number
i
) ||
const red: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
red
.
position: table.Getter
(i: number) => d.v3f
position
(
const i: number
i
).
v3f.x: number
x
> 0) return;
const blue: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
blue
.
device: TgpuAccessor<table.Rows<{
position: d.Vec3f;
health: d.F32;
}>>

Provides GPU-written rows with read-write access in compute pipelines and read-only access in render pipelines.

device
.
TgpuAccessor<Rows<{ position: Vec3f; health: F32; }>>.$: {
position: d.v3f;
health: number;
}[]
$
[
import std
std
.
atomicAdd<d.atomicU32>(a: d.atomicU32, value: number): number
export atomicAdd
atomicAdd
(
const claimed: TgpuMutable<d.Atomic<d.U32>>
claimed
.
TgpuMutable<Atomic<U32>>.$: d.atomicU32
$
, 1)].
health: number
health
=
const red: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
red
.
health: table.Getter
(i: number) => number
health
(
const i: number
i
);
const red: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
red
.
device: TgpuAccessor<table.Rows<{
position: d.Vec3f;
health: d.F32;
}>>

Provides GPU-written rows with read-write access in compute pipelines and read-only access in render pipelines.

device
.
TgpuAccessor<Rows<{ position: Vec3f; health: F32; }>>.$: {
position: d.v3f;
health: number;
}[]
$
[
const i: number
i
].
health: number
health
= 0;
});
const
const defecting: TgpuComputePipeline
defecting
=
const root: TgpuRoot
root
.
WithBinding.createComputePipeline<{
gid: d.BuiltinGlobalInvocationId;
}>(descriptor: TgpuComputePipeline.Descriptor<{
gid: d.BuiltinGlobalInvocationId;
}>): TgpuComputePipeline
createComputePipeline
({
compute: TgpuComputeFn<{
gid: d.BuiltinGlobalInvocationId;
}>
compute
:
const defect: TgpuComputeFn<{
gid: d.BuiltinGlobalInvocationId;
}>
defect
});

Connecting modules states the limits of members.

A kernel writes through a reference from enemy.device, and reads through the getters:

const
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemy
=
const enemies: table.Table<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemies
.
Table<TableDef<"Enemy", { species: U32; tint: Vec4f; radius: F32; }, { position: Vec3f; velocity: Vec3f; health: F32; }>>.gpu: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>

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
const STEP: number
STEP
= 1 / 60;
const
const crowd: TgpuComputeFn<{
gid: d.BuiltinGlobalInvocationId;
}>
crowd
=
const tgpu: {
const: typeof import("node_modules/typegpu/core/constant/tgpuConstant").constant;
fn: typeof import("node_modules/typegpu/core/function/tgpuFn").fn;
comptime: typeof import("node_modules/typegpu/core/function/comptime").comptime;
resolve: typeof import("node_modules/typegpu/core/resolve/tgpuResolve").resolve;
resolveWithContext: typeof import("node_modules/typegpu/core/resolve/tgpuResolve").resolveWithContext;
init: typeof import("node_modules/typegpu/core/root/init").init;
initFromDevice: typeof import("node_modules/typegpu/core/root/init").initFromDevice;
slot: typeof import("node_modules/typegpu/core/slot/slot").slot;
lazy: typeof import("node_modules/typegpu/core/slot/lazy").lazy;
... 10 more ...;
'~unstable': typeof import("node_modules/typegpu/tgpuUnstable");
}

@module ― typegpu

tgpu
.
computeFn: <{
gid: d.BuiltinGlobalInvocationId;
}>(options: {
in: {
gid: d.BuiltinGlobalInvocationId;
};
workgroupSize: number[];
}) => TgpuComputeFnShell<{
gid: d.BuiltinGlobalInvocationId;
}> (+1 overload)
computeFn
({
workgroupSize: number[]
workgroupSize
: [64],
in: {
gid: d.BuiltinGlobalInvocationId;
}
in
: {
gid: d.BuiltinGlobalInvocationId
gid
:
import d
d
.
const builtin: {
readonly vertexIndex: d.BuiltinVertexIndex;
readonly instanceIndex: d.BuiltinInstanceIndex;
readonly clipDistances: d.BuiltinClipDistances;
readonly position: d.BuiltinPosition;
readonly frontFacing: d.BuiltinFrontFacing;
readonly fragDepth: d.BuiltinFragDepth;
readonly primitiveIndex: d.BuiltinPrimitiveIndex;
readonly sampleIndex: d.BuiltinSampleIndex;
... 11 more ...;
readonly numSubgroups: d.BuiltinNumSubgroups;
}
export builtin
builtin
.
globalInvocationId: d.BuiltinGlobalInvocationId
globalInvocationId
},
})(({
gid: d.v3u
gid
}) => {
'use gpu';
const
const i: number
i
=
gid: d.v3u
gid
.
v3u.x: number
x
;
if (!
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemy
.
alive: TgpuGenericFn
(i: number) => boolean

Tests whether slot i holds a row. Returns false beyond the span.

alive
(
const i: number
i
)) return; // also false past the span
const
const speed: number
speed
= 2 /
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemy
.
radius: table.Getter
(i: number) => number
radius
(
const i: number
i
); // a host field: small enemies are fast
const
const row: {
position: d.v3f;
velocity: d.v3f;
health: number;
}
row
=
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
tint: d.Vec4f;
radius: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>
enemy
.
device: TgpuAccessor<table.Rows<{
position: d.Vec3f;
velocity: d.Vec3f;
health: d.F32;
}>>

Provides GPU-written rows with read-write access in compute pipelines and read-only access in render pipelines.

device
.
TgpuAccessor<Rows<{ position: Vec3f; velocity: Vec3f; health: F32; }>>.$: {
position: d.v3f;
velocity: d.v3f;
health: number;
}[]
$
[
const i: number
i
]; // a reference: writes go to the buffer
const row: {
position: d.v3f;
velocity: d.v3f;
health: number;
}
row
.
velocity: d.v3f
velocity
=
import std
std
.
mix<d.v3f>(e1: d.v3f, e2: d.v3f, e3: number): d.v3f (+2 overloads)
export mix
mix
(
const row: {
position: d.v3f;
velocity: d.v3f;
health: number;
}
row
.
velocity: d.v3f
velocity
,
const toPlayer: (position: d.v3f) => d.v3f
toPlayer
(
const row: {
position: d.v3f;
velocity: d.v3f;
health: number;
}
row
.
position: d.v3f
position
) *
const speed: number
speed
, 0.1);
const row: {
position: d.v3f;
velocity: d.v3f;
health: number;
}
row
.
position: d.v3f
position
+=
const row: {
position: d.v3f;
velocity: d.v3f;
health: number;
}
row
.
velocity: d.v3f
velocity
*
const STEP: number
STEP
;
});

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

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:

const
const fish: GpuMembers<table.TableDef<"Fish", {
species: d.U32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
}>>
fish
=
const school: table.Table<table.TableDef<"Fish", {
species: d.U32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
}>>
school
.
Table<TableDef<"Fish", { species: U32; }, { position: Vec3f; velocity: Vec3f; }>>.gpu: GpuMembers<table.TableDef<"Fish", {
species: d.U32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
}>>

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
const Seen: 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.

@example const CircleStruct = d.struct({ radius: d.f32, pos: d.vec3f });

@param ― props Record with string keys and TgpuData values, each entry describing one struct member.

struct
({
position: d.Vec3f
position
:
import d
d
.
const vec3f: d.Vec3f
vec3f
,
velocity: d.Vec3f
velocity
:
import d
d
.
const vec3f: d.Vec3f
vec3f
});
const
const seen: TgpuMutable<d.WgslArray<d.WgslStruct<{
position: d.Vec3f;
velocity: d.Vec3f;
}>>>
seen
=
const root: TgpuRoot
root
.
TgpuRoot.createMutable<d.WgslArray<d.WgslStruct<{
position: d.Vec3f;
velocity: d.Vec3f;
}>>>(typeSchema: d.WgslArray<d.WgslStruct<{
position: d.Vec3f;
velocity: d.Vec3f;
}>>, initial?: ((buffer: TgpuBuffer<NoInfer<d.WgslArray<d.WgslStruct<{
position: d.Vec3f;
velocity: d.Vec3f;
}>>>>) => void) | d.InferInput<NoInfer<d.WgslArray<d.WgslStruct<{
position: d.Vec3f;
velocity: d.Vec3f;
}>>>> | undefined): TgpuMutable<d.WgslArray<d.WgslStruct<{
position: d.Vec3f;
velocity: d.Vec3f;
}>>> (+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
.
arrayOf<d.WgslStruct<{
position: d.Vec3f;
velocity: d.Vec3f;
}>>(elementType: d.WgslStruct<{
position: d.Vec3f;
velocity: d.Vec3f;
}>, elementCount: number): d.WgslArray<d.WgslStruct<{
position: d.Vec3f;
velocity: d.Vec3f;
}>> (+2 overloads)
export arrayOf

@location ― . Wrap align/size in a struct instead, e.g. d.arrayOf(d.struct({ value: d.align(16, d.u32) }), n).

arrayOf
(
const Seen: d.WgslStruct<{
position: d.Vec3f;
velocity: d.Vec3f;
}>

A fish as its neighbours see it.

Seen
, 4096));
const
const snapshot: TgpuComputeFn<{
gid: d.BuiltinGlobalInvocationId;
}>
snapshot
=
const tgpu: {
const: typeof import("node_modules/typegpu/core/constant/tgpuConstant").constant;
fn: typeof import("node_modules/typegpu/core/function/tgpuFn").fn;
comptime: typeof import("node_modules/typegpu/core/function/comptime").comptime;
resolve: typeof import("node_modules/typegpu/core/resolve/tgpuResolve").resolve;
resolveWithContext: typeof import("node_modules/typegpu/core/resolve/tgpuResolve").resolveWithContext;
init: typeof import("node_modules/typegpu/core/root/init").init;
initFromDevice: typeof import("node_modules/typegpu/core/root/init").initFromDevice;
slot: typeof import("node_modules/typegpu/core/slot/slot").slot;
lazy: typeof import("node_modules/typegpu/core/slot/lazy").lazy;
... 10 more ...;
'~unstable': typeof import("node_modules/typegpu/tgpuUnstable");
}

@module ― typegpu

tgpu
.
computeFn: <{
gid: d.BuiltinGlobalInvocationId;
}>(options: {
in: {
gid: d.BuiltinGlobalInvocationId;
};
workgroupSize: number[];
}) => TgpuComputeFnShell<{
gid: d.BuiltinGlobalInvocationId;
}> (+1 overload)
computeFn
({
workgroupSize: number[]
workgroupSize
: [64],
in: {
gid: d.BuiltinGlobalInvocationId;
}
in
: {
gid: d.BuiltinGlobalInvocationId
gid
:
import d
d
.
const builtin: {
readonly vertexIndex: d.BuiltinVertexIndex;
readonly instanceIndex: d.BuiltinInstanceIndex;
readonly clipDistances: d.BuiltinClipDistances;
readonly position: d.BuiltinPosition;
readonly frontFacing: d.BuiltinFrontFacing;
readonly fragDepth: d.BuiltinFragDepth;
readonly primitiveIndex: d.BuiltinPrimitiveIndex;
readonly sampleIndex: d.BuiltinSampleIndex;
... 11 more ...;
readonly numSubgroups: d.BuiltinNumSubgroups;
}
export builtin
builtin
.
globalInvocationId: d.BuiltinGlobalInvocationId
globalInvocationId
},
})(({
gid: d.v3u
gid
}) => {
'use gpu';
const
const i: number
i
=
gid: d.v3u
gid
.
v3u.x: number
x
;
if (!
const fish: GpuMembers<table.TableDef<"Fish", {
species: d.U32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
}>>
fish
.
alive: TgpuGenericFn
(i: number) => boolean

Tests whether slot i holds a row. Returns false beyond the span.

alive
(
const i: number
i
)) return;
const seen: TgpuMutable<d.WgslArray<d.WgslStruct<{
position: d.Vec3f;
velocity: d.Vec3f;
}>>>
seen
.
TgpuMutable<WgslArray<WgslStruct<{ position: Vec3f; velocity: Vec3f; }>>>.$: {
position: d.v3f;
velocity: d.v3f;
}[]
$
[
const i: number
i
] =
const Seen: d.WgslStruct
(props?: {
position: d.v3f;
velocity: d.v3f;
} | undefined) => {
position: d.v3f;
velocity: d.v3f;
}

A fish as its neighbours see it.

Seen
({
position: d.v3f
position
:
const fish: GpuMembers<table.TableDef<"Fish", {
species: d.U32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
}>>
fish
.
position: table.Getter
(i: number) => d.v3f
position
(
const i: number
i
),
velocity: d.v3f
velocity
:
const fish: GpuMembers<table.TableDef<"Fish", {
species: d.U32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
}>>
fish
.
velocity: table.Getter
(i: number) => d.v3f
velocity
(
const i: number
i
) });
});
// the steering kernel reads seen.$[k] and writes only fish.device.$[i]
  • Two dispatches in one compute pass run in order, so the copy is whole before steering starts.
  • The copy holds only what neighbours read. In the fish tank, the neighbour grid’s sort writes it, in sorted order.

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
interface TrailsOptions
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
.
interface Getter<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
.
interface Getter<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.

F32
>;
}
export function
function createTrails(root: TgpuRoot, options: TrailsOptions): TgpuComputePipeline
createTrails
(
root: TgpuRoot
root
:
(alias) interface TgpuRoot
import TgpuRoot
TgpuRoot
,
options: TrailsOptions
options
:
interface TrailsOptions
TrailsOptions
) {
const {
const capacity: number
capacity
,
const alive: (i: number) => boolean
alive
,
const position: table.Getter<d.Vec3f>
position
,
const width: table.Getter<d.F32>
width
} =
options: TrailsOptions
options
;
const
const step: TgpuComputeFn<{
gid: d.BuiltinGlobalInvocationId;
}>
step
=
const tgpu: {
const: typeof import("node_modules/typegpu/core/constant/tgpuConstant").constant;
fn: typeof import("node_modules/typegpu/core/function/tgpuFn").fn;
comptime: typeof import("node_modules/typegpu/core/function/comptime").comptime;
resolve: typeof import("node_modules/typegpu/core/resolve/tgpuResolve").resolve;
resolveWithContext: typeof import("node_modules/typegpu/core/resolve/tgpuResolve").resolveWithContext;
init: typeof import("node_modules/typegpu/core/root/init").init;
initFromDevice: typeof import("node_modules/typegpu/core/root/init").initFromDevice;
slot: typeof import("node_modules/typegpu/core/slot/slot").slot;
lazy: typeof import("node_modules/typegpu/core/slot/lazy").lazy;
... 10 more ...;
'~unstable': typeof import("node_modules/typegpu/tgpuUnstable");
}

@module ― typegpu

tgpu
.
computeFn: <{
gid: d.BuiltinGlobalInvocationId;
}>(options: {
in: {
gid: d.BuiltinGlobalInvocationId;
};
workgroupSize: number[];
}) => TgpuComputeFnShell<{
gid: d.BuiltinGlobalInvocationId;
}> (+1 overload)
computeFn
({
workgroupSize: number[]
workgroupSize
: [64],
in: {
gid: d.BuiltinGlobalInvocationId;
}
in
: {
gid: d.BuiltinGlobalInvocationId
gid
:
import d
d
.
const builtin: {
readonly vertexIndex: d.BuiltinVertexIndex;
readonly instanceIndex: d.BuiltinInstanceIndex;
readonly clipDistances: d.BuiltinClipDistances;
readonly position: d.BuiltinPosition;
readonly frontFacing: d.BuiltinFrontFacing;
readonly fragDepth: d.BuiltinFragDepth;
readonly primitiveIndex: d.BuiltinPrimitiveIndex;
readonly sampleIndex: d.BuiltinSampleIndex;
... 11 more ...;
readonly numSubgroups: d.BuiltinNumSubgroups;
}
export builtin
builtin
.
globalInvocationId: d.BuiltinGlobalInvocationId
globalInvocationId
},
})(({
gid: d.v3u
gid
}) => {
'use gpu';
if (
gid: d.v3u
gid
.
v3u.x: number
x
>=
const capacity: number
capacity
|| !
const alive: (i: number) => boolean
alive
(
gid: d.v3u
gid
.
v3u.x: number
x
)) return;
const
const p: d.v3f
p
=
const position: table.Getter
(i: number) => d.v3f
position
(
gid: d.v3u
gid
.
v3u.x: number
x
);
const
const w: number
w
=
const width: table.Getter
(i: number) => number
width
(
gid: d.v3u
gid
.
v3u.x: number
x
);
// ..
});
return
root: TgpuRoot
root
.
WithBinding.createComputePipeline<{
gid: d.BuiltinGlobalInvocationId;
}>(descriptor: TgpuComputePipeline.Descriptor<{
gid: d.BuiltinGlobalInvocationId;
}>): TgpuComputePipeline
createComputePipeline
({
compute: TgpuComputeFn<{
gid: d.BuiltinGlobalInvocationId;
}>
compute
:
const step: TgpuComputeFn<{
gid: d.BuiltinGlobalInvocationId;
}>
step
});
}
const
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
size: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
}>>
enemy
=
const enemies: table.Table<table.TableDef<"Enemy", {
species: d.U32;
size: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
}>>
enemies
.
Table<TableDef<"Enemy", { species: U32; size: F32; }, { position: Vec3f; velocity: Vec3f; }>>.gpu: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
size: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
}>>

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
const trails: {
position: table.Getter<d.Vec3f>;
capacity: number;
alive: (i: number) => boolean;
span?: () => number;
}
trails
= { ...
const enemies: table.Table<table.TableDef<"Enemy", {
species: d.U32;
size: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
}>>
enemies
.
Table<TableDef<"Enemy", { species: U32; size: F32; }, { position: Vec3f; velocity: Vec3f; }>>.live(): table.Live

Returns the capacity and live test for live lists, a grid, a picker or state machines. Use liveLists.create(root, { ...coins.live(), draws }).

live
(),
position: table.Getter<d.Vec3f>
position
:
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
size: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
}>>
enemy
.
position: table.Getter<d.Vec3f>
position
};
function createTrails(root: TgpuRoot, options: TrailsOptions): TgpuComputePipeline
createTrails
(
const root: TgpuRoot
root
, { ...
const trails: {
position: table.Getter<d.Vec3f>;
capacity: number;
alive: (i: number) => boolean;
span?: () => number;
}
trails
,
TrailsOptions.width: table.Getter<d.F32>
width
:
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
size: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
}>>
enemy
.
size: table.Getter<d.F32>
size
});
function createTrails(root: TgpuRoot, options: TrailsOptions): TgpuComputePipeline
createTrails
(
const root: TgpuRoot
root
, { ...
const trails: {
position: table.Getter<d.Vec3f>;
capacity: number;
alive: (i: number) => boolean;
span?: () => number;
}
trails
, width:
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
size: d.F32;
}, {
position: d.Vec3f;
velocity: d.Vec3f;
}>>
enemy
.
species: table.Getter<d.U32>
species
}); // species is a u32
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
const Hits: table.TableDef<"Hits", {
victim: d.U32;
amount: d.F32;
}, {}>
Hits
=
import table
table
.
function define<"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
.
const u32: 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.

@example const value = u32(); // 0

@example const value = u32(7); // 7

@example const value = u32(3.14); // 3

@example const value = u32(-1); // 4294967295

@example const value = u32(-3.1); // 0

u32
,
amount: d.F32
amount
:
import d
d
.
const f32: 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.

@example const value = f32(); // 0

@example const value = f32(1.23); // 1.23

@example const value = f32(true); // 1

f32
} });
const
const hits: table.Table<table.TableDef<"Hits", {
victim: d.U32;
amount: d.F32;
}, {}>>
hits
=
import table
table
.
function create<table.TableDef<"Hits", {
victim: d.U32;
amount: d.F32;
}, {}>>(root: TgpuRoot, def: table.TableDef<"Hits", {
victim: d.U32;
amount: d.F32;
}, {}>, options: table.Options<table.TableDef<"Hits", {
victim: d.U32;
amount: d.F32;
}, {}>>): table.Table<table.TableDef<"Hits", {
victim: d.U32;
amount: d.F32;
}, {}>> (+1 overload)

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

capacity
: 256 });
const
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
enemy
=
const enemies: table.Table<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
enemies
.
Table<TableDef<"Enemy", { species: U32; }, { position: Vec3f; health: F32; }>>.gpu: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>

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
const hit: GpuMembers<table.TableDef<"Hits", {
victim: d.U32;
amount: d.F32;
}, {}>>
hit
=
const hits: table.Table<table.TableDef<"Hits", {
victim: d.U32;
amount: d.F32;
}, {}>>
hits
.
Table<TableDef<"Hits", { victim: U32; amount: F32; }, {}>>.gpu: GpuMembers<table.TableDef<"Hits", {
victim: d.U32;
amount: d.F32;
}, {}>>

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 hits: table.Table<table.TableDef<"Hits", {
victim: d.U32;
amount: d.F32;
}, {}>>
hits
.
Table<TableDef<"Hits", { victim: U32; amount: F32; }, {}>>.add(values?: table.Values<table.TableDef<"Hits", {
victim: d.U32;
amount: d.F32;
}, {}>> | undefined): table.RowId<table.TableDef<"Hits", {
victim: d.U32;
amount: d.F32;
}, {}>>

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.

add
({
victim?: number | undefined
victim
:
const id: table.RowId<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
id
,
amount?: number | undefined
amount
: 5 }); // an enemy's id, stored as a u32
// after the frame: hits.clear()
// The kernel that owns `health`:
const
const damage: TgpuComputeFn<{
gid: d.BuiltinGlobalInvocationId;
}>
damage
=
const tgpu: {
const: typeof import("node_modules/typegpu/core/constant/tgpuConstant").constant;
fn: typeof import("node_modules/typegpu/core/function/tgpuFn").fn;
comptime: typeof import("node_modules/typegpu/core/function/comptime").comptime;
resolve: typeof import("node_modules/typegpu/core/resolve/tgpuResolve").resolve;
resolveWithContext: typeof import("node_modules/typegpu/core/resolve/tgpuResolve").resolveWithContext;
init: typeof import("node_modules/typegpu/core/root/init").init;
initFromDevice: typeof import("node_modules/typegpu/core/root/init").initFromDevice;
slot: typeof import("node_modules/typegpu/core/slot/slot").slot;
lazy: typeof import("node_modules/typegpu/core/slot/lazy").lazy;
... 10 more ...;
'~unstable': typeof import("node_modules/typegpu/tgpuUnstable");
}

@module ― typegpu

tgpu
.
computeFn: <{
gid: d.BuiltinGlobalInvocationId;
}>(options: {
in: {
gid: d.BuiltinGlobalInvocationId;
};
workgroupSize: number[];
}) => TgpuComputeFnShell<{
gid: d.BuiltinGlobalInvocationId;
}> (+1 overload)
computeFn
({
workgroupSize: number[]
workgroupSize
: [64],
in: {
gid: d.BuiltinGlobalInvocationId;
}
in
: {
gid: d.BuiltinGlobalInvocationId
gid
:
import d
d
.
const builtin: {
readonly vertexIndex: d.BuiltinVertexIndex;
readonly instanceIndex: d.BuiltinInstanceIndex;
readonly clipDistances: d.BuiltinClipDistances;
readonly position: d.BuiltinPosition;
readonly frontFacing: d.BuiltinFrontFacing;
readonly fragDepth: d.BuiltinFragDepth;
readonly primitiveIndex: d.BuiltinPrimitiveIndex;
readonly sampleIndex: d.BuiltinSampleIndex;
... 11 more ...;
readonly numSubgroups: d.BuiltinNumSubgroups;
}
export builtin
builtin
.
globalInvocationId: d.BuiltinGlobalInvocationId
globalInvocationId
},
})(({
gid: d.v3u
gid
}) => {
'use gpu';
const
const i: number
i
=
gid: d.v3u
gid
.
v3u.x: number
x
;
if (!
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
enemy
.
alive: TgpuGenericFn
(i: number) => boolean

Tests whether slot i holds a row. Returns false beyond the span.

alive
(
const i: number
i
)) return;
const
const row: {
position: d.v3f;
health: number;
}
row
=
const enemy: GpuMembers<table.TableDef<"Enemy", {
species: d.U32;
}, {
position: d.Vec3f;
health: d.F32;
}>>
enemy
.
device: TgpuAccessor<table.Rows<{
position: d.Vec3f;
health: d.F32;
}>>

Provides GPU-written rows with read-write access in compute pipelines and read-only access in render pipelines.

device
.
TgpuAccessor<Rows<{ position: Vec3f; health: F32; }>>.$: {
position: d.v3f;
health: number;
}[]
$
[
const i: number
i
];
for (let
let h: number
h
=
import d
d
.
function u32(v?: number | boolean): number
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.

@example const value = u32(); // 0

@example const value = u32(7); // 7

@example const value = u32(3.14); // 3

@example const value = u32(-1); // 4294967295

@example const value = u32(-3.1); // 0

u32
(0);
let h: number
h
<
const hit: 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 (
const hit: 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
) &&
const enemy: 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
(
const hit: GpuMembers<table.TableDef<"Hits", {
victim: d.U32;
amount: d.F32;
}, {}>>
hit
.
victim: table.Getter
(i: number) => number
victim
(
let h: number
h
)) ===
const i: number
i
)
const row: {
position: d.v3f;
health: number;
}
row
.
health: number
health
-=
const hit: 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 mask slotOf(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 →
fillA fill is the resource a pipeline binds for an accessor its shaders read.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 →

Reference

kit.table

moduleHow to read this card

Callsborrows, never destroystakesreturns

table.create(root, def, { capacity, history, … }) → HistoryTable

root
the root
def
the definition its shaders were written against, from table.define
HistoryTable
a table you destroy
takes

Options

3 fields
capacity

number

Sets the table capacity for its lifetime.

defaults?

Values<T>

Sets values for fields omitted by add.Default none.

history?

boolean

Keeps last frame's rows, which code compiled with history.previously reads, such as a drawing's motion.Default false.

returns

HistoryTable

17 members
flush(target)

Uploads changes since the last flush and records into target, a frame or an encoder, the copies that keep last frame's rows.

write(id, values)

Writes values to several fields.

add(values?)

Adds a row in the lowest free slot.

capacity

number

Reports the table capacity.

live()

Returns the capacity and live test for live lists, a grid, a picker or state machines.

span

number

Reports the highest live slot plus one.

clear()

Removes every row.

destroy()

Destroys the storage buffers.

count

number

Counts live rows.

inject()

Fills the definition's own members with this table's buffers, for code written over any table of the definition, such as a kind's hooks.

gpu

GpuMembers<T>

Provides the definition's shader members bound to this table, such as reds.gpu.position(i) and reds.gpu.device.$[i].

fields

Fields<T>

Provides one handle per field.

buffers

{ host: (TgpuBuffer<Rows<HostOf<T>>> & StorageFlag) | undefined; device: (TgpuBuffer<Rows<DeviceOf<T>>> & StorageFlag) | undefined; live: TgpuBuffer<d.WgslArray<d.U32>> & StorageFlag; index: TgpuUniform<Index> }

Holds the rows; with history, a part's buffer holds last frame's rows from slot capacity.

remove(id)

Frees the row slot and makes its id stale.

has(id)

Tests whether id names a live row, without throwing for a stale one.

slotOf(id)

Returns the slot for a row id.

idAt(slot)

Returns the id of the row in slot, such as a slot read back from the GPU.

table.create(root, def, { capacity, … }) → Table

root
the root
def
the definition its shaders were written against, from table.define
Table
a table you destroy
returns

Table

17 members
capacity

number

Reports the table capacity.

count

number

Counts live rows.

span

number

Reports the highest live slot plus one.

gpu

GpuMembers<T>

Provides the definition's shader members bound to this table, such as reds.gpu.position(i) and reds.gpu.device.$[i].

inject()

Fills the definition's own members with this table's buffers, for code written over any table of the definition, such as a kind's hooks.

live()

Returns the capacity and live test for live lists, a grid, a picker or state machines.

fields

Fields<T>

Provides one handle per field.

buffers

{ host: (TgpuBuffer<Rows<HostOf<T>>> & StorageFlag) | undefined; device: (TgpuBuffer<Rows<DeviceOf<T>>> & StorageFlag) | undefined; live: TgpuBuffer<d.WgslArray<d.U32>> & StorageFlag; index: TgpuUniform<Index> }

Holds the rows; with history, a part's buffer holds last frame's rows from slot capacity.

add(values?)

Adds a row in the lowest free slot.

remove(id)

Frees the row slot and makes its id stale.

clear()

Removes every row.

has(id)

Tests whether id names a live row, without throwing for a stale one.

write(id, values)

Writes values to several fields.

slotOf(id)

Returns the slot for a row id.

idAt(slot)

Returns the id of the row in slot, such as a slot read back from the GPU.

flush(target?)

Uploads changes since the last flush with queue.writeBuffer.

destroy()

Destroys the storage buffers.

table.rows(def, part, capacity) → Rows

def
the definition, from table.define
part
'host' or 'device'
capacity
how many rows the array holds
Rows
an array schema of the part's rows

table.define(name, parts) → TableDef

name
a label for ids and WGSL
parts
the host fields the CPU writes and the device fields the GPU writes
TableDef
a definition whose getters shaders read and that table.create allocates
returns

TableDef

6 members
alive(...args)

Tests whether slot i holds a row.

span(...args)

Returns the highest live slot plus one.

slotOf(...args)

Extracts a slot from a row id, such as an id stored in a host field.

host

TgpuAccessor<Rows<H>>

Provides CPU-written rows with read-only shader access.

device

TgpuAccessor<Rows<D>>

Provides GPU-written rows with read-write access in compute pipelines and read-only access in render pipelines.

row(...args)

Returns a copy of the whole row, including every field, for helpers that take a row value.

In a frame

records

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.

Holds

owns

3 or 4 buffers

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.

lifecycle

destroy()[Symbol.dispose]

binds
  • 1 storage

    A field, per part, host or device, such as Boid.position(i); row(i) binds both parts.

  • 1 storage · 1 uniform

    alive(i), the live bits and the span; span() and slotOf(id) bind only the uniform.

Connects to

injects
  • definition

    Its definition's rows, live bits and span, for code written over any table of the definition; each table's gpu members carry its own.

Exports

types
Props Rows RowId Getter Getters AnyTableDef RowOf FieldInput Values HostField DeviceField Fields Live

used byBlockfallCloisterFish tankHex isleJelly worldNight marketPlatformerRobot arm