Skip to content
Merged
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
94 changes: 94 additions & 0 deletions crates/processing_render/src/destroy.rs
Original file line number Diff line number Diff line change
@@ -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<dyn FnOnce(&mut World) -> Result<()> + Send + Sync>;

/// Destroys waiting for the queued draw commands to run.
#[derive(Resource, Default)]
pub struct PendingDestroys(Vec<Destroy>);

/// 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::<PendingDestroys>() || commands_queued(world) {
return;
}
let pending = std::mem::take(&mut world.resource_mut::<PendingDestroys>().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::<CommandBuffer>(graphics).unwrap().clear();
run_pending(&mut world);
assert!(world.get_entity(thing).is_err());
}
}
1 change: 1 addition & 0 deletions crates/processing_render/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ pub fn flush(app: &mut App, entity: Entity) -> Result<()> {
}
}
graphics_mut!(app, entity).remove::<Flush>();
crate::destroy::run_pending(app.world_mut());
Ok(())
}

Expand Down
41 changes: 26 additions & 15 deletions crates/processing_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
})
})
}

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

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

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

Expand Down Expand Up @@ -2590,9 +2599,11 @@ fn buffer_read_range(entity: Entity, offset: u64, len: u64) -> error::Result<Vec

pub fn buffer_destroy(entity: Entity) -> 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()
})
})
}

Expand Down
13 changes: 4 additions & 9 deletions crates/processing_render/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,10 @@ pub fn destroy(
mut commands: Commands,
lights: Query<&LightOf, With<ProcessingLight>>,
) -> 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(())
}

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

Expand Down
6 changes: 3 additions & 3 deletions crates/processing_render/src/particles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
}

Expand Down
Loading