euv_core/runtime/streaming/prefetch/
impl.rs1use super::*;
2
3impl SpatialPrefetchVolume {
5 #[must_use]
11 pub fn predicted_center(&self) -> [f32; 3] {
12 let center: [f32; 3] = self.get_center();
13 let velocity: [f32; 3] = self.get_velocity();
14 let horizon: f32 = self.get_lookahead_secs().max(0.0);
15 [
16 center[0] + velocity[0] * horizon,
17 center[1] + velocity[1] * horizon,
18 center[2] + velocity[2] * horizon,
19 ]
20 }
21}
22
23impl Default for SpatialPrefetchGrid {
25 fn default() -> Self {
31 Self::new(PREFETCH_DEFAULT_CELL_SIZE)
32 }
33}
34
35impl SpatialPrefetchGrid {
37 #[must_use]
47 pub fn new(cell_size: f32) -> Self {
48 Self {
49 cell_size: cell_size.max(1.0),
50 anchors: Vec::new(),
51 key_to_index: HashMap::new(),
52 cells: HashMap::new(),
53 active_prefetches: HashSet::new(),
54 }
55 }
56
57 pub fn register_anchor(&mut self, anchor: SpatialAssetAnchor) {
63 let key_hash: u64 = anchor.get_key_hash();
64 let _: bool = self.unregister_anchor(key_hash);
65 let Self {
66 cell_size,
67 anchors,
68 key_to_index,
69 cells,
70 ..
71 } = self;
72 let idx: usize = anchors.len();
73 anchors.push(Some(anchor));
74 let _: Option<usize> = key_to_index.insert(key_hash, idx);
75 let pos: [f32; 3] = anchor.get_position();
76 let rad: f32 = anchor.get_bounding_radius().max(0.0);
77 let min_cell: (i32, i32, i32) =
78 world_to_prefetch_cell([pos[0] - rad, pos[1] - rad, pos[2] - rad], *cell_size);
79 let max_cell: (i32, i32, i32) =
80 world_to_prefetch_cell([pos[0] + rad, pos[1] + rad, pos[2] + rad], *cell_size);
81 for cx in min_cell.0..=max_cell.0 {
82 for cy in min_cell.1..=max_cell.1 {
83 for cz in min_cell.2..=max_cell.2 {
84 cells.entry((cx, cy, cz)).or_default().push(idx);
85 }
86 }
87 }
88 }
89
90 pub fn unregister_anchor(&mut self, key_hash: u64) -> bool {
100 let Self {
101 cell_size,
102 anchors,
103 key_to_index,
104 cells,
105 active_prefetches,
106 } = self;
107 let Some(idx) = key_to_index.remove(&key_hash) else {
108 return false;
109 };
110 let _: bool = active_prefetches.remove(&key_hash);
111 let Some(anchor) = anchors.get_mut(idx).and_then(Option::take) else {
112 return false;
113 };
114 let pos: [f32; 3] = anchor.get_position();
115 let rad: f32 = anchor.get_bounding_radius().max(0.0);
116 let min_cell: (i32, i32, i32) =
117 world_to_prefetch_cell([pos[0] - rad, pos[1] - rad, pos[2] - rad], *cell_size);
118 let max_cell: (i32, i32, i32) =
119 world_to_prefetch_cell([pos[0] + rad, pos[1] + rad, pos[2] + rad], *cell_size);
120 for cx in min_cell.0..=max_cell.0 {
121 for cy in min_cell.1..=max_cell.1 {
122 for cz in min_cell.2..=max_cell.2 {
123 if let Some(bucket) = cells.get_mut(&(cx, cy, cz)) {
124 bucket.retain(|&entry_idx: &usize| entry_idx != idx);
125 }
126 }
127 }
128 }
129 true
130 }
131
132 #[must_use]
138 pub fn anchor_count(&self) -> usize {
139 let Self { key_to_index, .. } = self;
140 key_to_index.len()
141 }
142
143 pub fn mark_prefetched(&mut self, key_hash: u64) {
149 let Self {
150 active_prefetches, ..
151 } = self;
152 let _: bool = active_prefetches.insert(key_hash);
153 }
154
155 pub fn unmark_prefetched(&mut self, key_hash: u64) {
161 let Self {
162 active_prefetches, ..
163 } = self;
164 let _: bool = active_prefetches.remove(&key_hash);
165 }
166
167 pub fn query_lookahead(
175 &self,
176 volume: &SpatialPrefetchVolume,
177 out_prefetch: &mut Vec<SpatialAssetAnchor>,
178 out_cancel: &mut Vec<u64>,
179 ) {
180 out_prefetch.clear();
181 out_cancel.clear();
182 let Self {
183 cell_size,
184 anchors,
185 key_to_index,
186 cells,
187 active_prefetches,
188 } = self;
189 let center: [f32; 3] = volume.get_center();
190 let velocity: [f32; 3] = volume.get_velocity();
191 let predicted: [f32; 3] = volume.predicted_center();
192 let radius: f32 = volume.get_radius().max(0.0);
193 let speed_sq: f32 = prefetch_dot_3d(velocity, velocity);
194 let speed: f32 = speed_sq.sqrt();
195 let min_cell: (i32, i32, i32) = world_to_prefetch_cell(
196 [
197 predicted[0] - radius,
198 predicted[1] - radius,
199 predicted[2] - radius,
200 ],
201 *cell_size,
202 );
203 let max_cell: (i32, i32, i32) = world_to_prefetch_cell(
204 [
205 predicted[0] + radius,
206 predicted[1] + radius,
207 predicted[2] + radius,
208 ],
209 *cell_size,
210 );
211 let mut visited: HashSet<usize> = HashSet::new();
212 let mut ranked: Vec<(f32, SpatialAssetAnchor)> = Vec::new();
213 for cx in min_cell.0..=max_cell.0 {
214 for cy in min_cell.1..=max_cell.1 {
215 for cz in min_cell.2..=max_cell.2 {
216 let Some(bucket) = cells.get(&(cx, cy, cz)) else {
217 continue;
218 };
219 for &anchor_idx in bucket.iter() {
220 if !visited.insert(anchor_idx) {
221 continue;
222 }
223 let Some(Some(anchor)) = anchors.get(anchor_idx) else {
224 continue;
225 };
226 let combined_r: f32 = radius + anchor.get_bounding_radius().max(0.0);
227 let dist_sq: f32 = prefetch_dist_sq_3d(anchor.get_position(), predicted);
228 if dist_sq <= combined_r * combined_r {
229 ranked.push((dist_sq, *anchor));
230 }
231 }
232 }
233 }
234 }
235 ranked.sort_by(
236 |left: &(f32, SpatialAssetAnchor), right: &(f32, SpatialAssetAnchor)| {
237 left.0.total_cmp(&right.0)
238 },
239 );
240 for (_, anchor) in ranked {
241 out_prefetch.push(anchor);
242 }
243 if speed >= PREFETCH_MIN_SPEED_EPSILON {
244 let dir: [f32; 3] = [
245 velocity[0] / speed,
246 velocity[1] / speed,
247 velocity[2] / speed,
248 ];
249 let rear_threshold: f32 = -(radius * PREFETCH_REAR_CANCEL_MARGIN_RATIO);
250 for &prefetched_key in active_prefetches.iter() {
251 let Some(&anchor_idx) = key_to_index.get(&prefetched_key) else {
252 continue;
253 };
254 let Some(Some(anchor)) = anchors.get(anchor_idx) else {
255 continue;
256 };
257 let anchor_pos: [f32; 3] = anchor.get_position();
258 let combined_r: f32 = radius + anchor.get_bounding_radius().max(0.0);
259 let in_lookahead: bool =
260 prefetch_dist_sq_3d(anchor_pos, predicted) <= combined_r * combined_r;
261 if in_lookahead {
262 continue;
263 }
264 let rel: [f32; 3] = [
265 anchor_pos[0] - center[0],
266 anchor_pos[1] - center[1],
267 anchor_pos[2] - center[2],
268 ];
269 let forward_proj: f32 = prefetch_dot_3d(rel, dir);
270 if forward_proj < rear_threshold {
271 out_cancel.push(prefetched_key);
272 }
273 }
274 }
275 }
276}