From a81c8f97448714f968eead7b159d9b3301b178a6 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 9 Dec 2024 19:31:17 -0800 Subject: [PATCH] Don't unconditionally create temporary render entities for visible objects. (#16723) PR #15756 made us create temporary render entities for all visible objects, even if they had no render world counterpart. This regressed our `many_cubes` time from about 3.59 ms/frame to 4.66 ms/frame. This commit changes that behavior to use `Entity::PLACEHOLDER` instead of creating a temporary render entity. This improves our `many_cubes` time from 5.66 ms/frame to 3.96 ms/frame, a 43% speedup. I tested 3D, 2D gizmos, and UI and they seem to work. See the following graph of `many_cubes` frame time (lower is better). PR #15756 is the one in October. ![Time (ms_frame) vs Date(3)](https://github.com/user-attachments/assets/2c31a893-97bd-40f6-9e89-d2195a44cf40) --- crates/bevy_pbr/src/render/light.rs | 13 +++++-------- crates/bevy_render/src/camera/camera.rs | 6 ++---- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index 57ed8f2686..fe14688a24 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -31,7 +31,7 @@ use bevy_render::{ }; use bevy_render::{ mesh::allocator::SlabId, - sync_world::{MainEntity, RenderEntity, TemporaryRenderEntity}, + sync_world::{MainEntity, RenderEntity}, }; use bevy_transform::{components::GlobalTransform, prelude::Transform}; #[cfg(feature = "trace")] @@ -286,7 +286,7 @@ pub fn extract_lights( let render_cubemap_visible_entities = RenderCubemapVisibleEntities { data: cubemap_visible_entities .iter() - .map(|v| create_render_visible_mesh_entities(&mut commands, &mapper, v)) + .map(|v| create_render_visible_mesh_entities(&mapper, v)) .collect::>() .try_into() .unwrap(), @@ -343,7 +343,7 @@ pub fn extract_lights( continue; } let render_visible_entities = - create_render_visible_mesh_entities(&mut commands, &mapper, visible_entities); + create_render_visible_mesh_entities(&mapper, visible_entities); let texel_size = 2.0 * ops::tan(spot_light.outer_angle) / directional_light_shadow_map.size as f32; @@ -430,7 +430,7 @@ pub fn extract_lights( cascade_visible_entities.insert( entity, v.iter() - .map(|v| create_render_visible_mesh_entities(&mut commands, &mapper, v)) + .map(|v| create_render_visible_mesh_entities(&mapper, v)) .collect(), ); } else { @@ -469,7 +469,6 @@ pub fn extract_lights( } fn create_render_visible_mesh_entities( - commands: &mut Commands, mapper: &Extract>, visible_entities: &VisibleMeshEntities, ) -> RenderVisibleMeshEntities { @@ -477,9 +476,7 @@ fn create_render_visible_mesh_entities( entities: visible_entities .iter() .map(|e| { - let render_entity = mapper - .get(*e) - .unwrap_or_else(|_| commands.spawn(TemporaryRenderEntity).id()); + let render_entity = mapper.get(*e).unwrap_or(Entity::PLACEHOLDER); (render_entity, MainEntity::from(*e)) }) .collect(), diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index f0603157fb..1221c66a79 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -6,7 +6,7 @@ use crate::{ render_asset::RenderAssets, render_graph::{InternedRenderSubGraph, RenderSubGraph}, render_resource::TextureView, - sync_world::{RenderEntity, SyncToRenderWorld, TemporaryRenderEntity}, + sync_world::{RenderEntity, SyncToRenderWorld}, texture::GpuImage, view::{ ColorGrading, ExtractedView, ExtractedWindows, GpuCulling, Msaa, RenderLayers, @@ -1098,9 +1098,7 @@ pub fn extract_cameras( .get(*entity) .cloned() .map(|entity| entity.id()) - .unwrap_or_else(|_e| { - commands.spawn(TemporaryRenderEntity).id() - }); + .unwrap_or(Entity::PLACEHOLDER); (render_entity, (*entity).into()) }) .collect();