1#define_import_path bevy_pbr::clustered_forward 4 mesh_view_bindings as bindings, 9 color_operations::hsv_to_rgb, 18struct ClusterableObjectIndexRanges { 20 first_point_light_index_offset: u32, 23 first_spot_light_index_offset: u32, 26 first_reflection_probe_index_offset: u32, 29 first_irradiance_volume_index_offset: u32, 30 first_decal_offset: u32, 33 last_clusterable_object_index_offset: u32, 38 cluster_factors: vec2<f32>, 43 var z_slice: u32 = 0u; 46 z_slice = u32(floor((view_z - cluster_factors.x) * cluster_factors.y)); 49 z_slice = u32(log(-view_z) * cluster_factors.x - cluster_factors.y + 1.0); 53 return min(z_slice, z_slices - 1u); 56fn view_fragment_cluster_index(frag_coord: vec2<f32>, view_z: f32, is_orthographic: bool) -> u32 { 57 let xy = vec2<u32>(floor((frag_coord - bindings::view.viewport.xy) * bindings::lights.cluster_factors.xy)); 58 let z_slice = view_z_to_z_slice( 59 bindings::lights.cluster_factors.zw, 60 bindings::lights.cluster_dimensions.z, 64 return fragment_cluster_index(vec3(xy, z_slice), bindings::lights.cluster_dimensions); 68fn fragment_cluster_index(p: vec3<u32>, cluster_dimensions: vec4<u32>) -> u32 { 72 (p.y * cluster_dimensions.x + p.x) * cluster_dimensions.z + p.z, 73 cluster_dimensions.w - 1u 78const CLUSTER_COUNT_SIZE = 9u; 85fn unpack_clusterable_object_index_ranges(cluster_index: u32) -> ClusterableObjectIndexRanges { 86#if AVAILABLE_STORAGE_BUFFER_BINDINGS >= 3 88 let offset_and_counts_a = bindings::cluster_offsets_and_counts.data[cluster_index][0]; 89 let offset_and_counts_b = bindings::cluster_offsets_and_counts.data[cluster_index][1]; 96 let point_light_offset = offset_and_counts_a.x; 97 let spot_light_offset = point_light_offset + offset_and_counts_a.y; 98 let reflection_probe_offset = spot_light_offset + offset_and_counts_a.z; 99 let irradiance_volume_offset = reflection_probe_offset + offset_and_counts_a.w; 100 let decal_offset = irradiance_volume_offset + offset_and_counts_b.x; 101 let last_clusterable_offset = decal_offset + offset_and_counts_b.y; 102 return ClusterableObjectIndexRanges( 105 reflection_probe_offset, 106 irradiance_volume_offset, 108 last_clusterable_offset 111#else // AVAILABLE_STORAGE_BUFFER_BINDINGS >= 3 113 let raw_offset_and_counts = bindings::cluster_offsets_and_counts.data[cluster_index >> 2u][cluster_index & ((1u << 2u) - 1u)]; 116 let offset_and_counts = vec3<u32>( 117 (raw_offset_and_counts >> (CLUSTER_COUNT_SIZE * 2u)) & ((1u << (32u - (CLUSTER_COUNT_SIZE * 2u))) - 1u), 118 (raw_offset_and_counts >> CLUSTER_COUNT_SIZE) & ((1u << CLUSTER_COUNT_SIZE) - 1u), 119 raw_offset_and_counts & ((1u << CLUSTER_COUNT_SIZE) - 1u), 127 let offset_a = offset_and_counts.x; 128 let offset_b = offset_a + offset_and_counts.y; 129 let offset_c = offset_b + offset_and_counts.z; 131 return ClusterableObjectIndexRanges(offset_a, offset_b, offset_c, offset_c, offset_c, offset_c); 133#endif // AVAILABLE_STORAGE_BUFFER_BINDINGS >= 3 140fn get_clusterable_object_id(index: u32) -> u32 { 141#if AVAILABLE_STORAGE_BUFFER_BINDINGS >= 3 142 return bindings::clusterable_object_index_lists.data[index]; 146 let indices = bindings::clusterable_object_index_lists.data[index >> 4u][(index >> 2u) & 149 return (indices >> (8u * (index & ((1u << 2u) - 1u)))) & ((1u << 8u) - 1u); 153fn cluster_debug_visualization( 154 input_color: vec4<f32>, 156 is_orthographic: bool, 157 clusterable_object_index_ranges: ClusterableObjectIndexRanges, 160 var output_color = input_color; 163#ifdef CLUSTERED_FORWARD_DEBUG_Z_SLICES 165 let cluster_overlay_alpha = 0.1; 166 var z_slice: u32 = view_z_to_z_slice( 167 bindings::lights.cluster_factors.zw, 168 bindings::lights.cluster_dimensions.z, 173 if (z_slice & 1u) == 1u { 174 z_slice = z_slice + bindings::lights.cluster_dimensions.z / 2u; 176 let slice_color_hsv = vec3( 177 f32(z_slice) / f32(bindings::lights.cluster_dimensions.z + 1u) * PI_2, 181 let slice_color = hsv_to_rgb(slice_color_hsv); 182 output_color = vec4<f32>( 183 (1.0 - cluster_overlay_alpha) * output_color.rgb + cluster_overlay_alpha * slice_color, 186#endif // CLUSTERED_FORWARD_DEBUG_Z_SLICES 187#ifdef CLUSTERED_FORWARD_DEBUG_CLUSTER_COMPLEXITY 191 let cluster_overlay_alpha = 0.1; 192 let max_complexity_per_cluster = 64.0; 193 let object_count = clusterable_object_index_ranges.first_reflection_probe_index_offset - 194 clusterable_object_index_ranges.first_point_light_index_offset; 195 output_color.r = (1.0 - cluster_overlay_alpha) * output_color.r + cluster_overlay_alpha * 196 smoothstep(0.0, max_complexity_per_cluster, f32(object_count)); 197 output_color.g = (1.0 - cluster_overlay_alpha) * output_color.g + cluster_overlay_alpha * 198 (1.0 - smoothstep(0.0, max_complexity_per_cluster, f32(object_count))); 199#endif // CLUSTERED_FORWARD_DEBUG_CLUSTER_COMPLEXITY 200#ifdef CLUSTERED_FORWARD_DEBUG_CLUSTER_COHERENCY 202 let cluster_overlay_alpha = 0.1; 203 var rng = cluster_index; 204 let cluster_color_hsv = vec3(rand_f(&rng) * PI_2, 1.0, 0.5); 205 let cluster_color = hsv_to_rgb(cluster_color_hsv); 206 output_color = vec4<f32>( 207 (1.0 - cluster_overlay_alpha) * output_color.rgb + cluster_overlay_alpha * cluster_color, 210#endif // CLUSTERED_FORWARD_DEBUG_CLUSTER_COHERENCY