Skip to content

Commit

Permalink
Deduplicate submitted surfaces to avoid a vulkan deadlock/crash (#5799)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elabajaba authored and cwfitzgerald committed Jun 12, 2024
1 parent 6f1507f commit c8b8876
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,9 @@ impl Global {

let mut used_surface_textures = track::TextureUsageScope::default();

let mut submit_surface_textures_owned = SmallVec::<[_; 2]>::new();
// Use a hashmap here to deduplicate the surface textures that are used in the command buffers.
// This avoids vulkan deadlocking from the same surface texture being submitted multiple times.
let mut submit_surface_textures_owned = FastHashMap::default();

{
let mut command_buffer_guard = hub.command_buffers.write();
Expand Down Expand Up @@ -1279,7 +1281,9 @@ impl Global {
Some(TextureInner::Native { .. }) => false,
Some(TextureInner::Surface { ref raw, .. }) => {
if raw.is_some() {
submit_surface_textures_owned.push(texture.clone());
// Compare the Arcs by pointer as Textures don't implement Eq.
submit_surface_textures_owned
.insert(Arc::as_ptr(&texture), texture.clone());
}

true
Expand Down Expand Up @@ -1472,7 +1476,9 @@ impl Global {
Some(TextureInner::Native { .. }) => {}
Some(TextureInner::Surface { ref raw, .. }) => {
if raw.is_some() {
submit_surface_textures_owned.push(texture.clone());
// Compare the Arcs by pointer as Textures don't implement Eq
submit_surface_textures_owned
.insert(Arc::as_ptr(texture), texture.clone());
}

unsafe {
Expand Down Expand Up @@ -1517,7 +1523,7 @@ impl Global {
let mut submit_surface_textures =
SmallVec::<[_; 2]>::with_capacity(submit_surface_textures_owned.len());

for texture in &submit_surface_textures_owned {
for texture in submit_surface_textures_owned.values() {
submit_surface_textures.extend(match texture.inner.get(&snatch_guard) {
Some(TextureInner::Surface { raw, .. }) => raw.as_ref(),
_ => None,
Expand Down

0 comments on commit c8b8876

Please sign in to comment.