Skip to content

Commit

Permalink
[wgpu-core] pass resources as Arcs when adding them to the registry (#…
Browse files Browse the repository at this point in the history
…5499)

* [wgpu-core] pass resources as Arcs when adding them to the registry (fix #5493)

* [wgpu-core] also add `Arc::new` to `#[cfg(dx12)]` blocks

* [wgpu-core] allow `clippy::arc_with_non_send_sync`
  • Loading branch information
lylythechosenone authored Apr 6, 2024
1 parent 1c48a23 commit d814851
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 deletions.
32 changes: 16 additions & 16 deletions wgpu-core/src/device/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
34 changes: 19 additions & 15 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -937,13 +941,13 @@ impl Global {

let (id, _adapter): (_, Arc<Adapter<A>>) = 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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/present.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

{
Expand Down
12 changes: 10 additions & 2 deletions wgpu-core/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,20 @@ impl<T: Resource> FutureId<'_, T> {
Arc::new(value)
}

pub fn init_in_place(&self, mut value: Arc<T>) -> Arc<T> {
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<T::Marker>, Arc<T>) {
pub fn assign(self, value: Arc<T>) -> (Id<T::Marker>, Arc<T>) {
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())
}

Expand Down

0 comments on commit d814851

Please sign in to comment.