Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix locking order in render_pipeline_get_bind_group_layout #4253

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 68 additions & 35 deletions wgpu-core/src/device/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1915,42 +1915,50 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
) {
let hub = A::hub(self);
let mut token = Token::root();
let (pipeline_layout_guard, mut token) = hub.pipeline_layouts.read(&mut token);

let error = loop {
let (bgl_guard, mut token) = hub.bind_group_layouts.read(&mut token);
let (_, mut token) = hub.bind_groups.read(&mut token);
let (pipeline_guard, _) = hub.render_pipelines.read(&mut token);
let device_id;
let id;

let pipeline = match pipeline_guard.get(pipeline_id) {
Ok(pipeline) => pipeline,
Err(_) => break binding_model::GetBindGroupLayoutError::InvalidPipeline,
};
let id = match pipeline_layout_guard[pipeline.layout_id.value]
.bind_group_layout_ids
.get(index as usize)
{
Some(id) => id,
None => break binding_model::GetBindGroupLayoutError::InvalidGroupIndex(index),
};
let (pipeline_layout_guard, mut token) = hub.pipeline_layouts.read(&mut token);

let (bgl_guard, mut token) = hub.bind_group_layouts.read(&mut token);
let (_, mut token) = hub.bind_groups.read(&mut token);
let (pipeline_guard, _) = hub.render_pipelines.read(&mut token);

let pipeline = match pipeline_guard.get(pipeline_id) {
Ok(pipeline) => pipeline,
Err(_) => break binding_model::GetBindGroupLayoutError::InvalidPipeline,
};
id = match pipeline_layout_guard[pipeline.layout_id.value]
.bind_group_layout_ids
.get(index as usize)
{
Some(id) => *id,
None => break binding_model::GetBindGroupLayoutError::InvalidGroupIndex(index),
};

let layout = &bgl_guard[*id];
layout.multi_ref_count.inc();
let layout = &bgl_guard[id];
layout.multi_ref_count.inc();

if G::ids_are_generated_in_wgpu() {
return (id.0, None);
if G::ids_are_generated_in_wgpu() {
return (id.0, None);
}

device_id = layout.device_id.clone();
}

// The ID is provided externally, so we must create a new bind group layout
// with the given ID as a duplicate of the existing one.
let new_layout = BindGroupLayout {
device_id: layout.device_id.clone(),
inner: crate::binding_model::BglOrDuplicate::<A>::Duplicate(*id),
device_id,
inner: crate::binding_model::BglOrDuplicate::<A>::Duplicate(id),
multi_ref_count: crate::MultiRefCount::new(),
};

let fid = hub.bind_group_layouts.prepare(id_in);
let id = fid.assign(new_layout, &mut Token::root());
let id = fid.assign(new_layout, &mut token);

return (id.0, None);
};
Expand Down Expand Up @@ -2076,26 +2084,51 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
) {
let hub = A::hub(self);
let mut token = Token::root();
let (pipeline_layout_guard, mut token) = hub.pipeline_layouts.read(&mut token);

let error = loop {
let (bgl_guard, mut token) = hub.bind_group_layouts.read(&mut token);
let (_, mut token) = hub.bind_groups.read(&mut token);
let (pipeline_guard, _) = hub.compute_pipelines.read(&mut token);
let device_id;
let id;

let pipeline = match pipeline_guard.get(pipeline_id) {
Ok(pipeline) => pipeline,
Err(_) => break binding_model::GetBindGroupLayoutError::InvalidPipeline,
};
let id = match pipeline_layout_guard[pipeline.layout_id.value]
.bind_group_layout_ids
.get(index as usize)
{
Some(id) => id,
None => break binding_model::GetBindGroupLayoutError::InvalidGroupIndex(index),
let (pipeline_layout_guard, mut token) = hub.pipeline_layouts.read(&mut token);

let (bgl_guard, mut token) = hub.bind_group_layouts.read(&mut token);
let (_, mut token) = hub.bind_groups.read(&mut token);
let (pipeline_guard, _) = hub.compute_pipelines.read(&mut token);

let pipeline = match pipeline_guard.get(pipeline_id) {
Ok(pipeline) => pipeline,
Err(_) => break binding_model::GetBindGroupLayoutError::InvalidPipeline,
};
id = match pipeline_layout_guard[pipeline.layout_id.value]
.bind_group_layout_ids
.get(index as usize)
{
Some(id) => *id,
None => break binding_model::GetBindGroupLayoutError::InvalidGroupIndex(index),
};

let layout = &bgl_guard[id];
layout.multi_ref_count.inc();

if G::ids_are_generated_in_wgpu() {
return (id.0, None);
}

device_id = layout.device_id.clone();
}

// The ID is provided externally, so we must create a new bind group layout
// with the given ID as a duplicate of the existing one.
let new_layout = BindGroupLayout {
device_id,
inner: crate::binding_model::BglOrDuplicate::<A>::Duplicate(id),
multi_ref_count: crate::MultiRefCount::new(),
};

bgl_guard[*id].multi_ref_count.inc();
let fid = hub.bind_group_layouts.prepare(id_in);
let id = fid.assign(new_layout, &mut token);

return (id.0, None);
};

Expand Down