mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-26 09:21:04 +01:00
Create instance from worker (#2858)
Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
This commit is contained in:
1 parent
f57db150f2
commit
3cd93983b2
4 files changed
+44
-7
No files matched your search
+9
-1
@@ -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
|
||||
@@ -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
|
||||
@@ -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)
|
||||
|
||||
|
||||
+3
-1
@@ -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"
|
||||
|
||||
@@ -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
|
||||
@@ -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() {
|
||||
|
||||
+29
-1
@@ -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.
|
||||
@@ -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(
|
||||
|
||||
Reference in new issue
Block a user