@luma.gl/graph/gpu-volume
From v10WebGPU requiredExperimental
@luma.gl/graph/gpu-volume provides three-dimensional image-processing operations for
GPUGraph and
caller-owned GPUCommandGraph.
GPU Volume Algorithms is inspired by RAPIDS cuCIM and its GPU-accelerated
n-dimensional image-processing categories. It is not a JavaScript port or an API-compatibility
layer: cuCIM targets CUDA arrays and a scikit-image-compatible Python surface, while GPU Volume Algorithms targets
portable browser WebGPU, explicit graph resources, and application-owned command submission.
| Operation | Result |
|---|---|
gpuVolumeThreshold | Classification mask and optional observation validity |
gpuVolumeMorphology | Dilated, eroded, opened, or closed volume with observation validity |
gpuVolumeConnectedComponents | Sparse component labels, validity, and convergence diagnostics |
gpuVolumeRegionMeasurements | Per-label voxel counts, index-space bounds, and capacity overflow |
Data model​
GPUVolume borrows one or more packed GPUVolumeBufferChannel objects. Each channel contains
exactly one scalar float32, uint32, or sint32 sample per voxel. Linear storage is x-fastest,
then y, then z:
voxelIndex = (z * height + y) * width + x
Optional validity flags are independent of sample values: a zero sample can remain valid, while a
zero validity flag rejects the corresponding observation. noDataValue is compared in the raw
source type before the optional sample * scale + offset calibration. Algorithms never infer
missingness from a calibrated value.
GPUVolumeMetadata retains physical spacing, origin, a row-major 3x3 direction matrix, and
whether integer coordinates identify voxel cells or sample points. GPUVolume uses JavaScript
double precision for coordinate conversion and physical voxel volume. GPU algorithms use the dense
index grid and never silently resample or reorient it.
Composing a pipeline​
Pass one grid shape through the stages. Bind packed x-fastest samples by name. The graph allocates intermediate masks, validity, and labels from the grid dimensions.
import {GPUGraph} from '@luma.gl/graph/gpu-graph';
import {
gpuVolumeConnectedComponents,
gpuVolumeMorphology,
gpuVolumeRegionMeasurements,
gpuVolumeThreshold
} from '@luma.gl/graph/gpu-volume';
const grid = {width, height, depth};
const graph = new GPUGraph();
graph.addInput('density', {type: 'vector', format: 'float32'});
graph
.add(gpuVolumeThreshold, {
...grid,
input: {values: 'density', noDataValue: -999},
threshold: 0.42,
output: 'seeds',
outputValidity: 'seedValidity'
})
.add(gpuVolumeMorphology, {
...grid,
input: {values: 'seeds', validity: 'seedValidity'},
mode: 'binary',
operation: 'dilate',
radius: 1,
structuringElement: 'ball',
noDataPolicy: 'ignore',
output: 'expanded',
outputValidity: 'expandedValidity'
})
.add(gpuVolumeConnectedComponents, {
...grid,
input: {values: 'expanded', validity: 'expandedValidity'},
output: 'labels',
outputValidity: 'labelValidity',
converged: 'converged',
connectivity: 26
})
.add(gpuVolumeRegionMeasurements, {
...grid,
labels: 'labels',
labelValidity: 'labelValidity',
output: {voxelCounts: 'counts', minimumCoordinates: 'minimum', maximumCoordinates: 'maximum'},
overflow: 'overflow'
});
const compiledGraph = graph.compile(device, {
outputs: ['counts', 'minimum', 'maximum', 'converged', 'overflow']
});
const regions = await compiledGraph.run({inputs: {density}});
Threshold validity distinguishes nodata from valid background. Pass it to morphology so missing
voxels stay missing. With noDataPolicy: 'ignore', a valid voxel can still use its other
neighbors. Inspect converged and overflow before using the measurements.
Use compiledGraph.prepare({inputs}) to record the same pipeline into a caller-owned encoder.
Add threshold and morphology within a GPU conditional using
scope.add(gpuVolumeThreshold, props) and scope.add(gpuVolumeMorphology, props). Declare their output values
before adding the conditional. Supply an execution binding for any destination that a later pass
may read even when the conditional skips its writes.
Compose with concrete buffer views​
Use the same operations in a GPUCommandGraph. Pass concrete views for inputs and use
getContribution(id) to pass outputs to the next stage before compiling. When a nested command
producer supplies concrete destinations, use gpuVolumeThreshold.commands(props) or
gpuVolumeMorphology.commands(props).
import type {
GPUVolumeConnectedComponentsResult,
GPUVolumeThresholdResult
} from '@luma.gl/graph/gpu-volume';
commandGraph.add(gpuVolumeThreshold, {
id: 'seeds',
...grid,
input: densityChannel,
threshold: 0.42,
outputValidity: true
});
const seeds = commandGraph.getContribution<GPUVolumeThresholdResult>('seeds');
commandGraph.add(gpuVolumeConnectedComponents, {
id: 'components',
...grid,
input: {
id: 'seeds',
format: 'uint32',
values: seeds.output,
validity: seeds.outputValidity
}
});
const components =
commandGraph.getContribution<GPUVolumeConnectedComponentsResult>('components');
commandGraph.add(gpuVolumeRegionMeasurements, {
id: 'regions',
...grid,
labels: components.output,
labelValidity: components.outputValidity
});
Omit destinations for graph-owned storage. Supply a view for an output that must stay
available to a separate graph or a renderer. Pass outputValidity: true on thresholding
or iterationCount: true on segmentation to allocate those optional outputs. The
command graph borrows any supplied views and owns the storage it allocates.
Morphology​
Binary morphology canonicalizes nonzero uint32 input to one. Grayscale morphology converts valid
native samples to calibrated float32 values before selecting extrema. The footprint families
mirror common volumetric morphology shapes:
cubeincludes every offset in the bounded radius.octahedronincludes offsets within the Manhattan radius.ballincludes offsets within the Euclidean radius.
Radii are limited to zero through four so the cooperatively loaded value-and-validity tile fits the
portable WebGPU workgroup-storage floor. clamp, reflect, constant, and nodata make boundary
behavior explicit. With noDataPolicy: 'propagate', any included missing neighbor invalidates the
result; ignore skips missing neighbors but never makes an invalid center valid.
Opening and closing contribute two ordered passes and allocate type-matching graph-owned scratch. They borrow their source and any supplied final outputs.
Connected components​
gpuVolumeConnectedComponents labels nonzero valid voxels using deterministic minimum x-fastest
roots. Label zero remains background or invalid; positive labels are the root voxel index plus one.
The result is intentionally sparse, avoiding an implicit sort, compaction, or readback.
The contributor declares bounded atomic hooking and path-compression rounds. GPU-written indirect
dispatch dimensions suppress later rounds after stabilization. converged and optional
iterationCount stay GPU-resident. If maximumIterations is insufficient, publication fails
closed by clearing every output label and validity flag.
Region measurements​
gpuVolumeRegionMeasurements accepts any zero-background one-based label field. Output capacity is
the length of voxelCounts; minimumCoordinates and maximumCoordinates contain one packed
uint32x3 row per slot. Maximum coordinates are exclusive, so an occupied region's grid extent is
maximum - minimum. Empty slots are canonical zero rows.
Sparse connected-component roots can be measured directly by allocating up to the voxel count, or
an application can supply its own dense labels and smaller capacity. A positive label beyond the
capacity sets overflow and is not folded into another region.
Use measurements in a slice overlay​
Bind the labels, their validity flags, and voxelCounts as read-only storage. For a
valid voxel with a label between 1 and the measurement capacity, read
voxelCounts[label - 1]. A fragment shader can use this count to hide small components.
Bounds can be read from the same slot. Ignore background, invalid voxels, and labels
outside the measured capacity.
Encode the segmentation and measurement passes before the draw on the same command encoder. The counts and labels can be reused on subsequent frames. Changing a display cutoff only changes a render uniform, so it can update without running segmentation again.
Boundaries and next tranches​
This first GPU Volume Algorithms slice intentionally does not claim cuCIM feature parity. Candidate later tranches include separable Gaussian/rank filters, Euclidean distance transforms, watershed and contour surfaces, multiscale registration, chunked/halo-aware out-of-core volumes, and dense component-label compaction. Those algorithms require separate numerical, storage, or capacity contracts and are not hidden inside the implemented operations.
Texture upload, medical-image decoding, chunk scheduling, command submission, synchronization, and readback remain application responsibilities.
Try the Volume Lab​
The GPU Volume Algorithms Volume Lab runs the implemented tranches over a
deterministic, non-diagnostic CT-like phantom. Its synchronized axial, coronal, and sagittal views
sample resident volume and sparse-label buffers. The lab wraps the measured voxel counts in a
one-column GPU table, with row i representing label i + 1. Minimum component voxels updates
a parameter on a compiled DataFrame query. The slice renderer samples its row-aligned selection
mask, so changing the minimum leaves segmentation results and component IDs intact. Segmentation,
selection, and drawing can all record to one frame encoder. CPU readback is limited to convergence,
iteration, and overflow status.