Skip to content

Commit

Permalink
Texture & buffer call now destroy on removal from pool (#1359)
Browse files Browse the repository at this point in the history
Fixes #592
  • Loading branch information
Wumpf authored Feb 20, 2023
1 parent 96437ee commit 548d269
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
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

1 comment on commit 548d269

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust Benchmark

Benchmark suite Current: 548d269 Previous: 96437ee Ratio
datastore/insert/batch/rects/insert 562227 ns/iter (± 5000) 563724 ns/iter (± 3791) 1.00
datastore/latest_at/batch/rects/query 1835 ns/iter (± 11) 1796 ns/iter (± 7) 1.02
datastore/latest_at/missing_components/primary 356 ns/iter (± 0) 356 ns/iter (± 3) 1
datastore/latest_at/missing_components/secondaries 433 ns/iter (± 2) 421 ns/iter (± 4) 1.03
datastore/range/batch/rects/query 153144 ns/iter (± 1033) 149726 ns/iter (± 1175) 1.02
mono_points_arrow/generate_message_bundles 45934909 ns/iter (± 954488) 46719032 ns/iter (± 1904548) 0.98
mono_points_arrow/generate_messages 124859394 ns/iter (± 1122795) 137478410 ns/iter (± 1668570) 0.91
mono_points_arrow/encode_log_msg 152120205 ns/iter (± 832273) 161753157 ns/iter (± 5188542) 0.94
mono_points_arrow/encode_total 322750928 ns/iter (± 1610749) 349874593 ns/iter (± 2521500) 0.92
mono_points_arrow/decode_log_msg 175393518 ns/iter (± 840170) 184719153 ns/iter (± 1625745) 0.95
mono_points_arrow/decode_message_bundles 64581240 ns/iter (± 884473) 73166990 ns/iter (± 1399405) 0.88
mono_points_arrow/decode_total 237345731 ns/iter (± 2848358) 254519335 ns/iter (± 2142192) 0.93
batch_points_arrow/generate_message_bundles 331144 ns/iter (± 1088) 328052 ns/iter (± 5573) 1.01
batch_points_arrow/generate_messages 6299 ns/iter (± 18) 6268 ns/iter (± 58) 1.00
batch_points_arrow/encode_log_msg 371276 ns/iter (± 1319) 367033 ns/iter (± 1573) 1.01
batch_points_arrow/encode_total 716954 ns/iter (± 2274) 711300 ns/iter (± 8149) 1.01
batch_points_arrow/decode_log_msg 348583 ns/iter (± 913) 349155 ns/iter (± 1028) 1.00
batch_points_arrow/decode_message_bundles 2058 ns/iter (± 15) 2005 ns/iter (± 19) 1.03
batch_points_arrow/decode_total 346709 ns/iter (± 1881) 355303 ns/iter (± 1899) 0.98
arrow_mono_points/insert 6021744068 ns/iter (± 15125163) 7037126675 ns/iter (± 30997466) 0.86
arrow_mono_points/query 1714409 ns/iter (± 8578) 1702561 ns/iter (± 16835) 1.01
arrow_batch_points/insert 2618441 ns/iter (± 10492) 2668694 ns/iter (± 21345) 0.98
arrow_batch_points/query 16864 ns/iter (± 56) 16548 ns/iter (± 221) 1.02
tuid/Tuid::random 34 ns/iter (± 0) 34 ns/iter (± 0) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.