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}
75
76/// Builder for ComputeContext
77pub struct ContextBuilder {
78    config: ContextConfig,
79}
80
81impl ContextBuilder {
82    pub fn new() -> Self {
83        Self {
84            config: ContextConfig::default(),
85        }
86    }
87    
88    pub fn app_name(mut self, name: impl Into<String>) -> Self {
89        self.config.app_name = name.into();
90        self
91    }
92    
93    pub fn enable_validation(mut self) -> Self {
94        self.config.enable_validation = true;
95        self
96    }
97    
98    pub fn prefer_vendor(mut self, vendor: impl Into<String>) -> Self {
99        self.config.preferred_vendor = Some(vendor.into());
100        self
101    }
102    
103    pub fn build(self) -> Result<ComputeContext> {
104        ComputeContext::new_with_config(self.config)
105    }
106}
107
108/// Entry point for the unified API
109/// 
110/// Example:
111/// ```no_run
112/// use kronos_compute::api;
113/// 
114/// let ctx = api::ComputeContext::builder()
115///     .app_name("My Compute App")
116///     .enable_validation()
117///     .build()?;
118/// ```
119impl ComputeContext {
120    /// Create a new ComputeContext with default settings
121    pub fn new() -> Result<Self> {
122        Self::builder().build()
123    }
124    
125    /// Create a builder for customized context creation
126    pub fn builder() -> ContextBuilder {
127        ContextBuilder::new()
128    }
129}