wgpu_context.rs

  1#[cfg(not(target_family = "wasm"))]
  2use anyhow::Context as _;
  3#[cfg(not(target_family = "wasm"))]
  4use gpui_util::ResultExt;
  5use std::sync::Arc;
  6use std::sync::atomic::{AtomicBool, Ordering};
  7use wgpu::TextureFormat;
  8
  9pub struct WgpuContext {
 10    pub instance: wgpu::Instance,
 11    pub adapter: wgpu::Adapter,
 12    pub device: Arc<wgpu::Device>,
 13    pub queue: Arc<wgpu::Queue>,
 14    dual_source_blending: bool,
 15    color_texture_format: wgpu::TextureFormat,
 16    device_lost: Arc<AtomicBool>,
 17}
 18
 19#[derive(Clone, Copy)]
 20pub struct CompositorGpuHint {
 21    pub vendor_id: u32,
 22    pub device_id: u32,
 23}
 24
 25impl WgpuContext {
 26    #[cfg(not(target_family = "wasm"))]
 27    pub fn new(
 28        instance: wgpu::Instance,
 29        surface: &wgpu::Surface<'_>,
 30        compositor_gpu: Option<CompositorGpuHint>,
 31    ) -> anyhow::Result<Self> {
 32        let device_id_filter = match std::env::var("ZED_DEVICE_ID") {
 33            Ok(val) => parse_pci_id(&val)
 34                .context("Failed to parse device ID from `ZED_DEVICE_ID` environment variable")
 35                .log_err(),
 36            Err(std::env::VarError::NotPresent) => None,
 37            err => {
 38                err.context("Failed to read value of `ZED_DEVICE_ID` environment variable")
 39                    .log_err();
 40                None
 41            }
 42        };
 43
 44        // Select an adapter by actually testing surface configuration with the real device.
 45        // This is the only reliable way to determine compatibility on hybrid GPU systems.
 46        let (adapter, device, queue, dual_source_blending, color_texture_format) =
 47            pollster::block_on(Self::select_adapter_and_device(
 48                &instance,
 49                device_id_filter,
 50                surface,
 51                compositor_gpu.as_ref(),
 52            ))?;
 53
 54        let device_lost = Arc::new(AtomicBool::new(false));
 55        device.set_device_lost_callback({
 56            let device_lost = Arc::clone(&device_lost);
 57            move |reason, message| {
 58                log::error!("wgpu device lost: reason={reason:?}, message={message}");
 59                if reason != wgpu::DeviceLostReason::Destroyed {
 60                    device_lost.store(true, Ordering::Relaxed);
 61                }
 62            }
 63        });
 64
 65        log::info!(
 66            "Selected GPU adapter: {:?} ({:?})",
 67            adapter.get_info().name,
 68            adapter.get_info().backend
 69        );
 70
 71        Ok(Self {
 72            instance,
 73            adapter,
 74            device: Arc::new(device),
 75            queue: Arc::new(queue),
 76            dual_source_blending,
 77            color_texture_format,
 78            device_lost,
 79        })
 80    }
 81
 82    #[cfg(target_family = "wasm")]
 83    pub async fn new_web() -> anyhow::Result<Self> {
 84        let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
 85            backends: wgpu::Backends::BROWSER_WEBGPU | wgpu::Backends::GL,
 86            flags: wgpu::InstanceFlags::default(),
 87            backend_options: wgpu::BackendOptions::default(),
 88            memory_budget_thresholds: wgpu::MemoryBudgetThresholds::default(),
 89            display: None,
 90        });
 91
 92        let adapter = instance
 93            .request_adapter(&wgpu::RequestAdapterOptions {
 94                power_preference: wgpu::PowerPreference::HighPerformance,
 95                compatible_surface: None,
 96                force_fallback_adapter: false,
 97            })
 98            .await
 99            .map_err(|e| anyhow::anyhow!("Failed to request GPU adapter: {e}"))?;
100
101        log::info!(
102            "Selected GPU adapter: {:?} ({:?})",
103            adapter.get_info().name,
104            adapter.get_info().backend
105        );
106
107        let device_lost = Arc::new(AtomicBool::new(false));
108        let (device, queue, dual_source_blending) = Self::create_device(&adapter).await?;
109
110        Ok(Self {
111            instance,
112            adapter,
113            device: Arc::new(device),
114            queue: Arc::new(queue),
115            dual_source_blending,
116            device_lost,
117        })
118    }
119
120    async fn create_device(
121        adapter: &wgpu::Adapter,
122    ) -> anyhow::Result<(wgpu::Device, wgpu::Queue, bool, TextureFormat)> {
123        let dual_source_blending = adapter
124            .features()
125            .contains(wgpu::Features::DUAL_SOURCE_BLENDING);
126
127        let mut required_features = wgpu::Features::empty();
128        if dual_source_blending {
129            required_features |= wgpu::Features::DUAL_SOURCE_BLENDING;
130        } else {
131            log::warn!(
132                "Dual-source blending not available on this GPU. \
133                Subpixel text antialiasing will be disabled."
134            );
135        }
136
137        let color_atlas_texture_format = Self::select_color_texture_format(adapter)?;
138
139        let (device, queue) = adapter
140            .request_device(&wgpu::DeviceDescriptor {
141                label: Some("gpui_device"),
142                required_features,
143                required_limits: wgpu::Limits::downlevel_defaults()
144                    .using_resolution(adapter.limits())
145                    .using_alignment(adapter.limits()),
146                memory_hints: wgpu::MemoryHints::MemoryUsage,
147                trace: wgpu::Trace::Off,
148                experimental_features: wgpu::ExperimentalFeatures::disabled(),
149            })
150            .await
151            .map_err(|e| anyhow::anyhow!("Failed to create wgpu device: {e}"))?;
152
153        Ok((
154            device,
155            queue,
156            dual_source_blending,
157            color_atlas_texture_format,
158        ))
159    }
160
161    #[cfg(not(target_family = "wasm"))]
162    pub fn instance(display: Box<dyn wgpu::wgt::WgpuHasDisplayHandle>) -> wgpu::Instance {
163        wgpu::Instance::new(wgpu::InstanceDescriptor {
164            backends: wgpu::Backends::VULKAN | wgpu::Backends::GL,
165            flags: wgpu::InstanceFlags::default(),
166            backend_options: wgpu::BackendOptions::default(),
167            memory_budget_thresholds: wgpu::MemoryBudgetThresholds::default(),
168            display: Some(display),
169        })
170    }
171
172    pub fn check_compatible_with_surface(&self, surface: &wgpu::Surface<'_>) -> anyhow::Result<()> {
173        let caps = surface.get_capabilities(&self.adapter);
174        if caps.formats.is_empty() {
175            let info = self.adapter.get_info();
176            anyhow::bail!(
177                "Adapter {:?} (backend={:?}, device={:#06x}) is not compatible with the \
178                 display surface for this window.",
179                info.name,
180                info.backend,
181                info.device,
182            );
183        }
184        Ok(())
185    }
186
187    /// Select an adapter and create a device, testing that the surface can actually be configured.
188    /// This is the only reliable way to determine compatibility on hybrid GPU systems, where
189    /// adapters may report surface compatibility via get_capabilities() but fail when actually
190    /// configuring (e.g., NVIDIA reporting Vulkan Wayland support but failing because the
191    /// Wayland compositor runs on the Intel GPU).
192    #[cfg(not(target_family = "wasm"))]
193    async fn select_adapter_and_device(
194        instance: &wgpu::Instance,
195        device_id_filter: Option<u32>,
196        surface: &wgpu::Surface<'_>,
197        compositor_gpu: Option<&CompositorGpuHint>,
198    ) -> anyhow::Result<(
199        wgpu::Adapter,
200        wgpu::Device,
201        wgpu::Queue,
202        bool,
203        TextureFormat,
204    )> {
205        let mut adapters: Vec<_> = instance.enumerate_adapters(wgpu::Backends::all()).await;
206
207        if adapters.is_empty() {
208            anyhow::bail!("No GPU adapters found");
209        }
210
211        if let Some(device_id) = device_id_filter {
212            log::info!("ZED_DEVICE_ID filter: {:#06x}", device_id);
213        }
214
215        // Sort adapters into a single priority order. Tiers (from highest to lowest):
216        //
217        // 1. ZED_DEVICE_ID match — explicit user override
218        // 2. Compositor GPU match — the GPU the display server is rendering on
219        // 3. Device type (Discrete > Integrated > Other > Virtual > Cpu).
220        //    "Other" ranks above "Virtual" because OpenGL seems to count as "Other".
221        // 4. Backend — prefer Vulkan/Metal/Dx12 over GL/etc.
222        adapters.sort_by_key(|adapter| {
223            let info = adapter.get_info();
224
225            // Backends like OpenGL report device=0 for all adapters, so
226            // device-based matching is only meaningful when non-zero.
227            let device_known = info.device != 0;
228
229            let user_override: u8 = match device_id_filter {
230                Some(id) if device_known && info.device == id => 0,
231                _ => 1,
232            };
233
234            let compositor_match: u8 = match compositor_gpu {
235                Some(hint)
236                    if device_known
237                        && info.vendor == hint.vendor_id
238                        && info.device == hint.device_id =>
239                {
240                    0
241                }
242                _ => 1,
243            };
244
245            let type_priority: u8 = match info.device_type {
246                wgpu::DeviceType::DiscreteGpu => 0,
247                wgpu::DeviceType::IntegratedGpu => 1,
248                wgpu::DeviceType::Other => 2,
249                wgpu::DeviceType::VirtualGpu => 3,
250                wgpu::DeviceType::Cpu => 4,
251            };
252
253            let backend_priority: u8 = match info.backend {
254                wgpu::Backend::Vulkan => 0,
255                wgpu::Backend::Metal => 0,
256                wgpu::Backend::Dx12 => 0,
257                _ => 1,
258            };
259
260            (
261                user_override,
262                compositor_match,
263                type_priority,
264                backend_priority,
265            )
266        });
267
268        // Log all available adapters (in sorted order)
269        log::info!("Found {} GPU adapter(s):", adapters.len());
270        for adapter in &adapters {
271            let info = adapter.get_info();
272            log::info!(
273                "  - {} (vendor={:#06x}, device={:#06x}, backend={:?}, type={:?})",
274                info.name,
275                info.vendor,
276                info.device,
277                info.backend,
278                info.device_type,
279            );
280        }
281
282        // Test each adapter by creating a device and configuring the surface
283        for adapter in adapters {
284            let info = adapter.get_info();
285            log::info!("Testing adapter: {} ({:?})...", info.name, info.backend);
286
287            match Self::try_adapter_with_surface(&adapter, surface).await {
288                Ok((device, queue, dual_source_blending, color_atlas_texture_format)) => {
289                    log::info!(
290                        "Selected GPU (passed configuration test): {} ({:?})",
291                        info.name,
292                        info.backend
293                    );
294                    return Ok((
295                        adapter,
296                        device,
297                        queue,
298                        dual_source_blending,
299                        color_atlas_texture_format,
300                    ));
301                }
302                Err(e) => {
303                    log::info!(
304                        "  Adapter {} ({:?}) failed: {}, trying next...",
305                        info.name,
306                        info.backend,
307                        e
308                    );
309                }
310            }
311        }
312
313        anyhow::bail!("No GPU adapter found that can configure the display surface")
314    }
315
316    /// Try to use an adapter with a surface by creating a device and testing configuration.
317    /// Returns the device and queue if successful, allowing them to be reused.
318    #[cfg(not(target_family = "wasm"))]
319    async fn try_adapter_with_surface(
320        adapter: &wgpu::Adapter,
321        surface: &wgpu::Surface<'_>,
322    ) -> anyhow::Result<(wgpu::Device, wgpu::Queue, bool, TextureFormat)> {
323        let caps = surface.get_capabilities(adapter);
324        if caps.formats.is_empty() {
325            anyhow::bail!("no compatible surface formats");
326        }
327        if caps.alpha_modes.is_empty() {
328            anyhow::bail!("no compatible alpha modes");
329        }
330
331        let (device, queue, dual_source_blending, color_atlas_texture_format) =
332            Self::create_device(adapter).await?;
333        let error_scope = device.push_error_scope(wgpu::ErrorFilter::Validation);
334
335        let test_config = wgpu::SurfaceConfiguration {
336            usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
337            format: caps.formats[0],
338            width: 64,
339            height: 64,
340            present_mode: wgpu::PresentMode::Fifo,
341            desired_maximum_frame_latency: 2,
342            alpha_mode: caps.alpha_modes[0],
343            view_formats: vec![],
344        };
345
346        surface.configure(&device, &test_config);
347
348        let error = error_scope.pop().await;
349        if let Some(e) = error {
350            anyhow::bail!("surface configuration failed: {e}");
351        }
352
353        Ok((
354            device,
355            queue,
356            dual_source_blending,
357            color_atlas_texture_format,
358        ))
359    }
360
361    fn select_color_texture_format(adapter: &wgpu::Adapter) -> anyhow::Result<wgpu::TextureFormat> {
362        let required_usages = wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST;
363        let bgra_features = adapter.get_texture_format_features(wgpu::TextureFormat::Bgra8Unorm);
364        if bgra_features.allowed_usages.contains(required_usages) {
365            return Ok(wgpu::TextureFormat::Bgra8Unorm);
366        }
367
368        let rgba_features = adapter.get_texture_format_features(wgpu::TextureFormat::Rgba8Unorm);
369        if rgba_features.allowed_usages.contains(required_usages) {
370            let info = adapter.get_info();
371            log::warn!(
372                "Adapter {} ({:?}) does not support Bgra8Unorm atlas textures with usages {:?}; \
373                 falling back to Rgba8Unorm atlas textures.",
374                info.name,
375                info.backend,
376                required_usages,
377            );
378            return Ok(wgpu::TextureFormat::Rgba8Unorm);
379        }
380
381        let info = adapter.get_info();
382        Err(anyhow::anyhow!(
383            "Adapter {} ({:?}, device={:#06x}) does not support a usable color atlas texture \
384             format with usages {:?}. Bgra8Unorm allowed usages: {:?}; \
385             Rgba8Unorm allowed usages: {:?}.",
386            info.name,
387            info.backend,
388            info.device,
389            required_usages,
390            bgra_features.allowed_usages,
391            rgba_features.allowed_usages,
392        ))
393    }
394    pub fn supports_dual_source_blending(&self) -> bool {
395        self.dual_source_blending
396    }
397
398    pub fn color_texture_format(&self) -> wgpu::TextureFormat {
399        self.color_texture_format
400    }
401
402    /// Returns true if the GPU device was lost (e.g., due to driver crash, suspend/resume).
403    /// When this returns true, the context should be recreated.
404    pub fn device_lost(&self) -> bool {
405        self.device_lost.load(Ordering::Relaxed)
406    }
407
408    /// Returns a clone of the device_lost flag for sharing with renderers.
409    pub(crate) fn device_lost_flag(&self) -> Arc<AtomicBool> {
410        Arc::clone(&self.device_lost)
411    }
412}
413
414#[cfg(not(target_family = "wasm"))]
415fn parse_pci_id(id: &str) -> anyhow::Result<u32> {
416    let mut id = id.trim();
417
418    if id.starts_with("0x") || id.starts_with("0X") {
419        id = &id[2..];
420    }
421    let is_hex_string = id.chars().all(|c| c.is_ascii_hexdigit());
422    let is_4_chars = id.len() == 4;
423    anyhow::ensure!(
424        is_4_chars && is_hex_string,
425        "Expected a 4 digit PCI ID in hexadecimal format"
426    );
427
428    u32::from_str_radix(id, 16).context("parsing PCI ID as hex")
429}
430
431#[cfg(test)]
432mod tests {
433    use super::parse_pci_id;
434
435    #[test]
436    fn test_parse_device_id() {
437        assert!(parse_pci_id("0xABCD").is_ok());
438        assert!(parse_pci_id("ABCD").is_ok());
439        assert!(parse_pci_id("abcd").is_ok());
440        assert!(parse_pci_id("1234").is_ok());
441        assert!(parse_pci_id("123").is_err());
442        assert_eq!(
443            parse_pci_id(&format!("{:x}", 0x1234)).unwrap(),
444            parse_pci_id(&format!("{:X}", 0x1234)).unwrap(),
445        );
446
447        assert_eq!(
448            parse_pci_id(&format!("{:#x}", 0x1234)).unwrap(),
449            parse_pci_id(&format!("{:#X}", 0x1234)).unwrap(),
450        );
451    }
452}