kronos_compute/api/
mod.rs

1//! Unified Safe API for Kronos Compute
2//! 
3//! This module provides a safe, ergonomic Rust API that wraps the low-level
4//! Vulkan-style FFI interface. All Kronos optimizations (persistent descriptors,
5//! smart barriers, timeline batching, and pool allocation) work transparently.
6
7use crate::core::*;
8use crate::sys::*;
9use crate::ffi::VkResult;
10use crate::implementation;
11use thiserror::Error;
12
13pub mod context;
14pub mod buffer;
15pub mod pipeline;
16pub mod command;
17pub mod sync;
18
19#[cfg(test)]
20mod tests;
21
22pub use context::ComputeContext;
23pub use buffer::{Buffer, BufferUsage};
24pub use pipeline::{Pipeline, Shader, PipelineConfig, BufferBinding};
25pub use command::CommandBuilder;
26pub use sync::{Fence, Semaphore};
27
28/// Result type for the unified API
29pub type Result<T> = std::result::Result<T, KronosError>;
30
31/// Unified API errors
32#[derive(Error, Debug)]
33pub enum KronosError {
34    #[error("Initialization failed: {0}")]
35    InitializationFailed(String),
36    
37    #[error("Device not found")]
38    DeviceNotFound,
39    
40    #[error("Shader compilation failed: {0}")]
41    ShaderCompilationFailed(String),
42    
43    #[error("Buffer creation failed: {0}")]
44    BufferCreationFailed(String),
45    
46    #[error("Command execution failed: {0}")]
47    CommandExecutionFailed(String),
48    
49    #[error("Synchronization error: {0}")]
50    SynchronizationError(String),
51    
52    #[error("Vulkan error: {0:?}")]
53    VulkanError(VkResult),
54    
55    #[error("Implementation error: {0}")]
56    ImplementationError(#[from] implementation::error::IcdError),
57}
58
59impl From<VkResult> for KronosError {
60    fn from(result: VkResult) -> Self {
61        KronosError::VulkanError(result)
62    }
63}
64
65/// Configuration for ComputeContext creation
66#[derive(Default)]
67pub struct ContextConfig {
68    /// Application name
69    pub app_name: String,
70    /// Enable validation layers
71    pub enable_validation: bool,
72    /// Preferred GPU vendor (AMD, NVIDIA, Intel)
73    pub preferred_vendor: Option<String>,
74    /// Preferred ICD by path (only works in aggregated mode or before first initialization)
75    pub preferred_icd_path: Option<std::path::PathBuf>,
76    /// Preferred ICD by index (only works in aggregated mode or before first initialization)
77    pub preferred_icd_index: Option<usize>,
78}
79
80/// Builder for ComputeContext
81pub struct ContextBuilder {
82    config: ContextConfig,
83}
84
85impl ContextBuilder {
86    pub fn new() -> Self {
87        Self {
88            config: ContextConfig::default(),
89        }
90    }
91    
92    pub fn app_name(mut self, name: impl Into<String>) -> Self {
93        self.config.app_name = name.into();
94        self
95    }
96    
97    pub fn enable_validation(mut self) -> Self {
98        self.config.enable_validation = true;
99        self
100    }
101    
102    pub fn prefer_vendor(mut self, vendor: impl Into<String>) -> Self {
103        self.config.preferred_vendor = Some(vendor.into());
104        self
105    }
106    
107    // REMOVED: ICD preferences - Kronos is now a pure Rust implementation
108    
109    pub fn build(self) -> Result<ComputeContext> {
110        ComputeContext::new_with_config(self.config)
111    }
112}
113
114/// Entry point for the unified API
115/// 
116/// Example:
117/// ```no_run
118/// use kronos_compute::api;
119/// 
120/// let ctx = api::ComputeContext::builder()
121///     .app_name("My Compute App")
122///     .enable_validation()
123///     .build()?;
124/// ```
125impl ComputeContext {
126    /// Create a new ComputeContext with default settings
127    pub fn new() -> Result<Self> {
128        Self::builder().build()
129    }
130    
131    /// Create a builder for customized context creation
132    pub fn builder() -> ContextBuilder {
133        ContextBuilder::new()
134    }
135}