From 252b1dc3dd00655f124f7c47c254074bb1c8f4b9 Mon Sep 17 00:00:00 2001 From: Matt Hargett Date: Thu, 10 Sep 2026 16:07:07 -0700 Subject: [PATCH] [metal] report an error instead of panicking on texture view creation (#10145) --- CHANGELOG.md | 1 + wgpu-hal/src/metal/device.rs | 47 +++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 352c12f88..86172184c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -218,6 +218,7 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109). - Fix a crash/hang when configuring a surface with the `Bt2100Pq`, `Bt2100Hlg`, or `ExtendedDisplayP3` color space: the dynamically resolved CoreGraphics color-space constants were read with one level of indirection missing. By @stuartparmenter in [#10175](https://github.com/gfx-rs/wgpu/pull/10175). - Fix bind group resources for the task, mesh, fragment, and compute shader stages being bound from the wrong offsets whenever a bind group contained resources visible to the task or mesh stages, which could bind the wrong buffer, texture, or sampler to a shader slot. By @teoxoy in [#10043](https://github.com/gfx-rs/wgpu/issues/10043). +- Report an error instead of panicking when Metal declines to create a texture view, which can occur for some views of textures with `TRANSIENT_ATTACHMENT` usage. By @matthargett in [#10145](https://github.com/gfx-rs/wgpu/pull/10145). - BREAKING: Advertise `CompositeAlphaMode::PreMultiplied` instead of `PostMultiplied`, matching the premultiplied alpha compositing that Core Animation actually performs for a non-opaque `CAMetalLayer`. By @nicoburns in [#9922](https://github.com/gfx-rs/wgpu/pull/9922). - If you previously hard-coded `PostMultiplied` to get a transparent macOS window, you will start receiving `UnsupportedAlphaMode` validation errors for this. Those affected should migrate to `PreMultiplied` instead. - 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). diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index 04ba3fab4..6d4c70817 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -692,7 +692,7 @@ impl crate::Device for super::Device { .array_layer_count .unwrap_or(texture.array_layers - desc.range.base_array_layer); - autoreleasepool(|_| { + autoreleasepool(|_| -> Result<_, crate::DeviceError> { let level_range = NSRange { location: desc.range.base_mip_level as _, length: mip_level_count as _, @@ -701,7 +701,7 @@ impl crate::Device for super::Device { location: desc.range.base_array_layer as _, length: array_layer_count as _, }; - let raw = unsafe { + let raw_result = unsafe { if let Some(swizzle) = swizzle { texture .raw @@ -712,7 +712,6 @@ impl crate::Device for super::Device { slice_range, swizzle, ) - .unwrap() } else { texture .raw @@ -722,14 +721,50 @@ impl crate::Device for super::Device { level_range, slice_range, ) - .unwrap() } }; + let raw = raw_result.ok_or_else(|| { + // Metal refuses some views of memoryless textures. Ideally such cases + // would be rejected by `wpgu-core` validation, but at least until that + // is implemented, we log a verbose error message. + // Related: . + let storage_mode = texture.raw.storageMode(); + let memoryless = if storage_mode == MTLStorageMode::Memoryless { + "This may be because the texture is memoryless (has TRANSIENT_ATTACHMENT usage). " + } else { + "" + }; + log::error!( + "Error creating Metal texture view. {memoryless}\ + Texture: {:?}, {:?}, {}x{}x{}, {} mip level(s), \ + {} array layer(s), Metal usage {:?}, storage mode {:?}. \ + Requested view: {:?}. \ + Metal arguments: pixel format {:?}, texture type {:?}, \ + levels {}..{}, slices {}..{}.", + texture.format, + texture.raw_type, + texture.copy_size.width, + texture.copy_size.height, + texture.copy_size.depth, + texture.mip_levels, + texture.array_layers, + texture.raw.usage(), + storage_mode, + desc, + raw_format, + raw_type, + level_range.location, + level_range.location + level_range.length, + slice_range.location, + slice_range.location + slice_range.length, + ); + crate::DeviceError::Unexpected + })?; if let Some(label) = desc.label { raw.setLabel(Some(&NSString::from_str(label))); } - raw - }) + Ok(raw) + })? }; self.counters.texture_views.add(1);