1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00

Update wgpu to 0.19 (#3824)

* Closes https://github.com/emilk/egui/issues/3675

---------

Co-authored-by: Andreas Reich <r_andreas2@web.de>
Co-authored-by: Mingun <Alexander_Sergey@mail.ru>
This commit is contained in:
Emil Ernerfeldt
2024-01-19 10:14:13 +01:00
committed by GitHub
parent 7733d1d87c
commit b766a48fa7
27 changed files with 578 additions and 344 deletions

View File

@@ -56,7 +56,14 @@ android-native-activity = ["egui-winit/android-native-activity"]
default_fonts = ["egui/default_fonts"]
## Use [`glow`](https://github.com/grovesNL/glow) for painting, via [`egui_glow`](https://github.com/emilk/egui/tree/master/crates/egui_glow).
glow = ["dep:glow", "dep:egui_glow", "dep:glutin", "dep:glutin-winit"]
glow = [
"dep:egui_glow",
"dep:glow",
"dep:glutin-winit",
"dep:glutin",
"dep:rwh_05",
"winit/rwh_05",
]
## Enable saving app state to disk.
persistence = [
@@ -92,8 +99,19 @@ web_screen_reader = [
]
## 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", "dep:raw-window-handle"]
##
## By default, only WebGPU is enabled on web.
## If you want to enable WebGL, you need to turn on the `webgl` feature of crate `wgpu`:
##
## ```ignore
## wgpu = { version = "*", features = ["webgpu", "webgl"] }
## ```
##
## By default, eframe will prefer WebGPU over WebGL, but
## you can configure this at run-time with [`NativeOptions::wgpu_options`].
wgpu = ["dep:wgpu", "dep:egui-wgpu", "dep:pollster"]
## Enables compiling for x11.
x11 = ["egui-winit/x11"]
@@ -109,6 +127,7 @@ egui = { version = "0.25.0", path = "../egui", default-features = false, feature
] }
log = { version = "0.4", features = ["std"] }
parking_lot = "0.12"
raw-window-handle.workspace = true
static_assertions = "1.1.0"
thiserror.workspace = true
@@ -118,6 +137,10 @@ document-features = { version = "0.2", optional = true }
egui_glow = { version = "0.25.0", path = "../egui_glow", optional = true, default-features = false }
glow = { workspace = true, optional = true }
# glutin stuck on old version of raw-window-handle:
rwh_05 = { package = "raw-window-handle", version = "0.5.2", optional = true, features = [
"std",
] }
ron = { version = "0.8", optional = true, features = ["integer128"] }
serde = { version = "1", optional = true, features = ["derive"] }
@@ -131,8 +154,7 @@ egui-winit = { version = "0.25.0", path = "../egui-winit", default-features = fa
image = { version = "0.24", default-features = false, features = [
"png",
] } # Needed for app icon
raw-window-handle.workspace = true
winit = { version = "0.29.4", default-features = false, features = ["rwh_05"] }
winit = { workspace = true, default-features = false, features = ["rwh_06"] }
# optional native:
directories-next = { version = "2", optional = true }
@@ -211,5 +233,4 @@ web-sys = { version = "0.3.58", features = [
# optional web:
egui-wgpu = { version = "0.25.0", path = "../egui-wgpu", optional = true } # if wgpu is used, use it without (!) winit
raw-window-handle = { workspace = true, optional = true }
wgpu = { workspace = true, optional = true }

View File

@@ -15,7 +15,8 @@ pub use crate::native::winit_integration::UserEvent;
#[cfg(not(target_arch = "wasm32"))]
use raw_window_handle::{
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawDisplayHandle,
RawWindowHandle, WindowHandle,
};
#[cfg(not(target_arch = "wasm32"))]
use static_assertions::assert_not_impl_any;
@@ -76,30 +77,28 @@ pub struct CreationContext<'s> {
/// Raw platform window handle
#[cfg(not(target_arch = "wasm32"))]
pub(crate) raw_window_handle: RawWindowHandle,
pub(crate) raw_window_handle: Result<RawWindowHandle, HandleError>,
/// Raw platform display handle for window
#[cfg(not(target_arch = "wasm32"))]
pub(crate) raw_display_handle: RawDisplayHandle,
pub(crate) raw_display_handle: Result<RawDisplayHandle, HandleError>,
}
// Implementing `Clone` would violate the guarantees of `HasRawWindowHandle` and `HasRawDisplayHandle`.
#[cfg(not(target_arch = "wasm32"))]
assert_not_impl_any!(CreationContext<'_>: Clone);
#[allow(unsafe_code)]
#[cfg(not(target_arch = "wasm32"))]
unsafe impl HasRawWindowHandle for CreationContext<'_> {
fn raw_window_handle(&self) -> RawWindowHandle {
self.raw_window_handle
impl HasWindowHandle for CreationContext<'_> {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
// Safety: the lifetime is correct.
unsafe { Ok(WindowHandle::borrow_raw(self.raw_window_handle.clone()?)) }
}
}
#[allow(unsafe_code)]
#[cfg(not(target_arch = "wasm32"))]
unsafe impl HasRawDisplayHandle for CreationContext<'_> {
fn raw_display_handle(&self) -> RawDisplayHandle {
self.raw_display_handle
impl HasDisplayHandle for CreationContext<'_> {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
// Safety: the lifetime is correct.
unsafe { Ok(DisplayHandle::borrow_raw(self.raw_display_handle.clone()?)) }
}
}
@@ -599,30 +598,32 @@ pub struct Frame {
/// Raw platform window handle
#[cfg(not(target_arch = "wasm32"))]
pub(crate) raw_window_handle: RawWindowHandle,
pub(crate) raw_window_handle: Result<RawWindowHandle, HandleError>,
/// Raw platform display handle for window
#[cfg(not(target_arch = "wasm32"))]
pub(crate) raw_display_handle: RawDisplayHandle,
pub(crate) raw_display_handle: Result<RawDisplayHandle, HandleError>,
}
// Implementing `Clone` would violate the guarantees of `HasRawWindowHandle` and `HasRawDisplayHandle`.
// Implementing `Clone` would violate the guarantees of `HasWindowHandle` and `HasDisplayHandle`.
#[cfg(not(target_arch = "wasm32"))]
assert_not_impl_any!(Frame: Clone);
#[allow(unsafe_code)]
#[cfg(not(target_arch = "wasm32"))]
unsafe impl HasRawWindowHandle for Frame {
fn raw_window_handle(&self) -> RawWindowHandle {
self.raw_window_handle
impl HasWindowHandle for Frame {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
// Safety: the lifetime is correct.
unsafe { Ok(WindowHandle::borrow_raw(self.raw_window_handle.clone()?)) }
}
}
#[allow(unsafe_code)]
#[cfg(not(target_arch = "wasm32"))]
unsafe impl HasRawDisplayHandle for Frame {
fn raw_display_handle(&self) -> RawDisplayHandle {
self.raw_display_handle
impl HasDisplayHandle for Frame {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
// Safety: the lifetime is correct.
unsafe { Ok(DisplayHandle::borrow_raw(self.raw_display_handle.clone()?)) }
}
}

View File

@@ -4,7 +4,7 @@ use std::time::Instant;
use winit::event_loop::EventLoopWindowTarget;
use raw_window_handle::{HasRawDisplayHandle as _, HasRawWindowHandle as _};
use raw_window_handle::{HasDisplayHandle as _, HasWindowHandle as _};
use egui::{DeferredViewportUiCallback, NumExt as _, ViewportBuilder, ViewportId};
use egui_winit::{EventResponse, WindowSettings};
@@ -165,8 +165,8 @@ impl EpiIntegration {
gl,
#[cfg(feature = "wgpu")]
wgpu_render_state,
raw_display_handle: window.raw_display_handle(),
raw_window_handle: window.raw_window_handle(),
raw_display_handle: window.display_handle().map(|h| h.as_raw()),
raw_window_handle: window.window_handle().map(|h| h.as_raw()),
};
let icon = native_options

View File

@@ -16,7 +16,6 @@ use glutin::{
prelude::{GlDisplay, PossiblyCurrentGlContext},
surface::GlSurface,
};
use raw_window_handle::{HasRawDisplayHandle as _, HasRawWindowHandle as _};
use winit::{
event_loop::{EventLoop, EventLoopProxy, EventLoopWindowTarget},
window::{Window, WindowId},
@@ -126,7 +125,7 @@ struct Viewport {
// These three live and die together.
// TODO(emilk): clump them together into one struct!
gl_surface: Option<glutin::surface::Surface<glutin::surface::WindowSurface>>,
window: Option<Rc<Window>>,
window: Option<Arc<Window>>,
egui_winit: Option<egui_winit::State>,
}
@@ -294,6 +293,9 @@ impl GlowWinitApp {
.expect("Single-use AppCreator has unexpectedly already been taken");
let app = {
// Use latest raw_window_handle for eframe compatibility
use raw_window_handle::{HasDisplayHandle as _, HasWindowHandle as _};
let window = glutin.window(ViewportId::ROOT);
let cc = CreationContext {
egui_ctx: integration.egui_ctx.clone(),
@@ -302,8 +304,8 @@ impl GlowWinitApp {
gl: Some(gl),
#[cfg(feature = "wgpu")]
wgpu_render_state: None,
raw_display_handle: window.raw_display_handle(),
raw_window_handle: window.raw_window_handle(),
raw_display_handle: window.display_handle().map(|h| h.as_raw()),
raw_window_handle: window.window_handle().map(|h| h.as_raw()),
};
crate::profile_scope!("app_creator");
app_creator(&cc)
@@ -373,7 +375,7 @@ impl WinitApp for GlowWinitApp {
self.running.as_ref().map(|r| &r.integration)
}
fn window(&self, window_id: WindowId) -> Option<Rc<Window>> {
fn window(&self, window_id: WindowId) -> Option<Arc<Window>> {
let running = self.running.as_ref()?;
let glutin = running.glutin.borrow();
let viewport_id = *glutin.viewport_from_window.get(&window_id)?;
@@ -898,15 +900,18 @@ impl GlutinWindowContext {
gl_display.version_string(),
gl_display.supported_features()
);
let raw_window_handle = window.as_ref().map(|w| w.raw_window_handle());
log::debug!("creating gl context using raw window handle: {raw_window_handle:?}");
let glutin_raw_window_handle = window.as_ref().map(|w| {
use rwh_05::HasRawWindowHandle as _; // glutin stuck on old version of raw-window-handle
w.raw_window_handle()
});
log::debug!("creating gl context using raw window handle: {glutin_raw_window_handle:?}");
// create gl context. if core context cannot be created, try gl es context as fallback.
let context_attributes =
glutin::context::ContextAttributesBuilder::new().build(raw_window_handle);
glutin::context::ContextAttributesBuilder::new().build(glutin_raw_window_handle);
let fallback_context_attributes = glutin::context::ContextAttributesBuilder::new()
.with_context_api(glutin::context::ContextApi::Gles(None))
.build(raw_window_handle);
.build(glutin_raw_window_handle);
let gl_context_result = unsafe {
crate::profile_scope!("create_context");
@@ -952,7 +957,7 @@ impl GlutinWindowContext {
screenshot_requested: false,
viewport_ui_cb: None,
gl_surface: None,
window: window.map(Rc::new),
window: window.map(Arc::new),
egui_winit: None,
},
);
@@ -1031,7 +1036,7 @@ impl GlutinWindowContext {
);
viewport.info.minimized = window.is_minimized();
viewport.info.maximized = Some(window.is_maximized());
viewport.window.insert(Rc::new(window))
viewport.window.insert(Arc::new(window))
};
viewport.egui_winit.get_or_insert_with(|| {
@@ -1052,9 +1057,11 @@ impl GlutinWindowContext {
let (width_px, height_px): (u32, u32) = window.inner_size().into();
let width_px = std::num::NonZeroU32::new(width_px.at_least(1)).unwrap();
let height_px = std::num::NonZeroU32::new(height_px.at_least(1)).unwrap();
let surface_attributes =
let surface_attributes = {
use rwh_05::HasRawWindowHandle as _; // glutin stuck on old version of raw-window-handle
glutin::surface::SurfaceAttributesBuilder::<glutin::surface::WindowSurface>::new()
.build(window.raw_window_handle(), width_px, height_px);
.build(window.raw_window_handle(), width_px, height_px)
};
log::trace!("creating surface with attributes: {surface_attributes:?}");
let gl_surface = unsafe {
@@ -1120,7 +1127,7 @@ impl GlutinWindowContext {
.expect("viewport doesn't exist")
}
fn window(&self, viewport_id: ViewportId) -> Rc<Window> {
fn window(&self, viewport_id: ViewportId) -> Arc<Window> {
self.viewport(viewport_id)
.window
.clone()

View File

@@ -8,7 +8,7 @@
use std::{cell::RefCell, rc::Rc, sync::Arc, time::Instant};
use parking_lot::Mutex;
use raw_window_handle::{HasRawDisplayHandle as _, HasRawWindowHandle as _};
use raw_window_handle::{HasDisplayHandle as _, HasWindowHandle as _};
use winit::{
event_loop::{EventLoop, EventLoopProxy, EventLoopWindowTarget},
window::{Window, WindowId},
@@ -84,7 +84,7 @@ pub struct Viewport {
/// Window surface state that's initialized when the app starts running via a Resumed event
/// and on Android will also be destroyed if the application is paused.
window: Option<Rc<Window>>,
window: Option<Arc<Window>>,
/// `window` and `egui_winit` are initialized together.
egui_winit: Option<egui_winit::State>,
@@ -170,9 +170,11 @@ impl WgpuWinitApp {
self.native_options.viewport.transparent.unwrap_or(false),
);
let window = Arc::new(window);
{
crate::profile_scope!("set_window");
pollster::block_on(painter.set_window(ViewportId::ROOT, Some(&window)))?;
pollster::block_on(painter.set_window(ViewportId::ROOT, Some(window.clone())))?;
}
let wgpu_render_state = painter.render_state();
@@ -235,8 +237,8 @@ impl WgpuWinitApp {
#[cfg(feature = "glow")]
gl: None,
wgpu_render_state,
raw_display_handle: window.raw_display_handle(),
raw_window_handle: window.raw_window_handle(),
raw_display_handle: window.display_handle().map(|h| h.as_raw()),
raw_window_handle: window.window_handle().map(|h| h.as_raw()),
};
let app = {
crate::profile_scope!("user_app_creator");
@@ -260,7 +262,7 @@ impl WgpuWinitApp {
},
screenshot_requested: false,
viewport_ui_cb: None,
window: Some(Rc::new(window)),
window: Some(window),
egui_winit: Some(egui_winit),
},
);
@@ -323,7 +325,7 @@ impl WinitApp for WgpuWinitApp {
self.running.as_ref().map(|r| &r.integration)
}
fn window(&self, window_id: WindowId) -> Option<Rc<Window>> {
fn window(&self, window_id: WindowId) -> Option<Arc<Window>> {
self.running
.as_ref()
.and_then(|r| {
@@ -544,7 +546,8 @@ impl WgpuWinitRunning {
{
crate::profile_scope!("set_window");
if let Err(err) = pollster::block_on(painter.set_window(viewport_id, Some(window)))
if let Err(err) =
pollster::block_on(painter.set_window(viewport_id, Some(window.clone())))
{
log::warn!("Failed to set window: {err}");
}
@@ -797,7 +800,10 @@ impl Viewport {
Ok(window) => {
windows_id.insert(window.id(), viewport_id);
if let Err(err) = pollster::block_on(painter.set_window(viewport_id, Some(&window)))
let window = Arc::new(window);
if let Err(err) =
pollster::block_on(painter.set_window(viewport_id, Some(window.clone())))
{
log::error!("on set_window: viewport_id {viewport_id:?} {err}");
}
@@ -813,7 +819,7 @@ impl Viewport {
self.info.minimized = window.is_minimized();
self.info.maximized = Some(window.is_maximized());
self.window = Some(Rc::new(window));
self.window = Some(window);
}
Err(err) => {
log::error!("Failed to create window: {err}");
@@ -930,7 +936,7 @@ fn render_immediate_viewport(
{
crate::profile_scope!("set_window");
if let Err(err) = pollster::block_on(painter.set_window(ids.this, Some(window))) {
if let Err(err) = pollster::block_on(painter.set_window(ids.this, Some(window.clone()))) {
log::error!(
"when rendering viewport_id={:?}, set_window Error {err}",
ids.this

View File

@@ -1,4 +1,4 @@
use std::{rc::Rc, time::Instant};
use std::{sync::Arc, time::Instant};
use winit::{
event_loop::EventLoopWindowTarget,
@@ -68,7 +68,7 @@ pub trait WinitApp {
fn integration(&self) -> Option<&EpiIntegration>;
fn window(&self, window_id: WindowId) -> Option<Rc<Window>>;
fn window(&self, window_id: WindowId) -> Option<Arc<Window>>;
fn window_id_from_viewport_id(&self, id: ViewportId) -> Option<WindowId>;

View File

@@ -19,6 +19,21 @@ impl WebLogger {
impl log::Log for WebLogger {
fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
/// Never log anything less serious than a `INFO` from these crates.
const CRATES_AT_INFO_LEVEL: &[&str] = &[
// wgpu crates spam a lot on debug level, which is really annoying
"naga",
"wgpu_core",
"wgpu_hal",
];
if CRATES_AT_INFO_LEVEL
.iter()
.any(|crate_name| metadata.target().starts_with(crate_name))
{
return metadata.level() <= log::LevelFilter::Info;
}
metadata.level() <= self.filter
}

View File

@@ -1,5 +1,9 @@
use std::sync::Arc;
use raw_window_handle::{
DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawDisplayHandle,
RawWindowHandle, WebDisplayHandle, WebWindowHandle, WindowHandle,
};
use wasm_bindgen::JsValue;
use web_sys::HtmlCanvasElement;
@@ -12,25 +16,33 @@ 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)
impl HasWindowHandle for EguiWebWindow {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
// SAFETY: there is no lifetime here.
unsafe {
Ok(WindowHandle::borrow_raw(RawWindowHandle::Web(
WebWindowHandle::new(self.0),
)))
}
}
}
#[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())
impl HasDisplayHandle for EguiWebWindow {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
// SAFETY: there is no lifetime here.
unsafe {
Ok(DisplayHandle::borrow_raw(RawDisplayHandle::Web(
WebDisplayHandle::new(),
)))
}
}
}
pub(crate) struct WebPainterWgpu {
canvas: HtmlCanvasElement,
canvas_id: String,
surface: wgpu::Surface,
surface: wgpu::Surface<'static>,
surface_configuration: wgpu::SurfaceConfiguration,
render_state: Option<RenderState>,
on_surface_error: Arc<dyn Fn(wgpu::SurfaceError) -> SurfaceErrorAction>,
@@ -75,6 +87,15 @@ impl WebPainterWgpu {
pub async fn new(canvas_id: &str, options: &WebOptions) -> Result<Self, String> {
log::debug!("Creating wgpu painter");
{
let is_secure_context = web_sys::window().map_or(false, |w| w.is_secure_context());
if !is_secure_context {
log::info!(
"WebGPU is only available in secure contexts, i.e. on HTTPS and on localhost"
);
}
}
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: options.wgpu_options.supported_backends,
..Default::default()
@@ -82,20 +103,9 @@ impl WebPainterWgpu {
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(("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 surface = instance
.create_surface(wgpu::SurfaceTarget::Canvas(canvas.clone()))
.map_err(|err| format!("failed to create wgpu surface: {err}"))?;
let depth_format = egui_wgpu::depth_format_from_bits(options.depth_buffer, 0);
let render_state =
@@ -103,14 +113,17 @@ impl WebPainterWgpu {
.await
.map_err(|err| err.to_string())?;
let (width, height) = (0, 0);
let surface_configuration = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: render_state.target_format,
width: 0,
height: 0,
present_mode: options.wgpu_options.present_mode,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: vec![render_state.target_format],
..surface
.get_default_config(&render_state.adapter, width, height)
.ok_or("The surface isn't supported by this adapter")?
};
log::debug!("wgpu painter initialized.");

View File

@@ -51,8 +51,8 @@ wgpu.workspace = true
## Enable this when generating docs.
document-features = { version = "0.2", optional = true }
winit = { version = "0.29.4", default-features = false, optional = true, features = [
"rwh_05",
winit = { workspace = true, optional = true, default-features = false, features = [
"rwh_06",
] }
# Native:

View File

@@ -1,5 +1,17 @@
//! This crates provides bindings between [`egui`](https://github.com/emilk/egui) and [wgpu](https://crates.io/crates/wgpu).
//!
//! If you're targeting WebGL you also need to turn on the
//! `webgl` feature of the `wgpu` crate:
//!
//! ```ignore
//! # Enable both WebGL and WebGPU backends on web.
//! wgpu = { version = "*", features = ["webgpu", "webgl"] }
//! ```
//!
//! You can control whether WebGL or WebGPU will be picked at runtime by setting
//! [`WgpuConfiguration::supported_backends`].
//! The default is to prefer WebGPU and fall back on WebGL.
//!
//! ## Feature flags
#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
//!
@@ -21,6 +33,7 @@ use std::sync::Arc;
use epaint::mutex::RwLock;
/// An error produced by egui-wgpu.
#[derive(thiserror::Error, Debug)]
pub enum WgpuError {
#[error("Failed to create wgpu adapter, no suitable adapter found.")]
@@ -34,6 +47,10 @@ pub enum WgpuError {
#[error(transparent)]
CreateSurfaceError(#[from] wgpu::CreateSurfaceError),
#[cfg(feature = "winit")]
#[error(transparent)]
HandleError(#[from] ::winit::raw_window_handle::HandleError),
}
/// Access to the render state for egui.
@@ -42,6 +59,13 @@ pub struct RenderState {
/// Wgpu adapter used for rendering.
pub adapter: Arc<wgpu::Adapter>,
/// All the available adapters.
///
/// This is not available on web.
/// On web, we always select WebGPU is available, then fall back to WebGL if not.
#[cfg(not(target_arch = "wasm32"))]
pub available_adapters: Arc<[wgpu::Adapter]>,
/// Wgpu device used for rendering, created from the adapter.
pub device: Arc<wgpu::Device>,
@@ -63,14 +87,15 @@ impl RenderState {
pub async fn create(
config: &WgpuConfiguration,
instance: &wgpu::Instance,
surface: &wgpu::Surface,
surface: &wgpu::Surface<'static>,
depth_format: Option<wgpu::TextureFormat>,
msaa_samples: u32,
) -> Result<Self, WgpuError> {
crate::profile_scope!("RenderState::create"); // async yield give bad names using `profile_function`
// This is always an empty list on web.
#[cfg(not(target_arch = "wasm32"))]
let adapters: Vec<_> = instance.enumerate_adapters(wgpu::Backends::all()).collect();
let available_adapters = instance.enumerate_adapters(wgpu::Backends::all());
let adapter = {
crate::profile_scope!("request_adapter");
@@ -83,18 +108,18 @@ impl RenderState {
.await
.ok_or_else(|| {
#[cfg(not(target_arch = "wasm32"))]
if adapters.is_empty() {
if available_adapters.is_empty() {
log::info!("No wgpu adapters found");
} else if adapters.len() == 1 {
} else if available_adapters.len() == 1 {
log::info!(
"The only available wgpu adapter was not suitable: {}",
adapter_info_summary(&adapters[0].get_info())
adapter_info_summary(&available_adapters[0].get_info())
);
} else {
log::info!(
"No suitable wgpu adapter found out of the {} available ones: {}",
adapters.len(),
describe_adapters(&adapters)
available_adapters.len(),
describe_adapters(&available_adapters)
);
}
@@ -109,7 +134,7 @@ impl RenderState {
);
#[cfg(not(target_arch = "wasm32"))]
if adapters.len() == 1 {
if available_adapters.len() == 1 {
log::debug!(
"Picked the only available wgpu adapter: {}",
adapter_info_summary(&adapter.get_info())
@@ -117,8 +142,8 @@ impl RenderState {
} else {
log::info!(
"There were {} available wgpu adapters: {}",
adapters.len(),
describe_adapters(&adapters)
available_adapters.len(),
describe_adapters(&available_adapters)
);
log::debug!(
"Picked wgpu adapter: {}",
@@ -143,6 +168,8 @@ impl RenderState {
Ok(Self {
adapter: Arc::new(adapter),
#[cfg(not(target_arch = "wasm32"))]
available_adapters: available_adapters.into(),
device: Arc::new(device),
queue: Arc::new(queue),
target_format,
@@ -180,12 +207,19 @@ pub enum SurfaceErrorAction {
/// Configuration for using wgpu with eframe or the egui-wgpu winit feature.
///
/// This can be configured with the environment variables:
/// This can also be configured with the environment variables:
/// * `WGPU_BACKEND`: `vulkan`, `dx11`, `dx12`, `metal`, `opengl`, `webgpu`
/// * `WGPU_POWER_PREF`: `low`, `high` or `none`
#[derive(Clone)]
pub struct WgpuConfiguration {
/// Backends that should be supported (wgpu will pick one of these)
/// Backends that should be supported (wgpu will pick one of these).
///
/// For instance, if you only want to support WebGL (and not WebGPU),
/// you can set this to [`wgpu::Backends::GL`].
///
/// By default on web, WebGPU will be used if available.
/// WebGL will only be used as a fallback,
/// and only if you have enabled the `webgl` feature of crate `wgpu`.
pub supported_backends: wgpu::Backends,
/// Configuration passed on device request, given an adapter
@@ -215,7 +249,7 @@ impl Default for WgpuConfiguration {
fn default() -> Self {
Self {
// 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)
// (note however, that the GL backend needs to be opted-in via the wgpu feature flag "webgl")
supported_backends: wgpu::util::backend_bits_from_env()
.unwrap_or(wgpu::Backends::PRIMARY | wgpu::Backends::GL),
@@ -228,8 +262,8 @@ impl Default for WgpuConfiguration {
wgpu::DeviceDescriptor {
label: Some("egui wgpu device"),
features: wgpu::Features::default(),
limits: wgpu::Limits {
required_features: wgpu::Features::default(),
required_limits: wgpu::Limits {
// When using a depth buffer, we have to be able to create a texture
// large enough for the entire surface, and we want to support 4k+ displays.
max_texture_dimension_2d: 8192,

View File

@@ -7,8 +7,14 @@ use epaint::{ahash::HashMap, emath::NumExt, PaintCallbackInfo, Primitive, Vertex
use wgpu;
use wgpu::util::DeviceExt as _;
/// You can use this for storage when implementing [`CallbackTrait`].
pub type CallbackResources = type_map::concurrent::TypeMap;
/// You can use this to do custom `wgpu` rendering in an egui app.
///
/// Implement [`CallbackTrait`] and call [`Callback::new_paint_callback`].
///
/// This can be turned into a [`epaint::PaintCallback`] and [`epaint::Shape`].
pub struct Callback(Box<dyn CallbackTrait>);
impl Callback {

View File

@@ -5,7 +5,7 @@ use egui::{ViewportId, ViewportIdMap, ViewportIdSet};
use crate::{renderer, RenderState, SurfaceErrorAction, WgpuConfiguration};
struct SurfaceState {
surface: wgpu::Surface,
surface: wgpu::Surface<'static>,
alpha_mode: wgpu::CompositeAlphaMode,
width: u32,
height: u32,
@@ -151,16 +151,23 @@ impl Painter {
} else {
wgpu::TextureUsages::RENDER_ATTACHMENT
};
let width = surface_state.width;
let height = surface_state.height;
surface_state.surface.configure(
&render_state.device,
&wgpu::SurfaceConfiguration {
// TODO(emilk): expose `desired_maximum_frame_latency` to eframe users
usage,
format: render_state.target_format,
width: surface_state.width,
height: surface_state.height,
present_mode,
alpha_mode: surface_state.alpha_mode,
view_formats: vec![render_state.target_format],
..surface_state
.surface
.get_default_config(&render_state.adapter, width, height)
.expect("The surface isn't supported by this adapter")
},
);
}
@@ -189,79 +196,15 @@ impl Painter {
pub async fn set_window(
&mut self,
viewport_id: ViewportId,
window: Option<&winit::window::Window>,
window: Option<Arc<winit::window::Window>>,
) -> Result<(), crate::WgpuError> {
crate::profile_scope!("Painter::set_window"); // profle_function gives bad names for async functions
crate::profile_scope!("Painter::set_window"); // profile_function gives bad names for async functions
if let Some(window) = window {
let size = window.inner_size();
if self.surfaces.get(&viewport_id).is_none() {
let surface = unsafe {
crate::profile_scope!("create_surface");
self.instance.create_surface(&window)?
};
let render_state = if let Some(render_state) = &self.render_state {
render_state
} else {
let render_state = RenderState::create(
&self.configuration,
&self.instance,
&surface,
self.depth_format,
self.msaa_samples,
)
.await?;
self.render_state.get_or_insert(render_state)
};
let alpha_mode = if self.support_transparent_backbuffer {
let supported_alpha_modes =
surface.get_capabilities(&render_state.adapter).alpha_modes;
// Prefer pre multiplied over post multiplied!
if supported_alpha_modes.contains(&wgpu::CompositeAlphaMode::PreMultiplied) {
wgpu::CompositeAlphaMode::PreMultiplied
} else if supported_alpha_modes
.contains(&wgpu::CompositeAlphaMode::PostMultiplied)
{
wgpu::CompositeAlphaMode::PostMultiplied
} else {
log::warn!("Transparent window was requested, but the active wgpu surface does not support a `CompositeAlphaMode` with transparency.");
wgpu::CompositeAlphaMode::Auto
}
} else {
wgpu::CompositeAlphaMode::Auto
};
let supports_screenshot =
!matches!(render_state.adapter.get_info().backend, wgpu::Backend::Gl);
self.surfaces.insert(
viewport_id,
SurfaceState {
surface,
width: size.width,
height: size.height,
alpha_mode,
supports_screenshot,
},
);
let Some(width) = NonZeroU32::new(size.width) else {
log::debug!("The window width was zero; skipping generate textures");
return Ok(());
};
let Some(height) = NonZeroU32::new(size.height) else {
log::debug!("The window height was zero; skipping generate textures");
return Ok(());
};
self.resize_and_generate_depth_texture_view_and_msaa_view(
viewport_id,
width,
height,
);
let surface = self.instance.create_surface(window)?;
self.add_surface(surface, viewport_id, size).await?;
}
} else {
log::warn!("No window - clearing all surfaces");
@@ -270,6 +213,93 @@ impl Painter {
Ok(())
}
/// Updates (or clears) the [`winit::window::Window`] associated with the [`Painter`] without taking ownership of the window.
///
/// Like [`set_window`](Self::set_window) except:
///
/// # Safety
/// The user is responsible for ensuring that the window is alive for as long as it is set.
pub async unsafe fn set_window_unsafe(
&mut self,
viewport_id: ViewportId,
window: Option<&winit::window::Window>,
) -> Result<(), crate::WgpuError> {
crate::profile_scope!("Painter::set_window_unsafe"); // profile_function gives bad names for async functions
if let Some(window) = window {
let size = window.inner_size();
if self.surfaces.get(&viewport_id).is_none() {
let surface = unsafe {
self.instance
.create_surface_unsafe(wgpu::SurfaceTargetUnsafe::from_window(&window)?)?
};
self.add_surface(surface, viewport_id, size).await?;
}
} else {
log::warn!("No window - clearing all surfaces");
self.surfaces.clear();
}
Ok(())
}
async fn add_surface(
&mut self,
surface: wgpu::Surface<'static>,
viewport_id: ViewportId,
size: winit::dpi::PhysicalSize<u32>,
) -> Result<(), crate::WgpuError> {
let render_state = if let Some(render_state) = &self.render_state {
render_state
} else {
let render_state = RenderState::create(
&self.configuration,
&self.instance,
&surface,
self.depth_format,
self.msaa_samples,
)
.await?;
self.render_state.get_or_insert(render_state)
};
let alpha_mode = if self.support_transparent_backbuffer {
let supported_alpha_modes = surface.get_capabilities(&render_state.adapter).alpha_modes;
// Prefer pre multiplied over post multiplied!
if supported_alpha_modes.contains(&wgpu::CompositeAlphaMode::PreMultiplied) {
wgpu::CompositeAlphaMode::PreMultiplied
} else if supported_alpha_modes.contains(&wgpu::CompositeAlphaMode::PostMultiplied) {
wgpu::CompositeAlphaMode::PostMultiplied
} else {
log::warn!("Transparent window was requested, but the active wgpu surface does not support a `CompositeAlphaMode` with transparency.");
wgpu::CompositeAlphaMode::Auto
}
} else {
wgpu::CompositeAlphaMode::Auto
};
let supports_screenshot =
!matches!(render_state.adapter.get_info().backend, wgpu::Backend::Gl);
self.surfaces.insert(
viewport_id,
SurfaceState {
surface,
width: size.width,
height: size.height,
alpha_mode,
supports_screenshot,
},
);
let Some(width) = NonZeroU32::new(size.width) else {
log::debug!("The window width was zero; skipping generate textures");
return Ok(());
};
let Some(height) = NonZeroU32::new(size.height) else {
log::debug!("The window height was zero; skipping generate textures");
return Ok(());
};
self.resize_and_generate_depth_texture_view_and_msaa_view(viewport_id, width, height);
Ok(())
}
/// Returns the maximum texture dimension supported if known
///
/// This API will only return a known dimension after `set_window()` has been called

View File

@@ -61,7 +61,7 @@ egui = { version = "0.25.0", path = "../egui", default-features = false, feature
log = { version = "0.4", features = ["std"] }
raw-window-handle.workspace = true
web-time = { version = "0.2" } # We use web-time so we can (maybe) compile for web
winit = { version = "0.29.4", default-features = false, features = ["rwh_05"] }
winit = { workspace = true, default-features = false, features = ["rwh_06"] }
#! ### Optional dependencies

View File

@@ -1,4 +1,4 @@
use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::RawDisplayHandle;
/// Handles interfacing with the OS clipboard.
///
@@ -26,7 +26,7 @@ pub struct Clipboard {
impl Clipboard {
/// Construct a new instance
pub fn new(_display_target: &dyn HasRawDisplayHandle) -> Self {
pub fn new(_raw_display_handle: Option<RawDisplayHandle>) -> Self {
Self {
#[cfg(all(feature = "arboard", not(target_os = "android")))]
arboard: init_arboard(),
@@ -41,7 +41,7 @@ impl Clipboard {
),
feature = "smithay-clipboard"
))]
smithay: init_smithay_clipboard(_display_target),
smithay: init_smithay_clipboard(_raw_display_handle),
clipboard: Default::default(),
}
@@ -135,15 +135,14 @@ fn init_arboard() -> Option<arboard::Clipboard> {
feature = "smithay-clipboard"
))]
fn init_smithay_clipboard(
_display_target: &dyn HasRawDisplayHandle,
raw_display_handle: Option<RawDisplayHandle>,
) -> Option<smithay_clipboard::Clipboard> {
crate::profile_function!();
use raw_window_handle::RawDisplayHandle;
if let RawDisplayHandle::Wayland(display) = _display_target.raw_display_handle() {
if let Some(RawDisplayHandle::Wayland(display)) = raw_display_handle {
log::debug!("Initializing smithay clipboard…");
#[allow(unsafe_code)]
Some(unsafe { smithay_clipboard::Clipboard::new(display.display) })
Some(unsafe { smithay_clipboard::Clipboard::new(display.display.as_ptr()) })
} else {
#[cfg(feature = "wayland")]
log::debug!("Cannot init smithay clipboard without a Wayland display handle");

View File

@@ -22,7 +22,7 @@ mod window_settings;
pub use window_settings::WindowSettings;
use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::HasDisplayHandle;
#[allow(unused_imports)]
pub(crate) use profiling_scopes::*;
@@ -106,7 +106,7 @@ impl State {
pub fn new(
egui_ctx: egui::Context,
viewport_id: ViewportId,
display_target: &dyn HasRawDisplayHandle,
display_target: &dyn HasDisplayHandle,
native_pixels_per_point: Option<f32>,
max_texture_side: Option<usize>,
) -> Self {
@@ -126,7 +126,9 @@ impl State {
any_pointer_button_down: false,
current_cursor_icon: None,
clipboard: clipboard::Clipboard::new(display_target),
clipboard: clipboard::Clipboard::new(
display_target.display_handle().ok().map(|h| h.as_raw()),
),
simulate_touch_screen: false,
pointer_touch_id: None,

View File

@@ -29,7 +29,7 @@ serde = ["dep:serde", "egui_demo_lib/serde", "egui/serde"]
syntect = ["egui_demo_lib/syntect"]
glow = ["eframe/glow"]
wgpu = ["eframe/wgpu", "bytemuck"]
wgpu = ["eframe/wgpu", "bytemuck", "dep:wgpu"]
[dependencies]
chrono = { version = "0.4", default-features = false, features = [
@@ -57,6 +57,8 @@ log = { version = "0.4", features = ["std"] }
bytemuck = { version = "1.7.1", optional = true }
puffin = { version = "0.18", optional = true }
puffin_http = { version = "0.15", optional = true }
# Enable both WebGL & WebGPU when targeting the web (these features have no effect when not targeting wasm32)
wgpu = { workspace = true, features = ["webgpu", "webgl"], optional = true }
# feature "http":
@@ -81,6 +83,6 @@ rfd = { version = "0.11", optional = true }
# web:
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "=0.2.89"
wasm-bindgen = "=0.2.90"
wasm-bindgen-futures = "0.4"
web-sys = "0.3"

View File

@@ -184,6 +184,103 @@ fn integration_ui(ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
ui.monospace(format!("{:#?}", _frame.info().web_info.location));
});
#[cfg(feature = "glow")]
if _frame.gl().is_some() {
ui.horizontal(|ui| {
ui.label("Renderer:");
ui.hyperlink_to("glow", "https://github.com/grovesNL/glow");
});
}
#[cfg(feature = "wgpu")]
if let Some(render_state) = _frame.wgpu_render_state() {
let wgpu_adapter_details_ui = |ui: &mut egui::Ui, adapter: &eframe::wgpu::Adapter| {
let info = &adapter.get_info();
let wgpu::AdapterInfo {
name,
vendor,
device,
device_type,
driver,
driver_info,
backend,
} = &info;
// Example values:
// > name: "llvmpipe (LLVM 16.0.6, 256 bits)", device_type: Cpu, backend: Vulkan, driver: "llvmpipe", driver_info: "Mesa 23.1.6-arch1.4 (LLVM 16.0.6)"
// > name: "Apple M1 Pro", device_type: IntegratedGpu, backend: Metal, driver: "", driver_info: ""
// > name: "ANGLE (Apple, Apple M1 Pro, OpenGL 4.1)", device_type: IntegratedGpu, backend: Gl, driver: "", driver_info: ""
egui::Grid::new("adapter_info").show(ui, |ui| {
ui.label("Backend:");
ui.label(format!("{backend:?}"));
ui.end_row();
ui.label("Device Type:");
ui.label(format!("{device_type:?}"));
ui.end_row();
if !name.is_empty() {
ui.label("Name:");
ui.label(format!("{name:?}"));
ui.end_row();
}
if !driver.is_empty() {
ui.label("Driver:");
ui.label(format!("{driver:?}"));
ui.end_row();
}
if !driver_info.is_empty() {
ui.label("Driver info:");
ui.label(format!("{driver_info:?}"));
ui.end_row();
}
if *vendor != 0 {
// TODO(emilk): decode using https://github.com/gfx-rs/wgpu/blob/767ac03245ee937d3dc552edc13fe7ab0a860eec/wgpu-hal/src/auxil/mod.rs#L7
ui.label("Vendor:");
ui.label(format!("0x{vendor:04X}"));
ui.end_row();
}
if *device != 0 {
ui.label("Device:");
ui.label(format!("0x{device:02X}"));
ui.end_row();
}
});
};
let wgpu_adapter_ui = |ui: &mut egui::Ui, adapter: &eframe::wgpu::Adapter| {
let info = &adapter.get_info();
ui.label(format!("{:?}", info.backend)).on_hover_ui(|ui| {
wgpu_adapter_details_ui(ui, adapter);
});
};
egui::Grid::new("wgpu_info").num_columns(2).show(ui, |ui| {
ui.label("Renderer:");
ui.hyperlink_to("wgpu", "https://wgpu.rs/");
ui.end_row();
ui.label("Backend:");
wgpu_adapter_ui(ui, &render_state.adapter);
ui.end_row();
#[cfg(not(target_arch = "wasm32"))]
if 1 < render_state.available_adapters.len() {
ui.label("Others:");
ui.vertical(|ui| {
for adapter in &*render_state.available_adapters {
if adapter.get_info() != render_state.adapter.get_info() {
wgpu_adapter_ui(ui, adapter);
}
}
});
ui.end_row();
}
});
}
#[cfg(not(target_arch = "wasm32"))]
{
ui.horizontal(|ui| {
@@ -214,15 +311,6 @@ fn integration_ui(ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
ui.close_menu();
}
});
let fullscreen = ui.input(|i| i.viewport().fullscreen.unwrap_or(false));
if !fullscreen
&& ui
.button("Drag me to drag window")
.is_pointer_button_down_on()
{
ui.ctx().send_viewport_cmd(egui::ViewportCommand::StartDrag);
}
}
}

View File

@@ -40,7 +40,7 @@ links = ["egui-winit?/links"]
puffin = ["dep:puffin", "egui-winit?/puffin", "egui/puffin"]
## Enable [`winit`](https://docs.rs/winit) integration.
winit = ["egui-winit"]
winit = ["egui-winit", "dep:winit"]
[dependencies]
@@ -52,6 +52,10 @@ bytemuck = "1.7"
glow.workspace = true
log = { version = "0.4", features = ["std"] }
memoffset = "0.7"
# glutin stuck on old version of raw-window-handle:
rwh_05 = { package = "raw-window-handle", version = "0.5.2", features = [
"std",
] }
#! ### Optional dependencies
## Enable this when generating docs.
@@ -61,6 +65,10 @@ document-features = { version = "0.2", optional = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
egui-winit = { version = "0.25.0", path = "../egui-winit", optional = true, default-features = false }
puffin = { workspace = true, optional = true }
winit = { workspace = true, optional = true, default-features = false, features = [
"rwh_05", # glutin stuck on old version of raw-window-handle
"rwh_06", # for compatibility with egui-winit
] }
# Web:
[target.'cfg(target_arch = "wasm32")'.dependencies]
@@ -69,8 +77,7 @@ wasm-bindgen = "0.2"
[dev-dependencies]
glutin = "0.31" # examples/pure_glow
raw-window-handle.workspace = true
glutin = "0.31" # examples/pure_glow
glutin-winit = "0.4.0"

View File

@@ -24,7 +24,7 @@ impl GlutinWindowContext {
use glutin::display::GetGlDisplay;
use glutin::display::GlDisplay;
use glutin::prelude::GlSurface;
use raw_window_handle::HasRawWindowHandle;
use rwh_05::HasRawWindowHandle;
let winit_window_builder = winit::window::WindowBuilder::new()
.with_resizable(true)
.with_inner_size(winit::dpi::LogicalSize {