Skip to content
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

[metal] Use raw-window-metal to do layer creation #6210

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 80 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ block = "0.1"
core-graphics-types = "0.1"
metal = { version = "0.29.0" }
objc = "0.2.5"
raw-window-metal = "1.0"

# Vulkan dependencies
android_system_properties = "0.1.1"
Expand Down
2 changes: 0 additions & 2 deletions examples/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,6 @@ async fn start<E: Example>(title: &str) {
&context.device,
&context.queue,
);

window_loop.window.request_redraw();
}
WindowEvent::KeyboardInput {
event:
Expand Down
3 changes: 0 additions & 3 deletions examples/src/hello_triangle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
.unwrap();
surface.configure(&device, &config);

let window = &window;
event_loop
.run(move |event, target| {
// Have the closure take ownership of the resources.
Expand All @@ -100,8 +99,6 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
config.width = new_size.width.max(1);
config.height = new_size.height.max(1);
surface.configure(&device, &config);
// On macos the window needs to be redrawn manually after resizing
window.request_redraw();
}
WindowEvent::RedrawRequested => {
let frame = surface
Expand Down
2 changes: 0 additions & 2 deletions examples/src/hello_windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ async fn run(event_loop: EventLoop<()>, viewports: Vec<(Arc<Window>, wgpu::Color
// Recreate the swap chain with the new size
if let Some(viewport) = viewports.get_mut(&window_id) {
viewport.resize(&device, new_size);
// On macos the window needs to be redrawn manually after resizing
viewport.desc.window.request_redraw();
}
}
WindowEvent::RedrawRequested => {
Expand Down
2 changes: 0 additions & 2 deletions examples/src/uniform_values/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ impl WgpuContext {
self.surface_config.width = new_size.width;
self.surface_config.height = new_size.height;
self.surface.configure(&self.device, &self.surface_config);
self.window.request_redraw();
}
}

Expand Down Expand Up @@ -285,7 +284,6 @@ async fn run(event_loop: EventLoop<()>, window: Arc<Window>) {
WindowEvent::Resized(new_size) => {
let wgpu_context_mut = wgpu_context.as_mut().unwrap();
wgpu_context_mut.resize(new_size);
wgpu_context_mut.window.request_redraw();
}
WindowEvent::RedrawRequested => {
let wgpu_context_ref = wgpu_context.as_ref().unwrap();
Expand Down
16 changes: 13 additions & 3 deletions wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ metal = [
# Metal is only available on Apple platforms, therefore request MSL output also only if we target an Apple platform.
"naga/msl-out-if-target-apple",
"dep:block",
"dep:core-graphics-types",
"dep:metal",
"dep:objc",
"dep:raw-window-metal",
]
vulkan = [
"naga/spv-out",
"dep:ash",
"dep:gpu-alloc",
"dep:gpu-descriptor",
"dep:libloading",
"dep:raw-window-metal",
"dep:smallvec",
"dep:android_system_properties",
]
Expand All @@ -61,6 +66,7 @@ gles = [
"dep:khronos-egl",
"dep:libloading",
"dep:ndk-sys",
"dep:objc",
"windows/Win32_Graphics_OpenGL",
"windows/Win32_Graphics_Gdi",
"windows/Win32_System_LibraryLoader",
Expand Down Expand Up @@ -165,10 +171,14 @@ glutin_wgl_sys = { workspace = true, optional = true }
[target.'cfg(any(target_os="macos", target_os="ios"))'.dependencies]
# backend: Metal
block = { workspace = true, optional = true }
metal = { workspace = true, optional = true }
core-graphics-types = { workspace = true, optional = true }

metal.workspace = true
objc.workspace = true
core-graphics-types.workspace = true
# backend: Metal + GLES
objc = { workspace = true, optional = true }

# backend: Metal + Vulkan
raw-window-metal = { workspace = true, optional = true }

[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen.workspace = true
Expand Down
34 changes: 20 additions & 14 deletions wgpu-hal/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use std::{

use arrayvec::ArrayVec;
use bitflags::bitflags;
use metal::foreign_types::ForeignTypeRef as _;
use metal::foreign_types::{ForeignType as _, ForeignTypeRef as _};
use parking_lot::{Mutex, RwLock};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -100,7 +100,7 @@ pub struct Instance {}

impl Instance {
pub fn create_surface_from_layer(&self, layer: &metal::MetalLayerRef) -> Surface {
unsafe { Surface::from_layer(layer) }
Surface::from_layer(layer)
}
}

Expand All @@ -119,19 +119,25 @@ impl crate::Instance for Instance {
_display_handle: raw_window_handle::RawDisplayHandle,
window_handle: raw_window_handle::RawWindowHandle,
) -> Result<Surface, crate::InstanceError> {
match window_handle {
#[cfg(target_os = "ios")]
raw_window_handle::RawWindowHandle::UiKit(handle) => {
Ok(unsafe { Surface::from_view(handle.ui_view.cast()) })
let layer = match window_handle {
raw_window_handle::RawWindowHandle::AppKit(handle) => unsafe {
raw_window_metal::Layer::from_ns_view(handle.ns_view)
},
raw_window_handle::RawWindowHandle::UiKit(handle) => unsafe {
raw_window_metal::Layer::from_ui_view(handle.ui_view)
},
_ => {
return Err(crate::InstanceError::new(format!(
"window handle {window_handle:?} is not a Metal-compatible handle"
)))
}
#[cfg(target_os = "macos")]
raw_window_handle::RawWindowHandle::AppKit(handle) => {
Ok(unsafe { Surface::from_view(handle.ns_view.cast()) })
}
_ => Err(crate::InstanceError::new(format!(
"window handle {window_handle:?} is not a Metal-compatible handle"
))),
}
};

// SAFETY: The layer is an initialized instance of `CAMetalLayer`, and
// we transfer the retain count to `MetalLayer` using `into_raw`.
let layer = unsafe { metal::MetalLayer::from_ptr(layer.into_raw().cast().as_ptr()) };

Ok(Surface::new(layer))
}

unsafe fn enumerate_adapters(
Expand Down
Loading
Loading