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

[InstanceInputUniformBuffer::remove] could cause incorrect behaviour. #17166

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
21 changes: 21 additions & 0 deletions crates/bevy_render/src/batching/gpu_preprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
Loading