Skip to content

Commit 4e8ecca

Browse files
committed
Reverts max_mip_dimension to control the short side.
1 parent 13ef894 commit 4e8ecca

File tree

5 files changed

+30
-25
lines changed

5 files changed

+30
-25
lines changed

crates/bevy_post_process/src/bloom/bloom.wgsl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,35 +52,35 @@ fn karis_average(color: vec3<f32>) -> f32 {
5252
// https://www.shadertoy.com/view/mdsyDf
5353
#ifdef FAST_BLUR
5454
fn bloom_down_kernel4(uv: vec2<f32>) -> vec3<f32> {
55-
let ps = uniforms.scale / vec2<f32>(textureDimensions(input_texture));
56-
let o = 0.5 + 1.0 / 4.0;
55+
let ps = uniforms.scale / vec2<f32>(textureDimensions(input_texture));
56+
let o = 0.5 + 1.0 / 4.0;
5757

58-
let a = textureSample(input_texture, s, uv + vec2<f32>(-1.0, -1.0) * o * ps).rgb * 0.25;
59-
let b = textureSample(input_texture, s, uv + vec2<f32>(1.0, -1.0) * o * ps).rgb * 0.25;
60-
let c = textureSample(input_texture, s, uv + vec2<f32>(-1.0, 1.0) * o * ps).rgb * 0.25;
61-
let d = textureSample(input_texture, s, uv + vec2<f32>(1.0, 1.0) * o * ps).rgb * 0.25;
58+
let a = textureSample(input_texture, s, uv + vec2<f32>(-1.0, -1.0) * o * ps).rgb * 0.25;
59+
let b = textureSample(input_texture, s, uv + vec2<f32>(1.0, -1.0) * o * ps).rgb * 0.25;
60+
let c = textureSample(input_texture, s, uv + vec2<f32>(-1.0, 1.0) * o * ps).rgb * 0.25;
61+
let d = textureSample(input_texture, s, uv + vec2<f32>(1.0, 1.0) * o * ps).rgb * 0.25;
6262

6363
#ifdef FIRST_DOWNSAMPLE
64-
return (a + b + c + d) * karis_average(a + b + c + d);
64+
return (a + b + c + d) * karis_average(a + b + c + d);
6565
#else
66-
return a + b + c + d;
66+
return a + b + c + d;
6767
#endif
6868
}
6969

7070
fn bloom_up_kernel4(uv: vec2<f32>) -> vec3<f32> {
7171
// Modified version of BloomUpKernel4B https://www.shadertoy.com/view/mdsyDf. I couldn't get a good result with the original version.
72-
let ps = uniforms.scale / vec2<f32>(textureDimensions(input_texture));
72+
let ps = uniforms.scale / vec2<f32>(textureDimensions(input_texture));
7373
let w = vec4<f32>(0.211029, 0.288971, 0.288971, 0.211029);
7474
// Add a small offset for better radial symmetry.
7575
let l00 = vec2<f32>(0.347209, 0.526425) + 0.1;
7676
let l10 = vec2<f32>(0.109840, 0.334045) + 0.1;
7777
let l01 = vec2<f32>(0.334045, 0.109840) + 0.1;
7878
let l11 = vec2<f32>(0.526425, 0.347209) + 0.1;
7979

80-
let a = textureSample(input_texture, s, uv + (vec2<f32>( -0.5, -1.5) + l00) * ps).rgb * w.x;
81-
let b = textureSample(input_texture, s, uv + (vec2<f32>(0.5, -0.5) + l10) * ps).rgb * w.y;
82-
let c = textureSample(input_texture, s, uv + (vec2<f32>( -0.5, 0.5) + l01) * ps).rgb * w.z;
83-
let d = textureSample(input_texture, s, uv + (vec2<f32>( -1.5,-0.5) + l11) * ps).rgb * w.w;
80+
let a = textureSample(input_texture, s, uv + (vec2<f32>(-0.5, -1.5) + l00) * ps).rgb * w.x;
81+
let b = textureSample(input_texture, s, uv + (vec2<f32>(0.5, -0.5) + l10) * ps).rgb * w.y;
82+
let c = textureSample(input_texture, s, uv + (vec2<f32>(-0.5, 0.5) + l01) * ps).rgb * w.z;
83+
let d = textureSample(input_texture, s, uv + (vec2<f32>(-1.5, -0.5) + l11) * ps).rgb * w.w;
8484

8585
return a + b + c + d;
8686
}
@@ -199,7 +199,7 @@ fn sample_input_3x3_tent(uv: vec2<f32>) -> vec3<f32> {
199199
fn downsample_first(@location(0) output_uv: vec2<f32>) -> @location(0) vec4<f32> {
200200
let sample_uv = uniforms.viewport.xy + output_uv * uniforms.viewport.zw;
201201
#ifdef FAST_BLUR
202-
var sample = bloom_down_kernel4(sample_uv);
202+
var sample = bloom_down_kernel4(sample_uv);
203203
#else
204204
var sample = sample_input_13_tap(sample_uv);
205205
#endif
@@ -220,7 +220,7 @@ fn downsample_first(@location(0) output_uv: vec2<f32>) -> @location(0) vec4<f32>
220220
@fragment
221221
fn downsample(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
222222
#ifdef FAST_BLUR
223-
return vec4<f32>(bloom_down_kernel4(uv), 1.0);
223+
return vec4<f32>(bloom_down_kernel4(uv), 1.0);
224224
#else
225225
return vec4<f32>(sample_input_13_tap(uv), 1.0);
226226
#endif
@@ -229,7 +229,7 @@ fn downsample(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
229229
@fragment
230230
fn upsample(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
231231
#ifdef FAST_BLUR
232-
return vec4<f32>(bloom_up_kernel4(uv), 1.0);
232+
return vec4<f32>(bloom_up_kernel4(uv), 1.0);
233233
#else
234234
return vec4<f32>(sample_input_3x3_tent(uv), 1.0);
235235
#endif

crates/bevy_post_process/src/bloom/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,9 @@ fn prepare_bloom_textures(
366366
.ilog2()
367367
.clamp(2, bloom.max_mip_count)
368368
- 1;
369+
// To better predict the mip count, `max_mip_dimension` actually controls the maximum short-side size of the bloom texture.
369370
let mip_dim_ratio = if viewport.y != 0 && viewport.x != 0 {
370-
(bloom.max_mip_dimension as f32 / viewport.as_vec2()).max_element()
371+
(bloom.max_mip_dimension as f32 / viewport.as_vec2()).min_element()
371372
} else {
372373
0.
373374
};

crates/bevy_post_process/src/bloom/settings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub struct Bloom {
115115
/// otherwise set to [`BloomCompositeMode::EnergyConserving`].
116116
pub composite_mode: BloomCompositeMode,
117117

118-
/// Maximum size of each dimension for the largest mipchain texture used in downscaling/upscaling.
118+
/// Maximum size of the short side for the largest mipchain texture used in downscaling/upscaling.
119119
/// Lower values can improve performance but result in more aliasing.
120120
pub max_mip_dimension: u32,
121121

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: Add performance options to Bloom.
3+
authors: ["@beicause"]
4+
pull_requests: [21340]
5+
---
6+
7+
Bloom is a relatively expensive post-processing for low-end devices, as it requires multiple render passes for downsampling and upsampling. For more performance configurability, we added the `high_quality` (default: true) and `max_mip_count` (default: unlimited) options to Bloom, in addition to the existing `max_mip_dimension`.
8+
9+
If `high_quality` is false, Bloom will use a faster but lower quality implementation, which significantly reduces texture sampling but still maintains reasonable visual quality. For low-end devices, this could potentially reduce frame time by a few milliseconds.
10+
11+
You can also set `max_mip_count` and/or `max_mip_dimension` to a lower value for a significant performance gain. By default the bloom texture has a maximum short-side size of 512 and uses all 8 mipmaps. You may be able to cut the Bloom frame time in half by reducing the mipmap count to a smaller value (such as 3 or 4). However, please note that these two options impact the bloom quality and need to be balanced for your needs.

release-content/release-notes/faster_bloom.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)