Manually check if web gpu support is available (#1372)

* Manually check if web gpu support is available

* Add web-sys features

* Disable use of unstable apis for non wasm targets
This commit is contained in:
Dennis Kobert 2023-08-08 14:04:01 +02:00 committed by GitHub
parent 48e3f7c54a
commit 5b6a74eb38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -77,6 +77,8 @@ features = [
"CanvasRenderingContext2d",
"ImageData",
"Document",
"Navigator",
"Gpu",
"HtmlCanvasElement",
"ImageBitmapRenderingContext",
]

View File

@ -40,11 +40,25 @@ pub struct WasmApplicationIo {
impl WasmApplicationIo {
pub async fn new() -> Self {
#[cfg(all(feature = "wgpu", target_arch = "wasm32"))]
let executor = if let Some(gpu) = web_sys::window().map(|w| w.navigator().gpu()) {
let request_adapter = js_sys::Reflect::get(&gpu, &wasm_bindgen::JsValue::from_str("requestAdapter")).unwrap();
let function = js_sys::Function::try_from(&request_adapter).unwrap();
let result = function.call0(&gpu);
match result {
Err(_) => None,
Ok(_) => WgpuExecutor::new().await,
}
} else {
None
};
#[cfg(all(feature = "wgpu", not(target_arch = "wasm32")))]
let executor = None;
let mut io = Self {
#[cfg(target_arch = "wasm32")]
ids: RefCell::new(0),
#[cfg(feature = "wgpu")]
gpu_executor: WgpuExecutor::new().await,
gpu_executor: executor,
#[cfg(not(target_arch = "wasm32"))]
windows: RefCell::new(Vec::new()),
resources: HashMap::new(),