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] - Allow unbatched render phases to use unstable sorts #5049

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions crates/bevy_core_pipeline/src/core_2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use bevy_render::{
render_phase::{
batch_phase_system, sort_phase_system, BatchedPhaseItem, CachedRenderPipelinePhaseItem,
DrawFunctionId, DrawFunctions, EntityPhaseItem, PhaseItem, RenderPhase,
RenderPhaseSortMode,
},
render_resource::CachedRenderPipelineId,
RenderApp, RenderStage,
Expand Down Expand Up @@ -90,6 +91,10 @@ impl PhaseItem for Transparent2d {
fn draw_function(&self) -> DrawFunctionId {
self.draw_function
}

fn sort_mode() -> RenderPhaseSortMode {
james7132 marked this conversation as resolved.
Show resolved Hide resolved
RenderPhaseSortMode::Stable
}
}

impl EntityPhaseItem for Transparent2d {
Expand Down
16 changes: 16 additions & 0 deletions crates/bevy_render/src/render_phase/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ pub trait Draw<P: PhaseItem>: Send + Sync + 'static {
);
}

pub enum RenderPhaseSortMode {
/// Requires a stable sort. Generally required for proper batching based on external criteria.
james7132 marked this conversation as resolved.
Show resolved Hide resolved
Stable,
/// The default: allows unstable sorting. Usually faster than Stable.
james7132 marked this conversation as resolved.
Show resolved Hide resolved
Unstable,
/// Unsorted. Omits sorting entirely.
Unsorted,
}

/// An item which will be drawn to the screen. A phase item should be queued up for rendering
/// during the [`RenderStage::Queue`](crate::RenderStage::Queue) stage.
/// Afterwards it will be sorted and rendered automatically in the
Expand All @@ -42,6 +51,13 @@ pub trait PhaseItem: Send + Sync + 'static {
fn sort_key(&self) -> Self::SortKey;
/// Specifies the [`Draw`] function used to render the item.
fn draw_function(&self) -> DrawFunctionId;

/// Specifies whether the phase requires batching. This should return true if and only if
/// the type implements [`BatchedPhaseItem`].
#[inline]
fn sort_mode() -> RenderPhaseSortMode {
RenderPhaseSortMode::Unstable
}
}

// TODO: make this generic?
Expand Down
10 changes: 9 additions & 1 deletion crates/bevy_render/src/render_phase/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ impl<I: PhaseItem> RenderPhase<I> {

/// Sorts all of its [`PhaseItems`](PhaseItem).
pub fn sort(&mut self) {
self.items.sort_by_key(|d| d.sort_key());
match I::sort_mode() {
RenderPhaseSortMode::Stable => self.items.sort_by_key(|d| d.sort_key()),
RenderPhaseSortMode::Unstable => self.items.sort_unstable_by_key(|d| d.sort_key()),
RenderPhaseSortMode::Unsorted => {}
}
}
}

Expand Down Expand Up @@ -98,6 +102,10 @@ mod tests {
fn draw_function(&self) -> DrawFunctionId {
unimplemented!();
}

fn sort_mode() -> RenderPhaseSortMode {
RenderPhaseSortMode::Stable
}
}
impl EntityPhaseItem for TestPhaseItem {
fn entity(&self) -> bevy_ecs::entity::Entity {
Expand Down