diff --git a/crates/processing_render/src/destroy.rs b/crates/processing_render/src/destroy.rs new file mode 100644 index 00000000..72785948 --- /dev/null +++ b/crates/processing_render/src/destroy.rs @@ -0,0 +1,94 @@ +//! Deferred destroy: a destroy waits until every command buffer has drained. + +use bevy::prelude::*; +use processing_core::error::Result; + +use crate::render::command::CommandBuffer; + +type Destroy = Box Result<()> + Send + Sync>; + +/// Destroys waiting for the queued draw commands to run. +#[derive(Resource, Default)] +pub struct PendingDestroys(Vec); + +/// Whether any graphics still has draw commands to run. +fn commands_queued(world: &mut World) -> bool { + world + .query::<&CommandBuffer>() + .iter(world) + .any(|buffer| !buffer.commands.is_empty()) +} + +/// Runs `destroy` now, or once the queued draw commands have run. +pub fn request( + world: &mut World, + destroy: impl FnOnce(&mut World) -> Result<()> + Send + Sync + 'static, +) -> Result<()> { + if commands_queued(world) { + world + .get_resource_or_insert_with(PendingDestroys::default) + .0 + .push(Box::new(destroy)); + Ok(()) + } else { + destroy(world) + } +} + +/// Runs the waiting destroys if no draw command can still reference them. +pub fn run_pending(world: &mut World) { + if !world.contains_resource::() || commands_queued(world) { + return; + } + let pending = std::mem::take(&mut world.resource_mut::().0); + for destroy in pending { + if let Err(error) = destroy(world) { + debug!("deferred destroy failed: {error}"); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::render::command::DrawCommand; + + #[derive(Component)] + struct Thing; + + fn despawn(entity: Entity) -> impl FnOnce(&mut World) -> Result<()> + Send + Sync { + move |world| { + world.despawn(entity); + Ok(()) + } + } + + #[test] + fn a_destroy_runs_at_once_when_nothing_is_queued() { + let mut world = World::new(); + world.spawn(CommandBuffer::new()); + let thing = world.spawn(Thing).id(); + request(&mut world, despawn(thing)).unwrap(); + assert!(world.get_entity(thing).is_err()); + } + + #[test] + fn a_destroy_waits_for_queued_commands() { + let mut world = World::new(); + let mut buffer = CommandBuffer::new(); + buffer.push(DrawCommand::NoStroke); + let graphics = world.spawn(buffer).id(); + let thing = world.spawn(Thing).id(); + + request(&mut world, despawn(thing)).unwrap(); + run_pending(&mut world); + assert!( + world.get_entity(thing).is_ok(), + "still referenced by queued commands" + ); + + world.get_mut::(graphics).unwrap().clear(); + run_pending(&mut world); + assert!(world.get_entity(thing).is_err()); + } +} diff --git a/crates/processing_render/src/graphics.rs b/crates/processing_render/src/graphics.rs index dbebd924..f7b9eff8 100644 --- a/crates/processing_render/src/graphics.rs +++ b/crates/processing_render/src/graphics.rs @@ -762,6 +762,7 @@ pub fn flush(app: &mut App, entity: Entity) -> Result<()> { } } graphics_mut!(app, entity).remove::(); + crate::destroy::run_pending(app.world_mut()); Ok(()) } diff --git a/crates/processing_render/src/lib.rs b/crates/processing_render/src/lib.rs index bae4f96f..e6322a88 100644 --- a/crates/processing_render/src/lib.rs +++ b/crates/processing_render/src/lib.rs @@ -4,6 +4,7 @@ pub mod camera; pub mod ci; pub mod color; pub mod compute; +pub mod destroy; pub mod geometry; pub mod gltf; pub mod graphics; @@ -1349,9 +1350,11 @@ pub fn image_set_sampler(entity: Entity, filter: u8, wrap_x: u8, wrap_y: u8) -> /// Destroy an existing image and free its resources. pub fn image_destroy(entity: Entity) -> error::Result<()> { app_mut(|app| { - app.world_mut() - .run_system_cached_with(image::destroy, entity) - .unwrap() + destroy::request(app.world_mut(), move |world| { + world + .run_system_cached_with(image::destroy, entity) + .unwrap() + }) }) } @@ -1457,9 +1460,11 @@ pub fn graphics_immediate_light( pub fn light_destroy(entity: Entity) -> error::Result<()> { app_mut(|app| { - app.world_mut() - .run_system_cached_with(light::destroy, entity) - .unwrap() + destroy::request(app.world_mut(), move |world| { + world + .run_system_cached_with(light::destroy, entity) + .unwrap() + }) }) } @@ -1766,9 +1771,11 @@ pub fn geometry_get_indices(entity: Entity, start: usize, end: usize) -> error:: pub fn geometry_destroy(entity: Entity) -> error::Result<()> { app_mut(|app| { - app.world_mut() - .run_system_cached_with(geometry::destroy, entity) - .unwrap() + destroy::request(app.world_mut(), move |world| { + world + .run_system_cached_with(geometry::destroy, entity) + .unwrap() + }) }) } @@ -2126,9 +2133,11 @@ pub fn material_set_custom_blend( pub fn material_destroy(entity: Entity) -> error::Result<()> { app_mut(|app| { - app.world_mut() - .run_system_cached_with(material::destroy, entity) - .unwrap() + destroy::request(app.world_mut(), move |world| { + world + .run_system_cached_with(material::destroy, entity) + .unwrap() + }) }) } @@ -2590,9 +2599,11 @@ fn buffer_read_range(entity: Entity, offset: u64, len: u64) -> error::Result error::Result<()> { app_mut(|app| { - app.world_mut() - .run_system_cached_with(compute::destroy_buffer, entity) - .unwrap() + destroy::request(app.world_mut(), move |world| { + world + .run_system_cached_with(compute::destroy_buffer, entity) + .unwrap() + }) }) } diff --git a/crates/processing_render/src/light.rs b/crates/processing_render/src/light.rs index 386fdcdd..0c085d15 100644 --- a/crates/processing_render/src/light.rs +++ b/crates/processing_render/src/light.rs @@ -119,11 +119,10 @@ pub fn destroy( mut commands: Commands, lights: Query<&LightOf, With>, ) -> Result<(), ProcessingError> { - let owner = lights + lights .get(entity) .map_err(|_| ProcessingError::LightNotFound)?; - // queued `Light(entity)` commands may still reference it, so despawn at the next begin_draw - commands.entity(entity).insert(DrawTransient(owner.0)); + commands.entity(entity).despawn(); Ok(()) } @@ -254,17 +253,13 @@ mod tests { } #[test] - fn destroy_despawns_a_light_at_the_next_begin_draw() { + fn destroy_despawns_a_light() { let mut world = World::new(); let graphics = world.spawn(RenderState::new()).id(); let light = create_light(&mut world, graphics, DIRECTIONAL); let result = world.run_system_once_with(destroy, light).unwrap(); assert!(result.is_ok()); - assert!(world.get_entity(light).is_ok()); - world - .run_system_once_with(begin_draw, graphics) - .unwrap() - .unwrap(); + world.flush(); assert!(world.get_entity(light).is_err()); } diff --git a/crates/processing_render/src/particles/mod.rs b/crates/processing_render/src/particles/mod.rs index 87e1c093..99ccfc3b 100644 --- a/crates/processing_render/src/particles/mod.rs +++ b/crates/processing_render/src/particles/mod.rs @@ -554,9 +554,9 @@ pub fn particles_create_from_geometry( pub fn particles_destroy(entity: Entity) -> error::Result<()> { app_mut(|app| { - app.world_mut() - .run_system_cached_with(destroy, entity) - .unwrap() + crate::destroy::request(app.world_mut(), move |world| { + world.run_system_cached_with(destroy, entity).unwrap() + }) }) }