gfx-rs / wgpu-native

Native WebGPU implementation based on wgpu-core
Apache License 2.0
880 stars 105 forks source link

Building for `wasm32-unknown-unknown`: cannot find type `OffscreenCanvas` in crate `web_sys` #207

Closed spindlebink closed 2 years ago

spindlebink commented 2 years ago

When attempting to build wgpu-native for WASM (--target=wasm32-unknown-unknown, but it happens with the other WASM targets as well), building fails with "cannot find type OffscreenCanvas in crate web_sys" while building wgpu-hal.

I am attempting to compile wgpu-native to use in a game framework which will ideally target WASM, and I'm not completely informed on WASM and compiling Rust to it, so it's also possible that I'm going about things all wrong. In which case is there documentation somewhere for using wgpu-native as a library in a WASM context?

grovesNL commented 2 years ago

Hi @spindlebink! :wave:

Normally when targeting wasm32-unknown-unknown, most Rust projects are using wgpu directly instead of wgpu-native. This avoids the issue that you're running into.

There does seem to be a bug in wgpu that you're running into though: wgpu should make sure types such as OffscreenCanvas are available. I just added it to wgpu in https://github.com/gfx-rs/wgpu/issues/2894

In the meantime, you could manually add web_sys as a dependency to your Cargo.toml and enable the necessary features. The easiest way could be to copy and paste the complete list from wgpu: https://github.com/gfx-rs/wgpu/blob/cb0646f8da8280a357001f13da8a336578252947/wgpu/Cargo.toml#L160-L284 (or optionally just enable OffscreenCanvas and other types that the build complains about)

spindlebink commented 2 years ago

I'm using WGPU in a non-Rust context and wanting to target WASM, hence WGPU-native instead of the Rust interface, so I don't actually have a crate that I could do that in except editing WGPU-native's. I suppose I could make one to build WGPU-native with the necessary dependencies and build on top of that instead of building this one, but that seems like a bit much for such a little issue. At any rate, since this is a WGPU-side issue rather than strictly a native one, I'll keep tracking on that side and close this one. Thanks for your help!