oximedia-gpu 0.1.8

GPU compute pipeline using WGPU for OxiMedia - cross-platform acceleration
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
#![allow(dead_code)]
//! GPU fence pool for efficient synchronization primitive reuse.
//!
//! This module provides a pooled allocation strategy for GPU fences,
//! reducing the overhead of creating and destroying fence objects
//! on every frame submission.

use std::collections::VecDeque;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};

/// Unique identifier for a pooled fence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FenceId(
    /// Inner identifier value.
    pub u64,
);

/// Current status of a fence in the pool.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FenceStatus {
    /// Fence is available for reuse.
    Available,
    /// Fence has been submitted and is pending GPU completion.
    Pending,
    /// Fence has been signaled by the GPU.
    Signaled,
    /// Fence encountered an error.
    Error,
}

impl std::fmt::Display for FenceStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Available => write!(f, "Available"),
            Self::Pending => write!(f, "Pending"),
            Self::Signaled => write!(f, "Signaled"),
            Self::Error => write!(f, "Error"),
        }
    }
}

/// A single fence entry in the pool.
#[derive(Debug, Clone)]
pub struct PooledFence {
    /// Unique identifier.
    pub id: FenceId,
    /// Current status of this fence.
    pub status: FenceStatus,
    /// Timestamp when the fence was submitted.
    pub submit_time: Option<Instant>,
    /// Timestamp when the fence was signaled.
    pub signal_time: Option<Instant>,
    /// Number of times this fence has been recycled.
    pub recycle_count: u64,
}

impl PooledFence {
    /// Create a new pooled fence with the given ID.
    #[must_use]
    pub fn new(id: FenceId) -> Self {
        Self {
            id,
            status: FenceStatus::Available,
            submit_time: None,
            signal_time: None,
            recycle_count: 0,
        }
    }

    /// Return the latency between submit and signal, if both are recorded.
    #[must_use]
    pub fn latency(&self) -> Option<Duration> {
        match (self.submit_time, self.signal_time) {
            (Some(submit), Some(signal)) => Some(signal.duration_since(submit)),
            _ => None,
        }
    }

    /// Check if the fence is currently available for reuse.
    #[must_use]
    pub fn is_available(&self) -> bool {
        self.status == FenceStatus::Available
    }

    /// Check if the fence is pending GPU completion.
    #[must_use]
    pub fn is_pending(&self) -> bool {
        self.status == FenceStatus::Pending
    }
}

/// Configuration for the fence pool.
#[derive(Debug, Clone)]
pub struct FencePoolConfig {
    /// Initial number of fences to pre-allocate.
    pub initial_size: usize,
    /// Maximum number of fences allowed in the pool.
    pub max_size: usize,
    /// Timeout for fence wait operations.
    pub wait_timeout: Duration,
    /// Whether to auto-grow the pool when exhausted.
    pub auto_grow: bool,
    /// Batch size for growing the pool.
    pub grow_batch_size: usize,
}

impl Default for FencePoolConfig {
    fn default() -> Self {
        Self {
            initial_size: 16,
            max_size: 256,
            wait_timeout: Duration::from_secs(5),
            auto_grow: true,
            grow_batch_size: 8,
        }
    }
}

/// Statistics about fence pool usage.
#[derive(Debug, Clone, Default)]
pub struct FencePoolStats {
    /// Total number of fences in the pool.
    pub total_fences: usize,
    /// Number of fences currently available.
    pub available_count: usize,
    /// Number of fences currently pending.
    pub pending_count: usize,
    /// Number of fences currently signaled.
    pub signaled_count: usize,
    /// Total number of allocations performed.
    pub total_allocations: u64,
    /// Total number of recycles performed.
    pub total_recycles: u64,
    /// Number of times the pool had to grow.
    pub grow_events: u64,
    /// Average latency of signaled fences in microseconds.
    pub avg_latency_us: f64,
}

impl FencePoolStats {
    /// Return the utilization ratio of the pool (pending / total).
    #[allow(clippy::cast_precision_loss)]
    #[must_use]
    pub fn utilization(&self) -> f64 {
        if self.total_fences == 0 {
            return 0.0;
        }
        self.pending_count as f64 / self.total_fences as f64
    }
}

/// A pool of reusable GPU fence objects.
///
/// Manages fence lifecycle to avoid repeated allocation/deallocation overhead.
pub struct FencePool {
    /// All fences in the pool.
    fences: Vec<PooledFence>,
    /// Queue of available fence indices.
    available: VecDeque<usize>,
    /// Configuration for this pool.
    config: FencePoolConfig,
    /// Counter for generating unique fence IDs.
    next_id: AtomicU64,
    /// Total allocations performed.
    total_allocations: u64,
    /// Total recycles performed.
    total_recycles: u64,
    /// Number of grow events.
    grow_events: u64,
}

impl FencePool {
    /// Create a new fence pool with default configuration.
    #[must_use]
    pub fn new() -> Self {
        Self::with_config(FencePoolConfig::default())
    }

    /// Create a new fence pool with the given configuration.
    #[must_use]
    pub fn with_config(config: FencePoolConfig) -> Self {
        let mut pool = Self {
            fences: Vec::with_capacity(config.initial_size),
            available: VecDeque::with_capacity(config.initial_size),
            next_id: AtomicU64::new(0),
            total_allocations: 0,
            total_recycles: 0,
            grow_events: 0,
            config,
        };
        let initial = pool.config.initial_size;
        pool.grow(initial);
        pool
    }

    /// Grow the pool by the specified number of fences.
    fn grow(&mut self, count: usize) {
        let max = self.config.max_size;
        let current = self.fences.len();
        let actual_count = count.min(max.saturating_sub(current));
        for _ in 0..actual_count {
            let id = FenceId(self.next_id.fetch_add(1, Ordering::Relaxed));
            let fence = PooledFence::new(id);
            let index = self.fences.len();
            self.fences.push(fence);
            self.available.push_back(index);
        }
        if actual_count > 0 {
            self.grow_events += 1;
        }
    }

    /// Acquire a fence from the pool. Returns `None` if no fences are available
    /// and auto-grow is disabled or the pool is at maximum size.
    pub fn acquire(&mut self) -> Option<FenceId> {
        if self.available.is_empty() && self.config.auto_grow {
            let batch = self.config.grow_batch_size;
            self.grow(batch);
        }
        let index = self.available.pop_front()?;
        let fence = &mut self.fences[index];
        fence.status = FenceStatus::Pending;
        fence.submit_time = Some(Instant::now());
        fence.signal_time = None;
        self.total_allocations += 1;
        Some(fence.id)
    }

    /// Signal that a fence has completed on the GPU.
    pub fn signal(&mut self, id: FenceId) -> bool {
        if let Some(fence) = self.fences.iter_mut().find(|f| f.id == id) {
            fence.status = FenceStatus::Signaled;
            fence.signal_time = Some(Instant::now());
            true
        } else {
            false
        }
    }

    /// Release a fence back to the pool for reuse.
    pub fn release(&mut self, id: FenceId) -> bool {
        if let Some((index, fence)) = self.fences.iter_mut().enumerate().find(|(_, f)| f.id == id) {
            fence.status = FenceStatus::Available;
            fence.submit_time = None;
            fence.signal_time = None;
            fence.recycle_count += 1;
            self.available.push_back(index);
            self.total_recycles += 1;
            true
        } else {
            false
        }
    }

    /// Get the current status of a fence.
    pub fn status(&self, id: FenceId) -> Option<FenceStatus> {
        self.fences.iter().find(|f| f.id == id).map(|f| f.status)
    }

    /// Return all currently pending fence IDs.
    pub fn pending_fences(&self) -> Vec<FenceId> {
        self.fences
            .iter()
            .filter(|f| f.status == FenceStatus::Pending)
            .map(|f| f.id)
            .collect()
    }

    /// Signal all pending fences and release them back to the pool.
    pub fn flush_all(&mut self) {
        let pending_ids: Vec<FenceId> = self.pending_fences();
        for id in &pending_ids {
            self.signal(*id);
        }
        let signaled_ids: Vec<FenceId> = self
            .fences
            .iter()
            .filter(|f| f.status == FenceStatus::Signaled)
            .map(|f| f.id)
            .collect();
        for id in signaled_ids {
            self.release(id);
        }
    }

    /// Return the total number of fences in the pool.
    pub fn total_count(&self) -> usize {
        self.fences.len()
    }

    /// Return the number of available fences.
    pub fn available_count(&self) -> usize {
        self.available.len()
    }

    /// Compute pool statistics.
    #[allow(clippy::cast_precision_loss)]
    pub fn stats(&self) -> FencePoolStats {
        let pending_count = self
            .fences
            .iter()
            .filter(|f| f.status == FenceStatus::Pending)
            .count();
        let signaled_count = self
            .fences
            .iter()
            .filter(|f| f.status == FenceStatus::Signaled)
            .count();

        let latencies: Vec<f64> = self
            .fences
            .iter()
            .filter_map(PooledFence::latency)
            .map(|d| d.as_micros() as f64)
            .collect();
        let avg_latency_us = if latencies.is_empty() {
            0.0
        } else {
            latencies.iter().sum::<f64>() / latencies.len() as f64
        };

        FencePoolStats {
            total_fences: self.fences.len(),
            available_count: self.available.len(),
            pending_count,
            signaled_count,
            total_allocations: self.total_allocations,
            total_recycles: self.total_recycles,
            grow_events: self.grow_events,
            avg_latency_us,
        }
    }

    /// Reset the pool, marking all fences as available.
    pub fn reset(&mut self) {
        self.available.clear();
        for (i, fence) in self.fences.iter_mut().enumerate() {
            fence.status = FenceStatus::Available;
            fence.submit_time = None;
            fence.signal_time = None;
            self.available.push_back(i);
        }
    }

    /// Return the wait timeout configured for this pool.
    pub fn wait_timeout(&self) -> Duration {
        self.config.wait_timeout
    }

    /// Check whether the pool has fences available without growing.
    pub fn has_available(&self) -> bool {
        !self.available.is_empty()
    }
}

impl Default for FencePool {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_create_default_pool() {
        let pool = FencePool::new();
        assert_eq!(pool.total_count(), 16);
        assert_eq!(pool.available_count(), 16);
    }

    #[test]
    fn test_create_with_config() {
        let config = FencePoolConfig {
            initial_size: 4,
            max_size: 32,
            auto_grow: true,
            grow_batch_size: 4,
            ..Default::default()
        };
        let pool = FencePool::with_config(config);
        assert_eq!(pool.total_count(), 4);
        assert_eq!(pool.available_count(), 4);
    }

    #[test]
    fn test_acquire_fence() {
        let mut pool = FencePool::new();
        let id = pool.acquire();
        assert!(id.is_some());
        assert_eq!(pool.available_count(), 15);
    }

    #[test]
    fn test_signal_fence() {
        let mut pool = FencePool::new();
        let id = pool.acquire().expect("fence acquire should succeed");
        assert!(pool.signal(id));
        assert_eq!(pool.status(id), Some(FenceStatus::Signaled));
    }

    #[test]
    fn test_release_fence() {
        let mut pool = FencePool::new();
        let initial_available = pool.available_count();
        let id = pool.acquire().expect("fence acquire should succeed");
        assert_eq!(pool.available_count(), initial_available - 1);
        pool.signal(id);
        pool.release(id);
        assert_eq!(pool.available_count(), initial_available);
        assert_eq!(pool.status(id), Some(FenceStatus::Available));
    }

    #[test]
    fn test_pending_fences() {
        let mut pool = FencePool::new();
        let id1 = pool.acquire().expect("fence acquire should succeed");
        let id2 = pool.acquire().expect("fence acquire should succeed");
        let pending = pool.pending_fences();
        assert_eq!(pending.len(), 2);
        assert!(pending.contains(&id1));
        assert!(pending.contains(&id2));
    }

    #[test]
    fn test_flush_all() {
        let mut pool = FencePool::new();
        let _id1 = pool.acquire().expect("fence acquire should succeed");
        let _id2 = pool.acquire().expect("fence acquire should succeed");
        assert_eq!(pool.pending_fences().len(), 2);
        pool.flush_all();
        assert_eq!(pool.pending_fences().len(), 0);
        assert_eq!(pool.available_count(), 16);
    }

    #[test]
    fn test_auto_grow() {
        let config = FencePoolConfig {
            initial_size: 2,
            max_size: 10,
            auto_grow: true,
            grow_batch_size: 3,
            ..Default::default()
        };
        let mut pool = FencePool::with_config(config);
        let _id1 = pool.acquire().expect("fence acquire should succeed");
        let _id2 = pool.acquire().expect("fence acquire should succeed");
        // Pool exhausted, should auto-grow
        let id3 = pool.acquire();
        assert!(id3.is_some());
        assert!(pool.total_count() > 2);
    }

    #[test]
    fn test_max_size_limit() {
        let config = FencePoolConfig {
            initial_size: 2,
            max_size: 3,
            auto_grow: true,
            grow_batch_size: 10,
            ..Default::default()
        };
        let mut pool = FencePool::with_config(config);
        let _id1 = pool.acquire().expect("fence acquire should succeed");
        let _id2 = pool.acquire().expect("fence acquire should succeed");
        let _id3 = pool.acquire();
        // Should not exceed max
        assert!(pool.total_count() <= 3);
    }

    #[test]
    fn test_no_auto_grow() {
        let config = FencePoolConfig {
            initial_size: 1,
            max_size: 10,
            auto_grow: false,
            ..Default::default()
        };
        let mut pool = FencePool::with_config(config);
        let _id = pool.acquire().expect("fence acquire should succeed");
        let id2 = pool.acquire();
        assert!(id2.is_none());
    }

    #[test]
    fn test_stats() {
        let mut pool = FencePool::new();
        let id1 = pool.acquire().expect("fence acquire should succeed");
        let _id2 = pool.acquire().expect("fence acquire should succeed");
        pool.signal(id1);
        let stats = pool.stats();
        assert_eq!(stats.total_fences, 16);
        assert_eq!(stats.pending_count, 1);
        assert_eq!(stats.signaled_count, 1);
        assert_eq!(stats.total_allocations, 2);
    }

    #[test]
    fn test_stats_utilization() {
        let stats = FencePoolStats {
            total_fences: 10,
            pending_count: 5,
            ..Default::default()
        };
        let util = stats.utilization();
        assert!((util - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_stats_utilization_empty() {
        let stats = FencePoolStats::default();
        assert!((stats.utilization() - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_reset_pool() {
        let mut pool = FencePool::new();
        let _id1 = pool.acquire().expect("fence acquire should succeed");
        let _id2 = pool.acquire().expect("fence acquire should succeed");
        pool.reset();
        assert_eq!(pool.available_count(), pool.total_count());
    }

    #[test]
    fn test_fence_display() {
        assert_eq!(format!("{}", FenceStatus::Available), "Available");
        assert_eq!(format!("{}", FenceStatus::Pending), "Pending");
        assert_eq!(format!("{}", FenceStatus::Signaled), "Signaled");
        assert_eq!(format!("{}", FenceStatus::Error), "Error");
    }

    #[test]
    fn test_pooled_fence_latency() {
        let mut fence = PooledFence::new(FenceId(0));
        assert!(fence.latency().is_none());
        fence.submit_time = Some(Instant::now());
        assert!(fence.latency().is_none());
        fence.signal_time = Some(Instant::now());
        assert!(fence.latency().is_some());
    }

    #[test]
    fn test_wait_timeout() {
        let pool = FencePool::new();
        assert_eq!(pool.wait_timeout(), Duration::from_secs(5));
    }

    #[test]
    fn test_has_available() {
        let config = FencePoolConfig {
            initial_size: 1,
            max_size: 1,
            auto_grow: false,
            ..Default::default()
        };
        let mut pool = FencePool::with_config(config);
        assert!(pool.has_available());
        let _id = pool.acquire().expect("fence acquire should succeed");
        assert!(!pool.has_available());
    }
}