kronos_compute/implementation/
instance.rs

1//! Instance creation and management
2
3use crate::sys::*;
4use crate::core::*;
5use crate::ffi::*;
6
7/// Create a Kronos instance
8// SAFETY: This function is called from C code. Caller must ensure:
9// 1. pCreateInfo points to a valid VkInstanceCreateInfo structure
10// 2. pAllocator is either null or points to valid allocation callbacks
11// 3. pInstance points to valid memory for writing the instance handle
12// 4. All pointers remain valid for the duration of this call
13#[no_mangle]
14pub unsafe extern "C" fn vkCreateInstance(
15    pCreateInfo: *const VkInstanceCreateInfo,
16    pAllocator: *const VkAllocationCallbacks,
17    pInstance: *mut VkInstance,
18) -> VkResult {
19    // Validate inputs
20    if pCreateInfo.is_null() || pInstance.is_null() {
21        return VkResult::ErrorInitializationFailed;
22    }
23    
24    // Try to use real Vulkan driver
25    if let Some(icd) = super::icd_loader::get_icd() {
26        if let Some(create_instance_fn) = icd.create_instance {
27            let result = create_instance_fn(pCreateInfo, pAllocator, pInstance);
28            
29            // If successful, load instance functions
30            if result == VkResult::Success {
31                if let Ok(mut icd_mut) = super::icd_loader::ICD_LOADER.lock() {
32                    if let Some(icd) = icd_mut.as_mut() {
33                        let _ = super::icd_loader::load_instance_functions(icd, *pInstance);
34                    }
35                }
36            }
37            
38            return result;
39        }
40    }
41    
42    // No ICD available
43    VkResult::ErrorInitializationFailed
44}
45
46/// Destroy instance
47// SAFETY: This function is called from C code. Caller must ensure:
48// 1. instance is a valid VkInstance created by vkCreateInstance
49// 2. pAllocator matches the allocator used in vkCreateInstance (or both are null)
50// 3. All objects created from this instance have been destroyed
51#[no_mangle]
52pub unsafe extern "C" fn vkDestroyInstance(
53    instance: VkInstance,
54    pAllocator: *const VkAllocationCallbacks,
55) {
56    if instance.is_null() {
57        return;
58    }
59    
60    // Forward to real ICD if available
61    if let Some(icd) = super::forward::get_icd_if_enabled() {
62        if let Some(destroy_instance) = icd.destroy_instance {
63            destroy_instance(instance, pAllocator);
64        }
65    }
66}
67
68/// Enumerate physical devices (GPUs)
69// SAFETY: This function is called from C code. Caller must ensure:
70// 1. instance is a valid VkInstance
71// 2. pPhysicalDeviceCount points to valid memory
72// 3. If pPhysicalDevices is not null, it points to an array of at least *pPhysicalDeviceCount elements
73#[no_mangle]
74pub unsafe extern "C" fn vkEnumeratePhysicalDevices(
75    instance: VkInstance,
76    pPhysicalDeviceCount: *mut u32,
77    pPhysicalDevices: *mut VkPhysicalDevice,
78) -> VkResult {
79    if instance.is_null() || pPhysicalDeviceCount.is_null() {
80        return VkResult::ErrorInitializationFailed;
81    }
82    
83    // Forward to real ICD
84    if let Some(icd) = super::forward::get_icd_if_enabled() {
85        if let Some(enumerate_physical_devices) = icd.enumerate_physical_devices {
86            return enumerate_physical_devices(instance, pPhysicalDeviceCount, pPhysicalDevices);
87        } else {
88            log::warn!("ICD loaded but enumerate_physical_devices function pointer is null");
89        }
90    } else {
91        log::warn!("No ICD available for enumerate_physical_devices");
92    }
93    
94    // No ICD available
95    VkResult::ErrorInitializationFailed
96}
97
98/// Get physical device properties
99// SAFETY: This function is called from C code. Caller must ensure:
100// 1. physicalDevice is a valid VkPhysicalDevice obtained from vkEnumeratePhysicalDevices
101// 2. pProperties points to valid memory for a VkPhysicalDeviceProperties structure
102#[no_mangle]
103pub unsafe extern "C" fn vkGetPhysicalDeviceProperties(
104    physicalDevice: VkPhysicalDevice,
105    pProperties: *mut VkPhysicalDeviceProperties,
106) {
107    if physicalDevice.is_null() || pProperties.is_null() {
108        return;
109    }
110    
111    // Forward to real ICD
112    if let Some(icd) = super::forward::get_icd_if_enabled() {
113        if let Some(get_physical_device_properties) = icd.get_physical_device_properties {
114            get_physical_device_properties(physicalDevice, pProperties);
115        }
116    }
117}
118
119/// Get physical device memory properties
120// SAFETY: This function is called from C code. Caller must ensure:
121// 1. physicalDevice is a valid VkPhysicalDevice obtained from vkEnumeratePhysicalDevices
122// 2. pMemoryProperties points to valid memory for a VkPhysicalDeviceMemoryProperties structure
123#[no_mangle]
124pub unsafe extern "C" fn vkGetPhysicalDeviceMemoryProperties(
125    physicalDevice: VkPhysicalDevice,
126    pMemoryProperties: *mut VkPhysicalDeviceMemoryProperties,
127) {
128    if physicalDevice.is_null() || pMemoryProperties.is_null() {
129        return;
130    }
131    
132    // Forward to real ICD
133    if let Some(icd) = super::forward::get_icd_if_enabled() {
134        if let Some(get_physical_device_memory_properties) = icd.get_physical_device_memory_properties {
135            get_physical_device_memory_properties(physicalDevice, pMemoryProperties);
136        }
137    }
138}
139
140/// Get physical device queue family properties
141#[no_mangle]
142pub unsafe extern "C" fn vkGetPhysicalDeviceQueueFamilyProperties(
143    physicalDevice: VkPhysicalDevice,
144    pQueueFamilyPropertyCount: *mut u32,
145    pQueueFamilyProperties: *mut VkQueueFamilyProperties,
146) {
147    if physicalDevice.is_null() || pQueueFamilyPropertyCount.is_null() {
148        return;
149    }
150    
151    // Forward to real ICD
152    if let Some(icd) = super::forward::get_icd_if_enabled() {
153        if let Some(get_physical_device_queue_family_properties) = icd.get_physical_device_queue_family_properties {
154            get_physical_device_queue_family_properties(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
155        }
156    }
157}