Skip to content

Commit bcaec79

Browse files
committed
tweak up kernel, mix ClampToBorder and ClampToEdge
1 parent 11735a2 commit bcaec79

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

crates/bevy_post_process/src/bloom/bloom.wgsl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ fn bloom_down_kernel4(uv: vec2<f32>) -> vec3<f32> {
6868
}
6969

7070
fn bloom_up_kernel4(uv: vec2<f32>) -> vec3<f32> {
71+
// Modified version of BloomUpKernel4B https://www.shadertoy.com/view/mdsyDf. I couldn't get a good result with the original version.
7172
let ps = uniforms.scale / vec2<f32>(textureDimensions(input_texture));
7273
let w = vec4<f32>(0.211029, 0.288971, 0.288971, 0.211029);
73-
let l00 = vec2<f32>(0.347209, 0.526425);
74-
let l10 = vec2<f32>(0.109840, 0.334045);
75-
let l01 = vec2<f32>(0.334045, 0.109840);
76-
let l11 = vec2<f32>(0.526425, 0.347209);
74+
// Add a small offset for better radial symmetry.
75+
let l00 = vec2<f32>(0.347209, 0.526425) + 0.1;
76+
let l10 = vec2<f32>(0.109840, 0.334045) + 0.1;
77+
let l01 = vec2<f32>(0.334045, 0.109840) + 0.1;
78+
let l11 = vec2<f32>(0.526425, 0.347209) + 0.1;
7779

7880
let a = textureSample(input_texture, s, uv + (vec2<f32>( -0.5, -1.5) + l00) * ps).rgb * w.x;
7981
let b = textureSample(input_texture, s, uv + (vec2<f32>(0.5, -0.5) + l10) * ps).rgb * w.y;

crates/bevy_post_process/src/bloom/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl ViewNode for BloomNode {
181181
&BindGroupEntries::sequential((
182182
// Read from main texture directly
183183
view_texture,
184-
&bind_groups.sampler,
184+
&bind_groups.downsample_sampler,
185185
uniforms.clone(),
186186
)),
187187
);
@@ -418,7 +418,7 @@ fn prepare_bloom_textures(
418418
struct BloomBindGroups {
419419
downsampling_bind_groups: Box<[BindGroup]>,
420420
upsampling_bind_groups: Box<[BindGroup]>,
421-
sampler: Sampler,
421+
downsample_sampler: Sampler,
422422
}
423423

424424
fn prepare_bloom_bind_groups(
@@ -429,8 +429,6 @@ fn prepare_bloom_bind_groups(
429429
views: Query<(Entity, &BloomTexture)>,
430430
uniforms: Res<ComponentUniforms<BloomUniforms>>,
431431
) {
432-
let sampler = &downsampling_pipeline.sampler;
433-
434432
for (entity, bloom_texture) in &views {
435433
let bind_group_count = bloom_texture.mip_count as usize - 1;
436434

@@ -441,7 +439,7 @@ fn prepare_bloom_bind_groups(
441439
&downsampling_pipeline.bind_group_layout,
442440
&BindGroupEntries::sequential((
443441
&bloom_texture.view(mip - 1),
444-
sampler,
442+
&downsampling_pipeline.sampler,
445443
uniforms.binding().unwrap(),
446444
)),
447445
));
@@ -454,7 +452,7 @@ fn prepare_bloom_bind_groups(
454452
&upsampling_pipeline.bind_group_layout,
455453
&BindGroupEntries::sequential((
456454
&bloom_texture.view(mip),
457-
sampler,
455+
&upsampling_pipeline.sampler,
458456
uniforms.binding().unwrap(),
459457
)),
460458
));
@@ -463,7 +461,7 @@ fn prepare_bloom_bind_groups(
463461
commands.entity(entity).insert(BloomBindGroups {
464462
downsampling_bind_groups: downsampling_bind_groups.into_boxed_slice(),
465463
upsampling_bind_groups: upsampling_bind_groups.into_boxed_slice(),
466-
sampler: sampler.clone(),
464+
downsample_sampler: downsampling_pipeline.sampler.clone(),
467465
});
468466
}
469467
}

crates/bevy_post_process/src/bloom/upsampling_pipeline.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct UpsamplingPipelineIds {
2929
#[derive(Resource)]
3030
pub struct BloomUpsamplingPipeline {
3131
pub bind_group_layout: BindGroupLayout,
32+
pub sampler: Sampler,
3233
/// The asset handle for the fullscreen vertex shader.
3334
pub fullscreen_shader: FullscreenShader,
3435
/// The fragment shader asset handle.
@@ -63,8 +64,18 @@ pub fn init_bloom_upscaling_pipeline(
6364
),
6465
);
6566

67+
// Sampler
68+
let sampler = render_device.create_sampler(&SamplerDescriptor {
69+
min_filter: FilterMode::Linear,
70+
mag_filter: FilterMode::Linear,
71+
address_mode_u: AddressMode::ClampToEdge,
72+
address_mode_v: AddressMode::ClampToEdge,
73+
..Default::default()
74+
});
75+
6676
commands.insert_resource(BloomUpsamplingPipeline {
6777
bind_group_layout,
78+
sampler,
6879
fullscreen_shader: fullscreen_shader.clone(),
6980
fragment_shader: load_embedded_asset!(asset_server.as_ref(), "bloom.wgsl"),
7081
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: Add a faster bloom implementation.
3+
authors: ["@beicause"]
4+
pull_requests: [21340]
5+
---
6+
7+
Bloom now has a `high_quality` (default: true) option to control whether to use a high quality implementation, or a faster but lower quality implementation. The lower quality bloom still maintains reasonable visual quality while significantly reducing texture sampling. For low-end devices, this could potentially reduce frame time by a few milliseconds.

0 commit comments

Comments
 (0)