mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-24 08:21:25 +01:00
Vulkan: recover from fragmented descriptor pool allocation failures (#10264)
This commit is contained in:
1 parent
79348d63e6
commit
573944a971
2 files changed
+117
-37
No files matched your search
@@ -246,6 +246,7 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109).
|
||||
- Request `VK_KHR_spirv_1_4` and raise the generated SPIR-V version to 1.4 when `EXPERIMENTAL_RAY_TRACING_PIPELINES` or `EXPERIMENTAL_MESH_SHADER` is enabled on a pre-Vulkan-1.2 device. Both `SPV_KHR_ray_tracing` and `SPV_EXT_mesh_shader` require SPIR-V 1.4, but shaders were generated as 1.3 there: ray tracing pipelines requested neither the extension nor the version, and mesh shaders requested the extension without raising the version. Naga now rejects ray tracing pipeline shaders targeting below SPIR-V 1.4, as it already did for mesh shaders. By @JMS55 in [#10193](https://github.com/gfx-rs/wgpu/pull/10193).
|
||||
- Fix feature detection for [Robust Image Access](https://docs.vulkan.org/spec/latest/chapters/shaders.html#shaders-robust-image-access) when `VK_EXT_robustness2` is present but reports no support for [Robust Image Access 2](https://docs.vulkan.org/spec/latest/chapters/shaders.html#shaders-robust-image-access2), fixing a shader compilation crash on some Mali drivers. By @raphlinus in [#10291](https://github.com/gfx-rs/wgpu/pull/10291).
|
||||
- Fixed a panic on the Vulkan backend when dropping a surface whose acquired texture was still alive (e.g. after `present` failed due to a lost device). By @MarcelStruckWO in [#10230](https://github.com/gfx-rs/wgpu/pull/10230).
|
||||
- Recover from `VK_ERROR_FRAGMENTED_POOL` and `VK_ERROR_OUT_OF_POOL_MEMORY` when allocating descriptor sets by resetting or retiring the offending pool and retrying on the next available (or a freshly created) pool instead of panicking. By @beicause in [#10264](https://github.com/gfx-rs/wgpu/pull/10264).
|
||||
|
||||
#### Metal
|
||||
|
||||
|
||||
@@ -56,6 +56,10 @@ struct Pool {
|
||||
raw: vk::DescriptorPool,
|
||||
capacity: u32,
|
||||
available: u32,
|
||||
/// Set when the driver refused to allocate from this pool despite it
|
||||
/// having free capacity, making `alloc` skip it until the pool is empty
|
||||
/// again and can be reset.
|
||||
fragmented: bool,
|
||||
}
|
||||
|
||||
/// Keeps track of all pools created with this bucket's [`BucketKey`].
|
||||
@@ -169,46 +173,100 @@ impl DescriptorAllocator {
|
||||
|
||||
let key = layout.bucket_key();
|
||||
let bucket = self.buckets.get_mut(&key).unwrap();
|
||||
let capacity_hint = bucket.allocated_sets;
|
||||
|
||||
// Prefer smaller/older/fuller pools for new allocations to prevent
|
||||
// fragmentation and possibly fragmentation of hardware resources
|
||||
// (VK_ERROR_FRAGMENTATION)
|
||||
let pool = bucket
|
||||
let mut candidate = bucket
|
||||
.pools
|
||||
.iter_mut()
|
||||
.enumerate()
|
||||
.find(|(_, pool)| pool.available != 0);
|
||||
.iter()
|
||||
.position(|pool| !pool.fragmented && pool.available != 0);
|
||||
|
||||
let (pool_index, pool) = if let Some(pool) = pool {
|
||||
pool
|
||||
} else {
|
||||
let capacity_hint = bucket.allocated_sets;
|
||||
bucket.create_pool(device, &key, capacity_hint)?
|
||||
let (raw, pool_index) = loop {
|
||||
let index = match candidate {
|
||||
Some(index) => index,
|
||||
None => bucket.create_pool(device, &key, capacity_hint)?.0,
|
||||
};
|
||||
let created_pool = candidate.is_none();
|
||||
|
||||
let vk_info = vk::DescriptorSetAllocateInfo::default()
|
||||
.descriptor_pool(bucket.pools[index].raw)
|
||||
.set_layouts(core::slice::from_ref(&layout.raw));
|
||||
|
||||
// From https://docs.vulkan.org/refpages/latest/refpages/source/VkDescriptorPoolCreateInfo.html#_description
|
||||
// > if all sets allocated from the pool since it was created or
|
||||
// > most recently reset use the same number of descriptors
|
||||
// > (of each type) and the requested allocation also uses that
|
||||
// > same number of descriptors (of each type), then
|
||||
// > fragmentation must not cause an allocation failure
|
||||
//
|
||||
// These pools satisfy that condition, but some drivers (e.g.
|
||||
// Mali) fail such allocations anyway once a pool has served
|
||||
// a number of allocations; freeing sets does not make the
|
||||
// pool usable again, only resetting it does.
|
||||
//
|
||||
// Recover instead of failing: reset a pool that has no live sets,
|
||||
// otherwise mark the pool `fragmented` so that allocations skip
|
||||
// it. A fragmented pool is reset once its last set is freed.
|
||||
//
|
||||
// See https://github.com/gfx-rs/wgpu/pull/10264
|
||||
match unsafe { device.allocate_descriptor_sets(&vk_info) } {
|
||||
Ok(sets) => break (sets[0], index),
|
||||
Err(vk::Result::ERROR_FRAGMENTED_POOL | vk::Result::ERROR_OUT_OF_POOL_MEMORY) => {
|
||||
if created_pool {
|
||||
// A fresh pool failing to allocate even a single set leaves
|
||||
// no pool left to fall back to, so drop it and give up.
|
||||
let pool = bucket.pools.pop().unwrap();
|
||||
bucket.available_sets -= pool.capacity;
|
||||
unsafe { device.destroy_descriptor_pool(pool.raw, None) };
|
||||
log::error!(
|
||||
"descriptor set allocation from a fresh pool failed unexpectedly"
|
||||
);
|
||||
return Err(crate::DeviceError::OutOfMemory);
|
||||
}
|
||||
|
||||
// Resetting a pool restores the spec guarantee that
|
||||
// fragmentation cannot cause an allocation failure, but it
|
||||
// implicitly frees all of the pool's sets, so only reset a
|
||||
// pool that has no live sets.
|
||||
let pool = &bucket.pools[index];
|
||||
if pool.available == pool.capacity {
|
||||
let reset_result = unsafe {
|
||||
device.reset_descriptor_pool(
|
||||
pool.raw,
|
||||
vk::DescriptorPoolResetFlags::empty(),
|
||||
)
|
||||
};
|
||||
if reset_result.is_ok() {
|
||||
match unsafe { device.allocate_descriptor_sets(&vk_info) } {
|
||||
Ok(sets) => break (sets[0], index),
|
||||
Err(
|
||||
vk::Result::ERROR_FRAGMENTED_POOL
|
||||
| vk::Result::ERROR_OUT_OF_POOL_MEMORY,
|
||||
) => {}
|
||||
Err(err) => return Err(super::map_host_device_oom_err(err)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let available = bucket.pools[index].available;
|
||||
let capacity = bucket.pools[index].capacity;
|
||||
bucket.pools[index].fragmented = true;
|
||||
bucket.available_sets -= available;
|
||||
log::debug!(
|
||||
"Retiring fragmented descriptor pool ({available}/{capacity} sets free), retrying on another pool"
|
||||
);
|
||||
candidate = bucket
|
||||
.pools
|
||||
.iter()
|
||||
.position(|pool| !pool.fragmented && pool.available != 0);
|
||||
}
|
||||
Err(err) => return Err(super::map_host_device_oom_err(err)),
|
||||
}
|
||||
};
|
||||
|
||||
let vk_info = vk::DescriptorSetAllocateInfo::default()
|
||||
.descriptor_pool(pool.raw)
|
||||
.set_layouts(core::slice::from_ref(&layout.raw));
|
||||
|
||||
let raw = match unsafe { device.allocate_descriptor_sets(&vk_info) } {
|
||||
Ok(sets) => Ok(sets[0]),
|
||||
// We make sure not to exceed the size of the pool.
|
||||
Err(vk::Result::ERROR_OUT_OF_POOL_MEMORY) => unreachable!(),
|
||||
// We only allocate from a pool if the nr and type of descriptors
|
||||
// used by a layout is the same as those specified at pool
|
||||
// creation time.
|
||||
//
|
||||
// > Additionally, if all sets allocated from the pool since it was
|
||||
// created or most recently reset use the same number of
|
||||
// descriptors (of each type) and the requested allocation also
|
||||
// uses that same number of descriptors (of each type),
|
||||
// then fragmentation must not cause an allocation failure.
|
||||
//
|
||||
// from https://docs.vulkan.org/refpages/latest/refpages/source/VkDescriptorPoolCreateInfo.html#_description
|
||||
Err(vk::Result::ERROR_FRAGMENTED_POOL) => unreachable!(),
|
||||
Err(err) => Err(super::map_host_device_oom_err(err)),
|
||||
}?;
|
||||
|
||||
let pool = &mut bucket.pools[pool_index];
|
||||
pool.available -= 1;
|
||||
bucket.available_sets -= 1;
|
||||
bucket.allocated_sets += 1;
|
||||
@@ -240,7 +298,22 @@ impl DescriptorAllocator {
|
||||
}
|
||||
|
||||
pool.available += 1;
|
||||
bucket.available_sets += 1;
|
||||
|
||||
if !pool.fragmented {
|
||||
bucket.available_sets += 1;
|
||||
} else if pool.available == pool.capacity {
|
||||
// A fragmented pool's free sets are unusable until it is reset, so
|
||||
// they do not count towards `available_sets`.
|
||||
// On Mali, freeing sets does not make such a pool usable again;
|
||||
// resetting it is what does, and that is only safe once no sets are live.
|
||||
let reset_result = unsafe {
|
||||
device.reset_descriptor_pool(pool.raw, vk::DescriptorPoolResetFlags::empty())
|
||||
};
|
||||
if reset_result.is_ok() {
|
||||
pool.fragmented = false;
|
||||
bucket.available_sets += pool.capacity;
|
||||
}
|
||||
}
|
||||
bucket.allocated_sets -= 1;
|
||||
if set.bucket_key.update_after_bind {
|
||||
self.update_after_bind_descriptors_in_all_pools -= set.bucket_key.counts.total();
|
||||
@@ -251,13 +324,18 @@ impl DescriptorAllocator {
|
||||
// Destroy a pool only if it's empty and we have 1/4th its capacity
|
||||
// in other pools.
|
||||
// Note that this logic will never destroy the last pool.
|
||||
let pool = bucket.pools.last().unwrap();
|
||||
if pool.available == pool.capacity
|
||||
&& bucket.available_sets - pool.capacity > pool.capacity / 4
|
||||
let last = bucket.pools.last().unwrap();
|
||||
let last_available = last.available;
|
||||
let last_capacity = last.capacity;
|
||||
// Only a non-fragmented pool contributes its capacity to
|
||||
// `available_sets`.
|
||||
let last_contribution = if last.fragmented { 0 } else { last_capacity };
|
||||
if last_available == last_capacity
|
||||
&& bucket.available_sets - last_contribution > last_capacity / 4
|
||||
{
|
||||
let pool = bucket.pools.pop().unwrap();
|
||||
unsafe { device.destroy_descriptor_pool(pool.raw, None) };
|
||||
bucket.available_sets -= pool.capacity;
|
||||
bucket.available_sets -= last_contribution;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -346,5 +424,6 @@ fn create_descriptor_pool(
|
||||
raw,
|
||||
capacity,
|
||||
available: capacity,
|
||||
fragmented: false,
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user