euv-core 0.48.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
use super::*;

/// Default bounded GPU resource ring configured for 3 in-flight frames and 16 MiB scroll tile cache.
impl Default for GpuResourceRing {
    /// Creates a bounded GPU resource ring with default staging and tile budgets.
    ///
    /// # Returns
    ///
    /// - `Self` - Initialized GPU resource ring.
    fn default() -> Self {
        Self::with_limits(
            GPU_RING_DEFAULT_SLOTS,
            GPU_RING_DEFAULT_MAX_TOTAL_BYTES,
            GPU_TILE_CACHE_DEFAULT_BUDGET_BYTES,
        )
    }
}

/// Bounded staging ring acquisition, fence completion, persistent scroll tile caching, and deferred reclamation for `GpuResourceRing`.
impl GpuResourceRing {
    /// Creates a bounded GPU resource ring with explicit staging slot count, staging byte ceiling, and tile budget.
    ///
    /// # Arguments
    ///
    /// - `usize` - Maximum number of staging ring slots (`>= 1`).
    /// - `usize` - Maximum aggregate staging bytes across all slots.
    /// - `usize` - Maximum resident bytes for persistent scroll tile textures.
    ///
    /// # Returns
    ///
    /// - `Self` - Configured GPU resource ring.
    #[must_use]
    pub fn with_limits(max_slots: usize, max_total_bytes: usize, tile_budget_bytes: usize) -> Self {
        let clamped_slots: usize = max_slots.max(1);
        let clamped_staging: usize = max_total_bytes.max(4096);
        let clamped_tiles: usize = tile_budget_bytes.max(GPU_TILE_CACHE_MIN_BUDGET_BYTES);
        let mut slots: Vec<GpuStagingSlot> = Vec::with_capacity(clamped_slots);
        for idx in 0..clamped_slots {
            slots.push(GpuStagingSlot {
                slot_index: idx,
                handle: RenderResourceHandle::new(
                    (idx as u64) + 1,
                    1,
                    RenderResourceKind::StagingSlot,
                    0,
                    0,
                ),
                capacity_bytes: 0,
                used_bytes: 0,
                fence_id: 0,
                in_flight: false,
                reuse_count: 0,
            });
        }
        Self {
            max_slots: clamped_slots,
            max_total_bytes: clamped_staging,
            slots,
            next_fence_id: 1,
            completed_fence_id: 0,
            next_handle_id: (clamped_slots as u64) + 1,
            cursor: 0,
            total_allocations: 0,
            total_reuses: 0,
            dropped_prefetch_uploads: 0,
            deferred_frame_uploads: 0,
            tile_budget_bytes: clamped_tiles,
            tile_resident_bytes: 0,
            tile_clock: 0,
            tile_hits: 0,
            tile_misses: 0,
            tile_evictions: 0,
            tiles: Vec::new(),
            pending_reclaim: Vec::new(),
            pending_reclaim_bytes: 0,
            reclaimed_count: 0,
            pending_texture_uploads: Vec::new(),
            total_texture_upload_bytes: 0,
        }
    }

    /// Returns whether `fence_id` has retired relative to `completed_fence_id` with wrap-around safety.
    ///
    /// # Arguments
    ///
    /// - `u64` - Fence identifier guarding a GPU resource or staging slot.
    /// - `u64` - Latest completed GPU fence identifier (`0` when no fence has completed yet).
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when `fence_id` is retired and safe for CPU reuse.
    #[must_use]
    pub fn fence_is_completed(fence_id: u64, completed_fence_id: u64) -> bool {
        if fence_id == 0 {
            return true;
        }
        if completed_fence_id == 0 {
            return false;
        }
        (completed_fence_id.wrapping_sub(fence_id) as i64) >= 0
    }

    /// Advances `fence_id` monotonically across the non-zero 64-bit fence space.
    ///
    /// # Arguments
    ///
    /// - `u64` - Current fence identifier.
    ///
    /// # Returns
    ///
    /// - `u64` - Next non-zero monotonic fence identifier.
    #[must_use]
    pub fn next_monotonic_fence(fence_id: u64) -> u64 {
        let next: u64 = fence_id.wrapping_add(1);
        if next == 0 { 1 } else { next }
    }

    /// Allocates the next monotonic `RenderResourceHandle` of `kind` and `byte_size`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable GPU resource ring.
    /// - `RenderResourceKind` - Resource category.
    /// - `usize` - Resident byte size.
    /// - `u64` - Associated GPU fence ID.
    ///
    /// # Returns
    ///
    /// - `RenderResourceHandle` - Fresh generation-1 handle.
    fn alloc_handle(
        &mut self,
        kind: RenderResourceKind,
        byte_size: usize,
        fence_id: u64,
    ) -> RenderResourceHandle {
        let id: u64 = self.get_next_handle_id();
        self.set_next_handle_id(id.wrapping_add(1).max(1));
        RenderResourceHandle::new(id, 1, kind, byte_size, fence_id)
    }

    /// Acquires an idle or fence-completed staging slot for `required_bytes` without unbounded queue growth.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable GPU resource ring.
    /// - `usize` - Upload payload size in bytes.
    /// - `GpuUploadPriority` - Priority deciding whether a full ring drops prefetch or defers the frame.
    ///
    /// # Returns
    ///
    /// - `GpuRingAcquireResult` - Acquired slot and fence ID, or bounded backpressure status.
    pub fn acquire_staging(
        &mut self,
        required_bytes: usize,
        priority: GpuUploadPriority,
    ) -> GpuRingAcquireResult {
        let completed: u64 = self.get_completed_fence_id();
        let slot_count: usize = self.get_slots().len();
        let start: usize = self.get_cursor() % slot_count.max(1);
        let mut chosen: Option<usize> = None;
        for step in 0..slot_count {
            let idx: usize = (start + step) % slot_count;
            let slot: GpuStagingSlot = self.get_slots()[idx];
            if !slot.get_in_flight() || Self::fence_is_completed(slot.get_fence_id(), completed) {
                chosen = Some(idx);
                break;
            }
        }
        let Some(slot_idx) = chosen else {
            return match priority {
                GpuUploadPriority::OptionalPrefetch => {
                    self.set_dropped_prefetch_uploads(
                        self.get_dropped_prefetch_uploads().saturating_add(1),
                    );
                    GpuRingAcquireResult::DroppedPrefetchRingFull
                }
                GpuUploadPriority::RequiredFrame => {
                    self.set_deferred_frame_uploads(
                        self.get_deferred_frame_uploads().saturating_add(1),
                    );
                    GpuRingAcquireResult::DeferredFrameRingFull
                }
            };
        };
        let fence_id: u64 = self.get_next_fence_id();
        self.set_next_fence_id(Self::next_monotonic_fence(fence_id));
        let prev_slot: GpuStagingSlot = self.get_slots()[slot_idx];
        let old_cap: usize = prev_slot.get_capacity_bytes();
        let mut total_other_bytes: usize = 0;
        for (idx, s) in self.get_slots().iter().enumerate() {
            if idx != slot_idx {
                total_other_bytes = total_other_bytes.saturating_add(s.get_capacity_bytes());
            }
        }
        let needed_cap: usize = if required_bytes == 0 {
            old_cap.max(4096)
        } else {
            required_bytes
                .max(GPU_RING_DEFAULT_SLOT_BYTES.min(required_bytes.next_power_of_two()))
                .next_power_of_two()
        };
        let mut reused: bool = old_cap >= required_bytes && old_cap > 0;
        let new_cap: usize = if reused {
            old_cap
        } else {
            let max_allowed: usize = self.get_max_total_bytes().saturating_sub(total_other_bytes);
            if required_bytes > max_allowed && priority == GpuUploadPriority::OptionalPrefetch {
                self.set_dropped_prefetch_uploads(
                    self.get_dropped_prefetch_uploads().saturating_add(1),
                );
                return GpuRingAcquireResult::DroppedPrefetchRingFull;
            }
            if old_cap > 0 && !Self::fence_is_completed(prev_slot.get_fence_id(), completed) {
                self.queue_deferred_reclaim(prev_slot.get_handle(), prev_slot.get_fence_id());
            }
            reused = false;
            needed_cap.min(max_allowed.max(required_bytes))
        };
        let slot_ref: &mut GpuStagingSlot = &mut self.get_mut_slots()[slot_idx];
        if !reused {
            let next_gen: u32 = slot_ref
                .get_handle()
                .get_generation()
                .wrapping_add(1)
                .max(1);
            let mut next_handle: RenderResourceHandle = slot_ref.get_handle();
            next_handle.set_generation(next_gen);
            next_handle.set_byte_size(new_cap);
            next_handle.set_fence_id(fence_id);
            slot_ref.set_handle(next_handle);
            slot_ref.set_capacity_bytes(new_cap);
        } else {
            let mut next_handle: RenderResourceHandle = slot_ref.get_handle();
            next_handle.set_fence_id(fence_id);
            slot_ref.set_handle(next_handle);
            slot_ref.set_reuse_count(slot_ref.get_reuse_count().saturating_add(1));
        }
        slot_ref.set_used_bytes(required_bytes);
        slot_ref.set_fence_id(fence_id);
        slot_ref.set_in_flight(true);
        if reused {
            self.set_total_reuses(self.get_total_reuses().saturating_add(1));
        } else {
            self.set_total_allocations(self.get_total_allocations().saturating_add(1));
        }
        self.set_cursor((slot_idx + 1) % slot_count.max(1));
        GpuRingAcquireResult::Acquired {
            slot_index: slot_idx,
            fence_id,
            reused,
        }
    }

    /// Marks GPU fence `fence_id` complete, releasing finished staging slots and reclaiming deferred GPU resources.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable GPU resource ring.
    /// - `u64` - Completed monotonic GPU fence identifier.
    ///
    /// # Returns
    ///
    /// - `usize` - Number of deferred GPU resources reclaimed by this fence completion.
    pub fn complete_fence(&mut self, fence_id: u64) -> usize {
        let prev_completed: u64 = self.get_completed_fence_id();
        if fence_id != 0
            && (prev_completed == 0 || (fence_id.wrapping_sub(prev_completed) as i64) > 0)
        {
            self.set_completed_fence_id(fence_id);
        }
        let completed: u64 = self.get_completed_fence_id();
        for slot in self.get_mut_slots().iter_mut() {
            if slot.get_in_flight() && Self::fence_is_completed(slot.get_fence_id(), completed) {
                slot.set_in_flight(false);
            }
        }
        let Self {
            pending_reclaim,
            pending_reclaim_bytes,
            reclaimed_count,
            ..
        } = self;
        let before: usize = pending_reclaim.len();
        let mut reclaimed_bytes: usize = 0;
        pending_reclaim.retain(|item: &DeferredGpuReclaim| {
            if Self::fence_is_completed(item.get_retire_fence_id(), completed) {
                reclaimed_bytes = reclaimed_bytes.saturating_add(item.get_byte_size());
                false
            } else {
                true
            }
        });
        let reclaimed_now: usize = before.saturating_sub(pending_reclaim.len());
        *pending_reclaim_bytes = pending_reclaim_bytes.saturating_sub(reclaimed_bytes);
        *reclaimed_count = reclaimed_count.saturating_add(reclaimed_now as u64);
        reclaimed_now
    }

    /// Returns the number of staging slots currently in flight on the GPU.
    ///
    /// # Arguments
    ///
    /// - `&Self` - GPU resource ring reference.
    ///
    /// # Returns
    ///
    /// - `usize` - Count of in-flight staging slots.
    #[must_use]
    pub fn in_flight_slot_count(&self) -> usize {
        let completed: u64 = self.get_completed_fence_id();
        self.get_slots()
            .iter()
            .filter(|s: &&GpuStagingSlot| {
                s.get_in_flight() && !Self::fence_is_completed(s.get_fence_id(), completed)
            })
            .count()
    }

    /// Returns total allocated bytes across all staging buffer slots.
    ///
    /// # Arguments
    ///
    /// - `&Self` - GPU resource ring reference.
    ///
    /// # Returns
    ///
    /// - `usize` - Aggregate staging buffer bytes.
    #[must_use]
    pub fn staging_allocated_bytes(&self) -> usize {
        self.get_slots()
            .iter()
            .fold(0_usize, |acc: usize, s: &GpuStagingSlot| {
                acc.saturating_add(s.get_capacity_bytes())
            })
    }

    /// Queues an evicted GPU resource handle for fence-deferred reclamation.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable GPU resource ring.
    /// - `RenderResourceHandle` - Evicted resource handle.
    /// - `u64` - Fence ID that must complete before release.
    fn queue_deferred_reclaim(&mut self, handle: RenderResourceHandle, retire_fence_id: u64) {
        let bytes: usize = handle.get_byte_size();
        if Self::fence_is_completed(retire_fence_id, self.get_completed_fence_id()) {
            self.set_reclaimed_count(self.get_reclaimed_count().saturating_add(1));
            return;
        }
        self.get_mut_pending_reclaim().push(DeferredGpuReclaim::new(
            handle,
            retire_fence_id,
            bytes,
        ));
        self.set_pending_reclaim_bytes(self.get_pending_reclaim_bytes().saturating_add(bytes));
    }

    /// Marks all cached scroll tiles as non-visible before starting a new frame's visibility pass.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable GPU resource ring.
    pub fn begin_tile_frame(&mut self) {
        for tile in self.get_mut_tiles().iter_mut() {
            tile.set_visible_this_frame(false);
        }
    }

    /// Looks up or allocates a persistent scroll tile texture, touching LRU age and tagging `fence_id`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable GPU resource ring.
    /// - `GpuScrollTileKey` - Content/DPI/layer key for the tile.
    /// - `u32` - Tile physical width in pixels.
    /// - `u32` - Tile physical height in pixels.
    /// - `u64` - Active frame GPU fence identifier.
    /// - `bool` - Whether this tile is visible in the current viewport.
    ///
    /// # Returns
    ///
    /// - `(RenderResourceHandle, bool)` - Tile handle and `true` on cache hit (`false` on fresh miss).
    pub fn acquire_scroll_tile(
        &mut self,
        key: GpuScrollTileKey,
        width: u32,
        height: u32,
        fence_id: u64,
        visible: bool,
    ) -> (RenderResourceHandle, bool) {
        let next_tick: u64 = self.get_tile_clock().wrapping_add(1);
        self.set_tile_clock(next_tick);
        let hit_idx: Option<usize> = self
            .get_tiles()
            .iter()
            .position(|entry: &GpuScrollTileEntry| entry.get_key() == key);
        if let Some(idx) = hit_idx {
            let tile_ref: &mut GpuScrollTileEntry = &mut self.get_mut_tiles()[idx];
            tile_ref.set_last_used_tick(next_tick);
            tile_ref.set_last_fence_id(fence_id.max(tile_ref.get_last_fence_id()));
            if visible {
                tile_ref.set_visible_this_frame(true);
            }
            let handle: RenderResourceHandle = tile_ref.get_handle();
            self.set_tile_hits(self.get_tile_hits().saturating_add(1));
            return (handle, true);
        }
        let byte_size: usize = (width.max(1) as usize)
            .saturating_mul(height.max(1) as usize)
            .saturating_mul(4);
        let handle: RenderResourceHandle =
            self.alloc_handle(RenderResourceKind::TileLayer, byte_size, fence_id);
        self.get_mut_tiles().push(GpuScrollTileEntry {
            key,
            handle,
            width: width.max(1),
            height: height.max(1),
            byte_size,
            last_used_tick: next_tick,
            last_fence_id: fence_id,
            visible_this_frame: visible,
        });
        self.set_tile_resident_bytes(self.get_tile_resident_bytes().saturating_add(byte_size));
        self.set_tile_misses(self.get_tile_misses().saturating_add(1));
        self.evict_cold_tiles_to_budget(self.get_tile_budget_bytes());
        (handle, false)
    }

    /// Invalidates all cached scroll tiles belonging to a destroyed or invalidated layer node or old DPI scale.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable GPU resource ring.
    /// - `Option<NodeId>` - Optional specific layer node to invalidate (`None` invalidates all non-matching DPI tiles).
    /// - `Option<f32>` - Optional active DPI scale; tiles with a different scale are evicted cleanly.
    ///
    /// # Returns
    ///
    /// - `usize` - Number of invalidated tiles moved to fence-deferred reclamation.
    pub fn invalidate_scroll_tiles(
        &mut self,
        layer_node: Option<NodeId>,
        active_dpr: Option<f32>,
    ) -> usize {
        let target_dpr_bits: Option<u32> = active_dpr.map(|d: f32| d.max(0.5).to_bits());
        let mut evicted: Vec<GpuScrollTileEntry> = Vec::new();
        self.get_mut_tiles().retain(|entry: &GpuScrollTileEntry| {
            let match_node: bool = layer_node.is_some_and(|nid: NodeId| {
                entry.get_key().get_layer_node_index() == nid.get_index()
                    && entry.get_key().get_layer_node_gen() == nid.get_generation()
            });
            let mismatch_dpr: bool =
                target_dpr_bits.is_some_and(|bits: u32| entry.get_key().get_dpr_bits() != bits);
            if match_node || mismatch_dpr {
                evicted.push(*entry);
                false
            } else {
                true
            }
        });
        let count: usize = evicted.len();
        for item in evicted.into_iter() {
            self.set_tile_resident_bytes(
                self.get_tile_resident_bytes()
                    .saturating_sub(item.get_byte_size()),
            );
            self.set_tile_evictions(self.get_tile_evictions().saturating_add(1));
            self.queue_deferred_reclaim(item.get_handle(), item.get_last_fence_id());
        }
        count
    }

    /// Trims invisible cold scroll tiles down to `target_bytes`, never evicting currently visible tiles.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable GPU resource ring.
    /// - `usize` - Target resident VRAM ceiling in bytes.
    ///
    /// # Returns
    ///
    /// - `usize` - Number of cold tiles evicted.
    pub fn evict_cold_tiles_to_budget(&mut self, target_bytes: usize) -> usize {
        if self.get_tile_resident_bytes() <= target_bytes || self.get_tiles().is_empty() {
            return 0;
        }
        let mut cold_indices: Vec<(u64, usize)> = Vec::new();
        for (idx, tile) in self.get_tiles().iter().enumerate() {
            if !tile.get_visible_this_frame() {
                cold_indices.push((tile.get_last_used_tick(), idx));
            }
        }
        cold_indices.sort_unstable_by_key(|pair: &(u64, usize)| pair.0);
        let mut drop_flags: Vec<bool> = vec![false; self.get_tiles().len()];
        let mut rem_bytes: usize = self.get_tile_resident_bytes();
        let mut dropped_count: usize = 0;
        for (_, idx) in cold_indices.into_iter() {
            if rem_bytes <= target_bytes {
                break;
            }
            drop_flags[idx] = true;
            rem_bytes = rem_bytes.saturating_sub(self.get_tiles()[idx].get_byte_size());
            dropped_count += 1;
        }
        if dropped_count == 0 {
            return 0;
        }
        let mut evicted: Vec<GpuScrollTileEntry> = Vec::with_capacity(dropped_count);
        let mut write_idx: usize = 0;
        for (read_idx, &dropped) in drop_flags.iter().enumerate() {
            if dropped {
                evicted.push(self.get_tiles()[read_idx]);
            } else {
                if write_idx != read_idx {
                    self.get_mut_tiles().swap(write_idx, read_idx);
                }
                write_idx += 1;
            }
        }
        self.get_mut_tiles().truncate(write_idx);
        for item in evicted.into_iter() {
            self.set_tile_resident_bytes(
                self.get_tile_resident_bytes()
                    .saturating_sub(item.get_byte_size()),
            );
            self.set_tile_evictions(self.get_tile_evictions().saturating_add(1));
            self.queue_deferred_reclaim(item.get_handle(), item.get_last_fence_id());
        }
        dropped_count
    }

    /// Shrinks the persistent tile cache budget under memory pressure and evicts invisible cold tiles immediately.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable GPU resource ring.
    /// - `f32` - Retained budget fraction in `(0.0..=1.0]`.
    ///
    /// # Returns
    ///
    /// - `usize` - Number of cold tiles evicted.
    pub fn apply_retention_ratio(&mut self, retain_ratio: f32) -> usize {
        let clamped: f32 = retain_ratio.clamp(0.1, 1.0);
        let target: usize = ((GPU_TILE_CACHE_DEFAULT_BUDGET_BYTES as f32) * clamped)
            .round()
            .max(GPU_TILE_CACHE_MIN_BUDGET_BYTES as f32) as usize;
        self.set_tile_budget_bytes(target);
        self.evict_cold_tiles_to_budget(target)
    }

    /// Returns total live GPU bytes combining staging slots, resident scroll tiles, and pending fence-deferred bytes.
    ///
    /// # Arguments
    ///
    /// - `&Self` - GPU resource ring reference.
    ///
    /// # Returns
    ///
    /// - `usize` - Total live GPU allocation bytes.
    #[must_use]
    pub fn total_live_gpu_bytes(&self) -> usize {
        self.staging_allocated_bytes()
            .saturating_add(self.get_tile_resident_bytes())
            .saturating_add(self.get_pending_reclaim_bytes())
    }

    /// Queues a dirty-rectangle R8 or RGBA8 atlas/image sub-region upload for the active frame.
    ///
    /// # Arguments
    ///
    /// - `GpuTextureUploadRect` - Dirty sub-region upload descriptor.
    pub fn queue_texture_dirty_rect(&mut self, upload: GpuTextureUploadRect) {
        if upload.get_width() == 0 || upload.get_height() == 0 {
            return;
        }
        let bpp: u64 = match upload.get_format() {
            GpuAtlasPixelFormat::R8Unorm => 1,
            GpuAtlasPixelFormat::Rgba8Unorm => 4,
        };
        let bytes: u64 = (upload.get_width() as u64)
            .saturating_mul(upload.get_height() as u64)
            .saturating_mul(bpp);
        self.set_total_texture_upload_bytes(
            self.get_total_texture_upload_bytes().saturating_add(bytes),
        );
        self.get_mut_pending_texture_uploads().push(upload);
    }

    /// Drains all queued dirty-rectangle R8/RGBA8 texture uploads for GPU command encoding.
    ///
    /// # Returns
    ///
    /// - `Vec<GpuTextureUploadRect>` - Pending dirty-rectangle sub-region uploads.
    #[must_use]
    pub fn drain_texture_uploads(&mut self) -> Vec<GpuTextureUploadRect> {
        take(self.get_mut_pending_texture_uploads())
    }
}