Skip to content

Commit fa8233c

Browse files
committed
Add "wgpu on the web" blog post
1 parent efd20cb commit fa8233c

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

_posts/2020-04-21-wgpu-web.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
layout: post
3+
title: wgpu-rs on the web
4+
---
5+
6+
[gfx-rs](https://github.com/gfx-rs/gfx) is a Rust project aiming to make low-level GPU programming portable with low overhead. It's a single Vulkan-like Rust API with multiple backends that implement it: Direct3D 12/11, Metal, Vulkan, and even OpenGL.
7+
8+
[wgpu-rs](https://github.com/gfx-rs/wgpu-rs) is a Rust project on top of gfx-rs that provides safety, accessibility, and even stronger portability.
9+
10+
## Running wgpu-rs natively
11+
12+
As we previously discussed in [The rise of wgpu](http://gfx-rs.github.io/2019/03/06/wgpu.html), the gfx team has been busy with development of `wgpu-native` (a C API which implements the [WebGPU specification](https://gpuweb.github.io/gpuweb/)) and `wgpu-rs` (an idiomatic Rust wrapper around `wgpu-native`).
13+
14+
Even though these crates are still relatively new, it's been very exciting to watch people build all kinds of neat projects which use them, including:
15+
16+
- a [GUI library](https://github.com/hecrj/iced)
17+
- a [pixel editor](https://github.com/cloudhead/rx)
18+
- [tutorials](https://sotrh.github.io/learn-wgpu/)
19+
- [multiple](https://github.com/hecrj/coffee) [game](https://github.com/Ruddle/oxidator) [engines](https://github.com/StarArawn/harmony)
20+
- a [creative-coding toolkit](https://github.com/nannou-org/nannou)
21+
- a [sailing navigation application](https://github.com/Yatekii/sailor)
22+
- a [Brawl character file parser](https://github.com/rukai/brawllib_rs)
23+
- a [hardware-accelerated pixel buffer](https://github.com/parasyte/pixels)
24+
- [Python bindings](https://github.com/almarklein/wgpu-py)
25+
- a [Rust vangers clone](https://github.com/kvark/vange-rs)
26+
- a [Chinese calligraphy app](https://apps.apple.com/us/app/id1492608770)
27+
- [Scopes bindings](https://nest.pijul.com/porky11/wgpu)
28+
- and many more interesting projects!
29+
30+
There is even an [awesome-wgpu list](https://github.com/rofrol/awesome-wgpu) that the community has started in order to aggregate wgpu-related learning resources and examples in one place.
31+
32+
Because `wgpu-rs` is cross-platform, all of these projects can run natively on top of Vulkan, Metal, DX12, and DX11. Internally `wgpu-rs` automatically selects a graphics API for the user based on which graphics APIs are available at runtime, although users may specify which graphics API to use if they prefer. For older devices which don't support these modern APIs, limited GL support is also [actively being worked on](https://github.com/gfx-rs/wgpu/issues/450).
33+
34+
## Running wgpu-rs on the web
35+
36+
When we started the `wgpu-rs` crate, we wanted to eventually support two backends:
37+
38+
- a "native" backend implemented with `wgpu-native`/`gfx`
39+
- a "web" backend which uses the WebGPU API provided by the browser
40+
41+
Until recently, `wgpu-rs` was only able to run on the native backend, simply because the WebGPU API wasn't available in any browsers. But now that's changing, because browser vendors have been making [rapid progress on implementations of WebGPU](https://github.com/gpuweb/gpuweb/wiki/Implementation-Status#implementation-status).
42+
43+
With the WebGPU API beginning to become available in Firefox Nightly and Chrome Canary behind experimental flags, we decided it was time to add a web backend. We [mapped the existing `wgpu-rs` API to the browser API](https://github.com/gfx-rs/wgpu-rs/pull/193), compiled our examples using the `wasm32-unknown-unknown` target, and were quickly able to run our examples on the web:
44+
45+
![Boids](/img/wgpu-boids.png)
46+
47+
In the above screenshot, the same example is running inside of Firefox Nightly (top left), Chrome Canary (bottom left), and natively (right). [This example](https://github.com/gfx-rs/wgpu-rs/tree/master/examples/boids) uses a compute shader to [simulate flocking](https://en.wikipedia.org/wiki/Boids) and renders the result. macOS is used in the screenshot, but this example also runs on Windows and Linux without any code changes.
48+
49+
All `wgpu-rs` examples are currently available at <https://wgpu.rs/examples/>. [The WebGPU API has to be enabled in the browser](https://github.com/gfx-rs/wgpu-rs/pull/193#issuecomment-613182772) in order to run these examples. Note that browser WebGPU support is still experimental and a work in progress, so attempting to run them may cause the browser to crash or they may fail to run/render.
50+
51+
## Getting started with wgpu-rs on the web
52+
53+
To take advantage of the new web backend in `wgpu-rs` and run on the web, crates can be compiled using the `wasm32-unknown-unknown` target. Because the WebGPU API is experimental, for now an unstable APIs flag (`--cfg=web_sys_unstable_apis`) must be provided in the `RUSTFLAGS` environment variable in order to use WebGPU from `web-sys` (the crate which exposes web APIs to Rust).
54+
55+
After the crate has been compiled with `wasm32-unknown-unknown`, the WebAssembly can be executed within a browser. [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) can be used to generate JavaScript bindings, or the WebAssembly can be called manually.
56+
57+
For example, to compile the [`hello-triangle`](https://github.com/gfx-rs/wgpu-rs/tree/master/examples/hello-triangle) example and generate bindings with `wasm-bindgen`:
58+
59+
```bash
60+
# Build with the wasm target
61+
RUSTFLAGS=--cfg=web_sys_unstable_apis cargo build --target wasm32-unknown-unknown --example hello-triangle
62+
# Generate bindings with wasm-bindgen-cli into a `generated` directory
63+
cargo install -f wasm-bindgen-cli && wasm-bindgen --out-dir generated --web target/wasm32-unknown-unknown/debug/examples/hello-triangle.wasm
64+
```
65+
66+
Then add a simple `index.html` inside the `generated` directory to run the WebAssembly:
67+
68+
```html
69+
<html>
70+
<body>
71+
<script type="module">
72+
import init from "./hello-triangle.js";
73+
init();
74+
</script>
75+
</body>
76+
</html>
77+
```
78+
79+
Now run a web server from the `generated` directory to see `hello-triangle` in the browser:
80+
81+
![Hello Triangle](/img/wgpu-hello-triangle.png)
82+
83+
We are really excited to see `wgpu-rs` projects running on the web. Even though it's still early days for WebGPU, we look forward to the new possibilities in high-performance, portable graphics and compute that this will enable. Please let us know on [the wgpu-rs Matrix channel (#wgpu:matrix.org)](https://matrix.to/#/#wgpu:matrix.org) or [through the wgpu-rs issue tracker](https://github.com/gfx-rs/wgpu-rs/issues) if your project takes advantage of this new web backend.

img/wgpu-boids.png

298 KB
Loading

img/wgpu-hello-triangle.png

64.2 KB
Loading

0 commit comments

Comments
 (0)