From 2d8823b87c8488b5b66df0b6ba351df27911fd16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoi=CC=82t=20Rouleau?= Date: Fri, 12 Jun 2026 23:38:53 -0400 Subject: [PATCH] metal: assert on `texture_generate_mipmaps` for non-mipmapped textures Calling `texture_generate_mipmaps` on a texture allocated without mipmaps is a programmer error 99% of the time. Today the Metal implementation unconditionally calls `generateMipmapsForTexture:`, which Metal validation rejects when the texture has `mipmapLevelCount == 1`: -[MTLDebugBlitCommandEncoder generateMipmapsForTexture:]:1105: failed assertion `Generate Mipmaps For Texture Validation [tex mipmapLevelCount](1) must be > 1.` Raise the failure at the call site with a clear panic message instead of crashing deep inside the blit encoder. The fix is on the caller: pass `TextureParams::allocate_mipmaps = true` at creation if you intend to generate mipmaps later. --- src/graphics/metal.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/graphics/metal.rs b/src/graphics/metal.rs index 39004579..6b26cd9f 100644 --- a/src/graphics/metal.rs +++ b/src/graphics/metal.rs @@ -498,13 +498,21 @@ impl RenderingBackend for MetalContext { unimplemented!() } fn texture_generate_mipmaps(&mut self, texture: TextureId) { + let texture = self.textures.get(texture); + // Catch the bug at the call site instead of letting Metal + // assert on `mipmapLevelCount > 1` deep inside the blit + // encoder. Set `TextureParams::allocate_mipmaps = true` at + // creation to use this entry point. + assert!( + texture.params.allocate_mipmaps, + "texture_generate_mipmaps called on a texture allocated without mipmaps", + ); unsafe { if self.command_buffer.is_none() { self.command_buffer = Some(msg_send![self.command_queue, commandBuffer]); } let command_buffer = self.command_buffer.unwrap(); let encoder = msg_send_![command_buffer, blitCommandEncoder]; - let texture = self.textures.get(texture); msg_send_![encoder, generateMipmapsForTexture: texture.texture]; msg_send_![encoder, endEncoding]; }