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