From d814851350d6b99e381ce01cd51ddab6dcbe69c5 Mon Sep 17 00:00:00 2001 From: lylythechosenone Date: Sat, 6 Apr 2024 03:10:24 -0400 Subject: [PATCH] [wgpu-core] pass resources as Arcs when adding them to the registry (#5499) * [wgpu-core] pass resources as Arcs when adding them to the registry (fix gfx-rs#5493) * [wgpu-core] also add `Arc::new` to `#[cfg(dx12)]` blocks * [wgpu-core] allow `clippy::arc_with_non_send_sync` --- wgpu-core/src/device/global.rs | 32 ++++++++++++++++---------------- wgpu-core/src/device/queue.rs | 2 +- wgpu-core/src/instance.rs | 34 +++++++++++++++++++--------------- wgpu-core/src/present.rs | 4 ++-- wgpu-core/src/registry.rs | 12 ++++++++++-- 5 files changed, 48 insertions(+), 36 deletions(-) diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index 891a62ad2..7f682f555 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -257,7 +257,7 @@ impl Global { hal::BufferUses::COPY_DST }; - let (id, resource) = fid.assign(buffer); + let (id, resource) = fid.assign(Arc::new(buffer)); api_log!("Device::create_buffer({desc:?}) -> {id:?}"); device @@ -572,7 +572,7 @@ impl Global { Err(error) => break error, }; - let (id, resource) = fid.assign(texture); + let (id, resource) = fid.assign(Arc::new(texture)); api_log!("Device::create_texture({desc:?}) -> {id:?}"); device @@ -646,7 +646,7 @@ impl Global { texture.initialization_status = RwLock::new(TextureInitTracker::new(desc.mip_level_count, 0)); - let (id, resource) = fid.assign(texture); + let (id, resource) = fid.assign(Arc::new(texture)); api_log!("Device::create_texture({desc:?}) -> {id:?}"); device @@ -699,7 +699,7 @@ impl Global { let buffer = device.create_buffer_from_hal(hal_buffer, desc); - let (id, buffer) = fid.assign(buffer); + let (id, buffer) = fid.assign(Arc::new(buffer)); api_log!("Device::create_buffer -> {id:?}"); device @@ -818,7 +818,7 @@ impl Global { Err(e) => break e, }; - let (id, resource) = fid.assign(view); + let (id, resource) = fid.assign(Arc::new(view)); { let mut views = texture.views.lock(); @@ -900,7 +900,7 @@ impl Global { Err(e) => break e, }; - let (id, resource) = fid.assign(sampler); + let (id, resource) = fid.assign(Arc::new(sampler)); api_log!("Device::create_sampler -> {id:?}"); device.trackers.lock().samplers.insert_single(resource); @@ -982,7 +982,7 @@ impl Global { let bgl = device.create_bind_group_layout(&desc.label, entry_map, bgl::Origin::Pool)?; - let (id_inner, arc) = fid.take().unwrap().assign(bgl); + let (id_inner, arc) = fid.take().unwrap().assign(Arc::new(bgl)); id = Some(id_inner); Ok(arc) @@ -1063,7 +1063,7 @@ impl Global { Err(e) => break e, }; - let (id, _) = fid.assign(layout); + let (id, _) = fid.assign(Arc::new(layout)); api_log!("Device::create_pipeline_layout -> {id:?}"); return (id, None); }; @@ -1130,7 +1130,7 @@ impl Global { Err(e) => break e, }; - let (id, resource) = fid.assign(bind_group); + let (id, resource) = fid.assign(Arc::new(bind_group)); let weak_ref = Arc::downgrade(&resource); for range in &resource.used_texture_ranges { @@ -1245,7 +1245,7 @@ impl Global { Err(e) => break e, }; - let (id, _) = fid.assign(shader); + let (id, _) = fid.assign(Arc::new(shader)); api_log!("Device::create_shader_module -> {id:?}"); return (id, None); }; @@ -1302,7 +1302,7 @@ impl Global { Ok(shader) => shader, Err(e) => break e, }; - let (id, _) = fid.assign(shader); + let (id, _) = fid.assign(Arc::new(shader)); api_log!("Device::create_shader_module_spirv -> {id:?}"); return (id, None); }; @@ -1367,7 +1367,7 @@ impl Global { .map(|s| s.to_string()), ); - let (id, _) = fid.assign(command_buffer); + let (id, _) = fid.assign(Arc::new(command_buffer)); api_log!("Device::create_command_encoder -> {id:?}"); return (id.transmute(), None); }; @@ -1460,7 +1460,7 @@ impl Global { Err(e) => break e, }; - let (id, resource) = fid.assign(render_bundle); + let (id, resource) = fid.assign(Arc::new(render_bundle)); api_log!("RenderBundleEncoder::finish -> {id:?}"); device.trackers.lock().bundles.insert_single(resource); return (id, None); @@ -1523,7 +1523,7 @@ impl Global { Err(err) => break err, }; - let (id, resource) = fid.assign(query_set); + let (id, resource) = fid.assign(Arc::new(query_set)); api_log!("Device::create_query_set -> {id:?}"); device.trackers.lock().query_sets.insert_single(resource); @@ -1601,7 +1601,7 @@ impl Global { Err(e) => break e, }; - let (id, resource) = fid.assign(pipeline); + let (id, resource) = fid.assign(Arc::new(pipeline)); api_log!("Device::create_render_pipeline -> {id:?}"); device @@ -1734,7 +1734,7 @@ impl Global { Err(e) => break e, }; - let (id, resource) = fid.assign(pipeline); + let (id, resource) = fid.assign(Arc::new(pipeline)); api_log!("Device::create_compute_pipeline -> {id:?}"); device diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 3cb5f695a..26d6c8b51 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -490,7 +490,7 @@ impl Global { prepare_staging_buffer(device, buffer_size.get(), device.instance_flags)?; let fid = hub.staging_buffers.prepare(id_in); - let (id, _) = fid.assign(staging_buffer); + let (id, _) = fid.assign(Arc::new(staging_buffer)); resource_log!("Queue::create_staging_buffer {id:?}"); Ok((id, staging_buffer_ptr)) diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index b909245fa..55c7b581e 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -525,7 +525,8 @@ impl Global { raw: hal_surface, }; - let (id, _) = self.surfaces.prepare(id_in).assign(surface); + #[allow(clippy::arc_with_non_send_sync)] + let (id, _) = self.surfaces.prepare(id_in).assign(Arc::new(surface)); Ok(id) } @@ -558,7 +559,7 @@ impl Global { }, }; - let (id, _) = self.surfaces.prepare(id_in).assign(surface); + let (id, _) = self.surfaces.prepare(id_in).assign(Arc::new(surface)); id } @@ -587,7 +588,7 @@ impl Global { }, }; - let (id, _) = self.surfaces.prepare(id_in).assign(surface); + let (id, _) = self.surfaces.prepare(id_in).assign(Arc::new(surface)); id } @@ -616,7 +617,7 @@ impl Global { }, }; - let (id, _) = self.surfaces.prepare(id_in).assign(surface); + let (id, _) = self.surfaces.prepare(id_in).assign(Arc::new(surface)); id } @@ -647,7 +648,7 @@ impl Global { }, }; - let (id, _) = self.surfaces.prepare(id_in).assign(surface); + let (id, _) = self.surfaces.prepare(id_in).assign(Arc::new(surface)); id } @@ -707,7 +708,7 @@ impl Global { for raw in hal_adapters { let adapter = Adapter::new(raw); log::info!("Adapter {:?} {:?}", A::VARIANT, adapter.raw.info); - let (id, _) = hub.adapters.prepare(id_backend).assign(adapter); + let (id, _) = hub.adapters.prepare(id_backend).assign(Arc::new(adapter)); list.push(id); } } @@ -754,7 +755,10 @@ impl Global { None => { let adapter = Adapter::new(list.swap_remove(*selected)); log::info!("Adapter {:?} {:?}", A::VARIANT, adapter.raw.info); - let (id, _) = HalApi::hub(self).adapters.prepare(new_id).assign(adapter); + let (id, _) = HalApi::hub(self) + .adapters + .prepare(new_id) + .assign(Arc::new(adapter)); Some(id) } } @@ -937,13 +941,13 @@ impl Global { let (id, _adapter): (_, Arc>) = match A::VARIANT { #[cfg(vulkan)] - Backend::Vulkan => fid.assign(Adapter::new(hal_adapter)), + Backend::Vulkan => fid.assign(Arc::new(Adapter::new(hal_adapter))), #[cfg(metal)] - Backend::Metal => fid.assign(Adapter::new(hal_adapter)), + Backend::Metal => fid.assign(Arc::new(Adapter::new(hal_adapter))), #[cfg(dx12)] - Backend::Dx12 => fid.assign(Adapter::new(hal_adapter)), + Backend::Dx12 => fid.assign(Arc::new(Adapter::new(hal_adapter))), #[cfg(gles)] - Backend::Gl => fid.assign(Adapter::new(hal_adapter)), + Backend::Gl => fid.assign(Arc::new(Adapter::new(hal_adapter))), _ => unreachable!(), }; resource_log!("Created Adapter {:?}", id); @@ -1066,13 +1070,13 @@ impl Global { Ok((device, queue)) => (device, queue), Err(e) => break e, }; - let (device_id, _) = device_fid.assign(device); + let (device_id, _) = device_fid.assign(Arc::new(device)); resource_log!("Created Device {:?}", device_id); let device = hub.devices.get(device_id).unwrap(); queue.device = Some(device.clone()); - let (queue_id, queue) = queue_fid.assign(queue); + let (queue_id, queue) = queue_fid.assign(Arc::new(queue)); resource_log!("Created Queue {:?}", queue_id); device.set_queue(queue); @@ -1118,13 +1122,13 @@ impl Global { Ok(device) => device, Err(e) => break e, }; - let (device_id, _) = devices_fid.assign(device); + let (device_id, _) = devices_fid.assign(Arc::new(device)); resource_log!("Created Device {:?}", device_id); let device = hub.devices.get(device_id).unwrap(); queue.device = Some(device.clone()); - let (queue_id, queue) = queues_fid.assign(queue); + let (queue_id, queue) = queues_fid.assign(Arc::new(queue)); resource_log!("Created Queue {:?}", queue_id); device.set_queue(queue); diff --git a/wgpu-core/src/present.rs b/wgpu-core/src/present.rs index cb4e17798..2f274cd55 100644 --- a/wgpu-core/src/present.rs +++ b/wgpu-core/src/present.rs @@ -9,7 +9,7 @@ When this texture is presented, we remove it from the device tracker as well as extract it from the hub. !*/ -use std::borrow::Borrow; +use std::{borrow::Borrow, sync::Arc}; #[cfg(feature = "trace")] use crate::device::trace::Action; @@ -231,7 +231,7 @@ impl Global { bind_groups: Mutex::new(Vec::new()), }; - let (id, resource) = fid.assign(texture); + let (id, resource) = fid.assign(Arc::new(texture)); log::debug!("Created CURRENT Surface Texture {:?}", id); { diff --git a/wgpu-core/src/registry.rs b/wgpu-core/src/registry.rs index 80394351a..878d61453 100644 --- a/wgpu-core/src/registry.rs +++ b/wgpu-core/src/registry.rs @@ -78,12 +78,20 @@ impl FutureId<'_, T> { Arc::new(value) } + pub fn init_in_place(&self, mut value: Arc) -> Arc { + Arc::get_mut(&mut value) + .unwrap() + .as_info_mut() + .set_id(self.id); + value + } + /// Assign a new resource to this ID. /// /// Registers it with the registry, and fills out the resource info. - pub fn assign(self, value: T) -> (Id, Arc) { + pub fn assign(self, value: Arc) -> (Id, Arc) { let mut data = self.data.write(); - data.insert(self.id, self.init(value)); + data.insert(self.id, self.init_in_place(value)); (self.id, data.get(self.id).unwrap().clone()) }