From c0c938bb0bd66b392848880eb98e527412cb8ee5 Mon Sep 17 00:00:00 2001 From: Horu <73709188+HigherOrderLogic@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:35:37 +1100 Subject: [PATCH] Discard unused `StagingBelt`'s free chunks (#10383) --- wgpu/src/util/belt.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/wgpu/src/util/belt.rs b/wgpu/src/util/belt.rs index 9a63ef50f..7d0ff3535 100644 --- a/wgpu/src/util/belt.rs +++ b/wgpu/src/util/belt.rs @@ -48,6 +48,8 @@ pub struct StagingBelt { sender: Exclusive>, /// Free chunks are received here to be put on `self.free_chunks`. receiver: Exclusive>, + /// Whether the belt has grown since last recall. + grew_since_recall: bool, } impl StagingBelt { @@ -116,6 +118,7 @@ impl StagingBelt { free_chunks: Vec::new(), sender: Exclusive::new(sender), receiver: Exclusive::new(receiver), + grew_since_recall: false, } } @@ -215,6 +218,7 @@ impl StagingBelt { { self.free_chunks.swap_remove(index) } else { + self.grew_since_recall = true; Chunk { buffer: self.device.create_buffer(&BufferDescriptor { label: Some("(wgpu internal) StagingBelt staging buffer"), @@ -223,11 +227,13 @@ impl StagingBelt { mapped_at_creation: true, }), offset: 0, + used_since_recall: false, } } }; let allocation_offset = chunk.allocate(size, alignment); + chunk.used_since_recall = true; self.active_chunks.push(chunk); let chunk = self.active_chunks.last().unwrap(); @@ -313,6 +319,22 @@ impl StagingBelt { self.free_chunks.push(chunk); } } + + /// Discard all free chunks that have not been used since the last + /// [`StagingBelt::recall()`] back to the device and unmap their buffers. + pub fn discard_free_chunks(&mut self) { + self.receive_chunks(); + self.free_chunks.retain_mut(|chunk| { + if !self.grew_since_recall || chunk.used_since_recall { + chunk.used_since_recall = false; + true + } else { + chunk.buffer.unmap(); + false + } + }); + self.grew_since_recall = false; + } } impl fmt::Debug for StagingBelt { @@ -326,6 +348,7 @@ impl fmt::Debug for StagingBelt { free_chunks, sender: _, receiver: _, + grew_since_recall: _, } = self; f.debug_struct("StagingBelt") .field("device", device) @@ -341,6 +364,7 @@ impl fmt::Debug for StagingBelt { struct Chunk { buffer: Buffer, offset: BufferAddress, + used_since_recall: bool, } impl Chunk {