1#define_import_path bevy_pbr::clustered_forward 3#import bevy_pbr::mesh_view_bindings as Bindings 6fn view_z_to_z_slice(view_z: f32, is_orthographic: bool) -> u32 { 10 z_slice = u32(floor((view_z - Bindings::lights.cluster_factors.z) * Bindings::lights.cluster_factors.w)); 13 z_slice = u32(log(-view_z) * Bindings::lights.cluster_factors.z - Bindings::lights.cluster_factors.w + 1.0); 17 return min(z_slice, Bindings::lights.cluster_dimensions.z - 1u); 20fn fragment_cluster_index(frag_coord: vec2<f32>, view_z: f32, is_orthographic: bool) -> u32 { 21 let xy = vec2<u32>(floor(frag_coord * Bindings::lights.cluster_factors.xy)); 22 let z_slice = view_z_to_z_slice(view_z, is_orthographic); 26 (xy.y * Bindings::lights.cluster_dimensions.x + xy.x) * Bindings::lights.cluster_dimensions.z + z_slice, 27 Bindings::lights.cluster_dimensions.w - 1u 32const CLUSTER_COUNT_SIZE = 9u; 33fn unpack_offset_and_counts(cluster_index: u32) -> vec3<u32> { 34#ifdef NO_STORAGE_BUFFERS_SUPPORT 35 let offset_and_counts = Bindings::cluster_offsets_and_counts.data[cluster_index >> 2u][cluster_index & ((1u << 2u) - 1u)]; 39 (offset_and_counts >> (CLUSTER_COUNT_SIZE * 2u)) & ((1u << (32u - (CLUSTER_COUNT_SIZE * 2u))) - 1u), 40 (offset_and_counts >> CLUSTER_COUNT_SIZE) & ((1u << CLUSTER_COUNT_SIZE) - 1u), 41 offset_and_counts & ((1u << CLUSTER_COUNT_SIZE) - 1u), 44 return Bindings::cluster_offsets_and_counts.data[cluster_index].xyz; 48fn get_light_id(index: u32) -> u32 { 49#ifdef NO_STORAGE_BUFFERS_SUPPORT 52 let indices = Bindings::cluster_light_index_lists.data[index >> 4u][(index >> 2u) & ((1u << 2u) - 1u)]; 54 return (indices >> (8u * (index & ((1u << 2u) - 1u)))) & ((1u << 8u) - 1u); 56 return Bindings::cluster_light_index_lists.data[index]; 60fn cluster_debug_visualization( 61 output_color: vec4<f32>, 63 is_orthographic: bool, 64 offset_and_counts: vec3<u32>, 68#ifdef CLUSTERED_FORWARD_DEBUG_Z_SLICES 70 let cluster_overlay_alpha = 0.1; 71 var z_slice: u32 = view_z_to_z_slice(view_z, is_orthographic); 73 if ((z_slice & 1u) == 1u) { 74 z_slice = z_slice + Bindings::lights.cluster_dimensions.z / 2u; 76 let slice_color = hsv2rgb(f32(z_slice) / f32(Bindings::lights.cluster_dimensions.z + 1u), 1.0, 0.5); 77 output_color = vec4<f32>( 78 (1.0 - cluster_overlay_alpha) * output_color.rgb + cluster_overlay_alpha * slice_color, 81#endif // CLUSTERED_FORWARD_DEBUG_Z_SLICES 82#ifdef CLUSTERED_FORWARD_DEBUG_CLUSTER_LIGHT_COMPLEXITY 85 let cluster_overlay_alpha = 0.1; 86 let max_light_complexity_per_cluster = 64.0; 87 output_color.r = (1.0 - cluster_overlay_alpha) * output_color.r 88 + cluster_overlay_alpha * smoothStep(0.0, max_light_complexity_per_cluster, f32(offset_and_counts[1] + offset_and_counts[2])); 89 output_color.g = (1.0 - cluster_overlay_alpha) * output_color.g 90 + cluster_overlay_alpha * (1.0 - smoothStep(0.0, max_light_complexity_per_cluster, f32(offset_and_counts[1] + offset_and_counts[2]))); 91#endif // CLUSTERED_FORWARD_DEBUG_CLUSTER_LIGHT_COMPLEXITY 92#ifdef CLUSTERED_FORWARD_DEBUG_CLUSTER_COHERENCY 94 let cluster_overlay_alpha = 0.1; 95 let cluster_color = hsv2rgb(random1D(f32(cluster_index)), 1.0, 0.5); 96 output_color = vec4<f32>( 97 (1.0 - cluster_overlay_alpha) * output_color.rgb + cluster_overlay_alpha * cluster_color, 100#endif // CLUSTERED_FORWARD_DEBUG_CLUSTER_COHERENCY