bevy/.github/workflows
Ian Kettlewell b35974010b
Get Bevy building for WebAssembly with multithreading (#12205)
# Objective

This gets Bevy building on Wasm when the `atomics` flag is enabled. This
does not yet multithread Bevy itself, but it allows Bevy users to use a
crate like `wasm_thread` to spawn their own threads and manually
parallelize work. This is a first step towards resolving #4078 . Also
fixes #9304.

This provides a foothold so that Bevy contributors can begin to think
about multithreaded Wasm's constraints and Bevy can work towards changes
to get the engine itself multithreaded.

Some flags need to be set on the Rust compiler when compiling for Wasm
multithreading. Here's what my build script looks like, with the correct
flags set, to test out Bevy examples on web:

```bash
set -e
RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals' \
     cargo build --example breakout --target wasm32-unknown-unknown -Z build-std=std,panic_abort --release
 wasm-bindgen --out-name wasm_example \
   --out-dir examples/wasm/target \
   --target web target/wasm32-unknown-unknown/release/examples/breakout.wasm
 devserver --header Cross-Origin-Opener-Policy='same-origin' --header Cross-Origin-Embedder-Policy='require-corp' --path examples/wasm
```

A few notes:

1. `cpal` crashes immediately when the `atomics` flag is set. That is
patched in https://github.com/RustAudio/cpal/pull/837, but not yet in
the latest crates.io release.

That can be temporarily worked around by patching Cpal like so:
```toml
[patch.crates-io]
cpal = { git = "https://github.com/RustAudio/cpal" }
```

2. When testing out `wasm_thread` you need to enable the `es_modules`
feature.

## Solution

The largest obstacle to compiling Bevy with `atomics` on web is that
`wgpu` types are _not_ Send and Sync. Longer term Bevy will need an
approach to handle that, but in the near term Bevy is already configured
to be single-threaded on web.

Therefor it is enough to wrap `wgpu` types in a
`send_wrapper::SendWrapper` that _is_ Send / Sync, but panics if
accessed off the `wgpu` thread.

---

## Changelog

- `wgpu` types that are not `Send` are wrapped in
`send_wrapper::SendWrapper` on Wasm + 'atomics'
- CommandBuffers are not generated in parallel on Wasm + 'atomics'

## Questions
- Bevy should probably add CI checks to make sure this doesn't regress.
Should that go in this PR or a separate PR? **Edit:** Added checks to
build Wasm with atomics

---------

Co-authored-by: François <mockersf@gmail.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: daxpedda <daxpedda@gmail.com>
Co-authored-by: François <francois.mockers@vleue.com>
2024-03-25 19:10:18 +00:00
..
action-on-PR-labeled.yml Bump actions/github-script from 6 to 7 (#10653) 2023-11-20 09:01:27 +00:00
ci-comment-failures.yml Bump actions/github-script from 6 to 7 (#10653) 2023-11-20 09:01:27 +00:00
ci.yml Get Bevy building for WebAssembly with multithreading (#12205) 2024-03-25 19:10:18 +00:00
daily.yml Bump actions/upload-artifact from 2 to 4 (#11014) 2023-12-24 00:34:02 +00:00
dependencies.yml add cancel in progress support to automated CI checks (#11130) 2024-02-28 16:07:29 +00:00
docs.yml Make feature(doc_auto_cfg) work (#12642) 2024-03-23 02:22:52 +00:00
post-release.yml Bump peter-evans/create-pull-request from 5 to 6 (#11712) 2024-02-09 06:01:31 +00:00
release.yml Bump peter-evans/create-pull-request from 5 to 6 (#11712) 2024-02-09 06:01:31 +00:00
validation-jobs.yml check that patches are not broken (#12457) 2024-03-17 19:03:55 +00:00
welcome.yml Bump actions/github-script from 6 to 7 (#10653) 2023-11-20 09:01:27 +00:00