hive_gpu/shaders/
metal_shaders.rs

1//! Metal Shader Management
2//!
3//! This module provides loading and management of Metal shaders for Apple Silicon.
4
5use crate::error::{Result, HiveGpuError};
6
7/// Metal shader manager for loading and compiling Metal shaders
8pub struct MetalShaderManager {
9    /// Loaded shader source
10    shader_source: String,
11}
12
13impl MetalShaderManager {
14    /// Create a new Metal shader manager
15    pub fn new() -> Self {
16        Self {
17            shader_source: Self::load_metal_shaders(),
18        }
19    }
20
21    /// Get the Metal shader source
22    pub fn get_shader_source(&self) -> &str {
23        &self.shader_source
24    }
25
26    /// Load Metal shaders from embedded source
27    fn load_metal_shaders() -> String {
28        // This will be replaced with the actual Metal shader source
29        // when we migrate the shaders from the vectorizer project
30        include_str!("../shaders/metal_hnsw.metal").to_string()
31    }
32
33    /// Validate shader source
34    pub fn validate_shader(&self) -> Result<()> {
35        if self.shader_source.is_empty() {
36            return Err(HiveGpuError::ShaderCompilationFailed("Empty shader source".to_string()));
37        }
38
39        // Basic validation - check for required functions
40        let required_functions = [
41            "cosine_similarity",
42            "euclidean_distance", 
43            "dot_product",
44            "hnsw_construction",
45            "hnsw_search"
46        ];
47
48        for function in &required_functions {
49            if !self.shader_source.contains(function) {
50                return Err(HiveGpuError::ShaderCompilationFailed(
51                    format!("Missing required function: {}", function)
52                ));
53            }
54        }
55
56        Ok(())
57    }
58}
59
60impl Default for MetalShaderManager {
61    fn default() -> Self {
62        Self::new()
63    }
64}