Closes #4173. ## The problem `change_gl_context` takes an early-out when the surface is already current, but that early-out is disabled on Windows for **every** case: ```rust if !cfg!(target_os = "windows") { // According to https://github.com/emilk/egui/issues/4289 // we cannot do this early-out on Windows. ``` So on Windows every frame pays `make_not_current` + `make_current` — two `wglMakeCurrent` calls — whether or not anything changed. ## What this changes `change_gl_context` takes a `single_viewport: bool`, and the early-out is allowed on Windows when it is true. Call sites compute it as `viewports.len() == 1`. With a second viewport present the condition is false and the old path runs unchanged, so the corruption #4289 describes is out of scope by construction. On the `render_immediate_viewport` path the count is two or more by definition, so it is never affected. ## Measurements Windows 11, RTX 3060 (NVIDIA 610.74), release builds, window focused, pointer parked off the window. A minimal app — one viewport, one label, `request_repaint()` every frame, i.e. the "light application" from #4173: ```rust fn main() -> eframe::Result { eframe::run_ui_native("bench", eframe::NativeOptions::default(), |ui, _frame| { ui.label("bench"); ui.ctx().request_repaint(); }) } ``` Interleaved runs, alternating binaries, 8 s samples, process CPU: | | runs (% of one core) | median | |---|---|---| | `main` | 104.1, 100.0, 97.0, 96.0, 98.3 | **98.3** | | this PR | 2.3, 10.1, 20.2, 9.3, 3.1 | **9.3** | The two sets do not overlap. `main` sits at a whole core for an application that draws one label. Per-scope numbers from a real application (a launcher with an animated panel, 30 fps, `profiling` + puffin), µs of CPU per frame: | scope | `main` | this PR | |---|---|---| | `change_gl_context` | 550 | **1.0** | | egui pass (incl. the app's own `ui`) | 152 | 141 | | `swap_buffers` | 21 | 103 | | `update_viewport_info` | 65 | 65 | | tessellation | 59 | 53 | | painting | 26 | 22 | | **whole frame** | **917** | **422** | `swap_buffers` rises because it now does the waiting the context switch used to absorb — a blocking wait, not work. That application went from 7.4–11.3% to 2.7–3.5% of one core. ## Checking #4289 has not come back `examples/multiple_viewports`, forced onto the glow backend, with both the immediate and the deferred child viewport opened from the first frame, plus a temporary log of which branch `change_gl_context` took: ``` Count Name ----- ---- 22 single_viewport=false <- three viewports open: old path 1 single_viewport=true <- first frame, before the children exist ``` Ran for 7 s with three viewports, no crash, no flicker, nothing on stderr. Neither the log nor the forced-open change is part of this PR. ## Caveats, honestly - One machine, one GPU vendor, one Windows version. I have no way to test AMD or Intel, or an older Windows. - In #4173, @gerwin3 reported that #4284 — which enabled this early-out without the viewport-count guard — "did not noticeably decrease" CPU for them. I cannot reproduce that; the numbers above are unambiguous on my machine. My guess is that they were also hitting a second, unrelated thing: `Context::request_repaint_after` subtracts `predicted_dt`, and nothing in `egui-winit` ever assigns it, so it stays at its `1/60` default and a throttled application still runs at vsync (see #5353). That would put a `hello_world` measurement at the display's rate regardless of this change. That is a hypothesis, not something I have verified on their setup. - `cargo fmt --all --check` is clean and `cargo clippy -p eframe --no-default-features --features glow,default_fonts --all-targets` reports no warnings. I have not run the full `scripts/check.sh` (wasm toolchain). Happy to reshape this if you would rather thread the flag differently, or gate it on something other than the viewport count. --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
eframe: the egui framework
eframe is the official framework library for writing apps using egui. The app can be compiled both to run natively (for Linux, Mac, Windows, and Android) or as a web app (using Wasm).
To get started, see the examples.
To learn how to set up eframe for web and native, go to https://github.com/emilk/eframe_template/ and follow the instructions there!
There is also a tutorial video at https://www.youtube.com/watch?v=NtUkr_z7l84.
For how to use egui, see the egui docs.
eframe defaults to using wgpu for rendering (with an option to change to glow), and on native it uses egui-winit.
To use on Linux, first run:
sudo apt-get install libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev libssl-dev
You need to either use edition = "2024", or set resolver = "2" in the [workspace] section of your to-level Cargo.toml. See this link for more info.
You can opt-in to the using egui_glow for rendering by enabling the glow feature and setting NativeOptions::renderer to Renderer::Glow.
Alternatives
eframe is not the only way to write an app using egui! You can also try egui-miniquad, bevy_egui, egui_sdl2_gl, and others.
You can also use egui_glow and winit to build your own app as demonstrated in https://github.com/emilk/egui/blob/main/crates/egui_glow/examples/pure_glow.rs.
Limitations when running egui on the web
eframe and egui compiles to Wasm using either WebGPU (when available) or WebGL2 for rendering, and almost nothing else from the web tech stack. This has some benefits, but also produces some challenges and serious downsides.
- Rendering: Getting pixel-perfect rendering right on the web is very difficult.
- Search: you cannot search an egui web page like you would a normal web page.
- Bringing up an on-screen keyboard on mobile: there is no JS function to do this, so
eframefakes it by adding some invisible DOM elements. It doesn't always work. - Mobile text editing is not as good as for a normal web app.
- No integration with browser settings for colors and fonts.
- Accessibility: There is an experimental screen reader for
eframe, but it has to be enabled explicitly. There is no JS function to ask "Does the user want a screen reader?" (and there should probably not be such a function, due to user tracking/integrity concerns).eguisupports AccessKit, but as of early 2024, AccessKit lacks a Web backend.
In many ways, eframe is trying to make the browser do something it wasn't designed to do (though there are many things browser vendors could do to improve how well libraries like egui work).
The suggested use for eframe are for web apps where performance and responsiveness are more important than accessibility and mobile text editing.
Companion crates
Not all rust crates work when compiled to Wasm, but here are some useful crates have been designed to work well both natively and as Wasm:
Name
The frame in eframe stands both for the frame in which your egui app resides and also for "framework" (eframe is a framework, egui is a library).