Log inGet started

substrate

Updated November 24, 2026

Typed data buffers, on the CPU or the GPU — the engine's one buffer primitive.

require("@builtin::modules.api.engine.substrate") -- substrate (also available as global 'substrate')

The substrate namespace: 48 functions.

globals/substrate/bindFrames

substrate.bindFrames(key: string, id: string, slots: { any }) -> (boolean, string?)

Bind the play key to the sequence id through slots, so showFrame(key, f) places its elements from frame f. slots[s] is { kind = "instances", key, transforms, data?, capacity, draw, interval? } for elements an instanced draw copies from transforms — the scatter binding key places up to capacity of them there and the draw draw draws the frame's count — or { kind = "entities", key, binding?, interval? } for elements standing in entities: the scatter binding key, whose entities scatterTransforms has already set, places them, and the frame's fields are written across the entity binding binding.

Parameters

  • key string — The play's name.
  • id string — The sequence the play shows.
  • slots { any } — What each numbered slot places, for this play.

Returns (boolean, string?) — True when the binding was queued, and why it was refused when not.

substrate.bindFrames("spark#1", "spark", { { kind = "instances", key = "spark#1", transforms = "spark#1.xf", capacity = 64, draw = drawId } })

globals/substrate/bufferSource

substrate.bufferSource(name: string) -> string?

The chunk that created the GPU buffer name holds, as debug.info names a chunk — a module's identity, a test suite's, an execute body's.

The record is the name's for as long as a buffer holds the name, across a device rebuilt underneath it. A name created again after its buffer went answers for the call that created it again. A buffer the engine made itself, or one made through another surface, has no record.

Parameters

  • name string — The name the buffer was created under.

Returns string? — The creating chunk's name, or nil.

if substrate.bufferSource("debug_viz.vtx") then print("made through the substrate") end

globals/substrate/clearScatterTransforms

substrate.clearScatterTransforms(key: string) -> boolean

Drop the device transform binding under key. Its entities go back to being placed by whatever writes their Transform. Queued.

Parameters

  • key string — The key the binding was made under.

Returns boolean — True when the drop was queued.

substrate.clearScatterTransforms("wave")

globals/substrate/compactions

substrate.compactions() -> number

How many times a page group has moved runs into a page made for them: a group whose runs stood still for a few frames, with more room in its pages than an eighth of what its runs hold, repacks the runs of its emptiest pages.

A run keeps its name when it moves, and substrate.placement(name) answers the page and offset it stands at now. Work that recorded where runs stood (a dispatch that bound a page whole and addressed runs by offset) reads this before it runs: when the count moved, the runs it recorded may stand elsewhere.

Returns number — The count since the engine started; 0 where the engine thread's state is not reachable (a worker thread).

local seen = substrate.compactions()

globals/substrate/createBuffer

substrate.createBuffer(opts: BufferOpts) -> TypedBuffer?

Allocate a typed buffer and return its handle.

A "gpu" buffer is storage a compute shader binds; usage adds "vertex", "index", "indirect" or "readback" on top of the storage it always has. A "cpu" buffer is storage the engine holds outside the Luau heap, and reads back as a flat array of floats. Reading its bytes into Luau (:bytes(), :read():resultBytes()) makes a copy in the Luau heap.

The handle's write answers whether the words landed: a payload whose end falls past the end of the buffer is refused whole on both kinds, so the buffer keeps what it held and the call answers false. writeU32 and writeBytes answer the same way, against the same extent.

A "cpu" buffer holds at most 1 GiB of storage: 268435456 f32 words, 16777216 mat4 records. A len past that raises an error naming the bytes it needs and the limit, and a len within it whose storage the heap cannot supply raises an error naming the bytes. Neither allocates anything.

A "gpu" buffer is held by what made it. One created under a name stays until substrate.destroyBuffer(name) frees it, and a run created for an owner stays until substrate.releaseOwner(owner). One created with neither is held by its handles: it goes once Luau collects the last of them, so the buffers a script made and can no longer reach (it ended, or raised) go with it. Its bytes pace the collector the way a Luau value of that size would.

A "gpu" buffer holds at most what the device creates in one buffer, its max_buffer_size. A len whose bytes pass it raises an error naming the records, the bytes they need, that limit and the most records of the type one buffer holds; nothing is allocated and the name stays free.

class makes a "gpu" buffer a run: it stands in a range of a page the engine's device arenas share among many runs of the class, rather than in an allocation of its own, so thousands of short-lived buffers cost a handful of device allocations. "effect" is for runs made and let go of as an effect plays, "persistent" for runs held until let go of. A run binds, writes, reads and frees like any "gpu" buffer under the name its handle carries, which the engine mints, so a run takes no name; a compute program declared with ranges = true binds any number of runs of one page through one bind group. A run carries "readback" usage and no other: a vertex, index or indirect buffer is one of its own. owner names whom a run is held for, and substrate.releaseOwner(owner) lets go of every run created with it at once.

A job on a worker thread makes a run the same way. The handle answers at once, with the type and length asked for and a name the worker mints; substrate.listBuffers lists the run at once; and the engine thread places it in its arenas ahead of every write, dispatch and read the job makes over it afterwards, so one job can make a run, fill it, dispatch over it and read it back. A class, usage or len the engine thread refuses raises at the call in the job too. When the engine thread cannot place the run, the handle's next method call raises why. substrate.setOwner and substrate.releaseOwner answer in a job as they do on the engine thread, counting the job's own changes at once.

Parameters

  • opts BufferOpts — { type, len, kind?, usage?, name?, class?, group?, owner? } — type is "f32", "vec3", "vec4", "quat" or "mat4"; kind is "cpu" (the default) or "gpu". name is the name a dispatch binds a "gpu" buffer by, and the name substrate.getBuffer and substrate.destroyBuffer reach it under. class ("effect" or "persistent") makes a "gpu" buffer a run; owner names whom the run is held for.

Returns TypedBuffer? — The buffer handle, or nil when the allocation failed — an unknown type or kind, a zero length, or a name that already holds a GPU buffer of another shape. A name holding a buffer of the SAME type and length hands that buffer back, contents and all; substrate.destroyBuffer frees a name whose buffer is the wrong shape.

local pose = substrate.createBuffer({ type = "mat4", len = boneCount })
local field = substrate.createBuffer({ type = "vec3", len = 4096, kind = "gpu" })
local column = substrate.createBuffer({ type = "f32", len = n, kind = "gpu", class = "effect", owner = "burst:" .. id })
local values = pose:read(0, 16):result()

globals/substrate/defineFrames

substrate.defineFrames(id: string, frames: { any }) -> (boolean, string?)

Hold a sequence of frames on the engine under id, for plays bound to it (bindFrames) to show by index (showFrame) with no script running per frame. frames[f] is { scatters = {...}, fields = {...} }. A scatter { slot, positions, rotations?, scales?, lanes?, identities?, count } names the buffers the slot's elements are placed from in that frame and how many elements they hold; lanes[k] is a buffer name or false. A field { slot, component, field, values } gives the value the field takes on each of the slot's entities, in binding order. Slots are numbered from 1. The buffers stay the caller's and must stand for as long as the sequence is held. Defining id again replaces it.

Parameters

  • id string — The sequence's name.
  • frames { any } — The frames, in order.

Returns (boolean, string?) — True when the sequence was queued, and why it was refused when not.

substrate.defineFrames("spark", { { scatters = { { slot = 1, positions = P:bufferName(), count = 64 } } } })

globals/substrate/destroyBuffer

substrate.destroyBuffer(name: string) -> boolean

Free the GPU buffer name denotes, whatever else still holds a handle to it.

The allocation goes and the name is free to be created again at any type and length; every handle that pointed at it answers :alive() false. This is what releases a name whose creating handle is gone, so a build that re-runs at a different size gets its name back.

Parameters

  • name string — The name the buffer was created under.

Returns boolean — True when a GPU buffer under that name was freed.

substrate.destroyBuffer("env.town.xf")

globals/substrate/forgetFrames

substrate.forgetFrames(id: string) -> boolean

Drop the sequence held under id. A play still bound to it shows nothing further.

Parameters

  • id string — The sequence's name.

Returns boolean — True when the drop was queued.

substrate.forgetFrames("spark")

globals/substrate/getBuffer

substrate.getBuffer(name: string) -> TypedBuffer?

The GPU buffer name denotes, as a handle you now hold.

A name is how a dispatch binds a buffer, so the name is what an owner asks by once the handle it created with has gone out of scope — a .module that hot-reloaded, a build that ran in an earlier execute. The handle carries everything createBuffer's does and releases its reference with :destroy().

Parameters

  • name string — The name the buffer was created under.

Returns TypedBuffer? — The buffer handle, or nil when no GPU buffer holds that name.

local xf = substrate.getBuffer("env.town.xf")
local shape = xf and { xf:type(), xf:length() }

globals/substrate/gpuReadback

substrate.gpuReadback(key: string?) -> Readback?

Wrap the key an FFI read handed back as the Readback that polls it. Every GPU→CPU read reaches the caller through this, so a texture's read and a buffer's read answer with the same thing.

Parameters

  • key string (optional) — The key the read returned.

Returns Readback? — The Readback, or nil when the read did not start.

local pending = substrate.gpuReadback(compute.readTexture3D(handle))

globals/substrate/listBuffers

substrate.listBuffers() -> { NamedBuffer }

Every named GPU buffer the engine holds, in name order.

Each record states id, name, type ("F32", "Vec3", "Vec4", "Quat", "Mat4"), len in records, refs — how many holders it has — holder, the owner a run is held for when one was named (createBuffer's owner), class, the lifetime class of a run ("effect" or "persistent"; absent for a buffer of its own), and bytes, the device bytes it takes: the range a run stands in, or the extent of a buffer of its own. This is what states which names are taken, at what shape, and what an owner holds.

Returns { NamedBuffer } — Array of { id, name, type, len, refs, holder?, class?, bytes }.

for _, b in ipairs(substrate.listBuffers()) do print(b.name, b.type, b.len) end

globals/substrate/losses

substrate.losses() -> number

How many times a buffer has been freed by name or by owner: destroyBuffer, releaseOwner and the reset to a baseline. A holder that checked that the buffers it names exist at one count knows they still do while the count is the same, whatever else it asks.

Returns number — The count, which only grows.

local at = substrate.losses()

globals/substrate/markBaseline

substrate.markBaseline() -> number

Record which named GPU buffers the engine holds right now, and make that the point substrate.resetToBaseline returns to.

A caller running work in an engine it shares — a sweep of test suites, a tool driving a session — leaves buffers behind under the names the work chose: a view's geometry built on its first capture, a kernel's storage. This is how such a caller says where its own allocations start.

Returns number — The mark's generation, counting from 1 — what substrate.resetToBaseline takes to restore to this mark.

local mark = substrate.markBaseline() -- then run, then substrate.resetToBaseline(mark)

globals/substrate/mintName

substrate.mintName(prefix: string) -> string

prefix followed by a number no VM of this engine was handed before. The engine thread and every worker VM draw from one count, so a name minted in one VM is never one another VM minted: what names a buffer, a run's owner or anything else found by name when several VMs make them at once.

Parameters

  • prefix string — What the name starts with.

Returns string — The prefix and a number of its own.

local name = substrate.mintName("cook.points.")

globals/substrate/placement

substrate.placement(name: string) -> BufferPlacement?

Where the GPU buffer name names stands on the device.

A run stands in a range of an arena page that many runs share: page is the buffer a dispatch binds to reach it, offset the byte it starts at in that buffer (a multiple of 256) and bytes the bytes it holds. A buffer of its own is its own page at offset 0. A kernel that binds a page once and addresses every run of it by offset reads this to state each run's base. rangeBytes is the bytes the range takes in the page, pageBytes the bytes the page holds on the device and pageLiveBytes the bytes standing under the page's live ranges: a set of runs keeps its pages alive by the share of pageLiveBytes the set holds. A job on a worker thread is answered by the engine thread, after it has placed every run the job created before the call.

Parameters

  • name string — The name the buffer binds under (its handle's name).

Returns BufferPlacement? — { page, offset, bytes, rangeBytes, pageBytes, pageLiveBytes }, or nil when no buffer holds the name.

local at = substrate.placement(column.name)

globals/substrate/releaseBaseline

substrate.releaseBaseline(generation: number) -> boolean

Drop the mark generation names, and every mark taken after it, once the caller that took it is done resetting to it. It touches the marks alone.

Parameters

  • generation number — The mark to drop, as substrate.markBaseline returned it.

Returns boolean — True when the mark stood.

substrate.releaseBaseline(mark) -- the run is over

globals/substrate/releaseOwner

substrate.releaseOwner(owner: string) -> number

Let go of every run created with owner (createBuffer's class and owner), whatever else still holds a handle to it.

Each run's range goes back to its arena and every handle to it answers :alive() false. This is what something that makes runs as it plays — an effect, a cook, a cache — calls when it ends, so everything it made goes with it at once, runs it handed on to others included.

Parameters

  • owner string — The owner the runs were created with.

Returns number — How many runs were let go of.

substrate.releaseOwner("burst:" .. id)

globals/substrate/resetToBaseline

substrate.resetToBaseline(generation: number?) -> number

Free every named GPU buffer created since substrate.markBaseline, leaving the ones it recorded and buffers owned by standing render features where they are. Restore the renderer baseline first to release nonbaseline features.

A module that created one of these holds its own handle to it. Put that module's state back alongside this (engine.restoreModuleState) or it goes on addressing storage this freed. is touched.

Parameters

  • generation number (optional) — The mark to restore to, as substrate.markBaseline returned it; the newest mark when omitted. The marks taken after it go with the reset.

Returns number — How many buffers were freed.

local freed = substrate.resetToBaseline(mark)

globals/substrate/scatterTransforms

substrate.scatterTransforms(key: string, entityIds: { string }?, positions: string, rotations: string?, scales: string?, lanes: { string? }?, interval: number?, identities: string?) -> boolean

Place entities from the buffers that already hold their transform, on the device, instead of from a Transform written per entity.

entityIds[i] is placed from row i of every named buffer, so the order is the buffers' own. Pass nil for entityIds to keep the entities a binding already has and replace only the buffers — what a re-cook does, since a field chain mints a fresh run each time while its membership stands still, and sending the element list again each time would put back the per-element crossing the binding exists to remove.

Re-binding the same key with a list replaces the previous binding. Queued: it takes effect on the next frame, and an entity whose renderable has not landed yet is placed on the frame it does.

Parameters

  • key string — Stable identity of the binding.
  • entityIds { string } (optional) — The entities in buffer order, or nil to rebind the buffers alone.
  • positions string — Name of a buffer holding three f32 per element.
  • rotations string (optional) — Name of a buffer holding four f32 per element.
  • scales string (optional) — Name of a buffer holding three f32 per element.
  • lanes { string? } (optional) — Per-instance shader data, by lane: lanes[k + 1] names a buffer holding one vec4 per element, written into lane k of each element's block — what its material reads back as input.shader_data[k]. A lane left out is left to whatever else writes it.
  • interval number (optional) — Seconds between the presents of the domain the buffers hold. Given, every frame draws each element between the last two presents — position, rotation, scale and lanes blended — the way a fixed-rate simulation is drawn between its ticks, so its motion is continuous at any frame rate while the domain presents at its own. An element new to a slot starts where it was presented. Omitted or 0, each present is drawn as it lands.
  • identities string (optional) — Name of a buffer holding one f32 per element naming which element stands in that row at each present — for a domain whose rows are taken by different elements over time, as a particle's slot is when it dies and another is born into it. A row whose identity moved since the last present starts where it was presented rather than blending from the element that held it before. -1 names a row no element stands in. Omitted, each row's element is the entity placed from it.

Returns boolean — True when the binding was queued.

substrate.scatterTransforms("wave", ids, positions:bufferName())
substrate.scatterTransforms("wave", nil, positions:bufferName())
substrate.scatterTransforms("wave", nil, positions:bufferName(), nil, nil, { colors:bufferName() })
substrate.scatterTransforms("sparks", ids, positions:bufferName(), nil, nil, nil, 1 / 30)
substrate.scatterTransforms("sparks", nil, positions:bufferName(), nil, scales:bufferName(), nil, 1 / 30, ids:bufferName())

globals/substrate/scatterTransformsInto

substrate.scatterTransformsInto(key: string, into: { transforms: string, data: string?, count: number }, positions: string, rotations: string?, scales: string?, lanes: { string? }?, interval: number?, identities: string?) -> boolean

Place elements that stand in no entity from the substrate buffers that hold their transform, on the device: row i of the columns is composed into matrix i of into.transforms, and lane k of block i of into.data — the two buffers an instanced draw (renderer.mesh.drawInstanced with transformBuffer and instanceDataBuffer) copies into its slots every frame. It is scatterTransforms for a population: the same columns, the same blend between presents when interval is given, the same identities. Binding the same key into the same buffers again replaces only the columns it reads. Queued: it takes effect on the next frame, ahead of the instanced copies that frame records.

Parameters

  • key string — Stable identity of the binding.
  • into { transforms: string, data: string?, count: number } — { transforms, data?, count }: a buffer of count column-major mat4, a buffer of count 64-byte blocks of four vec4 lanes when lanes are bound, and how many elements there are.
  • positions string — Name of a buffer holding three f32 per element.
  • rotations string (optional) — Name of a buffer holding four f32 per element.
  • scales string (optional) — Name of a buffer holding three f32 per element.
  • lanes { string? } (optional) — lanes[k + 1] names a buffer holding one vec4 per element, written into lane k of each element's block in into.data.
  • interval number (optional) — Seconds between the domain's presents; given, each element is drawn between the last two presents.
  • identities string (optional) — Name of a buffer holding one f32 per element naming which element stands in that row at each present; -1 names a row no element stands in.

Returns boolean — True when the binding was queued.

substrate.scatterTransformsInto("sparks", { transforms = "sparks.xf", count = 4096 }, P:bufferName())

globals/substrate/scatterWorlds

substrate.scatterWorlds(key: string) -> any

Read back where the device drew the elements of the transform binding under key in the last frame drawn — the device's own answer, since the CPU never learns where an element it places stands. 16 floats per placed element, column-major world matrices, in the binding's element order, leaving out elements whose renderable has not landed. A binding drawn between its presents answers the blended pose that frame drew.

Parameters

  • key string — The key the binding was made under.

Returns any — A pending readback: :ready(), then :result() for the floats.

local pending = substrate.scatterWorlds("sparks")
while not pending:ready() do task.wait() end
local x = pending:result()[13]

globals/substrate/setOwner

substrate.setOwner(name: string, owner: string?) -> boolean

Hold the run name names for owner from now on, in place of the owner it was created with, or for no one when owner is nil.

substrate.releaseOwner(owner) then lets go of it with the rest of what owner holds, and releasing the owner it was made for leaves it standing. What hands a run on to whatever keeps it longer than the one that made it — a cache several makers read from.

Parameters

  • name string — The name the run binds under (its handle's name).
  • owner string (optional) — The owner to hold it for, or nil.

Returns boolean — Whether name names a run (a buffer created with a class).

substrate.setOwner(column.name, "cache#3")

globals/substrate/showFrame

substrate.showFrame(key: string, frame: number) -> boolean

Show frame frame (from 1) of the sequence the play key is bound to: each slot is placed from that frame's buffers, each instanced draw draws that frame's count, and that frame's fields are written. Showing the frame already shown changes nothing. Queued.

Parameters

  • key string — The play's name.
  • frame number — The frame, from 1.

Returns boolean — True when the frame was queued.

substrate.showFrame("spark#1", 12)

globals/substrate/unbindFrames

substrate.unbindFrames(key: string) -> boolean

Drop the play key's binding to its sequence. What it placed stays as the last frame shown left it.

Parameters

  • key string — The play's name.

Returns boolean — True when the drop was queued.

substrate.unbindFrames("spark#1")

typed/builtin//modules/api/engine/substrate/substrate/bindFrames

substrate.bindFrames(key: string, id: string, slots: { any }) -> (boolean, string?)

Bind the play key to the sequence id through slots, so showFrame(key, f) places its elements from frame f. slots[s] is { kind = "instances", key, transforms, data?, capacity, draw, interval? } for elements an instanced draw copies from transforms — the scatter binding key places up to capacity of them there and the draw draw draws the frame's count — or { kind = "entities", key, binding?, interval? } for elements standing in entities: the scatter binding key, whose entities scatterTransforms has already set, places them, and the frame's fields are written across the entity binding binding.

Parameters

  • key string — The play's name.
  • id string — The sequence the play shows.
  • slots { any } — What each numbered slot places, for this play.

Returns (boolean, string?) — True when the binding was queued, and why it was refused when not.

substrate.bindFrames("spark#1", "spark", { { kind = "instances", key = "spark#1", transforms = "spark#1.xf", capacity = 64, draw = drawId } })

typed/builtin//modules/api/engine/substrate/substrate/bufferSource

substrate.bufferSource(name: string) -> string?

The chunk that created the GPU buffer name holds, as debug.info names a chunk — a module's identity, a test suite's, an execute body's.

The record is the name's for as long as a buffer holds the name, across a device rebuilt underneath it. A name created again after its buffer went answers for the call that created it again. A buffer the engine made itself, or one made through another surface, has no record.

Parameters

  • name string — The name the buffer was created under.

Returns string? — The creating chunk's name, or nil.

if substrate.bufferSource("debug_viz.vtx") then print("made through the substrate") end

typed/builtin//modules/api/engine/substrate/substrate/clearScatterTransforms

substrate.clearScatterTransforms(key: string) -> boolean

Drop the device transform binding under key. Its entities go back to being placed by whatever writes their Transform. Queued.

Parameters

  • key string — The key the binding was made under.

Returns boolean — True when the drop was queued.

substrate.clearScatterTransforms("wave")

typed/builtin//modules/api/engine/substrate/substrate/compactions

substrate.compactions() -> number

How many times a page group has moved runs into a page made for them: a group whose runs stood still for a few frames, with more room in its pages than an eighth of what its runs hold, repacks the runs of its emptiest pages.

A run keeps its name when it moves, and substrate.placement(name) answers the page and offset it stands at now. Work that recorded where runs stood (a dispatch that bound a page whole and addressed runs by offset) reads this before it runs: when the count moved, the runs it recorded may stand elsewhere.

Returns number — The count since the engine started; 0 where the engine thread's state is not reachable (a worker thread).

local seen = substrate.compactions()

typed/builtin//modules/api/engine/substrate/substrate/createBuffer

substrate.createBuffer(opts: BufferOpts) -> TypedBuffer?

Allocate a typed buffer and return its handle.

A "gpu" buffer is storage a compute shader binds; usage adds "vertex", "index", "indirect" or "readback" on top of the storage it always has. A "cpu" buffer is storage the engine holds outside the Luau heap, and reads back as a flat array of floats. Reading its bytes into Luau (:bytes(), :read():resultBytes()) makes a copy in the Luau heap.

The handle's write answers whether the words landed: a payload whose end falls past the end of the buffer is refused whole on both kinds, so the buffer keeps what it held and the call answers false. writeU32 and writeBytes answer the same way, against the same extent.

A "cpu" buffer holds at most 1 GiB of storage: 268435456 f32 words, 16777216 mat4 records. A len past that raises an error naming the bytes it needs and the limit, and a len within it whose storage the heap cannot supply raises an error naming the bytes. Neither allocates anything.

A "gpu" buffer is held by what made it. One created under a name stays until substrate.destroyBuffer(name) frees it, and a run created for an owner stays until substrate.releaseOwner(owner). One created with neither is held by its handles: it goes once Luau collects the last of them, so the buffers a script made and can no longer reach (it ended, or raised) go with it. Its bytes pace the collector the way a Luau value of that size would.

A "gpu" buffer holds at most what the device creates in one buffer, its max_buffer_size. A len whose bytes pass it raises an error naming the records, the bytes they need, that limit and the most records of the type one buffer holds; nothing is allocated and the name stays free.

class makes a "gpu" buffer a run: it stands in a range of a page the engine's device arenas share among many runs of the class, rather than in an allocation of its own, so thousands of short-lived buffers cost a handful of device allocations. "effect" is for runs made and let go of as an effect plays, "persistent" for runs held until let go of. A run binds, writes, reads and frees like any "gpu" buffer under the name its handle carries, which the engine mints, so a run takes no name; a compute program declared with ranges = true binds any number of runs of one page through one bind group. A run carries "readback" usage and no other: a vertex, index or indirect buffer is one of its own. owner names whom a run is held for, and substrate.releaseOwner(owner) lets go of every run created with it at once.

A job on a worker thread makes a run the same way. The handle answers at once, with the type and length asked for and a name the worker mints; substrate.listBuffers lists the run at once; and the engine thread places it in its arenas ahead of every write, dispatch and read the job makes over it afterwards, so one job can make a run, fill it, dispatch over it and read it back. A class, usage or len the engine thread refuses raises at the call in the job too. When the engine thread cannot place the run, the handle's next method call raises why. substrate.setOwner and substrate.releaseOwner answer in a job as they do on the engine thread, counting the job's own changes at once.

Parameters

  • opts BufferOpts — { type, len, kind?, usage?, name?, class?, group?, owner? } — type is "f32", "vec3", "vec4", "quat" or "mat4"; kind is "cpu" (the default) or "gpu". name is the name a dispatch binds a "gpu" buffer by, and the name substrate.getBuffer and substrate.destroyBuffer reach it under. class ("effect" or "persistent") makes a "gpu" buffer a run; owner names whom the run is held for.

Returns TypedBuffer? — The buffer handle, or nil when the allocation failed — an unknown type or kind, a zero length, or a name that already holds a GPU buffer of another shape. A name holding a buffer of the SAME type and length hands that buffer back, contents and all; substrate.destroyBuffer frees a name whose buffer is the wrong shape.

local pose = substrate.createBuffer({ type = "mat4", len = boneCount })
local field = substrate.createBuffer({ type = "vec3", len = 4096, kind = "gpu" })
local column = substrate.createBuffer({ type = "f32", len = n, kind = "gpu", class = "effect", owner = "burst:" .. id })
local values = pose:read(0, 16):result()

typed/builtin//modules/api/engine/substrate/substrate/defineFrames

substrate.defineFrames(id: string, frames: { any }) -> (boolean, string?)

Hold a sequence of frames on the engine under id, for plays bound to it (bindFrames) to show by index (showFrame) with no script running per frame. frames[f] is { scatters = {...}, fields = {...} }. A scatter { slot, positions, rotations?, scales?, lanes?, identities?, count } names the buffers the slot's elements are placed from in that frame and how many elements they hold; lanes[k] is a buffer name or false. A field { slot, component, field, values } gives the value the field takes on each of the slot's entities, in binding order. Slots are numbered from 1. The buffers stay the caller's and must stand for as long as the sequence is held. Defining id again replaces it.

Parameters

  • id string — The sequence's name.
  • frames { any } — The frames, in order.

Returns (boolean, string?) — True when the sequence was queued, and why it was refused when not.

substrate.defineFrames("spark", { { scatters = { { slot = 1, positions = P:bufferName(), count = 64 } } } })

typed/builtin//modules/api/engine/substrate/substrate/destroyBuffer

substrate.destroyBuffer(name: string) -> boolean

Free the GPU buffer name denotes, whatever else still holds a handle to it.

The allocation goes and the name is free to be created again at any type and length; every handle that pointed at it answers :alive() false. This is what releases a name whose creating handle is gone, so a build that re-runs at a different size gets its name back.

Parameters

  • name string — The name the buffer was created under.

Returns boolean — True when a GPU buffer under that name was freed.

substrate.destroyBuffer("env.town.xf")

typed/builtin//modules/api/engine/substrate/substrate/forgetFrames

substrate.forgetFrames(id: string) -> boolean

Drop the sequence held under id. A play still bound to it shows nothing further.

Parameters

  • id string — The sequence's name.

Returns boolean — True when the drop was queued.

substrate.forgetFrames("spark")

typed/builtin//modules/api/engine/substrate/substrate/getBuffer

substrate.getBuffer(name: string) -> TypedBuffer?

The GPU buffer name denotes, as a handle you now hold.

A name is how a dispatch binds a buffer, so the name is what an owner asks by once the handle it created with has gone out of scope — a .module that hot-reloaded, a build that ran in an earlier execute. The handle carries everything createBuffer's does and releases its reference with :destroy().

Parameters

  • name string — The name the buffer was created under.

Returns TypedBuffer? — The buffer handle, or nil when no GPU buffer holds that name.

local xf = substrate.getBuffer("env.town.xf")
local shape = xf and { xf:type(), xf:length() }

typed/builtin//modules/api/engine/substrate/substrate/gpuReadback

substrate.gpuReadback(key: string?) -> Readback?

Wrap the key an FFI read handed back as the Readback that polls it. Every GPU→CPU read reaches the caller through this, so a texture's read and a buffer's read answer with the same thing.

Parameters

  • key string (optional) — The key the read returned.

Returns Readback? — The Readback, or nil when the read did not start.

local pending = substrate.gpuReadback(compute.readTexture3D(handle))

typed/builtin//modules/api/engine/substrate/substrate/listBuffers

substrate.listBuffers() -> { NamedBuffer }

Every named GPU buffer the engine holds, in name order.

Each record states id, name, type ("F32", "Vec3", "Vec4", "Quat", "Mat4"), len in records, refs — how many holders it has — holder, the owner a run is held for when one was named (createBuffer's owner), class, the lifetime class of a run ("effect" or "persistent"; absent for a buffer of its own), and bytes, the device bytes it takes: the range a run stands in, or the extent of a buffer of its own. This is what states which names are taken, at what shape, and what an owner holds.

Returns { NamedBuffer } — Array of { id, name, type, len, refs, holder?, class?, bytes }.

for _, b in ipairs(substrate.listBuffers()) do print(b.name, b.type, b.len) end

typed/builtin//modules/api/engine/substrate/substrate/losses

substrate.losses() -> number

How many times a buffer has been freed by name or by owner: destroyBuffer, releaseOwner and the reset to a baseline. A holder that checked that the buffers it names exist at one count knows they still do while the count is the same, whatever else it asks.

Returns number — The count, which only grows.

local at = substrate.losses()

typed/builtin//modules/api/engine/substrate/substrate/markBaseline

substrate.markBaseline() -> number

Record which named GPU buffers the engine holds right now, and make that the point substrate.resetToBaseline returns to.

A caller running work in an engine it shares — a sweep of test suites, a tool driving a session — leaves buffers behind under the names the work chose: a view's geometry built on its first capture, a kernel's storage. This is how such a caller says where its own allocations start.

Returns number — The mark's generation, counting from 1 — what substrate.resetToBaseline takes to restore to this mark.

local mark = substrate.markBaseline() -- then run, then substrate.resetToBaseline(mark)

typed/builtin//modules/api/engine/substrate/substrate/mintName

substrate.mintName(prefix: string) -> string

prefix followed by a number no VM of this engine was handed before. The engine thread and every worker VM draw from one count, so a name minted in one VM is never one another VM minted: what names a buffer, a run's owner or anything else found by name when several VMs make them at once.

Parameters

  • prefix string — What the name starts with.

Returns string — The prefix and a number of its own.

local name = substrate.mintName("cook.points.")

typed/builtin//modules/api/engine/substrate/substrate/placement

substrate.placement(name: string) -> BufferPlacement?

Where the GPU buffer name names stands on the device.

A run stands in a range of an arena page that many runs share: page is the buffer a dispatch binds to reach it, offset the byte it starts at in that buffer (a multiple of 256) and bytes the bytes it holds. A buffer of its own is its own page at offset 0. A kernel that binds a page once and addresses every run of it by offset reads this to state each run's base. rangeBytes is the bytes the range takes in the page, pageBytes the bytes the page holds on the device and pageLiveBytes the bytes standing under the page's live ranges: a set of runs keeps its pages alive by the share of pageLiveBytes the set holds. A job on a worker thread is answered by the engine thread, after it has placed every run the job created before the call.

Parameters

  • name string — The name the buffer binds under (its handle's name).

Returns BufferPlacement? — { page, offset, bytes, rangeBytes, pageBytes, pageLiveBytes }, or nil when no buffer holds the name.

local at = substrate.placement(column.name)

typed/builtin//modules/api/engine/substrate/substrate/releaseBaseline

substrate.releaseBaseline(generation: number) -> boolean

Drop the mark generation names, and every mark taken after it, once the caller that took it is done resetting to it. It touches the marks alone.

Parameters

  • generation number — The mark to drop, as substrate.markBaseline returned it.

Returns boolean — True when the mark stood.

substrate.releaseBaseline(mark) -- the run is over

typed/builtin//modules/api/engine/substrate/substrate/releaseOwner

substrate.releaseOwner(owner: string) -> number

Let go of every run created with owner (createBuffer's class and owner), whatever else still holds a handle to it.

Each run's range goes back to its arena and every handle to it answers :alive() false. This is what something that makes runs as it plays — an effect, a cook, a cache — calls when it ends, so everything it made goes with it at once, runs it handed on to others included.

Parameters

  • owner string — The owner the runs were created with.

Returns number — How many runs were let go of.

substrate.releaseOwner("burst:" .. id)

typed/builtin//modules/api/engine/substrate/substrate/resetToBaseline

substrate.resetToBaseline(generation: number?) -> number

Free every named GPU buffer created since substrate.markBaseline, leaving the ones it recorded and buffers owned by standing render features where they are. Restore the renderer baseline first to release nonbaseline features.

A module that created one of these holds its own handle to it. Put that module's state back alongside this (engine.restoreModuleState) or it goes on addressing storage this freed. is touched.

Parameters

  • generation number (optional) — The mark to restore to, as substrate.markBaseline returned it; the newest mark when omitted. The marks taken after it go with the reset.

Returns number — How many buffers were freed.

local freed = substrate.resetToBaseline(mark)

typed/builtin//modules/api/engine/substrate/substrate/scatterTransforms

substrate.scatterTransforms(key: string, entityIds: { string }?, positions: string, rotations: string?, scales: string?, lanes: { string? }?, interval: number?, identities: string?) -> boolean

Place entities from the buffers that already hold their transform, on the device, instead of from a Transform written per entity.

entityIds[i] is placed from row i of every named buffer, so the order is the buffers' own. Pass nil for entityIds to keep the entities a binding already has and replace only the buffers — what a re-cook does, since a field chain mints a fresh run each time while its membership stands still, and sending the element list again each time would put back the per-element crossing the binding exists to remove.

Re-binding the same key with a list replaces the previous binding. Queued: it takes effect on the next frame, and an entity whose renderable has not landed yet is placed on the frame it does.

Parameters

  • key string — Stable identity of the binding.
  • entityIds { string } (optional) — The entities in buffer order, or nil to rebind the buffers alone.
  • positions string — Name of a buffer holding three f32 per element.
  • rotations string (optional) — Name of a buffer holding four f32 per element.
  • scales string (optional) — Name of a buffer holding three f32 per element.
  • lanes { string? } (optional) — Per-instance shader data, by lane: lanes[k + 1] names a buffer holding one vec4 per element, written into lane k of each element's block — what its material reads back as input.shader_data[k]. A lane left out is left to whatever else writes it.
  • interval number (optional) — Seconds between the presents of the domain the buffers hold. Given, every frame draws each element between the last two presents — position, rotation, scale and lanes blended — the way a fixed-rate simulation is drawn between its ticks, so its motion is continuous at any frame rate while the domain presents at its own. An element new to a slot starts where it was presented. Omitted or 0, each present is drawn as it lands.
  • identities string (optional) — Name of a buffer holding one f32 per element naming which element stands in that row at each present — for a domain whose rows are taken by different elements over time, as a particle's slot is when it dies and another is born into it. A row whose identity moved since the last present starts where it was presented rather than blending from the element that held it before. -1 names a row no element stands in. Omitted, each row's element is the entity placed from it.

Returns boolean — True when the binding was queued.

substrate.scatterTransforms("wave", ids, positions:bufferName())
substrate.scatterTransforms("wave", nil, positions:bufferName())
substrate.scatterTransforms("wave", nil, positions:bufferName(), nil, nil, { colors:bufferName() })
substrate.scatterTransforms("sparks", ids, positions:bufferName(), nil, nil, nil, 1 / 30)
substrate.scatterTransforms("sparks", nil, positions:bufferName(), nil, scales:bufferName(), nil, 1 / 30, ids:bufferName())

typed/builtin//modules/api/engine/substrate/substrate/scatterTransformsInto

substrate.scatterTransformsInto(key: string, into: { transforms: string, data: string?, count: number }, positions: string, rotations: string?, scales: string?, lanes: { string? }?, interval: number?, identities: string?) -> boolean

Place elements that stand in no entity from the substrate buffers that hold their transform, on the device: row i of the columns is composed into matrix i of into.transforms, and lane k of block i of into.data — the two buffers an instanced draw (renderer.mesh.drawInstanced with transformBuffer and instanceDataBuffer) copies into its slots every frame. It is scatterTransforms for a population: the same columns, the same blend between presents when interval is given, the same identities. Binding the same key into the same buffers again replaces only the columns it reads. Queued: it takes effect on the next frame, ahead of the instanced copies that frame records.

Parameters

  • key string — Stable identity of the binding.
  • into { transforms: string, data: string?, count: number } — { transforms, data?, count }: a buffer of count column-major mat4, a buffer of count 64-byte blocks of four vec4 lanes when lanes are bound, and how many elements there are.
  • positions string — Name of a buffer holding three f32 per element.
  • rotations string (optional) — Name of a buffer holding four f32 per element.
  • scales string (optional) — Name of a buffer holding three f32 per element.
  • lanes { string? } (optional) — lanes[k + 1] names a buffer holding one vec4 per element, written into lane k of each element's block in into.data.
  • interval number (optional) — Seconds between the domain's presents; given, each element is drawn between the last two presents.
  • identities string (optional) — Name of a buffer holding one f32 per element naming which element stands in that row at each present; -1 names a row no element stands in.

Returns boolean — True when the binding was queued.

substrate.scatterTransformsInto("sparks", { transforms = "sparks.xf", count = 4096 }, P:bufferName())

typed/builtin//modules/api/engine/substrate/substrate/scatterWorlds

substrate.scatterWorlds(key: string) -> any

Read back where the device drew the elements of the transform binding under key in the last frame drawn — the device's own answer, since the CPU never learns where an element it places stands. 16 floats per placed element, column-major world matrices, in the binding's element order, leaving out elements whose renderable has not landed. A binding drawn between its presents answers the blended pose that frame drew.

Parameters

  • key string — The key the binding was made under.

Returns any — A pending readback: :ready(), then :result() for the floats.

local pending = substrate.scatterWorlds("sparks")
while not pending:ready() do task.wait() end
local x = pending:result()[13]

typed/builtin//modules/api/engine/substrate/substrate/setOwner

substrate.setOwner(name: string, owner: string?) -> boolean

Hold the run name names for owner from now on, in place of the owner it was created with, or for no one when owner is nil.

substrate.releaseOwner(owner) then lets go of it with the rest of what owner holds, and releasing the owner it was made for leaves it standing. What hands a run on to whatever keeps it longer than the one that made it — a cache several makers read from.

Parameters

  • name string — The name the run binds under (its handle's name).
  • owner string (optional) — The owner to hold it for, or nil.

Returns boolean — Whether name names a run (a buffer created with a class).

substrate.setOwner(column.name, "cache#3")

typed/builtin//modules/api/engine/substrate/substrate/showFrame

substrate.showFrame(key: string, frame: number) -> boolean

Show frame frame (from 1) of the sequence the play key is bound to: each slot is placed from that frame's buffers, each instanced draw draws that frame's count, and that frame's fields are written. Showing the frame already shown changes nothing. Queued.

Parameters

  • key string — The play's name.
  • frame number — The frame, from 1.

Returns boolean — True when the frame was queued.

substrate.showFrame("spark#1", 12)

typed/builtin//modules/api/engine/substrate/substrate/unbindFrames

substrate.unbindFrames(key: string) -> boolean

Drop the play key's binding to its sequence. What it placed stays as the last frame shown left it.

Parameters

  • key string — The play's name.

Returns boolean — True when the drop was queued.

substrate.unbindFrames("spark#1")
  • api
  • reference