A table keeps in stable , leaving holes when rows are removed. Drawing over every slot still does vertex work for those holes. With half the slots dead, the pass takes 1.5 to 2 times as long.
Live lists list each group’s live slots on the GPU every and write each group’s count into an , so the CPU never counts them.
Table slots grouped by kind into indirect draws
Try Click a slot to change its kind or free it. Each kind starts at a fixed offset, encoded in separate pipelines or supplied through firstInstance so one pipeline can draw every kind.
import{
import liveLists
liveLists}from'@typegpu/kit';
const
constBlock:table.TableDef<"Block",{
shape:d.U32;
position:d.Vec3f;
},{}>
Block=
import table
table.
functiondefine<"Block",{
shape:d.U32;
position:d.Vec3f;
},{}>(name:"Block",parts:{
host?:{
shape:d.U32;
position:d.Vec3f;
}|undefined;
device?:{}|undefined;
}):table.TableDef<"Block",{
shape:d.U32;
position:d.Vec3f;
},{}>
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('Block',{
host?:{
shape:d.U32;
position: d.Vec3f;
}|undefined
host:{
shape:d.U32
shape:
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.
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,
constBlock:table.TableDef<"Block",{
shape:d.U32;
position:d.Vec3f;
},{}>
Block,{
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.
Creates a list of live slots and an indirect draw per group. Each group reserves capacity
slots. A shader finds rows with live.slot(group, i) using a captured group index. With
firstInstance, it can use live.instanceSlot(instanceIndex) instead. Throws if the groups,
draws or workgroup size are invalid, the slot buffer exceeds maxStorageBufferBindingSize,
compact over the capacity needs more than maxComputeWorkgroupsPerDimension workgroups, or
firstInstance lacks its required feature.
@param ― options.alive whether a slot holds a live row, carrying the fills of what it reads, such
as a table's from rows.live(), spread into the options with its capacity
@param ― options.draws one draw per group
@returns ― live lists you run every frame and destroy
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.
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();
const
constcompute:TgpuComputePass
compute=
frame:frames.Frame
frame.
Frame.encoder: TgpuCommandEncoder
Records commands for submission after the recorder returns. submit() replaces this encoder.
Read it at each call instead of keeping it, and let the frame submit it.
Rewrites the live slots and instance counts. Record it after the table's flush.
compact(
constcompute:TgpuComputePass
compute);// each group's live slots, then its instance count
constcompute:TgpuComputePass
compute.
TgpuComputePass.end(): void
Completes the recording of this compute pass
end();
const
constpass:TgpuRenderPass
pass=
frame:frames.Frame
frame.
Frame.encoder: TgpuCommandEncoder
Records commands for submission after the recorder returns. submit() replaces this encoder.
Read it at each call instead of keeping it, and let the frame submit it.
Draws group into the pass with the pipeline set on it. Uses an indexed draw when the group's
draw is indexed.
draw(
constpass:TgpuRenderPass
pass,
constSOLID:0
SOLID);// drawIndirect, or drawIndexedIndirect for an indexed draw
constpass:TgpuRenderPass
pass.
TgpuRenderPass.end(): void
Completes the recording of this render pass
end();
});
The vertex shader reads its row’s slot with live.slot(SOLID, instanceIndex), in any pipeline. The pipeline captures SOLID as a constant, including the start of that group’s list, so the draw keeps firstInstance at 0. This works on every device and requires one pipeline per group.
Grouping Group by mesh or pipeline, such as fish species. Rows that share both stay one group and branch on a field, as the platformer’s walkers and bats do.
Default group Without group, every live row is in group 0; rows whose group is out of range aren’t drawn.
Swapping a drawsetDraw(group, draw) swaps a group’s draw, such as for a new mesh, and keeps its count.
Table rowsblocks.live() returns the ’s capacity, its gpu.alive and its span. Without alive, every slot is live, and the types refuse a span.
Your own testalive can be any shader function. (i) => block.alive(i) && block.shape(i) !== HIDDEN, with block = blocks.gpu, selects some table rows. Particles test their ages. The neighbour grid takes its alive, position and itemOf the same way.
Compactingcompact tests every slot up to the span it reads as it records, or capacity without one. A row added after compact records shows from the next frame.
Recording orderdraw and dispatch throw until a compact or finish has been recorded. A render bundle can record its draws before that.
Cost Drawing through the list costs the same as drawing dense rows.
With firstInstance: true, each group’s draw starts at its own region. The shader finds its row from the instance index alone. One pipeline draws every group, so adding a group compiles no pipeline.
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,
constBlock:table.TableDef<"Block",{
shape:d.U32;
position:d.Vec3f;
},{}>
Block,{
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.
Creates a list of live slots and an indirect draw per group. Each group reserves capacity
slots. A shader finds rows with live.slot(group, i) using a captured group index. With
firstInstance, it can use live.instanceSlot(instanceIndex) instead. Throws if the groups,
draws or workgroup size are invalid, the slot buffer exceeds maxStorageBufferBindingSize,
compact over the capacity needs more than maxComputeWorkgroupsPerDimension workgroups, or
firstInstance lacks its required feature.
@param ― options.alive whether a slot holds a live row, carrying the fills of what it reads, such
as a table's from rows.live(), spread into the options with its capacity
@param ― options.draws one draw per group
@returns ― live lists you run every frame and destroy
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 one draw per group. Requires 1 to 256 draws. Default an empty draw for each of
groups, for lists that only dispatch.
draws:[{
indexCount: number
indexCount:36},{
indexCount: number
indexCount:240,
firstIndex?: number |undefined
firstIndex:36,
baseVertex?: number |undefined
baseVertex:24}],
ListOptions.firstInstance?: boolean |undefined
Uses firstInstance to start each draw at its group's region. Shaders can then find rows with
live.instanceSlot(instanceIndex), and one pipeline can draw every group. Throws without the
device feature indirect-first-instance, because those draws otherwise draw nothing without
reporting an error. Default false.
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.
position:table.Getter
(i:number)=>d.v3f
position(
constslot:number
slot),1)};
});
Set the pipeline on the pass once, then draw each group with live.draw(pass, 0) and live.draw(pass, 1). create throws without indirect-first-instance because these indirect draws otherwise produce nothing without an error. Check root.device.features and fall back to a pipeline per group when the feature is absent.
compact also writes indirect arguments for each group’s live rows. live.dispatch(pass, group) dispatches the pipeline set on the pass over the group’s rows, in of workgroupSize , 64 by default. Its threads return at or beyond live.count(group), the group’s live row count, and find their rows with live.slot(group, i). An update to the walkers then skips holes and bats:
Returns the live row count for group in a shader. Kernels run by
dispatch must return for threads at or beyond this count. Pass group as a captured
constant.
count(
constWALKERS:1
WALKERS))return;
const
constslot:number
slot=
constlive: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(
constWALKERS:1
WALKERS,
gid:d.v3u
gid.
v3u.x: number
x);
// … move Block slot `slot`
});
const
constwalking:TgpuComputePipeline
walking=
constroot:TgpuRoot
root.
WithBinding.createComputePipeline<{
gid:d.BuiltinGlobalInvocationId;
}>(descriptor:TgpuComputePipeline.Descriptor<{
gid:d.BuiltinGlobalInvocationId;
}>):TgpuComputePipeline
createComputePipeline({
compute:TgpuComputeFn<{
gid:d.BuiltinGlobalInvocationId;
}>
compute:
constwalk:TgpuComputeFn<{
gid:d.BuiltinGlobalInvocationId;
}>
walk});
constlive:liveLists.LiveLists
live.
LiveLists.compact(pass: TgpuComputePass): void
Rewrites the live slots and instance counts. Record it after the table's flush.
Dispatches the pipeline set on the compute pass over the live rows of group. Record it after
compact. Dispatches enough workgroups of workgroupSize for count(group) threads. Each
thread finds its row with slot(group, i).
dispatch(
constcompute:TgpuComputePass
compute,
constWALKERS:1
WALKERS);
Match the ’s workgroupSize to the lists’ and pass the group as a captured constant.
A kernel that already visits every row, such as a simulation step, can list rows as it goes with live.list(group, i) and skip the separate pass of compact. Every thread of a workgroup must call it once, outside any branch, because the workgroup lists together. A row it doesn’t list passes a group of groups or above. Record live.finish(pass) after the kernel to write the counts. Machines list rows by state this way. Without draws, groups sets the number of groups for lists that only dispatch:
Creates a list of live slots and an indirect draw per group. Each group reserves capacity
slots. A shader finds rows with live.slot(group, i) using a captured group index. With
firstInstance, it can use live.instanceSlot(instanceIndex) instead. Throws if the groups,
draws or workgroup size are invalid, the slot buffer exceeds maxStorageBufferBindingSize,
compact over the capacity needs more than maxComputeWorkgroupsPerDimension workgroups, or
firstInstance lacks its required feature.
@param ― options.alive whether a slot holds a live row, carrying the fills of what it reads, such
as a table's from rows.live(), spread into the options with its capacity
@param ― options.draws one draw per group
@returns ― live lists you run every frame and destroy
create(
constroot:TgpuRoot
root,{
ListOptions.capacity: number
Sets the number of slots, including dead slots. Indices range from 0 to capacity - 1.
capacity:
constCOUNT:4096
COUNT,
ListOptions.groups?: number |undefined
Sets the number of groups, from 1 to 256. With draws, it must equal their number. Default
the number of draws, or 1.
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(1);// not listed
if(
consti:number
i<
constCOUNT:4096
COUNT&&
functionheat(i:number):number
heat(
consti:number
i)>0.5)
let group:number
group=0;
constburning:liveLists.LiveLists
burning.
LiveLists.list: (group:number,i:number)=>void
Lists row i in group from a kernel that already visits the rows, in place of compact.
Every invocation of the workgroup must call it once, outside any branch, with a group of
groups or above for a row it doesn't list, because it synchronises the workgroup. Record
finish after the kernel.
Writes the counts of the rows listed with list into the draws and dispatches, and empties
the lists for the next listing. Record it after the kernels that list.
At most 256 groups are allowed. The slot list uses groups × capacity × 4 bytes and must fit one storage , 128 MiB unless the device requests a higher limit. create throws if either limit is exceeded.
compact runs a thread a slot, 256 to a workgroup, so create throws past 256 times the device’s maxComputeWorkgroupsPerDimension. dispatch runs a thread a listed row; past that many workgroups, the device skips it.
The order within a group changes between frames, so overlapping see-through instances may shimmer. Draw see-through groups in a stable order of your own.
rowA row is one object's values in a buffer, such as one enemy, one particle or one light.Objects are rows in buffers →
slotA slot is a row's place in its buffers, which a new row can take once the first is removed.Row ids →
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 →
instanceAn instance is one copy of a mesh with its own position, rotation, scale and material, kept as a row of a table.Instances →
indirect drawAn indirect draw reads how much to draw from a buffer, so a kernel can decide the count and the CPU never reads it.Indirect draws →
tableA table is a buffer of rows with named fields, some written by the CPU and the others by kernels.Rows →
dispatchA dispatch runs a kernel once over a number of threads, inside a compute pass.Words →
workgroupA workgroup is a block of threads that the GPU starts together, and a dispatch runs as many workgroups as cover the work.Slime mould →
threadA thread is one run of a kernel, and the GPU starts many at once, each with its own number.Slime mould →
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 →
bindingA binding is one buffer, texture or sampler in a bind group, which a shader reads by its place there.Words →