forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hacky tweaks to get Bevy closer to Wasm threading support
- Loading branch information
Showing
8 changed files
with
106 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use wasm_bindgen::prelude::*; | ||
|
||
pub struct WasmWorkerBuilder { | ||
stack_size: usize, | ||
} | ||
|
||
impl WasmWorkerBuilder { | ||
pub fn new() -> Self { | ||
Self { stack_size: 65536 } | ||
} | ||
|
||
pub fn name(self, _name: String) -> Self { | ||
self | ||
} | ||
|
||
pub fn stack_size(self, size: usize) -> Self { | ||
Self { stack_size: size } | ||
} | ||
|
||
pub fn spawn<F: FnOnce() + Send + 'static>(self, f: F) -> Result<WebWorkerJoinHandle, JsValue> { | ||
let worker = web_sys::Worker::new_with_options( | ||
"./worker.js", | ||
web_sys::WorkerOptions::new().type_(web_sys::WorkerType::Module), | ||
)?; | ||
// Double-boxing because `dyn FnOnce` is unsized and so `Box<dyn FnOnce()>` is a fat pointer. | ||
// But `Box<Box<dyn FnOnce()>>` is just a plain pointer, and since wasm has 32-bit pointers, | ||
// we can cast it to a `u32` and back. | ||
let ptr = Box::into_raw(Box::new(Box::new(f) as Box<dyn FnOnce()>)); | ||
let msg = js_sys::Array::new(); | ||
|
||
msg.push(&wasm_bindgen::module()); | ||
msg.push(&wasm_bindgen::memory()); | ||
|
||
// Send the address of the closure to execute. | ||
msg.push(&JsValue::from(ptr as u32)); | ||
worker.post_message(&msg); | ||
Ok(WebWorkerJoinHandle {}) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct WebWorkerJoinHandle {} | ||
|
||
impl WebWorkerJoinHandle { | ||
pub fn join(self) -> Result<(), ()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
/// This function is here for `worker.js` to call. | ||
pub fn bevy_worker_entry_point(addr: u32) { | ||
// Interpret the address we were given as a pointer to a closure to call. | ||
let closure = unsafe { Box::from_raw(addr as *mut Box<dyn FnOnce()>) }; | ||
(*closure)(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import wasm_bindgen from "./target/wasm_example.js"; | ||
|
||
self.onmessage = async event => { | ||
const initialized = await wasm_bindgen( | ||
event.data[0], // Module | ||
event.data[1] // Memoryx | ||
); | ||
|
||
console.log("INITIALIZED: ", initialized); | ||
initialized.bevy_worker_entry_point(Number(event.data[2])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals' \ | ||
cargo build --example breakout --target wasm32-unknown-unknown -Z build-std=std,panic_abort | ||
wasm-bindgen --out-name wasm_example \ | ||
--out-dir examples/wasm/target \ | ||
--target web target/wasm32-unknown-unknown/debug/examples/breakout.wasm | ||
devserver --header Cross-Origin-Opener-Policy='same-origin' --header Cross-Origin-Embedder-Policy='require-corp' --path examples/wasm |