From 9ffafcf7b816325296160a5c1131e2bece1460d0 Mon Sep 17 00:00:00 2001 From: sotaroikeda Date: Wed, 27 Nov 2024 04:25:07 +0900 Subject: [PATCH] Add Metal SharedEvent to fence if it is supported (#6610) Co-authored-by: Sotaro Ikeda Co-authored-by: Erich Gubler --- wgpu-hal/src/metal/adapter.rs | 1 + wgpu-hal/src/metal/device.rs | 6 ++++++ wgpu-hal/src/metal/mod.rs | 10 ++++++++++ 3 files changed, 17 insertions(+) diff --git a/wgpu-hal/src/metal/adapter.rs b/wgpu-hal/src/metal/adapter.rs index fa7622a37..426571b24 100644 --- a/wgpu-hal/src/metal/adapter.rs +++ b/wgpu-hal/src/metal/adapter.rs @@ -828,6 +828,7 @@ impl super::PrivateCapabilities { && ((device.supports_family(MTLGPUFamily::Apple8) && device.supports_family(MTLGPUFamily::Mac2)) || device.supports_family(MTLGPUFamily::Apple9)), + supports_shared_event: version.at_least((10, 14), (12, 0), os_is_mac), } } diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index 745ad598a..9485559f1 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -1326,9 +1326,15 @@ impl crate::Device for super::Device { unsafe fn create_fence(&self) -> DeviceResult { self.counters.fences.add(1); + let shared_event = if self.shared.private_caps.supports_shared_event { + Some(self.shared.device.lock().new_shared_event()) + } else { + None + }; Ok(super::Fence { completed_value: Arc::new(atomic::AtomicU64::new(0)), pending_command_buffers: Vec::new(), + shared_event, }) } diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index 1935e843e..b6d7c6b9e 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -289,6 +289,7 @@ struct PrivateCapabilities { supports_simd_scoped_operations: bool, int64: bool, int64_atomics: bool, + supports_shared_event: bool, } #[derive(Clone, Debug)] @@ -428,6 +429,10 @@ impl crate::Queue for Queue { signal_fence .pending_command_buffers .push((signal_value, raw.to_owned())); + + if let Some(shared_event) = signal_fence.shared_event.as_ref() { + raw.encode_signal_event(shared_event, signal_value); + } // only return an extra one if it's extra match command_buffers.last() { Some(_) => None, @@ -818,6 +823,7 @@ pub struct Fence { completed_value: Arc, /// The pending fence values have to be ascending. pending_command_buffers: Vec<(crate::FenceValue, metal::CommandBuffer)>, + shared_event: Option, } impl crate::DynFence for Fence {} @@ -841,6 +847,10 @@ impl Fence { self.pending_command_buffers .retain(|&(value, _)| value > latest); } + + pub fn raw_shared_event(&self) -> Option<&metal::SharedEvent> { + self.shared_event.as_ref() + } } struct IndexState {