diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index 6e6783caf676fc..cdf1e7ab1548e7 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -26,7 +26,6 @@ use bevy_render::{ Extract, }; use bevy_transform::{components::GlobalTransform, prelude::Transform}; -use bevy_utils::FloatOrd; use bevy_utils::{ tracing::{error, warn}, HashMap, @@ -1653,7 +1652,7 @@ pub struct Shadow { } impl PhaseItem for Shadow { - type SortKey = FloatOrd; + type SortKey = usize; #[inline] fn entity(&self) -> Entity { @@ -1662,7 +1661,7 @@ impl PhaseItem for Shadow { #[inline] fn sort_key(&self) -> Self::SortKey { - FloatOrd(self.distance) + self.pipeline.id() } #[inline] @@ -1672,7 +1671,10 @@ impl PhaseItem for Shadow { #[inline] fn sort(items: &mut [Self]) { - radsort::sort_by_key(items, |item| item.distance); + // The shadow phase is sorted by pipeline id for performance reasons. + // Grouping all draw commands using the same pipeline together performs + // better than rebinding everything at a high rate. + radsort::sort_by_key(items, |item| item.pipeline.id()); } } diff --git a/crates/bevy_render/src/render_resource/pipeline_cache.rs b/crates/bevy_render/src/render_resource/pipeline_cache.rs index ddc31c77661fb7..6413ff834f06a2 100644 --- a/crates/bevy_render/src/render_resource/pipeline_cache.rs +++ b/crates/bevy_render/src/render_resource/pipeline_cache.rs @@ -56,6 +56,11 @@ pub struct CachedRenderPipelineId(CachedPipelineId); impl CachedRenderPipelineId { /// An invalid cached render pipeline index, often used to initialize a variable. pub const INVALID: Self = CachedRenderPipelineId(usize::MAX); + + #[inline] + pub fn id(&self) -> usize { + self.0 + } } /// Index of a cached compute pipeline in a [`PipelineCache`]. @@ -65,6 +70,11 @@ pub struct CachedComputePipelineId(CachedPipelineId); impl CachedComputePipelineId { /// An invalid cached compute pipeline index, often used to initialize a variable. pub const INVALID: Self = CachedComputePipelineId(usize::MAX); + + #[inline] + pub fn id(&self) -> usize { + self.0 + } } pub struct CachedPipeline {