Skip to content

Commit

Permalink
Some WASM changes - including a run_wasm alias (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
DJMcNab authored Jan 31, 2023
1 parent f84e244 commit 9721d4a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[alias]
run_wasm = "run --release --package run_wasm --"
# Other crates use the alias run-wasm, even though crate names should use `_`s not `-`s
# Allow this to be used
run-wasm = "run_wasm"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
target/
/target
Cargo.lock
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ cargo run -p with_bevy
Because Vello relies heavily on compute shaders, we rely on the emerging WebGPU standard to run on the web.
Until browser support becomes widespread, it will probably be necessary to use development browser versions (e.g. Chrome Canary) and explicitly enable WebGPU.

The following command builds and runs a web version of the [winit demo](#winit).
Note: Other examples use the `-p` shorthand, but `cargo-run-wasm` requires the full `--package` to be specified

The following command builds and runs a web version of the [winit demo](#winit).
This uses [`cargo-run-wasm`](https://github.com/rukai/cargo-run-wasm) to build the example for web, and host a local server for it:

```shell
cargo run --release -p run-wasm -- --package with_winit
cargo run_wasm --package with_winit
```

Additionally, the web is not currently a primary target, so other issues are likely to arise.
The web is not currently a primary target for vello, and WebGPU implementations are incomplete, so you might run into issues running this example.

## Community

Expand Down
5 changes: 3 additions & 2 deletions examples/run_wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[package]
name = "run-wasm"
name = "run_wasm"
version.workspace = true
edition.workspace = true
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cargo-run-wasm = "0.2.0"
# We use cargo-run-wasm main because of our workspace layout
cargo-run-wasm = { git = "https://github.com/rukai/cargo-run-wasm", rev = "d14f73de77eaae44714b4817d660026a31f5f5a9" }
18 changes: 18 additions & 0 deletions examples/run_wasm/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/// Use [cargo-run-wasm](https://github.com/rukai/cargo-run-wasm) to build an example for web
///
/// Usage:
/// ```
/// cargo run_wasm --package [example_name]
/// ```
/// Generally:
/// ```
/// cargo run_wasm --package with_winit
/// ```
fn main() {
// HACK: We rely heavily on compute shaders; which means we need WebGPU to be supported
// However, that requires unstable APIs to be enabled, which are not exposed through a feature
let current_value = std::env::var("RUSTFLAGS").unwrap_or("".to_owned());
std::env::set_var(
"RUSTFLAGS",
format!("{current_value} --cfg=web_sys_unstable_apis",),
);
cargo_run_wasm::run_wasm_with_css("body { margin: 0px; }");
}
4 changes: 2 additions & 2 deletions examples/with_winit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod pico_svg;
mod simple_text;
mod test_scene;

use std::{borrow::Cow, path::PathBuf, time::Instant};
use std::{borrow::Cow, time::Instant};

use clap::Parser;
use vello::{
Expand All @@ -41,7 +41,7 @@ struct Args {
/// Path to the svg file to render. If not set, the GhostScript Tiger will be rendered
#[arg(long)]
#[cfg(not(target_arch = "wasm32"))]
svg: Option<PathBuf>,
svg: Option<std::path::PathBuf>,
/// When rendering an svg, what scale to use
#[arg(long)]
scale: Option<f64>,
Expand Down
4 changes: 1 addition & 3 deletions examples/with_winit/src/test_scene.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::time::Instant;

use crate::pico_svg::PicoSvg;
use crate::simple_text::SimpleText;
use vello::kurbo::{Affine, BezPath, Ellipse, PathEl, Point, Rect};
Expand Down Expand Up @@ -83,7 +81,7 @@ pub fn render_svg_scene(
let scene_frag = scene.get_or_insert_with(|| {
use super::pico_svg::*;
#[cfg(not(target_arch = "wasm32"))]
let start = Instant::now();
let start = std::time::Instant::now();
eprintln!("Starting to parse svg");
let svg = PicoSvg::load(svg, scale).unwrap();
#[cfg(not(target_arch = "wasm32"))]
Expand Down

0 comments on commit 9721d4a

Please sign in to comment.