From 5b38989ac4a5b02f577407106009a514b8256fc5 Mon Sep 17 00:00:00 2001 From: atlv Date: Sun, 6 Jul 2025 15:36:58 -0400 Subject: [PATCH] dont hard code clustering limits on cpu side so they can be informed by Limits later (#19985) # Objective - prepare bevy_light for split - make limits more dynamically configurable ## Solution - use settings struct ## Testing - 3d_scene, lighting --- crates/bevy_pbr/src/cluster/assign.rs | 26 ++++++++++++------- .../src/cluster/extract_and_prepare.rs | 2 ++ crates/bevy_pbr/src/cluster/mod.rs | 2 ++ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/crates/bevy_pbr/src/cluster/assign.rs b/crates/bevy_pbr/src/cluster/assign.rs index 9dc9a56b52..e204644204 100644 --- a/crates/bevy_pbr/src/cluster/assign.rs +++ b/crates/bevy_pbr/src/cluster/assign.rs @@ -20,8 +20,7 @@ use tracing::warn; use super::{ ClusterConfig, ClusterFarZMode, ClusteredDecal, Clusters, GlobalClusterSettings, - GlobalVisibleClusterableObjects, ViewClusterBindings, VisibleClusterableObjects, - MAX_UNIFORM_BUFFER_CLUSTERABLE_OBJECTS, + GlobalVisibleClusterableObjects, VisibleClusterableObjects, }; use crate::{ prelude::EnvironmentMapLight, ExtractedPointLight, LightProbe, PointLight, SpotLight, @@ -263,7 +262,7 @@ pub(crate) fn assign_objects_to_clusters( })); } - if clusterable_objects.len() > MAX_UNIFORM_BUFFER_CLUSTERABLE_OBJECTS + if clusterable_objects.len() > global_cluster_settings.max_uniform_buffer_clusterable_objects && !global_cluster_settings.supports_storage_buffers { clusterable_objects.sort_by_cached_key(|clusterable_object| { @@ -282,7 +281,9 @@ pub(crate) fn assign_objects_to_clusters( let mut clusterable_objects_in_view_count = 0; clusterable_objects.retain(|clusterable_object| { // take one extra clusterable object to check if we should emit the warning - if clusterable_objects_in_view_count == MAX_UNIFORM_BUFFER_CLUSTERABLE_OBJECTS + 1 { + if clusterable_objects_in_view_count + == global_cluster_settings.max_uniform_buffer_clusterable_objects + 1 + { false } else { let clusterable_object_sphere = clusterable_object.sphere(); @@ -298,17 +299,19 @@ pub(crate) fn assign_objects_to_clusters( } }); - if clusterable_objects.len() > MAX_UNIFORM_BUFFER_CLUSTERABLE_OBJECTS + if clusterable_objects.len() + > global_cluster_settings.max_uniform_buffer_clusterable_objects && !*max_clusterable_objects_warning_emitted { warn!( - "MAX_UNIFORM_BUFFER_CLUSTERABLE_OBJECTS ({}) exceeded", - MAX_UNIFORM_BUFFER_CLUSTERABLE_OBJECTS + "max_uniform_buffer_clusterable_objects ({}) exceeded", + global_cluster_settings.max_uniform_buffer_clusterable_objects ); *max_clusterable_objects_warning_emitted = true; } - clusterable_objects.truncate(MAX_UNIFORM_BUFFER_CLUSTERABLE_OBJECTS); + clusterable_objects + .truncate(global_cluster_settings.max_uniform_buffer_clusterable_objects); } for ( @@ -448,14 +451,17 @@ pub(crate) fn assign_objects_to_clusters( (xy_count.x + x_overlap) * (xy_count.y + y_overlap) * z_count as f32; } - if cluster_index_estimate > ViewClusterBindings::MAX_INDICES as f32 { + if cluster_index_estimate + > global_cluster_settings.view_cluster_bindings_max_indices as f32 + { // scale x and y cluster count to be able to fit all our indices // we take the ratio of the actual indices over the index estimate. // this is not guaranteed to be small enough due to overlapped tiles, but // the conservative estimate is more than sufficient to cover the // difference - let index_ratio = ViewClusterBindings::MAX_INDICES as f32 / cluster_index_estimate; + let index_ratio = global_cluster_settings.view_cluster_bindings_max_indices as f32 + / cluster_index_estimate; let xy_ratio = index_ratio.sqrt(); requested_cluster_dimensions.x = diff --git a/crates/bevy_pbr/src/cluster/extract_and_prepare.rs b/crates/bevy_pbr/src/cluster/extract_and_prepare.rs index 56267f46b2..53187a2b02 100644 --- a/crates/bevy_pbr/src/cluster/extract_and_prepare.rs +++ b/crates/bevy_pbr/src/cluster/extract_and_prepare.rs @@ -47,6 +47,8 @@ pub(crate) fn make_global_cluster_settings(world: &World) -> GlobalClusterSettin GlobalClusterSettings { supports_storage_buffers, clustered_decals_are_usable, + max_uniform_buffer_clusterable_objects: MAX_UNIFORM_BUFFER_CLUSTERABLE_OBJECTS, + view_cluster_bindings_max_indices: ViewClusterBindings::MAX_INDICES, } } diff --git a/crates/bevy_pbr/src/cluster/mod.rs b/crates/bevy_pbr/src/cluster/mod.rs index 90af295cf6..5132326fb1 100644 --- a/crates/bevy_pbr/src/cluster/mod.rs +++ b/crates/bevy_pbr/src/cluster/mod.rs @@ -43,6 +43,8 @@ mod test; pub struct GlobalClusterSettings { pub supports_storage_buffers: bool, pub clustered_decals_are_usable: bool, + pub max_uniform_buffer_clusterable_objects: usize, + pub view_cluster_bindings_max_indices: usize, } /// Configure the far z-plane mode used for the furthest depth slice for clustered forward