Skip to content

Commit 45dfa71

Browse files
committed
fix bloom viewport (#6802)
# Objective fix bloom when used on a camera with a viewport specified ## Solution - pass viewport into the prefilter shader, and use it to read from the correct section of the original rendered screen - don't apply viewport for the intermediate bloom passes, only for the final blend output
1 parent 1cc663f commit 45dfa71

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

crates/bevy_core_pipeline/src/bloom/bloom.wgsl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ struct BloomUniforms {
55
knee: f32,
66
scale: f32,
77
intensity: f32,
8+
viewport: vec4<f32>,
89
};
910

1011
@group(0) @binding(0)
@@ -87,7 +88,8 @@ fn sample_original_3x3_tent(uv: vec2<f32>, scale: vec2<f32>) -> vec4<f32> {
8788
}
8889

8990
@fragment
90-
fn downsample_prefilter(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
91+
fn downsample_prefilter(@location(0) output_uv: vec2<f32>) -> @location(0) vec4<f32> {
92+
let sample_uv = uniforms.viewport.xy + output_uv * uniforms.viewport.zw;
9193
let texel_size = 1.0 / vec2<f32>(textureDimensions(original));
9294

9395
let scale = texel_size;
@@ -98,7 +100,7 @@ fn downsample_prefilter(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
98100
0.25 / uniforms.knee,
99101
);
100102

101-
var o: vec4<f32> = sample_13_tap(uv, scale);
103+
var o: vec4<f32> = sample_13_tap(sample_uv, scale);
102104

103105
o = quadratic_threshold(o, uniforms.threshold, curve);
104106
o = max(o, vec4<f32>(0.00001));

crates/bevy_core_pipeline/src/bloom/mod.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bevy_ecs::{
77
system::{Commands, Query, Res, ResMut, Resource},
88
world::{FromWorld, World},
99
};
10-
use bevy_math::UVec2;
10+
use bevy_math::{UVec2, UVec4, Vec4};
1111
use bevy_reflect::{Reflect, TypeUuid};
1212
use bevy_render::{
1313
camera::ExtractedCamera,
@@ -152,18 +152,27 @@ impl ExtractComponent for BloomSettings {
152152
return None;
153153
}
154154

155-
camera.physical_viewport_size().map(|size| {
155+
if let (Some((origin, _)), Some(size), Some(target_size)) = (
156+
camera.physical_viewport_rect(),
157+
camera.physical_viewport_size(),
158+
camera.physical_target_size(),
159+
) {
156160
let min_view = size.x.min(size.y) / 2;
157161
let mip_count = calculate_mip_count(min_view);
158162
let scale = (min_view / 2u32.pow(mip_count)) as f32 / 8.0;
159163

160-
BloomUniform {
164+
Some(BloomUniform {
161165
threshold: settings.threshold,
162166
knee: settings.knee,
163167
scale: settings.scale * scale,
164168
intensity: settings.intensity,
165-
}
166-
})
169+
viewport: UVec4::new(origin.x, origin.y, size.x, size.y).as_vec4()
170+
/ UVec4::new(target_size.x, target_size.y, target_size.x, target_size.y)
171+
.as_vec4(),
172+
})
173+
} else {
174+
None
175+
}
167176
}
168177
}
169178

@@ -246,9 +255,6 @@ impl Node for BloomNode {
246255
&bind_groups.prefilter_bind_group,
247256
&[uniform_index.index()],
248257
);
249-
if let Some(viewport) = camera.viewport.as_ref() {
250-
prefilter_pass.set_camera_viewport(viewport);
251-
}
252258
prefilter_pass.draw(0..3, 0..1);
253259
}
254260

@@ -270,9 +276,6 @@ impl Node for BloomNode {
270276
&bind_groups.downsampling_bind_groups[mip as usize - 1],
271277
&[uniform_index.index()],
272278
);
273-
if let Some(viewport) = camera.viewport.as_ref() {
274-
downsampling_pass.set_camera_viewport(viewport);
275-
}
276279
downsampling_pass.draw(0..3, 0..1);
277280
}
278281

@@ -294,9 +297,6 @@ impl Node for BloomNode {
294297
&bind_groups.upsampling_bind_groups[mip as usize - 1],
295298
&[uniform_index.index()],
296299
);
297-
if let Some(viewport) = camera.viewport.as_ref() {
298-
upsampling_pass.set_camera_viewport(viewport);
299-
}
300300
upsampling_pass.draw(0..3, 0..1);
301301
}
302302

@@ -612,6 +612,7 @@ pub struct BloomUniform {
612612
knee: f32,
613613
scale: f32,
614614
intensity: f32,
615+
viewport: Vec4,
615616
}
616617

617618
#[derive(Component)]

0 commit comments

Comments
 (0)