Skip to content

Commit

Permalink
Disable camera on window close (#8802)
Browse files Browse the repository at this point in the history
# Objective

- When a window is closed, the associated camera keeps rendering even if
the RenderTarget isn't valid anymore.
	- This is essentially just wasting a lot of performance.

## Solution

- Detect the window close event and disable any camera that used the
window has a RenderTarget.

## Notes

It's possible a similar thing could be done for camera that use an image
handle, but I would fix that in a separate PR.
  • Loading branch information
IceSentry authored Jun 10, 2023
1 parent c1fd505 commit 75da2e7
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions crates/bevy_render/src/camera/camera_driver_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,24 @@ impl Node for CameraDriverNode {
world: &World,
) -> Result<(), NodeRunError> {
let sorted_cameras = world.resource::<SortedCameras>();
let windows = world.resource::<ExtractedWindows>();
let mut camera_windows = HashSet::new();
for sorted_camera in &sorted_cameras.0 {
if let Ok(camera) = self.cameras.get_manual(world, sorted_camera.entity) {
if let Some(NormalizedRenderTarget::Window(window_ref)) = camera.target {
camera_windows.insert(window_ref.entity());
let Ok(camera) = self.cameras.get_manual(world, sorted_camera.entity) else {
continue;
};

let mut run_graph = true;
if let Some(NormalizedRenderTarget::Window(window_ref)) = camera.target {
let window_entity = window_ref.entity();
if windows.windows.get(&window_entity).is_some() {
camera_windows.insert(window_entity);
} else {
// The window doesn't exist anymore so we don't need to run the graph
run_graph = false;
}
}
if run_graph {
graph.run_sub_graph(
camera.render_graph.clone(),
vec![],
Expand Down

0 comments on commit 75da2e7

Please sign in to comment.