From fd253ce79def38a2c1775ca0080ea7f3e0ee6f05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Rouleau?= Date: Sat, 13 Jun 2026 00:16:13 -0400 Subject: [PATCH] metal: render-target color textures must use the view's pixel format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `new_pipeline` builds each pipeline's color attachment with the view's `colorPixelFormat`, so Metal pipelines effectively bake in that format at creation time. Color render textures created via `new_render_texture` were following the requested `TextureFormat` instead (e.g. `RGBA8` → `RGBA8Unorm`). The instant such a target was bound to the pipeline, Metal validation tripped: For color attachment 0, the render pipeline's pixelFormat (MTLPixelFormatBGRA8Unorm) does not match the framebuffer's pixelFormat (MTLPixelFormatRGBA8Unorm). MTKView's allowed presentable formats are all `BGRA*` (or `RGBA16Float`) — `RGBA8Unorm` isn't among them — so honoring `TextureFormat::RGBA8` literally for color render targets silently breaks every offscreen color attachment. Use the view's actual `colorPixelFormat` for those textures so they round-trip through the pipeline without a format mismatch. Sampling them in a shader is unaffected: Metal's hardware swizzle returns RGBA semantics regardless of memory order. --- src/graphics/metal.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/graphics/metal.rs b/src/graphics/metal.rs index 39004579c..70341d92f 100644 --- a/src/graphics/metal.rs +++ b/src/graphics/metal.rs @@ -711,8 +711,14 @@ impl RenderingBackend for MetalContext { if access == TextureAccess::RenderTarget { if params.format != TextureFormat::Depth { - let pixel_format: MTLPixelFormat = params.format.into(); - msg_send_![descriptor, setPixelFormat: pixel_format]; + // Match the view's `colorPixelFormat` — pipelines + // bake it and reject mismatched attachments, and + // MTKView's presentable formats are all `BGRA*` / + // `RGBA16Float` so honoring `RGBA8` literally + // would break every offscreen color target. + let view_pixel_format: MTLPixelFormat = + msg_send![self.view, colorPixelFormat]; + msg_send_![descriptor, setPixelFormat: view_pixel_format]; } msg_send_![descriptor, setStorageMode: MTLStorageMode::Private]; msg_send_![