From c7f3db8bf13824ccdd7e289ba788e86e6395ce03 Mon Sep 17 00:00:00 2001 From: Inner Daemons <85136135+inner-daemons@users.noreply.github.com> Date: Wed, 18 Feb 2026 18:55:20 -0600 Subject: [PATCH] Remove dispatch2 dependency (#9048) --- Cargo.lock | 4 -- Cargo.toml | 2 - wgpu-hal/Cargo.toml | 2 - wgpu-hal/src/metal/device.rs | 26 +++-------- wgpu-hal/src/metal/library_from_metallib.rs | 48 +++++++++++++++++++++ wgpu-hal/src/metal/mod.rs | 1 + 6 files changed, 55 insertions(+), 28 deletions(-) create mode 100644 wgpu-hal/src/metal/library_from_metallib.rs diff --git a/Cargo.lock b/Cargo.lock index 0bf34f7ae..d9a76417e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1178,8 +1178,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89a09f22a6c6069a18470eb92d2298acf25463f14256d24778e1230d789a2aec" dependencies = [ "bitflags 2.11.0", - "block2 0.6.2", - "libc", "objc2 0.6.3", ] @@ -2869,7 +2867,6 @@ checksum = "a0125f776a10d00af4152d74616409f0d4a2053a6f57fa5b7d6aa2854ac04794" dependencies = [ "bitflags 2.11.0", "block2 0.6.2", - "dispatch2", "objc2 0.6.3", "objc2-foundation 0.3.2", ] @@ -4940,7 +4937,6 @@ dependencies = [ "bytemuck", "cfg-if", "cfg_aliases 0.2.1", - "dispatch2", "env_logger", "glam", "glow", diff --git a/Cargo.toml b/Cargo.toml index 7a63764e1..88d5fc1c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -203,7 +203,6 @@ xshell = "0.2.2" # Metal dependencies block2 = "0.6.2" -dispatch2 = "0.3.0" objc2 = "0.6.3" objc2-core-foundation = { version = "0.3.2", default-features = false, features = [ "std", @@ -219,7 +218,6 @@ objc2-foundation = { version = "0.3.2", default-features = false, features = [ objc2-metal = { version = "0.3.2", default-features = false, features = [ "std", "block2", - "dispatch2", "MTLAllocation", "MTLBlitCommandEncoder", "MTLBlitPass", diff --git a/wgpu-hal/Cargo.toml b/wgpu-hal/Cargo.toml index c172d2929..507196661 100644 --- a/wgpu-hal/Cargo.toml +++ b/wgpu-hal/Cargo.toml @@ -75,7 +75,6 @@ metal = [ "naga/msl-out", "dep:arrayvec", "dep:block2", - "dep:dispatch2", "dep:bytemuck", "dep:hashbrown", "dep:libc", @@ -285,7 +284,6 @@ mach-dxcompiler-rs = { workspace = true, optional = true } [target.'cfg(target_vendor = "apple")'.dependencies] # Backend: Metal block2 = { workspace = true, optional = true } -dispatch2 = { workspace = true, optional = true } bytemuck = { workspace = true, optional = true } objc2 = { workspace = true, optional = true } objc2-core-foundation = { workspace = true, optional = true } diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index 4f87532d5..0df9da448 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -1,10 +1,5 @@ use alloc::{borrow::ToOwned as _, sync::Arc, vec::Vec}; -use core::{ - ffi::c_void, - ptr::{self, NonNull}, - sync::atomic, -}; -use dispatch2::DispatchData; +use core::{ptr::NonNull, sync::atomic}; use std::{thread, time}; use bytemuck::TransparentWrapper; @@ -1143,20 +1138,11 @@ impl crate::Device for super::Device { num_workgroups, } => { // SAFETY: this creates a reference to `file` that is dropped before `file` is dropped. - let data = unsafe { - DispatchData::new( - NonNull::new(file.as_ptr() as *mut c_void).unwrap(), - file.len(), - None, - ptr::null_mut(), - ) - }; - let library = self - .shared - .device - .newLibraryWithData_error(&data) - .map_err(|e| crate::ShaderError::Compilation(format!("Metallib: {e:?}")))?; - drop(data); + let library = super::library_from_metallib::new_library_from_metallib_bytes( + &self.shared.device, + file, + ) + .map_err(|e| crate::ShaderError::Compilation(format!("Metallib: {e:?}")))?; Ok(super::ShaderModule { source: ShaderModuleSource::Passthrough(PassthroughShader { library, diff --git a/wgpu-hal/src/metal/library_from_metallib.rs b/wgpu-hal/src/metal/library_from_metallib.rs new file mode 100644 index 000000000..23898474d --- /dev/null +++ b/wgpu-hal/src/metal/library_from_metallib.rs @@ -0,0 +1,48 @@ +use core::{ffi::c_void, ptr::NonNull}; + +use objc2::{ + msg_send, + rc::Retained, + runtime::{AnyObject, ProtocolObject}, +}; +use objc2_foundation::NSError; +use objc2_metal::{MTLDevice, MTLLibrary}; + +#[link(name = "System")] // libdispatch lives inside libSystem +extern "C" { + fn dispatch_data_create( + buffer: NonNull, + size: usize, + queue: Option>, // dispatch_queue_t _Nullable + destructor: *mut c_void, // dispatch_block_t _Nullable + ) -> *mut AnyObject; // dispatch_data_t + + fn dispatch_release(object: *mut AnyObject); // dispatch_data_t +} + +const DISPATCH_DATA_DESTRUCTOR_DEFAULT: *mut c_void = core::ptr::null_mut(); + +/// See . +/// +/// newLibraryWithData requires DispatchData, part of the Dispatch library. +/// Not only is this a big additional dependency for a glorified `Vec` +/// in this case, but the code in the dispatch2 rust crate is a WIP and +/// Mozilla has decided not to vendor it yet. +/// +/// This function allows us to deal with DispatchData without dealing +/// with these problems. +pub(crate) fn new_library_from_metallib_bytes( + device: &ProtocolObject, + data: &[u8], +) -> Result>, Retained> { + let buffer = NonNull::new(data.as_ptr().cast_mut()).unwrap().cast(); + let data = + unsafe { dispatch_data_create(buffer, data.len(), None, DISPATCH_DATA_DESTRUCTOR_DEFAULT) }; + assert!(!data.is_null()); + + let res: Result>, Retained> = + unsafe { msg_send![device, newLibraryWithData: data, error: _] }; + + unsafe { dispatch_release(data) }; + res +} diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index d55e070b5..d4412aeaf 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -22,6 +22,7 @@ mod adapter; mod command; mod conv; mod device; +mod library_from_metallib; mod surface; mod time;