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

Reorder render sets, refactor bevy_sprite to take advantage #9236

Merged
merged 36 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e62be96
Reorder render sets, prepare now occurs after sort
james-j-obrien May 19, 2023
3ccb838
Cleanup after rebase
james-j-obrien Jul 21, 2023
1732ab0
Refactor bevy_sprite to remove double sorting
james-j-obrien May 19, 2023
cb66efe
Refactor prepare_sprites to be clearer and more idiomatic
james-j-obrien Jul 21, 2023
6c575d6
Remove duplicated data in SpriteBatch
james-j-obrien Jul 21, 2023
8be84b7
Satisfy clippy
james-j-obrien Jul 21, 2023
2f59700
Refactor UI rendering enough to restore functionality
james-j-obrien Jul 21, 2023
4b00b96
Satisfy clippy
james-j-obrien Jul 21, 2023
1cbaff0
Add additional check in queue_sprites to not queue sprites whose text…
james-j-obrien Jul 21, 2023
ec0e7a7
Fix bevy_ui and improve bevy_sprite
james-j-obrien Aug 4, 2023
4a8c4ce
Fix batching logic when clipping ui nodes
james-j-obrien Aug 4, 2023
6408611
Fix formatting
james-j-obrien Aug 4, 2023
31de780
Add back the elusive render_range
james-j-obrien Aug 4, 2023
40f7508
Fix 3d meshes
james-j-obrien Aug 4, 2023
62ba9a6
Incorporate feedback, swap back to batch_size
james-j-obrien Aug 11, 2023
cc61c73
Fix mesh2d_manual example
james-j-obrien Aug 11, 2023
71d8127
Remove all references to per_object_binding_dynamic_offset, move GpuC…
james-j-obrien Aug 13, 2023
3acf3d0
Implement prepare_mesh_uniform_buffer to build MeshUniform buffer in …
james-j-obrien Aug 13, 2023
d7b9d15
Merge main
james-j-obrien Aug 13, 2023
c0de20d
Fix example
james-j-obrien Aug 13, 2023
a669dc1
Fix docs
james-j-obrien Aug 13, 2023
18ac3dd
Remmove per_object_binding_dynamic_offset from Shadow PhaseItems
james-j-obrien Aug 14, 2023
7e08a37
Moving various systems to account for the new ordering
james-j-obrien Aug 14, 2023
7da82e9
Fix shadows
james-j-obrien Aug 14, 2023
077c618
Satisfy clippy
james-j-obrien Aug 14, 2023
2d8bfee
Move prepare_taa_history_textures for consistency
james-j-obrien Aug 15, 2023
b7a7580
Merge branch 'main' into sprite-refactor
james-j-obrien Aug 15, 2023
ff2d08e
update order
robtfm Aug 22, 2023
6cfac8d
Merge pull request #1 from robtfm/render-reorder
james-j-obrien Aug 22, 2023
5f4984d
Rename PrepareBuffers to PrepareResources
james-j-obrien Aug 22, 2023
436d9b1
Make use of seen FixedBitSet
james-j-obrien Aug 22, 2023
d6fc11a
Improve docs
james-j-obrien Aug 22, 2023
8e71d83
Satiate clippy
james-j-obrien Aug 22, 2023
8691ed4
Add additional constraint to PrepareAssets, remove WindowSystem::Prepare
james-j-obrien Aug 22, 2023
3763790
Merge branch 'main' into sprite-refactor
superdump Aug 27, 2023
e8b5ea3
Fix formatting
superdump Aug 27, 2023
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
2 changes: 1 addition & 1 deletion assets/shaders/instancing.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn vertex(vertex: Vertex) -> VertexOutput {
// index in the Mesh array. This index could be passed in via another
// uniform instead but it's unnecessary for the example.
out.clip_position = mesh_position_local_to_clip(
get_model_matrix(0),
get_model_matrix(0u),
vec4<f32>(position, 1.0)
);
out.color = vertex.i_color;
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_core_pipeline/src/bloom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ impl Plugin for BloomPlugin {
.add_systems(
Render,
(
prepare_bloom_textures.in_set(RenderSet::Prepare),
prepare_downsampling_pipeline.in_set(RenderSet::Prepare),
prepare_upsampling_pipeline.in_set(RenderSet::Prepare),
queue_bloom_bind_groups.in_set(RenderSet::Queue),
prepare_bloom_textures.in_set(RenderSet::PrepareResources),
prepare_bloom_bind_groups.in_set(RenderSet::PrepareBindGroups),
),
)
// Add bloom to the 3d render graph
Expand Down Expand Up @@ -400,7 +400,7 @@ struct BloomBindGroups {
sampler: Sampler,
}

fn queue_bloom_bind_groups(
fn prepare_bloom_bind_groups(
mut commands: Commands,
render_device: Res<RenderDevice>,
downsampling_pipeline: Res<BloomDownsamplingPipeline>,
Expand Down
30 changes: 9 additions & 21 deletions crates/bevy_core_pipeline/src/core_2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ use bevy_render::{
extract_component::ExtractComponentPlugin,
render_graph::{EmptyNode, RenderGraphApp, ViewNodeRunner},
render_phase::{
batch_phase_system, sort_phase_system, BatchedPhaseItem, CachedRenderPipelinePhaseItem,
DrawFunctionId, DrawFunctions, PhaseItem, RenderPhase,
sort_phase_system, CachedRenderPipelinePhaseItem, DrawFunctionId, DrawFunctions, PhaseItem,
RenderPhase,
},
render_resource::CachedRenderPipelineId,
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_utils::FloatOrd;
use std::ops::Range;

use crate::{tonemapping::TonemappingNode, upscaling::UpscalingNode};

Expand All @@ -57,12 +56,7 @@ impl Plugin for Core2dPlugin {
.add_systems(ExtractSchedule, extract_core_2d_camera_phases)
.add_systems(
Render,
(
sort_phase_system::<Transparent2d>.in_set(RenderSet::PhaseSort),
batch_phase_system::<Transparent2d>
.after(sort_phase_system::<Transparent2d>)
.in_set(RenderSet::PhaseSort),
),
sort_phase_system::<Transparent2d>.in_set(RenderSet::PhaseSort),
);

use graph::node::*;
Expand All @@ -89,8 +83,7 @@ pub struct Transparent2d {
pub entity: Entity,
pub pipeline: CachedRenderPipelineId,
pub draw_function: DrawFunctionId,
/// Range in the vertex buffer of this item
pub batch_range: Option<Range<u32>>,
pub batch_size: usize,
}

impl PhaseItem for Transparent2d {
Expand All @@ -115,6 +108,11 @@ impl PhaseItem for Transparent2d {
fn sort(items: &mut [Self]) {
items.sort_by_key(|item| item.sort_key());
}

#[inline]
fn batch_size(&self) -> usize {
self.batch_size
}
}

impl CachedRenderPipelinePhaseItem for Transparent2d {
Expand All @@ -124,16 +122,6 @@ impl CachedRenderPipelinePhaseItem for Transparent2d {
}
}

impl BatchedPhaseItem for Transparent2d {
fn batch_range(&self) -> &Option<Range<u32>> {
&self.batch_range
}

fn batch_range_mut(&mut self) -> &mut Option<Range<u32>> {
&mut self.batch_range
}
}

pub fn extract_core_2d_camera_phases(
mut commands: Commands,
cameras_2d: Extract<Query<(Entity, &Camera), With<Camera2d>>>,
Expand Down
56 changes: 26 additions & 30 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,13 @@ impl Plugin for Core3dPlugin {
.add_systems(
Render,
(
prepare_core_3d_depth_textures
.in_set(RenderSet::Prepare)
.after(bevy_render::view::prepare_windows),
prepare_prepass_textures
.in_set(RenderSet::Prepare)
.after(bevy_render::view::prepare_windows),
sort_phase_system::<Opaque3d>.in_set(RenderSet::PhaseSort),
sort_phase_system::<AlphaMask3d>.in_set(RenderSet::PhaseSort),
sort_phase_system::<Transparent3d>.in_set(RenderSet::PhaseSort),
sort_phase_system::<Opaque3dPrepass>.in_set(RenderSet::PhaseSort),
sort_phase_system::<AlphaMask3dPrepass>.in_set(RenderSet::PhaseSort),
prepare_core_3d_depth_textures.in_set(RenderSet::PrepareResources),
prepare_prepass_textures.in_set(RenderSet::PrepareResources),
),
);

Expand Down Expand Up @@ -136,18 +132,15 @@ impl Plugin for Core3dPlugin {

pub struct Opaque3d {
pub distance: f32,
// Per-object data may be bound at different dynamic offsets within a buffer. If it is, then
// each batch of per-object data starts at the same dynamic offset.
pub per_object_binding_dynamic_offset: u32,
pub pipeline: CachedRenderPipelineId,
pub entity: Entity,
pub draw_function: DrawFunctionId,
pub batch_size: usize,
}

impl PhaseItem for Opaque3d {
// NOTE: (dynamic offset, -distance)
// NOTE: Values increase towards the camera. Front-to-back ordering for opaque means we need a descending sort.
type SortKey = (u32, Reverse<FloatOrd>);
type SortKey = Reverse<FloatOrd>;

#[inline]
fn entity(&self) -> Entity {
Expand All @@ -156,10 +149,7 @@ impl PhaseItem for Opaque3d {

#[inline]
fn sort_key(&self) -> Self::SortKey {
(
self.per_object_binding_dynamic_offset,
Reverse(FloatOrd(self.distance)),
)
Reverse(FloatOrd(self.distance))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we sort by pipeline here? Unrelated to this PR really, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do that separately. :)

}

#[inline]
Expand All @@ -170,9 +160,12 @@ impl PhaseItem for Opaque3d {
#[inline]
fn sort(items: &mut [Self]) {
// Key negated to match reversed SortKey ordering
radsort::sort_by_key(items, |item| {
(item.per_object_binding_dynamic_offset, -item.distance)
});
radsort::sort_by_key(items, |item| -item.distance);
}

#[inline]
fn batch_size(&self) -> usize {
self.batch_size
}
}

Expand All @@ -185,18 +178,15 @@ impl CachedRenderPipelinePhaseItem for Opaque3d {

pub struct AlphaMask3d {
pub distance: f32,
// Per-object data may be bound at different dynamic offsets within a buffer. If it is, then
// each batch of per-object data starts at the same dynamic offset.
pub per_object_binding_dynamic_offset: u32,
pub pipeline: CachedRenderPipelineId,
pub entity: Entity,
pub draw_function: DrawFunctionId,
pub batch_size: usize,
}

impl PhaseItem for AlphaMask3d {
// NOTE: (dynamic offset, -distance)
// NOTE: Values increase towards the camera. Front-to-back ordering for alpha mask means we need a descending sort.
type SortKey = (u32, Reverse<FloatOrd>);
type SortKey = Reverse<FloatOrd>;

#[inline]
fn entity(&self) -> Entity {
Expand All @@ -205,10 +195,7 @@ impl PhaseItem for AlphaMask3d {

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

#[inline]
Expand All @@ -219,9 +206,12 @@ impl PhaseItem for AlphaMask3d {
#[inline]
fn sort(items: &mut [Self]) {
// Key negated to match reversed SortKey ordering
radsort::sort_by_key(items, |item| {
(item.per_object_binding_dynamic_offset, -item.distance)
});
radsort::sort_by_key(items, |item| -item.distance);
}

#[inline]
fn batch_size(&self) -> usize {
self.batch_size
}
}

Expand All @@ -237,6 +227,7 @@ pub struct Transparent3d {
pub pipeline: CachedRenderPipelineId,
pub entity: Entity,
pub draw_function: DrawFunctionId,
pub batch_size: usize,
}

impl PhaseItem for Transparent3d {
Expand All @@ -262,6 +253,11 @@ impl PhaseItem for Transparent3d {
fn sort(items: &mut [Self]) {
radsort::sort_by_key(items, |item| item.distance);
}

#[inline]
fn batch_size(&self) -> usize {
self.batch_size
}
}

impl CachedRenderPipelinePhaseItem for Transparent3d {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/msaa_writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Plugin for MsaaWritebackPlugin {

render_app.add_systems(
Render,
queue_msaa_writeback_pipelines.in_set(RenderSet::Queue),
prepare_msaa_writeback_pipelines.in_set(RenderSet::Prepare),
);
{
use core_2d::graph::node::*;
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Node for MsaaWritebackNode {
#[derive(Component)]
pub struct MsaaWritebackBlitPipeline(CachedRenderPipelineId);

fn queue_msaa_writeback_pipelines(
fn prepare_msaa_writeback_pipelines(
mut commands: Commands,
pipeline_cache: Res<PipelineCache>,
mut pipelines: ResMut<SpecializedRenderPipelines<BlitPipeline>>,
Expand Down
30 changes: 6 additions & 24 deletions crates/bevy_core_pipeline/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,14 @@ pub struct ViewPrepassTextures {
/// Used to render all 3D meshes with materials that have no transparency.
pub struct Opaque3dPrepass {
pub distance: f32,
// Per-object data may be bound at different dynamic offsets within a buffer. If it is, then
// each batch of per-object data starts at the same dynamic offset.
pub per_object_binding_dynamic_offset: u32,
pub entity: Entity,
pub pipeline_id: CachedRenderPipelineId,
pub draw_function: DrawFunctionId,
}

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

#[inline]
fn entity(&self) -> Entity {
Expand All @@ -100,10 +96,7 @@ impl PhaseItem for Opaque3dPrepass {

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

#[inline]
Expand All @@ -114,9 +107,7 @@ impl PhaseItem for Opaque3dPrepass {
#[inline]
fn sort(items: &mut [Self]) {
// Key negated to match reversed SortKey ordering
radsort::sort_by_key(items, |item| {
(item.per_object_binding_dynamic_offset, -item.distance)
});
radsort::sort_by_key(items, |item| -item.distance);
}
}

Expand All @@ -134,18 +125,14 @@ impl CachedRenderPipelinePhaseItem for Opaque3dPrepass {
/// Used to render all meshes with a material with an alpha mask.
pub struct AlphaMask3dPrepass {
pub distance: f32,
// Per-object data may be bound at different dynamic offsets within a buffer. If it is, then
// each batch of per-object data starts at the same dynamic offset.
pub per_object_binding_dynamic_offset: u32,
pub entity: Entity,
pub pipeline_id: CachedRenderPipelineId,
pub draw_function: DrawFunctionId,
}

impl PhaseItem for AlphaMask3dPrepass {
// NOTE: (dynamic offset, -distance)
// NOTE: Values increase towards the camera. Front-to-back ordering for opaque means we need a descending sort.
type SortKey = (u32, Reverse<FloatOrd>);
type SortKey = Reverse<FloatOrd>;

#[inline]
fn entity(&self) -> Entity {
Expand All @@ -154,10 +141,7 @@ impl PhaseItem for AlphaMask3dPrepass {

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

#[inline]
Expand All @@ -168,9 +152,7 @@ impl PhaseItem for AlphaMask3dPrepass {
#[inline]
fn sort(items: &mut [Self]) {
// Key negated to match reversed SortKey ordering
radsort::sort_by_key(items, |item| {
(item.per_object_binding_dynamic_offset, -item.distance)
});
radsort::sort_by_key(items, |item| -item.distance);
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/skybox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Plugin for SkyboxPlugin {
Render,
(
prepare_skybox_pipelines.in_set(RenderSet::Prepare),
queue_skybox_bind_groups.in_set(RenderSet::Queue),
prepare_skybox_bind_groups.in_set(RenderSet::PrepareBindGroups),
),
);
}
Expand Down Expand Up @@ -209,7 +209,7 @@ fn prepare_skybox_pipelines(
#[derive(Component)]
pub struct SkyboxBindGroup(pub BindGroup);

fn queue_skybox_bind_groups(
fn prepare_skybox_bind_groups(
mut commands: Commands,
pipeline: Res<SkyboxPipeline>,
view_uniforms: Res<ViewUniforms>,
Expand Down
11 changes: 4 additions & 7 deletions crates/bevy_core_pipeline/src/taa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy_core::FrameCount;
use bevy_ecs::{
prelude::{Bundle, Component, Entity},
query::{QueryItem, With},
schedule::{apply_deferred, IntoSystemConfigs},
schedule::IntoSystemConfigs,
system::{Commands, Query, Res, ResMut, Resource},
world::{FromWorld, World},
};
Expand All @@ -31,7 +31,7 @@ use bevy_render::{
},
renderer::{RenderContext, RenderDevice},
texture::{BevyDefault, CachedTexture, TextureCache},
view::{prepare_view_uniforms, ExtractedView, Msaa, ViewTarget},
view::{ExtractedView, Msaa, ViewTarget},
ExtractSchedule, MainWorld, Render, RenderApp, RenderSet,
};

Expand Down Expand Up @@ -65,12 +65,9 @@ impl Plugin for TemporalAntiAliasPlugin {
.add_systems(
Render,
(
(prepare_taa_jitter_and_mip_bias, apply_deferred)
.chain()
.before(prepare_view_uniforms)
.in_set(RenderSet::Prepare),
prepare_taa_history_textures.in_set(RenderSet::Prepare),
prepare_taa_jitter_and_mip_bias.in_set(RenderSet::ManageViews),
prepare_taa_pipelines.in_set(RenderSet::Prepare),
prepare_taa_history_textures.in_set(RenderSet::PrepareResources),
),
)
.add_render_graph_node::<ViewNodeRunner<TAANode>>(CORE_3D, draw_3d_graph::node::TAA)
Expand Down
Loading
Loading