Skip to content

Commit 0d2b527

Browse files
committed
Avoid windows with a physical size of zero (#4098)
# Objective Fix #4097 ## Solution Return `None` from `RenderTarget::get_physical_size` if either dimension is zero.
1 parent 3b81a50 commit 0d2b527

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

crates/bevy_pbr/src/light.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,13 @@ impl Clusters {
389389
// NOTE: Maximum 4096 clusters due to uniform buffer size constraints
390390
debug_assert!(self.dimensions.x * self.dimensions.y * self.dimensions.z <= 4096);
391391
}
392+
fn clear(&mut self) {
393+
self.tile_size = UVec2::ONE;
394+
self.dimensions = UVec3::ONE;
395+
self.near = 0.0;
396+
self.far = 0.0;
397+
self.lights.clear();
398+
}
392399
}
393400

394401
fn clip_to_view(inverse_projection: Mat4, clip: Vec4) -> Vec4 {
@@ -722,17 +729,22 @@ pub(crate) fn assign_lights_to_clusters(
722729
for (view_entity, camera_transform, camera, frustum, config, clusters, mut visible_lights) in
723730
views.iter_mut()
724731
{
732+
let clusters = clusters.into_inner();
733+
725734
if matches!(config, ClusterConfig::None) && visible_lights.is_some() {
726735
commands.entity(view_entity).remove::<VisiblePointLights>();
736+
clusters.clear();
727737
continue;
728738
}
729739

730-
let clusters = clusters.into_inner();
731-
let screen_size = camera.target.get_physical_size(&windows, &images);
732-
733-
clusters.lights.clear();
740+
let screen_size =
741+
if let Some(screen_size) = camera.target.get_physical_size(&windows, &images) {
742+
screen_size
743+
} else {
744+
clusters.clear();
745+
continue;
746+
};
734747

735-
let screen_size = screen_size.unwrap_or_default();
736748
let mut requested_cluster_dimensions = config.dimensions_for_screen_size(screen_size);
737749

738750
let view_transform = camera_transform.compute_matrix();
@@ -862,10 +874,6 @@ pub(crate) fn assign_lights_to_clusters(
862874
VisiblePointLights::default,
863875
);
864876

865-
if screen_size.x == 0 || screen_size.y == 0 {
866-
continue;
867-
}
868-
869877
// Calculate the x/y/z cluster frustum planes in view space
870878
let mut x_planes = Vec::with_capacity(clusters.dimensions.x as usize + 1);
871879
let mut y_planes = Vec::with_capacity(clusters.dimensions.y as usize + 1);
@@ -923,16 +931,7 @@ pub(crate) fn assign_lights_to_clusters(
923931
z_planes.push(Plane::new(normal.extend(d)));
924932
}
925933

926-
let mut visible_lights_scratch = Vec::new();
927-
928-
{
929-
// reuse existing visible lights Vec, if it exists
930-
let visible_lights = if let Some(visible_lights) = visible_lights.as_mut() {
931-
visible_lights.entities.clear();
932-
&mut visible_lights.entities
933-
} else {
934-
&mut visible_lights_scratch
935-
};
934+
let mut update_from_light_intersections = |visible_lights: &mut Vec<Entity>| {
936935
for light in lights.iter() {
937936
let light_sphere = Sphere {
938937
center: Vec3A::from(light.translation),
@@ -1085,12 +1084,18 @@ pub(crate) fn assign_lights_to_clusters(
10851084
}
10861085
}
10871086
}
1088-
}
1087+
};
10891088

1090-
if visible_lights.is_none() {
1091-
commands.entity(view_entity).insert(VisiblePointLights {
1092-
entities: visible_lights_scratch,
1093-
});
1089+
// reuse existing visible lights Vec, if it exists
1090+
if let Some(visible_lights) = visible_lights.as_mut() {
1091+
visible_lights.entities.clear();
1092+
update_from_light_intersections(&mut visible_lights.entities);
1093+
} else {
1094+
let mut entities = Vec::new();
1095+
update_from_light_intersections(&mut entities);
1096+
commands
1097+
.entity(view_entity)
1098+
.insert(VisiblePointLights { entities });
10941099
}
10951100
}
10961101
}

crates/bevy_render/src/camera/camera.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl RenderTarget {
7979
UVec2::new(width, height)
8080
}),
8181
}
82+
.filter(|size| size.x > 0 && size.y > 0)
8283
}
8384
pub fn get_logical_size(&self, windows: &Windows, images: &Assets<Image>) -> Option<Vec2> {
8485
match self {
@@ -312,8 +313,8 @@ pub fn extract_cameras<M: Component + Default>(
312313
ExtractedView {
313314
projection: camera.projection_matrix,
314315
transform: *transform,
315-
width: size.x.max(1),
316-
height: size.y.max(1),
316+
width: size.x,
317+
height: size.y,
317318
near: camera.near,
318319
far: camera.far,
319320
},

0 commit comments

Comments
 (0)