Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Fix performance regression with shadow mapping #7914

Closed
Closed
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
10 changes: 6 additions & 4 deletions crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1653,7 +1652,7 @@ pub struct Shadow {
}

impl PhaseItem for Shadow {
type SortKey = FloatOrd;
type SortKey = usize;

#[inline]
fn entity(&self) -> Entity {
Expand All @@ -1662,7 +1661,7 @@ impl PhaseItem for Shadow {

#[inline]
fn sort_key(&self) -> Self::SortKey {
FloatOrd(self.distance)
self.pipeline.id()
}

#[inline]
Expand All @@ -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());
}
}

Expand Down
10 changes: 10 additions & 0 deletions crates/bevy_render/src/render_resource/pipeline_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand All @@ -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 {
Expand Down