From bdc4167cc9726a5db557402049103a528b1f4625 Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Wed, 6 Jul 2022 10:08:44 -0400 Subject: [PATCH 01/15] get wgpu from window or worker --- wgpu/src/backend/web.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index 54ebeadb8a..eae24ee657 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -1010,7 +1010,26 @@ impl crate::Context for Context { MakeSendFuture Option>; fn init(_backends: wgt::Backends) -> Self { - Context(web_sys::window().unwrap().navigator().gpu()) + // took that from 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; + } + let global: Global = js_sys::global().unchecked_into(); + let gpu = if !global.window().is_undefined() { + global.unchecked_into::().navigator().gpu() + } else if !global.worker().is_undefined() { + global.unchecked_into::().navigator().gpu() + } else { + panic!("Only supported in a browser or web worker"); + }; + Context(gpu) } fn instance_create_surface( From 3ad904d01c1531330f7e3e14a42ba771f6924d50 Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Wed, 6 Jul 2022 10:18:35 -0400 Subject: [PATCH 02/15] moved declaration of Global out of Context::init --- wgpu/src/backend/web.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index eae24ee657..da39f972cf 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -972,6 +972,18 @@ impl Context { } } +// took that from 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; +} + impl crate::Context for Context { type AdapterId = Sendable; type DeviceId = Sendable; @@ -1010,24 +1022,13 @@ impl crate::Context for Context { MakeSendFuture Option>; fn init(_backends: wgt::Backends) -> Self { - // took that from 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; - } let global: Global = js_sys::global().unchecked_into(); let gpu = if !global.window().is_undefined() { global.unchecked_into::().navigator().gpu() } else if !global.worker().is_undefined() { global.unchecked_into::().navigator().gpu() } else { - panic!("Only supported in a browser or web worker"); + panic!("Accessing the GPU is only supported on the main thread or from a dedicated worker"); }; Context(gpu) } From 40c877ec722f2ff5ac29b912cff842d92235f9fb Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Wed, 6 Jul 2022 10:28:18 -0400 Subject: [PATCH 03/15] add WorkerGlobalScope and WorkerNavigator to web_sys features used --- wgpu/Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 0e52132f7b..d4392547a8 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -280,7 +280,9 @@ web-sys = { version = "0.3.58", features = [ "OffscreenCanvas", "ImageBitmap", "ImageBitmapRenderingContext", - "Window" + "Window", + "WorkerGlobalScope", + "WorkerNavigator" ] } wasm-bindgen = "0.2.81" js-sys = "0.3.58" From 42c9829db3d8862bd8475a44e44ae033a257b4f5 Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Wed, 6 Jul 2022 10:30:46 -0400 Subject: [PATCH 04/15] update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c71c9da01..bf8a1d46e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,9 @@ Bottom level categories: ### Bug Fixes +#### WebGPU +- `Context::init()` now uses `web_sys::WorkerGlobalContext` to create a `wgpu::Instance` instead of trying to access the unavailable `web_sys::Window`. + #### DX12 - `DownlevelCapabilities::default()` now returns the `ANISOTROPIC_FILTERING` flag set to true so DX12 lists `ANISOTROPIC_FILTERING` as true again by @cwfitzgerald in [#2851](https://github.com/gfx-rs/wgpu/pull/2851) From 34b58518351766d5dcbbd273ad6c2d71e8e7746c Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Wed, 6 Jul 2022 13:19:44 -0400 Subject: [PATCH 05/15] log adapter info in wasm --- wgpu/examples/hello/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/wgpu/examples/hello/main.rs b/wgpu/examples/hello/main.rs index 1a9928d21b..11f28f05bd 100644 --- a/wgpu/examples/hello/main.rs +++ b/wgpu/examples/hello/main.rs @@ -7,7 +7,10 @@ async fn run() { .unwrap(); #[cfg(not(target_arch = "wasm32"))] - println!("{:?}", adapter.get_info()) + println!("{:?}", adapter.get_info()); + + #[cfg(target_arch = "wasm32")] + log::info!("{:?}", adapter.get_info()); } fn main() { From 0d0e0bc68622c5ad6b7d1e5e32a0d7f796f1b831 Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Wed, 6 Jul 2022 13:27:25 -0400 Subject: [PATCH 06/15] add change to hello, add pr link --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf8a1d46e5..35f9178fca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,7 +43,7 @@ Bottom level categories: ### Bug Fixes #### WebGPU -- `Context::init()` now uses `web_sys::WorkerGlobalContext` to create a `wgpu::Instance` instead of trying to access the unavailable `web_sys::Window`. +- `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 [#2587](https://github.com/gfx-rs/wgpu/pull/2587) #### DX12 - `DownlevelCapabilities::default()` now returns the `ANISOTROPIC_FILTERING` flag set to true so DX12 lists `ANISOTROPIC_FILTERING` as true again by @cwfitzgerald in [#2851](https://github.com/gfx-rs/wgpu/pull/2851) @@ -52,6 +52,8 @@ Bottom level categories: - 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) +### Examples +Log adapter info in hello example on wasm taret by @JolifantoBambla in [#2587](https://github.com/gfx-rs/wgpu/pull/2587) ## wgpu-0.13.1 (2022-07-02) From ea070da2a2bf8f3223e1328878a691b4e465fef8 Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Wed, 6 Jul 2022 14:18:12 -0400 Subject: [PATCH 07/15] change order of log statements for different architectures --- wgpu/examples/hello/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wgpu/examples/hello/main.rs b/wgpu/examples/hello/main.rs index 11f28f05bd..adfdb18d34 100644 --- a/wgpu/examples/hello/main.rs +++ b/wgpu/examples/hello/main.rs @@ -6,11 +6,11 @@ async fn run() { .await .unwrap(); - #[cfg(not(target_arch = "wasm32"))] - println!("{:?}", adapter.get_info()); - #[cfg(target_arch = "wasm32")] log::info!("{:?}", adapter.get_info()); + + #[cfg(not(target_arch = "wasm32"))] + println!("{:?}", adapter.get_info()) } fn main() { From e61a59cc504a4e72c08ebcf485935558797ce9e5 Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Wed, 6 Jul 2022 14:22:23 -0400 Subject: [PATCH 08/15] fix link to PR --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35f9178fca..bb52c36cc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,7 +43,7 @@ Bottom level categories: ### Bug Fixes #### WebGPU -- `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 [#2587](https://github.com/gfx-rs/wgpu/pull/2587) +- `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) #### DX12 - `DownlevelCapabilities::default()` now returns the `ANISOTROPIC_FILTERING` flag set to true so DX12 lists `ANISOTROPIC_FILTERING` as true again by @cwfitzgerald in [#2851](https://github.com/gfx-rs/wgpu/pull/2851) @@ -53,7 +53,7 @@ Bottom level categories: - 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) ### Examples -Log adapter info in hello example on wasm taret by @JolifantoBambla in [#2587](https://github.com/gfx-rs/wgpu/pull/2587) +Log adapter info in hello example on wasm taret by @JolifantoBambla in [#2858](https://github.com/gfx-rs/wgpu/pull/2858) ## wgpu-0.13.1 (2022-07-02) From 01dc5bc093a8f70073fc1082bfc31f00d0c9202f Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Wed, 6 Jul 2022 14:42:24 -0400 Subject: [PATCH 09/15] fix typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03740d83d0..8206f4a572 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,7 +63,7 @@ Bottom level categories: - 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) ### Examples -- Log adapter info in hello example on wasm taret by @JolifantoBambla in [#2858](https://github.com/gfx-rs/wgpu/pull/2858) +- Log adapter info in hello example on wasm target by @JolifantoBambla in [#2858](https://github.com/gfx-rs/wgpu/pull/2858) ## wgpu-0.13.1 (2022-07-02) From bb8ef85cf5cd61cc33c9b699cf617e8c231ddee6 Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Wed, 6 Jul 2022 14:44:25 -0400 Subject: [PATCH 10/15] fix link to PR --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8206f4a572..afbabc1b9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,7 +49,7 @@ Bottom level categories: - `get_texture_format_features` only lists the COPY_* usages if the adapter actually supports that usage by @cwfitzgerald in [#2856](https://github.com/gfx-rs/wgpu/pull/2856) #### WebGPU -- `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/g> +- `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) #### DX12 - `DownlevelCapabilities::default()` now returns the `ANISOTROPIC_FILTERING` flag set to true so DX12 lists `ANISOTROPIC_FILTERING` as true again by @cwfitzgerald in [#2851](https://github.com/gfx-rs/wgpu/pull/2851) From eddf67f5f423141788553577943f4bfdccee42da Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Wed, 6 Jul 2022 14:45:29 -0400 Subject: [PATCH 11/15] clarify change --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afbabc1b9f..eda0271b55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,7 +49,7 @@ Bottom level categories: - `get_texture_format_features` only lists the COPY_* usages if the adapter actually supports that usage by @cwfitzgerald in [#2856](https://github.com/gfx-rs/wgpu/pull/2856) #### WebGPU -- `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) +- 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) #### DX12 - `DownlevelCapabilities::default()` now returns the `ANISOTROPIC_FILTERING` flag set to true so DX12 lists `ANISOTROPIC_FILTERING` as true again by @cwfitzgerald in [#2851](https://github.com/gfx-rs/wgpu/pull/2851) From 6eb2d489d70d7580857619687449b78423a8dffc Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Fri, 8 Jul 2022 10:01:11 -0400 Subject: [PATCH 12/15] run rustfmt --- wgpu/src/backend/web.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index da39f972cf..9bed01ee6c 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -1026,9 +1026,14 @@ impl crate::Context for Context { let gpu = if !global.window().is_undefined() { global.unchecked_into::().navigator().gpu() } else if !global.worker().is_undefined() { - global.unchecked_into::().navigator().gpu() + global + .unchecked_into::() + .navigator() + .gpu() } else { - panic!("Accessing the GPU is only supported on the main thread or from a dedicated worker"); + panic!( + "Accessing the GPU is only supported on the main thread or from a dedicated worker" + ); }; Context(gpu) } From 5c7b4ca8f769e18d45cf08bb7d95a4ce6e6e5f06 Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Mon, 11 Jul 2022 10:57:14 -0400 Subject: [PATCH 13/15] clarify comments on Global type --- wgpu/src/backend/web.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index 9bed01ee6c..e466535e87 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -972,7 +972,10 @@ impl Context { } } -// took that from https://github.com/rustwasm/gloo/blob/2c9e776701ecb90c53e62dec1abd19c2b70e47c7/crates/timers/src/callback.rs#L8-L40 +// 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; From bbb1fe4b31c48b97cf97ccfa05f0aa02cda18950 Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Tue, 26 Jul 2022 09:53:20 -0400 Subject: [PATCH 14/15] fix changelog --- CHANGELOG.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31414107ed..30d0845e7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,11 +45,17 @@ Bottom level categories: #### General - Improve the validation and error reporting of buffer mappings by @nical in [#2848](https://github.com/gfx-rs/wgpu/pull/2848) +#### 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 #### Metal - Extract the generic code into `get_metal_layer` by @jinleili in [#2826](https://github.com/gfx-rs/wgpu/pull/2826) +### Examples +- Log adapter info in hello example on wasm target by @JolifantoBambla in [#2858](https://github.com/gfx-rs/wgpu/pull/2858) + ## wgpu-0.13.2 (2022-07-13) ### Bug Fixes @@ -64,9 +70,6 @@ Bottom level categories: - Add some validation in map_async by @nical in [#2876](https://github.com/gfx-rs/wgpu/pull/2876) - Fix bugs when mapping/unmapping zero-sized buffers and ranges by @nical in [#2877](https://github.com/gfx-rs/wgpu/pull/2877) -#### 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) - #### DX12 - `DownlevelCapabilities::default()` now returns the `ANISOTROPIC_FILTERING` flag set to true so DX12 lists `ANISOTROPIC_FILTERING` as true again by @cwfitzgerald in [#2851](https://github.com/gfx-rs/wgpu/pull/2851) - Properly query format features for UAV/SRV usages of depth formats by @cwfitzgerald in [#2856](https://github.com/gfx-rs/wgpu/pull/2856) @@ -91,9 +94,6 @@ Bottom level categories: #### Metal - Extract the generic code into `get_metal_layer` by @jinleili in [#2826](https://github.com/gfx-rs/wgpu/pull/2826) -### Examples -- Log adapter info in hello example on wasm target by @JolifantoBambla in [#2858](https://github.com/gfx-rs/wgpu/pull/2858) - ## wgpu-0.13.1 (2022-07-02) ### Bug Fixes From 1b4d89a35a961d8a81a712daaf3a6de2f1597140 Mon Sep 17 00:00:00 2001 From: Lukas Herzberger Date: Mon, 12 Sep 2022 14:56:12 -0400 Subject: [PATCH 15/15] fix duplicate line --- CHANGELOG.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 526b296220..5cc234d8f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -170,11 +170,6 @@ the same every time it is rendered, we now warn if it is missing. - 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) -### Changes - -#### Metal -- Extract the generic code into `get_metal_layer` by @jinleili in [#2826](https://github.com/gfx-rs/wgpu/pull/2826) - ## wgpu-0.13.1 (2022-07-02) ### Bug Fixes