Skip to content

Commit 3cd9398

Browse files
Create instance from worker (#2858)
Co-authored-by: Connor Fitzgerald <[email protected]>
1 parent f57db15 commit 3cd9398

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ Add the `"wgsl"` feature, to enable WGSL shaders in `wgpu-core` and `wgpu`. Enab
5858
- Bother to free the `hal::Api::CommandBuffer` when a `wgpu_core::command::CommandEncoder` is dropped. By @jimblandy in [#3069](https://github.com/gfx-rs/wgpu/pull/3069).
5959
- Fixed the mipmap example by adding the missing WRITE_TIMESTAMP_INSIDE_PASSES feature. By @Olaroll in [#3081](https://github.com/gfx-rs/wgpu/pull/3081).
6060

61+
#### WebGPU
62+
- Use `log` instead of `println` in hello example by @JolifantoBambla in [#2858](https://github.com/gfx-rs/wgpu/pull/2858)
63+
64+
### Examples
65+
- Log adapter info in hello example on wasm target by @JolifantoBambla in [#2858](https://github.com/gfx-rs/wgpu/pull/2858)
66+
6167
### Testing/Internal
6268

6369
- Update the `minimum supported rust version` to 1.62
@@ -151,6 +157,9 @@ both `raw_window_handle::HasRawWindowHandle` and `raw_window_handle::HasRawDispl
151157
- Report vendor id for Mesa and Apple GPUs. By @i509VCB [#3036](https://github.com/gfx-rs/wgpu/pull/3036)
152158
- Report Apple M2 gpu as integrated. By @i509VCB [#3036](https://github.com/gfx-rs/wgpu/pull/3036)
153159

160+
#### WebGPU
161+
- When called in a web worker, `Context::init()` now uses `web_sys::WorkerGlobalContext` to create a `wgpu::Instance` instead of trying to access the unavailable `web_sys::Window` by @JolifantoBambla in [#2858](https://github.com/gfx-rs/wgpu/pull/2858)
162+
154163
### Changes
155164

156165
#### General
@@ -302,7 +311,6 @@ Added items to the public API
302311
- Update present_mode docs as most of them don't automatically fall back to Fifo anymore. by @Elabajaba in [#2855](https://github.com/gfx-rs/wgpu/pull/2855)
303312

304313
#### Hal
305-
306314
- Document safety requirements for `Adapter::from_external` in gles hal by @i509VCB in [#2863](https://github.com/gfx-rs/wgpu/pull/2863)
307315
- Make `AdapterContext` a publicly accessible type in the gles hal by @i509VCB in [#2870](https://github.com/gfx-rs/wgpu/pull/2870)
308316

wgpu/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ web-sys = { version = "0.3.60", features = [
286286
"OffscreenCanvas",
287287
"ImageBitmap",
288288
"ImageBitmapRenderingContext",
289-
"Window"
289+
"Window",
290+
"WorkerGlobalScope",
291+
"WorkerNavigator"
290292
] }
291293
wasm-bindgen = "0.2.83"
292294
js-sys = "0.3.60"

wgpu/examples/hello/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ async fn run() {
55
let instance = wgpu::Instance::new(wgpu::Backends::all());
66
#[cfg(not(target_arch = "wasm32"))]
77
{
8-
println!("Available adapters:");
8+
log::info!("Available adapters:");
99
for a in instance.enumerate_adapters(wgpu::Backends::all()) {
10-
println!(" {:?}", a.get_info())
10+
log::info!(" {:?}", a.get_info())
1111
}
1212
}
1313
instance
@@ -16,8 +16,7 @@ async fn run() {
1616
.unwrap()
1717
};
1818

19-
#[cfg(not(target_arch = "wasm32"))]
20-
println!("Selected adapter: {:?}", adapter.get_info())
19+
log::info!("Selected adapter: {:?}", adapter.get_info())
2120
}
2221

2322
fn main() {

wgpu/src/backend/web.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,21 @@ impl Context {
10071007
}
10081008
}
10091009

1010+
// Represents the global object in the JavaScript context.
1011+
// It can be cast to from `web_sys::global` and exposes two getters `window` and `worker` of which only one is defined depending on the caller's context.
1012+
// When called from the UI thread only `window` is defined whereas `worker` is only defined within a web worker context.
1013+
// See: https://github.com/rustwasm/gloo/blob/2c9e776701ecb90c53e62dec1abd19c2b70e47c7/crates/timers/src/callback.rs#L8-L40
1014+
#[wasm_bindgen]
1015+
extern "C" {
1016+
type Global;
1017+
1018+
#[wasm_bindgen(method, getter, js_name = Window)]
1019+
fn window(this: &Global) -> JsValue;
1020+
1021+
#[wasm_bindgen(method, getter, js_name = WorkerGlobalScope)]
1022+
fn worker(this: &Global) -> JsValue;
1023+
}
1024+
10101025
// The web doesn't provide any way to identify specific queue
10111026
// submissions. But Clippy gets concerned if we pass around `()` as if
10121027
// it were meaningful.
@@ -1051,7 +1066,20 @@ impl crate::Context for Context {
10511066
MakeSendFuture<wasm_bindgen_futures::JsFuture, fn(JsFutureResult) -> Option<crate::Error>>;
10521067

10531068
fn init(_backends: wgt::Backends) -> Self {
1054-
Context(web_sys::window().unwrap().navigator().gpu())
1069+
let global: Global = js_sys::global().unchecked_into();
1070+
let gpu = if !global.window().is_undefined() {
1071+
global.unchecked_into::<web_sys::Window>().navigator().gpu()
1072+
} else if !global.worker().is_undefined() {
1073+
global
1074+
.unchecked_into::<web_sys::WorkerGlobalScope>()
1075+
.navigator()
1076+
.gpu()
1077+
} else {
1078+
panic!(
1079+
"Accessing the GPU is only supported on the main thread or from a dedicated worker"
1080+
);
1081+
};
1082+
Context(gpu)
10551083
}
10561084

10571085
fn instance_create_surface(

0 commit comments

Comments
 (0)