Log inGet started
Package

compute.module

Newest version

This package holds more than this page lists. See all of its files in its world.

README.md

compute Module

Public Luau surface over the __compute Internal FFI namespace — GPU compute pipelines.

Purpose

A compute shader is a .computeShader asset, and everything you do with one you do through a resolved reference to it. The asset owns its compiled program: the compile the reference asks for registers it, and removing the asset releases it. This module carries what the reference does not: the readback poll, the health reads, the GPU textures and samplers a dispatch binds by name, and the scene BVH.

Usage

local double = asset.resolve("@builtin::shaders.compute_double", "computeShader")

local scratch = double:createBuffer("data", { type = "f32", len = 64, usage = { "readback" } })
scratch:write({ 1.0, 2.0, 3.0, 4.0 })

double:dispatch({ buffers = { scratch }, workgroups = { 1, 1, 1 } })

local read = scratch:read(0, 16)
while not read:ready() do task.wait(0) end
local floats = read:result()

asset.resolve records your script's dependency on the shader — which is what carries the shader along when the world is packed, pulled or published.

Is it compiled yet?

compute.programState(ref) says where a shader's compiled program stands: "absent" (nothing held under this key and nothing in flight), "pending" (a registration or compile was asked for and has not reached the device — a recompile of a resident program reads pending too), "ready" (compiled and resident, so a dispatch binds it), or "failed", with the reason as a second return.

if compute.programState(shaderRef) == "ready" then
    shaderRef:dispatch({ buffers = { buf }, workgroups = { 1 } })
end

ref:compile() queues the compile and the pipeline is built on the render side frames later, so that call cannot answer this. A dispatch issued before the program is ready is held behind the compile and dropped if it never lands, and a readback taken then reports the buffer as it was.

Is anything broken?

compute.failing() lists every (shader, target) pair whose most recent dispatch failed, with the reason. An empty result means every dispatch the engine has been given is running. For one shader, ref:status() reports each of its targets, running or not.

for _, d in ipairs(compute.failing()) do
    print(d.shader, d.target, d.failures, d.lastError)
end

A dispatch is recorded into a command encoder frames after the call that asked for it returned, so the call answers that it queued the dispatch and these two answer what the dispatch did. A zero in any workgroups dimension is one of the things they report: it is refused, and a count computed from how much data there is passes through math.max(1, ...) so an empty set dispatches one workgroup rather than none.

Exports

  • Compute lists: list(key, entries), runList(key, slots?, label?), dropList(key). A list is an ordered run of dispatches (over buffers or typed resources) and buffer writes registered once and queued whole by one runList call, each value a run supplies named as a slot; see "Many dispatches as one call" in types/computeShader.
  • Health: programState, failing.
  • Readback: isReadbackReady, readbackState, getReadbackResult, getReadbackResultU32, getReadbackResultBytes. A handle answers a read itself — buf:read(offset, len) gives a Readback with ready, state and the three drains on it.