From a4f91402a59121071f5bce8f9147cd54794f2fd8 Mon Sep 17 00:00:00 2001 From: Tim Overbeek Date: Wed, 15 Jan 2025 16:26:46 +0100 Subject: [PATCH 1/4] remove is safer --- .../src/batching/gpu_preprocessing.rs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/bevy_render/src/batching/gpu_preprocessing.rs b/crates/bevy_render/src/batching/gpu_preprocessing.rs index 2e893616f9294..12ed7732144b7 100644 --- a/crates/bevy_render/src/batching/gpu_preprocessing.rs +++ b/crates/bevy_render/src/batching/gpu_preprocessing.rs @@ -218,7 +218,22 @@ where /// Removes a piece of buffered data from the uniform buffer. /// /// This simply marks the data as free. + /// + /// # Panics + /// + /// If [`Self::buffer`] length < `uniform_index`. pub fn remove(&mut self, uniform_index: u32) { + assert!((uniform_index as usize) < self.buffer.len()); + if !self.free_uniform_indices.contains(&uniform_index) { + self.free_uniform_indices.push(uniform_index); + } + } + + /// Removes a piece of buffered data from the uniform buffer. + /// + /// Ensure uniform_index is within bounds of [`Self::buffer`] and not removed twice, + /// as this may cause incorrect behavior or panicking when inserting new data. + pub fn remove_unchecked(&mut self, uniform_index: u32) { self.free_uniform_indices.push(uniform_index); } @@ -1470,11 +1485,17 @@ mod tests { let mut instance_buffer = InstanceInputUniformBuffer::new(); let index = instance_buffer.add(2); + // double removal should not queue the same index twice. + instance_buffer.remove(index); instance_buffer.remove(index); assert_eq!(instance_buffer.get_unchecked(index), 2); assert_eq!(instance_buffer.get(index), None); instance_buffer.add(5); + assert_eq!(instance_buffer.buffer().len(), 1); + + instance_buffer.add(5); + assert_eq!(instance_buffer.buffer().len(), 2); } } From 2c741e0d20ad4867cc621a9cd9355e81829500ac Mon Sep 17 00:00:00 2001 From: Tim Overbeek Date: Wed, 15 Jan 2025 16:31:35 +0100 Subject: [PATCH 2/4] whoops --- crates/bevy_pbr/src/render/mesh.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index dd92ef7828eec..cd0aa515564b3 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -1038,7 +1038,7 @@ fn remove_mesh_input_uniform( let removed_render_mesh_instance = render_mesh_instances.remove(&entity)?; let removed_uniform_index = removed_render_mesh_instance.current_uniform_index.get(); - current_input_buffer.remove(removed_uniform_index); + current_input_buffer.remove_unchecked(removed_uniform_index); Some(removed_uniform_index) } From eadce6421b2a98d4622f719c3592412a64b37505 Mon Sep 17 00:00:00 2001 From: Tim Overbeek Date: Wed, 15 Jan 2025 16:39:22 +0100 Subject: [PATCH 3/4] docs --- crates/bevy_render/src/batching/gpu_preprocessing.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_render/src/batching/gpu_preprocessing.rs b/crates/bevy_render/src/batching/gpu_preprocessing.rs index 12ed7732144b7..7b0da824ea971 100644 --- a/crates/bevy_render/src/batching/gpu_preprocessing.rs +++ b/crates/bevy_render/src/batching/gpu_preprocessing.rs @@ -231,7 +231,7 @@ where /// Removes a piece of buffered data from the uniform buffer. /// - /// Ensure uniform_index is within bounds of [`Self::buffer`] and not removed twice, + /// Ensure `uniform_index` is within bounds of [`Self::buffer`] and not removed twice, /// as this may cause incorrect behavior or panicking when inserting new data. pub fn remove_unchecked(&mut self, uniform_index: u32) { self.free_uniform_indices.push(uniform_index); @@ -1499,3 +1499,4 @@ mod tests { assert_eq!(instance_buffer.buffer().len(), 2); } } + From e77f0e5e96044b661ac7a55396926a335ff02e9c Mon Sep 17 00:00:00 2001 From: Tim Overbeek Date: Wed, 15 Jan 2025 16:45:49 +0100 Subject: [PATCH 4/4] fmt --- crates/bevy_render/src/batching/gpu_preprocessing.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/bevy_render/src/batching/gpu_preprocessing.rs b/crates/bevy_render/src/batching/gpu_preprocessing.rs index 7b0da824ea971..e3d9132a82c67 100644 --- a/crates/bevy_render/src/batching/gpu_preprocessing.rs +++ b/crates/bevy_render/src/batching/gpu_preprocessing.rs @@ -1499,4 +1499,3 @@ mod tests { assert_eq!(instance_buffer.buffer().len(), 2); } } -