Skip to content

Commit

Permalink
Avoid windows with a physical size of zero (bevyengine#4098)
Browse files Browse the repository at this point in the history
# Objective

Fix bevyengine#4097

## Solution

Return `None` from `RenderTarget::get_physical_size` if either dimension is zero.
  • Loading branch information
HackerFoo committed Apr 15, 2022
1 parent 3b81a50 commit 0d2b527
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
53 changes: 29 additions & 24 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,13 @@ impl Clusters {
// NOTE: Maximum 4096 clusters due to uniform buffer size constraints
debug_assert!(self.dimensions.x * self.dimensions.y * self.dimensions.z <= 4096);
}
fn clear(&mut self) {
self.tile_size = UVec2::ONE;
self.dimensions = UVec3::ONE;
self.near = 0.0;
self.far = 0.0;
self.lights.clear();
}
}

fn clip_to_view(inverse_projection: Mat4, clip: Vec4) -> Vec4 {
Expand Down Expand Up @@ -722,17 +729,22 @@ pub(crate) fn assign_lights_to_clusters(
for (view_entity, camera_transform, camera, frustum, config, clusters, mut visible_lights) in
views.iter_mut()
{
let clusters = clusters.into_inner();

if matches!(config, ClusterConfig::None) && visible_lights.is_some() {
commands.entity(view_entity).remove::<VisiblePointLights>();
clusters.clear();
continue;
}

let clusters = clusters.into_inner();
let screen_size = camera.target.get_physical_size(&windows, &images);

clusters.lights.clear();
let screen_size =
if let Some(screen_size) = camera.target.get_physical_size(&windows, &images) {
screen_size
} else {
clusters.clear();
continue;
};

let screen_size = screen_size.unwrap_or_default();
let mut requested_cluster_dimensions = config.dimensions_for_screen_size(screen_size);

let view_transform = camera_transform.compute_matrix();
Expand Down Expand Up @@ -862,10 +874,6 @@ pub(crate) fn assign_lights_to_clusters(
VisiblePointLights::default,
);

if screen_size.x == 0 || screen_size.y == 0 {
continue;
}

// Calculate the x/y/z cluster frustum planes in view space
let mut x_planes = Vec::with_capacity(clusters.dimensions.x as usize + 1);
let mut y_planes = Vec::with_capacity(clusters.dimensions.y as usize + 1);
Expand Down Expand Up @@ -923,16 +931,7 @@ pub(crate) fn assign_lights_to_clusters(
z_planes.push(Plane::new(normal.extend(d)));
}

let mut visible_lights_scratch = Vec::new();

{
// reuse existing visible lights Vec, if it exists
let visible_lights = if let Some(visible_lights) = visible_lights.as_mut() {
visible_lights.entities.clear();
&mut visible_lights.entities
} else {
&mut visible_lights_scratch
};
let mut update_from_light_intersections = |visible_lights: &mut Vec<Entity>| {
for light in lights.iter() {
let light_sphere = Sphere {
center: Vec3A::from(light.translation),
Expand Down Expand Up @@ -1085,12 +1084,18 @@ pub(crate) fn assign_lights_to_clusters(
}
}
}
}
};

if visible_lights.is_none() {
commands.entity(view_entity).insert(VisiblePointLights {
entities: visible_lights_scratch,
});
// reuse existing visible lights Vec, if it exists
if let Some(visible_lights) = visible_lights.as_mut() {
visible_lights.entities.clear();
update_from_light_intersections(&mut visible_lights.entities);
} else {
let mut entities = Vec::new();
update_from_light_intersections(&mut entities);
commands
.entity(view_entity)
.insert(VisiblePointLights { entities });
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl RenderTarget {
UVec2::new(width, height)
}),
}
.filter(|size| size.x > 0 && size.y > 0)
}
pub fn get_logical_size(&self, windows: &Windows, images: &Assets<Image>) -> Option<Vec2> {
match self {
Expand Down Expand Up @@ -312,8 +313,8 @@ pub fn extract_cameras<M: Component + Default>(
ExtractedView {
projection: camera.projection_matrix,
transform: *transform,
width: size.x.max(1),
height: size.y.max(1),
width: size.x,
height: size.y,
near: camera.near,
far: camera.far,
},
Expand Down

0 comments on commit 0d2b527

Please sign in to comment.