GPUGraph
A GPUGraph describes a computation as a sequence of GPU operations. Results of
operations are available to be used as input for later operations.
import {GPUGraph, gpuExpression} from '@luma.gl/graph/gpu-graph';
const graph = new GPUGraph();
graph.addInput('values', {type: 'vector', format: 'float32'});
graph.add(gpuExpression, {output: 'doubled', expression: 'values * 2'});
graph.add(gpuExpression, {output: 'result', expression: 'doubled + 1'});
Here the named output doubled connects the two operations. The
getting started guide
shows how to compile the graph, supply a vector, and read back the result.
Inputs​
Declare a named input with graph.addInput(name, descriptor). In the example above, graph.addInput('values', {type: 'vector', format: 'float32'}) tells the graph to expect a vector of 32-bit floats. Operations can then refer to it by the name values.
Supply the actual data when you run the compiled graph. Here samples is a GPUVector supplied by the application.
const compiledGraph = graph.compile(device, {outputs: ['result']});
const {result} = await compiledGraph.run({inputs: {values: samples}});
To process another vector, run the same compiled graph again with a different value for inputs.values. See graph inputs, results, and state for the supported input types.
GPU Graph operations​
Add an operation with graph.add(operation, props). The operation defines the work and its properties identify the inputs, outputs, and settings for that step.
gpuExpressionevaluates an expression for each row of a vector.gpuCompactselects rows and produces a GPU-resident count of the selected rows.gpuDrawFullscreendraws a fullscreen pass with named texture inputs.
You can define a custom operation with its own input and output ports. Add it to a graph the same way you add a built-in operation.
Data flow​
In the example above, values comes from the caller, doubled passes from one operation to the next, and result is the output the caller wants to keep. Declare external inputs with addInput(), add each producer before its consumers, and select the outputs when you compile. The graph manages storage for intermediate results.
Use mutable state when a step needs to update a value that a later iteration or execution will read. See graph inputs, results, and state for binding and ownership, and data flow for how values move between CPU and GPU operations.
The graph is designed to keep data on the GPU and eliminate CPU readbacks​
In the example above, the second operation reads doubled directly on the GPU. The same applies when a render pass consumes a texture produced by an earlier operation. Read a result back when JavaScript needs its contents.
Scalar GPU values​
The graph supports scalar values on the GPU. A scalar holds one GPU-resident number. GPU operations can take scalars as inputs and produce them as outputs. Scalars can also control graph execution through graph.if() and graph.loop().
Applications can declare scalar inputs with graph.addInput().
graph.addInput('scale', {type: 'scalar', format: 'float32'});
This declaration lets you supply scale as a JavaScript number when you run the graph.
For example, a float32 scalar named scale can multiply every row of a vector in the expression values * scale.
GPU operations can also write GPU-resident scalars. For example, a solver can write a uint32 flag after each iteration and let the GPU use it to decide whether to continue, without reading the flag back to JavaScript.
Reusable scopes​
Use graph.createScope() to build a named group of operations. Pass the scope to graph.add(scope) to capture its current steps, or use it as the body of graph.if() or graph.loop().
Control flow​
GPU Graph provides support for basic conditional and repeated execution using GPU-resident predicates.
Use graph.if(predicate, body) to run a group of operations when a GPU scalar is nonzero. Use graph.loop(options, body) to repeat a group up to a specified iteration limit. A loop can use a GPU predicate to stop doing work once a solver has converged.
graph.loop({maximumIterations: 20, predicate: keepGoing}, scope =>
scope.add(updateEstimate, props)
);
The callback describes the body once. During execution the GPU evaluates keepGoing for each iteration. See GPU Graph control flow for predicates, bounds, and nested scopes.
For practical examples, see the GPU Graph Cookbook. For execution and resource bindings, see Compiling GPU Graphs.