Skip to content

Add 'Metal' feature for iOS/macOS uses CAMetalLayer on UIView/NSView #4283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v0.30.x
Choose a base branch
from

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/ .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants