Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1#define_import_path bevy_pbr::clustered_forward
3#import bevy_pbr::{
4 mesh_view_bindings as bindings,
5 utils::rand_f,
6}
8#import bevy_render::{
9 color_operations::hsv_to_rgb,
10 maths::PI_2,
13// Offsets within the `cluster_offsets_and_counts` buffer for a single cluster.
14//
15// These offsets must be monotonically nondecreasing. That is, indices are
16// always sorted into the following order: point lights, spot lights, reflection
17// probes, irradiance volumes.
18struct ClusterableObjectIndexRanges {
19 // The offset of the index of the first point light.
20 first_point_light_index_offset: u32,
21 // The offset of the index of the first spot light, which also terminates
22 // the list of point lights.
23 first_spot_light_index_offset: u32,
24 // The offset of the index of the first reflection probe, which also
25 // terminates the list of spot lights.
26 first_reflection_probe_index_offset: u32,
27 // The offset of the index of the first irradiance volumes, which also
28 // terminates the list of reflection probes.
29 first_irradiance_volume_index_offset: u32,
30 first_decal_offset: u32,
31 // One past the offset of the index of the final clusterable object for this
32 // cluster.
33 last_clusterable_object_index_offset: u32,
36// NOTE: Keep in sync with bevy_pbr/src/light.rs
37fn view_z_to_z_slice(view_z: f32, is_orthographic: bool) -> u32 {
38 var z_slice: u32 = 0u;
39 if is_orthographic {
40 // NOTE: view_z is correct in the orthographic case
41 z_slice = u32(floor((view_z - bindings::lights.cluster_factors.z) * bindings::lights.cluster_factors.w));
42 } else {
43 // NOTE: had to use -view_z to make it positive else log(negative) is nan
44 z_slice = u32(log(-view_z) * bindings::lights.cluster_factors.z - bindings::lights.cluster_factors.w + 1.0);
45 }
46 // NOTE: We use min as we may limit the far z plane used for clustering to be closer than
47 // the furthest thing being drawn. This means that we need to limit to the maximum cluster.
48 return min(z_slice, bindings::lights.cluster_dimensions.z - 1u);
51fn fragment_cluster_index(frag_coord: vec2<f32>, view_z: f32, is_orthographic: bool) -> u32 {
52 let xy = vec2<u32>(floor((frag_coord - bindings::view.viewport.xy) * bindings::lights.cluster_factors.xy));
53 let z_slice = view_z_to_z_slice(view_z, is_orthographic);
54 // NOTE: Restricting cluster index to avoid undefined behavior when accessing uniform buffer
55 // arrays based on the cluster index.
56 return min(
57 (xy.y * bindings::lights.cluster_dimensions.x + xy.x) * bindings::lights.cluster_dimensions.z + z_slice,
58 bindings::lights.cluster_dimensions.w - 1u
59 );
62// this must match CLUSTER_COUNT_SIZE in light.rs
63const CLUSTER_COUNT_SIZE = 9u;
65// Returns the indices of clusterable objects belonging to the given cluster.
66//
67// Note that if fewer than 3 SSBO bindings are available (in WebGL 2,
68// primarily), light probes aren't clustered, and therefore both light probe
69// index ranges will be empty.
70fn unpack_clusterable_object_index_ranges(cluster_index: u32) -> ClusterableObjectIndexRanges {
71#if AVAILABLE_STORAGE_BUFFER_BINDINGS >= 3
73 let offset_and_counts_a = bindings::cluster_offsets_and_counts.data[cluster_index][0];
74 let offset_and_counts_b = bindings::cluster_offsets_and_counts.data[cluster_index][1];
76 // Sum up the counts to produce the range brackets.
77 //
78 // We could have stored the range brackets in `cluster_offsets_and_counts`
79 // directly, but doing it this way makes the logic in this path more
80 // consistent with the WebGL 2 path below.
81 let point_light_offset = offset_and_counts_a.x;
82 let spot_light_offset = point_light_offset + offset_and_counts_a.y;
83 let reflection_probe_offset = spot_light_offset + offset_and_counts_a.z;
84 let irradiance_volume_offset = reflection_probe_offset + offset_and_counts_a.w;
85 let decal_offset = irradiance_volume_offset + offset_and_counts_b.x;
86 let last_clusterable_offset = decal_offset + offset_and_counts_b.y;
87 return ClusterableObjectIndexRanges(
88 point_light_offset,
89 spot_light_offset,
90 reflection_probe_offset,
91 irradiance_volume_offset,
92 decal_offset,
93 last_clusterable_offset
94 );
96#else // AVAILABLE_STORAGE_BUFFER_BINDINGS >= 3
98 let raw_offset_and_counts = bindings::cluster_offsets_and_counts.data[cluster_index >> 2u][cluster_index & ((1u << 2u) - 1u)];
99 // [ 31 .. 18 | 17 .. 9 | 8 .. 0 ]
100 // [ offset | point light count | spot light count ]
101 let offset_and_counts = vec3<u32>(
102 (raw_offset_and_counts >> (CLUSTER_COUNT_SIZE * 2u)) & ((1u << (32u - (CLUSTER_COUNT_SIZE * 2u))) - 1u),
103 (raw_offset_and_counts >> CLUSTER_COUNT_SIZE) & ((1u << CLUSTER_COUNT_SIZE) - 1u),
104 raw_offset_and_counts & ((1u << CLUSTER_COUNT_SIZE) - 1u),
105 );
107 // We don't cluster reflection probes or irradiance volumes on this
108 // platform, as there's no room in the UBO. Thus, those offset ranges
109 // (corresponding to `offset_d` and `offset_e` above) are empty and are
110 // simply copies of `offset_c`.
112 let offset_a = offset_and_counts.x;
113 let offset_b = offset_a + offset_and_counts.y;
114 let offset_c = offset_b + offset_and_counts.z;
116 return ClusterableObjectIndexRanges(offset_a, offset_b, offset_c, offset_c, offset_c, offset_c);
118#endif // AVAILABLE_STORAGE_BUFFER_BINDINGS >= 3
121// Returns the index of the clusterable object at the given offset.
122//
123// Note that, in the case of a light probe, the index refers to an element in
124// one of the two `light_probes` sublists, not the `clusterable_objects` list.
125fn get_clusterable_object_id(index: u32) -> u32 {
126#if AVAILABLE_STORAGE_BUFFER_BINDINGS >= 3
127 return bindings::clusterable_object_index_lists.data[index];
128#else
129 // The index is correct but in clusterable_object_index_lists we pack 4 u8s into a u32
130 // This means the index into clusterable_object_index_lists is index / 4
131 let indices = bindings::clusterable_object_index_lists.data[index >> 4u][(index >> 2u) &
132 ((1u << 2u) - 1u)];
133 // And index % 4 gives the sub-index of the u8 within the u32 so we shift by 8 * sub-index
134 return (indices >> (8u * (index & ((1u << 2u) - 1u)))) & ((1u << 8u) - 1u);
135#endif
138fn cluster_debug_visualization(
139 input_color: vec4<f32>,
140 view_z: f32,
141 is_orthographic: bool,
142 clusterable_object_index_ranges: ClusterableObjectIndexRanges,
143 cluster_index: u32,
144) -> vec4<f32> {
145 var output_color = input_color;
147 // Cluster allocation debug (using 'over' alpha blending)
148#ifdef CLUSTERED_FORWARD_DEBUG_Z_SLICES
149 // NOTE: This debug mode visualizes the z-slices
150 let cluster_overlay_alpha = 0.1;
151 var z_slice: u32 = view_z_to_z_slice(view_z, is_orthographic);
152 // A hack to make the colors alternate a bit more
153 if (z_slice & 1u) == 1u {
154 z_slice = z_slice + bindings::lights.cluster_dimensions.z / 2u;
155 }
156 let slice_color_hsv = vec3(
157 f32(z_slice) / f32(bindings::lights.cluster_dimensions.z + 1u) * PI_2,
158 1.0,
159 0.5
160 );
161 let slice_color = hsv_to_rgb(slice_color_hsv);
162 output_color = vec4<f32>(
163 (1.0 - cluster_overlay_alpha) * output_color.rgb + cluster_overlay_alpha * slice_color,
164 output_color.a
165 );
166#endif // CLUSTERED_FORWARD_DEBUG_Z_SLICES
167#ifdef CLUSTERED_FORWARD_DEBUG_CLUSTER_COMPLEXITY
168 // NOTE: This debug mode visualizes the number of clusterable objects within
169 // the cluster that contains the fragment. It shows a sort of cluster
170 // complexity measure.
171 let cluster_overlay_alpha = 0.1;
172 let max_complexity_per_cluster = 64.0;
173 let object_count = clusterable_object_index_ranges.first_reflection_probe_index_offset -
174 clusterable_object_index_ranges.first_point_light_index_offset;
175 output_color.r = (1.0 - cluster_overlay_alpha) * output_color.r + cluster_overlay_alpha *
176 smoothstep(0.0, max_complexity_per_cluster, f32(object_count));
177 output_color.g = (1.0 - cluster_overlay_alpha) * output_color.g + cluster_overlay_alpha *
178 (1.0 - smoothstep(0.0, max_complexity_per_cluster, f32(object_count)));
179#endif // CLUSTERED_FORWARD_DEBUG_CLUSTER_COMPLEXITY
180#ifdef CLUSTERED_FORWARD_DEBUG_CLUSTER_COHERENCY
181 // NOTE: Visualizes the cluster to which the fragment belongs
182 let cluster_overlay_alpha = 0.1;
183 var rng = cluster_index;
184 let cluster_color_hsv = vec3(rand_f(&rng) * PI_2, 1.0, 0.5);
185 let cluster_color = hsv_to_rgb(cluster_color_hsv);
186 output_color = vec4<f32>(
187 (1.0 - cluster_overlay_alpha) * output_color.rgb + cluster_overlay_alpha * cluster_color,
188 output_color.a
189 );
190#endif // CLUSTERED_FORWARD_DEBUG_CLUSTER_COHERENCY
192 return output_color;
Contents