Skip to content

Conversation

@rebecca-src
Copy link

Hello winit Team 😃,

thank you for this awesome repository 👍

I was experimenting with macOS/iOS and skia-safe with GPU Backend Metal. It was very difficult to get the Metal to work, especially on iOS because I needed the gl_or_metal_backed line 525 in platform_impl/ios/window.rs to be true, otherwise the render with window.request_redraw() will not work. With the metal feature enabeld the UIView/NSView will automatically create a CAMetalLayer as its layer.

The MetalLayer can then be extracted as followed:

use raw_window_handle::RawWindowHandle;

impl ApplicationHandler for Application {

    /* Cargo.toml
    winit = { version = "0.30.11", features = ["metal"] }
    metal = "0.32"
    objc = "0.2"
    raw-window-handle = "0.6"
    */

    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        let window = event_loop.create_window(WindowAttributes::default()).expect("Failed to create window");
        let window_handle = window.window_handle().expect("Failed to retrieve a window handle");
        let view = match window_handle.as_raw() {
            RawWindowHandle::AppKit(appkit) => appkit.ns_view.as_ptr(),
            RawWindowHandle::UiKit(uikit) => uikit.ui_view.as_ptr(),
            _ => panic!("Wrong window handle type")
        } as *mut objc::runtime::Object;

        let metal_layer = unsafe {
            let metal_layer: *mut metal::CAMetalLayer = objc::msg_send![view, layer];
            let ml = metal::MetalLayer::from_ptr(metal_layer);
            ml
        }
    }
}

…ith CAMetalLayer and sets gl_or_metal_backed (iOS) true
@rebecca-src rebecca-src requested a review from madsmtm as a code owner June 14, 2025 15:48
@tronical
Copy link
Contributor

I think that perhaps you might be looking for this crate: https://docs.rs/raw-window-metal/1.1.0/raw_window_metal/ .

@madsmtm madsmtm added S - enhancement Wouldn't this be the coolest? DS - uikit Affects the UIKit backend (iOS, tvOS, watchOS, visionOS) labels Sep 4, 2025
@madsmtm madsmtm self-assigned this Sep 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

DS - uikit Affects the UIKit backend (iOS, tvOS, watchOS, visionOS) S - enhancement Wouldn't this be the coolest?

Development

Successfully merging this pull request may close these issues.

4 participants