From 4bec0ef46d11a9e8da39d9b8faa5138a234ef4b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoi=CC=82t=20Rouleau?= Date: Mon, 5 Oct 2026 12:05:18 -0400 Subject: [PATCH] metal: implement texture_read_pixels The Metal backend left `texture_read_pixels` `unimplemented!()`, so nothing drawn through Metal could be read back to the CPU. Render targets live in private storage, so the texture is blitted into a shared buffer and copied out of it. The blit joins the frame's command buffer, so it runs after whatever the frame already drew into the texture; that command buffer is committed and waited on, and a fresh one carries the rest of the frame. Render targets take the view's BGRA pixel format, so their pixels are swapped to the RGBA order `TextureFormat::RGBA8` describes, which is what OpenGL returns. As on OpenGL, rows come back in the order `texture_update` takes them. A render pass draws the top of its viewport into a texture's first row on Metal and into its last row on OpenGL, so a caller that reads back a render target flips the rows on one of the two. Verified on macOS: a texture created from bytes reads back the same bytes, and a render target cleared to red reads back as red in RGBA order in the middle of a frame, which then carries on and presents. Co-Authored-By: Claude Opus 5.5 --- src/graphics/metal.rs | 65 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/src/graphics/metal.rs b/src/graphics/metal.rs index e18ec9f75..5fa68282b 100644 --- a/src/graphics/metal.rs +++ b/src/graphics/metal.rs @@ -545,8 +545,69 @@ impl RenderingBackend for MetalContext { ) { unimplemented!() } - fn texture_read_pixels(&mut self, _texture: TextureId, _bytes: &mut [u8]) { - unimplemented!() + fn texture_read_pixels(&mut self, texture: TextureId, bytes: &mut [u8]) { + let texture = self.textures.get(texture); + let TextureParams { + width, + height, + format, + sample_count, + .. + } = texture.params; + assert!( + sample_count <= 1, + "texture_read_pixels not yet implemented for multisampled textures" + ); + let bytes_per_row = format.size(width, 1) as u64; + let length = bytes_per_row * height as u64; + let origin = MTLOrigin { x: 0, y: 0, z: 0 }; + let size = MTLSize { + width: width as u64, + height: height as u64, + depth: 1, + }; + // Render targets live in private storage, out of the CPU's reach, + // so the pixels go through a shared buffer. The blit has to run + // after whatever this frame already drew into the texture, so it + // joins the frame's command buffer, which is committed early and + // replaced for the rest of the frame. + self.really_end_encoder(); + unsafe { + let buffer: ObjcId = msg_send![self.device, + newBufferWithLength:length + options:MTLResourceOptions::StorageModeShared]; + let command_buffer = self + .command_buffer + .unwrap_or_else(|| msg_send![self.command_queue, commandBuffer]); + let encoder = msg_send_![command_buffer, blitCommandEncoder]; + msg_send_![encoder, + copyFromTexture:texture.texture + sourceSlice:0u64 + sourceLevel:0u64 + sourceOrigin:origin + sourceSize:size + toBuffer:buffer + destinationOffset:0u64 + destinationBytesPerRow:bytes_per_row + destinationBytesPerImage:length]; + msg_send_![encoder, endEncoding]; + msg_send_![command_buffer, commit]; + msg_send_![command_buffer, waitUntilCompleted]; + self.command_buffer = Some(msg_send![self.command_queue, commandBuffer]); + + let contents: *const u8 = msg_send![buffer, contents]; + let pixels = &mut bytes[..length as usize]; + std::ptr::copy_nonoverlapping(contents, pixels.as_mut_ptr(), pixels.len()); + msg_send_![buffer, release]; + + // Render targets take the view's pixel format, which is BGRA. + let pixel_format: MTLPixelFormat = msg_send![texture.texture, pixelFormat]; + if pixel_format == MTLPixelFormat::BGRA8Unorm { + for pixel in pixels.chunks_exact_mut(4) { + pixel.swap(0, 2); + } + } + } } fn texture_generate_mipmaps(&mut self, texture: TextureId) { let texture = self.textures.get(texture);