Skip to main content

oximedia_gpu/backend/
mod.rs

1//! Backend implementations for GPU and CPU compute
2
3pub mod cpu;
4pub mod vulkan;
5
6pub use cpu::CpuBackend;
7pub use vulkan::VulkanBackend;
8
9use crate::Result;
10
11/// Backend type
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum BackendType {
14    /// Vulkan backend
15    Vulkan,
16    /// Metal backend (macOS/iOS)
17    Metal,
18    /// DirectX 12 backend (Windows)
19    DX12,
20    /// CPU SIMD fallback
21    CPU,
22}
23
24impl BackendType {
25    /// Get the backend name
26    #[must_use]
27    pub fn name(self) -> &'static str {
28        match self {
29            Self::Vulkan => "Vulkan",
30            Self::Metal => "Metal",
31            Self::DX12 => "DirectX 12",
32            Self::CPU => "CPU SIMD",
33        }
34    }
35
36    /// Check if this is a GPU backend
37    #[must_use]
38    pub fn is_gpu(self) -> bool {
39        !matches!(self, Self::CPU)
40    }
41}
42
43/// Backend capabilities
44#[derive(Debug, Clone)]
45pub struct BackendCapabilities {
46    /// Backend type
47    pub backend_type: BackendType,
48    /// Maximum workgroup size (x, y, z)
49    pub max_workgroup_size: (u32, u32, u32),
50    /// Maximum workgroup invocations
51    pub max_workgroup_invocations: u32,
52    /// Maximum buffer size in bytes
53    pub max_buffer_size: u64,
54    /// Supports compute shaders
55    pub compute_shaders: bool,
56    /// Supports subgroups
57    pub subgroups: bool,
58    /// Supports push constants
59    pub push_constants: bool,
60}
61
62impl Default for BackendCapabilities {
63    fn default() -> Self {
64        Self {
65            backend_type: BackendType::Vulkan,
66            max_workgroup_size: (256, 256, 64),
67            max_workgroup_invocations: 256,
68            max_buffer_size: 1024 * 1024 * 1024, // 1 GB
69            compute_shaders: true,
70            subgroups: false,
71            push_constants: false,
72        }
73    }
74}
75
76/// Backend trait for different compute backends
77pub trait Backend {
78    /// Get backend capabilities
79    fn capabilities(&self) -> &BackendCapabilities;
80
81    /// Get backend type
82    fn backend_type(&self) -> BackendType {
83        self.capabilities().backend_type
84    }
85
86    /// Check if this backend is available on the current system
87    fn is_available() -> bool
88    where
89        Self: Sized;
90
91    /// Initialize the backend
92    fn initialize() -> Result<Self>
93    where
94        Self: Sized;
95}