Skip to content

Commit

Permalink
Create instance from worker (#2858)
Browse files Browse the repository at this point in the history
Co-authored-by: Connor Fitzgerald <[email protected]>
  • Loading branch information
JolifantoBambla and cwfitzgerald authored Oct 8, 2022
1 parent f57db15 commit 3cd9398
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ Add the `"wgsl"` feature, to enable WGSL shaders in `wgpu-core` and `wgpu`. Enab
- 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).
- 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).

#### WebGPU
- Use `log` instead of `println` in hello example by @JolifantoBambla in [#2858](https://github.com/gfx-rs/wgpu/pull/2858)

### Examples
- Log adapter info in hello example on wasm target by @JolifantoBambla in [#2858](https://github.com/gfx-rs/wgpu/pull/2858)

### Testing/Internal

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

#### WebGPU
- 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)

### Changes

#### General
Expand Down Expand Up @@ -302,7 +311,6 @@ Added items to the public API
- 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)

#### Hal

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

Expand Down
4 changes: 3 additions & 1 deletion wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ web-sys = { version = "0.3.60", features = [
"OffscreenCanvas",
"ImageBitmap",
"ImageBitmapRenderingContext",
"Window"
"Window",
"WorkerGlobalScope",
"WorkerNavigator"
] }
wasm-bindgen = "0.2.83"
js-sys = "0.3.60"
Expand Down
7 changes: 3 additions & 4 deletions wgpu/examples/hello/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ async fn run() {
let instance = wgpu::Instance::new(wgpu::Backends::all());
#[cfg(not(target_arch = "wasm32"))]
{
println!("Available adapters:");
log::info!("Available adapters:");
for a in instance.enumerate_adapters(wgpu::Backends::all()) {
println!(" {:?}", a.get_info())
log::info!(" {:?}", a.get_info())
}
}
instance
Expand All @@ -16,8 +16,7 @@ async fn run() {
.unwrap()
};

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

fn main() {
Expand Down
30 changes: 29 additions & 1 deletion wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,21 @@ impl Context {
}
}

// Represents the global object in the JavaScript context.
// 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.
// When called from the UI thread only `window` is defined whereas `worker` is only defined within a web worker context.
// See: https://github.com/rustwasm/gloo/blob/2c9e776701ecb90c53e62dec1abd19c2b70e47c7/crates/timers/src/callback.rs#L8-L40
#[wasm_bindgen]
extern "C" {
type Global;

#[wasm_bindgen(method, getter, js_name = Window)]
fn window(this: &Global) -> JsValue;

#[wasm_bindgen(method, getter, js_name = WorkerGlobalScope)]
fn worker(this: &Global) -> JsValue;
}

// The web doesn't provide any way to identify specific queue
// submissions. But Clippy gets concerned if we pass around `()` as if
// it were meaningful.
Expand Down Expand Up @@ -1051,7 +1066,20 @@ impl crate::Context for Context {
MakeSendFuture<wasm_bindgen_futures::JsFuture, fn(JsFutureResult) -> Option<crate::Error>>;

fn init(_backends: wgt::Backends) -> Self {
Context(web_sys::window().unwrap().navigator().gpu())
let global: Global = js_sys::global().unchecked_into();
let gpu = if !global.window().is_undefined() {
global.unchecked_into::<web_sys::Window>().navigator().gpu()
} else if !global.worker().is_undefined() {
global
.unchecked_into::<web_sys::WorkerGlobalScope>()
.navigator()
.gpu()
} else {
panic!(
"Accessing the GPU is only supported on the main thread or from a dedicated worker"
);
};
Context(gpu)
}

fn instance_create_surface(
Expand Down

0 comments on commit 3cd9398

Please sign in to comment.