crcbl_render/forward/skin_cull.rs
1//! The skinning cull: which of a frame's planned skinning ranges anything the
2//! frame records can draw.
3//!
4//! The camera, light and occlusion culls already drop a skinned object whose
5//! own box is out of their reach — see [`SkinnedInstanceDesc::bounds`]. The
6//! dispatch that deforms its vertices ran regardless, so an off-screen
7//! character still cost its whole skinning pass. This is the host-side
8//! question that lets the dispatch be dropped with it, asked once per frame as
9//! the passes are recorded, when every view the frame draws has been begun.
10//!
11//! [`SkinnedInstanceDesc::bounds`]: super::SkinnedInstanceDesc::bounds
12
13use super::ForwardRenderer;
14use super::shadow_inputs::CasterFootprint;
15use super::view::ViewTarget;
16use crate::cull::Frustum;
17
18/// One planned region's answer, in [`SkinCull::verdicts`].
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20enum Verdict {
21 /// Some object drawn out of it is reached by a cull the frame records.
22 Seen,
23 /// Every object drawn out of it is out of every cull, and the frame's plan
24 /// did not name it: there is no dispatch to drop.
25 Unseen,
26 /// [`Verdict::Unseen`], and the plan named it: its dispatch is dropped.
27 Skipped,
28}
29
30/// The skinning cull's per-frame storage, kept on the renderer so a frame
31/// allocates nothing once it has grown — see
32/// [`ForwardRenderer::cull_skinned_regions`].
33#[derive(Debug, Default)]
34pub(super) struct SkinCull {
35 /// The frame's culls: the cameras of the views it records, then
36 /// [`ForwardRenderer::skin_shadow_reach`].
37 culls: Vec<Frustum>,
38 /// Every skinned object's region and footprint, sorted by region.
39 members: Vec<([u32; 2], CasterFootprint)>,
40 /// One entry per region an object is drawn out of, sorted by region.
41 verdicts: Vec<([u32; 2], Verdict)>,
42}
43
44impl SkinCull {
45 /// Whether the last [`ForwardRenderer::cull_skinned_regions`] dropped
46 /// `region`'s dispatch.
47 pub(super) fn skips(&self, region: [u32; 2]) -> bool {
48 self.verdicts
49 .binary_search_by_key(®ion, |(region, _)| *region)
50 .is_ok_and(|at| self.verdicts[at].1 == Verdict::Skipped)
51 }
52}
53
54impl ForwardRenderer {
55 /// Decides which regions of this frame's skinning plan, `planned`,
56 /// **nothing this frame draws can read**, and so need no dispatch: the
57 /// frame's skinning cull, for the primary camera and the secondary `views`
58 /// the frame records. [`SkinCull::skips`] answers it afterwards.
59 ///
60 /// # The test, and why it is conservative
61 ///
62 /// A region is skipped when at least one object is drawn out of it and
63 /// **every** such object's [`CasterFootprint`] reaches none of:
64 ///
65 /// * the primary camera's frustum, and each of `views`' — the very
66 /// matrices their culls were handed, see `View::camera_view_proj`;
67 /// * every shadow cull the frame redraws, and the probe updater's cascade
68 /// and light culls on a frame with shadows off — see
69 /// [`ForwardRenderer::skin_shadow_reach`].
70 ///
71 /// The footprint is the shadow cache's own: the record's box carried by
72 /// its transform with slack in the safe direction, so the host answers
73 /// "outside" only where the device's cull would too. An object with no box
74 /// is [`CasterFootprint::Everywhere`] and keeps its region, which is
75 /// today's behaviour for every caller that passes `None`; so does a region
76 /// no object is drawn out of, which only a readback could want.
77 ///
78 /// **The footprint is of the record the device holds for that slot this
79 /// frame**, the one `note_caster_writes` took when the frame's writes were
80 /// uploaded — every skinned object is re-pointed, and so written, on every
81 /// skinned frame. A caller's write between the frame's begin and its passes
82 /// is not uploaded until the next frame, so it does not decide this one
83 /// either: an object placed since, into a reused slot, is judged by what
84 /// that slot drew before it, which is all the device can draw from it this
85 /// frame. A slot past every footprint taken has never been uploaded, and
86 /// is taken as everywhere. A view's mask and the occlusion cull only drop
87 /// more, so they are not asked.
88 ///
89 /// # Coming back into view
90 ///
91 /// A skipped region's halves keep the poses of the last frame that skinned
92 /// it, and the parity goes on alternating. Every object drawn out of it is
93 /// therefore marked **not pointed** — the rule a fresh object and a change
94 /// of region already go through, see `SkinnedInstance::pointed`. The next
95 /// skinned frame then writes its current half into both base lanes, so
96 /// the frame it returns on reports no deformation motion rather than one
97 /// against a pose it held frames ago, and the frame after that subtracts
98 /// against the half its return filled. Its transform history is the
99 /// pool's and is untouched: a character that walked while it was out of
100 /// view still reports the walk.
101 ///
102 /// # The shadow cache
103 ///
104 /// Nothing changes there, by construction. A skipped object is still
105 /// re-pointed every skinned frame, which is the instance write that keeps
106 /// a held map honest; its footprint before and after that write is what
107 /// `shadow_groups_reached` tests. A box that has just left a light's reach
108 /// redraws that light's map through the *before* half, and the map is
109 /// redrawn without the object, which its cull now drops. And a box that
110 /// reaches no cull at all reaches no group's record, so skipping its
111 /// region holds every map it held.
112 ///
113 /// # What it costs
114 ///
115 /// Nothing past one pass over the objects where no skinned object owns a
116 /// box, which is every caller that passes `None`. Otherwise a sort of the
117 /// objects by region, one footprint test per object until its region is
118 /// seen, and a binary search per object and per planned range — in storage
119 /// the renderer keeps, see [`SkinCull`].
120 ///
121 /// With [`set_skinning_culls`](Self::set_skinning_culls) off, nothing is
122 /// skipped and nothing is marked.
123 pub(super) fn cull_skinned_regions(
124 &mut self,
125 planned: impl Iterator<Item = [u32; 2]>,
126 views: &[ViewTarget],
127 ) {
128 let cull = &mut self.skin_cull;
129 cull.culls.clear();
130 cull.members.clear();
131 cull.verdicts.clear();
132 self.skinned_skipped = 0;
133 if !self.skinning_culls {
134 return;
135 }
136
137 let footprints = &self.shadow_footprints;
138 let instances = &self.instances;
139 let footprint = |handle| {
140 instances
141 .index(handle)
142 .and_then(|element| footprints.get(element as usize))
143 .copied()
144 .unwrap_or(CasterFootprint::Everywhere)
145 };
146 cull.members.extend(
147 self.skinned_instances
148 .iter()
149 .map(|entry| (entry.bases, footprint(entry.handle))),
150 );
151 if cull
152 .members
153 .iter()
154 .all(|(_, footprint)| *footprint == CasterFootprint::Everywhere)
155 {
156 return;
157 }
158
159 cull.culls
160 .push(Frustum::from_view_projection(self.primary.camera_view_proj));
161 cull.culls.extend(views.iter().filter_map(|target| {
162 self.views
163 .get(target.view.index() - 1)
164 .and_then(Option::as_ref)
165 .map(|view| Frustum::from_view_projection(view.camera_view_proj))
166 }));
167 cull.culls.extend_from_slice(&self.skin_shadow_reach);
168 // A plane that is not finite compares false everywhere, which reads as
169 // "outside" — the one answer this must never give by accident. Such a
170 // cull reaches everything, so nothing is skipped.
171 if cull
172 .culls
173 .iter()
174 .any(|frustum| !frustum.planes.iter().all(|plane| plane.is_finite()))
175 {
176 return;
177 }
178
179 cull.members.sort_unstable_by_key(|(region, _)| *region);
180 let culls = &cull.culls;
181 for &(region, footprint) in &cull.members {
182 let fresh = cull.verdicts.last().is_none_or(|(last, _)| *last != region);
183 if fresh {
184 cull.verdicts.push((region, Verdict::Unseen));
185 }
186 let verdict = &mut cull.verdicts.last_mut().expect("pushed above").1;
187 if *verdict == Verdict::Unseen && culls.iter().any(|frustum| footprint.reaches(frustum))
188 {
189 *verdict = Verdict::Seen;
190 }
191 }
192
193 for region in planned {
194 if let Ok(at) = cull
195 .verdicts
196 .binary_search_by_key(®ion, |(region, _)| *region)
197 && cull.verdicts[at].1 == Verdict::Unseen
198 {
199 cull.verdicts[at].1 = Verdict::Skipped;
200 self.skinned_skipped += 1;
201 }
202 }
203 if self.skinned_skipped > 0 {
204 for entry in &mut self.skinned_instances {
205 if cull.skips(entry.bases) {
206 entry.pointed = false;
207 }
208 }
209 }
210 }
211
212 /// How many regions the last frame recorded through
213 /// [`add_skinned_passes`](Self::add_skinned_passes) — or another entry
214 /// point handed a [`Skinning`](crate::Skinning) — had planned and did not
215 /// dispatch, because nothing that frame drew could read them. See
216 /// [`set_skinning_culls`](Self::set_skinning_culls).
217 ///
218 /// What a pricing run or a test reads to know the cull did anything: the
219 /// colour is the same either way, and the motion target differs only on
220 /// the frame a skipped object returns — see
221 /// [`set_skinning_culls`](Self::set_skinning_culls).
222 #[must_use]
223 pub const fn skinned_regions_skipped(&self) -> u32 {
224 self.skinned_skipped
225 }
226}