fix(metal): permit acquiring drawables until the window has been visible (#10302)

This commit is contained in:
Simon Hausmann authored and GitHub committed 2026-10-20 17:38:14 +00:00
1 parent 0342e2fba5
commit 05ae83af5e
3 files changed
+16 -1

No files matched your search

+1
View File
@@ -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
+3
View File
@@ -649,6 +649,9 @@ pub struct Surface {
render_layer: Mutex<Retained<CAMetalLayer>>,
swapchain_format: RwLock<Option<wgt::TextureFormat>>,
extent: RwLock<wgt::Extent3d>,
/// 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 {}
+12 -1
View File
@@ -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);
}
}