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
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
599
600
601
602
603
604
605
606
607
608
609
610
611
#![allow(dead_code)]
//! GPU buffer copy and blit operations.
//!
//! This module provides abstractions for copying data between GPU buffers,
//! performing region-based copies, and managing staging buffers for
//! host-device data transfer.

use std::collections::VecDeque;

/// Describes a region within a buffer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BufferRegion {
    /// Byte offset from buffer start.
    pub offset: u64,
    /// Size of the region in bytes.
    pub size: u64,
}

impl BufferRegion {
    /// Create a new buffer region.
    #[must_use]
    pub fn new(offset: u64, size: u64) -> Self {
        Self { offset, size }
    }

    /// Create a region starting at offset 0.
    #[must_use]
    pub fn from_start(size: u64) -> Self {
        Self { offset: 0, size }
    }

    /// End offset (exclusive) of the region.
    #[must_use]
    pub fn end(&self) -> u64 {
        self.offset + self.size
    }

    /// Check if this region overlaps with another.
    #[must_use]
    pub fn overlaps(&self, other: &BufferRegion) -> bool {
        self.offset < other.end() && other.offset < self.end()
    }

    /// Check if this region is entirely contained within another.
    #[must_use]
    pub fn contained_in(&self, other: &BufferRegion) -> bool {
        self.offset >= other.offset && self.end() <= other.end()
    }

    /// Compute the intersection of two regions, if any.
    #[must_use]
    pub fn intersection(&self, other: &BufferRegion) -> Option<BufferRegion> {
        let start = self.offset.max(other.offset);
        let end = self.end().min(other.end());
        if start < end {
            Some(BufferRegion::new(start, end - start))
        } else {
            None
        }
    }
}

/// Describes a 2D region for image-like buffer copies.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ImageRegion {
    /// X offset in pixels.
    pub x: u32,
    /// Y offset in pixels.
    pub y: u32,
    /// Width in pixels.
    pub width: u32,
    /// Height in pixels.
    pub height: u32,
}

impl ImageRegion {
    /// Create a new image region.
    #[must_use]
    pub fn new(x: u32, y: u32, width: u32, height: u32) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }

    /// Create a region starting at the origin.
    #[must_use]
    pub fn from_size(width: u32, height: u32) -> Self {
        Self {
            x: 0,
            y: 0,
            width,
            height,
        }
    }

    /// Total pixel count.
    #[must_use]
    pub fn pixel_count(&self) -> u64 {
        u64::from(self.width) * u64::from(self.height)
    }

    /// Check if a point is inside the region.
    #[must_use]
    pub fn contains_point(&self, px: u32, py: u32) -> bool {
        px >= self.x && px < self.x + self.width && py >= self.y && py < self.y + self.height
    }
}

/// Direction of a buffer copy operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CopyDirection {
    /// Host (CPU) to Device (GPU).
    HostToDevice,
    /// Device (GPU) to Host (CPU).
    DeviceToHost,
    /// Device to Device (same GPU).
    DeviceToDevice,
    /// Peer to Peer (between GPUs).
    PeerToPeer,
}

impl std::fmt::Display for CopyDirection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::HostToDevice => write!(f, "Host -> Device"),
            Self::DeviceToHost => write!(f, "Device -> Host"),
            Self::DeviceToDevice => write!(f, "Device -> Device"),
            Self::PeerToPeer => write!(f, "Peer -> Peer"),
        }
    }
}

/// A single buffer copy command.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CopyCommand {
    /// Source buffer identifier.
    pub src_id: u64,
    /// Destination buffer identifier.
    pub dst_id: u64,
    /// Source region.
    pub src_region: BufferRegion,
    /// Destination offset.
    pub dst_offset: u64,
    /// Direction of the copy.
    pub direction: CopyDirection,
}

impl CopyCommand {
    /// Create a new copy command.
    #[must_use]
    pub fn new(
        src_id: u64,
        dst_id: u64,
        src_region: BufferRegion,
        dst_offset: u64,
        direction: CopyDirection,
    ) -> Self {
        Self {
            src_id,
            dst_id,
            src_region,
            dst_offset,
            direction,
        }
    }

    /// The destination region implied by this copy.
    #[must_use]
    pub fn dst_region(&self) -> BufferRegion {
        BufferRegion::new(self.dst_offset, self.src_region.size)
    }

    /// Check if this copy would alias with another (read-write hazard).
    #[must_use]
    pub fn aliases_with(&self, other: &CopyCommand) -> bool {
        // Writing to same buffer with overlapping regions
        if self.dst_id == other.dst_id {
            let my_dst = self.dst_region();
            let other_dst = other.dst_region();
            if my_dst.overlaps(&other_dst) {
                return true;
            }
        }
        // Source of one is destination of another with overlap
        if self.src_id == other.dst_id {
            let other_dst = other.dst_region();
            if self.src_region.overlaps(&other_dst) {
                return true;
            }
        }
        if self.dst_id == other.src_id {
            let my_dst = self.dst_region();
            if my_dst.overlaps(&other.src_region) {
                return true;
            }
        }
        false
    }
}

/// Batches multiple copy commands for efficient submission.
#[derive(Debug, Default)]
pub struct CopyBatch {
    /// Queued commands.
    commands: VecDeque<CopyCommand>,
    /// Total bytes scheduled.
    total_bytes: u64,
}

impl CopyBatch {
    /// Create a new empty batch.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a copy command to the batch.
    pub fn push(&mut self, cmd: CopyCommand) {
        self.total_bytes += cmd.src_region.size;
        self.commands.push_back(cmd);
    }

    /// Number of commands in the batch.
    #[must_use]
    pub fn len(&self) -> usize {
        self.commands.len()
    }

    /// Whether the batch is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.commands.is_empty()
    }

    /// Total bytes to be copied.
    #[must_use]
    pub fn total_bytes(&self) -> u64 {
        self.total_bytes
    }

    /// Drain all commands from the batch.
    pub fn drain(&mut self) -> Vec<CopyCommand> {
        self.total_bytes = 0;
        self.commands.drain(..).collect()
    }

    /// Check if any commands in the batch alias with each other.
    #[must_use]
    pub fn has_hazards(&self) -> bool {
        let cmds: Vec<_> = self.commands.iter().collect();
        for i in 0..cmds.len() {
            for j in (i + 1)..cmds.len() {
                if cmds[i].aliases_with(cmds[j]) {
                    return true;
                }
            }
        }
        false
    }

    /// Split the batch into independent sub-batches that can run in parallel.
    #[must_use]
    pub fn split_independent(mut self) -> Vec<Vec<CopyCommand>> {
        let all = self.drain();
        if all.is_empty() {
            return Vec::new();
        }

        let mut batches: Vec<Vec<CopyCommand>> = Vec::new();

        for cmd in all {
            let mut placed = false;
            for batch in &mut batches {
                let conflicts = batch.iter().any(|existing| existing.aliases_with(&cmd));
                if !conflicts {
                    batch.push(cmd.clone());
                    placed = true;
                    break;
                }
            }
            if !placed {
                batches.push(vec![cmd]);
            }
        }

        batches
    }
}

/// Statistics about copy operations.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct CopyStats {
    /// Total copies performed.
    pub copy_count: u64,
    /// Total bytes transferred.
    pub total_bytes: u64,
    /// Host-to-device transfers.
    pub h2d_count: u64,
    /// Device-to-host transfers.
    pub d2h_count: u64,
    /// Device-to-device transfers.
    pub d2d_count: u64,
}

impl CopyStats {
    /// Create empty stats.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Record a copy command.
    pub fn record(&mut self, cmd: &CopyCommand) {
        self.copy_count += 1;
        self.total_bytes += cmd.src_region.size;
        match cmd.direction {
            CopyDirection::HostToDevice => self.h2d_count += 1,
            CopyDirection::DeviceToHost => self.d2h_count += 1,
            CopyDirection::DeviceToDevice | CopyDirection::PeerToPeer => self.d2d_count += 1,
        }
    }

    /// Reset all counters.
    pub fn reset(&mut self) {
        *self = Self::default();
    }
}

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

    #[test]
    fn test_buffer_region_basic() {
        let r = BufferRegion::new(10, 20);
        assert_eq!(r.offset, 10);
        assert_eq!(r.size, 20);
        assert_eq!(r.end(), 30);
    }

    #[test]
    fn test_buffer_region_from_start() {
        let r = BufferRegion::from_start(100);
        assert_eq!(r.offset, 0);
        assert_eq!(r.size, 100);
    }

    #[test]
    fn test_buffer_region_overlaps() {
        let a = BufferRegion::new(0, 10);
        let b = BufferRegion::new(5, 10);
        assert!(a.overlaps(&b));
        assert!(b.overlaps(&a));
    }

    #[test]
    fn test_buffer_region_no_overlap() {
        let a = BufferRegion::new(0, 10);
        let b = BufferRegion::new(10, 10);
        assert!(!a.overlaps(&b));
    }

    #[test]
    fn test_buffer_region_contained() {
        let inner = BufferRegion::new(5, 5);
        let outer = BufferRegion::new(0, 20);
        assert!(inner.contained_in(&outer));
        assert!(!outer.contained_in(&inner));
    }

    #[test]
    fn test_buffer_region_intersection() {
        let a = BufferRegion::new(0, 10);
        let b = BufferRegion::new(5, 10);
        let i = a.intersection(&b).expect("intersection should succeed");
        assert_eq!(i.offset, 5);
        assert_eq!(i.size, 5);
    }

    #[test]
    fn test_buffer_region_no_intersection() {
        let a = BufferRegion::new(0, 5);
        let b = BufferRegion::new(10, 5);
        assert!(a.intersection(&b).is_none());
    }

    #[test]
    fn test_image_region_basic() {
        let r = ImageRegion::new(10, 20, 100, 50);
        assert_eq!(r.pixel_count(), 5000);
    }

    #[test]
    fn test_image_region_contains_point() {
        let r = ImageRegion::from_size(100, 100);
        assert!(r.contains_point(50, 50));
        assert!(!r.contains_point(100, 100));
        assert!(r.contains_point(0, 0));
    }

    #[test]
    fn test_copy_direction_display() {
        assert_eq!(format!("{}", CopyDirection::HostToDevice), "Host -> Device");
        assert_eq!(format!("{}", CopyDirection::DeviceToHost), "Device -> Host");
    }

    #[test]
    fn test_copy_command_dst_region() {
        let cmd = CopyCommand::new(
            1,
            2,
            BufferRegion::new(0, 1024),
            512,
            CopyDirection::DeviceToDevice,
        );
        let dst = cmd.dst_region();
        assert_eq!(dst.offset, 512);
        assert_eq!(dst.size, 1024);
    }

    #[test]
    fn test_copy_command_aliases() {
        let a = CopyCommand::new(
            1,
            2,
            BufferRegion::new(0, 100),
            0,
            CopyDirection::DeviceToDevice,
        );
        let b = CopyCommand::new(
            3,
            2,
            BufferRegion::new(0, 100),
            50,
            CopyDirection::DeviceToDevice,
        );
        assert!(a.aliases_with(&b));
    }

    #[test]
    fn test_copy_command_no_alias() {
        let a = CopyCommand::new(
            1,
            2,
            BufferRegion::new(0, 100),
            0,
            CopyDirection::DeviceToDevice,
        );
        let b = CopyCommand::new(
            3,
            4,
            BufferRegion::new(0, 100),
            0,
            CopyDirection::DeviceToDevice,
        );
        assert!(!a.aliases_with(&b));
    }

    #[test]
    fn test_copy_batch_push_and_drain() {
        let mut batch = CopyBatch::new();
        assert!(batch.is_empty());

        batch.push(CopyCommand::new(
            1,
            2,
            BufferRegion::from_start(256),
            0,
            CopyDirection::HostToDevice,
        ));
        batch.push(CopyCommand::new(
            3,
            4,
            BufferRegion::from_start(512),
            0,
            CopyDirection::DeviceToHost,
        ));

        assert_eq!(batch.len(), 2);
        assert_eq!(batch.total_bytes(), 768);

        let cmds = batch.drain();
        assert_eq!(cmds.len(), 2);
        assert!(batch.is_empty());
        assert_eq!(batch.total_bytes(), 0);
    }

    #[test]
    fn test_copy_batch_no_hazards() {
        let mut batch = CopyBatch::new();
        batch.push(CopyCommand::new(
            1,
            2,
            BufferRegion::from_start(100),
            0,
            CopyDirection::DeviceToDevice,
        ));
        batch.push(CopyCommand::new(
            3,
            4,
            BufferRegion::from_start(100),
            0,
            CopyDirection::DeviceToDevice,
        ));
        assert!(!batch.has_hazards());
    }

    #[test]
    fn test_copy_batch_with_hazards() {
        let mut batch = CopyBatch::new();
        batch.push(CopyCommand::new(
            1,
            2,
            BufferRegion::from_start(100),
            0,
            CopyDirection::DeviceToDevice,
        ));
        batch.push(CopyCommand::new(
            3,
            2,
            BufferRegion::from_start(100),
            50,
            CopyDirection::DeviceToDevice,
        ));
        assert!(batch.has_hazards());
    }

    #[test]
    fn test_copy_batch_split_independent() {
        let mut batch = CopyBatch::new();
        batch.push(CopyCommand::new(
            1,
            2,
            BufferRegion::from_start(100),
            0,
            CopyDirection::DeviceToDevice,
        ));
        batch.push(CopyCommand::new(
            3,
            2,
            BufferRegion::from_start(100),
            50,
            CopyDirection::DeviceToDevice,
        ));
        batch.push(CopyCommand::new(
            5,
            6,
            BufferRegion::from_start(100),
            0,
            CopyDirection::DeviceToDevice,
        ));

        let batches = batch.split_independent();
        // First and third can go together, second conflicts with first
        assert!(batches.len() >= 2);
    }

    #[test]
    fn test_copy_stats() {
        let mut stats = CopyStats::new();
        let cmd = CopyCommand::new(
            1,
            2,
            BufferRegion::from_start(1024),
            0,
            CopyDirection::HostToDevice,
        );
        stats.record(&cmd);
        assert_eq!(stats.copy_count, 1);
        assert_eq!(stats.total_bytes, 1024);
        assert_eq!(stats.h2d_count, 1);
        assert_eq!(stats.d2h_count, 0);

        stats.reset();
        assert_eq!(stats.copy_count, 0);
    }

    #[test]
    fn test_copy_stats_multiple_directions() {
        let mut stats = CopyStats::new();
        stats.record(&CopyCommand::new(
            1,
            2,
            BufferRegion::from_start(100),
            0,
            CopyDirection::HostToDevice,
        ));
        stats.record(&CopyCommand::new(
            2,
            1,
            BufferRegion::from_start(200),
            0,
            CopyDirection::DeviceToHost,
        ));
        stats.record(&CopyCommand::new(
            2,
            3,
            BufferRegion::from_start(300),
            0,
            CopyDirection::DeviceToDevice,
        ));
        assert_eq!(stats.copy_count, 3);
        assert_eq!(stats.total_bytes, 600);
        assert_eq!(stats.h2d_count, 1);
        assert_eq!(stats.d2h_count, 1);
        assert_eq!(stats.d2d_count, 1);
    }
}