Skip to content

Commit

Permalink
Add basic pool memory management functions
Browse files Browse the repository at this point in the history
  • Loading branch information
attackgoat committed Jan 28, 2024
1 parent 4b308e7 commit a334604
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 6 deletions.
55 changes: 54 additions & 1 deletion src/pool/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use {
RenderPass, RenderPassInfo,
},
parking_lot::Mutex,
paste::paste,
std::{
collections::{HashMap, VecDeque},
sync::Arc,
Expand All @@ -34,7 +35,6 @@ pub struct HashPool {
render_pass_cache: HashMap<RenderPassInfo, Cache<RenderPass>>,
}

// TODO: Add some sort of manager features (like, I dunno, "Clear Some Memory For me")
impl HashPool {
/// Constructs a new `HashPool`.
pub fn new(device: &Arc<Device>) -> Self {
Expand All @@ -50,8 +50,61 @@ impl HashPool {
render_pass_cache: Default::default(),
}
}

/// Clears the pool, removing all resources.
pub fn clear(&mut self) {
self.clear_accel_structs();
self.clear_buffers();
self.clear_images();
}
}

macro_rules! resource_mgmt_fns {
($fn_plural:literal, $doc_plural:literal, $ty:ty, $field:ident) => {
paste! {
impl HashPool {
#[doc = "Clears the pool of " $doc_plural ", removing all resources."]
pub fn [<clear_ $fn_plural>](&mut self) {
self.$field.clear();
}

#[doc = "Clears the pool of " $doc_plural ", removing resources matching the
given information."]
pub fn [<clear_ $fn_plural _by_info>](
&mut self,
info: impl Into<$ty>,
) {
self.$field.remove(&info.into());
}

#[doc = "Retains only the " $doc_plural " specified by the predicate.\n\nIn other
words, remove all resources for which `f(&" $ty ")` returns `false`.\n\n"]
/// The elements are visited in unsorted (and unspecified) order.
///
/// # Performance
///
/// Provides the same performance guarantees as
/// [`HashMap::retain`](HashMap::retain).
pub fn [<retain_ $fn_plural>]<F>(&mut self, mut f: F)
where
F: FnMut(&$ty) -> bool,
{
self.$field.retain(|info, _| f(info))
}
}
}
};
}

resource_mgmt_fns!(
"accel_structs",
"acceleration structures",
AccelerationStructureInfo,
acceleration_structure_cache
);
resource_mgmt_fns!("buffers", "buffers", BufferInfo, buffer_cache);
resource_mgmt_fns!("images", "images", ImageInfo, image_cache);

impl Pool<CommandBufferInfo, CommandBuffer> for HashPool {
#[profiling::function]
fn lease(&mut self, info: CommandBufferInfo) -> Result<Lease<CommandBuffer>, DriverError> {
Expand Down
52 changes: 47 additions & 5 deletions src/pool/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub struct LazyPool {
render_pass_cache: HashMap<RenderPassInfo, Cache<RenderPass>>,
}

// TODO: Add some sort of manager features (like, I dunno, "Clear Some Memory For me")
impl LazyPool {
/// Constructs a new `LazyPool`.
pub fn new(device: &Arc<Device>) -> Self {
Expand All @@ -66,6 +65,52 @@ impl LazyPool {
render_pass_cache: Default::default(),
}
}

/// Clears the pool, removing all resources.
pub fn clear(&mut self) {
self.clear_accel_structs();
self.clear_buffers();
self.clear_images();
}

/// Clears the pool of acceleration structures, removing all resources.
pub fn clear_accel_structs(&mut self) {
self.acceleration_structure_cache.clear();
}

/// Clears the pool of acceleration structures, removing resources matching the given
/// type.
pub fn clear_accel_structs_by_ty(&mut self, ty: vk::AccelerationStructureTypeKHR) {
self.acceleration_structure_cache.remove(&ty);
}

/// Clears the pool of buffers, removing all resources.
pub fn clear_buffers(&mut self) {
self.buffer_cache.clear();
}

/// Clears the pool of images, removing all resources.
pub fn clear_images(&mut self) {
self.image_cache.clear();
}

/// Retains only the acceleration structures specified by the predicate.
///
/// In other words, remove all resources for which `f(vk::AccelerationStructureTypeKHR)` returns
/// `false`.
///
/// The elements are visited in unsorted (and unspecified) order.
///
/// # Performance
///
/// Provides the same performance guarantees as
/// [`HashMap::retain`](HashMap::retain).
pub fn retain_accel_structs<F>(&mut self, mut f: F)
where
F: FnMut(vk::AccelerationStructureTypeKHR) -> bool,
{
self.acceleration_structure_cache.retain(|&ty, _| f(ty))
}
}

impl Pool<AccelerationStructureInfo, AccelerationStructure> for LazyPool {
Expand Down Expand Up @@ -123,10 +168,7 @@ impl Pool<BufferInfo, Buffer> for LazyPool {
// Look for a compatible buffer (same mapping mode, big enough, superset of usage flags)
for idx in 0..cache.len() {
let item = &cache[idx];
if item.info.can_map == info.can_map
&& item.info.size >= info.size
&& item.info.usage.contains(info.usage)
{
if item.info.size >= info.size && item.info.usage.contains(info.usage) {
let item = cache.remove(idx).unwrap();

return Ok(Lease::new(cache_ref, item));
Expand Down

0 comments on commit a334604

Please sign in to comment.