daedalus_gpu/
traits.rs

1use crate::{
2    GpuAdapterInfo, GpuBackendKind, GpuCapabilities, GpuError, GpuImageHandle, GpuImageRequest,
3    GpuOptions, GpuRequest, buffer::TransferStats, handles::GpuBufferHandle,
4};
5use std::any::Any;
6#[cfg(feature = "gpu-wgpu")]
7use std::sync::Arc;
8
9/// GPU backend trait; no backend-specific types exposed.
10///
11/// ```no_run
12/// use daedalus_gpu::{GpuBackend, GpuBackendKind};
13/// fn uses_backend(backend: &dyn GpuBackend) -> GpuBackendKind {
14///     backend.kind()
15/// }
16/// let _ = uses_backend;
17/// ```
18pub trait GpuBackend: Send + Sync {
19    fn kind(&self) -> GpuBackendKind;
20    fn adapter_info(&self) -> GpuAdapterInfo;
21    fn capabilities(&self) -> GpuCapabilities;
22    fn select_adapter(&self, opts: &GpuOptions) -> Result<GpuAdapterInfo, GpuError>;
23    fn as_any(&self) -> &dyn Any;
24    fn create_buffer(&self, req: &GpuRequest) -> Result<GpuBufferHandle, GpuError>;
25    fn create_image(&self, req: &GpuImageRequest) -> Result<GpuImageHandle, GpuError>;
26    fn upload_texture(
27        &self,
28        _req: &GpuImageRequest,
29        _data: &[u8],
30    ) -> Result<GpuImageHandle, GpuError> {
31        Err(GpuError::Unsupported)
32    }
33    fn read_texture(&self, _handle: &GpuImageHandle) -> Result<Vec<u8>, GpuError> {
34        Err(GpuError::Unsupported)
35    }
36    fn stats(&self) -> TransferStats {
37        TransferStats::default()
38    }
39    fn take_stats(&self) -> TransferStats {
40        self.stats()
41    }
42    fn record_download(&self, _bytes: u64) {}
43
44    /// wgpu-only escape hatches used by the shader dispatch path.
45    ///
46    /// These are object-safe so plugins can call into the host backend implementation without
47    /// relying on `Any` downcasts, which are not reliable across dynamic library boundaries.
48    #[cfg(feature = "gpu-wgpu")]
49    fn wgpu_device_queue(&self) -> Option<(&wgpu::Device, &wgpu::Queue)> {
50        None
51    }
52
53    #[cfg(feature = "gpu-wgpu")]
54    fn wgpu_get_texture(&self, _handle: &GpuImageHandle) -> Option<Arc<wgpu::Texture>> {
55        None
56    }
57
58    #[cfg(feature = "gpu-wgpu")]
59    fn wgpu_register_texture(
60        &self,
61        _texture: Arc<wgpu::Texture>,
62        _format: wgpu::TextureFormat,
63        _width: u32,
64        _height: u32,
65        _usage: wgpu::TextureUsages,
66    ) -> Option<GpuImageHandle> {
67        None
68    }
69}
70
71/// Optional context trait if backends need per-thread context.
72///
73/// ```no_run
74/// use daedalus_gpu::{GpuContext, GpuBackendKind};
75/// fn kind(ctx: &dyn GpuContext) -> GpuBackendKind {
76///     ctx.backend()
77/// }
78/// let _ = kind;
79/// ```
80pub trait GpuContext: Send + Sync {
81    fn backend(&self) -> GpuBackendKind;
82    fn adapter_info(&self) -> GpuAdapterInfo;
83    fn capabilities(&self) -> GpuCapabilities;
84    fn stats(&self) -> TransferStats {
85        TransferStats::default()
86    }
87    fn take_stats(&self) -> TransferStats {
88        self.stats()
89    }
90    fn record_download(&self, _bytes: u64) {}
91}