Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm support #6

Closed
databasedav opened this issue May 17, 2024 · 6 comments
Closed

wasm support #6

databasedav opened this issue May 17, 2024 · 6 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@databasedav
Copy link
Contributor

just wanted to add some tracking for this

However, when the task executor is running on a single thread (on wasm, for example), the async world will probably deadlock. If this is a pain-point for you, please open a GitHub issue.

i believe this is blocked on this PR smol-rs/async-io#153, however, when i update the async-io dependency to

async-io = { git = "https://github.com/smol-rs/async-io", branch = "notgull/web" }

and try to run the basic example following these instructions https://bevy-cheatbook.github.io/platforms/wasm.html, i get the following on when navigating to the served url

on firefox

ERROR app: panicked at /home/avi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-0.19.1/src/lib.rs:1809:13:
No wgpu backend feature that is implemented for the target platform was enabled. See `wgpu::Instance::any_backend_feature_enabled()` for more information.

Stack:

__wbg_get_imports/imports.wbg.__wbg_new_abda76e883ba8a5f@http://127.0.0.1:1334/api/wasm.js:408:13
__wbg_new_abda76e883ba8a5f externref shim@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[233694]:0x4057f7c
console_error_panic_hook::Error::new::h63df324f7c858bae@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[166912]:0x3cf73a3
console_error_panic_hook::hook_impl::hf766909e468ef70e@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[32286]:0x26e69d3
console_error_panic_hook::hook::hfde64c16164fad88@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[203252]:0x3f23855
core::ops::function::Fn::call::ha1f82010650f515b@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[180579]:0x3de57b9
std::panicking::rust_panic_with_hook::h1e6ac5d404b8e31b@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[67732]:0x30cc5e0
std::panicking::begin_panic_handler::{{closure}}::h24b0f4622f2766a5@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[88845]:0x34a8105
std::sys_common::backtrace::__rust_end_short_backtrace::h19f35d272c126e7c@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[235506]:0x405deda
rust_begin_unwind@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[180984]:0x3dec01c
core::panicking::panic_fmt::h87755523850ece9e@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[195922]:0x3ec4dd7
wgpu::Instance::new::hcdd38eeb57f95ec1@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[73947]:0x320a7d2
<bevy_render::RenderPlugin as bevy_app::plugin::Plugin>::build::{{closure}}::h21bdd6de059d8d5f@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[473]:0x66f133
bevy_tasks::single_threaded_task_pool::TaskPool::spawn::{{closure}}::hac47b39232b39b8a@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[24864]:0x23c1a02
wasm_bindgen_futures::task::singlethread::Task::run::hd49527446ce517a3@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[23592]:0x231d706
wasm_bindgen_futures::queue::QueueState::run_all::h3ad72d2a6d6c3f02@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[15692]:0x1e637e4
wasm_bindgen_futures::queue::Queue::new::{{closure}}::hd683b5a3dbdbb7b9@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[110933]:0x37c4317
<dyn core::ops::function::FnMut<(A,)>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::hd548e900a55de4b4@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[69241]:0x311cd0d
closure40191 externref shim@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[233271]:0x4055a5b
__wbg_adapter_57@http://127.0.0.1:1334/api/wasm.js:222:6
real@http://127.0.0.1:1334/api/wasm.js:171:32

on chrome

ERROR app: panicked at /home/avi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-0.19.1/src/lib.rs:1809:13:
No wgpu backend feature that is implemented for the target platform was enabled. See `wgpu::Instance::any_backend_feature_enabled()` for more information.

Stack:

Error
    at imports.wbg.__wbg_new_abda76e883ba8a5f (http://127.0.0.1:1334/api/wasm.js:408:13)
    at __wbg_new_abda76e883ba8a5f externref shim (http://127.0.0.1:1334/api/wasm.wasm:wasm-function[233694]:0x4057f7c)
    at console_error_panic_hook::Error::new::h63df324f7c858bae (http://127.0.0.1:1334/api/wasm.wasm:wasm-function[166912]:0x3cf73a3)
    at console_error_panic_hook::hook_impl::hf766909e468ef70e (http://127.0.0.1:1334/api/wasm.wasm:wasm-function[32286]:0x26e69d3)
    at console_error_panic_hook::hook::hfde64c16164fad88 (http://127.0.0.1:1334/api/wasm.wasm:wasm-function[203252]:0x3f23855)
    at core::ops::function::Fn::call::ha1f82010650f515b (http://127.0.0.1:1334/api/wasm.wasm:wasm-function[180579]:0x3de57b9)
    at std::panicking::rust_panic_with_hook::h1e6ac5d404b8e31b (http://127.0.0.1:1334/api/wasm.wasm:wasm-function[67732]:0x30cc5e0)
    at std::panicking::begin_panic_handler::{{closure}}::h24b0f4622f2766a5 (http://127.0.0.1:1334/api/wasm.wasm:wasm-function[88845]:0x34a8105)
    at std::sys_common::backtrace::__rust_end_short_backtrace::h19f35d272c126e7c (http://127.0.0.1:1334/api/wasm.wasm:wasm-function[235506]:0x405deda)
    at rust_begin_unwind (http://127.0.0.1:1334/api/wasm.wasm:wasm-function[180984]:0x3dec01c)

i'm not sure if this is actually relevant to this crate, or i'm just doing something wrong, also wasn't able to find anything about the wgpu backend feature stuff

@dlom
Copy link
Owner

dlom commented Jun 4, 2024

Thank you for your experiments. I will attempt to enable WASM support likely after bevy 0.14 releases in the near future.

@dlom dlom added enhancement New feature or request help wanted Extra attention is needed labels Jun 8, 2024
@databasedav
Copy link
Contributor Author

databasedav commented Aug 15, 2024

after some investigation, i've discovered that the CommandQueueSender contained by the AsyncWorld is closed within the body of the this future

let async_world = AsyncWorld::from_world(world);
let fut = async move {
let print_names = async_world.register_system(print_names).await;
let entity = async_world.spawn_named("Frank").await;
print_names.run().await;
entity.despawn().await;
info!("done! you can close the window");
};
AsyncComputeTaskPool::get().spawn(fut).detach();
on wasm, but not on linux

@databasedav
Copy link
Contributor Author

should be reproducible on this branch https://github.com/databasedav/bevy-async-ecs/tree/wasm_experimenting

run with these

rustup target install wasm32-unknown-unknown
cargo install wasm-server-runner
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=wasm-server-runner cargo run --target wasm32-unknown-unknown --example basic

and navigate to the served url

note i also disabled a bunch of bevy features in the dev dependencies here https://github.com/databasedav/bevy-async-ecs/blob/2db817e65142800347897f9783ccef7bbfb75cf9/Cargo.toml#L23-L36, i think winit might have been causing a separate issue

@dlom
Copy link
Owner

dlom commented Aug 15, 2024

Thanks for your effort, I will try to reproduce myself soon 👍

@Catchawink
Copy link

Catchawink commented Oct 27, 2024

I was able to fix this! On the wasm_experimenting branch, modify plugins in the basic example to this:

App::new()
     .add_plugins((MinimalPlugins, LogPlugin::default(), AsyncEcsPlugin))

This appears to resolve a blocking issue (probably adding individual systems from DefaultPlugins could help narrow this down).

There was also a separate blocking issue in bevy-async-ecs's recv_and_yield function. This rewrite seems to work:

async fn recv_and_yield<T: Send>(receiver: Receiver<T>) -> T {
     receiver.recv().await.unwrap()
}

The previous implementation used pin!(), poll_once() and yield_now() from futures_lite. I'm not super familiar pinning so this reimplementation may cause issues. Also there might be some incompatibility between the futures_lite crate and WASM which led to this issue--but I'm not sure.

@dlom dlom closed this as completed in f064334 Oct 30, 2024
@dlom
Copy link
Owner

dlom commented Oct 30, 2024

@Catchawink thank you for your diagnoses. futures-lite and the recv_and_yield function were legacy workarounds for a problem that no longer exists. Wasm support will soon be publish to crates.io!

0.15 update will come shortly after full release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants