Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create instance from worker #2858

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
bdc4167
get wgpu from window or worker
JolifantoBambla Jul 6, 2022
3ad904d
moved declaration of Global out of Context::init
JolifantoBambla Jul 6, 2022
40c877e
add WorkerGlobalScope and WorkerNavigator to web_sys features used
JolifantoBambla Jul 6, 2022
42c9829
update changelog
JolifantoBambla Jul 6, 2022
34b5851
log adapter info in wasm
JolifantoBambla Jul 6, 2022
0d0e0bc
add change to hello, add pr link
JolifantoBambla Jul 6, 2022
ea070da
change order of log statements for different architectures
JolifantoBambla Jul 6, 2022
e61a59c
fix link to PR
JolifantoBambla Jul 6, 2022
04f6fab
merge master, fix conflicts in CHANGELOG
JolifantoBambla Jul 6, 2022
01dc5bc
fix typo
JolifantoBambla Jul 6, 2022
bb8ef85
fix link to PR
JolifantoBambla Jul 6, 2022
eddf67f
clarify change
JolifantoBambla Jul 6, 2022
6eb2d48
run rustfmt
JolifantoBambla Jul 8, 2022
89ae445
merge upstream, resolve conflicts
JolifantoBambla Jul 11, 2022
5c7b4ca
clarify comments on Global type
JolifantoBambla Jul 11, 2022
560d8fd
merge upstream, fix conflicts
JolifantoBambla Jul 13, 2022
5eb3a3e
merge upstream, resolve conflicts
JolifantoBambla Jul 14, 2022
bbb1fe4
fix changelog
JolifantoBambla Jul 26, 2022
623f671
merge upstream, resolve conflicts
JolifantoBambla Jul 26, 2022
2168024
merge upstream, fix conflicts
JolifantoBambla Aug 15, 2022
29cd18e
merge upstream, resolve conflicts
JolifantoBambla Aug 23, 2022
a7efc8c
merge master, resolve conflicts, use log instead of println in hello …
JolifantoBambla Sep 12, 2022
1b4d89a
fix duplicate line
JolifantoBambla Sep 12, 2022
2707214
Merge branch 'master' into create-instance-from-worker
cwfitzgerald Oct 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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).

#### 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 @@ -150,6 +156,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 @@ -301,7 +310,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