1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Make egui work on WebGPU out of the box. (#2945)

* Make wgpu webgl/gles opt-in (but still work out of the box via feature flag), workaround canvas creation issue

* missing allow unsafe code annotations

* add breaking change not to eframe release notes

* Add --webgpu flag to build_demo_web.sh

* Improve CHANGELOG docs

* Clean up, and prepare for having to wasm:s

* put canvas without workaround under `if false`

* fix spelling

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
Andreas Reich
2023-04-24 11:26:45 +02:00
committed by GitHub
parent 09e1569bf3
commit 0e6d69d4c4
7 changed files with 73 additions and 28 deletions

View File

@@ -10,6 +10,24 @@ use crate::WebOptions;
use super::web_painter::WebPainter;
struct EguiWebWindow(u32);
#[allow(unsafe_code)]
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)
}
}
#[allow(unsafe_code)]
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,15 +77,28 @@ impl WebPainterWgpu {
pub async fn new(canvas_id: &str, options: &WebOptions) -> Result<Self, String> {
log::debug!("Creating wgpu painter");
let canvas = super::canvas_element_or_die(canvas_id);
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())
.map_err(|err| format!("failed to create wgpu surface: {err}"))?;
let canvas = super::canvas_element_or_die(canvas_id);
let surface = if false {
instance.create_surface_from_canvas(canvas.clone())
} else {
// Workaround for https://github.com/gfx-rs/wgpu/issues/3710:
// Don't use `create_surface_from_canvas`, but `create_surface` instead!
let raw_window =
EguiWebWindow(egui::util::hash(&format!("egui on wgpu {canvas_id}")) as u32);
canvas.set_attribute("data-raw-handle", &raw_window.0.to_string());
#[allow(unsafe_code)]
unsafe {
instance.create_surface(&raw_window)
}
}
.map_err(|err| format!("failed to create wgpu surface: {err}"))?;
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {