From 05ae83af5ed55e826b91ea339f4210d586dfa337 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Tue, 20 Oct 2026 19:38:14 +0200 Subject: [PATCH] fix(metal): permit acquiring drawables until the window has been visible (#10302) --- CHANGELOG.md | 1 + wgpu-hal/src/metal/mod.rs | 3 +++ wgpu-hal/src/metal/surface.rs | 13 ++++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 776056bc4..837e9124a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -289,6 +289,7 @@ By @sagudev in [#10307](https://github.com/gfx-rs/wgpu/pull/10307). - Fix a crash in `Surface::configure` on iOS below 16. `wantsExtendedDynamicRangeContent` is iOS 16+ and is now only called there. By @VladasZ in [#10257](https://github.com/gfx-rs/wgpu/pull/10257). - Fix a crash when creating a declared alternate sRGB view of a render-attachment-only surface with Metal API Validation enabled. By @jinleili in [#10280](https://github.com/gfx-rs/wgpu/pull/10280). - Fix oversaturated sRGB surface colors on wide-gamut displays by explicitly setting the layer's sRGB color space. By @jinleili in [#10286](https://github.com/gfx-rs/wgpu/pull/10286). +- To avoid rendering failures in new windows (see [#9430](https://github.com/gfx-rs/wgpu/issues/9430)), do not return `SurfaceStatus::Occluded` from `Surface::get_current_texture` when it is called for a window that hasn't been visible yet. By @tronical in [#10302](https://github.com/gfx-rs/wgpu/pull/10302). #### GLES diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index a2b3f04b2..3f62d5d09 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -649,6 +649,9 @@ pub struct Surface { render_layer: Mutex>, swapchain_format: RwLock>, extent: RwLock, + /// Whether the hosting window has been visible at some point, see `acquire_texture`. + #[cfg(target_os = "macos")] + has_been_visible: atomic::AtomicBool, } unsafe impl Send for Surface {} diff --git a/wgpu-hal/src/metal/surface.rs b/wgpu-hal/src/metal/surface.rs index 13ee7a39b..92f8b9260 100644 --- a/wgpu-hal/src/metal/surface.rs +++ b/wgpu-hal/src/metal/surface.rs @@ -38,6 +38,8 @@ impl super::Surface { render_layer: Mutex::new(layer), swapchain_format: RwLock::new(None), extent: RwLock::new(wgt::Extent3d::default()), + #[cfg(target_os = "macos")] + has_been_visible: wgpu_sync::atomic::AtomicBool::new(false), } } @@ -369,14 +371,23 @@ impl crate::Surface for super::Surface { // When the window is occluded on macOS, presented drawables get stuck waiting // for vsync. Check the window's occlusion state and skip acquisition if // the window is not visible - this avoids a 1-second hang in nextDrawable(). + // + // Only drawables presented while the window was visible get stuck, so this + // waits until the window has been visible once. Until then, nextDrawable() + // returns right away, and applications rely on that to render a first frame + // before the window is shown, or right after, while AppKit hasn't updated the + // occlusion state yet. use objc2::rc::Retained; + use wgpu_sync::atomic; // The CAMetalLayer is typically a sublayer; find the hosting window // and skip acquisition while it is occluded. if let Some(window) = hosting_window(Retained::into_super(render_layer.clone())) { const NS_WINDOW_OCCLUSION_STATE_VISIBLE: usize = 1 << 1; let occlusion_state: usize = unsafe { objc2::msg_send![&*window, occlusionState] }; - if occlusion_state & NS_WINDOW_OCCLUSION_STATE_VISIBLE == 0 { + if occlusion_state & NS_WINDOW_OCCLUSION_STATE_VISIBLE != 0 { + self.has_been_visible.store(true, atomic::Ordering::Relaxed); + } else if self.has_been_visible.load(atomic::Ordering::Relaxed) { return Err(crate::SurfaceError::Occluded); } }