Skip to content

Commit 8f61fcf

Browse files
beicausetigregalis
authored andcommitted
Optimize prepare_bloom_bind_groups (bevyengine#21420)
# Objective Closes bevyengine#21395 ## Solution If bloom texture and uniform buffer don't change, then we skip updating the bind groups. I also tried implementing a global hash map cache similar to TextureCache but the performance is worse than the current. So I think we'd better cache bind groups locally rather than globally. Also, these tasks can be parallelized on the CPU so the performance impact may not be significant. ## Testing After this the overhead of `prepare_bloom_bind_groups` become negligible according to tracy.
1 parent bd1bcaf commit 8f61fcf

File tree

1 file changed

+17
-2
lines changed
  • crates/bevy_post_process/src/bloom

1 file changed

+17
-2
lines changed

crates/bevy_post_process/src/bloom/mod.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ fn prepare_bloom_textures(
416416

417417
#[derive(Component)]
418418
struct BloomBindGroups {
419+
cache_key: (TextureId, BufferId),
419420
downsampling_bind_groups: Box<[BindGroup]>,
420421
upsampling_bind_groups: Box<[BindGroup]>,
421422
sampler: Sampler,
@@ -426,12 +427,22 @@ fn prepare_bloom_bind_groups(
426427
render_device: Res<RenderDevice>,
427428
downsampling_pipeline: Res<BloomDownsamplingPipeline>,
428429
upsampling_pipeline: Res<BloomUpsamplingPipeline>,
429-
views: Query<(Entity, &BloomTexture)>,
430+
views: Query<(Entity, &BloomTexture, Option<&BloomBindGroups>)>,
430431
uniforms: Res<ComponentUniforms<BloomUniforms>>,
431432
) {
432433
let sampler = &downsampling_pipeline.sampler;
433434

434-
for (entity, bloom_texture) in &views {
435+
for (entity, bloom_texture, bloom_bind_groups) in &views {
436+
if let Some(b) = bloom_bind_groups
437+
&& b.cache_key
438+
== (
439+
bloom_texture.texture.texture.id(),
440+
uniforms.buffer().unwrap().id(),
441+
)
442+
{
443+
continue;
444+
}
445+
435446
let bind_group_count = bloom_texture.mip_count as usize - 1;
436447

437448
let mut downsampling_bind_groups = Vec::with_capacity(bind_group_count);
@@ -461,6 +472,10 @@ fn prepare_bloom_bind_groups(
461472
}
462473

463474
commands.entity(entity).insert(BloomBindGroups {
475+
cache_key: (
476+
bloom_texture.texture.texture.id(),
477+
uniforms.buffer().unwrap().id(),
478+
),
464479
downsampling_bind_groups: downsampling_bind_groups.into_boxed_slice(),
465480
upsampling_bind_groups: upsampling_bind_groups.into_boxed_slice(),
466481
sampler: sampler.clone(),

0 commit comments

Comments
 (0)