Skip to content

Commit

Permalink
Fix 2D BatchedInstanceBuffer clear
Browse files Browse the repository at this point in the history
  • Loading branch information
superdump committed Apr 10, 2024
1 parent 5c3ae32 commit 05cae44
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 30 deletions.
15 changes: 11 additions & 4 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use bevy_ecs::{
use bevy_math::{Affine3, Rect, UVec2, Vec3, Vec4};
use bevy_render::{
batching::{
clear_batched_instance_buffers, gpu_preprocessing, no_gpu_preprocessing, GetBatchData,
GetFullBatchData, NoAutomaticBatching,
gpu_preprocessing, no_gpu_preprocessing, GetBatchData, GetFullBatchData,
NoAutomaticBatching,
},
mesh::*,
render_asset::RenderAssets,
Expand Down Expand Up @@ -139,10 +139,14 @@ impl Plugin for MeshRenderPlugin {
.init_resource::<SkinIndices>()
.init_resource::<MorphUniform>()
.init_resource::<MorphIndices>()
.add_systems(ExtractSchedule, (extract_skins, extract_morphs))
.add_systems(
ExtractSchedule,
clear_batched_instance_buffers::<MeshPipeline>.before(ExtractMeshesSet),
(
extract_skins,
extract_morphs,
gpu_preprocessing::clear_batched_gpu_instance_buffers::<MeshPipeline>
.before(ExtractMeshesSet),
),
)
.add_systems(
Render,
Expand All @@ -151,6 +155,9 @@ impl Plugin for MeshRenderPlugin {
prepare_morphs.in_set(RenderSet::PrepareResources),
prepare_mesh_bind_group.in_set(RenderSet::PrepareBindGroups),
prepare_mesh_view_bind_groups.in_set(RenderSet::PrepareBindGroups),
no_gpu_preprocessing::clear_batched_cpu_instance_buffers::<MeshPipeline>
.in_set(RenderSet::Cleanup)
.after(RenderSet::Render),
),
);
}
Expand Down
18 changes: 18 additions & 0 deletions crates/bevy_render/src/batching/gpu_preprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ where
}
}

/// A system that runs early in extraction and clears out all the
/// [`gpu_preprocessing::BatchedInstanceBuffers`] for the frame.
///
/// We have to run this during extraction because, if GPU preprocessing is in
/// use, the extraction phase will write to the mesh input uniform buffers
/// directly, so the buffers need to be cleared before then.
pub fn clear_batched_gpu_instance_buffers<GFBD>(
gpu_batched_instance_buffers: Option<
ResMut<BatchedInstanceBuffers<GFBD::BufferData, GFBD::BufferInputData>>,
>,
) where
GFBD: GetFullBatchData,
{
if let Some(mut gpu_batched_instance_buffers) = gpu_batched_instance_buffers {
gpu_batched_instance_buffers.clear();
}
}

/// A system that removes GPU preprocessing work item buffers that correspond to
/// deleted [`ViewTarget`]s.
///
Expand Down
26 changes: 1 addition & 25 deletions crates/bevy_render/src/batching/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy_ecs::{
component::Component,
entity::Entity,
system::{Query, ResMut, SystemParam, SystemParamItem},
system::{Query, SystemParam, SystemParamItem},
};
use bytemuck::Pod;
use nonmax::NonMaxU32;
Expand Down Expand Up @@ -135,30 +135,6 @@ pub trait GetFullBatchData: GetBatchData {
) -> Option<NonMaxU32>;
}

/// A system that runs early in extraction and clears out all the
/// [`gpu_preprocessing::BatchedInstanceBuffers`] for the frame.
///
/// We have to run this during extraction because, if GPU preprocessing is in
/// use, the extraction phase will write to the mesh input uniform buffers
/// directly, so the buffers need to be cleared before then.
pub fn clear_batched_instance_buffers<GFBD>(
cpu_batched_instance_buffer: Option<
ResMut<no_gpu_preprocessing::BatchedInstanceBuffer<GFBD::BufferData>>,
>,
gpu_batched_instance_buffers: Option<
ResMut<gpu_preprocessing::BatchedInstanceBuffers<GFBD::BufferData, GFBD::BufferInputData>>,
>,
) where
GFBD: GetFullBatchData,
{
if let Some(mut cpu_batched_instance_buffer) = cpu_batched_instance_buffer {
cpu_batched_instance_buffer.clear();
}
if let Some(mut gpu_batched_instance_buffers) = gpu_batched_instance_buffers {
gpu_batched_instance_buffers.clear();
}
}

/// Sorts a render phase that uses bins.
pub fn sort_binned_render_phase<BPI>(mut views: Query<&mut BinnedRenderPhase<BPI>>)
where
Expand Down
13 changes: 13 additions & 0 deletions crates/bevy_render/src/batching/no_gpu_preprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ where
}
}

/// A system that clears out the [`BatchedInstanceBuffer`] for the frame.
///
/// This needs to run before the CPU batched instance buffers are used.
pub fn clear_batched_cpu_instance_buffers<GBD>(
cpu_batched_instance_buffer: Option<ResMut<BatchedInstanceBuffer<GBD::BufferData>>>,
) where
GBD: GetBatchData,
{
if let Some(mut cpu_batched_instance_buffer) = cpu_batched_instance_buffer {
cpu_batched_instance_buffer.clear();
}
}

/// Batch the items in a sorted render phase, when GPU instance buffer building
/// isn't in use. This means comparing metadata needed to draw each phase item
/// and trying to combine the draws into a batch.
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_sprite/src/mesh2d/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use bevy_ecs::{
use bevy_math::{Affine3, Vec4};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::batching::no_gpu_preprocessing::{
batch_and_prepare_sorted_render_phase, write_batched_instance_buffer, BatchedInstanceBuffer,
self, batch_and_prepare_sorted_render_phase, write_batched_instance_buffer,
BatchedInstanceBuffer,
};
use bevy_render::mesh::{GpuMesh, MeshVertexBufferLayoutRef};
use bevy_render::{
Expand Down Expand Up @@ -107,6 +108,9 @@ impl Plugin for Mesh2dRenderPlugin {
.in_set(RenderSet::PrepareResourcesFlush),
prepare_mesh2d_bind_group.in_set(RenderSet::PrepareBindGroups),
prepare_mesh2d_view_bind_groups.in_set(RenderSet::PrepareBindGroups),
no_gpu_preprocessing::clear_batched_cpu_instance_buffers::<Mesh2dPipeline>
.in_set(RenderSet::Cleanup)
.after(RenderSet::Render),
),
);
}
Expand Down

0 comments on commit 05cae44

Please sign in to comment.