Skip to content

Commit

Permalink
bevy_core_pipeline: Fix prepass sort orders (#7539)
Browse files Browse the repository at this point in the history
# Objective

- Prepass opaque and alpha mask are incorrectly sorted back to front. This slipped through review by accident.

## Solution

- Sort prepass opaque and alpha mask front to back
  • Loading branch information
superdump committed Feb 7, 2023
1 parent f3a23fd commit 6d399bf
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions crates/bevy_core_pipeline/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
pub mod node;

use std::cmp::Reverse;

use bevy_ecs::prelude::*;
use bevy_reflect::Reflect;
use bevy_render::{
Expand Down Expand Up @@ -75,15 +77,17 @@ pub struct Opaque3dPrepass {
}

impl PhaseItem for Opaque3dPrepass {
type SortKey = FloatOrd;
// NOTE: Values increase towards the camera. Front-to-back ordering for opaque means we need a descending sort.
type SortKey = Reverse<FloatOrd>;

#[inline]
fn entity(&self) -> Entity {
self.entity
}

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

#[inline]
Expand All @@ -93,7 +97,8 @@ impl PhaseItem for Opaque3dPrepass {

#[inline]
fn sort(items: &mut [Self]) {
radsort::sort_by_key(items, |item| item.distance);
// Key negated to match reversed SortKey ordering
radsort::sort_by_key(items, |item| -item.distance);
}
}

Expand All @@ -117,15 +122,17 @@ pub struct AlphaMask3dPrepass {
}

impl PhaseItem for AlphaMask3dPrepass {
type SortKey = FloatOrd;
// NOTE: Values increase towards the camera. Front-to-back ordering for alpha mask means we need a descending sort.
type SortKey = Reverse<FloatOrd>;

#[inline]
fn entity(&self) -> Entity {
self.entity
}

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

#[inline]
Expand All @@ -135,7 +142,8 @@ impl PhaseItem for AlphaMask3dPrepass {

#[inline]
fn sort(items: &mut [Self]) {
radsort::sort_by_key(items, |item| item.distance);
// Key negated to match reversed SortKey ordering
radsort::sort_by_key(items, |item| -item.distance);
}
}

Expand Down

0 comments on commit 6d399bf

Please sign in to comment.