From 0b8780a9b314b0e87c36e3b741c5bb4fa7ecacd8 Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Fri, 21 Apr 2023 15:57:03 +0200 Subject: [PATCH] Make wgpu webgl/gles opt-in (but still work out of the box via feature flag), workaround canvas creation issue --- Cargo.lock | 13 ++---------- crates/eframe/Cargo.toml | 5 +++-- crates/eframe/src/epi/mod.rs | 4 ++-- crates/eframe/src/web/web_painter_wgpu.rs | 24 +++++++++++++++++++++-- crates/egui-wgpu/src/lib.rs | 2 ++ 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4811a0d55af6..df8f811ae7ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -921,12 +921,6 @@ dependencies = [ "typenum", ] -[[package]] -name = "cty" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" - [[package]] name = "custom_3d_glow" version = "0.1.0" @@ -2948,12 +2942,9 @@ checksum = "63e935c45e09cc6dcf00d2f0b2d630a58f4095320223d47fc68918722f0538b6" [[package]] name = "raw-window-handle" -version = "0.5.0" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed7e3d950b66e19e0c372f3fa3fbbcf85b1746b571f74e0c2af6042a5c93420a" -dependencies = [ - "cty", -] +checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9" [[package]] name = "rctree" diff --git a/crates/eframe/Cargo.toml b/crates/eframe/Cargo.toml index a7a52c137644..5eee9709f648 100644 --- a/crates/eframe/Cargo.toml +++ b/crates/eframe/Cargo.toml @@ -62,7 +62,7 @@ __screenshot = [] ## Use [`wgpu`](https://docs.rs/wgpu) for painting (via [`egui-wgpu`](https://github.com/emilk/egui/tree/master/crates/egui-wgpu)). ## This overrides the `glow` feature. -wgpu = ["dep:wgpu", "dep:egui-wgpu", "dep:pollster"] +wgpu = ["dep:wgpu", "dep:egui-wgpu", "dep:pollster", "dep:raw-window-handle"] # Allow crates to choose an android-activity backend via Winit # - It's important that most applications should not have to depend on android-activity directly, and can @@ -181,5 +181,6 @@ web-sys = { version = "0.3.58", features = [ # optional web: egui-wgpu = { version = "0.21.0", path = "../egui-wgpu", optional = true } # if wgpu is used, use it without (!) winit +raw-window-handle = { version = "0.5.2", optional = true } tts = { version = "0.25", optional = true, default-features = false } -wgpu = { version = "0.16.0", optional = true, features = ["webgl"] } +wgpu = { version = "0.16.0", optional = true } diff --git a/crates/eframe/src/epi/mod.rs b/crates/eframe/src/epi/mod.rs index 33ef0f75556d..75804980123f 100644 --- a/crates/eframe/src/epi/mod.rs +++ b/crates/eframe/src/epi/mod.rs @@ -498,8 +498,8 @@ impl Default for WebOptions { #[cfg(feature = "wgpu")] wgpu_options: egui_wgpu::WgpuConfiguration { - // WebGPU is not stable enough yet, use WebGL emulation - backends: wgpu::Backends::GL, + // Use WebGPU or WebGL. Note that WebGL needs to be opted in via a wgpu feature. + backends: wgpu::Backends::BROWSER_WEBGPU | wgpu::Backends::GL, device_descriptor: wgpu::DeviceDescriptor { label: Some("egui wgpu device"), features: wgpu::Features::default(), diff --git a/crates/eframe/src/web/web_painter_wgpu.rs b/crates/eframe/src/web/web_painter_wgpu.rs index 3daa3bdc0f7a..27f78164a698 100644 --- a/crates/eframe/src/web/web_painter_wgpu.rs +++ b/crates/eframe/src/web/web_painter_wgpu.rs @@ -10,6 +10,22 @@ use crate::WebOptions; use super::web_painter::WebPainter; +struct EguiWebWindow(u32); + +unsafe impl raw_window_handle::HasRawWindowHandle for EguiWebWindow { + fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle { + let mut window_handle = raw_window_handle::WebWindowHandle::empty(); + window_handle.id = self.0; + raw_window_handle::RawWindowHandle::Web(window_handle) + } +} + +unsafe impl raw_window_handle::HasRawDisplayHandle for EguiWebWindow { + fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle { + raw_window_handle::RawDisplayHandle::Web(raw_window_handle::WebDisplayHandle::empty()) + } +} + pub(crate) struct WebPainterWgpu { canvas: HtmlCanvasElement, canvas_id: String, @@ -59,14 +75,18 @@ impl WebPainterWgpu { pub async fn new(canvas_id: &str, options: &WebOptions) -> Result { log::debug!("Creating wgpu painter"); + // Workaround for https://github.com/gfx-rs/wgpu/issues/3710: + // Don't use `create_surface_from_canvas`, but `create_surface` instead! let canvas = super::canvas_element_or_die(canvas_id); + let raw_window = EguiWebWindow(hash64("egui canvas running on wgpu!")); + canvas.set_attribute("data-raw-handle", &raw_window.0.to_string()); let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { backends: options.wgpu_options.backends, dx12_shader_compiler: Default::default(), }); - let surface = instance - .create_surface_from_canvas(canvas.clone()) + + let surface = unsafe { instance.create_surface(&raw_window) } .map_err(|err| format!("failed to create wgpu surface: {err}"))?; let adapter = instance diff --git a/crates/egui-wgpu/src/lib.rs b/crates/egui-wgpu/src/lib.rs index 9edbcd44f5c0..a89539f327ad 100644 --- a/crates/egui-wgpu/src/lib.rs +++ b/crates/egui-wgpu/src/lib.rs @@ -68,6 +68,8 @@ impl Default for WgpuConfiguration { features: wgpu::Features::default(), limits: wgpu::Limits::default(), }, + // Add GL backend, primarily because WebGPU is not stable enough yet. + // (note however, that the GL backend needs to be opted-in via a wgpu feature flag) backends: wgpu::Backends::PRIMARY | wgpu::Backends::GL, present_mode: wgpu::PresentMode::AutoVsync, power_preference: wgpu::PowerPreference::HighPerformance,