kronos_compute/implementation/
buffer.rs

1//! Buffer creation and management
2
3use crate::sys::*;
4use crate::core::*;
5use crate::ffi::*;
6use crate::implementation::icd_loader;
7
8/// Create a buffer
9// SAFETY: This function is called from C code. Caller must ensure:
10// 1. device is a valid VkDevice created by vkCreateDevice
11// 2. pCreateInfo points to a valid VkBufferCreateInfo structure
12// 3. pAllocator is either null or points to valid allocation callbacks
13// 4. pBuffer points to valid memory for writing the buffer handle
14// 5. All fields in pCreateInfo are valid (size > 0, valid usage flags, etc.)
15#[no_mangle]
16pub unsafe extern "C" fn vkCreateBuffer(
17    device: VkDevice,
18    pCreateInfo: *const VkBufferCreateInfo,
19    pAllocator: *const VkAllocationCallbacks,
20    pBuffer: *mut VkBuffer,
21) -> VkResult {
22    log::info!("=== KRONOS vkCreateBuffer called (v0.2.0-rc10) ===");
23    log::info!("device: {:?}, pCreateInfo: {:?}, pBuffer: {:?}", device, pCreateInfo, pBuffer);
24    
25    if device.is_null() || pCreateInfo.is_null() || pBuffer.is_null() {
26        log::error!("vkCreateBuffer: NULL parameter detected, returning ErrorInitializationFailed");
27        return VkResult::ErrorInitializationFailed;
28    }
29    
30    // Route via owning ICD if known
31    if let Some(icd) = icd_loader::icd_for_device(device) {
32        log::debug!("Found ICD for device {:?}", device);
33        if let Some(f) = icd.create_buffer { 
34            log::debug!("ICD has create_buffer function, calling it");
35            return f(device, pCreateInfo, pAllocator, pBuffer); 
36        } else {
37            log::error!("ICD for device {:?} does not have create_buffer function!", device);
38        }
39    } else {
40        log::warn!("No ICD found for device {:?} - checking fallback", device);
41    }
42    // Fallback
43    if let Some(icd) = super::forward::get_icd_if_enabled() {
44        log::info!("Using fallback ICD for buffer creation");
45        if let Some(create_buffer) = icd.create_buffer { 
46            log::info!("Fallback ICD has create_buffer function, calling it");
47            return create_buffer(device, pCreateInfo, pAllocator, pBuffer); 
48        } else {
49            log::error!("Fallback ICD does not have create_buffer function!");
50        }
51    }
52    log::error!("No ICD available for buffer creation - returning ErrorInitializationFailed");
53    VkResult::ErrorInitializationFailed
54}
55
56/// Destroy a buffer
57// SAFETY: This function is called from C code. Caller must ensure:
58// 1. device is a valid VkDevice
59// 2. buffer is a valid VkBuffer created by vkCreateBuffer, or VK_NULL_HANDLE
60// 3. pAllocator matches the allocator used in vkCreateBuffer (or both are null)
61// 4. The buffer is not currently bound to memory or in use by any operations
62// 5. All command buffers using this buffer have completed execution
63#[no_mangle]
64pub unsafe extern "C" fn vkDestroyBuffer(
65    device: VkDevice,
66    buffer: VkBuffer,
67    pAllocator: *const VkAllocationCallbacks,
68) {
69    if device.is_null() || buffer.is_null() {
70        return;
71    }
72    
73    if let Some(icd) = icd_loader::icd_for_device(device) {
74        if let Some(f) = icd.destroy_buffer { f(device, buffer, pAllocator); }
75        return;
76    }
77    if let Some(icd) = super::forward::get_icd_if_enabled() {
78        if let Some(destroy_buffer) = icd.destroy_buffer { destroy_buffer(device, buffer, pAllocator); }
79    }
80}
81
82/// Get buffer memory requirements
83// SAFETY: This function is called from C code. Caller must ensure:
84// 1. device is a valid VkDevice
85// 2. buffer is a valid VkBuffer created by vkCreateBuffer
86// 3. pMemoryRequirements points to valid memory for a VkMemoryRequirements structure
87// 4. The buffer has not been destroyed
88#[no_mangle]
89pub unsafe extern "C" fn vkGetBufferMemoryRequirements(
90    device: VkDevice,
91    buffer: VkBuffer,
92    pMemoryRequirements: *mut VkMemoryRequirements,
93) {
94    if device.is_null() || buffer.is_null() || pMemoryRequirements.is_null() {
95        return;
96    }
97    
98    if let Some(icd) = icd_loader::icd_for_device(device) {
99        if let Some(f) = icd.get_buffer_memory_requirements { f(device, buffer, pMemoryRequirements); }
100        return;
101    }
102    if let Some(icd) = super::forward::get_icd_if_enabled() {
103        if let Some(get_buffer_memory_requirements) = icd.get_buffer_memory_requirements { get_buffer_memory_requirements(device, buffer, pMemoryRequirements); }
104    }
105}
106
107/// Bind buffer to memory
108// SAFETY: This function is called from C code. Caller must ensure:
109// 1. device is a valid VkDevice
110// 2. buffer is a valid VkBuffer that has not been bound to memory yet
111// 3. memory is a valid VkDeviceMemory allocated with vkAllocateMemory
112// 4. memoryOffset + buffer.size <= memory.size (fits within allocated memory)
113// 5. The memory type is compatible with the buffer's memory requirements
114// 6. Neither buffer nor memory are currently in use by GPU operations
115#[no_mangle]
116pub unsafe extern "C" fn vkBindBufferMemory(
117    device: VkDevice,
118    buffer: VkBuffer,
119    memory: VkDeviceMemory,
120    memoryOffset: VkDeviceSize,
121) -> VkResult {
122    if device.is_null() || buffer.is_null() || memory.is_null() {
123        return VkResult::ErrorInitializationFailed;
124    }
125    
126    if let Some(icd) = icd_loader::icd_for_device(device) {
127        if let Some(f) = icd.bind_buffer_memory { return f(device, buffer, memory, memoryOffset); }
128    }
129    if let Some(icd) = super::forward::get_icd_if_enabled() {
130        if let Some(bind_buffer_memory) = icd.bind_buffer_memory { return bind_buffer_memory(device, buffer, memory, memoryOffset); }
131    }
132    VkResult::ErrorInitializationFailed
133}