baracuda-driver 0.0.1-alpha.30

Safe Rust wrappers for the CUDA Driver API (devices, contexts, streams, events, memory, kernels, graphs).
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! Graphics-API interop — register GL / D3D / VDPAU / EGL resources with
//! CUDA for zero-copy compute on their memory.
//!
//! **Note:** For modern flows on Vulkan and D3D12, prefer
//! [`crate::external::ExternalMemory`] — it's the forward-looking API
//! NVIDIA recommends. The `cuGraphics*` functions here remain useful for:
//!
//! - OpenGL interop on drivers without `GL_EXT_memory_object_fd/win32`.
//! - D3D9 / D3D10 / D3D11 legacy code paths.
//! - VDPAU video-surface import on Linux.
//! - EGL image / stream interop on Jetson.
//!
//! # Workflow
//!
//! 1. Register the graphics resource (`gl::register_buffer`,
//!    `d3d11::register_resource`, ...) — returns a [`GraphicsResource`].
//! 2. Call [`GraphicsResource::map`] on a stream to expose it to CUDA.
//! 3. Call [`GraphicsResource::mapped_pointer`] or
//!    [`GraphicsResource::mapped_array`] to get a usable handle.
//! 4. ... compute ...
//! 5. [`GraphicsResource::unmap`] to release it back to the graphics API.
//!
//! On drop, the resource is unregistered (and unmapped if still mapped,
//! best-effort).

use std::sync::Arc;

use baracuda_cuda_sys::types::CUmipmappedArray;
use baracuda_cuda_sys::{driver, CUarray, CUdeviceptr, CUgraphicsResource};

use crate::context::Context;
use crate::error::{check, Result};
use crate::stream::Stream;

pub use baracuda_cuda_sys::types::{
    CUGLDeviceList as GLDeviceList, CUgraphicsMapResourceFlags as MapResourceFlags,
    CUgraphicsRegisterFlags as RegisterFlags,
};

/// A CUDA-side handle to a graphics-API resource. Drops unregister the
/// resource via `cuGraphicsUnregisterResource`.
#[derive(Clone)]
pub struct GraphicsResource {
    inner: Arc<GraphicsResourceInner>,
}

struct GraphicsResourceInner {
    handle: CUgraphicsResource,
    #[allow(dead_code)]
    context: Context,
}

unsafe impl Send for GraphicsResourceInner {}
unsafe impl Sync for GraphicsResourceInner {}

impl core::fmt::Debug for GraphicsResourceInner {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("GraphicsResource")
            .field("handle", &self.handle)
            .finish_non_exhaustive()
    }
}

impl core::fmt::Debug for GraphicsResource {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.inner.fmt(f)
    }
}

impl GraphicsResource {
    /// Wrap a raw `CUgraphicsResource`. The resource is assumed to be
    /// registered already (e.g. from `cu<API>Register*`).
    ///
    /// # Safety
    ///
    /// `handle` must be a live, registered resource. baracuda unregisters
    /// it when this wrapper (and all clones) drop.
    pub unsafe fn from_raw(context: &Context, handle: CUgraphicsResource) -> Self {
        Self {
            inner: Arc::new(GraphicsResourceInner {
                handle,
                context: context.clone(),
            }),
        }
    }

    #[inline]
    pub fn as_raw(&self) -> CUgraphicsResource {
        self.inner.handle
    }

    /// Change the map flags before mapping (e.g. READ_ONLY for shader
    /// sampling, WRITE_DISCARD for compute output).
    pub fn set_map_flags(&self, flags: u32) -> Result<()> {
        let d = driver()?;
        let cu = d.cu_graphics_resource_set_map_flags()?;
        check(unsafe { cu(self.inner.handle, flags) })
    }

    /// Map the resource on `stream` so CUDA can access it. Pair with
    /// [`Self::unmap`].
    pub fn map(&self, stream: &Stream) -> Result<()> {
        let d = driver()?;
        let cu = d.cu_graphics_map_resources()?;
        let mut arr = [self.inner.handle];
        check(unsafe { cu(1, arr.as_mut_ptr(), stream.as_raw()) })
    }

    /// Unmap the resource on `stream`, releasing it back to the graphics API.
    pub fn unmap(&self, stream: &Stream) -> Result<()> {
        let d = driver()?;
        let cu = d.cu_graphics_unmap_resources()?;
        let mut arr = [self.inner.handle];
        check(unsafe { cu(1, arr.as_mut_ptr(), stream.as_raw()) })
    }

    /// Retrieve the device pointer + size for a mapped buffer-type
    /// resource (typical of GL buffer objects, D3D buffers).
    pub fn mapped_pointer(&self) -> Result<(CUdeviceptr, usize)> {
        let d = driver()?;
        let cu = d.cu_graphics_resource_get_mapped_pointer()?;
        let mut dptr = CUdeviceptr(0);
        let mut size: usize = 0;
        check(unsafe { cu(&mut dptr, &mut size, self.inner.handle) })?;
        Ok((dptr, size))
    }

    /// Retrieve the subresource CUDA array for a mapped image / texture
    /// resource (GL textures, D3D textures, VDPAU surfaces).
    pub fn mapped_array(&self, array_index: u32, mip_level: u32) -> Result<CUarray> {
        let d = driver()?;
        let cu = d.cu_graphics_sub_resource_get_mapped_array()?;
        let mut arr: CUarray = core::ptr::null_mut();
        check(unsafe { cu(&mut arr, self.inner.handle, array_index, mip_level) })?;
        Ok(arr)
    }

    /// Retrieve the mipmapped-array handle for a mapped mipmapped texture.
    pub fn mapped_mipmapped_array(&self) -> Result<CUmipmappedArray> {
        let d = driver()?;
        let cu = d.cu_graphics_resource_get_mapped_mipmapped_array()?;
        let mut mip: CUmipmappedArray = core::ptr::null_mut();
        check(unsafe { cu(&mut mip, self.inner.handle) })?;
        Ok(mip)
    }

    /// Bulk-map multiple resources in one call. More efficient than
    /// calling [`Self::map`] per resource.
    pub fn map_all(resources: &[Self], stream: &Stream) -> Result<()> {
        if resources.is_empty() {
            return Ok(());
        }
        let d = driver()?;
        let cu = d.cu_graphics_map_resources()?;
        let mut raws: Vec<CUgraphicsResource> = resources.iter().map(|r| r.as_raw()).collect();
        check(unsafe {
            cu(
                raws.len() as core::ffi::c_uint,
                raws.as_mut_ptr(),
                stream.as_raw(),
            )
        })
    }

    /// Bulk-unmap.
    pub fn unmap_all(resources: &[Self], stream: &Stream) -> Result<()> {
        if resources.is_empty() {
            return Ok(());
        }
        let d = driver()?;
        let cu = d.cu_graphics_unmap_resources()?;
        let mut raws: Vec<CUgraphicsResource> = resources.iter().map(|r| r.as_raw()).collect();
        check(unsafe {
            cu(
                raws.len() as core::ffi::c_uint,
                raws.as_mut_ptr(),
                stream.as_raw(),
            )
        })
    }
}

impl Drop for GraphicsResourceInner {
    fn drop(&mut self) {
        if self.handle.is_null() {
            return;
        }
        if let Ok(d) = driver() {
            if let Ok(cu) = d.cu_graphics_unregister_resource() {
                let _ = unsafe { cu(self.handle) };
            }
        }
    }
}

/// OpenGL-specific interop.
pub mod gl {
    use super::*;
    use crate::device::Device;
    use baracuda_cuda_sys::types::{GLenum, GLuint};

    /// Initialize the GL interop subsystem. Must be called from a thread
    /// with a current GL context. Deprecated but retained for legacy code.
    pub fn init() -> Result<()> {
        let d = driver()?;
        let cu = d.cu_gl_init()?;
        check(unsafe { cu() })
    }

    /// List CUDA devices associated with a given slice of the GL
    /// rendering pipeline (current frame, next frame, or all devices).
    pub fn get_devices(device_list: u32) -> Result<Vec<Device>> {
        let d = driver()?;
        let cu = d.cu_gl_get_devices()?;
        // Query count first by passing cuda_device_count_in = 0.
        let mut count: core::ffi::c_uint = 0;
        let probe = unsafe { cu(&mut count, core::ptr::null_mut(), 0, device_list) };
        // Some driver versions return SUCCESS with count set; others
        // return NOT_INITIALIZED if there's no GL context. Treat the
        // latter as "no GL devices" rather than error.
        if !probe.is_success() && probe != baracuda_cuda_sys::CUresult::ERROR_NOT_INITIALIZED {
            check(probe)?;
        }
        if count == 0 {
            return Ok(Vec::new());
        }
        let mut raw = vec![baracuda_cuda_sys::CUdevice(0); count as usize];
        check(unsafe {
            cu(
                &mut count,
                raw.as_mut_ptr(),
                raw.len() as core::ffi::c_uint,
                device_list,
            )
        })?;
        // SAFETY: `Device` is a `pub(crate)` newtype over `CUdevice`;
        // constructing from driver-returned ordinals is safe.
        Ok(raw.into_iter().map(Device).collect())
    }

    /// Register an OpenGL buffer object (VBO / SSBO / UBO / ...) for CUDA
    /// access. `buffer` is the GL buffer name from `glGenBuffers`.
    ///
    /// # Safety
    ///
    /// `buffer` must be a valid GL buffer in a GL context that is current
    /// on the calling thread. The buffer must outlive the returned
    /// [`GraphicsResource`].
    pub unsafe fn register_buffer(
        context: &Context,
        buffer: GLuint,
        flags: u32,
    ) -> Result<GraphicsResource> { unsafe {
        context.set_current()?;
        let d = driver()?;
        let cu = d.cu_graphics_gl_register_buffer()?;
        let mut resource: CUgraphicsResource = core::ptr::null_mut();
        check(cu(&mut resource, buffer, flags))?;
        Ok(GraphicsResource::from_raw(context, resource))
    }}

    /// Register an OpenGL texture / renderbuffer. `target` is the GL
    /// binding target (`GL_TEXTURE_2D`, `GL_RENDERBUFFER`, ...).
    ///
    /// # Safety
    ///
    /// Same discipline as [`register_buffer`]: `image` must be live in a
    /// current GL context, matching the declared `target`.
    pub unsafe fn register_image(
        context: &Context,
        image: GLuint,
        target: GLenum,
        flags: u32,
    ) -> Result<GraphicsResource> { unsafe {
        context.set_current()?;
        let d = driver()?;
        let cu = d.cu_graphics_gl_register_image()?;
        let mut resource: CUgraphicsResource = core::ptr::null_mut();
        check(cu(&mut resource, image, target, flags))?;
        Ok(GraphicsResource::from_raw(context, resource))
    }}
}

/// Direct3D 9 interop (Windows).
pub mod d3d9 {
    use super::*;
    use crate::device::Device;
    use baracuda_cuda_sys::types::{ID3DDevice, ID3DResource};

    /// Find the CUDA device powering a D3D9 adapter, by adapter name.
    ///
    /// # Safety
    ///
    /// `adapter_name` must be a NUL-terminated C string naming a live
    /// DXGI adapter.
    pub unsafe fn get_device(adapter_name: *const core::ffi::c_char) -> Result<Device> { unsafe {
        let d = driver()?;
        let cu = d.cu_d3d9_get_device()?;
        let mut dev = baracuda_cuda_sys::CUdevice(0);
        check(cu(&mut dev, adapter_name))?;
        Ok(Device(dev))
    }}

    /// List CUDA devices associated with a given D3D9 device.
    ///
    /// # Safety
    ///
    /// `d3d_device` must be a valid `IDirect3DDevice9*` pointer.
    pub unsafe fn get_devices(d3d_device: ID3DDevice, device_list: u32) -> Result<Vec<Device>> { unsafe {
        let d = driver()?;
        let cu = d.cu_d3d9_get_devices()?;
        let mut count: core::ffi::c_uint = 0;
        check(cu(
            &mut count,
            core::ptr::null_mut(),
            0,
            d3d_device,
            device_list,
        ))?;
        if count == 0 {
            return Ok(Vec::new());
        }
        let mut raw = vec![baracuda_cuda_sys::CUdevice(0); count as usize];
        check(cu(
            &mut count,
            raw.as_mut_ptr(),
            raw.len() as core::ffi::c_uint,
            d3d_device,
            device_list,
        ))?;
        Ok(raw.into_iter().map(Device).collect())
    }}

    /// Register a D3D9 resource (`IDirect3DResource9*`) for CUDA access.
    ///
    /// # Safety
    ///
    /// `resource` must be live for the duration of the returned
    /// [`GraphicsResource`].
    pub unsafe fn register_resource(
        context: &Context,
        resource: ID3DResource,
        flags: u32,
    ) -> Result<GraphicsResource> { unsafe {
        context.set_current()?;
        let d = driver()?;
        let cu = d.cu_graphics_d3d9_register_resource()?;
        let mut res: CUgraphicsResource = core::ptr::null_mut();
        check(cu(&mut res, resource, flags))?;
        Ok(GraphicsResource::from_raw(context, res))
    }}
}

/// Direct3D 10 interop (Windows).
pub mod d3d10 {
    use super::*;
    use crate::device::Device;
    use baracuda_cuda_sys::types::{ID3DDevice, ID3DResource};

    /// # Safety
    ///
    /// `adapter` must be a valid `IDXGIAdapter*`.
    pub unsafe fn get_device(adapter: ID3DDevice) -> Result<Device> { unsafe {
        let d = driver()?;
        let cu = d.cu_d3d10_get_device()?;
        let mut dev = baracuda_cuda_sys::CUdevice(0);
        check(cu(&mut dev, adapter))?;
        Ok(Device(dev))
    }}

    /// # Safety
    ///
    /// `d3d_device` must be a valid `ID3D10Device*`.
    pub unsafe fn get_devices(d3d_device: ID3DDevice, device_list: u32) -> Result<Vec<Device>> { unsafe {
        let d = driver()?;
        let cu = d.cu_d3d10_get_devices()?;
        let mut count: core::ffi::c_uint = 0;
        check(cu(
            &mut count,
            core::ptr::null_mut(),
            0,
            d3d_device,
            device_list,
        ))?;
        if count == 0 {
            return Ok(Vec::new());
        }
        let mut raw = vec![baracuda_cuda_sys::CUdevice(0); count as usize];
        check(cu(
            &mut count,
            raw.as_mut_ptr(),
            raw.len() as core::ffi::c_uint,
            d3d_device,
            device_list,
        ))?;
        Ok(raw.into_iter().map(Device).collect())
    }}

    /// # Safety
    ///
    /// `resource` must be a live `ID3D10Resource*`.
    pub unsafe fn register_resource(
        context: &Context,
        resource: ID3DResource,
        flags: u32,
    ) -> Result<GraphicsResource> { unsafe {
        context.set_current()?;
        let d = driver()?;
        let cu = d.cu_graphics_d3d10_register_resource()?;
        let mut res: CUgraphicsResource = core::ptr::null_mut();
        check(cu(&mut res, resource, flags))?;
        Ok(GraphicsResource::from_raw(context, res))
    }}
}

/// VDPAU interop (Linux). Register VDPAU video / output surfaces from
/// libvdpau into CUDA for zero-copy decode → compute pipelines.
pub mod vdpau {
    use super::*;
    use crate::device::Device;
    use baracuda_cuda_sys::types::{
        VdpDevice, VdpGetProcAddress, VdpOutputSurface, VdpVideoSurface,
    };

    /// Look up the CUDA device powering a given VDPAU device.
    ///
    /// # Safety
    ///
    /// `vdp_device` and `vdp_get_proc_address` must come from a live
    /// libvdpau `VdpDeviceCreateX11` call on the current X display.
    pub unsafe fn get_device(
        vdp_device: VdpDevice,
        vdp_get_proc_address: VdpGetProcAddress,
    ) -> Result<Device> { unsafe {
        let d = driver()?;
        let cu = d.cu_vdpau_get_device()?;
        let mut dev = baracuda_cuda_sys::CUdevice(0);
        check(cu(&mut dev, vdp_device, vdp_get_proc_address))?;
        Ok(Device(dev))
    }}

    /// Register a VDPAU video surface (e.g. a decoded frame output) for
    /// CUDA access.
    ///
    /// # Safety
    ///
    /// `vdp_surface` must be a live VDPAU video surface on the VDPAU
    /// device associated with the current CUDA context.
    pub unsafe fn register_video_surface(
        context: &Context,
        vdp_surface: VdpVideoSurface,
        flags: u32,
    ) -> Result<GraphicsResource> { unsafe {
        context.set_current()?;
        let d = driver()?;
        let cu = d.cu_graphics_vdpau_register_video_surface()?;
        let mut res: CUgraphicsResource = core::ptr::null_mut();
        check(cu(&mut res, vdp_surface, flags))?;
        Ok(GraphicsResource::from_raw(context, res))
    }}

    /// Register a VDPAU output surface.
    ///
    /// # Safety
    ///
    /// Same as [`register_video_surface`].
    pub unsafe fn register_output_surface(
        context: &Context,
        vdp_surface: VdpOutputSurface,
        flags: u32,
    ) -> Result<GraphicsResource> { unsafe {
        context.set_current()?;
        let d = driver()?;
        let cu = d.cu_graphics_vdpau_register_output_surface()?;
        let mut res: CUgraphicsResource = core::ptr::null_mut();
        check(cu(&mut res, vdp_surface, flags))?;
        Ok(GraphicsResource::from_raw(context, res))
    }}
}

/// EGL interop — primary path for Jetson video pipelines (NvMM streams,
/// camera capture). Supports both image-based and EGLStream-based flows.
pub mod egl {
    use super::*;
    use baracuda_cuda_sys::types::{CUeglFrame, EGLImageKHR, EGLStreamKHR, EGLSyncKHR};
    use baracuda_cuda_sys::CUevent;
    use core::ffi::c_void;

    /// Register an `EGLImageKHR` for CUDA access.
    ///
    /// # Safety
    ///
    /// `image` must be a live `EGLImageKHR` (typically created via
    /// `eglCreateImageKHR`). The image must outlive the returned resource.
    pub unsafe fn register_image(
        context: &Context,
        image: EGLImageKHR,
        flags: u32,
    ) -> Result<GraphicsResource> { unsafe {
        context.set_current()?;
        let d = driver()?;
        let cu = d.cu_graphics_egl_register_image()?;
        let mut res: CUgraphicsResource = core::ptr::null_mut();
        check(cu(&mut res, image, flags))?;
        Ok(GraphicsResource::from_raw(context, res))
    }}

    /// Fetch the `CUeglFrame` (YUV / RGB descriptor) for a mapped EGL
    /// resource. `index` and `mip_level` select the plane / mip.
    pub fn mapped_frame(
        resource: &GraphicsResource,
        index: u32,
        mip_level: u32,
    ) -> Result<CUeglFrame> {
        let d = driver()?;
        let cu = d.cu_graphics_resource_get_mapped_egl_frame()?;
        let mut frame = CUeglFrame::default();
        check(unsafe { cu(&mut frame, resource.as_raw(), index, mip_level) })?;
        Ok(frame)
    }

    /// Wrap an `EGLSyncKHR` as a CUDA event — lets CUDA wait on GPU work
    /// submitted via EGL's synchronization primitives.
    ///
    /// # Safety
    ///
    /// `egl_sync` must be a live `EGLSyncKHR`.
    pub unsafe fn event_from_sync(egl_sync: EGLSyncKHR, flags: u32) -> Result<CUevent> { unsafe {
        let d = driver()?;
        let cu = d.cu_event_create_from_egl_sync()?;
        let mut event: CUevent = core::ptr::null_mut();
        check(cu(&mut event, egl_sync, flags))?;
        Ok(event)
    }}

    /// EGLStream consumer-side connection — CUDA receives frames from an
    /// EGL producer (e.g. a camera or decoder).
    ///
    /// `connection` is an opaque pointer handed back by the driver;
    /// baracuda does not wrap it in a typed struct, so you'll need to
    /// keep the pointer alive yourself.
    ///
    /// # Safety
    ///
    /// `stream` must be a live `EGLStreamKHR`. `connection` must be a
    /// pointer to a single `CUeglStreamConnection` slot.
    pub unsafe fn stream_consumer_connect(
        connection: *mut c_void,
        stream: EGLStreamKHR,
    ) -> Result<()> { unsafe {
        let d = driver()?;
        let cu = d.cu_egl_stream_consumer_connect()?;
        check(cu(connection, stream))
    }}

    /// # Safety
    ///
    /// `connection` must be a connection previously set up by
    /// [`stream_consumer_connect`].
    pub unsafe fn stream_consumer_disconnect(connection: *mut c_void) -> Result<()> { unsafe {
        let d = driver()?;
        let cu = d.cu_egl_stream_consumer_disconnect()?;
        check(cu(connection))
    }}

    /// Acquire the next frame from an EGLStream consumer. `timeout` is
    /// in nanoseconds (0 = non-blocking).
    ///
    /// # Safety
    ///
    /// `connection` must be a connected consumer (see
    /// [`stream_consumer_connect`]).
    pub unsafe fn stream_consumer_acquire_frame(
        context: &Context,
        connection: *mut c_void,
        cu_stream_out: *mut baracuda_cuda_sys::CUstream,
        timeout: u32,
    ) -> Result<GraphicsResource> { unsafe {
        let d = driver()?;
        let cu = d.cu_egl_stream_consumer_acquire_frame()?;
        let mut resource: CUgraphicsResource = core::ptr::null_mut();
        check(cu(connection, &mut resource, cu_stream_out, timeout))?;
        Ok(GraphicsResource::from_raw(context, resource))
    }}

    /// # Safety
    ///
    /// `connection` must match the one used to acquire `resource`.
    pub unsafe fn stream_consumer_release_frame(
        connection: *mut c_void,
        resource: &GraphicsResource,
        cu_stream_inout: *mut baracuda_cuda_sys::CUstream,
    ) -> Result<()> { unsafe {
        let d = driver()?;
        let cu = d.cu_egl_stream_consumer_release_frame()?;
        check(cu(connection, resource.as_raw(), cu_stream_inout))
    }}

    /// EGLStream producer-side connection — CUDA feeds frames into an
    /// EGL stream (typically to a compositor or encoder consumer).
    ///
    /// # Safety
    ///
    /// `stream` must be a live `EGLStreamKHR`. `connection` must be a
    /// pointer to a single `CUeglStreamConnection` slot.
    pub unsafe fn stream_producer_connect(
        connection: *mut c_void,
        stream: EGLStreamKHR,
        width: i32,
        height: i32,
    ) -> Result<()> { unsafe {
        let d = driver()?;
        let cu = d.cu_egl_stream_producer_connect()?;
        check(cu(connection, stream, width, height))
    }}

    /// # Safety
    ///
    /// `connection` must be a connected producer.
    pub unsafe fn stream_producer_disconnect(connection: *mut c_void) -> Result<()> { unsafe {
        let d = driver()?;
        let cu = d.cu_egl_stream_producer_disconnect()?;
        check(cu(connection))
    }}

    /// Push a frame to the EGL stream.
    ///
    /// # Safety
    ///
    /// `egl_frame` must be populated with live plane pointers pointing at
    /// memory the producer owns until the consumer returns it via
    /// [`stream_producer_return_frame`].
    pub unsafe fn stream_producer_present_frame(
        connection: *mut c_void,
        egl_frame: CUeglFrame,
        cu_stream_inout: *mut baracuda_cuda_sys::CUstream,
    ) -> Result<()> { unsafe {
        let d = driver()?;
        let cu = d.cu_egl_stream_producer_present_frame()?;
        check(cu(connection, egl_frame, cu_stream_inout))
    }}

    /// Reclaim a previously-presented frame. `egl_frame` is overwritten
    /// with the plane descriptors of the returned frame.
    ///
    /// # Safety
    ///
    /// `connection` must be a connected producer.
    pub unsafe fn stream_producer_return_frame(
        connection: *mut c_void,
        egl_frame: *mut CUeglFrame,
        cu_stream_inout: *mut baracuda_cuda_sys::CUstream,
    ) -> Result<()> { unsafe {
        let d = driver()?;
        let cu = d.cu_egl_stream_producer_return_frame()?;
        check(cu(connection, egl_frame, cu_stream_inout))
    }}
}

/// NvSci interop (Jetson / DRIVE platforms). Query attributes for
/// `NvSciSyncObj`s from a CUDA device, then feed the resulting objects
/// into [`crate::external::ExternalSemaphore::import`] with the
/// `NVSCISYNC` handle type.
pub mod nvsci {
    use super::*;
    use crate::device::Device;
    use baracuda_cuda_sys::types::NvSciSyncAttrList;

    /// `CUnvSciSyncAttr::SIGNAL` — see [`baracuda_cuda_sys::types::CUnvSciSyncAttr`].
    pub const SIGNAL: i32 = baracuda_cuda_sys::types::CUnvSciSyncAttr::SIGNAL;
    /// `CUnvSciSyncAttr::WAIT`.
    pub const WAIT: i32 = baracuda_cuda_sys::types::CUnvSciSyncAttr::WAIT;

    /// Fill `attr_list` with the NvSciSync attributes CUDA requires for
    /// `device` to participate as a signaler or waiter.
    ///
    /// # Safety
    ///
    /// `attr_list` must be a live `NvSciSyncAttrList` created via
    /// libnvsciSync's `NvSciSyncAttrListCreate`.
    pub unsafe fn device_sync_attributes(
        attr_list: NvSciSyncAttrList,
        device: &Device,
        direction: i32,
    ) -> Result<()> { unsafe {
        let d = driver()?;
        let cu = d.cu_device_get_nv_sci_sync_attributes()?;
        check(cu(attr_list, device.as_raw(), direction))
    }}
}

/// Direct3D 11 interop (Windows) — the D3D path most still relevant today.
pub mod d3d11 {
    use super::*;
    use crate::device::Device;
    use baracuda_cuda_sys::types::{ID3DDevice, ID3DResource};

    /// # Safety
    ///
    /// `adapter` must be a valid `IDXGIAdapter*`.
    pub unsafe fn get_device(adapter: ID3DDevice) -> Result<Device> { unsafe {
        let d = driver()?;
        let cu = d.cu_d3d11_get_device()?;
        let mut dev = baracuda_cuda_sys::CUdevice(0);
        check(cu(&mut dev, adapter))?;
        Ok(Device(dev))
    }}

    /// # Safety
    ///
    /// `d3d_device` must be a valid `ID3D11Device*`.
    pub unsafe fn get_devices(d3d_device: ID3DDevice, device_list: u32) -> Result<Vec<Device>> { unsafe {
        let d = driver()?;
        let cu = d.cu_d3d11_get_devices()?;
        let mut count: core::ffi::c_uint = 0;
        check(cu(
            &mut count,
            core::ptr::null_mut(),
            0,
            d3d_device,
            device_list,
        ))?;
        if count == 0 {
            return Ok(Vec::new());
        }
        let mut raw = vec![baracuda_cuda_sys::CUdevice(0); count as usize];
        check(cu(
            &mut count,
            raw.as_mut_ptr(),
            raw.len() as core::ffi::c_uint,
            d3d_device,
            device_list,
        ))?;
        Ok(raw.into_iter().map(Device).collect())
    }}

    /// # Safety
    ///
    /// `resource` must be a live `ID3D11Resource*`.
    pub unsafe fn register_resource(
        context: &Context,
        resource: ID3DResource,
        flags: u32,
    ) -> Result<GraphicsResource> { unsafe {
        context.set_current()?;
        let d = driver()?;
        let cu = d.cu_graphics_d3d11_register_resource()?;
        let mut res: CUgraphicsResource = core::ptr::null_mut();
        check(cu(&mut res, resource, flags))?;
        Ok(GraphicsResource::from_raw(context, res))
    }}
}