Skip to content

Commit 0e6d69d

Browse files
Wumpfemilk
andauthored
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>
1 parent 09e1569 commit 0e6d69d

File tree

7 files changed

+73
-28
lines changed

7 files changed

+73
-28
lines changed

Cargo.lock

+2-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/eframe/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ NOTE: [`egui-winit`](../egui-winit/CHANGELOG.md), [`egui_glium`](../egui_glium/C
1212

1313
#### Web:
1414
* Bug fix: modifiers keys getting stuck on alt-tab ([#2857](https://github.com/emilk/egui/pull/2857)).
15+
* ⚠️ BREAKING: WebGPU is now the default web renderer when using the `wgpu` feature of `eframe`. To use WebGL with `wgpu`, you need to add `wgpu = { version = "0.16.0", features = ["webgl"] }` to your own `Cargo.toml`. ([#2945](https://github.com/emilk/egui/pull/2945))
1516

1617
## 0.21.3 - 2023-02-15
1718
* Fix typing the letter 'P' on web ([#2740](https://github.com/emilk/egui/pull/2740)).

crates/eframe/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ __screenshot = []
6262

6363
## Use [`wgpu`](https://docs.rs/wgpu) for painting (via [`egui-wgpu`](https://github.com/emilk/egui/tree/master/crates/egui-wgpu)).
6464
## This overrides the `glow` feature.
65-
wgpu = ["dep:wgpu", "dep:egui-wgpu", "dep:pollster"]
65+
wgpu = ["dep:wgpu", "dep:egui-wgpu", "dep:pollster", "dep:raw-window-handle"]
6666

6767
# Allow crates to choose an android-activity backend via Winit
6868
# - 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 = [
181181

182182
# optional web:
183183
egui-wgpu = { version = "0.21.0", path = "../egui-wgpu", optional = true } # if wgpu is used, use it without (!) winit
184+
raw-window-handle = { version = "0.5.2", optional = true }
184185
tts = { version = "0.25", optional = true, default-features = false }
185-
wgpu = { version = "0.16.0", optional = true, features = ["webgl"] }
186+
wgpu = { version = "0.16.0", optional = true }

crates/eframe/src/epi/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,8 @@ impl Default for WebOptions {
498498

499499
#[cfg(feature = "wgpu")]
500500
wgpu_options: egui_wgpu::WgpuConfiguration {
501-
// WebGPU is not stable enough yet, use WebGL emulation
502-
backends: wgpu::Backends::GL,
501+
// Use WebGPU or WebGL. Note that WebGL needs to be opted in via a wgpu feature.
502+
backends: wgpu::Backends::BROWSER_WEBGPU | wgpu::Backends::GL,
503503
device_descriptor: wgpu::DeviceDescriptor {
504504
label: Some("egui wgpu device"),
505505
features: wgpu::Features::default(),

crates/eframe/src/web/web_painter_wgpu.rs

+36-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ use crate::WebOptions;
1010

1111
use super::web_painter::WebPainter;
1212

13+
struct EguiWebWindow(u32);
14+
15+
#[allow(unsafe_code)]
16+
unsafe impl raw_window_handle::HasRawWindowHandle for EguiWebWindow {
17+
fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
18+
let mut window_handle = raw_window_handle::WebWindowHandle::empty();
19+
window_handle.id = self.0;
20+
raw_window_handle::RawWindowHandle::Web(window_handle)
21+
}
22+
}
23+
24+
#[allow(unsafe_code)]
25+
unsafe impl raw_window_handle::HasRawDisplayHandle for EguiWebWindow {
26+
fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle {
27+
raw_window_handle::RawDisplayHandle::Web(raw_window_handle::WebDisplayHandle::empty())
28+
}
29+
}
30+
1331
pub(crate) struct WebPainterWgpu {
1432
canvas: HtmlCanvasElement,
1533
canvas_id: String,
@@ -59,15 +77,28 @@ impl WebPainterWgpu {
5977
pub async fn new(canvas_id: &str, options: &WebOptions) -> Result<Self, String> {
6078
log::debug!("Creating wgpu painter");
6179

62-
let canvas = super::canvas_element_or_die(canvas_id);
63-
6480
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
6581
backends: options.wgpu_options.backends,
6682
dx12_shader_compiler: Default::default(),
6783
});
68-
let surface = instance
69-
.create_surface_from_canvas(canvas.clone())
70-
.map_err(|err| format!("failed to create wgpu surface: {err}"))?;
84+
85+
let canvas = super::canvas_element_or_die(canvas_id);
86+
87+
let surface = if false {
88+
instance.create_surface_from_canvas(canvas.clone())
89+
} else {
90+
// Workaround for https://github.com/gfx-rs/wgpu/issues/3710:
91+
// Don't use `create_surface_from_canvas`, but `create_surface` instead!
92+
let raw_window =
93+
EguiWebWindow(egui::util::hash(&format!("egui on wgpu {canvas_id}")) as u32);
94+
canvas.set_attribute("data-raw-handle", &raw_window.0.to_string());
95+
96+
#[allow(unsafe_code)]
97+
unsafe {
98+
instance.create_surface(&raw_window)
99+
}
100+
}
101+
.map_err(|err| format!("failed to create wgpu surface: {err}"))?;
71102

72103
let adapter = instance
73104
.request_adapter(&wgpu::RequestAdapterOptions {

crates/egui-wgpu/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ impl Default for WgpuConfiguration {
6868
features: wgpu::Features::default(),
6969
limits: wgpu::Limits::default(),
7070
},
71+
// Add GL backend, primarily because WebGPU is not stable enough yet.
72+
// (note however, that the GL backend needs to be opted-in via a wgpu feature flag)
7173
backends: wgpu::Backends::PRIMARY | wgpu::Backends::GL,
7274
present_mode: wgpu::PresentMode::AutoVsync,
7375
power_preference: wgpu::PowerPreference::HighPerformance,

scripts/build_demo_web.sh

+27-8
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,27 @@ export RUSTFLAGS=--cfg=web_sys_unstable_apis
1313
CRATE_NAME="egui_demo_app"
1414

1515
# NOTE: persistence use up about 400kB (10%) of the WASM!
16-
FEATURES="glow,http,persistence,web_screen_reader"
16+
FEATURES="http,persistence,web_screen_reader"
1717

1818
OPEN=false
1919
OPTIMIZE=false
2020
BUILD=debug
2121
BUILD_FLAGS=""
22+
WEB_GPU=false
2223

2324
while test $# -gt 0; do
2425
case "$1" in
2526
-h|--help)
26-
echo "build_demo_web.sh [--release] [--open]"
27+
echo "build_demo_web.sh [--release] [--webgpu] [--open]"
2728
echo ""
2829
echo " --release: Build with --release, and enable extra optimization step"
2930
echo " Runs wasm-opt."
3031
echo " NOTE: --release also removes debug symbols which are otherwise useful for in-browser profiling."
3132
echo ""
32-
echo " --open: Open the result in a browser"
33+
echo " --webgpu: Build a binary for WebGPU instead of WebGL"
34+
echo " Note that the resulting wasm will ONLY work on browsers with WebGPU."
35+
echo ""
36+
echo " --open: Open the result in a browser"
3337
exit 0
3438
;;
3539

@@ -40,6 +44,11 @@ while test $# -gt 0; do
4044
BUILD_FLAGS="--release"
4145
;;
4246

47+
--webgpu)
48+
shift
49+
WEB_GPU=true
50+
;;
51+
4352
--open)
4453
shift
4554
OPEN=true
@@ -51,8 +60,18 @@ while test $# -gt 0; do
5160
esac
5261
done
5362

63+
OUT_FILE_NAME="egui_demo_app"
64+
65+
if [[ "${WEB_GPU}" == true ]]; then
66+
FEATURES="${FEATURES},wgpu"
67+
else
68+
FEATURES="${FEATURES},glow"
69+
fi
70+
71+
FINAL_WASM_PATH=docs/${OUT_FILE_NAME}_bg.wasm
72+
5473
# Clear output from old stuff:
55-
rm -f "docs/${CRATE_NAME}_bg.wasm"
74+
rm -f "${FINAL_WASM_PATH}"
5675

5776
echo "Building rust…"
5877

@@ -71,22 +90,22 @@ TARGET=`cargo metadata --format-version=1 | jq --raw-output .target_directory`
7190
echo "Generating JS bindings for wasm…"
7291
TARGET_NAME="${CRATE_NAME}.wasm"
7392
WASM_PATH="${TARGET}/wasm32-unknown-unknown/$BUILD/$TARGET_NAME"
74-
wasm-bindgen "${WASM_PATH}" --out-dir docs --no-modules --no-typescript
93+
wasm-bindgen "${WASM_PATH}" --out-dir docs --out-name ${OUT_FILE_NAME} --no-modules --no-typescript
7594

7695
# if this fails with "error: cannot import from modules (`env`) with `--no-modules`", you can use:
7796
# wasm2wat target/wasm32-unknown-unknown/release/egui_demo_app.wasm | rg env
7897
# wasm2wat target/wasm32-unknown-unknown/release/egui_demo_app.wasm | rg "call .now\b" -B 20 # What calls `$now` (often a culprit)
7998

8099
# to get wasm-strip: apt/brew/dnf install wabt
81-
# wasm-strip docs/${CRATE_NAME}_bg.wasm
100+
# wasm-strip ${FINAL_WASM_PATH}
82101

83102
if [[ "${OPTIMIZE}" = true ]]; then
84103
echo "Optimizing wasm…"
85104
# to get wasm-opt: apt/brew/dnf install binaryen
86-
wasm-opt "docs/${CRATE_NAME}_bg.wasm" -O2 --fast-math -o "docs/${CRATE_NAME}_bg.wasm" # add -g to get debug symbols
105+
wasm-opt "${FINAL_WASM_PATH}" -O2 --fast-math -o "${FINAL_WASM_PATH}" # add -g to get debug symbols
87106
fi
88107

89-
echo "Finished docs/${CRATE_NAME}_bg.wasm"
108+
echo "Finished ${FINAL_WASM_PATH}"
90109

91110
if [[ "${OPEN}" == true ]]; then
92111
if [[ "$OSTYPE" == "linux-gnu"* ]]; then

0 commit comments

Comments
 (0)