diff --git a/wgpu-bindings/src/main.rs b/wgpu-bindings/src/main.rs index a1d14669b..42a5d402f 100644 --- a/wgpu-bindings/src/main.rs +++ b/wgpu-bindings/src/main.rs @@ -2,12 +2,20 @@ extern crate cbindgen; use std::path::PathBuf; +const HEADER: &str = " +#ifdef WGPU_REMOTE + typedef uint32_t WGPUId; +#else + typedef void *WGPUId; +#endif +"; + fn main() { let mut crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); crate_dir.push("../wgpu-native"); let config = cbindgen::Config { - header: Some(String::from("#ifdef WGPU_REMOTE\n typedef uint32_t WGPUId;\n#else\n typedef void *WGPUId;\n#endif")), + header: Some(String::from(HEADER.trim())), enumeration: cbindgen::EnumConfig { prefix_with_name: true, ..Default::default() @@ -15,7 +23,7 @@ fn main() { export: cbindgen::ExportConfig { prefix: Some(String::from("WGPU")), exclude: vec![ - // We manually define `Id` is with an `#ifdef`, so exclude it here + // We manually define `Id` is within the header, so exclude it here String::from("Id"), ], ..Default::default() diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index c11acbfb4..8579d1dff 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -36,15 +36,11 @@ pub extern "C" fn wgpu_device_create_shader_module( device_id: DeviceId, desc: pipeline::ShaderModuleDescriptor, ) -> ShaderModuleId { - let device_registry = registry::DEVICE_REGISTRY.lock().unwrap(); - let device = device_registry.get(device_id).unwrap(); + let device = registry::DEVICE_REGISTRY.get(device_id).unwrap(); let shader = device .device .create_shader_module(unsafe { ::std::slice::from_raw_parts(desc.code.bytes, desc.code.length) }).unwrap(); - registry::SHADER_MODULE_REGISTRY - .lock() - .unwrap() - .register(ShaderModule { raw: shader }) + registry::SHADER_MODULE_REGISTRY.register(ShaderModule { raw: shader }) } diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 3308defcd..c3832f884 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -33,9 +33,8 @@ pub extern "C" fn wgpu_create_instance() -> InstanceId { feature = "gfx-backend-metal" ))] { - let mut registry = registry::INSTANCE_REGISTRY.lock().unwrap(); let inst = ::back::Instance::create("wgpu", 1); - registry.register(inst) + registry::INSTANCE_REGISTRY.register(inst) } #[cfg(not(any( feature = "gfx-backend-vulkan", @@ -52,8 +51,7 @@ pub extern "C" fn wgpu_instance_get_adapter( instance_id: InstanceId, desc: AdapterDescriptor, ) -> AdapterId { - let instance_registry = registry::INSTANCE_REGISTRY.lock().unwrap(); - let instance = instance_registry.get(instance_id).unwrap(); + let instance = registry::INSTANCE_REGISTRY.get(instance_id).unwrap(); let (mut low, mut high, mut other) = (None, None, None); for adapter in instance.enumerate_adapters() { match adapter.info.device_type { @@ -67,10 +65,7 @@ pub extern "C" fn wgpu_instance_get_adapter( PowerPreference::LowPower => low.or(high), PowerPreference::HighPerformance | PowerPreference::Default => high.or(low), }; - registry::ADAPTER_REGISTRY - .lock() - .unwrap() - .register(some.or(other).unwrap()) + registry::ADAPTER_REGISTRY.register(some.or(other).unwrap()) } #[no_mangle] @@ -78,12 +73,8 @@ pub extern "C" fn wgpu_adapter_create_device( adapter_id: AdapterId, desc: DeviceDescriptor, ) -> DeviceId { - let mut adapter_registry = registry::ADAPTER_REGISTRY.lock().unwrap(); - let adapter = adapter_registry.get_mut(adapter_id).unwrap(); + let adapter = registry::ADAPTER_REGISTRY.get_mut(adapter_id).unwrap(); let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap(); let mem_props = adapter.physical_device.memory_properties(); - registry::DEVICE_REGISTRY - .lock() - .unwrap() - .register(Device::new(device, queue_group, mem_props)) + registry::DEVICE_REGISTRY.register(Device::new(device, queue_group, mem_props)) } diff --git a/wgpu-native/src/registry.rs b/wgpu-native/src/registry.rs index 8d482c246..f67ec3293 100644 --- a/wgpu-native/src/registry.rs +++ b/wgpu-native/src/registry.rs @@ -13,9 +13,9 @@ pub(crate) type Id = u32; pub(crate) trait Registry { fn new() -> Self; - fn register(&mut self, handle: T) -> Id; + fn register(&self, handle: T) -> Id; fn get(&self, id: Id) -> Option<&T>; - fn get_mut(&mut self, id: Id) -> Option<&mut T>; + fn get_mut(&self, id: Id) -> Option<&mut T>; } #[cfg(not(feature = "remote"))] @@ -31,7 +31,7 @@ impl Registry for LocalRegistry { } } - fn register(&mut self, handle: T) -> Id { + fn register(&self, handle: T) -> Id { ::std::boxed::Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void } @@ -39,62 +39,71 @@ impl Registry for LocalRegistry { unsafe { (id as *const T).as_ref() } } - fn get_mut(&mut self, id: Id) -> Option<&mut T> { + fn get_mut(&self, id: Id) -> Option<&mut T> { unsafe { (id as *mut T).as_mut() } } } #[cfg(feature = "remote")] -pub(crate) struct RemoteRegistry { +struct Registrations { next_id: Id, tracked: FastHashMap, } +#[cfg(feature = "remote")] +impl Registrations { + fn new() -> Self { + Registrations { + next_id: 0, + tracked: FastHashMap::default(), + } + } +} + +#[cfg(feature = "remote")] +pub(crate) struct RemoteRegistry { + registrations: Arc>>, +} + #[cfg(feature = "remote")] impl Registry for RemoteRegistry { fn new() -> Self { RemoteRegistry { - next_id: 0, - tracked: FastHashMap::default(), + registrations: Arc::new(Mutex::new(Registrations::new())), } } - fn register(&mut self, handle: T) -> Id { - let id = self.next_id; - self.tracked.insert(id, handle); - self.next_id += 1; + fn register(&self, handle: T) -> Id { + let mut registrations = self.registrations.lock().unwrap(); + let id = registrations.next_id; + registrations.tracked.insert(id, handle); + registrations.next_id += 1; id } fn get(&self, id: Id) -> Option<&T> { - self.tracked.get(&id) + let registrations = self.registrations.lock().unwrap(); + registrations.tracked.get(&id) } - fn get_mut(&mut self, id: Id) -> Option<&mut T> { - self.tracked.get_mut(&id) + fn get_mut(&self, id: Id) -> Option<&mut T> { + let registrations = self.registrations.lock().unwrap(); + registrations.tracked.get_mut(&id) } } #[cfg(not(feature = "remote"))] lazy_static! { - pub(crate) static ref ADAPTER_REGISTRY: Mutex> = - Mutex::new(LocalRegistry::new()); - pub(crate) static ref DEVICE_REGISTRY: Mutex> = - Mutex::new(LocalRegistry::new()); - pub(crate) static ref INSTANCE_REGISTRY: Mutex> = - Mutex::new(LocalRegistry::new()); - pub(crate) static ref SHADER_MODULE_REGISTRY: Mutex> = - Mutex::new(LocalRegistry::new()); + pub(crate) static ref ADAPTER_REGISTRY: LocalRegistry = LocalRegistry::new(); + pub(crate) static ref DEVICE_REGISTRY: LocalRegistry = LocalRegistry::new(); + pub(crate) static ref INSTANCE_REGISTRY: LocalRegistry = LocalRegistry::new(); + pub(crate) static ref SHADER_MODULE_REGISTRY: LocalRegistry = LocalRegistry::new(); } #[cfg(feature = "remote")] lazy_static! { - pub(crate) static ref ADAPTER_REGISTRY: Arc>> = - Arc::new(Mutex::new(RemoteRegistry::new())); - pub(crate) static ref DEVICE_REGISTRY: Arc>> = - Arc::new(Mutex::new(RemoteRegistry::new())); - pub(crate) static ref INSTANCE_REGISTRY: Arc>> = - Arc::new(Mutex::new(RemoteRegistry::new())); - pub(crate) static ref SHADER_MODULE_REGISTRY: Arc>> = - Arc::new(Mutex::new(RemoteRegistry::new())); + pub(crate) static ref ADAPTER_REGISTRY: RemoteRegistry = RemoteRegistry::new(); + pub(crate) static ref DEVICE_REGISTRY: RemoteRegistry = RemoteRegistry::new(); + pub(crate) static ref INSTANCE_REGISTRY: RemoteRegistry = RemoteRegistry::new(); + pub(crate) static ref SHADER_MODULE_REGISTRY: RemoteRegistry = RemoteRegistry::new(); }