Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - fix cluster tiling calculations #4148

Closed
wants to merge 8 commits into from
Closed
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 92 additions & 8 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,26 @@ impl ClusterConfig {
} => {
let aspect_ratio = screen_size.x as f32 / screen_size.y as f32;
let per_layer = *total as f32 / *z_slices as f32;
assert!(
per_layer >= 1.0,
"ClusterConfig has more z-slices than total clusters!"
);
robtfm marked this conversation as resolved.
Show resolved Hide resolved

let y = f32::sqrt(per_layer / aspect_ratio);
let x = (y * aspect_ratio).floor() as u32;
let y = y.floor() as u32;

let mut x = (y * aspect_ratio).floor() as u32;
let mut y = y.floor() as u32;
robtfm marked this conversation as resolved.
Show resolved Hide resolved

// check extremes
if x == 0 {
x = 1;
y = per_layer as u32;
}
if y == 0 {
x = per_layer as u32;
y = 1;
}

UVec3::new(x, y, *z_slices)
}
}
Expand Down Expand Up @@ -369,8 +386,12 @@ impl Clusters {
near: f32,
far: f32,
) -> Self {
assert!(screen_size.x > 0 && screen_size.y > 0);
assert!(dimensions.x > 0 && dimensions.y > 0 && dimensions.z > 0);
robtfm marked this conversation as resolved.
Show resolved Hide resolved
Clusters::new(
(screen_size + UVec2::ONE) / dimensions.xy(),
(screen_size.as_vec2() / dimensions.xy().as_vec2())
.ceil()
.as_uvec2(),
screen_size,
dimensions.z,
near,
Expand All @@ -380,11 +401,10 @@ impl Clusters {

fn update(&mut self, tile_size: UVec2, screen_size: UVec2, z_slices: u32) {
self.tile_size = tile_size;
self.axis_slices = UVec3::new(
(screen_size.x + 1) / tile_size.x,
(screen_size.y + 1) / tile_size.y,
z_slices,
);
self.axis_slices = (screen_size.as_vec2() / tile_size.as_vec2())
.ceil()
.as_uvec2()
.extend(z_slices);
// NOTE: Maximum 4096 clusters due to uniform buffer size constraints
assert!(self.axis_slices.x * self.axis_slices.y * self.axis_slices.z <= 4096);
}
Expand Down Expand Up @@ -1240,3 +1260,67 @@ pub fn check_light_mesh_visibility(
}
}
}

mod test {
use super::*;

#[allow(dead_code)]
fn test_cluster_tiling(config: ClusterConfig, screen_size: UVec2) -> Clusters {
let dims = config.dimensions_for_screen_size(screen_size);

// note: near & far do not affect tiling
let clusters = Clusters::from_screen_size_and_dimensions(screen_size, dims, 5.0, 1000.0);

// check we cover the screen
assert!(clusters.tile_size.x * clusters.axis_slices.x >= screen_size.x);
assert!(clusters.tile_size.y * clusters.axis_slices.y >= screen_size.y);
// check a smaller number of clusters would not cover the screen
assert!(clusters.tile_size.x * (clusters.axis_slices.x - 1) < screen_size.x);
assert!(clusters.tile_size.y * (clusters.axis_slices.y - 1) < screen_size.y);
// check a smaller tilesize would not cover the screen
assert!((clusters.tile_size.x - 1) * clusters.axis_slices.x < screen_size.x);
assert!((clusters.tile_size.y - 1) * clusters.axis_slices.y < screen_size.y);
// check we don't have more clusters than pixels
assert!(clusters.axis_slices.x <= screen_size.x);
assert!(clusters.axis_slices.y <= screen_size.y);

clusters
}

#[test]
// check tiling for small screen sizes
fn test_default_cluster_setup_small_screensizes() {
for x in 1..100 {
for y in 1..100 {
let screen_size = UVec2::new(x, y);
let clusters = test_cluster_tiling(ClusterConfig::default(), screen_size);
assert!(
clusters.axis_slices.x * clusters.axis_slices.y * clusters.axis_slices.z
<= 4096
);
}
}
}

#[test]
// check tiling for long thin screen sizes
fn test_default_cluster_setup_small_x() {
for x in 1..10 {
for y in 1..5000 {
let screen_size = UVec2::new(x, y);
let clusters = test_cluster_tiling(ClusterConfig::default(), screen_size);
assert!(
clusters.axis_slices.x * clusters.axis_slices.y * clusters.axis_slices.z
<= 4096
);

let screen_size = UVec2::new(y, x);
let clusters = test_cluster_tiling(ClusterConfig::default(), screen_size);
assert!(
clusters.axis_slices.x * clusters.axis_slices.y * clusters.axis_slices.z
<= 4096
);
}
}
}
}