GPU compute and rendering effects
The rendering runtime extends drawing with two related GPU capabilities: graph-based computation and reusable visual effects. Graphs keep data work on the GPU and effects turn rendered images and scene data into a final view. See GPGPU compute for general-purpose computation and Rendering effects for effect composition. Related graph structure is described in GPU graph systems and GPU graph workflows.
Runtime boundary
The graph and rendering systems share GPU resources and command submission but solve different problems. Graph computation produces data, buffers, or textures for later work. Rendering effects consume images and scene buffers to change the displayed result.
Graph computation
Graph computation describes values and operations before execution. The runtime can compile that description for a WebGPU device and run it with new input bindings. This separates reusable computation from one invocation.
References: modules/…/index.ts• L612-644 modules/…/gpu-graph.ts• L33-130 modules/…/gpu-command-graph.ts• L663-668
Rendering kernels
Rendering kernels provide a small reusable program boundary beside graph computation. A kernel keeps shader and pipeline state reusable while each draw supplies its bindings and geometry. Higher layers can then choose the scene, effect, or command graph that uses it.
References: modules/…/kernel.ts• L42-48 modules/…/render-kernel.ts• L49-55
Graph-based computation
The graph system turns named inputs, state values, operations, and outputs into executable GPU work. It supports semantic graphs that compile into reusable programs and command graphs that join compute, render, and copy work with explicit resource flow.
Declarative graph programs
GPUGraph is a backend-independent description of scalar, vector, and texture values. It records operations and value roles, then produces a compiled program that can prepare and run many invocations with caller-provided bindings. This model keeps the computation definition separate from changing data.
Numeric expression operations add a compact way to describe scalar and vector math inside that graph. They accept references, literals, arithmetic, vector construction and selection, swizzles, supported math functions, and explicit casts. The graph checks referenced values and result shapes before execution. Arithmetic uses float32 values, while integer values can take part through explicit casts. A cast to an integer format can use truncation or nearest rounding, with truncation as the default.
The expression contract is shared by the graph's execution paths. It rejects texture inputs, unknown names, unsupported syntax, incompatible vector widths, and mixed component types where the expression needs matching types. This keeps an expression a reusable graph operation rather than a device-specific shader fragment.
References: modules/…/gpu-graph.ts• L33-130 modules/…/gpu-expression-operation.ts• L11-67 modules/…/gpu-expression-operation.ts• L107-196 modules/…/gpu-expression-operation.ts• L231-327 modules/…/compiled-gpu-graph.ts• L22-59
Graph statistics
Graph statistics provide reusable GPU data operations between ordinary graph values and later compute or render work. Their contracts define input formats, optional row selection, and result shape before the compiler maps them to aggregation or reduction work.
Spatial statistics extend this model to two-dimensional point data. Grid binning produces row-major cell counts. Grid aggregation produces weighted sums, minima, maxima, or means. Bounds may come from fixed values or GPU data, so a frame can update its spatial domain without a CPU readback.
These operations preserve the graph resource model. They accept packed data or ordered vector chunks, write to caller-owned result views, and use graph-owned intermediate storage when a calculation needs more than one GPU pass. Large data sets and their summaries can therefore remain on the GPU.
References: modules/…/gpu-semantic-aggregation-operation.ts• L14-149 modules/…/gpu-graph-compiler.ts• L340-390 modules/…/gpu-group-aggregation.ts• L42-90 modules/…/gpu-reduction.ts• L42-84 modules/…/gpu-grid-binning.ts• L32-71 modules/…/gpu-grid-aggregation.ts• L28-78
Spatial indexing
Spatial indexing keeps point locations and cell membership on the GPU. A grid index builds cell offsets and object IDs. Grid queries then select candidate IDs for a point, bounds, or radius request without requiring exact geometry tests at this stage.
The result is a conservative candidate set. Bounds and radius queries can include objects outside the exact shape. A full output buffer can report overflow instead of silently dropping candidates. A later stage can use the candidates or an optional mask for exact filtering.
References: modules/…/gpu-grid-index-internals.ts• L26-217 modules/…/gpu-grid-index-query.ts• L25-73 modules/…/gpu-grid-index-query.ts• L116-310 modules/…/index.ts• L417-431
Prefix and segmented operations
Prefix and segmented operations provide the data-flow steps needed by indexing and other GPU algorithms. A scan creates inclusive or exclusive prefixes and can restart at supplied flags. Segmented scan and reduction apply the same idea to ranges described by offsets.
These operations keep vector chunk boundaries as logical input structure while producing ordered results. The graph can compose them with other nodes, so counts, offsets, candidate ranges, and summaries remain available to later GPU work.
References: modules/…/gpu-scan.ts• L35-70 modules/…/gpu-segmented-scan.ts• L18-37 modules/…/gpu-segmented-reduction.ts• L21-38 modules/…/index.ts• L135-191
Command graphs
GPUCommandGraph describes the order and resource use of a GPU workflow. It accepts compute, render, and copy nodes. It infers dependencies from declared resource access and compiles the graph into an executable form. The application still controls encoding and submission, so a graph can fit inside a larger frame or schedule.
The graph treats buffers, textures, views, and frame-scoped images as logical resources. Declared read and write roles order dependent work. Texture views allow hazard checks to follow overlapping mip, layer, and aspect ranges. Graph-owned transient resources can be reused when their lifetimes do not overlap.
Compilation can prepare node resources synchronously or through concurrent asynchronous pipeline creation. The compiled graph also exposes resource, workload, device-limit, and allocation information for inspection before submission.
The following diagram shows how a graph definition becomes ordered GPU work and produces data for rendering or later computation.
flowchart LR
I[Inputs and state] --> G[Graph operations]
G --> C[Compiled graph]
C --> E[Ordered compute, render, or copy work]
E --> O[GPU outputs]
O --> R[Later computation or rendering]
References: modules/…/gpu-command-graph-types.ts• L31-60 modules/…/gpu-command-graph.ts• L663-668 modules/…/gpu-command-graph.ts• L917-1039 modules/…/gpu-command-graph-compiler.ts• L120-185 modules/…/gpu-command-graph-compiler.ts• L188-275 modules/…/gpu-command-graph-compiler.ts• L428-521
Conditional graph work
Command graphs can suppress work from CPU state or use a GPU-written indirect dispatch count. A graph can therefore skip empty work without a CPU readback. The condition and indirect buffer remain part of the graph's resource and diagnostic view.
References: modules/…/gpu-command-graph-types.ts• L517-569 modules/…/gpu-command-graph.ts• L1041-1111
Simple graph workflows
GPUSimpleGraph provides a graph-shaped view over lazy data evaluators and shared render operations. It can compile a declarative expression program or lower evaluator-based operations into a dependency plan with selected outputs. In the evaluator path, data work runs before appended render work, and the compiled result exposes the reachable plan for inspection.
The simple graph keeps vector chunk boundaries instead of silently packing them into one resource. A vector transform applies independently to each chunk, then materializes the ordered chunks as one logical output. Existing GPU data can therefore remain usable without a repack step.
References: modules/…/gpu-simple-graph.ts• L83-115 modules/…/gpu-simple-graph.ts• L166-243 modules/…/gpu-simple-graph.ts• L274-408 modules/…/gpu-vector-evaluator.ts• L43-52 modules/…/gpu-vector-evaluator.ts• L147-165
Backend paths
The graph API presents one operation model while backend entry points provide device-specific execution paths. The simple graph can evaluate supported data work synchronously on WebGL or asynchronously on WebGPU. WebGPU also supports caller-controlled evaluation scopes, so encoded work can join a larger submission.
References: modules/…/webgpu.ts• L5-21 modules/…/gpu-simple-graph.ts• L99-115 modules/…/gpu-simple-graph.ts• L499-585
Resource ownership
Graph resources have clear ownership boundaries. Imported buffers and textures remain with the caller. Graph-owned transient resources belong to the compiled graph. Frame-scoped textures and external images are borrowed for one numbered encoding and are not acquired, presented, or destroyed by the graph.
Evaluator inputs can be borrowed without copying, and selected deferred outputs can write into caller-provided buffers. Compiled results remain available until the graph is destroyed, with no automatic CPU readback. A graph becomes fixed after compilation, so later encodings change bindings and parameters rather than the graph definition.
References: modules/…/gpu-command-graph-types.ts• L62-92 modules/…/gpu-command-graph-types.ts• L284-364 modules/…/gpu-command-graph.ts• L700-741 modules/…/gpu-command-graph.ts• L843-900 modules/…/gpu-simple-graph.ts• L455-497 modules/…/gpu-data-evaluator.ts• L104-112 modules/…/gpu-data-evaluator.ts• L409-429
Bounded graph execution
Large command graphs can be planned as several ordered submission steps. The plan uses workload bounds and a caller-selected latency and publication policy. An application can trade completion time for responsive frame updates without changing the graph's data flow.
Each step keeps graph nodes atomic and preserves dependency order. A progressive policy can expose declared partial results at safe boundaries. A final policy waits for the complete graph result.
References: modules/…/gpu-command-graph.ts• L144-240 modules/…/gpu-command-graph.ts• L254-322 modules/…/gpu-command-graph.ts• L334-405 modules/…/gpu-command-graph-types.ts• L579-664
Reusable visual effects
An effect is a reusable shader pass or group of passes that changes a source image. Effects stay independent of the scene renderer because they receive routed textures and frame inputs, then return a final texture for display or for another stage.
Effect chains
The effect pipeline moves an image through a shared chain of passes. Each pass can read the original image, the previous result, or a named intermediate target. The final chain result is shown on screen or returned to another rendering stage.
References: modules/…/shader-pass-renderer.ts• L77-89 modules/…/shader-pass-renderer.ts• L185-305 modules/…/shader-pass-renderer.ts• L465-516
Effect composition
Composite effects express a visual result as connected stages rather than one isolated shader. Bloom builds and combines glow levels. Screen-space reflections use depth, normals, and frame history to add reflected detail. These patterns let effects share routing and history rules while using different scene inputs.
References: modules/…/bloom-composite-shader-pass.ts• L863-946 modules/…/screen-space-reflections.ts• L653-753
Compute-backed effects
Some effects can use a compute path when the device supports the needed storage textures and workgroup limits. The runtime checks these capabilities and keeps a render-pass path available when the compute path is not suitable. The high-level visual feature therefore does not depend on one GPU route.
References: modules/…/shader-pass-renderer.ts• L370-388 modules/…/shader-pass-renderer.ts• L767-797 modules/…/bloom-composite-shader-pass.ts• L1142-1159
Frame history
History targets let temporal effects compare the current frame with an earlier result. The renderer initializes and swaps history only after a successful frame. It resets history when the stored image no longer matches the current scene state.
References: modules/…/shader-pass-renderer.ts• L38-47 modules/…/shader-pass-renderer.ts• L390-455
Cross-system flow
Compute and effects meet at GPU resources and command order. A compute stage can write a storage texture or buffer that a later render stage samples. An effect can return a texture for another graph or frame stage. This connection avoids a CPU round trip when the data matches the next operation.
Frame data flow
When a frame uses both systems, it follows a resource-driven sequence:
- The application supplies scene data and dynamic bindings.
- A graph computes buffers or textures needed by the scene or effect.
- Rendering produces the source image and scene buffers.
- Effects route those resources through one or more visual passes.
- The runtime presents the final image or returns it to the caller.
References: modules/…/gpu-command-graph.ts• L921-983 modules/…/gpu-simple-graph.ts• L526-540 modules/…/shader-pass-renderer.ts• L239-287 modules/…/shader-pass-renderer.ts• L651-689
Change checklist
When these files or sections are changed, remember to consider these:
- When numeric expression syntax or type rules change, keep parsing, format inference, output shape, integer rounding, and both backend execution paths aligned.
- When a graph operation changes its value format or output role, update its lowering, resource view, and output binding contract together.
- When graph-level aggregation or reduction changes, keep its semantic operation contract, inferred result shape, and compiler lowering aligned.
- When a command graph changes a resource use, preserve dependency inference, transient lifetime rules, frame binding rules, and imported-resource ownership.
- When a graph adds a render or compute node, keep its resource declarations consistent with the commands that the node records.
- When graph workload or publication metadata changes, keep preflight reports and bounded execution plans aligned with the nodes that they describe.
- When an effect adds or renames an intermediate target, update every pass that reads or writes that target.
- When an effect adds frame history or a compute alternative, keep history reset rules and device capability checks aligned with the fallback render path.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A reusable graph is compiled for a non-WebGPU device | Compilation fails before work is recorded | Select a WebGPU device for GPUGraph programs. |
| A numeric expression reads a texture or uses unsupported syntax | Expression parsing or graph compilation fails | Use declared scalar or vector inputs and the supported numeric expression forms. |
| A numeric expression mixes integer values with arithmetic or uses incompatible vector widths | Format validation rejects the operation | Use explicit float32 casts for arithmetic and match widths or use one-component broadcasting. |
| A numeric cast uses a non-finite value or an unsupported format | Expression validation or GPU execution cannot produce the intended value | Use finite values and the declared float32, sint32, or uint32 formats. |
| A simple graph uses textures on a non-WebGPU device | Construction fails before execution | Use the simple graph texture path only with WebGPU. |
| A simple graph uses state, custom lowerings, or an unsupported operation in its declarative path | Compilation or execution is rejected | Use the supported simple graph operation set, or use the full GPUGraph path. |
| A grid input contains non-finite or out-of-bounds points | The points are ignored and do not contribute to a cell or query candidate set | Validate spatial data when the application needs complete coverage. |
| A grid output length does not equal the declared cell count | Grid operation construction is rejected | Set output length to the product of the grid dimensions. |
| A spatial query output is too small for its candidates | The count reports overflow and the candidate list is incomplete | Size the output for the expected candidate set and check the overflow result. |
| A graph aggregation input, mask, or value has a different logical length | Graph validation or operation creation rejects the calculation | Keep paired graph columns aligned by logical row count. |
A reduction result does not have one row, or two rows for extent | The reduction operation is rejected | Match the result shape to the selected reduction operation. |
| A grouped statistic or reduction shares its output buffer with an input | Operation creation rejects the resource layout | Use a separate caller-owned output buffer. |
| A simple graph places evaluator work after a render operation | Graph construction rejects the ordering | Add all evaluator work before render work. |
| A simple graph run overlaps another run or destruction | The execution rejects the concurrent or invalid operation | Wait for the run to finish before starting another run or destroying the graph. |
| An evaluator remains retained by an unsubmitted scope | Other evaluations or destruction are rejected | Submit or cancel the scope before using or releasing the evaluator. |
| A graph reuses a value or node identifier | Graph construction rejects the definition | Keep value and node identifiers unique within each graph. |
| A command graph is changed after compilation | The graph rejects later declarations or nodes | Build the full graph before compiling it. |
| An imported resource does not match the declared graph layout | Encoding or binding fails, or resource use becomes invalid | Keep imported format, extent, usage, and view data equal to the graph declaration. |
| A frame-scoped texture or external image is reused with an old frame identifier | Encoding rejects the stale binding | Supply a fresh caller-owned binding with a strictly increasing frame identifier. |
| A prepared graph execution is encoded twice or on another device | The execution rejects the second or mismatched encode | Encode once with an encoder from the same device, then destroy after submission. |
| A bounded graph step contains one indivisible node larger than the requested budget | That step still exceeds the budget | Split the operation into graph nodes when progressive scheduling is required. |
| An effect reads and writes the same non-history target | The pass result is undefined or rejected | Route the read through another target or through history. |
| A compute effect exceeds device storage or workgroup limits | The compute path is skipped or pipeline creation fails if forced | Use capability checks and retain the render fallback. |
| A temporal effect keeps history after a scene discontinuity | Old pixels affect the new frame | Reset history when the scene or camera state no longer matches. |
Key files
| File | Why to read it |
|---|---|
| modules/…/index.ts | Defines the public graph, command graph, spatial operation, and kernel boundary. |
| modules/…/gpu-graph.ts | Shows how named values and operations form a reusable computation. |
| modules/…/gpu-expression-operation.ts | Defines the portable numeric expression feature, graph-level type rules, and integer cast behavior. |
| modules/…/gpu-graph-compiler.ts | Shows how semantic graph operations become executable WebGPU command work. |
| modules/…/gpu-command-graph.ts | Defines resource flow, node ordering, conditional work, compilation, and bounded execution. |
| modules/…/gpu-command-graph-types.ts | Defines logical resources, access roles, node contracts, workload reports, and frame bindings. |
| modules/…/gpu-command-graph-compiler.ts | Shows dependency ordering, transient resource reuse, asynchronous compilation, and preflight reports. |
| modules/…/gpu-simple-graph.ts | Shows the graph-shaped data workflow, inspection plan, output selection, and render integration. |
| modules/…/kernel.ts | Defines the reusable compute program boundary. |
| modules/…/render-kernel.ts | Defines the reusable render program boundary. |
| modules/…/shader-pass-renderer.ts | Defines effect routing, intermediate targets, history, and compute replacements. |
| modules/…/index.ts | Shows the public catalog of reusable visual effects. |
| modules/…/bloom-composite-shader-pass.ts | Illustrates a multi-stage effect with an optional compute path. |
| modules/…/screen-space-reflections.ts | Illustrates a temporal effect that consumes scene buffers and frame history. |