Skip to main content

euv_core/render/ring/
impl.rs

1use super::*;
2
3/// Default bounded GPU resource ring configured for 3 in-flight frames and 16 MiB scroll tile cache.
4impl Default for GpuResourceRing {
5    /// Creates a bounded GPU resource ring with default staging and tile budgets.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Initialized GPU resource ring.
10    fn default() -> Self {
11        Self::with_limits(
12            GPU_RING_DEFAULT_SLOTS,
13            GPU_RING_DEFAULT_MAX_TOTAL_BYTES,
14            GPU_TILE_CACHE_DEFAULT_BUDGET_BYTES,
15        )
16    }
17}
18
19/// Bounded staging ring acquisition, fence completion, persistent scroll tile caching, and deferred reclamation for `GpuResourceRing`.
20impl GpuResourceRing {
21    /// Creates a bounded GPU resource ring with explicit staging slot count, staging byte ceiling, and tile budget.
22    ///
23    /// # Arguments
24    ///
25    /// - `usize` - Maximum number of staging ring slots (`>= 1`).
26    /// - `usize` - Maximum aggregate staging bytes across all slots.
27    /// - `usize` - Maximum resident bytes for persistent scroll tile textures.
28    ///
29    /// # Returns
30    ///
31    /// - `Self` - Configured GPU resource ring.
32    #[must_use]
33    pub fn with_limits(max_slots: usize, max_total_bytes: usize, tile_budget_bytes: usize) -> Self {
34        let clamped_slots: usize = max_slots.max(1);
35        let clamped_staging: usize = max_total_bytes.max(4096);
36        let clamped_tiles: usize = tile_budget_bytes.max(GPU_TILE_CACHE_MIN_BUDGET_BYTES);
37        let mut slots: Vec<GpuStagingSlot> = Vec::with_capacity(clamped_slots);
38        for idx in 0..clamped_slots {
39            slots.push(GpuStagingSlot {
40                slot_index: idx,
41                handle: RenderResourceHandle::new(
42                    (idx as u64) + 1,
43                    1,
44                    RenderResourceKind::StagingSlot,
45                    0,
46                    0,
47                ),
48                capacity_bytes: 0,
49                used_bytes: 0,
50                fence_id: 0,
51                in_flight: false,
52                reuse_count: 0,
53            });
54        }
55        Self {
56            max_slots: clamped_slots,
57            max_total_bytes: clamped_staging,
58            slots,
59            next_fence_id: 1,
60            completed_fence_id: 0,
61            next_handle_id: (clamped_slots as u64) + 1,
62            cursor: 0,
63            total_allocations: 0,
64            total_reuses: 0,
65            dropped_prefetch_uploads: 0,
66            deferred_frame_uploads: 0,
67            tile_budget_bytes: clamped_tiles,
68            tile_resident_bytes: 0,
69            tile_clock: 0,
70            tile_hits: 0,
71            tile_misses: 0,
72            tile_evictions: 0,
73            tiles: Vec::new(),
74            pending_reclaim: Vec::new(),
75            pending_reclaim_bytes: 0,
76            reclaimed_count: 0,
77            pending_texture_uploads: Vec::new(),
78            total_texture_upload_bytes: 0,
79        }
80    }
81
82    /// Returns whether `fence_id` has retired relative to `completed_fence_id` with wrap-around safety.
83    ///
84    /// # Arguments
85    ///
86    /// - `u64` - Fence identifier guarding a GPU resource or staging slot.
87    /// - `u64` - Latest completed GPU fence identifier (`0` when no fence has completed yet).
88    ///
89    /// # Returns
90    ///
91    /// - `bool` - `true` when `fence_id` is retired and safe for CPU reuse.
92    #[must_use]
93    pub fn fence_is_completed(fence_id: u64, completed_fence_id: u64) -> bool {
94        if fence_id == 0 {
95            return true;
96        }
97        if completed_fence_id == 0 {
98            return false;
99        }
100        (completed_fence_id.wrapping_sub(fence_id) as i64) >= 0
101    }
102
103    /// Advances `fence_id` monotonically across the non-zero 64-bit fence space.
104    ///
105    /// # Arguments
106    ///
107    /// - `u64` - Current fence identifier.
108    ///
109    /// # Returns
110    ///
111    /// - `u64` - Next non-zero monotonic fence identifier.
112    #[must_use]
113    pub fn next_monotonic_fence(fence_id: u64) -> u64 {
114        let next: u64 = fence_id.wrapping_add(1);
115        if next == 0 { 1 } else { next }
116    }
117
118    /// Allocates the next monotonic `RenderResourceHandle` of `kind` and `byte_size`.
119    ///
120    /// # Arguments
121    ///
122    /// - `&Self` - Mutable GPU resource ring.
123    /// - `RenderResourceKind` - Resource category.
124    /// - `usize` - Resident byte size.
125    /// - `u64` - Associated GPU fence ID.
126    ///
127    /// # Returns
128    ///
129    /// - `RenderResourceHandle` - Fresh generation-1 handle.
130    fn alloc_handle(
131        &mut self,
132        kind: RenderResourceKind,
133        byte_size: usize,
134        fence_id: u64,
135    ) -> RenderResourceHandle {
136        let id: u64 = self.get_next_handle_id();
137        self.set_next_handle_id(id.wrapping_add(1).max(1));
138        RenderResourceHandle::new(id, 1, kind, byte_size, fence_id)
139    }
140
141    /// Acquires an idle or fence-completed staging slot for `required_bytes` without unbounded queue growth.
142    ///
143    /// # Arguments
144    ///
145    /// - `&Self` - Mutable GPU resource ring.
146    /// - `usize` - Upload payload size in bytes.
147    /// - `GpuUploadPriority` - Priority deciding whether a full ring drops prefetch or defers the frame.
148    ///
149    /// # Returns
150    ///
151    /// - `GpuRingAcquireResult` - Acquired slot and fence ID, or bounded backpressure status.
152    pub fn acquire_staging(
153        &mut self,
154        required_bytes: usize,
155        priority: GpuUploadPriority,
156    ) -> GpuRingAcquireResult {
157        let completed: u64 = self.get_completed_fence_id();
158        let slot_count: usize = self.get_slots().len();
159        let start: usize = self.get_cursor() % slot_count.max(1);
160        let mut chosen: Option<usize> = None;
161        for step in 0..slot_count {
162            let idx: usize = (start + step) % slot_count;
163            let slot: GpuStagingSlot = self.get_slots()[idx];
164            if !slot.get_in_flight() || Self::fence_is_completed(slot.get_fence_id(), completed) {
165                chosen = Some(idx);
166                break;
167            }
168        }
169        let Some(slot_idx) = chosen else {
170            return match priority {
171                GpuUploadPriority::OptionalPrefetch => {
172                    self.set_dropped_prefetch_uploads(
173                        self.get_dropped_prefetch_uploads().saturating_add(1),
174                    );
175                    GpuRingAcquireResult::DroppedPrefetchRingFull
176                }
177                GpuUploadPriority::RequiredFrame => {
178                    self.set_deferred_frame_uploads(
179                        self.get_deferred_frame_uploads().saturating_add(1),
180                    );
181                    GpuRingAcquireResult::DeferredFrameRingFull
182                }
183            };
184        };
185        let fence_id: u64 = self.get_next_fence_id();
186        self.set_next_fence_id(Self::next_monotonic_fence(fence_id));
187        let prev_slot: GpuStagingSlot = self.get_slots()[slot_idx];
188        let old_cap: usize = prev_slot.get_capacity_bytes();
189        let mut total_other_bytes: usize = 0;
190        for (idx, source_val) in self.get_slots().iter().enumerate() {
191            if idx != slot_idx {
192                total_other_bytes =
193                    total_other_bytes.saturating_add(source_val.get_capacity_bytes());
194            }
195        }
196        let needed_cap: usize = if required_bytes == 0 {
197            old_cap.max(4096)
198        } else {
199            required_bytes
200                .max(GPU_RING_DEFAULT_SLOT_BYTES.min(required_bytes.next_power_of_two()))
201                .next_power_of_two()
202        };
203        let mut reused: bool = old_cap >= required_bytes && old_cap > 0;
204        let new_cap: usize = if reused {
205            old_cap
206        } else {
207            let max_allowed: usize = self.get_max_total_bytes().saturating_sub(total_other_bytes);
208            if required_bytes > max_allowed && priority == GpuUploadPriority::OptionalPrefetch {
209                self.set_dropped_prefetch_uploads(
210                    self.get_dropped_prefetch_uploads().saturating_add(1),
211                );
212                return GpuRingAcquireResult::DroppedPrefetchRingFull;
213            }
214            if old_cap > 0 && !Self::fence_is_completed(prev_slot.get_fence_id(), completed) {
215                self.queue_deferred_reclaim(prev_slot.get_handle(), prev_slot.get_fence_id());
216            }
217            reused = false;
218            needed_cap.min(max_allowed.max(required_bytes))
219        };
220        let slot_ref: &mut GpuStagingSlot = &mut self.get_mut_slots()[slot_idx];
221        if !reused {
222            let next_gen: u32 = slot_ref
223                .get_handle()
224                .get_generation()
225                .wrapping_add(1)
226                .max(1);
227            let mut next_handle: RenderResourceHandle = slot_ref.get_handle();
228            next_handle.set_generation(next_gen);
229            next_handle.set_byte_size(new_cap);
230            next_handle.set_fence_id(fence_id);
231            slot_ref.set_handle(next_handle);
232            slot_ref.set_capacity_bytes(new_cap);
233        } else {
234            let mut next_handle: RenderResourceHandle = slot_ref.get_handle();
235            next_handle.set_fence_id(fence_id);
236            slot_ref.set_handle(next_handle);
237            slot_ref.set_reuse_count(slot_ref.get_reuse_count().saturating_add(1));
238        }
239        slot_ref.set_used_bytes(required_bytes);
240        slot_ref.set_fence_id(fence_id);
241        slot_ref.set_in_flight(true);
242        if reused {
243            self.set_total_reuses(self.get_total_reuses().saturating_add(1));
244        } else {
245            self.set_total_allocations(self.get_total_allocations().saturating_add(1));
246        }
247        self.set_cursor((slot_idx + 1) % slot_count.max(1));
248        GpuRingAcquireResult::Acquired {
249            slot_index: slot_idx,
250            fence_id,
251            reused,
252        }
253    }
254
255    /// Marks GPU fence `fence_id` complete, releasing finished staging slots and reclaiming deferred GPU resources.
256    ///
257    /// # Arguments
258    ///
259    /// - `&Self` - Mutable GPU resource ring.
260    /// - `u64` - Completed monotonic GPU fence identifier.
261    ///
262    /// # Returns
263    ///
264    /// - `usize` - Number of deferred GPU resources reclaimed by this fence completion.
265    pub fn complete_fence(&mut self, fence_id: u64) -> usize {
266        let prev_completed: u64 = self.get_completed_fence_id();
267        if fence_id != 0
268            && (prev_completed == 0 || (fence_id.wrapping_sub(prev_completed) as i64) > 0)
269        {
270            self.set_completed_fence_id(fence_id);
271        }
272        let completed: u64 = self.get_completed_fence_id();
273        for slot in self.get_mut_slots().iter_mut() {
274            if slot.get_in_flight() && Self::fence_is_completed(slot.get_fence_id(), completed) {
275                slot.set_in_flight(false);
276            }
277        }
278        let Self {
279            pending_reclaim,
280            pending_reclaim_bytes,
281            reclaimed_count,
282            ..
283        } = self;
284        let before: usize = pending_reclaim.len();
285        let mut reclaimed_bytes: usize = 0;
286        pending_reclaim.retain(|item: &DeferredGpuReclaim| {
287            if Self::fence_is_completed(item.get_retire_fence_id(), completed) {
288                reclaimed_bytes = reclaimed_bytes.saturating_add(item.get_byte_size());
289                false
290            } else {
291                true
292            }
293        });
294        let reclaimed_now: usize = before.saturating_sub(pending_reclaim.len());
295        *pending_reclaim_bytes = pending_reclaim_bytes.saturating_sub(reclaimed_bytes);
296        *reclaimed_count = reclaimed_count.saturating_add(reclaimed_now as u64);
297        reclaimed_now
298    }
299
300    /// Returns the number of staging slots currently in flight on the GPU.
301    ///
302    /// # Arguments
303    ///
304    /// - `&Self` - GPU resource ring reference.
305    ///
306    /// # Returns
307    ///
308    /// - `usize` - Count of in-flight staging slots.
309    #[must_use]
310    pub fn in_flight_slot_count(&self) -> usize {
311        let completed: u64 = self.get_completed_fence_id();
312        self.get_slots()
313            .iter()
314            .filter(|source_val: &&GpuStagingSlot| {
315                source_val.get_in_flight()
316                    && !Self::fence_is_completed(source_val.get_fence_id(), completed)
317            })
318            .count()
319    }
320
321    /// Returns total allocated bytes across all staging buffer slots.
322    ///
323    /// # Arguments
324    ///
325    /// - `&Self` - GPU resource ring reference.
326    ///
327    /// # Returns
328    ///
329    /// - `usize` - Aggregate staging buffer bytes.
330    #[must_use]
331    pub fn staging_allocated_bytes(&self) -> usize {
332        self.get_slots()
333            .iter()
334            .fold(0_usize, |acc: usize, source_val: &GpuStagingSlot| {
335                acc.saturating_add(source_val.get_capacity_bytes())
336            })
337    }
338
339    /// Queues an evicted GPU resource handle for fence-deferred reclamation.
340    ///
341    /// # Arguments
342    ///
343    /// - `&Self` - Mutable GPU resource ring.
344    /// - `RenderResourceHandle` - Evicted resource handle.
345    /// - `u64` - Fence ID that must complete before release.
346    fn queue_deferred_reclaim(&mut self, handle: RenderResourceHandle, retire_fence_id: u64) {
347        let bytes: usize = handle.get_byte_size();
348        if Self::fence_is_completed(retire_fence_id, self.get_completed_fence_id()) {
349            self.set_reclaimed_count(self.get_reclaimed_count().saturating_add(1));
350            return;
351        }
352        self.get_mut_pending_reclaim().push(DeferredGpuReclaim::new(
353            handle,
354            retire_fence_id,
355            bytes,
356        ));
357        self.set_pending_reclaim_bytes(self.get_pending_reclaim_bytes().saturating_add(bytes));
358    }
359
360    /// Marks all cached scroll tiles as non-visible before starting a new frame's visibility pass.
361    ///
362    /// # Arguments
363    ///
364    /// - `&Self` - Mutable GPU resource ring.
365    pub fn begin_tile_frame(&mut self) {
366        for tile in self.get_mut_tiles().iter_mut() {
367            tile.set_visible_this_frame(false);
368        }
369    }
370
371    /// Looks up or allocates a persistent scroll tile texture, touching LRU age and tagging `fence_id`.
372    ///
373    /// # Arguments
374    ///
375    /// - `&Self` - Mutable GPU resource ring.
376    /// - `GpuScrollTileKey` - Content/DPI/layer key for the tile.
377    /// - `u32` - Tile physical width in pixels.
378    /// - `u32` - Tile physical height in pixels.
379    /// - `u64` - Active frame GPU fence identifier.
380    /// - `bool` - Whether this tile is visible in the current viewport.
381    ///
382    /// # Returns
383    ///
384    /// - `(RenderResourceHandle, bool)` - Tile handle and `true` on cache hit (`false` on fresh miss).
385    pub fn acquire_scroll_tile(
386        &mut self,
387        key: GpuScrollTileKey,
388        width: u32,
389        height: u32,
390        fence_id: u64,
391        visible: bool,
392    ) -> (RenderResourceHandle, bool) {
393        let next_tick: u64 = self.get_tile_clock().wrapping_add(1);
394        self.set_tile_clock(next_tick);
395        let hit_idx: Option<usize> = self
396            .get_tiles()
397            .iter()
398            .position(|entry: &GpuScrollTileEntry| entry.get_key() == key);
399        if let Some(idx) = hit_idx {
400            let tile_ref: &mut GpuScrollTileEntry = &mut self.get_mut_tiles()[idx];
401            tile_ref.set_last_used_tick(next_tick);
402            tile_ref.set_last_fence_id(fence_id.max(tile_ref.get_last_fence_id()));
403            if visible {
404                tile_ref.set_visible_this_frame(true);
405            }
406            let handle: RenderResourceHandle = tile_ref.get_handle();
407            self.set_tile_hits(self.get_tile_hits().saturating_add(1));
408            return (handle, true);
409        }
410        let byte_size: usize = (width.max(1) as usize)
411            .saturating_mul(height.max(1) as usize)
412            .saturating_mul(4);
413        let handle: RenderResourceHandle =
414            self.alloc_handle(RenderResourceKind::TileLayer, byte_size, fence_id);
415        self.get_mut_tiles().push(GpuScrollTileEntry {
416            key,
417            handle,
418            width: width.max(1),
419            height: height.max(1),
420            byte_size,
421            last_used_tick: next_tick,
422            last_fence_id: fence_id,
423            visible_this_frame: visible,
424        });
425        self.set_tile_resident_bytes(self.get_tile_resident_bytes().saturating_add(byte_size));
426        self.set_tile_misses(self.get_tile_misses().saturating_add(1));
427        self.evict_cold_tiles_to_budget(self.get_tile_budget_bytes());
428        (handle, false)
429    }
430
431    /// Invalidates all cached scroll tiles belonging to a destroyed or invalidated layer node or old DPI scale.
432    ///
433    /// # Arguments
434    ///
435    /// - `&Self` - Mutable GPU resource ring.
436    /// - `Option<NodeId>` - Optional specific layer node to invalidate (`None` invalidates all non-matching DPI tiles).
437    /// - `Option<f32>` - Optional active DPI scale; tiles with a different scale are evicted cleanly.
438    ///
439    /// # Returns
440    ///
441    /// - `usize` - Number of invalidated tiles moved to fence-deferred reclamation.
442    pub fn invalidate_scroll_tiles(
443        &mut self,
444        layer_node: Option<NodeId>,
445        active_dpr: Option<f32>,
446    ) -> usize {
447        let target_dpr_bits: Option<u32> =
448            active_dpr.map(|delta_val: f32| delta_val.max(0.5).to_bits());
449        let mut evicted: Vec<GpuScrollTileEntry> = Vec::new();
450        self.get_mut_tiles().retain(|entry: &GpuScrollTileEntry| {
451            let match_node: bool = layer_node.is_some_and(|nid: NodeId| {
452                entry.get_key().get_layer_node_index() == nid.get_index()
453                    && entry.get_key().get_layer_node_gen() == nid.get_generation()
454            });
455            let mismatch_dpr: bool =
456                target_dpr_bits.is_some_and(|bits: u32| entry.get_key().get_dpr_bits() != bits);
457            if match_node || mismatch_dpr {
458                evicted.push(*entry);
459                false
460            } else {
461                true
462            }
463        });
464        let count: usize = evicted.len();
465        for item in evicted.into_iter() {
466            self.set_tile_resident_bytes(
467                self.get_tile_resident_bytes()
468                    .saturating_sub(item.get_byte_size()),
469            );
470            self.set_tile_evictions(self.get_tile_evictions().saturating_add(1));
471            self.queue_deferred_reclaim(item.get_handle(), item.get_last_fence_id());
472        }
473        count
474    }
475
476    /// Trims invisible cold scroll tiles down to `target_bytes`, never evicting currently visible tiles.
477    ///
478    /// # Arguments
479    ///
480    /// - `&Self` - Mutable GPU resource ring.
481    /// - `usize` - Target resident VRAM ceiling in bytes.
482    ///
483    /// # Returns
484    ///
485    /// - `usize` - Number of cold tiles evicted.
486    pub fn evict_cold_tiles_to_budget(&mut self, target_bytes: usize) -> usize {
487        if self.get_tile_resident_bytes() <= target_bytes || self.get_tiles().is_empty() {
488            return 0;
489        }
490        let mut cold_indices: Vec<(u64, usize)> = Vec::new();
491        for (idx, tile) in self.get_tiles().iter().enumerate() {
492            if !tile.get_visible_this_frame() {
493                cold_indices.push((tile.get_last_used_tick(), idx));
494            }
495        }
496        cold_indices.sort_unstable_by_key(|pair: &(u64, usize)| pair.0);
497        let mut drop_flags: Vec<bool> = vec![false; self.get_tiles().len()];
498        let mut rem_bytes: usize = self.get_tile_resident_bytes();
499        let mut dropped_count: usize = 0;
500        for (_, idx) in cold_indices.into_iter() {
501            if rem_bytes <= target_bytes {
502                break;
503            }
504            drop_flags[idx] = true;
505            rem_bytes = rem_bytes.saturating_sub(self.get_tiles()[idx].get_byte_size());
506            dropped_count += 1;
507        }
508        if dropped_count == 0 {
509            return 0;
510        }
511        let mut evicted: Vec<GpuScrollTileEntry> = Vec::with_capacity(dropped_count);
512        let mut write_idx: usize = 0;
513        for (read_idx, &dropped) in drop_flags.iter().enumerate() {
514            if dropped {
515                evicted.push(self.get_tiles()[read_idx]);
516            } else {
517                if write_idx != read_idx {
518                    self.get_mut_tiles().swap(write_idx, read_idx);
519                }
520                write_idx += 1;
521            }
522        }
523        self.get_mut_tiles().truncate(write_idx);
524        for item in evicted.into_iter() {
525            self.set_tile_resident_bytes(
526                self.get_tile_resident_bytes()
527                    .saturating_sub(item.get_byte_size()),
528            );
529            self.set_tile_evictions(self.get_tile_evictions().saturating_add(1));
530            self.queue_deferred_reclaim(item.get_handle(), item.get_last_fence_id());
531        }
532        dropped_count
533    }
534
535    /// Shrinks the persistent tile cache budget under memory pressure and evicts invisible cold tiles immediately.
536    ///
537    /// # Arguments
538    ///
539    /// - `&Self` - Mutable GPU resource ring.
540    /// - `f32` - Retained budget fraction in `(0.0..=1.0]`.
541    ///
542    /// # Returns
543    ///
544    /// - `usize` - Number of cold tiles evicted.
545    pub fn apply_retention_ratio(&mut self, retain_ratio: f32) -> usize {
546        let clamped: f32 = retain_ratio.clamp(0.1, 1.0);
547        let target: usize = ((GPU_TILE_CACHE_DEFAULT_BUDGET_BYTES as f32) * clamped)
548            .round()
549            .max(GPU_TILE_CACHE_MIN_BUDGET_BYTES as f32) as usize;
550        self.set_tile_budget_bytes(target);
551        self.evict_cold_tiles_to_budget(target)
552    }
553
554    /// Returns total live GPU bytes combining staging slots, resident scroll tiles, and pending fence-deferred bytes.
555    ///
556    /// # Arguments
557    ///
558    /// - `&Self` - GPU resource ring reference.
559    ///
560    /// # Returns
561    ///
562    /// - `usize` - Total live GPU allocation bytes.
563    #[must_use]
564    pub fn total_live_gpu_bytes(&self) -> usize {
565        self.staging_allocated_bytes()
566            .saturating_add(self.get_tile_resident_bytes())
567            .saturating_add(self.get_pending_reclaim_bytes())
568    }
569
570    /// Queues a dirty-rectangle R8 or RGBA8 atlas/image sub-region upload for the active frame.
571    ///
572    /// # Arguments
573    ///
574    /// - `GpuTextureUploadRect` - Dirty sub-region upload descriptor.
575    pub fn queue_texture_dirty_rect(&mut self, upload: GpuTextureUploadRect) {
576        if upload.get_width() == 0 || upload.get_height() == 0 {
577            return;
578        }
579        let bpp: u64 = match upload.get_format() {
580            GpuAtlasPixelFormat::R8Unorm => 1,
581            GpuAtlasPixelFormat::Rgba8Unorm => 4,
582        };
583        let bytes: u64 = (upload.get_width() as u64)
584            .saturating_mul(upload.get_height() as u64)
585            .saturating_mul(bpp);
586        self.set_total_texture_upload_bytes(
587            self.get_total_texture_upload_bytes().saturating_add(bytes),
588        );
589        self.get_mut_pending_texture_uploads().push(upload);
590    }
591
592    /// Drains all queued dirty-rectangle R8/RGBA8 texture uploads for GPU command encoding.
593    ///
594    /// # Returns
595    ///
596    /// - `Vec<GpuTextureUploadRect>` - Pending dirty-rectangle sub-region uploads.
597    #[must_use]
598    pub fn drain_texture_uploads(&mut self) -> Vec<GpuTextureUploadRect> {
599        take(self.get_mut_pending_texture_uploads())
600    }
601
602    /// Synchronizes retired ring slots and deferred reclamations against the real hardware GPU completion fence (`completed_gpu_fence()`).
603    ///
604    /// # Returns
605    ///
606    /// - `usize` - Number of deferred GPU resources reclaimed.
607    pub fn sync_hardware_completed_fence(&mut self) -> usize {
608        let hw_completed: u64 = completed_gpu_fence();
609        if hw_completed > self.get_completed_fence_id() {
610            self.complete_fence(hw_completed)
611        } else {
612            0
613        }
614    }
615}