Skip to main content

egui_wgpu/
setup.rs

1use std::sync::Arc;
2
3/// A cloneable display handle for use with [`wgpu::InstanceDescriptor`].
4///
5/// [`wgpu::InstanceDescriptor`] stores its display handle as a non-cloneable
6/// `Box<dyn WgpuHasDisplayHandle>`. This trait wraps it so it can be cloned
7/// alongside the rest of the egui wgpu configuration.
8///
9/// Automatically implemented for all types that satisfy the bounds
10/// (including [`winit::event_loop::OwnedDisplayHandle`]).
11pub trait EguiDisplayHandle:
12    wgpu::rwh::HasDisplayHandle + std::fmt::Debug + Send + Sync + 'static
13{
14    /// Clone into a `Box<dyn WgpuHasDisplayHandle>` for [`wgpu::InstanceDescriptor::display`].
15    fn clone_for_wgpu(&self) -> Box<dyn wgpu::wgt::WgpuHasDisplayHandle>;
16
17    /// Clone into a new `Box<dyn EguiDisplayHandle>`.
18    fn clone_display_handle(&self) -> Box<dyn EguiDisplayHandle>;
19}
20
21impl Clone for Box<dyn EguiDisplayHandle> {
22    fn clone(&self) -> Self {
23        // We need to deref here, otherwise this causes infinite recursion stack overflow.
24        (**self).clone_display_handle()
25    }
26}
27
28impl<T> EguiDisplayHandle for T
29where
30    T: wgpu::rwh::HasDisplayHandle + Clone + std::fmt::Debug + Send + Sync + 'static,
31{
32    fn clone_for_wgpu(&self) -> Box<dyn wgpu::wgt::WgpuHasDisplayHandle> {
33        Box::new(self.clone())
34    }
35
36    fn clone_display_handle(&self) -> Box<dyn EguiDisplayHandle> {
37        Box::new(self.clone())
38    }
39}
40
41#[derive(Clone)]
42pub enum WgpuSetup {
43    /// Construct a wgpu setup using some predefined settings & heuristics.
44    /// This is the default option. You can customize most behaviours overriding the
45    /// supported backends, power preferences, and device description.
46    ///
47    /// By default can also be configured with various environment variables:
48    /// * `WGPU_BACKEND`: `vulkan`, `dx12`, `metal`, `opengl`, `webgpu`
49    /// * `WGPU_POWER_PREF`: `low`, `high` or `none`
50    /// * `WGPU_TRACE`: Path to a file to output a wgpu trace file.
51    ///
52    /// Each instance flag also comes with an environment variable (for details see [`wgpu::InstanceFlags`]):
53    /// * `WGPU_VALIDATION`: Enables validation (enabled by default in debug builds).
54    /// * `WGPU_DEBUG`: Generate debug information in shaders and objects  (enabled by default in debug builds).
55    /// * `WGPU_ALLOW_UNDERLYING_NONCOMPLIANT_ADAPTER`: Whether wgpu should expose adapters that run on top of non-compliant adapters.
56    /// * `WGPU_GPU_BASED_VALIDATION`: Enable GPU-based validation.
57    CreateNew(WgpuSetupCreateNew),
58
59    /// Run on an existing wgpu setup.
60    Existing(WgpuSetupExisting),
61}
62
63impl WgpuSetup {
64    /// Creates a new [`WgpuSetup::CreateNew`] with the given display handle.
65    ///
66    /// See [`WgpuSetupCreateNew::from_display_handle`] for details.
67    pub fn from_display_handle(display_handle: impl EguiDisplayHandle) -> Self {
68        Self::CreateNew(WgpuSetupCreateNew::from_display_handle(display_handle))
69    }
70
71    /// Creates a new [`WgpuSetup::CreateNew`] without a display handle.
72    ///
73    /// See [`WgpuSetupCreateNew::without_display_handle`] for details.
74    pub fn without_display_handle() -> Self {
75        Self::CreateNew(WgpuSetupCreateNew::without_display_handle())
76    }
77}
78
79impl std::fmt::Debug for WgpuSetup {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        match self {
82            Self::CreateNew(create_new) => f
83                .debug_tuple("WgpuSetup::CreateNew")
84                .field(create_new)
85                .finish(),
86            Self::Existing { .. } => f.debug_tuple("WgpuSetup::Existing").finish(),
87        }
88    }
89}
90
91impl WgpuSetup {
92    /// Creates a new [`wgpu::Instance`] or clones the existing one.
93    ///
94    /// Does *not* store the wgpu instance, so calling this repeatedly may
95    /// create a new instance every time!
96    pub async fn new_instance(&self) -> wgpu::Instance {
97        match self {
98            Self::CreateNew(create_new) => {
99                #[allow(clippy::allow_attributes, unused_mut)]
100                let mut backends = create_new.instance_descriptor.backends;
101
102                // Don't try WebGPU if we're not in a secure context.
103                #[cfg(target_arch = "wasm32")]
104                if backends.contains(wgpu::Backends::BROWSER_WEBGPU) {
105                    let is_secure_context =
106                        wgpu::web_sys::window().is_some_and(|w| w.is_secure_context());
107                    if !is_secure_context {
108                        log::info!(
109                            "WebGPU is only available in secure contexts, i.e. on HTTPS and on localhost."
110                        );
111                        backends.remove(wgpu::Backends::BROWSER_WEBGPU);
112                    }
113                }
114
115                log::debug!("Creating wgpu instance with backends {backends:?}");
116                let desc = &create_new.instance_descriptor;
117                let descriptor = wgpu::InstanceDescriptor {
118                    backends: desc.backends,
119                    flags: desc.flags,
120                    backend_options: desc.backend_options.clone(),
121                    memory_budget_thresholds: desc.memory_budget_thresholds,
122                    display: create_new
123                        .display_handle
124                        .as_ref()
125                        .map(|handle| handle.clone_for_wgpu()),
126                };
127                wgpu::util::new_instance_with_webgpu_detection(descriptor).await
128            }
129            Self::Existing(existing) => existing.instance.clone(),
130        }
131    }
132}
133
134impl From<WgpuSetupCreateNew> for WgpuSetup {
135    fn from(create_new: WgpuSetupCreateNew) -> Self {
136        Self::CreateNew(create_new)
137    }
138}
139
140impl From<WgpuSetupExisting> for WgpuSetup {
141    fn from(existing: WgpuSetupExisting) -> Self {
142        Self::Existing(existing)
143    }
144}
145
146/// Method for selecting an adapter on native.
147///
148/// This can be used for fully custom adapter selection.
149/// If available, `wgpu::Surface` is passed to allow checking for surface compatibility.
150pub type NativeAdapterSelectorMethod = Arc<
151    dyn Fn(&[wgpu::Adapter], Option<&wgpu::Surface<'_>>) -> Result<wgpu::Adapter, String>
152        + Send
153        + Sync,
154>;
155
156/// Configuration for creating a new wgpu setup.
157///
158/// Used for [`WgpuSetup::CreateNew`].
159///
160/// Prefer [`Self::from_display_handle`] when you have a display handle available.
161/// Most platforms work without one, but some (e.g. Wayland with GLES, or WebGL)
162/// require it, so providing one ensures maximum compatibility.
163/// With winit, pass [`EventLoop::owned_display_handle`](winit::event_loop::EventLoop::owned_display_handle).
164///
165/// Note: The display handle is stored in [`Self::display_handle`] rather than in
166/// [`Self::instance_descriptor`] so the config can be cloned
167/// ([`wgpu::InstanceDescriptor`] is not `Clone`). It is injected at instance creation time.
168pub struct WgpuSetupCreateNew {
169    /// Descriptor for the wgpu instance.
170    ///
171    /// Leave [`wgpu::InstanceDescriptor::display`] as `None` — use [`Self::display_handle`]
172    /// instead (injected at instance creation time).
173    ///
174    /// The most important field is [`wgpu::InstanceDescriptor::backends`], which controls
175    /// which backends are supported (wgpu will pick one of these). For example, set it to
176    /// [`wgpu::Backends::GL`] to use only WebGL. By default on web, WebGPU is preferred
177    /// with WebGL as a fallback (requires the `webgl` feature of crate `wgpu`).
178    pub instance_descriptor: wgpu::InstanceDescriptor,
179
180    /// Display handle passed to wgpu at instance creation time.
181    ///
182    /// Required on some platforms (e.g. Wayland with GLES, WebGL); optional elsewhere.
183    /// With winit, use [`winit::event_loop::OwnedDisplayHandle`].
184    ///
185    /// `eframe` 's winit & web integrations will attempt to fill the display handle automatically if it is left empty.
186    pub display_handle: Option<Box<dyn EguiDisplayHandle>>,
187
188    /// Power preference for the adapter if [`Self::native_adapter_selector`] is not set or targeting web.
189    pub power_preference: wgpu::PowerPreference,
190
191    /// Optional selector for native adapters.
192    ///
193    /// This field has no effect when targeting web!
194    /// Otherwise, if set [`Self::power_preference`] is ignored and the adapter is instead selected by this method.
195    /// Note that [`Self::instance_descriptor`]'s [`wgpu::InstanceDescriptor::backends`]
196    /// are still used to filter the adapter enumeration in the first place.
197    ///
198    /// Defaults to `None`.
199    pub native_adapter_selector: Option<NativeAdapterSelectorMethod>,
200
201    /// Configuration passed on device request, given an adapter
202    pub device_descriptor:
203        Arc<dyn Fn(&wgpu::Adapter) -> wgpu::DeviceDescriptor<'static> + Send + Sync>,
204}
205
206impl WgpuSetupCreateNew {
207    /// Creates a new configuration with the given display handle.
208    ///
209    /// This is the recommended constructor. Most platforms (Windows, macOS/iOS, Android, web)
210    /// work fine without a display handle, but some (e.g. Wayland on Linux with GLES) require
211    /// one. Providing it unconditionally ensures your app works everywhere.
212    ///
213    /// If you don't have a display handle available, use [`Self::without_display_handle`]
214    /// instead — it will still work on the majority of platforms.
215    ///
216    /// With winit, pass [`EventLoop::owned_display_handle`](winit::event_loop::EventLoop::owned_display_handle).
217    pub fn from_display_handle(display_handle: impl EguiDisplayHandle) -> Self {
218        Self {
219            display_handle: Some(Box::new(display_handle)),
220            ..Self::without_display_handle()
221        }
222    }
223
224    /// Creates a new configuration without a display handle.
225    ///
226    /// A display handle is not required for headless operation (offscreen rendering, tests,
227    /// compute-only workloads). It also isn't needed on most platforms even when presenting
228    /// to a window — only some configurations (e.g. Wayland on Linux with GLES) require one.
229    ///
230    /// If you do have a display handle available, prefer [`Self::from_display_handle`] for
231    /// maximum compatibility.
232    ///
233    /// With winit you can obtain one via [`EventLoop::owned_display_handle`](winit::event_loop::EventLoop::owned_display_handle).
234    ///
235    /// `eframe` 's winit & web integrations will attempt to fill the display handle automatically if it is left empty.
236    pub fn without_display_handle() -> Self {
237        Self {
238            instance_descriptor: wgpu::InstanceDescriptor {
239                // Add GL backend, primarily because WebGPU is not stable enough yet.
240                // (note however, that the GL backend needs to be opted-in via the wgpu feature flag "webgl")
241                backends: wgpu::Backends::from_env()
242                    .unwrap_or(wgpu::Backends::PRIMARY | wgpu::Backends::GL),
243                flags: wgpu::InstanceFlags::from_build_config().with_env(),
244                backend_options: wgpu::BackendOptions::from_env_or_default(),
245                memory_budget_thresholds: wgpu::MemoryBudgetThresholds::default(),
246                display: None,
247            },
248
249            display_handle: None,
250
251            power_preference: wgpu::PowerPreference::from_env()
252                .unwrap_or(wgpu::PowerPreference::HighPerformance),
253
254            native_adapter_selector: None,
255
256            device_descriptor: Arc::new(|adapter| {
257                let base_limits = if adapter.get_info().backend == wgpu::Backend::Gl {
258                    wgpu::Limits::downlevel_webgl2_defaults()
259                } else {
260                    wgpu::Limits::default()
261                };
262
263                wgpu::DeviceDescriptor {
264                    label: Some("egui wgpu device"),
265                    required_limits: wgpu::Limits {
266                        // When using a depth buffer, we have to be able to create a texture
267                        // large enough for the entire surface, and we want to support 4k+ displays.
268                        max_texture_dimension_2d: 8192,
269                        ..base_limits
270                    },
271                    ..Default::default()
272                }
273            }),
274        }
275    }
276}
277
278impl Clone for WgpuSetupCreateNew {
279    fn clone(&self) -> Self {
280        let desc = &self.instance_descriptor;
281        Self {
282            instance_descriptor: wgpu::InstanceDescriptor {
283                backends: desc.backends,
284                flags: desc.flags,
285                backend_options: desc.backend_options.clone(),
286                memory_budget_thresholds: desc.memory_budget_thresholds,
287                display: None,
288            },
289            display_handle: self.display_handle.clone(),
290            power_preference: self.power_preference,
291            native_adapter_selector: self.native_adapter_selector.clone(),
292            device_descriptor: Arc::clone(&self.device_descriptor),
293        }
294    }
295}
296
297impl std::fmt::Debug for WgpuSetupCreateNew {
298    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
299        f.debug_struct("WgpuSetupCreateNew")
300            .field("instance_descriptor", &self.instance_descriptor)
301            .field("display_handle", &self.display_handle)
302            .field("power_preference", &self.power_preference)
303            .field(
304                "native_adapter_selector",
305                &self.native_adapter_selector.is_some(),
306            )
307            .finish()
308    }
309}
310
311/// Configuration for using an existing wgpu setup.
312///
313/// Used for [`WgpuSetup::Existing`].
314#[derive(Clone)]
315pub struct WgpuSetupExisting {
316    pub instance: wgpu::Instance,
317    pub adapter: wgpu::Adapter,
318    pub device: wgpu::Device,
319    pub queue: wgpu::Queue,
320}