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

Texture & buffer call now destroy on removal from pool #1359

Merged
merged 1 commit into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion crates/re_renderer/src/wgpu_resources/bind_group_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl GpuBindGroupPool {
_buffers: &mut GpuBufferPool,
_samplers: &mut GpuSamplerPool,
) {
self.pool.frame_maintenance(frame_index);
self.pool.frame_maintenance(frame_index, |_res| {});
// TODO(andreas): Update usage counter on dependent resources.
}

Expand Down
3 changes: 2 additions & 1 deletion crates/re_renderer/src/wgpu_resources/buffer_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ impl GpuBufferPool {

/// Called by `RenderContext` every frame. Updates statistics and may free unused buffers.
pub fn frame_maintenance(&mut self, frame_index: u64) {
self.pool.frame_maintenance(frame_index);
self.pool
.frame_maintenance(frame_index, |res| res.destroy());
}

/// Takes strong buffer handle to ensure the user is still holding on to the buffer.
Expand Down
32 changes: 23 additions & 9 deletions crates/re_renderer/src/wgpu_resources/dynamic_resource_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ where
})
}

pub fn frame_maintenance(&mut self, frame_index: u64) {
pub fn frame_maintenance(
&mut self,
frame_index: u64,
mut on_destroy_resource: impl FnMut(&Res),
) {
self.current_frame_index = frame_index;

// Throw out any resources that we haven't reclaimed last frame.
Expand All @@ -102,7 +106,9 @@ where
"Drained dangling resources from last frame",
);
for handle in handles {
self.resources.remove(*handle);
if let Some((_, res)) = self.resources.remove(*handle) {
on_destroy_resource(&res);
}
self.total_resource_size_in_bytes -= desc.resource_size_in_bytes();
}
}
Expand Down Expand Up @@ -199,8 +205,10 @@ mod tests {
// Still, no resources were dropped.
{
let drop_counter_before = drop_counter.load(Ordering::Acquire);
pool.frame_maintenance(1);
let mut called_destroy = false;
pool.frame_maintenance(1, |_| called_destroy = true);

assert!(!called_destroy);
assert_eq!(drop_counter_before, drop_counter.load(Ordering::Acquire),);
}

Expand All @@ -212,8 +220,11 @@ mod tests {
// Doing frame maintenance twice will drop all resources
{
let drop_counter_before = drop_counter.load(Ordering::Acquire);
pool.frame_maintenance(2);
pool.frame_maintenance(3);
let mut called_destroy = false;
pool.frame_maintenance(2, |_| called_destroy = true);
assert!(!called_destroy);
pool.frame_maintenance(3, |_| called_destroy = true);
assert!(called_destroy);
let drop_counter_now = drop_counter.load(Ordering::Acquire);
assert_eq!(
drop_counter_before + initial_resource_descs.len() * 2,
Expand All @@ -236,9 +247,12 @@ mod tests {
assert_ne!(handle0, handle1);
drop(handle1);

pool.frame_maintenance(4);
let mut called_destroy = false;
pool.frame_maintenance(4, |_| called_destroy = true);
assert!(!called_destroy);
assert_eq!(drop_counter_before, drop_counter.load(Ordering::Acquire),);
pool.frame_maintenance(5);
pool.frame_maintenance(5, |_| called_destroy = true);
assert!(called_destroy);
assert_eq!(
drop_counter_before + 1,
drop_counter.load(Ordering::Acquire),
Expand Down Expand Up @@ -314,8 +328,8 @@ mod tests {
// Query with invalid handle
let inner_handle = *handle;
drop(handle);
pool.frame_maintenance(0);
pool.frame_maintenance(1);
pool.frame_maintenance(0, |_| {});
pool.frame_maintenance(1, |_| {});
assert!(matches!(
pool.get_resource(inner_handle),
Err(PoolError::ResourceNotAvailable)
Expand Down
3 changes: 2 additions & 1 deletion crates/re_renderer/src/wgpu_resources/texture_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ impl GpuTexturePool {

/// Called by `RenderContext` every frame. Updates statistics and may free unused textures.
pub fn frame_maintenance(&mut self, frame_index: u64) {
self.pool.frame_maintenance(frame_index);
self.pool
.frame_maintenance(frame_index, |res| res.texture.destroy());
}

/// Takes strong texture handle to ensure the user is still holding on to the texture.
Expand Down