gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
12.72k stars 933 forks source link

Support Device fence sharing with metal on macOS #6575

Open sotaroikeda opened 1 day ago

sotaroikeda commented 1 day ago

Is your feature request related to a problem? Please describe.

It is necessary to add a capability of sharing Device fence synchronization between gecko. See Bug 1910520. Metal provides MTLSharedEvent to do it. MTLSharedEvent is supported on macOS 10.14+ and iOS 12.0+.

Similar capability is already added on Windows by #4872. It uses ID3D12Fence for synchronization.

Describe the solution you'd like

MTLSharedEvents can be got from the Device fence like

    let shared_event = unsafe {
        global.device_fence_as_hal::<wgc::api::Metal, _, Option<metal::SharedEvent>>(
            device_id,
            |hal_fence| hal_fence.map(|fence| fence.raw_shared_event().clone()),
        )
    };

MTLSharedEvent could be added to Fence like.

 pub struct Fence {
     completed_value: Arc<atomic::AtomicU64>,
     /// The pending fence values have to be ascending.
     pending_command_buffers: Vec<(crate::FenceValue, metal::CommandBuffer)>,
+    shared_event: Option<metal::SharedEvent>,
 }

MTLSharedEvent could be created in Fence like

     unsafe fn create_fence(&self) -> DeviceResult<super::Fence> {
         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: shared_event,
         })
     }

Describe alternatives you've considered

Necessary api to wgpu-core is already added by #4872. Modifying wgpu-hal metal source could address the problem.

Additional context

Device fence sharing with metal on macOS in gecko is tracked in Bug 1910520.