Skip to content

About

WebGPU renderer for Vega visualizations

Topics

Resources

Code of conduct

Security policy

Stars

11 stars

Watchers

4 watching

Forks

Repository files navigation

WebGPU Renderer for Vega

npm version CI

A third renderer for Vega, next to canvas and svg, that draws the scenegraph with WebGPU. It registers as renderer: 'webgpu', so a spec needs no changes.

Canvas is the reference. Every spec in the corpus is drawn by both renderers and compared on every commit, and what still differs is documented.

  • Project page: features, known differences from canvas, roadmap and every release
  • Live demo: any spec from the corpus, with canvas beside it and a pixel diff. benchmark animates up to 300k points
  • Mark playground: one mark at a time, with its properties to switch on and off
  • Render gallery: every case with its measured difference, for each of the last ten runs on main and for every release. ?run=<sha> or ?run=2.0.0 picks one

Started by lsh and continued by KaNaDaAT.

Usage

Load it after Vega and it registers itself:

<script src="https://cdn.jsdelivr.net/npm/vega@6/build/vega.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-webgpu@2/build/vega-webgpu-renderer.js"></script>
<div id="vis"></div>
<script>
  fetch('https://vega.github.io/vega/examples/bar-chart.vg.json')
    .then(res => res.json())
    .then(spec => {
      new vega.View(vega.parse(spec), {
        renderer: 'webgpu',
        container: '#vis',
        hover: true,
      }).runAsync();
    });
</script>

vega-webgpu@2 is the newest 2.x release. To stay on one, pin its exact version, for example vega-webgpu@2.0.4. Every release is listed under versions.

Or install it from npm. Importing the package registers the renderer, the same as the script tag does:

npm install vega vega-webgpu
import * as vega from 'vega';
import 'vega-webgpu';

2.0.0 and later need Vega 6. The 1.x builds need Vega 5 and fail next to Vega 6.

Without navigator.gpu, 'webgpu' draws with the canvas renderer and warns. A browser can also have navigator.gpu and no adapter, a blocklisted GPU for instance, and then nothing is drawn. To fall back there too, pick the renderer from the adapter:

const renderer = (await navigator.gpu?.requestAdapter()) ? 'webgpu' : 'canvas';

Options

Options live on view._renderer.wgOptions and can be changed between frames:

view._renderer.wgOptions.debugLog = true;
Option What it does Default
sampleCount MSAA samples per pixel, 4 or 1. 4
redrawOnZoom Resize and redraw when browser zoom changes the pixel ratio. When false the canvas keeps its first ratio and the browser scales it. true
renderLock While a frame is in flight, keep only the latest render call and run it next. true
cacheShapes Keep triangulated geometry between frames. true
canvasTextDrift Place each label where the canvas renderer would, which can be a row off on a half pixel baseline. Only for matching a canvas view side by side. false
offscreen Draw into a texture the renderer owns and leave the canvas blank, for a headless runner with no compositor. Read the frame with captureFrame(). false
debugLog Log per-frame timings to the console. false

renderBatch and simpleLine were removed in 2.0.0.

Features and limitations

All twelve Vega mark types are drawn, with gradient fills and strokes, dashes, caps, joins and every blend mode. The project page has the per-mark table and the known differences from canvas. Open work is in the issues.

Development

npm install
npm run build      # bundles and type declarations into build/
npm run serve      # the repo at http://localhost:5500
npm run dev        # rollup in watch mode
npm run typecheck
npm run lint

Then open http://localhost:5500/test/?spec=bar&renderer=webgpu&version=dev to run the local build against any spec in test/specs-valid.

npm install also sets up a pre-commit hook. It runs eslint --fix and prettier on the staged files that npm run lint and npm run format cover.

To add a spec, drop a .vg.json into test/specs-valid and build, which regenerates the list the demo page and the suite read. A test fails if that list falls behind the directory. Name a spec in an optional test/specs-ignore.json to keep it out of the demo page's picker.

To add a scenegraph fixture, drop a JSON file into test/render/scenes: { "description", "width", "height", "origin", "scene" }, where scene is what vega.sceneToJSON produces. A fixture skips the View, the dataflow and the layout, so a failure there is in the mark code.

Render tests

npm test draws every spec in test/specs-valid and every scenegraph fixture in test/render/scenes with both renderers in headless Chromium (WebGPU on SwiftShader), and compares the two. Canvas is the reference, so there are no stored baselines. A known difference carries its own budget in test/render/specs.ts or test/render/scenes.ts. The suite loads the built bundle, so build first.

npx playwright install chromium --no-shell   # once
npm run build
npm test
npx playwright test -g bar                   # one spec by name
npm run test:report                          # the last run's HTML report
npm run gallery:record                       # write every case to test/render/output
npm run gallery                              # and browse them

If Playwright's Chromium does not run on your machine, set PLAYWRIGHT_BROWSER_PATH to any Chromium-based browser.

Releasing

npm run release -- <version> bumps the version, runs the checks and puts the release up as a pull request. Once that is merged, npm run release:tag tags it, and the tag starts the workflow that creates the GitHub release and publishes to npm. See Release.md.

Contributing

Contributions are welcome. Please make sure npm run typecheck, npm run lint and npm test pass. PRs are squash merged, so the PR title becomes the commit and has to be a conventional commit, like fix: ... or feat: ....

License

BSD-3-Clause, the same as Vega. See LICENSE.

About

WebGPU renderer for Vega visualizations

Topics

Resources

Code of conduct

Security policy

Stars

11 stars

Watchers

4 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages