Rendering — the loop, the two paths, and faults

Render on demand

The engine runs an invalidate-driven loop. It does not redraw every frame unless something is animating: mutate data, change a prop, tick a simulation or run an effect, and the affected subsystem marks itself dirty so the next animation frame draws. When nothing is happening, the GPU is idle.

In practice:

  • a static graph costs ~0 frames after the initial render;
  • a running force layout costs frames until its alpha decays;
  • hover, selection and dim transitions cost frames for the length of the tween.

If you write a custom layer, this is the contract you join: call ctx.invalidate() after any visual change, and return true from frame() while you still need frames. A layer that forgets leaves the picture on the last drawn state.

Two render paths

The engine can draw through WebGL or WebGPU. WebGL is the default and the complete path; WebGPU is opt-in and still gaining parity.

<GraphCanvas data={graph} enableWebGPURenderPath />
api.renderer.setWebGPURenderPathEnabled(true);
api.renderer.isWebGPURenderPathEnabled();   // host permission, NOT "currently using WebGPU"

The flag means only "the host allows it." The engine also needs a capable, available device and no prior fault before it will actually use it. The prop and the method are the same switch; last writer wins.

Turning it back off is a switch, not a fallback — it never reports a fault.

Once a device loss or a shader-compile failure has occurred, that engine instance is stuck on WebGL for the rest of its life. Recovering means recreating the engine instance.

Knowing when a fault happened

api.events.on('renderFault', (report) => {
  // report.faultKind        'device-lost' | 'shader-compile-failed'
  // report.sanitizedReason  the same closed set — never a raw browser string
  // report.resultingPathId  'webgl' | 'webgpu' — the path in use afterwards
  // report.causedFallback   true when this fault is what moved the path
  // report.reportId         stable id, for de-duplicating your own logging
  // report.occurredAt       engine-monotonic ms (performance.now()-based, not epoch)
  telemetry.warn('render fault', report.faultKind, report.resultingPathId);
});

faultKind is a stable de-duplication key, and sanitizedReason is a closed union rather than whatever the browser said — so neither is a channel for device-specific strings.

If your code switches exhaustively over keyof EventMap, note that renderFault is a new member: such a switch needs a new arm.

What differs on the WebGPU path today

  • Node shapes draw as circles. The other seven outlines are WebGL-only for now.
  • GPU readbacks (picking, minimap) are asynchronous underneath. Click, double click and context menu still resolve synchronously inside the DOM event that triggered them, so your handlers are unchanged; hover goes through the asynchronous face.

Nothing else about the public API changes with the path.

Building a backend directly

Hosts that constructed the renderer classes themselves use the backend factory instead:

import { createRenderBackend } from '@kineviz/gl';

const backend = createRenderBackend('webgl');   // or 'webgpu'
backend.kind;                 // 'webgl' | 'webgpu'
backend.supportsPrimitives;   // false on 'webgpu' in this release
const nodes = backend.createNodeRenderer(scene, store);
const edges = backend.createEdgeRenderer(scene, store);

The returned objects keep the method surface they always had (NodeRendererBackend, EdgeRendererBackend, GroupRingRendererBackend, GroupSphereRendererBackend). On 'webgpu' the factories throw WebGPUBackendNotReadyError in this release.

The four renderer classes that used to be exported — GPUNodeRenderer, GPUEdgeRenderer, GPUGroupRingRenderer, GroupSphereRenderer — are no longer part of the public surface, and there are no deprecated aliases. See Upgrading.

Size under perspective

Perspective has no upper bound on apparent size, so flying close to a node would let it fill the frame. Two knobs bound and scale this, and both are documented with the rest of node sizing in Node appearance: api.renderer.setNodeSizeScale and api.renderer.setMaxScreenFraction.

Capture

const blob = await api.renderer.screenshot();

Resolves with a PNG of the last drawn frame at the canvas's live pixel size. It rejects until the canvas is interactive, so call it after onReady.

Theme and projection

api.renderer.setTheme('light');     // 'dark' | 'light'
api.renderer.getMode();             // '2D' | '3D'
api.renderer.setMinimapVisible(true);

None of these are undoable — they are view preferences, not graph edits, and the undo stack is untouched by anything on api.renderer.

See also