Create instance from worker#2858
Conversation
|
the example I used to test this is here: gfx-rs/wgpu-rs.github.io#24 |
|
Hey, @cwfitzgerald, I fixed the formatting. Could you maybe try running the checks again? Thanks :) |
|
Re-run! If you see failures on nightly only, ignore them, it's an upstream issue. |
|
Thanks! Looks like it worked :) |
cwfitzgerald
left a comment
There was a problem hiding this comment.
One comment, but otherwise looks good.
|
Do you have an example with Rust code on how you use a web worker with wgpu? This is an interesting approach, but doesn't #1932 already handle this with offscreencanvas? When using a web worker, don't you need an offscreencanvas to begin with rendering? Where do you get it? Note that I am using 0.12 and maybe 0.13 does something different and I am overlooking something. Anyway, my approach with wgpu 0.12 would be to pass the offscreencanvas to a web worker and init wgpu with that. |
Checklist
cargo clippy.RUSTFLAGS=--cfg=web_sys_unstable_apis cargo clippy --target wasm32-unknown-unknownif applicable.Connections
#1986
#1466
#2652
Description
On the WebGPU backend,
Context::init()usesweb_sys::window()to accessnavigator.gpu.Since
windowis not available on web workers this function panics when called from a web worker (#1986).This is solved by introducing a
Globaltype the result ofjs_sys::global()is cast to.This instance of
Globalis then used to check whether or notwindow()is defined to determine if the function was called in a worker context or not. If it's in a worker context, it will useWorkerGlobalScopeinstead ofWindowto access thegpuproperty.I'm relatively new to rust and took the solution from here after stumbling upon this issue in wasm-bindgen, so it's maybe not the prettiest solution.
Let me know if you have a better solution in mind.
Testing
I added logging of
adapter.get_info()to thehelloexample and am about to submit another PR over at https://github.com/gfx-rs/wgpu-rs.github.io that adds an example running thehelloexample in a webworker.Note though, that this example doesn't work in Firefox since it doesn't support loading modules in workers. I tested it in Chromium on Linux.